NIN Connect Docs
Providers

Vercel AI SDK

Use NIN Connect tools with the Vercel AI SDK — generateText, tool(), and jsonSchema().

Installation

npm install @nin.in/core ai @ai-sdk/openai

Environment Variables

NIN_API_KEY=nk_live_xxxxxxxxx
OPENAI_API_KEY=sk-xxxxxxxxx

Full Example

import { Nin } from "@nin.in/core";
import { generateText, tool, jsonSchema, stepCountIs } from "ai";
import { openai } from "@ai-sdk/openai";

const nin = new Nin({ apiKey: process.env.NIN_API_KEY! });
const session = nin.session("user_123");

// fetch plugins — each has pluginPrompt for LLM guidance
const plugins = await nin.getPlugins();
const gmailPlugin = plugins.find((p) => p.slug === "gmail");

// fetch tool definitions — each has name, description, and inputSchema
const toolDefs = await session.getTools({ plugins: ["gmail"] });

// build Vercel AI SDK v5 tools
// jsonSchema() passes the full param schema to the LLM so it
// knows exactly what arguments each tool expects
const tools: Record<string, ReturnType<typeof tool>> = {};
for (const t of toolDefs) {
  tools[t.slug] = tool({
    description: t.description,
    inputSchema: jsonSchema(
      t.inputSchema ?? { type: "object", properties: {} }
    ),
    execute: async (params) => session.execute(t.slug, params),
  });
}

// inject the plugin prompt into the system message if available
const systemPrompt = [
  "You are a helpful assistant.",
  gmailPlugin?.pluginPrompt,
]
  .filter(Boolean)
  .join("\n\n");

const { text } = await generateText({
  model: openai("gpt-4o"),
  system: systemPrompt,
  tools,
  prompt: "Send an email to john@example.com with subject 'Hello'",
  stopWhen: stepCountIs(10),
});

console.log(text);

How It Works

  1. session.getTools() returns tool definitions with inputSchema (JSON Schema)
  2. jsonSchema() wraps the schema so the Vercel AI SDK passes it to the LLM
  3. tool() binds description, inputSchema, and an execute function together
  4. generateText() sends tools to the LLM; when the model calls a tool, execute runs session.execute() which proxies through NIN with auth injected
  5. stopWhen: stepCountIs(10) caps the agentic loop at 10 tool-call steps

Plugin Prompt

Plugins can have a pluginPrompt field — optional text that should be injected into the system message when using that plugin's tools. This gives the LLM provider-specific guidance (e.g. "always confirm the recipient before sending an email").

const systemPrompt = [
  "You are a helpful assistant.",
  gmailPlugin?.pluginPrompt,
  slackPlugin?.pluginPrompt,
]
  .filter(Boolean)
  .join("\n\n");

On this page