clanka 0.22.6 → 0.23.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/src/AgentTools.ts CHANGED
@@ -17,6 +17,7 @@ import * as Fiber from "effect/Fiber"
17
17
  import * as Toolkit from "effect/ai/Toolkit"
18
18
  import * as Tool from "effect/ai/Tool"
19
19
  import * as Schema from "effect/Schema"
20
+ import * as Scope from "effect/Scope"
20
21
  import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"
21
22
  import * as FileSystem from "effect/FileSystem"
22
23
  import * as Path from "effect/Path"
@@ -59,7 +60,7 @@ export class TaskCompleter extends Context.Service<
59
60
 
60
61
  /**
61
62
  * What a task is: a background `bash` command or a sub-agent started with
62
- * `delegate`.
63
+ * `subagent`.
63
64
  *
64
65
  * @since 1.0.0
65
66
  * @category Models
@@ -130,7 +131,7 @@ export interface AgentTasks {
130
131
  * it resolves once the agent has finished, and dies if the agent failed.
131
132
  * In the background it resolves straight away with `status: "running"`.
132
133
  */
133
- readonly delegate: (options: {
134
+ readonly subagent: (options: {
134
135
  readonly id: string
135
136
  readonly prompt: string
136
137
  readonly background: boolean
@@ -184,7 +185,7 @@ export class AgentTaskExecutor extends Context.Service<
184
185
  * @category Context
185
186
  */
186
187
  export const agentTasksUnavailable: AgentTasks = {
187
- delegate: () => Effect.die(new Error("Sub-agents are not available here")),
188
+ subagent: () => Effect.die(new Error("Sub-agents are not available here")),
188
189
  taskOutput: () => Effect.fail("Sub-agents are not available here"),
189
190
  waitTasks: () => Effect.fail("Sub-agents are not available here"),
190
191
  messageAgent: () => Effect.fail("Sub-agents are not available here"),
@@ -387,7 +388,7 @@ export const AgentTools = Toolkit.make(
387
388
  success: Schema.String,
388
389
  dependencies: [CurrentDirectory],
389
390
  }),
390
- Tool.make("delegate", {
391
+ Tool.make("subagent", {
391
392
  description:
392
393
  "Delegate a task to another software engineer / sub-agent. With `background: true` the sub-agent works in the background, and its result is sent to you when it finishes.",
393
394
  parameters: Schema.Struct({
@@ -581,23 +582,46 @@ export const AgentToolHandlersNoDeps = AgentToolsWithSearch.toLayer(
581
582
  const commandScope = yield* Effect.scope
582
583
  const startCommand = Effect.fnUntraced(function* (
583
584
  command: ChildProcess.Command,
585
+ saveLargeOutput = false,
584
586
  ) {
585
587
  const { outputChars } = yield* CommandLimits
586
- const output = makeOutputBuffer(outputChars)
588
+ const maxChars = saveLargeOutput ? (outputChars ?? 30_000) : outputChars
589
+ const output = makeOutputBuffer(maxChars)
590
+ let total = 0
591
+ let outputFile: string | undefined
592
+ const append = Effect.fnUntraced(function* (chunk: string) {
593
+ if (saveLargeOutput) {
594
+ if (outputFile !== undefined) {
595
+ yield* fs.writeFileString(outputFile, chunk, { flag: "a" })
596
+ } else if (total + chunk.length > maxChars!) {
597
+ const file = yield* fs
598
+ .makeTempFileScoped({ prefix: "clanka-bash-" })
599
+ .pipe(Effect.provideService(Scope.Scope, commandScope))
600
+ yield* fs.writeFileString(file, output.render() + chunk)
601
+ outputFile = file
602
+ }
603
+ }
604
+ total += chunk.length
605
+ output.append(chunk)
606
+ })
587
607
  const fiber = yield* spawner.spawn(command).pipe(
588
608
  Effect.flatMap((handle) =>
589
609
  handle.all.pipe(
590
610
  Stream.decodeText,
591
- Stream.runForEach((chunk) =>
592
- Effect.sync(() => output.append(chunk)),
593
- ),
611
+ Stream.runForEach(append),
594
612
  Effect.andThen(handle.exitCode),
595
613
  ),
596
614
  ),
597
615
  Effect.scoped,
598
616
  Effect.forkIn(commandScope),
599
617
  )
600
- return { fiber, output: output.render }
618
+ return {
619
+ fiber,
620
+ output: () =>
621
+ outputFile === undefined
622
+ ? output.render()
623
+ : `Output was too large. Saved to: ${outputFile}`,
624
+ }
601
625
  })
602
626
 
603
627
  // Every task started on this executor, bash and agent alike, so both
@@ -605,7 +629,7 @@ export const AgentToolHandlersNoDeps = AgentToolsWithSearch.toLayer(
605
629
  // started it.
606
630
  const tasks = new Map<string, TaskEntry>()
607
631
  let nextTaskId = 1
608
- const registerAgentTask = Effect.fnUntraced(function* () {
632
+ const registerAgentTask = Effect.gen(function* () {
609
633
  const id = String(nextTaskId++)
610
634
  tasks.set(id, { kind: "agent", id, owner: yield* TaskOwner })
611
635
  return id
@@ -883,7 +907,7 @@ export const AgentToolHandlersNoDeps = AgentToolsWithSearch.toLayer(
883
907
  })
884
908
  const entry = yield* addBashTask(
885
909
  options.command,
886
- yield* startCommand(cmd),
910
+ yield* startCommand(cmd, true),
887
911
  )
888
912
  if (options.background !== true) {
889
913
  const exit = yield* awaitCommand(
@@ -1174,14 +1198,14 @@ export const AgentToolHandlersNoDeps = AgentToolsWithSearch.toLayer(
1174
1198
 
1175
1199
  return `Success. Updated the following files:\n${out.join("\n")}`
1176
1200
  }, Effect.orDie),
1177
- delegate: Effect.fn("AgentTools.delegate")(function* (options: unknown) {
1178
- const { prompt, background = false } = yield* decodeDelegate(options)
1179
- yield* Effect.logInfo(`Calling "delegate"`).pipe(
1201
+ subagent: Effect.fn("AgentTools.subagent")(function* (options: unknown) {
1202
+ const { prompt, background = false } = yield* decodeSubagent(options)
1203
+ yield* Effect.logInfo(`Calling "subagent"`).pipe(
1180
1204
  Effect.annotateLogs({ background }),
1181
1205
  )
1182
- const id = yield* registerAgentTask()
1206
+ const id = yield* registerAgentTask
1183
1207
  const agentTasks = yield* AgentTaskExecutor
1184
- return yield* agentTasks.delegate({ id, prompt, background })
1208
+ return yield* agentTasks.subagent({ id, prompt, background })
1185
1209
  }),
1186
1210
  messageAgent: Effect.fn("AgentTools.messageAgent")(function* (
1187
1211
  input: unknown,
@@ -1250,9 +1274,9 @@ const decodeParams = <S extends Schema.Decoder<unknown>>(
1250
1274
  return (input: unknown) => Effect.mapError(decode(input), () => usage)
1251
1275
  }
1252
1276
 
1253
- const decodeDelegate = decodeParams(
1254
- AgentTools.tools.delegate.parametersSchema,
1255
- 'delegate requires an options object, for example: delegate({ prompt: "..." }) or delegate({ prompt: "...", background: true }).',
1277
+ const decodeSubagent = decodeParams(
1278
+ AgentTools.tools.subagent.parametersSchema,
1279
+ 'subagent requires an options object, for example: subagent({ prompt: "..." }) or subagent({ prompt: "...", background: true }).',
1256
1280
  )
1257
1281
  const decodeMessageAgent = decodeParams(
1258
1282
  AgentTools.tools.messageAgent.parametersSchema,
@@ -853,7 +853,7 @@ await taskComplete(JSON.stringify({ crashed, after: await attempt(mcp.fake.echo(
853
853
  return "await taskComplete(await mcp.fake.ping({}))"
854
854
  }
855
855
  return call.index === 0
856
- ? `const child = await delegate({ prompt: "TASK-CHILD" })
856
+ ? `const child = await subagent({ prompt: "TASK-CHILD" })
857
857
  console.log(JSON.stringify(child))
858
858
  console.log(await mcp.fake.screenshot({}))`
859
859
  : 'await taskComplete("done")'