@amical/aiagent-sdk 0.1.9 → 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";
@@ -820,15 +894,36 @@ 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;
897
+ const includeNodeModules = tool.params.include_node_modules === "true";
823
898
  if (regexStr && path) {
824
899
  try {
825
900
  let result;
826
901
  if (typeof this.runtime.fs.searchFiles === "function") {
827
902
  result = await this.runtime.fs.searchFiles(path, regexStr, filePattern);
828
903
  } else {
829
- result = await this.searchFiles(path, regexStr, filePattern);
904
+ result = await this.searchFiles(path, regexStr, filePattern, includeNodeModules);
905
+ }
906
+ toolResults.push({ tool, result });
907
+ } catch (e) {
908
+ toolResults.push({ tool, result: `Error: ${e.message}` });
909
+ }
910
+ } else {
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);
830
925
  }
831
- toolResults.push({ tool, result: `${result}` });
926
+ toolResults.push({ tool, result });
832
927
  } catch (e) {
833
928
  toolResults.push({ tool, result: `Error: ${e.message}` });
834
929
  }
@@ -1003,13 +1098,80 @@ ${searchBlock.substring(0, 100)}...`);
1003
1098
  async getHistory() {
1004
1099
  return this.memory.getHistory();
1005
1100
  }
1006
- async searchFiles(dirpath, regex, filePattern) {
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) {
1007
1116
  const matches = [];
1008
- const contentRe = new RegExp(regex);
1009
- const nameRe = filePattern ? new RegExp(filePattern.replace(/\./g, "\\.").replace(/\*/g, ".*")) : null;
1117
+ const MAX_RESULTS = 100;
1118
+ const MAX_DEPTH = 8;
1010
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;
1011
1173
  const walk = async (dir, depth) => {
1012
- if (depth > 8 || matches.length >= 100) {
1174
+ if (depth > MAX_DEPTH || matches.length >= MAX_RESULTS) {
1013
1175
  return;
1014
1176
  }
1015
1177
  let entries;
@@ -1019,21 +1181,24 @@ ${searchBlock.substring(0, 100)}...`);
1019
1181
  return;
1020
1182
  }
1021
1183
  for (const entry of entries) {
1022
- if (matches.length >= 100) {
1184
+ if (matches.length >= MAX_RESULTS) {
1023
1185
  break;
1024
1186
  }
1025
1187
  const child = dir === "." ? entry.name : `${dir}/${entry.name}`;
1026
1188
  if (entry.isDirectory()) {
1027
- if (!SKIP_DIRS.has(entry.name)) {
1189
+ if (!shouldSkipDir(entry.name)) {
1028
1190
  await walk(child, depth + 1);
1029
1191
  }
1030
1192
  } else if (entry.isFile()) {
1031
- if (nameRe && !nameRe.test(entry.name)) {
1193
+ if (!this.isTextFile(entry.name)) {
1194
+ continue;
1195
+ }
1196
+ if (globRe && !globRe.test(entry.name)) {
1032
1197
  continue;
1033
1198
  }
1034
1199
  try {
1035
- const content = (await this.runtime.fs.readFile(child, "utf8")).toString();
1036
- if (contentRe.test(content)) {
1200
+ const content = await this.runtime.fs.readFile(child);
1201
+ if (typeof content === "string" && contentRe.test(content)) {
1037
1202
  matches.push(child);
1038
1203
  }
1039
1204
  } catch {
@@ -1042,6 +1207,9 @@ ${searchBlock.substring(0, 100)}...`);
1042
1207
  }
1043
1208
  };
1044
1209
  await walk(dirpath, 0);
1210
+ if (matches.length === 0) {
1211
+ return "No files found with matching content.";
1212
+ }
1045
1213
  return matches.join("\n");
1046
1214
  }
1047
1215
  };