clanka 0.18.1 → 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.1",
4
+ "version": "0.18.2",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
@@ -207,6 +207,29 @@ describe("RPC executeUnsafe", () => {
207
207
  ),
208
208
  )
209
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
+
210
233
  it.live("passes a caller's tool params shaped like bytes unchanged", () => {
211
234
  const payload = { _tag: "Bytes", base64: "AA==" } as const
212
235
  const Echo = Toolkit.make(
@@ -236,6 +259,38 @@ describe("RPC executeUnsafe", () => {
236
259
  ),
237
260
  )
238
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
+ })
239
294
  })
240
295
 
241
296
  // Both executor modes with the same limits. The RPC mode serves
@@ -57,6 +57,7 @@ import * as Schema from "effect/Schema"
57
57
  import * as SchemaTransformation from "effect/SchemaTransformation"
58
58
  import * as RpcGroup from "effect/rpc/RpcGroup"
59
59
  import * as Rpc from "effect/rpc/Rpc"
60
+ import * as Predicate from "effect/Predicate"
60
61
  import * as Result from "effect/Result"
61
62
  import { SemanticSearch } from "./SemanticSearch/Service.ts"
62
63
  import { preprocessScript } from "./ScriptPreprocessing.ts"
@@ -775,14 +776,14 @@ export const makeRpc = Effect.gen(function* () {
775
776
  client
776
777
  .renderEnvironment({ environment, reset: options?.reset ?? false })
777
778
  .pipe(Effect.orDie),
778
- executeUnsafe({ environment, ...options }) {
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
- ),
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
+ }),
786
787
  ),
787
788
  Effect.orDie,
788
789
  )
@@ -1103,7 +1104,7 @@ export class Rpcs extends RpcGroup.make(
1103
1104
  Rpc.make("executeUnsafe", {
1104
1105
  payload: Schema.Struct({
1105
1106
  tool: Schema.String,
1106
- params: Schema.Json,
1107
+ params: Schema.optionalKey(Schema.Json),
1107
1108
  environment: Schema.optionalKey(ExecutionEnvironment),
1108
1109
  }),
1109
1110
  success: Schema.Json,
@@ -1114,10 +1115,13 @@ export class Rpcs extends RpcGroup.make(
1114
1115
  // Internal
1115
1116
  // -------------------------------------------
1116
1117
 
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.
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.
1121
1125
  const RpcWriteFileParams = Schema.Struct({
1122
1126
  ...AgentTools.tools.writeFile.parametersSchema.fields,
1123
1127
  content: Schema.Union([
@@ -1134,17 +1138,40 @@ const RpcWriteFileParams = Schema.Struct({
1134
1138
  ]),
1135
1139
  })
1136
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
+
1137
1162
  const encodeRpcParams = (
1138
1163
  tool: string,
1139
1164
  params: unknown,
1140
- ): Effect.Effect<Schema.Json, Schema.SchemaError> =>
1141
- tool === "writeFile"
1142
- ? Schema.encodeUnknownEffect(RpcWriteFileParams)(params)
1143
- : Effect.succeed(params as Schema.Json)
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
+ }
1144
1171
 
1145
1172
  const decodeRpcParams = (
1146
1173
  tool: string,
1147
- params: Schema.Json,
1174
+ params: Schema.Json | undefined,
1148
1175
  ): Effect.Effect<unknown, Schema.SchemaError> =>
1149
1176
  tool === "writeFile"
1150
1177
  ? Schema.decodeUnknownEffect(RpcWriteFileParams)(params)