Skip to main content
New protocol version released: This page may contain outdated information.
The HAIP client is the main interface for interacting with HAIP servers. It manages connections, handles protocol state, and provides methods for sending messages and managing runs.

Creating a Client

Direct Constructor

Configuration Options

string
required
The server URL to connect to. Supports WebSocket, SSE, and HTTP streaming endpoints.
string
required
JWT authentication token for server authentication.
'websocket' | 'sse' | 'http-streaming'
default:"'websocket'"
The transport protocol to use for communication.
HAIPFlowControlConfig
Flow control configuration for managing message back-pressure.
number
default:"5"
Maximum number of concurrent runs allowed.
number
default:"5"
Maximum number of reconnection attempts on connection loss.
number
default:"1000"
Initial delay between reconnection attempts (uses exponential backoff).
number
default:"30000"
Interval for heartbeat ping/pong messages in milliseconds.

Connection Management

connect()

Establishes a connection to the HAIP server.
Returns: Promise<void> Events:
  • connect - Emitted when connection is established
  • handshake - Emitted when handshake is completed
  • error - Emitted if connection fails

disconnect()

Gracefully disconnects from the server.
Returns: Promise<void> Events:
  • disconnect - Emitted when disconnection is complete

isConnected()

Checks if the client is currently connected.
Returns: boolean

Run Management

startRun()

Starts a new HAIP run session.
string
Optional thread identifier for grouping related runs.
Record<string, any>
Optional metadata to associate with the run.
Returns: Promise<string> - The generated run ID

finishRun()

Completes a run with the specified status.
string
required
The ID of the run to finish.
'OK' | 'CANCELLED' | 'ERROR'
default:"'OK'"
The final status of the run.
string
Optional summary text describing the run outcome.

cancelRun()

Cancels an active run.
string
required
The ID of the run to cancel.

Messaging

sendTextMessage()

Sends a text message on the specified channel.
HAIPChannel
required
The channel to send the message on (‘USER’, ‘AGENT’, ‘SYSTEM’).
string
required
The text content to send.
string
The author of the message.
string
Optional run ID to associate with the message.
string
Optional thread ID to associate with the message.
Returns: Promise<string> - The generated message ID

sendMessage()

Sends a custom HAIP message.
HAIPMessage
required
The HAIP message to send.
HAIPMessageOptions
Optional message options for flow control.

sendBinary()

Sends binary data.
ArrayBuffer
required
The binary data to send.

Tool Integration

callTool()

Initiates a tool call.
HAIPChannel
required
The channel for the tool call.
string
required
The name of the tool to call.
Record<string, any>
Parameters to pass to the tool.
string
Optional run ID to associate with the tool call.
string
Optional thread ID to associate with the tool call.
Returns: Promise<string> - The generated call ID

updateTool()

Updates the status of a tool call.

completeTool()

Completes a tool call with results.

cancelTool()

Cancels a tool call.

Event Handling

The client extends EventEmitter and provides the following events:

Connection Events

Message Events

Error Events

State Management

getConnectionState()

Gets the current connection state.
Returns: HAIPConnectionState

getPerformanceMetrics()

Gets performance metrics.
Returns: HAIPPerformanceMetrics

getActiveRuns()

Gets all active runs.
Returns: HAIPRun[]

getRun()

Gets a specific run by ID.
Returns: HAIPRun | undefined

Flow Control

sendFlowUpdate()

Sends a flow control update.
string
required
The channel to update flow control for.
number
Number of message credits to add.
number
Number of byte credits to add.

Channel Control

pauseChannel()

Pauses message flow on a channel.

resumeChannel()

Resumes message flow on a channel.

Replay Support

requestReplay()

Requests message replay from a specific sequence number.
string
required
Starting sequence number for replay.
string
Ending sequence number for replay (optional).

Audio Support

sendAudioChunk()

Sends an audio chunk.
HAIPChannel
required
The channel for the audio data.
string
required
Unique identifier for the audio message.
string
required
MIME type of the audio data.
ArrayBuffer
required
The audio data to send.
number
Duration of the audio chunk in milliseconds.
string
Optional run ID to associate with the audio.
string
Optional thread ID to associate with the audio.

Error Handling

The client provides comprehensive error handling:

Next Steps

Transports

Learn about different transport options and configurations.

Examples

See practical examples of client usage.

Types

Explore the complete type definitions.

API Reference

Full API reference documentation.