@allbluecn/collab-server 0.0.0-stage → 0.1.7
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/LICENSE +661 -0
- package/README.md +126 -2
- package/package.json +64 -3
- package/src/__tests__/clients.test.ts +93 -0
- package/src/__tests__/embed.test.ts +227 -0
- package/src/__tests__/env-setup.ts +33 -0
- package/src/__tests__/exporters-bug.test.ts +552 -0
- package/src/__tests__/exporters-case.test.ts +356 -0
- package/src/__tests__/exporters-design.test.ts +356 -0
- package/src/__tests__/exporters-knowledge.test.ts +366 -0
- package/src/__tests__/exporters-stats.test.ts +105 -0
- package/src/__tests__/exporters-story.test.ts +519 -0
- package/src/__tests__/internal-apply-channel.test.ts +233 -0
- package/src/__tests__/internal-apply-race.test.ts +165 -0
- package/src/__tests__/internal-apply.test.ts +418 -0
- package/src/__tests__/multiplex.test.ts +166 -0
- package/src/__tests__/persistence-retry.test.ts +262 -0
- package/src/__tests__/persistence.test.ts +302 -0
- package/src/__tests__/server.test.ts +145 -0
- package/src/__tests__/share-room-limit.test.ts +183 -0
- package/src/__tests__/shutdown.test.ts +130 -0
- package/src/__tests__/test-utils.ts +176 -0
- package/src/auth.ts +84 -0
- package/src/clients.ts +133 -0
- package/src/embed.ts +156 -0
- package/src/exporters/bug.ts +454 -0
- package/src/exporters/case.ts +317 -0
- package/src/exporters/design.ts +268 -0
- package/src/exporters/entity-shared.ts +75 -0
- package/src/exporters/knowledge.ts +259 -0
- package/src/exporters/story.ts +483 -0
- package/src/exporters.ts +173 -0
- package/src/index.ts +354 -0
- package/src/internal-apply-limit.ts +99 -0
- package/src/internal-apply.ts +888 -0
- package/src/metrics.ts +82 -0
- package/src/persistence.ts +409 -0
- package/src/rooms.ts +32 -0
- package/src/share-room-limit.ts +73 -0
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
// AllBlue - 理想之海
|
|
3
|
+
// Copyright (C) 2026 AllBlue Contributors
|
|
4
|
+
//
|
|
5
|
+
// This program is free software: you can redistribute it and/or modify
|
|
6
|
+
// it under the terms of the GNU Affero General Public License as published by
|
|
7
|
+
// the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+
// (at your option) any later version.
|
|
9
|
+
//
|
|
10
|
+
// This program is distributed in the hope that it will be useful,
|
|
11
|
+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+
// GNU Affero General Public License for more details.
|
|
14
|
+
//
|
|
15
|
+
// You should have received a copy of the GNU Affero General Public License
|
|
16
|
+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
17
|
+
|
|
18
|
+
// TestCase 回写导出器(Yjs 多人协作 §5.2 回写 / §7.1 entity:case 行 / §10.2,
|
|
19
|
+
// 任务 22,与 exporters/story.ts、exporters/bug.ts 同构——复制适配):
|
|
20
|
+
//
|
|
21
|
+
// Y 属主面 = fields Y.Map + steps Y.Array(TestCase 无 description 列,
|
|
22
|
+
// room 的 default fragment 恒空——种子化不写、exporter 不读):
|
|
23
|
+
// - 6 字段:title/precondition/type/priority/state/assigneeId(与 web 写端
|
|
24
|
+
// CASE_COLLAB_FIELDS 一致;folderId/storyId/automation 不入 Y——非协同
|
|
25
|
+
// serverFn 直写面,DB 单一事实源无互吞面)。
|
|
26
|
+
// - steps:steps.steps 列表(order 不入 Y,落库按索引+1 归一;空 Y.Array =
|
|
27
|
+
// Y 无意见不触碰业务列——业务 schema 步骤 min(1),空集永不合法)。
|
|
28
|
+
//
|
|
29
|
+
// **协同落库 version+1(计划复审补)**:TestCase.version 是非协同客户端
|
|
30
|
+
// expectedVersion 乐观锁的语义锚——协同编辑经 exporter/离线直落写库时,内容面
|
|
31
|
+
// (title/precondition/steps/type/priority,与 updateTestCaseFn 的
|
|
32
|
+
// CONTENT_FIELDS 同口径)实际变更 → version 自增;仅元数据面(state/
|
|
33
|
+
// assigneeId)变更不 bump。与 DesignPage.version 自增移交服务端同口径。
|
|
34
|
+
//
|
|
35
|
+
// 活动:TestCase 无专属活动模型——落 ProjectActivity(action="updated" +
|
|
36
|
+
// targetType="testcase",按 Story 口径写归属作者 userId + metadata 变更流水;
|
|
37
|
+
// ProjectActivity 无 actorType 字段,零 schema 改动)。
|
|
38
|
+
//
|
|
39
|
+
// 挂载模式 / 幽灵防御 / touch 快照(锚点 M②)/ FK 防御:逐条同 story.ts。
|
|
40
|
+
|
|
41
|
+
import type { CollabRoomName } from "@allbluecn/collab-core/rooms";
|
|
42
|
+
import { buildRoomName } from "@allbluecn/collab-core/rooms";
|
|
43
|
+
import { readStepsFromDoc, type CollabStep } from "@allbluecn/collab-core/steps";
|
|
44
|
+
import type * as Y from "yjs";
|
|
45
|
+
import {
|
|
46
|
+
computeAuthorAttribution,
|
|
47
|
+
registerExporter,
|
|
48
|
+
registerRoomGuard,
|
|
49
|
+
type CollabAuthorStat,
|
|
50
|
+
} from "../exporters";
|
|
51
|
+
import {
|
|
52
|
+
COLLAB_FIELDS_MAP_KEY,
|
|
53
|
+
stepsWithOrder,
|
|
54
|
+
stepsEqual,
|
|
55
|
+
normalizeRowSteps,
|
|
56
|
+
type StoryTx,
|
|
57
|
+
} from "./entity-shared";
|
|
58
|
+
|
|
59
|
+
/** 协同字段清单(web 写端 CASE_COLLAB_FIELDS 的镜像,两侧必须同步维护) */
|
|
60
|
+
// 枚举白名单(web testcase constants 镜像,apps 不互 import——变更需双侧同步)
|
|
61
|
+
const CASE_ENUM_KEYS = ["type", "priority", "state"] as const;
|
|
62
|
+
const COLLAB_CASE_TYPE_VALUES = new Set([
|
|
63
|
+
"functional",
|
|
64
|
+
"boundary",
|
|
65
|
+
"exception",
|
|
66
|
+
"performance",
|
|
67
|
+
"security",
|
|
68
|
+
"compatibility",
|
|
69
|
+
]);
|
|
70
|
+
const COLLAB_CASE_PRIORITY_VALUES = new Set(["p0", "p1", "p2", "p3"]);
|
|
71
|
+
const COLLAB_CASE_STATE_VALUES = new Set(["draft", "in_review", "approved", "deprecated"]);
|
|
72
|
+
|
|
73
|
+
export const COLLAB_CASE_FIELDS = [
|
|
74
|
+
"title",
|
|
75
|
+
"precondition",
|
|
76
|
+
"type",
|
|
77
|
+
"priority",
|
|
78
|
+
"state",
|
|
79
|
+
"assigneeId",
|
|
80
|
+
] as const;
|
|
81
|
+
|
|
82
|
+
export type CollabCaseField = (typeof COLLAB_CASE_FIELDS)[number];
|
|
83
|
+
|
|
84
|
+
/** 字段 patch(只含出现的 key;undefined key = Y 无意见不触碰) */
|
|
85
|
+
export type CaseFieldsPatch = Partial<Record<CollabCaseField, unknown>>;
|
|
86
|
+
|
|
87
|
+
/** 内容面字段(实际变更才 version+1——与 updateTestCaseFn CONTENT_FIELDS 同口径) */
|
|
88
|
+
const CASE_CONTENT_FIELDS: readonly CollabCaseField[] = [
|
|
89
|
+
"title",
|
|
90
|
+
"precondition",
|
|
91
|
+
"type",
|
|
92
|
+
"priority",
|
|
93
|
+
];
|
|
94
|
+
|
|
95
|
+
/** description + 字段 + steps patch 的统一入参(undefined = 不触碰该面) */
|
|
96
|
+
export interface CaseColumnWrite {
|
|
97
|
+
fields?: CaseFieldsPatch;
|
|
98
|
+
/** Y 权威步骤目标态(readStepsFromDoc 产物;undefined = Y 无意见) */
|
|
99
|
+
steps?: CollabStep[];
|
|
100
|
+
/** 活动归属作者(null = 无归属不写活动) */
|
|
101
|
+
actor: string | null;
|
|
102
|
+
activityExtra?: Record<string, unknown>;
|
|
103
|
+
/** true(exporter)= 同事务显式 touch CollabDocument.updatedAt(锚点 M②) */
|
|
104
|
+
touchSnapshot: boolean;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* 把「Y/patch 目标态」diff 进 TestCase 业务列(单事务内调用):存在性复查 →
|
|
109
|
+
* FK 防御复核 → diff 建数据(无变化即止,不 bump version)→ testCase.update
|
|
110
|
+
* (内容面变更 version 自增)→ ProjectActivity(归属作者 + changedFields)→
|
|
111
|
+
* 可选快照 touch。供 case exporter(Y 全量态)与 internal-apply 离线直落
|
|
112
|
+
* (patch 增量)复用。
|
|
113
|
+
*/
|
|
114
|
+
export async function writeCaseColumns(
|
|
115
|
+
tx: StoryTx,
|
|
116
|
+
opts: { caseId: string; roomName: string } & CaseColumnWrite,
|
|
117
|
+
): Promise<void> {
|
|
118
|
+
const row = await tx.testCase.findUnique({
|
|
119
|
+
where: { id: opts.caseId },
|
|
120
|
+
select: {
|
|
121
|
+
projectId: true,
|
|
122
|
+
title: true,
|
|
123
|
+
precondition: true,
|
|
124
|
+
type: true,
|
|
125
|
+
priority: true,
|
|
126
|
+
state: true,
|
|
127
|
+
assigneeId: true,
|
|
128
|
+
steps: true,
|
|
129
|
+
},
|
|
130
|
+
});
|
|
131
|
+
// 幽灵兜底(首道防线是 store/load 守卫):实体已删则整体跳过
|
|
132
|
+
if (!row) return;
|
|
133
|
+
|
|
134
|
+
const data: Record<string, unknown> = {};
|
|
135
|
+
const changedFields: string[] = [];
|
|
136
|
+
|
|
137
|
+
const fields = opts.fields ?? {};
|
|
138
|
+
const has = (key: CollabCaseField) => fields[key] !== undefined;
|
|
139
|
+
|
|
140
|
+
if (has("title")) {
|
|
141
|
+
const title = fields.title;
|
|
142
|
+
if (typeof title === "string" && title.trim() !== "") {
|
|
143
|
+
if (title !== row.title) {
|
|
144
|
+
changedFields.push("title");
|
|
145
|
+
data.title = title;
|
|
146
|
+
}
|
|
147
|
+
} else {
|
|
148
|
+
console.warn(`[collab-exporters] case 字段 title 非法(${typeof title}),跳过 caseId=${opts.caseId}`);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// precondition(可空文本:null = 清空)
|
|
153
|
+
if (has("precondition")) {
|
|
154
|
+
const precondition = fields.precondition;
|
|
155
|
+
if (precondition !== null && typeof precondition !== "string") {
|
|
156
|
+
console.warn(`[collab-exporters] case 字段 precondition 非法(${typeof precondition}),跳过 caseId=${opts.caseId}`);
|
|
157
|
+
} else if (precondition !== row.precondition) {
|
|
158
|
+
changedFields.push("precondition");
|
|
159
|
+
data.precondition = precondition;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// type/priority/state 白名单(web testcase constants 镜像,apps 不互 import)
|
|
164
|
+
const allowed: Record<(typeof CASE_ENUM_KEYS)[number], Set<string>> = {
|
|
165
|
+
type: COLLAB_CASE_TYPE_VALUES,
|
|
166
|
+
priority: COLLAB_CASE_PRIORITY_VALUES,
|
|
167
|
+
state: COLLAB_CASE_STATE_VALUES,
|
|
168
|
+
};
|
|
169
|
+
for (const key of CASE_ENUM_KEYS) {
|
|
170
|
+
if (has(key) && typeof fields[key] === "string" && fields[key] !== row[key]) {
|
|
171
|
+
if (!allowed[key].has(fields[key])) {
|
|
172
|
+
console.warn(`[collab-exporters] case 字段 ${key} 非法(${fields[key]}),跳过 caseId=${opts.caseId}`);
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
changedFields.push(key);
|
|
176
|
+
data[key] = fields[key];
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// assigneeId(FK 防御;关系形态 connect/disconnect,同 story/bug)
|
|
181
|
+
if (has("assigneeId")) {
|
|
182
|
+
const assigneeId = fields.assigneeId as string | null;
|
|
183
|
+
if (assigneeId !== row.assigneeId) {
|
|
184
|
+
if (assigneeId !== null) {
|
|
185
|
+
const user = await tx.user.findUnique({ where: { id: assigneeId }, select: { id: true } });
|
|
186
|
+
if (!user) {
|
|
187
|
+
console.warn(`[collab-exporters] case assigneeId 引用不存在(${assigneeId}),跳过 caseId=${opts.caseId}`);
|
|
188
|
+
} else {
|
|
189
|
+
changedFields.push("assigneeId");
|
|
190
|
+
data.assignee = { connect: { id: assigneeId } };
|
|
191
|
+
}
|
|
192
|
+
} else {
|
|
193
|
+
changedFields.push("assigneeId");
|
|
194
|
+
data.assignee = { disconnect: true };
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// steps(空 Y.Array = 无意见不触碰;diff 逐位比较;order 落库归一)
|
|
200
|
+
if (opts.steps !== undefined) {
|
|
201
|
+
const rowSteps = normalizeRowSteps(row.steps) ?? [];
|
|
202
|
+
if (!stepsEqual(rowSteps, opts.steps)) {
|
|
203
|
+
changedFields.push("steps");
|
|
204
|
+
data.steps = { steps: stepsWithOrder(opts.steps) };
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// 无 diff 即止:不 bump updatedAt/version(避免无谓的重种子化判定输入)
|
|
209
|
+
if (changedFields.length === 0) return;
|
|
210
|
+
|
|
211
|
+
// **协同落库 version+1**:内容面实际变更才自增(非协同客户端 expectedVersion
|
|
212
|
+
// 乐观锁的语义由服务端落库维护;state/assigneeId 元数据变更不 bump)
|
|
213
|
+
const contentChanged = changedFields.some(
|
|
214
|
+
(key) => key === "steps" || (CASE_CONTENT_FIELDS as readonly string[]).includes(key),
|
|
215
|
+
);
|
|
216
|
+
if (contentChanged) data.version = { increment: 1 };
|
|
217
|
+
|
|
218
|
+
const updated = await tx.testCase.update({
|
|
219
|
+
where: { id: opts.caseId },
|
|
220
|
+
data,
|
|
221
|
+
select: { updatedAt: true },
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
// §10.2 活动:ProjectActivity(无 actorType——按 Story 口径写归属作者
|
|
225
|
+
// userId + metadata 变更流水);actor null 不写(宁缺勿错)。
|
|
226
|
+
if (opts.actor) {
|
|
227
|
+
await tx.projectActivity.create({
|
|
228
|
+
data: {
|
|
229
|
+
projectId: row.projectId,
|
|
230
|
+
userId: opts.actor,
|
|
231
|
+
action: "updated",
|
|
232
|
+
targetType: "testcase",
|
|
233
|
+
targetId: opts.caseId,
|
|
234
|
+
metadata: {
|
|
235
|
+
source: "collab",
|
|
236
|
+
summary: "更新了测试用例",
|
|
237
|
+
changedFields,
|
|
238
|
+
...(opts.activityExtra ?? {}),
|
|
239
|
+
},
|
|
240
|
+
},
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// 锚点 M②:同事务显式 touch 快照 updatedAt = case 回读值(语义同 story/bug
|
|
245
|
+
// 导出器——快照行尚不存在时 updateMany 计 0 跳过,随后快照 upsert 天然 ≥)
|
|
246
|
+
if (opts.touchSnapshot) {
|
|
247
|
+
await tx.collabDocument.updateMany({
|
|
248
|
+
where: { roomName: opts.roomName },
|
|
249
|
+
data: { updatedAt: updated.updatedAt },
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// ── 导出器主体 ────────────────────────────────────────────────────────
|
|
255
|
+
|
|
256
|
+
/** 从 Y.Doc 读取 6 字段 patch(只收 COLLAB_CASE_FIELDS 内出现的 key) */
|
|
257
|
+
export function readCaseFieldsPatch(doc: Y.Doc): CaseFieldsPatch {
|
|
258
|
+
const map = doc.getMap<unknown>(COLLAB_FIELDS_MAP_KEY);
|
|
259
|
+
const patch: CaseFieldsPatch = {};
|
|
260
|
+
for (const key of COLLAB_CASE_FIELDS) {
|
|
261
|
+
const value = map.get(key);
|
|
262
|
+
if (value !== undefined) patch[key] = value;
|
|
263
|
+
}
|
|
264
|
+
return patch;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/** 幽灵 room 守卫:TestCase 行仍存在 → true(归档不算删除) */
|
|
268
|
+
async function assertCaseRoomAlive(
|
|
269
|
+
room: Extract<CollabRoomName, { kind: "entity-case" }>,
|
|
270
|
+
): Promise<boolean> {
|
|
271
|
+
const db = await import("@allbluecn/database");
|
|
272
|
+
const row = await db.prisma.testCase.findUnique({
|
|
273
|
+
where: { id: room.caseId },
|
|
274
|
+
select: { id: true },
|
|
275
|
+
});
|
|
276
|
+
return row != null;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
async function exportCase(input: {
|
|
280
|
+
room: CollabRoomName;
|
|
281
|
+
doc: Y.Doc;
|
|
282
|
+
authorStats: CollabAuthorStat[];
|
|
283
|
+
}): Promise<void> {
|
|
284
|
+
const room = input.room as Extract<CollabRoomName, { kind: "entity-case" }>;
|
|
285
|
+
const db = await import("@allbluecn/database");
|
|
286
|
+
// 回写前复核实体存在(防已删实体被回写复活——锚点 J 第二道防线)
|
|
287
|
+
const alive = await db.prisma.testCase.findUnique({
|
|
288
|
+
where: { id: room.caseId },
|
|
289
|
+
select: { id: true },
|
|
290
|
+
});
|
|
291
|
+
if (!alive) return;
|
|
292
|
+
|
|
293
|
+
const fields = readCaseFieldsPatch(input.doc);
|
|
294
|
+
// 空 Y.Array → null = Y 无意见,不触碰业务 steps 列
|
|
295
|
+
const steps = readStepsFromDoc(input.doc);
|
|
296
|
+
|
|
297
|
+
// per-author 占比(§10.2,共享助手单源)
|
|
298
|
+
const { primary, totalBytes, perAuthor } = computeAuthorAttribution(input.authorStats);
|
|
299
|
+
|
|
300
|
+
await db.prisma.$transaction((tx) =>
|
|
301
|
+
writeCaseColumns(tx, {
|
|
302
|
+
caseId: room.caseId,
|
|
303
|
+
roomName: buildRoomName({ kind: "entity-case", caseId: room.caseId }),
|
|
304
|
+
fields,
|
|
305
|
+
...(steps && { steps }),
|
|
306
|
+
actor: primary,
|
|
307
|
+
activityExtra: { perAuthor, totalBytes },
|
|
308
|
+
touchSnapshot: true,
|
|
309
|
+
}),
|
|
310
|
+
);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/** 注册 TestCase 导出器 + 幽灵 room 守卫(生产经 startCollabServerFromEnv 调用) */
|
|
314
|
+
export function registerCaseCollab(): void {
|
|
315
|
+
registerExporter("entity-case", exportCase);
|
|
316
|
+
registerRoomGuard("entity-case", assertCaseRoomAlive);
|
|
317
|
+
}
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
// AllBlue - 理想之海
|
|
3
|
+
// Copyright (C) 2026 AllBlue Contributors
|
|
4
|
+
//
|
|
5
|
+
// This program is free software: you can redistribute it and/or modify
|
|
6
|
+
// it under the terms of the GNU Affero General Public License as published by
|
|
7
|
+
// the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+
// (at your option) any later version.
|
|
9
|
+
//
|
|
10
|
+
// This program is distributed in the hope that it will be useful,
|
|
11
|
+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+
// GNU Affero General Public License for more details.
|
|
14
|
+
//
|
|
15
|
+
// You should have received a copy of the GNU Affero General Public License
|
|
16
|
+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
17
|
+
|
|
18
|
+
// Design 画布回写导出器(Yjs 多人协作 §5.2 回写 / §7.1 design-canvas 行 /
|
|
19
|
+
// §10.2,任务 29;与 exporters/story.ts、exporters/case.ts 同构——复制适配):
|
|
20
|
+
//
|
|
21
|
+
// room 的 Y 属主面 = canvas 根 Y.Map(画布树,T26 canvas-tree 契约)+ default
|
|
22
|
+
// fragment(Design 文档 TipTap,同 room 双载体 DESIGN_DOC_FRAGMENT):
|
|
23
|
+
// - canvas:yTreeToPackage(Y 树 → LeaferJSONPackage 权威格式,锚点 P 锁定
|
|
24
|
+
// 与 getJSON 产物深度相等)→ DesignPage.canvas;**DesignPage.version 自增
|
|
25
|
+
// 移交服务端**(内容面变更 version+1——与 case 先例同口径,非协同客户端
|
|
26
|
+
// 版本号语义由服务端落库维护;**不产 PageVersion 快照行**——快照体系仍
|
|
27
|
+
// 纯手动触发,简报裁决)。
|
|
28
|
+
// - 文档 fragment:yDocToContent → {html, markdown} → DesignPage.content。
|
|
29
|
+
// 仅 fragment 有真实内容时触碰 content 列(空 fragment 不写——防「从未
|
|
30
|
+
// 种子化文档面」的 room 把非空 content 列清空,宁可少写不吞数据;旁路
|
|
31
|
+
// 清空语义由重种子化兜底在下次打开时对齐)。
|
|
32
|
+
//
|
|
33
|
+
// 活动:Design 无专属活动模型(schema 核实)——按 knowledge 口径落 AuditLog
|
|
34
|
+
// (resourceType="design",action="design.collab_update",metadata 变更流水
|
|
35
|
+
// + per-author 归属统计)。
|
|
36
|
+
//
|
|
37
|
+
// 挂载模式 / 幽灵防御(双重)/ touch 快照(锚点 M②)/ per-author 统计:
|
|
38
|
+
// 逐条同 story/case.ts。共享写入口 writeDesignColumns 同时被 /internal/apply
|
|
39
|
+
// 的离线直落路径(internal-apply.ts design-canvas 分支)复用——同一
|
|
40
|
+
// diff/version/活动/touch 语义单源。
|
|
41
|
+
//
|
|
42
|
+
// 格式迁移:存量 canvas 列可能是旧扁平 SceneDocument——exporter 导出的
|
|
43
|
+
// 权威格式(LeaferJSONPackage)与之必然「不同」,diff 判定为变更自然触发
|
|
44
|
+
// 惰性迁移写回(新格式覆盖旧格式,读侧 normalizeSceneDocument 双格式兼容)。
|
|
45
|
+
|
|
46
|
+
import type { CollabRoomName } from "@allbluecn/collab-core/rooms";
|
|
47
|
+
import { buildRoomName } from "@allbluecn/collab-core/rooms";
|
|
48
|
+
import {
|
|
49
|
+
yTreeToPackage,
|
|
50
|
+
type CollabLeaferJSONPackage,
|
|
51
|
+
} from "@allbluecn/collab-core/canvas-package";
|
|
52
|
+
import { yDocToContent } from "@allbluecn/collab-core/tiptap-export";
|
|
53
|
+
import type * as Y from "yjs";
|
|
54
|
+
import {
|
|
55
|
+
computeAuthorAttribution,
|
|
56
|
+
registerExporter,
|
|
57
|
+
registerRoomGuard,
|
|
58
|
+
type CollabAuthorStat,
|
|
59
|
+
} from "../exporters";
|
|
60
|
+
import { ensureServerDom } from "./knowledge";
|
|
61
|
+
import { isEmptyEditorHtml, storyDocExtensions, type StoryTx } from "./story";
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* 与 web 种子化完全同一扩展组合(Design 文档编辑器同为 NotionDocEditor,
|
|
65
|
+
* story.ts storyDocExtensions 单源复用——漂移 = round-trip 丢内容)。
|
|
66
|
+
*/
|
|
67
|
+
export const designDocExtensions = storyDocExtensions;
|
|
68
|
+
|
|
69
|
+
/** 协同活动审计动作名(knowledge「document.collab_update」命名族对齐) */
|
|
70
|
+
export const COLLAB_DESIGN_UPDATE_ACTION = "design.collab_update";
|
|
71
|
+
|
|
72
|
+
/** 文档 fragment 导出产物(DesignPage.content 列存储形态,DocContent 同构) */
|
|
73
|
+
export interface DesignDocContent {
|
|
74
|
+
html: string;
|
|
75
|
+
markdown: string;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** canvas + 文档 patch 的统一入参(undefined = 不触碰该面) */
|
|
79
|
+
export interface DesignColumnWrite {
|
|
80
|
+
/**
|
|
81
|
+
* 画布目标态:exporter 传 yTreeToPackage 产物(权威格式);internal-apply
|
|
82
|
+
* 离线直落传 patch 原值(LeaferJSONPackage 或旧扁平 SceneDocument,与 web
|
|
83
|
+
* 直写同语义)。undefined = Y 无意见不触碰。
|
|
84
|
+
*/
|
|
85
|
+
canvas?: unknown;
|
|
86
|
+
/** 文档目标态(fragment 导出);undefined = Y 无意见不触碰 */
|
|
87
|
+
content?: DesignDocContent;
|
|
88
|
+
/** 活动归属作者(null = 无归属不写活动,宁缺勿错) */
|
|
89
|
+
actor: string | null;
|
|
90
|
+
/** 活动附加 metadata(exporter 传 perAuthor/totalBytes 归属统计) */
|
|
91
|
+
activityExtra?: Record<string, unknown>;
|
|
92
|
+
/**
|
|
93
|
+
* 锚点 M②:true(exporter)= 同事务显式 touch CollabDocument.updatedAt 为
|
|
94
|
+
* designPage 回读值;false(internal-apply 离线直落)= 不触碰——旁路写必须
|
|
95
|
+
* 让业务列反超快照,重种子化兜底才有正确输入。
|
|
96
|
+
*/
|
|
97
|
+
touchSnapshot: boolean;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** 键序无关的稳定序列化(diff 比较专用;Y Map 键序跨会话不稳定) */
|
|
101
|
+
function stableStringify(value: unknown): string {
|
|
102
|
+
if (value === null || typeof value !== "object") {
|
|
103
|
+
return JSON.stringify(value) ?? "undefined";
|
|
104
|
+
}
|
|
105
|
+
if (Array.isArray(value)) {
|
|
106
|
+
return `[${value.map((item) => stableStringify(item)).join(",")}]`;
|
|
107
|
+
}
|
|
108
|
+
const entries = Object.entries(value as Record<string, unknown>)
|
|
109
|
+
.filter(([, v]) => v !== undefined)
|
|
110
|
+
.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0));
|
|
111
|
+
return `{${entries
|
|
112
|
+
.map(([key, v]) => `${JSON.stringify(key)}:${stableStringify(v)}`)
|
|
113
|
+
.join(",")}}`;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* 把「Y/patch 目标态」diff 进 DesignPage 业务列(单事务内调用):存在性复查 →
|
|
118
|
+
* diff 建数据(无变化即止,不 bump updatedAt/version)→ designPage.update
|
|
119
|
+
* (内容面变更 version 自增——DesignPage.version 自增移交服务端,不产
|
|
120
|
+
* PageVersion 快照行)→ AuditLog 活动(归属作者 + changedFields)→
|
|
121
|
+
* 可选快照 touch。供 design exporter(Y 全量态)与 internal-apply 离线直落
|
|
122
|
+
* (patch 增量)复用。
|
|
123
|
+
*/
|
|
124
|
+
export async function writeDesignColumns(
|
|
125
|
+
tx: StoryTx,
|
|
126
|
+
opts: { pageId: string; roomName: string } & DesignColumnWrite,
|
|
127
|
+
): Promise<void> {
|
|
128
|
+
const row = await tx.designPage.findUnique({
|
|
129
|
+
where: { id: opts.pageId },
|
|
130
|
+
select: { canvas: true, content: true },
|
|
131
|
+
});
|
|
132
|
+
// 幽灵兜底(首道防线是 store/load 守卫):实体已删则整体跳过
|
|
133
|
+
if (!row) return;
|
|
134
|
+
|
|
135
|
+
const data: Record<string, unknown> = {};
|
|
136
|
+
const changedFields: string[] = [];
|
|
137
|
+
|
|
138
|
+
// canvas:稳定序列化 diff(键序无关)。存量旧扁平格式与权威格式必然不等
|
|
139
|
+
// ——首次回写自然完成惰性迁移(新格式覆盖)
|
|
140
|
+
if (opts.canvas !== undefined && stableStringify(opts.canvas) !== stableStringify(row.canvas)) {
|
|
141
|
+
changedFields.push("canvas");
|
|
142
|
+
data.canvas = opts.canvas as Record<string, unknown>;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// 文档:仅 fragment 有真实内容时触碰(空 fragment 不写——防未种子化文档面
|
|
146
|
+
// 的 room 清空非空 content 列;见文件头「文档 fragment」注)。diff 以 html
|
|
147
|
+
// 为准(渲染权威;markdown 是派生镜像——序列化器版本差不该触发 version+1)
|
|
148
|
+
if (opts.content !== undefined && !isEmptyEditorHtml(opts.content.html)) {
|
|
149
|
+
const currentHtml =
|
|
150
|
+
typeof row.content === "object" && row.content !== null
|
|
151
|
+
? (row.content as { html?: unknown }).html
|
|
152
|
+
: undefined;
|
|
153
|
+
if (opts.content.html !== currentHtml) {
|
|
154
|
+
changedFields.push("content");
|
|
155
|
+
data.content = { html: opts.content.html, markdown: opts.content.markdown };
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// 无 diff 即止:不 bump updatedAt/version(避免无谓的重种子化判定输入)
|
|
160
|
+
if (changedFields.length === 0) return;
|
|
161
|
+
|
|
162
|
+
// **DesignPage.version 自增移交服务端**(内容面 canvas/content 任一实际
|
|
163
|
+
// 变更;与 case 内容面口径一致)。不产 PageVersion 快照行——快照体系纯手动
|
|
164
|
+
// 触发(createDesignPageVersionFn),协同态零自动快照(简报裁决)。
|
|
165
|
+
data.version = { increment: 1 };
|
|
166
|
+
|
|
167
|
+
const updated = await tx.designPage.update({
|
|
168
|
+
where: { id: opts.pageId },
|
|
169
|
+
data,
|
|
170
|
+
select: { updatedAt: true },
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
// §10.2 活动:Design 无专属活动模型 → AuditLog(knowledge 口径,宁缺勿错)
|
|
174
|
+
if (opts.actor) {
|
|
175
|
+
await tx.auditLog.create({
|
|
176
|
+
data: {
|
|
177
|
+
userId: opts.actor,
|
|
178
|
+
action: COLLAB_DESIGN_UPDATE_ACTION,
|
|
179
|
+
resourceType: "design",
|
|
180
|
+
resourceId: opts.pageId,
|
|
181
|
+
metadata: {
|
|
182
|
+
source: "collab",
|
|
183
|
+
summary: "更新了设计页面",
|
|
184
|
+
changedFields,
|
|
185
|
+
...(opts.activityExtra ?? {}),
|
|
186
|
+
},
|
|
187
|
+
},
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// 锚点 M②:同事务显式 touch 快照 updatedAt = designPage 回读值(语义同
|
|
192
|
+
// story/case——快照行尚不存在时 updateMany 计 0 跳过,随后快照 upsert 天然 ≥)
|
|
193
|
+
if (opts.touchSnapshot) {
|
|
194
|
+
await tx.collabDocument.updateMany({
|
|
195
|
+
where: { roomName: opts.roomName },
|
|
196
|
+
data: { updatedAt: updated.updatedAt },
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** 从 Y.Doc 导出画布权威格式(room 未种子化画布面 → undefined 不触碰) */
|
|
202
|
+
export function readCanvasPackage(doc: Y.Doc): CollabLeaferJSONPackage | undefined {
|
|
203
|
+
return yTreeToPackage(doc) ?? undefined;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* 从 Y.Doc 导出文档 fragment(同 room 双载体 DESIGN_DOC_FRAGMENT="default")。
|
|
208
|
+
* 空产物(编辑器空文档)返回 undefined = Y 无意见不触碰 content 列。
|
|
209
|
+
*/
|
|
210
|
+
export function readDesignDocContent(doc: Y.Doc): DesignDocContent | undefined {
|
|
211
|
+
const content = yDocToContent(doc, designDocExtensions());
|
|
212
|
+
if (isEmptyEditorHtml(content.html)) return undefined;
|
|
213
|
+
return { html: content.html, markdown: content.markdown };
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** 幽灵 room 守卫:DesignPage 行仍存在 → true(硬删,无软删位) */
|
|
217
|
+
async function assertDesignRoomAlive(
|
|
218
|
+
room: Extract<CollabRoomName, { kind: "design-canvas" }>,
|
|
219
|
+
): Promise<boolean> {
|
|
220
|
+
const db = await import("@allbluecn/database");
|
|
221
|
+
const page = await db.prisma.designPage.findUnique({
|
|
222
|
+
where: { id: room.pageId },
|
|
223
|
+
select: { id: true },
|
|
224
|
+
});
|
|
225
|
+
return page != null;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
async function exportDesign(input: {
|
|
229
|
+
room: CollabRoomName;
|
|
230
|
+
doc: Y.Doc;
|
|
231
|
+
authorStats: CollabAuthorStat[];
|
|
232
|
+
}): Promise<void> {
|
|
233
|
+
const room = input.room as Extract<CollabRoomName, { kind: "design-canvas" }>;
|
|
234
|
+
const db = await import("@allbluecn/database");
|
|
235
|
+
// 回写前复核实体存在(防已删实体被回写复活——锚点 J 第二道防线)
|
|
236
|
+
const alive = await db.prisma.designPage.findUnique({
|
|
237
|
+
where: { id: room.pageId },
|
|
238
|
+
select: { id: true },
|
|
239
|
+
});
|
|
240
|
+
if (!alive) return;
|
|
241
|
+
|
|
242
|
+
await ensureServerDom();
|
|
243
|
+
const canvas = readCanvasPackage(input.doc);
|
|
244
|
+
const content = readDesignDocContent(input.doc);
|
|
245
|
+
// 双面皆无(空 room)→ 无可回写
|
|
246
|
+
if (canvas === undefined && content === undefined) return;
|
|
247
|
+
|
|
248
|
+
// per-author 占比(§10.2,共享助手单源)
|
|
249
|
+
const { primary, totalBytes, perAuthor } = computeAuthorAttribution(input.authorStats);
|
|
250
|
+
|
|
251
|
+
await db.prisma.$transaction((tx) =>
|
|
252
|
+
writeDesignColumns(tx, {
|
|
253
|
+
pageId: room.pageId,
|
|
254
|
+
roomName: buildRoomName({ kind: "design-canvas", pageId: room.pageId }),
|
|
255
|
+
canvas,
|
|
256
|
+
content,
|
|
257
|
+
actor: primary,
|
|
258
|
+
activityExtra: { perAuthor, totalBytes },
|
|
259
|
+
touchSnapshot: true,
|
|
260
|
+
}),
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/** 注册 Design 导出器 + 幽灵 room 守卫(生产经 startCollabServerFromEnv 调用) */
|
|
265
|
+
export function registerDesignCollab(): void {
|
|
266
|
+
registerExporter("design-canvas", exportDesign);
|
|
267
|
+
registerRoomGuard("design-canvas", assertDesignRoomAlive);
|
|
268
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
// AllBlue - 理想之海
|
|
3
|
+
// Copyright (C) 2026 AllBlue Contributors
|
|
4
|
+
//
|
|
5
|
+
// This program is free software: you can redistribute it and/or modify
|
|
6
|
+
// it under the terms of the GNU Affero General Public License as published by
|
|
7
|
+
// the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+
// (at your option) any later version.
|
|
9
|
+
//
|
|
10
|
+
// This program is distributed in the hope that it will be useful,
|
|
11
|
+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+
// GNU Affero General Public License for more details.
|
|
14
|
+
//
|
|
15
|
+
// You should have received a copy of the GNU Affero General Public License
|
|
16
|
+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
17
|
+
|
|
18
|
+
// entity room 导出器共享 helper(任务 22:bug/case 导出器共用的 Y 结构常量与
|
|
19
|
+
// steps 归一化工具;纯函数零 IO。story 导出器保留自有实现不动——存量为先)。
|
|
20
|
+
|
|
21
|
+
import type { CollabStep } from "@allbluecn/collab-core/steps";
|
|
22
|
+
|
|
23
|
+
/** 字段值所在根 Y.Map 的 key(web 写端 COLLAB_FIELDS_MAP_KEY 的镜像) */
|
|
24
|
+
export { COLLAB_FIELDS_MAP_KEY } from "./story";
|
|
25
|
+
|
|
26
|
+
/** $transaction 回调的 tx 类型(story-transfer.ts 同款推导,story.ts 导出) */
|
|
27
|
+
export type { StoryTx } from "./story";
|
|
28
|
+
|
|
29
|
+
/** 步骤列表落库形态:order 按索引+1 归一(业务 zod schema 要求 1 起唯一) */
|
|
30
|
+
export function stepsWithOrder(steps: CollabStep[]): Array<{
|
|
31
|
+
order: number;
|
|
32
|
+
action: string;
|
|
33
|
+
expected?: string;
|
|
34
|
+
}> {
|
|
35
|
+
return steps.map((step, index) => ({
|
|
36
|
+
order: index + 1,
|
|
37
|
+
action: step.action,
|
|
38
|
+
...(step.expected !== undefined && { expected: step.expected }),
|
|
39
|
+
}));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* 归一化业务列 steps JSON(Bug.reproduceSteps / TestCase.steps 同构)的比对
|
|
44
|
+
* 形态(只取 Y 拥有的 steps 列表;Bug 的 expectedResult/actualResult 是旁路
|
|
45
|
+
* 保留面不参与 diff)。非对象/无 steps 键/空列表 → null。
|
|
46
|
+
*/
|
|
47
|
+
export function normalizeRowSteps(
|
|
48
|
+
raw: unknown,
|
|
49
|
+
): Array<{ action: string; expected?: string }> | null {
|
|
50
|
+
if (typeof raw !== "object" || raw === null) return null;
|
|
51
|
+
const list = (raw as Record<string, unknown>).steps;
|
|
52
|
+
if (!Array.isArray(list)) return null;
|
|
53
|
+
const out: Array<{ action: string; expected?: string }> = [];
|
|
54
|
+
for (const item of list) {
|
|
55
|
+
if (typeof item !== "object" || item === null) continue;
|
|
56
|
+
const record = item as Record<string, unknown>;
|
|
57
|
+
out.push({
|
|
58
|
+
action: typeof record.action === "string" ? record.action : "",
|
|
59
|
+
...(typeof record.expected === "string" &&
|
|
60
|
+
record.expected !== "" && { expected: record.expected }),
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
return out.length > 0 ? out : null;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** 步骤集比对(action/expected 逐位相等;长度不等即变更) */
|
|
67
|
+
export function stepsEqual(
|
|
68
|
+
a: Array<{ action: string; expected?: string }>,
|
|
69
|
+
b: Array<{ action: string; expected?: string }>,
|
|
70
|
+
): boolean {
|
|
71
|
+
if (a.length !== b.length) return false;
|
|
72
|
+
return a.every(
|
|
73
|
+
(step, i) => step.action === b[i]!.action && step.expected === b[i]!.expected,
|
|
74
|
+
);
|
|
75
|
+
}
|