pulse-coder-engine 0.0.1-alpha.2 → 0.0.1-alpha.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/built-in/index.cjs +1327 -0
- package/dist/built-in/index.cjs.map +1 -0
- package/dist/built-in/index.d.cts +3 -0
- package/dist/built-in/index.d.ts +3 -0
- package/dist/built-in/index.js +1286 -0
- package/dist/built-in/index.js.map +1 -0
- package/dist/index-DDqISE31.d.cts +120 -0
- package/dist/index-DDqISE31.d.ts +120 -0
- package/dist/index.cjs +1874 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +627 -0
- package/dist/index.d.ts +148 -119
- package/dist/index.js +78 -30
- package/dist/index.js.map +1 -1
- package/package.json +8 -5
|
@@ -0,0 +1,1327 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/built-in/index.ts
|
|
31
|
+
var built_in_exports = {};
|
|
32
|
+
__export(built_in_exports, {
|
|
33
|
+
BuiltInSkillRegistry: () => BuiltInSkillRegistry,
|
|
34
|
+
SubAgentPlugin: () => SubAgentPlugin,
|
|
35
|
+
builtInMCPPlugin: () => builtInMCPPlugin,
|
|
36
|
+
builtInPlugins: () => builtInPlugins,
|
|
37
|
+
builtInSkillsPlugin: () => builtInSkillsPlugin,
|
|
38
|
+
default: () => built_in_default
|
|
39
|
+
});
|
|
40
|
+
module.exports = __toCommonJS(built_in_exports);
|
|
41
|
+
|
|
42
|
+
// src/built-in/mcp-plugin/index.ts
|
|
43
|
+
var import_mcp = require("@ai-sdk/mcp");
|
|
44
|
+
var import_fs = require("fs");
|
|
45
|
+
var path = __toESM(require("path"), 1);
|
|
46
|
+
async function loadMCPConfig(cwd) {
|
|
47
|
+
const newConfigPath = path.join(cwd, ".pulse-coder", "mcp.json");
|
|
48
|
+
const legacyConfigPath = path.join(cwd, ".coder", "mcp.json");
|
|
49
|
+
const configPath = (0, import_fs.existsSync)(newConfigPath) ? newConfigPath : legacyConfigPath;
|
|
50
|
+
if (!(0, import_fs.existsSync)(configPath)) {
|
|
51
|
+
return { servers: {} };
|
|
52
|
+
}
|
|
53
|
+
try {
|
|
54
|
+
const content = (0, import_fs.readFileSync)(configPath, "utf-8");
|
|
55
|
+
const parsed = JSON.parse(content);
|
|
56
|
+
if (!parsed.servers || typeof parsed.servers !== "object") {
|
|
57
|
+
console.warn('[MCP] Invalid config: missing "servers" object');
|
|
58
|
+
return { servers: {} };
|
|
59
|
+
}
|
|
60
|
+
return { servers: parsed.servers };
|
|
61
|
+
} catch (error) {
|
|
62
|
+
console.warn(`[MCP] Failed to load config: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
63
|
+
return { servers: {} };
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
function createHTTPTransport(config) {
|
|
67
|
+
return {
|
|
68
|
+
type: "http",
|
|
69
|
+
url: config.url
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
var builtInMCPPlugin = {
|
|
73
|
+
name: "pulse-coder-engine/built-in-mcp",
|
|
74
|
+
version: "1.0.0",
|
|
75
|
+
async initialize(context) {
|
|
76
|
+
const config = await loadMCPConfig(process.cwd());
|
|
77
|
+
const serverCount = Object.keys(config.servers).length;
|
|
78
|
+
if (serverCount === 0) {
|
|
79
|
+
console.log("[MCP] No MCP servers configured");
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
let loadedCount = 0;
|
|
83
|
+
for (const [serverName, serverConfig] of Object.entries(config.servers)) {
|
|
84
|
+
try {
|
|
85
|
+
if (!serverConfig.url) {
|
|
86
|
+
console.warn(`[MCP] Server "${serverName}" missing URL, skipping`);
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
const transport = createHTTPTransport(serverConfig);
|
|
90
|
+
const client = await (0, import_mcp.createMCPClient)({ transport });
|
|
91
|
+
const tools = await client.tools();
|
|
92
|
+
const namespacedTools = Object.fromEntries(
|
|
93
|
+
Object.entries(tools).map(([toolName, tool2]) => [
|
|
94
|
+
`mcp_${serverName}_${toolName}`,
|
|
95
|
+
tool2
|
|
96
|
+
])
|
|
97
|
+
);
|
|
98
|
+
context.registerTools(namespacedTools);
|
|
99
|
+
loadedCount++;
|
|
100
|
+
console.log(`[MCP] Server "${serverName}" loaded (${Object.keys(tools).length} tools)`);
|
|
101
|
+
context.registerService(`mcp:${serverName}`, client);
|
|
102
|
+
} catch (error) {
|
|
103
|
+
console.warn(`[MCP] Failed to load server "${serverName}": ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
if (loadedCount > 0) {
|
|
107
|
+
console.log(`[MCP] Successfully loaded ${loadedCount}/${serverCount} MCP servers`);
|
|
108
|
+
} else {
|
|
109
|
+
console.warn("[MCP] No MCP servers were loaded successfully");
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
// src/built-in/skills-plugin/index.ts
|
|
115
|
+
var import_fs2 = require("fs");
|
|
116
|
+
var import_glob = require("glob");
|
|
117
|
+
var import_gray_matter = __toESM(require("gray-matter"), 1);
|
|
118
|
+
var import_os = require("os");
|
|
119
|
+
var import_zod = require("zod");
|
|
120
|
+
var BuiltInSkillRegistry = class {
|
|
121
|
+
skills = /* @__PURE__ */ new Map();
|
|
122
|
+
initialized = false;
|
|
123
|
+
/**
|
|
124
|
+
* 初始化注册表,扫描并加载所有技能
|
|
125
|
+
*/
|
|
126
|
+
async initialize(cwd) {
|
|
127
|
+
if (this.initialized) {
|
|
128
|
+
console.warn("SkillRegistry already initialized");
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
console.log("Scanning built-in skills...");
|
|
132
|
+
const skillList = await this.scanSkills(cwd);
|
|
133
|
+
this.skills.clear();
|
|
134
|
+
for (const skill of skillList) {
|
|
135
|
+
this.skills.set(skill.name, skill);
|
|
136
|
+
}
|
|
137
|
+
this.initialized = true;
|
|
138
|
+
console.log(`Loaded ${this.skills.size} built-in skill(s)`);
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* 扫描技能文件
|
|
142
|
+
*/
|
|
143
|
+
async scanSkills(cwd) {
|
|
144
|
+
const skills = [];
|
|
145
|
+
const scanPaths = [
|
|
146
|
+
// 项目级技能(优先 .pulse-coder,兼容旧版 .coder 和 .claude)
|
|
147
|
+
{ base: cwd, pattern: ".pulse-coder/skills/**/SKILL.md" },
|
|
148
|
+
{ base: cwd, pattern: ".coder/skills/**/SKILL.md" },
|
|
149
|
+
{ base: cwd, pattern: ".claude/skills/**/SKILL.md" },
|
|
150
|
+
// 用户级技能(优先 .pulse-coder,兼容旧版 .coder)
|
|
151
|
+
{ base: (0, import_os.homedir)(), pattern: ".pulse-coder/skills/**/SKILL.md" },
|
|
152
|
+
{ base: (0, import_os.homedir)(), pattern: ".coder/skills/**/SKILL.md" }
|
|
153
|
+
];
|
|
154
|
+
for (const { base, pattern } of scanPaths) {
|
|
155
|
+
try {
|
|
156
|
+
const files = (0, import_glob.globSync)(pattern, { cwd: base, absolute: true });
|
|
157
|
+
for (const filePath of files) {
|
|
158
|
+
try {
|
|
159
|
+
const skillInfo = this.parseSkillFile(filePath);
|
|
160
|
+
if (skillInfo) {
|
|
161
|
+
skills.push(skillInfo);
|
|
162
|
+
}
|
|
163
|
+
} catch (error) {
|
|
164
|
+
console.warn(`Failed to parse skill file ${filePath}:`, error);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
} catch (error) {
|
|
168
|
+
console.debug(`Skip scanning ${pattern} in ${base}:`, error);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return skills;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* 解析技能文件
|
|
175
|
+
*/
|
|
176
|
+
parseSkillFile(filePath) {
|
|
177
|
+
try {
|
|
178
|
+
const content = (0, import_fs2.readFileSync)(filePath, "utf-8");
|
|
179
|
+
const { data, content: markdownContent } = (0, import_gray_matter.default)(content);
|
|
180
|
+
if (!data.name || !data.description) {
|
|
181
|
+
console.warn(`Skill file ${filePath} missing required fields (name or description)`);
|
|
182
|
+
return null;
|
|
183
|
+
}
|
|
184
|
+
return {
|
|
185
|
+
name: data.name,
|
|
186
|
+
description: data.description,
|
|
187
|
+
location: filePath,
|
|
188
|
+
content: markdownContent,
|
|
189
|
+
metadata: data
|
|
190
|
+
};
|
|
191
|
+
} catch (error) {
|
|
192
|
+
console.warn(`Failed to read skill file ${filePath}:`, error);
|
|
193
|
+
return null;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* 获取所有技能
|
|
198
|
+
*/
|
|
199
|
+
getAll() {
|
|
200
|
+
return Array.from(this.skills.values());
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* 根据名称获取技能
|
|
204
|
+
*/
|
|
205
|
+
get(name) {
|
|
206
|
+
return this.skills.get(name);
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* 检查技能是否存在
|
|
210
|
+
*/
|
|
211
|
+
has(name) {
|
|
212
|
+
return this.skills.has(name);
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* 搜索技能(模糊匹配)
|
|
216
|
+
*/
|
|
217
|
+
search(keyword) {
|
|
218
|
+
const lowerKeyword = keyword.toLowerCase();
|
|
219
|
+
return this.getAll().filter(
|
|
220
|
+
(skill) => skill.name.toLowerCase().includes(lowerKeyword) || skill.description.toLowerCase().includes(lowerKeyword)
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
var skillToolSchema = import_zod.z.object({
|
|
225
|
+
name: import_zod.z.string().describe("The name of the skill to execute")
|
|
226
|
+
});
|
|
227
|
+
function generateSkillTool(skills) {
|
|
228
|
+
const getSkillsPrompt = (availableSkills) => {
|
|
229
|
+
return [
|
|
230
|
+
"If query matches an available skill's description or instruction [use skill], use the skill tool to get detailed instructions.",
|
|
231
|
+
"Load a skill to get detailed instructions for a specific task.",
|
|
232
|
+
"Skills provide specialized knowledge and step-by-step guidance.",
|
|
233
|
+
"Use this when a task matches an available skill's description.",
|
|
234
|
+
"Only the skills listed here are available:",
|
|
235
|
+
"[!important] You should follow the skill's step-by-step guidance. If the skill is not complete, ask the user for more information.",
|
|
236
|
+
"<available_skills>",
|
|
237
|
+
...availableSkills.flatMap((skill) => [
|
|
238
|
+
` <skill>`,
|
|
239
|
+
` <name>${skill.name}</name>`,
|
|
240
|
+
` <description>${skill.description}</description>`,
|
|
241
|
+
` </skill>`
|
|
242
|
+
]),
|
|
243
|
+
"</available_skills>"
|
|
244
|
+
].join(" ");
|
|
245
|
+
};
|
|
246
|
+
return {
|
|
247
|
+
name: "skill",
|
|
248
|
+
description: getSkillsPrompt(skills),
|
|
249
|
+
inputSchema: skillToolSchema,
|
|
250
|
+
execute: async ({ name }) => {
|
|
251
|
+
const skill = skills.find((skill2) => skill2.name === name);
|
|
252
|
+
if (!skill) {
|
|
253
|
+
throw new Error(`Skill ${name} not found`);
|
|
254
|
+
}
|
|
255
|
+
return skill;
|
|
256
|
+
}
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
var builtInSkillsPlugin = {
|
|
260
|
+
name: "pulse-coder-engine/built-in-skills",
|
|
261
|
+
version: "1.0.0",
|
|
262
|
+
async initialize(context) {
|
|
263
|
+
const registry = new BuiltInSkillRegistry();
|
|
264
|
+
await registry.initialize(process.cwd());
|
|
265
|
+
const skills = registry.getAll();
|
|
266
|
+
if (skills.length === 0) {
|
|
267
|
+
console.log("[Skills] No skills found");
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
const skillTool = generateSkillTool(skills);
|
|
271
|
+
context.registerTool("skill", skillTool);
|
|
272
|
+
context.registerService("skillRegistry", registry);
|
|
273
|
+
console.log(`[Skills] Registered ${skills.length} skill(s)`);
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
// src/built-in/sub-agent-plugin/index.ts
|
|
278
|
+
var import_zod10 = require("zod");
|
|
279
|
+
var import_fs8 = require("fs");
|
|
280
|
+
var import_path2 = __toESM(require("path"), 1);
|
|
281
|
+
|
|
282
|
+
// src/ai/index.ts
|
|
283
|
+
var import_ai = require("ai");
|
|
284
|
+
|
|
285
|
+
// src/config/index.ts
|
|
286
|
+
var import_dotenv = __toESM(require("dotenv"), 1);
|
|
287
|
+
var import_openai = require("@ai-sdk/openai");
|
|
288
|
+
var import_anthropic = require("@ai-sdk/anthropic");
|
|
289
|
+
import_dotenv.default.config();
|
|
290
|
+
var CoderAI = process.env.USE_ANTHROPIC ? (0, import_anthropic.createAnthropic)({
|
|
291
|
+
apiKey: process.env.ANTHROPIC_API_KEY || "",
|
|
292
|
+
baseURL: process.env.ANTHROPIC_API_URL || "https://api.anthropic.com/v1"
|
|
293
|
+
}) : (0, import_openai.createOpenAI)({
|
|
294
|
+
apiKey: process.env.OPENAI_API_KEY || "",
|
|
295
|
+
baseURL: process.env.OPENAI_API_URL || "https://api.openai.com/v1"
|
|
296
|
+
}).responses;
|
|
297
|
+
var DEFAULT_MODEL = process.env.ANTHROPIC_MODEL || process.env.OPENAI_MODEL || "novita/deepseek/deepseek_v3";
|
|
298
|
+
var MAX_ERROR_COUNT = 3;
|
|
299
|
+
var MAX_STEPS = 100;
|
|
300
|
+
var MAX_TOOL_OUTPUT_LENGTH = 3e4;
|
|
301
|
+
var CONTEXT_WINDOW_TOKENS = Number(process.env.CONTEXT_WINDOW_TOKENS ?? 64e3);
|
|
302
|
+
var COMPACT_TRIGGER = Number(process.env.COMPACT_TRIGGER ?? Math.floor(CONTEXT_WINDOW_TOKENS * 0.75));
|
|
303
|
+
var COMPACT_TARGET = Number(process.env.COMPACT_TARGET ?? Math.floor(CONTEXT_WINDOW_TOKENS * 0.5));
|
|
304
|
+
var KEEP_LAST_TURNS = Number(process.env.KEEP_LAST_TURNS ?? 6);
|
|
305
|
+
var COMPACT_SUMMARY_MAX_TOKENS = Number(process.env.COMPACT_SUMMARY_MAX_TOKENS ?? 1200);
|
|
306
|
+
var MAX_COMPACTION_ATTEMPTS = Number(process.env.MAX_COMPACTION_ATTEMPTS ?? 2);
|
|
307
|
+
var OPENAI_REASONING_EFFORT = process.env.OPENAI_REASONING_EFFORT;
|
|
308
|
+
var CLARIFICATION_TIMEOUT = Number(process.env.CLARIFICATION_TIMEOUT ?? 3e5);
|
|
309
|
+
var CLARIFICATION_ENABLED = process.env.CLARIFICATION_ENABLED !== "false";
|
|
310
|
+
|
|
311
|
+
// src/prompt/system.ts
|
|
312
|
+
var generateSystemPrompt = () => {
|
|
313
|
+
const basePrompt = `
|
|
314
|
+
You are Pulse Coder, the best coding agent on the planet.
|
|
315
|
+
|
|
316
|
+
You are an interactive CLI tool that helps users with software engineering tasks. Use the instructions below and the tools available to you to assist the user.
|
|
317
|
+
|
|
318
|
+
## Editing constraints
|
|
319
|
+
- Default to ASCII when editing or creating files. Only introduce non-ASCII or other Unicode characters when there is a clear justification and the file already uses them.
|
|
320
|
+
- Only add comments if they are necessary to make a non-obvious block easier to understand.
|
|
321
|
+
- Try to use apply_patch for single file edits, but it is fine to explore other options to make the edit if it does not work well. Do not use apply_patch for changes that are auto-generated (i.e. generating package.json or running a lint or format command like gofmt) or when scripting is more efficient (such as search and replacing a string across a codebase).
|
|
322
|
+
|
|
323
|
+
## Skills
|
|
324
|
+
- If query matches an available skill's description or instruction [use skill], use the skill tool to get detailed instructions.
|
|
325
|
+
- You should Load a skill to get detailed instructions for a specific task. It always is a complex task that requires multiple steps.
|
|
326
|
+
- You should check the skill is complete and follow the step-by-step guidance. If the skill is not complete, you should ask the user for more information.
|
|
327
|
+
|
|
328
|
+
## Tool usage
|
|
329
|
+
- Prefer specialized tools over shell for file operations:
|
|
330
|
+
- Use Read to view files, Edit to modify files, and Write only when needed.
|
|
331
|
+
- Use Glob to find files by name and Grep to search file contents.
|
|
332
|
+
- Use Bash for terminal operations (git, bun, builds, tests, running scripts).
|
|
333
|
+
- Run tool calls in parallel when neither call needs the other\u2019s output; otherwise run sequentially.
|
|
334
|
+
|
|
335
|
+
## Git and workspace hygiene
|
|
336
|
+
- You may be in a dirty git worktree.
|
|
337
|
+
* NEVER revert existing changes you did not make unless explicitly requested, since these changes were made by the user.
|
|
338
|
+
* If asked to make a commit or code edits and there are unrelated changes to your work or changes that you didn't make in those files, don't revert those changes.
|
|
339
|
+
* If the changes are in files you've touched recently, you should read carefully and understand how you can work with the changes rather than reverting them.
|
|
340
|
+
* If the changes are in unrelated files, just ignore them and don't revert them.
|
|
341
|
+
- Do not amend commits unless explicitly requested.
|
|
342
|
+
- **NEVER** use destructive commands like \`git reset --hard\` or \`git checkout--\` unless specifically requested or approved by the user.
|
|
343
|
+
|
|
344
|
+
## Frontend tasks
|
|
345
|
+
When doing frontend design tasks, avoid collapsing into bland, generic layouts.
|
|
346
|
+
Aim for interfaces that feel intentional and deliberate.
|
|
347
|
+
- Typography: Use expressive, purposeful fonts and avoid default stacks (Inter, Roboto, Arial, system).
|
|
348
|
+
- Color & Look: Choose a clear visual direction; define CSS variables; avoid purple-on-white defaults. No purple bias or dark mode bias.
|
|
349
|
+
- Motion: Use a few meaningful animations (page-load, staggered reveals) instead of generic micro-motions.
|
|
350
|
+
- Background: Don't rely on flat, single-color backgrounds; use gradients, shapes, or subtle patterns to build atmosphere.
|
|
351
|
+
- Overall: Avoid boilerplate layouts and interchangeable UI patterns. Vary themes, type families, and visual languages across outputs.
|
|
352
|
+
- Ensure the page loads properly on both desktop and mobile.
|
|
353
|
+
|
|
354
|
+
Exception: If working within an existing website or design system, preserve the established patterns, structure, and visual language.
|
|
355
|
+
|
|
356
|
+
## Presenting your work and final message
|
|
357
|
+
|
|
358
|
+
You are producing plain text that will later be styled by the CLI. Follow these rules exactly. Formatting should make results easy to scan, but not feel mechanical. Use judgment to decide how much structure adds value.
|
|
359
|
+
|
|
360
|
+
- Default: be very concise; friendly coding teammate tone.
|
|
361
|
+
- Default: do the work without asking questions. Treat short tasks as sufficient direction; infer missing details by reading the codebase and following existing conventions.
|
|
362
|
+
- Questions: only ask when you are truly blocked after checking relevant context AND you cannot safely pick a reasonable default. This usually means one of:
|
|
363
|
+
* The request is ambiguous in a way that materially changes the result and you cannot disambiguate by reading the repo.
|
|
364
|
+
* The action is destructive/irreversible, touches production, or changes billing/security posture.
|
|
365
|
+
* You need a secret/credential/value that cannot be inferred (API key, account id, etc.).
|
|
366
|
+
- If you must ask: do all non-blocked work first, then ask exactly one targeted question, include your recommended default, and state what would change based on the answer.
|
|
367
|
+
- Never ask permission questions like "Should I proceed?" or "Do you want me to run tests?"; proceed with the most reasonable option and mention what you did.
|
|
368
|
+
|
|
369
|
+
## Clarification Tool
|
|
370
|
+
|
|
371
|
+
Use the 'clarify' tool when you genuinely need information from the user to proceed. This tool pauses execution and waits for user input.
|
|
372
|
+
|
|
373
|
+
**When to use clarify:**
|
|
374
|
+
- The request is ambiguous in a way that materially affects the implementation and cannot be resolved by reading the codebase
|
|
375
|
+
- You cannot safely infer the answer from existing code, conventions, or context
|
|
376
|
+
- You need confirmation before destructive or irreversible actions (e.g., deleting resources, modifying production data)
|
|
377
|
+
- You need specific values that cannot be guessed (API keys, account IDs, specific user choices between valid alternatives)
|
|
378
|
+
|
|
379
|
+
**When NOT to use clarify:**
|
|
380
|
+
- For trivial decisions you can make based on codebase conventions or common practices
|
|
381
|
+
- For permission questions like "Should I proceed?" (just proceed with the best option)
|
|
382
|
+
- For information that's likely in the codebase, configuration files, or documentation (read those first)
|
|
383
|
+
- Multiple times in a row - complete all non-blocked work first, then ask one clear question
|
|
384
|
+
- For choices where a reasonable default exists (use the default and mention what you chose)
|
|
385
|
+
|
|
386
|
+
**How to use clarify:**
|
|
387
|
+
- Ask ONE clear, specific question per clarification
|
|
388
|
+
- Provide context if needed to help the user understand the choice
|
|
389
|
+
- Include a recommended default answer when applicable
|
|
390
|
+
- Explain briefly what would change based on the answer
|
|
391
|
+
|
|
392
|
+
Example usage: Call clarify with a question, optional context, and optional default answer. The tool will pause and wait for the user's response.
|
|
393
|
+
- For substantial work, summarize clearly; follow final\u2011answer formatting.
|
|
394
|
+
- Skip heavy formatting for simple confirmations.
|
|
395
|
+
- Don't dump large files you've written; reference paths only.
|
|
396
|
+
- No "save/copy this file" - User is on the same machine.
|
|
397
|
+
- Offer logical next steps (tests, commits, build) briefly; add verify steps if you couldn't do something.
|
|
398
|
+
- For code changes:
|
|
399
|
+
* Lead with a quick explanation of the change, and then give more details on the context covering where and why a change was made. Do not start this explanation with "summary", just jump right in.
|
|
400
|
+
* If there are natural next steps the user may want to take, suggest them at the end of your response. Do not make suggestions if there are no natural next steps.
|
|
401
|
+
* When suggesting multiple options, use numeric lists for the suggestions so the user can quickly respond with a single number.
|
|
402
|
+
- The user does not command execution outputs. When asked to show the output of a command (e.g. \`git show\`), relay the important details in your answer or summarize the key lines so the user understands the result.
|
|
403
|
+
|
|
404
|
+
## Final answer structure and style guidelines
|
|
405
|
+
|
|
406
|
+
- Plain text; CLI handles styling. Use structure only when it helps scanability.
|
|
407
|
+
- Headers: optional; short Title Case (1-3 words) wrapped in **\u2026**; no blank line before the first bullet; add only if they truly help.
|
|
408
|
+
- Bullets: use - ; merge related points; keep to one line when possible; 4\u20136 per list ordered by importance; keep phrasing consistent.
|
|
409
|
+
- Monospace: backticks for commands/paths/env vars/code ids and inline examples; use for literal keyword bullets; never combine with **.
|
|
410
|
+
- Code samples or multi-line snippets should be wrapped in fenced code blocks; include an info string as often as possible.
|
|
411
|
+
- Structure: group related bullets; order sections general \u2192 specific \u2192 supporting; for subsections, start with a bolded keyword bullet, then items; match complexity to the task.
|
|
412
|
+
- Tone: collaborative, concise, factual; present tense, active voice; self\u2011contained; no "above/below"; parallel wording.
|
|
413
|
+
- Don'ts: no nested bullets/hierarchies; no ANSI codes; don't cram unrelated keywords; keep keyword lists short\u2014wrap/reformat if long; avoid naming formatting styles in answers.
|
|
414
|
+
- Adaptation: code explanations \u2192 precise, structured with code refs; simple tasks \u2192 lead with outcome; big changes \u2192 logical walkthrough + rationale + next actions; casual one-offs \u2192 plain sentences, no headers/bullets.
|
|
415
|
+
- File References: When referencing files in your response follow the below rules:
|
|
416
|
+
* Use inline code to make file paths clickable.
|
|
417
|
+
* Each reference should have a stand alone path. Even if it's the same file.
|
|
418
|
+
* Accepted: absolute, workspace\u2011relative, a/ or b/ diff prefixes, or bare filename/suffix.
|
|
419
|
+
* Optionally include line/column (1\u2011based): :line[:column] or #Lline[Ccolumn] (column defaults to 1).
|
|
420
|
+
* Do not use URIs like file://, vscode://, or https://.
|
|
421
|
+
* Do not provide range of lines
|
|
422
|
+
* Examples: src/app.ts, src/app.ts:42, b/server/index.js#L10, C:\repoprojectmain.rs:12:5
|
|
423
|
+
|
|
424
|
+
Here is some useful information about the environment you are running in:
|
|
425
|
+
<env>
|
|
426
|
+
Working directory: ${process.cwd()}
|
|
427
|
+
Platform: darwin
|
|
428
|
+
Today's date: ${(/* @__PURE__ */ new Date()).toLocaleDateString()}
|
|
429
|
+
</env>
|
|
430
|
+
<files>
|
|
431
|
+
|
|
432
|
+
</files>`;
|
|
433
|
+
return basePrompt;
|
|
434
|
+
};
|
|
435
|
+
|
|
436
|
+
// src/ai/index.ts
|
|
437
|
+
var providerOptions = { openai: { store: false, reasoningEffort: OPENAI_REASONING_EFFORT } };
|
|
438
|
+
var resolveSystemPrompt = (option) => {
|
|
439
|
+
const base = generateSystemPrompt();
|
|
440
|
+
if (!option) return base;
|
|
441
|
+
if (typeof option === "string") return option;
|
|
442
|
+
if (typeof option === "function") return option();
|
|
443
|
+
return `${base}
|
|
444
|
+
|
|
445
|
+
${option.append}`;
|
|
446
|
+
};
|
|
447
|
+
var wrapToolsWithContext = (tools, context) => {
|
|
448
|
+
const wrappedTools = {};
|
|
449
|
+
for (const [name, tool2] of Object.entries(tools)) {
|
|
450
|
+
wrappedTools[name] = {
|
|
451
|
+
...tool2,
|
|
452
|
+
execute: async (input) => {
|
|
453
|
+
return await tool2.execute(input, context);
|
|
454
|
+
}
|
|
455
|
+
};
|
|
456
|
+
}
|
|
457
|
+
return wrappedTools;
|
|
458
|
+
};
|
|
459
|
+
var streamTextAI = (messages, tools, options) => {
|
|
460
|
+
const provider = options?.provider ?? CoderAI;
|
|
461
|
+
const model = options?.model ?? DEFAULT_MODEL;
|
|
462
|
+
const wrappedTools = options?.toolExecutionContext ? wrapToolsWithContext(tools, options.toolExecutionContext) : tools;
|
|
463
|
+
return (0, import_ai.streamText)({
|
|
464
|
+
model: provider(model),
|
|
465
|
+
system: resolveSystemPrompt(options?.systemPrompt),
|
|
466
|
+
messages,
|
|
467
|
+
tools: wrappedTools,
|
|
468
|
+
providerOptions,
|
|
469
|
+
abortSignal: options?.abortSignal,
|
|
470
|
+
onStepFinish: options?.onStepFinish,
|
|
471
|
+
onChunk: options?.onChunk
|
|
472
|
+
});
|
|
473
|
+
};
|
|
474
|
+
var summarizeMessages = async (messages, options) => {
|
|
475
|
+
const provider = options?.provider ?? CoderAI;
|
|
476
|
+
const model = options?.model ?? DEFAULT_MODEL;
|
|
477
|
+
const SUMMARY_SYSTEM_PROMPT = "\u4F60\u662F\u8D1F\u8D23\u538B\u7F29\u5BF9\u8BDD\u4E0A\u4E0B\u6587\u7684\u52A9\u624B\u3002\u8BF7\u57FA\u4E8E\u7ED9\u5B9A\u5386\u53F2\u6D88\u606F\uFF0C\u63D0\u70BC\u5173\u952E\u4E8B\u5B9E\u4E0E\u51B3\u7B56\uFF0C\u907F\u514D\u81C6\u6D4B\u6216\u6269\u5C55\u3002";
|
|
478
|
+
const SUMMARY_USER_PROMPT = [
|
|
479
|
+
"\u8BF7\u5C06\u4EE5\u4E0A\u5BF9\u8BDD\u538B\u7F29\u4E3A\u4EE5\u4E0B\u683C\u5F0F\uFF0C\u4F7F\u7528\u4E2D\u6587\uFF1A",
|
|
480
|
+
"[COMPACTED_CONTEXT]",
|
|
481
|
+
"- \u76EE\u6807: ...",
|
|
482
|
+
"- \u8FDB\u5C55: ...",
|
|
483
|
+
"- \u5173\u952E\u7ED3\u679C: ...",
|
|
484
|
+
"- \u6587\u4EF6\u4E0E\u53D8\u66F4: ...",
|
|
485
|
+
'- \u5173\u952E\u7247\u6BB5: "..." / `...` / "..."',
|
|
486
|
+
"- \u5F85\u786E\u8BA4: ...",
|
|
487
|
+
"",
|
|
488
|
+
"\u8981\u6C42\uFF1A\u5185\u5BB9\u7B80\u6D01\u51C6\u786E\uFF0C\u4E0D\u8981\u7F16\u9020\u3002"
|
|
489
|
+
].join("\n");
|
|
490
|
+
const result = await (0, import_ai.generateText)({
|
|
491
|
+
model: provider(model),
|
|
492
|
+
system: SUMMARY_SYSTEM_PROMPT,
|
|
493
|
+
messages: [
|
|
494
|
+
...messages,
|
|
495
|
+
{ role: "user", content: SUMMARY_USER_PROMPT }
|
|
496
|
+
],
|
|
497
|
+
maxOutputTokens: options?.maxOutputTokens ?? COMPACT_SUMMARY_MAX_TOKENS,
|
|
498
|
+
providerOptions
|
|
499
|
+
});
|
|
500
|
+
return result.text ?? "";
|
|
501
|
+
};
|
|
502
|
+
|
|
503
|
+
// src/context/index.ts
|
|
504
|
+
var import_ai2 = require("ai");
|
|
505
|
+
var ensureSummaryPrefix = (summary) => {
|
|
506
|
+
const trimmed = summary.trim();
|
|
507
|
+
if (trimmed.length === 0) {
|
|
508
|
+
return "";
|
|
509
|
+
}
|
|
510
|
+
if (trimmed.startsWith("[COMPACTED_CONTEXT]")) {
|
|
511
|
+
return trimmed;
|
|
512
|
+
}
|
|
513
|
+
return `[COMPACTED_CONTEXT]
|
|
514
|
+
${trimmed}`;
|
|
515
|
+
};
|
|
516
|
+
var safeStringify = (value) => {
|
|
517
|
+
try {
|
|
518
|
+
return JSON.stringify(value);
|
|
519
|
+
} catch {
|
|
520
|
+
return String(value);
|
|
521
|
+
}
|
|
522
|
+
};
|
|
523
|
+
var estimateTokens = (messages) => {
|
|
524
|
+
let totalChars = 0;
|
|
525
|
+
for (const message of messages) {
|
|
526
|
+
totalChars += message.role.length;
|
|
527
|
+
if (typeof message.content === "string") {
|
|
528
|
+
totalChars += message.content.length;
|
|
529
|
+
} else {
|
|
530
|
+
totalChars += safeStringify(message.content).length;
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
return Math.ceil(totalChars / 4);
|
|
534
|
+
};
|
|
535
|
+
var splitByTurns = (messages, keepLastTurns) => {
|
|
536
|
+
const userIndices = [];
|
|
537
|
+
messages.forEach((message, index) => {
|
|
538
|
+
if (message.role === "user") {
|
|
539
|
+
userIndices.push(index);
|
|
540
|
+
}
|
|
541
|
+
});
|
|
542
|
+
if (userIndices.length <= keepLastTurns) {
|
|
543
|
+
return { oldMessages: [], recentMessages: messages };
|
|
544
|
+
}
|
|
545
|
+
const cutIndex = userIndices[userIndices.length - keepLastTurns];
|
|
546
|
+
return {
|
|
547
|
+
oldMessages: messages.slice(0, cutIndex),
|
|
548
|
+
recentMessages: messages.slice(cutIndex)
|
|
549
|
+
};
|
|
550
|
+
};
|
|
551
|
+
var takeLastTurns = (messages, keepLastTurns) => {
|
|
552
|
+
const userIndices = [];
|
|
553
|
+
messages.forEach((message, index) => {
|
|
554
|
+
if (message.role === "user") {
|
|
555
|
+
userIndices.push(index);
|
|
556
|
+
}
|
|
557
|
+
});
|
|
558
|
+
if (userIndices.length === 0) {
|
|
559
|
+
return messages;
|
|
560
|
+
}
|
|
561
|
+
if (userIndices.length <= keepLastTurns) {
|
|
562
|
+
return messages;
|
|
563
|
+
}
|
|
564
|
+
const startIndex = userIndices[userIndices.length - keepLastTurns];
|
|
565
|
+
return messages.slice(startIndex);
|
|
566
|
+
};
|
|
567
|
+
var maybeCompactContext = async (context, options) => {
|
|
568
|
+
const { messages } = context;
|
|
569
|
+
if (messages.length === 0) {
|
|
570
|
+
return { didCompact: false };
|
|
571
|
+
}
|
|
572
|
+
const estimatedTokens = estimateTokens(messages);
|
|
573
|
+
if (!options?.force && estimatedTokens < COMPACT_TRIGGER) {
|
|
574
|
+
return { didCompact: false };
|
|
575
|
+
}
|
|
576
|
+
const { oldMessages, recentMessages } = splitByTurns(messages, KEEP_LAST_TURNS);
|
|
577
|
+
if (oldMessages.length === 0) {
|
|
578
|
+
return { didCompact: false };
|
|
579
|
+
}
|
|
580
|
+
try {
|
|
581
|
+
const summary = await summarizeMessages(oldMessages, {
|
|
582
|
+
provider: options?.provider,
|
|
583
|
+
model: options?.model
|
|
584
|
+
});
|
|
585
|
+
const summaryText = ensureSummaryPrefix(summary);
|
|
586
|
+
if (!summaryText) {
|
|
587
|
+
throw new Error("Empty summary result");
|
|
588
|
+
}
|
|
589
|
+
const nextMessages = [
|
|
590
|
+
{ role: "assistant", content: summaryText },
|
|
591
|
+
...recentMessages
|
|
592
|
+
];
|
|
593
|
+
if (estimateTokens(nextMessages) > COMPACT_TARGET) {
|
|
594
|
+
const newMessages = takeLastTurns(messages, KEEP_LAST_TURNS);
|
|
595
|
+
return { didCompact: true, reason: "summary-too-large", newMessages };
|
|
596
|
+
}
|
|
597
|
+
return { didCompact: true, newMessages: nextMessages };
|
|
598
|
+
} catch (error) {
|
|
599
|
+
const pruned = (0, import_ai2.pruneMessages)({
|
|
600
|
+
messages,
|
|
601
|
+
reasoning: "all",
|
|
602
|
+
toolCalls: "all",
|
|
603
|
+
emptyMessages: "remove"
|
|
604
|
+
});
|
|
605
|
+
const newMessages = takeLastTurns(pruned, KEEP_LAST_TURNS);
|
|
606
|
+
return { didCompact: true, reason: "fallback", newMessages };
|
|
607
|
+
}
|
|
608
|
+
};
|
|
609
|
+
|
|
610
|
+
// src/core/loop.ts
|
|
611
|
+
function applyToolHooks(tools, hooks) {
|
|
612
|
+
const wrapped = {};
|
|
613
|
+
for (const [name, t] of Object.entries(tools)) {
|
|
614
|
+
wrapped[name] = {
|
|
615
|
+
...t,
|
|
616
|
+
execute: async (input, ctx) => {
|
|
617
|
+
const finalInput = hooks.onBeforeToolCall ? await hooks.onBeforeToolCall(name, input) ?? input : input;
|
|
618
|
+
const output = await t.execute(finalInput, ctx);
|
|
619
|
+
return hooks.onAfterToolCall ? await hooks.onAfterToolCall(name, finalInput, output) ?? output : output;
|
|
620
|
+
}
|
|
621
|
+
};
|
|
622
|
+
}
|
|
623
|
+
return wrapped;
|
|
624
|
+
}
|
|
625
|
+
async function loop(context, options) {
|
|
626
|
+
let errorCount = 0;
|
|
627
|
+
let totalSteps = 0;
|
|
628
|
+
let compactionAttempts = 0;
|
|
629
|
+
while (true) {
|
|
630
|
+
try {
|
|
631
|
+
if (compactionAttempts < MAX_COMPACTION_ATTEMPTS) {
|
|
632
|
+
const { didCompact, newMessages } = await maybeCompactContext(context, {
|
|
633
|
+
provider: options?.provider,
|
|
634
|
+
model: options?.model
|
|
635
|
+
});
|
|
636
|
+
if (didCompact) {
|
|
637
|
+
compactionAttempts++;
|
|
638
|
+
if (newMessages) {
|
|
639
|
+
options?.onCompacted?.(newMessages);
|
|
640
|
+
}
|
|
641
|
+
continue;
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
let tools = options?.tools || {};
|
|
645
|
+
if (options?.hooks) {
|
|
646
|
+
tools = applyToolHooks(tools, options.hooks);
|
|
647
|
+
}
|
|
648
|
+
const toolExecutionContext = {
|
|
649
|
+
onClarificationRequest: options?.onClarificationRequest,
|
|
650
|
+
abortSignal: options?.abortSignal
|
|
651
|
+
};
|
|
652
|
+
const result = streamTextAI(context.messages, tools, {
|
|
653
|
+
abortSignal: options?.abortSignal,
|
|
654
|
+
toolExecutionContext,
|
|
655
|
+
provider: options?.provider,
|
|
656
|
+
model: options?.model,
|
|
657
|
+
systemPrompt: options?.systemPrompt,
|
|
658
|
+
onStepFinish: (step) => {
|
|
659
|
+
options?.onStepFinish?.(step);
|
|
660
|
+
},
|
|
661
|
+
onChunk: ({ chunk }) => {
|
|
662
|
+
if (chunk.type === "text-delta") {
|
|
663
|
+
options?.onText?.(chunk.text);
|
|
664
|
+
}
|
|
665
|
+
if (chunk.type === "tool-call") {
|
|
666
|
+
options?.onToolCall?.(chunk);
|
|
667
|
+
}
|
|
668
|
+
if (chunk.type === "tool-result") {
|
|
669
|
+
options?.onToolResult?.(chunk);
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
});
|
|
673
|
+
const [text, steps, finishReason] = await Promise.all([
|
|
674
|
+
result.text,
|
|
675
|
+
result.steps,
|
|
676
|
+
result.finishReason
|
|
677
|
+
]);
|
|
678
|
+
totalSteps += steps.length;
|
|
679
|
+
for (const step of steps) {
|
|
680
|
+
if (step.response?.messages?.length) {
|
|
681
|
+
const messages = [...step.response.messages];
|
|
682
|
+
options?.onResponse?.(messages);
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
if (finishReason === "stop") {
|
|
686
|
+
return text || "Task completed.";
|
|
687
|
+
}
|
|
688
|
+
if (finishReason === "length") {
|
|
689
|
+
if (compactionAttempts < MAX_COMPACTION_ATTEMPTS) {
|
|
690
|
+
const { didCompact, newMessages } = await maybeCompactContext(context, {
|
|
691
|
+
force: true,
|
|
692
|
+
provider: options?.provider,
|
|
693
|
+
model: options?.model
|
|
694
|
+
});
|
|
695
|
+
if (didCompact) {
|
|
696
|
+
compactionAttempts++;
|
|
697
|
+
if (newMessages) {
|
|
698
|
+
options?.onCompacted?.(newMessages);
|
|
699
|
+
}
|
|
700
|
+
continue;
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
return text || "Context limit reached.";
|
|
704
|
+
}
|
|
705
|
+
if (finishReason === "content-filter") {
|
|
706
|
+
return text || "Content filtered.";
|
|
707
|
+
}
|
|
708
|
+
if (finishReason === "error") {
|
|
709
|
+
return text || "Task failed.";
|
|
710
|
+
}
|
|
711
|
+
if (finishReason === "tool-calls") {
|
|
712
|
+
if (totalSteps >= MAX_STEPS) {
|
|
713
|
+
return text || "Max steps reached, task may be incomplete.";
|
|
714
|
+
}
|
|
715
|
+
continue;
|
|
716
|
+
}
|
|
717
|
+
return text || "Task completed.";
|
|
718
|
+
} catch (error) {
|
|
719
|
+
if (options?.abortSignal?.aborted || error?.name === "AbortError") {
|
|
720
|
+
return "Request aborted.";
|
|
721
|
+
}
|
|
722
|
+
errorCount++;
|
|
723
|
+
if (errorCount >= MAX_ERROR_COUNT) {
|
|
724
|
+
return `Failed after ${errorCount} errors: ${error?.message ?? String(error)}`;
|
|
725
|
+
}
|
|
726
|
+
if (isRetryableError(error)) {
|
|
727
|
+
const delay = Math.min(2e3 * Math.pow(2, errorCount - 1), 3e4);
|
|
728
|
+
await sleep(delay);
|
|
729
|
+
continue;
|
|
730
|
+
}
|
|
731
|
+
return `Error: ${error?.message ?? String(error)}`;
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
function isRetryableError(error) {
|
|
736
|
+
const status = error?.status ?? error?.statusCode;
|
|
737
|
+
return status === 429 || status === 500 || status === 502 || status === 503;
|
|
738
|
+
}
|
|
739
|
+
function sleep(ms) {
|
|
740
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
// src/tools/read.ts
|
|
744
|
+
var import_zod2 = __toESM(require("zod"), 1);
|
|
745
|
+
var import_fs3 = require("fs");
|
|
746
|
+
|
|
747
|
+
// src/tools/utils.ts
|
|
748
|
+
var truncateOutput = (output) => {
|
|
749
|
+
if (output.length <= MAX_TOOL_OUTPUT_LENGTH) {
|
|
750
|
+
return output;
|
|
751
|
+
}
|
|
752
|
+
const half = Math.floor(MAX_TOOL_OUTPUT_LENGTH / 2);
|
|
753
|
+
const removed = output.length - MAX_TOOL_OUTPUT_LENGTH;
|
|
754
|
+
return output.slice(0, half) + `
|
|
755
|
+
|
|
756
|
+
... [truncated ${removed} characters] ...
|
|
757
|
+
|
|
758
|
+
` + output.slice(-half);
|
|
759
|
+
};
|
|
760
|
+
|
|
761
|
+
// src/tools/read.ts
|
|
762
|
+
var ReadTool = {
|
|
763
|
+
name: "read",
|
|
764
|
+
description: "Read the contents of a file. Supports reading specific line ranges with offset and limit.",
|
|
765
|
+
inputSchema: import_zod2.default.object({
|
|
766
|
+
filePath: import_zod2.default.string().describe("The absolute path to the file to read"),
|
|
767
|
+
offset: import_zod2.default.number().optional().describe("The line number to start reading from (0-based). Only provide if the file is too large to read at once."),
|
|
768
|
+
limit: import_zod2.default.number().optional().describe("The number of lines to read. Only provide if the file is too large to read at once.")
|
|
769
|
+
}),
|
|
770
|
+
execute: async ({ filePath, offset, limit }) => {
|
|
771
|
+
if (!(0, import_fs3.existsSync)(filePath)) {
|
|
772
|
+
throw new Error(`File does not exist: ${filePath}`);
|
|
773
|
+
}
|
|
774
|
+
const stats = (0, import_fs3.statSync)(filePath);
|
|
775
|
+
if (stats.isDirectory()) {
|
|
776
|
+
throw new Error(`Cannot read directory: ${filePath}. Use 'ls' tool to list directory contents.`);
|
|
777
|
+
}
|
|
778
|
+
const content = (0, import_fs3.readFileSync)(filePath, "utf-8");
|
|
779
|
+
const lines = content.split("\n");
|
|
780
|
+
const totalLines = lines.length;
|
|
781
|
+
if (offset === void 0 && limit === void 0) {
|
|
782
|
+
return {
|
|
783
|
+
content: truncateOutput(content),
|
|
784
|
+
totalLines
|
|
785
|
+
};
|
|
786
|
+
}
|
|
787
|
+
const startLine = offset || 0;
|
|
788
|
+
const endLine = limit ? startLine + limit : lines.length;
|
|
789
|
+
if (startLine < 0 || startLine >= totalLines) {
|
|
790
|
+
throw new Error(`Invalid offset: ${startLine}. File has ${totalLines} lines.`);
|
|
791
|
+
}
|
|
792
|
+
const selectedLines = lines.slice(startLine, endLine);
|
|
793
|
+
const numberedContent = selectedLines.map((line, idx) => {
|
|
794
|
+
const lineNum = startLine + idx + 1;
|
|
795
|
+
return `${String(lineNum).padStart(6, " ")}\u2192${line}`;
|
|
796
|
+
}).join("\n");
|
|
797
|
+
return {
|
|
798
|
+
content: truncateOutput(numberedContent),
|
|
799
|
+
totalLines
|
|
800
|
+
};
|
|
801
|
+
}
|
|
802
|
+
};
|
|
803
|
+
|
|
804
|
+
// src/tools/write.ts
|
|
805
|
+
var import_zod3 = __toESM(require("zod"), 1);
|
|
806
|
+
var import_fs4 = require("fs");
|
|
807
|
+
var import_path = require("path");
|
|
808
|
+
var WriteTool = {
|
|
809
|
+
name: "write",
|
|
810
|
+
description: "Write contents to a file. Automatically creates parent directories if they do not exist. Will overwrite existing files.",
|
|
811
|
+
inputSchema: import_zod3.default.object({
|
|
812
|
+
filePath: import_zod3.default.string().describe("The absolute path to the file to write (must be absolute, not relative)"),
|
|
813
|
+
content: import_zod3.default.string().describe("The content to write to the file")
|
|
814
|
+
}),
|
|
815
|
+
execute: async ({ filePath, content }) => {
|
|
816
|
+
const fileExists = (0, import_fs4.existsSync)(filePath);
|
|
817
|
+
const dir = (0, import_path.dirname)(filePath);
|
|
818
|
+
if (!(0, import_fs4.existsSync)(dir)) {
|
|
819
|
+
(0, import_fs4.mkdirSync)(dir, { recursive: true });
|
|
820
|
+
}
|
|
821
|
+
(0, import_fs4.writeFileSync)(filePath, content, "utf-8");
|
|
822
|
+
const bytes = Buffer.byteLength(content, "utf-8");
|
|
823
|
+
return {
|
|
824
|
+
success: true,
|
|
825
|
+
created: !fileExists,
|
|
826
|
+
bytes
|
|
827
|
+
};
|
|
828
|
+
}
|
|
829
|
+
};
|
|
830
|
+
|
|
831
|
+
// src/tools/edit.ts
|
|
832
|
+
var import_zod4 = __toESM(require("zod"), 1);
|
|
833
|
+
var import_fs5 = require("fs");
|
|
834
|
+
var EditTool = {
|
|
835
|
+
name: "edit",
|
|
836
|
+
description: "Performs exact string replacements in files. Use this to edit existing files by replacing old_string with new_string.",
|
|
837
|
+
inputSchema: import_zod4.default.object({
|
|
838
|
+
filePath: import_zod4.default.string().describe("The absolute path to the file to modify"),
|
|
839
|
+
oldString: import_zod4.default.string().describe("The exact text to replace (must match exactly)"),
|
|
840
|
+
newString: import_zod4.default.string().describe("The text to replace it with (must be different from old_string)"),
|
|
841
|
+
replaceAll: import_zod4.default.boolean().optional().default(false).describe("Replace all occurrences of old_string (default false)")
|
|
842
|
+
}),
|
|
843
|
+
execute: async ({ filePath, oldString, newString, replaceAll = false }) => {
|
|
844
|
+
if (oldString === newString) {
|
|
845
|
+
throw new Error("old_string and new_string must be different");
|
|
846
|
+
}
|
|
847
|
+
const content = (0, import_fs5.readFileSync)(filePath, "utf-8");
|
|
848
|
+
if (!content.includes(oldString)) {
|
|
849
|
+
throw new Error(`old_string not found in file: ${filePath}`);
|
|
850
|
+
}
|
|
851
|
+
let newContent;
|
|
852
|
+
let replacements = 0;
|
|
853
|
+
if (replaceAll) {
|
|
854
|
+
const parts = content.split(oldString);
|
|
855
|
+
replacements = parts.length - 1;
|
|
856
|
+
newContent = parts.join(newString);
|
|
857
|
+
} else {
|
|
858
|
+
const index = content.indexOf(oldString);
|
|
859
|
+
if (index === -1) {
|
|
860
|
+
throw new Error(`old_string not found in file: ${filePath}`);
|
|
861
|
+
}
|
|
862
|
+
const secondIndex = content.indexOf(oldString, index + oldString.length);
|
|
863
|
+
if (secondIndex !== -1) {
|
|
864
|
+
throw new Error(
|
|
865
|
+
"old_string is not unique in the file. Either provide a larger string with more surrounding context to make it unique or use replace_all to change every instance."
|
|
866
|
+
);
|
|
867
|
+
}
|
|
868
|
+
newContent = content.slice(0, index) + newString + content.slice(index + oldString.length);
|
|
869
|
+
replacements = 1;
|
|
870
|
+
}
|
|
871
|
+
(0, import_fs5.writeFileSync)(filePath, newContent, "utf-8");
|
|
872
|
+
const changedIndex = newContent.indexOf(newString);
|
|
873
|
+
const contextLength = 200;
|
|
874
|
+
const start = Math.max(0, changedIndex - contextLength);
|
|
875
|
+
const end = Math.min(newContent.length, changedIndex + newString.length + contextLength);
|
|
876
|
+
const preview = truncateOutput(
|
|
877
|
+
`...${newContent.slice(start, end)}...`
|
|
878
|
+
);
|
|
879
|
+
return {
|
|
880
|
+
success: true,
|
|
881
|
+
replacements,
|
|
882
|
+
preview
|
|
883
|
+
};
|
|
884
|
+
}
|
|
885
|
+
};
|
|
886
|
+
|
|
887
|
+
// src/tools/grep.ts
|
|
888
|
+
var import_zod5 = __toESM(require("zod"), 1);
|
|
889
|
+
var import_child_process = require("child_process");
|
|
890
|
+
var import_fs6 = require("fs");
|
|
891
|
+
var GrepTool = {
|
|
892
|
+
name: "grep",
|
|
893
|
+
description: "A powerful search tool built on ripgrep. Supports regex patterns, file filtering, and multiple output modes.",
|
|
894
|
+
inputSchema: import_zod5.default.object({
|
|
895
|
+
pattern: import_zod5.default.string().describe("The regular expression pattern to search for in file contents"),
|
|
896
|
+
path: import_zod5.default.string().optional().describe("File or directory to search in. Defaults to current working directory."),
|
|
897
|
+
glob: import_zod5.default.string().optional().describe('Glob pattern to filter files (e.g. "*.js", "*.{ts,tsx}")'),
|
|
898
|
+
type: import_zod5.default.string().optional().describe("File type to search (e.g., js, py, rust, go, java, ts, tsx, json, md)"),
|
|
899
|
+
outputMode: import_zod5.default.enum(["content", "files_with_matches", "count"]).optional().default("files_with_matches").describe('Output mode: "content" shows matching lines, "files_with_matches" shows file paths, "count" shows match counts'),
|
|
900
|
+
context: import_zod5.default.number().optional().describe('Number of lines to show before and after each match (only with output_mode: "content")'),
|
|
901
|
+
caseInsensitive: import_zod5.default.boolean().optional().default(false).describe("Case insensitive search"),
|
|
902
|
+
headLimit: import_zod5.default.number().optional().default(0).describe("Limit output to first N lines/entries. 0 means unlimited."),
|
|
903
|
+
offset: import_zod5.default.number().optional().default(0).describe("Skip first N lines/entries before applying head_limit"),
|
|
904
|
+
multiline: import_zod5.default.boolean().optional().default(false).describe("Enable multiline mode where patterns can span lines")
|
|
905
|
+
}),
|
|
906
|
+
execute: async ({
|
|
907
|
+
pattern,
|
|
908
|
+
path: path3 = ".",
|
|
909
|
+
glob,
|
|
910
|
+
type,
|
|
911
|
+
outputMode = "files_with_matches",
|
|
912
|
+
context,
|
|
913
|
+
caseInsensitive = false,
|
|
914
|
+
headLimit = 0,
|
|
915
|
+
offset = 0,
|
|
916
|
+
multiline = false
|
|
917
|
+
}) => {
|
|
918
|
+
const args = ["rg"];
|
|
919
|
+
args.push(pattern);
|
|
920
|
+
if (caseInsensitive) {
|
|
921
|
+
args.push("-i");
|
|
922
|
+
}
|
|
923
|
+
if (outputMode === "files_with_matches") {
|
|
924
|
+
args.push("-l");
|
|
925
|
+
} else if (outputMode === "count") {
|
|
926
|
+
args.push("-c");
|
|
927
|
+
} else if (outputMode === "content") {
|
|
928
|
+
args.push("-n");
|
|
929
|
+
if (context !== void 0) {
|
|
930
|
+
args.push(`-C${context}`);
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
if (multiline) {
|
|
934
|
+
args.push("-U");
|
|
935
|
+
args.push("--multiline-dotall");
|
|
936
|
+
}
|
|
937
|
+
if (glob) {
|
|
938
|
+
args.push("--glob", glob);
|
|
939
|
+
}
|
|
940
|
+
if (type) {
|
|
941
|
+
args.push("--type", type);
|
|
942
|
+
}
|
|
943
|
+
if (path3 && path3 !== ".") {
|
|
944
|
+
if (!(0, import_fs6.existsSync)(path3)) {
|
|
945
|
+
throw new Error(`Path does not exist: ${path3}`);
|
|
946
|
+
}
|
|
947
|
+
args.push(path3);
|
|
948
|
+
}
|
|
949
|
+
let command = args.map((arg) => {
|
|
950
|
+
if (arg.includes(" ") || arg.includes("$") || arg.includes("*")) {
|
|
951
|
+
return `'${arg.replace(/'/g, "'\\''")}'`;
|
|
952
|
+
}
|
|
953
|
+
return arg;
|
|
954
|
+
}).join(" ");
|
|
955
|
+
if (offset > 0) {
|
|
956
|
+
command += ` | tail -n +${offset + 1}`;
|
|
957
|
+
}
|
|
958
|
+
if (headLimit > 0) {
|
|
959
|
+
command += ` | head -n ${headLimit}`;
|
|
960
|
+
}
|
|
961
|
+
try {
|
|
962
|
+
const output = (0, import_child_process.execSync)(command, {
|
|
963
|
+
encoding: "utf-8",
|
|
964
|
+
maxBuffer: 1024 * 1024 * 10,
|
|
965
|
+
// 10MB
|
|
966
|
+
shell: "/bin/bash"
|
|
967
|
+
});
|
|
968
|
+
let matches;
|
|
969
|
+
if (outputMode === "count") {
|
|
970
|
+
matches = output.split("\n").filter((line) => line.trim()).length;
|
|
971
|
+
} else if (outputMode === "files_with_matches") {
|
|
972
|
+
matches = output.split("\n").filter((line) => line.trim()).length;
|
|
973
|
+
}
|
|
974
|
+
return {
|
|
975
|
+
output: truncateOutput(output || "(no matches found)"),
|
|
976
|
+
matches
|
|
977
|
+
};
|
|
978
|
+
} catch (error) {
|
|
979
|
+
if (error.status === 1) {
|
|
980
|
+
return {
|
|
981
|
+
output: "(no matches found)",
|
|
982
|
+
matches: 0
|
|
983
|
+
};
|
|
984
|
+
}
|
|
985
|
+
throw new Error(
|
|
986
|
+
`grep failed: ${error.stderr || error.message}
|
|
987
|
+
Command: ${command}`
|
|
988
|
+
);
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
};
|
|
992
|
+
|
|
993
|
+
// src/tools/ls.ts
|
|
994
|
+
var import_zod6 = __toESM(require("zod"), 1);
|
|
995
|
+
var import_fs7 = require("fs");
|
|
996
|
+
var LsTool = {
|
|
997
|
+
name: "ls",
|
|
998
|
+
description: "List files and directories in a given path",
|
|
999
|
+
inputSchema: import_zod6.default.object({
|
|
1000
|
+
path: import_zod6.default.string().optional().describe("The path to list files from (defaults to current directory)")
|
|
1001
|
+
}),
|
|
1002
|
+
execute: async ({ path: path3 = "." }) => {
|
|
1003
|
+
const files = (0, import_fs7.readdirSync)(path3);
|
|
1004
|
+
return { files };
|
|
1005
|
+
}
|
|
1006
|
+
};
|
|
1007
|
+
|
|
1008
|
+
// src/tools/bash.ts
|
|
1009
|
+
var import_zod7 = __toESM(require("zod"), 1);
|
|
1010
|
+
var import_child_process2 = require("child_process");
|
|
1011
|
+
var BashTool = {
|
|
1012
|
+
name: "bash",
|
|
1013
|
+
description: "Execute a bash command and return the output. Supports timeout and working directory configuration.",
|
|
1014
|
+
inputSchema: import_zod7.default.object({
|
|
1015
|
+
command: import_zod7.default.string().describe("The bash command to execute"),
|
|
1016
|
+
timeout: import_zod7.default.number().optional().describe("Optional timeout in milliseconds (max 600000ms / 10 minutes). Defaults to 120000ms (2 minutes)."),
|
|
1017
|
+
cwd: import_zod7.default.string().optional().describe("Optional working directory for command execution. Defaults to current directory."),
|
|
1018
|
+
description: import_zod7.default.string().optional().describe("Optional description of what this command does (for logging/debugging)")
|
|
1019
|
+
}),
|
|
1020
|
+
execute: async ({ command, timeout = 12e4, cwd, description }) => {
|
|
1021
|
+
if (timeout && (timeout < 0 || timeout > 6e5)) {
|
|
1022
|
+
throw new Error("Timeout must be between 0 and 600000ms (10 minutes)");
|
|
1023
|
+
}
|
|
1024
|
+
try {
|
|
1025
|
+
const output = (0, import_child_process2.execSync)(command, {
|
|
1026
|
+
encoding: "utf-8",
|
|
1027
|
+
maxBuffer: 1024 * 1024 * 10,
|
|
1028
|
+
// 10MB
|
|
1029
|
+
timeout: timeout || 12e4,
|
|
1030
|
+
cwd: cwd || process.cwd(),
|
|
1031
|
+
shell: "/bin/bash"
|
|
1032
|
+
});
|
|
1033
|
+
return {
|
|
1034
|
+
output: truncateOutput(output || "(command completed with no output)"),
|
|
1035
|
+
exitCode: 0
|
|
1036
|
+
};
|
|
1037
|
+
} catch (error) {
|
|
1038
|
+
const exitCode = error.status || error.code || 1;
|
|
1039
|
+
const stdout = String(error.stdout || "");
|
|
1040
|
+
const stderr = String(error.stderr || error.message || "");
|
|
1041
|
+
if (error.killed && error.signal === "SIGTERM") {
|
|
1042
|
+
return {
|
|
1043
|
+
output: truncateOutput(stdout),
|
|
1044
|
+
error: truncateOutput(`Command timed out after ${timeout}ms
|
|
1045
|
+
${stderr}`),
|
|
1046
|
+
exitCode
|
|
1047
|
+
};
|
|
1048
|
+
}
|
|
1049
|
+
return {
|
|
1050
|
+
output: truncateOutput(stdout),
|
|
1051
|
+
error: truncateOutput(stderr),
|
|
1052
|
+
exitCode
|
|
1053
|
+
};
|
|
1054
|
+
}
|
|
1055
|
+
}
|
|
1056
|
+
};
|
|
1057
|
+
|
|
1058
|
+
// src/tools/tavily.ts
|
|
1059
|
+
var import_zod8 = __toESM(require("zod"), 1);
|
|
1060
|
+
var TavilyTool = {
|
|
1061
|
+
name: "tavily",
|
|
1062
|
+
description: "Search the web using Tavily API",
|
|
1063
|
+
inputSchema: import_zod8.default.object({
|
|
1064
|
+
query: import_zod8.default.string().describe("The search query"),
|
|
1065
|
+
maxResults: import_zod8.default.number().optional().default(5).describe("Maximum number of results to return")
|
|
1066
|
+
}),
|
|
1067
|
+
execute: async ({ query, maxResults = 5 }) => {
|
|
1068
|
+
const apiKey = process.env.TAVILY_API_KEY;
|
|
1069
|
+
if (!apiKey) {
|
|
1070
|
+
throw new Error("TAVILY_API_KEY environment variable is not set");
|
|
1071
|
+
}
|
|
1072
|
+
const response = await fetch("https://api.tavily.com/search", {
|
|
1073
|
+
method: "POST",
|
|
1074
|
+
headers: {
|
|
1075
|
+
"Content-Type": "application/json"
|
|
1076
|
+
},
|
|
1077
|
+
body: JSON.stringify({
|
|
1078
|
+
api_key: apiKey,
|
|
1079
|
+
query,
|
|
1080
|
+
max_results: maxResults,
|
|
1081
|
+
search_depth: "basic",
|
|
1082
|
+
include_answer: false,
|
|
1083
|
+
include_raw_content: false,
|
|
1084
|
+
include_images: false
|
|
1085
|
+
})
|
|
1086
|
+
});
|
|
1087
|
+
if (!response.ok) {
|
|
1088
|
+
throw new Error(`Tavily API error: ${response.status} ${response.statusText}`);
|
|
1089
|
+
}
|
|
1090
|
+
const data = await response.json();
|
|
1091
|
+
return {
|
|
1092
|
+
results: data.results?.map((result) => ({
|
|
1093
|
+
title: result.title || "",
|
|
1094
|
+
url: result.url || "",
|
|
1095
|
+
content: truncateOutput(result.content || ""),
|
|
1096
|
+
score: result.score || 0
|
|
1097
|
+
})) || []
|
|
1098
|
+
};
|
|
1099
|
+
}
|
|
1100
|
+
};
|
|
1101
|
+
|
|
1102
|
+
// src/tools/clarify.ts
|
|
1103
|
+
var import_zod9 = __toESM(require("zod"), 1);
|
|
1104
|
+
var import_crypto = require("crypto");
|
|
1105
|
+
var ClarifyTool = {
|
|
1106
|
+
name: "clarify",
|
|
1107
|
+
description: "Ask the user a clarifying question and wait for their response. Use this when you need information from the user to proceed with the task.",
|
|
1108
|
+
inputSchema: import_zod9.default.object({
|
|
1109
|
+
question: import_zod9.default.string().describe("The question to ask the user"),
|
|
1110
|
+
context: import_zod9.default.string().optional().describe("Additional context to help the user answer"),
|
|
1111
|
+
defaultAnswer: import_zod9.default.string().optional().describe("Default answer if user does not respond within timeout"),
|
|
1112
|
+
timeout: import_zod9.default.number().optional().describe("Timeout in milliseconds (default: 5 minutes)")
|
|
1113
|
+
}),
|
|
1114
|
+
execute: async (input, toolContext) => {
|
|
1115
|
+
if (!toolContext?.onClarificationRequest) {
|
|
1116
|
+
throw new Error("Clarification is not supported in this context. The clarify tool requires a CLI interface with user interaction.");
|
|
1117
|
+
}
|
|
1118
|
+
const timeout = input.timeout ?? CLARIFICATION_TIMEOUT;
|
|
1119
|
+
const requestId = (0, import_crypto.randomUUID)();
|
|
1120
|
+
try {
|
|
1121
|
+
const timeoutPromise = new Promise((_, reject) => {
|
|
1122
|
+
setTimeout(() => {
|
|
1123
|
+
reject(new Error(`Clarification request timed out after ${timeout}ms`));
|
|
1124
|
+
}, timeout);
|
|
1125
|
+
});
|
|
1126
|
+
const answer = await Promise.race([
|
|
1127
|
+
toolContext.onClarificationRequest({
|
|
1128
|
+
id: requestId,
|
|
1129
|
+
question: input.question,
|
|
1130
|
+
context: input.context,
|
|
1131
|
+
defaultAnswer: input.defaultAnswer,
|
|
1132
|
+
timeout
|
|
1133
|
+
}),
|
|
1134
|
+
timeoutPromise
|
|
1135
|
+
]);
|
|
1136
|
+
return {
|
|
1137
|
+
answer,
|
|
1138
|
+
timedOut: false
|
|
1139
|
+
};
|
|
1140
|
+
} catch (error) {
|
|
1141
|
+
if (error?.message?.includes("timed out") && input.defaultAnswer) {
|
|
1142
|
+
return {
|
|
1143
|
+
answer: input.defaultAnswer,
|
|
1144
|
+
timedOut: true
|
|
1145
|
+
};
|
|
1146
|
+
}
|
|
1147
|
+
throw error;
|
|
1148
|
+
}
|
|
1149
|
+
}
|
|
1150
|
+
};
|
|
1151
|
+
|
|
1152
|
+
// src/tools/index.ts
|
|
1153
|
+
var BuiltinTools = [
|
|
1154
|
+
ReadTool,
|
|
1155
|
+
WriteTool,
|
|
1156
|
+
EditTool,
|
|
1157
|
+
GrepTool,
|
|
1158
|
+
LsTool,
|
|
1159
|
+
BashTool,
|
|
1160
|
+
TavilyTool,
|
|
1161
|
+
ClarifyTool
|
|
1162
|
+
];
|
|
1163
|
+
var BuiltinToolsMap = BuiltinTools.reduce((acc, toolInstance) => {
|
|
1164
|
+
acc[toolInstance.name] = toolInstance;
|
|
1165
|
+
return acc;
|
|
1166
|
+
}, {});
|
|
1167
|
+
|
|
1168
|
+
// src/built-in/sub-agent-plugin/index.ts
|
|
1169
|
+
var ConfigLoader = class {
|
|
1170
|
+
async getAgentFilesInfo(configDirs) {
|
|
1171
|
+
const fileInfos = [];
|
|
1172
|
+
for (let configDir of configDirs) {
|
|
1173
|
+
try {
|
|
1174
|
+
await import_fs8.promises.access(configDir);
|
|
1175
|
+
const files = await import_fs8.promises.readdir(configDir);
|
|
1176
|
+
fileInfos.push({ files, configDir });
|
|
1177
|
+
} catch {
|
|
1178
|
+
continue;
|
|
1179
|
+
}
|
|
1180
|
+
}
|
|
1181
|
+
return fileInfos;
|
|
1182
|
+
}
|
|
1183
|
+
async loadAgentConfigs(configDir = [".pulse-coder/agents", ".coder/agents"]) {
|
|
1184
|
+
const configs = [];
|
|
1185
|
+
const configDirs = Array.isArray(configDir) ? configDir : [configDir];
|
|
1186
|
+
try {
|
|
1187
|
+
const filesInfo = await this.getAgentFilesInfo(configDirs);
|
|
1188
|
+
for (const fileInfo of filesInfo) {
|
|
1189
|
+
const files = fileInfo.files;
|
|
1190
|
+
for (const file of files) {
|
|
1191
|
+
if (file.endsWith(".md")) {
|
|
1192
|
+
const config = await this.parseConfig(import_path2.default.join(fileInfo.configDir, file));
|
|
1193
|
+
if (config) configs.push(config);
|
|
1194
|
+
}
|
|
1195
|
+
}
|
|
1196
|
+
}
|
|
1197
|
+
} catch (error) {
|
|
1198
|
+
console.warn(`Failed to scan agent configs: ${error}`);
|
|
1199
|
+
}
|
|
1200
|
+
return configs;
|
|
1201
|
+
}
|
|
1202
|
+
async parseConfig(filePath) {
|
|
1203
|
+
try {
|
|
1204
|
+
const content = await import_fs8.promises.readFile(filePath, "utf-8");
|
|
1205
|
+
const lines = content.split("\n");
|
|
1206
|
+
let name = "";
|
|
1207
|
+
let description = "";
|
|
1208
|
+
let systemPrompt = "";
|
|
1209
|
+
let inFrontmatter = false;
|
|
1210
|
+
let frontmatterEnd = false;
|
|
1211
|
+
for (const line of lines) {
|
|
1212
|
+
if (line.trim() === "---") {
|
|
1213
|
+
if (!inFrontmatter) {
|
|
1214
|
+
inFrontmatter = true;
|
|
1215
|
+
continue;
|
|
1216
|
+
} else {
|
|
1217
|
+
frontmatterEnd = true;
|
|
1218
|
+
continue;
|
|
1219
|
+
}
|
|
1220
|
+
}
|
|
1221
|
+
if (inFrontmatter && !frontmatterEnd) {
|
|
1222
|
+
const match = line.match(/^\s*(\w+)\s*:\s*(.+)$/);
|
|
1223
|
+
if (match) {
|
|
1224
|
+
const [, key, value] = match;
|
|
1225
|
+
if (key === "name") name = value.trim();
|
|
1226
|
+
if (key === "description") description = value.trim();
|
|
1227
|
+
}
|
|
1228
|
+
} else if (frontmatterEnd) {
|
|
1229
|
+
systemPrompt += line + "\n";
|
|
1230
|
+
}
|
|
1231
|
+
}
|
|
1232
|
+
if (!name) {
|
|
1233
|
+
name = import_path2.default.basename(filePath, ".md");
|
|
1234
|
+
}
|
|
1235
|
+
return {
|
|
1236
|
+
name: name.trim(),
|
|
1237
|
+
description: description.trim(),
|
|
1238
|
+
systemPrompt: systemPrompt.trim(),
|
|
1239
|
+
filePath
|
|
1240
|
+
};
|
|
1241
|
+
} catch (error) {
|
|
1242
|
+
console.warn(`Failed to parse agent config ${filePath}: ${error}`);
|
|
1243
|
+
return null;
|
|
1244
|
+
}
|
|
1245
|
+
}
|
|
1246
|
+
};
|
|
1247
|
+
var AgentRunner = class {
|
|
1248
|
+
async runAgent(config, task, context, tools) {
|
|
1249
|
+
const subContext = {
|
|
1250
|
+
messages: [
|
|
1251
|
+
{ role: "user", content: task }
|
|
1252
|
+
]
|
|
1253
|
+
};
|
|
1254
|
+
if (context && Object.keys(context).length > 0) {
|
|
1255
|
+
subContext.messages.push({
|
|
1256
|
+
role: "user",
|
|
1257
|
+
content: `\u4E0A\u4E0B\u6587\u4FE1\u606F\uFF1A
|
|
1258
|
+
${JSON.stringify(context, null, 2)}`
|
|
1259
|
+
});
|
|
1260
|
+
}
|
|
1261
|
+
return await loop(subContext, { tools, systemPrompt: config.systemPrompt });
|
|
1262
|
+
}
|
|
1263
|
+
};
|
|
1264
|
+
var SubAgentPlugin = class {
|
|
1265
|
+
name = "sub-agent";
|
|
1266
|
+
version = "1.0.0";
|
|
1267
|
+
configLoader = new ConfigLoader();
|
|
1268
|
+
agentRunner = new AgentRunner();
|
|
1269
|
+
async initialize(context) {
|
|
1270
|
+
try {
|
|
1271
|
+
const configs = await this.configLoader.loadAgentConfigs();
|
|
1272
|
+
const tools = this.getAvailableTools(context);
|
|
1273
|
+
for (const config of configs) {
|
|
1274
|
+
this.registerAgentTool(context, config, tools);
|
|
1275
|
+
}
|
|
1276
|
+
context.logger.info(`SubAgentPlugin loaded ${configs.length} agents.`);
|
|
1277
|
+
} catch (error) {
|
|
1278
|
+
context.logger.error("Failed to initialize SubAgentPlugin", error);
|
|
1279
|
+
}
|
|
1280
|
+
}
|
|
1281
|
+
getAvailableTools(context) {
|
|
1282
|
+
const allTools = {};
|
|
1283
|
+
return BuiltinToolsMap;
|
|
1284
|
+
}
|
|
1285
|
+
registerAgentTool(context, config, tools) {
|
|
1286
|
+
const toolName = `${config.name}_agent`;
|
|
1287
|
+
const tool2 = {
|
|
1288
|
+
description: config.description,
|
|
1289
|
+
inputSchema: import_zod10.z.object({
|
|
1290
|
+
task: import_zod10.z.string().describe("\u8981\u6267\u884C\u7684\u4EFB\u52A1\u63CF\u8FF0"),
|
|
1291
|
+
context: import_zod10.z.any().optional().describe("\u4EFB\u52A1\u4E0A\u4E0B\u6587\u4FE1\u606F")
|
|
1292
|
+
}),
|
|
1293
|
+
execute: async ({ task, context: taskContext }) => {
|
|
1294
|
+
try {
|
|
1295
|
+
context.logger.info(`Running agent ${config.name}: ${task}`);
|
|
1296
|
+
const result = await this.agentRunner.runAgent(config, task, taskContext, tools);
|
|
1297
|
+
context.logger.info(`Agent ${config.name} completed successfully`);
|
|
1298
|
+
return result;
|
|
1299
|
+
} catch (error) {
|
|
1300
|
+
context.logger.error(`Agent ${config.name} failed`, error);
|
|
1301
|
+
throw new Error(`Agent ${config.name} failed: ${error}`);
|
|
1302
|
+
}
|
|
1303
|
+
}
|
|
1304
|
+
};
|
|
1305
|
+
context.registerTool(toolName, tool2);
|
|
1306
|
+
}
|
|
1307
|
+
async destroy(context) {
|
|
1308
|
+
context.logger.info("SubAgentPlugin destroyed");
|
|
1309
|
+
}
|
|
1310
|
+
};
|
|
1311
|
+
|
|
1312
|
+
// src/built-in/index.ts
|
|
1313
|
+
var builtInPlugins = [
|
|
1314
|
+
builtInMCPPlugin,
|
|
1315
|
+
builtInSkillsPlugin,
|
|
1316
|
+
new SubAgentPlugin()
|
|
1317
|
+
];
|
|
1318
|
+
var built_in_default = builtInPlugins;
|
|
1319
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
1320
|
+
0 && (module.exports = {
|
|
1321
|
+
BuiltInSkillRegistry,
|
|
1322
|
+
SubAgentPlugin,
|
|
1323
|
+
builtInMCPPlugin,
|
|
1324
|
+
builtInPlugins,
|
|
1325
|
+
builtInSkillsPlugin
|
|
1326
|
+
});
|
|
1327
|
+
//# sourceMappingURL=index.cjs.map
|