@shareai-lab/kode-sdk 1.0.0-beta.9 → 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,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,9 @@
|
|
|
1
|
+
import { PermissionConfig } from '../template';
|
|
2
|
+
import { ToolDescriptor } from '../../tools/registry';
|
|
3
|
+
import { PermissionDecision } from '../permission-modes';
|
|
4
|
+
export declare class PermissionManager {
|
|
5
|
+
private readonly config;
|
|
6
|
+
private readonly descriptors;
|
|
7
|
+
constructor(config: PermissionConfig, descriptors: Map<string, ToolDescriptor>);
|
|
8
|
+
evaluate(toolName: string): PermissionDecision;
|
|
9
|
+
}
|
|
@@ -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,26 @@
|
|
|
1
|
+
import { TodoService, TodoInput, TodoItem } from '../todo';
|
|
2
|
+
import { TodoConfig } from '../template';
|
|
3
|
+
import { ReminderOptions } from '../types';
|
|
4
|
+
import { EventBus } from '../events';
|
|
5
|
+
export interface TodoManagerOptions {
|
|
6
|
+
service?: TodoService;
|
|
7
|
+
config?: TodoConfig;
|
|
8
|
+
events: EventBus;
|
|
9
|
+
remind: (content: string, options?: ReminderOptions) => void;
|
|
10
|
+
}
|
|
11
|
+
export declare class TodoManager {
|
|
12
|
+
private readonly opts;
|
|
13
|
+
private stepsSinceReminder;
|
|
14
|
+
constructor(opts: TodoManagerOptions);
|
|
15
|
+
get enabled(): boolean;
|
|
16
|
+
list(): TodoItem[];
|
|
17
|
+
setTodos(todos: TodoInput[]): Promise<void>;
|
|
18
|
+
update(todo: TodoInput): Promise<void>;
|
|
19
|
+
remove(id: string): Promise<void>;
|
|
20
|
+
handleStartup(): void;
|
|
21
|
+
onStep(): void;
|
|
22
|
+
private publishChange;
|
|
23
|
+
private sendReminder;
|
|
24
|
+
private sendEmptyReminder;
|
|
25
|
+
private formatTodoReminder;
|
|
26
|
+
}
|
|
@@ -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;
|
package/dist/core/agent.d.ts
CHANGED
|
@@ -1,85 +1,251 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { AgentEvent, AgentEventEnvelope, AgentInfo, AgentStatus, Bookmark, ContentBlock, ControlEvent, MonitorEvent, ProgressEvent, ReminderOptions, ResumeStrategy, SnapshotId } from './types';
|
|
2
2
|
import { Hooks } from './hooks';
|
|
3
3
|
import { Scheduler } from './scheduler';
|
|
4
|
-
import {
|
|
4
|
+
import { TodoInput, TodoItem } from './todo';
|
|
5
|
+
import { AgentTemplateRegistry, AgentTemplateDefinition, PermissionConfig, SubAgentConfig, TodoConfig } from './template';
|
|
5
6
|
import { Store } from '../infra/store';
|
|
6
|
-
import { Sandbox } from '../infra/sandbox';
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
7
|
+
import { Sandbox, SandboxKind } from '../infra/sandbox';
|
|
8
|
+
import { SandboxFactory } from '../infra/sandbox-factory';
|
|
9
|
+
import { ModelProvider, ModelConfig } from '../infra/provider';
|
|
10
|
+
import { ToolRegistry, ToolInstance, ToolDescriptor } from '../tools/registry';
|
|
11
|
+
import { ContextManagerOptions } from './context-manager';
|
|
12
|
+
import { SendOptions as QueueSendOptions } from './agent/message-queue';
|
|
13
|
+
export interface ModelFactory {
|
|
14
|
+
(config: ModelConfig): ModelProvider;
|
|
15
|
+
}
|
|
16
|
+
export interface AgentDependencies {
|
|
12
17
|
store: Store;
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
templateRegistry: AgentTemplateRegistry;
|
|
19
|
+
sandboxFactory: SandboxFactory;
|
|
20
|
+
toolRegistry: ToolRegistry;
|
|
21
|
+
modelFactory?: ModelFactory;
|
|
22
|
+
skillsManager?: import('./skills/manager').SkillsManager;
|
|
23
|
+
}
|
|
24
|
+
export type SendOptions = QueueSendOptions;
|
|
25
|
+
export interface SandboxConfig {
|
|
26
|
+
kind: SandboxKind;
|
|
27
|
+
workDir?: string;
|
|
28
|
+
enforceBoundary?: boolean;
|
|
29
|
+
allowPaths?: string[];
|
|
30
|
+
watchFiles?: boolean;
|
|
31
|
+
[key: string]: any;
|
|
32
|
+
}
|
|
33
|
+
export interface AgentConfig {
|
|
34
|
+
agentId?: string;
|
|
35
|
+
templateId: string;
|
|
36
|
+
templateVersion?: string;
|
|
37
|
+
model?: ModelProvider;
|
|
38
|
+
modelConfig?: ModelConfig;
|
|
39
|
+
sandbox?: Sandbox | SandboxConfig;
|
|
40
|
+
tools?: string[];
|
|
41
|
+
exposeThinking?: boolean;
|
|
42
|
+
retainThinking?: boolean;
|
|
43
|
+
multimodalContinuation?: 'history';
|
|
44
|
+
multimodalRetention?: {
|
|
45
|
+
keepRecent?: number;
|
|
46
|
+
};
|
|
47
|
+
overrides?: {
|
|
48
|
+
permission?: PermissionConfig;
|
|
49
|
+
todo?: TodoConfig;
|
|
50
|
+
subagents?: SubAgentConfig;
|
|
51
|
+
hooks?: Hooks;
|
|
52
|
+
};
|
|
53
|
+
context?: ContextManagerOptions;
|
|
54
|
+
metadata?: Record<string, any>;
|
|
55
|
+
}
|
|
56
|
+
interface SubAgentRuntime {
|
|
57
|
+
depthRemaining: number;
|
|
58
|
+
}
|
|
59
|
+
export interface CompleteResult {
|
|
60
|
+
status: 'ok' | 'paused';
|
|
61
|
+
text?: string;
|
|
62
|
+
last?: Bookmark;
|
|
63
|
+
permissionIds?: string[];
|
|
64
|
+
}
|
|
65
|
+
export interface StreamOptions {
|
|
66
|
+
since?: Bookmark;
|
|
67
|
+
kinds?: Array<ProgressEvent['type']>;
|
|
68
|
+
}
|
|
69
|
+
export interface SubscribeOptions {
|
|
70
|
+
since?: Bookmark;
|
|
71
|
+
kinds?: Array<AgentEvent['type']>;
|
|
20
72
|
}
|
|
21
73
|
export declare class Agent {
|
|
22
|
-
private
|
|
23
|
-
private
|
|
24
|
-
private
|
|
25
|
-
private
|
|
26
|
-
private
|
|
27
|
-
private
|
|
28
|
-
private
|
|
29
|
-
private
|
|
30
|
-
private
|
|
31
|
-
private
|
|
74
|
+
private readonly config;
|
|
75
|
+
private readonly deps;
|
|
76
|
+
private readonly events;
|
|
77
|
+
private readonly hooks;
|
|
78
|
+
private readonly scheduler;
|
|
79
|
+
private readonly todoService?;
|
|
80
|
+
private readonly contextManager;
|
|
81
|
+
private readonly filePool;
|
|
82
|
+
private readonly breakpoints;
|
|
83
|
+
private readonly permissions;
|
|
84
|
+
private readonly model;
|
|
85
|
+
private readonly sandbox;
|
|
86
|
+
private readonly sandboxConfig?;
|
|
87
|
+
private readonly todoConfig?;
|
|
88
|
+
private readonly messageQueue;
|
|
89
|
+
private readonly todoManager;
|
|
90
|
+
private readonly ajv;
|
|
91
|
+
private readonly validatorCache;
|
|
92
|
+
private readonly toolControllers;
|
|
93
|
+
private readonly toolTimeoutMs;
|
|
94
|
+
private readonly maxToolConcurrency;
|
|
95
|
+
private readonly tools;
|
|
96
|
+
private readonly toolDescriptors;
|
|
97
|
+
private readonly toolDescriptorIndex;
|
|
98
|
+
private skillsManager?;
|
|
99
|
+
private createdAt;
|
|
100
|
+
private readonly pendingPermissions;
|
|
101
|
+
private readonly toolRunner;
|
|
32
102
|
private messages;
|
|
33
103
|
private state;
|
|
34
|
-
private
|
|
35
|
-
private stepCount;
|
|
36
|
-
private events;
|
|
37
|
-
private hooks;
|
|
38
|
-
private scheduler?;
|
|
39
|
-
private pendingPermissions;
|
|
104
|
+
private toolRecords;
|
|
40
105
|
private interrupted;
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
106
|
+
private processingPromise;
|
|
107
|
+
private pendingNextRound;
|
|
108
|
+
private lastProcessingStart;
|
|
109
|
+
private readonly PROCESSING_TIMEOUT;
|
|
110
|
+
private stepCount;
|
|
111
|
+
private lastSfpIndex;
|
|
112
|
+
private lastBookmark?;
|
|
113
|
+
private exposeThinking;
|
|
114
|
+
private retainThinking;
|
|
115
|
+
private multimodalContinuation;
|
|
116
|
+
private multimodalRetentionKeepRecent;
|
|
117
|
+
private permission;
|
|
118
|
+
private subagents?;
|
|
119
|
+
private template;
|
|
120
|
+
private lineage;
|
|
121
|
+
private mediaCache;
|
|
122
|
+
private get persistentStore();
|
|
123
|
+
private static requireStore;
|
|
124
|
+
constructor(config: AgentConfig, deps: AgentDependencies, runtime: {
|
|
125
|
+
template: AgentTemplateDefinition;
|
|
126
|
+
model: ModelProvider;
|
|
127
|
+
sandbox: Sandbox;
|
|
128
|
+
sandboxConfig?: SandboxConfig;
|
|
129
|
+
tools: ToolInstance[];
|
|
130
|
+
toolDescriptors: ToolDescriptor[];
|
|
131
|
+
permission: PermissionConfig;
|
|
132
|
+
todoConfig?: TodoConfig;
|
|
133
|
+
subagents?: SubAgentConfig;
|
|
134
|
+
context?: ContextManagerOptions;
|
|
135
|
+
});
|
|
136
|
+
get agentId(): string;
|
|
137
|
+
static create(config: AgentConfig, deps: AgentDependencies): Promise<Agent>;
|
|
138
|
+
private initialize;
|
|
139
|
+
chatStream(input: string | ContentBlock[], opts?: StreamOptions): AsyncIterable<AgentEventEnvelope<ProgressEvent>>;
|
|
140
|
+
chat(input: string | ContentBlock[], opts?: StreamOptions): Promise<CompleteResult>;
|
|
141
|
+
complete(input: string | ContentBlock[], opts?: StreamOptions): Promise<CompleteResult>;
|
|
142
|
+
stream(input: string | ContentBlock[], opts?: StreamOptions): AsyncIterable<AgentEventEnvelope<ProgressEvent>>;
|
|
143
|
+
send(message: string | ContentBlock[], options?: SendOptions): Promise<string>;
|
|
144
|
+
schedule(): Scheduler;
|
|
145
|
+
on<T extends ControlEvent['type'] | MonitorEvent['type']>(event: T, handler: (evt: any) => void): () => void;
|
|
146
|
+
subscribe(channels?: Array<'progress' | 'control' | 'monitor'>, opts?: SubscribeOptions): AsyncIterable<AgentEventEnvelope<AgentEvent>>;
|
|
147
|
+
getTodos(): TodoItem[];
|
|
148
|
+
setTodos(todos: TodoInput[]): Promise<void>;
|
|
149
|
+
updateTodo(todo: TodoInput): Promise<void>;
|
|
150
|
+
deleteTodo(id: string): Promise<void>;
|
|
151
|
+
decide(permissionId: string, decision: 'allow' | 'deny', note?: string): Promise<void>;
|
|
55
152
|
interrupt(opts?: {
|
|
56
153
|
note?: string;
|
|
57
154
|
}): Promise<void>;
|
|
58
|
-
decide(permId: string, decision: 'allow' | 'deny', note?: string): Promise<void>;
|
|
59
155
|
snapshot(label?: string): Promise<SnapshotId>;
|
|
60
156
|
fork(sel?: SnapshotId | {
|
|
61
157
|
at?: string;
|
|
62
158
|
}): Promise<Agent>;
|
|
63
|
-
static resume(sessionId: string, opts: AgentOptions & {
|
|
64
|
-
autoRun?: boolean;
|
|
65
|
-
strategy?: 'crash' | 'manual';
|
|
66
|
-
}): Promise<Agent>;
|
|
67
|
-
private findLastSfp;
|
|
68
|
-
private findSealedTools;
|
|
69
|
-
history(opts?: {
|
|
70
|
-
since?: number;
|
|
71
|
-
limit?: number;
|
|
72
|
-
}): Promise<AgentEvent[]>;
|
|
73
159
|
status(): Promise<AgentStatus>;
|
|
74
160
|
info(): Promise<AgentInfo>;
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
161
|
+
private setBreakpoint;
|
|
162
|
+
remind(content: string, options?: ReminderOptions): void;
|
|
163
|
+
spawnSubAgent(templateId: string, prompt: string, runtime?: SubAgentRuntime): Promise<CompleteResult>;
|
|
164
|
+
/**
|
|
165
|
+
* Create and run a sub-agent with a task, without requiring subagents config.
|
|
166
|
+
* This is useful for tools that want to delegate work to specialized agents.
|
|
167
|
+
*/
|
|
168
|
+
delegateTask(config: {
|
|
169
|
+
templateId: string;
|
|
170
|
+
prompt: string;
|
|
171
|
+
model?: string;
|
|
172
|
+
tools?: string[];
|
|
173
|
+
}): Promise<CompleteResult>;
|
|
174
|
+
static resume(agentId: string, config: AgentConfig, deps: AgentDependencies, opts?: {
|
|
175
|
+
autoRun?: boolean;
|
|
176
|
+
strategy?: ResumeStrategy;
|
|
177
|
+
}): Promise<Agent>;
|
|
178
|
+
static resumeFromStore(agentId: string, deps: AgentDependencies, opts?: {
|
|
179
|
+
autoRun?: boolean;
|
|
180
|
+
strategy?: ResumeStrategy;
|
|
181
|
+
overrides?: Partial<AgentConfig>;
|
|
182
|
+
}): Promise<Agent>;
|
|
183
|
+
private ensureProcessing;
|
|
184
|
+
private runStep;
|
|
81
185
|
private executeTools;
|
|
186
|
+
private processToolCall;
|
|
187
|
+
private registerToolRecord;
|
|
188
|
+
private updateToolRecord;
|
|
189
|
+
private snapshotToolRecord;
|
|
190
|
+
private normalizeToolRecord;
|
|
191
|
+
private preview;
|
|
192
|
+
private validateBlocks;
|
|
193
|
+
private estimateBase64Bytes;
|
|
194
|
+
private resolveMultimodalBlocks;
|
|
195
|
+
private applyMediaCache;
|
|
196
|
+
private computeSha256;
|
|
197
|
+
private persistMediaCache;
|
|
198
|
+
private splitThinkBlocksIfNeeded;
|
|
199
|
+
private splitThinkText;
|
|
200
|
+
private shouldPreserveBlocks;
|
|
201
|
+
private buildMessageMetadata;
|
|
82
202
|
private requestPermission;
|
|
203
|
+
private findLastSfp;
|
|
204
|
+
private appendSyntheticToolResults;
|
|
205
|
+
private autoSealIncompleteCalls;
|
|
206
|
+
private validateToolArgs;
|
|
207
|
+
private makeToolResult;
|
|
208
|
+
private buildSealPayload;
|
|
209
|
+
private wrapReminder;
|
|
83
210
|
private getToolSchemas;
|
|
211
|
+
private setState;
|
|
84
212
|
private persistMessages;
|
|
213
|
+
private persistToolRecords;
|
|
214
|
+
private persistInfo;
|
|
215
|
+
private registerTodoTools;
|
|
216
|
+
/**
|
|
217
|
+
* 收集所有工具的使用说明
|
|
218
|
+
*/
|
|
219
|
+
private collectToolPrompts;
|
|
220
|
+
/**
|
|
221
|
+
* 渲染工具手册
|
|
222
|
+
*/
|
|
223
|
+
private renderManual;
|
|
224
|
+
/**
|
|
225
|
+
* 刷新工具手册(运行时工具变更时调用)
|
|
226
|
+
*/
|
|
227
|
+
private refreshToolManual;
|
|
228
|
+
/**
|
|
229
|
+
* 根据错误类型生成建议
|
|
230
|
+
*/
|
|
231
|
+
private getErrorRecommendations;
|
|
232
|
+
/**
|
|
233
|
+
* 将工具手册注入到系统提示中
|
|
234
|
+
*/
|
|
235
|
+
private injectManualIntoSystemPrompt;
|
|
236
|
+
/**
|
|
237
|
+
* 将skills元数据注入到系统提示中
|
|
238
|
+
* 参考openskills设计,使用<available_skills> XML格式
|
|
239
|
+
*/
|
|
240
|
+
private injectSkillsMetadataIntoSystemPrompt;
|
|
241
|
+
/**
|
|
242
|
+
* 刷新skills元数据(运行时skills变更时调用)
|
|
243
|
+
* 由于支持热更新,可在执行过程中调用此方法
|
|
244
|
+
*/
|
|
245
|
+
private refreshSkillsMetadata;
|
|
246
|
+
private enqueueMessage;
|
|
247
|
+
private handleExternalFileChange;
|
|
248
|
+
private relativePath;
|
|
249
|
+
private static generateAgentId;
|
|
85
250
|
}
|
|
251
|
+
export {};
|