@ai-sdk/gateway 4.0.0-beta.1 → 4.0.0-beta.108
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +794 -4
- package/dist/index.d.ts +314 -42
- package/dist/index.js +1369 -397
- package/dist/index.js.map +1 -1
- package/docs/00-ai-gateway.mdx +447 -61
- package/package.json +15 -15
- package/src/errors/as-gateway-error.ts +2 -1
- package/src/errors/create-gateway-error.ts +17 -3
- package/src/errors/gateway-authentication-error.ts +8 -5
- package/src/errors/gateway-error.ts +8 -0
- package/src/errors/gateway-failed-dependency-error.ts +35 -0
- package/src/errors/gateway-forbidden-error.ts +34 -0
- package/src/errors/gateway-response-error.ts +1 -1
- package/src/errors/index.ts +2 -0
- package/src/errors/parse-auth-method.ts +1 -2
- package/src/gateway-config.ts +1 -1
- package/src/gateway-embedding-model-settings.ts +1 -0
- package/src/gateway-embedding-model.ts +62 -15
- package/src/gateway-fetch-metadata.ts +51 -38
- package/src/gateway-generation-info.ts +149 -0
- package/src/gateway-headers.ts +3 -0
- package/src/gateway-image-model-settings.ts +14 -1
- package/src/gateway-image-model.ts +46 -21
- package/src/gateway-language-model-settings.ts +52 -31
- package/src/gateway-language-model.ts +72 -42
- package/src/gateway-model-entry.ts +15 -3
- package/src/gateway-provider-options.ts +50 -78
- package/src/gateway-provider.ts +239 -35
- package/src/gateway-realtime-auth.ts +126 -0
- package/src/gateway-realtime-model-settings.ts +1 -0
- package/src/gateway-realtime-model.ts +107 -0
- package/src/gateway-reranking-model-settings.ts +7 -0
- package/src/gateway-reranking-model.ts +142 -0
- package/src/gateway-speech-model-settings.ts +1 -0
- package/src/gateway-speech-model.ts +145 -0
- package/src/gateway-spend-report.ts +193 -0
- package/src/gateway-transcription-model-settings.ts +1 -0
- package/src/gateway-transcription-model.ts +155 -0
- package/src/gateway-video-model-settings.ts +4 -0
- package/src/gateway-video-model.ts +29 -19
- package/src/index.ts +30 -5
- package/src/tool/parallel-search.ts +10 -11
- package/src/tool/perplexity-search.ts +10 -11
- package/dist/index.d.mts +0 -602
- package/dist/index.mjs +0 -1539
- package/dist/index.mjs.map +0 -1
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
RerankingModelV4,
|
|
3
|
+
SharedV4ProviderMetadata,
|
|
4
|
+
} from '@ai-sdk/provider';
|
|
5
|
+
import {
|
|
6
|
+
combineHeaders,
|
|
7
|
+
createJsonErrorResponseHandler,
|
|
8
|
+
createJsonResponseHandler,
|
|
9
|
+
lazySchema,
|
|
10
|
+
postJsonToApi,
|
|
11
|
+
resolve,
|
|
12
|
+
zodSchema,
|
|
13
|
+
type Resolvable,
|
|
14
|
+
} from '@ai-sdk/provider-utils';
|
|
15
|
+
import { z } from 'zod/v4';
|
|
16
|
+
import { asGatewayError } from './errors';
|
|
17
|
+
import { parseAuthMethod } from './errors/parse-auth-method';
|
|
18
|
+
import type { GatewayConfig } from './gateway-config';
|
|
19
|
+
|
|
20
|
+
export class GatewayRerankingModel implements RerankingModelV4 {
|
|
21
|
+
readonly specificationVersion = 'v4';
|
|
22
|
+
|
|
23
|
+
constructor(
|
|
24
|
+
readonly modelId: string,
|
|
25
|
+
private readonly config: GatewayConfig & {
|
|
26
|
+
provider: string;
|
|
27
|
+
o11yHeaders: Resolvable<Record<string, string>>;
|
|
28
|
+
},
|
|
29
|
+
) {}
|
|
30
|
+
|
|
31
|
+
get provider(): string {
|
|
32
|
+
return this.config.provider;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async doRerank({
|
|
36
|
+
documents,
|
|
37
|
+
query,
|
|
38
|
+
topN,
|
|
39
|
+
headers,
|
|
40
|
+
abortSignal,
|
|
41
|
+
providerOptions,
|
|
42
|
+
}: Parameters<RerankingModelV4['doRerank']>[0]): Promise<
|
|
43
|
+
Awaited<ReturnType<RerankingModelV4['doRerank']>>
|
|
44
|
+
> {
|
|
45
|
+
const resolvedHeaders = this.config.headers
|
|
46
|
+
? await resolve(this.config.headers)
|
|
47
|
+
: undefined;
|
|
48
|
+
try {
|
|
49
|
+
const {
|
|
50
|
+
responseHeaders,
|
|
51
|
+
value: responseBody,
|
|
52
|
+
rawValue,
|
|
53
|
+
} = await postJsonToApi({
|
|
54
|
+
url: this.getUrl(),
|
|
55
|
+
headers: combineHeaders(
|
|
56
|
+
resolvedHeaders,
|
|
57
|
+
headers ?? {},
|
|
58
|
+
this.getModelConfigHeaders(),
|
|
59
|
+
await resolve(this.config.o11yHeaders),
|
|
60
|
+
),
|
|
61
|
+
body: {
|
|
62
|
+
documents,
|
|
63
|
+
query,
|
|
64
|
+
...(topN != null ? { topN } : {}),
|
|
65
|
+
...(providerOptions ? { providerOptions } : {}),
|
|
66
|
+
},
|
|
67
|
+
successfulResponseHandler: createJsonResponseHandler(
|
|
68
|
+
gatewayRerankingResponseSchema,
|
|
69
|
+
),
|
|
70
|
+
failedResponseHandler: createJsonErrorResponseHandler({
|
|
71
|
+
errorSchema: z.any(),
|
|
72
|
+
errorToMessage: data => data,
|
|
73
|
+
}),
|
|
74
|
+
...(abortSignal && { abortSignal }),
|
|
75
|
+
fetch: this.config.fetch,
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
return {
|
|
79
|
+
ranking: responseBody.ranking,
|
|
80
|
+
providerMetadata:
|
|
81
|
+
responseBody.providerMetadata as unknown as SharedV4ProviderMetadata,
|
|
82
|
+
response: { headers: responseHeaders, body: rawValue },
|
|
83
|
+
warnings: responseBody.warnings ?? [],
|
|
84
|
+
};
|
|
85
|
+
} catch (error) {
|
|
86
|
+
throw await asGatewayError(
|
|
87
|
+
error,
|
|
88
|
+
await parseAuthMethod(resolvedHeaders ?? {}),
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
private getUrl() {
|
|
94
|
+
return `${this.config.baseURL}/reranking-model`;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
private getModelConfigHeaders() {
|
|
98
|
+
return {
|
|
99
|
+
'ai-reranking-model-specification-version': '4',
|
|
100
|
+
'ai-model-id': this.modelId,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const gatewayRerankingWarningSchema = z.discriminatedUnion('type', [
|
|
106
|
+
z.object({
|
|
107
|
+
type: z.literal('unsupported'),
|
|
108
|
+
feature: z.string(),
|
|
109
|
+
details: z.string().optional(),
|
|
110
|
+
}),
|
|
111
|
+
z.object({
|
|
112
|
+
type: z.literal('compatibility'),
|
|
113
|
+
feature: z.string(),
|
|
114
|
+
details: z.string().optional(),
|
|
115
|
+
}),
|
|
116
|
+
z.object({
|
|
117
|
+
type: z.literal('deprecated'),
|
|
118
|
+
setting: z.string(),
|
|
119
|
+
message: z.string(),
|
|
120
|
+
}),
|
|
121
|
+
z.object({
|
|
122
|
+
type: z.literal('other'),
|
|
123
|
+
message: z.string(),
|
|
124
|
+
}),
|
|
125
|
+
]);
|
|
126
|
+
|
|
127
|
+
const gatewayRerankingResponseSchema = lazySchema(() =>
|
|
128
|
+
zodSchema(
|
|
129
|
+
z.object({
|
|
130
|
+
ranking: z.array(
|
|
131
|
+
z.object({
|
|
132
|
+
index: z.number(),
|
|
133
|
+
relevanceScore: z.number(),
|
|
134
|
+
}),
|
|
135
|
+
),
|
|
136
|
+
warnings: z.array(gatewayRerankingWarningSchema).optional(),
|
|
137
|
+
providerMetadata: z
|
|
138
|
+
.record(z.string(), z.record(z.string(), z.unknown()))
|
|
139
|
+
.optional(),
|
|
140
|
+
}),
|
|
141
|
+
),
|
|
142
|
+
);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type GatewaySpeechModelId = string & {};
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
SharedV4ProviderMetadata,
|
|
3
|
+
SharedV4Warning,
|
|
4
|
+
SpeechModelV4,
|
|
5
|
+
} from '@ai-sdk/provider';
|
|
6
|
+
import {
|
|
7
|
+
combineHeaders,
|
|
8
|
+
createJsonErrorResponseHandler,
|
|
9
|
+
createJsonResponseHandler,
|
|
10
|
+
postJsonToApi,
|
|
11
|
+
resolve,
|
|
12
|
+
type Resolvable,
|
|
13
|
+
} from '@ai-sdk/provider-utils';
|
|
14
|
+
import { z } from 'zod/v4';
|
|
15
|
+
import { asGatewayError } from './errors';
|
|
16
|
+
import { parseAuthMethod } from './errors/parse-auth-method';
|
|
17
|
+
import type { GatewayConfig } from './gateway-config';
|
|
18
|
+
|
|
19
|
+
export class GatewaySpeechModel implements SpeechModelV4 {
|
|
20
|
+
readonly specificationVersion = 'v4' as const;
|
|
21
|
+
|
|
22
|
+
constructor(
|
|
23
|
+
readonly modelId: string,
|
|
24
|
+
private readonly config: GatewayConfig & {
|
|
25
|
+
provider: string;
|
|
26
|
+
o11yHeaders: Resolvable<Record<string, string>>;
|
|
27
|
+
},
|
|
28
|
+
) {}
|
|
29
|
+
|
|
30
|
+
get provider(): string {
|
|
31
|
+
return this.config.provider;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async doGenerate({
|
|
35
|
+
text,
|
|
36
|
+
voice,
|
|
37
|
+
outputFormat,
|
|
38
|
+
instructions,
|
|
39
|
+
speed,
|
|
40
|
+
language,
|
|
41
|
+
providerOptions,
|
|
42
|
+
headers,
|
|
43
|
+
abortSignal,
|
|
44
|
+
}: Parameters<SpeechModelV4['doGenerate']>[0]): Promise<
|
|
45
|
+
Awaited<ReturnType<SpeechModelV4['doGenerate']>>
|
|
46
|
+
> {
|
|
47
|
+
const resolvedHeaders = this.config.headers
|
|
48
|
+
? await resolve(this.config.headers)
|
|
49
|
+
: undefined;
|
|
50
|
+
try {
|
|
51
|
+
const {
|
|
52
|
+
responseHeaders,
|
|
53
|
+
value: responseBody,
|
|
54
|
+
rawValue,
|
|
55
|
+
} = await postJsonToApi({
|
|
56
|
+
url: this.getUrl(),
|
|
57
|
+
headers: combineHeaders(
|
|
58
|
+
resolvedHeaders,
|
|
59
|
+
headers ?? {},
|
|
60
|
+
this.getModelConfigHeaders(),
|
|
61
|
+
await resolve(this.config.o11yHeaders),
|
|
62
|
+
),
|
|
63
|
+
body: {
|
|
64
|
+
text,
|
|
65
|
+
...(voice && { voice }),
|
|
66
|
+
...(outputFormat && { outputFormat }),
|
|
67
|
+
...(instructions && { instructions }),
|
|
68
|
+
...(speed != null && { speed }),
|
|
69
|
+
...(language && { language }),
|
|
70
|
+
...(providerOptions && { providerOptions }),
|
|
71
|
+
},
|
|
72
|
+
successfulResponseHandler: createJsonResponseHandler(
|
|
73
|
+
gatewaySpeechResponseSchema,
|
|
74
|
+
),
|
|
75
|
+
failedResponseHandler: createJsonErrorResponseHandler({
|
|
76
|
+
errorSchema: z.any(),
|
|
77
|
+
errorToMessage: data => data,
|
|
78
|
+
}),
|
|
79
|
+
...(abortSignal && { abortSignal }),
|
|
80
|
+
fetch: this.config.fetch,
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
return {
|
|
84
|
+
audio: responseBody.audio,
|
|
85
|
+
warnings: (responseBody.warnings ?? []) as Array<SharedV4Warning>,
|
|
86
|
+
providerMetadata:
|
|
87
|
+
responseBody.providerMetadata as SharedV4ProviderMetadata,
|
|
88
|
+
response: {
|
|
89
|
+
timestamp: new Date(),
|
|
90
|
+
modelId: this.modelId,
|
|
91
|
+
headers: responseHeaders,
|
|
92
|
+
body: rawValue,
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
} catch (error) {
|
|
96
|
+
throw await asGatewayError(
|
|
97
|
+
error,
|
|
98
|
+
await parseAuthMethod(resolvedHeaders ?? {}),
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
private getUrl() {
|
|
104
|
+
return `${this.config.baseURL}/speech-model`;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
private getModelConfigHeaders() {
|
|
108
|
+
return {
|
|
109
|
+
'ai-speech-model-specification-version': '4',
|
|
110
|
+
'ai-model-id': this.modelId,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const providerMetadataEntrySchema = z.object({}).catchall(z.unknown());
|
|
116
|
+
|
|
117
|
+
const gatewaySpeechWarningSchema = z.discriminatedUnion('type', [
|
|
118
|
+
z.object({
|
|
119
|
+
type: z.literal('unsupported'),
|
|
120
|
+
feature: z.string(),
|
|
121
|
+
details: z.string().optional(),
|
|
122
|
+
}),
|
|
123
|
+
z.object({
|
|
124
|
+
type: z.literal('compatibility'),
|
|
125
|
+
feature: z.string(),
|
|
126
|
+
details: z.string().optional(),
|
|
127
|
+
}),
|
|
128
|
+
z.object({
|
|
129
|
+
type: z.literal('deprecated'),
|
|
130
|
+
setting: z.string(),
|
|
131
|
+
message: z.string(),
|
|
132
|
+
}),
|
|
133
|
+
z.object({
|
|
134
|
+
type: z.literal('other'),
|
|
135
|
+
message: z.string(),
|
|
136
|
+
}),
|
|
137
|
+
]);
|
|
138
|
+
|
|
139
|
+
const gatewaySpeechResponseSchema = z.object({
|
|
140
|
+
audio: z.string(),
|
|
141
|
+
warnings: z.array(gatewaySpeechWarningSchema).optional(),
|
|
142
|
+
providerMetadata: z
|
|
143
|
+
.record(z.string(), providerMetadataEntrySchema)
|
|
144
|
+
.optional(),
|
|
145
|
+
});
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createJsonErrorResponseHandler,
|
|
3
|
+
createJsonResponseHandler,
|
|
4
|
+
getFromApi,
|
|
5
|
+
lazySchema,
|
|
6
|
+
resolve,
|
|
7
|
+
zodSchema,
|
|
8
|
+
} from '@ai-sdk/provider-utils';
|
|
9
|
+
import { z } from 'zod/v4';
|
|
10
|
+
import { asGatewayError } from './errors';
|
|
11
|
+
import type { GatewayConfig } from './gateway-config';
|
|
12
|
+
|
|
13
|
+
export interface GatewaySpendReportParams {
|
|
14
|
+
/** Start date in YYYY-MM-DD format (inclusive) */
|
|
15
|
+
startDate: string;
|
|
16
|
+
/** End date in YYYY-MM-DD format (inclusive) */
|
|
17
|
+
endDate: string;
|
|
18
|
+
/** Primary aggregation dimension. Defaults to 'day'. */
|
|
19
|
+
groupBy?: 'day' | 'user' | 'model' | 'tag' | 'provider' | 'credential_type';
|
|
20
|
+
/** Time granularity when groupBy is 'day'. */
|
|
21
|
+
datePart?: 'day' | 'hour';
|
|
22
|
+
/** Filter to a specific user's spend. */
|
|
23
|
+
userId?: string;
|
|
24
|
+
/** Filter to a specific model (e.g. 'anthropic/claude-sonnet-4.5'). */
|
|
25
|
+
model?: string;
|
|
26
|
+
/** Filter to a specific provider (e.g. 'anthropic'). */
|
|
27
|
+
provider?: string;
|
|
28
|
+
/** Filter to BYOK or system credentials. */
|
|
29
|
+
credentialType?: 'byok' | 'system';
|
|
30
|
+
/** Filter to requests with these tags. */
|
|
31
|
+
tags?: string[];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface GatewaySpendReportRow {
|
|
35
|
+
/** Date string (present when groupBy is 'day') */
|
|
36
|
+
day?: string;
|
|
37
|
+
/** Hour timestamp (present when groupBy is 'day' and datePart is 'hour') */
|
|
38
|
+
hour?: string;
|
|
39
|
+
/** User identifier (present when groupBy is 'user') */
|
|
40
|
+
user?: string;
|
|
41
|
+
/** Model identifier (present when groupBy is 'model') */
|
|
42
|
+
model?: string;
|
|
43
|
+
/** Tag value (present when groupBy is 'tag') */
|
|
44
|
+
tag?: string;
|
|
45
|
+
/** Provider name (present when groupBy is 'provider') */
|
|
46
|
+
provider?: string;
|
|
47
|
+
/** Credential type (present when groupBy is 'credential_type') */
|
|
48
|
+
credentialType?: 'byok' | 'system';
|
|
49
|
+
|
|
50
|
+
/** Total cost in USD */
|
|
51
|
+
totalCost: number;
|
|
52
|
+
/** Market cost in USD */
|
|
53
|
+
marketCost?: number;
|
|
54
|
+
/** Number of input tokens */
|
|
55
|
+
inputTokens?: number;
|
|
56
|
+
/** Number of output tokens */
|
|
57
|
+
outputTokens?: number;
|
|
58
|
+
/** Number of cached input tokens */
|
|
59
|
+
cachedInputTokens?: number;
|
|
60
|
+
/** Number of cache creation input tokens */
|
|
61
|
+
cacheCreationInputTokens?: number;
|
|
62
|
+
/** Number of reasoning tokens */
|
|
63
|
+
reasoningTokens?: number;
|
|
64
|
+
/** Number of requests */
|
|
65
|
+
requestCount?: number;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export interface GatewaySpendReportResponse {
|
|
69
|
+
results: GatewaySpendReportRow[];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export class GatewaySpendReport {
|
|
73
|
+
constructor(private readonly config: GatewayConfig) {}
|
|
74
|
+
|
|
75
|
+
async getSpendReport(
|
|
76
|
+
params: GatewaySpendReportParams,
|
|
77
|
+
): Promise<GatewaySpendReportResponse> {
|
|
78
|
+
try {
|
|
79
|
+
const baseUrl = new URL(this.config.baseURL);
|
|
80
|
+
|
|
81
|
+
const searchParams = new URLSearchParams();
|
|
82
|
+
searchParams.set('start_date', params.startDate);
|
|
83
|
+
searchParams.set('end_date', params.endDate);
|
|
84
|
+
|
|
85
|
+
if (params.groupBy) {
|
|
86
|
+
searchParams.set('group_by', params.groupBy);
|
|
87
|
+
}
|
|
88
|
+
if (params.datePart) {
|
|
89
|
+
searchParams.set('date_part', params.datePart);
|
|
90
|
+
}
|
|
91
|
+
if (params.userId) {
|
|
92
|
+
searchParams.set('user_id', params.userId);
|
|
93
|
+
}
|
|
94
|
+
if (params.model) {
|
|
95
|
+
searchParams.set('model', params.model);
|
|
96
|
+
}
|
|
97
|
+
if (params.provider) {
|
|
98
|
+
searchParams.set('provider', params.provider);
|
|
99
|
+
}
|
|
100
|
+
if (params.credentialType) {
|
|
101
|
+
searchParams.set('credential_type', params.credentialType);
|
|
102
|
+
}
|
|
103
|
+
if (params.tags && params.tags.length > 0) {
|
|
104
|
+
searchParams.set('tags', params.tags.join(','));
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const { value } = await getFromApi({
|
|
108
|
+
url: `${baseUrl.origin}/v1/report?${searchParams.toString()}`,
|
|
109
|
+
headers: this.config.headers
|
|
110
|
+
? await resolve(this.config.headers)
|
|
111
|
+
: undefined,
|
|
112
|
+
successfulResponseHandler: createJsonResponseHandler(
|
|
113
|
+
gatewaySpendReportResponseSchema,
|
|
114
|
+
),
|
|
115
|
+
failedResponseHandler: createJsonErrorResponseHandler({
|
|
116
|
+
errorSchema: z.any(),
|
|
117
|
+
errorToMessage: data => data,
|
|
118
|
+
}),
|
|
119
|
+
fetch: this.config.fetch,
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
return value;
|
|
123
|
+
} catch (error) {
|
|
124
|
+
throw await asGatewayError(error);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const gatewaySpendReportResponseSchema = lazySchema(() =>
|
|
130
|
+
zodSchema(
|
|
131
|
+
z.object({
|
|
132
|
+
results: z.array(
|
|
133
|
+
z
|
|
134
|
+
.object({
|
|
135
|
+
day: z.string().optional(),
|
|
136
|
+
hour: z.string().optional(),
|
|
137
|
+
user: z.string().optional(),
|
|
138
|
+
model: z.string().optional(),
|
|
139
|
+
tag: z.string().optional(),
|
|
140
|
+
provider: z.string().optional(),
|
|
141
|
+
credential_type: z.enum(['byok', 'system']).optional(),
|
|
142
|
+
total_cost: z.number(),
|
|
143
|
+
market_cost: z.number().optional(),
|
|
144
|
+
input_tokens: z.number().optional(),
|
|
145
|
+
output_tokens: z.number().optional(),
|
|
146
|
+
cached_input_tokens: z.number().optional(),
|
|
147
|
+
cache_creation_input_tokens: z.number().optional(),
|
|
148
|
+
reasoning_tokens: z.number().optional(),
|
|
149
|
+
request_count: z.number().optional(),
|
|
150
|
+
})
|
|
151
|
+
.transform(
|
|
152
|
+
({
|
|
153
|
+
credential_type,
|
|
154
|
+
total_cost,
|
|
155
|
+
market_cost,
|
|
156
|
+
input_tokens,
|
|
157
|
+
output_tokens,
|
|
158
|
+
cached_input_tokens,
|
|
159
|
+
cache_creation_input_tokens,
|
|
160
|
+
reasoning_tokens,
|
|
161
|
+
request_count,
|
|
162
|
+
...rest
|
|
163
|
+
}) => ({
|
|
164
|
+
...rest,
|
|
165
|
+
...(credential_type !== undefined
|
|
166
|
+
? { credentialType: credential_type }
|
|
167
|
+
: {}),
|
|
168
|
+
totalCost: total_cost,
|
|
169
|
+
...(market_cost !== undefined ? { marketCost: market_cost } : {}),
|
|
170
|
+
...(input_tokens !== undefined
|
|
171
|
+
? { inputTokens: input_tokens }
|
|
172
|
+
: {}),
|
|
173
|
+
...(output_tokens !== undefined
|
|
174
|
+
? { outputTokens: output_tokens }
|
|
175
|
+
: {}),
|
|
176
|
+
...(cached_input_tokens !== undefined
|
|
177
|
+
? { cachedInputTokens: cached_input_tokens }
|
|
178
|
+
: {}),
|
|
179
|
+
...(cache_creation_input_tokens !== undefined
|
|
180
|
+
? { cacheCreationInputTokens: cache_creation_input_tokens }
|
|
181
|
+
: {}),
|
|
182
|
+
...(reasoning_tokens !== undefined
|
|
183
|
+
? { reasoningTokens: reasoning_tokens }
|
|
184
|
+
: {}),
|
|
185
|
+
...(request_count !== undefined
|
|
186
|
+
? { requestCount: request_count }
|
|
187
|
+
: {}),
|
|
188
|
+
}),
|
|
189
|
+
),
|
|
190
|
+
),
|
|
191
|
+
}),
|
|
192
|
+
),
|
|
193
|
+
);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type GatewayTranscriptionModelId = string & {};
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
SharedV4ProviderMetadata,
|
|
3
|
+
SharedV4Warning,
|
|
4
|
+
TranscriptionModelV4,
|
|
5
|
+
} from '@ai-sdk/provider';
|
|
6
|
+
import {
|
|
7
|
+
combineHeaders,
|
|
8
|
+
convertUint8ArrayToBase64,
|
|
9
|
+
createJsonErrorResponseHandler,
|
|
10
|
+
createJsonResponseHandler,
|
|
11
|
+
postJsonToApi,
|
|
12
|
+
resolve,
|
|
13
|
+
type Resolvable,
|
|
14
|
+
} from '@ai-sdk/provider-utils';
|
|
15
|
+
import { z } from 'zod/v4';
|
|
16
|
+
import { asGatewayError } from './errors';
|
|
17
|
+
import { parseAuthMethod } from './errors/parse-auth-method';
|
|
18
|
+
import type { GatewayConfig } from './gateway-config';
|
|
19
|
+
|
|
20
|
+
export class GatewayTranscriptionModel implements TranscriptionModelV4 {
|
|
21
|
+
readonly specificationVersion = 'v4' as const;
|
|
22
|
+
|
|
23
|
+
constructor(
|
|
24
|
+
readonly modelId: string,
|
|
25
|
+
private readonly config: GatewayConfig & {
|
|
26
|
+
provider: string;
|
|
27
|
+
o11yHeaders: Resolvable<Record<string, string>>;
|
|
28
|
+
},
|
|
29
|
+
) {}
|
|
30
|
+
|
|
31
|
+
get provider(): string {
|
|
32
|
+
return this.config.provider;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async doGenerate({
|
|
36
|
+
audio,
|
|
37
|
+
mediaType,
|
|
38
|
+
providerOptions,
|
|
39
|
+
headers,
|
|
40
|
+
abortSignal,
|
|
41
|
+
}: Parameters<TranscriptionModelV4['doGenerate']>[0]): Promise<
|
|
42
|
+
Awaited<ReturnType<TranscriptionModelV4['doGenerate']>>
|
|
43
|
+
> {
|
|
44
|
+
const resolvedHeaders = this.config.headers
|
|
45
|
+
? await resolve(this.config.headers)
|
|
46
|
+
: undefined;
|
|
47
|
+
try {
|
|
48
|
+
const {
|
|
49
|
+
responseHeaders,
|
|
50
|
+
value: responseBody,
|
|
51
|
+
rawValue,
|
|
52
|
+
} = await postJsonToApi({
|
|
53
|
+
url: this.getUrl(),
|
|
54
|
+
headers: combineHeaders(
|
|
55
|
+
resolvedHeaders,
|
|
56
|
+
headers ?? {},
|
|
57
|
+
this.getModelConfigHeaders(),
|
|
58
|
+
await resolve(this.config.o11yHeaders),
|
|
59
|
+
),
|
|
60
|
+
body: {
|
|
61
|
+
audio:
|
|
62
|
+
audio instanceof Uint8Array
|
|
63
|
+
? convertUint8ArrayToBase64(audio)
|
|
64
|
+
: audio,
|
|
65
|
+
mediaType,
|
|
66
|
+
...(providerOptions && { providerOptions }),
|
|
67
|
+
},
|
|
68
|
+
successfulResponseHandler: createJsonResponseHandler(
|
|
69
|
+
gatewayTranscriptionResponseSchema,
|
|
70
|
+
),
|
|
71
|
+
failedResponseHandler: createJsonErrorResponseHandler({
|
|
72
|
+
errorSchema: z.any(),
|
|
73
|
+
errorToMessage: data => data,
|
|
74
|
+
}),
|
|
75
|
+
...(abortSignal && { abortSignal }),
|
|
76
|
+
fetch: this.config.fetch,
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
return {
|
|
80
|
+
text: responseBody.text,
|
|
81
|
+
segments: responseBody.segments ?? [],
|
|
82
|
+
language: responseBody.language ?? undefined,
|
|
83
|
+
durationInSeconds: responseBody.durationInSeconds ?? undefined,
|
|
84
|
+
warnings: (responseBody.warnings ?? []) as Array<SharedV4Warning>,
|
|
85
|
+
providerMetadata:
|
|
86
|
+
responseBody.providerMetadata as SharedV4ProviderMetadata,
|
|
87
|
+
response: {
|
|
88
|
+
timestamp: new Date(),
|
|
89
|
+
modelId: this.modelId,
|
|
90
|
+
headers: responseHeaders,
|
|
91
|
+
body: rawValue,
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
} catch (error) {
|
|
95
|
+
throw await asGatewayError(
|
|
96
|
+
error,
|
|
97
|
+
await parseAuthMethod(resolvedHeaders ?? {}),
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
private getUrl() {
|
|
103
|
+
return `${this.config.baseURL}/transcription-model`;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
private getModelConfigHeaders() {
|
|
107
|
+
return {
|
|
108
|
+
'ai-transcription-model-specification-version': '4',
|
|
109
|
+
'ai-model-id': this.modelId,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const providerMetadataEntrySchema = z.object({}).catchall(z.unknown());
|
|
115
|
+
|
|
116
|
+
const gatewayTranscriptionWarningSchema = z.discriminatedUnion('type', [
|
|
117
|
+
z.object({
|
|
118
|
+
type: z.literal('unsupported'),
|
|
119
|
+
feature: z.string(),
|
|
120
|
+
details: z.string().optional(),
|
|
121
|
+
}),
|
|
122
|
+
z.object({
|
|
123
|
+
type: z.literal('compatibility'),
|
|
124
|
+
feature: z.string(),
|
|
125
|
+
details: z.string().optional(),
|
|
126
|
+
}),
|
|
127
|
+
z.object({
|
|
128
|
+
type: z.literal('deprecated'),
|
|
129
|
+
setting: z.string(),
|
|
130
|
+
message: z.string(),
|
|
131
|
+
}),
|
|
132
|
+
z.object({
|
|
133
|
+
type: z.literal('other'),
|
|
134
|
+
message: z.string(),
|
|
135
|
+
}),
|
|
136
|
+
]);
|
|
137
|
+
|
|
138
|
+
const gatewayTranscriptionResponseSchema = z.object({
|
|
139
|
+
text: z.string(),
|
|
140
|
+
segments: z
|
|
141
|
+
.array(
|
|
142
|
+
z.object({
|
|
143
|
+
text: z.string(),
|
|
144
|
+
startSecond: z.number(),
|
|
145
|
+
endSecond: z.number(),
|
|
146
|
+
}),
|
|
147
|
+
)
|
|
148
|
+
.optional(),
|
|
149
|
+
language: z.string().nullish(),
|
|
150
|
+
durationInSeconds: z.number().nullish(),
|
|
151
|
+
warnings: z.array(gatewayTranscriptionWarningSchema).optional(),
|
|
152
|
+
providerMetadata: z
|
|
153
|
+
.record(z.string(), providerMetadataEntrySchema)
|
|
154
|
+
.optional(),
|
|
155
|
+
});
|
|
@@ -5,6 +5,8 @@ export type GatewayVideoModelId =
|
|
|
5
5
|
| 'alibaba/wan-v2.6-r2v'
|
|
6
6
|
| 'alibaba/wan-v2.6-r2v-flash'
|
|
7
7
|
| 'alibaba/wan-v2.6-t2v'
|
|
8
|
+
| 'bytedance/seedance-2.0'
|
|
9
|
+
| 'bytedance/seedance-2.0-fast'
|
|
8
10
|
| 'bytedance/seedance-v1.0-lite-i2v'
|
|
9
11
|
| 'bytedance/seedance-v1.0-lite-t2v'
|
|
10
12
|
| 'bytedance/seedance-v1.0-pro'
|
|
@@ -20,6 +22,8 @@ export type GatewayVideoModelId =
|
|
|
20
22
|
| 'klingai/kling-v2.6-motion-control'
|
|
21
23
|
| 'klingai/kling-v2.6-t2v'
|
|
22
24
|
| 'klingai/kling-v3.0-i2v'
|
|
25
|
+
| 'klingai/kling-v3.0-motion-control'
|
|
23
26
|
| 'klingai/kling-v3.0-t2v'
|
|
24
27
|
| 'xai/grok-imagine-video'
|
|
28
|
+
| 'xai/grok-imagine-video-1.5-preview'
|
|
25
29
|
| (string & {});
|