@convex-dev/ai-sdk-provider 0.2.1 → 0.3.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 +63 -9
- package/dist/commonjs/decision-model.d.ts +4 -0
- package/dist/commonjs/decision-model.d.ts.map +1 -0
- package/dist/commonjs/{evaluation-model.js → decision-model.js} +3 -3
- package/dist/commonjs/decision-model.js.map +1 -0
- package/dist/commonjs/index.d.ts +46 -37
- package/dist/commonjs/index.d.ts.map +1 -1
- package/dist/commonjs/index.js +97 -26
- package/dist/commonjs/index.js.map +1 -1
- package/dist/commonjs/transcription-model.d.ts +10 -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/commonjs/video-model.d.ts +1 -1
- package/dist/commonjs/video-model.d.ts.map +1 -1
- package/dist/esm/decision-model.d.ts +4 -0
- package/dist/esm/decision-model.d.ts.map +1 -0
- package/dist/esm/{evaluation-model.js → decision-model.js} +2 -2
- package/dist/esm/decision-model.js.map +1 -0
- package/dist/esm/index.d.ts +46 -9
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +96 -24
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/transcription-model.d.ts +10 -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/dist/esm/video-model.d.ts +1 -1
- package/dist/esm/video-model.d.ts.map +1 -1
- package/package.json +6 -5
- package/src/{evaluation-model.ts → decision-model.ts} +3 -3
- package/src/index.ts +138 -25
- package/src/transcription-model.ts +133 -0
- package/src/video-model.ts +1 -1
- package/dist/commonjs/evaluation-model.d.ts +0 -5
- package/dist/commonjs/evaluation-model.d.ts.map +0 -1
- package/dist/commonjs/evaluation-model.js.map +0 -1
- package/dist/esm/evaluation-model.d.ts +0 -5
- package/dist/esm/evaluation-model.d.ts.map +0 -1
- package/dist/esm/evaluation-model.js.map +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decision-model.d.ts","sourceRoot":"","sources":["../../src/decision-model.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,qBAAqB,EAC1B,KAAK,gBAAgB,EACtB,MAAM,IAAI,CAAC;AAGZ,MAAM,MAAM,aAAa,GAAG,OAAO,CACjC,UAAU,CAAC,OAAO,qBAAqB,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,EACpD,MAAM,CACP,CAAC;AAyBF,wBAAgB,mBAAmB,CACjC,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,aAAa,CA+Ef"}
|
|
@@ -19,7 +19,7 @@ const responseSchema = z.object({
|
|
|
19
19
|
cost: z.number().nonnegative().optional(),
|
|
20
20
|
}),
|
|
21
21
|
});
|
|
22
|
-
export function
|
|
22
|
+
export function createDecisionModel(modelId, baseURL, fetch, usageMetadata) {
|
|
23
23
|
return {
|
|
24
24
|
specificationVersion: "v4",
|
|
25
25
|
provider: "convexGateway",
|
|
@@ -90,4 +90,4 @@ export function createEvaluationModel(modelId, baseURL, fetch, usageMetadata) {
|
|
|
90
90
|
},
|
|
91
91
|
};
|
|
92
92
|
}
|
|
93
|
-
//# sourceMappingURL=
|
|
93
|
+
//# sourceMappingURL=decision-model.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decision-model.js","sourceRoot":"","sources":["../../src/decision-model.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,YAAY,GAGb,MAAM,IAAI,CAAC;AACZ,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAOxB,MAAM,aAAa,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC,CA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,EAAE;iBACvC,CAAC,CAAC;gBACH,QAAQ,EAAE;oBACR,EAAE,EAAE,MAAM,CAAC,EAAE;oBACb,OAAO,EAAE,MAAM,CAAC,KAAK;oBACrB,OAAO,EAAE,MAAM,CAAC,WAAW,CAAC,QAAQ,CAAC,OAAO,CAAC;oBAC7C,IAAI,EAAE,OAAO;iBACd;aACF,CAAC;QACJ,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,10 +1,19 @@
|
|
|
1
|
+
import { createOpenAI } from "@ai-sdk/openai";
|
|
1
2
|
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
|
|
2
3
|
import { wrapEmbeddingModel, wrapLanguageModel } from "ai";
|
|
4
|
+
import { type DecisionModel } from "./decision-model.js";
|
|
5
|
+
import { type TranscriptionModel } from "./transcription-model.js";
|
|
6
|
+
import { type GatewayVideoModel } from "./video-model.js";
|
|
3
7
|
type Provider = ReturnType<typeof createOpenAICompatible>;
|
|
4
8
|
type ChatModel = ReturnType<Provider>;
|
|
5
9
|
type EmbeddingModel = ReturnType<typeof wrapEmbeddingModel>;
|
|
6
10
|
type ImageModel = ReturnType<Provider["imageModel"]>;
|
|
11
|
+
type SpeechModel = ReturnType<ReturnType<typeof createOpenAI>["speech"]>;
|
|
7
12
|
type GatewayLanguageModel = ReturnType<typeof wrapLanguageModel>;
|
|
13
|
+
export interface GatewayModelOptions {
|
|
14
|
+
/** Require an inference endpoint with a zero-data-retention policy. */
|
|
15
|
+
zdr?: boolean;
|
|
16
|
+
}
|
|
8
17
|
/**
|
|
9
18
|
* The recommended model interface for text generation through the Convex AI gateway.
|
|
10
19
|
* Use `messages` or `responses` only for endpoint-specific features.
|
|
@@ -12,14 +21,42 @@ type GatewayLanguageModel = ReturnType<typeof wrapLanguageModel>;
|
|
|
12
21
|
* `getServiceToken` caches and refreshes credentials within the current action,
|
|
13
22
|
* so calling this more than once in the same action is fine.
|
|
14
23
|
*/
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
+
declare function convexGateway(modelId: string, options?: GatewayModelOptions): ChatModel;
|
|
25
|
+
declare function messages(modelId: string, options?: GatewayModelOptions): GatewayLanguageModel;
|
|
26
|
+
declare function responses(modelId: string, options?: GatewayModelOptions): GatewayLanguageModel;
|
|
27
|
+
/** Decide typed questions through the alpha Decisions API. */
|
|
28
|
+
declare function decisionModel(modelId: string, options?: GatewayModelOptions): DecisionModel;
|
|
29
|
+
declare function embeddingModel(modelId: string, options?: GatewayModelOptions): EmbeddingModel;
|
|
30
|
+
/** Image model for the AI SDK's `generateImage`. */
|
|
31
|
+
declare function imageModel(modelId: string): ImageModel;
|
|
32
|
+
/** Speech-to-text for the AI SDK's `experimental_transcribe`. */
|
|
33
|
+
declare function transcriptionModel(modelId: string): TranscriptionModel;
|
|
34
|
+
/**
|
|
35
|
+
* Text-to-speech for the AI SDK's `experimental_generateSpeech`. The gateway
|
|
36
|
+
* reports speech cost after the call, so results carry no cost metadata.
|
|
37
|
+
*/
|
|
38
|
+
declare function speechModel(modelId: string): SpeechModel;
|
|
39
|
+
/** Video generation for the AI SDK's `experimental_generateVideo`. */
|
|
40
|
+
declare function videoModel(modelId: string): GatewayVideoModel;
|
|
41
|
+
/**
|
|
42
|
+
* The recommended model interface for text generation through the Convex AI gateway.
|
|
43
|
+
* Use `messages` or `responses` only for endpoint-specific features.
|
|
44
|
+
*
|
|
45
|
+
* `getServiceToken` caches and refreshes credentials within the current action,
|
|
46
|
+
* so calling this more than once in the same action is fine.
|
|
47
|
+
*/
|
|
48
|
+
declare const gateway: typeof convexGateway & {
|
|
49
|
+
messages: typeof messages;
|
|
50
|
+
responses: typeof responses;
|
|
51
|
+
decisionModel: typeof decisionModel;
|
|
52
|
+
/** @deprecated Use `decisionModel` instead. */
|
|
53
|
+
evaluationModel: typeof decisionModel;
|
|
54
|
+
embeddingModel: typeof embeddingModel;
|
|
55
|
+
imageModel: typeof imageModel;
|
|
56
|
+
transcriptionModel: typeof transcriptionModel;
|
|
57
|
+
speechModel: typeof speechModel;
|
|
58
|
+
videoModel: typeof videoModel;
|
|
59
|
+
};
|
|
60
|
+
export { gateway as convexGateway };
|
|
24
61
|
export { verifyVideoWebhook, type VideoWebhookEvent } from "./video-webhook.js";
|
|
25
62
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/esm/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,EACL,sBAAsB,EAEvB,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAEL,kBAAkB,EAClB,iBAAiB,EAClB,MAAM,IAAI,CAAC;AAEZ,OAAO,EAAuB,KAAK,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAC9E,OAAO,EAEL,KAAK,kBAAkB,EACxB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAoB,KAAK,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAE5E,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;AACrD,KAAK,WAAW,GAAG,UAAU,CAAC,UAAU,CAAC,OAAO,YAAY,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC;AAEzE,KAAK,oBAAoB,GAAG,UAAU,CAAC,OAAO,iBAAiB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ai-compatible";
|
|
4
4
|
import { defaultSettingsMiddleware, wrapEmbeddingModel, wrapLanguageModel, } from "ai";
|
|
5
5
|
import { getServiceToken } from "convex/server";
|
|
6
|
-
import {
|
|
6
|
+
import { createDecisionModel } from "./decision-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,49 +95,117 @@ 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
|
-
|
|
95
|
-
return createGatewayProvider()(modelId);
|
|
98
|
+
function convexGateway(modelId, options) {
|
|
99
|
+
return createGatewayProvider((input, init) => gatewayModelFetch(modelId, input, init, options))(modelId);
|
|
96
100
|
}
|
|
97
|
-
|
|
101
|
+
function messages(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
|
-
}
|
|
106
|
-
|
|
109
|
+
}
|
|
110
|
+
function responses(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
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
|
|
119
|
-
return
|
|
120
|
-
}
|
|
121
|
-
|
|
120
|
+
}
|
|
121
|
+
/** Decide typed questions through the alpha Decisions API. */
|
|
122
|
+
function decisionModel(modelId, options) {
|
|
123
|
+
return createDecisionModel(modelId, gatewayBaseURL("alpha"), (input, init) => gatewayModelFetch(modelId, input, init, options), convexGatewayUsageMetadata);
|
|
124
|
+
}
|
|
125
|
+
function embeddingModel(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,
|
|
127
131
|
},
|
|
128
132
|
});
|
|
129
|
-
}
|
|
133
|
+
}
|
|
130
134
|
/** Image model for the AI SDK's `generateImage`. */
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
135
|
+
function imageModel(modelId) {
|
|
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
|
+
function transcriptionModel(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
|
+
function speechModel(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);
|
|
184
|
+
}
|
|
134
185
|
/** Video generation for the AI SDK's `experimental_generateVideo`. */
|
|
135
|
-
|
|
186
|
+
function videoModel(modelId) {
|
|
136
187
|
return createVideoModel(modelId, gatewayBaseURL(), gatewayFetch, convexGatewayUsageMetadata);
|
|
137
|
-
}
|
|
188
|
+
}
|
|
189
|
+
// Editors show this copy on hover over the import and the function's copy on calls and signature help.
|
|
190
|
+
/**
|
|
191
|
+
* The recommended model interface for text generation through the Convex AI gateway.
|
|
192
|
+
* Use `messages` or `responses` only for endpoint-specific features.
|
|
193
|
+
*
|
|
194
|
+
* `getServiceToken` caches and refreshes credentials within the current action,
|
|
195
|
+
* so calling this more than once in the same action is fine.
|
|
196
|
+
*/
|
|
197
|
+
const gateway = Object.assign(convexGateway, {
|
|
198
|
+
messages,
|
|
199
|
+
responses,
|
|
200
|
+
decisionModel,
|
|
201
|
+
/** @deprecated Use `decisionModel` instead. */
|
|
202
|
+
evaluationModel: decisionModel,
|
|
203
|
+
embeddingModel,
|
|
204
|
+
imageModel,
|
|
205
|
+
transcriptionModel,
|
|
206
|
+
speechModel,
|
|
207
|
+
videoModel,
|
|
208
|
+
});
|
|
209
|
+
export { gateway as convexGateway };
|
|
138
210
|
export { verifyVideoWebhook } from "./video-webhook.js";
|
|
139
211
|
//# sourceMappingURL=index.js.map
|
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,
|
|
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,mBAAmB,EAAsB,MAAM,qBAAqB,CAAC;AAC9E,OAAO,EACL,wBAAwB,GAEzB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,gBAAgB,EAA0B,MAAM,kBAAkB,CAAC;AAkB5E,MAAM,oBAAoB,GAAG,GAAG,CAAC;AACjC,i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AAC;aACH,CAAC;QACJ,CAAC;KACF,CAAC;AACJ,CAAC;AAED,iEAAiE;AACjE,SAAS,kBAAkB,CAAC,OAAe;IACzC,OAAO,wBAAwB,CAC7B,OAAO,EACP,cAAc,EAAE,EAChB,YAAY,EACZ,0BAA0B,CAC3B,CAAC;AACJ,CAAC;AAED;;;GAGG;AACH,SAAS,WAAW,CAAC,OAAe;IAClC,MAAM,QAAQ,GAAG,YAAY,CAAC;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;AAED,sEAAsE;AACtE,SAAS,UAAU,CAAC,OAAe;IACjC,OAAO,gBAAgB,CACrB,OAAO,EACP,cAAc,EAAE,EAChB,YAAY,EACZ,0BAA0B,CAC3B,CAAC;AACJ,CAAC;AAED,uGAAuG;AACvG;;;;;;GAMG;AACH,MAAM,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC,aAAa,EAAE;IAC3C,QAAQ;IACR,SAAS;IACT,aAAa;IACb,+CAA+C;IAC/C,eAAe,EAAE,aAAa;IAC9B,cAAc;IACd,UAAU;IACV,kBAAkB;IAClB,WAAW;IACX,UAAU;CACX,CAAC,CAAC;AAEH,OAAO,EAAE,OAAO,IAAI,aAAa,EAAE,CAAC;AAEpC,OAAO,EAAE,kBAAkB,EAA0B,MAAM,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type experimental_transcribe as transcribe, type ProviderMetadata } from "ai";
|
|
2
|
+
export 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
|
+
//# 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,MAAM,MAAM,kBAAkB,GAAG,OAAO,CACtC,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":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AAE;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"}
|
|
@@ -21,7 +21,7 @@ type StatusResult = {
|
|
|
21
21
|
status: "error";
|
|
22
22
|
error: string;
|
|
23
23
|
});
|
|
24
|
-
type GatewayVideoModel = Omit<VideoModel, "doStatus" | "handleWebhookOption"> & {
|
|
24
|
+
export type GatewayVideoModel = Omit<VideoModel, "doStatus" | "handleWebhookOption"> & {
|
|
25
25
|
doStart(options: VideoOptions & {
|
|
26
26
|
webhookUrl?: string;
|
|
27
27
|
}): Promise<StartResult>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"video-model.d.ts","sourceRoot":"","sources":["../../src/video-model.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,0BAA0B,EAC1B,SAAS,EACT,gBAAgB,EACjB,MAAM,IAAI,CAAC;AAEZ,KAAK,UAAU,GAAG,OAAO,CACvB,UAAU,CAAC,OAAO,0BAA0B,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,EACzD;IAAE,oBAAoB,EAAE,IAAI,CAAA;CAAE,CAC/B,CAAC;AACF,KAAK,YAAY,GAAG,UAAU,CAAC,WAAW,CAAC,UAAU,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACzE,KAAK,WAAW,GAAG,OAAO,CAAC,UAAU,CAAC,WAAW,CAAC,UAAU,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC;AAE9E,KAAK,aAAa,GAAG;IACnB,SAAS,EAAE,SAAS,CAAC;IACrB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,C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,SAAS,EAAE,SAAS,CAAA;CAAE,CAAC;AAC1E,KAAK,YAAY,GAAG;IAClB,gBAAgB,CAAC,EAAE,gBAAgB,CAAC;IACpC,QAAQ,EAAE,WAAW,CAAC,UAAU,CAAC,CAAC;CACnC,GAAG,CAAC;IAAE,MAAM,EAAE,SAAS,GAAG,WAAW,CAAA;CAAE,GAAG;IAAE,MAAM,EAAE,OAAO,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAAC,CAAC;AAC/E,MAAM,MAAM,iBAAiB,GAAG,IAAI,CAClC,UAAU,EACV,UAAU,GAAG,qBAAqB,CACnC,GAAG;IACF,OAAO,CACL,OAAO,EAAE,YAAY,GAAG;QAAE,UAAU,CAAC,EAAE,MAAM,CAAA;KAAE,GAC9C,OAAO,CAAC,WAAW,CAAC,CAAC;IACxB,SAAS,CAAC,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC;IACzD,QAAQ,CAAC,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC;CACxD,CAAC;AAoHF,wBAAgB,gBAAgB,CAC9B,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,iBAAiB,CA4GnB"}
|
package/package.json
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@convex-dev/ai-sdk-provider",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "AI SDK provider for the Convex AI gateway",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"repository": {
|
|
8
8
|
"type": "git",
|
|
9
|
-
"url": "git+https://github.com/
|
|
9
|
+
"url": "git+https://github.com/convex/convex-backend.git",
|
|
10
10
|
"directory": "npm-packages/@convex-dev/ai-sdk-provider"
|
|
11
11
|
},
|
|
12
12
|
"files": [
|
|
13
13
|
"dist",
|
|
14
14
|
"src/index.ts",
|
|
15
|
-
"src/
|
|
15
|
+
"src/decision-model.ts",
|
|
16
|
+
"src/transcription-model.ts",
|
|
16
17
|
"src/video-model.ts",
|
|
17
18
|
"src/video-webhook.ts",
|
|
18
19
|
"LICENSE"
|
|
@@ -45,12 +46,12 @@
|
|
|
45
46
|
"node": ">=22"
|
|
46
47
|
},
|
|
47
48
|
"devDependencies": {
|
|
48
|
-
"@types/node": "^
|
|
49
|
+
"@types/node": "^22.0.0",
|
|
49
50
|
"ai": "7.0.105",
|
|
50
51
|
"tshy": "^3.0.0",
|
|
51
52
|
"typescript": "~5.0.3",
|
|
52
53
|
"vitest": "^4.1.0",
|
|
53
|
-
"convex": "1.
|
|
54
|
+
"convex": "1.51.1"
|
|
54
55
|
},
|
|
55
56
|
"exports": {
|
|
56
57
|
".": {
|
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
} from "ai";
|
|
6
6
|
import { z } from "zod";
|
|
7
7
|
|
|
8
|
-
type
|
|
8
|
+
export type DecisionModel = Exclude<
|
|
9
9
|
Parameters<typeof experimental_evaluate>[0]["model"],
|
|
10
10
|
string
|
|
11
11
|
>;
|
|
@@ -33,12 +33,12 @@ const responseSchema = z.object({
|
|
|
33
33
|
}),
|
|
34
34
|
});
|
|
35
35
|
|
|
36
|
-
export function
|
|
36
|
+
export function createDecisionModel(
|
|
37
37
|
modelId: string,
|
|
38
38
|
baseURL: string,
|
|
39
39
|
fetch: typeof globalThis.fetch,
|
|
40
40
|
usageMetadata: (usage: unknown) => ProviderMetadata | undefined,
|
|
41
|
-
):
|
|
41
|
+
): DecisionModel {
|
|
42
42
|
return {
|
|
43
43
|
specificationVersion: "v4",
|
|
44
44
|
provider: "convexGateway",
|