@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
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/** Marks each token's position in a message's `text` (U+FFFC, the object replacement character). */
|
|
2
|
+
export declare const TOKEN_MARKER = "\uFFFC";
|
|
3
|
+
export declare const MAX_TOKENS = 20;
|
|
4
|
+
export declare const MAX_CONNECTOR_MENTIONS = 3;
|
|
5
|
+
/** With tool search on, a mentioned connector offering at most this many tools has them loaded. */
|
|
6
|
+
export declare const MENTION_LOAD_MAX_TOOLS = 20;
|
|
7
|
+
export type MessageToken = {
|
|
8
|
+
type: "connector";
|
|
9
|
+
connectorId: string;
|
|
10
|
+
} | {
|
|
11
|
+
type: "resource";
|
|
12
|
+
connectorId: string;
|
|
13
|
+
uri: string;
|
|
14
|
+
} | {
|
|
15
|
+
type: "file";
|
|
16
|
+
id: string;
|
|
17
|
+
} | {
|
|
18
|
+
type: "projectFile";
|
|
19
|
+
path: string;
|
|
20
|
+
} | {
|
|
21
|
+
type: "conversation";
|
|
22
|
+
id: string;
|
|
23
|
+
};
|
|
24
|
+
export type InlineRef = {
|
|
25
|
+
connectorId: string;
|
|
26
|
+
} | {
|
|
27
|
+
connectorId: string;
|
|
28
|
+
uri: string;
|
|
29
|
+
} | {
|
|
30
|
+
fileId: string;
|
|
31
|
+
} | {
|
|
32
|
+
path: string;
|
|
33
|
+
} | {
|
|
34
|
+
id: string;
|
|
35
|
+
};
|
|
36
|
+
/** One token's placeholder in the rendered text: UTF-16 offsets covering the whole placeholder. */
|
|
37
|
+
export interface InlineEntry {
|
|
38
|
+
kind: MessageToken["type"];
|
|
39
|
+
start: number;
|
|
40
|
+
end: number;
|
|
41
|
+
label: string;
|
|
42
|
+
ref: InlineRef;
|
|
43
|
+
}
|
|
44
|
+
export declare function countMarkers(text: string): number;
|
|
45
|
+
/**
|
|
46
|
+
* The identity of the entity a token names; a repeated entity counts and numbers once. A project
|
|
47
|
+
* file path is normalized through `projectFilePath`, which throws an `ApiError`
|
|
48
|
+
* `device.path_outside_project` for a path that is absolute, escapes the project, or names its root.
|
|
49
|
+
*/
|
|
50
|
+
export declare function tokenEntityKey(t: MessageToken): string;
|
|
51
|
+
/**
|
|
52
|
+
* A label safe inside a placeholder: control characters, line breaks and the token marker become
|
|
53
|
+
* a space, other invisible format characters (Cf, except the zero-width joiners that emoji and
|
|
54
|
+
* some scripts need) are dropped, square brackets become parentheses, trimmed, at most 80 code
|
|
55
|
+
* points (then an ellipsis, after trimming the cut end).
|
|
56
|
+
*/
|
|
57
|
+
export declare function foldLabel(raw: string): string;
|
|
58
|
+
/**
|
|
59
|
+
* Numbers colliding labels: the first entity (by first appearance) keeps its label, a second
|
|
60
|
+
* different entity with the same label gets " (2)", the third " (3)"; a numbered label that is
|
|
61
|
+
* itself taken by another entity skips to the next free number. A repeated key keeps its first
|
|
62
|
+
* label. Callers number per kind, passing file tokens before uploads.
|
|
63
|
+
*/
|
|
64
|
+
export declare function numberLabels(items: {
|
|
65
|
+
key: string;
|
|
66
|
+
label: string;
|
|
67
|
+
}[]): Map<string, string>;
|
|
68
|
+
export declare function placeholder(kind: MessageToken["type"], label: string, connectorLabel?: string): string;
|
|
69
|
+
/**
|
|
70
|
+
* Replaces each marker with its token's placeholder. `labelOf` gives the (already folded and
|
|
71
|
+
* numbered) label. Throws when the marker count differs from `tokens.length`; the controller
|
|
72
|
+
* validates that first, so this is a guard.
|
|
73
|
+
*/
|
|
74
|
+
export declare function renderInline(text: string, tokens: readonly MessageToken[], labelOf: (t: MessageToken) => string, connectorLabelOf: (connectorId: string) => string): {
|
|
75
|
+
text: string;
|
|
76
|
+
inline: InlineEntry[];
|
|
77
|
+
};
|
|
78
|
+
/** The connector mention block text; the last sentence is decided at send time. */
|
|
79
|
+
export declare function connectorMentionText(m: {
|
|
80
|
+
label: string;
|
|
81
|
+
name: string;
|
|
82
|
+
slug: string;
|
|
83
|
+
toolCount: number;
|
|
84
|
+
toolSearch: boolean;
|
|
85
|
+
}): string;
|
|
86
|
+
/** The text block that precedes each file's media block. */
|
|
87
|
+
export declare function fileLine(label: string): string;
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { projectFilePath } from "../devices/project-paths.js";
|
|
2
|
+
/** Marks each token's position in a message's `text` (U+FFFC, the object replacement character). */
|
|
3
|
+
export const TOKEN_MARKER = "";
|
|
4
|
+
export const MAX_TOKENS = 20;
|
|
5
|
+
export const MAX_CONNECTOR_MENTIONS = 3;
|
|
6
|
+
/** With tool search on, a mentioned connector offering at most this many tools has them loaded. */
|
|
7
|
+
export const MENTION_LOAD_MAX_TOOLS = 20;
|
|
8
|
+
const MAX_LABEL_POINTS = 80;
|
|
9
|
+
const MARKER_RE = new RegExp(TOKEN_MARKER, "g");
|
|
10
|
+
// invisible format characters, except the zero-width (non-)joiners that emoji and some scripts need
|
|
11
|
+
const FORMAT_CHARS = /(?![\u200C\u200D])\p{Cf}/gu;
|
|
12
|
+
// C0 and C1 control characters, DEL, and the Unicode line and paragraph separators
|
|
13
|
+
// biome-ignore lint/suspicious/noControlCharactersInRegex: control characters are what it folds
|
|
14
|
+
const CONTROLS = /[\u0000-\u001f\u007f-\u009f\u2028\u2029]+/g;
|
|
15
|
+
export function countMarkers(text) {
|
|
16
|
+
let n = 0;
|
|
17
|
+
for (let i = text.indexOf(TOKEN_MARKER); i !== -1; i = text.indexOf(TOKEN_MARKER, i + 1))
|
|
18
|
+
n++;
|
|
19
|
+
return n;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* The identity of the entity a token names; a repeated entity counts and numbers once. A project
|
|
23
|
+
* file path is normalized through `projectFilePath`, which throws an `ApiError`
|
|
24
|
+
* `device.path_outside_project` for a path that is absolute, escapes the project, or names its root.
|
|
25
|
+
*/
|
|
26
|
+
export function tokenEntityKey(t) {
|
|
27
|
+
switch (t.type) {
|
|
28
|
+
case "connector":
|
|
29
|
+
return `connector:${t.connectorId}`;
|
|
30
|
+
case "resource":
|
|
31
|
+
return `resource:${t.connectorId}:${t.uri}`;
|
|
32
|
+
case "file":
|
|
33
|
+
return `file:${t.id}`;
|
|
34
|
+
case "projectFile":
|
|
35
|
+
return `projectFile:${projectFilePath(t.path)}`;
|
|
36
|
+
case "conversation":
|
|
37
|
+
return `conversation:${t.id}`;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* A label safe inside a placeholder: control characters, line breaks and the token marker become
|
|
42
|
+
* a space, other invisible format characters (Cf, except the zero-width joiners that emoji and
|
|
43
|
+
* some scripts need) are dropped, square brackets become parentheses, trimmed, at most 80 code
|
|
44
|
+
* points (then an ellipsis, after trimming the cut end).
|
|
45
|
+
*/
|
|
46
|
+
export function foldLabel(raw) {
|
|
47
|
+
const folded = raw
|
|
48
|
+
.replace(CONTROLS, " ")
|
|
49
|
+
.replace(MARKER_RE, " ")
|
|
50
|
+
.replace(FORMAT_CHARS, "")
|
|
51
|
+
.replace(/\[/g, "(")
|
|
52
|
+
.replace(/\]/g, ")")
|
|
53
|
+
.trim();
|
|
54
|
+
const points = Array.from(folded);
|
|
55
|
+
return points.length > MAX_LABEL_POINTS
|
|
56
|
+
? `${points.slice(0, MAX_LABEL_POINTS).join("").trimEnd()}…`
|
|
57
|
+
: folded;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Numbers colliding labels: the first entity (by first appearance) keeps its label, a second
|
|
61
|
+
* different entity with the same label gets " (2)", the third " (3)"; a numbered label that is
|
|
62
|
+
* itself taken by another entity skips to the next free number. A repeated key keeps its first
|
|
63
|
+
* label. Callers number per kind, passing file tokens before uploads.
|
|
64
|
+
*/
|
|
65
|
+
export function numberLabels(items) {
|
|
66
|
+
const out = new Map();
|
|
67
|
+
const used = new Set();
|
|
68
|
+
const next = new Map();
|
|
69
|
+
// plain labels are claimed first-come, so a later entity's real label is never numbered away
|
|
70
|
+
for (const { key, label } of items) {
|
|
71
|
+
if (out.has(key))
|
|
72
|
+
continue;
|
|
73
|
+
if (!used.has(label)) {
|
|
74
|
+
used.add(label);
|
|
75
|
+
out.set(key, label);
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
let n = next.get(label) ?? 2;
|
|
79
|
+
while (used.has(`${label} (${n})`))
|
|
80
|
+
n++;
|
|
81
|
+
next.set(label, n + 1);
|
|
82
|
+
const numbered = `${label} (${n})`;
|
|
83
|
+
used.add(numbered);
|
|
84
|
+
out.set(key, numbered);
|
|
85
|
+
}
|
|
86
|
+
return out;
|
|
87
|
+
}
|
|
88
|
+
export function placeholder(kind, label, connectorLabel) {
|
|
89
|
+
switch (kind) {
|
|
90
|
+
case "connector":
|
|
91
|
+
return `[connector: ${label}]`;
|
|
92
|
+
case "resource":
|
|
93
|
+
return `[resource: ${label} from ${connectorLabel ?? ""}]`;
|
|
94
|
+
case "file":
|
|
95
|
+
return `[file: ${label}]`;
|
|
96
|
+
case "projectFile":
|
|
97
|
+
return `[project file: ${label}]`;
|
|
98
|
+
case "conversation":
|
|
99
|
+
return `[conversation: ${label}]`;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
function refOf(t) {
|
|
103
|
+
switch (t.type) {
|
|
104
|
+
case "connector":
|
|
105
|
+
return { connectorId: t.connectorId };
|
|
106
|
+
case "resource":
|
|
107
|
+
return { connectorId: t.connectorId, uri: t.uri };
|
|
108
|
+
case "file":
|
|
109
|
+
return { fileId: t.id };
|
|
110
|
+
case "projectFile":
|
|
111
|
+
return { path: t.path };
|
|
112
|
+
case "conversation":
|
|
113
|
+
return { id: t.id };
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Replaces each marker with its token's placeholder. `labelOf` gives the (already folded and
|
|
118
|
+
* numbered) label. Throws when the marker count differs from `tokens.length`; the controller
|
|
119
|
+
* validates that first, so this is a guard.
|
|
120
|
+
*/
|
|
121
|
+
export function renderInline(text, tokens, labelOf, connectorLabelOf) {
|
|
122
|
+
const parts = text.split(TOKEN_MARKER);
|
|
123
|
+
if (parts.length - 1 !== tokens.length) {
|
|
124
|
+
throw new Error(`renderInline: ${parts.length - 1} markers in text but ${tokens.length} tokens`);
|
|
125
|
+
}
|
|
126
|
+
let out = parts[0] ?? "";
|
|
127
|
+
const inline = [];
|
|
128
|
+
tokens.forEach((t, i) => {
|
|
129
|
+
const label = labelOf(t);
|
|
130
|
+
const ph = placeholder(t.type, label, t.type === "resource" ? connectorLabelOf(t.connectorId) : undefined);
|
|
131
|
+
inline.push({
|
|
132
|
+
kind: t.type,
|
|
133
|
+
start: out.length,
|
|
134
|
+
end: out.length + ph.length,
|
|
135
|
+
label,
|
|
136
|
+
ref: refOf(t),
|
|
137
|
+
});
|
|
138
|
+
out += ph + (parts[i + 1] ?? "");
|
|
139
|
+
});
|
|
140
|
+
return { text: out, inline };
|
|
141
|
+
}
|
|
142
|
+
/** The connector mention block text; the last sentence is decided at send time. */
|
|
143
|
+
export function connectorMentionText(m) {
|
|
144
|
+
const head = `${placeholder("connector", m.label)} The person named the connector "${m.name}" for this message. Its tools are named mcp__${m.slug}__<tool>.`;
|
|
145
|
+
if (!m.toolSearch)
|
|
146
|
+
return head;
|
|
147
|
+
return m.toolCount <= MENTION_LOAD_MAX_TOOLS
|
|
148
|
+
? `${head} Its ${m.toolCount} tools are loaded from this message on.`
|
|
149
|
+
: `${head} It has ${m.toolCount} tools; load the ones this message needs with ToolSearch.`;
|
|
150
|
+
}
|
|
151
|
+
/** The text block that precedes each file's media block. */
|
|
152
|
+
export function fileLine(label) {
|
|
153
|
+
return placeholder("file", label);
|
|
154
|
+
}
|
|
@@ -11,17 +11,24 @@ export declare const FirstMessageBody: z.ZodObject<{
|
|
|
11
11
|
project: z.ZodOptional<z.ZodString & z.ZodType<string, string, z.core.$ZodTypeInternals<string, string>>>;
|
|
12
12
|
message: z.ZodNonOptional<z.ZodOptional<z.ZodObject<{
|
|
13
13
|
text: z.ZodString;
|
|
14
|
-
|
|
14
|
+
tokens: z.ZodOptional<z.ZodArray<z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
15
|
+
type: z.ZodLiteral<"connector">;
|
|
16
|
+
connectorId: z.ZodUUID;
|
|
17
|
+
}, z.core.$strict>, z.ZodObject<{
|
|
18
|
+
type: z.ZodLiteral<"resource">;
|
|
15
19
|
connectorId: z.ZodUUID;
|
|
16
20
|
uri: z.ZodString;
|
|
17
|
-
}, z.core.$strict
|
|
18
|
-
|
|
19
|
-
|
|
21
|
+
}, z.core.$strict>, z.ZodObject<{
|
|
22
|
+
type: z.ZodLiteral<"file">;
|
|
23
|
+
id: z.ZodUUID;
|
|
24
|
+
}, z.core.$strict>, z.ZodObject<{
|
|
25
|
+
type: z.ZodLiteral<"projectFile">;
|
|
20
26
|
path: z.ZodString;
|
|
21
|
-
}, z.core.$strict
|
|
22
|
-
|
|
27
|
+
}, z.core.$strict>, z.ZodObject<{
|
|
28
|
+
type: z.ZodLiteral<"conversation">;
|
|
23
29
|
id: z.ZodString;
|
|
24
|
-
}, z.core.$strict>>>;
|
|
30
|
+
}, z.core.$strict>], "type">>>;
|
|
31
|
+
files: z.ZodOptional<z.ZodArray<z.ZodUUID>>;
|
|
25
32
|
model: z.ZodOptional<z.ZodObject<{
|
|
26
33
|
provider: z.ZodString;
|
|
27
34
|
model: z.ZodString;
|
|
@@ -49,12 +49,8 @@ let PermissionProfilesController = class PermissionProfilesController {
|
|
|
49
49
|
const out = await this.conversations.createWithMessage(org.orgId, requireUser(p).account.id, agentId, {
|
|
50
50
|
text: message.text,
|
|
51
51
|
clientMessageId: message.clientMessageId,
|
|
52
|
-
...(message.
|
|
52
|
+
...(message.tokens !== undefined ? { tokens: message.tokens } : {}),
|
|
53
53
|
...(message.files !== undefined ? { files: message.files } : {}),
|
|
54
|
-
...(message.projectFiles !== undefined ? { projectFiles: message.projectFiles } : {}),
|
|
55
|
-
...(message.conversationRefs !== undefined
|
|
56
|
-
? { conversationRefs: message.conversationRefs }
|
|
57
|
-
: {}),
|
|
58
54
|
...(message.model !== undefined ? { model: message.model } : {}),
|
|
59
55
|
...(message.effort !== undefined ? { effort: message.effort } : {}),
|
|
60
56
|
...(message.skill !== undefined ? { skill: message.skill } : {}),
|
|
@@ -414,10 +414,8 @@ let PromptSuggestionGenerator = class PromptSuggestionGenerator {
|
|
|
414
414
|
step: 0,
|
|
415
415
|
provider: u.provider,
|
|
416
416
|
model: u.model,
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
cacheReadTokens: u.usage.cacheReadTokens,
|
|
420
|
-
cacheWriteTokens: u.usage.cacheWriteTokens,
|
|
417
|
+
// the whole usage, `cacheWriteByTtl` included (1 h writes are priced by their tier)
|
|
418
|
+
...u.usage,
|
|
421
419
|
purpose: "suggestion",
|
|
422
420
|
},
|
|
423
421
|
};
|
|
@@ -3,7 +3,6 @@ import type { ScheduleMarker } from "../automations/types.js";
|
|
|
3
3
|
import type { KernelSchemaDb } from "../db/kernel-schema.js";
|
|
4
4
|
import { ApiError } from "../errors.js";
|
|
5
5
|
import { type InvocationRecord, type RenderedInvocation } from "../skills/invoke.js";
|
|
6
|
-
import type { AttachmentRef } from "./conversations.service.js";
|
|
7
6
|
import { type AppendEvent, type EventLog } from "./event-log.js";
|
|
8
7
|
import type { ConversationEvent, LogContentBlock } from "./events.js";
|
|
9
8
|
import { type MessageContext } from "./message-context.js";
|
|
@@ -80,7 +79,6 @@ export interface QueuedFile {
|
|
|
80
79
|
export interface QueueInput {
|
|
81
80
|
text: string;
|
|
82
81
|
clientMessageId: string;
|
|
83
|
-
attachments?: AttachmentRef[];
|
|
84
82
|
files?: string[];
|
|
85
83
|
/** A model / effort pick, applied when the message is sent (as a direct send's). */
|
|
86
84
|
model?: ModelRef;
|
|
@@ -77,12 +77,12 @@ let ConversationQueue = class ConversationQueue {
|
|
|
77
77
|
async add(orgId, conversationId, accountId, input, internal = {}) {
|
|
78
78
|
const conv = await this.conversations.writable(orgId, conversationId, accountId);
|
|
79
79
|
const files = input.files ?? [];
|
|
80
|
-
checkText(input.text, files.length,
|
|
80
|
+
checkText(input.text, files.length, 0, input.skill !== undefined);
|
|
81
81
|
const prior = await this.prior(conversationId, input.clientMessageId);
|
|
82
82
|
if (prior)
|
|
83
83
|
return prior;
|
|
84
|
-
// a cheap unlocked check first
|
|
85
|
-
//
|
|
84
|
+
// a cheap unlocked check first (before a skill is rendered); the locked check below stays
|
|
85
|
+
// authoritative
|
|
86
86
|
const { n: queued } = await this.db
|
|
87
87
|
.selectFrom("conversation_queue")
|
|
88
88
|
.select((eb) => eb.fn.countAll().as("n"))
|
|
@@ -91,13 +91,12 @@ let ConversationQueue = class ConversationQueue {
|
|
|
91
91
|
if (Number(queued) >= QUEUE_MAX_ITEMS)
|
|
92
92
|
throw queueFull();
|
|
93
93
|
await this.conversations.checkPick(orgId, conv, accountId, input);
|
|
94
|
-
// a skill invocation is rendered now
|
|
94
|
+
// a skill invocation is rendered now (spec §4.5)
|
|
95
95
|
const invoked = await this.conversations.prepareQueuedSkill(orgId, conv, accountId, input);
|
|
96
|
-
const extra = await this.conversations.resolveExtra(orgId, accountId, input);
|
|
97
96
|
const skill = invoked === undefined || input.skill === undefined
|
|
98
97
|
? undefined
|
|
99
98
|
: storedSkill(input.skill.name, invoked);
|
|
100
|
-
const out = await this.addIn(orgId, conversationId, accountId, input, files, skill,
|
|
99
|
+
const out = await this.addIn(orgId, conversationId, accountId, input, files, skill, internal);
|
|
101
100
|
if (out === null) {
|
|
102
101
|
return ((await this.prior(conversationId, input.clientMessageId)) ?? { item: null, created: false });
|
|
103
102
|
}
|
|
@@ -108,7 +107,7 @@ let ConversationQueue = class ConversationQueue {
|
|
|
108
107
|
return { item: toItem(out.row), created: out.created };
|
|
109
108
|
}
|
|
110
109
|
// its own transaction: the caller notifies and publishes after the commit
|
|
111
|
-
async addIn(orgId, conversationId, accountId, input, files, skill,
|
|
110
|
+
async addIn(orgId, conversationId, accountId, input, files, skill, internal) {
|
|
112
111
|
const t = this.db.db;
|
|
113
112
|
const conv = await t
|
|
114
113
|
.selectFrom("conversations")
|
|
@@ -160,12 +159,9 @@ let ConversationQueue = class ConversationQueue {
|
|
|
160
159
|
account_id: accountId,
|
|
161
160
|
position: last === null ? 1 : Number(last) + 1,
|
|
162
161
|
text: noNul(input.text),
|
|
163
|
-
content: payloadJson([
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
...blocks,
|
|
167
|
-
]),
|
|
168
|
-
attachments: payloadJson(extra.attachments),
|
|
162
|
+
content: payloadJson([...(skill === undefined ? [] : [skill.first]), ...blocks]),
|
|
163
|
+
// only leftover rows (older versions) carry `@` resources
|
|
164
|
+
attachments: payloadJson([]),
|
|
169
165
|
files: JSON.stringify(fileMeta),
|
|
170
166
|
client_message_id: input.clientMessageId,
|
|
171
167
|
model: input.model === undefined ? null : JSON.stringify(input.model),
|
|
@@ -807,6 +807,8 @@ export interface ToolOutputsTable {
|
|
|
807
807
|
export interface ToolSpecsTable {
|
|
808
808
|
spec_hash: string;
|
|
809
809
|
spec: Json<unknown>;
|
|
810
|
+
/** 0201: the definition's JSON text as sent (key order kept); null on older rows. */
|
|
811
|
+
spec_text: ColumnType<string | null, string | null | undefined, string | null>;
|
|
810
812
|
created_at: Ts;
|
|
811
813
|
}
|
|
812
814
|
/** bytea 列:读出为 Buffer,写入可传 Buffer / Uint8Array。 */
|
package/dist/errors.d.ts
CHANGED
|
@@ -404,6 +404,12 @@ export declare const ERROR_CODES: {
|
|
|
404
404
|
readonly "conversation.reference_unavailable": 422;
|
|
405
405
|
/** A message references the conversation it is written in; `details: [{ id }]`. */
|
|
406
406
|
readonly "conversation.reference_self": 422;
|
|
407
|
+
/**
|
|
408
|
+
* A connector a message names cannot be used by the conversation's agent now (composer inline
|
|
409
|
+
* tokens spec §3.4); `details: [{ connectorId, reason: "not_found" | "needs_config" |
|
|
410
|
+
* "not_offered" }]`.
|
|
411
|
+
*/
|
|
412
|
+
readonly "conversation.connector_unavailable": 422;
|
|
407
413
|
readonly "run.not_active": 409;
|
|
408
414
|
/** A run that is not one of the conversation's (the live draft route, spec 2026-11-07 §3.2). */
|
|
409
415
|
readonly "run.not_found": 404;
|
|
@@ -420,6 +426,12 @@ export declare const ERROR_CODES: {
|
|
|
420
426
|
readonly "connector.tool_not_found": 404;
|
|
421
427
|
readonly "connector.tool_not_pending": 409;
|
|
422
428
|
readonly "connector.resources_disabled": 409;
|
|
429
|
+
/**
|
|
430
|
+
* A resource a message references has nothing to read: an MCP Apps UI template or an empty body
|
|
431
|
+
* (composer inline tokens spec §3.4); `details: [{ connectorId, uri, reason: "ui_template" |
|
|
432
|
+
* "empty" }]`.
|
|
433
|
+
*/
|
|
434
|
+
readonly "connector.resource_unreadable": 422;
|
|
423
435
|
/** Per-member tool policies on a connector with one org credential (decision 2026-11-09). */
|
|
424
436
|
readonly "connector.not_personal": 409;
|
|
425
437
|
/**
|
package/dist/errors.js
CHANGED
|
@@ -404,6 +404,12 @@ export const ERROR_CODES = {
|
|
|
404
404
|
"conversation.reference_unavailable": 422,
|
|
405
405
|
/** A message references the conversation it is written in; `details: [{ id }]`. */
|
|
406
406
|
"conversation.reference_self": 422,
|
|
407
|
+
/**
|
|
408
|
+
* A connector a message names cannot be used by the conversation's agent now (composer inline
|
|
409
|
+
* tokens spec §3.4); `details: [{ connectorId, reason: "not_found" | "needs_config" |
|
|
410
|
+
* "not_offered" }]`.
|
|
411
|
+
*/
|
|
412
|
+
"conversation.connector_unavailable": 422,
|
|
407
413
|
"run.not_active": 409,
|
|
408
414
|
/** A run that is not one of the conversation's (the live draft route, spec 2026-11-07 §3.2). */
|
|
409
415
|
"run.not_found": 404,
|
|
@@ -420,6 +426,12 @@ export const ERROR_CODES = {
|
|
|
420
426
|
"connector.tool_not_found": 404,
|
|
421
427
|
"connector.tool_not_pending": 409,
|
|
422
428
|
"connector.resources_disabled": 409,
|
|
429
|
+
/**
|
|
430
|
+
* A resource a message references has nothing to read: an MCP Apps UI template or an empty body
|
|
431
|
+
* (composer inline tokens spec §3.4); `details: [{ connectorId, uri, reason: "ui_template" |
|
|
432
|
+
* "empty" }]`.
|
|
433
|
+
*/
|
|
434
|
+
"connector.resource_unreadable": 422,
|
|
423
435
|
/** Per-member tool policies on a connector with one org credential (decision 2026-11-09). */
|
|
424
436
|
"connector.not_personal": 409,
|
|
425
437
|
/**
|
package/dist/evals/snapshot.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { normalizeStoredDefinition } from "../agents/definition.js";
|
|
2
2
|
import { ApiError } from "../errors.js";
|
|
3
3
|
import { childRunDefinition } from "../runtime/subagents/definition.js";
|
|
4
|
+
import { storedToolSpec } from "../tools/specs.js";
|
|
4
5
|
/**
|
|
5
6
|
* The follow-up narrowing of run `runId`: the one on the last sub-agent task message up to the
|
|
6
7
|
* run's last event (its terminal event when it has one), if any. A task message logged after the
|
|
@@ -75,12 +76,13 @@ async function snapshotOf(db, ids, conv, agentVersion, events) {
|
|
|
75
76
|
if (hashes.size > 0) {
|
|
76
77
|
const specRows = await db
|
|
77
78
|
.selectFrom("tool_specs")
|
|
78
|
-
.select(["spec_hash", "spec"])
|
|
79
|
+
.select(["spec_hash", "spec", "spec_text"])
|
|
79
80
|
.where("spec_hash", "in", [...hashes].sort())
|
|
80
81
|
.orderBy("spec_hash")
|
|
81
82
|
.execute();
|
|
83
|
+
// the JSON text as sent (key order kept), so the rebuilt request serializes like the original
|
|
82
84
|
for (const r of specRows)
|
|
83
|
-
toolSpecs[r.spec_hash] = r
|
|
85
|
+
toolSpecs[r.spec_hash] = storedToolSpec(r);
|
|
84
86
|
}
|
|
85
87
|
const started = events.find((e) => e.type === "subagent_started")?.payload;
|
|
86
88
|
const invokedSkill = started?.template.invokedSkill;
|
|
@@ -227,6 +227,19 @@ export declare class LibraryFilesService {
|
|
|
227
227
|
* of what it held owed (`file-release`). Returns how many it took.
|
|
228
228
|
*/
|
|
229
229
|
expireBatch(orgId: string): Promise<number>;
|
|
230
|
+
/**
|
|
231
|
+
* The per-message file cap (`attachments.maxPerMessage`): more files → `validation_failed`
|
|
232
|
+
* (path `files`). {@link attachForMessage} checks it; a message with file tokens is checked
|
|
233
|
+
* before its remote reads too.
|
|
234
|
+
*/
|
|
235
|
+
checkMessageFileCount(count: number): void;
|
|
236
|
+
/**
|
|
237
|
+
* The names of the files in `ids` the sender owns and has not deleted (composer inline tokens
|
|
238
|
+
* spec §3.2: the labels a message's file tokens and uploads get), read-only and outside any
|
|
239
|
+
* transaction. An id it does not return is refused by {@link attachForMessage} in the message's
|
|
240
|
+
* transaction, with the codes it gives.
|
|
241
|
+
*/
|
|
242
|
+
labelsFor(orgId: string, accountId: string, ids: readonly string[]): Promise<Map<string, string>>;
|
|
230
243
|
/**
|
|
231
244
|
* Attaches the sender's files to a message, in the message's transaction (rows `FOR UPDATE`, in
|
|
232
245
|
* id order). The caller has already checked that the sender may write to the conversation (this
|
|
@@ -710,6 +710,38 @@ let LibraryFilesService = class LibraryFilesService {
|
|
|
710
710
|
}
|
|
711
711
|
return rows.length;
|
|
712
712
|
}
|
|
713
|
+
/**
|
|
714
|
+
* The per-message file cap (`attachments.maxPerMessage`): more files → `validation_failed`
|
|
715
|
+
* (path `files`). {@link attachForMessage} checks it; a message with file tokens is checked
|
|
716
|
+
* before its remote reads too.
|
|
717
|
+
*/
|
|
718
|
+
checkMessageFileCount(count) {
|
|
719
|
+
const max = this.config.get().attachments.maxPerMessage;
|
|
720
|
+
if (count > max) {
|
|
721
|
+
throw new ApiError("validation_failed", "Request validation failed", [
|
|
722
|
+
{ path: "files", message: `At most ${max} files per message` },
|
|
723
|
+
]);
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
/**
|
|
727
|
+
* The names of the files in `ids` the sender owns and has not deleted (composer inline tokens
|
|
728
|
+
* spec §3.2: the labels a message's file tokens and uploads get), read-only and outside any
|
|
729
|
+
* transaction. An id it does not return is refused by {@link attachForMessage} in the message's
|
|
730
|
+
* transaction, with the codes it gives.
|
|
731
|
+
*/
|
|
732
|
+
async labelsFor(orgId, accountId, ids) {
|
|
733
|
+
if (ids.length === 0)
|
|
734
|
+
return new Map();
|
|
735
|
+
const rows = await this.db
|
|
736
|
+
.selectFrom("files")
|
|
737
|
+
.select(["id", "name"])
|
|
738
|
+
.where("org_id", "=", orgId)
|
|
739
|
+
.where("account_id", "=", accountId)
|
|
740
|
+
.where("id", "in", [...new Set(ids)])
|
|
741
|
+
.where("status", "!=", "deleted")
|
|
742
|
+
.execute();
|
|
743
|
+
return new Map(rows.map((r) => [r.id, r.name]));
|
|
744
|
+
}
|
|
713
745
|
/**
|
|
714
746
|
* Attaches the sender's files to a message, in the message's transaction (rows `FOR UPDATE`, in
|
|
715
747
|
* id order). The caller has already checked that the sender may write to the conversation (this
|
|
@@ -727,12 +759,7 @@ let LibraryFilesService = class LibraryFilesService {
|
|
|
727
759
|
*/
|
|
728
760
|
async attachForMessage(orgId, conversationId, accountId, ids, firstSeq) {
|
|
729
761
|
const unique = [...new Set(ids)];
|
|
730
|
-
|
|
731
|
-
if (ids.length > max) {
|
|
732
|
-
throw new ApiError("validation_failed", "Request validation failed", [
|
|
733
|
-
{ path: "files", message: `At most ${max} files per message` },
|
|
734
|
-
]);
|
|
735
|
-
}
|
|
762
|
+
this.checkMessageFileCount(ids.length);
|
|
736
763
|
if (unique.length !== ids.length) {
|
|
737
764
|
throw new ApiError("validation_failed", "Request validation failed", [
|
|
738
765
|
{ path: "files", message: "Each file may appear only once per message" },
|
package/dist/net/safe-fetch.js
CHANGED
|
@@ -113,7 +113,7 @@ export function describeFetchError(e) {
|
|
|
113
113
|
const code = cause.code;
|
|
114
114
|
const text = `${e.message} (${typeof code === "string" ? `${code}: ` : ""}${cause.message})`;
|
|
115
115
|
return typeof code === "string" && CONNECTION_CODES.has(code)
|
|
116
|
-
? `${text}. If this deployment reaches the internet through a proxy, set net.httpsProxy (
|
|
116
|
+
? `${text}. If this deployment reaches the internet through a proxy, set net.httpsProxy (CAPYBARA_NET__HTTPS_PROXY); HTTPS_PROXY is not read`
|
|
117
117
|
: text;
|
|
118
118
|
}
|
|
119
119
|
export async function safeFetch(url, init, policy, opts = {}) {
|
|
@@ -80,6 +80,11 @@ export const PLATFORM_EXCLUSIONS = [
|
|
|
80
80
|
category: "transport",
|
|
81
81
|
reason: "Streams avatar image bytes",
|
|
82
82
|
},
|
|
83
|
+
{
|
|
84
|
+
route: "GET /api/v1/orgs/:orgId/agents/:agentId/mentionable-connectors",
|
|
85
|
+
category: "internal",
|
|
86
|
+
reason: "Feeds the @ connector menu of the web input box; no operation gives it, an agent already has its connectors' tools",
|
|
87
|
+
},
|
|
83
88
|
{
|
|
84
89
|
route: "POST /api/v1/orgs/:orgId/agents/:agentId/owner",
|
|
85
90
|
category: "human-only",
|
|
@@ -672,7 +677,7 @@ export const PLATFORM_EXCLUSIONS = [
|
|
|
672
677
|
{
|
|
673
678
|
route: "GET /api/v1/orgs/:orgId/connectors/resources",
|
|
674
679
|
category: "internal",
|
|
675
|
-
reason: "Feeds the @
|
|
680
|
+
reason: "Feeds the @ picker of the web input box, whose second level lists one connector's readable resources (`connectorId`); no operation gives it, an agent reads connector resources through its connector tools",
|
|
676
681
|
},
|
|
677
682
|
{
|
|
678
683
|
route: "POST /api/v1/orgs/:orgId/connectors/:connectorId/prompts/:name",
|
|
@@ -411,8 +411,13 @@ export declare function buildModelRequest(input: BuildModelRequestInput): {
|
|
|
411
411
|
mediaTokens: number;
|
|
412
412
|
};
|
|
413
413
|
/**
|
|
414
|
-
*
|
|
415
|
-
*
|
|
414
|
+
* The tool keys loaded in the log under tool search, in order of first appearance by seq: the union
|
|
415
|
+
* of every `tool_result.loadedTools` (what `ToolSearch` loaded) and every
|
|
416
|
+
* `user_message.payload.connectorMentions[].loadedTools` (a mentioned connector's offered tools,
|
|
417
|
+
* composer inline tokens spec §3.5). Read only when the step runs with tool search; a key the step
|
|
418
|
+
* does not offer is ignored by the caller. The compaction boundary is not looked at: loaded tools
|
|
419
|
+
* stay loaded after a summary (the model may keep using them from the summary, and the tool list
|
|
420
|
+
* sent is append-only).
|
|
416
421
|
*/
|
|
417
422
|
export declare function loadedToolKeys(events: readonly ConversationEvent[]): string[];
|
|
418
423
|
/** system 各块文本的 sha256(hex);按 JSON 数组哈希,避免块边界歧义。 */
|
|
@@ -1176,16 +1176,27 @@ export function buildModelRequest(input) {
|
|
|
1176
1176
|
return { request, meta, mediaTokens };
|
|
1177
1177
|
}
|
|
1178
1178
|
/**
|
|
1179
|
-
*
|
|
1180
|
-
*
|
|
1179
|
+
* The tool keys loaded in the log under tool search, in order of first appearance by seq: the union
|
|
1180
|
+
* of every `tool_result.loadedTools` (what `ToolSearch` loaded) and every
|
|
1181
|
+
* `user_message.payload.connectorMentions[].loadedTools` (a mentioned connector's offered tools,
|
|
1182
|
+
* composer inline tokens spec §3.5). Read only when the step runs with tool search; a key the step
|
|
1183
|
+
* does not offer is ignored by the caller. The compaction boundary is not looked at: loaded tools
|
|
1184
|
+
* stay loaded after a summary (the model may keep using them from the summary, and the tool list
|
|
1185
|
+
* sent is append-only).
|
|
1181
1186
|
*/
|
|
1182
1187
|
export function loadedToolKeys(events) {
|
|
1183
1188
|
const out = new Set();
|
|
1184
1189
|
for (const e of [...events].sort((a, b) => a.seq - b.seq)) {
|
|
1185
|
-
if (e.type
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1190
|
+
if (e.type === "tool_result") {
|
|
1191
|
+
for (const k of e.payload.loadedTools ?? [])
|
|
1192
|
+
out.add(k);
|
|
1193
|
+
}
|
|
1194
|
+
else if (e.type === "user_message") {
|
|
1195
|
+
for (const m of e.payload.connectorMentions ?? []) {
|
|
1196
|
+
for (const k of m.loadedTools)
|
|
1197
|
+
out.add(k);
|
|
1198
|
+
}
|
|
1199
|
+
}
|
|
1189
1200
|
}
|
|
1190
1201
|
return [...out];
|
|
1191
1202
|
}
|
|
@@ -124,9 +124,9 @@ export declare function openingSeqs(events: readonly ConversationEvent[], runId:
|
|
|
124
124
|
/**
|
|
125
125
|
* The opening messages a manual compaction's summary re-renders (context compaction spec §4.7,
|
|
126
126
|
* maintainer decision 2026-11-28): a compact run is started by no message, so its own run has none;
|
|
127
|
-
* the turn it cuts into is the one the latest run a message started (trigger `message
|
|
128
|
-
* `schedule`) opened, and {@link openingSeqs} of that run decides which of
|
|
129
|
-
* summary covers. None when no such run is in the log.
|
|
127
|
+
* the turn it cuts into is the one the latest run a message started (trigger `message`,
|
|
128
|
+
* `schedule` or `automation_event`) opened, and {@link openingSeqs} of that run decides which of
|
|
129
|
+
* its opening messages the summary covers. None when no such run is in the log.
|
|
130
130
|
*/
|
|
131
131
|
export declare function manualOpeningSeqs(events: readonly ConversationEvent[], coversUpToSeq: number, absorbedUpToSeq: number): number[];
|
|
132
132
|
/**
|