@segmentstream/agent 0.73.2 → 0.75.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 (47) hide show
  1. package/CHANGELOG.md +48 -0
  2. package/dist/_chunks/{build-metadata-3mxf7v2b.mjs → build-metadata-CLenocka.mjs} +1 -1
  3. package/dist/_chunks/{chat-archive-BCPLSZeK.mjs → chat-archive-CDk-FPLX.mjs} +11 -4
  4. package/dist/_chunks/{chat-archive-DK7uCgff.mjs → chat-archive-DHYplDT5.mjs} +1 -1
  5. package/dist/extension/config.d.ts +12 -0
  6. package/dist/extension/config.mjs +21 -2
  7. package/dist/extension/extension.d.ts +1 -1
  8. package/dist/extension/hooks/chat-context.mjs +1 -1
  9. package/dist/extension/hooks/chat-recorder.mjs +1 -1
  10. package/dist/extension/lib/build-metadata.mjs +1 -1
  11. package/dist/extension/lib/chat-archive.d.ts +6 -1
  12. package/dist/extension/lib/chat-archive.mjs +1 -1
  13. package/dist/extension/lib/chat-context.mjs +1 -1
  14. package/dist/extension/lib/chat-messages.d.ts +108 -0
  15. package/dist/extension/lib/chat-messages.mjs +169 -4
  16. package/dist/extension/lib/chat-recorder.mjs +44 -13
  17. package/dist/extension/lib/chat-store.d.ts +23 -1
  18. package/dist/extension/lib/email/channel.d.ts +3 -0
  19. package/dist/extension/lib/email/channel.mjs +60 -14
  20. package/dist/extension/lib/group-routing/routing-context.mjs +1 -1
  21. package/dist/extension/lib/model-service-alert.mjs +1 -1
  22. package/dist/extension/lib/review/judge.mjs +1 -1
  23. package/dist/extension/lib/review/reply.mjs +1 -1
  24. package/dist/extension/lib/review/revision-channel.mjs +1 -1
  25. package/dist/extension/lib/slack/adapter.mjs +1 -1
  26. package/dist/extension/lib/telegram/adapter.mjs +3 -3
  27. package/dist/extension/lib/telegram/chat-archive.mjs +1 -1
  28. package/dist/extension/lib/telegram/confirmations.mjs +1 -1
  29. package/dist/extension/lib/telegram/delivery-diagnostics.mjs +1 -1
  30. package/dist/extension/lib/telegram/health.mjs +1 -1
  31. package/dist/extension/lib/telegram/inbound.mjs +1 -1
  32. package/dist/extension/lib/telegram/lifecycle.mjs +1 -1
  33. package/dist/extension/lib/telegram/store-capture.mjs +1 -1
  34. package/dist/extension/lib/telegram/turn-errors.mjs +1 -1
  35. package/dist/extension/lib/whatsapp/adapter.d.ts +21 -1
  36. package/dist/extension/lib/whatsapp/adapter.mjs +82 -13
  37. package/dist/extension/lib/whatsapp/attachments-send.mjs +1 -1
  38. package/dist/extension/lib/whatsapp/changes.d.ts +46 -0
  39. package/dist/extension/lib/whatsapp/changes.mjs +212 -0
  40. package/dist/extension/lib/whatsapp/sent.mjs +1 -1
  41. package/dist/extension/lib/whatsapp/statuses.d.ts +9 -0
  42. package/dist/extension/lib/whatsapp/statuses.mjs +9 -0
  43. package/dist/extension/lib/workspace-channel/channel.d.ts +68 -7
  44. package/dist/extension/lib/workspace-channel/channel.mjs +149 -33
  45. package/dist/extension/lib/workspace-chat-archive.d.ts +7 -0
  46. package/dist/extension/lib/workspace-chat-archive.mjs +1 -1
  47. package/package.json +2 -2
@@ -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, r as chatArchive, t as AGENT_SENDER, x as reasonCode } from "../../../_chunks/chat-archive-CDk-FPLX.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
@@ -38,4 +38,11 @@ export declare function sendFingerprint(input: {
38
38
  }): string;
39
39
  /** A row of the store as the port's event, with what the agent did with it. */
40
40
  export declare function eventOf(row: StoredMessage): ChatPage["events"][number];
41
+ /**
42
+ * A person's message nothing proved the sender of, such as mail that failed DMARC: the key of
43
+ * `meta` that marks it from its record on, and the reason it is `skipped` with. Staff read it in
44
+ * the inbox; the agent's own reads of a chat never return it, so it reaches no session, nor a
45
+ * reply's review, as the person's words.
46
+ */
47
+ export declare const UNVERIFIED = "unverified";
41
48
  export declare function workspaceChatArchive(store: ChatStore): ChatArchive;
@@ -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 UNVERIFIED, g as boundedMeta, _ as cutText, v as eventOf, y as messageInput, b as metaOf, x as reasonCode, S as sendFingerprint, C as storedFiles, w as storedMessageId, T as workspaceChatArchive } from "../../_chunks/chat-archive-CDk-FPLX.mjs";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@segmentstream/agent",
3
- "version": "0.73.2",
3
+ "version": "0.75.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.1",
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",