ai 7.0.0-beta.113 → 7.0.0-beta.115
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 +32 -0
- package/dist/index.d.ts +1506 -1468
- package/dist/index.js +889 -979
- package/dist/index.js.map +1 -1
- package/dist/internal/index.d.ts +273 -273
- package/dist/internal/index.js +367 -405
- package/dist/internal/index.js.map +1 -1
- package/dist/test/index.d.ts +18 -18
- package/dist/test/index.js +22 -22
- package/dist/test/index.js.map +1 -1
- package/docs/02-foundations/03-prompts.mdx +13 -11
- package/docs/03-ai-sdk-core/15-tools-and-tool-calling.mdx +2 -1
- package/docs/03-ai-sdk-core/39-file-uploads.mdx +1 -1
- package/docs/03-ai-sdk-core/45-provider-management.mdx +1 -1
- package/docs/03-ai-sdk-core/60-telemetry.mdx +87 -13
- package/docs/07-reference/01-ai-sdk-core/12-generate-speech.mdx +2 -2
- package/docs/07-reference/01-ai-sdk-core/20-tool.mdx +7 -0
- package/docs/07-reference/01-ai-sdk-core/22-dynamic-tool.mdx +7 -0
- package/docs/07-reference/01-ai-sdk-core/30-model-message.mdx +9 -1
- package/docs/07-reference/01-ai-sdk-core/40-provider-registry.mdx +104 -0
- package/docs/07-reference/01-ai-sdk-core/42-custom-provider.mdx +56 -10
- package/docs/07-reference/02-ai-sdk-ui/31-convert-to-model-messages.mdx +1 -1
- package/docs/08-migration-guides/23-migration-guide-7-0.mdx +42 -0
- package/docs/09-troubleshooting/70-high-memory-usage-with-images.mdx +2 -2
- package/package.json +5 -5
- package/src/agent/agent.ts +13 -9
- package/src/agent/create-agent-ui-stream-response.ts +8 -8
- package/src/agent/create-agent-ui-stream.ts +9 -9
- package/src/agent/infer-agent-tools.ts +1 -1
- package/src/agent/infer-agent-ui-message.ts +2 -2
- package/src/agent/pipe-agent-ui-stream-to-response.ts +9 -9
- package/src/agent/tool-loop-agent-settings.ts +17 -19
- package/src/agent/tool-loop-agent.ts +16 -8
- package/src/embed/embed-many-result.ts +3 -4
- package/src/embed/embed-many.ts +5 -5
- package/src/embed/embed-result.ts +3 -4
- package/src/embed/embed.ts +4 -4
- package/src/error/invalid-stream-part-error.ts +1 -1
- package/src/error/no-image-generated-error.ts +1 -1
- package/src/error/no-object-generated-error.ts +3 -3
- package/src/error/no-speech-generated-error.ts +1 -1
- package/src/error/no-transcript-generated-error.ts +1 -1
- package/src/error/no-video-generated-error.ts +1 -1
- package/src/error/tool-call-repair-error.ts +2 -2
- package/src/error/verify-no-object-generated-error.ts +1 -1
- package/src/generate-image/generate-image-result.ts +5 -5
- package/src/generate-image/generate-image.ts +12 -15
- package/src/generate-image/index.ts +3 -2
- package/src/generate-object/generate-object-result.ts +2 -2
- package/src/generate-object/generate-object.ts +14 -14
- package/src/generate-object/inject-json-instruction.ts +1 -1
- package/src/generate-object/output-strategy.ts +10 -10
- package/src/generate-object/parse-and-validate-object-result.ts +1 -1
- package/src/generate-object/repair-text.ts +1 -1
- package/src/generate-object/stream-object-result.ts +4 -4
- package/src/generate-object/stream-object.ts +22 -19
- package/src/generate-object/validate-object-generation-input.ts +1 -1
- package/src/generate-speech/generate-speech-result.ts +4 -4
- package/src/generate-speech/generate-speech.ts +12 -13
- package/src/generate-speech/generated-audio-file.ts +1 -2
- package/src/generate-text/collect-tool-approvals.ts +4 -4
- package/src/generate-text/content-part.ts +9 -9
- package/src/generate-text/create-execute-tools-transformation.ts +9 -7
- package/src/generate-text/execute-tool-call.ts +11 -13
- package/src/generate-text/extract-reasoning-content.ts +1 -1
- package/src/generate-text/extract-text-content.ts +4 -1
- package/src/generate-text/generate-text-events.ts +2 -2
- package/src/generate-text/generate-text-result.ts +18 -14
- package/src/generate-text/generate-text.ts +42 -38
- package/src/generate-text/invoke-tool-callbacks-from-stream.ts +2 -3
- package/src/generate-text/output-utils.ts +1 -1
- package/src/generate-text/output.ts +8 -8
- package/src/generate-text/parse-tool-call.ts +36 -9
- package/src/generate-text/prepare-step.ts +3 -5
- package/src/generate-text/prune-messages.ts +1 -1
- package/src/generate-text/reasoning-output.ts +26 -9
- package/src/generate-text/reasoning.ts +1 -1
- package/src/generate-text/resolve-tool-approval.ts +3 -3
- package/src/generate-text/response-message.ts +1 -1
- package/src/generate-text/restricted-telemetry-dispatcher.ts +1 -1
- package/src/generate-text/smooth-stream.ts +6 -5
- package/src/generate-text/step-result.ts +16 -11
- package/src/generate-text/stop-condition.ts +1 -1
- package/src/generate-text/stream-language-model-call.ts +26 -21
- package/src/generate-text/stream-text-result.ts +29 -26
- package/src/generate-text/stream-text.ts +48 -46
- package/src/generate-text/to-response-messages.ts +2 -2
- package/src/generate-text/tool-approval-configuration.ts +2 -2
- package/src/generate-text/tool-approval-request-output.ts +1 -1
- package/src/generate-text/tool-approval-response-output.ts +1 -1
- package/src/generate-text/tool-call-repair-function.ts +4 -4
- package/src/generate-text/tool-call.ts +3 -4
- package/src/generate-text/tool-error.ts +3 -4
- package/src/generate-text/tool-execution-events.ts +2 -2
- package/src/generate-text/tool-output-denied.ts +1 -1
- package/src/generate-text/tool-output.ts +2 -2
- package/src/generate-text/tool-result.ts +7 -4
- package/src/generate-text/tools-context-parameter.ts +1 -1
- package/src/generate-text/validate-tool-context.ts +1 -2
- package/src/generate-video/generate-video-result.ts +4 -4
- package/src/generate-video/generate-video.ts +6 -10
- package/src/global.ts +2 -2
- package/src/index.ts +3 -3
- package/src/logger/log-warnings.ts +1 -1
- package/src/middleware/add-tool-input-examples-middleware.ts +2 -2
- package/src/middleware/default-embedding-settings-middleware.ts +2 -2
- package/src/middleware/default-settings-middleware.ts +2 -2
- package/src/middleware/extract-json-middleware.ts +1 -1
- package/src/middleware/extract-reasoning-middleware.ts +1 -1
- package/src/middleware/simulate-streaming-middleware.ts +1 -1
- package/src/middleware/wrap-embedding-model.ts +2 -2
- package/src/middleware/wrap-image-model.ts +2 -2
- package/src/middleware/wrap-language-model.ts +2 -2
- package/src/middleware/wrap-provider.ts +2 -2
- package/src/model/as-embedding-model-v3.ts +1 -1
- package/src/model/as-embedding-model-v4.ts +1 -1
- package/src/model/as-image-model-v3.ts +1 -1
- package/src/model/as-image-model-v4.ts +5 -1
- package/src/model/as-language-model-v3.ts +1 -1
- package/src/model/as-language-model-v4.ts +1 -1
- package/src/model/as-provider-v3.ts +1 -1
- package/src/model/as-provider-v4.ts +1 -1
- package/src/model/as-reranking-model-v4.ts +1 -1
- package/src/model/as-speech-model-v3.ts +1 -1
- package/src/model/as-speech-model-v4.ts +5 -1
- package/src/model/as-transcription-model-v3.ts +4 -1
- package/src/model/as-transcription-model-v4.ts +1 -1
- package/src/model/as-video-model-v4.ts +1 -1
- package/src/model/resolve-model.ts +8 -8
- package/src/prompt/content-part.ts +45 -15
- package/src/prompt/convert-to-language-model-prompt.ts +108 -122
- package/src/prompt/create-tool-model-output.ts +2 -2
- package/src/prompt/data-content.ts +1 -78
- package/src/prompt/file-part-data.ts +125 -0
- package/src/prompt/index.ts +3 -2
- package/src/prompt/language-model-call-options.ts +1 -1
- package/src/prompt/message-conversion-error.ts +1 -1
- package/src/prompt/message.ts +1 -1
- package/src/prompt/prepare-language-model-call-options.ts +1 -1
- package/src/prompt/prepare-tool-choice.ts +2 -2
- package/src/prompt/prepare-tools.ts +2 -2
- package/src/prompt/prompt.ts +1 -1
- package/src/prompt/standardize-prompt.ts +3 -3
- package/src/registry/custom-provider.ts +127 -92
- package/src/registry/index.ts +1 -1
- package/src/registry/provider-registry.ts +91 -87
- package/src/rerank/rerank-result.ts +1 -1
- package/src/rerank/rerank.ts +8 -5
- package/src/telemetry/create-telemetry-dispatcher.ts +11 -1
- package/src/telemetry/diagnostic-channel-publisher.ts +50 -0
- package/src/telemetry/diagnostic-channel.ts +24 -0
- package/src/telemetry/index.ts +5 -0
- package/src/telemetry/telemetry.ts +1 -1
- package/src/test/mock-embedding-model-v2.ts +1 -1
- package/src/test/mock-embedding-model-v3.ts +1 -1
- package/src/test/mock-embedding-model-v4.ts +1 -1
- package/src/test/mock-image-model-v2.ts +1 -1
- package/src/test/mock-image-model-v3.ts +1 -1
- package/src/test/mock-image-model-v4.ts +1 -1
- package/src/test/mock-language-model-v2.ts +1 -1
- package/src/test/mock-language-model-v3.ts +1 -1
- package/src/test/mock-language-model-v4.ts +1 -1
- package/src/test/mock-provider-v2.ts +6 -7
- package/src/test/mock-provider-v3.ts +7 -8
- package/src/test/mock-provider-v4.ts +7 -8
- package/src/test/mock-reranking-model-v3.ts +1 -1
- package/src/test/mock-reranking-model-v4.ts +1 -1
- package/src/test/mock-server-response.ts +1 -1
- package/src/test/mock-speech-model-v2.ts +1 -1
- package/src/test/mock-speech-model-v3.ts +1 -1
- package/src/test/mock-speech-model-v4.ts +1 -1
- package/src/test/mock-transcription-model-v2.ts +1 -1
- package/src/test/mock-transcription-model-v3.ts +1 -1
- package/src/test/mock-transcription-model-v4.ts +1 -1
- package/src/test/mock-video-model-v3.ts +1 -1
- package/src/test/mock-video-model-v4.ts +1 -1
- package/src/text-stream/pipe-text-stream-to-response.ts +1 -1
- package/src/transcribe/transcribe-result.ts +3 -3
- package/src/transcribe/transcribe.ts +12 -12
- package/src/types/embedding-model-middleware.ts +1 -1
- package/src/types/embedding-model.ts +1 -1
- package/src/types/image-model-middleware.ts +1 -1
- package/src/types/image-model.ts +1 -1
- package/src/types/json-value.ts +1 -1
- package/src/types/language-model-middleware.ts +1 -1
- package/src/types/language-model.ts +2 -2
- package/src/types/provider-metadata.ts +1 -1
- package/src/types/provider-reference.ts +1 -1
- package/src/types/provider.ts +4 -4
- package/src/types/reranking-model.ts +1 -1
- package/src/types/speech-model.ts +5 -1
- package/src/types/transcription-model.ts +1 -1
- package/src/types/usage.ts +1 -1
- package/src/types/video-model.ts +1 -1
- package/src/types/warning.ts +1 -1
- package/src/ui/call-completion-api.ts +2 -2
- package/src/ui/chat-transport.ts +3 -3
- package/src/ui/chat.ts +10 -11
- package/src/ui/convert-file-list-to-file-ui-parts.ts +1 -1
- package/src/ui/convert-to-model-messages.ts +34 -23
- package/src/ui/default-chat-transport.ts +4 -4
- package/src/ui/direct-chat-transport.ts +6 -6
- package/src/ui/http-chat-transport.ts +5 -5
- package/src/ui/last-assistant-message-is-complete-with-approval-responses.ts +0 -1
- package/src/ui/last-assistant-message-is-complete-with-tool-calls.ts +0 -1
- package/src/ui/process-ui-message-stream.ts +20 -21
- package/src/ui/text-stream-chat-transport.ts +3 -3
- package/src/ui/transform-text-to-ui-message-stream.ts +1 -1
- package/src/ui/ui-messages.ts +13 -6
- package/src/ui/use-completion.ts +1 -1
- package/src/ui/validate-ui-messages.ts +3 -3
- package/src/ui-message-stream/create-ui-message-stream-response.ts +2 -2
- package/src/ui-message-stream/create-ui-message-stream.ts +6 -6
- package/src/ui-message-stream/get-response-ui-message-id.ts +2 -2
- package/src/ui-message-stream/handle-ui-message-stream-finish.ts +6 -6
- package/src/ui-message-stream/pipe-ui-message-stream-to-response.ts +3 -3
- package/src/ui-message-stream/read-ui-message-stream.ts +4 -4
- package/src/ui-message-stream/ui-message-chunks.ts +4 -4
- package/src/ui-message-stream/ui-message-stream-on-finish-callback.ts +2 -2
- package/src/ui-message-stream/ui-message-stream-on-step-finish-callback.ts +1 -1
- package/src/ui-message-stream/ui-message-stream-writer.ts +3 -3
- package/src/upload-file/upload-file-result.ts +3 -3
- package/src/upload-file/upload-file.ts +34 -56
- package/src/upload-skill/upload-skill-result.ts +3 -3
- package/src/upload-skill/upload-skill.ts +27 -8
- package/src/util/create-resolvable-promise.ts +1 -1
- package/src/util/deep-partial.ts +1 -1
- package/src/util/download/download.ts +0 -2
- package/src/util/extract-literal-union.ts +18 -0
- package/src/util/is-node-runtime.ts +3 -0
- package/src/util/notify.ts +1 -1
- package/src/util/parse-partial-json.ts +1 -1
- package/src/util/prepare-retries.ts +1 -2
- package/src/util/serial-job-executor.ts +1 -1
- package/src/util/write-to-server-response.ts +1 -1
- package/src/util/detect-media-type.ts +0 -235
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ProviderV2, ProviderV3 } from '@ai-sdk/provider';
|
|
1
|
+
import type { ProviderV2, ProviderV3 } from '@ai-sdk/provider';
|
|
2
2
|
import { asEmbeddingModelV3 } from './as-embedding-model-v3';
|
|
3
3
|
import { asImageModelV3 } from './as-image-model-v3';
|
|
4
4
|
import { asLanguageModelV3 } from './as-language-model-v3';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ProviderV2, ProviderV3, ProviderV4 } from '@ai-sdk/provider';
|
|
1
|
+
import type { ProviderV2, ProviderV3, ProviderV4 } from '@ai-sdk/provider';
|
|
2
2
|
import { asProviderV3 } from './as-provider-v3';
|
|
3
3
|
import { asEmbeddingModelV4 } from './as-embedding-model-v4';
|
|
4
4
|
import { asImageModelV4 } from './as-image-model-v4';
|
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type {
|
|
2
|
+
SpeechModelV2,
|
|
3
|
+
SpeechModelV3,
|
|
4
|
+
SpeechModelV4,
|
|
5
|
+
} from '@ai-sdk/provider';
|
|
2
6
|
import { asSpeechModelV3 } from './as-speech-model-v3';
|
|
3
7
|
|
|
4
8
|
export function asSpeechModelV4(
|
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type {
|
|
2
|
+
TranscriptionModelV2,
|
|
3
|
+
TranscriptionModelV3,
|
|
4
|
+
} from '@ai-sdk/provider';
|
|
2
5
|
import { logV2CompatibilityWarning } from '../util/log-v2-compatibility-warning';
|
|
3
6
|
|
|
4
7
|
export function asTranscriptionModelV3(
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { gateway } from '@ai-sdk/gateway';
|
|
2
|
-
import {
|
|
2
|
+
import type {
|
|
3
3
|
EmbeddingModelV4,
|
|
4
4
|
Experimental_VideoModelV4,
|
|
5
5
|
ImageModelV4,
|
|
@@ -10,10 +10,10 @@ import {
|
|
|
10
10
|
TranscriptionModelV4,
|
|
11
11
|
} from '@ai-sdk/provider';
|
|
12
12
|
import { UnsupportedModelVersionError } from '../error';
|
|
13
|
-
import { EmbeddingModel } from '../types/embedding-model';
|
|
14
|
-
import { LanguageModel } from '../types/language-model';
|
|
15
|
-
import { SpeechModel } from '../types/speech-model';
|
|
16
|
-
import { TranscriptionModel } from '../types/transcription-model';
|
|
13
|
+
import type { EmbeddingModel } from '../types/embedding-model';
|
|
14
|
+
import type { LanguageModel } from '../types/language-model';
|
|
15
|
+
import type { SpeechModel } from '../types/speech-model';
|
|
16
|
+
import type { TranscriptionModel } from '../types/transcription-model';
|
|
17
17
|
import { asEmbeddingModelV4 } from './as-embedding-model-v4';
|
|
18
18
|
import { asImageModelV4 } from './as-image-model-v4';
|
|
19
19
|
import { asLanguageModelV4 } from './as-language-model-v4';
|
|
@@ -22,9 +22,9 @@ import { asSpeechModelV4 } from './as-speech-model-v4';
|
|
|
22
22
|
import { asTranscriptionModelV4 } from './as-transcription-model-v4';
|
|
23
23
|
import { asVideoModelV4 } from './as-video-model-v4';
|
|
24
24
|
import { asProviderV4 } from './as-provider-v4';
|
|
25
|
-
import { ImageModel } from '../types/image-model';
|
|
26
|
-
import { RerankingModel } from '../types/reranking-model';
|
|
27
|
-
import { VideoModel } from '../types/video-model';
|
|
25
|
+
import type { ImageModel } from '../types/image-model';
|
|
26
|
+
import type { RerankingModel } from '../types/reranking-model';
|
|
27
|
+
import type { VideoModel } from '../types/video-model';
|
|
28
28
|
|
|
29
29
|
export function resolveLanguageModel(model: LanguageModel): LanguageModelV4 {
|
|
30
30
|
if (typeof model === 'string') {
|
|
@@ -1,20 +1,28 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
2
|
+
isBuffer,
|
|
3
|
+
type CustomPart,
|
|
4
|
+
type DataContent,
|
|
5
|
+
type FilePart,
|
|
6
|
+
type ImagePart,
|
|
7
|
+
type ProviderOptions,
|
|
8
|
+
type ReasoningFilePart,
|
|
9
|
+
type ReasoningPart,
|
|
10
|
+
type TextPart,
|
|
11
|
+
type ToolApprovalRequest,
|
|
12
|
+
type ToolApprovalResponse,
|
|
13
|
+
type ToolResultOutput,
|
|
14
|
+
type ToolResultPart,
|
|
13
15
|
} from '@ai-sdk/provider-utils';
|
|
14
16
|
import { z } from 'zod/v4';
|
|
15
17
|
import { jsonValueSchema } from '../types/json-value';
|
|
16
18
|
import { providerMetadataSchema } from '../types/provider-metadata';
|
|
17
|
-
|
|
19
|
+
|
|
20
|
+
const fileInlineDataSchema: z.ZodType<DataContent> = z.union([
|
|
21
|
+
z.string(),
|
|
22
|
+
z.instanceof(Uint8Array),
|
|
23
|
+
z.instanceof(ArrayBuffer),
|
|
24
|
+
z.custom<Buffer>(isBuffer, { message: 'Must be a Buffer' }),
|
|
25
|
+
]);
|
|
18
26
|
|
|
19
27
|
const providerReferenceSchema = z.record(z.string(), z.string());
|
|
20
28
|
|
|
@@ -29,11 +37,13 @@ export const textPartSchema: z.ZodType<TextPart> = z.object({
|
|
|
29
37
|
|
|
30
38
|
/**
|
|
31
39
|
* @internal
|
|
40
|
+
* @deprecated Use `filePartSchema` with `mediaType: 'image'` instead:
|
|
41
|
+
* `{ type: 'file', mediaType: 'image', data: { type: 'data', data } }`.
|
|
32
42
|
*/
|
|
33
43
|
export const imagePartSchema: z.ZodType<ImagePart> = z.object({
|
|
34
44
|
type: z.literal('image'),
|
|
35
45
|
image: z.union([
|
|
36
|
-
|
|
46
|
+
fileInlineDataSchema,
|
|
37
47
|
z.instanceof(URL),
|
|
38
48
|
providerReferenceSchema,
|
|
39
49
|
]),
|
|
@@ -41,13 +51,29 @@ export const imagePartSchema: z.ZodType<ImagePart> = z.object({
|
|
|
41
51
|
providerOptions: providerMetadataSchema.optional(),
|
|
42
52
|
});
|
|
43
53
|
|
|
54
|
+
const taggedFileDataSchema = z.discriminatedUnion('type', [
|
|
55
|
+
z.object({ type: z.literal('data'), data: fileInlineDataSchema }),
|
|
56
|
+
z.object({ type: z.literal('url'), url: z.instanceof(URL) }),
|
|
57
|
+
z.object({
|
|
58
|
+
type: z.literal('reference'),
|
|
59
|
+
reference: providerReferenceSchema,
|
|
60
|
+
}),
|
|
61
|
+
z.object({ type: z.literal('text'), text: z.string() }),
|
|
62
|
+
]);
|
|
63
|
+
|
|
64
|
+
const taggedReasoningFileDataSchema = z.discriminatedUnion('type', [
|
|
65
|
+
z.object({ type: z.literal('data'), data: fileInlineDataSchema }),
|
|
66
|
+
z.object({ type: z.literal('url'), url: z.instanceof(URL) }),
|
|
67
|
+
]);
|
|
68
|
+
|
|
44
69
|
/**
|
|
45
70
|
* @internal
|
|
46
71
|
*/
|
|
47
72
|
export const filePartSchema: z.ZodType<FilePart> = z.object({
|
|
48
73
|
type: z.literal('file'),
|
|
49
74
|
data: z.union([
|
|
50
|
-
|
|
75
|
+
taggedFileDataSchema,
|
|
76
|
+
fileInlineDataSchema,
|
|
51
77
|
z.instanceof(URL),
|
|
52
78
|
providerReferenceSchema,
|
|
53
79
|
]),
|
|
@@ -79,7 +105,11 @@ export const customPartSchema: z.ZodType<CustomPart> = z.object({
|
|
|
79
105
|
*/
|
|
80
106
|
export const reasoningFilePartSchema: z.ZodType<ReasoningFilePart> = z.object({
|
|
81
107
|
type: z.literal('reasoning-file'),
|
|
82
|
-
data: z.union([
|
|
108
|
+
data: z.union([
|
|
109
|
+
taggedReasoningFileDataSchema,
|
|
110
|
+
fileInlineDataSchema,
|
|
111
|
+
z.instanceof(URL),
|
|
112
|
+
]),
|
|
83
113
|
mediaType: z.string(),
|
|
84
114
|
providerOptions: providerMetadataSchema.optional(),
|
|
85
115
|
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type {
|
|
2
2
|
LanguageModelV4FilePart,
|
|
3
3
|
LanguageModelV4Message,
|
|
4
4
|
LanguageModelV4Prompt,
|
|
@@ -7,33 +7,29 @@ import {
|
|
|
7
7
|
} from '@ai-sdk/provider';
|
|
8
8
|
import {
|
|
9
9
|
asArray,
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
FilePart,
|
|
13
|
-
ImagePart,
|
|
14
|
-
isProviderReference,
|
|
10
|
+
detectMediaType,
|
|
11
|
+
isFullMediaType,
|
|
15
12
|
isUrlSupported,
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
13
|
+
type CustomPart,
|
|
14
|
+
type FilePart,
|
|
15
|
+
type ImagePart,
|
|
16
|
+
type ModelMessage,
|
|
17
|
+
type ReasoningFilePart,
|
|
18
|
+
type ReasoningPart,
|
|
19
|
+
type TextPart,
|
|
20
|
+
type ToolCallPart,
|
|
21
|
+
type ToolResultOutput,
|
|
22
|
+
type ToolResultPart,
|
|
23
23
|
} from '@ai-sdk/provider-utils';
|
|
24
|
-
import {
|
|
25
|
-
detectMediaType,
|
|
26
|
-
imageMediaTypeSignatures,
|
|
27
|
-
} from '../util/detect-media-type';
|
|
28
24
|
import {
|
|
29
25
|
createDefaultDownloadFunction,
|
|
30
|
-
DownloadFunction,
|
|
26
|
+
type DownloadFunction,
|
|
31
27
|
} from '../util/download/download-function';
|
|
32
|
-
import {
|
|
28
|
+
import { convertToLanguageModelV4FilePart } from './file-part-data';
|
|
33
29
|
import { logWarnings } from '../logger/log-warnings';
|
|
34
30
|
import type { Warning } from '../types/warning';
|
|
35
31
|
import { InvalidMessageRoleError } from './invalid-message-role-error';
|
|
36
|
-
import { StandardizedPrompt } from './standardize-prompt';
|
|
32
|
+
import type { StandardizedPrompt } from './standardize-prompt';
|
|
37
33
|
import { MissingToolResultsError } from '../error/missing-tool-result-error';
|
|
38
34
|
|
|
39
35
|
export async function convertToLanguageModelPrompt({
|
|
@@ -217,6 +213,16 @@ export function convertToLanguageModelMessage({
|
|
|
217
213
|
const converted = {
|
|
218
214
|
role: 'user' as const,
|
|
219
215
|
content: message.content
|
|
216
|
+
.map(part => {
|
|
217
|
+
if (part.type === 'image') {
|
|
218
|
+
warnings.push({
|
|
219
|
+
type: 'deprecated',
|
|
220
|
+
setting: '"image" content part',
|
|
221
|
+
message: `The "image" content part type is deprecated. Use a "file" part with mediaType: 'image' (or a more specific image/* subtype) instead.`,
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
return convertImagePartToFilePart(part);
|
|
225
|
+
})
|
|
220
226
|
.map(part => convertPartToLanguageModelPart(part, downloadedAssets))
|
|
221
227
|
// remove empty text parts:
|
|
222
228
|
.filter(part => part.type !== 'text' || part.text !== ''),
|
|
@@ -271,19 +277,7 @@ export function convertToLanguageModelMessage({
|
|
|
271
277
|
};
|
|
272
278
|
}
|
|
273
279
|
case 'file': {
|
|
274
|
-
|
|
275
|
-
!(part.data instanceof ArrayBuffer) &&
|
|
276
|
-
isProviderReference(part.data)
|
|
277
|
-
) {
|
|
278
|
-
return {
|
|
279
|
-
type: 'file' as const,
|
|
280
|
-
data: part.data,
|
|
281
|
-
filename: part.filename,
|
|
282
|
-
mediaType: part.mediaType,
|
|
283
|
-
providerOptions,
|
|
284
|
-
};
|
|
285
|
-
}
|
|
286
|
-
const { data, mediaType } = convertToLanguageModelV4DataContent(
|
|
280
|
+
const { data, mediaType } = convertToLanguageModelV4FilePart(
|
|
287
281
|
part.data,
|
|
288
282
|
);
|
|
289
283
|
return {
|
|
@@ -302,9 +296,14 @@ export function convertToLanguageModelMessage({
|
|
|
302
296
|
};
|
|
303
297
|
}
|
|
304
298
|
case 'reasoning-file': {
|
|
305
|
-
const { data, mediaType } =
|
|
299
|
+
const { data, mediaType } = convertToLanguageModelV4FilePart(
|
|
306
300
|
part.data,
|
|
307
301
|
);
|
|
302
|
+
if (data.type !== 'data' && data.type !== 'url') {
|
|
303
|
+
throw new Error(
|
|
304
|
+
`Unsupported reasoning-file data type: ${data.type}`,
|
|
305
|
+
);
|
|
306
|
+
}
|
|
308
307
|
return {
|
|
309
308
|
type: 'reasoning-file' as const,
|
|
310
309
|
data,
|
|
@@ -401,8 +400,29 @@ export function convertToLanguageModelMessage({
|
|
|
401
400
|
}
|
|
402
401
|
}
|
|
403
402
|
|
|
403
|
+
/*
|
|
404
|
+
* Rewrites a legacy `ImagePart` into an equivalent `FilePart`. The default
|
|
405
|
+
* `mediaType` for a bare `ImagePart` (no `mediaType`) is `"image"` (top-level
|
|
406
|
+
* segment); an explicit `mediaType` is carried through verbatim. After this
|
|
407
|
+
* pre-pass, only `TextPart` and `FilePart` ever reach the provider-facing
|
|
408
|
+
* conversion logic.
|
|
409
|
+
*/
|
|
410
|
+
function convertImagePartToFilePart(
|
|
411
|
+
part: TextPart | ImagePart | FilePart,
|
|
412
|
+
): TextPart | FilePart {
|
|
413
|
+
if (part.type !== 'image') {
|
|
414
|
+
return part;
|
|
415
|
+
}
|
|
416
|
+
return {
|
|
417
|
+
type: 'file',
|
|
418
|
+
data: part.image,
|
|
419
|
+
mediaType: part.mediaType ?? 'image',
|
|
420
|
+
providerOptions: part.providerOptions,
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
|
|
404
424
|
/**
|
|
405
|
-
* Downloads
|
|
425
|
+
* Downloads files from URLs in the user messages.
|
|
406
426
|
*/
|
|
407
427
|
async function downloadAssets(
|
|
408
428
|
messages: ModelMessage[],
|
|
@@ -411,6 +431,15 @@ async function downloadAssets(
|
|
|
411
431
|
): Promise<
|
|
412
432
|
Record<string, { mediaType: string | undefined; data: Uint8Array }>
|
|
413
433
|
> {
|
|
434
|
+
type ConvertedFile = {
|
|
435
|
+
mediaType: string | undefined;
|
|
436
|
+
data: LanguageModelV4FilePart['data'];
|
|
437
|
+
};
|
|
438
|
+
type UrlTaggedFile = {
|
|
439
|
+
mediaType: string | undefined;
|
|
440
|
+
data: { type: 'url'; url: URL };
|
|
441
|
+
};
|
|
442
|
+
|
|
414
443
|
const plannedDownloads = messages
|
|
415
444
|
.filter(message => message.role === 'user')
|
|
416
445
|
.map(message => message.content)
|
|
@@ -418,34 +447,20 @@ async function downloadAssets(
|
|
|
418
447
|
Array.isArray(content),
|
|
419
448
|
)
|
|
420
449
|
.flat()
|
|
421
|
-
.
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
const mediaType =
|
|
427
|
-
part.mediaType ?? (part.type === 'image' ? 'image/*' : undefined);
|
|
428
|
-
|
|
429
|
-
let data = part.type === 'image' ? part.image : part.data;
|
|
430
|
-
if (typeof data === 'string') {
|
|
431
|
-
try {
|
|
432
|
-
data = new URL(data);
|
|
433
|
-
} catch {}
|
|
434
|
-
}
|
|
435
|
-
|
|
450
|
+
.map(part => convertImagePartToFilePart(part))
|
|
451
|
+
.filter((part): part is FilePart => part.type === 'file')
|
|
452
|
+
.map((part): ConvertedFile => {
|
|
453
|
+
const mediaType = part.mediaType;
|
|
454
|
+
const { data } = convertToLanguageModelV4FilePart(part.data);
|
|
436
455
|
return { mediaType, data };
|
|
437
456
|
})
|
|
438
|
-
|
|
439
|
-
.filter(
|
|
440
|
-
(part): part is { mediaType: string | undefined; data: URL } =>
|
|
441
|
-
part.data instanceof URL,
|
|
442
|
-
)
|
|
457
|
+
.filter((part): part is UrlTaggedFile => part.data.type === 'url')
|
|
443
458
|
.map(part => ({
|
|
444
|
-
url: part.data,
|
|
459
|
+
url: part.data.url,
|
|
445
460
|
isUrlSupportedByModel:
|
|
446
461
|
part.mediaType != null &&
|
|
447
462
|
isUrlSupported({
|
|
448
|
-
url: part.data.toString(),
|
|
463
|
+
url: part.data.url.toString(),
|
|
449
464
|
mediaType: part.mediaType,
|
|
450
465
|
supportedUrls,
|
|
451
466
|
}),
|
|
@@ -469,7 +484,7 @@ async function downloadAssets(
|
|
|
469
484
|
}
|
|
470
485
|
|
|
471
486
|
/**
|
|
472
|
-
* Convert part of a message to a LanguageModelV4Part.
|
|
487
|
+
* Convert part of a user message to a LanguageModelV4Part.
|
|
473
488
|
*
|
|
474
489
|
* @param part - The part to convert.
|
|
475
490
|
* @param downloadedAssets - A map of URLs to their downloaded data. Only
|
|
@@ -477,7 +492,7 @@ async function downloadAssets(
|
|
|
477
492
|
* @returns The converted part.
|
|
478
493
|
*/
|
|
479
494
|
function convertPartToLanguageModelPart(
|
|
480
|
-
part: TextPart |
|
|
495
|
+
part: TextPart | FilePart,
|
|
481
496
|
downloadedAssets: Record<
|
|
482
497
|
string,
|
|
483
498
|
{ mediaType: string | undefined; data: Uint8Array }
|
|
@@ -491,78 +506,49 @@ function convertPartToLanguageModelPart(
|
|
|
491
506
|
};
|
|
492
507
|
}
|
|
493
508
|
|
|
494
|
-
const
|
|
495
|
-
|
|
509
|
+
const { data: normalizedData, mediaType: dataUrlMediaType } =
|
|
510
|
+
convertToLanguageModelV4FilePart(part.data);
|
|
496
511
|
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
!(rawData instanceof Uint8Array) &&
|
|
500
|
-
!(rawData instanceof ArrayBuffer) &&
|
|
501
|
-
!(rawData instanceof URL)
|
|
502
|
-
) {
|
|
503
|
-
return {
|
|
504
|
-
type: 'file',
|
|
505
|
-
mediaType: part.mediaType ?? (type === 'image' ? 'image/*' : ''),
|
|
506
|
-
filename: type === 'file' ? part.filename : undefined,
|
|
507
|
-
data: rawData,
|
|
508
|
-
providerOptions: part.providerOptions,
|
|
509
|
-
};
|
|
510
|
-
}
|
|
511
|
-
|
|
512
|
-
const originalData: DataContent | URL = rawData;
|
|
512
|
+
let mediaType: string | undefined = dataUrlMediaType ?? part.mediaType;
|
|
513
|
+
let data: LanguageModelV4FilePart['data'] = normalizedData;
|
|
513
514
|
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
let mediaType: string | undefined = convertedMediaType ?? part.mediaType;
|
|
518
|
-
let data: Uint8Array | string | URL = convertedData; // binary | base64 | url
|
|
519
|
-
|
|
520
|
-
// If the content is a URL, we check if it was downloaded:
|
|
521
|
-
if (data instanceof URL) {
|
|
522
|
-
const downloadedFile = downloadedAssets[data.toString()];
|
|
515
|
+
if (data.type === 'url') {
|
|
516
|
+
const downloadedFile = downloadedAssets[data.url.toString()];
|
|
523
517
|
if (downloadedFile) {
|
|
524
|
-
data = downloadedFile.data;
|
|
525
|
-
|
|
518
|
+
data = { type: 'data', data: downloadedFile.data };
|
|
519
|
+
if (
|
|
520
|
+
downloadedFile.mediaType != null &&
|
|
521
|
+
(mediaType == null || !isFullMediaType(mediaType))
|
|
522
|
+
) {
|
|
523
|
+
mediaType = downloadedFile.mediaType;
|
|
524
|
+
}
|
|
526
525
|
}
|
|
527
526
|
}
|
|
528
527
|
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
mediaType;
|
|
540
|
-
}
|
|
541
|
-
|
|
542
|
-
return {
|
|
543
|
-
type: 'file',
|
|
544
|
-
mediaType: mediaType ?? 'image/*', // any image
|
|
545
|
-
filename: undefined,
|
|
546
|
-
data,
|
|
547
|
-
providerOptions: part.providerOptions,
|
|
548
|
-
};
|
|
528
|
+
if (
|
|
529
|
+
data.type === 'data' &&
|
|
530
|
+
(data.data instanceof Uint8Array || typeof data.data === 'string')
|
|
531
|
+
) {
|
|
532
|
+
const imageMediaType = detectMediaType({
|
|
533
|
+
data: data.data,
|
|
534
|
+
topLevelType: 'image',
|
|
535
|
+
});
|
|
536
|
+
if (imageMediaType != null) {
|
|
537
|
+
mediaType = imageMediaType;
|
|
549
538
|
}
|
|
539
|
+
}
|
|
550
540
|
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
if (mediaType == null) {
|
|
554
|
-
throw new Error(`Media type is missing for file part`);
|
|
555
|
-
}
|
|
556
|
-
|
|
557
|
-
return {
|
|
558
|
-
type: 'file',
|
|
559
|
-
mediaType,
|
|
560
|
-
filename: part.filename,
|
|
561
|
-
data,
|
|
562
|
-
providerOptions: part.providerOptions,
|
|
563
|
-
};
|
|
564
|
-
}
|
|
541
|
+
if (mediaType == null) {
|
|
542
|
+
throw new Error(`Media type is missing for file part`);
|
|
565
543
|
}
|
|
544
|
+
|
|
545
|
+
return {
|
|
546
|
+
type: 'file',
|
|
547
|
+
mediaType,
|
|
548
|
+
filename: part.filename,
|
|
549
|
+
data,
|
|
550
|
+
providerOptions: part.providerOptions,
|
|
551
|
+
};
|
|
566
552
|
}
|
|
567
553
|
|
|
568
554
|
function mapToolResultOutput({
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { getErrorMessage, JSONValue } from '@ai-sdk/provider';
|
|
2
|
-
import { Tool, ToolResultOutput } from '@ai-sdk/provider-utils';
|
|
1
|
+
import { getErrorMessage, type JSONValue } from '@ai-sdk/provider';
|
|
2
|
+
import type { Tool, ToolResultOutput } from '@ai-sdk/provider-utils';
|
|
3
3
|
|
|
4
4
|
export async function createToolModelOutput({
|
|
5
5
|
toolCallId,
|
|
@@ -1,72 +1,9 @@
|
|
|
1
|
-
import { AISDKError, LanguageModelV4DataContent } from '@ai-sdk/provider';
|
|
2
1
|
import {
|
|
3
2
|
convertBase64ToUint8Array,
|
|
4
3
|
convertUint8ArrayToBase64,
|
|
5
|
-
DataContent,
|
|
4
|
+
type DataContent,
|
|
6
5
|
} from '@ai-sdk/provider-utils';
|
|
7
|
-
import { z } from 'zod/v4';
|
|
8
6
|
import { InvalidDataContentError } from './invalid-data-content-error';
|
|
9
|
-
import { splitDataUrl } from './split-data-url';
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* @internal
|
|
13
|
-
*/
|
|
14
|
-
export const dataContentSchema: z.ZodType<DataContent> = z.union([
|
|
15
|
-
z.string(),
|
|
16
|
-
z.instanceof(Uint8Array),
|
|
17
|
-
z.instanceof(ArrayBuffer),
|
|
18
|
-
z.custom<Buffer>(
|
|
19
|
-
// Buffer might not be available in some environments such as CloudFlare:
|
|
20
|
-
(value: unknown): value is Buffer =>
|
|
21
|
-
globalThis.Buffer?.isBuffer(value) ?? false,
|
|
22
|
-
{ message: 'Must be a Buffer' },
|
|
23
|
-
),
|
|
24
|
-
]);
|
|
25
|
-
|
|
26
|
-
export function convertToLanguageModelV4DataContent(
|
|
27
|
-
content: DataContent | URL,
|
|
28
|
-
): {
|
|
29
|
-
data: LanguageModelV4DataContent;
|
|
30
|
-
mediaType: string | undefined;
|
|
31
|
-
} {
|
|
32
|
-
// Buffer & Uint8Array:
|
|
33
|
-
if (content instanceof Uint8Array) {
|
|
34
|
-
return { data: content, mediaType: undefined };
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// ArrayBuffer needs conversion to Uint8Array (lightweight):
|
|
38
|
-
if (content instanceof ArrayBuffer) {
|
|
39
|
-
return { data: new Uint8Array(content), mediaType: undefined };
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// Attempt to create a URL from the data. If it fails, we can assume the data
|
|
43
|
-
// is not a URL and likely some other sort of data.
|
|
44
|
-
if (typeof content === 'string') {
|
|
45
|
-
try {
|
|
46
|
-
content = new URL(content);
|
|
47
|
-
} catch {
|
|
48
|
-
// ignored
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
// Extract data from data URL:
|
|
53
|
-
if (content instanceof URL && content.protocol === 'data:') {
|
|
54
|
-
const { mediaType: dataUrlMediaType, base64Content } = splitDataUrl(
|
|
55
|
-
content.toString(),
|
|
56
|
-
);
|
|
57
|
-
|
|
58
|
-
if (dataUrlMediaType == null || base64Content == null) {
|
|
59
|
-
throw new AISDKError({
|
|
60
|
-
name: 'InvalidDataContentError',
|
|
61
|
-
message: `Invalid data URL format in content ${content.toString()}`,
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
return { data: base64Content, mediaType: dataUrlMediaType };
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
return { data: content, mediaType: undefined };
|
|
69
|
-
}
|
|
70
7
|
|
|
71
8
|
/**
|
|
72
9
|
* Converts data content to a base64-encoded string.
|
|
@@ -118,17 +55,3 @@ export function convertDataContentToUint8Array(
|
|
|
118
55
|
|
|
119
56
|
throw new InvalidDataContentError({ content });
|
|
120
57
|
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* Converts a Uint8Array to a string of text.
|
|
124
|
-
*
|
|
125
|
-
* @param uint8Array - The Uint8Array to convert.
|
|
126
|
-
* @returns The converted string.
|
|
127
|
-
*/
|
|
128
|
-
export function convertUint8ArrayToText(uint8Array: Uint8Array): string {
|
|
129
|
-
try {
|
|
130
|
-
return new TextDecoder().decode(uint8Array);
|
|
131
|
-
} catch {
|
|
132
|
-
throw new Error('Error decoding Uint8Array to text');
|
|
133
|
-
}
|
|
134
|
-
}
|