@shareai-lab/kode-sdk 2.7.1 → 2.7.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.
- package/dist/core/agent/breakpoint-manager.js +36 -0
- package/dist/core/agent/message-queue.js +57 -0
- package/dist/core/agent/permission-manager.js +32 -0
- package/dist/core/agent/todo-manager.js +91 -0
- package/dist/core/agent/tool-runner.js +45 -0
- package/dist/core/agent.js +2035 -0
- package/dist/core/config.js +2 -0
- package/dist/core/context-manager.js +241 -0
- package/dist/core/errors.js +49 -0
- package/dist/core/events.js +329 -0
- package/dist/core/file-pool.d.ts +2 -0
- package/dist/core/file-pool.js +125 -0
- package/dist/core/hooks.js +71 -0
- package/dist/core/permission-modes.js +61 -0
- package/dist/core/pool.js +301 -0
- package/dist/core/room.js +57 -0
- package/dist/core/scheduler.js +58 -0
- package/dist/core/skills/index.js +20 -0
- package/dist/core/skills/management-manager.js +557 -0
- package/dist/core/skills/manager.js +243 -0
- package/dist/core/skills/operation-queue.js +113 -0
- package/dist/core/skills/sandbox-file-manager.js +183 -0
- package/dist/core/skills/types.js +9 -0
- package/dist/core/skills/xml-generator.js +70 -0
- package/dist/core/template.js +35 -0
- package/dist/core/time-bridge.js +100 -0
- package/dist/core/todo.js +89 -0
- package/dist/core/types.js +3 -0
- package/dist/index.js +148 -60461
- package/dist/infra/db/postgres/postgres-store.js +1073 -0
- package/dist/infra/db/sqlite/sqlite-store.js +800 -0
- package/dist/infra/e2b/e2b-fs.js +128 -0
- package/dist/infra/e2b/e2b-sandbox.js +156 -0
- package/dist/infra/e2b/e2b-template.js +105 -0
- package/dist/infra/e2b/index.js +9 -0
- package/dist/infra/e2b/types.js +2 -0
- package/dist/infra/provider.js +67 -0
- package/dist/infra/providers/anthropic.js +308 -0
- package/dist/infra/providers/core/errors.js +353 -0
- package/dist/infra/providers/core/fork.js +418 -0
- package/dist/infra/providers/core/index.js +76 -0
- package/dist/infra/providers/core/logger.js +191 -0
- package/dist/infra/providers/core/retry.js +189 -0
- package/dist/infra/providers/core/usage.js +376 -0
- package/dist/infra/providers/gemini.js +493 -0
- package/dist/infra/providers/index.js +83 -0
- package/dist/infra/providers/openai.js +662 -0
- package/dist/infra/providers/types.js +20 -0
- package/dist/infra/providers/utils.js +400 -0
- package/dist/infra/sandbox-factory.js +30 -0
- package/dist/infra/sandbox.js +243 -0
- package/dist/infra/store/factory.js +80 -0
- package/dist/infra/store/index.js +26 -0
- package/dist/infra/store/json-store.js +606 -0
- package/dist/infra/store/types.js +2 -0
- package/dist/infra/store.js +29 -0
- package/dist/tools/bash_kill/index.js +35 -0
- package/dist/tools/bash_kill/prompt.js +14 -0
- package/dist/tools/bash_logs/index.js +40 -0
- package/dist/tools/bash_logs/prompt.js +14 -0
- package/dist/tools/bash_run/index.js +61 -0
- package/dist/tools/bash_run/prompt.js +18 -0
- package/dist/tools/builtin.js +26 -0
- package/dist/tools/define.js +214 -0
- package/dist/tools/fs_edit/index.js +62 -0
- package/dist/tools/fs_edit/prompt.js +15 -0
- package/dist/tools/fs_glob/index.js +40 -0
- package/dist/tools/fs_glob/prompt.js +15 -0
- package/dist/tools/fs_grep/index.js +66 -0
- package/dist/tools/fs_grep/prompt.js +16 -0
- package/dist/tools/fs_multi_edit/index.js +106 -0
- package/dist/tools/fs_multi_edit/prompt.js +16 -0
- package/dist/tools/fs_read/index.js +40 -0
- package/dist/tools/fs_read/prompt.js +16 -0
- package/dist/tools/fs_write/index.js +40 -0
- package/dist/tools/fs_write/prompt.js +15 -0
- package/dist/tools/index.js +61 -0
- package/dist/tools/mcp.js +185 -0
- package/dist/tools/registry.js +26 -0
- package/dist/tools/scripts.js +205 -0
- package/dist/tools/skills.js +115 -0
- package/dist/tools/task_run/index.js +58 -0
- package/dist/tools/task_run/prompt.js +25 -0
- package/dist/tools/todo_read/index.js +29 -0
- package/dist/tools/todo_read/prompt.js +18 -0
- package/dist/tools/todo_write/index.js +42 -0
- package/dist/tools/todo_write/prompt.js +23 -0
- package/dist/tools/tool.js +211 -0
- package/dist/tools/toolkit.js +98 -0
- package/dist/tools/type-inference.js +207 -0
- package/dist/utils/agent-id.js +28 -0
- package/dist/utils/logger.js +44 -0
- package/dist/utils/session-id.js +64 -0
- package/package.json +7 -38
- package/dist/index.js.map +0 -7
- package/dist/index.mjs +0 -60385
- package/dist/index.mjs.map +0 -7
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.tool = tool;
|
|
4
|
+
exports.tools = tools;
|
|
5
|
+
const zod_1 = require("zod");
|
|
6
|
+
const registry_1 = require("./registry");
|
|
7
|
+
/**
|
|
8
|
+
* 实现
|
|
9
|
+
*/
|
|
10
|
+
function tool(nameOrDef, executeFn) {
|
|
11
|
+
// 解析参数
|
|
12
|
+
const def = typeof nameOrDef === 'string'
|
|
13
|
+
? {
|
|
14
|
+
name: nameOrDef,
|
|
15
|
+
description: `Execute ${nameOrDef}`,
|
|
16
|
+
parameters: zod_1.z.any(),
|
|
17
|
+
execute: executeFn,
|
|
18
|
+
}
|
|
19
|
+
: nameOrDef;
|
|
20
|
+
// 生成 JSON Schema (使用 Zod v4 原生方法)
|
|
21
|
+
let input_schema;
|
|
22
|
+
if (def.parameters) {
|
|
23
|
+
// Zod v4: 使用 zodToJsonSchema 的替代方案
|
|
24
|
+
// 由于 zod-to-json-schema 已被弃用,我们需要手动转换 Zod schema 为 JSON Schema
|
|
25
|
+
input_schema = zodToJsonSchemaManual(def.parameters);
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
input_schema = { type: 'object', properties: {} };
|
|
29
|
+
}
|
|
30
|
+
// 创建工具实例
|
|
31
|
+
const toolInstance = {
|
|
32
|
+
name: def.name,
|
|
33
|
+
description: def.description || `Execute ${def.name}`,
|
|
34
|
+
input_schema,
|
|
35
|
+
hooks: def.hooks,
|
|
36
|
+
async exec(args, ctx) {
|
|
37
|
+
try {
|
|
38
|
+
// 参数验证
|
|
39
|
+
if (def.parameters) {
|
|
40
|
+
const parseResult = def.parameters.safeParse(args);
|
|
41
|
+
if (!parseResult.success) {
|
|
42
|
+
return {
|
|
43
|
+
ok: false,
|
|
44
|
+
error: `Invalid parameters: ${parseResult.error.message}`,
|
|
45
|
+
_validationError: true,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
args = parseResult.data;
|
|
49
|
+
}
|
|
50
|
+
// 增强上下文
|
|
51
|
+
const enhancedCtx = {
|
|
52
|
+
...ctx,
|
|
53
|
+
emit(eventType, data) {
|
|
54
|
+
ctx.agent?.events?.emitMonitor({
|
|
55
|
+
type: 'tool_custom_event',
|
|
56
|
+
toolName: def.name,
|
|
57
|
+
eventType,
|
|
58
|
+
data,
|
|
59
|
+
timestamp: Date.now(),
|
|
60
|
+
});
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
// 执行工具
|
|
64
|
+
const result = await def.execute(args, enhancedCtx);
|
|
65
|
+
// 如果工具返回 {ok: false},保持原样
|
|
66
|
+
if (result && typeof result === 'object' && 'ok' in result && result.ok === false) {
|
|
67
|
+
return result;
|
|
68
|
+
}
|
|
69
|
+
// 正常结果
|
|
70
|
+
return result;
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
// 捕获工具执行中的所有错误,统一返回格式
|
|
74
|
+
return {
|
|
75
|
+
ok: false,
|
|
76
|
+
error: error?.message || String(error),
|
|
77
|
+
_thrownError: true,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
toDescriptor() {
|
|
82
|
+
return {
|
|
83
|
+
source: 'registered',
|
|
84
|
+
name: def.name,
|
|
85
|
+
registryId: def.name,
|
|
86
|
+
metadata: {
|
|
87
|
+
version: def.metadata?.version,
|
|
88
|
+
tags: def.metadata?.tags,
|
|
89
|
+
cacheable: def.metadata?.cacheable,
|
|
90
|
+
cacheTTL: def.metadata?.cacheTTL,
|
|
91
|
+
timeout: def.metadata?.timeout,
|
|
92
|
+
concurrent: def.metadata?.concurrent,
|
|
93
|
+
access: def.metadata?.readonly ? 'read' : 'write',
|
|
94
|
+
mutates: !def.metadata?.readonly,
|
|
95
|
+
},
|
|
96
|
+
};
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
// 自动注册到全局 registry
|
|
100
|
+
registry_1.globalToolRegistry.register(def.name, () => toolInstance);
|
|
101
|
+
return toolInstance;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* 批量定义工具
|
|
105
|
+
*/
|
|
106
|
+
function tools(definitions) {
|
|
107
|
+
return definitions.map((def) => tool(def));
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* 手动转换 Zod Schema 为 JSON Schema (替代已弃用的 zod-to-json-schema)
|
|
111
|
+
*
|
|
112
|
+
* 设计原则:
|
|
113
|
+
* - 简洁:只处理工具定义中常用的 Zod 类型
|
|
114
|
+
* - 健壮:对于不支持的类型,返回默认的 object schema
|
|
115
|
+
* - 可扩展:可以根据需要添加更多类型的支持
|
|
116
|
+
*/
|
|
117
|
+
function zodToJsonSchemaManual(zodType) {
|
|
118
|
+
// 处理 ZodEffects 类型(经过 .transform()、.refine() 等转换的 schema)
|
|
119
|
+
const typeName = zodType._def?.typeName;
|
|
120
|
+
if (typeName === 'ZodEffects' || (typeof typeName === 'string' && typeName.includes('ZodEffects'))) {
|
|
121
|
+
const innerSchema = zodType._def.schema;
|
|
122
|
+
if (innerSchema) {
|
|
123
|
+
return zodToJsonSchemaManual(innerSchema);
|
|
124
|
+
}
|
|
125
|
+
return { type: 'object', properties: {}, required: [] };
|
|
126
|
+
}
|
|
127
|
+
// 如果是 ZodObject
|
|
128
|
+
if (zodType instanceof zod_1.z.ZodObject) {
|
|
129
|
+
// Zod v4: shape 是属性而非方法
|
|
130
|
+
const shape = zodType.shape || zodType._def.shape;
|
|
131
|
+
const properties = {};
|
|
132
|
+
const required = [];
|
|
133
|
+
for (const [key, value] of Object.entries(shape)) {
|
|
134
|
+
const fieldSchema = convertZodType(value);
|
|
135
|
+
properties[key] = fieldSchema;
|
|
136
|
+
// 检查是否可选
|
|
137
|
+
const isOptional = isZodTypeOptional(value);
|
|
138
|
+
if (!isOptional) {
|
|
139
|
+
required.push(key);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return {
|
|
143
|
+
type: 'object',
|
|
144
|
+
properties,
|
|
145
|
+
required,
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
// 默认返回空对象
|
|
149
|
+
return {
|
|
150
|
+
type: 'object',
|
|
151
|
+
properties: {},
|
|
152
|
+
required: [],
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* 转换 Zod 类型为 JSON Schema 类型
|
|
157
|
+
*/
|
|
158
|
+
function convertZodType(zodType) {
|
|
159
|
+
// 处理可选类型
|
|
160
|
+
if (zodType instanceof zod_1.z.ZodOptional) {
|
|
161
|
+
const innerType = zodType._def.innerType;
|
|
162
|
+
return convertZodType(innerType);
|
|
163
|
+
}
|
|
164
|
+
// 处理默认值类型
|
|
165
|
+
if (zodType instanceof zod_1.z.ZodDefault) {
|
|
166
|
+
const innerType = zodType._def.innerType;
|
|
167
|
+
return convertZodType(innerType);
|
|
168
|
+
}
|
|
169
|
+
// 基本类型映射
|
|
170
|
+
if (zodType instanceof zod_1.z.ZodString) {
|
|
171
|
+
return { type: 'string' };
|
|
172
|
+
}
|
|
173
|
+
if (zodType instanceof zod_1.z.ZodNumber) {
|
|
174
|
+
return { type: 'number' };
|
|
175
|
+
}
|
|
176
|
+
if (zodType instanceof zod_1.z.ZodBoolean) {
|
|
177
|
+
return { type: 'boolean' };
|
|
178
|
+
}
|
|
179
|
+
if (zodType instanceof zod_1.z.ZodArray) {
|
|
180
|
+
const elementType = zodType._def.type;
|
|
181
|
+
return {
|
|
182
|
+
type: 'array',
|
|
183
|
+
items: convertZodType(elementType),
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
if (zodType instanceof zod_1.z.ZodObject) {
|
|
187
|
+
return zodToJsonSchemaManual(zodType);
|
|
188
|
+
}
|
|
189
|
+
if (zodType instanceof zod_1.z.ZodEnum) {
|
|
190
|
+
return {
|
|
191
|
+
type: 'string',
|
|
192
|
+
enum: zodType._def.values,
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
if (zodType instanceof zod_1.z.ZodLiteral) {
|
|
196
|
+
return {
|
|
197
|
+
type: typeof zodType._def.value,
|
|
198
|
+
const: zodType._def.value,
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
// 未知类型,默认为 object(避免意外返回 string)
|
|
202
|
+
return { type: 'object', properties: {}, required: [] };
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* 检查 Zod 类型是否可选
|
|
206
|
+
*/
|
|
207
|
+
function isZodTypeOptional(zodType) {
|
|
208
|
+
return (zodType instanceof zod_1.z.ZodOptional ||
|
|
209
|
+
zodType instanceof zod_1.z.ZodDefault ||
|
|
210
|
+
zodType.isNullable?.() === true);
|
|
211
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ToolKit = void 0;
|
|
4
|
+
exports.toolMethod = toolMethod;
|
|
5
|
+
const zod_1 = require("zod");
|
|
6
|
+
const tool_1 = require("./tool");
|
|
7
|
+
/**
|
|
8
|
+
* 工具方法装饰器
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* class WeatherKit extends ToolKit {
|
|
13
|
+
* @toolMethod({ description: 'Get current weather' })
|
|
14
|
+
* async getWeather(args: { city: string }, ctx: ToolContext) {
|
|
15
|
+
* return { temperature: 25, city: args.city };
|
|
16
|
+
* }
|
|
17
|
+
* }
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
function toolMethod(metadata = {}) {
|
|
21
|
+
return function (target, propertyKey, descriptor) {
|
|
22
|
+
// 存储元数据到类的原型
|
|
23
|
+
if (!target.constructor._toolMethods) {
|
|
24
|
+
target.constructor._toolMethods = new Map();
|
|
25
|
+
}
|
|
26
|
+
target.constructor._toolMethods.set(propertyKey, {
|
|
27
|
+
...metadata,
|
|
28
|
+
method: descriptor.value,
|
|
29
|
+
});
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* ToolKit 基类
|
|
34
|
+
*
|
|
35
|
+
* 提供组织化的工具定义方式
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```ts
|
|
39
|
+
* class DatabaseKit extends ToolKit {
|
|
40
|
+
* constructor(private db: Database) {
|
|
41
|
+
* super('db');
|
|
42
|
+
* }
|
|
43
|
+
*
|
|
44
|
+
* @toolMethod({
|
|
45
|
+
* description: 'Query database',
|
|
46
|
+
* parameters: z.object({ query: z.string() })
|
|
47
|
+
* })
|
|
48
|
+
* async query(args: { query: string }, ctx: ToolContext) {
|
|
49
|
+
* return await this.db.query(args.query);
|
|
50
|
+
* }
|
|
51
|
+
*
|
|
52
|
+
* @toolMethod({ description: 'Insert record' })
|
|
53
|
+
* async insert(args: { table: string; data: any }, ctx: ToolContext) {
|
|
54
|
+
* return await this.db.insert(args.table, args.data);
|
|
55
|
+
* }
|
|
56
|
+
* }
|
|
57
|
+
*
|
|
58
|
+
* // 使用
|
|
59
|
+
* const dbKit = new DatabaseKit(myDatabase);
|
|
60
|
+
* const tools = dbKit.getTools();
|
|
61
|
+
* // 返回: [db__query, db__insert]
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
class ToolKit {
|
|
65
|
+
constructor(namespace) {
|
|
66
|
+
this.namespace = namespace;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* 获取所有工具实例
|
|
70
|
+
*/
|
|
71
|
+
getTools() {
|
|
72
|
+
const constructor = this.constructor;
|
|
73
|
+
const toolMethods = constructor._toolMethods;
|
|
74
|
+
if (!toolMethods) {
|
|
75
|
+
return [];
|
|
76
|
+
}
|
|
77
|
+
const tools = [];
|
|
78
|
+
for (const [methodName, metadata] of toolMethods) {
|
|
79
|
+
const toolName = this.namespace ? `${this.namespace}__${methodName}` : methodName;
|
|
80
|
+
const def = {
|
|
81
|
+
name: toolName,
|
|
82
|
+
description: metadata.description || `Execute ${methodName}`,
|
|
83
|
+
parameters: metadata.parameters || zod_1.z.any(),
|
|
84
|
+
execute: metadata.method.bind(this),
|
|
85
|
+
metadata: metadata.metadata,
|
|
86
|
+
};
|
|
87
|
+
tools.push((0, tool_1.tool)(def));
|
|
88
|
+
}
|
|
89
|
+
return tools;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* 获取工具名称列表
|
|
93
|
+
*/
|
|
94
|
+
getToolNames() {
|
|
95
|
+
return this.getTools().map((t) => t.name);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
exports.ToolKit = ToolKit;
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.patterns = exports.SchemaBuilder = void 0;
|
|
4
|
+
exports.inferFromExample = inferFromExample;
|
|
5
|
+
exports.schema = schema;
|
|
6
|
+
exports.mergeSchemas = mergeSchemas;
|
|
7
|
+
exports.extendSchema = extendSchema;
|
|
8
|
+
const zod_1 = require("zod");
|
|
9
|
+
/**
|
|
10
|
+
* TypeScript 类型到 Zod schema 的自动推断
|
|
11
|
+
*
|
|
12
|
+
* 注意:由于 TypeScript 类型在运行时被擦除,我们无法直接从类型生成 schema。
|
|
13
|
+
* 这个模块提供了一些辅助函数来简化 schema 定义。
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* 从示例对象推断 Zod schema
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* const schema = inferFromExample({
|
|
21
|
+
* name: 'string',
|
|
22
|
+
* age: 0,
|
|
23
|
+
* active: true,
|
|
24
|
+
* tags: ['string']
|
|
25
|
+
* });
|
|
26
|
+
* // 等价于:
|
|
27
|
+
* z.object({
|
|
28
|
+
* name: z.string(),
|
|
29
|
+
* age: z.number(),
|
|
30
|
+
* active: z.boolean(),
|
|
31
|
+
* tags: z.array(z.string())
|
|
32
|
+
* })
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
function inferFromExample(example) {
|
|
36
|
+
const shape = {};
|
|
37
|
+
for (const [key, value] of Object.entries(example)) {
|
|
38
|
+
shape[key] = inferValueType(value);
|
|
39
|
+
}
|
|
40
|
+
return zod_1.z.object(shape);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* 推断单个值的类型
|
|
44
|
+
*/
|
|
45
|
+
function inferValueType(value) {
|
|
46
|
+
if (value === null || value === undefined) {
|
|
47
|
+
return zod_1.z.any();
|
|
48
|
+
}
|
|
49
|
+
const type = typeof value;
|
|
50
|
+
switch (type) {
|
|
51
|
+
case 'string':
|
|
52
|
+
return zod_1.z.string();
|
|
53
|
+
case 'number':
|
|
54
|
+
return zod_1.z.number();
|
|
55
|
+
case 'boolean':
|
|
56
|
+
return zod_1.z.boolean();
|
|
57
|
+
case 'object':
|
|
58
|
+
if (Array.isArray(value)) {
|
|
59
|
+
if (value.length === 0) {
|
|
60
|
+
return zod_1.z.array(zod_1.z.any());
|
|
61
|
+
}
|
|
62
|
+
return zod_1.z.array(inferValueType(value[0]));
|
|
63
|
+
}
|
|
64
|
+
return inferFromExample(value);
|
|
65
|
+
default:
|
|
66
|
+
return zod_1.z.any();
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Schema 构建器 - 提供流畅的 API
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```ts
|
|
74
|
+
* const schema = schema()
|
|
75
|
+
* .string('name', 'User name')
|
|
76
|
+
* .number('age', 'User age').optional()
|
|
77
|
+
* .boolean('active').default(true)
|
|
78
|
+
* .array('tags', z.string())
|
|
79
|
+
* .build();
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
class SchemaBuilder {
|
|
83
|
+
constructor() {
|
|
84
|
+
this.fields = {};
|
|
85
|
+
}
|
|
86
|
+
string(name, description) {
|
|
87
|
+
this.fields[name] = description ? zod_1.z.string().describe(description) : zod_1.z.string();
|
|
88
|
+
return this;
|
|
89
|
+
}
|
|
90
|
+
number(name, description) {
|
|
91
|
+
this.fields[name] = description ? zod_1.z.number().describe(description) : zod_1.z.number();
|
|
92
|
+
return this;
|
|
93
|
+
}
|
|
94
|
+
boolean(name, description) {
|
|
95
|
+
this.fields[name] = description ? zod_1.z.boolean().describe(description) : zod_1.z.boolean();
|
|
96
|
+
return this;
|
|
97
|
+
}
|
|
98
|
+
array(name, itemSchema, description) {
|
|
99
|
+
const schema = zod_1.z.array(itemSchema);
|
|
100
|
+
this.fields[name] = description ? schema.describe(description) : schema;
|
|
101
|
+
return this;
|
|
102
|
+
}
|
|
103
|
+
object(name, shape, description) {
|
|
104
|
+
const schema = zod_1.z.object(shape);
|
|
105
|
+
this.fields[name] = description ? schema.describe(description) : schema;
|
|
106
|
+
return this;
|
|
107
|
+
}
|
|
108
|
+
enum(name, values, description) {
|
|
109
|
+
const schema = zod_1.z.enum(values);
|
|
110
|
+
this.fields[name] = description ? schema.describe(description) : schema;
|
|
111
|
+
return this;
|
|
112
|
+
}
|
|
113
|
+
optional(name) {
|
|
114
|
+
if (this.fields[name]) {
|
|
115
|
+
this.fields[name] = this.fields[name].optional();
|
|
116
|
+
}
|
|
117
|
+
return this;
|
|
118
|
+
}
|
|
119
|
+
default(name, defaultValue) {
|
|
120
|
+
if (this.fields[name]) {
|
|
121
|
+
this.fields[name] = this.fields[name].default(defaultValue);
|
|
122
|
+
}
|
|
123
|
+
return this;
|
|
124
|
+
}
|
|
125
|
+
custom(name, schema) {
|
|
126
|
+
this.fields[name] = schema;
|
|
127
|
+
return this;
|
|
128
|
+
}
|
|
129
|
+
build() {
|
|
130
|
+
return zod_1.z.object(this.fields);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
exports.SchemaBuilder = SchemaBuilder;
|
|
134
|
+
/**
|
|
135
|
+
* 创建 schema 构建器
|
|
136
|
+
*/
|
|
137
|
+
function schema() {
|
|
138
|
+
return new SchemaBuilder();
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* 快速定义常用的 schema 模式
|
|
142
|
+
*/
|
|
143
|
+
exports.patterns = {
|
|
144
|
+
/**
|
|
145
|
+
* 文件路径
|
|
146
|
+
*/
|
|
147
|
+
filePath: (description = 'File path') => zod_1.z.string().describe(description),
|
|
148
|
+
/**
|
|
149
|
+
* 目录路径
|
|
150
|
+
*/
|
|
151
|
+
dirPath: (description = 'Directory path') => zod_1.z.string().describe(description),
|
|
152
|
+
/**
|
|
153
|
+
* URL
|
|
154
|
+
*/
|
|
155
|
+
url: (description = 'URL') => zod_1.z.string().url().describe(description),
|
|
156
|
+
/**
|
|
157
|
+
* Email
|
|
158
|
+
*/
|
|
159
|
+
email: (description = 'Email address') => zod_1.z.string().email().describe(description),
|
|
160
|
+
/**
|
|
161
|
+
* 正整数
|
|
162
|
+
*/
|
|
163
|
+
positiveInt: (description = 'Positive integer') => zod_1.z.number().int().positive().describe(description),
|
|
164
|
+
/**
|
|
165
|
+
* 非负整数
|
|
166
|
+
*/
|
|
167
|
+
nonNegativeInt: (description = 'Non-negative integer') => zod_1.z.number().int().nonnegative().describe(description),
|
|
168
|
+
/**
|
|
169
|
+
* 字符串数组
|
|
170
|
+
*/
|
|
171
|
+
stringArray: (description = 'Array of strings') => zod_1.z.array(zod_1.z.string()).describe(description),
|
|
172
|
+
/**
|
|
173
|
+
* 可选字符串
|
|
174
|
+
*/
|
|
175
|
+
optionalString: (description) => zod_1.z.string().optional().describe(description || 'Optional string'),
|
|
176
|
+
/**
|
|
177
|
+
* 可选数字
|
|
178
|
+
*/
|
|
179
|
+
optionalNumber: (description) => zod_1.z.number().optional().describe(description || 'Optional number'),
|
|
180
|
+
/**
|
|
181
|
+
* JSON 对象
|
|
182
|
+
*/
|
|
183
|
+
json: (description = 'JSON object') => zod_1.z.record(zod_1.z.string(), zod_1.z.any()).describe(description),
|
|
184
|
+
};
|
|
185
|
+
/**
|
|
186
|
+
* 辅助函数:合并多个 schema
|
|
187
|
+
*/
|
|
188
|
+
function mergeSchemas(...schemas) {
|
|
189
|
+
if (schemas.length === 0) {
|
|
190
|
+
return zod_1.z.object({});
|
|
191
|
+
}
|
|
192
|
+
if (schemas.length === 1) {
|
|
193
|
+
return schemas[0];
|
|
194
|
+
}
|
|
195
|
+
// 使用 z.intersection 合并
|
|
196
|
+
return schemas.reduce((acc, schema) => acc.and(schema));
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* 辅助函数:扩展 schema
|
|
200
|
+
*/
|
|
201
|
+
function extendSchema(base, extension) {
|
|
202
|
+
if (base instanceof zod_1.z.ZodObject) {
|
|
203
|
+
return base.extend(extension);
|
|
204
|
+
}
|
|
205
|
+
// 如果不是 object schema,创建新的 object schema
|
|
206
|
+
return zod_1.z.object(extension);
|
|
207
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateAgentId = generateAgentId;
|
|
4
|
+
const CROCKFORD32 = '0123456789ABCDEFGHJKMNPQRSTVWXYZ';
|
|
5
|
+
function encodeTime(time, length) {
|
|
6
|
+
let remaining = time;
|
|
7
|
+
const chars = Array(length);
|
|
8
|
+
for (let i = length - 1; i >= 0; i--) {
|
|
9
|
+
const mod = remaining % 32;
|
|
10
|
+
chars[i] = CROCKFORD32.charAt(mod);
|
|
11
|
+
remaining = Math.floor(remaining / 32);
|
|
12
|
+
}
|
|
13
|
+
return chars.join('');
|
|
14
|
+
}
|
|
15
|
+
function encodeRandom(length) {
|
|
16
|
+
const chars = Array(length);
|
|
17
|
+
for (let i = 0; i < length; i++) {
|
|
18
|
+
const rand = Math.floor(Math.random() * 32);
|
|
19
|
+
chars[i] = CROCKFORD32.charAt(rand);
|
|
20
|
+
}
|
|
21
|
+
return chars.join('');
|
|
22
|
+
}
|
|
23
|
+
function generateAgentId() {
|
|
24
|
+
const time = Date.now();
|
|
25
|
+
const timePart = encodeTime(time, 10);
|
|
26
|
+
const randomPart = encodeRandom(16);
|
|
27
|
+
return `agt-${timePart}${randomPart}`;
|
|
28
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* 日志工具模块
|
|
4
|
+
*
|
|
5
|
+
* 仅在 NODE_ENV=local 时输出日志,生产环境静默
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.logger = void 0;
|
|
9
|
+
/**
|
|
10
|
+
* 检查是否启用日志(仅在本地开发环境)
|
|
11
|
+
*/
|
|
12
|
+
function isLoggingEnabled() {
|
|
13
|
+
return process.env.NODE_ENV === 'local';
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* 日志输出(仅在 NODE_ENV=local 时输出)
|
|
17
|
+
*/
|
|
18
|
+
exports.logger = {
|
|
19
|
+
log: (...args) => {
|
|
20
|
+
if (isLoggingEnabled()) {
|
|
21
|
+
console.log(...args);
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
info: (...args) => {
|
|
25
|
+
if (isLoggingEnabled()) {
|
|
26
|
+
console.info(...args);
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
warn: (...args) => {
|
|
30
|
+
if (isLoggingEnabled()) {
|
|
31
|
+
console.warn(...args);
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
error: (...args) => {
|
|
35
|
+
if (isLoggingEnabled()) {
|
|
36
|
+
console.error(...args);
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
debug: (...args) => {
|
|
40
|
+
if (isLoggingEnabled()) {
|
|
41
|
+
console.debug(...args);
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SessionId = void 0;
|
|
4
|
+
class SessionId {
|
|
5
|
+
static parse(id) {
|
|
6
|
+
const parts = id.split('/');
|
|
7
|
+
const components = {
|
|
8
|
+
agentTemplate: '',
|
|
9
|
+
rootId: '',
|
|
10
|
+
forkIds: [],
|
|
11
|
+
};
|
|
12
|
+
for (const part of parts) {
|
|
13
|
+
if (part.startsWith('org-')) {
|
|
14
|
+
components.orgId = part.slice(4);
|
|
15
|
+
}
|
|
16
|
+
else if (part.startsWith('team-')) {
|
|
17
|
+
components.teamId = part.slice(5);
|
|
18
|
+
}
|
|
19
|
+
else if (part.startsWith('user-')) {
|
|
20
|
+
components.userId = part.slice(5);
|
|
21
|
+
}
|
|
22
|
+
else if (part.startsWith('agent-')) {
|
|
23
|
+
components.agentTemplate = part.slice(6);
|
|
24
|
+
}
|
|
25
|
+
else if (part.startsWith('session-')) {
|
|
26
|
+
components.rootId = part.slice(8);
|
|
27
|
+
}
|
|
28
|
+
else if (part.startsWith('fork-')) {
|
|
29
|
+
components.forkIds.push(part.slice(5));
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return components;
|
|
33
|
+
}
|
|
34
|
+
static generate(opts) {
|
|
35
|
+
const parts = [];
|
|
36
|
+
if (opts.orgId)
|
|
37
|
+
parts.push(`org-${opts.orgId}`);
|
|
38
|
+
if (opts.teamId)
|
|
39
|
+
parts.push(`team-${opts.teamId}`);
|
|
40
|
+
if (opts.userId)
|
|
41
|
+
parts.push(`user-${opts.userId}`);
|
|
42
|
+
parts.push(`agent-${opts.agentTemplate}`);
|
|
43
|
+
if (opts.parentSessionId) {
|
|
44
|
+
const parent = SessionId.parse(opts.parentSessionId);
|
|
45
|
+
parts.push(`session-${parent.rootId}`);
|
|
46
|
+
parts.push(...parent.forkIds.map((id) => `fork-${id}`));
|
|
47
|
+
parts.push(`fork-${this.randomId()}`);
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
parts.push(`session-${this.randomId()}`);
|
|
51
|
+
}
|
|
52
|
+
return parts.join('/');
|
|
53
|
+
}
|
|
54
|
+
static snapshot(sessionId, sfpIndex) {
|
|
55
|
+
return `${sessionId}@sfp-${sfpIndex}`;
|
|
56
|
+
}
|
|
57
|
+
static label(sessionId, label) {
|
|
58
|
+
return `${sessionId}@label-${label}`;
|
|
59
|
+
}
|
|
60
|
+
static randomId() {
|
|
61
|
+
return Math.random().toString(36).slice(2, 8);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
exports.SessionId = SessionId;
|