@witqq/agent-sdk 0.6.1 → 0.7.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/README.md +433 -6
- package/dist/auth/index.cjs +188 -1
- package/dist/auth/index.cjs.map +1 -1
- package/dist/auth/index.d.cts +154 -138
- package/dist/auth/index.d.ts +154 -138
- package/dist/auth/index.js +188 -2
- package/dist/auth/index.js.map +1 -1
- package/dist/backends/claude.cjs +315 -21
- package/dist/backends/claude.cjs.map +1 -1
- package/dist/backends/claude.d.cts +2 -1
- package/dist/backends/claude.d.ts +2 -1
- package/dist/backends/claude.js +315 -21
- package/dist/backends/claude.js.map +1 -1
- package/dist/backends/copilot.cjs +132 -24
- package/dist/backends/copilot.cjs.map +1 -1
- package/dist/backends/copilot.d.cts +2 -1
- package/dist/backends/copilot.d.ts +2 -1
- package/dist/backends/copilot.js +132 -24
- package/dist/backends/copilot.js.map +1 -1
- package/dist/backends/vercel-ai.cjs +56 -17
- package/dist/backends/vercel-ai.cjs.map +1 -1
- package/dist/backends/vercel-ai.d.cts +1 -1
- package/dist/backends/vercel-ai.d.ts +1 -1
- package/dist/backends/vercel-ai.js +56 -17
- package/dist/backends/vercel-ai.js.map +1 -1
- package/dist/chat/accumulator.cjs +147 -0
- package/dist/chat/accumulator.cjs.map +1 -0
- package/dist/chat/accumulator.d.cts +61 -0
- package/dist/chat/accumulator.d.ts +61 -0
- package/dist/chat/accumulator.js +145 -0
- package/dist/chat/accumulator.js.map +1 -0
- package/dist/chat/backends.cjs +3534 -0
- package/dist/chat/backends.cjs.map +1 -0
- package/dist/chat/backends.d.cts +62 -0
- package/dist/chat/backends.d.ts +62 -0
- package/dist/chat/backends.js +3501 -0
- package/dist/chat/backends.js.map +1 -0
- package/dist/chat/context.cjs +230 -0
- package/dist/chat/context.cjs.map +1 -0
- package/dist/chat/context.d.cts +167 -0
- package/dist/chat/context.d.ts +167 -0
- package/dist/chat/context.js +227 -0
- package/dist/chat/context.js.map +1 -0
- package/dist/chat/core.cjs +282 -0
- package/dist/chat/core.cjs.map +1 -0
- package/dist/chat/core.d.cts +435 -0
- package/dist/chat/core.d.ts +435 -0
- package/dist/chat/core.js +261 -0
- package/dist/chat/core.js.map +1 -0
- package/dist/chat/errors.cjs +251 -0
- package/dist/chat/errors.cjs.map +1 -0
- package/dist/chat/errors.d.cts +122 -0
- package/dist/chat/errors.d.ts +122 -0
- package/dist/chat/errors.js +243 -0
- package/dist/chat/errors.js.map +1 -0
- package/dist/chat/events.cjs +203 -0
- package/dist/chat/events.cjs.map +1 -0
- package/dist/chat/events.d.cts +241 -0
- package/dist/chat/events.d.ts +241 -0
- package/dist/chat/events.js +196 -0
- package/dist/chat/events.js.map +1 -0
- package/dist/chat/index.cjs +5359 -0
- package/dist/chat/index.cjs.map +1 -0
- package/dist/chat/index.d.cts +52 -0
- package/dist/chat/index.d.ts +52 -0
- package/dist/chat/index.js +5296 -0
- package/dist/chat/index.js.map +1 -0
- package/dist/chat/react.cjs +2739 -0
- package/dist/chat/react.cjs.map +1 -0
- package/dist/chat/react.d.cts +619 -0
- package/dist/chat/react.d.ts +619 -0
- package/dist/chat/react.js +2714 -0
- package/dist/chat/react.js.map +1 -0
- package/dist/chat/runtime.cjs +1030 -0
- package/dist/chat/runtime.cjs.map +1 -0
- package/dist/chat/runtime.d.cts +118 -0
- package/dist/chat/runtime.d.ts +118 -0
- package/dist/chat/runtime.js +1028 -0
- package/dist/chat/runtime.js.map +1 -0
- package/dist/chat/server.cjs +643 -0
- package/dist/chat/server.cjs.map +1 -0
- package/dist/chat/server.d.cts +287 -0
- package/dist/chat/server.d.ts +287 -0
- package/dist/chat/server.js +617 -0
- package/dist/chat/server.js.map +1 -0
- package/dist/chat/sessions.cjs +398 -0
- package/dist/chat/sessions.cjs.map +1 -0
- package/dist/chat/sessions.d.cts +239 -0
- package/dist/chat/sessions.d.ts +239 -0
- package/dist/chat/sessions.js +394 -0
- package/dist/chat/sessions.js.map +1 -0
- package/dist/chat/state.cjs +177 -0
- package/dist/chat/state.cjs.map +1 -0
- package/dist/chat/state.d.cts +92 -0
- package/dist/chat/state.d.ts +92 -0
- package/dist/chat/state.js +167 -0
- package/dist/chat/state.js.map +1 -0
- package/dist/chat/storage.cjs +240 -0
- package/dist/chat/storage.cjs.map +1 -0
- package/dist/chat/storage.d.cts +191 -0
- package/dist/chat/storage.d.ts +191 -0
- package/dist/chat/storage.js +236 -0
- package/dist/chat/storage.js.map +1 -0
- package/dist/errors-BDLbNu9w.d.cts +13 -0
- package/dist/errors-BDLbNu9w.d.ts +13 -0
- package/dist/in-process-transport-C2oPTYs6.d.ts +223 -0
- package/dist/in-process-transport-DG-w5G6k.d.cts +223 -0
- package/dist/index.cjs +25 -13
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +32 -4
- package/dist/index.d.ts +32 -4
- package/dist/index.js +25 -13
- package/dist/index.js.map +1 -1
- package/dist/transport-D1OaUgRk.d.ts +67 -0
- package/dist/transport-DX1Nhm4N.d.cts +67 -0
- package/dist/types-Bh5AhqD-.d.ts +141 -0
- package/dist/types-CGF7AEX1.d.cts +141 -0
- package/dist/{types-BvwNzZCj.d.cts → types-CqvUAYxt.d.cts} +21 -3
- package/dist/{types-BvwNzZCj.d.ts → types-CqvUAYxt.d.ts} +21 -3
- package/dist/types-DLZzlJxt.d.ts +39 -0
- package/dist/types-tE0CXwBl.d.cts +39 -0
- package/package.json +149 -2
package/dist/backends/claude.cjs
CHANGED
|
@@ -8,10 +8,16 @@ function getTextContent(content) {
|
|
|
8
8
|
|
|
9
9
|
// src/errors.ts
|
|
10
10
|
var AgentSDKError = class extends Error {
|
|
11
|
+
/** @internal Marker for cross-bundle identity checks */
|
|
12
|
+
_agentSDKError = true;
|
|
11
13
|
constructor(message, options) {
|
|
12
14
|
super(message, options);
|
|
13
15
|
this.name = "AgentSDKError";
|
|
14
16
|
}
|
|
17
|
+
/** Check if an error is an AgentSDKError (works across bundled copies) */
|
|
18
|
+
static is(error) {
|
|
19
|
+
return error instanceof Error && "_agentSDKError" in error && error._agentSDKError === true;
|
|
20
|
+
}
|
|
15
21
|
};
|
|
16
22
|
var ReentrancyError = class extends AgentSDKError {
|
|
17
23
|
constructor() {
|
|
@@ -43,6 +49,7 @@ var BaseAgent = class {
|
|
|
43
49
|
state = "idle";
|
|
44
50
|
abortController = null;
|
|
45
51
|
config;
|
|
52
|
+
_cleanupExternalSignal = null;
|
|
46
53
|
/** CLI session ID for persistent mode. Override in backends that support it. */
|
|
47
54
|
get sessionId() {
|
|
48
55
|
return void 0;
|
|
@@ -62,8 +69,7 @@ var BaseAgent = class {
|
|
|
62
69
|
this.enrichAndNotifyUsage(result);
|
|
63
70
|
return result;
|
|
64
71
|
} finally {
|
|
65
|
-
this.
|
|
66
|
-
this.abortController = null;
|
|
72
|
+
this.cleanupRun();
|
|
67
73
|
}
|
|
68
74
|
}
|
|
69
75
|
async runWithContext(messages, options) {
|
|
@@ -76,8 +82,7 @@ var BaseAgent = class {
|
|
|
76
82
|
this.enrichAndNotifyUsage(result);
|
|
77
83
|
return result;
|
|
78
84
|
} finally {
|
|
79
|
-
this.
|
|
80
|
-
this.abortController = null;
|
|
85
|
+
this.cleanupRun();
|
|
81
86
|
}
|
|
82
87
|
}
|
|
83
88
|
async runStructured(prompt, schema, options) {
|
|
@@ -96,8 +101,7 @@ var BaseAgent = class {
|
|
|
96
101
|
this.enrichAndNotifyUsage(result);
|
|
97
102
|
return result;
|
|
98
103
|
} finally {
|
|
99
|
-
this.
|
|
100
|
-
this.abortController = null;
|
|
104
|
+
this.cleanupRun();
|
|
101
105
|
}
|
|
102
106
|
}
|
|
103
107
|
async *stream(prompt, options) {
|
|
@@ -110,8 +114,7 @@ var BaseAgent = class {
|
|
|
110
114
|
const enriched = this.enrichStream(this.executeStream(messages, options, ac.signal));
|
|
111
115
|
yield* this.heartbeatStream(enriched);
|
|
112
116
|
} finally {
|
|
113
|
-
this.
|
|
114
|
-
this.abortController = null;
|
|
117
|
+
this.cleanupRun();
|
|
115
118
|
}
|
|
116
119
|
}
|
|
117
120
|
async *streamWithContext(messages, options) {
|
|
@@ -123,8 +126,7 @@ var BaseAgent = class {
|
|
|
123
126
|
const enriched = this.enrichStream(this.executeStream(messages, options, ac.signal));
|
|
124
127
|
yield* this.heartbeatStream(enriched);
|
|
125
128
|
} finally {
|
|
126
|
-
this.
|
|
127
|
-
this.abortController = null;
|
|
129
|
+
this.cleanupRun();
|
|
128
130
|
}
|
|
129
131
|
}
|
|
130
132
|
abort() {
|
|
@@ -144,6 +146,8 @@ var BaseAgent = class {
|
|
|
144
146
|
}
|
|
145
147
|
/** Mark agent as disposed. Override to add cleanup. */
|
|
146
148
|
dispose() {
|
|
149
|
+
this._cleanupExternalSignal?.();
|
|
150
|
+
this._cleanupExternalSignal = null;
|
|
147
151
|
this.abort();
|
|
148
152
|
this.state = "disposed";
|
|
149
153
|
}
|
|
@@ -254,16 +258,24 @@ var BaseAgent = class {
|
|
|
254
258
|
}
|
|
255
259
|
}
|
|
256
260
|
// ─── Internal Helpers ─────────────────────────────────────────
|
|
261
|
+
/** Clean up after a run completes (success, error, or abort). */
|
|
262
|
+
cleanupRun() {
|
|
263
|
+
this._cleanupExternalSignal?.();
|
|
264
|
+
this._cleanupExternalSignal = null;
|
|
265
|
+
this.state = "idle";
|
|
266
|
+
this.abortController = null;
|
|
267
|
+
}
|
|
257
268
|
createAbortController(externalSignal) {
|
|
258
269
|
const ac = new AbortController();
|
|
259
270
|
this.abortController = ac;
|
|
271
|
+
this._cleanupExternalSignal = null;
|
|
260
272
|
if (externalSignal) {
|
|
261
273
|
if (externalSignal.aborted) {
|
|
262
274
|
ac.abort();
|
|
263
275
|
} else {
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
276
|
+
const listener = () => ac.abort();
|
|
277
|
+
externalSignal.addEventListener("abort", listener, { once: true });
|
|
278
|
+
this._cleanupExternalSignal = () => externalSignal.removeEventListener("abort", listener);
|
|
267
279
|
}
|
|
268
280
|
}
|
|
269
281
|
return ac;
|
|
@@ -605,9 +617,12 @@ var ClaudeAgent = class extends BaseAgent {
|
|
|
605
617
|
constructor(config, options) {
|
|
606
618
|
super(config);
|
|
607
619
|
this.options = options;
|
|
608
|
-
this.tools = config.tools;
|
|
620
|
+
this.tools = config.tools ?? [];
|
|
609
621
|
this.canUseTool = buildCanUseTool(config);
|
|
610
622
|
this.isPersistent = config.sessionMode === "persistent";
|
|
623
|
+
if (options.resumeSessionId) {
|
|
624
|
+
this._sessionId = options.resumeSessionId;
|
|
625
|
+
}
|
|
611
626
|
if (config.supervisor?.onAskUser) {
|
|
612
627
|
console.warn(
|
|
613
628
|
"[agent-sdk/claude] supervisor.onAskUser is not supported by the Claude CLI backend. User interaction requests from the model will not be forwarded."
|
|
@@ -666,7 +681,7 @@ var ClaudeAgent = class extends BaseAgent {
|
|
|
666
681
|
opts.permissionMode = "default";
|
|
667
682
|
}
|
|
668
683
|
if (this.config.availableTools) {
|
|
669
|
-
opts.
|
|
684
|
+
opts.tools = [...this.config.availableTools];
|
|
670
685
|
}
|
|
671
686
|
return opts;
|
|
672
687
|
}
|
|
@@ -740,8 +755,78 @@ var ClaudeAgent = class extends BaseAgent {
|
|
|
740
755
|
}
|
|
741
756
|
}
|
|
742
757
|
} catch (e) {
|
|
743
|
-
if (this.isPersistent) this.clearPersistentSession();
|
|
744
758
|
if (signal.aborted) throw new AbortError();
|
|
759
|
+
if (isResuming && this.isPersistent) {
|
|
760
|
+
this.clearPersistentSession();
|
|
761
|
+
const retryPrompt = buildContextualPrompt(messages);
|
|
762
|
+
let retryOpts = this.buildQueryOptions(signal);
|
|
763
|
+
toolResultCapture.clear();
|
|
764
|
+
retryOpts = await this.buildMcpConfig(retryOpts, toolResultCapture);
|
|
765
|
+
const retryQ = sdk.query({ prompt: retryPrompt, options: retryOpts });
|
|
766
|
+
this.activeQuery = retryQ;
|
|
767
|
+
toolCalls.length = 0;
|
|
768
|
+
output = null;
|
|
769
|
+
try {
|
|
770
|
+
for await (const msg of retryQ) {
|
|
771
|
+
if (msg.type === "assistant") {
|
|
772
|
+
const betaMessage = msg.message;
|
|
773
|
+
if (betaMessage?.content) {
|
|
774
|
+
for (const block of betaMessage.content) {
|
|
775
|
+
if (block.type === "tool_use") {
|
|
776
|
+
const toolName = stripMcpPrefix(block.name ?? "unknown");
|
|
777
|
+
toolCalls.push({
|
|
778
|
+
toolName,
|
|
779
|
+
args: block.input ?? {},
|
|
780
|
+
result: toolResultCapture.get(toolName) ?? null,
|
|
781
|
+
approved: true
|
|
782
|
+
});
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
if (msg.type === "tool_use_summary" || msg.type === "result") {
|
|
788
|
+
for (const tc of toolCalls) {
|
|
789
|
+
if (tc.result === null) {
|
|
790
|
+
const captured = toolResultCapture.get(tc.toolName);
|
|
791
|
+
if (captured !== void 0) tc.result = captured;
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
}
|
|
795
|
+
if (msg.type === "result") {
|
|
796
|
+
if (msg.subtype === "success") {
|
|
797
|
+
const r = msg;
|
|
798
|
+
output = r.result;
|
|
799
|
+
usage = aggregateUsage(r.modelUsage);
|
|
800
|
+
if (this.isPersistent && r.session_id) {
|
|
801
|
+
this._sessionId = r.session_id;
|
|
802
|
+
}
|
|
803
|
+
} else if (msg.is_error) {
|
|
804
|
+
const r = msg;
|
|
805
|
+
throw new Error(
|
|
806
|
+
`Claude query failed: ${r.errors?.join("; ") ?? "unknown error"}`
|
|
807
|
+
);
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
} catch (retryError) {
|
|
812
|
+
if (this.isPersistent) this.clearPersistentSession();
|
|
813
|
+
if (signal.aborted) throw new AbortError();
|
|
814
|
+
throw retryError;
|
|
815
|
+
} finally {
|
|
816
|
+
this.activeQuery = null;
|
|
817
|
+
}
|
|
818
|
+
return {
|
|
819
|
+
output,
|
|
820
|
+
structuredOutput: void 0,
|
|
821
|
+
toolCalls,
|
|
822
|
+
messages: [
|
|
823
|
+
...messages,
|
|
824
|
+
...output !== null ? [{ role: "assistant", content: output }] : []
|
|
825
|
+
],
|
|
826
|
+
usage
|
|
827
|
+
};
|
|
828
|
+
}
|
|
829
|
+
if (this.isPersistent) this.clearPersistentSession();
|
|
745
830
|
throw e;
|
|
746
831
|
} finally {
|
|
747
832
|
this.activeQuery = null;
|
|
@@ -835,8 +920,98 @@ var ClaudeAgent = class extends BaseAgent {
|
|
|
835
920
|
}
|
|
836
921
|
}
|
|
837
922
|
} catch (e) {
|
|
838
|
-
if (this.isPersistent) this.clearPersistentSession();
|
|
839
923
|
if (signal.aborted) throw new AbortError();
|
|
924
|
+
if (isResuming && this.isPersistent) {
|
|
925
|
+
this.clearPersistentSession();
|
|
926
|
+
const retryPrompt = buildContextualPrompt(messages);
|
|
927
|
+
let retryOpts = this.buildQueryOptions(signal);
|
|
928
|
+
toolResultCapture.clear();
|
|
929
|
+
retryOpts = await this.buildMcpConfig(retryOpts, toolResultCapture);
|
|
930
|
+
retryOpts.outputFormat = {
|
|
931
|
+
type: "json_schema",
|
|
932
|
+
schema: jsonSchema
|
|
933
|
+
};
|
|
934
|
+
const retryQ = sdk.query({ prompt: retryPrompt, options: retryOpts });
|
|
935
|
+
this.activeQuery = retryQ;
|
|
936
|
+
toolCalls.length = 0;
|
|
937
|
+
output = null;
|
|
938
|
+
structuredOutput = void 0;
|
|
939
|
+
try {
|
|
940
|
+
for await (const msg of retryQ) {
|
|
941
|
+
if (msg.type === "assistant") {
|
|
942
|
+
const betaMessage = msg.message;
|
|
943
|
+
if (betaMessage?.content) {
|
|
944
|
+
for (const block of betaMessage.content) {
|
|
945
|
+
if (block.type === "tool_use") {
|
|
946
|
+
const toolName = stripMcpPrefix(block.name ?? "unknown");
|
|
947
|
+
toolCalls.push({
|
|
948
|
+
toolName,
|
|
949
|
+
args: block.input ?? {},
|
|
950
|
+
result: toolResultCapture.get(toolName) ?? null,
|
|
951
|
+
approved: true
|
|
952
|
+
});
|
|
953
|
+
}
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
if (msg.type === "tool_use_summary" || msg.type === "result") {
|
|
958
|
+
for (const tc of toolCalls) {
|
|
959
|
+
if (tc.result === null) {
|
|
960
|
+
const captured = toolResultCapture.get(tc.toolName);
|
|
961
|
+
if (captured !== void 0) tc.result = captured;
|
|
962
|
+
}
|
|
963
|
+
}
|
|
964
|
+
}
|
|
965
|
+
if (msg.type === "result" && msg.subtype === "success") {
|
|
966
|
+
const r = msg;
|
|
967
|
+
output = r.result;
|
|
968
|
+
if (r.structured_output !== void 0) {
|
|
969
|
+
try {
|
|
970
|
+
structuredOutput = schema.schema.parse(r.structured_output);
|
|
971
|
+
} catch {
|
|
972
|
+
try {
|
|
973
|
+
structuredOutput = schema.schema.parse(JSON.parse(r.result));
|
|
974
|
+
} catch {
|
|
975
|
+
}
|
|
976
|
+
}
|
|
977
|
+
} else if (r.result) {
|
|
978
|
+
try {
|
|
979
|
+
const jsonMatch = r.result.match(/```(?:json)?\s*([\s\S]*?)```/);
|
|
980
|
+
const raw = jsonMatch ? jsonMatch[1].trim() : r.result.trim();
|
|
981
|
+
structuredOutput = schema.schema.parse(JSON.parse(raw));
|
|
982
|
+
} catch {
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
usage = aggregateUsage(r.modelUsage);
|
|
986
|
+
if (this.isPersistent && r.session_id) {
|
|
987
|
+
this._sessionId = r.session_id;
|
|
988
|
+
}
|
|
989
|
+
} else if (msg.type === "result" && msg.is_error) {
|
|
990
|
+
const r = msg;
|
|
991
|
+
throw new Error(
|
|
992
|
+
`Claude query failed: ${r.errors?.join("; ") ?? "unknown error"}`
|
|
993
|
+
);
|
|
994
|
+
}
|
|
995
|
+
}
|
|
996
|
+
} catch (retryError) {
|
|
997
|
+
if (this.isPersistent) this.clearPersistentSession();
|
|
998
|
+
if (signal.aborted) throw new AbortError();
|
|
999
|
+
throw retryError;
|
|
1000
|
+
} finally {
|
|
1001
|
+
this.activeQuery = null;
|
|
1002
|
+
}
|
|
1003
|
+
return {
|
|
1004
|
+
output,
|
|
1005
|
+
structuredOutput,
|
|
1006
|
+
toolCalls,
|
|
1007
|
+
messages: [
|
|
1008
|
+
...messages,
|
|
1009
|
+
...output !== null ? [{ role: "assistant", content: output }] : []
|
|
1010
|
+
],
|
|
1011
|
+
usage
|
|
1012
|
+
};
|
|
1013
|
+
}
|
|
1014
|
+
if (this.isPersistent) this.clearPersistentSession();
|
|
840
1015
|
throw e;
|
|
841
1016
|
} finally {
|
|
842
1017
|
this.activeQuery = null;
|
|
@@ -859,21 +1034,47 @@ var ClaudeAgent = class extends BaseAgent {
|
|
|
859
1034
|
const isResuming = this.isPersistent && this._sessionId !== void 0;
|
|
860
1035
|
const prompt = isResuming ? extractLastUserPrompt(messages) : buildContextualPrompt(messages);
|
|
861
1036
|
let opts = this.buildQueryOptions(signal);
|
|
862
|
-
|
|
1037
|
+
const toolResultCapture = /* @__PURE__ */ new Map();
|
|
1038
|
+
opts = await this.buildMcpConfig(opts, toolResultCapture);
|
|
863
1039
|
const q = sdk.query({ prompt, options: opts });
|
|
864
1040
|
this.activeQuery = q;
|
|
865
1041
|
const thinkingBlockIndices = /* @__PURE__ */ new Set();
|
|
866
1042
|
const toolCallTracker = new ClaudeToolCallTracker();
|
|
1043
|
+
const pendingStreamToolCalls = /* @__PURE__ */ new Map();
|
|
867
1044
|
try {
|
|
868
1045
|
for await (const msg of q) {
|
|
869
1046
|
if (signal.aborted) throw new AbortError();
|
|
870
1047
|
const events = mapSDKMessage(msg, thinkingBlockIndices, toolCallTracker);
|
|
871
1048
|
if (events) {
|
|
872
1049
|
const mapped = Array.isArray(events) ? events : [events];
|
|
873
|
-
for (const e of mapped)
|
|
1050
|
+
for (const e of mapped) {
|
|
1051
|
+
if (e.type === "tool_call_start") {
|
|
1052
|
+
pendingStreamToolCalls.set(e.toolCallId, e.toolName);
|
|
1053
|
+
}
|
|
1054
|
+
if (e.type === "tool_call_end" && toolResultCapture.has(e.toolName)) {
|
|
1055
|
+
e.result = toolResultCapture.get(e.toolName);
|
|
1056
|
+
toolResultCapture.delete(e.toolName);
|
|
1057
|
+
pendingStreamToolCalls.delete(e.toolCallId);
|
|
1058
|
+
} else if (e.type === "tool_call_end") {
|
|
1059
|
+
pendingStreamToolCalls.delete(e.toolCallId);
|
|
1060
|
+
}
|
|
1061
|
+
yield e;
|
|
1062
|
+
}
|
|
874
1063
|
}
|
|
875
1064
|
if (msg.type === "result" && msg.subtype === "success") {
|
|
876
1065
|
const r = msg;
|
|
1066
|
+
for (const [toolCallId, toolName] of pendingStreamToolCalls) {
|
|
1067
|
+
if (toolResultCapture.has(toolName)) {
|
|
1068
|
+
yield {
|
|
1069
|
+
type: "tool_call_end",
|
|
1070
|
+
toolCallId,
|
|
1071
|
+
toolName,
|
|
1072
|
+
result: toolResultCapture.get(toolName)
|
|
1073
|
+
};
|
|
1074
|
+
toolResultCapture.delete(toolName);
|
|
1075
|
+
}
|
|
1076
|
+
}
|
|
1077
|
+
pendingStreamToolCalls.clear();
|
|
877
1078
|
if (r.session_id) {
|
|
878
1079
|
if (this.isPersistent) {
|
|
879
1080
|
this._sessionId = r.session_id;
|
|
@@ -884,8 +1085,71 @@ var ClaudeAgent = class extends BaseAgent {
|
|
|
884
1085
|
}
|
|
885
1086
|
}
|
|
886
1087
|
} catch (e) {
|
|
887
|
-
if (this.isPersistent) this.clearPersistentSession();
|
|
888
1088
|
if (signal.aborted) throw new AbortError();
|
|
1089
|
+
if (isResuming && this.isPersistent) {
|
|
1090
|
+
this.clearPersistentSession();
|
|
1091
|
+
const retryPrompt = buildContextualPrompt(messages);
|
|
1092
|
+
let retryOpts = this.buildQueryOptions(signal);
|
|
1093
|
+
toolResultCapture.clear();
|
|
1094
|
+
retryOpts = await this.buildMcpConfig(retryOpts, toolResultCapture);
|
|
1095
|
+
const retryQ = sdk.query({ prompt: retryPrompt, options: retryOpts });
|
|
1096
|
+
this.activeQuery = retryQ;
|
|
1097
|
+
const retryThinkingBlockIndices = /* @__PURE__ */ new Set();
|
|
1098
|
+
const retryToolCallTracker = new ClaudeToolCallTracker();
|
|
1099
|
+
const retryPendingToolCalls = /* @__PURE__ */ new Map();
|
|
1100
|
+
try {
|
|
1101
|
+
for await (const msg of retryQ) {
|
|
1102
|
+
if (signal.aborted) throw new AbortError();
|
|
1103
|
+
const retryEvents = mapSDKMessage(msg, retryThinkingBlockIndices, retryToolCallTracker);
|
|
1104
|
+
if (retryEvents) {
|
|
1105
|
+
const mapped = Array.isArray(retryEvents) ? retryEvents : [retryEvents];
|
|
1106
|
+
for (const ev of mapped) {
|
|
1107
|
+
if (ev.type === "tool_call_start") {
|
|
1108
|
+
retryPendingToolCalls.set(ev.toolCallId, ev.toolName);
|
|
1109
|
+
}
|
|
1110
|
+
if (ev.type === "tool_call_end" && toolResultCapture.has(ev.toolName)) {
|
|
1111
|
+
ev.result = toolResultCapture.get(ev.toolName);
|
|
1112
|
+
toolResultCapture.delete(ev.toolName);
|
|
1113
|
+
retryPendingToolCalls.delete(ev.toolCallId);
|
|
1114
|
+
} else if (ev.type === "tool_call_end") {
|
|
1115
|
+
retryPendingToolCalls.delete(ev.toolCallId);
|
|
1116
|
+
}
|
|
1117
|
+
yield ev;
|
|
1118
|
+
}
|
|
1119
|
+
}
|
|
1120
|
+
if (msg.type === "result" && msg.subtype === "success") {
|
|
1121
|
+
const r = msg;
|
|
1122
|
+
for (const [toolCallId, toolName] of retryPendingToolCalls) {
|
|
1123
|
+
if (toolResultCapture.has(toolName)) {
|
|
1124
|
+
yield {
|
|
1125
|
+
type: "tool_call_end",
|
|
1126
|
+
toolCallId,
|
|
1127
|
+
toolName,
|
|
1128
|
+
result: toolResultCapture.get(toolName)
|
|
1129
|
+
};
|
|
1130
|
+
toolResultCapture.delete(toolName);
|
|
1131
|
+
}
|
|
1132
|
+
}
|
|
1133
|
+
retryPendingToolCalls.clear();
|
|
1134
|
+
if (r.session_id) {
|
|
1135
|
+
if (this.isPersistent) {
|
|
1136
|
+
this._sessionId = r.session_id;
|
|
1137
|
+
}
|
|
1138
|
+
yield this.emitSessionInfo(r.session_id);
|
|
1139
|
+
}
|
|
1140
|
+
yield { type: "done", finalOutput: r.result };
|
|
1141
|
+
}
|
|
1142
|
+
}
|
|
1143
|
+
} catch (retryError) {
|
|
1144
|
+
if (this.isPersistent) this.clearPersistentSession();
|
|
1145
|
+
if (signal.aborted) throw new AbortError();
|
|
1146
|
+
throw retryError;
|
|
1147
|
+
} finally {
|
|
1148
|
+
this.activeQuery = null;
|
|
1149
|
+
}
|
|
1150
|
+
return;
|
|
1151
|
+
}
|
|
1152
|
+
if (this.isPersistent) this.clearPersistentSession();
|
|
889
1153
|
throw e;
|
|
890
1154
|
} finally {
|
|
891
1155
|
this.activeQuery = null;
|
|
@@ -905,13 +1169,43 @@ function extractLastUserPrompt(messages) {
|
|
|
905
1169
|
}
|
|
906
1170
|
return "";
|
|
907
1171
|
}
|
|
1172
|
+
function serializeToolCall(tc) {
|
|
1173
|
+
const args = typeof tc.args === "string" ? tc.args : JSON.stringify(tc.args);
|
|
1174
|
+
return ` Tool call: ${tc.name}(${args})`;
|
|
1175
|
+
}
|
|
1176
|
+
function serializeToolResult(tr) {
|
|
1177
|
+
const result = typeof tr.result === "string" ? tr.result : JSON.stringify(tr.result);
|
|
1178
|
+
const prefix = tr.isError ? "[ERROR] " : "";
|
|
1179
|
+
return ` ${tr.name} \u2192 ${prefix}${result}`;
|
|
1180
|
+
}
|
|
908
1181
|
function buildContextualPrompt(messages) {
|
|
909
1182
|
if (messages.length <= 1) {
|
|
910
1183
|
return extractLastUserPrompt(messages);
|
|
911
1184
|
}
|
|
912
1185
|
const history = messages.slice(0, -1).map((msg) => {
|
|
1186
|
+
if (msg.role === "user") {
|
|
1187
|
+
return `User: ${msg.content ? getTextContent(msg.content) : ""}`;
|
|
1188
|
+
}
|
|
1189
|
+
if (msg.role === "tool" && msg.toolResults) {
|
|
1190
|
+
const results = msg.toolResults.map(serializeToolResult).join("\n");
|
|
1191
|
+
return `Tool results:
|
|
1192
|
+
${results}`;
|
|
1193
|
+
}
|
|
1194
|
+
if (msg.role === "assistant") {
|
|
1195
|
+
const parts = [];
|
|
1196
|
+
const thinking = msg.thinking;
|
|
1197
|
+
if (thinking) {
|
|
1198
|
+
parts.push(`[reasoning: ${thinking}]`);
|
|
1199
|
+
}
|
|
1200
|
+
const text2 = msg.content ? getTextContent(msg.content) : "";
|
|
1201
|
+
if (text2) parts.push(text2);
|
|
1202
|
+
if (msg.toolCalls && msg.toolCalls.length > 0) {
|
|
1203
|
+
parts.push(msg.toolCalls.map(serializeToolCall).join("\n"));
|
|
1204
|
+
}
|
|
1205
|
+
return `Assistant: ${parts.join("\n")}`;
|
|
1206
|
+
}
|
|
913
1207
|
const text = msg.content ? getTextContent(msg.content) : "";
|
|
914
|
-
return msg.role
|
|
1208
|
+
return `${msg.role}: ${text}`;
|
|
915
1209
|
}).join("\n");
|
|
916
1210
|
const lastPrompt = extractLastUserPrompt(messages);
|
|
917
1211
|
return `Conversation history:
|