@shareai-lab/kode-sdk 1.0.0-beta.8 → 2.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +115 -273
- package/README.zh-CN.md +114 -0
- package/dist/core/agent/breakpoint-manager.d.ts +16 -0
- package/dist/core/agent/breakpoint-manager.js +36 -0
- package/dist/core/agent/message-queue.d.ts +26 -0
- package/dist/core/agent/message-queue.js +57 -0
- package/dist/core/agent/permission-manager.d.ts +9 -0
- package/dist/core/agent/permission-manager.js +32 -0
- package/dist/core/agent/todo-manager.d.ts +26 -0
- package/dist/core/agent/todo-manager.js +91 -0
- package/dist/core/agent/tool-runner.d.ts +9 -0
- package/dist/core/agent/tool-runner.js +45 -0
- package/dist/core/agent.d.ts +228 -62
- package/dist/core/agent.js +1890 -615
- package/dist/core/config.d.ts +10 -0
- package/dist/core/config.js +2 -0
- package/dist/core/context-manager.d.ts +82 -0
- package/dist/core/context-manager.js +241 -0
- package/dist/core/errors.d.ts +22 -0
- package/dist/core/errors.js +49 -0
- package/dist/core/events.d.ts +41 -10
- package/dist/core/events.js +270 -68
- package/dist/core/file-pool.d.ts +41 -0
- package/dist/core/file-pool.js +102 -0
- package/dist/core/hooks.d.ts +3 -3
- package/dist/core/hooks.js +1 -1
- package/dist/core/permission-modes.d.ts +31 -0
- package/dist/core/permission-modes.js +61 -0
- package/dist/core/pool.d.ts +56 -13
- package/dist/core/pool.js +244 -34
- package/dist/core/room.d.ts +2 -2
- package/dist/core/room.js +10 -10
- package/dist/core/scheduler.d.ts +30 -23
- package/dist/core/scheduler.js +42 -168
- package/dist/core/skills/index.d.ts +10 -0
- package/dist/core/skills/index.js +20 -0
- package/dist/core/skills/management-manager.d.ts +130 -0
- package/dist/core/skills/management-manager.js +557 -0
- package/dist/core/skills/manager.d.ts +47 -0
- package/dist/core/skills/manager.js +243 -0
- package/dist/core/skills/operation-queue.d.ts +87 -0
- package/dist/core/skills/operation-queue.js +113 -0
- package/dist/core/skills/sandbox-file-manager.d.ts +82 -0
- package/dist/core/skills/sandbox-file-manager.js +183 -0
- package/dist/core/skills/types.d.ts +120 -0
- package/dist/core/skills/types.js +9 -0
- package/dist/core/skills/xml-generator.d.ts +13 -0
- package/dist/core/skills/xml-generator.js +70 -0
- package/dist/core/template.d.ts +57 -0
- package/dist/core/template.js +35 -0
- package/dist/core/time-bridge.d.ts +18 -0
- package/dist/core/time-bridge.js +100 -0
- package/dist/core/todo.d.ts +34 -0
- package/dist/core/todo.js +89 -0
- package/dist/core/types.d.ts +311 -114
- package/dist/core/types.js +1 -12
- package/dist/index.d.ts +47 -9
- package/dist/index.js +108 -15
- package/dist/infra/db/postgres/postgres-store.d.ts +97 -0
- package/dist/infra/db/postgres/postgres-store.js +1073 -0
- package/dist/infra/db/sqlite/sqlite-store.d.ts +84 -0
- package/dist/infra/db/sqlite/sqlite-store.js +800 -0
- package/dist/infra/e2b/e2b-fs.d.ts +29 -0
- package/dist/infra/e2b/e2b-fs.js +128 -0
- package/dist/infra/e2b/e2b-sandbox.d.ts +37 -0
- package/dist/infra/e2b/e2b-sandbox.js +156 -0
- package/dist/infra/e2b/e2b-template.d.ts +24 -0
- package/dist/infra/e2b/e2b-template.js +105 -0
- package/dist/infra/e2b/index.d.ts +4 -0
- package/dist/infra/e2b/index.js +9 -0
- package/dist/infra/e2b/types.d.ts +46 -0
- package/dist/infra/e2b/types.js +2 -0
- package/dist/infra/provider.d.ts +17 -58
- package/dist/infra/provider.js +65 -116
- package/dist/infra/providers/anthropic.d.ts +42 -0
- package/dist/infra/providers/anthropic.js +308 -0
- package/dist/infra/providers/core/errors.d.ts +230 -0
- package/dist/infra/providers/core/errors.js +353 -0
- package/dist/infra/providers/core/fork.d.ts +106 -0
- package/dist/infra/providers/core/fork.js +418 -0
- package/dist/infra/providers/core/index.d.ts +10 -0
- package/dist/infra/providers/core/index.js +76 -0
- package/dist/infra/providers/core/logger.d.ts +186 -0
- package/dist/infra/providers/core/logger.js +191 -0
- package/dist/infra/providers/core/retry.d.ts +62 -0
- package/dist/infra/providers/core/retry.js +189 -0
- package/dist/infra/providers/core/usage.d.ts +151 -0
- package/dist/infra/providers/core/usage.js +376 -0
- package/dist/infra/providers/gemini.d.ts +49 -0
- package/dist/infra/providers/gemini.js +493 -0
- package/dist/infra/providers/index.d.ts +25 -0
- package/dist/infra/providers/index.js +83 -0
- package/dist/infra/providers/openai.d.ts +123 -0
- package/dist/infra/providers/openai.js +662 -0
- package/dist/infra/providers/types.d.ts +334 -0
- package/dist/infra/providers/types.js +20 -0
- package/dist/infra/providers/utils.d.ts +53 -0
- package/dist/infra/providers/utils.js +400 -0
- package/dist/infra/sandbox-factory.d.ts +13 -0
- package/dist/infra/sandbox-factory.js +30 -0
- package/dist/infra/sandbox.d.ts +35 -6
- package/dist/infra/sandbox.js +174 -8
- package/dist/infra/store/factory.d.ts +45 -0
- package/dist/infra/store/factory.js +80 -0
- package/dist/infra/store/index.d.ts +3 -0
- package/dist/infra/store/index.js +26 -0
- package/dist/infra/store/json-store.d.ts +67 -0
- package/dist/infra/store/json-store.js +606 -0
- package/dist/infra/store/types.d.ts +342 -0
- package/dist/infra/store/types.js +2 -0
- package/dist/infra/store.d.ts +12 -32
- package/dist/infra/store.js +27 -130
- package/dist/tools/bash_kill/index.d.ts +1 -0
- package/dist/tools/bash_kill/index.js +35 -0
- package/dist/tools/bash_kill/prompt.d.ts +2 -0
- package/dist/tools/bash_kill/prompt.js +14 -0
- package/dist/tools/bash_logs/index.d.ts +1 -0
- package/dist/tools/bash_logs/index.js +40 -0
- package/dist/tools/bash_logs/prompt.d.ts +2 -0
- package/dist/tools/bash_logs/prompt.js +14 -0
- package/dist/tools/bash_run/index.d.ts +16 -0
- package/dist/tools/bash_run/index.js +61 -0
- package/dist/tools/bash_run/prompt.d.ts +2 -0
- package/dist/tools/bash_run/prompt.js +18 -0
- package/dist/tools/builtin.d.ts +7 -13
- package/dist/tools/builtin.js +19 -90
- package/dist/tools/define.d.ts +101 -0
- package/dist/tools/define.js +214 -0
- package/dist/tools/fs_edit/index.d.ts +1 -0
- package/dist/tools/fs_edit/index.js +62 -0
- package/dist/tools/fs_edit/prompt.d.ts +2 -0
- package/dist/tools/fs_edit/prompt.js +15 -0
- package/dist/tools/fs_glob/index.d.ts +1 -0
- package/dist/tools/fs_glob/index.js +40 -0
- package/dist/tools/fs_glob/prompt.d.ts +2 -0
- package/dist/tools/fs_glob/prompt.js +15 -0
- package/dist/tools/fs_grep/index.d.ts +1 -0
- package/dist/tools/fs_grep/index.js +66 -0
- package/dist/tools/fs_grep/prompt.d.ts +2 -0
- package/dist/tools/fs_grep/prompt.js +16 -0
- package/dist/tools/fs_multi_edit/index.d.ts +1 -0
- package/dist/tools/fs_multi_edit/index.js +106 -0
- package/dist/tools/fs_multi_edit/prompt.d.ts +2 -0
- package/dist/tools/fs_multi_edit/prompt.js +16 -0
- package/dist/tools/fs_read/index.d.ts +1 -0
- package/dist/tools/fs_read/index.js +40 -0
- package/dist/tools/fs_read/prompt.d.ts +2 -0
- package/dist/tools/fs_read/prompt.js +16 -0
- package/dist/tools/fs_write/index.d.ts +1 -0
- package/dist/tools/fs_write/index.js +40 -0
- package/dist/tools/fs_write/prompt.d.ts +2 -0
- package/dist/tools/fs_write/prompt.js +15 -0
- package/dist/tools/index.d.ts +11 -0
- package/dist/tools/index.js +61 -0
- package/dist/tools/mcp.d.ts +69 -0
- package/dist/tools/mcp.js +185 -0
- package/dist/tools/registry.d.ts +29 -0
- package/dist/tools/registry.js +26 -0
- package/dist/tools/scripts.d.ts +22 -0
- package/dist/tools/scripts.js +205 -0
- package/dist/tools/skills.d.ts +20 -0
- package/dist/tools/skills.js +115 -0
- package/dist/tools/task_run/index.d.ts +7 -0
- package/dist/tools/task_run/index.js +58 -0
- package/dist/tools/task_run/prompt.d.ts +5 -0
- package/dist/tools/task_run/prompt.js +25 -0
- package/dist/tools/todo_read/index.d.ts +1 -0
- package/dist/tools/todo_read/index.js +29 -0
- package/dist/tools/todo_read/prompt.d.ts +2 -0
- package/dist/tools/todo_read/prompt.js +18 -0
- package/dist/tools/todo_write/index.d.ts +1 -0
- package/dist/tools/todo_write/index.js +42 -0
- package/dist/tools/todo_write/prompt.d.ts +2 -0
- package/dist/tools/todo_write/prompt.js +23 -0
- package/dist/tools/tool.d.ts +43 -0
- package/dist/tools/tool.js +211 -0
- package/dist/tools/toolkit.d.ts +69 -0
- package/dist/tools/toolkit.js +98 -0
- package/dist/tools/type-inference.d.ts +127 -0
- package/dist/tools/type-inference.js +207 -0
- package/dist/utils/agent-id.d.ts +1 -0
- package/dist/utils/agent-id.js +28 -0
- package/dist/utils/logger.d.ts +15 -0
- package/dist/utils/logger.js +44 -0
- package/dist/utils/session-id.js +16 -16
- package/package.json +35 -11
- package/dist/tools/bash.d.ts +0 -63
- package/dist/tools/bash.js +0 -92
- package/dist/tools/fs.d.ts +0 -96
- package/dist/tools/fs.js +0 -100
- package/dist/tools/task.d.ts +0 -38
- package/dist/tools/task.js +0 -45
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
import { Message, Timeline, Snapshot, AgentInfo, ToolCallRecord, Bookmark, AgentChannel, ToolCallState, BreakpointState } from '../../core/types';
|
|
2
|
+
import { TodoSnapshot } from '../../core/todo';
|
|
3
|
+
export interface HistoryWindow {
|
|
4
|
+
id: string;
|
|
5
|
+
messages: Message[];
|
|
6
|
+
events: Timeline[];
|
|
7
|
+
stats: {
|
|
8
|
+
messageCount: number;
|
|
9
|
+
tokenCount: number;
|
|
10
|
+
eventCount: number;
|
|
11
|
+
};
|
|
12
|
+
timestamp: number;
|
|
13
|
+
}
|
|
14
|
+
export interface CompressionRecord {
|
|
15
|
+
id: string;
|
|
16
|
+
windowId: string;
|
|
17
|
+
config: {
|
|
18
|
+
model: string;
|
|
19
|
+
prompt: string;
|
|
20
|
+
threshold: number;
|
|
21
|
+
};
|
|
22
|
+
summary: string;
|
|
23
|
+
ratio: number;
|
|
24
|
+
recoveredFiles: string[];
|
|
25
|
+
timestamp: number;
|
|
26
|
+
}
|
|
27
|
+
export interface RecoveredFile {
|
|
28
|
+
path: string;
|
|
29
|
+
content: string;
|
|
30
|
+
mtime: number;
|
|
31
|
+
timestamp: number;
|
|
32
|
+
}
|
|
33
|
+
export interface MediaCacheRecord {
|
|
34
|
+
key: string;
|
|
35
|
+
provider: string;
|
|
36
|
+
mimeType: string;
|
|
37
|
+
sizeBytes: number;
|
|
38
|
+
fileId?: string;
|
|
39
|
+
fileUri?: string;
|
|
40
|
+
createdAt: number;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* 查询过滤器类型定义
|
|
44
|
+
*/
|
|
45
|
+
export interface SessionFilters {
|
|
46
|
+
agentId?: string;
|
|
47
|
+
templateId?: string;
|
|
48
|
+
userId?: string;
|
|
49
|
+
startDate?: number;
|
|
50
|
+
endDate?: number;
|
|
51
|
+
limit?: number;
|
|
52
|
+
offset?: number;
|
|
53
|
+
sortBy?: 'created_at' | 'updated_at' | 'message_count';
|
|
54
|
+
sortOrder?: 'asc' | 'desc';
|
|
55
|
+
}
|
|
56
|
+
export interface MessageFilters {
|
|
57
|
+
agentId?: string;
|
|
58
|
+
role?: 'user' | 'assistant' | 'system';
|
|
59
|
+
startDate?: number;
|
|
60
|
+
endDate?: number;
|
|
61
|
+
limit?: number;
|
|
62
|
+
offset?: number;
|
|
63
|
+
}
|
|
64
|
+
export interface ToolCallFilters {
|
|
65
|
+
agentId?: string;
|
|
66
|
+
toolName?: string;
|
|
67
|
+
state?: ToolCallState;
|
|
68
|
+
startDate?: number;
|
|
69
|
+
endDate?: number;
|
|
70
|
+
limit?: number;
|
|
71
|
+
offset?: number;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* 查询结果类型定义
|
|
75
|
+
*/
|
|
76
|
+
export interface SessionInfo {
|
|
77
|
+
agentId: string;
|
|
78
|
+
templateId: string;
|
|
79
|
+
createdAt: string;
|
|
80
|
+
messageCount: number;
|
|
81
|
+
lastSfpIndex: number;
|
|
82
|
+
breakpoint?: BreakpointState;
|
|
83
|
+
}
|
|
84
|
+
export interface AgentStats {
|
|
85
|
+
totalMessages: number;
|
|
86
|
+
totalToolCalls: number;
|
|
87
|
+
totalSnapshots: number;
|
|
88
|
+
avgMessagesPerSession?: number;
|
|
89
|
+
toolCallsByName?: Record<string, number>;
|
|
90
|
+
toolCallsByState?: Record<string, number>;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Store 接口定义 Agent 持久化的所有能力
|
|
94
|
+
*
|
|
95
|
+
* 设计原则:
|
|
96
|
+
* 1. 所有方法都是必需的,不使用可选方法
|
|
97
|
+
* 2. 职责清晰:运行时状态、历史管理、事件流、元数据管理
|
|
98
|
+
* 3. 实现无关:接口不暴露存储细节(如 WAL、文件格式等)
|
|
99
|
+
*/
|
|
100
|
+
export interface Store {
|
|
101
|
+
/** 保存对话消息 */
|
|
102
|
+
saveMessages(agentId: string, messages: Message[]): Promise<void>;
|
|
103
|
+
/** 加载对话消息 */
|
|
104
|
+
loadMessages(agentId: string): Promise<Message[]>;
|
|
105
|
+
/** 保存工具调用记录 */
|
|
106
|
+
saveToolCallRecords(agentId: string, records: ToolCallRecord[]): Promise<void>;
|
|
107
|
+
/** 加载工具调用记录 */
|
|
108
|
+
loadToolCallRecords(agentId: string): Promise<ToolCallRecord[]>;
|
|
109
|
+
/** 保存 Todo 快照 */
|
|
110
|
+
saveTodos(agentId: string, snapshot: TodoSnapshot): Promise<void>;
|
|
111
|
+
/** 加载 Todo 快照 */
|
|
112
|
+
loadTodos(agentId: string): Promise<TodoSnapshot | undefined>;
|
|
113
|
+
/** 追加事件到流中 */
|
|
114
|
+
appendEvent(agentId: string, timeline: Timeline): Promise<void>;
|
|
115
|
+
/** 读取事件流(支持 Bookmark 续读和 Channel 过滤) */
|
|
116
|
+
readEvents(agentId: string, opts?: {
|
|
117
|
+
since?: Bookmark;
|
|
118
|
+
channel?: AgentChannel;
|
|
119
|
+
}): AsyncIterable<Timeline>;
|
|
120
|
+
/** 保存历史窗口(压缩前的完整快照) */
|
|
121
|
+
saveHistoryWindow(agentId: string, window: HistoryWindow): Promise<void>;
|
|
122
|
+
/** 加载所有历史窗口 */
|
|
123
|
+
loadHistoryWindows(agentId: string): Promise<HistoryWindow[]>;
|
|
124
|
+
/** 保存压缩记录 */
|
|
125
|
+
saveCompressionRecord(agentId: string, record: CompressionRecord): Promise<void>;
|
|
126
|
+
/** 加载所有压缩记录 */
|
|
127
|
+
loadCompressionRecords(agentId: string): Promise<CompressionRecord[]>;
|
|
128
|
+
/** 保存恢复文件快照 */
|
|
129
|
+
saveRecoveredFile(agentId: string, file: RecoveredFile): Promise<void>;
|
|
130
|
+
/** 加载所有恢复文件 */
|
|
131
|
+
loadRecoveredFiles(agentId: string): Promise<RecoveredFile[]>;
|
|
132
|
+
/** 保存多模态缓存 */
|
|
133
|
+
saveMediaCache(agentId: string, records: MediaCacheRecord[]): Promise<void>;
|
|
134
|
+
/** 加载多模态缓存 */
|
|
135
|
+
loadMediaCache(agentId: string): Promise<MediaCacheRecord[]>;
|
|
136
|
+
/** 保存快照 */
|
|
137
|
+
saveSnapshot(agentId: string, snapshot: Snapshot): Promise<void>;
|
|
138
|
+
/** 加载指定快照 */
|
|
139
|
+
loadSnapshot(agentId: string, snapshotId: string): Promise<Snapshot | undefined>;
|
|
140
|
+
/** 列出所有快照 */
|
|
141
|
+
listSnapshots(agentId: string): Promise<Snapshot[]>;
|
|
142
|
+
/** 保存 Agent 元信息 */
|
|
143
|
+
saveInfo(agentId: string, info: AgentInfo): Promise<void>;
|
|
144
|
+
/** 加载 Agent 元信息 */
|
|
145
|
+
loadInfo(agentId: string): Promise<AgentInfo | undefined>;
|
|
146
|
+
/** 检查 Agent 是否存在 */
|
|
147
|
+
exists(agentId: string): Promise<boolean>;
|
|
148
|
+
/** 删除 Agent 所有数据 */
|
|
149
|
+
delete(agentId: string): Promise<void>;
|
|
150
|
+
/** 列出所有 Agent ID */
|
|
151
|
+
list(prefix?: string): Promise<string[]>;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* QueryableStore 接口
|
|
155
|
+
* 扩展 Store 接口,提供查询能力
|
|
156
|
+
*
|
|
157
|
+
* 设计原则:
|
|
158
|
+
* 1. 继承 Store 的所有能力
|
|
159
|
+
* 2. 新增查询和聚合统计方法
|
|
160
|
+
* 3. 支持灵活的过滤和分页
|
|
161
|
+
*/
|
|
162
|
+
export interface QueryableStore extends Store {
|
|
163
|
+
/**
|
|
164
|
+
* 查询 Agent 会话信息
|
|
165
|
+
* @param filters - 过滤条件
|
|
166
|
+
* @returns 符合条件的会话信息列表
|
|
167
|
+
*/
|
|
168
|
+
querySessions(filters: SessionFilters): Promise<SessionInfo[]>;
|
|
169
|
+
/**
|
|
170
|
+
* 查询消息
|
|
171
|
+
* @param filters - 过滤条件
|
|
172
|
+
* @returns 符合条件的消息列表
|
|
173
|
+
*/
|
|
174
|
+
queryMessages(filters: MessageFilters): Promise<Message[]>;
|
|
175
|
+
/**
|
|
176
|
+
* 查询工具调用记录
|
|
177
|
+
* @param filters - 过滤条件
|
|
178
|
+
* @returns 符合条件的工具调用记录列表
|
|
179
|
+
*/
|
|
180
|
+
queryToolCalls(filters: ToolCallFilters): Promise<ToolCallRecord[]>;
|
|
181
|
+
/**
|
|
182
|
+
* 聚合统计
|
|
183
|
+
* @param agentId - Agent ID
|
|
184
|
+
* @returns Agent 的统计信息
|
|
185
|
+
*/
|
|
186
|
+
aggregateStats(agentId: string): Promise<AgentStats>;
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* PostgreSQL 连接配置
|
|
190
|
+
*/
|
|
191
|
+
export interface PostgresConfig {
|
|
192
|
+
/** 数据库主机地址 */
|
|
193
|
+
host: string;
|
|
194
|
+
/** 数据库端口,默认 5432 */
|
|
195
|
+
port?: number;
|
|
196
|
+
/** 数据库名称 */
|
|
197
|
+
database: string;
|
|
198
|
+
/** 用户名 */
|
|
199
|
+
user: string;
|
|
200
|
+
/** 密码 */
|
|
201
|
+
password: string;
|
|
202
|
+
/** SSL 配置 */
|
|
203
|
+
ssl?: boolean | {
|
|
204
|
+
rejectUnauthorized?: boolean;
|
|
205
|
+
};
|
|
206
|
+
/** 连接池最大连接数,默认 10 */
|
|
207
|
+
max?: number;
|
|
208
|
+
/** 空闲连接超时(毫秒),默认 30000 */
|
|
209
|
+
idleTimeoutMillis?: number;
|
|
210
|
+
/** 连接超时(毫秒),默认 5000 */
|
|
211
|
+
connectionTimeoutMillis?: number;
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Store 健康检查状态
|
|
215
|
+
*/
|
|
216
|
+
export interface StoreHealthStatus {
|
|
217
|
+
/** 整体健康状态 */
|
|
218
|
+
healthy: boolean;
|
|
219
|
+
/** 数据库连接状态 */
|
|
220
|
+
database: {
|
|
221
|
+
connected: boolean;
|
|
222
|
+
latencyMs?: number;
|
|
223
|
+
};
|
|
224
|
+
/** 文件系统状态 */
|
|
225
|
+
fileSystem: {
|
|
226
|
+
writable: boolean;
|
|
227
|
+
};
|
|
228
|
+
/** 检查时间 */
|
|
229
|
+
checkedAt: number;
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* 一致性检查结果
|
|
233
|
+
*/
|
|
234
|
+
export interface ConsistencyCheckResult {
|
|
235
|
+
/** 是否一致 */
|
|
236
|
+
consistent: boolean;
|
|
237
|
+
/** 发现的问题列表 */
|
|
238
|
+
issues: string[];
|
|
239
|
+
/** 检查时间 */
|
|
240
|
+
checkedAt: number;
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Store 指标统计
|
|
244
|
+
*/
|
|
245
|
+
export interface StoreMetrics {
|
|
246
|
+
/** 操作计数 */
|
|
247
|
+
operations: {
|
|
248
|
+
saves: number;
|
|
249
|
+
loads: number;
|
|
250
|
+
queries: number;
|
|
251
|
+
deletes: number;
|
|
252
|
+
};
|
|
253
|
+
/** 性能指标 */
|
|
254
|
+
performance: {
|
|
255
|
+
avgLatencyMs: number;
|
|
256
|
+
maxLatencyMs: number;
|
|
257
|
+
minLatencyMs: number;
|
|
258
|
+
};
|
|
259
|
+
/** 存储统计 */
|
|
260
|
+
storage: {
|
|
261
|
+
totalAgents: number;
|
|
262
|
+
totalMessages: number;
|
|
263
|
+
totalToolCalls: number;
|
|
264
|
+
dbSizeBytes?: number;
|
|
265
|
+
};
|
|
266
|
+
/** 统计时间 */
|
|
267
|
+
collectedAt: number;
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* 分布式锁释放函数
|
|
271
|
+
*/
|
|
272
|
+
export type LockReleaseFn = () => Promise<void>;
|
|
273
|
+
/**
|
|
274
|
+
* JSON Store 配置
|
|
275
|
+
*/
|
|
276
|
+
export interface JSONStoreConfig {
|
|
277
|
+
type: 'json';
|
|
278
|
+
baseDir: string;
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* SQLite Store 配置
|
|
282
|
+
*/
|
|
283
|
+
export interface SqliteStoreConfig {
|
|
284
|
+
type: 'sqlite';
|
|
285
|
+
dbPath: string;
|
|
286
|
+
fileStoreBaseDir?: string;
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* PostgreSQL Store 配置
|
|
290
|
+
*/
|
|
291
|
+
export interface PostgresStoreConfig {
|
|
292
|
+
type: 'postgres';
|
|
293
|
+
connection: PostgresConfig;
|
|
294
|
+
fileStoreBaseDir: string;
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* Store 工厂配置联合类型
|
|
298
|
+
*/
|
|
299
|
+
export type StoreConfig = JSONStoreConfig | SqliteStoreConfig | PostgresStoreConfig;
|
|
300
|
+
/**
|
|
301
|
+
* ExtendedStore 接口
|
|
302
|
+
* 扩展 QueryableStore,提供健康检查、一致性检查、分布式锁等高级功能
|
|
303
|
+
*/
|
|
304
|
+
export interface ExtendedStore extends QueryableStore {
|
|
305
|
+
/**
|
|
306
|
+
* 健康检查
|
|
307
|
+
* @returns 健康状态
|
|
308
|
+
*/
|
|
309
|
+
healthCheck(): Promise<StoreHealthStatus>;
|
|
310
|
+
/**
|
|
311
|
+
* 一致性检查
|
|
312
|
+
* 检查数据库和文件系统之间的数据一致性
|
|
313
|
+
* @param agentId - Agent ID
|
|
314
|
+
* @returns 一致性检查结果
|
|
315
|
+
*/
|
|
316
|
+
checkConsistency(agentId: string): Promise<ConsistencyCheckResult>;
|
|
317
|
+
/**
|
|
318
|
+
* 获取指标统计
|
|
319
|
+
* @returns 指标统计
|
|
320
|
+
*/
|
|
321
|
+
getMetrics(): Promise<StoreMetrics>;
|
|
322
|
+
/**
|
|
323
|
+
* 获取分布式锁
|
|
324
|
+
* 用于多 Worker 场景下保护 Agent 操作
|
|
325
|
+
* @param agentId - Agent ID
|
|
326
|
+
* @param timeoutMs - 锁超时时间(毫秒),默认 30000
|
|
327
|
+
* @returns 锁释放函数
|
|
328
|
+
*/
|
|
329
|
+
acquireAgentLock(agentId: string, timeoutMs?: number): Promise<LockReleaseFn>;
|
|
330
|
+
/**
|
|
331
|
+
* 批量 Fork Agent
|
|
332
|
+
* 优化大量 Fork 场景的性能
|
|
333
|
+
* @param agentId - 源 Agent ID
|
|
334
|
+
* @param count - Fork 数量
|
|
335
|
+
* @returns 新创建的 Agent ID 列表
|
|
336
|
+
*/
|
|
337
|
+
batchFork(agentId: string, count: number): Promise<string[]>;
|
|
338
|
+
/**
|
|
339
|
+
* 关闭连接
|
|
340
|
+
*/
|
|
341
|
+
close(): Promise<void>;
|
|
342
|
+
}
|
package/dist/infra/store.d.ts
CHANGED
|
@@ -1,32 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
delete(sessionId: string): Promise<void>;
|
|
14
|
-
list(prefix?: string): Promise<string[]>;
|
|
15
|
-
}
|
|
16
|
-
export declare class JSONStore implements Store {
|
|
17
|
-
private baseDir;
|
|
18
|
-
constructor(baseDir: string);
|
|
19
|
-
private getPath;
|
|
20
|
-
saveMessages(sessionId: string, messages: Message[]): Promise<void>;
|
|
21
|
-
loadMessages(sessionId: string): Promise<Message[]>;
|
|
22
|
-
appendEvent(sessionId: string, timeline: Timeline): Promise<void>;
|
|
23
|
-
readEvents(sessionId: string, since?: number): AsyncIterable<Timeline>;
|
|
24
|
-
saveSnapshot(sessionId: string, snapshot: Snapshot): Promise<void>;
|
|
25
|
-
loadSnapshot(sessionId: string, snapshotId: string): Promise<Snapshot | undefined>;
|
|
26
|
-
listSnapshots(sessionId: string): Promise<Snapshot[]>;
|
|
27
|
-
saveInfo(sessionId: string, info: AgentInfo): Promise<void>;
|
|
28
|
-
loadInfo(sessionId: string): Promise<AgentInfo | undefined>;
|
|
29
|
-
exists(sessionId: string): Promise<boolean>;
|
|
30
|
-
delete(sessionId: string): Promise<void>;
|
|
31
|
-
list(prefix?: string): Promise<string[]>;
|
|
32
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Store 模块 - Agent 持久化
|
|
3
|
+
*
|
|
4
|
+
* 本文件已重构为模块化结构,实际实现已拆分到 store/ 目录下:
|
|
5
|
+
* - store/types.ts - 接口和类型定义
|
|
6
|
+
* - store/json-store.ts - JSONStore 文件存储实现
|
|
7
|
+
* - db/sqlite/ - SQLite 数据库实现
|
|
8
|
+
* - db/postgres/ - PostgreSQL 数据库实现
|
|
9
|
+
*
|
|
10
|
+
* 本文件仅做向后兼容的导出
|
|
11
|
+
*/
|
|
12
|
+
export * from './store/index';
|
package/dist/infra/store.js
CHANGED
|
@@ -1,132 +1,29 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Store 模块 - Agent 持久化
|
|
4
|
+
*
|
|
5
|
+
* 本文件已重构为模块化结构,实际实现已拆分到 store/ 目录下:
|
|
6
|
+
* - store/types.ts - 接口和类型定义
|
|
7
|
+
* - store/json-store.ts - JSONStore 文件存储实现
|
|
8
|
+
* - db/sqlite/ - SQLite 数据库实现
|
|
9
|
+
* - db/postgres/ - PostgreSQL 数据库实现
|
|
10
|
+
*
|
|
11
|
+
* 本文件仅做向后兼容的导出
|
|
12
|
+
*/
|
|
13
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
14
|
+
if (k2 === undefined) k2 = k;
|
|
15
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
16
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
17
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
18
|
+
}
|
|
19
|
+
Object.defineProperty(o, k2, desc);
|
|
20
|
+
}) : (function(o, m, k, k2) {
|
|
21
|
+
if (k2 === undefined) k2 = k;
|
|
22
|
+
o[k2] = m[k];
|
|
23
|
+
}));
|
|
24
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
25
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
26
|
+
};
|
|
2
27
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
constructor(baseDir) {
|
|
6
|
-
this.baseDir = baseDir;
|
|
7
|
-
}
|
|
8
|
-
getPath(sessionId, file) {
|
|
9
|
-
const fs = require('fs');
|
|
10
|
-
const path = require('path');
|
|
11
|
-
const dir = path.join(this.baseDir, sessionId);
|
|
12
|
-
if (!fs.existsSync(dir)) {
|
|
13
|
-
fs.mkdirSync(dir, { recursive: true });
|
|
14
|
-
}
|
|
15
|
-
return path.join(dir, file);
|
|
16
|
-
}
|
|
17
|
-
async saveMessages(sessionId, messages) {
|
|
18
|
-
const fs = require('fs').promises;
|
|
19
|
-
await fs.writeFile(this.getPath(sessionId, 'messages.json'), JSON.stringify(messages, null, 2));
|
|
20
|
-
}
|
|
21
|
-
async loadMessages(sessionId) {
|
|
22
|
-
const fs = require('fs').promises;
|
|
23
|
-
try {
|
|
24
|
-
const data = await fs.readFile(this.getPath(sessionId, 'messages.json'), 'utf-8');
|
|
25
|
-
return JSON.parse(data);
|
|
26
|
-
}
|
|
27
|
-
catch {
|
|
28
|
-
return [];
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
async appendEvent(sessionId, timeline) {
|
|
32
|
-
const fs = require('fs').promises;
|
|
33
|
-
const path = this.getPath(sessionId, 'events.jsonl');
|
|
34
|
-
await fs.appendFile(path, JSON.stringify(timeline) + '\n');
|
|
35
|
-
}
|
|
36
|
-
async *readEvents(sessionId, since) {
|
|
37
|
-
const fs = require('fs').promises;
|
|
38
|
-
try {
|
|
39
|
-
const data = await fs.readFile(this.getPath(sessionId, 'events.jsonl'), 'utf-8');
|
|
40
|
-
const events = data
|
|
41
|
-
.trim()
|
|
42
|
-
.split('\n')
|
|
43
|
-
.filter((line) => line)
|
|
44
|
-
.map((line) => JSON.parse(line));
|
|
45
|
-
const filtered = since !== undefined ? events.filter((e) => e.cursor >= since) : events;
|
|
46
|
-
for (const event of filtered) {
|
|
47
|
-
yield event;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
catch {
|
|
51
|
-
// No events file, return empty
|
|
52
|
-
return;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
async saveSnapshot(sessionId, snapshot) {
|
|
56
|
-
const fs = require('fs').promises;
|
|
57
|
-
const path = this.getPath(sessionId, `snapshot-${snapshot.id}.json`);
|
|
58
|
-
await fs.writeFile(path, JSON.stringify(snapshot, null, 2));
|
|
59
|
-
}
|
|
60
|
-
async loadSnapshot(sessionId, snapshotId) {
|
|
61
|
-
const fs = require('fs').promises;
|
|
62
|
-
try {
|
|
63
|
-
const data = await fs.readFile(this.getPath(sessionId, `snapshot-${snapshotId}.json`), 'utf-8');
|
|
64
|
-
return JSON.parse(data);
|
|
65
|
-
}
|
|
66
|
-
catch {
|
|
67
|
-
return undefined;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
async listSnapshots(sessionId) {
|
|
71
|
-
const fs = require('fs').promises;
|
|
72
|
-
const path = require('path');
|
|
73
|
-
try {
|
|
74
|
-
const dir = path.join(this.baseDir, sessionId);
|
|
75
|
-
const files = await fs.readdir(dir);
|
|
76
|
-
const snapshots = [];
|
|
77
|
-
for (const file of files) {
|
|
78
|
-
if (file.startsWith('snapshot-') && file.endsWith('.json')) {
|
|
79
|
-
const data = await fs.readFile(path.join(dir, file), 'utf-8');
|
|
80
|
-
snapshots.push(JSON.parse(data));
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
return snapshots;
|
|
84
|
-
}
|
|
85
|
-
catch {
|
|
86
|
-
return [];
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
async saveInfo(sessionId, info) {
|
|
90
|
-
const fs = require('fs').promises;
|
|
91
|
-
await fs.writeFile(this.getPath(sessionId, 'info.json'), JSON.stringify(info, null, 2));
|
|
92
|
-
}
|
|
93
|
-
async loadInfo(sessionId) {
|
|
94
|
-
const fs = require('fs').promises;
|
|
95
|
-
try {
|
|
96
|
-
const data = await fs.readFile(this.getPath(sessionId, 'info.json'), 'utf-8');
|
|
97
|
-
return JSON.parse(data);
|
|
98
|
-
}
|
|
99
|
-
catch {
|
|
100
|
-
return undefined;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
async exists(sessionId) {
|
|
104
|
-
const fs = require('fs').promises;
|
|
105
|
-
const path = require('path');
|
|
106
|
-
try {
|
|
107
|
-
await fs.access(path.join(this.baseDir, sessionId));
|
|
108
|
-
return true;
|
|
109
|
-
}
|
|
110
|
-
catch {
|
|
111
|
-
return false;
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
async delete(sessionId) {
|
|
115
|
-
const fs = require('fs').promises;
|
|
116
|
-
const path = require('path');
|
|
117
|
-
const dir = path.join(this.baseDir, sessionId);
|
|
118
|
-
await fs.rm(dir, { recursive: true, force: true });
|
|
119
|
-
}
|
|
120
|
-
async list(prefix) {
|
|
121
|
-
const fs = require('fs').promises;
|
|
122
|
-
const path = require('path');
|
|
123
|
-
try {
|
|
124
|
-
const dirs = await fs.readdir(this.baseDir);
|
|
125
|
-
return prefix ? dirs.filter((d) => d.startsWith(prefix)) : dirs;
|
|
126
|
-
}
|
|
127
|
-
catch {
|
|
128
|
-
return [];
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
exports.JSONStore = JSONStore;
|
|
28
|
+
// 重导出所有内容以保持向后兼容
|
|
29
|
+
__exportStar(require("./store/index"), exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const BashKill: import("..").ToolInstance;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BashKill = void 0;
|
|
4
|
+
const tool_1 = require("../tool");
|
|
5
|
+
const zod_1 = require("zod");
|
|
6
|
+
const prompt_1 = require("./prompt");
|
|
7
|
+
const bash_run_1 = require("../bash_run");
|
|
8
|
+
exports.BashKill = (0, tool_1.tool)({
|
|
9
|
+
name: 'bash_kill',
|
|
10
|
+
description: prompt_1.DESCRIPTION,
|
|
11
|
+
parameters: zod_1.z.object({
|
|
12
|
+
shell_id: zod_1.z.string().describe('Shell ID from bash_run'),
|
|
13
|
+
}),
|
|
14
|
+
async execute(args) {
|
|
15
|
+
const { shell_id } = args;
|
|
16
|
+
const proc = bash_run_1.processes.get(shell_id);
|
|
17
|
+
if (!proc) {
|
|
18
|
+
return {
|
|
19
|
+
ok: false,
|
|
20
|
+
error: `Shell not found: ${shell_id}`,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
bash_run_1.processes.delete(shell_id);
|
|
24
|
+
return {
|
|
25
|
+
ok: true,
|
|
26
|
+
shell_id,
|
|
27
|
+
message: `Killed shell ${shell_id}`,
|
|
28
|
+
};
|
|
29
|
+
},
|
|
30
|
+
metadata: {
|
|
31
|
+
readonly: false,
|
|
32
|
+
version: '1.0',
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
exports.BashKill.prompt = prompt_1.PROMPT;
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export declare const DESCRIPTION = "Kill a background bash shell";
|
|
2
|
+
export declare const PROMPT = "Terminate a long-running background bash session identified by shell_id.\n\nGuidelines:\n- Use this to clean up stuck processes.\n- Provide the shell_id from bash_run to terminate that specific process.\n- Once killed, the process cannot be restarted or accessed.\n\nSafety/Limitations:\n- Only background processes started in the current session can be killed.\n- Force termination may leave incomplete work or locks.";
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PROMPT = exports.DESCRIPTION = void 0;
|
|
4
|
+
exports.DESCRIPTION = 'Kill a background bash shell';
|
|
5
|
+
exports.PROMPT = `Terminate a long-running background bash session identified by shell_id.
|
|
6
|
+
|
|
7
|
+
Guidelines:
|
|
8
|
+
- Use this to clean up stuck processes.
|
|
9
|
+
- Provide the shell_id from bash_run to terminate that specific process.
|
|
10
|
+
- Once killed, the process cannot be restarted or accessed.
|
|
11
|
+
|
|
12
|
+
Safety/Limitations:
|
|
13
|
+
- Only background processes started in the current session can be killed.
|
|
14
|
+
- Force termination may leave incomplete work or locks.`;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const BashLogs: import("..").ToolInstance;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BashLogs = void 0;
|
|
4
|
+
const tool_1 = require("../tool");
|
|
5
|
+
const zod_1 = require("zod");
|
|
6
|
+
const prompt_1 = require("./prompt");
|
|
7
|
+
const bash_run_1 = require("../bash_run");
|
|
8
|
+
exports.BashLogs = (0, tool_1.tool)({
|
|
9
|
+
name: 'bash_logs',
|
|
10
|
+
description: prompt_1.DESCRIPTION,
|
|
11
|
+
parameters: zod_1.z.object({
|
|
12
|
+
shell_id: zod_1.z.string().describe('Shell ID from bash_run'),
|
|
13
|
+
}),
|
|
14
|
+
async execute(args) {
|
|
15
|
+
const { shell_id } = args;
|
|
16
|
+
const proc = bash_run_1.processes.get(shell_id);
|
|
17
|
+
if (!proc) {
|
|
18
|
+
return {
|
|
19
|
+
ok: false,
|
|
20
|
+
error: `Shell not found: ${shell_id}`,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
const isRunning = proc.code === undefined;
|
|
24
|
+
const status = isRunning ? 'running' : `completed (exit code ${proc.code})`;
|
|
25
|
+
const output = [proc.stdout, proc.stderr].filter(Boolean).join('\n').trim();
|
|
26
|
+
return {
|
|
27
|
+
ok: true,
|
|
28
|
+
shell_id,
|
|
29
|
+
status,
|
|
30
|
+
running: isRunning,
|
|
31
|
+
code: proc.code,
|
|
32
|
+
output: output || '(no output yet)',
|
|
33
|
+
};
|
|
34
|
+
},
|
|
35
|
+
metadata: {
|
|
36
|
+
readonly: true,
|
|
37
|
+
version: '1.0',
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
exports.BashLogs.prompt = prompt_1.PROMPT;
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export declare const DESCRIPTION = "Get output from a background bash shell";
|
|
2
|
+
export declare const PROMPT = "Fetch stdout/stderr from a background bash session started via bash_run with \"background\": true.\n\nGuidelines:\n- Provide the shell_id returned by bash_run to retrieve incremental logs.\n- Check the status to see if the process is still running or completed.\n- Output includes both stdout and stderr streams.\n\nSafety/Limitations:\n- Only processes started in the current session are accessible.\n- Process history is not persisted across SDK restarts.";
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PROMPT = exports.DESCRIPTION = void 0;
|
|
4
|
+
exports.DESCRIPTION = 'Get output from a background bash shell';
|
|
5
|
+
exports.PROMPT = `Fetch stdout/stderr from a background bash session started via bash_run with "background": true.
|
|
6
|
+
|
|
7
|
+
Guidelines:
|
|
8
|
+
- Provide the shell_id returned by bash_run to retrieve incremental logs.
|
|
9
|
+
- Check the status to see if the process is still running or completed.
|
|
10
|
+
- Output includes both stdout and stderr streams.
|
|
11
|
+
|
|
12
|
+
Safety/Limitations:
|
|
13
|
+
- Only processes started in the current session are accessible.
|
|
14
|
+
- Process history is not persisted across SDK restarts.`;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
interface BashProcess {
|
|
2
|
+
id: string;
|
|
3
|
+
cmd: string;
|
|
4
|
+
startTime: number;
|
|
5
|
+
promise: Promise<{
|
|
6
|
+
code: number;
|
|
7
|
+
stdout: string;
|
|
8
|
+
stderr: string;
|
|
9
|
+
}>;
|
|
10
|
+
stdout: string;
|
|
11
|
+
stderr: string;
|
|
12
|
+
code?: number;
|
|
13
|
+
}
|
|
14
|
+
declare const processes: Map<string, BashProcess>;
|
|
15
|
+
export declare const BashRun: import("..").ToolInstance;
|
|
16
|
+
export { processes };
|