Next.js Chat Example

This example builds a support chat interface using Next.js App Router with HTTP/SSE transport. This is the recommended pattern for most web applications.

What You're Building

A chat interface that:

  • Creates sessions server-side
  • Streams AI responses in real-time
  • Handles tool calls on your server
  • Shows typing status during streaming

Architecture

Rendering diagram...

Prerequisites

  • Next.js 14+ with App Router
  • Octavus account with API key
  • An agent configured in Octavus

Step 1: Install Dependencies

bash

Step 2: Configure Environment

bash

Step 3: Create the Octavus Client

typescript

Step 4: Create Session Endpoint

Sessions hold conversation state. Create one when the user opens the chat:

typescript

Protocol Note: The input object contains variables defined in your agent's protocol. For example, if your agent has COMPANY_NAME as an input variable:

typescript

Step 5: Create Trigger Endpoint

Triggers execute agent actions. The user-message trigger is the most common:

typescript

Protocol Note: Tool names and arguments are defined in your agent's protocol YAML. The tool handlers here must match those definitions.

Step 6: Build the Chat Component

tsx

Step 7: Create the Page

tsx

Protocol Integration

Your agent's protocol defines the triggers and tools. Here's how the code maps to protocol:

Triggers

yaml

The send() call maps directly:

typescript

Tools

yaml

Tool handlers receive the parameters as args:

typescript

Next Steps