@ai-zen/agents-sdk 0.4.0 → 0.5.1
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 +27 -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 +2 -2
- 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
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-zen/agents-sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.1",
|
|
4
4
|
"description": "AI-Zen Agents SDK — shared business logic for CLI and Desktop",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"dependencies": {
|
|
19
19
|
"@modelcontextprotocol/sdk": "^1.29.0",
|
|
20
20
|
"dayjs": "^1.11.21",
|
|
21
|
-
"@ai-zen/agents-core": "3.0
|
|
21
|
+
"@ai-zen/agents-core": "^3.1.0"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
24
24
|
"@types/node": "^24.12.2",
|
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
import { CallbackTool } from "@ai-zen/agents-core";
|
|
2
|
-
import * as fsp from "fs/promises";
|
|
3
|
-
export const batchEditTool = new CallbackTool({
|
|
4
|
-
function: {
|
|
5
|
-
name: "batchEdit",
|
|
6
|
-
description: "批量编辑文件文本,可以优先使用这个工具对文件进行编辑",
|
|
7
|
-
parameters: {
|
|
8
|
-
type: "object",
|
|
9
|
-
properties: {
|
|
10
|
-
path: {
|
|
11
|
-
type: "string",
|
|
12
|
-
description: "文件路径",
|
|
13
|
-
},
|
|
14
|
-
replacements: {
|
|
15
|
-
type: "array",
|
|
16
|
-
description: "要替换的文本数组",
|
|
17
|
-
items: {
|
|
18
|
-
type: "object",
|
|
19
|
-
properties: {
|
|
20
|
-
oldText: {
|
|
21
|
-
type: "string",
|
|
22
|
-
description: "要替换的文本",
|
|
23
|
-
},
|
|
24
|
-
newText: {
|
|
25
|
-
type: "string",
|
|
26
|
-
description: "替换后的文本",
|
|
27
|
-
},
|
|
28
|
-
isReplaceAll: {
|
|
29
|
-
type: "boolean",
|
|
30
|
-
description: "是否替换所有匹配的文本(默认仅替换首次匹配)。使用此功能前应确保你提供的 oldText 足够精确,避免误替换",
|
|
31
|
-
default: false,
|
|
32
|
-
},
|
|
33
|
-
},
|
|
34
|
-
required: ["oldText", "newText"],
|
|
35
|
-
additionalProperties: false,
|
|
36
|
-
},
|
|
37
|
-
},
|
|
38
|
-
},
|
|
39
|
-
required: ["path", "replacements"],
|
|
40
|
-
additionalProperties: false,
|
|
41
|
-
},
|
|
42
|
-
},
|
|
43
|
-
async callback(input) {
|
|
44
|
-
try {
|
|
45
|
-
const content = await fsp.readFile(input.path, "utf-8");
|
|
46
|
-
let newContent = content;
|
|
47
|
-
const results = [];
|
|
48
|
-
for (const replacement of input.replacements) {
|
|
49
|
-
if (!newContent.includes(replacement.oldText)) {
|
|
50
|
-
results.push({
|
|
51
|
-
oldText: replacement.oldText,
|
|
52
|
-
newText: replacement.newText,
|
|
53
|
-
result: "文件中未精确匹配到要替换的文本",
|
|
54
|
-
});
|
|
55
|
-
continue;
|
|
56
|
-
}
|
|
57
|
-
if (replacement.isReplaceAll) {
|
|
58
|
-
newContent = newContent.replaceAll(replacement.oldText, replacement.newText);
|
|
59
|
-
}
|
|
60
|
-
else {
|
|
61
|
-
newContent = newContent.replace(replacement.oldText, replacement.newText);
|
|
62
|
-
}
|
|
63
|
-
results.push({
|
|
64
|
-
oldText: replacement.oldText,
|
|
65
|
-
newText: replacement.newText,
|
|
66
|
-
result: "success",
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
await fsp.writeFile(input.path, newContent);
|
|
70
|
-
return JSON.stringify(results);
|
|
71
|
-
}
|
|
72
|
-
catch (error) {
|
|
73
|
-
return error?.message;
|
|
74
|
-
}
|
|
75
|
-
},
|
|
76
|
-
});
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { CallbackTool } from "@ai-zen/agents-core";
|
|
2
|
-
import * as fsp from "fs/promises";
|
|
3
|
-
export const copyTool = new CallbackTool({
|
|
4
|
-
function: {
|
|
5
|
-
name: "copy",
|
|
6
|
-
description: "复制文件或目录(相当于 cp 命令)",
|
|
7
|
-
parameters: {
|
|
8
|
-
type: "object",
|
|
9
|
-
properties: {
|
|
10
|
-
src: {
|
|
11
|
-
type: "string",
|
|
12
|
-
description: "源路径",
|
|
13
|
-
},
|
|
14
|
-
dest: {
|
|
15
|
-
type: "string",
|
|
16
|
-
description: "目标路径",
|
|
17
|
-
},
|
|
18
|
-
recursive: {
|
|
19
|
-
type: "boolean",
|
|
20
|
-
description: "是否递归复制目录,复制目录时需设为 true",
|
|
21
|
-
},
|
|
22
|
-
},
|
|
23
|
-
required: ["src", "dest"],
|
|
24
|
-
additionalProperties: false,
|
|
25
|
-
},
|
|
26
|
-
},
|
|
27
|
-
async callback(input) {
|
|
28
|
-
try {
|
|
29
|
-
await fsp.cp(input.src, input.dest, {
|
|
30
|
-
recursive: input.recursive ?? false,
|
|
31
|
-
});
|
|
32
|
-
return "success";
|
|
33
|
-
}
|
|
34
|
-
catch (error) {
|
|
35
|
-
return error?.message;
|
|
36
|
-
}
|
|
37
|
-
},
|
|
38
|
-
});
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { CallbackTool } from "@ai-zen/agents-core";
|
|
2
|
-
export const cwdTool = new CallbackTool({
|
|
3
|
-
function: {
|
|
4
|
-
name: "cwd",
|
|
5
|
-
description: "获取当前工作目录 cwd",
|
|
6
|
-
parameters: {
|
|
7
|
-
type: "object",
|
|
8
|
-
properties: {},
|
|
9
|
-
required: [],
|
|
10
|
-
},
|
|
11
|
-
},
|
|
12
|
-
async callback() {
|
|
13
|
-
return process.cwd();
|
|
14
|
-
},
|
|
15
|
-
});
|
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
import { CallbackTool } from "@ai-zen/agents-core";
|
|
2
|
-
import * as fsp from "fs/promises";
|
|
3
|
-
function countOccurrences(content, text) {
|
|
4
|
-
let count = 0;
|
|
5
|
-
let pos = 0;
|
|
6
|
-
while (true) {
|
|
7
|
-
pos = content.indexOf(text, pos);
|
|
8
|
-
if (pos === -1)
|
|
9
|
-
break;
|
|
10
|
-
count++;
|
|
11
|
-
pos += text.length;
|
|
12
|
-
}
|
|
13
|
-
return count;
|
|
14
|
-
}
|
|
15
|
-
export const editTool = new CallbackTool({
|
|
16
|
-
function: {
|
|
17
|
-
name: "edit",
|
|
18
|
-
description: "编辑文件中的文本,支持精确替换或替换所有匹配项",
|
|
19
|
-
parameters: {
|
|
20
|
-
type: "object",
|
|
21
|
-
properties: {
|
|
22
|
-
path: {
|
|
23
|
-
type: "string",
|
|
24
|
-
description: "文件路径",
|
|
25
|
-
},
|
|
26
|
-
oldText: {
|
|
27
|
-
type: "string",
|
|
28
|
-
description: "要替换的文本",
|
|
29
|
-
},
|
|
30
|
-
newText: {
|
|
31
|
-
type: "string",
|
|
32
|
-
description: "替换后的文本",
|
|
33
|
-
},
|
|
34
|
-
isReplaceAll: {
|
|
35
|
-
type: "boolean",
|
|
36
|
-
description: "是否替换所有匹配的文本(默认 false,仅替换首次匹配)。使用此功能前应确保你提供的 oldText 足够精确,避免误替换",
|
|
37
|
-
default: false,
|
|
38
|
-
},
|
|
39
|
-
},
|
|
40
|
-
required: ["path", "oldText", "newText"],
|
|
41
|
-
additionalProperties: false,
|
|
42
|
-
},
|
|
43
|
-
},
|
|
44
|
-
async callback(input) {
|
|
45
|
-
try {
|
|
46
|
-
const path = input.path;
|
|
47
|
-
const oldText = input.oldText;
|
|
48
|
-
const newText = input.newText;
|
|
49
|
-
const isReplaceAll = input.isReplaceAll;
|
|
50
|
-
const content = await fsp.readFile(path, "utf-8");
|
|
51
|
-
const count = countOccurrences(content, oldText);
|
|
52
|
-
if (count === 0) {
|
|
53
|
-
return JSON.stringify({
|
|
54
|
-
result: "文件中未精确匹配到要替换的文本",
|
|
55
|
-
matchCount: 0,
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
const newContent = isReplaceAll
|
|
59
|
-
? content.replaceAll(oldText, newText)
|
|
60
|
-
: content.replace(oldText, newText);
|
|
61
|
-
await fsp.writeFile(path, newContent);
|
|
62
|
-
if (!isReplaceAll && count > 1) {
|
|
63
|
-
return JSON.stringify({
|
|
64
|
-
result: `警告:未开启替换所有(isReplaceAll=false),但文本 "${oldText}" 在文件中出现了 ${count} 次,仅替换了首次匹配。如果希望替换所有匹配,请将 isReplaceAll 设为 true`,
|
|
65
|
-
matchCount: count,
|
|
66
|
-
replacedCount: 1,
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
return JSON.stringify({
|
|
70
|
-
result: "success",
|
|
71
|
-
matchCount: count,
|
|
72
|
-
replacedCount: isReplaceAll ? count : 1,
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
catch (error) {
|
|
76
|
-
return error?.message;
|
|
77
|
-
}
|
|
78
|
-
},
|
|
79
|
-
});
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { CallbackTool } from "@ai-zen/agents-core";
|
|
2
|
-
import { exec } from "child_process";
|
|
3
|
-
export const execTool = new CallbackTool({
|
|
4
|
-
function: {
|
|
5
|
-
name: "exec",
|
|
6
|
-
description: "执行命令",
|
|
7
|
-
parameters: {
|
|
8
|
-
type: "object",
|
|
9
|
-
properties: {
|
|
10
|
-
command: {
|
|
11
|
-
type: "string",
|
|
12
|
-
description: "要执行的命令",
|
|
13
|
-
},
|
|
14
|
-
timeout: {
|
|
15
|
-
type: "number",
|
|
16
|
-
description: "超时时间(毫秒),超时后会终止进程。默认无超时。建议长时间运行的命令使用 exec_async 异步执行。",
|
|
17
|
-
},
|
|
18
|
-
},
|
|
19
|
-
required: ["command"],
|
|
20
|
-
additionalProperties: false,
|
|
21
|
-
},
|
|
22
|
-
},
|
|
23
|
-
async callback(input) {
|
|
24
|
-
const command = input.command;
|
|
25
|
-
const timeout = input.timeout;
|
|
26
|
-
const result = await new Promise((resolve) => {
|
|
27
|
-
const child = exec(command, { timeout: timeout && timeout > 0 ? timeout : undefined }, (error, stdout, stderr) => {
|
|
28
|
-
resolve({
|
|
29
|
-
stdout,
|
|
30
|
-
stderr,
|
|
31
|
-
exitCode: error?.code ?? (error ? 1 : null),
|
|
32
|
-
killed: error?.killed ?? false,
|
|
33
|
-
});
|
|
34
|
-
});
|
|
35
|
-
});
|
|
36
|
-
return JSON.stringify(result);
|
|
37
|
-
},
|
|
38
|
-
});
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { CallbackTool } from "@ai-zen/agents-core";
|
|
2
|
-
import * as fsp from "fs/promises";
|
|
3
|
-
export const existTool = new CallbackTool({
|
|
4
|
-
function: {
|
|
5
|
-
name: "exist",
|
|
6
|
-
description: "检查文件或目录是否存在",
|
|
7
|
-
parameters: {
|
|
8
|
-
type: "object",
|
|
9
|
-
properties: {
|
|
10
|
-
path: {
|
|
11
|
-
type: "string",
|
|
12
|
-
description: "文件或目录路径",
|
|
13
|
-
},
|
|
14
|
-
},
|
|
15
|
-
required: ["path"],
|
|
16
|
-
additionalProperties: false,
|
|
17
|
-
},
|
|
18
|
-
},
|
|
19
|
-
async callback(input) {
|
|
20
|
-
return await fsp
|
|
21
|
-
.access(input.path)
|
|
22
|
-
.then(() => "true")
|
|
23
|
-
.catch(() => "false");
|
|
24
|
-
},
|
|
25
|
-
});
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { CallbackTool } from "@ai-zen/agents-core";
|
|
2
|
-
import type { AppConfig } from "../../../types/index.js";
|
|
3
|
-
/**
|
|
4
|
-
* 根据文字描述生成图片。返回图片 URL,可通过 downloadFile 工具保存到本地。
|
|
5
|
-
*
|
|
6
|
-
* 依赖 AppConfig 中的 imageModels 和 defaultImageModel 配置。
|
|
7
|
-
* 如果未配置图片模型,工具会返回错误提示。
|
|
8
|
-
*/
|
|
9
|
-
export declare function createGenerateImageTool(config: AppConfig): CallbackTool;
|
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
import { CallbackTool, OpenAI, ZhipuImage } from "@ai-zen/agents-core";
|
|
2
|
-
/**
|
|
3
|
-
* 根据文字描述生成图片。返回图片 URL,可通过 downloadFile 工具保存到本地。
|
|
4
|
-
*
|
|
5
|
-
* 依赖 AppConfig 中的 imageModels 和 defaultImageModel 配置。
|
|
6
|
-
* 如果未配置图片模型,工具会返回错误提示。
|
|
7
|
-
*/
|
|
8
|
-
export function createGenerateImageTool(config) {
|
|
9
|
-
return new CallbackTool({
|
|
10
|
-
function: {
|
|
11
|
-
name: "generateImage",
|
|
12
|
-
description: "根据文字描述生成图片。返回图片 URL,可通过 downloadFile 工具保存到本地。",
|
|
13
|
-
parameters: {
|
|
14
|
-
type: "object",
|
|
15
|
-
properties: {
|
|
16
|
-
prompt: {
|
|
17
|
-
type: "string",
|
|
18
|
-
description: "所需图像的文本描述,应详细描述画面内容、风格、构图等",
|
|
19
|
-
},
|
|
20
|
-
model: {
|
|
21
|
-
type: "string",
|
|
22
|
-
description: "图片模型 ID,不指定则使用配置中的默认图片模型。配置中的图片模型可通过 'aiz config show' 查看。",
|
|
23
|
-
},
|
|
24
|
-
size: {
|
|
25
|
-
type: "string",
|
|
26
|
-
description: '图片尺寸。不指定则使用模型的默认尺寸。cogview系列: 1024x1024, 768x1344, 864x1152, 1344x768, 1152x864, 1440x720, 720x1440。glm-image: 1280x1280, 1568x1056, 1056x1568, 1472x1088, 1088x1472, 1728x960, 960x1728。',
|
|
27
|
-
},
|
|
28
|
-
quality: {
|
|
29
|
-
type: "string",
|
|
30
|
-
description: "图像质量。hd: 精细(约20秒), standard: 快速(约5-10秒)",
|
|
31
|
-
enum: ["hd", "standard"],
|
|
32
|
-
},
|
|
33
|
-
},
|
|
34
|
-
required: ["prompt"],
|
|
35
|
-
additionalProperties: false,
|
|
36
|
-
},
|
|
37
|
-
},
|
|
38
|
-
async callback(input) {
|
|
39
|
-
try {
|
|
40
|
-
const prompt = input.prompt;
|
|
41
|
-
if (!prompt || !prompt.trim()) {
|
|
42
|
-
return JSON.stringify({ success: false, error: "prompt 不能为空" });
|
|
43
|
-
}
|
|
44
|
-
const imageModels = config.imageModels;
|
|
45
|
-
const defaultImageModel = config.defaultImageModel;
|
|
46
|
-
if (!imageModels || imageModels.length === 0) {
|
|
47
|
-
return JSON.stringify({
|
|
48
|
-
success: false,
|
|
49
|
-
error: "未配置图片生成模型。请在配置中添加 imageModels。",
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
const modelId = input.model || defaultImageModel;
|
|
53
|
-
const imageModel = modelId
|
|
54
|
-
? imageModels.find((m) => m.id === modelId)
|
|
55
|
-
: imageModels[0];
|
|
56
|
-
if (!imageModel) {
|
|
57
|
-
return JSON.stringify({
|
|
58
|
-
success: false,
|
|
59
|
-
error: `图片模型 "${modelId}" 不存在。可用的图片模型: ${imageModels.map((m) => m.id).join(", ")}`,
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
const endpoint = config.endpoints.find((e) => e.id === imageModel.endpointId);
|
|
63
|
-
if (!endpoint) {
|
|
64
|
-
return JSON.stringify({
|
|
65
|
-
success: false,
|
|
66
|
-
error: `图片模型 "${imageModel.name}" 对应的端点 "${imageModel.endpointId}" 未配置`,
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
if (!endpoint.apiKey) {
|
|
70
|
-
return JSON.stringify({
|
|
71
|
-
success: false,
|
|
72
|
-
error: `端点 "${endpoint.name}" 的 API Key 未设置`,
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
const ep = new OpenAI({
|
|
76
|
-
openai_endpoint: endpoint.baseUrl,
|
|
77
|
-
api_key: endpoint.apiKey,
|
|
78
|
-
});
|
|
79
|
-
const modelName = input.model || imageModel.modelName;
|
|
80
|
-
const model = new ZhipuImage({
|
|
81
|
-
request_config: await ep.imageGeneration(modelName),
|
|
82
|
-
});
|
|
83
|
-
const result = await model.generate({
|
|
84
|
-
prompt: prompt.trim(),
|
|
85
|
-
model: modelName,
|
|
86
|
-
size: input.size || imageModel.defaultSize || undefined,
|
|
87
|
-
quality: input.quality || imageModel.defaultQuality || undefined,
|
|
88
|
-
});
|
|
89
|
-
const images = result.data.map((img, i) => ({
|
|
90
|
-
index: i,
|
|
91
|
-
url: img.url,
|
|
92
|
-
description: `图片 ${i + 1}`,
|
|
93
|
-
}));
|
|
94
|
-
return JSON.stringify({
|
|
95
|
-
success: true,
|
|
96
|
-
model: imageModel.name,
|
|
97
|
-
modelId: imageModel.id,
|
|
98
|
-
created: result.created,
|
|
99
|
-
images,
|
|
100
|
-
content_filter: result.content_filter || [],
|
|
101
|
-
note: "图片临时链接有效期为30天,请及时转存图片。",
|
|
102
|
-
}, null, 2);
|
|
103
|
-
}
|
|
104
|
-
catch (error) {
|
|
105
|
-
return JSON.stringify({
|
|
106
|
-
success: false,
|
|
107
|
-
error: error instanceof Error ? error.message : "图片生成失败",
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
},
|
|
111
|
-
});
|
|
112
|
-
}
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { CallbackTool } from "@ai-zen/agents-core";
|
|
2
|
-
import * as fsp from "fs/promises";
|
|
3
|
-
export const globTool = new CallbackTool({
|
|
4
|
-
function: {
|
|
5
|
-
name: "glob",
|
|
6
|
-
description: "使用 glob 模式递归扫描和查找文件。这是进行文件系统搜索的首选工具,功能远优于简单的 'ls' 列表命令。当你需要查找特定类型的文件、遍历目录树或需要排除特定文件时,请优先使用此函数。",
|
|
7
|
-
parameters: {
|
|
8
|
-
type: "object",
|
|
9
|
-
properties: {
|
|
10
|
-
path: {
|
|
11
|
-
type: "string",
|
|
12
|
-
description: "glob cwd",
|
|
13
|
-
},
|
|
14
|
-
pattern: {
|
|
15
|
-
type: "string",
|
|
16
|
-
description: "glob pattern",
|
|
17
|
-
},
|
|
18
|
-
exclude: {
|
|
19
|
-
type: "array",
|
|
20
|
-
description: "glob pattern to exclude",
|
|
21
|
-
items: {
|
|
22
|
-
type: "string",
|
|
23
|
-
},
|
|
24
|
-
},
|
|
25
|
-
},
|
|
26
|
-
required: ["pattern"],
|
|
27
|
-
additionalProperties: false,
|
|
28
|
-
},
|
|
29
|
-
},
|
|
30
|
-
async callback(input) {
|
|
31
|
-
try {
|
|
32
|
-
const result = [];
|
|
33
|
-
for await (const file of fsp.glob(input.pattern, {
|
|
34
|
-
exclude: input.exclude || ["**/node_modules/**"],
|
|
35
|
-
cwd: input.path,
|
|
36
|
-
})) {
|
|
37
|
-
result.push(file);
|
|
38
|
-
}
|
|
39
|
-
return JSON.stringify(result);
|
|
40
|
-
}
|
|
41
|
-
catch (error) {
|
|
42
|
-
return error?.message;
|
|
43
|
-
}
|
|
44
|
-
},
|
|
45
|
-
});
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { CallbackTool } from "@ai-zen/agents-core";
|
|
2
|
-
import * as fsp from "fs/promises";
|
|
3
|
-
export const lsTool = new CallbackTool({
|
|
4
|
-
function: {
|
|
5
|
-
name: "ls",
|
|
6
|
-
description: "列出目录",
|
|
7
|
-
parameters: {
|
|
8
|
-
type: "object",
|
|
9
|
-
properties: {
|
|
10
|
-
path: {
|
|
11
|
-
type: "string",
|
|
12
|
-
description: "目录路径",
|
|
13
|
-
},
|
|
14
|
-
},
|
|
15
|
-
required: ["path"],
|
|
16
|
-
additionalProperties: false,
|
|
17
|
-
},
|
|
18
|
-
},
|
|
19
|
-
async callback(input) {
|
|
20
|
-
try {
|
|
21
|
-
const result = await fsp.readdir(input.path);
|
|
22
|
-
return JSON.stringify(result);
|
|
23
|
-
}
|
|
24
|
-
catch (error) {
|
|
25
|
-
return error?.message;
|
|
26
|
-
}
|
|
27
|
-
},
|
|
28
|
-
});
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { CallbackTool } from "@ai-zen/agents-core";
|
|
2
|
-
import * as fsp from "fs/promises";
|
|
3
|
-
export const mkdirTool = new CallbackTool({
|
|
4
|
-
function: {
|
|
5
|
-
name: "mkdir",
|
|
6
|
-
description: "创建目录",
|
|
7
|
-
parameters: {
|
|
8
|
-
type: "object",
|
|
9
|
-
properties: {
|
|
10
|
-
path: {
|
|
11
|
-
type: "string",
|
|
12
|
-
description: "目录路径",
|
|
13
|
-
},
|
|
14
|
-
recursive: {
|
|
15
|
-
type: "boolean",
|
|
16
|
-
description: "是否递归创建子目录",
|
|
17
|
-
},
|
|
18
|
-
},
|
|
19
|
-
required: ["path"],
|
|
20
|
-
additionalProperties: false,
|
|
21
|
-
},
|
|
22
|
-
},
|
|
23
|
-
async callback(input) {
|
|
24
|
-
try {
|
|
25
|
-
await fsp.mkdir(input.path, {
|
|
26
|
-
recursive: input.recursive,
|
|
27
|
-
});
|
|
28
|
-
return "success";
|
|
29
|
-
}
|
|
30
|
-
catch (error) {
|
|
31
|
-
return error?.message;
|
|
32
|
-
}
|
|
33
|
-
},
|
|
34
|
-
});
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { CallbackTool } from "@ai-zen/agents-core";
|
|
2
|
-
import * as fsp from "fs/promises";
|
|
3
|
-
export const readFileTool = new CallbackTool({
|
|
4
|
-
function: {
|
|
5
|
-
name: "readFile",
|
|
6
|
-
description: "读取文件",
|
|
7
|
-
parameters: {
|
|
8
|
-
type: "object",
|
|
9
|
-
properties: {
|
|
10
|
-
path: {
|
|
11
|
-
type: "string",
|
|
12
|
-
description: "文件路径",
|
|
13
|
-
},
|
|
14
|
-
},
|
|
15
|
-
required: ["path"],
|
|
16
|
-
additionalProperties: false,
|
|
17
|
-
},
|
|
18
|
-
},
|
|
19
|
-
async callback(input) {
|
|
20
|
-
try {
|
|
21
|
-
const stats = await fsp.stat(input.path);
|
|
22
|
-
if (stats.size > 300 * 1024) {
|
|
23
|
-
throw new Error(`文件过大,无法读取,当前文件大小 ${stats.size} 字节`);
|
|
24
|
-
}
|
|
25
|
-
return await fsp.readFile(input.path, "utf-8");
|
|
26
|
-
}
|
|
27
|
-
catch (error) {
|
|
28
|
-
return error?.message;
|
|
29
|
-
}
|
|
30
|
-
},
|
|
31
|
-
});
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { CallbackTool } from "@ai-zen/agents-core";
|
|
2
|
-
import * as fsp from "fs/promises";
|
|
3
|
-
export const renameTool = new CallbackTool({
|
|
4
|
-
function: {
|
|
5
|
-
name: "rename",
|
|
6
|
-
description: "重命名或移动文件/目录(相当于 mv 命令)",
|
|
7
|
-
parameters: {
|
|
8
|
-
type: "object",
|
|
9
|
-
properties: {
|
|
10
|
-
oldPath: {
|
|
11
|
-
type: "string",
|
|
12
|
-
description: "原路径",
|
|
13
|
-
},
|
|
14
|
-
newPath: {
|
|
15
|
-
type: "string",
|
|
16
|
-
description: "新路径",
|
|
17
|
-
},
|
|
18
|
-
},
|
|
19
|
-
required: ["oldPath", "newPath"],
|
|
20
|
-
additionalProperties: false,
|
|
21
|
-
},
|
|
22
|
-
},
|
|
23
|
-
async callback(input) {
|
|
24
|
-
try {
|
|
25
|
-
await fsp.rename(input.oldPath, input.newPath);
|
|
26
|
-
return "success";
|
|
27
|
-
}
|
|
28
|
-
catch (error) {
|
|
29
|
-
return error?.message;
|
|
30
|
-
}
|
|
31
|
-
},
|
|
32
|
-
});
|