@kilncraft/core 0.0.0-stage → 1.1.0
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/README.md +306 -2
- package/dist/index.d.ts +496 -0
- package/dist/index.js +2942 -0
- package/dist/index.js.map +1 -0
- package/package.json +39 -4
package/README.md
CHANGED
|
@@ -1,3 +1,307 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @kilncraft/core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The Kilncraft agent runtime: provider adapters, the tool loop, permissions,
|
|
4
|
+
sessions, compaction, plugins, checkpoints, and audit logging.
|
|
5
|
+
|
|
6
|
+
`@kilncraft/cli`, `@kilncraft/server`, `@kilncraft/sdk`, and `@kilncraft/tui` all
|
|
7
|
+
build on this package. Use it directly when you want the agent loop without the
|
|
8
|
+
HTTP server, CLI, or terminal UI.
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```sh
|
|
13
|
+
npm i @kilncraft/core @kilncraft/shared
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Requires Node.js >= 24 (uses `node:sqlite`).
|
|
17
|
+
|
|
18
|
+
## What it provides
|
|
19
|
+
|
|
20
|
+
| Area | Main exports |
|
|
21
|
+
| ----------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
22
|
+
| Agent | `Agent`, `AgentEvent`, `AgentOptions`, `TurnResult` |
|
|
23
|
+
| Providers | `ProviderRegistry`, `createDefaultProviderRegistry`, `AnthropicProvider`, `OpenAIProvider`, `GeminiProvider`, `MODEL_CATALOG`, `listKnownModels`, `modelInfoFor` |
|
|
24
|
+
| Tools | `ToolRegistry`, `createDefaultToolRegistry`, individual tools |
|
|
25
|
+
| Sessions | `SessionStore`, `resolveDataDir`, `SearchResult` |
|
|
26
|
+
| Permissions | `PermissionEngine` |
|
|
27
|
+
| Compaction | `estimateTokens`, `estimateMessagesTokens`, `shouldCompact`, `compactMessages` |
|
|
28
|
+
| Prompts | `buildSystemPrompt` |
|
|
29
|
+
| Plugins | `PluginHost`, `loadPlugins` |
|
|
30
|
+
| Audit | `AuditLog` |
|
|
31
|
+
| Checkpoints | `CheckpointManager` |
|
|
32
|
+
| Utilities | `isPathInside`, `matchesGlob`, `diffText`, `truncateText`, `lineNumbered` |
|
|
33
|
+
|
|
34
|
+
## Agent loop
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
import {
|
|
38
|
+
Agent,
|
|
39
|
+
createDefaultProviderRegistry,
|
|
40
|
+
createDefaultToolRegistry,
|
|
41
|
+
PermissionEngine,
|
|
42
|
+
} from "@kilncraft/core";
|
|
43
|
+
import { createLogger } from "@kilncraft/shared";
|
|
44
|
+
|
|
45
|
+
const registry = createDefaultProviderRegistry(process.env, {
|
|
46
|
+
anthropic: { apiKey: "{env:ANTHROPIC_API_KEY}" },
|
|
47
|
+
});
|
|
48
|
+
const provider = registry.get("anthropic");
|
|
49
|
+
if (!provider) throw new Error("ANTHROPIC_API_KEY is not set");
|
|
50
|
+
|
|
51
|
+
const agent = new Agent({
|
|
52
|
+
cwd: process.cwd(),
|
|
53
|
+
provider,
|
|
54
|
+
model: "claude-sonnet-4-5",
|
|
55
|
+
tools: createDefaultToolRegistry(),
|
|
56
|
+
permissions: new PermissionEngine({ mode: "acceptEdits", cwd: process.cwd() }),
|
|
57
|
+
logger: createLogger({ level: "info" }),
|
|
58
|
+
askPermission: async (request) => {
|
|
59
|
+
console.log("Permission requested:", request.tool, request.input);
|
|
60
|
+
return "allow";
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
const result = await agent.prompt("Summarize this repository", {
|
|
65
|
+
onEvent: (event) => {
|
|
66
|
+
if (event.type === "assistant.text") process.stdout.write(event.text);
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
console.log(result.text, result.usage, result.cost, result.stopReason);
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
`AgentOptions`:
|
|
74
|
+
|
|
75
|
+
| Field | Required | Purpose |
|
|
76
|
+
| --------------- | -------- | --------------------------------------- |
|
|
77
|
+
| `cwd` | yes | Workspace root |
|
|
78
|
+
| `provider` | yes | A `ProviderAdapter` |
|
|
79
|
+
| `model` | yes | Model id without the provider prefix |
|
|
80
|
+
| `tools` | yes | `ToolRegistry` |
|
|
81
|
+
| `permissions` | yes | `PermissionEngine` |
|
|
82
|
+
| `logger` | yes | Pino logger |
|
|
83
|
+
| `maxTurns` | no | Tool-loop turn cap (default 25) |
|
|
84
|
+
| `maxTokens` | no | Max output tokens per request |
|
|
85
|
+
| `systemPrompt` | no | Override the generated system prompt |
|
|
86
|
+
| `agent` | no | Agent preset name stored on the session |
|
|
87
|
+
| `store` | no | Persist sessions/messages |
|
|
88
|
+
| `session` | no | Resume an existing session |
|
|
89
|
+
| `audit` | no | Permission audit log |
|
|
90
|
+
| `checkpoints` | no | File snapshot manager |
|
|
91
|
+
| `askPermission` | no | Interactive approval callback |
|
|
92
|
+
| `plugins` | no | Loaded `PluginHost` |
|
|
93
|
+
| `sandbox` | no | Wrap `bash` in a sandbox adapter |
|
|
94
|
+
| `lsp` | no | Language-server manager |
|
|
95
|
+
|
|
96
|
+
`Agent` methods: `prompt`, `compact`, `getSession`, `getMessages`,
|
|
97
|
+
`replaceMessages`, `getUsage`, `getCost`, `getPermissions`, `getTodos`, `id`.
|
|
98
|
+
|
|
99
|
+
`AgentEvent` covers `turn.start`, `assistant.text`, `assistant.message`,
|
|
100
|
+
`tool.call`, `tool.result`, `permission.requested`, `permission.denied`,
|
|
101
|
+
`compaction.started`, `compaction.completed`, `turn.end`, and `error`.
|
|
102
|
+
|
|
103
|
+
## Providers
|
|
104
|
+
|
|
105
|
+
`createDefaultProviderRegistry(env, providerConfigs)` registers a provider only
|
|
106
|
+
when it has credentials:
|
|
107
|
+
|
|
108
|
+
| Provider | Credential source | Notes |
|
|
109
|
+
| ------------ | --------------------------------------------------------- | --------------------------------- |
|
|
110
|
+
| `anthropic` | `ANTHROPIC_API_KEY` or `providers.anthropic.apiKey` | Messages API |
|
|
111
|
+
| `openai` | `OPENAI_API_KEY` or `providers.openai.apiKey` | Chat Completions |
|
|
112
|
+
| `openrouter` | `OPENROUTER_API_KEY` or `providers.openrouter.apiKey` | OpenAI-compatible |
|
|
113
|
+
| `gemini` | `GEMINI_API_KEY` / `GOOGLE_API_KEY` or config | `streamGenerateContent` |
|
|
114
|
+
| `ollama` | always registered | `OLLAMA_HOST` or config `baseURL` |
|
|
115
|
+
| `custom` | `providers.custom.baseURL` or `KILNCRAFT_CUSTOM_BASE_URL` | Any OpenAI-compatible endpoint |
|
|
116
|
+
|
|
117
|
+
Config values accept `{env:NAME}`:
|
|
118
|
+
|
|
119
|
+
```ts
|
|
120
|
+
createDefaultProviderRegistry(process.env, {
|
|
121
|
+
openrouter: {
|
|
122
|
+
apiKey: "{env:OPENROUTER_API_KEY}",
|
|
123
|
+
baseURL: "https://openrouter.ai/api/v1",
|
|
124
|
+
headers: { "X-Title": "my-app" },
|
|
125
|
+
},
|
|
126
|
+
});
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
`availableProviderIds(env, providerConfigs)` reports the ids that would actually
|
|
130
|
+
be registered. `MODEL_CATALOG`, `listKnownModels()`, and `modelInfoFor()` expose
|
|
131
|
+
known models, context windows, pricing, and capabilities.
|
|
132
|
+
|
|
133
|
+
## Built-in tools
|
|
134
|
+
|
|
135
|
+
`createDefaultToolRegistry()` registers:
|
|
136
|
+
|
|
137
|
+
| Tool | Kind | Purpose |
|
|
138
|
+
| ----------------- | --------- | -------------------------------------------- |
|
|
139
|
+
| `read` | read-only | Read a file, optional line range |
|
|
140
|
+
| `write` | mutating | Create or overwrite a file |
|
|
141
|
+
| `edit` | mutating | Exact string replacement |
|
|
142
|
+
| `glob` | read-only | Fast filename matching |
|
|
143
|
+
| `grep` | read-only | Content search (ripgrep when available) |
|
|
144
|
+
| `ls` | read-only | List directory entries |
|
|
145
|
+
| `bash` | mutating | Run a shell command (sandboxed when enabled) |
|
|
146
|
+
| `todo_write` | read-only | Replace the task checklist |
|
|
147
|
+
| `lsp_diagnostics` | read-only | Diagnostics from a language server |
|
|
148
|
+
| `lsp_symbols` | read-only | Document symbols from a language server |
|
|
149
|
+
|
|
150
|
+
Tools are plain objects:
|
|
151
|
+
|
|
152
|
+
```ts
|
|
153
|
+
import type { ToolDefinition, ToolOutput } from "@kilncraft/shared";
|
|
154
|
+
import { z } from "zod";
|
|
155
|
+
|
|
156
|
+
const input = z.object({ path: z.string() });
|
|
157
|
+
|
|
158
|
+
export const myTool: ToolDefinition<z.infer<typeof input>> = {
|
|
159
|
+
id: "my_tool",
|
|
160
|
+
description: "Read a file and return its size",
|
|
161
|
+
input,
|
|
162
|
+
readOnly: true,
|
|
163
|
+
parallelizable: true,
|
|
164
|
+
defaultDecision: "allow",
|
|
165
|
+
async execute({ path }, context): Promise<ToolOutput> {
|
|
166
|
+
context.logger.debug({ path }, "my_tool");
|
|
167
|
+
return { kind: "text", text: path };
|
|
168
|
+
},
|
|
169
|
+
};
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
`ToolRegistry` provides `register`, `get`, `list`, `toSpecs`, and validated
|
|
173
|
+
`execute`. Read-only tools may run in parallel; mutating tools are serialized by
|
|
174
|
+
the agent loop.
|
|
175
|
+
|
|
176
|
+
## Permissions
|
|
177
|
+
|
|
178
|
+
`PermissionEngine` evaluates rules first, then the mode:
|
|
179
|
+
|
|
180
|
+
| Mode | Behavior |
|
|
181
|
+
| ------------------- | ------------------------------------------------ |
|
|
182
|
+
| `default` | Ask for mutating tools, allow read-only tools |
|
|
183
|
+
| `acceptEdits` | Allow edits inside the workspace; ask outside it |
|
|
184
|
+
| `plan` | Deny mutating tools; read-only allowed |
|
|
185
|
+
| `bypassPermissions` | Allow everything (use with care) |
|
|
186
|
+
| `custom` | Behaves like `default`; combine with rules |
|
|
187
|
+
|
|
188
|
+
```ts
|
|
189
|
+
const permissions = new PermissionEngine({
|
|
190
|
+
mode: "acceptEdits",
|
|
191
|
+
cwd: process.cwd(),
|
|
192
|
+
rules: [
|
|
193
|
+
{ tool: "bash", pattern: "git status", decision: "allow", scope: "session" },
|
|
194
|
+
{ tool: "bash", pattern: "rm -rf *", decision: "deny", scope: "session" },
|
|
195
|
+
],
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
permissions.decide({ tool: "read", input: { path: "a.ts" } }); // "allow"
|
|
199
|
+
permissions.decide({ tool: "bash", input: { command: "git status" } }); // "allow"
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
Deny beats allow. Dangerous commands (`rm -rf /`, `mkfs`, `curl … | sh`,
|
|
203
|
+
force-push, etc.) require approval unless `bypassPermissions` is set.
|
|
204
|
+
|
|
205
|
+
## Sessions and persistence
|
|
206
|
+
|
|
207
|
+
`SessionStore` uses SQLite (`node:sqlite`) plus a JSONL event mirror under the
|
|
208
|
+
data directory (default `~/.kilncraft`, override with `KILNCRAFT_DATA_DIR`).
|
|
209
|
+
|
|
210
|
+
```ts
|
|
211
|
+
const store = new SessionStore({ dataDir: "/tmp/kilncraft-data" });
|
|
212
|
+
|
|
213
|
+
const session = store.create({
|
|
214
|
+
cwd: process.cwd(),
|
|
215
|
+
model: "anthropic/claude-sonnet-4-5",
|
|
216
|
+
title: "My session",
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
store.appendMessage(session.id, { role: "user", content: "hello" });
|
|
220
|
+
const messages = store.readMessages(session.id);
|
|
221
|
+
const events = store.readEvents(session.id);
|
|
222
|
+
|
|
223
|
+
store.search("hello");
|
|
224
|
+
store.export(session.id, "md");
|
|
225
|
+
store.delete(session.id);
|
|
226
|
+
store.close();
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
`SessionStore` also provides `get`, `list`, `update`, `replaceMessages`, and
|
|
230
|
+
`sessionDirectory`.
|
|
231
|
+
|
|
232
|
+
## Compaction
|
|
233
|
+
|
|
234
|
+
```ts
|
|
235
|
+
import { compactMessages, estimateMessagesTokens } from "@kilncraft/core";
|
|
236
|
+
|
|
237
|
+
const used = estimateMessagesTokens(messages);
|
|
238
|
+
const result = compactMessages(messages, {
|
|
239
|
+
contextWindow: 200_000,
|
|
240
|
+
keepRecent: 6,
|
|
241
|
+
threshold: 0.8,
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
console.log(result.omitted, result.tokensBefore, result.tokensAfter);
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
`Agent.prompt` compacts automatically when the estimated context exceeds the
|
|
248
|
+
threshold, emitting `compaction.started` and `compaction.completed` events.
|
|
249
|
+
|
|
250
|
+
## Plugins
|
|
251
|
+
|
|
252
|
+
```ts
|
|
253
|
+
import { loadPlugins } from "@kilncraft/core";
|
|
254
|
+
|
|
255
|
+
const host = await loadPlugins(["./my-plugin.ts"], {
|
|
256
|
+
cwd: process.cwd(),
|
|
257
|
+
dataDir: "/tmp/kilncraft-data",
|
|
258
|
+
logger,
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
const tools = host.getTools();
|
|
262
|
+
const commands = host.getCommands();
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
Plugin hooks are `session.start`, `message.received`, `tool.before`,
|
|
266
|
+
`tool.after`, and `event`. See `@kilncraft/plugin-sdk` for types and an example.
|
|
267
|
+
|
|
268
|
+
## Checkpoints and audit
|
|
269
|
+
|
|
270
|
+
`CheckpointManager` snapshots files before edits and can restore them:
|
|
271
|
+
|
|
272
|
+
```ts
|
|
273
|
+
const checkpoints = new CheckpointManager("/tmp/kilncraft-data");
|
|
274
|
+
const checkpoint = checkpoints.snapshot(sessionID, "/repo/src/a.ts");
|
|
275
|
+
checkpoints.latest(sessionID);
|
|
276
|
+
checkpoints.restoreLatest(sessionID);
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
`AuditLog` appends JSONL permission decisions:
|
|
280
|
+
|
|
281
|
+
```ts
|
|
282
|
+
const audit = new AuditLog("/tmp/kilncraft-data/audit.jsonl");
|
|
283
|
+
audit.record({
|
|
284
|
+
sessionID,
|
|
285
|
+
tool: "bash",
|
|
286
|
+
decision: "allow",
|
|
287
|
+
cwd: process.cwd(),
|
|
288
|
+
inputHash: "sha256:…",
|
|
289
|
+
summary: "git status",
|
|
290
|
+
});
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
## Development
|
|
294
|
+
|
|
295
|
+
```sh
|
|
296
|
+
pnpm --filter @kilncraft/core build
|
|
297
|
+
pnpm --filter @kilncraft/core test
|
|
298
|
+
pnpm --filter @kilncraft/core typecheck
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
## Related packages
|
|
302
|
+
|
|
303
|
+
- `@kilncraft/shared` — types and schemas
|
|
304
|
+
- `@kilncraft/sdk` — in-process facade over this package
|
|
305
|
+
- `@kilncraft/server` — HTTP/SSE daemon
|
|
306
|
+
- `@kilncraft/tui` — terminal UI
|
|
307
|
+
- [Root README](../../README.md)
|