@causyne/agent 1.10.21 → 1.12.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/docs/THIRD_PARTY_NOTICES.md +12 -0
- package/native-skills/media/professional-document-publishing/SKILL.md +44 -0
- package/native-skills/media/screenplay-and-shotlist-forge/SKILL.md +42 -0
- package/package.json +1 -1
- package/src/acp/client.js +235 -0
- package/src/acp/client.ts +282 -0
- package/src/acp/peerProfiles.js +122 -0
- package/src/acp/peerProfiles.ts +128 -0
- package/src/acp/protocol.js +55 -0
- package/src/acp/protocol.ts +456 -0
- package/src/acp/server.js +746 -0
- package/src/acp/server.ts +822 -0
- package/src/agentLoop.d.ts +64 -0
- package/src/agentLoop.js +156 -12
- package/src/approvalGrants.js +33 -0
- package/src/approvalGrants.ts +50 -0
- package/src/commands.js +203 -0
- package/src/compiledSkills.js +10 -0
- package/src/decisionPolicy.js +2 -0
- package/src/decisionPolicy.ts +1 -0
- package/src/documentEngine/antiSlopAuditor.js +156 -0
- package/src/documentEngine/antiSlopAuditor.ts +221 -0
- package/src/documentEngine/documentForge.js +443 -0
- package/src/documentEngine/documentForge.ts +519 -0
- package/src/documentEngine/screenplayFormatter.js +336 -0
- package/src/documentEngine/screenplayFormatter.ts +407 -0
- package/src/engines/cad.js +113 -0
- package/src/engines/cad.ts +128 -0
- package/src/engines/film.js +108 -0
- package/src/engines/film.ts +104 -0
- package/src/engines/installer.js +226 -0
- package/src/engines/installer.ts +248 -0
- package/src/engines/photo.js +126 -0
- package/src/engines/photo.ts +137 -0
- package/src/engines/registry.js +186 -0
- package/src/engines/registry.ts +185 -0
- package/src/engines/sound.js +117 -0
- package/src/engines/sound.ts +130 -0
- package/src/engines/types.js +1 -0
- package/src/engines/types.ts +73 -0
- package/src/entrypoint.js +13 -0
- package/src/execSafe.js +10 -13
- package/src/execSafe.ts +25 -0
- package/src/memoryManager.js +9 -3
- package/src/modelRouter.js +56 -123
- package/src/modelRouter.ts +134 -0
- package/src/observer/advisory.js +164 -0
- package/src/observer/advisory.ts +171 -0
- package/src/observer/coldObserver.js +462 -0
- package/src/observer/coldObserver.ts +540 -0
- package/src/observer/evidence.js +435 -0
- package/src/observer/evidence.ts +499 -0
- package/src/perception.js +303 -118
- package/src/perception.ts +387 -0
- package/src/procSignal.js +15 -16
- package/src/procSignal.ts +31 -0
- package/src/promptMentions.js +170 -0
- package/src/promptMentions.ts +212 -0
- package/src/sandbox.js +9 -1
- package/src/sessionJournal.js +12 -2
- package/src/sessionJournal.ts +12 -2
- package/src/textDiff.js +117 -109
- package/src/textDiff.ts +169 -0
- package/src/toolPacks.js +19 -8
- package/src/tools/documentForge.js +215 -0
- package/src/tools/documentForge.ts +265 -0
- package/src/tools/generateMediaViaApi.js +1 -1
- package/src/tools/nativeEngine.js +464 -0
- package/src/tools/nativeEngine.ts +433 -0
- package/src/tools/openSourceToolProvisioner.js +510 -0
- package/src/tools/openSourceToolProvisioner.ts +590 -0
- package/src/tools/peerAgent.js +102 -0
- package/src/tools/peerAgent.ts +116 -0
- package/src/tools/registry.js +6 -1
- package/src/tui/completion.js +98 -42
- package/src/tui/prompt.js +17 -17
- package/src/uncertainty.js +93 -205
- package/src/uncertainty.ts +255 -0
- package/src/utils/paths.js +15 -18
- package/src/utils/paths.ts +36 -0
- package/src/vfeTracker.js +122 -181
- package/src/vfeTracker.ts +242 -0
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
// Resolution of `@` mentions for files and directories in user prompts.
|
|
2
|
+
//
|
|
3
|
+
// Pure Node.js. Zero dependencies. No external shell commands.
|
|
4
|
+
//
|
|
5
|
+
// Extracts both quoted (`@"path with spaces/file.js"`, `@'path'`) and
|
|
6
|
+
// unquoted (`@path/to/file.js`) mentions, resolves them against the active
|
|
7
|
+
// working directory and known subprojects, and formats bounded context
|
|
8
|
+
// representations so the agent immediately possesses referenced files and
|
|
9
|
+
// folder trees without needing exploratory tool calls.
|
|
10
|
+
|
|
11
|
+
import fs from 'node:fs';
|
|
12
|
+
import path from 'node:path';
|
|
13
|
+
|
|
14
|
+
const MAX_MENTION_FILE_LINES = 500;
|
|
15
|
+
const MAX_MENTION_DIR_ITEMS = 40;
|
|
16
|
+
|
|
17
|
+
export interface Mention {
|
|
18
|
+
raw: string;
|
|
19
|
+
path: string;
|
|
20
|
+
isQuoted: boolean;
|
|
21
|
+
isMeta?: boolean;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface ResolveMentionOptions {
|
|
25
|
+
cwd?: string;
|
|
26
|
+
subprojects?: string[];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface ResolvedMentionContext {
|
|
30
|
+
mentions: Mention[];
|
|
31
|
+
referencedContext: string;
|
|
32
|
+
detectedSubproject: string | null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Extract all @ mentions from prompt text.
|
|
37
|
+
*
|
|
38
|
+
* @param text - User prompt text.
|
|
39
|
+
* @returns Array of parsed mentions.
|
|
40
|
+
*/
|
|
41
|
+
export function extractMentions(text: string): Mention[] {
|
|
42
|
+
if (!text || typeof text !== 'string') return [];
|
|
43
|
+
const mentions: Mention[] = [];
|
|
44
|
+
const seen = new Set<string>();
|
|
45
|
+
|
|
46
|
+
// 1. Quoted mentions: @"..." or @'...'
|
|
47
|
+
// This allows spaces, e.g. @"pixel2d games/main.js" or @'assets/sprite sheet.png'
|
|
48
|
+
const quotedRegex = /(?:^|\s)@(["'])([^"'\n\r]+)\1/g;
|
|
49
|
+
let match: RegExpExecArray | null;
|
|
50
|
+
while ((match = quotedRegex.exec(text)) !== null) {
|
|
51
|
+
const raw = match[0].trim();
|
|
52
|
+
const cleanPath = (match[2] ?? '').trim();
|
|
53
|
+
if (cleanPath && !seen.has(cleanPath)) {
|
|
54
|
+
seen.add(cleanPath);
|
|
55
|
+
mentions.push({ raw, path: cleanPath, isQuoted: true });
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// 2. Unquoted mentions: @path/to/file or @dir/
|
|
60
|
+
// Matches @ preceded by start-of-line or whitespace, not part of an email address
|
|
61
|
+
const unquotedRegex = /(?:^|\s)@([a-zA-Z0-9_.\-/\\]+)(?=$|\s|[.,;:?!)\]}])/g;
|
|
62
|
+
while ((match = unquotedRegex.exec(text)) !== null) {
|
|
63
|
+
const raw = match[0].trim();
|
|
64
|
+
const cleanPath = (match[1] ?? '').trim();
|
|
65
|
+
|
|
66
|
+
// Skip if already captured via quotes or if empty
|
|
67
|
+
if (!cleanPath || seen.has(cleanPath)) continue;
|
|
68
|
+
|
|
69
|
+
// Do not capture npm scope prefixes on their own or common non-file @ words
|
|
70
|
+
if (cleanPath === 'here' || cleanPath === 'all' || cleanPath === 'workspace') {
|
|
71
|
+
seen.add(cleanPath);
|
|
72
|
+
mentions.push({ raw, path: cleanPath, isQuoted: false, isMeta: true });
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
seen.add(cleanPath);
|
|
77
|
+
mentions.push({ raw, path: cleanPath, isQuoted: false });
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return mentions;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Resolve mentioned files and folders against the active working directory
|
|
85
|
+
* and detected subprojects. Returns bounded context blocks for model ingestion.
|
|
86
|
+
*
|
|
87
|
+
* @param prompt - Raw prompt string.
|
|
88
|
+
* @param options - Resolution options.
|
|
89
|
+
* @returns Bounded context and list of mentions.
|
|
90
|
+
*/
|
|
91
|
+
export async function resolveMentionedContext(
|
|
92
|
+
prompt: string,
|
|
93
|
+
options: ResolveMentionOptions = {}
|
|
94
|
+
): Promise<ResolvedMentionContext> {
|
|
95
|
+
const mentions = extractMentions(prompt);
|
|
96
|
+
if (mentions.length === 0) {
|
|
97
|
+
return { mentions: [], referencedContext: '', detectedSubproject: null };
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const cwd = options.cwd || process.cwd();
|
|
101
|
+
const subprojects = Array.isArray(options.subprojects) ? options.subprojects : [];
|
|
102
|
+
const sections: string[] = [];
|
|
103
|
+
let detectedSubproject: string | null = null;
|
|
104
|
+
|
|
105
|
+
for (const m of mentions) {
|
|
106
|
+
if (m.isMeta) {
|
|
107
|
+
if (m.path === 'workspace' || m.path === 'here') {
|
|
108
|
+
sections.push(`📁 REFERENCED SCOPE: Whole workspace (${cwd})`);
|
|
109
|
+
}
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
let targetPath = path.resolve(cwd, m.path);
|
|
114
|
+
let found = false;
|
|
115
|
+
|
|
116
|
+
try {
|
|
117
|
+
if (fs.existsSync(targetPath)) {
|
|
118
|
+
found = true;
|
|
119
|
+
}
|
|
120
|
+
} catch {
|
|
121
|
+
// ignore
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Check if path refers to a file/folder inside any detected subproject
|
|
125
|
+
if (!found && subprojects.length > 0) {
|
|
126
|
+
for (const sp of subprojects) {
|
|
127
|
+
const candidate = path.resolve(cwd, sp, m.path);
|
|
128
|
+
try {
|
|
129
|
+
if (fs.existsSync(candidate)) {
|
|
130
|
+
targetPath = candidate;
|
|
131
|
+
found = true;
|
|
132
|
+
detectedSubproject = sp;
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
} catch {
|
|
136
|
+
// ignore
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// Check if the mention path explicitly names a subproject (e.g. @"pixel2d games/...")
|
|
142
|
+
if (subprojects.length > 0) {
|
|
143
|
+
for (const sp of subprojects) {
|
|
144
|
+
if (m.path === sp || m.path.startsWith(sp + '/') || m.path.startsWith(sp + '\\')) {
|
|
145
|
+
detectedSubproject = sp;
|
|
146
|
+
break;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const relPath = path.relative(cwd, targetPath).split(path.sep).join('/');
|
|
152
|
+
|
|
153
|
+
if (!found) {
|
|
154
|
+
sections.push(`📂 REFERENCED PATH: ${m.path} (Note: path was not found on disk at ${relPath})`);
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
try {
|
|
159
|
+
const stats = fs.statSync(targetPath);
|
|
160
|
+
if (stats.isDirectory()) {
|
|
161
|
+
const entries = fs.readdirSync(targetPath, { withFileTypes: true });
|
|
162
|
+
const summary = entries
|
|
163
|
+
.filter(e => !e.name.startsWith('.') && e.name !== 'node_modules')
|
|
164
|
+
.slice(0, MAX_MENTION_DIR_ITEMS)
|
|
165
|
+
.map(e => e.isDirectory() ? `${e.name}/` : e.name);
|
|
166
|
+
|
|
167
|
+
const more = entries.length > MAX_MENTION_DIR_ITEMS
|
|
168
|
+
? `\n ... (${entries.length - MAX_MENTION_DIR_ITEMS} more items)`
|
|
169
|
+
: '';
|
|
170
|
+
|
|
171
|
+
sections.push(
|
|
172
|
+
`📁 REFERENCED DIRECTORY: ${relPath}/\n` +
|
|
173
|
+
`Directory listing (${summary.length} items):\n` +
|
|
174
|
+
summary.map(s => ` - ${s}`).join('\n') +
|
|
175
|
+
more
|
|
176
|
+
);
|
|
177
|
+
} else if (stats.isFile()) {
|
|
178
|
+
const content = fs.readFileSync(targetPath, 'utf8');
|
|
179
|
+
const lines = content.split('\n');
|
|
180
|
+
let body: string;
|
|
181
|
+
if (lines.length > MAX_MENTION_FILE_LINES) {
|
|
182
|
+
body = lines.slice(0, MAX_MENTION_FILE_LINES).join('\n') +
|
|
183
|
+
`\n\n... [Truncated: showing first ${MAX_MENTION_FILE_LINES} of ${lines.length} lines. Use fs.read for remaining lines.]`;
|
|
184
|
+
} else {
|
|
185
|
+
body = content;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
sections.push(
|
|
189
|
+
`📄 REFERENCED FILE: ${relPath} (${lines.length} lines):\n` +
|
|
190
|
+
'```\n' +
|
|
191
|
+
body + '\n' +
|
|
192
|
+
'```'
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
} catch (err: unknown) {
|
|
196
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
197
|
+
sections.push(`📂 REFERENCED PATH: ${relPath} (Error reading: ${msg})`);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const referencedContext = sections.length > 0
|
|
202
|
+
? `\n\n=== REFERENCED WORKSPACE CONTEXT (@ mentions) ===\n` +
|
|
203
|
+
sections.join('\n\n') +
|
|
204
|
+
`\n=== END REFERENCED WORKSPACE CONTEXT ===\n`
|
|
205
|
+
: '';
|
|
206
|
+
|
|
207
|
+
return {
|
|
208
|
+
mentions,
|
|
209
|
+
referencedContext,
|
|
210
|
+
detectedSubproject
|
|
211
|
+
};
|
|
212
|
+
}
|
package/src/sandbox.js
CHANGED
|
@@ -302,6 +302,14 @@ export class Sandbox {
|
|
|
302
302
|
return { allowed: true, risk: args.action === 'status' ? 'safe' : 'dangerous' };
|
|
303
303
|
}
|
|
304
304
|
|
|
305
|
+
if (toolName === 'native_engine') {
|
|
306
|
+
if (args.action === 'install') {
|
|
307
|
+
return { allowed: true, risk: 'extreme', reason: 'Downloads and installs a native engine binary' };
|
|
308
|
+
}
|
|
309
|
+
const readOnly = ['status', 'catalog', 'describe', 'inspect', 'probe'];
|
|
310
|
+
return { allowed: true, risk: readOnly.includes(args.action) ? 'safe' : 'dangerous' };
|
|
311
|
+
}
|
|
312
|
+
|
|
305
313
|
if (toolName === 'game_asset_fetch' && args.action === 'download_asset') {
|
|
306
314
|
if (!args.url || this.isInternalUrl(args.url)) {
|
|
307
315
|
return { allowed: false, reason: 'Cannot download from an internal or invalid URL', risk: 'forbidden' };
|
|
@@ -360,7 +368,7 @@ export class Sandbox {
|
|
|
360
368
|
if (toolName === 'desktop_act' || toolName === 'computer_use') {
|
|
361
369
|
return { allowed: true, risk: 'extreme' };
|
|
362
370
|
}
|
|
363
|
-
if (toolName === 'ui_forge') {
|
|
371
|
+
if (toolName === 'ui_forge' || toolName === 'document_forge') {
|
|
364
372
|
return { allowed: true, risk: 'safe' };
|
|
365
373
|
}
|
|
366
374
|
if (toolName === 'fs_write' || toolName === 'fs_move' || toolName === 'fs_edit' || toolName === 'MultiEdit') {
|
package/src/sessionJournal.js
CHANGED
|
@@ -263,7 +263,7 @@ export function pruneSessions(max = MAX_SESSIONS) {
|
|
|
263
263
|
}
|
|
264
264
|
return removed;
|
|
265
265
|
}
|
|
266
|
-
export function createSession(workspace, title = 'Untitled') {
|
|
266
|
+
export function createSession(workspace, title = 'Untitled', makeActive = true) {
|
|
267
267
|
const now = Date.now();
|
|
268
268
|
const workspaceText = String(workspace ?? '');
|
|
269
269
|
const session = {
|
|
@@ -278,10 +278,20 @@ export function createSession(workspace, title = 'Untitled') {
|
|
|
278
278
|
// Persistence is best-effort. An unavailable state directory must not stop a
|
|
279
279
|
// user from starting an otherwise usable foreground session.
|
|
280
280
|
writeNewTranscript(session);
|
|
281
|
-
|
|
281
|
+
if (makeActive)
|
|
282
|
+
setActiveSession(session.id);
|
|
282
283
|
pruneSessions();
|
|
283
284
|
return session;
|
|
284
285
|
}
|
|
286
|
+
/**
|
|
287
|
+
* Read one conversation without making it the active session.
|
|
288
|
+
*
|
|
289
|
+
* Server-owned conversations must never move the terminal's active pointer;
|
|
290
|
+
* they only need the transcript that belongs to their own id.
|
|
291
|
+
*/
|
|
292
|
+
export function getSession(id) {
|
|
293
|
+
return readSession(id);
|
|
294
|
+
}
|
|
285
295
|
export function getActiveSessionId() {
|
|
286
296
|
try {
|
|
287
297
|
if (!fs.existsSync(POINTER_FILE))
|
package/src/sessionJournal.ts
CHANGED
|
@@ -295,7 +295,7 @@ export function pruneSessions(max = MAX_SESSIONS): number {
|
|
|
295
295
|
return removed;
|
|
296
296
|
}
|
|
297
297
|
|
|
298
|
-
export function createSession(workspace: unknown, title = 'Untitled'): SessionRecord {
|
|
298
|
+
export function createSession(workspace: unknown, title = 'Untitled', makeActive = true): SessionRecord {
|
|
299
299
|
const now = Date.now();
|
|
300
300
|
const workspaceText = String(workspace ?? '');
|
|
301
301
|
const session: SessionRecord = {
|
|
@@ -310,11 +310,21 @@ export function createSession(workspace: unknown, title = 'Untitled'): SessionRe
|
|
|
310
310
|
// Persistence is best-effort. An unavailable state directory must not stop a
|
|
311
311
|
// user from starting an otherwise usable foreground session.
|
|
312
312
|
writeNewTranscript(session);
|
|
313
|
-
setActiveSession(session.id);
|
|
313
|
+
if (makeActive) setActiveSession(session.id);
|
|
314
314
|
pruneSessions();
|
|
315
315
|
return session;
|
|
316
316
|
}
|
|
317
317
|
|
|
318
|
+
/**
|
|
319
|
+
* Read one conversation without making it the active session.
|
|
320
|
+
*
|
|
321
|
+
* Server-owned conversations must never move the terminal's active pointer;
|
|
322
|
+
* they only need the transcript that belongs to their own id.
|
|
323
|
+
*/
|
|
324
|
+
export function getSession(id: unknown): SessionRecord | null {
|
|
325
|
+
return readSession(id);
|
|
326
|
+
}
|
|
327
|
+
|
|
318
328
|
export function getActiveSessionId(): string | null {
|
|
319
329
|
try {
|
|
320
330
|
if (!fs.existsSync(POINTER_FILE)) return null;
|
package/src/textDiff.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// A unified diff, in pure JavaScript.
|
|
1
|
+
// A unified diff, in pure JavaScript / TypeScript.
|
|
2
2
|
//
|
|
3
3
|
// The git backend gets patches from git itself. This exists so the NATIVE
|
|
4
4
|
// backend — the fallback for a machine with no git, or a workspace that is not
|
|
@@ -9,13 +9,10 @@
|
|
|
9
9
|
// No dependency: this is a standard LCS over lines, which is what `diff -u`
|
|
10
10
|
// computes. Bounded, because a checkpoint diff is a UI affordance and must
|
|
11
11
|
// never become the reason a request times out.
|
|
12
|
-
|
|
13
12
|
/** Above this many lines on either side, fall back to counts without a patch. */
|
|
14
13
|
const MAX_DIFF_LINES = 4000;
|
|
15
|
-
|
|
16
14
|
/** Context lines around each change, matching `diff -u` and git's default. */
|
|
17
15
|
const CONTEXT = 3;
|
|
18
|
-
|
|
19
16
|
/**
|
|
20
17
|
* Longest common subsequence over two line arrays.
|
|
21
18
|
*
|
|
@@ -24,127 +21,138 @@ const CONTEXT = 3;
|
|
|
24
21
|
* second, and anything larger is not something a human reads in a dialog.
|
|
25
22
|
*/
|
|
26
23
|
function lcsTable(a, b) {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
for (let
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
24
|
+
const table = [];
|
|
25
|
+
for (let i = 0; i <= a.length; i++)
|
|
26
|
+
table.push(new Uint32Array(b.length + 1));
|
|
27
|
+
for (let i = a.length - 1; i >= 0; i--) {
|
|
28
|
+
const tableI = table[i];
|
|
29
|
+
const tableNext = table[i + 1];
|
|
30
|
+
for (let j = b.length - 1; j >= 0; j--) {
|
|
31
|
+
tableI[j] = a[i] === b[j]
|
|
32
|
+
? tableNext[j + 1] + 1
|
|
33
|
+
: Math.max(tableNext[j], tableI[j + 1]);
|
|
34
|
+
}
|
|
34
35
|
}
|
|
35
|
-
|
|
36
|
-
return table;
|
|
36
|
+
return table;
|
|
37
37
|
}
|
|
38
|
-
|
|
39
38
|
/** Walk the table into a flat edit script: ' ' keep, '-' remove, '+' add. */
|
|
40
39
|
function editScript(a, b) {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
40
|
+
const table = lcsTable(a, b);
|
|
41
|
+
const ops = [];
|
|
42
|
+
let i = 0;
|
|
43
|
+
let j = 0;
|
|
44
|
+
while (i < a.length && j < b.length) {
|
|
45
|
+
if (a[i] === b[j]) {
|
|
46
|
+
ops.push([' ', a[i]]);
|
|
47
|
+
i++;
|
|
48
|
+
j++;
|
|
49
|
+
}
|
|
50
|
+
else if (table[i + 1][j] >= table[i][j + 1]) {
|
|
51
|
+
ops.push(['-', a[i]]);
|
|
52
|
+
i++;
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
ops.push(['+', b[j]]);
|
|
56
|
+
j++;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
while (i < a.length)
|
|
60
|
+
ops.push(['-', a[i++]]);
|
|
61
|
+
while (j < b.length)
|
|
62
|
+
ops.push(['+', b[j++]]);
|
|
63
|
+
return ops;
|
|
53
64
|
}
|
|
54
|
-
|
|
55
65
|
function splitLines(text) {
|
|
56
|
-
|
|
57
|
-
|
|
66
|
+
if (text === '')
|
|
67
|
+
return [];
|
|
68
|
+
return text.replace(/\n$/, '').split('\n');
|
|
58
69
|
}
|
|
59
|
-
|
|
60
70
|
/**
|
|
61
71
|
* A unified diff of `before` → `after`.
|
|
62
|
-
*
|
|
63
|
-
* @returns {{patch: string, additions: number, deletions: number, truncated: boolean}}
|
|
64
72
|
*/
|
|
65
73
|
export function unifiedDiff(before, after, pathLabel = 'file') {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
const ops = editScript(a, b);
|
|
81
|
-
let additions = 0;
|
|
82
|
-
let deletions = 0;
|
|
83
|
-
for (const [kind] of ops) {
|
|
84
|
-
if (kind === '+') additions++;
|
|
85
|
-
else if (kind === '-') deletions++;
|
|
86
|
-
}
|
|
87
|
-
if (additions === 0 && deletions === 0) {
|
|
88
|
-
return { patch: '', additions: 0, deletions: 0, truncated: false };
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
// Group changes into hunks with CONTEXT lines of surrounding text, so the
|
|
92
|
-
// output is readable rather than the whole file with markers.
|
|
93
|
-
const lines = [`--- a/${pathLabel}`, `+++ b/${pathLabel}`];
|
|
94
|
-
let index = 0;
|
|
95
|
-
let oldLine = 1;
|
|
96
|
-
let newLine = 1;
|
|
97
|
-
|
|
98
|
-
while (index < ops.length) {
|
|
99
|
-
if (ops[index][0] === ' ') {
|
|
100
|
-
if (ops[index][0] === ' ') { oldLine++; newLine++; }
|
|
101
|
-
index++;
|
|
102
|
-
continue;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
// Walk back for leading context, then forward to the end of the change.
|
|
106
|
-
const start = Math.max(0, index - CONTEXT);
|
|
107
|
-
let end = index;
|
|
108
|
-
let quiet = 0;
|
|
109
|
-
while (end < ops.length && quiet <= CONTEXT * 2) {
|
|
110
|
-
quiet = ops[end][0] === ' ' ? quiet + 1 : 0;
|
|
111
|
-
end++;
|
|
74
|
+
const a = splitLines(before);
|
|
75
|
+
const b = splitLines(after);
|
|
76
|
+
if (a.length > MAX_DIFF_LINES || b.length > MAX_DIFF_LINES) {
|
|
77
|
+
// Counting without the table keeps a huge file cheap: the exact per-line
|
|
78
|
+
// patch is not worth an O(n*m) allocation nobody will read.
|
|
79
|
+
return {
|
|
80
|
+
patch: '',
|
|
81
|
+
additions: b.length,
|
|
82
|
+
deletions: a.length,
|
|
83
|
+
truncated: true
|
|
84
|
+
};
|
|
112
85
|
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
let
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
86
|
+
const ops = editScript(a, b);
|
|
87
|
+
let additions = 0;
|
|
88
|
+
let deletions = 0;
|
|
89
|
+
for (const [kind] of ops) {
|
|
90
|
+
if (kind === '+')
|
|
91
|
+
additions++;
|
|
92
|
+
else if (kind === '-')
|
|
93
|
+
deletions++;
|
|
120
94
|
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
let newCount = 0;
|
|
124
|
-
const body = [];
|
|
125
|
-
for (let k = start; k < end; k++) {
|
|
126
|
-
const [kind, text] = ops[k];
|
|
127
|
-
if (kind !== '+') oldCount++;
|
|
128
|
-
if (kind !== '-') newCount++;
|
|
129
|
-
body.push(`${kind}${text}`);
|
|
95
|
+
if (additions === 0 && deletions === 0) {
|
|
96
|
+
return { patch: '', additions: 0, deletions: 0, truncated: false };
|
|
130
97
|
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
lines
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
98
|
+
// Group changes into hunks with CONTEXT lines of surrounding text, so the
|
|
99
|
+
// output is readable rather than the whole file with markers.
|
|
100
|
+
const lines = [`--- a/${pathLabel}`, `+++ b/${pathLabel}`];
|
|
101
|
+
let index = 0;
|
|
102
|
+
let oldLine = 1;
|
|
103
|
+
let newLine = 1;
|
|
104
|
+
while (index < ops.length) {
|
|
105
|
+
const opIndex = ops[index];
|
|
106
|
+
if (opIndex[0] === ' ') {
|
|
107
|
+
oldLine++;
|
|
108
|
+
newLine++;
|
|
109
|
+
index++;
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
// Walk back for leading context, then forward to the end of the change.
|
|
113
|
+
const start = Math.max(0, index - CONTEXT);
|
|
114
|
+
let end = index;
|
|
115
|
+
let quiet = 0;
|
|
116
|
+
while (end < ops.length && quiet <= CONTEXT * 2) {
|
|
117
|
+
quiet = ops[end][0] === ' ' ? quiet + 1 : 0;
|
|
118
|
+
end++;
|
|
119
|
+
}
|
|
120
|
+
end = Math.min(ops.length, end);
|
|
121
|
+
let oldStart = oldLine;
|
|
122
|
+
let newStart = newLine;
|
|
123
|
+
for (let k = start; k < index; k++) {
|
|
124
|
+
if (ops[k][0] !== '+')
|
|
125
|
+
oldStart--;
|
|
126
|
+
if (ops[k][0] !== '-')
|
|
127
|
+
newStart--;
|
|
128
|
+
}
|
|
129
|
+
let oldCount = 0;
|
|
130
|
+
let newCount = 0;
|
|
131
|
+
const body = [];
|
|
132
|
+
for (let k = start; k < end; k++) {
|
|
133
|
+
const [kind, text] = ops[k];
|
|
134
|
+
if (kind !== '+')
|
|
135
|
+
oldCount++;
|
|
136
|
+
if (kind !== '-')
|
|
137
|
+
newCount++;
|
|
138
|
+
body.push(`${kind}${text}`);
|
|
139
|
+
}
|
|
140
|
+
lines.push(`@@ -${Math.max(1, oldStart)},${oldCount} +${Math.max(1, newStart)},${newCount} @@`);
|
|
141
|
+
lines.push(...body);
|
|
142
|
+
for (let k = index; k < end; k++) {
|
|
143
|
+
if (ops[k][0] !== '+')
|
|
144
|
+
oldLine++;
|
|
145
|
+
if (ops[k][0] !== '-')
|
|
146
|
+
newLine++;
|
|
147
|
+
}
|
|
148
|
+
index = end;
|
|
138
149
|
}
|
|
139
|
-
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
return { patch: lines.join('\n'), additions, deletions, truncated: false };
|
|
150
|
+
return { patch: lines.join('\n'), additions, deletions, truncated: false };
|
|
143
151
|
}
|
|
144
|
-
|
|
145
152
|
/** Binary content has no useful line diff — detected the way git detects it. */
|
|
146
153
|
export function looksBinary(buffer) {
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
154
|
+
if (!buffer || buffer.length === 0)
|
|
155
|
+
return false;
|
|
156
|
+
const sample = buffer.subarray(0, Math.min(8000, buffer.length));
|
|
157
|
+
return sample.includes(0);
|
|
150
158
|
}
|