clanka 0.18.0 → 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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "clanka",
3
3
  "type": "module",
4
- "version": "0.18.0",
4
+ "version": "0.18.1",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
@@ -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
- AgentExecutor.layerRpcServer({ directory }).pipe(
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.
@@ -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 client
779
- .executeUnsafe(
780
- environment === undefined ? options : { ...options, environment },
781
- )
782
- .pipe(Effect.orDie)
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
- return local.executor.executeUnsafe(opts as any)
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,
@@ -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.String,
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* fs.writeFileString(path, options.content)
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) {