@convex-dev/ai-sdk-provider 0.2.1 → 0.3.0
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 +63 -9
- package/dist/commonjs/index.d.ts +11 -5
- package/dist/commonjs/index.d.ts.map +1 -1
- package/dist/commonjs/index.js +65 -14
- package/dist/commonjs/index.js.map +1 -1
- package/dist/commonjs/transcription-model.d.ts +11 -0
- package/dist/commonjs/transcription-model.d.ts.map +1 -0
- package/dist/commonjs/transcription-model.js +102 -0
- package/dist/commonjs/transcription-model.js.map +1 -0
- package/dist/esm/index.d.ts +10 -4
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +65 -14
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/transcription-model.d.ts +11 -0
- package/dist/esm/transcription-model.d.ts.map +1 -0
- package/dist/esm/transcription-model.js +99 -0
- package/dist/esm/transcription-model.js.map +1 -0
- package/package.json +2 -1
- package/src/index.ts +99 -13
- package/src/transcription-model.ts +133 -0
package/README.md
CHANGED
|
@@ -36,6 +36,26 @@ export const embed = action({
|
|
|
36
36
|
Embedding batches larger than the gateway's 512-input limit are split into
|
|
37
37
|
multiple requests by the AI SDK.
|
|
38
38
|
|
|
39
|
+
## Optional ZDR routing
|
|
40
|
+
|
|
41
|
+
Pass `{ zdr: true }` to use inference endpoints with a zero-data-retention
|
|
42
|
+
policy:
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
const { text } = await generateText({
|
|
46
|
+
model: convexGateway("openai/gpt-4o-mini", { zdr: true }),
|
|
47
|
+
prompt: "Summarize this document",
|
|
48
|
+
});
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Also supported by `messages`, `responses`, `embeddingModel`, and
|
|
52
|
+
`evaluationModel`. The setting applies to that model instance and overrides
|
|
53
|
+
`provider.zdr` from other SDK options. Omit it to use the default policy;
|
|
54
|
+
`false` cannot disable a stricter gateway policy.
|
|
55
|
+
|
|
56
|
+
Not every model has a ZDR endpoint. See
|
|
57
|
+
[routing requirements and retention limits](https://docs.convex.dev/ai-gateway/api#provider-preferences).
|
|
58
|
+
|
|
39
59
|
## Choose a model interface
|
|
40
60
|
|
|
41
61
|
For text generation, start with `convexGateway(model)`. It works across model
|
|
@@ -91,10 +111,11 @@ gateway response, including provider-specific fields such as `confidence`, is
|
|
|
91
111
|
available in `decision.response.body`.
|
|
92
112
|
|
|
93
113
|
`getServiceToken("ai-gateway")` supplies a short-lived deployment JWT. The
|
|
94
|
-
action runtime caches and refreshes the credential as needed, so
|
|
95
|
-
|
|
114
|
+
action runtime caches and refreshes the credential as needed, so `convexGateway`
|
|
115
|
+
calls it on every request.
|
|
96
116
|
|
|
97
|
-
Requires Convex 1.45 or later
|
|
117
|
+
Requires Convex 1.45 or later and AI SDK 7.0.105 or later. The provider works in
|
|
118
|
+
the default runtime and in Node.js actions, which need Node.js 22 or later.
|
|
98
119
|
|
|
99
120
|
## Generate images
|
|
100
121
|
|
|
@@ -110,6 +131,38 @@ const { images } = await generateImage({
|
|
|
110
131
|
});
|
|
111
132
|
```
|
|
112
133
|
|
|
134
|
+
Image request costs are available in each entry of `result.calls`, at
|
|
135
|
+
`providerMetadata?.convexGateway?.cost`, in US dollars. Each call may generate
|
|
136
|
+
multiple images; the cost is for the call, not each image.
|
|
137
|
+
|
|
138
|
+
## Transcribe and generate speech
|
|
139
|
+
|
|
140
|
+
Speech-to-text and text-to-speech are in alpha.
|
|
141
|
+
|
|
142
|
+
```ts
|
|
143
|
+
import {
|
|
144
|
+
experimental_generateSpeech as generateSpeech,
|
|
145
|
+
experimental_transcribe as transcribe,
|
|
146
|
+
} from "ai";
|
|
147
|
+
import { convexGateway } from "@convex-dev/ai-sdk-provider";
|
|
148
|
+
|
|
149
|
+
const { text, providerMetadata } = await transcribe({
|
|
150
|
+
model: convexGateway.transcriptionModel("openai/whisper-large-v3-turbo"),
|
|
151
|
+
audio: recordingBytes,
|
|
152
|
+
});
|
|
153
|
+
const cost = providerMetadata.convexGateway?.cost;
|
|
154
|
+
|
|
155
|
+
const { audio } = await generateSpeech({
|
|
156
|
+
model: convexGateway.speechModel("hexgrad/kokoro-82m"),
|
|
157
|
+
text: "Your order has shipped.",
|
|
158
|
+
voice: "af_heart",
|
|
159
|
+
});
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
Speech results have no `cost` in `providerMetadata`, because the cost is known
|
|
163
|
+
only after the audio is sent. Convex still bills for speech. The charge appears
|
|
164
|
+
on the Convex usage page.
|
|
165
|
+
|
|
113
166
|
## Generate videos
|
|
114
167
|
|
|
115
168
|
Video generation is in alpha. APIs may change, and completion callbacks are best
|
|
@@ -131,10 +184,11 @@ const bytes = video.uint8Array;
|
|
|
131
184
|
const cost = providerMetadata?.convexGateway?.cost;
|
|
132
185
|
```
|
|
133
186
|
|
|
134
|
-
The call waits for generation and download, with a
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
187
|
+
The call waits for generation and download, with a 64 MiB limit per video. It
|
|
188
|
+
fails if generation takes longer than about five minutes; use async videos for
|
|
189
|
+
longer jobs. Store the returned bytes in Convex file storage. The AI SDK splits
|
|
190
|
+
`n > 1` into separate requests. Cancelling a request does not cancel the
|
|
191
|
+
upstream job and can still incur a charge.
|
|
138
192
|
|
|
139
193
|
An image in the prompt becomes the first frame. Use `frameImages` for explicit
|
|
140
194
|
frames, `inputReferences` for image/audio/video references, and `generateAudio`
|
|
@@ -147,8 +201,8 @@ take precedence. Supported values depend on the
|
|
|
147
201
|
|
|
148
202
|
## Async videos
|
|
149
203
|
|
|
150
|
-
With AI SDK 7.0.
|
|
151
|
-
waiting for the video. See the
|
|
204
|
+
With AI SDK 7.0.105 or later, use `experimental_startVideo` to submit a job
|
|
205
|
+
without waiting for the video. See the
|
|
152
206
|
[async availability requirements](https://docs.convex.dev/ai-gateway/images-and-videos#async-videos)
|
|
153
207
|
before using these routes.
|
|
154
208
|
|
package/dist/commonjs/index.d.ts
CHANGED
|
@@ -5,6 +5,10 @@ type ChatModel = ReturnType<Provider>;
|
|
|
5
5
|
type EmbeddingModel = ReturnType<typeof wrapEmbeddingModel>;
|
|
6
6
|
type ImageModel = ReturnType<Provider["imageModel"]>;
|
|
7
7
|
type GatewayLanguageModel = ReturnType<typeof wrapLanguageModel>;
|
|
8
|
+
export interface GatewayModelOptions {
|
|
9
|
+
/** Require an inference endpoint with a zero-data-retention policy. */
|
|
10
|
+
zdr?: boolean;
|
|
11
|
+
}
|
|
8
12
|
/**
|
|
9
13
|
* The recommended model interface for text generation through the Convex AI gateway.
|
|
10
14
|
* Use `messages` or `responses` only for endpoint-specific features.
|
|
@@ -12,13 +16,15 @@ type GatewayLanguageModel = ReturnType<typeof wrapLanguageModel>;
|
|
|
12
16
|
* `getServiceToken` caches and refreshes credentials within the current action,
|
|
13
17
|
* so calling this more than once in the same action is fine.
|
|
14
18
|
*/
|
|
15
|
-
export declare function convexGateway(modelId: string): ChatModel;
|
|
19
|
+
export declare function convexGateway(modelId: string, options?: GatewayModelOptions): ChatModel;
|
|
16
20
|
export declare namespace convexGateway {
|
|
17
|
-
var messages: (modelId: string) => GatewayLanguageModel;
|
|
18
|
-
var responses: (modelId: string) => GatewayLanguageModel;
|
|
19
|
-
var evaluationModel: (modelId: string) => import(".pnpm/@ai-sdk+provider@4.0.17/node_modules/@ai-sdk/provider", { with: { "resolution-mode": "import" } }).Experimental_EvaluationModelV4;
|
|
20
|
-
var embeddingModel: (modelId: string) => EmbeddingModel;
|
|
21
|
+
var messages: (modelId: string, options?: GatewayModelOptions) => GatewayLanguageModel;
|
|
22
|
+
var responses: (modelId: string, options?: GatewayModelOptions) => GatewayLanguageModel;
|
|
23
|
+
var evaluationModel: (modelId: string, options?: GatewayModelOptions) => import(".pnpm/@ai-sdk+provider@4.0.17/node_modules/@ai-sdk/provider", { with: { "resolution-mode": "import" } }).Experimental_EvaluationModelV4;
|
|
24
|
+
var embeddingModel: (modelId: string, options?: GatewayModelOptions) => EmbeddingModel;
|
|
21
25
|
var imageModel: (modelId: string) => ImageModel;
|
|
26
|
+
var transcriptionModel: (modelId: string) => import(".pnpm/@ai-sdk+provider@4.0.17/node_modules/@ai-sdk/provider", { with: { "resolution-mode": "import" } }).TranscriptionModelV4;
|
|
27
|
+
var speechModel: (modelId: string) => import(".pnpm/@ai-sdk+provider@4.0.7/node_modules/@ai-sdk/provider", { with: { "resolution-mode": "import" } }).SpeechModelV4;
|
|
22
28
|
var videoModel: (modelId: string) => Omit<import(".pnpm/@ai-sdk+provider@4.0.17/node_modules/@ai-sdk/provider", { with: { "resolution-mode": "import" } }).Experimental_VideoModelV4, "doStatus" | "handleWebhookOption"> & {
|
|
23
29
|
doStart(options: import(".pnpm/@ai-sdk+provider@4.0.17/node_modules/@ai-sdk/provider", { with: { "resolution-mode": "import" } }).Experimental_VideoModelV4CallOptions & {
|
|
24
30
|
webhookUrl?: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,sBAAsB,EAEvB,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAEL,kBAAkB,EAClB,iBAAiB,EAClB,MAAM,IAAI,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,sBAAsB,EAEvB,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAEL,kBAAkB,EAClB,iBAAiB,EAClB,MAAM,IAAI,CAAC;AAMZ,KAAK,QAAQ,GAAG,UAAU,CAAC,OAAO,sBAAsB,CAAC,CAAC;AAC1D,KAAK,SAAS,GAAG,UAAU,CAAC,QAAQ,CAAC,CAAC;AACtC,KAAK,cAAc,GAAG,UAAU,CAAC,OAAO,kBAAkB,CAAC,CAAC;AAI5D,KAAK,UAAU,GAAG,UAAU,CAAC,QAAQ,CAAC,YAAY,CAAC,CAAC,CAAC;AAErD,KAAK,oBAAoB,GAAG,UAAU,CAAC,OAAO,iBAAiB,CAAC,CAAC;AAEjE,MAAM,WAAW,mBAAmB;IAClC,uEAAuE;IACvE,GAAG,CAAC,EAAE,OAAO,CAAC;CACf;AA+GD;;;;;;GAMG;AACH,wBAAgB,aAAa,CAC3B,OAAO,EAAE,MAAM,EACf,OAAO,CAAC,EAAE,mBAAmB,GAC5B,SAAS,CAIX;yBAPe,aAAa;4BAUlB,MAAM,YACL,mBAAmB,KAC5B,oBAAoB;6BAcZ,MAAM,YACL,mBAAmB,KAC5B,oBAAoB;mCAkBZ,MAAM,YACL,mBAAmB;kCAWpB,MAAM,YACL,mBAAmB,KAC5B,cAAc;8BAa6B,MAAM,KAAG,UAAU;sCAsCX,MAAM;+BAab,MAAM;8BAWP,MAAM;;sBAlQtB,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2Q/B,OAAO,EAAE,kBAAkB,EAAE,KAAK,iBAAiB,EAAE,MAAM,oBAAoB,CAAC"}
|
package/dist/commonjs/index.js
CHANGED
|
@@ -8,6 +8,7 @@ const openai_compatible_1 = require("@ai-sdk/openai-compatible");
|
|
|
8
8
|
const ai_1 = require("ai");
|
|
9
9
|
const server_1 = require("convex/server");
|
|
10
10
|
const evaluation_model_js_1 = require("./evaluation-model.js");
|
|
11
|
+
const transcription_model_js_1 = require("./transcription-model.js");
|
|
11
12
|
const video_model_js_1 = require("./video-model.js");
|
|
12
13
|
const maxEmbeddingsPerCall = 512;
|
|
13
14
|
// Official providers require a credential before gatewayFetch replaces it with a deployment JWT.
|
|
@@ -57,11 +58,11 @@ const costMetadataExtractor = {
|
|
|
57
58
|
};
|
|
58
59
|
},
|
|
59
60
|
};
|
|
60
|
-
function createGatewayProvider() {
|
|
61
|
+
function createGatewayProvider(fetch = gatewayFetch) {
|
|
61
62
|
return (0, openai_compatible_1.createOpenAICompatible)({
|
|
62
63
|
name: "convexGateway",
|
|
63
64
|
baseURL: gatewayBaseURL(),
|
|
64
|
-
fetch
|
|
65
|
+
fetch,
|
|
65
66
|
metadataExtractor: costMetadataExtractor,
|
|
66
67
|
supportsStructuredOutputs: true,
|
|
67
68
|
supportedUrls: () => ({ "image/*": [/^https?:\/\/.*$/] }),
|
|
@@ -75,9 +76,12 @@ function sdkModelId(gatewayModelId, provider) {
|
|
|
75
76
|
// OpenRouter uses dots in Anthropic versions; the Anthropic SDK's capability lookup uses hyphens.
|
|
76
77
|
return provider === "anthropic" ? modelId.replaceAll(".", "-") : modelId;
|
|
77
78
|
}
|
|
78
|
-
function gatewayModelFetch(gatewayModelId, input, init) {
|
|
79
|
+
function gatewayModelFetch(gatewayModelId, input, init, options) {
|
|
79
80
|
const body = JSON.parse(init?.body);
|
|
80
81
|
body.model = gatewayModelId;
|
|
82
|
+
if (options?.zdr !== undefined) {
|
|
83
|
+
body.provider = { ...body.provider, zdr: options.zdr };
|
|
84
|
+
}
|
|
81
85
|
return gatewayFetch(input, { ...init, body: JSON.stringify(body) });
|
|
82
86
|
}
|
|
83
87
|
function gatewayLanguageModel(modelId, model, middleware) {
|
|
@@ -95,36 +99,36 @@ function gatewayLanguageModel(modelId, model, middleware) {
|
|
|
95
99
|
* `getServiceToken` caches and refreshes credentials within the current action,
|
|
96
100
|
* so calling this more than once in the same action is fine.
|
|
97
101
|
*/
|
|
98
|
-
function convexGateway(modelId) {
|
|
99
|
-
return createGatewayProvider()(modelId);
|
|
102
|
+
function convexGateway(modelId, options) {
|
|
103
|
+
return createGatewayProvider((input, init) => gatewayModelFetch(modelId, input, init, options))(modelId);
|
|
100
104
|
}
|
|
101
|
-
convexGateway.messages = function (modelId) {
|
|
105
|
+
convexGateway.messages = function (modelId, options) {
|
|
102
106
|
const provider = (0, anthropic_1.createAnthropic)({
|
|
103
107
|
name: "convexGateway.messages",
|
|
104
108
|
baseURL: gatewayBaseURL(),
|
|
105
109
|
authToken: placeholderCredential,
|
|
106
|
-
fetch: (input, init) => gatewayModelFetch(modelId, input, init),
|
|
110
|
+
fetch: (input, init) => gatewayModelFetch(modelId, input, init, options),
|
|
107
111
|
});
|
|
108
112
|
return gatewayLanguageModel(modelId, provider.messages(sdkModelId(modelId, "anthropic")));
|
|
109
113
|
};
|
|
110
|
-
convexGateway.responses = function (modelId) {
|
|
114
|
+
convexGateway.responses = function (modelId, options) {
|
|
111
115
|
const provider = (0, openai_1.createOpenAI)({
|
|
112
116
|
name: "convexGateway.responses",
|
|
113
117
|
baseURL: gatewayBaseURL(),
|
|
114
118
|
apiKey: placeholderCredential,
|
|
115
|
-
fetch: (input, init) => gatewayModelFetch(modelId, input, init),
|
|
119
|
+
fetch: (input, init) => gatewayModelFetch(modelId, input, init, options),
|
|
116
120
|
});
|
|
117
121
|
return gatewayLanguageModel(modelId, provider.responses(sdkModelId(modelId, "openai")), (0, ai_1.defaultSettingsMiddleware)({
|
|
118
122
|
settings: { providerOptions: { openai: { store: false } } },
|
|
119
123
|
}));
|
|
120
124
|
};
|
|
121
125
|
/** Evaluate typed questions through the alpha Decisions API. */
|
|
122
|
-
convexGateway.evaluationModel = function (modelId) {
|
|
123
|
-
return (0, evaluation_model_js_1.createEvaluationModel)(modelId, gatewayBaseURL("alpha"),
|
|
126
|
+
convexGateway.evaluationModel = function (modelId, options) {
|
|
127
|
+
return (0, evaluation_model_js_1.createEvaluationModel)(modelId, gatewayBaseURL("alpha"), (input, init) => gatewayModelFetch(modelId, input, init, options), convexGatewayUsageMetadata);
|
|
124
128
|
};
|
|
125
|
-
convexGateway.embeddingModel = function (modelId) {
|
|
129
|
+
convexGateway.embeddingModel = function (modelId, options) {
|
|
126
130
|
return (0, ai_1.wrapEmbeddingModel)({
|
|
127
|
-
model: createGatewayProvider().embeddingModel(modelId),
|
|
131
|
+
model: createGatewayProvider((input, init) => gatewayModelFetch(modelId, input, init, options)).embeddingModel(modelId),
|
|
128
132
|
middleware: {
|
|
129
133
|
specificationVersion: "v4",
|
|
130
134
|
overrideMaxEmbeddingsPerCall: () => maxEmbeddingsPerCall,
|
|
@@ -133,7 +137,54 @@ convexGateway.embeddingModel = function (modelId) {
|
|
|
133
137
|
};
|
|
134
138
|
/** Image model for the AI SDK's `generateImage`. */
|
|
135
139
|
convexGateway.imageModel = function (modelId) {
|
|
136
|
-
|
|
140
|
+
const { specificationVersion, provider, maxImagesPerCall } = createGatewayProvider().imageModel(modelId);
|
|
141
|
+
return {
|
|
142
|
+
specificationVersion,
|
|
143
|
+
provider,
|
|
144
|
+
modelId,
|
|
145
|
+
maxImagesPerCall,
|
|
146
|
+
async doGenerate(options) {
|
|
147
|
+
if (options.files?.length || options.mask) {
|
|
148
|
+
throw new Error("Convex AI Gateway does not support image editing. Use a text prompt without input images or a mask.");
|
|
149
|
+
}
|
|
150
|
+
// The SDK's image adapter discards `usage` and has no metadata hook, so
|
|
151
|
+
// each call reads it from its own response through a per-call fetch.
|
|
152
|
+
let usage;
|
|
153
|
+
const model = createGatewayProvider(async (input, init) => {
|
|
154
|
+
const response = await gatewayFetch(input, init);
|
|
155
|
+
if (response.ok) {
|
|
156
|
+
usage = (await response.clone().json()).usage;
|
|
157
|
+
}
|
|
158
|
+
return response;
|
|
159
|
+
}).imageModel(modelId);
|
|
160
|
+
const result = await model.doGenerate(options);
|
|
161
|
+
const metadata = convexGatewayUsageMetadata(usage)?.convexGateway;
|
|
162
|
+
return {
|
|
163
|
+
...result,
|
|
164
|
+
...(metadata && {
|
|
165
|
+
// The SDK's image metadata type requires a per-image `images` array.
|
|
166
|
+
providerMetadata: { convexGateway: { ...metadata, images: [] } },
|
|
167
|
+
}),
|
|
168
|
+
};
|
|
169
|
+
},
|
|
170
|
+
};
|
|
171
|
+
};
|
|
172
|
+
/** Speech-to-text for the AI SDK's `experimental_transcribe`. */
|
|
173
|
+
convexGateway.transcriptionModel = function (modelId) {
|
|
174
|
+
return (0, transcription_model_js_1.createTranscriptionModel)(modelId, gatewayBaseURL(), gatewayFetch, convexGatewayUsageMetadata);
|
|
175
|
+
};
|
|
176
|
+
/**
|
|
177
|
+
* Text-to-speech for the AI SDK's `experimental_generateSpeech`. The gateway
|
|
178
|
+
* reports speech cost after the call, so results carry no cost metadata.
|
|
179
|
+
*/
|
|
180
|
+
convexGateway.speechModel = function (modelId) {
|
|
181
|
+
const provider = (0, openai_1.createOpenAI)({
|
|
182
|
+
name: "convexGateway.speech",
|
|
183
|
+
baseURL: gatewayBaseURL(),
|
|
184
|
+
apiKey: placeholderCredential,
|
|
185
|
+
fetch: (input, init) => gatewayModelFetch(modelId, input, init),
|
|
186
|
+
});
|
|
187
|
+
return provider.speech(modelId);
|
|
137
188
|
};
|
|
138
189
|
/** Video generation for the AI SDK's `experimental_generateVideo`. */
|
|
139
190
|
convexGateway.videoModel = function (modelId) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAmJA,sCAOC;AA1JD,iDAAoD;AACpD,2CAA8C;AAC9C,iEAGmC;AACnC,2BAIY;AACZ,0CAAgD;AAChD,+DAA8D;AAC9D,qEAAoE;AACpE,qDAAoD;AAiBpD,MAAM,oBAAoB,GAAG,GAAG,CAAC;AACjC,iGAAiG;AACjG,MAAM,qBAAqB,GAAG,gBAAgB,CAAC;AAE/C;;;GAGG;AACH,MAAM,qBAAqB,GAAG,+BAA+B,CAAC;AAE9D,SAAS,cAAc,CAAC,UAA0B,IAAI;IACpD,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,+BAA+B,IAAI,qBAAqB,IAAI,OAAO,EAAE,CAAC;AAC9F,CAAC;AAED,KAAK,UAAU,YAAY,CACzB,KAAwB,EACxB,IAAkB;IAElB,MAAM,KAAK,GAAG,MAAM,IAAA,wBAAe,EAAC,YAAY,CAAC,CAAC;IAClD,MAAM,OAAO,GAAG,IAAI,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;IAC3C,yEAAyE;IACzE,OAAO,CAAC,GAAG,CAAC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QAC5B,IAAI,EAAE,sBAAsB;QAC5B,OAAO,EAAE,cAAc,EAAE;QACzB,MAAM,EAAE,qBAAqB;QAC7B,KAAK,EAAE,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE,CAAC,iBAAiB,CAAC,OAAO,EAAE,KAAK,EAAE,IAAI,CAAC;KAChE,CAAC,CAAC;IACH,OAAO,QAAQ,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;AAClC,CAAC,CAAC;AAEF,sEAAsE;AACtE,aAAa,CAAC,UAAU,GAAG,UAAU,OAAe;IAClD,OAAO,IAAA,iCAAgB,EACrB,OAAO,EACP,cAAc,EAAE,EAChB,YAAY,EACZ,0BAA0B,CAC3B,CAAC;AACJ,CAAC,CAAC;AAEF,uDAAgF;AAAvE,sHAAA,kBAAkB,OAAA"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type experimental_transcribe as transcribe, type ProviderMetadata } from "ai";
|
|
2
|
+
type TranscriptionModel = Extract<Parameters<typeof transcribe>[0]["model"], {
|
|
3
|
+
specificationVersion: "v4";
|
|
4
|
+
}>;
|
|
5
|
+
/**
|
|
6
|
+
* The gateway accepts JSON only, so this model sends base64 audio instead of
|
|
7
|
+
* the multipart upload `@ai-sdk/openai` uses.
|
|
8
|
+
*/
|
|
9
|
+
export declare function createTranscriptionModel(modelId: string, baseURL: string, fetch: typeof globalThis.fetch, usageMetadata: (usage: unknown) => ProviderMetadata | undefined): TranscriptionModel;
|
|
10
|
+
export {};
|
|
11
|
+
//# sourceMappingURL=transcription-model.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transcription-model.d.ts","sourceRoot":"","sources":["../../src/transcription-model.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,uBAAuB,IAAI,UAAU,EAC1C,KAAK,gBAAgB,EACtB,MAAM,IAAI,CAAC;AAEZ,KAAK,kBAAkB,GAAG,OAAO,CAC/B,UAAU,CAAC,OAAO,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,EACzC;IAAE,oBAAoB,EAAE,IAAI,CAAA;CAAE,CAC/B,CAAC;AAmCF;;;GAGG;AACH,wBAAgB,wBAAwB,CACtC,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,MAAM,EACf,KAAK,EAAE,OAAO,UAAU,CAAC,KAAK,EAC9B,aAAa,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,gBAAgB,GAAG,SAAS,GAC9D,kBAAkB,CA+EpB"}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createTranscriptionModel = createTranscriptionModel;
|
|
4
|
+
const ai_1 = require("ai");
|
|
5
|
+
// OpenRouter names formats by extension, not media type.
|
|
6
|
+
const audioFormats = {
|
|
7
|
+
"audio/mpeg": "mp3",
|
|
8
|
+
"audio/mp3": "mp3",
|
|
9
|
+
"audio/mpga": "mp3",
|
|
10
|
+
"audio/x-mp3": "mp3",
|
|
11
|
+
"audio/wav": "wav",
|
|
12
|
+
"audio/wave": "wav",
|
|
13
|
+
"audio/x-wav": "wav",
|
|
14
|
+
"audio/flac": "flac",
|
|
15
|
+
"audio/x-flac": "flac",
|
|
16
|
+
"audio/mp4": "m4a",
|
|
17
|
+
"audio/m4a": "m4a",
|
|
18
|
+
"audio/x-m4a": "m4a",
|
|
19
|
+
"audio/ogg": "ogg",
|
|
20
|
+
"audio/webm": "webm",
|
|
21
|
+
"audio/aac": "aac",
|
|
22
|
+
};
|
|
23
|
+
function base64(audio) {
|
|
24
|
+
if (typeof audio === "string")
|
|
25
|
+
return audio;
|
|
26
|
+
// Chunked so large recordings stay under the engine's argument limit.
|
|
27
|
+
let binary = "";
|
|
28
|
+
for (let i = 0; i < audio.length; i += 0x8000) {
|
|
29
|
+
binary += String.fromCharCode(...audio.subarray(i, i + 0x8000));
|
|
30
|
+
}
|
|
31
|
+
return btoa(binary);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The gateway accepts JSON only, so this model sends base64 audio instead of
|
|
35
|
+
* the multipart upload `@ai-sdk/openai` uses.
|
|
36
|
+
*/
|
|
37
|
+
function createTranscriptionModel(modelId, baseURL, fetch, usageMetadata) {
|
|
38
|
+
return {
|
|
39
|
+
specificationVersion: "v4",
|
|
40
|
+
provider: "convexGateway",
|
|
41
|
+
modelId,
|
|
42
|
+
async doGenerate(options) {
|
|
43
|
+
const format = audioFormats[options.mediaType.split(";")[0].trim().toLowerCase()];
|
|
44
|
+
if (!format) {
|
|
45
|
+
throw new Error(`Unsupported audio type for the Convex AI gateway: ${options.mediaType}`);
|
|
46
|
+
}
|
|
47
|
+
const extra = options.providerOptions?.convexGateway ?? {};
|
|
48
|
+
const headers = new Headers();
|
|
49
|
+
for (const [key, value] of Object.entries(options.headers ?? {})) {
|
|
50
|
+
if (value !== undefined)
|
|
51
|
+
headers.set(key, value);
|
|
52
|
+
}
|
|
53
|
+
headers.set("Content-Type", "application/json");
|
|
54
|
+
const timestamp = new Date();
|
|
55
|
+
const url = `${baseURL}/audio/transcriptions`;
|
|
56
|
+
const requestBody = {
|
|
57
|
+
...extra,
|
|
58
|
+
model: modelId,
|
|
59
|
+
input_audio: { data: base64(options.audio), format },
|
|
60
|
+
};
|
|
61
|
+
const response = await fetch(url, {
|
|
62
|
+
method: "POST",
|
|
63
|
+
headers,
|
|
64
|
+
signal: options.abortSignal,
|
|
65
|
+
body: JSON.stringify(requestBody),
|
|
66
|
+
});
|
|
67
|
+
// A proxy error page is not JSON; the status message covers it.
|
|
68
|
+
const body = (await response.json().catch(() => ({})));
|
|
69
|
+
if (!response.ok || typeof body.text !== "string") {
|
|
70
|
+
throw new ai_1.APICallError({
|
|
71
|
+
message: body.error?.message ?? `Transcription failed (${response.status})`,
|
|
72
|
+
url,
|
|
73
|
+
requestBodyValues: requestBody,
|
|
74
|
+
statusCode: response.status,
|
|
75
|
+
responseHeaders: Object.fromEntries(response.headers),
|
|
76
|
+
responseBody: JSON.stringify(body),
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
const duration = body.duration ?? body.usage?.seconds;
|
|
80
|
+
return {
|
|
81
|
+
text: body.text,
|
|
82
|
+
// Present with `response_format: "verbose_json"`.
|
|
83
|
+
segments: (Array.isArray(body.segments) ? body.segments : []).flatMap(({ text, start, end }) => typeof text === "string" &&
|
|
84
|
+
typeof start === "number" &&
|
|
85
|
+
typeof end === "number"
|
|
86
|
+
? [{ text, startSecond: start, endSecond: end }]
|
|
87
|
+
: []),
|
|
88
|
+
language: typeof body.language === "string" ? body.language : undefined,
|
|
89
|
+
durationInSeconds: typeof duration === "number" ? duration : undefined,
|
|
90
|
+
warnings: [],
|
|
91
|
+
providerMetadata: usageMetadata(body.usage),
|
|
92
|
+
response: {
|
|
93
|
+
timestamp,
|
|
94
|
+
modelId,
|
|
95
|
+
headers: Object.fromEntries(response.headers),
|
|
96
|
+
body,
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
//# sourceMappingURL=transcription-model.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transcription-model.js","sourceRoot":"","sources":["../../src/transcription-model.ts"],"names":[],"mappings":";;AAgDA,4DAoFC;AApID,2BAIY;AAWZ,yDAAyD;AACzD,MAAM,YAAY,GAA2B;IAC3C,YAAY,EAAE,KAAK;IACnB,WAAW,EAAE,KAAK;IAClB,YAAY,EAAE,KAAK;IACnB,aAAa,EAAE,KAAK;IACpB,WAAW,EAAE,KAAK;IAClB,YAAY,EAAE,KAAK;IACnB,aAAa,EAAE,KAAK;IACpB,YAAY,EAAE,MAAM;IACpB,cAAc,EAAE,MAAM;IACtB,WAAW,EAAE,KAAK;IAClB,WAAW,EAAE,KAAK;IAClB,aAAa,EAAE,KAAK;IACpB,WAAW,EAAE,KAAK;IAClB,YAAY,EAAE,MAAM;IACpB,WAAW,EAA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,CAAC,KAAK,EAAE,OAAO,CAAC;YACtD,OAAO;gBACL,IAAI,EAAE,IAAI,CAAC,IAAI;gBACf,kDAAkD;gBAClD,QAAQ,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,OAAO,CACnE,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,EAAE,EAAE,EAAE,CACvB,OAAO,IAAI,KAAK,QAAQ;oBACxB,OAAO,KAAK,KAAK,QAAQ;oBACzB,OAAO,GAAG,KAAK,QAAQ;oBACrB,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,WAAW,EAAE,KAAK,EAAE,SAAS,EAAE,GAAG,EAAE,CAAC;oBAChD,CAAC,CAAC,EAAE,CACT;gBACD,QAAQ,EAAE,OAAO,IAAI,CAAC,QAAQ,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,SAAS;gBACvE,iBAAiB,EAAE,OAAO,QAAQ,KAAK,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,SAAS;gBACtE,QAAQ,EAAE,EAAE;gBACZ,gBAAgB,EAAE,aAAa,CAAC,IAAI,CAAC,KAAK,CAAC;gBAC3C,QAAQ,EAAE;oBACR,SAAS;oBACT,OAAO;oBACP,OAAO,EAAE,MAAM,CAAC,WAAW,CAAC,QAAQ,CAAC,OAAO,CAAC;oBAC7C,IAAI;iBACL;aACF,CAAC;QACJ,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -5,6 +5,10 @@ type ChatModel = ReturnType<Provider>;
|
|
|
5
5
|
type EmbeddingModel = ReturnType<typeof wrapEmbeddingModel>;
|
|
6
6
|
type ImageModel = ReturnType<Provider["imageModel"]>;
|
|
7
7
|
type GatewayLanguageModel = ReturnType<typeof wrapLanguageModel>;
|
|
8
|
+
export interface GatewayModelOptions {
|
|
9
|
+
/** Require an inference endpoint with a zero-data-retention policy. */
|
|
10
|
+
zdr?: boolean;
|
|
11
|
+
}
|
|
8
12
|
/**
|
|
9
13
|
* The recommended model interface for text generation through the Convex AI gateway.
|
|
10
14
|
* Use `messages` or `responses` only for endpoint-specific features.
|
|
@@ -12,13 +16,15 @@ type GatewayLanguageModel = ReturnType<typeof wrapLanguageModel>;
|
|
|
12
16
|
* `getServiceToken` caches and refreshes credentials within the current action,
|
|
13
17
|
* so calling this more than once in the same action is fine.
|
|
14
18
|
*/
|
|
15
|
-
export declare function convexGateway(modelId: string): ChatModel;
|
|
19
|
+
export declare function convexGateway(modelId: string, options?: GatewayModelOptions): ChatModel;
|
|
16
20
|
export declare namespace convexGateway {
|
|
17
|
-
var messages: (modelId: string) => GatewayLanguageModel;
|
|
18
|
-
var responses: (modelId: string) => GatewayLanguageModel;
|
|
21
|
+
var messages: (modelId: string, options?: GatewayModelOptions) => GatewayLanguageModel;
|
|
22
|
+
var responses: (modelId: string, options?: GatewayModelOptions) => GatewayLanguageModel;
|
|
19
23
|
var evaluationModel;
|
|
20
|
-
var embeddingModel: (modelId: string) => EmbeddingModel;
|
|
24
|
+
var embeddingModel: (modelId: string, options?: GatewayModelOptions) => EmbeddingModel;
|
|
21
25
|
var imageModel: (modelId: string) => ImageModel;
|
|
26
|
+
var transcriptionModel;
|
|
27
|
+
var speechModel;
|
|
22
28
|
var videoModel;
|
|
23
29
|
}
|
|
24
30
|
export { verifyVideoWebhook, type VideoWebhookEvent } from "./video-webhook.js";
|
package/dist/esm/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,sBAAsB,EAEvB,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAEL,kBAAkB,EAClB,iBAAiB,EAClB,MAAM,IAAI,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,sBAAsB,EAEvB,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAEL,kBAAkB,EAClB,iBAAiB,EAClB,MAAM,IAAI,CAAC;AAMZ,KAAK,QAAQ,GAAG,UAAU,CAAC,OAAO,sBAAsB,CAAC,CAAC;AAC1D,KAAK,SAAS,GAAG,UAAU,CAAC,QAAQ,CAAC,CAAC;AACtC,KAAK,cAAc,GAAG,UAAU,CAAC,OAAO,kBAAkB,CAAC,CAAC;AAI5D,KAAK,UAAU,GAAG,UAAU,CAAC,QAAQ,CAAC,YAAY,CAAC,CAAC,CAAC;AAErD,KAAK,oBAAoB,GAAG,UAAU,CAAC,OAAO,iBAAiB,CAAC,CAAC;AAEjE,MAAM,WAAW,mBAAmB;IAClC,uEAAuE;IACvE,GAAG,CAAC,EAAE,OAAO,CAAC;CACf;AA+GD;;;;;;GAMG;AACH,wBAAgB,aAAa,CAC3B,OAAO,EAAE,MAAM,EACf,OAAO,CAAC,EAAE,mBAAmB,GAC5B,SAAS,CAIX;yBAPe,aAAa;4BAUlB,MAAM,YACL,mBAAmB,KAC5B,oBAAoB;6BAcZ,MAAM,YACL,mBAAmB,KAC5B,oBAAoB;;kCA8BZ,MAAM,YACL,mBAAmB,KAC5B,cAAc;8BAa6B,MAAM,KAAG,UAAU;;;;;AAuEjE,OAAO,EAAE,kBAAkB,EAAE,KAAK,iBAAiB,EAAE,MAAM,oBAAoB,CAAC"}
|
package/dist/esm/index.js
CHANGED
|
@@ -4,6 +4,7 @@ import { createOpenAICompatible, } from "@ai-sdk/openai-compatible";
|
|
|
4
4
|
import { defaultSettingsMiddleware, wrapEmbeddingModel, wrapLanguageModel, } from "ai";
|
|
5
5
|
import { getServiceToken } from "convex/server";
|
|
6
6
|
import { createEvaluationModel } from "./evaluation-model.js";
|
|
7
|
+
import { createTranscriptionModel } from "./transcription-model.js";
|
|
7
8
|
import { createVideoModel } from "./video-model.js";
|
|
8
9
|
const maxEmbeddingsPerCall = 512;
|
|
9
10
|
// Official providers require a credential before gatewayFetch replaces it with a deployment JWT.
|
|
@@ -53,11 +54,11 @@ const costMetadataExtractor = {
|
|
|
53
54
|
};
|
|
54
55
|
},
|
|
55
56
|
};
|
|
56
|
-
function createGatewayProvider() {
|
|
57
|
+
function createGatewayProvider(fetch = gatewayFetch) {
|
|
57
58
|
return createOpenAICompatible({
|
|
58
59
|
name: "convexGateway",
|
|
59
60
|
baseURL: gatewayBaseURL(),
|
|
60
|
-
fetch
|
|
61
|
+
fetch,
|
|
61
62
|
metadataExtractor: costMetadataExtractor,
|
|
62
63
|
supportsStructuredOutputs: true,
|
|
63
64
|
supportedUrls: () => ({ "image/*": [/^https?:\/\/.*$/] }),
|
|
@@ -71,9 +72,12 @@ function sdkModelId(gatewayModelId, provider) {
|
|
|
71
72
|
// OpenRouter uses dots in Anthropic versions; the Anthropic SDK's capability lookup uses hyphens.
|
|
72
73
|
return provider === "anthropic" ? modelId.replaceAll(".", "-") : modelId;
|
|
73
74
|
}
|
|
74
|
-
function gatewayModelFetch(gatewayModelId, input, init) {
|
|
75
|
+
function gatewayModelFetch(gatewayModelId, input, init, options) {
|
|
75
76
|
const body = JSON.parse(init?.body);
|
|
76
77
|
body.model = gatewayModelId;
|
|
78
|
+
if (options?.zdr !== undefined) {
|
|
79
|
+
body.provider = { ...body.provider, zdr: options.zdr };
|
|
80
|
+
}
|
|
77
81
|
return gatewayFetch(input, { ...init, body: JSON.stringify(body) });
|
|
78
82
|
}
|
|
79
83
|
function gatewayLanguageModel(modelId, model, middleware) {
|
|
@@ -91,36 +95,36 @@ function gatewayLanguageModel(modelId, model, middleware) {
|
|
|
91
95
|
* `getServiceToken` caches and refreshes credentials within the current action,
|
|
92
96
|
* so calling this more than once in the same action is fine.
|
|
93
97
|
*/
|
|
94
|
-
export function convexGateway(modelId) {
|
|
95
|
-
return createGatewayProvider()(modelId);
|
|
98
|
+
export function convexGateway(modelId, options) {
|
|
99
|
+
return createGatewayProvider((input, init) => gatewayModelFetch(modelId, input, init, options))(modelId);
|
|
96
100
|
}
|
|
97
|
-
convexGateway.messages = function (modelId) {
|
|
101
|
+
convexGateway.messages = function (modelId, options) {
|
|
98
102
|
const provider = createAnthropic({
|
|
99
103
|
name: "convexGateway.messages",
|
|
100
104
|
baseURL: gatewayBaseURL(),
|
|
101
105
|
authToken: placeholderCredential,
|
|
102
|
-
fetch: (input, init) => gatewayModelFetch(modelId, input, init),
|
|
106
|
+
fetch: (input, init) => gatewayModelFetch(modelId, input, init, options),
|
|
103
107
|
});
|
|
104
108
|
return gatewayLanguageModel(modelId, provider.messages(sdkModelId(modelId, "anthropic")));
|
|
105
109
|
};
|
|
106
|
-
convexGateway.responses = function (modelId) {
|
|
110
|
+
convexGateway.responses = function (modelId, options) {
|
|
107
111
|
const provider = createOpenAI({
|
|
108
112
|
name: "convexGateway.responses",
|
|
109
113
|
baseURL: gatewayBaseURL(),
|
|
110
114
|
apiKey: placeholderCredential,
|
|
111
|
-
fetch: (input, init) => gatewayModelFetch(modelId, input, init),
|
|
115
|
+
fetch: (input, init) => gatewayModelFetch(modelId, input, init, options),
|
|
112
116
|
});
|
|
113
117
|
return gatewayLanguageModel(modelId, provider.responses(sdkModelId(modelId, "openai")), defaultSettingsMiddleware({
|
|
114
118
|
settings: { providerOptions: { openai: { store: false } } },
|
|
115
119
|
}));
|
|
116
120
|
};
|
|
117
121
|
/** Evaluate typed questions through the alpha Decisions API. */
|
|
118
|
-
convexGateway.evaluationModel = function (modelId) {
|
|
119
|
-
return createEvaluationModel(modelId, gatewayBaseURL("alpha"),
|
|
122
|
+
convexGateway.evaluationModel = function (modelId, options) {
|
|
123
|
+
return createEvaluationModel(modelId, gatewayBaseURL("alpha"), (input, init) => gatewayModelFetch(modelId, input, init, options), convexGatewayUsageMetadata);
|
|
120
124
|
};
|
|
121
|
-
convexGateway.embeddingModel = function (modelId) {
|
|
125
|
+
convexGateway.embeddingModel = function (modelId, options) {
|
|
122
126
|
return wrapEmbeddingModel({
|
|
123
|
-
model: createGatewayProvider().embeddingModel(modelId),
|
|
127
|
+
model: createGatewayProvider((input, init) => gatewayModelFetch(modelId, input, init, options)).embeddingModel(modelId),
|
|
124
128
|
middleware: {
|
|
125
129
|
specificationVersion: "v4",
|
|
126
130
|
overrideMaxEmbeddingsPerCall: () => maxEmbeddingsPerCall,
|
|
@@ -129,7 +133,54 @@ convexGateway.embeddingModel = function (modelId) {
|
|
|
129
133
|
};
|
|
130
134
|
/** Image model for the AI SDK's `generateImage`. */
|
|
131
135
|
convexGateway.imageModel = function (modelId) {
|
|
132
|
-
|
|
136
|
+
const { specificationVersion, provider, maxImagesPerCall } = createGatewayProvider().imageModel(modelId);
|
|
137
|
+
return {
|
|
138
|
+
specificationVersion,
|
|
139
|
+
provider,
|
|
140
|
+
modelId,
|
|
141
|
+
maxImagesPerCall,
|
|
142
|
+
async doGenerate(options) {
|
|
143
|
+
if (options.files?.length || options.mask) {
|
|
144
|
+
throw new Error("Convex AI Gateway does not support image editing. Use a text prompt without input images or a mask.");
|
|
145
|
+
}
|
|
146
|
+
// The SDK's image adapter discards `usage` and has no metadata hook, so
|
|
147
|
+
// each call reads it from its own response through a per-call fetch.
|
|
148
|
+
let usage;
|
|
149
|
+
const model = createGatewayProvider(async (input, init) => {
|
|
150
|
+
const response = await gatewayFetch(input, init);
|
|
151
|
+
if (response.ok) {
|
|
152
|
+
usage = (await response.clone().json()).usage;
|
|
153
|
+
}
|
|
154
|
+
return response;
|
|
155
|
+
}).imageModel(modelId);
|
|
156
|
+
const result = await model.doGenerate(options);
|
|
157
|
+
const metadata = convexGatewayUsageMetadata(usage)?.convexGateway;
|
|
158
|
+
return {
|
|
159
|
+
...result,
|
|
160
|
+
...(metadata && {
|
|
161
|
+
// The SDK's image metadata type requires a per-image `images` array.
|
|
162
|
+
providerMetadata: { convexGateway: { ...metadata, images: [] } },
|
|
163
|
+
}),
|
|
164
|
+
};
|
|
165
|
+
},
|
|
166
|
+
};
|
|
167
|
+
};
|
|
168
|
+
/** Speech-to-text for the AI SDK's `experimental_transcribe`. */
|
|
169
|
+
convexGateway.transcriptionModel = function (modelId) {
|
|
170
|
+
return createTranscriptionModel(modelId, gatewayBaseURL(), gatewayFetch, convexGatewayUsageMetadata);
|
|
171
|
+
};
|
|
172
|
+
/**
|
|
173
|
+
* Text-to-speech for the AI SDK's `experimental_generateSpeech`. The gateway
|
|
174
|
+
* reports speech cost after the call, so results carry no cost metadata.
|
|
175
|
+
*/
|
|
176
|
+
convexGateway.speechModel = function (modelId) {
|
|
177
|
+
const provider = createOpenAI({
|
|
178
|
+
name: "convexGateway.speech",
|
|
179
|
+
baseURL: gatewayBaseURL(),
|
|
180
|
+
apiKey: placeholderCredential,
|
|
181
|
+
fetch: (input, init) => gatewayModelFetch(modelId, input, init),
|
|
182
|
+
});
|
|
183
|
+
return provider.speech(modelId);
|
|
133
184
|
};
|
|
134
185
|
/** Video generation for the AI SDK's `experimental_generateVideo`. */
|
|
135
186
|
convexGateway.videoModel = function (modelId) {
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,EACL,sBAAsB,GAEvB,MAAM,2BAA2B,CAAC;AACnC,OAAO,EACL,yBAAyB,EACzB,kBAAkB,EAClB,iBAAiB,GAClB,MAAM,IAAI,CAAC;AACZ,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAChD,OAAO,EAAE,qBAAqB,EAAE,MAAM,uBAAuB,CAAC;AAC9D,OAAO,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":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sE;AACtE,aAAa,CAAC,UAAU,GAAG,UAAU,OAAe;IAClD,OAAO,gBAAgB,CACrB,OAAO,EACP,cAAc,EAAE,EAChB,YAAY,EACZ,0BAA0B,CAC3B,CAAC;AACJ,CAAC,CAAC;AAEF,OAAO,EAAE,kBAAkB,EAA0B,MAAM,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type experimental_transcribe as transcribe, type ProviderMetadata } from "ai";
|
|
2
|
+
type TranscriptionModel = Extract<Parameters<typeof transcribe>[0]["model"], {
|
|
3
|
+
specificationVersion: "v4";
|
|
4
|
+
}>;
|
|
5
|
+
/**
|
|
6
|
+
* The gateway accepts JSON only, so this model sends base64 audio instead of
|
|
7
|
+
* the multipart upload `@ai-sdk/openai` uses.
|
|
8
|
+
*/
|
|
9
|
+
export declare function createTranscriptionModel(modelId: string, baseURL: string, fetch: typeof globalThis.fetch, usageMetadata: (usage: unknown) => ProviderMetadata | undefined): TranscriptionModel;
|
|
10
|
+
export {};
|
|
11
|
+
//# sourceMappingURL=transcription-model.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transcription-model.d.ts","sourceRoot":"","sources":["../../src/transcription-model.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,uBAAuB,IAAI,UAAU,EAC1C,KAAK,gBAAgB,EACtB,MAAM,IAAI,CAAC;AAEZ,KAAK,kBAAkB,GAAG,OAAO,CAC/B,UAAU,CAAC,OAAO,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,EACzC;IAAE,oBAAoB,EAAE,IAAI,CAAA;CAAE,CAC/B,CAAC;AAmCF;;;GAGG;AACH,wBAAgB,wBAAwB,CACtC,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,MAAM,EACf,KAAK,EAAE,OAAO,UAAU,CAAC,KAAK,EAC9B,aAAa,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,gBAAgB,GAAG,SAAS,GAC9D,kBAAkB,CA+EpB"}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { APICallError, } from "ai";
|
|
2
|
+
// OpenRouter names formats by extension, not media type.
|
|
3
|
+
const audioFormats = {
|
|
4
|
+
"audio/mpeg": "mp3",
|
|
5
|
+
"audio/mp3": "mp3",
|
|
6
|
+
"audio/mpga": "mp3",
|
|
7
|
+
"audio/x-mp3": "mp3",
|
|
8
|
+
"audio/wav": "wav",
|
|
9
|
+
"audio/wave": "wav",
|
|
10
|
+
"audio/x-wav": "wav",
|
|
11
|
+
"audio/flac": "flac",
|
|
12
|
+
"audio/x-flac": "flac",
|
|
13
|
+
"audio/mp4": "m4a",
|
|
14
|
+
"audio/m4a": "m4a",
|
|
15
|
+
"audio/x-m4a": "m4a",
|
|
16
|
+
"audio/ogg": "ogg",
|
|
17
|
+
"audio/webm": "webm",
|
|
18
|
+
"audio/aac": "aac",
|
|
19
|
+
};
|
|
20
|
+
function base64(audio) {
|
|
21
|
+
if (typeof audio === "string")
|
|
22
|
+
return audio;
|
|
23
|
+
// Chunked so large recordings stay under the engine's argument limit.
|
|
24
|
+
let binary = "";
|
|
25
|
+
for (let i = 0; i < audio.length; i += 0x8000) {
|
|
26
|
+
binary += String.fromCharCode(...audio.subarray(i, i + 0x8000));
|
|
27
|
+
}
|
|
28
|
+
return btoa(binary);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The gateway accepts JSON only, so this model sends base64 audio instead of
|
|
32
|
+
* the multipart upload `@ai-sdk/openai` uses.
|
|
33
|
+
*/
|
|
34
|
+
export function createTranscriptionModel(modelId, baseURL, fetch, usageMetadata) {
|
|
35
|
+
return {
|
|
36
|
+
specificationVersion: "v4",
|
|
37
|
+
provider: "convexGateway",
|
|
38
|
+
modelId,
|
|
39
|
+
async doGenerate(options) {
|
|
40
|
+
const format = audioFormats[options.mediaType.split(";")[0].trim().toLowerCase()];
|
|
41
|
+
if (!format) {
|
|
42
|
+
throw new Error(`Unsupported audio type for the Convex AI gateway: ${options.mediaType}`);
|
|
43
|
+
}
|
|
44
|
+
const extra = options.providerOptions?.convexGateway ?? {};
|
|
45
|
+
const headers = new Headers();
|
|
46
|
+
for (const [key, value] of Object.entries(options.headers ?? {})) {
|
|
47
|
+
if (value !== undefined)
|
|
48
|
+
headers.set(key, value);
|
|
49
|
+
}
|
|
50
|
+
headers.set("Content-Type", "application/json");
|
|
51
|
+
const timestamp = new Date();
|
|
52
|
+
const url = `${baseURL}/audio/transcriptions`;
|
|
53
|
+
const requestBody = {
|
|
54
|
+
...extra,
|
|
55
|
+
model: modelId,
|
|
56
|
+
input_audio: { data: base64(options.audio), format },
|
|
57
|
+
};
|
|
58
|
+
const response = await fetch(url, {
|
|
59
|
+
method: "POST",
|
|
60
|
+
headers,
|
|
61
|
+
signal: options.abortSignal,
|
|
62
|
+
body: JSON.stringify(requestBody),
|
|
63
|
+
});
|
|
64
|
+
// A proxy error page is not JSON; the status message covers it.
|
|
65
|
+
const body = (await response.json().catch(() => ({})));
|
|
66
|
+
if (!response.ok || typeof body.text !== "string") {
|
|
67
|
+
throw new APICallError({
|
|
68
|
+
message: body.error?.message ?? `Transcription failed (${response.status})`,
|
|
69
|
+
url,
|
|
70
|
+
requestBodyValues: requestBody,
|
|
71
|
+
statusCode: response.status,
|
|
72
|
+
responseHeaders: Object.fromEntries(response.headers),
|
|
73
|
+
responseBody: JSON.stringify(body),
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
const duration = body.duration ?? body.usage?.seconds;
|
|
77
|
+
return {
|
|
78
|
+
text: body.text,
|
|
79
|
+
// Present with `response_format: "verbose_json"`.
|
|
80
|
+
segments: (Array.isArray(body.segments) ? body.segments : []).flatMap(({ text, start, end }) => typeof text === "string" &&
|
|
81
|
+
typeof start === "number" &&
|
|
82
|
+
typeof end === "number"
|
|
83
|
+
? [{ text, startSecond: start, endSecond: end }]
|
|
84
|
+
: []),
|
|
85
|
+
language: typeof body.language === "string" ? body.language : undefined,
|
|
86
|
+
durationInSeconds: typeof duration === "number" ? duration : undefined,
|
|
87
|
+
warnings: [],
|
|
88
|
+
providerMetadata: usageMetadata(body.usage),
|
|
89
|
+
response: {
|
|
90
|
+
timestamp,
|
|
91
|
+
modelId,
|
|
92
|
+
headers: Object.fromEntries(response.headers),
|
|
93
|
+
body,
|
|
94
|
+
},
|
|
95
|
+
};
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
//# sourceMappingURL=transcription-model.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transcription-model.js","sourceRoot":"","sources":["../../src/transcription-model.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,YAAY,GAGb,MAAM,IAAI,CAAC;AAWZ,yDAAyD;AACzD,MAAM,YAAY,GAA2B;IAC3C,YAAY,EAAE,KAAK;IACnB,WAAW,EAAE,KAAK;IAClB,YAAY,EAAE,KAAK;IACnB,aAAa,EAAE,KAAK;IACpB,WAAW,EAAE,KAAK;IAClB,YAAY,EAAE,KAAK;IACnB,aAAa,EAAE,KAAK;IACpB,YAAY,EAAE,MAAM;IACpB,cAAc,EAAE,MAAM;IACtB,WAAW,EAAE,KAAK;IAClB,WAAW,EAAE,KAAK;IAClB,aAAa,EAAE,KAAK;IACpB,WAAW,EAAE,KAAK;IAClB,YAAY,EAAE,MAAM;IACpB,WAAW,EAAE,KAAK;CACnB,CAAC;AAEF,SAAS,MAAM,CAAC,KAA0B;IACxC,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,KAAK,CAAC;IAC5C,sEAAsE;IACtE,IAAI,MAAM,GAAG,EAAE,CAAC;IAChB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,IAAI,MAAM,EAAE,CAAC;QAC9C,MAAM,IAAI,MAAM,CAAC,YAAY,CAAC,GAAG,KAAK,CAAC,QAAQ,CAAC,CAAC,EAAE,CAAC,GAAG,MAAM,CA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;oBACzB,OAAO,GAAG,KAAK,QAAQ;oBACrB,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,WAAW,EAAE,KAAK,EAAE,SAAS,EAAE,GAAG,EAAE,CAAC;oBAChD,CAAC,CAAC,EAAE,CACT;gBACD,QAAQ,EAAE,OAAO,IAAI,CAAC,QAAQ,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,SAAS;gBACvE,iBAAiB,EAAE,OAAO,QAAQ,KAAK,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,SAAS;gBACtE,QAAQ,EAAE,EAAE;gBACZ,gBAAgB,EAAE,aAAa,CAAC,IAAI,CAAC,KAAK,CAAC;gBAC3C,QAAQ,EAAE;oBACR,SAAS;oBACT,OAAO;oBACP,OAAO,EAAE,MAAM,CAAC,WAAW,CAAC,QAAQ,CAAC,OAAO,CAAC;oBAC7C,IAAI;iBACL;aACF,CAAC;QACJ,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@convex-dev/ai-sdk-provider",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "AI SDK provider for the Convex AI gateway",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"type": "module",
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
"dist",
|
|
14
14
|
"src/index.ts",
|
|
15
15
|
"src/evaluation-model.ts",
|
|
16
|
+
"src/transcription-model.ts",
|
|
16
17
|
"src/video-model.ts",
|
|
17
18
|
"src/video-webhook.ts",
|
|
18
19
|
"LICENSE"
|
package/src/index.ts
CHANGED
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
} from "ai";
|
|
12
12
|
import { getServiceToken } from "convex/server";
|
|
13
13
|
import { createEvaluationModel } from "./evaluation-model.js";
|
|
14
|
+
import { createTranscriptionModel } from "./transcription-model.js";
|
|
14
15
|
import { createVideoModel } from "./video-model.js";
|
|
15
16
|
|
|
16
17
|
type Provider = ReturnType<typeof createOpenAICompatible>;
|
|
@@ -23,6 +24,11 @@ type ImageModel = ReturnType<Provider["imageModel"]>;
|
|
|
23
24
|
type LanguageModel = Parameters<typeof wrapLanguageModel>[0]["model"];
|
|
24
25
|
type GatewayLanguageModel = ReturnType<typeof wrapLanguageModel>;
|
|
25
26
|
|
|
27
|
+
export interface GatewayModelOptions {
|
|
28
|
+
/** Require an inference endpoint with a zero-data-retention policy. */
|
|
29
|
+
zdr?: boolean;
|
|
30
|
+
}
|
|
31
|
+
|
|
26
32
|
const maxEmbeddingsPerCall = 512;
|
|
27
33
|
// Official providers require a credential before gatewayFetch replaces it with a deployment JWT.
|
|
28
34
|
const placeholderCredential = "convex-gateway";
|
|
@@ -82,11 +88,11 @@ const costMetadataExtractor: MetadataExtractor = {
|
|
|
82
88
|
},
|
|
83
89
|
};
|
|
84
90
|
|
|
85
|
-
function createGatewayProvider(): Provider {
|
|
91
|
+
function createGatewayProvider(fetch = gatewayFetch): Provider {
|
|
86
92
|
return createOpenAICompatible({
|
|
87
93
|
name: "convexGateway",
|
|
88
94
|
baseURL: gatewayBaseURL(),
|
|
89
|
-
fetch
|
|
95
|
+
fetch,
|
|
90
96
|
metadataExtractor: costMetadataExtractor,
|
|
91
97
|
supportsStructuredOutputs: true,
|
|
92
98
|
supportedUrls: () => ({ "image/*": [/^https?:\/\/.*$/] }),
|
|
@@ -109,9 +115,13 @@ function gatewayModelFetch(
|
|
|
109
115
|
gatewayModelId: string,
|
|
110
116
|
input: RequestInfo | URL,
|
|
111
117
|
init?: RequestInit,
|
|
118
|
+
options?: GatewayModelOptions,
|
|
112
119
|
): Promise<Response> {
|
|
113
120
|
const body = JSON.parse(init?.body as string);
|
|
114
121
|
body.model = gatewayModelId;
|
|
122
|
+
if (options?.zdr !== undefined) {
|
|
123
|
+
body.provider = { ...body.provider, zdr: options.zdr };
|
|
124
|
+
}
|
|
115
125
|
return gatewayFetch(input, { ...init, body: JSON.stringify(body) });
|
|
116
126
|
}
|
|
117
127
|
|
|
@@ -135,16 +145,24 @@ function gatewayLanguageModel(
|
|
|
135
145
|
* `getServiceToken` caches and refreshes credentials within the current action,
|
|
136
146
|
* so calling this more than once in the same action is fine.
|
|
137
147
|
*/
|
|
138
|
-
export function convexGateway(
|
|
139
|
-
|
|
148
|
+
export function convexGateway(
|
|
149
|
+
modelId: string,
|
|
150
|
+
options?: GatewayModelOptions,
|
|
151
|
+
): ChatModel {
|
|
152
|
+
return createGatewayProvider((input, init) =>
|
|
153
|
+
gatewayModelFetch(modelId, input, init, options),
|
|
154
|
+
)(modelId);
|
|
140
155
|
}
|
|
141
156
|
|
|
142
|
-
convexGateway.messages = function (
|
|
157
|
+
convexGateway.messages = function (
|
|
158
|
+
modelId: string,
|
|
159
|
+
options?: GatewayModelOptions,
|
|
160
|
+
): GatewayLanguageModel {
|
|
143
161
|
const provider = createAnthropic({
|
|
144
162
|
name: "convexGateway.messages",
|
|
145
163
|
baseURL: gatewayBaseURL(),
|
|
146
164
|
authToken: placeholderCredential,
|
|
147
|
-
fetch: (input, init) => gatewayModelFetch(modelId, input, init),
|
|
165
|
+
fetch: (input, init) => gatewayModelFetch(modelId, input, init, options),
|
|
148
166
|
});
|
|
149
167
|
return gatewayLanguageModel(
|
|
150
168
|
modelId,
|
|
@@ -152,12 +170,15 @@ convexGateway.messages = function (modelId: string): GatewayLanguageModel {
|
|
|
152
170
|
);
|
|
153
171
|
};
|
|
154
172
|
|
|
155
|
-
convexGateway.responses = function (
|
|
173
|
+
convexGateway.responses = function (
|
|
174
|
+
modelId: string,
|
|
175
|
+
options?: GatewayModelOptions,
|
|
176
|
+
): GatewayLanguageModel {
|
|
156
177
|
const provider = createOpenAI({
|
|
157
178
|
name: "convexGateway.responses",
|
|
158
179
|
baseURL: gatewayBaseURL(),
|
|
159
180
|
apiKey: placeholderCredential,
|
|
160
|
-
fetch: (input, init) => gatewayModelFetch(modelId, input, init),
|
|
181
|
+
fetch: (input, init) => gatewayModelFetch(modelId, input, init, options),
|
|
161
182
|
});
|
|
162
183
|
return gatewayLanguageModel(
|
|
163
184
|
modelId,
|
|
@@ -169,18 +190,26 @@ convexGateway.responses = function (modelId: string): GatewayLanguageModel {
|
|
|
169
190
|
};
|
|
170
191
|
|
|
171
192
|
/** Evaluate typed questions through the alpha Decisions API. */
|
|
172
|
-
convexGateway.evaluationModel = function (
|
|
193
|
+
convexGateway.evaluationModel = function (
|
|
194
|
+
modelId: string,
|
|
195
|
+
options?: GatewayModelOptions,
|
|
196
|
+
) {
|
|
173
197
|
return createEvaluationModel(
|
|
174
198
|
modelId,
|
|
175
199
|
gatewayBaseURL("alpha"),
|
|
176
|
-
|
|
200
|
+
(input, init) => gatewayModelFetch(modelId, input, init, options),
|
|
177
201
|
convexGatewayUsageMetadata,
|
|
178
202
|
);
|
|
179
203
|
};
|
|
180
204
|
|
|
181
|
-
convexGateway.embeddingModel = function (
|
|
205
|
+
convexGateway.embeddingModel = function (
|
|
206
|
+
modelId: string,
|
|
207
|
+
options?: GatewayModelOptions,
|
|
208
|
+
): EmbeddingModel {
|
|
182
209
|
return wrapEmbeddingModel({
|
|
183
|
-
model: createGatewayProvider(
|
|
210
|
+
model: createGatewayProvider((input, init) =>
|
|
211
|
+
gatewayModelFetch(modelId, input, init, options),
|
|
212
|
+
).embeddingModel(modelId),
|
|
184
213
|
middleware: {
|
|
185
214
|
specificationVersion: "v4",
|
|
186
215
|
overrideMaxEmbeddingsPerCall: () => maxEmbeddingsPerCall,
|
|
@@ -190,7 +219,64 @@ convexGateway.embeddingModel = function (modelId: string): EmbeddingModel {
|
|
|
190
219
|
|
|
191
220
|
/** Image model for the AI SDK's `generateImage`. */
|
|
192
221
|
convexGateway.imageModel = function (modelId: string): ImageModel {
|
|
193
|
-
|
|
222
|
+
const { specificationVersion, provider, maxImagesPerCall } =
|
|
223
|
+
createGatewayProvider().imageModel(modelId);
|
|
224
|
+
return {
|
|
225
|
+
specificationVersion,
|
|
226
|
+
provider,
|
|
227
|
+
modelId,
|
|
228
|
+
maxImagesPerCall,
|
|
229
|
+
async doGenerate(options) {
|
|
230
|
+
if (options.files?.length || options.mask) {
|
|
231
|
+
throw new Error(
|
|
232
|
+
"Convex AI Gateway does not support image editing. Use a text prompt without input images or a mask.",
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
// The SDK's image adapter discards `usage` and has no metadata hook, so
|
|
236
|
+
// each call reads it from its own response through a per-call fetch.
|
|
237
|
+
let usage: unknown;
|
|
238
|
+
const model = createGatewayProvider(async (input, init) => {
|
|
239
|
+
const response = await gatewayFetch(input, init);
|
|
240
|
+
if (response.ok) {
|
|
241
|
+
usage = (await response.clone().json()).usage;
|
|
242
|
+
}
|
|
243
|
+
return response;
|
|
244
|
+
}).imageModel(modelId);
|
|
245
|
+
const result = await model.doGenerate(options);
|
|
246
|
+
const metadata = convexGatewayUsageMetadata(usage)?.convexGateway;
|
|
247
|
+
return {
|
|
248
|
+
...result,
|
|
249
|
+
...(metadata && {
|
|
250
|
+
// The SDK's image metadata type requires a per-image `images` array.
|
|
251
|
+
providerMetadata: { convexGateway: { ...metadata, images: [] } },
|
|
252
|
+
}),
|
|
253
|
+
};
|
|
254
|
+
},
|
|
255
|
+
};
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
/** Speech-to-text for the AI SDK's `experimental_transcribe`. */
|
|
259
|
+
convexGateway.transcriptionModel = function (modelId: string) {
|
|
260
|
+
return createTranscriptionModel(
|
|
261
|
+
modelId,
|
|
262
|
+
gatewayBaseURL(),
|
|
263
|
+
gatewayFetch,
|
|
264
|
+
convexGatewayUsageMetadata,
|
|
265
|
+
);
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Text-to-speech for the AI SDK's `experimental_generateSpeech`. The gateway
|
|
270
|
+
* reports speech cost after the call, so results carry no cost metadata.
|
|
271
|
+
*/
|
|
272
|
+
convexGateway.speechModel = function (modelId: string) {
|
|
273
|
+
const provider = createOpenAI({
|
|
274
|
+
name: "convexGateway.speech",
|
|
275
|
+
baseURL: gatewayBaseURL(),
|
|
276
|
+
apiKey: placeholderCredential,
|
|
277
|
+
fetch: (input, init) => gatewayModelFetch(modelId, input, init),
|
|
278
|
+
});
|
|
279
|
+
return provider.speech(modelId);
|
|
194
280
|
};
|
|
195
281
|
|
|
196
282
|
/** Video generation for the AI SDK's `experimental_generateVideo`. */
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import {
|
|
2
|
+
APICallError,
|
|
3
|
+
type experimental_transcribe as transcribe,
|
|
4
|
+
type ProviderMetadata,
|
|
5
|
+
} from "ai";
|
|
6
|
+
|
|
7
|
+
type TranscriptionModel = Extract<
|
|
8
|
+
Parameters<typeof transcribe>[0]["model"],
|
|
9
|
+
{ specificationVersion: "v4" }
|
|
10
|
+
>;
|
|
11
|
+
type TranscriptionOptions = Parameters<TranscriptionModel["doGenerate"]>[0];
|
|
12
|
+
type TranscriptionResult = Awaited<
|
|
13
|
+
ReturnType<TranscriptionModel["doGenerate"]>
|
|
14
|
+
>;
|
|
15
|
+
|
|
16
|
+
// OpenRouter names formats by extension, not media type.
|
|
17
|
+
const audioFormats: Record<string, string> = {
|
|
18
|
+
"audio/mpeg": "mp3",
|
|
19
|
+
"audio/mp3": "mp3",
|
|
20
|
+
"audio/mpga": "mp3",
|
|
21
|
+
"audio/x-mp3": "mp3",
|
|
22
|
+
"audio/wav": "wav",
|
|
23
|
+
"audio/wave": "wav",
|
|
24
|
+
"audio/x-wav": "wav",
|
|
25
|
+
"audio/flac": "flac",
|
|
26
|
+
"audio/x-flac": "flac",
|
|
27
|
+
"audio/mp4": "m4a",
|
|
28
|
+
"audio/m4a": "m4a",
|
|
29
|
+
"audio/x-m4a": "m4a",
|
|
30
|
+
"audio/ogg": "ogg",
|
|
31
|
+
"audio/webm": "webm",
|
|
32
|
+
"audio/aac": "aac",
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
function base64(audio: Uint8Array | string): string {
|
|
36
|
+
if (typeof audio === "string") return audio;
|
|
37
|
+
// Chunked so large recordings stay under the engine's argument limit.
|
|
38
|
+
let binary = "";
|
|
39
|
+
for (let i = 0; i < audio.length; i += 0x8000) {
|
|
40
|
+
binary += String.fromCharCode(...audio.subarray(i, i + 0x8000));
|
|
41
|
+
}
|
|
42
|
+
return btoa(binary);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The gateway accepts JSON only, so this model sends base64 audio instead of
|
|
47
|
+
* the multipart upload `@ai-sdk/openai` uses.
|
|
48
|
+
*/
|
|
49
|
+
export function createTranscriptionModel(
|
|
50
|
+
modelId: string,
|
|
51
|
+
baseURL: string,
|
|
52
|
+
fetch: typeof globalThis.fetch,
|
|
53
|
+
usageMetadata: (usage: unknown) => ProviderMetadata | undefined,
|
|
54
|
+
): TranscriptionModel {
|
|
55
|
+
return {
|
|
56
|
+
specificationVersion: "v4",
|
|
57
|
+
provider: "convexGateway",
|
|
58
|
+
modelId,
|
|
59
|
+
async doGenerate(
|
|
60
|
+
options: TranscriptionOptions,
|
|
61
|
+
): Promise<TranscriptionResult> {
|
|
62
|
+
const format =
|
|
63
|
+
audioFormats[options.mediaType.split(";")[0].trim().toLowerCase()];
|
|
64
|
+
if (!format) {
|
|
65
|
+
throw new Error(
|
|
66
|
+
`Unsupported audio type for the Convex AI gateway: ${options.mediaType}`,
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
const extra = options.providerOptions?.convexGateway ?? {};
|
|
70
|
+
const headers = new Headers();
|
|
71
|
+
for (const [key, value] of Object.entries(options.headers ?? {})) {
|
|
72
|
+
if (value !== undefined) headers.set(key, value);
|
|
73
|
+
}
|
|
74
|
+
headers.set("Content-Type", "application/json");
|
|
75
|
+
const timestamp = new Date();
|
|
76
|
+
const url = `${baseURL}/audio/transcriptions`;
|
|
77
|
+
const requestBody = {
|
|
78
|
+
...extra,
|
|
79
|
+
model: modelId,
|
|
80
|
+
input_audio: { data: base64(options.audio), format },
|
|
81
|
+
};
|
|
82
|
+
const response = await fetch(url, {
|
|
83
|
+
method: "POST",
|
|
84
|
+
headers,
|
|
85
|
+
signal: options.abortSignal,
|
|
86
|
+
body: JSON.stringify(requestBody),
|
|
87
|
+
});
|
|
88
|
+
// A proxy error page is not JSON; the status message covers it.
|
|
89
|
+
const body = (await response.json().catch(() => ({}))) as {
|
|
90
|
+
text?: unknown;
|
|
91
|
+
language?: unknown;
|
|
92
|
+
duration?: unknown;
|
|
93
|
+
segments?: Array<{ text?: unknown; start?: unknown; end?: unknown }>;
|
|
94
|
+
usage?: { seconds?: unknown };
|
|
95
|
+
error?: { message?: string };
|
|
96
|
+
};
|
|
97
|
+
if (!response.ok || typeof body.text !== "string") {
|
|
98
|
+
throw new APICallError({
|
|
99
|
+
message:
|
|
100
|
+
body.error?.message ?? `Transcription failed (${response.status})`,
|
|
101
|
+
url,
|
|
102
|
+
requestBodyValues: requestBody,
|
|
103
|
+
statusCode: response.status,
|
|
104
|
+
responseHeaders: Object.fromEntries(response.headers),
|
|
105
|
+
responseBody: JSON.stringify(body),
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
const duration = body.duration ?? body.usage?.seconds;
|
|
109
|
+
return {
|
|
110
|
+
text: body.text,
|
|
111
|
+
// Present with `response_format: "verbose_json"`.
|
|
112
|
+
segments: (Array.isArray(body.segments) ? body.segments : []).flatMap(
|
|
113
|
+
({ text, start, end }) =>
|
|
114
|
+
typeof text === "string" &&
|
|
115
|
+
typeof start === "number" &&
|
|
116
|
+
typeof end === "number"
|
|
117
|
+
? [{ text, startSecond: start, endSecond: end }]
|
|
118
|
+
: [],
|
|
119
|
+
),
|
|
120
|
+
language: typeof body.language === "string" ? body.language : undefined,
|
|
121
|
+
durationInSeconds: typeof duration === "number" ? duration : undefined,
|
|
122
|
+
warnings: [],
|
|
123
|
+
providerMetadata: usageMetadata(body.usage),
|
|
124
|
+
response: {
|
|
125
|
+
timestamp,
|
|
126
|
+
modelId,
|
|
127
|
+
headers: Object.fromEntries(response.headers),
|
|
128
|
+
body,
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
},
|
|
132
|
+
};
|
|
133
|
+
}
|