goatchain 0.0.31 → 0.0.33
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 +106 -174
- package/dist/index.d.ts +1 -1
- package/dist/index.js +425 -416
- package/dist/middleware/contextCompressionMiddleware.d.ts +169 -21
- package/dist/state/types.d.ts +55 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -28,7 +28,6 @@ hooks: {
|
|
|
28
28
|
```
|
|
29
29
|
|
|
30
30
|
If you need both, use:
|
|
31
|
-
|
|
32
31
|
- `preToolUse` to rewrite tool arguments/tool call
|
|
33
32
|
- `permissionRequest` to allow or block execution
|
|
34
33
|
|
|
@@ -116,7 +115,6 @@ const session = await agent.createSession({
|
|
|
116
115
|
interface CreateSessionOptions {
|
|
117
116
|
sessionId?: string // Custom session ID
|
|
118
117
|
model?: ModelRef // Optional model override
|
|
119
|
-
variants?: ModelRef[] // Optional fallback model refs
|
|
120
118
|
maxIterations?: number // Max agent loop iterations (default: 1000)
|
|
121
119
|
cwd?: string // Working directory for file operations
|
|
122
120
|
messageQueueConfig?: {
|
|
@@ -126,15 +124,14 @@ interface CreateSessionOptions {
|
|
|
126
124
|
requestParams?: {
|
|
127
125
|
temperature?: number // Model temperature
|
|
128
126
|
maxTokens?: number // Max output tokens
|
|
129
|
-
|
|
130
|
-
[key: string]: unknown // Provider-specific request params
|
|
127
|
+
topP?: number // Nucleus sampling parameter
|
|
131
128
|
}
|
|
132
129
|
}
|
|
133
130
|
```
|
|
134
131
|
|
|
135
132
|
### Working Directory (CWD) Configuration
|
|
136
133
|
|
|
137
|
-
You can set a working directory for the session, which will be automatically applied to
|
|
134
|
+
You can set a working directory for the session, which will be automatically applied to all file operation tools (Read, Write, Edit, Glob, Grep, Bash, AstGrepSearch, AstGrepReplace):
|
|
138
135
|
|
|
139
136
|
```typescript
|
|
140
137
|
// Set CWD when creating a session
|
|
@@ -276,7 +273,7 @@ for await (const event of session.receive()) {
|
|
|
276
273
|
break
|
|
277
274
|
|
|
278
275
|
case 'tool_call_start':
|
|
279
|
-
console.log(`\nCalling tool: ${event.
|
|
276
|
+
console.log(`\nCalling tool: ${event.name}`)
|
|
280
277
|
break
|
|
281
278
|
|
|
282
279
|
case 'tool_result':
|
|
@@ -292,28 +289,30 @@ for await (const event of session.receive()) {
|
|
|
292
289
|
console.log(`\nConversation done: ${event.stopReason}`)
|
|
293
290
|
console.log(`Total tokens: ${event.usage?.totalTokens}`)
|
|
294
291
|
break
|
|
292
|
+
|
|
293
|
+
case 'error':
|
|
294
|
+
console.error(`Error: ${event.error}`)
|
|
295
|
+
break
|
|
295
296
|
}
|
|
296
297
|
}
|
|
297
298
|
```
|
|
298
299
|
|
|
299
300
|
### Session Event Types
|
|
300
301
|
|
|
301
|
-
| Event Type
|
|
302
|
-
|
|
|
303
|
-
| `
|
|
304
|
-
| `
|
|
305
|
-
| `thinking_start`
|
|
306
|
-
| `
|
|
307
|
-
| `
|
|
308
|
-
| `
|
|
309
|
-
| `
|
|
310
|
-
| `
|
|
311
|
-
| `
|
|
312
|
-
| `
|
|
313
|
-
| `
|
|
314
|
-
| `
|
|
315
|
-
| `hook_evaluation` | Prompt-hook evaluation lifecycle | `hookName`, `phase`, `status` |
|
|
316
|
-
| `done` | Stream finished | `stopReason`, `modelStopReason`, `error`, `usage` |
|
|
302
|
+
| Event Type | Description | Key Fields |
|
|
303
|
+
| ----------------- | --------------------------- | --------------------- |
|
|
304
|
+
| `iteration_start` | Agent loop iteration begins | `iteration` |
|
|
305
|
+
| `text_delta` | Partial text response | `delta` |
|
|
306
|
+
| `thinking_start` | Reasoning phase begins | - |
|
|
307
|
+
| `thinking_delta` | Reasoning content | `delta` |
|
|
308
|
+
| `thinking_end` | Reasoning phase ends | - |
|
|
309
|
+
| `tool_call_start` | Tool invocation begins | `name`, `id` |
|
|
310
|
+
| `tool_call_delta` | Tool arguments stream | `delta` |
|
|
311
|
+
| `tool_call_end` | Tool call complete | `name`, `args` |
|
|
312
|
+
| `tool_result` | Tool execution result | `result`, `error` |
|
|
313
|
+
| `iteration_end` | Iteration complete | `usage`, `iteration` |
|
|
314
|
+
| `done` | Stream finished | `stopReason`, `usage` |
|
|
315
|
+
| `error` | Error occurred | `error` |
|
|
317
316
|
|
|
318
317
|
### Session State Management
|
|
319
318
|
|
|
@@ -322,7 +321,7 @@ for await (const event of session.receive()) {
|
|
|
322
321
|
console.log(session.messages) // Message[]
|
|
323
322
|
|
|
324
323
|
// Check session status
|
|
325
|
-
console.log(session.status) // '
|
|
324
|
+
console.log(session.status) // 'idle' | 'running' | 'completed' | 'error'
|
|
326
325
|
|
|
327
326
|
// Get token usage
|
|
328
327
|
console.log(session.usage)
|
|
@@ -382,8 +381,8 @@ for await (const event of session.receive()) {
|
|
|
382
381
|
}
|
|
383
382
|
}
|
|
384
383
|
|
|
385
|
-
// Session now contains
|
|
386
|
-
console.log(session.messages.length) //
|
|
384
|
+
// Session now contains full conversation history
|
|
385
|
+
console.log(session.messages.length) // 4 (2 user, 2 assistant)
|
|
387
386
|
```
|
|
388
387
|
|
|
389
388
|
### Resuming Sessions
|
|
@@ -420,7 +419,7 @@ for await (const event of resumed.receive()) {
|
|
|
420
419
|
}
|
|
421
420
|
```
|
|
422
421
|
|
|
423
|
-
### Session
|
|
422
|
+
### Session Lifecycle Hooks
|
|
424
423
|
|
|
425
424
|
```typescript
|
|
426
425
|
// Add message manually
|
|
@@ -432,9 +431,8 @@ session.addMessage({
|
|
|
432
431
|
// Save session state manually
|
|
433
432
|
await session.save()
|
|
434
433
|
|
|
435
|
-
//
|
|
436
|
-
|
|
437
|
-
session.messages.push({ role: 'assistant', content: 'Synthetic entry' })
|
|
434
|
+
// Clear session history
|
|
435
|
+
session.messages = []
|
|
438
436
|
```
|
|
439
437
|
|
|
440
438
|
## 🤖 Agent Configuration
|
|
@@ -479,9 +477,7 @@ interface AgentOptions {
|
|
|
479
477
|
agent.setModel({ provider: 'openai', modelId: 'gpt-4o-mini' })
|
|
480
478
|
|
|
481
479
|
// Switch to another concrete model client instance
|
|
482
|
-
const otherModelClient = createModel({
|
|
483
|
-
adapter: createOpenAIAdapter({ defaultModelId: 'gpt-4o' }),
|
|
484
|
-
})
|
|
480
|
+
const otherModelClient = createModel({ adapter: createOpenAIAdapter({ defaultModelId: 'gpt-4o' }) })
|
|
485
481
|
agent.setModel(otherModelClient)
|
|
486
482
|
```
|
|
487
483
|
|
|
@@ -508,22 +504,21 @@ await sessionManager.destroy('session-id')
|
|
|
508
504
|
|
|
509
505
|
GoatChain SDK exports the following built-in tools:
|
|
510
506
|
|
|
511
|
-
| Tool Class
|
|
512
|
-
|
|
|
513
|
-
| `ReadTool`
|
|
514
|
-
| `WriteTool`
|
|
515
|
-
| `EditTool`
|
|
516
|
-
| `GlobTool`
|
|
517
|
-
| `GrepTool`
|
|
518
|
-
| `BashTool`
|
|
519
|
-
| `WebSearchTool`
|
|
520
|
-
| `WebFetchTool`
|
|
521
|
-
| `
|
|
522
|
-
| `
|
|
523
|
-
| `
|
|
524
|
-
| `
|
|
525
|
-
| `
|
|
526
|
-
| `ExitPlanModeTool` | `ExitPlanMode` | Mode | Exit plan mode |
|
|
507
|
+
| Tool Class | Runtime Name | Category | Purpose |
|
|
508
|
+
| --- | --- | --- | --- |
|
|
509
|
+
| `ReadTool` | `Read` | File | Read file content (text, binary metadata, and selected converted formats) |
|
|
510
|
+
| `WriteTool` | `Write` | File | Create or overwrite files |
|
|
511
|
+
| `EditTool` | `Edit` | File | In-place text replacement edits |
|
|
512
|
+
| `GlobTool` | `Glob` | File/Search | Find files by glob pattern |
|
|
513
|
+
| `GrepTool` | `Grep` | File/Search | Search file contents by pattern |
|
|
514
|
+
| `BashTool` | `Bash` | Command | Execute shell commands |
|
|
515
|
+
| `WebSearchTool` | `WebSearch` | Web | Search the web (e.g. via Serper API) |
|
|
516
|
+
| `WebFetchTool` | `WebFetch` | Web | Fetch and extract content from a specific URL |
|
|
517
|
+
| `TodoWriteTool` | `TodoWrite` | Planning | Manage structured todo lists |
|
|
518
|
+
| `TodoPlanTool` | `TodoPlan` | Planning | Create/update planning todos for plan flows |
|
|
519
|
+
| `AskUserTool` | `AskUserQuestion` | Interaction | Ask the user structured follow-up questions |
|
|
520
|
+
| `EnterPlanModeTool` | `EnterPlanMode` | Mode | Enter plan mode |
|
|
521
|
+
| `ExitPlanModeTool` | `ExitPlanMode` | Mode | Exit plan mode |
|
|
527
522
|
|
|
528
523
|
```typescript
|
|
529
524
|
import {
|
|
@@ -626,14 +621,14 @@ tools.register(
|
|
|
626
621
|
|
|
627
622
|
**How each file tool uses `cwd`:**
|
|
628
623
|
|
|
629
|
-
| Tool
|
|
630
|
-
|
|
|
631
|
-
| `ReadTool`
|
|
632
|
-
| `WriteTool` | Writes/overwrites files
|
|
633
|
-
| `EditTool`
|
|
634
|
-
| `GlobTool`
|
|
635
|
-
| `GrepTool`
|
|
636
|
-
| `BashTool`
|
|
624
|
+
| Tool | What it does | How `cwd` is applied | Per-call override | Extra sandbox options |
|
|
625
|
+
| --- | --- | --- | --- | --- |
|
|
626
|
+
| `ReadTool` | Reads files (and some converted formats) | Relative `file_path` resolves from `cwd` | `file_path` can be absolute | `allowedDirectory`, `fileBlacklist`, `disableBlacklist` |
|
|
627
|
+
| `WriteTool` | Writes/overwrites files | Relative `file_path` resolves from `cwd` | `file_path` can be absolute | `allowedDirectory`, `fileBlacklist`, `disableBlacklist` |
|
|
628
|
+
| `EditTool` | Replaces `old_string` with `new_string` in a file | Relative `file_path` resolves from `cwd` | `file_path` can be absolute | `fileBlacklist`, `disableBlacklist` |
|
|
629
|
+
| `GlobTool` | Finds files by pattern | Search root defaults to `cwd` | `path` argument can change search root | `fileBlacklist`, `disableBlacklist` |
|
|
630
|
+
| `GrepTool` | Searches text content in files | Search runs under `cwd` | `path` argument narrows search scope | `fileBlacklist`, `disableBlacklist` |
|
|
631
|
+
| `BashTool` | Runs shell commands | Commands execute in `cwd` | `workdir` argument overrides per call | None |
|
|
637
632
|
|
|
638
633
|
**Directory & Protection Options:**
|
|
639
634
|
|
|
@@ -746,41 +741,26 @@ interface AgentHooks {
|
|
|
746
741
|
userPromptSubmit?:
|
|
747
742
|
| ((ctx: UserPromptSubmitContext) => Promise<UserPromptSubmitResult>)
|
|
748
743
|
| PromptHookEntry
|
|
749
|
-
| Array<
|
|
750
|
-
| ((ctx: UserPromptSubmitContext) => Promise<UserPromptSubmitResult>)
|
|
751
|
-
| PromptHookEntry
|
|
752
|
-
>
|
|
744
|
+
| Array<((ctx: UserPromptSubmitContext) => Promise<UserPromptSubmitResult>) | PromptHookEntry>
|
|
753
745
|
|
|
754
746
|
// Tool lifecycle
|
|
755
747
|
// - Can modify tool call with modifiedToolCall
|
|
756
748
|
preToolUse?:
|
|
757
749
|
| ((ctx: ToolHookContext) => Promise<PreToolUseResult | void>)
|
|
758
750
|
| PromptHookEntry
|
|
759
|
-
| Array<
|
|
760
|
-
| ((ctx: ToolHookContext) => Promise<PreToolUseResult | void>)
|
|
761
|
-
| PromptHookEntry
|
|
762
|
-
>
|
|
751
|
+
| Array<((ctx: ToolHookContext) => Promise<PreToolUseResult | void>) | PromptHookEntry>
|
|
763
752
|
permissionRequest?:
|
|
764
753
|
| ((ctx: ToolHookContext) => Promise<PermissionRequestResult>)
|
|
765
754
|
| PromptHookEntry
|
|
766
|
-
| Array<
|
|
767
|
-
| ((ctx: ToolHookContext) => Promise<PermissionRequestResult>)
|
|
768
|
-
| PromptHookEntry
|
|
769
|
-
>
|
|
755
|
+
| Array<((ctx: ToolHookContext) => Promise<PermissionRequestResult>) | PromptHookEntry>
|
|
770
756
|
postToolUse?:
|
|
771
757
|
| ((ctx: ToolHookContext, result: unknown) => Promise<void>)
|
|
772
758
|
| PromptHookEntry
|
|
773
|
-
| Array<
|
|
774
|
-
| ((ctx: ToolHookContext, result: unknown) => Promise<void>)
|
|
775
|
-
| PromptHookEntry
|
|
776
|
-
>
|
|
759
|
+
| Array<((ctx: ToolHookContext, result: unknown) => Promise<void>) | PromptHookEntry>
|
|
777
760
|
postToolUseFailure?:
|
|
778
761
|
| ((ctx: ToolHookContext, error: Error) => Promise<void>)
|
|
779
762
|
| PromptHookEntry
|
|
780
|
-
| Array<
|
|
781
|
-
| ((ctx: ToolHookContext, error: Error) => Promise<void>)
|
|
782
|
-
| PromptHookEntry
|
|
783
|
-
>
|
|
763
|
+
| Array<((ctx: ToolHookContext, error: Error) => Promise<void>) | PromptHookEntry>
|
|
784
764
|
|
|
785
765
|
// Subagent lifecycle (used by parallel task/subagent middleware)
|
|
786
766
|
subagentStart?: (ctx: SubagentStartContext) => Promise<void>
|
|
@@ -1017,12 +997,7 @@ const agent = new Agent({
|
|
|
1017
997
|
name: 'MyAgent',
|
|
1018
998
|
systemPrompt: 'You are helpful.',
|
|
1019
999
|
model,
|
|
1020
|
-
tools: (()
|
|
1021
|
-
const tools = new ToolRegistry()
|
|
1022
|
-
tools.register(new ReadTool())
|
|
1023
|
-
tools.register(new WriteTool())
|
|
1024
|
-
return tools
|
|
1025
|
-
})(),
|
|
1000
|
+
tools: new ToolRegistry().register(new ReadTool()).register(new WriteTool()),
|
|
1026
1001
|
})
|
|
1027
1002
|
|
|
1028
1003
|
// Create session with hooks
|
|
@@ -1257,7 +1232,7 @@ const session = await agent.createSession({
|
|
|
1257
1232
|
|
|
1258
1233
|
### Tool Context
|
|
1259
1234
|
|
|
1260
|
-
The `toolContext` parameter in `send()` and `receive()`
|
|
1235
|
+
The `toolContext` parameter in `send()` and `receive()` allows passing additional context:
|
|
1261
1236
|
|
|
1262
1237
|
```typescript
|
|
1263
1238
|
session.send('Do something risky', {
|
|
@@ -1270,14 +1245,8 @@ session.send('Do something risky', {
|
|
|
1270
1245
|
tool_call_id_456: { approved: false, reason: 'Too dangerous' },
|
|
1271
1246
|
},
|
|
1272
1247
|
},
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
tool_call_id_789: {
|
|
1276
|
-
framework: 'React',
|
|
1277
|
-
styling: 'Tailwind CSS',
|
|
1278
|
-
},
|
|
1279
|
-
},
|
|
1280
|
-
},
|
|
1248
|
+
// Your custom context
|
|
1249
|
+
custom: { userId: '123', environment: 'production' },
|
|
1281
1250
|
},
|
|
1282
1251
|
})
|
|
1283
1252
|
```
|
|
@@ -1294,7 +1263,7 @@ outer:before → inner:before → exec (model.stream) → inner:after → outer:
|
|
|
1294
1263
|
|
|
1295
1264
|
```typescript
|
|
1296
1265
|
// Add named middleware (recommended)
|
|
1297
|
-
|
|
1266
|
+
agent.use(async (state, next) => {
|
|
1298
1267
|
const start = Date.now()
|
|
1299
1268
|
console.log(`[${state.iteration}] Before model call`)
|
|
1300
1269
|
|
|
@@ -1311,7 +1280,7 @@ agent.removeMiddleware('logging')
|
|
|
1311
1280
|
console.log(agent.middlewareNames) // ['logging', 'compression', ...]
|
|
1312
1281
|
|
|
1313
1282
|
// Use unsubscribe function
|
|
1314
|
-
const unsubscribe =
|
|
1283
|
+
const unsubscribe = agent.use(middleware, 'temp')
|
|
1315
1284
|
unsubscribe() // Remove middleware
|
|
1316
1285
|
```
|
|
1317
1286
|
|
|
@@ -1325,10 +1294,10 @@ Adds planning phase before execution:
|
|
|
1325
1294
|
import { createPlanModeMiddleware } from 'goatchain'
|
|
1326
1295
|
|
|
1327
1296
|
// Automatically named 'plan_mode'
|
|
1328
|
-
|
|
1297
|
+
agent.use(createPlanModeMiddleware())
|
|
1329
1298
|
|
|
1330
1299
|
// With custom configuration
|
|
1331
|
-
|
|
1300
|
+
agent.use(
|
|
1332
1301
|
createPlanModeMiddleware({
|
|
1333
1302
|
name: 'my-plan', // Custom name
|
|
1334
1303
|
planPrompt: 'Create a detailed plan...', // Custom prompt
|
|
@@ -1338,36 +1307,35 @@ await agent.use(
|
|
|
1338
1307
|
|
|
1339
1308
|
#### Context Compression Middleware
|
|
1340
1309
|
|
|
1341
|
-
Automatically compresses context
|
|
1342
|
-
|
|
1343
|
-
- reuses any persisted rolling summary first
|
|
1344
|
-
- removes old `tool` messages before touching the current round
|
|
1345
|
-
- preserves the last user round
|
|
1346
|
-
- performs at most one AI summary pass per overflow event
|
|
1347
|
-
- guarantees the final prompt stays within `contextLength` or fails locally before the model call
|
|
1348
|
-
- can emit per-stage snapshots in the large E2E example for inspection
|
|
1310
|
+
Automatically compresses context when token limit is reached using a two-stage strategy:
|
|
1349
1311
|
|
|
1350
1312
|
```typescript
|
|
1351
1313
|
import { createContextCompressionMiddleware } from 'goatchain'
|
|
1352
1314
|
|
|
1353
1315
|
// Automatically named 'context_compression'
|
|
1354
|
-
|
|
1316
|
+
agent.use(
|
|
1355
1317
|
createContextCompressionMiddleware({
|
|
1356
|
-
|
|
1318
|
+
maxTokens: 128000,
|
|
1319
|
+
protectedTurns: 2, // Keep last 2 conversation turns
|
|
1320
|
+
model: model,
|
|
1321
|
+
stateStore: agent.stateStore,
|
|
1322
|
+
toolCompressionTarget: 0.45, // Compress to 45% of maxTokens
|
|
1323
|
+
minKeepToolResults: 5, // Keep last 5 tool results
|
|
1324
|
+
// Optional: Enable detailed logging
|
|
1325
|
+
enableLogging: true,
|
|
1326
|
+
logFilePath: 'compression-logs.jsonl',
|
|
1357
1327
|
}),
|
|
1358
1328
|
)
|
|
1359
1329
|
```
|
|
1360
1330
|
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
See [`src/spec/middleware.md`](./src/spec/middleware.md) for the full middleware and compression spec.
|
|
1331
|
+
See [Context Compression Logging Guide](./docs/context-compression-logging.md) for details on monitoring compression behavior.
|
|
1364
1332
|
|
|
1365
1333
|
### Custom Middleware Examples
|
|
1366
1334
|
|
|
1367
1335
|
#### Logging Middleware
|
|
1368
1336
|
|
|
1369
1337
|
```typescript
|
|
1370
|
-
|
|
1338
|
+
agent.use(async (state, next) => {
|
|
1371
1339
|
console.log(`Iteration ${state.iteration}:`, {
|
|
1372
1340
|
messages: state.messages.length,
|
|
1373
1341
|
pendingTools: state.pendingToolCalls.length,
|
|
@@ -1387,7 +1355,7 @@ await agent.use(async (state, next) => {
|
|
|
1387
1355
|
#### Error Handling Middleware
|
|
1388
1356
|
|
|
1389
1357
|
```typescript
|
|
1390
|
-
|
|
1358
|
+
agent.use(async (state, next) => {
|
|
1391
1359
|
try {
|
|
1392
1360
|
return await next(state)
|
|
1393
1361
|
} catch (error) {
|
|
@@ -1407,7 +1375,7 @@ import { RateLimiter } from 'some-rate-limiter'
|
|
|
1407
1375
|
|
|
1408
1376
|
const limiter = new RateLimiter({ requestsPerMinute: 60 })
|
|
1409
1377
|
|
|
1410
|
-
|
|
1378
|
+
agent.use(async (state, next) => {
|
|
1411
1379
|
await limiter.acquire()
|
|
1412
1380
|
return next(state)
|
|
1413
1381
|
}, 'rate-limiter')
|
|
@@ -1416,7 +1384,7 @@ await agent.use(async (state, next) => {
|
|
|
1416
1384
|
#### Custom Retry Middleware
|
|
1417
1385
|
|
|
1418
1386
|
```typescript
|
|
1419
|
-
|
|
1387
|
+
agent.use(async (state, next) => {
|
|
1420
1388
|
let retries = 3
|
|
1421
1389
|
|
|
1422
1390
|
while (retries > 0) {
|
|
@@ -1446,12 +1414,10 @@ interface AgentLoopState {
|
|
|
1446
1414
|
iteration: number // Current iteration number
|
|
1447
1415
|
pendingToolCalls: ToolCallWithResult[] // Pending tool executions
|
|
1448
1416
|
currentResponse: string // Current LLM response
|
|
1449
|
-
currentThinking?: string // Current reasoning content, if the model emits it
|
|
1450
1417
|
shouldContinue: boolean // Whether to continue loop
|
|
1451
1418
|
stopReason?: string // Reason for stopping
|
|
1452
|
-
usage
|
|
1419
|
+
usage?: Usage // Token usage
|
|
1453
1420
|
error?: Error // Error if any
|
|
1454
|
-
metadata: Record<string, unknown> // Middleware/hook shared runtime data
|
|
1455
1421
|
}
|
|
1456
1422
|
```
|
|
1457
1423
|
|
|
@@ -1574,15 +1540,8 @@ Implement custom state stores:
|
|
|
1574
1540
|
interface StateStore {
|
|
1575
1541
|
deleteOnComplete: boolean
|
|
1576
1542
|
|
|
1577
|
-
save<T>(sessionId: string, key: string, data: T): Promise<void>
|
|
1578
|
-
load<T>(sessionId: string, key: string): Promise<T | undefined>
|
|
1579
|
-
delete(sessionId: string, key: string): Promise<void>
|
|
1580
|
-
listKeys(sessionId: string): Promise<string[]>
|
|
1581
|
-
listSessions(): Promise<string[]>
|
|
1582
|
-
|
|
1583
|
-
// Checkpoint helpers
|
|
1584
1543
|
saveCheckpoint(checkpoint: AgentLoopCheckpoint): Promise<void>
|
|
1585
|
-
loadCheckpoint(sessionId: string): Promise<AgentLoopCheckpoint |
|
|
1544
|
+
loadCheckpoint(sessionId: string): Promise<AgentLoopCheckpoint | null>
|
|
1586
1545
|
deleteCheckpoint(sessionId: string): Promise<void>
|
|
1587
1546
|
listCheckpoints(): Promise<AgentLoopCheckpoint[]>
|
|
1588
1547
|
}
|
|
@@ -1600,8 +1559,17 @@ interface AgentLoopCheckpoint {
|
|
|
1600
1559
|
### Manual Checkpoint Management
|
|
1601
1560
|
|
|
1602
1561
|
```typescript
|
|
1603
|
-
//
|
|
1604
|
-
|
|
1562
|
+
// Save checkpoint manually
|
|
1563
|
+
await stateStore.saveCheckpoint({
|
|
1564
|
+
sessionId: session.id,
|
|
1565
|
+
messages: session.messages,
|
|
1566
|
+
iteration: 3,
|
|
1567
|
+
usage: session.usage,
|
|
1568
|
+
createdAt: Date.now(),
|
|
1569
|
+
updatedAt: Date.now(),
|
|
1570
|
+
})
|
|
1571
|
+
|
|
1572
|
+
// Load checkpoint
|
|
1605
1573
|
const checkpoint = await stateStore.loadCheckpoint('session-id')
|
|
1606
1574
|
|
|
1607
1575
|
// List all checkpoints
|
|
@@ -1681,7 +1649,7 @@ for await (const event of session.receive()) {
|
|
|
1681
1649
|
if (event.type === 'text_delta') {
|
|
1682
1650
|
process.stdout.write(event.delta)
|
|
1683
1651
|
} else if (event.type === 'tool_call_start') {
|
|
1684
|
-
console.log(`\nCalling: ${event.
|
|
1652
|
+
console.log(`\nCalling: ${event.name}`)
|
|
1685
1653
|
} else if (event.type === 'tool_result') {
|
|
1686
1654
|
console.log(`Result: ${JSON.stringify(event.result).slice(0, 100)}...`)
|
|
1687
1655
|
}
|
|
@@ -1762,7 +1730,7 @@ const agent = new Agent({
|
|
|
1762
1730
|
})
|
|
1763
1731
|
|
|
1764
1732
|
// Add logging middleware
|
|
1765
|
-
|
|
1733
|
+
agent.use(async (state, next) => {
|
|
1766
1734
|
console.log(`\n=== Iteration ${state.iteration} ===`)
|
|
1767
1735
|
const result = await next(state)
|
|
1768
1736
|
console.log(`Tokens used: ${result.usage?.totalTokens || 0}`)
|
|
@@ -1770,7 +1738,7 @@ await agent.use(async (state, next) => {
|
|
|
1770
1738
|
}, 'logger')
|
|
1771
1739
|
|
|
1772
1740
|
// Add plan mode
|
|
1773
|
-
|
|
1741
|
+
agent.use(createPlanModeMiddleware())
|
|
1774
1742
|
|
|
1775
1743
|
const session = await agent.createSession()
|
|
1776
1744
|
session.send('Create a todo list app with React and TypeScript')
|
|
@@ -1836,12 +1804,7 @@ import {
|
|
|
1836
1804
|
Agent,
|
|
1837
1805
|
createModel,
|
|
1838
1806
|
createOpenAIAdapter,
|
|
1839
|
-
|
|
1840
|
-
ReadTool,
|
|
1841
|
-
WriteTool,
|
|
1842
|
-
EditTool,
|
|
1843
|
-
GlobTool,
|
|
1844
|
-
GrepTool,
|
|
1807
|
+
createBuiltinTools,
|
|
1845
1808
|
} from 'goatchain'
|
|
1846
1809
|
|
|
1847
1810
|
const model = createModel({
|
|
@@ -1851,18 +1814,11 @@ const model = createModel({
|
|
|
1851
1814
|
}),
|
|
1852
1815
|
})
|
|
1853
1816
|
|
|
1854
|
-
const tools = new ToolRegistry()
|
|
1855
|
-
tools.register(new ReadTool())
|
|
1856
|
-
tools.register(new WriteTool())
|
|
1857
|
-
tools.register(new EditTool())
|
|
1858
|
-
tools.register(new GlobTool())
|
|
1859
|
-
tools.register(new GrepTool())
|
|
1860
|
-
|
|
1861
1817
|
const agent = new Agent({
|
|
1862
1818
|
name: 'File Agent',
|
|
1863
1819
|
systemPrompt: 'You are a file management assistant.',
|
|
1864
1820
|
model,
|
|
1865
|
-
tools,
|
|
1821
|
+
tools: createBuiltinTools(), // All file tools included
|
|
1866
1822
|
})
|
|
1867
1823
|
|
|
1868
1824
|
// Set working directory at session creation
|
|
@@ -1969,7 +1925,7 @@ for await (const event of session.receive()) {
|
|
|
1969
1925
|
if (event.type === 'text_delta') {
|
|
1970
1926
|
process.stdout.write(event.delta)
|
|
1971
1927
|
} else if (event.type === 'tool_call_start') {
|
|
1972
|
-
console.log(`\n[Tool] ${event.
|
|
1928
|
+
console.log(`\n[Tool] ${event.name}`)
|
|
1973
1929
|
}
|
|
1974
1930
|
}
|
|
1975
1931
|
```
|
|
@@ -2158,7 +2114,7 @@ session.setCwd('/path/to/project')
|
|
|
2158
2114
|
#### Properties
|
|
2159
2115
|
|
|
2160
2116
|
- `id: string` - Session ID
|
|
2161
|
-
- `status: SessionStatus` - Session status (
|
|
2117
|
+
- `status: SessionStatus` - Session status ('idle' | 'running' | 'completed' | 'error')
|
|
2162
2118
|
- `messages: Message[]` - Message history
|
|
2163
2119
|
- `usage: Usage` - Token usage statistics
|
|
2164
2120
|
- `createdAt: number` - Creation timestamp
|
|
@@ -2169,12 +2125,9 @@ session.setCwd('/path/to/project')
|
|
|
2169
2125
|
```typescript
|
|
2170
2126
|
interface Message {
|
|
2171
2127
|
role: 'system' | 'user' | 'assistant' | 'tool'
|
|
2172
|
-
content:
|
|
2173
|
-
|
|
2174
|
-
|
|
2175
|
-
tool_call_id?: string // Tool messages
|
|
2176
|
-
name?: string
|
|
2177
|
-
isError?: boolean // Tool messages
|
|
2128
|
+
content: string | ToolCall[] | ToolResult[]
|
|
2129
|
+
name?: string // For tool messages
|
|
2130
|
+
toolCallId?: string // For tool messages
|
|
2178
2131
|
}
|
|
2179
2132
|
```
|
|
2180
2133
|
|
|
@@ -2192,17 +2145,10 @@ interface Usage {
|
|
|
2192
2145
|
|
|
2193
2146
|
```typescript
|
|
2194
2147
|
type AgentEvent =
|
|
2195
|
-
| TextStartEvent
|
|
2196
2148
|
| TextDeltaEvent
|
|
2197
|
-
| TextEndEvent
|
|
2198
2149
|
| ToolCallStartEvent
|
|
2199
2150
|
| ToolCallDeltaEvent
|
|
2200
2151
|
| ToolCallEndEvent
|
|
2201
|
-
| ToolOutputStartEvent
|
|
2202
|
-
| ToolOutputDeltaEvent
|
|
2203
|
-
| ToolApprovalRequestedEvent
|
|
2204
|
-
| RequiresActionEvent
|
|
2205
|
-
| ToolSkippedEvent
|
|
2206
2152
|
| ToolResultEvent
|
|
2207
2153
|
| ThinkingStartEvent
|
|
2208
2154
|
| ThinkingDeltaEvent
|
|
@@ -2210,6 +2156,7 @@ type AgentEvent =
|
|
|
2210
2156
|
| IterationStartEvent
|
|
2211
2157
|
| IterationEndEvent
|
|
2212
2158
|
| DoneEvent
|
|
2159
|
+
| ErrorEvent
|
|
2213
2160
|
|
|
2214
2161
|
interface TextDeltaEvent {
|
|
2215
2162
|
type: 'text_delta'
|
|
@@ -2218,15 +2165,8 @@ interface TextDeltaEvent {
|
|
|
2218
2165
|
|
|
2219
2166
|
interface ToolCallStartEvent {
|
|
2220
2167
|
type: 'tool_call_start'
|
|
2221
|
-
|
|
2222
|
-
|
|
2223
|
-
}
|
|
2224
|
-
|
|
2225
|
-
interface RequiresActionEvent {
|
|
2226
|
-
type: 'requires_action'
|
|
2227
|
-
kind: 'tool_approval' | 'ask_user'
|
|
2228
|
-
checkpoint?: AgentLoopCheckpoint
|
|
2229
|
-
checkpointRef?: { sessionId: string }
|
|
2168
|
+
id: string
|
|
2169
|
+
name: string
|
|
2230
2170
|
}
|
|
2231
2171
|
|
|
2232
2172
|
interface ToolResultEvent {
|
|
@@ -2238,7 +2178,6 @@ interface ToolResultEvent {
|
|
|
2238
2178
|
|
|
2239
2179
|
interface DoneEvent {
|
|
2240
2180
|
type: 'done'
|
|
2241
|
-
finalResponse?: string
|
|
2242
2181
|
stopReason:
|
|
2243
2182
|
| 'max_iterations'
|
|
2244
2183
|
| 'final_response'
|
|
@@ -2246,13 +2185,6 @@ interface DoneEvent {
|
|
|
2246
2185
|
| 'cancelled'
|
|
2247
2186
|
| 'approval_required'
|
|
2248
2187
|
| 'max_follow_ups'
|
|
2249
|
-
modelStopReason?: 'tool_call' | 'final' | 'length' | 'error' | 'cancelled'
|
|
2250
|
-
error?: {
|
|
2251
|
-
code?: string
|
|
2252
|
-
message: string
|
|
2253
|
-
status?: number
|
|
2254
|
-
retryable?: boolean
|
|
2255
|
-
}
|
|
2256
2188
|
usage?: Usage
|
|
2257
2189
|
}
|
|
2258
2190
|
```
|
package/dist/index.d.ts
CHANGED
|
@@ -37,4 +37,4 @@ export { AskUserTool, BashTool, EditTool, GlobTool, GrepTool, ReadTool, softWrap
|
|
|
37
37
|
export type { BashArgs, BashResult, RiskLevel, ToolApprovalResult, ToolApprovalSettings, ToolApprovalStrategy, ToolExecutionContext, ToolExecutionContextInput, ToolRunUsage, } from './tool';
|
|
38
38
|
export type { AskUserArgs, AskUserResult, DeliveryMode, EditArgs, EditResult, FetchFormat, GlobArgs, GlobResult, GrepArgs, GrepOutputMode, GrepResult, Question, QuestionOption, ReadArgs, ReadResult, TodoItem, TodoPlanArgs, TodoPlanResult, TodoStatus, TodoWriteArgs, TodoWriteResult, WebFetchArgs, WebFetchResult, WebSearchArgs, WebSearchResult, WriteArgs, WriteResult, } from './tool';
|
|
39
39
|
export * from './tool/converters';
|
|
40
|
-
export type { AgentEvent, AgentEventType, AgentLoopCheckpoint, AssistantMessage, BaseEvent, CallModelOnceOptions, CallModelOnceResult, CallToolResult, CompletionAssessedEvent, CompressionEndEvent, CompressionStartEvent, ContentBlock, DoneEvent, HookEvaluationEvent, ImageContent, IterationEndEvent, IterationStartEvent, JSONSchemaProperty, Message, MessageContent, MessageRole,
|
|
40
|
+
export type { AgentEvent, AgentEventType, AgentLoopCheckpoint, AssistantMessage, BaseEvent, CallModelOnceOptions, CallModelOnceResult, CallToolResult, CompletionAssessedEvent, CompressionEndEvent, CompressionStartEvent, ContentBlock, DoneEvent, HookEvaluationEvent, ImageContent, IterationEndEvent, IterationStartEvent, JSONSchemaProperty, Message, MessageContent, MessageRole, SystemMessage, TextContent, TextDeltaEvent, TextEndEvent, TextStartEvent, ThinkingDeltaEvent, Tool, ToolCall, ToolCallDeltaEvent, ToolCallEndEvent, ToolCallStartEvent, ToolMessage, ToolResultEvent, Usage, UserMessage, } from './types';
|