dsh-mobile-plugin 0.5.45 → 0.5.47
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/lib/client.js +179 -130
- package/lib/impl.js +9 -0
- package/lib/mirror/pusher.cjs +92 -17
- package/lib/mobile/approval-facts.js +116 -24
- package/lib/mobile/history-derive.js +51 -10
- package/lib/mobile/run-timeline.js +2 -2
- package/lib/mobile/session-cache.js +16 -0
- package/lib/mobile/stats-collect.cjs +73 -1
- package/lib/mobile/stats-ledger.cjs +9 -0
- package/lib/mobile/stream.js +8 -0
- package/lib/mobile/workflow-live.js +74 -0
- package/lib/mobile/workflow-runs.js +194 -0
- package/lib/mobile/workspace-tools.js +594 -0
- package/lib/panel/index.html +15 -15
- package/lib/routes/control-plane.js +32 -1
- package/package.json +1 -1
|
@@ -0,0 +1,594 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 给模型用的「这台电脑上有什么」,以及 App 能做的那些整理动作。
|
|
5
|
+
*
|
|
6
|
+
* 这些能力此前只挂在移动 API 上,只有手机 App 能用;模型想看一眼自己的工作区与对话,
|
|
7
|
+
* 只能绕开(读日志、猜目录)。这里把它们包成一个工具,走的是与路由**同一份**底层调用:
|
|
8
|
+
* 工作区清单 `buildWorkspacesBody`、会话清单 `buildSessionsResponse`、正文
|
|
9
|
+
* `buildMessagesSnapshot`、发送 `promptOnce`、停止 `cancel`、新建 / 分叉 / 改名
|
|
10
|
+
* `session.*`、归档与置顶 `workspace.*`。插件里本来就有自己调宿主的那套
|
|
11
|
+
* (`session_cleanup` 就是),这是同一个形状。
|
|
12
|
+
*
|
|
13
|
+
* ## 能力面
|
|
14
|
+
*
|
|
15
|
+
* **与手机 App 一致**:App 界面上做得到的,这里都开放 —— 列、读、发、停、新建、改名、
|
|
16
|
+
* 分叉、归档 / 取消归档、置顶 / 取消置顶、换工作区,外加工作区的添加 / 改名 / 删除。
|
|
17
|
+
* 依据是用户 2026-11-13 那句「APP 能实现的能力,插件这边也应该开放」:两边本来就跑在
|
|
18
|
+
* 同一台机器、同一个宿主上,插件这边另设一道门槛只是让模型多绕一步,不是多一层安全。
|
|
19
|
+
*
|
|
20
|
+
* 不开放的仍是**进程级**那几样,它们不是「App 能做而这里不给」:shell、系统设置、
|
|
21
|
+
* 任意文件读写(那几样的界面入口在桌面端,不在 App)。
|
|
22
|
+
*
|
|
23
|
+
* 发送走 `promptOnce`(与手机输入框同一条),**不**替调用方决定排队还是插话 ——
|
|
24
|
+
* `mode` 原样透传给宿主,省略就按宿主的默认。
|
|
25
|
+
*
|
|
26
|
+
* ## 范围
|
|
27
|
+
*
|
|
28
|
+
* 列出来的是**这台电脑上的全部**工作区与会话,不是「当前这条会话所在的项目」。这正是
|
|
29
|
+
* 这个工具的意义(问一句「你能看到哪些对话」就能真去列),所以不做按会话过滤。
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
const path = require('node:path');
|
|
33
|
+
|
|
34
|
+
const { unwrap } = require('./envelope.js');
|
|
35
|
+
const { callSessions } = require('./rpc.js');
|
|
36
|
+
const { buildWorkspacesBody } = require('./workspaces.js');
|
|
37
|
+
const { buildSessionsResponse } = require('./sessions.js');
|
|
38
|
+
const { buildMessagesSnapshot } = require('./session-snapshot.js');
|
|
39
|
+
const { promptOnce } = require('./session-send.js');
|
|
40
|
+
const { invalidateSessionList } = require('./session-cache.js');
|
|
41
|
+
const { workspaceRoots, isWithin } = require('./allowed-zone.js');
|
|
42
|
+
|
|
43
|
+
/** 一次最多回多少条,避免把整个列表塞进模型上下文。 */
|
|
44
|
+
const LIST_LIMIT = 200;
|
|
45
|
+
/** 读会话时最多回多少条消息(宿主历史接口的窗口)。 */
|
|
46
|
+
const READ_LIMIT = 50;
|
|
47
|
+
/** 一条消息正文截到多少字符:长会话整段塞回去只会挤掉别的上下文。 */
|
|
48
|
+
const MESSAGE_TEXT_LIMIT = 4000;
|
|
49
|
+
|
|
50
|
+
/** 工作区行:只留「是什么、在哪、叫什么」三样。 */
|
|
51
|
+
function workspaceRow(item) {
|
|
52
|
+
if (!item || typeof item !== 'object') return null;
|
|
53
|
+
const path = typeof item.path === 'string' ? item.path : '';
|
|
54
|
+
if (!path) return null;
|
|
55
|
+
return {
|
|
56
|
+
workspaceId: typeof item.workspaceId === 'string' ? item.workspaceId : '',
|
|
57
|
+
title: typeof item.title === 'string' ? item.title : '',
|
|
58
|
+
path,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** 会话行:ID、标题、工作区、是否在跑、最后活动时间。 */
|
|
63
|
+
function sessionRow(item) {
|
|
64
|
+
if (!item || typeof item.sessionId !== 'string') return null;
|
|
65
|
+
return {
|
|
66
|
+
sessionId: item.sessionId,
|
|
67
|
+
title: typeof item.title === 'string' ? item.title : '',
|
|
68
|
+
cwd: typeof item.cwd === 'string' ? item.cwd : '',
|
|
69
|
+
running: item.running === true,
|
|
70
|
+
updatedAt: Number.isFinite(item.updatedAt) ? item.updatedAt : 0,
|
|
71
|
+
...(item.pinned ? { pinned: true } : {}),
|
|
72
|
+
...(item.origin ? { origin: item.origin } : {}),
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* 一条消息压成「谁说了什么」;正文过长就截断,标明截了。
|
|
78
|
+
*
|
|
79
|
+
* `deriveMessages` 的结果里混着压缩行、重试行、轮次错误这些**结构化行**(带 `kind`、没有
|
|
80
|
+
* 正文),它们是给界面画卡片用的,对模型没有意义 —— 没有正文的一律丢掉,别在结果里留一堆
|
|
81
|
+
* 空行让模型以为那里说过什么。
|
|
82
|
+
*/
|
|
83
|
+
function messageRow(item) {
|
|
84
|
+
if (!item || typeof item !== 'object') return null;
|
|
85
|
+
if (typeof item.kind === 'string') return null;
|
|
86
|
+
const text = typeof item.text === 'string' ? item.text : '';
|
|
87
|
+
if (!text.trim()) return null;
|
|
88
|
+
const truncated = text.length > MESSAGE_TEXT_LIMIT;
|
|
89
|
+
return {
|
|
90
|
+
role: typeof item.role === 'string' ? item.role : '',
|
|
91
|
+
text: truncated ? text.slice(0, MESSAGE_TEXT_LIMIT) : text,
|
|
92
|
+
...(truncated ? { truncated: true } : {}),
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* 列工作区。`buildWorkspacesBody` 失败时把宿主的错误原样抛出去 —— 静默回空清单会让
|
|
98
|
+
* 模型以为这台电脑一个项目都没有。
|
|
99
|
+
*/
|
|
100
|
+
async function listWorkspaces(ctx) {
|
|
101
|
+
const body = await buildWorkspacesBody(ctx, {});
|
|
102
|
+
if (!body || body.ok !== true) {
|
|
103
|
+
const failure = (body && body.error) || {};
|
|
104
|
+
throw new Error(failure.message || '读工作区清单失败');
|
|
105
|
+
}
|
|
106
|
+
const items = Array.isArray(body.items) ? body.items.map(workspaceRow).filter(Boolean) : [];
|
|
107
|
+
return { workspaces: items.slice(0, LIST_LIMIT), total: items.length };
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* 列会话。可选按工作区路径筛。
|
|
112
|
+
*
|
|
113
|
+
* 走 `buildSessionsResponse`:归档会话照手机端同一口径剔除,`running` 已经是「子代理也
|
|
114
|
+
* 在跑」顶过的值 —— 别在这里另写一套判据。
|
|
115
|
+
*
|
|
116
|
+
* **子代理会话不列**:它们是某条对话派出去的活,不是一个能打开、能往里说话的对话;手机
|
|
117
|
+
* 侧栏同样直接跳过它们(`app/src/drawerGroups.ts` 的 `origin === 'subagent'`)。列出来
|
|
118
|
+
* 只会给模型一个点开就失败的入口 —— 读子代理要另带父会话地址,发消息要走 subagent 投递,
|
|
119
|
+
* 那是另一条路,这一版不做。
|
|
120
|
+
*/
|
|
121
|
+
async function listSessions(ctx, options = {}) {
|
|
122
|
+
const built = await buildSessionsResponse(ctx, 'legacy');
|
|
123
|
+
if (!built || built.body.ok !== true) {
|
|
124
|
+
const failure = (built && built.body && built.body.error) || {};
|
|
125
|
+
throw new Error(failure.message || '读会话清单失败');
|
|
126
|
+
}
|
|
127
|
+
// 完整列表走全量那条分支:`buildSessionsResponse` 不传分页参数时 items 就是全部。
|
|
128
|
+
const all = Array.isArray(built.body.items) ? built.body.items : [];
|
|
129
|
+
const cwd = typeof options.cwd === 'string' ? options.cwd.trim() : '';
|
|
130
|
+
const scoped = all.filter((item) => item && item.origin !== 'subagent'
|
|
131
|
+
&& (!cwd || item.cwd === cwd));
|
|
132
|
+
const rows = scoped.map(sessionRow).filter(Boolean);
|
|
133
|
+
return { sessions: rows.slice(0, LIST_LIMIT), total: rows.length };
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** 读一条会话的最近若干条消息。 */
|
|
137
|
+
async function readSession(ctx, sessionId, limit) {
|
|
138
|
+
const snapshot = await buildMessagesSnapshot(ctx, sessionId, limit);
|
|
139
|
+
if (!snapshot || snapshot.ok !== true) {
|
|
140
|
+
const failure = (snapshot && snapshot.error) || {};
|
|
141
|
+
throw new Error(failure.message || '读会话正文失败');
|
|
142
|
+
}
|
|
143
|
+
const items = Array.isArray(snapshot.items) ? snapshot.items.map(messageRow).filter(Boolean) : [];
|
|
144
|
+
return { sessionId, messages: items };
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* 往一条会话里发一句话。
|
|
149
|
+
*
|
|
150
|
+
* 与手机 `POST /session/send` 共用 `promptOnce`(幂等、slash 命令那条派发不在这一版里:
|
|
151
|
+
* 这里只发普通消息)。`mode` 不替调用方定 —— 给队列就走队列,给插话就插话,不给就交给
|
|
152
|
+
* 宿主默认。
|
|
153
|
+
*/
|
|
154
|
+
async function sendToSession(ctx, sessionId, text, mode) {
|
|
155
|
+
const payload = { sessionId, content: [{ type: 'text', text }] };
|
|
156
|
+
if (mode) payload.mode = mode;
|
|
157
|
+
const response = await promptOnce(ctx, '', payload);
|
|
158
|
+
const { error } = unwrap(response);
|
|
159
|
+
if (error) throw new Error(error.message || '发送失败');
|
|
160
|
+
return { sessionId, sent: true };
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** 停掉一条会话正在跑的那一轮(与手机那颗停止按钮同一条 `session.cancel`)。 */
|
|
164
|
+
async function stopSession(ctx, sessionId) {
|
|
165
|
+
const { error } = unwrap(await callSessions(ctx, 'cancel', { sessionId }));
|
|
166
|
+
if (error) throw new Error(error.message || '停止失败');
|
|
167
|
+
return { sessionId, stopped: true };
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* 新建一条会话。
|
|
172
|
+
*
|
|
173
|
+
* 只接受已登记的工作区(`workspaceId`)或它的路径(`cwd`)—— 与手机上「新对话」同一个
|
|
174
|
+
* 入口,宿主自己会把新会话挂到那个项目的账上。这里不做隔离工作树那一条:它是手机侧的
|
|
175
|
+
* 一种特殊用法,不是「新建对话」的默认语义。
|
|
176
|
+
*
|
|
177
|
+
* **cwd 必须落在已登记的工作区里**,与 `POST /session/new` 同一条边界(`session-new.js`
|
|
178
|
+
* 那句「随手塞一个 / 进来等于把整台机器变成允许读写区」):会话的 cwd 就是手机侧按路径
|
|
179
|
+
* 读写文件的第二个根,这条口子放开等于绕过 AGENTS.md §2 的能力面。工作区清单读不到时
|
|
180
|
+
* 照实抛,不按「没有工作区」处理。
|
|
181
|
+
*/
|
|
182
|
+
async function createSession(ctx, options = {}) {
|
|
183
|
+
const payload = {};
|
|
184
|
+
const workspaceId = typeof options.workspaceId === 'string' ? options.workspaceId.trim() : '';
|
|
185
|
+
const declaredCwd = typeof options.cwd === 'string' ? options.cwd.trim() : '';
|
|
186
|
+
if (workspaceId) {
|
|
187
|
+
payload.workspaceId = workspaceId;
|
|
188
|
+
} else if (declaredCwd) {
|
|
189
|
+
if (!path.isAbsolute(declaredCwd)) throw new Error('cwd 必须是绝对路径');
|
|
190
|
+
const target = path.resolve(declaredCwd);
|
|
191
|
+
const roots = await workspaceRoots(ctx);
|
|
192
|
+
if (!roots.some((root) => isWithin(root, target))) {
|
|
193
|
+
throw new Error('这个目录不在已登记的工作区里,不能用来新建会话');
|
|
194
|
+
}
|
|
195
|
+
payload.cwd = target;
|
|
196
|
+
}
|
|
197
|
+
if (typeof options.agentPreset === 'string' && options.agentPreset.trim()) {
|
|
198
|
+
payload.agentPreset = options.agentPreset.trim();
|
|
199
|
+
}
|
|
200
|
+
const { value, error } = unwrap(await callSessions(ctx, 'create', payload));
|
|
201
|
+
if (error) throw new Error(error.message || '新建会话失败');
|
|
202
|
+
const sessionId = value && typeof value.sessionId === 'string' ? value.sessionId : '';
|
|
203
|
+
if (!sessionId) throw new Error('新建会话没有返回会话 ID');
|
|
204
|
+
// 与路由同一个口径:新会话必须马上出现在下一次列表里。
|
|
205
|
+
invalidateSessionList(ctx);
|
|
206
|
+
return { sessionId, cwd: (value && typeof value.cwd === 'string' && value.cwd) || '' };
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* 会话改名(`session.rename`,与手机会话菜单里那一项同一条)。
|
|
211
|
+
*
|
|
212
|
+
* 改完立刻作废列表缓存:否则下一次列表拿到的还是 10 秒窗口内的旧标题,看着像没生效。
|
|
213
|
+
*/
|
|
214
|
+
async function renameSession(ctx, sessionId, title) {
|
|
215
|
+
const { error } = unwrap(await callSessions(ctx, 'rename', { sessionId, title }));
|
|
216
|
+
if (error) throw new Error(error.message || '改名失败');
|
|
217
|
+
invalidateSessionList(ctx);
|
|
218
|
+
return { sessionId, title };
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* 分叉一条会话(`session.fork`,与 App 会话菜单里的「Fork」同一条)。
|
|
223
|
+
*
|
|
224
|
+
* `atSeq` 给到就只分叉到那一步,不给就整条复制。分叉是**非幂等**写 —— 重投一次会多一个
|
|
225
|
+
* 子会话;这里没有 requestId,所以调用方必须自己知道它在新建东西。
|
|
226
|
+
*/
|
|
227
|
+
async function forkSession(ctx, sessionId, options = {}) {
|
|
228
|
+
const payload = { sessionId };
|
|
229
|
+
if (Number.isSafeInteger(options.atSeq) && options.atSeq >= 0) {
|
|
230
|
+
payload.atSeq = options.atSeq;
|
|
231
|
+
payload.increaseTitle = options.increaseTitle !== false;
|
|
232
|
+
}
|
|
233
|
+
const { value, error } = unwrap(await callSessions(ctx, 'fork', payload));
|
|
234
|
+
if (error) throw new Error(error.message || '分叉失败');
|
|
235
|
+
const forkedId = value && typeof value.sessionId === 'string' ? value.sessionId : '';
|
|
236
|
+
if (!forkedId) throw new Error('分叉没有返回新会话 ID');
|
|
237
|
+
invalidateSessionList(ctx);
|
|
238
|
+
return { sessionId: forkedId, from: sessionId };
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* 把一条会话移进 / 移出归档集合,以及置顶。
|
|
243
|
+
*
|
|
244
|
+
* 三个动作共用 `workspace.<method>`(`archiveSession` / `unarchiveSession` / `pinSession` /
|
|
245
|
+
* `unpinSession`),与手机侧栏行尾那几颗按钮同一套动词。归档在移动端就是「删除」语义,
|
|
246
|
+
* 日志留着;取消归档与置顶都是幂等的。
|
|
247
|
+
*/
|
|
248
|
+
async function setSessionFlag(ctx, method, sessionId, extra = {}) {
|
|
249
|
+
const { value, error } = unwrap(await callWorkspace(ctx, method, { sessionId, ...extra }));
|
|
250
|
+
if (error) throw new Error(error.message || `${method} 失败`);
|
|
251
|
+
invalidateSessionList(ctx);
|
|
252
|
+
return { sessionId, ...(value && typeof value === 'object' ? value : {}) };
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/** 会话换到另一个工作区(`workspace.insertSessionBefore`,与侧栏拖动排序同一个动词)。 */
|
|
256
|
+
async function moveSession(ctx, workspaceId, sessionId, beforeSessionId) {
|
|
257
|
+
const payload = { workspaceId, sessionId };
|
|
258
|
+
if (typeof beforeSessionId === 'string' && beforeSessionId.trim()) {
|
|
259
|
+
payload.beforeSessionId = beforeSessionId.trim();
|
|
260
|
+
}
|
|
261
|
+
const { value, error } = unwrap(await callWorkspace(ctx, 'insertSessionBefore', payload));
|
|
262
|
+
if (error) throw new Error(error.message || '移动会话失败');
|
|
263
|
+
invalidateSessionList(ctx);
|
|
264
|
+
return { sessionId, workspaceId, workspace: value && value.workspace };
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* 把一个目录采纳成工作区(`workspace.create`)。
|
|
269
|
+
*
|
|
270
|
+
* 与手机上「添加工作区」同一个动词:已经登记过的返回现有那一项,`created` 说这次是不是
|
|
271
|
+
* 真新建。路径不做「必须在某个允许区里」的检查 —— 这条**就是**在扩大允许区本身,它和
|
|
272
|
+
* 手机上点「添加工作区」选一个目录是同一件事。
|
|
273
|
+
*/
|
|
274
|
+
async function createWorkspace(ctx, dirPath) {
|
|
275
|
+
const { value, error } = unwrap(await callWorkspace(ctx, 'create', { path: dirPath }));
|
|
276
|
+
if (error) throw new Error(error.message || '添加工作区失败');
|
|
277
|
+
invalidateSessionList(ctx);
|
|
278
|
+
const workspace = value && value.workspace ? value.workspace : null;
|
|
279
|
+
return {
|
|
280
|
+
workspaceId: workspace && typeof workspace.workspaceId === 'string' ? workspace.workspaceId : '',
|
|
281
|
+
title: workspace && typeof workspace.title === 'string' ? workspace.title : '',
|
|
282
|
+
path: workspace && typeof workspace.path === 'string' ? workspace.path : dirPath,
|
|
283
|
+
created: !!(value && value.created),
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* 删除工作区登记(`workspace.delete`)。
|
|
289
|
+
*
|
|
290
|
+
* 与手机上那一项同一口径:**只删登记,目录与里面的会话日志一概不动**。名字取 `remove*`
|
|
291
|
+
* 而不是 `delete*`,免得读的人以为它会删目录。
|
|
292
|
+
*/
|
|
293
|
+
async function removeWorkspace(ctx, workspaceId) {
|
|
294
|
+
const { error } = unwrap(await callWorkspace(ctx, 'delete', { workspaceId }));
|
|
295
|
+
if (error) throw new Error(error.message || '删除工作区失败');
|
|
296
|
+
invalidateSessionList(ctx);
|
|
297
|
+
return { workspaceId, removed: true };
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/** 改工作区显示名(`workspace.rename`,重名时宿主回 `workspace/name-conflict`)。 */
|
|
301
|
+
async function renameWorkspace(ctx, workspaceId, title) {
|
|
302
|
+
const { value, error } = unwrap(await callWorkspace(ctx, 'rename', { workspaceId, title }));
|
|
303
|
+
if (error) throw new Error(error.message || '重命名工作区失败');
|
|
304
|
+
invalidateSessionList(ctx);
|
|
305
|
+
return { workspaceId, title, workspace: value && value.workspace };
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/** 只读把一条已经拆出去的活儿接回来:这里只是把 `callWorkspace` 暴露给上面几个动作。 */
|
|
309
|
+
function callWorkspace(ctx, method, payload) {
|
|
310
|
+
return ctx.apiProxy.workspace[method]({ rpcId: 'mobile', method: `workspace.${method}`, payload });
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* 工具定义。一个工具、五个动作:能力是同一件事(这台电脑上的工作区与对话),拆成五个只会
|
|
315
|
+
* 让工具列表变长。
|
|
316
|
+
*/
|
|
317
|
+
function registerWorkspaceTool(ctx, log) {
|
|
318
|
+
const { defineTool } = require('@deepseek-ai/dsh-tools');
|
|
319
|
+
ctx.tools.register(defineTool({
|
|
320
|
+
name: 'dsh_workspace',
|
|
321
|
+
description: 'Inspect and act on the DSH workspaces and conversations on this computer: list registered workspaces, list conversations, read a conversation, send a message into one, stop the turn that is running, or start a new conversation. Covers every workspace on this machine, not only the current one.',
|
|
322
|
+
parameters: {
|
|
323
|
+
action: {
|
|
324
|
+
type: 'string',
|
|
325
|
+
required: true,
|
|
326
|
+
enum: [
|
|
327
|
+
'list_workspaces', 'list_sessions', 'read_session',
|
|
328
|
+
'send_message', 'stop_session', 'new_session',
|
|
329
|
+
'rename_session', 'fork_session',
|
|
330
|
+
'archive_session', 'unarchive_session', 'pin_session', 'unpin_session',
|
|
331
|
+
'move_session',
|
|
332
|
+
'create_workspace', 'rename_workspace', 'remove_workspace',
|
|
333
|
+
],
|
|
334
|
+
description: 'Which operation to run.',
|
|
335
|
+
},
|
|
336
|
+
sessionId: { type: 'string', description: 'Target conversation; used by most session actions.' },
|
|
337
|
+
text: { type: 'string', description: 'Message body for send_message.' },
|
|
338
|
+
title: { type: 'string', description: 'New title for rename_session and rename_workspace.' },
|
|
339
|
+
mode: { type: 'string', enum: ['queue', 'steer'], description: 'Optional delivery mode for send_message: queue waits for the current turn, steer interjects. Omit to keep the host default.' },
|
|
340
|
+
cwd: { type: 'string', description: 'Workspace path: filters list_sessions, and picks the project for new_session. For new_session it must be an absolute path inside a registered workspace.' },
|
|
341
|
+
path: { type: 'string', description: 'Absolute directory to adopt as a workspace, for create_workspace.' },
|
|
342
|
+
workspaceId: { type: 'string', description: 'Registered workspace id, for new_session / move_session / rename_workspace / remove_workspace.' },
|
|
343
|
+
beforeSessionId: { type: 'string', description: 'Optional ordering anchor for move_session: place the session right before this one.' },
|
|
344
|
+
atSeq: { type: 'integer', description: 'Optional point to fork at for fork_session; omit to copy the whole conversation.' },
|
|
345
|
+
agentPreset: { type: 'string', description: 'Optional agent preset for new_session.' },
|
|
346
|
+
limit: { type: 'integer', description: 'Maximum messages returned by read_session (default 50).' },
|
|
347
|
+
},
|
|
348
|
+
output: {
|
|
349
|
+
schema: {
|
|
350
|
+
type: 'object',
|
|
351
|
+
additionalProperties: false,
|
|
352
|
+
properties: {
|
|
353
|
+
action: { type: 'string' },
|
|
354
|
+
workspaces: {
|
|
355
|
+
type: 'array',
|
|
356
|
+
items: {
|
|
357
|
+
type: 'object',
|
|
358
|
+
additionalProperties: false,
|
|
359
|
+
properties: {
|
|
360
|
+
workspaceId: { type: 'string' },
|
|
361
|
+
title: { type: 'string' },
|
|
362
|
+
path: { type: 'string' },
|
|
363
|
+
},
|
|
364
|
+
},
|
|
365
|
+
},
|
|
366
|
+
sessions: {
|
|
367
|
+
type: 'array',
|
|
368
|
+
items: {
|
|
369
|
+
type: 'object',
|
|
370
|
+
additionalProperties: false,
|
|
371
|
+
properties: {
|
|
372
|
+
sessionId: { type: 'string' },
|
|
373
|
+
title: { type: 'string' },
|
|
374
|
+
cwd: { type: 'string' },
|
|
375
|
+
running: { type: 'boolean' },
|
|
376
|
+
updatedAt: { type: 'number' },
|
|
377
|
+
pinned: { type: 'boolean' },
|
|
378
|
+
origin: { type: 'string' },
|
|
379
|
+
},
|
|
380
|
+
},
|
|
381
|
+
},
|
|
382
|
+
messages: {
|
|
383
|
+
type: 'array',
|
|
384
|
+
items: {
|
|
385
|
+
type: 'object',
|
|
386
|
+
additionalProperties: false,
|
|
387
|
+
properties: {
|
|
388
|
+
role: { type: 'string' },
|
|
389
|
+
text: { type: 'string' },
|
|
390
|
+
truncated: { type: 'boolean' },
|
|
391
|
+
},
|
|
392
|
+
},
|
|
393
|
+
},
|
|
394
|
+
sessions_total: { type: 'integer' },
|
|
395
|
+
workspaces_total: { type: 'integer' },
|
|
396
|
+
sessionId: { type: 'string' },
|
|
397
|
+
workspaceId: { type: 'string' },
|
|
398
|
+
title: { type: 'string' },
|
|
399
|
+
cwd: { type: 'string' },
|
|
400
|
+
sent: { type: 'boolean' },
|
|
401
|
+
stopped: { type: 'boolean' },
|
|
402
|
+
},
|
|
403
|
+
},
|
|
404
|
+
render: (_args, value) => [{ type: 'text', text: renderSummary(value) }],
|
|
405
|
+
},
|
|
406
|
+
execute: async (args) => {
|
|
407
|
+
switch (args.action) {
|
|
408
|
+
case 'list_workspaces': {
|
|
409
|
+
const listed = await listWorkspaces(ctx);
|
|
410
|
+
return { action: args.action, workspaces: listed.workspaces, workspaces_total: listed.total };
|
|
411
|
+
}
|
|
412
|
+
case 'list_sessions': {
|
|
413
|
+
const listed = await listSessions(ctx, { cwd: args.cwd });
|
|
414
|
+
return { action: args.action, sessions: listed.sessions, sessions_total: listed.total };
|
|
415
|
+
}
|
|
416
|
+
case 'read_session': {
|
|
417
|
+
const sessionId = requiredSessionId(args);
|
|
418
|
+
const limit = Number.isInteger(args.limit) && args.limit > 0 ? Math.min(args.limit, READ_LIMIT) : READ_LIMIT;
|
|
419
|
+
const read = await readSession(ctx, sessionId, limit);
|
|
420
|
+
return { action: args.action, sessionId, messages: read.messages };
|
|
421
|
+
}
|
|
422
|
+
case 'send_message': {
|
|
423
|
+
const sessionId = requiredSessionId(args);
|
|
424
|
+
const text = typeof args.text === 'string' ? args.text.trim() : '';
|
|
425
|
+
if (!text) throw new Error('send_message 需要 text');
|
|
426
|
+
const sent = await sendToSession(ctx, sessionId, text, args.mode);
|
|
427
|
+
return { action: args.action, sessionId: sent.sessionId, sent: true };
|
|
428
|
+
}
|
|
429
|
+
case 'stop_session': {
|
|
430
|
+
const sessionId = requiredSessionId(args);
|
|
431
|
+
const stopped = await stopSession(ctx, sessionId);
|
|
432
|
+
return { action: args.action, sessionId: stopped.sessionId, stopped: true };
|
|
433
|
+
}
|
|
434
|
+
case 'new_session': {
|
|
435
|
+
const created = await createSession(ctx, args);
|
|
436
|
+
return { action: args.action, sessionId: created.sessionId, cwd: created.cwd };
|
|
437
|
+
}
|
|
438
|
+
case 'rename_session': {
|
|
439
|
+
const sessionId = requiredSessionId(args);
|
|
440
|
+
const title = requiredText(args, 'title');
|
|
441
|
+
const renamed = await renameSession(ctx, sessionId, title);
|
|
442
|
+
return { action: args.action, sessionId: renamed.sessionId, title: renamed.title };
|
|
443
|
+
}
|
|
444
|
+
case 'fork_session': {
|
|
445
|
+
const sessionId = requiredSessionId(args);
|
|
446
|
+
const forked = await forkSession(ctx, sessionId, { atSeq: args.atSeq });
|
|
447
|
+
return { action: args.action, sessionId: forked.sessionId };
|
|
448
|
+
}
|
|
449
|
+
case 'archive_session':
|
|
450
|
+
case 'unarchive_session':
|
|
451
|
+
case 'pin_session':
|
|
452
|
+
case 'unpin_session': {
|
|
453
|
+
const sessionId = requiredSessionId(args);
|
|
454
|
+
const method = ACTION_TO_FLAG[args.action];
|
|
455
|
+
await setSessionFlag(ctx, method, sessionId, args.action === 'archive_session' ? {} : {});
|
|
456
|
+
return { action: args.action, sessionId };
|
|
457
|
+
}
|
|
458
|
+
case 'move_session': {
|
|
459
|
+
const sessionId = requiredSessionId(args);
|
|
460
|
+
const workspaceId = requiredText(args, 'workspaceId');
|
|
461
|
+
await moveSession(ctx, workspaceId, sessionId, args.beforeSessionId);
|
|
462
|
+
return { action: args.action, sessionId, workspaceId };
|
|
463
|
+
}
|
|
464
|
+
case 'create_workspace': {
|
|
465
|
+
const created = await createWorkspace(ctx, requiredText(args, 'path'));
|
|
466
|
+
return {
|
|
467
|
+
action: args.action,
|
|
468
|
+
workspaceId: created.workspaceId,
|
|
469
|
+
title: created.title,
|
|
470
|
+
cwd: created.path,
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
case 'rename_workspace': {
|
|
474
|
+
const workspaceId = requiredText(args, 'workspaceId');
|
|
475
|
+
const title = requiredText(args, 'title');
|
|
476
|
+
await renameWorkspace(ctx, workspaceId, title);
|
|
477
|
+
return { action: args.action, workspaceId, title };
|
|
478
|
+
}
|
|
479
|
+
case 'remove_workspace': {
|
|
480
|
+
const workspaceId = requiredText(args, 'workspaceId');
|
|
481
|
+
await removeWorkspace(ctx, workspaceId);
|
|
482
|
+
return { action: args.action, workspaceId };
|
|
483
|
+
}
|
|
484
|
+
default:
|
|
485
|
+
throw new Error(`未知的 action:${args.action}`);
|
|
486
|
+
}
|
|
487
|
+
},
|
|
488
|
+
}));
|
|
489
|
+
log?.('dsh_workspace 工具已注册');
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
function requiredSessionId(args) {
|
|
493
|
+
const sessionId = typeof args.sessionId === 'string' ? args.sessionId.trim() : '';
|
|
494
|
+
if (!sessionId) throw new Error('这个动作需要 sessionId');
|
|
495
|
+
return sessionId;
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
/** 取一个必填的文本参数(标题、工作区 id、目录路径都走它)。 */
|
|
499
|
+
function requiredText(args, key) {
|
|
500
|
+
const value = typeof args[key] === 'string' ? args[key].trim() : '';
|
|
501
|
+
if (!value) throw new Error(`这个动作需要 ${key}`);
|
|
502
|
+
return value;
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
/** 四个会话标记动作 → 宿主动词。 */
|
|
506
|
+
const ACTION_TO_FLAG = {
|
|
507
|
+
archive_session: 'archiveSession',
|
|
508
|
+
unarchive_session: 'unarchiveSession',
|
|
509
|
+
pin_session: 'pinSession',
|
|
510
|
+
unpin_session: 'unpinSession',
|
|
511
|
+
};
|
|
512
|
+
|
|
513
|
+
/** 结果压成一句话,给模型看的那份正文。 */
|
|
514
|
+
function renderSummary(value) {
|
|
515
|
+
switch (value.action) {
|
|
516
|
+
case 'list_workspaces': {
|
|
517
|
+
const names = (value.workspaces || []).map((w) => w.title || w.path).slice(0, 20);
|
|
518
|
+
return `工作区 ${value.workspaces_total || 0} 个:${names.join('、') || '(空)'}`;
|
|
519
|
+
}
|
|
520
|
+
case 'list_sessions': {
|
|
521
|
+
const names = (value.sessions || []).map((s) => s.title || s.sessionId).slice(0, 20);
|
|
522
|
+
return `会话 ${value.sessions_total || 0} 条:${names.join('、') || '(空)'}`;
|
|
523
|
+
}
|
|
524
|
+
case 'read_session':
|
|
525
|
+
return `读了会话 ${value.sessionId} 的最近 ${(value.messages || []).length} 条消息。`;
|
|
526
|
+
case 'send_message':
|
|
527
|
+
return `已把消息发给会话 ${value.sessionId}。`;
|
|
528
|
+
case 'stop_session':
|
|
529
|
+
return `已停止会话 ${value.sessionId} 正在跑的这一轮。`;
|
|
530
|
+
case 'new_session':
|
|
531
|
+
return `已新建会话 ${value.sessionId}${value.cwd ? `(${value.cwd})` : ''}。`;
|
|
532
|
+
case 'rename_session':
|
|
533
|
+
return `会话 ${value.sessionId} 已改名为「${value.title}」。`;
|
|
534
|
+
case 'fork_session':
|
|
535
|
+
return `已从 ${value.sessionId ? '原会话' : '会话'}分叉出新会话 ${value.sessionId}。`;
|
|
536
|
+
case 'archive_session':
|
|
537
|
+
return `已归档会话 ${value.sessionId}(对话日志保留,可从归档里找回来)。`;
|
|
538
|
+
case 'unarchive_session':
|
|
539
|
+
return `已把会话 ${value.sessionId} 移出归档。`;
|
|
540
|
+
case 'pin_session':
|
|
541
|
+
return `已置顶会话 ${value.sessionId}。`;
|
|
542
|
+
case 'unpin_session':
|
|
543
|
+
return `已取消置顶会话 ${value.sessionId}。`;
|
|
544
|
+
case 'move_session':
|
|
545
|
+
return `已把会话 ${value.sessionId} 移到工作区 ${value.workspaceId}。`;
|
|
546
|
+
case 'create_workspace':
|
|
547
|
+
return `工作区「${value.title || value.cwd}」已在 ${value.cwd}。`;
|
|
548
|
+
case 'rename_workspace':
|
|
549
|
+
return `工作区 ${value.workspaceId} 已改名为「${value.title}」。`;
|
|
550
|
+
case 'remove_workspace':
|
|
551
|
+
return `已删除工作区登记 ${value.workspaceId}(目录与对话都还在)。`;
|
|
552
|
+
default:
|
|
553
|
+
return '完成。';
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
/**
|
|
558
|
+
* 装配层的入口:挂 `dsh_workspace` 工具。
|
|
559
|
+
*
|
|
560
|
+
* 与 `installExtensions` 同一个口径:没有 tools 注入的 profile 只少这个工具,插件其余部分
|
|
561
|
+
* 照常装 —— 缺注入时 cordis 抛的是 "without inject"。别的错误照抛,不能把真的注册失败
|
|
562
|
+
* 也当成「这个 profile 没注入」。
|
|
563
|
+
*/
|
|
564
|
+
function installWorkspaceTools(ctx) {
|
|
565
|
+
try {
|
|
566
|
+
registerWorkspaceTool(ctx, (message) => {
|
|
567
|
+
try { ctx.logger?.info?.(message); } catch { /* 日志失败不影响注册 */ }
|
|
568
|
+
});
|
|
569
|
+
} catch (error) {
|
|
570
|
+
const message = error && error.message ? error.message : String(error);
|
|
571
|
+
if (!message.includes('without inject')) throw error;
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
module.exports = {
|
|
576
|
+
registerWorkspaceTool,
|
|
577
|
+
installWorkspaceTools,
|
|
578
|
+
listWorkspaces,
|
|
579
|
+
listSessions,
|
|
580
|
+
readSession,
|
|
581
|
+
sendToSession,
|
|
582
|
+
stopSession,
|
|
583
|
+
createSession,
|
|
584
|
+
renameSession,
|
|
585
|
+
forkSession,
|
|
586
|
+
setSessionFlag,
|
|
587
|
+
moveSession,
|
|
588
|
+
createWorkspace,
|
|
589
|
+
removeWorkspace,
|
|
590
|
+
renameWorkspace,
|
|
591
|
+
workspaceRow,
|
|
592
|
+
sessionRow,
|
|
593
|
+
messageRow,
|
|
594
|
+
};
|