dsh-shiori 0.2.1 → 0.3.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 +304 -288
- package/client/index.js +1109 -705
- package/client/shiori.css +1 -0
- package/package.json +1 -1
- package/src/core/render.mjs +84 -69
- package/src/core/topics.mjs +146 -136
- package/src/host/tools.mjs +582 -582
- package/src/organize/executor.mjs +766 -680
- package/src/organize/queue.mjs +110 -26
- package/src/plugin/management.mjs +110 -7
- package/src/plugin/protocol.mjs +53 -12
- package/src/plugin/state.mjs +677 -666
- package/src/service/index.mjs +304 -231
- package/src/sparks/graph.mjs +305 -278
- package/src/sparks/store.mjs +1954 -1930
- package/src/storage/errors.mjs +58 -46
- package/src/storage/frontmatter.mjs +186 -186
- package/src/storage/fsutil.mjs +181 -134
- package/src/storage/journal.mjs +133 -127
- package/src/storage/store.mjs +1085 -1079
package/client/shiori.css
CHANGED
|
@@ -43,6 +43,7 @@
|
|
|
43
43
|
.dshi_graphWrap{border:1px solid var(--dsw-alias-border-l2,#dce2eb);border-radius:10px;overflow:hidden;position:relative;user-select:none;cursor:grab;background:var(--dsw-alias-bg-layer-2,transparent)}
|
|
44
44
|
.dshi_graphSvg{display:block;width:100%;height:380px;touch-action:none}
|
|
45
45
|
.dshi_graphNode{cursor:pointer}
|
|
46
|
+
.dshi_graphNode:focus-visible{outline:2px solid var(--dshi-accent,#e5484d);outline-offset:2px}
|
|
46
47
|
.dshi_nodeEvergreen{fill:var(--dsw-alias-state-success-primary,#1a7f37)}
|
|
47
48
|
.dshi_nodeEphemeral{fill:var(--dsw-alias-state-business-primary,#3964fe)}
|
|
48
49
|
.dshi_nodeDim{fill:var(--dsw-alias-label-tertiary,#8b93a1)}
|
package/package.json
CHANGED
package/src/core/render.mjs
CHANGED
|
@@ -1,69 +1,84 @@
|
|
|
1
|
-
// Shiori P0-CORE · L1 完整渲染(公共契约导出 renderL1)。
|
|
2
|
-
//
|
|
3
|
-
// 磁盘 core.md 只存人工正文;renderL1 在渲染时派生索引,禁止回写。
|
|
4
|
-
// 索引格式固定(CONTRACTS §A):
|
|
5
|
-
// 标题行:## 主题记忆索引 / Topic memory index
|
|
6
|
-
// 条目行:- key [scopeId] — summary
|
|
7
|
-
//
|
|
8
|
-
//
|
|
9
|
-
//
|
|
10
|
-
//
|
|
11
|
-
//
|
|
12
|
-
//
|
|
13
|
-
//
|
|
14
|
-
//
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
1
|
+
// Shiori P0-CORE · L1 完整渲染(公共契约导出 renderL1)。
|
|
2
|
+
//
|
|
3
|
+
// 磁盘 core.md 只存人工正文;renderL1 在渲染时派生索引,禁止回写。
|
|
4
|
+
// 索引格式固定(CONTRACTS §A):
|
|
5
|
+
// 标题行:## 主题记忆索引 / Topic memory index
|
|
6
|
+
// 条目行:- key [scopeId] — summary
|
|
7
|
+
// 归档钩子:另有 N 篇冷专题,memory_search 可查 ← N>0 才出现(DESIGN §2.1)
|
|
8
|
+
// core 非空且存在索引时以两个 LF 连接;无 topic 且无归档专题不加索引标题。
|
|
9
|
+
// chars 按 UTF-16 码元计(text.length),bytes 为 UTF-8 字节数(仅诊断)。
|
|
10
|
+
// 标题、换行、来源 [scopeId] 全部计入 chars——预算以完整渲染为准。
|
|
11
|
+
//
|
|
12
|
+
// renderL1 是纯渲染:不做预算裁决(那是 evaluateMutation 的职责),
|
|
13
|
+
// 超限文本也必须能渲染出来,供修复候选比较使用。
|
|
14
|
+
// 输入 topics 必须是已去重的可见列表(同 key 重复即拒绝);
|
|
15
|
+
// 带 archivedAt(非空字符串)的归档专题跳过条目渲染,只计入钩子行 N;
|
|
16
|
+
// 排序按 key 的 UTF-16 码元序,与系统 locale 无关,输入不变时逐字稳定。
|
|
17
|
+
|
|
18
|
+
import { fail, describe, requireObject, requireExactKeys } from "./errors.mjs";
|
|
19
|
+
import { validateTopic, compareKeys } from "./topics.mjs";
|
|
20
|
+
|
|
21
|
+
export const INDEX_HEADER = "## 主题记忆索引 / Topic memory index";
|
|
22
|
+
|
|
23
|
+
/** 索引末尾的归档钩子行(N>0 时出现,指向 memory_search 冷搜)。 */
|
|
24
|
+
export function archiveHookLine(archivedCount) {
|
|
25
|
+
return `另有 ${archivedCount} 篇冷专题,memory_search 可查`;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const encoder = new TextEncoder();
|
|
29
|
+
|
|
30
|
+
export function renderL1(input) {
|
|
31
|
+
requireObject(input, "renderL1 input");
|
|
32
|
+
requireExactKeys(input, "renderL1 input", ["coreText", "topics"]);
|
|
33
|
+
const { coreText, topics } = input;
|
|
34
|
+
if (typeof coreText !== "string") {
|
|
35
|
+
fail("INVALID_INPUT", `coreText must be a string, got ${describe(coreText)}`);
|
|
36
|
+
}
|
|
37
|
+
if (!Array.isArray(topics)) {
|
|
38
|
+
fail("INVALID_INPUT", `topics must be an array, got ${describe(topics)}`);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// 索引循环而非 map:稀疏数组的孔(undefined)必须进入校验被显式拒绝,
|
|
42
|
+
// 不允许被跳孔逻辑静默当成无 topic。
|
|
43
|
+
const seenKeys = new Set();
|
|
44
|
+
const validated = [];
|
|
45
|
+
let archivedCount = 0;
|
|
46
|
+
for (let index = 0; index < topics.length; index++) {
|
|
47
|
+
const topic = validateTopic(topics[index], `topics[${index}]`);
|
|
48
|
+
if (seenKeys.has(topic.key)) {
|
|
49
|
+
fail(
|
|
50
|
+
"INVALID_INPUT",
|
|
51
|
+
`duplicate topic key "${topic.key}" (topics[${index}]); renderL1 expects a deduplicated visible topic list`,
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
seenKeys.add(topic.key);
|
|
55
|
+
// 归档专题不进 L1 索引条目(L1 预算的核心目的);只计入钩子行 N。
|
|
56
|
+
// 归档真相只在专题文件本身:有 archivedAt 即归档(topics.mjs 校验非空串)。
|
|
57
|
+
if (topic.archivedAt !== undefined) {
|
|
58
|
+
archivedCount++;
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
validated.push(topic);
|
|
62
|
+
}
|
|
63
|
+
validated.sort((a, b) => compareKeys(a.key, b.key));
|
|
64
|
+
|
|
65
|
+
const indexText =
|
|
66
|
+
validated.length === 0 && archivedCount === 0
|
|
67
|
+
? ""
|
|
68
|
+
: [
|
|
69
|
+
INDEX_HEADER,
|
|
70
|
+
...validated.map((topic) => `- ${topic.key} [${topic.scopeId}] — ${topic.summary}`),
|
|
71
|
+
...(archivedCount > 0 ? [archiveHookLine(archivedCount)] : []),
|
|
72
|
+
].join("\n");
|
|
73
|
+
|
|
74
|
+
let text;
|
|
75
|
+
if (coreText.length > 0 && indexText.length > 0) {
|
|
76
|
+
text = `${coreText}\n\n${indexText}`;
|
|
77
|
+
} else if (indexText.length > 0) {
|
|
78
|
+
text = indexText;
|
|
79
|
+
} else {
|
|
80
|
+
text = coreText;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return { text, chars: text.length, bytes: encoder.encode(text).length, indexText };
|
|
84
|
+
}
|
package/src/core/topics.mjs
CHANGED
|
@@ -1,136 +1,146 @@
|
|
|
1
|
-
// Shiori P0-CORE · 作用域可见性与 Topic 元数据校验。
|
|
2
|
-
// 公共契约导出 selectVisibleTopics;validateTopic/validateScopeId/compareKeys
|
|
3
|
-
// 为 render 与本模块共用的内部助手(不进公共入口)。
|
|
4
|
-
//
|
|
5
|
-
// 可见性规则(CONTRACTS §A / DESIGN §4.4、§10):
|
|
6
|
-
// - scopeId === activeScopeId 或 scopeId === "global" 的专题可见;
|
|
7
|
-
// - 同 key 时 active 覆盖 global;
|
|
8
|
-
// - 其他 scope 一律不可见;
|
|
9
|
-
// - 相同 key 可在不同 scope 并存,但同一 scope 内重复 key 是数据错误,拒绝;
|
|
10
|
-
// - 输出按 key 的 UTF-16 码元序稳定排序,绝不依赖系统 locale。
|
|
11
|
-
//
|
|
12
|
-
// Topic 形状:{key,title,summary,scopeId,revision,updated,body?}。
|
|
13
|
-
// body 之外的字段全部必填;summary ≤60 UTF-16 且不得含换行(会破坏索引行结构);
|
|
14
|
-
// 允许未知字段以保持数据前向兼容(Topic 是数据记录,不是控制接口)。
|
|
15
|
-
|
|
16
|
-
import { fail, describe, requireObject, requireExactKeys } from "./errors.mjs";
|
|
17
|
-
import { validateKey } from "./keys.mjs";
|
|
18
|
-
|
|
19
|
-
// DESIGN §10 目录布局 scopes/global/ 即共享作用域,因此字面量 "global"
|
|
20
|
-
// 标记全局专题;B/C 必须使用同一字面量(见 REPORT.md 集成约定)。
|
|
21
|
-
export const GLOBAL_SCOPE_ID = "global";
|
|
22
|
-
|
|
23
|
-
export const SUMMARY_MAX_CHARS = 60;
|
|
24
|
-
const MAX_SCOPE_ID_CHARS = 128;
|
|
25
|
-
|
|
26
|
-
export function validateScopeId(value, name) {
|
|
27
|
-
if (typeof value !== "string" || value.length === 0) {
|
|
28
|
-
fail("INVALID_INPUT", `${name} must be a non-empty string, got ${describe(value)}`);
|
|
29
|
-
}
|
|
30
|
-
if (value.length > MAX_SCOPE_ID_CHARS) {
|
|
31
|
-
fail("INVALID_INPUT", `${name} must be at most ${MAX_SCOPE_ID_CHARS} UTF-16 chars, got ${value.length}`);
|
|
32
|
-
}
|
|
33
|
-
if (value !== value.trim()) {
|
|
34
|
-
fail("INVALID_INPUT", `${name} must not start or end with whitespace`);
|
|
35
|
-
}
|
|
36
|
-
for (const ch of value) {
|
|
37
|
-
const cp = ch.codePointAt(0);
|
|
38
|
-
if (cp >= 0xd800 && cp <= 0xdfff) {
|
|
39
|
-
fail("INVALID_INPUT", `${name} must not contain lone surrogate code units`);
|
|
40
|
-
}
|
|
41
|
-
if (cp <= 0x1f || cp === 0x7f || (cp >= 0x80 && cp <= 0x9f)) {
|
|
42
|
-
fail("INVALID_INPUT", `${name} must not contain control characters`);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
return value;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
// locale 无关的 key 比较:直接按 UTF-16 码元比较,结果跨平台逐字一致。
|
|
49
|
-
export function compareKeys(a, b) {
|
|
50
|
-
return a < b ? -1 : a > b ? 1 : 0;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export function validateTopic(topic, where) {
|
|
54
|
-
requireObject(topic, where);
|
|
55
|
-
const required = ["key", "title", "summary", "scopeId", "revision", "updated"];
|
|
56
|
-
// 必填字段必须是自有属性:仅存在于原型链上的字段不算数
|
|
57
|
-
for (const field of required) {
|
|
58
|
-
if (!Object.prototype.hasOwnProperty.call(topic, field)) {
|
|
59
|
-
fail("INVALID_INPUT", `${where} is missing required property "${field}"`);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
try {
|
|
63
|
-
validateKey(topic.key);
|
|
64
|
-
} catch (error) {
|
|
65
|
-
fail("INVALID_INPUT", `${where}: ${error.message}`);
|
|
66
|
-
}
|
|
67
|
-
if (typeof topic.title !== "string" || topic.title.length === 0) {
|
|
68
|
-
fail("INVALID_INPUT", `${where}: title must be a non-empty string`);
|
|
69
|
-
}
|
|
70
|
-
if (typeof topic.summary !== "string") {
|
|
71
|
-
fail("INVALID_INPUT", `${where}: summary must be a string`);
|
|
72
|
-
}
|
|
73
|
-
if (topic.summary.length > SUMMARY_MAX_CHARS) {
|
|
74
|
-
fail(
|
|
75
|
-
"INVALID_INPUT",
|
|
76
|
-
`${where}: summary is ${topic.summary.length} UTF-16 chars, max is ${SUMMARY_MAX_CHARS}`,
|
|
77
|
-
);
|
|
78
|
-
}
|
|
79
|
-
if (topic.summary.includes("\n") || topic.summary.includes("\r")) {
|
|
80
|
-
fail("INVALID_INPUT", `${where}: summary must not contain line breaks`);
|
|
81
|
-
}
|
|
82
|
-
validateScopeId(topic.scopeId, `${where}: scopeId`);
|
|
83
|
-
if (typeof topic.revision !== "string" || topic.revision.length === 0) {
|
|
84
|
-
fail("INVALID_INPUT", `${where}: revision must be a non-empty string`);
|
|
85
|
-
}
|
|
86
|
-
const updated = topic.updated;
|
|
87
|
-
const updatedOk =
|
|
88
|
-
(typeof updated === "string" && updated.length > 0) ||
|
|
89
|
-
(typeof updated === "number" && Number.isFinite(updated)) ||
|
|
90
|
-
(updated instanceof Date && Number.isFinite(updated.getTime()));
|
|
91
|
-
if (!updatedOk) {
|
|
92
|
-
fail("INVALID_INPUT", `${where}: updated must be a non-empty string, a finite number, or a valid Date`);
|
|
93
|
-
}
|
|
94
|
-
if (topic.body !== undefined && typeof topic.body !== "string") {
|
|
95
|
-
fail("INVALID_INPUT", `${where}: body must be a string when present`);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
1
|
+
// Shiori P0-CORE · 作用域可见性与 Topic 元数据校验。
|
|
2
|
+
// 公共契约导出 selectVisibleTopics;validateTopic/validateScopeId/compareKeys
|
|
3
|
+
// 为 render 与本模块共用的内部助手(不进公共入口)。
|
|
4
|
+
//
|
|
5
|
+
// 可见性规则(CONTRACTS §A / DESIGN §4.4、§10):
|
|
6
|
+
// - scopeId === activeScopeId 或 scopeId === "global" 的专题可见;
|
|
7
|
+
// - 同 key 时 active 覆盖 global;
|
|
8
|
+
// - 其他 scope 一律不可见;
|
|
9
|
+
// - 相同 key 可在不同 scope 并存,但同一 scope 内重复 key 是数据错误,拒绝;
|
|
10
|
+
// - 输出按 key 的 UTF-16 码元序稳定排序,绝不依赖系统 locale。
|
|
11
|
+
//
|
|
12
|
+
// Topic 形状:{key,title,summary,scopeId,revision,updated,body?}。
|
|
13
|
+
// body 之外的字段全部必填;summary ≤60 UTF-16 且不得含换行(会破坏索引行结构);
|
|
14
|
+
// 允许未知字段以保持数据前向兼容(Topic 是数据记录,不是控制接口)。
|
|
15
|
+
|
|
16
|
+
import { fail, describe, requireObject, requireExactKeys } from "./errors.mjs";
|
|
17
|
+
import { validateKey } from "./keys.mjs";
|
|
18
|
+
|
|
19
|
+
// DESIGN §10 目录布局 scopes/global/ 即共享作用域,因此字面量 "global"
|
|
20
|
+
// 标记全局专题;B/C 必须使用同一字面量(见 REPORT.md 集成约定)。
|
|
21
|
+
export const GLOBAL_SCOPE_ID = "global";
|
|
22
|
+
|
|
23
|
+
export const SUMMARY_MAX_CHARS = 60;
|
|
24
|
+
const MAX_SCOPE_ID_CHARS = 128;
|
|
25
|
+
|
|
26
|
+
export function validateScopeId(value, name) {
|
|
27
|
+
if (typeof value !== "string" || value.length === 0) {
|
|
28
|
+
fail("INVALID_INPUT", `${name} must be a non-empty string, got ${describe(value)}`);
|
|
29
|
+
}
|
|
30
|
+
if (value.length > MAX_SCOPE_ID_CHARS) {
|
|
31
|
+
fail("INVALID_INPUT", `${name} must be at most ${MAX_SCOPE_ID_CHARS} UTF-16 chars, got ${value.length}`);
|
|
32
|
+
}
|
|
33
|
+
if (value !== value.trim()) {
|
|
34
|
+
fail("INVALID_INPUT", `${name} must not start or end with whitespace`);
|
|
35
|
+
}
|
|
36
|
+
for (const ch of value) {
|
|
37
|
+
const cp = ch.codePointAt(0);
|
|
38
|
+
if (cp >= 0xd800 && cp <= 0xdfff) {
|
|
39
|
+
fail("INVALID_INPUT", `${name} must not contain lone surrogate code units`);
|
|
40
|
+
}
|
|
41
|
+
if (cp <= 0x1f || cp === 0x7f || (cp >= 0x80 && cp <= 0x9f)) {
|
|
42
|
+
fail("INVALID_INPUT", `${name} must not contain control characters`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return value;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// locale 无关的 key 比较:直接按 UTF-16 码元比较,结果跨平台逐字一致。
|
|
49
|
+
export function compareKeys(a, b) {
|
|
50
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function validateTopic(topic, where) {
|
|
54
|
+
requireObject(topic, where);
|
|
55
|
+
const required = ["key", "title", "summary", "scopeId", "revision", "updated"];
|
|
56
|
+
// 必填字段必须是自有属性:仅存在于原型链上的字段不算数
|
|
57
|
+
for (const field of required) {
|
|
58
|
+
if (!Object.prototype.hasOwnProperty.call(topic, field)) {
|
|
59
|
+
fail("INVALID_INPUT", `${where} is missing required property "${field}"`);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
try {
|
|
63
|
+
validateKey(topic.key);
|
|
64
|
+
} catch (error) {
|
|
65
|
+
fail("INVALID_INPUT", `${where}: ${error.message}`);
|
|
66
|
+
}
|
|
67
|
+
if (typeof topic.title !== "string" || topic.title.length === 0) {
|
|
68
|
+
fail("INVALID_INPUT", `${where}: title must be a non-empty string`);
|
|
69
|
+
}
|
|
70
|
+
if (typeof topic.summary !== "string") {
|
|
71
|
+
fail("INVALID_INPUT", `${where}: summary must be a string`);
|
|
72
|
+
}
|
|
73
|
+
if (topic.summary.length > SUMMARY_MAX_CHARS) {
|
|
74
|
+
fail(
|
|
75
|
+
"INVALID_INPUT",
|
|
76
|
+
`${where}: summary is ${topic.summary.length} UTF-16 chars, max is ${SUMMARY_MAX_CHARS}`,
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
if (topic.summary.includes("\n") || topic.summary.includes("\r")) {
|
|
80
|
+
fail("INVALID_INPUT", `${where}: summary must not contain line breaks`);
|
|
81
|
+
}
|
|
82
|
+
validateScopeId(topic.scopeId, `${where}: scopeId`);
|
|
83
|
+
if (typeof topic.revision !== "string" || topic.revision.length === 0) {
|
|
84
|
+
fail("INVALID_INPUT", `${where}: revision must be a non-empty string`);
|
|
85
|
+
}
|
|
86
|
+
const updated = topic.updated;
|
|
87
|
+
const updatedOk =
|
|
88
|
+
(typeof updated === "string" && updated.length > 0) ||
|
|
89
|
+
(typeof updated === "number" && Number.isFinite(updated)) ||
|
|
90
|
+
(updated instanceof Date && Number.isFinite(updated.getTime()));
|
|
91
|
+
if (!updatedOk) {
|
|
92
|
+
fail("INVALID_INPUT", `${where}: updated must be a non-empty string, a finite number, or a valid Date`);
|
|
93
|
+
}
|
|
94
|
+
if (topic.body !== undefined && typeof topic.body !== "string") {
|
|
95
|
+
fail("INVALID_INPUT", `${where}: body must be a string when present`);
|
|
96
|
+
}
|
|
97
|
+
// 归档标记(可选):有值即归档(ISO 时间串),缺省/空 = 未归档;
|
|
98
|
+
// 老文件没有该字段,行为不变(DESIGN §2.1 已定方案)。
|
|
99
|
+
if (topic.archivedAt !== undefined) {
|
|
100
|
+
if (typeof topic.archivedAt !== "string" || topic.archivedAt.length === 0) {
|
|
101
|
+
fail("INVALID_INPUT", `${where}: archivedAt must be a non-empty string when present`);
|
|
102
|
+
}
|
|
103
|
+
if (topic.archivedAt.includes("\n") || topic.archivedAt.includes("\r")) {
|
|
104
|
+
fail("INVALID_INPUT", `${where}: archivedAt must not contain line breaks`);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return topic;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function selectVisibleTopics(input) {
|
|
111
|
+
requireObject(input, "selectVisibleTopics input");
|
|
112
|
+
requireExactKeys(input, "selectVisibleTopics input", ["activeScopeId", "topics"]);
|
|
113
|
+
validateScopeId(input.activeScopeId, "activeScopeId");
|
|
114
|
+
if (!Array.isArray(input.topics)) {
|
|
115
|
+
fail("INVALID_INPUT", `topics must be an array, got ${describe(input.topics)}`);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// 全部输入(含不可见 scope)都做完整校验:坏数据即刻显式失败,
|
|
119
|
+
// 不静默跳过。同一 scope 内重复 key 是存储层错误。
|
|
120
|
+
// 用索引循环而非 forEach:稀疏数组的孔(undefined)必须进入校验,
|
|
121
|
+
// 不允许被跳孔逻辑静默当成无 topic。
|
|
122
|
+
const seenScopeKeys = new Set();
|
|
123
|
+
const activeByKey = new Map();
|
|
124
|
+
const globalByKey = new Map();
|
|
125
|
+
for (let index = 0; index < input.topics.length; index++) {
|
|
126
|
+
const topic = validateTopic(input.topics[index], `topics[${index}]`);
|
|
127
|
+
const scopeKey = `${topic.scopeId}\u0000${topic.key}`;
|
|
128
|
+
if (seenScopeKeys.has(scopeKey)) {
|
|
129
|
+
fail(
|
|
130
|
+
"INVALID_INPUT",
|
|
131
|
+
`duplicate topic key "${topic.key}" within scope "${topic.scopeId}" (topics[${index}])`,
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
seenScopeKeys.add(scopeKey);
|
|
135
|
+
if (topic.scopeId === input.activeScopeId) {
|
|
136
|
+
activeByKey.set(topic.key, topic);
|
|
137
|
+
} else if (topic.scopeId === GLOBAL_SCOPE_ID) {
|
|
138
|
+
globalByKey.set(topic.key, topic);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// 同 key:active 覆盖 global(activeScopeId 为 "global" 时自然退化为仅全局)。
|
|
143
|
+
const visibleByKey = new Map(globalByKey);
|
|
144
|
+
for (const [key, topic] of activeByKey) visibleByKey.set(key, topic);
|
|
145
|
+
return [...visibleByKey.values()].sort((a, b) => compareKeys(a.key, b.key));
|
|
146
|
+
}
|