@ai-zen/agents-sdk 0.3.4 → 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 +35 -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/discovery/mcp.js +2 -1
- package/dist/capabilities/discovery/skills.js +2 -2
- package/dist/capabilities/discovery/usertools.js +3 -2
- 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/capabilities/implements/mcpTools.js +6 -4
- package/dist/capabilities/implements/skillTools.js +2 -2
- package/dist/index.d.ts +6 -10
- package/dist/index.js +7 -10
- package/dist/plugin/AutoMigratePlugin.js +2 -2
- package/dist/runtime/McpConnectionManager.js +4 -3
- 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/shared/logger.d.ts +6 -2
- package/dist/shared/logger.js +14 -7
- 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,28 @@
|
|
|
1
|
+
import { Tool, FunctionCallContext } from "@ai-zen/agents-core";
|
|
2
|
+
import type { AgentNS } from "@ai-zen/agents-core";
|
|
3
|
+
import type { ToolEnv } from "../types/index.js";
|
|
4
|
+
/**
|
|
5
|
+
* SdkCallbackTool — SDK 内置工具基类。
|
|
6
|
+
*
|
|
7
|
+
* 环境(ToolEnv)在构造时注入,工具逻辑在子类的 call() 中实现,
|
|
8
|
+
* 不再依赖全局 process.cwd(),也不需要在运行时反查 Agent 上下文。
|
|
9
|
+
*
|
|
10
|
+
* 设计原则:
|
|
11
|
+
* - 一个工具一个类,文件名 = 类名(PascalCase)
|
|
12
|
+
* - 依赖环境(cwd、config)通过构造参数显式注入
|
|
13
|
+
* - 相对路径统一用 resolve() 解析到 env.cwd
|
|
14
|
+
*/
|
|
15
|
+
export declare abstract class SdkCallbackTool extends Tool {
|
|
16
|
+
/** 注入的工具环境 */
|
|
17
|
+
readonly env: ToolEnv;
|
|
18
|
+
constructor(options: {
|
|
19
|
+
function: AgentNS.FunctionDefine;
|
|
20
|
+
env: ToolEnv;
|
|
21
|
+
});
|
|
22
|
+
/** 工具核心逻辑(由子类实现,参数为 parsed_args) */
|
|
23
|
+
abstract call(input: unknown): unknown | Promise<unknown>;
|
|
24
|
+
/** 桥接 core 的 Tool.exec:解析参数 → call → 序列化 */
|
|
25
|
+
exec(ctx: FunctionCallContext): Promise<string>;
|
|
26
|
+
/** 将相对路径解析到 env.cwd,绝对路径原样返回 */
|
|
27
|
+
resolve(p: string): string;
|
|
28
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { Tool } from "@ai-zen/agents-core";
|
|
3
|
+
/**
|
|
4
|
+
* SdkCallbackTool — SDK 内置工具基类。
|
|
5
|
+
*
|
|
6
|
+
* 环境(ToolEnv)在构造时注入,工具逻辑在子类的 call() 中实现,
|
|
7
|
+
* 不再依赖全局 process.cwd(),也不需要在运行时反查 Agent 上下文。
|
|
8
|
+
*
|
|
9
|
+
* 设计原则:
|
|
10
|
+
* - 一个工具一个类,文件名 = 类名(PascalCase)
|
|
11
|
+
* - 依赖环境(cwd、config)通过构造参数显式注入
|
|
12
|
+
* - 相对路径统一用 resolve() 解析到 env.cwd
|
|
13
|
+
*/
|
|
14
|
+
export class SdkCallbackTool extends Tool {
|
|
15
|
+
/** 注入的工具环境 */
|
|
16
|
+
env;
|
|
17
|
+
constructor(options) {
|
|
18
|
+
super({ function: options.function });
|
|
19
|
+
this.env = options.env;
|
|
20
|
+
}
|
|
21
|
+
/** 桥接 core 的 Tool.exec:解析参数 → call → 序列化 */
|
|
22
|
+
async exec(ctx) {
|
|
23
|
+
const result = await this.call(ctx.parsed_args);
|
|
24
|
+
if (typeof result === "string")
|
|
25
|
+
return result;
|
|
26
|
+
// JSON.stringify(undefined) 返回 undefined(非字符串),会破坏 Promise<string> 契约
|
|
27
|
+
return JSON.stringify(result) ?? "";
|
|
28
|
+
}
|
|
29
|
+
/** 将相对路径解析到 env.cwd,绝对路径原样返回 */
|
|
30
|
+
resolve(p) {
|
|
31
|
+
return path.isAbsolute(p) ? p : path.join(this.env.cwd, p);
|
|
32
|
+
}
|
|
33
|
+
}
|
package/dist/shared/logger.d.ts
CHANGED
|
@@ -5,6 +5,10 @@ export interface LogFunctions {
|
|
|
5
5
|
}
|
|
6
6
|
export type Logger = LogFunctions;
|
|
7
7
|
/**
|
|
8
|
-
*
|
|
8
|
+
* 获取全局 Logger 实例。
|
|
9
9
|
*/
|
|
10
|
-
export declare function
|
|
10
|
+
export declare function getLogger(): Logger;
|
|
11
|
+
/**
|
|
12
|
+
* 设置全局 Logger 实例,替换默认的 console 输出。
|
|
13
|
+
*/
|
|
14
|
+
export declare function setLogger(log: Logger): void;
|
package/dist/shared/logger.js
CHANGED
|
@@ -1,10 +1,17 @@
|
|
|
1
|
+
let currentLogger = {
|
|
2
|
+
info: console.log,
|
|
3
|
+
warn: console.warn,
|
|
4
|
+
error: console.error,
|
|
5
|
+
};
|
|
1
6
|
/**
|
|
2
|
-
*
|
|
7
|
+
* 获取全局 Logger 实例。
|
|
3
8
|
*/
|
|
4
|
-
export function
|
|
5
|
-
return
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
9
|
+
export function getLogger() {
|
|
10
|
+
return currentLogger;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* 设置全局 Logger 实例,替换默认的 console 输出。
|
|
14
|
+
*/
|
|
15
|
+
export function setLogger(log) {
|
|
16
|
+
currentLogger = log;
|
|
10
17
|
}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -45,26 +45,6 @@ export interface AgentDefinition {
|
|
|
45
45
|
updatedAt: string;
|
|
46
46
|
version?: number;
|
|
47
47
|
}
|
|
48
|
-
/** 对话记录 */
|
|
49
|
-
export interface Conversation {
|
|
50
|
-
id: string;
|
|
51
|
-
agentId: string;
|
|
52
|
-
modelId: string;
|
|
53
|
-
messages: AgentNS.Message[];
|
|
54
|
-
lastPromptTokens?: number;
|
|
55
|
-
cwd?: string;
|
|
56
|
-
createdAt: string;
|
|
57
|
-
updatedAt: string;
|
|
58
|
-
}
|
|
59
|
-
/** 当前会话草稿 */
|
|
60
|
-
export interface Draft {
|
|
61
|
-
conversationId?: string;
|
|
62
|
-
agentId: string;
|
|
63
|
-
modelId: string;
|
|
64
|
-
messages: AgentNS.Message[];
|
|
65
|
-
cwd?: string;
|
|
66
|
-
updatedAt: string;
|
|
67
|
-
}
|
|
68
48
|
/** 图片生成模型配置 */
|
|
69
49
|
export interface ImageModel {
|
|
70
50
|
id: string;
|
|
@@ -90,6 +70,18 @@ export interface AppConfig {
|
|
|
90
70
|
/** 默认迁移模型 ID */
|
|
91
71
|
defaultMigrationModel?: string;
|
|
92
72
|
}
|
|
73
|
+
/**
|
|
74
|
+
* 工具环境 — Provider 在实例化内置工具时注入的环境信息。
|
|
75
|
+
*
|
|
76
|
+
* cwd 是相对路径解析的基准(每个 Provider 对应一个工作目录);
|
|
77
|
+
* config 供需要模型/端点配置的工具(如 generateImage)使用。
|
|
78
|
+
*/
|
|
79
|
+
export interface ToolEnv {
|
|
80
|
+
/** 当前工作目录 */
|
|
81
|
+
cwd: string;
|
|
82
|
+
/** 应用配置 */
|
|
83
|
+
config: AppConfig;
|
|
84
|
+
}
|
|
93
85
|
/** MCP 连接状态 */
|
|
94
86
|
export type McpConnectionState = "disconnected" | "connecting" | "connected" | "error";
|
|
95
87
|
/** MCP 服务器配置 */
|
package/package.json
CHANGED
|
@@ -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
|
-
});
|