Skip to main content
The @mariozechner/pi-agent-core package supports custom transport implementations for routing agent requests to different backends.

Overview

By default, the agent uses the streamSimple() function from @mariozechner/pi-ai to call LLM APIs directly. For custom architectures (proxies, edge workers, microservices), you can provide a custom streamFn.

StreamFn Type

Your custom stream function must:
  1. Accept the same parameters as streamSimple()
  2. Return an AssistantMessageEventStream (or Promise of one)
  3. Emit standard events: text, thinking, tool_call, usage, stop, error

Example: HTTP Proxy

Example: WebSocket Transport

Example: Mock Transport (Testing)

Event Stream Contract

Your custom transport must emit events in this order:
  1. Streaming phase: Emit text, thinking, and tool_call events as content arrives
  2. Usage phase: Emit one usage event with token statistics
  3. Stop phase: Emit one stop event with stop reason
  4. Close stream: Call stream.close()
If an error occurs, emit an error event and call stream.error(error).

Example Event Sequence

Cancellation Support

Your transport should respect the AbortSignal:

Backend Implementation

If you’re building a custom backend that receives requests from your transport: