kode-sdk 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.
Files changed (169) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +74 -0
  3. package/dist/core/agent/breakpoint-manager.d.ts +16 -0
  4. package/dist/core/agent/breakpoint-manager.js +36 -0
  5. package/dist/core/agent/message-queue.d.ts +26 -0
  6. package/dist/core/agent/message-queue.js +47 -0
  7. package/dist/core/agent/permission-manager.d.ts +9 -0
  8. package/dist/core/agent/permission-manager.js +32 -0
  9. package/dist/core/agent/todo-manager.d.ts +26 -0
  10. package/dist/core/agent/todo-manager.js +91 -0
  11. package/dist/core/agent/tool-runner.d.ts +9 -0
  12. package/dist/core/agent/tool-runner.js +45 -0
  13. package/dist/core/agent.d.ts +271 -0
  14. package/dist/core/agent.js +2334 -0
  15. package/dist/core/checkpointer.d.ts +96 -0
  16. package/dist/core/checkpointer.js +57 -0
  17. package/dist/core/checkpointers/file.d.ts +20 -0
  18. package/dist/core/checkpointers/file.js +153 -0
  19. package/dist/core/checkpointers/index.d.ts +3 -0
  20. package/dist/core/checkpointers/index.js +9 -0
  21. package/dist/core/checkpointers/redis.d.ts +35 -0
  22. package/dist/core/checkpointers/redis.js +113 -0
  23. package/dist/core/compression/ai-strategy.d.ts +53 -0
  24. package/dist/core/compression/ai-strategy.js +298 -0
  25. package/dist/core/compression/index.d.ts +12 -0
  26. package/dist/core/compression/index.js +27 -0
  27. package/dist/core/compression/prompts.d.ts +35 -0
  28. package/dist/core/compression/prompts.js +114 -0
  29. package/dist/core/compression/simple-strategy.d.ts +44 -0
  30. package/dist/core/compression/simple-strategy.js +240 -0
  31. package/dist/core/compression/token-estimator.d.ts +42 -0
  32. package/dist/core/compression/token-estimator.js +121 -0
  33. package/dist/core/compression/types.d.ts +140 -0
  34. package/dist/core/compression/types.js +9 -0
  35. package/dist/core/config.d.ts +10 -0
  36. package/dist/core/config.js +2 -0
  37. package/dist/core/context-manager.d.ts +115 -0
  38. package/dist/core/context-manager.js +107 -0
  39. package/dist/core/errors.d.ts +6 -0
  40. package/dist/core/errors.js +17 -0
  41. package/dist/core/events.d.ts +49 -0
  42. package/dist/core/events.js +312 -0
  43. package/dist/core/file-pool.d.ts +43 -0
  44. package/dist/core/file-pool.js +120 -0
  45. package/dist/core/hooks.d.ts +23 -0
  46. package/dist/core/hooks.js +71 -0
  47. package/dist/core/permission-modes.d.ts +31 -0
  48. package/dist/core/permission-modes.js +61 -0
  49. package/dist/core/pool.d.ts +31 -0
  50. package/dist/core/pool.js +87 -0
  51. package/dist/core/room.d.ts +15 -0
  52. package/dist/core/room.js +57 -0
  53. package/dist/core/scheduler.d.ts +33 -0
  54. package/dist/core/scheduler.js +58 -0
  55. package/dist/core/template.d.ts +69 -0
  56. package/dist/core/template.js +35 -0
  57. package/dist/core/time-bridge.d.ts +18 -0
  58. package/dist/core/time-bridge.js +100 -0
  59. package/dist/core/todo.d.ts +34 -0
  60. package/dist/core/todo.js +89 -0
  61. package/dist/core/types.d.ts +380 -0
  62. package/dist/core/types.js +3 -0
  63. package/dist/index.d.ts +51 -0
  64. package/dist/index.js +147 -0
  65. package/dist/infra/provider.d.ts +144 -0
  66. package/dist/infra/provider.js +294 -0
  67. package/dist/infra/sandbox-factory.d.ts +10 -0
  68. package/dist/infra/sandbox-factory.js +21 -0
  69. package/dist/infra/sandbox.d.ts +87 -0
  70. package/dist/infra/sandbox.js +255 -0
  71. package/dist/infra/store.d.ts +154 -0
  72. package/dist/infra/store.js +584 -0
  73. package/dist/skills/index.d.ts +12 -0
  74. package/dist/skills/index.js +36 -0
  75. package/dist/skills/injector.d.ts +29 -0
  76. package/dist/skills/injector.js +96 -0
  77. package/dist/skills/loader.d.ts +59 -0
  78. package/dist/skills/loader.js +215 -0
  79. package/dist/skills/manager.d.ts +85 -0
  80. package/dist/skills/manager.js +221 -0
  81. package/dist/skills/parser.d.ts +40 -0
  82. package/dist/skills/parser.js +107 -0
  83. package/dist/skills/types.d.ts +107 -0
  84. package/dist/skills/types.js +7 -0
  85. package/dist/skills/validator.d.ts +30 -0
  86. package/dist/skills/validator.js +121 -0
  87. package/dist/store.d.ts +1 -0
  88. package/dist/store.js +5 -0
  89. package/dist/tools/bash_kill/index.d.ts +1 -0
  90. package/dist/tools/bash_kill/index.js +35 -0
  91. package/dist/tools/bash_kill/prompt.d.ts +2 -0
  92. package/dist/tools/bash_kill/prompt.js +14 -0
  93. package/dist/tools/bash_logs/index.d.ts +1 -0
  94. package/dist/tools/bash_logs/index.js +40 -0
  95. package/dist/tools/bash_logs/prompt.d.ts +2 -0
  96. package/dist/tools/bash_logs/prompt.js +14 -0
  97. package/dist/tools/bash_run/index.d.ts +16 -0
  98. package/dist/tools/bash_run/index.js +61 -0
  99. package/dist/tools/bash_run/prompt.d.ts +2 -0
  100. package/dist/tools/bash_run/prompt.js +18 -0
  101. package/dist/tools/builtin.d.ts +9 -0
  102. package/dist/tools/builtin.js +27 -0
  103. package/dist/tools/define.d.ts +101 -0
  104. package/dist/tools/define.js +214 -0
  105. package/dist/tools/fs_edit/index.d.ts +1 -0
  106. package/dist/tools/fs_edit/index.js +62 -0
  107. package/dist/tools/fs_edit/prompt.d.ts +2 -0
  108. package/dist/tools/fs_edit/prompt.js +15 -0
  109. package/dist/tools/fs_glob/index.d.ts +1 -0
  110. package/dist/tools/fs_glob/index.js +60 -0
  111. package/dist/tools/fs_glob/prompt.d.ts +2 -0
  112. package/dist/tools/fs_glob/prompt.js +18 -0
  113. package/dist/tools/fs_grep/index.d.ts +1 -0
  114. package/dist/tools/fs_grep/index.js +66 -0
  115. package/dist/tools/fs_grep/prompt.d.ts +2 -0
  116. package/dist/tools/fs_grep/prompt.js +16 -0
  117. package/dist/tools/fs_multi_edit/index.d.ts +1 -0
  118. package/dist/tools/fs_multi_edit/index.js +106 -0
  119. package/dist/tools/fs_multi_edit/prompt.d.ts +2 -0
  120. package/dist/tools/fs_multi_edit/prompt.js +16 -0
  121. package/dist/tools/fs_read/index.d.ts +1 -0
  122. package/dist/tools/fs_read/index.js +40 -0
  123. package/dist/tools/fs_read/prompt.d.ts +2 -0
  124. package/dist/tools/fs_read/prompt.js +16 -0
  125. package/dist/tools/fs_rm/index.d.ts +1 -0
  126. package/dist/tools/fs_rm/index.js +41 -0
  127. package/dist/tools/fs_rm/prompt.d.ts +2 -0
  128. package/dist/tools/fs_rm/prompt.js +14 -0
  129. package/dist/tools/fs_write/index.d.ts +1 -0
  130. package/dist/tools/fs_write/index.js +40 -0
  131. package/dist/tools/fs_write/prompt.d.ts +2 -0
  132. package/dist/tools/fs_write/prompt.js +15 -0
  133. package/dist/tools/index.d.ts +9 -0
  134. package/dist/tools/index.js +56 -0
  135. package/dist/tools/mcp.d.ts +73 -0
  136. package/dist/tools/mcp.js +198 -0
  137. package/dist/tools/registry.d.ts +29 -0
  138. package/dist/tools/registry.js +26 -0
  139. package/dist/tools/skill_activate/index.d.ts +5 -0
  140. package/dist/tools/skill_activate/index.js +63 -0
  141. package/dist/tools/skill_list/index.d.ts +5 -0
  142. package/dist/tools/skill_list/index.js +48 -0
  143. package/dist/tools/skill_resource/index.d.ts +5 -0
  144. package/dist/tools/skill_resource/index.js +82 -0
  145. package/dist/tools/task_run/index.d.ts +7 -0
  146. package/dist/tools/task_run/index.js +60 -0
  147. package/dist/tools/task_run/prompt.d.ts +5 -0
  148. package/dist/tools/task_run/prompt.js +29 -0
  149. package/dist/tools/todo_read/index.d.ts +1 -0
  150. package/dist/tools/todo_read/index.js +29 -0
  151. package/dist/tools/todo_read/prompt.d.ts +2 -0
  152. package/dist/tools/todo_read/prompt.js +18 -0
  153. package/dist/tools/todo_write/index.d.ts +1 -0
  154. package/dist/tools/todo_write/index.js +42 -0
  155. package/dist/tools/todo_write/prompt.d.ts +2 -0
  156. package/dist/tools/todo_write/prompt.js +23 -0
  157. package/dist/tools/tool.d.ts +43 -0
  158. package/dist/tools/tool.js +104 -0
  159. package/dist/tools/toolkit.d.ts +69 -0
  160. package/dist/tools/toolkit.js +98 -0
  161. package/dist/tools/type-inference.d.ts +127 -0
  162. package/dist/tools/type-inference.js +207 -0
  163. package/dist/utils/agent-id.d.ts +1 -0
  164. package/dist/utils/agent-id.js +28 -0
  165. package/dist/utils/session-id.d.ts +21 -0
  166. package/dist/utils/session-id.js +64 -0
  167. package/dist/utils/unicode.d.ts +17 -0
  168. package/dist/utils/unicode.js +62 -0
  169. package/package.json +117 -0
@@ -0,0 +1,69 @@
1
+ import { ZodType } from 'zod';
2
+ import type { ToolInstance } from '../index';
3
+ /**
4
+ * ToolKit 装饰器元数据
5
+ */
6
+ interface ToolMethodMetadata {
7
+ description?: string;
8
+ parameters?: ZodType;
9
+ metadata?: any;
10
+ }
11
+ /**
12
+ * 工具方法装饰器
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * class WeatherKit extends ToolKit {
17
+ * @toolMethod({ description: 'Get current weather' })
18
+ * async getWeather(args: { city: string }, ctx: ToolContext) {
19
+ * return { temperature: 25, city: args.city };
20
+ * }
21
+ * }
22
+ * ```
23
+ */
24
+ export declare function toolMethod(metadata?: ToolMethodMetadata): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
25
+ /**
26
+ * ToolKit 基类
27
+ *
28
+ * 提供组织化的工具定义方式
29
+ *
30
+ * @example
31
+ * ```ts
32
+ * class DatabaseKit extends ToolKit {
33
+ * constructor(private db: Database) {
34
+ * super('db');
35
+ * }
36
+ *
37
+ * @toolMethod({
38
+ * description: 'Query database',
39
+ * parameters: z.object({ query: z.string() })
40
+ * })
41
+ * async query(args: { query: string }, ctx: ToolContext) {
42
+ * return await this.db.query(args.query);
43
+ * }
44
+ *
45
+ * @toolMethod({ description: 'Insert record' })
46
+ * async insert(args: { table: string; data: any }, ctx: ToolContext) {
47
+ * return await this.db.insert(args.table, args.data);
48
+ * }
49
+ * }
50
+ *
51
+ * // 使用
52
+ * const dbKit = new DatabaseKit(myDatabase);
53
+ * const tools = dbKit.getTools();
54
+ * // 返回: [db__query, db__insert]
55
+ * ```
56
+ */
57
+ export declare class ToolKit {
58
+ private readonly namespace?;
59
+ constructor(namespace?: string | undefined);
60
+ /**
61
+ * 获取所有工具实例
62
+ */
63
+ getTools(): ToolInstance[];
64
+ /**
65
+ * 获取工具名称列表
66
+ */
67
+ getToolNames(): string[];
68
+ }
69
+ export {};
@@ -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,127 @@
1
+ import { z, ZodType, ZodTypeAny } from 'zod';
2
+ /**
3
+ * TypeScript 类型到 Zod schema 的自动推断
4
+ *
5
+ * 注意:由于 TypeScript 类型在运行时被擦除,我们无法直接从类型生成 schema。
6
+ * 这个模块提供了一些辅助函数来简化 schema 定义。
7
+ */
8
+ /**
9
+ * 从示例对象推断 Zod schema
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * const schema = inferFromExample({
14
+ * name: 'string',
15
+ * age: 0,
16
+ * active: true,
17
+ * tags: ['string']
18
+ * });
19
+ * // 等价于:
20
+ * z.object({
21
+ * name: z.string(),
22
+ * age: z.number(),
23
+ * active: z.boolean(),
24
+ * tags: z.array(z.string())
25
+ * })
26
+ * ```
27
+ */
28
+ export declare function inferFromExample<T extends Record<string, any>>(example: T): ZodType<any>;
29
+ /**
30
+ * Schema 构建器 - 提供流畅的 API
31
+ *
32
+ * @example
33
+ * ```ts
34
+ * const schema = schema()
35
+ * .string('name', 'User name')
36
+ * .number('age', 'User age').optional()
37
+ * .boolean('active').default(true)
38
+ * .array('tags', z.string())
39
+ * .build();
40
+ * ```
41
+ */
42
+ export declare class SchemaBuilder {
43
+ private fields;
44
+ string(name: string, description?: string): this;
45
+ number(name: string, description?: string): this;
46
+ boolean(name: string, description?: string): this;
47
+ array(name: string, itemSchema: ZodTypeAny, description?: string): this;
48
+ object(name: string, shape: Record<string, ZodTypeAny>, description?: string): this;
49
+ enum(name: string, values: readonly [string, ...string[]], description?: string): this;
50
+ optional(name: string): this;
51
+ default(name: string, defaultValue: any): this;
52
+ custom(name: string, schema: ZodTypeAny): this;
53
+ build(): ZodType<any>;
54
+ }
55
+ /**
56
+ * 创建 schema 构建器
57
+ */
58
+ export declare function schema(): SchemaBuilder;
59
+ /**
60
+ * 快速定义常用的 schema 模式
61
+ */
62
+ export declare const patterns: {
63
+ /**
64
+ * 文件路径
65
+ */
66
+ filePath: (description?: string) => z.ZodString;
67
+ /**
68
+ * 目录路径
69
+ */
70
+ dirPath: (description?: string) => z.ZodString;
71
+ /**
72
+ * URL
73
+ */
74
+ url: (description?: string) => z.ZodString;
75
+ /**
76
+ * Email
77
+ */
78
+ email: (description?: string) => z.ZodString;
79
+ /**
80
+ * 正整数
81
+ */
82
+ positiveInt: (description?: string) => z.ZodNumber;
83
+ /**
84
+ * 非负整数
85
+ */
86
+ nonNegativeInt: (description?: string) => z.ZodNumber;
87
+ /**
88
+ * 字符串数组
89
+ */
90
+ stringArray: (description?: string) => z.ZodArray<z.ZodString, "many">;
91
+ /**
92
+ * 可选字符串
93
+ */
94
+ optionalString: (description?: string) => z.ZodOptional<z.ZodString>;
95
+ /**
96
+ * 可选数字
97
+ */
98
+ optionalNumber: (description?: string) => z.ZodOptional<z.ZodNumber>;
99
+ /**
100
+ * JSON 对象
101
+ */
102
+ json: (description?: string) => z.ZodRecord<z.ZodString, z.ZodAny>;
103
+ };
104
+ /**
105
+ * 从 JSDoc 注释推断 schema(实验性)
106
+ *
107
+ * 这需要在构建时使用 TypeScript Compiler API 解析
108
+ * 当前仅提供接口,实际实现需要编译时支持
109
+ */
110
+ export interface JSDocSchema {
111
+ /**
112
+ * @param name - Parameter name
113
+ * @param type - TypeScript type string (e.g., 'string', 'number', 'Array<string>')
114
+ * @param description - Parameter description
115
+ * @param optional - Whether parameter is optional
116
+ */
117
+ param(name: string, type: string, description?: string, optional?: boolean): this;
118
+ build(): ZodType<any>;
119
+ }
120
+ /**
121
+ * 辅助函数:合并多个 schema
122
+ */
123
+ export declare function mergeSchemas(...schemas: ZodType<any>[]): ZodType<any>;
124
+ /**
125
+ * 辅助函数:扩展 schema
126
+ */
127
+ export declare function extendSchema<T extends ZodType<any>>(base: T, extension: Record<string, ZodTypeAny>): ZodType<any>;
@@ -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.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 @@
1
+ export declare function generateAgentId(): string;
@@ -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,21 @@
1
+ export interface SessionIdComponents {
2
+ orgId?: string;
3
+ teamId?: string;
4
+ userId?: string;
5
+ agentTemplate: string;
6
+ rootId: string;
7
+ forkIds: string[];
8
+ }
9
+ export declare class SessionId {
10
+ static parse(id: string): SessionIdComponents;
11
+ static generate(opts: {
12
+ orgId?: string;
13
+ teamId?: string;
14
+ userId?: string;
15
+ agentTemplate: string;
16
+ parentSessionId?: string;
17
+ }): string;
18
+ static snapshot(sessionId: string, sfpIndex: number): string;
19
+ static label(sessionId: string, label: string): string;
20
+ private static randomId;
21
+ }
@@ -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;
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Unicode helpers
3
+ *
4
+ * JavaScript strings are UTF-16. If we truncate with `slice()` we can split a surrogate pair
5
+ * (common with emoji), producing an invalid lone surrogate such as `\uD83D`.
6
+ *
7
+ * Some downstream JSON/UTF-8 encoders (notably server-side Python) reject lone surrogates with:
8
+ * "surrogates not allowed".
9
+ */
10
+ export declare function stripInvalidSurrogates(input: string, replacement?: string): string;
11
+ /**
12
+ * Truncate a UTF-16 JS string without splitting surrogate pairs.
13
+ *
14
+ * NOTE: `maxCodeUnits` is the same unit as `string.length` (UTF-16 code units), so this is a
15
+ * drop-in safer alternative to `slice(0, maxCodeUnits)`.
16
+ */
17
+ export declare function safeTruncateUtf16(input: string, maxCodeUnits: number): string;
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.stripInvalidSurrogates = stripInvalidSurrogates;
4
+ exports.safeTruncateUtf16 = safeTruncateUtf16;
5
+ /**
6
+ * Unicode helpers
7
+ *
8
+ * JavaScript strings are UTF-16. If we truncate with `slice()` we can split a surrogate pair
9
+ * (common with emoji), producing an invalid lone surrogate such as `\uD83D`.
10
+ *
11
+ * Some downstream JSON/UTF-8 encoders (notably server-side Python) reject lone surrogates with:
12
+ * "surrogates not allowed".
13
+ */
14
+ function stripInvalidSurrogates(input, replacement = '\uFFFD') {
15
+ if (!input)
16
+ return '';
17
+ let out = '';
18
+ for (let i = 0; i < input.length; i++) {
19
+ const code = input.charCodeAt(i);
20
+ // High surrogate
21
+ if (code >= 0xd800 && code <= 0xdbff) {
22
+ const next = i + 1 < input.length ? input.charCodeAt(i + 1) : 0;
23
+ // Valid pair
24
+ if (next >= 0xdc00 && next <= 0xdfff) {
25
+ out += input[i] + input[i + 1];
26
+ i++;
27
+ continue;
28
+ }
29
+ // Lone high surrogate
30
+ out += replacement;
31
+ continue;
32
+ }
33
+ // Lone low surrogate
34
+ if (code >= 0xdc00 && code <= 0xdfff) {
35
+ out += replacement;
36
+ continue;
37
+ }
38
+ out += input[i];
39
+ }
40
+ return out;
41
+ }
42
+ /**
43
+ * Truncate a UTF-16 JS string without splitting surrogate pairs.
44
+ *
45
+ * NOTE: `maxCodeUnits` is the same unit as `string.length` (UTF-16 code units), so this is a
46
+ * drop-in safer alternative to `slice(0, maxCodeUnits)`.
47
+ */
48
+ function safeTruncateUtf16(input, maxCodeUnits) {
49
+ const text = stripInvalidSurrogates(String(input ?? ''));
50
+ const limit = Number.isFinite(maxCodeUnits) ? Math.max(0, Math.floor(maxCodeUnits)) : 0;
51
+ if (limit <= 0)
52
+ return '';
53
+ if (text.length <= limit)
54
+ return text;
55
+ let sliced = text.slice(0, limit);
56
+ // If we ended on a high surrogate, drop it to avoid creating a lone surrogate.
57
+ const last = sliced.charCodeAt(sliced.length - 1);
58
+ if (last >= 0xd800 && last <= 0xdbff) {
59
+ sliced = sliced.slice(0, -1);
60
+ }
61
+ return stripInvalidSurrogates(sliced);
62
+ }