Skip to content

Connection

connect(options)

ts
await client.connect({
  conversationUrl: 'https://llmor.example.com/v1/conversations/<token>',
  transports: ['websocket'],
  reconnection: true,
  reconnectionAttempts: 5,
  reconnectionDelay: 2000,
});

Options

OptionTypeDefaultNotes
conversationUrlstringrequiredThe HTTP endpoint that returns conversation metadata + relay address.
transports('websocket' | 'polling')[]['websocket']Forwarded to Socket.IO.
reconnectionbooleantrueForwarded to Socket.IO.
reconnectionAttemptsnumber5Forwarded to Socket.IO.
reconnectionDelaynumber (ms)2000Forwarded to Socket.IO.
syncStateBeforeFunctionResponsebooleantrueWithhold 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.
syncStateBeforeFunctionResponseTimeoutMsnumber (ms)2000Max time to wait for the relay acknowledgement before sending the withheld response anyway.
contextResolutionTimeoutMsnumber (ms)10000Max time a pending context resolution may withhold a function response before it is sent anyway.
functionCallHistoryLimitnumber50How 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()

ts
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.

ts
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

FieldTypeNotes
kind'offline' | 'network' | 'http' | 'invalid_response' | 'cancelled' | 'unknown'Machine-readable category to branch on.
messagestringShort, human-readable summary (no library prefix — safe to show directly).
hintstring?Actionable troubleshooting guidance.
statusnumber?HTTP status, present when kind === 'http'.
urlstring?The request URL involved.

CORS vs. network: the browser deliberately hides whether a cross-origin fetch was blocked by CORS or simply failed to reach the server — both surface as the same opaque TypeError ("Failed to fetch" / "Load failed" / "…due to access control checks"). The client therefore reports one network kind whose hint names both causes, and only splits out offline (the one case navigator.onLine exposes). The @llmor/autopilot-vue overlay renders this as a banner with a Retry button automatically.

reconnect()

ts
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()

ts
client.isConnected(); // boolean
client.getStatus();   // 'disconnected' | 'connecting' | 'connected'

Prefer subscribing to the status event for live updates:

ts
const unsubscribe = client.on('status', (status) => {
  document.body.dataset.autopilot = status;
});

interruptStreaming()

ts
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

SymptomLikely cause
connect() rejects with 404 or 401The conversation URL is wrong or its token has expired.
Status flaps connecting → disconnected → connectingThe relay URL returned by the HTTP call is unreachable from the browser (CORS, firewall).
Stuck in connecting forevertransports: ['websocket'] is set but the network blocks WS — fall back to ['websocket', 'polling'].