@shareai-lab/kode-sdk 2.7.1 → 2.7.3

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 (107) hide show
  1. package/README.md +3 -0
  2. package/README.zh-CN.md +3 -0
  3. package/dist/core/agent/breakpoint-manager.js +36 -0
  4. package/dist/core/agent/message-queue.js +57 -0
  5. package/dist/core/agent/permission-manager.js +32 -0
  6. package/dist/core/agent/todo-manager.js +91 -0
  7. package/dist/core/agent/tool-runner.js +45 -0
  8. package/dist/core/agent.d.ts +7 -0
  9. package/dist/core/agent.js +2128 -0
  10. package/dist/core/config.js +2 -0
  11. package/dist/core/context-manager.js +241 -0
  12. package/dist/core/errors.js +49 -0
  13. package/dist/core/events.js +329 -0
  14. package/dist/core/file-pool.d.ts +2 -0
  15. package/dist/core/file-pool.js +125 -0
  16. package/dist/core/hooks.js +71 -0
  17. package/dist/core/permission-modes.js +61 -0
  18. package/dist/core/pool.js +301 -0
  19. package/dist/core/room.js +57 -0
  20. package/dist/core/scheduler.js +58 -0
  21. package/dist/core/skills/index.js +20 -0
  22. package/dist/core/skills/management-manager.d.ts +93 -60
  23. package/dist/core/skills/management-manager.js +827 -0
  24. package/dist/core/skills/manager.d.ts +4 -0
  25. package/dist/core/skills/manager.js +266 -0
  26. package/dist/core/skills/operation-queue.js +113 -0
  27. package/dist/core/skills/sandbox-file-manager.js +183 -0
  28. package/dist/core/skills/types.d.ts +12 -0
  29. package/dist/core/skills/types.js +9 -0
  30. package/dist/core/skills/xml-generator.js +70 -0
  31. package/dist/core/template.js +35 -0
  32. package/dist/core/time-bridge.js +100 -0
  33. package/dist/core/todo.js +89 -0
  34. package/dist/core/types.d.ts +9 -1
  35. package/dist/core/types.js +3 -0
  36. package/dist/index.js +148 -60461
  37. package/dist/infra/db/postgres/postgres-store.js +1073 -0
  38. package/dist/infra/db/sqlite/sqlite-store.js +800 -0
  39. package/dist/infra/e2b/e2b-fs.js +128 -0
  40. package/dist/infra/e2b/e2b-sandbox.js +156 -0
  41. package/dist/infra/e2b/e2b-template.js +105 -0
  42. package/dist/infra/e2b/index.js +9 -0
  43. package/dist/infra/e2b/types.js +2 -0
  44. package/dist/infra/provider.js +67 -0
  45. package/dist/infra/providers/anthropic.js +324 -0
  46. package/dist/infra/providers/core/errors.js +353 -0
  47. package/dist/infra/providers/core/fork.js +418 -0
  48. package/dist/infra/providers/core/index.js +76 -0
  49. package/dist/infra/providers/core/logger.js +191 -0
  50. package/dist/infra/providers/core/retry.js +189 -0
  51. package/dist/infra/providers/core/usage.js +376 -0
  52. package/dist/infra/providers/gemini.js +521 -0
  53. package/dist/infra/providers/index.js +83 -0
  54. package/dist/infra/providers/openai.js +686 -0
  55. package/dist/infra/providers/types.d.ts +35 -1
  56. package/dist/infra/providers/types.js +20 -0
  57. package/dist/infra/providers/utils.d.ts +19 -1
  58. package/dist/infra/providers/utils.js +480 -0
  59. package/dist/infra/sandbox-factory.js +30 -0
  60. package/dist/infra/sandbox.js +243 -0
  61. package/dist/infra/store/factory.js +80 -0
  62. package/dist/infra/store/index.js +26 -0
  63. package/dist/infra/store/json-store.js +607 -0
  64. package/dist/infra/store/types.js +2 -0
  65. package/dist/infra/store.js +29 -0
  66. package/dist/tools/bash_kill/index.js +35 -0
  67. package/dist/tools/bash_kill/prompt.js +14 -0
  68. package/dist/tools/bash_logs/index.js +40 -0
  69. package/dist/tools/bash_logs/prompt.js +14 -0
  70. package/dist/tools/bash_run/index.js +61 -0
  71. package/dist/tools/bash_run/prompt.js +18 -0
  72. package/dist/tools/builtin.js +26 -0
  73. package/dist/tools/define.js +214 -0
  74. package/dist/tools/fs_edit/index.js +62 -0
  75. package/dist/tools/fs_edit/prompt.js +15 -0
  76. package/dist/tools/fs_glob/index.js +40 -0
  77. package/dist/tools/fs_glob/prompt.js +15 -0
  78. package/dist/tools/fs_grep/index.js +66 -0
  79. package/dist/tools/fs_grep/prompt.js +16 -0
  80. package/dist/tools/fs_multi_edit/index.js +106 -0
  81. package/dist/tools/fs_multi_edit/prompt.js +16 -0
  82. package/dist/tools/fs_read/index.js +40 -0
  83. package/dist/tools/fs_read/prompt.js +16 -0
  84. package/dist/tools/fs_write/index.js +40 -0
  85. package/dist/tools/fs_write/prompt.js +15 -0
  86. package/dist/tools/index.js +61 -0
  87. package/dist/tools/mcp.js +185 -0
  88. package/dist/tools/registry.js +26 -0
  89. package/dist/tools/scripts.js +205 -0
  90. package/dist/tools/skills.js +115 -0
  91. package/dist/tools/task_run/index.js +58 -0
  92. package/dist/tools/task_run/prompt.js +25 -0
  93. package/dist/tools/todo_read/index.js +29 -0
  94. package/dist/tools/todo_read/prompt.js +18 -0
  95. package/dist/tools/todo_write/index.js +42 -0
  96. package/dist/tools/todo_write/prompt.js +23 -0
  97. package/dist/tools/tool.js +211 -0
  98. package/dist/tools/toolkit.js +98 -0
  99. package/dist/tools/type-inference.d.ts +1 -1
  100. package/dist/tools/type-inference.js +207 -0
  101. package/dist/utils/agent-id.js +28 -0
  102. package/dist/utils/logger.js +44 -0
  103. package/dist/utils/session-id.js +64 -0
  104. package/package.json +9 -40
  105. package/dist/index.js.map +0 -7
  106. package/dist/index.mjs +0 -60385
  107. package/dist/index.mjs.map +0 -7
package/README.md CHANGED
@@ -142,9 +142,12 @@ See [docs/en/guides/architecture.md](./docs/en/guides/architecture.md) for detai
142
142
  | **Guides** | |
143
143
  | [Events](./docs/en/guides/events.md) | Three-channel event system |
144
144
  | [Tools](./docs/en/guides/tools.md) | Built-in tools & custom tools |
145
+ | [Skills](./docs/en/guides/skills.md) | Skills system for reusable prompts |
145
146
  | [Providers](./docs/en/guides/providers.md) | Model provider configuration |
146
147
  | [Database](./docs/en/guides/database.md) | SQLite/PostgreSQL persistence |
147
148
  | [Resume & Fork](./docs/en/guides/resume-fork.md) | Crash recovery & branching |
149
+ | **Project** | |
150
+ | [Contribution Guide](./docs/en/contribution.md) | How to contribute |
148
151
  | **Reference** | |
149
152
  | [API Reference](./docs/en/reference/api.md) | Complete API documentation |
150
153
  | [Examples](./docs/en/examples/playbooks.md) | All examples explained |
package/README.zh-CN.md CHANGED
@@ -102,9 +102,12 @@ npm run example:room # 多Agent协作
102
102
  | **使用指南** | |
103
103
  | [事件系统](./docs/zh-CN/guides/events.md) | 三通道事件系统 |
104
104
  | [工具系统](./docs/zh-CN/guides/tools.md) | 内置工具与自定义工具 |
105
+ | [Skills 系统](./docs/zh-CN/guides/skills.md) | Skills 可复用提示词系统 |
105
106
  | [Provider 配置](./docs/zh-CN/guides/providers.md) | 模型 Provider 配置 |
106
107
  | [数据库存储](./docs/zh-CN/guides/database.md) | SQLite/PostgreSQL 持久化 |
107
108
  | [恢复与分叉](./docs/zh-CN/guides/resume-fork.md) | 崩溃恢复与分支 |
109
+ | **项目** | |
110
+ | [贡献指南](./docs/zh-CN/contribution.md) | 提交 PR 的要求与流程 |
108
111
  | **参考** | |
109
112
  | [API 参考](./docs/zh-CN/reference/api.md) | 完整 API 文档 |
110
113
  | [示例集](./docs/zh-CN/examples/playbooks.md) | 所有示例详解 |
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BreakpointManager = void 0;
4
+ class BreakpointManager {
5
+ constructor(onChange) {
6
+ this.onChange = onChange;
7
+ this.current = 'READY';
8
+ this.history = [];
9
+ }
10
+ getCurrent() {
11
+ return this.current;
12
+ }
13
+ getHistory() {
14
+ return this.history;
15
+ }
16
+ set(state, note) {
17
+ if (this.current === state)
18
+ return;
19
+ const entry = {
20
+ state,
21
+ timestamp: Date.now(),
22
+ note,
23
+ };
24
+ const previous = this.current;
25
+ this.current = state;
26
+ this.history.push(entry);
27
+ if (this.onChange) {
28
+ this.onChange(previous, state, entry);
29
+ }
30
+ }
31
+ reset(state = 'READY') {
32
+ this.current = state;
33
+ this.history = [];
34
+ }
35
+ }
36
+ exports.BreakpointManager = BreakpointManager;
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MessageQueue = void 0;
4
+ const logger_1 = require("../../utils/logger");
5
+ const errors_1 = require("../errors");
6
+ class MessageQueue {
7
+ constructor(options) {
8
+ this.options = options;
9
+ this.pending = [];
10
+ }
11
+ send(content, opts = {}) {
12
+ const kind = opts.kind ?? 'user';
13
+ const isText = typeof content === 'string';
14
+ if (kind === 'reminder' && !isText) {
15
+ throw new errors_1.MultimodalValidationError('Reminder messages must be plain text.');
16
+ }
17
+ const payload = isText
18
+ ? kind === 'reminder'
19
+ ? this.options.wrapReminder(content, opts.reminder)
20
+ : content
21
+ : content;
22
+ const id = `msg-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`;
23
+ this.pending.push({
24
+ message: {
25
+ role: 'user',
26
+ content: isText ? [{ type: 'text', text: payload }] : payload,
27
+ },
28
+ kind,
29
+ metadata: { id, ...(opts.metadata || {}) },
30
+ });
31
+ if (kind === 'user') {
32
+ this.options.ensureProcessing();
33
+ }
34
+ return id;
35
+ }
36
+ async flush() {
37
+ if (this.pending.length === 0)
38
+ return;
39
+ const queue = this.pending;
40
+ try {
41
+ // 先添加到消息历史
42
+ for (const entry of queue) {
43
+ this.options.addMessage(entry.message, entry.kind);
44
+ }
45
+ // 持久化成功后才清空队列
46
+ await this.options.persist();
47
+ // 成功:从队列中移除已处理的消息
48
+ this.pending = this.pending.filter(item => !queue.includes(item));
49
+ }
50
+ catch (err) {
51
+ // 失败:保留队列,下次重试
52
+ logger_1.logger.error('[MessageQueue] Flush failed, messages retained:', err);
53
+ throw err; // 重新抛出让调用者知道失败
54
+ }
55
+ }
56
+ }
57
+ exports.MessageQueue = MessageQueue;
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PermissionManager = void 0;
4
+ const permission_modes_1 = require("../permission-modes");
5
+ class PermissionManager {
6
+ constructor(config, descriptors) {
7
+ this.config = config;
8
+ this.descriptors = descriptors;
9
+ }
10
+ evaluate(toolName) {
11
+ if (this.config.denyTools?.includes(toolName)) {
12
+ return 'deny';
13
+ }
14
+ if (this.config.allowTools && this.config.allowTools.length > 0 && !this.config.allowTools.includes(toolName)) {
15
+ return 'deny';
16
+ }
17
+ if (this.config.requireApprovalTools?.includes(toolName)) {
18
+ return 'ask';
19
+ }
20
+ const handler = permission_modes_1.permissionModes.get(this.config.mode || 'auto') || permission_modes_1.permissionModes.get('auto');
21
+ if (!handler) {
22
+ return 'allow';
23
+ }
24
+ const context = {
25
+ toolName,
26
+ descriptor: this.descriptors.get(toolName),
27
+ config: this.config,
28
+ };
29
+ return handler(context);
30
+ }
31
+ }
32
+ exports.PermissionManager = PermissionManager;
@@ -0,0 +1,91 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TodoManager = void 0;
4
+ class TodoManager {
5
+ constructor(opts) {
6
+ this.opts = opts;
7
+ this.stepsSinceReminder = 0;
8
+ }
9
+ get enabled() {
10
+ return !!this.opts.service && !!this.opts.config?.enabled;
11
+ }
12
+ list() {
13
+ return this.opts.service ? this.opts.service.list() : [];
14
+ }
15
+ async setTodos(todos) {
16
+ if (!this.opts.service)
17
+ throw new Error('Todo service not enabled for this agent');
18
+ const prev = this.opts.service.list();
19
+ await this.opts.service.setTodos(todos);
20
+ this.publishChange(prev, this.opts.service.list());
21
+ }
22
+ async update(todo) {
23
+ if (!this.opts.service)
24
+ throw new Error('Todo service not enabled for this agent');
25
+ const prev = this.opts.service.list();
26
+ await this.opts.service.update(todo);
27
+ this.publishChange(prev, this.opts.service.list());
28
+ }
29
+ async remove(id) {
30
+ if (!this.opts.service)
31
+ throw new Error('Todo service not enabled for this agent');
32
+ const prev = this.opts.service.list();
33
+ await this.opts.service.delete(id);
34
+ this.publishChange(prev, this.opts.service.list());
35
+ }
36
+ handleStartup() {
37
+ if (!this.enabled || !this.opts.config?.reminderOnStart)
38
+ return;
39
+ const todos = this.list().filter((todo) => todo.status !== 'completed');
40
+ if (todos.length === 0) {
41
+ this.sendEmptyReminder();
42
+ }
43
+ else {
44
+ this.sendReminder(todos, 'startup');
45
+ }
46
+ }
47
+ onStep() {
48
+ if (!this.enabled)
49
+ return;
50
+ if (!this.opts.config?.remindIntervalSteps)
51
+ return;
52
+ if (this.opts.config.remindIntervalSteps <= 0)
53
+ return;
54
+ this.stepsSinceReminder += 1;
55
+ if (this.stepsSinceReminder < this.opts.config.remindIntervalSteps)
56
+ return;
57
+ const todos = this.list().filter((todo) => todo.status !== 'completed');
58
+ if (todos.length === 0)
59
+ return;
60
+ this.sendReminder(todos, 'interval');
61
+ }
62
+ publishChange(previous, current) {
63
+ if (!this.opts.events)
64
+ return;
65
+ this.stepsSinceReminder = 0;
66
+ this.opts.events.emitMonitor({ channel: 'monitor', type: 'todo_changed', previous, current });
67
+ if (current.length === 0) {
68
+ this.sendEmptyReminder();
69
+ }
70
+ }
71
+ sendReminder(todos, reason) {
72
+ this.stepsSinceReminder = 0;
73
+ this.opts.events.emitMonitor({ channel: 'monitor', type: 'todo_reminder', todos, reason });
74
+ this.opts.remind(this.formatTodoReminder(todos), { category: 'todo', priority: 'medium' });
75
+ }
76
+ sendEmptyReminder() {
77
+ this.opts.remind('当前 todo 列表为空,如需跟踪任务请使用 todo_write 建立清单。', {
78
+ category: 'todo',
79
+ priority: 'low',
80
+ });
81
+ }
82
+ formatTodoReminder(todos) {
83
+ const bulletList = todos
84
+ .slice(0, 10)
85
+ .map((todo, index) => `${index + 1}. [${todo.status}] ${todo.title}`)
86
+ .join('\n');
87
+ const more = todos.length > 10 ? `\n… 还有 ${todos.length - 10} 项` : '';
88
+ return `Todo 列表仍有未完成项:\n${bulletList}${more}\n请结合 todo_write 及时更新进度,不要向用户直接提及本提醒。`;
89
+ }
90
+ }
91
+ exports.TodoManager = TodoManager;
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ToolRunner = void 0;
4
+ class ToolRunner {
5
+ constructor(concurrency) {
6
+ this.concurrency = concurrency;
7
+ this.active = 0;
8
+ this.queue = [];
9
+ if (!Number.isFinite(concurrency) || concurrency <= 0) {
10
+ throw new Error('ToolRunner requires a positive concurrency limit');
11
+ }
12
+ }
13
+ run(task) {
14
+ return new Promise((resolve, reject) => {
15
+ const execute = () => {
16
+ this.active += 1;
17
+ task()
18
+ .then(resolve, reject)
19
+ .finally(() => {
20
+ this.active -= 1;
21
+ this.flush();
22
+ });
23
+ };
24
+ if (this.active < this.concurrency) {
25
+ execute();
26
+ }
27
+ else {
28
+ this.queue.push(execute);
29
+ }
30
+ });
31
+ }
32
+ clear() {
33
+ this.queue.length = 0;
34
+ }
35
+ flush() {
36
+ if (this.queue.length === 0)
37
+ return;
38
+ if (this.active >= this.concurrency)
39
+ return;
40
+ const next = this.queue.shift();
41
+ if (next)
42
+ next();
43
+ }
44
+ }
45
+ exports.ToolRunner = ToolRunner;
@@ -9,6 +9,7 @@ import { SandboxFactory } from '../infra/sandbox-factory';
9
9
  import { ModelProvider, ModelConfig } from '../infra/provider';
10
10
  import { ToolRegistry, ToolInstance, ToolDescriptor } from '../tools/registry';
11
11
  import { ContextManagerOptions } from './context-manager';
12
+ import { ResumeError } from './errors';
12
13
  import { SendOptions as QueueSendOptions } from './agent/message-queue';
13
14
  export interface ModelFactory {
14
15
  (config: ModelConfig): ModelProvider;
@@ -180,6 +181,12 @@ export declare class Agent {
180
181
  strategy?: ResumeStrategy;
181
182
  overrides?: Partial<AgentConfig>;
182
183
  }): Promise<Agent>;
184
+ static resumeOrCreate(agentId: string, config: AgentConfig, deps: AgentDependencies, opts?: {
185
+ autoRun?: boolean;
186
+ strategy?: ResumeStrategy;
187
+ overrides?: Partial<AgentConfig>;
188
+ onCorrupted?: (agentId: string, error: ResumeError) => Promise<void> | void;
189
+ }): Promise<Agent>;
183
190
  private ensureProcessing;
184
191
  private runStep;
185
192
  private executeTools;