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