ai 7.0.37 → 7.0.39
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 +22 -0
- package/dist/index.d.ts +283 -41
- package/dist/index.js +688 -425
- package/dist/index.js.map +1 -1
- package/dist/internal/index.js +1 -1
- package/dist/internal/index.js.map +1 -1
- package/dist/test/index.d.ts +14 -2
- package/dist/test/index.js +15 -0
- package/dist/test/index.js.map +1 -1
- package/docs/03-agents/07-workflow-agent.mdx +3 -2
- package/docs/03-ai-sdk-core/36-transcription.mdx +40 -35
- package/docs/03-ai-sdk-core/36-translation.mdx +209 -0
- package/docs/03-ai-sdk-core/index.mdx +5 -0
- package/docs/03-ai-sdk-harnesses/06-workflow-utilities.mdx +225 -122
- package/docs/07-reference/01-ai-sdk-core/02-stream-text.mdx +1 -1
- package/docs/07-reference/01-ai-sdk-core/11-stream-translate.mdx +176 -0
- package/docs/07-reference/01-ai-sdk-core/index.mdx +5 -0
- package/docs/07-reference/04-ai-sdk-workflow/01-workflow-agent.mdx +11 -4
- package/docs/07-reference/05-ai-sdk-errors/ai-no-translation-generated-error.mdx +26 -0
- package/docs/07-reference/05-ai-sdk-errors/index.mdx +1 -0
- package/package.json +4 -4
- package/src/error/index.ts +1 -0
- package/src/error/no-translation-generated-error.ts +28 -0
- package/src/generate-object/generate-object.ts +12 -2
- package/src/generate-object/stream-object.ts +14 -1
- package/src/generate-text/generate-text.ts +4 -1
- package/src/generate-text/stream-language-model-call.ts +14 -12
- package/src/generate-text/stream-text-result.ts +3 -2
- package/src/index.ts +1 -0
- package/src/model/resolve-model.ts +35 -0
- package/src/test/mock-speech-translation-model-v4.ts +24 -0
- package/src/transcribe/stream-transcribe.ts +2 -11
- package/src/translate/index.ts +5 -0
- package/src/translate/stream-translate-result.ts +146 -0
- package/src/translate/stream-translate.ts +374 -0
- package/src/types/speech-translation-model-response-metadata.ts +22 -0
- package/src/types/speech-translation-model.ts +11 -0
|
@@ -63,6 +63,11 @@ AI SDK Core contains the following main functions:
|
|
|
63
63
|
description: 'Stream a transcript from live raw audio.',
|
|
64
64
|
href: '/docs/reference/ai-sdk-core/stream-transcribe',
|
|
65
65
|
},
|
|
66
|
+
{
|
|
67
|
+
title: 'experimental_streamTranslate()',
|
|
68
|
+
description: 'Stream a speech-to-speech translation from live raw audio.',
|
|
69
|
+
href: '/docs/reference/ai-sdk-core/stream-translate',
|
|
70
|
+
},
|
|
66
71
|
{
|
|
67
72
|
title: 'generateSpeech()',
|
|
68
73
|
description: 'Generate speech audio from text.',
|
|
@@ -134,7 +134,7 @@ To see `WorkflowAgent` in action, check out [these examples](#examples).
|
|
|
134
134
|
type: 'Experimental_SandboxSession',
|
|
135
135
|
isOptional: true,
|
|
136
136
|
description:
|
|
137
|
-
'Default sandbox session passed to tool execution as `experimental_sandbox
|
|
137
|
+
'Default sandbox session passed to tool descriptions and execution as `experimental_sandbox`, and exposed to `prepareStep`. Per-stream values override this default.',
|
|
138
138
|
},
|
|
139
139
|
{
|
|
140
140
|
name: 'prepareStep',
|
|
@@ -471,11 +471,17 @@ const result = await agent.stream({
|
|
|
471
471
|
description:
|
|
472
472
|
'A writable stream that receives raw model stream parts in real-time. Convert to UI message chunks at the response boundary using `createModelCallToUIChunkTransform()`.',
|
|
473
473
|
},
|
|
474
|
+
{
|
|
475
|
+
name: 'instructions',
|
|
476
|
+
type: 'Instructions',
|
|
477
|
+
isOptional: true,
|
|
478
|
+
description: 'Override the agent instructions for this call.',
|
|
479
|
+
},
|
|
474
480
|
{
|
|
475
481
|
name: 'system',
|
|
476
482
|
type: 'string',
|
|
477
483
|
isOptional: true,
|
|
478
|
-
description: '
|
|
484
|
+
description: 'Deprecated. Use `instructions` instead.',
|
|
479
485
|
},
|
|
480
486
|
{
|
|
481
487
|
name: 'stopWhen',
|
|
@@ -550,7 +556,7 @@ const result = await agent.stream({
|
|
|
550
556
|
type: 'Experimental_SandboxSession',
|
|
551
557
|
isOptional: true,
|
|
552
558
|
description:
|
|
553
|
-
'Sandbox session passed to tool execution as `experimental_sandbox
|
|
559
|
+
'Sandbox session passed to tool descriptions and execution as `experimental_sandbox`, and exposed to `prepareStep`. Overrides the constructor default.',
|
|
554
560
|
},
|
|
555
561
|
{
|
|
556
562
|
name: 'telemetry',
|
|
@@ -576,7 +582,8 @@ const result = await agent.stream({
|
|
|
576
582
|
name: 'prepareStep',
|
|
577
583
|
type: 'PrepareStepCallback',
|
|
578
584
|
isOptional: true,
|
|
579
|
-
description:
|
|
585
|
+
description:
|
|
586
|
+
'Per-call prepareStep override. Receives the initial instructions and messages alongside the current step state.',
|
|
580
587
|
},
|
|
581
588
|
{
|
|
582
589
|
name: 'experimental_onStart',
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: AI_NoTranslationGeneratedError
|
|
3
|
+
description: Learn how to fix AI_NoTranslationGeneratedError
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# AI_NoTranslationGeneratedError
|
|
7
|
+
|
|
8
|
+
This error occurs when no translation could be generated from the input audio:
|
|
9
|
+
no `audio` part was emitted and the final output text is empty, or the stream
|
|
10
|
+
ended without a finish event.
|
|
11
|
+
|
|
12
|
+
## Properties
|
|
13
|
+
|
|
14
|
+
- `response`: Speech translation model response metadata (required in constructor)
|
|
15
|
+
|
|
16
|
+
## Checking for this Error
|
|
17
|
+
|
|
18
|
+
You can check if an error is an instance of `AI_NoTranslationGeneratedError` using:
|
|
19
|
+
|
|
20
|
+
```typescript
|
|
21
|
+
import { NoTranslationGeneratedError } from 'ai';
|
|
22
|
+
|
|
23
|
+
if (NoTranslationGeneratedError.isInstance(error)) {
|
|
24
|
+
// Handle the error
|
|
25
|
+
}
|
|
26
|
+
```
|
|
@@ -24,6 +24,7 @@ collapsed: true
|
|
|
24
24
|
- [AI_NoContentGeneratedError](/docs/reference/ai-sdk-errors/ai-no-content-generated-error)
|
|
25
25
|
- [AI_NoImageGeneratedError](/docs/reference/ai-sdk-errors/ai-no-image-generated-error)
|
|
26
26
|
- [AI_NoTranscriptGeneratedError](/docs/reference/ai-sdk-errors/ai-no-transcript-generated-error)
|
|
27
|
+
- [AI_NoTranslationGeneratedError](/docs/reference/ai-sdk-errors/ai-no-translation-generated-error)
|
|
27
28
|
- [AI_NoVideoGeneratedError](/docs/reference/ai-sdk-errors/ai-no-video-generated-error)
|
|
28
29
|
- [AI_NoObjectGeneratedError](/docs/reference/ai-sdk-errors/ai-no-object-generated-error)
|
|
29
30
|
- [AI_NoOutputGeneratedError](/docs/reference/ai-sdk-errors/ai-no-output-generated-error)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ai",
|
|
3
|
-
"version": "7.0.
|
|
3
|
+
"version": "7.0.39",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "AI SDK by Vercel - build apps like ChatGPT, Claude, Gemini, and more with a single interface for any model using the Vercel AI Gateway or go direct to OpenAI, Anthropic, Google, or any other model provider.",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -42,9 +42,9 @@
|
|
|
42
42
|
}
|
|
43
43
|
},
|
|
44
44
|
"dependencies": {
|
|
45
|
-
"@ai-sdk/gateway": "4.0.
|
|
46
|
-
"@ai-sdk/provider": "4.0.
|
|
47
|
-
"@ai-sdk/provider-utils": "5.0.
|
|
45
|
+
"@ai-sdk/gateway": "4.0.30",
|
|
46
|
+
"@ai-sdk/provider": "4.0.4",
|
|
47
|
+
"@ai-sdk/provider-utils": "5.0.14"
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
50
50
|
"@edge-runtime/vm": "^5.0.0",
|
package/src/error/index.ts
CHANGED
|
@@ -27,6 +27,7 @@ export { NoObjectGeneratedError } from './no-object-generated-error';
|
|
|
27
27
|
export { NoOutputGeneratedError } from './no-output-generated-error';
|
|
28
28
|
export { NoSpeechGeneratedError } from './no-speech-generated-error';
|
|
29
29
|
export { NoTranscriptGeneratedError } from './no-transcript-generated-error';
|
|
30
|
+
export { NoTranslationGeneratedError } from './no-translation-generated-error';
|
|
30
31
|
export { NoVideoGeneratedError } from './no-video-generated-error';
|
|
31
32
|
export { NoSuchToolError } from './no-such-tool-error';
|
|
32
33
|
export { ToolCallRepairError } from './tool-call-repair-error';
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { AISDKError } from '@ai-sdk/provider';
|
|
2
|
+
import type { SpeechTranslationModelResponseMetadata } from '../types/speech-translation-model-response-metadata';
|
|
3
|
+
|
|
4
|
+
const name = 'AI_NoTranslationGeneratedError';
|
|
5
|
+
const marker = `vercel.ai.error.${name}`;
|
|
6
|
+
const symbol = Symbol.for(marker);
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Error that is thrown when no translation was generated.
|
|
10
|
+
*/
|
|
11
|
+
export class NoTranslationGeneratedError extends AISDKError {
|
|
12
|
+
private readonly [symbol] = true; // used in isInstance
|
|
13
|
+
|
|
14
|
+
readonly response: SpeechTranslationModelResponseMetadata;
|
|
15
|
+
|
|
16
|
+
constructor(options: { response: SpeechTranslationModelResponseMetadata }) {
|
|
17
|
+
super({
|
|
18
|
+
name,
|
|
19
|
+
message: 'No translation generated.',
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
this.response = options.response;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
static isInstance(error: unknown): error is NoTranslationGeneratedError {
|
|
26
|
+
return AISDKError.hasMarker(error, marker);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -96,8 +96,9 @@ const originalGenerateId = createIdGenerator({ prefix: 'aiobj', size: 24 });
|
|
|
96
96
|
* - 'enum': The output is an enum.
|
|
97
97
|
* - 'no-schema': The output is not a schema.
|
|
98
98
|
*
|
|
99
|
-
* @param
|
|
99
|
+
* @param repairText - A function that attempts to repair the raw output of the model
|
|
100
100
|
* to enable JSON parsing.
|
|
101
|
+
* @param experimental_repairText - Deprecated alias for `repairText`.
|
|
101
102
|
*
|
|
102
103
|
* @param telemetry - Optional telemetry configuration.
|
|
103
104
|
*
|
|
@@ -169,6 +170,14 @@ export async function generateObject<
|
|
|
169
170
|
* A function that attempts to repair the raw output of the model
|
|
170
171
|
* to enable JSON parsing.
|
|
171
172
|
*/
|
|
173
|
+
repairText?: RepairTextFunction;
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* A function that attempts to repair the raw output of the model
|
|
177
|
+
* to enable JSON parsing.
|
|
178
|
+
*
|
|
179
|
+
* @deprecated Use `repairText` instead.
|
|
180
|
+
*/
|
|
172
181
|
experimental_repairText?: RepairTextFunction;
|
|
173
182
|
|
|
174
183
|
/**
|
|
@@ -265,7 +274,8 @@ export async function generateObject<
|
|
|
265
274
|
maxRetries: maxRetriesArg,
|
|
266
275
|
abortSignal,
|
|
267
276
|
headers,
|
|
268
|
-
experimental_repairText
|
|
277
|
+
experimental_repairText,
|
|
278
|
+
repairText = experimental_repairText,
|
|
269
279
|
experimental_telemetry,
|
|
270
280
|
telemetry = experimental_telemetry,
|
|
271
281
|
experimental_download: download,
|
|
@@ -168,6 +168,10 @@ export type StreamObjectOnFinishCallback<RESULT> = (event: {
|
|
|
168
168
|
* - 'enum': The output is an enum.
|
|
169
169
|
* - 'no-schema': The output is not a schema.
|
|
170
170
|
*
|
|
171
|
+
* @param repairText - A function that attempts to repair the raw output of the model
|
|
172
|
+
* to enable JSON parsing.
|
|
173
|
+
* @param experimental_repairText - Deprecated alias for `repairText`.
|
|
174
|
+
*
|
|
171
175
|
* @param telemetry - Optional telemetry configuration.
|
|
172
176
|
*
|
|
173
177
|
* @param providerOptions - Additional provider-specific options. They are passed through
|
|
@@ -231,6 +235,14 @@ export function streamObject<
|
|
|
231
235
|
* A function that attempts to repair the raw output of the model
|
|
232
236
|
* to enable JSON parsing.
|
|
233
237
|
*/
|
|
238
|
+
repairText?: RepairTextFunction;
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* A function that attempts to repair the raw output of the model
|
|
242
|
+
* to enable JSON parsing.
|
|
243
|
+
*
|
|
244
|
+
* @deprecated Use `repairText` instead.
|
|
245
|
+
*/
|
|
234
246
|
experimental_repairText?: RepairTextFunction;
|
|
235
247
|
|
|
236
248
|
/**
|
|
@@ -346,7 +358,8 @@ export function streamObject<
|
|
|
346
358
|
maxRetries,
|
|
347
359
|
abortSignal,
|
|
348
360
|
headers,
|
|
349
|
-
experimental_repairText
|
|
361
|
+
experimental_repairText,
|
|
362
|
+
repairText = experimental_repairText,
|
|
350
363
|
experimental_telemetry,
|
|
351
364
|
telemetry = experimental_telemetry,
|
|
352
365
|
experimental_download: download,
|
|
@@ -1164,7 +1164,10 @@ export async function generateText<
|
|
|
1164
1164
|
// insert error tool outputs for invalid tool calls:
|
|
1165
1165
|
// TODO AI SDK 6: invalid inputs should not require output parts
|
|
1166
1166
|
const invalidToolCalls = stepToolCalls.filter(
|
|
1167
|
-
toolCall =>
|
|
1167
|
+
toolCall =>
|
|
1168
|
+
toolCall.invalid &&
|
|
1169
|
+
toolCall.dynamic &&
|
|
1170
|
+
!toolCall.providerExecuted,
|
|
1168
1171
|
);
|
|
1169
1172
|
|
|
1170
1173
|
clientToolOutputs = [];
|
|
@@ -627,18 +627,20 @@ function createLanguageModelV4StreamPartToLanguageModelStreamPartTransform<
|
|
|
627
627
|
modelCallContent.push(toolCall);
|
|
628
628
|
|
|
629
629
|
if (toolCall.invalid) {
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
630
|
+
if (!toolCall.providerExecuted) {
|
|
631
|
+
controller.enqueue({
|
|
632
|
+
type: 'tool-error',
|
|
633
|
+
toolCallId: toolCall.toolCallId,
|
|
634
|
+
toolName: toolCall.toolName,
|
|
635
|
+
input: toolCall.input,
|
|
636
|
+
error: getErrorMessage(toolCall.error!),
|
|
637
|
+
dynamic: true,
|
|
638
|
+
title: toolCall.title,
|
|
639
|
+
...(toolCall.toolMetadata != null
|
|
640
|
+
? { toolMetadata: toolCall.toolMetadata }
|
|
641
|
+
: {}),
|
|
642
|
+
});
|
|
643
|
+
}
|
|
642
644
|
break;
|
|
643
645
|
}
|
|
644
646
|
} catch (error) {
|
|
@@ -300,8 +300,9 @@ export interface StreamTextResult<
|
|
|
300
300
|
|
|
301
301
|
/**
|
|
302
302
|
* A text stream that returns only the generated text deltas. You can use it
|
|
303
|
-
* as either an AsyncIterable or a ReadableStream.
|
|
304
|
-
* stream
|
|
303
|
+
* as either an AsyncIterable or a ReadableStream. Error parts are not
|
|
304
|
+
* surfaced in this stream. Use the `onError` callback or `stream` to observe
|
|
305
|
+
* them.
|
|
305
306
|
*/
|
|
306
307
|
readonly textStream: AsyncIterableStream<string>;
|
|
307
308
|
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { gateway } from '@ai-sdk/gateway';
|
|
2
2
|
import type {
|
|
3
3
|
EmbeddingModelV4,
|
|
4
|
+
Experimental_SpeechTranslationModelV4,
|
|
4
5
|
Experimental_VideoModelV4,
|
|
5
6
|
ImageModelV4,
|
|
6
7
|
LanguageModelV4,
|
|
@@ -13,6 +14,7 @@ import { UnsupportedModelVersionError } from '../error';
|
|
|
13
14
|
import type { EmbeddingModel } from '../types/embedding-model';
|
|
14
15
|
import type { LanguageModel } from '../types/language-model';
|
|
15
16
|
import type { SpeechModel } from '../types/speech-model';
|
|
17
|
+
import type { SpeechTranslationModel } from '../types/speech-translation-model';
|
|
16
18
|
import type { TranscriptionModel } from '../types/transcription-model';
|
|
17
19
|
import { asEmbeddingModelV4 } from './as-embedding-model-v4';
|
|
18
20
|
import { asImageModelV4 } from './as-image-model-v4';
|
|
@@ -79,6 +81,39 @@ export function resolveTranscriptionModel(
|
|
|
79
81
|
return asTranscriptionModelV4(model);
|
|
80
82
|
}
|
|
81
83
|
|
|
84
|
+
export function resolveSpeechTranslationModel(
|
|
85
|
+
model: SpeechTranslationModel,
|
|
86
|
+
): Experimental_SpeechTranslationModelV4 {
|
|
87
|
+
if (typeof model === 'string') {
|
|
88
|
+
// Use raw global provider because speechTranslationModel is experimental
|
|
89
|
+
// and not part of the ProviderV4 interface
|
|
90
|
+
const provider = globalThis.AI_SDK_DEFAULT_PROVIDER ?? gateway;
|
|
91
|
+
// TODO AI SDK v7
|
|
92
|
+
// @ts-expect-error - speechTranslationModel support is experimental
|
|
93
|
+
const speechTranslationModel = provider.speechTranslationModel;
|
|
94
|
+
|
|
95
|
+
if (!speechTranslationModel) {
|
|
96
|
+
throw new Error(
|
|
97
|
+
'The default provider does not support speech translation models. ' +
|
|
98
|
+
'Please pass a provider model instance that implements the experimental speech translation model specification.',
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return speechTranslationModel(model);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (model.specificationVersion !== 'v4') {
|
|
106
|
+
const unsupportedModel: any = model;
|
|
107
|
+
throw new UnsupportedModelVersionError({
|
|
108
|
+
version: unsupportedModel.specificationVersion,
|
|
109
|
+
provider: unsupportedModel.provider,
|
|
110
|
+
modelId: unsupportedModel.modelId,
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return model;
|
|
115
|
+
}
|
|
116
|
+
|
|
82
117
|
export function resolveSpeechModel(
|
|
83
118
|
model: SpeechModel,
|
|
84
119
|
): SpeechModelV4 | undefined {
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Experimental_SpeechTranslationModelV4 } from '@ai-sdk/provider';
|
|
2
|
+
import { notImplemented } from './not-implemented';
|
|
3
|
+
|
|
4
|
+
export class MockSpeechTranslationModelV4 implements Experimental_SpeechTranslationModelV4 {
|
|
5
|
+
readonly specificationVersion = 'v4';
|
|
6
|
+
readonly provider: Experimental_SpeechTranslationModelV4['provider'];
|
|
7
|
+
readonly modelId: Experimental_SpeechTranslationModelV4['modelId'];
|
|
8
|
+
|
|
9
|
+
doStream: Experimental_SpeechTranslationModelV4['doStream'];
|
|
10
|
+
|
|
11
|
+
constructor({
|
|
12
|
+
provider = 'mock-provider',
|
|
13
|
+
modelId = 'mock-model-id',
|
|
14
|
+
doStream = notImplemented,
|
|
15
|
+
}: {
|
|
16
|
+
provider?: Experimental_SpeechTranslationModelV4['provider'];
|
|
17
|
+
modelId?: Experimental_SpeechTranslationModelV4['modelId'];
|
|
18
|
+
doStream?: Experimental_SpeechTranslationModelV4['doStream'];
|
|
19
|
+
} = {}) {
|
|
20
|
+
this.provider = provider;
|
|
21
|
+
this.modelId = modelId;
|
|
22
|
+
this.doStream = doStream;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -2,6 +2,7 @@ import {
|
|
|
2
2
|
UnsupportedFunctionalityError,
|
|
3
3
|
type Experimental_TranscriptionModelV4StreamPart,
|
|
4
4
|
type JSONObject,
|
|
5
|
+
type SharedV4AudioFormat,
|
|
5
6
|
} from '@ai-sdk/provider';
|
|
6
7
|
import {
|
|
7
8
|
DelayedPromise,
|
|
@@ -63,17 +64,7 @@ export function streamTranscribe({
|
|
|
63
64
|
/**
|
|
64
65
|
* The input audio format for the raw audio chunks.
|
|
65
66
|
*/
|
|
66
|
-
inputAudioFormat:
|
|
67
|
-
/**
|
|
68
|
-
* Audio format type, e.g. `audio/pcm`, `audio/pcmu`, or `audio/pcma`.
|
|
69
|
-
*/
|
|
70
|
-
type: string;
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Sample rate in Hz. Only applicable for formats that require a rate.
|
|
74
|
-
*/
|
|
75
|
-
rate?: number;
|
|
76
|
-
};
|
|
67
|
+
inputAudioFormat: SharedV4AudioFormat;
|
|
77
68
|
|
|
78
69
|
/**
|
|
79
70
|
* Additional provider-specific options.
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Experimental_SpeechTranslationModelV4Usage,
|
|
3
|
+
JSONObject,
|
|
4
|
+
SharedV4ProviderMetadata,
|
|
5
|
+
} from '@ai-sdk/provider';
|
|
6
|
+
import type { SpeechTranslationModelResponseMetadata } from '../types/speech-translation-model-response-metadata';
|
|
7
|
+
import type { Warning } from '../types/warning';
|
|
8
|
+
import type { AsyncIterableStream } from '../util/async-iterable-stream';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Stream parts emitted by `experimental_streamTranslate`.
|
|
12
|
+
*
|
|
13
|
+
* Speech translation model stream parts are passed through unchanged, except
|
|
14
|
+
* for `stream-start`, `response-metadata`, and `finish`, which are consumed
|
|
15
|
+
* internally and surfaced via the result promises.
|
|
16
|
+
*/
|
|
17
|
+
export type TranslationStreamPart =
|
|
18
|
+
| {
|
|
19
|
+
/**
|
|
20
|
+
* Translated audio chunk in the target language.
|
|
21
|
+
*
|
|
22
|
+
* `Uint8Array` chunks contain raw audio bytes. `string` chunks contain
|
|
23
|
+
* base64-encoded raw audio bytes.
|
|
24
|
+
*/
|
|
25
|
+
type: 'audio';
|
|
26
|
+
id?: string;
|
|
27
|
+
audio: Uint8Array | string;
|
|
28
|
+
providerMetadata?: SharedV4ProviderMetadata;
|
|
29
|
+
}
|
|
30
|
+
| {
|
|
31
|
+
/**
|
|
32
|
+
* Append-only translated text delta.
|
|
33
|
+
*/
|
|
34
|
+
type: 'output-text-delta';
|
|
35
|
+
id?: string;
|
|
36
|
+
delta: string;
|
|
37
|
+
providerMetadata?: SharedV4ProviderMetadata;
|
|
38
|
+
}
|
|
39
|
+
| {
|
|
40
|
+
/**
|
|
41
|
+
* Final translated text for a provider-defined segment or utterance.
|
|
42
|
+
*/
|
|
43
|
+
type: 'output-text-final';
|
|
44
|
+
id?: string;
|
|
45
|
+
text: string;
|
|
46
|
+
providerMetadata?: SharedV4ProviderMetadata;
|
|
47
|
+
}
|
|
48
|
+
| {
|
|
49
|
+
/**
|
|
50
|
+
* Append-only source transcript delta.
|
|
51
|
+
*/
|
|
52
|
+
type: 'source-transcript-delta';
|
|
53
|
+
id?: string;
|
|
54
|
+
delta: string;
|
|
55
|
+
providerMetadata?: SharedV4ProviderMetadata;
|
|
56
|
+
}
|
|
57
|
+
| {
|
|
58
|
+
/**
|
|
59
|
+
* Non-final source transcript text. The text may be revised by later parts.
|
|
60
|
+
*/
|
|
61
|
+
type: 'source-transcript-partial';
|
|
62
|
+
id?: string;
|
|
63
|
+
text: string;
|
|
64
|
+
startSecond?: number;
|
|
65
|
+
endSecond?: number;
|
|
66
|
+
channelIndex?: number;
|
|
67
|
+
providerMetadata?: SharedV4ProviderMetadata;
|
|
68
|
+
}
|
|
69
|
+
| {
|
|
70
|
+
/**
|
|
71
|
+
* Final source transcript text for a provider-defined segment or utterance.
|
|
72
|
+
*/
|
|
73
|
+
type: 'source-transcript-final';
|
|
74
|
+
id?: string;
|
|
75
|
+
text: string;
|
|
76
|
+
startSecond?: number;
|
|
77
|
+
endSecond?: number;
|
|
78
|
+
channelIndex?: number;
|
|
79
|
+
providerMetadata?: SharedV4ProviderMetadata;
|
|
80
|
+
}
|
|
81
|
+
| {
|
|
82
|
+
/**
|
|
83
|
+
* Raw provider chunks if enabled via `includeRawChunks`.
|
|
84
|
+
*/
|
|
85
|
+
type: 'raw';
|
|
86
|
+
rawValue: unknown;
|
|
87
|
+
}
|
|
88
|
+
| {
|
|
89
|
+
/**
|
|
90
|
+
* Error parts are streamed, allowing for multiple errors.
|
|
91
|
+
*/
|
|
92
|
+
type: 'error';
|
|
93
|
+
error: unknown;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
export interface StreamTranslationResult {
|
|
97
|
+
/**
|
|
98
|
+
* The final source-language transcript of the input audio.
|
|
99
|
+
*/
|
|
100
|
+
readonly sourceText: PromiseLike<string>;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* The final translated text in the target language.
|
|
104
|
+
*
|
|
105
|
+
* May resolve to an empty string for providers that produce only audio
|
|
106
|
+
* output.
|
|
107
|
+
*/
|
|
108
|
+
readonly translationText: PromiseLike<string>;
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* The duration of the source audio in seconds, if available.
|
|
112
|
+
*/
|
|
113
|
+
readonly durationInSeconds: PromiseLike<number | undefined>;
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Usage information for the translation call, if reported by the provider.
|
|
117
|
+
*/
|
|
118
|
+
readonly usage: PromiseLike<
|
|
119
|
+
Experimental_SpeechTranslationModelV4Usage | undefined
|
|
120
|
+
>;
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Warnings for the call, e.g. unsupported settings.
|
|
124
|
+
*/
|
|
125
|
+
readonly warnings: PromiseLike<Array<Warning>>;
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Response metadata.
|
|
129
|
+
*/
|
|
130
|
+
readonly response: PromiseLike<SpeechTranslationModelResponseMetadata>;
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Additional provider-specific metadata.
|
|
134
|
+
*/
|
|
135
|
+
readonly providerMetadata: PromiseLike<Record<string, JSONObject>>;
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Full stream of translation parts.
|
|
139
|
+
*
|
|
140
|
+
* This is a single-consumer live stream and can only be accessed once.
|
|
141
|
+
* Access it before any result promise when both stream parts and final
|
|
142
|
+
* results are needed; accessing a result promise first consumes the stream
|
|
143
|
+
* internally and makes `fullStream` unavailable.
|
|
144
|
+
*/
|
|
145
|
+
readonly fullStream: AsyncIterableStream<TranslationStreamPart>;
|
|
146
|
+
}
|