@ai-zen/agents-sdk 0.11.0 → 1.0.0-alpha.0
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/CHANGELOG.md +85 -24
- package/README.md +37 -26
- package/README.zh.md +37 -26
- package/dist/{plugin → agent-plugins}/AutoRefreshToolsPlugin.js +2 -2
- package/dist/{plugin → agent-plugins}/UnknownToolHintPlugin.d.ts +7 -5
- package/dist/{plugin → agent-plugins}/UnknownToolHintPlugin.js +9 -5
- package/dist/config/ConfigManager.d.ts +3 -1
- package/dist/config/ConfigManager.js +31 -4
- package/dist/config/constants.js +6 -1
- package/dist/index.d.ts +37 -29
- package/dist/index.js +28 -21
- package/dist/runtime/SdkAgent.d.ts +5 -5
- package/dist/runtime/SdkAgent.js +4 -4
- package/dist/runtime/TaskMigrationService.d.ts +1 -1
- package/dist/runtime/TaskMigrationService.js +2 -2
- package/dist/runtime/createAgent.d.ts +4 -4
- package/dist/runtime/createAgent.js +8 -8
- package/dist/runtime/createModel.d.ts +3 -3
- package/dist/runtime/createModel.js +3 -3
- package/dist/scope/Scope.d.ts +111 -0
- package/dist/scope/Scope.js +165 -0
- package/dist/scope/ScopePlugin.d.ts +51 -0
- package/dist/scope/ScopePlugin.js +1 -0
- package/dist/scope/plugins/allInOne.d.ts +18 -0
- package/dist/scope/plugins/allInOne.js +23 -0
- package/dist/{capabilities/implements → scope/plugins}/builtin/BatchEditTool.d.ts +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/BatchEditTool.js +12 -10
- package/dist/scope/plugins/builtin/BuiltinToolsScopePlugin.d.ts +20 -0
- package/dist/scope/plugins/builtin/BuiltinToolsScopePlugin.js +30 -0
- package/dist/{capabilities/implements → scope/plugins}/builtin/CopyTool.d.ts +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/CopyTool.js +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/CwdTool.d.ts +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/CwdTool.js +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/DownloadFileTool.d.ts +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/DownloadFileTool.js +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/EditTool.d.ts +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/EditTool.js +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/ExecAsyncTool.d.ts +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/ExecAsyncTool.js +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/ExecTool.d.ts +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/ExecTool.js +34 -2
- package/dist/{capabilities/implements → scope/plugins}/builtin/ExistTool.d.ts +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/ExistTool.js +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/FindTextTool.d.ts +6 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/FindTextTool.js +29 -4
- package/dist/{capabilities/implements → scope/plugins}/builtin/GenerateImageTool.d.ts +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/GenerateImageTool.js +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/GlobTool.d.ts +6 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/GlobTool.js +29 -3
- package/dist/scope/plugins/builtin/InspectFileTool.d.ts +17 -0
- package/dist/scope/plugins/builtin/InspectFileTool.js +150 -0
- package/dist/{capabilities/implements → scope/plugins}/builtin/LsTool.d.ts +1 -1
- package/dist/scope/plugins/builtin/LsTool.js +52 -0
- package/dist/{capabilities/implements → scope/plugins}/builtin/MkdirTool.d.ts +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/MkdirTool.js +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/ReadFileTool.d.ts +2 -1
- package/dist/scope/plugins/builtin/ReadFileTool.js +94 -0
- package/dist/{capabilities/implements → scope/plugins}/builtin/RenameTool.d.ts +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/RenameTool.js +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/RmTool.d.ts +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/RmTool.js +1 -1
- package/dist/{runtime → scope/plugins/builtin}/SdkCallbackTool.d.ts +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/SleepTool.d.ts +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/SleepTool.js +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/ViewImageTool.d.ts +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/ViewImageTool.js +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/WriteFileTool.d.ts +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/WriteFileTool.js +1 -1
- package/dist/{capabilities/discovery/builtin.d.ts → scope/plugins/builtin/discover.d.ts} +1 -1
- package/dist/{capabilities/discovery/builtin.js → scope/plugins/builtin/discover.js} +1 -1
- package/dist/{capabilities/implements → scope/plugins}/builtin/index.d.ts +5 -4
- package/dist/{capabilities/implements → scope/plugins}/builtin/index.js +5 -3
- package/dist/scope/plugins/builtin/outputGuard.d.ts +81 -0
- package/dist/scope/plugins/builtin/outputGuard.js +73 -0
- package/dist/{runtime → scope/plugins/mcp}/McpConnectionManager.d.ts +1 -1
- package/dist/{runtime → scope/plugins/mcp}/McpConnectionManager.js +1 -1
- package/dist/scope/plugins/mcp/McpScopePlugin.d.ts +29 -0
- package/dist/scope/plugins/mcp/McpScopePlugin.js +60 -0
- package/dist/{capabilities/discovery/mcp.d.ts → scope/plugins/mcp/discover.d.ts} +1 -1
- package/dist/{capabilities/discovery/mcp.js → scope/plugins/mcp/discover.js} +1 -1
- package/dist/{capabilities/implements → scope/plugins/mcp}/mcpTools.d.ts +2 -2
- package/dist/{capabilities/implements → scope/plugins/mcp}/mcpTools.js +2 -2
- package/dist/scope/plugins/skills/SkillsScopePlugin.d.ts +28 -0
- package/dist/scope/plugins/skills/SkillsScopePlugin.js +48 -0
- package/dist/{capabilities/discovery/skills.js → scope/plugins/skills/discover.js} +1 -1
- package/dist/{capabilities/implements → scope/plugins/skills}/skillTools.d.ts +1 -1
- package/dist/{capabilities/implements → scope/plugins/skills}/skillTools.js +3 -3
- package/dist/scope/plugins/subagents/SubAgentsScopePlugin.d.ts +20 -0
- package/dist/scope/plugins/subagents/SubAgentsScopePlugin.js +34 -0
- package/dist/{capabilities/discovery/subagents.d.ts → scope/plugins/subagents/discover.d.ts} +1 -1
- package/dist/{capabilities/implements → scope/plugins/subagents}/subAgentTools.d.ts +3 -3
- package/dist/{capabilities/implements → scope/plugins/subagents}/subAgentTools.js +5 -5
- package/dist/scope/plugins/usertools/UserToolsScopePlugin.d.ts +24 -0
- package/dist/scope/plugins/usertools/UserToolsScopePlugin.js +28 -0
- package/dist/{capabilities/discovery/usertools.js → scope/plugins/usertools/discover.js} +1 -1
- package/dist/types/index.d.ts +17 -2
- package/package.json +3 -3
- package/dist/capabilities/implements/builtin/LsTool.js +0 -31
- package/dist/capabilities/implements/builtin/ReadFileTool.js +0 -35
- package/dist/runtime/Provider.d.ts +0 -119
- package/dist/runtime/Provider.js +0 -218
- /package/dist/{plugin → agent-plugins}/AutoMigratePlugin.d.ts +0 -0
- /package/dist/{plugin → agent-plugins}/AutoMigratePlugin.js +0 -0
- /package/dist/{plugin → agent-plugins}/AutoRefreshToolsPlugin.d.ts +0 -0
- /package/dist/{plugin → agent-plugins}/ContextGuardPlugin.d.ts +0 -0
- /package/dist/{plugin → agent-plugins}/ContextGuardPlugin.js +0 -0
- /package/dist/{capabilities → scope}/PermissionEvaluator.d.ts +0 -0
- /package/dist/{capabilities → scope}/PermissionEvaluator.js +0 -0
- /package/dist/{capabilities → scope}/disclosure.d.ts +0 -0
- /package/dist/{capabilities → scope}/disclosure.js +0 -0
- /package/dist/{runtime → scope/plugins/builtin}/SdkCallbackTool.js +0 -0
- /package/dist/{capabilities/implements → scope/plugins}/builtin/test-helpers.d.ts +0 -0
- /package/dist/{capabilities/implements → scope/plugins}/builtin/test-helpers.js +0 -0
- /package/dist/{capabilities/discovery/skills.d.ts → scope/plugins/skills/discover.d.ts} +0 -0
- /package/dist/{capabilities/discovery/subagents.js → scope/plugins/subagents/discover.js} +0 -0
- /package/dist/{capabilities/discovery/usertools.d.ts → scope/plugins/usertools/discover.d.ts} +0 -0
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { Tool } from "@ai-zen/agents-core";
|
|
2
|
+
import type { AgentDefinition } from "../types/index.js";
|
|
3
|
+
import type { Scope, FilterOutput } from "./Scope.js";
|
|
4
|
+
/**
|
|
5
|
+
* 插件向四维权限体系贡献的候选名称(任意子集)。
|
|
6
|
+
*/
|
|
7
|
+
export interface ScopeCandidates {
|
|
8
|
+
tools?: string[];
|
|
9
|
+
skills?: string[];
|
|
10
|
+
mcps?: string[];
|
|
11
|
+
subagents?: string[];
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* ScopePlugin — 能力插件契约(v1 核心扩展点)。
|
|
15
|
+
*
|
|
16
|
+
* 一个插件**自持一类能力**的完整生命周期:自己发现、自己持有发现结果、
|
|
17
|
+
* 自己声明候选名、自己按过滤结果实例化工具、自己负责释放。
|
|
18
|
+
* `Scope` 只做编排,不感知任何具体能力的发现状态。
|
|
19
|
+
*
|
|
20
|
+
* 与 `AgentPlugin`(core,挂在 Agent 运行时 send/loop/toolcall)不同,
|
|
21
|
+
* `ScopePlugin` 挂在**能力装配管线**(discover/filter/instantiate)。二者互不相关。
|
|
22
|
+
*
|
|
23
|
+
* 生命周期与三阶段管线的对应:
|
|
24
|
+
* discover —— 阶段 1 · 发现
|
|
25
|
+
* candidates —— 阶段 2 · 候选(供权限过滤)
|
|
26
|
+
* instantiate —— 阶段 3 · 实例化
|
|
27
|
+
* dispose —— 释放
|
|
28
|
+
*/
|
|
29
|
+
export interface ScopePlugin {
|
|
30
|
+
/** 唯一标识,如 "builtin" / "user-tools" / "skills" / "mcp" / "subagents" */
|
|
31
|
+
readonly id: string;
|
|
32
|
+
/**
|
|
33
|
+
* 阶段 1 · 发现:扫描来源、刷新自身状态(每次内部重新扫描,不跨 refresh 累积)。
|
|
34
|
+
* 相对路径应在此基于 `scope.cwd` 解析。
|
|
35
|
+
*/
|
|
36
|
+
discover?(scope: Scope, options: {
|
|
37
|
+
silent?: boolean;
|
|
38
|
+
}): Promise<void> | void;
|
|
39
|
+
/**
|
|
40
|
+
* 阶段 2 · 候选:返回本插件向四维贡献的候选名。
|
|
41
|
+
* 可结合 `definition` 做可用性判断(如内置工具的 isAvailable)。同步返回。
|
|
42
|
+
*/
|
|
43
|
+
candidates(scope: Scope, definition: AgentDefinition): ScopeCandidates;
|
|
44
|
+
/**
|
|
45
|
+
* 阶段 3 · 实例化:按过滤后的允许名产出 Tool 实例。
|
|
46
|
+
* `filtered` 为 `Scope.filter()` 的产物(四维允许名)。
|
|
47
|
+
*/
|
|
48
|
+
instantiate(scope: Scope, filtered: FilterOutput): Tool[];
|
|
49
|
+
/** 可选 · 释放:逆序于注册顺序调用。如 MCP 断开所有连接。 */
|
|
50
|
+
dispose?(): Promise<void> | void;
|
|
51
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { ScopePlugin } from "../ScopePlugin.js";
|
|
2
|
+
/** allInOne 的装配选项:各类能力来源的路径(绝对或相对 scope.cwd) */
|
|
3
|
+
export interface AllInOneOptions {
|
|
4
|
+
skillsPaths?: string[];
|
|
5
|
+
toolsPaths?: string[];
|
|
6
|
+
subAgentsPaths?: string[];
|
|
7
|
+
mcpPaths?: string[];
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* 标准能力插件工厂 —— 恒返回 5 个标准插件("全家桶")。
|
|
11
|
+
*
|
|
12
|
+
* 顺序即优先级(去重时后注册覆盖先注册):
|
|
13
|
+
* builtin → user-tools → skills → mcp → subagents
|
|
14
|
+
* 因此用户工具可覆盖同名内置工具(与 v0 一致)。
|
|
15
|
+
*
|
|
16
|
+
* 想裁剪/替换某一类,改用手工逐个 `new XxxScopePlugin(...)` 后 `.use()`。
|
|
17
|
+
*/
|
|
18
|
+
export declare function allInOne(options?: AllInOneOptions): ScopePlugin[];
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { BuiltinToolsScopePlugin } from "./builtin/BuiltinToolsScopePlugin.js";
|
|
2
|
+
import { UserToolsScopePlugin } from "./usertools/UserToolsScopePlugin.js";
|
|
3
|
+
import { SkillsScopePlugin } from "./skills/SkillsScopePlugin.js";
|
|
4
|
+
import { McpScopePlugin } from "./mcp/McpScopePlugin.js";
|
|
5
|
+
import { SubAgentsScopePlugin } from "./subagents/SubAgentsScopePlugin.js";
|
|
6
|
+
/**
|
|
7
|
+
* 标准能力插件工厂 —— 恒返回 5 个标准插件("全家桶")。
|
|
8
|
+
*
|
|
9
|
+
* 顺序即优先级(去重时后注册覆盖先注册):
|
|
10
|
+
* builtin → user-tools → skills → mcp → subagents
|
|
11
|
+
* 因此用户工具可覆盖同名内置工具(与 v0 一致)。
|
|
12
|
+
*
|
|
13
|
+
* 想裁剪/替换某一类,改用手工逐个 `new XxxScopePlugin(...)` 后 `.use()`。
|
|
14
|
+
*/
|
|
15
|
+
export function allInOne(options) {
|
|
16
|
+
return [
|
|
17
|
+
new BuiltinToolsScopePlugin(),
|
|
18
|
+
new UserToolsScopePlugin({ paths: options?.toolsPaths }),
|
|
19
|
+
new SkillsScopePlugin({ paths: options?.skillsPaths }),
|
|
20
|
+
new McpScopePlugin({ paths: options?.mcpPaths }),
|
|
21
|
+
new SubAgentsScopePlugin({ paths: options?.subAgentsPaths }),
|
|
22
|
+
];
|
|
23
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as fsp from "fs/promises";
|
|
2
|
-
import { SdkCallbackTool } from "
|
|
2
|
+
import { SdkCallbackTool } from "./SdkCallbackTool.js";
|
|
3
3
|
export class BatchEditTool extends SdkCallbackTool {
|
|
4
4
|
function = {
|
|
5
5
|
name: "batchEdit",
|
|
@@ -48,13 +48,16 @@ export class BatchEditTool extends SdkCallbackTool {
|
|
|
48
48
|
const filePath = this.resolve(input.path);
|
|
49
49
|
const content = await fsp.readFile(filePath, "utf-8");
|
|
50
50
|
let newContent = content;
|
|
51
|
-
|
|
51
|
+
let replacedCount = 0;
|
|
52
|
+
// 仅回显"有问题"的替换项(未匹配到),便于模型据此修正;
|
|
53
|
+
// 成功项不回显原文与新文,仅计入成功数,避免大段文本回显撑大输出
|
|
54
|
+
const failed = [];
|
|
52
55
|
for (const replacement of input.replacements) {
|
|
53
56
|
if (!newContent.includes(replacement.oldText)) {
|
|
54
|
-
|
|
57
|
+
failed.push({
|
|
55
58
|
oldText: replacement.oldText,
|
|
56
59
|
newText: replacement.newText,
|
|
57
|
-
|
|
60
|
+
reason: "文件中未精确匹配到要替换的文本",
|
|
58
61
|
});
|
|
59
62
|
continue;
|
|
60
63
|
}
|
|
@@ -64,14 +67,13 @@ export class BatchEditTool extends SdkCallbackTool {
|
|
|
64
67
|
else {
|
|
65
68
|
newContent = newContent.replace(replacement.oldText, replacement.newText);
|
|
66
69
|
}
|
|
67
|
-
|
|
68
|
-
oldText: replacement.oldText,
|
|
69
|
-
newText: replacement.newText,
|
|
70
|
-
result: "success",
|
|
71
|
-
});
|
|
70
|
+
replacedCount++;
|
|
72
71
|
}
|
|
73
72
|
await fsp.writeFile(filePath, newContent);
|
|
74
|
-
|
|
73
|
+
if (failed.length === 0) {
|
|
74
|
+
return JSON.stringify({ result: "success", replacedCount });
|
|
75
|
+
}
|
|
76
|
+
return JSON.stringify({ result: "partial", replacedCount, failed });
|
|
75
77
|
}
|
|
76
78
|
catch (error) {
|
|
77
79
|
return error?.message;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Tool } from "@ai-zen/agents-core";
|
|
2
|
+
import type { AgentDefinition } from "../../../types/index.js";
|
|
3
|
+
import type { Scope, FilterOutput } from "../../Scope.js";
|
|
4
|
+
import type { ScopeCandidates, ScopePlugin } from "../../ScopePlugin.js";
|
|
5
|
+
/**
|
|
6
|
+
* 内置工具插件 —— 20 个内置工具类(BUILTIN_TOOL_CLASSES)。
|
|
7
|
+
*
|
|
8
|
+
* 发现层零过滤;可用性由各工具 `isAvailable(config, definition)` 自陈,
|
|
9
|
+
* 在本插件 `candidates()` 阶段按声明剔除(如 GenerateImageTool 依赖 defaultImageModel、
|
|
10
|
+
* ViewImageTool 仅视觉模型可用)。
|
|
11
|
+
*/
|
|
12
|
+
export declare class BuiltinToolsScopePlugin implements ScopePlugin {
|
|
13
|
+
static readonly ID = "builtin";
|
|
14
|
+
readonly id = "builtin";
|
|
15
|
+
/** 发现结果自持(每次 discover 重新实例化) */
|
|
16
|
+
private tools;
|
|
17
|
+
discover(scope: Scope): void;
|
|
18
|
+
candidates(scope: Scope, definition: AgentDefinition): ScopeCandidates;
|
|
19
|
+
instantiate(_scope: Scope, filtered: FilterOutput): Tool[];
|
|
20
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { discoverBuiltinTools } from "./discover.js";
|
|
2
|
+
/**
|
|
3
|
+
* 内置工具插件 —— 20 个内置工具类(BUILTIN_TOOL_CLASSES)。
|
|
4
|
+
*
|
|
5
|
+
* 发现层零过滤;可用性由各工具 `isAvailable(config, definition)` 自陈,
|
|
6
|
+
* 在本插件 `candidates()` 阶段按声明剔除(如 GenerateImageTool 依赖 defaultImageModel、
|
|
7
|
+
* ViewImageTool 仅视觉模型可用)。
|
|
8
|
+
*/
|
|
9
|
+
export class BuiltinToolsScopePlugin {
|
|
10
|
+
static ID = "builtin";
|
|
11
|
+
id = BuiltinToolsScopePlugin.ID;
|
|
12
|
+
/** 发现结果自持(每次 discover 重新实例化) */
|
|
13
|
+
tools = [];
|
|
14
|
+
discover(scope) {
|
|
15
|
+
this.tools = discoverBuiltinTools(scope.env);
|
|
16
|
+
}
|
|
17
|
+
candidates(scope, definition) {
|
|
18
|
+
const tools = this.tools
|
|
19
|
+
.filter((t) => {
|
|
20
|
+
const isAvailable = t?.isAvailable;
|
|
21
|
+
return isAvailable ? isAvailable(scope.config, definition) : true;
|
|
22
|
+
})
|
|
23
|
+
.map((t) => t.function.name);
|
|
24
|
+
return { tools };
|
|
25
|
+
}
|
|
26
|
+
instantiate(_scope, filtered) {
|
|
27
|
+
const allow = new Set(filtered.tools);
|
|
28
|
+
return this.tools.filter((t) => allow.has(t.function.name));
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { SdkCallbackTool } from "
|
|
1
|
+
import { SdkCallbackTool } from "./SdkCallbackTool.js";
|
|
2
2
|
import type { ToolEnv } from "../../../types/index.js";
|
|
3
3
|
import type { AgentNS } from "@ai-zen/agents-core";
|
|
4
4
|
export declare class CopyTool extends SdkCallbackTool {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { SdkCallbackTool } from "
|
|
1
|
+
import { SdkCallbackTool } from "./SdkCallbackTool.js";
|
|
2
2
|
import type { ToolEnv } from "../../../types/index.js";
|
|
3
3
|
import type { AgentNS } from "@ai-zen/agents-core";
|
|
4
4
|
export declare class CwdTool extends SdkCallbackTool {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { SdkCallbackTool } from "
|
|
1
|
+
import { SdkCallbackTool } from "./SdkCallbackTool.js";
|
|
2
2
|
import type { ToolEnv } from "../../../types/index.js";
|
|
3
3
|
import type { AgentNS, ToolCallContext } from "@ai-zen/agents-core";
|
|
4
4
|
export declare class DownloadFileTool extends SdkCallbackTool {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as fsp from "fs/promises";
|
|
2
2
|
import * as path from "path";
|
|
3
|
-
import { SdkCallbackTool } from "
|
|
3
|
+
import { SdkCallbackTool } from "./SdkCallbackTool.js";
|
|
4
4
|
export class DownloadFileTool extends SdkCallbackTool {
|
|
5
5
|
function = {
|
|
6
6
|
name: "downloadFile",
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { SdkCallbackTool } from "
|
|
1
|
+
import { SdkCallbackTool } from "./SdkCallbackTool.js";
|
|
2
2
|
import type { ToolEnv } from "../../../types/index.js";
|
|
3
3
|
import type { AgentNS } from "@ai-zen/agents-core";
|
|
4
4
|
export declare class EditTool extends SdkCallbackTool {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { SdkCallbackTool } from "
|
|
1
|
+
import { SdkCallbackTool } from "./SdkCallbackTool.js";
|
|
2
2
|
import type { ToolEnv } from "../../../types/index.js";
|
|
3
3
|
import type { AgentNS } from "@ai-zen/agents-core";
|
|
4
4
|
export declare class ExecAsyncTool extends SdkCallbackTool {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { SdkCallbackTool } from "
|
|
1
|
+
import { SdkCallbackTool } from "./SdkCallbackTool.js";
|
|
2
2
|
import type { ToolEnv } from "../../../types/index.js";
|
|
3
3
|
import type { AgentNS, ToolCallContext } from "@ai-zen/agents-core";
|
|
4
4
|
export declare class ExecTool extends SdkCallbackTool {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { exec } from "child_process";
|
|
2
|
-
import { SdkCallbackTool } from "
|
|
2
|
+
import { SdkCallbackTool } from "./SdkCallbackTool.js";
|
|
3
|
+
import { DEFAULT_MAX_TOOL_OUTPUT, guardOutput, headTailPreview } from "./outputGuard.js";
|
|
3
4
|
export class ExecTool extends SdkCallbackTool {
|
|
4
5
|
function = {
|
|
5
6
|
name: "exec",
|
|
@@ -81,6 +82,37 @@ export class ExecTool extends SdkCallbackTool {
|
|
|
81
82
|
signal.addEventListener("abort", onAbort, { once: true });
|
|
82
83
|
}
|
|
83
84
|
});
|
|
84
|
-
|
|
85
|
+
// 输出保护:stdout + stderr 合计超过 maxToolOutput 时,分文件落盘并返回警告 + 头尾预览
|
|
86
|
+
const limit = this.env.config.maxToolOutput ?? DEFAULT_MAX_TOOL_OUTPUT;
|
|
87
|
+
const guarded = await guardOutput({
|
|
88
|
+
tool: "exec",
|
|
89
|
+
content: result,
|
|
90
|
+
isOverLimit: (output) => output.stdout.length + output.stderr.length > limit,
|
|
91
|
+
dump: (_dir, output) => [
|
|
92
|
+
{ name: "stdout.log", content: output.stdout },
|
|
93
|
+
{ name: "stderr.log", content: output.stderr },
|
|
94
|
+
],
|
|
95
|
+
buildWarning: (ctx) => {
|
|
96
|
+
const stdout = headTailPreview(result.stdout);
|
|
97
|
+
const stderr = headTailPreview(result.stderr);
|
|
98
|
+
return JSON.stringify({
|
|
99
|
+
outputTooLarge: true,
|
|
100
|
+
warning: `输出过大:stdout + stderr 合计 ${result.stdout.length + result.stderr.length} 字符,` +
|
|
101
|
+
`超过上限 ${limit} 字符。完整内容已分文件落盘(stdout.log / stderr.log),可用 readFile 分批读取。`,
|
|
102
|
+
files: ctx.files,
|
|
103
|
+
stdout: { chars: result.stdout.length, lines: countLines(result.stdout), ...stdout },
|
|
104
|
+
stderr: { chars: result.stderr.length, lines: countLines(result.stderr), ...stderr },
|
|
105
|
+
exitCode: result.exitCode,
|
|
106
|
+
killed: result.killed,
|
|
107
|
+
terminated: result.terminated,
|
|
108
|
+
});
|
|
109
|
+
},
|
|
110
|
+
});
|
|
111
|
+
// 未超限:content 为原始结果对象,按原有结构序列化;超限:buildWarning 已生成返回字符串
|
|
112
|
+
return typeof guarded === "string" ? guarded : JSON.stringify(guarded);
|
|
85
113
|
}
|
|
86
114
|
}
|
|
115
|
+
/** 统计文本行数(空文本记 0 行) */
|
|
116
|
+
function countLines(text) {
|
|
117
|
+
return text.length === 0 ? 0 : text.split("\n").length;
|
|
118
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { SdkCallbackTool } from "
|
|
1
|
+
import { SdkCallbackTool } from "./SdkCallbackTool.js";
|
|
2
2
|
import type { ToolEnv } from "../../../types/index.js";
|
|
3
3
|
import type { AgentNS } from "@ai-zen/agents-core";
|
|
4
4
|
export declare class ExistTool extends SdkCallbackTool {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { SdkCallbackTool } from "
|
|
1
|
+
import { SdkCallbackTool } from "./SdkCallbackTool.js";
|
|
2
2
|
import type { ToolEnv } from "../../../types/index.js";
|
|
3
3
|
import type { AgentNS, ToolCallContext } from "@ai-zen/agents-core";
|
|
4
4
|
export declare class FindTextTool extends SdkCallbackTool {
|
|
@@ -14,4 +14,9 @@ export declare class FindTextTool extends SdkCallbackTool {
|
|
|
14
14
|
maxFileSize?: number;
|
|
15
15
|
maxLineLength?: number;
|
|
16
16
|
}, ctx?: ToolCallContext): Promise<string>;
|
|
17
|
+
/**
|
|
18
|
+
* 输出保护:结果 JSON 超过 maxToolOutput 时落盘为 result.json,
|
|
19
|
+
* 返回警告 + 头尾预览;未超限则原样返回。
|
|
20
|
+
*/
|
|
21
|
+
private protect;
|
|
17
22
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import * as fsp from "fs/promises";
|
|
2
2
|
import * as path from "path";
|
|
3
|
-
import { SdkCallbackTool } from "
|
|
3
|
+
import { SdkCallbackTool } from "./SdkCallbackTool.js";
|
|
4
|
+
import { DEFAULT_MAX_TOOL_OUTPUT, guardOutput, headPreview } from "./outputGuard.js";
|
|
4
5
|
/** 默认总匹配数上限 */
|
|
5
6
|
const DEFAULT_MAX_MATCHES = 200;
|
|
6
7
|
/** 默认文件大小上限(字节,与 ReadFileTool 的 300KB 口径一致) */
|
|
@@ -126,17 +127,41 @@ export class FindTextTool extends SdkCallbackTool {
|
|
|
126
127
|
}
|
|
127
128
|
}
|
|
128
129
|
if (signal?.aborted) {
|
|
129
|
-
return JSON.stringify({ aborted: true, results: result });
|
|
130
|
+
return await this.protect(JSON.stringify({ aborted: true, results: result }));
|
|
130
131
|
}
|
|
131
132
|
if (truncated) {
|
|
132
|
-
return JSON.stringify({ truncated: true, totalMatches, results: result });
|
|
133
|
+
return await this.protect(JSON.stringify({ truncated: true, totalMatches, results: result }));
|
|
133
134
|
}
|
|
134
|
-
return JSON.stringify(result);
|
|
135
|
+
return await this.protect(JSON.stringify(result));
|
|
135
136
|
}
|
|
136
137
|
catch (error) {
|
|
137
138
|
return error?.message;
|
|
138
139
|
}
|
|
139
140
|
}
|
|
141
|
+
/**
|
|
142
|
+
* 输出保护:结果 JSON 超过 maxToolOutput 时落盘为 result.json,
|
|
143
|
+
* 返回警告 + 头尾预览;未超限则原样返回。
|
|
144
|
+
*/
|
|
145
|
+
async protect(json) {
|
|
146
|
+
const limit = this.env.config.maxToolOutput ?? DEFAULT_MAX_TOOL_OUTPUT;
|
|
147
|
+
return await guardOutput({
|
|
148
|
+
tool: "findText",
|
|
149
|
+
content: json,
|
|
150
|
+
isOverLimit: (text) => text.length > limit,
|
|
151
|
+
dump: (_dir, text) => [{ name: "result.json", content: text }],
|
|
152
|
+
buildWarning: (ctx) => {
|
|
153
|
+
const preview = headPreview(json);
|
|
154
|
+
return JSON.stringify({
|
|
155
|
+
outputTooLarge: true,
|
|
156
|
+
warning: `输出过大:结果共 ${json.length} 字符,超过上限 ${limit} 字符。` +
|
|
157
|
+
`完整结果已落盘(result.json),可用 readFile 分批读取。`,
|
|
158
|
+
files: ctx.files,
|
|
159
|
+
chars: json.length,
|
|
160
|
+
...preview,
|
|
161
|
+
});
|
|
162
|
+
},
|
|
163
|
+
});
|
|
164
|
+
}
|
|
140
165
|
}
|
|
141
166
|
/** 截断字符串到指定长度,超出部分以 "…" 结尾标记 */
|
|
142
167
|
function truncate(s, max) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { AgentNS, ToolCallContext } from "@ai-zen/agents-core";
|
|
2
|
-
import { SdkCallbackTool } from "
|
|
2
|
+
import { SdkCallbackTool } from "./SdkCallbackTool.js";
|
|
3
3
|
import type { AgentDefinition, AppConfig, ToolEnv } from "../../../types/index.js";
|
|
4
4
|
/**
|
|
5
5
|
* 根据文字描述生成图片。**统一返回字符串**(JSON,含图片 URL 列表):
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { SdkCallbackTool } from "
|
|
1
|
+
import { SdkCallbackTool } from "./SdkCallbackTool.js";
|
|
2
2
|
import type { ToolEnv } from "../../../types/index.js";
|
|
3
3
|
import type { AgentNS, ToolCallContext } from "@ai-zen/agents-core";
|
|
4
4
|
export declare class GlobTool extends SdkCallbackTool {
|
|
@@ -9,4 +9,9 @@ export declare class GlobTool extends SdkCallbackTool {
|
|
|
9
9
|
pattern: string;
|
|
10
10
|
exclude?: string[];
|
|
11
11
|
}, ctx?: ToolCallContext): Promise<string>;
|
|
12
|
+
/**
|
|
13
|
+
* 输出保护:结果 JSON 超过 maxToolOutput 时落盘为 result.json,
|
|
14
|
+
* 返回警告 + 头部预览;未超限则原样返回。
|
|
15
|
+
*/
|
|
16
|
+
private protect;
|
|
12
17
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import * as fsp from "fs/promises";
|
|
2
|
-
import { SdkCallbackTool } from "
|
|
2
|
+
import { SdkCallbackTool } from "./SdkCallbackTool.js";
|
|
3
|
+
import { DEFAULT_MAX_TOOL_OUTPUT, guardOutput, headPreview } from "./outputGuard.js";
|
|
3
4
|
export class GlobTool extends SdkCallbackTool {
|
|
4
5
|
function = {
|
|
5
6
|
name: "glob",
|
|
@@ -44,12 +45,37 @@ export class GlobTool extends SdkCallbackTool {
|
|
|
44
45
|
result.push(file);
|
|
45
46
|
}
|
|
46
47
|
if (signal?.aborted) {
|
|
47
|
-
return JSON.stringify({ aborted: true, files: result });
|
|
48
|
+
return await this.protect(JSON.stringify({ aborted: true, files: result }));
|
|
48
49
|
}
|
|
49
|
-
return JSON.stringify(result);
|
|
50
|
+
return await this.protect(JSON.stringify(result));
|
|
50
51
|
}
|
|
51
52
|
catch (error) {
|
|
52
53
|
return error?.message;
|
|
53
54
|
}
|
|
54
55
|
}
|
|
56
|
+
/**
|
|
57
|
+
* 输出保护:结果 JSON 超过 maxToolOutput 时落盘为 result.json,
|
|
58
|
+
* 返回警告 + 头部预览;未超限则原样返回。
|
|
59
|
+
*/
|
|
60
|
+
async protect(json) {
|
|
61
|
+
const limit = this.env.config.maxToolOutput ?? DEFAULT_MAX_TOOL_OUTPUT;
|
|
62
|
+
return await guardOutput({
|
|
63
|
+
tool: "glob",
|
|
64
|
+
content: json,
|
|
65
|
+
isOverLimit: (text) => text.length > limit,
|
|
66
|
+
dump: (_dir, text) => [{ name: "result.json", content: text }],
|
|
67
|
+
buildWarning: (ctx) => {
|
|
68
|
+
const preview = headPreview(json);
|
|
69
|
+
return JSON.stringify({
|
|
70
|
+
outputTooLarge: true,
|
|
71
|
+
warning: `输出过大:匹配结果共 ${json.length} 字符,超过上限 ${limit} 字符。` +
|
|
72
|
+
`完整结果已落盘(result.json),可用 readFile 分批读取;` +
|
|
73
|
+
`也可用更精确的 pattern 或 exclude 缩小范围后重新查找。`,
|
|
74
|
+
files: ctx.files,
|
|
75
|
+
chars: json.length,
|
|
76
|
+
...preview,
|
|
77
|
+
});
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
}
|
|
55
81
|
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { SdkCallbackTool } from "./SdkCallbackTool.js";
|
|
2
|
+
import type { ToolEnv } from "../../../types/index.js";
|
|
3
|
+
import type { AgentNS } from "@ai-zen/agents-core";
|
|
4
|
+
/**
|
|
5
|
+
* 勘察文件结构概况(行数、每行列数等),不做内容读取。
|
|
6
|
+
*
|
|
7
|
+
* 定位与 readFile 的 range 配套:大文件先看结构(有多少行、每行多长),
|
|
8
|
+
* 再决定 readFile 的读取范围。流式扫描,故不受 readFile 的 300KB 读取阈值限制。
|
|
9
|
+
*/
|
|
10
|
+
export declare class InspectFileTool extends SdkCallbackTool {
|
|
11
|
+
function: AgentNS.FunctionDefine;
|
|
12
|
+
constructor(env: ToolEnv);
|
|
13
|
+
call(input: {
|
|
14
|
+
path: string;
|
|
15
|
+
withColCountMap?: boolean;
|
|
16
|
+
}): Promise<string>;
|
|
17
|
+
}
|