ContextGuides

Vercel AI SDK

Spread withSkills(skills) into generateText, streamText or a ToolLoopAgent.

@viewportai/context/ai-sdk works with ai 7 (and 6). TypeScript only; in Python, see Pydantic AI.

const skills = await loadSkills("../quickstart/skills", { tools: [lookupOrder, issueRefund, searchDocs] });
const model = scriptedModel(script(load("refunds"), call("lookup_order", { order_id: "A-1001" }), say("Refunded.")));
const prompt = "My order A-1001 arrived damaged.";

const result = await generateText({ model, prompt, stopWhen: isStepCount(10), ...withSkills(skills) });

withSkills(skills) returns { tools, activeTools, prepareStep }:

  • tools holds load_skill and every registry tool. Each call runs through the conversation's session, so the guard refuses an unbound call even if a provider ignores activeTools.
  • prepareStep rebuilds the session from the step's messages (the transcript is the state), runs triggers and the router on the first step, and returns activeTools (bound tools, in Context's stable order) and instructions (yours, then Context's system prompt).
  • Your own prepareStep still runs: pass it as withSkills(skills, { prepareStep }). activeTools merge by intersection, instructions are yours then Context's, every other field is yours.

One spread serves every user: two concurrent generateText calls never share a session.

Keep history on the server. The session is rebuilt from the messages you pass, and Context treats them as the model's own actions. If a browser posts the whole conversation (the default useChat setup), a client can forge load_skill calls in it: that can load any skill inside allow the model could have asked for, never a trigger skill. Store the conversation server-side, or pass only the new user message and rebuild from your stored copy. Tool calls in the history are never replayed, so a tool_called trigger fires only in the step that ran the tool and is not kept on later steps; use input triggers or code loads with this adapter. See Known limits.

Options. tools for AI SDK tools outside the registry (always active), expose: "deferred" with @ai-sdk/anthropic to declare every bindable tool with Anthropic defer_loading, and stateFrom to read code or router loads from runtimeContext. coreTools(record) converts existing AI SDK tools so skills can bind them while keeping their schemas, toModelOutput and provider options.

Testing. scriptedModel(script(load("refunds"), call(...), say(...))) is an AI SDK language model that replays a Context script with no network; model.calls records what each step offered.