@amical/aiagent-sdk 0.1.10 → 0.1.11

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
@@ -279,6 +279,44 @@ var READ_ONLY_TOOLS = [
279
279
  "agent_load_skill" /* LOAD_SKILL */,
280
280
  "agent_use_subagents" /* USE_SUBAGENTS */
281
281
  ];
282
+
283
+ // src/shared/custom-tools.ts
284
+ function isCustomToolGroup(tool) {
285
+ return Array.isArray(tool.children) && tool.children.length > 0;
286
+ }
287
+ function collectCustomToolNames(tools) {
288
+ const names = [];
289
+ const visit = (list) => {
290
+ for (const tool of list) {
291
+ names.push(tool.name);
292
+ if (Array.isArray(tool.children) && tool.children.length > 0) {
293
+ visit(tool.children);
294
+ }
295
+ }
296
+ };
297
+ visit(tools);
298
+ return names;
299
+ }
300
+ function renderCustomToolPrompt(tool, cwd = "") {
301
+ const params = tool.params ? Object.entries(tool.params) : [];
302
+ const replaceCwd = (text) => String(text).replaceAll("${context.cwd}", cwd);
303
+ const paramLines = params.map(([name, description]) => `- ${name}: ${replaceCwd(description)}`).join("\n");
304
+ const usageLines = params.map(([name, description]) => `<${name}>${replaceCwd(description)}</${name}>`).join("\n");
305
+ return `### ${tool.name}
306
+ Description: ${tool.description}
307
+ Parameters:
308
+ ${paramLines}
309
+ - task_progress: (optional) A checklist showing task progress after this tool use is completed. The task_progress parameter must be included as a separate parameter inside of the parent tool call, it must be separate from other parameters such as content, arguments, etc. (See 'UPDATING TASK PROGRESS' section for more details)
310
+ Usage:
311
+ <${tool.name}>
312
+ ${usageLines}
313
+ <task_progress>Checklist here (optional)</task_progress>
314
+ </${tool.name}>
315
+ `;
316
+ }
317
+ function renderCustomToolsPrompt(tools, cwd = "") {
318
+ return tools.map((tool) => renderCustomToolPrompt(tool, cwd)).join("\n");
319
+ }
282
320
  function parseAssistantMessageV2(assistantMessage, toolParamNames2) {
283
321
  const contentBlocks = [];
284
322
  let currentTextContentStart = 0;
@@ -562,9 +600,19 @@ var ClineAgent = class {
562
600
  skillDir;
563
601
  rulesDir;
564
602
  customTools;
603
+ /** 当前可用(已展开集合)的顶层工具列表,传给 PromptManager 渲染 */
604
+ activeCustomTools;
605
+ /** 所有已知工具名(含未激活 children),便于查找定义 */
606
+ customToolsByName;
565
607
  customToolNames;
566
608
  allowDefaultTools;
567
609
  customInstructions;
610
+ /** 已激活的工具集入口名 */
611
+ activatedGroups = /* @__PURE__ */ new Set();
612
+ /** activeCustomTools 变更版本号,用于 Prompt 缓存失效 */
613
+ activeToolsVersion = 0;
614
+ /** 缓存 sysPrompt 时记录的版本号 */
615
+ promptActiveToolsVersion = -1;
568
616
  sysPrompt;
569
617
  noUseTollSysPrompt;
570
618
  // 简单的事件回调系统,供宿主环境注册以监听 Agent 内部事件
@@ -584,31 +632,38 @@ var ClineAgent = class {
584
632
  this.allowDefaultTools.push("agent_attempt_completion" /* ATTEMPT */);
585
633
  }
586
634
  this.customInstructions = options.customInstructions;
635
+ this.customToolsByName = /* @__PURE__ */ new Map();
636
+ this.activeCustomTools = [...this.customTools];
637
+ this.customToolNames = new Set(collectCustomToolNames(this.customTools));
587
638
  if (this.customTools.length > 0) {
588
- const dynamicToolNames = /* @__PURE__ */ new Set();
589
639
  const customToolParams = /* @__PURE__ */ new Set();
590
- for (const customTool of this.customTools) {
591
- dynamicToolNames.add(customTool.name);
592
- if (customTool.params) {
593
- for (const key in customTool.params) {
594
- if (!Object.hasOwn(customTool.params, key)) continue;
595
- customToolParams.add(key);
640
+ const visit = (tools) => {
641
+ for (const customTool of tools) {
642
+ this.customToolsByName.set(customTool.name, customTool);
643
+ if (customTool.params) {
644
+ for (const key in customTool.params) {
645
+ if (!Object.hasOwn(customTool.params, key)) continue;
646
+ customToolParams.add(key);
647
+ }
648
+ }
649
+ if (Array.isArray(customTool.children) && customTool.children.length > 0) {
650
+ visit(customTool.children);
596
651
  }
597
652
  }
598
- }
653
+ };
654
+ visit(this.customTools);
599
655
  const defaultToolNames = Object.values(AgentDefaultTool);
600
- const overlappingTools = Array.from(dynamicToolNames).filter((name) => defaultToolNames.includes(name));
656
+ const overlappingTools = Array.from(this.customToolNames).filter((name) => defaultToolNames.includes(name));
601
657
  if (overlappingTools.length > 0) {
602
658
  throw new Error(`Custom tools and default tools have overlapping names: ${overlappingTools.join(", ")}`);
603
659
  }
604
- if (dynamicToolNames.size > 0) {
605
- setDynamicToolUseNames("customUser", Array.from(dynamicToolNames));
660
+ if (this.customToolNames.size > 0) {
661
+ setDynamicToolUseNames("customUser", Array.from(this.customToolNames));
606
662
  }
607
663
  if (customToolParams.size > 0) {
608
664
  this.allToolParamNames = Array.from(/* @__PURE__ */ new Set([...this.allToolParamNames, ...Array.from(customToolParams)]));
609
665
  }
610
666
  }
611
- this.customToolNames = new Set(this.customTools.map((t) => t.name));
612
667
  this.skillDir = this.runtime.path.join(this.runtime.env.cwd(), ".agents/skills");
613
668
  this.rulesDir = this.runtime.path.join(this.runtime.env.cwd(), ".agents/rules");
614
669
  }
@@ -632,10 +687,11 @@ var ClineAgent = class {
632
687
  shell: this.runtime.env.shell(),
633
688
  skills: await this.getSkills(),
634
689
  agentRules: await this.getAgentRules(),
635
- customTools: this.customTools,
690
+ customTools: this.activeCustomTools,
636
691
  allowDefaultTools: this.allowDefaultTools,
637
692
  customInstructions: this.customInstructions
638
693
  };
694
+ this.promptActiveToolsVersion = this.activeToolsVersion;
639
695
  this.sysPrompt = await this.promptManager.getSystemPrompt(context);
640
696
  this.noUseTollSysPrompt = this.promptManager.getNoUseToolInstructions(context);
641
697
  return { sysPrompt: this.sysPrompt, noUseTollSysPrompt: this.noUseTollSysPrompt };
@@ -744,9 +800,9 @@ ${content.trim()}\`\`\``);
744
800
  * 核心任务循环
745
801
  */
746
802
  async loop() {
747
- const { sysPrompt, noUseTollSysPrompt } = await this.getPrompt(true);
748
- const { customTools, customToolNames } = this;
803
+ const { customToolsByName, customToolNames } = this;
749
804
  while (this.state === "thinking" /* THINKING */) {
805
+ const { sysPrompt, noUseTollSysPrompt } = await this.getCurrentPrompt();
750
806
  this.loopCount++;
751
807
  if (this.loopCount > this.maxLoopCount) {
752
808
  await this.memory.appendMessage({
@@ -791,7 +847,33 @@ ${content.trim()}\`\`\``);
791
847
  }
792
848
  }
793
849
  if (customToolNames.has(tool.name)) {
794
- const customTool = customTools.find((t) => t.name === tool.name);
850
+ const customTool = customToolsByName.get(tool.name);
851
+ if (!customTool) {
852
+ toolResults.push({ tool, result: `Error: Custom tool "${tool.name}" not found.` });
853
+ continue;
854
+ }
855
+ if (isCustomToolGroup(customTool)) {
856
+ let groupResult;
857
+ if (customTool.onCall) {
858
+ try {
859
+ groupResult = await customTool.onCall(tool.params);
860
+ } catch (error) {
861
+ toolResults.push({ tool, result: `Error: ${error.message}` });
862
+ continue;
863
+ }
864
+ }
865
+ const childrenPrompt = this.activateCustomToolGroup(customTool);
866
+ const finalPrompt = `# New Available Tools
867
+
868
+ The following are instructions for using the new available tools.
869
+
870
+ ${childrenPrompt}`;
871
+ const promptBlock = groupResult !== void 0 ? `${typeof groupResult === "string" ? groupResult : JSON.stringify(groupResult)}
872
+
873
+ ${finalPrompt}` : finalPrompt;
874
+ toolResults.push({ tool, result: promptBlock });
875
+ continue;
876
+ }
795
877
  if (customTool.onCall) {
796
878
  try {
797
879
  const res = await customTool.onCall(tool.params);
@@ -1052,6 +1134,27 @@ ${this.promptManager.getUsedToolResultInstructions()}`;
1052
1134
  }
1053
1135
  }
1054
1136
  }
1137
+ /**
1138
+ * 激活自定义工具集:将入口的 children 注册到活跃工具列表并返回渲染后的 prompt。
1139
+ */
1140
+ activateCustomToolGroup(group) {
1141
+ if (this.activatedGroups.has(group.name)) {
1142
+ return renderCustomToolsPrompt(group.children || [], this.runtime.env.cwd());
1143
+ }
1144
+ this.activatedGroups.add(group.name);
1145
+ this.activeCustomTools = [...this.activeCustomTools, ...group.children || []];
1146
+ this.activeToolsVersion++;
1147
+ return renderCustomToolsPrompt(group.children || [], this.runtime.env.cwd());
1148
+ }
1149
+ /**
1150
+ * 检查自上次获取 prompt 后是否有工具集被激活,若有则刷新缓存。
1151
+ */
1152
+ async getCurrentPrompt() {
1153
+ if (this.promptActiveToolsVersion !== this.activeToolsVersion) {
1154
+ return this.getPrompt(true);
1155
+ }
1156
+ return this.getPrompt();
1157
+ }
1055
1158
  /**
1056
1159
  * 简单的 Diff 解析与替换辅助方法
1057
1160
  */
@@ -1218,6 +1321,6 @@ ${searchBlock.substring(0, 100)}...`);
1218
1321
  var VERSION = "0.1.0";
1219
1322
  globalThis.AI_SDK_LOG_WARNINGS = false;
1220
1323
 
1221
- export { AgentDefaultTool, AgentState, ClineAgent, READ_ONLY_TOOLS, TOOL_RESULT_BLOCK, VERSION, defaultTools, getToolUseNames, parseAssistantMessageV2, setDynamicToolUseNames, toolParamNames, toolUseNames };
1324
+ export { AgentDefaultTool, AgentState, ClineAgent, READ_ONLY_TOOLS, TOOL_RESULT_BLOCK, VERSION, collectCustomToolNames, defaultTools, getToolUseNames, isCustomToolGroup, parseAssistantMessageV2, renderCustomToolPrompt, renderCustomToolsPrompt, setDynamicToolUseNames, toolParamNames, toolUseNames };
1222
1325
  //# sourceMappingURL=index.mjs.map
1223
1326
  //# sourceMappingURL=index.mjs.map