@amical/aiagent-sdk 0.1.8 → 0.1.10

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.mjs CHANGED
@@ -12,6 +12,7 @@ var AgentDefaultTool = /* @__PURE__ */ ((AgentDefaultTool2) => {
12
12
  AgentDefaultTool2["FILE_DELETE"] = "agent_delete_file";
13
13
  AgentDefaultTool2["DIR_DELETE"] = "agent_delete_directory";
14
14
  AgentDefaultTool2["SEARCH"] = "agent_search_files";
15
+ AgentDefaultTool2["SEARCH_CONTENT"] = "agent_search_file_content";
15
16
  AgentDefaultTool2["LIST_FILES"] = "agent_list_files";
16
17
  AgentDefaultTool2["LIST_CODE_DEF"] = "agent_list_code_definition_names";
17
18
  AgentDefaultTool2["BROWSER"] = "agent_browser_action";
@@ -185,11 +186,22 @@ return (
185
186
  },
186
187
  {
187
188
  name: "agent_search_files" /* SEARCH */,
188
- description: "Search for files in a directory that match a regex pattern or glob pattern.",
189
+ description: "Search for files or directories by name in a directory tree, matching the name against a regex (and optionally a glob). Directories are traversed level by level: all matches in the current level are returned before descending into deeper levels. Use this tool only to locate files/directories by name.",
189
190
  params: {
190
191
  path: "(required) The absolute or relative directory path to search.",
191
- regex: "(required) The regex pattern to match files against.",
192
- file_pattern: "(optional) glob pattern like *.ts."
192
+ regex: "(required) The regex pattern to match file or directory names against.",
193
+ file_pattern: "(optional) glob pattern like *.ts.",
194
+ include_node_modules: "(optional) set to true to include node_modules in the search. Defaults to false, so node_modules is skipped."
195
+ }
196
+ },
197
+ {
198
+ name: "agent_search_file_content" /* SEARCH_CONTENT */,
199
+ description: "Search the contents of files in a directory tree for a regex pattern. Use this tool when you need to find text or code inside files rather than matching file or directory names.",
200
+ params: {
201
+ path: "(required) The absolute or relative directory path to search.",
202
+ regex: "(required) The regex pattern to match file content against.",
203
+ file_pattern: "(optional) glob pattern like *.ts to limit which files are searched.",
204
+ include_node_modules: "(optional) set to true to include node_modules in the search. Defaults to false, so node_modules is skipped."
193
205
  }
194
206
  },
195
207
  {
@@ -258,6 +270,7 @@ var READ_ONLY_TOOLS = [
258
270
  "agent_list_files" /* LIST_FILES */,
259
271
  "agent_read_file" /* FILE_READ */,
260
272
  "agent_search_files" /* SEARCH */,
273
+ "agent_search_file_content" /* SEARCH_CONTENT */,
261
274
  "agent_list_code_definition_names" /* LIST_CODE_DEF */,
262
275
  "agent_browser_action" /* BROWSER */,
263
276
  "agent_ask_followup_question" /* ASK */,
@@ -427,6 +440,7 @@ var toolParamNames = [
427
440
  "diff",
428
441
  "regex",
429
442
  "file_pattern",
443
+ "include_node_modules",
430
444
  "recursive",
431
445
  "action",
432
446
  "url",
@@ -465,6 +479,66 @@ var toolParamNames = [
465
479
  "start_line",
466
480
  "end_line"
467
481
  ];
482
+ var TEXT_FILE_EXTENSIONS = /* @__PURE__ */ new Set([
483
+ "txt",
484
+ "md",
485
+ "markdown",
486
+ "json",
487
+ "js",
488
+ "mjs",
489
+ "cjs",
490
+ "ts",
491
+ "tsx",
492
+ "jsx",
493
+ "html",
494
+ "htm",
495
+ "css",
496
+ "scss",
497
+ "sass",
498
+ "less",
499
+ "py",
500
+ "java",
501
+ "c",
502
+ "cpp",
503
+ "cc",
504
+ "cxx",
505
+ "h",
506
+ "hpp",
507
+ "cs",
508
+ "go",
509
+ "rs",
510
+ "rb",
511
+ "php",
512
+ "sh",
513
+ "bash",
514
+ "zsh",
515
+ "bat",
516
+ "cmd",
517
+ "ps1",
518
+ "yaml",
519
+ "yml",
520
+ "toml",
521
+ "ini",
522
+ "cfg",
523
+ "conf",
524
+ "xml",
525
+ "svg",
526
+ "csv",
527
+ "log",
528
+ "sql",
529
+ "vue",
530
+ "svelte",
531
+ "astro",
532
+ "graphql",
533
+ "gql",
534
+ "prisma",
535
+ "env",
536
+ "gitignore",
537
+ "dockerfile",
538
+ "makefile",
539
+ "properties",
540
+ "lock"
541
+ ]);
468
542
  var AgentState = /* @__PURE__ */ ((AgentState2) => {
469
543
  AgentState2["IDLE"] = "idle";
470
544
  AgentState2["THINKING"] = "thinking";
@@ -545,7 +619,7 @@ var ClineAgent = class {
545
619
  }
546
620
  }
547
621
  /**
548
- * 获取当前 Agent 的系统提示词
622
+ * 获取当前 Agent 的系统提示词
549
623
  * 如果 force 为 true,则强制重新获取系统提示词
550
624
  */
551
625
  async getPrompt(force = false) {
@@ -820,15 +894,41 @@ STDERR: ${result.stderr}` });
820
894
  const path = tool.params.path || this.runtime.env.cwd();
821
895
  const regexStr = tool.params.regex;
822
896
  const filePattern = tool.params.file_pattern;
823
- if (regexStr && this.runtime.fs.searchFiles) {
897
+ const includeNodeModules = tool.params.include_node_modules === "true";
898
+ if (regexStr && path) {
824
899
  try {
825
- const result = await this.runtime.fs.searchFiles(path, regexStr, filePattern);
826
- toolResults.push({ tool, result: `${result}` });
900
+ let result;
901
+ if (typeof this.runtime.fs.searchFiles === "function") {
902
+ result = await this.runtime.fs.searchFiles(path, regexStr, filePattern);
903
+ } else {
904
+ result = await this.searchFiles(path, regexStr, filePattern, includeNodeModules);
905
+ }
906
+ toolResults.push({ tool, result });
827
907
  } catch (e) {
828
908
  toolResults.push({ tool, result: `Error: ${e.message}` });
829
909
  }
830
910
  } else {
831
- toolResults.push({ tool, result: `Error: searchFiles not supported by runtime or missing regex.` });
911
+ toolResults.push({ tool, result: `Error: miss path or regex params` });
912
+ }
913
+ } else if (tool.name === "agent_search_file_content" /* SEARCH_CONTENT */) {
914
+ const path = tool.params.path || this.runtime.env.cwd();
915
+ const regexStr = tool.params.regex;
916
+ const filePattern = tool.params.file_pattern;
917
+ const includeNodeModules = tool.params.include_node_modules === "true";
918
+ if (regexStr && path) {
919
+ try {
920
+ let result;
921
+ if (typeof this.runtime.fs.searchFileContent === "function") {
922
+ result = await this.runtime.fs.searchFileContent(path, regexStr, filePattern);
923
+ } else {
924
+ result = await this.searchFileContent(path, regexStr, filePattern, includeNodeModules);
925
+ }
926
+ toolResults.push({ tool, result });
927
+ } catch (e) {
928
+ toolResults.push({ tool, result: `Error: ${e.message}` });
929
+ }
930
+ } else {
931
+ toolResults.push({ tool, result: `Error: miss path or regex params` });
832
932
  }
833
933
  } else if (tool.name === "agent_use_mcp_tool" /* MCP_USE */) {
834
934
  const serverName = tool.params.server_name;
@@ -933,9 +1033,14 @@ IMPORTANT: The skill is now loaded. Do NOT call agent_load_skill again for this
933
1033
  }
934
1034
  }
935
1035
  if (this.state === "thinking" /* THINKING */ && toolResults.length > 0) {
936
- const toolResultsContext = toolResults.map((tr) => `<${TOOL_RESULT_BLOCK} tool_name="${tr.tool.name}">
1036
+ let toolResultsContext = toolResults.map((tr) => `<${TOOL_RESULT_BLOCK} tool_name="${tr.tool.name}">
937
1037
  ${typeof tr.result === "string" ? tr.result.trim() : JSON.stringify(tr.result)}
938
1038
  </${TOOL_RESULT_BLOCK}>`).join("\n\n");
1039
+ const lastResult = toolResults[toolResults.length - 1];
1040
+ if (lastResult && lastResult.tool.name !== "agent_attempt_completion" /* ATTEMPT */) {
1041
+ toolResultsContext += `
1042
+ ${this.promptManager.getUsedToolResultInstructions()}`;
1043
+ }
939
1044
  if (this.onStreamChunk) {
940
1045
  this.onStreamChunk({ type: "text", text: toolResultsContext });
941
1046
  }
@@ -993,6 +1098,120 @@ ${searchBlock.substring(0, 100)}...`);
993
1098
  async getHistory() {
994
1099
  return this.memory.getHistory();
995
1100
  }
1101
+ /**
1102
+ * 判断文件是否为可进行内容搜索的文本类文件
1103
+ */
1104
+ isTextFile(name) {
1105
+ const ext = name.includes(".") ? String(name.split(".").pop()).toLowerCase() : "";
1106
+ if (!ext) {
1107
+ return true;
1108
+ }
1109
+ return TEXT_FILE_EXTENSIONS.has(ext);
1110
+ }
1111
+ /**
1112
+ * 按文件名/目录名搜索,层级优先(BFS):
1113
+ * 每一层先匹配完当前层级的所有条目,再进入更深的子目录。
1114
+ */
1115
+ async searchFiles(dirpath, regex, filePattern, includeNodeModules = false) {
1116
+ const matches = [];
1117
+ const MAX_RESULTS = 100;
1118
+ const MAX_DEPTH = 8;
1119
+ const SKIP_DIRS = /* @__PURE__ */ new Set([".git", "dist", ".agent", ".turbopack"]);
1120
+ const shouldSkipDir = (name) => SKIP_DIRS.has(name) || name === "node_modules" && !includeNodeModules;
1121
+ const nameRe = new RegExp(regex);
1122
+ const globRe = filePattern ? new RegExp(filePattern.replace(/\./g, "\\.").replace(/\*/g, ".*")) : null;
1123
+ const queue = [{ dir: dirpath, depth: 0 }];
1124
+ while (queue.length > 0 && matches.length < MAX_RESULTS) {
1125
+ const { dir, depth } = queue.shift();
1126
+ if (depth > MAX_DEPTH) {
1127
+ continue;
1128
+ }
1129
+ let entries;
1130
+ try {
1131
+ entries = await this.runtime.fs.readDir(dir, { recursive: false });
1132
+ } catch {
1133
+ continue;
1134
+ }
1135
+ for (const entry of entries) {
1136
+ if (matches.length >= MAX_RESULTS) {
1137
+ break;
1138
+ }
1139
+ const child = dir === "." ? entry.name : `${dir}/${entry.name}`;
1140
+ if (entry.isDirectory()) {
1141
+ if (!shouldSkipDir(entry.name)) {
1142
+ if (nameRe.test(entry.name)) {
1143
+ matches.push(`${child}/`);
1144
+ }
1145
+ queue.push({ dir: child, depth: depth + 1 });
1146
+ }
1147
+ } else if (entry.isFile()) {
1148
+ if (globRe && !globRe.test(entry.name)) {
1149
+ continue;
1150
+ }
1151
+ if (nameRe.test(entry.name)) {
1152
+ matches.push(child);
1153
+ }
1154
+ }
1155
+ }
1156
+ }
1157
+ if (matches.length === 0) {
1158
+ return "No files found.";
1159
+ }
1160
+ return matches.join("\n");
1161
+ }
1162
+ /**
1163
+ * 按文件内容搜索:正则匹配文件内容,返回匹配文件路径。
1164
+ */
1165
+ async searchFileContent(dirpath, regex, filePattern, includeNodeModules = false) {
1166
+ const matches = [];
1167
+ const MAX_RESULTS = 100;
1168
+ const MAX_DEPTH = 8;
1169
+ const SKIP_DIRS = /* @__PURE__ */ new Set([".git", "dist", ".agent", ".turbopack"]);
1170
+ const shouldSkipDir = (name) => SKIP_DIRS.has(name) || name === "node_modules" && !includeNodeModules;
1171
+ const contentRe = new RegExp(regex);
1172
+ const globRe = filePattern ? new RegExp(filePattern.replace(/\./g, "\\.").replace(/\*/g, ".*")) : null;
1173
+ const walk = async (dir, depth) => {
1174
+ if (depth > MAX_DEPTH || matches.length >= MAX_RESULTS) {
1175
+ return;
1176
+ }
1177
+ let entries;
1178
+ try {
1179
+ entries = await this.runtime.fs.readDir(dir, { recursive: false });
1180
+ } catch {
1181
+ return;
1182
+ }
1183
+ for (const entry of entries) {
1184
+ if (matches.length >= MAX_RESULTS) {
1185
+ break;
1186
+ }
1187
+ const child = dir === "." ? entry.name : `${dir}/${entry.name}`;
1188
+ if (entry.isDirectory()) {
1189
+ if (!shouldSkipDir(entry.name)) {
1190
+ await walk(child, depth + 1);
1191
+ }
1192
+ } else if (entry.isFile()) {
1193
+ if (!this.isTextFile(entry.name)) {
1194
+ continue;
1195
+ }
1196
+ if (globRe && !globRe.test(entry.name)) {
1197
+ continue;
1198
+ }
1199
+ try {
1200
+ const content = await this.runtime.fs.readFile(child);
1201
+ if (typeof content === "string" && contentRe.test(content)) {
1202
+ matches.push(child);
1203
+ }
1204
+ } catch {
1205
+ }
1206
+ }
1207
+ }
1208
+ };
1209
+ await walk(dirpath, 0);
1210
+ if (matches.length === 0) {
1211
+ return "No files found with matching content.";
1212
+ }
1213
+ return matches.join("\n");
1214
+ }
996
1215
  };
997
1216
 
998
1217
  // src/index.ts