@theokit/sdk 1.7.0 → 1.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/bin/init-claude.mjs +68 -0
- package/claude-template/AGENTS.md +139 -0
- package/claude-template/CLAUDE.md +51 -0
- package/claude-template/dot-claude/rules/theokit-conventions.md +33 -0
- package/claude-template/dot-claude/settings.json +16 -0
- package/claude-template/dot-claude/skills/theokit-agent-core/SKILL.md +209 -0
- package/claude-template/dot-claude/skills/theokit-budget/SKILL.md +176 -0
- package/claude-template/dot-claude/skills/theokit-config/SKILL.md +139 -0
- package/claude-template/dot-claude/skills/theokit-cron/SKILL.md +148 -0
- package/claude-template/dot-claude/skills/theokit-di/SKILL.md +233 -0
- package/claude-template/dot-claude/skills/theokit-di-agent/SKILL.md +294 -0
- package/claude-template/dot-claude/skills/theokit-errors/SKILL.md +172 -0
- package/claude-template/dot-claude/skills/theokit-eval/SKILL.md +144 -0
- package/claude-template/dot-claude/skills/theokit-gateways/SKILL.md +209 -0
- package/claude-template/dot-claude/skills/theokit-memory/SKILL.md +176 -0
- package/claude-template/dot-claude/skills/theokit-rag/SKILL.md +226 -0
- package/claude-template/dot-claude/skills/theokit-streaming/SKILL.md +156 -0
- package/claude-template/dot-claude/skills/theokit-subscriptions/SKILL.md +148 -0
- package/claude-template/dot-claude/skills/theokit-tools/SKILL.md +170 -0
- package/claude-template/dot-claude/skills/theokit-workflows/SKILL.md +218 -0
- package/package.json +3 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,18 @@
|
|
|
2
2
|
|
|
3
3
|
## [Unreleased]
|
|
4
4
|
|
|
5
|
+
## [1.8.1] - 2026-06-12
|
|
6
|
+
|
|
7
|
+
### Changed
|
|
8
|
+
|
|
9
|
+
- `theokit-init-claude` now merges into existing `.claude/` directories instead of refusing. Adds only missing files, preserves user customizations. Use `--force` to overwrite all files.
|
|
10
|
+
|
|
11
|
+
## [1.8.0] - 2026-06-12
|
|
12
|
+
|
|
13
|
+
### Added
|
|
14
|
+
|
|
15
|
+
- `.claude/` consumer template with 15 domain-specific passive skills, convention rules, AGENTS.md (cross-agent), and CLAUDE.md for AI coding tool integration. Scaffold via `npx theokit-init-claude`. Skills auto-inject TheoKit API knowledge when editing files matching each domain (Agent Core, Tools, Memory, DI, DI-Agent, Gateways, RAG, Workflows, Eval, Cron, Subscriptions, Errors, Config, Streaming, Budget). 33 tests.
|
|
16
|
+
|
|
5
17
|
## [1.7.0] - 2026-06-11
|
|
6
18
|
|
|
7
19
|
### Changed
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { cpSync, existsSync, mkdirSync, readdirSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
|
|
5
|
+
// EC-1: Node version guard (matches SDK engines.node)
|
|
6
|
+
const [major, minor] = process.versions.node.split(".").map(Number);
|
|
7
|
+
if (major < 22 || (major === 22 && minor < 12)) {
|
|
8
|
+
console.error(`@theokit/sdk requires Node >= 22.12.0. Current: ${process.version}`);
|
|
9
|
+
process.exit(1);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const templateDir = join(import.meta.dirname, "../claude-template");
|
|
13
|
+
const cwd = process.cwd();
|
|
14
|
+
const targetDir = join(cwd, ".claude");
|
|
15
|
+
const force = process.argv.includes("--force");
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Merge-copy: recursively copies src into dest, skipping files that already
|
|
19
|
+
* exist in dest. With --force, overwrites everything.
|
|
20
|
+
* Returns { added, skipped } counts.
|
|
21
|
+
*/
|
|
22
|
+
function mergeCopy(src, dest) {
|
|
23
|
+
let added = 0;
|
|
24
|
+
let skipped = 0;
|
|
25
|
+
mkdirSync(dest, { recursive: true });
|
|
26
|
+
for (const entry of readdirSync(src, { withFileTypes: true })) {
|
|
27
|
+
const srcPath = join(src, entry.name);
|
|
28
|
+
const destPath = join(dest, entry.name);
|
|
29
|
+
if (entry.isDirectory()) {
|
|
30
|
+
const sub = mergeCopy(srcPath, destPath);
|
|
31
|
+
added += sub.added;
|
|
32
|
+
skipped += sub.skipped;
|
|
33
|
+
} else if (force || !existsSync(destPath)) {
|
|
34
|
+
cpSync(srcPath, destPath);
|
|
35
|
+
added++;
|
|
36
|
+
} else {
|
|
37
|
+
skipped++;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return { added, skipped };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Merge .claude/ directory (skills, rules, settings)
|
|
44
|
+
const dotClaude = mergeCopy(join(templateDir, "dot-claude"), targetDir);
|
|
45
|
+
|
|
46
|
+
// Merge root files (AGENTS.md, CLAUDE.md)
|
|
47
|
+
let rootAdded = 0;
|
|
48
|
+
let rootSkipped = 0;
|
|
49
|
+
for (const file of ["AGENTS.md", "CLAUDE.md"]) {
|
|
50
|
+
const dest = join(cwd, file);
|
|
51
|
+
if (force || !existsSync(dest)) {
|
|
52
|
+
cpSync(join(templateDir, file), dest);
|
|
53
|
+
rootAdded++;
|
|
54
|
+
} else {
|
|
55
|
+
rootSkipped++;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const totalAdded = dotClaude.added + rootAdded;
|
|
60
|
+
const totalSkipped = dotClaude.skipped + rootSkipped;
|
|
61
|
+
|
|
62
|
+
if (totalAdded === 0) {
|
|
63
|
+
console.log("All TheoKit SDK files already present. Nothing to add.");
|
|
64
|
+
console.log("Use --force to overwrite existing files.");
|
|
65
|
+
} else {
|
|
66
|
+
console.log(`Added ${totalAdded} file(s). Skipped ${totalSkipped} existing file(s).`);
|
|
67
|
+
console.log("\nNext: open Claude Code and start building with TheoKit.");
|
|
68
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
# @theokit/sdk — TypeScript SDK for AI Agents
|
|
2
|
+
|
|
3
|
+
Build AI agents that run locally or in the cloud. Same code, same API, pick your runtime.
|
|
4
|
+
|
|
5
|
+
## Setup
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @theokit/sdk
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Set your API key:
|
|
12
|
+
```bash
|
|
13
|
+
export THEOKIT_API_KEY="your-key"
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Import Map
|
|
17
|
+
|
|
18
|
+
```typescript
|
|
19
|
+
import { Agent } from "@theokit/sdk"; // Core: Agent, Run, SDKMessage
|
|
20
|
+
import { defineTool } from "@theokit/sdk"; // Tool definitions
|
|
21
|
+
import { TheokitAgentError } from "@theokit/sdk/errors"; // Error hierarchy
|
|
22
|
+
import { Cron } from "@theokit/sdk/cron"; // Scheduled jobs
|
|
23
|
+
import { Eval } from "@theokit/sdk/eval"; // Evaluation suite
|
|
24
|
+
import { Workflow } from "@theokit/sdk/workflow"; // Multi-step workflows
|
|
25
|
+
import { defineSubscription } from "@theokit/sdk/subscription"; // SSE/WebSocket subscriptions
|
|
26
|
+
import { VectorRetriever } from "@theokit/sdk/rag"; // RAG: retrievers, rerankers, splitters
|
|
27
|
+
import { defineSubAgent } from "@theokit/sdk/a2a"; // Agent-to-agent delegation
|
|
28
|
+
import { SandboxBackend } from "@theokit/sdk/sandbox"; // Sandbox backends
|
|
29
|
+
import { defineAuth } from "@theokit/sdk/server/auth"; // Authentication
|
|
30
|
+
import { TaskStore } from "@theokit/sdk/task-store"; // Task persistence
|
|
31
|
+
import { createClient } from "@theokit/sdk/client"; // HTTP client
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Quick Start
|
|
35
|
+
|
|
36
|
+
```typescript
|
|
37
|
+
const agent = await Agent.create({
|
|
38
|
+
apiKey: process.env.THEOKIT_API_KEY!,
|
|
39
|
+
model: { id: "google/gemini-2.0-flash-001" },
|
|
40
|
+
local: { cwd: process.cwd() },
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
const run = await agent.send("Summarize this repository");
|
|
44
|
+
for await (const event of run.stream()) {
|
|
45
|
+
if (event.type === "assistant") console.log(event.content);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
agent.dispose(); // Always clean up
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Core Patterns
|
|
52
|
+
|
|
53
|
+
### Agent lifecycle
|
|
54
|
+
- `Agent.create(options)` — create an agent (local or cloud)
|
|
55
|
+
- `agent.send(prompt)` — send a message, get a Run
|
|
56
|
+
- `run.stream()` — AsyncGenerator of SDKMessage events
|
|
57
|
+
- `agent.dispose()` — clean up resources (or use `await using`)
|
|
58
|
+
- `Agent.prompt(options, prompt)` — one-shot: create, send, dispose
|
|
59
|
+
|
|
60
|
+
### Tool definition
|
|
61
|
+
```typescript
|
|
62
|
+
const searchTool = defineTool({
|
|
63
|
+
name: "search",
|
|
64
|
+
description: "Search the web",
|
|
65
|
+
inputSchema: z.object({ query: z.string() }),
|
|
66
|
+
execute: async ({ query }) => ({ results: await search(query) }),
|
|
67
|
+
});
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Streaming events (SDKMessage)
|
|
71
|
+
- `{ type: "assistant", content }` — text from the model
|
|
72
|
+
- `{ type: "tool_use", name, input }` — tool call
|
|
73
|
+
- `{ type: "tool_result", name, output }` — tool response
|
|
74
|
+
- `{ type: "status", status }` — run status change
|
|
75
|
+
- `{ type: "error", error }` — error event
|
|
76
|
+
- `{ type: "usage", tokens }` — token usage update
|
|
77
|
+
|
|
78
|
+
### Error handling
|
|
79
|
+
```typescript
|
|
80
|
+
try {
|
|
81
|
+
await agent.send("...");
|
|
82
|
+
} catch (e) {
|
|
83
|
+
if (e instanceof TheokitAgentError) {
|
|
84
|
+
console.error(e.code, e.message); // typed error with code
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### DI decorators (`@theokit/di` + `@theokit/di-agent`)
|
|
90
|
+
```typescript
|
|
91
|
+
import { Injectable, Container } from "@theokit/di";
|
|
92
|
+
import { Tool, Workflow, Cron, InjectAgent } from "@theokit/di-agent";
|
|
93
|
+
|
|
94
|
+
@Injectable()
|
|
95
|
+
class MyService {
|
|
96
|
+
@Tool({ name: "search", description: "Search" })
|
|
97
|
+
searchTool!: ToolOptions;
|
|
98
|
+
|
|
99
|
+
@Cron({ schedule: "*/5 * * * *" })
|
|
100
|
+
cleanup() { /* runs every 5 min */ }
|
|
101
|
+
}
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Gateways
|
|
105
|
+
```typescript
|
|
106
|
+
import { defineGateway } from "@theokit/gateway-telegram"; // or -slack, -discord, etc.
|
|
107
|
+
const gateway = defineGateway({ token: process.env.BOT_TOKEN });
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
Available: telegram, slack, discord, whatsapp, teams, email, sms, mattermost, line, matrix.
|
|
111
|
+
|
|
112
|
+
## Anti-patterns
|
|
113
|
+
|
|
114
|
+
- NEVER import from `@theokit/sdk/internal/...` — internal paths are not public API
|
|
115
|
+
- NEVER import from `@theokit/sdk/dist/...` — use the exports map above
|
|
116
|
+
- NEVER forget `agent.dispose()` — causes resource leaks
|
|
117
|
+
- NEVER use `new Agent()` — always use `Agent.create()`
|
|
118
|
+
- NEVER use `any` for tool input schemas — use Zod schemas
|
|
119
|
+
|
|
120
|
+
## Packages
|
|
121
|
+
|
|
122
|
+
| Package | Purpose |
|
|
123
|
+
|---------|---------|
|
|
124
|
+
| `@theokit/sdk` | Core SDK (Agent, Run, Tools, Memory, Streaming) |
|
|
125
|
+
| `@theokit/di` | Dependency injection container |
|
|
126
|
+
| `@theokit/di-agent` | 15 agentic decorators for DI |
|
|
127
|
+
| `@theokit/gateway-*` | Platform gateways (Telegram, Slack, etc.) |
|
|
128
|
+
| `@theokit/react` | React hooks for agent UIs |
|
|
129
|
+
|
|
130
|
+
## Configuration
|
|
131
|
+
|
|
132
|
+
Project config lives in `.theokit/`:
|
|
133
|
+
- `.theokit/mcp.json` — MCP server configuration
|
|
134
|
+
- `.theokit/hooks.json` — lifecycle hooks
|
|
135
|
+
- `.theokit/agents/*.md` — agent instruction files
|
|
136
|
+
|
|
137
|
+
Environment variables:
|
|
138
|
+
- `THEOKIT_API_KEY` — API key (required)
|
|
139
|
+
- `THEOKIT_MODEL_ID` — default model override
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
@AGENTS.md
|
|
2
|
+
|
|
3
|
+
## Claude Code — TheoKit SDK Extensions
|
|
4
|
+
|
|
5
|
+
This project uses `@theokit/sdk`. The AGENTS.md above contains the full API reference. Below are Claude Code-specific extensions.
|
|
6
|
+
|
|
7
|
+
### Available Skills (auto-loaded by domain)
|
|
8
|
+
|
|
9
|
+
These skills inject TheoKit knowledge automatically when you edit files matching their domain:
|
|
10
|
+
|
|
11
|
+
| Skill | Triggers on files matching |
|
|
12
|
+
|-------|---------------------------|
|
|
13
|
+
| `theokit-agent-core` | `*agent*`, `*Agent*`, `sdk.*` |
|
|
14
|
+
| `theokit-tools` | `*tool*`, `*Tool*` |
|
|
15
|
+
| `theokit-memory` | `*memory*`, `*Memory*`, `*embed*` |
|
|
16
|
+
| `theokit-di` | `*container*`, `*inject*`, `*provider*`, `*module*` |
|
|
17
|
+
| `theokit-di-agent` | `*decorator*`, `*Decorator*`, `di-agent*` |
|
|
18
|
+
| `theokit-gateways` | `*gateway*`, `*telegram*`, `*slack*`, `*discord*` |
|
|
19
|
+
| `theokit-rag` | `*retriev*`, `*rerank*`, `*splitter*`, `*rag*` |
|
|
20
|
+
| `theokit-workflows` | `*workflow*`, `*Workflow*`, `*step*` |
|
|
21
|
+
| `theokit-eval` | `*eval*`, `*Eval*`, `*scorer*` |
|
|
22
|
+
| `theokit-cron` | `*cron*`, `*Cron*`, `*job*`, `*schedule*` |
|
|
23
|
+
| `theokit-subscriptions` | `*subscri*`, `*sse*`, `*websocket*`, `*ws.*` |
|
|
24
|
+
| `theokit-errors` | `*error*`, `*Error*`, `*exception*` |
|
|
25
|
+
| `theokit-config` | `.theokit/**`, `config.*`, `theo.config.*` |
|
|
26
|
+
| `theokit-streaming` | `*stream*`, `*Stream*`, `*SDKMessage*` |
|
|
27
|
+
| `theokit-budget` | `*budget*`, `*Budget*`, `*cost*`, `*token*` |
|
|
28
|
+
|
|
29
|
+
### Settings
|
|
30
|
+
|
|
31
|
+
`.claude/settings.json` is pre-configured with safe defaults:
|
|
32
|
+
- Allows: `npm run *`, `pnpm *`, `git status`, `git diff`, reading `src/` and `docs/`
|
|
33
|
+
- Denies: `.env*` file reads, `sudo`, `rm -rf`
|
|
34
|
+
- Override locally: create `.claude/settings.local.json` (add to `.gitignore`)
|
|
35
|
+
|
|
36
|
+
### Customization
|
|
37
|
+
|
|
38
|
+
Add your project-specific instructions below this line. The SDK knowledge above stays current with your installed `@theokit/sdk` version.
|
|
39
|
+
|
|
40
|
+
```markdown
|
|
41
|
+
## My Project
|
|
42
|
+
|
|
43
|
+
Build: `npm run build`
|
|
44
|
+
Test: `npm test`
|
|
45
|
+
Lint: `npm run lint`
|
|
46
|
+
|
|
47
|
+
## Architecture
|
|
48
|
+
- `src/agents/` — agent definitions
|
|
49
|
+
- `src/tools/` — tool implementations
|
|
50
|
+
- `src/services/` — business logic
|
|
51
|
+
```
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# TheoKit SDK Conventions
|
|
2
|
+
|
|
3
|
+
## Agent lifecycle
|
|
4
|
+
- Always use `Agent.create()` to create agents — NEVER `new Agent()`
|
|
5
|
+
- Always call `agent.dispose()` or use `await using` when done
|
|
6
|
+
- Use `Agent.prompt()` for one-shot operations (auto-disposes)
|
|
7
|
+
|
|
8
|
+
## Imports
|
|
9
|
+
- Use `@theokit/sdk` for core (Agent, defineTool, Memory)
|
|
10
|
+
- Use `@theokit/sdk/errors` for error types
|
|
11
|
+
- Use `@theokit/sdk/subscription` for SSE/WebSocket
|
|
12
|
+
- Use `@theokit/sdk/rag` for retrievers, rerankers, splitters
|
|
13
|
+
- Use `@theokit/sdk/cron` for scheduled jobs
|
|
14
|
+
- Use `@theokit/sdk/eval` for evaluation
|
|
15
|
+
- Use `@theokit/sdk/workflow` for workflows
|
|
16
|
+
- NEVER import from `@theokit/sdk/internal/...`
|
|
17
|
+
- NEVER import from `@theokit/sdk/dist/...`
|
|
18
|
+
|
|
19
|
+
## Tools
|
|
20
|
+
- Tool `inputSchema` MUST use Zod schemas — NEVER `any` or untyped objects
|
|
21
|
+
- Tool `execute` MUST return a serializable value
|
|
22
|
+
|
|
23
|
+
## DI
|
|
24
|
+
- Use `@Injectable()` + `@Inject()` from `@theokit/di`
|
|
25
|
+
- NEVER manually `new` a service — let the container resolve it
|
|
26
|
+
|
|
27
|
+
## Error handling
|
|
28
|
+
- Catch `TheokitAgentError` (base class) and check `error.code`
|
|
29
|
+
- NEVER silently swallow errors — log with context or rethrow
|
|
30
|
+
|
|
31
|
+
## Gateways
|
|
32
|
+
- One gateway per agent instance
|
|
33
|
+
- Configure via `defineGateway()` from the specific gateway package
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"permissions": {
|
|
3
|
+
"allow": [
|
|
4
|
+
"Bash(npm run *)",
|
|
5
|
+
"Bash(pnpm *)",
|
|
6
|
+
"Bash(npx *)",
|
|
7
|
+
"Read(./src/**)",
|
|
8
|
+
"Read(./docs/**)",
|
|
9
|
+
"Read(./tests/**)",
|
|
10
|
+
"Bash(git status)",
|
|
11
|
+
"Bash(git diff)",
|
|
12
|
+
"Bash(git log *)"
|
|
13
|
+
],
|
|
14
|
+
"deny": ["Read(.env*)", "Read(**/.env*)", "Bash(sudo *)", "Bash(rm -rf *)"]
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
---
|
|
2
|
+
user-invocable: false
|
|
3
|
+
description: Agent lifecycle, sending messages, streaming, and disposal patterns for @theokit/sdk.
|
|
4
|
+
paths:
|
|
5
|
+
- "**/*agent*"
|
|
6
|
+
- "**/*Agent*"
|
|
7
|
+
- "**/sdk.*"
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
# TheoKit SDK -- Agent Core
|
|
11
|
+
|
|
12
|
+
Quick reference for Agent lifecycle, Run streaming, and disposal.
|
|
13
|
+
|
|
14
|
+
## Agent.create
|
|
15
|
+
|
|
16
|
+
```typescript
|
|
17
|
+
import { Agent } from "@theokit/sdk";
|
|
18
|
+
|
|
19
|
+
const agent = await Agent.create({
|
|
20
|
+
apiKey: process.env.THEOKIT_API_KEY!,
|
|
21
|
+
model: { id: "google/gemini-2.0-flash-001" },
|
|
22
|
+
local: { cwd: process.cwd() },
|
|
23
|
+
});
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Returns an `SDKAgent`. Local agents get `agent-<uuid>` IDs; cloud agents get `bc-<uuid>`.
|
|
27
|
+
|
|
28
|
+
### AgentOptions (key fields)
|
|
29
|
+
|
|
30
|
+
| Property | Type | Notes |
|
|
31
|
+
|---|---|---|
|
|
32
|
+
| `model` | `ModelSelection` | Required for local; `{ id, params? }`. |
|
|
33
|
+
| `apiKey` | `string` | Falls back to `THEOKIT_API_KEY` env. |
|
|
34
|
+
| `local` | `{ cwd, settingSources?, sandboxOptions? }` | Local runtime config. |
|
|
35
|
+
| `cloud` | `CloudOptions` | Cloud runtime config (repos, autoCreatePR, envVars). |
|
|
36
|
+
| `systemPrompt` | `string \| (ctx: SystemPromptContext) => string` | Static string or async resolver. |
|
|
37
|
+
| `mcpServers` | `Record<string, McpServerConfig>` | Inline MCP server definitions. |
|
|
38
|
+
| `agents` | `Record<string, AgentDefinition>` | Subagent definitions. |
|
|
39
|
+
| `tools` | `CustomTool[]` | Inline custom tools (local only). |
|
|
40
|
+
| `memory` | `MemoryOptions` | Durable memory config. |
|
|
41
|
+
| `handoffs` | `Array<SDKAgent \| Handoff>` | Peer-to-peer agent handoffs. |
|
|
42
|
+
| `conversationStorage` | `ConversationStorageAdapter` | Pluggable persistence. |
|
|
43
|
+
|
|
44
|
+
## Agent.prompt (one-shot)
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
const result = await Agent.prompt("What does the auth middleware do?", {
|
|
48
|
+
apiKey: process.env.THEOKIT_API_KEY!,
|
|
49
|
+
model: { id: "google/gemini-2.0-flash-001" },
|
|
50
|
+
local: { cwd: process.cwd() },
|
|
51
|
+
});
|
|
52
|
+
// result: { id, status, result?, model?, durationMs?, git? }
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Pass `throwOnError: true` to reject with `AgentRunError` instead of resolving with `status: 'error'`.
|
|
56
|
+
|
|
57
|
+
## agent.send and Run
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
const run = await agent.send("Find the bug in src/auth.ts");
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Run interface
|
|
64
|
+
|
|
65
|
+
| Member | Type | Description |
|
|
66
|
+
|---|---|---|
|
|
67
|
+
| `id` | `string` | Run identifier. |
|
|
68
|
+
| `status` | `RunStatus` | `"running" \| "finished" \| "error" \| "cancelled"` |
|
|
69
|
+
| `stream()` | `AsyncGenerator<SDKMessage>` | Normalized event stream. |
|
|
70
|
+
| `wait()` | `Promise<RunResult>` | Block until finished. |
|
|
71
|
+
| `cancel()` | `Promise<void>` | Cancel the run. |
|
|
72
|
+
| `conversation()` | `Promise<ConversationTurn[]>` | Structured turn history. |
|
|
73
|
+
| `onDidChangeStatus(fn)` | `() => void` | Status change listener; returns unsubscribe. |
|
|
74
|
+
|
|
75
|
+
### Streaming SDKMessage types
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
for await (const event of run.stream()) {
|
|
79
|
+
switch (event.type) {
|
|
80
|
+
case "assistant": /* event.message.content: (TextBlock | ToolUseBlock)[] */ break;
|
|
81
|
+
case "thinking": /* event.text, event.thinking_duration_ms? */ break;
|
|
82
|
+
case "tool_call": /* event.name, event.status, event.args?, event.result? */ break;
|
|
83
|
+
case "status": /* event.status: cloud lifecycle transitions */ break;
|
|
84
|
+
case "task": /* event.text: task milestones */ break;
|
|
85
|
+
case "request": /* event.request_id: awaiting user input */ break;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### Per-send options
|
|
91
|
+
|
|
92
|
+
```typescript
|
|
93
|
+
await agent.send("Plan the refactor", {
|
|
94
|
+
model: { id: "claude-sonnet-4-6", params: [{ id: "thinking", value: "high" }] },
|
|
95
|
+
systemPrompt: "Focus on performance.",
|
|
96
|
+
signal: abortController.signal,
|
|
97
|
+
onDelta: ({ update }) => { /* InteractionUpdate */ },
|
|
98
|
+
onStep: ({ step }) => { /* ConversationStep */ },
|
|
99
|
+
});
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## Agent.resume
|
|
103
|
+
|
|
104
|
+
```typescript
|
|
105
|
+
const agent = await Agent.resume("bc-abc123", {
|
|
106
|
+
apiKey: process.env.THEOKIT_API_KEY!,
|
|
107
|
+
});
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
Runtime auto-detected from ID prefix. Inline `mcpServers` and `tools` are NOT persisted -- re-pass on resume.
|
|
111
|
+
|
|
112
|
+
## Agent.getOrCreate
|
|
113
|
+
|
|
114
|
+
```typescript
|
|
115
|
+
const agent = await Agent.getOrCreate(`tg-user-${userId}`, {
|
|
116
|
+
apiKey: process.env.THEOKIT_API_KEY!,
|
|
117
|
+
model: { id: "claude-sonnet-4-6" },
|
|
118
|
+
local: { cwd: process.cwd() },
|
|
119
|
+
});
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
Tries resume first; on `UnknownAgentError` falls through to create.
|
|
123
|
+
|
|
124
|
+
## Agent.get / Agent.list / Agent.listRuns
|
|
125
|
+
|
|
126
|
+
```typescript
|
|
127
|
+
const info = await Agent.get(agentId);
|
|
128
|
+
const { items, nextCursor } = await Agent.list({ runtime: "local", cwd: process.cwd() });
|
|
129
|
+
const { items: runs } = await Agent.listRuns(agentId);
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
## createAgentFactory
|
|
133
|
+
|
|
134
|
+
```typescript
|
|
135
|
+
import { createAgentFactory } from "@theokit/sdk";
|
|
136
|
+
|
|
137
|
+
const factory = createAgentFactory({
|
|
138
|
+
apiKey: process.env.THEOKIT_API_KEY!,
|
|
139
|
+
model: { id: "claude-sonnet-4-6" },
|
|
140
|
+
local: { cwd: process.cwd() },
|
|
141
|
+
systemPrompt: "You are a helpful assistant.",
|
|
142
|
+
});
|
|
143
|
+
const agent = await factory.getOrCreate(`user-${userId}`);
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
## Agent.builder (fluent API)
|
|
147
|
+
|
|
148
|
+
```typescript
|
|
149
|
+
const agent = await Agent.builder()
|
|
150
|
+
.apiKey(process.env.THEOKIT_API_KEY!)
|
|
151
|
+
.model({ id: "claude-sonnet-4-6" })
|
|
152
|
+
.local({ cwd: process.cwd() })
|
|
153
|
+
.tools([myTool])
|
|
154
|
+
.getOrCreate(`user-${userId}`);
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
## Agent.generateObject / Agent.streamObject
|
|
158
|
+
|
|
159
|
+
```typescript
|
|
160
|
+
import { z } from "zod";
|
|
161
|
+
|
|
162
|
+
const { object } = await Agent.generateObject({
|
|
163
|
+
schema: z.object({ title: z.string(), year: z.number().nullable() }),
|
|
164
|
+
prompt: "Fact card about jazz.",
|
|
165
|
+
model: { id: "google/gemini-2.0-flash-001" },
|
|
166
|
+
local: { cwd: process.cwd() },
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
for await (const evt of Agent.streamObject({ schema, prompt, model, local })) {
|
|
170
|
+
if (evt.type === "partial") render(evt.partial);
|
|
171
|
+
if (evt.type === "complete") finalize(evt.object);
|
|
172
|
+
}
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
## Disposal patterns
|
|
176
|
+
|
|
177
|
+
```typescript
|
|
178
|
+
// Preferred: await using (auto-dispose on block exit)
|
|
179
|
+
await using agent = await Agent.create({ /* ... */ });
|
|
180
|
+
|
|
181
|
+
// Explicit dispose
|
|
182
|
+
await agent[Symbol.asyncDispose]();
|
|
183
|
+
|
|
184
|
+
// Fire-and-forget close
|
|
185
|
+
agent.close();
|
|
186
|
+
|
|
187
|
+
// Reload config without disposing
|
|
188
|
+
await agent.reload();
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
## Agent.registry (production)
|
|
192
|
+
|
|
193
|
+
```typescript
|
|
194
|
+
Agent.registry.configure({ maxAgents: 1000, idleTimeoutMs: 15 * 60 * 1000 });
|
|
195
|
+
Agent.registry.size();
|
|
196
|
+
Agent.registry.evict("agent-42");
|
|
197
|
+
await Agent.registry.evictAll(); // graceful shutdown
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
## Cancellation
|
|
201
|
+
|
|
202
|
+
```typescript
|
|
203
|
+
const run = await agent.send(message, { signal: request.signal });
|
|
204
|
+
// On abort: AgentRunError with code "aborted"
|
|
205
|
+
|
|
206
|
+
// Compose with timeout:
|
|
207
|
+
const composed = AbortSignal.any([request.signal, AbortSignal.timeout(30_000)]);
|
|
208
|
+
await agent.send(message, { signal: composed });
|
|
209
|
+
```
|