@slopus/happy-providers 0.0.1 → 0.0.5
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/EXAMPLES.md +42 -74
- package/README.md +176 -100
- package/dist/core/BaseSession.d.ts +3 -2
- package/dist/core/BaseSession.d.ts.map +1 -1
- package/dist/core/BaseSession.js.map +1 -1
- package/dist/core/EmptyResponseError.d.ts +3 -3
- package/dist/core/EmptyResponseError.d.ts.map +1 -1
- package/dist/core/EmptyResponseError.js.map +1 -1
- package/dist/core/SessionAssistantMessageAccumulator.d.ts +17 -0
- package/dist/core/SessionAssistantMessageAccumulator.d.ts.map +1 -0
- package/dist/core/SessionAssistantMessageAccumulator.js +154 -0
- package/dist/core/SessionAssistantMessageAccumulator.js.map +1 -0
- package/dist/core/SessionCompaction.d.ts +4 -11
- package/dist/core/SessionCompaction.d.ts.map +1 -1
- package/dist/core/SessionContext.d.ts +35 -33
- package/dist/core/SessionContext.d.ts.map +1 -1
- package/dist/core/SessionEvent.d.ts +23 -8
- package/dist/core/SessionEvent.d.ts.map +1 -1
- package/dist/core/SessionEvent.js.map +1 -1
- package/dist/core/SessionRunRequest.d.ts +2 -5
- package/dist/core/SessionRunRequest.d.ts.map +1 -1
- package/dist/core/SessionTool.d.ts +1 -3
- package/dist/core/SessionTool.d.ts.map +1 -1
- package/dist/core/SessionUsage.d.ts +14 -0
- package/dist/core/SessionUsage.d.ts.map +1 -0
- package/dist/core/{SessionCacheUsage.js → SessionUsage.js} +2 -2
- package/dist/core/SessionUsage.js.map +1 -0
- package/dist/core/addSessionUsage.d.ts +3 -0
- package/dist/core/addSessionUsage.d.ts.map +1 -0
- package/dist/core/{addSessionCacheUsage.js → addSessionUsage.js} +3 -3
- package/dist/core/addSessionUsage.js.map +1 -0
- package/dist/core/emitToolCallResult.d.ts +1 -0
- package/dist/core/emitToolCallResult.d.ts.map +1 -1
- package/dist/core/emitToolCallResult.js +6 -2
- package/dist/core/emitToolCallResult.js.map +1 -1
- package/dist/core/toSessionReminderMessage.d.ts.map +1 -1
- package/dist/core/toSessionReminderMessage.js +6 -2
- package/dist/core/toSessionReminderMessage.js.map +1 -1
- package/dist/index.d.ts +6 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/protocol/anthropic/collectAnthropicCompaction.d.ts +2 -2
- package/dist/protocol/anthropic/collectAnthropicCompaction.d.ts.map +1 -1
- package/dist/protocol/anthropic/collectAnthropicCompaction.js +14 -9
- package/dist/protocol/anthropic/collectAnthropicCompaction.js.map +1 -1
- package/dist/protocol/anthropic/mapAnthropicStream.d.ts.map +1 -1
- package/dist/protocol/anthropic/mapAnthropicStream.js +52 -30
- package/dist/protocol/anthropic/mapAnthropicStream.js.map +1 -1
- package/dist/protocol/anthropic/toAnthropicMessages.d.ts +1 -2
- package/dist/protocol/anthropic/toAnthropicMessages.d.ts.map +1 -1
- package/dist/protocol/anthropic/toAnthropicMessages.js +46 -108
- package/dist/protocol/anthropic/toAnthropicMessages.js.map +1 -1
- package/dist/protocol/responses/ResponsesSession.d.ts +3 -2
- package/dist/protocol/responses/ResponsesSession.d.ts.map +1 -1
- package/dist/protocol/responses/ResponsesSession.js +19 -48
- package/dist/protocol/responses/ResponsesSession.js.map +1 -1
- package/dist/protocol/responses/mapOpenAIResponseStream.d.ts +9 -6
- package/dist/protocol/responses/mapOpenAIResponseStream.d.ts.map +1 -1
- package/dist/protocol/responses/mapOpenAIResponseStream.js +191 -59
- package/dist/protocol/responses/mapOpenAIResponseStream.js.map +1 -1
- package/dist/protocol/responses/toOpenAIInputContent.d.ts +2 -2
- package/dist/protocol/responses/toOpenAIInputContent.d.ts.map +1 -1
- package/dist/protocol/responses/toOpenAIInputContent.js +4 -4
- package/dist/protocol/responses/toOpenAIInputContent.js.map +1 -1
- package/dist/protocol/responses/toOpenAIResponseInput.d.ts.map +1 -1
- package/dist/protocol/responses/toOpenAIResponseInput.js +74 -68
- package/dist/protocol/responses/toOpenAIResponseInput.js.map +1 -1
- package/dist/protocol/responses/toSessionUsage.d.ts +4 -0
- package/dist/protocol/responses/toSessionUsage.d.ts.map +1 -0
- package/dist/protocol/responses/{toSessionCacheUsage.js → toSessionUsage.js} +4 -4
- package/dist/protocol/responses/toSessionUsage.js.map +1 -0
- package/dist/vendors/bedrock/AnthropicBedrockSession.d.ts +3 -2
- package/dist/vendors/bedrock/AnthropicBedrockSession.d.ts.map +1 -1
- package/dist/vendors/bedrock/AnthropicBedrockSession.js +34 -73
- package/dist/vendors/bedrock/AnthropicBedrockSession.js.map +1 -1
- package/dist/vendors/claude/ClaudeSession.d.ts +3 -2
- package/dist/vendors/claude/ClaudeSession.d.ts.map +1 -1
- package/dist/vendors/claude/ClaudeSession.js +180 -82
- package/dist/vendors/claude/ClaudeSession.js.map +1 -1
- package/dist/vendors/claude/impl/ClaudeToolBridge.js +1 -7
- package/dist/vendors/claude/impl/ClaudeToolBridge.js.map +1 -1
- package/dist/vendors/claude/impl/claudeMessageIdentity.d.ts +3 -4
- package/dist/vendors/claude/impl/claudeMessageIdentity.d.ts.map +1 -1
- package/dist/vendors/claude/impl/claudeMessageIdentity.js +9 -12
- package/dist/vendors/claude/impl/claudeMessageIdentity.js.map +1 -1
- package/dist/vendors/claude/impl/createClaudeSessionReplay.js +36 -30
- package/dist/vendors/claude/impl/createClaudeSessionReplay.js.map +1 -1
- package/dist/vendors/claude/impl/toClaudeSdkOptions.d.ts.map +1 -1
- package/dist/vendors/claude/impl/toClaudeSdkOptions.js +2 -2
- package/dist/vendors/claude/impl/toClaudeSdkOptions.js.map +1 -1
- package/dist/vendors/codex/CodexSession.d.ts +3 -2
- package/dist/vendors/codex/CodexSession.d.ts.map +1 -1
- package/dist/vendors/codex/CodexSession.js +45 -65
- package/dist/vendors/codex/CodexSession.js.map +1 -1
- package/dist/vendors/codex/CodexToolVendor.d.ts +2 -1
- package/dist/vendors/codex/CodexToolVendor.d.ts.map +1 -1
- package/dist/vendors/codex/impl/codexCompaction.d.ts +2 -2
- package/dist/vendors/codex/impl/codexCompaction.d.ts.map +1 -1
- package/dist/vendors/codex/impl/codexCompaction.js +15 -7
- package/dist/vendors/codex/impl/codexCompaction.js.map +1 -1
- package/dist/vendors/codex/impl/createCodexModelSwitchMessage.d.ts.map +1 -1
- package/dist/vendors/codex/impl/createCodexModelSwitchMessage.js +11 -8
- package/dist/vendors/codex/impl/createCodexModelSwitchMessage.js.map +1 -1
- package/dist/vendors/codex/impl/getCodexContextSuffix.js +13 -15
- package/dist/vendors/codex/impl/getCodexContextSuffix.js.map +1 -1
- package/dist/vendors/codex/impl/getCodexIncrementalInput.d.ts +2 -2
- package/dist/vendors/codex/impl/getCodexIncrementalInput.d.ts.map +1 -1
- package/dist/vendors/codex/impl/getCodexIncrementalInput.js +15 -4
- package/dist/vendors/codex/impl/getCodexIncrementalInput.js.map +1 -1
- package/dist/vendors/codex/impl/getCodexTurnKey.js +1 -1
- package/dist/vendors/codex/impl/getCodexTurnKey.js.map +1 -1
- package/dist/vendors/codex/impl/settleCodexToolSearch.d.ts +4 -3
- package/dist/vendors/codex/impl/settleCodexToolSearch.d.ts.map +1 -1
- package/dist/vendors/codex/impl/settleCodexToolSearch.js +14 -2
- package/dist/vendors/codex/impl/settleCodexToolSearch.js.map +1 -1
- package/dist/vendors/codex/impl/toCodexToolDefinitions.d.ts.map +1 -1
- package/dist/vendors/codex/impl/toCodexToolDefinitions.js +6 -27
- package/dist/vendors/codex/impl/toCodexToolDefinitions.js.map +1 -1
- package/dist/vendors/codex/impl/withCodexSkills.d.ts.map +1 -1
- package/dist/vendors/codex/impl/withCodexSkills.js +8 -12
- package/dist/vendors/codex/impl/withCodexSkills.js.map +1 -1
- package/dist/vendors/codex/tools/tool_search.d.ts +9 -5
- package/dist/vendors/codex/tools/tool_search.d.ts.map +1 -1
- package/dist/vendors/codex/tools/tool_search.js +8 -10
- package/dist/vendors/codex/tools/tool_search.js.map +1 -1
- package/dist/vendors/grok/GrokSession.d.ts +3 -2
- package/dist/vendors/grok/GrokSession.d.ts.map +1 -1
- package/dist/vendors/grok/GrokSession.js +26 -48
- package/dist/vendors/grok/GrokSession.js.map +1 -1
- package/dist/vendors/grok/impl/grokMessages.js +11 -5
- package/dist/vendors/grok/impl/grokMessages.js.map +1 -1
- package/dist/vendors/grok/impl/stripGrokContextImages.d.ts.map +1 -1
- package/dist/vendors/grok/impl/stripGrokContextImages.js +3 -3
- package/dist/vendors/grok/impl/stripGrokContextImages.js.map +1 -1
- package/dist/vendors/grok/impl/toGrokInputContent.d.ts +2 -2
- package/dist/vendors/grok/impl/toGrokInputContent.d.ts.map +1 -1
- package/dist/vendors/grok/impl/toGrokInputContent.js +4 -4
- package/dist/vendors/grok/impl/toGrokInputContent.js.map +1 -1
- package/dist/vendors/grok/impl/toGrokResponseInput.d.ts.map +1 -1
- package/dist/vendors/grok/impl/toGrokResponseInput.js +78 -70
- package/dist/vendors/grok/impl/toGrokResponseInput.js.map +1 -1
- package/package.json +2 -1
- package/dist/core/SessionCacheUsage.d.ts +0 -9
- package/dist/core/SessionCacheUsage.d.ts.map +0 -1
- package/dist/core/SessionCacheUsage.js.map +0 -1
- package/dist/core/addSessionCacheUsage.d.ts +0 -3
- package/dist/core/addSessionCacheUsage.d.ts.map +0 -1
- package/dist/core/addSessionCacheUsage.js.map +0 -1
- package/dist/protocol/responses/toSessionCacheUsage.d.ts +0 -4
- package/dist/protocol/responses/toSessionCacheUsage.d.ts.map +0 -1
- package/dist/protocol/responses/toSessionCacheUsage.js.map +0 -1
|
@@ -3,7 +3,8 @@ import { query as defaultClaudeSdkQuery, } from "@anthropic-ai/claude-agent-sdk"
|
|
|
3
3
|
import { BaseSession } from "../../core/BaseSession.js";
|
|
4
4
|
import { createInferenceMaxRetriesResolver, } from "../../core/inferenceRetrySettings.js";
|
|
5
5
|
import { EmptyResponseError, emptyResponseDoneEvent } from "../../core/EmptyResponseError.js";
|
|
6
|
-
import {
|
|
6
|
+
import { EMPTY_SESSION_USAGE } from "../../core/SessionUsage.js";
|
|
7
|
+
import { SessionAssistantMessageAccumulator } from "../../core/SessionAssistantMessageAccumulator.js";
|
|
7
8
|
import { waitForInferenceRetry } from "../../core/waitForInferenceRetry.js";
|
|
8
9
|
import { resolveClaudeModelId } from "../../vendors/claude/impl/resolveClaudeModelId.js";
|
|
9
10
|
import { claudeUsageFromRateLimitInfo } from "../../vendors/claude/claudeUsageFromRateLimitInfo.js";
|
|
@@ -82,19 +83,18 @@ export class ClaudeSession extends BaseSession {
|
|
|
82
83
|
// A usage listener never decides whether inference continues.
|
|
83
84
|
}
|
|
84
85
|
}
|
|
85
|
-
run(request) {
|
|
86
|
-
if (
|
|
86
|
+
run(ctx, request) {
|
|
87
|
+
if (ctx.lifetime?.aborted)
|
|
87
88
|
return emptyStream();
|
|
88
|
-
return this.streamRun(request);
|
|
89
|
+
return this.streamRun(ctx, request);
|
|
89
90
|
}
|
|
90
|
-
async compact(options
|
|
91
|
-
const original =
|
|
92
|
-
|
|
93
|
-
:
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
const { instructions, signal } = options;
|
|
91
|
+
async compact(ctx, options) {
|
|
92
|
+
const original = {
|
|
93
|
+
instructions: options.context.instructions,
|
|
94
|
+
messages: [...options.context.messages],
|
|
95
|
+
};
|
|
96
|
+
const { instructions } = options;
|
|
97
|
+
const signal = ctx.lifetime;
|
|
98
98
|
if (signal?.aborted)
|
|
99
99
|
return { status: "cancelled", context: original };
|
|
100
100
|
const requestedModel = options.model ?? this.activeModel ?? this.model;
|
|
@@ -108,9 +108,14 @@ export class ClaudeSession extends BaseSession {
|
|
|
108
108
|
...original.messages,
|
|
109
109
|
{
|
|
110
110
|
role: "user",
|
|
111
|
-
content:
|
|
112
|
-
|
|
113
|
-
|
|
111
|
+
content: [
|
|
112
|
+
{
|
|
113
|
+
type: "text",
|
|
114
|
+
text: instructions === undefined || instructions.trim().length === 0
|
|
115
|
+
? "/compact"
|
|
116
|
+
: `/compact ${instructions}`,
|
|
117
|
+
},
|
|
118
|
+
],
|
|
114
119
|
},
|
|
115
120
|
],
|
|
116
121
|
};
|
|
@@ -138,7 +143,6 @@ export class ClaudeSession extends BaseSession {
|
|
|
138
143
|
status: "failed",
|
|
139
144
|
kind: "tool_call",
|
|
140
145
|
message: "Claude attempted to call a tool while compacting.",
|
|
141
|
-
context: original,
|
|
142
146
|
};
|
|
143
147
|
}
|
|
144
148
|
if (done?.state === "error") {
|
|
@@ -146,7 +150,6 @@ export class ClaudeSession extends BaseSession {
|
|
|
146
150
|
status: "failed",
|
|
147
151
|
kind: "inference_error",
|
|
148
152
|
message: done.message,
|
|
149
|
-
context: original,
|
|
150
153
|
};
|
|
151
154
|
}
|
|
152
155
|
if (summary.trim().length === 0) {
|
|
@@ -154,7 +157,6 @@ export class ClaudeSession extends BaseSession {
|
|
|
154
157
|
status: "failed",
|
|
155
158
|
kind: "invalid_summary",
|
|
156
159
|
message: "Claude returned an empty compaction summary.",
|
|
157
|
-
context: original,
|
|
158
160
|
};
|
|
159
161
|
}
|
|
160
162
|
if (usage === undefined) {
|
|
@@ -162,13 +164,15 @@ export class ClaudeSession extends BaseSession {
|
|
|
162
164
|
status: "failed",
|
|
163
165
|
kind: "inference_error",
|
|
164
166
|
message: "Claude completed compaction without reporting token usage.",
|
|
165
|
-
context: original,
|
|
166
167
|
};
|
|
167
168
|
}
|
|
168
169
|
const preservedMessages = original.messages.filter((message) => message.role === "system");
|
|
169
170
|
this.context = {
|
|
170
171
|
instructions: original.instructions,
|
|
171
|
-
messages: [
|
|
172
|
+
messages: [
|
|
173
|
+
...preservedMessages,
|
|
174
|
+
{ role: "user", content: [{ type: "text", text: summary }] },
|
|
175
|
+
],
|
|
172
176
|
};
|
|
173
177
|
return {
|
|
174
178
|
status: "completed",
|
|
@@ -181,7 +185,8 @@ export class ClaudeSession extends BaseSession {
|
|
|
181
185
|
destroy() {
|
|
182
186
|
this.closeActiveQuery();
|
|
183
187
|
}
|
|
184
|
-
async *streamRun(request) {
|
|
188
|
+
async *streamRun(ctx, request) {
|
|
189
|
+
const signal = ctx.lifetime;
|
|
185
190
|
const requestedModel = request.model ?? this.activeModel ?? this.model;
|
|
186
191
|
const model = requestedModel === undefined ? undefined : resolveClaudeModelId(requestedModel);
|
|
187
192
|
if (model === undefined)
|
|
@@ -190,15 +195,13 @@ export class ClaudeSession extends BaseSession {
|
|
|
190
195
|
const effort = request.effort ?? this.activeEffort;
|
|
191
196
|
this.activeEffort = effort;
|
|
192
197
|
this.context = {
|
|
193
|
-
instructions: this.context.instructions,
|
|
198
|
+
instructions: request.context.instructions ?? this.context.instructions,
|
|
194
199
|
messages: [...request.context.messages],
|
|
195
200
|
};
|
|
196
201
|
let emptyResponseRetries = 0;
|
|
197
202
|
let completedMidResponseAttempts = 0;
|
|
198
203
|
for (;;) {
|
|
199
|
-
|
|
200
|
-
let reasoningText = "";
|
|
201
|
-
let reasoning = [];
|
|
204
|
+
const assistant = new SessionAssistantMessageAccumulator();
|
|
202
205
|
let usage;
|
|
203
206
|
let blockStopped = false;
|
|
204
207
|
let terminal;
|
|
@@ -206,22 +209,13 @@ export class ClaudeSession extends BaseSession {
|
|
|
206
209
|
context: this.context,
|
|
207
210
|
model,
|
|
208
211
|
...(effort === undefined ? {} : { effort }),
|
|
209
|
-
...(
|
|
212
|
+
...(signal === undefined ? {} : { abort: signal }),
|
|
210
213
|
...(request.structuredOutput === undefined
|
|
211
214
|
? {}
|
|
212
215
|
: { structuredOutput: request.structuredOutput }),
|
|
213
216
|
maxRetries: Math.max(0, this.resolveInferenceMaxRetries() - completedMidResponseAttempts),
|
|
214
217
|
})) {
|
|
215
|
-
|
|
216
|
-
assistantText += event.delta;
|
|
217
|
-
if (event.type === "reasoning_delta")
|
|
218
|
-
reasoningText += event.delta;
|
|
219
|
-
// The signature closes the thinking block it was issued for, so the pair is
|
|
220
|
-
// banked together and the buffer reopens for whatever the model reasons about.
|
|
221
|
-
if (event.type === "encrypted_reasoning") {
|
|
222
|
-
reasoning = [...reasoning, { text: reasoningText, signature: event.content }];
|
|
223
|
-
reasoningText = "";
|
|
224
|
-
}
|
|
218
|
+
assistant.add(event);
|
|
225
219
|
if (event.type === "token_usage") {
|
|
226
220
|
usage = event;
|
|
227
221
|
continue;
|
|
@@ -234,11 +228,6 @@ export class ClaudeSession extends BaseSession {
|
|
|
234
228
|
terminal = event;
|
|
235
229
|
continue;
|
|
236
230
|
}
|
|
237
|
-
if (event.type === "block_reset") {
|
|
238
|
-
assistantText = "";
|
|
239
|
-
reasoningText = "";
|
|
240
|
-
reasoning = [];
|
|
241
|
-
}
|
|
242
231
|
yield event;
|
|
243
232
|
}
|
|
244
233
|
if (terminal === undefined)
|
|
@@ -258,10 +247,10 @@ export class ClaudeSession extends BaseSession {
|
|
|
258
247
|
reason: "Claude's response was interrupted by a server error.",
|
|
259
248
|
};
|
|
260
249
|
try {
|
|
261
|
-
await this.retryWait(completedMidResponseAttempts,
|
|
250
|
+
await this.retryWait(completedMidResponseAttempts, signal);
|
|
262
251
|
}
|
|
263
252
|
catch (delayError) {
|
|
264
|
-
if (
|
|
253
|
+
if (signal?.aborted) {
|
|
265
254
|
yield { type: "done", state: "cancelled" };
|
|
266
255
|
return;
|
|
267
256
|
}
|
|
@@ -288,10 +277,10 @@ export class ClaudeSession extends BaseSession {
|
|
|
288
277
|
reason: error.message,
|
|
289
278
|
};
|
|
290
279
|
try {
|
|
291
|
-
await this.retryWait(emptyResponseRetries,
|
|
280
|
+
await this.retryWait(emptyResponseRetries, signal);
|
|
292
281
|
}
|
|
293
282
|
catch (delayError) {
|
|
294
|
-
if (
|
|
283
|
+
if (signal?.aborted) {
|
|
295
284
|
yield { type: "done", state: "cancelled" };
|
|
296
285
|
return;
|
|
297
286
|
}
|
|
@@ -305,25 +294,20 @@ export class ClaudeSession extends BaseSession {
|
|
|
305
294
|
if (usage !== undefined)
|
|
306
295
|
yield usage;
|
|
307
296
|
if (terminal.state !== "error" && terminal.state !== "cancelled") {
|
|
308
|
-
const assistantMessage =
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
if (this.sentConversation !== undefined) {
|
|
323
|
-
this.sentConversation = [
|
|
324
|
-
...this.sentConversation,
|
|
325
|
-
claudeMessageIdentity(assistantMessage),
|
|
326
|
-
];
|
|
297
|
+
const assistantMessage = assistant.message();
|
|
298
|
+
if (assistantMessage !== undefined) {
|
|
299
|
+
this.context = {
|
|
300
|
+
instructions: this.context.instructions,
|
|
301
|
+
messages: [...this.context.messages, assistantMessage],
|
|
302
|
+
};
|
|
303
|
+
// The query generated this turn, so it holds it even though no caller sent it.
|
|
304
|
+
// Leaving it out would let a later edit of this message look like an append.
|
|
305
|
+
if (this.sentConversation !== undefined) {
|
|
306
|
+
this.sentConversation = [
|
|
307
|
+
...this.sentConversation,
|
|
308
|
+
claudeMessageIdentity(assistantMessage),
|
|
309
|
+
];
|
|
310
|
+
}
|
|
327
311
|
}
|
|
328
312
|
}
|
|
329
313
|
if (blockStopped)
|
|
@@ -402,6 +386,8 @@ export class ClaudeSession extends BaseSession {
|
|
|
402
386
|
};
|
|
403
387
|
options.abort?.addEventListener("abort", abort, { once: true });
|
|
404
388
|
const activeTools = new Map();
|
|
389
|
+
const completedTools = [];
|
|
390
|
+
const activeOutputBlocks = new Map();
|
|
405
391
|
// Claude Code runs a server tool inside its own process and answers it there, so Rig
|
|
406
392
|
// reports these calls without ever collecting them as work for the executor.
|
|
407
393
|
const serverToolNames = new Set(claudeSdkBuiltInToolNames(tools));
|
|
@@ -415,7 +401,7 @@ export class ClaudeSession extends BaseSession {
|
|
|
415
401
|
let rateLimitInfo;
|
|
416
402
|
let requestId;
|
|
417
403
|
let attempts = 1;
|
|
418
|
-
let usage = { ...
|
|
404
|
+
let usage = { ...EMPTY_SESSION_USAGE };
|
|
419
405
|
let sawInferenceUsage = false;
|
|
420
406
|
try {
|
|
421
407
|
if (!continuingQuery) {
|
|
@@ -519,6 +505,41 @@ export class ClaudeSession extends BaseSession {
|
|
|
519
505
|
usage = toUsage(event.message.usage);
|
|
520
506
|
sawInferenceUsage = typeof event.message.usage.output_tokens === "number";
|
|
521
507
|
}
|
|
508
|
+
if (event.type === "content_block_start" &&
|
|
509
|
+
event.content_block.type === "text" &&
|
|
510
|
+
options.structuredOutput === undefined) {
|
|
511
|
+
activeOutputBlocks.set(event.index, { type: "text" });
|
|
512
|
+
sawText = true;
|
|
513
|
+
yield { type: "text_start" };
|
|
514
|
+
if (event.content_block.text.length > 0) {
|
|
515
|
+
yield { type: "text_delta", delta: event.content_block.text };
|
|
516
|
+
}
|
|
517
|
+
continue;
|
|
518
|
+
}
|
|
519
|
+
if (event.type === "content_block_start" &&
|
|
520
|
+
event.content_block.type === "thinking") {
|
|
521
|
+
activeOutputBlocks.set(event.index, {
|
|
522
|
+
type: "reasoning",
|
|
523
|
+
reasoning: event.content_block.signature,
|
|
524
|
+
});
|
|
525
|
+
yield { type: "reasoning_start" };
|
|
526
|
+
if (event.content_block.thinking.length > 0) {
|
|
527
|
+
yield {
|
|
528
|
+
type: "reasoning_delta",
|
|
529
|
+
delta: event.content_block.thinking,
|
|
530
|
+
};
|
|
531
|
+
}
|
|
532
|
+
continue;
|
|
533
|
+
}
|
|
534
|
+
if (event.type === "content_block_start" &&
|
|
535
|
+
event.content_block.type === "redacted_thinking") {
|
|
536
|
+
activeOutputBlocks.set(event.index, {
|
|
537
|
+
type: "reasoning",
|
|
538
|
+
reasoning: event.content_block.data,
|
|
539
|
+
});
|
|
540
|
+
yield { type: "reasoning_start" };
|
|
541
|
+
continue;
|
|
542
|
+
}
|
|
522
543
|
if (event.type === "content_block_start" &&
|
|
523
544
|
(event.content_block.type === "tool_use" ||
|
|
524
545
|
event.content_block.type === "server_tool_use")) {
|
|
@@ -542,6 +563,7 @@ export class ClaudeSession extends BaseSession {
|
|
|
542
563
|
name: event.content_block.name,
|
|
543
564
|
arguments: "",
|
|
544
565
|
vendor: { type: "claude_tool_use" },
|
|
566
|
+
...(server ? { server: true } : {}),
|
|
545
567
|
});
|
|
546
568
|
yield {
|
|
547
569
|
type: "toolcall_start",
|
|
@@ -573,14 +595,31 @@ export class ClaudeSession extends BaseSession {
|
|
|
573
595
|
if (event.type === "content_block_delta") {
|
|
574
596
|
if (event.delta.type === "text_delta" &&
|
|
575
597
|
options.structuredOutput === undefined) {
|
|
598
|
+
if (activeOutputBlocks.get(event.index)?.type !== "text") {
|
|
599
|
+
activeOutputBlocks.set(event.index, { type: "text" });
|
|
600
|
+
yield { type: "text_start" };
|
|
601
|
+
}
|
|
576
602
|
sawText = true;
|
|
577
603
|
yield { type: "text_delta", delta: event.delta.text };
|
|
578
604
|
}
|
|
579
605
|
else if (event.delta.type === "thinking_delta") {
|
|
606
|
+
if (activeOutputBlocks.get(event.index)?.type !== "reasoning") {
|
|
607
|
+
activeOutputBlocks.set(event.index, {
|
|
608
|
+
type: "reasoning",
|
|
609
|
+
reasoning: "",
|
|
610
|
+
});
|
|
611
|
+
yield { type: "reasoning_start" };
|
|
612
|
+
}
|
|
580
613
|
yield { type: "reasoning_delta", delta: event.delta.thinking };
|
|
581
614
|
}
|
|
582
615
|
else if (event.delta.type === "signature_delta") {
|
|
583
|
-
|
|
616
|
+
let block = activeOutputBlocks.get(event.index);
|
|
617
|
+
if (block?.type !== "reasoning") {
|
|
618
|
+
block = { type: "reasoning", reasoning: "" };
|
|
619
|
+
activeOutputBlocks.set(event.index, block);
|
|
620
|
+
yield { type: "reasoning_start" };
|
|
621
|
+
}
|
|
622
|
+
block.reasoning += event.delta.signature;
|
|
584
623
|
}
|
|
585
624
|
else if (event.delta.type === "input_json_delta") {
|
|
586
625
|
const block = activeTools.get(event.index);
|
|
@@ -604,6 +643,20 @@ export class ClaudeSession extends BaseSession {
|
|
|
604
643
|
}
|
|
605
644
|
}
|
|
606
645
|
if (event.type === "content_block_stop") {
|
|
646
|
+
const outputBlock = activeOutputBlocks.get(event.index);
|
|
647
|
+
if (outputBlock?.type === "text") {
|
|
648
|
+
yield { type: "text_end" };
|
|
649
|
+
activeOutputBlocks.delete(event.index);
|
|
650
|
+
}
|
|
651
|
+
else if (outputBlock?.type === "reasoning") {
|
|
652
|
+
yield {
|
|
653
|
+
type: "reasoning_end",
|
|
654
|
+
...(outputBlock.reasoning.length === 0
|
|
655
|
+
? {}
|
|
656
|
+
: { reasoning: outputBlock.reasoning }),
|
|
657
|
+
};
|
|
658
|
+
activeOutputBlocks.delete(event.index);
|
|
659
|
+
}
|
|
607
660
|
const block = activeTools.get(event.index);
|
|
608
661
|
if (block !== undefined) {
|
|
609
662
|
yield {
|
|
@@ -611,6 +664,8 @@ export class ClaudeSession extends BaseSession {
|
|
|
611
664
|
callId: block.callId,
|
|
612
665
|
arguments: block.arguments,
|
|
613
666
|
};
|
|
667
|
+
completedTools.push(block);
|
|
668
|
+
activeTools.delete(event.index);
|
|
614
669
|
// Claude Code answers built-ins out of band and does not stream their
|
|
615
670
|
// results as content blocks here. A later text block is the model's
|
|
616
671
|
// synthesis, not the tool payload, so server calls end without a
|
|
@@ -623,19 +678,29 @@ export class ClaudeSession extends BaseSession {
|
|
|
623
678
|
isClaudeServerToolResultBlock(event.content_block)) {
|
|
624
679
|
const callId = event.content_block.tool_use_id;
|
|
625
680
|
const result = JSON.stringify(event.content_block.content ?? null);
|
|
626
|
-
|
|
681
|
+
const vendor = { outputBlock: JSON.stringify(event.content_block) };
|
|
682
|
+
yield { type: "toolcall_result_start", callId, vendor };
|
|
627
683
|
if (result.length > 0) {
|
|
628
684
|
yield { type: "toolcall_result_delta", callId, delta: result };
|
|
629
685
|
}
|
|
630
|
-
yield {
|
|
686
|
+
yield {
|
|
687
|
+
type: "toolcall_result_end",
|
|
688
|
+
callId,
|
|
689
|
+
content: [{ type: "text", text: result }],
|
|
690
|
+
};
|
|
631
691
|
continue;
|
|
632
692
|
}
|
|
633
693
|
if (event.type === "message_stop" && sawToolCall) {
|
|
634
|
-
|
|
694
|
+
yield* closeClaudeOutputBlocks(activeOutputBlocks);
|
|
695
|
+
this.lastQueryToolCalls = completedTools.filter((call) => call.server !== true);
|
|
635
696
|
if (sawInferenceUsage)
|
|
636
697
|
yield { type: "token_usage", usage };
|
|
637
698
|
yield { type: "block_stop" };
|
|
638
|
-
yield {
|
|
699
|
+
yield {
|
|
700
|
+
type: "done",
|
|
701
|
+
state: "tool_call",
|
|
702
|
+
tokens: { input: usage.input, output: usage.output },
|
|
703
|
+
};
|
|
639
704
|
return;
|
|
640
705
|
}
|
|
641
706
|
continue;
|
|
@@ -659,10 +724,16 @@ export class ClaudeSession extends BaseSession {
|
|
|
659
724
|
throw new Error("Claude SDK compacted the session without persisting a summary.");
|
|
660
725
|
}
|
|
661
726
|
this.closeActiveQuery();
|
|
727
|
+
yield { type: "text_start" };
|
|
662
728
|
yield { type: "text_delta", delta: summary };
|
|
729
|
+
yield { type: "text_end" };
|
|
663
730
|
if (sawInferenceUsage)
|
|
664
731
|
yield { type: "token_usage", usage };
|
|
665
|
-
yield {
|
|
732
|
+
yield {
|
|
733
|
+
type: "done",
|
|
734
|
+
state: "normal",
|
|
735
|
+
tokens: { input: usage.input, output: usage.output },
|
|
736
|
+
};
|
|
666
737
|
return;
|
|
667
738
|
}
|
|
668
739
|
if (result === undefined && !sawToolCall) {
|
|
@@ -684,13 +755,19 @@ export class ClaudeSession extends BaseSession {
|
|
|
684
755
|
if (result.structured_output === undefined) {
|
|
685
756
|
throw new Error("Claude completed structured output without returning a value.");
|
|
686
757
|
}
|
|
758
|
+
yield {
|
|
759
|
+
type: "text_start",
|
|
760
|
+
};
|
|
687
761
|
yield {
|
|
688
762
|
type: "text_delta",
|
|
689
763
|
delta: JSON.stringify(result.structured_output),
|
|
690
764
|
};
|
|
765
|
+
yield { type: "text_end" };
|
|
691
766
|
}
|
|
692
767
|
else if (!sawText && result.subtype === "success" && result.result.length > 0) {
|
|
768
|
+
yield { type: "text_start" };
|
|
693
769
|
yield { type: "text_delta", delta: result.result };
|
|
770
|
+
yield { type: "text_end" };
|
|
694
771
|
}
|
|
695
772
|
if (result.subtype !== "success" || result.is_error) {
|
|
696
773
|
const message = result.subtype === "success"
|
|
@@ -720,11 +797,16 @@ export class ClaudeSession extends BaseSession {
|
|
|
720
797
|
return;
|
|
721
798
|
}
|
|
722
799
|
}
|
|
723
|
-
this.lastQueryToolCalls =
|
|
800
|
+
this.lastQueryToolCalls = completedTools.filter((call) => call.server !== true);
|
|
801
|
+
yield* closeClaudeOutputBlocks(activeOutputBlocks);
|
|
724
802
|
if (sawInferenceUsage)
|
|
725
803
|
yield { type: "token_usage", usage };
|
|
726
804
|
yield { type: "block_stop" };
|
|
727
|
-
yield {
|
|
805
|
+
yield {
|
|
806
|
+
type: "done",
|
|
807
|
+
state: sawToolCall ? "tool_call" : "normal",
|
|
808
|
+
tokens: { input: usage.input, output: usage.output },
|
|
809
|
+
};
|
|
728
810
|
}
|
|
729
811
|
catch (error) {
|
|
730
812
|
if (options.abort?.aborted)
|
|
@@ -777,45 +859,47 @@ async function nextClaudeMessage(stream, aborted, signal) {
|
|
|
777
859
|
return signal?.aborted ? CLAUDE_QUERY_ABORTED : Promise.race([stream.next(), aborted]);
|
|
778
860
|
}
|
|
779
861
|
function toUsage(usage) {
|
|
780
|
-
const input = usage.input_tokens ?? 0;
|
|
781
|
-
const output = usage.output_tokens ?? 0;
|
|
782
862
|
const cacheRead = usage.cache_read_input_tokens ?? 0;
|
|
783
863
|
const cacheWrite = usage.cache_creation_input_tokens ?? 0;
|
|
864
|
+
const input = (usage.input_tokens ?? 0) + cacheRead + cacheWrite;
|
|
865
|
+
const output = usage.output_tokens ?? 0;
|
|
784
866
|
return {
|
|
785
867
|
input,
|
|
786
868
|
output,
|
|
787
869
|
cacheRead,
|
|
788
870
|
cacheWrite,
|
|
789
|
-
totalTokens: input + output
|
|
871
|
+
totalTokens: input + output,
|
|
790
872
|
};
|
|
791
873
|
}
|
|
792
874
|
function mergeUsage(current, update) {
|
|
793
|
-
const input = update.input_tokens ?? current.input;
|
|
794
|
-
const output = update.output_tokens ?? current.output;
|
|
795
875
|
const cacheRead = update.cache_read_input_tokens ?? current.cacheRead;
|
|
796
876
|
const cacheWrite = update.cache_creation_input_tokens ?? current.cacheWrite;
|
|
877
|
+
const input = update.input_tokens === undefined || update.input_tokens === null
|
|
878
|
+
? current.input
|
|
879
|
+
: update.input_tokens + cacheRead + cacheWrite;
|
|
880
|
+
const output = update.output_tokens ?? current.output;
|
|
797
881
|
return {
|
|
798
882
|
input,
|
|
799
883
|
output,
|
|
800
884
|
cacheRead,
|
|
801
885
|
cacheWrite,
|
|
802
|
-
totalTokens: input + output
|
|
886
|
+
totalTokens: input + output,
|
|
803
887
|
};
|
|
804
888
|
}
|
|
805
889
|
function toAggregateModelUsage(modelUsage) {
|
|
806
890
|
const entries = Object.values(modelUsage);
|
|
807
891
|
if (entries.length === 0)
|
|
808
892
|
return undefined;
|
|
809
|
-
const input = entries.reduce((total, usage) => total + usage.inputTokens, 0);
|
|
810
|
-
const output = entries.reduce((total, usage) => total + usage.outputTokens, 0);
|
|
811
893
|
const cacheRead = entries.reduce((total, usage) => total + usage.cacheReadInputTokens, 0);
|
|
812
894
|
const cacheWrite = entries.reduce((total, usage) => total + usage.cacheCreationInputTokens, 0);
|
|
895
|
+
const input = entries.reduce((total, usage) => total + usage.inputTokens, 0) + cacheRead + cacheWrite;
|
|
896
|
+
const output = entries.reduce((total, usage) => total + usage.outputTokens, 0);
|
|
813
897
|
return {
|
|
814
898
|
input,
|
|
815
899
|
output,
|
|
816
900
|
cacheRead,
|
|
817
901
|
cacheWrite,
|
|
818
|
-
totalTokens: input + output
|
|
902
|
+
totalTokens: input + output,
|
|
819
903
|
};
|
|
820
904
|
}
|
|
821
905
|
function emptyStream() {
|
|
@@ -852,4 +936,18 @@ function isClaudeServerToolResultBlock(block) {
|
|
|
852
936
|
}
|
|
853
937
|
return "tool_use_id" in block && typeof block.tool_use_id === "string";
|
|
854
938
|
}
|
|
939
|
+
function* closeClaudeOutputBlocks(blocks) {
|
|
940
|
+
for (const [, block] of blocks) {
|
|
941
|
+
if (block.type === "text") {
|
|
942
|
+
yield { type: "text_end" };
|
|
943
|
+
}
|
|
944
|
+
else {
|
|
945
|
+
yield {
|
|
946
|
+
type: "reasoning_end",
|
|
947
|
+
...(block.reasoning.length === 0 ? {} : { reasoning: block.reasoning }),
|
|
948
|
+
};
|
|
949
|
+
}
|
|
950
|
+
}
|
|
951
|
+
blocks.clear();
|
|
952
|
+
}
|
|
855
953
|
//# sourceMappingURL=ClaudeSession.js.map
|