opencode-dux 1.0.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/LICENSE +21 -0
- package/README.md +452 -0
- package/dist/agents/descriptions.d.ts +6 -0
- package/dist/agents/designer.d.ts +2 -0
- package/dist/agents/explorer.d.ts +2 -0
- package/dist/agents/fixer.d.ts +2 -0
- package/dist/agents/index.d.ts +22 -0
- package/dist/agents/interpreter.d.ts +2 -0
- package/dist/agents/librarian.d.ts +2 -0
- package/dist/agents/oracle.d.ts +2 -0
- package/dist/agents/orchestrator.d.ts +27 -0
- package/dist/agents/overrides.d.ts +18 -0
- package/dist/agents/prompt-blocks.d.ts +97 -0
- package/dist/agents/steward.d.ts +3 -0
- package/dist/cli/config-io.d.ts +24 -0
- package/dist/cli/config-manager.d.ts +4 -0
- package/dist/cli/index.d.ts +2 -0
- package/dist/cli/index.js +1006 -0
- package/dist/cli/install.d.ts +2 -0
- package/dist/cli/mcps.d.ts +13 -0
- package/dist/cli/model-key-normalization.d.ts +1 -0
- package/dist/cli/paths.d.ts +35 -0
- package/dist/cli/providers.d.ts +137 -0
- package/dist/cli/skills.d.ts +22 -0
- package/dist/cli/system.d.ts +5 -0
- package/dist/cli/types.d.ts +38 -0
- package/dist/config/constants.d.ts +12 -0
- package/dist/config/index.d.ts +4 -0
- package/dist/config/loader.d.ts +40 -0
- package/dist/config/runtime-preset.d.ts +12 -0
- package/dist/config/schema.d.ts +281 -0
- package/dist/config/utils.d.ts +10 -0
- package/dist/discovery/local/types.d.ts +79 -0
- package/dist/discovery/local.d.ts +73 -0
- package/dist/discovery/mcp-servers.d.ts +88 -0
- package/dist/discovery/skills.d.ts +94 -0
- package/dist/hooks/apply-patch/codec.d.ts +7 -0
- package/dist/hooks/apply-patch/errors.d.ts +25 -0
- package/dist/hooks/apply-patch/execution-context.d.ts +27 -0
- package/dist/hooks/apply-patch/index.d.ts +15 -0
- package/dist/hooks/apply-patch/matching.d.ts +26 -0
- package/dist/hooks/apply-patch/operations.d.ts +3 -0
- package/dist/hooks/apply-patch/patch.d.ts +2 -0
- package/dist/hooks/apply-patch/prepared-changes.d.ts +17 -0
- package/dist/hooks/apply-patch/resolution.d.ts +19 -0
- package/dist/hooks/apply-patch/rewrite.d.ts +7 -0
- package/dist/hooks/apply-patch/test-helpers.d.ts +6 -0
- package/dist/hooks/apply-patch/types.d.ts +80 -0
- package/dist/hooks/auto-update-checker/cache.d.ts +11 -0
- package/dist/hooks/auto-update-checker/checker.d.ts +32 -0
- package/dist/hooks/auto-update-checker/constants.d.ts +11 -0
- package/dist/hooks/auto-update-checker/index.d.ts +18 -0
- package/dist/hooks/auto-update-checker/types.d.ts +22 -0
- package/dist/hooks/chat-headers.d.ts +16 -0
- package/dist/hooks/context-pressure-reminder/index.d.ts +33 -0
- package/dist/hooks/delegate-task-retry/guidance.d.ts +2 -0
- package/dist/hooks/delegate-task-retry/hook.d.ts +8 -0
- package/dist/hooks/delegate-task-retry/index.d.ts +4 -0
- package/dist/hooks/delegate-task-retry/patterns.d.ts +11 -0
- package/dist/hooks/filter-available-skills/index.d.ts +32 -0
- package/dist/hooks/foreground-fallback/index.d.ts +72 -0
- package/dist/hooks/image-hook.d.ts +5 -0
- package/dist/hooks/index.d.ts +14 -0
- package/dist/hooks/json-error-recovery/hook.d.ts +18 -0
- package/dist/hooks/json-error-recovery/index.d.ts +1 -0
- package/dist/hooks/phase-reminder/index.d.ts +26 -0
- package/dist/hooks/post-file-tool-nudge/index.d.ts +19 -0
- package/dist/hooks/task-session-manager/index.d.ts +52 -0
- package/dist/hooks/todo-continuation/index.d.ts +53 -0
- package/dist/hooks/todo-continuation/todo-hygiene.d.ts +35 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +31782 -0
- package/dist/mcp/context7.d.ts +6 -0
- package/dist/mcp/grep-app.d.ts +6 -0
- package/dist/mcp/index.d.ts +13 -0
- package/dist/mcp/types.d.ts +12 -0
- package/dist/mcp/websearch.d.ts +9 -0
- package/dist/skills/registry.d.ts +29 -0
- package/dist/subscriptions/accounts-store.d.ts +57 -0
- package/dist/subscriptions/index.d.ts +13 -0
- package/dist/subscriptions/neuralwatt-scraper.d.ts +14 -0
- package/dist/subscriptions/opencode-go-scraper.d.ts +27 -0
- package/dist/subscriptions/types.d.ts +115 -0
- package/dist/subscriptions/usage-service.d.ts +74 -0
- package/dist/tools/ast-grep/cli.d.ts +15 -0
- package/dist/tools/ast-grep/constants.d.ts +25 -0
- package/dist/tools/ast-grep/downloader.d.ts +5 -0
- package/dist/tools/ast-grep/index.d.ts +10 -0
- package/dist/tools/ast-grep/tools.d.ts +3 -0
- package/dist/tools/ast-grep/types.d.ts +30 -0
- package/dist/tools/ast-grep/utils.d.ts +4 -0
- package/dist/tools/delegate.d.ts +14 -0
- package/dist/tools/index.d.ts +5 -0
- package/dist/tools/preset-manager.d.ts +27 -0
- package/dist/tools/smartfetch/binary.d.ts +3 -0
- package/dist/tools/smartfetch/cache.d.ts +6 -0
- package/dist/tools/smartfetch/constants.d.ts +12 -0
- package/dist/tools/smartfetch/index.d.ts +3 -0
- package/dist/tools/smartfetch/network.d.ts +38 -0
- package/dist/tools/smartfetch/secondary-model.d.ts +28 -0
- package/dist/tools/smartfetch/tool.d.ts +3 -0
- package/dist/tools/smartfetch/types.d.ts +122 -0
- package/dist/tools/smartfetch/utils.d.ts +18 -0
- package/dist/tui-state.d.ts +168 -0
- package/dist/tui.d.ts +37 -0
- package/dist/tui.js +1896 -0
- package/dist/utils/agent-variant.d.ts +63 -0
- package/dist/utils/compat.d.ts +30 -0
- package/dist/utils/env.d.ts +1 -0
- package/dist/utils/index.d.ts +9 -0
- package/dist/utils/internal-initiator.d.ts +6 -0
- package/dist/utils/logger.d.ts +8 -0
- package/dist/utils/polling.d.ts +21 -0
- package/dist/utils/session-manager.d.ts +55 -0
- package/dist/utils/session.d.ts +90 -0
- package/dist/utils/subagent-depth.d.ts +35 -0
- package/dist/utils/system-collapse.d.ts +6 -0
- package/dist/utils/task.d.ts +4 -0
- package/dist/utils/zip-extractor.d.ts +1 -0
- package/index.ts +1 -0
- package/opencode-dux.schema.json +634 -0
- package/package.json +103 -0
- package/src/agents/descriptions.ts +55 -0
- package/src/agents/designer.test.ts +86 -0
- package/src/agents/designer.ts +154 -0
- package/src/agents/display-name.test.ts +186 -0
- package/src/agents/explorer.test.ts +79 -0
- package/src/agents/explorer.ts +144 -0
- package/src/agents/fixer.test.ts +79 -0
- package/src/agents/fixer.ts +145 -0
- package/src/agents/index.test.ts +472 -0
- package/src/agents/index.ts +248 -0
- package/src/agents/interpreter.ts +136 -0
- package/src/agents/librarian.test.ts +80 -0
- package/src/agents/librarian.ts +145 -0
- package/src/agents/oracle.test.ts +89 -0
- package/src/agents/oracle.ts +184 -0
- package/src/agents/orchestrator.test.ts +116 -0
- package/src/agents/orchestrator.ts +574 -0
- package/src/agents/overrides.ts +95 -0
- package/src/agents/prompt-blocks.test.ts +114 -0
- package/src/agents/prompt-blocks.ts +640 -0
- package/src/agents/steward.ts +146 -0
- package/src/cli/config-io.test.ts +536 -0
- package/src/cli/config-io.ts +473 -0
- package/src/cli/config-manager.test.ts +141 -0
- package/src/cli/config-manager.ts +4 -0
- package/src/cli/index.ts +88 -0
- package/src/cli/install.ts +282 -0
- package/src/cli/mcps.test.ts +62 -0
- package/src/cli/mcps.ts +39 -0
- package/src/cli/model-key-normalization.test.ts +21 -0
- package/src/cli/model-key-normalization.ts +60 -0
- package/src/cli/paths.test.ts +167 -0
- package/src/cli/paths.ts +144 -0
- package/src/cli/providers.test.ts +118 -0
- package/src/cli/providers.ts +141 -0
- package/src/cli/skills.test.ts +111 -0
- package/src/cli/skills.ts +103 -0
- package/src/cli/system.test.ts +91 -0
- package/src/cli/system.ts +180 -0
- package/src/cli/types.ts +43 -0
- package/src/config/constants.ts +58 -0
- package/src/config/index.ts +4 -0
- package/src/config/loader.test.ts +1194 -0
- package/src/config/loader.ts +269 -0
- package/src/config/model-resolution.test.ts +176 -0
- package/src/config/runtime-preset.test.ts +61 -0
- package/src/config/runtime-preset.ts +37 -0
- package/src/config/schema.ts +248 -0
- package/src/config/utils.test.ts +41 -0
- package/src/config/utils.ts +23 -0
- package/src/discovery/local/types.ts +85 -0
- package/src/discovery/local.ts +322 -0
- package/src/discovery/mcp-servers.ts +804 -0
- package/src/discovery/skills.ts +959 -0
- package/src/hooks/apply-patch/codec.test.ts +184 -0
- package/src/hooks/apply-patch/codec.ts +352 -0
- package/src/hooks/apply-patch/errors.ts +117 -0
- package/src/hooks/apply-patch/execution-context.ts +432 -0
- package/src/hooks/apply-patch/hook.test.ts +768 -0
- package/src/hooks/apply-patch/index.ts +126 -0
- package/src/hooks/apply-patch/matching.test.ts +215 -0
- package/src/hooks/apply-patch/matching.ts +586 -0
- package/src/hooks/apply-patch/operations.test.ts +1535 -0
- package/src/hooks/apply-patch/operations.ts +3 -0
- package/src/hooks/apply-patch/patch.ts +9 -0
- package/src/hooks/apply-patch/prepared-changes.ts +400 -0
- package/src/hooks/apply-patch/resolution.test.ts +420 -0
- package/src/hooks/apply-patch/resolution.ts +437 -0
- package/src/hooks/apply-patch/rewrite.ts +496 -0
- package/src/hooks/apply-patch/test-helpers.ts +52 -0
- package/src/hooks/apply-patch/types.ts +111 -0
- package/src/hooks/auto-update-checker/cache.test.ts +179 -0
- package/src/hooks/auto-update-checker/cache.ts +188 -0
- package/src/hooks/auto-update-checker/checker.test.ts +159 -0
- package/src/hooks/auto-update-checker/checker.ts +308 -0
- package/src/hooks/auto-update-checker/constants.ts +33 -0
- package/src/hooks/auto-update-checker/index.test.ts +282 -0
- package/src/hooks/auto-update-checker/index.ts +225 -0
- package/src/hooks/auto-update-checker/types.ts +26 -0
- package/src/hooks/chat-headers.test.ts +236 -0
- package/src/hooks/chat-headers.ts +97 -0
- package/src/hooks/context-pressure-reminder/index.test.ts +179 -0
- package/src/hooks/context-pressure-reminder/index.ts +137 -0
- package/src/hooks/delegate-task-retry/guidance.ts +41 -0
- package/src/hooks/delegate-task-retry/hook.ts +23 -0
- package/src/hooks/delegate-task-retry/index.test.ts +38 -0
- package/src/hooks/delegate-task-retry/index.ts +7 -0
- package/src/hooks/delegate-task-retry/patterns.ts +79 -0
- package/src/hooks/filter-available-skills/index.test.ts +297 -0
- package/src/hooks/filter-available-skills/index.ts +160 -0
- package/src/hooks/foreground-fallback/index.test.ts +624 -0
- package/src/hooks/foreground-fallback/index.ts +374 -0
- package/src/hooks/image-hook.ts +6 -0
- package/src/hooks/index.ts +17 -0
- package/src/hooks/json-error-recovery/hook.ts +73 -0
- package/src/hooks/json-error-recovery/index.test.ts +111 -0
- package/src/hooks/json-error-recovery/index.ts +6 -0
- package/src/hooks/phase-reminder/index.test.ts +74 -0
- package/src/hooks/phase-reminder/index.ts +85 -0
- package/src/hooks/post-file-tool-nudge/index.test.ts +94 -0
- package/src/hooks/post-file-tool-nudge/index.ts +63 -0
- package/src/hooks/task-session-manager/index.test.ts +833 -0
- package/src/hooks/task-session-manager/index.ts +434 -0
- package/src/hooks/todo-continuation/index.test.ts +3026 -0
- package/src/hooks/todo-continuation/index.ts +878 -0
- package/src/hooks/todo-continuation/todo-hygiene.test.ts +204 -0
- package/src/hooks/todo-continuation/todo-hygiene.ts +207 -0
- package/src/index.ts +1672 -0
- package/src/mcp/context7.ts +14 -0
- package/src/mcp/grep-app.ts +11 -0
- package/src/mcp/index.test.ts +96 -0
- package/src/mcp/index.ts +66 -0
- package/src/mcp/types.ts +16 -0
- package/src/mcp/websearch.ts +47 -0
- package/src/skills/codemap/README.md +60 -0
- package/src/skills/codemap/SKILL.md +174 -0
- package/src/skills/codemap/scripts/codemap.mjs +483 -0
- package/src/skills/codemap/scripts/codemap.test.ts +129 -0
- package/src/skills/registry.ts +218 -0
- package/src/skills/simplify/README.md +19 -0
- package/src/skills/simplify/SKILL.md +138 -0
- package/src/subscriptions/accounts-store.test.ts +236 -0
- package/src/subscriptions/accounts-store.ts +184 -0
- package/src/subscriptions/index.ts +30 -0
- package/src/subscriptions/neuralwatt-scraper.ts +108 -0
- package/src/subscriptions/opencode-go-scraper.ts +301 -0
- package/src/subscriptions/types.ts +145 -0
- package/src/subscriptions/usage-service.test.ts +202 -0
- package/src/subscriptions/usage-service.ts +651 -0
- package/src/tools/ast-grep/cli.ts +257 -0
- package/src/tools/ast-grep/constants.ts +214 -0
- package/src/tools/ast-grep/downloader.ts +131 -0
- package/src/tools/ast-grep/index.ts +24 -0
- package/src/tools/ast-grep/tools.ts +117 -0
- package/src/tools/ast-grep/types.ts +51 -0
- package/src/tools/ast-grep/utils.ts +126 -0
- package/src/tools/delegate-handoff.test.ts +18 -0
- package/src/tools/delegate.ts +508 -0
- package/src/tools/index.ts +8 -0
- package/src/tools/preset-manager.test.ts +795 -0
- package/src/tools/preset-manager.ts +332 -0
- package/src/tools/smartfetch/binary.ts +58 -0
- package/src/tools/smartfetch/cache.test.ts +34 -0
- package/src/tools/smartfetch/cache.ts +112 -0
- package/src/tools/smartfetch/constants.ts +29 -0
- package/src/tools/smartfetch/index.ts +8 -0
- package/src/tools/smartfetch/network.test.ts +178 -0
- package/src/tools/smartfetch/network.ts +614 -0
- package/src/tools/smartfetch/secondary-model.test.ts +85 -0
- package/src/tools/smartfetch/secondary-model.ts +276 -0
- package/src/tools/smartfetch/tool.test.ts +60 -0
- package/src/tools/smartfetch/tool.ts +832 -0
- package/src/tools/smartfetch/types.ts +135 -0
- package/src/tools/smartfetch/utils.test.ts +24 -0
- package/src/tools/smartfetch/utils.ts +456 -0
- package/src/tui-state.test.ts +867 -0
- package/src/tui-state.ts +1255 -0
- package/src/tui.test.ts +336 -0
- package/src/tui.ts +1539 -0
- package/src/utils/agent-variant.test.ts +244 -0
- package/src/utils/agent-variant.ts +187 -0
- package/src/utils/compat.ts +91 -0
- package/src/utils/env.ts +12 -0
- package/src/utils/index.ts +9 -0
- package/src/utils/internal-initiator.ts +28 -0
- package/src/utils/logger.test.ts +220 -0
- package/src/utils/logger.ts +136 -0
- package/src/utils/polling.test.ts +191 -0
- package/src/utils/polling.ts +67 -0
- package/src/utils/session-manager.test.ts +173 -0
- package/src/utils/session-manager.ts +356 -0
- package/src/utils/session.test.ts +110 -0
- package/src/utils/session.ts +389 -0
- package/src/utils/subagent-depth.test.ts +170 -0
- package/src/utils/subagent-depth.ts +75 -0
- package/src/utils/system-collapse.test.ts +86 -0
- package/src/utils/system-collapse.ts +24 -0
- package/src/utils/task.test.ts +24 -0
- package/src/utils/task.ts +20 -0
- package/src/utils/zip-extractor.ts +102 -0
|
@@ -0,0 +1,437 @@
|
|
|
1
|
+
import * as fs from 'node:fs/promises';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
matchPreparedAutoRescueComparator,
|
|
5
|
+
prefix,
|
|
6
|
+
prepareAutoRescueTarget,
|
|
7
|
+
rescueByLcs,
|
|
8
|
+
rescueByPrefixSuffix,
|
|
9
|
+
seek,
|
|
10
|
+
seekMatch,
|
|
11
|
+
suffix,
|
|
12
|
+
} from './matching';
|
|
13
|
+
import type {
|
|
14
|
+
ApplyPatchRescueStrategy,
|
|
15
|
+
ApplyPatchRuntimeOptions,
|
|
16
|
+
MatchComparatorName,
|
|
17
|
+
MatchHit,
|
|
18
|
+
PatchChunk,
|
|
19
|
+
ResolvedChunk,
|
|
20
|
+
} from './types';
|
|
21
|
+
|
|
22
|
+
type FileLines = {
|
|
23
|
+
lines: string[];
|
|
24
|
+
eol: '\n' | '\r\n';
|
|
25
|
+
hasFinalNewline: boolean;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
function splitFileLines(text: string): FileLines {
|
|
29
|
+
const eol = text.match(/\r\n|\n|\r/)?.[0] === '\r\n' ? '\r\n' : '\n';
|
|
30
|
+
const normalized = text.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
|
|
31
|
+
const hasFinalNewline = normalized.endsWith('\n');
|
|
32
|
+
const lines = normalized.split('\n');
|
|
33
|
+
if (hasFinalNewline) {
|
|
34
|
+
lines.pop();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return { lines, eol, hasFinalNewline };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async function readFileLinesWithEol(file: string): Promise<FileLines> {
|
|
41
|
+
let text: string;
|
|
42
|
+
|
|
43
|
+
try {
|
|
44
|
+
text = await fs.readFile(file, 'utf-8');
|
|
45
|
+
} catch (error) {
|
|
46
|
+
throw new Error(`Failed to read file ${file}: ${error}`);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return splitFileLines(text);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export async function readFileLines(file: string): Promise<string[]> {
|
|
53
|
+
return (await readFileLinesWithEol(file)).lines;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function resolveChunkStart(
|
|
57
|
+
lines: string[],
|
|
58
|
+
chunk: PatchChunk,
|
|
59
|
+
start: number,
|
|
60
|
+
): number {
|
|
61
|
+
if (!chunk.change_context) {
|
|
62
|
+
return start;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const at = seek(lines, [chunk.change_context], start);
|
|
66
|
+
return at === -1 ? start : at + 1;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function resolveUniqueAnchor(
|
|
70
|
+
lines: string[],
|
|
71
|
+
changeContext: string,
|
|
72
|
+
start: number,
|
|
73
|
+
):
|
|
74
|
+
| { kind: 'missing' }
|
|
75
|
+
| { kind: 'ambiguous' }
|
|
76
|
+
| {
|
|
77
|
+
kind: 'match';
|
|
78
|
+
index: number;
|
|
79
|
+
exact: boolean;
|
|
80
|
+
comparator: MatchComparatorName;
|
|
81
|
+
canonicalLine: string;
|
|
82
|
+
} {
|
|
83
|
+
let matchedIndex: number | undefined;
|
|
84
|
+
let matchedComparator: MatchComparatorName | undefined;
|
|
85
|
+
const anchorTarget = prepareAutoRescueTarget(changeContext);
|
|
86
|
+
|
|
87
|
+
for (let index = start; index < lines.length; index += 1) {
|
|
88
|
+
const comparator = matchPreparedAutoRescueComparator(
|
|
89
|
+
lines[index],
|
|
90
|
+
anchorTarget,
|
|
91
|
+
);
|
|
92
|
+
if (!comparator) {
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (matchedIndex !== undefined) {
|
|
97
|
+
return { kind: 'ambiguous' };
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
matchedIndex = index;
|
|
101
|
+
matchedComparator = comparator;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (matchedIndex === undefined) {
|
|
105
|
+
return { kind: 'missing' };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const canonicalLine = lines[matchedIndex];
|
|
109
|
+
|
|
110
|
+
return {
|
|
111
|
+
kind: 'match',
|
|
112
|
+
index: matchedIndex,
|
|
113
|
+
exact: canonicalLine === changeContext,
|
|
114
|
+
comparator: matchedComparator ?? 'exact',
|
|
115
|
+
canonicalLine,
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export function locateChunk(
|
|
120
|
+
lines: string[],
|
|
121
|
+
file: string,
|
|
122
|
+
chunk: PatchChunk,
|
|
123
|
+
start: number,
|
|
124
|
+
cfg: ApplyPatchRuntimeOptions,
|
|
125
|
+
): ResolvedChunk {
|
|
126
|
+
const old_lines = chunk.old_lines;
|
|
127
|
+
const new_lines = chunk.new_lines;
|
|
128
|
+
const match = seekMatch(
|
|
129
|
+
lines,
|
|
130
|
+
old_lines,
|
|
131
|
+
start,
|
|
132
|
+
chunk.is_end_of_file ?? false,
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
if (match) {
|
|
136
|
+
const canonical_old_lines = lines.slice(
|
|
137
|
+
match.index,
|
|
138
|
+
match.index + old_lines.length,
|
|
139
|
+
);
|
|
140
|
+
const rewritten = !match.exact;
|
|
141
|
+
|
|
142
|
+
return {
|
|
143
|
+
hit: { start: match.index, del: old_lines.length, add: [...new_lines] },
|
|
144
|
+
old_lines,
|
|
145
|
+
canonical_old_lines,
|
|
146
|
+
canonical_new_lines: [...chunk.new_lines],
|
|
147
|
+
resolved_is_end_of_file:
|
|
148
|
+
match.index + canonical_old_lines.length === lines.length,
|
|
149
|
+
rewritten,
|
|
150
|
+
strategy: undefined,
|
|
151
|
+
matchComparator: match.comparator,
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (cfg.prefixSuffix) {
|
|
156
|
+
const rescued = rescueByPrefixSuffix(lines, old_lines, new_lines, start);
|
|
157
|
+
|
|
158
|
+
if (rescued.kind === 'ambiguous') {
|
|
159
|
+
throw new Error(
|
|
160
|
+
`Prefix/suffix rescue was ambiguous in ${file}:\n${chunk.old_lines.join(
|
|
161
|
+
'\n',
|
|
162
|
+
)}`,
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (rescued.kind === 'match') {
|
|
167
|
+
const prefixLength = prefix(old_lines, new_lines);
|
|
168
|
+
const suffixLength = suffix(old_lines, new_lines, prefixLength);
|
|
169
|
+
const canonicalStart = rescued.hit.start - prefixLength;
|
|
170
|
+
const canonicalEnd = rescued.hit.start + rescued.hit.del + suffixLength;
|
|
171
|
+
|
|
172
|
+
return {
|
|
173
|
+
hit: rescued.hit,
|
|
174
|
+
old_lines,
|
|
175
|
+
canonical_old_lines: lines.slice(canonicalStart, canonicalEnd),
|
|
176
|
+
canonical_new_lines: [...chunk.new_lines],
|
|
177
|
+
resolved_is_end_of_file: canonicalEnd === lines.length,
|
|
178
|
+
rewritten: true,
|
|
179
|
+
strategy: 'prefix/suffix',
|
|
180
|
+
matchComparator: 'exact',
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
if (cfg.lcsRescue) {
|
|
186
|
+
const rescued = rescueByLcs(lines, old_lines, new_lines, start);
|
|
187
|
+
|
|
188
|
+
if (rescued.kind === 'ambiguous') {
|
|
189
|
+
throw new Error(
|
|
190
|
+
`LCS rescue was ambiguous in ${file}:\n${chunk.old_lines.join('\n')}`,
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if (rescued.kind === 'match') {
|
|
195
|
+
return {
|
|
196
|
+
hit: rescued.hit,
|
|
197
|
+
old_lines,
|
|
198
|
+
canonical_old_lines: lines.slice(
|
|
199
|
+
rescued.hit.start,
|
|
200
|
+
rescued.hit.start + rescued.hit.del,
|
|
201
|
+
),
|
|
202
|
+
canonical_new_lines: [...chunk.new_lines],
|
|
203
|
+
resolved_is_end_of_file:
|
|
204
|
+
rescued.hit.start + rescued.hit.del === lines.length,
|
|
205
|
+
rewritten: true,
|
|
206
|
+
strategy: 'lcs',
|
|
207
|
+
matchComparator: 'exact',
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
throw new Error(
|
|
213
|
+
`Failed to find expected lines in ${file}:\n${chunk.old_lines.join('\n')}`,
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export function applyHits(
|
|
218
|
+
lines: string[],
|
|
219
|
+
hits: MatchHit[],
|
|
220
|
+
eol: '\n' | '\r\n' = '\n',
|
|
221
|
+
hasFinalNewline = true,
|
|
222
|
+
): string {
|
|
223
|
+
const out = [...lines];
|
|
224
|
+
|
|
225
|
+
for (let index = hits.length - 1; index >= 0; index -= 1) {
|
|
226
|
+
out.splice(hits[index].start, hits[index].del, ...hits[index].add);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
if (out.length === 0) {
|
|
230
|
+
return '';
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
const rendered = out.join(eol);
|
|
234
|
+
return hasFinalNewline ? `${rendered}${eol}` : rendered;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function resolveUpdateChunksFromFileLines(
|
|
238
|
+
file: string,
|
|
239
|
+
state: FileLines,
|
|
240
|
+
chunks: PatchChunk[],
|
|
241
|
+
cfg: ApplyPatchRuntimeOptions,
|
|
242
|
+
): {
|
|
243
|
+
lines: string[];
|
|
244
|
+
resolved: ResolvedChunk[];
|
|
245
|
+
eol: '\n' | '\r\n';
|
|
246
|
+
hasFinalNewline: boolean;
|
|
247
|
+
} {
|
|
248
|
+
const lines = [...state.lines];
|
|
249
|
+
const resolved: ResolvedChunk[] = [];
|
|
250
|
+
let start = 0;
|
|
251
|
+
|
|
252
|
+
for (const chunk of chunks) {
|
|
253
|
+
const chunkStart = resolveChunkStart(lines, chunk, start);
|
|
254
|
+
let strategy: ApplyPatchRescueStrategy | undefined;
|
|
255
|
+
|
|
256
|
+
if (chunk.old_lines.length === 0) {
|
|
257
|
+
if (chunk.is_end_of_file) {
|
|
258
|
+
resolved.push({
|
|
259
|
+
hit: {
|
|
260
|
+
start: lines.length,
|
|
261
|
+
del: 0,
|
|
262
|
+
add: [...chunk.new_lines],
|
|
263
|
+
},
|
|
264
|
+
old_lines: [],
|
|
265
|
+
canonical_old_lines: [],
|
|
266
|
+
canonical_new_lines: [...chunk.new_lines],
|
|
267
|
+
resolved_is_end_of_file: true,
|
|
268
|
+
rewritten: false,
|
|
269
|
+
strategy,
|
|
270
|
+
matchComparator: 'exact',
|
|
271
|
+
});
|
|
272
|
+
start = lines.length;
|
|
273
|
+
continue;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
if (!chunk.change_context) {
|
|
277
|
+
throw new Error(`Missing insertion anchor in ${file}`);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
const anchorMatch = resolveUniqueAnchor(
|
|
281
|
+
lines,
|
|
282
|
+
chunk.change_context,
|
|
283
|
+
start,
|
|
284
|
+
);
|
|
285
|
+
if (anchorMatch.kind === 'missing') {
|
|
286
|
+
throw new Error(
|
|
287
|
+
`Failed to find insertion anchor in ${file}:\n${chunk.change_context}`,
|
|
288
|
+
);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
if (anchorMatch.kind === 'ambiguous') {
|
|
292
|
+
throw new Error(
|
|
293
|
+
`Insertion anchor was ambiguous in ${file}:\n${chunk.change_context}`,
|
|
294
|
+
);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
const insertAt = anchorMatch.index + 1;
|
|
298
|
+
if (insertAt === lines.length) {
|
|
299
|
+
resolved.push({
|
|
300
|
+
hit: {
|
|
301
|
+
start: insertAt,
|
|
302
|
+
del: 0,
|
|
303
|
+
add: [...chunk.new_lines],
|
|
304
|
+
},
|
|
305
|
+
old_lines: [],
|
|
306
|
+
canonical_old_lines: [],
|
|
307
|
+
canonical_new_lines: [...chunk.new_lines],
|
|
308
|
+
canonical_change_context: anchorMatch.exact
|
|
309
|
+
? undefined
|
|
310
|
+
: anchorMatch.canonicalLine,
|
|
311
|
+
resolved_is_end_of_file: insertAt === lines.length,
|
|
312
|
+
rewritten: !anchorMatch.exact,
|
|
313
|
+
strategy: anchorMatch.exact ? strategy : 'anchor',
|
|
314
|
+
matchComparator: anchorMatch.comparator,
|
|
315
|
+
});
|
|
316
|
+
start = insertAt;
|
|
317
|
+
continue;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
const anchor = lines[insertAt];
|
|
321
|
+
|
|
322
|
+
strategy = 'anchor';
|
|
323
|
+
resolved.push({
|
|
324
|
+
hit: {
|
|
325
|
+
start: insertAt,
|
|
326
|
+
del: 0,
|
|
327
|
+
add: [...chunk.new_lines],
|
|
328
|
+
},
|
|
329
|
+
old_lines: [],
|
|
330
|
+
canonical_old_lines: [anchor],
|
|
331
|
+
canonical_new_lines: [...chunk.new_lines, anchor],
|
|
332
|
+
canonical_change_context: anchorMatch.exact
|
|
333
|
+
? undefined
|
|
334
|
+
: anchorMatch.canonicalLine,
|
|
335
|
+
resolved_is_end_of_file: insertAt + 1 === lines.length,
|
|
336
|
+
rewritten: true,
|
|
337
|
+
strategy,
|
|
338
|
+
matchComparator: anchorMatch.comparator,
|
|
339
|
+
});
|
|
340
|
+
start = insertAt;
|
|
341
|
+
continue;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
const found = locateChunk(lines, file, chunk, chunkStart, cfg);
|
|
345
|
+
resolved.push(found);
|
|
346
|
+
start = found.hit.start + found.hit.del;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
resolved.sort((a, b) => a.hit.start - b.hit.start);
|
|
350
|
+
|
|
351
|
+
for (let index = 1; index < resolved.length; index += 1) {
|
|
352
|
+
const previous = resolved[index - 1].hit;
|
|
353
|
+
const current = resolved[index].hit;
|
|
354
|
+
if (previous.start + previous.del > current.start) {
|
|
355
|
+
throw new Error(`Overlapping patch chunks in ${file}`);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
return {
|
|
360
|
+
lines,
|
|
361
|
+
resolved,
|
|
362
|
+
eol: state.eol,
|
|
363
|
+
hasFinalNewline: state.hasFinalNewline,
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
export async function resolveUpdateChunks(
|
|
368
|
+
file: string,
|
|
369
|
+
chunks: PatchChunk[],
|
|
370
|
+
cfg: ApplyPatchRuntimeOptions,
|
|
371
|
+
): Promise<{
|
|
372
|
+
lines: string[];
|
|
373
|
+
resolved: ResolvedChunk[];
|
|
374
|
+
eol: '\n' | '\r\n';
|
|
375
|
+
hasFinalNewline: boolean;
|
|
376
|
+
}> {
|
|
377
|
+
return resolveUpdateChunksFromFileLines(
|
|
378
|
+
file,
|
|
379
|
+
await readFileLinesWithEol(file),
|
|
380
|
+
chunks,
|
|
381
|
+
cfg,
|
|
382
|
+
);
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
export function deriveNewContentFromText(
|
|
386
|
+
file: string,
|
|
387
|
+
text: string,
|
|
388
|
+
chunks: PatchChunk[],
|
|
389
|
+
cfg: ApplyPatchRuntimeOptions,
|
|
390
|
+
): string {
|
|
391
|
+
const { lines, resolved, eol, hasFinalNewline } =
|
|
392
|
+
resolveUpdateChunksFromFileLines(file, splitFileLines(text), chunks, cfg);
|
|
393
|
+
|
|
394
|
+
return applyHits(
|
|
395
|
+
lines,
|
|
396
|
+
resolved.map((chunk) => chunk.hit),
|
|
397
|
+
eol,
|
|
398
|
+
hasFinalNewline,
|
|
399
|
+
);
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
export function resolveUpdateChunksFromText(
|
|
403
|
+
file: string,
|
|
404
|
+
text: string,
|
|
405
|
+
chunks: PatchChunk[],
|
|
406
|
+
cfg: ApplyPatchRuntimeOptions,
|
|
407
|
+
): {
|
|
408
|
+
lines: string[];
|
|
409
|
+
resolved: ResolvedChunk[];
|
|
410
|
+
eol: '\n' | '\r\n';
|
|
411
|
+
hasFinalNewline: boolean;
|
|
412
|
+
} {
|
|
413
|
+
return resolveUpdateChunksFromFileLines(
|
|
414
|
+
file,
|
|
415
|
+
splitFileLines(text),
|
|
416
|
+
chunks,
|
|
417
|
+
cfg,
|
|
418
|
+
);
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
export async function deriveNewContent(
|
|
422
|
+
file: string,
|
|
423
|
+
chunks: PatchChunk[],
|
|
424
|
+
cfg: ApplyPatchRuntimeOptions,
|
|
425
|
+
): Promise<string> {
|
|
426
|
+
const { lines, resolved, eol, hasFinalNewline } = await resolveUpdateChunks(
|
|
427
|
+
file,
|
|
428
|
+
chunks,
|
|
429
|
+
cfg,
|
|
430
|
+
);
|
|
431
|
+
return applyHits(
|
|
432
|
+
lines,
|
|
433
|
+
resolved.map((chunk) => chunk.hit),
|
|
434
|
+
eol,
|
|
435
|
+
hasFinalNewline,
|
|
436
|
+
);
|
|
437
|
+
}
|