@ai-zen/agents-sdk 0.4.0 → 0.5.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 +21 -0
- package/README.md +19 -15
- package/dist/capabilities/discovery/builtin.d.ts +7 -6
- package/dist/capabilities/discovery/builtin.js +10 -9
- package/dist/capabilities/implements/builtin/BatchEditTool.d.ts +15 -0
- package/dist/capabilities/implements/builtin/BatchEditTool.js +82 -0
- package/dist/capabilities/implements/builtin/CopyTool.d.ts +10 -0
- package/dist/capabilities/implements/builtin/CopyTool.js +43 -0
- package/dist/capabilities/implements/builtin/CwdTool.d.ts +6 -0
- package/dist/capabilities/implements/builtin/CwdTool.js +20 -0
- package/dist/capabilities/implements/builtin/DownloadFileTool.d.ts +9 -0
- package/dist/capabilities/implements/builtin/{downloadFile.js → DownloadFileTool.js} +29 -24
- package/dist/capabilities/implements/builtin/EditTool.d.ts +11 -0
- package/dist/capabilities/implements/builtin/EditTool.js +84 -0
- package/dist/capabilities/implements/builtin/ExecAsyncTool.d.ts +10 -0
- package/dist/capabilities/implements/builtin/{execAsync.js → ExecAsyncTool.js} +32 -34
- package/dist/capabilities/implements/builtin/ExecTool.d.ts +9 -0
- package/dist/capabilities/implements/builtin/ExecTool.js +46 -0
- package/dist/capabilities/implements/builtin/ExistTool.d.ts +8 -0
- package/dist/capabilities/implements/builtin/ExistTool.js +30 -0
- package/dist/capabilities/implements/builtin/FindTextTool.d.ts +12 -0
- package/dist/capabilities/implements/builtin/{findText.js → FindTextTool.js} +43 -37
- package/dist/capabilities/implements/builtin/GenerateImageTool.d.ts +21 -0
- package/dist/capabilities/implements/builtin/GenerateImageTool.js +121 -0
- package/dist/capabilities/implements/builtin/GlobTool.d.ts +10 -0
- package/dist/capabilities/implements/builtin/GlobTool.js +51 -0
- package/dist/capabilities/implements/builtin/LsTool.d.ts +8 -0
- package/dist/capabilities/implements/builtin/LsTool.js +33 -0
- package/dist/capabilities/implements/builtin/MkdirTool.d.ts +9 -0
- package/dist/capabilities/implements/builtin/MkdirTool.js +39 -0
- package/dist/capabilities/implements/builtin/ReadFileTool.d.ts +8 -0
- package/dist/capabilities/implements/builtin/ReadFileTool.js +37 -0
- package/dist/capabilities/implements/builtin/RenameTool.d.ts +9 -0
- package/dist/capabilities/implements/builtin/RenameTool.js +37 -0
- package/dist/capabilities/implements/builtin/RmTool.d.ts +9 -0
- package/dist/capabilities/implements/builtin/RmTool.js +39 -0
- package/dist/capabilities/implements/builtin/SleepTool.d.ts +8 -0
- package/dist/capabilities/implements/builtin/SleepTool.js +46 -0
- package/dist/capabilities/implements/builtin/WriteFileTool.d.ts +9 -0
- package/dist/capabilities/implements/builtin/WriteFileTool.js +40 -0
- package/dist/capabilities/implements/builtin/index.d.ts +29 -21
- package/dist/capabilities/implements/builtin/index.js +44 -37
- package/dist/capabilities/implements/builtin/test-helpers.d.ts +3 -0
- package/dist/capabilities/implements/builtin/test-helpers.js +4 -0
- package/dist/index.d.ts +5 -9
- package/dist/index.js +6 -9
- package/dist/runtime/Provider.d.ts +9 -1
- package/dist/runtime/Provider.js +7 -1
- package/dist/runtime/SdkCallbackTool.d.ts +28 -0
- package/dist/runtime/SdkCallbackTool.js +33 -0
- package/dist/types/index.d.ts +12 -20
- package/package.json +1 -1
- package/dist/capabilities/implements/builtin/batchEdit.d.ts +0 -2
- package/dist/capabilities/implements/builtin/batchEdit.js +0 -76
- package/dist/capabilities/implements/builtin/copy.d.ts +0 -2
- package/dist/capabilities/implements/builtin/copy.js +0 -38
- package/dist/capabilities/implements/builtin/cwd.d.ts +0 -2
- package/dist/capabilities/implements/builtin/cwd.js +0 -15
- package/dist/capabilities/implements/builtin/downloadFile.d.ts +0 -2
- package/dist/capabilities/implements/builtin/edit.d.ts +0 -2
- package/dist/capabilities/implements/builtin/edit.js +0 -79
- package/dist/capabilities/implements/builtin/exec.d.ts +0 -2
- package/dist/capabilities/implements/builtin/exec.js +0 -38
- package/dist/capabilities/implements/builtin/execAsync.d.ts +0 -2
- package/dist/capabilities/implements/builtin/exist.d.ts +0 -2
- package/dist/capabilities/implements/builtin/exist.js +0 -25
- package/dist/capabilities/implements/builtin/findText.d.ts +0 -2
- package/dist/capabilities/implements/builtin/generateImage.d.ts +0 -9
- package/dist/capabilities/implements/builtin/generateImage.js +0 -112
- package/dist/capabilities/implements/builtin/glob.d.ts +0 -2
- package/dist/capabilities/implements/builtin/glob.js +0 -45
- package/dist/capabilities/implements/builtin/ls.d.ts +0 -2
- package/dist/capabilities/implements/builtin/ls.js +0 -28
- package/dist/capabilities/implements/builtin/mkdir.d.ts +0 -2
- package/dist/capabilities/implements/builtin/mkdir.js +0 -34
- package/dist/capabilities/implements/builtin/readFile.d.ts +0 -2
- package/dist/capabilities/implements/builtin/readFile.js +0 -31
- package/dist/capabilities/implements/builtin/rename.d.ts +0 -2
- package/dist/capabilities/implements/builtin/rename.js +0 -32
- package/dist/capabilities/implements/builtin/rm.d.ts +0 -2
- package/dist/capabilities/implements/builtin/rm.js +0 -34
- package/dist/capabilities/implements/builtin/sleep.d.ts +0 -2
- package/dist/capabilities/implements/builtin/sleep.js +0 -41
- package/dist/capabilities/implements/builtin/writeFile.d.ts +0 -2
- package/dist/capabilities/implements/builtin/writeFile.js +0 -34
- package/dist/crud/ConversationRepository.d.ts +0 -9
- package/dist/crud/ConversationRepository.js +0 -10
- package/dist/crud/DraftRepository.d.ts +0 -15
- package/dist/crud/DraftRepository.js +0 -52
- package/dist/plugin/AutoDraftPlugin.d.ts +0 -26
- package/dist/plugin/AutoDraftPlugin.js +0 -62
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { SdkCallbackTool } from "../../../runtime/SdkCallbackTool.js";
|
|
2
|
+
import type { ToolEnv } from "../../../types/index.js";
|
|
3
|
+
export declare class ExecTool extends SdkCallbackTool {
|
|
4
|
+
constructor(env: ToolEnv);
|
|
5
|
+
call(input: {
|
|
6
|
+
command: string;
|
|
7
|
+
timeout?: number;
|
|
8
|
+
}): Promise<string>;
|
|
9
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { exec } from "child_process";
|
|
2
|
+
import { SdkCallbackTool } from "../../../runtime/SdkCallbackTool.js";
|
|
3
|
+
export class ExecTool extends SdkCallbackTool {
|
|
4
|
+
constructor(env) {
|
|
5
|
+
super({
|
|
6
|
+
function: {
|
|
7
|
+
name: "exec",
|
|
8
|
+
description: "执行命令",
|
|
9
|
+
parameters: {
|
|
10
|
+
type: "object",
|
|
11
|
+
properties: {
|
|
12
|
+
command: {
|
|
13
|
+
type: "string",
|
|
14
|
+
description: "要执行的命令",
|
|
15
|
+
},
|
|
16
|
+
timeout: {
|
|
17
|
+
type: "number",
|
|
18
|
+
description: "超时时间(毫秒),超时后会终止进程。默认无超时。建议长时间运行的命令使用 exec_async 异步执行。",
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
required: ["command"],
|
|
22
|
+
additionalProperties: false,
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
env,
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
async call(input) {
|
|
29
|
+
const command = input.command;
|
|
30
|
+
const timeout = input.timeout;
|
|
31
|
+
const result = await new Promise((resolve) => {
|
|
32
|
+
const child = exec(command, {
|
|
33
|
+
cwd: this.env.cwd,
|
|
34
|
+
timeout: timeout && timeout > 0 ? timeout : undefined,
|
|
35
|
+
}, (error, stdout, stderr) => {
|
|
36
|
+
resolve({
|
|
37
|
+
stdout,
|
|
38
|
+
stderr,
|
|
39
|
+
exitCode: error?.code ?? (error ? 1 : null),
|
|
40
|
+
killed: error?.killed ?? false,
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
return JSON.stringify(result);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { SdkCallbackTool } from "../../../runtime/SdkCallbackTool.js";
|
|
2
|
+
import type { ToolEnv } from "../../../types/index.js";
|
|
3
|
+
export declare class ExistTool extends SdkCallbackTool {
|
|
4
|
+
constructor(env: ToolEnv);
|
|
5
|
+
call(input: {
|
|
6
|
+
path: string;
|
|
7
|
+
}): Promise<string>;
|
|
8
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import * as fsp from "fs/promises";
|
|
2
|
+
import { SdkCallbackTool } from "../../../runtime/SdkCallbackTool.js";
|
|
3
|
+
export class ExistTool extends SdkCallbackTool {
|
|
4
|
+
constructor(env) {
|
|
5
|
+
super({
|
|
6
|
+
function: {
|
|
7
|
+
name: "exist",
|
|
8
|
+
description: "检查文件或目录是否存在",
|
|
9
|
+
parameters: {
|
|
10
|
+
type: "object",
|
|
11
|
+
properties: {
|
|
12
|
+
path: {
|
|
13
|
+
type: "string",
|
|
14
|
+
description: "文件或目录路径",
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
required: ["path"],
|
|
18
|
+
additionalProperties: false,
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
env,
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
async call(input) {
|
|
25
|
+
return await fsp
|
|
26
|
+
.access(this.resolve(input.path))
|
|
27
|
+
.then(() => "true")
|
|
28
|
+
.catch(() => "false");
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { SdkCallbackTool } from "../../../runtime/SdkCallbackTool.js";
|
|
2
|
+
import type { ToolEnv } from "../../../types/index.js";
|
|
3
|
+
export declare class FindTextTool extends SdkCallbackTool {
|
|
4
|
+
constructor(env: ToolEnv);
|
|
5
|
+
call(input: {
|
|
6
|
+
path?: string;
|
|
7
|
+
pattern: string;
|
|
8
|
+
text?: string;
|
|
9
|
+
regex?: string;
|
|
10
|
+
exclude?: string[];
|
|
11
|
+
}): Promise<string>;
|
|
12
|
+
}
|
|
@@ -1,42 +1,48 @@
|
|
|
1
|
-
import { CallbackTool } from "@ai-zen/agents-core";
|
|
2
1
|
import * as fsp from "fs/promises";
|
|
3
2
|
import * as path from "path";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
type: "
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
3
|
+
import { SdkCallbackTool } from "../../../runtime/SdkCallbackTool.js";
|
|
4
|
+
export class FindTextTool extends SdkCallbackTool {
|
|
5
|
+
constructor(env) {
|
|
6
|
+
super({
|
|
7
|
+
function: {
|
|
8
|
+
name: "findText",
|
|
9
|
+
description: "查找文本出现的位置,支持普通文本或正则匹配,返回文件名及具体行号、行内容",
|
|
10
|
+
parameters: {
|
|
11
|
+
type: "object",
|
|
12
|
+
properties: {
|
|
13
|
+
path: {
|
|
14
|
+
type: "string",
|
|
15
|
+
description: "glob cwd",
|
|
16
|
+
},
|
|
17
|
+
pattern: {
|
|
18
|
+
type: "string",
|
|
19
|
+
description: "glob pattern",
|
|
20
|
+
},
|
|
21
|
+
text: {
|
|
22
|
+
type: "string",
|
|
23
|
+
description: "要查找的文本(与 regex 二选一)",
|
|
24
|
+
},
|
|
25
|
+
regex: {
|
|
26
|
+
type: "string",
|
|
27
|
+
description: "正则表达式(与 text 二选一,例如 \\bconst\\s+\\w+)",
|
|
28
|
+
},
|
|
29
|
+
exclude: {
|
|
30
|
+
type: "array",
|
|
31
|
+
description: "要排除的 glob 模式数组",
|
|
32
|
+
items: {
|
|
33
|
+
type: "string",
|
|
34
|
+
},
|
|
35
|
+
},
|
|
32
36
|
},
|
|
37
|
+
required: ["path", "pattern"],
|
|
33
38
|
},
|
|
34
39
|
},
|
|
35
|
-
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
async
|
|
40
|
+
env,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
async call(input) {
|
|
39
44
|
try {
|
|
45
|
+
const cwd = input.path ? this.resolve(input.path) : this.env.cwd;
|
|
40
46
|
const result = [];
|
|
41
47
|
const text = input.text;
|
|
42
48
|
const regexStr = input.regex;
|
|
@@ -45,10 +51,10 @@ export const findTextTool = new CallbackTool({
|
|
|
45
51
|
}
|
|
46
52
|
const regex = regexStr ? new RegExp(regexStr) : null;
|
|
47
53
|
for await (const file of fsp.glob(input.pattern, {
|
|
48
|
-
cwd
|
|
54
|
+
cwd,
|
|
49
55
|
exclude: input.exclude || ["**/node_modules/**"],
|
|
50
56
|
})) {
|
|
51
|
-
const fullPath = path.join(
|
|
57
|
+
const fullPath = path.join(cwd, file);
|
|
52
58
|
const stats = await fsp.stat(fullPath);
|
|
53
59
|
if (stats.isFile()) {
|
|
54
60
|
const content = await fsp.readFile(fullPath, "utf-8");
|
|
@@ -82,5 +88,5 @@ export const findTextTool = new CallbackTool({
|
|
|
82
88
|
catch (error) {
|
|
83
89
|
return error?.message;
|
|
84
90
|
}
|
|
85
|
-
}
|
|
86
|
-
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { SdkCallbackTool } from "../../../runtime/SdkCallbackTool.js";
|
|
2
|
+
import type { ToolEnv } from "../../../types/index.js";
|
|
3
|
+
/**
|
|
4
|
+
* 根据文字描述生成图片。返回图片 URL,可通过 downloadFile 工具保存到本地。
|
|
5
|
+
*
|
|
6
|
+
* 依赖 AppConfig 中的 imageModels 和 defaultImageModel 配置。
|
|
7
|
+
* 如果未配置图片模型,工具会返回错误提示。
|
|
8
|
+
*
|
|
9
|
+
* 注意:与其它内置工具不同,generateImage 依赖图片模型配置,
|
|
10
|
+
* 因此由 discoverBuiltinTools 按 env.config.defaultImageModel 条件实例化,
|
|
11
|
+
* 不加入 BUILTIN_TOOL_CLASSES 静态注册表。
|
|
12
|
+
*/
|
|
13
|
+
export declare class GenerateImageTool extends SdkCallbackTool {
|
|
14
|
+
constructor(env: ToolEnv);
|
|
15
|
+
call(input: {
|
|
16
|
+
prompt: string;
|
|
17
|
+
model?: string;
|
|
18
|
+
size?: string;
|
|
19
|
+
quality?: string;
|
|
20
|
+
}): Promise<string>;
|
|
21
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { OpenAI, ZhipuImage } from "@ai-zen/agents-core";
|
|
2
|
+
import { SdkCallbackTool } from "../../../runtime/SdkCallbackTool.js";
|
|
3
|
+
/**
|
|
4
|
+
* 根据文字描述生成图片。返回图片 URL,可通过 downloadFile 工具保存到本地。
|
|
5
|
+
*
|
|
6
|
+
* 依赖 AppConfig 中的 imageModels 和 defaultImageModel 配置。
|
|
7
|
+
* 如果未配置图片模型,工具会返回错误提示。
|
|
8
|
+
*
|
|
9
|
+
* 注意:与其它内置工具不同,generateImage 依赖图片模型配置,
|
|
10
|
+
* 因此由 discoverBuiltinTools 按 env.config.defaultImageModel 条件实例化,
|
|
11
|
+
* 不加入 BUILTIN_TOOL_CLASSES 静态注册表。
|
|
12
|
+
*/
|
|
13
|
+
export class GenerateImageTool extends SdkCallbackTool {
|
|
14
|
+
constructor(env) {
|
|
15
|
+
super({
|
|
16
|
+
function: {
|
|
17
|
+
name: "generateImage",
|
|
18
|
+
description: "根据文字描述生成图片。返回图片 URL,可通过 downloadFile 工具保存到本地。",
|
|
19
|
+
parameters: {
|
|
20
|
+
type: "object",
|
|
21
|
+
properties: {
|
|
22
|
+
prompt: {
|
|
23
|
+
type: "string",
|
|
24
|
+
description: "所需图像的文本描述,应详细描述画面内容、风格、构图等",
|
|
25
|
+
},
|
|
26
|
+
model: {
|
|
27
|
+
type: "string",
|
|
28
|
+
description: "图片模型 ID,不指定则使用配置中的默认图片模型。配置中的图片模型可通过 'aiz config show' 查看。",
|
|
29
|
+
},
|
|
30
|
+
size: {
|
|
31
|
+
type: "string",
|
|
32
|
+
description: '图片尺寸。不指定则使用模型的默认尺寸。cogview系列: 1024x1024, 768x1344, 864x1152, 1344x768, 1152x864, 1440x720, 720x1440。glm-image: 1280x1280, 1568x1056, 1056x1568, 1472x1088, 1088x1472, 1728x960, 960x1728。',
|
|
33
|
+
},
|
|
34
|
+
quality: {
|
|
35
|
+
type: "string",
|
|
36
|
+
description: "图像质量。hd: 精细(约20秒), standard: 快速(约5-10秒)",
|
|
37
|
+
enum: ["hd", "standard"],
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
required: ["prompt"],
|
|
41
|
+
additionalProperties: false,
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
env,
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
async call(input) {
|
|
48
|
+
try {
|
|
49
|
+
const prompt = input.prompt;
|
|
50
|
+
if (!prompt || !prompt.trim()) {
|
|
51
|
+
return JSON.stringify({ success: false, error: "prompt 不能为空" });
|
|
52
|
+
}
|
|
53
|
+
const config = this.env.config;
|
|
54
|
+
const imageModels = config.imageModels;
|
|
55
|
+
const defaultImageModel = config.defaultImageModel;
|
|
56
|
+
if (!imageModels || imageModels.length === 0) {
|
|
57
|
+
return JSON.stringify({
|
|
58
|
+
success: false,
|
|
59
|
+
error: "未配置图片生成模型。请在配置中添加 imageModels。",
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
const modelId = input.model || defaultImageModel;
|
|
63
|
+
const imageModel = modelId
|
|
64
|
+
? imageModels.find((m) => m.id === modelId)
|
|
65
|
+
: imageModels[0];
|
|
66
|
+
if (!imageModel) {
|
|
67
|
+
return JSON.stringify({
|
|
68
|
+
success: false,
|
|
69
|
+
error: `图片模型 "${modelId}" 不存在。可用的图片模型: ${imageModels.map((m) => m.id).join(", ")}`,
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
const endpoint = config.endpoints.find((e) => e.id === imageModel.endpointId);
|
|
73
|
+
if (!endpoint) {
|
|
74
|
+
return JSON.stringify({
|
|
75
|
+
success: false,
|
|
76
|
+
error: `图片模型 "${imageModel.name}" 对应的端点 "${imageModel.endpointId}" 未配置`,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
if (!endpoint.apiKey) {
|
|
80
|
+
return JSON.stringify({
|
|
81
|
+
success: false,
|
|
82
|
+
error: `端点 "${endpoint.name}" 的 API Key 未设置`,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
const ep = new OpenAI({
|
|
86
|
+
openai_endpoint: endpoint.baseUrl,
|
|
87
|
+
api_key: endpoint.apiKey,
|
|
88
|
+
});
|
|
89
|
+
const modelName = input.model || imageModel.modelName;
|
|
90
|
+
const model = new ZhipuImage({
|
|
91
|
+
request_config: await ep.imageGeneration(modelName),
|
|
92
|
+
});
|
|
93
|
+
const result = await model.generate({
|
|
94
|
+
prompt: prompt.trim(),
|
|
95
|
+
model: modelName,
|
|
96
|
+
size: input.size || imageModel.defaultSize || undefined,
|
|
97
|
+
quality: input.quality || imageModel.defaultQuality || undefined,
|
|
98
|
+
});
|
|
99
|
+
const images = result.data.map((img, i) => ({
|
|
100
|
+
index: i,
|
|
101
|
+
url: img.url,
|
|
102
|
+
description: `图片 ${i + 1}`,
|
|
103
|
+
}));
|
|
104
|
+
return JSON.stringify({
|
|
105
|
+
success: true,
|
|
106
|
+
model: imageModel.name,
|
|
107
|
+
modelId: imageModel.id,
|
|
108
|
+
created: result.created,
|
|
109
|
+
images,
|
|
110
|
+
content_filter: result.content_filter || [],
|
|
111
|
+
note: "图片临时链接有效期为30天,请及时转存图片。",
|
|
112
|
+
}, null, 2);
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
return JSON.stringify({
|
|
116
|
+
success: false,
|
|
117
|
+
error: error instanceof Error ? error.message : "图片生成失败",
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { SdkCallbackTool } from "../../../runtime/SdkCallbackTool.js";
|
|
2
|
+
import type { ToolEnv } from "../../../types/index.js";
|
|
3
|
+
export declare class GlobTool extends SdkCallbackTool {
|
|
4
|
+
constructor(env: ToolEnv);
|
|
5
|
+
call(input: {
|
|
6
|
+
path?: string;
|
|
7
|
+
pattern: string;
|
|
8
|
+
exclude?: string[];
|
|
9
|
+
}): Promise<string>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import * as fsp from "fs/promises";
|
|
2
|
+
import { SdkCallbackTool } from "../../../runtime/SdkCallbackTool.js";
|
|
3
|
+
export class GlobTool extends SdkCallbackTool {
|
|
4
|
+
constructor(env) {
|
|
5
|
+
super({
|
|
6
|
+
function: {
|
|
7
|
+
name: "glob",
|
|
8
|
+
description: "使用 glob 模式递归扫描和查找文件。这是进行文件系统搜索的首选工具,功能远优于简单的 'ls' 列表命令。当你需要查找特定类型的文件、遍历目录树或需要排除特定文件时,请优先使用此函数。",
|
|
9
|
+
parameters: {
|
|
10
|
+
type: "object",
|
|
11
|
+
properties: {
|
|
12
|
+
path: {
|
|
13
|
+
type: "string",
|
|
14
|
+
description: "glob cwd",
|
|
15
|
+
},
|
|
16
|
+
pattern: {
|
|
17
|
+
type: "string",
|
|
18
|
+
description: "glob pattern",
|
|
19
|
+
},
|
|
20
|
+
exclude: {
|
|
21
|
+
type: "array",
|
|
22
|
+
description: "glob pattern to exclude",
|
|
23
|
+
items: {
|
|
24
|
+
type: "string",
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
required: ["pattern"],
|
|
29
|
+
additionalProperties: false,
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
env,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
async call(input) {
|
|
36
|
+
try {
|
|
37
|
+
const cwd = input.path ? this.resolve(input.path) : this.env.cwd;
|
|
38
|
+
const result = [];
|
|
39
|
+
for await (const file of fsp.glob(input.pattern, {
|
|
40
|
+
exclude: input.exclude || ["**/node_modules/**"],
|
|
41
|
+
cwd,
|
|
42
|
+
})) {
|
|
43
|
+
result.push(file);
|
|
44
|
+
}
|
|
45
|
+
return JSON.stringify(result);
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
return error?.message;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { SdkCallbackTool } from "../../../runtime/SdkCallbackTool.js";
|
|
2
|
+
import type { ToolEnv } from "../../../types/index.js";
|
|
3
|
+
export declare class LsTool extends SdkCallbackTool {
|
|
4
|
+
constructor(env: ToolEnv);
|
|
5
|
+
call(input: {
|
|
6
|
+
path: string;
|
|
7
|
+
}): Promise<string>;
|
|
8
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import * as fsp from "fs/promises";
|
|
2
|
+
import { SdkCallbackTool } from "../../../runtime/SdkCallbackTool.js";
|
|
3
|
+
export class LsTool extends SdkCallbackTool {
|
|
4
|
+
constructor(env) {
|
|
5
|
+
super({
|
|
6
|
+
function: {
|
|
7
|
+
name: "ls",
|
|
8
|
+
description: "列出目录",
|
|
9
|
+
parameters: {
|
|
10
|
+
type: "object",
|
|
11
|
+
properties: {
|
|
12
|
+
path: {
|
|
13
|
+
type: "string",
|
|
14
|
+
description: "目录路径",
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
required: ["path"],
|
|
18
|
+
additionalProperties: false,
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
env,
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
async call(input) {
|
|
25
|
+
try {
|
|
26
|
+
const result = await fsp.readdir(this.resolve(input.path));
|
|
27
|
+
return JSON.stringify(result);
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
return error?.message;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { SdkCallbackTool } from "../../../runtime/SdkCallbackTool.js";
|
|
2
|
+
import type { ToolEnv } from "../../../types/index.js";
|
|
3
|
+
export declare class MkdirTool extends SdkCallbackTool {
|
|
4
|
+
constructor(env: ToolEnv);
|
|
5
|
+
call(input: {
|
|
6
|
+
path: string;
|
|
7
|
+
recursive?: boolean;
|
|
8
|
+
}): Promise<string>;
|
|
9
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import * as fsp from "fs/promises";
|
|
2
|
+
import { SdkCallbackTool } from "../../../runtime/SdkCallbackTool.js";
|
|
3
|
+
export class MkdirTool extends SdkCallbackTool {
|
|
4
|
+
constructor(env) {
|
|
5
|
+
super({
|
|
6
|
+
function: {
|
|
7
|
+
name: "mkdir",
|
|
8
|
+
description: "创建目录",
|
|
9
|
+
parameters: {
|
|
10
|
+
type: "object",
|
|
11
|
+
properties: {
|
|
12
|
+
path: {
|
|
13
|
+
type: "string",
|
|
14
|
+
description: "目录路径",
|
|
15
|
+
},
|
|
16
|
+
recursive: {
|
|
17
|
+
type: "boolean",
|
|
18
|
+
description: "是否递归创建子目录",
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
required: ["path"],
|
|
22
|
+
additionalProperties: false,
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
env,
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
async call(input) {
|
|
29
|
+
try {
|
|
30
|
+
await fsp.mkdir(this.resolve(input.path), {
|
|
31
|
+
recursive: input.recursive ?? false,
|
|
32
|
+
});
|
|
33
|
+
return "success";
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
return error?.message;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { SdkCallbackTool } from "../../../runtime/SdkCallbackTool.js";
|
|
2
|
+
import type { ToolEnv } from "../../../types/index.js";
|
|
3
|
+
export declare class ReadFileTool extends SdkCallbackTool {
|
|
4
|
+
constructor(env: ToolEnv);
|
|
5
|
+
call(input: {
|
|
6
|
+
path: string;
|
|
7
|
+
}): Promise<string>;
|
|
8
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import * as fsp from "fs/promises";
|
|
2
|
+
import { SdkCallbackTool } from "../../../runtime/SdkCallbackTool.js";
|
|
3
|
+
export class ReadFileTool extends SdkCallbackTool {
|
|
4
|
+
constructor(env) {
|
|
5
|
+
super({
|
|
6
|
+
function: {
|
|
7
|
+
name: "readFile",
|
|
8
|
+
description: "读取文件",
|
|
9
|
+
parameters: {
|
|
10
|
+
type: "object",
|
|
11
|
+
properties: {
|
|
12
|
+
path: {
|
|
13
|
+
type: "string",
|
|
14
|
+
description: "文件路径",
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
required: ["path"],
|
|
18
|
+
additionalProperties: false,
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
env,
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
async call(input) {
|
|
25
|
+
try {
|
|
26
|
+
const filePath = this.resolve(input.path);
|
|
27
|
+
const stats = await fsp.stat(filePath);
|
|
28
|
+
if (stats.size > 300 * 1024) {
|
|
29
|
+
throw new Error(`文件过大,无法读取,当前文件大小 ${stats.size} 字节`);
|
|
30
|
+
}
|
|
31
|
+
return await fsp.readFile(filePath, "utf-8");
|
|
32
|
+
}
|
|
33
|
+
catch (error) {
|
|
34
|
+
return error?.message;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { SdkCallbackTool } from "../../../runtime/SdkCallbackTool.js";
|
|
2
|
+
import type { ToolEnv } from "../../../types/index.js";
|
|
3
|
+
export declare class RenameTool extends SdkCallbackTool {
|
|
4
|
+
constructor(env: ToolEnv);
|
|
5
|
+
call(input: {
|
|
6
|
+
oldPath: string;
|
|
7
|
+
newPath: string;
|
|
8
|
+
}): Promise<string>;
|
|
9
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import * as fsp from "fs/promises";
|
|
2
|
+
import { SdkCallbackTool } from "../../../runtime/SdkCallbackTool.js";
|
|
3
|
+
export class RenameTool extends SdkCallbackTool {
|
|
4
|
+
constructor(env) {
|
|
5
|
+
super({
|
|
6
|
+
function: {
|
|
7
|
+
name: "rename",
|
|
8
|
+
description: "重命名或移动文件/目录(相当于 mv 命令)",
|
|
9
|
+
parameters: {
|
|
10
|
+
type: "object",
|
|
11
|
+
properties: {
|
|
12
|
+
oldPath: {
|
|
13
|
+
type: "string",
|
|
14
|
+
description: "原路径",
|
|
15
|
+
},
|
|
16
|
+
newPath: {
|
|
17
|
+
type: "string",
|
|
18
|
+
description: "新路径",
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
required: ["oldPath", "newPath"],
|
|
22
|
+
additionalProperties: false,
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
env,
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
async call(input) {
|
|
29
|
+
try {
|
|
30
|
+
await fsp.rename(this.resolve(input.oldPath), this.resolve(input.newPath));
|
|
31
|
+
return "success";
|
|
32
|
+
}
|
|
33
|
+
catch (error) {
|
|
34
|
+
return error?.message;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { SdkCallbackTool } from "../../../runtime/SdkCallbackTool.js";
|
|
2
|
+
import type { ToolEnv } from "../../../types/index.js";
|
|
3
|
+
export declare class RmTool extends SdkCallbackTool {
|
|
4
|
+
constructor(env: ToolEnv);
|
|
5
|
+
call(input: {
|
|
6
|
+
path: string;
|
|
7
|
+
recursive?: boolean;
|
|
8
|
+
}): Promise<string>;
|
|
9
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import * as fsp from "fs/promises";
|
|
2
|
+
import { SdkCallbackTool } from "../../../runtime/SdkCallbackTool.js";
|
|
3
|
+
export class RmTool extends SdkCallbackTool {
|
|
4
|
+
constructor(env) {
|
|
5
|
+
super({
|
|
6
|
+
function: {
|
|
7
|
+
name: "rm",
|
|
8
|
+
description: "删除文件或目录",
|
|
9
|
+
parameters: {
|
|
10
|
+
type: "object",
|
|
11
|
+
properties: {
|
|
12
|
+
path: {
|
|
13
|
+
type: "string",
|
|
14
|
+
description: "文件或目录路径",
|
|
15
|
+
},
|
|
16
|
+
recursive: {
|
|
17
|
+
type: "boolean",
|
|
18
|
+
description: "是否递归删除子目录",
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
required: ["path"],
|
|
22
|
+
additionalProperties: false,
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
env,
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
async call(input) {
|
|
29
|
+
try {
|
|
30
|
+
await fsp.rm(this.resolve(input.path), {
|
|
31
|
+
recursive: input.recursive ?? false,
|
|
32
|
+
});
|
|
33
|
+
return "success";
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
return error?.message;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|