ai 7.0.107 → 7.0.109

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.
@@ -1,7 +1,7 @@
1
1
  import * as _ai_sdk_provider from '@ai-sdk/provider';
2
2
  import { EmbeddingModelV4Embedding, LanguageModelV4Source, SharedV4Warning, LanguageModelV4, LanguageModelV3, LanguageModelV2, SharedV4ProviderMetadata, JSONObject, LanguageModelV4Usage, LanguageModelV4CallOptions, LanguageModelV4Prompt, AISDKError, LanguageModelV4ToolCall, JSONSchema7, ProviderV4, ProviderV3, ProviderV2, LanguageModelV4ToolResultOutput, LanguageModelV4ToolChoice, LanguageModelV4FunctionTool, LanguageModelV4ProviderTool } from '@ai-sdk/provider';
3
3
  import { GatewayModelId } from '@ai-sdk/gateway';
4
- import { ModelMessage, AssistantModelMessage, ToolModelMessage, Context, ProviderOptions, ToolSet, SystemModelMessage, InferToolSetContext, Arrayable, InferToolInput, InferToolOutput, ReasoningPart, ReasoningFilePart, MaybePromiseLike, ToolExecutionOptions, InferToolContext, ToolResultOutput, Tool, Experimental_SandboxSession, ShouldRetryFunction, RetryFunction, ToolApprovalRequest, ToolApprovalResponse, ToolResultPart } from '@ai-sdk/provider-utils';
4
+ import { ModelMessage, AssistantModelMessage, ToolModelMessage, Context, ProviderOptions, ToolSet, SystemModelMessage, InferToolSetContext, Arrayable, InferToolInput, InferToolOutput, ReasoningPart, ReasoningFilePart, MaybePromiseLike, ToolExecutionOptions, InferToolContext, HasRequiredKey, ToolResultOutput, Tool, Experimental_SandboxSession, ShouldRetryFunction, RetryFunction, FlexibleSchema, ToolApprovalRequest, ToolApprovalResponse, ToolResultPart } from '@ai-sdk/provider-utils';
5
5
  export { convertAsyncIteratorToReadableStream } from '@ai-sdk/provider-utils';
6
6
 
7
7
  /**
@@ -1763,6 +1763,7 @@ declare class NoSuchToolError extends AISDKError {
1763
1763
  * @param options.tools - The tools that are available.
1764
1764
  * @param options.inputSchema - A function that returns the JSON Schema for a tool.
1765
1765
  * @param options.error - The error that occurred while parsing the tool call.
1766
+ * @param options.abortSignal - An optional signal for cancelling the repair.
1766
1767
  */
1767
1768
  type ToolCallRepairFunction<TOOLS extends ToolSet> = (options: {
1768
1769
  instructions: Instructions | undefined;
@@ -1777,6 +1778,7 @@ type ToolCallRepairFunction<TOOLS extends ToolSet> = (options: {
1777
1778
  toolName: string;
1778
1779
  }) => PromiseLike<JSONSchema7>;
1779
1780
  error: NoSuchToolError | InvalidToolInputError;
1781
+ abortSignal?: AbortSignal;
1780
1782
  }) => Promise<LanguageModelV4ToolCall | null>;
1781
1783
 
1782
1784
  type ToolOutput<TOOLS extends ToolSet> = TypedToolResult<TOOLS> | TypedToolError<TOOLS>;
@@ -1924,6 +1926,22 @@ type ToolInputRefinement<TOOLS extends ToolSet> = {
1924
1926
  [NAME in keyof TOOLS]?: (input: InferToolInput<TOOLS[NAME]>) => MaybePromiseLike<InferToolInput<TOOLS[NAME]>>;
1925
1927
  };
1926
1928
 
1929
+ /**
1930
+ * Checks whether a tool context map contains any contextual tool entries.
1931
+ */
1932
+ type IsEmptyObject<OBJECT> = keyof OBJECT extends never ? true : false;
1933
+ /**
1934
+ * Makes the toolsContext setting optional, required, or unavailable based on
1935
+ * the tool set.
1936
+ */
1937
+ type ToolsContextSettings<TOOLS extends ToolSet> = IsEmptyObject<InferToolSetContext<TOOLS>> extends true ? {
1938
+ toolsContext?: never;
1939
+ } : HasRequiredKey<InferToolSetContext<TOOLS>> extends true ? {
1940
+ toolsContext: InferToolSetContext<TOOLS>;
1941
+ } : {
1942
+ toolsContext?: InferToolSetContext<TOOLS>;
1943
+ };
1944
+
1927
1945
  type EnrichedStreamPart<TOOLS extends ToolSet, PARTIAL_OUTPUT> = {
1928
1946
  part: TextStreamPart<TOOLS>;
1929
1947
  partialOutput: PARTIAL_OUTPUT | undefined;
@@ -2681,15 +2699,32 @@ declare function createRestrictedTelemetryDispatcher<TOOLS extends ToolSet, RUNT
2681
2699
  includeToolsContext?: IncludedToolsContext<TOOLS>;
2682
2700
  }): RestrictedTelemetryDispatcher<TOOLS, RUNTIME_CONTEXT, OUTPUT>;
2683
2701
 
2684
- declare function parseToolCall<TOOLS extends ToolSet>({ toolCall, tools, repairToolCall, refineToolInput, messages, instructions, }: {
2702
+ declare function parseToolCall<TOOLS extends ToolSet>({ toolCall, tools, repairToolCall, refineToolInput, messages, instructions, abortSignal, }: {
2685
2703
  toolCall: LanguageModelV4ToolCall;
2686
2704
  tools: TOOLS | undefined;
2687
2705
  repairToolCall: ToolCallRepairFunction<TOOLS> | undefined;
2688
2706
  refineToolInput?: ToolInputRefinement<TOOLS> | undefined;
2689
2707
  instructions: Instructions | undefined;
2690
2708
  messages: ModelMessage[];
2709
+ abortSignal?: AbortSignal;
2691
2710
  }): Promise<TypedToolCall<TOOLS>>;
2692
2711
 
2712
+ /**
2713
+ * Validates a tool context value against the tool's optional context schema.
2714
+ *
2715
+ * When no context schema is defined, the original context value is returned as-is.
2716
+ * Otherwise, the context is validated and normalized through the schema before
2717
+ * being passed into tool execution and approval hooks.
2718
+ *
2719
+ * @throws {TypeValidationError} When the provided tool context does not match
2720
+ * the tool's declared `contextSchema`.
2721
+ */
2722
+ declare function validateToolContext<CONTEXT>({ toolName, context, contextSchema, }: {
2723
+ toolName: string;
2724
+ context: unknown;
2725
+ contextSchema: FlexibleSchema<CONTEXT> | undefined;
2726
+ }): Promise<CONTEXT>;
2727
+
2693
2728
  /** Create discovery state for one generation, never for a shared tool instance. */
2694
2729
  declare function createToolSearchState({ tools, toolCallers, }: {
2695
2730
  tools: ToolSet | undefined;
@@ -2761,4 +2796,4 @@ declare function toResponseMessages<TOOLS extends ToolSet>({ content: inputConte
2761
2796
  tools: TOOLS | undefined;
2762
2797
  }): Promise<Array<AssistantModelMessage | ToolModelMessage>>;
2763
2798
 
2764
- export { type CollectedToolApprovals, DefaultStepResult, type DownloadFunction, addLanguageModelUsage, asLanguageModelUsage, collectToolApprovals, convertToLanguageModelPrompt, createAsyncIterableStream, createDefaultDownloadFunction, createNullLanguageModelUsage, createRestrictedTelemetryDispatcher, createTelemetryDispatcher, createToolModelOutput, createToolSearchState, downloadAssets, mapToolResultOutput, mergeAbortSignals, mergeCallbacks, parseToolCall, prepareLanguageModelCallOptions as prepareCallSettings, prepareLanguageModelCallOptions, prepareRetries, prepareToolChoice, prepareTools, resolveLanguageModel, signToolApproval, standardizePrompt, toResponseMessages, validateApprovedToolApprovals, verifyToolApprovalSignature };
2799
+ export { type CollectedToolApprovals, DefaultStepResult, type DownloadFunction, type ToolsContextSettings, addLanguageModelUsage, asLanguageModelUsage, collectToolApprovals, convertToLanguageModelPrompt, createAsyncIterableStream, createDefaultDownloadFunction, createNullLanguageModelUsage, createRestrictedTelemetryDispatcher, createTelemetryDispatcher, createToolModelOutput, createToolSearchState, downloadAssets, mapToolResultOutput, mergeAbortSignals, mergeCallbacks, parseToolCall, prepareLanguageModelCallOptions as prepareCallSettings, prepareLanguageModelCallOptions, prepareRetries, prepareToolChoice, prepareTools, resolveLanguageModel, signToolApproval, standardizePrompt, toResponseMessages, validateApprovedToolApprovals, validateToolContext, verifyToolApprovalSignature };
@@ -91,7 +91,7 @@ import {
91
91
  } from "@ai-sdk/provider-utils";
92
92
 
93
93
  // src/version.ts
94
- var VERSION = true ? "7.0.107" : "0.0.0-test";
94
+ var VERSION = true ? "7.0.109" : "0.0.0-test";
95
95
 
96
96
  // src/util/download/download.ts
97
97
  var download = async ({
@@ -2126,6 +2126,9 @@ function asImageModelV4(model) {
2126
2126
  });
2127
2127
  }
2128
2128
 
2129
+ // src/model/as-language-model-v4.ts
2130
+ import { convertUint8ArrayToBase64 } from "@ai-sdk/provider-utils";
2131
+
2129
2132
  // src/model/as-language-model-v3.ts
2130
2133
  function asLanguageModelV3(model) {
2131
2134
  if (model.specificationVersion === "v3") {
@@ -2213,11 +2216,131 @@ function asLanguageModelV4(model) {
2213
2216
  const v3Model = model.specificationVersion === "v2" ? asLanguageModelV3(model) : model;
2214
2217
  return new Proxy(v3Model, {
2215
2218
  get(target, prop) {
2216
- if (prop === "specificationVersion") return "v4";
2217
- return target[prop];
2219
+ switch (prop) {
2220
+ case "specificationVersion":
2221
+ return "v4";
2222
+ case "doGenerate":
2223
+ return async (options) => {
2224
+ const result = await target.doGenerate({
2225
+ ...options,
2226
+ prompt: convertV4PromptToV3(options.prompt)
2227
+ });
2228
+ return {
2229
+ ...result,
2230
+ content: result.content.map(convertV3ContentToV4)
2231
+ };
2232
+ };
2233
+ case "doStream":
2234
+ return async (options) => {
2235
+ const result = await target.doStream({
2236
+ ...options,
2237
+ prompt: convertV4PromptToV3(options.prompt)
2238
+ });
2239
+ return {
2240
+ ...result,
2241
+ stream: convertV3StreamToV4(result.stream)
2242
+ };
2243
+ };
2244
+ default:
2245
+ return target[prop];
2246
+ }
2218
2247
  }
2219
2248
  });
2220
2249
  }
2250
+ function convertV4PromptToV3(prompt) {
2251
+ return prompt.map((message) => {
2252
+ if (message.role === "system") {
2253
+ return message;
2254
+ }
2255
+ return {
2256
+ ...message,
2257
+ content: message.content.map((part) => {
2258
+ switch (part.type) {
2259
+ case "file":
2260
+ return {
2261
+ ...part,
2262
+ data: convertV4FileDataToV3(part.data)
2263
+ };
2264
+ case "tool-result":
2265
+ return {
2266
+ ...part,
2267
+ output: convertV4ToolResultOutputToV3(part.output)
2268
+ };
2269
+ default:
2270
+ return part;
2271
+ }
2272
+ })
2273
+ };
2274
+ });
2275
+ }
2276
+ function convertV4FileDataToV3(data) {
2277
+ switch (data.type) {
2278
+ case "data":
2279
+ return data.data;
2280
+ case "url":
2281
+ return data.url;
2282
+ case "reference":
2283
+ case "text":
2284
+ return data;
2285
+ }
2286
+ }
2287
+ function convertV4ToolResultOutputToV3(output) {
2288
+ if (output.type !== "content") {
2289
+ return output;
2290
+ }
2291
+ return {
2292
+ ...output,
2293
+ value: output.value.map((part) => {
2294
+ if (part.type !== "file") {
2295
+ return part;
2296
+ }
2297
+ switch (part.data.type) {
2298
+ case "data":
2299
+ return {
2300
+ type: "file-data",
2301
+ data: typeof part.data.data === "string" ? part.data.data : convertUint8ArrayToBase64(part.data.data),
2302
+ mediaType: part.mediaType,
2303
+ filename: part.filename,
2304
+ providerOptions: part.providerOptions
2305
+ };
2306
+ case "url":
2307
+ return {
2308
+ type: "file-url",
2309
+ url: part.data.url.toString(),
2310
+ providerOptions: part.providerOptions
2311
+ };
2312
+ case "reference":
2313
+ return {
2314
+ type: "file-id",
2315
+ fileId: part.data.reference,
2316
+ providerOptions: part.providerOptions
2317
+ };
2318
+ case "text":
2319
+ return part;
2320
+ }
2321
+ })
2322
+ };
2323
+ }
2324
+ function convertV3ContentToV4(content) {
2325
+ return content.type === "file" ? {
2326
+ ...content,
2327
+ data: { type: "data", data: content.data }
2328
+ } : content;
2329
+ }
2330
+ function convertV3StreamToV4(stream) {
2331
+ return stream.pipeThrough(
2332
+ new TransformStream({
2333
+ transform(chunk, controller) {
2334
+ controller.enqueue(
2335
+ chunk.type === "file" ? {
2336
+ ...chunk,
2337
+ data: { type: "data", data: chunk.data }
2338
+ } : chunk
2339
+ );
2340
+ }
2341
+ })
2342
+ );
2343
+ }
2221
2344
 
2222
2345
  // src/model/as-reranking-model-v4.ts
2223
2346
  function asRerankingModelV4(model) {
@@ -2968,7 +3091,8 @@ async function parseToolCall({
2968
3091
  repairToolCall,
2969
3092
  refineToolInput,
2970
3093
  messages,
2971
- instructions
3094
+ instructions,
3095
+ abortSignal
2972
3096
  }) {
2973
3097
  try {
2974
3098
  if (tools == null) {
@@ -2991,20 +3115,26 @@ async function parseToolCall({
2991
3115
  }
2992
3116
  let repairedToolCall = null;
2993
3117
  try {
2994
- repairedToolCall = await repairToolCall({
2995
- toolCall,
2996
- tools,
2997
- inputSchema: async ({ toolName }) => {
2998
- var _a12;
2999
- const inputSchema = (_a12 = getOwn(tools, toolName)) == null ? void 0 : _a12.inputSchema;
3000
- return await asSchema2(inputSchema).jsonSchema;
3001
- },
3002
- instructions,
3003
- system: instructions,
3004
- messages,
3005
- error
3118
+ abortSignal == null ? void 0 : abortSignal.throwIfAborted();
3119
+ repairedToolCall = await waitForPromiseWithAbortSignal({
3120
+ promise: repairToolCall({
3121
+ toolCall,
3122
+ tools,
3123
+ inputSchema: async ({ toolName }) => {
3124
+ var _a12;
3125
+ const inputSchema = (_a12 = getOwn(tools, toolName)) == null ? void 0 : _a12.inputSchema;
3126
+ return await asSchema2(inputSchema).jsonSchema;
3127
+ },
3128
+ instructions,
3129
+ system: instructions,
3130
+ messages,
3131
+ error,
3132
+ abortSignal
3133
+ }),
3134
+ abortSignal
3006
3135
  });
3007
3136
  } catch (repairError) {
3137
+ abortSignal == null ? void 0 : abortSignal.throwIfAborted();
3008
3138
  throw new ToolCallRepairError({
3009
3139
  cause: repairError,
3010
3140
  originalError: error
@@ -3013,12 +3143,15 @@ async function parseToolCall({
3013
3143
  if (repairedToolCall == null) {
3014
3144
  throw error;
3015
3145
  }
3016
- return await refineParsedToolCallInput({
3146
+ const parsedRepairedToolCall = await refineParsedToolCallInput({
3017
3147
  toolCall: await doParseToolCall({ toolCall: repairedToolCall, tools }),
3018
3148
  refineToolInput
3019
3149
  });
3150
+ abortSignal == null ? void 0 : abortSignal.throwIfAborted();
3151
+ return parsedRepairedToolCall;
3020
3152
  }
3021
3153
  } catch (error) {
3154
+ abortSignal == null ? void 0 : abortSignal.throwIfAborted();
3022
3155
  const parsedInput = await safeParseJSON({ text: toolCall.input });
3023
3156
  const input = parsedInput.success ? parsedInput.value : toolCall.input;
3024
3157
  const tool2 = getOwn(tools, toolCall.toolName);
@@ -3037,6 +3170,34 @@ async function parseToolCall({
3037
3170
  };
3038
3171
  }
3039
3172
  }
3173
+ async function waitForPromiseWithAbortSignal({
3174
+ promise,
3175
+ abortSignal
3176
+ }) {
3177
+ if (abortSignal == null) {
3178
+ return await promise;
3179
+ }
3180
+ return await new Promise((resolve, reject) => {
3181
+ const cleanup = () => {
3182
+ abortSignal.removeEventListener("abort", onAbort);
3183
+ };
3184
+ const onAbort = () => {
3185
+ cleanup();
3186
+ reject(abortSignal.reason);
3187
+ };
3188
+ Promise.resolve(promise).then((value) => {
3189
+ cleanup();
3190
+ resolve(value);
3191
+ }).catch((error) => {
3192
+ cleanup();
3193
+ reject(error);
3194
+ });
3195
+ abortSignal.addEventListener("abort", onAbort, { once: true });
3196
+ if (abortSignal.aborted) {
3197
+ onAbort();
3198
+ }
3199
+ });
3200
+ }
3040
3201
  async function refineParsedToolCallInput({
3041
3202
  toolCall,
3042
3203
  refineToolInput
@@ -3115,6 +3276,26 @@ async function doParseToolCall({
3115
3276
  };
3116
3277
  }
3117
3278
 
3279
+ // src/generate-text/validate-tool-context.ts
3280
+ import { validateTypes } from "@ai-sdk/provider-utils";
3281
+ async function validateToolContext({
3282
+ toolName,
3283
+ context,
3284
+ contextSchema
3285
+ }) {
3286
+ if (contextSchema == null) {
3287
+ return context;
3288
+ }
3289
+ return await validateTypes({
3290
+ value: context,
3291
+ schema: contextSchema,
3292
+ context: {
3293
+ field: "tool context",
3294
+ entityName: toolName
3295
+ }
3296
+ });
3297
+ }
3298
+
3118
3299
  // src/tool-search/prepare-tool-search.ts
3119
3300
  import {
3120
3301
  experimental_getToolCaller as experimental_getToolCaller2
@@ -3304,7 +3485,7 @@ function collectToolApprovals({
3304
3485
  import { convertBase64ToUint8Array } from "@ai-sdk/provider-utils";
3305
3486
 
3306
3487
  // src/util/canonical-hash.ts
3307
- import { convertUint8ArrayToBase64 } from "@ai-sdk/provider-utils";
3488
+ import { convertUint8ArrayToBase64 as convertUint8ArrayToBase642 } from "@ai-sdk/provider-utils";
3308
3489
  var encoder = new TextEncoder();
3309
3490
  function canonicalJSON(value) {
3310
3491
  if (value === null || value === void 0) {
@@ -3323,7 +3504,7 @@ function canonicalJSON(value) {
3323
3504
  return `{${entries.join(",")}}`;
3324
3505
  }
3325
3506
  function toBase64url(bytes) {
3326
- return convertUint8ArrayToBase64(bytes).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/g, "");
3507
+ return convertUint8ArrayToBase642(bytes).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/g, "");
3327
3508
  }
3328
3509
  async function hashCanonical(value) {
3329
3510
  const digest = await crypto.subtle.digest(
@@ -3414,26 +3595,6 @@ import {
3414
3595
  safeValidateTypes as safeValidateTypes3
3415
3596
  } from "@ai-sdk/provider-utils";
3416
3597
 
3417
- // src/generate-text/validate-tool-context.ts
3418
- import { validateTypes } from "@ai-sdk/provider-utils";
3419
- async function validateToolContext({
3420
- toolName,
3421
- context,
3422
- contextSchema
3423
- }) {
3424
- if (contextSchema == null) {
3425
- return context;
3426
- }
3427
- return await validateTypes({
3428
- value: context,
3429
- schema: contextSchema,
3430
- context: {
3431
- field: "tool context",
3432
- entityName: toolName
3433
- }
3434
- });
3435
- }
3436
-
3437
3598
  // src/generate-text/resolve-tool-approval.ts
3438
3599
  async function resolveToolApproval({
3439
3600
  tools,
@@ -3803,6 +3964,7 @@ export {
3803
3964
  standardizePrompt,
3804
3965
  toResponseMessages,
3805
3966
  validateApprovedToolApprovals,
3967
+ validateToolContext,
3806
3968
  verifyToolApprovalSignature
3807
3969
  };
3808
3970
  //# sourceMappingURL=index.js.map