Skip to main content
New protocol version released: This page may contain outdated information.

API Reference

Complete reference documentation for the HAIP Server API, including all methods, events, and configuration options.

Server Class

HAIPServer

The main server class that handles all HAI Protocol operations.

Constructor

Parameters:
  • config - Configuration object (optional)
Returns: HAIPServer instance Example:

Configuration

HAIPServerConfig

FlowControlConfig

Public Methods

start()

Start the server and begin accepting connections.
Example:

stop()

Stop the server and close all connections.
Example:

registerTool()

Register a new tool with the server.
Parameters:
  • tool - Tool definition object
Example:

unregisterTool()

Remove a tool from the server.
Parameters:
  • toolName - Name of the tool to remove
Example:

getTools()

Get all registered tools.
Returns: Array of tool definitions Example:

getSession()

Get a specific session by ID.
Parameters:
  • sessionId - Session identifier
Returns: Session object or undefined Example:

getStats()

Get current server statistics.
Returns: Server statistics object Example:

broadcast()

Send a message to all connected sessions.
Parameters:
  • message - HAIP message to broadcast
Example:

sendMessage()

Send a message to a specific session.
Parameters:
  • sessionId - Target session ID
  • message - HAIP message to send
Example:

sendToolDone()

Send a tool completion message to a session.
Parameters:
  • sessionId - Target session ID
  • callId - Tool call identifier
  • status - Completion status (‘OK’, ‘ERROR’, ‘CANCELLED’)
  • result - Tool result (optional)
Example:

closeAllConnections()

Close all WebSocket connections.
Example:

Events

The HAIPServer extends EventEmitter and emits various events.

Server Events

’started’

Emitted when the server starts successfully.

‘stopped’

Emitted when the server stops.

Connection Events

’connect’

Emitted when a new client connects.

‘disconnect’

Emitted when a client disconnects.

‘handshake’

Emitted when a client completes handshake.

Message Events

’textMessage’

Emitted when a text message is received.

‘audioChunk’

Emitted when an audio chunk is received.

‘binary’

Emitted when binary data is received.

Run Events

’runStarted’

Emitted when a run starts.

‘runFinished’

Emitted when a run finishes.

‘runCancelled’

Emitted when a run is cancelled.

Tool Events

’toolCall’

Emitted when a tool is called.

Error Events

’error’

Emitted when an error occurs.

HTTP Endpoints

Health Check

Endpoint: GET /health Response:

Statistics

Endpoint: GET /stats Response:

WebSocket Endpoints

WebSocket Connection

Endpoint: ws://localhost:8080/haip/websocket?token=<jwt-token> Parameters:
  • token - JWT authentication token (required)
Example:

SSE Endpoints

Server-Sent Events

Endpoint: GET /haip/sse?token=<jwt-token> Parameters:
  • token - JWT authentication token (required)
Example:

HTTP Streaming Endpoints

HTTP Streaming

Endpoint: POST /haip/stream Headers:
  • Authorization: Bearer <jwt-token> (required)
  • Content-Type: application/json
Example:

Types

HAIPSession

HAIPServerStats

HAIPRun

HAIPToolExecution

HAIPToolDefinition

Error Codes

Authentication Errors

  • INVALID_TOKEN - JWT token is invalid or expired
  • MISSING_TOKEN - JWT token is required but not provided

Message Errors

  • INVALID_MESSAGE - Message format is invalid
  • UNSUPPORTED_TYPE - Message type is not supported

Tool Errors

  • TOOL_NOT_FOUND - Requested tool does not exist
  • TOOL_EXECUTION_ERROR - Tool execution failed

Flow Control Errors

  • INSUFFICIENT_CREDITS - Not enough credits for the channel
  • CHANNEL_PAUSED - Channel is paused

Run Errors

  • MISSING_RUN_ID - Run ID is required but not provided
  • RUN_NOT_FOUND - Run does not exist
  • MAX_RUNS_EXCEEDED - Maximum concurrent runs exceeded

Environment Variables

Required

  • JWT_SECRET - Secret key for JWT token signing

Optional

  • PORT - Server port (default: 8080)
  • HOST - Server host (default: 0.0.0.0)
  • NODE_ENV - Environment (development/production)
  • JWT_EXPIRES_IN - JWT expiration time (default: 24h)
  • MAX_CONNECTIONS - Maximum connections (default: 1000)
  • HEARTBEAT_INTERVAL - Heartbeat interval in ms (default: 30000)
  • HEARTBEAT_TIMEOUT - Heartbeat timeout in ms (default: 5000)
  • ENABLE_CORS - Enable CORS (default: true)
  • ENABLE_COMPRESSION - Enable compression (default: true)
  • ENABLE_LOGGING - Enable logging (default: true)

Flow Control

  • FLOW_CONTROL_ENABLED - Enable flow control (default: true)
  • FLOW_CONTROL_INITIAL_CREDITS - Initial credits (default: 1000)
  • FLOW_CONTROL_MIN_CREDITS - Minimum credits (default: 100)
  • FLOW_CONTROL_MAX_CREDITS - Maximum credits (default: 10000)
  • FLOW_CONTROL_CREDIT_THRESHOLD - Credit threshold (default: 200)
  • FLOW_CONTROL_BACK_PRESSURE_THRESHOLD - Back-pressure threshold (default: 0.8)
  • FLOW_CONTROL_ADAPTIVE_ADJUSTMENT - Adaptive adjustment (default: true)
  • FLOW_CONTROL_INITIAL_CREDIT_MESSAGES - Initial message credits (default: 1000)
  • FLOW_CONTROL_INITIAL_CREDIT_BYTES - Initial byte credits (default: 1048576)

Examples

Basic Server Setup

Custom Tool Implementation

Monitoring and Statistics

Next Steps