clanka 0.19.1 → 0.20.0

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.
Files changed (40) hide show
  1. package/dist/Acp.d.ts.map +1 -1
  2. package/dist/Acp.js +14 -0
  3. package/dist/Acp.js.map +1 -1
  4. package/dist/Acp.test.js +77 -0
  5. package/dist/Acp.test.js.map +1 -1
  6. package/dist/Agent.d.ts.map +1 -1
  7. package/dist/Agent.js +64 -3
  8. package/dist/Agent.js.map +1 -1
  9. package/dist/AgentBashJobs.test.d.ts +2 -0
  10. package/dist/AgentBashJobs.test.d.ts.map +1 -0
  11. package/dist/AgentBashJobs.test.js +173 -0
  12. package/dist/AgentBashJobs.test.js.map +1 -0
  13. package/dist/AgentExecutor.d.ts +35 -2
  14. package/dist/AgentExecutor.d.ts.map +1 -1
  15. package/dist/AgentExecutor.js +73 -3
  16. package/dist/AgentExecutor.js.map +1 -1
  17. package/dist/AgentOutput.d.ts +19 -4
  18. package/dist/AgentOutput.d.ts.map +1 -1
  19. package/dist/AgentOutput.js +15 -0
  20. package/dist/AgentOutput.js.map +1 -1
  21. package/dist/AgentTools.d.ts +28 -0
  22. package/dist/AgentTools.d.ts.map +1 -1
  23. package/dist/AgentTools.js +35 -6
  24. package/dist/AgentTools.js.map +1 -1
  25. package/dist/OutputFormatter.d.ts.map +1 -1
  26. package/dist/OutputFormatter.js +6 -0
  27. package/dist/OutputFormatter.js.map +1 -1
  28. package/dist/bin/{OutputFormatter-Dqxyk2qD.mjs → OutputFormatter-U02YKTfy.mjs} +5 -1
  29. package/dist/bin/{OutputFormatter-Dqxyk2qD.mjs.map → OutputFormatter-U02YKTfy.mjs.map} +1 -1
  30. package/dist/bin/cli.mjs +130 -17
  31. package/dist/bin/cli.mjs.map +1 -1
  32. package/package.json +1 -1
  33. package/src/Acp.test.ts +110 -0
  34. package/src/Acp.ts +15 -0
  35. package/src/Agent.ts +97 -1
  36. package/src/AgentBashJobs.test.ts +295 -0
  37. package/src/AgentExecutor.ts +86 -0
  38. package/src/AgentOutput.ts +19 -0
  39. package/src/AgentTools.ts +62 -6
  40. package/src/OutputFormatter.ts +7 -0
package/dist/bin/cli.mjs CHANGED
@@ -11579,6 +11579,7 @@ const BashJob = Struct({
11579
11579
  exitCode: optional$2(Number$2),
11580
11580
  output: String$7
11581
11581
  });
11582
+ var JobWatcher = class extends Reference("clanka/AgentTools/JobWatcher", { defaultValue: () => () => void_ }) {};
11582
11583
  const defaultBashTimeout = minutes(10);
11583
11584
  const defaultMcpCallTimeout = minutes(10);
11584
11585
  var CommandLimits = class extends Reference("clanka/AgentTools/CommandLimits", { defaultValue: () => ({
@@ -11745,11 +11746,24 @@ const AgentToolHandlersNoDeps = AgentToolsWithSearch.toLayer(gen(function* () {
11745
11746
  });
11746
11747
  const jobs = new Map();
11747
11748
  let nextJobId = 1;
11748
- const addJob = (running) => {
11749
+ const addJob = fnUntraced(function* (command) {
11749
11750
  const id = String(nextJobId++);
11751
+ const running = {
11752
+ ...command,
11753
+ reported: false
11754
+ };
11750
11755
  jobs.set(id, running);
11756
+ yield* (yield* JobWatcher)({
11757
+ id,
11758
+ await: await_(running.fiber).pipe(map$5(() => jobInfo(id, running))),
11759
+ claim: sync(() => {
11760
+ if (running.reported) return false;
11761
+ running.reported = true;
11762
+ return true;
11763
+ })
11764
+ });
11751
11765
  return id;
11752
- };
11766
+ });
11753
11767
  const findJob = (id) => {
11754
11768
  const running = jobs.get(id);
11755
11769
  return running === void 0 ? fail$1(`No background job with id "${id}"`) : succeed$1(running);
@@ -11764,7 +11778,7 @@ const AgentToolHandlersNoDeps = AgentToolsWithSearch.toLayer(gen(function* () {
11764
11778
  yield* interrupt$2(running.fiber);
11765
11779
  return yield* die(new Error(`Command timed out after ${timeoutMs}ms`));
11766
11780
  }
11767
- const id = addJob(running);
11781
+ const id = yield* addJob(running);
11768
11782
  const output = running.output();
11769
11783
  return `${output}${output === "" || output.endsWith("\n") ? "" : "\n"}[Still running after ${timeoutMs}ms, so it was moved to the background as job ${id}. ${jobUsage(id)}]`;
11770
11784
  }
@@ -11890,7 +11904,7 @@ const AgentToolHandlersNoDeps = AgentToolsWithSearch.toLayer(gen(function* () {
11890
11904
  stdin: "ignore"
11891
11905
  });
11892
11906
  if (options.background === true) {
11893
- const id = addJob(yield* startCommand(cmd));
11907
+ const id = yield* addJob(yield* startCommand(cmd));
11894
11908
  return `Started background job ${id}. ${jobUsage(id)}`;
11895
11909
  }
11896
11910
  return yield* runCommand(cmd, millis(timeoutMs), "background");
@@ -11898,13 +11912,17 @@ const AgentToolHandlersNoDeps = AgentToolsWithSearch.toLayer(gen(function* () {
11898
11912
  jobOutput: fn("AgentTools.jobOutput")(function* (input) {
11899
11913
  const id = yield* decodeJobOutput(input);
11900
11914
  yield* logInfo(`Calling "jobOutput"`).pipe(annotateLogs({ id }));
11901
- return jobInfo(id, yield* findJob(id));
11915
+ const running = yield* findJob(id);
11916
+ const job = jobInfo(id, running);
11917
+ if (job.status !== "running") running.reported = true;
11918
+ return job;
11902
11919
  }),
11903
11920
  stopJob: fn("AgentTools.stopJob")(function* (input) {
11904
11921
  const id = yield* decodeStopJob(input);
11905
11922
  yield* logInfo(`Calling "stopJob"`).pipe(annotateLogs({ id }));
11906
11923
  const running = yield* findJob(id);
11907
11924
  yield* interrupt$2(running.fiber);
11925
+ running.reported = true;
11908
11926
  return jobInfo(id, running);
11909
11927
  }),
11910
11928
  gh: fn("AgentTools.gh")(function* (args) {
@@ -14104,7 +14122,8 @@ var Capabilities = class extends Class$1("Capabilities")({
14104
14122
  supportsSearch: Boolean$4,
14105
14123
  environment: ExecutionEnvironment,
14106
14124
  tools: ArraySchema(String$7),
14107
- skills: ArraySchema(Skill).pipe(withDecodingDefaultKey(succeed$1([])), withConstructorDefault(succeed$1([])))
14125
+ skills: ArraySchema(Skill).pipe(withDecodingDefaultKey(succeed$1([])), withConstructorDefault(succeed$1([]))),
14126
+ jobPushes: Boolean$4.pipe(withDecodingDefaultKey(succeed$1(false)), withConstructorDefault(succeed$1(false)))
14108
14127
  }) {};
14109
14128
  const makeLocal = (options) => map$5(makeLocalWith(options), (local) => local.executor);
14110
14129
  const makeLocalWith = fnUntraced(function* (options) {
@@ -14199,7 +14218,7 @@ const makeLocalWith = fnUntraced(function* (options) {
14199
14218
  ...options,
14200
14219
  environment
14201
14220
  })))
14202
- }), add(ImageAttacher, opts.onImage), add(CommandLimits, commandLimits), add(Console, console), add(CurrentLogAnnotations, {}), isSome(search) ? add(SemanticSearch, search.value) : identity);
14221
+ }), add(ImageAttacher, opts.onImage), add(JobWatcher, opts.onJob ?? (() => void_)), add(CommandLimits, commandLimits), add(Console, console), add(CurrentLogAnnotations, {}), isSome(search) ? add(SemanticSearch, search.value) : identity);
14203
14222
  yield* gen(function* () {
14204
14223
  const source = prepareScript(opts.script);
14205
14224
  const settled = yield* make$42();
@@ -14284,7 +14303,8 @@ const makeLocalWith = fnUntraced(function* (options) {
14284
14303
  seen: []
14285
14304
  }),
14286
14305
  tools: Object.keys(tools.tools),
14287
- skills
14306
+ skills,
14307
+ jobPushes: true
14288
14308
  });
14289
14309
  }),
14290
14310
  execute: (opts) => execute(opts),
@@ -14313,7 +14333,8 @@ const makeRpc = gen(function* () {
14313
14333
  return client.execute({
14314
14334
  script: opts.script,
14315
14335
  environment: opts.environment,
14316
- loopTools: Object.keys(opts.loopTools ?? {})
14336
+ loopTools: Object.keys(opts.loopTools ?? {}),
14337
+ ...opts.onJob === void 0 ? {} : { watchJobs: true }
14317
14338
  }).pipe(tap$1((part) => {
14318
14339
  switch (part._tag) {
14319
14340
  case "Text": return void_;
@@ -14333,6 +14354,15 @@ const makeRpc = gen(function* () {
14333
14354
  case "Environment":
14334
14355
  latest = part.environment;
14335
14356
  return void_;
14357
+ case "Job": {
14358
+ if (opts.onJob === void 0) return void_;
14359
+ const id = part.id;
14360
+ return opts.onJob({
14361
+ id,
14362
+ await: orDie(client.awaitJob({ id })),
14363
+ claim: orDie(client.claimJob({ id }))
14364
+ });
14365
+ }
14336
14366
  }
14337
14367
  }), orDie$2, filterMap((part) => part._tag === "Text" ? succeed$2(part.text) : failVoid), ensuring$1(suspend(() => opts.onEnvironment(latest))));
14338
14368
  }).pipe(unwrap$6),
@@ -14386,7 +14416,8 @@ const ExecuteOutput = TaggedUnion({
14386
14416
  mediaType: ImageMediaType,
14387
14417
  data: Uint8Array$2
14388
14418
  },
14389
- Environment: { environment: ExecutionEnvironment }
14419
+ Environment: { environment: ExecutionEnvironment },
14420
+ Job: { id: String$7 }
14390
14421
  });
14391
14422
  var Rpcs = class extends make$12(make$14("capabilities", { success: Capabilities }), make$14("respond", { payload: {
14392
14423
  id: Finite,
@@ -14395,10 +14426,19 @@ var Rpcs = class extends make$12(make$14("capabilities", { success: Capabilities
14395
14426
  payload: Struct({
14396
14427
  script: String$7,
14397
14428
  environment: ExecutionEnvironment,
14398
- loopTools: ArraySchema(String$7).pipe(withDecodingDefaultKey(succeed$1([])))
14429
+ loopTools: ArraySchema(String$7).pipe(withDecodingDefaultKey(succeed$1([]))),
14430
+ watchJobs: optionalKey(Boolean$4)
14399
14431
  }),
14400
14432
  success: ExecuteOutput,
14401
14433
  stream: true
14434
+ }), make$14("awaitJob", {
14435
+ payload: { id: String$7 },
14436
+ success: BashJob,
14437
+ error: String$7
14438
+ }), make$14("claimJob", {
14439
+ payload: { id: String$7 },
14440
+ success: Boolean$4,
14441
+ error: String$7
14402
14442
  }), make$14("renderEnvironment", {
14403
14443
  payload: {
14404
14444
  environment: ExecutionEnvironment,
@@ -20457,6 +20497,16 @@ var SessionRestarted = class extends TaggedClass$2()("SessionRestarted", {
20457
20497
  cacheWrite: Number$2
20458
20498
  }) {};
20459
20499
  var EnvironmentUpdated = class extends TaggedClass$2()("EnvironmentUpdated", { update: String$7 }) {};
20500
+ var JobComplete = class extends TaggedClass$2()("JobComplete", {
20501
+ id: String$7,
20502
+ status: Literals$2([
20503
+ "completed",
20504
+ "failed",
20505
+ "stopped"
20506
+ ]),
20507
+ exitCode: optional$2(Number$2),
20508
+ output: String$7
20509
+ }) {};
20460
20510
  const ContentPart = Union([
20461
20511
  ReasoningStart,
20462
20512
  ReasoningDelta,
@@ -20471,7 +20521,8 @@ const ContentPart = Union([
20471
20521
  CompactionStarted,
20472
20522
  CompactionEnded,
20473
20523
  SessionRestarted,
20474
- EnvironmentUpdated
20524
+ EnvironmentUpdated,
20525
+ JobComplete
20475
20526
  ]);
20476
20527
  var SubagentPart = class extends TaggedClass$2()("SubagentPart", {
20477
20528
  id: Number$2,
@@ -20498,6 +20549,7 @@ const TypeId$2 = "~clanka/Agent";
20498
20549
  const Agent = Service("clanka/Agent");
20499
20550
  const makeWith$1 = fnUntraced(function* (options) {
20500
20551
  const executor = yield* AgentExecutor;
20552
+ const agentScope = yield* scope;
20501
20553
  const singleTool = yield* SingleTools.pipe(provide$1(SingleToolHandlers));
20502
20554
  const capabilities = yield* executor.capabilities;
20503
20555
  const mcpServers = getOrUndefined(yield* serviceOption(McpServers));
@@ -20513,6 +20565,7 @@ const makeWith$1 = fnUntraced(function* (options) {
20513
20565
  mcpServers?.dts ?? ""
20514
20566
  ].filter((dts) => dts !== "").join("\n\n"),
20515
20567
  offers: (name) => capabilities.tools.includes(name),
20568
+ jobPushes: capabilities.jobPushes,
20516
20569
  skillsCatalog: options.skills === void 0 ? renderCatalog(capabilities.skills) : renderProvidedCatalog(options.skills),
20517
20570
  mcp: mcpServers?.prompt ?? ""
20518
20571
  };
@@ -20550,6 +20603,21 @@ const makeWith$1 = fnUntraced(function* (options) {
20550
20603
  let cacheWrite = 0;
20551
20604
  const prompt = opts.history;
20552
20605
  const mailbox = opts.mailbox;
20606
+ const onJob = (job) => job.await.pipe(flatMap$1((final) => sync(() => {
20607
+ if (final.status === "running") return;
20608
+ mailbox.jobs.push({
20609
+ job: final,
20610
+ claim: job.claim
20611
+ });
20612
+ const event = new JobComplete({
20613
+ id: final.id,
20614
+ status: final.status,
20615
+ ...final.exitCode === void 0 ? {} : { exitCode: final.exitCode },
20616
+ output: final.output
20617
+ });
20618
+ if (mailbox.emit === void 0) mailbox.events.push(event);
20619
+ else mailbox.emit(event);
20620
+ })), forkIn(agentScope), asVoid);
20553
20621
  const appendHistory = (input) => {
20554
20622
  update(prompt, concat(input));
20555
20623
  if (!opts.historyEvents) return;
@@ -20880,6 +20948,7 @@ ${prompt}`,
20880
20948
  subagents,
20881
20949
  onTaskComplete,
20882
20950
  onImage,
20951
+ onJob,
20883
20952
  loopTools: scriptLoopTools
20884
20953
  }), mkString);
20885
20954
  const capped = capOutput(output);
@@ -20912,9 +20981,19 @@ ${prompt}`,
20912
20981
  const messages = opts.prompt.content.filter((message) => message.role !== "system");
20913
20982
  if (messages.length > 0) yield* offer(output, new HistoryAppended({ messages }));
20914
20983
  }
20984
+ for (const part of mailbox.events.splice(0)) maybeSend({
20985
+ agentId,
20986
+ part
20987
+ });
20988
+ const emit = (part) => maybeSend({
20989
+ agentId,
20990
+ part
20991
+ });
20992
+ mailbox.emit = emit;
20915
20993
  yield* gen(function* () {
20916
20994
  while (true) {
20917
20995
  if (isSome(finalSummary)) {
20996
+ mailbox.emit = void 0;
20918
20997
  yield* fail$7(output, new AgentFinished({ summary: finalSummary.value }));
20919
20998
  return;
20920
20999
  }
@@ -20941,6 +21020,14 @@ ${capOutput(turn.output ?? "").output}
20941
21020
  </subagent>`
20942
21021
  }))));
20943
21022
  }
21023
+ if (mailbox.jobs.length > 0) {
21024
+ const pushes = [];
21025
+ for (const { job, claim } of mailbox.jobs.splice(0)) if (yield* claim) pushes.push(job);
21026
+ if (pushes.length > 0) appendHistory(make$21(pushes.map((job) => ({
21027
+ role: "user",
21028
+ content: jobMessage(job)
21029
+ }))));
21030
+ }
20944
21031
  yield* compactWithEvents("threshold", compactIfNeeded({
20945
21032
  prompt: prompt.current,
20946
21033
  contextTokens: lastContextTokens,
@@ -21132,7 +21219,9 @@ ${capOutput(turn.output ?? "").output}
21132
21219
  part: new ScriptDelta({ delta: script })
21133
21220
  });
21134
21221
  return executeScript(script);
21135
- }), ensuring(close(runScope, void_$1)), catchCause((cause) => failCause$2(output, cause)), forkScoped);
21222
+ }), ensuring(close(runScope, void_$1)), ensuring(sync(() => {
21223
+ if (mailbox.emit === emit) mailbox.emit = void 0;
21224
+ })), catchCause((cause) => failCause$2(output, cause)), forkScoped);
21136
21225
  return fromQueue(output);
21137
21226
  }, unwrap$6);
21138
21227
  const sendLock = makeUnsafe$4(1);
@@ -21167,8 +21256,17 @@ ${capOutput(turn.output ?? "").output}
21167
21256
  const make$8 = makeWith$1({});
21168
21257
  const makeMailbox = () => ({
21169
21258
  messages: new Set(),
21170
- notify: () => {}
21259
+ notify: () => {},
21260
+ jobs: [],
21261
+ emit: void 0,
21262
+ events: []
21171
21263
  });
21264
+ const jobMessage = (job) => {
21265
+ const exitCode = job.exitCode === void 0 ? "" : ` exitCode="${job.exitCode}"`;
21266
+ return `<job id="${job.id}" status="${job.status}"${exitCode}>
21267
+ ${capOutput(job.output).output}
21268
+ </job>`;
21269
+ };
21172
21270
  const makeTurn = (agentId, background) => ({
21173
21271
  agentId,
21174
21272
  status: "running",
@@ -21219,7 +21317,7 @@ const generateSystemTools = (tools, conversationMode) => `YOU ONLY HAVE ONE TOOL
21219
21317
  - Prefer using the "search" function over "rg", unless you are targeting specific files or patterns.`)}${when(tools.offers("applyPatch"), `
21220
21318
  - You can add / update / remove multiple files in one go with "applyPatch".${when(tools.offers("writeFile"), ` Prefer it over "writeFile" to update existing files.`)}`)}${when(tools.offers("bash"), `
21221
21319
  - Avoid passing scripts into the "bash" function, and instead write javascript.
21222
- - Pass \`background: true\` to "bash" for commands that keep running, such as dev servers or watchers. A command still running at its timeout is moved to the background instead of being stopped. Use "jobOutput" and "stopJob" with the returned job id.`)}
21320
+ - Pass \`background: true\` to "bash" for commands that keep running, such as dev servers or watchers. A command still running at its timeout is moved to the background instead of being stopped. Use "jobOutput" and "stopJob" with the returned job id.${when(tools.jobPushes, ` When a job ends, its status and output are sent to you as a \`<job id="..." status="..." exitCode="...">\` message, unless "jobOutput" or "stopJob" already gave you its final status. You do not need to poll jobs with "jobOutput" to find out when they end.`)}`)}
21223
21321
  - Variables are not shared between executions.
21224
21322
  - Do not use \`require\`, \`import\`, \`process\`, or any other Node.js apis.${when(tools.offers("delegate"), `
21225
21323
  - "delegate" runs a sub-agent and waits for it. Pass \`background: true\` to keep working while it runs: its result is sent to you later as a \`<subagent id="..." status="...">\` message.${when(tools.offers("waitAgents"), ` Use "waitAgents" to wait for the returned id.`)}${when(tools.offers("messageAgent"), ` Use "messageAgent" to send a message to the returned id.`)}${when(tools.offers("stopAgent"), ` Use "stopAgent" to stop the returned id.`)}${when(tools.offers("listAgents"), ` "listAgents" shows the agents you started.`)}`)}${when(!conversationMode, `
@@ -21830,6 +21928,21 @@ const make$7 = fnUntraced(function* (options) {
21830
21928
  content: textContent(part.summary)
21831
21929
  }]
21832
21930
  });
21931
+ case "JobComplete": {
21932
+ const exitCode = part.exitCode === void 0 ? "" : ` (exit code ${part.exitCode})`;
21933
+ return update(session.id, {
21934
+ sessionUpdate: "tool_call",
21935
+ toolCallId: `job-${part.id}`,
21936
+ title: `background job ${part.id} ${part.status}${exitCode}`,
21937
+ kind: "execute",
21938
+ status: part.status === "completed" ? "completed" : "failed",
21939
+ rawOutput: part.output,
21940
+ content: [{
21941
+ type: "content",
21942
+ content: textContent(part.output)
21943
+ }]
21944
+ });
21945
+ }
21833
21946
  default: return void_;
21834
21947
  }
21835
21948
  };
@@ -28350,7 +28463,7 @@ const transformToolCallParams$1 = fnUntraced(function* (options, toolName, toolP
28350
28463
  return yield* decodeEffect(codec)(toolParams).pipe(flatMap$1((decoded) => encodeUnknownEffect(tool.parametersSchema)(decoded)), orElseSucceed(() => toolParams));
28351
28464
  });
28352
28465
  // package.json
28353
- var version$1 = "0.19.1";
28466
+ var version$1 = "0.20.0";
28354
28467
  // src/ClaudeAgent.ts
28355
28468
  const minimumClaudeCodeVersion = "2.1.248";
28356
28469
  const serverName = "clanka";
@@ -31123,7 +31236,7 @@ make$27("clanka", {
31123
31236
  files: mcpConfig,
31124
31237
  enabled: mcp
31125
31238
  });
31126
- const OutputFormatter = yield* promise(() => import("./OutputFormatter-Dqxyk2qD.mjs"));
31239
+ const OutputFormatter = yield* promise(() => import("./OutputFormatter-U02YKTfy.mjs"));
31127
31240
  const { model, effort } = yield* match$2(modelRaw, {
31128
31241
  onSome: (selected) => {
31129
31242
  const [model, effort] = selected.split("/");