dsh-wecom-bot 0.4.2 → 0.5.1
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/README.md +160 -60
- package/dist/client.js +604 -383
- package/dist/host/bridge/commands.js +71 -20
- package/dist/host/bridge/commands.js.map +1 -1
- package/dist/host/bridge/directory.js +6 -8
- package/dist/host/bridge/directory.js.map +1 -1
- package/dist/host/bridge/index.js +124 -54
- package/dist/host/bridge/index.js.map +1 -1
- package/dist/host/bridge/interactions.js +61 -25
- package/dist/host/bridge/interactions.js.map +1 -1
- package/dist/host/bridge/notices.js +88 -3
- package/dist/host/bridge/notices.js.map +1 -1
- package/dist/host/bridge/outbox.js +11 -0
- package/dist/host/bridge/outbox.js.map +1 -1
- package/dist/host/bridge/policy.js +3 -4
- package/dist/host/bridge/policy.js.map +1 -1
- package/dist/host/bridge/selection.js +7 -0
- package/dist/host/bridge/selection.js.map +1 -1
- package/dist/host/catalog.js +24 -11
- package/dist/host/catalog.js.map +1 -1
- package/dist/host/config.js +31 -32
- package/dist/host/config.js.map +1 -1
- package/dist/host/groups-view.js +442 -143
- package/dist/host/groups-view.js.map +1 -1
- package/dist/host/index.js +11 -28
- package/dist/host/index.js.map +1 -1
- package/dist/host/interaction-transport.js +170 -0
- package/dist/host/interaction-transport.js.map +1 -0
- package/dist/host/journal.js +10 -3
- package/dist/host/journal.js.map +1 -1
- package/dist/host/observation.js +1 -1
- package/dist/host/observation.js.map +1 -1
- package/dist/host/routes.js +21 -13
- package/dist/host/routes.js.map +1 -1
- package/dist/host/routing.js +26 -13
- package/dist/host/routing.js.map +1 -1
- package/dist/host/runtime.js +142 -40
- package/dist/host/runtime.js.map +1 -1
- package/dist/host/session-ops.js +35 -3
- package/dist/host/session-ops.js.map +1 -1
- package/dist/host/session-title.js +47 -0
- package/dist/host/session-title.js.map +1 -0
- package/dist/host/sessions.js +38 -6
- package/dist/host/sessions.js.map +1 -1
- package/dist/host/{routing-store.js → store.js} +173 -26
- package/dist/host/store.js.map +1 -0
- package/dist/host/turns.js +5 -0
- package/dist/host/turns.js.map +1 -1
- package/dist/shared/protocol.js +18 -8
- package/dist/shared/protocol.js.map +1 -1
- package/package.json +1 -1
- package/dist/host/routing-store.js.map +0 -1
package/dist/host/groups-view.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { DEFAULT_GROUP_ID, MAX_GROUPS, } from '../shared/protocol.js';
|
|
2
|
-
import {
|
|
1
|
+
import { DEFAULT_GROUP_ID, MAX_GROUPS, MAX_LOG_RETENTION_DAYS, MAX_QUESTION_TIMEOUT_MS, MIN_LOG_RETENTION_DAYS, MIN_QUESTION_TIMEOUT_MS, } from '../shared/protocol.js';
|
|
2
|
+
import { isTrustedWsUrl, uniqueIds } from './config.js';
|
|
3
|
+
import { bindingKey, normalizeLabel, parseBindingKey, validBindingId } from './store.js';
|
|
3
4
|
async function memberView(deps, binding) {
|
|
4
5
|
const key = bindingKey(binding.kind, binding.id);
|
|
5
6
|
let sessionTitle = null;
|
|
@@ -18,6 +19,8 @@ async function memberView(deps, binding) {
|
|
|
18
19
|
sessionContext = described.context;
|
|
19
20
|
}
|
|
20
21
|
const seen = deps.observations?.stats(binding.kind, binding.id);
|
|
22
|
+
const group = deps.store.group(binding.groupId);
|
|
23
|
+
const sessionSelection = binding.sessionSelection ?? (group ? deps.provisioner.desired(group) : undefined);
|
|
21
24
|
return {
|
|
22
25
|
key,
|
|
23
26
|
kind: binding.kind,
|
|
@@ -30,6 +33,7 @@ async function memberView(deps, binding) {
|
|
|
30
33
|
sessionState,
|
|
31
34
|
sessionRunning,
|
|
32
35
|
...(sessionModel ? { sessionModel } : {}),
|
|
36
|
+
...(binding.sessionId && sessionSelection ? { sessionSelection } : {}),
|
|
33
37
|
...(sessionPreset ? { sessionPreset } : {}),
|
|
34
38
|
...(sessionContext ? { sessionContext } : {}),
|
|
35
39
|
messages: seen?.messages ?? 0,
|
|
@@ -106,15 +110,139 @@ export async function buildRoutingView(deps) {
|
|
|
106
110
|
full: deps.store.full(),
|
|
107
111
|
};
|
|
108
112
|
}
|
|
109
|
-
/**
|
|
110
|
-
|
|
113
|
+
/**
|
|
114
|
+
* Validate one settings patch, or answer with the message to show.
|
|
115
|
+
*
|
|
116
|
+
* The store coerces and clamps whatever it finds in the file, because the file is
|
|
117
|
+
* hand-editable content. An edit that arrives *from the page* is a different
|
|
118
|
+
* thing: it is a decision the operator just made, so an unusable WebSocket
|
|
119
|
+
* address or an out-of-range timeout is refused out loud instead of being
|
|
120
|
+
* silently rounded into something nobody asked for.
|
|
121
|
+
*/
|
|
122
|
+
export function validateSettingsPatch(patch) {
|
|
123
|
+
const record = patch;
|
|
124
|
+
const known = new Set([
|
|
125
|
+
'wsUrl', 'adminUserIds', 'adminWorkspaces', 'questionTimeoutMs', 'remoteApprovals', 'logEnabled', 'logRetentionDays',
|
|
126
|
+
]);
|
|
127
|
+
for (const key of Object.keys(record))
|
|
128
|
+
if (!known.has(key))
|
|
129
|
+
return `无法识别的设置项:${key}`;
|
|
130
|
+
const clean = {};
|
|
131
|
+
if (record.wsUrl !== undefined) {
|
|
132
|
+
const url = typeof record.wsUrl === 'string' ? record.wsUrl.trim() : '';
|
|
133
|
+
if (url && !isTrustedWsUrl(url))
|
|
134
|
+
return 'WebSocket 地址必须是不含账号密码的 wss:// 地址。';
|
|
135
|
+
clean.wsUrl = url;
|
|
136
|
+
}
|
|
137
|
+
if (record.adminUserIds !== undefined) {
|
|
138
|
+
if (!Array.isArray(record.adminUserIds))
|
|
139
|
+
return '管理员列表不正确。';
|
|
140
|
+
for (const id of record.adminUserIds)
|
|
141
|
+
if (!validBindingId(id))
|
|
142
|
+
return '需要有效的企微 userid。';
|
|
143
|
+
clean.adminUserIds = uniqueIds(record.adminUserIds);
|
|
144
|
+
}
|
|
145
|
+
if (record.adminWorkspaces !== undefined) {
|
|
146
|
+
if (!Array.isArray(record.adminWorkspaces))
|
|
147
|
+
return '通知范围不正确。';
|
|
148
|
+
for (const path of record.adminWorkspaces) {
|
|
149
|
+
if (typeof path !== 'string' || !path.trim() || path.length > 1024)
|
|
150
|
+
return '通知范围里的 Workspace 路径不正确。';
|
|
151
|
+
}
|
|
152
|
+
clean.adminWorkspaces = uniqueIds(record.adminWorkspaces);
|
|
153
|
+
}
|
|
154
|
+
if (record.questionTimeoutMs !== undefined) {
|
|
155
|
+
const ms = Number(record.questionTimeoutMs);
|
|
156
|
+
if (!Number.isFinite(ms) || ms < MIN_QUESTION_TIMEOUT_MS || ms > MAX_QUESTION_TIMEOUT_MS) {
|
|
157
|
+
return `「等待企微回复」需要在 ${MIN_QUESTION_TIMEOUT_MS / 1000}–${MAX_QUESTION_TIMEOUT_MS / 1000} 秒之间。`;
|
|
158
|
+
}
|
|
159
|
+
clean.questionTimeoutMs = Math.round(ms);
|
|
160
|
+
}
|
|
161
|
+
if (record.remoteApprovals !== undefined) {
|
|
162
|
+
if (typeof record.remoteApprovals !== 'boolean')
|
|
163
|
+
return '「远程审批」只能是开或关。';
|
|
164
|
+
clean.remoteApprovals = record.remoteApprovals;
|
|
165
|
+
}
|
|
166
|
+
if (record.logEnabled !== undefined) {
|
|
167
|
+
if (typeof record.logEnabled !== 'boolean')
|
|
168
|
+
return '「对话日志」只能是开或关。';
|
|
169
|
+
clean.logEnabled = record.logEnabled;
|
|
170
|
+
}
|
|
171
|
+
if (record.logRetentionDays !== undefined) {
|
|
172
|
+
const days = Number(record.logRetentionDays);
|
|
173
|
+
if (!Number.isInteger(days) || days < MIN_LOG_RETENTION_DAYS || days > MAX_LOG_RETENTION_DAYS) {
|
|
174
|
+
return `「日志保留天数」需要在 ${MIN_LOG_RETENTION_DAYS}–${MAX_LOG_RETENTION_DAYS} 天之间。`;
|
|
175
|
+
}
|
|
176
|
+
clean.logRetentionDays = days;
|
|
177
|
+
}
|
|
178
|
+
return clean;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Decide, before a group change is committed, whether that group's remembered
|
|
182
|
+
* Session may be restored.
|
|
183
|
+
*
|
|
184
|
+
* The store restores the candidate as part of the move itself, so an unusable
|
|
185
|
+
* candidate has to be dropped from the history *first*: validating after the
|
|
186
|
+
* write leaves it routable while the check awaits, and a failed check would
|
|
187
|
+
* leave it bound for good. An unreadable Workspace or catalog also refuses the
|
|
188
|
+
* move instead of silently discarding a Session the operator still owns.
|
|
189
|
+
*/
|
|
190
|
+
async function prepareRestore(deps, key, binding, groupId) {
|
|
191
|
+
const memory = binding.sessionsByGroup?.[groupId];
|
|
192
|
+
if (!memory)
|
|
193
|
+
return undefined;
|
|
194
|
+
const group = deps.store.group(groupId);
|
|
195
|
+
if (!group)
|
|
196
|
+
return `分组不存在:${groupId}`;
|
|
197
|
+
deps.catalogs.invalidate?.();
|
|
198
|
+
let workspacePath;
|
|
199
|
+
try {
|
|
200
|
+
workspacePath = (await deps.workspaces.describe(group.workspace)).path;
|
|
201
|
+
}
|
|
202
|
+
catch (error) {
|
|
203
|
+
return `无法读取分组「${group.name}」的 Workspace:${error instanceof Error ? error.message : String(error)}`;
|
|
204
|
+
}
|
|
205
|
+
if (deps.store.get(key) !== binding || deps.store.group(groupId) !== group)
|
|
206
|
+
return '成员信息已变化,请刷新后重试。';
|
|
207
|
+
let state;
|
|
208
|
+
let sessionPath;
|
|
209
|
+
try {
|
|
210
|
+
const described = await deps.provisioner.describe(memory.sessionId);
|
|
211
|
+
state = described.state;
|
|
212
|
+
sessionPath = described.workspacePath;
|
|
213
|
+
}
|
|
214
|
+
catch {
|
|
215
|
+
return '暂时无法确认旧会话的状态,请稍后重试。';
|
|
216
|
+
}
|
|
217
|
+
if (deps.store.get(key) !== binding || deps.store.group(groupId) !== group)
|
|
218
|
+
return '成员信息已变化,请刷新后重试。';
|
|
219
|
+
if (['bound', 'blank'].includes(state) && sessionPath === workspacePath)
|
|
220
|
+
return undefined;
|
|
221
|
+
// Unusable in this group: forget the candidate before the move so nothing is bound.
|
|
222
|
+
await deps.store.forgetSession(key, groupId);
|
|
223
|
+
if (deps.store.get(key)?.groupId !== binding.groupId)
|
|
224
|
+
return '成员信息已变化,请刷新后重试。';
|
|
225
|
+
return undefined;
|
|
226
|
+
}
|
|
227
|
+
/** Apply settings-page edits; the first failing op stops the rest. */
|
|
228
|
+
export async function applyStoreOps(deps, ops) {
|
|
229
|
+
await deps.store.load();
|
|
230
|
+
const initialBindings = new Map(deps.store.bindings().map(binding => [bindingKey(binding.kind, binding.id), binding]));
|
|
231
|
+
const initialAdmins = new Set(deps.store.settings().adminUserIds);
|
|
111
232
|
let changed = 0;
|
|
233
|
+
let settingsChanged = false;
|
|
112
234
|
let createdGroupId;
|
|
113
|
-
|
|
235
|
+
let failure;
|
|
236
|
+
/** Newly authorized principals, keyed by binding key (a later move replaces this). */
|
|
237
|
+
const authorized = new Map();
|
|
238
|
+
/** Principals whose group changed, keyed the same way so one batch notifies once. */
|
|
239
|
+
const assigned = new Map();
|
|
240
|
+
/** Administrators this batch added; a person already on the list is not promoted again. */
|
|
241
|
+
const promotedAdmins = new Set();
|
|
114
242
|
// A model edit is only worth accepting against the real catalog; read it
|
|
115
243
|
// once when the page has never asked for it on this process yet.
|
|
116
|
-
const touchesModel = ops.some(op => op.op === 'group.update'
|
|
117
|
-
&& (op.patch.provider !== undefined || op.patch.model !== undefined));
|
|
244
|
+
const touchesModel = ops.some(op => op.op === 'member.model' || (op.op === 'group.update'
|
|
245
|
+
&& (op.patch.provider !== undefined || op.patch.model !== undefined)));
|
|
118
246
|
if (touchesModel && !deps.models.snapshot()) {
|
|
119
247
|
try {
|
|
120
248
|
await deps.models.load();
|
|
@@ -136,159 +264,310 @@ export async function applyRoutingOps(deps, ops) {
|
|
|
136
264
|
}
|
|
137
265
|
const modelChanged = new Set();
|
|
138
266
|
for (const op of ops.slice(0, 200)) {
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
createdGroupId = created.id;
|
|
147
|
-
changed++;
|
|
148
|
-
}
|
|
149
|
-
catch (error) {
|
|
150
|
-
return { ok: false, error: `无法新增分组:${error instanceof Error ? error.message : String(error)}`, changed };
|
|
151
|
-
}
|
|
152
|
-
break;
|
|
153
|
-
}
|
|
154
|
-
case 'group.update': {
|
|
155
|
-
const group = deps.store.group(op.id);
|
|
156
|
-
if (!group)
|
|
157
|
-
return { ok: false, error: `分组不存在:${op.id}`, changed };
|
|
158
|
-
const patch = op.patch;
|
|
159
|
-
if (patch.workspace !== undefined && patch.workspace.trim()) {
|
|
267
|
+
try {
|
|
268
|
+
switch (op.op) {
|
|
269
|
+
case 'group.add': {
|
|
270
|
+
if (deps.store.groups().length >= MAX_GROUPS) {
|
|
271
|
+
failure = `最多只能有 ${MAX_GROUPS} 个分组。`;
|
|
272
|
+
break;
|
|
273
|
+
}
|
|
160
274
|
try {
|
|
161
|
-
await deps.
|
|
275
|
+
const created = await deps.store.createGroup(op.name);
|
|
276
|
+
createdGroupId = created.id;
|
|
277
|
+
changed++;
|
|
162
278
|
}
|
|
163
279
|
catch (error) {
|
|
164
|
-
|
|
280
|
+
failure = `无法新增分组:${error instanceof Error ? error.message : String(error)}`;
|
|
165
281
|
}
|
|
282
|
+
break;
|
|
166
283
|
}
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
284
|
+
case 'group.update': {
|
|
285
|
+
const group = deps.store.group(op.id);
|
|
286
|
+
if (!group) {
|
|
287
|
+
failure = `分组不存在:${op.id}`;
|
|
288
|
+
break;
|
|
289
|
+
}
|
|
290
|
+
const patch = op.patch;
|
|
291
|
+
const fields = new Set(['name', 'workspace', 'provider', 'model', 'reasoningEffort', 'preset']);
|
|
292
|
+
if (Object.entries(patch).some(([key, value]) => !fields.has(key) || typeof value !== 'string')) {
|
|
293
|
+
failure = '分组设置只能包含名称、Workspace、模型、effort 与 preset,且必须是文本。';
|
|
294
|
+
break;
|
|
295
|
+
}
|
|
296
|
+
if (patch.workspace !== undefined && patch.workspace.trim()) {
|
|
297
|
+
try {
|
|
298
|
+
await deps.workspaces.resolve(patch.workspace);
|
|
299
|
+
}
|
|
300
|
+
catch (error) {
|
|
301
|
+
failure = error instanceof Error ? error.message : String(error);
|
|
302
|
+
break;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
const nextProvider = (patch.provider ?? group.provider).trim();
|
|
306
|
+
const nextModel = (patch.model ?? group.model).trim();
|
|
307
|
+
if (Boolean(nextProvider) !== Boolean(nextModel)) {
|
|
308
|
+
failure = '请同时选择服务商与模型,或都留空以跟随 DSH 默认模型。';
|
|
309
|
+
break;
|
|
310
|
+
}
|
|
311
|
+
if (nextProvider && nextModel && !deps.models.known({ provider: nextProvider, model: nextModel })) {
|
|
312
|
+
failure = `DSH 当前没有模型 ${nextProvider}/${nextModel},请重新选择。`;
|
|
313
|
+
break;
|
|
314
|
+
}
|
|
315
|
+
const nextPreset = (patch.preset ?? group.preset).trim();
|
|
316
|
+
// Without a preset service there is nothing to validate against.
|
|
317
|
+
if (nextPreset && deps.presets && !deps.presets.known(nextPreset)) {
|
|
318
|
+
failure = `DSH 当前没有 preset ${nextPreset},请重新选择。`;
|
|
319
|
+
break;
|
|
320
|
+
}
|
|
321
|
+
const updated = await deps.store.updateGroup(op.id, patch);
|
|
322
|
+
if (!updated) {
|
|
323
|
+
failure = `分组不存在:${op.id}`;
|
|
324
|
+
break;
|
|
325
|
+
}
|
|
326
|
+
changed++;
|
|
327
|
+
if (patch.provider !== undefined || patch.model !== undefined || patch.reasoningEffort !== undefined) {
|
|
328
|
+
modelChanged.add(op.id);
|
|
329
|
+
}
|
|
330
|
+
// A group's Workspace defines where its Sessions live, so moving the
|
|
331
|
+
// group must not leave members pointing at Sessions of the old one.
|
|
332
|
+
if (updated.workspace !== group.workspace) {
|
|
333
|
+
for (const member of deps.store.inGroup(op.id)) {
|
|
334
|
+
// Replace even an empty row: provisioning may currently be creating
|
|
335
|
+
// its first Session in the old Workspace and must fail its CAS.
|
|
336
|
+
await deps.store.update(bindingKey(member.kind, member.id), { sessionId: '' });
|
|
337
|
+
changed++;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
break;
|
|
179
341
|
}
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
342
|
+
case 'group.delete': {
|
|
343
|
+
if (op.id === DEFAULT_GROUP_ID) {
|
|
344
|
+
failure = '默认分组不能删除。';
|
|
345
|
+
break;
|
|
346
|
+
}
|
|
347
|
+
const result = await deps.store.removeGroup(op.id);
|
|
348
|
+
if (!result.ok) {
|
|
349
|
+
failure = `分组不存在:${op.id}`;
|
|
350
|
+
break;
|
|
351
|
+
}
|
|
352
|
+
changed += 1 + result.removed;
|
|
353
|
+
break;
|
|
186
354
|
}
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
355
|
+
case 'member.add': {
|
|
356
|
+
const id = typeof op.id === 'string' ? op.id.trim() : '';
|
|
357
|
+
if ((op.kind !== 'user' && op.kind !== 'group') || !validBindingId(id)) {
|
|
358
|
+
failure = '需要有效的企微 userid(个人)或 chatid(群聊)。';
|
|
359
|
+
break;
|
|
360
|
+
}
|
|
361
|
+
if (!deps.store.group(op.groupId)) {
|
|
362
|
+
failure = `分组不存在:${op.groupId}`;
|
|
363
|
+
break;
|
|
364
|
+
}
|
|
365
|
+
try {
|
|
366
|
+
const previous = deps.store.get(bindingKey(op.kind, id));
|
|
367
|
+
if (previous && previous.groupId !== op.groupId) {
|
|
368
|
+
const restoreError = await prepareRestore(deps, bindingKey(op.kind, id), previous, op.groupId);
|
|
369
|
+
if (restoreError) {
|
|
370
|
+
failure = restoreError;
|
|
371
|
+
break;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
await deps.store.create(op.kind, id, {
|
|
375
|
+
groupId: op.groupId, ...(op.label === undefined ? {} : { label: op.label }),
|
|
376
|
+
});
|
|
194
377
|
changed++;
|
|
195
378
|
}
|
|
379
|
+
catch (error) {
|
|
380
|
+
failure = `无法加入分组:${error instanceof Error ? error.message : String(error)}`;
|
|
381
|
+
}
|
|
382
|
+
break;
|
|
196
383
|
}
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
384
|
+
case 'member.update': {
|
|
385
|
+
const parsed = parseBindingKey(op.key);
|
|
386
|
+
if (!parsed) {
|
|
387
|
+
failure = `无法识别的成员 id:${op.key}`;
|
|
388
|
+
break;
|
|
389
|
+
}
|
|
390
|
+
const key = bindingKey(parsed.kind, parsed.id);
|
|
391
|
+
const existing = deps.store.get(key);
|
|
392
|
+
if (!existing) {
|
|
393
|
+
failure = `成员不存在:${op.key}`;
|
|
394
|
+
break;
|
|
395
|
+
}
|
|
396
|
+
if (Object.entries(op.patch).some(([key, value]) => !['groupId', 'sessionId'].includes(key) || typeof value !== 'string')) {
|
|
397
|
+
failure = '成员设置只能包含文本形式的分组或会话 ID。';
|
|
398
|
+
break;
|
|
399
|
+
}
|
|
400
|
+
if (op.patch.groupId !== undefined && !deps.store.group(op.patch.groupId)) {
|
|
401
|
+
failure = `分组不存在:${op.patch.groupId}`;
|
|
402
|
+
break;
|
|
403
|
+
}
|
|
404
|
+
if (op.patch.groupId !== undefined && op.patch.groupId !== existing.groupId) {
|
|
405
|
+
const restoreError = await prepareRestore(deps, key, existing, op.patch.groupId);
|
|
406
|
+
if (restoreError) {
|
|
407
|
+
failure = restoreError;
|
|
408
|
+
break;
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
const updated = await deps.store.update(key, op.patch);
|
|
412
|
+
if (!updated) {
|
|
413
|
+
failure = `成员不存在:${op.key}`;
|
|
414
|
+
break;
|
|
415
|
+
}
|
|
218
416
|
changed++;
|
|
417
|
+
break;
|
|
219
418
|
}
|
|
220
|
-
|
|
221
|
-
|
|
419
|
+
case 'member.model': {
|
|
420
|
+
const parsed = parseBindingKey(op.key);
|
|
421
|
+
if (!parsed) {
|
|
422
|
+
failure = `无法识别的成员 id:${op.key}`;
|
|
423
|
+
break;
|
|
424
|
+
}
|
|
425
|
+
const key = bindingKey(parsed.kind, parsed.id);
|
|
426
|
+
const binding = deps.store.get(key);
|
|
427
|
+
if (!binding || !binding.sessionId || binding.sessionId !== op.sessionId) {
|
|
428
|
+
failure = '成员的绑定会话已变化,请刷新后重试。';
|
|
429
|
+
break;
|
|
430
|
+
}
|
|
431
|
+
const raw = op.selection;
|
|
432
|
+
if (!raw || typeof raw !== 'object' || Array.isArray(raw)
|
|
433
|
+
|| Object.entries(raw).some(([key, value]) => !['provider', 'model', 'reasoningEffort'].includes(key) || typeof value !== 'string')
|
|
434
|
+
|| !raw.provider?.trim() || !raw.model?.trim()) {
|
|
435
|
+
failure = '请选择有效的服务商、模型与 effort。';
|
|
436
|
+
break;
|
|
437
|
+
}
|
|
438
|
+
const selection = {
|
|
439
|
+
provider: raw.provider.trim(), model: raw.model.trim(),
|
|
440
|
+
...(raw.reasoningEffort?.trim() ? { reasoningEffort: raw.reasoningEffort.trim() } : {}),
|
|
441
|
+
};
|
|
442
|
+
const catalog = deps.models.snapshot();
|
|
443
|
+
const choice = catalog?.choices.find(item => item.provider === selection.provider && item.model === selection.model);
|
|
444
|
+
if (!choice || !deps.models.known(selection)) {
|
|
445
|
+
failure = 'DSH 当前没有这个模型,请重新选择。';
|
|
446
|
+
break;
|
|
447
|
+
}
|
|
448
|
+
if (selection.reasoningEffort && !choice.efforts?.some(item => item.id === selection.reasoningEffort)) {
|
|
449
|
+
failure = '这个模型不支持所选的 effort,请重新选择。';
|
|
450
|
+
break;
|
|
451
|
+
}
|
|
452
|
+
const described = await deps.provisioner.describe(binding.sessionId);
|
|
453
|
+
if (!['bound', 'blank'].includes(described.state)) {
|
|
454
|
+
failure = '绑定的会话已不可用,请先发送消息创建新会话。';
|
|
455
|
+
break;
|
|
456
|
+
}
|
|
457
|
+
if (deps.store.get(key) !== binding) {
|
|
458
|
+
failure = '成员的绑定会话已变化,请刷新后重试。';
|
|
459
|
+
break;
|
|
460
|
+
}
|
|
461
|
+
const selected = await deps.provisioner.applyModel(binding.sessionId, selection);
|
|
462
|
+
const updated = await deps.store.update(key, { sessionSelection: selected }, binding);
|
|
463
|
+
if (!updated) {
|
|
464
|
+
failure = '成员的绑定会话已变化,模型未保存到新绑定,请刷新后重试。';
|
|
465
|
+
break;
|
|
466
|
+
}
|
|
467
|
+
deps.catalogs.invalidate?.();
|
|
468
|
+
changed++;
|
|
469
|
+
break;
|
|
222
470
|
}
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
471
|
+
case 'label.set': {
|
|
472
|
+
const parsed = parseBindingKey(op.key);
|
|
473
|
+
if (!parsed) {
|
|
474
|
+
failure = `无法识别的人员 id:${op.key}`;
|
|
475
|
+
break;
|
|
476
|
+
}
|
|
477
|
+
const key = bindingKey(parsed.kind, parsed.id);
|
|
478
|
+
if (typeof op.label !== 'string') {
|
|
479
|
+
failure = '备注必须是文本。';
|
|
480
|
+
break;
|
|
481
|
+
}
|
|
482
|
+
if (op.label.trim() && !normalizeLabel(op.label)) {
|
|
483
|
+
failure = '备注只能是单行文本。';
|
|
484
|
+
break;
|
|
485
|
+
}
|
|
486
|
+
await deps.store.setLabel(key, op.label);
|
|
487
|
+
changed++;
|
|
488
|
+
break;
|
|
234
489
|
}
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
490
|
+
case 'member.archive': {
|
|
491
|
+
const parsed = parseBindingKey(op.key);
|
|
492
|
+
if (!parsed) {
|
|
493
|
+
failure = `无法识别的成员 id:${op.key}`;
|
|
494
|
+
break;
|
|
495
|
+
}
|
|
496
|
+
const key = bindingKey(parsed.kind, parsed.id);
|
|
497
|
+
const binding = deps.store.get(key);
|
|
498
|
+
if (!binding) {
|
|
499
|
+
failure = `成员不存在:${op.key}`;
|
|
500
|
+
break;
|
|
501
|
+
}
|
|
502
|
+
if (!binding.sessionId) {
|
|
503
|
+
failure = '这个成员还没有会话可以归档。';
|
|
504
|
+
break;
|
|
505
|
+
}
|
|
506
|
+
if (!deps.ops) {
|
|
507
|
+
failure = '当前 DSH 不支持归档会话。';
|
|
508
|
+
break;
|
|
509
|
+
}
|
|
510
|
+
try {
|
|
511
|
+
await deps.ops.archive(binding.sessionId);
|
|
512
|
+
}
|
|
513
|
+
catch (error) {
|
|
514
|
+
failure = error instanceof Error ? error.message : String(error);
|
|
515
|
+
break;
|
|
516
|
+
}
|
|
517
|
+
// Clearing the binding is what makes the next message create a fresh
|
|
518
|
+
// Session from the group's current Workspace/model/preset.
|
|
519
|
+
await deps.store.update(key, { sessionId: '' });
|
|
520
|
+
changed++;
|
|
521
|
+
break;
|
|
250
522
|
}
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
return { ok: false, error: '这个成员还没有会话可以归档。', changed };
|
|
265
|
-
if (!deps.ops)
|
|
266
|
-
return { ok: false, error: '当前 DSH 不支持归档会话。', changed };
|
|
267
|
-
try {
|
|
268
|
-
await deps.ops.archive(binding.sessionId);
|
|
523
|
+
case 'member.remove': {
|
|
524
|
+
const parsed = parseBindingKey(op.key);
|
|
525
|
+
if (!parsed) {
|
|
526
|
+
failure = `无法识别的成员 id:${op.key}`;
|
|
527
|
+
break;
|
|
528
|
+
}
|
|
529
|
+
const removed = await deps.store.remove(bindingKey(parsed.kind, parsed.id));
|
|
530
|
+
if (!removed) {
|
|
531
|
+
failure = `成员不存在:${op.key}`;
|
|
532
|
+
break;
|
|
533
|
+
}
|
|
534
|
+
changed++;
|
|
535
|
+
break;
|
|
269
536
|
}
|
|
270
|
-
|
|
271
|
-
|
|
537
|
+
case 'settings.set': {
|
|
538
|
+
if (!op.patch || typeof op.patch !== 'object') {
|
|
539
|
+
failure = '设置内容不正确。';
|
|
540
|
+
break;
|
|
541
|
+
}
|
|
542
|
+
const validated = validateSettingsPatch(op.patch);
|
|
543
|
+
if (typeof validated === 'string') {
|
|
544
|
+
failure = validated;
|
|
545
|
+
break;
|
|
546
|
+
}
|
|
547
|
+
try {
|
|
548
|
+
await deps.store.setSettings(validated);
|
|
549
|
+
}
|
|
550
|
+
catch (error) {
|
|
551
|
+
failure = `无法保存设置:${error instanceof Error ? error.message : String(error)}`;
|
|
552
|
+
break;
|
|
553
|
+
}
|
|
554
|
+
settingsChanged = true;
|
|
555
|
+
changed++;
|
|
556
|
+
break;
|
|
272
557
|
}
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
changed++;
|
|
277
|
-
break;
|
|
278
|
-
}
|
|
279
|
-
case 'member.remove': {
|
|
280
|
-
const parsed = parseBindingKey(op.key);
|
|
281
|
-
if (!parsed)
|
|
282
|
-
return { ok: false, error: `无法识别的成员 id:${op.key}`, changed };
|
|
283
|
-
const removed = await deps.store.remove(bindingKey(parsed.kind, parsed.id));
|
|
284
|
-
if (!removed)
|
|
285
|
-
return { ok: false, error: `成员不存在:${op.key}`, changed };
|
|
286
|
-
changed++;
|
|
287
|
-
break;
|
|
558
|
+
default:
|
|
559
|
+
failure = '未知的修改类型。';
|
|
560
|
+
break;
|
|
288
561
|
}
|
|
289
|
-
default:
|
|
290
|
-
return { ok: false, error: '未知的修改类型。', changed };
|
|
291
562
|
}
|
|
563
|
+
catch (error) {
|
|
564
|
+
failure = `无法应用修改:${error instanceof Error ? error.message : String(error)}`;
|
|
565
|
+
}
|
|
566
|
+
// Earlier ops stay applied even when this one failed; the result reports
|
|
567
|
+
// both, so the runtime can adopt the settings and notify the people they
|
|
568
|
+
// authorized before the failure. Only the rest of the batch is skipped.
|
|
569
|
+
if (failure)
|
|
570
|
+
break;
|
|
292
571
|
}
|
|
293
572
|
// A group's model applies to every Session it already owns.
|
|
294
573
|
for (const id of modelChanged) {
|
|
@@ -299,10 +578,30 @@ export async function applyRoutingOps(deps, ops) {
|
|
|
299
578
|
deps.log?.warn(`WeCom could not apply the group model: ${error instanceof Error ? error.message : String(error)}`);
|
|
300
579
|
}
|
|
301
580
|
}
|
|
581
|
+
// Notifications describe the final effective assignment, not transient
|
|
582
|
+
// states within a batch (including a successful prefix of a failed batch).
|
|
583
|
+
for (const binding of deps.store.bindings()) {
|
|
584
|
+
const key = bindingKey(binding.kind, binding.id);
|
|
585
|
+
const previous = initialBindings.get(key);
|
|
586
|
+
const entry = { kind: binding.kind, id: binding.id, groupId: binding.groupId };
|
|
587
|
+
if (!previous)
|
|
588
|
+
authorized.set(key, entry);
|
|
589
|
+
else if (previous.groupId !== binding.groupId)
|
|
590
|
+
assigned.set(key, { ...entry, previousGroupId: previous.groupId });
|
|
591
|
+
}
|
|
592
|
+
for (const id of deps.store.settings().adminUserIds)
|
|
593
|
+
if (!initialAdmins.has(id))
|
|
594
|
+
promotedAdmins.add(id);
|
|
595
|
+
const assignedList = [...assigned.values()];
|
|
302
596
|
return {
|
|
303
|
-
ok:
|
|
597
|
+
ok: failure === undefined,
|
|
598
|
+
...(failure ? { error: failure } : {}),
|
|
599
|
+
changed,
|
|
600
|
+
...(settingsChanged ? { settingsChanged } : {}),
|
|
304
601
|
...(createdGroupId ? { createdGroupId } : {}),
|
|
305
|
-
...(authorized.
|
|
602
|
+
...(authorized.size ? { authorized: [...authorized.values()] } : {}),
|
|
603
|
+
...(assignedList.length ? { assigned: assignedList } : {}),
|
|
604
|
+
...(promotedAdmins.size ? { promotedAdmins: [...promotedAdmins] } : {}),
|
|
306
605
|
};
|
|
307
606
|
}
|
|
308
607
|
//# sourceMappingURL=groups-view.js.map
|