@shareai-lab/kode-sdk 1.0.0-beta.8 → 2.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/LICENSE +21 -0
- package/README.md +115 -273
- package/README.zh-CN.md +114 -0
- package/dist/core/agent/breakpoint-manager.d.ts +16 -0
- package/dist/core/agent/breakpoint-manager.js +36 -0
- package/dist/core/agent/message-queue.d.ts +26 -0
- package/dist/core/agent/message-queue.js +57 -0
- package/dist/core/agent/permission-manager.d.ts +9 -0
- package/dist/core/agent/permission-manager.js +32 -0
- package/dist/core/agent/todo-manager.d.ts +26 -0
- package/dist/core/agent/todo-manager.js +91 -0
- package/dist/core/agent/tool-runner.d.ts +9 -0
- package/dist/core/agent/tool-runner.js +45 -0
- package/dist/core/agent.d.ts +228 -62
- package/dist/core/agent.js +1890 -615
- package/dist/core/config.d.ts +10 -0
- package/dist/core/config.js +2 -0
- package/dist/core/context-manager.d.ts +82 -0
- package/dist/core/context-manager.js +241 -0
- package/dist/core/errors.d.ts +22 -0
- package/dist/core/errors.js +49 -0
- package/dist/core/events.d.ts +41 -10
- package/dist/core/events.js +270 -68
- package/dist/core/file-pool.d.ts +41 -0
- package/dist/core/file-pool.js +102 -0
- package/dist/core/hooks.d.ts +3 -3
- package/dist/core/hooks.js +1 -1
- package/dist/core/permission-modes.d.ts +31 -0
- package/dist/core/permission-modes.js +61 -0
- package/dist/core/pool.d.ts +56 -13
- package/dist/core/pool.js +244 -34
- package/dist/core/room.d.ts +2 -2
- package/dist/core/room.js +10 -10
- package/dist/core/scheduler.d.ts +30 -23
- package/dist/core/scheduler.js +42 -168
- package/dist/core/skills/index.d.ts +10 -0
- package/dist/core/skills/index.js +20 -0
- package/dist/core/skills/management-manager.d.ts +130 -0
- package/dist/core/skills/management-manager.js +557 -0
- package/dist/core/skills/manager.d.ts +47 -0
- package/dist/core/skills/manager.js +243 -0
- package/dist/core/skills/operation-queue.d.ts +87 -0
- package/dist/core/skills/operation-queue.js +113 -0
- package/dist/core/skills/sandbox-file-manager.d.ts +82 -0
- package/dist/core/skills/sandbox-file-manager.js +183 -0
- package/dist/core/skills/types.d.ts +120 -0
- package/dist/core/skills/types.js +9 -0
- package/dist/core/skills/xml-generator.d.ts +13 -0
- package/dist/core/skills/xml-generator.js +70 -0
- package/dist/core/template.d.ts +57 -0
- package/dist/core/template.js +35 -0
- package/dist/core/time-bridge.d.ts +18 -0
- package/dist/core/time-bridge.js +100 -0
- package/dist/core/todo.d.ts +34 -0
- package/dist/core/todo.js +89 -0
- package/dist/core/types.d.ts +311 -114
- package/dist/core/types.js +1 -12
- package/dist/index.d.ts +47 -9
- package/dist/index.js +108 -15
- package/dist/infra/db/postgres/postgres-store.d.ts +97 -0
- package/dist/infra/db/postgres/postgres-store.js +1073 -0
- package/dist/infra/db/sqlite/sqlite-store.d.ts +84 -0
- package/dist/infra/db/sqlite/sqlite-store.js +800 -0
- package/dist/infra/e2b/e2b-fs.d.ts +29 -0
- package/dist/infra/e2b/e2b-fs.js +128 -0
- package/dist/infra/e2b/e2b-sandbox.d.ts +37 -0
- package/dist/infra/e2b/e2b-sandbox.js +156 -0
- package/dist/infra/e2b/e2b-template.d.ts +24 -0
- package/dist/infra/e2b/e2b-template.js +105 -0
- package/dist/infra/e2b/index.d.ts +4 -0
- package/dist/infra/e2b/index.js +9 -0
- package/dist/infra/e2b/types.d.ts +46 -0
- package/dist/infra/e2b/types.js +2 -0
- package/dist/infra/provider.d.ts +17 -58
- package/dist/infra/provider.js +65 -116
- package/dist/infra/providers/anthropic.d.ts +42 -0
- package/dist/infra/providers/anthropic.js +308 -0
- package/dist/infra/providers/core/errors.d.ts +230 -0
- package/dist/infra/providers/core/errors.js +353 -0
- package/dist/infra/providers/core/fork.d.ts +106 -0
- package/dist/infra/providers/core/fork.js +418 -0
- package/dist/infra/providers/core/index.d.ts +10 -0
- package/dist/infra/providers/core/index.js +76 -0
- package/dist/infra/providers/core/logger.d.ts +186 -0
- package/dist/infra/providers/core/logger.js +191 -0
- package/dist/infra/providers/core/retry.d.ts +62 -0
- package/dist/infra/providers/core/retry.js +189 -0
- package/dist/infra/providers/core/usage.d.ts +151 -0
- package/dist/infra/providers/core/usage.js +376 -0
- package/dist/infra/providers/gemini.d.ts +49 -0
- package/dist/infra/providers/gemini.js +493 -0
- package/dist/infra/providers/index.d.ts +25 -0
- package/dist/infra/providers/index.js +83 -0
- package/dist/infra/providers/openai.d.ts +123 -0
- package/dist/infra/providers/openai.js +662 -0
- package/dist/infra/providers/types.d.ts +334 -0
- package/dist/infra/providers/types.js +20 -0
- package/dist/infra/providers/utils.d.ts +53 -0
- package/dist/infra/providers/utils.js +400 -0
- package/dist/infra/sandbox-factory.d.ts +13 -0
- package/dist/infra/sandbox-factory.js +30 -0
- package/dist/infra/sandbox.d.ts +35 -6
- package/dist/infra/sandbox.js +174 -8
- package/dist/infra/store/factory.d.ts +45 -0
- package/dist/infra/store/factory.js +80 -0
- package/dist/infra/store/index.d.ts +3 -0
- package/dist/infra/store/index.js +26 -0
- package/dist/infra/store/json-store.d.ts +67 -0
- package/dist/infra/store/json-store.js +606 -0
- package/dist/infra/store/types.d.ts +342 -0
- package/dist/infra/store/types.js +2 -0
- package/dist/infra/store.d.ts +12 -32
- package/dist/infra/store.js +27 -130
- package/dist/tools/bash_kill/index.d.ts +1 -0
- package/dist/tools/bash_kill/index.js +35 -0
- package/dist/tools/bash_kill/prompt.d.ts +2 -0
- package/dist/tools/bash_kill/prompt.js +14 -0
- package/dist/tools/bash_logs/index.d.ts +1 -0
- package/dist/tools/bash_logs/index.js +40 -0
- package/dist/tools/bash_logs/prompt.d.ts +2 -0
- package/dist/tools/bash_logs/prompt.js +14 -0
- package/dist/tools/bash_run/index.d.ts +16 -0
- package/dist/tools/bash_run/index.js +61 -0
- package/dist/tools/bash_run/prompt.d.ts +2 -0
- package/dist/tools/bash_run/prompt.js +18 -0
- package/dist/tools/builtin.d.ts +7 -13
- package/dist/tools/builtin.js +19 -90
- package/dist/tools/define.d.ts +101 -0
- package/dist/tools/define.js +214 -0
- package/dist/tools/fs_edit/index.d.ts +1 -0
- package/dist/tools/fs_edit/index.js +62 -0
- package/dist/tools/fs_edit/prompt.d.ts +2 -0
- package/dist/tools/fs_edit/prompt.js +15 -0
- package/dist/tools/fs_glob/index.d.ts +1 -0
- package/dist/tools/fs_glob/index.js +40 -0
- package/dist/tools/fs_glob/prompt.d.ts +2 -0
- package/dist/tools/fs_glob/prompt.js +15 -0
- package/dist/tools/fs_grep/index.d.ts +1 -0
- package/dist/tools/fs_grep/index.js +66 -0
- package/dist/tools/fs_grep/prompt.d.ts +2 -0
- package/dist/tools/fs_grep/prompt.js +16 -0
- package/dist/tools/fs_multi_edit/index.d.ts +1 -0
- package/dist/tools/fs_multi_edit/index.js +106 -0
- package/dist/tools/fs_multi_edit/prompt.d.ts +2 -0
- package/dist/tools/fs_multi_edit/prompt.js +16 -0
- package/dist/tools/fs_read/index.d.ts +1 -0
- package/dist/tools/fs_read/index.js +40 -0
- package/dist/tools/fs_read/prompt.d.ts +2 -0
- package/dist/tools/fs_read/prompt.js +16 -0
- package/dist/tools/fs_write/index.d.ts +1 -0
- package/dist/tools/fs_write/index.js +40 -0
- package/dist/tools/fs_write/prompt.d.ts +2 -0
- package/dist/tools/fs_write/prompt.js +15 -0
- package/dist/tools/index.d.ts +11 -0
- package/dist/tools/index.js +61 -0
- package/dist/tools/mcp.d.ts +69 -0
- package/dist/tools/mcp.js +185 -0
- package/dist/tools/registry.d.ts +29 -0
- package/dist/tools/registry.js +26 -0
- package/dist/tools/scripts.d.ts +22 -0
- package/dist/tools/scripts.js +205 -0
- package/dist/tools/skills.d.ts +20 -0
- package/dist/tools/skills.js +115 -0
- package/dist/tools/task_run/index.d.ts +7 -0
- package/dist/tools/task_run/index.js +58 -0
- package/dist/tools/task_run/prompt.d.ts +5 -0
- package/dist/tools/task_run/prompt.js +25 -0
- package/dist/tools/todo_read/index.d.ts +1 -0
- package/dist/tools/todo_read/index.js +29 -0
- package/dist/tools/todo_read/prompt.d.ts +2 -0
- package/dist/tools/todo_read/prompt.js +18 -0
- package/dist/tools/todo_write/index.d.ts +1 -0
- package/dist/tools/todo_write/index.js +42 -0
- package/dist/tools/todo_write/prompt.d.ts +2 -0
- package/dist/tools/todo_write/prompt.js +23 -0
- package/dist/tools/tool.d.ts +43 -0
- package/dist/tools/tool.js +211 -0
- package/dist/tools/toolkit.d.ts +69 -0
- package/dist/tools/toolkit.js +98 -0
- package/dist/tools/type-inference.d.ts +127 -0
- package/dist/tools/type-inference.js +207 -0
- package/dist/utils/agent-id.d.ts +1 -0
- package/dist/utils/agent-id.js +28 -0
- package/dist/utils/logger.d.ts +15 -0
- package/dist/utils/logger.js +44 -0
- package/dist/utils/session-id.js +16 -16
- package/package.json +35 -11
- package/dist/tools/bash.d.ts +0 -63
- package/dist/tools/bash.js +0 -92
- package/dist/tools/fs.d.ts +0 -96
- package/dist/tools/fs.js +0 -100
- package/dist/tools/task.d.ts +0 -38
- package/dist/tools/task.js +0 -45
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getMCPTools = getMCPTools;
|
|
4
|
+
exports.disconnectMCP = disconnectMCP;
|
|
5
|
+
exports.disconnectAllMCP = disconnectAllMCP;
|
|
6
|
+
const index_js_1 = require("@modelcontextprotocol/sdk/client/index.js");
|
|
7
|
+
const stdio_js_1 = require("@modelcontextprotocol/sdk/client/stdio.js");
|
|
8
|
+
const sse_js_1 = require("@modelcontextprotocol/sdk/client/sse.js");
|
|
9
|
+
const registry_1 = require("./registry");
|
|
10
|
+
/**
|
|
11
|
+
* MCP Client 管理器
|
|
12
|
+
*
|
|
13
|
+
* 维护 MCP 客户端连接,支持多种传输方式
|
|
14
|
+
*/
|
|
15
|
+
class MCPClientManager {
|
|
16
|
+
constructor() {
|
|
17
|
+
this.clients = new Map();
|
|
18
|
+
this.transports = new Map();
|
|
19
|
+
}
|
|
20
|
+
async connect(serverName, config) {
|
|
21
|
+
// 如果已连接,返回现有客户端
|
|
22
|
+
if (this.clients.has(serverName)) {
|
|
23
|
+
return this.clients.get(serverName);
|
|
24
|
+
}
|
|
25
|
+
// 创建 transport
|
|
26
|
+
let transport;
|
|
27
|
+
if (config.transport === 'stdio') {
|
|
28
|
+
if (!config.command) {
|
|
29
|
+
throw new Error('command is required for stdio transport');
|
|
30
|
+
}
|
|
31
|
+
transport = new stdio_js_1.StdioClientTransport({
|
|
32
|
+
command: config.command,
|
|
33
|
+
args: config.args || [],
|
|
34
|
+
env: config.env,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
else if (config.transport === 'sse' || config.transport === 'http') {
|
|
38
|
+
if (!config.url) {
|
|
39
|
+
throw new Error('url is required for sse/http transport');
|
|
40
|
+
}
|
|
41
|
+
transport = new sse_js_1.SSEClientTransport(new URL(config.url));
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
throw new Error(`Unsupported transport type: ${config.transport}`);
|
|
45
|
+
}
|
|
46
|
+
// 创建客户端
|
|
47
|
+
const client = new index_js_1.Client({
|
|
48
|
+
name: 'kode-sdk',
|
|
49
|
+
version: '2.0.0',
|
|
50
|
+
}, {
|
|
51
|
+
capabilities: {},
|
|
52
|
+
});
|
|
53
|
+
// 连接
|
|
54
|
+
await client.connect(transport);
|
|
55
|
+
// 缓存
|
|
56
|
+
this.clients.set(serverName, client);
|
|
57
|
+
this.transports.set(serverName, transport);
|
|
58
|
+
return client;
|
|
59
|
+
}
|
|
60
|
+
async disconnect(serverName) {
|
|
61
|
+
const client = this.clients.get(serverName);
|
|
62
|
+
if (client) {
|
|
63
|
+
await client.close();
|
|
64
|
+
this.clients.delete(serverName);
|
|
65
|
+
}
|
|
66
|
+
const transport = this.transports.get(serverName);
|
|
67
|
+
if (transport) {
|
|
68
|
+
await transport.close();
|
|
69
|
+
this.transports.delete(serverName);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
async disconnectAll() {
|
|
73
|
+
const servers = Array.from(this.clients.keys());
|
|
74
|
+
await Promise.all(servers.map((name) => this.disconnect(name)));
|
|
75
|
+
}
|
|
76
|
+
getClient(serverName) {
|
|
77
|
+
return this.clients.get(serverName);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
const mcpManager = new MCPClientManager();
|
|
81
|
+
/**
|
|
82
|
+
* 获取 MCP 工具
|
|
83
|
+
*
|
|
84
|
+
* 连接到 MCP 服务器并将其工具转换为 ToolInstance[]
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```ts
|
|
88
|
+
* // Stdio transport
|
|
89
|
+
* const tools = await getMCPTools({
|
|
90
|
+
* transport: 'stdio',
|
|
91
|
+
* command: 'uvx',
|
|
92
|
+
* args: ['mcp-server-git'],
|
|
93
|
+
* serverName: 'git'
|
|
94
|
+
* });
|
|
95
|
+
*
|
|
96
|
+
* // HTTP/SSE transport
|
|
97
|
+
* const tools = await getMCPTools({
|
|
98
|
+
* transport: 'sse',
|
|
99
|
+
* url: 'http://localhost:3000/mcp',
|
|
100
|
+
* serverName: 'company',
|
|
101
|
+
* include: ['search', 'summarize']
|
|
102
|
+
* });
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
async function getMCPTools(config) {
|
|
106
|
+
const serverName = config.serverName || 'default';
|
|
107
|
+
// 连接到 MCP 服务器
|
|
108
|
+
const client = await mcpManager.connect(serverName, config);
|
|
109
|
+
// 列出可用工具
|
|
110
|
+
const toolsResponse = await client.listTools();
|
|
111
|
+
const mcpTools = toolsResponse.tools;
|
|
112
|
+
// 过滤工具
|
|
113
|
+
let filtered = mcpTools;
|
|
114
|
+
if (config.include) {
|
|
115
|
+
filtered = filtered.filter((tool) => config.include.includes(tool.name));
|
|
116
|
+
}
|
|
117
|
+
if (config.exclude) {
|
|
118
|
+
filtered = filtered.filter((tool) => !config.exclude.includes(tool.name));
|
|
119
|
+
}
|
|
120
|
+
// 转换为 ToolInstance[]
|
|
121
|
+
const toolInstances = filtered.map((mcpTool) => {
|
|
122
|
+
// 生成命名空间化的工具名:mcp__serverName__toolName
|
|
123
|
+
const toolName = `mcp__${serverName}__${mcpTool.name}`;
|
|
124
|
+
const toolInstance = {
|
|
125
|
+
name: toolName,
|
|
126
|
+
description: mcpTool.description || `MCP tool: ${mcpTool.name}`,
|
|
127
|
+
input_schema: mcpTool.inputSchema,
|
|
128
|
+
async exec(args, _ctx) {
|
|
129
|
+
try {
|
|
130
|
+
// 调用 MCP 工具
|
|
131
|
+
const result = await client.callTool({
|
|
132
|
+
name: mcpTool.name,
|
|
133
|
+
arguments: args,
|
|
134
|
+
});
|
|
135
|
+
// 返回结果
|
|
136
|
+
return {
|
|
137
|
+
content: result.content,
|
|
138
|
+
isError: result.isError,
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
catch (error) {
|
|
142
|
+
throw new Error(`MCP tool execution failed: ${error}`);
|
|
143
|
+
}
|
|
144
|
+
},
|
|
145
|
+
toDescriptor() {
|
|
146
|
+
return {
|
|
147
|
+
source: 'mcp',
|
|
148
|
+
name: toolName,
|
|
149
|
+
registryId: toolName,
|
|
150
|
+
metadata: {
|
|
151
|
+
mcpServer: serverName,
|
|
152
|
+
mcpToolName: mcpTool.name,
|
|
153
|
+
transport: config.transport,
|
|
154
|
+
},
|
|
155
|
+
config: {
|
|
156
|
+
serverName,
|
|
157
|
+
transport: config.transport,
|
|
158
|
+
url: config.url,
|
|
159
|
+
command: config.command,
|
|
160
|
+
args: config.args,
|
|
161
|
+
},
|
|
162
|
+
};
|
|
163
|
+
},
|
|
164
|
+
};
|
|
165
|
+
// 自动注册到 global registry(支持 Resume)
|
|
166
|
+
registry_1.globalToolRegistry.register(toolName, (_registryConfig) => {
|
|
167
|
+
// Resume 时重建 MCP 连接
|
|
168
|
+
return toolInstance;
|
|
169
|
+
});
|
|
170
|
+
return toolInstance;
|
|
171
|
+
});
|
|
172
|
+
return toolInstances;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* 断开 MCP 服务器连接
|
|
176
|
+
*/
|
|
177
|
+
async function disconnectMCP(serverName) {
|
|
178
|
+
await mcpManager.disconnect(serverName);
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* 断开所有 MCP 服务器连接
|
|
182
|
+
*/
|
|
183
|
+
async function disconnectAllMCP() {
|
|
184
|
+
await mcpManager.disconnectAll();
|
|
185
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Hooks } from '../core/hooks';
|
|
2
|
+
import { ToolContext } from '../core/types';
|
|
3
|
+
export type ToolSource = 'builtin' | 'registered' | 'mcp';
|
|
4
|
+
export interface ToolDescriptor {
|
|
5
|
+
source: ToolSource;
|
|
6
|
+
name: string;
|
|
7
|
+
registryId?: string;
|
|
8
|
+
config?: Record<string, any>;
|
|
9
|
+
metadata?: Record<string, any>;
|
|
10
|
+
}
|
|
11
|
+
export interface ToolInstance {
|
|
12
|
+
name: string;
|
|
13
|
+
description: string;
|
|
14
|
+
input_schema: any;
|
|
15
|
+
hooks?: Hooks;
|
|
16
|
+
permissionDetails?: (call: any, ctx: ToolContext) => any;
|
|
17
|
+
exec(args: any, ctx: ToolContext): Promise<any>;
|
|
18
|
+
prompt?: string | ((ctx: ToolContext) => string | Promise<string>);
|
|
19
|
+
toDescriptor(): ToolDescriptor;
|
|
20
|
+
}
|
|
21
|
+
export type ToolFactory = (config?: Record<string, any>) => ToolInstance;
|
|
22
|
+
export declare class ToolRegistry {
|
|
23
|
+
private factories;
|
|
24
|
+
register(id: string, factory: ToolFactory): void;
|
|
25
|
+
has(id: string): boolean;
|
|
26
|
+
create(id: string, config?: Record<string, any>): ToolInstance;
|
|
27
|
+
list(): string[];
|
|
28
|
+
}
|
|
29
|
+
export declare const globalToolRegistry: ToolRegistry;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.globalToolRegistry = exports.ToolRegistry = void 0;
|
|
4
|
+
class ToolRegistry {
|
|
5
|
+
constructor() {
|
|
6
|
+
this.factories = new Map();
|
|
7
|
+
}
|
|
8
|
+
register(id, factory) {
|
|
9
|
+
this.factories.set(id, factory);
|
|
10
|
+
}
|
|
11
|
+
has(id) {
|
|
12
|
+
return this.factories.has(id);
|
|
13
|
+
}
|
|
14
|
+
create(id, config) {
|
|
15
|
+
const factory = this.factories.get(id);
|
|
16
|
+
if (!factory) {
|
|
17
|
+
throw new Error(`Tool not registered: ${id}`);
|
|
18
|
+
}
|
|
19
|
+
return factory(config);
|
|
20
|
+
}
|
|
21
|
+
list() {
|
|
22
|
+
return Array.from(this.factories.keys());
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
exports.ToolRegistry = ToolRegistry;
|
|
26
|
+
exports.globalToolRegistry = new ToolRegistry();
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Scripts 执行工具
|
|
3
|
+
*
|
|
4
|
+
* 设计原则 (UNIX哲学):
|
|
5
|
+
* - 简洁: 只负责执行scripts,支持双模式(直接执行/sandbox隔离)
|
|
6
|
+
* - 安全: 支持sandbox隔离执行,拦截危险命令
|
|
7
|
+
* - 跨平台: 自动适配Windows、Linux、MacOS
|
|
8
|
+
*/
|
|
9
|
+
import type { SkillsManager } from '../core/skills/manager';
|
|
10
|
+
import type { SandboxFactory } from '../infra/sandbox-factory';
|
|
11
|
+
/**
|
|
12
|
+
* 创建Scripts工具
|
|
13
|
+
*
|
|
14
|
+
* @param skillsManager Skills管理器实例
|
|
15
|
+
* @param sandboxFactory Sandbox工厂(可选)
|
|
16
|
+
* @returns ToolInstance
|
|
17
|
+
*/
|
|
18
|
+
export declare function createScriptsTool(skillsManager: SkillsManager, sandboxFactory?: SandboxFactory): import("./registry").ToolInstance;
|
|
19
|
+
/**
|
|
20
|
+
* 导出工具创建函数
|
|
21
|
+
*/
|
|
22
|
+
export default createScriptsTool;
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Scripts 执行工具
|
|
4
|
+
*
|
|
5
|
+
* 设计原则 (UNIX哲学):
|
|
6
|
+
* - 简洁: 只负责执行scripts,支持双模式(直接执行/sandbox隔离)
|
|
7
|
+
* - 安全: 支持sandbox隔离执行,拦截危险命令
|
|
8
|
+
* - 跨平台: 自动适配Windows、Linux、MacOS
|
|
9
|
+
*/
|
|
10
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
11
|
+
if (k2 === undefined) k2 = k;
|
|
12
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
13
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
14
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
15
|
+
}
|
|
16
|
+
Object.defineProperty(o, k2, desc);
|
|
17
|
+
}) : (function(o, m, k, k2) {
|
|
18
|
+
if (k2 === undefined) k2 = k;
|
|
19
|
+
o[k2] = m[k];
|
|
20
|
+
}));
|
|
21
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
22
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
23
|
+
}) : function(o, v) {
|
|
24
|
+
o["default"] = v;
|
|
25
|
+
});
|
|
26
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
27
|
+
var ownKeys = function(o) {
|
|
28
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
29
|
+
var ar = [];
|
|
30
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
31
|
+
return ar;
|
|
32
|
+
};
|
|
33
|
+
return ownKeys(o);
|
|
34
|
+
};
|
|
35
|
+
return function (mod) {
|
|
36
|
+
if (mod && mod.__esModule) return mod;
|
|
37
|
+
var result = {};
|
|
38
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
39
|
+
__setModuleDefault(result, mod);
|
|
40
|
+
return result;
|
|
41
|
+
};
|
|
42
|
+
})();
|
|
43
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
44
|
+
exports.createScriptsTool = createScriptsTool;
|
|
45
|
+
const tool_1 = require("../tools/tool");
|
|
46
|
+
const zod_1 = require("zod");
|
|
47
|
+
const child_process_1 = require("child_process");
|
|
48
|
+
const path = __importStar(require("path"));
|
|
49
|
+
const os = __importStar(require("os"));
|
|
50
|
+
/**
|
|
51
|
+
* Scripts 工具描述
|
|
52
|
+
*/
|
|
53
|
+
const DESCRIPTION = `执行skill中的scripts脚本。
|
|
54
|
+
|
|
55
|
+
支持两种执行模式:
|
|
56
|
+
- 直接执行模式(默认): 本地开发时使用,直接在当前环境执行
|
|
57
|
+
- Sandbox隔离模式: 生产环境使用,在安全隔离环境中执行
|
|
58
|
+
|
|
59
|
+
支持的脚本类型:
|
|
60
|
+
- Node.js脚本 (.js, .ts): 跨平台兼容
|
|
61
|
+
- Shell脚本 (.sh): Linux/MacOS
|
|
62
|
+
- Batch脚本 (.bat): Windows
|
|
63
|
+
|
|
64
|
+
注意: 危险命令会被自动拦截(如rm -rf /、sudo等)`;
|
|
65
|
+
/**
|
|
66
|
+
* 检测平台
|
|
67
|
+
*/
|
|
68
|
+
function detectPlatform() {
|
|
69
|
+
const platform = os.platform();
|
|
70
|
+
if (platform === 'win32')
|
|
71
|
+
return 'windows';
|
|
72
|
+
if (platform === 'darwin')
|
|
73
|
+
return 'macos';
|
|
74
|
+
return 'linux';
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* 获取脚本执行命令
|
|
78
|
+
*/
|
|
79
|
+
function getScriptCommand(scriptPath, platform) {
|
|
80
|
+
const ext = path.extname(scriptPath).toLowerCase();
|
|
81
|
+
switch (ext) {
|
|
82
|
+
case '.js':
|
|
83
|
+
return `node "${scriptPath}"`;
|
|
84
|
+
case '.ts':
|
|
85
|
+
return `ts-node "${scriptPath}"`;
|
|
86
|
+
case '.sh':
|
|
87
|
+
if (platform === 'windows') {
|
|
88
|
+
// Windows下需要Git Bash或WSL来执行.sh
|
|
89
|
+
return `bash "${scriptPath}"`;
|
|
90
|
+
}
|
|
91
|
+
return `sh "${scriptPath}"`;
|
|
92
|
+
case '.bat':
|
|
93
|
+
case '.cmd':
|
|
94
|
+
if (platform !== 'windows') {
|
|
95
|
+
throw new Error(`Batch scripts (.bat/.cmd) are only supported on Windows`);
|
|
96
|
+
}
|
|
97
|
+
return `"${scriptPath}"`;
|
|
98
|
+
default:
|
|
99
|
+
throw new Error(`Unsupported script type: ${ext}`);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* 创建Scripts工具
|
|
104
|
+
*
|
|
105
|
+
* @param skillsManager Skills管理器实例
|
|
106
|
+
* @param sandboxFactory Sandbox工厂(可选)
|
|
107
|
+
* @returns ToolInstance
|
|
108
|
+
*/
|
|
109
|
+
function createScriptsTool(skillsManager, sandboxFactory) {
|
|
110
|
+
const scriptsTool = (0, tool_1.tool)({
|
|
111
|
+
name: 'execute_script',
|
|
112
|
+
description: DESCRIPTION,
|
|
113
|
+
parameters: zod_1.z.object({
|
|
114
|
+
skill_name: zod_1.z.string().describe('技能名称'),
|
|
115
|
+
script_name: zod_1.z.string().describe('脚本文件名(如"script.js")'),
|
|
116
|
+
use_sandbox: zod_1.z.boolean().optional().default(true).describe('是否使用sandbox隔离执行'),
|
|
117
|
+
args: zod_1.z.array(zod_1.z.string()).optional().describe('脚本参数(可选)'),
|
|
118
|
+
}),
|
|
119
|
+
async execute(args, ctx) {
|
|
120
|
+
const { skill_name, script_name, use_sandbox, args: scriptArgs = [] } = args;
|
|
121
|
+
// 加载skill内容
|
|
122
|
+
const skillContent = await skillsManager.loadSkillContent(skill_name);
|
|
123
|
+
if (!skillContent) {
|
|
124
|
+
return {
|
|
125
|
+
ok: false,
|
|
126
|
+
error: `Skill '${skill_name}' not found`,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
// 查找脚本文件
|
|
130
|
+
const scriptPath = skillContent.scripts.find(p => path.basename(p) === script_name);
|
|
131
|
+
if (!scriptPath) {
|
|
132
|
+
return {
|
|
133
|
+
ok: false,
|
|
134
|
+
error: `Script '${script_name}' not found in skill '${skill_name}'. Available scripts: ${skillContent.scripts.map(p => path.basename(p)).join(', ')}`,
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
const platform = detectPlatform();
|
|
138
|
+
let result;
|
|
139
|
+
try {
|
|
140
|
+
if (use_sandbox && sandboxFactory) {
|
|
141
|
+
// 使用sandbox隔离执行
|
|
142
|
+
const sandbox = sandboxFactory.create({
|
|
143
|
+
kind: 'local',
|
|
144
|
+
workDir: skillContent.metadata.baseDir,
|
|
145
|
+
});
|
|
146
|
+
const cmd = getScriptCommand(scriptPath, platform);
|
|
147
|
+
const cmdWithArgs = `${cmd} ${scriptArgs.join(' ')}`;
|
|
148
|
+
result = await sandbox.exec(cmdWithArgs, { timeoutMs: 60000 });
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
// 直接执行(本地开发模式)
|
|
152
|
+
const cmd = getScriptCommand(scriptPath, platform);
|
|
153
|
+
const cmdWithArgs = `${cmd} ${scriptArgs.join(' ')}`;
|
|
154
|
+
const stdout = (0, child_process_1.execSync)(cmdWithArgs, {
|
|
155
|
+
cwd: skillContent.metadata.baseDir,
|
|
156
|
+
encoding: 'utf-8',
|
|
157
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
158
|
+
timeout: 60000,
|
|
159
|
+
});
|
|
160
|
+
result = {
|
|
161
|
+
code: 0,
|
|
162
|
+
stdout: stdout || '',
|
|
163
|
+
stderr: '',
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
if (result.code !== 0) {
|
|
167
|
+
return {
|
|
168
|
+
ok: false,
|
|
169
|
+
error: `Script execution failed with code ${result.code}`,
|
|
170
|
+
data: {
|
|
171
|
+
stdout: result.stdout,
|
|
172
|
+
stderr: result.stderr,
|
|
173
|
+
},
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
return {
|
|
177
|
+
ok: true,
|
|
178
|
+
data: {
|
|
179
|
+
stdout: result.stdout,
|
|
180
|
+
stderr: result.stderr,
|
|
181
|
+
},
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
catch (error) {
|
|
185
|
+
return {
|
|
186
|
+
ok: false,
|
|
187
|
+
error: error.message || 'Script execution failed',
|
|
188
|
+
data: {
|
|
189
|
+
stdout: error.stdout || '',
|
|
190
|
+
stderr: error.stderr || '',
|
|
191
|
+
},
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
},
|
|
195
|
+
metadata: {
|
|
196
|
+
readonly: false,
|
|
197
|
+
version: '1.0',
|
|
198
|
+
},
|
|
199
|
+
});
|
|
200
|
+
return scriptsTool;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* 导出工具创建函数
|
|
204
|
+
*/
|
|
205
|
+
exports.default = createScriptsTool;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Skills 工具
|
|
3
|
+
*
|
|
4
|
+
* 设计原则 (UNIX哲学):
|
|
5
|
+
* - 简洁: 只负责列出和加载skills,不处理业务逻辑
|
|
6
|
+
* - 模块化: 复用SkillsManager进行实际操作
|
|
7
|
+
* - 组合: 与SDK工具系统无缝集成
|
|
8
|
+
*/
|
|
9
|
+
import type { SkillsManager } from '../core/skills/manager';
|
|
10
|
+
/**
|
|
11
|
+
* 创建Skills工具
|
|
12
|
+
*
|
|
13
|
+
* @param skillsManager Skills管理器实例
|
|
14
|
+
* @returns ToolInstance
|
|
15
|
+
*/
|
|
16
|
+
export declare function createSkillsTool(skillsManager: SkillsManager): import("./registry").ToolInstance;
|
|
17
|
+
/**
|
|
18
|
+
* 导出工具创建函数
|
|
19
|
+
*/
|
|
20
|
+
export default createSkillsTool;
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Skills 工具
|
|
4
|
+
*
|
|
5
|
+
* 设计原则 (UNIX哲学):
|
|
6
|
+
* - 简洁: 只负责列出和加载skills,不处理业务逻辑
|
|
7
|
+
* - 模块化: 复用SkillsManager进行实际操作
|
|
8
|
+
* - 组合: 与SDK工具系统无缝集成
|
|
9
|
+
*/
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
exports.createSkillsTool = createSkillsTool;
|
|
12
|
+
const tool_1 = require("../tools/tool");
|
|
13
|
+
const zod_1 = require("zod");
|
|
14
|
+
/**
|
|
15
|
+
* Skills 工具描述
|
|
16
|
+
*
|
|
17
|
+
* 原始描述(已备份,list操作已临时禁用):
|
|
18
|
+
* const ORIGINAL_DESCRIPTION = `管理和加载skills。
|
|
19
|
+
*
|
|
20
|
+
* 使用此工具来:
|
|
21
|
+
* - 列出所有可用的skills(获取元数据列表)
|
|
22
|
+
* - 加载特定skill的详细内容(包含指令、references、scripts、assets)
|
|
23
|
+
*
|
|
24
|
+
* Skills是可重用的能力单元,可以扩展Agent的功能。`;
|
|
25
|
+
*/
|
|
26
|
+
const DESCRIPTION = `加载特定skill的详细内容。
|
|
27
|
+
|
|
28
|
+
使用此工具来:
|
|
29
|
+
- 加载特定skill的详细内容(包含指令、references、scripts、assets)
|
|
30
|
+
- 需要提供skill_name参数来指定要加载的技能
|
|
31
|
+
|
|
32
|
+
Skills是可重用的能力单元,可以扩展Agent的功能。`;
|
|
33
|
+
/**
|
|
34
|
+
* 创建Skills工具
|
|
35
|
+
*
|
|
36
|
+
* @param skillsManager Skills管理器实例
|
|
37
|
+
* @returns ToolInstance
|
|
38
|
+
*/
|
|
39
|
+
function createSkillsTool(skillsManager) {
|
|
40
|
+
// 临时禁用 list 操作,只保留 load 操作
|
|
41
|
+
// const actionSchema = z.enum(['list', 'load']).describe('操作类型');
|
|
42
|
+
const actionSchema = zod_1.z.enum(['load']).describe('操作类型');
|
|
43
|
+
const skillsTool = (0, tool_1.tool)({
|
|
44
|
+
name: 'skills',
|
|
45
|
+
description: DESCRIPTION,
|
|
46
|
+
parameters: zod_1.z.object({
|
|
47
|
+
action: actionSchema,
|
|
48
|
+
skill_name: zod_1.z.string().optional().describe('技能名称(当action=load时必需)'),
|
|
49
|
+
}),
|
|
50
|
+
async execute(args, ctx) {
|
|
51
|
+
const { action, skill_name } = args;
|
|
52
|
+
// 注释掉 list 操作的代码
|
|
53
|
+
// if (action === 'list') {
|
|
54
|
+
// // 列出所有skills
|
|
55
|
+
// const skills = await skillsManager.getSkillsMetadata();
|
|
56
|
+
//
|
|
57
|
+
// const skillsList = skills.map(s => ({
|
|
58
|
+
// name: s.name,
|
|
59
|
+
// description: s.description,
|
|
60
|
+
// }));
|
|
61
|
+
//
|
|
62
|
+
// return {
|
|
63
|
+
// ok: true,
|
|
64
|
+
// data: {
|
|
65
|
+
// count: skillsList.length,
|
|
66
|
+
// skills: skillsList,
|
|
67
|
+
// },
|
|
68
|
+
// };
|
|
69
|
+
// } else if (action === 'load') {
|
|
70
|
+
if (action === 'load') {
|
|
71
|
+
// 加载特定skill内容
|
|
72
|
+
if (!skill_name) {
|
|
73
|
+
return {
|
|
74
|
+
ok: false,
|
|
75
|
+
error: 'skill_name is required when action=load',
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
const content = await skillsManager.loadSkillContent(skill_name);
|
|
79
|
+
if (!content) {
|
|
80
|
+
return {
|
|
81
|
+
ok: false,
|
|
82
|
+
error: `Skill '${skill_name}' not found`,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
return {
|
|
86
|
+
ok: true,
|
|
87
|
+
data: {
|
|
88
|
+
name: content.metadata.name,
|
|
89
|
+
description: content.metadata.description,
|
|
90
|
+
content: content.content,
|
|
91
|
+
base_dir: content.metadata.baseDir,
|
|
92
|
+
references: content.references,
|
|
93
|
+
scripts: content.scripts,
|
|
94
|
+
assets: content.assets,
|
|
95
|
+
},
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
return {
|
|
100
|
+
ok: false,
|
|
101
|
+
error: `Unknown action: ${action}`,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
metadata: {
|
|
106
|
+
readonly: true,
|
|
107
|
+
version: '1.0',
|
|
108
|
+
},
|
|
109
|
+
});
|
|
110
|
+
return skillsTool;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* 导出工具创建函数
|
|
114
|
+
*/
|
|
115
|
+
exports.default = createSkillsTool;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createTaskRunTool = createTaskRunTool;
|
|
4
|
+
const tool_1 = require("../tool");
|
|
5
|
+
const zod_1 = require("zod");
|
|
6
|
+
const prompt_1 = require("./prompt");
|
|
7
|
+
function createTaskRunTool(templates) {
|
|
8
|
+
if (!templates || templates.length === 0) {
|
|
9
|
+
throw new Error('Cannot create task_run tool: no agent templates provided');
|
|
10
|
+
}
|
|
11
|
+
const TaskRun = (0, tool_1.tool)({
|
|
12
|
+
name: 'task_run',
|
|
13
|
+
description: prompt_1.DESCRIPTION,
|
|
14
|
+
parameters: zod_1.z.object({
|
|
15
|
+
description: zod_1.z.string().describe('Short description of the task (3-5 words)'),
|
|
16
|
+
prompt: zod_1.z.string().describe('Detailed instructions for the sub-agent'),
|
|
17
|
+
agentTemplateId: zod_1.z.string().describe('Agent template ID to use for this task'),
|
|
18
|
+
context: zod_1.z.string().optional().describe('Additional context to append'),
|
|
19
|
+
}),
|
|
20
|
+
async execute(args, ctx) {
|
|
21
|
+
const { description, prompt, agentTemplateId, context } = args;
|
|
22
|
+
const template = templates.find((tpl) => tpl.id === agentTemplateId);
|
|
23
|
+
if (!template) {
|
|
24
|
+
const availableTemplates = templates
|
|
25
|
+
.map((tpl) => ` - ${tpl.id}: ${tpl.whenToUse || 'General purpose agent'}`)
|
|
26
|
+
.join('\n');
|
|
27
|
+
throw new Error(`Agent template '${agentTemplateId}' not found.\n\nAvailable templates:\n${availableTemplates}\n\nPlease choose one of the available template IDs.`);
|
|
28
|
+
}
|
|
29
|
+
const detailedPrompt = [
|
|
30
|
+
`# Task: ${description}`,
|
|
31
|
+
prompt,
|
|
32
|
+
context ? `\n# Additional Context\n${context}` : undefined,
|
|
33
|
+
]
|
|
34
|
+
.filter(Boolean)
|
|
35
|
+
.join('\n\n');
|
|
36
|
+
if (!ctx.agent?.delegateTask) {
|
|
37
|
+
throw new Error('Task delegation not supported by this agent version');
|
|
38
|
+
}
|
|
39
|
+
const result = await ctx.agent.delegateTask({
|
|
40
|
+
templateId: template.id,
|
|
41
|
+
prompt: detailedPrompt,
|
|
42
|
+
tools: template.tools,
|
|
43
|
+
});
|
|
44
|
+
return {
|
|
45
|
+
status: result.status,
|
|
46
|
+
template: template.id,
|
|
47
|
+
text: result.text,
|
|
48
|
+
permissionIds: result.permissionIds,
|
|
49
|
+
};
|
|
50
|
+
},
|
|
51
|
+
metadata: {
|
|
52
|
+
readonly: false,
|
|
53
|
+
version: '1.0',
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
TaskRun.prompt = (0, prompt_1.generatePrompt)(templates);
|
|
57
|
+
return TaskRun;
|
|
58
|
+
}
|