Files
cline-multi-mirror/src/shared/Patch.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

66 lines
1.3 KiB
TypeScript

/**
* Apply Patch constants
*/
export const PATCH_MARKERS = {
BEGIN: "*** Begin Patch",
END: "*** End Patch",
ADD: "*** Add File: ",
UPDATE: "*** Update File: ",
DELETE: "*** Delete File: ",
MOVE: "*** Move to: ",
SECTION: "@@",
END_FILE: "*** End of File",
} as const
/**
* Expected bash wrappers for apply patch content
*/
export const BASH_WRAPPERS = ["%%bash", "apply_patch", "EOF", "```"] as const
/**
* Domains of patch actions
*/
export enum PatchActionType {
ADD = "add",
DELETE = "delete", // TODO: Implement delete action in diff editor.
UPDATE = "update",
}
export interface PatchChunk {
origIndex: number // line index in original file where change starts
delLines: string[] // Lines to delete (without the "-" prefix)
insLines: string[] // Lines to insert (without the "+" prefix)
}
export interface PatchAction {
type: PatchActionType
newFile?: string
chunks: PatchChunk[]
movePath?: string
}
/**
* Warning information for skipped/problematic chunks
*/
export interface PatchWarning {
path: string
chunkIndex?: number
message: string
context?: string
}
/**
* Apply Patch structure
*/
export interface Patch {
actions: Record<string, PatchAction>
warnings?: PatchWarning[]
}
export class DiffError extends Error {
constructor(message: string) {
super(message)
this.name = "DiffError"
}
}