@theokit/sdk 4.2.6 → 4.2.7
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 +6 -0
- package/claude-template/AGENTS.md +73 -55
- package/claude-template/CLAUDE.md +0 -1
- package/claude-template/dot-claude/rules/theokit-conventions.md +2 -3
- package/claude-template/dot-claude/skills/theokit-agent-core/SKILL.md +3 -3
- package/claude-template/dot-claude/skills/theokit-subscriptions/SKILL.md +6 -6
- package/claude-template/dot-claude/skills/theokit-tools/SKILL.md +9 -9
- package/package.json +1 -1
- package/claude-template/dot-claude/skills/theokit-rag/SKILL.md +0 -226
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 4.2.7
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- fix(init-claude): the scaffolded `.claude/` template (`npx theokit-init-claude`) now teaches the current `X.create()` API instead of the pre-3.0 surface removed by SE36 (#139). `AGENTS.md` + the affected skills/rules were corrected: `defineTool`→`Tool.create`, `defineSubscription`→`Subscription.create`, `createAgentFactory`→`AgentFactory.create`; the tool spec field is `handler` (not `execute`); streaming events are `system`/`user`/`assistant`/`thinking`/`tool_call`/`status`/`task`/`request` (there is no `tool_use`/`tool_result`/`usage`/`error`); assistant text is `event.message.content`; `Agent.prompt(prompt, options)` (prompt first); built-in coding tools import from `@theokit/sdk-tools` (not a `@theokit/sdk/tools` subpath). The phantom `theokit-rag` skill and the non-existent `@theokit/sdk/rag` import were removed. A `tests/lint/claude-template-no-drift.test.ts` gate now fails CI if the scaffold teaches a removed factory, phantom subpath, or non-existent stream event.
|
|
8
|
+
|
|
3
9
|
## 4.2.6
|
|
4
10
|
|
|
5
11
|
### Patch Changes
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @theokit/sdk — TypeScript SDK for AI Agents
|
|
2
2
|
|
|
3
|
-
Build AI agents that run locally or in the cloud. Same code, same API, pick your runtime.
|
|
3
|
+
Build AI agents that run locally or in the cloud. Same code, same API, pick your runtime. The exported types are the canonical contract.
|
|
4
4
|
|
|
5
5
|
## Setup
|
|
6
6
|
|
|
@@ -13,24 +13,23 @@ Set your API key:
|
|
|
13
13
|
export THEOKIT_API_KEY="your-key"
|
|
14
14
|
```
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
Node 22.12+ required.
|
|
17
|
+
|
|
18
|
+
## Import Map (verified subpaths)
|
|
17
19
|
|
|
18
20
|
```typescript
|
|
19
|
-
import { Agent } from "@theokit/sdk";
|
|
20
|
-
import {
|
|
21
|
-
import {
|
|
22
|
-
import {
|
|
23
|
-
import {
|
|
24
|
-
import {
|
|
25
|
-
import {
|
|
26
|
-
import {
|
|
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
|
|
21
|
+
import { Agent, Cron, Tool } from "@theokit/sdk"; // core: Agent, Run, Cron, Tool, SDKMessage
|
|
22
|
+
import { TheokitAgentError } from "@theokit/sdk/errors"; // error hierarchy
|
|
23
|
+
import { Workflow } from "@theokit/sdk/workflow"; // multi-step workflows
|
|
24
|
+
import { Eval } from "@theokit/sdk/eval"; // evaluation suite
|
|
25
|
+
import { Subscription } from "@theokit/sdk/subscription"; // SSE / WebSocket subscriptions
|
|
26
|
+
import { SubAgent } from "@theokit/sdk/a2a"; // agent-to-agent delegation
|
|
27
|
+
import { Auth } from "@theokit/sdk/server/auth"; // authentication
|
|
28
|
+
import { TaskStore } from "@theokit/sdk/task-store"; // task persistence
|
|
32
29
|
```
|
|
33
30
|
|
|
31
|
+
Other public subpaths: `/messages`, `/models`, `/skills`, `/project`, `/subagents`, `/sandbox`, `/client`, `/persistence`, `/retry`, `/concurrency`, `/sanitize`. There is **no** `@theokit/sdk/rag` subpath. Never import from `@theokit/sdk/internal/*` or `@theokit/sdk/dist/*`.
|
|
32
|
+
|
|
34
33
|
## Quick Start
|
|
35
34
|
|
|
36
35
|
```typescript
|
|
@@ -42,90 +41,109 @@ const agent = await Agent.create({
|
|
|
42
41
|
|
|
43
42
|
const run = await agent.send("Summarize this repository");
|
|
44
43
|
for await (const event of run.stream()) {
|
|
45
|
-
if (event.type === "assistant")
|
|
44
|
+
if (event.type === "assistant") {
|
|
45
|
+
for (const block of event.message.content) {
|
|
46
|
+
if (block.type === "text") process.stdout.write(block.text);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
46
49
|
}
|
|
47
50
|
|
|
48
|
-
agent.
|
|
51
|
+
await agent[Symbol.asyncDispose](); // or: await using agent = await Agent.create(...)
|
|
49
52
|
```
|
|
50
53
|
|
|
51
54
|
## Core Patterns
|
|
52
55
|
|
|
53
56
|
### 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
|
-
- `
|
|
58
|
-
- `Agent.prompt(
|
|
59
|
-
|
|
60
|
-
|
|
57
|
+
- `Agent.create(options)` — create an agent (local or cloud); returns immediately, `agent.agentId` is `agent-<uuid>` (local) or `bc-<uuid>` (cloud).
|
|
58
|
+
- `agent.send(prompt)` — send a message, get a `Run` (context is retained across sends).
|
|
59
|
+
- `run.stream()` — `AsyncGenerator` of `SDKMessage` events.
|
|
60
|
+
- `run.wait()` — resolve to `{ status, result, model, durationMs, git? }` after the run ends.
|
|
61
|
+
- `Agent.prompt(prompt, options)` — one-shot (create + send + dispose). **Prompt is the first argument.**
|
|
62
|
+
- `Agent.resume(agentId, { apiKey })` — reattach; runtime auto-detected from the ID prefix.
|
|
63
|
+
- Dispose with `await using`, `await agent[Symbol.asyncDispose]()`, or `agent.close()` (fire-and-forget).
|
|
64
|
+
|
|
65
|
+
### Tool definition — `Tool.create` with a Zod schema
|
|
61
66
|
```typescript
|
|
62
|
-
|
|
67
|
+
import { z } from "zod";
|
|
68
|
+
import { Tool } from "@theokit/sdk";
|
|
69
|
+
|
|
70
|
+
const searchTool = Tool.create({
|
|
63
71
|
name: "search",
|
|
64
72
|
description: "Search the web",
|
|
65
73
|
inputSchema: z.object({ query: z.string() }),
|
|
66
|
-
|
|
74
|
+
handler: async ({ query }) => JSON.stringify({ results: await search(query) }),
|
|
67
75
|
});
|
|
68
76
|
```
|
|
69
77
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
- `{ type: "
|
|
78
|
+
The tool spec field is `handler` (returns a string, or a typed value when you set `outputSchema`). Built-in coding tools (`createReadFileTool`, …) live in the separate `@theokit/sdk-tools` package, not a `@theokit/sdk/tools` subpath.
|
|
79
|
+
|
|
80
|
+
`Tool.create` is the canonical factory (uniform `X.create()` API since v3.0). Every public factory follows it: `Provider.create`, `Plugin.create`, `Subscription.create`, `Auth.create`, `SubAgent.create`, `Squad.create`, `Retry.create`. There is **no** `defineTool` / `define*` export — those were removed at v3.0.
|
|
81
|
+
|
|
82
|
+
### Streaming events (`SDKMessage`)
|
|
83
|
+
Discriminate on `type`. All events carry `agent_id` and `run_id`.
|
|
84
|
+
- `{ type: "system" }` — init metadata, once at start (`model?`, `tools?`)
|
|
85
|
+
- `{ type: "user", message: { content } }` — echo of the prompt
|
|
86
|
+
- `{ type: "assistant", message: { content } }` — model output; `content` is a `(TextBlock | ToolUseBlock)[]`
|
|
87
|
+
- `{ type: "thinking", text }` — reasoning content
|
|
88
|
+
- `{ type: "tool_call", call_id, name, status, args?, result? }` — tool lifecycle
|
|
89
|
+
- `{ type: "status", status }` — cloud run lifecycle
|
|
90
|
+
- `{ type: "task" }` / `{ type: "request", request_id }` — task milestones / awaiting input
|
|
91
|
+
|
|
92
|
+
There is no `tool_use` / `tool_result` / `usage` / `error` event. Read assistant text from `event.message.content` (a block array), not `event.content`. Treat `tool_call` `args`/`result` as `unknown`.
|
|
77
93
|
|
|
78
94
|
### Error handling
|
|
79
95
|
```typescript
|
|
96
|
+
import { TheokitAgentError } from "@theokit/sdk/errors";
|
|
97
|
+
|
|
80
98
|
try {
|
|
81
99
|
await agent.send("...");
|
|
82
100
|
} catch (e) {
|
|
83
|
-
if (e instanceof TheokitAgentError)
|
|
84
|
-
console.error(e.code, e.message); // typed error with code
|
|
85
|
-
}
|
|
101
|
+
if (e instanceof TheokitAgentError) console.error(e.code, e.isRetryable, e.message);
|
|
86
102
|
}
|
|
87
103
|
```
|
|
104
|
+
Subclasses: `AuthenticationError`, `RateLimitError`, `ConfigurationError`, `IntegrationNotConnectedError`, `NetworkError`, `UnknownAgentError`, `UnsupportedRunOperationError`.
|
|
88
105
|
|
|
89
|
-
### DI decorators (`@theokit/di` + `@theokit/di-agent`)
|
|
106
|
+
### Optional: DI decorators (`@theokit/di` + `@theokit/di-agent`)
|
|
107
|
+
Decorators are an **optional** convenience layer in separate packages — the factory API above is canonical and never requires them.
|
|
90
108
|
```typescript
|
|
91
|
-
import { Injectable
|
|
92
|
-
import { Tool
|
|
109
|
+
import { Injectable } from "@theokit/di";
|
|
110
|
+
import { Tool as ToolDecorator, Cron as CronDecorator } from "@theokit/di-agent";
|
|
93
111
|
|
|
94
112
|
@Injectable()
|
|
95
113
|
class MyService {
|
|
96
|
-
@
|
|
97
|
-
searchTool!:
|
|
114
|
+
@ToolDecorator({ name: "search", description: "Search" })
|
|
115
|
+
searchTool!: unknown;
|
|
98
116
|
|
|
99
|
-
@
|
|
117
|
+
@CronDecorator({ schedule: "*/5 * * * *" })
|
|
100
118
|
cleanup() { /* runs every 5 min */ }
|
|
101
119
|
}
|
|
102
120
|
```
|
|
103
121
|
|
|
104
|
-
### Gateways
|
|
122
|
+
### Optional: Gateways
|
|
105
123
|
```typescript
|
|
106
124
|
import { defineGateway } from "@theokit/gateway-telegram"; // or -slack, -discord, etc.
|
|
107
125
|
const gateway = defineGateway({ token: process.env.BOT_TOKEN });
|
|
108
126
|
```
|
|
109
127
|
|
|
110
|
-
Available: telegram, slack, discord, whatsapp, teams, email, sms, mattermost, line, matrix.
|
|
111
|
-
|
|
112
128
|
## Anti-patterns
|
|
113
129
|
|
|
114
|
-
- NEVER
|
|
115
|
-
- NEVER
|
|
116
|
-
- NEVER
|
|
117
|
-
- NEVER
|
|
118
|
-
- NEVER
|
|
130
|
+
- NEVER `new Agent()` — always `await Agent.create()`.
|
|
131
|
+
- NEVER author `defineTool` / `defineSubscription` / `defineAuth` / `defineSubAgent` — use `Tool.create` / `Subscription.create` / `Auth.create` / `SubAgent.create`.
|
|
132
|
+
- NEVER switch on `tool_use` / `tool_result` / `usage` / `error` stream events — they don't exist; use `tool_call` / `assistant` / `thinking` / `status`.
|
|
133
|
+
- NEVER read assistant text as `event.content` — it's `event.message.content`.
|
|
134
|
+
- NEVER import from `@theokit/sdk/internal/*`, `@theokit/sdk/dist/*`, or `@theokit/sdk/rag` (no such subpath).
|
|
135
|
+
- NEVER forget disposal (`await using` / `Symbol.asyncDispose` / `close()`) — it leaks the runtime.
|
|
136
|
+
- NEVER use `any` for tool input schemas — use Zod schemas.
|
|
119
137
|
|
|
120
138
|
## Packages
|
|
121
139
|
|
|
122
140
|
| Package | Purpose |
|
|
123
141
|
|---------|---------|
|
|
124
|
-
| `@theokit/sdk` | Core SDK (Agent, Run,
|
|
125
|
-
| `@theokit/di` | Dependency injection container |
|
|
126
|
-
| `@theokit/di-agent` |
|
|
127
|
-
| `@theokit/gateway-*` | Platform gateways
|
|
128
|
-
| `@theokit/react` | React hooks for agent UIs |
|
|
142
|
+
| `@theokit/sdk` | Core SDK (Agent, Run, Tool, Cron, streaming, memory, workflows, eval, subscriptions) |
|
|
143
|
+
| `@theokit/di` | Dependency injection container (optional) |
|
|
144
|
+
| `@theokit/di-agent` | Agentic decorators for DI (optional) |
|
|
145
|
+
| `@theokit/gateway-*` | Platform gateways — telegram, slack, discord, etc. (optional) |
|
|
146
|
+
| `@theokit/react` | React hooks for agent UIs (optional) |
|
|
129
147
|
|
|
130
148
|
## Configuration
|
|
131
149
|
|
|
@@ -16,7 +16,6 @@ These skills inject TheoKit knowledge automatically when you edit files matching
|
|
|
16
16
|
| `theokit-di` | `*container*`, `*inject*`, `*provider*`, `*module*` |
|
|
17
17
|
| `theokit-di-agent` | `*decorator*`, `*Decorator*`, `di-agent*` |
|
|
18
18
|
| `theokit-gateways` | `*gateway*`, `*telegram*`, `*slack*`, `*discord*` |
|
|
19
|
-
| `theokit-rag` | `*retriev*`, `*rerank*`, `*splitter*`, `*rag*` |
|
|
20
19
|
| `theokit-workflows` | `*workflow*`, `*Workflow*`, `*step*` |
|
|
21
20
|
| `theokit-eval` | `*eval*`, `*Eval*`, `*scorer*` |
|
|
22
21
|
| `theokit-cron` | `*cron*`, `*Cron*`, `*job*`, `*schedule*` |
|
|
@@ -6,10 +6,9 @@
|
|
|
6
6
|
- Use `Agent.prompt()` for one-shot operations (auto-disposes)
|
|
7
7
|
|
|
8
8
|
## Imports
|
|
9
|
-
- Use `@theokit/sdk` for core (Agent,
|
|
9
|
+
- Use `@theokit/sdk` for core (Agent, Tool, Cron, Memory)
|
|
10
10
|
- Use `@theokit/sdk/errors` for error types
|
|
11
11
|
- Use `@theokit/sdk/subscription` for SSE/WebSocket
|
|
12
|
-
- Use `@theokit/sdk/rag` for retrievers, rerankers, splitters
|
|
13
12
|
- Use `@theokit/sdk/cron` for scheduled jobs
|
|
14
13
|
- Use `@theokit/sdk/eval` for evaluation
|
|
15
14
|
- Use `@theokit/sdk/workflow` for workflows
|
|
@@ -18,7 +17,7 @@
|
|
|
18
17
|
|
|
19
18
|
## Tools
|
|
20
19
|
- Tool `inputSchema` MUST use Zod schemas — NEVER `any` or untyped objects
|
|
21
|
-
- Tool `
|
|
20
|
+
- Tool `handler` MUST return a string (or a value matching `outputSchema` when set)
|
|
22
21
|
|
|
23
22
|
## DI
|
|
24
23
|
- Use `@Injectable()` + `@Inject()` from `@theokit/di`
|
|
@@ -129,12 +129,12 @@ const { items, nextCursor } = await Agent.list({ runtime: "local", cwd: process.
|
|
|
129
129
|
const { items: runs } = await Agent.listRuns(agentId);
|
|
130
130
|
```
|
|
131
131
|
|
|
132
|
-
##
|
|
132
|
+
## AgentFactory.create
|
|
133
133
|
|
|
134
134
|
```typescript
|
|
135
|
-
import {
|
|
135
|
+
import { AgentFactory } from "@theokit/sdk";
|
|
136
136
|
|
|
137
|
-
const factory =
|
|
137
|
+
const factory = AgentFactory.create({
|
|
138
138
|
apiKey: process.env.THEOKIT_API_KEY!,
|
|
139
139
|
model: { id: "claude-sonnet-4-6" },
|
|
140
140
|
local: { cwd: process.cwd() },
|
|
@@ -5,7 +5,7 @@ paths:
|
|
|
5
5
|
- "**/*sse*"
|
|
6
6
|
- "**/*websocket*"
|
|
7
7
|
- "**/*ws.*"
|
|
8
|
-
description: TheoKit SDK Subscriptions API —
|
|
8
|
+
description: TheoKit SDK Subscriptions API — Subscription.create, SSE/WebSocket transport, subscribe, tracked, resume tokens
|
|
9
9
|
---
|
|
10
10
|
|
|
11
11
|
# TheoKit Subscriptions
|
|
@@ -13,13 +13,13 @@ description: TheoKit SDK Subscriptions API — defineSubscription, SSE/WebSocket
|
|
|
13
13
|
Typed WebSocket + W3C SSE subscriptions with opaque resume tokens. Available
|
|
14
14
|
via the `@theokit/sdk/subscription` sub-path import (not on the main barrel).
|
|
15
15
|
|
|
16
|
-
## Server side — `
|
|
16
|
+
## Server side — `Subscription.create`
|
|
17
17
|
|
|
18
18
|
```typescript
|
|
19
|
-
import {
|
|
19
|
+
import { Subscription } from "@theokit/sdk/subscription";
|
|
20
20
|
import { z } from "zod";
|
|
21
21
|
|
|
22
|
-
export default
|
|
22
|
+
export default Subscription.create({
|
|
23
23
|
input: z.object({
|
|
24
24
|
room: z.string(),
|
|
25
25
|
lastEventId: z.string().optional(),
|
|
@@ -91,11 +91,11 @@ for await (const msg of subscribe<
|
|
|
91
91
|
|
|
92
92
|
## Composing with LLM streaming
|
|
93
93
|
|
|
94
|
-
`Agent.streamObject` and `
|
|
94
|
+
`Agent.streamObject` and `Subscription.create` are independent surfaces. Call
|
|
95
95
|
`Agent.streamObject` inside a subscription handler:
|
|
96
96
|
|
|
97
97
|
```typescript
|
|
98
|
-
export default
|
|
98
|
+
export default Subscription.create({
|
|
99
99
|
input: z.object({ topic: z.string() }),
|
|
100
100
|
output: z.object({ kind: z.enum(["partial", "complete"]), text: z.string() }),
|
|
101
101
|
async *handler(input, ctx) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
user-invocable: false
|
|
3
|
-
description: Custom tools,
|
|
3
|
+
description: Custom tools, Tool.create with Zod schemas, and built-in coding tools for @theokit/sdk.
|
|
4
4
|
paths:
|
|
5
5
|
- "**/*tool*"
|
|
6
6
|
- "**/*Tool*"
|
|
@@ -10,13 +10,13 @@ paths:
|
|
|
10
10
|
|
|
11
11
|
Quick reference for custom inline tools and built-in coding tools.
|
|
12
12
|
|
|
13
|
-
##
|
|
13
|
+
## Tool.create (type-safe builder)
|
|
14
14
|
|
|
15
15
|
```typescript
|
|
16
16
|
import { z } from "zod";
|
|
17
|
-
import {
|
|
17
|
+
import { Tool } from "@theokit/sdk";
|
|
18
18
|
|
|
19
|
-
const rollTool =
|
|
19
|
+
const rollTool = Tool.create({
|
|
20
20
|
name: "roll",
|
|
21
21
|
description: "Roll N dice with S sides each.",
|
|
22
22
|
inputSchema: z.object({
|
|
@@ -84,22 +84,22 @@ await agent.send("Use only the calculator.", {
|
|
|
84
84
|
// tools: [] -> no custom tools for this run
|
|
85
85
|
```
|
|
86
86
|
|
|
87
|
-
## Built-in coding tools (`@theokit/sdk
|
|
87
|
+
## Built-in coding tools (`@theokit/sdk-tools`)
|
|
88
88
|
|
|
89
|
-
Drop-in toolkit for coding agents. All tools are project-scoped and refuse sensitive files.
|
|
89
|
+
Drop-in toolkit for coding agents, shipped as the separate `@theokit/sdk-tools` package (not a `@theokit/sdk/tools` subpath). All tools are project-scoped and refuse sensitive files.
|
|
90
90
|
|
|
91
91
|
```typescript
|
|
92
|
-
import {
|
|
92
|
+
import { AgentFactory } from "@theokit/sdk";
|
|
93
93
|
import {
|
|
94
94
|
createReadFileTool,
|
|
95
95
|
createListDirTool,
|
|
96
96
|
createSearchTextTool,
|
|
97
97
|
createGitDiffTool,
|
|
98
98
|
createRunVitestTool,
|
|
99
|
-
} from "@theokit/sdk
|
|
99
|
+
} from "@theokit/sdk-tools";
|
|
100
100
|
|
|
101
101
|
const projectRoot = process.cwd();
|
|
102
|
-
const factory =
|
|
102
|
+
const factory = AgentFactory.create({
|
|
103
103
|
apiKey: process.env.ANTHROPIC_API_KEY!,
|
|
104
104
|
model: { id: "claude-sonnet-4-6" },
|
|
105
105
|
tools: [
|
package/package.json
CHANGED
|
@@ -1,226 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
user-invocable: false
|
|
3
|
-
description: RAG primitives -- VectorRetriever, CohereReranker, text splitters from @theokit/sdk/rag.
|
|
4
|
-
paths:
|
|
5
|
-
- "**/*retriev*"
|
|
6
|
-
- "**/*rerank*"
|
|
7
|
-
- "**/*splitter*"
|
|
8
|
-
- "**/*rag*"
|
|
9
|
-
---
|
|
10
|
-
|
|
11
|
-
# TheoKit SDK -- RAG
|
|
12
|
-
|
|
13
|
-
Quick reference for the RAG (Retrieval-Augmented Generation) sub-path at `@theokit/sdk/rag`.
|
|
14
|
-
|
|
15
|
-
## Installation
|
|
16
|
-
|
|
17
|
-
The RAG module ships with `@theokit/sdk` -- no additional install needed.
|
|
18
|
-
|
|
19
|
-
```typescript
|
|
20
|
-
import {
|
|
21
|
-
VectorRetriever,
|
|
22
|
-
CohereReranker,
|
|
23
|
-
NoopReranker,
|
|
24
|
-
splitByCharacter,
|
|
25
|
-
splitBySentence,
|
|
26
|
-
splitRecursive,
|
|
27
|
-
} from "@theokit/sdk/rag";
|
|
28
|
-
```
|
|
29
|
-
|
|
30
|
-
## Types
|
|
31
|
-
|
|
32
|
-
```typescript
|
|
33
|
-
interface Document {
|
|
34
|
-
id: string;
|
|
35
|
-
text: string;
|
|
36
|
-
metadata?: Record<string, unknown>;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
interface Chunk {
|
|
40
|
-
text: string;
|
|
41
|
-
index: number;
|
|
42
|
-
metadata?: Record<string, unknown>;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
interface RetrievalResult {
|
|
46
|
-
text: string;
|
|
47
|
-
score: number;
|
|
48
|
-
metadata?: Record<string, unknown>;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
interface RankedChunk {
|
|
52
|
-
text: string;
|
|
53
|
-
score: number;
|
|
54
|
-
originalIndex: number;
|
|
55
|
-
metadata?: Record<string, unknown>;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
interface SplitOptions {
|
|
59
|
-
chunkSize: number;
|
|
60
|
-
overlap?: number;
|
|
61
|
-
}
|
|
62
|
-
```
|
|
63
|
-
|
|
64
|
-
## Interfaces
|
|
65
|
-
|
|
66
|
-
### Retriever
|
|
67
|
-
|
|
68
|
-
```typescript
|
|
69
|
-
interface Retriever {
|
|
70
|
-
retrieve(query: string, options?: { topK?: number }): Promise<RetrievalResult[]>;
|
|
71
|
-
}
|
|
72
|
-
```
|
|
73
|
-
|
|
74
|
-
### Reranker
|
|
75
|
-
|
|
76
|
-
```typescript
|
|
77
|
-
interface Reranker {
|
|
78
|
-
rerank(query: string, chunks: RetrievalResult[]): Promise<RankedChunk[]>;
|
|
79
|
-
}
|
|
80
|
-
```
|
|
81
|
-
|
|
82
|
-
## VectorRetriever
|
|
83
|
-
|
|
84
|
-
Wraps any index that implements `search(query, topK)`.
|
|
85
|
-
|
|
86
|
-
```typescript
|
|
87
|
-
interface VectorIndex {
|
|
88
|
-
search(query: string, topK: number): Promise<RetrievalResult[]>;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
interface VectorRetrieverOptions {
|
|
92
|
-
index: VectorIndex;
|
|
93
|
-
topK?: number; // default 5
|
|
94
|
-
}
|
|
95
|
-
```
|
|
96
|
-
|
|
97
|
-
Usage:
|
|
98
|
-
|
|
99
|
-
```typescript
|
|
100
|
-
import { VectorRetriever } from "@theokit/sdk/rag";
|
|
101
|
-
|
|
102
|
-
const retriever = new VectorRetriever({
|
|
103
|
-
index: myVectorIndex,
|
|
104
|
-
topK: 10,
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
const results = await retriever.retrieve("How does auth work?");
|
|
108
|
-
// results: RetrievalResult[] sorted by relevance
|
|
109
|
-
```
|
|
110
|
-
|
|
111
|
-
The `VectorIndex` interface is the DI boundary. Consumers depend on the interface; implementations (e.g., backed by Memory's SQLite-vec or LanceDB index) depend on the index adapter.
|
|
112
|
-
|
|
113
|
-
## CohereReranker
|
|
114
|
-
|
|
115
|
-
Calls the Cohere Rerank v2 API to re-score retrieval results by relevance.
|
|
116
|
-
|
|
117
|
-
```typescript
|
|
118
|
-
interface CohereRerankerOptions {
|
|
119
|
-
apiKey: string;
|
|
120
|
-
model?: string; // default "rerank-v3.5"
|
|
121
|
-
}
|
|
122
|
-
```
|
|
123
|
-
|
|
124
|
-
Usage:
|
|
125
|
-
|
|
126
|
-
```typescript
|
|
127
|
-
import { CohereReranker } from "@theokit/sdk/rag";
|
|
128
|
-
|
|
129
|
-
const reranker = new CohereReranker({
|
|
130
|
-
apiKey: process.env.COHERE_API_KEY!,
|
|
131
|
-
model: "rerank-v3.5",
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
const ranked = await reranker.rerank("auth middleware", retrievalResults);
|
|
135
|
-
// ranked: RankedChunk[] re-scored by Cohere
|
|
136
|
-
```
|
|
137
|
-
|
|
138
|
-
## NoopReranker
|
|
139
|
-
|
|
140
|
-
Passes through results unchanged. Useful as a baseline or when reranking is not needed.
|
|
141
|
-
|
|
142
|
-
```typescript
|
|
143
|
-
import { NoopReranker } from "@theokit/sdk/rag";
|
|
144
|
-
|
|
145
|
-
const reranker = new NoopReranker();
|
|
146
|
-
const ranked = await reranker.rerank(query, results);
|
|
147
|
-
// ranked === results (with originalIndex added)
|
|
148
|
-
```
|
|
149
|
-
|
|
150
|
-
## Text splitters
|
|
151
|
-
|
|
152
|
-
Three strategies for splitting documents into chunks. All return `Chunk[]` with `text` and `index`. Empty input returns an empty array.
|
|
153
|
-
|
|
154
|
-
### splitByCharacter
|
|
155
|
-
|
|
156
|
-
Fixed-size character windows with optional overlap.
|
|
157
|
-
|
|
158
|
-
```typescript
|
|
159
|
-
import { splitByCharacter } from "@theokit/sdk/rag";
|
|
160
|
-
|
|
161
|
-
const chunks = splitByCharacter(longText, { chunkSize: 500, overlap: 50 });
|
|
162
|
-
```
|
|
163
|
-
|
|
164
|
-
### splitBySentence
|
|
165
|
-
|
|
166
|
-
Groups sentences into chunks up to `chunkSize` characters.
|
|
167
|
-
|
|
168
|
-
```typescript
|
|
169
|
-
import { splitBySentence } from "@theokit/sdk/rag";
|
|
170
|
-
|
|
171
|
-
const chunks = splitBySentence(longText, { chunkSize: 500 });
|
|
172
|
-
```
|
|
173
|
-
|
|
174
|
-
Splits on sentence boundaries (`.`, `!`, `?` followed by whitespace). Sentences are never broken mid-sentence.
|
|
175
|
-
|
|
176
|
-
### splitRecursive
|
|
177
|
-
|
|
178
|
-
Three-level cascading split: paragraph, then sentence, then character.
|
|
179
|
-
|
|
180
|
-
```typescript
|
|
181
|
-
import { splitRecursive } from "@theokit/sdk/rag";
|
|
182
|
-
|
|
183
|
-
const chunks = splitRecursive(longText, { chunkSize: 500, overlap: 50 });
|
|
184
|
-
```
|
|
185
|
-
|
|
186
|
-
Algorithm:
|
|
187
|
-
1. Split by double newlines (paragraphs).
|
|
188
|
-
2. Paragraphs that exceed `chunkSize` are split by sentence.
|
|
189
|
-
3. Sentences that still exceed `chunkSize` are split by character.
|
|
190
|
-
|
|
191
|
-
This is the recommended default for most RAG use cases.
|
|
192
|
-
|
|
193
|
-
## Full RAG pipeline example
|
|
194
|
-
|
|
195
|
-
```typescript
|
|
196
|
-
import { VectorRetriever, CohereReranker, splitRecursive } from "@theokit/sdk/rag";
|
|
197
|
-
|
|
198
|
-
// 1. Split documents
|
|
199
|
-
const chunks = splitRecursive(documentText, { chunkSize: 500, overlap: 50 });
|
|
200
|
-
|
|
201
|
-
// 2. Index chunks (your vector store)
|
|
202
|
-
await vectorStore.upsert(chunks.map((c, i) => ({
|
|
203
|
-
id: `doc-${i}`,
|
|
204
|
-
text: c.text,
|
|
205
|
-
embedding: await embed(c.text),
|
|
206
|
-
})));
|
|
207
|
-
|
|
208
|
-
// 3. Retrieve
|
|
209
|
-
const retriever = new VectorRetriever({ index: vectorStore, topK: 20 });
|
|
210
|
-
const results = await retriever.retrieve(userQuery);
|
|
211
|
-
|
|
212
|
-
// 4. Rerank
|
|
213
|
-
const reranker = new CohereReranker({ apiKey: process.env.COHERE_API_KEY! });
|
|
214
|
-
const ranked = await reranker.rerank(userQuery, results);
|
|
215
|
-
|
|
216
|
-
// 5. Use top results as agent context
|
|
217
|
-
const context = ranked.slice(0, 5).map((r) => r.text).join("\n\n");
|
|
218
|
-
const agent = await Agent.create({
|
|
219
|
-
systemPrompt: `Use this context:\n${context}`,
|
|
220
|
-
// ...
|
|
221
|
-
});
|
|
222
|
-
```
|
|
223
|
-
|
|
224
|
-
## DI integration
|
|
225
|
-
|
|
226
|
-
Use `@Retriever` and `@Reranker` decorators from `@theokit/di-agent` to register RAG components in the DI container. See the theokit-di-agent skill for details.
|