@code-yeongyu/senpi-agent-core 2026.7.25-2
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 +508 -0
- package/dist/agent-loop.d.ts +31 -0
- package/dist/agent-loop.d.ts.map +1 -0
- package/dist/agent-loop.js +851 -0
- package/dist/agent-loop.js.map +1 -0
- package/dist/agent.d.ts +140 -0
- package/dist/agent.d.ts.map +1 -0
- package/dist/agent.js +464 -0
- package/dist/agent.js.map +1 -0
- package/dist/harness/agent-harness.d.ts +94 -0
- package/dist/harness/agent-harness.d.ts.map +1 -0
- package/dist/harness/agent-harness.js +988 -0
- package/dist/harness/agent-harness.js.map +1 -0
- package/dist/harness/compaction/branch-summarization.d.ts +55 -0
- package/dist/harness/compaction/branch-summarization.d.ts.map +1 -0
- package/dist/harness/compaction/branch-summarization.js +172 -0
- package/dist/harness/compaction/branch-summarization.js.map +1 -0
- package/dist/harness/compaction/compaction.d.ts +107 -0
- package/dist/harness/compaction/compaction.d.ts.map +1 -0
- package/dist/harness/compaction/compaction.js +588 -0
- package/dist/harness/compaction/compaction.js.map +1 -0
- package/dist/harness/compaction/utils.d.ts +36 -0
- package/dist/harness/compaction/utils.d.ts.map +1 -0
- package/dist/harness/compaction/utils.js +136 -0
- package/dist/harness/compaction/utils.js.map +1 -0
- package/dist/harness/env/nodejs.d.ts +45 -0
- package/dist/harness/env/nodejs.d.ts.map +1 -0
- package/dist/harness/env/nodejs.js +620 -0
- package/dist/harness/env/nodejs.js.map +1 -0
- package/dist/harness/messages.d.ts +52 -0
- package/dist/harness/messages.d.ts.map +1 -0
- package/dist/harness/messages.js +102 -0
- package/dist/harness/messages.js.map +1 -0
- package/dist/harness/prompt-templates.d.ts +48 -0
- package/dist/harness/prompt-templates.d.ts.map +1 -0
- package/dist/harness/prompt-templates.js +230 -0
- package/dist/harness/prompt-templates.js.map +1 -0
- package/dist/harness/session/jsonl-repo.d.ts +26 -0
- package/dist/harness/session/jsonl-repo.d.ts.map +1 -0
- package/dist/harness/session/jsonl-repo.js +100 -0
- package/dist/harness/session/jsonl-repo.js.map +1 -0
- package/dist/harness/session/jsonl-storage.d.ts +42 -0
- package/dist/harness/session/jsonl-storage.d.ts.map +1 -0
- package/dist/harness/session/jsonl-storage.js +286 -0
- package/dist/harness/session/jsonl-storage.js.map +1 -0
- package/dist/harness/session/memory-repo.d.ts +18 -0
- package/dist/harness/session/memory-repo.d.ts.map +1 -0
- package/dist/harness/session/memory-repo.js +44 -0
- package/dist/harness/session/memory-repo.js.map +1 -0
- package/dist/harness/session/memory-storage.d.ts +33 -0
- package/dist/harness/session/memory-storage.d.ts.map +1 -0
- package/dist/harness/session/memory-storage.js +163 -0
- package/dist/harness/session/memory-storage.js.map +1 -0
- package/dist/harness/session/repo-utils.d.ts +11 -0
- package/dist/harness/session/repo-utils.d.ts.map +1 -0
- package/dist/harness/session/repo-utils.js +39 -0
- package/dist/harness/session/repo-utils.js.map +1 -0
- package/dist/harness/session/session.d.ts +49 -0
- package/dist/harness/session/session.d.ts.map +1 -0
- package/dist/harness/session/session.js +261 -0
- package/dist/harness/session/session.js.map +1 -0
- package/dist/harness/skills.d.ts +44 -0
- package/dist/harness/skills.d.ts.map +1 -0
- package/dist/harness/skills.js +311 -0
- package/dist/harness/skills.js.map +1 -0
- package/dist/harness/system-prompt.d.ts +3 -0
- package/dist/harness/system-prompt.d.ts.map +1 -0
- package/dist/harness/system-prompt.js +30 -0
- package/dist/harness/system-prompt.js.map +1 -0
- package/dist/harness/tools/bash.d.ts +27 -0
- package/dist/harness/tools/bash.d.ts.map +1 -0
- package/dist/harness/tools/bash.js +132 -0
- package/dist/harness/tools/bash.js.map +1 -0
- package/dist/harness/tools/edit-diff.d.ts +89 -0
- package/dist/harness/tools/edit-diff.d.ts.map +1 -0
- package/dist/harness/tools/edit-diff.js +386 -0
- package/dist/harness/tools/edit-diff.js.map +1 -0
- package/dist/harness/tools/edit.d.ts +19 -0
- package/dist/harness/tools/edit.d.ts.map +1 -0
- package/dist/harness/tools/edit.js +95 -0
- package/dist/harness/tools/edit.js.map +1 -0
- package/dist/harness/tools/file-mutation-queue.d.ts +4 -0
- package/dist/harness/tools/file-mutation-queue.d.ts.map +1 -0
- package/dist/harness/tools/file-mutation-queue.js +46 -0
- package/dist/harness/tools/file-mutation-queue.js.map +1 -0
- package/dist/harness/tools/image.d.ts +3 -0
- package/dist/harness/tools/image.d.ts.map +1 -0
- package/dist/harness/tools/image.js +106 -0
- package/dist/harness/tools/image.js.map +1 -0
- package/dist/harness/tools/index.d.ts +6 -0
- package/dist/harness/tools/index.d.ts.map +1 -0
- package/dist/harness/tools/index.js +5 -0
- package/dist/harness/tools/index.js.map +1 -0
- package/dist/harness/tools/path-utils.d.ts +4 -0
- package/dist/harness/tools/path-utils.d.ts.map +1 -0
- package/dist/harness/tools/path-utils.js +26 -0
- package/dist/harness/tools/path-utils.js.map +1 -0
- package/dist/harness/tools/read.d.ts +34 -0
- package/dist/harness/tools/read.d.ts.map +1 -0
- package/dist/harness/tools/read.js +110 -0
- package/dist/harness/tools/read.js.map +1 -0
- package/dist/harness/tools/tool-context.d.ts +6 -0
- package/dist/harness/tools/tool-context.d.ts.map +1 -0
- package/dist/harness/tools/tool-context.js +2 -0
- package/dist/harness/tools/tool-context.js.map +1 -0
- package/dist/harness/tools/write.d.ts +11 -0
- package/dist/harness/tools/write.d.ts.map +1 -0
- package/dist/harness/tools/write.js +31 -0
- package/dist/harness/tools/write.js.map +1 -0
- package/dist/harness/types.d.ts +682 -0
- package/dist/harness/types.d.ts.map +1 -0
- package/dist/harness/types.js +83 -0
- package/dist/harness/types.js.map +1 -0
- package/dist/harness/utils/shell-output.d.ts +22 -0
- package/dist/harness/utils/shell-output.d.ts.map +1 -0
- package/dist/harness/utils/shell-output.js +181 -0
- package/dist/harness/utils/shell-output.js.map +1 -0
- package/dist/harness/utils/truncate.d.ts +70 -0
- package/dist/harness/utils/truncate.d.ts.map +1 -0
- package/dist/harness/utils/truncate.js +296 -0
- package/dist/harness/utils/truncate.js.map +1 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +30 -0
- package/dist/index.js.map +1 -0
- package/dist/node.d.ts +3 -0
- package/dist/node.d.ts.map +1 -0
- package/dist/node.js +3 -0
- package/dist/node.js.map +1 -0
- package/dist/proxy.d.ts +71 -0
- package/dist/proxy.d.ts.map +1 -0
- package/dist/proxy.js +280 -0
- package/dist/proxy.js.map +1 -0
- package/dist/stream-fn.d.ts +10 -0
- package/dist/stream-fn.d.ts.map +1 -0
- package/dist/stream-fn.js +17 -0
- package/dist/stream-fn.js.map +1 -0
- package/dist/types.d.ts +415 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +61 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bash.js","sourceRoot":"","sources":["../../../src/harness/tools/bash.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,IAAI,EAAE,MAAM,SAAS,CAAC;AAE5C,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,uBAAuB,EAA6B,MAAM,0BAA0B,CAAC;AAC9F,OAAO,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,UAAU,EAAyB,MAAM,sBAAsB,CAAC;AAG/G,MAAM,mBAAmB,GAAG,aAAa,GAAG,IAAI,CAAC;AACjD,MAAM,uBAAuB,GAAG,GAAG,CAAC;AAEpC,MAAM,UAAU,GAAG,IAAI,CAAC,MAAM,CAAC;IAC9B,OAAO,EAAE,IAAI,CAAC,MAAM,CAAC,EAAE,WAAW,EAAE,yBAAyB,EAAE,CAAC;IAChE,OAAO,EAAE,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,WAAW,EAAE,mDAAmD,EAAE,CAAC,CAAC;CACzG,CAAC,CAAC;AA2BH,SAAS,eAAe,CAAC,OAA2B;IACnD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,EAAE,IAAI,EAAE,UAAU,IAAI,aAAa,EAAE,CAAC,EAAE,OAAO,EAAE,CAAC;YACpF,CAAC;oBAAS,CAAC;gBACV,gBAAgB,EAAE,CAAC;YACpB,CAAC;QACF,CAAC;KACD,CAAC;AACH,CAAC","sourcesContent":["import { type Static, Type } from \"typebox\";\nimport type { AgentHarnessTool } from \"../types.ts\";\nimport { getOrThrow } from \"../types.ts\";\nimport { executeShellWithCapture, type ShellCaptureProgress } from \"../utils/shell-output.ts\";\nimport { DEFAULT_MAX_BYTES, DEFAULT_MAX_LINES, formatSize, type TruncationResult } from \"../utils/truncate.ts\";\nimport type { ExecutionToolContext } from \"./tool-context.ts\";\n\nconst MAX_TIMEOUT_SECONDS = 2_147_483_647 / 1000;\nconst BASH_UPDATE_THROTTLE_MS = 100;\n\nconst bashSchema = Type.Object({\n\tcommand: Type.String({ description: \"Bash command to execute\" }),\n\ttimeout: Type.Optional(Type.Number({ description: \"Timeout in seconds (optional, no default timeout)\" })),\n});\n\nexport type BashToolInput = Static<typeof bashSchema>;\n\nexport interface BashToolDetails {\n\ttruncation?: TruncationResult;\n\tfullOutputPath?: string;\n}\n\nexport interface BashExecution {\n\tcommand: string;\n\tcwd: string;\n\tenv: Record<string, string>;\n\tinheritEnv: boolean;\n}\n\nexport type BashPrepare<TContext extends ExecutionToolContext = ExecutionToolContext> = (\n\texecution: BashExecution,\n\tcontext: TContext,\n\tsignal?: AbortSignal,\n) => void | Promise<void>;\n\nexport interface BashToolOptions<TContext extends ExecutionToolContext = ExecutionToolContext> {\n\tcommandPrefix?: string;\n\tprepare?: BashPrepare<TContext>;\n}\n\nfunction validateTimeout(timeout: number | undefined): void {\n\tif (timeout === undefined) return;\n\tif (!Number.isFinite(timeout) || timeout <= 0) {\n\t\tthrow new Error(\"Invalid timeout: must be a finite number of seconds\");\n\t}\n\tif (timeout > MAX_TIMEOUT_SECONDS) {\n\t\tthrow new Error(`Invalid timeout: maximum is ${MAX_TIMEOUT_SECONDS} seconds`);\n\t}\n}\n\nexport function createBashTool<TContext extends ExecutionToolContext = ExecutionToolContext>(\n\toptions?: BashToolOptions<TContext>,\n): AgentHarnessTool<TContext, typeof bashSchema, BashToolDetails | undefined> {\n\treturn {\n\t\tname: \"bash\",\n\t\tlabel: \"bash\",\n\t\tdescription: `Execute a bash command in the current working directory. Returns stdout and stderr. Output is truncated to last ${DEFAULT_MAX_LINES} lines or ${DEFAULT_MAX_BYTES / 1024}KB (whichever is hit first). If truncated, full output is saved to a temp file. Optionally provide a timeout in seconds.`,\n\t\tparameters: bashSchema,\n\t\tasync execute(_toolCallId, { command, timeout }, signal, onUpdate, context) {\n\t\t\tvalidateTimeout(timeout);\n\t\t\tconst { env } = context;\n\t\t\tconst execution: BashExecution = {\n\t\t\t\tcommand: options?.commandPrefix ? `${options.commandPrefix}\\n${command}` : command,\n\t\t\t\tcwd: env.cwd,\n\t\t\t\tenv: {},\n\t\t\t\tinheritEnv: true,\n\t\t\t};\n\t\t\tawait options?.prepare?.(execution, context, signal);\n\t\t\tlet getLatestProgress: (() => ShellCaptureProgress) | undefined;\n\t\t\tlet updateTimer: ReturnType<typeof setTimeout> | undefined;\n\t\t\tlet updateDirty = false;\n\t\t\tlet lastUpdateAt = 0;\n\n\t\t\tconst emitOutputUpdate = (): void => {\n\t\t\t\tif (!onUpdate || !updateDirty || !getLatestProgress) return;\n\t\t\t\tupdateDirty = false;\n\t\t\t\tlastUpdateAt = Date.now();\n\t\t\t\tconst progress = getLatestProgress();\n\t\t\t\tonUpdate({\n\t\t\t\t\tcontent: [{ type: \"text\", text: progress.output }],\n\t\t\t\t\tdetails: {\n\t\t\t\t\t\ttruncation: progress.truncation.truncated ? progress.truncation : undefined,\n\t\t\t\t\t\tfullOutputPath: progress.fullOutputPath,\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t};\n\t\t\tconst clearUpdateTimer = (): void => {\n\t\t\t\tif (!updateTimer) return;\n\t\t\t\tclearTimeout(updateTimer);\n\t\t\t\tupdateTimer = undefined;\n\t\t\t};\n\t\t\tconst scheduleOutputUpdate = (): void => {\n\t\t\t\tif (!onUpdate) return;\n\t\t\t\tupdateDirty = true;\n\t\t\t\tconst delay = BASH_UPDATE_THROTTLE_MS - (Date.now() - lastUpdateAt);\n\t\t\t\tif (delay <= 0) {\n\t\t\t\t\tclearUpdateTimer();\n\t\t\t\t\temitOutputUpdate();\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tupdateTimer ??= setTimeout(() => {\n\t\t\t\t\tupdateTimer = undefined;\n\t\t\t\t\temitOutputUpdate();\n\t\t\t\t}, delay);\n\t\t\t};\n\n\t\t\tonUpdate?.({ content: [], details: undefined });\n\t\t\ttry {\n\t\t\t\tconst capture = getOrThrow(\n\t\t\t\t\tawait executeShellWithCapture(env, execution.command, {\n\t\t\t\t\t\tcwd: execution.cwd,\n\t\t\t\t\t\tenv: execution.env,\n\t\t\t\t\t\tinheritEnv: execution.inheritEnv,\n\t\t\t\t\t\ttimeout,\n\t\t\t\t\t\tabortSignal: signal,\n\t\t\t\t\t\treturnExecutionErrors: true,\n\t\t\t\t\t\tonChunk: (_chunk, getProgress) => {\n\t\t\t\t\t\t\tgetLatestProgress = getProgress;\n\t\t\t\t\t\t\tscheduleOutputUpdate();\n\t\t\t\t\t\t},\n\t\t\t\t\t}),\n\t\t\t\t);\n\t\t\t\tclearUpdateTimer();\n\t\t\t\tgetLatestProgress = () => capture;\n\t\t\t\tupdateDirty = true;\n\t\t\t\temitOutputUpdate();\n\n\t\t\t\tlet outputText = capture.output;\n\t\t\t\tlet details: BashToolDetails | undefined;\n\t\t\t\tif (capture.truncation.truncated) {\n\t\t\t\t\tdetails = { truncation: capture.truncation, fullOutputPath: capture.fullOutputPath };\n\t\t\t\t\tconst startLine = capture.truncation.totalLines - capture.truncation.outputLines + 1;\n\t\t\t\t\tconst endLine = capture.truncation.totalLines;\n\t\t\t\t\tif (capture.truncation.lastLinePartial) {\n\t\t\t\t\t\tconst lastLineSize = formatSize(capture.lastLineBytes);\n\t\t\t\t\t\toutputText += `\\n\\n[Showing last ${formatSize(capture.truncation.outputBytes)} of line ${endLine} (line is ${lastLineSize}). Full output: ${capture.fullOutputPath}]`;\n\t\t\t\t\t} else if (capture.truncation.truncatedBy === \"lines\") {\n\t\t\t\t\t\toutputText += `\\n\\n[Showing lines ${startLine}-${endLine} of ${capture.truncation.totalLines}. Full output: ${capture.fullOutputPath}]`;\n\t\t\t\t\t} else {\n\t\t\t\t\t\toutputText += `\\n\\n[Showing lines ${startLine}-${endLine} of ${capture.truncation.totalLines} (${formatSize(DEFAULT_MAX_BYTES)} limit). Full output: ${capture.fullOutputPath}]`;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tconst appendStatus = (status: string): string => `${outputText ? `${outputText}\\n\\n` : \"\"}${status}`;\n\t\t\t\tif (capture.cancelled) throw new Error(appendStatus(\"Command aborted\"));\n\t\t\t\tif (capture.executionError?.code === \"timeout\") {\n\t\t\t\t\tthrow new Error(appendStatus(`Command timed out after ${timeout} seconds`), {\n\t\t\t\t\t\tcause: capture.executionError,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t\tif (capture.executionError) throw capture.executionError;\n\t\t\t\tif (capture.exitCode !== 0 && capture.exitCode !== undefined) {\n\t\t\t\t\tthrow new Error(appendStatus(`Command exited with code ${capture.exitCode}`));\n\t\t\t\t}\n\t\t\t\treturn { content: [{ type: \"text\", text: outputText || \"(no output)\" }], details };\n\t\t\t} finally {\n\t\t\t\tclearUpdateTimer();\n\t\t\t}\n\t\t},\n\t};\n}\n"]}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared diff computation utilities for the edit and similar tools.
|
|
3
|
+
*/
|
|
4
|
+
export declare function detectLineEnding(content: string): "\r\n" | "\n";
|
|
5
|
+
export declare function normalizeToLF(text: string): string;
|
|
6
|
+
export declare function restoreLineEndings(text: string, ending: "\r\n" | "\n"): string;
|
|
7
|
+
/**
|
|
8
|
+
* Normalize text for fuzzy matching. Applies progressive transformations:
|
|
9
|
+
* - Strip trailing whitespace from each line
|
|
10
|
+
* - Normalize smart quotes to ASCII equivalents
|
|
11
|
+
* - Normalize Unicode dashes/hyphens to ASCII hyphen
|
|
12
|
+
* - Normalize special Unicode spaces to regular space
|
|
13
|
+
*/
|
|
14
|
+
export declare function normalizeForFuzzyMatch(text: string): string;
|
|
15
|
+
interface MatchedEdit {
|
|
16
|
+
editIndex: number;
|
|
17
|
+
matchIndex: number;
|
|
18
|
+
matchLength: number;
|
|
19
|
+
newText: string;
|
|
20
|
+
}
|
|
21
|
+
type TextReplacement = Pick<MatchedEdit, "matchIndex" | "matchLength" | "newText">;
|
|
22
|
+
/**
|
|
23
|
+
* Apply replacements matched against `baseContent` to `originalContent` while
|
|
24
|
+
* preserving unchanged line blocks from the original.
|
|
25
|
+
*
|
|
26
|
+
* This is useful when `baseContent` is a normalized view of the original. Each
|
|
27
|
+
* replacement is widened to the lines it actually touches, those touched lines
|
|
28
|
+
* are rewritten from the normalized base, and all other lines are copied back
|
|
29
|
+
* from `originalContent`. The actual replacement ranges drive preservation so
|
|
30
|
+
* duplicate normalized lines cannot be aligned to the wrong occurrence.
|
|
31
|
+
*/
|
|
32
|
+
export declare function applyReplacementsPreservingUnchangedLines(originalContent: string, baseContent: string, replacements: TextReplacement[]): string;
|
|
33
|
+
export interface FuzzyMatchResult {
|
|
34
|
+
/** Whether a match was found */
|
|
35
|
+
found: boolean;
|
|
36
|
+
/** The index where the match starts (in the content that should be used for replacement) */
|
|
37
|
+
index: number;
|
|
38
|
+
/** Length of the matched text */
|
|
39
|
+
matchLength: number;
|
|
40
|
+
/** Whether fuzzy matching was used (false = exact match) */
|
|
41
|
+
usedFuzzyMatch: boolean;
|
|
42
|
+
/**
|
|
43
|
+
* The content to use for replacement operations.
|
|
44
|
+
* When exact match: original content. When fuzzy match: normalized content.
|
|
45
|
+
*/
|
|
46
|
+
contentForReplacement: string;
|
|
47
|
+
}
|
|
48
|
+
export interface Edit {
|
|
49
|
+
oldText: string;
|
|
50
|
+
newText: string;
|
|
51
|
+
}
|
|
52
|
+
export interface AppliedEditsResult {
|
|
53
|
+
baseContent: string;
|
|
54
|
+
newContent: string;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Find oldText in content, trying exact match first, then fuzzy match.
|
|
58
|
+
* When fuzzy matching is used, the returned contentForReplacement is the
|
|
59
|
+
* fuzzy-normalized version of the content (trailing whitespace stripped,
|
|
60
|
+
* Unicode quotes/dashes normalized to ASCII).
|
|
61
|
+
*/
|
|
62
|
+
export declare function fuzzyFindText(content: string, oldText: string): FuzzyMatchResult;
|
|
63
|
+
/** Strip UTF-8 BOM if present, return both the BOM (if any) and the text without it */
|
|
64
|
+
export declare function stripBom(content: string): {
|
|
65
|
+
bom: string;
|
|
66
|
+
text: string;
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* Apply one or more exact-text replacements to LF-normalized content.
|
|
70
|
+
*
|
|
71
|
+
* All edits are matched against the same original content. Replacements are
|
|
72
|
+
* then applied in reverse order so offsets remain stable. If any edit needs
|
|
73
|
+
* fuzzy matching, the operation runs in fuzzy-normalized content space and then
|
|
74
|
+
* overlays those line-level changes onto the original content so unchanged line
|
|
75
|
+
* blocks keep their original bytes.
|
|
76
|
+
*/
|
|
77
|
+
export declare function applyEditsToNormalizedContent(normalizedContent: string, edits: Edit[], path: string): AppliedEditsResult;
|
|
78
|
+
/** Generate a standard unified patch. */
|
|
79
|
+
export declare function generateUnifiedPatch(path: string, oldContent: string, newContent: string, contextLines?: number): string;
|
|
80
|
+
/**
|
|
81
|
+
* Generate a display-oriented diff string with line numbers and context.
|
|
82
|
+
* Returns both the diff string and the first changed line number (in the new file).
|
|
83
|
+
*/
|
|
84
|
+
export declare function generateDiffString(oldContent: string, newContent: string, contextLines?: number): {
|
|
85
|
+
diff: string;
|
|
86
|
+
firstChangedLine: number | undefined;
|
|
87
|
+
};
|
|
88
|
+
export {};
|
|
89
|
+
//# sourceMappingURL=edit-diff.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"edit-diff.d.ts","sourceRoot":"","sources":["../../../src/harness/tools/edit-diff.ts"],"names":[],"mappings":"AAAA;;GAEG;AAIH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAM/D;AAED,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAElD;AAED,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI,GAAG,MAAM,CAE9E;AAED;;;;;;GAMG;AACH,wBAAgB,sBAAsB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAqB3D;AAWD,UAAU,WAAW;IACpB,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,EAAE,MAAM,CAAC;IACpB,OAAO,EAAE,MAAM,CAAC;CAChB;AAED,KAAK,eAAe,GAAG,IAAI,CAAC,WAAW,EAAE,YAAY,GAAG,aAAa,GAAG,SAAS,CAAC,CAAC;AAiDnF;;;;;;;;;GASG;AACH,wBAAgB,yCAAyC,CACxD,eAAe,EAAE,MAAM,EACvB,WAAW,EAAE,MAAM,EACnB,YAAY,EAAE,eAAe,EAAE,GAC7B,MAAM,CAqCR;AAED,MAAM,WAAW,gBAAgB;IAChC,gCAAgC;IAChC,KAAK,EAAE,OAAO,CAAC;IACf,4FAA4F;IAC5F,KAAK,EAAE,MAAM,CAAC;IACd,iCAAiC;IACjC,WAAW,EAAE,MAAM,CAAC;IACpB,4DAA4D;IAC5D,cAAc,EAAE,OAAO,CAAC;IACxB;;;OAGG;IACH,qBAAqB,EAAE,MAAM,CAAC;CAC9B;AAED,MAAM,WAAW,IAAI;IACpB,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,kBAAkB;IAClC,WAAW,EAAE,MAAM,CAAC;IACpB,UAAU,EAAE,MAAM,CAAC;CACnB;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,gBAAgB,CAsChF;AAED,uFAAuF;AACvF,wBAAgB,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAEvE;AA8CD;;;;;;;;GAQG;AACH,wBAAgB,6BAA6B,CAC5C,iBAAiB,EAAE,MAAM,EACzB,KAAK,EAAE,IAAI,EAAE,EACb,IAAI,EAAE,MAAM,GACV,kBAAkB,CA0DpB;AAED,yCAAyC;AACzC,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,YAAY,SAAI,GAAG,MAAM,CAKnH;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CACjC,UAAU,EAAE,MAAM,EAClB,UAAU,EAAE,MAAM,EAClB,YAAY,SAAI,GACd;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,gBAAgB,EAAE,MAAM,GAAG,SAAS,CAAA;CAAE,CAuHxD"}
|
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared diff computation utilities for the edit and similar tools.
|
|
3
|
+
*/
|
|
4
|
+
import * as Diff from "diff";
|
|
5
|
+
export function detectLineEnding(content) {
|
|
6
|
+
const crlfIdx = content.indexOf("\r\n");
|
|
7
|
+
const lfIdx = content.indexOf("\n");
|
|
8
|
+
if (lfIdx === -1)
|
|
9
|
+
return "\n";
|
|
10
|
+
if (crlfIdx === -1)
|
|
11
|
+
return "\n";
|
|
12
|
+
return crlfIdx < lfIdx ? "\r\n" : "\n";
|
|
13
|
+
}
|
|
14
|
+
export function normalizeToLF(text) {
|
|
15
|
+
return text.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
|
|
16
|
+
}
|
|
17
|
+
export function restoreLineEndings(text, ending) {
|
|
18
|
+
return ending === "\r\n" ? text.replace(/\n/g, "\r\n") : text;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Normalize text for fuzzy matching. Applies progressive transformations:
|
|
22
|
+
* - Strip trailing whitespace from each line
|
|
23
|
+
* - Normalize smart quotes to ASCII equivalents
|
|
24
|
+
* - Normalize Unicode dashes/hyphens to ASCII hyphen
|
|
25
|
+
* - Normalize special Unicode spaces to regular space
|
|
26
|
+
*/
|
|
27
|
+
export function normalizeForFuzzyMatch(text) {
|
|
28
|
+
return (text
|
|
29
|
+
.normalize("NFKC")
|
|
30
|
+
// Strip trailing whitespace per line
|
|
31
|
+
.split("\n")
|
|
32
|
+
.map((line) => line.trimEnd())
|
|
33
|
+
.join("\n")
|
|
34
|
+
// Smart single quotes → '
|
|
35
|
+
.replace(/[\u2018\u2019\u201A\u201B]/g, "'")
|
|
36
|
+
// Smart double quotes → "
|
|
37
|
+
.replace(/[\u201C\u201D\u201E\u201F]/g, '"')
|
|
38
|
+
// Various dashes/hyphens → -
|
|
39
|
+
// U+2010 hyphen, U+2011 non-breaking hyphen, U+2012 figure dash,
|
|
40
|
+
// U+2013 en-dash, U+2014 em-dash, U+2015 horizontal bar, U+2212 minus
|
|
41
|
+
.replace(/[\u2010\u2011\u2012\u2013\u2014\u2015\u2212]/g, "-")
|
|
42
|
+
// Special spaces → regular space
|
|
43
|
+
// U+00A0 NBSP, U+2002-U+200A various spaces, U+202F narrow NBSP,
|
|
44
|
+
// U+205F medium math space, U+3000 ideographic space
|
|
45
|
+
.replace(/[\u00A0\u2002-\u200A\u202F\u205F\u3000]/g, " "));
|
|
46
|
+
}
|
|
47
|
+
function splitLinesWithEndings(content) {
|
|
48
|
+
return content.match(/[^\n]*\n|[^\n]+/g) ?? [];
|
|
49
|
+
}
|
|
50
|
+
function getLineSpans(content) {
|
|
51
|
+
let offset = 0;
|
|
52
|
+
return splitLinesWithEndings(content).map((line) => {
|
|
53
|
+
const span = { start: offset, end: offset + line.length };
|
|
54
|
+
offset = span.end;
|
|
55
|
+
return span;
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
function getReplacementLineRange(lines, replacement) {
|
|
59
|
+
const replacementStart = replacement.matchIndex;
|
|
60
|
+
const replacementEnd = replacement.matchIndex + replacement.matchLength;
|
|
61
|
+
let startLine = -1;
|
|
62
|
+
for (let i = 0; i < lines.length; i++) {
|
|
63
|
+
const line = lines[i];
|
|
64
|
+
if (replacementStart >= line.start && replacementStart < line.end) {
|
|
65
|
+
startLine = i;
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
if (startLine === -1) {
|
|
70
|
+
throw new Error("Replacement range is outside the base content.");
|
|
71
|
+
}
|
|
72
|
+
let endLine = startLine;
|
|
73
|
+
while (endLine < lines.length && lines[endLine].end < replacementEnd) {
|
|
74
|
+
endLine++;
|
|
75
|
+
}
|
|
76
|
+
if (endLine >= lines.length) {
|
|
77
|
+
throw new Error("Replacement range is outside the base content.");
|
|
78
|
+
}
|
|
79
|
+
return { startLine, endLine: endLine + 1 };
|
|
80
|
+
}
|
|
81
|
+
function applyReplacements(content, replacements, offset = 0) {
|
|
82
|
+
let result = content;
|
|
83
|
+
for (let i = replacements.length - 1; i >= 0; i--) {
|
|
84
|
+
const replacement = replacements[i];
|
|
85
|
+
const matchIndex = replacement.matchIndex - offset;
|
|
86
|
+
result =
|
|
87
|
+
result.substring(0, matchIndex) + replacement.newText + result.substring(matchIndex + replacement.matchLength);
|
|
88
|
+
}
|
|
89
|
+
return result;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Apply replacements matched against `baseContent` to `originalContent` while
|
|
93
|
+
* preserving unchanged line blocks from the original.
|
|
94
|
+
*
|
|
95
|
+
* This is useful when `baseContent` is a normalized view of the original. Each
|
|
96
|
+
* replacement is widened to the lines it actually touches, those touched lines
|
|
97
|
+
* are rewritten from the normalized base, and all other lines are copied back
|
|
98
|
+
* from `originalContent`. The actual replacement ranges drive preservation so
|
|
99
|
+
* duplicate normalized lines cannot be aligned to the wrong occurrence.
|
|
100
|
+
*/
|
|
101
|
+
export function applyReplacementsPreservingUnchangedLines(originalContent, baseContent, replacements) {
|
|
102
|
+
const originalLines = splitLinesWithEndings(originalContent);
|
|
103
|
+
const baseLines = getLineSpans(baseContent);
|
|
104
|
+
if (originalLines.length !== baseLines.length) {
|
|
105
|
+
throw new Error("Cannot preserve unchanged lines because the base content has a different line count.");
|
|
106
|
+
}
|
|
107
|
+
const groups = [];
|
|
108
|
+
const sortedReplacements = [...replacements].sort((a, b) => a.matchIndex - b.matchIndex);
|
|
109
|
+
for (const replacement of sortedReplacements) {
|
|
110
|
+
const range = getReplacementLineRange(baseLines, replacement);
|
|
111
|
+
const current = groups[groups.length - 1];
|
|
112
|
+
if (current && range.startLine < current.endLine) {
|
|
113
|
+
current.endLine = Math.max(current.endLine, range.endLine);
|
|
114
|
+
current.replacements.push(replacement);
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
groups.push({ ...range, replacements: [replacement] });
|
|
118
|
+
}
|
|
119
|
+
let originalLineIndex = 0;
|
|
120
|
+
let result = "";
|
|
121
|
+
for (const group of groups) {
|
|
122
|
+
result += originalLines.slice(originalLineIndex, group.startLine).join("");
|
|
123
|
+
const groupStartOffset = baseLines[group.startLine].start;
|
|
124
|
+
const groupEndOffset = baseLines[group.endLine - 1].end;
|
|
125
|
+
result += applyReplacements(baseContent.slice(groupStartOffset, groupEndOffset), group.replacements, groupStartOffset);
|
|
126
|
+
originalLineIndex = group.endLine;
|
|
127
|
+
}
|
|
128
|
+
result += originalLines.slice(originalLineIndex).join("");
|
|
129
|
+
return result;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Find oldText in content, trying exact match first, then fuzzy match.
|
|
133
|
+
* When fuzzy matching is used, the returned contentForReplacement is the
|
|
134
|
+
* fuzzy-normalized version of the content (trailing whitespace stripped,
|
|
135
|
+
* Unicode quotes/dashes normalized to ASCII).
|
|
136
|
+
*/
|
|
137
|
+
export function fuzzyFindText(content, oldText) {
|
|
138
|
+
// Try exact match first
|
|
139
|
+
const exactIndex = content.indexOf(oldText);
|
|
140
|
+
if (exactIndex !== -1) {
|
|
141
|
+
return {
|
|
142
|
+
found: true,
|
|
143
|
+
index: exactIndex,
|
|
144
|
+
matchLength: oldText.length,
|
|
145
|
+
usedFuzzyMatch: false,
|
|
146
|
+
contentForReplacement: content,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
// Try fuzzy match - work entirely in normalized space
|
|
150
|
+
const fuzzyContent = normalizeForFuzzyMatch(content);
|
|
151
|
+
const fuzzyOldText = normalizeForFuzzyMatch(oldText);
|
|
152
|
+
const fuzzyIndex = fuzzyContent.indexOf(fuzzyOldText);
|
|
153
|
+
if (fuzzyIndex === -1) {
|
|
154
|
+
return {
|
|
155
|
+
found: false,
|
|
156
|
+
index: -1,
|
|
157
|
+
matchLength: 0,
|
|
158
|
+
usedFuzzyMatch: false,
|
|
159
|
+
contentForReplacement: content,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
// When fuzzy matching, return offsets in normalized space. Callers can use
|
|
163
|
+
// the normalized content to compute replacements, then decide how much of
|
|
164
|
+
// that normalized output should be written back.
|
|
165
|
+
return {
|
|
166
|
+
found: true,
|
|
167
|
+
index: fuzzyIndex,
|
|
168
|
+
matchLength: fuzzyOldText.length,
|
|
169
|
+
usedFuzzyMatch: true,
|
|
170
|
+
contentForReplacement: fuzzyContent,
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
/** Strip UTF-8 BOM if present, return both the BOM (if any) and the text without it */
|
|
174
|
+
export function stripBom(content) {
|
|
175
|
+
return content.startsWith("\uFEFF") ? { bom: "\uFEFF", text: content.slice(1) } : { bom: "", text: content };
|
|
176
|
+
}
|
|
177
|
+
function countOccurrences(content, oldText) {
|
|
178
|
+
const fuzzyContent = normalizeForFuzzyMatch(content);
|
|
179
|
+
const fuzzyOldText = normalizeForFuzzyMatch(oldText);
|
|
180
|
+
return fuzzyContent.split(fuzzyOldText).length - 1;
|
|
181
|
+
}
|
|
182
|
+
function getNotFoundError(path, editIndex, totalEdits) {
|
|
183
|
+
if (totalEdits === 1) {
|
|
184
|
+
return new Error(`Could not find the exact text in ${path}. The old text must match exactly including all whitespace and newlines.`);
|
|
185
|
+
}
|
|
186
|
+
return new Error(`Could not find edits[${editIndex}] in ${path}. The oldText must match exactly including all whitespace and newlines.`);
|
|
187
|
+
}
|
|
188
|
+
function getDuplicateError(path, editIndex, totalEdits, occurrences) {
|
|
189
|
+
if (totalEdits === 1) {
|
|
190
|
+
return new Error(`Found ${occurrences} occurrences of the text in ${path}. The text must be unique. Please provide more context to make it unique.`);
|
|
191
|
+
}
|
|
192
|
+
return new Error(`Found ${occurrences} occurrences of edits[${editIndex}] in ${path}. Each oldText must be unique. Please provide more context to make it unique.`);
|
|
193
|
+
}
|
|
194
|
+
function getEmptyOldTextError(path, editIndex, totalEdits) {
|
|
195
|
+
if (totalEdits === 1) {
|
|
196
|
+
return new Error(`oldText must not be empty in ${path}.`);
|
|
197
|
+
}
|
|
198
|
+
return new Error(`edits[${editIndex}].oldText must not be empty in ${path}.`);
|
|
199
|
+
}
|
|
200
|
+
function getNoChangeError(path, totalEdits) {
|
|
201
|
+
if (totalEdits === 1) {
|
|
202
|
+
return new Error(`No changes made to ${path}. The replacement produced identical content. This might indicate an issue with special characters or the text not existing as expected.`);
|
|
203
|
+
}
|
|
204
|
+
return new Error(`No changes made to ${path}. The replacements produced identical content.`);
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Apply one or more exact-text replacements to LF-normalized content.
|
|
208
|
+
*
|
|
209
|
+
* All edits are matched against the same original content. Replacements are
|
|
210
|
+
* then applied in reverse order so offsets remain stable. If any edit needs
|
|
211
|
+
* fuzzy matching, the operation runs in fuzzy-normalized content space and then
|
|
212
|
+
* overlays those line-level changes onto the original content so unchanged line
|
|
213
|
+
* blocks keep their original bytes.
|
|
214
|
+
*/
|
|
215
|
+
export function applyEditsToNormalizedContent(normalizedContent, edits, path) {
|
|
216
|
+
const normalizedEdits = edits.map((edit) => ({
|
|
217
|
+
oldText: normalizeToLF(edit.oldText),
|
|
218
|
+
newText: normalizeToLF(edit.newText),
|
|
219
|
+
}));
|
|
220
|
+
for (let i = 0; i < normalizedEdits.length; i++) {
|
|
221
|
+
if (normalizedEdits[i].oldText.length === 0) {
|
|
222
|
+
throw getEmptyOldTextError(path, i, normalizedEdits.length);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
const initialMatches = normalizedEdits.map((edit) => fuzzyFindText(normalizedContent, edit.oldText));
|
|
226
|
+
const usedFuzzyMatch = initialMatches.some((match) => match.usedFuzzyMatch);
|
|
227
|
+
const replacementBaseContent = usedFuzzyMatch ? normalizeForFuzzyMatch(normalizedContent) : normalizedContent;
|
|
228
|
+
const matchedEdits = [];
|
|
229
|
+
for (let i = 0; i < normalizedEdits.length; i++) {
|
|
230
|
+
const edit = normalizedEdits[i];
|
|
231
|
+
const matchResult = fuzzyFindText(replacementBaseContent, edit.oldText);
|
|
232
|
+
if (!matchResult.found) {
|
|
233
|
+
throw getNotFoundError(path, i, normalizedEdits.length);
|
|
234
|
+
}
|
|
235
|
+
const occurrences = countOccurrences(replacementBaseContent, edit.oldText);
|
|
236
|
+
if (occurrences > 1) {
|
|
237
|
+
throw getDuplicateError(path, i, normalizedEdits.length, occurrences);
|
|
238
|
+
}
|
|
239
|
+
matchedEdits.push({
|
|
240
|
+
editIndex: i,
|
|
241
|
+
matchIndex: matchResult.index,
|
|
242
|
+
matchLength: matchResult.matchLength,
|
|
243
|
+
newText: edit.newText,
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
matchedEdits.sort((a, b) => a.matchIndex - b.matchIndex);
|
|
247
|
+
for (let i = 1; i < matchedEdits.length; i++) {
|
|
248
|
+
const previous = matchedEdits[i - 1];
|
|
249
|
+
const current = matchedEdits[i];
|
|
250
|
+
if (previous.matchIndex + previous.matchLength > current.matchIndex) {
|
|
251
|
+
throw new Error(`edits[${previous.editIndex}] and edits[${current.editIndex}] overlap in ${path}. Merge them into one edit or target disjoint regions.`);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
const baseContent = normalizedContent;
|
|
255
|
+
const newContent = usedFuzzyMatch
|
|
256
|
+
? applyReplacementsPreservingUnchangedLines(normalizedContent, replacementBaseContent, matchedEdits)
|
|
257
|
+
: applyReplacements(replacementBaseContent, matchedEdits);
|
|
258
|
+
if (baseContent === newContent) {
|
|
259
|
+
throw getNoChangeError(path, normalizedEdits.length);
|
|
260
|
+
}
|
|
261
|
+
return { baseContent, newContent };
|
|
262
|
+
}
|
|
263
|
+
/** Generate a standard unified patch. */
|
|
264
|
+
export function generateUnifiedPatch(path, oldContent, newContent, contextLines = 4) {
|
|
265
|
+
return Diff.createTwoFilesPatch(path, path, oldContent, newContent, undefined, undefined, {
|
|
266
|
+
context: contextLines,
|
|
267
|
+
headerOptions: Diff.FILE_HEADERS_ONLY,
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Generate a display-oriented diff string with line numbers and context.
|
|
272
|
+
* Returns both the diff string and the first changed line number (in the new file).
|
|
273
|
+
*/
|
|
274
|
+
export function generateDiffString(oldContent, newContent, contextLines = 4) {
|
|
275
|
+
const parts = Diff.diffLines(oldContent, newContent);
|
|
276
|
+
const output = [];
|
|
277
|
+
const oldLines = oldContent.split("\n");
|
|
278
|
+
const newLines = newContent.split("\n");
|
|
279
|
+
const maxLineNum = Math.max(oldLines.length, newLines.length);
|
|
280
|
+
const lineNumWidth = String(maxLineNum).length;
|
|
281
|
+
let oldLineNum = 1;
|
|
282
|
+
let newLineNum = 1;
|
|
283
|
+
let lastWasChange = false;
|
|
284
|
+
let firstChangedLine;
|
|
285
|
+
for (let i = 0; i < parts.length; i++) {
|
|
286
|
+
const part = parts[i];
|
|
287
|
+
const raw = part.value.split("\n");
|
|
288
|
+
if (raw[raw.length - 1] === "") {
|
|
289
|
+
raw.pop();
|
|
290
|
+
}
|
|
291
|
+
if (part.added || part.removed) {
|
|
292
|
+
// Capture the first changed line (in the new file)
|
|
293
|
+
if (firstChangedLine === undefined) {
|
|
294
|
+
firstChangedLine = newLineNum;
|
|
295
|
+
}
|
|
296
|
+
// Show the change
|
|
297
|
+
for (const line of raw) {
|
|
298
|
+
if (part.added) {
|
|
299
|
+
const lineNum = String(newLineNum).padStart(lineNumWidth, " ");
|
|
300
|
+
output.push(`+${lineNum} ${line}`);
|
|
301
|
+
newLineNum++;
|
|
302
|
+
}
|
|
303
|
+
else {
|
|
304
|
+
// removed
|
|
305
|
+
const lineNum = String(oldLineNum).padStart(lineNumWidth, " ");
|
|
306
|
+
output.push(`-${lineNum} ${line}`);
|
|
307
|
+
oldLineNum++;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
lastWasChange = true;
|
|
311
|
+
}
|
|
312
|
+
else {
|
|
313
|
+
// Context lines - only show a few before/after changes
|
|
314
|
+
const nextPartIsChange = i < parts.length - 1 && (parts[i + 1].added || parts[i + 1].removed);
|
|
315
|
+
const hasLeadingChange = lastWasChange;
|
|
316
|
+
const hasTrailingChange = nextPartIsChange;
|
|
317
|
+
if (hasLeadingChange && hasTrailingChange) {
|
|
318
|
+
if (raw.length <= contextLines * 2) {
|
|
319
|
+
for (const line of raw) {
|
|
320
|
+
const lineNum = String(oldLineNum).padStart(lineNumWidth, " ");
|
|
321
|
+
output.push(` ${lineNum} ${line}`);
|
|
322
|
+
oldLineNum++;
|
|
323
|
+
newLineNum++;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
else {
|
|
327
|
+
const leadingLines = raw.slice(0, contextLines);
|
|
328
|
+
const trailingLines = raw.slice(raw.length - contextLines);
|
|
329
|
+
const skippedLines = raw.length - leadingLines.length - trailingLines.length;
|
|
330
|
+
for (const line of leadingLines) {
|
|
331
|
+
const lineNum = String(oldLineNum).padStart(lineNumWidth, " ");
|
|
332
|
+
output.push(` ${lineNum} ${line}`);
|
|
333
|
+
oldLineNum++;
|
|
334
|
+
newLineNum++;
|
|
335
|
+
}
|
|
336
|
+
output.push(` ${"".padStart(lineNumWidth, " ")} ...`);
|
|
337
|
+
oldLineNum += skippedLines;
|
|
338
|
+
newLineNum += skippedLines;
|
|
339
|
+
for (const line of trailingLines) {
|
|
340
|
+
const lineNum = String(oldLineNum).padStart(lineNumWidth, " ");
|
|
341
|
+
output.push(` ${lineNum} ${line}`);
|
|
342
|
+
oldLineNum++;
|
|
343
|
+
newLineNum++;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
else if (hasLeadingChange) {
|
|
348
|
+
const shownLines = raw.slice(0, contextLines);
|
|
349
|
+
const skippedLines = raw.length - shownLines.length;
|
|
350
|
+
for (const line of shownLines) {
|
|
351
|
+
const lineNum = String(oldLineNum).padStart(lineNumWidth, " ");
|
|
352
|
+
output.push(` ${lineNum} ${line}`);
|
|
353
|
+
oldLineNum++;
|
|
354
|
+
newLineNum++;
|
|
355
|
+
}
|
|
356
|
+
if (skippedLines > 0) {
|
|
357
|
+
output.push(` ${"".padStart(lineNumWidth, " ")} ...`);
|
|
358
|
+
oldLineNum += skippedLines;
|
|
359
|
+
newLineNum += skippedLines;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
else if (hasTrailingChange) {
|
|
363
|
+
const skippedLines = Math.max(0, raw.length - contextLines);
|
|
364
|
+
if (skippedLines > 0) {
|
|
365
|
+
output.push(` ${"".padStart(lineNumWidth, " ")} ...`);
|
|
366
|
+
oldLineNum += skippedLines;
|
|
367
|
+
newLineNum += skippedLines;
|
|
368
|
+
}
|
|
369
|
+
for (const line of raw.slice(skippedLines)) {
|
|
370
|
+
const lineNum = String(oldLineNum).padStart(lineNumWidth, " ");
|
|
371
|
+
output.push(` ${lineNum} ${line}`);
|
|
372
|
+
oldLineNum++;
|
|
373
|
+
newLineNum++;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
else {
|
|
377
|
+
// Skip these context lines entirely
|
|
378
|
+
oldLineNum += raw.length;
|
|
379
|
+
newLineNum += raw.length;
|
|
380
|
+
}
|
|
381
|
+
lastWasChange = false;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
return { diff: output.join("\n"), firstChangedLine };
|
|
385
|
+
}
|
|
386
|
+
//# sourceMappingURL=edit-diff.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"edit-diff.js","sourceRoot":"","sources":["../../../src/harness/tools/edit-diff.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAE7B,MAAM,UAAU,gBAAgB,CAAC,OAAe;IAC/C,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;IACxC,MAAM,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IACpC,IAAI,KAAK,KAAK,CAAC,CAAC;QAAE,OAAO,IAAI,CAAC;IAC9B,IAAI,OAAO,KAAK,CAAC,CAAC;QAAE,OAAO,IAAI,CAAC;IAChC,OAAO,OAAO,GAAG,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC;AACxC,CAAC;AAED,MAAM,UAAU,aAAa,CAAC,IAAY;IACzC,OAAO,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;AACzD,CAAC;AAED,MAAM,UAAU,kBAAkB,CAAC,IAAY,EAAE,MAAqB;IACrE,OAAO,MAAM,KAAK,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;AA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AAC,YAAY,CAAC,EAAE,CAAC;oBAC5C,MAAM,OAAO,GAAG,MAAM,CAAC,UAAU,CAAC,CAAC,QAAQ,CAAC,YAAY,EAAE,GAAG,CAAC,CAAC;oBAC/D,MAAM,CAAC,IAAI,CAAC,IAAI,OAAO,IAAI,IAAI,EAAE,CAAC,CAAC;oBACnC,UAAU,EAAE,CAAC;oBACb,UAAU,EAAE,CAAC;gBACd,CAAC;YACF,CAAC;iBAAM,CAAC;gBACP,oCAAoC;gBACpC,UAAU,IAAI,GAAG,CAAC,MAAM,CAAC;gBACzB,UAAU,IAAI,GAAG,CAAC,MAAM,CAAC;YAC1B,CAAC;YAED,aAAa,GAAG,KAAK,CAAC;QACvB,CAAC;IACF,CAAC;IAED,OAAO,EAAE,IAAI,EAAE,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,gBAAgB,EAAE,CAAC;AACtD,CAAC","sourcesContent":["/**\n * Shared diff computation utilities for the edit and similar tools.\n */\n\nimport * as Diff from \"diff\";\n\nexport function detectLineEnding(content: string): \"\\r\\n\" | \"\\n\" {\n\tconst crlfIdx = content.indexOf(\"\\r\\n\");\n\tconst lfIdx = content.indexOf(\"\\n\");\n\tif (lfIdx === -1) return \"\\n\";\n\tif (crlfIdx === -1) return \"\\n\";\n\treturn crlfIdx < lfIdx ? \"\\r\\n\" : \"\\n\";\n}\n\nexport function normalizeToLF(text: string): string {\n\treturn text.replace(/\\r\\n/g, \"\\n\").replace(/\\r/g, \"\\n\");\n}\n\nexport function restoreLineEndings(text: string, ending: \"\\r\\n\" | \"\\n\"): string {\n\treturn ending === \"\\r\\n\" ? text.replace(/\\n/g, \"\\r\\n\") : text;\n}\n\n/**\n * Normalize text for fuzzy matching. Applies progressive transformations:\n * - Strip trailing whitespace from each line\n * - Normalize smart quotes to ASCII equivalents\n * - Normalize Unicode dashes/hyphens to ASCII hyphen\n * - Normalize special Unicode spaces to regular space\n */\nexport function normalizeForFuzzyMatch(text: string): string {\n\treturn (\n\t\ttext\n\t\t\t.normalize(\"NFKC\")\n\t\t\t// Strip trailing whitespace per line\n\t\t\t.split(\"\\n\")\n\t\t\t.map((line) => line.trimEnd())\n\t\t\t.join(\"\\n\")\n\t\t\t// Smart single quotes → '\n\t\t\t.replace(/[\\u2018\\u2019\\u201A\\u201B]/g, \"'\")\n\t\t\t// Smart double quotes → \"\n\t\t\t.replace(/[\\u201C\\u201D\\u201E\\u201F]/g, '\"')\n\t\t\t// Various dashes/hyphens → -\n\t\t\t// U+2010 hyphen, U+2011 non-breaking hyphen, U+2012 figure dash,\n\t\t\t// U+2013 en-dash, U+2014 em-dash, U+2015 horizontal bar, U+2212 minus\n\t\t\t.replace(/[\\u2010\\u2011\\u2012\\u2013\\u2014\\u2015\\u2212]/g, \"-\")\n\t\t\t// Special spaces → regular space\n\t\t\t// U+00A0 NBSP, U+2002-U+200A various spaces, U+202F narrow NBSP,\n\t\t\t// U+205F medium math space, U+3000 ideographic space\n\t\t\t.replace(/[\\u00A0\\u2002-\\u200A\\u202F\\u205F\\u3000]/g, \" \")\n\t);\n}\n\nfunction splitLinesWithEndings(content: string): string[] {\n\treturn content.match(/[^\\n]*\\n|[^\\n]+/g) ?? [];\n}\n\ninterface LineSpan {\n\tstart: number;\n\tend: number;\n}\n\ninterface MatchedEdit {\n\teditIndex: number;\n\tmatchIndex: number;\n\tmatchLength: number;\n\tnewText: string;\n}\n\ntype TextReplacement = Pick<MatchedEdit, \"matchIndex\" | \"matchLength\" | \"newText\">;\n\nfunction getLineSpans(content: string): LineSpan[] {\n\tlet offset = 0;\n\treturn splitLinesWithEndings(content).map((line) => {\n\t\tconst span = { start: offset, end: offset + line.length };\n\t\toffset = span.end;\n\t\treturn span;\n\t});\n}\n\nfunction getReplacementLineRange(lines: LineSpan[], replacement: TextReplacement) {\n\tconst replacementStart = replacement.matchIndex;\n\tconst replacementEnd = replacement.matchIndex + replacement.matchLength;\n\n\tlet startLine = -1;\n\tfor (let i = 0; i < lines.length; i++) {\n\t\tconst line = lines[i];\n\t\tif (replacementStart >= line.start && replacementStart < line.end) {\n\t\t\tstartLine = i;\n\t\t\tbreak;\n\t\t}\n\t}\n\tif (startLine === -1) {\n\t\tthrow new Error(\"Replacement range is outside the base content.\");\n\t}\n\n\tlet endLine = startLine;\n\twhile (endLine < lines.length && lines[endLine].end < replacementEnd) {\n\t\tendLine++;\n\t}\n\tif (endLine >= lines.length) {\n\t\tthrow new Error(\"Replacement range is outside the base content.\");\n\t}\n\n\treturn { startLine, endLine: endLine + 1 };\n}\n\nfunction applyReplacements(content: string, replacements: TextReplacement[], offset = 0): string {\n\tlet result = content;\n\tfor (let i = replacements.length - 1; i >= 0; i--) {\n\t\tconst replacement = replacements[i];\n\t\tconst matchIndex = replacement.matchIndex - offset;\n\t\tresult =\n\t\t\tresult.substring(0, matchIndex) + replacement.newText + result.substring(matchIndex + replacement.matchLength);\n\t}\n\treturn result;\n}\n\n/**\n * Apply replacements matched against `baseContent` to `originalContent` while\n * preserving unchanged line blocks from the original.\n *\n * This is useful when `baseContent` is a normalized view of the original. Each\n * replacement is widened to the lines it actually touches, those touched lines\n * are rewritten from the normalized base, and all other lines are copied back\n * from `originalContent`. The actual replacement ranges drive preservation so\n * duplicate normalized lines cannot be aligned to the wrong occurrence.\n */\nexport function applyReplacementsPreservingUnchangedLines(\n\toriginalContent: string,\n\tbaseContent: string,\n\treplacements: TextReplacement[],\n): string {\n\tconst originalLines = splitLinesWithEndings(originalContent);\n\tconst baseLines = getLineSpans(baseContent);\n\tif (originalLines.length !== baseLines.length) {\n\t\tthrow new Error(\"Cannot preserve unchanged lines because the base content has a different line count.\");\n\t}\n\n\tconst groups: Array<{ startLine: number; endLine: number; replacements: TextReplacement[] }> = [];\n\tconst sortedReplacements = [...replacements].sort((a, b) => a.matchIndex - b.matchIndex);\n\tfor (const replacement of sortedReplacements) {\n\t\tconst range = getReplacementLineRange(baseLines, replacement);\n\t\tconst current = groups[groups.length - 1];\n\t\tif (current && range.startLine < current.endLine) {\n\t\t\tcurrent.endLine = Math.max(current.endLine, range.endLine);\n\t\t\tcurrent.replacements.push(replacement);\n\t\t\tcontinue;\n\t\t}\n\t\tgroups.push({ ...range, replacements: [replacement] });\n\t}\n\n\tlet originalLineIndex = 0;\n\tlet result = \"\";\n\tfor (const group of groups) {\n\t\tresult += originalLines.slice(originalLineIndex, group.startLine).join(\"\");\n\n\t\tconst groupStartOffset = baseLines[group.startLine].start;\n\t\tconst groupEndOffset = baseLines[group.endLine - 1].end;\n\t\tresult += applyReplacements(\n\t\t\tbaseContent.slice(groupStartOffset, groupEndOffset),\n\t\t\tgroup.replacements,\n\t\t\tgroupStartOffset,\n\t\t);\n\t\toriginalLineIndex = group.endLine;\n\t}\n\tresult += originalLines.slice(originalLineIndex).join(\"\");\n\n\treturn result;\n}\n\nexport interface FuzzyMatchResult {\n\t/** Whether a match was found */\n\tfound: boolean;\n\t/** The index where the match starts (in the content that should be used for replacement) */\n\tindex: number;\n\t/** Length of the matched text */\n\tmatchLength: number;\n\t/** Whether fuzzy matching was used (false = exact match) */\n\tusedFuzzyMatch: boolean;\n\t/**\n\t * The content to use for replacement operations.\n\t * When exact match: original content. When fuzzy match: normalized content.\n\t */\n\tcontentForReplacement: string;\n}\n\nexport interface Edit {\n\toldText: string;\n\tnewText: string;\n}\n\nexport interface AppliedEditsResult {\n\tbaseContent: string;\n\tnewContent: string;\n}\n\n/**\n * Find oldText in content, trying exact match first, then fuzzy match.\n * When fuzzy matching is used, the returned contentForReplacement is the\n * fuzzy-normalized version of the content (trailing whitespace stripped,\n * Unicode quotes/dashes normalized to ASCII).\n */\nexport function fuzzyFindText(content: string, oldText: string): FuzzyMatchResult {\n\t// Try exact match first\n\tconst exactIndex = content.indexOf(oldText);\n\tif (exactIndex !== -1) {\n\t\treturn {\n\t\t\tfound: true,\n\t\t\tindex: exactIndex,\n\t\t\tmatchLength: oldText.length,\n\t\t\tusedFuzzyMatch: false,\n\t\t\tcontentForReplacement: content,\n\t\t};\n\t}\n\n\t// Try fuzzy match - work entirely in normalized space\n\tconst fuzzyContent = normalizeForFuzzyMatch(content);\n\tconst fuzzyOldText = normalizeForFuzzyMatch(oldText);\n\tconst fuzzyIndex = fuzzyContent.indexOf(fuzzyOldText);\n\n\tif (fuzzyIndex === -1) {\n\t\treturn {\n\t\t\tfound: false,\n\t\t\tindex: -1,\n\t\t\tmatchLength: 0,\n\t\t\tusedFuzzyMatch: false,\n\t\t\tcontentForReplacement: content,\n\t\t};\n\t}\n\n\t// When fuzzy matching, return offsets in normalized space. Callers can use\n\t// the normalized content to compute replacements, then decide how much of\n\t// that normalized output should be written back.\n\treturn {\n\t\tfound: true,\n\t\tindex: fuzzyIndex,\n\t\tmatchLength: fuzzyOldText.length,\n\t\tusedFuzzyMatch: true,\n\t\tcontentForReplacement: fuzzyContent,\n\t};\n}\n\n/** Strip UTF-8 BOM if present, return both the BOM (if any) and the text without it */\nexport function stripBom(content: string): { bom: string; text: string } {\n\treturn content.startsWith(\"\\uFEFF\") ? { bom: \"\\uFEFF\", text: content.slice(1) } : { bom: \"\", text: content };\n}\n\nfunction countOccurrences(content: string, oldText: string): number {\n\tconst fuzzyContent = normalizeForFuzzyMatch(content);\n\tconst fuzzyOldText = normalizeForFuzzyMatch(oldText);\n\treturn fuzzyContent.split(fuzzyOldText).length - 1;\n}\n\nfunction getNotFoundError(path: string, editIndex: number, totalEdits: number): Error {\n\tif (totalEdits === 1) {\n\t\treturn new Error(\n\t\t\t`Could not find the exact text in ${path}. The old text must match exactly including all whitespace and newlines.`,\n\t\t);\n\t}\n\treturn new Error(\n\t\t`Could not find edits[${editIndex}] in ${path}. The oldText must match exactly including all whitespace and newlines.`,\n\t);\n}\n\nfunction getDuplicateError(path: string, editIndex: number, totalEdits: number, occurrences: number): Error {\n\tif (totalEdits === 1) {\n\t\treturn new Error(\n\t\t\t`Found ${occurrences} occurrences of the text in ${path}. The text must be unique. Please provide more context to make it unique.`,\n\t\t);\n\t}\n\treturn new Error(\n\t\t`Found ${occurrences} occurrences of edits[${editIndex}] in ${path}. Each oldText must be unique. Please provide more context to make it unique.`,\n\t);\n}\n\nfunction getEmptyOldTextError(path: string, editIndex: number, totalEdits: number): Error {\n\tif (totalEdits === 1) {\n\t\treturn new Error(`oldText must not be empty in ${path}.`);\n\t}\n\treturn new Error(`edits[${editIndex}].oldText must not be empty in ${path}.`);\n}\n\nfunction getNoChangeError(path: string, totalEdits: number): Error {\n\tif (totalEdits === 1) {\n\t\treturn new Error(\n\t\t\t`No changes made to ${path}. The replacement produced identical content. This might indicate an issue with special characters or the text not existing as expected.`,\n\t\t);\n\t}\n\treturn new Error(`No changes made to ${path}. The replacements produced identical content.`);\n}\n\n/**\n * Apply one or more exact-text replacements to LF-normalized content.\n *\n * All edits are matched against the same original content. Replacements are\n * then applied in reverse order so offsets remain stable. If any edit needs\n * fuzzy matching, the operation runs in fuzzy-normalized content space and then\n * overlays those line-level changes onto the original content so unchanged line\n * blocks keep their original bytes.\n */\nexport function applyEditsToNormalizedContent(\n\tnormalizedContent: string,\n\tedits: Edit[],\n\tpath: string,\n): AppliedEditsResult {\n\tconst normalizedEdits = edits.map((edit) => ({\n\t\toldText: normalizeToLF(edit.oldText),\n\t\tnewText: normalizeToLF(edit.newText),\n\t}));\n\n\tfor (let i = 0; i < normalizedEdits.length; i++) {\n\t\tif (normalizedEdits[i].oldText.length === 0) {\n\t\t\tthrow getEmptyOldTextError(path, i, normalizedEdits.length);\n\t\t}\n\t}\n\n\tconst initialMatches = normalizedEdits.map((edit) => fuzzyFindText(normalizedContent, edit.oldText));\n\tconst usedFuzzyMatch = initialMatches.some((match) => match.usedFuzzyMatch);\n\tconst replacementBaseContent = usedFuzzyMatch ? normalizeForFuzzyMatch(normalizedContent) : normalizedContent;\n\n\tconst matchedEdits: MatchedEdit[] = [];\n\tfor (let i = 0; i < normalizedEdits.length; i++) {\n\t\tconst edit = normalizedEdits[i];\n\t\tconst matchResult = fuzzyFindText(replacementBaseContent, edit.oldText);\n\t\tif (!matchResult.found) {\n\t\t\tthrow getNotFoundError(path, i, normalizedEdits.length);\n\t\t}\n\n\t\tconst occurrences = countOccurrences(replacementBaseContent, edit.oldText);\n\t\tif (occurrences > 1) {\n\t\t\tthrow getDuplicateError(path, i, normalizedEdits.length, occurrences);\n\t\t}\n\n\t\tmatchedEdits.push({\n\t\t\teditIndex: i,\n\t\t\tmatchIndex: matchResult.index,\n\t\t\tmatchLength: matchResult.matchLength,\n\t\t\tnewText: edit.newText,\n\t\t});\n\t}\n\n\tmatchedEdits.sort((a, b) => a.matchIndex - b.matchIndex);\n\tfor (let i = 1; i < matchedEdits.length; i++) {\n\t\tconst previous = matchedEdits[i - 1];\n\t\tconst current = matchedEdits[i];\n\t\tif (previous.matchIndex + previous.matchLength > current.matchIndex) {\n\t\t\tthrow new Error(\n\t\t\t\t`edits[${previous.editIndex}] and edits[${current.editIndex}] overlap in ${path}. Merge them into one edit or target disjoint regions.`,\n\t\t\t);\n\t\t}\n\t}\n\n\tconst baseContent = normalizedContent;\n\tconst newContent = usedFuzzyMatch\n\t\t? applyReplacementsPreservingUnchangedLines(normalizedContent, replacementBaseContent, matchedEdits)\n\t\t: applyReplacements(replacementBaseContent, matchedEdits);\n\n\tif (baseContent === newContent) {\n\t\tthrow getNoChangeError(path, normalizedEdits.length);\n\t}\n\n\treturn { baseContent, newContent };\n}\n\n/** Generate a standard unified patch. */\nexport function generateUnifiedPatch(path: string, oldContent: string, newContent: string, contextLines = 4): string {\n\treturn Diff.createTwoFilesPatch(path, path, oldContent, newContent, undefined, undefined, {\n\t\tcontext: contextLines,\n\t\theaderOptions: Diff.FILE_HEADERS_ONLY,\n\t});\n}\n\n/**\n * Generate a display-oriented diff string with line numbers and context.\n * Returns both the diff string and the first changed line number (in the new file).\n */\nexport function generateDiffString(\n\toldContent: string,\n\tnewContent: string,\n\tcontextLines = 4,\n): { diff: string; firstChangedLine: number | undefined } {\n\tconst parts = Diff.diffLines(oldContent, newContent);\n\tconst output: string[] = [];\n\n\tconst oldLines = oldContent.split(\"\\n\");\n\tconst newLines = newContent.split(\"\\n\");\n\tconst maxLineNum = Math.max(oldLines.length, newLines.length);\n\tconst lineNumWidth = String(maxLineNum).length;\n\n\tlet oldLineNum = 1;\n\tlet newLineNum = 1;\n\tlet lastWasChange = false;\n\tlet firstChangedLine: number | undefined;\n\n\tfor (let i = 0; i < parts.length; i++) {\n\t\tconst part = parts[i];\n\t\tconst raw = part.value.split(\"\\n\");\n\t\tif (raw[raw.length - 1] === \"\") {\n\t\t\traw.pop();\n\t\t}\n\n\t\tif (part.added || part.removed) {\n\t\t\t// Capture the first changed line (in the new file)\n\t\t\tif (firstChangedLine === undefined) {\n\t\t\t\tfirstChangedLine = newLineNum;\n\t\t\t}\n\n\t\t\t// Show the change\n\t\t\tfor (const line of raw) {\n\t\t\t\tif (part.added) {\n\t\t\t\t\tconst lineNum = String(newLineNum).padStart(lineNumWidth, \" \");\n\t\t\t\t\toutput.push(`+${lineNum} ${line}`);\n\t\t\t\t\tnewLineNum++;\n\t\t\t\t} else {\n\t\t\t\t\t// removed\n\t\t\t\t\tconst lineNum = String(oldLineNum).padStart(lineNumWidth, \" \");\n\t\t\t\t\toutput.push(`-${lineNum} ${line}`);\n\t\t\t\t\toldLineNum++;\n\t\t\t\t}\n\t\t\t}\n\t\t\tlastWasChange = true;\n\t\t} else {\n\t\t\t// Context lines - only show a few before/after changes\n\t\t\tconst nextPartIsChange = i < parts.length - 1 && (parts[i + 1].added || parts[i + 1].removed);\n\t\t\tconst hasLeadingChange = lastWasChange;\n\t\t\tconst hasTrailingChange = nextPartIsChange;\n\n\t\t\tif (hasLeadingChange && hasTrailingChange) {\n\t\t\t\tif (raw.length <= contextLines * 2) {\n\t\t\t\t\tfor (const line of raw) {\n\t\t\t\t\t\tconst lineNum = String(oldLineNum).padStart(lineNumWidth, \" \");\n\t\t\t\t\t\toutput.push(` ${lineNum} ${line}`);\n\t\t\t\t\t\toldLineNum++;\n\t\t\t\t\t\tnewLineNum++;\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tconst leadingLines = raw.slice(0, contextLines);\n\t\t\t\t\tconst trailingLines = raw.slice(raw.length - contextLines);\n\t\t\t\t\tconst skippedLines = raw.length - leadingLines.length - trailingLines.length;\n\n\t\t\t\t\tfor (const line of leadingLines) {\n\t\t\t\t\t\tconst lineNum = String(oldLineNum).padStart(lineNumWidth, \" \");\n\t\t\t\t\t\toutput.push(` ${lineNum} ${line}`);\n\t\t\t\t\t\toldLineNum++;\n\t\t\t\t\t\tnewLineNum++;\n\t\t\t\t\t}\n\n\t\t\t\t\toutput.push(` ${\"\".padStart(lineNumWidth, \" \")} ...`);\n\t\t\t\t\toldLineNum += skippedLines;\n\t\t\t\t\tnewLineNum += skippedLines;\n\n\t\t\t\t\tfor (const line of trailingLines) {\n\t\t\t\t\t\tconst lineNum = String(oldLineNum).padStart(lineNumWidth, \" \");\n\t\t\t\t\t\toutput.push(` ${lineNum} ${line}`);\n\t\t\t\t\t\toldLineNum++;\n\t\t\t\t\t\tnewLineNum++;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if (hasLeadingChange) {\n\t\t\t\tconst shownLines = raw.slice(0, contextLines);\n\t\t\t\tconst skippedLines = raw.length - shownLines.length;\n\n\t\t\t\tfor (const line of shownLines) {\n\t\t\t\t\tconst lineNum = String(oldLineNum).padStart(lineNumWidth, \" \");\n\t\t\t\t\toutput.push(` ${lineNum} ${line}`);\n\t\t\t\t\toldLineNum++;\n\t\t\t\t\tnewLineNum++;\n\t\t\t\t}\n\n\t\t\t\tif (skippedLines > 0) {\n\t\t\t\t\toutput.push(` ${\"\".padStart(lineNumWidth, \" \")} ...`);\n\t\t\t\t\toldLineNum += skippedLines;\n\t\t\t\t\tnewLineNum += skippedLines;\n\t\t\t\t}\n\t\t\t} else if (hasTrailingChange) {\n\t\t\t\tconst skippedLines = Math.max(0, raw.length - contextLines);\n\t\t\t\tif (skippedLines > 0) {\n\t\t\t\t\toutput.push(` ${\"\".padStart(lineNumWidth, \" \")} ...`);\n\t\t\t\t\toldLineNum += skippedLines;\n\t\t\t\t\tnewLineNum += skippedLines;\n\t\t\t\t}\n\n\t\t\t\tfor (const line of raw.slice(skippedLines)) {\n\t\t\t\t\tconst lineNum = String(oldLineNum).padStart(lineNumWidth, \" \");\n\t\t\t\t\toutput.push(` ${lineNum} ${line}`);\n\t\t\t\t\toldLineNum++;\n\t\t\t\t\tnewLineNum++;\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\t// Skip these context lines entirely\n\t\t\t\toldLineNum += raw.length;\n\t\t\t\tnewLineNum += raw.length;\n\t\t\t}\n\n\t\t\tlastWasChange = false;\n\t\t}\n\t}\n\n\treturn { diff: output.join(\"\\n\"), firstChangedLine };\n}\n"]}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type Static, Type } from "typebox";
|
|
2
|
+
import type { AgentHarnessTool } from "../types.ts";
|
|
3
|
+
import type { ExecutionToolContext } from "./tool-context.ts";
|
|
4
|
+
declare const editSchema: Type.TObject<{
|
|
5
|
+
path: Type.TString;
|
|
6
|
+
edits: Type.TArray<Type.TObject<{
|
|
7
|
+
oldText: Type.TString;
|
|
8
|
+
newText: Type.TString;
|
|
9
|
+
}>>;
|
|
10
|
+
}>;
|
|
11
|
+
export type EditToolInput = Static<typeof editSchema>;
|
|
12
|
+
export interface EditToolDetails {
|
|
13
|
+
diff: string;
|
|
14
|
+
patch: string;
|
|
15
|
+
firstChangedLine?: number;
|
|
16
|
+
}
|
|
17
|
+
export declare function createEditTool<TContext extends ExecutionToolContext = ExecutionToolContext>(): AgentHarnessTool<TContext, typeof editSchema, EditToolDetails | undefined>;
|
|
18
|
+
export {};
|
|
19
|
+
//# sourceMappingURL=edit.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"edit.d.ts","sourceRoot":"","sources":["../../../src/harness/tools/edit.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,MAAM,EAAE,IAAI,EAAE,MAAM,SAAS,CAAC;AAC5C,OAAO,KAAK,EAAE,gBAAgB,EAAa,MAAM,aAAa,CAAC;AAa/D,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAa9D,QAAA,MAAM,UAAU;;;;;;EASf,CAAC;AAEF,MAAM,MAAM,aAAa,GAAG,MAAM,CAAC,OAAO,UAAU,CAAC,CAAC;AAGtD,MAAM,WAAW,eAAe;IAC/B,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,gBAAgB,CAAC,EAAE,MAAM,CAAC;CAC1B;AA+BD,wBAAgB,cAAc,CAAC,QAAQ,SAAS,oBAAoB,GAAG,oBAAoB,KAAK,gBAAgB,CAC/G,QAAQ,EACR,OAAO,UAAU,EACjB,eAAe,GAAG,SAAS,CAC3B,CA8CA"}
|