@segmentstream/ontology 0.182.0 → 0.184.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 +62 -0
- package/README.md +23 -15
- package/dist/action.d.ts +11 -4
- package/dist/agent-chat.d.ts +6 -0
- package/dist/agent-chat.js +43 -28
- package/dist/analyze.d.ts +6 -1
- package/dist/analyze.js +51 -19
- package/dist/authoring.js +7 -1
- package/dist/channel-files.d.ts +17 -0
- package/dist/chat.d.ts +32 -3
- package/dist/chat.js +33 -12
- package/dist/email.d.ts +3 -7
- package/dist/manifest.d.ts +64 -0
- package/dist/manifest.js +9 -0
- package/dist/migrate/model.d.ts +16 -0
- package/dist/permissions.d.ts +17 -0
- package/dist/permissions.js +17 -0
- package/dist/render.d.ts +6 -0
- package/dist/render.js +25 -10
- package/dist/runtime/pipeline.d.ts +3 -3
- package/dist/runtime/pipeline.js +11 -4
- package/dist/service/catalog.js +3 -3
- package/dist/service/chat/chat-draft.d.ts +1 -1
- package/dist/service/chat/delivery.js +43 -22
- package/dist/service/chat/providers/email.js +13 -37
- package/dist/service/chat/providers/files-send.d.ts +2 -0
- package/dist/service/chat/providers/files-send.js +2 -0
- package/dist/service/chat/providers/provider.d.ts +15 -1
- package/dist/service/chat/providers/provider.js +53 -0
- package/dist/service/chat/providers/telegram.d.ts +21 -8
- package/dist/service/chat/providers/telegram.js +220 -48
- package/dist/service/chat/routes.js +2 -0
- package/dist/service/chat/schemas.d.ts +40 -0
- package/dist/service/chat/schemas.js +3 -1
- package/dist/service/mcp/server.js +2 -0
- package/dist/service/mcp/tools.d.ts +10 -2
- package/dist/service/mcp/tools.js +11 -4
- package/dist/service/operations.js +8 -4
- package/dist/service/plan.d.ts +3 -1
- package/dist/service/plan.js +4 -2
- package/dist/service/schemas.d.ts +4 -0
- package/dist/service/service.js +2 -2
- package/dist/telegram.d.ts +84 -1
- package/dist/telegram.js +14 -0
- package/package.json +1 -1
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { isTelegramBotUsername, miniAppLink, telegramMarkdownLinks, telegramMarkdownWithLinks, telegramTextLinks, telegramPreviewOptions, } from "../../../telegram.js";
|
|
1
|
+
import { isTelegramBotUsername, miniAppLink, telegramDeliverRoute, telegramMarkdownLinks, telegramMarkdownWithLinks, telegramTextLinks, telegramPreviewOptions, TELEGRAM_MESSAGE_LIMIT, } from "../../../telegram.js";
|
|
2
2
|
import { escapeMarkdownV2, TelegramFormatConverter } from "@chat-adapter/telegram";
|
|
3
|
-
import { Effect } from "effect";
|
|
3
|
+
import { Cause, Effect } from "effect";
|
|
4
4
|
import { refuseTelegramFiles, sendTelegramWithFiles, TELEGRAM_FILE_LIMITS } from "./files-send.js";
|
|
5
|
-
import { record, transportFailure, } from "./provider.js";
|
|
6
|
-
export
|
|
5
|
+
import { agentFileLinks, postToAgent, record, transportFailure, } from "./provider.js";
|
|
6
|
+
export { TELEGRAM_MESSAGE_LIMIT };
|
|
7
7
|
const converter = new TelegramFormatConverter();
|
|
8
8
|
/**
|
|
9
9
|
* Render source independently so user Markdown cannot open an entity around the footer. Without an
|
|
@@ -88,21 +88,42 @@ export function telegramKeyboard(rows, links = new Map()) {
|
|
|
88
88
|
: { text: button.text, callback_data: `ws:${button.token}` })),
|
|
89
89
|
};
|
|
90
90
|
}
|
|
91
|
+
/**
|
|
92
|
+
* One text's `sendMessage` fields as Telegram is sent them: the MarkdownV2 text and, for the one
|
|
93
|
+
* retry a refusal of its formatting allows, the plain text, each with the link preview its own
|
|
94
|
+
* links choose.
|
|
95
|
+
*/
|
|
96
|
+
export function telegramPart(input) {
|
|
97
|
+
const caller = {
|
|
98
|
+
link_preview_options: input.link_preview_options,
|
|
99
|
+
disable_web_page_preview: input.disable_web_page_preview,
|
|
100
|
+
};
|
|
101
|
+
const preview = (links) => telegramPreviewOptions(caller, links, input.previews);
|
|
102
|
+
return {
|
|
103
|
+
formatted: {
|
|
104
|
+
text: input.finalText,
|
|
105
|
+
parse_mode: "MarkdownV2",
|
|
106
|
+
...preview(input.formattedLinks ?? telegramMarkdownLinks(input.plainText)),
|
|
107
|
+
},
|
|
108
|
+
plain: { text: input.plainText, ...preview(telegramTextLinks(input.plainText)) },
|
|
109
|
+
};
|
|
110
|
+
}
|
|
91
111
|
/** One proxy POST, except one plain retry after a definite 400 entity parse refusal.
|
|
92
112
|
* Never retry an unknown outcome; never chunk, truncate or invoke an agent.
|
|
113
|
+
* @legacy ontology.telegram-own-sender The ontology's own send of a text, which the agent's
|
|
114
|
+
* Telegram channel's deliver route does in its place.
|
|
93
115
|
*/
|
|
94
116
|
export function sendTelegram(input) {
|
|
117
|
+
return sendTelegramPart(input, telegramPart(input), input.replyMarkup);
|
|
118
|
+
}
|
|
119
|
+
function sendTelegramPart(target, part, replyMarkup) {
|
|
95
120
|
return Effect.gen(function* () {
|
|
96
|
-
const markup =
|
|
97
|
-
const formatted = yield* attemptTelegram(
|
|
98
|
-
text: input.finalText,
|
|
99
|
-
parse_mode: "MarkdownV2",
|
|
100
|
-
...markup,
|
|
101
|
-
});
|
|
121
|
+
const markup = replyMarkup === undefined ? {} : { reply_markup: replyMarkup };
|
|
122
|
+
const formatted = yield* attemptTelegram(target, part.formatted, markup);
|
|
102
123
|
if (!formatted.formattingRefused) {
|
|
103
124
|
return formatted;
|
|
104
125
|
}
|
|
105
|
-
const plain = yield* attemptTelegram(
|
|
126
|
+
const plain = yield* attemptTelegram(target, part.plain, markup);
|
|
106
127
|
return {
|
|
107
128
|
...plain,
|
|
108
129
|
cause: plain.cause === undefined
|
|
@@ -111,8 +132,14 @@ export function sendTelegram(input) {
|
|
|
111
132
|
};
|
|
112
133
|
});
|
|
113
134
|
}
|
|
114
|
-
function attemptTelegram(input,
|
|
135
|
+
function attemptTelegram(input, text, markup) {
|
|
115
136
|
return Effect.gen(function* () {
|
|
137
|
+
// What the delivery's history keeps: the text and its format, with its buttons.
|
|
138
|
+
const payload = {
|
|
139
|
+
text: text.text,
|
|
140
|
+
...(text.parse_mode === undefined ? {} : { parse_mode: text.parse_mode }),
|
|
141
|
+
...markup,
|
|
142
|
+
};
|
|
116
143
|
const result = yield* input.transport
|
|
117
144
|
.request({
|
|
118
145
|
connection: input.connection,
|
|
@@ -127,13 +154,8 @@ function attemptTelegram(input, payload) {
|
|
|
127
154
|
!/^[1-9][0-9]*$/.test(input.threadId)
|
|
128
155
|
? {}
|
|
129
156
|
: { message_thread_id: Number(input.threadId) }),
|
|
130
|
-
...
|
|
131
|
-
...
|
|
132
|
-
link_preview_options: input.link_preview_options,
|
|
133
|
-
disable_web_page_preview: input.disable_web_page_preview,
|
|
134
|
-
}, payload.parse_mode === "MarkdownV2"
|
|
135
|
-
? (input.formattedLinks ?? telegramMarkdownLinks(input.plainText))
|
|
136
|
-
: telegramTextLinks(payload.text), input.previews),
|
|
157
|
+
...text,
|
|
158
|
+
...markup,
|
|
137
159
|
...(input.replyTo === null
|
|
138
160
|
? {}
|
|
139
161
|
: {
|
|
@@ -202,7 +224,10 @@ function attemptTelegram(input, payload) {
|
|
|
202
224
|
return { payload, status: "sent", messageId, url };
|
|
203
225
|
});
|
|
204
226
|
}
|
|
205
|
-
/**
|
|
227
|
+
/**
|
|
228
|
+
* Telegram: the ontology renders the message, its MarkdownV2 with one plain retry after a definite
|
|
229
|
+
* entity parse refusal, and the agent's Telegram channel of the chat's name sends it.
|
|
230
|
+
*/
|
|
206
231
|
export const telegramProvider = {
|
|
207
232
|
provider: "telegram",
|
|
208
233
|
limit: TELEGRAM_MESSAGE_LIMIT,
|
|
@@ -221,30 +246,20 @@ export const telegramProvider = {
|
|
|
221
246
|
refuseButtons: () => undefined,
|
|
222
247
|
files: TELEGRAM_FILE_LIMITS,
|
|
223
248
|
refuseFiles: refuseTelegramFiles,
|
|
224
|
-
send:
|
|
249
|
+
send: sendTelegramMessage,
|
|
225
250
|
};
|
|
226
251
|
/**
|
|
227
|
-
*
|
|
228
|
-
*
|
|
252
|
+
* The member's Markdown as Telegram is sent it, with the chat's links to the app and previews: only
|
|
253
|
+
* where a link to the app leads changes, so the text reads as the member approved it. A text that
|
|
254
|
+
* goes as the caption of files is the same, so its links open in the bot's Mini App too.
|
|
229
255
|
*/
|
|
230
|
-
function
|
|
231
|
-
return Effect.gen(function* () {
|
|
232
|
-
const links = yield* miniAppLinksOf(input, telegramMarkdownLinks(input.text));
|
|
233
|
-
const finalText = links.size === 0
|
|
234
|
-
? input.finalText
|
|
235
|
-
: renderTelegram(input.text, input.attribution, input.separator, links);
|
|
236
|
-
return yield* sendTelegramWithFiles({ ...input, finalText }, sendTelegramText);
|
|
237
|
-
});
|
|
238
|
-
}
|
|
239
|
-
/** A text reply or post: the member's Markdown, with the chat's links to the app and previews. */
|
|
240
|
-
function sendTelegramText(input) {
|
|
256
|
+
function telegramMessage(input) {
|
|
241
257
|
return Effect.gen(function* () {
|
|
242
258
|
const written = [
|
|
243
259
|
...telegramMarkdownLinks(input.text),
|
|
244
260
|
...telegramTextLinks(input.separator),
|
|
245
261
|
...telegramTextLinks(input.attribution),
|
|
246
262
|
];
|
|
247
|
-
// Only where a link to the app leads changes: the text reads as the member approved it.
|
|
248
263
|
const buttonUrls = (input.buttons ?? []).flatMap((row) => row.flatMap((button) => ("url" in button ? [button.url] : [])));
|
|
249
264
|
const links = yield* miniAppLinksOf(input, [
|
|
250
265
|
...telegramMarkdownLinks(input.text),
|
|
@@ -256,22 +271,179 @@ function sendTelegramText(input) {
|
|
|
256
271
|
const preview = input.previews?.settings?.linkPreviews === "all" && first !== undefined && links.has(first)
|
|
257
272
|
? { link_preview_options: { url: first } }
|
|
258
273
|
: {};
|
|
259
|
-
return
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
:
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
plainText: input.plainText,
|
|
270
|
-
replyTo: input.replyTo,
|
|
271
|
-
...(input.threadId === undefined ? {} : { threadId: input.threadId }),
|
|
274
|
+
return {
|
|
275
|
+
part: telegramPart({
|
|
276
|
+
finalText: links.size === 0
|
|
277
|
+
? input.finalText
|
|
278
|
+
: renderTelegram(input.text, input.attribution, input.separator, links),
|
|
279
|
+
plainText: input.plainText,
|
|
280
|
+
formattedLinks: written,
|
|
281
|
+
...(input.previews === undefined ? {} : { previews: input.previews }),
|
|
282
|
+
...preview,
|
|
283
|
+
}),
|
|
272
284
|
...(input.buttons === undefined
|
|
273
285
|
? {}
|
|
274
286
|
: { replyMarkup: telegramKeyboard(input.buttons, links) }),
|
|
287
|
+
};
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* One message, rendered here and sent by the agent's Telegram channel; by the ontology itself for
|
|
292
|
+
* an agent without its deliver route.
|
|
293
|
+
*/
|
|
294
|
+
function sendTelegramMessage(input) {
|
|
295
|
+
return Effect.gen(function* () {
|
|
296
|
+
const message = yield* telegramMessage(input);
|
|
297
|
+
if (input.agent?.origin !== undefined && input.agent.identityToken !== undefined) {
|
|
298
|
+
const delivered = yield* deliverThroughAgent(input, message);
|
|
299
|
+
if (delivered !== undefined) {
|
|
300
|
+
return delivered;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
// @legacy ontology.telegram-own-sender The ontology's own Telegram sender, for an agent
|
|
304
|
+
// without the Telegram channel's deliver route or without a Telegram channel of the chat's
|
|
305
|
+
// name on its connection: the text, then the files' bytes, read and checked before the message
|
|
306
|
+
// was reserved, through the ontology's connection.
|
|
307
|
+
const text = () => sendTelegramPart(input, message.part, message.replyMarkup);
|
|
308
|
+
return yield* (input.files ?? []).length > 0
|
|
309
|
+
? sendTelegramWithFiles({ ...input, finalText: message.part.formatted.text }, text)
|
|
310
|
+
: text();
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* How long the agent's Telegram channel has to send one message: its texts, then its files, each
|
|
315
|
+
* through the connection.
|
|
316
|
+
*/
|
|
317
|
+
const AGENT_DELIVER_TIMEOUT_MS = 90_000;
|
|
318
|
+
/** A message id Telegram names. */
|
|
319
|
+
const MESSAGE_ID = /^[1-9][0-9]*$/;
|
|
320
|
+
/**
|
|
321
|
+
* One message through the agent's Telegram channel (`POST` {@link telegramDeliverRoute}), never
|
|
322
|
+
* retried: the channel sends the parts rendered here in order, through its bot, then the files by
|
|
323
|
+
* their links. Undefined when the agent has no such route (`404`) or no Telegram channel of the
|
|
324
|
+
* chat's name on this connection (`channel_unknown`), so the ontology sends it itself. The agent's
|
|
325
|
+
* other `4xx` sent nothing; a `2xx` says what reached the person; a timeout, a `5xx` or an answer
|
|
326
|
+
* without its shape may have sent it, and is uncertain.
|
|
327
|
+
*/
|
|
328
|
+
function deliverThroughAgent(input, message) {
|
|
329
|
+
return Effect.gen(function* () {
|
|
330
|
+
const files = input.files ?? [];
|
|
331
|
+
const markup = message.replyMarkup === undefined ? {} : { reply_markup: message.replyMarkup };
|
|
332
|
+
const payload = {
|
|
333
|
+
text: message.part.formatted.text,
|
|
334
|
+
parse_mode: "MarkdownV2",
|
|
335
|
+
...markup,
|
|
336
|
+
};
|
|
337
|
+
// What became of each file, as the history keeps it, by its name.
|
|
338
|
+
const outcome = (status, results, rest) => ({
|
|
339
|
+
payload: results === undefined
|
|
340
|
+
? payload
|
|
341
|
+
: {
|
|
342
|
+
...payload,
|
|
343
|
+
files: results.map((result) => ({
|
|
344
|
+
...result,
|
|
345
|
+
filename: files[result.index]?.filename ?? "",
|
|
346
|
+
})),
|
|
347
|
+
},
|
|
348
|
+
status,
|
|
349
|
+
...rest,
|
|
350
|
+
...(results === undefined ? {} : { files: results }),
|
|
275
351
|
});
|
|
352
|
+
const unsent = files.length === 0
|
|
353
|
+
? undefined
|
|
354
|
+
: files.map((_file, index) => ({ index, status: "failed", messageId: null }));
|
|
355
|
+
const links = yield* agentFileLinks(files).pipe(Effect.exit);
|
|
356
|
+
if (links._tag === "Failure") {
|
|
357
|
+
return outcome("failed", unsent, {
|
|
358
|
+
messageId: null,
|
|
359
|
+
url: null,
|
|
360
|
+
cause: Cause.squash(links.cause),
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
const thread = typeof input.threadId === "string" && /^[1-9][0-9]*$/u.test(input.threadId)
|
|
364
|
+
? input.threadId
|
|
365
|
+
: null;
|
|
366
|
+
const body = {
|
|
367
|
+
operation_id: input.operationId ?? input.destination.id,
|
|
368
|
+
connection: input.connection,
|
|
369
|
+
chat_id: input.destination.external_id,
|
|
370
|
+
thread_id: thread,
|
|
371
|
+
reply_to: input.replyTo,
|
|
372
|
+
// A message of files alone has no text to send, nor to caption them with.
|
|
373
|
+
parts: input.plainText.trim() === "" ? [] : [message.part],
|
|
374
|
+
...markup,
|
|
375
|
+
...(links.value.length === 0 ? {} : { files: links.value }),
|
|
376
|
+
};
|
|
377
|
+
const answered = yield* postToAgent(input.agent, telegramDeliverRoute(input.destination.provider), body, AGENT_DELIVER_TIMEOUT_MS).pipe(Effect.exit);
|
|
378
|
+
if (answered._tag === "Failure") {
|
|
379
|
+
return outcome("uncertain", undefined, {
|
|
380
|
+
messageId: null,
|
|
381
|
+
url: null,
|
|
382
|
+
cause: Cause.squash(answered.cause),
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
const { status, answer } = answered.value;
|
|
386
|
+
if (status === 404 || (status === 422 && answer.error === "channel_unknown")) {
|
|
387
|
+
return undefined;
|
|
388
|
+
}
|
|
389
|
+
if (status >= 400 && status < 500) {
|
|
390
|
+
return outcome("failed", unsent, {
|
|
391
|
+
messageId: null,
|
|
392
|
+
url: null,
|
|
393
|
+
cause: new Error(`The agent's Telegram channel refused the message (${typeof answer.error === "string" ? answer.error : `status ${status}`}${typeof answer.reference === "string" ? `, reference ${answer.reference}` : ""}).`),
|
|
394
|
+
...(answer.refusal === "recipient_unreachable"
|
|
395
|
+
? { refusal: "recipient_unreachable" }
|
|
396
|
+
: {}),
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
const results = deliveredFiles(answer.files, files.length);
|
|
400
|
+
const id = answer.message_id;
|
|
401
|
+
const url = typeof answer.url === "string" && answer.url.startsWith("https://t.me/") ? answer.url : null;
|
|
402
|
+
if (status < 200 ||
|
|
403
|
+
status >= 300 ||
|
|
404
|
+
(answer.status !== "sent" && answer.status !== "uncertain") ||
|
|
405
|
+
(id !== null && (typeof id !== "string" || !MESSAGE_ID.test(id))) ||
|
|
406
|
+
(answer.status === "sent" &&
|
|
407
|
+
(id === null || results?.some((result) => result.status !== "sent") === true)) ||
|
|
408
|
+
(files.length > 0 && results === undefined)) {
|
|
409
|
+
return outcome("uncertain", undefined, {
|
|
410
|
+
messageId: null,
|
|
411
|
+
url: null,
|
|
412
|
+
cause: new Error(`The agent's Telegram channel answered with status ${status}.`),
|
|
413
|
+
});
|
|
414
|
+
}
|
|
415
|
+
return answer.status === "sent"
|
|
416
|
+
? outcome("sent", results, { messageId: id, url })
|
|
417
|
+
: outcome("uncertain", results, {
|
|
418
|
+
messageId: id,
|
|
419
|
+
url,
|
|
420
|
+
cause: new Error("The agent's Telegram channel could not prove every message sent" +
|
|
421
|
+
`${typeof answer.reference === "string" ? ` (reference ${answer.reference})` : ""}.`),
|
|
422
|
+
});
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
/** The result of each file the agent's answer names, one per file in order, or undefined. */
|
|
426
|
+
function deliveredFiles(value, count) {
|
|
427
|
+
if (count === 0) {
|
|
428
|
+
return undefined;
|
|
429
|
+
}
|
|
430
|
+
if (!Array.isArray(value) || value.length !== count) {
|
|
431
|
+
return undefined;
|
|
432
|
+
}
|
|
433
|
+
const results = value.map((item, index) => {
|
|
434
|
+
const result = record(item);
|
|
435
|
+
const id = result.message_id;
|
|
436
|
+
return result.index === index &&
|
|
437
|
+
(result.status === "sent" || result.status === "uncertain" || result.status === "failed") &&
|
|
438
|
+
(id === null || (typeof id === "string" && MESSAGE_ID.test(id)))
|
|
439
|
+
? {
|
|
440
|
+
index,
|
|
441
|
+
status: result.status,
|
|
442
|
+
messageId: result.status === "sent" ? id : null,
|
|
443
|
+
}
|
|
444
|
+
: undefined;
|
|
276
445
|
});
|
|
446
|
+
return results.every((result) => result !== undefined)
|
|
447
|
+
? results
|
|
448
|
+
: undefined;
|
|
277
449
|
}
|
|
@@ -28,6 +28,8 @@ export function chatRoutes(options) {
|
|
|
28
28
|
});
|
|
29
29
|
const operations = makeOperations(backend, options.built.plan, options.hooks, options.connections, options.replicate, options.chatDelivery);
|
|
30
30
|
const planned = options.built.plan.entities.find((entity) => entity.name === "agent_chat");
|
|
31
|
+
// @legacy ontology.chat-post-message `chat/actions/post_message`, send as me by its earlier
|
|
32
|
+
// name: `agent_chat/actions/send` serves every message.
|
|
31
33
|
const action = planned.actions.find((candidate) => candidate.name === "post_message");
|
|
32
34
|
for (const method of ["list", "history", "search", "actions/post_message"]) {
|
|
33
35
|
yield* router.add("POST", `/api/ontology/chat/${method}`, (request) => Effect.gen(function* () {
|
|
@@ -1,4 +1,44 @@
|
|
|
1
1
|
import { Schema } from "effect";
|
|
2
|
+
export declare const ChatSendWire: Schema.encodeKeys<Schema.Struct<{
|
|
3
|
+
readonly chat: Schema.String;
|
|
4
|
+
readonly text: Schema.optionalKey<Schema.String>;
|
|
5
|
+
readonly template: Schema.optionalKey<Schema.Struct<{
|
|
6
|
+
readonly name: Schema.String;
|
|
7
|
+
readonly language: Schema.String;
|
|
8
|
+
readonly parameters: Schema.optionalKey<Schema.$Record<Schema.String, Schema.String>>;
|
|
9
|
+
readonly header: Schema.optionalKey<Schema.Struct<{
|
|
10
|
+
readonly parameters: Schema.optionalKey<Schema.$Record<Schema.String, Schema.String>>;
|
|
11
|
+
readonly media: Schema.optionalKey<Schema.Struct<{
|
|
12
|
+
readonly url: Schema.optionalKey<Schema.String>;
|
|
13
|
+
readonly file: Schema.optionalKey<Schema.Struct<{
|
|
14
|
+
readonly entity: Schema.String;
|
|
15
|
+
readonly id: Schema.String;
|
|
16
|
+
readonly property: Schema.optionalKey<Schema.String>;
|
|
17
|
+
}>>;
|
|
18
|
+
readonly filename: Schema.optionalKey<Schema.String>;
|
|
19
|
+
}>>;
|
|
20
|
+
}>>;
|
|
21
|
+
readonly buttons: Schema.optionalKey<Schema.$Record<Schema.String, Schema.String>>;
|
|
22
|
+
}>>;
|
|
23
|
+
readonly replyTo: Schema.optionalKey<Schema.String>;
|
|
24
|
+
readonly buttons: Schema.optionalKey<Schema.$Array<Schema.$Array<Schema.Union<readonly [Schema.Struct<{
|
|
25
|
+
readonly text: Schema.String;
|
|
26
|
+
readonly url: Schema.String;
|
|
27
|
+
}>, Schema.Struct<{
|
|
28
|
+
readonly text: Schema.String;
|
|
29
|
+
readonly token: Schema.String;
|
|
30
|
+
}>]>>>>;
|
|
31
|
+
readonly files: Schema.optionalKey<Schema.$Array<Schema.Struct<{
|
|
32
|
+
readonly entity: Schema.String;
|
|
33
|
+
readonly id: Schema.String;
|
|
34
|
+
readonly property: Schema.optionalKey<Schema.String>;
|
|
35
|
+
}>>>;
|
|
36
|
+
readonly as: Schema.optionalKey<Schema.Literals<readonly ["agent", "me"]>>;
|
|
37
|
+
readonly idempotencyKey: Schema.String;
|
|
38
|
+
}>, {
|
|
39
|
+
readonly replyTo: "reply_to";
|
|
40
|
+
readonly idempotencyKey: "idempotency_key";
|
|
41
|
+
}>;
|
|
2
42
|
export declare const ChatPostWire: Schema.encodeKeys<Schema.Struct<{
|
|
3
43
|
readonly chat: Schema.String;
|
|
4
44
|
readonly text: Schema.String;
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { Schema } from "effect";
|
|
2
|
-
import { ChatPostInput } from "../../chat.js";
|
|
2
|
+
import { ChatPostInput, ChatSendInput } from "../../chat.js";
|
|
3
3
|
import { CHAT_KINDS } from "../../chat-channel.js";
|
|
4
4
|
const Id = Schema.String.check(Schema.isMinLength(1), Schema.isMaxLength(512));
|
|
5
|
+
export const ChatSendWire = ChatSendInput.pipe(Schema.encodeKeys({ replyTo: "reply_to", idempotencyKey: "idempotency_key" }));
|
|
6
|
+
// @legacy ontology.chat-post-message The input of post_message, the earlier name of send as me.
|
|
5
7
|
export const ChatPostWire = ChatPostInput.pipe(Schema.encodeKeys({ replyTo: "reply_to", idempotencyKey: "idempotency_key" }));
|
|
6
8
|
export const RuntimeChatPostWire = Schema.Struct({
|
|
7
9
|
chat: ChatPostInput.fields.chat,
|
|
@@ -482,6 +482,8 @@ function runTool(server, tool, args, request, preview) {
|
|
|
482
482
|
};
|
|
483
483
|
}
|
|
484
484
|
case "action": {
|
|
485
|
+
// @legacy ontology.chat-post-message chat_post_message, send as me by its earlier name,
|
|
486
|
+
// answers the flat receipt, and is the tool through which the agent's sessions post.
|
|
485
487
|
if (target.planned.name === "agent_chat" && target.action.name === "post_message") {
|
|
486
488
|
yield* decodeArguments(caller.kind === "runtime" ? RuntimeChatPostWire : ChatPostWire, args);
|
|
487
489
|
const answer = yield* operations.runAction(target.planned, target.action, {
|
|
@@ -57,8 +57,13 @@ export interface McpTool {
|
|
|
57
57
|
/** The JSON Schema of the tool's `structuredContent`, for the tools that return a known shape. */
|
|
58
58
|
readonly outputSchema?: JsonSchemaObject;
|
|
59
59
|
readonly annotations: McpToolAnnotations;
|
|
60
|
-
/**
|
|
60
|
+
/**
|
|
61
|
+
* Every permission a caller needs to see and run the tool, or, with `anyPermission`, one of them;
|
|
62
|
+
* none for the global tools.
|
|
63
|
+
*/
|
|
61
64
|
readonly permissions: ReadonlyArray<string>;
|
|
65
|
+
/** Any one of `permissions` opens the tool: an action several permissions open. */
|
|
66
|
+
readonly anyPermission?: true;
|
|
62
67
|
/**
|
|
63
68
|
* For a tool whose input depends on what the caller may read: the caller's input schema, or
|
|
64
69
|
* `undefined` when the tool can do nothing for them and is not listed. Calls are checked anyway.
|
|
@@ -83,7 +88,10 @@ export declare function encodedJsonSchema(schema: Schema.Top): JsonSchemaObject;
|
|
|
83
88
|
export declare const ACTION_OPTIONS_ARGUMENT = "_options";
|
|
84
89
|
/** The `me` tool's marker: tool calls accept the runtime envelope's chat source. */
|
|
85
90
|
export declare const RUNTIME_SOURCE_META = "segmentstream/runtime-source";
|
|
86
|
-
/**
|
|
91
|
+
/**
|
|
92
|
+
* The `chat_post_message` tool's marker: the deployment posts exact messages to its chat.
|
|
93
|
+
* @legacy ontology.chat-post-message
|
|
94
|
+
*/
|
|
87
95
|
export declare const CHAT_POST_META = "segmentstream/chat-post";
|
|
88
96
|
/**
|
|
89
97
|
* The MCP tools of an ontology: `me`, `discover` when it ranks by meaning, opt-in `navigation`,
|
|
@@ -2,7 +2,7 @@ import { Schema } from "effect";
|
|
|
2
2
|
import { METRIC_QUERY_NAME_PATTERN, isBuiltInUser } from "../../metric-routes.js";
|
|
3
3
|
import { InvalidOntology } from "../../diagnostics.js";
|
|
4
4
|
import { GENERATED_UPSERT_PREFIX, humanize, mcpToolName } from "../../names.js";
|
|
5
|
-
import { effectivePermissions, isAllowed, permissionOf, READ_VERB, } from "../../permissions.js";
|
|
5
|
+
import { allowsAction, effectivePermissions, isAllowed, permissionOf, READ_VERB, } from "../../permissions.js";
|
|
6
6
|
import { FIND_CONDITIONS, findToolInput } from "./find-input.js";
|
|
7
7
|
import { metricDescription, metricsFor, modelMetricDescription, modelMetricsFor, reachesOf, RECORDS_OR_METRIC_QUERIES, } from "../../render.js";
|
|
8
8
|
import { actionInputJsonSchema } from "../../runtime/generated-actions.js";
|
|
@@ -267,7 +267,10 @@ function readTools(planned, schemas, built) {
|
|
|
267
267
|
export const ACTION_OPTIONS_ARGUMENT = "_options";
|
|
268
268
|
/** The `me` tool's marker: tool calls accept the runtime envelope's chat source. */
|
|
269
269
|
export const RUNTIME_SOURCE_META = "segmentstream/runtime-source";
|
|
270
|
-
/**
|
|
270
|
+
/**
|
|
271
|
+
* The `chat_post_message` tool's marker: the deployment posts exact messages to its chat.
|
|
272
|
+
* @legacy ontology.chat-post-message
|
|
273
|
+
*/
|
|
271
274
|
export const CHAT_POST_META = "segmentstream/chat-post";
|
|
272
275
|
const ACTION_OPTIONS_SCHEMA = {
|
|
273
276
|
type: "object",
|
|
@@ -299,7 +302,7 @@ function actionToolInput(action) {
|
|
|
299
302
|
};
|
|
300
303
|
}
|
|
301
304
|
function actionTool(planned, plannedAction) {
|
|
302
|
-
const { name, action, permissions } = plannedAction;
|
|
305
|
+
const { name, action, permissions, anyPermission } = plannedAction;
|
|
303
306
|
const hints = hintsOf(planned, plannedAction);
|
|
304
307
|
return {
|
|
305
308
|
name: mcpToolName(planned.name, name),
|
|
@@ -314,6 +317,7 @@ function actionTool(planned, plannedAction) {
|
|
|
314
317
|
openWorldHint: action.connections.length > 0 && hints.connectionsReadOnly !== true,
|
|
315
318
|
},
|
|
316
319
|
permissions,
|
|
320
|
+
...(anyPermission === undefined ? {} : { anyPermission }),
|
|
317
321
|
target: { kind: "action", planned, action: plannedAction },
|
|
318
322
|
};
|
|
319
323
|
}
|
|
@@ -772,6 +776,7 @@ export function mcpTools(built, chatHistory = false, storage = {}, navigation =
|
|
|
772
776
|
tools[discover] = { ...tools[discover], description: DISCOVER_BY_MEANING };
|
|
773
777
|
}
|
|
774
778
|
}
|
|
779
|
+
// @legacy ontology.chat-post-message chat_post_message, send as me by its earlier name.
|
|
775
780
|
const post = tools.findIndex((tool) => tool.name === "chat_post_message");
|
|
776
781
|
if (post >= 0) {
|
|
777
782
|
const original = tools[post];
|
|
@@ -943,6 +948,7 @@ export function mcpTools(built, chatHistory = false, storage = {}, navigation =
|
|
|
943
948
|
...tool,
|
|
944
949
|
inputSchema: withConfirmation(tool.inputSchema, tool.annotations),
|
|
945
950
|
// Exact chat posting confirms its own preview of the destination and the posted text.
|
|
951
|
+
// @legacy ontology.chat-post-message
|
|
946
952
|
...(tool.target.kind === "action" &&
|
|
947
953
|
tool.name !== "chat_post_message" &&
|
|
948
954
|
needsConfirmation(tool.annotations)
|
|
@@ -980,7 +986,7 @@ export function visibleTools(tools, caller, provenance = { kind: "unverified" })
|
|
|
980
986
|
if ((caller.kind === "guest" && USER_TOOL_TARGETS.has(tool.target.kind)) ||
|
|
981
987
|
!runtimeAllows(provenance, tool) ||
|
|
982
988
|
(tool.target.kind === "toolset" && isReadScoped(provenance)) ||
|
|
983
|
-
!tool
|
|
989
|
+
!allowsAction(tool, caller)) {
|
|
984
990
|
return [];
|
|
985
991
|
}
|
|
986
992
|
const description = tool.descriptionFor?.(caller) ?? tool.description;
|
|
@@ -989,6 +995,7 @@ export function visibleTools(tools, caller, provenance = { kind: "unverified" })
|
|
|
989
995
|
{
|
|
990
996
|
...tool,
|
|
991
997
|
description,
|
|
998
|
+
// @legacy ontology.chat-post-message
|
|
992
999
|
...(tool.name === "chat_post_message" && provenance.kind === "runtime"
|
|
993
1000
|
? {
|
|
994
1001
|
inputSchema: withConfirmation(encodedJsonSchema(RuntimeChatPostWire), tool.annotations),
|
|
@@ -14,7 +14,7 @@ import { requireRuntimeOperation } from "./mcp/runtime-policy.js";
|
|
|
14
14
|
import { ChatDelivery } from "../chat.js";
|
|
15
15
|
import { withoutConfirmation } from "./mcp/confirmation.js";
|
|
16
16
|
import { consumeChatEnvelope, CurrentChatCall } from "./chat/envelope.js";
|
|
17
|
-
import { ChatPostWire, RuntimeChatPostWire } from "./chat/schemas.js";
|
|
17
|
+
import { ChatPostWire, ChatSendWire, RuntimeChatPostWire } from "./chat/schemas.js";
|
|
18
18
|
import { OIDC_TOKEN_HEADER } from "./connections.js";
|
|
19
19
|
import { InvalidActionInput, RequestId } from "./errors.js";
|
|
20
20
|
import { recordReads } from "./record-reads.js";
|
|
@@ -233,6 +233,7 @@ function runActionOf(backend, plan, hooks, connections, replicate, chatDelivery)
|
|
|
233
233
|
"for a task whatever its workflow's role grants; the workflow sends what the task wrote.",
|
|
234
234
|
});
|
|
235
235
|
}
|
|
236
|
+
// @legacy ontology.chat-post-message The agent's sessions post through post_message alone.
|
|
236
237
|
if (planned.name === "agent_chat" && name === "post_message" && caller.kind === "runtime") {
|
|
237
238
|
const values = yield* Schema.decodeUnknownEffect(RuntimeChatPostWire)(consumed.input, {
|
|
238
239
|
onExcessProperty: "error",
|
|
@@ -333,13 +334,16 @@ function runActionOf(backend, plan, hooks, connections, replicate, chatDelivery)
|
|
|
333
334
|
const outcome = yield* delivery === undefined
|
|
334
335
|
? run
|
|
335
336
|
: run.pipe(Effect.provideService(ChatDelivery, delivery));
|
|
337
|
+
// Validating a message checks its destination, sender and exact text, sending nothing.
|
|
336
338
|
if (call.validateOnly &&
|
|
337
339
|
outcome.validation.ok &&
|
|
338
340
|
planned.name === "agent_chat" &&
|
|
339
|
-
name === "post_message" &&
|
|
341
|
+
(name === "send" || name === "post_message") &&
|
|
340
342
|
delivery) {
|
|
341
|
-
const decoded = yield* Schema.decodeUnknownEffect(
|
|
342
|
-
|
|
343
|
+
const decoded = yield* Schema.decodeUnknownEffect(
|
|
344
|
+
// @legacy ontology.chat-post-message post_message, which posts as send with as me.
|
|
345
|
+
name === "send" ? ChatSendWire : ChatPostWire)(input).pipe(Effect.mapError(() => new ActionRejected({ message: "Invalid exact chat post input." })));
|
|
346
|
+
yield* delivery.validate(name === "send" ? decoded : { ...decoded, as: "me" });
|
|
343
347
|
}
|
|
344
348
|
const { issues } = outcome.validation;
|
|
345
349
|
if (!outcome.validation.ok && !call.validateOnly) {
|
package/dist/service/plan.d.ts
CHANGED
|
@@ -31,8 +31,10 @@ export interface PlannedAction {
|
|
|
31
31
|
readonly name: string;
|
|
32
32
|
readonly action: AnyAction;
|
|
33
33
|
readonly generated: boolean;
|
|
34
|
-
/** Every permission a principal needs to run it. */
|
|
34
|
+
/** Every permission a principal needs to run it, or, with `anyPermission`, one of them. */
|
|
35
35
|
readonly permissions: ReadonlyArray<string>;
|
|
36
|
+
/** Any one of `permissions` runs the action, which checks inside what its input needs. */
|
|
37
|
+
readonly anyPermission?: true;
|
|
36
38
|
/** The entities an authored action declares it writes, by name; absent when it declares none. */
|
|
37
39
|
readonly writes?: ReadonlyArray<string>;
|
|
38
40
|
}
|
package/dist/service/plan.js
CHANGED
|
@@ -48,11 +48,13 @@ function plannedActions(resolved, generatedActions, report) {
|
|
|
48
48
|
generated: true,
|
|
49
49
|
permissions: generatedActionPermissions(resolved.name, name),
|
|
50
50
|
}));
|
|
51
|
-
const authored = resolved.actions.map(({ name, action, permission, writes }) => ({
|
|
51
|
+
const authored = resolved.actions.map(({ name, action, permission, anyOf, writes }) => ({
|
|
52
52
|
name,
|
|
53
53
|
action,
|
|
54
54
|
generated: false,
|
|
55
|
-
|
|
55
|
+
...(anyOf === undefined
|
|
56
|
+
? { permissions: [permission] }
|
|
57
|
+
: { permissions: anyOf, anyPermission: true }),
|
|
56
58
|
...(writes === undefined ? {} : { writes }),
|
|
57
59
|
}));
|
|
58
60
|
const actions = [...generated, ...authored];
|
|
@@ -386,6 +386,7 @@ export declare const Catalog: Schema.decodeTo<Schema.Struct<{
|
|
|
386
386
|
readonly name: Schema.String;
|
|
387
387
|
readonly description: Schema.String;
|
|
388
388
|
readonly permission: Schema.optionalKey<Schema.String>;
|
|
389
|
+
readonly anyOf: Schema.optionalKey<Schema.$Array<Schema.String>>;
|
|
389
390
|
readonly input: Schema.$Record<Schema.String, Schema.Codec<Schema.Json, Schema.Json, never, never>>;
|
|
390
391
|
readonly retryOnDeadlock: Schema.optionalKey<Schema.Boolean>;
|
|
391
392
|
readonly writes: Schema.optionalKey<Schema.$Array<Schema.String>>;
|
|
@@ -677,6 +678,7 @@ export declare const Catalog: Schema.decodeTo<Schema.Struct<{
|
|
|
677
678
|
readonly name: Schema.String;
|
|
678
679
|
readonly description: Schema.String;
|
|
679
680
|
readonly permission: Schema.optionalKey<Schema.String>;
|
|
681
|
+
readonly anyOf: Schema.optionalKey<Schema.$Array<Schema.String>>;
|
|
680
682
|
readonly input: Schema.$Record<Schema.String, Schema.Codec<Schema.Json, Schema.Json, never, never>>;
|
|
681
683
|
readonly retryOnDeadlock: Schema.optionalKey<Schema.Boolean>;
|
|
682
684
|
readonly writes: Schema.optionalKey<Schema.$Array<Schema.String>>;
|
|
@@ -779,6 +781,7 @@ export declare const Catalog: Schema.decodeTo<Schema.Struct<{
|
|
|
779
781
|
readonly name: Schema.String;
|
|
780
782
|
readonly description: Schema.String;
|
|
781
783
|
readonly permission: Schema.optionalKey<Schema.String>;
|
|
784
|
+
readonly anyOf: Schema.optionalKey<Schema.$Array<Schema.String>>;
|
|
782
785
|
readonly input: Schema.$Record<Schema.String, Schema.Codec<Schema.Json, Schema.Json, never, never>>;
|
|
783
786
|
readonly retryOnDeadlock: Schema.optionalKey<Schema.Boolean>;
|
|
784
787
|
readonly writes: Schema.optionalKey<Schema.$Array<Schema.String>>;
|
|
@@ -1070,6 +1073,7 @@ export declare const Catalog: Schema.decodeTo<Schema.Struct<{
|
|
|
1070
1073
|
readonly name: Schema.String;
|
|
1071
1074
|
readonly description: Schema.String;
|
|
1072
1075
|
readonly permission: Schema.optionalKey<Schema.String>;
|
|
1076
|
+
readonly anyOf: Schema.optionalKey<Schema.$Array<Schema.String>>;
|
|
1073
1077
|
readonly input: Schema.$Record<Schema.String, Schema.Codec<Schema.Json, Schema.Json, never, never>>;
|
|
1074
1078
|
readonly retryOnDeadlock: Schema.optionalKey<Schema.Boolean>;
|
|
1075
1079
|
readonly writes: Schema.optionalKey<Schema.$Array<Schema.String>>;
|
package/dist/service/service.js
CHANGED
|
@@ -11,7 +11,7 @@ import { nameToWire } from "../authoring-names.js";
|
|
|
11
11
|
import { bundledNavigationCatalog } from "../navigation-artifact.js";
|
|
12
12
|
import { parsePageCatalog } from "../navigation-catalog.js";
|
|
13
13
|
import { chatRetentionDaysOf, declaredChatRetentionDaysOf, guestOf, signUpOriginsOf, signUpRolesOf, } from "../manifest.js";
|
|
14
|
-
import {
|
|
14
|
+
import { allowsAction } from "../permissions.js";
|
|
15
15
|
import { agentToolsFor } from "../runtime/guests.js";
|
|
16
16
|
import { implicitMemberOf } from "../role.js";
|
|
17
17
|
import { sqlServiceRuntime } from "../runtime/service-binding.js";
|
|
@@ -130,7 +130,7 @@ function unmatchedRoutes(plan, verifyToken, exchangeSession, verifyRuntime) {
|
|
|
130
130
|
const authenticated = yield* withPrincipal(credential, verifyToken, backend, Effect.gen(function* () {
|
|
131
131
|
const principal = yield* CurrentPrincipal;
|
|
132
132
|
const caller = yield* currentCaller;
|
|
133
|
-
const visible = entity.actions.filter((action) => action
|
|
133
|
+
const visible = entity.actions.filter((action) => allowsAction(action, principal) &&
|
|
134
134
|
runtimeAllows(caller, { entity: entity.name, action: action.name }));
|
|
135
135
|
const suggestions = suggestNames(match[2], visible.map(({ name }) => name));
|
|
136
136
|
return toServerResponse(routeNotFound(requestId, suggestions));
|