clanka 0.18.0 → 0.18.2

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.2",
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,110 @@ 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("accepts undefined params and undefined param properties", () =>
211
+ withRpc((executor) =>
212
+ Effect.gen(function* () {
213
+ const fs = yield* FileSystem.FileSystem
214
+ const { environment } = yield* executor.capabilities
215
+ yield* fs.writeFileString(
216
+ NodePath.join(environment.cwd, "x.txt"),
217
+ "hello",
218
+ )
219
+ const agents = yield* executor.executeUnsafe({
220
+ tool: "listAgents",
221
+ params: undefined,
222
+ })
223
+ assert.deepStrictEqual(agents, [])
224
+ const content = yield* executor.executeUnsafe({
225
+ tool: "readFile",
226
+ params: { path: "x.txt", startLine: undefined, lineNumbers: false },
227
+ })
228
+ assert.strictEqual(content, "hello")
229
+ }),
230
+ ),
231
+ )
232
+
233
+ it.live("passes a caller's tool params shaped like bytes unchanged", () => {
234
+ const payload = { _tag: "Bytes", base64: "AA==" } as const
235
+ const Echo = Toolkit.make(
236
+ Tool.make("echo", {
237
+ parameters: Schema.Struct({
238
+ payload: Schema.TaggedStruct("Bytes", { base64: Schema.String }),
239
+ }),
240
+ success: Schema.TaggedStruct("Bytes", { base64: Schema.String }),
241
+ }),
242
+ )
243
+ return withRpc(
244
+ (executor) =>
245
+ Effect.gen(function* () {
246
+ // `executeUnsafe` is typed for the built-in tools, but the server
247
+ // runs the caller's tools too.
248
+ const output = yield* executor.executeUnsafe({
249
+ tool: "echo" as never,
250
+ params: { payload } as never,
251
+ })
252
+ assert.deepStrictEqual(output, payload)
253
+ }),
254
+ (directory) =>
255
+ AgentExecutor.layerRpcServer({ directory, tools: Echo }).pipe(
256
+ Layer.provide(
257
+ Echo.toLayer({ echo: ({ payload }) => Effect.succeed(payload) }),
258
+ ),
259
+ ),
260
+ )
261
+ })
262
+
263
+ it.live("passes a caller's null params and __proto__ keys unchanged", () => {
264
+ const Echo = Toolkit.make(
265
+ Tool.make("echo", { parameters: Schema.Json, success: Schema.Json }),
266
+ )
267
+ return withRpc(
268
+ (executor) =>
269
+ Effect.gen(function* () {
270
+ const nullOutput = yield* executor.executeUnsafe({
271
+ tool: "echo" as never,
272
+ params: null as never,
273
+ })
274
+ assert.strictEqual(nullOutput, null)
275
+ const output = (yield* executor.executeUnsafe({
276
+ tool: "echo" as never,
277
+ params: JSON.parse('{"__proto__":{"x":1}}') as never,
278
+ })) as object
279
+ // An own `__proto__` key, not a prototype.
280
+ assert.deepStrictEqual(Object.keys(output), ["__proto__"])
281
+ assert.deepStrictEqual(
282
+ Object.getOwnPropertyDescriptor(output, "__proto__")?.value,
283
+ { x: 1 },
284
+ )
285
+ }),
286
+ (directory) =>
287
+ AgentExecutor.layerRpcServer({ directory, tools: Echo }).pipe(
288
+ Layer.provide(
289
+ Echo.toLayer({ echo: (params) => Effect.succeed(params) }),
290
+ ),
291
+ ),
292
+ )
293
+ })
294
+ })
295
+
185
296
  // Both executor modes with the same limits. The RPC mode serves
186
297
  // `layerRpcServer` through an in-memory web handler, so requests go through
187
298
  // the real serialization without opening a socket.
@@ -54,8 +54,10 @@ 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"
60
+ import * as Predicate from "effect/Predicate"
59
61
  import * as Result from "effect/Result"
60
62
  import { SemanticSearch } from "./SemanticSearch/Service.ts"
61
63
  import { preprocessScript } from "./ScriptPreprocessing.ts"
@@ -774,12 +776,17 @@ export const makeRpc = Effect.gen(function* () {
774
776
  client
775
777
  .renderEnvironment({ environment, reset: options?.reset ?? false })
776
778
  .pipe(Effect.orDie),
777
- executeUnsafe({ environment, ...options }) {
778
- return client
779
- .executeUnsafe(
780
- environment === undefined ? options : { ...options, environment },
781
- )
782
- .pipe(Effect.orDie)
779
+ executeUnsafe({ tool, params, environment }) {
780
+ return encodeRpcParams(tool, params).pipe(
781
+ Effect.flatMap((json) =>
782
+ client.executeUnsafe({
783
+ tool,
784
+ ...(json === undefined ? undefined : { params: json }),
785
+ ...(environment === undefined ? undefined : { environment }),
786
+ }),
787
+ ),
788
+ Effect.orDie,
789
+ )
783
790
  },
784
791
  })
785
792
  })
@@ -994,9 +1001,13 @@ export const layerRpcServer = <Toolkit extends Toolkit.Any = never>(
994
1001
 
995
1002
  return queue
996
1003
  }),
997
- executeUnsafe(opts) {
998
- return local.executor.executeUnsafe(opts as any)
999
- },
1004
+ executeUnsafe: (opts) =>
1005
+ decodeRpcParams(opts.tool, opts.params).pipe(
1006
+ Effect.orDie,
1007
+ Effect.flatMap((params) =>
1008
+ local.executor.executeUnsafe({ ...opts, params } as any),
1009
+ ),
1010
+ ),
1000
1011
  })
1001
1012
  }),
1002
1013
  ),
@@ -1093,7 +1104,7 @@ export class Rpcs extends RpcGroup.make(
1093
1104
  Rpc.make("executeUnsafe", {
1094
1105
  payload: Schema.Struct({
1095
1106
  tool: Schema.String,
1096
- params: Schema.Json,
1107
+ params: Schema.optionalKey(Schema.Json),
1097
1108
  environment: Schema.optionalKey(ExecutionEnvironment),
1098
1109
  }),
1099
1110
  success: Schema.Json,
@@ -1104,6 +1115,68 @@ export class Rpcs extends RpcGroup.make(
1104
1115
  // Internal
1105
1116
  // -------------------------------------------
1106
1117
 
1118
+ // `executeUnsafe` params cross RPC as JSON. JSON has no `undefined`, so
1119
+ // `undefined` params are omitted from the payload, `undefined` properties are
1120
+ // dropped, and `undefined` array items become `null`, as `JSON.stringify` does.
1121
+ // `writeFile` is the only built-in tool whose params JSON can't carry: its
1122
+ // content may be bytes. The default JSON form of bytes is a base64 string,
1123
+ // which the server couldn't tell apart from string content, so over RPC bytes
1124
+ // travel as `{ base64 }` instead.
1125
+ const RpcWriteFileParams = Schema.Struct({
1126
+ ...AgentTools.tools.writeFile.parametersSchema.fields,
1127
+ content: Schema.Union([
1128
+ Schema.String,
1129
+ Schema.Struct({ base64: Schema.Uint8ArrayFromBase64 }).pipe(
1130
+ Schema.decodeTo(
1131
+ Schema.Uint8Array,
1132
+ SchemaTransformation.transform({
1133
+ decode: ({ base64 }) => base64,
1134
+ encode: (base64) => ({ base64 }),
1135
+ }),
1136
+ ),
1137
+ ),
1138
+ ]),
1139
+ })
1140
+
1141
+ const isPlainObject = (value: unknown): value is Record<string, unknown> => {
1142
+ if (!Predicate.isObject(value)) return false
1143
+ const proto = Object.getPrototypeOf(value)
1144
+ return proto === Object.prototype || proto === null
1145
+ }
1146
+
1147
+ // Only arrays and plain objects are walked, so bytes and other values reach
1148
+ // the schema untouched.
1149
+ const dropUndefined = (value: unknown): unknown => {
1150
+ if (Array.isArray(value)) {
1151
+ return value.map((item) => dropUndefined(item) ?? null)
1152
+ }
1153
+ if (!isPlainObject(value)) return value
1154
+ // `Object.fromEntries` keeps an own `__proto__` key as a data property.
1155
+ return Object.fromEntries(
1156
+ Object.entries(value)
1157
+ .filter(([, field]) => field !== undefined)
1158
+ .map(([key, field]) => [key, dropUndefined(field)]),
1159
+ )
1160
+ }
1161
+
1162
+ const encodeRpcParams = (
1163
+ tool: string,
1164
+ params: unknown,
1165
+ ): Effect.Effect<Schema.Json | undefined, Schema.SchemaError> => {
1166
+ const json = dropUndefined(params)
1167
+ return tool === "writeFile"
1168
+ ? Schema.encodeUnknownEffect(RpcWriteFileParams)(json)
1169
+ : Effect.succeed(json as Schema.Json | undefined)
1170
+ }
1171
+
1172
+ const decodeRpcParams = (
1173
+ tool: string,
1174
+ params: Schema.Json | undefined,
1175
+ ): Effect.Effect<unknown, Schema.SchemaError> =>
1176
+ tool === "writeFile"
1177
+ ? Schema.decodeUnknownEffect(RpcWriteFileParams)(params)
1178
+ : Effect.succeed(params)
1179
+
1107
1180
  const handleRequest = (
1108
1181
  opts: ExecuteOptions,
1109
1182
  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) {