clanka 0.8.6 → 0.10.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 +6 -1
- package/dist/Acp.d.ts.map +1 -1
- package/dist/Acp.js +34 -3
- package/dist/Acp.js.map +1 -1
- package/dist/Acp.test.js +178 -2
- package/dist/Acp.test.js.map +1 -1
- package/dist/AcpImage.test.js +7 -1
- package/dist/AcpImage.test.js.map +1 -1
- package/dist/Agent.d.ts +82 -8
- package/dist/Agent.d.ts.map +1 -1
- package/dist/Agent.js +556 -113
- package/dist/Agent.js.map +1 -1
- package/dist/Agent.test.js +86 -5
- package/dist/Agent.test.js.map +1 -1
- package/dist/AgentCallerConfig.test.d.ts +2 -0
- package/dist/AgentCallerConfig.test.d.ts.map +1 -0
- package/dist/AgentCallerConfig.test.js +335 -0
- package/dist/AgentCallerConfig.test.js.map +1 -0
- package/dist/AgentEnvironment.d.ts +76 -0
- package/dist/AgentEnvironment.d.ts.map +1 -0
- package/dist/AgentEnvironment.js +119 -0
- package/dist/AgentEnvironment.js.map +1 -0
- package/dist/AgentEnvironment.test.d.ts +2 -0
- package/dist/AgentEnvironment.test.d.ts.map +1 -0
- package/dist/AgentEnvironment.test.js +304 -0
- package/dist/AgentEnvironment.test.js.map +1 -0
- package/dist/AgentExecutor.d.ts +342 -183
- package/dist/AgentExecutor.d.ts.map +1 -1
- package/dist/AgentExecutor.js +607 -127
- package/dist/AgentExecutor.js.map +1 -1
- package/dist/AgentExecutor.test.js +287 -31
- package/dist/AgentExecutor.test.js.map +1 -1
- package/dist/AgentExecutorImage.test.js +5 -1
- package/dist/AgentExecutorImage.test.js.map +1 -1
- package/dist/AgentImage.test.js +15 -2
- package/dist/AgentImage.test.js.map +1 -1
- package/dist/AgentOutput.d.ts +70 -5
- package/dist/AgentOutput.d.ts.map +1 -1
- package/dist/AgentOutput.js +65 -0
- package/dist/AgentOutput.js.map +1 -1
- package/dist/AgentSkills.d.ts +37 -0
- package/dist/AgentSkills.d.ts.map +1 -1
- package/dist/AgentSkills.js +96 -32
- package/dist/AgentSkills.js.map +1 -1
- package/dist/AgentSkills.test.js +14 -2
- package/dist/AgentSkills.test.js.map +1 -1
- package/dist/AgentSubagents.test.d.ts +2 -0
- package/dist/AgentSubagents.test.d.ts.map +1 -0
- package/dist/AgentSubagents.test.js +494 -0
- package/dist/AgentSubagents.test.js.map +1 -0
- package/dist/AgentTools.d.ts +356 -14
- package/dist/AgentTools.d.ts.map +1 -1
- package/dist/AgentTools.js +214 -35
- package/dist/AgentTools.js.map +1 -1
- package/dist/OutputFormatter.d.ts.map +1 -1
- package/dist/OutputFormatter.js +19 -2
- package/dist/OutputFormatter.js.map +1 -1
- package/dist/ToolkitRenderer.d.ts.map +1 -1
- package/dist/ToolkitRenderer.js +6 -1
- package/dist/ToolkitRenderer.js.map +1 -1
- package/dist/bin/{Effect-rADALOOD.mjs → Effect-2jPlJ8eu.mjs} +43 -38
- package/dist/bin/Effect-2jPlJ8eu.mjs.map +1 -0
- package/dist/bin/{McpClient-NazR2OwL.mjs → McpClient-1W508adc.mjs} +3 -3
- package/dist/bin/{McpClient-NazR2OwL.mjs.map → McpClient-1W508adc.mjs.map} +1 -1
- package/dist/bin/{OutputFormatter-BVHbgF2b.mjs → OutputFormatter-BFAv48k9.mjs} +17 -5
- package/dist/bin/{OutputFormatter-BVHbgF2b.mjs.map → OutputFormatter-BFAv48k9.mjs.map} +1 -1
- package/dist/bin/{Schema-C7NXAOWA.mjs → Schema-D-7VuCiC.mjs} +2 -2
- package/dist/bin/{Schema-C7NXAOWA.mjs.map → Schema-D-7VuCiC.mjs.map} +1 -1
- package/dist/bin/{SemanticSearch-4Jes3jCl.mjs → SemanticSearch-Cb8aqvGv.mjs} +4 -4
- package/dist/bin/{SemanticSearch-4Jes3jCl.mjs.map → SemanticSearch-Cb8aqvGv.mjs.map} +1 -1
- package/dist/bin/{Stream-CSLA1_GD.mjs → Stream-DEu2tfCV.mjs} +12 -3
- package/dist/bin/Stream-DEu2tfCV.mjs.map +1 -0
- package/dist/bin/cli.mjs +13333 -12470
- package/dist/bin/cli.mjs.map +1 -1
- package/package.json +1 -1
- package/src/Acp.test.ts +279 -3
- package/src/Acp.ts +34 -4
- package/src/AcpImage.test.ts +7 -1
- package/src/Agent.test.ts +109 -4
- package/src/Agent.ts +874 -140
- package/src/AgentCallerConfig.test.ts +522 -0
- package/src/AgentEnvironment.test.ts +504 -0
- package/src/AgentEnvironment.ts +161 -0
- package/src/AgentExecutor.test.ts +414 -37
- package/src/AgentExecutor.ts +1028 -191
- package/src/AgentExecutorImage.test.ts +5 -1
- package/src/AgentImage.test.ts +21 -3
- package/src/AgentOutput.ts +91 -1
- package/src/AgentSkills.test.ts +14 -2
- package/src/AgentSkills.ts +117 -34
- package/src/AgentSubagents.test.ts +824 -0
- package/src/AgentTools.ts +377 -53
- package/src/OutputFormatter.ts +19 -2
- package/src/ToolkitRenderer.ts +7 -1
- package/dist/bin/Effect-rADALOOD.mjs.map +0 -1
- package/dist/bin/Stream-CSLA1_GD.mjs.map +0 -1
package/package.json
CHANGED
package/src/Acp.test.ts
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
import { assert, describe, it } from "@effect/vitest"
|
|
2
|
+
import * as NodeServices from "@effect/platform-node/NodeServices"
|
|
3
|
+
import * as Context from "effect/Context"
|
|
2
4
|
import * as Effect from "effect/Effect"
|
|
5
|
+
import * as FileSystem from "effect/FileSystem"
|
|
3
6
|
import * as Fiber from "effect/Fiber"
|
|
4
7
|
import * as Layer from "effect/Layer"
|
|
5
8
|
import * as Option from "effect/Option"
|
|
6
9
|
import * as Stream from "effect/Stream"
|
|
10
|
+
import * as FetchHttpClient from "effect/http/FetchHttpClient"
|
|
7
11
|
import * as Prompt from "effect/ai/Prompt"
|
|
8
12
|
import * as KeyValueStore from "effect/persistence/KeyValueStore"
|
|
9
13
|
import * as LanguageModel from "effect/ai/LanguageModel"
|
|
@@ -24,8 +28,14 @@ type Message = {
|
|
|
24
28
|
|
|
25
29
|
const capabilities = new AgentExecutor.Capabilities({
|
|
26
30
|
toolsDts: "",
|
|
27
|
-
|
|
31
|
+
environment: new AgentExecutor.ExecutionEnvironment({
|
|
32
|
+
cwd: "/",
|
|
33
|
+
baseline: [],
|
|
34
|
+
seen: [],
|
|
35
|
+
}),
|
|
36
|
+
instructions: Option.none(),
|
|
28
37
|
supportsSearch: false,
|
|
38
|
+
tools: [],
|
|
29
39
|
skills: [],
|
|
30
40
|
})
|
|
31
41
|
|
|
@@ -35,6 +45,16 @@ const executor = AgentExecutor.AgentExecutor.of({
|
|
|
35
45
|
executeUnsafe: () => Effect.die("executeUnsafe not implemented"),
|
|
36
46
|
})
|
|
37
47
|
|
|
48
|
+
// A fresh executor per server, like the CLI builds one per session.
|
|
49
|
+
const localExecutor = (directory: string) =>
|
|
50
|
+
Layer.build(
|
|
51
|
+
AgentExecutor.layerLocal({ directory }).pipe(
|
|
52
|
+
Layer.provide(Layer.mergeAll(NodeServices.layer, FetchHttpClient.layer)),
|
|
53
|
+
),
|
|
54
|
+
).pipe(
|
|
55
|
+
Effect.map((context) => Context.get(context, AgentExecutor.AgentExecutor)),
|
|
56
|
+
)
|
|
57
|
+
|
|
38
58
|
const makeServer = (
|
|
39
59
|
streamText: Parameters<typeof LanguageModel.make>[0]["streamText"],
|
|
40
60
|
agentExecutor = executor,
|
|
@@ -99,6 +119,25 @@ const assistantSays = (text: string) =>
|
|
|
99
119
|
{ type: "text-end", id: "1" },
|
|
100
120
|
)
|
|
101
121
|
|
|
122
|
+
// A model that runs `script` once, then answers "done".
|
|
123
|
+
const runsScript = (script: string) => {
|
|
124
|
+
let calls = 0
|
|
125
|
+
return () =>
|
|
126
|
+
calls++ === 0
|
|
127
|
+
? parts({
|
|
128
|
+
type: "tool-call",
|
|
129
|
+
id: "call-1",
|
|
130
|
+
name: "execute",
|
|
131
|
+
params: { script },
|
|
132
|
+
})
|
|
133
|
+
: assistantSays("done")
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const message = (role: "user" | "assistant", text: string) =>
|
|
137
|
+
Prompt.makeMessage(role, {
|
|
138
|
+
content: [Prompt.makePart("text", { text })],
|
|
139
|
+
})
|
|
140
|
+
|
|
102
141
|
const withStore = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
|
|
103
142
|
effect.pipe(
|
|
104
143
|
Effect.provide(
|
|
@@ -194,6 +233,8 @@ describe("Acp", () => {
|
|
|
194
233
|
.find((update) => update.sessionUpdate === "usage_update"),
|
|
195
234
|
{
|
|
196
235
|
sessionUpdate: "usage_update",
|
|
236
|
+
used: 120,
|
|
237
|
+
size: 236_000,
|
|
197
238
|
usage: {
|
|
198
239
|
inputTokens: 84,
|
|
199
240
|
outputTokens: 12,
|
|
@@ -244,6 +285,8 @@ describe("Acp", () => {
|
|
|
244
285
|
.find((update) => update.sessionUpdate === "usage_update"),
|
|
245
286
|
{
|
|
246
287
|
sessionUpdate: "usage_update",
|
|
288
|
+
used: 120,
|
|
289
|
+
size: 236_000,
|
|
247
290
|
usage: {
|
|
248
291
|
inputTokens: 120,
|
|
249
292
|
outputTokens: 12,
|
|
@@ -425,8 +468,20 @@ describe("Acp", () => {
|
|
|
425
468
|
: assistantSays(summary),
|
|
426
469
|
{
|
|
427
470
|
...executor,
|
|
428
|
-
execute: ({
|
|
429
|
-
Stream.fromEffect(
|
|
471
|
+
execute: ({ subagents, environment }) =>
|
|
472
|
+
Stream.fromEffect(
|
|
473
|
+
subagents.delegate({
|
|
474
|
+
prompt,
|
|
475
|
+
background: false,
|
|
476
|
+
environment: new AgentExecutor.EnvironmentUpdate({
|
|
477
|
+
environment,
|
|
478
|
+
update: Option.none(),
|
|
479
|
+
}),
|
|
480
|
+
}),
|
|
481
|
+
).pipe(
|
|
482
|
+
Stream.map((result) => result.output ?? ""),
|
|
483
|
+
Stream.orDie,
|
|
484
|
+
),
|
|
430
485
|
},
|
|
431
486
|
)
|
|
432
487
|
const created = yield* server.request(1, "session/new", {
|
|
@@ -468,6 +523,126 @@ describe("Acp", () => {
|
|
|
468
523
|
),
|
|
469
524
|
)
|
|
470
525
|
|
|
526
|
+
it.effect(
|
|
527
|
+
"resuming a finished subagent does not create its tool call twice",
|
|
528
|
+
() =>
|
|
529
|
+
withStore(
|
|
530
|
+
Effect.scoped(
|
|
531
|
+
Effect.gen(function* () {
|
|
532
|
+
let childId: string | undefined
|
|
533
|
+
let executions = 0
|
|
534
|
+
const server = yield* makeServer(
|
|
535
|
+
({ prompt }) => {
|
|
536
|
+
if (JSON.stringify(prompt.content).includes("TASK-CHILD")) {
|
|
537
|
+
const resumed = JSON.stringify(prompt.content).includes(
|
|
538
|
+
"FOLLOW-UP",
|
|
539
|
+
)
|
|
540
|
+
return assistantSays(
|
|
541
|
+
resumed ? "SECOND-RESULT" : "FIRST-RESULT",
|
|
542
|
+
)
|
|
543
|
+
}
|
|
544
|
+
const seen = JSON.stringify(prompt.content)
|
|
545
|
+
if (seen.includes("SECOND-RESULT")) return assistantSays("done")
|
|
546
|
+
if (
|
|
547
|
+
executions === 0 ||
|
|
548
|
+
(executions === 1 && seen.includes("FIRST-RESULT"))
|
|
549
|
+
) {
|
|
550
|
+
return parts({
|
|
551
|
+
type: "tool-call",
|
|
552
|
+
id: `call-${executions}`,
|
|
553
|
+
name: "execute",
|
|
554
|
+
params: { script: "await work()" },
|
|
555
|
+
})
|
|
556
|
+
}
|
|
557
|
+
return assistantSays("waiting")
|
|
558
|
+
},
|
|
559
|
+
{
|
|
560
|
+
...executor,
|
|
561
|
+
// The first script starts a background child; the second
|
|
562
|
+
// resumes it once its first result has been delivered.
|
|
563
|
+
execute: ({ subagents, environment }) =>
|
|
564
|
+
Stream.fromEffect(
|
|
565
|
+
executions++ === 0
|
|
566
|
+
? subagents
|
|
567
|
+
.delegate({
|
|
568
|
+
prompt: "TASK-CHILD",
|
|
569
|
+
background: true,
|
|
570
|
+
environment: new AgentExecutor.EnvironmentUpdate({
|
|
571
|
+
environment,
|
|
572
|
+
update: Option.none(),
|
|
573
|
+
}),
|
|
574
|
+
})
|
|
575
|
+
.pipe(
|
|
576
|
+
Effect.map((result) => {
|
|
577
|
+
childId = result.id
|
|
578
|
+
return "started"
|
|
579
|
+
}),
|
|
580
|
+
)
|
|
581
|
+
: subagents
|
|
582
|
+
.messageAgent({ id: childId!, message: "FOLLOW-UP" })
|
|
583
|
+
.pipe(Effect.as("resumed")),
|
|
584
|
+
).pipe(Stream.orDie),
|
|
585
|
+
},
|
|
586
|
+
)
|
|
587
|
+
const created = yield* server.request(1, "session/new", {
|
|
588
|
+
cwd: "/tmp",
|
|
589
|
+
})
|
|
590
|
+
const sessionId: string = created.result.sessionId
|
|
591
|
+
yield* server.request(2, "session/prompt", {
|
|
592
|
+
sessionId,
|
|
593
|
+
prompt: [{ type: "text", text: "delegate and resume" }],
|
|
594
|
+
})
|
|
595
|
+
|
|
596
|
+
assert.strictEqual(executions, 2, "the child was resumed")
|
|
597
|
+
const tools = server
|
|
598
|
+
.updates(sessionId)
|
|
599
|
+
.filter(
|
|
600
|
+
(u) =>
|
|
601
|
+
u.sessionUpdate.startsWith("tool_call") &&
|
|
602
|
+
u.toolCallId.startsWith("subagent-"),
|
|
603
|
+
)
|
|
604
|
+
const creations = tools
|
|
605
|
+
.filter((u) => u.sessionUpdate === "tool_call")
|
|
606
|
+
.map((u) => u.toolCallId)
|
|
607
|
+
assert.deepStrictEqual(
|
|
608
|
+
creations,
|
|
609
|
+
[...new Set(creations)],
|
|
610
|
+
"ACP tool call IDs must be unique within the session",
|
|
611
|
+
)
|
|
612
|
+
for (const u of tools) {
|
|
613
|
+
assert.include(
|
|
614
|
+
creations,
|
|
615
|
+
u.toolCallId,
|
|
616
|
+
"updates refer to a created tool call",
|
|
617
|
+
)
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
const firstDone = tools.findIndex(
|
|
621
|
+
(u) => u.status === "completed" && u.rawOutput === "FIRST-RESULT",
|
|
622
|
+
)
|
|
623
|
+
const secondDone = tools.findIndex(
|
|
624
|
+
(u) =>
|
|
625
|
+
u.status === "completed" && u.rawOutput === "SECOND-RESULT",
|
|
626
|
+
)
|
|
627
|
+
assert.isAbove(firstDone, -1, "first result shown")
|
|
628
|
+
assert.isAbove(secondDone, firstDone, "second result shown")
|
|
629
|
+
assert.isTrue(
|
|
630
|
+
tools
|
|
631
|
+
.slice(firstDone + 1, secondDone)
|
|
632
|
+
.some((u) => u.status === "in_progress"),
|
|
633
|
+
"the resumed turn is shown as running",
|
|
634
|
+
)
|
|
635
|
+
assert.deepStrictEqual(tools[secondDone].content, [
|
|
636
|
+
{
|
|
637
|
+
type: "content",
|
|
638
|
+
content: { type: "text", text: "SECOND-RESULT" },
|
|
639
|
+
},
|
|
640
|
+
])
|
|
641
|
+
}),
|
|
642
|
+
),
|
|
643
|
+
),
|
|
644
|
+
)
|
|
645
|
+
|
|
471
646
|
it.effect("reports canonical state for new and loaded sessions", () =>
|
|
472
647
|
withStore(
|
|
473
648
|
Effect.gen(function* () {
|
|
@@ -621,6 +796,107 @@ describe("Acp", () => {
|
|
|
621
796
|
),
|
|
622
797
|
)
|
|
623
798
|
|
|
799
|
+
it.live("keeps the working directory across a session reload", () =>
|
|
800
|
+
withStore(
|
|
801
|
+
Effect.gen(function* () {
|
|
802
|
+
const fs = yield* FileSystem.FileSystem
|
|
803
|
+
const cwd = yield* fs.makeTempDirectoryScoped()
|
|
804
|
+
const sessionId = yield* Effect.scoped(
|
|
805
|
+
Effect.gen(function* () {
|
|
806
|
+
const server = yield* makeServer(
|
|
807
|
+
runsScript(`await bash({ command: "mkdir sub" })
|
|
808
|
+
await changeDirectory("sub")`),
|
|
809
|
+
yield* localExecutor(cwd),
|
|
810
|
+
)
|
|
811
|
+
const created = yield* server.request(1, "session/new", { cwd })
|
|
812
|
+
const sessionId: string = created.result.sessionId
|
|
813
|
+
yield* server.request(2, "session/prompt", {
|
|
814
|
+
sessionId,
|
|
815
|
+
prompt: [{ type: "text", text: "change directory" }],
|
|
816
|
+
})
|
|
817
|
+
return sessionId
|
|
818
|
+
}),
|
|
819
|
+
)
|
|
820
|
+
|
|
821
|
+
const output = yield* Effect.scoped(
|
|
822
|
+
Effect.gen(function* () {
|
|
823
|
+
const server = yield* makeServer(
|
|
824
|
+
runsScript(
|
|
825
|
+
`console.log("PWD:" + (await bash({ command: "pwd" })).trim())`,
|
|
826
|
+
),
|
|
827
|
+
yield* localExecutor(cwd),
|
|
828
|
+
)
|
|
829
|
+
yield* server.request(1, "session/load", { sessionId, cwd })
|
|
830
|
+
yield* server.request(2, "session/prompt", {
|
|
831
|
+
sessionId,
|
|
832
|
+
prompt: [{ type: "text", text: "print it" }],
|
|
833
|
+
})
|
|
834
|
+
return server
|
|
835
|
+
.updates(sessionId)
|
|
836
|
+
.filter((u) => u.sessionUpdate === "tool_call_update")
|
|
837
|
+
.map((u) => String(u.rawOutput))
|
|
838
|
+
.join("\n")
|
|
839
|
+
}),
|
|
840
|
+
)
|
|
841
|
+
assert.match(output, /PWD:\S*\/sub$/m)
|
|
842
|
+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
|
|
843
|
+
),
|
|
844
|
+
)
|
|
845
|
+
|
|
846
|
+
it.effect(
|
|
847
|
+
"hides environment updates and sub-agent results during replay",
|
|
848
|
+
() =>
|
|
849
|
+
withStore(
|
|
850
|
+
Effect.scoped(
|
|
851
|
+
Effect.gen(function* () {
|
|
852
|
+
const kvs = yield* KeyValueStore.KeyValueStore
|
|
853
|
+
const store = KeyValueStore.toSchemaStore(
|
|
854
|
+
KeyValueStore.prefix(kvs, "session-"),
|
|
855
|
+
Acp.SessionRecord,
|
|
856
|
+
)
|
|
857
|
+
const sessionId = "injected-session"
|
|
858
|
+
yield* store.set(
|
|
859
|
+
sessionId,
|
|
860
|
+
new Acp.SessionRecord({
|
|
861
|
+
cwd: "/tmp",
|
|
862
|
+
model: "test:model",
|
|
863
|
+
thoughtLevel: "medium",
|
|
864
|
+
history: Prompt.fromMessages([
|
|
865
|
+
message("user", "Typed by the user"),
|
|
866
|
+
message(
|
|
867
|
+
"user",
|
|
868
|
+
"<environment-update>\nPKG-RULES\n</environment-update>",
|
|
869
|
+
),
|
|
870
|
+
message(
|
|
871
|
+
"user",
|
|
872
|
+
'<subagent id="1" status="completed">\nCHILD-RESULT\n</subagent>',
|
|
873
|
+
),
|
|
874
|
+
message("assistant", "Reply"),
|
|
875
|
+
]),
|
|
876
|
+
}),
|
|
877
|
+
)
|
|
878
|
+
|
|
879
|
+
const server = yield* makeServer(() => assistantSays("unused"))
|
|
880
|
+
const loaded = yield* server.request(1, "session/load", {
|
|
881
|
+
sessionId,
|
|
882
|
+
cwd: "/tmp",
|
|
883
|
+
})
|
|
884
|
+
assert.isUndefined(loaded.error)
|
|
885
|
+
assert.deepStrictEqual(server.updates(sessionId), [
|
|
886
|
+
{
|
|
887
|
+
sessionUpdate: "user_message_chunk",
|
|
888
|
+
content: { type: "text", text: "Typed by the user" },
|
|
889
|
+
},
|
|
890
|
+
{
|
|
891
|
+
sessionUpdate: "agent_message_chunk",
|
|
892
|
+
content: { type: "text", text: "Reply" },
|
|
893
|
+
},
|
|
894
|
+
])
|
|
895
|
+
}),
|
|
896
|
+
),
|
|
897
|
+
),
|
|
898
|
+
)
|
|
899
|
+
|
|
624
900
|
it.effect("rejects unknown models and methods", () =>
|
|
625
901
|
withStore(
|
|
626
902
|
Effect.scoped(
|
package/src/Acp.ts
CHANGED
|
@@ -29,7 +29,8 @@ import type * as Model from "effect/ai/Model"
|
|
|
29
29
|
import * as Prompt from "effect/ai/Prompt"
|
|
30
30
|
import * as HttpClient from "effect/http/HttpClient"
|
|
31
31
|
import * as KeyValueStore from "effect/persistence/KeyValueStore"
|
|
32
|
-
import
|
|
32
|
+
import * as Agent from "./Agent.ts"
|
|
33
|
+
import * as AgentExecutor from "./AgentExecutor.ts"
|
|
33
34
|
import type * as AgentOutput from "./AgentOutput.ts"
|
|
34
35
|
import * as Compaction from "./Compaction.ts"
|
|
35
36
|
import * as Image from "./Image.ts"
|
|
@@ -125,6 +126,10 @@ export class SessionRecord extends Schema.Class<SessionRecord>(
|
|
|
125
126
|
model: Schema.String,
|
|
126
127
|
thoughtLevel: ThoughtLevel,
|
|
127
128
|
history: Prompt.Prompt,
|
|
129
|
+
/**
|
|
130
|
+
* The agent's executor environment, such as its working directory.
|
|
131
|
+
*/
|
|
132
|
+
environment: Schema.optionalKey(AgentExecutor.ExecutionEnvironment),
|
|
128
133
|
}) {}
|
|
129
134
|
|
|
130
135
|
/**
|
|
@@ -273,6 +278,7 @@ const historyUpdates = (history: Prompt.Prompt) => {
|
|
|
273
278
|
for (const message of history.content) {
|
|
274
279
|
if (message.role !== "user" && message.role !== "assistant") continue
|
|
275
280
|
if (Compaction.isSummaryMessage(message)) continue
|
|
281
|
+
if (Agent.isInjectedMessage(message)) continue
|
|
276
282
|
const sessionUpdate =
|
|
277
283
|
message.role === "user" ? "user_message_chunk" : "agent_message_chunk"
|
|
278
284
|
for (const part of message.content) {
|
|
@@ -297,6 +303,7 @@ interface Session {
|
|
|
297
303
|
readonly agent: Agent.Agent
|
|
298
304
|
running: Fiber.Fiber<void, unknown> | undefined
|
|
299
305
|
toolCalls: number
|
|
306
|
+
readonly announcedSubagents: Set<number>
|
|
300
307
|
}
|
|
301
308
|
|
|
302
309
|
/**
|
|
@@ -311,6 +318,10 @@ export const make = Effect.fnUntraced(function* <RAgent, RModel>(
|
|
|
311
318
|
KeyValueStore.KeyValueStore | Scope.Scope
|
|
312
319
|
> {
|
|
313
320
|
const kvs = yield* KeyValueStore.KeyValueStore
|
|
321
|
+
const { contextWindow } = yield* Compaction.CompactionConfig
|
|
322
|
+
const size = Number.isFinite(contextWindow)
|
|
323
|
+
? Math.max(0, Math.round(contextWindow))
|
|
324
|
+
: 0
|
|
314
325
|
// Optional: `https:` images need an HttpClient and `file:` images need a
|
|
315
326
|
// FileSystem and Path. Inline (base64) images work without either. When a
|
|
316
327
|
// service is missing the corresponding block is rejected, never fetched.
|
|
@@ -372,6 +383,7 @@ export const make = Effect.fnUntraced(function* <RAgent, RModel>(
|
|
|
372
383
|
model: session.model,
|
|
373
384
|
thoughtLevel: session.thoughtLevel,
|
|
374
385
|
history: session.agent.history.current,
|
|
386
|
+
environment: session.agent.environment.current,
|
|
375
387
|
}),
|
|
376
388
|
)
|
|
377
389
|
.pipe(
|
|
@@ -386,6 +398,9 @@ export const make = Effect.fnUntraced(function* <RAgent, RModel>(
|
|
|
386
398
|
) {
|
|
387
399
|
const agent = yield* Scope.provide(options.makeAgent(record.cwd), scope)
|
|
388
400
|
MutableRef.set(agent.history, record.history)
|
|
401
|
+
if (record.environment !== undefined) {
|
|
402
|
+
MutableRef.set(agent.environment, record.environment)
|
|
403
|
+
}
|
|
389
404
|
const session: Session = {
|
|
390
405
|
id,
|
|
391
406
|
cwd: record.cwd,
|
|
@@ -394,6 +409,7 @@ export const make = Effect.fnUntraced(function* <RAgent, RModel>(
|
|
|
394
409
|
agent,
|
|
395
410
|
running: undefined,
|
|
396
411
|
toolCalls: 0,
|
|
412
|
+
announcedSubagents: new Set(),
|
|
397
413
|
}
|
|
398
414
|
sessions.set(id, session)
|
|
399
415
|
return session
|
|
@@ -652,6 +668,10 @@ export const make = Effect.fnUntraced(function* <RAgent, RModel>(
|
|
|
652
668
|
case "Usage":
|
|
653
669
|
return update(session.id, {
|
|
654
670
|
sessionUpdate: "usage_update",
|
|
671
|
+
used: Number.isFinite(part.contextTokens)
|
|
672
|
+
? Math.max(0, Math.round(part.contextTokens))
|
|
673
|
+
: 0,
|
|
674
|
+
size,
|
|
655
675
|
usage: {
|
|
656
676
|
inputTokens: Math.max(
|
|
657
677
|
0,
|
|
@@ -669,20 +689,25 @@ export const make = Effect.fnUntraced(function* <RAgent, RModel>(
|
|
|
669
689
|
`Retrying after error: ${part.error.message}\n`,
|
|
670
690
|
),
|
|
671
691
|
})
|
|
672
|
-
|
|
692
|
+
// A background agent's tool call stays in progress until the agent
|
|
693
|
+
// finishes, while the parent keeps taking turns.
|
|
694
|
+
case "SubagentStart": {
|
|
695
|
+
const announced = session.announcedSubagents.has(part.id)
|
|
696
|
+
session.announcedSubagents.add(part.id)
|
|
673
697
|
return update(session.id, {
|
|
674
|
-
sessionUpdate: "tool_call",
|
|
698
|
+
sessionUpdate: announced ? "tool_call_update" : "tool_call",
|
|
675
699
|
toolCallId: `subagent-${part.id}`,
|
|
676
700
|
title: `delegate: ${part.prompt.replace(/\s+/g, " ").trim().slice(0, 120)}`,
|
|
677
701
|
kind: "think",
|
|
678
702
|
status: "in_progress",
|
|
679
703
|
rawInput: { prompt: part.prompt },
|
|
680
704
|
})
|
|
705
|
+
}
|
|
681
706
|
case "SubagentComplete":
|
|
682
707
|
return update(session.id, {
|
|
683
708
|
sessionUpdate: "tool_call_update",
|
|
684
709
|
toolCallId: `subagent-${part.id}`,
|
|
685
|
-
status: "completed",
|
|
710
|
+
status: part.status === "completed" ? "completed" : "failed",
|
|
686
711
|
rawOutput: part.summary,
|
|
687
712
|
content: [{ type: "content", content: textContent(part.summary) }],
|
|
688
713
|
})
|
|
@@ -762,6 +787,11 @@ export const make = Effect.fnUntraced(function* <RAgent, RModel>(
|
|
|
762
787
|
model: modelId,
|
|
763
788
|
thoughtLevel: record.value.thoughtLevel,
|
|
764
789
|
history: record.value.history,
|
|
790
|
+
// The stored environment belongs to the stored cwd.
|
|
791
|
+
...(record.value.environment !== undefined &&
|
|
792
|
+
(cwd === undefined || cwd === record.value.cwd)
|
|
793
|
+
? { environment: record.value.environment }
|
|
794
|
+
: {}),
|
|
765
795
|
}),
|
|
766
796
|
)
|
|
767
797
|
if (replay) {
|
package/src/AcpImage.test.ts
CHANGED
|
@@ -40,8 +40,14 @@ type Message = {
|
|
|
40
40
|
|
|
41
41
|
const capabilities = new AgentExecutor.Capabilities({
|
|
42
42
|
toolsDts: "",
|
|
43
|
-
|
|
43
|
+
environment: new AgentExecutor.ExecutionEnvironment({
|
|
44
|
+
cwd: "/",
|
|
45
|
+
baseline: [],
|
|
46
|
+
seen: [],
|
|
47
|
+
}),
|
|
48
|
+
instructions: Option.none(),
|
|
44
49
|
supportsSearch: false,
|
|
50
|
+
tools: [],
|
|
45
51
|
skills: [],
|
|
46
52
|
})
|
|
47
53
|
|
package/src/Agent.test.ts
CHANGED
|
@@ -18,13 +18,20 @@ import type * as Response from "effect/ai/Response"
|
|
|
18
18
|
import * as ResponseIdTracker from "effect/ai/ResponseIdTracker"
|
|
19
19
|
import * as Agent from "./Agent.ts"
|
|
20
20
|
import * as AgentExecutor from "./AgentExecutor.ts"
|
|
21
|
+
import { subagentsUnavailable } from "./AgentTools.ts"
|
|
21
22
|
import * as AgentOutput from "./AgentOutput.ts"
|
|
22
23
|
import * as Compaction from "./Compaction.ts"
|
|
23
24
|
|
|
24
25
|
const capabilities = new AgentExecutor.Capabilities({
|
|
25
26
|
toolsDts: "",
|
|
26
|
-
|
|
27
|
+
environment: new AgentExecutor.ExecutionEnvironment({
|
|
28
|
+
cwd: "/",
|
|
29
|
+
baseline: [],
|
|
30
|
+
seen: [],
|
|
31
|
+
}),
|
|
32
|
+
instructions: Option.none(),
|
|
27
33
|
supportsSearch: false,
|
|
34
|
+
tools: [],
|
|
28
35
|
skills: [],
|
|
29
36
|
})
|
|
30
37
|
|
|
@@ -114,7 +121,10 @@ describe("Agent", () => {
|
|
|
114
121
|
conversationMode: true,
|
|
115
122
|
executor: makeExecutor(({ onTaskComplete }) =>
|
|
116
123
|
Stream.fromEffect(
|
|
117
|
-
onTaskComplete("done from taskComplete").pipe(
|
|
124
|
+
onTaskComplete("done from taskComplete").pipe(
|
|
125
|
+
Effect.as(""),
|
|
126
|
+
Effect.orDie,
|
|
127
|
+
),
|
|
118
128
|
),
|
|
119
129
|
),
|
|
120
130
|
streamText: () =>
|
|
@@ -162,6 +172,7 @@ const runAgentCollect = (options: {
|
|
|
162
172
|
readonly compaction?: Partial<Compaction.CompactionConfigService> | undefined
|
|
163
173
|
readonly prompt?: string | undefined
|
|
164
174
|
readonly turnTimeout?: Duration.Duration | undefined
|
|
175
|
+
readonly maxRetries?: number | undefined
|
|
165
176
|
readonly respond: ScriptedResponse
|
|
166
177
|
}) =>
|
|
167
178
|
Effect.scoped(
|
|
@@ -243,6 +254,9 @@ const runAgentCollect = (options: {
|
|
|
243
254
|
Agent.TurnTimeout,
|
|
244
255
|
options.turnTimeout ?? Duration.minutes(5),
|
|
245
256
|
),
|
|
257
|
+
options.maxRetries === undefined
|
|
258
|
+
? Layer.empty
|
|
259
|
+
: Agent.MaxRetries.layer(options.maxRetries),
|
|
246
260
|
),
|
|
247
261
|
),
|
|
248
262
|
Effect.exit,
|
|
@@ -467,14 +481,17 @@ it.layer(localExecutorLayer, { excludeTestServices: true })(
|
|
|
467
481
|
AgentExecutor.AgentExecutor.use((executor) =>
|
|
468
482
|
Effect.gen(function* () {
|
|
469
483
|
const summaries: Array<string> = []
|
|
484
|
+
const { environment } = yield* executor.capabilities
|
|
470
485
|
const output = yield* executor
|
|
471
486
|
.execute({
|
|
487
|
+
environment,
|
|
488
|
+
onEnvironment: () => Effect.void,
|
|
472
489
|
script: `try { await ${invocation}; console.log("ACCEPTED") } catch (error) { console.log("REJECTED:", String(error)) } console.log("SCRIPT_SURVIVED")`,
|
|
473
490
|
onTaskComplete: (summary) =>
|
|
474
491
|
Effect.sync(() => {
|
|
475
492
|
summaries.push(summary)
|
|
476
493
|
}),
|
|
477
|
-
|
|
494
|
+
subagents: subagentsUnavailable,
|
|
478
495
|
onImage: () => Effect.void,
|
|
479
496
|
})
|
|
480
497
|
.pipe(Stream.mkString)
|
|
@@ -1164,7 +1181,7 @@ describe("Agent overflow compaction", () => {
|
|
|
1164
1181
|
})
|
|
1165
1182
|
assert.isTrue(Exit.isFailure(exit), "turn must fail")
|
|
1166
1183
|
const error = Option.getOrThrow(Exit.findErrorOption(exit))
|
|
1167
|
-
assert
|
|
1184
|
+
assert(error._tag === "AiError")
|
|
1168
1185
|
assert.strictEqual(error.reason._tag, "InvalidRequestError")
|
|
1169
1186
|
assert.strictEqual(calls.length, 3)
|
|
1170
1187
|
}),
|
|
@@ -1285,3 +1302,91 @@ describe("Agent overflow compaction", () => {
|
|
|
1285
1302
|
}),
|
|
1286
1303
|
)
|
|
1287
1304
|
})
|
|
1305
|
+
|
|
1306
|
+
describe("Agent retry limit", () => {
|
|
1307
|
+
it.live(
|
|
1308
|
+
"fails with RetriesExhausted once model errors and turn timeouts reach MaxRetries",
|
|
1309
|
+
() =>
|
|
1310
|
+
Effect.gen(function* () {
|
|
1311
|
+
const { exit, calls } = yield* runAgentCollect({
|
|
1312
|
+
maxRetries: 2,
|
|
1313
|
+
turnTimeout: Duration.millis(100),
|
|
1314
|
+
respond: (_call, index) => {
|
|
1315
|
+
switch (index) {
|
|
1316
|
+
case 0:
|
|
1317
|
+
return Stream.fail(providerError)
|
|
1318
|
+
case 1:
|
|
1319
|
+
return Stream.fromEffect(Effect.never)
|
|
1320
|
+
case 2:
|
|
1321
|
+
return Stream.fail(providerError)
|
|
1322
|
+
default:
|
|
1323
|
+
return Stream.die(
|
|
1324
|
+
`unexpected model call #${index}: must stop retrying`,
|
|
1325
|
+
)
|
|
1326
|
+
}
|
|
1327
|
+
},
|
|
1328
|
+
}).pipe(Effect.timeout("3 seconds"))
|
|
1329
|
+
assert.strictEqual(calls.length, 3)
|
|
1330
|
+
const error = Option.getOrThrow(Exit.findErrorOption(exit))
|
|
1331
|
+
assert(error._tag === "RetriesExhausted")
|
|
1332
|
+
assert.strictEqual(error.retries, 2)
|
|
1333
|
+
assert.strictEqual(error.error.reason._tag, "InternalProviderError")
|
|
1334
|
+
}),
|
|
1335
|
+
)
|
|
1336
|
+
})
|
|
1337
|
+
|
|
1338
|
+
describe("Agent history events", () => {
|
|
1339
|
+
it.effect(
|
|
1340
|
+
"rebuild the agent's history from the history it was resumed with",
|
|
1341
|
+
() =>
|
|
1342
|
+
Effect.gen(function* () {
|
|
1343
|
+
const { exit, outputs, history } = yield* runAgentCollect({
|
|
1344
|
+
history: fatHistory,
|
|
1345
|
+
compaction: {
|
|
1346
|
+
contextWindow: 12_000,
|
|
1347
|
+
reserveTokens: 2_000,
|
|
1348
|
+
keepRecentTokens: 9_000,
|
|
1349
|
+
},
|
|
1350
|
+
executor: makeExecutor(() => Stream.succeed("listed")),
|
|
1351
|
+
respond: (call, index) => {
|
|
1352
|
+
switch (index) {
|
|
1353
|
+
case 0:
|
|
1354
|
+
assertSummarizerCall(call)
|
|
1355
|
+
return Stream.fromIterable(text("SUMMARY"))
|
|
1356
|
+
case 1:
|
|
1357
|
+
return Stream.fromIterable([
|
|
1358
|
+
toolCall("call-3", "console.log(await ls('.'))"),
|
|
1359
|
+
finish(500),
|
|
1360
|
+
])
|
|
1361
|
+
case 2:
|
|
1362
|
+
return Stream.fromIterable([
|
|
1363
|
+
...text("done"),
|
|
1364
|
+
finish(600, "stop"),
|
|
1365
|
+
])
|
|
1366
|
+
default:
|
|
1367
|
+
return Stream.die(`unexpected model call #${index}`)
|
|
1368
|
+
}
|
|
1369
|
+
},
|
|
1370
|
+
})
|
|
1371
|
+
assert.deepStrictEqual(exit, Exit.succeed("done"))
|
|
1372
|
+
|
|
1373
|
+
let replayed: Array<Prompt.Message> = [...fatHistory.content]
|
|
1374
|
+
for (const output of outputs) {
|
|
1375
|
+
if (output._tag === "HistoryAppended") {
|
|
1376
|
+
replayed.push(...output.messages)
|
|
1377
|
+
} else if (output._tag === "HistoryReplaced") {
|
|
1378
|
+
replayed = [...output.messages]
|
|
1379
|
+
}
|
|
1380
|
+
}
|
|
1381
|
+
assert.strictEqual(outputsOfTag(outputs, "HistoryReplaced").length, 1)
|
|
1382
|
+
assert.strictEqual(
|
|
1383
|
+
promptJson(Prompt.fromMessages(replayed)),
|
|
1384
|
+
promptJson(
|
|
1385
|
+
Prompt.fromMessages(
|
|
1386
|
+
history.content.filter((message) => message.role !== "system"),
|
|
1387
|
+
),
|
|
1388
|
+
),
|
|
1389
|
+
)
|
|
1390
|
+
}),
|
|
1391
|
+
)
|
|
1392
|
+
})
|