@@ -31,7 +31,11 @@ type PatchTool struct {
3131 Callback PatchCallback // may be nil
3232 // Pwd is the working directory for resolving relative paths
3333 Pwd string
34+ // Simplified indicates whether to use the simplified input schema.
35+ // Helpful for weaker models.
36+ Simplified bool
3437 // ClipboardEnabled controls whether clipboard functionality is enabled.
38+ // Ignored if Simplified is true.
3539 // NB: The actual implementation of the patch tool is unchanged,
3640 // this flag merely extends the description and input schema to include the clipboard operations.
3741 ClipboardEnabled bool
@@ -43,7 +47,10 @@ type PatchTool struct {
4347func (p * PatchTool ) Tool () * llm.Tool {
4448 description := PatchBaseDescription + PatchUsageNotes
4549 schema := PatchStandardInputSchema
46- if p .ClipboardEnabled {
50+ switch {
51+ case p .Simplified :
52+ schema = PatchStandardSimplifiedSchema
53+ case p .ClipboardEnabled :
4754 description = PatchBaseDescription + PatchClipboardDescription + PatchUsageNotes
4855 schema = PatchClipboardInputSchema
4956 }
@@ -130,6 +137,36 @@ Usage notes:
130137}
131138`
132139
140+ PatchStandardSimplifiedSchema = `{
141+ "type": "object",
142+ "required": ["path", "patch"],
143+ "properties": {
144+ "path": {
145+ "type": "string",
146+ "description": "Path to the file to patch"
147+ },
148+ "patch": {
149+ "type": "object",
150+ "required": ["operation", "newText"],
151+ "properties": {
152+ "operation": {
153+ "type": "string",
154+ "enum": ["replace", "append_eof", "prepend_bof", "overwrite"],
155+ "description": "Type of operation to perform"
156+ },
157+ "oldText": {
158+ "type": "string",
159+ "description": "Text to locate for the operation (must be unique in file, required for replace)"
160+ },
161+ "newText": {
162+ "type": "string",
163+ "description": "The new text to use (empty for deletions)"
164+ }
165+ }
166+ }
167+ }
168+ }`
169+
133170 PatchClipboardInputSchema = `
134171{
135172 "type": "object",
@@ -199,8 +236,14 @@ type PatchInput struct {
199236
200237// PatchInputOne is a simplified version of PatchInput for single patch operations.
201238type PatchInputOne struct {
202- Path string `json:"path"`
203- Patches PatchRequest `json:"patches"`
239+ Path string `json:"path"`
240+ Patches * PatchRequest `json:"patches"`
241+ }
242+
243+ // PatchInputOneSingular is PatchInputOne with a better name for the singular case.
244+ type PatchInputOneSingular struct {
245+ Path string `json:"path"`
246+ Patch * PatchRequest `json:"patch"`
204247}
205248
206249type PatchInputOneString struct {
@@ -253,17 +296,21 @@ func (p *PatchTool) Run(ctx context.Context, m json.RawMessage) llm.ToolOut {
253296func (p * PatchTool ) patchParse (m json.RawMessage ) (PatchInput , error ) {
254297 var input PatchInput
255298 originalErr := json .Unmarshal (m , & input )
256- if originalErr == nil {
299+ if originalErr == nil && len ( input . Patches ) > 0 {
257300 return input , nil
258301 }
259302 var inputOne PatchInputOne
260- if err := json .Unmarshal (m , & inputOne ); err == nil {
261- return PatchInput {Path : inputOne .Path , Patches : []PatchRequest {inputOne .Patches }}, nil
303+ if err := json .Unmarshal (m , & inputOne ); err == nil && inputOne .Patches != nil {
304+ return PatchInput {Path : inputOne .Path , Patches : []PatchRequest {* inputOne .Patches }}, nil
305+ }
306+ var inputOneSingular PatchInputOneSingular
307+ if err := json .Unmarshal (m , & inputOneSingular ); err == nil && inputOneSingular .Patch != nil {
308+ return PatchInput {Path : inputOneSingular .Path , Patches : []PatchRequest {* inputOneSingular .Patch }}, nil
262309 }
263310 var inputOneString PatchInputOneString
264311 if err := json .Unmarshal (m , & inputOneString ); err == nil {
265312 var onePatch PatchRequest
266- if err := json .Unmarshal ([]byte (inputOneString .Patches ), & onePatch ); err == nil {
313+ if err := json .Unmarshal ([]byte (inputOneString .Patches ), & onePatch ); err == nil && onePatch . Operation != "" {
267314 return PatchInput {Path : inputOneString .Path , Patches : []PatchRequest {onePatch }}, nil
268315 }
269316 var patches []PatchRequest
0 commit comments