@slopus/happy-providers 0.0.16 → 0.0.18
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 +8 -0
- package/dist/core/ProviderUsage.d.ts +9 -3
- package/dist/core/ProviderUsage.d.ts.map +1 -1
- package/dist/core/SessionAssistantMessageAccumulator.d.ts.map +1 -1
- package/dist/core/SessionAssistantMessageAccumulator.js +2 -0
- package/dist/core/SessionAssistantMessageAccumulator.js.map +1 -1
- package/dist/core/SessionEvent.d.ts +2 -0
- package/dist/core/SessionEvent.d.ts.map +1 -1
- package/dist/core/SessionEvent.js.map +1 -1
- package/dist/core/SessionTool.d.ts +2 -0
- package/dist/core/SessionTool.d.ts.map +1 -1
- package/dist/protocol/anthropic/createAnthropicRequest.d.ts.map +1 -1
- package/dist/protocol/anthropic/createAnthropicRequest.js +4 -1
- package/dist/protocol/anthropic/createAnthropicRequest.js.map +1 -1
- package/dist/protocol/anthropic/mapAnthropicStream.d.ts.map +1 -1
- package/dist/protocol/anthropic/mapAnthropicStream.js +34 -10
- package/dist/protocol/anthropic/mapAnthropicStream.js.map +1 -1
- package/dist/protocol/anthropic/toAnthropicMessages.js +6 -0
- package/dist/protocol/anthropic/toAnthropicMessages.js.map +1 -1
- package/dist/protocol/anthropic/toAnthropicTools.d.ts +3 -2
- package/dist/protocol/anthropic/toAnthropicTools.d.ts.map +1 -1
- package/dist/protocol/anthropic/toAnthropicTools.js +14 -0
- package/dist/protocol/anthropic/toAnthropicTools.js.map +1 -1
- package/dist/protocol/responses/ResponsesSession.d.ts.map +1 -1
- package/dist/protocol/responses/ResponsesSession.js +15 -3
- package/dist/protocol/responses/ResponsesSession.js.map +1 -1
- package/dist/protocol/responses/mapOpenAIResponseStream.d.ts +9 -3
- package/dist/protocol/responses/mapOpenAIResponseStream.d.ts.map +1 -1
- package/dist/protocol/responses/mapOpenAIResponseStream.js +220 -35
- package/dist/protocol/responses/mapOpenAIResponseStream.js.map +1 -1
- package/dist/protocol/responses/toOpenAIResponseInput.d.ts.map +1 -1
- package/dist/protocol/responses/toOpenAIResponseInput.js +20 -1
- package/dist/protocol/responses/toOpenAIResponseInput.js.map +1 -1
- package/dist/protocol/responses/toResponsesToolDefinitions.d.ts +1 -0
- package/dist/protocol/responses/toResponsesToolDefinitions.d.ts.map +1 -1
- package/dist/protocol/responses/toResponsesToolDefinitions.js +44 -9
- package/dist/protocol/responses/toResponsesToolDefinitions.js.map +1 -1
- package/dist/vendors/claude/ClaudeSession.d.ts.map +1 -1
- package/dist/vendors/claude/ClaudeSession.js +72 -16
- package/dist/vendors/claude/ClaudeSession.js.map +1 -1
- package/dist/vendors/claude/claudeUsageFromRateLimitInfo.d.ts.map +1 -1
- package/dist/vendors/claude/claudeUsageFromRateLimitInfo.js +9 -6
- package/dist/vendors/claude/claudeUsageFromRateLimitInfo.js.map +1 -1
- package/dist/vendors/claude/fetchClaudeProviderUsage.d.ts.map +1 -1
- package/dist/vendors/claude/fetchClaudeProviderUsage.js +21 -0
- package/dist/vendors/claude/fetchClaudeProviderUsage.js.map +1 -1
- package/dist/vendors/claude/impl/createClaudeSessionReplay.d.ts.map +1 -1
- package/dist/vendors/claude/impl/createClaudeSessionReplay.js +106 -22
- package/dist/vendors/claude/impl/createClaudeSessionReplay.js.map +1 -1
- package/dist/vendors/claude/impl/toClaudeSdkOptions.d.ts +1 -1
- package/dist/vendors/claude/impl/toClaudeSdkOptions.d.ts.map +1 -1
- package/dist/vendors/claude/impl/toClaudeSdkOptions.js +22 -15
- package/dist/vendors/claude/impl/toClaudeSdkOptions.js.map +1 -1
- package/dist/vendors/codex/CodexSession.d.ts.map +1 -1
- package/dist/vendors/codex/CodexSession.js +54 -6
- package/dist/vendors/codex/CodexSession.js.map +1 -1
- package/dist/vendors/codex/impl/searchCodexTools.d.ts.map +1 -1
- package/dist/vendors/codex/impl/searchCodexTools.js +2 -0
- package/dist/vendors/codex/impl/searchCodexTools.js.map +1 -1
- package/dist/vendors/codex/impl/settleCodexToolSearch.d.ts +5 -0
- package/dist/vendors/codex/impl/settleCodexToolSearch.d.ts.map +1 -1
- package/dist/vendors/codex/impl/settleCodexToolSearch.js +12 -2
- package/dist/vendors/codex/impl/settleCodexToolSearch.js.map +1 -1
- package/dist/vendors/codex/impl/toCodexToolDefinitions.d.ts +1 -0
- package/dist/vendors/codex/impl/toCodexToolDefinitions.d.ts.map +1 -1
- package/dist/vendors/codex/impl/toCodexToolDefinitions.js +24 -19
- package/dist/vendors/codex/impl/toCodexToolDefinitions.js.map +1 -1
- package/dist/vendors/grok/impl/GrokConnection.d.ts.map +1 -1
- package/dist/vendors/grok/impl/GrokConnection.js +5 -1
- package/dist/vendors/grok/impl/GrokConnection.js.map +1 -1
- package/dist/vendors/grok/impl/toGrokToolDefinitions.d.ts.map +1 -1
- package/dist/vendors/grok/impl/toGrokToolDefinitions.js +20 -17
- package/dist/vendors/grok/impl/toGrokToolDefinitions.js.map +1 -1
- package/package.json +1 -1
|
@@ -11,6 +11,46 @@ const HOSTED_SEARCH_CALL_PREFIX = "xs_";
|
|
|
11
11
|
const completedResponseExtensionSchema = Type.Object({
|
|
12
12
|
end_turn: Type.Optional(Type.Boolean()),
|
|
13
13
|
});
|
|
14
|
+
/** Pairs nullable hosted-search result IDs with their preceding call in output order. */
|
|
15
|
+
class ServerToolSearchPairing {
|
|
16
|
+
#callIds = new Map();
|
|
17
|
+
#resultCallIds = new Map();
|
|
18
|
+
#pendingCallIds = [];
|
|
19
|
+
#scope;
|
|
20
|
+
constructor(scope) {
|
|
21
|
+
this.#scope = scope;
|
|
22
|
+
}
|
|
23
|
+
callId(item, outputIndex) {
|
|
24
|
+
const parsed = asOutputItem(item);
|
|
25
|
+
if (parsed?.call_id != null)
|
|
26
|
+
return parsed.call_id;
|
|
27
|
+
const key = parsed?.id ?? `call:${String(outputIndex)}`;
|
|
28
|
+
const existing = this.#callIds.get(key);
|
|
29
|
+
if (existing !== undefined)
|
|
30
|
+
return existing;
|
|
31
|
+
const callId = parsed?.id ?? `server_tool_call_${this.#scope}_${String(outputIndex)}`;
|
|
32
|
+
this.#callIds.set(key, callId);
|
|
33
|
+
this.#pendingCallIds.push(callId);
|
|
34
|
+
return callId;
|
|
35
|
+
}
|
|
36
|
+
resultCallId(item, outputIndex) {
|
|
37
|
+
const parsed = asOutputItem(item);
|
|
38
|
+
if (parsed?.call_id != null)
|
|
39
|
+
return parsed.call_id;
|
|
40
|
+
const key = parsed?.id ?? `result:${String(outputIndex)}`;
|
|
41
|
+
const existing = this.#resultCallIds.get(key);
|
|
42
|
+
if (existing !== undefined)
|
|
43
|
+
return existing;
|
|
44
|
+
const callId = this.#pendingCallIds.shift() ??
|
|
45
|
+
parsed?.id ??
|
|
46
|
+
`server_tool_result_${this.#scope}_${String(outputIndex)}`;
|
|
47
|
+
this.#resultCallIds.set(key, callId);
|
|
48
|
+
return callId;
|
|
49
|
+
}
|
|
50
|
+
fallbackCallId(outputIndex) {
|
|
51
|
+
return `server_tool_call_${this.#scope}_${String(outputIndex)}`;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
14
54
|
/**
|
|
15
55
|
* Maps the OpenAI Responses event protocol shared by Codex, Grok, and Bedrock Mantle.
|
|
16
56
|
*
|
|
@@ -27,9 +67,11 @@ export async function* mapOpenAIResponseStream(responseStream, options) {
|
|
|
27
67
|
const toolCalls = [];
|
|
28
68
|
const outputItems = new Map();
|
|
29
69
|
const finishedServerToolCalls = new Set();
|
|
70
|
+
const finishedServerToolResults = new Set();
|
|
30
71
|
const finishedMessageItems = new Set();
|
|
31
72
|
const finishedReasoningItems = new Set();
|
|
32
73
|
const fallbackCallIdScope = randomUUID();
|
|
74
|
+
const serverToolSearchPairing = new ServerToolSearchPairing(fallbackCallIdScope);
|
|
33
75
|
let usage = { ...EMPTY_SESSION_USAGE };
|
|
34
76
|
let outputTokensReported = false;
|
|
35
77
|
const finish = (stopReason) => {
|
|
@@ -38,7 +80,7 @@ export async function* mapOpenAIResponseStream(responseStream, options) {
|
|
|
38
80
|
.map(([, item]) => item);
|
|
39
81
|
return {
|
|
40
82
|
assistantText,
|
|
41
|
-
message: toSessionAssistantMessage(orderedOutputItems, toolCalls, options,
|
|
83
|
+
message: toSessionAssistantMessage(orderedOutputItems, toolCalls, options, serverToolSearchPairing),
|
|
42
84
|
outputTokensReported,
|
|
43
85
|
outputItems: orderedOutputItems,
|
|
44
86
|
stopReason,
|
|
@@ -53,7 +95,7 @@ export async function* mapOpenAIResponseStream(responseStream, options) {
|
|
|
53
95
|
event.type !== "response.incomplete" &&
|
|
54
96
|
event.type !== "response.failed" &&
|
|
55
97
|
event.type !== "error") {
|
|
56
|
-
yield* settleServerToolCalls(activeItems, [], finishedServerToolCalls, options,
|
|
98
|
+
yield* settleServerToolCalls(activeItems, [], finishedServerToolCalls, options, serverToolSearchPairing, true);
|
|
57
99
|
return finish("stop");
|
|
58
100
|
}
|
|
59
101
|
if (event.type === "response.output_item.added") {
|
|
@@ -171,13 +213,26 @@ export async function* mapOpenAIResponseStream(responseStream, options) {
|
|
|
171
213
|
else {
|
|
172
214
|
const name = serverExecutedItemName(event.item, options);
|
|
173
215
|
if (name !== undefined) {
|
|
174
|
-
const callId = serverToolCallId(event.item, event.output_index,
|
|
216
|
+
const callId = serverToolCallId(event.item, event.output_index, serverToolSearchPairing);
|
|
217
|
+
const namespace = serverExecutedItemNamespace(event.item, options);
|
|
175
218
|
activeItems.set(event.output_index, {
|
|
176
219
|
type: "server_tool_call",
|
|
177
220
|
callId,
|
|
178
221
|
name,
|
|
222
|
+
...(namespace === undefined ? {} : { namespace }),
|
|
179
223
|
});
|
|
180
|
-
yield {
|
|
224
|
+
yield {
|
|
225
|
+
type: "toolcall_start",
|
|
226
|
+
callId,
|
|
227
|
+
name,
|
|
228
|
+
...(namespace === undefined ? {} : { namespace }),
|
|
229
|
+
server: true,
|
|
230
|
+
...(event.item.type === "tool_search_call"
|
|
231
|
+
? {
|
|
232
|
+
vendor: responseServerToolVendor(options.vendor, "tool_search_call"),
|
|
233
|
+
}
|
|
234
|
+
: {}),
|
|
235
|
+
};
|
|
181
236
|
}
|
|
182
237
|
}
|
|
183
238
|
continue;
|
|
@@ -254,6 +309,21 @@ export async function* mapOpenAIResponseStream(responseStream, options) {
|
|
|
254
309
|
assertServerCallWasDeclared(event.item, options);
|
|
255
310
|
const activeItem = activeItems.get(event.output_index);
|
|
256
311
|
outputItems.set(event.output_index, JSON.stringify(event.item));
|
|
312
|
+
if (isServerToolSearchOutputItem(event.item, options)) {
|
|
313
|
+
const callId = serverToolSearchOutputCallId(event.item, event.output_index, serverToolSearchPairing);
|
|
314
|
+
const incomplete = isIncompleteServerToolCall(event.item);
|
|
315
|
+
yield* emitToolCallResult(callId, serverToolSearchOutputResult(event.item), {
|
|
316
|
+
...(incomplete ? { incomplete: true } : {}),
|
|
317
|
+
vendor: {
|
|
318
|
+
provider: options.vendor ?? "grok",
|
|
319
|
+
type: "server_tool_result",
|
|
320
|
+
outputItem: JSON.stringify(event.item),
|
|
321
|
+
},
|
|
322
|
+
});
|
|
323
|
+
finishedServerToolResults.add(callId);
|
|
324
|
+
activeItems.delete(event.output_index);
|
|
325
|
+
continue;
|
|
326
|
+
}
|
|
257
327
|
if (event.item.type === "reasoning") {
|
|
258
328
|
encryptedReasoning = JSON.stringify(event.item);
|
|
259
329
|
if (activeItem?.type !== "reasoning")
|
|
@@ -283,15 +353,22 @@ export async function* mapOpenAIResponseStream(responseStream, options) {
|
|
|
283
353
|
: serverExecutedItemName(event.item, options);
|
|
284
354
|
if (serverToolName !== undefined) {
|
|
285
355
|
const callId = activeItem?.callId ??
|
|
286
|
-
serverToolCallId(event.item, event.output_index,
|
|
356
|
+
serverToolCallId(event.item, event.output_index, serverToolSearchPairing);
|
|
287
357
|
const argumentsJson = serverToolCallArguments(event.item);
|
|
288
358
|
const incomplete = isIncompleteServerToolCall(event.item);
|
|
359
|
+
const namespace = activeItem?.namespace ?? serverExecutedItemNamespace(event.item, options);
|
|
289
360
|
if (activeItem === undefined) {
|
|
290
361
|
yield {
|
|
291
362
|
type: "toolcall_start",
|
|
292
363
|
callId,
|
|
293
364
|
name: serverToolName,
|
|
365
|
+
...(namespace === undefined ? {} : { namespace }),
|
|
294
366
|
server: true,
|
|
367
|
+
...(event.item.type === "tool_search_call"
|
|
368
|
+
? {
|
|
369
|
+
vendor: responseServerToolVendor(options.vendor, "tool_search_call"),
|
|
370
|
+
}
|
|
371
|
+
: {}),
|
|
295
372
|
};
|
|
296
373
|
}
|
|
297
374
|
yield {
|
|
@@ -299,6 +376,7 @@ export async function* mapOpenAIResponseStream(responseStream, options) {
|
|
|
299
376
|
callId,
|
|
300
377
|
arguments: argumentsJson,
|
|
301
378
|
...(incomplete ? { incomplete: true } : {}),
|
|
379
|
+
vendor: responseCompletedServerToolVendor(options.vendor, event.item),
|
|
302
380
|
};
|
|
303
381
|
// Sources and other provider-owned outcomes ride beside the call, not inside
|
|
304
382
|
// the executor tool loop. Emit them only when the item actually carries a
|
|
@@ -432,7 +510,8 @@ export async function* mapOpenAIResponseStream(responseStream, options) {
|
|
|
432
510
|
for (const [outputIndex, item] of (event.response.output ?? []).entries()) {
|
|
433
511
|
outputItems.set(outputIndex, JSON.stringify(item));
|
|
434
512
|
}
|
|
435
|
-
yield* settleServerToolCalls(activeItems, event.response.output ?? [], finishedServerToolCalls, options,
|
|
513
|
+
yield* settleServerToolCalls(activeItems, event.response.output ?? [], finishedServerToolCalls, options, serverToolSearchPairing, true);
|
|
514
|
+
yield* settleServerToolResults(event.response.output ?? [], finishedServerToolResults, options, serverToolSearchPairing, true);
|
|
436
515
|
yield* settleTerminalReasoningBlocks(activeItems, event.response.output ?? [], finishedReasoningItems);
|
|
437
516
|
yield* settleTerminalMessageText(activeItems, event.response.output ?? [], stripCitations, finishedMessageItems, (delta) => {
|
|
438
517
|
assistantText += delta;
|
|
@@ -496,7 +575,8 @@ export async function* mapOpenAIResponseStream(responseStream, options) {
|
|
|
496
575
|
for (const [outputIndex, item] of (event.response.output ?? []).entries()) {
|
|
497
576
|
outputItems.set(outputIndex, JSON.stringify(item));
|
|
498
577
|
}
|
|
499
|
-
yield* settleServerToolCalls(activeItems, event.response.output ?? [], finishedServerToolCalls, options,
|
|
578
|
+
yield* settleServerToolCalls(activeItems, event.response.output ?? [], finishedServerToolCalls, options, serverToolSearchPairing);
|
|
579
|
+
yield* settleServerToolResults(event.response.output ?? [], finishedServerToolResults, options, serverToolSearchPairing);
|
|
500
580
|
yield* settleTerminalReasoningBlocks(activeItems, event.response.output ?? [], finishedReasoningItems);
|
|
501
581
|
yield* settleTerminalMessageText(activeItems, event.response.output ?? [], stripCitations, finishedMessageItems, (delta) => {
|
|
502
582
|
assistantText += delta;
|
|
@@ -561,21 +641,21 @@ export async function* mapOpenAIResponseStream(responseStream, options) {
|
|
|
561
641
|
return finish(sawToolUse ? "tool_use" : "stop");
|
|
562
642
|
}
|
|
563
643
|
if (event.type === "error") {
|
|
564
|
-
yield* settleServerToolCalls(activeItems, [], finishedServerToolCalls, options,
|
|
644
|
+
yield* settleServerToolCalls(activeItems, [], finishedServerToolCalls, options, serverToolSearchPairing, true);
|
|
565
645
|
throw responseStreamError(event, options.failureMessage);
|
|
566
646
|
}
|
|
567
647
|
if (event.type === "response.failed") {
|
|
568
|
-
yield* settleServerToolCalls(activeItems, event.response.output ?? [], finishedServerToolCalls, options,
|
|
648
|
+
yield* settleServerToolCalls(activeItems, event.response.output ?? [], finishedServerToolCalls, options, serverToolSearchPairing, true);
|
|
569
649
|
throw responseStreamError(event, options.failureMessage);
|
|
570
650
|
}
|
|
571
651
|
}
|
|
572
652
|
}
|
|
573
653
|
catch (error) {
|
|
574
|
-
yield* settleServerToolCalls(activeItems, [], finishedServerToolCalls, options,
|
|
654
|
+
yield* settleServerToolCalls(activeItems, [], finishedServerToolCalls, options, serverToolSearchPairing, true);
|
|
575
655
|
throw error;
|
|
576
656
|
}
|
|
577
657
|
if (options.requireTerminalEvent) {
|
|
578
|
-
yield* settleServerToolCalls(activeItems, [], finishedServerToolCalls, options,
|
|
658
|
+
yield* settleServerToolCalls(activeItems, [], finishedServerToolCalls, options, serverToolSearchPairing, true);
|
|
579
659
|
throw new Error("Response stream closed before completion.");
|
|
580
660
|
}
|
|
581
661
|
yield { type: "token_usage", usage };
|
|
@@ -586,7 +666,7 @@ export async function* mapOpenAIResponseStream(responseStream, options) {
|
|
|
586
666
|
};
|
|
587
667
|
return finish(sawToolUse ? "tool_use" : "stop");
|
|
588
668
|
}
|
|
589
|
-
function toSessionAssistantMessage(outputItems, toolCalls, options,
|
|
669
|
+
function toSessionAssistantMessage(outputItems, toolCalls, options, serverToolSearchPairing) {
|
|
590
670
|
const stripCitations = createServerCitationFilter();
|
|
591
671
|
const content = outputItems.flatMap((encoded, outputIndex) => {
|
|
592
672
|
let item;
|
|
@@ -610,9 +690,25 @@ function toSessionAssistantMessage(outputItems, toolCalls, options, fallbackCall
|
|
|
610
690
|
const text = outputMessageText(item);
|
|
611
691
|
return text === undefined ? [] : [{ type: "text", text: stripCitations(text) }];
|
|
612
692
|
}
|
|
693
|
+
if (isServerToolSearchOutputItem(item, options)) {
|
|
694
|
+
return [
|
|
695
|
+
{
|
|
696
|
+
type: "tool_result",
|
|
697
|
+
callId: serverToolSearchOutputCallId(item, outputIndex, serverToolSearchPairing),
|
|
698
|
+
content: [{ type: "text", text: serverToolSearchOutputResult(item) }],
|
|
699
|
+
...(isIncompleteServerToolCall(item) ? { incomplete: true } : {}),
|
|
700
|
+
vendor: {
|
|
701
|
+
provider: options.vendor ?? "grok",
|
|
702
|
+
type: "server_tool_result",
|
|
703
|
+
outputItem: encoded,
|
|
704
|
+
},
|
|
705
|
+
},
|
|
706
|
+
];
|
|
707
|
+
}
|
|
613
708
|
const serverName = serverExecutedItemName(item, options);
|
|
614
709
|
if (serverName !== undefined) {
|
|
615
|
-
const callId = serverToolCallId(item, outputIndex,
|
|
710
|
+
const callId = serverToolCallId(item, outputIndex, serverToolSearchPairing);
|
|
711
|
+
const namespace = serverExecutedItemNamespace(item, options);
|
|
616
712
|
const incomplete = isIncompleteServerToolCall(item);
|
|
617
713
|
const result = serverToolCallResult(item);
|
|
618
714
|
return [
|
|
@@ -620,6 +716,7 @@ function toSessionAssistantMessage(outputItems, toolCalls, options, fallbackCall
|
|
|
620
716
|
type: "tool_call",
|
|
621
717
|
callId,
|
|
622
718
|
name: serverName,
|
|
719
|
+
...(namespace === undefined ? {} : { namespace }),
|
|
623
720
|
arguments: serverToolCallArguments(item),
|
|
624
721
|
server: true,
|
|
625
722
|
...(incomplete ? { incomplete: true } : {}),
|
|
@@ -642,7 +739,7 @@ function toSessionAssistantMessage(outputItems, toolCalls, options, fallbackCall
|
|
|
642
739
|
];
|
|
643
740
|
}
|
|
644
741
|
const parsed = asOutputItem(item);
|
|
645
|
-
const callId = parsed?.call_id;
|
|
742
|
+
const callId = parsed?.call_id ?? undefined;
|
|
646
743
|
if (callId === undefined)
|
|
647
744
|
return [];
|
|
648
745
|
const call = toolCalls.find((candidate) => candidate.callId === callId);
|
|
@@ -697,7 +794,7 @@ function hasReportedOutputTokens(usage) {
|
|
|
697
794
|
* that never streamed at all would go unreported even though the provider ran it. The end is the
|
|
698
795
|
* durable half of the pair, so losing it strands a live row and erases the record of the search.
|
|
699
796
|
*/
|
|
700
|
-
function* settleServerToolCalls(activeItems, terminalOutput, finished, options,
|
|
797
|
+
function* settleServerToolCalls(activeItems, terminalOutput, finished, options, serverToolSearchPairing, incomplete = false) {
|
|
701
798
|
for (const [outputIndex, activeItem] of activeItems) {
|
|
702
799
|
if (activeItem.type !== "server_tool_call" ||
|
|
703
800
|
activeItem.callId === undefined ||
|
|
@@ -706,7 +803,7 @@ function* settleServerToolCalls(activeItems, terminalOutput, finished, options,
|
|
|
706
803
|
}
|
|
707
804
|
const callId = activeItem.callId;
|
|
708
805
|
const terminal = terminalOutput.find((item, terminalIndex) => serverExecutedItemName(item, options) !== undefined &&
|
|
709
|
-
serverToolCallId(item, terminalIndex,
|
|
806
|
+
serverToolCallId(item, terminalIndex, serverToolSearchPairing) === callId);
|
|
710
807
|
const settled = terminal === undefined
|
|
711
808
|
? undefined
|
|
712
809
|
: emptyToUndefined(serverToolCallArguments(terminal));
|
|
@@ -719,6 +816,9 @@ function* settleServerToolCalls(activeItems, terminalOutput, finished, options,
|
|
|
719
816
|
callId,
|
|
720
817
|
arguments: settled ?? activeItem.argumentsJson ?? "",
|
|
721
818
|
...(callIncomplete ? { incomplete: true } : {}),
|
|
819
|
+
...(terminal === undefined
|
|
820
|
+
? {}
|
|
821
|
+
: { vendor: responseCompletedServerToolVendor(options.vendor, terminal) }),
|
|
722
822
|
};
|
|
723
823
|
const result = terminal === undefined ? undefined : serverToolCallResult(terminal);
|
|
724
824
|
if (result !== undefined) {
|
|
@@ -733,16 +833,29 @@ function* settleServerToolCalls(activeItems, terminalOutput, finished, options,
|
|
|
733
833
|
const name = serverExecutedItemName(item, options);
|
|
734
834
|
if (name === undefined)
|
|
735
835
|
continue;
|
|
736
|
-
const callId = serverToolCallId(item, terminalIndex,
|
|
836
|
+
const callId = serverToolCallId(item, terminalIndex, serverToolSearchPairing);
|
|
837
|
+
const namespace = serverExecutedItemNamespace(item, options);
|
|
737
838
|
if (finished.has(callId))
|
|
738
839
|
continue;
|
|
739
840
|
const callIncomplete = isIncompleteServerToolCall(item) || (incomplete && isIncompleteOutputItem(item));
|
|
740
|
-
yield {
|
|
841
|
+
yield {
|
|
842
|
+
type: "toolcall_start",
|
|
843
|
+
callId,
|
|
844
|
+
name,
|
|
845
|
+
...(namespace === undefined ? {} : { namespace }),
|
|
846
|
+
server: true,
|
|
847
|
+
...(isItemType(item, "tool_search_call")
|
|
848
|
+
? {
|
|
849
|
+
vendor: responseServerToolVendor(options.vendor, "tool_search_call"),
|
|
850
|
+
}
|
|
851
|
+
: {}),
|
|
852
|
+
};
|
|
741
853
|
yield {
|
|
742
854
|
type: "toolcall_end",
|
|
743
855
|
callId,
|
|
744
856
|
arguments: serverToolCallArguments(item),
|
|
745
857
|
...(callIncomplete ? { incomplete: true } : {}),
|
|
858
|
+
vendor: responseCompletedServerToolVendor(options.vendor, item),
|
|
746
859
|
};
|
|
747
860
|
const result = serverToolCallResult(item);
|
|
748
861
|
if (result !== undefined) {
|
|
@@ -753,14 +866,33 @@ function* settleServerToolCalls(activeItems, terminalOutput, finished, options,
|
|
|
753
866
|
finished.add(callId);
|
|
754
867
|
}
|
|
755
868
|
}
|
|
869
|
+
function* settleServerToolResults(terminalOutput, finished, options, serverToolSearchPairing, incomplete = false) {
|
|
870
|
+
for (const [outputIndex, item] of terminalOutput.entries()) {
|
|
871
|
+
if (!isServerToolSearchOutputItem(item, options))
|
|
872
|
+
continue;
|
|
873
|
+
const callId = serverToolSearchOutputCallId(item, outputIndex, serverToolSearchPairing);
|
|
874
|
+
if (finished.has(callId))
|
|
875
|
+
continue;
|
|
876
|
+
const resultIncomplete = isIncompleteServerToolCall(item) || (incomplete && isIncompleteOutputItem(item));
|
|
877
|
+
yield* emitToolCallResult(callId, serverToolSearchOutputResult(item), {
|
|
878
|
+
...(resultIncomplete ? { incomplete: true } : {}),
|
|
879
|
+
vendor: {
|
|
880
|
+
provider: options.vendor ?? "grok",
|
|
881
|
+
type: "server_tool_result",
|
|
882
|
+
outputItem: JSON.stringify(item),
|
|
883
|
+
},
|
|
884
|
+
});
|
|
885
|
+
finished.add(callId);
|
|
886
|
+
}
|
|
887
|
+
}
|
|
756
888
|
function emptyToUndefined(value) {
|
|
757
889
|
return value.length === 0 ? undefined : value;
|
|
758
890
|
}
|
|
759
891
|
/**
|
|
760
892
|
* Names the server tool behind an output item the provider executed itself.
|
|
761
893
|
*
|
|
762
|
-
*
|
|
763
|
-
*
|
|
894
|
+
* Three shapes reach us, and each has to be positively recognized. Hosted web and tool search
|
|
895
|
+
* have their own item types. Grok's X search instead reports its backend sub-calls as ordinary
|
|
764
896
|
* custom tool calls, which look exactly like a call the client must answer, so classification
|
|
765
897
|
* rests on two facts that are true only of a server call: the request asked for server tools at
|
|
766
898
|
* all — nothing runs upstream that was never enabled, which also keeps compaction, which sends
|
|
@@ -776,34 +908,69 @@ function serverExecutedItemName(item, options) {
|
|
|
776
908
|
const parsed = asOutputItem(item);
|
|
777
909
|
if (parsed === undefined)
|
|
778
910
|
return undefined;
|
|
779
|
-
const
|
|
780
|
-
if (
|
|
911
|
+
const nativeName = serverExecutedNativeName(parsed, options);
|
|
912
|
+
if (nativeName === undefined)
|
|
913
|
+
return undefined;
|
|
914
|
+
return (options.serverToolDisplayNames?.get(nativeName) ??
|
|
915
|
+
(nativeName === "x_search" ? parsed.name : nativeName));
|
|
916
|
+
}
|
|
917
|
+
function serverExecutedItemNamespace(item, options) {
|
|
918
|
+
const parsed = asOutputItem(item);
|
|
919
|
+
if (parsed === undefined)
|
|
920
|
+
return undefined;
|
|
921
|
+
const nativeName = serverExecutedNativeName(parsed, options);
|
|
922
|
+
return nativeName === undefined
|
|
923
|
+
? undefined
|
|
924
|
+
: options.serverToolDisplayNamespaces?.get(nativeName);
|
|
925
|
+
}
|
|
926
|
+
function serverExecutedNativeName(item, options) {
|
|
927
|
+
if (item.type === "web_search_call") {
|
|
781
928
|
return options.serverToolNames?.has("web_search") === true ? "web_search" : undefined;
|
|
782
929
|
}
|
|
783
|
-
if (type
|
|
784
|
-
return
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
930
|
+
if (item.type === "tool_search_call") {
|
|
931
|
+
return item.execution === "server" && options.serverToolNames?.has("tool_search") === true
|
|
932
|
+
? "tool_search"
|
|
933
|
+
: undefined;
|
|
934
|
+
}
|
|
935
|
+
return item.type === "custom_tool_call" &&
|
|
936
|
+
options.serverToolNames?.has("x_search") === true &&
|
|
937
|
+
item.call_id?.startsWith(HOSTED_SEARCH_CALL_PREFIX) === true
|
|
938
|
+
? "x_search"
|
|
788
939
|
: undefined;
|
|
789
940
|
}
|
|
790
941
|
/**
|
|
791
|
-
* Identifies a server call. Grok's X search
|
|
792
|
-
* the output index stands in for anything that reports neither, so two
|
|
793
|
-
* collide under one empty identifier.
|
|
942
|
+
* Identifies a server call. Tool search and Grok's X search carry a `call_id`, while web search
|
|
943
|
+
* carries only an `id`; the output index stands in for anything that reports neither, so two
|
|
944
|
+
* concurrent calls can never collide under one empty identifier.
|
|
794
945
|
*/
|
|
795
|
-
function serverToolCallId(item, outputIndex,
|
|
946
|
+
function serverToolCallId(item, outputIndex, serverToolSearchPairing) {
|
|
796
947
|
const parsed = asOutputItem(item);
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
948
|
+
if (parsed?.type === "tool_search_call" && parsed.execution === "server") {
|
|
949
|
+
return serverToolSearchPairing.callId(item, outputIndex);
|
|
950
|
+
}
|
|
951
|
+
return parsed?.call_id ?? parsed?.id ?? serverToolSearchPairing.fallbackCallId(outputIndex);
|
|
800
952
|
}
|
|
801
953
|
function serverToolCallArguments(item) {
|
|
802
954
|
const parsed = asOutputItem(item);
|
|
803
955
|
if (parsed?.input !== undefined)
|
|
804
956
|
return parsed.input;
|
|
957
|
+
if (parsed?.type === "tool_search_call" && parsed.arguments !== undefined) {
|
|
958
|
+
return JSON.stringify(parsed.arguments);
|
|
959
|
+
}
|
|
805
960
|
return parsed?.action === undefined ? "" : JSON.stringify(parsed.action);
|
|
806
961
|
}
|
|
962
|
+
function isServerToolSearchOutputItem(item, options) {
|
|
963
|
+
const parsed = asOutputItem(item);
|
|
964
|
+
return (parsed?.type === "tool_search_output" &&
|
|
965
|
+
parsed.execution === "server" &&
|
|
966
|
+
options.serverToolNames?.has("tool_search") === true);
|
|
967
|
+
}
|
|
968
|
+
function serverToolSearchOutputCallId(item, outputIndex, serverToolSearchPairing) {
|
|
969
|
+
return serverToolSearchPairing.resultCallId(item, outputIndex);
|
|
970
|
+
}
|
|
971
|
+
function serverToolSearchOutputResult(item) {
|
|
972
|
+
return JSON.stringify(asOutputItem(item)?.tools ?? []);
|
|
973
|
+
}
|
|
807
974
|
/**
|
|
808
975
|
* The provider-owned outcome of a server call, when the item carries one.
|
|
809
976
|
*
|
|
@@ -996,9 +1163,12 @@ const outputItemSchema = Type.Object({
|
|
|
996
1163
|
type: Type.Optional(Type.String()),
|
|
997
1164
|
name: Type.Optional(Type.String()),
|
|
998
1165
|
id: Type.Optional(Type.String()),
|
|
999
|
-
call_id: Type.Optional(Type.String()),
|
|
1166
|
+
call_id: Type.Optional(Type.Union([Type.String(), Type.Null()])),
|
|
1000
1167
|
input: Type.Optional(Type.String()),
|
|
1001
1168
|
action: Type.Optional(Type.Unknown()),
|
|
1169
|
+
arguments: Type.Optional(Type.Unknown()),
|
|
1170
|
+
execution: Type.Optional(Type.Union([Type.Literal("server"), Type.Literal("client")])),
|
|
1171
|
+
tools: Type.Optional(Type.Array(Type.Unknown())),
|
|
1002
1172
|
}, { additionalProperties: true });
|
|
1003
1173
|
const clientToolCallItemSchema = Type.Union([
|
|
1004
1174
|
Type.Object({
|
|
@@ -1074,6 +1244,11 @@ function assertServerCallWasDeclared(item, options) {
|
|
|
1074
1244
|
if (parsed?.type === "web_search_call" && options.serverToolNames?.has("web_search") !== true) {
|
|
1075
1245
|
throw new Error("Provider returned web_search_call without a declared server tool.");
|
|
1076
1246
|
}
|
|
1247
|
+
if ((parsed?.type === "tool_search_call" || parsed?.type === "tool_search_output") &&
|
|
1248
|
+
parsed.execution === "server" &&
|
|
1249
|
+
options.serverToolNames?.has("tool_search") !== true) {
|
|
1250
|
+
throw new Error("Provider returned server tool search without a declared server tool.");
|
|
1251
|
+
}
|
|
1077
1252
|
if (parsed?.type === "custom_tool_call" &&
|
|
1078
1253
|
parsed.call_id?.startsWith(HOSTED_SEARCH_CALL_PREFIX) === true &&
|
|
1079
1254
|
options.serverToolNames?.has("x_search") !== true) {
|
|
@@ -1099,6 +1274,16 @@ function responseToolVendor(vendor, type) {
|
|
|
1099
1274
|
? { provider, type, execution: "client" }
|
|
1100
1275
|
: { provider, type };
|
|
1101
1276
|
}
|
|
1277
|
+
function responseServerToolVendor(vendor, nativeType) {
|
|
1278
|
+
return { provider: vendor ?? "grok", type: "server_tool_call", nativeType };
|
|
1279
|
+
}
|
|
1280
|
+
function responseCompletedServerToolVendor(vendor, item) {
|
|
1281
|
+
return {
|
|
1282
|
+
provider: vendor ?? "grok",
|
|
1283
|
+
type: "server_tool_call",
|
|
1284
|
+
outputItem: JSON.stringify(item),
|
|
1285
|
+
};
|
|
1286
|
+
}
|
|
1102
1287
|
function isIncompleteOutputItem(item) {
|
|
1103
1288
|
return (typeof item === "object" &&
|
|
1104
1289
|
item !== null &&
|