コンテンツにスキップ

Vercel AI SDK

In AI SDK routes, @vel4ai/ai-sdk can create the OpenAI-compatible provider/model from a prepared Vela execution. Vela-managed relay endpoints record their own execution results, so the app does not attach report callbacks for the same relay session.

Prepare with the core SDK, then pass the relay-backed model from createVelaOpenAI() to AI SDK.

import { createVelaOpenAI } from "@vel4ai/ai-sdk";
import { createVelaIntegrationFromEnvironment } from "@vel4ai/sdk";
import { streamText } from "ai";
declare const process: { env: Record<string, string | undefined> };
declare const messages: unknown[];
export async function runStream() {
const vela = createVelaIntegrationFromEnvironment({ env: process.env });
const prepared = await vela.prepareExecutionWithResponse({
estimatedCostUsd: 0.05,
});
if (prepared.outcome !== "allow") {
return prepared.response;
}
return streamText({
model: createVelaOpenAI(prepared).model,
messages,
});
}

In routes that should avoid loading the AI SDK runtime in setup or deny paths, prepare with the core SDK first, then import @vel4ai/ai-sdk and ai only after allow.

import { createVelaIntegrationFromEnvironment } from "@vel4ai/sdk";
declare const process: { env: Record<string, string | undefined> };
declare const messages: unknown[];
export async function runAllowOnlyStream() {
const vela = createVelaIntegrationFromEnvironment({ env: process.env });
const prepared = await vela.prepareExecutionWithResponse({
estimatedCostUsd: 0.05,
});
if (prepared.outcome !== "allow") {
return prepared.response;
}
const [{ createVelaOpenAI }, { streamText }] = await Promise.all([
import("@vel4ai/ai-sdk"),
import("ai"),
]);
return streamText({
model: createVelaOpenAI(prepared).model,
messages,
});
}
HelperPurpose
createVelaOpenAI()Create an AI SDK OpenAI provider/model from a prepared execution
createVelaStreamCallbacks()Attach AI SDK callbacks to an existing app-owned report target
createVelaStreamTextCallbacks()Create callbacks from a prepared execution for app-owned reporting
createVelaStreamTextOptions()Get model and callbacks together for app-owned reporting
createVelaStreamTextInput()Merge app-owned streamText() fields with Vela-controlled options for compatibility paths