Vercel AI SDK

medium10 minai-sdk-adapter.ts

Your UI is built on the Vercel AI SDK. You want Cogitator agents as AI SDK models and tools shared both ways.

What it shows

  • Use an agent as a model with cogitatorModel() in generateText and streamText
  • Convert tools with toAISDKTool() and fromAISDKTool()

The code

ai-sdk-adapter.ts
import { Agent, Cogitator, calculator } from '@cogitator-ai/core';
import { cogitatorModel, fromAISDKTool, toAISDKTool } from '@cogitator-ai/ai-sdk';
import { generateText, streamText, tool as aiTool } from 'ai';
import { z } from 'zod';

const apiKey = process.env.GOOGLE_API_KEY;
if (!apiKey) throw new Error('Set GOOGLE_API_KEY');

const cog = new Cogitator({ llm: { providers: { google: { apiKey } } } });

const chat = new Agent({
  name: 'chat',
  model: 'google/gemini-3.5-flash-lite',
  instructions: 'You are a helpful assistant. Answer in one sentence.',
  temperature: 0.3,
});

const { text, usage } = await generateText({
  model: cogitatorModel(cog, chat),
  prompt: 'What is the capital of France?',
});
console.log(text, usage.inputTokens, usage.outputTokens);

const math = new Agent({
  name: 'math',
  model: 'google/gemini-3.5-flash-lite',
  instructions: 'Use the calculator tool for arithmetic, then answer in one sentence.',
  tools: [calculator],
  temperature: 0,
});

const stream = streamText({ model: cogitatorModel(cog, math), prompt: 'What is 1234 * 5678?' });
for await (const chunk of stream.textStream) process.stdout.write(chunk);
console.log();

const aiCalculator = toAISDKTool(calculator);
console.log('AI SDK tool:', aiCalculator.description);

const greet = aiTool({
  description: 'Generate a greeting',
  inputSchema: z.object({ name: z.string() }),
  execute: async ({ name }) => `Hello, ${name}!`,
});
const greetForCogitator = fromAISDKTool(greet, 'greet');
console.log('Cogitator tool:', greetForCogitator.name, greetForCogitator.toJSON().parameters);

await cog.close();

Run it

install · bash
pnpm add @cogitator-ai/core @cogitator-ai/ai-sdk ai zod
EnvironmentGOOGLE_API_KEY
run · bash
GOOGLE_API_KEY=your-key npx tsx ai-sdk-adapter.ts

Or run the full example from a clone of the repository (keys in .env):

repository · bash
npx tsx examples/integrations/07-ai-sdk-adapter.ts

Learn more