@zvada/agent-server 0.3.26 → 0.3.27
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +33 -0
- package/README.md +1 -1
- package/dist/client/client.js +27 -11
- package/dist/client/connection.js +35 -3
- package/dist/core/agents/claude-code/approvals.js +1 -1
- package/dist/core/agents/codex-app-server/plan-approval.js +1 -1
- package/dist/core/agents/scripted/scripted-agent.js +30 -3
- package/dist/core/presets.js +4 -1
- package/dist/core/runtime/agent-runtime.js +15 -5
- package/dist/core/runtime/event-processor.js +13 -8
- package/dist/core/runtime/origin-line.js +42 -0
- package/dist/core/runtime/request-broker.js +2 -1
- package/dist/core/runtime/steering.js +4 -3
- package/dist/core/runtime/turn.js +6 -4
- package/dist/operator/errors.js +97 -0
- package/dist/operator/index.js +10 -5
- package/dist/operator/mcp.js +7 -12
- package/dist/operator/operator.js +1336 -380
- package/dist/operator/refs.js +59 -0
- package/dist/operator/render.js +171 -74
- package/dist/operator/session-tools.js +87 -0
- package/dist/operator/types.js +30 -0
- package/dist/operator/view.js +82 -27
- package/dist/protocol/config.js +12 -5
- package/dist/protocol/conversation.js +3 -0
- package/dist/protocol/history.js +2 -1
- package/dist/protocol/index.js +1 -0
- package/dist/protocol/lifecycle.js +19 -0
- package/dist/protocol/origin.js +39 -0
- package/dist/protocol/reduce.js +2 -0
- package/dist/protocol/wire.js +17 -0
- package/dist/server/routes.js +15 -5
- package/dist/types/client/client.d.ts.map +1 -1
- package/dist/types/client/connection.d.ts +14 -2
- package/dist/types/client/connection.d.ts.map +1 -1
- package/dist/types/client/turns.d.ts +2 -2
- package/dist/types/client/turns.d.ts.map +1 -1
- package/dist/types/core/agents/base.d.ts +2 -0
- package/dist/types/core/agents/base.d.ts.map +1 -1
- package/dist/types/core/agents/scripted/scripted-agent.d.ts +14 -1
- package/dist/types/core/agents/scripted/scripted-agent.d.ts.map +1 -1
- package/dist/types/core/presets.d.ts.map +1 -1
- package/dist/types/core/runtime/agent-runtime.d.ts +4 -2
- package/dist/types/core/runtime/agent-runtime.d.ts.map +1 -1
- package/dist/types/core/runtime/event-processor.d.ts +2 -2
- package/dist/types/core/runtime/event-processor.d.ts.map +1 -1
- package/dist/types/core/runtime/origin-line.d.ts +15 -0
- package/dist/types/core/runtime/origin-line.d.ts.map +1 -0
- package/dist/types/core/runtime/request-broker.d.ts +1 -1
- package/dist/types/core/runtime/request-broker.d.ts.map +1 -1
- package/dist/types/core/runtime/steering.d.ts +7 -4
- package/dist/types/core/runtime/steering.d.ts.map +1 -1
- package/dist/types/core/runtime/turn.d.ts.map +1 -1
- package/dist/types/operator/errors.d.ts +83 -0
- package/dist/types/operator/errors.d.ts.map +1 -0
- package/dist/types/operator/index.d.ts +10 -5
- package/dist/types/operator/index.d.ts.map +1 -1
- package/dist/types/operator/mcp.d.ts +1 -1
- package/dist/types/operator/mcp.d.ts.map +1 -1
- package/dist/types/operator/operator.d.ts +15 -197
- package/dist/types/operator/operator.d.ts.map +1 -1
- package/dist/types/operator/refs.d.ts +25 -0
- package/dist/types/operator/refs.d.ts.map +1 -0
- package/dist/types/operator/render.d.ts +59 -19
- package/dist/types/operator/render.d.ts.map +1 -1
- package/dist/types/operator/session-tools.d.ts +61 -0
- package/dist/types/operator/session-tools.d.ts.map +1 -0
- package/dist/types/operator/types.d.ts +441 -0
- package/dist/types/operator/types.d.ts.map +1 -0
- package/dist/types/operator/view.d.ts +19 -29
- package/dist/types/operator/view.d.ts.map +1 -1
- package/dist/types/protocol/config.d.ts +20 -5
- package/dist/types/protocol/config.d.ts.map +1 -1
- package/dist/types/protocol/conversation.d.ts +44 -3
- package/dist/types/protocol/conversation.d.ts.map +1 -1
- package/dist/types/protocol/history.d.ts +1 -1
- package/dist/types/protocol/history.d.ts.map +1 -1
- package/dist/types/protocol/index.d.ts +1 -0
- package/dist/types/protocol/index.d.ts.map +1 -1
- package/dist/types/protocol/lifecycle.d.ts +40 -2
- package/dist/types/protocol/lifecycle.d.ts.map +1 -1
- package/dist/types/protocol/origin.d.ts +49 -0
- package/dist/types/protocol/origin.d.ts.map +1 -0
- package/dist/types/protocol/reduce.d.ts.map +1 -1
- package/dist/types/protocol/wire-api.d.ts +39 -1
- package/dist/types/protocol/wire-api.d.ts.map +1 -1
- package/dist/types/protocol/wire.d.ts +65 -2
- package/dist/types/protocol/wire.d.ts.map +1 -1
- package/dist/types/server/routes.d.ts.map +1 -1
- package/docs/consuming.md +56 -121
- package/docs/sync-history.md +2 -1
- package/package.json +1 -1
- package/src/client/client.ts +36 -9
- package/src/client/connection.ts +31 -1
- package/src/client/turns.ts +2 -1
- package/src/core/agents/base.ts +2 -0
- package/src/core/agents/claude-code/approvals.ts +2 -2
- package/src/core/agents/codex-app-server/plan-approval.ts +1 -1
- package/src/core/agents/scripted/scripted-agent.ts +42 -3
- package/src/core/presets.ts +4 -1
- package/src/core/runtime/agent-runtime.ts +31 -7
- package/src/core/runtime/event-processor.ts +13 -6
- package/src/core/runtime/origin-line.ts +43 -0
- package/src/core/runtime/request-broker.ts +2 -0
- package/src/core/runtime/steering.ts +8 -4
- package/src/core/runtime/turn.ts +11 -4
- package/src/operator/errors.ts +218 -0
- package/src/operator/index.ts +18 -23
- package/src/operator/mcp.ts +7 -16
- package/src/operator/operator.ts +1583 -613
- package/src/operator/refs.ts +75 -0
- package/src/operator/render.ts +208 -98
- package/src/operator/session-tools.ts +126 -0
- package/src/operator/types.ts +505 -0
- package/src/operator/view.ts +93 -42
- package/src/protocol/config.ts +14 -6
- package/src/protocol/conversation.ts +3 -0
- package/src/protocol/history.ts +2 -1
- package/src/protocol/index.ts +1 -0
- package/src/protocol/lifecycle.ts +21 -0
- package/src/protocol/origin.ts +47 -0
- package/src/protocol/reduce.ts +4 -0
- package/src/protocol/wire.ts +18 -0
- package/src/server/routes.ts +16 -5
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* References a model types: `id / [project "/"] workspace [":" chat]`. What
|
|
3
|
+
* the tools print is always explicit (`workspace:N`, or the session's id when
|
|
4
|
+
* the host numbers no chats), so a printed reference never moves to another
|
|
5
|
+
* chat.
|
|
6
|
+
*/
|
|
7
|
+
/** How the tools print an entry: `[project/]workspace:N`, or its id when it has no number. */
|
|
8
|
+
export function refOf(entry, all) {
|
|
9
|
+
if (entry.chat === undefined)
|
|
10
|
+
return entry.id;
|
|
11
|
+
const shared = all.some((other) => other.workspace.id !== entry.workspace.id &&
|
|
12
|
+
other.workspace.name.toLowerCase() === entry.workspace.name.toLowerCase());
|
|
13
|
+
const project = shared && entry.project ? `${entry.project.name}/` : "";
|
|
14
|
+
return `${project}${entry.workspace.name}:${entry.chat}`;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* The entry `ref` names among `all` (everything the caller may see, archived
|
|
18
|
+
* included). An id is exact: a session's, or a workspace's (its current
|
|
19
|
+
* chat). A name is case-insensitive; a bare workspace means its current chat,
|
|
20
|
+
* resolved now.
|
|
21
|
+
*/
|
|
22
|
+
export function resolve(ref, all) {
|
|
23
|
+
const wanted = ref.trim();
|
|
24
|
+
const byId = all.find((entry) => entry.id === wanted);
|
|
25
|
+
if (byId)
|
|
26
|
+
return { found: byId };
|
|
27
|
+
const inWorkspace = all.filter((entry) => entry.workspace.id === wanted);
|
|
28
|
+
if (inWorkspace.length)
|
|
29
|
+
return pick(inWorkspace, undefined);
|
|
30
|
+
const slash = wanted.lastIndexOf("/");
|
|
31
|
+
const project = slash >= 0 ? wanted.slice(0, slash).toLowerCase() : undefined;
|
|
32
|
+
const rest = slash >= 0 ? wanted.slice(slash + 1) : wanted;
|
|
33
|
+
const colon = rest.lastIndexOf(":");
|
|
34
|
+
const number = colon >= 0 ? Number(rest.slice(colon + 1)) : undefined;
|
|
35
|
+
const chat = number !== undefined && Number.isInteger(number) && number > 0 ? number : undefined;
|
|
36
|
+
const workspace = (chat !== undefined ? rest.slice(0, colon) : rest).toLowerCase();
|
|
37
|
+
const named = all.filter((entry) => entry.workspace.name.toLowerCase() === workspace &&
|
|
38
|
+
(project === undefined || entry.project?.name.toLowerCase() === project));
|
|
39
|
+
const workspaces = [...new Set(named.map((entry) => entry.workspace.id))];
|
|
40
|
+
if (workspaces.length > 1)
|
|
41
|
+
return {
|
|
42
|
+
ambiguous: workspaces.map((id) => (named.find((e) => e.workspace.id === id && e.current) ??
|
|
43
|
+
named.find((e) => e.workspace.id === id))),
|
|
44
|
+
};
|
|
45
|
+
if (!named.length)
|
|
46
|
+
return { missing: true };
|
|
47
|
+
return pick(named, chat);
|
|
48
|
+
}
|
|
49
|
+
/** In one workspace's entries: chat `chat`, else its current chat. */
|
|
50
|
+
function pick(entries, chat) {
|
|
51
|
+
if (chat !== undefined) {
|
|
52
|
+
const hit = entries.find((entry) => (entry.chat ?? 1) === chat);
|
|
53
|
+
return hit ? { found: hit } : { missing: true };
|
|
54
|
+
}
|
|
55
|
+
const current = entries.find((entry) => entry.current && entry.status !== "archived") ??
|
|
56
|
+
entries.find((entry) => entry.current) ??
|
|
57
|
+
(entries.length === 1 ? entries[0] : undefined);
|
|
58
|
+
return current ? { found: current } : { ambiguous: [...entries] };
|
|
59
|
+
}
|
package/dist/operator/render.js
CHANGED
|
@@ -6,7 +6,6 @@
|
|
|
6
6
|
* name alone is ambiguous.
|
|
7
7
|
*/
|
|
8
8
|
import { PLAN_OPTION_IDS } from "../protocol/plan.js";
|
|
9
|
-
import { isPlan, phaseOf, } from "./view.js";
|
|
10
9
|
const clip = (text, limit) => text.length > limit ? `${text.slice(0, limit - 1)}…` : text;
|
|
11
10
|
// @ref LLP 0014#untrusted-text — text an agent wrote or a repository holds never starts a line
|
|
12
11
|
// Untrusted text (what an agent or a person wrote, a question, an option, an
|
|
@@ -47,10 +46,16 @@ const textOf = (parts) => parts
|
|
|
47
46
|
* The next step, as the tool call that takes it:
|
|
48
47
|
* `-> answer_request(session: "voyager", answer: "<your answer>")`.
|
|
49
48
|
*/
|
|
49
|
+
// @ref LLP 0014#untrusted-text — every value a line names goes through `json`
|
|
50
|
+
/**
|
|
51
|
+
* A value as JSON on one line, losslessly: `\uXXXX` for what JSON leaves raw
|
|
52
|
+
* (U+2028, C1, bidi controls), so it reads back the same and breaks no line.
|
|
53
|
+
*/
|
|
54
|
+
export const json = (value) => JSON.stringify(value).replace(UNSAFE_IN_JSON, (found) => [...found].map((c) => `\\u${c.charCodeAt(0).toString(16).padStart(4, "0")}`).join(""));
|
|
50
55
|
export function callHint(tool, args = {}) {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
56
|
+
const named = Object.entries(args)
|
|
57
|
+
.filter(([, value]) => value !== undefined)
|
|
58
|
+
.map(([key, value]) => `${key}: ${json(value)}`);
|
|
54
59
|
return `-> ${tool}(${named.join(", ")})`;
|
|
55
60
|
}
|
|
56
61
|
/** A choice's labels on one line: `options: "USD", "EUR"`. */
|
|
@@ -226,38 +231,61 @@ function aboutLines(toolCall, root) {
|
|
|
226
231
|
.join("");
|
|
227
232
|
}
|
|
228
233
|
/**
|
|
229
|
-
*
|
|
230
|
-
*
|
|
231
|
-
|
|
234
|
+
* One request's context: named by the broker's own id when more than one
|
|
235
|
+
* waits, and offered the answer only when this caller may give that one.
|
|
236
|
+
*/
|
|
237
|
+
export function perRequest(context, waiting) {
|
|
238
|
+
const id = waiting.request.requestId;
|
|
239
|
+
return {
|
|
240
|
+
...context,
|
|
241
|
+
// Its id in every hint, one request or many: a hint followed late cannot reach a replacement.
|
|
242
|
+
handle: id,
|
|
243
|
+
...(context.answerable ? { canAnswer: context.answerable.has(id) } : {}),
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* What the agent waits on, and the call that answers it, when that tool is
|
|
248
|
+
* installed. What only the person can answer (`personOnly`) is one line, with
|
|
249
|
+
* no call.
|
|
232
250
|
*/
|
|
233
|
-
export function describeWaiting(waiting,
|
|
251
|
+
export function describeWaiting(waiting, context) {
|
|
252
|
+
const { ref, handle, root = "", hint } = context;
|
|
234
253
|
const only = waiting.kind === "question" && personOnly(waiting);
|
|
235
254
|
if (only) {
|
|
236
255
|
return `only the person can answer this, where the session runs (${only}): ${quoted(clip(waiting.request.message, 300))}`;
|
|
237
256
|
}
|
|
238
|
-
const
|
|
257
|
+
const named = handle ? ` (request ${quoted(handle)})` : "";
|
|
258
|
+
const answer = (text) => {
|
|
259
|
+
if (context.canAnswer === false)
|
|
260
|
+
return "";
|
|
261
|
+
const call = hint("answer_request", { session: ref, request: handle, answer: text });
|
|
262
|
+
return call ? `\n ${call}` : "";
|
|
263
|
+
};
|
|
239
264
|
const options = (labels, ownWords = false) => labels.length ? `\n${optionsLine(labels)}${ownWords ? " (or your own words)" : ""}` : "";
|
|
240
265
|
if (waiting.kind === "question") {
|
|
241
266
|
const questions = waiting.request.questions ?? [];
|
|
242
267
|
const asked = questions.length
|
|
243
268
|
? questions
|
|
244
|
-
.map((q) => `${block(
|
|
269
|
+
.map((q) => `${block(`question${named}:`, q.question)}${options((q.options ?? []).map((o) => o.label), true)}`)
|
|
245
270
|
.join("\n")
|
|
246
|
-
: block(
|
|
271
|
+
: block(`question${named}:`, waiting.request.message);
|
|
247
272
|
const form = questions.length > 1 ? "<one answer per question, separated by ; >" : "<your answer>";
|
|
248
|
-
return `${asked}
|
|
273
|
+
return `${asked}${answer(form)}`;
|
|
249
274
|
}
|
|
250
|
-
if (
|
|
251
|
-
const approve =
|
|
275
|
+
if (waiting.kind === "plan") {
|
|
276
|
+
const approve = answer("approve");
|
|
277
|
+
const or = approve ? ` (or answer: "<what to change>")` : "";
|
|
252
278
|
// Never approved unseen: the plan itself, or the call that shows it.
|
|
253
279
|
const plan = waiting.request.toolCall?.rawInput?.plan;
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
280
|
+
if (typeof plan === "string" && plan.trim())
|
|
281
|
+
return `plan waiting for approval${named}:\n${quote(clip(plan, 800))}${approve}${or}`;
|
|
282
|
+
const read = hint("read_session", { session: ref });
|
|
283
|
+
return `plan waiting for approval${named}; read it before answering${read ? `\n ${read}` : ""}${approve}${or}`;
|
|
257
284
|
}
|
|
258
|
-
const
|
|
285
|
+
const choices = waiting.request.options.map((option) => option.name);
|
|
259
286
|
const about = aboutLines(waiting.request.toolCall, root);
|
|
260
|
-
|
|
287
|
+
const allow = answer("allow");
|
|
288
|
+
return `${block(`permission${named}:`, waiting.request.title)}${about}${options(choices)}${allow}${allow ? ` (or answer: "deny")` : ""}`;
|
|
261
289
|
}
|
|
262
290
|
/** The agent's words since the person last spoke. */
|
|
263
291
|
export function lastReply(messages) {
|
|
@@ -272,80 +300,149 @@ export function lastReply(messages) {
|
|
|
272
300
|
.filter(Boolean)
|
|
273
301
|
.join("\n");
|
|
274
302
|
}
|
|
303
|
+
/** Every message of a view, oldest first. */
|
|
304
|
+
export const messagesOf = (view) => view.turns.flatMap((turn) => turn.messages);
|
|
305
|
+
/** How a status reads in an outcome. */
|
|
306
|
+
const STATUS_WORDS = {
|
|
307
|
+
idle: "done",
|
|
308
|
+
stopped: "stopped",
|
|
309
|
+
error: "error",
|
|
310
|
+
needs_input: "needs_input",
|
|
311
|
+
archived: "archived",
|
|
312
|
+
};
|
|
275
313
|
/**
|
|
276
|
-
* How a
|
|
277
|
-
*
|
|
278
|
-
* facts for a turn that is not working any more; paths under `root` in what it
|
|
279
|
-
* waits on read relative to it.
|
|
314
|
+
* How a session stands: done, stopped, waiting on someone, failed, or still
|
|
315
|
+
* going. Each request it waits on is said with the call that answers it.
|
|
280
316
|
*/
|
|
281
|
-
export function renderOutcome(view,
|
|
282
|
-
const
|
|
283
|
-
if (
|
|
284
|
-
|
|
317
|
+
export function renderOutcome(view, context) {
|
|
318
|
+
const { ref, hint } = context;
|
|
319
|
+
if (view.status === "working" || view.status === "setting_up") {
|
|
320
|
+
const messages = messagesOf(view);
|
|
285
321
|
const detail = [
|
|
286
|
-
view.
|
|
287
|
-
|
|
322
|
+
view.status === "setting_up"
|
|
323
|
+
? "being set up"
|
|
324
|
+
: messages.length
|
|
325
|
+
? ""
|
|
326
|
+
: "starting, no messages yet",
|
|
327
|
+
context.waited ? `still running after ${context.waited}s` : "",
|
|
288
328
|
].filter(Boolean);
|
|
289
|
-
|
|
329
|
+
const next = hint("wait_for_sessions", { sessions: [ref] });
|
|
330
|
+
return `status: working${detail.length ? ` (${detail.join("; ")})` : ""}${next ? `\n ${next}` : ""}`;
|
|
290
331
|
}
|
|
291
|
-
const
|
|
292
|
-
const
|
|
293
|
-
const
|
|
332
|
+
const messages = messagesOf(view);
|
|
333
|
+
const fresh = view.status === "idle" && !messages.length;
|
|
334
|
+
const lines = [
|
|
335
|
+
`status: ${fresh ? "idle (no messages yet)" : (STATUS_WORDS[view.status] ?? view.status)}`,
|
|
336
|
+
];
|
|
337
|
+
const reply = lastReply(messages);
|
|
294
338
|
if (reply)
|
|
295
339
|
lines.push(block("agent said:", clip(reply, 1500)));
|
|
296
340
|
for (const waiting of view.waiting)
|
|
297
|
-
lines.push(describeWaiting(waiting,
|
|
298
|
-
if (
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
if (
|
|
303
|
-
lines.push(
|
|
341
|
+
lines.push(describeWaiting(waiting, perRequest(context, waiting)));
|
|
342
|
+
if (view.status === "error") {
|
|
343
|
+
const failed = view.turns.at(-1)?.error;
|
|
344
|
+
lines.push(block("error:", failed ?? "the turn failed"));
|
|
345
|
+
}
|
|
346
|
+
if (context.note)
|
|
347
|
+
lines.push(oneLine(context.note));
|
|
348
|
+
if (view.status === "error" || reply.length > 1500) {
|
|
349
|
+
const read = hint("read_session", { session: ref });
|
|
350
|
+
if (read)
|
|
351
|
+
lines.push(` ${read}`);
|
|
304
352
|
}
|
|
305
353
|
return lines.join("\n");
|
|
306
354
|
}
|
|
355
|
+
/** Who a user message is from, as `read_session` labels it; `you` is the caller's own origin. */
|
|
356
|
+
export function senderLabel(origin, you) {
|
|
357
|
+
if (!origin || origin.kind === "person")
|
|
358
|
+
return you.kind === "person" ? "[you]" : "[the person]";
|
|
359
|
+
if (sameOrigin(origin, you))
|
|
360
|
+
return "[you]";
|
|
361
|
+
if (origin.kind === "session") {
|
|
362
|
+
const { name, sessionId } = origin;
|
|
363
|
+
return `[${quoted(name ?? sessionId)}]`;
|
|
364
|
+
}
|
|
365
|
+
if (origin.kind === "assistant")
|
|
366
|
+
return `[assistant ${quoted(String(origin.name))}]`;
|
|
367
|
+
return `[${quoted(origin.kind)}]`;
|
|
368
|
+
}
|
|
369
|
+
/** Whether two origins are the same sender. */
|
|
370
|
+
export function sameOrigin(a, b) {
|
|
371
|
+
const key = (o) => !o || o.kind === "person"
|
|
372
|
+
? "person"
|
|
373
|
+
: o.kind === "session"
|
|
374
|
+
? `session:${o.sessionId}`
|
|
375
|
+
: o.kind === "assistant"
|
|
376
|
+
? `assistant:${o.id}`
|
|
377
|
+
: JSON.stringify(o);
|
|
378
|
+
return key(a) === key(b);
|
|
379
|
+
}
|
|
380
|
+
/** How a turn that is over ended, after its messages. */
|
|
381
|
+
function endOf(turn, you) {
|
|
382
|
+
if (turn.outcome === "running")
|
|
383
|
+
return undefined;
|
|
384
|
+
if (turn.outcome === "done")
|
|
385
|
+
return "— turn done";
|
|
386
|
+
if (turn.outcome === "cancelled") {
|
|
387
|
+
const by = turn.cancelledBy ? ` by ${senderLabel(turn.cancelledBy, you).slice(1, -1)}` : "";
|
|
388
|
+
return `— turn cancelled${by}`;
|
|
389
|
+
}
|
|
390
|
+
return block("— turn failed:", turn.error ?? "the turn failed");
|
|
391
|
+
}
|
|
307
392
|
/**
|
|
308
|
-
* The conversation, condensed:
|
|
309
|
-
* tools it ran
|
|
310
|
-
*
|
|
393
|
+
* The conversation, condensed: each user message under its own sender, what
|
|
394
|
+
* the agent said, which tools it ran (with what a call asked the person),
|
|
395
|
+
* how each turn ended, and what the session waits on.
|
|
311
396
|
*/
|
|
312
|
-
export function renderConversation(view,
|
|
313
|
-
const
|
|
314
|
-
|
|
315
|
-
const
|
|
316
|
-
|
|
317
|
-
const
|
|
318
|
-
for (
|
|
319
|
-
const
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
397
|
+
export function renderConversation(view, options) {
|
|
398
|
+
const { root, you } = options;
|
|
399
|
+
const lines = options.header ? [options.header] : [];
|
|
400
|
+
const limit = options.all ? Number.POSITIVE_INFINITY : Math.max(1, options.last ?? 10);
|
|
401
|
+
let count = 0;
|
|
402
|
+
const shownTurns = [];
|
|
403
|
+
for (let t = view.turns.length - 1; t >= 0 && count < limit; t--) {
|
|
404
|
+
const turn = view.turns[t];
|
|
405
|
+
const messages = turn.messages.slice(-(limit - count));
|
|
406
|
+
count += messages.length;
|
|
407
|
+
shownTurns.unshift({ turn, messages });
|
|
408
|
+
}
|
|
409
|
+
const working = view.status === "working" || view.status === "needs_input";
|
|
410
|
+
for (const { turn, messages } of shownTurns) {
|
|
411
|
+
for (const message of messages) {
|
|
412
|
+
const text = textOf(message.parts);
|
|
413
|
+
if (message.role === "user") {
|
|
414
|
+
lines.push(block(senderLabel(message.origin, you), clip(text, 800)));
|
|
327
415
|
continue;
|
|
328
|
-
lines.push(` · ${line}`);
|
|
329
|
-
// Under the call's line, aligned with its name.
|
|
330
|
-
for (const asked of askedLines(part, options.root)) {
|
|
331
|
-
lines.push(asked.replace(/^/gm, " "));
|
|
332
416
|
}
|
|
417
|
+
for (const part of message.parts) {
|
|
418
|
+
const line = toolLine(part, root, working && turn.outcome === "running");
|
|
419
|
+
if (!line)
|
|
420
|
+
continue;
|
|
421
|
+
lines.push(` · ${line}`);
|
|
422
|
+
for (const asked of askedLines(part, root))
|
|
423
|
+
lines.push(asked.replace(/^/gm, " "));
|
|
424
|
+
}
|
|
425
|
+
if (text)
|
|
426
|
+
lines.push(block("[agent]", clip(text, 1200)));
|
|
333
427
|
}
|
|
334
|
-
|
|
335
|
-
|
|
428
|
+
const end = endOf(turn, you);
|
|
429
|
+
if (end)
|
|
430
|
+
lines.push(end);
|
|
336
431
|
}
|
|
337
|
-
if (!
|
|
432
|
+
if (!count && !view.turns.length)
|
|
338
433
|
lines.push("(no messages yet)");
|
|
339
|
-
|
|
340
|
-
lines.push(`(status: ${phaseOf(view)})`);
|
|
341
|
-
if (view.error && phaseOf(view) === "error")
|
|
342
|
-
lines.push(block("error:", view.error));
|
|
434
|
+
lines.push(`(status: ${view.status})`);
|
|
343
435
|
for (const waiting of view.waiting)
|
|
344
|
-
lines.push(describeWaiting(waiting,
|
|
345
|
-
|
|
346
|
-
|
|
436
|
+
lines.push(describeWaiting(waiting, perRequest(options, waiting)));
|
|
437
|
+
const total = view.turns.reduce((sum, turn) => sum + turn.messages.length, 0);
|
|
438
|
+
if (view.older || count < total) {
|
|
439
|
+
const more = options.hint("read_session", {
|
|
440
|
+
session: options.ref,
|
|
441
|
+
last: Math.min(count + 10, MAX_LAST),
|
|
442
|
+
});
|
|
443
|
+
lines.push(count >= MAX_LAST || !more
|
|
347
444
|
? `older messages exist; read_session shows at most the last ${MAX_LAST}.`
|
|
348
|
-
: `older messages exist: ${
|
|
445
|
+
: `older messages exist: ${more}`);
|
|
349
446
|
}
|
|
350
447
|
return lines.join("\n");
|
|
351
448
|
}
|
|
@@ -391,7 +488,7 @@ export function answerFor(waiting, text) {
|
|
|
391
488
|
requestId,
|
|
392
489
|
outcome: { outcome: "selected", optionId, ...(message ? { message } : {}) },
|
|
393
490
|
});
|
|
394
|
-
if (
|
|
491
|
+
if (waiting.kind === "plan") {
|
|
395
492
|
// Approving starts the work with edits still asked for; anything else is what to change.
|
|
396
493
|
return YES.test(said)
|
|
397
494
|
? pick(PLAN_OPTION_IDS.approve)
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The operator as a session's own tools: what a host returns from the
|
|
3
|
+
* engine's `hostTools` (LLP 0018 (host tools)), its other tools for the
|
|
4
|
+
* session in the same one source. The shapes are the engine's, said here by
|
|
5
|
+
* structure: the operator imports nothing of the engine's core.
|
|
6
|
+
*/
|
|
7
|
+
// @ref LLP 0019#guidance — the paragraph a host adds to its sessions' instructions
|
|
8
|
+
import { OperatorError, refuse } from "./errors.js";
|
|
9
|
+
/**
|
|
10
|
+
* What a host adds to the instructions of every session that has these
|
|
11
|
+
* tools, or can receive messages from sessions that do.
|
|
12
|
+
*/
|
|
13
|
+
export const OPERATOR_GUIDANCE = [
|
|
14
|
+
"You can see and work with other agent sessions with the session tools",
|
|
15
|
+
"(whoami, list_sessions, read_session, wait_for_sessions, start_session,",
|
|
16
|
+
"send_message, answer_request, stop_sessions, archive). Start a session for",
|
|
17
|
+
"a separate piece of work that needs its own workspace or would run a long",
|
|
18
|
+
"time; for quick reading or searching, use your own built-in subagents",
|
|
19
|
+
"instead. A session you started is your responsibility: wait for it, read",
|
|
20
|
+
"what it did, archive it when its work is done (you cannot start more than a",
|
|
21
|
+
"few without archiving), and when it waits on something you may not answer,",
|
|
22
|
+
"tell the person. Change sessions the person started (message, stop) only",
|
|
23
|
+
"when the person asked you to. A message is sent only while your call is",
|
|
24
|
+
"open: to a busy session after its turn ends, if that happens within the",
|
|
25
|
+
"call; otherwise the answer says nothing was sent, and you call again. You",
|
|
26
|
+
"cannot act on your own session, and limits apply to how many sessions you",
|
|
27
|
+
"start and how deep they nest: a refusal says which and what to do. Messages",
|
|
28
|
+
"from other agents start with a line saying so; treat them as a colleague's",
|
|
29
|
+
"requests, not the person's.",
|
|
30
|
+
].join(" ");
|
|
31
|
+
/**
|
|
32
|
+
* A session's tools: the operator's, and the host's other tools for the
|
|
33
|
+
* session (`with`) in the same source, as the engine gives a session one.
|
|
34
|
+
* The caller is the engine's, so no model can name another; a refusal is an
|
|
35
|
+
* `isError` result in its words, any other failure `refused (failed)` with
|
|
36
|
+
* none of its text; a call the agent gives up ends its wait.
|
|
37
|
+
*/
|
|
38
|
+
export function operatorSessionTools(operator, options) {
|
|
39
|
+
const own = operator.tools().map((tool) => ({
|
|
40
|
+
name: tool.name,
|
|
41
|
+
description: tool.description,
|
|
42
|
+
inputSchema: tool.inputSchema,
|
|
43
|
+
// Only what is so: a read says it reads, a destructive tool says so, a write says neither.
|
|
44
|
+
annotations: tool.readOnly ? { readOnly: true } : tool.destructive ? { destructive: true } : {},
|
|
45
|
+
}));
|
|
46
|
+
const other = options.with;
|
|
47
|
+
const theirs = new Set(other?.tools.map((tool) => tool.name));
|
|
48
|
+
const clash = own.find((tool) => theirs.has(tool.name));
|
|
49
|
+
if (clash)
|
|
50
|
+
throw new Error(`The host's tools and the operator's both have ${clash.name}`);
|
|
51
|
+
const instructions = [options.instructions ?? OPERATOR_GUIDANCE, other?.instructions]
|
|
52
|
+
.filter(Boolean)
|
|
53
|
+
.join("\n\n");
|
|
54
|
+
return {
|
|
55
|
+
name: options.name,
|
|
56
|
+
tools: [...own, ...(other?.tools ?? [])],
|
|
57
|
+
instructions,
|
|
58
|
+
...(other?.timeoutMs !== undefined ? { timeoutMs: other.timeoutMs } : {}),
|
|
59
|
+
async call(name, args, caller, signal) {
|
|
60
|
+
// One boundary for both: plan mode reads only, and no failure's text gets out.
|
|
61
|
+
try {
|
|
62
|
+
// A call the agent gave up runs nothing.
|
|
63
|
+
if (signal.aborted)
|
|
64
|
+
throw refuse.unsupported("The call was given up before it ran. Nothing was done.");
|
|
65
|
+
if (other && theirs.has(name)) {
|
|
66
|
+
const writes = !other.tools.find((t) => t.name === name)?.annotations?.readOnly;
|
|
67
|
+
if (writes && caller.permissionMode === "plan")
|
|
68
|
+
throw refuse.readOnly();
|
|
69
|
+
return await other.call(name, args, caller, signal);
|
|
70
|
+
}
|
|
71
|
+
const text = await operator.call(name, args, {
|
|
72
|
+
kind: "session",
|
|
73
|
+
sessionId: caller.sessionId,
|
|
74
|
+
turnId: caller.turnId,
|
|
75
|
+
harness: caller.harness,
|
|
76
|
+
permissionMode: caller.permissionMode,
|
|
77
|
+
signal,
|
|
78
|
+
});
|
|
79
|
+
return { content: [{ type: "text", text }] };
|
|
80
|
+
}
|
|
81
|
+
catch (error) {
|
|
82
|
+
const said = error instanceof OperatorError ? error.message : refuse.failed().message;
|
|
83
|
+
return { content: [{ type: "text", text: said }], isError: true };
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The operator's public types (LLP 0019): who calls, what a session is as the
|
|
3
|
+
* tools report it, the host the product implements, and the refusals.
|
|
4
|
+
* `Origin` is the protocol's (it rides `turn/start` and `turn/steer`).
|
|
5
|
+
*/
|
|
6
|
+
// ---- refusals -------------------------------------------------------------------
|
|
7
|
+
/** Every refusal the tools give, and nothing else (LLP 0019, Errors). */
|
|
8
|
+
export const OPERATOR_ERROR_CODES = [
|
|
9
|
+
"invalid_input",
|
|
10
|
+
"not_found",
|
|
11
|
+
"ambiguous",
|
|
12
|
+
"archived",
|
|
13
|
+
"self",
|
|
14
|
+
"read_only",
|
|
15
|
+
"access",
|
|
16
|
+
"not_yours",
|
|
17
|
+
"has_children",
|
|
18
|
+
"busy",
|
|
19
|
+
"not_waiting",
|
|
20
|
+
"person_only",
|
|
21
|
+
"setup_failed",
|
|
22
|
+
"limit_depth",
|
|
23
|
+
"limit_sessions",
|
|
24
|
+
"limit_setting_up",
|
|
25
|
+
"limit_rate",
|
|
26
|
+
"limit_resources",
|
|
27
|
+
"stale_cursor",
|
|
28
|
+
"unsupported",
|
|
29
|
+
"failed",
|
|
30
|
+
];
|
package/dist/operator/view.js
CHANGED
|
@@ -1,35 +1,90 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* What
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* the same shape directly. Nothing here knows about projects, workspaces,
|
|
7
|
-
* accounts or storage: those are the host's.
|
|
2
|
+
* What the operator needs to know about one session, however its host keeps
|
|
3
|
+
* it. A host that holds a folded `ConversationState` gets it with `viewOf`; a
|
|
4
|
+
* host that keeps its own rows builds the same shape. Nothing here knows about
|
|
5
|
+
* projects, workspaces, accounts or storage: those are the host's.
|
|
8
6
|
*/
|
|
9
7
|
import { conversationMessages, pendingElicitations, pendingPermissions, } from "../protocol/conversation.js";
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
// @ref LLP 0019#answer_request-write — a plan only when the engine says so; anything else is a permission
|
|
9
|
+
/** A decision's kind, from its `purpose`: `plan` when it says so, else a permission. */
|
|
10
|
+
export function waitingOf(request) {
|
|
11
|
+
return { kind: request.purpose === "plan" ? "plan" : "permission", request };
|
|
13
12
|
}
|
|
14
|
-
/**
|
|
15
|
-
export
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
13
|
+
/** Whether a status needs no more waiting: a wait returns it. */
|
|
14
|
+
export const settledStatus = (status) => status !== "working" && status !== "setting_up";
|
|
15
|
+
/**
|
|
16
|
+
* The view of a session whose events were folded with the engine's reducer,
|
|
17
|
+
* with its turn facts (`running`, `lastEnded`) from every turn, not a page.
|
|
18
|
+
*/
|
|
19
19
|
export function viewOf(state) {
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
return viewWithFacts(state);
|
|
21
|
+
}
|
|
22
|
+
/** One turn of a folded state, by id: the host's `turn`, for a host that folds. */
|
|
23
|
+
export function turnOf(state, turnId) {
|
|
24
|
+
const turn = state.turns.find((t) => t.turnId === turnId);
|
|
25
|
+
if (!turn)
|
|
26
|
+
return { state: "unknown" };
|
|
27
|
+
if (turn.status === "active")
|
|
28
|
+
return { state: "running" };
|
|
29
|
+
return {
|
|
30
|
+
state: "ended",
|
|
31
|
+
outcome: turn.error ? "failed" : turn.stopReason === "cancelled" ? "cancelled" : "done",
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
function viewWithFacts(state) {
|
|
35
|
+
const messages = conversationMessages(state);
|
|
36
|
+
const waiting = [
|
|
37
|
+
...pendingElicitations(state).map((request) => ({ kind: "question", request })),
|
|
38
|
+
...pendingPermissions(state).map(waitingOf),
|
|
39
|
+
];
|
|
40
|
+
const turns = state.turns.map((turn) => {
|
|
41
|
+
const outcome = turn.status === "active"
|
|
42
|
+
? "running"
|
|
43
|
+
: turn.error
|
|
44
|
+
? "failed"
|
|
45
|
+
: turn.stopReason === "cancelled"
|
|
46
|
+
? "cancelled"
|
|
47
|
+
: "done";
|
|
48
|
+
return {
|
|
49
|
+
turnId: turn.turnId,
|
|
50
|
+
outcome,
|
|
51
|
+
...(turn.error ? { error: turn.error.message } : {}),
|
|
52
|
+
messages: messages
|
|
53
|
+
.filter((message) => message.turnId === turn.turnId)
|
|
54
|
+
.map((message) => ({
|
|
55
|
+
role: message.role,
|
|
56
|
+
parts: message.parts,
|
|
57
|
+
...(message.parentToolCallId ? { nested: true } : {}),
|
|
58
|
+
...(message.origin ? { origin: message.origin } : {}),
|
|
59
|
+
})),
|
|
60
|
+
startedAt: turn.startedAt,
|
|
61
|
+
...(turn.endedAt !== undefined ? { endedAt: turn.endedAt } : {}),
|
|
62
|
+
};
|
|
63
|
+
});
|
|
64
|
+
const last = turns.at(-1)?.outcome;
|
|
65
|
+
const status = waiting.length
|
|
66
|
+
? "needs_input"
|
|
67
|
+
: last === "running"
|
|
68
|
+
? "working"
|
|
69
|
+
: last === "failed"
|
|
70
|
+
? "error"
|
|
71
|
+
: last === "cancelled"
|
|
72
|
+
? "stopped"
|
|
73
|
+
: "idle";
|
|
74
|
+
const running = turns.find((t) => t.outcome === "running")?.turnId;
|
|
75
|
+
const ended = [...turns].reverse().find((t) => t.outcome !== "running");
|
|
22
76
|
return {
|
|
23
|
-
status
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
77
|
+
status,
|
|
78
|
+
turns,
|
|
79
|
+
waiting,
|
|
80
|
+
...(running ? { running } : {}),
|
|
81
|
+
...(ended
|
|
82
|
+
? {
|
|
83
|
+
lastEnded: {
|
|
84
|
+
turnId: ended.turnId,
|
|
85
|
+
outcome: ended.outcome,
|
|
86
|
+
},
|
|
87
|
+
}
|
|
88
|
+
: {}),
|
|
34
89
|
};
|
|
35
90
|
}
|
package/dist/protocol/config.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import { AgentHarnessSchema } from "./harness.js";
|
|
3
|
+
import { OriginSchema, agentOrigin } from "./origin.js";
|
|
3
4
|
import { AgentInputSchema } from "./part-input.js";
|
|
4
5
|
import { ThinkingLevelSchema } from "./thinking.js";
|
|
5
6
|
import { openVocabulary } from "./vocabulary.js";
|
|
@@ -144,14 +145,20 @@ export const RunRequestSchema = z.object({
|
|
|
144
145
|
turnId: z.string(),
|
|
145
146
|
input: AgentInputSchema,
|
|
146
147
|
config: RunConfigSchema,
|
|
148
|
+
/** Who sent the input; absent is the person (`OriginSchema`). */
|
|
149
|
+
origin: OriginSchema.optional(),
|
|
147
150
|
});
|
|
151
|
+
// @ref LLP 0019#origin-on-the-wire-and-to-the-model — another sender is another turn
|
|
148
152
|
/**
|
|
149
|
-
* What makes a turn the same turn when a client sends its ids again: its
|
|
150
|
-
* and the config that shapes what the agent does. What a
|
|
151
|
-
* can change without making it another turn stays out:
|
|
152
|
-
* per-session URL), the environment, the API key, and
|
|
153
|
+
* What makes a turn the same turn when a client sends its ids again: its
|
|
154
|
+
* input, who sent it, and the config that shapes what the agent does. What a
|
|
155
|
+
* client's own restart can change without making it another turn stays out:
|
|
156
|
+
* MCP endpoints (a fresh per-session URL), the environment, the API key, and
|
|
157
|
+
* raw passthrough. The person's origin, said or not, adds nothing, so a
|
|
158
|
+
* request's identity is what it was before origins existed.
|
|
153
159
|
*/
|
|
154
160
|
export function turnIdentity(request) {
|
|
155
161
|
const { mcpServers: _mcp, env: _env, apiKey: _key, includeRaw: _raw, ...config } = request.config;
|
|
156
|
-
|
|
162
|
+
const origin = agentOrigin(request.origin);
|
|
163
|
+
return { input: request.input, config, ...(origin && { origin }) };
|
|
157
164
|
}
|