createChat

Create a chat runtime and HTTP handler for Peam

Overview

Creates a DefaultChatRuntime instance with a .handler function for HTTP requests.

Type Definition

import type { LanguageModel } from 'ai';
import type { SearchEngine, SearchIndexStore } from '@peam-ai/search';
import type { ConversationSummarizer, DefaultChatRuntime, SummarizationOptions } from 'peam/server';

export interface ChatRuntimeOptions {
  /**
   * The language model to use for generating responses and summarization.
   * Defaults to OpenAI GPT-4o if not provided.
   */
  model?: LanguageModel;

  /**
   * Maximum allowed length for a single message.
   * @default 30000
   */
  maxMessageLength?: number;

  /**
   * Search index store to use for loading the search index.
   */
  searchIndexStore?: SearchIndexStore;

  /**
   * SearchEngine to use for retrieving relevant documents.
   */
  searchEngine?: SearchEngine;

  /**
   * Options for message summarization.
   */
  summarization?: SummarizationOptions | false;

  /**
   * Custom summarizer implementation.
   */
  summarizer?: ConversationSummarizer;
}

export declare function createChat(options?: ChatRuntimeOptions): DefaultChatRuntime;

Request Body

The handler expects the following JSON payload:

import type { UIMessage } from 'ai';

export interface Summary {
  text: string;
  lastSummarizedMessageId: string;
}

export interface ChatRequestBody {
  messages: UIMessage[];
  summary?: Summary;
}

Usage

Basic Usage

import { createChat } from 'peam/server';

export const POST = createChat().handler;

With Custom Model

import { createChat } from 'peam/server';
import { openai } from '@ai-sdk/openai';

export const POST = createChat({
  model: openai('gpt-4o'),
}).handler;

With Custom Search Store

import { createChat } from 'peam/server';
import { FileBasedSearchIndexStore } from '@peam-ai/search';

export const POST = createChat({
  searchIndexStore: new FileBasedSearchIndexStore({
    indexPath: '.peam/index.json',
  }),
}).handler;

Disable Summarization

import { createChat } from 'peam/server';

export const POST = createChat({
  summarization: false,
}).handler;

Notes

  • If searchIndexStore is not provided, DefaultChatRuntime uses a FileBasedSearchIndexStore pointing to .peam/index.json.
  • If searchEngine is provided, it is used directly instead of loading from a store (see getSearchEngine).
  • If summarization is not set to false, WindowedConversationSummarizer is used with maxMessages defaulting to 10.