@segmentstream/ontology 0.163.0 → 0.165.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 (36) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/dist/agent-chat.d.ts +10 -0
  3. package/dist/agent-draft.d.ts +18 -6
  4. package/dist/agent-draft.js +29 -0
  5. package/dist/chat.d.ts +18 -0
  6. package/dist/chat.js +10 -0
  7. package/dist/email.d.ts +1 -1
  8. package/dist/email.js +1 -1
  9. package/dist/gmail-send.d.ts +10 -0
  10. package/dist/gmail-send.js +31 -9
  11. package/dist/manifest.d.ts +469 -469
  12. package/dist/migrate/model.d.ts +351 -351
  13. package/dist/service/chat/delivery.js +79 -3
  14. package/dist/service/chat/draft-files.d.ts +7 -0
  15. package/dist/service/chat/draft-files.js +33 -4
  16. package/dist/service/chat/envelope.d.ts +6 -6
  17. package/dist/service/chat/local-reads.d.ts +1 -1
  18. package/dist/service/chat/providers/custom.js +2 -2
  19. package/dist/service/chat/providers/email.js +13 -11
  20. package/dist/service/chat/providers/files-send.d.ts +36 -0
  21. package/dist/service/chat/providers/files-send.js +182 -0
  22. package/dist/service/chat/providers/provider.d.ts +22 -3
  23. package/dist/service/chat/providers/provider.js +43 -0
  24. package/dist/service/chat/providers/slack.js +4 -1
  25. package/dist/service/chat/providers/telegram.js +23 -3
  26. package/dist/service/chat/providers/whatsapp.js +4 -1
  27. package/dist/service/chat/review-delivery.d.ts +4 -2
  28. package/dist/service/chat/review-delivery.js +22 -2
  29. package/dist/service/chat/schemas.d.ts +5 -0
  30. package/dist/service/discovery-search.d.ts +1 -1
  31. package/dist/slack-files.d.ts +2 -0
  32. package/dist/slack-files.js +1 -1
  33. package/dist/telegram-files.d.ts +4 -0
  34. package/dist/telegram-files.js +6 -2
  35. package/dist/user-identity.d.ts +1 -1
  36. package/package.json +1 -1
package/CHANGELOG.md CHANGED
@@ -1,5 +1,33 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.165.0
4
+
5
+ Staff choose a draft's files and post files of their own. `agent_draft.approve` takes `files`, the
6
+ `{ entity, id, property, revisionId }` of the draft's files to send, leaving the others out;
7
+ naming a file the draft does not hold is refused with `file_not_held`, and the draft keeps the
8
+ files it sent. `agent_chat.post_message` takes `files`, one to ten `{ entity, id, property? }`,
9
+ read as the caller and sent with the text in one delivery, refused before the key is reserved
10
+ when the caller may not read one or the channel cannot send it. In `@segmentstream/app/agent`, a
11
+ draft of `useInboxChat` lists its `files`, `approve` takes the files to send as its fourth
12
+ argument, and `send` takes the record files to post with the text.
13
+
14
+ ## 0.164.0
15
+
16
+ Approving a draft with files now sends them on Telegram, Slack and WhatsApp too, with the shared
17
+ senders of `@segmentstream/ontology/<channel>`. Telegram sends one message when the text fits a
18
+ caption and the files go as one file or one album of one type, otherwise the text and then the
19
+ files; Slack uploads every file and shares them with the text as one message; WhatsApp sends each
20
+ file by a link granted just before its message, the text as the first's caption. Each channel
21
+ declares its file limits and refuses what it cannot send before anything is reserved. A reply that
22
+ reached the person only in part is `uncertain` and never sent again, and its delivery keeps what
23
+ became of each file (`ProviderOutcome.files`). A Telegram caption may now be written in MarkdownV2
24
+ (`parseMode`), with `plainCaption` sent when Telegram refuses its formatting, and `sendSlackFiles`
25
+ takes `requireAll` to share nothing unless every file uploaded.
26
+
27
+ `sendGmailMessage` throws `GmailMaybeSent` when an upload fails after its last chunk went to
28
+ Gmail, which may have sent the message, and an approved email reply that fails so is `uncertain`,
29
+ never `failed`.
30
+
3
31
  ## 0.163.0
4
32
 
5
33
  The ontology of an app preview can serve the workspace's agent, built from the preview's commit,
@@ -39,6 +39,11 @@ export declare const AgentChat: import("./entity.js").Entity<"agent_chat", {
39
39
  readonly chat: Schema.String;
40
40
  readonly text: Schema.String;
41
41
  readonly replyTo: Schema.optionalKey<Schema.String>;
42
+ readonly files: Schema.optionalKey<Schema.$Array<Schema.Struct<{
43
+ readonly entity: Schema.String;
44
+ readonly id: Schema.String;
45
+ readonly property: Schema.optionalKey<Schema.String>;
46
+ }>>>;
42
47
  readonly idempotencyKey: Schema.String;
43
48
  }>, import("./chat.js").ChatPreview>;
44
49
  readonly importHistory: import("./action.js").Action<Schema.Struct<{
@@ -109,6 +114,11 @@ export declare const AgentChat: import("./entity.js").Entity<"agent_chat", {
109
114
  readonly chat: Schema.String;
110
115
  readonly text: Schema.String;
111
116
  readonly replyTo: Schema.optionalKey<Schema.String>;
117
+ readonly files: Schema.optionalKey<Schema.$Array<Schema.Struct<{
118
+ readonly entity: Schema.String;
119
+ readonly id: Schema.String;
120
+ readonly property: Schema.optionalKey<Schema.String>;
121
+ }>>>;
112
122
  readonly idempotencyKey: Schema.String;
113
123
  }>, {
114
124
  readonly status: "sent" | "uncertain";
@@ -98,7 +98,7 @@ export declare const AgentDraft: import("./entity.js").Entity<"agent_draft", {
98
98
  readonly draft: import("./property.js").Property<string, false, false>;
99
99
  readonly files: Schema.optionalKey<import("./property.js").Property<Schema.Json, false, false>>;
100
100
  readonly text: Schema.optionalKey<import("./property.js").Property<string, false, false>>;
101
- readonly status: import("./property.js").Property<"pending" | "sent" | "uncertain" | "failed" | "rejected" | "superseded" | "dismissed", false, false>;
101
+ readonly status: import("./property.js").Property<"pending" | "sent" | "failed" | "uncertain" | "rejected" | "superseded" | "dismissed", false, false>;
102
102
  readonly mode: import("./property.js").Property<"always" | "whenUnsure", false, false>;
103
103
  readonly confidence: Schema.optionalKey<import("./property.js").Property<number, false, false>>;
104
104
  readonly messageId: Schema.optionalKey<import("./property.js").Property<string, false, false>>;
@@ -119,13 +119,19 @@ export declare const AgentDraft: import("./entity.js").Entity<"agent_draft", {
119
119
  readonly text: Schema.optionalKey<import("./property.js").Property<string, false, false>>;
120
120
  readonly to: Schema.optionalKey<Schema.$Array<Schema.String>>;
121
121
  readonly cc: Schema.optionalKey<Schema.$Array<Schema.String>>;
122
+ readonly files: Schema.optionalKey<Schema.$Array<Schema.Struct<{
123
+ readonly entity: Schema.String;
124
+ readonly id: Schema.String;
125
+ readonly property: Schema.String;
126
+ readonly revisionId: Schema.String;
127
+ }>>>;
122
128
  readonly unlessNewer: Schema.optionalKey<Schema.Boolean>;
123
129
  }>, {
124
130
  to?: readonly string[] | undefined;
125
131
  cc?: readonly string[] | undefined;
126
132
  draft: string;
127
133
  review: string;
128
- status: "sent" | "uncertain" | "failed";
134
+ status: "sent" | "failed" | "uncertain";
129
135
  message_id: string | null;
130
136
  duplicate: boolean;
131
137
  }>;
@@ -182,7 +188,7 @@ export declare const AgentDraft: import("./entity.js").Entity<"agent_draft", {
182
188
  readonly draft: string;
183
189
  readonly files: Schema.Json;
184
190
  readonly text: string | null;
185
- readonly status: "pending" | "sent" | "uncertain" | "failed" | "rejected" | "superseded" | "dismissed";
191
+ readonly status: "pending" | "sent" | "failed" | "uncertain" | "rejected" | "superseded" | "dismissed";
186
192
  readonly mode: "always" | "whenUnsure";
187
193
  readonly confidence: number | null;
188
194
  readonly messageId: string | null;
@@ -218,7 +224,7 @@ export declare const AgentReview: import("./entity.js").Entity<"agent_draft", {
218
224
  readonly draft: import("./property.js").Property<string, false, false>;
219
225
  readonly files: Schema.optionalKey<import("./property.js").Property<Schema.Json, false, false>>;
220
226
  readonly text: Schema.optionalKey<import("./property.js").Property<string, false, false>>;
221
- readonly status: import("./property.js").Property<"pending" | "sent" | "uncertain" | "failed" | "rejected" | "superseded" | "dismissed", false, false>;
227
+ readonly status: import("./property.js").Property<"pending" | "sent" | "failed" | "uncertain" | "rejected" | "superseded" | "dismissed", false, false>;
222
228
  readonly mode: import("./property.js").Property<"always" | "whenUnsure", false, false>;
223
229
  readonly confidence: Schema.optionalKey<import("./property.js").Property<number, false, false>>;
224
230
  readonly messageId: Schema.optionalKey<import("./property.js").Property<string, false, false>>;
@@ -239,13 +245,19 @@ export declare const AgentReview: import("./entity.js").Entity<"agent_draft", {
239
245
  readonly text: Schema.optionalKey<import("./property.js").Property<string, false, false>>;
240
246
  readonly to: Schema.optionalKey<Schema.$Array<Schema.String>>;
241
247
  readonly cc: Schema.optionalKey<Schema.$Array<Schema.String>>;
248
+ readonly files: Schema.optionalKey<Schema.$Array<Schema.Struct<{
249
+ readonly entity: Schema.String;
250
+ readonly id: Schema.String;
251
+ readonly property: Schema.String;
252
+ readonly revisionId: Schema.String;
253
+ }>>>;
242
254
  readonly unlessNewer: Schema.optionalKey<Schema.Boolean>;
243
255
  }>, {
244
256
  to?: readonly string[] | undefined;
245
257
  cc?: readonly string[] | undefined;
246
258
  draft: string;
247
259
  review: string;
248
- status: "sent" | "uncertain" | "failed";
260
+ status: "sent" | "failed" | "uncertain";
249
261
  message_id: string | null;
250
262
  duplicate: boolean;
251
263
  }>;
@@ -302,7 +314,7 @@ export declare const AgentReview: import("./entity.js").Entity<"agent_draft", {
302
314
  readonly draft: string;
303
315
  readonly files: Schema.Json;
304
316
  readonly text: string | null;
305
- readonly status: "pending" | "sent" | "uncertain" | "failed" | "rejected" | "superseded" | "dismissed";
317
+ readonly status: "pending" | "sent" | "failed" | "uncertain" | "rejected" | "superseded" | "dismissed";
306
318
  readonly mode: "always" | "whenUnsure";
307
319
  readonly confidence: number | null;
308
320
  readonly messageId: string | null;
@@ -393,6 +393,18 @@ export const AgentDraft = entity("agent_draft", {
393
393
  description: "A reply to mail: the addresses it copies, in place of the draft's cc; [] copies " +
394
394
  "no one, and omitting it keeps them.",
395
395
  })),
396
+ files: Schema.optionalKey(Schema.Array(Schema.Struct({
397
+ entity: Schema.String,
398
+ id: Schema.String,
399
+ property: Schema.String,
400
+ revisionId: Schema.String,
401
+ }))
402
+ .check(Schema.isMaxLength(AGENT_DRAFT_FILES_LIMIT))
403
+ .annotate({
404
+ description: "The draft's files to send, each { entity, id, property, revisionId } as the draft " +
405
+ "lists it, in place of all of them: an approval may leave files out, never add one. " +
406
+ "Omit it to send every file; [] sends none.",
407
+ })),
396
408
  unlessNewer: Schema.optionalKey(Schema.Boolean.annotate({
397
409
  description: "true refuses the approval, with refusal newer_message and nothing sent, when the " +
398
410
  "person wrote in the chat after the turn that wrote the reply, or basis_unknown when " +
@@ -415,6 +427,7 @@ export const AgentDraft = entity("agent_draft", {
415
427
  ...(input.to === undefined ? {} : { to: input.to }),
416
428
  ...(input.cc === undefined ? {} : { cc: input.cc }),
417
429
  ...(input.unlessNewer === true ? { unlessNewer: true } : {}),
430
+ ...(input.files === undefined ? {} : { files: input.files }),
418
431
  });
419
432
  }),
420
433
  handler: ({ input, prepared, principal }) => Effect.gen(function* () {
@@ -443,6 +456,22 @@ export const AgentDraft = entity("agent_draft", {
443
456
  to: draftAddresses(recipients.to),
444
457
  cc: recipients.cc.length === 0 ? null : draftAddresses(recipients.cc),
445
458
  }),
459
+ // The draft keeps the files sent, as it keeps the text approved.
460
+ ...(prepared.files === undefined
461
+ ? {}
462
+ : {
463
+ files: prepared.files.length === 0
464
+ ? null
465
+ : prepared.files.map(({ entity, id, property, revisionId, filename, contentType, size }) => ({
466
+ entity,
467
+ id,
468
+ property,
469
+ revisionId,
470
+ filename,
471
+ contentType,
472
+ size,
473
+ })),
474
+ }),
446
475
  });
447
476
  return {
448
477
  draft: review.id,
package/dist/chat.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import type { AgentDraftFile } from "./agent-draft.js";
1
2
  /** Workspace-authored exact-post attribution; neutral defaults are name and a blank line. */
2
3
  export interface ChatAttributionSettings {
3
4
  readonly template?: string;
@@ -39,6 +40,11 @@ export declare const ChatPostInput: Schema.Struct<{
39
40
  readonly chat: Schema.String;
40
41
  readonly text: Schema.String;
41
42
  readonly replyTo: Schema.optionalKey<Schema.String>;
43
+ readonly files: Schema.optionalKey<Schema.$Array<Schema.Struct<{
44
+ readonly entity: Schema.String;
45
+ readonly id: Schema.String;
46
+ readonly property: Schema.optionalKey<Schema.String>;
47
+ }>>>;
42
48
  readonly idempotencyKey: Schema.String;
43
49
  }>;
44
50
  export type ChatPostInput = typeof ChatPostInput.Type;
@@ -215,6 +221,16 @@ export interface ReviewSendInput {
215
221
  * after the turn that wrote the reply, or when that cannot be told.
216
222
  */
217
223
  readonly unlessNewer?: boolean;
224
+ /**
225
+ * The draft's files to send, each by its record and revision, in place of all it holds: it can
226
+ * only leave files out, never add one. `[]` sends none of them.
227
+ */
228
+ readonly files?: ReadonlyArray<{
229
+ readonly entity: string;
230
+ readonly id: string;
231
+ readonly property: string;
232
+ readonly revisionId: string;
233
+ }>;
218
234
  }
219
235
  /** Whether the person wrote after a draft's turn; `null` when it cannot be told. */
220
236
  export interface DraftNewer {
@@ -228,6 +244,8 @@ export interface ReviewSendResult {
228
244
  readonly text: string;
229
245
  readonly messageId: string | null;
230
246
  readonly duplicate: boolean;
247
+ /** The files sent with the text, or attempted; absent for a reply of text alone. */
248
+ readonly files?: ReadonlyArray<AgentDraftFile>;
231
249
  /** The reply's message in the workspace chat store, by its id; absent before the store held it. */
232
250
  readonly message?: string | null;
233
251
  /** A reply to mail: whom it was sent to, or attempted; absent on another channel. */
package/dist/chat.js CHANGED
@@ -9,6 +9,16 @@ export const ChatPostInput = Schema.Struct({
9
9
  description: "Markdown source; the platform adds sender attribution after channel rendering.",
10
10
  }),
11
11
  replyTo: Schema.optionalKey(Identifier),
12
+ files: Schema.optionalKey(Schema.Array(Schema.Struct({
13
+ entity: Identifier,
14
+ id: Identifier,
15
+ property: Schema.optionalKey(Identifier),
16
+ }))
17
+ .check(Schema.isMinLength(1), Schema.isMaxLength(10))
18
+ .annotate({
19
+ description: "Stored files the post carries after its text, each a record and its file property, " +
20
+ "which the caller may read: content by default. Each is sent at the revision read now.",
21
+ })),
12
22
  idempotencyKey: Identifier,
13
23
  });
14
24
  /** The file of a template's image, video or document header, where it is stored or served. */
package/dist/email.d.ts CHANGED
@@ -78,4 +78,4 @@ export declare function emailReplyMessage(reply: EmailReply): string;
78
78
  /** Bytes as URL-safe base64 without padding, as the Gmail API takes a raw message. */
79
79
  export declare function base64Url(text: string): string;
80
80
  export type { ChannelCall, ChannelReply, ChannelRequest } from "./channel-files.js";
81
- export { EMAIL_FILES_BYTES, GmailError, gmailExplanation, gmailJson, sendGmailMessage, } from "./gmail-send.js";
81
+ export { EMAIL_FILES_BYTES, GmailError, gmailExplanation, GmailMaybeSent, gmailJson, sendGmailMessage, } from "./gmail-send.js";
package/dist/email.js CHANGED
@@ -210,4 +210,4 @@ export function emailReplyMessage(reply) {
210
210
  export function base64Url(text) {
211
211
  return Buffer.from(text, "utf8").toString("base64url");
212
212
  }
213
- export { EMAIL_FILES_BYTES, GmailError, gmailExplanation, gmailJson, sendGmailMessage, } from "./gmail-send.js";
213
+ export { EMAIL_FILES_BYTES, GmailError, gmailExplanation, GmailMaybeSent, gmailJson, sendGmailMessage, } from "./gmail-send.js";
@@ -21,6 +21,16 @@ export declare class GmailError extends Error {
21
21
  readonly name = "GmailError";
22
22
  constructor(operation: string, status: number, detail?: string | undefined);
23
23
  }
24
+ /**
25
+ * A failure after the message's last byte went to Gmail: the upload may have completed and the
26
+ * message been sent, whatever the failure says, so it is never retried as unsent. `cause` is what
27
+ * failed, such as the probe of what Gmail held.
28
+ */
29
+ export declare class GmailMaybeSent extends Error {
30
+ readonly cause: unknown;
31
+ readonly name = "GmailMaybeSent";
32
+ constructor(cause: unknown);
33
+ }
24
34
  /** A reply's body as a JSON object, or an empty one. */
25
35
  export declare function gmailJson(reply: Pick<ChannelReply, "body">): Record<string, unknown>;
26
36
  /** Google's explanation of a refused call: its error message and reasons. */
@@ -29,6 +29,19 @@ export class GmailError extends Error {
29
29
  this.detail = detail;
30
30
  }
31
31
  }
32
+ /**
33
+ * A failure after the message's last byte went to Gmail: the upload may have completed and the
34
+ * message been sent, whatever the failure says, so it is never retried as unsent. `cause` is what
35
+ * failed, such as the probe of what Gmail held.
36
+ */
37
+ export class GmailMaybeSent extends Error {
38
+ cause;
39
+ name = "GmailMaybeSent";
40
+ constructor(cause) {
41
+ super("Gmail may have sent the message: its upload's last answer was lost.", { cause });
42
+ this.cause = cause;
43
+ }
44
+ }
32
45
  /** A reply's body as a JSON object, or an empty one. */
33
46
  export function gmailJson(reply) {
34
47
  try {
@@ -134,9 +147,13 @@ export async function sendGmailMessage(options) {
134
147
  const total = message.byteLength;
135
148
  let offset = 0;
136
149
  let recoveries = 0;
150
+ // Once the last byte was offered, Gmail may hold the whole message and have sent it.
151
+ let whole = false;
152
+ const maybeSent = (error) => (whole ? new GmailMaybeSent(error) : error);
137
153
  for (;;) {
138
154
  const end = Math.min(offset + UPLOAD_CHUNK_BYTES, total);
139
155
  let reply;
156
+ whole ||= end === total;
140
157
  try {
141
158
  reply = await request({
142
159
  api: session.api,
@@ -156,21 +173,26 @@ export async function sendGmailMessage(options) {
156
173
  // The chunk may or may not have arrived: Gmail says how much it holds.
157
174
  recoveries += 1;
158
175
  if (recoveries > UPLOAD_RECOVERIES) {
159
- throw error;
176
+ throw maybeSent(error);
177
+ }
178
+ try {
179
+ reply = await request({
180
+ api: session.api,
181
+ method: "PUT",
182
+ path: session.path,
183
+ headers: [["content-range", `bytes */${total}`]],
184
+ body: new Uint8Array(),
185
+ });
186
+ }
187
+ catch (probe) {
188
+ throw maybeSent(probe);
160
189
  }
161
- reply = await request({
162
- api: session.api,
163
- method: "PUT",
164
- path: session.path,
165
- headers: [["content-range", `bytes */${total}`]],
166
- body: new Uint8Array(),
167
- });
168
190
  }
169
191
  if (reply.status === 200 || reply.status === 201) {
170
192
  return gmailJson(reply);
171
193
  }
172
194
  if (reply.status !== 308) {
173
- throw new GmailError("messages.send", reply.status, gmailExplanation(reply));
195
+ throw maybeSent(new GmailError("messages.send", reply.status, gmailExplanation(reply)));
174
196
  }
175
197
  // `Range: bytes=0-N` is what Gmail holds; without it, nothing yet.
176
198
  const held = /^bytes=0-(\d+)$/u.exec(replyHeader(reply, "range") ?? "")?.[1];