clanka 0.19.1 → 0.21.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.
- package/dist/Acp.d.ts.map +1 -1
- package/dist/Acp.js +14 -0
- package/dist/Acp.js.map +1 -1
- package/dist/Acp.test.js +78 -1
- package/dist/Acp.test.js.map +1 -1
- package/dist/Agent.d.ts.map +1 -1
- package/dist/Agent.js +64 -3
- package/dist/Agent.js.map +1 -1
- package/dist/AgentBashJobs.test.d.ts +2 -0
- package/dist/AgentBashJobs.test.d.ts.map +1 -0
- package/dist/AgentBashJobs.test.js +177 -0
- package/dist/AgentBashJobs.test.js.map +1 -0
- package/dist/AgentExecutor.d.ts +35 -2
- package/dist/AgentExecutor.d.ts.map +1 -1
- package/dist/AgentExecutor.js +74 -3
- package/dist/AgentExecutor.js.map +1 -1
- package/dist/AgentExecutor.test.js +87 -11
- package/dist/AgentExecutor.test.js.map +1 -1
- package/dist/AgentOutput.d.ts +19 -4
- package/dist/AgentOutput.d.ts.map +1 -1
- package/dist/AgentOutput.js +15 -0
- package/dist/AgentOutput.js.map +1 -1
- package/dist/AgentSubagents.test.js +2 -2
- package/dist/AgentSubagents.test.js.map +1 -1
- package/dist/AgentTools.d.ts +119 -7
- package/dist/AgentTools.d.ts.map +1 -1
- package/dist/AgentTools.js +106 -39
- package/dist/AgentTools.js.map +1 -1
- package/dist/OutputFormatter.d.ts.map +1 -1
- package/dist/OutputFormatter.js +6 -0
- package/dist/OutputFormatter.js.map +1 -1
- package/dist/bin/{OutputFormatter-Dqxyk2qD.mjs → OutputFormatter-U02YKTfy.mjs} +5 -1
- package/dist/bin/{OutputFormatter-Dqxyk2qD.mjs.map → OutputFormatter-U02YKTfy.mjs.map} +1 -1
- package/dist/bin/cli.mjs +187 -44
- package/dist/bin/cli.mjs.map +1 -1
- package/package.json +1 -1
- package/src/Acp.test.ts +111 -1
- package/src/Acp.ts +15 -0
- package/src/Agent.ts +100 -1
- package/src/AgentBashJobs.test.ts +299 -0
- package/src/AgentExecutor.test.ts +127 -11
- package/src/AgentExecutor.ts +87 -0
- package/src/AgentOutput.ts +19 -0
- package/src/AgentSubagents.test.ts +2 -2
- package/src/AgentTools.ts +153 -48
- package/src/OutputFormatter.ts +7 -0
package/package.json
CHANGED
package/src/Acp.test.ts
CHANGED
|
@@ -530,6 +530,116 @@ describe("Acp", () => {
|
|
|
530
530
|
),
|
|
531
531
|
)
|
|
532
532
|
|
|
533
|
+
it.effect("reports a finished background job as a tool call", () =>
|
|
534
|
+
withStore(
|
|
535
|
+
Effect.scoped(
|
|
536
|
+
Effect.gen(function* () {
|
|
537
|
+
const server = yield* makeServer(runsScript("bash()"), {
|
|
538
|
+
...executor,
|
|
539
|
+
execute: ({ onJob }) =>
|
|
540
|
+
Stream.fromEffect(
|
|
541
|
+
onJob!({
|
|
542
|
+
id: "7",
|
|
543
|
+
await: Effect.succeed({
|
|
544
|
+
id: "7",
|
|
545
|
+
status: "failed",
|
|
546
|
+
exitCode: 3,
|
|
547
|
+
output: "boom\n",
|
|
548
|
+
}),
|
|
549
|
+
claim: Effect.succeed(true),
|
|
550
|
+
}).pipe(Effect.as("Started background job 7.")),
|
|
551
|
+
),
|
|
552
|
+
})
|
|
553
|
+
const created = yield* server.request(1, "session/new", {
|
|
554
|
+
cwd: "/tmp",
|
|
555
|
+
})
|
|
556
|
+
const sessionId: string = created.result.sessionId
|
|
557
|
+
yield* server.request(2, "session/prompt", {
|
|
558
|
+
sessionId,
|
|
559
|
+
prompt: [{ type: "text", text: "run it" }],
|
|
560
|
+
})
|
|
561
|
+
const jobs = server
|
|
562
|
+
.updates(sessionId)
|
|
563
|
+
.filter(
|
|
564
|
+
(u) =>
|
|
565
|
+
u.sessionUpdate.startsWith("tool_call") &&
|
|
566
|
+
u.toolCallId === "job-7",
|
|
567
|
+
)
|
|
568
|
+
assert.strictEqual(jobs.length, 1)
|
|
569
|
+
assert.strictEqual(jobs[0].sessionUpdate, "tool_call")
|
|
570
|
+
assert.strictEqual(jobs[0].kind, "execute")
|
|
571
|
+
assert.strictEqual(jobs[0].status, "failed")
|
|
572
|
+
assert.strictEqual(
|
|
573
|
+
jobs[0].title,
|
|
574
|
+
"background job 7 failed (exit code 3)",
|
|
575
|
+
)
|
|
576
|
+
assert.strictEqual(jobs[0].rawOutput, "boom\n")
|
|
577
|
+
assert.deepStrictEqual(jobs[0].content, [
|
|
578
|
+
{ type: "content", content: { type: "text", text: "boom\n" } },
|
|
579
|
+
])
|
|
580
|
+
}),
|
|
581
|
+
),
|
|
582
|
+
),
|
|
583
|
+
)
|
|
584
|
+
|
|
585
|
+
// A short turn that starts a background command usually ends before the
|
|
586
|
+
// command does.
|
|
587
|
+
it.effect("reports a background job that ends between prompts", () =>
|
|
588
|
+
withStore(
|
|
589
|
+
Effect.scoped(
|
|
590
|
+
Effect.gen(function* () {
|
|
591
|
+
const ended = yield* Deferred.make<void>()
|
|
592
|
+
const server = yield* makeServer(runsScript("bash()"), {
|
|
593
|
+
...executor,
|
|
594
|
+
execute: ({ onJob }) =>
|
|
595
|
+
Stream.fromEffect(
|
|
596
|
+
onJob!({
|
|
597
|
+
id: "7",
|
|
598
|
+
await: Deferred.await(ended).pipe(
|
|
599
|
+
Effect.as({
|
|
600
|
+
id: "7",
|
|
601
|
+
status: "completed" as const,
|
|
602
|
+
exitCode: 0,
|
|
603
|
+
output: "built\n",
|
|
604
|
+
}),
|
|
605
|
+
),
|
|
606
|
+
claim: Effect.succeed(true),
|
|
607
|
+
}).pipe(Effect.as("Started background job 7.")),
|
|
608
|
+
),
|
|
609
|
+
})
|
|
610
|
+
const created = yield* server.request(1, "session/new", {
|
|
611
|
+
cwd: "/tmp",
|
|
612
|
+
})
|
|
613
|
+
const sessionId: string = created.result.sessionId
|
|
614
|
+
const jobUpdates = () =>
|
|
615
|
+
server
|
|
616
|
+
.updates(sessionId)
|
|
617
|
+
.filter(
|
|
618
|
+
(u) =>
|
|
619
|
+
u.sessionUpdate.startsWith("tool_call") &&
|
|
620
|
+
u.toolCallId === "job-7",
|
|
621
|
+
)
|
|
622
|
+
yield* server.request(2, "session/prompt", {
|
|
623
|
+
sessionId,
|
|
624
|
+
prompt: [{ type: "text", text: "start the build" }],
|
|
625
|
+
})
|
|
626
|
+
assert.strictEqual(jobUpdates().length, 0, "still running")
|
|
627
|
+
|
|
628
|
+
yield* Deferred.succeed(ended, undefined)
|
|
629
|
+
yield* Effect.yieldNow
|
|
630
|
+
yield* server.request(3, "session/prompt", {
|
|
631
|
+
sessionId,
|
|
632
|
+
prompt: [{ type: "text", text: "anything else?" }],
|
|
633
|
+
})
|
|
634
|
+
const jobs = jobUpdates()
|
|
635
|
+
assert.strictEqual(jobs.length, 1, "reported once")
|
|
636
|
+
assert.strictEqual(jobs[0].status, "completed")
|
|
637
|
+
assert.strictEqual(jobs[0].rawOutput, "built\n")
|
|
638
|
+
}),
|
|
639
|
+
),
|
|
640
|
+
),
|
|
641
|
+
)
|
|
642
|
+
|
|
533
643
|
it.effect("reports subagents with a delegate prompt preview", () =>
|
|
534
644
|
withStore(
|
|
535
645
|
Effect.scoped(
|
|
@@ -929,7 +1039,7 @@ await changeDirectory("sub")`),
|
|
|
929
1039
|
Effect.gen(function* () {
|
|
930
1040
|
const server = yield* makeServer(
|
|
931
1041
|
runsScript(
|
|
932
|
-
`console.log("PWD:" + (await bash({ command: "pwd" })).trim())`,
|
|
1042
|
+
`console.log("PWD:" + (await bash({ command: "pwd" })).output.trim())`,
|
|
933
1043
|
),
|
|
934
1044
|
yield* localExecutor(cwd),
|
|
935
1045
|
)
|
package/src/Acp.ts
CHANGED
|
@@ -934,6 +934,21 @@ export const make = Effect.fnUntraced(function* <RAgent, RModel>(
|
|
|
934
934
|
rawOutput: part.summary,
|
|
935
935
|
content: [{ type: "content", content: textContent(part.summary) }],
|
|
936
936
|
})
|
|
937
|
+
// The script that started the job has already ended, so the job gets
|
|
938
|
+
// a tool call of its own.
|
|
939
|
+
case "JobComplete": {
|
|
940
|
+
const exitCode =
|
|
941
|
+
part.exitCode === undefined ? "" : ` (exit code ${part.exitCode})`
|
|
942
|
+
return update(session.id, {
|
|
943
|
+
sessionUpdate: "tool_call",
|
|
944
|
+
toolCallId: `job-${part.id}`,
|
|
945
|
+
title: `background job ${part.id} ${part.status}${exitCode}`,
|
|
946
|
+
kind: "execute",
|
|
947
|
+
status: part.status === "completed" ? "completed" : "failed",
|
|
948
|
+
rawOutput: part.output,
|
|
949
|
+
content: [{ type: "content", content: textContent(part.output) }],
|
|
950
|
+
})
|
|
951
|
+
}
|
|
937
952
|
default:
|
|
938
953
|
return Effect.void
|
|
939
954
|
}
|
package/src/Agent.ts
CHANGED
|
@@ -12,6 +12,8 @@ import type * as Path from "effect/Path"
|
|
|
12
12
|
import type * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"
|
|
13
13
|
import type * as HttpClient from "effect/http/HttpClient"
|
|
14
14
|
import type {
|
|
15
|
+
BackgroundJob,
|
|
16
|
+
BashJob,
|
|
15
17
|
CurrentDirectory,
|
|
16
18
|
DirectoryChanger,
|
|
17
19
|
ExcludableTool,
|
|
@@ -73,6 +75,7 @@ import {
|
|
|
73
75
|
EnvironmentUpdated,
|
|
74
76
|
HistoryAppended,
|
|
75
77
|
HistoryReplaced,
|
|
78
|
+
JobComplete,
|
|
76
79
|
RetriesExhausted,
|
|
77
80
|
} from "./AgentOutput.ts"
|
|
78
81
|
|
|
@@ -203,6 +206,9 @@ export const makeWith = Effect.fnUntraced(function* <
|
|
|
203
206
|
Scope.Scope | AgentExecutor.AgentExecutor | LoopToolServices<LoopTools>
|
|
204
207
|
> {
|
|
205
208
|
const executor = yield* AgentExecutor.AgentExecutor
|
|
209
|
+
// Background bash jobs are followed here rather than in a run, since they
|
|
210
|
+
// can outlive the run that started them.
|
|
211
|
+
const agentScope = yield* Effect.scope
|
|
206
212
|
|
|
207
213
|
const singleTool = yield* SingleTools.pipe(Effect.provide(SingleToolHandlers))
|
|
208
214
|
const capabilities = yield* executor.capabilities
|
|
@@ -220,6 +226,7 @@ export const makeWith = Effect.fnUntraced(function* <
|
|
|
220
226
|
.filter((dts) => dts !== "")
|
|
221
227
|
.join("\n\n"),
|
|
222
228
|
offers: (name) => capabilities.tools.includes(name),
|
|
229
|
+
jobPushes: capabilities.jobPushes,
|
|
223
230
|
skillsCatalog:
|
|
224
231
|
options.skills === undefined
|
|
225
232
|
? AgentSkills.renderCatalog(capabilities.skills)
|
|
@@ -316,6 +323,35 @@ export const makeWith = Effect.fnUntraced(function* <
|
|
|
316
323
|
const prompt = opts.history
|
|
317
324
|
const mailbox = opts.mailbox
|
|
318
325
|
|
|
326
|
+
// A job that ends is queued for the model and shown straight away, or at
|
|
327
|
+
// the start of the next run when none is running. It neither wakes a
|
|
328
|
+
// waiting run nor keeps one alive.
|
|
329
|
+
const onJob = (job: BackgroundJob) =>
|
|
330
|
+
job.await.pipe(
|
|
331
|
+
Effect.flatMap((final) =>
|
|
332
|
+
Effect.sync(() => {
|
|
333
|
+
// `await` only returns once the job has ended.
|
|
334
|
+
if (final.status === "running") return
|
|
335
|
+
mailbox.jobs.push({ job: final, claim: job.claim })
|
|
336
|
+
const event = new JobComplete({
|
|
337
|
+
id: final.id,
|
|
338
|
+
status: final.status,
|
|
339
|
+
...(final.exitCode === undefined
|
|
340
|
+
? {}
|
|
341
|
+
: { exitCode: final.exitCode }),
|
|
342
|
+
output: final.output,
|
|
343
|
+
})
|
|
344
|
+
if (mailbox.emit === undefined) {
|
|
345
|
+
mailbox.events.push(event)
|
|
346
|
+
} else {
|
|
347
|
+
mailbox.emit(event)
|
|
348
|
+
}
|
|
349
|
+
}),
|
|
350
|
+
),
|
|
351
|
+
Effect.forkIn(agentScope),
|
|
352
|
+
Effect.asVoid,
|
|
353
|
+
)
|
|
354
|
+
|
|
319
355
|
// Every change to `prompt` after the system message is set goes through
|
|
320
356
|
// these two, so the history events always add up to the history.
|
|
321
357
|
const appendHistory = (input: Prompt.Prompt) => {
|
|
@@ -813,6 +849,7 @@ ${prompt}`,
|
|
|
813
849
|
subagents,
|
|
814
850
|
onTaskComplete,
|
|
815
851
|
onImage,
|
|
852
|
+
onJob,
|
|
816
853
|
loopTools: scriptLoopTools,
|
|
817
854
|
}),
|
|
818
855
|
Stream.mkString,
|
|
@@ -867,9 +904,17 @@ ${prompt}`,
|
|
|
867
904
|
}
|
|
868
905
|
}
|
|
869
906
|
|
|
907
|
+
// Events from between runs, then everything else as it happens.
|
|
908
|
+
for (const part of mailbox.events.splice(0)) {
|
|
909
|
+
maybeSend({ agentId, part })
|
|
910
|
+
}
|
|
911
|
+
const emit = (part: Output) => maybeSend({ agentId, part })
|
|
912
|
+
mailbox.emit = emit
|
|
913
|
+
|
|
870
914
|
yield* Effect.gen(function* () {
|
|
871
915
|
while (true) {
|
|
872
916
|
if (Option.isSome(finalSummary)) {
|
|
917
|
+
mailbox.emit = undefined
|
|
873
918
|
yield* Queue.fail(
|
|
874
919
|
output,
|
|
875
920
|
new AgentFinished({ summary: finalSummary.value }),
|
|
@@ -914,6 +959,24 @@ ${Compaction.capOutput(turn.output ?? "").output}
|
|
|
914
959
|
}
|
|
915
960
|
}
|
|
916
961
|
|
|
962
|
+
// Background bash jobs that ended and were not reported otherwise.
|
|
963
|
+
if (mailbox.jobs.length > 0) {
|
|
964
|
+
const pushes: Array<BashJob> = []
|
|
965
|
+
for (const { job, claim } of mailbox.jobs.splice(0)) {
|
|
966
|
+
if (yield* claim) pushes.push(job)
|
|
967
|
+
}
|
|
968
|
+
if (pushes.length > 0) {
|
|
969
|
+
appendHistory(
|
|
970
|
+
Prompt.make(
|
|
971
|
+
pushes.map((job) => ({
|
|
972
|
+
role: "user" as const,
|
|
973
|
+
content: jobMessage(job),
|
|
974
|
+
})),
|
|
975
|
+
),
|
|
976
|
+
)
|
|
977
|
+
}
|
|
978
|
+
}
|
|
979
|
+
|
|
917
980
|
// Threshold compaction before the next model call
|
|
918
981
|
yield* compactWithEvents(
|
|
919
982
|
"threshold",
|
|
@@ -1219,6 +1282,11 @@ ${Compaction.capOutput(turn.output ?? "").output}
|
|
|
1219
1282
|
return executeScript(script)
|
|
1220
1283
|
}),
|
|
1221
1284
|
Effect.ensuring(Scope.close(runScope, Exit.void)),
|
|
1285
|
+
Effect.ensuring(
|
|
1286
|
+
Effect.sync(() => {
|
|
1287
|
+
if (mailbox.emit === emit) mailbox.emit = undefined
|
|
1288
|
+
}),
|
|
1289
|
+
),
|
|
1222
1290
|
Effect.catchCause((cause) => Queue.failCause(output, cause)),
|
|
1223
1291
|
Effect.forkScoped,
|
|
1224
1292
|
)
|
|
@@ -1276,13 +1344,36 @@ interface Mailbox {
|
|
|
1276
1344
|
}>
|
|
1277
1345
|
/** Wakes the agent's run if it is waiting for activity. */
|
|
1278
1346
|
notify: () => void
|
|
1347
|
+
/**
|
|
1348
|
+
* Background bash jobs that ended. Each one is pushed to the model before
|
|
1349
|
+
* its next call, unless `claim` finds it was already reported.
|
|
1350
|
+
*/
|
|
1351
|
+
readonly jobs: Array<{
|
|
1352
|
+
readonly job: BashJob
|
|
1353
|
+
readonly claim: Effect.Effect<boolean>
|
|
1354
|
+
}>
|
|
1355
|
+
/** Emits output on the agent's current run. Unset between runs. */
|
|
1356
|
+
emit: ((part: Output) => void) | undefined
|
|
1357
|
+
/** Output from between runs, emitted when the next run starts. */
|
|
1358
|
+
readonly events: Array<Output>
|
|
1279
1359
|
}
|
|
1280
1360
|
|
|
1281
1361
|
const makeMailbox = (): Mailbox => ({
|
|
1282
1362
|
messages: new Set(),
|
|
1283
1363
|
notify: () => {},
|
|
1364
|
+
jobs: [],
|
|
1365
|
+
emit: undefined,
|
|
1366
|
+
events: [],
|
|
1284
1367
|
})
|
|
1285
1368
|
|
|
1369
|
+
const jobMessage = (job: BashJob): string => {
|
|
1370
|
+
const exitCode =
|
|
1371
|
+
job.exitCode === undefined ? "" : ` exitCode="${job.exitCode}"`
|
|
1372
|
+
return `<job id="${job.id}" status="${job.status}"${exitCode}>
|
|
1373
|
+
${Compaction.capOutput(job.output).output}
|
|
1374
|
+
</job>`
|
|
1375
|
+
}
|
|
1376
|
+
|
|
1286
1377
|
interface Child {
|
|
1287
1378
|
readonly id: number
|
|
1288
1379
|
readonly prompt: string
|
|
@@ -1393,6 +1484,8 @@ interface PromptTools {
|
|
|
1393
1484
|
readonly skillsCatalog: Option.Option<string>
|
|
1394
1485
|
/** The MCP servers section, or `""` without servers. */
|
|
1395
1486
|
readonly mcp: string
|
|
1487
|
+
/** Whether finished background bash jobs are pushed to the model. */
|
|
1488
|
+
readonly jobPushes: boolean
|
|
1396
1489
|
}
|
|
1397
1490
|
|
|
1398
1491
|
const when = (condition: boolean, text: string) => (condition ? text : "")
|
|
@@ -1422,7 +1515,13 @@ const generateSystemTools = (
|
|
|
1422
1515
|
tools.offers("bash"),
|
|
1423
1516
|
`
|
|
1424
1517
|
- Avoid passing scripts into the "bash" function, and instead write javascript.
|
|
1425
|
-
- 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
|
|
1518
|
+
- 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(
|
|
1519
|
+
tools.offers("waitJob"),
|
|
1520
|
+
` "bash" returns a task, and rejects when a command that has not been moved to the background fails. Use "waitJob" to wait for a running task; it returns the task as "running" if its timeout passes first. A background task that fails is returned with status "failed" and its \`exitCode\`.`,
|
|
1521
|
+
)}${when(
|
|
1522
|
+
tools.jobPushes,
|
|
1523
|
+
` 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.`,
|
|
1524
|
+
)}`,
|
|
1426
1525
|
)}
|
|
1427
1526
|
- Variables are not shared between executions.
|
|
1428
1527
|
- Do not use \`require\`, \`import\`, \`process\`, or any other Node.js apis.${when(
|
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
import { assert, describe, it } from "@effect/vitest"
|
|
2
|
+
import * as NodeHttpServer from "@effect/platform-node/NodeHttpServer"
|
|
3
|
+
import * as NodeServices from "@effect/platform-node/NodeServices"
|
|
4
|
+
import * as Effect from "effect/Effect"
|
|
5
|
+
import * as Exit from "effect/Exit"
|
|
6
|
+
import * as FileSystem from "effect/FileSystem"
|
|
7
|
+
import * as FetchHttpClient from "effect/http/FetchHttpClient"
|
|
8
|
+
import * as HttpServer from "effect/http/HttpServer"
|
|
9
|
+
import * as Layer from "effect/Layer"
|
|
10
|
+
import * as Stream from "effect/Stream"
|
|
11
|
+
import type * as AiError from "effect/ai/AiError"
|
|
12
|
+
import * as LanguageModel from "effect/ai/LanguageModel"
|
|
13
|
+
import * as Model from "effect/ai/Model"
|
|
14
|
+
import type * as Prompt from "effect/ai/Prompt"
|
|
15
|
+
import type * as Response from "effect/ai/Response"
|
|
16
|
+
import * as RpcClient from "effect/rpc/RpcClient"
|
|
17
|
+
import * as RpcSerialization from "effect/rpc/RpcSerialization"
|
|
18
|
+
import * as RpcServer from "effect/rpc/RpcServer"
|
|
19
|
+
import * as Agent from "./Agent.ts"
|
|
20
|
+
import * as AgentExecutor from "./AgentExecutor.ts"
|
|
21
|
+
|
|
22
|
+
// =============================================================================
|
|
23
|
+
// Background bash jobs: completion pushes.
|
|
24
|
+
//
|
|
25
|
+
// These drive the real Agent loop and a real executor. When a background job
|
|
26
|
+
// exits, the next model call gets a user message
|
|
27
|
+
// `<job id="…" status="…" exitCode="…">output</job>`, unless the job was
|
|
28
|
+
// already reported by `stopJob` or by a `jobOutput` call that saw its final
|
|
29
|
+
// status. Job IDs come from the executor's counter: "1", "2", ...
|
|
30
|
+
// =============================================================================
|
|
31
|
+
|
|
32
|
+
type Respond = (
|
|
33
|
+
index: number,
|
|
34
|
+
) => Stream.Stream<Response.StreamPartEncoded, AiError.AiError>
|
|
35
|
+
|
|
36
|
+
const runAgent = (respond: Respond) =>
|
|
37
|
+
Effect.gen(function* () {
|
|
38
|
+
const executor = yield* AgentExecutor.AgentExecutor
|
|
39
|
+
const prompts: Array<Prompt.Prompt> = []
|
|
40
|
+
const languageModel = yield* LanguageModel.make({
|
|
41
|
+
generateText: () => Effect.die("unexpected generateText call"),
|
|
42
|
+
streamText: (providerOptions) => {
|
|
43
|
+
const index = prompts.length
|
|
44
|
+
prompts.push(providerOptions.prompt)
|
|
45
|
+
return respond(index)
|
|
46
|
+
},
|
|
47
|
+
})
|
|
48
|
+
const agent = yield* Agent.make.pipe(
|
|
49
|
+
Effect.provideService(AgentExecutor.AgentExecutor, executor),
|
|
50
|
+
)
|
|
51
|
+
const exit = yield* agent.send({ prompt: "hello" }).pipe(
|
|
52
|
+
Effect.flatMap((stream) =>
|
|
53
|
+
stream.pipe(
|
|
54
|
+
Stream.runDrain,
|
|
55
|
+
Effect.as(""),
|
|
56
|
+
Effect.catchTag("AgentFinished", (finished) =>
|
|
57
|
+
Effect.succeed(finished.summary),
|
|
58
|
+
),
|
|
59
|
+
),
|
|
60
|
+
),
|
|
61
|
+
Effect.provide(
|
|
62
|
+
Layer.mergeAll(
|
|
63
|
+
Layer.succeed(LanguageModel.LanguageModel, languageModel),
|
|
64
|
+
Layer.succeed(Model.ProviderName, "test-provider"),
|
|
65
|
+
Layer.succeed(Model.ModelName, "test-model"),
|
|
66
|
+
Agent.ConversationMode.layer(true),
|
|
67
|
+
),
|
|
68
|
+
),
|
|
69
|
+
Effect.exit,
|
|
70
|
+
)
|
|
71
|
+
return { exit, prompts }
|
|
72
|
+
}).pipe(Effect.scoped, Effect.timeout("5 seconds"))
|
|
73
|
+
|
|
74
|
+
// --- fixtures ----------------------------------------------------------------
|
|
75
|
+
|
|
76
|
+
const execute = (
|
|
77
|
+
id: string,
|
|
78
|
+
script: string,
|
|
79
|
+
): Stream.Stream<Response.StreamPartEncoded> =>
|
|
80
|
+
Stream.make({
|
|
81
|
+
type: "tool-call",
|
|
82
|
+
id,
|
|
83
|
+
name: "execute",
|
|
84
|
+
params: { script },
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
const text = (content: string): Stream.Stream<Response.StreamPartEncoded> =>
|
|
88
|
+
Stream.make(
|
|
89
|
+
{ type: "text-start", id: "t" },
|
|
90
|
+
{ type: "text-delta", id: "t", delta: content },
|
|
91
|
+
{ type: "text-end", id: "t" },
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
const messageText = (message: Prompt.Message): string =>
|
|
95
|
+
typeof message.content === "string"
|
|
96
|
+
? message.content
|
|
97
|
+
: message.content
|
|
98
|
+
.map((part) => (part.type === "text" ? part.text : ""))
|
|
99
|
+
.join("")
|
|
100
|
+
|
|
101
|
+
/** The system message of the first model call. */
|
|
102
|
+
const systemPrompt = (prompts: ReadonlyArray<Prompt.Prompt>): string =>
|
|
103
|
+
messageText(prompts[0]!.content[0]!)
|
|
104
|
+
|
|
105
|
+
/** The user messages in `prompt` that carry a completion push. */
|
|
106
|
+
const jobPushes = (prompt: Prompt.Prompt): Array<string> =>
|
|
107
|
+
prompt.content.flatMap((message) => {
|
|
108
|
+
if (message.role !== "user") return []
|
|
109
|
+
const text = messageText(message)
|
|
110
|
+
return text.includes("<job id=") ? [text] : []
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
// --- executors ---------------------------------------------------------------
|
|
114
|
+
|
|
115
|
+
const localExecutorLayer = Layer.unwrap(
|
|
116
|
+
Effect.gen(function* () {
|
|
117
|
+
const fs = yield* FileSystem.FileSystem
|
|
118
|
+
const directory = yield* fs.makeTempDirectoryScoped()
|
|
119
|
+
return AgentExecutor.layerLocal({ directory })
|
|
120
|
+
}),
|
|
121
|
+
).pipe(Layer.provide(Layer.mergeAll(NodeServices.layer, FetchHttpClient.layer)))
|
|
122
|
+
|
|
123
|
+
// Serves a fresh executor and connects an RPC client protocol to it.
|
|
124
|
+
const rpcProtocolLayer = Layer.unwrap(
|
|
125
|
+
Effect.gen(function* () {
|
|
126
|
+
const fs = yield* FileSystem.FileSystem
|
|
127
|
+
const directory = yield* fs.makeTempDirectoryScoped()
|
|
128
|
+
const { protocol, httpEffect } =
|
|
129
|
+
yield* RpcServer.makeProtocolWithHttpEffect()
|
|
130
|
+
yield* Layer.build(
|
|
131
|
+
AgentExecutor.layerRpcServer({ directory }).pipe(
|
|
132
|
+
Layer.provide(Layer.succeed(RpcServer.Protocol, protocol)),
|
|
133
|
+
),
|
|
134
|
+
)
|
|
135
|
+
yield* HttpServer.serveEffect(httpEffect)
|
|
136
|
+
return RpcClient.layerProtocolHttp({ url: "" })
|
|
137
|
+
}),
|
|
138
|
+
).pipe(
|
|
139
|
+
Layer.provide(
|
|
140
|
+
Layer.mergeAll(
|
|
141
|
+
RpcSerialization.layerNdjson,
|
|
142
|
+
NodeHttpServer.layerTest,
|
|
143
|
+
NodeServices.layer,
|
|
144
|
+
),
|
|
145
|
+
),
|
|
146
|
+
)
|
|
147
|
+
|
|
148
|
+
const rpcExecutorLayer = AgentExecutor.layerRpc.pipe(
|
|
149
|
+
Layer.provide(rpcProtocolLayer),
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
const executors = [
|
|
153
|
+
["local", localExecutorLayer],
|
|
154
|
+
["rpc", rpcExecutorLayer],
|
|
155
|
+
] as const
|
|
156
|
+
|
|
157
|
+
// --- tests -------------------------------------------------------------------
|
|
158
|
+
|
|
159
|
+
for (const [executorName, executorLayer] of executors) {
|
|
160
|
+
// Each test gets its own executor, so job IDs start at "1".
|
|
161
|
+
describe(`background bash jobs (${executorName} executor)`, () => {
|
|
162
|
+
it.live(
|
|
163
|
+
"a finished job is pushed once with its id, status, exit code and output",
|
|
164
|
+
() =>
|
|
165
|
+
Effect.gen(function* () {
|
|
166
|
+
const { exit, prompts } = yield* runAgent((index) => {
|
|
167
|
+
switch (index) {
|
|
168
|
+
case 0:
|
|
169
|
+
// The script outlives the job, so it has exited before the
|
|
170
|
+
// next model call.
|
|
171
|
+
return execute(
|
|
172
|
+
"start",
|
|
173
|
+
`await bash({ command: "echo JOB-OUTPUT && exit 3", background: true })
|
|
174
|
+
await sleep(500)`,
|
|
175
|
+
)
|
|
176
|
+
case 1:
|
|
177
|
+
return execute("again", `console.log("AGAIN")`)
|
|
178
|
+
default:
|
|
179
|
+
return text("done")
|
|
180
|
+
}
|
|
181
|
+
})
|
|
182
|
+
|
|
183
|
+
assert.deepStrictEqual(exit, Exit.succeed("done"))
|
|
184
|
+
assert.strictEqual(prompts.length, 3)
|
|
185
|
+
const pushes = jobPushes(prompts[1]!)
|
|
186
|
+
assert.strictEqual(pushes.length, 1, "pushed before the next call")
|
|
187
|
+
assert.match(
|
|
188
|
+
pushes[0]!,
|
|
189
|
+
/^<job id="1" status="failed" exitCode="3">\s*JOB-OUTPUT\s*<\/job>$/,
|
|
190
|
+
)
|
|
191
|
+
assert.deepStrictEqual(
|
|
192
|
+
jobPushes(prompts[2]!),
|
|
193
|
+
pushes,
|
|
194
|
+
"not pushed a second time",
|
|
195
|
+
)
|
|
196
|
+
}).pipe(Effect.provide(executorLayer)),
|
|
197
|
+
)
|
|
198
|
+
|
|
199
|
+
it.live(
|
|
200
|
+
"a job whose final state stopJob, jobOutput, waitJob or bash returned is not pushed",
|
|
201
|
+
() =>
|
|
202
|
+
Effect.gen(function* () {
|
|
203
|
+
const { exit, prompts } = yield* runAgent((index) => {
|
|
204
|
+
switch (index) {
|
|
205
|
+
case 0:
|
|
206
|
+
// Job 3 is never read, so it is the only push.
|
|
207
|
+
return execute(
|
|
208
|
+
"start",
|
|
209
|
+
`await bash({ command: "sleep 30", background: true })
|
|
210
|
+
await bash({ command: "echo READ", background: true })
|
|
211
|
+
await bash({ command: "echo UNREAD", background: true })
|
|
212
|
+
await stopJob("1")
|
|
213
|
+
let job = await jobOutput("2")
|
|
214
|
+
while (job.status === "running") {
|
|
215
|
+
await sleep(20)
|
|
216
|
+
job = await jobOutput("2")
|
|
217
|
+
}
|
|
218
|
+
await bash({ command: "echo FOREGROUND" })
|
|
219
|
+
await waitJob({ id: (await bash({ command: "echo WAITED", background: true })).id })
|
|
220
|
+
await sleep(500)
|
|
221
|
+
console.log("SCRIPT" + "-DONE")`,
|
|
222
|
+
)
|
|
223
|
+
default:
|
|
224
|
+
return text("done")
|
|
225
|
+
}
|
|
226
|
+
})
|
|
227
|
+
|
|
228
|
+
assert.deepStrictEqual(exit, Exit.succeed("done"))
|
|
229
|
+
assert.strictEqual(prompts.length, 2)
|
|
230
|
+
const pushes = jobPushes(prompts[1]!)
|
|
231
|
+
assert.strictEqual(pushes.length, 1)
|
|
232
|
+
assert.match(
|
|
233
|
+
pushes[0]!,
|
|
234
|
+
/^<job id="3" status="completed" exitCode="0">\s*UNREAD\s*<\/job>$/,
|
|
235
|
+
)
|
|
236
|
+
assert.include(JSON.stringify(prompts[1]), "SCRIPT-DONE")
|
|
237
|
+
}).pipe(Effect.provide(executorLayer)),
|
|
238
|
+
)
|
|
239
|
+
})
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// Clients and executor servers of different versions must keep working
|
|
243
|
+
// together.
|
|
244
|
+
describe("background bash jobs across versions", () => {
|
|
245
|
+
it.live(
|
|
246
|
+
"the prompt promises job pushes only when the executor sends them",
|
|
247
|
+
() =>
|
|
248
|
+
Effect.gen(function* () {
|
|
249
|
+
const { current, capabilities } = yield* Effect.gen(function* () {
|
|
250
|
+
const executor = yield* AgentExecutor.AgentExecutor
|
|
251
|
+
return {
|
|
252
|
+
current: yield* runAgent(() => text("done")),
|
|
253
|
+
capabilities: yield* executor.capabilities,
|
|
254
|
+
}
|
|
255
|
+
}).pipe(Effect.provide(localExecutorLayer))
|
|
256
|
+
assert.include(systemPrompt(current.prompts), '<job id="..."')
|
|
257
|
+
|
|
258
|
+
// The current executor's tool typings without job pushes: what an
|
|
259
|
+
// older client sees from a current server, or what a custom executor
|
|
260
|
+
// that does not set `jobPushes` renders.
|
|
261
|
+
const withoutJobPushes = AgentExecutor.AgentExecutor.of({
|
|
262
|
+
capabilities: Effect.succeed(
|
|
263
|
+
new AgentExecutor.Capabilities({
|
|
264
|
+
...capabilities,
|
|
265
|
+
jobPushes: false,
|
|
266
|
+
}),
|
|
267
|
+
),
|
|
268
|
+
execute: () => Stream.empty,
|
|
269
|
+
executeUnsafe: () => Effect.die("executeUnsafe not implemented"),
|
|
270
|
+
})
|
|
271
|
+
const older = yield* runAgent(() => text("done")).pipe(
|
|
272
|
+
Effect.provideService(AgentExecutor.AgentExecutor, withoutJobPushes),
|
|
273
|
+
)
|
|
274
|
+
const olderPrompt = systemPrompt(older.prompts)
|
|
275
|
+
assert.include(olderPrompt, "bash(", "the tool typings are included")
|
|
276
|
+
assert.include(olderPrompt, '"jobOutput"')
|
|
277
|
+
assert.notInclude(olderPrompt, "<job", "no push is promised")
|
|
278
|
+
}),
|
|
279
|
+
)
|
|
280
|
+
|
|
281
|
+
// A client from before job pushes cannot decode a `Job` part, so one would
|
|
282
|
+
// end its script with a defect.
|
|
283
|
+
it.live("the server sends no Job parts to a client that did not ask", () =>
|
|
284
|
+
Effect.gen(function* () {
|
|
285
|
+
const client = yield* RpcClient.make(AgentExecutor.Rpcs)
|
|
286
|
+
const { environment } = yield* client.capabilities()
|
|
287
|
+
const parts = yield* client
|
|
288
|
+
.execute({
|
|
289
|
+
script: `await bash({ command: "true", background: true })`,
|
|
290
|
+
environment,
|
|
291
|
+
loopTools: [],
|
|
292
|
+
})
|
|
293
|
+
.pipe(Stream.runCollect)
|
|
294
|
+
const tags = parts.map((part) => part._tag)
|
|
295
|
+
assert.include(tags, "Environment", "the script ran")
|
|
296
|
+
assert.notInclude(tags, "Job")
|
|
297
|
+
}).pipe(Effect.scoped, Effect.provide(rpcProtocolLayer)),
|
|
298
|
+
)
|
|
299
|
+
})
|