GitHub 37
Skip to content

First Agent

Add an offline greeting Agent to your app, call it from a server route, and check its response.

Add an Agent that greets a name and returns JSON. An Agent Definition declares the work and its Driver runs it. Here the Driver is a plain function, so it runs offline with no model account or credentials.

Complete Installation, check /api/health, and continue in that app with Node.js 24.15 or newer and pnpm.

Prefer to skip the setup? Run this tutorial in your browser with a live editor and preview.

Install the Agent package:

commands/install-agent
pnpm add @vite-hub/agent

The file you create next in server/agents/ turns Agents on. You do not need to set agent or workflow for this inline Agent. Keep your other ViteHub options.

Nuxt
nuxt.config.ts
export default defineNuxtConfig({
  modules: ['vite-hub/nuxt'],
})
Nitro
vite.config.ts
import { defineConfig } from "vite"
import { nitro } from 'nitro/vite'
import { vitehub } from "vite-hub"

export default defineConfig({
  plugins: [
    vitehub(),
    nitro(),
  ],
})
Vite
vite.config.ts
import { defineConfig } from "vite"
import { vitehub } from "vite-hub"

export default defineConfig({
  plugins: [vitehub()],
})

Create server/agents/greeting.ts. ViteHub names the Agent greeting from its file path. runtime: false runs it inline. The function Driver gets the name as prompt and returns text. In a chat or vitehub agent dev, the Driver gets the user message text as prompt.

server/agents/greeting.ts
import { defineAgent } from "vite-hub/agent"

export default defineAgent({
  runtime: false,
  driver: {
    run({ prompt }) {
      const name = typeof prompt === "string" ? prompt : "friend"

      return {
        text: `Hello, ${name}. This result came from an Agent Invocation.`,
      }
    },
  },
})

Create server/api/greet.post.ts. Nitro and Nuxt serve it at POST /api/greet. The explicit H3 imports work without auto-imports.

server/api/greet.post.ts
import { defineEventHandler, readBody } from "h3"
import { runAgent } from "vite-hub/agent"
import greeting from "../agents/greeting"

export default defineEventHandler(async (event) => {
  const body = await readBody<{ name?: string }>(event)
  const [error, result] = await runAgent(greeting, {
    prompt: body?.name?.trim() || "friend",
  })

  if (error) throw error
  return { text: result.text }
})

runAgent() creates a memo cache, waits for background work to finish, and returns [undefined, result] on success or [error, undefined] on failure. Check error before reading result.text.

When a run needs request metadata or host bindings, pass the public getRuntimeContext(event) adapter as runAgent(agent, input, { runtimeContext: getRuntimeContext(event) }). This greeting needs only prompt.

Restart the app to load the new configuration:

commands/dev
pnpm dev

In another terminal, send a name:

commands/request
curl -X POST http://localhost:3000/api/greet \
  -H 'content-type: application/json' \
  -d '{"name":"Ada"}'

The route returns this JSON. It proves that the route called the function Driver without a model request:

output/response.json
{"text":"Hello, Ada. This result came from an Agent Invocation."}

Sending {} greets friend.

To check the production server of the Vite app, stop pnpm dev, build and start the server, and repeat the request:

commands/build
pnpm build
pnpm start

In Nuxt, run pnpm build and node .output/server/index.mjs instead.

A model Driver needs network access and a billed provider account. To use a model string through Vercel AI Gateway, install ai and set AI_GATEWAY_API_KEY in your server environment. Keep the key out of client code and version control. Each request can cost money.

For model output, replace driver.run with driver.model and driver.instructions. Agent Drivers shows the supported forms. Model replies vary, so the exact greeting above works only as the offline check.

Build a chat app adds a streaming chat UI with an offline Driver and an optional model Driver. Read Capabilities when your Agent needs tools or stored data.

Deploy an Agent on Cloudflare and Vercel checks the same inline Agent on two hosts. Choose a TypeScript Agent framework compares execution and state models.