clanka 0.17.1 → 0.17.3
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 +4 -0
- package/dist/Agent.d.ts.map +1 -1
- package/dist/Agent.js +5 -1
- package/dist/Agent.js.map +1 -1
- package/dist/Agent.test.js +35 -0
- package/dist/Agent.test.js.map +1 -1
- package/dist/ClaudeAgent.d.ts.map +1 -1
- package/dist/ClaudeAgent.js +93 -26
- package/dist/ClaudeAgent.js.map +1 -1
- package/dist/ClaudeAgent.test.js +191 -19
- package/dist/ClaudeAgent.test.js.map +1 -1
- package/dist/bin/cli.mjs +51 -20
- package/dist/bin/cli.mjs.map +1 -1
- package/package.json +1 -1
- package/src/Agent.test.ts +69 -0
- package/src/Agent.ts +5 -1
- package/src/ClaudeAgent.test.ts +277 -24
- package/src/ClaudeAgent.ts +122 -34
package/src/ClaudeAgent.ts
CHANGED
|
@@ -372,6 +372,7 @@ const makeConverter = () => {
|
|
|
372
372
|
switch (event.type) {
|
|
373
373
|
case "message_start":
|
|
374
374
|
messageId = event.message.id
|
|
375
|
+
calledTools = false
|
|
375
376
|
streamed.add(messageId)
|
|
376
377
|
usage.update(messageId, event.message.usage)
|
|
377
378
|
break
|
|
@@ -478,6 +479,24 @@ const makeConverter = () => {
|
|
|
478
479
|
reason: message.stop_reason === "max_tokens" ? "length" : "stop",
|
|
479
480
|
usage: usage.snapshot(),
|
|
480
481
|
})
|
|
482
|
+
} else if (message.type === "system" && message.subtype === "api_retry") {
|
|
483
|
+
// Logged so a long retry by Claude Code is not mistaken for a stall.
|
|
484
|
+
return Effect.logDebug("Claude Code is retrying an API request").pipe(
|
|
485
|
+
Effect.annotateLogs({
|
|
486
|
+
attempt: message.attempt,
|
|
487
|
+
maxRetries: message.max_retries,
|
|
488
|
+
retryDelayMs: message.retry_delay_ms,
|
|
489
|
+
errorStatus: message.error_status,
|
|
490
|
+
error: message.error,
|
|
491
|
+
...(message.no_response === undefined
|
|
492
|
+
? {}
|
|
493
|
+
: {
|
|
494
|
+
noResponseWaitedMs: message.no_response.waited_ms,
|
|
495
|
+
noResponseRetryWaitMs: message.no_response.retry_wait_ms,
|
|
496
|
+
}),
|
|
497
|
+
}),
|
|
498
|
+
Effect.as(parts),
|
|
499
|
+
)
|
|
481
500
|
}
|
|
482
501
|
return Effect.succeed(parts)
|
|
483
502
|
}
|
|
@@ -579,7 +598,7 @@ const makeToolServer = (
|
|
|
579
598
|
inputSchema: Tool.getJsonSchema(tool) as {
|
|
580
599
|
readonly type: "object"
|
|
581
600
|
},
|
|
582
|
-
annotations: { readOnlyHint:
|
|
601
|
+
annotations: { readOnlyHint: true },
|
|
583
602
|
_meta: { "anthropic/alwaysLoad": true },
|
|
584
603
|
}))
|
|
585
604
|
server.setRequestHandler(ListToolsRequestSchema, async () => ({
|
|
@@ -677,11 +696,14 @@ interface Session {
|
|
|
677
696
|
outcome: Outcome | undefined
|
|
678
697
|
}
|
|
679
698
|
|
|
699
|
+
// Ends `messages` before closing the scope, which interrupts the fiber that
|
|
700
|
+
// would end it, so a step still reading the session fails instead of waiting.
|
|
680
701
|
const closeSession = (session: Session) =>
|
|
681
702
|
Effect.suspend(() => {
|
|
682
703
|
session.bridge.cancel()
|
|
683
704
|
session.query.close()
|
|
684
705
|
return Queue.end(session.input).pipe(
|
|
706
|
+
Effect.andThen(Queue.end(session.messages)),
|
|
685
707
|
Effect.andThen(Scope.close(session.scope, Exit.void)),
|
|
686
708
|
)
|
|
687
709
|
})
|
|
@@ -867,22 +889,51 @@ const continueSession = (
|
|
|
867
889
|
plan.ids.forEach((id, i) => session.bridge.resolve(id, results[i]!))
|
|
868
890
|
}
|
|
869
891
|
|
|
870
|
-
// The
|
|
871
|
-
const
|
|
892
|
+
// The tool results in an SDK user message, with their text.
|
|
893
|
+
const toolResults = (
|
|
894
|
+
message: Sdk.SDKUserMessage,
|
|
895
|
+
): Array<{
|
|
896
|
+
readonly id: string
|
|
897
|
+
readonly isError: boolean
|
|
898
|
+
readonly text: string
|
|
899
|
+
}> =>
|
|
872
900
|
typeof message.message.content === "string"
|
|
873
901
|
? []
|
|
874
902
|
: message.message.content.flatMap((block) =>
|
|
875
|
-
block.type === "tool_result"
|
|
903
|
+
block.type === "tool_result"
|
|
904
|
+
? [
|
|
905
|
+
{
|
|
906
|
+
id: block.tool_use_id,
|
|
907
|
+
isError: block.is_error === true,
|
|
908
|
+
text:
|
|
909
|
+
typeof block.content === "string"
|
|
910
|
+
? block.content
|
|
911
|
+
: (block.content ?? [])
|
|
912
|
+
.flatMap((part) =>
|
|
913
|
+
part.type === "text" ? [part.text] : [],
|
|
914
|
+
)
|
|
915
|
+
.join("\n"),
|
|
916
|
+
},
|
|
917
|
+
]
|
|
918
|
+
: [],
|
|
876
919
|
)
|
|
877
920
|
|
|
921
|
+
// Claude Code wraps the errors it hands back to the model, such as invalid
|
|
922
|
+
// input, a streaming fallback or an unknown tool, in this tag, and marks
|
|
923
|
+
// their results `is_error`. Aborts are not wrapped.
|
|
924
|
+
const isToolUseError = (text: string) =>
|
|
925
|
+
text.trimStart().startsWith("<tool_use_error>")
|
|
926
|
+
|
|
878
927
|
// Reads one step from the session, recording how it ended. A step that does
|
|
879
928
|
// not finish leaves the session in an unknown state, so it is closed.
|
|
880
929
|
//
|
|
881
930
|
// A tool step holds back its tool calls and its finish until Claude Code has
|
|
882
931
|
// parked an MCP request for every call. Effect runs a call's handler as soon
|
|
883
932
|
// as more parts follow it, so emitting a call earlier could run a script
|
|
884
|
-
// that Claude Code never waits for.
|
|
885
|
-
//
|
|
933
|
+
// that Claude Code never waits for. When Claude Code answers a held call
|
|
934
|
+
// itself with an error for the model, the call is dropped and the step keeps
|
|
935
|
+
// reading while the model retries. An untagged request, or any other answer
|
|
936
|
+
// from Claude Code, fails the step before any script runs.
|
|
886
937
|
const readStep = (
|
|
887
938
|
session: Session,
|
|
888
939
|
onAbandon: Effect.Effect<void>,
|
|
@@ -911,7 +962,11 @@ const readStep = (
|
|
|
911
962
|
),
|
|
912
963
|
)
|
|
913
964
|
}
|
|
914
|
-
|
|
965
|
+
// With every call dropped, the step waits for the model's retry or
|
|
966
|
+
// the end of its turn.
|
|
967
|
+
if (calls.length === 0 || !calls.every((call) => parked.has(call.id))) {
|
|
968
|
+
return Effect.succeed([])
|
|
969
|
+
}
|
|
915
970
|
session.outcome = { _tag: "Tools", calls, text }
|
|
916
971
|
return Effect.succeed([...calls, finish])
|
|
917
972
|
}
|
|
@@ -931,16 +986,30 @@ const readStep = (
|
|
|
931
986
|
return release()
|
|
932
987
|
}
|
|
933
988
|
if (event.type === "user" && event.parent_tool_use_id === null) {
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
989
|
+
let dropped = false
|
|
990
|
+
for (const result of toolResults(event)) {
|
|
991
|
+
const index = calls.findIndex((call) => call.id === result.id)
|
|
992
|
+
if (index === -1 || parked.has(result.id)) continue
|
|
993
|
+
if (!result.isError || !isToolUseError(result.text)) {
|
|
994
|
+
return Effect.fail(
|
|
995
|
+
unknownError(
|
|
996
|
+
`Claude Code answered the tool call ${result.id} itself instead of calling clanka: ${result.text}`,
|
|
997
|
+
),
|
|
998
|
+
)
|
|
999
|
+
}
|
|
1000
|
+
calls.splice(index, 1)
|
|
1001
|
+
dropped = true
|
|
943
1002
|
}
|
|
1003
|
+
if (dropped) return release()
|
|
1004
|
+
}
|
|
1005
|
+
// A retry after dropped calls is a new API message; the last one's
|
|
1006
|
+
// finish must not release its calls before its own `message_stop`.
|
|
1007
|
+
if (
|
|
1008
|
+
event.type === "stream_event" &&
|
|
1009
|
+
event.parent_tool_use_id === null &&
|
|
1010
|
+
event.event.type === "message_start"
|
|
1011
|
+
) {
|
|
1012
|
+
finish = undefined
|
|
944
1013
|
}
|
|
945
1014
|
return Effect.flatMap(convert(event), (parts) => {
|
|
946
1015
|
const out: Array<Response.StreamPartEncoded> = []
|
|
@@ -972,8 +1041,8 @@ const readStep = (
|
|
|
972
1041
|
return Stream.fromQueue(session.messages).pipe(
|
|
973
1042
|
Stream.mapEffect(handle),
|
|
974
1043
|
Stream.flattenIterable,
|
|
975
|
-
// The queue ends when Claude Code exits
|
|
976
|
-
// stopped mid-step.
|
|
1044
|
+
// The queue ends when Claude Code exits or the session is closed, so
|
|
1045
|
+
// reaching this means it stopped mid-step.
|
|
977
1046
|
Stream.concat(
|
|
978
1047
|
Stream.fail(
|
|
979
1048
|
unknownError("Claude Code stopped before finishing its reply"),
|
|
@@ -1018,25 +1087,44 @@ const layerLanguageModel = (
|
|
|
1018
1087
|
return session === undefined ? Effect.void : closeSession(session)
|
|
1019
1088
|
})
|
|
1020
1089
|
yield* Effect.addFinalizer(() => closeCurrent)
|
|
1090
|
+
// Set while a step is read. Another step would close the session under
|
|
1091
|
+
// it, so it fails instead.
|
|
1092
|
+
let reading = false
|
|
1021
1093
|
|
|
1022
1094
|
const step = (provider: LanguageModel.ProviderOptions) =>
|
|
1023
|
-
Stream.
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1095
|
+
Stream.suspend(() => {
|
|
1096
|
+
if (reading) {
|
|
1097
|
+
return Stream.fail(
|
|
1098
|
+
unknownError(
|
|
1099
|
+
"This Claude Agent model is already reading a step. Every agent needs its own model instance.",
|
|
1100
|
+
),
|
|
1101
|
+
)
|
|
1102
|
+
}
|
|
1103
|
+
reading = true
|
|
1104
|
+
return Stream.unwrap(
|
|
1105
|
+
Effect.gen(function* () {
|
|
1106
|
+
const plan =
|
|
1107
|
+
current === undefined
|
|
1108
|
+
? undefined
|
|
1109
|
+
: continuation(current, provider)
|
|
1110
|
+
if (current !== undefined && plan !== undefined) {
|
|
1111
|
+
continueSession(current, plan)
|
|
1112
|
+
current.seen = provider.prompt.content
|
|
1113
|
+
current.outcome = undefined
|
|
1114
|
+
return readStep(current, closeCurrent)
|
|
1115
|
+
}
|
|
1116
|
+
yield* closeCurrent
|
|
1117
|
+
current = yield* startSession(model, options, provider, true)
|
|
1033
1118
|
return readStep(current, closeCurrent)
|
|
1034
|
-
}
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1119
|
+
}),
|
|
1120
|
+
).pipe(
|
|
1121
|
+
Stream.ensuring(
|
|
1122
|
+
Effect.sync(() => {
|
|
1123
|
+
reading = false
|
|
1124
|
+
}),
|
|
1125
|
+
),
|
|
1126
|
+
)
|
|
1127
|
+
})
|
|
1040
1128
|
|
|
1041
1129
|
const streamText = (provider: LanguageModel.ProviderOptions) =>
|
|
1042
1130
|
provider.tools.length === 0
|