@ai-zen/agents-sdk 0.2.0-alpha.2

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.
Files changed (97) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +100 -0
  3. package/dist/capabilities/Capabilities.d.ts +71 -0
  4. package/dist/capabilities/Capabilities.js +161 -0
  5. package/dist/capabilities/PermissionEvaluator.d.ts +19 -0
  6. package/dist/capabilities/PermissionEvaluator.js +50 -0
  7. package/dist/capabilities/disclosure.d.ts +10 -0
  8. package/dist/capabilities/disclosure.js +17 -0
  9. package/dist/capabilities/discovery/builtin.d.ts +8 -0
  10. package/dist/capabilities/discovery/builtin.js +17 -0
  11. package/dist/capabilities/discovery/mcp.d.ts +7 -0
  12. package/dist/capabilities/discovery/mcp.js +29 -0
  13. package/dist/capabilities/discovery/skills.d.ts +43 -0
  14. package/dist/capabilities/discovery/skills.js +175 -0
  15. package/dist/capabilities/discovery/subagents.d.ts +7 -0
  16. package/dist/capabilities/discovery/subagents.js +39 -0
  17. package/dist/capabilities/discovery/usertools.d.ts +13 -0
  18. package/dist/capabilities/discovery/usertools.js +136 -0
  19. package/dist/capabilities/implements/builtin/batchEdit.d.ts +2 -0
  20. package/dist/capabilities/implements/builtin/batchEdit.js +76 -0
  21. package/dist/capabilities/implements/builtin/copy.d.ts +2 -0
  22. package/dist/capabilities/implements/builtin/copy.js +38 -0
  23. package/dist/capabilities/implements/builtin/cwd.d.ts +2 -0
  24. package/dist/capabilities/implements/builtin/cwd.js +15 -0
  25. package/dist/capabilities/implements/builtin/downloadFile.d.ts +2 -0
  26. package/dist/capabilities/implements/builtin/downloadFile.js +70 -0
  27. package/dist/capabilities/implements/builtin/edit.d.ts +2 -0
  28. package/dist/capabilities/implements/builtin/edit.js +79 -0
  29. package/dist/capabilities/implements/builtin/exec.d.ts +2 -0
  30. package/dist/capabilities/implements/builtin/exec.js +31 -0
  31. package/dist/capabilities/implements/builtin/exist.d.ts +2 -0
  32. package/dist/capabilities/implements/builtin/exist.js +25 -0
  33. package/dist/capabilities/implements/builtin/findText.d.ts +2 -0
  34. package/dist/capabilities/implements/builtin/findText.js +86 -0
  35. package/dist/capabilities/implements/builtin/generateImage.d.ts +9 -0
  36. package/dist/capabilities/implements/builtin/generateImage.js +112 -0
  37. package/dist/capabilities/implements/builtin/glob.d.ts +2 -0
  38. package/dist/capabilities/implements/builtin/glob.js +45 -0
  39. package/dist/capabilities/implements/builtin/index.d.ts +19 -0
  40. package/dist/capabilities/implements/builtin/index.js +34 -0
  41. package/dist/capabilities/implements/builtin/ls.d.ts +2 -0
  42. package/dist/capabilities/implements/builtin/ls.js +28 -0
  43. package/dist/capabilities/implements/builtin/mkdir.d.ts +2 -0
  44. package/dist/capabilities/implements/builtin/mkdir.js +34 -0
  45. package/dist/capabilities/implements/builtin/readFile.d.ts +2 -0
  46. package/dist/capabilities/implements/builtin/readFile.js +31 -0
  47. package/dist/capabilities/implements/builtin/rename.d.ts +2 -0
  48. package/dist/capabilities/implements/builtin/rename.js +32 -0
  49. package/dist/capabilities/implements/builtin/rm.d.ts +2 -0
  50. package/dist/capabilities/implements/builtin/rm.js +34 -0
  51. package/dist/capabilities/implements/builtin/writeFile.d.ts +2 -0
  52. package/dist/capabilities/implements/builtin/writeFile.js +34 -0
  53. package/dist/capabilities/implements/mcpTools.d.ts +20 -0
  54. package/dist/capabilities/implements/mcpTools.js +147 -0
  55. package/dist/capabilities/implements/skillTools.d.ts +15 -0
  56. package/dist/capabilities/implements/skillTools.js +113 -0
  57. package/dist/capabilities/implements/subAgentTools.d.ts +11 -0
  58. package/dist/capabilities/implements/subAgentTools.js +53 -0
  59. package/dist/config/ConfigManager.d.ts +49 -0
  60. package/dist/config/ConfigManager.js +126 -0
  61. package/dist/config/constants.d.ts +20 -0
  62. package/dist/config/constants.js +185 -0
  63. package/dist/crud/AgentRepository.d.ts +9 -0
  64. package/dist/crud/AgentRepository.js +10 -0
  65. package/dist/crud/ConversationRepository.d.ts +9 -0
  66. package/dist/crud/ConversationRepository.js +10 -0
  67. package/dist/crud/DraftRepository.d.ts +15 -0
  68. package/dist/crud/DraftRepository.js +41 -0
  69. package/dist/index.d.ts +40 -0
  70. package/dist/index.js +37 -0
  71. package/dist/plugin/AutoDraftPlugin.d.ts +26 -0
  72. package/dist/plugin/AutoDraftPlugin.js +58 -0
  73. package/dist/plugin/AutoMigratePlugin.d.ts +25 -0
  74. package/dist/plugin/AutoMigratePlugin.js +74 -0
  75. package/dist/plugin/AutoRefreshToolsPlugin.d.ts +12 -0
  76. package/dist/plugin/AutoRefreshToolsPlugin.js +23 -0
  77. package/dist/runtime/McpConnectionManager.d.ts +42 -0
  78. package/dist/runtime/McpConnectionManager.js +238 -0
  79. package/dist/runtime/Provider.d.ts +44 -0
  80. package/dist/runtime/Provider.js +38 -0
  81. package/dist/runtime/SdkAgent.d.ts +100 -0
  82. package/dist/runtime/SdkAgent.js +103 -0
  83. package/dist/runtime/TaskMigrationService.d.ts +24 -0
  84. package/dist/runtime/TaskMigrationService.js +71 -0
  85. package/dist/runtime/createAgent.d.ts +14 -0
  86. package/dist/runtime/createAgent.js +41 -0
  87. package/dist/runtime/createModel.d.ts +11 -0
  88. package/dist/runtime/createModel.js +28 -0
  89. package/dist/shared/EntityRepository.d.ts +24 -0
  90. package/dist/shared/EntityRepository.js +62 -0
  91. package/dist/shared/errors.d.ts +8 -0
  92. package/dist/shared/errors.js +15 -0
  93. package/dist/shared/logger.d.ts +10 -0
  94. package/dist/shared/logger.js +10 -0
  95. package/dist/types/index.d.ts +178 -0
  96. package/dist/types/index.js +5 -0
  97. package/package.json +47 -0
@@ -0,0 +1,185 @@
1
+ import { AgentNS } from "@ai-zen/agents-core";
2
+ // ---------------------------------------------------------------------------
3
+ // 默认 Agent
4
+ // ---------------------------------------------------------------------------
5
+ /** 默认 Agent ID */
6
+ export const DEFAULT_AGENT_ID = "default";
7
+ /** 默认 Agent 定义(不含时间戳) */
8
+ export const DEFAULT_AGENT_DEFINITION = {
9
+ id: DEFAULT_AGENT_ID,
10
+ name: "默认助手",
11
+ description: "默认的 AI 助手,适用于日常问答和任务执行。",
12
+ messages: [
13
+ {
14
+ role: AgentNS.Role.System,
15
+ content: "你是一个AI助手,专门帮助用户回答问题和执行任务。请用中文回复。",
16
+ },
17
+ ],
18
+ permissions: {
19
+ tools: { allow: ["*"] },
20
+ skills: { allow: ["*"] },
21
+ mcps: { allow: ["*"] },
22
+ subagents: { allow: ["*"] },
23
+ },
24
+ };
25
+ // ---------------------------------------------------------------------------
26
+ // 默认 SubAgent
27
+ // ---------------------------------------------------------------------------
28
+ /** 默认 SubAgent ID */
29
+ export const DEFAULT_SUBAGENT_ID = "general-assistant";
30
+ /** 默认 SubAgent 定义(不含时间戳) */
31
+ export const DEFAULT_SUBAGENT_DEFINITION = {
32
+ id: DEFAULT_SUBAGENT_ID,
33
+ name: "通用助手",
34
+ description: "一个通用的子 Agent,擅长独立完成各类任务。",
35
+ messages: [
36
+ {
37
+ role: AgentNS.Role.System,
38
+ content: "你是一个通用助手,擅长独立完成各类任务。请根据给定的任务描述,认真分析并完成任务。完成任务后直接返回结果,不要解释你的思考过程。",
39
+ },
40
+ { role: AgentNS.Role.User, content: "{{query}}" },
41
+ ],
42
+ function: {
43
+ name: "general_assistant",
44
+ description: "通用助手,可独立完成各类任务",
45
+ parameters: {
46
+ type: "object",
47
+ properties: {
48
+ query: { type: "string", description: "任务描述" },
49
+ },
50
+ required: ["query"],
51
+ additionalProperties: false,
52
+ },
53
+ },
54
+ };
55
+ // ---------------------------------------------------------------------------
56
+ // 默认配置(含预置厂商、模型、图片模型)
57
+ // ---------------------------------------------------------------------------
58
+ /** SDK 出厂默认 AppConfig。CLI/Desktop 首次初始化时使用。 */
59
+ export const DEFAULT_APP_CONFIG = {
60
+ endpoints: [
61
+ {
62
+ id: "openai",
63
+ name: "OpenAI",
64
+ apiKey: "",
65
+ baseUrl: "https://api.openai.com/v1",
66
+ description: "OpenAI API 端点",
67
+ },
68
+ {
69
+ id: "bigmodelcn",
70
+ name: "BigModelCN (智谱AI)",
71
+ apiKey: "",
72
+ baseUrl: "https://open.bigmodel.cn/api/paas/v4",
73
+ description: "智谱AI大模型端点",
74
+ },
75
+ {
76
+ id: "deepseek",
77
+ name: "DeepSeek",
78
+ apiKey: "",
79
+ baseUrl: "https://api.deepseek.com/v1",
80
+ description: "DeepSeek API 端点",
81
+ },
82
+ ],
83
+ models: [
84
+ {
85
+ id: "gpt-5.5",
86
+ name: "GPT-5.5",
87
+ endpointId: "openai",
88
+ modelName: "gpt-5.5",
89
+ maxContextTokens: 250_000,
90
+ defaultParams: {},
91
+ description: "OpenAI 最新旗舰模型,擅长编程与代码调试、在线研究、数据分析",
92
+ },
93
+ {
94
+ id: "glm-5.1",
95
+ name: "GLM-5.1",
96
+ endpointId: "bigmodelcn",
97
+ modelName: "glm-5.1",
98
+ maxContextTokens: 250_000,
99
+ defaultParams: {},
100
+ description: "智谱AI 最新旗舰模型,支持8小时长程Agent任务",
101
+ },
102
+ {
103
+ id: "glm-5v-turbo",
104
+ name: "GLM-5V-Turbo",
105
+ endpointId: "bigmodelcn",
106
+ modelName: "glm-5v-turbo",
107
+ maxContextTokens: 250_000,
108
+ defaultParams: {},
109
+ description: "智谱AI 多模态Coding基座",
110
+ },
111
+ {
112
+ id: "glm-4.7-flash",
113
+ name: "GLM-4.7-Flash",
114
+ endpointId: "bigmodelcn",
115
+ modelName: "glm-4.7-flash",
116
+ maxContextTokens: 250_000,
117
+ defaultParams: {},
118
+ description: "智谱AI 免费轻量模型",
119
+ },
120
+ {
121
+ id: "deepseek-v4-pro",
122
+ name: "DeepSeek-V4-Pro",
123
+ endpointId: "deepseek",
124
+ modelName: "deepseek-v4-pro",
125
+ maxContextTokens: 250_000,
126
+ defaultParams: { thinking: { type: "disabled" } },
127
+ description: "DeepSeek 旗舰模型,Agentic Coding开源第一",
128
+ },
129
+ {
130
+ id: "deepseek-v4-flash",
131
+ name: "DeepSeek-V4-Flash",
132
+ endpointId: "deepseek",
133
+ modelName: "deepseek-v4-flash",
134
+ maxContextTokens: 250_000,
135
+ defaultParams: { thinking: { type: "disabled" } },
136
+ description: "DeepSeek 经济高效模型",
137
+ },
138
+ ],
139
+ imageModels: [
140
+ {
141
+ id: "cogview-4",
142
+ name: "CogView-4",
143
+ endpointId: "bigmodelcn",
144
+ modelName: "cogview-4",
145
+ defaultSize: "1024x1024",
146
+ },
147
+ {
148
+ id: "glm-image",
149
+ name: "GLM-Image",
150
+ endpointId: "bigmodelcn",
151
+ modelName: "glm-image",
152
+ defaultSize: "1280x1280",
153
+ defaultQuality: "hd",
154
+ },
155
+ {
156
+ id: "cogview-3-flash",
157
+ name: "CogView-3-Flash",
158
+ endpointId: "bigmodelcn",
159
+ modelName: "cogview-3-flash",
160
+ defaultSize: "1024x1024",
161
+ },
162
+ ],
163
+ defaultModel: "deepseek-v4-flash",
164
+ defaultImageModel: "cogview-4",
165
+ defaultAgent: "default",
166
+ defaultMigrationModel: "deepseek-v4-flash",
167
+ };
168
+ // ---------------------------------------------------------------------------
169
+ // 目录
170
+ // ---------------------------------------------------------------------------
171
+ /**
172
+ * 标准共享子目录列表(不包含运行时目录)。
173
+ *
174
+ * 各客户端(CLI/Desktop)的运行时数据(config.json、conversations/、drafts/)
175
+ * 由客户端自行在各自目录下管理:
176
+ * ~/.ai-zen/cli/ ← CLI 运行时
177
+ * ~/.ai-zen/desktop/ ← Desktop 运行时(未来)
178
+ */
179
+ export const CONFIG_SUB_DIRS = [
180
+ "agents",
181
+ "sub-agents",
182
+ "skills",
183
+ "tools",
184
+ "mcp-oauth",
185
+ ];
@@ -0,0 +1,9 @@
1
+ import type { AgentDefinition } from "../types/index.js";
2
+ import { EntityRepository } from "../shared/EntityRepository.js";
3
+ /**
4
+ * Agent 仓储。
5
+ * 每个 Agent 一个 JSON 文件存储在 ${agentsDir}/${id}.json。
6
+ */
7
+ export declare class AgentRepository extends EntityRepository<AgentDefinition> {
8
+ constructor(agentsDir: string);
9
+ }
@@ -0,0 +1,10 @@
1
+ import { EntityRepository } from "../shared/EntityRepository.js";
2
+ /**
3
+ * Agent 仓储。
4
+ * 每个 Agent 一个 JSON 文件存储在 ${agentsDir}/${id}.json。
5
+ */
6
+ export class AgentRepository extends EntityRepository {
7
+ constructor(agentsDir) {
8
+ super(agentsDir);
9
+ }
10
+ }
@@ -0,0 +1,9 @@
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
+ }
@@ -0,0 +1,10 @@
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
+ }
@@ -0,0 +1,15 @@
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): Draft | null;
13
+ write(draft: Draft): void;
14
+ delete(conversationId?: string): void;
15
+ }
@@ -0,0 +1,41 @@
1
+ import { readFileSync, writeFileSync, existsSync, unlinkSync, mkdirSync } 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
+ read(conversationId) {
19
+ const p = this.path(conversationId);
20
+ if (!existsSync(p))
21
+ return null;
22
+ try {
23
+ return JSON.parse(readFileSync(p, "utf-8"));
24
+ }
25
+ catch {
26
+ return null;
27
+ }
28
+ }
29
+ write(draft) {
30
+ if (!existsSync(this.dir)) {
31
+ mkdirSync(this.dir, { recursive: true });
32
+ }
33
+ writeFileSync(this.path(draft.conversationId), JSON.stringify(draft, null, 2), "utf-8");
34
+ }
35
+ delete(conversationId) {
36
+ const p = this.path(conversationId);
37
+ if (existsSync(p)) {
38
+ unlinkSync(p);
39
+ }
40
+ }
41
+ }
@@ -0,0 +1,40 @@
1
+ export type { PermissionPolicy, AgentPermissions, Endpoint, Model, ImageModel, AgentDefinition, Conversation, Draft, AppConfig, McpIcon, McpAnnotations, McpToolDef, McpResourceDef, McpPromptDef, McpServerManifest, McpServerConfig, McpConnectionState, } from "./types/index.js";
2
+ export { Capabilities } from "./capabilities/Capabilities.js";
3
+ export { PermissionEvaluator } from "./capabilities/PermissionEvaluator.js";
4
+ export type { CandidateSets } from "./capabilities/PermissionEvaluator.js";
5
+ export { createDisclosureParam } from "./capabilities/disclosure.js";
6
+ export type { DisclosureParam } from "./capabilities/disclosure.js";
7
+ export type { FilterOutput, ExcludeOptions } from "./capabilities/Capabilities.js";
8
+ export { discoverBuiltinTools } from "./capabilities/discovery/builtin.js";
9
+ export { discoverSubAgents } from "./capabilities/discovery/subagents.js";
10
+ export { discoverSkills, readSkill } from "./capabilities/discovery/skills.js";
11
+ export type { SkillInfo, Frontmatter } from "./capabilities/discovery/skills.js";
12
+ export { discoverMcpServers } from "./capabilities/discovery/mcp.js";
13
+ export { discoverUserTools } from "./capabilities/discovery/usertools.js";
14
+ export { ConfigManager, DEFAULT_AGENT_ID, DEFAULT_AGENT_DEFINITION, DEFAULT_SUBAGENT_ID, DEFAULT_SUBAGENT_DEFINITION, DEFAULT_APP_CONFIG, CONFIG_SUB_DIRS, } from "./config/ConfigManager.js";
15
+ export { EntityRepository } from "./shared/EntityRepository.js";
16
+ export { AgentRepository } from "./crud/AgentRepository.js";
17
+ export { ConversationRepository } from "./crud/ConversationRepository.js";
18
+ export { DraftRepository } from "./crud/DraftRepository.js";
19
+ export { Provider } from "./runtime/Provider.js";
20
+ export { createModel } from "./runtime/createModel.js";
21
+ export { SdkAgent } from "./runtime/SdkAgent.js";
22
+ export type { AgentPlugin, SendContext } from "./runtime/SdkAgent.js";
23
+ export { createAgent } from "./runtime/createAgent.js";
24
+ export { McpConnectionManager } from "./runtime/McpConnectionManager.js";
25
+ export type { McpConnectOptions } from "./runtime/McpConnectionManager.js";
26
+ export { TaskMigrationService } from "./runtime/TaskMigrationService.js";
27
+ export type { BuildMigrationAgentOptions } from "./runtime/TaskMigrationService.js";
28
+ export { AutoMigratePlugin } from "./plugin/AutoMigratePlugin.js";
29
+ export type { AutoMigrateOptions } from "./plugin/AutoMigratePlugin.js";
30
+ export { AutoDraftPlugin } from "./plugin/AutoDraftPlugin.js";
31
+ export type { AutoDraftOptions } from "./plugin/AutoDraftPlugin.js";
32
+ export { AutoRefreshToolsPlugin } from "./plugin/AutoRefreshToolsPlugin.js";
33
+ export { BUILTIN_TOOLS } from "./capabilities/implements/builtin/index.js";
34
+ export { createGenerateImageTool } from "./capabilities/implements/builtin/generateImage.js";
35
+ export { createLoadSkillTool, createCallSkillSubAgentTool } from "./capabilities/implements/skillTools.js";
36
+ export { createLoadMcpTool, createCallMcpTool, createReadMcpResourceTool } from "./capabilities/implements/mcpTools.js";
37
+ export { createSubAgentTool } from "./capabilities/implements/subAgentTools.js";
38
+ export { createLogger } from "./shared/logger.js";
39
+ export type { Logger, LogFunctions } from "./shared/logger.js";
40
+ export { SdkError } from "./shared/errors.js";
package/dist/index.js ADDED
@@ -0,0 +1,37 @@
1
+ // 能力管线
2
+ export { Capabilities } from "./capabilities/Capabilities.js";
3
+ export { PermissionEvaluator } from "./capabilities/PermissionEvaluator.js";
4
+ export { createDisclosureParam } from "./capabilities/disclosure.js";
5
+ // 发现
6
+ export { discoverBuiltinTools } from "./capabilities/discovery/builtin.js";
7
+ export { discoverSubAgents } from "./capabilities/discovery/subagents.js";
8
+ export { discoverSkills, readSkill } from "./capabilities/discovery/skills.js";
9
+ export { discoverMcpServers } from "./capabilities/discovery/mcp.js";
10
+ export { discoverUserTools } from "./capabilities/discovery/usertools.js";
11
+ // 配置 — ConfigManager
12
+ export { ConfigManager, DEFAULT_AGENT_ID, DEFAULT_AGENT_DEFINITION, DEFAULT_SUBAGENT_ID, DEFAULT_SUBAGENT_DEFINITION, DEFAULT_APP_CONFIG, CONFIG_SUB_DIRS, } from "./config/ConfigManager.js";
13
+ // CRUD — EntityRepository + 实体仓储
14
+ export { EntityRepository } from "./shared/EntityRepository.js";
15
+ export { AgentRepository } from "./crud/AgentRepository.js";
16
+ export { ConversationRepository } from "./crud/ConversationRepository.js";
17
+ export { DraftRepository } from "./crud/DraftRepository.js";
18
+ // 运行时
19
+ export { Provider } from "./runtime/Provider.js";
20
+ export { createModel } from "./runtime/createModel.js";
21
+ export { SdkAgent } from "./runtime/SdkAgent.js";
22
+ export { createAgent } from "./runtime/createAgent.js";
23
+ export { McpConnectionManager } from "./runtime/McpConnectionManager.js";
24
+ export { TaskMigrationService } from "./runtime/TaskMigrationService.js";
25
+ // 插件
26
+ export { AutoMigratePlugin } from "./plugin/AutoMigratePlugin.js";
27
+ export { AutoDraftPlugin } from "./plugin/AutoDraftPlugin.js";
28
+ export { AutoRefreshToolsPlugin } from "./plugin/AutoRefreshToolsPlugin.js";
29
+ // 工具
30
+ export { BUILTIN_TOOLS } from "./capabilities/implements/builtin/index.js";
31
+ export { createGenerateImageTool } from "./capabilities/implements/builtin/generateImage.js";
32
+ export { createLoadSkillTool, createCallSkillSubAgentTool } from "./capabilities/implements/skillTools.js";
33
+ export { createLoadMcpTool, createCallMcpTool, createReadMcpResourceTool } from "./capabilities/implements/mcpTools.js";
34
+ export { createSubAgentTool } from "./capabilities/implements/subAgentTools.js";
35
+ // 共享
36
+ export { createLogger } from "./shared/logger.js";
37
+ export { SdkError } from "./shared/errors.js";
@@ -0,0 +1,26 @@
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): Draft | null;
26
+ }
@@ -0,0 +1,58 @@
1
+ import { DraftRepository } from "../crud/DraftRepository.js";
2
+ import { createLogger } from "../shared/logger.js";
3
+ import { existsSync } from "node:fs";
4
+ import { join } from "node:path";
5
+ import dayjs from "dayjs";
6
+ const log = createLogger();
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
+ repo.write(draft);
38
+ }
39
+ catch (err) {
40
+ log.error(`[autoDraft] 保存失败: ${err?.message ?? err}`);
41
+ }
42
+ }
43
+ static checkDraftForRestore(draftsDir) {
44
+ const path = join(draftsDir, CURRENT_DRAFT);
45
+ if (!existsSync(path))
46
+ return null;
47
+ const repo = new DraftRepository(draftsDir);
48
+ const draft = repo.read();
49
+ if (!draft)
50
+ return null;
51
+ const age = dayjs().diff(dayjs(draft.updatedAt), "day");
52
+ if (age > EXPIRE_DAYS) {
53
+ repo.delete();
54
+ return null;
55
+ }
56
+ return draft;
57
+ }
58
+ }
@@ -0,0 +1,25 @@
1
+ import { SdkAgent } from "../runtime/SdkAgent.js";
2
+ import type { AgentPlugin, SendContext } from "../runtime/SdkAgent.js";
3
+ export interface AutoMigrateOptions {
4
+ maxTokens: number;
5
+ migrationAgent: SdkAgent;
6
+ onHandoff?: (handoffDoc: string, oldAgent: SdkAgent, newAgent: SdkAgent) => void;
7
+ }
8
+ /**
9
+ * 自动迁移插件。
10
+ *
11
+ * ```ts
12
+ * agent.use(new AutoMigratePlugin({
13
+ * maxTokens: 250_000,
14
+ * migrationAgent,
15
+ * onHandoff: (doc, old, next) => { ... },
16
+ * }));
17
+ * ```
18
+ */
19
+ export declare class AutoMigratePlugin implements AgentPlugin {
20
+ private options;
21
+ constructor(options: AutoMigrateOptions);
22
+ onAfterSend(ctx: SendContext): Promise<void>;
23
+ private serializeMessages;
24
+ private getLastAssistantContent;
25
+ }
@@ -0,0 +1,74 @@
1
+ import { AgentNS } from "@ai-zen/agents-core";
2
+ import { SdkAgent } from "../runtime/SdkAgent.js";
3
+ import { TaskMigrationService } from "../runtime/TaskMigrationService.js";
4
+ import { createLogger } from "../shared/logger.js";
5
+ const log = createLogger();
6
+ /**
7
+ * 自动迁移插件。
8
+ *
9
+ * ```ts
10
+ * agent.use(new AutoMigratePlugin({
11
+ * maxTokens: 250_000,
12
+ * migrationAgent,
13
+ * onHandoff: (doc, old, next) => { ... },
14
+ * }));
15
+ * ```
16
+ */
17
+ export class AutoMigratePlugin {
18
+ options;
19
+ constructor(options) {
20
+ this.options = options;
21
+ }
22
+ async onAfterSend(ctx) {
23
+ const { agent } = ctx;
24
+ const { maxTokens, migrationAgent, onHandoff } = this.options;
25
+ const promptTokens = agent.lastUsage?.prompt_tokens;
26
+ if (promptTokens == null)
27
+ return;
28
+ if (!TaskMigrationService.shouldMigrate(promptTokens, maxTokens))
29
+ return;
30
+ try {
31
+ const historyText = this.serializeMessages(agent.messages);
32
+ const migrationResult = await migrationAgent.send(historyText);
33
+ const handoffDoc = this.getLastAssistantContent(migrationResult);
34
+ if (!handoffDoc)
35
+ return;
36
+ const newMessages = [
37
+ ...agent.definition.messages,
38
+ ...TaskMigrationService.createPostMessages(handoffDoc),
39
+ ];
40
+ const newAgent = new SdkAgent({ ...agent, messages: newMessages });
41
+ ctx.agent = newAgent;
42
+ if (onHandoff) {
43
+ try {
44
+ await onHandoff(handoffDoc, agent, newAgent);
45
+ }
46
+ catch (err) {
47
+ log.error(`[autoMigrate] onHandoff 回调失败: ${err?.message ?? err}`);
48
+ }
49
+ }
50
+ }
51
+ catch (err) {
52
+ log.error(`[autoMigrate] 迁移失败: ${err?.message ?? err}`);
53
+ }
54
+ }
55
+ serializeMessages(messages) {
56
+ return messages
57
+ .map((m) => {
58
+ const role = m.role ?? "unknown";
59
+ const content = typeof m.content === "string" ? m.content : JSON.stringify(m.content);
60
+ return `[${role}]: ${content}`;
61
+ })
62
+ .join("\n\n");
63
+ }
64
+ getLastAssistantContent(messages) {
65
+ for (let i = messages.length - 1; i >= 0; i--) {
66
+ if (messages[i].role === AgentNS.Role.Assistant && messages[i].content) {
67
+ return typeof messages[i].content === "string"
68
+ ? messages[i].content
69
+ : null;
70
+ }
71
+ }
72
+ return null;
73
+ }
74
+ }
@@ -0,0 +1,12 @@
1
+ import type { AgentPlugin, SendContext } from "../runtime/SdkAgent.js";
2
+ /**
3
+ * 自动刷新能力插件。
4
+ *
5
+ * ```ts
6
+ * agent.use(new AutoRefreshToolsPlugin());
7
+ * await agent.init();
8
+ * ```
9
+ */
10
+ export declare class AutoRefreshToolsPlugin implements AgentPlugin {
11
+ onBeforeSend(ctx: SendContext): Promise<void>;
12
+ }
@@ -0,0 +1,23 @@
1
+ /**
2
+ * 自动刷新能力插件。
3
+ *
4
+ * ```ts
5
+ * agent.use(new AutoRefreshToolsPlugin());
6
+ * await agent.init();
7
+ * ```
8
+ */
9
+ export class AutoRefreshToolsPlugin {
10
+ async onBeforeSend(ctx) {
11
+ const { agent } = ctx;
12
+ if (!agent.caps)
13
+ return;
14
+ agent.caps.refresh();
15
+ agent.tools = agent.caps.buildTools(agent.permissions ?? {}, {
16
+ exclude: {
17
+ subagents: agent.definition.function?.name
18
+ ? [agent.definition.function.name]
19
+ : undefined,
20
+ },
21
+ });
22
+ }
23
+ }
@@ -0,0 +1,42 @@
1
+ import { Client } from "@modelcontextprotocol/sdk/client/index.js";
2
+ import type { Transport } from "@modelcontextprotocol/sdk/shared/transport.js";
3
+ import type { McpServerConfig, McpServerManifest, McpConnectionState } from "../types/index.js";
4
+ export interface McpConnectOptions {
5
+ /** 空闲超时(毫秒),默认 stdio: 30min, http: 5min */
6
+ idleTimeoutMs?: number;
7
+ /** 失败时自动重连 */
8
+ autoReconnect?: boolean;
9
+ /** 最大重试次数,默认 3 */
10
+ maxRetries?: number;
11
+ /** 判断是否为配置错误(不重试),默认全部重试 */
12
+ isConfigError?: (err: unknown) => boolean;
13
+ }
14
+ export declare class McpConnectionManager {
15
+ private servers;
16
+ /** 可替换的 transport 工厂,用于测试注入 */
17
+ private _transportFactory?;
18
+ /** 可替换的 Client 工厂,用于测试注入 */
19
+ private _clientFactory?;
20
+ /**
21
+ * @param transportFactory 可选的自定义 transport 工厂(用于测试注入)
22
+ * @param clientFactory 可选的自定义 Client 工厂(用于测试注入)
23
+ */
24
+ constructor(transportFactory?: (config: McpServerConfig) => Transport, clientFactory?: () => Client);
25
+ getState(name: string): McpConnectionState;
26
+ getManifest(name: string): McpServerManifest | undefined;
27
+ getClient(name: string): Client | undefined;
28
+ connect(name: string, config: McpServerConfig, options?: McpConnectOptions): Promise<McpServerManifest>;
29
+ private connectWithRetry;
30
+ private doConnect;
31
+ /**
32
+ * 根据配置创建对应的 Transport 实例。
33
+ * 如果设置了自定义工厂(测试用),则使用自定义工厂。
34
+ */
35
+ private createTransport;
36
+ disconnect(name: string): Promise<void>;
37
+ disconnectAll(): Promise<void>;
38
+ touch(name: string): void;
39
+ private scheduleTimeout;
40
+ private clearTimeout;
41
+ private throwNotConnected;
42
+ }