Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Ucbepic Docetl ChatRoute

From Leeroopedia
Revision as of 16:59, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Ucbepic_Docetl_ChatRoute.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Knowledge Sources
Domains Frontend, React_UI
Last Updated 2026-02-08 00:00 GMT

Overview

Concrete tool for the Next.js API route that provides AI chat functionality for the DocETL playground assistant.

Description

This API route handles chat requests from the AIChatPanel and PromptImprovementDialog components. It supports both Azure OpenAI and direct OpenAI backends, with automatic message truncation to stay within token limits (120K tokens / 500K characters). The route extracts the longest message for middle-section truncation, supports personal API keys via request headers, and tracks usage via Supabase. It conditionally sets temperature to 1 for GPT-5 models.

Usage

Called by the frontend chat components via useChat from the Vercel AI SDK. The route streams responses back to the client.

Code Reference

Source Location

Signature

export async function POST(req: Request): Promise<Response>

// Internal helper:
function truncateMessages(messages: ChatMessage[]): ChatMessage[]

Import

// This is an API route. Called via:
fetch("/api/chat", { method: "POST", body: JSON.stringify({ messages }) })

I/O Contract

Inputs (Props)

Name Type Required Description
messages ChatMessage[] Yes Array of chat messages with role and content
x-openai-key string (header) No Personal OpenAI API key
x-use-openai string (header) No Set to "true" to use OpenAI instead of Azure
x-namespace string (header) No User namespace for tracking
x-source string (header) No Source identifier (e.g., "ai_chat")

Outputs

Name Type Description
stream ReadableStream Streamed AI chat response

Usage Examples

const { messages, handleSubmit } = useChat({
  api: "/api/chat",
  headers: {
    "x-use-openai": "true",
    "x-openai-key": myApiKey,
    "x-namespace": "my-namespace",
    "x-source": "ai_chat",
  },
});

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment