@clinebot/core 0.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/README.md +88 -0
- package/dist/account/cline-account-service.d.ts +34 -0
- package/dist/account/index.d.ts +3 -0
- package/dist/account/rpc.d.ts +38 -0
- package/dist/account/types.d.ts +74 -0
- package/dist/agents/agent-config-loader.d.ts +18 -0
- package/dist/agents/agent-config-parser.d.ts +25 -0
- package/dist/agents/hooks-config-loader.d.ts +23 -0
- package/dist/agents/index.d.ts +11 -0
- package/dist/agents/plugin-config-loader.d.ts +22 -0
- package/dist/agents/plugin-loader.d.ts +9 -0
- package/dist/agents/plugin-sandbox.d.ts +12 -0
- package/dist/agents/unified-config-file-watcher.d.ts +77 -0
- package/dist/agents/user-instruction-config-loader.d.ts +63 -0
- package/dist/auth/client.d.ts +11 -0
- package/dist/auth/cline.d.ts +41 -0
- package/dist/auth/codex.d.ts +39 -0
- package/dist/auth/oca.d.ts +22 -0
- package/dist/auth/server.d.ts +22 -0
- package/dist/auth/types.d.ts +72 -0
- package/dist/auth/utils.d.ts +32 -0
- package/dist/chat/chat-schema.d.ts +145 -0
- package/dist/default-tools/constants.d.ts +23 -0
- package/dist/default-tools/definitions.d.ts +96 -0
- package/dist/default-tools/executors/apply-patch-parser.d.ts +68 -0
- package/dist/default-tools/executors/apply-patch.d.ts +26 -0
- package/dist/default-tools/executors/bash.d.ts +49 -0
- package/dist/default-tools/executors/editor.d.ts +31 -0
- package/dist/default-tools/executors/file-read.d.ts +40 -0
- package/dist/default-tools/executors/index.d.ts +44 -0
- package/dist/default-tools/executors/search.d.ts +50 -0
- package/dist/default-tools/executors/web-fetch.d.ts +58 -0
- package/dist/default-tools/index.d.ts +57 -0
- package/dist/default-tools/presets.d.ts +124 -0
- package/dist/default-tools/schemas.d.ts +121 -0
- package/dist/default-tools/types.d.ts +237 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +220 -0
- package/dist/input/file-indexer.d.ts +5 -0
- package/dist/input/index.d.ts +4 -0
- package/dist/input/mention-enricher.d.ts +12 -0
- package/dist/mcp/config-loader.d.ts +15 -0
- package/dist/mcp/index.d.ts +4 -0
- package/dist/mcp/manager.d.ts +24 -0
- package/dist/mcp/types.d.ts +66 -0
- package/dist/runtime/hook-file-hooks.d.ts +18 -0
- package/dist/runtime/rules.d.ts +5 -0
- package/dist/runtime/runtime-builder.d.ts +5 -0
- package/dist/runtime/sandbox/subprocess-sandbox.d.ts +19 -0
- package/dist/runtime/session-runtime.d.ts +36 -0
- package/dist/runtime/tool-approval.d.ts +9 -0
- package/dist/runtime/workflows.d.ts +13 -0
- package/dist/server/index.d.ts +47 -0
- package/dist/server/index.js +641 -0
- package/dist/session/default-session-manager.d.ts +77 -0
- package/dist/session/rpc-session-service.d.ts +12 -0
- package/dist/session/runtime-oauth-token-manager.d.ts +28 -0
- package/dist/session/session-artifacts.d.ts +19 -0
- package/dist/session/session-graph.d.ts +15 -0
- package/dist/session/session-host.d.ts +21 -0
- package/dist/session/session-manager.d.ts +50 -0
- package/dist/session/session-manifest.d.ts +30 -0
- package/dist/session/session-service.d.ts +113 -0
- package/dist/session/sqlite-rpc-session-backend.d.ts +30 -0
- package/dist/session/unified-session-persistence-service.d.ts +93 -0
- package/dist/session/workspace-manager.d.ts +28 -0
- package/dist/session/workspace-manifest.d.ts +25 -0
- package/dist/storage/provider-settings-legacy-migration.d.ts +13 -0
- package/dist/storage/provider-settings-manager.d.ts +20 -0
- package/dist/storage/sqlite-session-store.d.ts +29 -0
- package/dist/storage/sqlite-team-store.d.ts +31 -0
- package/dist/storage/team-store.d.ts +2 -0
- package/dist/team/index.d.ts +1 -0
- package/dist/team/projections.d.ts +8 -0
- package/dist/types/common.d.ts +10 -0
- package/dist/types/config.d.ts +37 -0
- package/dist/types/events.d.ts +54 -0
- package/dist/types/provider-settings.d.ts +20 -0
- package/dist/types/sessions.d.ts +9 -0
- package/dist/types/storage.d.ts +37 -0
- package/dist/types/workspace.d.ts +7 -0
- package/dist/types.d.ts +26 -0
- package/package.json +63 -0
- package/src/account/cline-account-service.test.ts +101 -0
- package/src/account/cline-account-service.ts +267 -0
- package/src/account/index.ts +20 -0
- package/src/account/rpc.test.ts +62 -0
- package/src/account/rpc.ts +172 -0
- package/src/account/types.ts +80 -0
- package/src/agents/agent-config-loader.test.ts +234 -0
- package/src/agents/agent-config-loader.ts +107 -0
- package/src/agents/agent-config-parser.ts +191 -0
- package/src/agents/hooks-config-loader.ts +97 -0
- package/src/agents/index.ts +84 -0
- package/src/agents/plugin-config-loader.test.ts +91 -0
- package/src/agents/plugin-config-loader.ts +160 -0
- package/src/agents/plugin-loader.test.ts +102 -0
- package/src/agents/plugin-loader.ts +105 -0
- package/src/agents/plugin-sandbox.test.ts +120 -0
- package/src/agents/plugin-sandbox.ts +471 -0
- package/src/agents/unified-config-file-watcher.test.ts +196 -0
- package/src/agents/unified-config-file-watcher.ts +483 -0
- package/src/agents/user-instruction-config-loader.test.ts +158 -0
- package/src/agents/user-instruction-config-loader.ts +438 -0
- package/src/auth/client.test.ts +40 -0
- package/src/auth/client.ts +25 -0
- package/src/auth/cline.test.ts +130 -0
- package/src/auth/cline.ts +414 -0
- package/src/auth/codex.test.ts +170 -0
- package/src/auth/codex.ts +466 -0
- package/src/auth/oca.test.ts +215 -0
- package/src/auth/oca.ts +546 -0
- package/src/auth/server.ts +216 -0
- package/src/auth/types.ts +78 -0
- package/src/auth/utils.test.ts +128 -0
- package/src/auth/utils.ts +247 -0
- package/src/chat/chat-schema.ts +82 -0
- package/src/default-tools/constants.ts +35 -0
- package/src/default-tools/definitions.test.ts +233 -0
- package/src/default-tools/definitions.ts +632 -0
- package/src/default-tools/executors/apply-patch-parser.ts +520 -0
- package/src/default-tools/executors/apply-patch.ts +359 -0
- package/src/default-tools/executors/bash.ts +205 -0
- package/src/default-tools/executors/editor.ts +231 -0
- package/src/default-tools/executors/file-read.test.ts +25 -0
- package/src/default-tools/executors/file-read.ts +94 -0
- package/src/default-tools/executors/index.ts +75 -0
- package/src/default-tools/executors/search.ts +278 -0
- package/src/default-tools/executors/web-fetch.ts +259 -0
- package/src/default-tools/index.ts +161 -0
- package/src/default-tools/presets.test.ts +63 -0
- package/src/default-tools/presets.ts +168 -0
- package/src/default-tools/schemas.ts +228 -0
- package/src/default-tools/types.ts +324 -0
- package/src/index.ts +119 -0
- package/src/input/file-indexer.d.ts +11 -0
- package/src/input/file-indexer.test.ts +87 -0
- package/src/input/file-indexer.ts +280 -0
- package/src/input/index.ts +7 -0
- package/src/input/mention-enricher.test.ts +82 -0
- package/src/input/mention-enricher.ts +119 -0
- package/src/mcp/config-loader.test.ts +238 -0
- package/src/mcp/config-loader.ts +219 -0
- package/src/mcp/index.ts +26 -0
- package/src/mcp/manager.test.ts +106 -0
- package/src/mcp/manager.ts +262 -0
- package/src/mcp/types.ts +88 -0
- package/src/runtime/hook-file-hooks.test.ts +106 -0
- package/src/runtime/hook-file-hooks.ts +736 -0
- package/src/runtime/index.ts +27 -0
- package/src/runtime/rules.ts +34 -0
- package/src/runtime/runtime-builder.team-persistence.test.ts +203 -0
- package/src/runtime/runtime-builder.test.ts +215 -0
- package/src/runtime/runtime-builder.ts +515 -0
- package/src/runtime/runtime-parity.test.ts +132 -0
- package/src/runtime/sandbox/subprocess-sandbox.ts +207 -0
- package/src/runtime/session-runtime.ts +44 -0
- package/src/runtime/tool-approval.ts +104 -0
- package/src/runtime/workflows.test.ts +119 -0
- package/src/runtime/workflows.ts +54 -0
- package/src/server/index.ts +282 -0
- package/src/session/default-session-manager.e2e.test.ts +354 -0
- package/src/session/default-session-manager.test.ts +816 -0
- package/src/session/default-session-manager.ts +1286 -0
- package/src/session/index.ts +37 -0
- package/src/session/rpc-session-service.ts +189 -0
- package/src/session/runtime-oauth-token-manager.test.ts +137 -0
- package/src/session/runtime-oauth-token-manager.ts +265 -0
- package/src/session/session-artifacts.ts +106 -0
- package/src/session/session-graph.ts +90 -0
- package/src/session/session-host.ts +190 -0
- package/src/session/session-manager.ts +56 -0
- package/src/session/session-manifest.ts +29 -0
- package/src/session/session-service.team-persistence.test.ts +48 -0
- package/src/session/session-service.ts +610 -0
- package/src/session/sqlite-rpc-session-backend.ts +303 -0
- package/src/session/unified-session-persistence-service.ts +781 -0
- package/src/session/workspace-manager.ts +98 -0
- package/src/session/workspace-manifest.ts +100 -0
- package/src/storage/artifact-store.ts +1 -0
- package/src/storage/index.ts +11 -0
- package/src/storage/provider-settings-legacy-migration.test.ts +175 -0
- package/src/storage/provider-settings-legacy-migration.ts +637 -0
- package/src/storage/provider-settings-manager.test.ts +111 -0
- package/src/storage/provider-settings-manager.ts +129 -0
- package/src/storage/session-store.ts +1 -0
- package/src/storage/sqlite-session-store.ts +270 -0
- package/src/storage/sqlite-team-store.ts +443 -0
- package/src/storage/team-store.ts +5 -0
- package/src/team/index.ts +4 -0
- package/src/team/projections.ts +285 -0
- package/src/types/common.ts +14 -0
- package/src/types/config.ts +64 -0
- package/src/types/events.ts +46 -0
- package/src/types/index.ts +24 -0
- package/src/types/provider-settings.ts +43 -0
- package/src/types/sessions.ts +16 -0
- package/src/types/storage.ts +64 -0
- package/src/types/workspace.ts +7 -0
- package/src/types.ts +127 -0
|
@@ -0,0 +1,520 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Apply Patch parser and patch model types.
|
|
3
|
+
*
|
|
4
|
+
* This parser supports the Cline apply_patch format used by the legacy runtime.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export const PATCH_MARKERS = {
|
|
8
|
+
BEGIN: "*** Begin Patch",
|
|
9
|
+
END: "*** End Patch",
|
|
10
|
+
ADD: "*** Add File: ",
|
|
11
|
+
UPDATE: "*** Update File: ",
|
|
12
|
+
DELETE: "*** Delete File: ",
|
|
13
|
+
MOVE: "*** Move to: ",
|
|
14
|
+
SECTION: "@@",
|
|
15
|
+
END_FILE: "*** End of File",
|
|
16
|
+
} as const;
|
|
17
|
+
|
|
18
|
+
export const BASH_WRAPPERS = ["%%bash", "apply_patch", "EOF", "```"] as const;
|
|
19
|
+
|
|
20
|
+
export enum PatchActionType {
|
|
21
|
+
ADD = "add",
|
|
22
|
+
DELETE = "delete",
|
|
23
|
+
UPDATE = "update",
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface PatchChunk {
|
|
27
|
+
origIndex: number;
|
|
28
|
+
delLines: string[];
|
|
29
|
+
insLines: string[];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface PatchAction {
|
|
33
|
+
type: PatchActionType;
|
|
34
|
+
newFile?: string;
|
|
35
|
+
chunks: PatchChunk[];
|
|
36
|
+
movePath?: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface PatchWarning {
|
|
40
|
+
path: string;
|
|
41
|
+
chunkIndex?: number;
|
|
42
|
+
message: string;
|
|
43
|
+
context?: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface Patch {
|
|
47
|
+
actions: Record<string, PatchAction>;
|
|
48
|
+
warnings?: PatchWarning[];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export class DiffError extends Error {
|
|
52
|
+
constructor(message: string) {
|
|
53
|
+
super(message);
|
|
54
|
+
this.name = "DiffError";
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function canonicalize(input: string): string {
|
|
59
|
+
const punctuationMap: Record<string, string> = {
|
|
60
|
+
"\u2010": "-",
|
|
61
|
+
"\u2011": "-",
|
|
62
|
+
"\u2012": "-",
|
|
63
|
+
"\u2013": "-",
|
|
64
|
+
"\u2014": "-",
|
|
65
|
+
"\u2212": "-",
|
|
66
|
+
"\u201C": '"',
|
|
67
|
+
"\u201D": '"',
|
|
68
|
+
"\u201E": '"',
|
|
69
|
+
"\u00AB": '"',
|
|
70
|
+
"\u00BB": '"',
|
|
71
|
+
"\u2018": "'",
|
|
72
|
+
"\u2019": "'",
|
|
73
|
+
"\u201B": "'",
|
|
74
|
+
"\u00A0": " ",
|
|
75
|
+
"\u202F": " ",
|
|
76
|
+
};
|
|
77
|
+
return input
|
|
78
|
+
.normalize("NFC")
|
|
79
|
+
.replace(/./gu, (char) => punctuationMap[char] ?? char)
|
|
80
|
+
.replace(/\\`/g, "`")
|
|
81
|
+
.replace(/\\'/g, "'")
|
|
82
|
+
.replace(/\\"/g, '"');
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export class PatchParser {
|
|
86
|
+
private patch: Patch = { actions: {}, warnings: [] };
|
|
87
|
+
private index = 0;
|
|
88
|
+
private fuzz = 0;
|
|
89
|
+
private currentPath?: string;
|
|
90
|
+
|
|
91
|
+
constructor(
|
|
92
|
+
private readonly lines: string[],
|
|
93
|
+
private readonly currentFiles: Record<string, string>,
|
|
94
|
+
) {}
|
|
95
|
+
|
|
96
|
+
parse(): { patch: Patch; fuzz: number } {
|
|
97
|
+
this.skipBeginSentinel();
|
|
98
|
+
|
|
99
|
+
while (this.hasMoreLines() && !this.isEndMarker()) {
|
|
100
|
+
this.parseNextAction();
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (this.patch.warnings?.length === 0) {
|
|
104
|
+
delete this.patch.warnings;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return { patch: this.patch, fuzz: this.fuzz };
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
private addWarning(warning: PatchWarning): void {
|
|
111
|
+
if (!this.patch.warnings) {
|
|
112
|
+
this.patch.warnings = [];
|
|
113
|
+
}
|
|
114
|
+
this.patch.warnings.push(warning);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
private skipBeginSentinel(): void {
|
|
118
|
+
if (this.lines[this.index]?.startsWith(PATCH_MARKERS.BEGIN)) {
|
|
119
|
+
this.index++;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
private hasMoreLines(): boolean {
|
|
124
|
+
return this.index < this.lines.length;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
private isEndMarker(): boolean {
|
|
128
|
+
return this.lines[this.index]?.startsWith(PATCH_MARKERS.END) ?? false;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
private parseNextAction(): void {
|
|
132
|
+
const line = this.lines[this.index];
|
|
133
|
+
if (line?.startsWith(PATCH_MARKERS.UPDATE)) {
|
|
134
|
+
this.parseUpdate(line.substring(PATCH_MARKERS.UPDATE.length).trim());
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
if (line?.startsWith(PATCH_MARKERS.DELETE)) {
|
|
138
|
+
this.parseDelete(line.substring(PATCH_MARKERS.DELETE.length).trim());
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
if (line?.startsWith(PATCH_MARKERS.ADD)) {
|
|
142
|
+
this.parseAdd(line.substring(PATCH_MARKERS.ADD.length).trim());
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
throw new DiffError(`Unknown line while parsing: ${line}`);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
private checkDuplicate(path: string, operation: string): void {
|
|
149
|
+
if (path in this.patch.actions) {
|
|
150
|
+
throw new DiffError(`Duplicate ${operation} for file: ${path}`);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
private parseUpdate(path: string): void {
|
|
155
|
+
this.checkDuplicate(path, "update");
|
|
156
|
+
this.currentPath = path;
|
|
157
|
+
|
|
158
|
+
this.index++;
|
|
159
|
+
const movePath = this.lines[this.index]?.startsWith(PATCH_MARKERS.MOVE)
|
|
160
|
+
? (this.lines[this.index++] ?? "")
|
|
161
|
+
.substring(PATCH_MARKERS.MOVE.length)
|
|
162
|
+
.trim()
|
|
163
|
+
: undefined;
|
|
164
|
+
|
|
165
|
+
if (!(path in this.currentFiles)) {
|
|
166
|
+
throw new DiffError(`Update File Error: Missing File: ${path}`);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const text = this.currentFiles[path] ?? "";
|
|
170
|
+
const action = this.parseUpdateFile(text, path);
|
|
171
|
+
action.movePath = movePath;
|
|
172
|
+
this.patch.actions[path] = action;
|
|
173
|
+
this.currentPath = undefined;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
private parseUpdateFile(text: string, path: string): PatchAction {
|
|
177
|
+
const action: PatchAction = { type: PatchActionType.UPDATE, chunks: [] };
|
|
178
|
+
const fileLines = text.split("\n");
|
|
179
|
+
let index = 0;
|
|
180
|
+
|
|
181
|
+
const stopMarkers = [
|
|
182
|
+
PATCH_MARKERS.END,
|
|
183
|
+
PATCH_MARKERS.UPDATE,
|
|
184
|
+
PATCH_MARKERS.DELETE,
|
|
185
|
+
PATCH_MARKERS.ADD,
|
|
186
|
+
PATCH_MARKERS.END_FILE,
|
|
187
|
+
];
|
|
188
|
+
|
|
189
|
+
while (
|
|
190
|
+
!stopMarkers.some((marker) =>
|
|
191
|
+
this.lines[this.index]?.startsWith(marker.trim()),
|
|
192
|
+
)
|
|
193
|
+
) {
|
|
194
|
+
const currentLine = this.lines[this.index];
|
|
195
|
+
const defStr = currentLine?.startsWith("@@ ")
|
|
196
|
+
? currentLine.substring(3)
|
|
197
|
+
: undefined;
|
|
198
|
+
const sectionStr = currentLine === "@@" ? currentLine : undefined;
|
|
199
|
+
|
|
200
|
+
if (defStr !== undefined || sectionStr !== undefined) {
|
|
201
|
+
this.index++;
|
|
202
|
+
} else if (index !== 0) {
|
|
203
|
+
throw new DiffError(`Invalid Line:\n${this.lines[this.index]}`);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
if (defStr?.trim()) {
|
|
207
|
+
const canonDefStr = canonicalize(defStr.trim());
|
|
208
|
+
for (let i = index; i < fileLines.length; i++) {
|
|
209
|
+
const fileLine = fileLines[i];
|
|
210
|
+
if (
|
|
211
|
+
fileLine &&
|
|
212
|
+
(canonicalize(fileLine) === canonDefStr ||
|
|
213
|
+
canonicalize(fileLine.trim()) === canonDefStr)
|
|
214
|
+
) {
|
|
215
|
+
index = i + 1;
|
|
216
|
+
if (
|
|
217
|
+
canonicalize(fileLine.trim()) === canonDefStr &&
|
|
218
|
+
canonicalize(fileLine) !== canonDefStr
|
|
219
|
+
) {
|
|
220
|
+
this.fuzz++;
|
|
221
|
+
}
|
|
222
|
+
break;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const [nextChunkContext, chunks, endPatchIndex, eof] = peek(
|
|
228
|
+
this.lines,
|
|
229
|
+
this.index,
|
|
230
|
+
);
|
|
231
|
+
const [newIndex, fuzz, similarity] = findContext(
|
|
232
|
+
fileLines,
|
|
233
|
+
nextChunkContext,
|
|
234
|
+
index,
|
|
235
|
+
eof,
|
|
236
|
+
);
|
|
237
|
+
|
|
238
|
+
if (newIndex === -1) {
|
|
239
|
+
const contextText = nextChunkContext.join("\n");
|
|
240
|
+
this.addWarning({
|
|
241
|
+
path: this.currentPath || path,
|
|
242
|
+
chunkIndex: action.chunks.length,
|
|
243
|
+
message: `Could not find matching context (similarity: ${similarity.toFixed(2)}). Chunk skipped.`,
|
|
244
|
+
context:
|
|
245
|
+
contextText.length > 200
|
|
246
|
+
? `${contextText.substring(0, 200)}...`
|
|
247
|
+
: contextText,
|
|
248
|
+
});
|
|
249
|
+
this.index = endPatchIndex;
|
|
250
|
+
} else {
|
|
251
|
+
this.fuzz += fuzz;
|
|
252
|
+
for (const chunk of chunks) {
|
|
253
|
+
chunk.origIndex += newIndex;
|
|
254
|
+
action.chunks.push(chunk);
|
|
255
|
+
}
|
|
256
|
+
index = newIndex + nextChunkContext.length;
|
|
257
|
+
this.index = endPatchIndex;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
return action;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
private parseDelete(path: string): void {
|
|
265
|
+
this.checkDuplicate(path, "delete");
|
|
266
|
+
if (!(path in this.currentFiles)) {
|
|
267
|
+
throw new DiffError(`Delete File Error: Missing File: ${path}`);
|
|
268
|
+
}
|
|
269
|
+
this.patch.actions[path] = { type: PatchActionType.DELETE, chunks: [] };
|
|
270
|
+
this.index++;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
private parseAdd(path: string): void {
|
|
274
|
+
this.checkDuplicate(path, "add");
|
|
275
|
+
if (path in this.currentFiles) {
|
|
276
|
+
throw new DiffError(`Add File Error: File already exists: ${path}`);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
this.index++;
|
|
280
|
+
const lines: string[] = [];
|
|
281
|
+
const stopMarkers = [
|
|
282
|
+
PATCH_MARKERS.END,
|
|
283
|
+
PATCH_MARKERS.UPDATE,
|
|
284
|
+
PATCH_MARKERS.DELETE,
|
|
285
|
+
PATCH_MARKERS.ADD,
|
|
286
|
+
];
|
|
287
|
+
|
|
288
|
+
while (
|
|
289
|
+
this.hasMoreLines() &&
|
|
290
|
+
!stopMarkers.some((marker) =>
|
|
291
|
+
this.lines[this.index]?.startsWith(marker.trim()),
|
|
292
|
+
)
|
|
293
|
+
) {
|
|
294
|
+
const line = this.lines[this.index++];
|
|
295
|
+
if (line === undefined) {
|
|
296
|
+
break;
|
|
297
|
+
}
|
|
298
|
+
if (!line.startsWith("+")) {
|
|
299
|
+
throw new DiffError(`Invalid Add File line (missing '+'): ${line}`);
|
|
300
|
+
}
|
|
301
|
+
lines.push(line.substring(1));
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
this.patch.actions[path] = {
|
|
305
|
+
type: PatchActionType.ADD,
|
|
306
|
+
newFile: lines.join("\n"),
|
|
307
|
+
chunks: [],
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
function calculateSimilarity(str1: string, str2: string): number {
|
|
313
|
+
const longer = str1.length > str2.length ? str1 : str2;
|
|
314
|
+
const shorter = str1.length > str2.length ? str2 : str1;
|
|
315
|
+
if (longer.length === 0) {
|
|
316
|
+
return 1;
|
|
317
|
+
}
|
|
318
|
+
const editDistance = levenshteinDistance(shorter, longer);
|
|
319
|
+
return (longer.length - editDistance) / longer.length;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
function levenshteinDistance(str1: string, str2: string): number {
|
|
323
|
+
const rows = str2.length + 1;
|
|
324
|
+
const cols = str1.length + 1;
|
|
325
|
+
const matrix = new Array<number>(rows * cols).fill(0);
|
|
326
|
+
const at = (r: number, c: number): number => matrix[r * cols + c] ?? 0;
|
|
327
|
+
const set = (r: number, c: number, value: number): void => {
|
|
328
|
+
matrix[r * cols + c] = value;
|
|
329
|
+
};
|
|
330
|
+
|
|
331
|
+
for (let i = 0; i <= str2.length; i++) set(i, 0, i);
|
|
332
|
+
for (let j = 0; j <= str1.length; j++) set(0, j, j);
|
|
333
|
+
|
|
334
|
+
for (let i = 1; i <= str2.length; i++) {
|
|
335
|
+
for (let j = 1; j <= str1.length; j++) {
|
|
336
|
+
if (str2[i - 1] === str1[j - 1]) {
|
|
337
|
+
set(i, j, at(i - 1, j - 1));
|
|
338
|
+
} else {
|
|
339
|
+
set(i, j, 1 + Math.min(at(i - 1, j - 1), at(i, j - 1), at(i - 1, j)));
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
return at(str2.length, str1.length);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
function findContext(
|
|
348
|
+
lines: string[],
|
|
349
|
+
context: string[],
|
|
350
|
+
start: number,
|
|
351
|
+
eof: boolean,
|
|
352
|
+
): [number, number, number] {
|
|
353
|
+
if (context.length === 0) {
|
|
354
|
+
return [start, 0, 1];
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
let bestSimilarity = 0;
|
|
358
|
+
const findCore = (startIdx: number): [number, number, number] => {
|
|
359
|
+
const canonicalContext = canonicalize(context.join("\n"));
|
|
360
|
+
|
|
361
|
+
for (let i = startIdx; i < lines.length; i++) {
|
|
362
|
+
const segment = canonicalize(
|
|
363
|
+
lines.slice(i, i + context.length).join("\n"),
|
|
364
|
+
);
|
|
365
|
+
if (segment === canonicalContext) {
|
|
366
|
+
return [i, 0, 1];
|
|
367
|
+
}
|
|
368
|
+
const similarity = calculateSimilarity(segment, canonicalContext);
|
|
369
|
+
if (similarity > bestSimilarity) {
|
|
370
|
+
bestSimilarity = similarity;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
for (let i = startIdx; i < lines.length; i++) {
|
|
375
|
+
const segment = canonicalize(
|
|
376
|
+
lines
|
|
377
|
+
.slice(i, i + context.length)
|
|
378
|
+
.map((line) => line.trimEnd())
|
|
379
|
+
.join("\n"),
|
|
380
|
+
);
|
|
381
|
+
const canonicalTrimmed = canonicalize(
|
|
382
|
+
context.map((line) => line.trimEnd()).join("\n"),
|
|
383
|
+
);
|
|
384
|
+
if (segment === canonicalTrimmed) {
|
|
385
|
+
return [i, 1, 1];
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
for (let i = startIdx; i < lines.length; i++) {
|
|
390
|
+
const segment = canonicalize(
|
|
391
|
+
lines
|
|
392
|
+
.slice(i, i + context.length)
|
|
393
|
+
.map((line) => line.trim())
|
|
394
|
+
.join("\n"),
|
|
395
|
+
);
|
|
396
|
+
const canonicalTrimmed = canonicalize(
|
|
397
|
+
context.map((line) => line.trim()).join("\n"),
|
|
398
|
+
);
|
|
399
|
+
if (segment === canonicalTrimmed) {
|
|
400
|
+
return [i, 100, 1];
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
const similarityThreshold = 0.66;
|
|
405
|
+
for (let i = startIdx; i < lines.length; i++) {
|
|
406
|
+
const segment = canonicalize(
|
|
407
|
+
lines.slice(i, i + context.length).join("\n"),
|
|
408
|
+
);
|
|
409
|
+
const similarity = calculateSimilarity(segment, canonicalContext);
|
|
410
|
+
if (similarity >= similarityThreshold) {
|
|
411
|
+
return [i, 1000, similarity];
|
|
412
|
+
}
|
|
413
|
+
if (similarity > bestSimilarity) {
|
|
414
|
+
bestSimilarity = similarity;
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
return [-1, 0, bestSimilarity];
|
|
419
|
+
};
|
|
420
|
+
|
|
421
|
+
if (eof) {
|
|
422
|
+
let [newIndex, fuzz, similarity] = findCore(lines.length - context.length);
|
|
423
|
+
if (newIndex !== -1) {
|
|
424
|
+
return [newIndex, fuzz, similarity];
|
|
425
|
+
}
|
|
426
|
+
[newIndex, fuzz, similarity] = findCore(start);
|
|
427
|
+
return [newIndex, fuzz + 10000, similarity];
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
return findCore(start);
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
type PeekResult = [string[], PatchChunk[], number, boolean];
|
|
434
|
+
|
|
435
|
+
function peek(lines: string[], initialIndex: number): PeekResult {
|
|
436
|
+
let index = initialIndex;
|
|
437
|
+
const old: string[] = [];
|
|
438
|
+
let delLines: string[] = [];
|
|
439
|
+
let insLines: string[] = [];
|
|
440
|
+
const chunks: PatchChunk[] = [];
|
|
441
|
+
let mode: "keep" | "add" | "delete" = "keep";
|
|
442
|
+
|
|
443
|
+
const stopMarkers = [
|
|
444
|
+
"@@",
|
|
445
|
+
PATCH_MARKERS.END,
|
|
446
|
+
PATCH_MARKERS.UPDATE,
|
|
447
|
+
PATCH_MARKERS.DELETE,
|
|
448
|
+
PATCH_MARKERS.ADD,
|
|
449
|
+
PATCH_MARKERS.END_FILE,
|
|
450
|
+
];
|
|
451
|
+
|
|
452
|
+
while (index < lines.length) {
|
|
453
|
+
const sourceLine = lines[index];
|
|
454
|
+
if (
|
|
455
|
+
!sourceLine ||
|
|
456
|
+
stopMarkers.some((marker) => sourceLine.startsWith(marker.trim()))
|
|
457
|
+
) {
|
|
458
|
+
break;
|
|
459
|
+
}
|
|
460
|
+
if (sourceLine === "***") {
|
|
461
|
+
break;
|
|
462
|
+
}
|
|
463
|
+
if (sourceLine.startsWith("***")) {
|
|
464
|
+
throw new DiffError(`Invalid line: ${sourceLine}`);
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
index++;
|
|
468
|
+
const previousMode: "keep" | "add" | "delete" = mode;
|
|
469
|
+
let line = sourceLine;
|
|
470
|
+
|
|
471
|
+
if (line[0] === "+") {
|
|
472
|
+
mode = "add";
|
|
473
|
+
} else if (line[0] === "-") {
|
|
474
|
+
mode = "delete";
|
|
475
|
+
} else if (line[0] === " ") {
|
|
476
|
+
mode = "keep";
|
|
477
|
+
} else {
|
|
478
|
+
mode = "keep";
|
|
479
|
+
line = ` ${line}`;
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
line = line.slice(1);
|
|
483
|
+
|
|
484
|
+
if (mode === "keep" && previousMode !== mode) {
|
|
485
|
+
if (insLines.length || delLines.length) {
|
|
486
|
+
chunks.push({
|
|
487
|
+
origIndex: old.length - delLines.length,
|
|
488
|
+
delLines,
|
|
489
|
+
insLines,
|
|
490
|
+
});
|
|
491
|
+
}
|
|
492
|
+
delLines = [];
|
|
493
|
+
insLines = [];
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
if (mode === "delete") {
|
|
497
|
+
delLines.push(line);
|
|
498
|
+
old.push(line);
|
|
499
|
+
} else if (mode === "add") {
|
|
500
|
+
insLines.push(line);
|
|
501
|
+
} else {
|
|
502
|
+
old.push(line);
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
if (insLines.length || delLines.length) {
|
|
507
|
+
chunks.push({
|
|
508
|
+
origIndex: old.length - delLines.length,
|
|
509
|
+
delLines,
|
|
510
|
+
insLines,
|
|
511
|
+
});
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
if (index < lines.length && lines[index] === PATCH_MARKERS.END_FILE) {
|
|
515
|
+
index++;
|
|
516
|
+
return [old, chunks, index, true];
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
return [old, chunks, index, false];
|
|
520
|
+
}
|