@segmentstream/agent 0.73.1 → 0.74.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 (43) hide show
  1. package/CHANGELOG.md +32 -0
  2. package/dist/_chunks/{build-metadata-B-1ohlK8.mjs → build-metadata-Cq9R60V5.mjs} +1 -1
  3. package/dist/_chunks/{chat-archive-BCPLSZeK.mjs → chat-archive-BgxTGPKZ.mjs} +4 -1
  4. package/dist/_chunks/{chat-archive-DK7uCgff.mjs → chat-archive-oJWmk8wm.mjs} +1 -1
  5. package/dist/extension/extension.d.ts +1 -1
  6. package/dist/extension/hooks/chat-context.mjs +1 -1
  7. package/dist/extension/hooks/chat-recorder.mjs +1 -1
  8. package/dist/extension/lib/build-metadata.mjs +1 -1
  9. package/dist/extension/lib/chat-archive.d.ts +6 -1
  10. package/dist/extension/lib/chat-archive.mjs +1 -1
  11. package/dist/extension/lib/chat-context.mjs +1 -1
  12. package/dist/extension/lib/chat-messages.d.ts +102 -0
  13. package/dist/extension/lib/chat-messages.mjs +168 -4
  14. package/dist/extension/lib/chat-recorder.mjs +44 -13
  15. package/dist/extension/lib/chat-store.d.ts +23 -1
  16. package/dist/extension/lib/email/channel.mjs +1 -1
  17. package/dist/extension/lib/group-routing/routing-context.mjs +1 -1
  18. package/dist/extension/lib/model-service-alert.mjs +1 -1
  19. package/dist/extension/lib/review/judge.mjs +1 -1
  20. package/dist/extension/lib/review/reply.mjs +1 -1
  21. package/dist/extension/lib/review/revision-channel.mjs +1 -1
  22. package/dist/extension/lib/slack/adapter.mjs +1 -1
  23. package/dist/extension/lib/telegram/adapter.mjs +3 -3
  24. package/dist/extension/lib/telegram/chat-archive.mjs +1 -1
  25. package/dist/extension/lib/telegram/confirmations.mjs +1 -1
  26. package/dist/extension/lib/telegram/delivery-diagnostics.mjs +1 -1
  27. package/dist/extension/lib/telegram/health.mjs +1 -1
  28. package/dist/extension/lib/telegram/inbound.mjs +1 -1
  29. package/dist/extension/lib/telegram/lifecycle.mjs +1 -1
  30. package/dist/extension/lib/telegram/store-capture.mjs +1 -1
  31. package/dist/extension/lib/telegram/turn-errors.mjs +1 -1
  32. package/dist/extension/lib/whatsapp/adapter.d.ts +21 -1
  33. package/dist/extension/lib/whatsapp/adapter.mjs +82 -13
  34. package/dist/extension/lib/whatsapp/attachments-send.mjs +1 -1
  35. package/dist/extension/lib/whatsapp/changes.d.ts +46 -0
  36. package/dist/extension/lib/whatsapp/changes.mjs +212 -0
  37. package/dist/extension/lib/whatsapp/sent.mjs +1 -1
  38. package/dist/extension/lib/whatsapp/statuses.d.ts +9 -0
  39. package/dist/extension/lib/whatsapp/statuses.mjs +9 -0
  40. package/dist/extension/lib/workspace-channel/channel.d.ts +68 -7
  41. package/dist/extension/lib/workspace-channel/channel.mjs +149 -33
  42. package/dist/extension/lib/workspace-chat-archive.mjs +1 -1
  43. package/package.json +2 -2
@@ -5,7 +5,7 @@ const __filename = __eveFileURLToPath(import.meta.url);
5
5
  __eveDirname(__filename);
6
6
  __eveCreateRequire(import.meta.url);
7
7
  import { channelKey } from "../channel-scope.mjs";
8
- import { a as chatEvent, t as AGENT_SENDER } from "../../../_chunks/chat-archive-BCPLSZeK.mjs";
8
+ import { a as chatEvent, t as AGENT_SENDER } from "../../../_chunks/chat-archive-BgxTGPKZ.mjs";
9
9
  import { stampedChat } from "../chat-recorder.mjs";
10
10
  import { AsyncLocalStorage } from "node:async_hooks";
11
11
  const collected = new AsyncLocalStorage();
@@ -0,0 +1,9 @@
1
+ /**
2
+ * The header the agent's WhatsApp channel answers a relayed delivery with, `recorded`, once it
3
+ * kept the delivery's statuses in the workspace chat store: a failure Meta reports for a message is
4
+ * then the message's own, and the relay records it nowhere else. An agent that keeps its chats in
5
+ * Cloud's archive, or one released before it read statuses, answers none.
6
+ */
7
+ export declare const STATUSES_HEADER = "x-segmentstream-message-statuses";
8
+ /** The value of {@link STATUSES_HEADER}. */
9
+ export declare const STATUSES_RECORDED = "recorded";
@@ -0,0 +1,9 @@
1
+ import { fileURLToPath as __eveFileURLToPath } from "node:url";
2
+ import { dirname as __eveDirname } from "node:path";
3
+ import { createRequire as __eveCreateRequire } from "node:module";
4
+ const __filename = __eveFileURLToPath(import.meta.url);
5
+ __eveDirname(__filename);
6
+ __eveCreateRequire(import.meta.url);
7
+ const STATUSES_HEADER = "x-segmentstream-message-statuses";
8
+ const STATUSES_RECORDED = "recorded";
9
+ export { STATUSES_HEADER, STATUSES_RECORDED };
@@ -3,6 +3,7 @@ import type { SessionAuthContext } from "eve/context";
3
3
  import { type SegmentStreamAgentConfigInput } from "../../config.js";
4
4
  import { type AgentAttachments } from "../attachments.js";
5
5
  import { type ChatArchive } from "../chat-archive.js";
6
+ import { type ChatPart } from "../chat-messages.js";
6
7
  import { type Environment } from "../environment.js";
7
8
  import { type AgentIdentities, type PairedMember } from "../identities.js";
8
9
  import { type CapabilityLookup } from "../inbound-media/plan.js";
@@ -15,6 +16,7 @@ import { type RuntimeToken } from "../runtime-token.js";
15
16
  import { type ChannelConnection } from "./connection.js";
16
17
  import { type ChannelChecks } from "./secrets.js";
17
18
  export type { ChannelChecks } from "./secrets.js";
19
+ export type { ChatPart } from "../chat-messages.js";
18
20
  /**
19
21
  * A channel the workspace defines in its own code (`docs/agent.md`, "Workspace channels"), for any
20
22
  * provider with an API: a CRM inbox, a marketplace's buyer chat, an in-house messenger. The
@@ -68,6 +70,10 @@ export interface ChannelMessage {
68
70
  readonly chatId: string;
69
71
  /** Who wrote it: the person, or for an `outbound` message the workspace's account. */
70
72
  readonly sender: ChannelSender;
73
+ /**
74
+ * What the agent reads. It may be empty when the message carries files or parts: a message with
75
+ * neither text nor files starts no turn.
76
+ */
71
77
  readonly text: string;
72
78
  /** Files the message carries; read only when `limits.files` is on. */
73
79
  readonly files?: ReadonlyArray<ChannelFile>;
@@ -82,6 +88,59 @@ export interface ChannelMessage {
82
88
  /** Who wrote an `outbound` message: `human` (the default), or `bot` for another program. */
83
89
  readonly by?: "human" | "bot";
84
90
  }
91
+ /**
92
+ * A message one provider event holds, with what the provider says of it beside its text, which the
93
+ * chat keeps: the message it quotes, when it was written, and its parts, such as a location or a
94
+ * contact card. The agent reads its text and files; its parts are for the chat, so a message whose
95
+ * only content is a part starts no turn, and the inbox shows it.
96
+ */
97
+ export type ChannelMessageEvent = ChannelMessage & {
98
+ readonly type?: "message";
99
+ /** The provider's id of the message it quotes. */
100
+ readonly replyTo?: string;
101
+ /** When it was written, ISO 8601; by default when it arrived. */
102
+ readonly at?: string;
103
+ readonly parts?: ReadonlyArray<ChatPart>;
104
+ };
105
+ /**
106
+ * What one provider event holds, as {@link ChannelDefinition.parse} reads it: messages, and
107
+ * changes to messages of the person's chat, each found by the provider's id of its message, the
108
+ * `target`. No change starts a turn: the chat shows it, and the agent reads an edited or deleted
109
+ * message again, marked, in its next turn. A change to a message the chat does not hold is
110
+ * dropped.
111
+ *
112
+ * - `reaction`: who reacted to a message, with which emoji; an empty emoji removes theirs.
113
+ * - `edit`: the message's new text.
114
+ * - `deletion`: the message is gone; the chat keeps its place, without its content.
115
+ * - `status`: what became of a message the workspace's side sent, the agent's replies among them:
116
+ * `delivered`, `read` (which implies delivered), or `failed`, with the provider's `reason` as a
117
+ * code. A status never moves a message backwards, except to `failed`.
118
+ */
119
+ export type ChannelEvent = ChannelMessageEvent | {
120
+ readonly type: "reaction";
121
+ readonly chatId: string;
122
+ readonly sender: ChannelSender;
123
+ readonly target: string;
124
+ readonly emoji: string;
125
+ } | {
126
+ readonly type: "edit";
127
+ readonly chatId: string;
128
+ readonly target: string;
129
+ readonly text: string;
130
+ readonly at?: string;
131
+ } | {
132
+ readonly type: "deletion";
133
+ readonly chatId: string;
134
+ readonly target: string;
135
+ readonly at?: string;
136
+ } | {
137
+ readonly type: "status";
138
+ readonly chatId: string;
139
+ readonly target: string;
140
+ readonly status: "delivered" | "read" | "failed";
141
+ /** The provider's refusal of a failed message, as a code such as `recipient_unreachable`. */
142
+ readonly reason?: string;
143
+ };
85
144
  /** A file the agent sends, its bytes read. */
86
145
  export interface ChannelReplyFile {
87
146
  readonly filename: string;
@@ -109,7 +168,8 @@ export interface ChannelContext {
109
168
  export interface ChannelRead {
110
169
  /**
111
170
  * The provider's events, each read as {@link ChannelDefinition.parse} reads a webhook's, holding
112
- * at most 50 messages together: a larger read is refused whole and its cursor kept.
171
+ * at most 50 messages together, whatever changes beside them: a larger read is refused whole and
172
+ * its cursor kept.
113
173
  */
114
174
  readonly events: ReadonlyArray<unknown>;
115
175
  /** Where the next read starts; the platform keeps it, at most 16,384 characters. */
@@ -148,11 +208,11 @@ export interface ChannelDefinition {
148
208
  };
149
209
  };
150
210
  /**
151
- * The messages one provider event holds: a webhook's body, parsed as JSON when it is JSON, or an
152
- * event a poll read. Events that hold no message, such as delivery receipts, hold none; what the
153
- * workspace's side sent is an `outbound` message.
211
+ * The messages and changes one provider event holds ({@link ChannelEvent}): a webhook's body,
212
+ * parsed as JSON when it is JSON, or an event a poll read. What the workspace's side sent is an
213
+ * `outbound` message; a delivery receipt is a `status`. An event that holds nothing holds none.
154
214
  */
155
- parse(event: unknown, context: ChannelContext): ReadonlyArray<ChannelMessage> | Promise<ReadonlyArray<ChannelMessage>>;
215
+ parse(event: unknown, context: ChannelContext): ReadonlyArray<ChannelEvent> | Promise<ReadonlyArray<ChannelEvent>>;
156
216
  /** Sends one message; answers the provider's id of it, when it gives one. */
157
217
  send(reply: ChannelReply, context: ChannelContext): Promise<{
158
218
  readonly id?: string;
@@ -192,8 +252,9 @@ export interface WorkspaceChannelOptions extends ChannelDefinition {
192
252
  readonly log?: (message: string, detail: unknown) => void;
193
253
  }
194
254
  /**
195
- * The most messages one webhook delivery or one read may hold. A larger one is refused whole,
196
- * before any of it starts a turn: its cursor stays, and the provider's delivery is answered 413.
255
+ * The most messages one webhook delivery or one read may hold, whatever changes beside them. A
256
+ * larger one is refused whole, before any of it starts a turn: its cursor stays, and the provider's
257
+ * delivery is answered 413.
197
258
  */
198
259
  export declare const MAXIMUM_MESSAGES = 50;
199
260
  /**
@@ -10,8 +10,8 @@ import { agentToolsAttribute } from "../agent-tools.mjs";
10
10
  import { providerChat } from "../attachment-tools.mjs";
11
11
  import { Bt as workspaceIdentityProvider, Pt as chatAddressOf, S as fetchStoredAttachment, b as agentAttachments, d as prepareInboundMedia, i as memberIdOf, m as turnContent, zt as withChatAddress } from "../../../_chunks/principal-Zf42Yf4r.mjs";
12
12
  import { CHANNEL_ATTRIBUTE, channelKey } from "../channel-scope.mjs";
13
- import { a as chatEvent, r as chatArchive, t as AGENT_SENDER } from "../../../_chunks/chat-archive-BCPLSZeK.mjs";
14
- import { recordHandledMessage, recordSentOutside } from "../chat-messages.mjs";
13
+ import { a as chatEvent, b as reasonCode, r as chatArchive, t as AGENT_SENDER } from "../../../_chunks/chat-archive-BgxTGPKZ.mjs";
14
+ import { applyChatChange, chatParts, recordHandledMessage, recordReceivedMessage, recordSentOutside, stagedFiles } from "../chat-messages.mjs";
15
15
  import { connectionProxy } from "../connection-proxy.mjs";
16
16
  import { redactDiagnostic } from "../diagnostics.mjs";
17
17
  import { appOrigin, channelsEnabled, proxyEnvironment, runtimeIdentityEnvironment, webhookSecret } from "../environment.mjs";
@@ -50,24 +50,90 @@ function signed(secret, body, signature) {
50
50
  function isRecord(value) {
51
51
  return typeof value === "object" && value !== null && !Array.isArray(value);
52
52
  }
53
- function validMessage(value) {
54
- if (!isRecord(value) || !isRecord(value.sender)) return;
55
- const { id, chatId, sender, text, files, direction, by } = value;
56
- return typeof id === "string" && WORKSPACE_CHANNEL_ID.test(id) && typeof chatId === "string" && WORKSPACE_CHANNEL_ID.test(chatId) && typeof sender.id === "string" && WORKSPACE_CHANNEL_ID.test(sender.id) && (sender.name === void 0 || typeof sender.name === "string") && typeof text === "string" && (files === void 0 || Array.isArray(files)) && (direction === void 0 || direction === "inbound" || direction === "outbound") && (by === void 0 || by === "human" || by === "bot") ? {
53
+ function validId(value) {
54
+ return typeof value === "string" && WORKSPACE_CHANNEL_ID.test(value);
55
+ }
56
+ function validTime(value) {
57
+ if (value === void 0) return;
58
+ return typeof value === "string" && !Number.isNaN(Date.parse(value)) ? new Date(Date.parse(value)).toISOString() : null;
59
+ }
60
+ function validSender(value) {
61
+ if (!isRecord(value)) return;
62
+ const { id, name } = value;
63
+ return validId(id) && (name === void 0 || typeof name === "string") ? {
57
64
  id,
58
- chatId,
59
- sender: {
60
- id: sender.id,
61
- ...sender.name?.trim() ? { name: sender.name.trim() } : {}
65
+ ...name?.trim() ? { name: name.trim() } : {}
66
+ } : void 0;
67
+ }
68
+ function validMessage(value) {
69
+ const { id, chatId, text, files, direction, by, replyTo } = value;
70
+ const sender = validSender(value.sender);
71
+ const at = validTime(value.at);
72
+ const parts = value.parts === void 0 ? [] : chatParts(value.parts);
73
+ return validId(id) && validId(chatId) && sender !== void 0 && typeof text === "string" && (files === void 0 || Array.isArray(files)) && (direction === void 0 || direction === "inbound" || direction === "outbound") && (by === void 0 || by === "human" || by === "bot") && (replyTo === void 0 || validId(replyTo)) && at !== null && parts !== void 0 ? {
74
+ kind: "message",
75
+ message: {
76
+ id,
77
+ chatId,
78
+ sender,
79
+ text: text.slice(0, MAXIMUM_TEXT),
80
+ ...files === void 0 ? {} : { files },
81
+ ...direction === "outbound" ? {
82
+ direction,
83
+ by: by ?? "human"
84
+ } : {}
62
85
  },
63
- text: text.slice(0, MAXIMUM_TEXT),
64
- ...files === void 0 ? {} : { files },
65
- ...direction === "outbound" ? {
66
- direction,
67
- by: by ?? "human"
68
- } : {}
86
+ ...replyTo === void 0 ? {} : { replyTo },
87
+ ...at === void 0 ? {} : { at },
88
+ parts
69
89
  } : void 0;
70
90
  }
91
+ const MAXIMUM_EMOJI = 64;
92
+ function validChange(value) {
93
+ const at = validTime(value.at);
94
+ switch (value.type) {
95
+ case "reaction": {
96
+ const sender = validSender(value.sender);
97
+ return sender !== void 0 && typeof value.emoji === "string" && value.emoji.length <= MAXIMUM_EMOJI ? {
98
+ change: "reaction",
99
+ senderId: sender.id,
100
+ emoji: value.emoji.trim()
101
+ } : void 0;
102
+ }
103
+ case "edit": return typeof value.text === "string" && at !== null ? {
104
+ change: "edit",
105
+ text: value.text.slice(0, MAXIMUM_TEXT),
106
+ ...at === void 0 ? {} : { at }
107
+ } : void 0;
108
+ case "deletion": return at === null ? void 0 : {
109
+ change: "delete",
110
+ ...at === void 0 ? {} : { at }
111
+ };
112
+ case "status": {
113
+ const failure = typeof value.reason === "string" ? reasonCode(value.reason) : void 0;
114
+ return (value.status === "delivered" || value.status === "read" || value.status === "failed") && (value.reason === void 0 || typeof value.reason === "string") ? {
115
+ change: "status",
116
+ status: value.status,
117
+ ...value.status === "failed" && failure !== void 0 ? { failure } : {}
118
+ } : void 0;
119
+ }
120
+ default: return;
121
+ }
122
+ }
123
+ function validEvent(value) {
124
+ if (!isRecord(value)) return;
125
+ if (value.type === void 0 || value.type === "message") return validMessage(value);
126
+ const { chatId, target } = value;
127
+ const change = validId(chatId) && validId(target) ? validChange(value) : void 0;
128
+ return change === void 0 ? void 0 : {
129
+ kind: "change",
130
+ change: {
131
+ ...change,
132
+ chatId,
133
+ target
134
+ }
135
+ };
136
+ }
71
137
  function splitReply(text, maxLength) {
72
138
  if (maxLength === void 0 || text.length <= maxLength) return [text];
73
139
  const parts = [];
@@ -215,7 +281,13 @@ function workspaceChannelDefinition(options) {
215
281
  }
216
282
  return parts;
217
283
  }
218
- async function dispatch(message, args) {
284
+ async function dispatch(read, args) {
285
+ const { message, parts } = read;
286
+ const beside = {
287
+ ...read.replyTo === void 0 ? {} : { replyTo: read.replyTo },
288
+ ...parts.length === 0 ? {} : { parts },
289
+ ...read.at === void 0 ? {} : { at: read.at }
290
+ };
219
291
  if (message.direction === "outbound") {
220
292
  await recordSentOutside(archive(), {
221
293
  provider: name,
@@ -230,7 +302,8 @@ function workspaceChannelDefinition(options) {
230
302
  name: file.filename,
231
303
  mimeType: file.contentType,
232
304
  ...file.size === void 0 ? {} : { size: file.size }
233
- }))
305
+ })),
306
+ ...beside
234
307
  });
235
308
  return;
236
309
  }
@@ -294,7 +367,7 @@ function workspaceChannelDefinition(options) {
294
367
  if (text !== void 0 && turn) await notice(text);
295
368
  return;
296
369
  }
297
- if (!turn) return;
370
+ if (!turn && parts.length === 0) return;
298
371
  if (resolution.handled === true) {
299
372
  await recordHandledMessage(archive(), {
300
373
  chat: {
@@ -310,7 +383,22 @@ function workspaceChannelDefinition(options) {
310
383
  name: file.filename,
311
384
  mimeType: file.contentType,
312
385
  ...file.size === void 0 ? {} : { size: file.size }
313
- }))
386
+ })),
387
+ ...beside
388
+ });
389
+ return;
390
+ }
391
+ if (!turn) {
392
+ await recordReceivedMessage(archive(), {
393
+ channel: name,
394
+ chatId: message.chatId,
395
+ memberId: resolution.member.id,
396
+ senderId: message.sender.id,
397
+ senderName: message.sender.name ?? null,
398
+ messageId: message.id,
399
+ text: message.text,
400
+ ...beside,
401
+ outcome: { skipped: "no_text" }
314
402
  });
315
403
  return;
316
404
  }
@@ -354,7 +442,7 @@ function workspaceChannelDefinition(options) {
354
442
  } }
355
443
  });
356
444
  const keys = new Map();
357
- const parts = inbound.filter((file) => media.files.has(file.key) || media.spoken.keys.has(file.key)).map((file) => {
445
+ const fileParts = inbound.filter((file) => media.files.has(file.key) || media.spoken.keys.has(file.key)).map((file) => {
358
446
  const part = {
359
447
  type: "file",
360
448
  data: new Uint8Array(),
@@ -364,10 +452,10 @@ function workspaceChannelDefinition(options) {
364
452
  keys.set(part, file.key);
365
453
  return part;
366
454
  });
367
- const content = parts.length === 0 ? media.text : turnContent([{
455
+ const content = fileParts.length === 0 ? media.text : turnContent([{
368
456
  type: "text",
369
457
  text: media.text
370
- }, ...parts], {
458
+ }, ...fileParts], {
371
459
  files: media.files,
372
460
  keyOf: (part) => keys.get(part),
373
461
  readStored: true,
@@ -376,11 +464,36 @@ function workspaceChannelDefinition(options) {
376
464
  text: ""
377
465
  }
378
466
  });
379
- await args.from(channelKey(WORKSPACE_CHANNEL_KIND, name, message.chatId)).send(content, {
467
+ const delivery = await args.from(channelKey(WORKSPACE_CHANNEL_KIND, name, message.chatId)).send(content, {
380
468
  auth,
381
469
  idempotencyKey: channelKey(WORKSPACE_CHANNEL_KIND, name, `${WORKSPACE_CHANNEL_KIND}:${message.id}`),
382
470
  ...media.context.length === 0 ? {} : { context: media.context }
383
471
  });
472
+ if (typeof delivery?.deliveryId === "string" && typeof delivery.sessionId === "string") await recordReceivedMessage(archive(), {
473
+ channel: name,
474
+ chatId: message.chatId,
475
+ memberId,
476
+ senderId: message.sender.id,
477
+ senderName: message.sender.name ?? null,
478
+ messageId: message.id,
479
+ text: media.text,
480
+ files: stagedFiles(media.files),
481
+ ...beside,
482
+ outcome: { turn: {
483
+ sessionId: delivery.sessionId,
484
+ deliveryId: delivery.deliveryId
485
+ } }
486
+ });
487
+ }
488
+ async function change(read) {
489
+ await applyChatChange(archive(), {
490
+ ...read.change,
491
+ channel: name
492
+ });
493
+ }
494
+ async function handle(events, args) {
495
+ for (const event of events) if (event.kind === "message") await dispatch(event, args);
496
+ else await change(event);
384
497
  }
385
498
  async function lastChat(account) {
386
499
  const store = archive();
@@ -395,18 +508,21 @@ function workspaceChannelDefinition(options) {
395
508
  participantId: lookup.member.id
396
509
  }) : void 0;
397
510
  }
398
- async function messagesOf(events) {
399
- const messages = [];
511
+ async function eventsOf(events) {
512
+ const read = [];
400
513
  for (const event of events) {
401
514
  const parsed = await options.parse(event, context());
402
515
  for (const candidate of Array.isArray(parsed) ? parsed : []) {
403
- const message = validMessage(candidate);
404
- if (message === void 0) report("message_invalid", new Error("The channel's parse returned an invalid message."));
405
- else messages.push(message);
516
+ const valid = validEvent(candidate);
517
+ if (valid === void 0) {
518
+ const message = !isRecord(candidate) || candidate.type === void 0 || candidate.type === "message";
519
+ report(message ? "message_invalid" : "change_invalid", new Error(`The channel's parse returned an invalid ${message ? "message" : "change"}.`));
520
+ } else read.push(valid);
406
521
  }
407
522
  }
408
- if (messages.length > 50) throw new TooManyMessages(`The channel's parse read ${messages.length} messages from one delivery or read; at most 50 start turns at once.`);
409
- return messages;
523
+ const messages = read.filter((event) => event.kind === "message").length;
524
+ if (messages > 50) throw new TooManyMessages(`The channel's parse read ${messages} messages from one delivery or read; at most 50 start turns at once.`);
525
+ return read;
410
526
  }
411
527
  async function receive(request, args) {
412
528
  if (!channelsEnabled(environment)) return error("not_found", 404);
@@ -438,7 +554,7 @@ function workspaceChannelDefinition(options) {
438
554
  event = JSON.parse(body);
439
555
  } catch {}
440
556
  try {
441
- for (const message of await messagesOf([event])) await dispatch(message, args);
557
+ await handle(await eventsOf([event]), args);
442
558
  } catch (cause) {
443
559
  report("webhook_failed", cause);
444
560
  return cause instanceof TooManyMessages ? error("too_many_messages", 413) : error("unavailable", 503);
@@ -464,7 +580,7 @@ function workspaceChannelDefinition(options) {
464
580
  report("read_invalid", new Error("The channel's read returned an invalid cursor or events."));
465
581
  return error("unavailable", 503);
466
582
  }
467
- for (const message of await messagesOf(read.events)) await dispatch(message, args);
583
+ await handle(await eventsOf(read.events), args);
468
584
  return Response.json({
469
585
  cursor,
470
586
  more: read.more === true
@@ -4,4 +4,4 @@ import { createRequire as __eveCreateRequire } from "node:module";
4
4
  const __filename = __eveFileURLToPath(import.meta.url);
5
5
  __eveDirname(__filename);
6
6
  __eveCreateRequire(import.meta.url);
7
- export { h as boundedMeta, g as cutText, _ as eventOf, v as messageInput, y as metaOf, b as reasonCode, x as sendFingerprint, S as storedFiles, C as storedMessageId, w as workspaceChatArchive } from "../../_chunks/chat-archive-BCPLSZeK.mjs";
7
+ export { h as boundedMeta, g as cutText, _ as eventOf, v as messageInput, y as metaOf, b as reasonCode, x as sendFingerprint, S as storedFiles, C as storedMessageId, w as workspaceChatArchive } from "../../_chunks/chat-archive-BgxTGPKZ.mjs";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@segmentstream/agent",
3
- "version": "0.73.1",
3
+ "version": "0.74.0",
4
4
  "description": "The SegmentStream workspace agent: the ontology as tools, acting as the member who writes, and the browser, Telegram, WhatsApp and Slack through the workspace's connections.",
5
5
  "license": "Apache-2.0",
6
6
  "author": "SegmentStream",
@@ -84,7 +84,7 @@
84
84
  "@chat-adapter/state-memory": "4.42.0",
85
85
  "@chat-adapter/telegram": "4.41.0",
86
86
  "@chat-adapter/whatsapp": "4.42.0",
87
- "@segmentstream/ontology": "0.150.0",
87
+ "@segmentstream/ontology": "0.150.2",
88
88
  "@vercel/oidc": "^3.8.4",
89
89
  "ai": "^7.0.172",
90
90
  "chat": "4.42.0",