editcodewithai 2.5.0 → 2.6.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 CHANGED
@@ -171,6 +171,45 @@ The library handles several file operations automatically:
171
171
  - **Creating new files**: When the AI includes a file name that doesn't exist in the original set.
172
172
  - **Deleting files**: When the AI returns empty/whitespace-only content for a file (works with `whole` format; for `diff`/`udiff`, deletions happen naturally when all content is replaced).
173
173
 
174
+ ### Best-effort application and warnings
175
+
176
+ `performAiEdit` applies edits independently and **never throws because the model
177
+ returned an edit that could not be matched**. If one edit references a missing
178
+ file, an anchor that cannot be found, or an ambiguous location, that edit is
179
+ skipped and reported — every other valid edit is still applied.
180
+
181
+ Skipped edits are returned in `result.warnings`:
182
+
183
+ ```typescript
184
+ const result = await performAiEdit({
185
+ prompt,
186
+ files,
187
+ llmFunction,
188
+ editFormat: "diff",
189
+ });
190
+
191
+ for (const warning of result.warnings ?? []) {
192
+ console.warn(warning.code, warning.fileName, warning.message);
193
+ }
194
+ ```
195
+
196
+ | Warning code | Meaning |
197
+ | ------------------ | ------------------------------------------------------------------- |
198
+ | `FILE_NOT_FOUND` | The edit targets a filename that isn't in the file set. |
199
+ | `SEARCH_NOT_FOUND` | The `SEARCH` text could not be located in the file. |
200
+ | `HUNK_NOT_FOUND` | A udiff hunk's original lines could not be located. |
201
+ | `AMBIGUOUS_SEARCH` | The anchor matched more than one location, so the edit was skipped. |
202
+ | `AMBIGUOUS_FILE` | More than one file shares the target name, so the edit was skipped. |
203
+ | `EMPTY_SEARCH` | The search block was empty, so the edit could not be applied. |
204
+ | `NO_EDITS_PARSED` | The model response contained no recognizable edits. |
205
+
206
+ Matching is deterministic: only harmless formatting drift is absorbed (CRLF vs
207
+ LF, a missing/extra trailing newline, trailing whitespace). Anything ambiguous or
208
+ unmatched is skipped rather than guessed. Successful normalized matches produce
209
+ no warnings.
210
+
211
+ `warnings` is optional, so existing consumers continue to work unchanged.
212
+
174
213
  ## API Reference
175
214
 
176
215
  ### `performAiEdit(params)`
@@ -198,16 +237,17 @@ type LlmFunction = (prompt: string) => Promise<{
198
237
 
199
238
  #### Output: `PerformAiEditResult`
200
239
 
201
- | Property | Type | Description |
202
- | ------------------------ | ---------- | -------------------------------------------------------------- |
203
- | `changedFiles` | `VizFiles` | The updated file collection with all edits applied. |
204
- | `openRouterGenerationId` | `string` | The generation ID from the LLM function response. |
205
- | `upstreamCostCents` | `number` | Cost in cents (only populated if `apiKey` was provided). |
206
- | `provider` | `string` | The OpenRouter provider name used. |
207
- | `inputTokens` | `number` | Number of input (prompt) tokens billed. |
208
- | `outputTokens` | `number` | Number of output (completion) tokens billed. |
209
- | `promptTemplateVersion` | `number` | Version of the prompt template used (for tracking migrations). |
210
- | `rawResponse` | `string` | The raw string response from the LLM, unmodified. |
240
+ | Property | Type | Description |
241
+ | ------------------------ | ---------------- | ----------------------------------------------------------------- |
242
+ | `changedFiles` | `VizFiles` | The updated file collection with all edits applied. |
243
+ | `openRouterGenerationId` | `string` | The generation ID from the LLM function response. |
244
+ | `upstreamCostCents` | `number` | Cost in cents (only populated if `apiKey` was provided). |
245
+ | `provider` | `string` | The OpenRouter provider name used. |
246
+ | `inputTokens` | `number` | Number of input (prompt) tokens billed. |
247
+ | `outputTokens` | `number` | Number of output (completion) tokens billed. |
248
+ | `promptTemplateVersion` | `number` | Version of the prompt template used (for tracking migrations). |
249
+ | `rawResponse` | `string` | The raw string response from the LLM, unmodified. |
250
+ | `warnings` | `ApplyWarning[]` | Edits that were skipped because they could not be applied safely. |
211
251
 
212
252
  ### Exported Utilities
213
253
 
@@ -225,11 +265,14 @@ import {
225
265
 
226
266
  // --- Diff parsing (manual use) ---
227
267
  parseDiffs, // Parse search/replace blocks (diff format)
228
- applyDiffs, // Apply parsed diffs to a file set
268
+ applyDiffs, // Apply parsed diffs to a file set (throws on unmatched edits)
269
+ applyDiffsSafe, // Best-effort apply: returns { files, warnings } instead of throwing
229
270
  parseDiffFenced, // Parse search/replace blocks (diff-fenced format)
230
271
  parseUdiffs, // Parse unified diff hunks
231
- applyUdiffs, // Apply parsed unified diff hunks to a file set
272
+ applyUdiffs, // Apply parsed unified diff hunks (throws on unmatched edits)
273
+ applyUdiffsSafe, // Best-effort apply: returns { files, warnings } instead of throwing
232
274
  applyHybridEdits, // Apply mixed whole-file + diff edits from a single response
275
+ applyHybridEditsSafe, // Best-effort hybrid apply: returns { files, warnings }
233
276
 
234
277
  // --- Cost metadata ---
235
278
  getGenerationMetadata, // Fetch OpenRouter cost data for a generation ID
@@ -0,0 +1,13 @@
1
+ import { VizFiles } from "@vizhub/viz-types";
2
+ import type { ApplyResult, ParsedEdit } from "./types";
3
+ /**
4
+ * Applies parsed edits on a best-effort basis.
5
+ *
6
+ * Each edit is applied independently: an edit that cannot be applied safely is
7
+ * skipped and reported in `warnings`, and never prevents other edits from being
8
+ * applied. The input `files` is never mutated.
9
+ *
10
+ * This function only throws for programmer error; anticipated model-output
11
+ * failures are reported as warnings.
12
+ */
13
+ export declare function applyEditsSafe(files: VizFiles, edits: ParsedEdit[]): ApplyResult;
@@ -0,0 +1,102 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.applyEditsSafe = applyEditsSafe;
4
+ const matching_1 = require("./matching");
5
+ /**
6
+ * Normalizes a filename for comparison: trims, converts Windows separators to
7
+ * "/", collapses duplicate separators, and strips leading "./" and "/".
8
+ *
9
+ * Case is intentionally preserved — case-insensitive matching is not part of
10
+ * the deterministic behavior and is deferred.
11
+ */
12
+ function normalizeFileName(name) {
13
+ return name
14
+ .trim()
15
+ .replace(/\\/g, "/")
16
+ .replace(/\/+/g, "/")
17
+ .replace(/^\.\//, "")
18
+ .replace(/^\/+/, "");
19
+ }
20
+ function resolveFileId(files, rawName) {
21
+ const target = normalizeFileName(rawName);
22
+ const matches = Object.keys(files).filter((id) => normalizeFileName(files[id].name) === target);
23
+ if (matches.length === 1)
24
+ return { kind: "found", id: matches[0] };
25
+ if (matches.length > 1)
26
+ return { kind: "ambiguous" };
27
+ return { kind: "none" };
28
+ }
29
+ function anchorOf(edit) {
30
+ return edit.kind === "diff" ? edit.search : edit.original;
31
+ }
32
+ function replacementOf(edit) {
33
+ return edit.kind === "diff" ? edit.replace : edit.updated;
34
+ }
35
+ /**
36
+ * Applies parsed edits on a best-effort basis.
37
+ *
38
+ * Each edit is applied independently: an edit that cannot be applied safely is
39
+ * skipped and reported in `warnings`, and never prevents other edits from being
40
+ * applied. The input `files` is never mutated.
41
+ *
42
+ * This function only throws for programmer error; anticipated model-output
43
+ * failures are reported as warnings.
44
+ */
45
+ function applyEditsSafe(files, edits) {
46
+ const work = JSON.parse(JSON.stringify(files));
47
+ const warnings = [];
48
+ for (const edit of edits) {
49
+ const resolution = resolveFileId(work, edit.fileName);
50
+ if (resolution.kind === "ambiguous") {
51
+ warnings.push({
52
+ code: "AMBIGUOUS_FILE",
53
+ fileName: edit.fileName,
54
+ message: `Skipped ${edit.fileName}: multiple files share that name.`,
55
+ });
56
+ continue;
57
+ }
58
+ if (resolution.kind === "none") {
59
+ warnings.push({
60
+ code: "FILE_NOT_FOUND",
61
+ fileName: edit.fileName,
62
+ message: `Skipped changes to ${edit.fileName}: file not found.`,
63
+ });
64
+ continue;
65
+ }
66
+ const anchor = anchorOf(edit);
67
+ if (anchor.trim() === "") {
68
+ warnings.push({
69
+ code: "EMPTY_SEARCH",
70
+ fileName: edit.fileName,
71
+ message: `Skipped a change to ${edit.fileName}: the search block was empty.`,
72
+ });
73
+ continue;
74
+ }
75
+ const file = work[resolution.id];
76
+ const match = (0, matching_1.findAnchor)(file.text, anchor);
77
+ if (match === null) {
78
+ warnings.push({
79
+ code: edit.kind === "udiff" ? "HUNK_NOT_FOUND" : "SEARCH_NOT_FOUND",
80
+ fileName: edit.fileName,
81
+ message: edit.kind === "udiff"
82
+ ? `Skipped part of a change to ${edit.fileName}: referenced lines were not found.`
83
+ : `Skipped a change to ${edit.fileName}: the referenced code was not found.`,
84
+ });
85
+ continue;
86
+ }
87
+ // Ambiguous anchors are never guessed: skip rather than replace the first.
88
+ if (match.matchCount > 1) {
89
+ warnings.push({
90
+ code: "AMBIGUOUS_SEARCH",
91
+ fileName: edit.fileName,
92
+ message: `Skipped a change to ${edit.fileName}: the referenced code matched ${match.matchCount} locations.`,
93
+ });
94
+ continue;
95
+ }
96
+ file.text =
97
+ file.text.slice(0, match.start) +
98
+ replacementOf(edit) +
99
+ file.text.slice(match.end);
100
+ }
101
+ return { files: work, warnings };
102
+ }
@@ -1,4 +1,5 @@
1
1
  import { VizFiles, VizFile, FileCollection } from "@vizhub/viz-types";
2
+ import type { ApplyResult } from "./types";
2
3
  /**
3
4
  * If the LLM outputs empty text for a file, we interpret this
4
5
  * as a request to delete the file.
@@ -43,4 +44,21 @@ export declare function parseUdiffs(responseText: string): UdiffHunk[];
43
44
  * (If the same file appears in both forms, the whole-file content wins.)
44
45
  */
45
46
  export declare function applyHybridEdits(responseText: string, originalFiles: VizFiles, parseWholeFiles: (text: string) => FileCollection): VizFiles;
47
+ /**
48
+ * Best-effort variant of `applyDiffs`. Never throws for unmatched files or
49
+ * anchors; skipped edits are reported in `warnings`.
50
+ */
51
+ export declare function applyDiffsSafe(originalFiles: VizFiles, diffs: Diff[]): ApplyResult;
52
+ /**
53
+ * Best-effort variant of `applyUdiffs`. Never throws for unmatched files or
54
+ * hunks; skipped edits are reported in `warnings`.
55
+ */
56
+ export declare function applyUdiffsSafe(originalFiles: VizFiles, hunks: UdiffHunk[]): ApplyResult;
57
+ /**
58
+ * Best-effort variant of `applyHybridEdits`. Applies search/replace diffs first,
59
+ * then whole-file replacements. Warnings for diffs that target a file which is
60
+ * also replaced wholesale are suppressed, since the whole-file content
61
+ * supersedes the failed diff.
62
+ */
63
+ export declare function applyHybridEditsSafe(responseText: string, originalFiles: VizFiles, parseWholeFiles: (text: string) => FileCollection): ApplyResult;
46
64
  export declare function applyUdiffs(originalFiles: VizFiles, hunks: UdiffHunk[]): VizFiles;
package/dist/fileUtils.js CHANGED
@@ -9,8 +9,12 @@ exports.applyDiffs = applyDiffs;
9
9
  exports.parseDiffFenced = parseDiffFenced;
10
10
  exports.parseUdiffs = parseUdiffs;
11
11
  exports.applyHybridEdits = applyHybridEdits;
12
+ exports.applyDiffsSafe = applyDiffsSafe;
13
+ exports.applyUdiffsSafe = applyUdiffsSafe;
14
+ exports.applyHybridEditsSafe = applyHybridEditsSafe;
12
15
  exports.applyUdiffs = applyUdiffs;
13
16
  const viz_utils_1 = require("@vizhub/viz-utils");
17
+ const applyEngine_1 = require("./applyEngine");
14
18
  /**
15
19
  * If the LLM outputs empty text for a file, we interpret this
16
20
  * as a request to delete the file.
@@ -184,6 +188,45 @@ function applyHybridEdits(responseText, originalFiles, parseWholeFiles) {
184
188
  changedFiles = mergeFileChanges(changedFiles, parsed);
185
189
  return changedFiles;
186
190
  }
191
+ /**
192
+ * Best-effort variant of `applyDiffs`. Never throws for unmatched files or
193
+ * anchors; skipped edits are reported in `warnings`.
194
+ */
195
+ function applyDiffsSafe(originalFiles, diffs) {
196
+ return (0, applyEngine_1.applyEditsSafe)(originalFiles, diffs.map((diff) => ({
197
+ kind: "diff",
198
+ fileName: diff.fileName,
199
+ search: diff.search,
200
+ replace: diff.replace,
201
+ })));
202
+ }
203
+ /**
204
+ * Best-effort variant of `applyUdiffs`. Never throws for unmatched files or
205
+ * hunks; skipped edits are reported in `warnings`.
206
+ */
207
+ function applyUdiffsSafe(originalFiles, hunks) {
208
+ return (0, applyEngine_1.applyEditsSafe)(originalFiles, hunks.map((hunk) => ({
209
+ kind: "udiff",
210
+ fileName: hunk.fileName,
211
+ original: hunk.original,
212
+ updated: hunk.updated,
213
+ })));
214
+ }
215
+ /**
216
+ * Best-effort variant of `applyHybridEdits`. Applies search/replace diffs first,
217
+ * then whole-file replacements. Warnings for diffs that target a file which is
218
+ * also replaced wholesale are suppressed, since the whole-file content
219
+ * supersedes the failed diff.
220
+ */
221
+ function applyHybridEditsSafe(responseText, originalFiles, parseWholeFiles) {
222
+ const diffResult = applyDiffsSafe(originalFiles, parseDiffs(responseText));
223
+ const parsedWholeFiles = parseWholeFiles(responseText);
224
+ const changedFiles = mergeFileChanges(diffResult.files, parsedWholeFiles);
225
+ const wholeFileNames = new Set(Object.keys(parsedWholeFiles).map((name) => name.trim()));
226
+ const warnings = diffResult.warnings.filter((warning) => warning.fileName === undefined ||
227
+ !wholeFileNames.has(warning.fileName.trim()));
228
+ return { files: changedFiles, warnings };
229
+ }
187
230
  function applyUdiffs(originalFiles, hunks) {
188
231
  const changedFiles = JSON.parse(JSON.stringify(originalFiles));
189
232
  for (const hunk of hunks) {
package/dist/index.d.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  import type { PerformAiEditParams, PerformAiEditResult } from "./types";
2
2
  import { PROMPT_TEMPLATE_VERSION, assembleFullPrompt, FORMAT_INSTRUCTIONS } from "./prompt";
3
3
  import { getGenerationMetadata } from "./metadata";
4
- import { prepareFilesForPrompt, mergeFileChanges, parseDiffs, applyDiffs, parseDiffFenced, parseUdiffs, applyUdiffs, applyHybridEdits, isImageFile } from "./fileUtils";
5
- export type { LlmFunction, PerformAiEditParams, PerformAiEditResult, EditFormat, } from "./types";
6
- export { FORMAT_INSTRUCTIONS, mergeFileChanges, prepareFilesForPrompt, isImageFile, parseDiffs, applyDiffs, parseDiffFenced, parseUdiffs, applyUdiffs, applyHybridEdits, assembleFullPrompt, getGenerationMetadata, PROMPT_TEMPLATE_VERSION, };
4
+ import { prepareFilesForPrompt, mergeFileChanges, parseDiffs, applyDiffs, applyDiffsSafe, parseDiffFenced, parseUdiffs, applyUdiffs, applyUdiffsSafe, applyHybridEdits, applyHybridEditsSafe, isImageFile } from "./fileUtils";
5
+ export type { LlmFunction, PerformAiEditParams, PerformAiEditResult, EditFormat, ApplyWarning, ApplyWarningCode, ApplyResult, ParsedEdit, } from "./types";
6
+ export { FORMAT_INSTRUCTIONS, mergeFileChanges, prepareFilesForPrompt, isImageFile, parseDiffs, applyDiffs, applyDiffsSafe, parseDiffFenced, parseUdiffs, applyUdiffs, applyUdiffsSafe, applyHybridEdits, applyHybridEditsSafe, assembleFullPrompt, getGenerationMetadata, PROMPT_TEMPLATE_VERSION, };
7
7
  /**
8
8
  * Core AI logic for:
9
9
  * - Building the prompt (including context)
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PROMPT_TEMPLATE_VERSION = exports.getGenerationMetadata = exports.assembleFullPrompt = exports.applyHybridEdits = exports.applyUdiffs = exports.parseUdiffs = exports.parseDiffFenced = exports.applyDiffs = exports.parseDiffs = exports.isImageFile = exports.prepareFilesForPrompt = exports.mergeFileChanges = exports.FORMAT_INSTRUCTIONS = void 0;
3
+ exports.PROMPT_TEMPLATE_VERSION = exports.getGenerationMetadata = exports.assembleFullPrompt = exports.applyHybridEditsSafe = exports.applyHybridEdits = exports.applyUdiffsSafe = exports.applyUdiffs = exports.parseUdiffs = exports.parseDiffFenced = exports.applyDiffsSafe = exports.applyDiffs = exports.parseDiffs = exports.isImageFile = exports.prepareFilesForPrompt = exports.mergeFileChanges = exports.FORMAT_INSTRUCTIONS = void 0;
4
4
  exports.performAiEdit = performAiEdit;
5
5
  const llm_code_format_1 = require("llm-code-format");
6
6
  const prompt_1 = require("./prompt");
@@ -14,10 +14,13 @@ Object.defineProperty(exports, "prepareFilesForPrompt", { enumerable: true, get:
14
14
  Object.defineProperty(exports, "mergeFileChanges", { enumerable: true, get: function () { return fileUtils_1.mergeFileChanges; } });
15
15
  Object.defineProperty(exports, "parseDiffs", { enumerable: true, get: function () { return fileUtils_1.parseDiffs; } });
16
16
  Object.defineProperty(exports, "applyDiffs", { enumerable: true, get: function () { return fileUtils_1.applyDiffs; } });
17
+ Object.defineProperty(exports, "applyDiffsSafe", { enumerable: true, get: function () { return fileUtils_1.applyDiffsSafe; } });
17
18
  Object.defineProperty(exports, "parseDiffFenced", { enumerable: true, get: function () { return fileUtils_1.parseDiffFenced; } });
18
19
  Object.defineProperty(exports, "parseUdiffs", { enumerable: true, get: function () { return fileUtils_1.parseUdiffs; } });
19
20
  Object.defineProperty(exports, "applyUdiffs", { enumerable: true, get: function () { return fileUtils_1.applyUdiffs; } });
21
+ Object.defineProperty(exports, "applyUdiffsSafe", { enumerable: true, get: function () { return fileUtils_1.applyUdiffsSafe; } });
20
22
  Object.defineProperty(exports, "applyHybridEdits", { enumerable: true, get: function () { return fileUtils_1.applyHybridEdits; } });
23
+ Object.defineProperty(exports, "applyHybridEditsSafe", { enumerable: true, get: function () { return fileUtils_1.applyHybridEditsSafe; } });
21
24
  Object.defineProperty(exports, "isImageFile", { enumerable: true, get: function () { return fileUtils_1.isImageFile; } });
22
25
  const debug = false;
23
26
  /**
@@ -44,35 +47,60 @@ async function performAiEdit({ prompt, files, llmFunction, apiKey, editFormat =
44
47
  // 4. We parse the output to figure out which files changed
45
48
  const resultString = result.content;
46
49
  let changedFiles;
50
+ const warnings = [];
51
+ let editsParsed = 0;
47
52
  switch (editFormat) {
48
53
  case "whole": {
49
54
  const parsed = (0, llm_code_format_1.parseMarkdownFiles)(resultString, "bold");
55
+ editsParsed = Object.keys(parsed.files).length;
50
56
  changedFiles = (0, fileUtils_1.mergeFileChanges)(files, parsed.files);
51
57
  break;
52
58
  }
53
59
  case "diff": {
54
60
  const diffs = (0, fileUtils_1.parseDiffs)(resultString);
55
- changedFiles = (0, fileUtils_1.applyDiffs)(files, diffs);
61
+ editsParsed = diffs.length;
62
+ const applied = (0, fileUtils_1.applyDiffsSafe)(files, diffs);
63
+ changedFiles = applied.files;
64
+ warnings.push(...applied.warnings);
56
65
  break;
57
66
  }
58
67
  case "diff-fenced": {
59
68
  const diffs = (0, fileUtils_1.parseDiffFenced)(resultString);
60
- changedFiles = (0, fileUtils_1.applyDiffs)(files, diffs);
69
+ editsParsed = diffs.length;
70
+ const applied = (0, fileUtils_1.applyDiffsSafe)(files, diffs);
71
+ changedFiles = applied.files;
72
+ warnings.push(...applied.warnings);
61
73
  break;
62
74
  }
63
75
  case "udiff": {
64
76
  const hunks = (0, fileUtils_1.parseUdiffs)(resultString);
65
- changedFiles = (0, fileUtils_1.applyUdiffs)(files, hunks);
77
+ editsParsed = hunks.length;
78
+ const applied = (0, fileUtils_1.applyUdiffsSafe)(files, hunks);
79
+ changedFiles = applied.files;
80
+ warnings.push(...applied.warnings);
66
81
  break;
67
82
  }
68
83
  case "hybrid": {
69
- changedFiles = (0, fileUtils_1.applyHybridEdits)(resultString, files, (text) => (0, llm_code_format_1.parseMarkdownFiles)(text, "bold").files);
84
+ const diffs = (0, fileUtils_1.parseDiffs)(resultString);
85
+ const wholeFiles = (0, llm_code_format_1.parseMarkdownFiles)(resultString, "bold").files;
86
+ editsParsed = diffs.length + Object.keys(wholeFiles).length;
87
+ const applied = (0, fileUtils_1.applyHybridEditsSafe)(resultString, files, (text) => (0, llm_code_format_1.parseMarkdownFiles)(text, "bold").files);
88
+ changedFiles = applied.files;
89
+ warnings.push(...applied.warnings);
70
90
  break;
71
91
  }
72
92
  default:
73
93
  // This will catch any unhandled or unknown edit formats.
74
94
  throw new Error(`Unknown edit format: ${editFormat}`);
75
95
  }
96
+ // Zero parsed edits is worth surfacing, but only when nothing else was
97
+ // reported — otherwise it just adds noise on top of real failures.
98
+ if (editsParsed === 0 && warnings.length === 0) {
99
+ warnings.push({
100
+ code: "NO_EDITS_PARSED",
101
+ message: "The model returned no applicable edits.",
102
+ });
103
+ }
76
104
  // 6. Retrieve cost metadata for charging the user
77
105
  const openRouterGenerationId = result.generationId || "";
78
106
  let upstreamCostCents = 0;
@@ -98,5 +126,6 @@ async function performAiEdit({ prompt, files, llmFunction, apiKey, editFormat =
98
126
  outputTokens,
99
127
  promptTemplateVersion: prompt_1.PROMPT_TEMPLATE_VERSION,
100
128
  rawResponse: resultString, // Include the raw response
129
+ warnings,
101
130
  };
102
131
  }
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Deterministic anchor matching for edit application.
3
+ *
4
+ * This module intentionally implements only deterministic strategies. It never
5
+ * guesses a location via similarity scoring; if a deterministic match cannot be
6
+ * found, callers skip the edit and report a warning.
7
+ *
8
+ * Strategies, in order:
9
+ * 1. `exact` — the anchor is a literal substring.
10
+ * 2. `line-normalized` — both sides split into lines and compared with
11
+ * trailing whitespace removed. This absorbs CRLF vs LF
12
+ * drift, a missing/extra trailing newline, and
13
+ * trailing-whitespace drift.
14
+ *
15
+ * Leading indentation drift is deliberately NOT handled (deferred).
16
+ */
17
+ export type MatchStrategy = "exact" | "line-normalized";
18
+ export interface AnchorMatch {
19
+ start: number;
20
+ end: number;
21
+ strategy: MatchStrategy;
22
+ /** Number of equally-valid locations found. `> 1` means ambiguous. */
23
+ matchCount: number;
24
+ }
25
+ /**
26
+ * Finds `anchor` within `source` using deterministic strategies only.
27
+ *
28
+ * Returns `null` when no deterministic match exists. When a match exists,
29
+ * `matchCount` reports how many equivalent locations were found so callers can
30
+ * skip ambiguous edits.
31
+ */
32
+ export declare function findAnchor(source: string, anchor: string): AnchorMatch | null;
@@ -0,0 +1,126 @@
1
+ "use strict";
2
+ /**
3
+ * Deterministic anchor matching for edit application.
4
+ *
5
+ * This module intentionally implements only deterministic strategies. It never
6
+ * guesses a location via similarity scoring; if a deterministic match cannot be
7
+ * found, callers skip the edit and report a warning.
8
+ *
9
+ * Strategies, in order:
10
+ * 1. `exact` — the anchor is a literal substring.
11
+ * 2. `line-normalized` — both sides split into lines and compared with
12
+ * trailing whitespace removed. This absorbs CRLF vs LF
13
+ * drift, a missing/extra trailing newline, and
14
+ * trailing-whitespace drift.
15
+ *
16
+ * Leading indentation drift is deliberately NOT handled (deferred).
17
+ */
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ exports.findAnchor = findAnchor;
20
+ /**
21
+ * Splits text into lines while retaining each line's character offsets.
22
+ * A trailing newline produces a final empty line, matching `String.split("\n")`.
23
+ */
24
+ function splitLinesWithOffsets(text) {
25
+ const lines = [];
26
+ if (text.length === 0)
27
+ return lines;
28
+ let start = 0;
29
+ // Loop is bounded: `start` always advances past the newline.
30
+ for (;;) {
31
+ const newlineIndex = text.indexOf("\n", start);
32
+ if (newlineIndex === -1) {
33
+ lines.push({ text: text.slice(start), start, end: text.length });
34
+ break;
35
+ }
36
+ lines.push({
37
+ text: text.slice(start, newlineIndex),
38
+ start,
39
+ end: newlineIndex,
40
+ });
41
+ start = newlineIndex + 1;
42
+ if (start === text.length) {
43
+ lines.push({ text: "", start, end: start });
44
+ break;
45
+ }
46
+ }
47
+ return lines;
48
+ }
49
+ function countOccurrences(haystack, needle) {
50
+ if (needle.length === 0)
51
+ return 0;
52
+ let count = 0;
53
+ let index = haystack.indexOf(needle);
54
+ while (index !== -1) {
55
+ count++;
56
+ index = haystack.indexOf(needle, index + needle.length);
57
+ }
58
+ return count;
59
+ }
60
+ function findLineNormalized(source, anchor) {
61
+ const sourceLines = splitLinesWithOffsets(source);
62
+ const anchorLines = splitLinesWithOffsets(anchor);
63
+ // Trailing/leading blank lines carry no anchoring information.
64
+ while (anchorLines.length > 0 &&
65
+ anchorLines[anchorLines.length - 1].text.trim() === "") {
66
+ anchorLines.pop();
67
+ }
68
+ while (anchorLines.length > 0 && anchorLines[0].text.trim() === "") {
69
+ anchorLines.shift();
70
+ }
71
+ if (anchorLines.length === 0)
72
+ return null;
73
+ if (anchorLines.length > sourceLines.length)
74
+ return null;
75
+ const normalizedAnchor = anchorLines.map((line) => line.text.trimEnd());
76
+ const normalizedSource = sourceLines.map((line) => line.text.trimEnd());
77
+ let matchCount = 0;
78
+ let first = null;
79
+ for (let i = 0; i + normalizedAnchor.length <= sourceLines.length; i++) {
80
+ let isMatch = true;
81
+ for (let j = 0; j < normalizedAnchor.length; j++) {
82
+ if (normalizedSource[i + j] !== normalizedAnchor[j]) {
83
+ isMatch = false;
84
+ break;
85
+ }
86
+ }
87
+ if (isMatch) {
88
+ matchCount++;
89
+ if (first === null) {
90
+ first = {
91
+ start: sourceLines[i].start,
92
+ end: sourceLines[i + normalizedAnchor.length - 1].end,
93
+ };
94
+ }
95
+ }
96
+ }
97
+ if (first === null)
98
+ return null;
99
+ return {
100
+ start: first.start,
101
+ end: first.end,
102
+ strategy: "line-normalized",
103
+ matchCount,
104
+ };
105
+ }
106
+ /**
107
+ * Finds `anchor` within `source` using deterministic strategies only.
108
+ *
109
+ * Returns `null` when no deterministic match exists. When a match exists,
110
+ * `matchCount` reports how many equivalent locations were found so callers can
111
+ * skip ambiguous edits.
112
+ */
113
+ function findAnchor(source, anchor) {
114
+ if (anchor.length === 0 || source.length === 0)
115
+ return null;
116
+ const exactIndex = source.indexOf(anchor);
117
+ if (exactIndex !== -1) {
118
+ return {
119
+ start: exactIndex,
120
+ end: exactIndex + anchor.length,
121
+ strategy: "exact",
122
+ matchCount: countOccurrences(source, anchor),
123
+ };
124
+ }
125
+ return findLineNormalized(source, anchor);
126
+ }
package/dist/prompt.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { EditFormat } from "./types";
2
- export declare const PROMPT_TEMPLATE_VERSION = 1;
2
+ export declare const PROMPT_TEMPLATE_VERSION = 2;
3
3
  export declare const FORMAT_INSTRUCTIONS: Record<EditFormat, string>;
4
4
  /**
5
5
  * Assembles the full prompt by combining task, files context, and formatting instructions
package/dist/prompt.js CHANGED
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.FORMAT_INSTRUCTIONS = exports.PROMPT_TEMPLATE_VERSION = void 0;
4
4
  exports.assembleFullPrompt = assembleFullPrompt;
5
5
  // Versions of the prompt template.
6
- exports.PROMPT_TEMPLATE_VERSION = 1;
6
+ exports.PROMPT_TEMPLATE_VERSION = 2;
7
7
  // Template pieces
8
8
  const TASK = (prompt) => `## Your Task\n\n${prompt}`;
9
9
  const FILES = (filesContext) => `## Original Files\n\n${filesContext}`;
@@ -32,6 +32,7 @@ exports.FORMAT_INSTRUCTIONS = {
32
32
  "// new code\n",
33
33
  ">>>>>>> REPLACE\n",
34
34
  "```\n",
35
+ "Use the exact filename listed in the Original Files section. Copy the SEARCH text verbatim, including whitespace and indentation. Do not invent filenames.",
35
36
  ].join(""),
36
37
  "diff-fenced": [
37
38
  "## Formatting Instructions\n\n",
@@ -44,6 +45,7 @@ exports.FORMAT_INSTRUCTIONS = {
44
45
  "// new code\n",
45
46
  ">>>>>>> REPLACE\n",
46
47
  "```\n",
48
+ "Use the exact filename listed in the Original Files section. Copy the SEARCH text verbatim, including whitespace and indentation. Do not invent filenames.",
47
49
  ].join(""),
48
50
  udiff: [
49
51
  "## Formatting Instructions\n\n",
@@ -55,6 +57,7 @@ exports.FORMAT_INSTRUCTIONS = {
55
57
  "-// line to be removed\n",
56
58
  "+// line to be added\n",
57
59
  "```\n",
60
+ "Use the exact filename listed in the Original Files section. Copy all context lines verbatim, including whitespace. Do not invent filenames; use the whole-file format when creating a new file.",
58
61
  ].join(""),
59
62
  hybrid: [
60
63
  "## Formatting Instructions\n\n",
package/dist/types.d.ts CHANGED
@@ -1,5 +1,42 @@
1
1
  import { VizFiles } from "@vizhub/viz-types";
2
2
  export type EditFormat = "whole" | "diff" | "diff-fenced" | "udiff" | "hybrid";
3
+ /**
4
+ * A parsed edit that can be applied independently to a file.
5
+ * `diff`/`diff-fenced` edits are search/replace pairs; `udiff` edits are
6
+ * original/updated blocks.
7
+ */
8
+ export interface DiffEdit {
9
+ kind: "diff";
10
+ fileName: string;
11
+ search: string;
12
+ replace: string;
13
+ }
14
+ export interface UdiffEdit {
15
+ kind: "udiff";
16
+ fileName: string;
17
+ original: string;
18
+ updated: string;
19
+ }
20
+ export type ParsedEdit = DiffEdit | UdiffEdit;
21
+ /**
22
+ * Reason an edit was skipped. Warnings strictly mean "something went wrong"
23
+ * or "this edit could not be applied" — successful normalized matches are not
24
+ * reported here.
25
+ */
26
+ export type ApplyWarningCode = "FILE_NOT_FOUND" | "SEARCH_NOT_FOUND" | "HUNK_NOT_FOUND" | "AMBIGUOUS_SEARCH" | "AMBIGUOUS_FILE" | "EMPTY_SEARCH" | "NO_EDITS_PARSED";
27
+ export interface ApplyWarning {
28
+ code: ApplyWarningCode;
29
+ fileName?: string;
30
+ message: string;
31
+ }
32
+ /**
33
+ * Result of best-effort edit application. `files` always contains the changes
34
+ * that could be applied safely; `warnings` describes everything skipped.
35
+ */
36
+ export interface ApplyResult {
37
+ files: VizFiles;
38
+ warnings: ApplyWarning[];
39
+ }
3
40
  export type LlmFunction = (prompt: string) => Promise<{
4
41
  content: string;
5
42
  generationId?: string;
@@ -20,4 +57,6 @@ export interface PerformAiEditResult {
20
57
  outputTokens?: number;
21
58
  promptTemplateVersion?: number;
22
59
  rawResponse?: string;
60
+ /** Edits that were skipped because they could not be applied safely. */
61
+ warnings?: ApplyWarning[];
23
62
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "editcodewithai",
3
- "version": "2.5.0",
3
+ "version": "2.6.0",
4
4
  "description": "Edit Code With AI",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",