@amical/aiagent-sdk 0.1.8 → 0.1.9

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.
@@ -4,7 +4,7 @@ import { streamText } from 'ai';
4
4
  import { createOpenAI } from '@ai-sdk/openai';
5
5
  import { createDifyProvider } from 'dify-ai-provider';
6
6
  import * as fs2 from 'fs/promises';
7
- import * as path2 from 'path';
7
+ import * as path from 'path';
8
8
  import * as os from 'os';
9
9
  import { exec } from 'child_process';
10
10
  import { promisify } from 'util';
@@ -555,7 +555,7 @@ var ClineAgent = class {
555
555
  }
556
556
  }
557
557
  /**
558
- * 获取当前 Agent 的系统提示词
558
+ * 获取当前 Agent 的系统提示词
559
559
  * 如果 force 为 true,则强制重新获取系统提示词
560
560
  */
561
561
  async getPrompt(force = false) {
@@ -830,15 +830,20 @@ STDERR: ${result.stderr}` });
830
830
  const path3 = tool.params.path || this.runtime.env.cwd();
831
831
  const regexStr = tool.params.regex;
832
832
  const filePattern = tool.params.file_pattern;
833
- if (regexStr && this.runtime.fs.searchFiles) {
833
+ if (regexStr && path3) {
834
834
  try {
835
- const result = await this.runtime.fs.searchFiles(path3, regexStr, filePattern);
835
+ let result;
836
+ if (typeof this.runtime.fs.searchFiles === "function") {
837
+ result = await this.runtime.fs.searchFiles(path3, regexStr, filePattern);
838
+ } else {
839
+ result = await this.searchFiles(path3, regexStr, filePattern);
840
+ }
836
841
  toolResults.push({ tool, result: `${result}` });
837
842
  } catch (e) {
838
843
  toolResults.push({ tool, result: `Error: ${e.message}` });
839
844
  }
840
845
  } else {
841
- toolResults.push({ tool, result: `Error: searchFiles not supported by runtime or missing regex.` });
846
+ toolResults.push({ tool, result: `Error: miss path or regex params` });
842
847
  }
843
848
  } else if (tool.name === "agent_use_mcp_tool" /* MCP_USE */) {
844
849
  const serverName = tool.params.server_name;
@@ -943,9 +948,14 @@ IMPORTANT: The skill is now loaded. Do NOT call agent_load_skill again for this
943
948
  }
944
949
  }
945
950
  if (this.state === "thinking" /* THINKING */ && toolResults.length > 0) {
946
- const toolResultsContext = toolResults.map((tr) => `<${TOOL_RESULT_BLOCK} tool_name="${tr.tool.name}">
951
+ let toolResultsContext = toolResults.map((tr) => `<${TOOL_RESULT_BLOCK} tool_name="${tr.tool.name}">
947
952
  ${typeof tr.result === "string" ? tr.result.trim() : JSON.stringify(tr.result)}
948
953
  </${TOOL_RESULT_BLOCK}>`).join("\n\n");
954
+ const lastResult = toolResults[toolResults.length - 1];
955
+ if (lastResult && lastResult.tool.name !== "agent_attempt_completion" /* ATTEMPT */) {
956
+ toolResultsContext += `
957
+ ${this.promptManager.getUsedToolResultInstructions()}`;
958
+ }
949
959
  if (this.onStreamChunk) {
950
960
  this.onStreamChunk({ type: "text", text: toolResultsContext });
951
961
  }
@@ -1003,6 +1013,47 @@ ${searchBlock.substring(0, 100)}...`);
1003
1013
  async getHistory() {
1004
1014
  return this.memory.getHistory();
1005
1015
  }
1016
+ async searchFiles(dirpath, regex, filePattern) {
1017
+ const matches = [];
1018
+ const contentRe = new RegExp(regex);
1019
+ const nameRe = filePattern ? new RegExp(filePattern.replace(/\./g, "\\.").replace(/\*/g, ".*")) : null;
1020
+ const SKIP_DIRS = /* @__PURE__ */ new Set([".git", "dist", ".agent", ".turbopack"]);
1021
+ const walk = async (dir, depth) => {
1022
+ if (depth > 8 || matches.length >= 100) {
1023
+ return;
1024
+ }
1025
+ let entries;
1026
+ try {
1027
+ entries = await this.runtime.fs.readDir(dir, { recursive: false });
1028
+ } catch {
1029
+ return;
1030
+ }
1031
+ for (const entry of entries) {
1032
+ if (matches.length >= 100) {
1033
+ break;
1034
+ }
1035
+ const child = dir === "." ? entry.name : `${dir}/${entry.name}`;
1036
+ if (entry.isDirectory()) {
1037
+ if (!SKIP_DIRS.has(entry.name)) {
1038
+ await walk(child, depth + 1);
1039
+ }
1040
+ } else if (entry.isFile()) {
1041
+ if (nameRe && !nameRe.test(entry.name)) {
1042
+ continue;
1043
+ }
1044
+ try {
1045
+ const content = (await this.runtime.fs.readFile(child, "utf8")).toString();
1046
+ if (contentRe.test(content)) {
1047
+ matches.push(child);
1048
+ }
1049
+ } catch {
1050
+ }
1051
+ }
1052
+ }
1053
+ };
1054
+ await walk(dirpath, 0);
1055
+ return matches.join("\n");
1056
+ }
1006
1057
  };
1007
1058
  var NodeApiHandler = class {
1008
1059
  config;
@@ -1377,6 +1428,21 @@ Otherwise, if you have not completed the task and do not need additional informa
1377
1428
  (This is an automated message, so do not respond to it conversationally.)
1378
1429
  ${this?.getEnvironmentInfoPrompt?.(context)}`;
1379
1430
  }
1431
+ getUsedToolResultInstructions() {
1432
+ return `The above are the results of the tool call. Please continue to complete the remaining tasks.
1433
+
1434
+ # TODO LIST UPDATE REQUIRED - You MUST include the task_progress parameter in your NEXT tool call.
1435
+
1436
+ 1. To create or update a todo list, include the task_progress parameter in the next tool call
1437
+ 2. Review each item and update its status:
1438
+ - Mark completed items with: - [x]
1439
+ - Keep incomplete items as: - [ ]
1440
+ - Add new items if you discover additional steps
1441
+ 3. Modify the list as needed:
1442
+ - Add any new steps you've discovered
1443
+ - Reorder if the sequence has changed
1444
+ 4. Ensure the list accurately reflects the current state`;
1445
+ }
1380
1446
  };
1381
1447
  var NodeMemoryManager = class {
1382
1448
  filePath;
@@ -1386,14 +1452,14 @@ var NodeMemoryManager = class {
1386
1452
  * @param maxMessages 保留的最大消息数量,用于简单的历史截断(默认 150)
1387
1453
  */
1388
1454
  constructor(dbPath, maxMessages = 150) {
1389
- this.filePath = path2.resolve(dbPath);
1455
+ this.filePath = path.resolve(dbPath);
1390
1456
  this.maxMessages = maxMessages;
1391
1457
  }
1392
1458
  async ensureFile() {
1393
1459
  try {
1394
1460
  await fs2.access(this.filePath);
1395
1461
  } catch {
1396
- const dir = path2.dirname(this.filePath);
1462
+ const dir = path.dirname(this.filePath);
1397
1463
  await fs2.mkdir(dir, { recursive: true });
1398
1464
  await fs2.writeFile(this.filePath, "[]", "utf-8");
1399
1465
  }
@@ -1569,10 +1635,13 @@ var NodeMcpManager = class {
1569
1635
  var execAsync = promisify(exec);
1570
1636
  var DefaultNodeRuntime = class {
1571
1637
  fs = {
1572
- readFile: async (filepath) => fs2.readFile(filepath, "utf-8"),
1638
+ readFile: async (filepath, options = "utf8") => fs2.readFile(filepath, options),
1573
1639
  writeFile: async (filepath, content) => {
1574
- await fs2.mkdir(path2.dirname(filepath), { recursive: true });
1575
- await fs2.writeFile(filepath, content, "utf-8");
1640
+ const dir = filepath.substring(0, filepath.lastIndexOf("/"));
1641
+ if (dir && dir !== filepath) {
1642
+ await fs2.mkdir(dir, { recursive: true });
1643
+ }
1644
+ await fs2.writeFile(filepath, content, "utf8");
1576
1645
  },
1577
1646
  deleteFile: (filepath) => fs2.unlink(filepath),
1578
1647
  deleteDirectory: (dirpath) => fs2.rm(dirpath, { recursive: true, force: true }),
@@ -1584,13 +1653,13 @@ var DefaultNodeRuntime = class {
1584
1653
  return false;
1585
1654
  }
1586
1655
  },
1587
- readDir: (dirpath) => fs2.readdir(dirpath, { withFileTypes: true }),
1656
+ readDir: (dirpath, options) => fs2.readdir(dirpath, { withFileTypes: true, ...options || {} }),
1588
1657
  mkdir: async (dirpath) => {
1589
1658
  await fs2.mkdir(dirpath, { recursive: true });
1590
1659
  }
1591
1660
  };
1592
1661
  path = {
1593
- join: (...paths) => path2.join(...paths)
1662
+ join: (...paths) => path.join(...paths)
1594
1663
  };
1595
1664
  env = {
1596
1665
  get: (key) => process.env[key],
@@ -1612,7 +1681,7 @@ var DefaultNodeRuntime = class {
1612
1681
  function createNodeAgent(options) {
1613
1682
  const runtime = new DefaultNodeRuntime();
1614
1683
  const api = new NodeApiHandler(options.apiConfig);
1615
- const memory = options.memory || new NodeMemoryManager(options.dbPath || path2.join(runtime.env.cwd(), ".cline_memory.json"));
1684
+ const memory = options.memory || new NodeMemoryManager(options.dbPath || path.join(runtime.env.cwd(), ".cline_memory.json"));
1616
1685
  const customTools = options.customTools || [];
1617
1686
  const customToolNames = new Set(customTools.map((t) => t.name));
1618
1687
  const promptManager = new NodePromptManager();