capy-acp 0.0.0-stage → 0.2.0
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/LICENSE +21 -0
- package/README.md +184 -2
- package/dist/agent.js +468 -0
- package/dist/agent.js.map +1 -0
- package/dist/bin.js +74 -0
- package/dist/bin.js.map +1 -0
- package/dist/capy/client.js +174 -0
- package/dist/capy/client.js.map +1 -0
- package/dist/capy/events.js +140 -0
- package/dist/capy/events.js.map +1 -0
- package/dist/changes.js +218 -0
- package/dist/changes.js.map +1 -0
- package/dist/config.js +41 -0
- package/dist/config.js.map +1 -0
- package/dist/doctor.js +102 -0
- package/dist/doctor.js.map +1 -0
- package/dist/pricing.js +60 -0
- package/dist/pricing.js.map +1 -0
- package/dist/problems.js +108 -0
- package/dist/problems.js.map +1 -0
- package/dist/prompt.js +101 -0
- package/dist/prompt.js.map +1 -0
- package/dist/setup.js +92 -0
- package/dist/setup.js.map +1 -0
- package/dist/sse.js +95 -0
- package/dist/sse.js.map +1 -0
- package/dist/store.js +85 -0
- package/dist/store.js.map +1 -0
- package/dist/translate.js +309 -0
- package/dist/translate.js.map +1 -0
- package/dist/turn.js +319 -0
- package/dist/turn.js.map +1 -0
- package/dist/version.js +5 -0
- package/dist/version.js.map +1 -0
- package/dist/workspace.js +164 -0
- package/dist/workspace.js.map +1 -0
- package/package.json +48 -4
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
import { messageFiles } from "./capy/events.js";
|
|
2
|
+
export const initialTurn = (cursor) => ({
|
|
3
|
+
cursor,
|
|
4
|
+
status: undefined,
|
|
5
|
+
active: false,
|
|
6
|
+
assistant: new Map(),
|
|
7
|
+
settled: new Set(),
|
|
8
|
+
endPending: false,
|
|
9
|
+
lastAssistantCallId: undefined,
|
|
10
|
+
tools: new Map(),
|
|
11
|
+
cancelled: false
|
|
12
|
+
});
|
|
13
|
+
const READ_TOOLS = new Set(["read", "view", "cat", "open"]);
|
|
14
|
+
const SEARCH_TOOLS = new Set(["grep", "glob", "search", "find", "ls", "list"]);
|
|
15
|
+
const FETCH_TOOLS = new Set(["fetch", "web_fetch", "web_search", "browse"]);
|
|
16
|
+
/**
|
|
17
|
+
* The ACP kind for a Capy tool call, from the inner tools it ran. Shell and write tools map to
|
|
18
|
+
* `other`, because Capy gives no command or path to fill `execute` and `edit` items with.
|
|
19
|
+
*/
|
|
20
|
+
export const toolKind = (names) => {
|
|
21
|
+
const lower = names.map((name) => name.toLowerCase());
|
|
22
|
+
if (lower.length > 0 && lower.every((name) => READ_TOOLS.has(name)))
|
|
23
|
+
return "read";
|
|
24
|
+
if (lower.length > 0 && lower.every((name) => SEARCH_TOOLS.has(name) || READ_TOOLS.has(name)))
|
|
25
|
+
return "search";
|
|
26
|
+
if (lower.length > 0 && lower.every((name) => FETCH_TOOLS.has(name)))
|
|
27
|
+
return "fetch";
|
|
28
|
+
if (lower.length > 0 && lower.every((name) => name === "think"))
|
|
29
|
+
return "think";
|
|
30
|
+
return "other";
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* How a committed tool entry ended. Each `execute` call that ran to its end lists its inner tools with
|
|
34
|
+
* their durations, and Capy commits a call that an interrupt stopped with no `calls` list at all (seen
|
|
35
|
+
* in every cancelled test turn). An entry that names its tool directly (Haiku's `bash`) never lists
|
|
36
|
+
* inner tools, so after a cancel its outcome is unknown, and it shows as failed rather than finished.
|
|
37
|
+
*/
|
|
38
|
+
export const toolStatus = (message, cancelled) => {
|
|
39
|
+
if ((message.calls?.length ?? 0) > 0)
|
|
40
|
+
return "completed";
|
|
41
|
+
if (message.tool === "execute")
|
|
42
|
+
return "failed";
|
|
43
|
+
return cancelled ? "failed" : "completed";
|
|
44
|
+
};
|
|
45
|
+
const formatSeconds = (ms) => (ms < 1000 ? `${Math.round(ms)} ms` : `${(ms / 1000).toFixed(1)} s`);
|
|
46
|
+
/** Title for a committed tool entry: its summary when it has one, else the inner tools it ran. */
|
|
47
|
+
export const toolTitle = (message) => {
|
|
48
|
+
const summary = message.text?.trim() ?? "";
|
|
49
|
+
if (summary !== "")
|
|
50
|
+
return summary;
|
|
51
|
+
const calls = message.calls ?? [];
|
|
52
|
+
if (calls.length === 0)
|
|
53
|
+
return message.tool === "execute" ? "Stopped tool call" : (message.tool ?? "Tool call");
|
|
54
|
+
const names = [...new Set(calls.map((call) => call.tool))].join(", ");
|
|
55
|
+
const total = calls.reduce((sum, call) => sum + (call.durationMs ?? 0), 0);
|
|
56
|
+
return total > 0 ? `${names} (${formatSeconds(total)})` : names;
|
|
57
|
+
};
|
|
58
|
+
const liveTitle = (name, text) => {
|
|
59
|
+
const summary = text.trim();
|
|
60
|
+
if (summary !== "")
|
|
61
|
+
return summary;
|
|
62
|
+
return name === "execute" ? "Running tools" : name;
|
|
63
|
+
};
|
|
64
|
+
const chunk = (text) => ({
|
|
65
|
+
sessionUpdate: "agent_message_chunk",
|
|
66
|
+
content: { type: "text", text }
|
|
67
|
+
});
|
|
68
|
+
/** Emits assistant text, separating consecutive replies (Astra sends interim messages). */
|
|
69
|
+
const emitAssistant = (state, callId, text) => {
|
|
70
|
+
if (text === "")
|
|
71
|
+
return { state, updates: [] };
|
|
72
|
+
const previous = state.assistant.get(callId) ?? "";
|
|
73
|
+
const separate = previous === "" && state.lastAssistantCallId !== undefined && state.lastAssistantCallId !== callId;
|
|
74
|
+
const assistant = new Map(state.assistant).set(callId, previous + text);
|
|
75
|
+
return {
|
|
76
|
+
state: { ...state, active: true, assistant, lastAssistantCallId: callId },
|
|
77
|
+
updates: [chunk(separate ? `\n\n${text}` : text)]
|
|
78
|
+
};
|
|
79
|
+
};
|
|
80
|
+
const unsettled = (state) => [...state.assistant.keys()].filter((id) => !state.settled.has(id)).length;
|
|
81
|
+
/** Every reply that streamed has committed. */
|
|
82
|
+
export const turnComplete = (state) => unsettled(state) === 0;
|
|
83
|
+
const endIfComplete = (step) => step.state.endPending && turnComplete(step.state)
|
|
84
|
+
? { ...step, state: { ...step.state, endPending: false }, signal: { _tag: "Ended" } }
|
|
85
|
+
: step;
|
|
86
|
+
const onTranscript = (state, cursor, message) => {
|
|
87
|
+
const base = { ...state, cursor: cursor ?? state.cursor };
|
|
88
|
+
switch (message.source) {
|
|
89
|
+
case "user":
|
|
90
|
+
// Our own message committing proves the agent picked the turn up.
|
|
91
|
+
return { state: { ...base, active: true }, updates: [] };
|
|
92
|
+
case "assistant": {
|
|
93
|
+
const callId = message.callId ?? message.id;
|
|
94
|
+
const text = message.text ?? "";
|
|
95
|
+
// A resumed stream can deliver a settled entry again; its files were already shown.
|
|
96
|
+
const files = base.settled.has(callId) ? [] : messageFiles(message);
|
|
97
|
+
const streamed = base.assistant.get(callId) ?? "";
|
|
98
|
+
// If the committed text diverged from the fragments, keep what streamed: chunks cannot be retracted.
|
|
99
|
+
const emitted = text.startsWith(streamed)
|
|
100
|
+
? emitAssistant(base, callId, text.slice(streamed.length))
|
|
101
|
+
: { state: { ...base, active: true }, updates: [] };
|
|
102
|
+
const state = {
|
|
103
|
+
...emitted.state,
|
|
104
|
+
assistant: new Map(emitted.state.assistant).set(callId, emitted.state.assistant.get(callId) ?? text),
|
|
105
|
+
settled: new Set(emitted.state.settled).add(callId)
|
|
106
|
+
};
|
|
107
|
+
return endIfComplete({ state, updates: emitted.updates, ...(files.length > 0 ? { files } : {}) });
|
|
108
|
+
}
|
|
109
|
+
case "tool": {
|
|
110
|
+
const known = base.tools.get(message.id);
|
|
111
|
+
// Report a tool's completion once. A resumed stream can deliver its entry again, and a repeat
|
|
112
|
+
// would replace the file diffs the runner attached to it.
|
|
113
|
+
if (known?.done === true)
|
|
114
|
+
return { state: { ...base, active: true }, updates: [] };
|
|
115
|
+
const names = message.calls?.map((call) => call.tool) ?? (message.tool === undefined ? [] : [message.tool]);
|
|
116
|
+
const title = toolTitle(message);
|
|
117
|
+
const kind = toolKind(names.length > 0 ? names : [message.tool ?? ""]);
|
|
118
|
+
const status = toolStatus(message, base.cancelled);
|
|
119
|
+
const tools = new Map(base.tools).set(message.id, { title, kind, done: true });
|
|
120
|
+
const next = { ...base, active: true, tools };
|
|
121
|
+
const fields = { toolCallId: message.id, title, kind, status };
|
|
122
|
+
return {
|
|
123
|
+
state: next,
|
|
124
|
+
updates: [known === undefined ? { sessionUpdate: "tool_call", ...fields } : { sessionUpdate: "tool_call_update", ...fields }]
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
/** Subagent ids are prefixed so they can never collide with Capy's own tool call ids. */
|
|
130
|
+
export const subagentCallId = (id) => `capy-subagent-${id}`;
|
|
131
|
+
const SUBAGENT_STATUS = {
|
|
132
|
+
draft: "pending",
|
|
133
|
+
working: "in_progress",
|
|
134
|
+
waiting: "in_progress",
|
|
135
|
+
idle: "completed",
|
|
136
|
+
done: "completed",
|
|
137
|
+
failed: "failed"
|
|
138
|
+
};
|
|
139
|
+
/** A subagent shows as one tool call that follows its lifecycle. */
|
|
140
|
+
const onSubagent = (state, event) => {
|
|
141
|
+
const toolCallId = subagentCallId(event.id);
|
|
142
|
+
const known = state.tools.get(toolCallId);
|
|
143
|
+
const mapped = SUBAGENT_STATUS[event.status] ?? "in_progress";
|
|
144
|
+
const ran = known?.ran === true || mapped === "in_progress";
|
|
145
|
+
// `idle` means at rest: a new subagent is idle before it starts (seen live), so only one that ran has finished.
|
|
146
|
+
const status = event.status === "idle" && !ran ? "pending" : mapped;
|
|
147
|
+
const done = status === "completed" || status === "failed";
|
|
148
|
+
if ((known?.done === true && done) || known?.shown === status)
|
|
149
|
+
return { state, updates: [] };
|
|
150
|
+
const title = `Subagent ${event.path ?? event.id}`;
|
|
151
|
+
const tools = new Map(state.tools).set(toolCallId, { title, kind: "other", done, shown: status, ran });
|
|
152
|
+
const update = known === undefined
|
|
153
|
+
? { sessionUpdate: "tool_call", toolCallId, title, kind: "other", status }
|
|
154
|
+
: { sessionUpdate: "tool_call_update", toolCallId, status };
|
|
155
|
+
return { state: { ...state, tools }, updates: [update] };
|
|
156
|
+
};
|
|
157
|
+
export const step = (state, event) => {
|
|
158
|
+
switch (event._tag) {
|
|
159
|
+
case "Assistant":
|
|
160
|
+
return emitAssistant(state, event.callId, event.text);
|
|
161
|
+
case "ToolCall": {
|
|
162
|
+
if (state.tools.has(event.callId))
|
|
163
|
+
return { state, updates: [] };
|
|
164
|
+
const title = liveTitle(event.name, event.text);
|
|
165
|
+
const kind = toolKind([event.name]);
|
|
166
|
+
return {
|
|
167
|
+
state: { ...state, active: true, tools: new Map(state.tools).set(event.callId, { title, kind, done: false }) },
|
|
168
|
+
updates: [{ sessionUpdate: "tool_call", toolCallId: event.callId, title, kind, status: "in_progress" }]
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
case "Transcript":
|
|
172
|
+
return onTranscript(state, event.cursor, event.message);
|
|
173
|
+
case "Status": {
|
|
174
|
+
const next = { ...state, status: event.status, active: state.active || event.status === "working" };
|
|
175
|
+
// The first status frame on a fresh connection can still read `idle` before the agent picks up
|
|
176
|
+
// the message, so idle/failed end the turn only after the turn became active.
|
|
177
|
+
if (event.status === "idle" && next.active) {
|
|
178
|
+
return turnComplete(next)
|
|
179
|
+
? { state: next, updates: [], signal: { _tag: "Ended" } }
|
|
180
|
+
: { state: { ...next, endPending: true }, updates: [] };
|
|
181
|
+
}
|
|
182
|
+
if (event.status === "failed" && next.active)
|
|
183
|
+
return { state: next, updates: [], signal: { _tag: "Failed" } };
|
|
184
|
+
if (event.status === "waiting" && next.active)
|
|
185
|
+
return { state: next, updates: [], signal: { _tag: "Waiting" } };
|
|
186
|
+
if (event.status === "archived")
|
|
187
|
+
return { state: next, updates: [], signal: { _tag: "Archived" } };
|
|
188
|
+
return { state: next, updates: [] };
|
|
189
|
+
}
|
|
190
|
+
case "Subagent":
|
|
191
|
+
return onSubagent(state, event);
|
|
192
|
+
case "StreamError":
|
|
193
|
+
return { state, updates: [], signal: { _tag: "StreamError", code: event.code, message: event.message } };
|
|
194
|
+
case "Done":
|
|
195
|
+
return { state, updates: [], signal: { _tag: "StreamDone" } };
|
|
196
|
+
case "Run":
|
|
197
|
+
case "Turn":
|
|
198
|
+
case "Heartbeat":
|
|
199
|
+
case "Unknown":
|
|
200
|
+
return { state, updates: [] };
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
const MAX_TITLE_FILES = 3;
|
|
204
|
+
/** "Edited README.md and docs/SPIKE.md", "Created a.ts, b.ts and 2 more files". */
|
|
205
|
+
export const changeTitle = (changes) => {
|
|
206
|
+
const verb = changes.every((c) => c.operation === "add")
|
|
207
|
+
? "Created"
|
|
208
|
+
: changes.every((c) => c.operation === "delete")
|
|
209
|
+
? "Deleted"
|
|
210
|
+
: "Edited";
|
|
211
|
+
const names = changes.map((c) => c.relativePath);
|
|
212
|
+
if (names.length <= MAX_TITLE_FILES) {
|
|
213
|
+
return `${verb} ${names.length === 1 ? names[0] : `${names.slice(0, -1).join(", ")} and ${names.at(-1)}`}`;
|
|
214
|
+
}
|
|
215
|
+
const rest = names.length - (MAX_TITLE_FILES - 1);
|
|
216
|
+
return `${verb} ${names.slice(0, MAX_TITLE_FILES - 1).join(", ")} and ${rest} more files`;
|
|
217
|
+
};
|
|
218
|
+
/**
|
|
219
|
+
* The ACP update that shows a tool call's file changes as diffs. `status` keeps a call that was closed
|
|
220
|
+
* as failed (cancelled, failed run) from reading as completed. With `outside`, it opens an item of its
|
|
221
|
+
* own for changes that no Capy tool call made: they happened while no call that can write ran, so
|
|
222
|
+
* they are the user's, or a background command's. Its kind is `other`, so that clients show the title,
|
|
223
|
+
* which says so, and not a plain "changed files".
|
|
224
|
+
*/
|
|
225
|
+
export const fileChangeUpdate = (toolCallId, changes, options = {}) => {
|
|
226
|
+
const content = changes.flatMap((change) => change.previewable
|
|
227
|
+
? [{
|
|
228
|
+
type: "diff",
|
|
229
|
+
path: change.path,
|
|
230
|
+
...(change.oldText === undefined ? {} : { oldText: change.oldText }),
|
|
231
|
+
newText: change.newText ?? ""
|
|
232
|
+
}]
|
|
233
|
+
: []);
|
|
234
|
+
const locations = changes.map((change) => ({ path: change.path }));
|
|
235
|
+
if (options.outside === true) {
|
|
236
|
+
const title = `${changeTitle(changes)} outside a Capy tool call`;
|
|
237
|
+
return { sessionUpdate: "tool_call", toolCallId, title, kind: "other", status: "completed", content, locations };
|
|
238
|
+
}
|
|
239
|
+
return {
|
|
240
|
+
sessionUpdate: "tool_call_update",
|
|
241
|
+
toolCallId,
|
|
242
|
+
title: changeTitle(changes),
|
|
243
|
+
kind: changes.every((c) => c.operation === "delete") ? "delete" : "edit",
|
|
244
|
+
status: options.status ?? "completed",
|
|
245
|
+
content,
|
|
246
|
+
locations
|
|
247
|
+
};
|
|
248
|
+
};
|
|
249
|
+
/** The tool calls an update set closes, in order. */
|
|
250
|
+
export const closedTools = (updates) => updates.flatMap((u) => (u.sessionUpdate === "tool_call" || u.sessionUpdate === "tool_call_update") && (u.status === "completed" || u.status === "failed")
|
|
251
|
+
? [{ toolCallId: u.toolCallId, status: u.status, kind: u.kind ?? undefined }]
|
|
252
|
+
: []);
|
|
253
|
+
/** Closes tool calls still in progress when the turn ends: completed on success, failed otherwise. */
|
|
254
|
+
export const finishTools = (state, outcome) => {
|
|
255
|
+
const updates = [];
|
|
256
|
+
const tools = new Map(state.tools);
|
|
257
|
+
for (const [toolCallId, tool] of state.tools) {
|
|
258
|
+
if (tool.done)
|
|
259
|
+
continue;
|
|
260
|
+
tools.set(toolCallId, { ...tool, done: true });
|
|
261
|
+
updates.push({ sessionUpdate: "tool_call_update", toolCallId, status: outcome });
|
|
262
|
+
}
|
|
263
|
+
return { state: { ...state, tools }, updates };
|
|
264
|
+
};
|
|
265
|
+
/**
|
|
266
|
+
* A committed transcript as ACP history, for `session/load`: user and assistant text as message
|
|
267
|
+
* chunks, tool entries as completed tool calls. Files are left to the caller, as in a live turn.
|
|
268
|
+
*/
|
|
269
|
+
export const history = (messages) => {
|
|
270
|
+
const out = [];
|
|
271
|
+
let previous;
|
|
272
|
+
for (const message of messages) {
|
|
273
|
+
const files = messageFiles(message);
|
|
274
|
+
const withFiles = files.length > 0 ? { files } : {};
|
|
275
|
+
const text = message.text ?? "";
|
|
276
|
+
if (message.source === "tool") {
|
|
277
|
+
const names = message.calls?.map((call) => call.tool) ?? (message.tool === undefined ? [] : [message.tool]);
|
|
278
|
+
out.push({
|
|
279
|
+
source: "assistant",
|
|
280
|
+
update: { sessionUpdate: "tool_call", toolCallId: message.id, title: toolTitle(message), kind: toolKind(names), status: toolStatus(message, false) }
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
else if (message.source === "user") {
|
|
284
|
+
out.push({ source: "user", update: { sessionUpdate: "user_message_chunk", content: { type: "text", text } }, ...withFiles });
|
|
285
|
+
}
|
|
286
|
+
else {
|
|
287
|
+
// Consecutive replies with nothing between them read as one message unless separated.
|
|
288
|
+
const separated = previous === "assistant" ? `\n\n${text}` : text;
|
|
289
|
+
out.push({ source: "assistant", update: { sessionUpdate: "agent_message_chunk", content: { type: "text", text: separated } }, ...withFiles });
|
|
290
|
+
}
|
|
291
|
+
previous = message.source;
|
|
292
|
+
}
|
|
293
|
+
return out;
|
|
294
|
+
};
|
|
295
|
+
/** Replays a whole recorded stream; used by tests and the replay CLI. */
|
|
296
|
+
export const replay = (events, cursor) => {
|
|
297
|
+
let state = initialTurn(cursor);
|
|
298
|
+
const updates = [];
|
|
299
|
+
const signals = [];
|
|
300
|
+
for (const event of events) {
|
|
301
|
+
const result = step(state, event);
|
|
302
|
+
state = result.state;
|
|
303
|
+
updates.push(...result.updates);
|
|
304
|
+
if (result.signal !== undefined)
|
|
305
|
+
signals.push(result.signal);
|
|
306
|
+
}
|
|
307
|
+
return { state, updates, signals };
|
|
308
|
+
};
|
|
309
|
+
//# sourceMappingURL=translate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"translate.js","sourceRoot":"","sources":["../src/translate.ts"],"names":[],"mappings":"AAsBA,OAAO,EAAwD,YAAY,EAAqB,MAAM,kBAAkB,CAAA;A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AK,CAAC,CAAA;QACjC,KAAK,GAAG,MAAM,CAAC,KAAK,CAAA;QACpB,OAAO,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC,OAAO,CAAC,CAAA;QAC/B,IAAI,MAAM,CAAC,MAAM,KAAK,SAAS;YAAE,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,CAAA;IAC9D,CAAC;IACD,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,CAAA;AACpC,CAAC,CAAA","sourcesContent":["/**\n * Translates one prompt turn's Capy stream events into ACP `session/update` payloads.\n *\n * Pure and synchronous so recorded streams replay deterministically in tests. Rules come from\n * the spike recordings (see docs/capy-api-notes.md):\n * - The turn is driven by `status` and `transcript` frames. `run`/`turn` framing is unreliable\n * at stream start (stray aborted turns, missing or late `run start`), so it is ignored.\n * - Live `assistant` fragments stream as message chunks. A committed assistant entry only adds\n * text the fragments missed (fast finishes, resumed streams, dropped frames).\n * - Capy commits the final reply's transcript entry after the `idle` status, so `Ended` waits for\n * every streamed reply to commit; otherwise the next turn's resume cursor would replay it. A reply\n * that streamed no fragments (a `dropped` turn) can still commit after `Ended`: the runner keeps\n * reading until the server closes the stream (see turn.ts).\n * - Strong models report every tool as `execute` with empty text; the committed tool entry's\n * `calls` list names the inner tools, so the title is upgraded when it lands. An `execute` entry\n * without that list is a call an interrupt stopped (see `toolStatus`).\n * - Capy never sends a tool's command line, file paths or output. Kinds that promise those\n * (`execute`, `edit`) are therefore not used for Capy's own frames: clients render them as an\n * empty command or a nameless file. File edits are reported by `fileChangeUpdate` instead, from\n * changes the adapter observes on disk (see changes.ts).\n */\nimport type { SessionUpdate, ToolCallContent, ToolCallStatus, ToolKind } from \"@agentclientprotocol/sdk\"\nimport { type CapyEvent, type Message, type MessageAttachment, messageFiles, type ThreadStatus } from \"./capy/events.js\"\nimport type { FileChange } from \"./changes.js\"\n\nexport type TurnSignal =\n | { readonly _tag: \"Ended\" }\n | { readonly _tag: \"Failed\" }\n | { readonly _tag: \"Waiting\" }\n | { readonly _tag: \"Archived\" }\n | { readonly _tag: \"StreamDone\" }\n | { readonly _tag: \"StreamError\"; readonly code: string; readonly message: string }\n\ninterface ToolState {\n readonly title: string\n readonly kind: ToolKind\n readonly done: boolean\n /** Subagents only: the status last shown, and whether it was ever seen running. */\n readonly shown?: string\n readonly ran?: boolean\n}\n\nexport interface TurnState {\n /** Last transcript cursor seen; the next stream resumes after it. */\n readonly cursor: string | undefined\n readonly status: ThreadStatus | undefined\n /** True once the agent has demonstrably picked up this turn's message. */\n readonly active: boolean\n readonly assistant: ReadonlyMap<string, string>\n /** Assistant callIds whose transcript entry has committed. */\n readonly settled: ReadonlySet<string>\n /** Saw `idle` while a streamed reply had not committed yet; end once it does. */\n readonly endPending: boolean\n readonly lastAssistantCallId: string | undefined\n readonly tools: ReadonlyMap<string, ToolState>\n /** The client cancelled the prompt. */\n readonly cancelled: boolean\n}\n\nexport interface Step {\n readonly state: TurnState\n readonly updates: ReadonlyArray<SessionUpdate>\n readonly signal?: TurnSignal\n /** Files the agent delivered with a reply. Linking them needs I/O, so the runner emits them. */\n readonly files?: ReadonlyArray<MessageAttachment>\n}\n\nexport const initialTurn = (cursor: string | undefined): TurnState => ({\n cursor,\n status: undefined,\n active: false,\n assistant: new Map(),\n settled: new Set(),\n endPending: false,\n lastAssistantCallId: undefined,\n tools: new Map(),\n cancelled: false\n})\n\nconst READ_TOOLS = new Set([\"read\", \"view\", \"cat\", \"open\"])\nconst SEARCH_TOOLS = new Set([\"grep\", \"glob\", \"search\", \"find\", \"ls\", \"list\"])\nconst FETCH_TOOLS = new Set([\"fetch\", \"web_fetch\", \"web_search\", \"browse\"])\n\n/**\n * The ACP kind for a Capy tool call, from the inner tools it ran. Shell and write tools map to\n * `other`, because Capy gives no command or path to fill `execute` and `edit` items with.\n */\nexport const toolKind = (names: ReadonlyArray<string>): ToolKind => {\n const lower = names.map((name) => name.toLowerCase())\n if (lower.length > 0 && lower.every((name) => READ_TOOLS.has(name))) return \"read\"\n if (lower.length > 0 && lower.every((name) => SEARCH_TOOLS.has(name) || READ_TOOLS.has(name))) return \"search\"\n if (lower.length > 0 && lower.every((name) => FETCH_TOOLS.has(name))) return \"fetch\"\n if (lower.length > 0 && lower.every((name) => name === \"think\")) return \"think\"\n return \"other\"\n}\n\n/**\n * How a committed tool entry ended. Each `execute` call that ran to its end lists its inner tools with\n * their durations, and Capy commits a call that an interrupt stopped with no `calls` list at all (seen\n * in every cancelled test turn). An entry that names its tool directly (Haiku's `bash`) never lists\n * inner tools, so after a cancel its outcome is unknown, and it shows as failed rather than finished.\n */\nexport const toolStatus = (message: Message, cancelled: boolean): \"completed\" | \"failed\" => {\n if ((message.calls?.length ?? 0) > 0) return \"completed\"\n if (message.tool === \"execute\") return \"failed\"\n return cancelled ? \"failed\" : \"completed\"\n}\n\nconst formatSeconds = (ms: number) => (ms < 1000 ? `${Math.round(ms)} ms` : `${(ms / 1000).toFixed(1)} s`)\n\n/** Title for a committed tool entry: its summary when it has one, else the inner tools it ran. */\nexport const toolTitle = (message: Message): string => {\n const summary = message.text?.trim() ?? \"\"\n if (summary !== \"\") return summary\n const calls = message.calls ?? []\n if (calls.length === 0) return message.tool === \"execute\" ? \"Stopped tool call\" : (message.tool ?? \"Tool call\")\n const names = [...new Set(calls.map((call) => call.tool))].join(\", \")\n const total = calls.reduce((sum, call) => sum + (call.durationMs ?? 0), 0)\n return total > 0 ? `${names} (${formatSeconds(total)})` : names\n}\n\nconst liveTitle = (name: string, text: string): string => {\n const summary = text.trim()\n if (summary !== \"\") return summary\n return name === \"execute\" ? \"Running tools\" : name\n}\n\nconst chunk = (text: string): SessionUpdate => ({\n sessionUpdate: \"agent_message_chunk\",\n content: { type: \"text\", text }\n})\n\n/** Emits assistant text, separating consecutive replies (Astra sends interim messages). */\nconst emitAssistant = (state: TurnState, callId: string, text: string): Step => {\n if (text === \"\") return { state, updates: [] }\n const previous = state.assistant.get(callId) ?? \"\"\n const separate = previous === \"\" && state.lastAssistantCallId !== undefined && state.lastAssistantCallId !== callId\n const assistant = new Map(state.assistant).set(callId, previous + text)\n return {\n state: { ...state, active: true, assistant, lastAssistantCallId: callId },\n updates: [chunk(separate ? `\\n\\n${text}` : text)]\n }\n}\n\nconst unsettled = (state: TurnState) => [...state.assistant.keys()].filter((id) => !state.settled.has(id)).length\n\n/** Every reply that streamed has committed. */\nexport const turnComplete = (state: TurnState) => unsettled(state) === 0\n\nconst endIfComplete = (step: Step): Step =>\n step.state.endPending && turnComplete(step.state)\n ? { ...step, state: { ...step.state, endPending: false }, signal: { _tag: \"Ended\" } }\n : step\n\nconst onTranscript = (state: TurnState, cursor: string | undefined, message: Message): Step => {\n const base: TurnState = { ...state, cursor: cursor ?? state.cursor }\n switch (message.source) {\n case \"user\":\n // Our own message committing proves the agent picked the turn up.\n return { state: { ...base, active: true }, updates: [] }\n case \"assistant\": {\n const callId = message.callId ?? message.id\n const text = message.text ?? \"\"\n // A resumed stream can deliver a settled entry again; its files were already shown.\n const files = base.settled.has(callId) ? [] : messageFiles(message)\n const streamed = base.assistant.get(callId) ?? \"\"\n // If the committed text diverged from the fragments, keep what streamed: chunks cannot be retracted.\n const emitted = text.startsWith(streamed)\n ? emitAssistant(base, callId, text.slice(streamed.length))\n : { state: { ...base, active: true }, updates: [] }\n const state: TurnState = {\n ...emitted.state,\n assistant: new Map(emitted.state.assistant).set(callId, emitted.state.assistant.get(callId) ?? text),\n settled: new Set(emitted.state.settled).add(callId)\n }\n return endIfComplete({ state, updates: emitted.updates, ...(files.length > 0 ? { files } : {}) })\n }\n case \"tool\": {\n const known = base.tools.get(message.id)\n // Report a tool's completion once. A resumed stream can deliver its entry again, and a repeat\n // would replace the file diffs the runner attached to it.\n if (known?.done === true) return { state: { ...base, active: true }, updates: [] }\n const names = message.calls?.map((call) => call.tool) ?? (message.tool === undefined ? [] : [message.tool])\n const title = toolTitle(message)\n const kind = toolKind(names.length > 0 ? names : [message.tool ?? \"\"])\n const status = toolStatus(message, base.cancelled)\n const tools = new Map(base.tools).set(message.id, { title, kind, done: true })\n const next: TurnState = { ...base, active: true, tools }\n const fields = { toolCallId: message.id, title, kind, status }\n return {\n state: next,\n updates: [known === undefined ? { sessionUpdate: \"tool_call\", ...fields } : { sessionUpdate: \"tool_call_update\", ...fields }]\n }\n }\n }\n}\n\n/** Subagent ids are prefixed so they can never collide with Capy's own tool call ids. */\nexport const subagentCallId = (id: string) => `capy-subagent-${id}`\n\nconst SUBAGENT_STATUS: Record<string, \"pending\" | \"in_progress\" | \"completed\" | \"failed\"> = {\n draft: \"pending\",\n working: \"in_progress\",\n waiting: \"in_progress\",\n idle: \"completed\",\n done: \"completed\",\n failed: \"failed\"\n}\n\n/** A subagent shows as one tool call that follows its lifecycle. */\nconst onSubagent = (state: TurnState, event: Extract<CapyEvent, { _tag: \"Subagent\" }>): Step => {\n const toolCallId = subagentCallId(event.id)\n const known = state.tools.get(toolCallId)\n const mapped = SUBAGENT_STATUS[event.status] ?? \"in_progress\"\n const ran = known?.ran === true || mapped === \"in_progress\"\n // `idle` means at rest: a new subagent is idle before it starts (seen live), so only one that ran has finished.\n const status = event.status === \"idle\" && !ran ? \"pending\" : mapped\n const done = status === \"completed\" || status === \"failed\"\n if ((known?.done === true && done) || known?.shown === status) return { state, updates: [] }\n const title = `Subagent ${event.path ?? event.id}`\n const tools = new Map(state.tools).set(toolCallId, { title, kind: \"other\" as const, done, shown: status, ran })\n const update: SessionUpdate = known === undefined\n ? { sessionUpdate: \"tool_call\", toolCallId, title, kind: \"other\", status }\n : { sessionUpdate: \"tool_call_update\", toolCallId, status }\n return { state: { ...state, tools }, updates: [update] }\n}\n\nexport const step = (state: TurnState, event: CapyEvent): Step => {\n switch (event._tag) {\n case \"Assistant\":\n return emitAssistant(state, event.callId, event.text)\n case \"ToolCall\": {\n if (state.tools.has(event.callId)) return { state, updates: [] }\n const title = liveTitle(event.name, event.text)\n const kind = toolKind([event.name])\n return {\n state: { ...state, active: true, tools: new Map(state.tools).set(event.callId, { title, kind, done: false }) },\n updates: [{ sessionUpdate: \"tool_call\", toolCallId: event.callId, title, kind, status: \"in_progress\" }]\n }\n }\n case \"Transcript\":\n return onTranscript(state, event.cursor, event.message)\n case \"Status\": {\n const next: TurnState = { ...state, status: event.status, active: state.active || event.status === \"working\" }\n // The first status frame on a fresh connection can still read `idle` before the agent picks up\n // the message, so idle/failed end the turn only after the turn became active.\n if (event.status === \"idle\" && next.active) {\n return turnComplete(next)\n ? { state: next, updates: [], signal: { _tag: \"Ended\" } }\n : { state: { ...next, endPending: true }, updates: [] }\n }\n if (event.status === \"failed\" && next.active) return { state: next, updates: [], signal: { _tag: \"Failed\" } }\n if (event.status === \"waiting\" && next.active) return { state: next, updates: [], signal: { _tag: \"Waiting\" } }\n if (event.status === \"archived\") return { state: next, updates: [], signal: { _tag: \"Archived\" } }\n return { state: next, updates: [] }\n }\n case \"Subagent\":\n return onSubagent(state, event)\n case \"StreamError\":\n return { state, updates: [], signal: { _tag: \"StreamError\", code: event.code, message: event.message } }\n case \"Done\":\n return { state, updates: [], signal: { _tag: \"StreamDone\" } }\n case \"Run\":\n case \"Turn\":\n case \"Heartbeat\":\n case \"Unknown\":\n return { state, updates: [] }\n }\n}\n\nconst MAX_TITLE_FILES = 3\n\n/** \"Edited README.md and docs/SPIKE.md\", \"Created a.ts, b.ts and 2 more files\". */\nexport const changeTitle = (changes: ReadonlyArray<FileChange>): string => {\n const verb = changes.every((c) => c.operation === \"add\")\n ? \"Created\"\n : changes.every((c) => c.operation === \"delete\")\n ? \"Deleted\"\n : \"Edited\"\n const names = changes.map((c) => c.relativePath)\n if (names.length <= MAX_TITLE_FILES) {\n return `${verb} ${names.length === 1 ? names[0] : `${names.slice(0, -1).join(\", \")} and ${names.at(-1)}`}`\n }\n const rest = names.length - (MAX_TITLE_FILES - 1)\n return `${verb} ${names.slice(0, MAX_TITLE_FILES - 1).join(\", \")} and ${rest} more files`\n}\n\n/**\n * The ACP update that shows a tool call's file changes as diffs. `status` keeps a call that was closed\n * as failed (cancelled, failed run) from reading as completed. With `outside`, it opens an item of its\n * own for changes that no Capy tool call made: they happened while no call that can write ran, so\n * they are the user's, or a background command's. Its kind is `other`, so that clients show the title,\n * which says so, and not a plain \"changed files\".\n */\nexport const fileChangeUpdate = (\n toolCallId: string,\n changes: ReadonlyArray<FileChange>,\n options: { readonly outside?: boolean; readonly status?: \"completed\" | \"failed\" } = {}\n): SessionUpdate => {\n const content: Array<ToolCallContent> = changes.flatMap((change) =>\n change.previewable\n ? [{\n type: \"diff\" as const,\n path: change.path,\n ...(change.oldText === undefined ? {} : { oldText: change.oldText }),\n newText: change.newText ?? \"\"\n }]\n : []\n )\n const locations = changes.map((change) => ({ path: change.path }))\n if (options.outside === true) {\n const title = `${changeTitle(changes)} outside a Capy tool call`\n return { sessionUpdate: \"tool_call\", toolCallId, title, kind: \"other\", status: \"completed\", content, locations }\n }\n return {\n sessionUpdate: \"tool_call_update\",\n toolCallId,\n title: changeTitle(changes),\n kind: changes.every((c) => c.operation === \"delete\") ? \"delete\" : \"edit\",\n status: options.status ?? \"completed\",\n content,\n locations\n }\n}\n\nexport interface ClosedTool {\n readonly toolCallId: string\n readonly status: Extract<ToolCallStatus, \"completed\" | \"failed\">\n /** Set when the update names it; subagent updates carry the status only. */\n readonly kind: ToolKind | undefined\n}\n\n/** The tool calls an update set closes, in order. */\nexport const closedTools = (updates: ReadonlyArray<SessionUpdate>): ReadonlyArray<ClosedTool> =>\n updates.flatMap((u) =>\n (u.sessionUpdate === \"tool_call\" || u.sessionUpdate === \"tool_call_update\") && (u.status === \"completed\" || u.status === \"failed\")\n ? [{ toolCallId: u.toolCallId, status: u.status, kind: u.kind ?? undefined }]\n : []\n )\n\n/** Closes tool calls still in progress when the turn ends: completed on success, failed otherwise. */\nexport const finishTools = (state: TurnState, outcome: \"completed\" | \"failed\"): Step => {\n const updates: Array<SessionUpdate> = []\n const tools = new Map(state.tools)\n for (const [toolCallId, tool] of state.tools) {\n if (tool.done) continue\n tools.set(toolCallId, { ...tool, done: true })\n updates.push({ sessionUpdate: \"tool_call_update\", toolCallId, status: outcome })\n }\n return { state: { ...state, tools }, updates }\n}\n\n/**\n * A committed transcript as ACP history, for `session/load`: user and assistant text as message\n * chunks, tool entries as completed tool calls. Files are left to the caller, as in a live turn.\n */\nexport const history = (messages: ReadonlyArray<Message>): ReadonlyArray<{ readonly update: SessionUpdate; readonly files?: ReadonlyArray<MessageAttachment>; readonly source: \"user\" | \"assistant\" }> => {\n const out: Array<{ update: SessionUpdate; files?: ReadonlyArray<MessageAttachment>; source: \"user\" | \"assistant\" }> = []\n let previous: Message[\"source\"] | undefined\n for (const message of messages) {\n const files = messageFiles(message)\n const withFiles = files.length > 0 ? { files } : {}\n const text = message.text ?? \"\"\n if (message.source === \"tool\") {\n const names = message.calls?.map((call) => call.tool) ?? (message.tool === undefined ? [] : [message.tool])\n out.push({\n source: \"assistant\",\n update: { sessionUpdate: \"tool_call\", toolCallId: message.id, title: toolTitle(message), kind: toolKind(names), status: toolStatus(message, false) }\n })\n } else if (message.source === \"user\") {\n out.push({ source: \"user\", update: { sessionUpdate: \"user_message_chunk\", content: { type: \"text\", text } }, ...withFiles })\n } else {\n // Consecutive replies with nothing between them read as one message unless separated.\n const separated = previous === \"assistant\" ? `\\n\\n${text}` : text\n out.push({ source: \"assistant\", update: { sessionUpdate: \"agent_message_chunk\", content: { type: \"text\", text: separated } }, ...withFiles })\n }\n previous = message.source\n }\n return out\n}\n\n/** Replays a whole recorded stream; used by tests and the replay CLI. */\nexport const replay = (events: ReadonlyArray<CapyEvent>, cursor?: string) => {\n let state = initialTurn(cursor)\n const updates: Array<SessionUpdate> = []\n const signals: Array<TurnSignal> = []\n for (const event of events) {\n const result = step(state, event)\n state = result.state\n updates.push(...result.updates)\n if (result.signal !== undefined) signals.push(result.signal)\n }\n return { state, updates, signals }\n}\n"]}
|