clanka 0.17.3 → 0.18.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 +24 -0
- package/dist/Acp.d.ts.map +1 -1
- package/dist/Acp.js +5 -0
- package/dist/Acp.js.map +1 -1
- package/dist/Agent.d.ts +11 -0
- package/dist/Agent.d.ts.map +1 -1
- package/dist/Agent.js +35 -15
- package/dist/Agent.js.map +1 -1
- package/dist/Agent.test.js +23 -0
- package/dist/Agent.test.js.map +1 -1
- package/dist/AgentExecutor.d.ts.map +1 -1
- package/dist/AgentExecutor.js +40 -8
- package/dist/AgentExecutor.js.map +1 -1
- package/dist/AgentExecutor.test.js +51 -2
- package/dist/AgentExecutor.test.js.map +1 -1
- package/dist/AgentOutput.d.ts +23 -4
- package/dist/AgentOutput.d.ts.map +1 -1
- package/dist/AgentOutput.js +19 -0
- package/dist/AgentOutput.js.map +1 -1
- package/dist/AgentTools.d.ts +4 -4
- package/dist/AgentTools.d.ts.map +1 -1
- package/dist/AgentTools.js +7 -2
- package/dist/AgentTools.js.map +1 -1
- package/dist/AgentTools.test.js +22 -0
- package/dist/AgentTools.test.js.map +1 -1
- package/dist/ClaudeAgent.d.ts.map +1 -1
- package/dist/ClaudeAgent.js +37 -4
- package/dist/ClaudeAgent.js.map +1 -1
- package/dist/ClaudeAgent.test.js +82 -0
- package/dist/ClaudeAgent.test.js.map +1 -1
- package/dist/Codex.js +9 -5
- package/dist/Codex.js.map +1 -1
- package/dist/Codex.test.js +26 -7
- package/dist/Codex.test.js.map +1 -1
- package/dist/Compaction.d.ts +41 -6
- package/dist/Compaction.d.ts.map +1 -1
- package/dist/Compaction.js +52 -11
- package/dist/Compaction.js.map +1 -1
- package/dist/Compaction.test.js +5 -4
- package/dist/Compaction.test.js.map +1 -1
- package/dist/CompactionTransport.test.js +11 -0
- package/dist/CompactionTransport.test.js.map +1 -1
- package/dist/Copilot.d.ts.map +1 -1
- package/dist/Copilot.js +3 -6
- package/dist/Copilot.js.map +1 -1
- package/dist/ModelLayer.d.ts +35 -6
- package/dist/ModelLayer.d.ts.map +1 -1
- package/dist/ModelLayer.js +30 -18
- package/dist/ModelLayer.js.map +1 -1
- package/dist/ModelLayer.test.js +11 -3
- package/dist/ModelLayer.test.js.map +1 -1
- package/dist/OutputFormatter.d.ts.map +1 -1
- package/dist/OutputFormatter.js +3 -0
- package/dist/OutputFormatter.js.map +1 -1
- package/dist/Xai.d.ts.map +1 -1
- package/dist/Xai.js +4 -12
- package/dist/Xai.js.map +1 -1
- package/dist/bin/{Effect-DNYYQ84C.mjs → Effect-5XYMLOiB.mjs} +31 -30
- package/dist/bin/Effect-5XYMLOiB.mjs.map +1 -0
- package/dist/bin/{Latch-BSlIwuxI.mjs → Latch-BnrpWMd1.mjs} +2 -2
- package/dist/bin/{Latch-BSlIwuxI.mjs.map → Latch-BnrpWMd1.mjs.map} +1 -1
- package/dist/bin/{McpClient-C7ctqJWo.mjs → McpClient-DoPNaNzW.mjs} +3 -3
- package/dist/bin/{McpClient-C7ctqJWo.mjs.map → McpClient-DoPNaNzW.mjs.map} +1 -1
- package/dist/bin/{OutputFormatter-DXej05JW.mjs → OutputFormatter-Dqxyk2qD.mjs} +4 -3
- package/dist/bin/{OutputFormatter-DXej05JW.mjs.map → OutputFormatter-Dqxyk2qD.mjs.map} +1 -1
- package/dist/bin/{Schema-CSQN_Zso.mjs → Schema-Box_9VZG.mjs} +6 -3
- package/dist/bin/{Schema-CSQN_Zso.mjs.map → Schema-Box_9VZG.mjs.map} +1 -1
- package/dist/bin/{SemanticSearch-BiwCnpKX.mjs → SemanticSearch-CH125AHH.mjs} +4 -4
- package/dist/bin/{SemanticSearch-BiwCnpKX.mjs.map → SemanticSearch-CH125AHH.mjs.map} +1 -1
- package/dist/bin/{Stream-Dwvj9X5X.mjs → Stream-C51usiew.mjs} +3 -3
- package/dist/bin/{Stream-Dwvj9X5X.mjs.map → Stream-C51usiew.mjs.map} +1 -1
- package/dist/bin/cli.mjs +337 -229
- package/dist/bin/cli.mjs.map +1 -1
- package/dist/bin/{upgrade-BSNjCnra.mjs → upgrade-HMRGSqq8.mjs} +4 -4
- package/dist/bin/{upgrade-BSNjCnra.mjs.map → upgrade-HMRGSqq8.mjs.map} +1 -1
- package/dist/cli.js +45 -5
- package/dist/cli.js.map +1 -1
- package/package.json +1 -1
- package/src/Acp.ts +7 -0
- package/src/Agent.test.ts +27 -0
- package/src/Agent.ts +49 -15
- package/src/AgentExecutor.test.ts +80 -2
- package/src/AgentExecutor.ts +70 -10
- package/src/AgentOutput.ts +23 -0
- package/src/AgentTools.test.ts +28 -0
- package/src/AgentTools.ts +7 -2
- package/src/ClaudeAgent.test.ts +113 -0
- package/src/ClaudeAgent.ts +51 -4
- package/src/Codex.test.ts +37 -9
- package/src/Codex.ts +25 -20
- package/src/Compaction.test.ts +18 -15
- package/src/Compaction.ts +89 -20
- package/src/CompactionTransport.test.ts +36 -0
- package/src/Copilot.ts +5 -6
- package/src/ModelLayer.test.ts +17 -2
- package/src/ModelLayer.ts +68 -22
- package/src/OutputFormatter.ts +3 -0
- package/src/Xai.ts +8 -12
- package/src/cli.ts +97 -5
- package/dist/bin/Effect-DNYYQ84C.mjs.map +0 -1
|
@@ -6,8 +6,11 @@ import * as Effect from "effect/Effect"
|
|
|
6
6
|
import * as Fiber from "effect/Fiber"
|
|
7
7
|
import * as FileSystem from "effect/FileSystem"
|
|
8
8
|
import * as Layer from "effect/Layer"
|
|
9
|
+
import * as Schema from "effect/Schema"
|
|
9
10
|
import * as Stream from "effect/Stream"
|
|
10
11
|
import * as TestClock from "effect/testing/TestClock"
|
|
12
|
+
import * as Tool from "effect/ai/Tool"
|
|
13
|
+
import * as Toolkit from "effect/ai/Toolkit"
|
|
11
14
|
import * as FetchHttpClient from "effect/http/FetchHttpClient"
|
|
12
15
|
import * as HttpEffect from "effect/http/HttpEffect"
|
|
13
16
|
import * as HttpServer from "effect/http/HttpServer"
|
|
@@ -24,6 +27,10 @@ const withRpc = <A, E, R>(
|
|
|
24
27
|
f: (
|
|
25
28
|
executor: AgentExecutor.AgentExecutor["Service"],
|
|
26
29
|
) => Effect.Effect<A, E, R>,
|
|
30
|
+
server: (
|
|
31
|
+
directory: string,
|
|
32
|
+
) => ReturnType<typeof AgentExecutor.layerRpcServer> = (directory) =>
|
|
33
|
+
AgentExecutor.layerRpcServer({ directory }),
|
|
27
34
|
) =>
|
|
28
35
|
Effect.gen(function* () {
|
|
29
36
|
const fs = yield* FileSystem.FileSystem
|
|
@@ -31,7 +38,7 @@ const withRpc = <A, E, R>(
|
|
|
31
38
|
const { protocol, httpEffect } =
|
|
32
39
|
yield* RpcServer.makeProtocolWithHttpEffect()
|
|
33
40
|
yield* Layer.build(
|
|
34
|
-
|
|
41
|
+
server(directory).pipe(
|
|
35
42
|
Layer.provide(Layer.succeed(RpcServer.Protocol, protocol)),
|
|
36
43
|
),
|
|
37
44
|
)
|
|
@@ -182,6 +189,55 @@ describe("RPC subagent executor", () => {
|
|
|
182
189
|
)
|
|
183
190
|
})
|
|
184
191
|
|
|
192
|
+
describe("RPC executeUnsafe", () => {
|
|
193
|
+
it.live("writes Uint8Array writeFile content as raw bytes", () =>
|
|
194
|
+
withRpc((executor) =>
|
|
195
|
+
Effect.gen(function* () {
|
|
196
|
+
const fs = yield* FileSystem.FileSystem
|
|
197
|
+
const { environment } = yield* executor.capabilities
|
|
198
|
+
yield* executor.executeUnsafe({
|
|
199
|
+
tool: "writeFile",
|
|
200
|
+
params: { path: "data.bin", content: new Uint8Array([0, 255, 128]) },
|
|
201
|
+
})
|
|
202
|
+
const bytes = yield* fs.readFile(
|
|
203
|
+
NodePath.join(environment.cwd, "data.bin"),
|
|
204
|
+
)
|
|
205
|
+
assert.deepStrictEqual(Array.from(bytes), [0, 255, 128])
|
|
206
|
+
}),
|
|
207
|
+
),
|
|
208
|
+
)
|
|
209
|
+
|
|
210
|
+
it.live("passes a caller's tool params shaped like bytes unchanged", () => {
|
|
211
|
+
const payload = { _tag: "Bytes", base64: "AA==" } as const
|
|
212
|
+
const Echo = Toolkit.make(
|
|
213
|
+
Tool.make("echo", {
|
|
214
|
+
parameters: Schema.Struct({
|
|
215
|
+
payload: Schema.TaggedStruct("Bytes", { base64: Schema.String }),
|
|
216
|
+
}),
|
|
217
|
+
success: Schema.TaggedStruct("Bytes", { base64: Schema.String }),
|
|
218
|
+
}),
|
|
219
|
+
)
|
|
220
|
+
return withRpc(
|
|
221
|
+
(executor) =>
|
|
222
|
+
Effect.gen(function* () {
|
|
223
|
+
// `executeUnsafe` is typed for the built-in tools, but the server
|
|
224
|
+
// runs the caller's tools too.
|
|
225
|
+
const output = yield* executor.executeUnsafe({
|
|
226
|
+
tool: "echo" as never,
|
|
227
|
+
params: { payload } as never,
|
|
228
|
+
})
|
|
229
|
+
assert.deepStrictEqual(output, payload)
|
|
230
|
+
}),
|
|
231
|
+
(directory) =>
|
|
232
|
+
AgentExecutor.layerRpcServer({ directory, tools: Echo }).pipe(
|
|
233
|
+
Layer.provide(
|
|
234
|
+
Echo.toLayer({ echo: ({ payload }) => Effect.succeed(payload) }),
|
|
235
|
+
),
|
|
236
|
+
),
|
|
237
|
+
)
|
|
238
|
+
})
|
|
239
|
+
})
|
|
240
|
+
|
|
185
241
|
// Both executor modes with the same limits. The RPC mode serves
|
|
186
242
|
// `layerRpcServer` through an in-memory web handler, so requests go through
|
|
187
243
|
// the real serialization without opening a socket.
|
|
@@ -268,6 +324,28 @@ const withSlowGh = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
|
|
|
268
324
|
}),
|
|
269
325
|
)
|
|
270
326
|
|
|
327
|
+
// Node's default `console.log` breaks values over many lines and pads short
|
|
328
|
+
// array entries into columns. That whitespace is paid for in every prompt.
|
|
329
|
+
describe("console.log in scripts", () => {
|
|
330
|
+
it.live("prints arrays and objects on one line without padding", () =>
|
|
331
|
+
Effect.gen(function* () {
|
|
332
|
+
const output = yield* runScript(`
|
|
333
|
+
console.log([".changeset", ".envrc", ".git", ".github", ".gitignore", ".husky", ".oxlintrc.json", ".prettierignore", ".prettierrc.json", ".specs", "AGENTS.md", "CHANGELOG.md", "CLAUDE.md", "README.md", "examples", "flake.lock", "flake.nix", "package.json", "pnpm-lock.yaml", "scripts", "src", "tsconfig.json", "tsdown.config.ts", "vitest.config.ts"])
|
|
334
|
+
console.log([0, 37, 74, 111, 148, 185, 222, 259, 296, 333, 370, 407, 444, 481, 518, 555, 592, 629, 666, 703, 740, 777, 814, 851, 888, 925, 962, 999, 1036, 1073])
|
|
335
|
+
console.log({ name: "my-project", version: "1.0.0", scripts: { check: "oxlint -c .oxlintrc.json --fix && pnpm tsc -b tsconfig.json", test: "vitest run" } })`)
|
|
336
|
+
const lines = output.trimEnd().split("\n")
|
|
337
|
+
assert.strictEqual(lines.length, 3, output)
|
|
338
|
+
assert.include(lines[0], "vitest.config.ts")
|
|
339
|
+
assert.include(lines[1], "1073")
|
|
340
|
+
assert.include(lines[2], "vitest run")
|
|
341
|
+
assert.notMatch(output, / {2,}/, "no padding or indentation")
|
|
342
|
+
}).pipe(
|
|
343
|
+
Effect.timeout("3 seconds"),
|
|
344
|
+
Effect.provide(executorModes.local({})),
|
|
345
|
+
),
|
|
346
|
+
)
|
|
347
|
+
})
|
|
348
|
+
|
|
271
349
|
for (const [mode, executorLayer] of Object.entries(executorModes)) {
|
|
272
350
|
describe(`${mode} executor environment`, () => {
|
|
273
351
|
// A cancelled script keeps the directory it changed to, as an ACP
|
|
@@ -533,4 +611,4 @@ sleep(100).then(() => console.constructor.constructor("return process")().exit(7
|
|
|
533
611
|
),
|
|
534
612
|
)
|
|
535
613
|
})
|
|
536
|
-
}
|
|
614
|
+
}
|
package/src/AgentExecutor.ts
CHANGED
|
@@ -54,6 +54,7 @@ import * as RpcClient from "effect/rpc/RpcClient"
|
|
|
54
54
|
import * as Layer from "effect/Layer"
|
|
55
55
|
import * as RpcServer from "effect/rpc/RpcServer"
|
|
56
56
|
import * as Schema from "effect/Schema"
|
|
57
|
+
import * as SchemaTransformation from "effect/SchemaTransformation"
|
|
57
58
|
import * as RpcGroup from "effect/rpc/RpcGroup"
|
|
58
59
|
import * as Rpc from "effect/rpc/Rpc"
|
|
59
60
|
import * as Result from "effect/Result"
|
|
@@ -775,11 +776,16 @@ export const makeRpc = Effect.gen(function* () {
|
|
|
775
776
|
.renderEnvironment({ environment, reset: options?.reset ?? false })
|
|
776
777
|
.pipe(Effect.orDie),
|
|
777
778
|
executeUnsafe({ environment, ...options }) {
|
|
778
|
-
return
|
|
779
|
-
.
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
779
|
+
return encodeRpcParams(options.tool, options.params).pipe(
|
|
780
|
+
Effect.flatMap((params) =>
|
|
781
|
+
client.executeUnsafe(
|
|
782
|
+
environment === undefined
|
|
783
|
+
? { ...options, params }
|
|
784
|
+
: { ...options, params, environment },
|
|
785
|
+
),
|
|
786
|
+
),
|
|
787
|
+
Effect.orDie,
|
|
788
|
+
)
|
|
783
789
|
},
|
|
784
790
|
})
|
|
785
791
|
})
|
|
@@ -994,9 +1000,13 @@ export const layerRpcServer = <Toolkit extends Toolkit.Any = never>(
|
|
|
994
1000
|
|
|
995
1001
|
return queue
|
|
996
1002
|
}),
|
|
997
|
-
executeUnsafe(opts)
|
|
998
|
-
|
|
999
|
-
|
|
1003
|
+
executeUnsafe: (opts) =>
|
|
1004
|
+
decodeRpcParams(opts.tool, opts.params).pipe(
|
|
1005
|
+
Effect.orDie,
|
|
1006
|
+
Effect.flatMap((params) =>
|
|
1007
|
+
local.executor.executeUnsafe({ ...opts, params } as any),
|
|
1008
|
+
),
|
|
1009
|
+
),
|
|
1000
1010
|
})
|
|
1001
1011
|
}),
|
|
1002
1012
|
),
|
|
@@ -1104,6 +1114,42 @@ export class Rpcs extends RpcGroup.make(
|
|
|
1104
1114
|
// Internal
|
|
1105
1115
|
// -------------------------------------------
|
|
1106
1116
|
|
|
1117
|
+
// `executeUnsafe` params cross RPC as JSON. `writeFile` is the only built-in
|
|
1118
|
+
// tool whose params JSON can't carry: its content may be bytes. The default
|
|
1119
|
+
// JSON form of bytes is a base64 string, which the server couldn't tell apart
|
|
1120
|
+
// from string content, so over RPC bytes travel as `{ base64 }` instead.
|
|
1121
|
+
const RpcWriteFileParams = Schema.Struct({
|
|
1122
|
+
...AgentTools.tools.writeFile.parametersSchema.fields,
|
|
1123
|
+
content: Schema.Union([
|
|
1124
|
+
Schema.String,
|
|
1125
|
+
Schema.Struct({ base64: Schema.Uint8ArrayFromBase64 }).pipe(
|
|
1126
|
+
Schema.decodeTo(
|
|
1127
|
+
Schema.Uint8Array,
|
|
1128
|
+
SchemaTransformation.transform({
|
|
1129
|
+
decode: ({ base64 }) => base64,
|
|
1130
|
+
encode: (base64) => ({ base64 }),
|
|
1131
|
+
}),
|
|
1132
|
+
),
|
|
1133
|
+
),
|
|
1134
|
+
]),
|
|
1135
|
+
})
|
|
1136
|
+
|
|
1137
|
+
const encodeRpcParams = (
|
|
1138
|
+
tool: string,
|
|
1139
|
+
params: unknown,
|
|
1140
|
+
): Effect.Effect<Schema.Json, Schema.SchemaError> =>
|
|
1141
|
+
tool === "writeFile"
|
|
1142
|
+
? Schema.encodeUnknownEffect(RpcWriteFileParams)(params)
|
|
1143
|
+
: Effect.succeed(params as Schema.Json)
|
|
1144
|
+
|
|
1145
|
+
const decodeRpcParams = (
|
|
1146
|
+
tool: string,
|
|
1147
|
+
params: Schema.Json,
|
|
1148
|
+
): Effect.Effect<unknown, Schema.SchemaError> =>
|
|
1149
|
+
tool === "writeFile"
|
|
1150
|
+
? Schema.decodeUnknownEffect(RpcWriteFileParams)(params)
|
|
1151
|
+
: Effect.succeed(params)
|
|
1152
|
+
|
|
1107
1153
|
const handleRequest = (
|
|
1108
1154
|
opts: ExecuteOptions,
|
|
1109
1155
|
request: typeof AgentRequest.Type,
|
|
@@ -1384,7 +1430,11 @@ function runScriptWorker(load: (id: string) => unknown): void {
|
|
|
1384
1430
|
})
|
|
1385
1431
|
|
|
1386
1432
|
const sandbox: Record<string, unknown> = {
|
|
1387
|
-
|
|
1433
|
+
// Matches `inspectOptions`, which the worker source cannot reference.
|
|
1434
|
+
console: new Console({
|
|
1435
|
+
stdout,
|
|
1436
|
+
inspectOptions: { colors: false, compact: true, breakLength: Infinity },
|
|
1437
|
+
}),
|
|
1388
1438
|
fetch,
|
|
1389
1439
|
process: undefined,
|
|
1390
1440
|
}
|
|
@@ -1432,6 +1482,16 @@ ${fromFailure}
|
|
|
1432
1482
|
(${runScriptWorker})(require)
|
|
1433
1483
|
`
|
|
1434
1484
|
|
|
1485
|
+
/**
|
|
1486
|
+
* Prints values on one line, without the column padding Node uses for short
|
|
1487
|
+
* array entries. `colors` replaces `colorMode`: Node rejects both together.
|
|
1488
|
+
*/
|
|
1489
|
+
const inspectOptions = {
|
|
1490
|
+
colors: false,
|
|
1491
|
+
compact: true,
|
|
1492
|
+
breakLength: Infinity,
|
|
1493
|
+
}
|
|
1494
|
+
|
|
1435
1495
|
const homeDirectory = (): Option.Option<string> => {
|
|
1436
1496
|
const fromEnv = process.env.HOME
|
|
1437
1497
|
if (fromEnv) return Option.some(fromEnv)
|
|
@@ -1448,7 +1508,7 @@ const makeConsole = Effect.fn(function* (
|
|
|
1448
1508
|
const writable = new QueueWriteStream(queue)
|
|
1449
1509
|
const newConsole = new NodeConsole.Console({
|
|
1450
1510
|
stdout: writable,
|
|
1451
|
-
|
|
1511
|
+
inspectOptions,
|
|
1452
1512
|
})
|
|
1453
1513
|
yield* Effect.addFinalizer(() => {
|
|
1454
1514
|
writable.end()
|
package/src/AgentOutput.ts
CHANGED
|
@@ -214,6 +214,27 @@ export class CompactionEnded extends Schema.TaggedClass<CompactionEnded>()(
|
|
|
214
214
|
},
|
|
215
215
|
) {}
|
|
216
216
|
|
|
217
|
+
/**
|
|
218
|
+
* Emitted when the provider could not continue its live session and was sent
|
|
219
|
+
* the whole history again, so the provider cache missed on it.
|
|
220
|
+
*
|
|
221
|
+
* `reason` is reported by the provider: `history-changed` (an earlier message
|
|
222
|
+
* was rewritten, e.g. by compaction), `step-abandoned` (the last
|
|
223
|
+
* step did not finish: a retry, timeout or interrupt) or `tools-changed`.
|
|
224
|
+
* `contextTokens` and `cacheWrite` are the usage of the resent request.
|
|
225
|
+
*
|
|
226
|
+
* @since 1.0.0
|
|
227
|
+
* @category Output
|
|
228
|
+
*/
|
|
229
|
+
export class SessionRestarted extends Schema.TaggedClass<SessionRestarted>()(
|
|
230
|
+
"SessionRestarted",
|
|
231
|
+
{
|
|
232
|
+
reason: Schema.String,
|
|
233
|
+
contextTokens: Schema.Number,
|
|
234
|
+
cacheWrite: Schema.Number,
|
|
235
|
+
},
|
|
236
|
+
) {}
|
|
237
|
+
|
|
217
238
|
/**
|
|
218
239
|
* Emitted when AGENTS.md files or skills the model has not seen yet are
|
|
219
240
|
* added to the history, after a directory change or a compaction. `update`
|
|
@@ -242,6 +263,7 @@ export type ContentPart =
|
|
|
242
263
|
| ExecuteOutputCapped
|
|
243
264
|
| CompactionStarted
|
|
244
265
|
| CompactionEnded
|
|
266
|
+
| SessionRestarted
|
|
245
267
|
| EnvironmentUpdated
|
|
246
268
|
|
|
247
269
|
export const ContentPart = Schema.Union([
|
|
@@ -257,6 +279,7 @@ export const ContentPart = Schema.Union([
|
|
|
257
279
|
ExecuteOutputCapped,
|
|
258
280
|
CompactionStarted,
|
|
259
281
|
CompactionEnded,
|
|
282
|
+
SessionRestarted,
|
|
260
283
|
EnvironmentUpdated,
|
|
261
284
|
])
|
|
262
285
|
|
package/src/AgentTools.test.ts
CHANGED
|
@@ -5,7 +5,9 @@ import * as Effect from "effect/Effect"
|
|
|
5
5
|
import * as FileSystem from "effect/FileSystem"
|
|
6
6
|
import * as Layer from "effect/Layer"
|
|
7
7
|
import * as Path from "effect/Path"
|
|
8
|
+
import * as Stream from "effect/Stream"
|
|
8
9
|
import * as AgentExecutor from "./AgentExecutor.ts"
|
|
10
|
+
import { subagentsUnavailable } from "./AgentTools.ts"
|
|
9
11
|
|
|
10
12
|
const match = "ignored-directory-regression-match"
|
|
11
13
|
|
|
@@ -114,3 +116,29 @@ describe("rg", () => {
|
|
|
114
116
|
),
|
|
115
117
|
)
|
|
116
118
|
})
|
|
119
|
+
|
|
120
|
+
describe("writeFile", () => {
|
|
121
|
+
it.effect("writes Uint8Array content as raw bytes", () =>
|
|
122
|
+
withProject(
|
|
123
|
+
Effect.gen(function* () {
|
|
124
|
+
const fs = yield* FileSystem.FileSystem
|
|
125
|
+
const path = yield* Path.Path
|
|
126
|
+
const executor = yield* AgentExecutor.AgentExecutor
|
|
127
|
+
const { environment } = yield* executor.capabilities
|
|
128
|
+
yield* executor
|
|
129
|
+
.execute({
|
|
130
|
+
environment,
|
|
131
|
+
onEnvironment: () => Effect.void,
|
|
132
|
+
script:
|
|
133
|
+
'await writeFile({ path: "data.bin", content: new Uint8Array([0, 255, 128]) })',
|
|
134
|
+
onImage: () => Effect.void,
|
|
135
|
+
onTaskComplete: () => Effect.void,
|
|
136
|
+
subagents: subagentsUnavailable,
|
|
137
|
+
})
|
|
138
|
+
.pipe(Stream.runDrain)
|
|
139
|
+
const bytes = yield* fs.readFile(path.join(environment.cwd, "data.bin"))
|
|
140
|
+
assert.deepStrictEqual(Array.from(bytes), [0, 255, 128])
|
|
141
|
+
}),
|
|
142
|
+
),
|
|
143
|
+
)
|
|
144
|
+
})
|
package/src/AgentTools.ts
CHANGED
|
@@ -402,7 +402,10 @@ export const AgentTools = Toolkit.make(
|
|
|
402
402
|
"Write content to a file, creating parent directories if needed.",
|
|
403
403
|
parameters: Schema.Struct({
|
|
404
404
|
path: Schema.String,
|
|
405
|
-
content: Schema.
|
|
405
|
+
content: Schema.Union([
|
|
406
|
+
Schema.String,
|
|
407
|
+
Schema.Uint8Array.annotate({ identifier: "Uint8Array" }),
|
|
408
|
+
]),
|
|
406
409
|
}),
|
|
407
410
|
dependencies: [CurrentDirectory],
|
|
408
411
|
}),
|
|
@@ -639,7 +642,9 @@ export const AgentToolHandlersNoDeps = AgentToolsWithSearch.toLayer(
|
|
|
639
642
|
yield* fs.makeDirectory(pathService.dirname(path), {
|
|
640
643
|
recursive: true,
|
|
641
644
|
})
|
|
642
|
-
yield*
|
|
645
|
+
yield* typeof options.content === "string"
|
|
646
|
+
? fs.writeFileString(path, options.content)
|
|
647
|
+
: fs.writeFile(path, options.content)
|
|
643
648
|
yield* SemanticSearch.maybeUpdateFile(path)
|
|
644
649
|
}, Effect.orDie),
|
|
645
650
|
removeFile: Effect.fn("AgentTools.removeFile")(function* (path) {
|
package/src/ClaudeAgent.test.ts
CHANGED
|
@@ -12,14 +12,18 @@ import * as Effect from "effect/Effect"
|
|
|
12
12
|
import * as Exit from "effect/Exit"
|
|
13
13
|
import * as Fiber from "effect/Fiber"
|
|
14
14
|
import * as Layer from "effect/Layer"
|
|
15
|
+
import * as Option from "effect/Option"
|
|
15
16
|
import * as Base64 from "effect/encoding/Base64"
|
|
16
17
|
import * as Schema from "effect/Schema"
|
|
17
18
|
import * as Scope from "effect/Scope"
|
|
18
19
|
import * as Stream from "effect/Stream"
|
|
19
20
|
import * as LanguageModel from "effect/ai/LanguageModel"
|
|
20
21
|
import * as Prompt from "effect/ai/Prompt"
|
|
22
|
+
import type * as Response from "effect/ai/Response"
|
|
21
23
|
import * as Tool from "effect/ai/Tool"
|
|
22
24
|
import * as Toolkit from "effect/ai/Toolkit"
|
|
25
|
+
import * as Agent from "./Agent.ts"
|
|
26
|
+
import * as AgentExecutor from "./AgentExecutor.ts"
|
|
23
27
|
import * as ClaudeAgent from "./ClaudeAgent.ts"
|
|
24
28
|
import { tinyPng } from "./fixtures/TestImages.ts"
|
|
25
29
|
|
|
@@ -492,6 +496,11 @@ const runSteps = Effect.fnUntraced(function* (
|
|
|
492
496
|
}).pipe(Effect.provide(modelWith(fake)))
|
|
493
497
|
})
|
|
494
498
|
|
|
499
|
+
// How a step's finish part says Claude Code re-read the history.
|
|
500
|
+
const historyResent = (parts: ReadonlyArray<Response.StreamPart<any>>) =>
|
|
501
|
+
(parts.find((part) => part.type === "finish")?.metadata.clanka as any)
|
|
502
|
+
?.historyResent
|
|
503
|
+
|
|
495
504
|
describe("ClaudeAgent.model", () => {
|
|
496
505
|
it.effect("hands each parked call the result of its own tool use", () =>
|
|
497
506
|
Effect.gen(function* () {
|
|
@@ -683,6 +692,110 @@ describe("ClaudeAgent.model", () => {
|
|
|
683
692
|
}),
|
|
684
693
|
)
|
|
685
694
|
|
|
695
|
+
it.effect("tells the caller why it started the session over", () =>
|
|
696
|
+
Effect.gen(function* () {
|
|
697
|
+
const fake = fakeClaude(async function* (session) {
|
|
698
|
+
await session.input.next()
|
|
699
|
+
yield* finalStep
|
|
700
|
+
})
|
|
701
|
+
const first = Prompt.fromMessages([system, user("go")])
|
|
702
|
+
yield* Effect.gen(function* () {
|
|
703
|
+
const ai = yield* LanguageModel.LanguageModel
|
|
704
|
+
const step = (prompt: Prompt.Prompt) =>
|
|
705
|
+
ai.streamText({ prompt, toolkit: Execute }).pipe(Stream.runCollect)
|
|
706
|
+
assert.isUndefined(historyResent(yield* step(first)))
|
|
707
|
+
const edited = Prompt.fromMessages([
|
|
708
|
+
...first.content,
|
|
709
|
+
Prompt.makeMessage("assistant", {
|
|
710
|
+
content: [Prompt.makePart("text", { text: "Edited." })],
|
|
711
|
+
}),
|
|
712
|
+
user("Repeat your answer"),
|
|
713
|
+
])
|
|
714
|
+
assert.deepStrictEqual(historyResent(yield* step(edited)), {
|
|
715
|
+
reason: "history-changed",
|
|
716
|
+
})
|
|
717
|
+
}).pipe(Effect.provide(modelWith(fake)))
|
|
718
|
+
assert.lengthOf(fake.sessions, 2)
|
|
719
|
+
}),
|
|
720
|
+
)
|
|
721
|
+
|
|
722
|
+
it.effect("keeps the session when the agent is steered mid-step", () =>
|
|
723
|
+
Effect.gen(function* () {
|
|
724
|
+
const steered = Promise.withResolvers<void>()
|
|
725
|
+
let steering = ""
|
|
726
|
+
const fake = fakeClaude(async function* (session) {
|
|
727
|
+
await session.input.next()
|
|
728
|
+
// A restarted session answers straight away.
|
|
729
|
+
if (session !== fake.sessions[0]) return yield* finalStep
|
|
730
|
+
yield* replying
|
|
731
|
+
await steered.promise
|
|
732
|
+
yield event({ type: "content_block_stop", index: 0 })
|
|
733
|
+
yield assistant({ type: "text", text: "Working" })
|
|
734
|
+
yield* executeStep([["toolu_a", "a"]]).slice(1)
|
|
735
|
+
await callExecute(session, [["toolu_a", "a"]])
|
|
736
|
+
steering = JSON.stringify((await session.input.next()).value)
|
|
737
|
+
yield* finalStep
|
|
738
|
+
})
|
|
739
|
+
const executor = AgentExecutor.AgentExecutor.of({
|
|
740
|
+
capabilities: Effect.succeed(
|
|
741
|
+
new AgentExecutor.Capabilities({
|
|
742
|
+
toolsDts: "",
|
|
743
|
+
environment: new AgentExecutor.ExecutionEnvironment({
|
|
744
|
+
cwd: "/",
|
|
745
|
+
baseline: [],
|
|
746
|
+
seen: [],
|
|
747
|
+
}),
|
|
748
|
+
instructions: Option.none(),
|
|
749
|
+
supportsSearch: false,
|
|
750
|
+
tools: [],
|
|
751
|
+
skills: [],
|
|
752
|
+
}),
|
|
753
|
+
),
|
|
754
|
+
execute: () => Stream.succeed("ran a"),
|
|
755
|
+
executeUnsafe: () => Effect.die("executeUnsafe not implemented"),
|
|
756
|
+
})
|
|
757
|
+
const summary = yield* Effect.gen(function* () {
|
|
758
|
+
const agent = yield* Agent.make
|
|
759
|
+
let sentSteering = false
|
|
760
|
+
return yield* agent.send({ prompt: "go" }).pipe(
|
|
761
|
+
Effect.flatMap((stream) =>
|
|
762
|
+
stream.pipe(
|
|
763
|
+
Stream.runForEach((output) =>
|
|
764
|
+
Effect.gen(function* () {
|
|
765
|
+
if (output._tag !== "ReasoningDelta" || sentSteering) return
|
|
766
|
+
sentSteering = true
|
|
767
|
+
yield* Effect.forkChild(agent.steer("Also check the docs"), {
|
|
768
|
+
startImmediately: true,
|
|
769
|
+
})
|
|
770
|
+
steered.resolve()
|
|
771
|
+
}),
|
|
772
|
+
),
|
|
773
|
+
Effect.as(""),
|
|
774
|
+
Effect.catchTag("AgentFinished", (finished) =>
|
|
775
|
+
Effect.succeed(finished.summary),
|
|
776
|
+
),
|
|
777
|
+
),
|
|
778
|
+
),
|
|
779
|
+
)
|
|
780
|
+
}).pipe(
|
|
781
|
+
Effect.provideService(AgentExecutor.AgentExecutor, executor),
|
|
782
|
+
Effect.provide(
|
|
783
|
+
Layer.merge(
|
|
784
|
+
ClaudeAgent.model("claude-opus-5-5", {
|
|
785
|
+
executable: "/fake/claude",
|
|
786
|
+
query: fake.query,
|
|
787
|
+
}),
|
|
788
|
+
Agent.ConversationMode.layer(true),
|
|
789
|
+
),
|
|
790
|
+
),
|
|
791
|
+
Effect.scoped,
|
|
792
|
+
)
|
|
793
|
+
assert.strictEqual(summary, "Done.")
|
|
794
|
+
assert.lengthOf(fake.sessions, 1)
|
|
795
|
+
assert.include(steering, "Also check the docs")
|
|
796
|
+
}),
|
|
797
|
+
)
|
|
798
|
+
|
|
686
799
|
it.effect("closes Claude Code when a step is cut short", () =>
|
|
687
800
|
Effect.gen(function* () {
|
|
688
801
|
const fake = fakeClaude(async function* (session) {
|
package/src/ClaudeAgent.ts
CHANGED
|
@@ -696,6 +696,11 @@ interface Session {
|
|
|
696
696
|
outcome: Outcome | undefined
|
|
697
697
|
}
|
|
698
698
|
|
|
699
|
+
// The tools a session was started with; a step with other tools cannot
|
|
700
|
+
// continue it.
|
|
701
|
+
const toolNames = (provider: LanguageModel.ProviderOptions) =>
|
|
702
|
+
provider.tools.map((tool) => tool.name).join(",")
|
|
703
|
+
|
|
699
704
|
// Ends `messages` before closing the scope, which interrupts the fiber that
|
|
700
705
|
// would end it, so a step still reading the session fails instead of waiting.
|
|
701
706
|
const closeSession = (session: Session) =>
|
|
@@ -759,7 +764,7 @@ const startSession = Effect.fnUntraced(function* (
|
|
|
759
764
|
bridge,
|
|
760
765
|
messages,
|
|
761
766
|
scope,
|
|
762
|
-
toolNames: provider
|
|
767
|
+
toolNames: toolNames(provider),
|
|
763
768
|
seen: provider.prompt.content,
|
|
764
769
|
outcome: undefined,
|
|
765
770
|
}
|
|
@@ -824,7 +829,7 @@ const continuation = (
|
|
|
824
829
|
const response = messages[seen.length]
|
|
825
830
|
if (
|
|
826
831
|
outcome === undefined ||
|
|
827
|
-
session.toolNames !== provider
|
|
832
|
+
session.toolNames !== toolNames(provider) ||
|
|
828
833
|
response?.role !== "assistant" ||
|
|
829
834
|
!seen.every((message, i) => messages[i] === message) ||
|
|
830
835
|
!isDeepStrictEqual(appended(response), emitted(outcome))
|
|
@@ -854,6 +859,36 @@ const continuation = (
|
|
|
854
859
|
return { ids, results, users }
|
|
855
860
|
}
|
|
856
861
|
|
|
862
|
+
// Why a step could not continue the live session. A step that did not
|
|
863
|
+
// finish closes its session, so no session also means an abandoned step.
|
|
864
|
+
const restartReason = (
|
|
865
|
+
session: Session | undefined,
|
|
866
|
+
provider: LanguageModel.ProviderOptions,
|
|
867
|
+
) =>
|
|
868
|
+
session === undefined || session.outcome === undefined
|
|
869
|
+
? "step-abandoned"
|
|
870
|
+
: session.toolNames !== toolNames(provider)
|
|
871
|
+
? "tools-changed"
|
|
872
|
+
: "history-changed"
|
|
873
|
+
|
|
874
|
+
// Marks the finish part of the first step of a restarted session, so the
|
|
875
|
+
// Agent can report what the restart cost.
|
|
876
|
+
const reportRestart = (
|
|
877
|
+
step: Stream.Stream<Response.StreamPartEncoded, AiError.AiError>,
|
|
878
|
+
reason: string,
|
|
879
|
+
) =>
|
|
880
|
+
Stream.map(step, (part) =>
|
|
881
|
+
part.type === "finish"
|
|
882
|
+
? {
|
|
883
|
+
...part,
|
|
884
|
+
metadata: {
|
|
885
|
+
...part.metadata,
|
|
886
|
+
clanka: { historyResent: { reason } },
|
|
887
|
+
},
|
|
888
|
+
}
|
|
889
|
+
: part,
|
|
890
|
+
)
|
|
891
|
+
|
|
857
892
|
const continueSession = (
|
|
858
893
|
session: Session,
|
|
859
894
|
plan: NonNullable<ReturnType<typeof continuation>>,
|
|
@@ -1090,6 +1125,8 @@ const layerLanguageModel = (
|
|
|
1090
1125
|
// Set while a step is read. Another step would close the session under
|
|
1091
1126
|
// it, so it fails instead.
|
|
1092
1127
|
let reading = false
|
|
1128
|
+
// Whether a session has run, so a new one is a restart.
|
|
1129
|
+
let started = false
|
|
1093
1130
|
|
|
1094
1131
|
const step = (provider: LanguageModel.ProviderOptions) =>
|
|
1095
1132
|
Stream.suspend(() => {
|
|
@@ -1113,9 +1150,14 @@ const layerLanguageModel = (
|
|
|
1113
1150
|
current.outcome = undefined
|
|
1114
1151
|
return readStep(current, closeCurrent)
|
|
1115
1152
|
}
|
|
1153
|
+
const reason = started
|
|
1154
|
+
? restartReason(current, provider)
|
|
1155
|
+
: undefined
|
|
1116
1156
|
yield* closeCurrent
|
|
1117
1157
|
current = yield* startSession(model, options, provider, true)
|
|
1118
|
-
|
|
1158
|
+
started = true
|
|
1159
|
+
const read = readStep(current, closeCurrent)
|
|
1160
|
+
return reason === undefined ? read : reportRestart(read, reason)
|
|
1119
1161
|
}),
|
|
1120
1162
|
).pipe(
|
|
1121
1163
|
Stream.ensuring(
|
|
@@ -1196,6 +1238,11 @@ export const model = (
|
|
|
1196
1238
|
// The system prompt stays in the Prompt, where `queryOptions` reads
|
|
1197
1239
|
// it. No Compaction.SummarizerTransform: Claude Code takes no
|
|
1198
1240
|
// per-request output cap.
|
|
1199
|
-
|
|
1241
|
+
// A step cut short closes the session, so steering waits for the step
|
|
1242
|
+
// to end and the session continues.
|
|
1243
|
+
AgentModelConfig.layer({
|
|
1244
|
+
supportsImages: options?.supportsImages,
|
|
1245
|
+
cutStreamOnSteering: false,
|
|
1246
|
+
}),
|
|
1200
1247
|
),
|
|
1201
1248
|
)
|
package/src/Codex.test.ts
CHANGED
|
@@ -9,8 +9,9 @@ import type * as HttpClientRequest from "effect/http/HttpClientRequest"
|
|
|
9
9
|
import * as HttpClientResponse from "effect/http/HttpClientResponse"
|
|
10
10
|
import * as Codex from "./Codex.ts"
|
|
11
11
|
|
|
12
|
-
const
|
|
12
|
+
const requestBodies = Effect.fnUntraced(function* (
|
|
13
13
|
options: Codex.ModelOptions | undefined,
|
|
14
|
+
calls = 1,
|
|
14
15
|
) {
|
|
15
16
|
const requests: Array<HttpClientRequest.HttpClientRequest> = []
|
|
16
17
|
const client = HttpClient.make((request) =>
|
|
@@ -24,7 +25,11 @@ const requestBody = Effect.fnUntraced(function* (
|
|
|
24
25
|
)
|
|
25
26
|
yield* Effect.gen(function* () {
|
|
26
27
|
const ai = yield* LanguageModel.LanguageModel
|
|
27
|
-
|
|
28
|
+
for (let i = 0; i < calls; i++) {
|
|
29
|
+
yield* ai
|
|
30
|
+
.streamText({ prompt: "hello" })
|
|
31
|
+
.pipe(Stream.runDrain, Effect.exit)
|
|
32
|
+
}
|
|
28
33
|
}).pipe(
|
|
29
34
|
Effect.provide(
|
|
30
35
|
Codex.model("gpt-6-astra", options).pipe(
|
|
@@ -33,15 +38,20 @@ const requestBody = Effect.fnUntraced(function* (
|
|
|
33
38
|
),
|
|
34
39
|
),
|
|
35
40
|
)
|
|
36
|
-
assert.lengthOf(requests,
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
41
|
+
assert.lengthOf(requests, calls)
|
|
42
|
+
return requests.map((request) => {
|
|
43
|
+
const body = request.body
|
|
44
|
+
if (body._tag !== "Uint8Array") throw new Error("Expected JSON body")
|
|
45
|
+
return JSON.parse(new TextDecoder().decode(body.body)) as Record<
|
|
46
|
+
string,
|
|
47
|
+
unknown
|
|
48
|
+
>
|
|
49
|
+
})
|
|
43
50
|
})
|
|
44
51
|
|
|
52
|
+
const requestBody = (options: Codex.ModelOptions | undefined) =>
|
|
53
|
+
Effect.map(requestBodies(options), (bodies) => bodies[0]!)
|
|
54
|
+
|
|
45
55
|
describe("Codex.model", () => {
|
|
46
56
|
it.effect("sends medium reasoning with auto summaries by default", () =>
|
|
47
57
|
Effect.gen(function* () {
|
|
@@ -57,4 +67,22 @@ describe("Codex.model", () => {
|
|
|
57
67
|
assert.isFalse("reasoning" in (yield* requestBody({ reasoning: null })))
|
|
58
68
|
}),
|
|
59
69
|
)
|
|
70
|
+
|
|
71
|
+
it.effect("sends one prompt cache key for every request of a model", () =>
|
|
72
|
+
Effect.gen(function* () {
|
|
73
|
+
const [first, second] = yield* requestBodies(undefined, 2)
|
|
74
|
+
assert.isString(first!.prompt_cache_key)
|
|
75
|
+
assert.isNotEmpty(first!.prompt_cache_key)
|
|
76
|
+
assert.strictEqual(second!.prompt_cache_key, first!.prompt_cache_key)
|
|
77
|
+
const [other] = yield* requestBodies(undefined)
|
|
78
|
+
assert.notStrictEqual(other!.prompt_cache_key, first!.prompt_cache_key)
|
|
79
|
+
}),
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
it.effect("keeps a prompt cache key the caller sets", () =>
|
|
83
|
+
Effect.gen(function* () {
|
|
84
|
+
const body = yield* requestBody({ prompt_cache_key: "session-1" })
|
|
85
|
+
assert.strictEqual(body.prompt_cache_key, "session-1")
|
|
86
|
+
}),
|
|
87
|
+
)
|
|
60
88
|
})
|