clanka 0.24.2 → 0.24.4
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/Agent.d.ts.map +1 -1
- package/dist/Agent.js +23 -10
- package/dist/Agent.js.map +1 -1
- package/dist/Agent.test.js +137 -0
- package/dist/Agent.test.js.map +1 -1
- package/dist/ClaudeAgent.d.ts.map +1 -1
- package/dist/ClaudeAgent.js +5 -1
- package/dist/ClaudeAgent.js.map +1 -1
- package/dist/ClaudeAgent.test.js +45 -0
- package/dist/ClaudeAgent.test.js.map +1 -1
- package/dist/OutputFormatter.d.ts +1 -1
- package/dist/OutputFormatter.d.ts.map +1 -1
- package/dist/OutputFormatter.js +42 -2
- package/dist/OutputFormatter.js.map +1 -1
- package/dist/OutputFormatter.test.d.ts +2 -0
- package/dist/OutputFormatter.test.d.ts.map +1 -0
- package/dist/OutputFormatter.test.js +93 -0
- package/dist/OutputFormatter.test.js.map +1 -0
- package/dist/bin/{Stream-C51usiew.mjs → AiError-fiHIiGOy.mjs} +469 -34
- package/dist/bin/{Latch-BnrpWMd1.mjs → Latch-D1909CI6.mjs} +1 -1
- package/dist/bin/{McpClient-DoPNaNzW.mjs → McpClient-CXwyRd_3.mjs} +2 -3
- package/dist/bin/{OutputFormatter-U02YKTfy.mjs → OutputFormatter-B8yF7iiq.mjs} +40 -4
- package/dist/bin/{Schema-Box_9VZG.mjs → Schema-D629S5yu.mjs} +5459 -278
- package/dist/bin/{SemanticSearch-CH125AHH.mjs → SemanticSearch-BlcfFvPr.mjs} +14 -15
- package/dist/bin/cli.mjs +393 -819
- package/dist/bin/{upgrade-HMRGSqq8.mjs → upgrade-E_f-q2h8.mjs} +3 -4
- package/package.json +1 -1
- package/src/Agent.test.ts +198 -0
- package/src/Agent.ts +27 -10
- package/src/ClaudeAgent.test.ts +62 -0
- package/src/ClaudeAgent.ts +7 -1
- package/src/OutputFormatter.test.ts +158 -0
- package/src/OutputFormatter.ts +56 -1
- package/dist/bin/Effect-5XYMLOiB.mjs +0 -5183
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { n as makeUnsafe } from "./Latch-
|
|
4
|
-
import { ei as value } from "./Schema-Box_9VZG.mjs";
|
|
1
|
+
import { $a as timeoutOrElse, Bs as addFinalizer, Dl as map, Ka as suspend, La as scope, Ld as identity, Ni as fail, Ua as succeed, Xi as gen, _u as merge, ao as updateContext, co as void_, do as withFiber, ei as value, li as as, mi as callback, ni as acquireRelease, pl as ensure, qa as sync, qs as provide } from "./Schema-D629S5yu.mjs";
|
|
2
|
+
import { C as make, S as isCloseEvent, _ as SocketError, b as SocketUpgradeError, g as SocketCloseError, v as SocketOpenError, x as SocketWriteError, y as SocketReadError } from "./cli.mjs";
|
|
3
|
+
import { n as makeUnsafe } from "./Latch-D1909CI6.mjs";
|
|
5
4
|
import { Readable, finished } from "node:stream";
|
|
6
5
|
import { Buffer as Buffer$1 } from "node:buffer";
|
|
7
6
|
import * as Tls from "node:tls";
|
package/package.json
CHANGED
package/src/Agent.test.ts
CHANGED
|
@@ -21,6 +21,8 @@ import * as Agent from "./Agent.ts"
|
|
|
21
21
|
import * as AgentExecutor from "./AgentExecutor.ts"
|
|
22
22
|
import { agentTasksUnavailable } from "./AgentTools.ts"
|
|
23
23
|
import * as AgentOutput from "./AgentOutput.ts"
|
|
24
|
+
import * as OutputFormatter from "./OutputFormatter.ts"
|
|
25
|
+
import { stripVTControlCharacters } from "node:util"
|
|
24
26
|
import * as Compaction from "./Compaction.ts"
|
|
25
27
|
|
|
26
28
|
const capabilities = new AgentExecutor.Capabilities({
|
|
@@ -621,6 +623,202 @@ it.layer(localExecutorLayer, { excludeTestServices: true })(
|
|
|
621
623
|
// --- tests -------------------------------------------------------------------
|
|
622
624
|
|
|
623
625
|
describe("Agent usage", () => {
|
|
626
|
+
for (const background of [false, true]) {
|
|
627
|
+
it.effect(
|
|
628
|
+
`aggregates nested subagent usage once (background: ${background})`,
|
|
629
|
+
() =>
|
|
630
|
+
Effect.gen(function* () {
|
|
631
|
+
const { exit, outputs } = yield* runAgentCollect({
|
|
632
|
+
executor: makeExecutor(({ script, environment, agentTasks }) =>
|
|
633
|
+
Stream.fromEffect(
|
|
634
|
+
Effect.gen(function* () {
|
|
635
|
+
const task = yield* agentTasks.subagent({
|
|
636
|
+
id: script,
|
|
637
|
+
prompt: "nested task",
|
|
638
|
+
background,
|
|
639
|
+
environment: new AgentExecutor.EnvironmentUpdate({
|
|
640
|
+
environment,
|
|
641
|
+
update: Option.none(),
|
|
642
|
+
}),
|
|
643
|
+
})
|
|
644
|
+
yield* agentTasks.waitTasks([task.id], { timeoutMs: 60_000 })
|
|
645
|
+
// Reading completion again must not add the child's usage again.
|
|
646
|
+
yield* agentTasks.taskOutput(task.id)
|
|
647
|
+
if (script === "1") {
|
|
648
|
+
yield* agentTasks.messageAgent({
|
|
649
|
+
id: task.id,
|
|
650
|
+
message: "follow up",
|
|
651
|
+
})
|
|
652
|
+
yield* agentTasks.waitTasks([task.id], {
|
|
653
|
+
timeoutMs: 60_000,
|
|
654
|
+
})
|
|
655
|
+
}
|
|
656
|
+
return "done"
|
|
657
|
+
}).pipe(Effect.orDie),
|
|
658
|
+
),
|
|
659
|
+
),
|
|
660
|
+
respond: (_call, index) => {
|
|
661
|
+
switch (index) {
|
|
662
|
+
case 0:
|
|
663
|
+
return Stream.make(
|
|
664
|
+
toolCall("root", "1"),
|
|
665
|
+
finish(100, "tool-calls", { read: 10, write: 1 }),
|
|
666
|
+
)
|
|
667
|
+
case 1:
|
|
668
|
+
return Stream.make(
|
|
669
|
+
toolCall("child", "2"),
|
|
670
|
+
finish(200, "tool-calls", { read: 20, write: 2 }),
|
|
671
|
+
)
|
|
672
|
+
case 2:
|
|
673
|
+
return Stream.fromIterable([
|
|
674
|
+
...text("grandchild"),
|
|
675
|
+
finish(300, "stop", { read: 30, write: 3 }),
|
|
676
|
+
])
|
|
677
|
+
case 3:
|
|
678
|
+
return Stream.fromIterable([
|
|
679
|
+
...text("child"),
|
|
680
|
+
finish(400, "stop", { read: 40, write: 4 }),
|
|
681
|
+
])
|
|
682
|
+
case 4:
|
|
683
|
+
return Stream.fromIterable([
|
|
684
|
+
...text("resumed child"),
|
|
685
|
+
finish(50, "stop", { read: 5, write: 1 }),
|
|
686
|
+
])
|
|
687
|
+
case 5:
|
|
688
|
+
return Stream.fromIterable([
|
|
689
|
+
...text("complete"),
|
|
690
|
+
finish(500, "stop", { read: 50, write: 5 }),
|
|
691
|
+
])
|
|
692
|
+
default:
|
|
693
|
+
return Stream.die(`unexpected call ${index}`)
|
|
694
|
+
}
|
|
695
|
+
},
|
|
696
|
+
})
|
|
697
|
+
assert.deepStrictEqual(exit, Exit.succeed("complete"))
|
|
698
|
+
const usage = outputsOfTag(outputs, "Usage")
|
|
699
|
+
// Child context windows must never replace the parent's window.
|
|
700
|
+
assert.deepStrictEqual(
|
|
701
|
+
usage.map((part) => [part.inputTokens, part.contextTokens]),
|
|
702
|
+
[
|
|
703
|
+
[300, 0],
|
|
704
|
+
[500, 0],
|
|
705
|
+
[900, 0],
|
|
706
|
+
[950, 0],
|
|
707
|
+
[1050, 100],
|
|
708
|
+
[1550, 500],
|
|
709
|
+
],
|
|
710
|
+
)
|
|
711
|
+
assert.deepStrictEqual(
|
|
712
|
+
usage.at(-1),
|
|
713
|
+
new AgentOutput.Usage({
|
|
714
|
+
contextTokens: 500,
|
|
715
|
+
inputTokens: 1550,
|
|
716
|
+
outputTokens: 60,
|
|
717
|
+
cacheRead: 155,
|
|
718
|
+
cacheWrite: 16,
|
|
719
|
+
}),
|
|
720
|
+
)
|
|
721
|
+
}),
|
|
722
|
+
)
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
it.effect(
|
|
726
|
+
"keeps background sibling usage out of the foreground output buffer",
|
|
727
|
+
() =>
|
|
728
|
+
Effect.gen(function* () {
|
|
729
|
+
const reasoningStarted = yield* Deferred.make<void>()
|
|
730
|
+
const backgroundDone = yield* Deferred.make<void>()
|
|
731
|
+
// B completes while A holds the display lock, before A ends its reasoning.
|
|
732
|
+
const { exit, outputs } = yield* runAgentCollect({
|
|
733
|
+
executor: makeExecutor(({ environment, agentTasks }) =>
|
|
734
|
+
Stream.fromEffect(
|
|
735
|
+
Effect.gen(function* () {
|
|
736
|
+
const update = new AgentExecutor.EnvironmentUpdate({
|
|
737
|
+
environment,
|
|
738
|
+
update: Option.none(),
|
|
739
|
+
})
|
|
740
|
+
const background = yield* agentTasks.subagent({
|
|
741
|
+
id: "1",
|
|
742
|
+
prompt: "sibling-b",
|
|
743
|
+
background: true,
|
|
744
|
+
environment: update,
|
|
745
|
+
})
|
|
746
|
+
yield* agentTasks
|
|
747
|
+
.waitTasks([background.id], { timeoutMs: 60_000 })
|
|
748
|
+
.pipe(
|
|
749
|
+
Effect.andThen(Deferred.done(backgroundDone, Exit.void)),
|
|
750
|
+
Effect.forkScoped,
|
|
751
|
+
)
|
|
752
|
+
yield* agentTasks.subagent({
|
|
753
|
+
id: "2",
|
|
754
|
+
prompt: "sibling-a",
|
|
755
|
+
background: false,
|
|
756
|
+
environment: update,
|
|
757
|
+
})
|
|
758
|
+
return "done"
|
|
759
|
+
}).pipe(Effect.scoped, Effect.orDie),
|
|
760
|
+
),
|
|
761
|
+
),
|
|
762
|
+
respond: (_call, index) => {
|
|
763
|
+
if (index === 0) {
|
|
764
|
+
return Stream.make(toolCall("root", "siblings"), finish(100))
|
|
765
|
+
}
|
|
766
|
+
if (index === 1) {
|
|
767
|
+
return Stream.fromEffect(Deferred.await(reasoningStarted)).pipe(
|
|
768
|
+
Stream.flatMap(() =>
|
|
769
|
+
Stream.fromIterable([...text("b done"), finish(300, "stop")]),
|
|
770
|
+
),
|
|
771
|
+
)
|
|
772
|
+
}
|
|
773
|
+
if (index === 2) {
|
|
774
|
+
const [start, delta, end] = reasoning("thinking")
|
|
775
|
+
return Stream.make(start!, delta!).pipe(
|
|
776
|
+
Stream.concat(
|
|
777
|
+
Stream.fromEffect(
|
|
778
|
+
Deferred.done(reasoningStarted, Exit.void).pipe(
|
|
779
|
+
Effect.andThen(Deferred.await(backgroundDone)),
|
|
780
|
+
Effect.as(end!),
|
|
781
|
+
),
|
|
782
|
+
),
|
|
783
|
+
),
|
|
784
|
+
Stream.concat(
|
|
785
|
+
Stream.fromIterable([...text("a done"), finish(200, "stop")]),
|
|
786
|
+
),
|
|
787
|
+
)
|
|
788
|
+
}
|
|
789
|
+
return Stream.fromIterable([
|
|
790
|
+
...text("complete"),
|
|
791
|
+
finish(400, "stop"),
|
|
792
|
+
])
|
|
793
|
+
},
|
|
794
|
+
})
|
|
795
|
+
assert.deepStrictEqual(exit, Exit.succeed("complete"))
|
|
796
|
+
const usage = outputsOfTag(outputs, "Usage")
|
|
797
|
+
assert.deepStrictEqual(
|
|
798
|
+
usage.map((part) => [part.inputTokens, part.contextTokens]),
|
|
799
|
+
[
|
|
800
|
+
[300, 0],
|
|
801
|
+
[500, 0],
|
|
802
|
+
[600, 100],
|
|
803
|
+
[1000, 400],
|
|
804
|
+
],
|
|
805
|
+
)
|
|
806
|
+
assert.strictEqual(usage.at(-1)?.outputTokens, 40)
|
|
807
|
+
const formatted = yield* Stream.fromIterable(outputs).pipe(
|
|
808
|
+
OutputFormatter.pretty(),
|
|
809
|
+
Stream.runCollect,
|
|
810
|
+
)
|
|
811
|
+
assert.include(
|
|
812
|
+
stripVTControlCharacters(formatted.join("")),
|
|
813
|
+
"Thinking: thinking\n\n",
|
|
814
|
+
)
|
|
815
|
+
assert.include(
|
|
816
|
+
stripVTControlCharacters(formatted.join("")),
|
|
817
|
+
"Usage: 0 context / 300 input / 10 output",
|
|
818
|
+
)
|
|
819
|
+
}),
|
|
820
|
+
)
|
|
821
|
+
|
|
624
822
|
it.effect("accumulates provider cache reads and writes", () =>
|
|
625
823
|
Effect.gen(function* () {
|
|
626
824
|
const { exit, outputs } = yield* runAgentCollect({
|
package/src/Agent.ts
CHANGED
|
@@ -338,6 +338,19 @@ export const makeWith = Effect.fnUntraced(function* <
|
|
|
338
338
|
let outputTokens = 0
|
|
339
339
|
let cacheRead = 0
|
|
340
340
|
let cacheWrite = 0
|
|
341
|
+
// Never through the display buffer: it is flushed into the stream of
|
|
342
|
+
// whichever agent releases it, where these totals would count as its own.
|
|
343
|
+
const sendUsage = () =>
|
|
344
|
+
Queue.offerUnsafe(
|
|
345
|
+
output,
|
|
346
|
+
new Usage({
|
|
347
|
+
contextTokens: lastContextTokens ?? 0,
|
|
348
|
+
inputTokens,
|
|
349
|
+
outputTokens,
|
|
350
|
+
cacheRead,
|
|
351
|
+
cacheWrite,
|
|
352
|
+
}),
|
|
353
|
+
)
|
|
341
354
|
const prompt = opts.history
|
|
342
355
|
const mailbox = opts.mailbox
|
|
343
356
|
|
|
@@ -533,6 +546,8 @@ export const makeWith = Effect.fnUntraced(function* <
|
|
|
533
546
|
}) {
|
|
534
547
|
const { child } = options
|
|
535
548
|
const turn = child.turn
|
|
549
|
+
// Each spawn starts fresh counters, including when a child is resumed.
|
|
550
|
+
let childUsage: Usage | undefined
|
|
536
551
|
const display = opts.display && !options.background
|
|
537
552
|
const stream = spawn({
|
|
538
553
|
agentId: child.id,
|
|
@@ -561,6 +576,17 @@ export const makeWith = Effect.fnUntraced(function* <
|
|
|
561
576
|
return yield* stream.pipe(
|
|
562
577
|
Stream.runForEachArray((parts) => {
|
|
563
578
|
for (const part of parts) {
|
|
579
|
+
// Only direct Usage snapshots contribute: nested SubagentParts
|
|
580
|
+
// are display events already included in the child's totals.
|
|
581
|
+
if (part._tag === "Usage") {
|
|
582
|
+
inputTokens += part.inputTokens - (childUsage?.inputTokens ?? 0)
|
|
583
|
+
outputTokens +=
|
|
584
|
+
part.outputTokens - (childUsage?.outputTokens ?? 0)
|
|
585
|
+
cacheRead += part.cacheRead - (childUsage?.cacheRead ?? 0)
|
|
586
|
+
cacheWrite += part.cacheWrite - (childUsage?.cacheWrite ?? 0)
|
|
587
|
+
childUsage = part
|
|
588
|
+
sendUsage()
|
|
589
|
+
}
|
|
564
590
|
switch (part._tag) {
|
|
565
591
|
case "AgentStart":
|
|
566
592
|
case "HistoryAppended":
|
|
@@ -1304,16 +1330,7 @@ ${prompt}`,
|
|
|
1304
1330
|
if (usage.inputTokens.total !== undefined) {
|
|
1305
1331
|
lastContextTokens = usage.inputTokens.total
|
|
1306
1332
|
inputTokens += usage.inputTokens.total
|
|
1307
|
-
|
|
1308
|
-
agentId,
|
|
1309
|
-
part: new Usage({
|
|
1310
|
-
contextTokens: usage.inputTokens.total,
|
|
1311
|
-
inputTokens,
|
|
1312
|
-
outputTokens,
|
|
1313
|
-
cacheRead,
|
|
1314
|
-
cacheWrite,
|
|
1315
|
-
}),
|
|
1316
|
-
})
|
|
1333
|
+
sendUsage()
|
|
1317
1334
|
}
|
|
1318
1335
|
break
|
|
1319
1336
|
case "tool-call":
|
package/src/ClaudeAgent.test.ts
CHANGED
|
@@ -250,6 +250,68 @@ describe("ClaudeAgent", () => {
|
|
|
250
250
|
)
|
|
251
251
|
})
|
|
252
252
|
|
|
253
|
+
it.effect("classifies credential cooldowns as retryable rate limits", () =>
|
|
254
|
+
Effect.gen(function* () {
|
|
255
|
+
const description =
|
|
256
|
+
"API Error: Request rejected (429) · All credentials for model claude-fable-5-2 are cooling down (last error: rate_limit_error: This request would exceed your account's rate limit. Please try again later.) (reset after 29m52s)"
|
|
257
|
+
for (const result of [
|
|
258
|
+
{ subtype: "success", is_error: true, result: description },
|
|
259
|
+
{ subtype: "error_during_execution", errors: [description] },
|
|
260
|
+
{
|
|
261
|
+
subtype: "success",
|
|
262
|
+
is_error: true,
|
|
263
|
+
result: "API Error: Request rejected (429)",
|
|
264
|
+
},
|
|
265
|
+
{
|
|
266
|
+
subtype: "success",
|
|
267
|
+
is_error: true,
|
|
268
|
+
result: "rate_limit_error: Please try again later.",
|
|
269
|
+
},
|
|
270
|
+
{
|
|
271
|
+
subtype: "success",
|
|
272
|
+
is_error: true,
|
|
273
|
+
result: "Too many requests",
|
|
274
|
+
api_error_status: 429,
|
|
275
|
+
},
|
|
276
|
+
]) {
|
|
277
|
+
const error = yield* Stream.fromIterable([
|
|
278
|
+
{ ...frame, type: "result", ...result },
|
|
279
|
+
] as Array<any>).pipe(
|
|
280
|
+
ClaudeAgent.toStreamParts,
|
|
281
|
+
Stream.runCollect,
|
|
282
|
+
Effect.flip,
|
|
283
|
+
)
|
|
284
|
+
assert.strictEqual(error.reason._tag, "RateLimitError")
|
|
285
|
+
if (error.reason._tag !== "RateLimitError") return
|
|
286
|
+
assert.isTrue(error.isRetryable)
|
|
287
|
+
assert.strictEqual(
|
|
288
|
+
error.reason.metadata.description,
|
|
289
|
+
"result" in result ? result.result : description,
|
|
290
|
+
)
|
|
291
|
+
}
|
|
292
|
+
}),
|
|
293
|
+
)
|
|
294
|
+
|
|
295
|
+
it.effect("keeps unrelated request rejections non-retryable", () =>
|
|
296
|
+
Effect.gen(function* () {
|
|
297
|
+
const error = yield* Stream.fromIterable([
|
|
298
|
+
{
|
|
299
|
+
...frame,
|
|
300
|
+
type: "result",
|
|
301
|
+
subtype: "success",
|
|
302
|
+
is_error: true,
|
|
303
|
+
result: "API Error: Request rejected (403)",
|
|
304
|
+
api_error_status: 403,
|
|
305
|
+
},
|
|
306
|
+
] as Array<any>).pipe(
|
|
307
|
+
ClaudeAgent.toStreamParts,
|
|
308
|
+
Stream.runCollect,
|
|
309
|
+
Effect.flip,
|
|
310
|
+
)
|
|
311
|
+
assert.isFalse(error.isRetryable)
|
|
312
|
+
}),
|
|
313
|
+
)
|
|
314
|
+
|
|
253
315
|
it.effect("converts a tool step to response parts", () =>
|
|
254
316
|
Effect.gen(function* () {
|
|
255
317
|
const parts = yield* Stream.fromIterable(toolStep as Array<any>).pipe(
|
package/src/ClaudeAgent.ts
CHANGED
|
@@ -344,7 +344,13 @@ const resultError = (
|
|
|
344
344
|
case "invalid_request":
|
|
345
345
|
return aiError(new AiError.InvalidRequestError({ description }))
|
|
346
346
|
}
|
|
347
|
-
//
|
|
347
|
+
// Credential cooldowns can outlast Claude Code's own retries.
|
|
348
|
+
if (
|
|
349
|
+
status === 429 ||
|
|
350
|
+
/Request rejected \(429\)|\brate_limit_error\b/i.test(description)
|
|
351
|
+
) {
|
|
352
|
+
return aiError(new AiError.RateLimitError({ metadata: { description } }))
|
|
353
|
+
}
|
|
348
354
|
return aiError(
|
|
349
355
|
/prompt is too long/i.test(description)
|
|
350
356
|
? new AiError.InvalidRequestError({ description })
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { assert, describe, it } from "@effect/vitest"
|
|
2
|
+
import * as Cause from "effect/Cause"
|
|
3
|
+
import * as Effect from "effect/Effect"
|
|
4
|
+
import * as Fiber from "effect/Fiber"
|
|
5
|
+
import * as Latch from "effect/Latch"
|
|
6
|
+
import * as Stream from "effect/Stream"
|
|
7
|
+
import * as AgentOutput from "./AgentOutput.ts"
|
|
8
|
+
import { layerMuxer, Muxer, pretty } from "./OutputFormatter.ts"
|
|
9
|
+
|
|
10
|
+
describe("layerMuxer", () => {
|
|
11
|
+
for (const block of ["Reasoning", "Script"] as const) {
|
|
12
|
+
it.effect(
|
|
13
|
+
"keeps concurrent output outside a " + block + " block containing Usage",
|
|
14
|
+
() =>
|
|
15
|
+
Effect.gen(function* () {
|
|
16
|
+
const muxer = yield* Muxer
|
|
17
|
+
const paused = yield* Latch.make()
|
|
18
|
+
const resume = yield* Latch.make()
|
|
19
|
+
const competing = yield* Latch.make()
|
|
20
|
+
const collected = yield* muxer.output.pipe(
|
|
21
|
+
Stream.take(6),
|
|
22
|
+
Stream.runCollect,
|
|
23
|
+
Effect.forkChild({ startImmediately: true }),
|
|
24
|
+
)
|
|
25
|
+
const start =
|
|
26
|
+
block === "Reasoning"
|
|
27
|
+
? new AgentOutput.ReasoningStart()
|
|
28
|
+
: new AgentOutput.ScriptStart()
|
|
29
|
+
const delta =
|
|
30
|
+
block === "Reasoning"
|
|
31
|
+
? new AgentOutput.ReasoningDelta({ delta: "text" })
|
|
32
|
+
: new AgentOutput.ScriptDelta({ delta: "text" })
|
|
33
|
+
const end =
|
|
34
|
+
block === "Reasoning"
|
|
35
|
+
? new AgentOutput.ReasoningEnd()
|
|
36
|
+
: new AgentOutput.ScriptEnd()
|
|
37
|
+
yield* muxer.add(
|
|
38
|
+
Stream.make(
|
|
39
|
+
start,
|
|
40
|
+
delta,
|
|
41
|
+
new AgentOutput.Usage({
|
|
42
|
+
contextTokens: 10,
|
|
43
|
+
inputTokens: 10,
|
|
44
|
+
outputTokens: 2,
|
|
45
|
+
cacheRead: 0,
|
|
46
|
+
cacheWrite: 0,
|
|
47
|
+
}),
|
|
48
|
+
).pipe(
|
|
49
|
+
Stream.concat(
|
|
50
|
+
Stream.fromEffect(
|
|
51
|
+
paused.open.pipe(
|
|
52
|
+
Effect.andThen(resume.await),
|
|
53
|
+
Effect.as(delta),
|
|
54
|
+
),
|
|
55
|
+
),
|
|
56
|
+
),
|
|
57
|
+
Stream.concat(Stream.succeed(end)),
|
|
58
|
+
),
|
|
59
|
+
)
|
|
60
|
+
yield* paused.await
|
|
61
|
+
yield* muxer.add(
|
|
62
|
+
Stream.fromEffect(
|
|
63
|
+
competing.open.pipe(
|
|
64
|
+
Effect.as(
|
|
65
|
+
new AgentOutput.ScriptOutput({ output: "other agent" }),
|
|
66
|
+
),
|
|
67
|
+
),
|
|
68
|
+
),
|
|
69
|
+
)
|
|
70
|
+
yield* competing.await
|
|
71
|
+
yield* Effect.yieldNow
|
|
72
|
+
yield* resume.open
|
|
73
|
+
const output = yield* Fiber.join(collected)
|
|
74
|
+
assert.deepStrictEqual(output.slice(0, 4), [
|
|
75
|
+
block + "Start",
|
|
76
|
+
block + "Delta",
|
|
77
|
+
block + "Delta",
|
|
78
|
+
block + "End",
|
|
79
|
+
])
|
|
80
|
+
assert.deepStrictEqual(output.slice(4).toSorted(), [
|
|
81
|
+
"ScriptOutput",
|
|
82
|
+
"Usage",
|
|
83
|
+
])
|
|
84
|
+
}).pipe(
|
|
85
|
+
Effect.provide(
|
|
86
|
+
layerMuxer((stream) =>
|
|
87
|
+
stream.pipe(
|
|
88
|
+
Stream.map((part) => part._tag),
|
|
89
|
+
Stream.orDie,
|
|
90
|
+
),
|
|
91
|
+
),
|
|
92
|
+
),
|
|
93
|
+
),
|
|
94
|
+
)
|
|
95
|
+
}
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
describe("pretty", () => {
|
|
99
|
+
for (const block of ["Reasoning", "Script"] as const) {
|
|
100
|
+
for (const termination of ["end", "eof", "finished", "error"] as const) {
|
|
101
|
+
it.effect(block + " defers Usage until " + termination, () =>
|
|
102
|
+
Effect.gen(function* () {
|
|
103
|
+
const start =
|
|
104
|
+
block === "Reasoning"
|
|
105
|
+
? new AgentOutput.ReasoningStart()
|
|
106
|
+
: new AgentOutput.ScriptStart()
|
|
107
|
+
const delta =
|
|
108
|
+
block === "Reasoning"
|
|
109
|
+
? new AgentOutput.ReasoningDelta({ delta: "before" })
|
|
110
|
+
: new AgentOutput.ScriptDelta({ delta: "before" })
|
|
111
|
+
const end =
|
|
112
|
+
block === "Reasoning"
|
|
113
|
+
? new AgentOutput.ReasoningEnd()
|
|
114
|
+
: new AgentOutput.ScriptEnd()
|
|
115
|
+
const tail: Stream.Stream<
|
|
116
|
+
AgentOutput.Output,
|
|
117
|
+
AgentOutput.AgentFinished | string
|
|
118
|
+
> =
|
|
119
|
+
termination === "end"
|
|
120
|
+
? Stream.succeed(end)
|
|
121
|
+
: termination === "eof"
|
|
122
|
+
? Stream.empty
|
|
123
|
+
: termination === "finished"
|
|
124
|
+
? Stream.fail(
|
|
125
|
+
new AgentOutput.AgentFinished({ summary: "done" }),
|
|
126
|
+
)
|
|
127
|
+
: Stream.fail("failure")
|
|
128
|
+
const output = yield* Stream.make(
|
|
129
|
+
start,
|
|
130
|
+
delta,
|
|
131
|
+
new AgentOutput.Usage({
|
|
132
|
+
contextTokens: 10,
|
|
133
|
+
inputTokens: 10,
|
|
134
|
+
outputTokens: 2,
|
|
135
|
+
cacheRead: 0,
|
|
136
|
+
cacheWrite: 0,
|
|
137
|
+
}),
|
|
138
|
+
block === "Reasoning"
|
|
139
|
+
? new AgentOutput.ReasoningDelta({ delta: "after" })
|
|
140
|
+
: new AgentOutput.ScriptDelta({ delta: "after" }),
|
|
141
|
+
).pipe(
|
|
142
|
+
Stream.concat(tail),
|
|
143
|
+
pretty(),
|
|
144
|
+
Stream.catchCause((cause) => Stream.succeed(Cause.pretty(cause))),
|
|
145
|
+
Stream.runCollect,
|
|
146
|
+
)
|
|
147
|
+
assert.include(output[2], "after")
|
|
148
|
+
assert.strictEqual(output[3], "\n\n")
|
|
149
|
+
assert.include(output[4], "Usage:")
|
|
150
|
+
assert.include(output[4], "10 context / 10 input / 2 output")
|
|
151
|
+
if (termination === "finished")
|
|
152
|
+
assert.include(output[5], "Task complete:")
|
|
153
|
+
if (termination === "error") assert.include(output[5], "failure")
|
|
154
|
+
}),
|
|
155
|
+
)
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
})
|
package/src/OutputFormatter.ts
CHANGED
|
@@ -7,7 +7,12 @@ import * as PubSub from "effect/PubSub"
|
|
|
7
7
|
import * as Semaphore from "effect/Semaphore"
|
|
8
8
|
import * as Context from "effect/Context"
|
|
9
9
|
import * as Stream from "effect/Stream"
|
|
10
|
-
import
|
|
10
|
+
import {
|
|
11
|
+
type AgentFinished,
|
|
12
|
+
type Output,
|
|
13
|
+
ReasoningEnd,
|
|
14
|
+
ScriptEnd,
|
|
15
|
+
} from "./AgentOutput.ts"
|
|
11
16
|
import chalk from "chalk"
|
|
12
17
|
import type * as Prompt from "effect/ai/Prompt"
|
|
13
18
|
import * as Cause from "effect/Cause"
|
|
@@ -49,6 +54,7 @@ export const pretty = (options?: {
|
|
|
49
54
|
: identity
|
|
50
55
|
return (stream) =>
|
|
51
56
|
stream.pipe(
|
|
57
|
+
deferBlockOutput,
|
|
52
58
|
Stream.map((output) => {
|
|
53
59
|
let prefix = ""
|
|
54
60
|
if (output._tag === "SubagentPart") {
|
|
@@ -147,6 +153,54 @@ ${chalk.dim(output.summary)}\n\n`
|
|
|
147
153
|
)
|
|
148
154
|
}
|
|
149
155
|
|
|
156
|
+
// Out-of-band updates stay live upstream; presentation waits for an open block.
|
|
157
|
+
const deferBlockOutput = <E, R>(stream: Stream.Stream<Output, E, R>) =>
|
|
158
|
+
Stream.suspend(() => {
|
|
159
|
+
let end: ReasoningEnd | ScriptEnd | undefined
|
|
160
|
+
let pending: Array<Output> = []
|
|
161
|
+
const flush = () => {
|
|
162
|
+
const parts = pending
|
|
163
|
+
pending = []
|
|
164
|
+
return parts
|
|
165
|
+
}
|
|
166
|
+
const finish = () => {
|
|
167
|
+
const parts = pending.length > 0 && end ? [end, ...flush()] : flush()
|
|
168
|
+
end = undefined
|
|
169
|
+
return Stream.fromIterable(parts)
|
|
170
|
+
}
|
|
171
|
+
return stream.pipe(
|
|
172
|
+
Stream.map((output): Array<Output> => {
|
|
173
|
+
const part = output._tag === "SubagentPart" ? output.part : output
|
|
174
|
+
switch (part._tag) {
|
|
175
|
+
case "ReasoningStart":
|
|
176
|
+
end = new ReasoningEnd()
|
|
177
|
+
break
|
|
178
|
+
case "ScriptStart":
|
|
179
|
+
end = new ScriptEnd()
|
|
180
|
+
break
|
|
181
|
+
case "ReasoningEnd":
|
|
182
|
+
case "ScriptEnd":
|
|
183
|
+
end = undefined
|
|
184
|
+
return [output, ...flush()]
|
|
185
|
+
case "ReasoningDelta":
|
|
186
|
+
case "ScriptDelta":
|
|
187
|
+
break
|
|
188
|
+
default:
|
|
189
|
+
if (end) {
|
|
190
|
+
pending.push(output)
|
|
191
|
+
return []
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
return [output]
|
|
195
|
+
}),
|
|
196
|
+
Stream.flattenIterable,
|
|
197
|
+
Stream.catchCause((cause) =>
|
|
198
|
+
finish().pipe(Stream.concat(Stream.failCause(cause))),
|
|
199
|
+
),
|
|
200
|
+
Stream.concat(Stream.suspend(finish)),
|
|
201
|
+
)
|
|
202
|
+
})
|
|
203
|
+
|
|
150
204
|
const promptToString = (prompt: Prompt.Prompt): string => {
|
|
151
205
|
let textParts: Array<string> = []
|
|
152
206
|
for (const message of prompt.content) {
|
|
@@ -206,6 +260,7 @@ export const layerMuxer = (formatter: OutputFormatter) =>
|
|
|
206
260
|
add(stream) {
|
|
207
261
|
const id = ++agentCount
|
|
208
262
|
return stream.pipe(
|
|
263
|
+
deferBlockOutput,
|
|
209
264
|
Stream.tap(
|
|
210
265
|
Effect.fnUntraced(function* (part_) {
|
|
211
266
|
if (currentAgentId === null || id !== currentAgentId) {
|