@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/dist/engine.js
ADDED
|
@@ -0,0 +1,496 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* QueryEngine - Core agentic loop
|
|
3
|
+
*
|
|
4
|
+
* Manages the full conversation lifecycle:
|
|
5
|
+
* 1. Take user prompt
|
|
6
|
+
* 2. Build system prompt with context (git status, project context, tools)
|
|
7
|
+
* 3. Call LLM API with tools (via provider abstraction)
|
|
8
|
+
* 4. Stream response
|
|
9
|
+
* 5. Execute tool calls (concurrent for read-only, serial for mutations)
|
|
10
|
+
* 6. Send results back, repeat until done
|
|
11
|
+
* 7. Auto-compact when context exceeds threshold
|
|
12
|
+
* 8. Retry with exponential backoff on transient errors
|
|
13
|
+
*/
|
|
14
|
+
import { estimateCost, } from './utils/tokens.js';
|
|
15
|
+
import { shouldAutoCompact, compactConversation, microCompactMessages, createAutoCompactState, } from './utils/compact.js';
|
|
16
|
+
import { withRetry, isPromptTooLongError, } from './utils/retry.js';
|
|
17
|
+
import { getSystemContext, getUserContext } from './utils/context.js';
|
|
18
|
+
import { normalizeMessagesForAPI } from './utils/messages.js';
|
|
19
|
+
// ============================================================================
|
|
20
|
+
// Tool format conversion
|
|
21
|
+
// ============================================================================
|
|
22
|
+
/** Convert a ToolDefinition to the normalized provider tool format. */
|
|
23
|
+
function toProviderTool(tool) {
|
|
24
|
+
return {
|
|
25
|
+
name: tool.name,
|
|
26
|
+
description: tool.description,
|
|
27
|
+
input_schema: tool.inputSchema,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
// ============================================================================
|
|
31
|
+
// System Prompt Builder
|
|
32
|
+
// ============================================================================
|
|
33
|
+
async function buildSystemPrompt(config) {
|
|
34
|
+
if (config.systemPrompt) {
|
|
35
|
+
const base = config.systemPrompt;
|
|
36
|
+
return config.appendSystemPrompt
|
|
37
|
+
? base + '\n\n' + config.appendSystemPrompt
|
|
38
|
+
: base;
|
|
39
|
+
}
|
|
40
|
+
const parts = [];
|
|
41
|
+
parts.push('You are an AI assistant with access to tools. Use the tools provided to help the user accomplish their tasks.', 'You should use tools when they would help you complete the task more accurately or efficiently.');
|
|
42
|
+
// List available tools with descriptions
|
|
43
|
+
parts.push('\n# Available Tools\n');
|
|
44
|
+
for (const tool of config.tools) {
|
|
45
|
+
parts.push(`- **${tool.name}**: ${tool.description}`);
|
|
46
|
+
}
|
|
47
|
+
// Add agent definitions
|
|
48
|
+
if (config.agents && Object.keys(config.agents).length > 0) {
|
|
49
|
+
parts.push('\n# Available Subagents\n');
|
|
50
|
+
for (const [name, def] of Object.entries(config.agents)) {
|
|
51
|
+
parts.push(`- **${name}**: ${def.description}`);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
// System context (git status, etc.)
|
|
55
|
+
try {
|
|
56
|
+
const sysCtx = await getSystemContext(config.cwd);
|
|
57
|
+
if (sysCtx) {
|
|
58
|
+
parts.push('\n# Environment\n');
|
|
59
|
+
parts.push(sysCtx);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
// Context is best-effort
|
|
64
|
+
}
|
|
65
|
+
// User context (AGENT.md, date)
|
|
66
|
+
try {
|
|
67
|
+
const userCtx = await getUserContext(config.cwd);
|
|
68
|
+
if (userCtx) {
|
|
69
|
+
parts.push('\n# Project Context\n');
|
|
70
|
+
parts.push(userCtx);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
// Context is best-effort
|
|
75
|
+
}
|
|
76
|
+
// Working directory
|
|
77
|
+
parts.push(`\n# Working Directory\n${config.cwd}`);
|
|
78
|
+
if (config.appendSystemPrompt) {
|
|
79
|
+
parts.push('\n' + config.appendSystemPrompt);
|
|
80
|
+
}
|
|
81
|
+
return parts.join('\n');
|
|
82
|
+
}
|
|
83
|
+
// ============================================================================
|
|
84
|
+
// QueryEngine
|
|
85
|
+
// ============================================================================
|
|
86
|
+
export class QueryEngine {
|
|
87
|
+
config;
|
|
88
|
+
provider;
|
|
89
|
+
messages = [];
|
|
90
|
+
totalUsage = { input_tokens: 0, output_tokens: 0 };
|
|
91
|
+
totalCost = 0;
|
|
92
|
+
turnCount = 0;
|
|
93
|
+
compactState;
|
|
94
|
+
sessionId;
|
|
95
|
+
apiTimeMs = 0;
|
|
96
|
+
hookRegistry;
|
|
97
|
+
constructor(config) {
|
|
98
|
+
this.config = config;
|
|
99
|
+
this.provider = config.provider;
|
|
100
|
+
this.compactState = createAutoCompactState();
|
|
101
|
+
this.sessionId = config.sessionId || crypto.randomUUID();
|
|
102
|
+
this.hookRegistry = config.hookRegistry;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Execute hooks for a lifecycle event.
|
|
106
|
+
* Returns hook outputs; never throws.
|
|
107
|
+
*/
|
|
108
|
+
async executeHooks(event, extra) {
|
|
109
|
+
if (!this.hookRegistry?.hasHooks(event))
|
|
110
|
+
return [];
|
|
111
|
+
try {
|
|
112
|
+
return await this.hookRegistry.execute(event, {
|
|
113
|
+
event,
|
|
114
|
+
sessionId: this.sessionId,
|
|
115
|
+
cwd: this.config.cwd,
|
|
116
|
+
...extra,
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
catch {
|
|
120
|
+
return [];
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Submit a user message and run the agentic loop.
|
|
125
|
+
* Yields SDKMessage events as the agent works.
|
|
126
|
+
*/
|
|
127
|
+
async *submitMessage(prompt) {
|
|
128
|
+
// Hook: SessionStart
|
|
129
|
+
await this.executeHooks('SessionStart');
|
|
130
|
+
// Hook: UserPromptSubmit
|
|
131
|
+
const userHookResults = await this.executeHooks('UserPromptSubmit', {
|
|
132
|
+
toolInput: prompt,
|
|
133
|
+
});
|
|
134
|
+
// Check if any hook blocks the submission
|
|
135
|
+
if (userHookResults.some((r) => r.block)) {
|
|
136
|
+
yield {
|
|
137
|
+
type: 'result',
|
|
138
|
+
subtype: 'error_during_execution',
|
|
139
|
+
is_error: true,
|
|
140
|
+
usage: this.totalUsage,
|
|
141
|
+
num_turns: 0,
|
|
142
|
+
cost: 0,
|
|
143
|
+
errors: ['Blocked by UserPromptSubmit hook'],
|
|
144
|
+
};
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
// Add user message
|
|
148
|
+
this.messages.push({ role: 'user', content: prompt });
|
|
149
|
+
// Build tool definitions for provider
|
|
150
|
+
const tools = this.config.tools.map(toProviderTool);
|
|
151
|
+
// Build system prompt
|
|
152
|
+
const systemPrompt = await buildSystemPrompt(this.config);
|
|
153
|
+
// Emit init system message
|
|
154
|
+
yield {
|
|
155
|
+
type: 'system',
|
|
156
|
+
subtype: 'init',
|
|
157
|
+
session_id: this.sessionId,
|
|
158
|
+
tools: this.config.tools.map(t => t.name),
|
|
159
|
+
model: this.config.model,
|
|
160
|
+
cwd: this.config.cwd,
|
|
161
|
+
mcp_servers: [],
|
|
162
|
+
permission_mode: 'bypassPermissions',
|
|
163
|
+
};
|
|
164
|
+
// Agentic loop
|
|
165
|
+
let turnsRemaining = this.config.maxTurns;
|
|
166
|
+
let budgetExceeded = false;
|
|
167
|
+
let maxOutputRecoveryAttempts = 0;
|
|
168
|
+
const MAX_OUTPUT_RECOVERY = 3;
|
|
169
|
+
while (turnsRemaining > 0) {
|
|
170
|
+
if (this.config.abortSignal?.aborted)
|
|
171
|
+
break;
|
|
172
|
+
// Check budget
|
|
173
|
+
if (this.config.maxBudgetUsd && this.totalCost >= this.config.maxBudgetUsd) {
|
|
174
|
+
budgetExceeded = true;
|
|
175
|
+
break;
|
|
176
|
+
}
|
|
177
|
+
// Auto-compact if context is too large
|
|
178
|
+
if (shouldAutoCompact(this.messages, this.config.model, this.compactState)) {
|
|
179
|
+
await this.executeHooks('PreCompact');
|
|
180
|
+
try {
|
|
181
|
+
const result = await compactConversation(this.provider, this.config.model, this.messages, this.compactState);
|
|
182
|
+
this.messages = result.compactedMessages;
|
|
183
|
+
this.compactState = result.state;
|
|
184
|
+
await this.executeHooks('PostCompact');
|
|
185
|
+
}
|
|
186
|
+
catch {
|
|
187
|
+
// Continue with uncompacted messages
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
// Micro-compact: truncate large tool results
|
|
191
|
+
const apiMessages = microCompactMessages(normalizeMessagesForAPI(this.messages));
|
|
192
|
+
this.turnCount++;
|
|
193
|
+
turnsRemaining--;
|
|
194
|
+
// Make API call with retry via provider
|
|
195
|
+
let response;
|
|
196
|
+
const apiStart = performance.now();
|
|
197
|
+
try {
|
|
198
|
+
response = await withRetry(async () => {
|
|
199
|
+
return this.provider.createMessage({
|
|
200
|
+
model: this.config.model,
|
|
201
|
+
maxTokens: this.config.maxTokens,
|
|
202
|
+
system: systemPrompt,
|
|
203
|
+
messages: apiMessages,
|
|
204
|
+
tools: tools.length > 0 ? tools : undefined,
|
|
205
|
+
thinking: this.config.thinking?.type === 'enabled' &&
|
|
206
|
+
this.config.thinking.budgetTokens
|
|
207
|
+
? {
|
|
208
|
+
type: 'enabled',
|
|
209
|
+
budget_tokens: this.config.thinking.budgetTokens,
|
|
210
|
+
}
|
|
211
|
+
: undefined,
|
|
212
|
+
});
|
|
213
|
+
}, undefined, this.config.abortSignal);
|
|
214
|
+
}
|
|
215
|
+
catch (err) {
|
|
216
|
+
// Handle prompt-too-long by compacting
|
|
217
|
+
if (isPromptTooLongError(err) && !this.compactState.compacted) {
|
|
218
|
+
try {
|
|
219
|
+
const result = await compactConversation(this.provider, this.config.model, this.messages, this.compactState);
|
|
220
|
+
this.messages = result.compactedMessages;
|
|
221
|
+
this.compactState = result.state;
|
|
222
|
+
turnsRemaining++; // Retry this turn
|
|
223
|
+
this.turnCount--;
|
|
224
|
+
continue;
|
|
225
|
+
}
|
|
226
|
+
catch {
|
|
227
|
+
// Can't compact, give up
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
const message = err?.message ? String(err.message) : String(err);
|
|
231
|
+
yield {
|
|
232
|
+
type: 'result',
|
|
233
|
+
subtype: 'error',
|
|
234
|
+
usage: this.totalUsage,
|
|
235
|
+
num_turns: this.turnCount,
|
|
236
|
+
cost: this.totalCost,
|
|
237
|
+
errors: [message],
|
|
238
|
+
};
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
// Track API timing
|
|
242
|
+
this.apiTimeMs += performance.now() - apiStart;
|
|
243
|
+
// Track usage (normalized by provider)
|
|
244
|
+
if (response.usage) {
|
|
245
|
+
this.totalUsage.input_tokens += response.usage.input_tokens;
|
|
246
|
+
this.totalUsage.output_tokens += response.usage.output_tokens;
|
|
247
|
+
if (response.usage.cache_creation_input_tokens) {
|
|
248
|
+
this.totalUsage.cache_creation_input_tokens =
|
|
249
|
+
(this.totalUsage.cache_creation_input_tokens || 0) +
|
|
250
|
+
response.usage.cache_creation_input_tokens;
|
|
251
|
+
}
|
|
252
|
+
if (response.usage.cache_read_input_tokens) {
|
|
253
|
+
this.totalUsage.cache_read_input_tokens =
|
|
254
|
+
(this.totalUsage.cache_read_input_tokens || 0) +
|
|
255
|
+
response.usage.cache_read_input_tokens;
|
|
256
|
+
}
|
|
257
|
+
this.totalCost += estimateCost(this.config.model, response.usage);
|
|
258
|
+
}
|
|
259
|
+
// Add assistant message to conversation
|
|
260
|
+
this.messages.push({ role: 'assistant', content: response.content });
|
|
261
|
+
// Yield assistant message
|
|
262
|
+
yield {
|
|
263
|
+
type: 'assistant',
|
|
264
|
+
message: {
|
|
265
|
+
role: 'assistant',
|
|
266
|
+
content: response.content,
|
|
267
|
+
},
|
|
268
|
+
};
|
|
269
|
+
// Handle max_output_tokens recovery
|
|
270
|
+
if (response.stopReason === 'max_tokens' &&
|
|
271
|
+
maxOutputRecoveryAttempts < MAX_OUTPUT_RECOVERY) {
|
|
272
|
+
maxOutputRecoveryAttempts++;
|
|
273
|
+
// Add continuation prompt
|
|
274
|
+
this.messages.push({
|
|
275
|
+
role: 'user',
|
|
276
|
+
content: 'Please continue from where you left off.',
|
|
277
|
+
});
|
|
278
|
+
continue;
|
|
279
|
+
}
|
|
280
|
+
// Check for tool use
|
|
281
|
+
const toolUseBlocks = response.content.filter((block) => block.type === 'tool_use');
|
|
282
|
+
if (toolUseBlocks.length === 0) {
|
|
283
|
+
break; // No tool calls - agent is done
|
|
284
|
+
}
|
|
285
|
+
// Reset max_output recovery counter on successful tool use
|
|
286
|
+
maxOutputRecoveryAttempts = 0;
|
|
287
|
+
// Execute tools (concurrent read-only, serial mutations)
|
|
288
|
+
const toolResults = await this.executeTools(toolUseBlocks);
|
|
289
|
+
// Yield tool results
|
|
290
|
+
for (const result of toolResults) {
|
|
291
|
+
yield {
|
|
292
|
+
type: 'tool_result',
|
|
293
|
+
result: {
|
|
294
|
+
tool_use_id: result.tool_use_id,
|
|
295
|
+
tool_name: result.tool_name || '',
|
|
296
|
+
output: typeof result.content === 'string'
|
|
297
|
+
? result.content
|
|
298
|
+
: JSON.stringify(result.content),
|
|
299
|
+
},
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
// Add tool results to conversation
|
|
303
|
+
this.messages.push({
|
|
304
|
+
role: 'user',
|
|
305
|
+
content: toolResults.map((r) => ({
|
|
306
|
+
type: 'tool_result',
|
|
307
|
+
tool_use_id: r.tool_use_id,
|
|
308
|
+
content: typeof r.content === 'string'
|
|
309
|
+
? r.content
|
|
310
|
+
: JSON.stringify(r.content),
|
|
311
|
+
is_error: r.is_error,
|
|
312
|
+
})),
|
|
313
|
+
});
|
|
314
|
+
if (response.stopReason === 'end_turn')
|
|
315
|
+
break;
|
|
316
|
+
}
|
|
317
|
+
// Hook: Stop (end of agentic loop)
|
|
318
|
+
await this.executeHooks('Stop');
|
|
319
|
+
// Hook: SessionEnd
|
|
320
|
+
await this.executeHooks('SessionEnd');
|
|
321
|
+
// Yield enriched final result
|
|
322
|
+
const endSubtype = budgetExceeded
|
|
323
|
+
? 'error_max_budget_usd'
|
|
324
|
+
: turnsRemaining <= 0
|
|
325
|
+
? 'error_max_turns'
|
|
326
|
+
: 'success';
|
|
327
|
+
yield {
|
|
328
|
+
type: 'result',
|
|
329
|
+
subtype: endSubtype,
|
|
330
|
+
session_id: this.sessionId,
|
|
331
|
+
is_error: endSubtype !== 'success',
|
|
332
|
+
num_turns: this.turnCount,
|
|
333
|
+
total_cost_usd: this.totalCost,
|
|
334
|
+
duration_api_ms: Math.round(this.apiTimeMs),
|
|
335
|
+
usage: this.totalUsage,
|
|
336
|
+
model_usage: { [this.config.model]: { input_tokens: this.totalUsage.input_tokens, output_tokens: this.totalUsage.output_tokens } },
|
|
337
|
+
cost: this.totalCost,
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* Execute tool calls with concurrency control.
|
|
342
|
+
*
|
|
343
|
+
* Read-only tools run concurrently (up to 10 at a time).
|
|
344
|
+
* Mutation tools run sequentially.
|
|
345
|
+
*/
|
|
346
|
+
async executeTools(toolUseBlocks) {
|
|
347
|
+
const context = {
|
|
348
|
+
cwd: this.config.cwd,
|
|
349
|
+
abortSignal: this.config.abortSignal,
|
|
350
|
+
provider: this.provider,
|
|
351
|
+
model: this.config.model,
|
|
352
|
+
apiType: this.provider.apiType,
|
|
353
|
+
};
|
|
354
|
+
const MAX_CONCURRENCY = parseInt(process.env.AGENT_SDK_MAX_TOOL_CONCURRENCY || '10');
|
|
355
|
+
// Partition into read-only (concurrent) and mutation (serial)
|
|
356
|
+
const readOnly = [];
|
|
357
|
+
const mutations = [];
|
|
358
|
+
for (const block of toolUseBlocks) {
|
|
359
|
+
const tool = this.config.tools.find((t) => t.name === block.name);
|
|
360
|
+
if (tool?.isReadOnly?.()) {
|
|
361
|
+
readOnly.push({ block, tool });
|
|
362
|
+
}
|
|
363
|
+
else {
|
|
364
|
+
mutations.push({ block, tool });
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
const results = [];
|
|
368
|
+
// Execute read-only tools concurrently (batched by MAX_CONCURRENCY)
|
|
369
|
+
for (let i = 0; i < readOnly.length; i += MAX_CONCURRENCY) {
|
|
370
|
+
const batch = readOnly.slice(i, i + MAX_CONCURRENCY);
|
|
371
|
+
const batchResults = await Promise.all(batch.map((item) => this.executeSingleTool(item.block, item.tool, context)));
|
|
372
|
+
results.push(...batchResults);
|
|
373
|
+
}
|
|
374
|
+
// Execute mutation tools sequentially
|
|
375
|
+
for (const item of mutations) {
|
|
376
|
+
const result = await this.executeSingleTool(item.block, item.tool, context);
|
|
377
|
+
results.push(result);
|
|
378
|
+
}
|
|
379
|
+
return results;
|
|
380
|
+
}
|
|
381
|
+
/**
|
|
382
|
+
* Execute a single tool with permission checking.
|
|
383
|
+
*/
|
|
384
|
+
async executeSingleTool(block, tool, context) {
|
|
385
|
+
if (!tool) {
|
|
386
|
+
return {
|
|
387
|
+
type: 'tool_result',
|
|
388
|
+
tool_use_id: block.id,
|
|
389
|
+
content: `Error: Unknown tool "${block.name}"`,
|
|
390
|
+
is_error: true,
|
|
391
|
+
tool_name: block.name,
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
// Check enabled
|
|
395
|
+
if (tool.isEnabled && !tool.isEnabled()) {
|
|
396
|
+
return {
|
|
397
|
+
type: 'tool_result',
|
|
398
|
+
tool_use_id: block.id,
|
|
399
|
+
content: `Error: Tool "${block.name}" is not enabled`,
|
|
400
|
+
is_error: true,
|
|
401
|
+
tool_name: block.name,
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
// Check permissions
|
|
405
|
+
if (this.config.canUseTool) {
|
|
406
|
+
try {
|
|
407
|
+
const permission = await this.config.canUseTool(tool, block.input);
|
|
408
|
+
if (permission.behavior === 'deny') {
|
|
409
|
+
return {
|
|
410
|
+
type: 'tool_result',
|
|
411
|
+
tool_use_id: block.id,
|
|
412
|
+
content: permission.message || `Permission denied for tool "${block.name}"`,
|
|
413
|
+
is_error: true,
|
|
414
|
+
tool_name: block.name,
|
|
415
|
+
};
|
|
416
|
+
}
|
|
417
|
+
if (permission.updatedInput !== undefined) {
|
|
418
|
+
block = { ...block, input: permission.updatedInput };
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
catch (err) {
|
|
422
|
+
return {
|
|
423
|
+
type: 'tool_result',
|
|
424
|
+
tool_use_id: block.id,
|
|
425
|
+
content: `Permission check error: ${err.message}`,
|
|
426
|
+
is_error: true,
|
|
427
|
+
tool_name: block.name,
|
|
428
|
+
};
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
// Hook: PreToolUse
|
|
432
|
+
const preHookResults = await this.executeHooks('PreToolUse', {
|
|
433
|
+
toolName: block.name,
|
|
434
|
+
toolInput: block.input,
|
|
435
|
+
toolUseId: block.id,
|
|
436
|
+
});
|
|
437
|
+
// Check if any hook blocks this tool
|
|
438
|
+
if (preHookResults.some((r) => r.block)) {
|
|
439
|
+
const msg = preHookResults.find((r) => r.message)?.message || 'Blocked by PreToolUse hook';
|
|
440
|
+
return {
|
|
441
|
+
type: 'tool_result',
|
|
442
|
+
tool_use_id: block.id,
|
|
443
|
+
content: msg,
|
|
444
|
+
is_error: true,
|
|
445
|
+
tool_name: block.name,
|
|
446
|
+
};
|
|
447
|
+
}
|
|
448
|
+
// Execute the tool
|
|
449
|
+
try {
|
|
450
|
+
const result = await tool.call(block.input, context);
|
|
451
|
+
// Hook: PostToolUse
|
|
452
|
+
await this.executeHooks('PostToolUse', {
|
|
453
|
+
toolName: block.name,
|
|
454
|
+
toolInput: block.input,
|
|
455
|
+
toolOutput: typeof result.content === 'string' ? result.content : JSON.stringify(result.content),
|
|
456
|
+
toolUseId: block.id,
|
|
457
|
+
});
|
|
458
|
+
return { ...result, tool_use_id: block.id, tool_name: block.name };
|
|
459
|
+
}
|
|
460
|
+
catch (err) {
|
|
461
|
+
// Hook: PostToolUseFailure
|
|
462
|
+
await this.executeHooks('PostToolUseFailure', {
|
|
463
|
+
toolName: block.name,
|
|
464
|
+
toolInput: block.input,
|
|
465
|
+
toolUseId: block.id,
|
|
466
|
+
error: err.message,
|
|
467
|
+
});
|
|
468
|
+
return {
|
|
469
|
+
type: 'tool_result',
|
|
470
|
+
tool_use_id: block.id,
|
|
471
|
+
content: `Tool execution error: ${err.message}`,
|
|
472
|
+
is_error: true,
|
|
473
|
+
tool_name: block.name,
|
|
474
|
+
};
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
/**
|
|
478
|
+
* Get current messages for session persistence.
|
|
479
|
+
*/
|
|
480
|
+
getMessages() {
|
|
481
|
+
return [...this.messages];
|
|
482
|
+
}
|
|
483
|
+
/**
|
|
484
|
+
* Get total usage across all turns.
|
|
485
|
+
*/
|
|
486
|
+
getUsage() {
|
|
487
|
+
return { ...this.totalUsage };
|
|
488
|
+
}
|
|
489
|
+
/**
|
|
490
|
+
* Get total cost.
|
|
491
|
+
*/
|
|
492
|
+
getCost() {
|
|
493
|
+
return this.totalCost;
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
//# sourceMappingURL=engine.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"engine.js","sourceRoot":"","sources":["../src/engine.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAgBH,OAAO,EAEL,YAAY,GAEb,MAAM,mBAAmB,CAAA;AAC1B,OAAO,EACL,iBAAiB,EACjB,mBAAmB,EACnB,oBAAoB,EACpB,sBAAsB,GAEvB,MAAM,oBAAoB,CAAA;AAC3B,OAAO,EACL,SAAS,EACT,oBAAoB,GACrB,MAAM,kBAAkB,CAAA;AACzB,OAAO,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAA;AACrE,OAAO,EAAE,uBAAuB,EAAE,MAAM,qBAAqB,CAAA;AAG7D,+EAA+E;AAC/E,yBAAyB;AACzB,+EAA+E;AAE/E,uEAAuE;AACvE,SAAS,cAAc,CAAC,IAAoB;IAC1C,OAAO;QAC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B,KAAK,EAAE,GAAG,CAAC,OAAO;aACnB,CAAC,CAAA;YAEF,OAAO;gBACL,IAAI,EAAE,aAAa;gBACnB,WAAW,EAAE,KAAK,CAAC,EAAE;gBACrB,OAAO,EAAE,yBAAyB,GAAG,CAAC,OAAO,EAAE;gBAC/C,QAAQ,EAAE,IAAI;gBACd,SAAS,EAAE,KAAK,CAAC,IAAI;aACtB,CAAA;QACH,CAAC;IACH,CAAC;IAED;;OAEG;IACH,WAAW;QACT,OAAO,CAAC,GAAG,IAAI,CAAC,QAAQ,CAAC,CAAA;IAC3B,CAAC;IAED;;OAEG;IACH,QAAQ;QACN,OAAO,EAAE,GAAG,IAAI,CAAC,UAAU,EAAE,CAAA;IAC/B,CAAC;IAED;;OAEG;IACH,OAAO;QACL,OAAO,IAAI,CAAC,SAAS,CAAA;IACvB,CAAC;CACF"}
|
package/dist/hooks.d.ts
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
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
|
+
* All supported hook events.
|
|
27
|
+
*/
|
|
28
|
+
export declare const HOOK_EVENTS: readonly ["PreToolUse", "PostToolUse", "PostToolUseFailure", "SessionStart", "SessionEnd", "Stop", "SubagentStart", "SubagentStop", "UserPromptSubmit", "PermissionRequest", "PermissionDenied", "TaskCreated", "TaskCompleted", "ConfigChange", "CwdChanged", "FileChanged", "Notification", "PreCompact", "PostCompact", "TeammateIdle"];
|
|
29
|
+
export type HookEvent = typeof HOOK_EVENTS[number];
|
|
30
|
+
/**
|
|
31
|
+
* Hook definition.
|
|
32
|
+
*/
|
|
33
|
+
export interface HookDefinition {
|
|
34
|
+
/** Shell command or function to execute */
|
|
35
|
+
command?: string;
|
|
36
|
+
/** Function handler */
|
|
37
|
+
handler?: (input: HookInput) => Promise<HookOutput | void>;
|
|
38
|
+
/** Tool name matcher (regex pattern) */
|
|
39
|
+
matcher?: string;
|
|
40
|
+
/** Timeout in milliseconds */
|
|
41
|
+
timeout?: number;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Hook input passed to handlers.
|
|
45
|
+
*/
|
|
46
|
+
export interface HookInput {
|
|
47
|
+
event: HookEvent;
|
|
48
|
+
toolName?: string;
|
|
49
|
+
toolInput?: unknown;
|
|
50
|
+
toolOutput?: unknown;
|
|
51
|
+
toolUseId?: string;
|
|
52
|
+
sessionId?: string;
|
|
53
|
+
cwd?: string;
|
|
54
|
+
error?: string;
|
|
55
|
+
[key: string]: unknown;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Hook output returned by handlers.
|
|
59
|
+
*/
|
|
60
|
+
export interface HookOutput {
|
|
61
|
+
/** Message to append to conversation */
|
|
62
|
+
message?: string;
|
|
63
|
+
/** Permission update */
|
|
64
|
+
permissionUpdate?: {
|
|
65
|
+
tool: string;
|
|
66
|
+
behavior: 'allow' | 'deny';
|
|
67
|
+
};
|
|
68
|
+
/** Whether to block the action */
|
|
69
|
+
block?: boolean;
|
|
70
|
+
/** Notification */
|
|
71
|
+
notification?: {
|
|
72
|
+
title: string;
|
|
73
|
+
body: string;
|
|
74
|
+
level?: 'info' | 'warning' | 'error';
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Hook configuration (from settings).
|
|
79
|
+
*/
|
|
80
|
+
export type HookConfig = Record<string, HookDefinition[]>;
|
|
81
|
+
/**
|
|
82
|
+
* Hook registry for managing and executing hooks.
|
|
83
|
+
*/
|
|
84
|
+
export declare class HookRegistry {
|
|
85
|
+
private hooks;
|
|
86
|
+
/**
|
|
87
|
+
* Register hooks from configuration.
|
|
88
|
+
*/
|
|
89
|
+
registerFromConfig(config: HookConfig): void;
|
|
90
|
+
/**
|
|
91
|
+
* Register a single hook.
|
|
92
|
+
*/
|
|
93
|
+
register(event: HookEvent, definition: HookDefinition): void;
|
|
94
|
+
/**
|
|
95
|
+
* Execute hooks for an event.
|
|
96
|
+
*/
|
|
97
|
+
execute(event: HookEvent, input: HookInput): Promise<HookOutput[]>;
|
|
98
|
+
/**
|
|
99
|
+
* Check if any hooks are registered for an event.
|
|
100
|
+
*/
|
|
101
|
+
hasHooks(event: HookEvent): boolean;
|
|
102
|
+
/**
|
|
103
|
+
* Clear all hooks.
|
|
104
|
+
*/
|
|
105
|
+
clear(): void;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Create a default hook registry.
|
|
109
|
+
*/
|
|
110
|
+
export declare function createHookRegistry(config?: HookConfig): HookRegistry;
|
|
111
|
+
//# sourceMappingURL=hooks.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../src/hooks.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAIH;;GAEG;AACH,eAAO,MAAM,WAAW,4UAqBd,CAAA;AAEV,MAAM,MAAM,SAAS,GAAG,OAAO,WAAW,CAAC,MAAM,CAAC,CAAA;AAElD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