@capybara-hq/plugin-sdk 0.1.0-rc.74 → 0.1.0-rc.76
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/dist/ctx.d.ts +10 -1
- package/dist/device-provider.d.ts +18 -3
- package/dist/device-provider.js +15 -0
- package/dist/device-service.d.ts +28 -0
- package/dist/device-service.js +44 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/tool.d.ts +30 -0
- package/dist/tool.js +51 -0
- package/package.json +1 -1
package/dist/ctx.d.ts
CHANGED
|
@@ -228,9 +228,18 @@ export interface DeviceAccessCtx {
|
|
|
228
228
|
addNetworkHosts(scope: DeviceAccessScope & {
|
|
229
229
|
hosts: string[];
|
|
230
230
|
}): Promise<void>;
|
|
231
|
-
/**
|
|
231
|
+
/**
|
|
232
|
+
* Answers the handoff question of this extension's tool call `callId` from the panel.
|
|
233
|
+
* `handoffConversationId` is the conversation whose tool call raised the handoff (the extension
|
|
234
|
+
* learns it from `ToolCallContext.conversationId`; the panel may echo it back, so it is
|
|
235
|
+
* untrusted input): the kernel answers only when it is the scope conversation or one of its
|
|
236
|
+
* sub-agent descendants (a call id is unique only within a run, so two sub-agents of one tree
|
|
237
|
+
* can wait under the same one). Any other conversation, like a question that is not pending or
|
|
238
|
+
* not this extension's, refuses with `question.not_found`.
|
|
239
|
+
*/
|
|
232
240
|
answerHandoff(scope: DeviceAccessScope & {
|
|
233
241
|
callId: string;
|
|
242
|
+
handoffConversationId: string;
|
|
234
243
|
done: boolean;
|
|
235
244
|
}): Promise<void>;
|
|
236
245
|
}
|
|
@@ -315,7 +315,20 @@ export interface DeviceServiceStatus {
|
|
|
315
315
|
* `node <codeDir ?? stateDir>/<entry>`, so a provider runs exactly that.
|
|
316
316
|
*/
|
|
317
317
|
codeDir?: string;
|
|
318
|
+
/**
|
|
319
|
+
* The {@link deviceServiceFilesDigest} of the files the running process was started from, when
|
|
320
|
+
* the provider knows it (absent when stopped). The kernel compares it with the spec's files: a
|
|
321
|
+
* running service on other files (an upgraded extension) gets a reviewed start, which restarts
|
|
322
|
+
* it on the new files.
|
|
323
|
+
*/
|
|
324
|
+
filesDigest?: string;
|
|
318
325
|
}
|
|
326
|
+
/**
|
|
327
|
+
* The digest of a service's files: `sha256:` and the hex SHA-256 over each file in path order
|
|
328
|
+
* (path, NUL, byte length, NUL, bytes). Providers report it in {@link DeviceServiceStatus} and
|
|
329
|
+
* may send it to a machine instead of the files.
|
|
330
|
+
*/
|
|
331
|
+
export declare function deviceServiceFilesDigest(files: Record<string, Uint8Array>): string;
|
|
319
332
|
/**
|
|
320
333
|
* A message stream to a device service ({@link DeviceProvider.openStream},
|
|
321
334
|
* `DialInChannel.openStream`): whole messages both ways, flow-controlled by credit.
|
|
@@ -644,8 +657,9 @@ export interface DeviceProvider {
|
|
|
644
657
|
* `<extension>.<name>`, {@link DEVICE_SERVICE_ID_RE}), never by the extension's bare name.
|
|
645
658
|
* It writes `spec.files` into the service's code directory, then runs `spec.entry` with Node, the
|
|
646
659
|
* service socket and state directory named in its environment (`DEVICE_SERVICE_SOCKET_ENV`,
|
|
647
|
-
* `DEVICE_SERVICE_STATE_ENV`). A service already running under the name is kept as is
|
|
648
|
-
*
|
|
660
|
+
* `DEVICE_SERVICE_STATE_ENV`). A service already running under the name is kept as is, unless
|
|
661
|
+
* it runs other files than the spec's: then it is stopped and started on them. The kernel has
|
|
662
|
+
* passed the launch through the command review before it calls this.
|
|
649
663
|
*
|
|
650
664
|
* With `opts.ifRunning` it never starts anything and reads only `spec.name` (the kernel sends
|
|
651
665
|
* `{ name, runtime: "node", entry: "", args: [], files: {} }`, which must not be refused): a
|
|
@@ -653,7 +667,8 @@ export interface DeviceProvider {
|
|
|
653
667
|
* a restart for that runs the spec it was launched with and its stored code (the kernel calls
|
|
654
668
|
* this after the device's allowlist changed, and before reviewing a launch); a stopped one
|
|
655
669
|
* answers its `stopped` status (with `stateDir`, and `codeDir` where kept apart) and stays
|
|
656
|
-
* stopped.
|
|
670
|
+
* stopped. A running one answers its `filesDigest` when known, so the kernel reviews a start on
|
|
671
|
+
* new files before calling this without `ifRunning`.
|
|
657
672
|
*/
|
|
658
673
|
ensureService?(h: SandboxHandle, spec: DeviceServiceSpec, signal: AbortSignal, opts?: {
|
|
659
674
|
ifRunning?: boolean;
|
package/dist/device-provider.js
CHANGED
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
* (`createFileSystem`) lives until the device is deleted (`destroyFileSystem`); instances
|
|
9
9
|
* (`create`) attach to it and come and go (`destroy` drops the instance and keeps the files).
|
|
10
10
|
*/
|
|
11
|
+
import { createHash } from "node:crypto";
|
|
11
12
|
/** Device provider extension point ref. */
|
|
12
13
|
export const DEVICE_PROVIDER_EXTENSION_POINT = "device-provider@1";
|
|
13
14
|
/** The roots inside the sandbox the model may access. */
|
|
@@ -214,6 +215,20 @@ export function deviceServiceId(extension, name) {
|
|
|
214
215
|
throw new TypeError(`Invalid device service ${JSON.stringify(name)} of extension ${JSON.stringify(extension)}`);
|
|
215
216
|
return id;
|
|
216
217
|
}
|
|
218
|
+
/**
|
|
219
|
+
* The digest of a service's files: `sha256:` and the hex SHA-256 over each file in path order
|
|
220
|
+
* (path, NUL, byte length, NUL, bytes). Providers report it in {@link DeviceServiceStatus} and
|
|
221
|
+
* may send it to a machine instead of the files.
|
|
222
|
+
*/
|
|
223
|
+
export function deviceServiceFilesDigest(files) {
|
|
224
|
+
const h = createHash("sha256");
|
|
225
|
+
for (const path of Object.keys(files).sort()) {
|
|
226
|
+
const data = files[path];
|
|
227
|
+
h.update(`${path}\0${data.byteLength}\0`);
|
|
228
|
+
h.update(data);
|
|
229
|
+
}
|
|
230
|
+
return `sha256:${h.digest("hex")}`;
|
|
231
|
+
}
|
|
217
232
|
// biome-ignore lint/suspicious/noControlCharactersInRegex: control characters are exactly what must be blocked
|
|
218
233
|
const CONTROL = /[\u0000-\u001f\u007f]/;
|
|
219
234
|
/**
|
package/dist/device-service.d.ts
CHANGED
|
@@ -14,6 +14,17 @@
|
|
|
14
14
|
* - `{"kind":"request"}`: the next provider message is the request body; the service answers
|
|
15
15
|
* with exactly one message and closes the connection.
|
|
16
16
|
* - `{"kind":"stream"}`: both sides keep exchanging messages until either side closes.
|
|
17
|
+
*
|
|
18
|
+
* **Viewer streams.** On a stream the kernel opens for a viewer (a person's panel, through a
|
|
19
|
+
* viewer ticket), the first stream message after `{"kind":"stream"}` is written by the kernel, not
|
|
20
|
+
* the panel: the viewer context {@link DeviceServiceViewerContext},
|
|
21
|
+
* `{"kind":"viewer","conversationId":"<id>","rootConversationId":"<id>"}`, naming the ticket's
|
|
22
|
+
* conversation (checked at mint to be owned by the viewing person) and the root of its
|
|
23
|
+
* conversation tree (the conversation itself outside sub-agents). The panel's own messages
|
|
24
|
+
* follow, unchanged and in order. A service reads only that first message as context
|
|
25
|
+
* ({@link parseViewerContext}); a later message of the same shape comes from the panel and is
|
|
26
|
+
* never context. Providers and the device program pass the viewer context through like any
|
|
27
|
+
* stream message.
|
|
17
28
|
*/
|
|
18
29
|
/** Names the unix socket path the service listens on. */
|
|
19
30
|
export declare const DEVICE_SERVICE_SOCKET_ENV = "CAPYBARA_SERVICE_SOCKET";
|
|
@@ -75,3 +86,20 @@ export declare class ServiceMessageDecoder {
|
|
|
75
86
|
/** Removes the first `n` (at most `pending`) bytes into one new array, copying each byte once. */
|
|
76
87
|
private take;
|
|
77
88
|
}
|
|
89
|
+
/**
|
|
90
|
+
* The first stream message of a viewer stream, written by the kernel (see the protocol above):
|
|
91
|
+
* the ticket's conversation and the root of its conversation tree.
|
|
92
|
+
*/
|
|
93
|
+
export interface DeviceServiceViewerContext {
|
|
94
|
+
kind: "viewer";
|
|
95
|
+
conversationId: string;
|
|
96
|
+
rootConversationId: string;
|
|
97
|
+
}
|
|
98
|
+
/** The body of the viewer context stream message (UTF-8 JSON; the provider frames it). */
|
|
99
|
+
export declare function encodeViewerContext(ctx: DeviceServiceViewerContext): Uint8Array;
|
|
100
|
+
/**
|
|
101
|
+
* Reads a stream message body as the viewer context: `kind` is `"viewer"` and both ids are
|
|
102
|
+
* non-empty strings; only those three fields are returned. Null for anything else (not UTF-8,
|
|
103
|
+
* not JSON, another shape).
|
|
104
|
+
*/
|
|
105
|
+
export declare function parseViewerContext(body: Uint8Array | string): DeviceServiceViewerContext | null;
|
package/dist/device-service.js
CHANGED
|
@@ -14,6 +14,17 @@
|
|
|
14
14
|
* - `{"kind":"request"}`: the next provider message is the request body; the service answers
|
|
15
15
|
* with exactly one message and closes the connection.
|
|
16
16
|
* - `{"kind":"stream"}`: both sides keep exchanging messages until either side closes.
|
|
17
|
+
*
|
|
18
|
+
* **Viewer streams.** On a stream the kernel opens for a viewer (a person's panel, through a
|
|
19
|
+
* viewer ticket), the first stream message after `{"kind":"stream"}` is written by the kernel, not
|
|
20
|
+
* the panel: the viewer context {@link DeviceServiceViewerContext},
|
|
21
|
+
* `{"kind":"viewer","conversationId":"<id>","rootConversationId":"<id>"}`, naming the ticket's
|
|
22
|
+
* conversation (checked at mint to be owned by the viewing person) and the root of its
|
|
23
|
+
* conversation tree (the conversation itself outside sub-agents). The panel's own messages
|
|
24
|
+
* follow, unchanged and in order. A service reads only that first message as context
|
|
25
|
+
* ({@link parseViewerContext}); a later message of the same shape comes from the panel and is
|
|
26
|
+
* never context. Providers and the device program pass the viewer context through like any
|
|
27
|
+
* stream message.
|
|
17
28
|
*/
|
|
18
29
|
/** Names the unix socket path the service listens on. */
|
|
19
30
|
export const DEVICE_SERVICE_SOCKET_ENV = "CAPYBARA_SERVICE_SOCKET";
|
|
@@ -128,3 +139,36 @@ export class ServiceMessageDecoder {
|
|
|
128
139
|
return out;
|
|
129
140
|
}
|
|
130
141
|
}
|
|
142
|
+
/** The body of the viewer context stream message (UTF-8 JSON; the provider frames it). */
|
|
143
|
+
export function encodeViewerContext(ctx) {
|
|
144
|
+
return new TextEncoder().encode(JSON.stringify({
|
|
145
|
+
kind: "viewer",
|
|
146
|
+
conversationId: ctx.conversationId,
|
|
147
|
+
rootConversationId: ctx.rootConversationId,
|
|
148
|
+
}));
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Reads a stream message body as the viewer context: `kind` is `"viewer"` and both ids are
|
|
152
|
+
* non-empty strings; only those three fields are returned. Null for anything else (not UTF-8,
|
|
153
|
+
* not JSON, another shape).
|
|
154
|
+
*/
|
|
155
|
+
export function parseViewerContext(body) {
|
|
156
|
+
let msg;
|
|
157
|
+
try {
|
|
158
|
+
const text = typeof body === "string" ? body : new TextDecoder("utf-8", { fatal: true }).decode(body);
|
|
159
|
+
msg = JSON.parse(text);
|
|
160
|
+
}
|
|
161
|
+
catch {
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
if (typeof msg !== "object" || msg === null || Array.isArray(msg))
|
|
165
|
+
return null;
|
|
166
|
+
const { kind, conversationId, rootConversationId } = msg;
|
|
167
|
+
if (kind !== "viewer")
|
|
168
|
+
return null;
|
|
169
|
+
if (typeof conversationId !== "string" || conversationId === "")
|
|
170
|
+
return null;
|
|
171
|
+
if (typeof rootConversationId !== "string" || rootConversationId === "")
|
|
172
|
+
return null;
|
|
173
|
+
return { kind: "viewer", conversationId, rootConversationId };
|
|
174
|
+
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/tool.d.ts
CHANGED
|
@@ -140,6 +140,11 @@ export interface InputForm {
|
|
|
140
140
|
* characters, are accepted.
|
|
141
141
|
*/
|
|
142
142
|
url?: string;
|
|
143
|
+
/**
|
|
144
|
+
* Optional: longer text under the title (the reason, what to do), at most
|
|
145
|
+
* {@link INPUT_FORM_BODY_MAX} characters, shown as plain text. Keep the title short.
|
|
146
|
+
*/
|
|
147
|
+
body?: LocalizedText;
|
|
143
148
|
fields: {
|
|
144
149
|
key: string;
|
|
145
150
|
label: LocalizedText;
|
|
@@ -148,8 +153,28 @@ export interface InputForm {
|
|
|
148
153
|
multiSelect?: boolean;
|
|
149
154
|
allowOther?: boolean;
|
|
150
155
|
required?: boolean;
|
|
156
|
+
/**
|
|
157
|
+
* Buttons in place of the field's control: one per choice, the first is the primary, and pressing one answers
|
|
158
|
+
* the form with its value at once (no separate Answer). Allowed only on the form's single
|
|
159
|
+
* field, when it is required and a single choice: a `boolean` (values `true` / `false`) or a
|
|
160
|
+
* string `enum` without `multiSelect` / `allowOther` (values from the enum). 1 to
|
|
161
|
+
* {@link INPUT_CHOICES_MAX} choices with distinct values; labels at most
|
|
162
|
+
* {@link INPUT_CHOICE_LABEL_MAX} characters. Example: a confirm form `{ done: boolean }` with
|
|
163
|
+
* `[{ value: true, label: "Done" }, { value: false, label: "Cancel" }]`, or a one-value enum
|
|
164
|
+
* that renders as one button ("End wait").
|
|
165
|
+
*/
|
|
166
|
+
choices?: {
|
|
167
|
+
value: string | boolean;
|
|
168
|
+
label: LocalizedText;
|
|
169
|
+
}[];
|
|
151
170
|
}[];
|
|
152
171
|
}
|
|
172
|
+
/** The longest {@link InputForm.body}, in characters (per language). */
|
|
173
|
+
export declare const INPUT_FORM_BODY_MAX = 2000;
|
|
174
|
+
/** The most buttons a form's choices may draw. */
|
|
175
|
+
export declare const INPUT_CHOICES_MAX = 3;
|
|
176
|
+
/** The longest choice label, in characters (per language). */
|
|
177
|
+
export declare const INPUT_CHOICE_LABEL_MAX = 40;
|
|
153
178
|
/**
|
|
154
179
|
* The tool needs user input: the kernel pauses the run and renders a form; after the user submits,
|
|
155
180
|
* the tool is called again with the same input,
|
|
@@ -192,6 +217,11 @@ export interface ToolCallContext {
|
|
|
192
217
|
accountId: string;
|
|
193
218
|
agentId: string;
|
|
194
219
|
conversationId: string;
|
|
220
|
+
/**
|
|
221
|
+
* The root of the call's conversation tree (the conversation the person looks at); equal to
|
|
222
|
+
* `conversationId` outside sub-agents. Set by the kernel, never from tool input.
|
|
223
|
+
*/
|
|
224
|
+
rootConversationId: string;
|
|
195
225
|
/**
|
|
196
226
|
* The model's id for this call. Stable across every invocation of one logical call (the
|
|
197
227
|
* `InputRequired` rounds), but unique only within its run: two runs of one conversation may
|
package/dist/tool.js
CHANGED
|
@@ -76,6 +76,12 @@ export function validateToolDisplay(d) {
|
|
|
76
76
|
}
|
|
77
77
|
return out;
|
|
78
78
|
}
|
|
79
|
+
/** The longest {@link InputForm.body}, in characters (per language). */
|
|
80
|
+
export const INPUT_FORM_BODY_MAX = 2000;
|
|
81
|
+
/** The most buttons a form's choices may draw. */
|
|
82
|
+
export const INPUT_CHOICES_MAX = 3;
|
|
83
|
+
/** The longest choice label, in characters (per language). */
|
|
84
|
+
export const INPUT_CHOICE_LABEL_MAX = 40;
|
|
79
85
|
/** A tool execution failure: message is for the model and must be in English and actionable. */
|
|
80
86
|
export class ToolError extends Error {
|
|
81
87
|
kind;
|
|
@@ -164,6 +170,8 @@ export function validateInputForm(form) {
|
|
|
164
170
|
out.push("form must have at least one field");
|
|
165
171
|
if (form.url !== undefined && !isHttpUrl(form.url))
|
|
166
172
|
out.push("url must be an absolute http(s) URL of at most 2000 characters");
|
|
173
|
+
if (form.body !== undefined && !textWithin(form.body, INPUT_FORM_BODY_MAX))
|
|
174
|
+
out.push(`body must be non-empty text of at most ${INPUT_FORM_BODY_MAX} characters`);
|
|
167
175
|
const seen = new Set();
|
|
168
176
|
for (const f of form.fields) {
|
|
169
177
|
const at = `field ${JSON.stringify(f?.key)}`;
|
|
@@ -181,6 +189,49 @@ export function validateInputForm(form) {
|
|
|
181
189
|
continue;
|
|
182
190
|
if ((f.multiSelect || f.allowOther) && !isEnum(f.schema))
|
|
183
191
|
out.push(`${at}: multiSelect and allowOther require a string enum schema`);
|
|
192
|
+
if (f.choices !== undefined)
|
|
193
|
+
out.push(...choicesProblems(form, f).map((p) => `${at}: ${p}`));
|
|
194
|
+
}
|
|
195
|
+
return out;
|
|
196
|
+
}
|
|
197
|
+
/** Non-empty text (a string or both languages) of at most `max` characters per language. */
|
|
198
|
+
function textWithin(t, max) {
|
|
199
|
+
const ok = (x) => typeof x === "string" && x.trim() !== "" && x.length <= max;
|
|
200
|
+
if (typeof t === "string")
|
|
201
|
+
return ok(t);
|
|
202
|
+
return (typeof t === "object" &&
|
|
203
|
+
t !== null &&
|
|
204
|
+
ok(t.zh) &&
|
|
205
|
+
ok(t.en));
|
|
206
|
+
}
|
|
207
|
+
/** The rules of {@link InputForm} `choices` (buttons): see the field's documentation. */
|
|
208
|
+
function choicesProblems(form, f) {
|
|
209
|
+
const out = [];
|
|
210
|
+
const c = f.choices;
|
|
211
|
+
if (!Array.isArray(c) || c.length === 0 || c.length > INPUT_CHOICES_MAX)
|
|
212
|
+
return [`choices must be an array of 1 to ${INPUT_CHOICES_MAX} items`];
|
|
213
|
+
if (form.fields.length !== 1)
|
|
214
|
+
out.push("choices are allowed only on a form's single field");
|
|
215
|
+
if (f.required !== true)
|
|
216
|
+
out.push("choices require required: true");
|
|
217
|
+
const bool = f.schema.type === "boolean";
|
|
218
|
+
const single = isEnum(f.schema) && !f.multiSelect && !f.allowOther;
|
|
219
|
+
if (!bool && !single)
|
|
220
|
+
out.push("choices require a boolean or a single-choice string enum (no multiSelect / allowOther)");
|
|
221
|
+
const seen = new Set();
|
|
222
|
+
for (const x of c) {
|
|
223
|
+
const v = x?.value;
|
|
224
|
+
const valid = bool
|
|
225
|
+
? typeof v === "boolean"
|
|
226
|
+
: single && typeof v === "string" && f.schema.enum.includes(v);
|
|
227
|
+
if (!valid)
|
|
228
|
+
out.push(`choice value ${JSON.stringify(v)} is not a value of the field`);
|
|
229
|
+
else if (seen.has(v))
|
|
230
|
+
out.push(`duplicate choice value ${JSON.stringify(v)}`);
|
|
231
|
+
else
|
|
232
|
+
seen.add(v);
|
|
233
|
+
if (!textWithin(x?.label, INPUT_CHOICE_LABEL_MAX))
|
|
234
|
+
out.push(`choice label must be non-empty text of at most ${INPUT_CHOICE_LABEL_MAX} characters`);
|
|
184
235
|
}
|
|
185
236
|
return out;
|
|
186
237
|
}
|