@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.d.mts CHANGED
@@ -180,6 +180,43 @@ declare function setDynamicToolUseNames(namespace: string, names: string[]): voi
180
180
  declare function getToolUseNames(): string[];
181
181
  declare const READ_ONLY_TOOLS: readonly [AgentDefaultTool.LIST_FILES, AgentDefaultTool.FILE_READ, AgentDefaultTool.SEARCH, AgentDefaultTool.SEARCH_CONTENT, AgentDefaultTool.LIST_CODE_DEF, AgentDefaultTool.BROWSER, AgentDefaultTool.ASK, AgentDefaultTool.WEB_SEARCH, AgentDefaultTool.WEB_FETCH, AgentDefaultTool.LOAD_SKILL, AgentDefaultTool.USE_SUBAGENTS];
182
182
 
183
+ /**
184
+ * 自定义工具的数据结构与渲染辅助函数。
185
+ *
186
+ * 自定义工具支持“工具集(tool group)”结构:
187
+ * - 顶层只展示工具集入口(name/description/params),避免系统提示词臃肿。
188
+ * - 当 Agent 调用某个工具集入口时,才把该集合的 children 注册为可用工具,
189
+ * 并将其 prompt 描述作为工具调用结果返回给模型。
190
+ */
191
+ interface CustomToolDefinition {
192
+ name: string;
193
+ description: string;
194
+ params?: Record<string, string>;
195
+ onCall?: (params: Record<string, any>) => Promise<any> | any;
196
+ /**
197
+ * 子工具。存在时表示该工具为“工具集入口”,初始不展开 children。
198
+ */
199
+ children?: CustomToolDefinition[];
200
+ }
201
+ /**
202
+ * 判断一个自定义工具是否为工具集入口。
203
+ */
204
+ declare function isCustomToolGroup(tool: CustomToolDefinition): boolean;
205
+ /**
206
+ * 递归收集所有自定义工具名(含嵌套 children),用于全局唯一性校验。
207
+ */
208
+ declare function collectCustomToolNames(tools: CustomToolDefinition[]): string[];
209
+ /**
210
+ * 将单个工具定义渲染为系统提示词中的工具说明块。
211
+ *
212
+ * 注意:该函数只渲染工具自身的 name/description/params,不会展开 children。
213
+ */
214
+ declare function renderCustomToolPrompt(tool: CustomToolDefinition, cwd?: string): string;
215
+ /**
216
+ * 批量渲染工具说明。
217
+ */
218
+ declare function renderCustomToolsPrompt(tools: CustomToolDefinition[], cwd?: string): string;
219
+
183
220
  /**
184
221
  * @description **Version 2**
185
222
  * Parses an assistant message string potentially containing mixed text and tool usage blocks
@@ -417,7 +454,7 @@ interface AgentOptions {
417
454
  promptManager: IPromptManager;
418
455
  memory: IMemoryManager;
419
456
  mcp?: IMcpManager;
420
- customTools?: any[];
457
+ customTools?: CustomToolDefinition[];
421
458
  allowDefaultTools?: string[];
422
459
  customInstructions?: string;
423
460
  /** 最大循环次数,默认150 */
@@ -451,10 +488,20 @@ declare class ClineAgent {
451
488
  private maxLoopCount;
452
489
  skillDir: string;
453
490
  rulesDir: string;
454
- customTools: any[];
491
+ customTools: CustomToolDefinition[];
492
+ /** 当前可用(已展开集合)的顶层工具列表,传给 PromptManager 渲染 */
493
+ activeCustomTools: CustomToolDefinition[];
494
+ /** 所有已知工具名(含未激活 children),便于查找定义 */
495
+ customToolsByName: Map<string, CustomToolDefinition>;
455
496
  customToolNames: Set<string>;
456
497
  allowDefaultTools?: string[];
457
498
  customInstructions?: string;
499
+ /** 已激活的工具集入口名 */
500
+ private activatedGroups;
501
+ /** activeCustomTools 变更版本号,用于 Prompt 缓存失效 */
502
+ private activeToolsVersion;
503
+ /** 缓存 sysPrompt 时记录的版本号 */
504
+ private promptActiveToolsVersion;
458
505
  sysPrompt?: string;
459
506
  noUseTollSysPrompt?: string;
460
507
  onStateChange?: (state: AgentState) => void;
@@ -486,6 +533,14 @@ declare class ClineAgent {
486
533
  * 核心任务循环
487
534
  */
488
535
  private loop;
536
+ /**
537
+ * 激活自定义工具集:将入口的 children 注册到活跃工具列表并返回渲染后的 prompt。
538
+ */
539
+ private activateCustomToolGroup;
540
+ /**
541
+ * 检查自上次获取 prompt 后是否有工具集被激活,若有则刷新缓存。
542
+ */
543
+ private getCurrentPrompt;
489
544
  /**
490
545
  * 简单的 Diff 解析与替换辅助方法
491
546
  */
@@ -513,4 +568,4 @@ declare class ClineAgent {
513
568
 
514
569
  declare const VERSION = "0.1.0";
515
570
 
516
- export { AgentDefaultTool, type AgentMessage, type AgentOptions, AgentState, type ApiProviderInfo, type ApiStreamChunk, type AssistantMessageContent, ClineAgent, type IAgentRuntime, type IApiHandler, type IMcpManager, type IMemoryManager, type IPromptManager, type McpServerInfo, type McpTool, type McpToolCallResult, READ_ONLY_TOOLS, type ReasoningStreamContent, TOOL_RESULT_BLOCK, type TextStreamContent, type ToolParamName, type ToolUse, VERSION, defaultTools, getToolUseNames, parseAssistantMessageV2, setDynamicToolUseNames, toolParamNames, toolUseNames };
571
+ export { AgentDefaultTool, type AgentMessage, type AgentOptions, AgentState, type ApiProviderInfo, type ApiStreamChunk, type AssistantMessageContent, ClineAgent, type CustomToolDefinition, type IAgentRuntime, type IApiHandler, type IMcpManager, type IMemoryManager, type IPromptManager, type McpServerInfo, type McpTool, type McpToolCallResult, READ_ONLY_TOOLS, type ReasoningStreamContent, TOOL_RESULT_BLOCK, type TextStreamContent, type ToolParamName, type ToolUse, VERSION, collectCustomToolNames, defaultTools, getToolUseNames, isCustomToolGroup, parseAssistantMessageV2, renderCustomToolPrompt, renderCustomToolsPrompt, setDynamicToolUseNames, toolParamNames, toolUseNames };
package/dist/index.d.ts CHANGED
@@ -180,6 +180,43 @@ declare function setDynamicToolUseNames(namespace: string, names: string[]): voi
180
180
  declare function getToolUseNames(): string[];
181
181
  declare const READ_ONLY_TOOLS: readonly [AgentDefaultTool.LIST_FILES, AgentDefaultTool.FILE_READ, AgentDefaultTool.SEARCH, AgentDefaultTool.SEARCH_CONTENT, AgentDefaultTool.LIST_CODE_DEF, AgentDefaultTool.BROWSER, AgentDefaultTool.ASK, AgentDefaultTool.WEB_SEARCH, AgentDefaultTool.WEB_FETCH, AgentDefaultTool.LOAD_SKILL, AgentDefaultTool.USE_SUBAGENTS];
182
182
 
183
+ /**
184
+ * 自定义工具的数据结构与渲染辅助函数。
185
+ *
186
+ * 自定义工具支持“工具集(tool group)”结构:
187
+ * - 顶层只展示工具集入口(name/description/params),避免系统提示词臃肿。
188
+ * - 当 Agent 调用某个工具集入口时,才把该集合的 children 注册为可用工具,
189
+ * 并将其 prompt 描述作为工具调用结果返回给模型。
190
+ */
191
+ interface CustomToolDefinition {
192
+ name: string;
193
+ description: string;
194
+ params?: Record<string, string>;
195
+ onCall?: (params: Record<string, any>) => Promise<any> | any;
196
+ /**
197
+ * 子工具。存在时表示该工具为“工具集入口”,初始不展开 children。
198
+ */
199
+ children?: CustomToolDefinition[];
200
+ }
201
+ /**
202
+ * 判断一个自定义工具是否为工具集入口。
203
+ */
204
+ declare function isCustomToolGroup(tool: CustomToolDefinition): boolean;
205
+ /**
206
+ * 递归收集所有自定义工具名(含嵌套 children),用于全局唯一性校验。
207
+ */
208
+ declare function collectCustomToolNames(tools: CustomToolDefinition[]): string[];
209
+ /**
210
+ * 将单个工具定义渲染为系统提示词中的工具说明块。
211
+ *
212
+ * 注意:该函数只渲染工具自身的 name/description/params,不会展开 children。
213
+ */
214
+ declare function renderCustomToolPrompt(tool: CustomToolDefinition, cwd?: string): string;
215
+ /**
216
+ * 批量渲染工具说明。
217
+ */
218
+ declare function renderCustomToolsPrompt(tools: CustomToolDefinition[], cwd?: string): string;
219
+
183
220
  /**
184
221
  * @description **Version 2**
185
222
  * Parses an assistant message string potentially containing mixed text and tool usage blocks
@@ -417,7 +454,7 @@ interface AgentOptions {
417
454
  promptManager: IPromptManager;
418
455
  memory: IMemoryManager;
419
456
  mcp?: IMcpManager;
420
- customTools?: any[];
457
+ customTools?: CustomToolDefinition[];
421
458
  allowDefaultTools?: string[];
422
459
  customInstructions?: string;
423
460
  /** 最大循环次数,默认150 */
@@ -451,10 +488,20 @@ declare class ClineAgent {
451
488
  private maxLoopCount;
452
489
  skillDir: string;
453
490
  rulesDir: string;
454
- customTools: any[];
491
+ customTools: CustomToolDefinition[];
492
+ /** 当前可用(已展开集合)的顶层工具列表,传给 PromptManager 渲染 */
493
+ activeCustomTools: CustomToolDefinition[];
494
+ /** 所有已知工具名(含未激活 children),便于查找定义 */
495
+ customToolsByName: Map<string, CustomToolDefinition>;
455
496
  customToolNames: Set<string>;
456
497
  allowDefaultTools?: string[];
457
498
  customInstructions?: string;
499
+ /** 已激活的工具集入口名 */
500
+ private activatedGroups;
501
+ /** activeCustomTools 变更版本号,用于 Prompt 缓存失效 */
502
+ private activeToolsVersion;
503
+ /** 缓存 sysPrompt 时记录的版本号 */
504
+ private promptActiveToolsVersion;
458
505
  sysPrompt?: string;
459
506
  noUseTollSysPrompt?: string;
460
507
  onStateChange?: (state: AgentState) => void;
@@ -486,6 +533,14 @@ declare class ClineAgent {
486
533
  * 核心任务循环
487
534
  */
488
535
  private loop;
536
+ /**
537
+ * 激活自定义工具集:将入口的 children 注册到活跃工具列表并返回渲染后的 prompt。
538
+ */
539
+ private activateCustomToolGroup;
540
+ /**
541
+ * 检查自上次获取 prompt 后是否有工具集被激活,若有则刷新缓存。
542
+ */
543
+ private getCurrentPrompt;
489
544
  /**
490
545
  * 简单的 Diff 解析与替换辅助方法
491
546
  */
@@ -513,4 +568,4 @@ declare class ClineAgent {
513
568
 
514
569
  declare const VERSION = "0.1.0";
515
570
 
516
- export { AgentDefaultTool, type AgentMessage, type AgentOptions, AgentState, type ApiProviderInfo, type ApiStreamChunk, type AssistantMessageContent, ClineAgent, type IAgentRuntime, type IApiHandler, type IMcpManager, type IMemoryManager, type IPromptManager, type McpServerInfo, type McpTool, type McpToolCallResult, READ_ONLY_TOOLS, type ReasoningStreamContent, TOOL_RESULT_BLOCK, type TextStreamContent, type ToolParamName, type ToolUse, VERSION, defaultTools, getToolUseNames, parseAssistantMessageV2, setDynamicToolUseNames, toolParamNames, toolUseNames };
571
+ export { AgentDefaultTool, type AgentMessage, type AgentOptions, AgentState, type ApiProviderInfo, type ApiStreamChunk, type AssistantMessageContent, ClineAgent, type CustomToolDefinition, type IAgentRuntime, type IApiHandler, type IMcpManager, type IMemoryManager, type IPromptManager, type McpServerInfo, type McpTool, type McpToolCallResult, READ_ONLY_TOOLS, type ReasoningStreamContent, TOOL_RESULT_BLOCK, type TextStreamContent, type ToolParamName, type ToolUse, VERSION, collectCustomToolNames, defaultTools, getToolUseNames, isCustomToolGroup, parseAssistantMessageV2, renderCustomToolPrompt, renderCustomToolsPrompt, setDynamicToolUseNames, toolParamNames, toolUseNames };
package/dist/index.js CHANGED
@@ -285,6 +285,44 @@ var READ_ONLY_TOOLS = [
285
285
  "agent_load_skill" /* LOAD_SKILL */,
286
286
  "agent_use_subagents" /* USE_SUBAGENTS */
287
287
  ];
288
+
289
+ // src/shared/custom-tools.ts
290
+ function isCustomToolGroup(tool) {
291
+ return Array.isArray(tool.children) && tool.children.length > 0;
292
+ }
293
+ function collectCustomToolNames(tools) {
294
+ const names = [];
295
+ const visit = (list) => {
296
+ for (const tool of list) {
297
+ names.push(tool.name);
298
+ if (Array.isArray(tool.children) && tool.children.length > 0) {
299
+ visit(tool.children);
300
+ }
301
+ }
302
+ };
303
+ visit(tools);
304
+ return names;
305
+ }
306
+ function renderCustomToolPrompt(tool, cwd = "") {
307
+ const params = tool.params ? Object.entries(tool.params) : [];
308
+ const replaceCwd = (text) => String(text).replaceAll("${context.cwd}", cwd);
309
+ const paramLines = params.map(([name, description]) => `- ${name}: ${replaceCwd(description)}`).join("\n");
310
+ const usageLines = params.map(([name, description]) => `<${name}>${replaceCwd(description)}</${name}>`).join("\n");
311
+ return `### ${tool.name}
312
+ Description: ${tool.description}
313
+ Parameters:
314
+ ${paramLines}
315
+ - 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)
316
+ Usage:
317
+ <${tool.name}>
318
+ ${usageLines}
319
+ <task_progress>Checklist here (optional)</task_progress>
320
+ </${tool.name}>
321
+ `;
322
+ }
323
+ function renderCustomToolsPrompt(tools, cwd = "") {
324
+ return tools.map((tool) => renderCustomToolPrompt(tool, cwd)).join("\n");
325
+ }
288
326
  function parseAssistantMessageV2(assistantMessage, toolParamNames2) {
289
327
  const contentBlocks = [];
290
328
  let currentTextContentStart = 0;
@@ -568,9 +606,19 @@ var ClineAgent = class {
568
606
  skillDir;
569
607
  rulesDir;
570
608
  customTools;
609
+ /** 当前可用(已展开集合)的顶层工具列表,传给 PromptManager 渲染 */
610
+ activeCustomTools;
611
+ /** 所有已知工具名(含未激活 children),便于查找定义 */
612
+ customToolsByName;
571
613
  customToolNames;
572
614
  allowDefaultTools;
573
615
  customInstructions;
616
+ /** 已激活的工具集入口名 */
617
+ activatedGroups = /* @__PURE__ */ new Set();
618
+ /** activeCustomTools 变更版本号,用于 Prompt 缓存失效 */
619
+ activeToolsVersion = 0;
620
+ /** 缓存 sysPrompt 时记录的版本号 */
621
+ promptActiveToolsVersion = -1;
574
622
  sysPrompt;
575
623
  noUseTollSysPrompt;
576
624
  // 简单的事件回调系统,供宿主环境注册以监听 Agent 内部事件
@@ -590,31 +638,38 @@ var ClineAgent = class {
590
638
  this.allowDefaultTools.push("agent_attempt_completion" /* ATTEMPT */);
591
639
  }
592
640
  this.customInstructions = options.customInstructions;
641
+ this.customToolsByName = /* @__PURE__ */ new Map();
642
+ this.activeCustomTools = [...this.customTools];
643
+ this.customToolNames = new Set(collectCustomToolNames(this.customTools));
593
644
  if (this.customTools.length > 0) {
594
- const dynamicToolNames = /* @__PURE__ */ new Set();
595
645
  const customToolParams = /* @__PURE__ */ new Set();
596
- for (const customTool of this.customTools) {
597
- dynamicToolNames.add(customTool.name);
598
- if (customTool.params) {
599
- for (const key in customTool.params) {
600
- if (!Object.hasOwn(customTool.params, key)) continue;
601
- customToolParams.add(key);
646
+ const visit = (tools) => {
647
+ for (const customTool of tools) {
648
+ this.customToolsByName.set(customTool.name, customTool);
649
+ if (customTool.params) {
650
+ for (const key in customTool.params) {
651
+ if (!Object.hasOwn(customTool.params, key)) continue;
652
+ customToolParams.add(key);
653
+ }
654
+ }
655
+ if (Array.isArray(customTool.children) && customTool.children.length > 0) {
656
+ visit(customTool.children);
602
657
  }
603
658
  }
604
- }
659
+ };
660
+ visit(this.customTools);
605
661
  const defaultToolNames = Object.values(AgentDefaultTool);
606
- const overlappingTools = Array.from(dynamicToolNames).filter((name) => defaultToolNames.includes(name));
662
+ const overlappingTools = Array.from(this.customToolNames).filter((name) => defaultToolNames.includes(name));
607
663
  if (overlappingTools.length > 0) {
608
664
  throw new Error(`Custom tools and default tools have overlapping names: ${overlappingTools.join(", ")}`);
609
665
  }
610
- if (dynamicToolNames.size > 0) {
611
- setDynamicToolUseNames("customUser", Array.from(dynamicToolNames));
666
+ if (this.customToolNames.size > 0) {
667
+ setDynamicToolUseNames("customUser", Array.from(this.customToolNames));
612
668
  }
613
669
  if (customToolParams.size > 0) {
614
670
  this.allToolParamNames = Array.from(/* @__PURE__ */ new Set([...this.allToolParamNames, ...Array.from(customToolParams)]));
615
671
  }
616
672
  }
617
- this.customToolNames = new Set(this.customTools.map((t) => t.name));
618
673
  this.skillDir = this.runtime.path.join(this.runtime.env.cwd(), ".agents/skills");
619
674
  this.rulesDir = this.runtime.path.join(this.runtime.env.cwd(), ".agents/rules");
620
675
  }
@@ -638,10 +693,11 @@ var ClineAgent = class {
638
693
  shell: this.runtime.env.shell(),
639
694
  skills: await this.getSkills(),
640
695
  agentRules: await this.getAgentRules(),
641
- customTools: this.customTools,
696
+ customTools: this.activeCustomTools,
642
697
  allowDefaultTools: this.allowDefaultTools,
643
698
  customInstructions: this.customInstructions
644
699
  };
700
+ this.promptActiveToolsVersion = this.activeToolsVersion;
645
701
  this.sysPrompt = await this.promptManager.getSystemPrompt(context);
646
702
  this.noUseTollSysPrompt = this.promptManager.getNoUseToolInstructions(context);
647
703
  return { sysPrompt: this.sysPrompt, noUseTollSysPrompt: this.noUseTollSysPrompt };
@@ -750,9 +806,9 @@ ${content.trim()}\`\`\``);
750
806
  * 核心任务循环
751
807
  */
752
808
  async loop() {
753
- const { sysPrompt, noUseTollSysPrompt } = await this.getPrompt(true);
754
- const { customTools, customToolNames } = this;
809
+ const { customToolsByName, customToolNames } = this;
755
810
  while (this.state === "thinking" /* THINKING */) {
811
+ const { sysPrompt, noUseTollSysPrompt } = await this.getCurrentPrompt();
756
812
  this.loopCount++;
757
813
  if (this.loopCount > this.maxLoopCount) {
758
814
  await this.memory.appendMessage({
@@ -797,7 +853,33 @@ ${content.trim()}\`\`\``);
797
853
  }
798
854
  }
799
855
  if (customToolNames.has(tool.name)) {
800
- const customTool = customTools.find((t) => t.name === tool.name);
856
+ const customTool = customToolsByName.get(tool.name);
857
+ if (!customTool) {
858
+ toolResults.push({ tool, result: `Error: Custom tool "${tool.name}" not found.` });
859
+ continue;
860
+ }
861
+ if (isCustomToolGroup(customTool)) {
862
+ let groupResult;
863
+ if (customTool.onCall) {
864
+ try {
865
+ groupResult = await customTool.onCall(tool.params);
866
+ } catch (error) {
867
+ toolResults.push({ tool, result: `Error: ${error.message}` });
868
+ continue;
869
+ }
870
+ }
871
+ const childrenPrompt = this.activateCustomToolGroup(customTool);
872
+ const finalPrompt = `# New Available Tools
873
+
874
+ The following are instructions for using the new available tools.
875
+
876
+ ${childrenPrompt}`;
877
+ const promptBlock = groupResult !== void 0 ? `${typeof groupResult === "string" ? groupResult : JSON.stringify(groupResult)}
878
+
879
+ ${finalPrompt}` : finalPrompt;
880
+ toolResults.push({ tool, result: promptBlock });
881
+ continue;
882
+ }
801
883
  if (customTool.onCall) {
802
884
  try {
803
885
  const res = await customTool.onCall(tool.params);
@@ -1058,6 +1140,27 @@ ${this.promptManager.getUsedToolResultInstructions()}`;
1058
1140
  }
1059
1141
  }
1060
1142
  }
1143
+ /**
1144
+ * 激活自定义工具集:将入口的 children 注册到活跃工具列表并返回渲染后的 prompt。
1145
+ */
1146
+ activateCustomToolGroup(group) {
1147
+ if (this.activatedGroups.has(group.name)) {
1148
+ return renderCustomToolsPrompt(group.children || [], this.runtime.env.cwd());
1149
+ }
1150
+ this.activatedGroups.add(group.name);
1151
+ this.activeCustomTools = [...this.activeCustomTools, ...group.children || []];
1152
+ this.activeToolsVersion++;
1153
+ return renderCustomToolsPrompt(group.children || [], this.runtime.env.cwd());
1154
+ }
1155
+ /**
1156
+ * 检查自上次获取 prompt 后是否有工具集被激活,若有则刷新缓存。
1157
+ */
1158
+ async getCurrentPrompt() {
1159
+ if (this.promptActiveToolsVersion !== this.activeToolsVersion) {
1160
+ return this.getPrompt(true);
1161
+ }
1162
+ return this.getPrompt();
1163
+ }
1061
1164
  /**
1062
1165
  * 简单的 Diff 解析与替换辅助方法
1063
1166
  */
@@ -1230,9 +1333,13 @@ exports.ClineAgent = ClineAgent;
1230
1333
  exports.READ_ONLY_TOOLS = READ_ONLY_TOOLS;
1231
1334
  exports.TOOL_RESULT_BLOCK = TOOL_RESULT_BLOCK;
1232
1335
  exports.VERSION = VERSION;
1336
+ exports.collectCustomToolNames = collectCustomToolNames;
1233
1337
  exports.defaultTools = defaultTools;
1234
1338
  exports.getToolUseNames = getToolUseNames;
1339
+ exports.isCustomToolGroup = isCustomToolGroup;
1235
1340
  exports.parseAssistantMessageV2 = parseAssistantMessageV2;
1341
+ exports.renderCustomToolPrompt = renderCustomToolPrompt;
1342
+ exports.renderCustomToolsPrompt = renderCustomToolsPrompt;
1236
1343
  exports.setDynamicToolUseNames = setDynamicToolUseNames;
1237
1344
  exports.toolParamNames = toolParamNames;
1238
1345
  exports.toolUseNames = toolUseNames;