Tulip Logo IconTulip
Getting Started

Quick Start

Create an agent, stream it through a server procedure, and render it with the chat components.

This page shows the smallest end-to-end shape that matches how the package is built. It uses Agents to run the agent and Chat to save the chats; create their tables first (see Services).

1. Create an agent

import "server-cli-only";

import { defineAgent } from "@tulip-systems/ai/agents/server";
import { instructions } from "./instructions";
import { openrouter } from "./providers";

export const assistantAgent = defineAgent({
  name: "assistant",
  title: "Assistant",
  model: openrouter("openai/gpt-5-mini"),
  instructions,
  tools: {},
});

Keep the agent's instructions in an instructions.ts next to the agent and export them as a string. Add tools made with your tool base to tools as they are; see Tools.

2. Register and activate it

Give the agent to Agents, and Agents to Chat:

// server/ai/init.ts
import { Agents } from "@tulip-systems/ai/agents/server";
import { Chat } from "@tulip-systems/ai/chat/server";
import { aiAgentsDrizzleAdapter, aiChatDrizzleAdapter } from "@tulip-systems/ai/drizzle";

export const agents = Agents.init({
  agents: [assistantAgent],
  database: aiAgentsDrizzleAdapter({ db, schema: { aiAgentStates, aiRuns, aiApprovals } }),
});

export const chat = Chat.init({
  agents,
  database: aiChatDrizzleAdapter({ db, schema: { aiChats, aiMessages } }),
});

Agents only run once an admin activated them for the workspace. Activate it once, from an admin page (see Manage agents) or a seed script:

await agents.setActive("assistant", true);

3. Stream the agent from your server boundary

import { streamToEventIterator, type } from "@orpc/server";
import type { AiChatRequest } from "@tulip-systems/ai/types";
import { chat } from "@/server/ai/init";
import { protectedProcedure } from "@/server/router/procedures";

export const assistantRouter = {
  send: protectedProcedure
    .input(type<AiChatRequest>())
    .handler(async ({ context, input, signal }) =>
      streamToEventIterator(
        await chat.send({
          ...input,
          user: context.user,
          agent: "assistant",
          abortSignal: signal,
        }),
      ),
    ),
};

Chat loads and saves the chat, and runs the agent's tools for the signed-in user. Validate the input with a schema in a real app (see Saving chats).

4. Render a chat client

"use client";

import { eventIteratorToUnproxiedDataStream } from "@orpc/client";
import {
  ChatComposer,
  ChatComposerInput,
  ChatComposerSubmit,
  ChatComposerToolbar,
  ChatThread,
  ChatThreadActivity,
  ChatThreadContent,
  ChatThreadMessage,
  useAssistantChat,
} from "@tulip-systems/ai/chat/client";
import { orpc } from "@/server/router/client";

export function AssistantChat({ chatId }: { chatId: string }) {
  const chat = useAssistantChat({
    chatId,
    send: async (request, { abortSignal }) =>
      eventIteratorToUnproxiedDataStream(
        await orpc.assistant.send.call(request, { signal: abortSignal }),
      ),
  });

  return (
    <ChatThread status={chat.status} answerApproval={chat.answerApproval}>
      <ChatThreadContent>
        {chat.messages.map((message) => (
          <ChatThreadMessage key={message.id} message={message} />
        ))}
        <ChatThreadActivity />
      </ChatThreadContent>

      <ChatComposer
        value={chat.input}
        onValueChange={chat.setInput}
        onSubmit={chat.submit}
        onStop={chat.stop}
        status={chat.status}
      >
        <ChatComposerInput />
        <ChatComposerToolbar>
          <ChatComposerSubmit />
        </ChatComposerToolbar>
      </ChatComposer>
    </ChatThread>
  );
}

Start a new chat with crypto.randomUUID() as its id, and render the component with key={chatId}. To open a saved chat, pass its messages from chat.get() to useAssistantChat.

ChatComposer keeps the textarea editable while the agent is responding, but submit is blocked until the response is done. Tools render with a default card that shows their title; see Chat for labels, approvals and custom tool cards.

On this page