@ai-sdk/gateway 3.0.52 → 3.0.54
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 +12 -0
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +84 -15
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +85 -16
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/gateway-language-model-settings.ts +1 -0
- package/src/gateway-video-model.ts +97 -16
|
@@ -6,11 +6,12 @@ import type {
|
|
|
6
6
|
SharedV3ProviderMetadata,
|
|
7
7
|
SharedV3Warning,
|
|
8
8
|
} from '@ai-sdk/provider';
|
|
9
|
+
import { APICallError } from '@ai-sdk/provider';
|
|
9
10
|
import {
|
|
10
11
|
combineHeaders,
|
|
11
12
|
convertUint8ArrayToBase64,
|
|
12
|
-
createJsonResponseHandler,
|
|
13
13
|
createJsonErrorResponseHandler,
|
|
14
|
+
parseJsonEventStream,
|
|
14
15
|
postJsonToApi,
|
|
15
16
|
resolve,
|
|
16
17
|
type Resolvable,
|
|
@@ -61,17 +62,14 @@ export class GatewayVideoModel implements Experimental_VideoModelV3 {
|
|
|
61
62
|
}> {
|
|
62
63
|
const resolvedHeaders = await resolve(this.config.headers());
|
|
63
64
|
try {
|
|
64
|
-
const {
|
|
65
|
-
responseHeaders,
|
|
66
|
-
value: responseBody,
|
|
67
|
-
rawValue,
|
|
68
|
-
} = await postJsonToApi({
|
|
65
|
+
const { responseHeaders, value: responseBody } = await postJsonToApi({
|
|
69
66
|
url: this.getUrl(),
|
|
70
67
|
headers: combineHeaders(
|
|
71
68
|
resolvedHeaders,
|
|
72
69
|
headers ?? {},
|
|
73
70
|
this.getModelConfigHeaders(),
|
|
74
71
|
await resolve(this.config.o11yHeaders),
|
|
72
|
+
{ accept: 'text/event-stream' },
|
|
75
73
|
),
|
|
76
74
|
body: {
|
|
77
75
|
prompt,
|
|
@@ -84,9 +82,82 @@ export class GatewayVideoModel implements Experimental_VideoModelV3 {
|
|
|
84
82
|
...(providerOptions && { providerOptions }),
|
|
85
83
|
...(image && { image: maybeEncodeVideoFile(image) }),
|
|
86
84
|
},
|
|
87
|
-
successfulResponseHandler:
|
|
88
|
-
|
|
89
|
-
|
|
85
|
+
successfulResponseHandler: async ({
|
|
86
|
+
response,
|
|
87
|
+
url,
|
|
88
|
+
requestBodyValues,
|
|
89
|
+
}: {
|
|
90
|
+
url: string;
|
|
91
|
+
requestBodyValues: unknown;
|
|
92
|
+
response: Response;
|
|
93
|
+
}) => {
|
|
94
|
+
if (response.body == null) {
|
|
95
|
+
throw new APICallError({
|
|
96
|
+
message: 'SSE response body is empty',
|
|
97
|
+
url,
|
|
98
|
+
requestBodyValues,
|
|
99
|
+
statusCode: response.status,
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const eventStream = parseJsonEventStream({
|
|
104
|
+
stream: response.body,
|
|
105
|
+
schema: gatewayVideoEventSchema,
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
const reader = eventStream.getReader();
|
|
109
|
+
const { done, value: parseResult } = await reader.read();
|
|
110
|
+
reader.releaseLock();
|
|
111
|
+
|
|
112
|
+
if (done || !parseResult) {
|
|
113
|
+
throw new APICallError({
|
|
114
|
+
message: 'SSE stream ended without a data event',
|
|
115
|
+
url,
|
|
116
|
+
requestBodyValues,
|
|
117
|
+
statusCode: response.status,
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (!parseResult.success) {
|
|
122
|
+
throw new APICallError({
|
|
123
|
+
message: 'Failed to parse video SSE event',
|
|
124
|
+
cause: parseResult.error,
|
|
125
|
+
url,
|
|
126
|
+
requestBodyValues,
|
|
127
|
+
statusCode: response.status,
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const event = parseResult.value;
|
|
132
|
+
|
|
133
|
+
if (event.type === 'error') {
|
|
134
|
+
throw new APICallError({
|
|
135
|
+
message: event.message,
|
|
136
|
+
statusCode: event.statusCode,
|
|
137
|
+
url,
|
|
138
|
+
requestBodyValues,
|
|
139
|
+
responseHeaders: Object.fromEntries([...response.headers]),
|
|
140
|
+
responseBody: JSON.stringify(event),
|
|
141
|
+
data: {
|
|
142
|
+
error: {
|
|
143
|
+
message: event.message,
|
|
144
|
+
type: event.errorType,
|
|
145
|
+
param: event.param,
|
|
146
|
+
},
|
|
147
|
+
},
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// event.type === 'result'
|
|
152
|
+
return {
|
|
153
|
+
value: {
|
|
154
|
+
videos: event.videos,
|
|
155
|
+
warnings: event.warnings,
|
|
156
|
+
providerMetadata: event.providerMetadata,
|
|
157
|
+
},
|
|
158
|
+
responseHeaders: Object.fromEntries([...response.headers]),
|
|
159
|
+
};
|
|
160
|
+
},
|
|
90
161
|
failedResponseHandler: createJsonErrorResponseHandler({
|
|
91
162
|
errorSchema: z.any(),
|
|
92
163
|
errorToMessage: data => data,
|
|
@@ -169,10 +240,20 @@ const gatewayVideoWarningSchema = z.discriminatedUnion('type', [
|
|
|
169
240
|
}),
|
|
170
241
|
]);
|
|
171
242
|
|
|
172
|
-
const
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
.
|
|
177
|
-
|
|
178
|
-
|
|
243
|
+
const gatewayVideoEventSchema = z.discriminatedUnion('type', [
|
|
244
|
+
z.object({
|
|
245
|
+
type: z.literal('result'),
|
|
246
|
+
videos: z.array(gatewayVideoDataSchema),
|
|
247
|
+
warnings: z.array(gatewayVideoWarningSchema).optional(),
|
|
248
|
+
providerMetadata: z
|
|
249
|
+
.record(z.string(), providerMetadataEntrySchema)
|
|
250
|
+
.optional(),
|
|
251
|
+
}),
|
|
252
|
+
z.object({
|
|
253
|
+
type: z.literal('error'),
|
|
254
|
+
message: z.string(),
|
|
255
|
+
errorType: z.string(),
|
|
256
|
+
statusCode: z.number(),
|
|
257
|
+
param: z.unknown().nullable(),
|
|
258
|
+
}),
|
|
259
|
+
]);
|