Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:AIPexStudio AIPex ChatAdapter

From Leeroopedia
Knowledge Sources
Domains UI_Architecture, React
Last Updated 2026-10-24 12:00 GMT

Overview

Concrete adapter class that converts a stream of AgentEvents from @aipexstudio/aipex-core into a UIMessage array with typed parts for React rendering.

Description

The ChatAdapter class bridges the core agent's event-based streaming architecture and the UI's message-based rendering model. It maintains an internal state machine with messages, a current assistant message ID, and a chat status (idle, streaming, executing_tools, error). The `processEvent` method dispatches on the AgentEvent type: content deltas are appended to the current assistant message, tool calls are added as tool parts with pending/complete/error states, and status transitions are tracked. The `getMessages` method returns a snapshot of the UIMessage array. Callbacks (`onMessagesUpdate`, `onStatusChange`) notify React components of state changes, enabling re-rendering.

Usage

  • Basic adapter: create with `new ChatAdapter()` or use the `createChatAdapter` factory.
  • With callbacks: pass `onMessagesUpdate` and `onStatusChange` for reactive UI updates.
  • Processing events: call `adapter.processEvent(event)` for each event from `agent.chat()`.
  • Getting messages: call `adapter.getMessages()` to retrieve the current UIMessage array.
  • Direct message manipulation: use `addUserMessage`, `setMessages`, `reset` for initialization.

Code Reference

Source Location

  • Repository: AIPex
  • File: packages/aipex-react/src/adapters/chat-adapter.ts
  • Lines: L29-200 (class), L135-199 (processEvent)

Signature

export class ChatAdapter {
  constructor(options: ChatAdapterOptions = {})
  getMessages(): UIMessage[]
  getStatus(): ChatStatus
  setMessages(messages: UIMessage[]): void
  addUserMessage(text: string, files?: File[], contexts?: ContextItem[]): UIMessage
  processEvent(event: AgentEvent): void
  reset(initialMessages?: UIMessage[]): void
}

interface ChatAdapterOptions {
  onMessagesUpdate?: (messages: UIMessage[]) => void;
  onStatusChange?: (status: ChatStatus) => void;
}

type ChatStatus = "idle" | "streaming" | "executing_tools" | "error"

interface UIMessage {
  id: string;
  role: "user" | "assistant";
  parts: UIPart[];
  timestamp: number;
}

Import

import { ChatAdapter } from "@aipexstudio/aipex-react";
// or
import { createChatAdapter } from "@aipexstudio/aipex-react";

I/O Contract

Inputs

Name Type Required Description
options.onMessagesUpdate (messages) => void No Callback when messages change
options.onStatusChange (status) => void No Callback when status changes
event AgentEvent Yes Event from agent.chat() generator (for processEvent)

Outputs

Name Type Description
getMessages() UIMessage[] Array of messages with typed parts (text, tool, file, context)
getStatus() ChatStatus Current chat state machine status

Usage Examples

Basic Usage with Agent

import { AIPex } from "@aipexstudio/aipex-core";
import { ChatAdapter } from "@aipexstudio/aipex-react";

const agent = AIPex.create({ /* ... */ });
const adapter = new ChatAdapter({
  onMessagesUpdate: (messages) => {
    setMessages([...messages]); // React state update
  },
  onStatusChange: (status) => {
    setStatus(status);
  },
});

adapter.addUserMessage("Click the login button");

for await (const event of agent.chat("Click the login button")) {
  adapter.processEvent(event);
}

React Hook Integration

// Simplified useChat pattern
function useChat(agent: AIPex) {
  const [messages, setMessages] = useState<UIMessage[]>([]);
  const [status, setStatus] = useState<ChatStatus>("idle");
  const adapterRef = useRef(new ChatAdapter({
    onMessagesUpdate: setMessages,
    onStatusChange: setStatus,
  }));

  const sendMessage = async (text: string) => {
    adapterRef.current.addUserMessage(text);
    for await (const event of agent.chat(text)) {
      adapterRef.current.processEvent(event);
    }
  };

  return { messages, status, sendMessage };
}

Related Pages

Implements Principle

Requires Environment

Page Connections

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