SDK
Interact with TF Code programmatically using the JavaScript/TypeScript SDK.
Installation
npm install @toothfairyai/sdk
Quick Start
import { createOpencode } from "@toothfairyai/sdk";
const tfcode = await createOpencode();
const session = await tfcode.session.create();
const result = await tfcode.session.message(
session.id,
"Explain the authentication flow in this codebase",
);
Connect to Running Server
import { createOpencodeClient } from "@toothfairyai/sdk/client";
const client = createOpencodeClient({
baseURL: "http://localhost:4096",
});
Entry Points
| Import | Description |
|---|---|
@toothfairyai/sdk | Combined client + server |
@toothfairyai/sdk/client | Standalone client to connect to server |
@toothfairyai/sdk/server | Server only |
@toothfairyai/sdk/v2 | V2 client + server |
@toothfairyai/sdk/v2/client | V2 generated client |
API Methods
Sessions
const sessions = await client.session.list();
const session = await client.session.create({ title: "My Session" });
const s = await client.session.get(session.id);
await client.session.delete(session.id);
Messages
const result = await client.session.message(session.id, {
parts: [{ type: "text", text: "Explain this function" }],
});
const messages = await client.session.messages(session.id);
Files
const results = await client.find("authentication");
const files = await client.findFile({ query: "auth" });
const content = await client.fileContent({ path: "/src/auth.ts" });
const tree = await client.file({ path: "/src" });
Config
const config = await client.config.get();
await client.config.patch({ model: "toothfairyai/mystica" });
const providers = await client.config.providers();
Events
const eventSource = client.event();
eventSource.addEventListener("session.updated", (event) => {
console.log("Session updated:", JSON.parse(event.data));
});
Logging
await client.app.log({
body: {
service: "my-app",
level: "info",
message: "Operation completed",
},
});
TypeScript Types
The SDK includes full TypeScript type definitions auto-generated from the OpenAPI 3.1 spec.