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

url
string
required
The server URL to connect to. Supports WebSocket, SSE, and HTTP streaming endpoints.
token
string
required
JWT authentication token for server authentication.
transport
'websocket' | 'sse' | 'http-streaming'
default:"'websocket'"
The transport protocol to use for communication.
flowControl
HAIPFlowControlConfig
Flow control configuration for managing message back-pressure.
maxConcurrentRuns
number
default:"5"
Maximum number of concurrent runs allowed.
maxReconnectAttempts
number
default:"5"
Maximum number of reconnection attempts on connection loss.
reconnectDelay
number
default:"1000"
Initial delay between reconnection attempts (uses exponential backoff).
heartbeatInterval
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.
threadId
string
Optional thread identifier for grouping related runs.
metadata
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.
runId
string
required
The ID of the run to finish.
status
'OK' | 'CANCELLED' | 'ERROR'
default:"'OK'"
The final status of the run.
summary
string
Optional summary text describing the run outcome.

cancelRun()

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

Messaging

sendTextMessage()

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

sendMessage()

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

sendBinary()

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

Tool Integration

callTool()

Initiates a tool call.
channel
HAIPChannel
required
The channel for the tool call.
tool
string
required
The name of the tool to call.
params
Record<string, any>
Parameters to pass to the tool.
runId
string
Optional run ID to associate with the tool call.
threadId
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.
channel
string
required
The channel to update flow control for.
addMessages
number
Number of message credits to add.
addBytes
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.
fromSeq
string
required
Starting sequence number for replay.
toSeq
string
Ending sequence number for replay (optional).

Audio Support

sendAudioChunk()

Sends an audio chunk.
channel
HAIPChannel
required
The channel for the audio data.
messageId
string
required
Unique identifier for the audio message.
mime
string
required
MIME type of the audio data.
data
ArrayBuffer
required
The audio data to send.
durationMs
number
Duration of the audio chunk in milliseconds.
runId
string
Optional run ID to associate with the audio.
threadId
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.