@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/README.md CHANGED
@@ -385,7 +385,8 @@ export default defineOntology({
385
385
  key and `rel.many` a join table. With a warehouse-backed end, `rel.one` declares one `on`
386
386
  pair and is matched by value. `User` is the built-in entity for app users and service
387
387
  principals; `AgentChat` (`agent_chat`) is the built-in entity for the group chats of the
388
- app's agent (see [Agents](#agents)). Every ontology includes both. Text feedback defaults on with built-in `AppFeedback`; files follow the non-feedback-blob policy
388
+ app's agent (see [Agents](#agents)), and `AgentReview` (`agent_review`) for the agent's replies
389
+ held for a member to approve, edit or reject. Every ontology includes them. Text feedback defaults on with built-in `AppFeedback`; files follow the non-feedback-blob policy
389
390
  (or an explicit opt-in) and add `AppFeedbackFile` (see [App feedback](#app-feedback)).
390
391
  - `role(name, { description, permissions })` declares a role: a named set of permissions.
391
392
  `member` is built in: every active app user holds it, with the permissions of
@@ -927,8 +928,8 @@ configured. This preparation does not advertise or enable exact chat posting.
927
928
  A permission is `<entity>:<verb>`. The verbs are `read`, the generated `create`, `update`,
928
929
  `delete`, `link` and `unlink`, and the names of authored actions. A role may use `*` for the
929
930
  entity, the verb or both: `lead:*`, `*:read`, `*:*`. An entity wildcard never covers the
930
- built-in `agent_chat`, `app_feedback` or `app_feedback_file`: reading or changing them needs a
931
- permission that names them, such as `agent_chat:read`.
931
+ built-in `agent_chat`, `agent_review`, `app_feedback`, `app_feedback_file` or `user_identity`:
932
+ reading or changing them needs a permission that names them, such as `agent_chat:read`.
932
933
 
933
934
  - Reads require `<entity>:read`: lists, queries, records, history, both entities of a
934
935
  relationship traversal, and every entity a metric query touches. `include` and a filter through a relationship require `read` on
@@ -1241,6 +1242,17 @@ is visible. Attachment guidance appears only when attach is visible. The caller
1241
1242
  problem, asks approval for the title and summary, then later reports and acknowledges the answer.
1242
1243
  Builders use `segmentstream ontology feedback list`, `show` and `respond` as their app member.
1243
1244
 
1245
+ ## Toolsets
1246
+
1247
+ `toolset(name, { connection, api, lock, description })` serves the tools of a connection's MCP
1248
+ API from the ontology's MCP server: each tool in the lock, written by
1249
+ `segmentstream connection mcp lock <key>`, is the MCP tool `<toolset>_<tool>`, granted by
1250
+ `<toolset>:<tool>` or `<toolset>:*`. A read tool needs no confirmation; every other tool is
1251
+ confirmed. Fixed values the connection declares are removed from the tool's input and added to each
1252
+ call, and backend-next admits only the declared tools and values. A toolset has no records. It is
1253
+ served over MCP only: not by the REST API, to workflows or under the OAuth `ontology:read` scope.
1254
+ See [MCP connections](https://github.com/segmentstream/segmentstream/blob/main/docs/mcp-connections-spec.md).
1255
+
1244
1256
  ## Agents
1245
1257
 
1246
1258
  The app's own agent runs in the same Vercel project as the service and calls it as the
@@ -0,0 +1,95 @@
1
+ import { Schema } from "effect";
2
+ /**
3
+ * A reply the workspace's agent held for review. `agent/segmentstream.json` says, per role of the
4
+ * person the agent replies to, whether a member reviews its replies (`conversations.*.review`);
5
+ * the agent records each reply it holds here, and only the actions below change it. Approving sends
6
+ * the reply, or the member's edit of it, to the person's conversation as the agent, without the
7
+ * sender attribution a chat post carries, at most once; rejecting discards it. Reading, approving
8
+ * and rejecting each need a permission that names `agent_review`: an entity wildcard does not cover
9
+ * it, since its records hold the workspace's conversations with the people it serves.
10
+ */
11
+ export declare const AGENT_REVIEW_STATUSES: readonly [{
12
+ readonly value: "pending";
13
+ readonly description: "Waiting for a member to approve, edit or reject it.";
14
+ }, {
15
+ readonly value: "sent";
16
+ readonly description: "Approved and delivered to the person.";
17
+ }, {
18
+ readonly value: "uncertain";
19
+ readonly description: "Approved; the provider may have delivered it, and it is never sent again.";
20
+ }, {
21
+ readonly value: "failed";
22
+ readonly description: "Approved; the provider refused it, and nothing was sent.";
23
+ }, {
24
+ readonly value: "rejected";
25
+ readonly description: "Rejected: the person never sees it.";
26
+ }];
27
+ export declare const AGENT_REVIEW_MODES: readonly [{
28
+ readonly value: "always";
29
+ readonly description: "Every reply in the conversation waits for a member.";
30
+ }, {
31
+ readonly value: "whenUnsure";
32
+ readonly description: "The decision model was not sure enough that staff would send it as written.";
33
+ }];
34
+ /** The longest reply a review holds; a provider may accept less in one message. */
35
+ export declare const AGENT_REVIEW_TEXT_LIMIT = 20000;
36
+ export declare const AgentReview: import("./entity.js").Entity<"agent_review", {
37
+ readonly key: import("./property.js").Property<string, false, true>;
38
+ readonly provider: import("./property.js").Property<string, false, false>;
39
+ readonly chatId: import("./property.js").Property<string, false, false>;
40
+ readonly recipient: import("./property.js").Property<string, false, false>;
41
+ readonly chatKind: import("./property.js").Property<"private" | "group", false, false>;
42
+ readonly threadId: Schema.optionalKey<import("./property.js").Property<string, false, false>>;
43
+ readonly replyTo: Schema.optionalKey<import("./property.js").Property<string, false, false>>;
44
+ readonly sessionId: import("./property.js").Property<string, false, false>;
45
+ readonly draft: import("./property.js").Property<string, false, false>;
46
+ readonly text: Schema.optionalKey<import("./property.js").Property<string, false, false>>;
47
+ readonly status: import("./property.js").Property<"pending" | "sent" | "uncertain" | "failed" | "rejected", false, false>;
48
+ readonly mode: import("./property.js").Property<"always" | "whenUnsure", false, false>;
49
+ readonly confidence: Schema.optionalKey<import("./property.js").Property<number, false, false>>;
50
+ readonly messageId: Schema.optionalKey<import("./property.js").Property<string, false, false>>;
51
+ readonly decidedAt: Schema.optionalKey<import("./property.js").Property<string, false, false>>;
52
+ }, {
53
+ readonly person: import("./relationship.js").OneRelationship<false, false, "user">;
54
+ readonly decidedBy: import("./relationship.js").OneRelationship<true, false, "user">;
55
+ }, import("./entity.js").InvocableActions<{
56
+ readonly approve: import("./action.js").Action<Schema.Struct<{
57
+ readonly review: Schema.Codec<string, string, never, never>;
58
+ readonly text: Schema.optionalKey<import("./property.js").Property<string, false, false>>;
59
+ }>, {
60
+ review: string;
61
+ status: "sent" | "uncertain" | "failed";
62
+ message_id: string | null;
63
+ duplicate: boolean;
64
+ }>;
65
+ readonly reject: import("./action.js").Action<Schema.Struct<{
66
+ readonly review: Schema.Codec<string, string, never, never>;
67
+ }>, {
68
+ review: string;
69
+ status: "rejected";
70
+ }>;
71
+ }, "agent_review", {
72
+ readonly id: string;
73
+ readonly createdAt: string;
74
+ readonly updatedAt: string;
75
+ readonly createdBy: string | null;
76
+ readonly updatedBy: string | null;
77
+ readonly key: string;
78
+ readonly provider: string;
79
+ readonly chatId: string;
80
+ readonly recipient: string;
81
+ readonly chatKind: "private" | "group";
82
+ readonly threadId: string | null;
83
+ readonly replyTo: string | null;
84
+ readonly sessionId: string;
85
+ readonly draft: string;
86
+ readonly text: string | null;
87
+ readonly status: "pending" | "sent" | "uncertain" | "failed" | "rejected";
88
+ readonly mode: "always" | "whenUnsure";
89
+ readonly confidence: number | null;
90
+ readonly messageId: string | null;
91
+ readonly decidedAt: string | null;
92
+ readonly personId: string;
93
+ readonly decidedById: string | null;
94
+ }>, Readonly<Record<never, never>>, "actions">;
95
+ export type AgentReview = typeof AgentReview.Type;
@@ -0,0 +1,200 @@
1
+ import { Effect, Option, Schema } from "effect";
2
+ import { action } from "./action.js";
3
+ import { ChatDelivery } from "./chat.js";
4
+ import { entity } from "./entity.js";
5
+ import { ActionRejected } from "./errors.js";
6
+ import { p } from "./property.js";
7
+ import { rel } from "./relationship.js";
8
+ /**
9
+ * A reply the workspace's agent held for review. `agent/segmentstream.json` says, per role of the
10
+ * person the agent replies to, whether a member reviews its replies (`conversations.*.review`);
11
+ * the agent records each reply it holds here, and only the actions below change it. Approving sends
12
+ * the reply, or the member's edit of it, to the person's conversation as the agent, without the
13
+ * sender attribution a chat post carries, at most once; rejecting discards it. Reading, approving
14
+ * and rejecting each need a permission that names `agent_review`: an entity wildcard does not cover
15
+ * it, since its records hold the workspace's conversations with the people it serves.
16
+ */
17
+ export const AGENT_REVIEW_STATUSES = [
18
+ { value: "pending", description: "Waiting for a member to approve, edit or reject it." },
19
+ { value: "sent", description: "Approved and delivered to the person." },
20
+ {
21
+ value: "uncertain",
22
+ description: "Approved; the provider may have delivered it, and it is never sent again.",
23
+ },
24
+ { value: "failed", description: "Approved; the provider refused it, and nothing was sent." },
25
+ { value: "rejected", description: "Rejected: the person never sees it." },
26
+ ];
27
+ export const AGENT_REVIEW_MODES = [
28
+ { value: "always", description: "Every reply in the conversation waits for a member." },
29
+ {
30
+ value: "whenUnsure",
31
+ description: "The decision model was not sure enough that staff would send it as written.",
32
+ },
33
+ ];
34
+ /** The longest reply a review holds; a provider may accept less in one message. */
35
+ export const AGENT_REVIEW_TEXT_LIMIT = 20_000;
36
+ export const AgentReview = entity("agent_review", {
37
+ id: "ent_agent_review",
38
+ label: "Agent reply review",
39
+ plural: "Agent reply reviews",
40
+ title: "draft",
41
+ description: "A reply the agent wrote to a person and held for a member to review, as the workspace's " +
42
+ "conversations settings ask. Approve it, with an edit if needed, to send it to the person as " +
43
+ "the agent; reject it to discard it.",
44
+ writes: "actions",
45
+ properties: {
46
+ key: p.text({
47
+ managed: true,
48
+ maxLength: 512,
49
+ unique: true,
50
+ description: "The agent's key for the reply, so a repeated report holds it once.",
51
+ }),
52
+ provider: p.text({ managed: true, description: "The channel, for example telegram." }),
53
+ chatId: p.text({ managed: true, description: "The conversation's id at the provider." }),
54
+ recipient: p.text({
55
+ managed: true,
56
+ description: "Whom the provider sends the reply to: the chat's id, or the person's number on WhatsApp.",
57
+ }),
58
+ chatKind: p.select([
59
+ { value: "private", description: "The person's own conversation with the agent." },
60
+ { value: "group", description: "A group chat." },
61
+ ], { managed: true, label: "Chat kind", description: "Whether the conversation is a group." }),
62
+ threadId: p.text({
63
+ optional: true,
64
+ managed: true,
65
+ description: "The thread or topic of the conversation, when it has one.",
66
+ }),
67
+ replyTo: p.text({
68
+ optional: true,
69
+ managed: true,
70
+ description: "The person's message the reply answers, when the provider replies to one.",
71
+ }),
72
+ sessionId: p.text({ managed: true, description: "The agent's session that wrote the reply." }),
73
+ draft: p.text({
74
+ managed: true,
75
+ maxLength: AGENT_REVIEW_TEXT_LIMIT,
76
+ display: "multiline",
77
+ description: "The reply as the agent wrote it, in Markdown.",
78
+ }),
79
+ text: p.text({
80
+ optional: true,
81
+ managed: true,
82
+ maxLength: AGENT_REVIEW_TEXT_LIMIT,
83
+ display: "multiline",
84
+ description: "The reply as approved: the draft, or the member's edit of it.",
85
+ }),
86
+ status: p.select(AGENT_REVIEW_STATUSES, {
87
+ managed: true,
88
+ description: "Whether the reply waits, was delivered, or was rejected.",
89
+ }),
90
+ mode: p.select(AGENT_REVIEW_MODES, { managed: true, description: "Why the reply was held." }),
91
+ confidence: p.number({
92
+ optional: true,
93
+ managed: true,
94
+ min: 0,
95
+ max: 1,
96
+ description: "The decision model's probability that staff would send the reply as written; empty " +
97
+ "when the conversation holds every reply or the model gave none.",
98
+ }),
99
+ messageId: p.text({
100
+ optional: true,
101
+ managed: true,
102
+ label: "Message id",
103
+ description: "The delivered message's id at the provider.",
104
+ }),
105
+ decidedAt: p.timestamp({
106
+ optional: true,
107
+ managed: true,
108
+ label: "Decided",
109
+ description: "When a member approved or rejected the reply.",
110
+ }),
111
+ },
112
+ relationships: {
113
+ // By name, so that an ontology rebuilt with the deployed `user` still resolves it.
114
+ person: rel.one("user", {
115
+ inverse: "agentReviews",
116
+ onDelete: "cascade",
117
+ phrase: ["answers", "is answered by"],
118
+ description: "The person the reply is for.",
119
+ }),
120
+ decidedBy: rel.one("user", {
121
+ optional: true,
122
+ inverse: "decidedAgentReviews",
123
+ onDelete: "setNull",
124
+ phrase: ["was decided by", "decided"],
125
+ description: "The member who approved or rejected the reply.",
126
+ }),
127
+ },
128
+ indexes: [{ properties: ["status"] }],
129
+ }).actions((self) => ({
130
+ approve: action({
131
+ description: "Send a reply the agent held for review to the person, as written or with your edit in " +
132
+ "text (Markdown), as the agent and without sender attribution. Confirm the final text " +
133
+ "with the member first. It is sent at most once: approving again with the same text " +
134
+ "returns the delivery, and an approved or rejected review is refused otherwise. status " +
135
+ "failed means the provider refused it and nothing was sent.",
136
+ hints: { destructive: false, irreversible: true, idempotent: true },
137
+ input: Schema.Struct({
138
+ review: self.ref,
139
+ text: Schema.optionalKey(p.text({
140
+ maxLength: AGENT_REVIEW_TEXT_LIMIT,
141
+ description: "The member's edit of the reply; omit it to send the draft.",
142
+ })),
143
+ }),
144
+ connections: [],
145
+ prepare: ({ input }) => Effect.gen(function* () {
146
+ const delivery = yield* Effect.serviceOption(ChatDelivery);
147
+ const send = Option.isSome(delivery) ? delivery.value.sendReview : undefined;
148
+ if (send === undefined) {
149
+ return yield* new ActionRejected({
150
+ message: "Sending a reviewed reply requires the authenticated ontology service.",
151
+ });
152
+ }
153
+ return yield* send({
154
+ review: String(input.review),
155
+ ...(input.text === undefined ? {} : { text: input.text }),
156
+ });
157
+ }),
158
+ handler: ({ input, prepared, principal }) => Effect.gen(function* () {
159
+ const review = yield* self.get(input.review, { forUpdate: true });
160
+ // The delivery happened, or definitely did not: the record says what the provider did,
161
+ // even when another member rejected the review meanwhile.
162
+ yield* self.update(review, {
163
+ status: prepared.status,
164
+ text: prepared.text,
165
+ messageId: prepared.messageId,
166
+ decidedAt: review.decidedAt ?? new Date().toISOString(),
167
+ decidedBy: review.decidedById ?? principal.id,
168
+ });
169
+ return {
170
+ review: review.id,
171
+ status: prepared.status,
172
+ message_id: prepared.messageId,
173
+ duplicate: prepared.duplicate,
174
+ };
175
+ }),
176
+ }),
177
+ reject: action({
178
+ description: "Discard a reply the agent held for review: the person never sees it. A review already " +
179
+ "approved is refused.",
180
+ hints: { destructive: true, irreversible: false, idempotent: true },
181
+ input: Schema.Struct({ review: self.ref }),
182
+ handler: ({ input, principal }) => Effect.gen(function* () {
183
+ const review = yield* self.get(input.review, { forUpdate: true });
184
+ if (review.status === "rejected") {
185
+ return { review: review.id, status: "rejected" };
186
+ }
187
+ if (review.status !== "pending") {
188
+ return yield* new ActionRejected({
189
+ message: "The reply was already approved and sent; it cannot be rejected.",
190
+ });
191
+ }
192
+ yield* self.update(review, {
193
+ status: "rejected",
194
+ decidedAt: new Date().toISOString(),
195
+ decidedBy: principal.id,
196
+ });
197
+ return { review: review.id, status: "rejected" };
198
+ }),
199
+ }),
200
+ }));
package/dist/analyze.d.ts CHANGED
@@ -14,11 +14,12 @@ import { type ModelMetricManifest, type WorkflowManifest } from "./manifest.js";
14
14
  import type { ModelMetrics } from "./metric.js";
15
15
  import { type EarlierMetricsField } from "./legacy.js";
16
16
  import { type AnyWorkflow } from "./workflow.js";
17
+ import { type ResolvedToolset, type Toolset } from "./toolset.js";
17
18
  /** The input of `defineOntology` and `check`. */
18
19
  export interface OntologyConfig<Entities extends ReadonlyArray<AnyEntity> = ReadonlyArray<AnyEntity>, Metrics extends ModelMetrics = ModelMetrics> extends EarlierMetricsField<Metrics> {
19
20
  /**
20
- * The app's entities. The built-in `User`, `AgentChat` and `UserIdentity` are always included;
21
- * listing them is optional.
21
+ * The app's entities. The built-in `User`, `AgentChat`, `UserIdentity` and `AgentReview` are
22
+ * always included; listing them is optional.
22
23
  */
23
24
  readonly entities: Entities;
24
25
  /**
@@ -77,6 +78,11 @@ export interface OntologyConfig<Entities extends ReadonlyArray<AnyEntity> = Read
77
78
  * handle an action lists must be here, and every handle here must be used by an action.
78
79
  */
79
80
  readonly connections?: ReadonlyArray<AnyConnectionHandle>;
81
+ /**
82
+ * Provider MCP tools the ontology serves, each declared with `toolset(name, { … })` over a
83
+ * connection's MCP API. Their connections must be listed in `connections`.
84
+ */
85
+ readonly toolsets?: ReadonlyArray<Toolset>;
80
86
  /**
81
87
  * The providers the built-in exact chat post delivers to, each through its connection, which
82
88
  * must be listed in `connections`. Only declared providers are destinations.
@@ -226,6 +232,8 @@ export interface Analysis {
226
232
  readonly metrics: ReadonlyArray<ModelMetricManifest>;
227
233
  /** The workflows that passed `check`, in their manifest form: wire names and wire input. */
228
234
  readonly workflows: ReadonlyArray<WorkflowManifest>;
235
+ /** The toolsets that passed `check`, with wire names. */
236
+ readonly toolsets: ReadonlyArray<ResolvedToolset>;
229
237
  readonly diagnostics: ReadonlyArray<Diagnostic>;
230
238
  }
231
239
  /**