soma-core 2.2.9 → 2.2.10
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/core/build/compiler/soma-precompile.js +6 -2
- package/core/client/engines/diff/code.ts +205 -0
- package/core/client/engines/diff/index.ts +27 -1
- package/core/client/engines/diff/port.ts +75 -0
- package/core/server/host/source-packages.cjs +3 -0
- package/core/shared/registry-generated/limits.ts +6 -2
- package/dist/browser/runtime-bundle.manifest.json +1 -0
- package/dist/browser/soma-runtime.js +1211 -963
- package/dist/core/index.cjs +1211 -963
- package/dist/host-native/aot-builder.cjs +346 -223
- package/dist/host-native/native-server.cjs +9 -2
- package/dist/lsp/component-tree.cjs +6 -2
- package/dist/types/client/engines/diff/code.d.ts +27 -0
- package/dist/types/client/engines/diff/index.d.ts +2 -1
- package/dist/types/client/engines/diff/port.d.ts +77 -0
- package/dist/types/server/host/source-packages.d.cts +5 -0
- package/dist/types/shared/registry-generated/limits.d.ts +4 -0
- package/host-native/src/aot-builder.ts +147 -91
- package/host-native/src/public-packages.cjs +147 -50
- package/package.json +1 -1
|
@@ -6745,7 +6745,9 @@ var SOMA_PIPELINE_LIMITS = {
|
|
|
6745
6745
|
"bodyAt": "\u65E0 key \u6761\u76EE\u6309\u6B63\u6587\u914D\u5BF9\u7684\u76F8\u4F3C\u5EA6\u9608\u503C\uFF08\u7B14\u8BB0 C.2 ItemOpts.match.bodyAt 0.5\uFF09",
|
|
6746
6746
|
"maxAlignTokens": "\u5E8F\u5217\u5BF9\u9F50\u7684\u4E0D\u540C\u952E\u6570\u4E0A\u9650\uFF08\u6BCF\u4E2A\u952E\u6620\u5C04\u4E3A\u4E00\u4E2A BMP \u79C1\u7528\u533A\u5B57\u7B26\uFF0CU+E000\u2013U+F8FF \u5171 6400 \u4E2A\uFF09",
|
|
6747
6747
|
"maxPairMatrix": "\u4E00\u6BB5\u5220\u589E\u533A\u95F4\u5185\u53E5 / \u6BB5\u914D\u5BF9\u7684\u5019\u9009\u77E9\u9635\u4E0A\u9650\uFF08\u5220\u6570 \xD7 \u589E\u6570\uFF09\uFF0C\u8D85\u51FA\u4E0D\u914D\u5BF9\u3001\u6309\u5220 + \u589E\u5448\u73B0\uFF1B\u7ECF\u9A8C\u503C\uFF0C\u8BC1\u636E\u5F31",
|
|
6748
|
-
"maxPairCandidates": "\u65E0 key \u6761\u76EE\u6309\u76F8\u4F3C\u5EA6\u914D\u5BF9\u7684\u5019\u9009\u77E9\u9635\u4E0A\u9650\uFF08\u5269\u4F59\u65E7\u6761\u76EE \xD7 \u5269\u4F59\u65B0\u6761\u76EE\uFF09\uFF1B\u7ECF\u9A8C\u503C\uFF0C\u8BC1\u636E\u5F31"
|
|
6748
|
+
"maxPairCandidates": "\u65E0 key \u6761\u76EE\u6309\u76F8\u4F3C\u5EA6\u914D\u5BF9\u7684\u5019\u9009\u77E9\u9635\u4E0A\u9650\uFF08\u5269\u4F59\u65E7\u6761\u76EE \xD7 \u5269\u4F59\u65B0\u6761\u76EE\uFF09\uFF1B\u7ECF\u9A8C\u503C\uFF0C\u8BC1\u636E\u5F31",
|
|
6749
|
+
"codeContext": "\u4EE3\u7801\u5DEE\u5F02\u6BCF\u5757\u7684\u4E0A\u4E0B\u6587\u884C\u6570\uFF08\u7B14\u8BB0 C.5\u300C\u4E0A\u4E0B\u6587 3 \u884C\u300D\uFF0C\u540C git diff -U3 / VS Code\uFF09",
|
|
6750
|
+
"codeIntraMaxLine": "\u4EE3\u7801\u6539\u884C\u505A\u884C\u5185\u5B57\u7B26\u5DEE\u5F02\u7684\u5355\u884C\u957F\u5EA6\u4E0A\u9650\uFF08\u5B57\u7B26\uFF09\uFF1B\u66F4\u957F\u7684\u884C\u53EA\u6807\u6574\u884C\u589E\u5220\uFF08\u7ECF\u9A8C\u503C\uFF1A\u538B\u7F29\u540E\u7684\u957F\u884C\u9010\u5B57\u6BD4\u8F83\u65E0\u610F\u4E49\u4E14\u8017\u65F6\uFF09"
|
|
6749
6751
|
},
|
|
6750
6752
|
"maxChars": 2e5,
|
|
6751
6753
|
"maxCharsPerCompare": 2e4,
|
|
@@ -6760,7 +6762,9 @@ var SOMA_PIPELINE_LIMITS = {
|
|
|
6760
6762
|
"bodyAt": 0.5,
|
|
6761
6763
|
"maxAlignTokens": 6400,
|
|
6762
6764
|
"maxPairMatrix": 2500,
|
|
6763
|
-
"maxPairCandidates": 25e4
|
|
6765
|
+
"maxPairCandidates": 25e4,
|
|
6766
|
+
"codeContext": 3,
|
|
6767
|
+
"codeIntraMaxLine": 500
|
|
6764
6768
|
},
|
|
6765
6769
|
"highlight": {
|
|
6766
6770
|
"basis": {
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
// 代码差异与 Markdown 差异流水线(内容展示批次 ⑥;纯函数 + 适配器调用,零 DOM,不 import soma 其它模块)。
|
|
2
|
+
// 代码:按行 LCS 对齐(alignKeys)→ 删增区间内按相似度配对成「改行」并做行内字符差异 → 按上下文行数切块(hunk)。
|
|
3
|
+
// Markdown:源码按块切分(空行分隔、围栏代码整块)→ 块序列对齐(键 = 块类型 + 归一化文字)→ 同类型且相似的块配对,
|
|
4
|
+
// 块内做中文词级差异;结构变化(标题层级、列表有序 / 无序、代码语言)给块级说明。渲染交给组件(块用 Markdown 端口渲染)。
|
|
5
|
+
|
|
6
|
+
import type { CodeDiff, CodeDiffHunk, CodeDiffLine, DiffFragment, DiffPart, MarkdownBlockKind, MarkdownDiff, MarkdownDiffBlock } from './port';
|
|
7
|
+
import { alignKeys, diceSimilarity, hasHan, pairAligned, safeDiff, tidy, type ProseRun } from './prose';
|
|
8
|
+
|
|
9
|
+
export interface CodeConfig {
|
|
10
|
+
readonly maxChars: number;
|
|
11
|
+
readonly maxAlignTokens: number;
|
|
12
|
+
readonly maxPairMatrix: number;
|
|
13
|
+
readonly pairAt: number;
|
|
14
|
+
/** 每块上下文行数 */
|
|
15
|
+
readonly context: number;
|
|
16
|
+
/** 行内字符差异只对不超过此长度的行做 */
|
|
17
|
+
readonly intraMaxLine: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const toFrags = (parts: readonly DiffPart[]): DiffFragment[] =>
|
|
21
|
+
tidy(parts.map((p) => ({ v: p.text, op: p.op === 'equal' ? 'eq' as const : p.op === 'insert' ? 'ins' as const : 'del' as const })));
|
|
22
|
+
|
|
23
|
+
const linesOf = (s: string): string[] => (s === '' ? [] : s.replace(/\r\n?/gu, '\n').split('\n'));
|
|
24
|
+
const squash = (s: string): string => s.replace(/\s+/gu, '');
|
|
25
|
+
|
|
26
|
+
/** 行级操作 → 按上下文切块(纯函数) */
|
|
27
|
+
export function hunksOf(lines: readonly CodeDiffLine[], context: number): CodeDiffHunk[] {
|
|
28
|
+
const changed: number[] = [];
|
|
29
|
+
lines.forEach((l, i) => { if (l.op !== 'eq') changed.push(i); });
|
|
30
|
+
if (changed.length === 0) return [];
|
|
31
|
+
const ranges: [number, number][] = [];
|
|
32
|
+
for (const i of changed) {
|
|
33
|
+
const from = Math.max(0, i - context);
|
|
34
|
+
const to = Math.min(lines.length - 1, i + context);
|
|
35
|
+
const last = ranges[ranges.length - 1];
|
|
36
|
+
if (last && from <= last[1] + 1) last[1] = Math.max(last[1], to);
|
|
37
|
+
else ranges.push([from, to]);
|
|
38
|
+
}
|
|
39
|
+
return ranges.map(([from, to]) => {
|
|
40
|
+
const slice = lines.slice(from, to + 1);
|
|
41
|
+
const firstA = slice.find((l) => l.a !== undefined)?.a ?? 0;
|
|
42
|
+
const firstB = slice.find((l) => l.b !== undefined)?.b ?? 0;
|
|
43
|
+
return {
|
|
44
|
+
aStart: firstA, aLines: slice.filter((l) => l.a !== undefined).length,
|
|
45
|
+
bStart: firstB, bLines: slice.filter((l) => l.b !== undefined).length,
|
|
46
|
+
from, to, lines: slice,
|
|
47
|
+
};
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export async function runCode(run: ProseRun, cfg: CodeConfig, a: string, b: string): Promise<CodeDiff> {
|
|
52
|
+
const aLines = linesOf(a);
|
|
53
|
+
const bLines = linesOf(b);
|
|
54
|
+
const whitespaceOnly = a !== b && squash(a) === squash(b);
|
|
55
|
+
if (a === b) {
|
|
56
|
+
const lines = aLines.map((text, i): CodeDiffLine => ({ op: 'eq', a: i + 1, b: i + 1, text }));
|
|
57
|
+
return { kind: 'same', lines, hunks: [], stats: { added: 0, removed: 0, modified: 0 }, whitespaceOnly: false };
|
|
58
|
+
}
|
|
59
|
+
const plain = (degraded: 'too-large' | 'timeout'): CodeDiff => {
|
|
60
|
+
const lines: CodeDiffLine[] = [
|
|
61
|
+
...aLines.map((text, i): CodeDiffLine => ({ op: 'del', a: i + 1, text })),
|
|
62
|
+
...bLines.map((text, i): CodeDiffLine => ({ op: 'ins', b: i + 1, text })),
|
|
63
|
+
];
|
|
64
|
+
return { kind: 'changed', lines, hunks: [{ aStart: 1, aLines: aLines.length, bStart: 1, bLines: bLines.length, from: 0, to: lines.length - 1, lines }], stats: { added: bLines.length, removed: aLines.length, modified: 0 }, whitespaceOnly, degraded };
|
|
65
|
+
};
|
|
66
|
+
if (a.length + b.length > cfg.maxChars) return plain('too-large');
|
|
67
|
+
const ops = await alignKeys(run, aLines, bLines, cfg.maxAlignTokens);
|
|
68
|
+
if (ops === null) return plain(run.degraded === 'timeout' ? 'timeout' : 'too-large');
|
|
69
|
+
const units = pairAligned(ops, aLines, bLines, cfg.pairAt, cfg.maxPairMatrix);
|
|
70
|
+
const lines: CodeDiffLine[] = [];
|
|
71
|
+
let added = 0;
|
|
72
|
+
let removed = 0;
|
|
73
|
+
let modified = 0;
|
|
74
|
+
// 改行:先列删行、再列增行(合并视图「删行在增行上方」);连续的改行成组输出
|
|
75
|
+
let pendingDel: CodeDiffLine[] = [];
|
|
76
|
+
let pendingIns: CodeDiffLine[] = [];
|
|
77
|
+
const flush = (): void => { lines.push(...pendingDel, ...pendingIns); pendingDel = []; pendingIns = []; };
|
|
78
|
+
for (const u of units) {
|
|
79
|
+
if (u.op === 'eq') { flush(); lines.push({ op: 'eq', a: u.a + 1, b: u.b + 1, text: aLines[u.a]! }); continue; }
|
|
80
|
+
if (u.op === 'del') { removed += 1; pendingDel.push({ op: 'del', a: u.a + 1, text: aLines[u.a]! }); continue; }
|
|
81
|
+
if (u.op === 'ins') { added += 1; pendingIns.push({ op: 'ins', b: u.b + 1, text: bLines[u.b]! }); continue; }
|
|
82
|
+
modified += 1;
|
|
83
|
+
const la = aLines[u.a]!;
|
|
84
|
+
const lb = bLines[u.b]!;
|
|
85
|
+
let frags: DiffFragment[] | null = null;
|
|
86
|
+
if (la.length <= cfg.intraMaxLine && lb.length <= cfg.intraMaxLine) {
|
|
87
|
+
const parts = await safeDiff(run, la, lb, 'chars');
|
|
88
|
+
if (parts !== null) frags = toFrags(parts);
|
|
89
|
+
}
|
|
90
|
+
pendingDel.push({ op: 'del', a: u.a + 1, text: la, pair: u.b + 1, ...(frags ? { fragments: frags.filter((f) => f.op !== 'ins') } : {}) });
|
|
91
|
+
pendingIns.push({ op: 'ins', b: u.b + 1, text: lb, pair: u.a + 1, ...(frags ? { fragments: frags.filter((f) => f.op !== 'del') } : {}) });
|
|
92
|
+
}
|
|
93
|
+
flush();
|
|
94
|
+
const out: CodeDiff = { kind: 'changed', lines, hunks: hunksOf(lines, cfg.context), stats: { added, removed, modified }, whitespaceOnly };
|
|
95
|
+
return run.degraded !== undefined ? { ...out, degraded: run.degraded === 'timeout' ? 'timeout' : 'too-large' } : out;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// ── Markdown ──
|
|
99
|
+
|
|
100
|
+
/** 源码 → 块(空行分隔;围栏代码整块,围栏里的空行不切);纯函数 */
|
|
101
|
+
export function splitMarkdownBlocks(source: string): string[] {
|
|
102
|
+
const out: string[] = [];
|
|
103
|
+
let cur: string[] = [];
|
|
104
|
+
let fence: string | null = null;
|
|
105
|
+
for (const line of source.replace(/\r\n?/gu, '\n').split('\n')) {
|
|
106
|
+
const m = /^\s{0,3}(`{3,}|~{3,})/u.exec(line);
|
|
107
|
+
if (fence !== null) {
|
|
108
|
+
cur.push(line);
|
|
109
|
+
if (m && m[1]!.startsWith(fence)) { out.push(cur.join('\n')); cur = []; fence = null; }
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
if (m) { if (cur.length > 0) out.push(cur.join('\n')); cur = [line]; fence = m[1]!.slice(0, 3); continue; }
|
|
113
|
+
if (line.trim() === '') { if (cur.length > 0) out.push(cur.join('\n')); cur = []; continue; }
|
|
114
|
+
cur.push(line);
|
|
115
|
+
}
|
|
116
|
+
if (cur.length > 0) out.push(cur.join('\n'));
|
|
117
|
+
return out;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export function markdownBlockKind(block: string): { kind: MarkdownBlockKind; detail: string } {
|
|
121
|
+
const first = block.split('\n', 1)[0]!.trimStart();
|
|
122
|
+
const h = /^(#{1,6})\s/u.exec(first);
|
|
123
|
+
if (h) return { kind: 'heading', detail: String(h[1]!.length) };
|
|
124
|
+
const f = /^(`{3,}|~{3,})\s*([^\s`]*)/u.exec(first);
|
|
125
|
+
if (f) return { kind: 'code', detail: f[2] ?? '' };
|
|
126
|
+
if (/^>/u.test(first)) return { kind: 'quote', detail: '' };
|
|
127
|
+
if (/^\d+[.)]\s/u.test(first)) return { kind: 'list', detail: 'ordered' };
|
|
128
|
+
if (/^[-*+]\s/u.test(first)) return { kind: 'list', detail: 'bullet' };
|
|
129
|
+
if (/^\|/u.test(first)) return { kind: 'table', detail: '' };
|
|
130
|
+
if (/^(-{3,}|\*{3,}|_{3,})\s*$/u.test(first)) return { kind: 'hr', detail: '' };
|
|
131
|
+
return { kind: 'paragraph', detail: '' };
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** 块的可比文字:去掉块首标记(# / > / 列表符号 / 围栏行),空白归一 */
|
|
135
|
+
export function markdownBlockText(block: string, kind: MarkdownBlockKind): string {
|
|
136
|
+
if (kind === 'code') return block.split('\n').slice(1).filter((l, i, arr) => !(i === arr.length - 1 && /^\s*(`{3,}|~{3,})\s*$/u.test(l))).join('\n');
|
|
137
|
+
return block.split('\n').map((l) => l.replace(/^\s{0,3}(#{1,6}\s+|>\s?|[-*+]\s+(\[[ xX]\]\s+)?|\d+[.)]\s+)/u, '')).join('\n').trim();
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export interface MarkdownConfig extends CodeConfig { readonly maxBlocks: number }
|
|
141
|
+
|
|
142
|
+
export async function runMarkdown(run: ProseRun, cfg: MarkdownConfig, a: string, b: string): Promise<MarkdownDiff> {
|
|
143
|
+
if (a === b) {
|
|
144
|
+
const blocks = splitMarkdownBlocks(a).map((s): MarkdownDiffBlock => ({ op: 'eq', kind: markdownBlockKind(s).kind, before: s, after: s }));
|
|
145
|
+
return { kind: 'same', blocks, stats: { added: 0, removed: 0, modified: 0, same: blocks.length } };
|
|
146
|
+
}
|
|
147
|
+
const aBlocks = splitMarkdownBlocks(a);
|
|
148
|
+
const bBlocks = splitMarkdownBlocks(b);
|
|
149
|
+
if (a.length + b.length > cfg.maxChars || aBlocks.length + bBlocks.length > cfg.maxBlocks) {
|
|
150
|
+
return { kind: 'changed', blocks: [], stats: { added: 0, removed: 0, modified: 0, same: 0 }, degraded: 'too-large' };
|
|
151
|
+
}
|
|
152
|
+
const meta = (s: string): { kind: MarkdownBlockKind; detail: string; text: string } => { const k = markdownBlockKind(s); return { ...k, text: markdownBlockText(s, k.kind) }; };
|
|
153
|
+
const am = aBlocks.map(meta);
|
|
154
|
+
const bm = bBlocks.map(meta);
|
|
155
|
+
// 对齐键:类型 + 归一化文字(标题层级、列表种类不进键,变化由块级说明给出)
|
|
156
|
+
const key = (m: { kind: string; text: string }): string => `${m.kind}\u0000${m.text.replace(/\s+/gu, ' ')}`;
|
|
157
|
+
const ops = await alignKeys(run, am.map(key), bm.map(key), cfg.maxAlignTokens);
|
|
158
|
+
if (ops === null) return { kind: 'changed', blocks: [], stats: { added: 0, removed: 0, modified: 0, same: 0 }, degraded: run.degraded === 'timeout' ? 'timeout' : 'too-large' };
|
|
159
|
+
// 配对:同类型才配(相似度用块文字);不同类型的键前缀不同,相似度天然低
|
|
160
|
+
const units = pairAligned(ops, am.map((m) => `${m.kind}:${m.text}`), bm.map((m) => `${m.kind}:${m.text}`), cfg.pairAt, cfg.maxPairMatrix);
|
|
161
|
+
const blocks: MarkdownDiffBlock[] = [];
|
|
162
|
+
let added = 0;
|
|
163
|
+
let removed = 0;
|
|
164
|
+
let modified = 0;
|
|
165
|
+
let same = 0;
|
|
166
|
+
for (const u of units) {
|
|
167
|
+
if (u.op === 'eq') {
|
|
168
|
+
const x = am[u.a]!;
|
|
169
|
+
const y = bm[u.b]!;
|
|
170
|
+
const notes = structureNotes(x, y);
|
|
171
|
+
if (notes.length === 0 && aBlocks[u.a] === bBlocks[u.b]) { same += 1; blocks.push({ op: 'eq', kind: y.kind, before: aBlocks[u.a]!, after: bBlocks[u.b]! }); continue; }
|
|
172
|
+
modified += 1;
|
|
173
|
+
blocks.push({ op: 'mod', kind: y.kind, before: aBlocks[u.a]!, after: bBlocks[u.b]!, fragments: [{ v: y.text, op: 'eq' }], ...(notes.length > 0 ? { notes } : {}) });
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
if (u.op === 'del') { removed += 1; blocks.push({ op: 'del', kind: am[u.a]!.kind, before: aBlocks[u.a]! }); continue; }
|
|
177
|
+
if (u.op === 'ins') { added += 1; blocks.push({ op: 'ins', kind: bm[u.b]!.kind, after: bBlocks[u.b]! }); continue; }
|
|
178
|
+
const x = am[u.a]!;
|
|
179
|
+
const y = bm[u.b]!;
|
|
180
|
+
if (x.kind !== y.kind) {
|
|
181
|
+
removed += 1; added += 1;
|
|
182
|
+
blocks.push({ op: 'del', kind: x.kind, before: aBlocks[u.a]! }, { op: 'ins', kind: y.kind, after: bBlocks[u.b]! });
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
modified += 1;
|
|
186
|
+
const mode = hasHan(x.text) || hasHan(y.text) ? 'words-zh' as const : 'words' as const;
|
|
187
|
+
const parts = x.kind === 'code' ? null : await safeDiff(run, x.text, y.text, mode);
|
|
188
|
+
const notes = structureNotes(x, y);
|
|
189
|
+
blocks.push({
|
|
190
|
+
op: 'mod', kind: y.kind, before: aBlocks[u.a]!, after: bBlocks[u.b]!,
|
|
191
|
+
...(parts !== null ? { fragments: toFrags(parts) } : {}),
|
|
192
|
+
...(notes.length > 0 ? { notes } : {}),
|
|
193
|
+
similarity: diceSimilarity(x.text, y.text),
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
const out: MarkdownDiff = { kind: 'changed', blocks, stats: { added, removed, modified, same } };
|
|
197
|
+
return run.degraded !== undefined ? { ...out, degraded: run.degraded === 'timeout' ? 'timeout' : 'too-large' } : out;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** 结构变化说明:标题层级、列表有序 / 无序、代码块语言(值只给数据,文案由组件定) */
|
|
201
|
+
function structureNotes(x: { kind: MarkdownBlockKind; detail: string }, y: { kind: MarkdownBlockKind; detail: string }): { readonly what: 'heading-level' | 'list-type' | 'code-language'; readonly before: string; readonly after: string }[] {
|
|
202
|
+
if (x.kind !== y.kind || x.detail === y.detail) return [];
|
|
203
|
+
const what = x.kind === 'heading' ? 'heading-level' as const : x.kind === 'list' ? 'list-type' as const : x.kind === 'code' ? 'code-language' as const : null;
|
|
204
|
+
return what === null ? [] : [{ what, before: x.detail, after: y.detail }];
|
|
205
|
+
}
|
|
@@ -2,18 +2,21 @@
|
|
|
2
2
|
// 输入校验(类型、粒度、上限)与散文 / 条目流水线在这里,换适配器也一样受约束。
|
|
3
3
|
// 上限与阈值的缺省只读 design(`limits.json#contentEngines.diff`);调用方给的时限 / 字数只能比缺省更小。
|
|
4
4
|
export type {
|
|
5
|
+
CodeDiff, CodeDiffHunk, CodeDiffLine, CodeDiffOptions, MarkdownBlockKind, MarkdownDiff, MarkdownDiffBlock,
|
|
5
6
|
DiffFragment, DiffMode, DiffOptions, DiffPart, ItemDiff, ItemDiffEntry, ItemDiffOptions, ItemFieldDiff, ItemFieldSpec, ItemFieldType,
|
|
6
7
|
ItemState, ProseBlock, ProseDegraded, ProseDiff, ProseDiffOptions, ProseNormalizeOptions, SomaDiffAdapter, SomaDiffPort,
|
|
7
8
|
} from './port';
|
|
8
9
|
export { assertDiffAdapter } from './port';
|
|
10
|
+
export { hunksOf, markdownBlockKind, markdownBlockText, splitMarkdownBlocks } from './code';
|
|
9
11
|
|
|
10
12
|
import { SomaEngineError } from '../../../shared/engines/engine-error';
|
|
11
13
|
import { SOMA_PIPELINE_LIMITS } from '../../../shared/registry-generated/limits';
|
|
12
14
|
import type { EngineLoadTelemetry } from '../load';
|
|
13
15
|
import { createJsdiffAdapter } from './adapter-jsdiff';
|
|
14
16
|
import { runItems, type ItemsConfig } from './items';
|
|
17
|
+
import { runCode, runMarkdown, type CodeConfig } from './code';
|
|
15
18
|
import {
|
|
16
|
-
assertDiffAdapter, type DiffMode, type DiffOptions, type DiffPart, type ItemDiff, type ItemDiffOptions, type ProseDiff,
|
|
19
|
+
assertDiffAdapter, type CodeDiff, type CodeDiffOptions, type MarkdownDiff, type DiffMode, type DiffOptions, type DiffPart, type ItemDiff, type ItemDiffOptions, type ProseDiff,
|
|
17
20
|
type ProseDiffOptions, type SomaDiffAdapter, type SomaDiffPort,
|
|
18
21
|
} from './port';
|
|
19
22
|
import { clock, runProse, type ProseRun } from './prose';
|
|
@@ -48,6 +51,17 @@ function configOf(o: ProseDiffOptions | ItemDiffOptions | undefined): ItemsConfi
|
|
|
48
51
|
};
|
|
49
52
|
}
|
|
50
53
|
|
|
54
|
+
function codeConfigOf(o: CodeDiffOptions | undefined): CodeConfig {
|
|
55
|
+
return {
|
|
56
|
+
maxChars: LIMITS.maxChars,
|
|
57
|
+
maxAlignTokens: LIMITS.maxAlignTokens,
|
|
58
|
+
maxPairMatrix: LIMITS.maxPairMatrix,
|
|
59
|
+
pairAt: LIMITS.pairAt,
|
|
60
|
+
context: typeof o?.context === 'number' && Number.isInteger(o.context) && o.context >= 0 ? Math.min(o.context, LIMITS.codeContext) : LIMITS.codeContext,
|
|
61
|
+
intraMaxLine: LIMITS.codeIntraMaxLine,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
51
65
|
/** 整段替换(端口级退化:超时、适配器失败) */
|
|
52
66
|
function wholeReplace(a: string, b: string): DiffPart[] {
|
|
53
67
|
if (a === b) return a === '' ? [] : [{ op: 'equal', text: a }];
|
|
@@ -104,5 +118,17 @@ export function createDiffPort(adapter?: SomaDiffAdapter, options: DiffPortOptio
|
|
|
104
118
|
record('items', started);
|
|
105
119
|
return out;
|
|
106
120
|
},
|
|
121
|
+
async diffCode(a: string, b: string, codeOptions?: CodeDiffOptions): Promise<CodeDiff> {
|
|
122
|
+
const started = clock();
|
|
123
|
+
const out = await runCode(newRun(codeOptions?.timeoutMs), codeConfigOf(codeOptions), textOrEmpty(a), textOrEmpty(b));
|
|
124
|
+
record('code', started);
|
|
125
|
+
return out;
|
|
126
|
+
},
|
|
127
|
+
async diffMarkdown(a: string, b: string, mdOptions?: CodeDiffOptions): Promise<MarkdownDiff> {
|
|
128
|
+
const started = clock();
|
|
129
|
+
const out = await runMarkdown(newRun(mdOptions?.timeoutMs), { ...codeConfigOf(mdOptions), maxBlocks: LIMITS.maxItems }, textOrEmpty(a), textOrEmpty(b));
|
|
130
|
+
record('markdown', started);
|
|
131
|
+
return out;
|
|
132
|
+
},
|
|
107
133
|
};
|
|
108
134
|
}
|
|
@@ -149,6 +149,77 @@ export interface ItemDiff {
|
|
|
149
149
|
readonly degraded?: ProseDegraded | 'too-many-items';
|
|
150
150
|
}
|
|
151
151
|
|
|
152
|
+
// ── 代码差异(内容展示批次 ⑥) ──
|
|
153
|
+
|
|
154
|
+
export interface CodeDiffLine {
|
|
155
|
+
readonly op: 'eq' | 'ins' | 'del';
|
|
156
|
+
/** 旧版行号(1 起;增行无) */
|
|
157
|
+
readonly a?: number;
|
|
158
|
+
/** 新版行号(1 起;删行无) */
|
|
159
|
+
readonly b?: number;
|
|
160
|
+
readonly text: string;
|
|
161
|
+
/** 改行:配对另一侧的行号(删行给新版行号,增行给旧版行号) */
|
|
162
|
+
readonly pair?: number;
|
|
163
|
+
/** 改行的行内字符差异(删行只含 eq / del,增行只含 eq / ins);行过长或超时缺省 */
|
|
164
|
+
readonly fragments?: readonly DiffFragment[];
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export interface CodeDiffHunk {
|
|
168
|
+
readonly aStart: number;
|
|
169
|
+
readonly aLines: number;
|
|
170
|
+
readonly bStart: number;
|
|
171
|
+
readonly bLines: number;
|
|
172
|
+
/** 在 `lines` 中的起止下标(含) */
|
|
173
|
+
readonly from: number;
|
|
174
|
+
readonly to: number;
|
|
175
|
+
readonly lines: readonly CodeDiffLine[];
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export interface CodeDiff {
|
|
179
|
+
readonly kind: 'same' | 'changed';
|
|
180
|
+
/** 全部行(合并视图顺序:改行组内删行在增行上方) */
|
|
181
|
+
readonly lines: readonly CodeDiffLine[];
|
|
182
|
+
/** 按 design `contentEngines.diff.codeContext` 行上下文切出的块 */
|
|
183
|
+
readonly hunks: readonly CodeDiffHunk[];
|
|
184
|
+
readonly stats: { readonly added: number; readonly removed: number; readonly modified: number };
|
|
185
|
+
/** 去掉全部空白后两侧相同 */
|
|
186
|
+
readonly whitespaceOnly: boolean;
|
|
187
|
+
/** too-large / timeout:不逐行对齐,整体按「全删 + 全增」给出 */
|
|
188
|
+
readonly degraded?: 'too-large' | 'timeout';
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// ── Markdown 差异(内容展示批次 ⑥;源码块级对齐 + 块内词级) ──
|
|
192
|
+
|
|
193
|
+
export type MarkdownBlockKind = 'heading' | 'paragraph' | 'list' | 'quote' | 'code' | 'table' | 'hr';
|
|
194
|
+
|
|
195
|
+
export interface MarkdownDiffBlock {
|
|
196
|
+
readonly op: 'eq' | 'ins' | 'del' | 'mod';
|
|
197
|
+
readonly kind: MarkdownBlockKind;
|
|
198
|
+
/** 块的 Markdown 源码(组件经 Markdown 端口渲染) */
|
|
199
|
+
readonly before?: string;
|
|
200
|
+
readonly after?: string;
|
|
201
|
+
/** mod:块文字(去块首标记)的词级差异;代码块不给(组件改用 diffCode) */
|
|
202
|
+
readonly fragments?: readonly DiffFragment[];
|
|
203
|
+
/** 结构变化:标题层级、列表有序 / 无序、代码语言 */
|
|
204
|
+
readonly notes?: readonly { readonly what: 'heading-level' | 'list-type' | 'code-language'; readonly before: string; readonly after: string }[];
|
|
205
|
+
readonly similarity?: number;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export interface MarkdownDiff {
|
|
209
|
+
readonly kind: 'same' | 'changed';
|
|
210
|
+
readonly blocks: readonly MarkdownDiffBlock[];
|
|
211
|
+
readonly stats: { readonly added: number; readonly removed: number; readonly modified: number; readonly same: number };
|
|
212
|
+
/** too-large / timeout:blocks 为空,组件退回源码对比(diffCode) */
|
|
213
|
+
readonly degraded?: 'too-large' | 'timeout';
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
export interface CodeDiffOptions {
|
|
217
|
+
/** 时限(调用方只能更小) */
|
|
218
|
+
readonly timeoutMs?: number;
|
|
219
|
+
/** 上下文行数(调用方只能更小) */
|
|
220
|
+
readonly context?: number;
|
|
221
|
+
}
|
|
222
|
+
|
|
152
223
|
export interface SomaDiffPort {
|
|
153
224
|
/** 两段字符串按粒度求片段(超时退回整段替换,不抛错) */
|
|
154
225
|
diff(a: string, b: string, mode: DiffMode, options?: DiffOptions): Promise<readonly DiffPart[]>;
|
|
@@ -156,6 +227,10 @@ export interface SomaDiffPort {
|
|
|
156
227
|
diffProse(a: string, b: string, options?: ProseDiffOptions): Promise<ProseDiff>;
|
|
157
228
|
/** 条目列表差异:key 或内容配对、字段差异、LIS 最少移动 */
|
|
158
229
|
diffItems(before: readonly unknown[], after: readonly unknown[], options?: ItemDiffOptions): Promise<ItemDiff>;
|
|
230
|
+
/** 代码差异:按行对齐、改行行内字符差异、按上下文切块;超长 / 超时退化为全删 + 全增(不抛) */
|
|
231
|
+
diffCode(a: string, b: string, options?: CodeDiffOptions): Promise<CodeDiff>;
|
|
232
|
+
/** Markdown 差异:源码块级对齐 + 块内词级 + 结构变化说明;超长 / 超时 blocks 为空并标 degraded(不抛) */
|
|
233
|
+
diffMarkdown(a: string, b: string, options?: CodeDiffOptions): Promise<MarkdownDiff>;
|
|
159
234
|
}
|
|
160
235
|
|
|
161
236
|
export function assertDiffAdapter(adapter: unknown): void {
|
|
@@ -240,6 +240,9 @@ function collectIncludes(sources) {
|
|
|
240
240
|
|
|
241
241
|
module.exports = {
|
|
242
242
|
SOMA_SOURCE_PACKAGES_ENV,
|
|
243
|
+
isSomaSourcePackage,
|
|
244
|
+
readExplicitSourcePackages,
|
|
245
|
+
resolvePackageRoot,
|
|
243
246
|
getSomaHostSourceRoot,
|
|
244
247
|
getSomaHostApiRoot,
|
|
245
248
|
resolveSafeChildRoot,
|
|
@@ -632,7 +632,9 @@ export const SOMA_PIPELINE_LIMITS = {
|
|
|
632
632
|
"bodyAt": "无 key 条目按正文配对的相似度阈值(笔记 C.2 ItemOpts.match.bodyAt 0.5)",
|
|
633
633
|
"maxAlignTokens": "序列对齐的不同键数上限(每个键映射为一个 BMP 私用区字符,U+E000–U+F8FF 共 6400 个)",
|
|
634
634
|
"maxPairMatrix": "一段删增区间内句 / 段配对的候选矩阵上限(删数 × 增数),超出不配对、按删 + 增呈现;经验值,证据弱",
|
|
635
|
-
"maxPairCandidates": "无 key 条目按相似度配对的候选矩阵上限(剩余旧条目 × 剩余新条目);经验值,证据弱"
|
|
635
|
+
"maxPairCandidates": "无 key 条目按相似度配对的候选矩阵上限(剩余旧条目 × 剩余新条目);经验值,证据弱",
|
|
636
|
+
"codeContext": "代码差异每块的上下文行数(笔记 C.5「上下文 3 行」,同 git diff -U3 / VS Code)",
|
|
637
|
+
"codeIntraMaxLine": "代码改行做行内字符差异的单行长度上限(字符);更长的行只标整行增删(经验值:压缩后的长行逐字比较无意义且耗时)"
|
|
636
638
|
},
|
|
637
639
|
"maxChars": 200000,
|
|
638
640
|
"maxCharsPerCompare": 20000,
|
|
@@ -647,7 +649,9 @@ export const SOMA_PIPELINE_LIMITS = {
|
|
|
647
649
|
"bodyAt": 0.5,
|
|
648
650
|
"maxAlignTokens": 6400,
|
|
649
651
|
"maxPairMatrix": 2500,
|
|
650
|
-
"maxPairCandidates": 250000
|
|
652
|
+
"maxPairCandidates": 250000,
|
|
653
|
+
"codeContext": 3,
|
|
654
|
+
"codeIntraMaxLine": 500
|
|
651
655
|
},
|
|
652
656
|
"highlight": {
|
|
653
657
|
"basis": {
|
|
@@ -841,6 +841,7 @@
|
|
|
841
841
|
"core/client/engines/chart/kind-modules.ts",
|
|
842
842
|
"core/client/engines/chart/port.ts",
|
|
843
843
|
"core/client/engines/diff/adapter-jsdiff.ts",
|
|
844
|
+
"core/client/engines/diff/code.ts",
|
|
844
845
|
"core/client/engines/diff/index.ts",
|
|
845
846
|
"core/client/engines/diff/items.ts",
|
|
846
847
|
"core/client/engines/diff/port.ts",
|