@240xu/dsh-message-ops 0.8.0 → 0.9.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/README.md +93 -7
- package/package.json +2 -2
- package/src/client.js +124 -107
- package/src/index.js +74 -102
- package/src/ops-core.js +213 -65
- package/test/branch-export-removed.test.js +73 -0
- package/test/fence.test.js +0 -27
- package/test/injected-regex.test.js +62 -0
- package/test/legacy-format.test.js +0 -17
- package/test/ops-core.test.js +42 -45
- package/test/revert-contract.test.js +118 -0
- package/test/session-file.test.js +0 -24
- package/test/step-restore.test.js +16 -0
- package/test/user-revert-anchor.test.js +103 -0
- package/src/branch.js +0 -57
package/src/index.js
CHANGED
|
@@ -5,16 +5,18 @@
|
|
|
5
5
|
* 五操作一条龙(同名插件,一个注入点):
|
|
6
6
|
* POST /api/message-ops/revert 回滚:surface replace 遮蔽 targetSeq..末尾
|
|
7
7
|
* POST /api/message-ops/delete 删除:surface replace 遮蔽单条 seq
|
|
8
|
-
* POST /api/message-ops/branch 分支:磁盘级 fork(新会话,parentSession 关联)
|
|
9
8
|
* GET /api/message-ops/messages 消息列表(磁盘真相 + 可见性标注)
|
|
10
|
-
* GET /api/message-ops/export 导出 Markdown(seq 可选上界,附件下载)
|
|
11
9
|
* POST /api/message-ops/restore 回滚恢复:重放被遮蔽的 user/assistant 消息
|
|
12
10
|
*
|
|
13
11
|
* 回滚/删除走 DSH 原生 surface replace(append-only,日志完整可恢复;
|
|
14
12
|
* 约束由引擎 assertProvenance 校验,违规如实上报 409)。
|
|
15
13
|
* 运行中的会话一律拒绝(409),由用户先停止再操作。
|
|
16
14
|
*
|
|
17
|
-
* Agent 工具:action revert/delete/
|
|
15
|
+
* Agent 工具:action revert/delete/list/restore 共用 HTTP 同一套
|
|
16
|
+
*
|
|
17
|
+
* 分支与导出不属本插件:dsh 官方已有 session/fork(任意 atSeq,且会补 fork closers)
|
|
18
|
+
* 与 /api/session.export(ZIP 归档)。0.9.0 移除了本插件的磁盘分支与 Markdown 导出 ——
|
|
19
|
+
* 前者语义有偏差(缺 isSeeded / inheritedEventCount / closers),后者零 UI 入口。
|
|
18
20
|
* 核心逻辑(src/ops-core.js)。工具注册是容错的——ctx 里没有 tools 服务、
|
|
19
21
|
* 或 @deepseek-ai/dsh-tools 包不可解析时,只跳过工具注册,不影响 HTTP。
|
|
20
22
|
* @module dsh-message-ops
|
|
@@ -23,9 +25,8 @@
|
|
|
23
25
|
import { zstdCompressSync } from "node:zlib";
|
|
24
26
|
import { open as openFile, truncate as truncateFile, stat as statFile } from "node:fs/promises";
|
|
25
27
|
import { isSessionId, sessionIdVariants, findSessionDirs, readSessionFile, readSessionFileAsync, listMessages, computeShadowed, messageText } from "./session-file.js";
|
|
26
|
-
import {
|
|
27
|
-
|
|
28
|
-
OpsError, planRevert, planDelete, planRestore, applyRestore, applySurfaceReplace, exportMarkdown,
|
|
28
|
+
import { restoreNoticeText, planRevertFromEvents, planDeleteFromEvents, nextSeqFrom, buildMarkerEvent, buildRestoreNoticeEvent, buildReplayEvent, planNoticeWindow, noticeWindowPreamble, noticeWindowPostamble,
|
|
29
|
+
OpsError, planRevert, planDelete, planRestore, applyRestore, applySurfaceReplace,
|
|
29
30
|
restoreProgress, pendingRestoreTurns,
|
|
30
31
|
isTrustedApiRequest, isJsonContentType,
|
|
31
32
|
} from "./ops-core.js";
|
|
@@ -303,8 +304,13 @@ async function opsCommit(targetCtx, mode, sessionId, seq) {
|
|
|
303
304
|
if (!Number.isSafeInteger(seq) || seq < 0) throw new OpsError("invalid seq", 400);
|
|
304
305
|
if (isRunning(targetCtx, sessionId)) throw new OpsError("session is running; stop it first", 409);
|
|
305
306
|
const { logPath } = pickSessionDir(sessionId);
|
|
306
|
-
const { events } = await readSessionFileAsync(logPath);
|
|
307
|
-
|
|
307
|
+
const { header, events } = await readSessionFileAsync(logPath);
|
|
308
|
+
// 0.8.2:空闲会话(实测 340/364)没有打开的 turn/step → 用自足的**合成 turn**
|
|
309
|
+
// 承载 notice,而不是拒绝。引擎 append-only 插不进已有 step,但合成一整个 turn 合法
|
|
310
|
+
//��已对官方校验闸实测:append 型 / 单节点 replace / 多节点 replace 全 PASS,遮蔽受保护头正确拒绝)。
|
|
311
|
+
// 带 reason:"message-ops",UI 与审计可识别,不与真实模型轮次混淆。
|
|
312
|
+
const win = planNoticeWindow(events);
|
|
313
|
+
const turnStep = { turn: win.turn, step: win.step };
|
|
308
314
|
const notice = mode === "delete"
|
|
309
315
|
? `[消息删除] 已遮蔽 seq ${seq}`
|
|
310
316
|
: null; // revert 文案含 shadowed 数量,规划后拼
|
|
@@ -317,10 +323,16 @@ async function opsCommit(targetCtx, mode, sessionId, seq) {
|
|
|
317
323
|
const plan = mode === "delete" ? planDelete(session.surface, seq) : planRevert(session.surface, seq);
|
|
318
324
|
const text = mode === "delete" ? notice
|
|
319
325
|
: `[消息回滚] 已回滚到 seq ${seq}(含)之后的 ${plan.shadowedSeqs.length} 个节点`;
|
|
326
|
+
// 0.8.2:会话 store 命中时走引擎路径 —— 同样要先开合成 turn 窗口,
|
|
327
|
+
// 否则 notice 落在已关闭的 turn/step 里,加载端拒绝整份历史。
|
|
328
|
+
for (const pre of noticeWindowPreamble(win)) session.append(pre.kind, pre.data);
|
|
320
329
|
const event = applySurfaceReplace(session, plan.startSeq, plan.endSeq, plan.shadowedSeqs, text, turnStep);
|
|
330
|
+
// 必须闭合:否则引擎内存里 turn 16 永远开着 → isRunning 恒真 → 后续操作全被 409
|
|
331
|
+
for (const post of noticeWindowPostamble(win)) session.append(post.kind, post.data);
|
|
321
332
|
flushSessions(targetCtx, session);
|
|
322
333
|
return {
|
|
323
334
|
ok: true, mode, seq,
|
|
335
|
+
syntheticTurn: win.synthetic === true,
|
|
324
336
|
shadowedCount: plan.shadowedSeqs.length,
|
|
325
337
|
eventSeq: event && event.seq != null ? event.seq : null,
|
|
326
338
|
};
|
|
@@ -332,30 +344,30 @@ async function opsCommit(targetCtx, mode, sessionId, seq) {
|
|
|
332
344
|
const plan = mode === "delete" ? planDeleteFromEvents(events, seq) : planRevertFromEvents(events, seq);
|
|
333
345
|
const text = mode === "delete" ? notice
|
|
334
346
|
: `[消息回滚] 已回滚到 seq ${seq}(含)之后的 ${plan.shadowedSeqs.length} 个节点`;
|
|
347
|
+
// 0.8.2:磁盘路径同样把 marker 包进合成 turn(裸 zstd 写、零校验,写错就是整份日志报废)
|
|
348
|
+
const baseTime = Date.now();
|
|
349
|
+
let cursor = nextSeqFrom(events);
|
|
350
|
+
const batch = noticeWindowPreamble(win).map((e, i) => ({
|
|
351
|
+
type: e.kind, seq: cursor++, time: baseTime + i, data: e.data,
|
|
352
|
+
}));
|
|
335
353
|
const marker = buildMarkerEvent({
|
|
336
|
-
seq:
|
|
354
|
+
seq: cursor++, time: baseTime + batch.length, turnStep, text, version: header?.version ?? 4,
|
|
337
355
|
startSeq: plan.startSeq, endSeq: plan.endSeq, shadowedSeqs: plan.shadowedSeqs,
|
|
338
356
|
});
|
|
339
|
-
|
|
357
|
+
batch.push(marker);
|
|
358
|
+
let tail = marker.time + 1;
|
|
359
|
+
for (const e of noticeWindowPostamble(win)) batch.push({ type: e.kind, seq: cursor++, time: tail++, data: e.data });
|
|
360
|
+
await diskAppend(logPath, batch);
|
|
340
361
|
return {
|
|
341
|
-
ok: true, mode, seq, disk: true,
|
|
362
|
+
ok: true, mode, seq, disk: true, syntheticTurn: win.synthetic === true,
|
|
342
363
|
shadowedCount: plan.shadowedSeqs.length,
|
|
343
364
|
eventSeq: marker.seq,
|
|
344
365
|
};
|
|
345
366
|
}
|
|
346
367
|
|
|
347
|
-
function opsBranch(targetCtx, sessionId, upToSeq) {
|
|
348
|
-
if (!isSessionId(sessionId)) throw new OpsError("invalid sessionId", 400);
|
|
349
|
-
if (!Number.isSafeInteger(upToSeq) || upToSeq < 0) throw new OpsError("invalid upToSeq", 400);
|
|
350
|
-
if (isRunning(targetCtx, sessionId)) {
|
|
351
|
-
throw new OpsError("session is running; stop it first (the log may be mid-append)", 409);
|
|
352
|
-
}
|
|
353
|
-
const { logPath } = pickSessionDir(sessionId);
|
|
354
|
-
return { ok: true, ...applyBranch(logPath, upToSeq) };
|
|
355
|
-
}
|
|
356
|
-
|
|
357
368
|
// 0.8.0:upToSeq 可选 —— 给定则按轮步进恢复(只重放到该 seq,后续仍遮蔽)。
|
|
358
|
-
|
|
369
|
+
// 0.8.1:opts.discard —— 不重放内容,仅停用标记(清贴条)。
|
|
370
|
+
async function opsRestore(targetCtx, sessionId, restoreSeq, upToSeq, discard) {
|
|
359
371
|
if (!isSessionId(sessionId)) throw new OpsError("invalid sessionId", 400);
|
|
360
372
|
if (!Number.isSafeInteger(restoreSeq) || restoreSeq < 0) throw new OpsError("invalid seq", 400);
|
|
361
373
|
if (upToSeq !== undefined && (!Number.isSafeInteger(upToSeq) || upToSeq < 0)) throw new OpsError("invalid upToSeq", 400);
|
|
@@ -364,8 +376,16 @@ async function opsRestore(targetCtx, sessionId, restoreSeq, upToSeq) {
|
|
|
364
376
|
const { events } = await readSessionFileAsync(logPath);
|
|
365
377
|
// 0.8.0(B1 修复):步进恢复必须排除先前轮次已重放的源 seq,
|
|
366
378
|
// 否则每轮从区间头重放 → 消息副本成倍出现。
|
|
379
|
+
// 0.8.2:空闲会话(实测 340/364)没有打开的 turn/step → 用自足的**合成 turn**
|
|
380
|
+
// 承载 notice,而不是拒绝。引擎 append-only 插不进已有 step,但合成一整个 turn 合法
|
|
381
|
+
//��已对官方校验闸实测:append 型 / 单节点 replace / 多节点 replace 全 PASS,遮蔽受保护头正确拒绝)。
|
|
382
|
+
// 带 reason:"message-ops",UI 与审计可识别,不与真实模型轮次混淆。
|
|
383
|
+
const win = planNoticeWindow(events);
|
|
367
384
|
const progress = restoreProgress(events, restoreSeq);
|
|
368
385
|
const plan = planRestore(events, restoreSeq, upToSeq, progress ? progress.restoredSeqs : null);
|
|
386
|
+
plan.turnStep = { turn: win.turn, step: win.step };
|
|
387
|
+
plan.window = win;
|
|
388
|
+
if (discard) plan.discard = true;
|
|
369
389
|
if (plan.replayable.length === 0 && progress && !progress.complete && progress.pendingSeqs.length > 0) {
|
|
370
390
|
// 请求的 upTo 之前已全部重放过 → 只补一条进度 notice(不重复重放)
|
|
371
391
|
plan.upToSeq = upToSeq != null ? Math.min(upToSeq, plan.endSeq) : plan.endSeq;
|
|
@@ -373,10 +393,12 @@ async function opsRestore(targetCtx, sessionId, restoreSeq, upToSeq) {
|
|
|
373
393
|
const acq = await acquireSession(targetCtx, sessionId);
|
|
374
394
|
if (acq !== null && acq.session !== undefined) {
|
|
375
395
|
try {
|
|
396
|
+
for (const pre of noticeWindowPreamble(win)) acq.session.append(pre.kind, pre.data);
|
|
376
397
|
const result = applyRestore(acq.session, plan, {
|
|
377
398
|
flush: () => { flushSessions(targetCtx, acq.session); },
|
|
378
399
|
});
|
|
379
|
-
|
|
400
|
+
for (const post of noticeWindowPostamble(winR)) acq.session.append(post.kind, post.data);
|
|
401
|
+
return { ok: true, ...result, discarded: plan.discard === true, upToSeq: plan.upToSeq, restoredSourceSeqs: plan.replayable.map((i) => i.seq), range: { startSeq: plan.startSeq, endSeq: plan.endSeq } };
|
|
380
402
|
} finally {
|
|
381
403
|
acq.release();
|
|
382
404
|
}
|
|
@@ -384,18 +406,28 @@ async function opsRestore(targetCtx, sessionId, restoreSeq, upToSeq) {
|
|
|
384
406
|
// store-miss → 磁盘路径:说明事件 + 重放事件批量追加(一个帧)
|
|
385
407
|
let cursor = nextSeqFrom(events);
|
|
386
408
|
const baseTime = Date.now();
|
|
387
|
-
const
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
409
|
+
const version = (await readSessionFileAsync(logPath)).header?.version ?? 4;
|
|
410
|
+
const batch = noticeWindowPreamble(win).map((e, i) => ({
|
|
411
|
+
type: e.kind, seq: cursor++, time: baseTime + i, data: e.data,
|
|
412
|
+
}));
|
|
413
|
+
const noticeBase = baseTime + batch.length;
|
|
414
|
+
batch.push(buildRestoreNoticeEvent({
|
|
415
|
+
seq: cursor++, time: noticeBase, turnStep: plan.turnStep, version,
|
|
416
|
+
text: plan.discard
|
|
417
|
+
? `[消息操作] 回撤标记 seq ${plan.restoreSeq} 已停用(丢弃):区间内容保持遮蔽、未重放;日志原文仍在,可搜索`
|
|
418
|
+
: restoreNoticeText(plan),
|
|
419
|
+
restoreSeq: plan.restoreSeq,
|
|
420
|
+
discard: plan.discard === true,
|
|
421
|
+
}));
|
|
392
422
|
let offset = 0;
|
|
393
|
-
for (const item of plan.replayable) {
|
|
394
|
-
batch.push(buildReplayEvent({ seq: cursor++, time:
|
|
423
|
+
for (const item of plan.discard ? [] : plan.replayable) {
|
|
424
|
+
batch.push(buildReplayEvent({ seq: cursor++, time: noticeBase + (++offset), item, turnStep: plan.turnStep, version }));
|
|
395
425
|
}
|
|
426
|
+
let tail = noticeBase + offset + 1;
|
|
427
|
+
for (const e of noticeWindowPostamble(win)) batch.push({ type: e.kind, seq: cursor++, time: tail++, data: e.data });
|
|
396
428
|
await diskAppend(logPath, batch);
|
|
397
429
|
return {
|
|
398
|
-
ok: true, disk: true,
|
|
430
|
+
ok: true, disk: true, syntheticTurn: win.synthetic === true,
|
|
399
431
|
restoredCount: plan.replayable.length,
|
|
400
432
|
upToSeq: plan.upToSeq,
|
|
401
433
|
restoredSourceSeqs: plan.replayable.map((i) => i.seq),
|
|
@@ -405,18 +437,6 @@ async function opsRestore(targetCtx, sessionId, restoreSeq, upToSeq) {
|
|
|
405
437
|
};
|
|
406
438
|
}
|
|
407
439
|
|
|
408
|
-
async function opsExport(targetCtx, sessionId, seq) {
|
|
409
|
-
if (!isSessionId(sessionId)) throw new OpsError("invalid sessionId", 400);
|
|
410
|
-
if (seq !== undefined && (!Number.isSafeInteger(seq) || seq < 0)) throw new OpsError("invalid seq", 400);
|
|
411
|
-
const { logPath } = pickSessionDir(sessionId);
|
|
412
|
-
const { header, events, frameCount, partial } = await readSessionFileAsync(logPath);
|
|
413
|
-
let markdown = exportMarkdown(header, events, seq);
|
|
414
|
-
// 超阈值提示(partial 语义):内容完整,仅告知本次解压耗时可能较长。
|
|
415
|
-
if (partial) {
|
|
416
|
-
markdown += `\n> 注:本日志共 ${frameCount} 个事件帧(超过 ${500} 帧阈值),本次导出已完整读取,但大日志解压耗时较长。\n`;
|
|
417
|
-
}
|
|
418
|
-
return { header, markdown, upToSeq: seq, frameCount, partial };
|
|
419
|
-
}
|
|
420
440
|
|
|
421
441
|
// ---------------------------------------------------------------------------
|
|
422
442
|
// Agent 工具 message_ops(容错注册)
|
|
@@ -431,12 +451,12 @@ export function createMessageOpsTool({ defineTool, ops, ctx }) {
|
|
|
431
451
|
return defineTool({
|
|
432
452
|
name: "message_ops",
|
|
433
453
|
description:
|
|
434
|
-
"Manage DSH conversation messages: list messages with visibility, revert to a seq (surface replace shadows the tail), delete a single message,
|
|
454
|
+
"Manage DSH conversation messages: list messages with visibility, revert to a seq (surface replace shadows the tail), delete a single message, restore (replay) messages shadowed by a previous revert. These are the message-level operations dsh does NOT provide natively (use the built-in session/fork for branching and /export for archives). Sessions must not be running for mutating actions.",
|
|
435
455
|
parameters: {
|
|
436
456
|
action: {
|
|
437
457
|
type: "string",
|
|
438
458
|
required: true,
|
|
439
|
-
enum: ["list", "revert", "delete", "
|
|
459
|
+
enum: ["list", "revert", "delete", "restore"],
|
|
440
460
|
description: "Which operation to perform.",
|
|
441
461
|
},
|
|
442
462
|
sessionId: {
|
|
@@ -446,11 +466,10 @@ export function createMessageOpsTool({ defineTool, ops, ctx }) {
|
|
|
446
466
|
},
|
|
447
467
|
seq: {
|
|
448
468
|
type: "integer",
|
|
449
|
-
description: "revert/delete: the target seq. restore: the seq of the revert/delete marker event.
|
|
469
|
+
description: "revert/delete: the target seq. restore: the seq of the revert/delete marker event. Ignored by list.",
|
|
450
470
|
},
|
|
451
471
|
upToSeq: {
|
|
452
472
|
type: "integer",
|
|
453
|
-
description: "branch: fork keeps events up to this seq (inclusive).",
|
|
454
473
|
},
|
|
455
474
|
},
|
|
456
475
|
output: {
|
|
@@ -463,19 +482,14 @@ export function createMessageOpsTool({ defineTool, ops, ctx }) {
|
|
|
463
482
|
try {
|
|
464
483
|
let result;
|
|
465
484
|
// 0.5.2(P1):ops 自 0.2.1 起为 async——不 await 会把 Promise 直接送进 renderResult,
|
|
466
|
-
// list/restore 必抛 TypeError
|
|
485
|
+
// list/restore 必抛 TypeError(测试桩曾是同步的故漏检)。
|
|
467
486
|
switch (action) {
|
|
468
487
|
case "list": result = await ops.list(ctx, sessionId); break;
|
|
469
488
|
case "revert": result = await ops.revert(ctx, sessionId, args.seq); break;
|
|
470
489
|
case "delete": result = await ops.delete(ctx, sessionId, args.seq); break;
|
|
471
|
-
case "branch": result = await ops.branch(ctx, sessionId, args.upToSeq); break;
|
|
472
490
|
case "restore": result = await ops.restore(ctx, sessionId, args.seq); break;
|
|
473
|
-
case "export": result = await ops.export(ctx, sessionId, args.seq); break;
|
|
474
491
|
default: throw new OpsError(`unknown action: ${action}`, 400);
|
|
475
492
|
}
|
|
476
|
-
if (action === "branch" && result && typeof result === "object" && result.parentSession == null) {
|
|
477
|
-
result.parentSession = sessionId; // renderResult 需要原始会话 id(branch 返回值不含/可为 undefined)
|
|
478
|
-
}
|
|
479
493
|
return renderResult(action, result);
|
|
480
494
|
} catch (e) {
|
|
481
495
|
return `${action} failed: ${e && e.message ? e.message : e}`;
|
|
@@ -489,7 +503,7 @@ function renderResult(action, r) {
|
|
|
489
503
|
switch (action) {
|
|
490
504
|
case "list": {
|
|
491
505
|
const lines = [
|
|
492
|
-
`session ${r.session.id}
|
|
506
|
+
`session ${r.session.id}`,
|
|
493
507
|
`running: ${r.running}; total: ${r.total}; visible: ${r.visibleCount}`,
|
|
494
508
|
];
|
|
495
509
|
for (const m of r.messages) {
|
|
@@ -500,12 +514,8 @@ function renderResult(action, r) {
|
|
|
500
514
|
case "revert":
|
|
501
515
|
case "delete":
|
|
502
516
|
return `${r.mode} ok: shadowed ${r.shadowedCount} node(s) from seq ${r.seq}; marker event seq ${r.eventSeq}`;
|
|
503
|
-
case "branch":
|
|
504
|
-
return `branch ok: new session ${r.newId} (parent ${r.parentSession ?? "n/a"}), kept ${r.keptEvents ?? "?"} event(s)`;
|
|
505
517
|
case "restore":
|
|
506
518
|
return `restore ok: replayed ${r.restoredCount} message(s) from range ${r.range.startSeq}..${r.range.endSeq} (${r.skipped} skipped); appended event seqs ${r.eventSeqs.join(", ")}`;
|
|
507
|
-
case "export":
|
|
508
|
-
return r.markdown;
|
|
509
519
|
default:
|
|
510
520
|
return JSON.stringify(r);
|
|
511
521
|
}
|
|
@@ -526,13 +536,15 @@ function registerToolTolerantly(ctx) {
|
|
|
526
536
|
list: (c, id) => opsList(c, id),
|
|
527
537
|
revert: (c, id, seq) => opsCommit(c, "revert", id, seq),
|
|
528
538
|
delete: (c, id, seq) => opsCommit(c, "delete", id, seq),
|
|
529
|
-
branch: (c, id, upToSeq) => opsBranch(c, id, upToSeq),
|
|
530
539
|
restore: (c, id, seq, upToSeq) => opsRestore(c, id, seq, upToSeq),
|
|
531
|
-
export: (c, id, seq) => opsExport(c, id, seq),
|
|
532
540
|
};
|
|
533
541
|
tools.register(createMessageOpsTool({ defineTool, ops, ctx: targetCtx }));
|
|
534
542
|
})
|
|
535
|
-
.catch(() => {
|
|
543
|
+
// 0.9.0:不再静默吞错。原先 catch(() => {}) 让「工具注册成功与否」完全无从诊断
|
|
544
|
+
// (实测日志里 defineTool / ERR_MODULE_NOT_FOUND 零命中)。
|
|
545
|
+
.catch((e) => {
|
|
546
|
+
console.warn('[message-ops] defineTool 注册失败,工具不可用(其余功能不受影响):', e && (e.code || e.message) ? (e.code || e.message) : e)
|
|
547
|
+
});
|
|
536
548
|
return true;
|
|
537
549
|
}
|
|
538
550
|
if (tryRegister(ctx)) return;
|
|
@@ -617,23 +629,6 @@ export function apply(ctx) {
|
|
|
617
629
|
kind: "exact", path: "/api/message-ops/delete", handler: commitHandler("delete"),
|
|
618
630
|
}), "dsh-message-ops: delete route");
|
|
619
631
|
|
|
620
|
-
// --- POST branch:磁盘级 fork(唯一非破坏操作) ---------------------------
|
|
621
|
-
targetCtx.effect(() => host.register({
|
|
622
|
-
kind: "exact",
|
|
623
|
-
path: "/api/message-ops/branch",
|
|
624
|
-
handler: async (req, res) => {
|
|
625
|
-
if (req.method !== "POST") return sendJson(res, 405, { ok: false, error: "method not allowed" });
|
|
626
|
-
if (!writeFence(req, res)) return;
|
|
627
|
-
const body = await readFencedBody(req, res);
|
|
628
|
-
if (body === undefined) return;
|
|
629
|
-
if (!body || typeof body !== "object") return sendJson(res, 400, { ok: false, error: "invalid json" });
|
|
630
|
-
try {
|
|
631
|
-
return sendJson(res, 200, opsBranch(targetCtx, body.sessionId, body.upToSeq));
|
|
632
|
-
} catch (err) {
|
|
633
|
-
return sendJson(res, err instanceof OpsError ? err.status : 500, { ok: false, error: String(err && err.message ? err.message : err) });
|
|
634
|
-
}
|
|
635
|
-
},
|
|
636
|
-
}), "dsh-message-ops: branch route");
|
|
637
632
|
|
|
638
633
|
// --- POST restore:回滚恢复(重放语义,见 ops-core.js 注释) --------------
|
|
639
634
|
targetCtx.effect(() => host.register({
|
|
@@ -647,36 +642,13 @@ export function apply(ctx) {
|
|
|
647
642
|
if (!body || typeof body !== "object") return sendJson(res, 400, { ok: false, error: "invalid json" });
|
|
648
643
|
try {
|
|
649
644
|
// 0.8.0:body.upToSeq 存在 → 按轮步进恢复(只到该 seq)
|
|
650
|
-
return sendJson(res, 200, await opsRestore(targetCtx, body.sessionId, body.seq, body.upToSeq != null ? Number(body.upToSeq) : undefined));
|
|
645
|
+
return sendJson(res, 200, await opsRestore(targetCtx, body.sessionId, body.seq, body.upToSeq != null ? Number(body.upToSeq) : undefined, body.discard === true));
|
|
651
646
|
} catch (err) {
|
|
652
647
|
return sendJson(res, err instanceof OpsError ? err.status : 500, { ok: false, error: String(err && err.message ? err.message : err) });
|
|
653
648
|
}
|
|
654
649
|
},
|
|
655
650
|
}), "dsh-message-ops: restore route");
|
|
656
651
|
|
|
657
|
-
// --- GET export:Markdown 附件下载 ----------------------------------------
|
|
658
|
-
targetCtx.effect(() => host.register({
|
|
659
|
-
kind: "exact",
|
|
660
|
-
path: "/api/message-ops/export",
|
|
661
|
-
handler: async (req, res) => {
|
|
662
|
-
if (!fence(req, res)) return;
|
|
663
|
-
try {
|
|
664
|
-
const url = new URL(req.url, "http://localhost");
|
|
665
|
-
const sessionId = url.searchParams.get("sessionId") || "";
|
|
666
|
-
const rawSeq = url.searchParams.get("seq");
|
|
667
|
-
const seq = rawSeq === null || rawSeq === "" ? undefined : Number(rawSeq);
|
|
668
|
-
const { header, markdown, upToSeq } = await opsExport(targetCtx, sessionId, seq);
|
|
669
|
-
const suffix = upToSeq === undefined ? "full" : `seq-${upToSeq}`;
|
|
670
|
-
res.writeHead(200, {
|
|
671
|
-
"content-type": "text/markdown; charset=utf-8",
|
|
672
|
-
"content-disposition": `attachment; filename="${header.id}-${suffix}.md"`,
|
|
673
|
-
});
|
|
674
|
-
res.end(markdown);
|
|
675
|
-
} catch (err) {
|
|
676
|
-
return sendJson(res, err instanceof OpsError ? err.status : 500, { ok: false, error: String(err && err.message ? err.message : err) });
|
|
677
|
-
}
|
|
678
|
-
},
|
|
679
|
-
}), "dsh-message-ops: export route");
|
|
680
652
|
}
|
|
681
653
|
|
|
682
654
|
const ws = ctx.get("webServer");
|