codexmate 0.0.17 → 0.0.18

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/web-ui/logic.mjs CHANGED
@@ -1,4 +1,394 @@
1
1
  // 逻辑纯函数:供 Web UI 与单元测试共享
2
+ export const DEFAULT_API_BODY_LIMIT_BYTES = 4 * 1024 * 1024;
3
+ const LARGE_DIFF_LINE_LIMIT = 3000;
4
+ const LARGE_DIFF_SYNC_LOOKAHEAD = 64;
5
+
6
+ function measureUtf8ByteLength(input) {
7
+ const text = typeof input === 'string' ? input : String(input ?? '');
8
+ if (typeof TextEncoder === 'function') {
9
+ return new TextEncoder().encode(text).length;
10
+ }
11
+ if (typeof Buffer !== 'undefined' && typeof Buffer.byteLength === 'function') {
12
+ return Buffer.byteLength(text, 'utf8');
13
+ }
14
+ return unescape(encodeURIComponent(text)).length;
15
+ }
16
+
17
+ function buildApiRequestByteLength(action, params) {
18
+ return measureUtf8ByteLength(JSON.stringify({ action, params }));
19
+ }
20
+
21
+ function normalizeDiffText(input) {
22
+ const safe = typeof input === 'string' ? input : '';
23
+ const withoutBom = safe.charCodeAt(0) === 0xFEFF ? safe.slice(1) : safe;
24
+ return withoutBom.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
25
+ }
26
+
27
+ function splitDiffLines(input) {
28
+ let normalized = normalizeDiffText(input);
29
+ if (!normalized) return [];
30
+ if (normalized.endsWith('\n')) {
31
+ normalized = normalized.slice(0, -1);
32
+ }
33
+ if (!normalized) return [];
34
+ return normalized.split('\n');
35
+ }
36
+
37
+ function buildDiffLcsMatrix(beforeLines, afterLines) {
38
+ const rows = beforeLines.length + 1;
39
+ const cols = afterLines.length + 1;
40
+ const matrix = Array.from({ length: rows }, () => new Array(cols).fill(0));
41
+ for (let i = 1; i < rows; i += 1) {
42
+ const beforeLine = beforeLines[i - 1];
43
+ for (let j = 1; j < cols; j += 1) {
44
+ if (beforeLine === afterLines[j - 1]) {
45
+ matrix[i][j] = matrix[i - 1][j - 1] + 1;
46
+ } else {
47
+ const up = matrix[i - 1][j];
48
+ const left = matrix[i][j - 1];
49
+ matrix[i][j] = up >= left ? up : left;
50
+ }
51
+ }
52
+ }
53
+ return matrix;
54
+ }
55
+
56
+ function countDiffStats(lines) {
57
+ let added = 0;
58
+ let removed = 0;
59
+ let unchanged = 0;
60
+ for (const line of lines) {
61
+ if (line.type === 'add') {
62
+ added += 1;
63
+ } else if (line.type === 'del') {
64
+ removed += 1;
65
+ } else {
66
+ unchanged += 1;
67
+ }
68
+ }
69
+ return { added, removed, unchanged };
70
+ }
71
+
72
+ function buildCollapsedContextLine(hiddenCount) {
73
+ return {
74
+ type: 'context',
75
+ value: `... ${hiddenCount} unchanged lines ...`,
76
+ oldNumber: null,
77
+ newNumber: null
78
+ };
79
+ }
80
+
81
+ function compactContextRuns(lines, contextSize = 3) {
82
+ const compacted = [];
83
+ const keepCount = Number.isFinite(contextSize) ? Math.max(1, Math.floor(contextSize)) : 3;
84
+ let index = 0;
85
+ while (index < lines.length) {
86
+ if (!lines[index] || lines[index].type !== 'context') {
87
+ compacted.push(lines[index]);
88
+ index += 1;
89
+ continue;
90
+ }
91
+ const start = index;
92
+ while (index < lines.length && lines[index] && lines[index].type === 'context') {
93
+ index += 1;
94
+ }
95
+ const run = lines.slice(start, index);
96
+ if (run.length <= keepCount * 2 + 1) {
97
+ compacted.push(...run);
98
+ continue;
99
+ }
100
+ compacted.push(...run.slice(0, keepCount));
101
+ compacted.push(buildCollapsedContextLine(run.length - keepCount * 2));
102
+ compacted.push(...run.slice(-keepCount));
103
+ }
104
+ return compacted;
105
+ }
106
+
107
+ function buildExactDiffLines(beforeLines, afterLines) {
108
+ const matrix = buildDiffLcsMatrix(beforeLines, afterLines);
109
+ const lines = [];
110
+ let i = beforeLines.length;
111
+ let j = afterLines.length;
112
+ while (i > 0 || j > 0) {
113
+ if (i > 0 && j > 0 && beforeLines[i - 1] === afterLines[j - 1]) {
114
+ lines.push({
115
+ type: 'context',
116
+ value: beforeLines[i - 1],
117
+ oldNumber: i,
118
+ newNumber: j
119
+ });
120
+ i -= 1;
121
+ j -= 1;
122
+ continue;
123
+ }
124
+ const canAdd = j > 0;
125
+ const canDel = i > 0;
126
+ if (canAdd && (!canDel || matrix[i][j - 1] >= matrix[i - 1][j])) {
127
+ lines.push({
128
+ type: 'add',
129
+ value: afterLines[j - 1],
130
+ oldNumber: null,
131
+ newNumber: j
132
+ });
133
+ j -= 1;
134
+ continue;
135
+ }
136
+ if (canDel) {
137
+ lines.push({
138
+ type: 'del',
139
+ value: beforeLines[i - 1],
140
+ oldNumber: i,
141
+ newNumber: null
142
+ });
143
+ i -= 1;
144
+ }
145
+ }
146
+ lines.reverse();
147
+ return lines;
148
+ }
149
+
150
+ function findSyncPointInWindow(beforeLines, afterLines, beforeIndex, afterIndex, maxBeforeOffset, maxAfterOffset) {
151
+ if (maxBeforeOffset <= 0 && maxAfterOffset <= 0) {
152
+ return null;
153
+ }
154
+
155
+ for (let offset = 1; offset <= maxAfterOffset; offset += 1) {
156
+ if (beforeLines[beforeIndex] === afterLines[afterIndex + offset]) {
157
+ return { beforeIndex, afterIndex: afterIndex + offset };
158
+ }
159
+ }
160
+ for (let offset = 1; offset <= maxBeforeOffset; offset += 1) {
161
+ if (beforeLines[beforeIndex + offset] === afterLines[afterIndex]) {
162
+ return { beforeIndex: beforeIndex + offset, afterIndex };
163
+ }
164
+ }
165
+
166
+ const maxDistance = maxBeforeOffset + maxAfterOffset;
167
+ for (let distance = 2; distance <= maxDistance; distance += 1) {
168
+ const beforeStart = Math.max(1, distance - maxAfterOffset);
169
+ const beforeEnd = Math.min(maxBeforeOffset, distance - 1);
170
+ for (let beforeOffset = beforeStart; beforeOffset <= beforeEnd; beforeOffset += 1) {
171
+ const afterOffset = distance - beforeOffset;
172
+ if (beforeLines[beforeIndex + beforeOffset] === afterLines[afterIndex + afterOffset]) {
173
+ return {
174
+ beforeIndex: beforeIndex + beforeOffset,
175
+ afterIndex: afterIndex + afterOffset
176
+ };
177
+ }
178
+ }
179
+ }
180
+ return null;
181
+ }
182
+
183
+ function findNextSyncPoint(beforeLines, afterLines, beforeIndex, afterIndex, lookahead = LARGE_DIFF_SYNC_LOOKAHEAD) {
184
+ const remainingBefore = Math.max(0, beforeLines.length - beforeIndex - 1);
185
+ const remainingAfter = Math.max(0, afterLines.length - afterIndex - 1);
186
+ const maxWindow = Math.max(remainingBefore, remainingAfter);
187
+ if (maxWindow <= 0) {
188
+ return null;
189
+ }
190
+
191
+ const initialWindow = Number.isFinite(lookahead)
192
+ ? Math.max(1, Math.floor(lookahead))
193
+ : LARGE_DIFF_SYNC_LOOKAHEAD;
194
+ let window = Math.min(maxWindow, initialWindow);
195
+ while (window > 0) {
196
+ const syncPoint = findSyncPointInWindow(
197
+ beforeLines,
198
+ afterLines,
199
+ beforeIndex,
200
+ afterIndex,
201
+ Math.min(window, remainingBefore),
202
+ Math.min(window, remainingAfter)
203
+ );
204
+ if (syncPoint) {
205
+ return syncPoint;
206
+ }
207
+ if (window >= maxWindow) {
208
+ return null;
209
+ }
210
+ window = Math.min(maxWindow, window * 2);
211
+ }
212
+ return null;
213
+ }
214
+
215
+ function buildLargeDiffLines(beforeLines, afterLines) {
216
+ const rawLines = [];
217
+ let beforeIndex = 0;
218
+ let afterIndex = 0;
219
+
220
+ while (beforeIndex < beforeLines.length && afterIndex < afterLines.length) {
221
+ if (beforeLines[beforeIndex] === afterLines[afterIndex]) {
222
+ rawLines.push({
223
+ type: 'context',
224
+ value: beforeLines[beforeIndex],
225
+ oldNumber: beforeIndex + 1,
226
+ newNumber: afterIndex + 1
227
+ });
228
+ beforeIndex += 1;
229
+ afterIndex += 1;
230
+ continue;
231
+ }
232
+
233
+ const syncPoint = findNextSyncPoint(beforeLines, afterLines, beforeIndex, afterIndex);
234
+ if (!syncPoint) {
235
+ rawLines.push({
236
+ type: 'del',
237
+ value: beforeLines[beforeIndex],
238
+ oldNumber: beforeIndex + 1,
239
+ newNumber: null
240
+ });
241
+ rawLines.push({
242
+ type: 'add',
243
+ value: afterLines[afterIndex],
244
+ oldNumber: null,
245
+ newNumber: afterIndex + 1
246
+ });
247
+ beforeIndex += 1;
248
+ afterIndex += 1;
249
+ continue;
250
+ }
251
+
252
+ while (beforeIndex < syncPoint.beforeIndex) {
253
+ rawLines.push({
254
+ type: 'del',
255
+ value: beforeLines[beforeIndex],
256
+ oldNumber: beforeIndex + 1,
257
+ newNumber: null
258
+ });
259
+ beforeIndex += 1;
260
+ }
261
+ while (afterIndex < syncPoint.afterIndex) {
262
+ rawLines.push({
263
+ type: 'add',
264
+ value: afterLines[afterIndex],
265
+ oldNumber: null,
266
+ newNumber: afterIndex + 1
267
+ });
268
+ afterIndex += 1;
269
+ }
270
+ }
271
+
272
+ while (beforeIndex < beforeLines.length) {
273
+ rawLines.push({
274
+ type: 'del',
275
+ value: beforeLines[beforeIndex],
276
+ oldNumber: beforeIndex + 1,
277
+ newNumber: null
278
+ });
279
+ beforeIndex += 1;
280
+ }
281
+ while (afterIndex < afterLines.length) {
282
+ rawLines.push({
283
+ type: 'add',
284
+ value: afterLines[afterIndex],
285
+ oldNumber: null,
286
+ newNumber: afterIndex + 1
287
+ });
288
+ afterIndex += 1;
289
+ }
290
+
291
+ return {
292
+ lines: compactContextRuns(rawLines),
293
+ stats: countDiffStats(rawLines)
294
+ };
295
+ }
296
+
297
+ export function buildLineDiff(beforeText, afterText) {
298
+ const beforeLines = splitDiffLines(beforeText);
299
+ const afterLines = splitDiffLines(afterText);
300
+ const result = (beforeLines.length > LARGE_DIFF_LINE_LIMIT || afterLines.length > LARGE_DIFF_LINE_LIMIT)
301
+ ? buildLargeDiffLines(beforeLines, afterLines)
302
+ : {
303
+ lines: buildExactDiffLines(beforeLines, afterLines),
304
+ stats: null
305
+ };
306
+ const stats = result.stats || countDiffStats(result.lines);
307
+ return {
308
+ lines: result.lines,
309
+ stats,
310
+ oldLineCount: beforeLines.length,
311
+ newLineCount: afterLines.length,
312
+ truncated: false
313
+ };
314
+ }
315
+
316
+ export function buildAgentsDiffPreview(options = {}) {
317
+ const beforeText = normalizeDiffText(options.baseContent);
318
+ const afterText = normalizeDiffText(options.content);
319
+ const diff = buildLineDiff(beforeText, afterText);
320
+ return {
321
+ ...diff,
322
+ truncated: !!diff.truncated,
323
+ hasChanges: diff.truncated
324
+ ? beforeText !== afterText
325
+ : (diff.stats.added > 0 || diff.stats.removed > 0)
326
+ };
327
+ }
328
+
329
+ export function buildAgentsDiffPreviewRequest(options = {}) {
330
+ const contextRaw = typeof options.context === 'string' ? options.context.trim() : '';
331
+ const context = contextRaw || 'codex';
332
+ const params = {
333
+ content: typeof options.content === 'string' ? options.content : '',
334
+ lineEnding: options.lineEnding === '\r\n' ? '\r\n' : '\n',
335
+ context
336
+ };
337
+ if (context === 'openclaw-workspace') {
338
+ const fileName = typeof options.fileName === 'string' ? options.fileName.trim() : '';
339
+ if (fileName) {
340
+ params.fileName = fileName;
341
+ }
342
+ }
343
+
344
+ const maxRequestBytes = Number.isFinite(options.maxRequestBytes)
345
+ ? Math.max(1024, Math.floor(options.maxRequestBytes))
346
+ : DEFAULT_API_BODY_LIMIT_BYTES;
347
+ const hasBaseContent = typeof options.baseContent === 'string';
348
+ const paramsWithBaseContent = hasBaseContent
349
+ ? { ...params, baseContent: options.baseContent }
350
+ : params;
351
+ if (!hasBaseContent) {
352
+ return {
353
+ params,
354
+ omittedBaseContent: false,
355
+ exceedsBodyLimit: buildApiRequestByteLength('preview-agents-diff', params) > maxRequestBytes
356
+ };
357
+ }
358
+ if (buildApiRequestByteLength('preview-agents-diff', paramsWithBaseContent) <= maxRequestBytes) {
359
+ return {
360
+ params: paramsWithBaseContent,
361
+ omittedBaseContent: false,
362
+ exceedsBodyLimit: false
363
+ };
364
+ }
365
+ return {
366
+ params,
367
+ omittedBaseContent: true,
368
+ exceedsBodyLimit: buildApiRequestByteLength('preview-agents-diff', params) > maxRequestBytes
369
+ };
370
+ }
371
+
372
+ export function isAgentsDiffPreviewPayloadTooLarge(result = {}) {
373
+ const status = Number(result && result.status);
374
+ const errorCode = result && typeof result.errorCode === 'string' ? result.errorCode : '';
375
+ return status === 413 || errorCode === 'payload-too-large';
376
+ }
377
+
378
+ export function shouldApplyAgentsDiffPreviewResponse(options = {}) {
379
+ const requestToken = options && options.requestToken;
380
+ const activeRequestToken = options && options.activeRequestToken;
381
+ if (!requestToken || requestToken !== activeRequestToken) {
382
+ return false;
383
+ }
384
+ if (!options || !options.isVisible) {
385
+ return false;
386
+ }
387
+ const requestFingerprint = typeof options.requestFingerprint === 'string' ? options.requestFingerprint : '';
388
+ const currentFingerprint = typeof options.currentFingerprint === 'string' ? options.currentFingerprint : '';
389
+ return requestFingerprint === currentFingerprint;
390
+ }
391
+
2
392
  export function normalizeClaudeValue(value) {
3
393
  return typeof value === 'string' ? value.trim() : '';
4
394
  }
@@ -308,7 +308,13 @@
308
308
  this.showMessage('请先选择要删除的 skill', 'error');
309
309
  return;
310
310
  }
311
- const confirmed = window.confirm(`确认删除 ${selected.length} 个 skill 吗?此操作不可撤销。`);
311
+ const confirmed = await this.requestConfirmDialog({
312
+ title: '删除 Skills',
313
+ message: `确认删除 ${selected.length} 个 skill 吗?此操作不可撤销。`,
314
+ confirmText: '删除',
315
+ cancelText: '取消',
316
+ danger: true
317
+ });
312
318
  if (!confirmed) {
313
319
  return;
314
320
  }