@ai-sdk/provider-utils 5.0.0-beta.23 → 5.0.0-beta.24
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/CHANGELOG.md +6 -0
- package/dist/index.d.ts +72 -2
- package/dist/index.js +136 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +5 -0
- package/src/streaming-tool-call-tracker.ts +243 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# @ai-sdk/provider-utils
|
|
2
2
|
|
|
3
|
+
## 5.0.0-beta.24
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- f807e45: Extract shared `StreamingToolCallTracker` class into `@ai-sdk/provider-utils` to deduplicate streaming tool call handling across OpenAI-compatible providers. Also adds missing `generateId()` fallback for `toolCallId` in Alibaba's `doGenerate` path and ensures all providers finalize unfinished tool calls during stream flush.
|
|
8
|
+
|
|
3
9
|
## 5.0.0-beta.23
|
|
4
10
|
|
|
5
11
|
### Patch Changes
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ImageModelV4File, LanguageModelV4FunctionTool, LanguageModelV4ProviderTool, AISDKError, JSONSchema7, JSONParseError, TypeValidationError, JSONValue, APICallError, LanguageModelV4Prompt, LanguageModelV4FilePart, SharedV4ProviderReference, LanguageModelV4CallOptions, SharedV4Warning, SharedV4ProviderOptions, JSONObject, TypeValidationContext } from '@ai-sdk/provider';
|
|
1
|
+
import { ImageModelV4File, LanguageModelV4FunctionTool, LanguageModelV4ProviderTool, AISDKError, JSONSchema7, JSONParseError, TypeValidationError, JSONValue, APICallError, LanguageModelV4Prompt, LanguageModelV4FilePart, SharedV4ProviderReference, LanguageModelV4CallOptions, SharedV4Warning, SharedV4ProviderOptions, JSONObject, SharedV4ProviderMetadata, LanguageModelV4StreamPart, TypeValidationContext } from '@ai-sdk/provider';
|
|
2
2
|
export { getErrorMessage } from '@ai-sdk/provider';
|
|
3
3
|
import { StandardSchemaV1, StandardJSONSchemaV1 } from '@standard-schema/spec';
|
|
4
4
|
export * from '@standard-schema/spec';
|
|
@@ -1479,6 +1479,76 @@ declare function serializeModelOptions<CONFIG extends {
|
|
|
1479
1479
|
config: JSONObject;
|
|
1480
1480
|
};
|
|
1481
1481
|
|
|
1482
|
+
/**
|
|
1483
|
+
* Minimal interface for a streaming tool call delta from an OpenAI-compatible API.
|
|
1484
|
+
*/
|
|
1485
|
+
interface StreamingToolCallDelta {
|
|
1486
|
+
index?: number | null;
|
|
1487
|
+
id?: string | null;
|
|
1488
|
+
type?: string | null;
|
|
1489
|
+
function?: {
|
|
1490
|
+
name?: string | null;
|
|
1491
|
+
arguments?: string | null;
|
|
1492
|
+
} | null;
|
|
1493
|
+
}
|
|
1494
|
+
interface StreamingToolCallTrackerOptions {
|
|
1495
|
+
/**
|
|
1496
|
+
* ID generator function for tool call IDs.
|
|
1497
|
+
* Defaults to the standard generateId.
|
|
1498
|
+
*/
|
|
1499
|
+
generateId?: () => string;
|
|
1500
|
+
/**
|
|
1501
|
+
* How to validate the `type` field on new tool call deltas.
|
|
1502
|
+
* - `'none'`: no validation (default)
|
|
1503
|
+
* - `'if-present'`: throw if type is present and not `'function'`
|
|
1504
|
+
* - `'required'`: throw if type is not exactly `'function'`
|
|
1505
|
+
*/
|
|
1506
|
+
typeValidation?: 'none' | 'if-present' | 'required';
|
|
1507
|
+
/**
|
|
1508
|
+
* Extract provider-specific metadata from a tool call delta.
|
|
1509
|
+
* Called once when a new tool call is detected.
|
|
1510
|
+
* The returned metadata is stored on the tool call and passed to
|
|
1511
|
+
* `buildToolCallProviderMetadata` when the tool call is finalized.
|
|
1512
|
+
*/
|
|
1513
|
+
extractMetadata?: (delta: StreamingToolCallDelta) => SharedV4ProviderMetadata | undefined;
|
|
1514
|
+
/**
|
|
1515
|
+
* Build the `providerMetadata` object for a `tool-call` event.
|
|
1516
|
+
* Receives the metadata previously extracted via `extractMetadata`.
|
|
1517
|
+
* If `undefined` is returned, no `providerMetadata` is included in the event.
|
|
1518
|
+
*/
|
|
1519
|
+
buildToolCallProviderMetadata?: (metadata: SharedV4ProviderMetadata | undefined) => SharedV4ProviderMetadata | undefined;
|
|
1520
|
+
}
|
|
1521
|
+
/**
|
|
1522
|
+
* Tracks streaming tool call state across multiple deltas from an
|
|
1523
|
+
* OpenAI-compatible chat completion stream. Handles argument accumulation,
|
|
1524
|
+
* emits tool-input-start/delta/end and tool-call events, and finalizes
|
|
1525
|
+
* unfinished tool calls on flush.
|
|
1526
|
+
*
|
|
1527
|
+
* Used by openai, openai-compatible, groq, deepseek, and alibaba providers.
|
|
1528
|
+
*/
|
|
1529
|
+
declare class StreamingToolCallTracker {
|
|
1530
|
+
private toolCalls;
|
|
1531
|
+
private readonly _generateId;
|
|
1532
|
+
private readonly typeValidation;
|
|
1533
|
+
private readonly extractMetadata?;
|
|
1534
|
+
private readonly buildToolCallProviderMetadata?;
|
|
1535
|
+
constructor(options?: StreamingToolCallTrackerOptions);
|
|
1536
|
+
/**
|
|
1537
|
+
* Process a tool call delta from a streaming response chunk.
|
|
1538
|
+
* Emits tool-input-start, tool-input-delta, tool-input-end, and tool-call
|
|
1539
|
+
* events as appropriate.
|
|
1540
|
+
*/
|
|
1541
|
+
processDelta(toolCallDelta: StreamingToolCallDelta, enqueue: (part: LanguageModelV4StreamPart) => void): void;
|
|
1542
|
+
/**
|
|
1543
|
+
* Finalize any unfinished tool calls. Should be called during the stream's
|
|
1544
|
+
* flush handler to ensure all tool calls are properly completed.
|
|
1545
|
+
*/
|
|
1546
|
+
flush(enqueue: (part: LanguageModelV4StreamPart) => void): void;
|
|
1547
|
+
private processNewToolCall;
|
|
1548
|
+
private processExistingToolCall;
|
|
1549
|
+
private finishToolCall;
|
|
1550
|
+
}
|
|
1551
|
+
|
|
1482
1552
|
/**
|
|
1483
1553
|
* Strips file extension segments from a filename.
|
|
1484
1554
|
*
|
|
@@ -1684,4 +1754,4 @@ interface ToolResult<NAME extends string, INPUT, OUTPUT> {
|
|
|
1684
1754
|
dynamic?: boolean;
|
|
1685
1755
|
}
|
|
1686
1756
|
|
|
1687
|
-
export { type Arrayable, type AssistantContent, type AssistantModelMessage, type Context, type CustomPart, DEFAULT_MAX_DOWNLOAD_SIZE, type DataContent, DelayedPromise, DownloadError, type ExecutableTool, type FetchFunction, type FilePart, type FlexibleSchema, type HasRequiredKey, type IdGenerator, type ImagePart, type InferSchema, type InferToolContext, type InferToolInput, type InferToolOutput, type InferToolSetContext, type LazySchema, type MaybePromiseLike, type ModelMessage, type ParseResult, type ProviderOptions, type ProviderReference, type ProviderToolFactory, type ProviderToolFactoryWithOutputSchema, type ReasoningFilePart, type ReasoningPart, type Resolvable, type ResponseHandler, type Schema, type SystemModelMessage, type TextPart, type Tool, type ToolApprovalRequest, type ToolApprovalResponse, type ToolCall, type ToolCallPart, type ToolContent, type ToolExecuteFunction, type ToolExecutionOptions, type ToolModelMessage, type ToolNameMapping, type ToolNeedsApprovalFunction, type ToolResult, type ToolResultOutput, type ToolResultPart, type ToolSet, type UserContent, type UserModelMessage, VERSION, type ValidationResult, asArray, asSchema, combineHeaders, convertAsyncIteratorToReadableStream, convertBase64ToUint8Array, convertImageModelFileToDataUri, convertToBase64, convertToFormData, convertUint8ArrayToBase64, createBinaryResponseHandler, createEventSourceResponseHandler, createIdGenerator, createJsonErrorResponseHandler, createJsonResponseHandler, createProviderToolFactory, createProviderToolFactoryWithOutputSchema, createStatusCodeErrorResponseHandler, createToolNameMapping, delay, downloadBlob, dynamicTool, executeTool, extractResponseHeaders, generateId, getFromApi, getRuntimeEnvironmentUserAgent, injectJsonInstructionIntoMessages, isAbortError, isCustomReasoning, isExecutableTool, isNonNullable, isParsableJson, isProviderReference, isUrlSupported, jsonSchema, lazySchema, loadApiKey, loadOptionalSetting, loadSetting, mapReasoningToProviderBudget, mapReasoningToProviderEffort, mediaTypeToExtension, normalizeHeaders, parseJSON, parseJsonEventStream, parseProviderOptions, postFormDataToApi, postJsonToApi, postToApi, readResponseWithSizeLimit, removeUndefinedEntries, resolve, resolveProviderReference, safeParseJSON, safeValidateTypes, serializeModelOptions, stripFileExtension, tool, validateDownloadUrl, validateTypes, withUserAgentSuffix, withoutTrailingSlash, zodSchema };
|
|
1757
|
+
export { type Arrayable, type AssistantContent, type AssistantModelMessage, type Context, type CustomPart, DEFAULT_MAX_DOWNLOAD_SIZE, type DataContent, DelayedPromise, DownloadError, type ExecutableTool, type FetchFunction, type FilePart, type FlexibleSchema, type HasRequiredKey, type IdGenerator, type ImagePart, type InferSchema, type InferToolContext, type InferToolInput, type InferToolOutput, type InferToolSetContext, type LazySchema, type MaybePromiseLike, type ModelMessage, type ParseResult, type ProviderOptions, type ProviderReference, type ProviderToolFactory, type ProviderToolFactoryWithOutputSchema, type ReasoningFilePart, type ReasoningPart, type Resolvable, type ResponseHandler, type Schema, type StreamingToolCallDelta, StreamingToolCallTracker, type StreamingToolCallTrackerOptions, type SystemModelMessage, type TextPart, type Tool, type ToolApprovalRequest, type ToolApprovalResponse, type ToolCall, type ToolCallPart, type ToolContent, type ToolExecuteFunction, type ToolExecutionOptions, type ToolModelMessage, type ToolNameMapping, type ToolNeedsApprovalFunction, type ToolResult, type ToolResultOutput, type ToolResultPart, type ToolSet, type UserContent, type UserModelMessage, VERSION, type ValidationResult, asArray, asSchema, combineHeaders, convertAsyncIteratorToReadableStream, convertBase64ToUint8Array, convertImageModelFileToDataUri, convertToBase64, convertToFormData, convertUint8ArrayToBase64, createBinaryResponseHandler, createEventSourceResponseHandler, createIdGenerator, createJsonErrorResponseHandler, createJsonResponseHandler, createProviderToolFactory, createProviderToolFactoryWithOutputSchema, createStatusCodeErrorResponseHandler, createToolNameMapping, delay, downloadBlob, dynamicTool, executeTool, extractResponseHeaders, generateId, getFromApi, getRuntimeEnvironmentUserAgent, injectJsonInstructionIntoMessages, isAbortError, isCustomReasoning, isExecutableTool, isNonNullable, isParsableJson, isProviderReference, isUrlSupported, jsonSchema, lazySchema, loadApiKey, loadOptionalSetting, loadSetting, mapReasoningToProviderBudget, mapReasoningToProviderEffort, mediaTypeToExtension, normalizeHeaders, parseJSON, parseJsonEventStream, parseProviderOptions, postFormDataToApi, postJsonToApi, postToApi, readResponseWithSizeLimit, removeUndefinedEntries, resolve, resolveProviderReference, safeParseJSON, safeValidateTypes, serializeModelOptions, stripFileExtension, tool, validateDownloadUrl, validateTypes, withUserAgentSuffix, withoutTrailingSlash, zodSchema };
|
package/dist/index.js
CHANGED
|
@@ -571,7 +571,7 @@ function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
|
|
|
571
571
|
}
|
|
572
572
|
|
|
573
573
|
// src/version.ts
|
|
574
|
-
var VERSION = true ? "5.0.0-beta.
|
|
574
|
+
var VERSION = true ? "5.0.0-beta.24" : "0.0.0-test";
|
|
575
575
|
|
|
576
576
|
// src/get-from-api.ts
|
|
577
577
|
var getOriginalFetch = () => globalThis.fetch;
|
|
@@ -2770,6 +2770,140 @@ function resolveSync(value) {
|
|
|
2770
2770
|
return next;
|
|
2771
2771
|
}
|
|
2772
2772
|
|
|
2773
|
+
// src/streaming-tool-call-tracker.ts
|
|
2774
|
+
import {
|
|
2775
|
+
InvalidResponseDataError
|
|
2776
|
+
} from "@ai-sdk/provider";
|
|
2777
|
+
var StreamingToolCallTracker = class {
|
|
2778
|
+
constructor(options = {}) {
|
|
2779
|
+
this.toolCalls = [];
|
|
2780
|
+
var _a2, _b2;
|
|
2781
|
+
this._generateId = (_a2 = options.generateId) != null ? _a2 : generateId;
|
|
2782
|
+
this.typeValidation = (_b2 = options.typeValidation) != null ? _b2 : "none";
|
|
2783
|
+
this.extractMetadata = options.extractMetadata;
|
|
2784
|
+
this.buildToolCallProviderMetadata = options.buildToolCallProviderMetadata;
|
|
2785
|
+
}
|
|
2786
|
+
/**
|
|
2787
|
+
* Process a tool call delta from a streaming response chunk.
|
|
2788
|
+
* Emits tool-input-start, tool-input-delta, tool-input-end, and tool-call
|
|
2789
|
+
* events as appropriate.
|
|
2790
|
+
*/
|
|
2791
|
+
processDelta(toolCallDelta, enqueue) {
|
|
2792
|
+
var _a2;
|
|
2793
|
+
const index = (_a2 = toolCallDelta.index) != null ? _a2 : this.toolCalls.length;
|
|
2794
|
+
if (this.toolCalls[index] == null) {
|
|
2795
|
+
this.processNewToolCall(index, toolCallDelta, enqueue);
|
|
2796
|
+
} else {
|
|
2797
|
+
this.processExistingToolCall(index, toolCallDelta, enqueue);
|
|
2798
|
+
}
|
|
2799
|
+
}
|
|
2800
|
+
/**
|
|
2801
|
+
* Finalize any unfinished tool calls. Should be called during the stream's
|
|
2802
|
+
* flush handler to ensure all tool calls are properly completed.
|
|
2803
|
+
*/
|
|
2804
|
+
flush(enqueue) {
|
|
2805
|
+
for (const toolCall of this.toolCalls) {
|
|
2806
|
+
if (!toolCall.hasFinished) {
|
|
2807
|
+
this.finishToolCall(toolCall, enqueue);
|
|
2808
|
+
}
|
|
2809
|
+
}
|
|
2810
|
+
}
|
|
2811
|
+
processNewToolCall(index, toolCallDelta, enqueue) {
|
|
2812
|
+
var _a2, _b2, _c;
|
|
2813
|
+
if (this.typeValidation === "required") {
|
|
2814
|
+
if (toolCallDelta.type !== "function") {
|
|
2815
|
+
throw new InvalidResponseDataError({
|
|
2816
|
+
data: toolCallDelta,
|
|
2817
|
+
message: `Expected 'function' type.`
|
|
2818
|
+
});
|
|
2819
|
+
}
|
|
2820
|
+
} else if (this.typeValidation === "if-present") {
|
|
2821
|
+
if (toolCallDelta.type != null && toolCallDelta.type !== "function") {
|
|
2822
|
+
throw new InvalidResponseDataError({
|
|
2823
|
+
data: toolCallDelta,
|
|
2824
|
+
message: `Expected 'function' type.`
|
|
2825
|
+
});
|
|
2826
|
+
}
|
|
2827
|
+
}
|
|
2828
|
+
if (toolCallDelta.id == null) {
|
|
2829
|
+
throw new InvalidResponseDataError({
|
|
2830
|
+
data: toolCallDelta,
|
|
2831
|
+
message: `Expected 'id' to be a string.`
|
|
2832
|
+
});
|
|
2833
|
+
}
|
|
2834
|
+
if (((_a2 = toolCallDelta.function) == null ? void 0 : _a2.name) == null) {
|
|
2835
|
+
throw new InvalidResponseDataError({
|
|
2836
|
+
data: toolCallDelta,
|
|
2837
|
+
message: `Expected 'function.name' to be a string.`
|
|
2838
|
+
});
|
|
2839
|
+
}
|
|
2840
|
+
enqueue({
|
|
2841
|
+
type: "tool-input-start",
|
|
2842
|
+
id: toolCallDelta.id,
|
|
2843
|
+
toolName: toolCallDelta.function.name
|
|
2844
|
+
});
|
|
2845
|
+
const metadata = (_b2 = this.extractMetadata) == null ? void 0 : _b2.call(this, toolCallDelta);
|
|
2846
|
+
this.toolCalls[index] = {
|
|
2847
|
+
id: toolCallDelta.id,
|
|
2848
|
+
type: "function",
|
|
2849
|
+
function: {
|
|
2850
|
+
name: toolCallDelta.function.name,
|
|
2851
|
+
arguments: (_c = toolCallDelta.function.arguments) != null ? _c : ""
|
|
2852
|
+
},
|
|
2853
|
+
hasFinished: false,
|
|
2854
|
+
metadata
|
|
2855
|
+
};
|
|
2856
|
+
const toolCall = this.toolCalls[index];
|
|
2857
|
+
if (toolCall.function.arguments.length > 0) {
|
|
2858
|
+
enqueue({
|
|
2859
|
+
type: "tool-input-delta",
|
|
2860
|
+
id: toolCall.id,
|
|
2861
|
+
delta: toolCall.function.arguments
|
|
2862
|
+
});
|
|
2863
|
+
}
|
|
2864
|
+
if (isParsableJson(toolCall.function.arguments)) {
|
|
2865
|
+
this.finishToolCall(toolCall, enqueue);
|
|
2866
|
+
}
|
|
2867
|
+
}
|
|
2868
|
+
processExistingToolCall(index, toolCallDelta, enqueue) {
|
|
2869
|
+
var _a2;
|
|
2870
|
+
const toolCall = this.toolCalls[index];
|
|
2871
|
+
if (toolCall.hasFinished) {
|
|
2872
|
+
return;
|
|
2873
|
+
}
|
|
2874
|
+
if (((_a2 = toolCallDelta.function) == null ? void 0 : _a2.arguments) != null) {
|
|
2875
|
+
toolCall.function.arguments += toolCallDelta.function.arguments;
|
|
2876
|
+
enqueue({
|
|
2877
|
+
type: "tool-input-delta",
|
|
2878
|
+
id: toolCall.id,
|
|
2879
|
+
delta: toolCallDelta.function.arguments
|
|
2880
|
+
});
|
|
2881
|
+
}
|
|
2882
|
+
if (isParsableJson(toolCall.function.arguments)) {
|
|
2883
|
+
this.finishToolCall(toolCall, enqueue);
|
|
2884
|
+
}
|
|
2885
|
+
}
|
|
2886
|
+
finishToolCall(toolCall, enqueue) {
|
|
2887
|
+
var _a2, _b2;
|
|
2888
|
+
enqueue({
|
|
2889
|
+
type: "tool-input-end",
|
|
2890
|
+
id: toolCall.id
|
|
2891
|
+
});
|
|
2892
|
+
const providerMetadata = (_a2 = this.buildToolCallProviderMetadata) == null ? void 0 : _a2.call(
|
|
2893
|
+
this,
|
|
2894
|
+
toolCall.metadata
|
|
2895
|
+
);
|
|
2896
|
+
enqueue({
|
|
2897
|
+
type: "tool-call",
|
|
2898
|
+
toolCallId: (_b2 = toolCall.id) != null ? _b2 : this._generateId(),
|
|
2899
|
+
toolName: toolCall.function.name,
|
|
2900
|
+
input: toolCall.function.arguments,
|
|
2901
|
+
...providerMetadata ? { providerMetadata } : {}
|
|
2902
|
+
});
|
|
2903
|
+
toolCall.hasFinished = true;
|
|
2904
|
+
}
|
|
2905
|
+
};
|
|
2906
|
+
|
|
2773
2907
|
// src/strip-file-extension.ts
|
|
2774
2908
|
function stripFileExtension(filename) {
|
|
2775
2909
|
const firstDotIndex = filename.indexOf(".");
|
|
@@ -2820,6 +2954,7 @@ export {
|
|
|
2820
2954
|
DelayedPromise,
|
|
2821
2955
|
DownloadError,
|
|
2822
2956
|
EventSourceParserStream2 as EventSourceParserStream,
|
|
2957
|
+
StreamingToolCallTracker,
|
|
2823
2958
|
VERSION,
|
|
2824
2959
|
WORKFLOW_DESERIALIZE,
|
|
2825
2960
|
WORKFLOW_SERIALIZE,
|