clanka 0.16.1 → 0.17.1
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.md +61 -0
- package/dist/Agent.d.ts.map +1 -1
- package/dist/Agent.js +5 -1
- package/dist/Agent.js.map +1 -1
- package/dist/AgentCallerConfig.test.js +5 -3
- package/dist/AgentCallerConfig.test.js.map +1 -1
- package/dist/ChunkRepo.d.ts +1 -1
- package/dist/ClaudeAgent.d.ts +111 -0
- package/dist/ClaudeAgent.d.ts.map +1 -0
- package/dist/ClaudeAgent.js +772 -0
- package/dist/ClaudeAgent.js.map +1 -0
- package/dist/ClaudeAgent.test.d.ts +2 -0
- package/dist/ClaudeAgent.test.d.ts.map +1 -0
- package/dist/ClaudeAgent.test.js +416 -0
- package/dist/ClaudeAgent.test.js.map +1 -0
- package/dist/Compaction.test.js +2 -1
- package/dist/Compaction.test.js.map +1 -1
- package/dist/CompactionTokens.test.js +15 -8
- package/dist/CompactionTokens.test.js.map +1 -1
- package/dist/ModelLayer.d.ts +2 -2
- package/dist/ModelLayer.d.ts.map +1 -1
- package/dist/ModelLayer.js +13 -7
- package/dist/ModelLayer.js.map +1 -1
- package/dist/ModelLayer.test.js +3 -0
- package/dist/ModelLayer.test.js.map +1 -1
- package/dist/ModelsRepo.d.ts +7 -6
- package/dist/ModelsRepo.d.ts.map +1 -1
- package/dist/ModelsRepo.js +44 -27
- package/dist/ModelsRepo.js.map +1 -1
- package/dist/ModelsRepo.test.js +47 -3
- package/dist/ModelsRepo.test.js.map +1 -1
- package/dist/bin/{Effect-4B1lzxy4.mjs → Effect-DNYYQ84C.mjs} +467 -365
- package/dist/bin/Effect-DNYYQ84C.mjs.map +1 -0
- package/dist/bin/Latch-BSlIwuxI.mjs +7 -0
- package/dist/bin/Latch-BSlIwuxI.mjs.map +1 -0
- package/dist/bin/{McpClient-DMfHHEx3.mjs → McpClient-C7ctqJWo.mjs} +4 -5
- package/dist/bin/{McpClient-DMfHHEx3.mjs.map → McpClient-C7ctqJWo.mjs.map} +1 -1
- package/dist/bin/{OutputFormatter-DRycCEdr.mjs → OutputFormatter-DXej05JW.mjs} +8 -13
- package/dist/bin/{OutputFormatter-DRycCEdr.mjs.map → OutputFormatter-DXej05JW.mjs.map} +1 -1
- package/dist/bin/{Schema-D4fxUgEU.mjs → Schema-CSQN_Zso.mjs} +754 -800
- package/dist/bin/Schema-CSQN_Zso.mjs.map +1 -0
- package/dist/bin/{SemanticSearch-BcLFv9qz.mjs → SemanticSearch-BiwCnpKX.mjs} +241 -148
- package/dist/bin/SemanticSearch-BiwCnpKX.mjs.map +1 -0
- package/dist/bin/{Stream-i37nF1ep.mjs → Stream-Dwvj9X5X.mjs} +244 -120
- package/dist/bin/Stream-Dwvj9X5X.mjs.map +1 -0
- package/dist/bin/cli.mjs +5122 -15959
- package/dist/bin/cli.mjs.map +1 -1
- package/dist/bin/helpers-CljL9OKa.mjs +7157 -0
- package/dist/bin/helpers-CljL9OKa.mjs.map +1 -0
- package/dist/bin/index.min-ClUfQ5ji.mjs +3782 -0
- package/dist/bin/index.min-ClUfQ5ji.mjs.map +1 -0
- package/dist/bin/{rolldown-runtime-zY4HT0JA.mjs → rolldown-runtime-5zChB7MF.mjs} +15 -3
- package/dist/bin/server-HlLriZxF.mjs +319 -0
- package/dist/bin/server-HlLriZxF.mjs.map +1 -0
- package/dist/bin/{turndown.cjs-LntFxFyd.mjs → turndown.cjs-C2WBxeA-.mjs} +56 -110
- package/dist/bin/{turndown.cjs-LntFxFyd.mjs.map → turndown.cjs-C2WBxeA-.mjs.map} +1 -1
- package/dist/bin/types-LcwjLVkt.mjs +5260 -0
- package/dist/bin/types-LcwjLVkt.mjs.map +1 -0
- package/dist/bin/upgrade-BSNjCnra.mjs +342 -0
- package/dist/bin/upgrade-BSNjCnra.mjs.map +1 -0
- package/dist/cli.js +4 -0
- package/dist/cli.js.map +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/package.json +15 -13
- package/src/Agent.ts +7 -1
- package/src/AgentCallerConfig.test.ts +5 -3
- package/src/ClaudeAgent.test.ts +526 -0
- package/src/ClaudeAgent.ts +1113 -0
- package/src/Compaction.test.ts +2 -1
- package/src/CompactionTokens.test.ts +15 -11
- package/src/ModelLayer.test.ts +4 -0
- package/src/ModelLayer.ts +21 -15
- package/src/ModelsRepo.test.ts +69 -3
- package/src/ModelsRepo.ts +24 -8
- package/src/cli.ts +4 -0
- package/src/index.ts +5 -0
- package/dist/bin/Effect-4B1lzxy4.mjs.map +0 -1
- package/dist/bin/Schema-D4fxUgEU.mjs.map +0 -1
- package/dist/bin/SemanticSearch-BcLFv9qz.mjs.map +0 -1
- package/dist/bin/Stream-i37nF1ep.mjs.map +0 -1
- package/dist/bin/index.min-Bkz7vIWv.mjs +0 -3300
- package/dist/bin/index.min-Bkz7vIWv.mjs.map +0 -1
|
@@ -0,0 +1,526 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Options,
|
|
3
|
+
query as SdkQuery,
|
|
4
|
+
SDKUserMessage,
|
|
5
|
+
} from "@anthropic-ai/claude-agent-sdk"
|
|
6
|
+
import { assert, describe, it } from "@effect/vitest"
|
|
7
|
+
import { Client } from "@modelcontextprotocol/sdk/client/index.js"
|
|
8
|
+
import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js"
|
|
9
|
+
import * as Effect from "effect/Effect"
|
|
10
|
+
import * as Layer from "effect/Layer"
|
|
11
|
+
import * as Base64 from "effect/encoding/Base64"
|
|
12
|
+
import * as Schema from "effect/Schema"
|
|
13
|
+
import * as Stream from "effect/Stream"
|
|
14
|
+
import * as LanguageModel from "effect/ai/LanguageModel"
|
|
15
|
+
import * as Prompt from "effect/ai/Prompt"
|
|
16
|
+
import * as Tool from "effect/ai/Tool"
|
|
17
|
+
import * as Toolkit from "effect/ai/Toolkit"
|
|
18
|
+
import * as ClaudeAgent from "./ClaudeAgent.ts"
|
|
19
|
+
import { tinyPng } from "./fixtures/TestImages.ts"
|
|
20
|
+
|
|
21
|
+
const system = Prompt.makeMessage("system", { content: "You are clanka." })
|
|
22
|
+
|
|
23
|
+
const user = (text: string) =>
|
|
24
|
+
Prompt.makeMessage("user", {
|
|
25
|
+
content: [Prompt.makePart("text", { text })],
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
const script = 'await bash("pnpm test")'
|
|
29
|
+
|
|
30
|
+
const history = Prompt.fromMessages([
|
|
31
|
+
system,
|
|
32
|
+
Prompt.makeMessage("user", {
|
|
33
|
+
content: [
|
|
34
|
+
Prompt.makePart("text", { text: "Fix the failing test" }),
|
|
35
|
+
Prompt.makePart("file", { mediaType: "image/png", data: tinyPng }),
|
|
36
|
+
],
|
|
37
|
+
}),
|
|
38
|
+
Prompt.makeMessage("assistant", {
|
|
39
|
+
content: [
|
|
40
|
+
Prompt.makePart("tool-call", {
|
|
41
|
+
id: "toolu_1",
|
|
42
|
+
name: "execute",
|
|
43
|
+
params: { script },
|
|
44
|
+
providerExecuted: false,
|
|
45
|
+
}),
|
|
46
|
+
],
|
|
47
|
+
}),
|
|
48
|
+
Prompt.makeMessage("tool", {
|
|
49
|
+
content: [
|
|
50
|
+
Prompt.makePart("tool-result", {
|
|
51
|
+
id: "toolu_1",
|
|
52
|
+
name: "execute",
|
|
53
|
+
isFailure: false,
|
|
54
|
+
result: "1 failed",
|
|
55
|
+
providerExecuted: false,
|
|
56
|
+
}),
|
|
57
|
+
],
|
|
58
|
+
}),
|
|
59
|
+
Prompt.makeMessage("assistant", {
|
|
60
|
+
content: [Prompt.makePart("text", { text: "Fixed it." })],
|
|
61
|
+
}),
|
|
62
|
+
user("Thanks, now commit"),
|
|
63
|
+
])
|
|
64
|
+
|
|
65
|
+
// Trimmed Agent SDK messages, carrying the fields a step's conversion reads.
|
|
66
|
+
const frame = { parent_tool_use_id: null, uuid: "u", session_id: "s" }
|
|
67
|
+
const event = (event: object) => ({ ...frame, type: "stream_event", event })
|
|
68
|
+
const assistant = (block: object) => ({
|
|
69
|
+
...frame,
|
|
70
|
+
type: "assistant",
|
|
71
|
+
message: {
|
|
72
|
+
id: "msg_1",
|
|
73
|
+
type: "message",
|
|
74
|
+
role: "assistant",
|
|
75
|
+
model: "claude-opus-5-5",
|
|
76
|
+
content: [block],
|
|
77
|
+
stop_reason: null,
|
|
78
|
+
stop_sequence: null,
|
|
79
|
+
usage: { input_tokens: 100, output_tokens: 1 },
|
|
80
|
+
},
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
// One model step that thinks, says something, then calls clanka's `execute`
|
|
84
|
+
// tool and a Claude Code tool clanka did not offer.
|
|
85
|
+
const toolStep = [
|
|
86
|
+
event({
|
|
87
|
+
type: "message_start",
|
|
88
|
+
message: {
|
|
89
|
+
id: "msg_1",
|
|
90
|
+
type: "message",
|
|
91
|
+
role: "assistant",
|
|
92
|
+
model: "claude-opus-5-5",
|
|
93
|
+
content: [],
|
|
94
|
+
stop_reason: null,
|
|
95
|
+
stop_sequence: null,
|
|
96
|
+
usage: {
|
|
97
|
+
input_tokens: 100,
|
|
98
|
+
cache_read_input_tokens: 50,
|
|
99
|
+
cache_creation_input_tokens: 10,
|
|
100
|
+
output_tokens: 1,
|
|
101
|
+
},
|
|
102
|
+
},
|
|
103
|
+
}),
|
|
104
|
+
event({
|
|
105
|
+
type: "content_block_start",
|
|
106
|
+
index: 0,
|
|
107
|
+
content_block: { type: "thinking", thinking: "", signature: "" },
|
|
108
|
+
}),
|
|
109
|
+
event({
|
|
110
|
+
type: "content_block_delta",
|
|
111
|
+
index: 0,
|
|
112
|
+
delta: { type: "thinking_delta", thinking: "Run the tests " },
|
|
113
|
+
}),
|
|
114
|
+
event({
|
|
115
|
+
type: "content_block_delta",
|
|
116
|
+
index: 0,
|
|
117
|
+
delta: { type: "thinking_delta", thinking: "first." },
|
|
118
|
+
}),
|
|
119
|
+
event({ type: "content_block_stop", index: 0 }),
|
|
120
|
+
assistant({
|
|
121
|
+
type: "thinking",
|
|
122
|
+
thinking: "Run the tests first.",
|
|
123
|
+
signature: "sig",
|
|
124
|
+
}),
|
|
125
|
+
event({
|
|
126
|
+
type: "content_block_start",
|
|
127
|
+
index: 1,
|
|
128
|
+
content_block: { type: "text", text: "" },
|
|
129
|
+
}),
|
|
130
|
+
event({
|
|
131
|
+
type: "content_block_delta",
|
|
132
|
+
index: 1,
|
|
133
|
+
delta: { type: "text_delta", text: "Running the tests." },
|
|
134
|
+
}),
|
|
135
|
+
event({ type: "content_block_stop", index: 1 }),
|
|
136
|
+
assistant({ type: "text", text: "Running the tests." }),
|
|
137
|
+
event({
|
|
138
|
+
type: "content_block_start",
|
|
139
|
+
index: 2,
|
|
140
|
+
content_block: {
|
|
141
|
+
type: "tool_use",
|
|
142
|
+
id: "toolu_1",
|
|
143
|
+
name: "mcp__clanka__execute",
|
|
144
|
+
input: {},
|
|
145
|
+
},
|
|
146
|
+
}),
|
|
147
|
+
event({
|
|
148
|
+
type: "content_block_delta",
|
|
149
|
+
index: 2,
|
|
150
|
+
delta: {
|
|
151
|
+
type: "input_json_delta",
|
|
152
|
+
partial_json: JSON.stringify({ script }),
|
|
153
|
+
},
|
|
154
|
+
}),
|
|
155
|
+
event({ type: "content_block_stop", index: 2 }),
|
|
156
|
+
assistant({
|
|
157
|
+
type: "tool_use",
|
|
158
|
+
id: "toolu_1",
|
|
159
|
+
name: "mcp__clanka__execute",
|
|
160
|
+
input: { script },
|
|
161
|
+
}),
|
|
162
|
+
assistant({
|
|
163
|
+
type: "tool_use",
|
|
164
|
+
id: "toolu_2",
|
|
165
|
+
name: "Bash",
|
|
166
|
+
input: { command: "ls" },
|
|
167
|
+
}),
|
|
168
|
+
event({
|
|
169
|
+
type: "message_delta",
|
|
170
|
+
delta: { stop_reason: "tool_use", stop_sequence: null },
|
|
171
|
+
usage: { output_tokens: 20 },
|
|
172
|
+
}),
|
|
173
|
+
event({ type: "message_stop" }),
|
|
174
|
+
]
|
|
175
|
+
|
|
176
|
+
describe("ClaudeAgent", () => {
|
|
177
|
+
it("runs Claude Code with only clanka's tools, instructions and settings", () => {
|
|
178
|
+
const options = ClaudeAgent.queryOptions({
|
|
179
|
+
model: "claude-opus-5-5",
|
|
180
|
+
prompt: Prompt.fromMessages([system, user("hello")]),
|
|
181
|
+
cwd: "/work",
|
|
182
|
+
pathToClaudeCodeExecutable: "/opt/claude",
|
|
183
|
+
})
|
|
184
|
+
assert.deepInclude(options, {
|
|
185
|
+
model: "claude-opus-5-5",
|
|
186
|
+
cwd: "/work",
|
|
187
|
+
pathToClaudeCodeExecutable: "/opt/claude",
|
|
188
|
+
tools: [],
|
|
189
|
+
allowedTools: ["mcp__clanka__*"],
|
|
190
|
+
permissionMode: "dontAsk",
|
|
191
|
+
settingSources: [],
|
|
192
|
+
settings: { disableAllHooks: true },
|
|
193
|
+
strictMcpConfig: true,
|
|
194
|
+
persistSession: false,
|
|
195
|
+
})
|
|
196
|
+
assert.deepInclude(options.systemPrompt, {
|
|
197
|
+
type: "custom",
|
|
198
|
+
prompt: "You are clanka.",
|
|
199
|
+
})
|
|
200
|
+
assert.deepInclude(options.env, {
|
|
201
|
+
PATH: process.env.PATH,
|
|
202
|
+
CLAUDE_CODE_DISABLE_CLAUDE_MDS: "1",
|
|
203
|
+
CLAUDE_CODE_DISABLE_AUTO_MEMORY: "1",
|
|
204
|
+
DISABLE_AUTO_COMPACT: "1",
|
|
205
|
+
ENABLE_TOOL_SEARCH: "false",
|
|
206
|
+
})
|
|
207
|
+
assert.match(options.env?.CLAUDE_AGENT_SDK_CLIENT_APP ?? "", /^clanka\//)
|
|
208
|
+
})
|
|
209
|
+
|
|
210
|
+
it("renders the history as one user message, with images as image blocks", () => {
|
|
211
|
+
const content = ClaudeAgent.toUserContent(history)
|
|
212
|
+
assert.deepStrictEqual(
|
|
213
|
+
content.filter((block) => block.type === "image"),
|
|
214
|
+
[
|
|
215
|
+
{
|
|
216
|
+
type: "image",
|
|
217
|
+
source: {
|
|
218
|
+
type: "base64",
|
|
219
|
+
media_type: "image/png",
|
|
220
|
+
data: Base64.encode(tinyPng),
|
|
221
|
+
},
|
|
222
|
+
},
|
|
223
|
+
],
|
|
224
|
+
)
|
|
225
|
+
const text = content.flatMap((block) =>
|
|
226
|
+
block.type === "text" ? [block.text] : [],
|
|
227
|
+
)
|
|
228
|
+
const rendered = text.join("\n")
|
|
229
|
+
assert.notInclude(rendered, "You are clanka.")
|
|
230
|
+
const positions = [
|
|
231
|
+
"Fix the failing test",
|
|
232
|
+
script,
|
|
233
|
+
"1 failed",
|
|
234
|
+
"Fixed it.",
|
|
235
|
+
"Thanks, now commit",
|
|
236
|
+
].map((part) => rendered.indexOf(part))
|
|
237
|
+
assert.notInclude(positions, -1)
|
|
238
|
+
assert.deepStrictEqual(
|
|
239
|
+
positions,
|
|
240
|
+
positions.toSorted((a, b) => a - b),
|
|
241
|
+
)
|
|
242
|
+
})
|
|
243
|
+
|
|
244
|
+
it.effect("converts a tool step to response parts", () =>
|
|
245
|
+
Effect.gen(function* () {
|
|
246
|
+
const parts = yield* Stream.fromIterable(toolStep as Array<any>).pipe(
|
|
247
|
+
ClaudeAgent.toStreamParts,
|
|
248
|
+
Stream.runCollect,
|
|
249
|
+
)
|
|
250
|
+
const deltas = (type: "text-delta" | "reasoning-delta") =>
|
|
251
|
+
parts.flatMap((part) => (part.type === type ? [part.delta] : []))
|
|
252
|
+
assert.strictEqual(
|
|
253
|
+
deltas("reasoning-delta").join(""),
|
|
254
|
+
"Run the tests first.",
|
|
255
|
+
)
|
|
256
|
+
assert.strictEqual(deltas("text-delta").join(""), "Running the tests.")
|
|
257
|
+
assert.deepStrictEqual(
|
|
258
|
+
parts.flatMap((part) =>
|
|
259
|
+
part.type === "tool-call"
|
|
260
|
+
? [{ id: part.id, name: part.name, params: part.params }]
|
|
261
|
+
: [],
|
|
262
|
+
),
|
|
263
|
+
[{ id: "toolu_1", name: "execute", params: { script } }],
|
|
264
|
+
)
|
|
265
|
+
const finish = parts.filter((part) => part.type === "finish")
|
|
266
|
+
assert.lengthOf(finish, 1)
|
|
267
|
+
assert.strictEqual(finish[0]!.reason, "tool-calls")
|
|
268
|
+
assert.strictEqual(finish[0]!.usage.inputTokens.total, 160)
|
|
269
|
+
assert.strictEqual(finish[0]!.usage.outputTokens.total, 20)
|
|
270
|
+
}),
|
|
271
|
+
)
|
|
272
|
+
})
|
|
273
|
+
|
|
274
|
+
// A fake Claude Code behind the SDK's `query()`. `run` plays one session
|
|
275
|
+
// against its input and clanka's MCP server.
|
|
276
|
+
interface FakeSession {
|
|
277
|
+
readonly options: Options
|
|
278
|
+
readonly input: AsyncIterator<SDKUserMessage>
|
|
279
|
+
closed: boolean
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
const fakeClaude = (run: (session: FakeSession) => AsyncGenerator<object>) => {
|
|
283
|
+
const sessions: Array<FakeSession> = []
|
|
284
|
+
const query = (params: {
|
|
285
|
+
readonly prompt: AsyncIterable<SDKUserMessage>
|
|
286
|
+
readonly options: Options
|
|
287
|
+
}) => {
|
|
288
|
+
const session: FakeSession = {
|
|
289
|
+
options: params.options,
|
|
290
|
+
input: params.prompt[Symbol.asyncIterator](),
|
|
291
|
+
closed: false,
|
|
292
|
+
}
|
|
293
|
+
sessions.push(session)
|
|
294
|
+
return Object.assign(run(session), {
|
|
295
|
+
initializationResult: async () => ({ claude_code_version: "2.1.312" }),
|
|
296
|
+
close: () => {
|
|
297
|
+
session.closed = true
|
|
298
|
+
},
|
|
299
|
+
})
|
|
300
|
+
}
|
|
301
|
+
return { sessions, query: query as unknown as typeof SdkQuery }
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
// Claude Code calling clanka's `execute` tool over MCP, each call tagged with
|
|
305
|
+
// its tool use id unless `tagged` is false.
|
|
306
|
+
const callExecute = async (
|
|
307
|
+
session: FakeSession,
|
|
308
|
+
calls: ReadonlyArray<readonly [id: string, script: string]>,
|
|
309
|
+
tagged = true,
|
|
310
|
+
) => {
|
|
311
|
+
const server = session.options.mcpServers?.clanka as {
|
|
312
|
+
readonly instance: {
|
|
313
|
+
connect: (transport: InMemoryTransport) => Promise<void>
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
const [clientTransport, serverTransport] =
|
|
317
|
+
InMemoryTransport.createLinkedPair()
|
|
318
|
+
await server.instance.connect(serverTransport)
|
|
319
|
+
const client = new Client({ name: "claude-code", version: "0.0.0" })
|
|
320
|
+
await client.connect(clientTransport)
|
|
321
|
+
return Promise.all(
|
|
322
|
+
calls.map(([id, script]) =>
|
|
323
|
+
client.callTool({
|
|
324
|
+
name: "execute",
|
|
325
|
+
arguments: { script },
|
|
326
|
+
...(tagged ? { _meta: { "claudecode/toolUseId": id } } : {}),
|
|
327
|
+
}),
|
|
328
|
+
),
|
|
329
|
+
)
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// One API message calling `execute` once per call.
|
|
333
|
+
const executeStep = (calls: ReadonlyArray<readonly [string, string]>) => [
|
|
334
|
+
event({
|
|
335
|
+
type: "message_start",
|
|
336
|
+
message: {
|
|
337
|
+
id: "msg_tools",
|
|
338
|
+
type: "message",
|
|
339
|
+
role: "assistant",
|
|
340
|
+
model: "claude-opus-5-5",
|
|
341
|
+
content: [],
|
|
342
|
+
stop_reason: null,
|
|
343
|
+
stop_sequence: null,
|
|
344
|
+
usage: { input_tokens: 10, output_tokens: 1 },
|
|
345
|
+
},
|
|
346
|
+
}),
|
|
347
|
+
...calls.map(([id, script]) =>
|
|
348
|
+
assistant({
|
|
349
|
+
type: "tool_use",
|
|
350
|
+
id,
|
|
351
|
+
name: "mcp__clanka__execute",
|
|
352
|
+
input: { script },
|
|
353
|
+
}),
|
|
354
|
+
),
|
|
355
|
+
event({
|
|
356
|
+
type: "message_delta",
|
|
357
|
+
delta: { stop_reason: "tool_use", stop_sequence: null },
|
|
358
|
+
usage: { output_tokens: 5 },
|
|
359
|
+
}),
|
|
360
|
+
event({ type: "message_stop" }),
|
|
361
|
+
]
|
|
362
|
+
|
|
363
|
+
const finalStep = [
|
|
364
|
+
assistant({ type: "text", text: "Done." }),
|
|
365
|
+
{
|
|
366
|
+
...frame,
|
|
367
|
+
type: "result",
|
|
368
|
+
subtype: "success",
|
|
369
|
+
is_error: false,
|
|
370
|
+
result: "Done.",
|
|
371
|
+
stop_reason: "end_turn",
|
|
372
|
+
usage: { input_tokens: 10, output_tokens: 2 },
|
|
373
|
+
},
|
|
374
|
+
]
|
|
375
|
+
|
|
376
|
+
const Execute = Toolkit.make(
|
|
377
|
+
Tool.make("execute", {
|
|
378
|
+
parameters: Schema.Struct({ script: Schema.String }),
|
|
379
|
+
success: Schema.String,
|
|
380
|
+
}),
|
|
381
|
+
)
|
|
382
|
+
|
|
383
|
+
// The scripts clanka's `execute` handler ran.
|
|
384
|
+
const executed: Array<string> = []
|
|
385
|
+
|
|
386
|
+
const ExecuteHandlers = Execute.toLayer({
|
|
387
|
+
execute: ({ script }) =>
|
|
388
|
+
Effect.sync(() => {
|
|
389
|
+
executed.push(script)
|
|
390
|
+
return `ran ${script}`
|
|
391
|
+
}),
|
|
392
|
+
})
|
|
393
|
+
|
|
394
|
+
const modelWith = (fake: ReturnType<typeof fakeClaude>) =>
|
|
395
|
+
Layer.merge(
|
|
396
|
+
ClaudeAgent.model("claude-opus-5-5", {
|
|
397
|
+
executable: "/fake/claude",
|
|
398
|
+
query: fake.query,
|
|
399
|
+
}),
|
|
400
|
+
ExecuteHandlers,
|
|
401
|
+
)
|
|
402
|
+
|
|
403
|
+
const runSteps = Effect.fnUntraced(function* (
|
|
404
|
+
fake: ReturnType<typeof fakeClaude>,
|
|
405
|
+
next: (first: Prompt.Prompt, response: Prompt.Prompt) => Prompt.Prompt,
|
|
406
|
+
) {
|
|
407
|
+
const first = Prompt.fromMessages([system, user("go")])
|
|
408
|
+
yield* Effect.gen(function* () {
|
|
409
|
+
const ai = yield* LanguageModel.LanguageModel
|
|
410
|
+
const parts = yield* ai
|
|
411
|
+
.streamText({ prompt: first, toolkit: Execute })
|
|
412
|
+
.pipe(Stream.runCollect)
|
|
413
|
+
yield* ai
|
|
414
|
+
.streamText({
|
|
415
|
+
prompt: next(first, Prompt.fromResponseParts(parts)),
|
|
416
|
+
toolkit: Execute,
|
|
417
|
+
})
|
|
418
|
+
.pipe(Stream.runCollect)
|
|
419
|
+
}).pipe(Effect.provide(modelWith(fake)))
|
|
420
|
+
})
|
|
421
|
+
|
|
422
|
+
describe("ClaudeAgent.model", () => {
|
|
423
|
+
it.effect("hands each parked call the result of its own tool use", () =>
|
|
424
|
+
Effect.gen(function* () {
|
|
425
|
+
const calls = [
|
|
426
|
+
["toolu_a", "a"],
|
|
427
|
+
["toolu_b", "b"],
|
|
428
|
+
] as const
|
|
429
|
+
let results: ReadonlyArray<unknown> = []
|
|
430
|
+
const fake = fakeClaude(async function* (session) {
|
|
431
|
+
await session.input.next()
|
|
432
|
+
yield* executeStep(calls)
|
|
433
|
+
results = await callExecute(session, calls)
|
|
434
|
+
yield* finalStep
|
|
435
|
+
})
|
|
436
|
+
yield* runSteps(fake, (first, response) => Prompt.concat(first, response))
|
|
437
|
+
assert.deepStrictEqual(
|
|
438
|
+
results.map((result: any) => result.content),
|
|
439
|
+
[[{ type: "text", text: "ran a" }], [{ type: "text", text: "ran b" }]],
|
|
440
|
+
)
|
|
441
|
+
assert.lengthOf(fake.sessions, 1)
|
|
442
|
+
}),
|
|
443
|
+
)
|
|
444
|
+
|
|
445
|
+
it.effect(
|
|
446
|
+
"fails untagged calls instead of guessing which call they are",
|
|
447
|
+
() =>
|
|
448
|
+
Effect.gen(function* () {
|
|
449
|
+
const calls = [
|
|
450
|
+
["toolu_a", "a"],
|
|
451
|
+
["toolu_b", "b"],
|
|
452
|
+
] as const
|
|
453
|
+
let results: ReadonlyArray<unknown> = []
|
|
454
|
+
executed.length = 0
|
|
455
|
+
const fake = fakeClaude(async function* (session) {
|
|
456
|
+
await session.input.next()
|
|
457
|
+
yield* executeStep(calls)
|
|
458
|
+
results = await callExecute(session, calls, false)
|
|
459
|
+
yield* finalStep
|
|
460
|
+
})
|
|
461
|
+
// The step may fail; what matters is that no script runs.
|
|
462
|
+
yield* Effect.exit(
|
|
463
|
+
runSteps(fake, (first, response) => Prompt.concat(first, response)),
|
|
464
|
+
)
|
|
465
|
+
assert.deepStrictEqual(
|
|
466
|
+
results.map((result: any) => result.isError),
|
|
467
|
+
[true, true],
|
|
468
|
+
)
|
|
469
|
+
assert.deepStrictEqual(executed, [])
|
|
470
|
+
}),
|
|
471
|
+
)
|
|
472
|
+
|
|
473
|
+
it.effect(
|
|
474
|
+
"starts over when the prompt's answer is not the one Claude gave",
|
|
475
|
+
() =>
|
|
476
|
+
Effect.gen(function* () {
|
|
477
|
+
const fake = fakeClaude(async function* (session) {
|
|
478
|
+
await session.input.next()
|
|
479
|
+
yield* finalStep
|
|
480
|
+
await session.input.next()
|
|
481
|
+
yield* finalStep
|
|
482
|
+
})
|
|
483
|
+
yield* runSteps(fake, (first) =>
|
|
484
|
+
Prompt.fromMessages([
|
|
485
|
+
...first.content,
|
|
486
|
+
Prompt.makeMessage("assistant", {
|
|
487
|
+
content: [Prompt.makePart("text", { text: "Edited." })],
|
|
488
|
+
}),
|
|
489
|
+
user("Repeat your answer"),
|
|
490
|
+
]),
|
|
491
|
+
)
|
|
492
|
+
assert.lengthOf(fake.sessions, 2)
|
|
493
|
+
assert.isTrue(fake.sessions[0]!.closed)
|
|
494
|
+
}),
|
|
495
|
+
)
|
|
496
|
+
|
|
497
|
+
it.effect("closes Claude Code when a step is cut short", () =>
|
|
498
|
+
Effect.gen(function* () {
|
|
499
|
+
const fake = fakeClaude(async function* (session) {
|
|
500
|
+
await session.input.next()
|
|
501
|
+
yield* executeStep([]).slice(0, 1)
|
|
502
|
+
yield event({
|
|
503
|
+
type: "content_block_start",
|
|
504
|
+
index: 0,
|
|
505
|
+
content_block: { type: "text", text: "" },
|
|
506
|
+
})
|
|
507
|
+
yield event({
|
|
508
|
+
type: "content_block_delta",
|
|
509
|
+
index: 0,
|
|
510
|
+
delta: { type: "text_delta", text: "Working" },
|
|
511
|
+
})
|
|
512
|
+
await new Promise(() => {})
|
|
513
|
+
})
|
|
514
|
+
const prompt = Prompt.fromMessages([system, user("go")])
|
|
515
|
+
yield* Effect.gen(function* () {
|
|
516
|
+
const ai = yield* LanguageModel.LanguageModel
|
|
517
|
+
// Steering cuts the stream after a text part, as Agent does.
|
|
518
|
+
yield* ai.streamText({ prompt, toolkit: Execute }).pipe(
|
|
519
|
+
Stream.takeUntil((part) => part.type === "text-delta"),
|
|
520
|
+
Stream.runDrain,
|
|
521
|
+
)
|
|
522
|
+
assert.isTrue(fake.sessions[0]!.closed)
|
|
523
|
+
}).pipe(Effect.provide(modelWith(fake)))
|
|
524
|
+
}),
|
|
525
|
+
)
|
|
526
|
+
})
|