KlokMailV1

The contract of the platform's shared mail service, KLK006 Messaging Services: mail received for an account, mail an account wants sent, and the outcome of every send. Provider neutral: Postmark's formats belong inside the mail services. Klok AI's mail (klok-api-mail) adds the mailbox a routed mail belongs to, its thread and Klok Svar's draft. The design, topics included, is KLK006 in klok/integration-solutions, and Klok AI's mail is its design-mail.md.

Property Value
Information model class A (public): clients of every tenant are to use it
Tenant / domain klok / public
Schema schema-definition-xsd/klok-mail-v1.xsd, root element KlokMail_v1
Java dev.klok.klok.datamodel:klok-mail-v1 on Maven Central, package dev.klok.klok.datamodel.public_.klok_mail_v1, catalog alias klokLibs.klokMailV1
Python klok-datamodel-klok-mail-v1 in this project's GitLab PyPI registry
TypeScript @klok-integration/datamodel-klok-mail-v1, not on npmjs.org yet: the first version of a new public package is published by a person from the pipeline's tarball (#2)
Owner Klok platform (solution KLK006)

KlokMail_v1 defines the contract for Klok's shared mail service. It describes three things: a mail received for an account, a mail an account wants sent, and the outcome of a send request. The model is provider neutral. Postmark formats live inside klok-postmark-ingress and klok-postmark-egress, not in the model, so a client never sees them. A field a provider cannot express is ignored by that provider's egress.

The model is class A, public, owned by the Klok platform as part of solution KLK006 Messaging Services. The services that publish and consume it, klok-postmark-ingress, klok-postmark-egress and klok-api-mail, are designed but not built yet, so nothing publishes or consumes the model today. When they are, an ingress receives provider webhooks and publishes inbound mail, a client publishes a send request, the egress sends it and answers with the result, and klok-api-mail routes Klok AI's mail to mailboxes and writes Klok Svar's drafts.

Records

The root element KlokMail_v1 only ties the schema together; nothing sends it. What travels is one of its three records, each on its own topic.

Record Published by (design) Key Meaning
KlokMailInboundV1 klok-postmark-ingress providerMessageId a mail received for the account; delivered at least once, the client deduplicates on providerMessageId
KlokMailOutboundV1 the client sendKey a mail the account wants sent
KlokMailSendResultV1 klok-postmark-egress sendKey the outcome of one request: SENT, REJECTED or FAILED
KlokMailDraftV1 klok-api-mail (KLK006A13) draftId Klok Svar's draft answer to the newest mail of a thread, and what a person decided about it

The shared types are KlokMailAddressV1 (address and display name), KlokMailHeaderV1 (one header), KlokMailKeyValueV1 (client metadata), KlokMailAttachmentV1 (one attachment), KlokMailMailboxRefV1 (the mailbox a routed mail belongs to) and the enumeration KlokMailSendStatusType.

Meaning of the fields

Field Meaning Source of truth
account the client account; the topic decides it, the field repeats it for consumers of several accounts the service
providerMessageId the provider's id of a received mail, and of a sent one in the result the provider
messageId the Message-ID header, with angle brackets; in a result the one the mail went out with, which replies carry in In-Reply-To the sender
sendKey the client's unique key of a mail it wants sent; correlates the result the client
status SENT: the provider accepted the mail. REJECTED: the provider refused this request, sending it again unchanged fails again. FAILED: no answer from the provider after the service's retries; the mail has not gone the service
errorCode POSTMARK_<ErrorCode> for a provider refusal, SENDER_NOT_ALLOWED or INVALID_REQUEST for the service's own, the Klok error code for a failure the service
headers every header of a received mail, for the client's own sender and auto reply checks; in a request, extra headers (the provider's own, such as From, are ignored) the sender / the client
attachments metadata always; the bytes by claimCheckUri (inbound when the account stores attachments; outbound for large files) or inline content (small outbound files) the sender / the client
htmlBodyOmitted true when the HTML of a received mail was left out because the message would not fit on the broker; the text is always kept the service
metadata the client's own name/value pairs, echoed in the result the client
mailbox the Klok AI mailbox a mail was routed to or is sent from (KlokMailMailboxRefV1: id mbx_<ULID>, kind, account acc_<ULID>, name); absent on an account's topic in klok/mail and on a notice klok-api-mail
mailbox.kind PERSONAL, FUNCTION, SVAR or KOLLEGA; a string, so a new kind is a compatible change klok-api-mail
threadId the mailbox's thread, thr_<ULID>: the thread of the mail inReplyTo or references name, else a new one klok-api-mail
KlokMailDraftV1.status READY, SENT, SENT_CHANGED (a person changed the text before sending), DROPPED, FAILED (the reason in failureReason); a string, for the same reason as kind klok-api-mail
KlokMailDraftV1.answerable false when the knowledge source did not answer the question Klok Kunskap's answer
KlokMailDraftV1.status ESCALATED KLK021: the Klok Kollega did not send by itself and asks a person; the reason in escalationReason. A person then sends or drops it as a READY draft klok-mail-api (KLK021T1, KLK021T2)
KlokMailOutboundV1.origin who wrote the mail: PERSON (a person, also when sending a draft), KOLLEGA (a Klok Kollega sent it by itself), NOTICE (a notice of the mail service); absent means PERSON klok-mail-api: KLK006A7 writes PERSON, the notices NOTICE, KLK021T1 and KLK021T2 KOLLEGA
KlokMailOutboundV1.aiGenerated true when an AI wrote the text and no person read it before it went; such a mail carries the AI label and Auto-Submitted: auto-replied; absent means false klok-mail-api (KLK021T1, KLK021T2)
KlokMailOutboundV1.kollegaMode for origin KOLLEGA: answer (an answer from the knowledge sources) or fill (a question for missing fields, or the confirmation of a filled category) klok-mail-api (KLK021T1, KLK021T2)
KlokMailDraftV1.mode the mailbox mode the draft was written in: suggest (Klok Svar), answer or fill; absent means suggest klok-mail-api (KLK006A13, KLK021T1, KLK021T2)
KlokMailDraftV1.escalated true when the Klok Kollega handed the thread to a person; stays true after a person sent or dropped the draft klok-mail-api (KLK021T1, KLK021T2)
KlokMailDraftV1.escalationReason why: NOT_ANSWERABLE, LOW_CONFIDENCE, SOURCE_REFUSED, CAP_REACHED, SENDER_NOT_APPROVED, ASKED_FOR_PERSON, TASK_STALE, FORM_GAVE_UP; a string, so a new reason is compatible klok-mail-api (KLK021T1, KLK021T2)

Why it is built this way

  • Provider neutral. Every Klok application that sends or receives mail used to carry its own Postmark webhook and API client. The model describes a mail, not a Postmark request, so the provider can change inside the two services without a change in any client. A field a provider cannot express is ignored by that provider's egress.
  • Three records in one model. Inbound mail, a send request and its result are one agreement between a client and the mail service. Three repositories would mean three pipelines and three catalog entries for one contract.
  • Class A. Applications of every tenant are to be clients, so the model is public and released on Maven Central. That makes a change expensive: a breaking change is a new major version, run in parallel for at least twelve months after the notice to every reader.
  • At least once, with a key the client owns. Postmark resends webhooks and a send can be retried, so every record carries the key a consumer deduplicates on: providerMessageId for inbound mail, sendKey for a request and its result. Every request gets exactly one result.
  • The topic decides the account, never a field. account repeats what the topic says, for consumers that read several accounts. The egress sends only from the sender addresses configured for the account whose topic the request came from, so a client cannot send in another account's name.
  • Attachments by claim check. A mail with attachments can exceed what fits comfortably on the broker. Inbound attachments are stored and referenced by a klokcc:// URI; a small outbound attachment may travel inline in content. When even the text and HTML do not fit, the HTML is dropped and htmlBodyOmitted says so.
  • The mailbox and the draft are on the event, the mailbox service's records are not. Every reader of a routed mail groups by mailbox and thread the same way, so mailbox and threadId travel with the mail. The mailbox itself, the routing table, the thread, the read state and the notice clock are klok-api-mail's own records, changed at its pace and not shared as a class A contract (design-mail.md §11).
  • Kinds and states are strings. A new value of an enumeration is a breaking change, so kind and the draft's status are strings with documented values; a reader treats a value it does not know as one it does not handle.
  • The Klok Kollega's mail is marked on the mail itself (KLK021, design.md §5.3). A mailbox in mode answer or fill sends without a person, so every reader (the egress, the thread, the app, an audit) must be able to tell an automatic mail from a person's without asking the mailbox. origin, aiGenerated and kollegaMode say it on KlokMailOutboundV1; they are optional, so a mail without them is a person's, as every mail before KLK021 was.
  • The draft is the escalation. What the Kollega does not send by itself becomes the same KlokMailDraftV1 a person already decides on in Klok Svar, with status ESCALATED and escalationReason. No second record and no second flow: a person sends, changes or drops it through the same calls.

The comment at the top of the XSD lists the topics of both namespaces; the Usage page says who publishes and reads each.

Governance

Class A: the model changes through a merge request in this repository, reviewed by the platform. Adding an optional element is compatible; removing, renaming or retyping one is not. A breaking change is a new major version (a new repository klok-mail-v2) and clients move at their own pace.