goatchain 0.0.23 → 0.0.25
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 +228 -71
- package/dist/agent/agent.d.ts +4 -4
- package/dist/agent/hooks/index.d.ts +1 -1
- package/dist/agent/hooks/manager.d.ts +10 -3
- package/dist/agent/hooks/types.d.ts +95 -8
- package/dist/agent/index.d.ts +3 -2
- package/dist/agent/middleware.d.ts +31 -2
- package/dist/agent/tokenCounter.d.ts +48 -2
- package/dist/agent/types.d.ts +15 -1
- package/dist/index.d.ts +12 -7
- package/dist/index.js +566 -542
- package/dist/lib/access-control/index.d.ts +1 -1
- package/dist/lib/access-control/policies.d.ts +10 -0
- package/dist/lib/session-blob.d.ts +50 -0
- package/dist/mcp-client/manager.d.ts +2 -0
- package/dist/middleware/attachmentMiddleware.d.ts +34 -0
- package/dist/middleware/commitModeMiddleware.d.ts +1 -1
- package/dist/middleware/contextCompressionMiddleware.d.ts +28 -2
- package/dist/middleware/envInfoMiddleware.d.ts +6 -0
- package/dist/middleware/gitUtils.d.ts +4 -0
- package/dist/middleware/longRunningMiddleware.d.ts +100 -0
- package/dist/middleware/parallelSubagentMiddleware.d.ts +2 -2
- package/dist/middleware/planModeMiddleware.d.ts +3 -3
- package/dist/middleware/reviewMiddleware.d.ts +1 -1
- package/dist/middleware/skillsMiddleware.d.ts +8 -0
- package/dist/model/anthropic/createAnthropicAdapter.d.ts +1 -1
- package/dist/model/codex/createCodexAdapter.d.ts +1 -1
- package/dist/model/index.d.ts +2 -0
- package/dist/model/openai/createOpenAIResponsesAdapter.d.ts +28 -0
- package/dist/session/completion/composite.d.ts +25 -0
- package/dist/session/completion/index.d.ts +8 -0
- package/dist/session/completion/strategies/reflection-decision-tool.d.ts +51 -0
- package/dist/session/completion/strategies/rule-based.d.ts +16 -0
- package/dist/session/completion/strategies/self-reflection.d.ts +54 -0
- package/dist/session/completion/strategies/todo-based.d.ts +17 -0
- package/dist/session/completion/types.d.ts +53 -0
- package/dist/session/executors/ToolExecutor.d.ts +4 -4
- package/dist/session/session.d.ts +9 -0
- package/dist/state/types.d.ts +3 -2
- package/dist/subagent/index.d.ts +1 -0
- package/dist/subagent/self-reflection-critic.d.ts +35 -0
- package/dist/tool/builtin/bash.d.ts +24 -0
- package/dist/tool/builtin/edit.d.ts +12 -0
- package/dist/tool/builtin/index.d.ts +2 -2
- package/dist/tool/builtin/pathProtection.d.ts +25 -0
- package/dist/tool/builtin/read.d.ts +69 -112
- package/dist/tool/builtin/task.d.ts +8 -0
- package/dist/tool/builtin/webFetch.d.ts +27 -4
- package/dist/tool/builtin/write.d.ts +15 -0
- package/dist/tool/index.d.ts +2 -2
- package/dist/types/common.d.ts +35 -0
- package/dist/types/event.d.ts +55 -3
- package/dist/types/snapshot.d.ts +1 -1
- package/package.json +5 -3
- package/dist/acp-server.d.ts +0 -28
- package/dist/server/core/event-store.d.ts +0 -15
- package/dist/server/core/mode-manager.d.ts +0 -17
- package/dist/server/core/state-store.d.ts +0 -11
- package/dist/server/core/types.d.ts +0 -66
- package/dist/server/http/server.d.ts +0 -27
- package/dist/server/http/sse.d.ts +0 -4
- package/dist/server/http/static.d.ts +0 -2
- package/dist/server/http/uploads.d.ts +0 -27
- package/dist/server/http/utils.d.ts +0 -5
- package/dist/server/index.d.ts +0 -36
- package/dist/server/ports/findAvailablePort.d.ts +0 -9
- package/dist/server/use-cases/query.d.ts +0 -27
package/README.md
CHANGED
|
@@ -4,6 +4,33 @@
|
|
|
4
4
|
|
|
5
5
|
[](https://badge.fury.io/js/goatchain)
|
|
6
6
|
|
|
7
|
+
## ⚠️ Breaking Change: `preToolUse` vs `permissionRequest`
|
|
8
|
+
|
|
9
|
+
`preToolUse` is now only for mutating tool calls (`modifiedToolCall`).
|
|
10
|
+
Permission decisions (`allow` / `deny`) must be handled in `permissionRequest`.
|
|
11
|
+
|
|
12
|
+
### Migration
|
|
13
|
+
|
|
14
|
+
Before (old pattern):
|
|
15
|
+
|
|
16
|
+
```typescript
|
|
17
|
+
hooks: {
|
|
18
|
+
preToolUse: async () => ({ allow: true }),
|
|
19
|
+
}
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
After (current pattern):
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
hooks: {
|
|
26
|
+
permissionRequest: async () => ({ allow: true }),
|
|
27
|
+
}
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
If you need both, use:
|
|
31
|
+
- `preToolUse` to rewrite tool arguments/tool call
|
|
32
|
+
- `permissionRequest` to allow or block execution
|
|
33
|
+
|
|
7
34
|
## 📦 Installation
|
|
8
35
|
|
|
9
36
|
```bash
|
|
@@ -360,8 +387,9 @@ const agent = new Agent({
|
|
|
360
387
|
model,
|
|
361
388
|
tools, // Optional: ToolRegistry instance
|
|
362
389
|
stateStore, // Optional: for persistence
|
|
363
|
-
|
|
364
|
-
|
|
390
|
+
middleware: [], // Optional: middlewares to register on startup
|
|
391
|
+
mcpServers: [], // Optional: MCP server configs
|
|
392
|
+
enableLogging: false, // Optional: internal logs
|
|
365
393
|
})
|
|
366
394
|
```
|
|
367
395
|
|
|
@@ -369,14 +397,15 @@ const agent = new Agent({
|
|
|
369
397
|
|
|
370
398
|
```typescript
|
|
371
399
|
interface AgentOptions {
|
|
400
|
+
id?: string // Optional custom agent ID
|
|
372
401
|
name: string // Agent name
|
|
373
402
|
systemPrompt: string // System instructions
|
|
374
|
-
model: ModelClient
|
|
403
|
+
model: ModelClient // LLM client
|
|
375
404
|
tools?: ToolRegistry // Tool registry (not an array)
|
|
376
405
|
stateStore?: StateStore // Persistence layer
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
406
|
+
middleware?: Middleware[] // Optional middleware list
|
|
407
|
+
mcpServers?: MCPServerConfig[] // Optional MCP server configs
|
|
408
|
+
enableLogging?: boolean // Internal debug logs
|
|
380
409
|
}
|
|
381
410
|
```
|
|
382
411
|
|
|
@@ -384,10 +413,11 @@ interface AgentOptions {
|
|
|
384
413
|
|
|
385
414
|
```typescript
|
|
386
415
|
// Pin to specific model
|
|
387
|
-
agent.setModel('gpt-4o-mini')
|
|
416
|
+
agent.setModel({ provider: 'openai', modelId: 'gpt-4o-mini' })
|
|
388
417
|
|
|
389
|
-
//
|
|
390
|
-
|
|
418
|
+
// Switch to another concrete model client instance
|
|
419
|
+
const otherModelClient = createModel({ adapter: createOpenAIAdapter({ defaultModelId: 'gpt-4o' }) })
|
|
420
|
+
agent.setModel(otherModelClient)
|
|
391
421
|
```
|
|
392
422
|
|
|
393
423
|
### Session Manager
|
|
@@ -411,7 +441,23 @@ await sessionManager.destroy('session-id')
|
|
|
411
441
|
|
|
412
442
|
### Using Built-in Tools
|
|
413
443
|
|
|
414
|
-
GoatChain
|
|
444
|
+
GoatChain SDK exports the following built-in tools:
|
|
445
|
+
|
|
446
|
+
| Tool Class | Runtime Name | Category | Purpose |
|
|
447
|
+
| --- | --- | --- | --- |
|
|
448
|
+
| `ReadTool` | `Read` | File | Read file content (text, binary metadata, and selected converted formats) |
|
|
449
|
+
| `WriteTool` | `Write` | File | Create or overwrite files |
|
|
450
|
+
| `EditTool` | `Edit` | File | In-place text replacement edits |
|
|
451
|
+
| `GlobTool` | `Glob` | File/Search | Find files by glob pattern |
|
|
452
|
+
| `GrepTool` | `Grep` | File/Search | Search file contents by pattern |
|
|
453
|
+
| `BashTool` | `Bash` | Command | Execute shell commands |
|
|
454
|
+
| `WebSearchTool` | `WebSearch` | Web | Search the web (e.g. via Serper API) |
|
|
455
|
+
| `WebFetchTool` | `WebFetch` | Web | Fetch and extract content from a specific URL |
|
|
456
|
+
| `TodoWriteTool` | `TodoWrite` | Planning | Manage structured todo lists |
|
|
457
|
+
| `TodoPlanTool` | `TodoPlan` | Planning | Create/update planning todos for plan flows |
|
|
458
|
+
| `AskUserTool` | `AskUserQuestion` | Interaction | Ask the user structured follow-up questions |
|
|
459
|
+
| `EnterPlanModeTool` | `EnterPlanMode` | Mode | Enter plan mode |
|
|
460
|
+
| `ExitPlanModeTool` | `ExitPlanMode` | Mode | Exit plan mode |
|
|
415
461
|
|
|
416
462
|
```typescript
|
|
417
463
|
import {
|
|
@@ -490,6 +536,7 @@ const OUTPUT_DIR = path.resolve(import.meta.dirname, 'output')
|
|
|
490
536
|
const tools = new ToolRegistry()
|
|
491
537
|
tools.register(new ReadTool({ cwd: OUTPUT_DIR }))
|
|
492
538
|
tools.register(new WriteTool({ cwd: OUTPUT_DIR }))
|
|
539
|
+
tools.register(new EditTool({ cwd: OUTPUT_DIR }))
|
|
493
540
|
tools.register(new GlobTool({ cwd: OUTPUT_DIR }))
|
|
494
541
|
tools.register(new GrepTool({ cwd: OUTPUT_DIR }))
|
|
495
542
|
tools.register(new BashTool({ cwd: OUTPUT_DIR }))
|
|
@@ -511,6 +558,17 @@ tools.register(
|
|
|
511
558
|
)
|
|
512
559
|
```
|
|
513
560
|
|
|
561
|
+
**How each file tool uses `cwd`:**
|
|
562
|
+
|
|
563
|
+
| Tool | What it does | How `cwd` is applied | Per-call override | Extra sandbox options |
|
|
564
|
+
| --- | --- | --- | --- | --- |
|
|
565
|
+
| `ReadTool` | Reads files (and some converted formats) | Relative `file_path` resolves from `cwd` | `file_path` can be absolute | `allowedDirectory`, `fileBlacklist`, `disableBlacklist` |
|
|
566
|
+
| `WriteTool` | Writes/overwrites files | Relative `file_path` resolves from `cwd` | `file_path` can be absolute | `allowedDirectory`, `readOnlyPaths`, `fileBlacklist`, `disableBlacklist` |
|
|
567
|
+
| `EditTool` | Replaces `old_string` with `new_string` in a file | Relative `file_path` resolves from `cwd` | `file_path` can be absolute | `readOnlyPaths`, `fileBlacklist`, `disableBlacklist` |
|
|
568
|
+
| `GlobTool` | Finds files by pattern | Search root defaults to `cwd` | `path` argument can change search root | `fileBlacklist`, `disableBlacklist` |
|
|
569
|
+
| `GrepTool` | Searches text content in files | Search runs under `cwd` | `path` argument narrows search scope | `fileBlacklist`, `disableBlacklist` |
|
|
570
|
+
| `BashTool` | Runs shell commands | Commands execute in `cwd` | `workdir` argument overrides per call | `readOnlyPaths` |
|
|
571
|
+
|
|
514
572
|
**Directory Configuration Options:**
|
|
515
573
|
|
|
516
574
|
| Option | Description | Example |
|
|
@@ -587,44 +645,60 @@ const openaiTools = registry.toOpenAIFormat()
|
|
|
587
645
|
|
|
588
646
|
## 🎣 Tool Approval & Hooks
|
|
589
647
|
|
|
590
|
-
Sessions support lifecycle hooks that let you intercept tool calls
|
|
648
|
+
Sessions support lifecycle hooks that let you intercept user input, tool calls, and session/subagent lifecycle events.
|
|
591
649
|
|
|
592
650
|
### Key Concepts
|
|
593
651
|
|
|
594
|
-
GoatChain has
|
|
652
|
+
GoatChain has three relevant mechanisms for tool execution control:
|
|
595
653
|
|
|
596
|
-
1. **preToolUse Hook** - For
|
|
597
|
-
-
|
|
598
|
-
-
|
|
654
|
+
1. **preToolUse Hook** - For tool-call mutation before permission/approval
|
|
655
|
+
- Can modify tool name/arguments with `modifiedToolCall`
|
|
656
|
+
- Runs before `permissionRequest`
|
|
657
|
+
|
|
658
|
+
2. **permissionRequest Hook** - For programmatic auto-approval/blocking
|
|
659
|
+
- Runs after `preToolUse`, so it sees the modified tool call
|
|
660
|
+
- `allow: true` → skips approval flow (execution still goes through normal middleware/disabled checks)
|
|
661
|
+
- `allow: false` → tool is blocked
|
|
599
662
|
- Use this for automated scenarios where you want to programmatically approve/deny tools
|
|
600
663
|
|
|
601
|
-
|
|
664
|
+
3. **Approval System** (via `toolContext.approval`) - For interactive user approval
|
|
602
665
|
- Pauses execution on high-risk tools
|
|
603
666
|
- Shows `requires_action` event
|
|
604
667
|
- Resumes with user's approval decisions
|
|
605
668
|
- Use this for interactive UIs where users manually approve tools
|
|
606
669
|
|
|
607
|
-
**These are independent**: If
|
|
670
|
+
**These are independent**: If `permissionRequest` returns `allow: true`, the approval system is bypassed.
|
|
608
671
|
|
|
609
672
|
### Hook Types
|
|
610
673
|
|
|
611
674
|
```typescript
|
|
612
|
-
interface
|
|
613
|
-
//
|
|
614
|
-
|
|
615
|
-
|
|
675
|
+
interface AgentHooks {
|
|
676
|
+
// Session lifecycle
|
|
677
|
+
sessionStart?: (ctx: SessionStartContext) => Promise<void>
|
|
678
|
+
sessionEnd?: (ctx: SessionEndContext) => Promise<void>
|
|
679
|
+
stop?: (ctx: StopContext) => Promise<void>
|
|
680
|
+
userPromptSubmit?: (ctx: UserPromptSubmitContext) => Promise<UserPromptSubmitResult>
|
|
681
|
+
|
|
682
|
+
// Tool lifecycle
|
|
616
683
|
// - Can modify tool call with modifiedToolCall
|
|
617
|
-
preToolUse?: (ctx:
|
|
684
|
+
preToolUse?: (ctx: ToolHookContext) => Promise<PreToolUseResult | void>
|
|
685
|
+
permissionRequest?: (ctx: ToolHookContext) => Promise<PermissionRequestResult>
|
|
686
|
+
postToolUse?: (ctx: ToolHookContext, result: unknown) => Promise<void>
|
|
687
|
+
postToolUseFailure?: (ctx: ToolHookContext, error: Error) => Promise<void>
|
|
688
|
+
|
|
689
|
+
// Subagent lifecycle (used by parallel task/subagent middleware)
|
|
690
|
+
subagentStart?: (ctx: SubagentStartContext) => Promise<void>
|
|
691
|
+
subagentStop?: (ctx: SubagentStopContext) => Promise<void>
|
|
692
|
+
}
|
|
618
693
|
|
|
619
|
-
|
|
620
|
-
|
|
694
|
+
// Backward-compatible alias
|
|
695
|
+
type ToolHooks = AgentHooks
|
|
621
696
|
|
|
622
|
-
|
|
623
|
-
|
|
697
|
+
interface BaseHookContext {
|
|
698
|
+
sessionId: string
|
|
624
699
|
}
|
|
625
700
|
|
|
626
|
-
interface
|
|
627
|
-
sessionId: string
|
|
701
|
+
interface ToolHookContext extends BaseHookContext {
|
|
628
702
|
toolCall: {
|
|
629
703
|
id: string
|
|
630
704
|
type: 'function'
|
|
@@ -636,15 +710,94 @@ interface HookContext {
|
|
|
636
710
|
toolContext: ToolExecutionContext
|
|
637
711
|
}
|
|
638
712
|
|
|
639
|
-
|
|
640
|
-
|
|
713
|
+
// `HookContext` is kept as a backward-compatible alias of ToolHookContext
|
|
714
|
+
type HookContext = ToolHookContext
|
|
715
|
+
|
|
716
|
+
interface PermissionRequestResult {
|
|
717
|
+
// If true, passes permission checks and skips approval flow
|
|
718
|
+
// (tool still goes through normal middleware/disabled checks)
|
|
641
719
|
// If false, blocks tool execution immediately
|
|
642
720
|
allow: boolean
|
|
643
721
|
// Optional: Modify the tool call before execution
|
|
644
722
|
modifiedToolCall?: ToolCall
|
|
645
723
|
}
|
|
724
|
+
|
|
725
|
+
interface PreToolUseResult {
|
|
726
|
+
// Optional: Modify the tool call before permission/approval/execution
|
|
727
|
+
modifiedToolCall?: ToolCall
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
interface UserPromptSubmitResult {
|
|
731
|
+
allow: boolean
|
|
732
|
+
modifiedInput?: MessageContent
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
interface SessionStartContext extends BaseHookContext {
|
|
736
|
+
startReason: 'new' | 'resume'
|
|
737
|
+
messages: Message[]
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
interface StopContext extends BaseHookContext {
|
|
741
|
+
stopReason:
|
|
742
|
+
| 'max_iterations'
|
|
743
|
+
| 'final_response'
|
|
744
|
+
| 'error'
|
|
745
|
+
| 'cancelled'
|
|
746
|
+
| 'approval_required'
|
|
747
|
+
| 'max_follow_ups'
|
|
748
|
+
// Stop reason reported by the latest model response, when available
|
|
749
|
+
// (e.g. 'tool_call' | 'final' | 'length' | 'error' | 'cancelled')
|
|
750
|
+
modelStopReason?: 'tool_call' | 'final' | 'length' | 'error' | 'cancelled'
|
|
751
|
+
finalResponse?: string
|
|
752
|
+
usage: Usage
|
|
753
|
+
error?: { code?: string; message: string }
|
|
754
|
+
messages: Message[]
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
interface SessionEndContext extends BaseHookContext {
|
|
758
|
+
stopReason: StopContext['stopReason']
|
|
759
|
+
finalResponse?: string
|
|
760
|
+
usage: Usage
|
|
761
|
+
durationMs: number
|
|
762
|
+
error?: { code?: string; message: string }
|
|
763
|
+
messages: Message[]
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
interface UserPromptSubmitContext extends BaseHookContext {
|
|
767
|
+
input: MessageContent
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
interface SubagentStartContext extends BaseHookContext {
|
|
771
|
+
subagentId: string
|
|
772
|
+
subagentType: string
|
|
773
|
+
taskDescription?: string
|
|
774
|
+
prompt: string
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
interface SubagentStopContext extends BaseHookContext {
|
|
778
|
+
subagentId: string
|
|
779
|
+
subagentType: string
|
|
780
|
+
result?: unknown
|
|
781
|
+
error?: Error
|
|
782
|
+
durationMs: number
|
|
783
|
+
usage?: Usage
|
|
784
|
+
messages: Message[]
|
|
785
|
+
}
|
|
646
786
|
```
|
|
647
787
|
|
|
788
|
+
### Hook Execution Order
|
|
789
|
+
|
|
790
|
+
Typical order in one run:
|
|
791
|
+
|
|
792
|
+
1. `sessionStart` (once, on the first `receive()` for this session)
|
|
793
|
+
2. `userPromptSubmit` (before a user message enters the loop; can block or rewrite input)
|
|
794
|
+
3. `preToolUse` (runs first for each tool call; can rewrite tool call)
|
|
795
|
+
4. `permissionRequest` (runs after `preToolUse`; allow/block decision before approval check)
|
|
796
|
+
5. Approval flow (`toolContext.approval`) if still required
|
|
797
|
+
6. `postToolUse` or `postToolUseFailure`
|
|
798
|
+
7. `stop` (after each LLM response completes; for no-LLM termination points, it runs before `done`)
|
|
799
|
+
8. `sessionEnd` (when a run fully finishes; not emitted at `approval_required` pause)
|
|
800
|
+
|
|
648
801
|
### Basic Hook Usage
|
|
649
802
|
|
|
650
803
|
```typescript
|
|
@@ -661,10 +814,17 @@ const agent = new Agent({
|
|
|
661
814
|
const session = await agent.createSession({
|
|
662
815
|
hooks: {
|
|
663
816
|
preToolUse: async (ctx) => {
|
|
817
|
+
// Pre-mutate tool call before permission/approval
|
|
818
|
+
const toolName = ctx.toolCall.function.name
|
|
819
|
+
console.log(`PreToolUse: ${toolName}`)
|
|
820
|
+
},
|
|
821
|
+
|
|
822
|
+
permissionRequest: async (ctx) => {
|
|
664
823
|
const toolName = ctx.toolCall.function.name
|
|
665
824
|
console.log(`Tool requested: ${toolName}`)
|
|
666
825
|
|
|
667
|
-
// Returning { allow: true } skips approval flow
|
|
826
|
+
// Returning { allow: true } skips approval flow
|
|
827
|
+
// (tool still goes through normal middleware/disabled checks before execution)
|
|
668
828
|
// Returning { allow: false } blocks tool execution
|
|
669
829
|
return { allow: true }
|
|
670
830
|
},
|
|
@@ -680,9 +840,9 @@ const session = await agent.createSession({
|
|
|
680
840
|
})
|
|
681
841
|
```
|
|
682
842
|
|
|
683
|
-
### Auto-Approval with
|
|
843
|
+
### Auto-Approval with permissionRequest Hook
|
|
684
844
|
|
|
685
|
-
The `
|
|
845
|
+
The `permissionRequest` hook is powerful because `allow: true` **bypasses the approval flow entirely**, even for high-risk tools. This is useful for automated scenarios:
|
|
686
846
|
|
|
687
847
|
```typescript
|
|
688
848
|
import type { RiskLevel } from 'goatchain'
|
|
@@ -706,7 +866,7 @@ function shouldAutoApprove(toolName: string, riskLevel: RiskLevel): boolean {
|
|
|
706
866
|
|
|
707
867
|
const session = await agent.createSession({
|
|
708
868
|
hooks: {
|
|
709
|
-
|
|
869
|
+
permissionRequest: async (ctx) => {
|
|
710
870
|
const tool = agent.tools.get(ctx.toolCall.function.name)
|
|
711
871
|
const riskLevel = tool?.riskLevel ?? 'safe'
|
|
712
872
|
const allow = shouldAutoApprove(ctx.toolCall.function.name, riskLevel)
|
|
@@ -769,12 +929,12 @@ session.send('Your task', {
|
|
|
769
929
|
|
|
770
930
|
### Interactive Approval with Pause/Resume
|
|
771
931
|
|
|
772
|
-
**Important**: The `
|
|
932
|
+
**Important**: The `permissionRequest` hook with `allow: true` **skips approval**. For interactive approval flows where you want to pause and ask the user, use `toolContext.approval` instead:
|
|
773
933
|
|
|
774
934
|
```typescript
|
|
775
935
|
import type { AgentLoopCheckpoint } from 'goatchain'
|
|
776
936
|
|
|
777
|
-
// Step 1: Start session WITHOUT
|
|
937
|
+
// Step 1: Start session WITHOUT permissionRequest hook (to use approval system)
|
|
778
938
|
const session = await agent.createSession()
|
|
779
939
|
|
|
780
940
|
let checkpoint: AgentLoopCheckpoint | undefined
|
|
@@ -872,12 +1032,9 @@ const session = await agent.createSession({
|
|
|
872
1032
|
}
|
|
873
1033
|
|
|
874
1034
|
return {
|
|
875
|
-
allow: true,
|
|
876
1035
|
modifiedToolCall,
|
|
877
1036
|
}
|
|
878
1037
|
}
|
|
879
|
-
|
|
880
|
-
return { allow: true }
|
|
881
1038
|
},
|
|
882
1039
|
},
|
|
883
1040
|
})
|
|
@@ -946,7 +1103,7 @@ Adds planning phase before execution:
|
|
|
946
1103
|
```typescript
|
|
947
1104
|
import { createPlanModeMiddleware } from 'goatchain'
|
|
948
1105
|
|
|
949
|
-
// Automatically named '
|
|
1106
|
+
// Automatically named 'plan_mode'
|
|
950
1107
|
agent.use(createPlanModeMiddleware())
|
|
951
1108
|
|
|
952
1109
|
// With custom configuration
|
|
@@ -965,7 +1122,7 @@ Automatically compresses context when token limit is reached using a two-stage s
|
|
|
965
1122
|
```typescript
|
|
966
1123
|
import { createContextCompressionMiddleware } from 'goatchain'
|
|
967
1124
|
|
|
968
|
-
// Automatically named '
|
|
1125
|
+
// Automatically named 'context_compression'
|
|
969
1126
|
agent.use(
|
|
970
1127
|
createContextCompressionMiddleware({
|
|
971
1128
|
maxTokens: 128000,
|
|
@@ -1597,22 +1754,22 @@ new Agent(options: AgentOptions)
|
|
|
1597
1754
|
|
|
1598
1755
|
- `name: string` - Agent name (required)
|
|
1599
1756
|
- `systemPrompt: string` - System instructions (required)
|
|
1600
|
-
- `model: ModelClient
|
|
1757
|
+
- `model: ModelClient` - LLM client (required)
|
|
1601
1758
|
- `tools?: ToolRegistry` - Tool registry (not an array)
|
|
1602
1759
|
- `stateStore?: StateStore` - Persistence layer
|
|
1603
|
-
- `
|
|
1604
|
-
- `
|
|
1605
|
-
- `
|
|
1760
|
+
- `middleware?: Middleware[]` - Middleware list to register at startup
|
|
1761
|
+
- `mcpServers?: MCPServerConfig[]` - MCP server configuration list
|
|
1762
|
+
- `enableLogging?: boolean` - Enable internal logs
|
|
1606
1763
|
|
|
1607
1764
|
#### Methods
|
|
1608
1765
|
|
|
1609
|
-
**`createSession(options?): Promise<
|
|
1766
|
+
**`createSession(options?): Promise<Session>`**
|
|
1610
1767
|
|
|
1611
1768
|
Create a new session.
|
|
1612
1769
|
|
|
1613
1770
|
```typescript
|
|
1614
1771
|
const session = await agent.createSession({
|
|
1615
|
-
|
|
1772
|
+
sessionId: 'custom-id', // Optional custom session ID
|
|
1616
1773
|
maxIterations: 10, // Optional max iterations
|
|
1617
1774
|
requestParams: {
|
|
1618
1775
|
// Optional request parameters
|
|
@@ -1622,7 +1779,7 @@ const session = await agent.createSession({
|
|
|
1622
1779
|
})
|
|
1623
1780
|
```
|
|
1624
1781
|
|
|
1625
|
-
**`resumeSession(sessionId, options?): Promise<
|
|
1782
|
+
**`resumeSession(sessionId, options?): Promise<Session>`**
|
|
1626
1783
|
|
|
1627
1784
|
Resume an existing session from checkpoint.
|
|
1628
1785
|
|
|
@@ -1630,21 +1787,21 @@ Resume an existing session from checkpoint.
|
|
|
1630
1787
|
const session = await agent.resumeSession('session-123')
|
|
1631
1788
|
```
|
|
1632
1789
|
|
|
1633
|
-
**`use(middleware, name?): () => void
|
|
1790
|
+
**`use(middleware, name?): Promise<() => void>`**
|
|
1634
1791
|
|
|
1635
1792
|
Add middleware. Returns unsubscribe function.
|
|
1636
1793
|
|
|
1637
1794
|
```typescript
|
|
1638
|
-
const unsubscribe = agent.use(myMiddleware, '
|
|
1795
|
+
const unsubscribe = await agent.use(myMiddleware, 'my_middleware')
|
|
1639
1796
|
unsubscribe() // Remove middleware
|
|
1640
1797
|
```
|
|
1641
1798
|
|
|
1642
|
-
**`removeMiddleware(
|
|
1799
|
+
**`removeMiddleware(nameOrFn): boolean`**
|
|
1643
1800
|
|
|
1644
|
-
Remove middleware by name.
|
|
1801
|
+
Remove middleware by name or function reference.
|
|
1645
1802
|
|
|
1646
1803
|
```typescript
|
|
1647
|
-
agent.removeMiddleware('
|
|
1804
|
+
agent.removeMiddleware('my_middleware')
|
|
1648
1805
|
```
|
|
1649
1806
|
|
|
1650
1807
|
**`setModel(modelOrRef): void`**
|
|
@@ -1652,11 +1809,7 @@ agent.removeMiddleware('my-middleware')
|
|
|
1652
1809
|
Switch or pin model at runtime.
|
|
1653
1810
|
|
|
1654
1811
|
```typescript
|
|
1655
|
-
|
|
1656
|
-
agent.setModel('gpt-4o-mini')
|
|
1657
|
-
|
|
1658
|
-
// Use dynamic reference
|
|
1659
|
-
agent.setModel({ type: 'ref', ref: 'the-model' })
|
|
1812
|
+
agent.setModel({ provider: 'openai', modelId: 'gpt-4o-mini' })
|
|
1660
1813
|
```
|
|
1661
1814
|
|
|
1662
1815
|
#### Properties
|
|
@@ -1665,11 +1818,10 @@ agent.setModel({ type: 'ref', ref: 'the-model' })
|
|
|
1665
1818
|
- `name: string` - Agent name
|
|
1666
1819
|
- `systemPrompt: string` - System prompt
|
|
1667
1820
|
- `model: ModelClient` - Current model client
|
|
1668
|
-
- `tools
|
|
1821
|
+
- `tools?: ToolRegistry` - Tool registry
|
|
1669
1822
|
- `stateStore?: StateStore` - State store
|
|
1670
|
-
- `sessionManager
|
|
1823
|
+
- `sessionManager?: BaseSessionManager` - Session manager
|
|
1671
1824
|
- `middlewareNames: string[]` - List of middleware names
|
|
1672
|
-
- `stats: AgentStats` - Usage statistics
|
|
1673
1825
|
|
|
1674
1826
|
### Session Class
|
|
1675
1827
|
|
|
@@ -1802,15 +1954,20 @@ interface ToolCallStartEvent {
|
|
|
1802
1954
|
|
|
1803
1955
|
interface ToolResultEvent {
|
|
1804
1956
|
type: 'tool_result'
|
|
1805
|
-
|
|
1806
|
-
name: string
|
|
1957
|
+
tool_call_id: string
|
|
1807
1958
|
result: unknown
|
|
1808
|
-
|
|
1959
|
+
isError?: boolean
|
|
1809
1960
|
}
|
|
1810
1961
|
|
|
1811
1962
|
interface DoneEvent {
|
|
1812
1963
|
type: 'done'
|
|
1813
|
-
stopReason:
|
|
1964
|
+
stopReason:
|
|
1965
|
+
| 'max_iterations'
|
|
1966
|
+
| 'final_response'
|
|
1967
|
+
| 'error'
|
|
1968
|
+
| 'cancelled'
|
|
1969
|
+
| 'approval_required'
|
|
1970
|
+
| 'max_follow_ups'
|
|
1814
1971
|
usage?: Usage
|
|
1815
1972
|
}
|
|
1816
1973
|
```
|
|
@@ -1826,12 +1983,12 @@ classDiagram
|
|
|
1826
1983
|
+name: string
|
|
1827
1984
|
+systemPrompt: string
|
|
1828
1985
|
+model: ModelClient
|
|
1829
|
-
+tools: ToolRegistry
|
|
1830
|
-
+stateStore: StateStore
|
|
1831
|
-
+sessionManager: BaseSessionManager
|
|
1832
|
-
+use(middleware):
|
|
1833
|
-
+createSession(): Promise~
|
|
1834
|
-
+resumeSession(id): Promise~
|
|
1986
|
+
+tools: ToolRegistry?
|
|
1987
|
+
+stateStore: StateStore?
|
|
1988
|
+
+sessionManager: BaseSessionManager?
|
|
1989
|
+
+use(middleware): Promise~function~
|
|
1990
|
+
+createSession(): Promise~Session~
|
|
1991
|
+
+resumeSession(id): Promise~Session~
|
|
1835
1992
|
}
|
|
1836
1993
|
|
|
1837
1994
|
class ModelClient {
|
package/dist/agent/agent.d.ts
CHANGED
|
@@ -115,17 +115,17 @@ export declare class Agent {
|
|
|
115
115
|
* }, 'logging');
|
|
116
116
|
*
|
|
117
117
|
* // Built-in middleware with default name
|
|
118
|
-
* await agent.use(createContextCompressionMiddleware({ ... })); // auto-named '
|
|
118
|
+
* await agent.use(createContextCompressionMiddleware({ ... })); // auto-named 'context_compression'
|
|
119
119
|
*
|
|
120
120
|
* // Override default name
|
|
121
|
-
* await agent.use(createPlanModeMiddleware(), '
|
|
121
|
+
* await agent.use(createPlanModeMiddleware(), 'my_plan_mode');
|
|
122
122
|
*
|
|
123
123
|
* // Middleware with tools (auto-registers tools with namespace)
|
|
124
124
|
* const middleware: Middleware = async (state, next) => next(state);
|
|
125
|
-
* middleware.__middlewareName = '
|
|
125
|
+
* middleware.__middlewareName = 'plan_mode';
|
|
126
126
|
* middleware.__createTools = async () => [new EnterPlanModeTool()];
|
|
127
127
|
* await agent.use(middleware);
|
|
128
|
-
* // Tool registered as: '
|
|
128
|
+
* // Tool registered as: 'plan_mode_EnterPlanMode'
|
|
129
129
|
*
|
|
130
130
|
* // Anonymous middleware (auto-named)
|
|
131
131
|
* const unsubscribe = await agent.use(async (state, next) => {
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export { HookManager } from './manager';
|
|
2
|
-
export type { HookContext, PostToolUseFailureHook, PostToolUseHook, PreToolUseHook, PreToolUseResult, ToolHooks, } from './types';
|
|
2
|
+
export type { AgentHooks, BaseHookContext, HookContext, PermissionRequestHook, PermissionRequestResult, PostToolUseFailureHook, PostToolUseHook, PreToolUseHook, PreToolUseResult, SessionEndContext, SessionEndHook, SessionStartContext, SessionStartHook, StopContext, StopHook, SubagentStartContext, SubagentStartHook, SubagentStopContext, SubagentStopHook, ToolHookContext, ToolHooks, UserPromptSubmitContext, UserPromptSubmitHook, UserPromptSubmitResult, } from './types';
|
|
@@ -1,8 +1,15 @@
|
|
|
1
|
-
import type { HookContext, PreToolUseResult,
|
|
1
|
+
import type { AgentHooks, HookContext, PermissionRequestResult, PreToolUseResult, SessionEndContext, SessionStartContext, SubagentStartContext, StopContext, SubagentStopContext, UserPromptSubmitContext, UserPromptSubmitResult } from './types';
|
|
2
2
|
export declare class HookManager {
|
|
3
3
|
private readonly hooks;
|
|
4
|
-
constructor(hooks?:
|
|
5
|
-
|
|
4
|
+
constructor(hooks?: AgentHooks);
|
|
5
|
+
executePermissionRequest(context: HookContext): Promise<PermissionRequestResult>;
|
|
6
|
+
executePreToolUse(context: HookContext): Promise<PreToolUseResult | void>;
|
|
7
|
+
executeUserPromptSubmit(context: UserPromptSubmitContext): Promise<UserPromptSubmitResult>;
|
|
8
|
+
executeSessionStart(context: SessionStartContext): Promise<void>;
|
|
9
|
+
executeSessionEnd(context: SessionEndContext): Promise<void>;
|
|
6
10
|
executePostToolUse(context: HookContext, result: unknown): Promise<void>;
|
|
7
11
|
executePostToolUseFailure(context: HookContext, error: Error): Promise<void>;
|
|
12
|
+
executeStop(context: StopContext): Promise<void>;
|
|
13
|
+
executeSubagentStart(context: SubagentStartContext): Promise<void>;
|
|
14
|
+
executeSubagentStop(context: SubagentStopContext): Promise<void>;
|
|
8
15
|
}
|