Files
cline-multi-mirror/src/shared/string.ts
T
Bee a3e000d5dd fix: refactor APPLY_PATCH tool [ENG-1174] (#7189)
* feat: refactor APPLY_PATCH tool

Replace separate file creation and editing tools with unified APPLY_PATCH tool for the native-gpt-5 model variant. This consolidation simplifies file operations through a single patch-based interface.

Changes:
- Replace FILE_NEW and FILE_EDIT tools with APPLY_PATCH in config for gpt-5 with native tool calling
- Disable EDITING_FILES system prompt section (no longer needed)
- Remove EDITING_FILES section from base template
- Refactor ApplyPatchHandler with improved architecture:
  - Extract patch parsing logic into PatchParser utility class
  - Extract file operations into FileProviderOperations utility
  - Extract path resolution into PathResolver utility
  - Add comprehensive error handling with DiffError types
  - Improve type safety with shared Patch types
- Add extensive test coverage for PatchParser including:
  - Edge cases (empty files, large files, unicode)
  - Error conditions (malformed patches, invalid operations)
  - Complex scenarios (multiple chunks, context matching)
- Export PATCH_MARKERS and BASH_WRAPPERS for reusability

This refactoring improves maintainability, testability, and provides a more robust patch application system for the GPT-5 model variant when native tool calling is enabled.

* Add diagnostic to result

* Update unit tests

* add feedback

* typo

* captureToolUsage

* fix test

* fix test

* Update ClineMessage

* update diff editor on stream

* revert content partial stream

* Disable Apply patch and add native gpt snap shot

* Remove delete action and make preserveEscaping conditional

* Add delete function to diff provider

* Update UI for file deletion
2025-11-05 20:10:26 -08:00

78 lines
2.6 KiB
TypeScript

/**
* Unicode punctuation normalisation helpers
* Makes patch matching resilient to visually identical but different Unicode code-points
*/
const PUNCT_EQUIV: Record<string, string> = {
// Hyphen / dash variants
"-": "-",
"\u2010": "-", // HYPHEN
"\u2011": "-", // NO-BREAK HYPHEN
"\u2012": "-", // FIGURE DASH
"\u2013": "-", // EN DASH
"\u2014": "-", // EM DASH
"\u2212": "-", // MINUS SIGN
// Double quotes
"\u0022": '"', // QUOTATION MARK
"\u201C": '"', // LEFT DOUBLE QUOTATION MARK
"\u201D": '"', // RIGHT DOUBLE QUOTATION MARK
"\u201E": '"', // DOUBLE LOW-9 QUOTATION MARK
"\u00AB": '"', // LEFT-POINTING DOUBLE ANGLE QUOTATION MARK
"\u00BB": '"', // RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK
// Single quotes
"\u0027": "'", // APOSTROPHE
"\u2018": "'", // LEFT SINGLE QUOTATION MARK
"\u2019": "'", // RIGHT SINGLE QUOTATION MARK
"\u201B": "'", // SINGLE HIGH-REVERSED-9 QUOTATION MARK
// Spaces
"\u00A0": " ", // NO-BREAK SPACE
"\u202F": " ", // NARROW NO-BREAK SPACE
}
/**
* Canonicalize a string by normalizing unicode punctuation and quotes
*/
export function canonicalize(s: string): string {
// First normalize unicode and punctuation
let normalized = s.normalize("NFC").replace(/./gu, (c) => PUNCT_EQUIV[c] ?? c)
// Then normalize escaped/unescaped quotes to handle cases where:
// - patch has ` but file has \`
// - patch has ' but file has \'
// - patch has " but file has \"
normalized = normalized
.replace(/\\`/g, "`") // \` -> `
.replace(/\\'/g, "'") // \' -> '
.replace(/\\"/g, '"') // \" -> "
return normalized
}
/**
* Preserve the escaping style from original text when applying new text
* If original has \`, preserve that style in the replacement
*/
export function preserveEscaping(originalText: string, newText: string): string {
// Check if original has escaped backticks, quotes, or apostrophes
const hasEscapedBacktick = originalText.includes("\\`")
const hasEscapedSingleQuote = originalText.includes("\\'")
const hasEscapedDoubleQuote = originalText.includes('\\"')
let result = newText
// Apply escaping to match original style
if (hasEscapedBacktick && !newText.includes("\\`")) {
// Escape backslashes first, then backticks
result = result.replace(/\\/g, "\\\\").replace(/`/g, "\\`")
}
if (hasEscapedSingleQuote && !newText.includes("\\'")) {
// Escape backslashes first, then single quotes
result = result.replace(/\\/g, "\\\\").replace(/'/g, "\\'")
}
if (hasEscapedDoubleQuote && !newText.includes('\\"')) {
// Escape backslashes first, then double quotes
result = result.replace(/\\/g, "\\\\").replace(/"/g, '\\"')
}
return result
}