@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
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { CallbackTool } from "@ai-zen/agents-core";
|
|
2
|
-
import * as fsp from "fs/promises";
|
|
3
|
-
export const rmTool = new CallbackTool({
|
|
4
|
-
function: {
|
|
5
|
-
name: "rm",
|
|
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.rm(input.path, {
|
|
26
|
-
recursive: input.recursive ?? false,
|
|
27
|
-
});
|
|
28
|
-
return "success";
|
|
29
|
-
}
|
|
30
|
-
catch (error) {
|
|
31
|
-
return error?.message;
|
|
32
|
-
}
|
|
33
|
-
},
|
|
34
|
-
});
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { CallbackTool } from "@ai-zen/agents-core";
|
|
2
|
-
export const sleepTool = new CallbackTool({
|
|
3
|
-
function: {
|
|
4
|
-
name: "sleep",
|
|
5
|
-
description: "等待指定的时间(毫秒)。用于需要延迟执行的场景,例如等待其他操作完成、操作间隔控制等。注意:过长的等待时间会影响响应速度,建议不超过 30000 毫秒(30 秒)。",
|
|
6
|
-
parameters: {
|
|
7
|
-
type: "object",
|
|
8
|
-
properties: {
|
|
9
|
-
ms: {
|
|
10
|
-
type: "number",
|
|
11
|
-
description: "等待的毫秒数(1 秒 = 1000 毫秒)。建议不超过 30000(30 秒)。",
|
|
12
|
-
},
|
|
13
|
-
},
|
|
14
|
-
required: ["ms"],
|
|
15
|
-
additionalProperties: false,
|
|
16
|
-
},
|
|
17
|
-
},
|
|
18
|
-
async callback(input) {
|
|
19
|
-
const ms = input.ms;
|
|
20
|
-
if (typeof ms !== "number" || !Number.isFinite(ms) || ms < 0) {
|
|
21
|
-
return JSON.stringify({
|
|
22
|
-
success: false,
|
|
23
|
-
error: "ms 必须是正整数",
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
if (ms > 300_000) {
|
|
27
|
-
// 超过 5 分钟给出警告
|
|
28
|
-
return JSON.stringify({
|
|
29
|
-
success: false,
|
|
30
|
-
error: `等待时间 ${ms}ms 过长,建议不超过 300000ms(5 分钟)。如需长时间等待请分段执行。`,
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
await new Promise((resolve) => setTimeout(resolve, ms));
|
|
34
|
-
return JSON.stringify({
|
|
35
|
-
success: true,
|
|
36
|
-
waitedMs: ms,
|
|
37
|
-
waitedSeconds: (ms / 1000).toFixed(1),
|
|
38
|
-
message: `已等待 ${ms}ms(${(ms / 1000).toFixed(1)} 秒)`,
|
|
39
|
-
});
|
|
40
|
-
},
|
|
41
|
-
});
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { CallbackTool } from "@ai-zen/agents-core";
|
|
2
|
-
import * as fsp from "fs/promises";
|
|
3
|
-
import * as path from "path";
|
|
4
|
-
export const writeFileTool = new CallbackTool({
|
|
5
|
-
function: {
|
|
6
|
-
name: "writeFile",
|
|
7
|
-
description: "写入文件",
|
|
8
|
-
parameters: {
|
|
9
|
-
type: "object",
|
|
10
|
-
properties: {
|
|
11
|
-
path: {
|
|
12
|
-
type: "string",
|
|
13
|
-
description: "文件路径",
|
|
14
|
-
},
|
|
15
|
-
content: {
|
|
16
|
-
type: "string",
|
|
17
|
-
description: "文件内容",
|
|
18
|
-
},
|
|
19
|
-
},
|
|
20
|
-
required: ["path", "content"],
|
|
21
|
-
additionalProperties: false,
|
|
22
|
-
},
|
|
23
|
-
},
|
|
24
|
-
async callback(input) {
|
|
25
|
-
try {
|
|
26
|
-
await fsp.mkdir(path.dirname(input.path), { recursive: true });
|
|
27
|
-
await fsp.writeFile(input.path, input.content);
|
|
28
|
-
return "success";
|
|
29
|
-
}
|
|
30
|
-
catch (error) {
|
|
31
|
-
return error?.message;
|
|
32
|
-
}
|
|
33
|
-
},
|
|
34
|
-
});
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { Conversation } from "../types/index.js";
|
|
2
|
-
import { EntityRepository } from "../shared/EntityRepository.js";
|
|
3
|
-
/**
|
|
4
|
-
* Conversation 仓储。
|
|
5
|
-
* 每个 Conversation 一个 JSON 文件存储在 ${conversationsDir}/${id}.json。
|
|
6
|
-
*/
|
|
7
|
-
export declare class ConversationRepository extends EntityRepository<Conversation> {
|
|
8
|
-
constructor(conversationsDir: string);
|
|
9
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { EntityRepository } from "../shared/EntityRepository.js";
|
|
2
|
-
/**
|
|
3
|
-
* Conversation 仓储。
|
|
4
|
-
* 每个 Conversation 一个 JSON 文件存储在 ${conversationsDir}/${id}.json。
|
|
5
|
-
*/
|
|
6
|
-
export class ConversationRepository extends EntityRepository {
|
|
7
|
-
constructor(conversationsDir) {
|
|
8
|
-
super(conversationsDir);
|
|
9
|
-
}
|
|
10
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import type { Draft } from "../types/index.js";
|
|
2
|
-
/**
|
|
3
|
-
* Draft 仓储。
|
|
4
|
-
* Draft 没有 id 字段,文件名基于 conversationId:
|
|
5
|
-
* - 有 conversationId → ${conversationId}.json
|
|
6
|
-
* - 无 conversationId → _current.json
|
|
7
|
-
*/
|
|
8
|
-
export declare class DraftRepository {
|
|
9
|
-
private dir;
|
|
10
|
-
constructor(dir: string);
|
|
11
|
-
private path;
|
|
12
|
-
read(conversationId?: string): Promise<Draft | null>;
|
|
13
|
-
write(draft: Draft): Promise<void>;
|
|
14
|
-
delete(conversationId?: string): Promise<void>;
|
|
15
|
-
}
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import { promises as fs } from "node:fs";
|
|
2
|
-
import { join } from "node:path";
|
|
3
|
-
const CURRENT_DRAFT = "_current.json";
|
|
4
|
-
/**
|
|
5
|
-
* Draft 仓储。
|
|
6
|
-
* Draft 没有 id 字段,文件名基于 conversationId:
|
|
7
|
-
* - 有 conversationId → ${conversationId}.json
|
|
8
|
-
* - 无 conversationId → _current.json
|
|
9
|
-
*/
|
|
10
|
-
export class DraftRepository {
|
|
11
|
-
dir;
|
|
12
|
-
constructor(dir) {
|
|
13
|
-
this.dir = dir;
|
|
14
|
-
}
|
|
15
|
-
path(conversationId) {
|
|
16
|
-
return join(this.dir, conversationId ? `${conversationId}.json` : CURRENT_DRAFT);
|
|
17
|
-
}
|
|
18
|
-
async read(conversationId) {
|
|
19
|
-
const p = this.path(conversationId);
|
|
20
|
-
try {
|
|
21
|
-
await fs.access(p);
|
|
22
|
-
}
|
|
23
|
-
catch {
|
|
24
|
-
return null;
|
|
25
|
-
}
|
|
26
|
-
try {
|
|
27
|
-
return JSON.parse(await fs.readFile(p, "utf-8"));
|
|
28
|
-
}
|
|
29
|
-
catch {
|
|
30
|
-
return null;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
async write(draft) {
|
|
34
|
-
try {
|
|
35
|
-
await fs.access(this.dir);
|
|
36
|
-
}
|
|
37
|
-
catch {
|
|
38
|
-
await fs.mkdir(this.dir, { recursive: true });
|
|
39
|
-
}
|
|
40
|
-
await fs.writeFile(this.path(draft.conversationId), JSON.stringify(draft, null, 2), "utf-8");
|
|
41
|
-
}
|
|
42
|
-
async delete(conversationId) {
|
|
43
|
-
const p = this.path(conversationId);
|
|
44
|
-
try {
|
|
45
|
-
await fs.access(p);
|
|
46
|
-
await fs.unlink(p);
|
|
47
|
-
}
|
|
48
|
-
catch {
|
|
49
|
-
// 文件不存在,忽略
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
}
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import type { Draft } from "../types/index.js";
|
|
2
|
-
import type { AgentPlugin, SendContext } from "../runtime/SdkAgent.js";
|
|
3
|
-
export interface AutoDraftOptions {
|
|
4
|
-
draftsDir: string;
|
|
5
|
-
agentId: string;
|
|
6
|
-
modelId: string;
|
|
7
|
-
conversationId?: string;
|
|
8
|
-
cwd?: string;
|
|
9
|
-
}
|
|
10
|
-
/**
|
|
11
|
-
* 自动保存 Draft 插件。
|
|
12
|
-
*
|
|
13
|
-
* ```ts
|
|
14
|
-
* agent.use(new AutoDraftPlugin({
|
|
15
|
-
* draftsDir: "/path/to/drafts",
|
|
16
|
-
* agentId: "my-agent",
|
|
17
|
-
* modelId: "gpt-4",
|
|
18
|
-
* }));
|
|
19
|
-
* ```
|
|
20
|
-
*/
|
|
21
|
-
export declare class AutoDraftPlugin implements AgentPlugin {
|
|
22
|
-
private options;
|
|
23
|
-
constructor(options: AutoDraftOptions);
|
|
24
|
-
onInnerLoopEnd(ctx: SendContext): Promise<void>;
|
|
25
|
-
static checkDraftForRestore(draftsDir: string): Promise<Draft | null>;
|
|
26
|
-
}
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import { DraftRepository } from "../crud/DraftRepository.js";
|
|
2
|
-
import { getLogger } from "../shared/logger.js";
|
|
3
|
-
import { promises as fs } from "node:fs";
|
|
4
|
-
import { join } from "node:path";
|
|
5
|
-
import dayjs from "dayjs";
|
|
6
|
-
const log = getLogger();
|
|
7
|
-
const CURRENT_DRAFT = "_current.json";
|
|
8
|
-
const EXPIRE_DAYS = 7;
|
|
9
|
-
/**
|
|
10
|
-
* 自动保存 Draft 插件。
|
|
11
|
-
*
|
|
12
|
-
* ```ts
|
|
13
|
-
* agent.use(new AutoDraftPlugin({
|
|
14
|
-
* draftsDir: "/path/to/drafts",
|
|
15
|
-
* agentId: "my-agent",
|
|
16
|
-
* modelId: "gpt-4",
|
|
17
|
-
* }));
|
|
18
|
-
* ```
|
|
19
|
-
*/
|
|
20
|
-
export class AutoDraftPlugin {
|
|
21
|
-
options;
|
|
22
|
-
constructor(options) {
|
|
23
|
-
this.options = options;
|
|
24
|
-
}
|
|
25
|
-
async onInnerLoopEnd(ctx) {
|
|
26
|
-
const { draftsDir, agentId, modelId, conversationId, cwd } = this.options;
|
|
27
|
-
try {
|
|
28
|
-
const draft = {
|
|
29
|
-
conversationId,
|
|
30
|
-
agentId,
|
|
31
|
-
modelId,
|
|
32
|
-
messages: ctx.agent.messages,
|
|
33
|
-
cwd,
|
|
34
|
-
updatedAt: new Date().toISOString(),
|
|
35
|
-
};
|
|
36
|
-
const repo = new DraftRepository(draftsDir);
|
|
37
|
-
await repo.write(draft);
|
|
38
|
-
}
|
|
39
|
-
catch (err) {
|
|
40
|
-
log.error(`[autoDraft] 保存失败: ${err?.message ?? err}`);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
static async checkDraftForRestore(draftsDir) {
|
|
44
|
-
const path = join(draftsDir, CURRENT_DRAFT);
|
|
45
|
-
try {
|
|
46
|
-
await fs.access(path);
|
|
47
|
-
}
|
|
48
|
-
catch {
|
|
49
|
-
return null;
|
|
50
|
-
}
|
|
51
|
-
const repo = new DraftRepository(draftsDir);
|
|
52
|
-
const draft = await repo.read();
|
|
53
|
-
if (!draft)
|
|
54
|
-
return null;
|
|
55
|
-
const age = dayjs().diff(dayjs(draft.updatedAt), "day");
|
|
56
|
-
if (age > EXPIRE_DAYS) {
|
|
57
|
-
await repo.delete();
|
|
58
|
-
return null;
|
|
59
|
-
}
|
|
60
|
-
return draft;
|
|
61
|
-
}
|
|
62
|
-
}
|