@convex-dev/ai-sdk-provider

中文文档
0.3.0发布于 2026/10/10Apache-2.0TypeScriptESM + CJS近 30 天下载 17.8万

AI SDK provider for the Convex AI gateway

原始使用文档
文档目录9 节
  1. Optional ZDR routing
  2. Choose a model interface
  3. Structured decisions
  4. Generate images
  5. Transcribe and generate speech
  6. Generate videos
  7. Async videos
  8. Receive a callback
  9. Retrieve the video

@convex-dev/ai-sdk-provider

Use the Convex AI gateway with the AI SDK from a Convex action.

"use node";

import { convexGateway } from "@convex-dev/ai-sdk-provider";
import { embedMany, generateText } from "ai";
import { action } from "./_generated/server";
import { v } from "convex/values";

export const chat = action({
  args: { prompt: v.string() },
  handler: async (_ctx, { prompt }) => {
    const { text } = await generateText({
      model: convexGateway("anthropic/claude-sonnet-4.5"),
      prompt,
    });
    return text;
  },
});

export const embed = action({
  args: { values: v.array(v.string()) },
  handler: async (_ctx, { values }) => {
    const { embeddings } = await embedMany({
      model: convexGateway.embeddingModel("openai/text-embedding-3-small"),
      values,
    });
    return embeddings;
  },
});

Embedding batches larger than the gateway's 512-input limit are split into multiple requests by the AI SDK.

Optional ZDR routing

Pass { zdr: true } to use inference endpoints with a zero-data-retention policy:

const { text } = await generateText({
  model: convexGateway("openai/gpt-4o-mini", { zdr: true }),
  prompt: "Summarize this document",
});

Also supported by messages, responses, embeddingModel, and evaluationModel. The setting applies to that model instance and overrides provider.zdr from other SDK options. Omit it to use the default policy; false cannot disable a stricter gateway policy.

Not every model has a ZDR endpoint. See routing requirements and retention limits.

Choose a model interface

For text generation, start with convexGateway(model). It works across model providers and is the recommended default. Use an endpoint-specific model only when you need features unique to Anthropic Messages or OpenAI Responses:

convexGateway.messages("anthropic/claude-sonnet-4.5");
convexGateway.responses("openai/gpt-5");

Structured decisions

Use provider version 0.2.1 or later for correct validation of Jev's rounded scores and probabilities.

Jev evaluates choice, score, and boolean questions about the context you provide in state. Call AI SDK's experimental evaluate from an action:

import { experimental_evaluate as evaluate } from "ai";
import { convexGateway } from "@convex-dev/ai-sdk-provider";

const decision = await evaluate({
  model: convexGateway.evaluationModel("typesafe/jev-1.13"),
  state: { ticket: "Customer cannot sign in" },
  questions: {
    priority: {
      type: "choice",
      instructions: "Choose the response priority",
      criteria: {
        urgent: "Respond now",
        normal: "Respond today",
      },
    },
    needsReview: {
      type: "boolean",
      instructions: "Does a human need to review this?",
    },
  },
});

console.log(decision.answers.priority.choice);
console.log(decision.answers.needsReview.probability);

This requires AI SDK 7.0.105 or later. The evaluation interface and /alpha/decisions endpoint are experimental. Authentication is handled automatically. Pass abortSignal to evaluate to cancel a request. Dollar cost is available in decision.providerMetadata?.convexGateway?.cost. The original gateway response, including provider-specific fields such as confidence, is available in decision.response.body.

getServiceToken("ai-gateway") supplies a short-lived deployment JWT. The action runtime caches and refreshes the credential as needed, so convexGateway calls it on every request.

Requires Convex 1.45 or later and AI SDK 7.0.105 or later. The provider works in the default runtime and in Node.js actions, which need Node.js 22 or later.

Generate images

Image generation is in alpha. The request and response format may change.

import { generateImage } from "ai";
import { convexGateway } from "@convex-dev/ai-sdk-provider";

const { images } = await generateImage({
  model: convexGateway.imageModel("openai/gpt-image-1"),
  prompt: "A mountain lake at sunrise",
});

Image request costs are available in each entry of result.calls, at providerMetadata?.convexGateway?.cost, in US dollars. Each call may generate multiple images; the cost is for the call, not each image.

Transcribe and generate speech

Speech-to-text and text-to-speech are in alpha.

import {
  experimental_generateSpeech as generateSpeech,
  experimental_transcribe as transcribe,
} from "ai";
import { convexGateway } from "@convex-dev/ai-sdk-provider";

const { text, providerMetadata } = await transcribe({
  model: convexGateway.transcriptionModel("openai/whisper-large-v3-turbo"),
  audio: recordingBytes,
});
const cost = providerMetadata.convexGateway?.cost;

const { audio } = await generateSpeech({
  model: convexGateway.speechModel("hexgrad/kokoro-82m"),
  text: "Your order has shipped.",
  voice: "af_heart",
});

Speech results have no cost in providerMetadata, because the cost is known only after the audio is sent. Convex still bills for speech. The charge appears on the Convex usage page.

Generate videos

Video generation is in alpha. APIs may change, and completion callbacks are best effort. Save async operation handles to check status and retrieve results.

import { experimental_generateVideo as generateVideo } from "ai";
import { convexGateway } from "@convex-dev/ai-sdk-provider";

const { video, providerMetadata } = await generateVideo({
  model: convexGateway.videoModel("google/veo-3.1"),
  prompt: "A camera pan across a mountain lake",
  duration: 8,
  aspectRatio: "16:9",
  resolution: "1280x720",
});

const bytes = video.uint8Array;
const cost = providerMetadata?.convexGateway?.cost;

The call waits for generation and download, with a 64 MiB limit per video. It fails if generation takes longer than about five minutes; use async videos for longer jobs. Store the returned bytes in Convex file storage. The AI SDK splits n > 1 into separate requests. Cancelling a request does not cancel the upstream job and can still incur a charge.

An image in the prompt becomes the first frame. Use frameImages for explicit frames, inputReferences for image/audio/video references, and generateAudio for audio. fps returns an unsupported-option warning.

providerOptions.convexGateway accepts resolution (such as 720p), generate_audio, frame_images, and input_references. Standard SDK options take precedence. Supported values depend on the OpenRouter model.

Async videos

With AI SDK 7.0.105 or later, use experimental_startVideo to submit a job without waiting for the video. See the async availability requirements before using these routes.

Create an application job first. Use its ID as requestId in the callback URL so the receiver can find the job and its saved secret.

import { experimental_startVideo as startVideo } from "ai";
import { convexGateway } from "@convex-dev/ai-sdk-provider";

async function startJob(requestId: string) {
  const modelId = "google/veo-3.1";
  const started = await startVideo({
    model: convexGateway.videoModel(modelId),
    prompt: "A camera pan across a mountain lake",
    duration: 8,
    webhookUrl: `${process.env.CONVEX_SITE_URL}/video-complete?requestId=${requestId}`,
    maxRetries: 0,
  });

  return {
    modelId,
    operation: started.operation,
    inferenceId: started.providerMetadata?.convexGateway?.inferenceId,
    webhookSecret: started.providerMetadata?.convexGateway?.webhookSecret,
  };
}

Save the returned job in private application storage before the action returns. Automatic submission retries are disabled because a lost response can still mean a paid job was accepted. Callback URLs must use the deployment's own HTTPS <deployment>.convex.site origin; redirects are rejected. If CONVEX_SITE_URL uses a custom domain, use the deployment's default convex.site origin in the example instead.

Receive a callback

Load the saved job and verify the raw request body before updating application state:

import { verifyVideoWebhook } from "@convex-dev/ai-sdk-provider";

const event = await verifyVideoWebhook({
  body: await request.text(),
  signature: request.headers.get("x-convex-video-signature"),
  secret: savedJob.webhookSecret,
});

if (event.id !== savedJob.inferenceId) {
  return new Response("Wrong job", { status: 400 });
}

Deduplicate (event.id, event.status) in the same mutation that saves the event. For completed, schedule an action to download the video. Record failed, cancelled, or expired as terminal failures. Return 204 after saving the event; return a non-2xx response if the job's secret has not been saved yet.

Retrieve the video

In a later action, use the saved operation to check status and download:

const model = convexGateway.videoModel(savedJob.modelId);
const status = await model.getStatus({ operation: savedJob.operation });

if (status.status === "completed") {
  const result = await model.download({ operation: savedJob.operation });
  const video = result.videos[0];
  const cost = result.providerMetadata?.convexGateway?.cost;
}

getStatus returns pending, completed, or error. When the status is error, the error field contains the error message. Store the downloaded video in application storage, since each download call fetches it again. Operations expire after seven days; upstream video retention may be shorter.

Omit webhookUrl to use status checks alone. If a completion callback is missed, use the saved operation to check the job and retrieve its result. Applications that need automatic recovery can periodically check unfinished jobs.

版本与发布记录

@convex-dev/ai-sdk-provider 共发布 7 个版本,首次发布于 2026/08/20;过去 12 个月发布了 7 个版本。

版本发布日期距今
0.3.0latest2026/10/101 个月前
0.2.12026/09/221 个月前
0.2.02026/09/221 个月前
0.2.0-alpha.1alpha2026/09/052 个月前
0.2.0-alpha.02026/08/292 个月前
0.1.02026/08/222 个月前

关于 @convex-dev/ai-sdk-provider 的常见问题

@convex-dev/ai-sdk-provider 是什么?

AI SDK provider for the Convex AI gateway

如何安装 @convex-dev/ai-sdk-provider?

在项目目录运行 npm install @convex-dev/ai-sdk-provider,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:ai(^7.0.105)、convex(^1.45.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@convex-dev/ai-sdk-provider 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@convex-dev/ai-sdk-provider 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@convex-dev/ai-sdk-provider 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@convex-dev/ai-sdk-provider 可以商用吗?

@convex-dev/ai-sdk-provider 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@convex-dev/ai-sdk-provider 的下载量有多少?

根据 npm 官方下载统计,@convex-dev/ai-sdk-provider 在 2026/10/11 – 2026/11/09 这 30 天内被下载 177,731 次,日均约 5,924 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@convex-dev/ai-sdk-provider 还在维护吗?

最新版本 0.3.0 发布于 2026/10/10(1 个月前);至今共发布 7 个版本,其中过去 12 个月发布了 7 个;首次发布于 2026/08/20。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。