ai 7.0.107 → 7.0.108

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.
@@ -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>;
@@ -2681,13 +2683,14 @@ declare function createRestrictedTelemetryDispatcher<TOOLS extends ToolSet, RUNT
2681
2683
  includeToolsContext?: IncludedToolsContext<TOOLS>;
2682
2684
  }): RestrictedTelemetryDispatcher<TOOLS, RUNTIME_CONTEXT, OUTPUT>;
2683
2685
 
2684
- declare function parseToolCall<TOOLS extends ToolSet>({ toolCall, tools, repairToolCall, refineToolInput, messages, instructions, }: {
2686
+ declare function parseToolCall<TOOLS extends ToolSet>({ toolCall, tools, repairToolCall, refineToolInput, messages, instructions, abortSignal, }: {
2685
2687
  toolCall: LanguageModelV4ToolCall;
2686
2688
  tools: TOOLS | undefined;
2687
2689
  repairToolCall: ToolCallRepairFunction<TOOLS> | undefined;
2688
2690
  refineToolInput?: ToolInputRefinement<TOOLS> | undefined;
2689
2691
  instructions: Instructions | undefined;
2690
2692
  messages: ModelMessage[];
2693
+ abortSignal?: AbortSignal;
2691
2694
  }): Promise<TypedToolCall<TOOLS>>;
2692
2695
 
2693
2696
  /** Create discovery state for one generation, never for a shared tool instance. */
@@ -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.108" : "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
@@ -3304,7 +3465,7 @@ function collectToolApprovals({
3304
3465
  import { convertBase64ToUint8Array } from "@ai-sdk/provider-utils";
3305
3466
 
3306
3467
  // src/util/canonical-hash.ts
3307
- import { convertUint8ArrayToBase64 } from "@ai-sdk/provider-utils";
3468
+ import { convertUint8ArrayToBase64 as convertUint8ArrayToBase642 } from "@ai-sdk/provider-utils";
3308
3469
  var encoder = new TextEncoder();
3309
3470
  function canonicalJSON(value) {
3310
3471
  if (value === null || value === void 0) {
@@ -3323,7 +3484,7 @@ function canonicalJSON(value) {
3323
3484
  return `{${entries.join(",")}}`;
3324
3485
  }
3325
3486
  function toBase64url(bytes) {
3326
- return convertUint8ArrayToBase64(bytes).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/g, "");
3487
+ return convertUint8ArrayToBase642(bytes).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/g, "");
3327
3488
  }
3328
3489
  async function hashCanonical(value) {
3329
3490
  const digest = await crypto.subtle.digest(