pi-web-ui 0.105.1 → 0.106.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/CHANGELOG.md +43 -1
- package/README.md +193 -797
- package/README.zh-CN.md +186 -625
- package/README.zh-TW.md +781 -0
- package/bin/frp-manager.mjs +1488 -0
- package/bin/pi-web-ui.mjs +27 -0
- package/deploy/nginx-subpath.conf +16 -1
- package/dist/server/agent-service-daemon.js +34 -0
- package/dist/server/agent-service.js +227 -44
- package/dist/server/claim-files-tool.js +8 -1
- package/dist/server/client-state.js +136 -30
- package/dist/server/conversation-read-tool.js +12 -4
- package/dist/server/delegate-task.js +9 -1
- package/dist/server/dispatch-domain-handlers.js +15 -0
- package/dist/server/dsh/dsh-agent-service.js +838 -241
- package/dist/server/dsh/dsh-client.js +59 -18
- package/dist/server/dsh/dsh-market-service.js +233 -0
- package/dist/server/dsh/dsh-serialize.js +11 -3
- package/dist/server/dsh/dsh-sessions.js +64 -2
- package/dist/server/dsh/preset-clones.js +12 -1
- package/dist/server/dsh/runtime/goal-rpc.mjs +183 -45
- package/dist/server/engine-capabilities.js +72 -0
- package/dist/server/engine-probe.js +243 -0
- package/dist/server/goal-service.js +23 -12
- package/dist/server/i18n.js +20 -1
- package/dist/server/index.js +171 -66
- package/dist/server/locales.js +1 -0
- package/dist/server/mcp-agent-tool.js +10 -7
- package/dist/server/mcp-bridge.js +428 -24
- package/dist/server/model-admin.js +172 -74
- package/dist/server/model-resolver.js +221 -0
- package/dist/server/plugin-installer.js +4 -2
- package/dist/server/plugins.js +71 -11
- package/dist/server/present-files-tool.js +10 -2
- package/dist/server/preset-fields.js +14 -0
- package/dist/server/preset-share.js +2 -0
- package/dist/server/protocol-version.js +1 -1
- package/dist/server/schedule-agent-tool.js +2 -1
- package/dist/server/scm-commitmsg.js +3 -2
- package/dist/server/settings-service.js +10 -2
- package/dist/server/skill-discovery.js +161 -0
- package/dist/server/skill-tool.js +2 -1
- package/dist/server/slash-commands.js +154 -73
- package/dist/server/subagent-templates-hant.js +215 -0
- package/dist/server/subagent-templates.js +49 -2
- package/dist/server/subagents.js +22 -12
- package/dist/server/terminals.js +2 -2
- package/dist/server/tool-manager.js +3 -0
- package/dist/server/universal/driver-base.js +56 -0
- package/dist/server/universal/drivers/claude-code.js +462 -0
- package/dist/server/universal/drivers/dsh-driver.js +376 -0
- package/dist/server/universal/drivers/mock-driver.js +103 -0
- package/dist/server/universal/index.js +10 -0
- package/dist/server/universal/session-store.js +100 -0
- package/dist/server/universal/universal-attachments.js +175 -0
- package/dist/server/universal/universal-service.js +149 -0
- package/dist/server/universal/universal-session.js +2610 -0
- package/dist/server/universal/universal-types.js +4 -0
- package/dist/server/update-check.js +4 -1
- package/dist/server/worktree-service.js +1 -1
- package/dist/server/worktree-tool.js +9 -7
- package/package.json +3 -2
- package/plugin-sdk/index.d.ts +12 -2
- package/plugins/catalog.json +39 -5
- package/themes/dark-teal.css +20 -23
- package/themes/md-preview.css +1 -2
- package/themes/wallpaper-obsidian.css +693 -0
- package/web/dist/assets/{TerminalPanel-Mk4laCAL.js → TerminalPanel-CfxKLDW9.js} +2 -2
- package/web/dist/assets/index-BtcP6BDk.css +1 -0
- package/web/dist/assets/index-XVtCzcHR.js +690 -0
- package/web/dist/index.html +2 -2
- package/web/dist/sw.js +1 -1
- package/web/dist/assets/index-BDLVjBeK.js +0 -690
- package/web/dist/assets/index-BQC6lADz.css +0 -1
|
@@ -0,0 +1,376 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-driver.ts — DeepSeek Harness (DSH) 引擎驱动实现(基于 Universal Agent 底座)。
|
|
3
|
+
*
|
|
4
|
+
* 通过 stdio JSON-RPC 与 DSH 运行时子进程通信,将 DSH 的事件流、思考过程、工具调用与
|
|
5
|
+
* 统计信息完整映射到 Universal 底座的 AgentEventSink。
|
|
6
|
+
*
|
|
7
|
+
* 特性:
|
|
8
|
+
* 1. 原生流式 Thinking 与正文增量(reasoning-delta / text-delta);
|
|
9
|
+
* 2. 工具调用生命周期(tool/call → onToolStart, tool/result → onToolEnd);
|
|
10
|
+
* 3. 自动计算 Token 用量与缓存命中率(normalizeDshUsage);
|
|
11
|
+
* 4. 人在回路问卷提问桥(question.pending ↔ onAskUser);
|
|
12
|
+
* 5. 全量接入公共模型底座(models.json 自定义端点与多密钥动态注入);
|
|
13
|
+
* 6. 强杀中断与自愈恢复(abort → runtime.kill)。
|
|
14
|
+
*/
|
|
15
|
+
import { randomUUID } from "node:crypto";
|
|
16
|
+
import { homedir } from "node:os";
|
|
17
|
+
import { join } from "node:path";
|
|
18
|
+
import { BaseAgentDriver } from "../driver-base.js";
|
|
19
|
+
import { DshRuntime } from "../../dsh/dsh-client.js";
|
|
20
|
+
import { normalizeDshUsage } from "../../dsh/dsh-usage.js";
|
|
21
|
+
import { listCustomModelsFromConfig, resolveProviderCredential } from "../../model-resolver.js";
|
|
22
|
+
import { discoverDiskSkills } from "../../skill-discovery.js";
|
|
23
|
+
const DEFAULT_MODEL = "deepseek-v4-flash";
|
|
24
|
+
const DSH_OFFICIAL_MODELS = [
|
|
25
|
+
{ id: "deepseek-v4-flash", name: "DeepSeek V4 Flash", provider: "deepseek", vision: false },
|
|
26
|
+
{ id: "deepseek-v4-pro", name: "DeepSeek V4 Pro", provider: "deepseek", vision: false },
|
|
27
|
+
{
|
|
28
|
+
id: "deepseek-v4-flash-vision-exp",
|
|
29
|
+
name: "DeepSeek V4 Flash Vision (exp)",
|
|
30
|
+
provider: "deepseek",
|
|
31
|
+
vision: true,
|
|
32
|
+
},
|
|
33
|
+
];
|
|
34
|
+
export class DshAgentDriver extends BaseAgentDriver {
|
|
35
|
+
id = "dsh";
|
|
36
|
+
name = "DeepSeek Harness";
|
|
37
|
+
capabilities = {
|
|
38
|
+
supportsTreeFork: false,
|
|
39
|
+
supportsHistoryEdit: false,
|
|
40
|
+
supportsToolPromptOverride: false,
|
|
41
|
+
supportsModelSwitch: true,
|
|
42
|
+
supportsThinkingLevels: true,
|
|
43
|
+
supportsAskUser: true,
|
|
44
|
+
};
|
|
45
|
+
agentDir;
|
|
46
|
+
dataDir;
|
|
47
|
+
sessionRoot;
|
|
48
|
+
currentModel;
|
|
49
|
+
currentProvider;
|
|
50
|
+
activeSessionId = null;
|
|
51
|
+
runtime;
|
|
52
|
+
activeSink = null;
|
|
53
|
+
turnCompleteEmitted = false;
|
|
54
|
+
constructor(cwd, opts) {
|
|
55
|
+
super(cwd);
|
|
56
|
+
this.agentDir = opts?.agentDir || process.env.PI_CODING_AGENT_DIR || join(homedir(), ".pi", "agent");
|
|
57
|
+
this.dataDir = opts?.dataDir || process.env.PI_WEB_DATA_DIR || join(homedir(), ".pi-web");
|
|
58
|
+
this.sessionRoot = opts?.sessionRoot;
|
|
59
|
+
this.currentModel = opts?.defaultModel ?? DEFAULT_MODEL;
|
|
60
|
+
this.currentProvider = opts?.defaultProvider ?? "deepseek";
|
|
61
|
+
if (opts?.runtime) {
|
|
62
|
+
this.runtime = opts.runtime;
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
this.runtime = new DshRuntime({
|
|
66
|
+
cwd: this.cwd,
|
|
67
|
+
provider: "deepseek-official",
|
|
68
|
+
model: this.currentModel,
|
|
69
|
+
modelProvider: this.currentProvider,
|
|
70
|
+
sessionRoot: this.sessionRoot,
|
|
71
|
+
dataDir: this.dataDir,
|
|
72
|
+
agentDir: this.agentDir,
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
this.setupNotificationHandler();
|
|
76
|
+
}
|
|
77
|
+
async createSession() {
|
|
78
|
+
const sessionId = `web-${randomUUID().slice(0, 12)}`;
|
|
79
|
+
this.activeSessionId = sessionId;
|
|
80
|
+
return sessionId;
|
|
81
|
+
}
|
|
82
|
+
async resumeSession(sessionId) {
|
|
83
|
+
this.activeSessionId = sessionId;
|
|
84
|
+
}
|
|
85
|
+
getActiveSessionId() {
|
|
86
|
+
return this.activeSessionId;
|
|
87
|
+
}
|
|
88
|
+
/** 绑定运行时通知事件分发到 Universal 底座的 sink */
|
|
89
|
+
setupNotificationHandler() {
|
|
90
|
+
this.runtime.onNotification((method, params) => {
|
|
91
|
+
const sink = this.activeSink;
|
|
92
|
+
if (!sink || typeof params !== "object" || params === null)
|
|
93
|
+
return;
|
|
94
|
+
const p = params;
|
|
95
|
+
if (method === "session.event") {
|
|
96
|
+
const event = p.event;
|
|
97
|
+
if (!event)
|
|
98
|
+
return;
|
|
99
|
+
const type = String(event.type ?? "");
|
|
100
|
+
const data = (event.data ?? {});
|
|
101
|
+
switch (type) {
|
|
102
|
+
case "assistant/chunk": {
|
|
103
|
+
const chunk = (data.chunk ?? {});
|
|
104
|
+
const chunkType = String(chunk.type ?? "");
|
|
105
|
+
if (chunkType === "reasoning-delta" && chunk.text) {
|
|
106
|
+
sink.onThinkingDelta(String(chunk.text));
|
|
107
|
+
}
|
|
108
|
+
else if (chunkType === "text-delta" && chunk.text) {
|
|
109
|
+
sink.onTextDelta(String(chunk.text));
|
|
110
|
+
}
|
|
111
|
+
break;
|
|
112
|
+
}
|
|
113
|
+
case "tool/call": {
|
|
114
|
+
const callId = String(data.id ?? `call_${Date.now()}`);
|
|
115
|
+
const toolName = String(data.name ?? "unknown");
|
|
116
|
+
const args = (data.args ?? {});
|
|
117
|
+
sink.onToolStart(callId, toolName, args);
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
case "tool/result": {
|
|
121
|
+
const message = (data.message ?? {});
|
|
122
|
+
const contentList = Array.isArray(message.content) ? message.content : [];
|
|
123
|
+
const tr = contentList.find((b) => b?.type === "tool-result");
|
|
124
|
+
const callId = String(tr?.toolCallId ?? data.id ?? "");
|
|
125
|
+
const isError = Boolean(tr?.isError ?? data.isError);
|
|
126
|
+
const rawText = Array.isArray(tr?.content)
|
|
127
|
+
? tr.content.map((c) => c?.text ?? "").join("\n")
|
|
128
|
+
: String(data.result ?? "");
|
|
129
|
+
sink.onToolEnd(callId, {
|
|
130
|
+
output: isError ? undefined : rawText,
|
|
131
|
+
error: isError ? rawText : undefined,
|
|
132
|
+
});
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
case "assistant/message": {
|
|
136
|
+
const message = (data.message ?? {});
|
|
137
|
+
const usage = message.usage ? normalizeDshUsage(message.usage) : null;
|
|
138
|
+
const meta = {
|
|
139
|
+
model: this.currentModel,
|
|
140
|
+
usage: usage
|
|
141
|
+
? {
|
|
142
|
+
inputTokens: usage.input,
|
|
143
|
+
outputTokens: usage.output,
|
|
144
|
+
cacheReadTokens: usage.cacheRead,
|
|
145
|
+
cacheWriteTokens: usage.cacheWrite,
|
|
146
|
+
}
|
|
147
|
+
: undefined,
|
|
148
|
+
};
|
|
149
|
+
if (!this.turnCompleteEmitted) {
|
|
150
|
+
this.turnCompleteEmitted = true;
|
|
151
|
+
sink.onTurnComplete(meta);
|
|
152
|
+
}
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
else if (method === "question.pending") {
|
|
158
|
+
if (sink.onAskUser && p.id && typeof p.question === "string") {
|
|
159
|
+
const qId = String(p.id);
|
|
160
|
+
const qText = String(p.question);
|
|
161
|
+
const options = Array.isArray(p.options)
|
|
162
|
+
? p.options
|
|
163
|
+
: undefined;
|
|
164
|
+
sink
|
|
165
|
+
.onAskUser(qId, qText, options)
|
|
166
|
+
.then((answers) => {
|
|
167
|
+
return this.runtime.answerQuestion(qId, [
|
|
168
|
+
{ id: qId, selected: answers.selected, custom: answers.custom },
|
|
169
|
+
]);
|
|
170
|
+
})
|
|
171
|
+
.catch(() => {
|
|
172
|
+
return this.runtime.answerQuestion(qId, [], true);
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
else if (method === "session.status") {
|
|
177
|
+
if (p.status === "idle" && !this.turnCompleteEmitted) {
|
|
178
|
+
this.turnCompleteEmitted = true;
|
|
179
|
+
sink.onTurnComplete({ model: this.currentModel });
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
async sendPrompt(prompt, attachments, sink) {
|
|
185
|
+
this.activeSink = sink;
|
|
186
|
+
this.turnCompleteEmitted = false;
|
|
187
|
+
if (!this.activeSessionId) {
|
|
188
|
+
await this.createSession();
|
|
189
|
+
}
|
|
190
|
+
const sessionId = this.activeSessionId;
|
|
191
|
+
await this.runtime.start();
|
|
192
|
+
// 构建 contentBlocks
|
|
193
|
+
const contentBlocks = [];
|
|
194
|
+
// 处理图片和文件附件
|
|
195
|
+
if (attachments && attachments.length > 0) {
|
|
196
|
+
for (const att of attachments) {
|
|
197
|
+
if (att.imageData) {
|
|
198
|
+
try {
|
|
199
|
+
const name = att.name || "image.png";
|
|
200
|
+
const mime = name.endsWith(".png")
|
|
201
|
+
? "image/png"
|
|
202
|
+
: name.endsWith(".gif")
|
|
203
|
+
? "image/gif"
|
|
204
|
+
: name.endsWith(".webp")
|
|
205
|
+
? "image/webp"
|
|
206
|
+
: "image/jpeg";
|
|
207
|
+
const res = await this.runtime.attachmentSave(mime, att.imageData, name);
|
|
208
|
+
if (res.ref) {
|
|
209
|
+
contentBlocks.push({ type: "image", attachmentRef: res.ref });
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
catch {
|
|
213
|
+
/* 回落内联 */
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
else if (att.path) {
|
|
217
|
+
contentBlocks.push({
|
|
218
|
+
type: "text",
|
|
219
|
+
text: `[Attachment: ${att.name || att.path}]\n${att.path}`,
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
contentBlocks.push({ type: "text", text: prompt });
|
|
225
|
+
let turnDoneResolve;
|
|
226
|
+
const turnPromise = new Promise((resolve) => {
|
|
227
|
+
turnDoneResolve = resolve;
|
|
228
|
+
});
|
|
229
|
+
// 代理 sink 以获知完成时点
|
|
230
|
+
const wrappedSink = {
|
|
231
|
+
onTextDelta: (d) => sink.onTextDelta(d),
|
|
232
|
+
onThinkingDelta: (d) => sink.onThinkingDelta(d),
|
|
233
|
+
onToolStart: (id, name, args) => sink.onToolStart(id, name, args),
|
|
234
|
+
onToolUpdate: (id, out) => sink.onToolUpdate?.(id, out),
|
|
235
|
+
onToolEnd: (id, res) => sink.onToolEnd(id, res),
|
|
236
|
+
onAskUser: sink.onAskUser ? (id, q, opts) => sink.onAskUser(id, q, opts) : undefined,
|
|
237
|
+
onTurnComplete: (meta) => {
|
|
238
|
+
sink.onTurnComplete(meta);
|
|
239
|
+
turnDoneResolve();
|
|
240
|
+
},
|
|
241
|
+
onError: (err) => {
|
|
242
|
+
sink.onError(err);
|
|
243
|
+
turnDoneResolve();
|
|
244
|
+
},
|
|
245
|
+
};
|
|
246
|
+
this.activeSink = wrappedSink;
|
|
247
|
+
try {
|
|
248
|
+
await this.runtime.prompt(sessionId, contentBlocks);
|
|
249
|
+
await turnPromise;
|
|
250
|
+
}
|
|
251
|
+
catch (err) {
|
|
252
|
+
wrappedSink.onError(err instanceof Error ? err : new Error(String(err)));
|
|
253
|
+
}
|
|
254
|
+
finally {
|
|
255
|
+
this.activeSink = null;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
async abort() {
|
|
259
|
+
await this.runtime.kill();
|
|
260
|
+
}
|
|
261
|
+
async listModels() {
|
|
262
|
+
const official = DSH_OFFICIAL_MODELS.map((m) => ({
|
|
263
|
+
id: `deepseek/${m.id}`,
|
|
264
|
+
name: m.name,
|
|
265
|
+
}));
|
|
266
|
+
let dynamic = [];
|
|
267
|
+
try {
|
|
268
|
+
if (this.runtime.alive) {
|
|
269
|
+
const res = await this.runtime.listModels();
|
|
270
|
+
dynamic = (res.models ?? []).map((m) => ({
|
|
271
|
+
id: m.id.includes("/") ? m.id : `deepseek/${m.id}`,
|
|
272
|
+
name: m.name || m.id,
|
|
273
|
+
}));
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
catch {
|
|
277
|
+
/* ignore */
|
|
278
|
+
}
|
|
279
|
+
let custom = [];
|
|
280
|
+
try {
|
|
281
|
+
const customList = listCustomModelsFromConfig(this.agentDir);
|
|
282
|
+
custom = customList.map((c) => ({
|
|
283
|
+
id: `${c.providerId}/${c.modelId}`,
|
|
284
|
+
name: c.name,
|
|
285
|
+
}));
|
|
286
|
+
}
|
|
287
|
+
catch {
|
|
288
|
+
/* ignore */
|
|
289
|
+
}
|
|
290
|
+
const seen = new Set();
|
|
291
|
+
const result = [];
|
|
292
|
+
for (const m of [...official, ...dynamic, ...custom]) {
|
|
293
|
+
if (!seen.has(m.id)) {
|
|
294
|
+
seen.add(m.id);
|
|
295
|
+
result.push(m);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
return result;
|
|
299
|
+
}
|
|
300
|
+
async setModel(modelId) {
|
|
301
|
+
const normId = modelId.trim();
|
|
302
|
+
const hasSlash = normId.includes("/");
|
|
303
|
+
const targetProvider = hasSlash ? normId.slice(0, normId.indexOf("/")) : "deepseek";
|
|
304
|
+
const bareModelId = hasSlash ? normId.slice(normId.indexOf("/") + 1) : normId;
|
|
305
|
+
this.currentModel = bareModelId;
|
|
306
|
+
this.currentProvider = targetProvider;
|
|
307
|
+
await this.runtime.restart(bareModelId, targetProvider);
|
|
308
|
+
}
|
|
309
|
+
async getSlashCommands() {
|
|
310
|
+
return [
|
|
311
|
+
{
|
|
312
|
+
name: "skills",
|
|
313
|
+
description: "查看 DeepSeek Harness 运行时与磁盘技能列表",
|
|
314
|
+
descriptionEn: "List DeepSeek Harness runtime and disk skills",
|
|
315
|
+
source: "extension",
|
|
316
|
+
},
|
|
317
|
+
{
|
|
318
|
+
name: "clear",
|
|
319
|
+
description: "重置 DSH 会话上下文",
|
|
320
|
+
descriptionEn: "Reset DSH session context",
|
|
321
|
+
source: "extension",
|
|
322
|
+
},
|
|
323
|
+
];
|
|
324
|
+
}
|
|
325
|
+
async execSlashCommand(name, _args, context) {
|
|
326
|
+
switch (name) {
|
|
327
|
+
case "skills": {
|
|
328
|
+
const skills = await this.getSkills();
|
|
329
|
+
const summary = skills.length > 0
|
|
330
|
+
? `DSH 可用技能 (${skills.length}):${skills.map((s) => s.name).join(", ")}`
|
|
331
|
+
: "DSH 当前无可用技能";
|
|
332
|
+
const summaryEn = skills.length > 0
|
|
333
|
+
? `DSH skills (${skills.length}): ${skills.map((s) => s.name).join(", ")}`
|
|
334
|
+
: "No DSH skills available";
|
|
335
|
+
context?.emitNotice?.("info", summary, summaryEn);
|
|
336
|
+
return true;
|
|
337
|
+
}
|
|
338
|
+
case "clear": {
|
|
339
|
+
await this.createSession();
|
|
340
|
+
context?.emitNotice?.("info", "DSH 会话上下文已重置", "DSH session context reset");
|
|
341
|
+
return true;
|
|
342
|
+
}
|
|
343
|
+
default:
|
|
344
|
+
return false;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
async getSkills() {
|
|
348
|
+
const diskSkills = discoverDiskSkills({ cwd: this.cwd, agentDir: this.agentDir });
|
|
349
|
+
const out = diskSkills.map((s) => ({
|
|
350
|
+
name: s.name,
|
|
351
|
+
description: s.description,
|
|
352
|
+
filePath: s.filePath,
|
|
353
|
+
}));
|
|
354
|
+
try {
|
|
355
|
+
if (this.runtime.alive) {
|
|
356
|
+
const rpcSkills = await this.runtime.listSkills();
|
|
357
|
+
for (const s of rpcSkills.skills ?? []) {
|
|
358
|
+
if (!out.some((x) => x.name === s.name)) {
|
|
359
|
+
out.push({
|
|
360
|
+
name: s.name,
|
|
361
|
+
description: s.description,
|
|
362
|
+
filePath: "",
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
catch {
|
|
369
|
+
/* ignore runtime error */
|
|
370
|
+
}
|
|
371
|
+
return out;
|
|
372
|
+
}
|
|
373
|
+
dispose() {
|
|
374
|
+
void this.runtime.close();
|
|
375
|
+
}
|
|
376
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* mock-driver.ts — 用于单元测试与驱动开发的 Mock 驱动。
|
|
3
|
+
* 演示了如何以极简代码向 AgentEventSink 发射思考流、正文流和工具调用。
|
|
4
|
+
*/
|
|
5
|
+
import { BaseAgentDriver } from "../driver-base.js";
|
|
6
|
+
export class MockAgentDriver extends BaseAgentDriver {
|
|
7
|
+
id = "mock";
|
|
8
|
+
name = "Mock Agent";
|
|
9
|
+
capabilities = {
|
|
10
|
+
supportsTreeFork: false,
|
|
11
|
+
supportsHistoryEdit: false,
|
|
12
|
+
supportsToolPromptOverride: false,
|
|
13
|
+
supportsModelSwitch: true,
|
|
14
|
+
supportsThinkingLevels: true,
|
|
15
|
+
};
|
|
16
|
+
aborted = false;
|
|
17
|
+
sessionCounter = 0;
|
|
18
|
+
async createSession() {
|
|
19
|
+
return `mock-session-${++this.sessionCounter}`;
|
|
20
|
+
}
|
|
21
|
+
async resumeSession(_sessionId) {
|
|
22
|
+
/* no-op in mock */
|
|
23
|
+
}
|
|
24
|
+
async sendPrompt(prompt, _attachments, sink) {
|
|
25
|
+
this.aborted = false;
|
|
26
|
+
// 1. 发射思考过程
|
|
27
|
+
sink.onThinkingDelta("Thinking about how to handle: ");
|
|
28
|
+
if (this.aborted)
|
|
29
|
+
return;
|
|
30
|
+
sink.onThinkingDelta(prompt);
|
|
31
|
+
if (this.aborted)
|
|
32
|
+
return;
|
|
33
|
+
// 2. 发射工具调用(如果 prompt 包含 "tool" 关键字)
|
|
34
|
+
if (prompt.includes("tool")) {
|
|
35
|
+
const callId = `call_${Date.now()}`;
|
|
36
|
+
sink.onToolStart(callId, "bash", { command: "echo hello world" });
|
|
37
|
+
if (this.aborted)
|
|
38
|
+
return;
|
|
39
|
+
sink.onToolEnd(callId, { output: "hello world\n" });
|
|
40
|
+
if (this.aborted)
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
// 3. 发射正文
|
|
44
|
+
sink.onTextDelta("Response: ");
|
|
45
|
+
if (this.aborted)
|
|
46
|
+
return;
|
|
47
|
+
sink.onTextDelta(`I have processed your request: "${prompt}".`);
|
|
48
|
+
if (this.aborted)
|
|
49
|
+
return;
|
|
50
|
+
// 4. 发射完成事件
|
|
51
|
+
sink.onTurnComplete({
|
|
52
|
+
usage: {
|
|
53
|
+
inputTokens: 100,
|
|
54
|
+
outputTokens: 50,
|
|
55
|
+
cacheReadTokens: 40,
|
|
56
|
+
cacheWriteTokens: 10,
|
|
57
|
+
costUSD: 0.005,
|
|
58
|
+
},
|
|
59
|
+
costUSD: 0.005,
|
|
60
|
+
contextTokens: 150,
|
|
61
|
+
model: "mock-model-v1",
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
async abort() {
|
|
65
|
+
this.aborted = true;
|
|
66
|
+
}
|
|
67
|
+
async listModels() {
|
|
68
|
+
return [
|
|
69
|
+
{ id: "mock-model-v1", name: "Mock Model v1" },
|
|
70
|
+
{ id: "mock-model-v2", name: "Mock Model v2" },
|
|
71
|
+
];
|
|
72
|
+
}
|
|
73
|
+
async getSlashCommands() {
|
|
74
|
+
return [
|
|
75
|
+
{
|
|
76
|
+
name: "mock-echo",
|
|
77
|
+
description: "测试 Mock 驱动命令回显",
|
|
78
|
+
descriptionEn: "Test Mock driver command echo",
|
|
79
|
+
source: "extension",
|
|
80
|
+
argumentHint: "[text]",
|
|
81
|
+
},
|
|
82
|
+
];
|
|
83
|
+
}
|
|
84
|
+
async execSlashCommand(name, args, context) {
|
|
85
|
+
if (name === "mock-echo") {
|
|
86
|
+
context?.emitNotice?.("info", `Mock echo: ${args}`, `Mock echo: ${args}`);
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
async getSkills() {
|
|
92
|
+
return [
|
|
93
|
+
{
|
|
94
|
+
name: "mock-analyzer",
|
|
95
|
+
description: "Mock 示例代码分析技能",
|
|
96
|
+
filePath: "",
|
|
97
|
+
},
|
|
98
|
+
];
|
|
99
|
+
}
|
|
100
|
+
dispose() {
|
|
101
|
+
this.aborted = true;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* server/universal/index.ts — 通用 Agent 底座统一导出。
|
|
3
|
+
*/
|
|
4
|
+
export { BaseAgentDriver } from "./driver-base.js";
|
|
5
|
+
export { UniversalSessionStore } from "./session-store.js";
|
|
6
|
+
export { UniversalSession } from "./universal-session.js";
|
|
7
|
+
export { UniversalAgentService } from "./universal-service.js";
|
|
8
|
+
export { MockAgentDriver } from "./drivers/mock-driver.js";
|
|
9
|
+
export { ClaudeCodeDriver, probeClaudeCode } from "./drivers/claude-code.js";
|
|
10
|
+
export { DshAgentDriver } from "./drivers/dsh-driver.js";
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* session-store.ts — 通用 Agent 会话的轻量级磁盘持久化。
|
|
3
|
+
*
|
|
4
|
+
* 会话存储在 <dataDir>/universal-sessions/<cwdHash>/<convId>.json 下。
|
|
5
|
+
* 格式为纯 JSON,严格线性结构,便于直读、回放与恢复。
|
|
6
|
+
*/
|
|
7
|
+
import { existsSync, mkdirSync, readFileSync, readdirSync, unlinkSync } from "node:fs";
|
|
8
|
+
import { join } from "node:path";
|
|
9
|
+
import { createHash } from "node:crypto";
|
|
10
|
+
import { writeJsonAtomicSync } from "../atomic-file.js";
|
|
11
|
+
import { normalizePathKey } from "../client-state.js";
|
|
12
|
+
export class UniversalSessionStore {
|
|
13
|
+
baseDir;
|
|
14
|
+
constructor(dataDir) {
|
|
15
|
+
this.baseDir = join(dataDir, "universal-sessions");
|
|
16
|
+
try {
|
|
17
|
+
mkdirSync(this.baseDir, { recursive: true });
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
/* best effort */
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
cwdDir(cwd) {
|
|
24
|
+
const normKey = normalizePathKey(cwd);
|
|
25
|
+
const hash = createHash("sha256").update(normKey).digest("hex").slice(0, 16);
|
|
26
|
+
const dir = join(this.baseDir, hash);
|
|
27
|
+
if (!existsSync(dir)) {
|
|
28
|
+
mkdirSync(dir, { recursive: true });
|
|
29
|
+
}
|
|
30
|
+
return dir;
|
|
31
|
+
}
|
|
32
|
+
convPath(cwd, convId) {
|
|
33
|
+
const safeId = convId.replace(/[^a-zA-Z0-9_-]/g, "_");
|
|
34
|
+
return join(this.cwdDir(cwd), `${safeId}.json`);
|
|
35
|
+
}
|
|
36
|
+
listConversations(cwd) {
|
|
37
|
+
const dir = this.cwdDir(cwd);
|
|
38
|
+
if (!existsSync(dir))
|
|
39
|
+
return [];
|
|
40
|
+
const list = [];
|
|
41
|
+
for (const file of readdirSync(dir)) {
|
|
42
|
+
if (!file.endsWith(".json"))
|
|
43
|
+
continue;
|
|
44
|
+
try {
|
|
45
|
+
const full = join(dir, file);
|
|
46
|
+
const data = JSON.parse(readFileSync(full, "utf-8"));
|
|
47
|
+
if (data && data.id && Array.isArray(data.messages)) {
|
|
48
|
+
list.push(data);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
/* skip corrupted files */
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
// 按更新时间降序排列
|
|
56
|
+
return list.sort((a, b) => b.updatedAt - a.updatedAt);
|
|
57
|
+
}
|
|
58
|
+
loadConversation(cwd, convId) {
|
|
59
|
+
const p = this.convPath(cwd, convId);
|
|
60
|
+
if (!existsSync(p))
|
|
61
|
+
return null;
|
|
62
|
+
try {
|
|
63
|
+
return JSON.parse(readFileSync(p, "utf-8"));
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
saveConversation(cwd, conv) {
|
|
70
|
+
const p = this.convPath(cwd, conv.id);
|
|
71
|
+
writeJsonAtomicSync(p, conv);
|
|
72
|
+
}
|
|
73
|
+
deleteConversation(cwd, convId) {
|
|
74
|
+
const p = this.convPath(cwd, convId);
|
|
75
|
+
if (existsSync(p)) {
|
|
76
|
+
try {
|
|
77
|
+
unlinkSync(p);
|
|
78
|
+
}
|
|
79
|
+
catch {
|
|
80
|
+
/* best effort */
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
renameConversation(cwd, convId, title) {
|
|
85
|
+
const conv = this.loadConversation(cwd, convId);
|
|
86
|
+
if (conv) {
|
|
87
|
+
conv.title = title;
|
|
88
|
+
conv.updatedAt = Date.now();
|
|
89
|
+
this.saveConversation(cwd, conv);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
pinConversation(cwd, convId, pinned) {
|
|
93
|
+
const conv = this.loadConversation(cwd, convId);
|
|
94
|
+
if (conv) {
|
|
95
|
+
conv.pinned = pinned;
|
|
96
|
+
conv.updatedAt = Date.now();
|
|
97
|
+
this.saveConversation(cwd, conv);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|