CADPrk6a…krfpump
habitat
Browse the docs

Developers

TypeScript SDK

A single dependency-free file that wraps the whole API — typed agents, knowledge, tools, analytics and streaming chat as an async iterator.

The SDK lives in this repository at sdk/typescript/src/index.ts. It's one file with no dependencies, and runs anywhere with fetch and ReadableStream: Node 18+, Bun, Deno, edge runtimes and modern browsers. It isn't published to npm yet — copy the file into your project.

import { HabitatClient } from "./habitat";

const habitat = new HabitatClient({
  apiKey: process.env.HABITAT_API_KEY!,
  baseUrl: "https://YOUR-HABITAT",
});
OptionDescription
apiKeyRequired. A key from Account → Developers
baseUrlYour Habitat origin (defaults to http://localhost:8800)
fetchOptional custom fetch (proxies, tests)
headersExtra headers for every request

Agents#

const { data, total, hasMore } = await habitat.agents.list({ status: "published", limit: 20 });

const atlas = await habitat.agents.create({
  name: "Atlas",
  category: "research",
  tagline: "A research companion that cites its sources.",
  config: { personality: { preset: "research", purpose: "Answer from the knowledge base." }, tools: { enabled: ["web_search"] } },
});

await habitat.agents.update(atlas.id, { config: { model: { creativity: 0.3 } } });
const { data: live, firstPublish, version } = await habitat.agents.publish(atlas.id, "unlisted");
const same = await habitat.agents.get("atlas"); // UUID or handle → OwnerAgent | PublicAgent
await habitat.agents.unpublish(atlas.id);

agents.get() returns access: "owner" (with config) for your agents and access: "public" for other people's published agents — narrow on access in TypeScript.

Knowledge#

await habitat.knowledge.add(atlas.id, { type: "text", title: "Notes", body: "…" });
await habitat.knowledge.add(atlas.id, { type: "faq", title: "FAQ", items: [{ q: "Hours?", a: "9–5 CET" }] });
await habitat.knowledge.add(atlas.id, { type: "url", url: "https://example.com/docs", maxPages: 10 });
await habitat.knowledge.add(atlas.id, { type: "file", file: pdfBlob, filename: "handbook.pdf" });

// Indexing is asynchronous
let sources = await habitat.knowledge.list(atlas.id);
while (sources.some((s) => s.status === "queued" || s.status === "processing")) {
  await new Promise((r) => setTimeout(r, 1000));
  sources = await habitat.knowledge.list(atlas.id);
}

Chat#

chat() returns an async iterable of chat events. The request starts when you begin iterating.

let conversationId: string | null = null;
let answer = "";
for await (const event of habitat.chat("atlas", { message: "What should I read next?" })) {
  switch (event.type) {
    case "meta":
      conversationId = event.conversationId;
      break;
    case "tool_call":
      console.log(`→ ${event.label}`);
      break;
    case "text":
      answer += event.delta;
      break;
    case "error":
      console.error(event.code, event.message);
      break;
  }
}

chatOnce() waits for the complete reply:

const reply = await habitat.chatOnce("atlas", { message: "And after that?", conversationId });
reply.text; reply.citations; reply.usage; reply.tools;

Pass signal (from an AbortController) to cancel either call. For agents with memory mode none, pass history yourself.

Tools and analytics#

const { data: enabled, catalog } = await habitat.tools.list(atlas.id);
const stats = await habitat.analytics.get(atlas.id, { days: 30 });
stats.totals.uniqueUsers;

Errors and rate limits#

Non-2xx responses throw HabitatError:

import { HabitatClient, HabitatError } from "./habitat";

try {
  await habitat.agents.publish(id, "public");
} catch (e) {
  if (e instanceof HabitatError) {
    e.status;     // 422
    e.code;       // "incomplete"
    e.details;    // { issues: [...] }
    e.requestId;  // "req_…"
    e.retryAfter; // seconds, on 429
  }
}

habitat.lastRateLimit; // { limit, remaining, reset: Date, burstLimit, burstRemaining }

parseEventStream(body) is exported too, if you call the chat endpoint with your own fetch.

React#

For embedding a published agent in a React app without an API key, use sdk/react/HabitatAgent.tsx — see Sharing & embedding.