The TypeScript SDK is a client for the `/v1` API with session handles, streaming, pagination, and typed errors.

## Install

```bash
npm install @ellipsis-dev/sdk
```

## Start a session

```typescript
import { Ellipsis } from '@ellipsis-dev/sdk';

const client = new Ellipsis({
  apiKey: process.env.ELLIPSIS_API_TOKEN!,
});
const handle = await client.sessions.run({
  claude_code: {
    model: 'claude-opus-5-5',
    prompt: 'Run the tests and report failures.',
  },
  environment: 'api-environment',
  conversation: { interactive: false },
  budget: { session: 3 },
});
const ended = await handle.wait({ timeoutMs: 900_000 });
console.log(ended?.status, ended?.error);
```

`run` returns a handle while work continues. `wait` polls until the session is `idle` or `closed` and returns how the prompt's message ended (`completed`, `failed`, `stopped`, or `cancelled`), or `null` when the session has no message. A timeout throws and leaves the session running.

Use `await client.sessions.handle(sessionId)` to attach to an existing session.

## Continue a conversation

Leave `conversation.interactive` enabled to accept messages:

```typescript
const conversation = await client.sessions.run({
  claude_code: {
    model: 'claude-opus-5-5',
    prompt: 'Investigate the failing validation test.',
  },
  environment: 'api-environment',
  budget: { session: 5 },
});
await conversation.wait();
const { message } = await conversation.send('Add a regression test.', {
  idempotencyKey: 'add-test',
});
const ended = await conversation.wait();
console.log(message.id, ended?.status);
```

`send` returns the message, and `wait` then waits for it to be answered. The same message key is accepted once per session. A message sent while the agent is working is answered after the one it is on. `stop()` ends the message the agent is on with `stopped` and cancels queued ones. It fails with `409` when the session is `idle` or `closed`.

## Start an agent session

Start an agent session as written:

```typescript
const { session } = await client.agents.start('test-repair');
const handle = await client.sessions.handle(session.id);
const ended = await handle.wait();
console.log(ended?.status, ended?.error);
```

Send any field of a session start to replace the agent's value for that session:

```typescript
await client.agents.start('test-repair', { budget: { session: 5 } });
```

Pass `null` to remove the agent's value so its default applies. Leave a field out to keep the agent's value.

For an agent with an input schema, pass `input`:

```typescript
await client.agents.start('classify-change', {
  input: { description: 'Reject expired reset tokens' },
});
```

## Stream a session

See [Session events](/docs/api/session-events) for every modeled event and complete JSON examples.

In Node.js, install `ws` and connect it to the stream adapter:

```typescript
import WebSocket from 'ws';
import { streamSession, type OpenSocket } from '@ellipsis-dev/sdk/stream';

const token = process.env.ELLIPSIS_API_TOKEN!;
const openSocket: OpenSocket = ({ sessionId, query }) => {
  const path = '/v1/sessions/' + encodeURIComponent(sessionId) + '/stream';
  const ws = new WebSocket('wss://api.ellipsis.dev' + path + '?' + query, {
    headers: { authorization: 'Bearer ' + token },
  });
  return {
    onOpen: (cb) => ws.on('open', cb),
    onMessage: (cb) => ws.on('message', (raw) => cb(raw.toString())),
    onClose: (cb) => ws.on('close', (code) => cb(code)),
    onError: (cb) => ws.on('error', (error) => cb(error)),
    close: () => ws.close(),
  };
};

const outcome = await streamSession({
  sessionId: handle.id,
  openSocket,
  onFrame: (frame) => console.log(frame.type),
});
console.log(outcome.type);
```

Pass the adapter's query unchanged. The SDK handles reconnects and resume cursors. Complete records persist; partial text deltas are live-only.

## Read records

```typescript
for await (const record of await client.sessions.records(handle.id)) {
  if (record.kind === 'platform') {
    console.log(record.record_type, record.payload);
  } else if (record.kind === 'claude_code') {
    console.log(record.payload);
  } else if (record.kind === 'codex' || record.kind === 'unknown') {
    console.log(record.record_format, record.payload);
  }
}
```

Native records preserve their original payloads. Handle unknown variants so new event types do not break your consumer.

## Pagination and errors

```typescript
import { APIError } from '@ellipsis-dev/sdk';

try {
  for await (const session of await client.sessions.list({ days: 7 })) {
    console.log(session.id, session.status);
  }
} catch (error) {
  if (!(error instanceof APIError)) throw error;
  console.error(error.status, error.code);
}
```

Manual pagination exposes `items`, `hasMore`, and `nextCursor`. API fields retain their wire names; SDK options use camelCase.

The client defaults to a 60-second request timeout and 2 retries for transport errors, `429`, `502`, `503`, and `504`. Configure `timeoutMs` and `maxRetries` when creating it. Each session start and message gets its own `idempotency_key` unless you pass one, so a retry never starts a second session or sends a message twice.
