@convex-dev/ai-sdk-provider 0.2.0-alpha.1 → 0.2.1
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/README.md +186 -4
- package/dist/commonjs/evaluation-model.d.ts +5 -0
- package/dist/commonjs/evaluation-model.d.ts.map +1 -0
- package/dist/commonjs/evaluation-model.js +96 -0
- package/dist/commonjs/evaluation-model.js.map +1 -0
- package/dist/commonjs/index.d.ts +37 -5
- package/dist/commonjs/index.d.ts.map +1 -1
- package/dist/commonjs/index.js +21 -7
- package/dist/commonjs/index.js.map +1 -1
- package/dist/commonjs/video-model.d.ts +33 -0
- package/dist/commonjs/video-model.d.ts.map +1 -0
- package/dist/commonjs/video-model.js +195 -0
- package/dist/commonjs/video-model.js.map +1 -0
- package/dist/commonjs/video-webhook.d.ts +14 -0
- package/dist/commonjs/video-webhook.d.ts.map +1 -0
- package/dist/commonjs/video-webhook.js +33 -0
- package/dist/commonjs/video-webhook.js.map +1 -0
- package/dist/esm/evaluation-model.d.ts +5 -0
- package/dist/esm/evaluation-model.d.ts.map +1 -0
- package/dist/esm/evaluation-model.js +93 -0
- package/dist/esm/evaluation-model.js.map +1 -0
- package/dist/esm/index.d.ts +9 -5
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +19 -7
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/video-model.d.ts +33 -0
- package/dist/esm/video-model.d.ts.map +1 -0
- package/dist/esm/video-model.js +192 -0
- package/dist/esm/video-model.js.map +1 -0
- package/dist/esm/video-webhook.d.ts +14 -0
- package/dist/esm/video-webhook.d.ts.map +1 -0
- package/dist/esm/video-webhook.js +30 -0
- package/dist/esm/video-webhook.js.map +1 -0
- package/package.json +9 -5
- package/src/evaluation-model.ts +120 -0
- package/src/index.ts +39 -13
- package/src/video-model.ts +262 -0
- package/src/video-webhook.ts +62 -0
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
experimental_generateVideo,
|
|
3
|
+
JSONValue,
|
|
4
|
+
ProviderMetadata,
|
|
5
|
+
} from "ai";
|
|
6
|
+
|
|
7
|
+
type VideoModel = Extract<
|
|
8
|
+
Parameters<typeof experimental_generateVideo>[0]["model"],
|
|
9
|
+
{ specificationVersion: "v4" }
|
|
10
|
+
>;
|
|
11
|
+
type VideoOptions = Parameters<NonNullable<VideoModel["doGenerate"]>>[0];
|
|
12
|
+
type VideoResult = Awaited<ReturnType<NonNullable<VideoModel["doGenerate"]>>>;
|
|
13
|
+
type VideoFile = NonNullable<VideoOptions["image"]>;
|
|
14
|
+
type StatusOptions = {
|
|
15
|
+
operation: JSONValue;
|
|
16
|
+
headers?: Record<string, string | undefined>;
|
|
17
|
+
abortSignal?: AbortSignal;
|
|
18
|
+
};
|
|
19
|
+
type StartResult = Omit<VideoResult, "videos"> & { operation: JSONValue };
|
|
20
|
+
type StatusResult = {
|
|
21
|
+
providerMetadata?: ProviderMetadata;
|
|
22
|
+
response: VideoResult["response"];
|
|
23
|
+
} & ({ status: "pending" | "completed" } | { status: "error"; error: string });
|
|
24
|
+
type GatewayVideoModel = Omit<
|
|
25
|
+
VideoModel,
|
|
26
|
+
"doStatus" | "handleWebhookOption"
|
|
27
|
+
> & {
|
|
28
|
+
doStart(
|
|
29
|
+
options: VideoOptions & { webhookUrl?: string },
|
|
30
|
+
): Promise<StartResult>;
|
|
31
|
+
getStatus(options: StatusOptions): Promise<StatusResult>;
|
|
32
|
+
download(options: StatusOptions): Promise<VideoResult>;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
function statusResult(
|
|
36
|
+
body: Record<string, unknown>,
|
|
37
|
+
response: VideoResult["response"],
|
|
38
|
+
providerMetadata?: ProviderMetadata,
|
|
39
|
+
): StatusResult {
|
|
40
|
+
if (body.status === "pending" || body.status === "completed") {
|
|
41
|
+
return { status: body.status, providerMetadata, response };
|
|
42
|
+
}
|
|
43
|
+
if (body.status === "error") {
|
|
44
|
+
return {
|
|
45
|
+
status: "error",
|
|
46
|
+
error:
|
|
47
|
+
typeof body.error === "string" ? body.error : "Video generation failed",
|
|
48
|
+
providerMetadata,
|
|
49
|
+
response,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
throw new Error("Invalid video status from the Convex AI gateway");
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function fileUrl(file: VideoFile): string {
|
|
56
|
+
if (file.type === "url") return file.url;
|
|
57
|
+
const base64 =
|
|
58
|
+
typeof file.data === "string"
|
|
59
|
+
? file.data
|
|
60
|
+
: btoa(
|
|
61
|
+
Array.from(file.data, (byte) => String.fromCharCode(byte)).join(""),
|
|
62
|
+
);
|
|
63
|
+
return `data:${file.mediaType};base64,${base64}`;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function prepare(options: VideoOptions, modelId: string) {
|
|
67
|
+
if (options.n !== 1)
|
|
68
|
+
throw new Error("The Convex video gateway accepts one video per call");
|
|
69
|
+
const warnings: VideoResult["warnings"] = [];
|
|
70
|
+
if (options.fps !== undefined)
|
|
71
|
+
warnings.push({ type: "unsupported", feature: "fps" });
|
|
72
|
+
const extra = options.providerOptions.convexGateway ?? {};
|
|
73
|
+
const allowedOptions = [
|
|
74
|
+
"resolution",
|
|
75
|
+
"generate_audio",
|
|
76
|
+
"frame_images",
|
|
77
|
+
"input_references",
|
|
78
|
+
];
|
|
79
|
+
for (const key of Object.keys(extra)) {
|
|
80
|
+
if (!allowedOptions.includes(key))
|
|
81
|
+
throw new Error(`Unsupported Convex video provider option: ${key}`);
|
|
82
|
+
}
|
|
83
|
+
return {
|
|
84
|
+
warnings,
|
|
85
|
+
body: {
|
|
86
|
+
...extra,
|
|
87
|
+
resolution:
|
|
88
|
+
options.resolution === undefined ? extra.resolution : undefined,
|
|
89
|
+
model: modelId,
|
|
90
|
+
prompt: options.prompt ?? "",
|
|
91
|
+
aspect_ratio: options.aspectRatio,
|
|
92
|
+
size: options.resolution,
|
|
93
|
+
duration: options.duration,
|
|
94
|
+
seed: options.seed,
|
|
95
|
+
...(options.generateAudio !== undefined && {
|
|
96
|
+
generate_audio: options.generateAudio,
|
|
97
|
+
}),
|
|
98
|
+
...(options.inputReferences && {
|
|
99
|
+
input_references: options.inputReferences.map((file) => {
|
|
100
|
+
const kind = file.mediaType?.startsWith("video/")
|
|
101
|
+
? "video"
|
|
102
|
+
: file.mediaType?.startsWith("audio/")
|
|
103
|
+
? "audio"
|
|
104
|
+
: "image";
|
|
105
|
+
return {
|
|
106
|
+
type: `${kind}_url`,
|
|
107
|
+
[`${kind}_url`]: { url: fileUrl(file) },
|
|
108
|
+
};
|
|
109
|
+
}),
|
|
110
|
+
}),
|
|
111
|
+
...(options.frameImages
|
|
112
|
+
? {
|
|
113
|
+
frame_images: options.frameImages.map((frame) => ({
|
|
114
|
+
type: "image_url",
|
|
115
|
+
image_url: { url: fileUrl(frame.image) },
|
|
116
|
+
frame_type: frame.frameType,
|
|
117
|
+
})),
|
|
118
|
+
}
|
|
119
|
+
: options.image && {
|
|
120
|
+
frame_images: [
|
|
121
|
+
{
|
|
122
|
+
type: "image_url",
|
|
123
|
+
image_url: { url: fileUrl(options.image) },
|
|
124
|
+
frame_type: "first_frame",
|
|
125
|
+
},
|
|
126
|
+
],
|
|
127
|
+
}),
|
|
128
|
+
},
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function videos(body: Record<string, unknown>): VideoResult["videos"] {
|
|
133
|
+
const data = body.data;
|
|
134
|
+
if (
|
|
135
|
+
!Array.isArray(data) ||
|
|
136
|
+
data.length !== 1 ||
|
|
137
|
+
typeof data[0]?.b64_json !== "string" ||
|
|
138
|
+
!data[0].b64_json ||
|
|
139
|
+
typeof data[0]?.media_type !== "string" ||
|
|
140
|
+
!data[0].media_type.startsWith("video/")
|
|
141
|
+
) {
|
|
142
|
+
throw new Error("Invalid video response from the Convex AI gateway");
|
|
143
|
+
}
|
|
144
|
+
return [
|
|
145
|
+
{ type: "base64", data: data[0].b64_json, mediaType: data[0].media_type },
|
|
146
|
+
];
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export function createVideoModel(
|
|
150
|
+
modelId: string,
|
|
151
|
+
baseURL: string,
|
|
152
|
+
fetch: typeof globalThis.fetch,
|
|
153
|
+
usageMetadata: (usage: unknown) => ProviderMetadata | undefined,
|
|
154
|
+
): GatewayVideoModel {
|
|
155
|
+
async function request(
|
|
156
|
+
path: string,
|
|
157
|
+
body: unknown,
|
|
158
|
+
options: {
|
|
159
|
+
headers?: Record<string, string | undefined>;
|
|
160
|
+
abortSignal?: AbortSignal;
|
|
161
|
+
},
|
|
162
|
+
) {
|
|
163
|
+
const headers = new Headers();
|
|
164
|
+
for (const [key, value] of Object.entries(options.headers ?? {})) {
|
|
165
|
+
if (value !== undefined) headers.set(key, value);
|
|
166
|
+
}
|
|
167
|
+
headers.set("Content-Type", "application/json");
|
|
168
|
+
const timestamp = new Date();
|
|
169
|
+
const response = await fetch(`${baseURL}/${path}`, {
|
|
170
|
+
method: "POST",
|
|
171
|
+
headers,
|
|
172
|
+
signal: options.abortSignal,
|
|
173
|
+
body: JSON.stringify(body),
|
|
174
|
+
});
|
|
175
|
+
const result: unknown = await response.json();
|
|
176
|
+
if (!result || typeof result !== "object" || Array.isArray(result))
|
|
177
|
+
throw new Error("Invalid video response from the Convex AI gateway");
|
|
178
|
+
const parsed = result as Record<string, unknown>;
|
|
179
|
+
// Async job failures are returned as status results for the caller to handle.
|
|
180
|
+
if (!response.ok || (parsed.error && parsed.status !== "error")) {
|
|
181
|
+
const error = parsed.error as { message?: string } | undefined;
|
|
182
|
+
throw new Error(
|
|
183
|
+
error?.message ?? `Video generation failed (${response.status})`,
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
return {
|
|
187
|
+
body: parsed,
|
|
188
|
+
response: {
|
|
189
|
+
timestamp,
|
|
190
|
+
modelId,
|
|
191
|
+
headers: Object.fromEntries(response.headers),
|
|
192
|
+
},
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
function operationRequest(path: string, options: StatusOptions) {
|
|
196
|
+
if (typeof options.operation !== "string" || !options.operation) {
|
|
197
|
+
throw new Error("Invalid Convex video operation");
|
|
198
|
+
}
|
|
199
|
+
return request(`videos/${path}`, { operation: options.operation }, options);
|
|
200
|
+
}
|
|
201
|
+
return {
|
|
202
|
+
specificationVersion: "v4",
|
|
203
|
+
provider: "convexGateway",
|
|
204
|
+
modelId,
|
|
205
|
+
maxVideosPerCall: 1,
|
|
206
|
+
async doGenerate(options) {
|
|
207
|
+
const prepared = prepare(options, modelId);
|
|
208
|
+
const { body, response } = await request(
|
|
209
|
+
"videos/generations",
|
|
210
|
+
prepared.body,
|
|
211
|
+
options,
|
|
212
|
+
);
|
|
213
|
+
return {
|
|
214
|
+
videos: videos(body),
|
|
215
|
+
warnings: prepared.warnings,
|
|
216
|
+
providerMetadata: usageMetadata(body.usage),
|
|
217
|
+
response,
|
|
218
|
+
};
|
|
219
|
+
},
|
|
220
|
+
async doStart(options) {
|
|
221
|
+
const prepared = prepare(options, modelId);
|
|
222
|
+
const { body, response } = await request(
|
|
223
|
+
"videos",
|
|
224
|
+
{ ...prepared.body, webhook_url: options.webhookUrl },
|
|
225
|
+
options,
|
|
226
|
+
);
|
|
227
|
+
if (
|
|
228
|
+
typeof body.operation !== "string" ||
|
|
229
|
+
!body.operation ||
|
|
230
|
+
typeof body.id !== "string" ||
|
|
231
|
+
(options.webhookUrl && typeof body.webhook_secret !== "string")
|
|
232
|
+
)
|
|
233
|
+
throw new Error("Invalid video operation from the Convex AI gateway");
|
|
234
|
+
return {
|
|
235
|
+
operation: body.operation,
|
|
236
|
+
warnings: prepared.warnings,
|
|
237
|
+
providerMetadata: {
|
|
238
|
+
convexGateway: {
|
|
239
|
+
inferenceId: body.id,
|
|
240
|
+
...(typeof body.webhook_secret === "string" && {
|
|
241
|
+
webhookSecret: body.webhook_secret,
|
|
242
|
+
}),
|
|
243
|
+
},
|
|
244
|
+
},
|
|
245
|
+
response,
|
|
246
|
+
};
|
|
247
|
+
},
|
|
248
|
+
async getStatus(options) {
|
|
249
|
+
const { body, response } = await operationRequest("status", options);
|
|
250
|
+
return statusResult(body, response, usageMetadata(body.usage));
|
|
251
|
+
},
|
|
252
|
+
async download(options) {
|
|
253
|
+
const { body, response } = await operationRequest("download", options);
|
|
254
|
+
return {
|
|
255
|
+
videos: videos(body),
|
|
256
|
+
warnings: [],
|
|
257
|
+
providerMetadata: usageMetadata(body.usage),
|
|
258
|
+
response,
|
|
259
|
+
};
|
|
260
|
+
},
|
|
261
|
+
};
|
|
262
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
export type VideoWebhookEvent = {
|
|
2
|
+
id: string;
|
|
3
|
+
type: string;
|
|
4
|
+
status: "completed" | "failed" | "cancelled" | "expired";
|
|
5
|
+
operation: string;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
/** Pass the unmodified request body: the signature covers its exact bytes. */
|
|
9
|
+
export async function verifyVideoWebhook({
|
|
10
|
+
body,
|
|
11
|
+
signature,
|
|
12
|
+
secret,
|
|
13
|
+
now = Date.now(),
|
|
14
|
+
}: {
|
|
15
|
+
body: string;
|
|
16
|
+
signature: string | null;
|
|
17
|
+
secret: string;
|
|
18
|
+
now?: number;
|
|
19
|
+
}): Promise<VideoWebhookEvent> {
|
|
20
|
+
const match = signature?.match(/^t=(\d+),v1=([0-9a-f]{64})$/);
|
|
21
|
+
const timestamp = Number(match?.[1]);
|
|
22
|
+
if (
|
|
23
|
+
!match ||
|
|
24
|
+
!secret ||
|
|
25
|
+
!Number.isSafeInteger(timestamp) ||
|
|
26
|
+
!Number.isFinite(now) ||
|
|
27
|
+
Math.abs(now / 1000 - timestamp) > 300
|
|
28
|
+
) {
|
|
29
|
+
throw new Error("Invalid or expired video webhook signature");
|
|
30
|
+
}
|
|
31
|
+
const encoder = new TextEncoder();
|
|
32
|
+
const key = await crypto.subtle.importKey(
|
|
33
|
+
"raw",
|
|
34
|
+
encoder.encode(secret),
|
|
35
|
+
{ name: "HMAC", hash: "SHA-256" },
|
|
36
|
+
false,
|
|
37
|
+
["verify"],
|
|
38
|
+
);
|
|
39
|
+
const mac = Uint8Array.from(match[2].match(/../g)!, (byte) =>
|
|
40
|
+
parseInt(byte, 16),
|
|
41
|
+
);
|
|
42
|
+
const valid = await crypto.subtle.verify(
|
|
43
|
+
"HMAC",
|
|
44
|
+
key,
|
|
45
|
+
mac,
|
|
46
|
+
encoder.encode(`${match[1]},${body}`),
|
|
47
|
+
);
|
|
48
|
+
if (!valid) throw new Error("Invalid video webhook signature");
|
|
49
|
+
const event = JSON.parse(body) as VideoWebhookEvent | null;
|
|
50
|
+
if (
|
|
51
|
+
!event ||
|
|
52
|
+
typeof event.id !== "string" ||
|
|
53
|
+
!event.id ||
|
|
54
|
+
typeof event.operation !== "string" ||
|
|
55
|
+
!event.operation ||
|
|
56
|
+
!["completed", "failed", "cancelled", "expired"].includes(event.status) ||
|
|
57
|
+
event.type !== `video.generation.${event.status}`
|
|
58
|
+
) {
|
|
59
|
+
throw new Error("Invalid video webhook payload");
|
|
60
|
+
}
|
|
61
|
+
return event;
|
|
62
|
+
}
|