@ai-sdk/provider-utils 5.0.0-beta.3 → 5.0.0-beta.30

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.
Files changed (110) hide show
  1. package/CHANGELOG.md +284 -0
  2. package/dist/index.d.ts +1339 -813
  3. package/dist/index.js +837 -288
  4. package/dist/index.js.map +1 -1
  5. package/dist/test/index.d.ts +2 -1
  6. package/dist/test/index.js +18 -37
  7. package/dist/test/index.js.map +1 -1
  8. package/package.json +13 -13
  9. package/src/add-additional-properties-to-json-schema.ts +1 -1
  10. package/src/as-array.ts +12 -0
  11. package/src/convert-image-model-file-to-data-uri.ts +3 -3
  12. package/src/convert-inline-file-data-to-uint8-array.ts +30 -0
  13. package/src/create-tool-name-mapping.ts +6 -22
  14. package/src/detect-media-type.ts +312 -0
  15. package/src/filter-nullable.ts +11 -0
  16. package/src/get-error-message.ts +1 -15
  17. package/src/get-from-api.ts +2 -2
  18. package/src/has-required-key.ts +6 -0
  19. package/src/index.ts +42 -12
  20. package/src/inject-json-instruction.ts +6 -6
  21. package/src/is-buffer.ts +9 -0
  22. package/src/is-json-serializable.ts +29 -0
  23. package/src/is-provider-reference.ts +21 -0
  24. package/src/is-url-supported.ts +17 -2
  25. package/src/load-api-key.ts +1 -1
  26. package/src/load-setting.ts +1 -1
  27. package/src/map-reasoning-to-provider.ts +108 -0
  28. package/src/maybe-promise-like.ts +3 -0
  29. package/src/parse-json-event-stream.ts +3 -3
  30. package/src/parse-json.ts +3 -3
  31. package/src/parse-provider-options.ts +1 -1
  32. package/src/post-to-api.ts +4 -4
  33. package/src/provider-defined-tool-factory.ts +129 -0
  34. package/src/provider-executed-tool-factory.ts +69 -0
  35. package/src/resolve-full-media-type.ts +49 -0
  36. package/src/resolve-provider-reference.ts +26 -0
  37. package/src/resolve.ts +16 -1
  38. package/src/response-handler.ts +3 -3
  39. package/src/schema.ts +6 -3
  40. package/src/secure-json-parse.ts +1 -1
  41. package/src/serialize-model-options.ts +63 -0
  42. package/src/streaming-tool-call-tracker.ts +241 -0
  43. package/src/test/convert-response-stream-to-array.ts +1 -1
  44. package/src/test/is-node-version.ts +22 -1
  45. package/src/to-json-schema/zod3-to-json-schema/options.ts +3 -3
  46. package/src/to-json-schema/zod3-to-json-schema/parse-def.ts +3 -3
  47. package/src/to-json-schema/zod3-to-json-schema/parse-types.ts +22 -22
  48. package/src/to-json-schema/zod3-to-json-schema/parsers/array.ts +3 -3
  49. package/src/to-json-schema/zod3-to-json-schema/parsers/bigint.ts +1 -1
  50. package/src/to-json-schema/zod3-to-json-schema/parsers/branded.ts +2 -2
  51. package/src/to-json-schema/zod3-to-json-schema/parsers/catch.ts +2 -2
  52. package/src/to-json-schema/zod3-to-json-schema/parsers/date.ts +4 -4
  53. package/src/to-json-schema/zod3-to-json-schema/parsers/default.ts +3 -3
  54. package/src/to-json-schema/zod3-to-json-schema/parsers/effects.ts +3 -3
  55. package/src/to-json-schema/zod3-to-json-schema/parsers/enum.ts +1 -1
  56. package/src/to-json-schema/zod3-to-json-schema/parsers/intersection.ts +5 -5
  57. package/src/to-json-schema/zod3-to-json-schema/parsers/literal.ts +1 -1
  58. package/src/to-json-schema/zod3-to-json-schema/parsers/map.ts +4 -5
  59. package/src/to-json-schema/zod3-to-json-schema/parsers/native-enum.ts +1 -1
  60. package/src/to-json-schema/zod3-to-json-schema/parsers/never.ts +1 -2
  61. package/src/to-json-schema/zod3-to-json-schema/parsers/nullable.ts +4 -4
  62. package/src/to-json-schema/zod3-to-json-schema/parsers/number.ts +1 -1
  63. package/src/to-json-schema/zod3-to-json-schema/parsers/object.ts +3 -3
  64. package/src/to-json-schema/zod3-to-json-schema/parsers/optional.ts +3 -3
  65. package/src/to-json-schema/zod3-to-json-schema/parsers/pipeline.ts +4 -4
  66. package/src/to-json-schema/zod3-to-json-schema/parsers/promise.ts +3 -3
  67. package/src/to-json-schema/zod3-to-json-schema/parsers/readonly.ts +2 -2
  68. package/src/to-json-schema/zod3-to-json-schema/parsers/record.ts +9 -10
  69. package/src/to-json-schema/zod3-to-json-schema/parsers/set.ts +3 -3
  70. package/src/to-json-schema/zod3-to-json-schema/parsers/string.ts +2 -2
  71. package/src/to-json-schema/zod3-to-json-schema/parsers/tuple.ts +3 -3
  72. package/src/to-json-schema/zod3-to-json-schema/parsers/undefined.ts +1 -2
  73. package/src/to-json-schema/zod3-to-json-schema/parsers/union.ts +3 -3
  74. package/src/to-json-schema/zod3-to-json-schema/parsers/unknown.ts +1 -2
  75. package/src/to-json-schema/zod3-to-json-schema/refs.ts +3 -3
  76. package/src/to-json-schema/zod3-to-json-schema/select-parser.ts +2 -2
  77. package/src/to-json-schema/zod3-to-json-schema/zod3-to-json-schema.ts +3 -3
  78. package/src/types/assistant-model-message.ts +5 -3
  79. package/src/types/content-part.ts +102 -24
  80. package/src/types/context.ts +4 -0
  81. package/src/types/executable-tool.ts +17 -0
  82. package/src/types/execute-tool.ts +29 -9
  83. package/src/types/file-data.ts +48 -0
  84. package/src/types/index.ts +26 -11
  85. package/src/types/infer-tool-context.ts +12 -0
  86. package/src/types/infer-tool-input.ts +7 -0
  87. package/src/types/infer-tool-output.ts +7 -0
  88. package/src/types/infer-tool-set-context.ts +15 -0
  89. package/src/types/model-message.ts +4 -4
  90. package/src/types/never-optional.ts +7 -0
  91. package/src/types/provider-options.ts +2 -2
  92. package/src/types/provider-reference.ts +10 -0
  93. package/src/types/sensitive-context.ts +9 -0
  94. package/src/types/system-model-message.ts +1 -1
  95. package/src/types/tool-approval-request.ts +7 -0
  96. package/src/types/tool-execute-function.ts +50 -0
  97. package/src/types/tool-model-message.ts +3 -3
  98. package/src/types/tool-needs-approval-function.ts +39 -0
  99. package/src/types/tool-set.ts +22 -0
  100. package/src/types/tool.ts +251 -222
  101. package/src/types/user-model-message.ts +2 -2
  102. package/src/validate-download-url.ts +7 -2
  103. package/src/validate-types.ts +5 -3
  104. package/dist/index.d.mts +0 -1458
  105. package/dist/index.mjs +0 -2759
  106. package/dist/index.mjs.map +0 -1
  107. package/dist/test/index.d.mts +0 -17
  108. package/dist/test/index.mjs +0 -77
  109. package/dist/test/index.mjs.map +0 -1
  110. package/src/provider-tool-factory.ts +0 -125
@@ -3,6 +3,7 @@ export type {
3
3
  AssistantModelMessage,
4
4
  } from './assistant-model-message';
5
5
  export type {
6
+ CustomPart,
6
7
  FilePart,
7
8
  ImagePart,
8
9
  ReasoningFilePart,
@@ -12,30 +13,44 @@ export type {
12
13
  ToolResultOutput,
13
14
  ToolResultPart,
14
15
  } from './content-part';
16
+ export type { Context } from './context';
15
17
  export type { DataContent } from './data-content';
16
18
  export { executeTool } from './execute-tool';
19
+ export type {
20
+ FileData,
21
+ FileDataData,
22
+ FileDataReference,
23
+ FileDataText,
24
+ FileDataUrl,
25
+ } from './file-data';
26
+ export { isExecutableTool, type ExecutableTool } from './executable-tool';
27
+ export type { InferToolContext } from './infer-tool-context';
28
+ export type { InferToolInput } from './infer-tool-input';
29
+ export type { InferToolOutput } from './infer-tool-output';
30
+ export type { InferToolSetContext } from './infer-tool-set-context';
17
31
  export type { ModelMessage } from './model-message';
18
32
  export type { ProviderOptions } from './provider-options';
33
+ export type { ProviderReference } from './provider-reference';
34
+ export type { SensitiveContext } from './sensitive-context';
19
35
  export type { SystemModelMessage } from './system-model-message';
20
36
  export {
21
37
  dynamicTool,
22
38
  tool,
23
- type InferToolInput,
24
- type InferToolOutput,
39
+ type DynamicTool,
40
+ type FunctionTool,
41
+ type ProviderDefinedTool,
42
+ type ProviderExecutedTool,
25
43
  type Tool,
26
- type ToolExecutionOptions,
27
- type ToolExecuteFunction,
28
- type ToolNeedsApprovalFunction,
29
44
  } from './tool';
45
+ export type {
46
+ ToolExecuteFunction,
47
+ ToolExecutionOptions,
48
+ } from './tool-execute-function';
49
+ export type { ToolNeedsApprovalFunction } from './tool-needs-approval-function';
50
+ export type { ToolSet } from './tool-set';
30
51
  export type { ToolApprovalRequest } from './tool-approval-request';
31
52
  export type { ToolApprovalResponse } from './tool-approval-response';
32
53
  export type { ToolCall } from './tool-call';
33
54
  export type { ToolContent, ToolModelMessage } from './tool-model-message';
34
55
  export type { ToolResult } from './tool-result';
35
56
  export type { UserContent, UserModelMessage } from './user-model-message';
36
- import type { ToolExecutionOptions } from './tool';
37
-
38
- /**
39
- * @deprecated Use ToolExecutionOptions instead.
40
- */
41
- export type ToolCallOptions = ToolExecutionOptions;
@@ -0,0 +1,12 @@
1
+ import type { HasRequiredKey } from '../has-required-key';
2
+ import type { Tool } from './tool';
3
+
4
+ /**
5
+ * Infer the context type of a tool.
6
+ */
7
+ export type InferToolContext<TOOL extends Tool> =
8
+ TOOL extends Tool<any, any, infer CONTEXT>
9
+ ? HasRequiredKey<CONTEXT> extends true
10
+ ? CONTEXT
11
+ : never
12
+ : never;
@@ -0,0 +1,7 @@
1
+ import type { Tool } from './tool';
2
+
3
+ /**
4
+ * Infer the input type of a tool.
5
+ */
6
+ export type InferToolInput<TOOL extends Tool<any, any, any>> =
7
+ TOOL extends Tool<infer INPUT, any, any> ? INPUT : never;
@@ -0,0 +1,7 @@
1
+ import type { Tool } from './tool';
2
+
3
+ /**
4
+ * Infer the output type of a tool.
5
+ */
6
+ export type InferToolOutput<TOOL extends Tool<any, any, any>> =
7
+ TOOL extends Tool<any, infer OUTPUT, any> ? OUTPUT : never;
@@ -0,0 +1,15 @@
1
+ import type { InferToolContext } from './infer-tool-context';
2
+ import type { ToolSet } from './tool-set';
3
+
4
+ /**
5
+ * Infer the context type for a tool set.
6
+ *
7
+ * The inferred type maps each tool name to its required context type.
8
+ *
9
+ * Tools without required context properties are omitted from the result.
10
+ */
11
+ export type InferToolSetContext<TOOLS extends ToolSet> = {
12
+ [K in keyof TOOLS as InferToolContext<NoInfer<TOOLS[K]>> extends never
13
+ ? never
14
+ : K]: InferToolContext<NoInfer<TOOLS[K]>>;
15
+ };
@@ -1,7 +1,7 @@
1
- import { AssistantModelMessage } from './assistant-model-message';
2
- import { SystemModelMessage } from './system-model-message';
3
- import { ToolModelMessage } from './tool-model-message';
4
- import { UserModelMessage } from './user-model-message';
1
+ import type { AssistantModelMessage } from './assistant-model-message';
2
+ import type { SystemModelMessage } from './system-model-message';
3
+ import type { ToolModelMessage } from './tool-model-message';
4
+ import type { UserModelMessage } from './user-model-message';
5
5
 
6
6
  /**
7
7
  * A message that can be used in the `messages` field of a prompt.
@@ -0,0 +1,7 @@
1
+ // 0 extends 1 & N checks for any
2
+ // [N] extends [never] checks for never
3
+ export type NeverOptional<N, T> = 0 extends 1 & N
4
+ ? Partial<T>
5
+ : [N] extends [never]
6
+ ? Partial<Record<keyof T, undefined>>
7
+ : T;
@@ -1,4 +1,4 @@
1
- import { SharedV3ProviderOptions } from '@ai-sdk/provider';
1
+ import type { SharedV4ProviderOptions } from '@ai-sdk/provider';
2
2
 
3
3
  /**
4
4
  * Additional provider-specific options.
@@ -6,4 +6,4 @@ import { SharedV3ProviderOptions } from '@ai-sdk/provider';
6
6
  * They are passed through to the provider from the AI SDK and enable
7
7
  * provider-specific functionality that can be fully encapsulated in the provider.
8
8
  */
9
- export type ProviderOptions = SharedV3ProviderOptions;
9
+ export type ProviderOptions = SharedV4ProviderOptions;
@@ -0,0 +1,10 @@
1
+ import type { SharedV4ProviderReference } from '@ai-sdk/provider';
2
+
3
+ /**
4
+ * A mapping of provider names to provider-specific file identifiers.
5
+ *
6
+ * Provider references allow files to be identified across different
7
+ * providers without re-uploading, by storing each provider's own
8
+ * identifier for the same logical file.
9
+ */
10
+ export type ProviderReference = SharedV4ProviderReference;
@@ -0,0 +1,9 @@
1
+ import type { Context } from './context';
2
+
3
+ /**
4
+ * Top-level context properties that contain sensitive data and should be
5
+ * excluded from telemetry.
6
+ */
7
+ export type SensitiveContext<CONTEXT extends Context | unknown | never> =
8
+ | { [KEY in keyof CONTEXT]?: boolean }
9
+ | undefined;
@@ -1,4 +1,4 @@
1
- import { ProviderOptions } from './provider-options';
1
+ import type { ProviderOptions } from './provider-options';
2
2
 
3
3
  /**
4
4
  * A system message. It can contain system information.
@@ -13,4 +13,11 @@ export type ToolApprovalRequest = {
13
13
  * ID of the tool call that the approval request is for.
14
14
  */
15
15
  toolCallId: string;
16
+
17
+ /**
18
+ * Flag indicating whether the tool was automatically approved or denied.
19
+ *
20
+ * @default false
21
+ */
22
+ isAutomatic?: boolean;
16
23
  };
@@ -0,0 +1,50 @@
1
+ import type { Context } from './context';
2
+ import type { ModelMessage } from './model-message';
3
+
4
+ /**
5
+ * Additional options that are sent into each tool execution.
6
+ */
7
+ export interface ToolExecutionOptions<
8
+ CONTEXT extends Context | unknown | never,
9
+ > {
10
+ /**
11
+ * The ID of the tool call. You can use it e.g. when sending tool-call related information with stream data.
12
+ */
13
+ toolCallId: string;
14
+
15
+ /**
16
+ * Messages that were sent to the language model to initiate the response that contained the tool call.
17
+ * The messages **do not** include the system prompt nor the assistant response that contained the tool call.
18
+ */
19
+ messages: ModelMessage[];
20
+
21
+ /**
22
+ * An optional abort signal that indicates that the overall operation should be aborted.
23
+ */
24
+ abortSignal?: AbortSignal;
25
+
26
+ /**
27
+ * Tool context as defined by the tool's context schema.
28
+ * The tool context is specific to the tool and is passed to the tool execution.
29
+ *
30
+ * Treat the context object as immutable inside tools.
31
+ * Mutating the context object can lead to race conditions and unexpected results
32
+ * when tools are called in parallel.
33
+ *
34
+ * If you need to mutate the context, analyze the tool calls and results
35
+ * in `prepareStep` and update it there.
36
+ */
37
+ context: CONTEXT;
38
+ }
39
+
40
+ /**
41
+ * Function that executes the tool and returns either a single result or a stream of results.
42
+ */
43
+ export type ToolExecuteFunction<
44
+ INPUT,
45
+ OUTPUT,
46
+ CONTEXT extends Context | unknown | never,
47
+ > = (
48
+ input: INPUT,
49
+ options: ToolExecutionOptions<CONTEXT>,
50
+ ) => AsyncIterable<OUTPUT> | PromiseLike<OUTPUT> | OUTPUT;
@@ -1,6 +1,6 @@
1
- import { ToolResultPart } from './content-part';
2
- import { ProviderOptions } from './provider-options';
3
- import { ToolApprovalResponse } from './tool-approval-response';
1
+ import type { ToolResultPart } from './content-part';
2
+ import type { ProviderOptions } from './provider-options';
3
+ import type { ToolApprovalResponse } from './tool-approval-response';
4
4
 
5
5
  /**
6
6
  * A tool message. It contains the result of one or more tool calls.
@@ -0,0 +1,39 @@
1
+ import type { Context } from './context';
2
+ import type { ModelMessage } from './model-message';
3
+
4
+ /**
5
+ * Function that is called to determine if the tool needs approval before it can be executed.
6
+ *
7
+ * @deprecated Tool approval is handled on a `generateText` / `streamText` level now.
8
+ */
9
+ export type ToolNeedsApprovalFunction<
10
+ INPUT,
11
+ CONTEXT extends Context | unknown | never,
12
+ > = (
13
+ input: INPUT,
14
+ options: {
15
+ /**
16
+ * The ID of the tool call. You can use it e.g. when sending tool-call related information with stream data.
17
+ */
18
+ toolCallId: string;
19
+
20
+ /**
21
+ * Messages that were sent to the language model to initiate the response that contained the tool call.
22
+ * The messages **do not** include the system prompt nor the assistant response that contained the tool call.
23
+ */
24
+ messages: ModelMessage[];
25
+
26
+ /**
27
+ * Tool context as defined by the tool's context schema.
28
+ * The tool context is specific to the tool and is passed to the tool execution.
29
+ *
30
+ * Treat the context object as immutable inside tools.
31
+ * Mutating the context object can lead to race conditions and unexpected results
32
+ * when tools are called in parallel.
33
+ *
34
+ * If you need to mutate the context, analyze the tool calls and results
35
+ * in `prepareStep` and update it there.
36
+ */
37
+ context: CONTEXT;
38
+ },
39
+ ) => boolean | PromiseLike<boolean>;
@@ -0,0 +1,22 @@
1
+ import type { Tool } from './tool';
2
+
3
+ /**
4
+ * A mapping of tool names to tool definitions.
5
+ */
6
+ export type ToolSet = Record<
7
+ string,
8
+ (
9
+ | Tool<never, never, any>
10
+ | Tool<any, any, any>
11
+ | Tool<any, never, any>
12
+ | Tool<never, any, any>
13
+ ) &
14
+ Pick<
15
+ Tool<any, any, any>,
16
+ | 'execute'
17
+ | 'onInputAvailable'
18
+ | 'onInputStart'
19
+ | 'onInputDelta'
20
+ | 'needsApproval'
21
+ >
22
+ >;