@akanjs/code 0.0.0 → 3.3.0-rc.70
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/README.ko.md +31 -0
- package/README.md +26 -4
- package/agent/AkanCodeCompaction.ts +49 -0
- package/agent/AkanCodeServices.ts +122 -0
- package/agent/AkanContextFiles.ts +94 -0
- package/agent/CodeAgent.ts +598 -0
- package/agent/CodeAgentAllowed.ts +35 -0
- package/agent/CodeAgentAsks.ts +66 -0
- package/agent/CodeAgentCommandLine.ts +372 -0
- package/agent/CodeAgentEventMapper.ts +225 -0
- package/agent/CodeAgentFrames.ts +57 -0
- package/agent/CodeAgentGate.ts +258 -0
- package/agent/CodeAgentProxy.ts +83 -0
- package/agent/CodeAgentRpcHost.ts +109 -0
- package/agent/CodeAgentRpcListener.ts +195 -0
- package/agent/CodeAgentStreamPrinter.ts +86 -0
- package/agent/CodeAgentSuspended.ts +85 -0
- package/agent/CodeAgentUi.ts +47 -0
- package/agent/CodeMailbox.ts +148 -0
- package/agent/CodeSessionFork.ts +56 -0
- package/agent/CodeSessionIndex.ts +78 -0
- package/agent/SubagentPool.ts +222 -0
- package/agent/WorkspaceTrust.ts +200 -0
- package/agent/akanCodeCompaction.test.ts +36 -0
- package/agent/akanCodeModel.ts +47 -0
- package/agent/akanCodeServices.test.ts +54 -0
- package/agent/akanContextFiles.test.ts +116 -0
- package/agent/akanSystemPrompt.ts +50 -0
- package/agent/codeAgent.test.ts +337 -0
- package/agent/codeAgentCommandLine.test.ts +212 -0
- package/agent/codeAgentContract.test.ts +757 -0
- package/agent/codeAgentHosts.ts +30 -0
- package/agent/codeAgentProxy.test.ts +125 -0
- package/agent/codeAgentRpcListener.test.ts +215 -0
- package/agent/codeAgentSuspended.test.ts +47 -0
- package/agent/codeMailbox.test.ts +76 -0
- package/agent/codeSessionFork.test.ts +58 -0
- package/agent/codeSessionIndex.test.ts +77 -0
- package/agent/consoleToStderr.ts +8 -0
- package/agent/jsonLines.ts +29 -0
- package/agent/stringArg.ts +5 -0
- package/bunfig.toml +2 -0
- package/feedback/DevLogFeedback.ts +82 -0
- package/feedback/PreviewFeedback.ts +46 -0
- package/feedback/TurnFeedback.ts +98 -0
- package/feedback/VerifyFeedback.ts +39 -0
- package/host/AkanCodeRunner.ts +297 -0
- package/host/CodeTrustPrompt.ts +57 -0
- package/host/codeRunnerPrompt.test.ts +181 -0
- package/host/hostGraph.test.ts +33 -0
- package/host/index.ts +2 -0
- package/index.ts +49 -0
- package/package.json +43 -9
- package/piDependencyPins.test.ts +45 -0
- package/test/preload.ts +3 -0
- package/tools/AkanCli.ts +56 -0
- package/tools/AkanCodePlugins.ts +145 -0
- package/tools/AkanEditScope.ts +53 -0
- package/tools/AkanEnvKeys.ts +50 -0
- package/tools/AkanToolPack.ts +87 -0
- package/tools/AkanVerifier.ts +30 -0
- package/tools/AskToolPack.ts +99 -0
- package/tools/DeepSeekWeb.ts +127 -0
- package/tools/MailToolPack.ts +76 -0
- package/tools/McpClient.ts +209 -0
- package/tools/McpOAuth.ts +233 -0
- package/tools/McpOAuthLoopback.ts +64 -0
- package/tools/McpServerConfig.ts +131 -0
- package/tools/McpSignIn.ts +158 -0
- package/tools/McpTokenStore.ts +128 -0
- package/tools/McpToolPack.ts +206 -0
- package/tools/SessionToolPack.ts +91 -0
- package/tools/WebToolPack.ts +315 -0
- package/tools/akanCodePlugins.test.ts +954 -0
- package/tools/askToolPack.test.ts +116 -0
- package/tools/deepSeekWeb.test.ts +113 -0
- package/tools/mcpOAuth.test.ts +648 -0
- package/tools/mcpServerConfig.test.ts +341 -0
- package/tsconfig.json +29 -0
- package/tui/CodeTui.tsx +394 -0
- package/tui/CodeTuiAgents.ts +65 -0
- package/tui/CodeTuiApp.test.tsx +251 -0
- package/tui/CodeTuiApp.tsx +312 -0
- package/tui/CodeTuiAutoCompact.ts +33 -0
- package/tui/CodeTuiClipboard.ts +94 -0
- package/tui/CodeTuiCommandRunner.ts +242 -0
- package/tui/CodeTuiCommands.ts +59 -0
- package/tui/CodeTuiEditor.ts +168 -0
- package/tui/CodeTuiFiles.ts +45 -0
- package/tui/CodeTuiFrame.ts +133 -0
- package/tui/CodeTuiLines.ts +117 -0
- package/tui/CodeTuiMarkdown.ts +159 -0
- package/tui/CodeTuiMcp.ts +111 -0
- package/tui/CodeTuiModels.ts +82 -0
- package/tui/CodeTuiMouse.ts +39 -0
- package/tui/CodeTuiOverlay.ts +148 -0
- package/tui/CodeTuiParts.ts +168 -0
- package/tui/CodeTuiPrompt.ts +271 -0
- package/tui/CodeTuiQuestion.ts +170 -0
- package/tui/codeTranscript.test.ts +445 -0
- package/tui/codeTui.test.tsx +1104 -0
- package/tui/codeTuiAgents.test.ts +61 -0
- package/tui/codeTuiClipboard.test.ts +16 -0
- package/tui/codeTuiCommands.test.ts +38 -0
- package/tui/codeTuiEditor.test.ts +122 -0
- package/tui/codeTuiFiles.test.ts +43 -0
- package/tui/codeTuiMarkdown.test.ts +91 -0
- package/tui/codeTuiMcp.test.ts +160 -0
- package/tui/codeTuiMouse.test.ts +61 -0
|
@@ -0,0 +1,445 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import {
|
|
3
|
+
type CodeAgentEvent,
|
|
4
|
+
type CodeAgentEventBody,
|
|
5
|
+
type CodeAgentEventType,
|
|
6
|
+
type CodeAgentSessionInfo,
|
|
7
|
+
type CodeAgentToolSummary,
|
|
8
|
+
CodeTranscript,
|
|
9
|
+
codeAgentEventPersistence,
|
|
10
|
+
} from "akanjs/common";
|
|
11
|
+
import { CodeTuiLines } from "./CodeTuiLines";
|
|
12
|
+
import { CodeTuiParts } from "./CodeTuiParts";
|
|
13
|
+
|
|
14
|
+
const info: CodeAgentSessionInfo = {
|
|
15
|
+
sessionId: "s1",
|
|
16
|
+
cwd: "/repo",
|
|
17
|
+
profile: "local",
|
|
18
|
+
model: { provider: "deepseek", id: "deepseek-v4-flash", name: "DeepSeek V4 Flash" },
|
|
19
|
+
tools: ["read", "bash"],
|
|
20
|
+
contextTokens: 1_000_000,
|
|
21
|
+
effort: "medium",
|
|
22
|
+
autoCompactAt: undefined,
|
|
23
|
+
name: undefined,
|
|
24
|
+
interaction: { question: "await", approval: "await" },
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const tool = (toolCallId: string, title: string): CodeAgentToolSummary => ({
|
|
28
|
+
toolCallId,
|
|
29
|
+
name: "read",
|
|
30
|
+
op: "read",
|
|
31
|
+
title,
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const drawn = (transcript: CodeTranscript, width = 200) =>
|
|
35
|
+
CodeTuiParts.lines(transcript.parts, width).map((line) => CodeTuiLines.text(line));
|
|
36
|
+
|
|
37
|
+
const feed = (transcript: CodeTranscript, bodies: CodeAgentEventBody[]) => {
|
|
38
|
+
let seq = 0;
|
|
39
|
+
for (const body of bodies) {
|
|
40
|
+
seq += 1;
|
|
41
|
+
transcript.apply({ ...body, seq } as CodeAgentEvent);
|
|
42
|
+
}
|
|
43
|
+
return transcript;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
describe("CodeTranscript", () => {
|
|
47
|
+
test("a streamed answer is one part, finalized by the message that follows it", () => {
|
|
48
|
+
const transcript = feed(new CodeTranscript(), [
|
|
49
|
+
{ type: "turn_start", turnId: "t1" },
|
|
50
|
+
{ type: "text_delta", turnId: "t1", text: "Hel" },
|
|
51
|
+
{ type: "text_delta", turnId: "t1", text: "lo" },
|
|
52
|
+
{ type: "message", turnId: "t1", role: "assistant", text: "Hello there" },
|
|
53
|
+
{ type: "turn_end", turnId: "t1", stopReason: "done" },
|
|
54
|
+
]);
|
|
55
|
+
const assistant = transcript.parts.filter((part) => part.kind === "assistant");
|
|
56
|
+
expect(assistant).toHaveLength(1);
|
|
57
|
+
// The final message wins: a retried request streams the abandoned attempt's tokens too.
|
|
58
|
+
expect(assistant[0]).toMatchObject({ text: "Hello there", streaming: false, truncated: false });
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test("the FIRST tool call of a turn is upserted, not appended twice", () => {
|
|
62
|
+
const transcript = feed(new CodeTranscript(), [
|
|
63
|
+
{ type: "turn_start", turnId: "t1" },
|
|
64
|
+
{ type: "tool_start", turnId: "t1", tool: tool("c0", "read(a.ts)") },
|
|
65
|
+
{ type: "tool_end", turnId: "t1", tool: tool("c0", "read(a.ts)"), outcome: "ok", output: "", truncated: false },
|
|
66
|
+
]);
|
|
67
|
+
const tools = transcript.parts.filter((part) => part.kind === "tool");
|
|
68
|
+
expect(tools).toHaveLength(1);
|
|
69
|
+
expect(tools[0]).toMatchObject({ outcome: "ok" });
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test("a second and third call keep their own rows", () => {
|
|
73
|
+
const transcript = feed(new CodeTranscript(), [
|
|
74
|
+
{ type: "tool_start", turnId: "t1", tool: tool("c0", "read(a)") },
|
|
75
|
+
{ type: "tool_start", turnId: "t1", tool: tool("c1", "read(b)") },
|
|
76
|
+
{ type: "tool_end", turnId: "t1", tool: tool("c0", "read(a)"), outcome: "ok", output: "", truncated: false },
|
|
77
|
+
{
|
|
78
|
+
type: "tool_end",
|
|
79
|
+
turnId: "t1",
|
|
80
|
+
tool: tool("c1", "read(b)"),
|
|
81
|
+
outcome: "error",
|
|
82
|
+
output: "nope",
|
|
83
|
+
truncated: false,
|
|
84
|
+
},
|
|
85
|
+
]);
|
|
86
|
+
const tools = transcript.parts.filter((part) => part.kind === "tool");
|
|
87
|
+
expect(tools.map((part) => part.outcome)).toEqual(["ok", "error"]);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test("the end frame's summary replaces the start frame's, so a finished bash keeps its command", () => {
|
|
91
|
+
const transcript = feed(new CodeTranscript(), [
|
|
92
|
+
{ type: "tool_start", turnId: "t1", tool: { toolCallId: "c0", name: "bash", op: "execute", title: "bash" } },
|
|
93
|
+
{
|
|
94
|
+
type: "tool_end",
|
|
95
|
+
turnId: "t1",
|
|
96
|
+
tool: { toolCallId: "c0", name: "bash", op: "execute", title: "bash: bun test" },
|
|
97
|
+
outcome: "ok",
|
|
98
|
+
output: "",
|
|
99
|
+
truncated: false,
|
|
100
|
+
},
|
|
101
|
+
]);
|
|
102
|
+
expect(transcript.parts.find((part) => part.kind === "tool")?.tool.title).toBe("bash: bun test");
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test("a blocked call is not an error, and it closes its own row", () => {
|
|
106
|
+
const transcript = feed(new CodeTranscript(), [
|
|
107
|
+
{ type: "tool_start", turnId: "t1", tool: tool("c0", "read(.env)") },
|
|
108
|
+
{
|
|
109
|
+
type: "tool_end",
|
|
110
|
+
turnId: "t1",
|
|
111
|
+
tool: tool("c0", "read(.env)"),
|
|
112
|
+
outcome: "blocked",
|
|
113
|
+
output: "This profile cannot read .env.",
|
|
114
|
+
truncated: false,
|
|
115
|
+
},
|
|
116
|
+
]);
|
|
117
|
+
const part = transcript.parts.find((entry) => entry.kind === "tool");
|
|
118
|
+
expect(part?.outcome).toBe("blocked");
|
|
119
|
+
const lines = CodeTuiParts.lines(transcript.parts, 80);
|
|
120
|
+
expect(CodeTuiLines.text(lines[0] ?? { key: "", spans: [] })).toContain("⦸");
|
|
121
|
+
expect(lines[0]?.spans[0]?.color).toBe("yellow");
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
test("a truncated turn says so, because a cut answer looks exactly like a finished one", () => {
|
|
125
|
+
const transcript = feed(new CodeTranscript(), [
|
|
126
|
+
{ type: "turn_start", turnId: "t1" },
|
|
127
|
+
{ type: "text_delta", turnId: "t1", text: "half a sen" },
|
|
128
|
+
{ type: "turn_end", turnId: "t1", stopReason: "truncated" },
|
|
129
|
+
]);
|
|
130
|
+
expect(transcript.parts.find((part) => part.kind === "assistant")?.truncated).toBe(true);
|
|
131
|
+
expect(drawn(transcript).some((line) => line.includes("output limit"))).toBe(true);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
test("an interrupted turn keeps the text it produced", () => {
|
|
135
|
+
const transcript = feed(new CodeTranscript(), [
|
|
136
|
+
{ type: "turn_start", turnId: "t1" },
|
|
137
|
+
{ type: "text_delta", turnId: "t1", text: "partial" },
|
|
138
|
+
{ type: "turn_end", turnId: "t1", stopReason: "aborted" },
|
|
139
|
+
]);
|
|
140
|
+
expect(transcript.parts.find((part) => part.kind === "assistant")).toMatchObject({
|
|
141
|
+
text: "partial",
|
|
142
|
+
truncated: false,
|
|
143
|
+
streaming: false,
|
|
144
|
+
});
|
|
145
|
+
expect(transcript.stopReason).toBe("aborted");
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
test("a locally echoed prompt is not drawn twice when the engine echoes it back", () => {
|
|
149
|
+
const transcript = new CodeTranscript();
|
|
150
|
+
transcript.echo("add a comment module");
|
|
151
|
+
feed(transcript, [{ type: "message", turnId: "t1", role: "user", text: "add a comment module" }]);
|
|
152
|
+
expect(transcript.parts.filter((part) => part.kind === "user")).toHaveLength(1);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
test("a feedback loop's own user message has no local echo and is shown", () => {
|
|
156
|
+
const transcript = new CodeTranscript();
|
|
157
|
+
transcript.echo("add a comment module");
|
|
158
|
+
feed(transcript, [
|
|
159
|
+
{ type: "message", turnId: "t1", role: "user", text: "add a comment module" },
|
|
160
|
+
{ type: "message", turnId: "t2", role: "user", text: "typecheck failed: TS2322" },
|
|
161
|
+
]);
|
|
162
|
+
expect(transcript.parts.filter((part) => part.kind === "user")).toHaveLength(2);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
test("a question holds the slot until it is answered, and the answer lands on its own row", () => {
|
|
166
|
+
const transcript = feed(new CodeTranscript(), [
|
|
167
|
+
{
|
|
168
|
+
type: "question",
|
|
169
|
+
question: { questionId: "q1", prompt: "Which app?", kind: "select", options: [{ key: "a", label: "akan" }] },
|
|
170
|
+
},
|
|
171
|
+
]);
|
|
172
|
+
expect(transcript.question?.questionId).toBe("q1");
|
|
173
|
+
feed(transcript, [{ type: "question_resolved", questionId: "q1", answer: { keys: ["a"] }, rendered: "akan" }]);
|
|
174
|
+
expect(transcript.question).toBeUndefined();
|
|
175
|
+
expect(drawn(transcript).some((line) => line.includes("= akan"))).toBe(true);
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
test("an approval holds its own slot and records the verdict", () => {
|
|
179
|
+
const transcript = feed(new CodeTranscript(), [
|
|
180
|
+
{
|
|
181
|
+
type: "approval",
|
|
182
|
+
request: { approvalId: "a1", toolCallId: "c0", name: "write", summary: "write a.ts", policy: "writes" },
|
|
183
|
+
},
|
|
184
|
+
]);
|
|
185
|
+
expect(transcript.approval?.approvalId).toBe("a1");
|
|
186
|
+
feed(transcript, [{ type: "approval_resolved", approvalId: "a1", approved: false }]);
|
|
187
|
+
expect(transcript.approval).toBeUndefined();
|
|
188
|
+
expect(drawn(transcript).some((line) => line.includes("denied"))).toBe(true);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
test("an approval answered for the session says which commands stop being asked about", () => {
|
|
192
|
+
const transcript = feed(new CodeTranscript(), [
|
|
193
|
+
{
|
|
194
|
+
type: "approval",
|
|
195
|
+
request: {
|
|
196
|
+
approvalId: "a1",
|
|
197
|
+
toolCallId: "c0",
|
|
198
|
+
name: "bash",
|
|
199
|
+
summary: "run: bun test",
|
|
200
|
+
policy: "commands",
|
|
201
|
+
remember: ["bun test"],
|
|
202
|
+
},
|
|
203
|
+
},
|
|
204
|
+
{ type: "approval_resolved", approvalId: "a1", approved: true, remembered: ["bun test"] },
|
|
205
|
+
{
|
|
206
|
+
type: "approval",
|
|
207
|
+
request: { approvalId: "a2", toolCallId: "c1", name: "bash", summary: "run: ls", policy: "commands" },
|
|
208
|
+
},
|
|
209
|
+
{ type: "approval_resolved", approvalId: "a2", approved: true },
|
|
210
|
+
]);
|
|
211
|
+
const lines = drawn(transcript);
|
|
212
|
+
expect(lines.filter((line) => line.includes("approved"))).toEqual([
|
|
213
|
+
" approved · bun test runs unasked until this session ends",
|
|
214
|
+
" approved",
|
|
215
|
+
]);
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
test("a host frame carrying an id is one row through its whole lifecycle", () => {
|
|
219
|
+
const transcript = feed(new CodeTranscript(), [
|
|
220
|
+
{ type: "host", kind: "step", id: "s1", payload: "pending" },
|
|
221
|
+
{ type: "host", kind: "step", id: "s1", payload: "running" },
|
|
222
|
+
{ type: "host", kind: "step", id: "s1", payload: "done" },
|
|
223
|
+
]);
|
|
224
|
+
const hosts = transcript.parts.filter((part) => part.kind === "host");
|
|
225
|
+
expect(hosts).toHaveLength(1);
|
|
226
|
+
expect(hosts[0]?.text).toBe("done");
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
test("a host frame with no id is a new row each time", () => {
|
|
230
|
+
const transcript = feed(new CodeTranscript(), [
|
|
231
|
+
{ type: "host", kind: "note", payload: "one" },
|
|
232
|
+
{ type: "host", kind: "note", payload: "two" },
|
|
233
|
+
]);
|
|
234
|
+
expect(transcript.parts.filter((part) => part.kind === "host")).toHaveLength(2);
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
test("context usage and the session line are both readable", () => {
|
|
238
|
+
const transcript = feed(new CodeTranscript(), [
|
|
239
|
+
{ type: "session", info },
|
|
240
|
+
{ type: "context", used: 42_000, max: 1_000_000 },
|
|
241
|
+
]);
|
|
242
|
+
expect(transcript.info?.model?.name).toBe("DeepSeek V4 Flash");
|
|
243
|
+
expect(transcript.context).toEqual({ used: 42_000, max: 1_000_000 });
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
test("idle clears the queue and stops the spinner", () => {
|
|
247
|
+
const transcript = feed(new CodeTranscript(), [
|
|
248
|
+
{ type: "turn_start", turnId: "t1" },
|
|
249
|
+
{ type: "queue", steering: ["a"], followUp: [] },
|
|
250
|
+
{ type: "idle" },
|
|
251
|
+
]);
|
|
252
|
+
expect(transcript.streaming).toBe(false);
|
|
253
|
+
expect(transcript.queue).toEqual({ steering: 0, followUp: 0 });
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
test("an automatic compaction that failed says so instead of claiming it compacted", () => {
|
|
257
|
+
const notices = (end: Extract<CodeAgentEventBody, { type: "compaction" }>) =>
|
|
258
|
+
feed(new CodeTranscript(), [{ type: "compaction", phase: "start", reason: end.reason }, end]).parts.flatMap(
|
|
259
|
+
(part) => (part.kind === "notice" ? [`${part.level}: ${part.text}`] : []),
|
|
260
|
+
);
|
|
261
|
+
expect(
|
|
262
|
+
notices({ type: "compaction", phase: "end", reason: "threshold", error: "Auto-compaction failed: 529" }),
|
|
263
|
+
).toEqual(["warning: Auto-compaction failed: 529"]);
|
|
264
|
+
expect(notices({ type: "compaction", phase: "end", reason: "overflow", aborted: true })).toEqual([
|
|
265
|
+
"info: Compaction cancelled.",
|
|
266
|
+
]);
|
|
267
|
+
expect(notices({ type: "compaction", phase: "end", reason: "threshold" })).toEqual([
|
|
268
|
+
"info: Compacted the conversation (threshold).",
|
|
269
|
+
]);
|
|
270
|
+
expect(notices({ type: "compaction", phase: "end", reason: "manual", error: "Compaction failed: x" })).toEqual([]);
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
test("every event in the contract changes something in the fold", () => {
|
|
274
|
+
const samples: { [key in CodeAgentEventType]: CodeAgentEventBody } = {
|
|
275
|
+
session: { type: "session", info },
|
|
276
|
+
turn_start: { type: "turn_start", turnId: "t1" },
|
|
277
|
+
turn_end: { type: "turn_end", turnId: "t1", stopReason: "truncated" },
|
|
278
|
+
text_delta: { type: "text_delta", turnId: "t1", text: "x" },
|
|
279
|
+
thinking_delta: { type: "thinking_delta", turnId: "t1", text: "x" },
|
|
280
|
+
message: { type: "message", turnId: "t1", role: "assistant", text: "x" },
|
|
281
|
+
tool_start: { type: "tool_start", turnId: "t1", tool: tool("c0", "read(a)") },
|
|
282
|
+
tool_progress: { type: "tool_progress", turnId: "t1", toolCallId: "c0", text: "50%" },
|
|
283
|
+
tool_end: {
|
|
284
|
+
type: "tool_end",
|
|
285
|
+
turnId: "t1",
|
|
286
|
+
tool: tool("c0", "read(a)"),
|
|
287
|
+
outcome: "ok",
|
|
288
|
+
output: "",
|
|
289
|
+
truncated: false,
|
|
290
|
+
},
|
|
291
|
+
question: { type: "question", question: { questionId: "q1", prompt: "?", kind: "text" } },
|
|
292
|
+
question_resolved: { type: "question_resolved", questionId: "q1", answer: { text: "y" }, rendered: "y" },
|
|
293
|
+
question_skipped: {
|
|
294
|
+
type: "question_skipped",
|
|
295
|
+
question: { questionId: "q2", prompt: "?", kind: "text" },
|
|
296
|
+
reason: "no-host",
|
|
297
|
+
},
|
|
298
|
+
approval: {
|
|
299
|
+
type: "approval",
|
|
300
|
+
request: { approvalId: "a1", toolCallId: "c0", name: "write", summary: "s", policy: "writes" },
|
|
301
|
+
},
|
|
302
|
+
approval_resolved: { type: "approval_resolved", approvalId: "a1", approved: true },
|
|
303
|
+
context: { type: "context", used: 1, max: 2 },
|
|
304
|
+
subagent: {
|
|
305
|
+
type: "subagent",
|
|
306
|
+
agents: [{ id: "c0", kind: "explore", description: "reading the store", startedAt: 1, tokens: 2 }],
|
|
307
|
+
},
|
|
308
|
+
compaction: { type: "compaction", phase: "end", reason: "threshold" },
|
|
309
|
+
retry: { type: "retry", attempt: 1, maxAttempts: 3, delayMs: 10, message: "429" },
|
|
310
|
+
queue: { type: "queue", steering: ["a"], followUp: [] },
|
|
311
|
+
notice: { type: "notice", level: "warning", message: "m" },
|
|
312
|
+
error: { type: "error", message: "boom", fatal: false },
|
|
313
|
+
idle: { type: "idle" },
|
|
314
|
+
host: { type: "host", kind: "step", payload: "p" },
|
|
315
|
+
};
|
|
316
|
+
// These three are idempotent against the setup, so they are probed with a different value.
|
|
317
|
+
const probes: { [key in CodeAgentEventType]: CodeAgentEventBody } = {
|
|
318
|
+
...samples,
|
|
319
|
+
session: { type: "session", info: { ...info, sessionId: "s2" } },
|
|
320
|
+
tool_start: { type: "tool_start", turnId: "t1", tool: tool("c1", "read(b)") },
|
|
321
|
+
queue: { type: "queue", steering: ["a", "b"], followUp: ["c"] },
|
|
322
|
+
};
|
|
323
|
+
const state = (transcript: CodeTranscript) =>
|
|
324
|
+
JSON.stringify([
|
|
325
|
+
transcript.parts,
|
|
326
|
+
transcript.info,
|
|
327
|
+
transcript.context,
|
|
328
|
+
transcript.queue,
|
|
329
|
+
transcript.subagents,
|
|
330
|
+
transcript.streaming,
|
|
331
|
+
transcript.compacting,
|
|
332
|
+
transcript.stopReason,
|
|
333
|
+
]);
|
|
334
|
+
const unread: string[] = [];
|
|
335
|
+
for (const type of Object.keys(codeAgentEventPersistence) as CodeAgentEventType[]) {
|
|
336
|
+
// `turn_start` is probed after `idle` closes the turn, or there is no flag left for it to flip.
|
|
337
|
+
const transcript = feed(new CodeTranscript(), [
|
|
338
|
+
samples.session,
|
|
339
|
+
samples.turn_start,
|
|
340
|
+
samples.tool_start,
|
|
341
|
+
samples.question,
|
|
342
|
+
samples.approval,
|
|
343
|
+
samples.queue,
|
|
344
|
+
...(type === "turn_start" ? [samples.idle] : []),
|
|
345
|
+
]);
|
|
346
|
+
const before = state(transcript);
|
|
347
|
+
feed(transcript, [probes[type]]);
|
|
348
|
+
if (before === state(transcript)) unread.push(type);
|
|
349
|
+
}
|
|
350
|
+
expect(unread).toEqual([]);
|
|
351
|
+
});
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
describe("assistant rendering", () => {
|
|
355
|
+
test("markdown is rendered while it is still arriving, not only once the turn ends", () => {
|
|
356
|
+
const transcript = feed(new CodeTranscript(), [
|
|
357
|
+
{ type: "turn_start", turnId: "t1" },
|
|
358
|
+
{ type: "text_delta", turnId: "t1", text: "## Results\n\n- **one** item" },
|
|
359
|
+
]);
|
|
360
|
+
const lines = drawn(transcript);
|
|
361
|
+
expect(lines.join("\n")).not.toContain("##");
|
|
362
|
+
expect(lines.join("\n")).not.toContain("**");
|
|
363
|
+
expect(lines).toContain("Results");
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
test("an unterminated fence is already a code block, so it does not change shape when it closes", () => {
|
|
367
|
+
const open = feed(new CodeTranscript(), [{ type: "text_delta", turnId: "t1", text: "```ts\nconst a = 1;" }]);
|
|
368
|
+
const closed = feed(new CodeTranscript(), [{ type: "text_delta", turnId: "t1", text: "```ts\nconst a = 1;\n```" }]);
|
|
369
|
+
expect(drawn(open)).toEqual(drawn(closed));
|
|
370
|
+
});
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
describe("CodeTui layout", () => {
|
|
374
|
+
test("the rows always add up to the terminal, whatever the prompt or the question needs", async () => {
|
|
375
|
+
const { CodeTui } = await import("./CodeTui");
|
|
376
|
+
for (let terminalRows = 10; terminalRows <= 60; terminalRows += 1)
|
|
377
|
+
for (const askRows of [1, 2, 5, 12, 40]) {
|
|
378
|
+
const { ask, bodyHeight } = CodeTui.layout(terminalRows, askRows);
|
|
379
|
+
expect(bodyHeight + ask + CodeTui.chromeRows).toBe(terminalRows);
|
|
380
|
+
expect(bodyHeight).toBeGreaterThanOrEqual(CodeTui.minBodyRows);
|
|
381
|
+
expect(ask).toBeLessThanOrEqual(askRows);
|
|
382
|
+
expect(ask).toBeGreaterThanOrEqual(1);
|
|
383
|
+
}
|
|
384
|
+
});
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
describe("printer and transcript agree", () => {
|
|
388
|
+
test("the same session says the same things through both hosts", async () => {
|
|
389
|
+
const { CodeAgentStreamPrinter } = await import("../agent/CodeAgentStreamPrinter");
|
|
390
|
+
const session: CodeAgentEventBody[] = [
|
|
391
|
+
{ type: "session", info },
|
|
392
|
+
{ type: "turn_start", turnId: "t1" },
|
|
393
|
+
{ type: "message", turnId: "t1", role: "user", text: "add a comment module" },
|
|
394
|
+
{ type: "tool_start", turnId: "t1", tool: tool("c0", "plan_workflow(create-module)") },
|
|
395
|
+
{
|
|
396
|
+
type: "tool_end",
|
|
397
|
+
turnId: "t1",
|
|
398
|
+
tool: tool("c0", "plan_workflow(create-module)"),
|
|
399
|
+
outcome: "ok",
|
|
400
|
+
output: "",
|
|
401
|
+
truncated: false,
|
|
402
|
+
},
|
|
403
|
+
{ type: "tool_start", turnId: "t1", tool: tool("c1", "read(.env)") },
|
|
404
|
+
{
|
|
405
|
+
type: "tool_end",
|
|
406
|
+
turnId: "t1",
|
|
407
|
+
tool: tool("c1", "read(.env)"),
|
|
408
|
+
outcome: "blocked",
|
|
409
|
+
output: "This profile cannot read .env.",
|
|
410
|
+
truncated: false,
|
|
411
|
+
},
|
|
412
|
+
{ type: "notice", level: "warning", message: "the dev server logged an error" },
|
|
413
|
+
{ type: "text_delta", turnId: "t1", text: "I planned the module" },
|
|
414
|
+
{ type: "turn_end", turnId: "t1", stopReason: "truncated" },
|
|
415
|
+
];
|
|
416
|
+
let printed = "";
|
|
417
|
+
const printer = new CodeAgentStreamPrinter({ write: (text) => (printed += text) });
|
|
418
|
+
const transcript = new CodeTranscript();
|
|
419
|
+
let seq = 0;
|
|
420
|
+
for (const body of session) {
|
|
421
|
+
seq += 1;
|
|
422
|
+
const event = { ...body, seq } as CodeAgentEvent;
|
|
423
|
+
printer.print(event);
|
|
424
|
+
transcript.apply(event);
|
|
425
|
+
}
|
|
426
|
+
printer.finish();
|
|
427
|
+
const plain = printed.replace(new RegExp(`${String.fromCharCode(27)}\\[[0-9;]*m`, "g"), "");
|
|
428
|
+
const rendered = [transcript.headline, ...drawn(transcript)].join("\n");
|
|
429
|
+
for (const fact of [
|
|
430
|
+
"DeepSeek V4 Flash",
|
|
431
|
+
"1000k ctx",
|
|
432
|
+
"plan_workflow(create-module)",
|
|
433
|
+
"read(.env)",
|
|
434
|
+
"the dev server logged an error",
|
|
435
|
+
"I planned the module",
|
|
436
|
+
])
|
|
437
|
+
for (const [host, text] of [
|
|
438
|
+
["printer", plain],
|
|
439
|
+
["transcript", rendered],
|
|
440
|
+
] as const)
|
|
441
|
+
expect(`${host}: ${text.includes(fact)}`).toBe(`${host}: true`);
|
|
442
|
+
expect(plain.toLowerCase()).toContain("cut off");
|
|
443
|
+
expect(rendered.toLowerCase()).toContain("output limit");
|
|
444
|
+
});
|
|
445
|
+
});
|