@capybara-hq/plugin-sdk 0.1.0-rc.74 → 0.1.0-rc.75

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 CHANGED
@@ -228,9 +228,18 @@ export interface DeviceAccessCtx {
228
228
  addNetworkHosts(scope: DeviceAccessScope & {
229
229
  hosts: string[];
230
230
  }): Promise<void>;
231
- /** Answers the handoff question of this extension's tool call `callId` from the panel. */
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
  }
@@ -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;
@@ -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
@@ -1,4 +1,4 @@
1
- export declare const SDK_VERSION = "0.1.0-rc.74";
1
+ export declare const SDK_VERSION = "0.1.0-rc.75";
2
2
  export * from "./agent-template.js";
3
3
  export * from "./avatar-presets.js";
4
4
  export * from "./bootstrap.js";
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- export const SDK_VERSION = "0.1.0-rc.74";
1
+ export const SDK_VERSION = "0.1.0-rc.75";
2
2
  export * from "./agent-template.js";
3
3
  export * from "./avatar-presets.js";
4
4
  export * from "./bootstrap.js";
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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capybara-hq/plugin-sdk",
3
- "version": "0.1.0-rc.74",
3
+ "version": "0.1.0-rc.75",
4
4
  "description": "Capybara plugin SDK: the types and pure functions plugins are written against",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {