@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
|
@@ -10,11 +10,17 @@ import type {
|
|
|
10
10
|
HarnessCliProgress,
|
|
11
11
|
ModelList,
|
|
12
12
|
ModelsListParams,
|
|
13
|
+
Origin,
|
|
13
14
|
PermissionOutcome,
|
|
14
15
|
RunRequest,
|
|
15
16
|
TurnSteerResult,
|
|
16
17
|
} from "../../protocol/index.ts";
|
|
17
|
-
import {
|
|
18
|
+
import {
|
|
19
|
+
RunRequestSchema,
|
|
20
|
+
agentOrigin,
|
|
21
|
+
canonicalJson,
|
|
22
|
+
turnIdentity,
|
|
23
|
+
} from "../../protocol/index.ts";
|
|
18
24
|
import type { CancelResult, SteerRequest } from "../agents/base.ts";
|
|
19
25
|
import type { AgentRegistry } from "../agents/registry.ts";
|
|
20
26
|
import type { DiagnosticHandler } from "../diagnostics.ts";
|
|
@@ -26,6 +32,7 @@ import { type EventSink, EventSinkError } from "./event-sink.ts";
|
|
|
26
32
|
import { awaitingInstall, harnessCliProgress, harnessClis } from "./harness-clis.ts";
|
|
27
33
|
import { HarnessLogins } from "./harness-logins.ts";
|
|
28
34
|
import { ModelLists, catalogModelList } from "./model-lists.ts";
|
|
35
|
+
import { withOriginLine } from "./origin-line.ts";
|
|
29
36
|
import { StandingRules } from "./permission-policy.ts";
|
|
30
37
|
import type { RequestAnswers, RequestKind } from "./request-broker.ts";
|
|
31
38
|
import {
|
|
@@ -370,12 +377,19 @@ export class AgentRuntime {
|
|
|
370
377
|
* ended; the same id with other input throws `TurnConflictError`. Input the
|
|
371
378
|
* harness cannot take throws, as it would fail a turn.
|
|
372
379
|
*/
|
|
373
|
-
async steer(
|
|
374
|
-
|
|
380
|
+
async steer(
|
|
381
|
+
sessionId: string,
|
|
382
|
+
request: SteerRequest & { origin?: Origin },
|
|
383
|
+
): Promise<TurnSteerResult> {
|
|
384
|
+
const { turnId, steerId, input, origin: sent } = request;
|
|
375
385
|
if (typeof input === "string" ? input.trim() === "" : input.length === 0) {
|
|
376
386
|
throw new Error("a steer carries input");
|
|
377
387
|
}
|
|
378
|
-
|
|
388
|
+
// Another sender is another steer; the person's origin, said or not, adds nothing.
|
|
389
|
+
// Its own copy, taken at admission: the caller's object may change after.
|
|
390
|
+
const given = agentOrigin(sent);
|
|
391
|
+
const origin = given && { ...given };
|
|
392
|
+
const inputKey = canonicalJson(origin ? { input, origin } : input);
|
|
379
393
|
const turn = this.turns.get(turnKeyOf(sessionId, turnId));
|
|
380
394
|
const known = turn
|
|
381
395
|
? turn.steers.find(steerId)
|
|
@@ -395,9 +409,19 @@ export class AgentRuntime {
|
|
|
395
409
|
const agent = this.registry.getAgent(turn.harness);
|
|
396
410
|
const deliver = agent.capabilities.steering ? agent.steer?.bind(agent) : undefined;
|
|
397
411
|
if (!deliver) return { outcome: "unsupported" };
|
|
398
|
-
// Its echo is the caller's input; the agent is given it well-formed
|
|
399
|
-
|
|
400
|
-
|
|
412
|
+
// Its echo is the caller's input; the agent is given it well-formed, after
|
|
413
|
+
// the sender's line when it is not the person.
|
|
414
|
+
return await turn.steers.accept(
|
|
415
|
+
steerId,
|
|
416
|
+
inputKey,
|
|
417
|
+
input,
|
|
418
|
+
() =>
|
|
419
|
+
deliver(sessionId, {
|
|
420
|
+
turnId,
|
|
421
|
+
steerId,
|
|
422
|
+
input: wellFormedInput(withOriginLine(input, origin)),
|
|
423
|
+
}),
|
|
424
|
+
origin,
|
|
401
425
|
);
|
|
402
426
|
}
|
|
403
427
|
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {
|
|
2
|
+
agentOrigin,
|
|
2
3
|
createSteerEchoParts,
|
|
3
4
|
createUserEchoParts,
|
|
4
5
|
echoMessageId,
|
|
@@ -10,6 +11,7 @@ import type {
|
|
|
10
11
|
AgentInput,
|
|
11
12
|
Delta,
|
|
12
13
|
LifecycleEvent,
|
|
14
|
+
Origin,
|
|
13
15
|
Part,
|
|
14
16
|
TurnEndedEvent,
|
|
15
17
|
TurnExecution,
|
|
@@ -129,8 +131,8 @@ export class EventProcessor {
|
|
|
129
131
|
* id (`echoMessageId`), so a consumer that minted the turnId can predict the
|
|
130
132
|
* whole echo instead of reconciling a look-alike against it.
|
|
131
133
|
*/
|
|
132
|
-
*echoUserMessage(input: AgentInput): Generator<LifecycleEvent> {
|
|
133
|
-
yield* this.openMessage("user", undefined, echoMessageId(this.turnId));
|
|
134
|
+
*echoUserMessage(input: AgentInput, origin?: Origin): Generator<LifecycleEvent> {
|
|
135
|
+
yield* this.openMessage("user", undefined, echoMessageId(this.turnId), undefined, origin);
|
|
134
136
|
for (const part of createUserEchoParts(input, this.turnId)) {
|
|
135
137
|
yield* this.emitPart(part);
|
|
136
138
|
}
|
|
@@ -145,10 +147,10 @@ export class EventProcessor {
|
|
|
145
147
|
* already echoed, is ignored.
|
|
146
148
|
*/
|
|
147
149
|
private *echoSteer(steerId: string): Generator<LifecycleEvent> {
|
|
148
|
-
const
|
|
149
|
-
if (
|
|
150
|
-
yield* this.openMessage("user", undefined, steerMessageId(steerId));
|
|
151
|
-
for (const part of createSteerEchoParts(input, steerId)) {
|
|
150
|
+
const steer = this.steers.read(steerId);
|
|
151
|
+
if (steer === undefined) return;
|
|
152
|
+
yield* this.openMessage("user", undefined, steerMessageId(steerId), undefined, steer.origin);
|
|
153
|
+
for (const part of createSteerEchoParts(steer.input, steerId)) {
|
|
152
154
|
yield* this.emitPart(part);
|
|
153
155
|
}
|
|
154
156
|
yield* this.closeMessage();
|
|
@@ -320,6 +322,8 @@ export class EventProcessor {
|
|
|
320
322
|
id?: string,
|
|
321
323
|
/** The model the harness said produced it. */
|
|
322
324
|
model?: string,
|
|
325
|
+
/** A user message's sender, when not the person. */
|
|
326
|
+
origin?: Origin,
|
|
323
327
|
): Generator<LifecycleEvent, string> {
|
|
324
328
|
if (this.openMessageId) yield* this.closeMessage();
|
|
325
329
|
if (role === "assistant") this.answered = true;
|
|
@@ -327,6 +331,7 @@ export class EventProcessor {
|
|
|
327
331
|
// with one that dates it by this event.
|
|
328
332
|
const startedAt = Date.now();
|
|
329
333
|
const messageId = id ?? generateUUIDv7(startedAt);
|
|
334
|
+
const sender = agentOrigin(origin);
|
|
330
335
|
this.openMessageId = messageId;
|
|
331
336
|
this.openMessageParent = parentToolCallId;
|
|
332
337
|
const named = role === "assistant" ? this.messageModel(model, parentToolCallId) : {};
|
|
@@ -348,6 +353,8 @@ export class EventProcessor {
|
|
|
348
353
|
// the model the harness said wrote it (Claude's API model), else the
|
|
349
354
|
// one it runs the turn on (Codex's thread model), else the request's.
|
|
350
355
|
...named,
|
|
356
|
+
// A copy: what a sink does to the event never reaches the request or the model's line.
|
|
357
|
+
...(role === "user" && sender && { origin: { ...sender } }),
|
|
351
358
|
timestamp: startedAt,
|
|
352
359
|
};
|
|
353
360
|
return messageId;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { type AgentInput, type Origin, agentOrigin } from "../../protocol/index.ts";
|
|
2
|
+
|
|
3
|
+
// @ref LLP 0019#origin-on-the-wire-and-to-the-model — the line is the engine's, first, and only for an agent
|
|
4
|
+
/**
|
|
5
|
+
* The one line an agent is given before a message that is not the person's,
|
|
6
|
+
* saying who sent it. Undefined for the person, whose messages carry none.
|
|
7
|
+
* The names and ids are the host's and untrusted: each is one JSON-quoted
|
|
8
|
+
* line, so none can end the line, the bracket or the quote.
|
|
9
|
+
*/
|
|
10
|
+
export function originLine(origin: Origin | undefined): string | undefined {
|
|
11
|
+
const sender = agentOrigin(origin);
|
|
12
|
+
if (!sender) return undefined;
|
|
13
|
+
const tail = "the person's own messages never carry this line.";
|
|
14
|
+
if (sender.kind === "session") {
|
|
15
|
+
const named = sender.name ? `session ${quoted(sender.name)}, id` : "session id";
|
|
16
|
+
return `[From another agent, not from the person: ${named} ${quoted(sender.sessionId)}. Weigh it as a colleague's request; ${tail}]`;
|
|
17
|
+
}
|
|
18
|
+
return `[From an outside assistant the person connected, not typed by the person: ${quoted(sender.name)}, id ${quoted(sender.id)}. It acts for the person but is not the person; ${tail}]`;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Line breaks a reader may honour, the C0/C1 controls and bidirectional overrides. */
|
|
22
|
+
// biome-ignore lint/suspicious/noControlCharactersInRegex: the controls are what it drops
|
|
23
|
+
const UNSAFE = /[\u0000-\u001f\u007f-\u009f\u2028\u2029\u200e\u200f\u202a-\u202e\u2066-\u2069]+/g;
|
|
24
|
+
/** The longest name or id the line carries. */
|
|
25
|
+
const LONGEST = 200;
|
|
26
|
+
|
|
27
|
+
/** One line, JSON-quoted: breaks and controls become a space, then it is clipped. */
|
|
28
|
+
function quoted(text: string): string {
|
|
29
|
+
const line = text.replace(UNSAFE, " ").replace(/\s+/g, " ").trim();
|
|
30
|
+
return JSON.stringify(line.length > LONGEST ? `${line.slice(0, LONGEST - 1)}…` : line);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The input as its agent is given it: the sender's line first when it is not
|
|
35
|
+
* the person (to text after a blank line; before a list as a text part of its
|
|
36
|
+
* own). The echo, and every store, keep the caller's input as it was sent.
|
|
37
|
+
*/
|
|
38
|
+
export function withOriginLine(input: AgentInput, origin: Origin | undefined): AgentInput {
|
|
39
|
+
const line = originLine(origin);
|
|
40
|
+
if (!line) return input;
|
|
41
|
+
if (typeof input === "string") return `${line}\n\n${input}`;
|
|
42
|
+
return [{ type: "text", text: line }, ...input];
|
|
43
|
+
}
|
|
@@ -88,6 +88,7 @@ export class RequestBroker {
|
|
|
88
88
|
toolCall: PermissionToolCall,
|
|
89
89
|
options: PermissionOption[],
|
|
90
90
|
signal?: AbortSignal,
|
|
91
|
+
purpose: "tool" | "plan" = "tool",
|
|
91
92
|
): Promise<PermissionOutcome> {
|
|
92
93
|
const { sessionId, turnId } = this.turn;
|
|
93
94
|
return this.park(
|
|
@@ -98,6 +99,7 @@ export class RequestBroker {
|
|
|
98
99
|
sessionId,
|
|
99
100
|
turnId,
|
|
100
101
|
requestId,
|
|
102
|
+
purpose,
|
|
101
103
|
title: toolCall.title ?? `Allow tool: ${toolCall.toolName}`,
|
|
102
104
|
toolCall,
|
|
103
105
|
options,
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { AgentInput, TurnSteerResult } from "../../protocol/index.ts";
|
|
1
|
+
import type { AgentInput, Origin, TurnSteerResult } from "../../protocol/index.ts";
|
|
2
2
|
import type { SteerOutcome } from "../agents/base.ts";
|
|
3
3
|
|
|
4
4
|
/** What a turn answered a steer: kept with the settled turn, so a retry converges. */
|
|
@@ -11,6 +11,8 @@ export interface SteerAnswer {
|
|
|
11
11
|
/** A steer the turn took: held until the agent reads it, unless the harness refused it. */
|
|
12
12
|
interface Steer extends SteerAnswer {
|
|
13
13
|
readonly input: AgentInput;
|
|
14
|
+
/** Who sent it, for its echo (absent: the person). */
|
|
15
|
+
readonly origin?: Origin;
|
|
14
16
|
state: "held" | "read" | "refused";
|
|
15
17
|
}
|
|
16
18
|
|
|
@@ -43,11 +45,13 @@ export class TurnSteers {
|
|
|
43
45
|
inputKey: string,
|
|
44
46
|
input: AgentInput,
|
|
45
47
|
deliver: () => Promise<SteerOutcome>,
|
|
48
|
+
origin?: Origin,
|
|
46
49
|
): Promise<TurnSteerResult> {
|
|
47
50
|
let answered!: (result: Promise<TurnSteerResult>) => void;
|
|
48
51
|
const steer: Steer = {
|
|
49
52
|
inputKey,
|
|
50
53
|
input,
|
|
54
|
+
...(origin && { origin }),
|
|
51
55
|
state: "held",
|
|
52
56
|
result: new Promise((resolve) => {
|
|
53
57
|
answered = resolve;
|
|
@@ -78,12 +82,12 @@ export class TurnSteers {
|
|
|
78
82
|
return { outcome: "no_active_turn" };
|
|
79
83
|
}
|
|
80
84
|
|
|
81
|
-
/** The agent read a steer: its input, once, for its echo. */
|
|
82
|
-
read(steerId: string): AgentInput | undefined {
|
|
85
|
+
/** The agent read a steer: its input and sender, once, for its echo. */
|
|
86
|
+
read(steerId: string): { input: AgentInput; origin?: Origin } | undefined {
|
|
83
87
|
const steer = this.steers.get(steerId);
|
|
84
88
|
if (steer?.state !== "held") return undefined;
|
|
85
89
|
steer.state = "read";
|
|
86
|
-
return steer.input;
|
|
90
|
+
return { input: steer.input, ...(steer.origin && { origin: steer.origin }) };
|
|
87
91
|
}
|
|
88
92
|
|
|
89
93
|
/** The steers it took that the agent has not read (all of them, once the turn is over). */
|
package/src/core/runtime/turn.ts
CHANGED
|
@@ -21,6 +21,7 @@ import { type DiagnosticHandler, emitDiagnostic } from "../diagnostics.ts";
|
|
|
21
21
|
import { EventProcessor } from "./event-processor.ts";
|
|
22
22
|
import { type EventSink, EventSinkError } from "./event-sink.ts";
|
|
23
23
|
import type { ModelLists } from "./model-lists.ts";
|
|
24
|
+
import { withOriginLine } from "./origin-line.ts";
|
|
24
25
|
import { type StandingRules, decisionFor, optionsFor, permissionAsk } from "./permission-policy.ts";
|
|
25
26
|
import { RequestBroker } from "./request-broker.ts";
|
|
26
27
|
import { type SteerAnswer, TurnSteers } from "./steering.ts";
|
|
@@ -203,7 +204,7 @@ export class Turn {
|
|
|
203
204
|
}
|
|
204
205
|
|
|
205
206
|
private async execute(): Promise<RunSummary> {
|
|
206
|
-
const { sessionId, turnId, input, config } = this.request;
|
|
207
|
+
const { sessionId, turnId, input, config, origin } = this.request;
|
|
207
208
|
// Looked up as the turn executes: a harness the registry lacks rejects
|
|
208
209
|
// the run, which is not remembered, so a retry may run.
|
|
209
210
|
const agent = this.host.registry.getAgent(config.harness);
|
|
@@ -238,11 +239,12 @@ export class Turn {
|
|
|
238
239
|
// the stream is a complete transcript and multi-client attach works. It is
|
|
239
240
|
// the caller's input as sent, which the caller can predict; the agent is
|
|
240
241
|
// given it well-formed (`wellFormedInput`).
|
|
241
|
-
for (const event of this.processor.echoUserMessage(input)) await this.emit(event);
|
|
242
|
+
for (const event of this.processor.echoUserMessage(input, origin)) await this.emit(event);
|
|
242
243
|
|
|
243
244
|
let threw: TurnThrew | undefined;
|
|
244
245
|
try {
|
|
245
|
-
|
|
246
|
+
// @ref LLP 0019#origin-on-the-wire-and-to-the-model — told who sent it, when not the person
|
|
247
|
+
const given = wellFormedInput(withOriginLine(input, origin));
|
|
246
248
|
for await (const raw of agent.execute(given, this.agentOptions(modelThinking))) {
|
|
247
249
|
await this.announceNativeSession();
|
|
248
250
|
if (config.includeRaw) {
|
|
@@ -358,7 +360,12 @@ export class Turn {
|
|
|
358
360
|
: undefined;
|
|
359
361
|
if (standing) return standing;
|
|
360
362
|
const options = optionsFor(ask);
|
|
361
|
-
const outcome = await this.requests.askPermission(
|
|
363
|
+
const outcome = await this.requests.askPermission(
|
|
364
|
+
toolCall,
|
|
365
|
+
options,
|
|
366
|
+
opts?.signal,
|
|
367
|
+
opts?.purpose ?? "tool",
|
|
368
|
+
);
|
|
362
369
|
if (outcome.outcome === "cancelled") return CANCEL;
|
|
363
370
|
const { decision, selected } = decisionFor(outcome, options);
|
|
364
371
|
if (ask.kind === "engine_always")
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The refusals: a closed set of codes (`OPERATOR_ERROR_CODES`), each with its
|
|
3
|
+
* sentence. A refusal reads `refused (<code>): <sentence>`, then the host's
|
|
4
|
+
* words, then the next call, which names only a tool that is installed.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
// @ref LLP 0019#errors — one sentence per code, and a next call that exists
|
|
8
|
+
|
|
9
|
+
import { json } from "./render.ts";
|
|
10
|
+
import type { OperatorErrorCode, Refusal } from "./types.ts";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* A host's refusal at its own commit (`start`, `archive`, `send`): the code
|
|
14
|
+
* and the sessions it is about, by id; the package words it as its own
|
|
15
|
+
* refusal of that code, naming the sessions as the caller sees them.
|
|
16
|
+
*/
|
|
17
|
+
export class Refused extends Error {
|
|
18
|
+
override name = "Refused";
|
|
19
|
+
constructor(readonly refusal: Refusal) {
|
|
20
|
+
super(`refused (${refusal.code})`);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* What can be archived, as `archive` takes it: a chat on its own, or, for a
|
|
26
|
+
* chat that is its workspace's only one, the workspace (a host may refuse
|
|
27
|
+
* archiving a workspace's last chat on its own).
|
|
28
|
+
*/
|
|
29
|
+
export interface Archivable {
|
|
30
|
+
sessions: readonly string[];
|
|
31
|
+
workspaces: readonly string[];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const archiveHint = (hint: Hint, { sessions, workspaces }: Archivable) =>
|
|
35
|
+
sessions.length || workspaces.length
|
|
36
|
+
? hint("archive", {
|
|
37
|
+
...(sessions.length ? { sessions } : {}),
|
|
38
|
+
...(workspaces.length ? { workspaces } : {}),
|
|
39
|
+
})
|
|
40
|
+
: undefined;
|
|
41
|
+
|
|
42
|
+
/** A refusal a model can act on. Anything else a tool throws is `failed`. */
|
|
43
|
+
export class OperatorError extends Error {
|
|
44
|
+
override name = "OperatorError";
|
|
45
|
+
constructor(
|
|
46
|
+
readonly code: OperatorErrorCode,
|
|
47
|
+
sentence: string,
|
|
48
|
+
readonly extra: {
|
|
49
|
+
/** The host's words: trusted, one line, said after the sentence. */
|
|
50
|
+
detail?: string;
|
|
51
|
+
/** The next call, already built from the installed tools. */
|
|
52
|
+
next?: string;
|
|
53
|
+
retryAfter?: number;
|
|
54
|
+
/** What was done before the refusal, when something was. */
|
|
55
|
+
done?: string;
|
|
56
|
+
} = {},
|
|
57
|
+
) {
|
|
58
|
+
const done = extra.done ? `${extra.done} ` : "";
|
|
59
|
+
const detail = extra.detail ? ` ${extra.detail}` : "";
|
|
60
|
+
const next = extra.next ? `\n ${extra.next}` : "";
|
|
61
|
+
super(`refused (${code}): ${done}${sentence}${detail}${next}`);
|
|
62
|
+
}
|
|
63
|
+
get retryAfter(): number | undefined {
|
|
64
|
+
return this.extra.retryAfter;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** A refusal whose words are already built (a batch whose every item was refused). */
|
|
68
|
+
static said(code: OperatorErrorCode, text: string): OperatorError {
|
|
69
|
+
const error = new OperatorError(code, "");
|
|
70
|
+
error.message = text;
|
|
71
|
+
return error;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Build a hint for a call, or nothing when that tool is not installed. */
|
|
76
|
+
export type Hint = (tool: string, args?: Record<string, unknown>) => string | undefined;
|
|
77
|
+
|
|
78
|
+
const NOTHING = "Nothing was done.";
|
|
79
|
+
/** Every value a refusal names: one JSON line, nothing raw (a repository's name, a setup's error). */
|
|
80
|
+
const q = (text: string) => json(text);
|
|
81
|
+
const list = (refs: readonly string[]) => refs.map(q).join(", ") || "(none)";
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Every refusal, by code. Each takes what its sentence names and the `hint`
|
|
85
|
+
* of the operator that refuses, so its next call is one the caller has.
|
|
86
|
+
*/
|
|
87
|
+
export const refuse = {
|
|
88
|
+
invalidInput: (tool: string, field: string, problem: string) =>
|
|
89
|
+
new OperatorError("invalid_input", `${tool}: ${field}: ${problem}. ${NOTHING}`),
|
|
90
|
+
notFound: (hint: Hint, ref: string, known: readonly string[]) =>
|
|
91
|
+
new OperatorError(
|
|
92
|
+
"not_found",
|
|
93
|
+
`No session, workspace or project ${q(ref)}. Known: ${list(known)}. ${NOTHING}`,
|
|
94
|
+
{ next: hint("list_sessions") },
|
|
95
|
+
),
|
|
96
|
+
ambiguous: (ref: string, refs: readonly string[]) =>
|
|
97
|
+
new OperatorError(
|
|
98
|
+
"ambiguous",
|
|
99
|
+
`${q(ref)} names several: ${list(refs)}. Use one of them. ${NOTHING}`,
|
|
100
|
+
),
|
|
101
|
+
archived: (hint: Hint, ref: string, project?: string) =>
|
|
102
|
+
new OperatorError("archived", `${q(ref)} is archived, so nothing was done.`, {
|
|
103
|
+
next: project ? hint("start_session", { project, message: "<the task>" }) : undefined,
|
|
104
|
+
}),
|
|
105
|
+
self: (what: "session" | "workspace") =>
|
|
106
|
+
new OperatorError(
|
|
107
|
+
"self",
|
|
108
|
+
`That is your own ${what}: a session cannot send to, stop, answer or archive itself. ${NOTHING}`,
|
|
109
|
+
),
|
|
110
|
+
readOnly: () =>
|
|
111
|
+
new OperatorError(
|
|
112
|
+
"read_only",
|
|
113
|
+
`You are in plan mode, so you may only list, read and wait. ${NOTHING} Put this step in your plan.`,
|
|
114
|
+
),
|
|
115
|
+
access: (detail?: string) =>
|
|
116
|
+
new OperatorError("access", `You may not do this here. ${NOTHING}`, { detail }),
|
|
117
|
+
notYoursArchive: (ref: string, busy: readonly string[]) =>
|
|
118
|
+
new OperatorError(
|
|
119
|
+
"not_yours",
|
|
120
|
+
`${q(ref)} has sessions you did not start working or waiting (${list(busy)}). Nothing was archived. Ask the person.`,
|
|
121
|
+
),
|
|
122
|
+
notYoursAnswer: (ref: string) =>
|
|
123
|
+
new OperatorError(
|
|
124
|
+
"not_yours",
|
|
125
|
+
`${q(ref)} was not started by you or a session you started, so its answer is the person's. Nothing was sent. Tell the person what it waits on.`,
|
|
126
|
+
),
|
|
127
|
+
notYoursPermission: () =>
|
|
128
|
+
new OperatorError(
|
|
129
|
+
"not_yours",
|
|
130
|
+
"Only a session with full access answers a permission. Nothing was sent. Tell the person.",
|
|
131
|
+
),
|
|
132
|
+
hasChildren: (hint: Hint, ref: string, children: readonly string[], archive: Archivable) =>
|
|
133
|
+
new OperatorError(
|
|
134
|
+
"has_children",
|
|
135
|
+
`${q(ref)} started sessions that are not archived (${list(children)}). Nothing was archived. Archive those first.`,
|
|
136
|
+
{ next: archiveHint(hint, archive) },
|
|
137
|
+
),
|
|
138
|
+
busy: (
|
|
139
|
+
hint: Hint,
|
|
140
|
+
ref: string,
|
|
141
|
+
state: "working" | "waiting on an answer" | "being set up",
|
|
142
|
+
undone: "archived" | "started in it" | "sent",
|
|
143
|
+
) =>
|
|
144
|
+
new OperatorError("busy", `${q(ref)} is ${state}, so nothing was ${undone}.`, {
|
|
145
|
+
next:
|
|
146
|
+
state === "being set up"
|
|
147
|
+
? hint("wait_for_sessions", { sessions: [ref] })
|
|
148
|
+
: hint("stop_sessions", { sessions: [ref] }),
|
|
149
|
+
}),
|
|
150
|
+
notWaiting: (hint: Hint, ref: string, status: string, current?: string) =>
|
|
151
|
+
new OperatorError(
|
|
152
|
+
"not_waiting",
|
|
153
|
+
`${q(ref)} is not waiting on that request (status: ${status}). Nothing was sent.${current ? ` It waits on request ${q(current)}.` : ""}`,
|
|
154
|
+
{
|
|
155
|
+
next: current
|
|
156
|
+
? hint("answer_request", { session: ref, request: current, answer: "<your answer>" })
|
|
157
|
+
: hint("send_message", { session: ref, message: "<your answer>" }),
|
|
158
|
+
},
|
|
159
|
+
),
|
|
160
|
+
personOnly: (ref: string, why: string) =>
|
|
161
|
+
new OperatorError(
|
|
162
|
+
"person_only",
|
|
163
|
+
`${q(ref)} waits on something only the person can answer, where the session runs (${why}). Nothing was sent. Tell the person.`,
|
|
164
|
+
),
|
|
165
|
+
setupFailed: (hint: Hint, ref: string, reason: string, done?: string) =>
|
|
166
|
+
new OperatorError(
|
|
167
|
+
"setup_failed",
|
|
168
|
+
`${q(ref)} could not be set up: ${q(reason)}. Nothing was sent.`,
|
|
169
|
+
{
|
|
170
|
+
next: hint("archive", { workspaces: [ref] }),
|
|
171
|
+
done,
|
|
172
|
+
},
|
|
173
|
+
),
|
|
174
|
+
limitDepth: (depth: number) =>
|
|
175
|
+
new OperatorError(
|
|
176
|
+
"limit_depth",
|
|
177
|
+
`You were started by a chain of ${depth} sessions, the most allowed, so you cannot start another. Nothing was started. Do the work yourself, or tell the person.`,
|
|
178
|
+
),
|
|
179
|
+
limitSessions: (hint: Hint, count: number, settled: Archivable) =>
|
|
180
|
+
new OperatorError(
|
|
181
|
+
"limit_sessions",
|
|
182
|
+
`You have ${count} sessions you started that are not archived (the limit). Nothing was started. Archive the ones whose work is done.`,
|
|
183
|
+
{ next: archiveHint(hint, settled) },
|
|
184
|
+
),
|
|
185
|
+
limitSettingUp: (retryAfter: number, detail?: string) =>
|
|
186
|
+
new OperatorError(
|
|
187
|
+
"limit_setting_up",
|
|
188
|
+
`As many workspaces as this host sets up at once are being set up. Nothing was started. Try again in about ${retryAfter}s.`,
|
|
189
|
+
{ detail, retryAfter },
|
|
190
|
+
),
|
|
191
|
+
limitRate: (count: number, retryAfter: number) =>
|
|
192
|
+
new OperatorError(
|
|
193
|
+
"limit_rate",
|
|
194
|
+
`You started ${count} sessions in the last minute (the limit). Nothing was started. Try again in ${retryAfter}s.`,
|
|
195
|
+
{ retryAfter },
|
|
196
|
+
),
|
|
197
|
+
limitResources: (retryAfter: number | undefined, detail?: string) =>
|
|
198
|
+
new OperatorError(
|
|
199
|
+
"limit_resources",
|
|
200
|
+
`This computer cannot take another session now. Nothing was started.${retryAfter ? ` Try again in ${retryAfter}s.` : ""}`,
|
|
201
|
+
{ detail, retryAfter },
|
|
202
|
+
),
|
|
203
|
+
staleCursor: (hint: Hint, ref: string) =>
|
|
204
|
+
new OperatorError(
|
|
205
|
+
"stale_cursor",
|
|
206
|
+
`That cursor no longer points into ${q(ref)}. Nothing was read.`,
|
|
207
|
+
{
|
|
208
|
+
next: hint("read_session", { session: ref }),
|
|
209
|
+
},
|
|
210
|
+
),
|
|
211
|
+
unsupported: (detail?: string) =>
|
|
212
|
+
new OperatorError("unsupported", `This cannot be done here. ${NOTHING}`, { detail }),
|
|
213
|
+
failed: () =>
|
|
214
|
+
new OperatorError(
|
|
215
|
+
"failed",
|
|
216
|
+
"The tool failed; nothing more is known. Try again, or tell the person.",
|
|
217
|
+
),
|
|
218
|
+
};
|
package/src/operator/index.ts
CHANGED
|
@@ -1,40 +1,35 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* `@zvada/agent-server/operator`: the session tools an outside agent
|
|
3
|
-
* product's coding agents with, and their
|
|
4
|
-
* protocol and zod; the product is the
|
|
2
|
+
* `@zvada/agent-server/operator`: the session tools an outside agent, or a
|
|
3
|
+
* product's own session, operates a product's coding agents with, and their
|
|
4
|
+
* MCP binding. Imports only the protocol and zod; the product is the
|
|
5
|
+
* `OperatorHost` (LLP 0014, LLP 0019).
|
|
5
6
|
*/
|
|
6
7
|
export { PLAN_OPTIONS, PLAN_TITLE } from "../protocol/plan.ts";
|
|
8
|
+
export { type Archivable, type Hint, OperatorError, Refused, refuse } from "./errors.ts";
|
|
7
9
|
export {
|
|
8
10
|
type McpHandler,
|
|
9
11
|
type McpServerInfo,
|
|
10
12
|
type ToolSource,
|
|
11
13
|
createMcpHandler,
|
|
12
14
|
} from "./mcp.ts";
|
|
15
|
+
export { DEFAULT_LIMITS, DEFAULT_WAITS, createOperator, tool } from "./operator.ts";
|
|
16
|
+
export { refOf } from "./refs.ts";
|
|
13
17
|
export {
|
|
14
|
-
|
|
15
|
-
type
|
|
16
|
-
type
|
|
17
|
-
type
|
|
18
|
-
type
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
type StartInput,
|
|
22
|
-
type StartResult,
|
|
23
|
-
type Tool,
|
|
24
|
-
type ToolInfo,
|
|
25
|
-
type ViewOptions,
|
|
26
|
-
OperatorError,
|
|
27
|
-
createOperator,
|
|
28
|
-
tool,
|
|
29
|
-
} from "./operator.ts";
|
|
18
|
+
OPERATOR_GUIDANCE,
|
|
19
|
+
type SessionTool,
|
|
20
|
+
type SessionToolCaller,
|
|
21
|
+
type SessionToolContent,
|
|
22
|
+
type SessionToolSource,
|
|
23
|
+
operatorSessionTools,
|
|
24
|
+
} from "./session-tools.ts";
|
|
30
25
|
export { type Answer, callHint, oneLine, quote, quoted } from "./render.ts";
|
|
26
|
+
export * from "./types.ts";
|
|
31
27
|
export {
|
|
32
28
|
type DecisionRequest,
|
|
33
|
-
type Phase,
|
|
34
29
|
type QuestionRequest,
|
|
35
|
-
type SessionView,
|
|
36
30
|
type ViewMessage,
|
|
37
|
-
|
|
38
|
-
|
|
31
|
+
settledStatus,
|
|
32
|
+
turnOf,
|
|
39
33
|
viewOf,
|
|
34
|
+
waitingOf,
|
|
40
35
|
} from "./view.ts";
|
package/src/operator/mcp.ts
CHANGED
|
@@ -11,8 +11,8 @@
|
|
|
11
11
|
|
|
12
12
|
// @ref LLP 0014#the-mcp-binding — the engine answers MCP's tool methods; the route, its authentication and its caller are the host's
|
|
13
13
|
|
|
14
|
-
import { OperatorError,
|
|
15
|
-
import {
|
|
14
|
+
import { OperatorError, refuse } from "./errors.ts";
|
|
15
|
+
import type { ToolInfo } from "./types.ts";
|
|
16
16
|
|
|
17
17
|
/** Anything that lists and runs tools: an operator, or a host's wrapper around one. */
|
|
18
18
|
export interface ToolSource<Caller> {
|
|
@@ -115,8 +115,9 @@ export function createMcpHandler<Caller>(
|
|
|
115
115
|
},
|
|
116
116
|
})),
|
|
117
117
|
});
|
|
118
|
-
} catch
|
|
119
|
-
|
|
118
|
+
} catch {
|
|
119
|
+
// Its text may be anyone's: a fixed sentence instead.
|
|
120
|
+
return fail(id, -32603, "The tool list could not be read.");
|
|
120
121
|
}
|
|
121
122
|
}
|
|
122
123
|
case "tools/call": {
|
|
@@ -127,16 +128,10 @@ export function createMcpHandler<Caller>(
|
|
|
127
128
|
return ok(id, text(await source.call(params.name, params.arguments ?? {}, caller)));
|
|
128
129
|
} catch (error) {
|
|
129
130
|
// The model reads the reason and can act on it; a JSON-RPC error it would not see.
|
|
130
|
-
// A refusal's words are meant for it; any other failure's may be an agent's or a
|
|
131
|
-
// repository's, so it is one quoted line, never a line that reads as a hint.
|
|
132
131
|
return ok(
|
|
133
132
|
id,
|
|
134
|
-
text
|
|
135
|
-
|
|
136
|
-
? error.message
|
|
137
|
-
: `The tool failed: ${quoted(messageOf(error))}`,
|
|
138
|
-
true,
|
|
139
|
-
),
|
|
133
|
+
// Any other failure's text may be an agent's or a repository's: never said.
|
|
134
|
+
text(error instanceof OperatorError ? error.message : refuse.failed().message, true),
|
|
140
135
|
);
|
|
141
136
|
}
|
|
142
137
|
}
|
|
@@ -165,7 +160,3 @@ function idOf(message: Record<string, unknown>): Id {
|
|
|
165
160
|
const id = message.id;
|
|
166
161
|
return typeof id === "string" || typeof id === "number" ? id : null;
|
|
167
162
|
}
|
|
168
|
-
|
|
169
|
-
function messageOf(error: unknown): string {
|
|
170
|
-
return error instanceof Error ? error.message : String(error);
|
|
171
|
-
}
|