@segmentstream/ontology 0.96.0 → 0.98.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 (50) hide show
  1. package/README.md +15 -3
  2. package/dist/agent-review.d.ts +95 -0
  3. package/dist/agent-review.js +200 -0
  4. package/dist/analyze.d.ts +10 -2
  5. package/dist/analyze.js +240 -19
  6. package/dist/bin/check.js +2 -0
  7. package/dist/chat.d.ts +16 -0
  8. package/dist/diagnostics.d.ts +3 -3
  9. package/dist/diagnostics.js +2 -0
  10. package/dist/index.d.ts +5 -1
  11. package/dist/index.js +2 -0
  12. package/dist/manifest.d.ts +505 -2
  13. package/dist/manifest.js +55 -2
  14. package/dist/migrate/model.d.ts +272 -0
  15. package/dist/ontology.d.ts +3 -0
  16. package/dist/ontology.js +1 -0
  17. package/dist/permissions.d.ts +5 -3
  18. package/dist/permissions.js +9 -5
  19. package/dist/runtime/agents.js +53 -0
  20. package/dist/service/agent/protocol.d.ts +18 -0
  21. package/dist/service/agent/protocol.js +33 -0
  22. package/dist/service/agent/server.js +48 -1
  23. package/dist/service/catalog.js +10 -1
  24. package/dist/service/chat/archive.d.ts +2 -1
  25. package/dist/service/chat/delivery.js +11 -0
  26. package/dist/service/chat/envelope.d.ts +16 -16
  27. package/dist/service/chat/providers/provider.d.ts +2 -0
  28. package/dist/service/chat/providers/slack.js +1 -1
  29. package/dist/service/chat/providers/telegram.d.ts +6 -1
  30. package/dist/service/chat/providers/telegram.js +14 -3
  31. package/dist/service/chat/providers/whatsapp.js +1 -1
  32. package/dist/service/chat/review-delivery.d.ts +38 -0
  33. package/dist/service/chat/review-delivery.js +258 -0
  34. package/dist/service/connections.js +1 -0
  35. package/dist/service/discovery-search.d.ts +1 -1
  36. package/dist/service/mcp/runtime-policy.js +5 -1
  37. package/dist/service/mcp/server.js +13 -1
  38. package/dist/service/mcp/tools.d.ts +5 -0
  39. package/dist/service/mcp/tools.js +30 -0
  40. package/dist/service/operations.d.ts +3 -1
  41. package/dist/service/operations.js +16 -1
  42. package/dist/service/runtime-contract.d.ts +28 -0
  43. package/dist/service/schemas.d.ts +62 -0
  44. package/dist/service/toolsets.d.ts +49 -0
  45. package/dist/service/toolsets.js +284 -0
  46. package/dist/slack.js +3 -1
  47. package/dist/toolset.d.ts +141 -0
  48. package/dist/toolset.js +154 -0
  49. package/dist/whatsapp.js +3 -1
  50. package/package.json +1 -1
package/dist/manifest.js CHANGED
@@ -351,6 +351,23 @@ export const WorkflowManifest = Schema.Struct({
351
351
  connections: Schema.Array(Schema.String),
352
352
  triggers: Schema.Array(WorkflowTriggerManifest),
353
353
  });
354
+ /**
355
+ * A provider's MCP tools the ontology serves, `defineOntology({ toolsets })`: the connection's MCP
356
+ * API, each tool's declared access and fixed values, and the action the catalog serves it as, whose
357
+ * `connections` is the toolset's connection. A toolset has no records.
358
+ */
359
+ export const ToolsetManifest = Schema.Struct({
360
+ name: Schema.String,
361
+ description: Schema.String,
362
+ connection: Schema.String,
363
+ api: Schema.String,
364
+ tools: Schema.Array(Schema.Struct({
365
+ name: Schema.String,
366
+ access: Schema.Literals(["read", "write"]),
367
+ fixed: Schema.Record(Schema.String, Schema.Union([Schema.String, Schema.Number, Schema.Boolean])),
368
+ })),
369
+ actions: Schema.Array(ActionManifest),
370
+ });
354
371
  /** A workspace connection the ontology's actions call, by its key in `connections/connections.py`. */
355
372
  export const ConnectionManifest = Schema.Struct({
356
373
  key: Schema.String,
@@ -450,6 +467,11 @@ const OntologyManifestModel = Schema.Struct({
450
467
  * The workflows, since version 9. Written only when there are some; absent reads as none.
451
468
  */
452
469
  workflows: Schema.optionalKey(Schema.Array(WorkflowManifest)),
470
+ /**
471
+ * Provider MCP tools the ontology serves, within version 15. Written only when there are some;
472
+ * absent reads as none, and older readers ignore it.
473
+ */
474
+ toolsets: Schema.optionalKey(Schema.Array(ToolsetManifest)),
453
475
  });
454
476
  /** A property in the snake_case wire spelling, for records that store definitions. */
455
477
  export const PropertyManifestWire = PropertyManifest.pipe(Schema.encodeKeys({
@@ -731,6 +753,7 @@ function entityManifest(input) {
731
753
  /** Builds the manifest of a resolved ontology that passed `check`. */
732
754
  export function buildManifest(input) {
733
755
  const connections = input.connections ?? [];
756
+ const toolsets = (input.toolsets ?? []).map(toolsetManifest);
734
757
  const emails = input.emails ?? {};
735
758
  const metrics = input.metrics ?? [];
736
759
  const workflows = input.workflows ?? [];
@@ -794,12 +817,42 @@ export function buildManifest(input) {
794
817
  : {}),
795
818
  ...(metrics.length > 0 ? { metrics } : {}),
796
819
  ...(workflows.length > 0 ? { workflows } : {}),
820
+ ...(toolsets.length > 0 ? { toolsets } : {}),
797
821
  };
798
822
  return { ...body, hash: manifestHash(encodeManifest({ ...body, hash: "" })) };
799
823
  }
800
- /** The keys of every connection the manifest's actions call, without duplicates. */
824
+ /** A checked toolset as the manifest lists it: its tools, and the action each is served as. */
825
+ export function toolsetManifest(resolved) {
826
+ return {
827
+ name: resolved.name,
828
+ description: resolved.description,
829
+ connection: resolved.connection,
830
+ api: resolved.api,
831
+ tools: resolved.tools.map((tool) => ({
832
+ name: tool.name,
833
+ access: tool.access,
834
+ fixed: { ...tool.fixed },
835
+ })),
836
+ actions: resolved.tools.map((tool) => ({
837
+ name: tool.name,
838
+ description: tool.description,
839
+ permission: tool.permission,
840
+ input: tool.inputSchema,
841
+ connections: [resolved.connection],
842
+ hints: {
843
+ readOnly: tool.hints.readOnly,
844
+ destructive: tool.hints.destructive,
845
+ idempotent: tool.hints.idempotent,
846
+ },
847
+ })),
848
+ };
849
+ }
850
+ /** The keys of every connection the manifest's actions and toolsets call, without duplicates. */
801
851
  export function actionConnectionKeys(manifest) {
802
852
  return [
803
- ...new Set(manifest.entities.flatMap((entity) => entity.actions.flatMap((candidate) => candidate.connections ?? []))),
853
+ ...new Set([
854
+ ...manifest.entities.flatMap((entity) => entity.actions.flatMap((candidate) => candidate.connections ?? [])),
855
+ ...(manifest.toolsets ?? []).map((candidate) => candidate.connection),
856
+ ]),
804
857
  ];
805
858
  }
@@ -514,6 +514,41 @@ export declare const MigrationPlan: Schema.Struct<{
514
514
  readonly footer?: string | undefined;
515
515
  };
516
516
  } | undefined;
517
+ readonly toolsets?: readonly {
518
+ readonly name: string;
519
+ readonly description: string;
520
+ readonly connection: string;
521
+ readonly api: string;
522
+ readonly tools: readonly {
523
+ readonly name: string;
524
+ readonly access: "read" | "write";
525
+ readonly fixed: {
526
+ readonly [x: string]: string | number | boolean;
527
+ };
528
+ }[];
529
+ readonly actions: readonly {
530
+ readonly name: string;
531
+ readonly description: string;
532
+ readonly input: {
533
+ readonly [x: string]: Schema.Json;
534
+ };
535
+ readonly permission?: string | undefined;
536
+ readonly retryOnDeadlock?: boolean | undefined;
537
+ readonly writes?: readonly string[] | undefined;
538
+ readonly connections?: readonly string[] | undefined;
539
+ readonly files?: readonly {
540
+ readonly entity: string;
541
+ readonly property: string;
542
+ }[] | undefined;
543
+ readonly hints?: {
544
+ readonly readOnly?: boolean | undefined;
545
+ readonly connectionsReadOnly?: boolean | undefined;
546
+ readonly destructive?: boolean | undefined;
547
+ readonly irreversible?: boolean | undefined;
548
+ readonly idempotent?: boolean | undefined;
549
+ } | undefined;
550
+ }[];
551
+ }[] | undefined;
517
552
  } | {
518
553
  readonly version: number;
519
554
  readonly hash: string;
@@ -747,6 +782,41 @@ export declare const MigrationPlan: Schema.Struct<{
747
782
  readonly footer?: string | undefined;
748
783
  };
749
784
  } | undefined;
785
+ readonly toolsets?: readonly {
786
+ readonly name: string;
787
+ readonly description: string;
788
+ readonly connection: string;
789
+ readonly api: string;
790
+ readonly tools: readonly {
791
+ readonly name: string;
792
+ readonly access: "read" | "write";
793
+ readonly fixed: {
794
+ readonly [x: string]: string | number | boolean;
795
+ };
796
+ }[];
797
+ readonly actions: readonly {
798
+ readonly name: string;
799
+ readonly description: string;
800
+ readonly input: {
801
+ readonly [x: string]: Schema.Json;
802
+ };
803
+ readonly permission?: string | undefined;
804
+ readonly retryOnDeadlock?: boolean | undefined;
805
+ readonly writes?: readonly string[] | undefined;
806
+ readonly connections?: readonly string[] | undefined;
807
+ readonly files?: readonly {
808
+ readonly entity: string;
809
+ readonly property: string;
810
+ }[] | undefined;
811
+ readonly hints?: {
812
+ readonly readOnly?: boolean | undefined;
813
+ readonly connectionsReadOnly?: boolean | undefined;
814
+ readonly destructive?: boolean | undefined;
815
+ readonly irreversible?: boolean | undefined;
816
+ readonly idempotent?: boolean | undefined;
817
+ } | undefined;
818
+ }[];
819
+ }[] | undefined;
750
820
  }, {
751
821
  readonly version: number;
752
822
  readonly hash: string;
@@ -958,6 +1028,41 @@ export declare const MigrationPlan: Schema.Struct<{
958
1028
  readonly reply_to?: string | undefined;
959
1029
  };
960
1030
  } | undefined;
1031
+ readonly toolsets?: readonly {
1032
+ readonly name: string;
1033
+ readonly description: string;
1034
+ readonly connection: string;
1035
+ readonly api: string;
1036
+ readonly tools: readonly {
1037
+ readonly name: string;
1038
+ readonly access: "read" | "write";
1039
+ readonly fixed: {
1040
+ readonly [x: string]: string | number | boolean;
1041
+ };
1042
+ }[];
1043
+ readonly actions: readonly {
1044
+ readonly name: string;
1045
+ readonly description: string;
1046
+ readonly input: {
1047
+ readonly [x: string]: Schema.Json;
1048
+ };
1049
+ readonly permission?: string | undefined;
1050
+ readonly retryOnDeadlock?: boolean | undefined;
1051
+ readonly writes?: readonly string[] | undefined;
1052
+ readonly connections?: readonly string[] | undefined;
1053
+ readonly files?: readonly {
1054
+ readonly entity: string;
1055
+ readonly property: string;
1056
+ }[] | undefined;
1057
+ readonly hints?: {
1058
+ readonly readOnly?: boolean | undefined;
1059
+ readonly connectionsReadOnly?: boolean | undefined;
1060
+ readonly destructive?: boolean | undefined;
1061
+ readonly irreversible?: boolean | undefined;
1062
+ readonly idempotent?: boolean | undefined;
1063
+ } | undefined;
1064
+ }[];
1065
+ }[] | undefined;
961
1066
  readonly sign_in?: {
962
1067
  readonly policy: string;
963
1068
  readonly browser?: {
@@ -1213,6 +1318,41 @@ export declare const MigrationPlan: Schema.Struct<{
1213
1318
  readonly reply_to?: string | undefined;
1214
1319
  };
1215
1320
  } | undefined;
1321
+ readonly toolsets?: readonly {
1322
+ readonly name: string;
1323
+ readonly description: string;
1324
+ readonly connection: string;
1325
+ readonly api: string;
1326
+ readonly tools: readonly {
1327
+ readonly name: string;
1328
+ readonly access: "read" | "write";
1329
+ readonly fixed: {
1330
+ readonly [x: string]: string | number | boolean;
1331
+ };
1332
+ }[];
1333
+ readonly actions: readonly {
1334
+ readonly name: string;
1335
+ readonly description: string;
1336
+ readonly input: {
1337
+ readonly [x: string]: Schema.Json;
1338
+ };
1339
+ readonly permission?: string | undefined;
1340
+ readonly retryOnDeadlock?: boolean | undefined;
1341
+ readonly writes?: readonly string[] | undefined;
1342
+ readonly connections?: readonly string[] | undefined;
1343
+ readonly files?: readonly {
1344
+ readonly entity: string;
1345
+ readonly property: string;
1346
+ }[] | undefined;
1347
+ readonly hints?: {
1348
+ readonly readOnly?: boolean | undefined;
1349
+ readonly connectionsReadOnly?: boolean | undefined;
1350
+ readonly destructive?: boolean | undefined;
1351
+ readonly irreversible?: boolean | undefined;
1352
+ readonly idempotent?: boolean | undefined;
1353
+ } | undefined;
1354
+ }[];
1355
+ }[] | undefined;
1216
1356
  }, readonly [Schema.Union<readonly [Schema.encodeKeys<Schema.Struct<{
1217
1357
  readonly entities: Schema.$Array<Schema.encodeKeys<Schema.Struct<{
1218
1358
  readonly metrics: Schema.optionalKey<Schema.$Array<Schema.encodeKeys<Schema.Struct<{
@@ -1454,6 +1594,37 @@ export declare const MigrationPlan: Schema.Struct<{
1454
1594
  readonly headers: Schema.optionalKey<Schema.$Array<Schema.String>>;
1455
1595
  }>, Schema.$Record<Schema.String, Schema.Codec<Schema.Json, Schema.Json, never, never>>]>>;
1456
1596
  }>>>;
1597
+ readonly toolsets: Schema.optionalKey<Schema.$Array<Schema.Struct<{
1598
+ readonly name: Schema.String;
1599
+ readonly description: Schema.String;
1600
+ readonly connection: Schema.String;
1601
+ readonly api: Schema.String;
1602
+ readonly tools: Schema.$Array<Schema.Struct<{
1603
+ readonly name: Schema.String;
1604
+ readonly access: Schema.Literals<readonly ["read", "write"]>;
1605
+ readonly fixed: Schema.$Record<Schema.String, Schema.Union<readonly [Schema.String, Schema.Number, Schema.Boolean]>>;
1606
+ }>>;
1607
+ readonly actions: Schema.$Array<Schema.Struct<{
1608
+ readonly name: Schema.String;
1609
+ readonly description: Schema.String;
1610
+ readonly permission: Schema.optionalKey<Schema.String>;
1611
+ readonly input: Schema.$Record<Schema.String, Schema.Codec<Schema.Json, Schema.Json, never, never>>;
1612
+ readonly retryOnDeadlock: Schema.optionalKey<Schema.Boolean>;
1613
+ readonly writes: Schema.optionalKey<Schema.$Array<Schema.String>>;
1614
+ readonly connections: Schema.optionalKey<Schema.$Array<Schema.String>>;
1615
+ readonly files: Schema.optionalKey<Schema.$Array<Schema.Struct<{
1616
+ readonly entity: Schema.String;
1617
+ readonly property: Schema.String;
1618
+ }>>>;
1619
+ readonly hints: Schema.optionalKey<Schema.Struct<{
1620
+ readonly readOnly: Schema.optionalKey<Schema.Boolean>;
1621
+ readonly connectionsReadOnly: Schema.optionalKey<Schema.Boolean>;
1622
+ readonly destructive: Schema.optionalKey<Schema.Boolean>;
1623
+ readonly irreversible: Schema.optionalKey<Schema.Boolean>;
1624
+ readonly idempotent: Schema.optionalKey<Schema.Boolean>;
1625
+ }>>;
1626
+ }>>;
1627
+ }>>>;
1457
1628
  }>, {
1458
1629
  readonly signIn: "sign_in";
1459
1630
  }>, Schema.Struct<{
@@ -1682,6 +1853,37 @@ export declare const MigrationPlan: Schema.Struct<{
1682
1853
  readonly headers: Schema.optionalKey<Schema.$Array<Schema.String>>;
1683
1854
  }>, Schema.$Record<Schema.String, Schema.Codec<Schema.Json, Schema.Json, never, never>>]>>;
1684
1855
  }>>>;
1856
+ readonly toolsets: Schema.optionalKey<Schema.$Array<Schema.Struct<{
1857
+ readonly name: Schema.String;
1858
+ readonly description: Schema.String;
1859
+ readonly connection: Schema.String;
1860
+ readonly api: Schema.String;
1861
+ readonly tools: Schema.$Array<Schema.Struct<{
1862
+ readonly name: Schema.String;
1863
+ readonly access: Schema.Literals<readonly ["read", "write"]>;
1864
+ readonly fixed: Schema.$Record<Schema.String, Schema.Union<readonly [Schema.String, Schema.Number, Schema.Boolean]>>;
1865
+ }>>;
1866
+ readonly actions: Schema.$Array<Schema.Struct<{
1867
+ readonly name: Schema.String;
1868
+ readonly description: Schema.String;
1869
+ readonly permission: Schema.optionalKey<Schema.String>;
1870
+ readonly input: Schema.$Record<Schema.String, Schema.Codec<Schema.Json, Schema.Json, never, never>>;
1871
+ readonly retryOnDeadlock: Schema.optionalKey<Schema.Boolean>;
1872
+ readonly writes: Schema.optionalKey<Schema.$Array<Schema.String>>;
1873
+ readonly connections: Schema.optionalKey<Schema.$Array<Schema.String>>;
1874
+ readonly files: Schema.optionalKey<Schema.$Array<Schema.Struct<{
1875
+ readonly entity: Schema.String;
1876
+ readonly property: Schema.String;
1877
+ }>>>;
1878
+ readonly hints: Schema.optionalKey<Schema.Struct<{
1879
+ readonly readOnly: Schema.optionalKey<Schema.Boolean>;
1880
+ readonly connectionsReadOnly: Schema.optionalKey<Schema.Boolean>;
1881
+ readonly destructive: Schema.optionalKey<Schema.Boolean>;
1882
+ readonly irreversible: Schema.optionalKey<Schema.Boolean>;
1883
+ readonly idempotent: Schema.optionalKey<Schema.Boolean>;
1884
+ }>>;
1885
+ }>>;
1886
+ }>>>;
1685
1887
  }>]>], {
1686
1888
  readonly version: number;
1687
1889
  readonly hash: string;
@@ -1915,6 +2117,41 @@ export declare const MigrationPlan: Schema.Struct<{
1915
2117
  readonly footer?: string | undefined;
1916
2118
  };
1917
2119
  } | undefined;
2120
+ readonly toolsets?: readonly {
2121
+ readonly name: string;
2122
+ readonly description: string;
2123
+ readonly connection: string;
2124
+ readonly api: string;
2125
+ readonly tools: readonly {
2126
+ readonly name: string;
2127
+ readonly access: "read" | "write";
2128
+ readonly fixed: {
2129
+ readonly [x: string]: string | number | boolean;
2130
+ };
2131
+ }[];
2132
+ readonly actions: readonly {
2133
+ readonly name: string;
2134
+ readonly description: string;
2135
+ readonly input: {
2136
+ readonly [x: string]: Schema.Json;
2137
+ };
2138
+ readonly permission?: string | undefined;
2139
+ readonly retryOnDeadlock?: boolean | undefined;
2140
+ readonly writes?: readonly string[] | undefined;
2141
+ readonly connections?: readonly string[] | undefined;
2142
+ readonly files?: readonly {
2143
+ readonly entity: string;
2144
+ readonly property: string;
2145
+ }[] | undefined;
2146
+ readonly hints?: {
2147
+ readonly readOnly?: boolean | undefined;
2148
+ readonly connectionsReadOnly?: boolean | undefined;
2149
+ readonly destructive?: boolean | undefined;
2150
+ readonly irreversible?: boolean | undefined;
2151
+ readonly idempotent?: boolean | undefined;
2152
+ } | undefined;
2153
+ }[];
2154
+ }[] | undefined;
1918
2155
  } | {
1919
2156
  readonly version: number;
1920
2157
  readonly hash: string;
@@ -2148,6 +2385,41 @@ export declare const MigrationPlan: Schema.Struct<{
2148
2385
  readonly footer?: string | undefined;
2149
2386
  };
2150
2387
  } | undefined;
2388
+ readonly toolsets?: readonly {
2389
+ readonly name: string;
2390
+ readonly description: string;
2391
+ readonly connection: string;
2392
+ readonly api: string;
2393
+ readonly tools: readonly {
2394
+ readonly name: string;
2395
+ readonly access: "read" | "write";
2396
+ readonly fixed: {
2397
+ readonly [x: string]: string | number | boolean;
2398
+ };
2399
+ }[];
2400
+ readonly actions: readonly {
2401
+ readonly name: string;
2402
+ readonly description: string;
2403
+ readonly input: {
2404
+ readonly [x: string]: Schema.Json;
2405
+ };
2406
+ readonly permission?: string | undefined;
2407
+ readonly retryOnDeadlock?: boolean | undefined;
2408
+ readonly writes?: readonly string[] | undefined;
2409
+ readonly connections?: readonly string[] | undefined;
2410
+ readonly files?: readonly {
2411
+ readonly entity: string;
2412
+ readonly property: string;
2413
+ }[] | undefined;
2414
+ readonly hints?: {
2415
+ readonly readOnly?: boolean | undefined;
2416
+ readonly connectionsReadOnly?: boolean | undefined;
2417
+ readonly destructive?: boolean | undefined;
2418
+ readonly irreversible?: boolean | undefined;
2419
+ readonly idempotent?: boolean | undefined;
2420
+ } | undefined;
2421
+ }[];
2422
+ }[] | undefined;
2151
2423
  }>;
2152
2424
  /** Non-transactional steps first, then the steps of the one transaction, in execution order. */
2153
2425
  readonly steps: Schema.$Array<Schema.Struct<{
@@ -5,6 +5,7 @@ import { type OntologyManifest, type WorkflowManifest } from "./manifest.js";
5
5
  import type { ModelMetrics } from "./metric.js";
6
6
  import type { Role } from "./role.js";
7
7
  import type { ResolvedSignInOptions } from "./sign-in.js";
8
+ import type { ResolvedToolset } from "./toolset.js";
8
9
  /** A checked ontology: the resolved definitions and the manifest built from them. */
9
10
  export interface Ontology<Entities extends ReadonlyArray<AnyEntity> = ReadonlyArray<AnyEntity>, Metrics extends ModelMetrics = ModelMetrics> {
10
11
  /** The definition as given to `defineOntology`. */
@@ -19,6 +20,8 @@ export interface Ontology<Entities extends ReadonlyArray<AnyEntity> = ReadonlyAr
19
20
  readonly connections: ReadonlyArray<ResolvedConnection>;
20
21
  /** The workflows, with wire names and their triggers' input in wire form, as the manifest lists them. */
21
22
  readonly workflows: ReadonlyArray<WorkflowManifest>;
23
+ /** The provider MCP tools the ontology serves, with wire names. */
24
+ readonly toolsets: ReadonlyArray<ResolvedToolset>;
22
25
  readonly manifest: OntologyManifest;
23
26
  /** Warning-level diagnostics; any error makes `defineOntology` throw. */
24
27
  readonly warnings: ReadonlyArray<Diagnostic>;
package/dist/ontology.js CHANGED
@@ -33,6 +33,7 @@ export function buildOntology(config, options) {
33
33
  signIn: analysis.signIn,
34
34
  connections: analysis.connections,
35
35
  workflows: analysis.workflows,
36
+ toolsets: analysis.toolsets,
36
37
  manifest: buildManifest(analysis),
37
38
  warnings: analysis.diagnostics,
38
39
  };
@@ -18,7 +18,9 @@ export declare const READ_VERB = "read";
18
18
  * App feedback is a member's own message to the builders: a member reads theirs through a scoped
19
19
  * `app_feedback:read`, and only the builders' roles name it unscoped. The built-in sign-in
20
20
  * identities hold every member's external accounts, such as their Telegram id and username: only a
21
- * role that names `user_identity` reads them.
21
+ * role that names `user_identity` reads them. The agent's held replies hold the workspace's
22
+ * conversations with the people it serves, and approving one sends it: only a role that names
23
+ * `agent_review` reads, approves or rejects them.
22
24
  */
23
25
  export declare const EXPLICIT_GRANT_ENTITIES: ReadonlySet<string>;
24
26
  /**
@@ -150,8 +152,8 @@ export interface PermissionHolder {
150
152
  }
151
153
  /**
152
154
  * Whether the principal may do what `required` names: one of its permissions equals it, or covers it
153
- * through a wildcard. Workload wildcards exclude user administration, feedback and writes of sign-in
154
- * identities.
155
+ * through a wildcard. Workload wildcards exclude user administration, feedback, the agent's held
156
+ * replies and writes of sign-in identities.
155
157
  */
156
158
  export declare function isAllowed(required: string, principal: PermissionHolder): boolean;
157
159
  /**
@@ -17,10 +17,13 @@ export const READ_VERB = "read";
17
17
  * App feedback is a member's own message to the builders: a member reads theirs through a scoped
18
18
  * `app_feedback:read`, and only the builders' roles name it unscoped. The built-in sign-in
19
19
  * identities hold every member's external accounts, such as their Telegram id and username: only a
20
- * role that names `user_identity` reads them.
20
+ * role that names `user_identity` reads them. The agent's held replies hold the workspace's
21
+ * conversations with the people it serves, and approving one sends it: only a role that names
22
+ * `agent_review` reads, approves or rejects them.
21
23
  */
22
24
  export const EXPLICIT_GRANT_ENTITIES = new Set([
23
25
  "agent_chat",
26
+ "agent_review",
24
27
  "app_feedback",
25
28
  "app_feedback_file",
26
29
  "user_identity",
@@ -151,7 +154,8 @@ function isUserAdministration(required) {
151
154
  !wildcardEntityCovers(USER_ENTITY, toSnakeCase(wanted.verb)));
152
155
  }
153
156
  /**
154
- * Feedback always needs named grants, including for pipeline workloads. The workspace's own pipeline
157
+ * Feedback and the agent's held replies always need named grants, including for pipeline
158
+ * workloads: nothing a pipeline runs approves a reply to a person. The workspace's own pipeline
155
159
  * code reads sign-in identities, for example to address a member in a chat, but nothing it runs
156
160
  * writes them: only the platform does.
157
161
  */
@@ -162,12 +166,12 @@ function workloadWildcardCovers(required) {
162
166
  }
163
167
  return (!isUserAdministration(required) &&
164
168
  (wanted === undefined ||
165
- !["app_feedback", "app_feedback_file"].includes(toSnakeCase(wanted.entity))));
169
+ !["app_feedback", "app_feedback_file", "agent_review"].includes(toSnakeCase(wanted.entity))));
166
170
  }
167
171
  /**
168
172
  * Whether the principal may do what `required` names: one of its permissions equals it, or covers it
169
- * through a wildcard. Workload wildcards exclude user administration, feedback and writes of sign-in
170
- * identities.
173
+ * through a wildcard. Workload wildcards exclude user administration, feedback, the agent's held
174
+ * replies and writes of sign-in identities.
171
175
  */
172
176
  export function isAllowed(required, principal) {
173
177
  return (
@@ -3,6 +3,7 @@ import { randomUUID } from "node:crypto";
3
3
  import { Effect } from "effect";
4
4
  import { SqlClient } from "effect/unstable/sql";
5
5
  import { AgentChat } from "../agent-chat.js";
6
+ import { AgentReview } from "../agent-review.js";
6
7
  import { AGENT_IDENTITIES_TABLE, AGENT_PAIRINGS_TABLE, CHANNEL_USERS_TABLE, IDENTITY_NOTICES_TABLE, USER_TABLE, } from "../migrate/storage.js";
7
8
  import { MAX_QUERY_LIMIT } from "../query.js";
8
9
  import { beginReplicationWrite } from "../replication/transaction.js";
@@ -435,6 +436,58 @@ export function sqlAgentStore(ontology, sql) {
435
436
  };
436
437
  }))
437
438
  .pipe(Effect.orDie),
439
+ holdReply: (input) => sql
440
+ .withTransaction(Effect.gen(function* () {
441
+ if (!isRecordId(input.memberId)) {
442
+ return undefined;
443
+ }
444
+ yield* beginReplicationWrite(sql);
445
+ // Reports of one reply run one after the other: the first creates it, the rest find it.
446
+ yield* rows(`SELECT pg_advisory_xact_lock(hashtext($1))`, [`review:${input.key}`]);
447
+ const runtime = makeOntologyRuntime(ontology, sql, {
448
+ action: `${AgentReview.name}.hold`,
449
+ actionId: randomUUID(),
450
+ principalId: null,
451
+ });
452
+ const [found] = yield* runtime.find({
453
+ entity: AgentReview.name,
454
+ options: { where: { key: input.key }, limit: 1 },
455
+ });
456
+ if (found !== undefined) {
457
+ return { id: String(found.id), status: String(found.status), created: false };
458
+ }
459
+ const [member] = yield* rows(`SELECT 1 FROM ${USERS} WHERE "id" = $1::uuid AND "kind"::text = 'member' AND "active"`, [input.memberId]);
460
+ if (member === undefined) {
461
+ return undefined;
462
+ }
463
+ const review = yield* runtime.create({
464
+ entity: AgentReview.name,
465
+ values: {
466
+ key: input.key,
467
+ provider: input.provider,
468
+ chat_id: input.chatId,
469
+ recipient: input.recipient,
470
+ // Telegram's groups and channels have negative ids; WhatsApp chats are private.
471
+ chat_kind: input.provider === "telegram" && input.chatId.startsWith("-")
472
+ ? "group"
473
+ : "private",
474
+ thread_id: input.threadId === "" ? null : input.threadId,
475
+ reply_to: input.replyTo,
476
+ session_id: input.sessionId,
477
+ draft: input.text,
478
+ text: null,
479
+ status: "pending",
480
+ // Select values are stored by their wire names.
481
+ mode: input.mode === "whenUnsure" ? "when_unsure" : "always",
482
+ confidence: input.confidence,
483
+ message_id: null,
484
+ decided_at: null,
485
+ person: input.memberId,
486
+ },
487
+ });
488
+ return { id: String(review.id), status: "pending", created: true };
489
+ }))
490
+ .pipe(Effect.orDie),
438
491
  };
439
492
  return store;
440
493
  }
@@ -68,6 +68,24 @@ export declare const ChatSeenRequest: Schema.Struct<{
68
68
  readonly agentLeft: Schema.optionalKey<Schema.Literal<true>>;
69
69
  readonly migratedFrom: Schema.optionalKey<Schema.String>;
70
70
  }>;
71
+ /**
72
+ * `POST /api/ontology/agent/reviews`: a reply the agent held for a member to review, for the member
73
+ * it answers. `key` names the reply, so a report repeated after a failure finds its review.
74
+ */
75
+ export declare const ReviewRequest: Schema.Struct<{
76
+ readonly key: Schema.String;
77
+ readonly provider: Schema.String;
78
+ readonly chat_id: Schema.String;
79
+ readonly recipient: Schema.String;
80
+ readonly thread_id: Schema.String;
81
+ readonly reply_to: Schema.NullOr<Schema.String>;
82
+ readonly member_id: Schema.String;
83
+ readonly session_id: Schema.String;
84
+ readonly text: Schema.String;
85
+ readonly mode: Schema.Literals<readonly ["always", "whenUnsure"]>;
86
+ readonly confidence: Schema.NullOr<Schema.Number>;
87
+ }>;
88
+ export type ReviewRequest = typeof ReviewRequest.Type;
71
89
  /** An identity on the wire, as the member's connect page and settings show it. */
72
90
  export declare function identityForWire(identity: AgentIdentity): {
73
91
  created_at: string;
@@ -94,6 +94,39 @@ export const ChatSeenRequest = Schema.Struct({
94
94
  description: "The chat's id before it became a supergroup: its record moves to externalId.",
95
95
  })),
96
96
  });
97
+ /**
98
+ * `POST /api/ontology/agent/reviews`: a reply the agent held for a member to review, for the member
99
+ * it answers. `key` names the reply, so a report repeated after a failure finds its review.
100
+ */
101
+ export const ReviewRequest = Schema.Struct({
102
+ key: Schema.String.check(Schema.isMinLength(1), Schema.isMaxLength(512)).annotate({
103
+ description: "The agent's key for the reply: its session and content part.",
104
+ }),
105
+ provider: Provider,
106
+ chat_id: ChatId.annotate({ description: "The conversation's id on the channel." }),
107
+ recipient: ChatId.annotate({
108
+ description: "Whom the provider sends the reply to: the chat's id, or the person's number.",
109
+ }),
110
+ thread_id: Schema.String.check(Schema.isMaxLength(MAX_EXTERNAL_ID_LENGTH)).annotate({
111
+ description: "The conversation's thread or topic; empty without one.",
112
+ }),
113
+ reply_to: Schema.NullOr(ChatId).annotate({
114
+ description: "The member's message the reply answers, when the channel replies to one.",
115
+ }),
116
+ member_id: Schema.String.annotate({ description: "The user id of the member the reply is for." }),
117
+ session_id: Schema.String.check(Schema.isMinLength(1), Schema.isMaxLength(512)).annotate({
118
+ description: "The agent's session that wrote the reply.",
119
+ }),
120
+ text: Schema.String.check(Schema.isMinLength(1), Schema.isMaxLength(20_000)).annotate({
121
+ description: "The reply as the agent wrote it, in Markdown.",
122
+ }),
123
+ mode: Schema.Literals(["always", "whenUnsure"]).annotate({
124
+ description: "Why the reply was held: every reply is reviewed, or the model was unsure.",
125
+ }),
126
+ confidence: Schema.NullOr(Schema.Number.check(Schema.isGreaterThanOrEqualTo(0), Schema.isLessThanOrEqualTo(1))).annotate({
127
+ description: "The decision model's probability that staff would send it as written.",
128
+ }),
129
+ });
97
130
  /** An identity on the wire, as the member's connect page and settings show it. */
98
131
  export function identityForWire(identity) {
99
132
  return {