@ai-zen/agents-sdk 0.5.7 → 0.7.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 +53 -0
- package/README.md +10 -7
- package/README.zh.md +10 -7
- package/dist/capabilities/discovery/builtin.d.ts +3 -3
- package/dist/capabilities/discovery/builtin.js +4 -12
- package/dist/capabilities/implements/builtin/BatchEditTool.d.ts +2 -0
- package/dist/capabilities/implements/builtin/BatchEditTool.js +36 -38
- package/dist/capabilities/implements/builtin/CopyTool.d.ts +2 -0
- package/dist/capabilities/implements/builtin/CopyTool.js +23 -25
- package/dist/capabilities/implements/builtin/CwdTool.d.ts +2 -0
- package/dist/capabilities/implements/builtin/CwdTool.js +10 -12
- package/dist/capabilities/implements/builtin/DownloadFileTool.d.ts +3 -1
- package/dist/capabilities/implements/builtin/DownloadFileTool.js +32 -24
- package/dist/capabilities/implements/builtin/EditTool.d.ts +2 -0
- package/dist/capabilities/implements/builtin/EditTool.js +28 -30
- package/dist/capabilities/implements/builtin/ExecAsyncTool.d.ts +2 -0
- package/dist/capabilities/implements/builtin/ExecAsyncTool.js +23 -25
- package/dist/capabilities/implements/builtin/ExecTool.d.ts +3 -1
- package/dist/capabilities/implements/builtin/ExecTool.js +62 -29
- package/dist/capabilities/implements/builtin/ExistTool.d.ts +2 -0
- package/dist/capabilities/implements/builtin/ExistTool.js +15 -17
- package/dist/capabilities/implements/builtin/FindTextTool.d.ts +3 -1
- package/dist/capabilities/implements/builtin/FindTextTool.js +43 -35
- package/dist/capabilities/implements/builtin/GenerateImageTool.d.ts +11 -9
- package/dist/capabilities/implements/builtin/GenerateImageTool.js +63 -56
- package/dist/capabilities/implements/builtin/GlobTool.d.ts +3 -1
- package/dist/capabilities/implements/builtin/GlobTool.js +32 -28
- package/dist/capabilities/implements/builtin/LsTool.d.ts +2 -0
- package/dist/capabilities/implements/builtin/LsTool.js +15 -17
- package/dist/capabilities/implements/builtin/MkdirTool.d.ts +2 -0
- package/dist/capabilities/implements/builtin/MkdirTool.js +19 -21
- package/dist/capabilities/implements/builtin/ReadFileTool.d.ts +2 -0
- package/dist/capabilities/implements/builtin/ReadFileTool.js +15 -17
- package/dist/capabilities/implements/builtin/RenameTool.d.ts +2 -0
- package/dist/capabilities/implements/builtin/RenameTool.js +19 -21
- package/dist/capabilities/implements/builtin/RmTool.d.ts +2 -0
- package/dist/capabilities/implements/builtin/RmTool.js +19 -21
- package/dist/capabilities/implements/builtin/SleepTool.d.ts +3 -1
- package/dist/capabilities/implements/builtin/SleepTool.js +39 -19
- package/dist/capabilities/implements/builtin/ViewImageTool.d.ts +24 -0
- package/dist/capabilities/implements/builtin/ViewImageTool.js +109 -0
- package/dist/capabilities/implements/builtin/WriteFileTool.d.ts +2 -0
- package/dist/capabilities/implements/builtin/WriteFileTool.js +19 -21
- package/dist/capabilities/implements/builtin/index.d.ts +6 -4
- package/dist/capabilities/implements/builtin/index.js +8 -4
- package/dist/capabilities/implements/mcpTools.js +4 -6
- package/dist/capabilities/implements/skillTools.js +18 -8
- package/dist/capabilities/implements/subAgentTools.js +17 -8
- package/dist/config/constants.js +12 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.js +2 -0
- package/dist/plugin/AutoMigratePlugin.d.ts +1 -1
- package/dist/plugin/AutoMigratePlugin.js +1 -1
- package/dist/plugin/AutoRefreshToolsPlugin.d.ts +1 -1
- package/dist/plugin/AutoRefreshToolsPlugin.js +2 -2
- package/dist/plugin/ContextGuardPlugin.d.ts +2 -2
- package/dist/plugin/ContextGuardPlugin.js +1 -1
- package/dist/plugin/UnknownToolHintPlugin.d.ts +21 -0
- package/dist/plugin/UnknownToolHintPlugin.js +30 -0
- package/dist/runtime/Provider.d.ts +8 -7
- package/dist/runtime/Provider.js +14 -4
- package/dist/runtime/SdkAgent.d.ts +14 -107
- package/dist/runtime/SdkAgent.js +8 -120
- package/dist/runtime/SdkCallbackTool.d.ts +25 -9
- package/dist/runtime/SdkCallbackTool.js +7 -3
- package/dist/runtime/createAgent.js +4 -3
- package/dist/runtime/createModel.d.ts +17 -4
- package/dist/runtime/createModel.js +15 -11
- package/dist/types/index.d.ts +2 -0
- package/package.json +3 -2
|
@@ -1,33 +1,31 @@
|
|
|
1
1
|
import { spawn } from "child_process";
|
|
2
2
|
import { SdkCallbackTool } from "../../../runtime/SdkCallbackTool.js";
|
|
3
3
|
export class ExecAsyncTool extends SdkCallbackTool {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
type: "boolean",
|
|
22
|
-
description: "是否完全脱离当前进程组(可选,默认 false)。设为 true 时,即使当前 Agent 进程退出,启动的程序仍会继续运行。适用于需要独立运行的守护进程或 GUI 程序。",
|
|
23
|
-
},
|
|
24
|
-
},
|
|
25
|
-
required: ["command"],
|
|
26
|
-
additionalProperties: false,
|
|
4
|
+
function = {
|
|
5
|
+
name: "exec_async",
|
|
6
|
+
description: "异步执行命令,启动程序后立即返回,不等待执行结果。适用于启动长期运行的进程(如服务器、监听程序、GUI应用等)或不需要等待结果的命令。注意:该工具本身不捕获命令的输出;若需获取/留存输出,可在命令中使用 shell 重定向写入文件(如 `> /path/file`,支持 `>>` 追加、`|` 管道)。命令经 shell 解析执行。",
|
|
7
|
+
parameters: {
|
|
8
|
+
type: "object",
|
|
9
|
+
properties: {
|
|
10
|
+
command: {
|
|
11
|
+
type: "string",
|
|
12
|
+
description: "要执行的命令(含参数)",
|
|
13
|
+
},
|
|
14
|
+
cwd: {
|
|
15
|
+
type: "string",
|
|
16
|
+
description: "工作目录(可选,默认当前工作目录)",
|
|
17
|
+
},
|
|
18
|
+
detached: {
|
|
19
|
+
type: "boolean",
|
|
20
|
+
description: "是否完全脱离当前进程组(可选,默认 false)。设为 true 时,即使当前 Agent 进程退出,启动的程序仍会继续运行。适用于需要独立运行的守护进程或 GUI 程序。",
|
|
27
21
|
},
|
|
28
22
|
},
|
|
29
|
-
|
|
30
|
-
|
|
23
|
+
required: ["command"],
|
|
24
|
+
additionalProperties: false,
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
constructor(env) {
|
|
28
|
+
super({ env });
|
|
31
29
|
}
|
|
32
30
|
async call(input) {
|
|
33
31
|
const command = input.command;
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { SdkCallbackTool } from "../../../runtime/SdkCallbackTool.js";
|
|
2
2
|
import type { ToolEnv } from "../../../types/index.js";
|
|
3
|
+
import type { AgentNS, ToolCallContext } from "@ai-zen/agents-core";
|
|
3
4
|
export declare class ExecTool extends SdkCallbackTool {
|
|
5
|
+
function: AgentNS.FunctionDefine;
|
|
4
6
|
constructor(env: ToolEnv);
|
|
5
7
|
call(input: {
|
|
6
8
|
command: string;
|
|
7
9
|
timeout: number;
|
|
8
|
-
}): Promise<string>;
|
|
10
|
+
}, ctx?: ToolCallContext): Promise<string>;
|
|
9
11
|
}
|
|
@@ -1,52 +1,85 @@
|
|
|
1
1
|
import { exec } from "child_process";
|
|
2
2
|
import { SdkCallbackTool } from "../../../runtime/SdkCallbackTool.js";
|
|
3
3
|
export class ExecTool extends SdkCallbackTool {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
type: "number",
|
|
18
|
-
description: "超时时间(毫秒),必填。超时后会终止进程;超过该时长仍未完成即视为超时并终止。建议长时间运行的命令使用 exec_async 异步执行。",
|
|
19
|
-
},
|
|
20
|
-
},
|
|
21
|
-
required: ["command", "timeout"],
|
|
22
|
-
additionalProperties: false,
|
|
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 异步执行。",
|
|
23
17
|
},
|
|
24
18
|
},
|
|
25
|
-
|
|
26
|
-
|
|
19
|
+
required: ["command", "timeout"],
|
|
20
|
+
additionalProperties: false,
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
constructor(env) {
|
|
24
|
+
super({ env });
|
|
27
25
|
}
|
|
28
|
-
async call(input) {
|
|
26
|
+
async call(input, ctx) {
|
|
29
27
|
const command = input.command;
|
|
30
28
|
const { timeout } = input;
|
|
31
29
|
// 运行时双校验:timeout 必填且必须为正数
|
|
32
30
|
if (typeof timeout !== "number" || !Number.isFinite(timeout) || timeout <= 0) {
|
|
33
31
|
throw new Error(`exec: 参数 timeout 为必填项,且必须是正数(毫秒),当前值: ${JSON.stringify(timeout)}`);
|
|
34
32
|
}
|
|
33
|
+
const signal = ctx?.signal;
|
|
35
34
|
const result = await new Promise((resolve) => {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
timeout,
|
|
39
|
-
}, (error, stdout, stderr) => {
|
|
40
|
-
const killed = error?.killed ?? false;
|
|
35
|
+
// 若 signal 已处于 aborted,直接标记中断结果
|
|
36
|
+
if (signal?.aborted) {
|
|
41
37
|
resolve({
|
|
38
|
+
stdout: "",
|
|
39
|
+
stderr: "进程已中断(aborted)",
|
|
40
|
+
exitCode: null,
|
|
41
|
+
killed: true,
|
|
42
|
+
terminated: "aborted",
|
|
43
|
+
});
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
let child;
|
|
47
|
+
let settled = false;
|
|
48
|
+
const settle = (value) => {
|
|
49
|
+
if (settled)
|
|
50
|
+
return;
|
|
51
|
+
settled = true;
|
|
52
|
+
signal?.removeEventListener("abort", onAbort);
|
|
53
|
+
resolve(value);
|
|
54
|
+
};
|
|
55
|
+
child = exec(command, { cwd: this.env.cwd, timeout }, (error, stdout, stderr) => {
|
|
56
|
+
const killed = error?.killed ?? false;
|
|
57
|
+
const terminated = killed
|
|
58
|
+
? (signal?.aborted ? "aborted" : "timeout")
|
|
59
|
+
: undefined;
|
|
60
|
+
settle({
|
|
42
61
|
stdout,
|
|
43
62
|
stderr,
|
|
44
63
|
exitCode: error?.code ?? (error ? 1 : null),
|
|
45
64
|
killed,
|
|
46
|
-
|
|
47
|
-
terminated: killed ? "timeout" : undefined,
|
|
65
|
+
terminated,
|
|
48
66
|
});
|
|
49
67
|
});
|
|
68
|
+
// 中断:kill 子进程,令其尽快结束
|
|
69
|
+
const onAbort = () => {
|
|
70
|
+
child?.kill("SIGKILL");
|
|
71
|
+
// 即便 exec 回调因信号处理而未能及时触发,也立即返回中断结果,避免挂起
|
|
72
|
+
settle({
|
|
73
|
+
stdout: "",
|
|
74
|
+
stderr: "进程已中断(aborted)",
|
|
75
|
+
exitCode: null,
|
|
76
|
+
killed: true,
|
|
77
|
+
terminated: "aborted",
|
|
78
|
+
});
|
|
79
|
+
};
|
|
80
|
+
if (signal) {
|
|
81
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
82
|
+
}
|
|
50
83
|
});
|
|
51
84
|
return JSON.stringify(result);
|
|
52
85
|
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { SdkCallbackTool } from "../../../runtime/SdkCallbackTool.js";
|
|
2
2
|
import type { ToolEnv } from "../../../types/index.js";
|
|
3
|
+
import type { AgentNS } from "@ai-zen/agents-core";
|
|
3
4
|
export declare class ExistTool extends SdkCallbackTool {
|
|
5
|
+
function: AgentNS.FunctionDefine;
|
|
4
6
|
constructor(env: ToolEnv);
|
|
5
7
|
call(input: {
|
|
6
8
|
path: string;
|
|
@@ -1,25 +1,23 @@
|
|
|
1
1
|
import * as fsp from "fs/promises";
|
|
2
2
|
import { SdkCallbackTool } from "../../../runtime/SdkCallbackTool.js";
|
|
3
3
|
export class ExistTool extends SdkCallbackTool {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
type: "string",
|
|
14
|
-
description: "文件或目录路径",
|
|
15
|
-
},
|
|
16
|
-
},
|
|
17
|
-
required: ["path"],
|
|
18
|
-
additionalProperties: false,
|
|
4
|
+
function = {
|
|
5
|
+
name: "exist",
|
|
6
|
+
description: "检查文件或目录是否存在",
|
|
7
|
+
parameters: {
|
|
8
|
+
type: "object",
|
|
9
|
+
properties: {
|
|
10
|
+
path: {
|
|
11
|
+
type: "string",
|
|
12
|
+
description: "文件或目录路径",
|
|
19
13
|
},
|
|
20
14
|
},
|
|
21
|
-
|
|
22
|
-
|
|
15
|
+
required: ["path"],
|
|
16
|
+
additionalProperties: false,
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
constructor(env) {
|
|
20
|
+
super({ env });
|
|
23
21
|
}
|
|
24
22
|
async call(input) {
|
|
25
23
|
return await fsp
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { SdkCallbackTool } from "../../../runtime/SdkCallbackTool.js";
|
|
2
2
|
import type { ToolEnv } from "../../../types/index.js";
|
|
3
|
+
import type { AgentNS, ToolCallContext } from "@ai-zen/agents-core";
|
|
3
4
|
export declare class FindTextTool extends SdkCallbackTool {
|
|
5
|
+
function: AgentNS.FunctionDefine;
|
|
4
6
|
constructor(env: ToolEnv);
|
|
5
7
|
call(input: {
|
|
6
8
|
path?: string;
|
|
@@ -8,5 +10,5 @@ export declare class FindTextTool extends SdkCallbackTool {
|
|
|
8
10
|
text?: string;
|
|
9
11
|
regex?: string;
|
|
10
12
|
exclude?: string[];
|
|
11
|
-
}): Promise<string>;
|
|
13
|
+
}, ctx?: ToolCallContext): Promise<string>;
|
|
12
14
|
}
|
|
@@ -2,45 +2,44 @@ import * as fsp from "fs/promises";
|
|
|
2
2
|
import * as path from "path";
|
|
3
3
|
import { SdkCallbackTool } from "../../../runtime/SdkCallbackTool.js";
|
|
4
4
|
export class FindTextTool extends SdkCallbackTool {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
items: {
|
|
33
|
-
type: "string",
|
|
34
|
-
},
|
|
35
|
-
},
|
|
5
|
+
function = {
|
|
6
|
+
name: "findText",
|
|
7
|
+
description: "查找文本出现的位置,支持普通文本或正则匹配,返回文件名及具体行号、行内容",
|
|
8
|
+
parameters: {
|
|
9
|
+
type: "object",
|
|
10
|
+
properties: {
|
|
11
|
+
path: {
|
|
12
|
+
type: "string",
|
|
13
|
+
description: "glob cwd",
|
|
14
|
+
},
|
|
15
|
+
pattern: {
|
|
16
|
+
type: "string",
|
|
17
|
+
description: "glob pattern",
|
|
18
|
+
},
|
|
19
|
+
text: {
|
|
20
|
+
type: "string",
|
|
21
|
+
description: "要查找的文本(与 regex 二选一)",
|
|
22
|
+
},
|
|
23
|
+
regex: {
|
|
24
|
+
type: "string",
|
|
25
|
+
description: "正则表达式(与 text 二选一,例如 \\bconst\\s+\\w+)",
|
|
26
|
+
},
|
|
27
|
+
exclude: {
|
|
28
|
+
type: "array",
|
|
29
|
+
description: "要排除的 glob 模式数组",
|
|
30
|
+
items: {
|
|
31
|
+
type: "string",
|
|
36
32
|
},
|
|
37
|
-
required: ["path", "pattern"],
|
|
38
33
|
},
|
|
39
34
|
},
|
|
40
|
-
|
|
41
|
-
}
|
|
35
|
+
required: ["path", "pattern"],
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
constructor(env) {
|
|
39
|
+
super({ env });
|
|
42
40
|
}
|
|
43
|
-
async call(input) {
|
|
41
|
+
async call(input, ctx) {
|
|
42
|
+
const signal = ctx?.signal;
|
|
44
43
|
try {
|
|
45
44
|
const cwd = input.path ? this.resolve(input.path) : this.env.cwd;
|
|
46
45
|
const result = [];
|
|
@@ -54,10 +53,16 @@ export class FindTextTool extends SdkCallbackTool {
|
|
|
54
53
|
cwd,
|
|
55
54
|
exclude: input.exclude || ["**/node_modules/**"],
|
|
56
55
|
})) {
|
|
56
|
+
if (signal?.aborted)
|
|
57
|
+
break; // 中断:停止继续遍历/消费
|
|
57
58
|
const fullPath = path.join(cwd, file);
|
|
58
59
|
const stats = await fsp.stat(fullPath);
|
|
60
|
+
if (signal?.aborted)
|
|
61
|
+
break; // 中断:stat 后再次检查
|
|
59
62
|
if (stats.isFile()) {
|
|
60
63
|
const content = await fsp.readFile(fullPath, "utf-8");
|
|
64
|
+
if (signal?.aborted)
|
|
65
|
+
break; // 中断:readFile 后再次检查
|
|
61
66
|
const lines = content.split("\n");
|
|
62
67
|
const matches = [];
|
|
63
68
|
lines.forEach((lineContent, index) => {
|
|
@@ -83,6 +88,9 @@ export class FindTextTool extends SdkCallbackTool {
|
|
|
83
88
|
}
|
|
84
89
|
}
|
|
85
90
|
}
|
|
91
|
+
if (signal?.aborted) {
|
|
92
|
+
return JSON.stringify({ aborted: true, results: result });
|
|
93
|
+
}
|
|
86
94
|
return JSON.stringify(result);
|
|
87
95
|
}
|
|
88
96
|
catch (error) {
|
|
@@ -1,21 +1,23 @@
|
|
|
1
|
+
import type { AgentNS, ToolCallContext } from "@ai-zen/agents-core";
|
|
1
2
|
import { SdkCallbackTool } from "../../../runtime/SdkCallbackTool.js";
|
|
2
|
-
import type { ToolEnv } from "../../../types/index.js";
|
|
3
|
+
import type { AgentDefinition, AppConfig, ToolEnv } from "../../../types/index.js";
|
|
3
4
|
/**
|
|
4
|
-
*
|
|
5
|
+
* 根据文字描述生成图片。**统一返回字符串**(JSON,含图片 URL 列表):
|
|
6
|
+
* 生成结果如何处理(查看/下载/转发)由模型与上层自由决定——
|
|
7
|
+
* 需要让模型看图时,模型可主动调用 viewImage(URL);需要保存时用 downloadFile。
|
|
5
8
|
*
|
|
6
|
-
* 依赖 AppConfig 中的 imageModels 和 defaultImageModel
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* 注意:与其它内置工具不同,generateImage 依赖图片模型配置,
|
|
10
|
-
* 因此由 discoverBuiltinTools 按 env.config.defaultImageModel 条件实例化,
|
|
11
|
-
* 不加入 BUILTIN_TOOL_CLASSES 静态注册表。
|
|
9
|
+
* 依赖 AppConfig 中的 imageModels 和 defaultImageModel 配置(isAvailable 声明),
|
|
10
|
+
* 未配置时由 buildTools 过滤,不暴露给 Agent。
|
|
12
11
|
*/
|
|
13
12
|
export declare class GenerateImageTool extends SdkCallbackTool {
|
|
13
|
+
/** 依赖图片模型配置(defaultImageModel),未配置时由 buildTools 按 isAvailable 过滤 */
|
|
14
|
+
isAvailable(config: AppConfig, _definition: AgentDefinition): boolean;
|
|
15
|
+
function: AgentNS.FunctionDefine;
|
|
14
16
|
constructor(env: ToolEnv);
|
|
15
17
|
call(input: {
|
|
16
18
|
prompt: string;
|
|
17
19
|
model?: string;
|
|
18
20
|
size?: string;
|
|
19
21
|
quality?: string;
|
|
20
|
-
}): Promise<string>;
|
|
22
|
+
}, ctx?: ToolCallContext): Promise<string>;
|
|
21
23
|
}
|
|
@@ -1,50 +1,51 @@
|
|
|
1
|
-
import
|
|
1
|
+
import OpenAI from "openai";
|
|
2
2
|
import { SdkCallbackTool } from "../../../runtime/SdkCallbackTool.js";
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* 根据文字描述生成图片。**统一返回字符串**(JSON,含图片 URL 列表):
|
|
5
|
+
* 生成结果如何处理(查看/下载/转发)由模型与上层自由决定——
|
|
6
|
+
* 需要让模型看图时,模型可主动调用 viewImage(URL);需要保存时用 downloadFile。
|
|
5
7
|
*
|
|
6
|
-
* 依赖 AppConfig 中的 imageModels 和 defaultImageModel
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* 注意:与其它内置工具不同,generateImage 依赖图片模型配置,
|
|
10
|
-
* 因此由 discoverBuiltinTools 按 env.config.defaultImageModel 条件实例化,
|
|
11
|
-
* 不加入 BUILTIN_TOOL_CLASSES 静态注册表。
|
|
8
|
+
* 依赖 AppConfig 中的 imageModels 和 defaultImageModel 配置(isAvailable 声明),
|
|
9
|
+
* 未配置时由 buildTools 过滤,不暴露给 Agent。
|
|
12
10
|
*/
|
|
13
11
|
export class GenerateImageTool extends SdkCallbackTool {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
required: ["prompt"],
|
|
41
|
-
additionalProperties: false,
|
|
12
|
+
/** 依赖图片模型配置(defaultImageModel),未配置时由 buildTools 按 isAvailable 过滤 */
|
|
13
|
+
isAvailable(config, _definition) {
|
|
14
|
+
return !!config.defaultImageModel;
|
|
15
|
+
}
|
|
16
|
+
function = {
|
|
17
|
+
name: "generateImage",
|
|
18
|
+
description: "根据文字描述生成图片,返回图片 URL 列表(JSON)。如需查看图片内容,请用 viewImage 工具(传入图片 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"],
|
|
42
38
|
},
|
|
43
39
|
},
|
|
44
|
-
|
|
45
|
-
|
|
40
|
+
required: ["prompt"],
|
|
41
|
+
additionalProperties: false,
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
constructor(env) {
|
|
45
|
+
super({ env });
|
|
46
46
|
}
|
|
47
|
-
async call(input) {
|
|
47
|
+
async call(input, ctx) {
|
|
48
|
+
const signal = ctx?.signal;
|
|
48
49
|
try {
|
|
49
50
|
const prompt = input.prompt;
|
|
50
51
|
if (!prompt || !prompt.trim()) {
|
|
@@ -82,36 +83,42 @@ export class GenerateImageTool extends SdkCallbackTool {
|
|
|
82
83
|
error: `端点 "${endpoint.name}" 的 API Key 未设置`,
|
|
83
84
|
});
|
|
84
85
|
}
|
|
85
|
-
const
|
|
86
|
-
|
|
87
|
-
|
|
86
|
+
const client = new OpenAI({
|
|
87
|
+
apiKey: endpoint.apiKey,
|
|
88
|
+
baseURL: endpoint.baseUrl,
|
|
88
89
|
});
|
|
89
90
|
const modelName = input.model || imageModel.modelName;
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
});
|
|
93
|
-
const result = await model.generate({
|
|
94
|
-
prompt: prompt.trim(),
|
|
91
|
+
// 通过官方 SDK 生成图片(厂商特有字段如 size/quality 用 any 透传)
|
|
92
|
+
const result = await client.images.generate({
|
|
95
93
|
model: modelName,
|
|
94
|
+
prompt: prompt.trim(),
|
|
96
95
|
size: input.size || imageModel.defaultSize || undefined,
|
|
97
96
|
quality: input.quality || imageModel.defaultQuality || undefined,
|
|
98
|
-
});
|
|
99
|
-
const
|
|
100
|
-
|
|
101
|
-
url
|
|
102
|
-
|
|
103
|
-
|
|
97
|
+
}, { signal });
|
|
98
|
+
const urls = (result.data ?? [])
|
|
99
|
+
.map((img) => img.url)
|
|
100
|
+
.filter((url) => !!url);
|
|
101
|
+
// 统一返回字符串(JSON):生成结果如何处理(查看/下载/转发)由模型与上层自由决定,
|
|
102
|
+
// 不替模型做「看或不看」的假设。需要让模型看图 → 模型可主动调用 viewImage(URL);
|
|
103
|
+
// 需要保存 → downloadFile 下载。
|
|
104
104
|
return JSON.stringify({
|
|
105
105
|
success: true,
|
|
106
106
|
model: imageModel.name,
|
|
107
107
|
modelId: imageModel.id,
|
|
108
108
|
created: result.created,
|
|
109
|
-
images,
|
|
110
|
-
|
|
111
|
-
note: "图片临时链接有效期为30天,请及时转存图片。",
|
|
109
|
+
images: urls.map((url, i) => ({ index: i, url })),
|
|
110
|
+
note: "图片临时链接有效期为30天。如需查看图片内容,可用 viewImage 工具(传入图片 URL);如需保存,可用 downloadFile 下载。",
|
|
112
111
|
}, null, 2);
|
|
113
112
|
}
|
|
114
113
|
catch (error) {
|
|
114
|
+
// 中断(abort)时返回明确的中断结果
|
|
115
|
+
if (signal?.aborted) {
|
|
116
|
+
return JSON.stringify({
|
|
117
|
+
success: false,
|
|
118
|
+
aborted: true,
|
|
119
|
+
error: "图片生成被中断(aborted)",
|
|
120
|
+
});
|
|
121
|
+
}
|
|
115
122
|
return JSON.stringify({
|
|
116
123
|
success: false,
|
|
117
124
|
error: error instanceof Error ? error.message : "图片生成失败",
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { SdkCallbackTool } from "../../../runtime/SdkCallbackTool.js";
|
|
2
2
|
import type { ToolEnv } from "../../../types/index.js";
|
|
3
|
+
import type { AgentNS, ToolCallContext } from "@ai-zen/agents-core";
|
|
3
4
|
export declare class GlobTool extends SdkCallbackTool {
|
|
5
|
+
function: AgentNS.FunctionDefine;
|
|
4
6
|
constructor(env: ToolEnv);
|
|
5
7
|
call(input: {
|
|
6
8
|
path?: string;
|
|
7
9
|
pattern: string;
|
|
8
10
|
exclude?: string[];
|
|
9
|
-
}): Promise<string>;
|
|
11
|
+
}, ctx?: ToolCallContext): Promise<string>;
|
|
10
12
|
}
|
|
@@ -1,38 +1,37 @@
|
|
|
1
1
|
import * as fsp from "fs/promises";
|
|
2
2
|
import { SdkCallbackTool } from "../../../runtime/SdkCallbackTool.js";
|
|
3
3
|
export class GlobTool extends SdkCallbackTool {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
items: {
|
|
24
|
-
type: "string",
|
|
25
|
-
},
|
|
26
|
-
},
|
|
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",
|
|
27
23
|
},
|
|
28
|
-
required: ["pattern"],
|
|
29
|
-
additionalProperties: false,
|
|
30
24
|
},
|
|
31
25
|
},
|
|
32
|
-
|
|
33
|
-
|
|
26
|
+
required: ["pattern"],
|
|
27
|
+
additionalProperties: false,
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
constructor(env) {
|
|
31
|
+
super({ env });
|
|
34
32
|
}
|
|
35
|
-
async call(input) {
|
|
33
|
+
async call(input, ctx) {
|
|
34
|
+
const signal = ctx?.signal;
|
|
36
35
|
try {
|
|
37
36
|
const cwd = input.path ? this.resolve(input.path) : this.env.cwd;
|
|
38
37
|
const result = [];
|
|
@@ -40,8 +39,13 @@ export class GlobTool extends SdkCallbackTool {
|
|
|
40
39
|
exclude: input.exclude || ["**/node_modules/**"],
|
|
41
40
|
cwd,
|
|
42
41
|
})) {
|
|
42
|
+
if (signal?.aborted)
|
|
43
|
+
break; // 中断:停止继续遍历/消费
|
|
43
44
|
result.push(file);
|
|
44
45
|
}
|
|
46
|
+
if (signal?.aborted) {
|
|
47
|
+
return JSON.stringify({ aborted: true, files: result });
|
|
48
|
+
}
|
|
45
49
|
return JSON.stringify(result);
|
|
46
50
|
}
|
|
47
51
|
catch (error) {
|