New protocol version released: This page may contain outdated information.
Creating a Client
Factory Function (Recommended)
Direct Constructor
Configuration Options
The server URL to connect to. Supports WebSocket, SSE, and HTTP streaming
endpoints.
JWT authentication token for server authentication.
The transport protocol to use for communication.
Flow control configuration for managing message back-pressure.
Maximum number of concurrent runs allowed.
Maximum number of reconnection attempts on connection loss.
Initial delay between reconnection attempts (uses exponential backoff).
Interval for heartbeat ping/pong messages in milliseconds.
Connection Management
connect()
Establishes a connection to the HAIP server.Promise<void>
Events:
connect- Emitted when connection is establishedhandshake- Emitted when handshake is completederror- Emitted if connection fails
disconnect()
Gracefully disconnects from the server.Promise<void>
Events:
disconnect- Emitted when disconnection is complete
isConnected()
Checks if the client is currently connected.boolean
Run Management
startRun()
Starts a new HAIP run session.Optional thread identifier for grouping related runs.
Optional metadata to associate with the run.
Promise<string> - The generated run ID
finishRun()
Completes a run with the specified status.The ID of the run to finish.
The final status of the run.
Optional summary text describing the run outcome.
cancelRun()
Cancels an active run.The ID of the run to cancel.
Messaging
sendTextMessage()
Sends a text message on the specified channel.The channel to send the message on (‘USER’, ‘AGENT’, ‘SYSTEM’).
The text content to send.
The author of the message.
Optional run ID to associate with the message.
Optional thread ID to associate with the message.
Promise<string> - The generated message ID
sendMessage()
Sends a custom HAIP message.The HAIP message to send.
Optional message options for flow control.
sendBinary()
Sends binary data.The binary data to send.
Tool Integration
callTool()
Initiates a tool call.The channel for the tool call.
The name of the tool to call.
Parameters to pass to the tool.
Optional run ID to associate with the tool call.
Optional thread ID to associate with the tool call.
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.HAIPConnectionState
getPerformanceMetrics()
Gets performance metrics.HAIPPerformanceMetrics
getActiveRuns()
Gets all active runs.HAIPRun[]
getRun()
Gets a specific run by ID.HAIPRun | undefined
Flow Control
sendFlowUpdate()
Sends a flow control update.The channel to update flow control for.
Number of message credits to add.
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.Starting sequence number for replay.
Ending sequence number for replay (optional).
Audio Support
sendAudioChunk()
Sends an audio chunk.The channel for the audio data.
Unique identifier for the audio message.
MIME type of the audio data.
The audio data to send.
Duration of the audio chunk in milliseconds.
Optional run ID to associate with the audio.
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.