@240xu/dsh-message-ops 0.10.2 → 0.10.8
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/package.json +2 -2
- package/src/client.js +116 -6
- package/src/index.js +118 -22
- package/src/ops-core.js +183 -8
- package/src/session-file.js +48 -5
- package/test/dock-preview.test.js +666 -0
package/src/session-file.js
CHANGED
|
@@ -20,15 +20,44 @@ const ZSTD_MAGIC = 0xfd2fb528;
|
|
|
20
20
|
/** 魔数的字节形态(Buffer.indexOf 用;小端 0xfd2fb528 → 28 b5 2f fd)。 */
|
|
21
21
|
const ZSTD_MAGIC_BYTES = Buffer.from([0x28, 0xb5, 0x2f, 0xfd]);
|
|
22
22
|
|
|
23
|
-
/**
|
|
24
|
-
export
|
|
23
|
+
/** 带语义状态码的操作错误(单点:HTTP 直接映射 status,工具端转失败文本)。 */
|
|
24
|
+
export class OpsError extends Error {
|
|
25
|
+
constructor(message, status = 400) {
|
|
26
|
+
super(message);
|
|
27
|
+
this.status = status;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** messageText 的「合格 text 段」选择口径(单点):type==='text' 且非空白。 */
|
|
32
|
+
function textSegments(e) {
|
|
25
33
|
const msg = e && e.data && e.data.message;
|
|
26
34
|
const content = msg && Array.isArray(msg.content) ? msg.content : (e && e.data && e.data.content);
|
|
27
|
-
if (!Array.isArray(content)) return
|
|
35
|
+
if (!Array.isArray(content)) return [];
|
|
36
|
+
const out = [];
|
|
28
37
|
for (const c of content) {
|
|
29
|
-
if (c && c.type === "text" && typeof c.text === "string" && c.text.trim())
|
|
38
|
+
if (c && c.type === "text" && typeof c.text === "string" && c.text.trim()) out.push(c.text);
|
|
30
39
|
}
|
|
31
|
-
return
|
|
40
|
+
return out;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** 从 message 事件提取首个非空 text 块(listMessages / ops-core 共用单点)。 */
|
|
44
|
+
export function messageText(e) {
|
|
45
|
+
const segs = textSegments(e);
|
|
46
|
+
return segs.length ? segs[0] : "";
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* message 事件的**全量** text(/text 路由用单点,别在路由里再造口径)。
|
|
51
|
+
*
|
|
52
|
+
* 宿主把长文本拆成多个 content 段(seq 6830 实测 = 321+7954+20 三段的
|
|
53
|
+
* compaction checkpoint),只取首段 = 摘要被拦腰截断。合并规则与 messageText
|
|
54
|
+
* **同一条口径**:段选择器共用 textSegments(非文本段按 messageText 的规则跳过),
|
|
55
|
+
* 差异只在聚合——全部合格段按 "\\n\\n" 接回(content 段是切块,接缝丢过换行,
|
|
56
|
+
* "\\n\\n" 恰好还原段落边界,也不会把「。」「#」硬粘成一行)。
|
|
57
|
+
*/
|
|
58
|
+
export function messageTextFull(e) {
|
|
59
|
+
const segs = textSegments(e);
|
|
60
|
+
return segs.join("\n\n");
|
|
32
61
|
}
|
|
33
62
|
|
|
34
63
|
/** 与 DSH 宿主一致:帧带 checksum(ZSTD_c_checksumFlag=1)。 */
|
|
@@ -298,11 +327,25 @@ export function markCommitted(messages) {
|
|
|
298
327
|
return messages;
|
|
299
328
|
}
|
|
300
329
|
|
|
330
|
+
const MAX_SPAN = 1_000_000;
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* M1 护栏:超 span 上限直接抛(fail loud;与 session-search 同款常量与阈值,
|
|
334
|
+
* 对方仓库不碰、本仓库自行持有常量)。路由层 catch 把 OpsError.status 映射为
|
|
335
|
+
* 404——历史 0.10.x marker 的区间都是个位数,一百万量级只可能来自损坏行或
|
|
336
|
+
* 伪造 marker,不允许把它卡死事件循环。
|
|
337
|
+
*/
|
|
301
338
|
export function computeShadowed(events) {
|
|
302
339
|
const shadowed = new Set();
|
|
303
340
|
for (const e of events) {
|
|
304
341
|
const range = readReplaceOp(e);
|
|
305
342
|
if (!range) continue;
|
|
343
|
+
if (range.endSeq - range.startSeq > MAX_SPAN) {
|
|
344
|
+
throw new OpsError(
|
|
345
|
+
`surface replace span too large: [${range.startSeq}..${range.endSeq}] exceeds MAX_SPAN=${MAX_SPAN} — refusing to materialize (corrupt or hostile replace marker?)`,
|
|
346
|
+
404,
|
|
347
|
+
);
|
|
348
|
+
}
|
|
306
349
|
for (let s = range.startSeq; s <= range.endSeq; s++) shadowed.add(s);
|
|
307
350
|
}
|
|
308
351
|
return shadowed;
|