ai 6.0.32 → 6.0.34
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 +16 -0
- package/dist/index.js +12 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +12 -2
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.js +1 -1
- package/dist/internal/index.mjs +1 -1
- package/docs/02-foundations/03-prompts.mdx +2 -2
- package/docs/03-ai-sdk-core/15-tools-and-tool-calling.mdx +1 -1
- package/docs/07-reference/01-ai-sdk-core/28-output.mdx +1 -1
- package/package.json +6 -4
- package/src/agent/agent.ts +116 -0
- package/src/agent/create-agent-ui-stream-response.test.ts +258 -0
- package/src/agent/create-agent-ui-stream-response.ts +50 -0
- package/src/agent/create-agent-ui-stream.ts +73 -0
- package/src/agent/index.ts +33 -0
- package/src/agent/infer-agent-tools.ts +7 -0
- package/src/agent/infer-agent-ui-message.test-d.ts +54 -0
- package/src/agent/infer-agent-ui-message.ts +11 -0
- package/src/agent/pipe-agent-ui-stream-to-response.ts +52 -0
- package/src/agent/tool-loop-agent-on-finish-callback.ts +31 -0
- package/src/agent/tool-loop-agent-on-step-finish-callback.ts +11 -0
- package/src/agent/tool-loop-agent-settings.ts +182 -0
- package/src/agent/tool-loop-agent.test-d.ts +114 -0
- package/src/agent/tool-loop-agent.test.ts +442 -0
- package/src/agent/tool-loop-agent.ts +114 -0
- package/src/embed/__snapshots__/embed-many.test.ts.snap +191 -0
- package/src/embed/__snapshots__/embed.test.ts.snap +81 -0
- package/src/embed/embed-many-result.ts +53 -0
- package/src/embed/embed-many.test.ts +653 -0
- package/src/embed/embed-many.ts +378 -0
- package/src/embed/embed-result.ts +50 -0
- package/src/embed/embed.test.ts +298 -0
- package/src/embed/embed.ts +211 -0
- package/src/embed/index.ts +4 -0
- package/src/error/index.ts +34 -0
- package/src/error/invalid-argument-error.ts +34 -0
- package/src/error/invalid-stream-part-error.ts +28 -0
- package/src/error/invalid-tool-approval-error.ts +26 -0
- package/src/error/invalid-tool-input-error.ts +33 -0
- package/src/error/no-image-generated-error.ts +39 -0
- package/src/error/no-object-generated-error.ts +70 -0
- package/src/error/no-output-generated-error.ts +26 -0
- package/src/error/no-speech-generated-error.ts +18 -0
- package/src/error/no-such-tool-error.ts +35 -0
- package/src/error/no-transcript-generated-error.ts +20 -0
- package/src/error/tool-call-not-found-for-approval-error.ts +32 -0
- package/src/error/tool-call-repair-error.ts +30 -0
- package/src/error/unsupported-model-version-error.ts +23 -0
- package/src/error/verify-no-object-generated-error.ts +27 -0
- package/src/generate-image/generate-image-result.ts +42 -0
- package/src/generate-image/generate-image.test.ts +1420 -0
- package/src/generate-image/generate-image.ts +360 -0
- package/src/generate-image/index.ts +18 -0
- package/src/generate-object/__snapshots__/generate-object.test.ts.snap +133 -0
- package/src/generate-object/__snapshots__/stream-object.test.ts.snap +297 -0
- package/src/generate-object/generate-object-result.ts +67 -0
- package/src/generate-object/generate-object.test-d.ts +49 -0
- package/src/generate-object/generate-object.test.ts +1191 -0
- package/src/generate-object/generate-object.ts +518 -0
- package/src/generate-object/index.ts +9 -0
- package/src/generate-object/inject-json-instruction.test.ts +181 -0
- package/src/generate-object/inject-json-instruction.ts +30 -0
- package/src/generate-object/output-strategy.ts +415 -0
- package/src/generate-object/parse-and-validate-object-result.ts +111 -0
- package/src/generate-object/repair-text.ts +12 -0
- package/src/generate-object/stream-object-result.ts +120 -0
- package/src/generate-object/stream-object.test-d.ts +74 -0
- package/src/generate-object/stream-object.test.ts +1950 -0
- package/src/generate-object/stream-object.ts +986 -0
- package/src/generate-object/validate-object-generation-input.ts +144 -0
- package/src/generate-speech/generate-speech-result.ts +30 -0
- package/src/generate-speech/generate-speech.test.ts +300 -0
- package/src/generate-speech/generate-speech.ts +190 -0
- package/src/generate-speech/generated-audio-file.ts +65 -0
- package/src/generate-speech/index.ts +3 -0
- package/src/generate-text/__snapshots__/generate-text.test.ts.snap +1872 -0
- package/src/generate-text/__snapshots__/stream-text.test.ts.snap +1255 -0
- package/src/generate-text/collect-tool-approvals.test.ts +553 -0
- package/src/generate-text/collect-tool-approvals.ts +116 -0
- package/src/generate-text/content-part.ts +25 -0
- package/src/generate-text/execute-tool-call.ts +129 -0
- package/src/generate-text/extract-reasoning-content.ts +17 -0
- package/src/generate-text/extract-text-content.ts +15 -0
- package/src/generate-text/generate-text-result.ts +168 -0
- package/src/generate-text/generate-text.test-d.ts +68 -0
- package/src/generate-text/generate-text.test.ts +7011 -0
- package/src/generate-text/generate-text.ts +1223 -0
- package/src/generate-text/generated-file.ts +70 -0
- package/src/generate-text/index.ts +57 -0
- package/src/generate-text/is-approval-needed.ts +29 -0
- package/src/generate-text/output-utils.ts +23 -0
- package/src/generate-text/output.test.ts +698 -0
- package/src/generate-text/output.ts +590 -0
- package/src/generate-text/parse-tool-call.test.ts +570 -0
- package/src/generate-text/parse-tool-call.ts +188 -0
- package/src/generate-text/prepare-step.ts +103 -0
- package/src/generate-text/prune-messages.test.ts +720 -0
- package/src/generate-text/prune-messages.ts +167 -0
- package/src/generate-text/reasoning-output.ts +20 -0
- package/src/generate-text/reasoning.ts +8 -0
- package/src/generate-text/response-message.ts +10 -0
- package/src/generate-text/run-tools-transformation.test.ts +1143 -0
- package/src/generate-text/run-tools-transformation.ts +420 -0
- package/src/generate-text/smooth-stream.test.ts +2101 -0
- package/src/generate-text/smooth-stream.ts +162 -0
- package/src/generate-text/step-result.ts +238 -0
- package/src/generate-text/stop-condition.ts +29 -0
- package/src/generate-text/stream-text-result.ts +463 -0
- package/src/generate-text/stream-text.test-d.ts +200 -0
- package/src/generate-text/stream-text.test.ts +19979 -0
- package/src/generate-text/stream-text.ts +2505 -0
- package/src/generate-text/to-response-messages.test.ts +922 -0
- package/src/generate-text/to-response-messages.ts +163 -0
- package/src/generate-text/tool-approval-request-output.ts +21 -0
- package/src/generate-text/tool-call-repair-function.ts +27 -0
- package/src/generate-text/tool-call.ts +47 -0
- package/src/generate-text/tool-error.ts +34 -0
- package/src/generate-text/tool-output-denied.ts +21 -0
- package/src/generate-text/tool-output.ts +7 -0
- package/src/generate-text/tool-result.ts +36 -0
- package/src/generate-text/tool-set.ts +14 -0
- package/src/global.ts +24 -0
- package/src/index.ts +50 -0
- package/src/logger/index.ts +6 -0
- package/src/logger/log-warnings.test.ts +351 -0
- package/src/logger/log-warnings.ts +119 -0
- package/src/middleware/__snapshots__/simulate-streaming-middleware.test.ts.snap +64 -0
- package/src/middleware/add-tool-input-examples-middleware.test.ts +476 -0
- package/src/middleware/add-tool-input-examples-middleware.ts +90 -0
- package/src/middleware/default-embedding-settings-middleware.test.ts +126 -0
- package/src/middleware/default-embedding-settings-middleware.ts +22 -0
- package/src/middleware/default-settings-middleware.test.ts +388 -0
- package/src/middleware/default-settings-middleware.ts +33 -0
- package/src/middleware/extract-json-middleware.test.ts +827 -0
- package/src/middleware/extract-json-middleware.ts +197 -0
- package/src/middleware/extract-reasoning-middleware.test.ts +1028 -0
- package/src/middleware/extract-reasoning-middleware.ts +238 -0
- package/src/middleware/index.ts +10 -0
- package/src/middleware/simulate-streaming-middleware.test.ts +911 -0
- package/src/middleware/simulate-streaming-middleware.ts +79 -0
- package/src/middleware/wrap-embedding-model.test.ts +358 -0
- package/src/middleware/wrap-embedding-model.ts +86 -0
- package/src/middleware/wrap-image-model.test.ts +423 -0
- package/src/middleware/wrap-image-model.ts +85 -0
- package/src/middleware/wrap-language-model.test.ts +518 -0
- package/src/middleware/wrap-language-model.ts +104 -0
- package/src/middleware/wrap-provider.test.ts +120 -0
- package/src/middleware/wrap-provider.ts +51 -0
- package/src/model/as-embedding-model-v3.test.ts +319 -0
- package/src/model/as-embedding-model-v3.ts +24 -0
- package/src/model/as-image-model-v3.test.ts +409 -0
- package/src/model/as-image-model-v3.ts +24 -0
- package/src/model/as-language-model-v3.test.ts +508 -0
- package/src/model/as-language-model-v3.ts +103 -0
- package/src/model/as-provider-v3.ts +36 -0
- package/src/model/as-speech-model-v3.test.ts +356 -0
- package/src/model/as-speech-model-v3.ts +24 -0
- package/src/model/as-transcription-model-v3.test.ts +529 -0
- package/src/model/as-transcription-model-v3.ts +24 -0
- package/src/model/resolve-model.test.ts +244 -0
- package/src/model/resolve-model.ts +126 -0
- package/src/prompt/call-settings.ts +148 -0
- package/src/prompt/content-part.ts +209 -0
- package/src/prompt/convert-to-language-model-prompt.test.ts +2018 -0
- package/src/prompt/convert-to-language-model-prompt.ts +442 -0
- package/src/prompt/create-tool-model-output.test.ts +508 -0
- package/src/prompt/create-tool-model-output.ts +34 -0
- package/src/prompt/data-content.test.ts +15 -0
- package/src/prompt/data-content.ts +134 -0
- package/src/prompt/index.ts +27 -0
- package/src/prompt/invalid-data-content-error.ts +29 -0
- package/src/prompt/invalid-message-role-error.ts +27 -0
- package/src/prompt/message-conversion-error.ts +28 -0
- package/src/prompt/message.ts +68 -0
- package/src/prompt/prepare-call-settings.test.ts +159 -0
- package/src/prompt/prepare-call-settings.ts +108 -0
- package/src/prompt/prepare-tools-and-tool-choice.test.ts +461 -0
- package/src/prompt/prepare-tools-and-tool-choice.ts +86 -0
- package/src/prompt/prompt.ts +43 -0
- package/src/prompt/split-data-url.ts +17 -0
- package/src/prompt/standardize-prompt.test.ts +82 -0
- package/src/prompt/standardize-prompt.ts +99 -0
- package/src/prompt/wrap-gateway-error.ts +29 -0
- package/src/registry/custom-provider.test.ts +211 -0
- package/src/registry/custom-provider.ts +155 -0
- package/src/registry/index.ts +7 -0
- package/src/registry/no-such-provider-error.ts +41 -0
- package/src/registry/provider-registry.test.ts +691 -0
- package/src/registry/provider-registry.ts +328 -0
- package/src/rerank/index.ts +2 -0
- package/src/rerank/rerank-result.ts +70 -0
- package/src/rerank/rerank.test.ts +516 -0
- package/src/rerank/rerank.ts +237 -0
- package/src/telemetry/assemble-operation-name.ts +21 -0
- package/src/telemetry/get-base-telemetry-attributes.ts +53 -0
- package/src/telemetry/get-tracer.ts +20 -0
- package/src/telemetry/noop-tracer.ts +69 -0
- package/src/telemetry/record-span.ts +63 -0
- package/src/telemetry/select-telemetry-attributes.ts +78 -0
- package/src/telemetry/select-temetry-attributes.test.ts +114 -0
- package/src/telemetry/stringify-for-telemetry.test.ts +114 -0
- package/src/telemetry/stringify-for-telemetry.ts +33 -0
- package/src/telemetry/telemetry-settings.ts +44 -0
- package/src/test/mock-embedding-model-v2.ts +35 -0
- package/src/test/mock-embedding-model-v3.ts +48 -0
- package/src/test/mock-image-model-v2.ts +28 -0
- package/src/test/mock-image-model-v3.ts +28 -0
- package/src/test/mock-language-model-v2.ts +72 -0
- package/src/test/mock-language-model-v3.ts +77 -0
- package/src/test/mock-provider-v2.ts +68 -0
- package/src/test/mock-provider-v3.ts +80 -0
- package/src/test/mock-reranking-model-v3.ts +25 -0
- package/src/test/mock-server-response.ts +69 -0
- package/src/test/mock-speech-model-v2.ts +24 -0
- package/src/test/mock-speech-model-v3.ts +24 -0
- package/src/test/mock-tracer.ts +156 -0
- package/src/test/mock-transcription-model-v2.ts +24 -0
- package/src/test/mock-transcription-model-v3.ts +24 -0
- package/src/test/mock-values.ts +4 -0
- package/src/test/not-implemented.ts +3 -0
- package/src/text-stream/create-text-stream-response.test.ts +38 -0
- package/src/text-stream/create-text-stream-response.ts +18 -0
- package/src/text-stream/index.ts +2 -0
- package/src/text-stream/pipe-text-stream-to-response.test.ts +38 -0
- package/src/text-stream/pipe-text-stream-to-response.ts +26 -0
- package/src/transcribe/index.ts +2 -0
- package/src/transcribe/transcribe-result.ts +60 -0
- package/src/transcribe/transcribe.test.ts +313 -0
- package/src/transcribe/transcribe.ts +173 -0
- package/src/types/embedding-model-middleware.ts +3 -0
- package/src/types/embedding-model.ts +18 -0
- package/src/types/image-model-middleware.ts +3 -0
- package/src/types/image-model-response-metadata.ts +16 -0
- package/src/types/image-model.ts +19 -0
- package/src/types/index.ts +29 -0
- package/src/types/json-value.ts +15 -0
- package/src/types/language-model-middleware.ts +3 -0
- package/src/types/language-model-request-metadata.ts +6 -0
- package/src/types/language-model-response-metadata.ts +21 -0
- package/src/types/language-model.ts +104 -0
- package/src/types/provider-metadata.ts +16 -0
- package/src/types/provider.ts +55 -0
- package/src/types/reranking-model.ts +6 -0
- package/src/types/speech-model-response-metadata.ts +21 -0
- package/src/types/speech-model.ts +6 -0
- package/src/types/transcription-model-response-metadata.ts +16 -0
- package/src/types/transcription-model.ts +9 -0
- package/src/types/usage.ts +200 -0
- package/src/types/warning.ts +7 -0
- package/src/ui/__snapshots__/append-response-messages.test.ts.snap +416 -0
- package/src/ui/__snapshots__/convert-to-model-messages.test.ts.snap +419 -0
- package/src/ui/__snapshots__/process-chat-text-response.test.ts.snap +142 -0
- package/src/ui/call-completion-api.ts +157 -0
- package/src/ui/chat-transport.ts +83 -0
- package/src/ui/chat.test-d.ts +233 -0
- package/src/ui/chat.test.ts +2695 -0
- package/src/ui/chat.ts +716 -0
- package/src/ui/convert-file-list-to-file-ui-parts.ts +36 -0
- package/src/ui/convert-to-model-messages.test.ts +2775 -0
- package/src/ui/convert-to-model-messages.ts +373 -0
- package/src/ui/default-chat-transport.ts +36 -0
- package/src/ui/direct-chat-transport.test.ts +446 -0
- package/src/ui/direct-chat-transport.ts +118 -0
- package/src/ui/http-chat-transport.test.ts +185 -0
- package/src/ui/http-chat-transport.ts +292 -0
- package/src/ui/index.ts +71 -0
- package/src/ui/last-assistant-message-is-complete-with-approval-responses.ts +44 -0
- package/src/ui/last-assistant-message-is-complete-with-tool-calls.test.ts +371 -0
- package/src/ui/last-assistant-message-is-complete-with-tool-calls.ts +39 -0
- package/src/ui/process-text-stream.test.ts +38 -0
- package/src/ui/process-text-stream.ts +16 -0
- package/src/ui/process-ui-message-stream.test.ts +8052 -0
- package/src/ui/process-ui-message-stream.ts +713 -0
- package/src/ui/text-stream-chat-transport.ts +23 -0
- package/src/ui/transform-text-to-ui-message-stream.test.ts +124 -0
- package/src/ui/transform-text-to-ui-message-stream.ts +27 -0
- package/src/ui/ui-messages.test.ts +48 -0
- package/src/ui/ui-messages.ts +534 -0
- package/src/ui/use-completion.ts +84 -0
- package/src/ui/validate-ui-messages.test.ts +1428 -0
- package/src/ui/validate-ui-messages.ts +476 -0
- package/src/ui-message-stream/create-ui-message-stream-response.test.ts +266 -0
- package/src/ui-message-stream/create-ui-message-stream-response.ts +32 -0
- package/src/ui-message-stream/create-ui-message-stream.test.ts +639 -0
- package/src/ui-message-stream/create-ui-message-stream.ts +124 -0
- package/src/ui-message-stream/get-response-ui-message-id.test.ts +55 -0
- package/src/ui-message-stream/get-response-ui-message-id.ts +24 -0
- package/src/ui-message-stream/handle-ui-message-stream-finish.test.ts +429 -0
- package/src/ui-message-stream/handle-ui-message-stream-finish.ts +135 -0
- package/src/ui-message-stream/index.ts +13 -0
- package/src/ui-message-stream/json-to-sse-transform-stream.ts +12 -0
- package/src/ui-message-stream/pipe-ui-message-stream-to-response.test.ts +90 -0
- package/src/ui-message-stream/pipe-ui-message-stream-to-response.ts +40 -0
- package/src/ui-message-stream/read-ui-message-stream.test.ts +122 -0
- package/src/ui-message-stream/read-ui-message-stream.ts +87 -0
- package/src/ui-message-stream/ui-message-chunks.test-d.ts +18 -0
- package/src/ui-message-stream/ui-message-chunks.ts +344 -0
- package/src/ui-message-stream/ui-message-stream-headers.ts +7 -0
- package/src/ui-message-stream/ui-message-stream-on-finish-callback.ts +32 -0
- package/src/ui-message-stream/ui-message-stream-response-init.ts +5 -0
- package/src/ui-message-stream/ui-message-stream-writer.ts +24 -0
- package/src/util/as-array.ts +3 -0
- package/src/util/async-iterable-stream.test.ts +241 -0
- package/src/util/async-iterable-stream.ts +94 -0
- package/src/util/consume-stream.ts +29 -0
- package/src/util/cosine-similarity.test.ts +57 -0
- package/src/util/cosine-similarity.ts +47 -0
- package/src/util/create-resolvable-promise.ts +30 -0
- package/src/util/create-stitchable-stream.test.ts +239 -0
- package/src/util/create-stitchable-stream.ts +112 -0
- package/src/util/data-url.ts +17 -0
- package/src/util/deep-partial.ts +84 -0
- package/src/util/detect-media-type.test.ts +670 -0
- package/src/util/detect-media-type.ts +184 -0
- package/src/util/download/download-function.ts +45 -0
- package/src/util/download/download.test.ts +69 -0
- package/src/util/download/download.ts +46 -0
- package/src/util/error-handler.ts +1 -0
- package/src/util/fix-json.test.ts +279 -0
- package/src/util/fix-json.ts +401 -0
- package/src/util/get-potential-start-index.test.ts +34 -0
- package/src/util/get-potential-start-index.ts +30 -0
- package/src/util/index.ts +11 -0
- package/src/util/is-deep-equal-data.test.ts +119 -0
- package/src/util/is-deep-equal-data.ts +48 -0
- package/src/util/is-non-empty-object.ts +5 -0
- package/src/util/job.ts +1 -0
- package/src/util/log-v2-compatibility-warning.ts +21 -0
- package/src/util/merge-abort-signals.test.ts +155 -0
- package/src/util/merge-abort-signals.ts +43 -0
- package/src/util/merge-objects.test.ts +118 -0
- package/src/util/merge-objects.ts +79 -0
- package/src/util/now.ts +4 -0
- package/src/util/parse-partial-json.test.ts +80 -0
- package/src/util/parse-partial-json.ts +30 -0
- package/src/util/prepare-headers.test.ts +51 -0
- package/src/util/prepare-headers.ts +14 -0
- package/src/util/prepare-retries.test.ts +10 -0
- package/src/util/prepare-retries.ts +47 -0
- package/src/util/retry-error.ts +41 -0
- package/src/util/retry-with-exponential-backoff.test.ts +446 -0
- package/src/util/retry-with-exponential-backoff.ts +154 -0
- package/src/util/serial-job-executor.test.ts +162 -0
- package/src/util/serial-job-executor.ts +36 -0
- package/src/util/simulate-readable-stream.test.ts +98 -0
- package/src/util/simulate-readable-stream.ts +39 -0
- package/src/util/split-array.test.ts +60 -0
- package/src/util/split-array.ts +20 -0
- package/src/util/value-of.ts +65 -0
- package/src/util/write-to-server-response.test.ts +266 -0
- package/src/util/write-to-server-response.ts +49 -0
- package/src/version.ts +5 -0
|
@@ -0,0 +1,508 @@
|
|
|
1
|
+
import { tool, Tool } from '@ai-sdk/provider-utils';
|
|
2
|
+
import { createToolModelOutput } from './create-tool-model-output';
|
|
3
|
+
import z from 'zod/v4';
|
|
4
|
+
import { describe, it, expect } from 'vitest';
|
|
5
|
+
|
|
6
|
+
describe('createToolModelOutput', () => {
|
|
7
|
+
describe('error cases', () => {
|
|
8
|
+
it('should return error type with string value when isError is true and output is string', async () => {
|
|
9
|
+
const result = await createToolModelOutput({
|
|
10
|
+
toolCallId: '123',
|
|
11
|
+
input: {},
|
|
12
|
+
output: 'Error message',
|
|
13
|
+
tool: undefined,
|
|
14
|
+
errorMode: 'text',
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
expect(result).toMatchInlineSnapshot(`
|
|
18
|
+
{
|
|
19
|
+
"type": "error-text",
|
|
20
|
+
"value": "Error message",
|
|
21
|
+
}
|
|
22
|
+
`);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('should return error type with JSON stringified value when isError is true and output is not string', async () => {
|
|
26
|
+
const errorOutput = { error: 'Something went wrong', code: 500 };
|
|
27
|
+
const result = await createToolModelOutput({
|
|
28
|
+
toolCallId: '123',
|
|
29
|
+
input: {},
|
|
30
|
+
output: errorOutput,
|
|
31
|
+
tool: undefined,
|
|
32
|
+
errorMode: 'text',
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
expect(result).toMatchInlineSnapshot(`
|
|
36
|
+
{
|
|
37
|
+
"type": "error-text",
|
|
38
|
+
"value": "{"error":"Something went wrong","code":500}",
|
|
39
|
+
}
|
|
40
|
+
`);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('should return error type with JSON stringified value for complex objects', async () => {
|
|
44
|
+
const complexError = {
|
|
45
|
+
message: 'Complex error',
|
|
46
|
+
details: {
|
|
47
|
+
timestamp: '2023-01-01T00:00:00Z',
|
|
48
|
+
stack: ['line1', 'line2'],
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
const result = await createToolModelOutput({
|
|
52
|
+
toolCallId: '123',
|
|
53
|
+
input: {},
|
|
54
|
+
output: complexError,
|
|
55
|
+
tool: undefined,
|
|
56
|
+
errorMode: 'text',
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
expect(result).toMatchInlineSnapshot(`
|
|
60
|
+
{
|
|
61
|
+
"type": "error-text",
|
|
62
|
+
"value": "{"message":"Complex error","details":{"timestamp":"2023-01-01T00:00:00Z","stack":["line1","line2"]}}",
|
|
63
|
+
}
|
|
64
|
+
`);
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
describe('tool with toModelOutput', () => {
|
|
69
|
+
it('should use tool.toModelOutput when available', async () => {
|
|
70
|
+
const mockTool = tool({
|
|
71
|
+
inputSchema: z.object({}),
|
|
72
|
+
toModelOutput: ({ output }) => ({
|
|
73
|
+
type: 'text',
|
|
74
|
+
value: `Custom output: ${output}`,
|
|
75
|
+
}),
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
const result = await createToolModelOutput({
|
|
79
|
+
toolCallId: '123',
|
|
80
|
+
input: {},
|
|
81
|
+
output: 'test output',
|
|
82
|
+
tool: mockTool,
|
|
83
|
+
errorMode: 'none',
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
expect(result).toMatchInlineSnapshot(`
|
|
87
|
+
{
|
|
88
|
+
"type": "text",
|
|
89
|
+
"value": "Custom output: test output",
|
|
90
|
+
}
|
|
91
|
+
`);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it('should use tool.toModelOutput with complex output', async () => {
|
|
95
|
+
const mockTool = tool({
|
|
96
|
+
inputSchema: z.object({}),
|
|
97
|
+
toModelOutput: ({ output }) => ({
|
|
98
|
+
type: 'json',
|
|
99
|
+
value: { processed: output, timestamp: '2023-01-01' },
|
|
100
|
+
}),
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
const result = await createToolModelOutput({
|
|
104
|
+
toolCallId: '123',
|
|
105
|
+
input: {},
|
|
106
|
+
output: { data: [1, 2, 3], status: 'success' },
|
|
107
|
+
tool: mockTool,
|
|
108
|
+
errorMode: 'none',
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
expect(result).toMatchInlineSnapshot(`
|
|
112
|
+
{
|
|
113
|
+
"type": "json",
|
|
114
|
+
"value": {
|
|
115
|
+
"processed": {
|
|
116
|
+
"data": [
|
|
117
|
+
1,
|
|
118
|
+
2,
|
|
119
|
+
3,
|
|
120
|
+
],
|
|
121
|
+
"status": "success",
|
|
122
|
+
},
|
|
123
|
+
"timestamp": "2023-01-01",
|
|
124
|
+
},
|
|
125
|
+
}
|
|
126
|
+
`);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it('should use tool.toModelOutput returning content type', async () => {
|
|
130
|
+
const mockTool: Tool = {
|
|
131
|
+
toModelOutput: () => ({
|
|
132
|
+
type: 'content',
|
|
133
|
+
value: [
|
|
134
|
+
{ type: 'text', text: 'Here is the result:' },
|
|
135
|
+
{ type: 'text', text: 'Additional information' },
|
|
136
|
+
],
|
|
137
|
+
}),
|
|
138
|
+
inputSchema: z.object({}),
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
const result = await createToolModelOutput({
|
|
142
|
+
toolCallId: '123',
|
|
143
|
+
input: {},
|
|
144
|
+
output: 'any output',
|
|
145
|
+
tool: mockTool,
|
|
146
|
+
errorMode: 'none',
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
expect(result).toMatchInlineSnapshot(`
|
|
150
|
+
{
|
|
151
|
+
"type": "content",
|
|
152
|
+
"value": [
|
|
153
|
+
{
|
|
154
|
+
"text": "Here is the result:",
|
|
155
|
+
"type": "text",
|
|
156
|
+
},
|
|
157
|
+
{
|
|
158
|
+
"text": "Additional information",
|
|
159
|
+
"type": "text",
|
|
160
|
+
},
|
|
161
|
+
],
|
|
162
|
+
}
|
|
163
|
+
`);
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
describe('string output without toModelOutput', () => {
|
|
168
|
+
it('should return text type for string output', async () => {
|
|
169
|
+
const result = await createToolModelOutput({
|
|
170
|
+
toolCallId: '123',
|
|
171
|
+
input: {},
|
|
172
|
+
output: 'Simple string output',
|
|
173
|
+
tool: undefined,
|
|
174
|
+
errorMode: 'none',
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
expect(result).toMatchInlineSnapshot(`
|
|
178
|
+
{
|
|
179
|
+
"type": "text",
|
|
180
|
+
"value": "Simple string output",
|
|
181
|
+
}
|
|
182
|
+
`);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it('should return text type for string output even with tool that has no toModelOutput', async () => {
|
|
186
|
+
const result = await createToolModelOutput({
|
|
187
|
+
toolCallId: '123',
|
|
188
|
+
input: {},
|
|
189
|
+
output: 'String output',
|
|
190
|
+
tool: {
|
|
191
|
+
description: 'A tool without toModelOutput',
|
|
192
|
+
inputSchema: z.object({}),
|
|
193
|
+
},
|
|
194
|
+
errorMode: 'none',
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
expect(result).toMatchInlineSnapshot(`
|
|
198
|
+
{
|
|
199
|
+
"type": "text",
|
|
200
|
+
"value": "String output",
|
|
201
|
+
}
|
|
202
|
+
`);
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
it('should return text type for empty string', async () => {
|
|
206
|
+
const result = await createToolModelOutput({
|
|
207
|
+
toolCallId: '123',
|
|
208
|
+
input: {},
|
|
209
|
+
output: '',
|
|
210
|
+
tool: undefined,
|
|
211
|
+
errorMode: 'none',
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
expect(result).toMatchInlineSnapshot(`
|
|
215
|
+
{
|
|
216
|
+
"type": "text",
|
|
217
|
+
"value": "",
|
|
218
|
+
}
|
|
219
|
+
`);
|
|
220
|
+
});
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
describe('non-string output without toModelOutput', () => {
|
|
224
|
+
it('should return json type for object output', async () => {
|
|
225
|
+
const result = await createToolModelOutput({
|
|
226
|
+
toolCallId: '123',
|
|
227
|
+
input: {},
|
|
228
|
+
output: { result: 'success', data: [1, 2, 3] },
|
|
229
|
+
tool: undefined,
|
|
230
|
+
errorMode: 'none',
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
expect(result).toMatchInlineSnapshot(`
|
|
234
|
+
{
|
|
235
|
+
"type": "json",
|
|
236
|
+
"value": {
|
|
237
|
+
"data": [
|
|
238
|
+
1,
|
|
239
|
+
2,
|
|
240
|
+
3,
|
|
241
|
+
],
|
|
242
|
+
"result": "success",
|
|
243
|
+
},
|
|
244
|
+
}
|
|
245
|
+
`);
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
it('should return json type for array output', async () => {
|
|
249
|
+
const result = await createToolModelOutput({
|
|
250
|
+
toolCallId: '123',
|
|
251
|
+
input: {},
|
|
252
|
+
output: [1, 2, 3, 'test'],
|
|
253
|
+
tool: undefined,
|
|
254
|
+
errorMode: 'none',
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
expect(result).toMatchInlineSnapshot(`
|
|
258
|
+
{
|
|
259
|
+
"type": "json",
|
|
260
|
+
"value": [
|
|
261
|
+
1,
|
|
262
|
+
2,
|
|
263
|
+
3,
|
|
264
|
+
"test",
|
|
265
|
+
],
|
|
266
|
+
}
|
|
267
|
+
`);
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
it('should return json type for number output', async () => {
|
|
271
|
+
const result = await createToolModelOutput({
|
|
272
|
+
toolCallId: '123',
|
|
273
|
+
input: {},
|
|
274
|
+
output: 42,
|
|
275
|
+
tool: undefined,
|
|
276
|
+
errorMode: 'none',
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
expect(result).toMatchInlineSnapshot(`
|
|
280
|
+
{
|
|
281
|
+
"type": "json",
|
|
282
|
+
"value": 42,
|
|
283
|
+
}
|
|
284
|
+
`);
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
it('should return json type for boolean output', async () => {
|
|
288
|
+
const result = await createToolModelOutput({
|
|
289
|
+
toolCallId: '123',
|
|
290
|
+
input: {},
|
|
291
|
+
output: true,
|
|
292
|
+
tool: undefined,
|
|
293
|
+
errorMode: 'none',
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
expect(result).toMatchInlineSnapshot(`
|
|
297
|
+
{
|
|
298
|
+
"type": "json",
|
|
299
|
+
"value": true,
|
|
300
|
+
}
|
|
301
|
+
`);
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
it('should return json type for null output', async () => {
|
|
305
|
+
const result = await createToolModelOutput({
|
|
306
|
+
toolCallId: '123',
|
|
307
|
+
input: {},
|
|
308
|
+
output: null,
|
|
309
|
+
tool: undefined,
|
|
310
|
+
errorMode: 'none',
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
expect(result).toMatchInlineSnapshot(`
|
|
314
|
+
{
|
|
315
|
+
"type": "json",
|
|
316
|
+
"value": null,
|
|
317
|
+
}
|
|
318
|
+
`);
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
it('should return json type for complex nested object', async () => {
|
|
322
|
+
const result = await createToolModelOutput({
|
|
323
|
+
toolCallId: '123',
|
|
324
|
+
input: {},
|
|
325
|
+
output: {
|
|
326
|
+
user: {
|
|
327
|
+
id: 123,
|
|
328
|
+
name: 'John Doe',
|
|
329
|
+
preferences: {
|
|
330
|
+
theme: 'dark',
|
|
331
|
+
notifications: true,
|
|
332
|
+
},
|
|
333
|
+
},
|
|
334
|
+
metadata: {
|
|
335
|
+
timestamp: '2023-01-01T00:00:00Z',
|
|
336
|
+
version: '1.0.0',
|
|
337
|
+
},
|
|
338
|
+
items: [
|
|
339
|
+
{ id: 1, name: 'Item 1' },
|
|
340
|
+
{ id: 2, name: 'Item 2' },
|
|
341
|
+
],
|
|
342
|
+
},
|
|
343
|
+
tool: undefined,
|
|
344
|
+
errorMode: 'none',
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
expect(result).toMatchInlineSnapshot(`
|
|
348
|
+
{
|
|
349
|
+
"type": "json",
|
|
350
|
+
"value": {
|
|
351
|
+
"items": [
|
|
352
|
+
{
|
|
353
|
+
"id": 1,
|
|
354
|
+
"name": "Item 1",
|
|
355
|
+
},
|
|
356
|
+
{
|
|
357
|
+
"id": 2,
|
|
358
|
+
"name": "Item 2",
|
|
359
|
+
},
|
|
360
|
+
],
|
|
361
|
+
"metadata": {
|
|
362
|
+
"timestamp": "2023-01-01T00:00:00Z",
|
|
363
|
+
"version": "1.0.0",
|
|
364
|
+
},
|
|
365
|
+
"user": {
|
|
366
|
+
"id": 123,
|
|
367
|
+
"name": "John Doe",
|
|
368
|
+
"preferences": {
|
|
369
|
+
"notifications": true,
|
|
370
|
+
"theme": "dark",
|
|
371
|
+
},
|
|
372
|
+
},
|
|
373
|
+
},
|
|
374
|
+
}
|
|
375
|
+
`);
|
|
376
|
+
});
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
describe('edge cases', () => {
|
|
380
|
+
it('should prioritize isError over tool.toModelOutput', async () => {
|
|
381
|
+
const mockTool: Tool = {
|
|
382
|
+
inputSchema: z.object({}),
|
|
383
|
+
toModelOutput: () => ({
|
|
384
|
+
type: 'text',
|
|
385
|
+
value: 'This should not be called',
|
|
386
|
+
}),
|
|
387
|
+
};
|
|
388
|
+
|
|
389
|
+
const result = await createToolModelOutput({
|
|
390
|
+
toolCallId: '123',
|
|
391
|
+
input: {},
|
|
392
|
+
output: 'Error occurred',
|
|
393
|
+
tool: mockTool,
|
|
394
|
+
errorMode: 'text',
|
|
395
|
+
});
|
|
396
|
+
|
|
397
|
+
expect(result).toMatchInlineSnapshot(`
|
|
398
|
+
{
|
|
399
|
+
"type": "error-text",
|
|
400
|
+
"value": "Error occurred",
|
|
401
|
+
}
|
|
402
|
+
`);
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
it('should handle undefined output in error text case', async () => {
|
|
406
|
+
const result = await createToolModelOutput({
|
|
407
|
+
toolCallId: '123',
|
|
408
|
+
input: {},
|
|
409
|
+
output: undefined,
|
|
410
|
+
tool: undefined,
|
|
411
|
+
errorMode: 'text',
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
expect(result).toMatchInlineSnapshot(`
|
|
415
|
+
{
|
|
416
|
+
"type": "error-text",
|
|
417
|
+
"value": "unknown error",
|
|
418
|
+
}
|
|
419
|
+
`);
|
|
420
|
+
});
|
|
421
|
+
|
|
422
|
+
it('should use null for undefined output in error json case', async () => {
|
|
423
|
+
const result = await createToolModelOutput({
|
|
424
|
+
toolCallId: '123',
|
|
425
|
+
input: {},
|
|
426
|
+
output: undefined,
|
|
427
|
+
tool: undefined,
|
|
428
|
+
errorMode: 'json',
|
|
429
|
+
});
|
|
430
|
+
|
|
431
|
+
expect(result).toMatchInlineSnapshot(`
|
|
432
|
+
{
|
|
433
|
+
"type": "error-json",
|
|
434
|
+
"value": null,
|
|
435
|
+
}
|
|
436
|
+
`);
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
it('should use null for undefined output in non-error case', async () => {
|
|
440
|
+
const result = await createToolModelOutput({
|
|
441
|
+
toolCallId: '123',
|
|
442
|
+
input: {},
|
|
443
|
+
output: undefined,
|
|
444
|
+
tool: undefined,
|
|
445
|
+
errorMode: 'none',
|
|
446
|
+
});
|
|
447
|
+
|
|
448
|
+
expect(result).toMatchInlineSnapshot(`
|
|
449
|
+
{
|
|
450
|
+
"type": "json",
|
|
451
|
+
"value": null,
|
|
452
|
+
}
|
|
453
|
+
`);
|
|
454
|
+
});
|
|
455
|
+
});
|
|
456
|
+
|
|
457
|
+
describe('arguments', () => {
|
|
458
|
+
it('should pass toolCallId to tool.toModelOutput', async () => {
|
|
459
|
+
const mockTool: Tool = {
|
|
460
|
+
inputSchema: z.object({}),
|
|
461
|
+
toModelOutput: ({ toolCallId }) => ({
|
|
462
|
+
type: 'text',
|
|
463
|
+
value: `Tool call ID: ${toolCallId}`,
|
|
464
|
+
}),
|
|
465
|
+
};
|
|
466
|
+
|
|
467
|
+
const result = await createToolModelOutput({
|
|
468
|
+
toolCallId: '2344',
|
|
469
|
+
input: {},
|
|
470
|
+
output: 'test',
|
|
471
|
+
tool: mockTool,
|
|
472
|
+
errorMode: 'none',
|
|
473
|
+
});
|
|
474
|
+
|
|
475
|
+
expect(result).toMatchInlineSnapshot(`
|
|
476
|
+
{
|
|
477
|
+
"type": "text",
|
|
478
|
+
"value": "Tool call ID: 2344",
|
|
479
|
+
}
|
|
480
|
+
`);
|
|
481
|
+
});
|
|
482
|
+
|
|
483
|
+
it('should pass input to tool.toModelOutput', async () => {
|
|
484
|
+
const mockTool: Tool = {
|
|
485
|
+
inputSchema: z.object({ number: z.number() }),
|
|
486
|
+
toModelOutput: ({ input }) => ({
|
|
487
|
+
type: 'text',
|
|
488
|
+
value: `Input: ${input.number}`,
|
|
489
|
+
}),
|
|
490
|
+
};
|
|
491
|
+
|
|
492
|
+
const result = await createToolModelOutput({
|
|
493
|
+
toolCallId: '2344',
|
|
494
|
+
input: { number: 8877 },
|
|
495
|
+
output: 'test',
|
|
496
|
+
tool: mockTool,
|
|
497
|
+
errorMode: 'none',
|
|
498
|
+
});
|
|
499
|
+
|
|
500
|
+
expect(result).toMatchInlineSnapshot(`
|
|
501
|
+
{
|
|
502
|
+
"type": "text",
|
|
503
|
+
"value": "Input: 8877",
|
|
504
|
+
}
|
|
505
|
+
`);
|
|
506
|
+
});
|
|
507
|
+
});
|
|
508
|
+
});
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { getErrorMessage, JSONValue } from '@ai-sdk/provider';
|
|
2
|
+
import { Tool, ToolResultOutput } from '@ai-sdk/provider-utils';
|
|
3
|
+
|
|
4
|
+
export async function createToolModelOutput({
|
|
5
|
+
toolCallId,
|
|
6
|
+
input,
|
|
7
|
+
output,
|
|
8
|
+
tool,
|
|
9
|
+
errorMode,
|
|
10
|
+
}: {
|
|
11
|
+
toolCallId: string;
|
|
12
|
+
input: unknown;
|
|
13
|
+
output: unknown;
|
|
14
|
+
tool: Tool | undefined;
|
|
15
|
+
errorMode: 'none' | 'text' | 'json';
|
|
16
|
+
}): Promise<ToolResultOutput> {
|
|
17
|
+
if (errorMode === 'text') {
|
|
18
|
+
return { type: 'error-text', value: getErrorMessage(output) };
|
|
19
|
+
} else if (errorMode === 'json') {
|
|
20
|
+
return { type: 'error-json', value: toJSONValue(output) };
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (tool?.toModelOutput) {
|
|
24
|
+
return await tool.toModelOutput({ toolCallId, input, output });
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return typeof output === 'string'
|
|
28
|
+
? { type: 'text', value: output }
|
|
29
|
+
: { type: 'json', value: toJSONValue(output) };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function toJSONValue(value: unknown): JSONValue {
|
|
33
|
+
return value === undefined ? null : (value as JSONValue);
|
|
34
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { dataContentSchema } from './data-content';
|
|
2
|
+
import { describe, it, expect } from 'vitest';
|
|
3
|
+
|
|
4
|
+
describe('dataContentSchema', () => {
|
|
5
|
+
it('should validate a Buffer', () => {
|
|
6
|
+
const buffer = Buffer.from('Hello, world!');
|
|
7
|
+
const result = dataContentSchema.parse(buffer);
|
|
8
|
+
expect(result).toEqual(buffer);
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it('should reject a non-matching object', () => {
|
|
12
|
+
const nonMatchingObject = { foo: 'bar' };
|
|
13
|
+
expect(() => dataContentSchema.parse(nonMatchingObject)).toThrow();
|
|
14
|
+
});
|
|
15
|
+
});
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { AISDKError, LanguageModelV3DataContent } from '@ai-sdk/provider';
|
|
2
|
+
import {
|
|
3
|
+
convertBase64ToUint8Array,
|
|
4
|
+
convertUint8ArrayToBase64,
|
|
5
|
+
DataContent,
|
|
6
|
+
} from '@ai-sdk/provider-utils';
|
|
7
|
+
import { z } from 'zod/v4';
|
|
8
|
+
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 convertToLanguageModelV3DataContent(
|
|
27
|
+
content: DataContent | URL,
|
|
28
|
+
): {
|
|
29
|
+
data: LanguageModelV3DataContent;
|
|
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 (error) {
|
|
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
|
+
|
|
71
|
+
/**
|
|
72
|
+
Converts data content to a base64-encoded string.
|
|
73
|
+
|
|
74
|
+
@param content - Data content to convert.
|
|
75
|
+
@returns Base64-encoded string.
|
|
76
|
+
*/
|
|
77
|
+
export function convertDataContentToBase64String(content: DataContent): string {
|
|
78
|
+
if (typeof content === 'string') {
|
|
79
|
+
return content;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (content instanceof ArrayBuffer) {
|
|
83
|
+
return convertUint8ArrayToBase64(new Uint8Array(content));
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return convertUint8ArrayToBase64(content);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
Converts data content to a Uint8Array.
|
|
91
|
+
|
|
92
|
+
@param content - Data content to convert.
|
|
93
|
+
@returns Uint8Array.
|
|
94
|
+
*/
|
|
95
|
+
export function convertDataContentToUint8Array(
|
|
96
|
+
content: DataContent,
|
|
97
|
+
): Uint8Array {
|
|
98
|
+
if (content instanceof Uint8Array) {
|
|
99
|
+
return content;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (typeof content === 'string') {
|
|
103
|
+
try {
|
|
104
|
+
return convertBase64ToUint8Array(content);
|
|
105
|
+
} catch (error) {
|
|
106
|
+
throw new InvalidDataContentError({
|
|
107
|
+
message:
|
|
108
|
+
'Invalid data content. Content string is not a base64-encoded media.',
|
|
109
|
+
content,
|
|
110
|
+
cause: error,
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (content instanceof ArrayBuffer) {
|
|
116
|
+
return new Uint8Array(content);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
throw new InvalidDataContentError({ content });
|
|
120
|
+
}
|
|
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 (error) {
|
|
132
|
+
throw new Error('Error decoding Uint8Array to text');
|
|
133
|
+
}
|
|
134
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export type { CallSettings, TimeoutConfiguration } from './call-settings';
|
|
2
|
+
export {
|
|
3
|
+
assistantModelMessageSchema,
|
|
4
|
+
modelMessageSchema,
|
|
5
|
+
systemModelMessageSchema,
|
|
6
|
+
toolModelMessageSchema,
|
|
7
|
+
userModelMessageSchema,
|
|
8
|
+
} from './message';
|
|
9
|
+
export type { Prompt } from './prompt';
|
|
10
|
+
|
|
11
|
+
// re-export types from provider-utils
|
|
12
|
+
export type {
|
|
13
|
+
AssistantContent,
|
|
14
|
+
AssistantModelMessage,
|
|
15
|
+
DataContent,
|
|
16
|
+
FilePart,
|
|
17
|
+
ImagePart,
|
|
18
|
+
ModelMessage,
|
|
19
|
+
SystemModelMessage,
|
|
20
|
+
TextPart,
|
|
21
|
+
ToolCallPart,
|
|
22
|
+
ToolContent,
|
|
23
|
+
ToolModelMessage,
|
|
24
|
+
ToolResultPart,
|
|
25
|
+
UserContent,
|
|
26
|
+
UserModelMessage,
|
|
27
|
+
} from '@ai-sdk/provider-utils';
|