@codeany/open-agent-sdk 0.2.2 → 0.2.3
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 +446 -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 +423 -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/types.ts
DELETED
|
@@ -1,486 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Core type definitions for the Agent SDK
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
// Content block types (provider-agnostic, compatible with Anthropic format)
|
|
6
|
-
export type ContentBlockParam =
|
|
7
|
-
| { type: 'text'; text: string }
|
|
8
|
-
| { type: 'image'; source: any }
|
|
9
|
-
| { type: 'tool_use'; id: string; name: string; input: any }
|
|
10
|
-
| { type: 'tool_result'; tool_use_id: string; content: string | any[]; is_error?: boolean }
|
|
11
|
-
|
|
12
|
-
export type ContentBlock =
|
|
13
|
-
| { type: 'text'; text: string }
|
|
14
|
-
| { type: 'tool_use'; id: string; name: string; input: any }
|
|
15
|
-
| { type: 'thinking'; thinking: string }
|
|
16
|
-
|
|
17
|
-
// --------------------------------------------------------------------------
|
|
18
|
-
// Message Types
|
|
19
|
-
// --------------------------------------------------------------------------
|
|
20
|
-
|
|
21
|
-
export type MessageRole = 'user' | 'assistant'
|
|
22
|
-
|
|
23
|
-
export interface ConversationMessage {
|
|
24
|
-
role: MessageRole
|
|
25
|
-
content: string | ContentBlockParam[]
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export interface UserMessage {
|
|
29
|
-
type: 'user'
|
|
30
|
-
message: ConversationMessage
|
|
31
|
-
uuid: string
|
|
32
|
-
timestamp: string
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export interface AssistantMessage {
|
|
36
|
-
type: 'assistant'
|
|
37
|
-
message: {
|
|
38
|
-
role: 'assistant'
|
|
39
|
-
content: ContentBlock[]
|
|
40
|
-
}
|
|
41
|
-
uuid: string
|
|
42
|
-
timestamp: string
|
|
43
|
-
usage?: TokenUsage
|
|
44
|
-
cost?: number
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export type Message = UserMessage | AssistantMessage
|
|
48
|
-
|
|
49
|
-
// --------------------------------------------------------------------------
|
|
50
|
-
// SDK Message Types (streaming events)
|
|
51
|
-
// --------------------------------------------------------------------------
|
|
52
|
-
|
|
53
|
-
export type SDKMessage =
|
|
54
|
-
| SDKAssistantMessage
|
|
55
|
-
| SDKToolResultMessage
|
|
56
|
-
| SDKResultMessage
|
|
57
|
-
| SDKPartialMessage
|
|
58
|
-
| SDKSystemMessage
|
|
59
|
-
| SDKCompactBoundaryMessage
|
|
60
|
-
| SDKStatusMessage
|
|
61
|
-
| SDKTaskNotificationMessage
|
|
62
|
-
| SDKRateLimitEvent
|
|
63
|
-
|
|
64
|
-
export interface SDKAssistantMessage {
|
|
65
|
-
type: 'assistant'
|
|
66
|
-
uuid?: string
|
|
67
|
-
session_id?: string
|
|
68
|
-
message: {
|
|
69
|
-
role: 'assistant'
|
|
70
|
-
content: ContentBlock[]
|
|
71
|
-
}
|
|
72
|
-
parent_tool_use_id?: string | null
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export interface SDKToolResultMessage {
|
|
76
|
-
type: 'tool_result'
|
|
77
|
-
result: {
|
|
78
|
-
tool_use_id: string
|
|
79
|
-
tool_name: string
|
|
80
|
-
output: string
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
export interface SDKResultMessage {
|
|
85
|
-
type: 'result'
|
|
86
|
-
subtype: 'success' | 'error_max_turns' | 'error_during_execution' | 'error_max_budget_usd' | string
|
|
87
|
-
uuid?: string
|
|
88
|
-
session_id?: string
|
|
89
|
-
is_error?: boolean
|
|
90
|
-
num_turns?: number
|
|
91
|
-
result?: string
|
|
92
|
-
stop_reason?: string | null
|
|
93
|
-
total_cost_usd?: number
|
|
94
|
-
duration_ms?: number
|
|
95
|
-
duration_api_ms?: number
|
|
96
|
-
usage?: TokenUsage
|
|
97
|
-
model_usage?: Record<string, { input_tokens: number; output_tokens: number }>
|
|
98
|
-
permission_denials?: Array<{ tool: string; reason: string }>
|
|
99
|
-
structured_output?: unknown
|
|
100
|
-
errors?: string[]
|
|
101
|
-
/** @deprecated Use total_cost_usd */
|
|
102
|
-
cost?: number
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
export interface SDKPartialMessage {
|
|
106
|
-
type: 'partial_message'
|
|
107
|
-
partial: {
|
|
108
|
-
type: 'text' | 'tool_use'
|
|
109
|
-
text?: string
|
|
110
|
-
name?: string
|
|
111
|
-
input?: string
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/** Emitted once at session start with initialization info. */
|
|
116
|
-
export interface SDKSystemMessage {
|
|
117
|
-
type: 'system'
|
|
118
|
-
subtype: 'init'
|
|
119
|
-
uuid?: string
|
|
120
|
-
session_id: string
|
|
121
|
-
tools: string[]
|
|
122
|
-
model: string
|
|
123
|
-
cwd: string
|
|
124
|
-
mcp_servers: Array<{ name: string; status: string }>
|
|
125
|
-
permission_mode: string
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/** Marks a compaction boundary in the conversation. */
|
|
129
|
-
export interface SDKCompactBoundaryMessage {
|
|
130
|
-
type: 'system'
|
|
131
|
-
subtype: 'compact_boundary'
|
|
132
|
-
summary?: string
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/** Status update during long operations. */
|
|
136
|
-
export interface SDKStatusMessage {
|
|
137
|
-
type: 'system'
|
|
138
|
-
subtype: 'status'
|
|
139
|
-
message: string
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
/** Task lifecycle notification. */
|
|
143
|
-
export interface SDKTaskNotificationMessage {
|
|
144
|
-
type: 'system'
|
|
145
|
-
subtype: 'task_notification'
|
|
146
|
-
task_id: string
|
|
147
|
-
status: string
|
|
148
|
-
message?: string
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/** Rate limit event. */
|
|
152
|
-
export interface SDKRateLimitEvent {
|
|
153
|
-
type: 'system'
|
|
154
|
-
subtype: 'rate_limit'
|
|
155
|
-
retry_after_ms?: number
|
|
156
|
-
message: string
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
// --------------------------------------------------------------------------
|
|
160
|
-
// Token Usage
|
|
161
|
-
// --------------------------------------------------------------------------
|
|
162
|
-
|
|
163
|
-
export interface TokenUsage {
|
|
164
|
-
input_tokens: number
|
|
165
|
-
output_tokens: number
|
|
166
|
-
cache_creation_input_tokens?: number
|
|
167
|
-
cache_read_input_tokens?: number
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
// --------------------------------------------------------------------------
|
|
171
|
-
// Tool Types
|
|
172
|
-
// --------------------------------------------------------------------------
|
|
173
|
-
|
|
174
|
-
export interface ToolDefinition {
|
|
175
|
-
name: string
|
|
176
|
-
description: string
|
|
177
|
-
inputSchema: ToolInputSchema
|
|
178
|
-
call: (input: any, context: ToolContext) => Promise<ToolResult>
|
|
179
|
-
isReadOnly?: () => boolean
|
|
180
|
-
isConcurrencySafe?: () => boolean
|
|
181
|
-
isEnabled?: () => boolean
|
|
182
|
-
prompt?: (context: ToolContext) => Promise<string>
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
export interface ToolInputSchema {
|
|
186
|
-
type: 'object'
|
|
187
|
-
properties: Record<string, any>
|
|
188
|
-
required?: string[]
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
export interface ToolContext {
|
|
192
|
-
cwd: string
|
|
193
|
-
abortSignal?: AbortSignal
|
|
194
|
-
/** Parent agent's LLM provider (inherited by subagents) */
|
|
195
|
-
provider?: import('./providers/types.js').LLMProvider
|
|
196
|
-
/** Parent agent's model ID */
|
|
197
|
-
model?: string
|
|
198
|
-
/** Parent agent's API type */
|
|
199
|
-
apiType?: import('./providers/types.js').ApiType
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
export interface ToolResult {
|
|
203
|
-
type: 'tool_result'
|
|
204
|
-
tool_use_id: string
|
|
205
|
-
content: string | any[]
|
|
206
|
-
is_error?: boolean
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
// --------------------------------------------------------------------------
|
|
210
|
-
// Permission Types
|
|
211
|
-
// --------------------------------------------------------------------------
|
|
212
|
-
|
|
213
|
-
export type PermissionMode =
|
|
214
|
-
| 'default'
|
|
215
|
-
| 'acceptEdits'
|
|
216
|
-
| 'bypassPermissions'
|
|
217
|
-
| 'plan'
|
|
218
|
-
| 'dontAsk'
|
|
219
|
-
| 'auto'
|
|
220
|
-
|
|
221
|
-
export type PermissionBehavior = 'allow' | 'deny'
|
|
222
|
-
|
|
223
|
-
export type CanUseToolResult = {
|
|
224
|
-
behavior: PermissionBehavior
|
|
225
|
-
updatedInput?: unknown
|
|
226
|
-
message?: string
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
export type CanUseToolFn = (
|
|
230
|
-
tool: ToolDefinition,
|
|
231
|
-
input: unknown,
|
|
232
|
-
) => Promise<CanUseToolResult>
|
|
233
|
-
|
|
234
|
-
// --------------------------------------------------------------------------
|
|
235
|
-
// MCP Types
|
|
236
|
-
// --------------------------------------------------------------------------
|
|
237
|
-
|
|
238
|
-
export type McpServerConfig =
|
|
239
|
-
| McpStdioConfig
|
|
240
|
-
| McpSseConfig
|
|
241
|
-
| McpHttpConfig
|
|
242
|
-
|
|
243
|
-
export interface McpStdioConfig {
|
|
244
|
-
type?: 'stdio'
|
|
245
|
-
command: string
|
|
246
|
-
args?: string[]
|
|
247
|
-
env?: Record<string, string>
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
export interface McpSseConfig {
|
|
251
|
-
type: 'sse'
|
|
252
|
-
url: string
|
|
253
|
-
headers?: Record<string, string>
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
export interface McpHttpConfig {
|
|
257
|
-
type: 'http'
|
|
258
|
-
url: string
|
|
259
|
-
headers?: Record<string, string>
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
// --------------------------------------------------------------------------
|
|
263
|
-
// Agent Types
|
|
264
|
-
// --------------------------------------------------------------------------
|
|
265
|
-
|
|
266
|
-
export interface AgentDefinition {
|
|
267
|
-
description: string
|
|
268
|
-
prompt: string
|
|
269
|
-
tools?: string[]
|
|
270
|
-
disallowedTools?: string[]
|
|
271
|
-
model?: 'sonnet' | 'opus' | 'haiku' | 'inherit' | string
|
|
272
|
-
mcpServers?: Array<string | { name: string; tools?: string[] }>
|
|
273
|
-
skills?: string[]
|
|
274
|
-
maxTurns?: number
|
|
275
|
-
criticalSystemReminder_EXPERIMENTAL?: string
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
export interface ThinkingConfig {
|
|
279
|
-
type: 'adaptive' | 'enabled' | 'disabled'
|
|
280
|
-
budgetTokens?: number
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
// --------------------------------------------------------------------------
|
|
284
|
-
// Sandbox Types
|
|
285
|
-
// --------------------------------------------------------------------------
|
|
286
|
-
|
|
287
|
-
export interface SandboxSettings {
|
|
288
|
-
enabled?: boolean
|
|
289
|
-
autoAllowBashIfSandboxed?: boolean
|
|
290
|
-
excludedCommands?: string[]
|
|
291
|
-
allowUnsandboxedCommands?: boolean
|
|
292
|
-
network?: SandboxNetworkConfig
|
|
293
|
-
filesystem?: SandboxFilesystemConfig
|
|
294
|
-
ignoreViolations?: Record<string, string[]>
|
|
295
|
-
enableWeakerNestedSandbox?: boolean
|
|
296
|
-
ripgrep?: { command: string; args?: string[] }
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
export interface SandboxNetworkConfig {
|
|
300
|
-
allowedDomains?: string[]
|
|
301
|
-
allowManagedDomainsOnly?: boolean
|
|
302
|
-
allowLocalBinding?: boolean
|
|
303
|
-
allowUnixSockets?: string[]
|
|
304
|
-
allowAllUnixSockets?: boolean
|
|
305
|
-
httpProxyPort?: number
|
|
306
|
-
socksProxyPort?: number
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
export interface SandboxFilesystemConfig {
|
|
310
|
-
allowWrite?: string[]
|
|
311
|
-
denyWrite?: string[]
|
|
312
|
-
denyRead?: string[]
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
// --------------------------------------------------------------------------
|
|
316
|
-
// Output Format
|
|
317
|
-
// --------------------------------------------------------------------------
|
|
318
|
-
|
|
319
|
-
export interface OutputFormat {
|
|
320
|
-
type: 'json_schema'
|
|
321
|
-
schema: Record<string, unknown>
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
// --------------------------------------------------------------------------
|
|
325
|
-
// Setting Sources
|
|
326
|
-
// --------------------------------------------------------------------------
|
|
327
|
-
|
|
328
|
-
export type SettingSource = 'user' | 'project' | 'local'
|
|
329
|
-
|
|
330
|
-
// --------------------------------------------------------------------------
|
|
331
|
-
// Model Info
|
|
332
|
-
// --------------------------------------------------------------------------
|
|
333
|
-
|
|
334
|
-
export interface ModelInfo {
|
|
335
|
-
value: string
|
|
336
|
-
displayName: string
|
|
337
|
-
description: string
|
|
338
|
-
supportsEffort?: boolean
|
|
339
|
-
supportedEffortLevels?: ('low' | 'medium' | 'high' | 'max')[]
|
|
340
|
-
supportsAdaptiveThinking?: boolean
|
|
341
|
-
supportsFastMode?: boolean
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
export interface AgentOptions {
|
|
345
|
-
/** LLM model ID */
|
|
346
|
-
model?: string
|
|
347
|
-
/**
|
|
348
|
-
* API type: 'anthropic-messages' or 'openai-completions'.
|
|
349
|
-
* Falls back to CODEANY_API_TYPE env var. Default: 'anthropic-messages'.
|
|
350
|
-
*/
|
|
351
|
-
apiType?: import('./providers/types.js').ApiType
|
|
352
|
-
/** API key. Falls back to CODEANY_API_KEY env var. */
|
|
353
|
-
apiKey?: string
|
|
354
|
-
/** API base URL override */
|
|
355
|
-
baseURL?: string
|
|
356
|
-
/** Working directory for file/shell tools */
|
|
357
|
-
cwd?: string
|
|
358
|
-
/** System prompt override or preset */
|
|
359
|
-
systemPrompt?: string | { type: 'preset'; preset: 'default'; append?: string }
|
|
360
|
-
/** Append to default system prompt */
|
|
361
|
-
appendSystemPrompt?: string
|
|
362
|
-
/** Available tools (ToolDefinition[] or string[] preset) */
|
|
363
|
-
tools?: ToolDefinition[] | string[] | { type: 'preset'; preset: 'default' }
|
|
364
|
-
/** Maximum number of agentic turns per query */
|
|
365
|
-
maxTurns?: number
|
|
366
|
-
/** Maximum USD budget per query */
|
|
367
|
-
maxBudgetUsd?: number
|
|
368
|
-
/** Extended thinking configuration */
|
|
369
|
-
thinking?: ThinkingConfig
|
|
370
|
-
/** Maximum thinking tokens (deprecated, use thinking.budgetTokens) */
|
|
371
|
-
maxThinkingTokens?: number
|
|
372
|
-
/** Structured output JSON schema */
|
|
373
|
-
jsonSchema?: Record<string, unknown>
|
|
374
|
-
/** Structured output format */
|
|
375
|
-
outputFormat?: OutputFormat
|
|
376
|
-
/** Permission handler callback */
|
|
377
|
-
canUseTool?: CanUseToolFn
|
|
378
|
-
/** Permission mode controlling tool approval behavior */
|
|
379
|
-
permissionMode?: PermissionMode
|
|
380
|
-
/** Abort controller for cancellation */
|
|
381
|
-
abortController?: AbortController
|
|
382
|
-
/** Abort signal for cancellation */
|
|
383
|
-
abortSignal?: AbortSignal
|
|
384
|
-
/** Whether to include partial streaming events */
|
|
385
|
-
includePartialMessages?: boolean
|
|
386
|
-
/** Environment variables */
|
|
387
|
-
env?: Record<string, string | undefined>
|
|
388
|
-
/** Tool names to pre-approve without prompting */
|
|
389
|
-
allowedTools?: string[]
|
|
390
|
-
/** Tool names to deny */
|
|
391
|
-
disallowedTools?: string[]
|
|
392
|
-
/** MCP server configurations */
|
|
393
|
-
mcpServers?: Record<string, McpServerConfig | any> // supports McpSdkServerConfig
|
|
394
|
-
/** Custom subagent definitions */
|
|
395
|
-
agents?: Record<string, AgentDefinition>
|
|
396
|
-
/** Maximum tokens for responses */
|
|
397
|
-
maxTokens?: number
|
|
398
|
-
/** Effort level for reasoning */
|
|
399
|
-
effort?: 'low' | 'medium' | 'high' | 'max'
|
|
400
|
-
/** Fallback model if primary is unavailable */
|
|
401
|
-
fallbackModel?: string
|
|
402
|
-
/** Continue the most recent session in cwd */
|
|
403
|
-
continue?: boolean
|
|
404
|
-
/** Resume a specific session by ID */
|
|
405
|
-
resume?: string
|
|
406
|
-
/** Fork a session instead of continuing it */
|
|
407
|
-
forkSession?: boolean
|
|
408
|
-
/** Persist session to disk */
|
|
409
|
-
persistSession?: boolean
|
|
410
|
-
/** Explicit session ID */
|
|
411
|
-
sessionId?: string
|
|
412
|
-
/** Enable file checkpointing (for rewindFiles) */
|
|
413
|
-
enableFileCheckpointing?: boolean
|
|
414
|
-
/** Sandbox configuration */
|
|
415
|
-
sandbox?: SandboxSettings
|
|
416
|
-
/** Load settings from filesystem */
|
|
417
|
-
settingSources?: SettingSource[]
|
|
418
|
-
/** Plugin configurations */
|
|
419
|
-
plugins?: Array<{ name: string; config?: Record<string, unknown> }>
|
|
420
|
-
/** Additional working directories */
|
|
421
|
-
additionalDirectories?: string[]
|
|
422
|
-
/** Default agent to use */
|
|
423
|
-
agent?: string
|
|
424
|
-
/** Debug mode */
|
|
425
|
-
debug?: boolean
|
|
426
|
-
/** Debug log file */
|
|
427
|
-
debugFile?: string
|
|
428
|
-
/** Tool-specific configuration */
|
|
429
|
-
toolConfig?: Record<string, unknown>
|
|
430
|
-
/** Enable prompt suggestions */
|
|
431
|
-
promptSuggestions?: boolean
|
|
432
|
-
/** Strict MCP config validation */
|
|
433
|
-
strictMcpConfig?: boolean
|
|
434
|
-
/** Extra CLI arguments */
|
|
435
|
-
extraArgs?: Record<string, string | null>
|
|
436
|
-
/** SDK betas to enable */
|
|
437
|
-
betas?: string[]
|
|
438
|
-
/** Permission prompt tool name override */
|
|
439
|
-
permissionPromptToolName?: string
|
|
440
|
-
/** Hook configurations */
|
|
441
|
-
hooks?: Record<string, Array<{
|
|
442
|
-
matcher?: string
|
|
443
|
-
hooks: Array<(input: any, toolUseId: string, context: { signal: AbortSignal }) => Promise<any>>
|
|
444
|
-
timeout?: number
|
|
445
|
-
}>>
|
|
446
|
-
}
|
|
447
|
-
|
|
448
|
-
export interface QueryResult {
|
|
449
|
-
/** Final text output from the assistant */
|
|
450
|
-
text: string
|
|
451
|
-
/** Token usage */
|
|
452
|
-
usage: TokenUsage
|
|
453
|
-
/** Number of agentic turns */
|
|
454
|
-
num_turns: number
|
|
455
|
-
/** Duration in milliseconds */
|
|
456
|
-
duration_ms: number
|
|
457
|
-
/** All conversation messages */
|
|
458
|
-
messages: Message[]
|
|
459
|
-
}
|
|
460
|
-
|
|
461
|
-
// --------------------------------------------------------------------------
|
|
462
|
-
// Query Engine Types
|
|
463
|
-
// --------------------------------------------------------------------------
|
|
464
|
-
|
|
465
|
-
export interface QueryEngineConfig {
|
|
466
|
-
cwd: string
|
|
467
|
-
model: string
|
|
468
|
-
/** LLM provider instance (created from apiType) */
|
|
469
|
-
provider: import('./providers/types.js').LLMProvider
|
|
470
|
-
tools: ToolDefinition[]
|
|
471
|
-
systemPrompt?: string
|
|
472
|
-
appendSystemPrompt?: string
|
|
473
|
-
maxTurns: number
|
|
474
|
-
maxBudgetUsd?: number
|
|
475
|
-
maxTokens: number
|
|
476
|
-
thinking?: ThinkingConfig
|
|
477
|
-
jsonSchema?: Record<string, unknown>
|
|
478
|
-
canUseTool: CanUseToolFn
|
|
479
|
-
includePartialMessages: boolean
|
|
480
|
-
abortSignal?: AbortSignal
|
|
481
|
-
agents?: Record<string, AgentDefinition>
|
|
482
|
-
/** Hook registry for lifecycle events */
|
|
483
|
-
hookRegistry?: import('./hooks.js').HookRegistry
|
|
484
|
-
/** Session ID for hook context */
|
|
485
|
-
sessionId?: string
|
|
486
|
-
}
|
package/src/utils/compact.ts
DELETED
|
@@ -1,207 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Context Compression / Auto-Compaction
|
|
3
|
-
*
|
|
4
|
-
* Summarizes long conversation histories when context window fills up.
|
|
5
|
-
* Three-tier system:
|
|
6
|
-
* 1. Auto-compact: triggered when tokens exceed threshold
|
|
7
|
-
* 2. Micro-compact: cache-aware per-request optimization
|
|
8
|
-
* 3. Session memory compaction: consolidates across sessions
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
import type { LLMProvider } from '../providers/types.js'
|
|
12
|
-
import type { NormalizedMessageParam } from '../providers/types.js'
|
|
13
|
-
import {
|
|
14
|
-
estimateMessagesTokens,
|
|
15
|
-
getAutoCompactThreshold,
|
|
16
|
-
} from './tokens.js'
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* State for tracking auto-compaction across turns.
|
|
20
|
-
*/
|
|
21
|
-
export interface AutoCompactState {
|
|
22
|
-
compacted: boolean
|
|
23
|
-
turnCounter: number
|
|
24
|
-
consecutiveFailures: number
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Create initial auto-compact state.
|
|
29
|
-
*/
|
|
30
|
-
export function createAutoCompactState(): AutoCompactState {
|
|
31
|
-
return {
|
|
32
|
-
compacted: false,
|
|
33
|
-
turnCounter: 0,
|
|
34
|
-
consecutiveFailures: 0,
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Check if auto-compaction should trigger.
|
|
40
|
-
*/
|
|
41
|
-
export function shouldAutoCompact(
|
|
42
|
-
messages: any[],
|
|
43
|
-
model: string,
|
|
44
|
-
state: AutoCompactState,
|
|
45
|
-
): boolean {
|
|
46
|
-
if (state.consecutiveFailures >= 3) return false
|
|
47
|
-
|
|
48
|
-
const estimatedTokens = estimateMessagesTokens(messages)
|
|
49
|
-
const threshold = getAutoCompactThreshold(model)
|
|
50
|
-
|
|
51
|
-
return estimatedTokens >= threshold
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Compact conversation by summarizing with the LLM.
|
|
56
|
-
*
|
|
57
|
-
* Sends the entire conversation to the LLM for summarization,
|
|
58
|
-
* then replaces the history with a compact summary.
|
|
59
|
-
*/
|
|
60
|
-
export async function compactConversation(
|
|
61
|
-
provider: LLMProvider,
|
|
62
|
-
model: string,
|
|
63
|
-
messages: any[],
|
|
64
|
-
state: AutoCompactState,
|
|
65
|
-
): Promise<{
|
|
66
|
-
compactedMessages: NormalizedMessageParam[]
|
|
67
|
-
summary: string
|
|
68
|
-
state: AutoCompactState
|
|
69
|
-
}> {
|
|
70
|
-
try {
|
|
71
|
-
// Strip images before compacting to save tokens
|
|
72
|
-
const strippedMessages = stripImagesFromMessages(messages)
|
|
73
|
-
|
|
74
|
-
// Build compaction prompt
|
|
75
|
-
const compactionPrompt = buildCompactionPrompt(strippedMessages)
|
|
76
|
-
|
|
77
|
-
const response = await provider.createMessage({
|
|
78
|
-
model,
|
|
79
|
-
maxTokens: 8192,
|
|
80
|
-
system: 'You are a conversation summarizer. Create a detailed summary of the conversation that preserves all important context, decisions made, files modified, tool outputs, and current state. The summary should allow the conversation to continue seamlessly.',
|
|
81
|
-
messages: [
|
|
82
|
-
{
|
|
83
|
-
role: 'user',
|
|
84
|
-
content: compactionPrompt,
|
|
85
|
-
},
|
|
86
|
-
],
|
|
87
|
-
})
|
|
88
|
-
|
|
89
|
-
const summary = response.content
|
|
90
|
-
.filter((b) => b.type === 'text')
|
|
91
|
-
.map((b) => (b as { type: 'text'; text: string }).text)
|
|
92
|
-
.join('\n')
|
|
93
|
-
|
|
94
|
-
// Replace messages with summary
|
|
95
|
-
const compactedMessages: NormalizedMessageParam[] = [
|
|
96
|
-
{
|
|
97
|
-
role: 'user',
|
|
98
|
-
content: `[Previous conversation summary]\n\n${summary}\n\n[End of summary - conversation continues below]`,
|
|
99
|
-
},
|
|
100
|
-
{
|
|
101
|
-
role: 'assistant',
|
|
102
|
-
content: 'I understand the context from the previous conversation. I\'ll continue from where we left off.',
|
|
103
|
-
},
|
|
104
|
-
]
|
|
105
|
-
|
|
106
|
-
return {
|
|
107
|
-
compactedMessages,
|
|
108
|
-
summary,
|
|
109
|
-
state: {
|
|
110
|
-
compacted: true,
|
|
111
|
-
turnCounter: state.turnCounter,
|
|
112
|
-
consecutiveFailures: 0,
|
|
113
|
-
},
|
|
114
|
-
}
|
|
115
|
-
} catch (err: any) {
|
|
116
|
-
return {
|
|
117
|
-
compactedMessages: messages,
|
|
118
|
-
summary: '',
|
|
119
|
-
state: {
|
|
120
|
-
...state,
|
|
121
|
-
consecutiveFailures: state.consecutiveFailures + 1,
|
|
122
|
-
},
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* Strip images from messages for compaction safety.
|
|
129
|
-
*/
|
|
130
|
-
function stripImagesFromMessages(
|
|
131
|
-
messages: any[],
|
|
132
|
-
): any[] {
|
|
133
|
-
return messages.map((msg: any) => {
|
|
134
|
-
if (typeof msg.content === 'string') return msg
|
|
135
|
-
|
|
136
|
-
const filtered = (msg.content as any[]).filter((block: any) => {
|
|
137
|
-
return block.type !== 'image'
|
|
138
|
-
})
|
|
139
|
-
|
|
140
|
-
return { ...msg, content: filtered.length > 0 ? filtered : '[content removed for compaction]' }
|
|
141
|
-
})
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Build compaction prompt from messages.
|
|
146
|
-
*/
|
|
147
|
-
function buildCompactionPrompt(messages: any[]): string {
|
|
148
|
-
const parts: string[] = ['Please summarize this conversation:\n']
|
|
149
|
-
|
|
150
|
-
for (const msg of messages) {
|
|
151
|
-
const role = msg.role === 'user' ? 'User' : 'Assistant'
|
|
152
|
-
|
|
153
|
-
if (typeof msg.content === 'string') {
|
|
154
|
-
parts.push(`${role}: ${msg.content.slice(0, 5000)}`)
|
|
155
|
-
} else if (Array.isArray(msg.content)) {
|
|
156
|
-
const texts: string[] = []
|
|
157
|
-
for (const block of msg.content as any[]) {
|
|
158
|
-
if (block.type === 'text') {
|
|
159
|
-
texts.push(block.text.slice(0, 3000))
|
|
160
|
-
} else if (block.type === 'tool_use') {
|
|
161
|
-
texts.push(`[Tool: ${block.name}]`)
|
|
162
|
-
} else if (block.type === 'tool_result') {
|
|
163
|
-
const content = typeof block.content === 'string'
|
|
164
|
-
? block.content.slice(0, 1000)
|
|
165
|
-
: '[tool result]'
|
|
166
|
-
texts.push(`[Tool Result: ${content}]`)
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
if (texts.length > 0) {
|
|
170
|
-
parts.push(`${role}: ${texts.join('\n')}`)
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
return parts.join('\n\n')
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* Micro-compact: optimize messages by truncating large tool results
|
|
180
|
-
* to fit within token budgets.
|
|
181
|
-
*/
|
|
182
|
-
export function microCompactMessages(
|
|
183
|
-
messages: any[],
|
|
184
|
-
maxToolResultChars: number = 50000,
|
|
185
|
-
): any[] {
|
|
186
|
-
return messages.map((msg: any) => {
|
|
187
|
-
if (typeof msg.content === 'string') return msg
|
|
188
|
-
if (!Array.isArray(msg.content)) return msg
|
|
189
|
-
|
|
190
|
-
const content = (msg.content as any[]).map((block: any) => {
|
|
191
|
-
if (block.type === 'tool_result' && typeof block.content === 'string') {
|
|
192
|
-
if (block.content.length > maxToolResultChars) {
|
|
193
|
-
return {
|
|
194
|
-
...block,
|
|
195
|
-
content:
|
|
196
|
-
block.content.slice(0, maxToolResultChars / 2) +
|
|
197
|
-
'\n...(truncated)...\n' +
|
|
198
|
-
block.content.slice(-maxToolResultChars / 2),
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
return block
|
|
203
|
-
})
|
|
204
|
-
|
|
205
|
-
return { ...msg, content }
|
|
206
|
-
})
|
|
207
|
-
}
|