Skip to content

feat(sdk): TypeScript SDK — Vercel AI SDK adapter - #105

Merged
prashar32 merged 1 commit into
mainfrom
feat/ts-sdk-vercel-adapter
Jun 12, 2026
Merged

feat(sdk): TypeScript SDK — Vercel AI SDK adapter#105
prashar32 merged 1 commit into
mainfrom
feat/ts-sdk-vercel-adapter

Conversation

@prashar32

Copy link
Copy Markdown
Owner

Govern a Vercel AI SDK agent with ~no code change — the JS analog of the Python LangChain / OpenAI-Agents adapters.

governMiddleware(run) (exposed as @riskkernel/sdk/vercel) is an AI SDK language-model middleware: its wrapGenerate / wrapStream hooks tick one governed run.step() per model call, so the run's loop/time budget is enforced and a halt surfaces as BudgetExceeded out of generateText / streamText — not swallowed. The daemon decides; the adapter carries no governance logic.

import { generateText, wrapLanguageModel } from "ai";
import { governMiddleware } from "@riskkernel/sdk/vercel";

await rt.governedRun({ budget: { loops: 20, dollars: 1 } }, async (run) => {
  const { baseUrl, headers } = run.proxyConfig();
  const openai = createOpenAI({ baseURL: baseUrl, headers });    // $ / tokens metered by the proxy
  const model = wrapLanguageModel({ model: openai("gpt-4o-mini"), middleware: governMiddleware(run) });
  await generateText({ model, prompt });                         // loops / time enforced by the middleware
});

Keeps the core dependency-free

The adapter is typed against @ai-sdk/provider via import type only, so it's erased at build — verified the built dist/vercel.js has no runtime import of it. @ai-sdk/provider is an optional peer (tiny, types-only), needed only when you import the adapter; the core client still pulls nothing. Ships as a second tsup entry behind the ./vercel subpath export.

Tests

  • Unit tests against the in-process mock daemon (no daemon, no keys): the middleware ticks a step per call, surfaces BudgetExceeded when the loop budget is spent, and does not run the model once the step is rejected (no overspend).
  • A runnable, key-free example (examples/vercel-ai-sdk) halts a looping agent on its loop budget. It typechecks against the real ai@5 package, proving governMiddleware(run) is assignable to wrapLanguageModel's middleware. Pinned + documented to AI SDK v5.

Closes #81.


Stacked on #104 (the dev-dep security bumps) so it inherits the vitest 3 / Node 20 baseline. Review base is chore/ts-sdk-dep-bumps; GitHub will retarget this to main once #104 merges.

@prashar32
prashar32 changed the base branch from chore/ts-sdk-dep-bumps to main June 12, 2026 19:52
Add governMiddleware(run), exposed as @riskkernel/sdk/vercel — an AI SDK
language-model middleware whose wrapGenerate/wrapStream hooks tick one governed
step per model call. So a run's loop/time budget is enforced and a halt surfaces
as BudgetExceeded out of generateText/streamText instead of being swallowed. The
daemon decides; the adapter carries no governance logic — the JS analog of the
Python LangChain / OpenAI-Agents adapters.

The adapter is typed against @ai-sdk/provider via `import type` only, so it's
erased at build time and the core stays dependency-free; @ai-sdk/provider is an
optional peer, needed only when importing the adapter. Ships as a second tsup
entry behind the ./vercel subpath export.

Tests cover the enforcement path against the in-process mock daemon: the
middleware ticks a step per call, surfaces BudgetExceeded when the loop budget
is spent, and never runs the model once the step is rejected (no overspend). A
runnable, key-free example (examples/vercel-ai-sdk) halts a looping agent on its
loop budget; it typechecks against the real ai@5 package, proving the middleware
plugs into wrapLanguageModel. Pinned to AI SDK v5.
@prashar32
prashar32 force-pushed the feat/ts-sdk-vercel-adapter branch from 3fd2394 to e9c72be Compare June 12, 2026 19:55
@prashar32
prashar32 merged commit 92294da into main Jun 12, 2026
6 checks passed
@prashar32 prashar32 mentioned this pull request Jun 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TS SDK: Vercel AI SDK adapter (govern a Node agent with ~no code change)

1 participant