Main SDK bundle for Codmir - unified API for all features

@codmir/sdk

The main Codmir SDK package that bundles all public APIs into a single, easy-to-use interface.

Installation

npm install @codmir/sdk
# or
pnpm add @codmir/sdk
# or
yarn add @codmir/sdk

Quick Start

import { CodmirClient } from '@codmir/sdk';

const codmir = new CodmirClient({
  apiKey: process.env.CODMIR_API_KEY,
});

API Reference

Constructor

new CodmirClient(config?: CodmirClientConfig)
OptionTypeRequiredDescription
apiKeystringNoSent as a Bearer token; falls back to CODMIR_API_KEY
baseUrlstringNoAPI base URL (default: https://codmir.com/api/v1)
timeoutnumberNoRequest timeout in ms (default: 30000)
headersRecord<string, string>NoExtra headers merged into every request

Chat API

Send a conversation to the chat endpoint.

const response = await codmir.chat({
  messages: [{ role: 'user', content: 'Help me debug this error' }],
});

// On success the body is a stream, not JSON: lines in the Vercel AI SDK data
// stream protocol, e.g. `0:"token"` for each text chunk. A chunk boundary can
// land mid-line or mid-character, so decode with `{ stream: true }` and keep
// the trailing partial line in a buffer between reads.
const reader = response.body!.getReader();
const decoder = new TextDecoder();
let text = '';
let buffer = '';
for (let chunk = await reader.read(); !chunk.done; chunk = await reader.read()) {
  buffer += decoder.decode(chunk.value, { stream: true });
  const lines = buffer.split('\n');
  buffer = lines.pop() ?? ''; // last entry may be an incomplete line
  for (const line of lines) {
    if (line.startsWith('0:')) text += JSON.parse(line.slice(2));
    // Other part prefixes (e.g. `3:` error, `d:` finish) are skipped.
  }
}
buffer += decoder.decode(); // flush any trailing bytes
if (buffer.startsWith('0:')) text += JSON.parse(buffer.slice(2));

chat() returns the raw fetch Response - there is no exported type for its body - and throws CodmirApiError if the HTTP response isn't ok, rather than resolving with an error body. There is no chat.stream(); @codmir/sdk has no streaming helper for chat responses.

To manage conversation history, use listConversations, createConversation, getConversation and deleteConversation.

Events API

The SDK bundle doesn't expose an events namespace on its client. Publish and read events with the EventClient from @codmir/events instead:

import { EventClient } from '@codmir/events';

const events = new EventClient({
  serviceUrl: process.env.CODMIR_EVENTS_URL,
  apiKey: process.env.CODMIR_TOKEN,
});

// Publish an event
await events.publish({
  type: 'ci.workflow.completed',
  data: { workflowName: 'Build', conclusion: 'success', duration: 120000 },
});

// Read recent events of a type
const recent = await events.getEvents('ci.workflow.failed', 100);

There's no push-based subscribe API. To react to events as they happen, build an event-triggered workflow (EventWorkflow) instead of subscribing from client code.

Projects API

Manage projects.

// List projects
const projects = await codmir.listProjects();

// Get a project
const project = await codmir.getProject('project-id');

// Create a project
const newProject = await codmir.createProject({
  name: 'My Project',
  accountId: 'account-id',
});

accountId is the id of the organization the project belongs to. If you don't already have it, call codmir.getWorkspaceStatus() and use the id of the entry you want from organizations, or call codmir.getUserData() and use the id of the entry you want from accounts. Skip the Personal Account entry (isPersonal: true) unless it is the only one: when you also belong to other organizations its id is your user id, and createProject rejects that with a 403.

Tickets API

Manage tickets.

// List tickets for a project
const tickets = await codmir.listTickets('project-id', { status: 'open' });

// Create a ticket
const ticket = await codmir.createTicket({
  projectId: 'project-id',
  title: 'Fix authentication bug',
  description: 'Users cannot login...',
});

// Update a ticket
await codmir.updateTicket('ticket-id', {
  status: 'in_progress',
});

Re-exported Packages

@codmir/sdk re-exports CodmirClient (the class itself is defined in this package) plus its own type definitions, and re-exports the Overseer error-tracking namespace:

import {
  CodmirClient,
  Overseer,
  type ChatMessage,
  type Project,
  type Ticket,
} from '@codmir/sdk';

It does not re-export anything from @codmir/events or @codmir/client — those are separate packages. For CIEventTypes and createCIWorkflowEvent, see @codmir/events directly.

Environment Variables

VariableDescription
CODMIR_API_KEYUsed as the Bearer token when apiKey isn't passed to the constructor

Error Handling

import { CodmirApiError } from '@codmir/sdk';

try {
  await codmir.chat({ messages: [{ role: 'user', content: 'Hello' }] });
} catch (error) {
  if (error instanceof CodmirApiError) {
    console.error(error.code, error.message, error.statusCode);
  }
}

CodmirApiError is the only error class the SDK throws - there are no dedicated subclasses per failure type. Its code is one of:

CodeMeaning
<server code>The server's error response included its own code; used as-is for any method, including chat()
API_ERRORAny method except chat(), when the response isn't ok and the server didn't include a code
CHAT_ERRORchat() only - used on a non-2xx response when the server didn't include a code
TIMEOUTThe request exceeded timeout, including chat()'s connection/headers phase - a streaming body already in progress is not cut off
NETWORK_ERRORThe request failed before a response came back

When present, error.details carries the server's details object and error.statusCode carries the HTTP status.

Auth and rate-limit failures also surface as a plain CodmirApiError, distinguished by statusCode (401/403 and 429 respectively).

TypeScript Support

Full TypeScript support with detailed type definitions:

import type {
  CodmirClientConfig,
  ChatRequest,
  ChatMessage,
  Project,
  Ticket,
} from '@codmir/sdk';

Next Steps