@segmentstream/ontology 0.148.0 → 0.150.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/CHANGELOG.md +52 -0
  2. package/README.md +24 -15
  3. package/dist/analyze.d.ts +11 -3
  4. package/dist/analyze.js +21 -4
  5. package/dist/diagnostics.d.ts +3 -3
  6. package/dist/diagnostics.js +1 -0
  7. package/dist/index.d.ts +1 -1
  8. package/dist/index.js +1 -1
  9. package/dist/manifest.d.ts +53 -5
  10. package/dist/manifest.js +34 -2
  11. package/dist/migrate/model.d.ts +14 -2
  12. package/dist/migrate/plan.js +1 -1
  13. package/dist/migrate/preview.js +4 -1
  14. package/dist/migrate/storage.d.ts +15 -3
  15. package/dist/migrate/storage.js +86 -3
  16. package/dist/runtime/agents.js +2 -0
  17. package/dist/runtime/chat-messages.d.ts +271 -0
  18. package/dist/runtime/chat-messages.js +576 -0
  19. package/dist/service/agent/archive-routes.d.ts +2 -0
  20. package/dist/service/agent/archive-routes.js +2 -1
  21. package/dist/service/agent/chat-archive.d.ts +12 -3
  22. package/dist/service/agent/chat-archive.js +5 -1
  23. package/dist/service/agent/chat-message-routes.d.ts +156 -0
  24. package/dist/service/agent/chat-message-routes.js +264 -0
  25. package/dist/service/agent/server.d.ts +5 -1
  26. package/dist/service/agent/server.js +15 -0
  27. package/dist/service/chat/local-reads.d.ts +71 -0
  28. package/dist/service/chat/local-reads.js +148 -0
  29. package/dist/service/chat/reads.js +7 -0
  30. package/dist/service/chat/routes.js +4 -1
  31. package/dist/service/chat/schemas.d.ts +5 -0
  32. package/dist/service/chat/schemas.js +5 -0
  33. package/dist/service/cron-secret.d.ts +6 -0
  34. package/dist/service/cron-secret.js +14 -0
  35. package/dist/service/mcp/server.js +4 -2
  36. package/dist/service/runtime-contract.d.ts +2 -0
  37. package/dist/service/schemas.d.ts +8 -2
  38. package/dist/service/service.js +4 -1
  39. package/dist/service/workflows.js +2 -9
  40. package/package.json +1 -1
@@ -428,6 +428,7 @@ export declare const MigrationPlan: Schema.Struct<{
428
428
  readonly template?: string | undefined;
429
429
  readonly separator?: string | undefined;
430
430
  } | undefined;
431
+ readonly retentionDays?: number | undefined;
431
432
  } | undefined;
432
433
  readonly workflows?: readonly {
433
434
  readonly description: string;
@@ -783,6 +784,7 @@ export declare const MigrationPlan: Schema.Struct<{
783
784
  readonly template?: string | undefined;
784
785
  readonly separator?: string | undefined;
785
786
  } | undefined;
787
+ readonly retentionDays?: number | undefined;
786
788
  } | undefined;
787
789
  readonly workflows?: readonly {
788
790
  readonly description: string;
@@ -1138,6 +1140,7 @@ export declare const MigrationPlan: Schema.Struct<{
1138
1140
  readonly template?: string | undefined;
1139
1141
  readonly separator?: string | undefined;
1140
1142
  } | undefined;
1143
+ readonly retention_days?: number | undefined;
1141
1144
  } | undefined;
1142
1145
  readonly workflows?: readonly {
1143
1146
  readonly description: string;
@@ -1493,6 +1496,7 @@ export declare const MigrationPlan: Schema.Struct<{
1493
1496
  readonly template?: string | undefined;
1494
1497
  readonly separator?: string | undefined;
1495
1498
  } | undefined;
1499
+ readonly retention_days?: number | undefined;
1496
1500
  } | undefined;
1497
1501
  readonly workflows?: readonly {
1498
1502
  readonly description: string;
@@ -1935,7 +1939,7 @@ export declare const MigrationPlan: Schema.Struct<{
1935
1939
  readonly description: Schema.String;
1936
1940
  readonly delivers: Schema.optionalKey<Schema.Literal<true>>;
1937
1941
  }>>>;
1938
- readonly chat: Schema.optionalKey<Schema.Struct<{
1942
+ readonly chat: Schema.optionalKey<Schema.encodeKeys<Schema.Struct<{
1939
1943
  readonly telegram: Schema.optionalKey<Schema.Struct<{
1940
1944
  readonly connection: Schema.String;
1941
1945
  }>>;
@@ -1956,6 +1960,9 @@ export declare const MigrationPlan: Schema.Struct<{
1956
1960
  readonly template: Schema.optionalKey<Schema.String>;
1957
1961
  readonly separator: Schema.optionalKey<Schema.String>;
1958
1962
  }>>;
1963
+ readonly retentionDays: Schema.optionalKey<Schema.Int>;
1964
+ }>, {
1965
+ readonly retentionDays: "retention_days";
1959
1966
  }>>;
1960
1967
  readonly inbox: Schema.optionalKey<Schema.Struct<{
1961
1968
  readonly roles: Schema.$Array<Schema.String>;
@@ -2286,7 +2293,7 @@ export declare const MigrationPlan: Schema.Struct<{
2286
2293
  readonly description: Schema.String;
2287
2294
  readonly delivers: Schema.optionalKey<Schema.Literal<true>>;
2288
2295
  }>>>;
2289
- readonly chat: Schema.optionalKey<Schema.Struct<{
2296
+ readonly chat: Schema.optionalKey<Schema.encodeKeys<Schema.Struct<{
2290
2297
  readonly telegram: Schema.optionalKey<Schema.Struct<{
2291
2298
  readonly connection: Schema.String;
2292
2299
  }>>;
@@ -2307,6 +2314,9 @@ export declare const MigrationPlan: Schema.Struct<{
2307
2314
  readonly template: Schema.optionalKey<Schema.String>;
2308
2315
  readonly separator: Schema.optionalKey<Schema.String>;
2309
2316
  }>>;
2317
+ readonly retentionDays: Schema.optionalKey<Schema.Int>;
2318
+ }>, {
2319
+ readonly retentionDays: "retention_days";
2310
2320
  }>>;
2311
2321
  readonly inbox: Schema.optionalKey<Schema.Struct<{
2312
2322
  readonly roles: Schema.$Array<Schema.String>;
@@ -2564,6 +2574,7 @@ export declare const MigrationPlan: Schema.Struct<{
2564
2574
  readonly template?: string | undefined;
2565
2575
  readonly separator?: string | undefined;
2566
2576
  } | undefined;
2577
+ readonly retentionDays?: number | undefined;
2567
2578
  } | undefined;
2568
2579
  readonly workflows?: readonly {
2569
2580
  readonly description: string;
@@ -2919,6 +2930,7 @@ export declare const MigrationPlan: Schema.Struct<{
2919
2930
  readonly template?: string | undefined;
2920
2931
  readonly separator?: string | undefined;
2921
2932
  } | undefined;
2933
+ readonly retentionDays?: number | undefined;
2922
2934
  } | undefined;
2923
2935
  readonly workflows?: readonly {
2924
2936
  readonly description: string;
@@ -213,7 +213,7 @@ export function planMigration(input) {
213
213
  draft.runtimeTables.push(step({
214
214
  kind: "createCatalog",
215
215
  classification: "additive",
216
- description: "Create the runtime's agent tables, which pair channel accounts with members.",
216
+ description: "Create the runtime's agent tables, which pair channel accounts with members and keep chats' messages.",
217
217
  sql: AGENT_TABLES_DDL,
218
218
  }));
219
219
  }
@@ -2,7 +2,7 @@ import { Effect } from "effect";
2
2
  import { AGENT_TASKS_TABLE, ATTACHMENTS_TABLE, SIGN_IN_EMAILS_TABLE, WORKFLOW_STARTS_TABLE, } from "../storage/tables.js";
3
3
  import { CONFIRMATION_TABLE } from "../runtime/confirmations.js";
4
4
  import { CHANNEL_ORIGIN_SQL, USER_IDENTITY_TABLE } from "../runtime/user-identity-sql.js";
5
- import { AGENT_COUNTERS_TABLE, AGENT_GUEST_LINKS_TABLE, AGENT_IDENTITIES_TABLE, AGENT_PAIRINGS_TABLE, API_KEYS_TABLE, IDENTITY_NOTICES_TABLE, OAUTH_TABLES, quoteIdentifier, } from "./storage.js";
5
+ import { AGENT_COUNTERS_TABLE, AGENT_GUEST_LINKS_TABLE, AGENT_IDENTITIES_TABLE, AGENT_PAIRINGS_TABLE, API_KEYS_TABLE, CHAT_MESSAGES_TABLE, IDENTITY_NOTICES_TABLE, OAUTH_TABLES, quoteIdentifier, } from "./storage.js";
6
6
  /**
7
7
  * Invalidate copied SDK credentials and runnable state on a verified, unexposed child database.
8
8
  * The caller owns branch verification and provider-side Neon Auth invalidation/configuration.
@@ -32,6 +32,9 @@ export function preparePreviewDatabase({ sql, }) {
32
32
  IDENTITY_NOTICES_TABLE,
33
33
  AGENT_PAIRINGS_TABLE,
34
34
  AGENT_GUEST_LINKS_TABLE,
35
+ // A preview is a child branch of production: production's chats, people's messages
36
+ // among them, must not reach it, nor a preview's agent read them as its own.
37
+ CHAT_MESSAGES_TABLE,
35
38
  AGENT_TASKS_TABLE,
36
39
  SIGN_IN_EMAILS_TABLE,
37
40
  WORKFLOW_STARTS_TABLE,
@@ -205,13 +205,25 @@ export declare const CHANNEL_LIMITS_TABLE = "_channel_limits";
205
205
  * other user, nor a queued sign-in email, which needs an existing user, holds these.
206
206
  */
207
207
  export declare const AGENT_GUEST_LINKS_TABLE = "_agent_guest_links";
208
+ /**
209
+ * The chats' messages, one row per message: what people and staff saw, with what the agent did
210
+ * with each inbound message (`disposition`) and the latest delivery state of each outbound one.
211
+ * A system table beside `agent_chat`, which stays the chat's record: no entity, history,
212
+ * replication or generated tools. See docs/agent-chat-storage.md.
213
+ */
214
+ export declare const CHAT_MESSAGES_TABLE = "_chat_messages";
215
+ /**
216
+ * Where a chat message's `revision` comes from: set on insert and on every change readers see, so
217
+ * that "what changed since my bookmark" is one indexed range of a chat.
218
+ */
219
+ export declare const CHAT_MESSAGE_REVISIONS = "_chat_messages_revision";
208
220
  /** The agent's tables, in the order they are created. */
209
221
  export declare const AGENT_TABLES: ReadonlyArray<string>;
210
222
  /**
211
223
  * The tables of the app's agent: pairing codes, stored only as SHA-256 hashes, the legacy channel
212
- * identities they create, and the first-contact notices of `user_identity` channel accounts. An
213
- * account and a member each have at most one live identity per provider. Every statement is
214
- * idempotent.
224
+ * identities they create, the first-contact notices of `user_identity` channel accounts, and the
225
+ * chats' messages. An account and a member each have at most one live identity per provider.
226
+ * Every statement is idempotent.
215
227
  */
216
228
  export declare const AGENT_TABLES_DDL: ReadonlyArray<string>;
217
229
  /**
@@ -614,6 +614,18 @@ export const CHANNEL_LIMITS_TABLE = "_channel_limits";
614
614
  * other user, nor a queued sign-in email, which needs an existing user, holds these.
615
615
  */
616
616
  export const AGENT_GUEST_LINKS_TABLE = "_agent_guest_links";
617
+ /**
618
+ * The chats' messages, one row per message: what people and staff saw, with what the agent did
619
+ * with each inbound message (`disposition`) and the latest delivery state of each outbound one.
620
+ * A system table beside `agent_chat`, which stays the chat's record: no entity, history,
621
+ * replication or generated tools. See docs/agent-chat-storage.md.
622
+ */
623
+ export const CHAT_MESSAGES_TABLE = "_chat_messages";
624
+ /**
625
+ * Where a chat message's `revision` comes from: set on insert and on every change readers see, so
626
+ * that "what changed since my bookmark" is one indexed range of a chat.
627
+ */
628
+ export const CHAT_MESSAGE_REVISIONS = "_chat_messages_revision";
617
629
  /** The agent's tables, in the order they are created. */
618
630
  export const AGENT_TABLES = [
619
631
  AGENT_PAIRINGS_TABLE,
@@ -624,12 +636,82 @@ export const AGENT_TABLES = [
624
636
  CONFIRMATION_TABLE,
625
637
  IDENTITY_NOTICES_TABLE,
626
638
  AGENT_GUEST_LINKS_TABLE,
639
+ CHAT_MESSAGES_TABLE,
640
+ ];
641
+ /**
642
+ * The chat messages' table, its revision sequence, and their keys: one row per provider message
643
+ * and one per send's idempotency key. Ids are made in the service (UUIDv7), so nothing depends on
644
+ * a PostgreSQL version. Every statement is idempotent.
645
+ */
646
+ const CHAT_MESSAGES_DDL = [
647
+ `CREATE SEQUENCE IF NOT EXISTS "${CHAT_MESSAGE_REVISIONS}" AS bigint`,
648
+ [
649
+ `CREATE TABLE IF NOT EXISTS "${CHAT_MESSAGES_TABLE}" (`,
650
+ ` "id" uuid NOT NULL,`,
651
+ ` "channel" text NOT NULL,`,
652
+ ` "chat_id" text NOT NULL,`,
653
+ ` "thread_id" text,`,
654
+ ` "chat_type" text NOT NULL,`,
655
+ ` "participants" uuid[] NOT NULL DEFAULT '{}',`,
656
+ ` "direction" text NOT NULL,`,
657
+ ` "author" text NOT NULL,`,
658
+ ` "sender_id" text,`,
659
+ ` "sender_name" text,`,
660
+ ` "user_id" uuid,`,
661
+ ` "message_id" text,`,
662
+ ` "reply_to" text,`,
663
+ ` "text" text,`,
664
+ ` "parts" jsonb NOT NULL DEFAULT '[]',`,
665
+ ` "files" jsonb NOT NULL DEFAULT '[]',`,
666
+ ` "reactions" jsonb NOT NULL DEFAULT '[]',`,
667
+ ` "at" timestamptz NOT NULL,`,
668
+ ` "edited_at" timestamptz,`,
669
+ ` "deleted_at" timestamptz,`,
670
+ ` "status" text,`,
671
+ ` "failure" text,`,
672
+ ` "operation" text,`,
673
+ ` "fingerprint" text,`,
674
+ ` "disposition" text,`,
675
+ ` "reason" text,`,
676
+ ` "disposition_at" timestamptz,`,
677
+ ` "session_id" text,`,
678
+ ` "turn_id" text,`,
679
+ ` "meta" jsonb,`,
680
+ ` "imported_at" timestamptz,`,
681
+ ` "revision" bigint NOT NULL DEFAULT nextval('"${CHAT_MESSAGE_REVISIONS}"'),`,
682
+ ` "purged_at" timestamptz,`,
683
+ ` "replaced_by" uuid,`,
684
+ ` CONSTRAINT "${CHAT_MESSAGES_TABLE}_pkey" PRIMARY KEY ("id"),`,
685
+ ` CONSTRAINT "${CHAT_MESSAGES_TABLE}_chat_type_check" ` +
686
+ `CHECK ("chat_type" IN ('private', 'group', 'channel')),`,
687
+ ` CONSTRAINT "${CHAT_MESSAGES_TABLE}_direction_check" ` +
688
+ `CHECK ("direction" IN ('inbound', 'outbound', 'internal')),`,
689
+ ` CONSTRAINT "${CHAT_MESSAGES_TABLE}_author_check" ` +
690
+ `CHECK ("author" IN ('human', 'agent', 'bot')),`,
691
+ ` CONSTRAINT "${CHAT_MESSAGES_TABLE}_status_check" ` +
692
+ `CHECK ("status" IN ('pending', 'sent', 'delivered', 'read', 'failed', 'uncertain')),`,
693
+ ` CONSTRAINT "${CHAT_MESSAGES_TABLE}_disposition_check" ` +
694
+ `CHECK ("disposition" IN ('claimed', 'turn', 'skipped'))`,
695
+ `)`,
696
+ ].join("\n"),
697
+ // One row per provider message: a redelivery, an echo or an import of it changes nothing.
698
+ `CREATE UNIQUE INDEX IF NOT EXISTS "${CHAT_MESSAGES_TABLE}_message_key" ` +
699
+ `ON "${CHAT_MESSAGES_TABLE}" ("channel", "chat_id", "message_id") ` +
700
+ `WHERE "message_id" IS NOT NULL`,
701
+ // One send per idempotency key: a retry finds its row, and a tombstone keeps the key.
702
+ `CREATE UNIQUE INDEX IF NOT EXISTS "${CHAT_MESSAGES_TABLE}_operation_key" ` +
703
+ `ON "${CHAT_MESSAGES_TABLE}" ("operation") WHERE "operation" IS NOT NULL`,
704
+ `CREATE INDEX IF NOT EXISTS "${CHAT_MESSAGES_TABLE}_chat_at_idx" ` +
705
+ `ON "${CHAT_MESSAGES_TABLE}" ("channel", "chat_id", "at", "id")`,
706
+ `CREATE INDEX IF NOT EXISTS "${CHAT_MESSAGES_TABLE}_chat_revision_idx" ` +
707
+ `ON "${CHAT_MESSAGES_TABLE}" ("channel", "chat_id", "revision")`,
708
+ `CREATE INDEX IF NOT EXISTS "${CHAT_MESSAGES_TABLE}_at_idx" ON "${CHAT_MESSAGES_TABLE}" ("at")`,
627
709
  ];
628
710
  /**
629
711
  * The tables of the app's agent: pairing codes, stored only as SHA-256 hashes, the legacy channel
630
- * identities they create, and the first-contact notices of `user_identity` channel accounts. An
631
- * account and a member each have at most one live identity per provider. Every statement is
632
- * idempotent.
712
+ * identities they create, the first-contact notices of `user_identity` channel accounts, and the
713
+ * chats' messages. An account and a member each have at most one live identity per provider.
714
+ * Every statement is idempotent.
633
715
  */
634
716
  export const AGENT_TABLES_DDL = [
635
717
  ...CONFIRMATION_DDL,
@@ -723,6 +805,7 @@ export const AGENT_TABLES_DDL = [
723
805
  )`,
724
806
  `CREATE INDEX IF NOT EXISTS "${AGENT_GUEST_LINKS_TABLE}_created_idx" ` +
725
807
  `ON "${AGENT_GUEST_LINKS_TABLE}" ("created_at")`,
808
+ ...CHAT_MESSAGES_DDL,
726
809
  ];
727
810
  /**
728
811
  * The runtime's own tables beside the catalog. They are created with it, after the entity tables
@@ -17,6 +17,7 @@ import { resolveAgentIdentity } from "./resolve-agent-identity.js";
17
17
  import { completeAgentLimitNotice } from "./agent-counters.js";
18
18
  import { isMemberBlocked, setMemberBlocked, resolveMemberById } from "./principals.js";
19
19
  import { sqlAgentWebStore } from "./agent-web.js";
20
+ import { sqlChatMessageStore } from "./chat-messages.js";
20
21
  import { guestChat } from "./guests.js";
21
22
  import { WEB_PROVIDER } from "../service/runtime-identity.js";
22
23
  import { isRecordId } from "./records.js";
@@ -1012,6 +1013,7 @@ export function sqlAgentStore(ontology, sql) {
1012
1013
  : timestampText(record.decided_at),
1013
1014
  }));
1014
1015
  }).pipe(Effect.orDie),
1016
+ chatMessages: sqlChatMessageStore(sql),
1015
1017
  web: sqlAgentWebStore(ontology, sql, {
1016
1018
  signUp: (visitorId) => resolveChannelSignIn(ontology, sql, { provider: WEB_PROVIDER, externalId: visitorId }),
1017
1019
  }),
@@ -0,0 +1,271 @@
1
+ import { Effect, Schema } from "effect";
2
+ import type { SqlClient } from "effect/unstable/sql";
3
+ /**
4
+ * The workspace's chat store: one row per message in `_chat_messages` (docs/agent-chat-storage.md,
5
+ * "The workspace chat store"). A message's state is its latest state: edits, deletions, reactions,
6
+ * delivery statuses and what the agent did with it change its row and take a new `revision`.
7
+ */
8
+ export declare const CHAT_TYPES: readonly ["private", "group", "channel"];
9
+ export declare const CHAT_DIRECTIONS: readonly ["inbound", "outbound", "internal"];
10
+ export declare const CHAT_MESSAGE_AUTHORS: readonly ["human", "agent", "bot"];
11
+ export declare const CHAT_MESSAGE_STATUSES: readonly ["pending", "sent", "delivered", "read", "failed", "uncertain"];
12
+ export declare const CHAT_DISPOSITIONS: readonly ["claimed", "turn", "skipped"];
13
+ export type ChatType = (typeof CHAT_TYPES)[number];
14
+ export type ChatDirection = (typeof CHAT_DIRECTIONS)[number];
15
+ export type ChatMessageAuthor = (typeof CHAT_MESSAGE_AUTHORS)[number];
16
+ export type ChatMessageStatus = (typeof CHAT_MESSAGE_STATUSES)[number];
17
+ export type ChatDisposition = (typeof CHAT_DISPOSITIONS)[number];
18
+ /** How long a claim on an inbound message holds: an abandoned one is taken again after it. */
19
+ export declare const CHAT_CLAIM_MINUTES = 10;
20
+ /** The most rows one purge batch deletes or scrubs, and the most batches one purge runs. */
21
+ export declare const CHAT_PURGE_BATCH = 1000;
22
+ export declare const CHAT_PURGE_BATCHES = 20;
23
+ /** A stored file of a message, by its `_agent_attachments` row. */
24
+ export interface ChatMessageFile {
25
+ readonly id: string;
26
+ readonly name?: string;
27
+ readonly type?: string;
28
+ readonly size?: number;
29
+ }
30
+ /** A reaction a message carries now: one per sender. */
31
+ export interface ChatReaction {
32
+ readonly senderId: string;
33
+ readonly emoji: string;
34
+ }
35
+ /** One message as the store holds it. Times are ISO 8601; `revision` is a decimal. */
36
+ export interface ChatMessage {
37
+ readonly id: string;
38
+ readonly channel: string;
39
+ readonly chatId: string;
40
+ readonly threadId: string | null;
41
+ readonly chatType: ChatType;
42
+ readonly participants: ReadonlyArray<string>;
43
+ readonly direction: ChatDirection;
44
+ readonly author: ChatMessageAuthor;
45
+ readonly senderId: string | null;
46
+ readonly senderName: string | null;
47
+ readonly userId: string | null;
48
+ readonly messageId: string | null;
49
+ readonly replyTo: string | null;
50
+ readonly text: string | null;
51
+ readonly parts: ReadonlyArray<unknown>;
52
+ readonly files: ReadonlyArray<ChatMessageFile>;
53
+ readonly reactions: ReadonlyArray<ChatReaction>;
54
+ readonly at: string;
55
+ readonly editedAt: string | null;
56
+ readonly deletedAt: string | null;
57
+ readonly status: ChatMessageStatus | null;
58
+ readonly failure: string | null;
59
+ readonly operation: string | null;
60
+ readonly disposition: ChatDisposition | null;
61
+ readonly reason: string | null;
62
+ readonly dispositionAt: string | null;
63
+ readonly sessionId: string | null;
64
+ readonly turnId: string | null;
65
+ readonly meta: unknown;
66
+ readonly importedAt: string | null;
67
+ readonly revision: string;
68
+ /**
69
+ * The message this row became: a provider's echo the send's own row took over. Only reads after
70
+ * a revision return such a row, so a reader that kept it drops it.
71
+ */
72
+ readonly replacedBy: string | null;
73
+ }
74
+ /** A message to record. */
75
+ export interface ChatMessageInput {
76
+ readonly channel: string;
77
+ readonly chatId: string;
78
+ readonly threadId?: string;
79
+ readonly chatType: ChatType;
80
+ readonly participants: ReadonlyArray<string>;
81
+ readonly direction: ChatDirection;
82
+ readonly author: ChatMessageAuthor;
83
+ readonly senderId?: string;
84
+ readonly senderName?: string;
85
+ readonly userId?: string;
86
+ readonly messageId?: string;
87
+ readonly replyTo?: string;
88
+ readonly text?: string;
89
+ readonly parts?: ReadonlyArray<unknown>;
90
+ readonly files?: ReadonlyArray<ChatMessageFile>;
91
+ readonly at?: string;
92
+ readonly status?: ChatMessageStatus;
93
+ readonly operation?: string;
94
+ readonly fingerprint?: string;
95
+ readonly sessionId?: string;
96
+ readonly turnId?: string;
97
+ readonly meta?: unknown;
98
+ }
99
+ /** The message a change is to, by the provider's id of it. */
100
+ interface Target {
101
+ readonly channel: string;
102
+ readonly chatId: string;
103
+ readonly messageId: string;
104
+ }
105
+ /** One change to a message. `sent` finds its message by the send's `operation` instead. */
106
+ export type ChatMessageChange =
107
+ /**
108
+ * The person's edit, or the agent's of its own message. It may carry the revision's routing
109
+ * state, in the same write and only while the message holds the `meta` it replaces
110
+ * (`expected`, `null` for none), and its files, which replace the message's: an edit that only
111
+ * replaced a file is an edit too.
112
+ */
113
+ (Target & {
114
+ readonly change: "edit";
115
+ readonly text: string;
116
+ readonly at?: string;
117
+ readonly meta?: unknown;
118
+ readonly expected?: unknown;
119
+ readonly files?: ReadonlyArray<ChatMessageFile>;
120
+ }) | (Target & {
121
+ readonly change: "delete";
122
+ readonly at?: string;
123
+ }) | (Target & {
124
+ readonly change: "reaction";
125
+ readonly senderId: string;
126
+ readonly emoji: string;
127
+ }) | (Target & {
128
+ readonly change: "status";
129
+ readonly status: "sent" | "delivered" | "read" | "failed";
130
+ readonly failure?: string;
131
+ }) | (Target & {
132
+ readonly change: "disposition";
133
+ readonly disposition: ChatDisposition;
134
+ readonly reason?: string;
135
+ readonly sessionId?: string;
136
+ readonly turnId?: string;
137
+ }) | {
138
+ readonly change: "sent";
139
+ readonly channel: string;
140
+ readonly chatId: string;
141
+ readonly operation: string;
142
+ readonly status: "sent" | "failed" | "uncertain";
143
+ readonly providerMessageId?: string;
144
+ readonly failure?: string;
145
+ }
146
+ /** A claim given up before its message was dispatched: the message is unclaimed again. */
147
+ | (Target & {
148
+ readonly change: "release";
149
+ })
150
+ /**
151
+ * What a channel read of the message after recording it, with its routing state, in one write
152
+ * that applies only while the message holds the `meta` it replaces (`null` for none): its `text`
153
+ * with what was transcribed of it, such as a voice message's transcript after its caption, and
154
+ * its stored files, by their `_agent_attachments` rows. Never an edit: `edited_at` stays.
155
+ */
156
+ | (Target & {
157
+ readonly change: "content";
158
+ readonly meta: unknown;
159
+ readonly expected: unknown;
160
+ readonly text?: string;
161
+ readonly files?: ReadonlyArray<ChatMessageFile>;
162
+ }) | (Target & {
163
+ readonly change: "meta";
164
+ readonly meta: unknown;
165
+ /**
166
+ * The `meta` the change replaces, when present: it applies only while the message still holds
167
+ * it (`null` for none), so two writers of one message never lose each other's state.
168
+ */
169
+ readonly expected?: unknown;
170
+ });
171
+ /**
172
+ * A place in a chat, newest first: messages before `at`, and at `at` those whose id sorts before
173
+ * `id`. Written `<ISO time>~<id>`, or `<ISO time>` alone.
174
+ */
175
+ export interface ChatCursor {
176
+ readonly at: string;
177
+ readonly id?: string;
178
+ }
179
+ /** The cursor a page continues at; `undefined` for text that is none. */
180
+ export declare function parseChatCursor(value: string): ChatCursor | undefined;
181
+ export declare function chatCursorOf(message: Pick<ChatMessage, "at" | "id">): string;
182
+ /**
183
+ * The runtime's reads of one chat, or with `pending` or `senderId` of every chat of a channel.
184
+ */
185
+ export interface ChatMessageQuery {
186
+ readonly channel: string;
187
+ readonly chatId?: string;
188
+ /** A thread's messages; `null` for those in no thread; absent for every thread. */
189
+ readonly threadId?: string | null;
190
+ /** The messages changed after this revision, oldest change first. */
191
+ readonly after?: string;
192
+ readonly before?: ChatCursor;
193
+ readonly messageId?: string;
194
+ /** Outbound messages still `pending`: sends still to deliver. */
195
+ readonly pending?: boolean;
196
+ /** With `pending`, only the sends requested no later than this time (ISO 8601). */
197
+ readonly pendingBefore?: string;
198
+ /** The messages a sender wrote, such as the chat a person last wrote from. */
199
+ readonly senderId?: string;
200
+ readonly limit: number;
201
+ }
202
+ /** A reader's search of one chat, as `chat_history` and `chat_search` ask it. */
203
+ export interface ChatMessageSearch {
204
+ readonly channel: string;
205
+ readonly chatId: string;
206
+ readonly threadId?: string;
207
+ readonly q?: string;
208
+ readonly senderId?: string;
209
+ readonly from?: string;
210
+ readonly to?: string;
211
+ readonly after?: string;
212
+ readonly before?: ChatCursor;
213
+ readonly limit: number;
214
+ }
215
+ /**
216
+ * Who reads, as the chat archive's reader rule takes it: every group message (`groups`, from
217
+ * `agent_chat:read` or membership of the chat), this exact chat's messages, never its internal
218
+ * rows nor sends still unsent (`chat`, a chat the ontology checked the reader may read), and the
219
+ * private messages whose `participants` hold `participantId`, row by row.
220
+ */
221
+ export interface ChatMessageReader {
222
+ readonly participantId: string | null;
223
+ readonly groups: boolean;
224
+ readonly chat: boolean;
225
+ }
226
+ export interface ChatMessagePage {
227
+ readonly messages: ReadonlyArray<ChatMessage>;
228
+ /** `after` mode: the revision to read after next; otherwise the cursor of the next page. */
229
+ readonly nextCursor: string | null;
230
+ }
231
+ declare const ChatMessageConflict_base: Schema.Class<ChatMessageConflict, Schema.TaggedStruct<"ChatMessageConflict", {
232
+ readonly code: Schema.Literals<readonly ["operation_conflict", "message_conflict"]>;
233
+ }>, import("effect/Cause").YieldableError>;
234
+ /** An operation replayed with another request, or a provider id another send holds. */
235
+ export declare class ChatMessageConflict extends ChatMessageConflict_base {
236
+ }
237
+ export interface ChatMessageStore {
238
+ /** Records a message once by `(channel, chatId, messageId)` and once by `operation`. */
239
+ readonly record: (input: ChatMessageInput) => Effect.Effect<{
240
+ readonly id: string;
241
+ readonly first: boolean;
242
+ }, ChatMessageConflict>;
243
+ /** Applies one change; `applied` is false when there was nothing to change. */
244
+ readonly change: (input: ChatMessageChange) => Effect.Effect<{
245
+ readonly applied: boolean;
246
+ }, ChatMessageConflict>;
247
+ readonly read: (input: ChatMessageQuery) => Effect.Effect<ChatMessagePage>;
248
+ /** Whether the store holds a message of the chat. */
249
+ readonly holds: (chat: {
250
+ readonly channel: string;
251
+ readonly chatId: string;
252
+ }) => Effect.Effect<boolean>;
253
+ /** One chat's messages as a reader may read them, row by row. */
254
+ readonly search: (search: ChatMessageSearch, reader: ChatMessageReader) => Effect.Effect<ChatMessagePage>;
255
+ /**
256
+ * Deletes the messages older than `retentionDays`, in bounded batches, and scrubs a send's row to
257
+ * its tombstone instead: content and coordinates go, its `operation` key and outcome stay, so a
258
+ * replay of the key never sends again. Kept files are not touched.
259
+ */
260
+ readonly purge: (input: {
261
+ readonly retentionDays: number;
262
+ }) => Effect.Effect<{
263
+ readonly deleted: number;
264
+ readonly scrubbed: number;
265
+ /** The last batch was full: older messages may remain for the next run. */
266
+ readonly more: boolean;
267
+ }>;
268
+ }
269
+ /** The chat store on `_chat_messages`, as `ontology_app`. Failures are defects. */
270
+ export declare function sqlChatMessageStore(sql: SqlClient.SqlClient): ChatMessageStore;
271
+ export {};