@segmentstream/ontology 0.129.0 → 0.131.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,29 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.131.0
4
+
5
+ Members can send a reply the agent held for review back with a note on what to change.
6
+
7
+ - **The action.** `agent_review.revise({ review, note })` sends a pending reply back. It needs the permission `agent_review:revise`.
8
+ - **The redraft.** The agent writes the reply again in a session of its own, never in the person's chat session. That session acts as the person, reads only and has none of the agent's own tools. It starts from the chat, what the turn that wrote the reply looked up, and the draft, ahead of the note.
9
+ - **The result.** The new draft becomes a pending review whose `revises` names the reply, which becomes `superseded`. A member approves, edits, rejects or sends it back again.
10
+ - **Learning from it.** The review keeps the member's `note` and `notedBy` for the workspace's own learning. Neither the person nor the agent's chat with them sees the note. Workflows can react with `after(...)` on `AgentReview.actions.revise`, and the hold triggers fire for the redraft.
11
+
12
+ The person's own session learns only that the reply was not sent, and that a rewritten one waits or was sent.
13
+
14
+ - **Agent.** The agent serves `POST /eve/segmentstream/v1/reviews/revise`, which only the deployment's own functions call. It narrows its ontology requests with `X-SegmentStream-Agent-Scope: ontology:read`, which the ontology now accepts from the agent as an OAuth grant of that scope.
15
+ - **App.** `useInboxChat` gains `revise(reply, note)`. Each held reply now carries `revision` (`redrafting` or `failed`) and `revises`.
16
+
17
+ Action required: give the roles that review replies `agent_review:revise` to let them send replies back; `*:*` does not grant it. Redrafting needs both this ontology and this agent release.
18
+
19
+ ## 0.130.0
20
+
21
+ Reply review now gives the workspace what it needs to learn from staff decisions, and gives the agent what became of its replies.
22
+
23
+ - **Reason for a rejection.** `agent_review.reject({ review, reason })` keeps a member's reason as the review's `reason`. `useInboxChat`'s `reject(reply, reason)` passes it. Neither the person nor the agent's chat with them sees it.
24
+ - **Workflows on reviews.** `after(...)` on `AgentReview.actions.approve` or `reject` runs a workspace workflow after a member's decision. `after(...)` on `AgentReview` with `when: (event) => event.action === "agent_review.hold"` runs one when the agent holds a reply, for example to tell reviewers. Holding starts these runs like an action does.
25
+ - **What the agent learns.** At its next turn, the agent's session gets a `<held_replies>` block for each reply it held: `waiting`, `sent` (`edited` when a member sent their own version), `uncertain` (approved, but delivery is in doubt) or `not_sent`. It now goes on from what the person actually saw, without learning anything a member wrote. The ontology service answers it at `POST /api/ontology/agent/reviews/session`. An ontology service that predates the route answers nothing, and the agent behaves as before.
26
+
3
27
  ## 0.129.0
4
28
 
5
29
  A provider's secret for checking what it sends, such as a webhook signing secret, lives on its
@@ -23,6 +23,16 @@ export declare const AGENT_REVIEW_STATUSES: readonly [{
23
23
  }, {
24
24
  readonly value: "rejected";
25
25
  readonly description: "Rejected: the person never sees it.";
26
+ }, {
27
+ readonly value: "superseded";
28
+ readonly description: "Redrafted at a member's request: the review that revises it replaces it.";
29
+ }];
30
+ export declare const AGENT_REVIEW_REVISION_STATES: readonly [{
31
+ readonly value: "requested";
32
+ readonly description: "A member asked the agent to redraft it; it is redrafting.";
33
+ }, {
34
+ readonly value: "failed";
35
+ readonly description: "The agent could not redraft it; it still waits as it is.";
26
36
  }];
27
37
  export declare const AGENT_REVIEW_MODES: readonly [{
28
38
  readonly value: "always";
@@ -33,6 +43,10 @@ export declare const AGENT_REVIEW_MODES: readonly [{
33
43
  }];
34
44
  /** The longest reply a review holds; a provider may accept less in one message. */
35
45
  export declare const AGENT_REVIEW_TEXT_LIMIT = 20000;
46
+ /** The longest reason a member gives for rejecting a reply. */
47
+ export declare const AGENT_REVIEW_REASON_LIMIT = 2000;
48
+ /** The longest note a member gives the agent for redrafting a reply. */
49
+ export declare const AGENT_REVIEW_NOTE_LIMIT = 2000;
36
50
  export declare const AgentReview: import("./entity.js").Entity<"agent_review", {
37
51
  readonly key: import("./property.js").Property<string, false, true>;
38
52
  readonly provider: import("./property.js").Property<string, false, false>;
@@ -45,14 +59,20 @@ export declare const AgentReview: import("./entity.js").Entity<"agent_review", {
45
59
  readonly sessionId: import("./property.js").Property<string, false, false>;
46
60
  readonly draft: import("./property.js").Property<string, false, false>;
47
61
  readonly text: Schema.optionalKey<import("./property.js").Property<string, false, false>>;
48
- readonly status: import("./property.js").Property<"pending" | "sent" | "uncertain" | "failed" | "rejected", false, false>;
62
+ readonly status: import("./property.js").Property<"pending" | "sent" | "uncertain" | "failed" | "rejected" | "superseded", false, false>;
49
63
  readonly mode: import("./property.js").Property<"always" | "whenUnsure", false, false>;
50
64
  readonly confidence: Schema.optionalKey<import("./property.js").Property<number, false, false>>;
51
65
  readonly messageId: Schema.optionalKey<import("./property.js").Property<string, false, false>>;
52
66
  readonly decidedAt: Schema.optionalKey<import("./property.js").Property<string, false, false>>;
67
+ readonly reason: Schema.optionalKey<import("./property.js").Property<string, false, false>>;
68
+ readonly note: Schema.optionalKey<import("./property.js").Property<string, false, false>>;
69
+ readonly revision: Schema.optionalKey<import("./property.js").Property<string, false, false>>;
70
+ readonly revisionState: Schema.optionalKey<import("./property.js").Property<"failed" | "requested", false, false>>;
53
71
  }, {
54
72
  readonly person: import("./relationship.js").OneRelationship<false, false, "user">;
55
73
  readonly decidedBy: import("./relationship.js").OneRelationship<true, false, "user">;
74
+ readonly notedBy: import("./relationship.js").OneRelationship<true, false, "user">;
75
+ readonly revises: import("./relationship.js").OneRelationship<true, false, "agent_review">;
56
76
  readonly agentChat: import("./relationship.js").OneRelationship<true, false, "agent_chat">;
57
77
  }, import("./entity.js").InvocableActions<{
58
78
  readonly approve: import("./action.js").Action<Schema.Struct<{
@@ -66,10 +86,19 @@ export declare const AgentReview: import("./entity.js").Entity<"agent_review", {
66
86
  }>;
67
87
  readonly reject: import("./action.js").Action<Schema.Struct<{
68
88
  readonly review: Schema.Codec<string, string, never, never>;
89
+ readonly reason: Schema.optionalKey<import("./property.js").Property<string, false, false>>;
69
90
  }>, {
70
91
  review: string;
71
92
  status: "rejected";
72
93
  }>;
94
+ readonly revise: import("./action.js").Action<Schema.Struct<{
95
+ readonly review: Schema.Codec<string, string, never, never>;
96
+ readonly note: import("./property.js").Property<string, false, false>;
97
+ }>, {
98
+ review: string;
99
+ revision: string;
100
+ status: "requested";
101
+ }>;
73
102
  }, "agent_review", {
74
103
  readonly id: string;
75
104
  readonly createdAt: string;
@@ -87,13 +116,19 @@ export declare const AgentReview: import("./entity.js").Entity<"agent_review", {
87
116
  readonly sessionId: string;
88
117
  readonly draft: string;
89
118
  readonly text: string | null;
90
- readonly status: "pending" | "sent" | "uncertain" | "failed" | "rejected";
119
+ readonly status: "pending" | "sent" | "uncertain" | "failed" | "rejected" | "superseded";
91
120
  readonly mode: "always" | "whenUnsure";
92
121
  readonly confidence: number | null;
93
122
  readonly messageId: string | null;
94
123
  readonly decidedAt: string | null;
124
+ readonly reason: string | null;
125
+ readonly note: string | null;
126
+ readonly revision: string | null;
127
+ readonly revisionState: "failed" | "requested" | null;
95
128
  readonly personId: string;
96
129
  readonly decidedById: string | null;
130
+ readonly notedById: string | null;
131
+ readonly revisesId: string | null;
97
132
  readonly agentChatId: string | null;
98
133
  }>, Readonly<Record<never, never>>, "actions">;
99
134
  export type AgentReview = typeof AgentReview.Type;
@@ -23,6 +23,14 @@ export const AGENT_REVIEW_STATUSES = [
23
23
  },
24
24
  { value: "failed", description: "Approved; the provider refused it, and nothing was sent." },
25
25
  { value: "rejected", description: "Rejected: the person never sees it." },
26
+ {
27
+ value: "superseded",
28
+ description: "Redrafted at a member's request: the review that revises it replaces it.",
29
+ },
30
+ ];
31
+ export const AGENT_REVIEW_REVISION_STATES = [
32
+ { value: "requested", description: "A member asked the agent to redraft it; it is redrafting." },
33
+ { value: "failed", description: "The agent could not redraft it; it still waits as it is." },
26
34
  ];
27
35
  export const AGENT_REVIEW_MODES = [
28
36
  { value: "always", description: "Every reply in the chat waits for a member." },
@@ -33,6 +41,10 @@ export const AGENT_REVIEW_MODES = [
33
41
  ];
34
42
  /** The longest reply a review holds; a provider may accept less in one message. */
35
43
  export const AGENT_REVIEW_TEXT_LIMIT = 20_000;
44
+ /** The longest reason a member gives for rejecting a reply. */
45
+ export const AGENT_REVIEW_REASON_LIMIT = 2_000;
46
+ /** The longest note a member gives the agent for redrafting a reply. */
47
+ export const AGENT_REVIEW_NOTE_LIMIT = 2_000;
36
48
  export const AgentReview = entity("agent_review", {
37
49
  id: "ent_agent_review",
38
50
  label: "Agent reply review",
@@ -116,7 +128,35 @@ export const AgentReview = entity("agent_review", {
116
128
  optional: true,
117
129
  managed: true,
118
130
  label: "Decided",
119
- description: "When a member approved or rejected the reply.",
131
+ description: "When a member approved, rejected or had the reply redrafted.",
132
+ }),
133
+ reason: p.text({
134
+ optional: true,
135
+ managed: true,
136
+ maxLength: AGENT_REVIEW_REASON_LIMIT,
137
+ display: "multiline",
138
+ description: "Why the member rejected the reply, when they said: for the workspace's own review of " +
139
+ "the agent's replies. The person and the agent's chat with them never see it.",
140
+ }),
141
+ note: p.text({
142
+ optional: true,
143
+ managed: true,
144
+ maxLength: AGENT_REVIEW_NOTE_LIMIT,
145
+ display: "multiline",
146
+ description: "What the member asked the agent to change when they asked it to redraft the reply. " +
147
+ "Only the redraft reads it; the person and the agent's chat with them never see it.",
148
+ }),
149
+ revision: p.text({
150
+ optional: true,
151
+ managed: true,
152
+ maxLength: 64,
153
+ description: "The redraft last asked for, by its id: only its result replaces the reply.",
154
+ }),
155
+ revisionState: p.select(AGENT_REVIEW_REVISION_STATES, {
156
+ optional: true,
157
+ managed: true,
158
+ label: "Revision",
159
+ description: "Whether the agent is redrafting the reply, or could not.",
120
160
  }),
121
161
  },
122
162
  relationships: {
@@ -132,7 +172,21 @@ export const AgentReview = entity("agent_review", {
132
172
  inverse: "decidedAgentReviews",
133
173
  onDelete: "setNull",
134
174
  phrase: ["was decided by", "decided"],
135
- description: "The member who approved or rejected the reply.",
175
+ description: "The member who approved, rejected or had the reply redrafted.",
176
+ }),
177
+ notedBy: rel.one("user", {
178
+ optional: true,
179
+ inverse: "notedAgentReviews",
180
+ onDelete: "setNull",
181
+ phrase: ["was sent back by", "sent back"],
182
+ description: "The member who last asked the agent to redraft the reply.",
183
+ }),
184
+ revises: rel.one("agent_review", {
185
+ optional: true,
186
+ inverse: "revisions",
187
+ onDelete: "setNull",
188
+ phrase: ["revises", "is revised by"],
189
+ description: "The reply this one redrafts at a member's request.",
136
190
  }),
137
191
  agentChat: rel.one("agent_chat", {
138
192
  optional: true,
@@ -143,7 +197,7 @@ export const AgentReview = entity("agent_review", {
143
197
  "is in, or a person's private chat in the inbox. Empty when the chat is not registered.",
144
198
  }),
145
199
  },
146
- indexes: [{ properties: ["status"] }],
200
+ indexes: [{ properties: ["status"] }, { properties: ["sessionId"] }],
147
201
  }).actions((self) => ({
148
202
  approve: action({
149
203
  description: "Send a reply the agent held for review to the person, as written or with your edit in " +
@@ -193,10 +247,17 @@ export const AgentReview = entity("agent_review", {
193
247
  }),
194
248
  }),
195
249
  reject: action({
196
- description: "Discard a reply the agent held for review: the person never sees it. A review already " +
197
- "approved is refused.",
250
+ description: "Discard a reply the agent held for review: the person never sees it. Say why in reason " +
251
+ "when the member gives one; it stays with the review and never reaches the person or the " +
252
+ "agent's chat with them. A review already approved is refused.",
198
253
  hints: { destructive: true, irreversible: false, idempotent: true },
199
- input: Schema.Struct({ review: self.ref }),
254
+ input: Schema.Struct({
255
+ review: self.ref,
256
+ reason: Schema.optionalKey(p.text({
257
+ maxLength: AGENT_REVIEW_REASON_LIMIT,
258
+ description: "Why the reply should not be sent, in the member's words.",
259
+ })),
260
+ }),
200
261
  handler: ({ input, principal }) => Effect.gen(function* () {
201
262
  const review = yield* self.get(input.review, { forUpdate: true });
202
263
  if (review.status === "rejected") {
@@ -207,12 +268,46 @@ export const AgentReview = entity("agent_review", {
207
268
  message: "The reply was already approved and sent; it cannot be rejected.",
208
269
  });
209
270
  }
271
+ const reason = input.reason?.trim();
210
272
  yield* self.update(review, {
211
273
  status: "rejected",
212
274
  decidedAt: new Date().toISOString(),
213
275
  decidedBy: principal.id,
276
+ ...(reason === undefined || reason === "" ? {} : { reason }),
214
277
  });
215
278
  return { review: review.id, status: "rejected" };
216
279
  }),
217
280
  }),
281
+ revise: action({
282
+ description: "Ask the agent to redraft a reply it held for review, with what to change in note, in the " +
283
+ "member's words. The agent writes it again in a session of its own, which reads the chat " +
284
+ "and what it looked up for the reply but changes nothing; the new draft replaces this one " +
285
+ "as a review that waits for a member. The person and the agent's chat with them never see " +
286
+ "the note. Only a reply that still waits can be redrafted.",
287
+ hints: { destructive: false, irreversible: false, idempotent: false },
288
+ input: Schema.Struct({
289
+ review: self.ref,
290
+ note: p.text({
291
+ maxLength: AGENT_REVIEW_NOTE_LIMIT,
292
+ description: "What the agent should change, in the member's words.",
293
+ }),
294
+ }),
295
+ connections: [],
296
+ prepare: ({ input }) => Effect.gen(function* () {
297
+ const delivery = yield* Effect.serviceOption(ChatDelivery);
298
+ const revise = Option.isSome(delivery) ? delivery.value.reviseReview : undefined;
299
+ if (revise === undefined) {
300
+ return yield* new ActionRejected({
301
+ message: "Redrafting a reply requires the authenticated ontology service.",
302
+ });
303
+ }
304
+ return yield* revise({ review: String(input.review), note: input.note.trim() });
305
+ }),
306
+ // The request is recorded before the agent is asked, so that its result always finds it.
307
+ handler: ({ input, prepared }) => Effect.succeed({
308
+ review: String(input.review),
309
+ revision: prepared.revision,
310
+ status: "requested",
311
+ }),
312
+ }),
218
313
  }));
package/dist/chat.d.ts CHANGED
@@ -62,16 +62,32 @@ export interface ReviewSendResult {
62
62
  readonly messageId: string | null;
63
63
  readonly duplicate: boolean;
64
64
  }
65
+ /** A member's request that the agent redraft a reply it held for review. */
66
+ export interface ReviewReviseInput {
67
+ /** The `agent_review` record's id. */
68
+ readonly review: string;
69
+ /** What the agent should change, in the member's words. */
70
+ readonly note: string;
71
+ }
72
+ /** The redraft the agent started: its result replaces the review only while it is the last one. */
73
+ export interface ReviewReviseResult {
74
+ readonly revision: string;
75
+ }
65
76
  declare const ChatDelivery_base: Context.ServiceClass<ChatDelivery, "@segmentstream/ontology/ChatDelivery", {
66
77
  readonly preview: (input: ChatPostInput) => Effect.Effect<ChatPreview, ActionError>;
67
78
  readonly validate: (input: ChatPostInput) => Effect.Effect<void, ActionError>;
68
79
  readonly post: (input: ChatPostInput) => Effect.Effect<ChatPostResult, ActionError>;
69
80
  /** Sends a reply a member approved, as the agent and without attribution, at most once. */
70
81
  readonly sendReview?: (input: ReviewSendInput) => Effect.Effect<ReviewSendResult, ActionError>;
82
+ /** Asks the agent to redraft a reply it held for review, in a session of its own. */
83
+ readonly reviseReview?: (input: ReviewReviseInput) => Effect.Effect<ReviewReviseResult, ActionError>;
71
84
  /** The channels whose private chats staff may take over, as the ontology's inbox lists them. */
72
85
  readonly takeoverChannels?: ReadonlySet<string>;
73
86
  }>;
74
- /** Deliver the exact Markdown source through the channel renderer, without invoking an agent. */
87
+ /**
88
+ * Deliver the exact Markdown source through the channel renderer, without invoking an agent; and
89
+ * hand a reply the agent held for review back to the agent to redraft.
90
+ */
75
91
  export declare class ChatDelivery extends ChatDelivery_base {
76
92
  }
77
93
  export {};
package/dist/chat.js CHANGED
@@ -29,6 +29,9 @@ export const chatPreviewDigest = (preview) => chatProtocolDigest([
29
29
  preview.finalText,
30
30
  preview.replyTo,
31
31
  ]);
32
- /** Deliver the exact Markdown source through the channel renderer, without invoking an agent. */
32
+ /**
33
+ * Deliver the exact Markdown source through the channel renderer, without invoking an agent; and
34
+ * hand a reply the agent held for review back to the agent to redraft.
35
+ */
33
36
  export class ChatDelivery extends Context.Service()("@segmentstream/ontology/ChatDelivery") {
34
37
  }
package/dist/index.d.ts CHANGED
@@ -66,7 +66,7 @@ export { AppFeedback, AppFeedbackFile, APP_FEEDBACK_ENTITIES, APP_FEEDBACK_FILE_
66
66
  export { ChatDelivery, ChatPostInput, ChatPostResult } from "./chat.js";
67
67
  export { CHAT_CHANNEL_KINDS, CHAT_CHANNEL_NAME, CHAT_KINDS, WORKSPACE_CHANNEL_KIND, channelIdentityProvider, } from "./chat-channel.js";
68
68
  export type { ChatChannelKind, ChatKind } from "./chat-channel.js";
69
- export type { InboxOptions, ReviewSendInput, ReviewSendResult } from "./chat.js";
69
+ export type { InboxOptions, ReviewReviseInput, ReviewReviseResult, ReviewSendInput, ReviewSendResult, } from "./chat.js";
70
70
  export { DEFAULT_QUERY_LIMIT, entityQueryResultSchema, entityQuerySchema, entityQuerySchemas, entityRecordSchema, FILTER_OPERATORS, LIST_FILTER_OPERATORS, MANY_QUANTIFIERS, MAX_IN_VALUES, MAX_QUERY_LIMIT, OrderBy, Query, queryFields, queryJsonSchema, queryLinks, QueryResult, queryResultJsonSchema, queryResultSchema, SortDirection, WHERE_COMBINATORS, } from "./query.js";
71
71
  export type { EntityQuerySchemas, FieldFilter, FilterOperator, JsonRecord, ListFilterOperator, ManyFilter, QueryField, QueryLink, StorageType, Where, } from "./query.js";
72
72
  export { InvalidQuery, InvalidValues, MemberNotFound, PrincipalRejected, StoredRecordMismatch, UnknownAction, UnknownRole, ValidationIssue, UndeclaredWrite, WriteOutsideAction, } from "./runtime/errors.js";
@@ -6,7 +6,9 @@ import { type AgentResolution, type AgentStore, type ResolveIdentityInput, type
6
6
  * The agent store on the runtime's agent tables, as `ontology_app`. Expiry is the database's
7
7
  * `now()`; a pairing is consumed with one `UPDATE … WHERE "consumed_at" IS NULL`, so two concurrent
8
8
  * approvals cannot both succeed. Ids that are not UUIDs find nothing rather than fail the cast.
9
- * Chats the agent sees are `agent_chat` records, written as the platform with history.
9
+ * Chats the agent sees are `agent_chat` records, written as the platform with history. Holding a
10
+ * reply is admitted, bounded and starts the workflows whose triggers on `agent_review` fire for it as
11
+ * an action is and does, with the `ActionTransactions` and `WorkflowStarter` it runs with.
10
12
  */
11
13
  export declare function sqlAgentStore(ontology: Ontology, sql: SqlClient.SqlClient): AgentStore;
12
14
  /** Shared verified admission for agents and channel-app adapters; never expose trusted inputs to browsers. */