@capybara-hq/kernel 0.1.0-rc.115 → 0.1.0-rc.116
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/bootstrap.json +3 -3
- package/dist/agents/agents.controller.d.ts +13 -1
- package/dist/agents/agents.controller.js +24 -2
- package/dist/catalog/catalog.service.d.ts +11 -1
- package/dist/catalog/catalog.service.js +36 -2
- package/dist/catalog/marketplaces.service.d.ts +1 -1
- package/dist/catalog/platform-components.js +13 -0
- package/dist/catalog/plugin-connectors.d.ts +35 -0
- package/dist/catalog/plugin-connectors.js +81 -0
- package/dist/channels/supervisor.d.ts +5 -1
- package/dist/channels/supervisor.js +17 -1
- package/dist/conversations/conversations.controller.d.ts +46 -14
- package/dist/conversations/conversations.controller.js +82 -47
- package/dist/conversations/conversations.service.d.ts +93 -59
- package/dist/conversations/conversations.service.js +255 -72
- package/dist/conversations/events.d.ts +31 -0
- package/dist/conversations/events.js +41 -2
- package/dist/conversations/inline-tokens.d.ts +87 -0
- package/dist/conversations/inline-tokens.js +154 -0
- package/dist/conversations/permission-profiles.controller.d.ts +14 -7
- package/dist/conversations/permission-profiles.controller.js +1 -5
- package/dist/conversations/prompt-suggestions.js +2 -4
- package/dist/conversations/queue.d.ts +0 -2
- package/dist/conversations/queue.service.js +9 -13
- package/dist/db/kernel-schema.d.ts +2 -0
- package/dist/errors.d.ts +12 -0
- package/dist/errors.js +12 -0
- package/dist/evals/snapshot.js +4 -2
- package/dist/files/library-files.service.d.ts +13 -0
- package/dist/files/library-files.service.js +33 -6
- package/dist/net/safe-fetch.js +1 -1
- package/dist/platform/exclusions.js +6 -1
- package/dist/runtime/context/build.d.ts +7 -2
- package/dist/runtime/context/build.js +17 -6
- package/dist/runtime/context/compaction.d.ts +3 -3
- package/dist/runtime/context/compaction.js +12 -13
- package/dist/sandbox/review/classifier.js +5 -1
- package/dist/tools/connector-content.controller.d.ts +6 -2
- package/dist/tools/connector-content.controller.js +13 -3
- package/dist/tools/connector-content.d.ts +55 -15
- package/dist/tools/connector-content.js +112 -54
- package/dist/tools/connectors.service.d.ts +3 -2
- package/dist/tools/connectors.service.js +6 -20
- package/dist/tools/mentionable-connectors.d.ts +88 -0
- package/dist/tools/mentionable-connectors.js +218 -0
- package/dist/tools/pipeline.js +43 -6
- package/dist/tools/specs.d.ts +13 -4
- package/dist/tools/specs.js +22 -8
- package/dist/tools/tools.module.js +7 -0
- package/package.json +3 -3
- package/src/migrations/0201_tool_specs_text.sql +8 -0
|
@@ -39,8 +39,10 @@ import { permissionScopeHash } from "../sandbox/permission-facts.js";
|
|
|
39
39
|
import { revokeSharesIn } from "../shares/revoke.js";
|
|
40
40
|
import { SkillInvoker } from "../skills/invoke.js";
|
|
41
41
|
import { ApprovalsService } from "../tools/approvals.service.js";
|
|
42
|
-
import { ConnectorContent } from "../tools/connector-content.js";
|
|
42
|
+
import { attachmentMeta, ConnectorContent, resourceBlockText, } from "../tools/connector-content.js";
|
|
43
|
+
import { MentionableConnectorsService } from "../tools/mentionable-connectors.js";
|
|
43
44
|
import { QuestionsService } from "../tools/questions.service.js";
|
|
45
|
+
import { READ_RESOURCE } from "../tools/resources.js";
|
|
44
46
|
import { ConversationAccess, parentReachable } from "./access.js";
|
|
45
47
|
import { readAgentTree } from "./agent-tree.js";
|
|
46
48
|
import { conversationRefText, REF_TURN_MESSAGES, refMessageText } from "./conversation-ref.js";
|
|
@@ -48,6 +50,7 @@ import { EventLog } from "./event-log.js";
|
|
|
48
50
|
import { peopleOfEvents } from "./event-people.js";
|
|
49
51
|
import { refFileId } from "./events.js";
|
|
50
52
|
import { userMessageIdemKey } from "./idem-keys.js";
|
|
53
|
+
import { connectorMentionText, fileLine, foldLabel, MENTION_LOAD_MAX_TOOLS, numberLabels, placeholder, renderInline, tokenEntityKey, } from "./inline-tokens.js";
|
|
51
54
|
import { readMessageContext, } from "./message-context.js";
|
|
52
55
|
import { PermissionProfilesService } from "./permission-profiles.service.js";
|
|
53
56
|
import { NO_SELECTION, planSelection, readSelection, sameSelection, } from "./selection.js";
|
|
@@ -112,6 +115,39 @@ export { MAX_PROJECT_FILES } from "../devices/device-project-files.service.js";
|
|
|
112
115
|
* the `user_message`, so replay rebuilds it from the log as it is.
|
|
113
116
|
*/
|
|
114
117
|
export const projectFileText = (path, root) => `Referenced project file: ${path} (in the project root ${root}; read it with Read when needed).`;
|
|
118
|
+
/** The files a message attaches, in block order: its file tokens' files, then its uploads. */
|
|
119
|
+
const messageFiles = (input, extra) => {
|
|
120
|
+
const tokens = extra.fileTokenIds ?? [];
|
|
121
|
+
return [...tokens, ...uploadsBeside(input.files ?? [], tokens)];
|
|
122
|
+
};
|
|
123
|
+
/** A message's uploads without the files its file tokens already name (attached once). */
|
|
124
|
+
const uploadsBeside = (uploads, fileTokenIds) => {
|
|
125
|
+
const named = new Set(fileTokenIds);
|
|
126
|
+
return uploads.filter((id) => !named.has(id));
|
|
127
|
+
};
|
|
128
|
+
/**
|
|
129
|
+
* The files' blocks with a `[file: <label>]` text block before each media block (composer inline
|
|
130
|
+
* tokens spec §3.3), so the model reads which file each one is (an image is sent bare). A file
|
|
131
|
+
* with no given label is labelled by its name, numbered after the given labels.
|
|
132
|
+
*/
|
|
133
|
+
export function withFileLines(blocks, labels) {
|
|
134
|
+
const given = labels ?? new Map();
|
|
135
|
+
const numbered = numberLabels([
|
|
136
|
+
...[...given].map(([id, label]) => ({ key: id, label })),
|
|
137
|
+
...blocks.flatMap((b) => (b.type === "image" || b.type === "document" || b.type === "file") && b.source.kind === "ref"
|
|
138
|
+
? [{ key: refFileId(b.source), label: foldLabel(b.name ?? "") }]
|
|
139
|
+
: []),
|
|
140
|
+
]);
|
|
141
|
+
return blocks.flatMap((b) => (b.type === "image" || b.type === "document" || b.type === "file") && b.source.kind === "ref"
|
|
142
|
+
? [
|
|
143
|
+
{
|
|
144
|
+
type: "text",
|
|
145
|
+
text: fileLine(given.get(refFileId(b.source)) ?? numbered.get(refFileId(b.source)) ?? ""),
|
|
146
|
+
},
|
|
147
|
+
b,
|
|
148
|
+
]
|
|
149
|
+
: [b]);
|
|
150
|
+
}
|
|
115
151
|
/**
|
|
116
152
|
* A referenced conversation that cannot be referenced (422, `details: [{ id, reason? }]`): one
|
|
117
153
|
* code and one message for every case, so another person's conversation reads as a missing one.
|
|
@@ -228,6 +264,7 @@ let ConversationsService = class ConversationsService {
|
|
|
228
264
|
txHost;
|
|
229
265
|
ceilings;
|
|
230
266
|
outbox;
|
|
267
|
+
mentions;
|
|
231
268
|
constructor(permissions, db, eventLog, agents, access,
|
|
232
269
|
/** Starts and stops the conversation's runs. */
|
|
233
270
|
runs,
|
|
@@ -252,7 +289,9 @@ let ConversationsService = class ConversationsService {
|
|
|
252
289
|
/** ... and its pending network asks (network ask-to-allow spec §6.7). */
|
|
253
290
|
approvals, events, txHost, ceilings,
|
|
254
291
|
/** Owes the snapshot deletions of the shares a deletion revokes (file sharing spec §9). */
|
|
255
|
-
outbox
|
|
292
|
+
outbox,
|
|
293
|
+
/** The agent's tools for a message naming connectors or resources (inline tokens §3.4). */
|
|
294
|
+
mentions) {
|
|
256
295
|
this.permissions = permissions;
|
|
257
296
|
this.db = db;
|
|
258
297
|
this.eventLog = eventLog;
|
|
@@ -273,6 +312,7 @@ let ConversationsService = class ConversationsService {
|
|
|
273
312
|
this.txHost = txHost;
|
|
274
313
|
this.ceilings = ceilings;
|
|
275
314
|
this.outbox = outbox;
|
|
315
|
+
this.mentions = mentions;
|
|
276
316
|
}
|
|
277
317
|
/**
|
|
278
318
|
* Creates an empty conversation bound to its device (`pick`, see
|
|
@@ -330,16 +370,16 @@ let ConversationsService = class ConversationsService {
|
|
|
330
370
|
const next = await this.planSwitch(orgId, agent.definition, NO_SELECTION, input);
|
|
331
371
|
// the project its files are checked in, or its project skill is rendered from, read before the
|
|
332
372
|
// write (the binding inside it decides)
|
|
333
|
-
const project =
|
|
373
|
+
const project = input.tokens?.some((t) => t.type === "projectFile") === true ||
|
|
334
374
|
(input.skill !== undefined && projectSkillOf(input.skill.name) !== undefined)
|
|
335
375
|
? await this.bindingFor(orgId, accountId, deviceId, pick.project)
|
|
336
376
|
: undefined;
|
|
337
|
-
const extra = await this.resolveExtra(orgId, accountId, input, {
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
});
|
|
342
|
-
const invoked = await this.prepareSkill(orgId, accountId, agentId, { conversationId: undefined, deviceId, ...(project !== undefined && { project }) }, agent.definition, input);
|
|
377
|
+
const extra = await this.resolveExtra(orgId, accountId, input, { deviceId, project, signal: internal.signal },
|
|
378
|
+
// no sandbox conversation yet: the row is written after the resolution (its id with it), so
|
|
379
|
+
// the device-bound builtins are listed as for a conversation without one; a connector's
|
|
380
|
+
// tools never depend on it, only the tool search threshold's count may
|
|
381
|
+
{ agent: agent.definition, source: internal.source });
|
|
382
|
+
const invoked = await this.prepareSkill(orgId, accountId, agentId, { conversationId: undefined, deviceId, ...(project !== undefined && { project }) }, agent.definition, { ...(input.skill !== undefined && { skill: input.skill }), text: extra.text ?? input.text });
|
|
343
383
|
const id = randomUUID();
|
|
344
384
|
let all;
|
|
345
385
|
try {
|
|
@@ -410,7 +450,7 @@ let ConversationsService = class ConversationsService {
|
|
|
410
450
|
const initialEvents = internal.initialPermission
|
|
411
451
|
? await this.permissions.initialize(orgId, id, accountId, conv.deviceId, project?.path ?? null, project?.name ?? null, internal.initialPermission)
|
|
412
452
|
: [];
|
|
413
|
-
const blocks = await this.bindIn(orgId, id, accountId, input
|
|
453
|
+
const blocks = await this.bindIn(orgId, id, accountId, messageFiles(input, extra), extra.fileLabels);
|
|
414
454
|
// a new conversation has no run yet, so nothing to skip
|
|
415
455
|
const written = await this.writeMessage(orgId, id, accountId, input, next, extra, blocks, invoked, internal);
|
|
416
456
|
return [...initialEvents, ...written.events];
|
|
@@ -1001,26 +1041,17 @@ let ConversationsService = class ConversationsService {
|
|
|
1001
1041
|
* ends. A failed `ensureRun` is logged, not thrown: the message's transaction owes the ensure-run
|
|
1002
1042
|
* backstop, which starts the run once the queue is reachable (transactional outbox spec §6).
|
|
1003
1043
|
*
|
|
1004
|
-
* `
|
|
1005
|
-
*
|
|
1006
|
-
*
|
|
1007
|
-
* `
|
|
1008
|
-
* `clientMessageId` returns the written event without
|
|
1044
|
+
* `tokens` (composer inline tokens spec §3): resolved as the sender before the write
|
|
1045
|
+
* ({@link resolveExtra}): each marker becomes its placeholder in `content[0]`, the referenced
|
|
1046
|
+
* blocks (resources, project files, conversations, connector mentions) follow it, and
|
|
1047
|
+
* `payload.inline` / `connectorMentions` record them. Any refusal writes nothing; a repeated send
|
|
1048
|
+
* with the same `clientMessageId` returns the written event without resolving again.
|
|
1009
1049
|
*
|
|
1010
1050
|
* `files` (uploaded attachment ids): checked and bound to this conversation in the transaction
|
|
1011
1051
|
* that writes the event (see {@link BindFiles}) and appended last as image / document reference
|
|
1012
|
-
* blocks
|
|
1013
|
-
* only references, never bytes. With empty `text` (files only: the HTTP layer ensures
|
|
1014
|
-
* and no
|
|
1015
|
-
*
|
|
1016
|
-
* `projectFiles` (composer menus §5): checked in the conversation's stored project before the
|
|
1017
|
-
* write ({@link resolveExtra}); one block each after the `@` resources' bodies, before the
|
|
1018
|
-
* uploaded files' blocks, and `payload.projectFiles`. Any refusal writes nothing; a repeated
|
|
1019
|
-
* send with the same `clientMessageId` returns the written event without checking again.
|
|
1020
|
-
*
|
|
1021
|
-
* `conversationRefs` (@ conversations spec §1): read as the sender in the same step, one block
|
|
1022
|
-
* each after the project files' blocks, and `payload.conversationRefs`; refused and retried
|
|
1023
|
-
* like the project files.
|
|
1052
|
+
* blocks, each after its `[file: <name>]` line; any unavailable file writes nothing. The log
|
|
1053
|
+
* holds only references, never bytes. With empty `text` (files only: the HTTP layer ensures
|
|
1054
|
+
* `files` and no tokens then) no text block of the person's is written.
|
|
1024
1055
|
*/
|
|
1025
1056
|
async postUserMessage(orgId, conversationId, accountId, input,
|
|
1026
1057
|
/** The request's signal: the project file check on the device stops when the client hangs up. */
|
|
@@ -1034,18 +1065,27 @@ let ConversationsService = class ConversationsService {
|
|
|
1034
1065
|
return existing;
|
|
1035
1066
|
}
|
|
1036
1067
|
}
|
|
1037
|
-
const
|
|
1068
|
+
const namesConnectors = input.tokens?.some((t) => t.type === "connector" || t.type === "resource") === true;
|
|
1069
|
+
const needsAgent = input.model !== undefined ||
|
|
1070
|
+
input.effort !== undefined ||
|
|
1071
|
+
input.skill !== undefined ||
|
|
1072
|
+
namesConnectors;
|
|
1038
1073
|
const agent = needsAgent
|
|
1039
1074
|
? (await this.agents.get(orgId, conv.agentId, accountId)).definition
|
|
1040
1075
|
: undefined;
|
|
1041
|
-
const extra = await this.resolveExtra(orgId, accountId, input, {
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1076
|
+
const extra = await this.resolveExtra(orgId, accountId, input, { ...conv, conversationId, signal }, agent !== undefined && namesConnectors
|
|
1077
|
+
? {
|
|
1078
|
+
agent,
|
|
1079
|
+
source: await this.sourceOf(orgId, conversationId),
|
|
1080
|
+
sandboxConversationId: conversationId,
|
|
1081
|
+
}
|
|
1082
|
+
: undefined);
|
|
1046
1083
|
const invoked = agent === undefined
|
|
1047
1084
|
? undefined
|
|
1048
|
-
: await this.prepareSkill(orgId, accountId, conv.agentId, { conversationId, deviceId: conv.deviceId }, agent,
|
|
1085
|
+
: await this.prepareSkill(orgId, accountId, conv.agentId, { conversationId, deviceId: conv.deviceId }, agent, {
|
|
1086
|
+
...(input.skill !== undefined && { skill: input.skill }),
|
|
1087
|
+
text: extra.text ?? input.text,
|
|
1088
|
+
});
|
|
1049
1089
|
// one transaction: the sender, time zone and language are read with the message's write
|
|
1050
1090
|
// (run context spec §3.2)
|
|
1051
1091
|
let current = conv.selection;
|
|
@@ -1106,7 +1146,7 @@ let ConversationsService = class ConversationsService {
|
|
|
1106
1146
|
if (!sameSelection(expectedSelection, selection))
|
|
1107
1147
|
return { selection };
|
|
1108
1148
|
}
|
|
1109
|
-
const blocks = await this.bindIn(orgId, conversationId, accountId, input
|
|
1149
|
+
const blocks = await this.bindIn(orgId, conversationId, accountId, messageFiles(input, extra), extra.fileLabels);
|
|
1110
1150
|
const written = await this.writeMessage(orgId, conversationId, accountId, input, next, extra, blocks, invoked);
|
|
1111
1151
|
const event = written.events.at(-1);
|
|
1112
1152
|
return {
|
|
@@ -1305,37 +1345,160 @@ let ConversationsService = class ConversationsService {
|
|
|
1305
1345
|
const agent = await this.agents.get(orgId, conv.agentId, accountId);
|
|
1306
1346
|
return this.prepareSkill(orgId, accountId, conv.agentId, { conversationId: conv.id, deviceId: conv.deviceId }, agent.definition, input);
|
|
1307
1347
|
}
|
|
1348
|
+
/** Resolves what a message's tokens reference before it is written ({@link resolveTokens}). */
|
|
1349
|
+
async resolveExtra(orgId, accountId, input, target,
|
|
1350
|
+
/** Where the agent's tools are resolved; required when the tokens name connectors or resources. */
|
|
1351
|
+
agentContext) {
|
|
1352
|
+
if (input.tokens !== undefined) {
|
|
1353
|
+
return this.resolveTokens(orgId, accountId, input, input.tokens, target, agentContext);
|
|
1354
|
+
}
|
|
1355
|
+
return { blocks: [], attachments: [] };
|
|
1356
|
+
}
|
|
1308
1357
|
/**
|
|
1309
|
-
*
|
|
1310
|
-
*
|
|
1311
|
-
*
|
|
1312
|
-
*
|
|
1313
|
-
*
|
|
1314
|
-
*
|
|
1315
|
-
*
|
|
1316
|
-
*
|
|
1317
|
-
*
|
|
1318
|
-
*
|
|
1358
|
+
* {@link resolveExtra} for a message with tokens (composer inline tokens spec §3), as the sender,
|
|
1359
|
+
* outside any transaction; any refusal writes nothing. Each entity is resolved once however
|
|
1360
|
+
* often its token repeats: project file paths are normalized first (a path leaving the project →
|
|
1361
|
+
* `device.path_outside_project` before anything is read), then the connectors named and the
|
|
1362
|
+
* resources' connectors against the agent's tools in one resolution
|
|
1363
|
+
* ({@link MentionableConnectorsService.resolveForMessage}: `conversation.connector_unavailable`),
|
|
1364
|
+
* the project files and the referenced conversations as without tokens, the resources last
|
|
1365
|
+
* ({@link ConnectorContent.readResources}; one whose `read_resource` the agent is not offered →
|
|
1366
|
+
* `connector.resources_disabled`), and the files' names. Labels are numbered per kind in text
|
|
1367
|
+
* order (file tokens before uploads); each marker becomes its placeholder in the text (the trimmed
|
|
1368
|
+
* arguments of a skill invocation, where `inline` then points). Blocks, in order: resources,
|
|
1369
|
+
* project files, conversations, connector mentions; the files' blocks follow in the write.
|
|
1319
1370
|
*/
|
|
1320
|
-
async
|
|
1321
|
-
|
|
1322
|
-
const
|
|
1323
|
-
const
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1371
|
+
async resolveTokens(orgId, accountId, input, tokens, target, agentContext) {
|
|
1372
|
+
// keyed first: a project file path leaving the project is refused before anything is read
|
|
1373
|
+
const keyed = tokens.map((t) => ({ t, key: tokenEntityKey(t) }));
|
|
1374
|
+
const distinct = (kind) => {
|
|
1375
|
+
const seen = new Set();
|
|
1376
|
+
return keyed.flatMap(({ t, key }) => {
|
|
1377
|
+
if (t.type !== kind || seen.has(key))
|
|
1378
|
+
return [];
|
|
1379
|
+
seen.add(key);
|
|
1380
|
+
return [t];
|
|
1381
|
+
});
|
|
1382
|
+
};
|
|
1383
|
+
const named = distinct("connector").map((t) => t.connectorId);
|
|
1384
|
+
const resourceRefs = distinct("resource").map(({ connectorId, uri }) => ({ connectorId, uri }));
|
|
1385
|
+
const paths = [...new Set(distinct("projectFile").map((t) => projectFilePath(t.path)))];
|
|
1386
|
+
const conversationIds = distinct("conversation").map((t) => t.id);
|
|
1387
|
+
const fileTokenIds = distinct("file").map((t) => t.id);
|
|
1388
|
+
// a file both named by a token and uploaded is attached once, as the token
|
|
1389
|
+
const uploads = uploadsBeside(input.files ?? [], fileTokenIds);
|
|
1390
|
+
// the per-message file cap, before anything remote is read (attaching checks it again)
|
|
1391
|
+
this.libraryFiles.checkMessageFileCount(fileTokenIds.length + uploads.length);
|
|
1392
|
+
// the agent's tools first (the database only), then the checks as without tokens, the
|
|
1393
|
+
// resources (remote reads) last
|
|
1394
|
+
let resolved;
|
|
1395
|
+
if (named.length > 0 || resourceRefs.length > 0) {
|
|
1396
|
+
if (agentContext === undefined) {
|
|
1397
|
+
throw new Error("resolveTokens: connector or resource tokens need the agent's context");
|
|
1398
|
+
}
|
|
1399
|
+
resolved = await this.mentions.resolveForMessage(orgId, agentContext.agent, {
|
|
1400
|
+
source: agentContext.source,
|
|
1401
|
+
sandboxConversationId: agentContext.sandboxConversationId,
|
|
1402
|
+
}, named);
|
|
1403
|
+
}
|
|
1404
|
+
const toolSearch = resolved?.toolSearch === true;
|
|
1405
|
+
const mentioned = resolved?.connectors ?? [];
|
|
1406
|
+
const files = await this.checkProjectFiles(orgId, accountId, paths.map((path) => ({ path })), target);
|
|
1407
|
+
const conversations = await this.readConversationRefs(orgId, accountId, conversationIds.map((id) => ({ id })), target?.conversationId);
|
|
1408
|
+
const read = resolved === undefined || resourceRefs.length === 0
|
|
1409
|
+
? []
|
|
1410
|
+
: await this.content.readResources(orgId, accountId, resourceRefs, new Set(resolved.tools.flatMap((t) => t.source.kind === "connector" &&
|
|
1411
|
+
t.source.resources === true &&
|
|
1412
|
+
t.source.name === READ_RESOURCE
|
|
1413
|
+
? [t.source.connectorId]
|
|
1414
|
+
: [])));
|
|
1415
|
+
const fileNames = await this.libraryFiles.labelsFor(orgId, accountId, [
|
|
1416
|
+
...fileTokenIds,
|
|
1417
|
+
...uploads,
|
|
1418
|
+
]);
|
|
1419
|
+
// labels, numbered per kind in text order
|
|
1420
|
+
const connectorName = new Map([
|
|
1421
|
+
...mentioned.map((c) => [c.id, c.name]),
|
|
1422
|
+
...read.map((r) => [r.connectorId, r.connectorName]),
|
|
1423
|
+
]);
|
|
1424
|
+
const connectorLabels = numberLabels(keyed.flatMap(({ t }) => t.type === "connector" || t.type === "resource"
|
|
1425
|
+
? [
|
|
1426
|
+
{
|
|
1427
|
+
key: t.connectorId,
|
|
1428
|
+
label: foldLabel(connectorName.get(t.connectorId) ?? t.connectorId),
|
|
1429
|
+
},
|
|
1430
|
+
]
|
|
1431
|
+
: []));
|
|
1432
|
+
const resourceName = new Map(read.map((r) => [`resource:${r.connectorId}:${r.uri}`, r.name]));
|
|
1433
|
+
const conversationTitle = new Map(conversations.map((c) => [`conversation:${c.meta.id}`, c.meta.title]));
|
|
1434
|
+
const labelOfKey = (t, key) => {
|
|
1435
|
+
switch (t.type) {
|
|
1436
|
+
case "connector":
|
|
1437
|
+
return foldLabel(connectorName.get(t.connectorId) ?? t.connectorId);
|
|
1438
|
+
case "resource":
|
|
1439
|
+
return foldLabel(resourceName.get(key) ?? t.uri);
|
|
1440
|
+
case "file":
|
|
1441
|
+
return foldLabel(fileNames.get(t.id) ?? t.id);
|
|
1442
|
+
case "projectFile":
|
|
1443
|
+
return foldLabel(key.slice("projectFile:".length));
|
|
1444
|
+
case "conversation":
|
|
1445
|
+
return foldLabel(conversationTitle.get(key) ?? "") || "Untitled conversation";
|
|
1446
|
+
}
|
|
1447
|
+
};
|
|
1448
|
+
// numbered per kind (each kind has its own placeholder), file tokens before uploads
|
|
1449
|
+
const kindLabels = new Map();
|
|
1450
|
+
for (const kind of ["resource", "file", "projectFile", "conversation"]) {
|
|
1451
|
+
const items = [
|
|
1452
|
+
...keyed
|
|
1453
|
+
.filter(({ t }) => t.type === kind)
|
|
1454
|
+
.map(({ t, key }) => ({ key, label: labelOfKey(t, key) })),
|
|
1455
|
+
...(kind === "file"
|
|
1456
|
+
? uploads.map((id) => ({ key: `file:${id}`, label: foldLabel(fileNames.get(id) ?? id) }))
|
|
1457
|
+
: []),
|
|
1458
|
+
];
|
|
1459
|
+
for (const [k, v] of numberLabels(items))
|
|
1460
|
+
kindLabels.set(k, v);
|
|
1461
|
+
}
|
|
1462
|
+
const labelOf = (t) => t.type === "connector"
|
|
1463
|
+
? (connectorLabels.get(t.connectorId) ?? t.connectorId)
|
|
1464
|
+
: (kindLabels.get(tokenEntityKey(t)) ?? "");
|
|
1465
|
+
const connectorLabelOf = (id) => connectorLabels.get(id) ?? id;
|
|
1466
|
+
const rendered = renderInline(input.skill === undefined ? input.text : input.text.trim(),
|
|
1467
|
+
// `inline` names a project file by its normalized path
|
|
1468
|
+
tokens.map((t) => (t.type === "projectFile" ? { ...t, path: projectFilePath(t.path) } : t)), labelOf, connectorLabelOf);
|
|
1469
|
+
const connectorMentions = mentioned.map((c) => {
|
|
1470
|
+
const keys = c.tools.map((t) => t.key);
|
|
1471
|
+
return {
|
|
1472
|
+
connectorId: c.id,
|
|
1473
|
+
slug: c.slug,
|
|
1474
|
+
name: c.name,
|
|
1475
|
+
toolCount: keys.length,
|
|
1476
|
+
// recorded whatever the send-time tool search state: request building reads the loaded
|
|
1477
|
+
// keys only under tool search, so they are inert when a step has it off
|
|
1478
|
+
loadedTools: keys.length <= MENTION_LOAD_MAX_TOOLS ? keys : [],
|
|
1479
|
+
};
|
|
1480
|
+
});
|
|
1481
|
+
const blocks = [
|
|
1482
|
+
...read.map((r) => resourceBlockText(r, placeholder("resource", kindLabels.get(`resource:${r.connectorId}:${r.uri}`) ?? r.name, connectorLabelOf(r.connectorId)))),
|
|
1483
|
+
...(files?.paths ?? []).map((path) => `${placeholder("projectFile", kindLabels.get(`projectFile:${path}`) ?? path)} ${projectFileText(path, files?.root ?? "")}`),
|
|
1484
|
+
...conversations.map((c) => `${placeholder("conversation", kindLabels.get(`conversation:${c.meta.id}`) ?? "")}\n${c.text}`),
|
|
1485
|
+
...mentioned.map((c) => connectorMentionText({
|
|
1486
|
+
label: connectorLabelOf(c.id),
|
|
1487
|
+
name: foldLabel(c.name),
|
|
1488
|
+
slug: c.slug,
|
|
1489
|
+
toolCount: c.tools.length,
|
|
1490
|
+
toolSearch,
|
|
1491
|
+
})),
|
|
1492
|
+
].map((text) => ({ type: "text", text }));
|
|
1493
|
+
const fileLabels = new Map([...fileTokenIds, ...uploads].map((id) => [id, kindLabels.get(`file:${id}`) ?? id]));
|
|
1329
1494
|
return {
|
|
1330
|
-
blocks
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
],
|
|
1338
|
-
attachments: resolved.attachments,
|
|
1495
|
+
blocks,
|
|
1496
|
+
text: rendered.text,
|
|
1497
|
+
inline: rendered.inline,
|
|
1498
|
+
connectorMentions,
|
|
1499
|
+
fileTokenIds,
|
|
1500
|
+
fileLabels,
|
|
1501
|
+
attachments: read.map(attachmentMeta),
|
|
1339
1502
|
...(files !== undefined && {
|
|
1340
1503
|
projectFiles: files.paths.map((path) => ({ path })),
|
|
1341
1504
|
projectRoot: files.root,
|
|
@@ -1343,6 +1506,16 @@ let ConversationsService = class ConversationsService {
|
|
|
1343
1506
|
...(conversations.length > 0 && { conversationRefs: conversations.map((c) => c.meta) }),
|
|
1344
1507
|
};
|
|
1345
1508
|
}
|
|
1509
|
+
/** The conversation's source (where it comes from: web, IM, an automation). */
|
|
1510
|
+
async sourceOf(orgId, conversationId) {
|
|
1511
|
+
const row = await this.db
|
|
1512
|
+
.selectFrom("conversations")
|
|
1513
|
+
.select("source")
|
|
1514
|
+
.where("id", "=", conversationId)
|
|
1515
|
+
.where("org_id", "=", orgId)
|
|
1516
|
+
.executeTakeFirstOrThrow();
|
|
1517
|
+
return row.source;
|
|
1518
|
+
}
|
|
1346
1519
|
/**
|
|
1347
1520
|
* {@link resolveExtra}'s referenced conversations (@ conversations spec §1), read as the sender
|
|
1348
1521
|
* outside any transaction, once each in the body's order. Each must be the sender's own chat in
|
|
@@ -1477,7 +1650,9 @@ let ConversationsService = class ConversationsService {
|
|
|
1477
1650
|
const paths = [...new Set(refs.map((r) => projectFilePath(r.path)))];
|
|
1478
1651
|
const { missing } = await this.projectFiles.statAll({ orgId, accountId, deviceId: target.deviceId }, project.path, paths, target.signal);
|
|
1479
1652
|
if (missing.length > 0) {
|
|
1480
|
-
throw projectFileUnavailable(`${clipped(missing[0])} is not a file in the project ${project.name}; pick it again`,
|
|
1653
|
+
throw projectFileUnavailable(`${clipped(missing[0])} is not a file in the project ${project.name}; pick it again`,
|
|
1654
|
+
// the whole normalized path, so the composer can mark the token it came from
|
|
1655
|
+
missing.map((path) => ({ path })));
|
|
1481
1656
|
}
|
|
1482
1657
|
return { paths, root: project.path };
|
|
1483
1658
|
}
|
|
@@ -1487,17 +1662,20 @@ let ConversationsService = class ConversationsService {
|
|
|
1487
1662
|
* none → no blocks. The files join the conversation's files, so open pages read the list again
|
|
1488
1663
|
* after the commit (`changed: files`, a hint).
|
|
1489
1664
|
*/
|
|
1490
|
-
async bindIn(orgId, conversationId, accountId, files
|
|
1665
|
+
async bindIn(orgId, conversationId, accountId, files,
|
|
1666
|
+
/** Each file's label (composer inline tokens spec §3.2); a file not in it: its name. */
|
|
1667
|
+
labels) {
|
|
1491
1668
|
if (files.length === 0)
|
|
1492
1669
|
return [];
|
|
1493
1670
|
const blocks = await this.libraryFiles.attachForMessage(orgId, conversationId, accountId, files);
|
|
1494
1671
|
afterCommit(() => this.eventLog.publishChanged(conversationId, "files"));
|
|
1495
|
-
return blocks;
|
|
1672
|
+
return withFileLines(blocks, labels);
|
|
1496
1673
|
}
|
|
1497
1674
|
userMessage(accountId, input, extra, fileBlocks, invoked, context, markers = {}) {
|
|
1498
1675
|
const { schedule, automationEvent } = markers;
|
|
1499
|
-
// a skill invocation's rendered text replaces the typed arguments as content[0]
|
|
1500
|
-
|
|
1676
|
+
// a skill invocation's rendered text replaces the typed arguments as content[0]; tokens
|
|
1677
|
+
// put their placeholders into the text
|
|
1678
|
+
const text = invoked?.text ?? extra.text ?? input.text;
|
|
1501
1679
|
return {
|
|
1502
1680
|
type: "user_message",
|
|
1503
1681
|
...(input.clientMessageId !== undefined && {
|
|
@@ -1515,6 +1693,9 @@ let ConversationsService = class ConversationsService {
|
|
|
1515
1693
|
extra.projectFiles.length > 0 && { projectFiles: extra.projectFiles }),
|
|
1516
1694
|
...(extra.conversationRefs !== undefined &&
|
|
1517
1695
|
extra.conversationRefs.length > 0 && { conversationRefs: extra.conversationRefs }),
|
|
1696
|
+
...(extra.connectorMentions !== undefined &&
|
|
1697
|
+
extra.connectorMentions.length > 0 && { connectorMentions: extra.connectorMentions }),
|
|
1698
|
+
...(extra.inline !== undefined && extra.inline.length > 0 && { inline: extra.inline }),
|
|
1518
1699
|
authorAccountId: accountId,
|
|
1519
1700
|
...context,
|
|
1520
1701
|
...(input.clientMessageId !== undefined && { clientMessageId: input.clientMessageId }),
|
|
@@ -1526,9 +1707,10 @@ let ConversationsService = class ConversationsService {
|
|
|
1526
1707
|
};
|
|
1527
1708
|
}
|
|
1528
1709
|
/**
|
|
1529
|
-
*
|
|
1530
|
-
*
|
|
1531
|
-
*
|
|
1710
|
+
* Aborts the conversation's current run (queued / running / waiting): returns its runId; none →
|
|
1711
|
+
* `run.not_active`. A run waiting for a person has no worker: its wait is cancelled and its end
|
|
1712
|
+
* written here. Any other is broadcast through Redis, reaching whichever replica runs it; an
|
|
1713
|
+
* orphaned run (no worker executing it) gets its end written directly.
|
|
1532
1714
|
*
|
|
1533
1715
|
* A root conversation's stop stops everything below it too (agent map spec §6, changed in
|
|
1534
1716
|
* acceptance 2026-11-13: after a person stops, the conversation stays stopped). In order:
|
|
@@ -1707,6 +1889,7 @@ ConversationsService = __decorate([
|
|
|
1707
1889
|
__param(16, Inject(KernelEvents)),
|
|
1708
1890
|
__param(17, Inject(TransactionHost)),
|
|
1709
1891
|
__param(18, Inject(PermissionCeilingWrites)),
|
|
1710
|
-
__param(19, Inject(OutboxService))
|
|
1892
|
+
__param(19, Inject(OutboxService)),
|
|
1893
|
+
__param(20, Inject(MentionableConnectorsService))
|
|
1711
1894
|
], ConversationsService);
|
|
1712
1895
|
export { ConversationsService };
|
|
@@ -985,6 +985,37 @@ export declare const ConversationEventSchema: z.ZodDiscriminatedUnion<[z.ZodObje
|
|
|
985
985
|
}>;
|
|
986
986
|
upToSeq: z.ZodNumber;
|
|
987
987
|
}, z.core.$strip>>>;
|
|
988
|
+
connectorMentions: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
989
|
+
connectorId: z.ZodString;
|
|
990
|
+
slug: z.ZodString;
|
|
991
|
+
name: z.ZodString;
|
|
992
|
+
toolCount: z.ZodNumber;
|
|
993
|
+
loadedTools: z.ZodArray<z.ZodString>;
|
|
994
|
+
}, z.core.$strip>>>;
|
|
995
|
+
inline: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
996
|
+
kind: z.ZodEnum<{
|
|
997
|
+
connector: "connector";
|
|
998
|
+
conversation: "conversation";
|
|
999
|
+
file: "file";
|
|
1000
|
+
projectFile: "projectFile";
|
|
1001
|
+
resource: "resource";
|
|
1002
|
+
}>;
|
|
1003
|
+
start: z.ZodNumber;
|
|
1004
|
+
end: z.ZodNumber;
|
|
1005
|
+
label: z.ZodString;
|
|
1006
|
+
ref: z.ZodUnion<readonly [z.ZodObject<{
|
|
1007
|
+
connectorId: z.ZodString;
|
|
1008
|
+
uri: z.ZodString;
|
|
1009
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
1010
|
+
connectorId: z.ZodString;
|
|
1011
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
1012
|
+
fileId: z.ZodString;
|
|
1013
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
1014
|
+
path: z.ZodString;
|
|
1015
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
1016
|
+
id: z.ZodString;
|
|
1017
|
+
}, z.core.$strip>]>;
|
|
1018
|
+
}, z.core.$strip>>>;
|
|
988
1019
|
authorAccountId: z.ZodString;
|
|
989
1020
|
sender: z.ZodOptional<z.ZodObject<{
|
|
990
1021
|
name: z.ZodString;
|
|
@@ -913,6 +913,43 @@ export const ConversationEventSchema = z.discriminatedUnion("type", [
|
|
|
913
913
|
upToSeq: z.number().int().nonnegative(),
|
|
914
914
|
}))
|
|
915
915
|
.optional(),
|
|
916
|
+
/**
|
|
917
|
+
* The connectors the message names (composer inline tokens spec §3.5), in block order:
|
|
918
|
+
* `toolCount` the agent's offered tools of it at send time, `loadedTools` its offered tool
|
|
919
|
+
* keys when it has at most 20 (empty with more), loaded from this message on by steps that
|
|
920
|
+
* run with tool search (inert in a step without it). The mention block's last sentence is
|
|
921
|
+
* the send-time tool search snapshot. Each has a mention block after the referenced
|
|
922
|
+
* conversations' blocks. Absent: none.
|
|
923
|
+
*/
|
|
924
|
+
connectorMentions: z
|
|
925
|
+
.array(z.object({
|
|
926
|
+
connectorId: z.string(),
|
|
927
|
+
slug: z.string(),
|
|
928
|
+
name: z.string(),
|
|
929
|
+
toolCount: z.number().int().nonnegative(),
|
|
930
|
+
loadedTools: z.array(z.string()),
|
|
931
|
+
}))
|
|
932
|
+
.optional(),
|
|
933
|
+
/**
|
|
934
|
+
* Where each inline token's placeholder stands (composer inline tokens spec §3.5): UTF-16
|
|
935
|
+
* offsets into `content[0]`, or into `skill.arguments` for an invocation, its label and what
|
|
936
|
+
* it names. Display only: request building never reads it. Absent: no tokens.
|
|
937
|
+
*/
|
|
938
|
+
inline: z
|
|
939
|
+
.array(z.object({
|
|
940
|
+
kind: z.enum(["connector", "resource", "file", "projectFile", "conversation"]),
|
|
941
|
+
start: z.number().int().nonnegative(),
|
|
942
|
+
end: z.number().int().nonnegative(),
|
|
943
|
+
label: z.string(),
|
|
944
|
+
ref: z.union([
|
|
945
|
+
z.object({ connectorId: z.string(), uri: z.string() }),
|
|
946
|
+
z.object({ connectorId: z.string() }),
|
|
947
|
+
z.object({ fileId: z.string() }),
|
|
948
|
+
z.object({ path: z.string() }),
|
|
949
|
+
z.object({ id: z.string() }),
|
|
950
|
+
]),
|
|
951
|
+
}))
|
|
952
|
+
.optional(),
|
|
916
953
|
authorAccountId: z.string(),
|
|
917
954
|
/**
|
|
918
955
|
* Who wrote the message, at send time (run context spec §3.2): display name and org role
|
|
@@ -1401,8 +1438,10 @@ export const ConversationEventSchema = z.discriminatedUnion("type", [
|
|
|
1401
1438
|
*/
|
|
1402
1439
|
review: ReviewRecordSchema.optional(),
|
|
1403
1440
|
/**
|
|
1404
|
-
* `ToolSearch`
|
|
1405
|
-
* `loadedTools`
|
|
1441
|
+
* The tool keys this `ToolSearch` call loaded (absent when nothing matched). The loaded set is
|
|
1442
|
+
* the union of every `tool_result.loadedTools` and `user_message` connector mention's
|
|
1443
|
+
* `loadedTools` in the conversation (including those a summary covers), rebuilt from the log
|
|
1444
|
+
* when a request is built, never kept in process memory.
|
|
1406
1445
|
*/
|
|
1407
1446
|
loadedTools: z.array(z.string()).optional(),
|
|
1408
1447
|
/**
|