@codeany/open-agent-sdk 0.2.2 → 0.2.4
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/dist/agent.d.ts +113 -0
- package/dist/agent.d.ts.map +1 -0
- package/dist/agent.js +453 -0
- package/dist/agent.js.map +1 -0
- package/dist/engine.d.ts +62 -0
- package/dist/engine.d.ts.map +1 -0
- package/dist/engine.js +496 -0
- package/dist/engine.js.map +1 -0
- package/dist/hooks.d.ts +111 -0
- package/dist/hooks.d.ts.map +1 -0
- package/dist/hooks.js +179 -0
- package/dist/hooks.js.map +1 -0
- package/dist/index.d.ts +66 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +133 -0
- package/dist/index.js.map +1 -0
- package/dist/mcp/client.d.ts +19 -0
- package/dist/mcp/client.d.ts.map +1 -0
- package/dist/mcp/client.js +126 -0
- package/dist/mcp/client.js.map +1 -0
- package/dist/providers/anthropic.d.ts +17 -0
- package/dist/providers/anthropic.d.ts.map +1 -0
- package/dist/providers/anthropic.js +47 -0
- package/dist/providers/anthropic.js.map +1 -0
- package/dist/providers/index.d.ts +20 -0
- package/dist/providers/index.d.ts.map +1 -0
- package/dist/providers/index.js +26 -0
- package/dist/providers/index.js.map +1 -0
- package/dist/providers/openai.d.ts +26 -0
- package/dist/providers/openai.d.ts.map +1 -0
- package/dist/providers/openai.js +212 -0
- package/dist/providers/openai.js.map +1 -0
- package/dist/providers/types.d.ts +84 -0
- package/dist/providers/types.d.ts.map +1 -0
- package/dist/providers/types.js +11 -0
- package/dist/providers/types.js.map +1 -0
- package/dist/sdk-mcp-server.d.ts +52 -0
- package/dist/sdk-mcp-server.d.ts.map +1 -0
- package/dist/sdk-mcp-server.js +57 -0
- package/dist/sdk-mcp-server.js.map +1 -0
- package/dist/session.d.ts +73 -0
- package/dist/session.d.ts.map +1 -0
- package/dist/session.js +159 -0
- package/dist/session.js.map +1 -0
- package/dist/skills/bundled/commit.d.ts +7 -0
- package/dist/skills/bundled/commit.d.ts.map +1 -0
- package/dist/skills/bundled/commit.js +35 -0
- package/dist/skills/bundled/commit.js.map +1 -0
- package/dist/skills/bundled/debug.d.ts +7 -0
- package/dist/skills/bundled/debug.d.ts.map +1 -0
- package/{src/skills/bundled/debug.ts → dist/skills/bundled/debug.js} +20 -22
- package/dist/skills/bundled/debug.js.map +1 -0
- package/dist/skills/bundled/index.d.ts +11 -0
- package/dist/skills/bundled/index.d.ts.map +1 -0
- package/dist/skills/bundled/index.js +26 -0
- package/dist/skills/bundled/index.js.map +1 -0
- package/dist/skills/bundled/review.d.ts +7 -0
- package/dist/skills/bundled/review.d.ts.map +1 -0
- package/{src/skills/bundled/review.ts → dist/skills/bundled/review.js} +18 -21
- package/dist/skills/bundled/review.js.map +1 -0
- package/dist/skills/bundled/simplify.d.ts +8 -0
- package/dist/skills/bundled/simplify.d.ts.map +1 -0
- package/{src/skills/bundled/simplify.ts → dist/skills/bundled/simplify.js} +16 -19
- package/dist/skills/bundled/simplify.js.map +1 -0
- package/dist/skills/bundled/test.d.ts +7 -0
- package/dist/skills/bundled/test.d.ts.map +1 -0
- package/{src/skills/bundled/test.ts → dist/skills/bundled/test.js} +18 -21
- package/dist/skills/bundled/test.js.map +1 -0
- package/dist/skills/index.d.ts +7 -0
- package/dist/skills/index.d.ts.map +1 -0
- package/dist/skills/index.js +8 -0
- package/dist/skills/index.js.map +1 -0
- package/dist/skills/registry.d.ts +43 -0
- package/dist/skills/registry.d.ts.map +1 -0
- package/dist/skills/registry.js +111 -0
- package/dist/skills/registry.js.map +1 -0
- package/dist/skills/types.d.ts +83 -0
- package/dist/skills/types.d.ts.map +1 -0
- package/dist/skills/types.js +8 -0
- package/dist/skills/types.js.map +1 -0
- package/dist/tool-helper.d.ts +73 -0
- package/dist/tool-helper.d.ts.map +1 -0
- package/dist/tool-helper.js +86 -0
- package/dist/tool-helper.js.map +1 -0
- package/dist/tools/agent-tool.d.ts +17 -0
- package/dist/tools/agent-tool.d.ts.map +1 -0
- package/dist/tools/agent-tool.js +146 -0
- package/dist/tools/agent-tool.js.map +1 -0
- package/dist/tools/ask-user.d.ts +18 -0
- package/dist/tools/ask-user.d.ts.map +1 -0
- package/dist/tools/ask-user.js +72 -0
- package/dist/tools/ask-user.js.map +1 -0
- package/dist/tools/bash.d.ts +5 -0
- package/dist/tools/bash.d.ts.map +1 -0
- package/dist/tools/bash.js +67 -0
- package/dist/tools/bash.js.map +1 -0
- package/dist/tools/config-tool.d.ts +20 -0
- package/dist/tools/config-tool.d.ts.map +1 -0
- package/dist/tools/config-tool.js +83 -0
- package/dist/tools/config-tool.js.map +1 -0
- package/dist/tools/cron-tools.d.ts +33 -0
- package/dist/tools/cron-tools.d.ts.map +1 -0
- package/dist/tools/cron-tools.js +128 -0
- package/dist/tools/cron-tools.js.map +1 -0
- package/dist/tools/edit.d.ts +5 -0
- package/dist/tools/edit.d.ts.map +1 -0
- package/dist/tools/edit.js +70 -0
- package/dist/tools/edit.js.map +1 -0
- package/dist/tools/glob.d.ts +5 -0
- package/dist/tools/glob.d.ts.map +1 -0
- package/dist/tools/glob.js +75 -0
- package/dist/tools/glob.js.map +1 -0
- package/dist/tools/grep.d.ts +5 -0
- package/dist/tools/grep.d.ts.map +1 -0
- package/dist/tools/grep.js +168 -0
- package/dist/tools/grep.js.map +1 -0
- package/dist/tools/index.d.ts +45 -0
- package/dist/tools/index.d.ts.map +1 -0
- package/dist/tools/index.js +162 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/tools/lsp-tool.d.ts +9 -0
- package/dist/tools/lsp-tool.d.ts.map +1 -0
- package/dist/tools/lsp-tool.js +137 -0
- package/dist/tools/lsp-tool.js.map +1 -0
- package/dist/tools/mcp-resource-tools.d.ts +14 -0
- package/dist/tools/mcp-resource-tools.d.ts.map +1 -0
- package/dist/tools/mcp-resource-tools.js +117 -0
- package/dist/tools/mcp-resource-tools.js.map +1 -0
- package/dist/tools/notebook-edit.d.ts +5 -0
- package/dist/tools/notebook-edit.d.ts.map +1 -0
- package/dist/tools/notebook-edit.js +85 -0
- package/dist/tools/notebook-edit.js.map +1 -0
- package/dist/tools/plan-tools.d.ts +12 -0
- package/dist/tools/plan-tools.d.ts.map +1 -0
- package/dist/tools/plan-tools.js +77 -0
- package/dist/tools/plan-tools.js.map +1 -0
- package/dist/tools/read.d.ts +5 -0
- package/dist/tools/read.d.ts.map +1 -0
- package/dist/tools/read.js +66 -0
- package/dist/tools/read.js.map +1 -0
- package/dist/tools/send-message.d.ts +31 -0
- package/dist/tools/send-message.d.ts.map +1 -0
- package/dist/tools/send-message.js +77 -0
- package/dist/tools/send-message.js.map +1 -0
- package/dist/tools/skill-tool.d.ts +9 -0
- package/dist/tools/skill-tool.d.ts.map +1 -0
- package/dist/tools/skill-tool.js +115 -0
- package/dist/tools/skill-tool.js.map +1 -0
- package/dist/tools/task-tools.d.ts +48 -0
- package/dist/tools/task-tools.d.ts.map +1 -0
- package/dist/tools/task-tools.js +242 -0
- package/dist/tools/task-tools.js.map +1 -0
- package/dist/tools/team-tools.d.ts +34 -0
- package/dist/tools/team-tools.d.ts.map +1 -0
- package/dist/tools/team-tools.js +103 -0
- package/dist/tools/team-tools.js.map +1 -0
- package/dist/tools/todo-tool.d.ts +22 -0
- package/dist/tools/todo-tool.d.ts.map +1 -0
- package/dist/tools/todo-tool.js +93 -0
- package/dist/tools/todo-tool.js.map +1 -0
- package/dist/tools/tool-search.d.ts +13 -0
- package/dist/tools/tool-search.d.ts.map +1 -0
- package/dist/tools/tool-search.js +76 -0
- package/dist/tools/tool-search.js.map +1 -0
- package/dist/tools/types.d.ts +29 -0
- package/dist/tools/types.d.ts.map +1 -0
- package/dist/tools/types.js +52 -0
- package/dist/tools/types.js.map +1 -0
- package/dist/tools/web-fetch.d.ts +5 -0
- package/dist/tools/web-fetch.d.ts.map +1 -0
- package/dist/tools/web-fetch.js +60 -0
- package/dist/tools/web-fetch.js.map +1 -0
- package/dist/tools/web-search.d.ts +5 -0
- package/dist/tools/web-search.d.ts.map +1 -0
- package/dist/tools/web-search.js +77 -0
- package/dist/tools/web-search.js.map +1 -0
- package/dist/tools/worktree-tools.d.ts +10 -0
- package/dist/tools/worktree-tools.d.ts.map +1 -0
- package/dist/tools/worktree-tools.js +130 -0
- package/dist/tools/worktree-tools.js.map +1 -0
- package/dist/tools/write.d.ts +5 -0
- package/dist/tools/write.d.ts.map +1 -0
- package/dist/tools/write.js +40 -0
- package/dist/tools/write.js.map +1 -0
- package/dist/types.d.ts +432 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +5 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/compact.d.ts +44 -0
- package/dist/utils/compact.d.ts.map +1 -0
- package/dist/utils/compact.js +162 -0
- package/dist/utils/compact.js.map +1 -0
- package/dist/utils/context.d.ts +35 -0
- package/dist/utils/context.d.ts.map +1 -0
- package/dist/utils/context.js +174 -0
- package/dist/utils/context.js.map +1 -0
- package/dist/utils/fileCache.d.ts +64 -0
- package/dist/utils/fileCache.d.ts.map +1 -0
- package/dist/utils/fileCache.js +117 -0
- package/dist/utils/fileCache.js.map +1 -0
- package/dist/utils/messages.d.ts +57 -0
- package/dist/utils/messages.d.ts.map +1 -0
- package/dist/utils/messages.js +152 -0
- package/dist/utils/messages.js.map +1 -0
- package/dist/utils/retry.d.ts +48 -0
- package/dist/utils/retry.d.ts.map +1 -0
- package/dist/utils/retry.js +111 -0
- package/dist/utils/retry.js.map +1 -0
- package/dist/utils/tokens.d.ts +57 -0
- package/dist/utils/tokens.d.ts.map +1 -0
- package/dist/utils/tokens.js +134 -0
- package/dist/utils/tokens.js.map +1 -0
- package/package.json +6 -1
- package/.env.example +0 -8
- package/examples/01-simple-query.ts +0 -43
- package/examples/02-multi-tool.ts +0 -44
- package/examples/03-multi-turn.ts +0 -39
- package/examples/04-prompt-api.ts +0 -29
- package/examples/05-custom-system-prompt.ts +0 -26
- package/examples/06-mcp-server.ts +0 -49
- package/examples/07-custom-tools.ts +0 -87
- package/examples/08-official-api-compat.ts +0 -38
- package/examples/09-subagents.ts +0 -48
- package/examples/10-permissions.ts +0 -40
- package/examples/11-custom-mcp-tools.ts +0 -101
- package/examples/12-skills.ts +0 -88
- package/examples/13-hooks.ts +0 -88
- package/examples/14-openai-compat.ts +0 -71
- package/examples/web/index.html +0 -365
- package/examples/web/server.ts +0 -157
- package/src/agent.ts +0 -516
- package/src/engine.ts +0 -633
- package/src/hooks.ts +0 -261
- package/src/index.ts +0 -426
- package/src/mcp/client.ts +0 -150
- package/src/providers/anthropic.ts +0 -60
- package/src/providers/index.ts +0 -34
- package/src/providers/openai.ts +0 -315
- package/src/providers/types.ts +0 -85
- package/src/sdk-mcp-server.ts +0 -78
- package/src/session.ts +0 -227
- package/src/skills/bundled/commit.ts +0 -38
- package/src/skills/bundled/index.ts +0 -28
- package/src/skills/index.ts +0 -25
- package/src/skills/registry.ts +0 -133
- package/src/skills/types.ts +0 -99
- package/src/tool-helper.ts +0 -127
- package/src/tools/agent-tool.ts +0 -164
- package/src/tools/ask-user.ts +0 -79
- package/src/tools/bash.ts +0 -75
- package/src/tools/config-tool.ts +0 -89
- package/src/tools/cron-tools.ts +0 -153
- package/src/tools/edit.ts +0 -74
- package/src/tools/glob.ts +0 -77
- package/src/tools/grep.ts +0 -168
- package/src/tools/index.ts +0 -240
- package/src/tools/lsp-tool.ts +0 -163
- package/src/tools/mcp-resource-tools.ts +0 -125
- package/src/tools/notebook-edit.ts +0 -93
- package/src/tools/plan-tools.ts +0 -88
- package/src/tools/read.ts +0 -73
- package/src/tools/send-message.ts +0 -96
- package/src/tools/skill-tool.ts +0 -133
- package/src/tools/task-tools.ts +0 -290
- package/src/tools/team-tools.ts +0 -128
- package/src/tools/todo-tool.ts +0 -112
- package/src/tools/tool-search.ts +0 -87
- package/src/tools/types.ts +0 -66
- package/src/tools/web-fetch.ts +0 -66
- package/src/tools/web-search.ts +0 -86
- package/src/tools/worktree-tools.ts +0 -140
- package/src/tools/write.ts +0 -42
- package/src/types.ts +0 -486
- package/src/utils/compact.ts +0 -207
- package/src/utils/context.ts +0 -191
- package/src/utils/fileCache.ts +0 -148
- package/src/utils/messages.ts +0 -195
- package/src/utils/retry.ts +0 -140
- package/src/utils/tokens.ts +0 -145
- package/tsconfig.json +0 -19
package/src/hooks.ts
DELETED
|
@@ -1,261 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Hook System
|
|
3
|
-
*
|
|
4
|
-
* Lifecycle hooks for intercepting agent behavior.
|
|
5
|
-
* Supports pre/post tool use, session lifecycle, and custom events.
|
|
6
|
-
*
|
|
7
|
-
* Hook events:
|
|
8
|
-
* - PreToolUse: before tool execution
|
|
9
|
-
* - PostToolUse: after tool execution
|
|
10
|
-
* - PostToolUseFailure: after tool failure
|
|
11
|
-
* - SessionStart: session initialization
|
|
12
|
-
* - SessionEnd: session cleanup
|
|
13
|
-
* - Stop: when turn completes
|
|
14
|
-
* - SubagentStart: subagent spawned
|
|
15
|
-
* - SubagentStop: subagent completed
|
|
16
|
-
* - UserPromptSubmit: user sends message
|
|
17
|
-
* - PermissionRequest: permission check triggered
|
|
18
|
-
* - TaskCreated: task created
|
|
19
|
-
* - TaskCompleted: task finished
|
|
20
|
-
* - ConfigChange: settings changed
|
|
21
|
-
* - CwdChanged: working directory changed
|
|
22
|
-
* - FileChanged: file modified
|
|
23
|
-
* - Notification: system notification
|
|
24
|
-
*/
|
|
25
|
-
|
|
26
|
-
import { spawn, type ChildProcess } from 'child_process'
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* All supported hook events.
|
|
30
|
-
*/
|
|
31
|
-
export const HOOK_EVENTS = [
|
|
32
|
-
'PreToolUse',
|
|
33
|
-
'PostToolUse',
|
|
34
|
-
'PostToolUseFailure',
|
|
35
|
-
'SessionStart',
|
|
36
|
-
'SessionEnd',
|
|
37
|
-
'Stop',
|
|
38
|
-
'SubagentStart',
|
|
39
|
-
'SubagentStop',
|
|
40
|
-
'UserPromptSubmit',
|
|
41
|
-
'PermissionRequest',
|
|
42
|
-
'PermissionDenied',
|
|
43
|
-
'TaskCreated',
|
|
44
|
-
'TaskCompleted',
|
|
45
|
-
'ConfigChange',
|
|
46
|
-
'CwdChanged',
|
|
47
|
-
'FileChanged',
|
|
48
|
-
'Notification',
|
|
49
|
-
'PreCompact',
|
|
50
|
-
'PostCompact',
|
|
51
|
-
'TeammateIdle',
|
|
52
|
-
] as const
|
|
53
|
-
|
|
54
|
-
export type HookEvent = typeof HOOK_EVENTS[number]
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Hook definition.
|
|
58
|
-
*/
|
|
59
|
-
export interface HookDefinition {
|
|
60
|
-
/** Shell command or function to execute */
|
|
61
|
-
command?: string
|
|
62
|
-
/** Function handler */
|
|
63
|
-
handler?: (input: HookInput) => Promise<HookOutput | void>
|
|
64
|
-
/** Tool name matcher (regex pattern) */
|
|
65
|
-
matcher?: string
|
|
66
|
-
/** Timeout in milliseconds */
|
|
67
|
-
timeout?: number
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Hook input passed to handlers.
|
|
72
|
-
*/
|
|
73
|
-
export interface HookInput {
|
|
74
|
-
event: HookEvent
|
|
75
|
-
toolName?: string
|
|
76
|
-
toolInput?: unknown
|
|
77
|
-
toolOutput?: unknown
|
|
78
|
-
toolUseId?: string
|
|
79
|
-
sessionId?: string
|
|
80
|
-
cwd?: string
|
|
81
|
-
error?: string
|
|
82
|
-
[key: string]: unknown
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Hook output returned by handlers.
|
|
87
|
-
*/
|
|
88
|
-
export interface HookOutput {
|
|
89
|
-
/** Message to append to conversation */
|
|
90
|
-
message?: string
|
|
91
|
-
/** Permission update */
|
|
92
|
-
permissionUpdate?: {
|
|
93
|
-
tool: string
|
|
94
|
-
behavior: 'allow' | 'deny'
|
|
95
|
-
}
|
|
96
|
-
/** Whether to block the action */
|
|
97
|
-
block?: boolean
|
|
98
|
-
/** Notification */
|
|
99
|
-
notification?: {
|
|
100
|
-
title: string
|
|
101
|
-
body: string
|
|
102
|
-
level?: 'info' | 'warning' | 'error'
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Hook configuration (from settings).
|
|
108
|
-
*/
|
|
109
|
-
export type HookConfig = Record<string, HookDefinition[]>
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* Hook registry for managing and executing hooks.
|
|
113
|
-
*/
|
|
114
|
-
export class HookRegistry {
|
|
115
|
-
private hooks: Map<HookEvent, HookDefinition[]> = new Map()
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* Register hooks from configuration.
|
|
119
|
-
*/
|
|
120
|
-
registerFromConfig(config: HookConfig): void {
|
|
121
|
-
for (const [event, definitions] of Object.entries(config)) {
|
|
122
|
-
const hookEvent = event as HookEvent
|
|
123
|
-
if (!HOOK_EVENTS.includes(hookEvent)) continue
|
|
124
|
-
|
|
125
|
-
const existing = this.hooks.get(hookEvent) || []
|
|
126
|
-
this.hooks.set(hookEvent, [...existing, ...definitions])
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Register a single hook.
|
|
132
|
-
*/
|
|
133
|
-
register(event: HookEvent, definition: HookDefinition): void {
|
|
134
|
-
const existing = this.hooks.get(event) || []
|
|
135
|
-
existing.push(definition)
|
|
136
|
-
this.hooks.set(event, existing)
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* Execute hooks for an event.
|
|
141
|
-
*/
|
|
142
|
-
async execute(
|
|
143
|
-
event: HookEvent,
|
|
144
|
-
input: HookInput,
|
|
145
|
-
): Promise<HookOutput[]> {
|
|
146
|
-
const definitions = this.hooks.get(event) || []
|
|
147
|
-
const results: HookOutput[] = []
|
|
148
|
-
|
|
149
|
-
for (const def of definitions) {
|
|
150
|
-
// Check matcher for tool-specific hooks
|
|
151
|
-
if (def.matcher && input.toolName) {
|
|
152
|
-
const regex = new RegExp(def.matcher)
|
|
153
|
-
if (!regex.test(input.toolName)) continue
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
try {
|
|
157
|
-
let output: HookOutput | void = undefined
|
|
158
|
-
|
|
159
|
-
if (def.handler) {
|
|
160
|
-
// Function handler
|
|
161
|
-
output = await Promise.race([
|
|
162
|
-
def.handler(input),
|
|
163
|
-
new Promise<void>((_, reject) =>
|
|
164
|
-
setTimeout(() => reject(new Error('Hook timeout')), def.timeout || 30000),
|
|
165
|
-
),
|
|
166
|
-
])
|
|
167
|
-
} else if (def.command) {
|
|
168
|
-
// Shell command handler
|
|
169
|
-
output = await executeShellHook(def.command, input, def.timeout || 30000)
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
if (output) {
|
|
173
|
-
results.push(output)
|
|
174
|
-
}
|
|
175
|
-
} catch (err: any) {
|
|
176
|
-
// Log but don't fail on hook errors
|
|
177
|
-
console.error(`[Hook] ${event} hook failed: ${err.message}`)
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
return results
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
/**
|
|
185
|
-
* Check if any hooks are registered for an event.
|
|
186
|
-
*/
|
|
187
|
-
hasHooks(event: HookEvent): boolean {
|
|
188
|
-
return (this.hooks.get(event)?.length || 0) > 0
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
/**
|
|
192
|
-
* Clear all hooks.
|
|
193
|
-
*/
|
|
194
|
-
clear(): void {
|
|
195
|
-
this.hooks.clear()
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
/**
|
|
200
|
-
* Execute a shell command as a hook.
|
|
201
|
-
*/
|
|
202
|
-
async function executeShellHook(
|
|
203
|
-
command: string,
|
|
204
|
-
input: HookInput,
|
|
205
|
-
timeout: number,
|
|
206
|
-
): Promise<HookOutput | void> {
|
|
207
|
-
return new Promise((resolve) => {
|
|
208
|
-
const proc = spawn('bash', ['-c', command], {
|
|
209
|
-
timeout,
|
|
210
|
-
env: {
|
|
211
|
-
...process.env,
|
|
212
|
-
HOOK_EVENT: input.event,
|
|
213
|
-
HOOK_TOOL_NAME: input.toolName || '',
|
|
214
|
-
HOOK_SESSION_ID: input.sessionId || '',
|
|
215
|
-
HOOK_CWD: input.cwd || '',
|
|
216
|
-
},
|
|
217
|
-
stdio: ['pipe', 'pipe', 'pipe'],
|
|
218
|
-
})
|
|
219
|
-
|
|
220
|
-
// Send input as JSON on stdin
|
|
221
|
-
proc.stdin?.write(JSON.stringify(input))
|
|
222
|
-
proc.stdin?.end()
|
|
223
|
-
|
|
224
|
-
const chunks: Buffer[] = []
|
|
225
|
-
proc.stdout?.on('data', (d: Buffer) => chunks.push(d))
|
|
226
|
-
|
|
227
|
-
proc.on('close', (code) => {
|
|
228
|
-
if (code !== 0) {
|
|
229
|
-
resolve(undefined)
|
|
230
|
-
return
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
const stdout = Buffer.concat(chunks).toString('utf-8').trim()
|
|
234
|
-
if (!stdout) {
|
|
235
|
-
resolve(undefined)
|
|
236
|
-
return
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
try {
|
|
240
|
-
const output = JSON.parse(stdout) as HookOutput
|
|
241
|
-
resolve(output)
|
|
242
|
-
} catch {
|
|
243
|
-
// Non-JSON output treated as message
|
|
244
|
-
resolve({ message: stdout })
|
|
245
|
-
}
|
|
246
|
-
})
|
|
247
|
-
|
|
248
|
-
proc.on('error', () => resolve(undefined))
|
|
249
|
-
})
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
/**
|
|
253
|
-
* Create a default hook registry.
|
|
254
|
-
*/
|
|
255
|
-
export function createHookRegistry(config?: HookConfig): HookRegistry {
|
|
256
|
-
const registry = new HookRegistry()
|
|
257
|
-
if (config) {
|
|
258
|
-
registry.registerFromConfig(config)
|
|
259
|
-
}
|
|
260
|
-
return registry
|
|
261
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,426 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @codeany/open-agent-sdk
|
|
3
|
-
*
|
|
4
|
-
* Open-source Agent SDK by CodeAny (https://codeany.ai).
|
|
5
|
-
* Runs the full agent loop in-process without spawning subprocesses.
|
|
6
|
-
*
|
|
7
|
-
* Features:
|
|
8
|
-
* - 30+ built-in tools (file I/O, shell, web, agents, tasks, teams, etc.)
|
|
9
|
-
* - Skill system (reusable prompt templates with bundled skills)
|
|
10
|
-
* - MCP server integration (stdio, SSE, HTTP)
|
|
11
|
-
* - Context compression (auto-compact, micro-compact)
|
|
12
|
-
* - Retry with exponential backoff
|
|
13
|
-
* - Git status & project context injection
|
|
14
|
-
* - Multi-turn session persistence
|
|
15
|
-
* - Permission system (allow/deny/bypass modes)
|
|
16
|
-
* - Subagent spawning & team coordination
|
|
17
|
-
* - Task management & scheduling
|
|
18
|
-
* - Hook system with lifecycle integration (pre/post tool use, session, compact)
|
|
19
|
-
* - Token estimation & cost tracking
|
|
20
|
-
* - File state LRU caching
|
|
21
|
-
* - Plan mode for structured workflows
|
|
22
|
-
*/
|
|
23
|
-
|
|
24
|
-
// --------------------------------------------------------------------------
|
|
25
|
-
// High-level Agent API
|
|
26
|
-
// --------------------------------------------------------------------------
|
|
27
|
-
|
|
28
|
-
export { Agent, createAgent, query } from './agent.js'
|
|
29
|
-
|
|
30
|
-
// --------------------------------------------------------------------------
|
|
31
|
-
// Tool Helper (Zod-based tool creation, compatible with official SDK)
|
|
32
|
-
// --------------------------------------------------------------------------
|
|
33
|
-
|
|
34
|
-
export { tool, sdkToolToToolDefinition } from './tool-helper.js'
|
|
35
|
-
export type {
|
|
36
|
-
ToolAnnotations,
|
|
37
|
-
CallToolResult,
|
|
38
|
-
SdkMcpToolDefinition,
|
|
39
|
-
} from './tool-helper.js'
|
|
40
|
-
|
|
41
|
-
// --------------------------------------------------------------------------
|
|
42
|
-
// In-Process MCP Server
|
|
43
|
-
// --------------------------------------------------------------------------
|
|
44
|
-
|
|
45
|
-
export { createSdkMcpServer, isSdkServerConfig } from './sdk-mcp-server.js'
|
|
46
|
-
export type { McpSdkServerConfig } from './sdk-mcp-server.js'
|
|
47
|
-
|
|
48
|
-
// --------------------------------------------------------------------------
|
|
49
|
-
// Core Engine
|
|
50
|
-
// --------------------------------------------------------------------------
|
|
51
|
-
|
|
52
|
-
export { QueryEngine } from './engine.js'
|
|
53
|
-
|
|
54
|
-
// --------------------------------------------------------------------------
|
|
55
|
-
// LLM Providers (Anthropic + OpenAI)
|
|
56
|
-
// --------------------------------------------------------------------------
|
|
57
|
-
|
|
58
|
-
export {
|
|
59
|
-
createProvider,
|
|
60
|
-
AnthropicProvider,
|
|
61
|
-
OpenAIProvider,
|
|
62
|
-
} from './providers/index.js'
|
|
63
|
-
export type {
|
|
64
|
-
ApiType,
|
|
65
|
-
LLMProvider,
|
|
66
|
-
CreateMessageParams,
|
|
67
|
-
CreateMessageResponse,
|
|
68
|
-
NormalizedMessageParam,
|
|
69
|
-
NormalizedContentBlock,
|
|
70
|
-
NormalizedTool,
|
|
71
|
-
NormalizedResponseBlock,
|
|
72
|
-
} from './providers/index.js'
|
|
73
|
-
|
|
74
|
-
// --------------------------------------------------------------------------
|
|
75
|
-
// Tool System (30+ tools)
|
|
76
|
-
// --------------------------------------------------------------------------
|
|
77
|
-
|
|
78
|
-
export {
|
|
79
|
-
// Registry
|
|
80
|
-
getAllBaseTools,
|
|
81
|
-
filterTools,
|
|
82
|
-
assembleToolPool,
|
|
83
|
-
|
|
84
|
-
// Helpers
|
|
85
|
-
defineTool,
|
|
86
|
-
toApiTool,
|
|
87
|
-
|
|
88
|
-
// Core file I/O & execution
|
|
89
|
-
BashTool,
|
|
90
|
-
FileReadTool,
|
|
91
|
-
FileWriteTool,
|
|
92
|
-
FileEditTool,
|
|
93
|
-
GlobTool,
|
|
94
|
-
GrepTool,
|
|
95
|
-
NotebookEditTool,
|
|
96
|
-
|
|
97
|
-
// Web
|
|
98
|
-
WebFetchTool,
|
|
99
|
-
WebSearchTool,
|
|
100
|
-
|
|
101
|
-
// Agent & Multi-agent
|
|
102
|
-
AgentTool,
|
|
103
|
-
SendMessageTool,
|
|
104
|
-
TeamCreateTool,
|
|
105
|
-
TeamDeleteTool,
|
|
106
|
-
|
|
107
|
-
// Tasks
|
|
108
|
-
TaskCreateTool,
|
|
109
|
-
TaskListTool,
|
|
110
|
-
TaskUpdateTool,
|
|
111
|
-
TaskGetTool,
|
|
112
|
-
TaskStopTool,
|
|
113
|
-
TaskOutputTool,
|
|
114
|
-
|
|
115
|
-
// Worktree
|
|
116
|
-
EnterWorktreeTool,
|
|
117
|
-
ExitWorktreeTool,
|
|
118
|
-
|
|
119
|
-
// Planning
|
|
120
|
-
EnterPlanModeTool,
|
|
121
|
-
ExitPlanModeTool,
|
|
122
|
-
|
|
123
|
-
// User interaction
|
|
124
|
-
AskUserQuestionTool,
|
|
125
|
-
|
|
126
|
-
// Discovery
|
|
127
|
-
ToolSearchTool,
|
|
128
|
-
|
|
129
|
-
// MCP Resources
|
|
130
|
-
ListMcpResourcesTool,
|
|
131
|
-
ReadMcpResourceTool,
|
|
132
|
-
|
|
133
|
-
// Scheduling
|
|
134
|
-
CronCreateTool,
|
|
135
|
-
CronDeleteTool,
|
|
136
|
-
CronListTool,
|
|
137
|
-
RemoteTriggerTool,
|
|
138
|
-
|
|
139
|
-
// LSP
|
|
140
|
-
LSPTool,
|
|
141
|
-
|
|
142
|
-
// Config
|
|
143
|
-
ConfigTool,
|
|
144
|
-
|
|
145
|
-
// Todo
|
|
146
|
-
TodoWriteTool,
|
|
147
|
-
|
|
148
|
-
// Skill
|
|
149
|
-
SkillTool,
|
|
150
|
-
} from './tools/index.js'
|
|
151
|
-
|
|
152
|
-
// --------------------------------------------------------------------------
|
|
153
|
-
// MCP Client
|
|
154
|
-
// --------------------------------------------------------------------------
|
|
155
|
-
|
|
156
|
-
export { connectMCPServer, closeAllConnections } from './mcp/client.js'
|
|
157
|
-
export type { MCPConnection } from './mcp/client.js'
|
|
158
|
-
|
|
159
|
-
// --------------------------------------------------------------------------
|
|
160
|
-
// Skill System
|
|
161
|
-
// --------------------------------------------------------------------------
|
|
162
|
-
|
|
163
|
-
export {
|
|
164
|
-
registerSkill,
|
|
165
|
-
getSkill,
|
|
166
|
-
getAllSkills,
|
|
167
|
-
getUserInvocableSkills,
|
|
168
|
-
hasSkill,
|
|
169
|
-
unregisterSkill,
|
|
170
|
-
clearSkills,
|
|
171
|
-
formatSkillsForPrompt,
|
|
172
|
-
initBundledSkills,
|
|
173
|
-
} from './skills/index.js'
|
|
174
|
-
export type {
|
|
175
|
-
SkillDefinition,
|
|
176
|
-
SkillContentBlock,
|
|
177
|
-
SkillResult,
|
|
178
|
-
} from './skills/index.js'
|
|
179
|
-
|
|
180
|
-
// --------------------------------------------------------------------------
|
|
181
|
-
// Hook System
|
|
182
|
-
// --------------------------------------------------------------------------
|
|
183
|
-
|
|
184
|
-
export {
|
|
185
|
-
HookRegistry,
|
|
186
|
-
createHookRegistry,
|
|
187
|
-
HOOK_EVENTS,
|
|
188
|
-
} from './hooks.js'
|
|
189
|
-
export type {
|
|
190
|
-
HookEvent,
|
|
191
|
-
HookDefinition,
|
|
192
|
-
HookInput,
|
|
193
|
-
HookOutput,
|
|
194
|
-
HookConfig,
|
|
195
|
-
} from './hooks.js'
|
|
196
|
-
|
|
197
|
-
// --------------------------------------------------------------------------
|
|
198
|
-
// Session Management
|
|
199
|
-
// --------------------------------------------------------------------------
|
|
200
|
-
|
|
201
|
-
export {
|
|
202
|
-
saveSession,
|
|
203
|
-
loadSession,
|
|
204
|
-
listSessions,
|
|
205
|
-
forkSession,
|
|
206
|
-
getSessionMessages,
|
|
207
|
-
getSessionInfo,
|
|
208
|
-
renameSession,
|
|
209
|
-
tagSession,
|
|
210
|
-
appendToSession,
|
|
211
|
-
deleteSession,
|
|
212
|
-
} from './session.js'
|
|
213
|
-
export type { SessionMetadata, SessionData } from './session.js'
|
|
214
|
-
|
|
215
|
-
// --------------------------------------------------------------------------
|
|
216
|
-
// Context Utilities
|
|
217
|
-
// --------------------------------------------------------------------------
|
|
218
|
-
|
|
219
|
-
export {
|
|
220
|
-
getSystemContext,
|
|
221
|
-
getUserContext,
|
|
222
|
-
getGitStatus,
|
|
223
|
-
readProjectContextContent,
|
|
224
|
-
discoverProjectContextFiles,
|
|
225
|
-
clearContextCache,
|
|
226
|
-
} from './utils/context.js'
|
|
227
|
-
|
|
228
|
-
// --------------------------------------------------------------------------
|
|
229
|
-
// Message Utilities
|
|
230
|
-
// --------------------------------------------------------------------------
|
|
231
|
-
|
|
232
|
-
export {
|
|
233
|
-
createUserMessage,
|
|
234
|
-
createAssistantMessage,
|
|
235
|
-
normalizeMessagesForAPI,
|
|
236
|
-
stripImagesFromMessages,
|
|
237
|
-
extractTextFromContent,
|
|
238
|
-
createCompactBoundaryMessage,
|
|
239
|
-
truncateText,
|
|
240
|
-
} from './utils/messages.js'
|
|
241
|
-
|
|
242
|
-
// --------------------------------------------------------------------------
|
|
243
|
-
// Token Estimation & Cost
|
|
244
|
-
// --------------------------------------------------------------------------
|
|
245
|
-
|
|
246
|
-
export {
|
|
247
|
-
estimateTokens,
|
|
248
|
-
estimateMessagesTokens,
|
|
249
|
-
estimateSystemPromptTokens,
|
|
250
|
-
getTokenCountFromUsage,
|
|
251
|
-
getContextWindowSize,
|
|
252
|
-
getAutoCompactThreshold,
|
|
253
|
-
estimateCost,
|
|
254
|
-
MODEL_PRICING,
|
|
255
|
-
AUTOCOMPACT_BUFFER_TOKENS,
|
|
256
|
-
} from './utils/tokens.js'
|
|
257
|
-
|
|
258
|
-
// --------------------------------------------------------------------------
|
|
259
|
-
// Context Compression
|
|
260
|
-
// --------------------------------------------------------------------------
|
|
261
|
-
|
|
262
|
-
export {
|
|
263
|
-
shouldAutoCompact,
|
|
264
|
-
compactConversation,
|
|
265
|
-
microCompactMessages,
|
|
266
|
-
createAutoCompactState,
|
|
267
|
-
} from './utils/compact.js'
|
|
268
|
-
export type { AutoCompactState } from './utils/compact.js'
|
|
269
|
-
|
|
270
|
-
// --------------------------------------------------------------------------
|
|
271
|
-
// Retry Logic
|
|
272
|
-
// --------------------------------------------------------------------------
|
|
273
|
-
|
|
274
|
-
export {
|
|
275
|
-
withRetry,
|
|
276
|
-
isRetryableError,
|
|
277
|
-
isPromptTooLongError,
|
|
278
|
-
isAuthError,
|
|
279
|
-
isRateLimitError,
|
|
280
|
-
formatApiError,
|
|
281
|
-
getRetryDelay,
|
|
282
|
-
DEFAULT_RETRY_CONFIG,
|
|
283
|
-
} from './utils/retry.js'
|
|
284
|
-
export type { RetryConfig } from './utils/retry.js'
|
|
285
|
-
|
|
286
|
-
// --------------------------------------------------------------------------
|
|
287
|
-
// File State Cache
|
|
288
|
-
// --------------------------------------------------------------------------
|
|
289
|
-
|
|
290
|
-
export {
|
|
291
|
-
FileStateCache,
|
|
292
|
-
createFileStateCache,
|
|
293
|
-
} from './utils/fileCache.js'
|
|
294
|
-
export type { FileState } from './utils/fileCache.js'
|
|
295
|
-
|
|
296
|
-
// --------------------------------------------------------------------------
|
|
297
|
-
// Task & Team State (for advanced usage)
|
|
298
|
-
// --------------------------------------------------------------------------
|
|
299
|
-
|
|
300
|
-
export {
|
|
301
|
-
getAllTasks,
|
|
302
|
-
getTask,
|
|
303
|
-
clearTasks,
|
|
304
|
-
} from './tools/task-tools.js'
|
|
305
|
-
export type { Task, TaskStatus } from './tools/task-tools.js'
|
|
306
|
-
|
|
307
|
-
export {
|
|
308
|
-
getAllTeams,
|
|
309
|
-
getTeam,
|
|
310
|
-
clearTeams,
|
|
311
|
-
} from './tools/team-tools.js'
|
|
312
|
-
export type { Team } from './tools/team-tools.js'
|
|
313
|
-
|
|
314
|
-
export {
|
|
315
|
-
readMailbox,
|
|
316
|
-
writeToMailbox,
|
|
317
|
-
clearMailboxes,
|
|
318
|
-
} from './tools/send-message.js'
|
|
319
|
-
export type { AgentMessage } from './tools/send-message.js'
|
|
320
|
-
|
|
321
|
-
export {
|
|
322
|
-
isPlanModeActive,
|
|
323
|
-
getCurrentPlan,
|
|
324
|
-
} from './tools/plan-tools.js'
|
|
325
|
-
|
|
326
|
-
export {
|
|
327
|
-
registerAgents,
|
|
328
|
-
clearAgents,
|
|
329
|
-
} from './tools/agent-tool.js'
|
|
330
|
-
|
|
331
|
-
export {
|
|
332
|
-
setQuestionHandler,
|
|
333
|
-
clearQuestionHandler,
|
|
334
|
-
} from './tools/ask-user.js'
|
|
335
|
-
|
|
336
|
-
export {
|
|
337
|
-
setDeferredTools,
|
|
338
|
-
} from './tools/tool-search.js'
|
|
339
|
-
|
|
340
|
-
export {
|
|
341
|
-
setMcpConnections,
|
|
342
|
-
} from './tools/mcp-resource-tools.js'
|
|
343
|
-
|
|
344
|
-
export {
|
|
345
|
-
getAllCronJobs,
|
|
346
|
-
clearCronJobs,
|
|
347
|
-
} from './tools/cron-tools.js'
|
|
348
|
-
export type { CronJob } from './tools/cron-tools.js'
|
|
349
|
-
|
|
350
|
-
export {
|
|
351
|
-
getConfig,
|
|
352
|
-
setConfig,
|
|
353
|
-
clearConfig,
|
|
354
|
-
} from './tools/config-tool.js'
|
|
355
|
-
|
|
356
|
-
export {
|
|
357
|
-
getTodos,
|
|
358
|
-
clearTodos,
|
|
359
|
-
} from './tools/todo-tool.js'
|
|
360
|
-
export type { TodoItem } from './tools/todo-tool.js'
|
|
361
|
-
|
|
362
|
-
// --------------------------------------------------------------------------
|
|
363
|
-
// Types
|
|
364
|
-
// --------------------------------------------------------------------------
|
|
365
|
-
|
|
366
|
-
export type {
|
|
367
|
-
// Message types
|
|
368
|
-
Message,
|
|
369
|
-
UserMessage,
|
|
370
|
-
AssistantMessage,
|
|
371
|
-
ConversationMessage,
|
|
372
|
-
MessageRole,
|
|
373
|
-
|
|
374
|
-
// SDK message types (streaming events)
|
|
375
|
-
SDKMessage,
|
|
376
|
-
SDKAssistantMessage,
|
|
377
|
-
SDKToolResultMessage,
|
|
378
|
-
SDKResultMessage,
|
|
379
|
-
SDKPartialMessage,
|
|
380
|
-
|
|
381
|
-
// Tool types
|
|
382
|
-
ToolDefinition,
|
|
383
|
-
ToolInputSchema,
|
|
384
|
-
ToolContext,
|
|
385
|
-
ToolResult,
|
|
386
|
-
|
|
387
|
-
// Permission types
|
|
388
|
-
PermissionMode,
|
|
389
|
-
PermissionBehavior,
|
|
390
|
-
CanUseToolFn,
|
|
391
|
-
CanUseToolResult,
|
|
392
|
-
|
|
393
|
-
// MCP types
|
|
394
|
-
McpServerConfig,
|
|
395
|
-
McpStdioConfig,
|
|
396
|
-
McpSseConfig,
|
|
397
|
-
McpHttpConfig,
|
|
398
|
-
|
|
399
|
-
// Agent types
|
|
400
|
-
AgentOptions,
|
|
401
|
-
AgentDefinition,
|
|
402
|
-
QueryResult,
|
|
403
|
-
ThinkingConfig,
|
|
404
|
-
TokenUsage,
|
|
405
|
-
|
|
406
|
-
// Engine types
|
|
407
|
-
QueryEngineConfig,
|
|
408
|
-
|
|
409
|
-
// Content block types
|
|
410
|
-
ContentBlockParam,
|
|
411
|
-
ContentBlock,
|
|
412
|
-
|
|
413
|
-
// Sandbox types
|
|
414
|
-
SandboxSettings,
|
|
415
|
-
SandboxNetworkConfig,
|
|
416
|
-
SandboxFilesystemConfig,
|
|
417
|
-
|
|
418
|
-
// Output format
|
|
419
|
-
OutputFormat,
|
|
420
|
-
|
|
421
|
-
// Setting sources
|
|
422
|
-
SettingSource,
|
|
423
|
-
|
|
424
|
-
// Model info
|
|
425
|
-
ModelInfo,
|
|
426
|
-
} from './types.js'
|