@zvada/agent-server 0.3.23 → 0.3.24
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 +34 -0
- package/README.md +14 -5
- package/dist/accounts/chatgpt.js +144 -0
- package/dist/accounts/check.js +71 -0
- package/dist/accounts/claude.js +80 -0
- package/dist/accounts/endpoint.js +104 -0
- package/dist/accounts/http.js +58 -0
- package/dist/accounts/index.js +9 -0
- package/dist/client/client.js +686 -0
- package/dist/client/connection.js +338 -0
- package/dist/client/history.js +49 -0
- package/dist/client/index.js +6 -0
- package/dist/client/listeners.js +19 -0
- package/dist/client/logins.js +92 -0
- package/dist/client/rpc.js +105 -0
- package/dist/client/streams.js +432 -0
- package/dist/client/transports.js +118 -0
- package/dist/client/turns.js +122 -0
- package/dist/core/agents/acp/acp-agent.js +425 -0
- package/dist/core/agents/acp/adapter.js +231 -0
- package/dist/core/agents/acp/client.js +222 -0
- package/dist/core/agents/acp/known-agents.js +57 -0
- package/dist/core/agents/acp/mappings.js +164 -0
- package/dist/core/agents/acp/requests.js +79 -0
- package/dist/core/agents/attachments.js +14 -0
- package/dist/core/agents/base.js +64 -0
- package/dist/core/agents/claude-code/adapter.js +474 -0
- package/dist/core/agents/claude-code/approvals.js +245 -0
- package/dist/core/agents/claude-code/claude-agent.js +319 -0
- package/dist/core/agents/claude-code/credentials.js +142 -0
- package/dist/core/agents/claude-code/events.js +11 -0
- package/dist/core/agents/claude-code/login.js +217 -0
- package/dist/core/agents/claude-code/mcp.js +181 -0
- package/dist/core/agents/claude-code/models.js +74 -0
- package/dist/core/agents/claude-code/options.js +240 -0
- package/dist/core/agents/claude-code/outcome.js +204 -0
- package/dist/core/agents/claude-code/results.js +58 -0
- package/dist/core/agents/claude-code/sdk-pair.js +59 -0
- package/dist/core/agents/claude-code/session.js +593 -0
- package/dist/core/agents/claude-code/spawn.js +125 -0
- package/dist/core/agents/claude-code/spend.js +118 -0
- package/dist/core/agents/claude-code/tools.js +131 -0
- package/dist/core/agents/claude-code/turn-usage.js +121 -0
- package/dist/core/agents/claude-code/turn.js +199 -0
- package/dist/core/agents/codex-app-server/adapter.js +463 -0
- package/dist/core/agents/codex-app-server/client.js +338 -0
- package/dist/core/agents/codex-app-server/codex-agent.js +294 -0
- package/dist/core/agents/codex-app-server/commands.js +184 -0
- package/dist/core/agents/codex-app-server/credentials.js +92 -0
- package/dist/core/agents/codex-app-server/effort.js +13 -0
- package/dist/core/agents/codex-app-server/events.js +18 -0
- package/dist/core/agents/codex-app-server/homes.js +118 -0
- package/dist/core/agents/codex-app-server/input.js +52 -0
- package/dist/core/agents/codex-app-server/items.js +307 -0
- package/dist/core/agents/codex-app-server/login.js +183 -0
- package/dist/core/agents/codex-app-server/mcp.js +323 -0
- package/dist/core/agents/codex-app-server/models.js +160 -0
- package/dist/core/agents/codex-app-server/options.js +155 -0
- package/dist/core/agents/codex-app-server/plan-approval.js +124 -0
- package/dist/core/agents/codex-app-server/protocol.js +38 -0
- package/dist/core/agents/codex-app-server/requests.js +250 -0
- package/dist/core/agents/codex-app-server/session.js +278 -0
- package/dist/core/agents/codex-app-server/subagents.js +260 -0
- package/dist/core/agents/codex-app-server/thread-settings.js +214 -0
- package/dist/core/agents/codex-app-server/turn-items.js +69 -0
- package/dist/core/agents/codex-app-server/turn.js +448 -0
- package/dist/core/agents/codex-app-server/user-login.js +78 -0
- package/dist/core/agents/config-fingerprint.js +10 -0
- package/dist/core/agents/credentials.js +71 -0
- package/dist/core/agents/diffs.js +27 -0
- package/dist/core/agents/elicitation.js +42 -0
- package/dist/core/agents/inherit-config.js +5 -0
- package/dist/core/agents/login.js +56 -0
- package/dist/core/agents/mcp-config.js +109 -0
- package/dist/core/agents/plan.js +6 -0
- package/dist/core/agents/process-pool.js +307 -0
- package/dist/core/agents/registry.js +49 -0
- package/dist/core/agents/scripted/adapter.js +86 -0
- package/dist/core/agents/scripted/mcp-call.js +74 -0
- package/dist/core/agents/scripted/scripted-agent.js +330 -0
- package/dist/core/agents/scripted/scripts.js +154 -0
- package/dist/core/agents/scripted/steps.js +87 -0
- package/dist/core/agents/session-agent.js +209 -0
- package/dist/core/agents/stderr-tail.js +71 -0
- package/dist/core/agents/thinking-levels.js +9 -0
- package/dist/core/agents/types.js +1 -0
- package/dist/core/diagnostics.js +23 -0
- package/dist/core/index.js +31 -0
- package/dist/core/presets.js +114 -0
- package/dist/core/processes/identity.js +91 -0
- package/dist/core/processes/lifeline.js +210 -0
- package/dist/core/processes/native-processes.js +299 -0
- package/dist/core/processes/process-table.js +163 -0
- package/dist/core/provision/cache-trust.js +121 -0
- package/dist/core/provision/cache-usage.js +61 -0
- package/dist/core/provision/checksums.js +22 -0
- package/dist/core/provision/extract.js +28 -0
- package/dist/core/provision/index.js +4 -0
- package/dist/core/provision/install-build.js +99 -0
- package/dist/core/provision/npm.js +316 -0
- package/dist/core/provision/pins.js +35 -0
- package/dist/core/provision/platform.js +65 -0
- package/dist/core/provision/provisioner.js +372 -0
- package/dist/core/provision/releases.js +68 -0
- package/dist/core/provision/tools.js +279 -0
- package/dist/core/proxy/anthropic-proxy.js +163 -0
- package/dist/core/proxy/api-key-store.js +30 -0
- package/dist/core/proxy/index.js +3 -0
- package/dist/core/proxy/loopback.js +86 -0
- package/dist/core/runtime/agent-runtime.js +434 -0
- package/dist/core/runtime/event-processor.js +379 -0
- package/dist/core/runtime/event-sink.js +65 -0
- package/dist/core/runtime/harness-clis.js +83 -0
- package/dist/core/runtime/harness-logins.js +313 -0
- package/dist/core/runtime/login-reads.js +71 -0
- package/dist/core/runtime/model-lists.js +160 -0
- package/dist/core/runtime/permission-policy.js +84 -0
- package/dist/core/runtime/request-broker.js +139 -0
- package/dist/core/runtime/steering.js +79 -0
- package/dist/core/runtime/turn-outcome.js +16 -0
- package/dist/core/runtime/turn.js +329 -0
- package/dist/core/utils/bounded.js +13 -0
- package/dist/core/utils/deadline.js +57 -0
- package/dist/core/utils/errors.js +176 -0
- package/dist/core/utils/is-record.js +4 -0
- package/dist/core/utils/loopback.js +11 -0
- package/dist/index.js +4 -0
- package/dist/operator/index.js +10 -0
- package/dist/operator/mcp.js +139 -0
- package/dist/operator/operator.js +499 -0
- package/dist/operator/render.js +408 -0
- package/dist/operator/view.js +35 -0
- package/dist/protocol/account.js +96 -0
- package/dist/protocol/async-queue.js +65 -0
- package/dist/protocol/auth.js +169 -0
- package/dist/protocol/canonical.js +18 -0
- package/dist/protocol/clis.js +63 -0
- package/dist/protocol/config.js +137 -0
- package/dist/protocol/conversation.js +231 -0
- package/dist/protocol/credentials.js +141 -0
- package/dist/protocol/errors.js +181 -0
- package/dist/protocol/execution.js +28 -0
- package/dist/protocol/factories.js +231 -0
- package/dist/protocol/guards.js +71 -0
- package/dist/protocol/harness.js +93 -0
- package/dist/protocol/history.js +201 -0
- package/dist/protocol/ids.js +37 -0
- package/dist/protocol/index.js +36 -0
- package/dist/protocol/lifecycle.js +562 -0
- package/dist/protocol/meta-keys.js +17 -0
- package/dist/protocol/meta.js +13 -0
- package/dist/protocol/model-catalog.js +223 -0
- package/dist/protocol/models.js +84 -0
- package/dist/protocol/part-input.js +94 -0
- package/dist/protocol/parts.js +166 -0
- package/dist/protocol/plan.js +31 -0
- package/dist/protocol/reduce.js +484 -0
- package/dist/protocol/selectors.js +136 -0
- package/dist/protocol/seq-cursor.js +37 -0
- package/dist/protocol/stop-reasons.js +41 -0
- package/dist/protocol/thinking.js +31 -0
- package/dist/protocol/time.js +21 -0
- package/dist/protocol/tokens.js +57 -0
- package/dist/protocol/tool-closure.js +70 -0
- package/dist/protocol/tool-state.js +153 -0
- package/dist/protocol/verify.js +467 -0
- package/dist/protocol/vocabulary.js +27 -0
- package/dist/protocol/wire-api.js +94 -0
- package/dist/protocol/wire.js +669 -0
- package/dist/server/acp/binding.js +251 -0
- package/dist/server/acp/session-records.js +53 -0
- package/dist/server/acp/translate.js +401 -0
- package/dist/server/agent-server.js +338 -0
- package/dist/server/bin.js +492 -0
- package/dist/server/feedback.js +216 -0
- package/dist/server/history.js +123 -0
- package/dist/server/index.js +8 -0
- package/dist/server/install.js +171 -0
- package/dist/server/open-parts.js +87 -0
- package/dist/server/routes.js +291 -0
- package/dist/server/secrets.js +19 -0
- package/dist/server/session-gate.js +54 -0
- package/dist/server/session-log.js +66 -0
- package/dist/server/session-store.js +243 -0
- package/dist/server/subscriptions.js +189 -0
- package/dist/server/transports.js +214 -0
- package/dist/server/wire-error.js +26 -0
- package/dist/sync/event-log.js +89 -0
- package/dist/sync/fit.js +202 -0
- package/dist/sync/history-reader.js +33 -0
- package/dist/sync/history-sqlite.js +281 -0
- package/dist/sync/history.js +163 -0
- package/dist/sync/index.js +5 -0
- package/dist/sync/journal-sqlite.js +316 -0
- package/dist/sync/journal.js +10 -0
- package/dist/sync/live-tails.js +85 -0
- package/dist/sync/node-sqlite.js +27 -0
- package/dist/sync/projection.js +32 -0
- package/dist/sync/recovery.js +117 -0
- package/dist/sync/retry.js +45 -0
- package/dist/sync/schema.js +73 -0
- package/dist/sync/sql.js +94 -0
- package/dist/sync/sqlite-file.js +31 -0
- package/dist/sync/sqlite.js +18 -0
- package/dist/sync/worker.js +212 -0
- package/dist/types/core/agents/claude-code/approvals.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/claude-agent.d.ts +4 -1
- package/dist/types/core/agents/claude-code/claude-agent.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/options.d.ts +24 -11
- package/dist/types/core/agents/claude-code/options.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/session.d.ts +8 -0
- package/dist/types/core/agents/claude-code/session.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/homes.d.ts +6 -0
- package/dist/types/core/agents/codex-app-server/homes.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/login.d.ts +12 -0
- package/dist/types/core/agents/codex-app-server/login.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/requests.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/session.d.ts +4 -1
- package/dist/types/core/agents/codex-app-server/session.d.ts.map +1 -1
- package/dist/types/core/agents/process-pool.d.ts +8 -0
- package/dist/types/core/agents/process-pool.d.ts.map +1 -1
- package/dist/types/core/presets.d.ts +2 -2
- package/dist/types/core/presets.d.ts.map +1 -1
- package/dist/types/server/bin.d.ts +3 -0
- package/dist/types/server/bin.d.ts.map +1 -0
- package/dist/types/server/transports.d.ts +2 -1
- package/dist/types/server/transports.d.ts.map +1 -1
- package/dist/types/sync/node-sqlite.d.ts +4 -1
- package/dist/types/sync/node-sqlite.d.ts.map +1 -1
- package/docs/consuming.md +30 -4
- package/docs/deploy.md +28 -0
- package/docs/harnesses.md +13 -1
- package/package.json +43 -18
- package/src/core/agents/claude-code/approvals.ts +5 -1
- package/src/core/agents/claude-code/claude-agent.ts +32 -5
- package/src/core/agents/claude-code/options.ts +24 -11
- package/src/core/agents/claude-code/session.ts +13 -0
- package/src/core/agents/codex-app-server/homes.ts +11 -2
- package/src/core/agents/codex-app-server/login.ts +30 -0
- package/src/core/agents/codex-app-server/requests.ts +7 -6
- package/src/core/agents/codex-app-server/session.ts +11 -2
- package/src/core/agents/process-pool.ts +30 -4
- package/src/core/presets.ts +3 -2
- package/src/server/bin.ts +25 -10
- package/src/server/transports.ts +6 -1
- package/src/sync/node-sqlite.ts +19 -3
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
import { createSteerEchoParts, createUserEchoParts, echoMessageId, extendPart, generateUUIDv7, steerMessageId, } from "../../protocol/index.js";
|
|
2
|
+
/**
|
|
3
|
+
* The execution `turn.ended` carries: the request, plus what the harness said
|
|
4
|
+
* it ran. Models the stream reported (Claude's API model, a Codex reroute) say
|
|
5
|
+
* what answered; the model the agent reported for its thread fills in
|
|
6
|
+
* otherwise. `applied` only for a turn whose model ran.
|
|
7
|
+
*/
|
|
8
|
+
function endedExecution(execution, result, notApplied, applied) {
|
|
9
|
+
const reportedModels = result.reportedModels?.length
|
|
10
|
+
? result.reportedModels
|
|
11
|
+
: applied?.model
|
|
12
|
+
? [applied.model]
|
|
13
|
+
: [];
|
|
14
|
+
return {
|
|
15
|
+
...execution,
|
|
16
|
+
...(applied?.thinkingLevel !== undefined && { appliedThinkingLevel: applied.thinkingLevel }),
|
|
17
|
+
...(reportedModels.length > 0 && { reportedModels }),
|
|
18
|
+
// Whether or not the model ran: what was not applied never was.
|
|
19
|
+
...(notApplied.size > 0 && { notApplied: [...notApplied] }),
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
/** A part deltas stream into: text and reasoning, and a pending tool's input. */
|
|
23
|
+
const isStreamed = (part) => part.type === "text" || part.type === "reasoning" || part.type === "tool";
|
|
24
|
+
/**
|
|
25
|
+
* Turns the harness-agnostic AdapterEvent stream into wire LifecycleEvents.
|
|
26
|
+
* Owns all id/index bookkeeping: assigns message ids, per-message output
|
|
27
|
+
* indices, and per-part indices, and brackets messages with started/ended.
|
|
28
|
+
*
|
|
29
|
+
* Parts are registered turn-wide: a part keeps the message/indices of its
|
|
30
|
+
* first emission even when updated after that message ended (e.g. a tool
|
|
31
|
+
* completing after the model message that issued it) — consumers upsert by
|
|
32
|
+
* `part.id`. One instance per turn.
|
|
33
|
+
*/
|
|
34
|
+
export class EventProcessor {
|
|
35
|
+
sessionId;
|
|
36
|
+
turnId;
|
|
37
|
+
execution;
|
|
38
|
+
steers;
|
|
39
|
+
openMessageId;
|
|
40
|
+
/** `parentToolCallId` of the open message (undefined for top-level messages). */
|
|
41
|
+
openMessageParent;
|
|
42
|
+
currentOutputIndex = -1;
|
|
43
|
+
/** Whether the harness produced any output this turn (its model answered). */
|
|
44
|
+
answered = false;
|
|
45
|
+
nextOutputIndex = 0;
|
|
46
|
+
nextPartIndex = 0;
|
|
47
|
+
parts = new Map();
|
|
48
|
+
/** The permission mode the session runs in: the turn's, until the agent switches it. */
|
|
49
|
+
permissionMode;
|
|
50
|
+
/** What the harness runs the turn with, once it says (`AgentExecuteOptions.onExecution`). */
|
|
51
|
+
applied;
|
|
52
|
+
/** The settings the turn set that were not applied, in the order they were found. */
|
|
53
|
+
notApplied = new Set();
|
|
54
|
+
/** The model writing what follows, by sub-agent tool call ("" for the turn's own). */
|
|
55
|
+
writtenBy = new Map();
|
|
56
|
+
/** The model the open message named. */
|
|
57
|
+
openMessageModel;
|
|
58
|
+
constructor(sessionId, turnId, execution,
|
|
59
|
+
/** The turn's steers: the echo of one the agent read, and those left unread. */
|
|
60
|
+
steers) {
|
|
61
|
+
this.sessionId = sessionId;
|
|
62
|
+
this.turnId = turnId;
|
|
63
|
+
this.execution = execution;
|
|
64
|
+
this.steers = steers;
|
|
65
|
+
this.permissionMode = execution.permissionMode ?? "default";
|
|
66
|
+
}
|
|
67
|
+
/** What the harness runs the turn with, as it learns it: its model names the messages no event names. */
|
|
68
|
+
applyExecution({ notApplied, ...update }) {
|
|
69
|
+
this.applied = { ...this.applied, ...update };
|
|
70
|
+
this.markNotApplied(notApplied ?? []);
|
|
71
|
+
}
|
|
72
|
+
/** Settings the turn set that were not applied: `turn.ended.execution.notApplied`. */
|
|
73
|
+
markNotApplied(fields) {
|
|
74
|
+
for (const field of fields)
|
|
75
|
+
this.notApplied.add(field);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* The user echo: emits the submitted input back onto the stream as a
|
|
79
|
+
* complete user message at outputIndex 0, before any harness output — the
|
|
80
|
+
* stream is a complete transcript. Its messageId is DERIVED from the turn
|
|
81
|
+
* id (`echoMessageId`), so a consumer that minted the turnId can predict the
|
|
82
|
+
* whole echo instead of reconciling a look-alike against it.
|
|
83
|
+
*/
|
|
84
|
+
*echoUserMessage(input) {
|
|
85
|
+
yield* this.openMessage("user", undefined, echoMessageId(this.turnId));
|
|
86
|
+
for (const part of createUserEchoParts(input, this.turnId)) {
|
|
87
|
+
yield* this.emitPart(part);
|
|
88
|
+
}
|
|
89
|
+
yield* this.closeMessage();
|
|
90
|
+
}
|
|
91
|
+
// @ref LLP 0002#steering — a steer is a user message where the agent read it
|
|
92
|
+
/**
|
|
93
|
+
* A steer's echo: a complete user message at the point the agent read it,
|
|
94
|
+
* after whatever it had produced so far. Its ids derive from the steer id,
|
|
95
|
+
* like the turn's echo from the turn id. A steer the turn did not take, or
|
|
96
|
+
* already echoed, is ignored.
|
|
97
|
+
*/
|
|
98
|
+
*echoSteer(steerId) {
|
|
99
|
+
const input = this.steers.read(steerId);
|
|
100
|
+
if (input === undefined)
|
|
101
|
+
return;
|
|
102
|
+
yield* this.openMessage("user", undefined, steerMessageId(steerId));
|
|
103
|
+
for (const part of createSteerEchoParts(input, steerId)) {
|
|
104
|
+
yield* this.emitPart(part);
|
|
105
|
+
}
|
|
106
|
+
yield* this.closeMessage();
|
|
107
|
+
}
|
|
108
|
+
*handle(ev) {
|
|
109
|
+
switch (ev.kind) {
|
|
110
|
+
case "steer-inserted":
|
|
111
|
+
yield* this.echoSteer(ev.steerId);
|
|
112
|
+
return;
|
|
113
|
+
case "message-start":
|
|
114
|
+
// The turn's own messages after it, opened by their parts (after a
|
|
115
|
+
// sub-agent's output cut in), are this model's too.
|
|
116
|
+
if (ev.role === "assistant" && ev.model)
|
|
117
|
+
this.writtenBy.set("", ev.model);
|
|
118
|
+
yield* this.openMessage(ev.role, undefined, undefined, ev.model);
|
|
119
|
+
return;
|
|
120
|
+
case "model": {
|
|
121
|
+
const scope = ev.parentToolCallId ?? "";
|
|
122
|
+
this.writtenBy.set(scope, ev.model);
|
|
123
|
+
// Another model writes what follows (a Codex reroute mid-turn): the
|
|
124
|
+
// message open under another one ends, and the next names this one.
|
|
125
|
+
if (this.openMessageId &&
|
|
126
|
+
(this.openMessageParent ?? "") === scope &&
|
|
127
|
+
this.openMessageModel !== ev.model)
|
|
128
|
+
yield* this.closeMessage();
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
case "message-end":
|
|
132
|
+
yield* this.closeMessage();
|
|
133
|
+
return;
|
|
134
|
+
case "part-open":
|
|
135
|
+
case "part-update":
|
|
136
|
+
yield* this.emitPart(ev.part);
|
|
137
|
+
return;
|
|
138
|
+
case "text-delta": {
|
|
139
|
+
yield* this.delta(ev.partId, { type: "text", text: ev.text });
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
case "reasoning-delta": {
|
|
143
|
+
yield* this.delta(ev.partId, { type: "reasoning", text: ev.text });
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
case "tool-input-delta": {
|
|
147
|
+
yield* this.delta(ev.partId, {
|
|
148
|
+
type: "tool_input",
|
|
149
|
+
toolCallId: ev.toolCallId,
|
|
150
|
+
toolName: ev.toolName,
|
|
151
|
+
input: ev.input,
|
|
152
|
+
});
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
case "usage":
|
|
156
|
+
yield {
|
|
157
|
+
type: "session.usage",
|
|
158
|
+
sessionId: this.sessionId,
|
|
159
|
+
turnId: this.turnId,
|
|
160
|
+
used: ev.used,
|
|
161
|
+
...(ev.size !== undefined && { size: ev.size }),
|
|
162
|
+
...(ev.cost !== undefined && { cost: ev.cost }),
|
|
163
|
+
timestamp: Date.now(),
|
|
164
|
+
};
|
|
165
|
+
return;
|
|
166
|
+
case "limits":
|
|
167
|
+
yield {
|
|
168
|
+
type: "session.limits",
|
|
169
|
+
sessionId: this.sessionId,
|
|
170
|
+
turnId: this.turnId,
|
|
171
|
+
windows: ev.windows,
|
|
172
|
+
...(ev.status !== undefined && { status: ev.status }),
|
|
173
|
+
...(ev.planType !== undefined && { planType: ev.planType }),
|
|
174
|
+
timestamp: Date.now(),
|
|
175
|
+
};
|
|
176
|
+
return;
|
|
177
|
+
case "mode":
|
|
178
|
+
// Harnesses restate the mode they run in; only a switch is news.
|
|
179
|
+
if (ev.permissionMode === this.permissionMode)
|
|
180
|
+
return;
|
|
181
|
+
this.permissionMode = ev.permissionMode;
|
|
182
|
+
yield {
|
|
183
|
+
type: "session.mode",
|
|
184
|
+
sessionId: this.sessionId,
|
|
185
|
+
turnId: this.turnId,
|
|
186
|
+
permissionMode: ev.permissionMode,
|
|
187
|
+
timestamp: Date.now(),
|
|
188
|
+
};
|
|
189
|
+
return;
|
|
190
|
+
case "retry":
|
|
191
|
+
yield {
|
|
192
|
+
type: "error",
|
|
193
|
+
sessionId: this.sessionId,
|
|
194
|
+
turnId: this.turnId,
|
|
195
|
+
category: ev.category,
|
|
196
|
+
message: ev.message,
|
|
197
|
+
recoverable: true,
|
|
198
|
+
retry: {
|
|
199
|
+
...(ev.attempt !== undefined && { attempt: ev.attempt }),
|
|
200
|
+
...(ev.maxAttempts !== undefined && { maxAttempts: ev.maxAttempts }),
|
|
201
|
+
...(ev.delayMs !== undefined && { delayMs: ev.delayMs }),
|
|
202
|
+
},
|
|
203
|
+
timestamp: Date.now(),
|
|
204
|
+
};
|
|
205
|
+
return;
|
|
206
|
+
case "compacted": {
|
|
207
|
+
// Harnesses report compaction after the fact — a single completed
|
|
208
|
+
// upsert of the positional entity (its first appearance anchors it).
|
|
209
|
+
const at = Date.now();
|
|
210
|
+
yield {
|
|
211
|
+
type: "session.compaction",
|
|
212
|
+
sessionId: this.sessionId,
|
|
213
|
+
turnId: this.turnId,
|
|
214
|
+
compactionId: generateUUIDv7(at),
|
|
215
|
+
status: "completed",
|
|
216
|
+
...(ev.trigger && { trigger: ev.trigger }),
|
|
217
|
+
...(ev.preTokens !== undefined && { preTokens: ev.preTokens }),
|
|
218
|
+
...(ev.postTokens !== undefined && { postTokens: ev.postTokens }),
|
|
219
|
+
timestamp: at,
|
|
220
|
+
};
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* The turn's last events: what its deltas left unsnapshotted, the open
|
|
227
|
+
* message's end, then `turn.ended`, which is returned on its own too.
|
|
228
|
+
*/
|
|
229
|
+
finish(result, outcome) {
|
|
230
|
+
const events = [];
|
|
231
|
+
for (const { streamed } of [...this.parts.values()]) {
|
|
232
|
+
if (streamed)
|
|
233
|
+
events.push(...this.emitPart(streamed));
|
|
234
|
+
}
|
|
235
|
+
events.push(...this.closeMessage());
|
|
236
|
+
// What the harness configured ran only if its model answered: a turn that
|
|
237
|
+
// failed or was cancelled first ran nothing.
|
|
238
|
+
const ran = this.answered || (outcome.stopReason !== "error" && outcome.stopReason !== "cancelled");
|
|
239
|
+
const undelivered = this.steers.undelivered();
|
|
240
|
+
const ended = {
|
|
241
|
+
type: "turn.ended",
|
|
242
|
+
turnId: this.turnId,
|
|
243
|
+
sessionId: this.sessionId,
|
|
244
|
+
stopReason: outcome.stopReason,
|
|
245
|
+
finishReason: result.finishReason,
|
|
246
|
+
tokens: result.usage,
|
|
247
|
+
cost: result.cost,
|
|
248
|
+
execution: endedExecution(this.execution, result, this.notApplied, ran ? this.applied : undefined),
|
|
249
|
+
error: outcome.error,
|
|
250
|
+
...(undelivered.length > 0 && { undeliveredSteers: undelivered }),
|
|
251
|
+
timestamp: Date.now(),
|
|
252
|
+
};
|
|
253
|
+
events.push(ended);
|
|
254
|
+
return { events, ended };
|
|
255
|
+
}
|
|
256
|
+
/** Opens a message, closing any open one first; returns its id. */
|
|
257
|
+
*openMessage(role, parentToolCallId,
|
|
258
|
+
/** Derived id for the user echo; harness messages get a fresh UUIDv7. */
|
|
259
|
+
id,
|
|
260
|
+
/** The model the harness said produced it. */
|
|
261
|
+
model) {
|
|
262
|
+
if (this.openMessageId)
|
|
263
|
+
yield* this.closeMessage();
|
|
264
|
+
if (role === "assistant")
|
|
265
|
+
this.answered = true;
|
|
266
|
+
// One clock reading: a consumer that dates a message by its id agrees
|
|
267
|
+
// with one that dates it by this event.
|
|
268
|
+
const startedAt = Date.now();
|
|
269
|
+
const messageId = id ?? generateUUIDv7(startedAt);
|
|
270
|
+
this.openMessageId = messageId;
|
|
271
|
+
this.openMessageParent = parentToolCallId;
|
|
272
|
+
const named = role === "assistant" ? this.messageModel(model, parentToolCallId) : {};
|
|
273
|
+
this.openMessageModel = named.model;
|
|
274
|
+
this.currentOutputIndex = this.nextOutputIndex++;
|
|
275
|
+
this.nextPartIndex = 0;
|
|
276
|
+
yield {
|
|
277
|
+
type: "message.started",
|
|
278
|
+
sessionId: this.sessionId,
|
|
279
|
+
turnId: this.turnId,
|
|
280
|
+
messageId,
|
|
281
|
+
outputIndex: this.currentOutputIndex,
|
|
282
|
+
role,
|
|
283
|
+
// A parented message is a sub-agent's output — nests under its tool call,
|
|
284
|
+
// not a top-level model message.
|
|
285
|
+
// @ref LLP 0001#d5--messages-map-to-model-messages-no-silent-surface — sub-agent output nests
|
|
286
|
+
...(parentToolCallId && { parentToolCallId }),
|
|
287
|
+
// `model` is assistant/model-message metadata, never on a user message:
|
|
288
|
+
// the model the harness said wrote it (Claude's API model), else the
|
|
289
|
+
// one it runs the turn on (Codex's thread model), else the request's.
|
|
290
|
+
...named,
|
|
291
|
+
timestamp: startedAt,
|
|
292
|
+
};
|
|
293
|
+
return messageId;
|
|
294
|
+
}
|
|
295
|
+
messageModel(named, parentToolCallId) {
|
|
296
|
+
const model = named ??
|
|
297
|
+
this.writtenBy.get(parentToolCallId ?? "") ??
|
|
298
|
+
this.applied?.model ??
|
|
299
|
+
this.execution.model;
|
|
300
|
+
return model ? { model } : {};
|
|
301
|
+
}
|
|
302
|
+
*closeMessage() {
|
|
303
|
+
if (!this.openMessageId)
|
|
304
|
+
return;
|
|
305
|
+
yield {
|
|
306
|
+
type: "message.ended",
|
|
307
|
+
sessionId: this.sessionId,
|
|
308
|
+
turnId: this.turnId,
|
|
309
|
+
messageId: this.openMessageId,
|
|
310
|
+
timestamp: Date.now(),
|
|
311
|
+
};
|
|
312
|
+
this.openMessageId = undefined;
|
|
313
|
+
this.openMessageParent = undefined;
|
|
314
|
+
}
|
|
315
|
+
/** The id of an open message whose parent matches the incoming part. A part
|
|
316
|
+
* whose `parentToolCallId` differs from the open message (main↔sub-agent, or
|
|
317
|
+
* between sibling sub-agents) starts a new message so sub-agent output is
|
|
318
|
+
* grouped under its own parented message rather than mixed into another. */
|
|
319
|
+
*ensureMessage(parentToolCallId) {
|
|
320
|
+
if (this.openMessageId && this.openMessageParent !== parentToolCallId) {
|
|
321
|
+
yield* this.closeMessage();
|
|
322
|
+
}
|
|
323
|
+
return this.openMessageId ?? (yield* this.openMessage("assistant", parentToolCallId));
|
|
324
|
+
}
|
|
325
|
+
*emitPart(part) {
|
|
326
|
+
// A new part takes the next index of a matching open message, opened first if need be.
|
|
327
|
+
const address = this.parts.get(part.id)?.address ?? {
|
|
328
|
+
messageId: yield* this.ensureMessage(part.parentToolCallId),
|
|
329
|
+
outputIndex: this.currentOutputIndex,
|
|
330
|
+
partIndex: this.nextPartIndex++,
|
|
331
|
+
};
|
|
332
|
+
// Snapshot: adapters mutate their part objects in place across
|
|
333
|
+
// open/update, so we must clone at emit time or buffered consumers would
|
|
334
|
+
// all observe the final state. Stamp ownership so the part is
|
|
335
|
+
// self-describing on the wire.
|
|
336
|
+
const snapshot = structuredClone(part);
|
|
337
|
+
snapshot.sessionId = this.sessionId;
|
|
338
|
+
snapshot.messageId = address.messageId;
|
|
339
|
+
const at = Date.now();
|
|
340
|
+
// A snapshot is authoritative over the deltas before it.
|
|
341
|
+
this.parts.set(part.id, { address, ...(isStreamed(snapshot) && { shown: snapshot }) });
|
|
342
|
+
yield {
|
|
343
|
+
type: "message.part",
|
|
344
|
+
sessionId: this.sessionId,
|
|
345
|
+
turnId: this.turnId,
|
|
346
|
+
messageId: address.messageId,
|
|
347
|
+
outputIndex: address.outputIndex,
|
|
348
|
+
partIndex: address.partIndex,
|
|
349
|
+
part: snapshot,
|
|
350
|
+
timestamp: at,
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
// @ref LLP 0008#which-snapshots-are-kept — a streaming part is kept at its opening, changes and end
|
|
354
|
+
/**
|
|
355
|
+
* A delta, live only, with no snapshot of its own. What it streamed is kept
|
|
356
|
+
* aside for `finish`, which snapshots what the harness never did; a client
|
|
357
|
+
* that joins mid-part gets the part's content from the server instead.
|
|
358
|
+
*/
|
|
359
|
+
*delta(partId, delta) {
|
|
360
|
+
// Deltas only ever follow a part-open, so an unknown part id means a
|
|
361
|
+
// misbehaving adapter — drop rather than fabricate an address.
|
|
362
|
+
const record = this.parts.get(partId);
|
|
363
|
+
if (!record)
|
|
364
|
+
return;
|
|
365
|
+
// A new part each time: the emitted snapshot belongs to the consumers that received it.
|
|
366
|
+
const extended = record.shown && extendPart(record.streamed ?? record.shown, delta);
|
|
367
|
+
if (extended)
|
|
368
|
+
record.streamed = extended;
|
|
369
|
+
yield {
|
|
370
|
+
type: "message.part.delta",
|
|
371
|
+
sessionId: this.sessionId,
|
|
372
|
+
turnId: this.turnId,
|
|
373
|
+
...record.address,
|
|
374
|
+
partId,
|
|
375
|
+
delta,
|
|
376
|
+
timestamp: Date.now(),
|
|
377
|
+
};
|
|
378
|
+
}
|
|
379
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Throw this FROM a sink whose writes are a durability boundary, such as a
|
|
3
|
+
* journal: the turn stops rather than run on unrecorded, its harness is
|
|
4
|
+
* cancelled, and a retry of the same turn converges on this failure instead of
|
|
5
|
+
* running it again. Anything else a sink throws is isolated (a diagnostic).
|
|
6
|
+
*/
|
|
7
|
+
export class EventSinkError extends Error {
|
|
8
|
+
name = "EventSinkError";
|
|
9
|
+
constructor(cause) {
|
|
10
|
+
super("Required event sink failed; execution needs recovery", { cause });
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
/** Build a sink from a plain callback. */
|
|
14
|
+
export function callbackSink(fn) {
|
|
15
|
+
return { emit: fn };
|
|
16
|
+
}
|
|
17
|
+
/** A sink that accumulates events in memory — handy for tests and batch runs. */
|
|
18
|
+
export class CollectingSink {
|
|
19
|
+
events = [];
|
|
20
|
+
emit(event) {
|
|
21
|
+
this.events.push(event);
|
|
22
|
+
}
|
|
23
|
+
byType(type) {
|
|
24
|
+
return this.events.filter((e) => e.type === type);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Write one event at a time, in the order they were emitted. A permission
|
|
29
|
+
* response can arrive while its request is still being written (from inside
|
|
30
|
+
* the sink, or from another caller), so an async sink that must record the
|
|
31
|
+
* request before the response wraps itself in this.
|
|
32
|
+
*/
|
|
33
|
+
export function serialSink(sink) {
|
|
34
|
+
let tail = Promise.resolve();
|
|
35
|
+
return {
|
|
36
|
+
emit(event) {
|
|
37
|
+
const write = tail.then(() => sink.emit(event));
|
|
38
|
+
tail = write.catch(() => { });
|
|
39
|
+
return write;
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Fan out to several sinks at once. Every sink gets every event, whichever
|
|
45
|
+
* throws; a durability failure (`EventSinkError`) outranks an ordinary one.
|
|
46
|
+
* A sink that answers a permission during `emit` reenters the tee: place it
|
|
47
|
+
* after the durable sink, or wrap the tee in `serialSink`.
|
|
48
|
+
*/
|
|
49
|
+
export function teeSink(...sinks) {
|
|
50
|
+
return {
|
|
51
|
+
async emit(event) {
|
|
52
|
+
const errors = [];
|
|
53
|
+
for (const sink of sinks) {
|
|
54
|
+
try {
|
|
55
|
+
await sink.emit(event);
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
errors.push(error);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
if (errors.length)
|
|
62
|
+
throw errors.find((error) => error instanceof EventSinkError) ?? errors[0];
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { toolForHarness } from "../provision/tools.js";
|
|
2
|
+
/** One harness's CLI as the wire says it; a harness with no managed CLI is `unmanaged`. */
|
|
3
|
+
function harnessCli(harness, status) {
|
|
4
|
+
if (!status)
|
|
5
|
+
return { harness, state: "unmanaged" };
|
|
6
|
+
const version = status.version !== undefined ? { version: status.version } : {};
|
|
7
|
+
switch (status.state) {
|
|
8
|
+
case "ready":
|
|
9
|
+
return { harness, state: "ready", ...version, source: status.source };
|
|
10
|
+
case "missing":
|
|
11
|
+
return { harness, state: "missing", ...version };
|
|
12
|
+
case "installing":
|
|
13
|
+
return {
|
|
14
|
+
harness,
|
|
15
|
+
state: "installing",
|
|
16
|
+
...version,
|
|
17
|
+
receivedBytes: status.receivedBytes,
|
|
18
|
+
...(status.totalBytes !== undefined && { totalBytes: status.totalBytes }),
|
|
19
|
+
};
|
|
20
|
+
case "failed":
|
|
21
|
+
return {
|
|
22
|
+
harness,
|
|
23
|
+
state: "failed",
|
|
24
|
+
...version,
|
|
25
|
+
error: status.error,
|
|
26
|
+
category: status.category,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Each harness's CLI, after resolving it when `prepare` (an install already
|
|
32
|
+
* running is joined; a failure is reported, not thrown).
|
|
33
|
+
*/
|
|
34
|
+
export async function harnessClis(provisioner, harnesses, prepare) {
|
|
35
|
+
return Promise.all(harnesses.map(async (harness) => {
|
|
36
|
+
const tool = toolForHarness(harness);
|
|
37
|
+
if (!provisioner || !tool)
|
|
38
|
+
return harnessCli(harness, undefined);
|
|
39
|
+
if (prepare)
|
|
40
|
+
await provisioner.resolve(tool).catch(() => { });
|
|
41
|
+
return harnessCli(harness, await provisioner.status(tool));
|
|
42
|
+
}));
|
|
43
|
+
}
|
|
44
|
+
// @ref LLP 0010#a-read-installs-nothing — a read answers without the CLI rather than install it
|
|
45
|
+
/**
|
|
46
|
+
* Why a read (a model list, a login's status) of this harness waits for an
|
|
47
|
+
* install it does not start: its CLI is not ready and the provisioner names
|
|
48
|
+
* the version an install would fetch (`missing` where the engine installs,
|
|
49
|
+
* `installing`, or `failed` after an install). `undefined` where a read goes
|
|
50
|
+
* ahead: the CLI is ready, nothing installs (`system`, which names no
|
|
51
|
+
* version), or the harness has no managed CLI.
|
|
52
|
+
*/
|
|
53
|
+
export async function awaitingInstall(provisioner, harness) {
|
|
54
|
+
const tool = toolForHarness(harness);
|
|
55
|
+
if (!provisioner || !tool)
|
|
56
|
+
return undefined;
|
|
57
|
+
const status = await provisioner.status(tool).catch(() => undefined);
|
|
58
|
+
if (!status || status.state === "ready" || status.version === undefined)
|
|
59
|
+
return undefined;
|
|
60
|
+
const cli = `The ${harness} CLI ${status.version}`;
|
|
61
|
+
switch (status.state) {
|
|
62
|
+
case "missing":
|
|
63
|
+
return `${cli} is not installed yet.`;
|
|
64
|
+
case "installing":
|
|
65
|
+
return `${cli} is being installed.`;
|
|
66
|
+
case "failed":
|
|
67
|
+
return `${cli} failed to install: ${status.error}`;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/** An install's progress, once per harness that runs its CLI. */
|
|
71
|
+
export function harnessCliProgress(progress, harnesses) {
|
|
72
|
+
return harnesses
|
|
73
|
+
.filter((harness) => toolForHarness(harness) === progress.tool)
|
|
74
|
+
.map((harness) => ({
|
|
75
|
+
harness,
|
|
76
|
+
version: progress.version,
|
|
77
|
+
phase: progress.phase,
|
|
78
|
+
receivedBytes: progress.receivedBytes,
|
|
79
|
+
...(progress.totalBytes !== undefined && { totalBytes: progress.totalBytes }),
|
|
80
|
+
...(progress.error !== undefined && { error: progress.error }),
|
|
81
|
+
...(progress.category !== undefined && { category: progress.category }),
|
|
82
|
+
}));
|
|
83
|
+
}
|