@amical/aiagent-sdk 0.1.0 → 0.1.1
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/index.d.mts +640 -0
- package/dist/index.d.ts +640 -0
- package/dist/index.js +2019 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1972 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +7 -1
- package/.agents/skills/Viral_Writer/SKILL.md +0 -239
- package/.cline_memory.json +0 -58
- package/.node-version +0 -1
- package/Agents.md +0 -95
- package/src/agent/Agent.ts +0 -566
- package/src/agent/index.ts +0 -1
- package/src/index.ts +0 -15
- package/src/llm/NodeApiHandler.ts +0 -117
- package/src/llm/WebApiHandler.ts +0 -118
- package/src/llm/index.ts +0 -47
- package/src/mcp/NodeMcpManager.ts +0 -158
- package/src/mcp/WebMcpManager.ts +0 -30
- package/src/mcp/index.ts +0 -56
- package/src/memory/NodeMemoryManager.ts +0 -82
- package/src/memory/WebMemoryManager.ts +0 -89
- package/src/memory/index.ts +0 -36
- package/src/parser/diff.ts +0 -855
- package/src/parser/index.ts +0 -111
- package/src/parser/parse-assistant-message.ts +0 -240
- package/src/prompt/NodePromptManager.ts +0 -276
- package/src/prompt/WebPromptManager.ts +0 -277
- package/src/prompt/index.ts +0 -28
- package/src/runtime/nodeRuntime.ts +0 -112
- package/src/runtime/web.test.ts +0 -95
- package/src/runtime/webRuntime.ts +0 -119
- package/src/runtime.ts +0 -46
- package/src/shared/tools.ts +0 -270
- package/test-node-agent.ts +0 -113
- package/test-runtime.mjs +0 -36
- package/test.md +0 -33
- package/tsconfig.json +0 -28
- package/tsup.config.ts +0 -21
- package/uapi.ts +0 -3
- package/webFetch.ts +0 -49
- package/webSearch.ts +0 -19
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,640 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* IAgentRuntime 抽象了所有与特定宿主环境(Node.js, Web, VS Code 等)相关的能力。
|
|
3
|
+
* 通过注入 IAgentRuntime,Agent 的核心逻辑可以运行在任何平台上。
|
|
4
|
+
*/
|
|
5
|
+
interface IAgentRuntime {
|
|
6
|
+
/**
|
|
7
|
+
* 文件系统相关操作
|
|
8
|
+
*/
|
|
9
|
+
fs: {
|
|
10
|
+
readFile(path: string): Promise<string>;
|
|
11
|
+
writeFile(path: string, content: string): Promise<void>;
|
|
12
|
+
deleteFile(path: string): Promise<void>;
|
|
13
|
+
deleteDirectory(path: string): Promise<void>;
|
|
14
|
+
fileExists(path: string): Promise<boolean>;
|
|
15
|
+
readDir(path: string): Promise<any[]>;
|
|
16
|
+
mkdir(path: string): Promise<void>;
|
|
17
|
+
/**
|
|
18
|
+
* 跨文件正则搜索
|
|
19
|
+
* @param path 搜索目录
|
|
20
|
+
* @param regex 正则表达式字符串
|
|
21
|
+
* @param filePattern (可选) 文件名匹配模式,比如 '*.ts'
|
|
22
|
+
* @returns 返回格式化后的包含上下文的搜索结果字符串
|
|
23
|
+
*/
|
|
24
|
+
searchFiles?(path: string, regex: string, filePattern?: string): Promise<string>;
|
|
25
|
+
};
|
|
26
|
+
path: {
|
|
27
|
+
join(...paths: string[]): string;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* 环境变量和系统信息
|
|
31
|
+
*/
|
|
32
|
+
env: {
|
|
33
|
+
get(key: string): string | undefined;
|
|
34
|
+
cwd(): string;
|
|
35
|
+
osInfo(): string;
|
|
36
|
+
shell(): string;
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* 终端/命令行相关操作
|
|
40
|
+
*/
|
|
41
|
+
terminal: {
|
|
42
|
+
execCommand(command: string, options?: {
|
|
43
|
+
cwd?: string;
|
|
44
|
+
}): Promise<{
|
|
45
|
+
stdout: string;
|
|
46
|
+
stderr: string;
|
|
47
|
+
exitCode: number;
|
|
48
|
+
}>;
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
declare enum AgentDefaultTool {
|
|
53
|
+
ASK = "agent_ask_followup_question",
|
|
54
|
+
ATTEMPT = "agent_attempt_completion",
|
|
55
|
+
BASH = "agent_execute_command",
|
|
56
|
+
FILE_EDIT = "agent_replace_in_file",
|
|
57
|
+
FILE_READ = "agent_read_file",
|
|
58
|
+
FILE_NEW = "agent_write_to_file",
|
|
59
|
+
FILE_DELETE = "agent_delete_file",
|
|
60
|
+
DIR_DELETE = "agent_delete_directory",
|
|
61
|
+
SEARCH = "agent_search_files",
|
|
62
|
+
LIST_FILES = "agent_list_files",
|
|
63
|
+
LIST_CODE_DEF = "agent_list_code_definition_names",
|
|
64
|
+
BROWSER = "agent_browser_action",
|
|
65
|
+
MCP_USE = "agent_use_mcp_tool",
|
|
66
|
+
MCP_ACCESS = "agent_access_mcp_resource",
|
|
67
|
+
MCP_DOCS = "agent_load_mcp_documentation",
|
|
68
|
+
NEW_TASK = "agent_new_task",
|
|
69
|
+
PLAN_MODE = "agent_plan_mode_respond",
|
|
70
|
+
ACT_MODE = "agent_act_mode_respond",
|
|
71
|
+
TODO = "agent_focus_chain",
|
|
72
|
+
WEB_FETCH = "agent_web_fetch",
|
|
73
|
+
WEB_SEARCH = "agent_web_search",
|
|
74
|
+
CONDENSE = "agent_condense",
|
|
75
|
+
SUMMARIZE_TASK = "agent_summarize_task",
|
|
76
|
+
REPORT_BUG = "agent_report_bug",
|
|
77
|
+
NEW_RULE = "agent_new_rule",
|
|
78
|
+
APPLY_PATCH = "agent_apply_patch",
|
|
79
|
+
GENERATE_EXPLANATION = "agent_generate_explanation",
|
|
80
|
+
LOAD_SKILL = "agent_load_skill",
|
|
81
|
+
USE_SUBAGENTS = "agent_use_subagents"
|
|
82
|
+
}
|
|
83
|
+
declare const TOOL_RESULT_BLOCK = "tool_call_result";
|
|
84
|
+
declare const defaultTools: readonly [{
|
|
85
|
+
readonly name: AgentDefaultTool.ASK;
|
|
86
|
+
readonly description: "Ask the user a clarifying question if more information is needed.";
|
|
87
|
+
readonly params: {
|
|
88
|
+
readonly question: "The question to ask the user.";
|
|
89
|
+
};
|
|
90
|
+
}, {
|
|
91
|
+
readonly name: AgentDefaultTool.ATTEMPT;
|
|
92
|
+
readonly description: "After each tool use, the user will respond with the result of that tool use, i.e. if it succeeded or failed, along with any reasons for failure. Once you've received the results of tool uses and can confirm that the task is complete, use this tool to present the result of your work to the user. Optionally you may provide a CLI command to showcase the result of your work. The user may respond with feedback if they are not satisfied with the result, which you can use to make improvements and try again. IMPORTANT NOTE: This tool CANNOT be used until you've confirmed from the user that any previous tool uses were successful. Failure to do so will result in code corruption and system failure. Before using this tool, you must ask yourself in tags if you've confirmed from the user that any previous tool uses were successful. If not, then DO NOT use this tool. If you were using task_progress to update the task progress, you must include the completed list in the result as well.";
|
|
93
|
+
readonly params: {
|
|
94
|
+
readonly result: "(required) The result of the tool use. This should be a clear, specific description of the result.";
|
|
95
|
+
};
|
|
96
|
+
readonly example: "## Example: agent_attempt_completion\n<agent_attempt_completion>\n<result>final result or message</result>\n<task_progress>\n- [x] Set up project structure\n- [x] Install dependencies\n- [x] Run command to start server\n- [x] Test application\n</task_progress>\n</agent_attempt_completion>";
|
|
97
|
+
}, {
|
|
98
|
+
readonly name: AgentDefaultTool.BASH;
|
|
99
|
+
readonly description: "Request to execute a CLI command on the system. Use this when you need to perform system operations or run specific commands to accomplish any step in the user's task. You must tailor your command to the user's system and provide a clear explanation of what the command does. For command chaining, use the appropriate chaining syntax for the user's shell. Prefer to execute complex CLI commands over creating executable scripts, as they are more flexible and easier to run. Commands will be executed in the current working directory: ${context.cwd} Use @workspace:path syntax (e.g., @frontend:src/index.ts) to specify a workspace.";
|
|
100
|
+
readonly params: {
|
|
101
|
+
readonly command: "(required) The CLI command to execute. This should be valid for the current operating system. Ensure the command is properly formatted and does not contain any harmful instructions.";
|
|
102
|
+
};
|
|
103
|
+
readonly example: "## Example: Requesting to execute a command\n\n<agent_execute_command>\n<command>npm run dev</command>\n<requires_approval>false</requires_approval>\n<task_progress>\n- [x] Set up project structure\n- [x] Install dependencies\n- [ ] Run command to start server\n- [ ] Test application\n</task_progress>\n</agent_execute_command>";
|
|
104
|
+
}, {
|
|
105
|
+
readonly name: AgentDefaultTool.FILE_EDIT;
|
|
106
|
+
readonly description: "Replace specific content in a file using a search/replace diff block.";
|
|
107
|
+
readonly params: {
|
|
108
|
+
readonly path: "(required) The absolute or relative file path to modify.";
|
|
109
|
+
readonly diff: "(required) The diff content to apply.";
|
|
110
|
+
};
|
|
111
|
+
readonly example: "## Example: Requesting to make targeted edits to a file\n\n<agent_replace_in_file>\n<path>src/components/App.tsx</path>\n<diff>\n------- SEARCH\nimport React from 'react';\n=======\nimport React, { useState } from 'react';\n+++++++ REPLACE\n\n------- SEARCH\nfunction handleSubmit() {\n saveData();\n setLoading(false);\n}\n\n=======\n+++++++ REPLACE\n\n------- SEARCH\nreturn (\n <div>\n=======\nfunction handleSubmit() {\n saveData();\n setLoading(false);\n}\n\nreturn (\n <div>\n+++++++ REPLACE\n</diff>\n<task_progress>\n- [x] Set up project structure\n- [x] Install dependencies\n- [ ] Create components\n- [ ] Test application\n</task_progress>\n</agent_replace_in_file>";
|
|
112
|
+
}, {
|
|
113
|
+
readonly name: AgentDefaultTool.FILE_READ;
|
|
114
|
+
readonly description: "Read the contents of a specific file.";
|
|
115
|
+
readonly params: {
|
|
116
|
+
readonly path: "(required) The absolute or relative file path to read.";
|
|
117
|
+
};
|
|
118
|
+
}, {
|
|
119
|
+
readonly name: AgentDefaultTool.FILE_NEW;
|
|
120
|
+
readonly description: "Create or overwrite a file with new content.";
|
|
121
|
+
readonly params: {
|
|
122
|
+
readonly path: "(required) The path of the file to write to (relative to the current working directory ${context.cwd}) Use @workspace:path syntax (e.g., @frontend:src/index.ts) to specify a workspace.";
|
|
123
|
+
readonly content: "(required) The content to write to the file.";
|
|
124
|
+
};
|
|
125
|
+
readonly example: "## Example: Requesting to create a new file\n\n<agent_write_to_file>\n<path>src/frontend-config.json</path>\n<content>\n{\n \"apiEndpoint\": \"https://api.example.com\",\n \"theme\": {\n \"primaryColor\": \"#007bff\",\n \"secondaryColor\": \"#6c757d\",\n \"fontFamily\": \"Arial, sans-serif\"\n },\n \"features\": {\n \"darkMode\": true,\n \"notifications\": true,\n \"analytics\": false\n },\n \"version\": \"1.0.0\"\n}\n</content>\n<task_progress>\n- [x] Set up project structure\n- [x] Install dependencies\n- [x] Create components\n- [ ] Test application\n</task_progress>\n</agent_write_to_file>";
|
|
126
|
+
}, {
|
|
127
|
+
readonly name: AgentDefaultTool.FILE_DELETE;
|
|
128
|
+
readonly description: "Delete a file.";
|
|
129
|
+
readonly params: {
|
|
130
|
+
readonly path: "(required) The path of the file to delete to (relative to the current working directory ${context.cwd}) Use @workspace:path syntax (e.g., @frontend:src/index.ts) to specify a workspace.";
|
|
131
|
+
};
|
|
132
|
+
}, {
|
|
133
|
+
readonly name: AgentDefaultTool.DIR_DELETE;
|
|
134
|
+
readonly description: "Delete a directory.";
|
|
135
|
+
readonly params: {
|
|
136
|
+
readonly path: "(required) The path of the directory to delete to (relative to the current working directory ${context.cwd}) Use @workspace:path syntax (e.g., @frontend:src/index.ts) to specify a workspace.";
|
|
137
|
+
};
|
|
138
|
+
}, {
|
|
139
|
+
readonly name: AgentDefaultTool.SEARCH;
|
|
140
|
+
readonly description: "Search for files in a directory that match a regex pattern or glob pattern.";
|
|
141
|
+
readonly params: {
|
|
142
|
+
readonly path: "(required) The absolute or relative directory path to search.";
|
|
143
|
+
readonly regex: "(required) The regex pattern to match files against.";
|
|
144
|
+
readonly file_pattern: "(optional) glob pattern like *.ts.";
|
|
145
|
+
};
|
|
146
|
+
}, {
|
|
147
|
+
readonly name: AgentDefaultTool.LIST_FILES;
|
|
148
|
+
readonly description: "List all files in a directory.";
|
|
149
|
+
readonly params: {
|
|
150
|
+
readonly path: "(required) The absolute or relative directory path to list.";
|
|
151
|
+
};
|
|
152
|
+
}, {
|
|
153
|
+
readonly name: AgentDefaultTool.NEW_TASK;
|
|
154
|
+
readonly description: "Create a new task with a given context.";
|
|
155
|
+
readonly params: {
|
|
156
|
+
readonly context: "(required) The context of the new task.";
|
|
157
|
+
};
|
|
158
|
+
readonly example: "## Example: Creating a new task\n<agent_new_task>\n<context>\n1. Current Work:\n [Detailed description]\n\n2. Key Technical Concepts:\n - [Concept 1]\n - [Concept 2]\n - [...]\n\n3. Relevant Files and Code:\n - [File Name 1]\n - [Summary of why this file is important]\n - [Summary of the changes made to this file, if any]\n - [Important Code Snippet]\n - [File Name 2]\n - [Important Code Snippet]\n - [...]\n\n4. Problem Solving:\n [Detailed description]\n\n5. Pending Tasks and Next Steps:\n - [Task 1 details & next steps]\n - [Task 2 details & next steps]\n - [...]\n</context>\n</agent_new_task>";
|
|
159
|
+
}, {
|
|
160
|
+
readonly name: AgentDefaultTool.LOAD_SKILL;
|
|
161
|
+
readonly description: "Load a skill to perform a task. important: skill_name is required. must use this tool to load the skill; do not use any other method to read it.";
|
|
162
|
+
readonly params: {
|
|
163
|
+
readonly skill_name: "(required) The name of the skill to load.";
|
|
164
|
+
};
|
|
165
|
+
}];
|
|
166
|
+
declare const toolUseNames: AgentDefaultTool[];
|
|
167
|
+
declare function setDynamicToolUseNames(namespace: string, names: string[]): void;
|
|
168
|
+
declare function getToolUseNames(): string[];
|
|
169
|
+
declare const READ_ONLY_TOOLS: readonly [AgentDefaultTool.LIST_FILES, AgentDefaultTool.FILE_READ, AgentDefaultTool.SEARCH, AgentDefaultTool.LIST_CODE_DEF, AgentDefaultTool.BROWSER, AgentDefaultTool.ASK, AgentDefaultTool.WEB_SEARCH, AgentDefaultTool.WEB_FETCH, AgentDefaultTool.LOAD_SKILL, AgentDefaultTool.USE_SUBAGENTS];
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* @description **Version 2**
|
|
173
|
+
* Parses an assistant message string potentially containing mixed text and tool usage blocks
|
|
174
|
+
* marked with XML-like tags into an array of structured content objects.
|
|
175
|
+
*
|
|
176
|
+
* This version aims for efficiency by avoiding the character-by-character accumulator of V1.
|
|
177
|
+
* It iterates through the string using an index `i`. At each position, it checks if the substring
|
|
178
|
+
* *ending* at `i` matches any known opening or closing tags for tools or parameters using `startsWith`
|
|
179
|
+
* with an offset.
|
|
180
|
+
* It uses pre-computed Maps (`toolUseOpenTags`, `toolParamOpenTags`) for quick tag lookups.
|
|
181
|
+
* State is managed using indices (`currentTextContentStart`, `currentToolUseStart`, `currentParamValueStart`)
|
|
182
|
+
* pointing to the start of the current block within the original `assistantMessage` string.
|
|
183
|
+
* Slicing is used to extract content only when a block (text, parameter, or tool use) is completed.
|
|
184
|
+
* Special handling for `write_to_file` and `new_rule` content parameters is included, using `indexOf`
|
|
185
|
+
* and `lastIndexOf` on the relevant slice to handle potentially nested closing tags.
|
|
186
|
+
* If the input string ends mid-block, the last open block is added and marked as partial.
|
|
187
|
+
*
|
|
188
|
+
* @param assistantMessage The raw string output from the assistant.
|
|
189
|
+
* @returns An array of `AssistantMessageContent` objects, which can be `TextContent` or `ToolUse`.
|
|
190
|
+
* Blocks that were not fully closed by the end of the input string will have their `partial` flag set to `true`.
|
|
191
|
+
*/
|
|
192
|
+
declare function parseAssistantMessageV2(assistantMessage: string, toolParamNames: any): AssistantMessageContent[];
|
|
193
|
+
|
|
194
|
+
type AssistantMessageContent = TextStreamContent | ToolUse | ReasoningStreamContent;
|
|
195
|
+
|
|
196
|
+
interface TextStreamContent {
|
|
197
|
+
type: "text";
|
|
198
|
+
content: string;
|
|
199
|
+
partial: boolean;
|
|
200
|
+
}
|
|
201
|
+
declare const toolParamNames: readonly ["command", "requires_approval", "path", "absolutePath", "content", "diff", "regex", "file_pattern", "recursive", "action", "url", "coordinate", "text", "query", "allowed_domains", "blocked_domains", "prompt", "server_name", "tool_name", "arguments", "uri", "question", "options", "response", "result", "context", "title", "what_happened", "steps_to_reproduce", "api_request_output", "additional_context", "needs_more_exploration", "task_progress", "timeout", "input", "from_ref", "to_ref", "skill_name", "prompt_1", "prompt_2", "prompt_3", "prompt_4", "prompt_5", "start_line", "end_line"];
|
|
202
|
+
type ToolParamName = (typeof toolParamNames)[number];
|
|
203
|
+
interface ToolUse {
|
|
204
|
+
type: "tool_use";
|
|
205
|
+
name: AgentDefaultTool | string;
|
|
206
|
+
params: Partial<Record<ToolParamName, string>>;
|
|
207
|
+
partial: boolean;
|
|
208
|
+
/**
|
|
209
|
+
* Whether this tool use was initiated by a native tool call
|
|
210
|
+
*/
|
|
211
|
+
isNativeToolCall?: boolean;
|
|
212
|
+
/**
|
|
213
|
+
* The call / response ID this tool use is associated with.
|
|
214
|
+
*/
|
|
215
|
+
call_id?: string;
|
|
216
|
+
/**
|
|
217
|
+
* Thought signature associated with this tool use, used by Gemini
|
|
218
|
+
*/
|
|
219
|
+
signature?: string;
|
|
220
|
+
}
|
|
221
|
+
interface ReasoningStreamContent {
|
|
222
|
+
type: "reasoning";
|
|
223
|
+
/**
|
|
224
|
+
* The reasoning text generated by the model.
|
|
225
|
+
* Redacted reasoning block will have this field set to "[REDACTED]" or an empty string.
|
|
226
|
+
*/
|
|
227
|
+
reasoning: string;
|
|
228
|
+
/**
|
|
229
|
+
* openrouter has various properties that we can pass back unmodified in api requests to preserve reasoning traces
|
|
230
|
+
*/
|
|
231
|
+
details?: any;
|
|
232
|
+
/**
|
|
233
|
+
* It's used when sending the thinking block back to the API.
|
|
234
|
+
* API expects this in completed form, not as array of deltas.
|
|
235
|
+
*/
|
|
236
|
+
signature?: string;
|
|
237
|
+
/**
|
|
238
|
+
* whether this reasoning block has been redacted
|
|
239
|
+
*/
|
|
240
|
+
redacted?: boolean;
|
|
241
|
+
/**
|
|
242
|
+
* redacted data
|
|
243
|
+
*/
|
|
244
|
+
data?: string;
|
|
245
|
+
/**
|
|
246
|
+
* Indicates whether this is a partial reasoning block
|
|
247
|
+
*/
|
|
248
|
+
partial: boolean;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
declare class NodePromptManager implements IPromptManager {
|
|
252
|
+
getSystemPrompt(context: {
|
|
253
|
+
cwd: string;
|
|
254
|
+
os: string;
|
|
255
|
+
shell: string;
|
|
256
|
+
skills: any;
|
|
257
|
+
customInstructions?: string;
|
|
258
|
+
allowDefaultTools?: string[];
|
|
259
|
+
customTools?: any[];
|
|
260
|
+
}): Promise<string>;
|
|
261
|
+
getEnvironmentInfoPrompt(context: {
|
|
262
|
+
cwd: string;
|
|
263
|
+
os: string;
|
|
264
|
+
shell: string;
|
|
265
|
+
}): string;
|
|
266
|
+
getSkillsPrompt(skills: any[]): Promise<string>;
|
|
267
|
+
getToolInstructions(context: {
|
|
268
|
+
cwd: string;
|
|
269
|
+
os: string;
|
|
270
|
+
allowDefaultTools?: string[];
|
|
271
|
+
customTools?: any[];
|
|
272
|
+
}): string;
|
|
273
|
+
getNoUseToolInstructions(context: any): string;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
declare class WebPromptManager implements IPromptManager {
|
|
277
|
+
getSystemPrompt(context: {
|
|
278
|
+
cwd: string;
|
|
279
|
+
os: string;
|
|
280
|
+
shell: string;
|
|
281
|
+
skills: any;
|
|
282
|
+
customInstructions?: string;
|
|
283
|
+
allowDefaultTools?: string[];
|
|
284
|
+
customTools?: any[];
|
|
285
|
+
}): Promise<string>;
|
|
286
|
+
getEnvironmentInfoPrompt(context: {
|
|
287
|
+
cwd: string;
|
|
288
|
+
os: string;
|
|
289
|
+
shell: string;
|
|
290
|
+
}): string;
|
|
291
|
+
getSkillsPrompt(skills: any[]): Promise<string>;
|
|
292
|
+
getToolInstructions(context: {
|
|
293
|
+
cwd: string;
|
|
294
|
+
os: string;
|
|
295
|
+
allowDefaultTools?: string[];
|
|
296
|
+
customTools?: any[];
|
|
297
|
+
}): string;
|
|
298
|
+
getNoUseToolInstructions(context: any): string;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* 抽象的 System Prompt 管理器。
|
|
303
|
+
* 替代原始项目中复杂的 templates/variants,交由宿主自行提供或使用默认简单实现。
|
|
304
|
+
*/
|
|
305
|
+
interface IPromptManager {
|
|
306
|
+
/**
|
|
307
|
+
* 根据当前运行环境与动态参数,获取最终的系统提示词
|
|
308
|
+
*/
|
|
309
|
+
getSystemPrompt(context: any): Promise<string>;
|
|
310
|
+
/**
|
|
311
|
+
* 获取特定工具或技能的指令文本
|
|
312
|
+
*/
|
|
313
|
+
getToolInstructions?(context: any): string;
|
|
314
|
+
/**
|
|
315
|
+
* 获取环境信息提示词
|
|
316
|
+
*/
|
|
317
|
+
getEnvironmentInfoPrompt?(context: any): string;
|
|
318
|
+
/**
|
|
319
|
+
* 获取未使用工具的错误提示词
|
|
320
|
+
*/
|
|
321
|
+
getNoUseToolInstructions(context: any): string;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* Node 端 MemoryManager 实现
|
|
326
|
+
* 将对话历史持久化到本地文件系统(JSON 格式)。
|
|
327
|
+
*/
|
|
328
|
+
declare class NodeMemoryManager implements IMemoryManager {
|
|
329
|
+
private filePath;
|
|
330
|
+
private maxMessages;
|
|
331
|
+
/**
|
|
332
|
+
* @param dbPath 存储历史记录的本地文件路径(例如:'./.cline_memory.json')
|
|
333
|
+
* @param maxMessages 保留的最大消息数量,用于简单的历史截断(默认 150)
|
|
334
|
+
*/
|
|
335
|
+
constructor(dbPath: string, maxMessages?: number);
|
|
336
|
+
private ensureFile;
|
|
337
|
+
private readMessages;
|
|
338
|
+
private writeMessages;
|
|
339
|
+
getHistory(): Promise<AgentMessage[]>;
|
|
340
|
+
appendMessage(message: AgentMessage): Promise<void>;
|
|
341
|
+
condenseHistory(): Promise<void>;
|
|
342
|
+
clear(): Promise<void>;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* Web 端 MemoryManager 实现
|
|
347
|
+
* 使用 IndexedDB (通过封装) 或 LocalStorage / SessionStorage 来持久化对话历史。
|
|
348
|
+
* 此处提供一个基于 SessionStorage / 内存 回退的轻量实现。
|
|
349
|
+
*/
|
|
350
|
+
declare class WebMemoryManager implements IMemoryManager {
|
|
351
|
+
private storageKey;
|
|
352
|
+
private maxMessages;
|
|
353
|
+
private inMemoryFallback;
|
|
354
|
+
/**
|
|
355
|
+
* @param storageKey SessionStorage 中的键名
|
|
356
|
+
* @param maxMessages 保留的最大消息数量,用于简单的历史截断(默认 150)
|
|
357
|
+
*/
|
|
358
|
+
constructor(storageKey?: string, maxMessages?: number);
|
|
359
|
+
private isStorageAvailable;
|
|
360
|
+
private readMessages;
|
|
361
|
+
private writeMessages;
|
|
362
|
+
getHistory(): Promise<AgentMessage[]>;
|
|
363
|
+
appendMessage(message: AgentMessage): Promise<void>;
|
|
364
|
+
condenseHistory(): Promise<void>;
|
|
365
|
+
clear(): Promise<void>;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/**
|
|
369
|
+
* 统一的消息结构(这里做简单抽象,实际使用可对齐 LLM API 标准结构)
|
|
370
|
+
*/
|
|
371
|
+
interface AgentMessage {
|
|
372
|
+
role: 'user' | 'assistant' | 'system';
|
|
373
|
+
content: string | any[];
|
|
374
|
+
}
|
|
375
|
+
/**
|
|
376
|
+
* 抽象的 Memory 管理器。
|
|
377
|
+
* 负责记录和截断对话历史,确保不超过 LLM Token 限制。
|
|
378
|
+
*/
|
|
379
|
+
interface IMemoryManager {
|
|
380
|
+
/**
|
|
381
|
+
* 获取当前所有对话历史
|
|
382
|
+
*/
|
|
383
|
+
getHistory(): Promise<AgentMessage[]>;
|
|
384
|
+
/**
|
|
385
|
+
* 追加一条消息到历史中
|
|
386
|
+
*/
|
|
387
|
+
appendMessage(message: AgentMessage): Promise<void>;
|
|
388
|
+
/**
|
|
389
|
+
* 截断或压缩过长历史
|
|
390
|
+
*/
|
|
391
|
+
condenseHistory?(): Promise<void>;
|
|
392
|
+
/**
|
|
393
|
+
* 清空记忆
|
|
394
|
+
*/
|
|
395
|
+
clear(): Promise<void>;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
interface NodeApiHandlerConfig {
|
|
399
|
+
providerId?: string;
|
|
400
|
+
modelId?: string;
|
|
401
|
+
apiKey: string;
|
|
402
|
+
baseURL?: string;
|
|
403
|
+
headers?: Record<string, string>;
|
|
404
|
+
fetch?: typeof fetch;
|
|
405
|
+
}
|
|
406
|
+
/**
|
|
407
|
+
* Node 端的 LLM API 处理器 (已通过 Vercel AI SDK 重构)
|
|
408
|
+
* 支持灵活对接 OpenAI 及各类兼容 OpenAI 规范的平台 (DeepSeek, Dify 等)
|
|
409
|
+
*/
|
|
410
|
+
declare class NodeApiHandler implements IApiHandler {
|
|
411
|
+
private config;
|
|
412
|
+
private conversationId?;
|
|
413
|
+
constructor(config: NodeApiHandlerConfig);
|
|
414
|
+
getModelInfo(): ApiProviderInfo;
|
|
415
|
+
createMessage(systemPrompt: string, messages: AgentMessage[]): AsyncGenerator<ApiStreamChunk, void, unknown>;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
interface WebApiHandlerConfig {
|
|
419
|
+
providerId?: string;
|
|
420
|
+
modelId?: string;
|
|
421
|
+
apiKey: string;
|
|
422
|
+
baseURL?: string;
|
|
423
|
+
headers?: Record<string, string>;
|
|
424
|
+
fetch?: typeof fetch;
|
|
425
|
+
}
|
|
426
|
+
/**
|
|
427
|
+
* Web 端的 LLM API 处理器 (已通过 Vercel AI SDK 重构)
|
|
428
|
+
* 支持灵活对接 OpenAI 及各类兼容 OpenAI 规范的平台 (DeepSeek, Dify 等)
|
|
429
|
+
* 由于 Vercel AI SDK 底层统一使用标准的 Web Fetch API,Web 端的逻辑可以做到与 Node 端完全一致。
|
|
430
|
+
*/
|
|
431
|
+
declare class WebApiHandler implements IApiHandler {
|
|
432
|
+
private config;
|
|
433
|
+
private conversationId?;
|
|
434
|
+
constructor(config: WebApiHandlerConfig);
|
|
435
|
+
getModelInfo(): ApiProviderInfo;
|
|
436
|
+
createMessage(systemPrompt: string, messages: AgentMessage[]): AsyncGenerator<ApiStreamChunk, void, unknown>;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
/**
|
|
440
|
+
* 统一的 API 响应流数据块
|
|
441
|
+
*/
|
|
442
|
+
interface ApiStreamChunk {
|
|
443
|
+
type: "text" | "usage" | "error";
|
|
444
|
+
text?: string;
|
|
445
|
+
usage?: {
|
|
446
|
+
inputTokens: number;
|
|
447
|
+
outputTokens: number;
|
|
448
|
+
};
|
|
449
|
+
error?: Error;
|
|
450
|
+
}
|
|
451
|
+
/**
|
|
452
|
+
* 通用的 LLM 提供者信息
|
|
453
|
+
*/
|
|
454
|
+
interface ApiProviderInfo {
|
|
455
|
+
providerId: string;
|
|
456
|
+
modelId?: string;
|
|
457
|
+
}
|
|
458
|
+
/**
|
|
459
|
+
* 抽象的 LLM API 处理器接口。
|
|
460
|
+
* 宿主环境(VSCode/Web/Node CLI)可以选择注入 AnthropicSDK、OpenAISDK 或是 Fetch 调用实现。
|
|
461
|
+
*/
|
|
462
|
+
interface IApiHandler {
|
|
463
|
+
/**
|
|
464
|
+
* 发起一次到大模型的请求,返回一个可异步迭代的流。
|
|
465
|
+
*/
|
|
466
|
+
createMessage(systemPrompt: string, messages: AgentMessage[]): AsyncGenerator<ApiStreamChunk, void, unknown>;
|
|
467
|
+
/**
|
|
468
|
+
* 获取当前配置的模型信息
|
|
469
|
+
*/
|
|
470
|
+
getModelInfo(): ApiProviderInfo;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
/**
|
|
474
|
+
* MCP Server 描述信息
|
|
475
|
+
*/
|
|
476
|
+
interface McpServerInfo {
|
|
477
|
+
name: string;
|
|
478
|
+
status: "connected" | "disconnected" | "connecting";
|
|
479
|
+
error?: string;
|
|
480
|
+
}
|
|
481
|
+
/**
|
|
482
|
+
* MCP 工具定义
|
|
483
|
+
*/
|
|
484
|
+
interface McpTool {
|
|
485
|
+
name: string;
|
|
486
|
+
description?: string;
|
|
487
|
+
inputSchema: any;
|
|
488
|
+
}
|
|
489
|
+
/**
|
|
490
|
+
* MCP 工具调用结果
|
|
491
|
+
*/
|
|
492
|
+
interface McpToolCallResult {
|
|
493
|
+
content: Array<{
|
|
494
|
+
type: string;
|
|
495
|
+
text?: string;
|
|
496
|
+
data?: any;
|
|
497
|
+
}>;
|
|
498
|
+
isError?: boolean;
|
|
499
|
+
}
|
|
500
|
+
/**
|
|
501
|
+
* 抽象的 MCP 管理器接口。
|
|
502
|
+
* 宿主环境(VSCode/Node)使用基于 StdIO 的 MCP SDK 实现,Web 环境可能使用 SSE 或其他代理机制实现。
|
|
503
|
+
* 通过注入 IMcpManager,Agent 可以无缝使用 MCP 提供的拓展工具。
|
|
504
|
+
*/
|
|
505
|
+
interface IMcpManager {
|
|
506
|
+
/**
|
|
507
|
+
* 获取所有连接的 MCP 服务器状态
|
|
508
|
+
*/
|
|
509
|
+
getServers(): McpServerInfo[];
|
|
510
|
+
/**
|
|
511
|
+
* 获取所有可用的 MCP 工具
|
|
512
|
+
*/
|
|
513
|
+
getTools(): Promise<McpTool[]>;
|
|
514
|
+
/**
|
|
515
|
+
* 调用特定的 MCP 工具
|
|
516
|
+
* @param serverName 提供该工具的服务器名称
|
|
517
|
+
* @param toolName 工具名称
|
|
518
|
+
* @param args 工具参数
|
|
519
|
+
*/
|
|
520
|
+
callTool(serverName: string, toolName: string, args: any): Promise<McpToolCallResult>;
|
|
521
|
+
/**
|
|
522
|
+
* 读取 MCP 资源
|
|
523
|
+
*/
|
|
524
|
+
readResource?(serverName: string, uri: string): Promise<any>;
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
/**
|
|
528
|
+
* Agent 初始化配置依赖
|
|
529
|
+
*/
|
|
530
|
+
interface AgentOptions {
|
|
531
|
+
runtime: IAgentRuntime;
|
|
532
|
+
api: IApiHandler;
|
|
533
|
+
promptManager: IPromptManager;
|
|
534
|
+
memory: IMemoryManager;
|
|
535
|
+
mcp?: IMcpManager;
|
|
536
|
+
customTools?: any[];
|
|
537
|
+
allowDefaultTools?: string[];
|
|
538
|
+
customInstructions?: string;
|
|
539
|
+
/** 最大循环次数,默认150 */
|
|
540
|
+
maxLoopCount?: number;
|
|
541
|
+
}
|
|
542
|
+
/**
|
|
543
|
+
* Agent 状态
|
|
544
|
+
*/
|
|
545
|
+
declare enum AgentState {
|
|
546
|
+
IDLE = "idle",
|
|
547
|
+
THINKING = "thinking",
|
|
548
|
+
WAITING_FOR_USER = "waiting_for_user",
|
|
549
|
+
FINISHED = "finished",
|
|
550
|
+
ERROR = "error"
|
|
551
|
+
}
|
|
552
|
+
/**
|
|
553
|
+
* ClineAgent 核心逻辑循环状态机
|
|
554
|
+
* 它独立于 VS Code 或 Node 环境,纯粹通过依赖注入的方式执行工作流。
|
|
555
|
+
*/
|
|
556
|
+
declare class ClineAgent {
|
|
557
|
+
private runtime;
|
|
558
|
+
private api;
|
|
559
|
+
private promptManager;
|
|
560
|
+
private memory;
|
|
561
|
+
private mcp?;
|
|
562
|
+
state: AgentState;
|
|
563
|
+
allToolParamNames: string[];
|
|
564
|
+
/** 循环计数器 */
|
|
565
|
+
private loopCount;
|
|
566
|
+
/** 最大循环次数 */
|
|
567
|
+
private maxLoopCount;
|
|
568
|
+
skillDir: string;
|
|
569
|
+
customTools: any[];
|
|
570
|
+
customToolNames: Set<string>;
|
|
571
|
+
allowDefaultTools?: string[];
|
|
572
|
+
customInstructions?: string;
|
|
573
|
+
sysPrompt?: string;
|
|
574
|
+
noUseTollSysPrompt?: string;
|
|
575
|
+
onStateChange?: (state: AgentState) => void;
|
|
576
|
+
onStreamChunk?: (chunk: ApiStreamChunk) => void;
|
|
577
|
+
onToolCall?: (tool: ToolUse) => Promise<any | void>;
|
|
578
|
+
constructor(options: AgentOptions);
|
|
579
|
+
private setState;
|
|
580
|
+
/**
|
|
581
|
+
* 获取当前 Agent 的系统提示词
|
|
582
|
+
* 如果 force 为 true,则强制重新获取系统提示词
|
|
583
|
+
*/
|
|
584
|
+
getPrompt(force?: boolean): Promise<{
|
|
585
|
+
sysPrompt: string;
|
|
586
|
+
noUseTollSysPrompt: string;
|
|
587
|
+
}>;
|
|
588
|
+
/**
|
|
589
|
+
* 启动一个新的任务
|
|
590
|
+
*/
|
|
591
|
+
startTask(taskDescription: AgentMessage['content']): Promise<void>;
|
|
592
|
+
private getSkills;
|
|
593
|
+
/**
|
|
594
|
+
* 核心任务循环
|
|
595
|
+
*/
|
|
596
|
+
private loop;
|
|
597
|
+
/**
|
|
598
|
+
* 简单的 Diff 解析与替换辅助方法
|
|
599
|
+
*/
|
|
600
|
+
private applyDiff;
|
|
601
|
+
/**
|
|
602
|
+
* 恢复被中断的循环(例如等待用户输入/确认后继续)
|
|
603
|
+
*/
|
|
604
|
+
resume(userInput?: string): Promise<void>;
|
|
605
|
+
appendHistory(history: AgentMessage[]): Promise<void>;
|
|
606
|
+
getHistory(): Promise<AgentMessage[]>;
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
interface NodeAgentOptions {
|
|
610
|
+
apiConfig: NodeApiHandlerConfig;
|
|
611
|
+
dbPath?: string;
|
|
612
|
+
customInstructions?: string;
|
|
613
|
+
customTools?: any[];
|
|
614
|
+
allowDefaultTools?: string[];
|
|
615
|
+
memory?: NodeMemoryManager | WebMemoryManager;
|
|
616
|
+
maxLoopCount?: number;
|
|
617
|
+
}
|
|
618
|
+
/**
|
|
619
|
+
* 快速创建 Node 端 Agent 的工厂方法
|
|
620
|
+
*/
|
|
621
|
+
declare function createNodeAgent(options: NodeAgentOptions): ClineAgent;
|
|
622
|
+
|
|
623
|
+
interface WebAgentOptions {
|
|
624
|
+
apiConfig: WebApiHandlerConfig;
|
|
625
|
+
dbPath?: string;
|
|
626
|
+
customInstructions?: string;
|
|
627
|
+
customTools?: any[];
|
|
628
|
+
allowDefaultTools?: string[];
|
|
629
|
+
memory?: WebMemoryManager;
|
|
630
|
+
maxLoopCount?: number;
|
|
631
|
+
storageKey?: string;
|
|
632
|
+
}
|
|
633
|
+
/**
|
|
634
|
+
* 快速创建 Web 端 Agent 的工厂方法
|
|
635
|
+
*/
|
|
636
|
+
declare function createWebAgent(options: WebAgentOptions): ClineAgent;
|
|
637
|
+
|
|
638
|
+
declare const VERSION = "0.1.0";
|
|
639
|
+
|
|
640
|
+
export { AgentDefaultTool, type AgentMessage, type AgentOptions, AgentState, type ApiProviderInfo, type ApiStreamChunk, type AssistantMessageContent, ClineAgent, type IAgentRuntime, type IApiHandler, type IMcpManager, type IMemoryManager, type IPromptManager, type McpServerInfo, type McpTool, type McpToolCallResult, NodeApiHandler, type NodeApiHandlerConfig, NodeMemoryManager, NodePromptManager, READ_ONLY_TOOLS, type ReasoningStreamContent, TOOL_RESULT_BLOCK, type TextStreamContent, type ToolParamName, type ToolUse, VERSION, WebApiHandler, type WebApiHandlerConfig, WebMemoryManager, WebPromptManager, createNodeAgent, createWebAgent, defaultTools, getToolUseNames, parseAssistantMessageV2, setDynamicToolUseNames, toolParamNames, toolUseNames };
|