dsh-mobile-plugin 0.5.35 → 0.5.36
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/impl.js +0 -7
- package/lib/mobile/answer-sources.js +223 -0
- package/lib/mobile/approval-memory.js +1 -1
- package/lib/mobile/events.js +0 -20
- package/lib/mobile/extension-data.js +105 -5
- package/lib/mobile/history-derive.js +54 -1
- package/lib/mobile/notification-prefs.js +2 -2
- package/lib/mobile/permissions.js +8 -5
- package/lib/mobile/plugin-market.js +103 -4
- package/lib/mobile/run-timeline.js +317 -0
- package/lib/mobile/session-stop-summary.js +122 -0
- package/lib/mobile/stats-ledger.cjs +45 -0
- package/lib/mobile/stop-summary.js +146 -0
- package/lib/mobile/third-party-extensions.js +86 -0
- package/lib/mobile/tool-result-cache.js +30 -2
- package/lib/panel/index.html +15 -15
- package/lib/relay/lan-proxy.js +20 -11
- package/lib/relay/tunnel.js +28 -12
- package/lib/routes/mobile-api.js +25 -19
- package/lib/shared/derived-cache.js +121 -0
- package/lib/shared/http-util.js +55 -18
- package/package.json +1 -2
- package/config/agent-presets/supervisor/agent.cordis.yml +0 -237
- package/config/agent-presets/supervisor/preset.yml +0 -3
- package/lib/mobile/auto-review-log.js +0 -160
- package/lib/mobile/knowledge-base.js +0 -481
- package/lib/mobile/mail.js +0 -621
- package/lib/mobile/supervisor-broadcast.js +0 -503
- package/lib/mobile/supervisor-memory/index.js +0 -305
- package/lib/mobile/supervisor-runtime/index.js +0 -54
- package/lib/mobile/supervisor-runtime/inject.js +0 -218
- package/lib/mobile/supervisor-runtime/judgment.js +0 -113
- package/lib/mobile/supervisor-tools.js +0 -419
- package/lib/mobile/task-ledger.js +0 -381
|
@@ -1,419 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* 总管那一组工具:它自己不干活,干活的是各项目里的对话。
|
|
5
|
-
*
|
|
6
|
-
* 这里给模型两条线:
|
|
7
|
-
* - 读:列所有会话(谁在跑 / 谁完成 / 谁报错 / 谁在等人答)、看某条会话现在到哪一步、
|
|
8
|
-
* 读某条会话的正文;
|
|
9
|
-
* - 写:往某条会话发一句话、按 Agent 预设新开一条会话、中断某条会话。
|
|
10
|
-
*
|
|
11
|
-
* ## 能力面的边界
|
|
12
|
-
*
|
|
13
|
-
* 参数全是结构化字段,没有任何一格能拼出一段 shell。**目标会话**由 `sessionId` 指定 ——
|
|
14
|
-
* 那正是「总管管的是别的对话」这件事本身;模型不能指定的是**身份**与**设备**:谁是发起人
|
|
15
|
-
* 一律从 `exec.agent.id` 取(就是总管这条会话自己的 id),不接受参数覆盖;也没有任何
|
|
16
|
-
* `deviceId` 之类的口子让它去动别的机器。会话 id 拿不到就整条不执行,绝不默默用空串。
|
|
17
|
-
*
|
|
18
|
-
* ## 为什么注册必须走 `ctx.inject(['tools'], …)`
|
|
19
|
-
*
|
|
20
|
-
* 宿主里 `tools` 是子代理作用域提供的服务,插件这一层直接读 `ctx.tools` 拿到的是
|
|
21
|
-
* undefined,`register` 一抛就被容错吞掉 —— 现象是这组工具**一条都没进过模型的工具清单**。
|
|
22
|
-
* 同树的 `phone/tools.js` 也是这个写法(见 dev-map A3-79 的坑)。
|
|
23
|
-
*
|
|
24
|
-
* ## 复用现成的读法,而不是自己挨个盯
|
|
25
|
-
*
|
|
26
|
-
* 「谁在跑、谁完成、谁报错、谁在等人答」直接吃 `/monitor` 那套投影(`buildMonitorState`)
|
|
27
|
-
* 与 `lastTurn` 的上一轮事实;正文走消息快照(`buildMessagesSnapshot`)。这两条都是宿主
|
|
28
|
-
* 已经维护好的账,重造一份只会与它们走偏。
|
|
29
|
-
*
|
|
30
|
-
* ## 结果里不许留 undefined
|
|
31
|
-
*
|
|
32
|
-
* 宿主会把带 `undefined` 字段的整条结果判成非法 JSON(见 A3-79)。所以每个工具的返回值
|
|
33
|
-
* 最后都过一遍 `clean()`(JSON 往返),`undefined` 的键当场消失,宁可不出字段也不占位。
|
|
34
|
-
*/
|
|
35
|
-
|
|
36
|
-
const path = require('node:path');
|
|
37
|
-
|
|
38
|
-
const { buildMonitorState } = require('./monitor.js');
|
|
39
|
-
const { buildMessagesSnapshot } = require('./session-snapshot.js');
|
|
40
|
-
const { callSessions } = require('./rpc.js');
|
|
41
|
-
const { unwrap } = require('./envelope.js');
|
|
42
|
-
const { promptErrorObject } = require('./history-derive.js');
|
|
43
|
-
const { invalidateSessionList } = require('./session-cache.js');
|
|
44
|
-
const { workspaceRoots, isWithin } = require('./allowed-zone.js');
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* 总管这份监控视图用的设备键。
|
|
48
|
-
*
|
|
49
|
-
* `/monitor` 的完成态是按设备算的(`clearedAt` 挡掉「这台已经读过的」)。总管不是某台
|
|
50
|
-
* 手机,它要的是「这条会话到底跑成没有」这个客观事实,所以给它一个专用的设备键 —— 这个
|
|
51
|
-
* 键从没读过任何会话,`completed` 就永远按事实走,也不会去污染真手机那份提醒账。
|
|
52
|
-
*/
|
|
53
|
-
const SUPERVISOR_DEVICE_KEY = 'supervisor-tools';
|
|
54
|
-
|
|
55
|
-
/** 一条消息的最大长度(字符)。给个上限只是防呆,正常对话远用不到。 */
|
|
56
|
-
const MAX_TEXT = 100000;
|
|
57
|
-
/** 会话 / 工作区 / 预设这类标识的长度上限。 */
|
|
58
|
-
const MAX_ID = 200;
|
|
59
|
-
|
|
60
|
-
/** 把值里值为 `undefined` 的键清掉:宿主只认干净的 JSON。 */
|
|
61
|
-
function clean(value) {
|
|
62
|
-
return JSON.parse(JSON.stringify(value));
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/** 从执行上下文里认出发起人(总管自己这条会话)。**只看 `exec.agent.id`**。 */
|
|
66
|
-
function sessionOf(exec) {
|
|
67
|
-
const id = exec && exec.agent && exec.agent.id;
|
|
68
|
-
return typeof id === 'string' ? id : '';
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/** 必填 id:去空白、非空、不超长。 */
|
|
72
|
-
function requiredId(value, label) {
|
|
73
|
-
const s = typeof value === 'string' ? value.trim() : '';
|
|
74
|
-
if (!s) throw new Error(`${label} 不能为空。`);
|
|
75
|
-
if (s.length > MAX_ID) throw new Error(`${label} 太长。`);
|
|
76
|
-
return s;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/** 可选 id:没有就是空串;有就按必填的规矩校验。 */
|
|
80
|
-
function optionalId(value, label) {
|
|
81
|
-
if (value === undefined || value === null) return '';
|
|
82
|
-
if (typeof value !== 'string') throw new Error(`${label} 必须是字符串。`);
|
|
83
|
-
const s = value.trim();
|
|
84
|
-
if (!s) return '';
|
|
85
|
-
if (s.length > MAX_ID) throw new Error(`${label} 太长。`);
|
|
86
|
-
return s;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/** 消息正文:去首尾空白、非空、不超长。 */
|
|
90
|
-
function cleanText(value) {
|
|
91
|
-
const s = typeof value === 'string' ? value.trim() : '';
|
|
92
|
-
if (!s) throw new Error('消息内容不能为空。');
|
|
93
|
-
if (s.length > MAX_TEXT) throw new Error('消息太长了。');
|
|
94
|
-
return s;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/** 把一条 `/monitor` 条目翻成一句人话里的状态。 */
|
|
98
|
-
function deriveState(row) {
|
|
99
|
-
if (row.running) return 'running';
|
|
100
|
-
if (row.pendingInteraction) return 'waiting';
|
|
101
|
-
if (row.lastTurnError) return 'error';
|
|
102
|
-
if (row.completed) return 'completed';
|
|
103
|
-
if (row.lastTurn && row.lastTurn.status === 'failed') return 'error';
|
|
104
|
-
if (row.lastTurn && row.lastTurn.status === 'completed') return 'completed';
|
|
105
|
-
return 'idle';
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/** 把 `/monitor` 条目 + 清单摘要里的项目信息拼成一行给模型看的账。 */
|
|
109
|
-
function buildRow(item, meta) {
|
|
110
|
-
const row = {
|
|
111
|
-
sessionId: item.sessionId,
|
|
112
|
-
title: item.title,
|
|
113
|
-
running: item.running === true,
|
|
114
|
-
completed: item.completed === true,
|
|
115
|
-
};
|
|
116
|
-
if (item.pendingInteraction) row.pendingInteraction = item.pendingInteraction;
|
|
117
|
-
if (item.lastTurnError) {
|
|
118
|
-
row.lastTurnError = {
|
|
119
|
-
code: item.lastTurnError.code ?? null,
|
|
120
|
-
message: item.lastTurnError.message ?? null,
|
|
121
|
-
};
|
|
122
|
-
}
|
|
123
|
-
if (item.lastTurn) row.lastTurn = item.lastTurn;
|
|
124
|
-
if (item.lastMessage && typeof item.lastMessage.text === 'string') {
|
|
125
|
-
row.lastMessage = { role: item.lastMessage.role, text: item.lastMessage.text };
|
|
126
|
-
}
|
|
127
|
-
// 项目目录与 Agent 预设来自会话清单摘要(`/monitor` 那条只投影状态位,不带这两格):
|
|
128
|
-
// 总管要回答「这条属于哪个项目」、也要知道新开一条会话该落在哪儿。
|
|
129
|
-
if (meta) {
|
|
130
|
-
if (typeof meta.cwd === 'string' && meta.cwd) row.cwd = meta.cwd;
|
|
131
|
-
if (typeof meta.agentPreset === 'string' && meta.agentPreset) row.agentPreset = meta.agentPreset;
|
|
132
|
-
}
|
|
133
|
-
row.state = deriveState(row);
|
|
134
|
-
return row;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/** 取原始会话清单(不折预览),只用来补项目信息与校验目标存在。 */
|
|
138
|
-
async function rawSessions(ctx) {
|
|
139
|
-
const { value } = unwrap(await callSessions(ctx, 'list', {}, 'mobile'));
|
|
140
|
-
if (Array.isArray(value)) return value;
|
|
141
|
-
if (value && Array.isArray(value.items)) return value.items;
|
|
142
|
-
return [];
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* 整份家底:状态位吃 `/monitor` 的投影,项目信息吃原始清单。
|
|
147
|
-
*
|
|
148
|
-
* 两次 list 并发发出去:`rpc.js` 会把同一时刻在飞的那次 list 合流掉,所以宿主那边实际
|
|
149
|
-
* 只扫一遍日志,不是两遍。
|
|
150
|
-
*/
|
|
151
|
-
async function readOverview(ctx) {
|
|
152
|
-
const [built, rawEnvelope] = await Promise.all([
|
|
153
|
-
buildMonitorState(ctx, SUPERVISOR_DEVICE_KEY),
|
|
154
|
-
callSessions(ctx, 'list', {}, 'mobile'),
|
|
155
|
-
]);
|
|
156
|
-
if (built && built.error) {
|
|
157
|
-
const message = (built.error && built.error.message) || (built.error && built.error.code) || '宿主报错';
|
|
158
|
-
throw new Error(`读会话清单失败:${message}`);
|
|
159
|
-
}
|
|
160
|
-
if (built && built.degraded) throw new Error('宿主会话列表形状异常,这一拍没读到。');
|
|
161
|
-
const items = Array.isArray(built && built.items) ? built.items : [];
|
|
162
|
-
|
|
163
|
-
const metaById = new Map();
|
|
164
|
-
const { value } = unwrap(rawEnvelope);
|
|
165
|
-
const summaries = Array.isArray(value) ? value : (value && Array.isArray(value.items) ? value.items : []);
|
|
166
|
-
for (const s of summaries) {
|
|
167
|
-
if (s && typeof s.sessionId === 'string') metaById.set(s.sessionId, s);
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
const rows = items.map((item) => buildRow(item, metaById.get(item.sessionId))).filter((row) => row.sessionId);
|
|
171
|
-
return { rows, metaById };
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
/** 校验目标会话存在且是一条普通会话(子代理不归总管直接使唤)。 */
|
|
175
|
-
async function requireSession(ctx, id) {
|
|
176
|
-
const rows = await rawSessions(ctx);
|
|
177
|
-
const found = rows.find((s) => s && s.sessionId === id) || null;
|
|
178
|
-
if (!found) throw new Error(`找不到会话 ${id},先用 supervisor_list_sessions 看有哪些。`);
|
|
179
|
-
if (found.origin === 'subagent') throw new Error(`会话 ${id} 是一条子代理,总管不直接操作它。`);
|
|
180
|
-
return found;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
/** 每行怎么念给模型听。 */
|
|
184
|
-
function rowLine(row) {
|
|
185
|
-
const bits = [`${row.sessionId}`, `《${row.title || '未命名'}》`];
|
|
186
|
-
const label = {
|
|
187
|
-
running: '正在跑', completed: '上一轮已完成', error: '上一轮报错',
|
|
188
|
-
waiting: '在等人答', idle: '空闲',
|
|
189
|
-
}[row.state] || row.state;
|
|
190
|
-
bits.push(label);
|
|
191
|
-
if (row.cwd) bits.push(`项目 ${row.cwd}`);
|
|
192
|
-
if (row.lastTurnError && row.lastTurnError.message) bits.push(`错误:${row.lastTurnError.message}`);
|
|
193
|
-
if (row.pendingInteraction) bits.push(`待处理:${row.pendingInteraction}`);
|
|
194
|
-
if (row.lastMessage && row.lastMessage.text) bits.push(`最后一句:${row.lastMessage.text.slice(0, 80)}`);
|
|
195
|
-
return `· ${bits.join(',')}`;
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
const OUTPUT_FREE = { type: 'object', additionalProperties: true };
|
|
199
|
-
|
|
200
|
-
/**
|
|
201
|
-
* 挂载总管工具组。缺 `tools` 注入的 profile(纯手机端宿主 / 没装这一层插件)静默跳过,
|
|
202
|
-
* 别把整块装配掀掉;注册本身抛错照旧往上抛 —— 那是真的坏了,不该当成「没有这个 profile」。
|
|
203
|
-
*/
|
|
204
|
-
function mountSupervisorTools(ctx) {
|
|
205
|
-
if (typeof ctx.inject !== 'function') return;
|
|
206
|
-
ctx.inject(['tools'], (scoped) => {
|
|
207
|
-
if (!scoped || !scoped.tools || typeof scoped.tools.register !== 'function') return;
|
|
208
|
-
let defineTool;
|
|
209
|
-
try {
|
|
210
|
-
({ defineTool } = require('@deepseek-ai/dsh-tools'));
|
|
211
|
-
} catch {
|
|
212
|
-
return;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
// ── 读:列所有会话 ────────────────────────────────────────────────────
|
|
216
|
-
scoped.tools.register(defineTool({
|
|
217
|
-
name: 'supervisor_list_sessions',
|
|
218
|
-
description: 'List every conversation on this computer with its current state: whether it is running, finished its last turn, ended in an error, or is waiting for the user to answer something. Use this first to see what is going on and to get the sessionId of the conversation you want to inspect or act on.',
|
|
219
|
-
parameters: {},
|
|
220
|
-
output: {
|
|
221
|
-
schema: OUTPUT_FREE,
|
|
222
|
-
render: (_args, value) => {
|
|
223
|
-
const rows = Array.isArray(value && value.sessions) ? value.sessions : [];
|
|
224
|
-
if (rows.length === 0) return [{ type: 'text', text: '这台电脑上现在没有会话。' }];
|
|
225
|
-
return [{ type: 'text', text: `${rows.length} 条会话:\n${rows.map(rowLine).join('\n')}` }];
|
|
226
|
-
},
|
|
227
|
-
},
|
|
228
|
-
execute: async () => {
|
|
229
|
-
const { rows } = await readOverview(ctx);
|
|
230
|
-
return clean({ sessions: rows });
|
|
231
|
-
},
|
|
232
|
-
}));
|
|
233
|
-
|
|
234
|
-
// ── 读:某条会话跑到哪一步 ────────────────────────────────────────────
|
|
235
|
-
scoped.tools.register(defineTool({
|
|
236
|
-
name: 'supervisor_session_status',
|
|
237
|
-
description: 'Read where one conversation is right now: running / finished / errored / waiting for an answer, plus the fact of its last turn (when it ended and how). Give the sessionId from supervisor_list_sessions.',
|
|
238
|
-
parameters: {
|
|
239
|
-
sessionId: { type: 'string', description: 'The conversation to inspect.' },
|
|
240
|
-
},
|
|
241
|
-
output: {
|
|
242
|
-
schema: OUTPUT_FREE,
|
|
243
|
-
render: (_args, value) => [{ type: 'text', text: rowLine(value || {}) }],
|
|
244
|
-
},
|
|
245
|
-
execute: async (args) => {
|
|
246
|
-
const id = requiredId(args.sessionId, 'sessionId');
|
|
247
|
-
const { rows, metaById } = await readOverview(ctx);
|
|
248
|
-
const found = rows.find((row) => row.sessionId === id);
|
|
249
|
-
if (found) return clean(found);
|
|
250
|
-
if (metaById.has(id)) throw new Error(`会话 ${id} 现在不在可监控清单里(可能已归档)。`);
|
|
251
|
-
throw new Error(`找不到会话 ${id},先用 supervisor_list_sessions 看有哪些。`);
|
|
252
|
-
},
|
|
253
|
-
}));
|
|
254
|
-
|
|
255
|
-
// ── 读:某条会话的正文 ────────────────────────────────────────────────
|
|
256
|
-
scoped.tools.register(defineTool({
|
|
257
|
-
name: 'supervisor_read_session',
|
|
258
|
-
description: 'Read the actual messages of one conversation (who said what), newest window first. Use it when you need the content, not just the status.',
|
|
259
|
-
parameters: {
|
|
260
|
-
sessionId: { type: 'string', description: 'The conversation to read.' },
|
|
261
|
-
limit: { type: 'integer', description: 'How many recent messages to read. Default 30, max 200.' },
|
|
262
|
-
},
|
|
263
|
-
output: {
|
|
264
|
-
schema: OUTPUT_FREE,
|
|
265
|
-
render: (_args, value) => {
|
|
266
|
-
const messages = Array.isArray(value && value.messages) ? value.messages : [];
|
|
267
|
-
if (messages.length === 0) return [{ type: 'text', text: '这条会话还没有正文。' }];
|
|
268
|
-
const text = messages.map((m) => `${m.role === 'user' ? '用户' : '助手'}:${m.text}`).join('\n');
|
|
269
|
-
return [{ type: 'text', text }];
|
|
270
|
-
},
|
|
271
|
-
},
|
|
272
|
-
execute: async (args) => {
|
|
273
|
-
const id = requiredId(args.sessionId, 'sessionId');
|
|
274
|
-
const rawLimit = Number(args.limit);
|
|
275
|
-
const limit = Number.isSafeInteger(rawLimit) && rawLimit > 0 ? Math.min(rawLimit, 200) : 30;
|
|
276
|
-
let snapshot;
|
|
277
|
-
try {
|
|
278
|
-
snapshot = await buildMessagesSnapshot(ctx, id, limit);
|
|
279
|
-
} catch (error) {
|
|
280
|
-
const message = (error && error.message) || '读取会话内容失败';
|
|
281
|
-
throw new Error(message);
|
|
282
|
-
}
|
|
283
|
-
if (!snapshot || snapshot.ok === false) {
|
|
284
|
-
const error = snapshot && snapshot.error;
|
|
285
|
-
throw new Error((error && error.message) || '读取会话内容失败');
|
|
286
|
-
}
|
|
287
|
-
const messages = (snapshot.items || [])
|
|
288
|
-
.filter((m) => m && typeof m === 'object' && typeof m.role === 'string')
|
|
289
|
-
.map((m) => ({ role: m.role, text: typeof m.text === 'string' ? m.text : '' }));
|
|
290
|
-
return clean({ sessionId: id, messages });
|
|
291
|
-
},
|
|
292
|
-
}));
|
|
293
|
-
|
|
294
|
-
// ── 写:往某条会话发消息 ──────────────────────────────────────────────
|
|
295
|
-
scoped.tools.register(defineTool({
|
|
296
|
-
name: 'supervisor_send_message',
|
|
297
|
-
description: 'Send a message into another conversation, as if the user typed it there. The target keeps running with its own agent and preset. Give the sessionId from supervisor_list_sessions and the text to send.',
|
|
298
|
-
parameters: {
|
|
299
|
-
sessionId: { type: 'string', description: 'The conversation to send into.' },
|
|
300
|
-
text: { type: 'string', description: 'The message text to send.' },
|
|
301
|
-
},
|
|
302
|
-
output: {
|
|
303
|
-
schema: OUTPUT_FREE,
|
|
304
|
-
render: (_args, value) => [{ type: 'text', text: `已发给 ${value && value.sessionId}:${(value && value.text) || ''}` }],
|
|
305
|
-
},
|
|
306
|
-
execute: async (args, exec) => {
|
|
307
|
-
// 发起人只认 exec.agent.id,参数里没有、也不许有「谁发的」这一格。
|
|
308
|
-
const callerId = sessionOf(exec);
|
|
309
|
-
if (!callerId) throw new Error('拿不到当前会话 id(exec.agent.id),这条消息不发送。');
|
|
310
|
-
const target = requiredId(args.sessionId, 'sessionId');
|
|
311
|
-
const text = cleanText(args.text);
|
|
312
|
-
if (target === callerId) throw new Error('不能给总管自己发消息。');
|
|
313
|
-
await requireSession(ctx, target);
|
|
314
|
-
let response;
|
|
315
|
-
try {
|
|
316
|
-
response = await callSessions(ctx, 'prompt', { sessionId: target, content: [{ type: 'text', text }] }, 'supervisor');
|
|
317
|
-
} catch (error) {
|
|
318
|
-
const normalized = promptErrorObject(error);
|
|
319
|
-
throw new Error(normalized.message || '发送失败');
|
|
320
|
-
}
|
|
321
|
-
const { error } = unwrap(response);
|
|
322
|
-
if (error) {
|
|
323
|
-
const normalized = promptErrorObject(error);
|
|
324
|
-
throw new Error(normalized.message || '发送失败');
|
|
325
|
-
}
|
|
326
|
-
return clean({ sessionId: target, text, sent: true });
|
|
327
|
-
},
|
|
328
|
-
}));
|
|
329
|
-
|
|
330
|
-
// ── 写:新建会话(可带 agentPreset) ──────────────────────────────────
|
|
331
|
-
scoped.tools.register(defineTool({
|
|
332
|
-
name: 'supervisor_create_session',
|
|
333
|
-
description: 'Create a new blank conversation. Give either workspaceId (a project on this computer) or cwd (an absolute path inside a registered workspace) to place it, and optionally an agentPreset id to start it with a specific preset. Returns the new sessionId, which you can then send a message into.',
|
|
334
|
-
parameters: {
|
|
335
|
-
workspaceId: { type: 'string', description: 'The project (workspace) to create the conversation in. Give this or cwd, not both.' },
|
|
336
|
-
cwd: { type: 'string', description: 'An absolute directory inside a registered workspace. Give this or workspaceId, not both.' },
|
|
337
|
-
agentPreset: { type: 'string', description: 'Optional Agent preset id to start the conversation with.' },
|
|
338
|
-
},
|
|
339
|
-
output: {
|
|
340
|
-
schema: OUTPUT_FREE,
|
|
341
|
-
render: (_args, value) => [{ type: 'text', text: `已新建会话 ${value && value.sessionId}` }],
|
|
342
|
-
},
|
|
343
|
-
execute: async (args, exec) => {
|
|
344
|
-
const callerId = sessionOf(exec);
|
|
345
|
-
if (!callerId) throw new Error('拿不到当前会话 id(exec.agent.id),不能新建会话。');
|
|
346
|
-
const workspaceId = optionalId(args.workspaceId, 'workspaceId');
|
|
347
|
-
const cwdRaw = typeof args.cwd === 'string' ? args.cwd.trim() : '';
|
|
348
|
-
const agentPreset = optionalId(args.agentPreset, 'agentPreset');
|
|
349
|
-
if (workspaceId && cwdRaw) throw new Error('workspaceId 和 cwd 只能给一个。');
|
|
350
|
-
|
|
351
|
-
const payload = {};
|
|
352
|
-
if (workspaceId) payload.workspaceId = workspaceId;
|
|
353
|
-
if (agentPreset) payload.agentPreset = agentPreset;
|
|
354
|
-
if (cwdRaw) {
|
|
355
|
-
if (!path.isAbsolute(cwdRaw)) throw new Error('cwd 必须是绝对路径。');
|
|
356
|
-
const cwd = path.resolve(cwdRaw);
|
|
357
|
-
let roots;
|
|
358
|
-
try {
|
|
359
|
-
roots = await workspaceRoots(ctx);
|
|
360
|
-
} catch (error) {
|
|
361
|
-
throw new Error((error && error.message) || '读工作区清单失败');
|
|
362
|
-
}
|
|
363
|
-
if (!roots.some((root) => isWithin(root, cwd))) {
|
|
364
|
-
throw new Error('这个目录不在能建会话的范围里(只能落在已登记的工作区)。');
|
|
365
|
-
}
|
|
366
|
-
payload.cwd = cwd;
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
let response;
|
|
370
|
-
try {
|
|
371
|
-
response = await callSessions(ctx, 'create', payload);
|
|
372
|
-
} catch (error) {
|
|
373
|
-
throw new Error((error && error.message) || '新建会话失败');
|
|
374
|
-
}
|
|
375
|
-
const { value, error } = unwrap(response);
|
|
376
|
-
if (error) throw new Error(error.message || '新建会话失败');
|
|
377
|
-
const sessionId = value && typeof value.sessionId === 'string' ? value.sessionId : '';
|
|
378
|
-
if (!sessionId) throw new Error('宿主新建会话没返回 sessionId。');
|
|
379
|
-
// 新会话要立刻出现在列表里,否则手机上最长 10 秒看不到它(与 handleNew 同口径)。
|
|
380
|
-
try { invalidateSessionList(ctx); } catch { /* 缓存层不在也不挡这一发 */ }
|
|
381
|
-
const out = { sessionId };
|
|
382
|
-
if (payload.cwd) out.cwd = payload.cwd;
|
|
383
|
-
if (workspaceId) out.workspaceId = workspaceId;
|
|
384
|
-
if (value && typeof value.agentPreset === 'string' && value.agentPreset) out.agentPreset = value.agentPreset;
|
|
385
|
-
return clean(out);
|
|
386
|
-
},
|
|
387
|
-
}));
|
|
388
|
-
|
|
389
|
-
// ── 写:中断某条会话 ──────────────────────────────────────────────────
|
|
390
|
-
scoped.tools.register(defineTool({
|
|
391
|
-
name: 'supervisor_interrupt_session',
|
|
392
|
-
description: 'Stop what a conversation is currently doing. Give the sessionId of the running conversation.',
|
|
393
|
-
parameters: {
|
|
394
|
-
sessionId: { type: 'string', description: 'The running conversation to interrupt.' },
|
|
395
|
-
},
|
|
396
|
-
output: {
|
|
397
|
-
schema: OUTPUT_FREE,
|
|
398
|
-
render: (_args, value) => [{ type: 'text', text: `已中断 ${value && value.sessionId}` }],
|
|
399
|
-
},
|
|
400
|
-
execute: async (args, exec) => {
|
|
401
|
-
const callerId = sessionOf(exec);
|
|
402
|
-
if (!callerId) throw new Error('拿不到当前会话 id(exec.agent.id),不能中断。');
|
|
403
|
-
const target = requiredId(args.sessionId, 'sessionId');
|
|
404
|
-
await requireSession(ctx, target);
|
|
405
|
-
let response;
|
|
406
|
-
try {
|
|
407
|
-
response = await callSessions(ctx, 'cancel', { sessionId: target });
|
|
408
|
-
} catch (error) {
|
|
409
|
-
throw new Error((error && error.message) || '中断失败');
|
|
410
|
-
}
|
|
411
|
-
const { error } = unwrap(response);
|
|
412
|
-
if (error) throw new Error(error.message || '中断失败');
|
|
413
|
-
return clean({ sessionId: target, interrupted: true });
|
|
414
|
-
},
|
|
415
|
-
}));
|
|
416
|
-
});
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
module.exports = { mountSupervisorTools };
|