Connection
connect(options)
await client.connect({
conversationUrl: 'https://llmor.example.com/v1/conversations/<token>',
transports: ['websocket'],
reconnection: true,
reconnectionAttempts: 5,
reconnectionDelay: 2000,
});Options
| Option | Type | Default | Notes |
|---|---|---|---|
conversationUrl | string | required | The HTTP endpoint that returns conversation metadata + relay address. |
transports | ('websocket' | 'polling')[] | ['websocket'] | Forwarded to Socket.IO. |
reconnection | boolean | true | Forwarded to Socket.IO. |
reconnectionAttempts | number | 5 | Forwarded to Socket.IO. |
reconnectionDelay | number (ms) | 2000 | Forwarded to Socket.IO. |
syncStateBeforeFunctionResponse | boolean | true | Withhold function-call responses until dirty context/function state has been flushed and acknowledged by the relay, so the LLM always sees up-to-date context. See Functions. |
syncStateBeforeFunctionResponseTimeoutMs | number (ms) | 2000 | Max time to wait for the relay acknowledgement before sending the withheld response anyway. |
contextResolutionTimeoutMs | number (ms) | 10000 | Max time a pending context resolution may withhold a function response before it is sent anyway. |
functionCallHistoryLimit | number | 50 | How many function-call timing records getFunctionCallHistory() retains. |
connect() returns a promise that resolves once the conversation metadata has been fetched and the relay connection has been initiated. The socket then connects asynchronously: getStatus() reports 'connecting' until the relay's connect event fires, at which point it flips to 'connected'. Subscribe to the status event if you need to act only once the connection is live.
disconnect()
client.disconnect();Closes the socket, clears any pending sync timers, and emits status: 'disconnected'. Safe to call when already disconnected.
If you call disconnect() (or start a new connect()) while a connect() is still fetching conversation metadata, the in-flight attempt is abandoned rather than completing. Instead of resolving silently, it emits an error event with context: 'connect_cancelled' so a caller awaiting connect() can tell it never finished.
Handling connection errors
When connect() fails, the rejection reason is an AutopilotConnectionError — a structured, display-ready error you can branch on instead of parsing a string. The same value is stored on the client and pushed via the connectionError event, so the failure survives long enough to render in a UI.
import { AutopilotConnectionError } from '@llmor/autopilot-core';
// Read the latest failure synchronously (null when healthy):
const err = client.getConnectionError();
// Or subscribe — fires with the error on failure, and with `null` when cleared
// (a fresh connect started, or a connection succeeded):
client.on('connectionError', ({ error }) => {
if (!error) return; // cleared
console.warn(error.kind, error.message, error.hint);
});
try {
await client.connect({ conversationUrl });
} catch (e) {
if (e instanceof AutopilotConnectionError && e.kind === 'network') {
// likely offline or a CORS policy blocking this origin
}
}AutopilotConnectionError
| Field | Type | Notes |
|---|---|---|
kind | 'offline' | 'network' | 'http' | 'invalid_response' | 'cancelled' | 'unknown' | Machine-readable category to branch on. |
message | string | Short, human-readable summary (no library prefix — safe to show directly). |
hint | string? | Actionable troubleshooting guidance. |
status | number? | HTTP status, present when kind === 'http'. |
url | string? | The request URL involved. |
CORS vs. network: the browser deliberately hides whether a cross-origin
fetchwas blocked by CORS or simply failed to reach the server — both surface as the same opaqueTypeError("Failed to fetch" / "Load failed" / "…due to access control checks"). The client therefore reports onenetworkkind whosehintnames both causes, and only splits outoffline(the one casenavigator.onLineexposes). The@llmor/autopilot-vueoverlay renders this as a banner with a Retry button automatically.
reconnect()
await client.reconnect();Retries the connection using the options from the most recent connect() call — handy for a "Retry" affordance after a connectionError. Rejects if connect() was never called.
isConnected() / getStatus()
client.isConnected(); // boolean
client.getStatus(); // 'disconnected' | 'connecting' | 'connected'Prefer subscribing to the status event for live updates:
const unsubscribe = client.on('status', (status) => {
document.body.dataset.autopilot = status;
});interruptStreaming()
client.interruptStreaming();Asks the server to stop the in-progress completion. The current streamingMessage is committed to the conversation, and a completionEnded event fires.
Reconnection behaviour
Socket.IO will automatically reconnect according to reconnectionAttempts and reconnectionDelay. Registered functions and context are kept on the client instance across disconnects, so you never need to re-register them: on the next connect() the client re-declares its function catalog and re-applies its local context.
If you call disconnect() explicitly, reconnection is not attempted.
Common errors
| Symptom | Likely cause |
|---|---|
connect() rejects with 404 or 401 | The conversation URL is wrong or its token has expired. |
Status flaps connecting → disconnected → connecting | The relay URL returned by the HTTP call is unreachable from the browser (CORS, firewall). |
Stuck in connecting forever | transports: ['websocket'] is set but the network blocks WS — fall back to ['websocket', 'polling']. |