opencode-tool-repair 0.0.0-stage → 0.1.1
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/CHANGELOG.md +11 -0
- package/LICENSE +21 -0
- package/README.md +30 -2
- package/index.js +356 -0
- package/package.json +12 -4
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.1 (2026-11-22)
|
|
4
|
+
|
|
5
|
+
- README: the install example used `"plugin"`; OpenCode 2.x reads `"plugins"`.
|
|
6
|
+
|
|
7
|
+
## 0.1.0 (2026-11-22)
|
|
8
|
+
|
|
9
|
+
- Fixes tool calls local models send in another agent's format: `edits` lists, other argument names, garbled edit keys, bad optional values.
|
|
10
|
+
- Corrects mistyped paths in `read`, `edit`, `write`, `glob` and `grep` when exactly one existing path fits, and a misspelled project folder as shell `workdir`.
|
|
11
|
+
- Never changes paths inside shell commands, never moves a `write` into a lookalike folder, and never redirects a `write` onto an existing file.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 the opencode-tool-repair authors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,31 @@
|
|
|
1
|
-
#
|
|
1
|
+
# opencode-tool-repair
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
An OpenCode 2.x plugin that fixes tool calls local models send in another agent's format, before OpenCode rejects them.
|
|
4
|
+
|
|
5
|
+
Some local models (Flint, for one) were trained on agents whose tools take different argument names. In OpenCode their edits fail with "oldString: Missing key", the model sends the same call again, and nothing changes. This plugin rewrites those calls:
|
|
6
|
+
|
|
7
|
+
- **edit with an `edits` list** (`[{oldText, newText}, …]`, as an array or a JSON string): one change becomes a normal edit. Several are written together when each old text is found exactly once, Windows line endings included. Otherwise the first is applied and the model is told to send the rest one per call.
|
|
8
|
+
- **Other names for the same argument:** `oldText`/`old_string` become `oldString`, `newText`/`new_string` become `newString`, `file_path`/`filePath` become `path`, `contents` becomes `content`, and `replace_all` becomes `replaceAll`.
|
|
9
|
+
- **Garbled edit keys.** Flint sometimes splits `newString` into `"new": "String"` plus an empty `""` key holding the text, or writes `edString` for `oldString`; both are put back. An edit with no `newString` at all can't be recovered, so the model is told to send all three fields in one call, or to use `write` for a large change.
|
|
10
|
+
- **Bad optional values,** like `"offset": ""`, `"limit": "10"` or `"replaceAll": "true"`, and an empty `""` key.
|
|
11
|
+
- **Mistyped paths.** Some models drop or add an underscore (`cloudsave` for `cloud_save`) or lose a couple of letters in a long folder name. When a path in `read`, `edit`, `write`, `glob` or `grep` doesn't exist, and exactly one existing path differs from it only that way, that path is used and the model is told. A shell `workdir` is corrected only when it's the project folder misspelled. Paths inside shell commands are never changed, so `rm -rf` on a missing folder fails instead of hitting a real folder with a similar name. A `write` keeps its new file name; only the folders above it are corrected, only for an underscore or dash slip, and never onto a file that already exists. Paths that could mean two things, short names, and paths with no close match are left alone.
|
|
12
|
+
|
|
13
|
+
Whenever it changes a call, it adds a line to the tool result naming the right form, so the model can switch to it.
|
|
14
|
+
|
|
15
|
+
OpenCode drops argument names a tool doesn't declare before plugins see a call. So the plugin rebuilds the `edit` and `write` tools' input as the same JSON Schema (same fields, types, descriptions and required list) that also lets extra fields through. The fields models are shown don't change. If a future OpenCode gives those tools fields that aren't plain text or true/false, the plugin leaves their schema as it is.
|
|
16
|
+
|
|
17
|
+
## Install
|
|
18
|
+
|
|
19
|
+
Add it to `opencode.json`:
|
|
20
|
+
|
|
21
|
+
```json
|
|
22
|
+
{ "plugins": ["opencode-tool-repair"] }
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
or copy `index.js` to `~/.config/opencode/plugins/tool-repair.js`. Restart OpenCode. To turn it off, remove it from the list or delete that file.
|
|
26
|
+
|
|
27
|
+
## Test
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
npm test
|
|
31
|
+
```
|
package/index.js
ADDED
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
// opencode-tool-repair (OpenCode 2.x plugin): fixes tool calls that local models
|
|
2
|
+
// send in another agent's format, before OpenCode rejects them.
|
|
3
|
+
//
|
|
4
|
+
// Some local models (Flint, for one) were trained on agents whose tools take
|
|
5
|
+
// different argument names. Their calls fail OpenCode's checks, the model retries
|
|
6
|
+
// the same call, and nothing changes. This plugin rewrites those calls:
|
|
7
|
+
// - edit with an `edits` list (an array or a JSON string of {oldText, newText}):
|
|
8
|
+
// one change becomes a normal edit; several are applied together when each
|
|
9
|
+
// old text is found exactly once, and otherwise the first is applied and
|
|
10
|
+
// the model is told to send the rest one per call,
|
|
11
|
+
// - other names for the same argument: oldText/old_string -> oldString,
|
|
12
|
+
// newText/new_string -> newString, file_path/filePath -> path, and so on,
|
|
13
|
+
// - empty or text values in number and true/false fields ("offset": ""),
|
|
14
|
+
// and an empty "" key,
|
|
15
|
+
// - a garbled newString ("new": "String" plus the text under a "" key),
|
|
16
|
+
// and a note when an edit has no newString at all,
|
|
17
|
+
// - mistyped paths in read, edit, write, glob and grep (an underscore lost or
|
|
18
|
+
// added, a couple of letters lost in a long folder name) when exactly one
|
|
19
|
+
// existing path fits, and a shell workdir that is the project folder
|
|
20
|
+
// misspelled. Paths inside shell commands are never changed: a command
|
|
21
|
+
// like `rm -rf <a folder that doesn't exist>` must fail, not be pointed at
|
|
22
|
+
// a real folder with a similar name.
|
|
23
|
+
// So that those other names reach it, the edit and write tools accept extra
|
|
24
|
+
// fields (see openSchema); the fields models are shown don't change.
|
|
25
|
+
// When a call was changed, the model is told the right form, so it learns it.
|
|
26
|
+
// Remove it from your plugin list (or delete the file) to turn it off.
|
|
27
|
+
|
|
28
|
+
import { existsSync, readFileSync, readdirSync, statSync } from "node:fs"
|
|
29
|
+
import { dirname, isAbsolute, join, relative, sep } from "node:path"
|
|
30
|
+
|
|
31
|
+
const PATH_ALIASES = ["file_path", "filePath", "filepath", "file", "filename"]
|
|
32
|
+
const EDIT_ALIASES = {
|
|
33
|
+
oldString: ["oldText", "old_string", "old_str", "old", "search", "find", "edString"],
|
|
34
|
+
newString: ["newText", "new_string", "new_str", "new", "replace", "replacement"],
|
|
35
|
+
replaceAll: ["replace_all"],
|
|
36
|
+
}
|
|
37
|
+
const NUMBER_FIELDS = new Set(["offset", "limit", "timeout"])
|
|
38
|
+
const BOOLEAN_FIELDS = new Set(["replaceAll", "background", "hidden", "literal", "caseSensitive"])
|
|
39
|
+
const OPTIONAL_TEXT_FIELDS = new Set(["workdir"])
|
|
40
|
+
const FILE_TOOLS = new Set(["read", "edit", "write"])
|
|
41
|
+
const PATH_TOOLS = new Set(["read", "edit", "write", "glob", "grep"])
|
|
42
|
+
const MAX_NOTES = 1000
|
|
43
|
+
|
|
44
|
+
function rename(input, to, aliases) {
|
|
45
|
+
if (input[to] !== undefined) return false
|
|
46
|
+
for (const alias of aliases) {
|
|
47
|
+
if (input[alias] !== undefined) {
|
|
48
|
+
input[to] = input[alias]
|
|
49
|
+
delete input[alias]
|
|
50
|
+
return true
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return false
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Cleans empty and mistyped optional fields. Returns true when it changed something.
|
|
57
|
+
function cleanFields(input) {
|
|
58
|
+
let changed = false
|
|
59
|
+
if ("" in input) {
|
|
60
|
+
delete input[""]
|
|
61
|
+
changed = true
|
|
62
|
+
}
|
|
63
|
+
for (const [key, value] of Object.entries(input)) {
|
|
64
|
+
const optional = NUMBER_FIELDS.has(key) || BOOLEAN_FIELDS.has(key) || OPTIONAL_TEXT_FIELDS.has(key)
|
|
65
|
+
if (optional && (value === "" || value === null)) {
|
|
66
|
+
delete input[key]
|
|
67
|
+
changed = true
|
|
68
|
+
} else if (NUMBER_FIELDS.has(key) && typeof value === "string" && /^\s*\d+\s*$/.test(value)) {
|
|
69
|
+
input[key] = Number(value)
|
|
70
|
+
changed = true
|
|
71
|
+
} else if (BOOLEAN_FIELDS.has(key) && (value === "true" || value === "false")) {
|
|
72
|
+
input[key] = value === "true"
|
|
73
|
+
changed = true
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return changed
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function parseEdits(value) {
|
|
80
|
+
let list = value
|
|
81
|
+
if (typeof list === "string") {
|
|
82
|
+
try {
|
|
83
|
+
list = JSON.parse(list)
|
|
84
|
+
} catch {
|
|
85
|
+
return undefined
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
if (list && typeof list === "object" && !Array.isArray(list)) list = [list]
|
|
89
|
+
if (!Array.isArray(list) || list.length === 0) return undefined
|
|
90
|
+
const edits = []
|
|
91
|
+
for (const item of list) {
|
|
92
|
+
if (!item || typeof item !== "object") return undefined
|
|
93
|
+
const edit = { ...item }
|
|
94
|
+
for (const [to, aliases] of Object.entries(EDIT_ALIASES)) rename(edit, to, aliases)
|
|
95
|
+
if (typeof edit.oldString !== "string" || typeof edit.newString !== "string") return undefined
|
|
96
|
+
edits.push({ oldString: edit.oldString, newString: edit.newString, ...(edit.replaceAll === true ? { replaceAll: true } : {}) })
|
|
97
|
+
}
|
|
98
|
+
return edits
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function count(text, part) {
|
|
102
|
+
if (!part) return 0
|
|
103
|
+
let n = 0
|
|
104
|
+
for (let i = text.indexOf(part); i !== -1; i = text.indexOf(part, i + part.length)) n++
|
|
105
|
+
return n
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Applies every edit to the file's text when each old text is found exactly once
|
|
109
|
+
// (or the edit says replaceAll). Returns the new text, or undefined if any doesn't fit.
|
|
110
|
+
function applyAll(text, edits) {
|
|
111
|
+
const crlf = text.includes("\r\n")
|
|
112
|
+
for (const edit of edits) {
|
|
113
|
+
let { oldString, newString } = edit
|
|
114
|
+
if (crlf && !oldString.includes("\r")) {
|
|
115
|
+
oldString = oldString.replace(/\n/g, "\r\n")
|
|
116
|
+
newString = newString.replace(/\n/g, "\r\n")
|
|
117
|
+
}
|
|
118
|
+
const found = count(text, oldString)
|
|
119
|
+
if (found === 0 || (found > 1 && !edit.replaceAll)) return undefined
|
|
120
|
+
text = edit.replaceAll ? text.split(oldString).join(newString) : text.replace(oldString, () => newString)
|
|
121
|
+
}
|
|
122
|
+
return text
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const squash = (path) => path.replace(/[^a-z0-9]/gi, "").toLowerCase()
|
|
126
|
+
|
|
127
|
+
// Edit distance, giving up (returning max + 1) once it can't stay within max.
|
|
128
|
+
function distance(a, b, max) {
|
|
129
|
+
if (Math.abs(a.length - b.length) > max) return max + 1
|
|
130
|
+
let prev = Array.from({ length: b.length + 1 }, (_, j) => j)
|
|
131
|
+
for (let i = 1; i <= a.length; i++) {
|
|
132
|
+
const cur = [i]
|
|
133
|
+
let best = i
|
|
134
|
+
for (let j = 1; j <= b.length; j++) {
|
|
135
|
+
cur[j] = Math.min(prev[j] + 1, cur[j - 1] + 1, prev[j - 1] + (a[i - 1] === b[j - 1] ? 0 : 1))
|
|
136
|
+
best = Math.min(best, cur[j])
|
|
137
|
+
}
|
|
138
|
+
if (best > max) return max + 1
|
|
139
|
+
prev = cur
|
|
140
|
+
}
|
|
141
|
+
return prev[b.length]
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Folder entries a mistyped name could stand for: the same letters and digits
|
|
145
|
+
// (an underscore or dash added or lost), else a name at most two letters off
|
|
146
|
+
// when the name is long enough for that to be unambiguous.
|
|
147
|
+
function lookalikes(dir, name, strict = false) {
|
|
148
|
+
let entries
|
|
149
|
+
try {
|
|
150
|
+
entries = readdirSync(dir)
|
|
151
|
+
} catch {
|
|
152
|
+
return []
|
|
153
|
+
}
|
|
154
|
+
const want = squash(name)
|
|
155
|
+
if (!want) return []
|
|
156
|
+
const same = entries.filter((e) => squash(e) === want)
|
|
157
|
+
if (same.length || strict || want.length < 8) return same
|
|
158
|
+
return entries.filter((e) => distance(squash(e), want, 2) <= 2)
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const MAX_TRIES = 200
|
|
162
|
+
|
|
163
|
+
// Finds the one existing path a mistyped absolute path stands for. With
|
|
164
|
+
// `newLeaf`, the last part may be a file that doesn't exist yet (a write), so
|
|
165
|
+
// only the folders above it are looked up, only underscore or dash slips count
|
|
166
|
+
// (a new "Scripts2" folder must not land in "Scripts"), and a fix that would
|
|
167
|
+
// overwrite an existing file is refused. Returns undefined when the path
|
|
168
|
+
// exists, when nothing fits, or when more than one path fits.
|
|
169
|
+
export function findPath(path, { newLeaf = false } = {}) {
|
|
170
|
+
if (typeof path !== "string" || !isAbsolute(path) || existsSync(path)) return undefined
|
|
171
|
+
const parts = path.split(sep).filter(Boolean)
|
|
172
|
+
const found = []
|
|
173
|
+
let tries = 0
|
|
174
|
+
const walk = (dir, i) => {
|
|
175
|
+
if (found.length > 1 || ++tries > MAX_TRIES) return
|
|
176
|
+
if (i === parts.length) {
|
|
177
|
+
found.push(dir)
|
|
178
|
+
return
|
|
179
|
+
}
|
|
180
|
+
const name = parts[i]
|
|
181
|
+
const next = join(dir, name)
|
|
182
|
+
const last = i === parts.length - 1
|
|
183
|
+
if (existsSync(next)) return walk(next, i + 1)
|
|
184
|
+
if (last && newLeaf) return walk(next, i + 1)
|
|
185
|
+
for (const entry of lookalikes(dir, name, newLeaf)) walk(join(dir, entry), i + 1)
|
|
186
|
+
}
|
|
187
|
+
walk(sep, 0)
|
|
188
|
+
if (found.length !== 1 || tries > MAX_TRIES) return undefined
|
|
189
|
+
const fixed = found[0]
|
|
190
|
+
if (newLeaf ? !existsSync(dirname(fixed)) || existsSync(fixed) : !existsSync(fixed)) return undefined
|
|
191
|
+
return fixed === path ? undefined : fixed
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const isDir = (path) => {
|
|
195
|
+
try {
|
|
196
|
+
return statSync(path).isDirectory()
|
|
197
|
+
} catch {
|
|
198
|
+
return false
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// OpenCode drops argument names a tool doesn't declare before plugins see the call,
|
|
203
|
+
// so `edits` or `old_string` would be gone by then. This rebuilds a tool's input
|
|
204
|
+
// as the same JSON Schema (names, types, descriptions, required fields) that also
|
|
205
|
+
// lets other fields through. Only tools whose fields are all plain text or
|
|
206
|
+
// true/false are changed; anything else is left as it is.
|
|
207
|
+
const JSON_TYPES = { String: "string", Boolean: "boolean" }
|
|
208
|
+
export function openSchema(input) {
|
|
209
|
+
const fields = input?.fields
|
|
210
|
+
if (!fields || typeof fields !== "object") return undefined
|
|
211
|
+
const properties = {}
|
|
212
|
+
const required = []
|
|
213
|
+
for (const [name, schema] of Object.entries(fields)) {
|
|
214
|
+
const ast = schema?.ast
|
|
215
|
+
const type = JSON_TYPES[ast?._tag]
|
|
216
|
+
if (!type || (Array.isArray(ast.checks) && ast.checks.length)) return undefined
|
|
217
|
+
const description = ast.annotations?.description
|
|
218
|
+
properties[name] = { type, ...(typeof description === "string" ? { description } : {}) }
|
|
219
|
+
if (!ast.context?.isOptional) required.push(name)
|
|
220
|
+
}
|
|
221
|
+
return { type: "object", properties, required, additionalProperties: true }
|
|
222
|
+
}
|
|
223
|
+
const OPEN_TOOLS = ["edit", "write"]
|
|
224
|
+
|
|
225
|
+
export function createPlugin() {
|
|
226
|
+
const notes = new Map() // call id -> note for the model
|
|
227
|
+
|
|
228
|
+
function note(call, text) {
|
|
229
|
+
if (notes.size > MAX_NOTES) notes.clear()
|
|
230
|
+
notes.set(call.id, text)
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
return {
|
|
234
|
+
id: "tool-repair",
|
|
235
|
+
async setup(ctx) {
|
|
236
|
+
const root = ctx?.location?.directory
|
|
237
|
+
|
|
238
|
+
const resolve = (path) => (isAbsolute(path) ? path : root ? join(root, path) : undefined)
|
|
239
|
+
|
|
240
|
+
if (typeof ctx.tool.transform === "function") {
|
|
241
|
+
await ctx.tool.transform((tools) => {
|
|
242
|
+
for (const name of OPEN_TOOLS) {
|
|
243
|
+
const open = openSchema(tools.get(name)?.input)
|
|
244
|
+
if (open) tools.update(name, (tool) => { tool.input = open })
|
|
245
|
+
}
|
|
246
|
+
})
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
await ctx.tool.hook("execute.before", (call) => {
|
|
250
|
+
const input = call.input
|
|
251
|
+
if (!input || typeof input !== "object" || Array.isArray(input)) return
|
|
252
|
+
let changed = false
|
|
253
|
+
if (call.tool === "edit" && typeof input[""] === "string" && input.newString === undefined) {
|
|
254
|
+
// A garbled key: "newString" split into "new": "String" and "": <the text>.
|
|
255
|
+
input.newString = input[""]
|
|
256
|
+
delete input[""]
|
|
257
|
+
if (input.new === "String") delete input.new
|
|
258
|
+
changed = true
|
|
259
|
+
}
|
|
260
|
+
changed = cleanFields(input) || changed
|
|
261
|
+
const pathNotes = []
|
|
262
|
+
|
|
263
|
+
if (FILE_TOOLS.has(call.tool)) changed = rename(input, "path", PATH_ALIASES) || changed
|
|
264
|
+
|
|
265
|
+
if (call.tool === "write") changed = rename(input, "content", ["contents", "text", "file_text", "data"]) || changed
|
|
266
|
+
|
|
267
|
+
if (PATH_TOOLS.has(call.tool) && typeof input.path === "string") {
|
|
268
|
+
const full = resolve(input.path)
|
|
269
|
+
const fixed = full && findPath(full, { newLeaf: call.tool === "write" })
|
|
270
|
+
if (fixed) {
|
|
271
|
+
const was = input.path
|
|
272
|
+
input.path = isAbsolute(was) ? fixed : relative(root, fixed)
|
|
273
|
+
pathNotes.push(`\`${was}\` doesn't exist; used \`${input.path}\``)
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
if (call.tool === "shell") {
|
|
278
|
+
changed = rename(input, "command", ["cmd", "script"]) || changed
|
|
279
|
+
const dir = input.workdir
|
|
280
|
+
if (typeof dir === "string" && !existsSync(dir)) {
|
|
281
|
+
// Only the project folder itself: a lookalike of any other folder could
|
|
282
|
+
// run the command (and anything it deletes) somewhere else.
|
|
283
|
+
const fixed = root && squash(dir) === squash(root) ? root : undefined
|
|
284
|
+
if (fixed && isDir(fixed)) {
|
|
285
|
+
input.workdir = fixed
|
|
286
|
+
pathNotes.push(`workdir \`${dir}\` doesn't exist; used \`${fixed}\``)
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
if (call.tool === "edit") {
|
|
292
|
+
for (const [to, aliases] of Object.entries(EDIT_ALIASES)) changed = rename(input, to, aliases) || changed
|
|
293
|
+
changed = cleanFields(input) || changed // a renamed replace_all may hold "true"
|
|
294
|
+
if (input.edits !== undefined && input.oldString === undefined) {
|
|
295
|
+
const edits = parseEdits(input.edits)
|
|
296
|
+
if (edits) {
|
|
297
|
+
delete input.edits
|
|
298
|
+
const rest = edits.slice(1)
|
|
299
|
+
let content
|
|
300
|
+
if (rest.length && typeof input.path === "string") {
|
|
301
|
+
const file = resolve(input.path)
|
|
302
|
+
try {
|
|
303
|
+
if (file) content = applyAll(readFileSync(file, "utf8"), edits)
|
|
304
|
+
} catch {}
|
|
305
|
+
}
|
|
306
|
+
if (content !== undefined) {
|
|
307
|
+
// All changes fit: write them in one go.
|
|
308
|
+
call.tool = "write"
|
|
309
|
+
for (const key of Object.keys(input)) if (key !== "path") delete input[key]
|
|
310
|
+
input.content = content
|
|
311
|
+
note(call, `tool-repair: applied all ${edits.length} changes from your \`edits\` list. ` +
|
|
312
|
+
"OpenCode's edit tool takes one change per call: {path, oldString, newString}.")
|
|
313
|
+
return
|
|
314
|
+
}
|
|
315
|
+
Object.assign(input, edits[0])
|
|
316
|
+
note(call, rest.length
|
|
317
|
+
? `tool-repair: OpenCode's edit tool takes one change per call ({path, oldString, newString}), so only the ` +
|
|
318
|
+
`first of your ${edits.length} changes was attempted. Send the other ${rest.length} as separate edit calls.`
|
|
319
|
+
: "tool-repair: edit takes {path, oldString, newString}, not an `edits` list; your change was sent that way.")
|
|
320
|
+
return
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
if (typeof input.oldString === "string" && input.newString === undefined && input.edits === undefined) {
|
|
324
|
+
note(call, "tool-repair: your edit call had no newString, so nothing changed. Send path, oldString and " +
|
|
325
|
+
"newString together in one call; to replace most of a file, use write.")
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
if (changed && !notes.has(call.id)) {
|
|
330
|
+
note(call, `tool-repair: your ${call.tool} call had argument names or value types OpenCode doesn't accept ` +
|
|
331
|
+
`(for example "10" where a number belongs); it was fixed and sent with the fields ` +
|
|
332
|
+
`${JSON.stringify(Object.keys(input))}. Use those names, and plain numbers and true/false, next time.`)
|
|
333
|
+
}
|
|
334
|
+
if (pathNotes.length) {
|
|
335
|
+
const text = `tool-repair: ${pathNotes.join("; ")}. Copy paths exactly as tools print them.`
|
|
336
|
+
note(call, notes.has(call.id) ? notes.get(call.id) + "\n" + text : text)
|
|
337
|
+
}
|
|
338
|
+
})
|
|
339
|
+
|
|
340
|
+
await ctx.tool.hook("execute.after", (call) => {
|
|
341
|
+
const text = notes.get(call.id)
|
|
342
|
+
if (text === undefined) return
|
|
343
|
+
notes.delete(call.id)
|
|
344
|
+
if (call.status === "completed" && Array.isArray(call.result?.content)) {
|
|
345
|
+
call.result.content.push({ type: "text", text: "\n\n" + text })
|
|
346
|
+
} else if (call.error && typeof call.error.message === "string") {
|
|
347
|
+
try {
|
|
348
|
+
call.error.message += "\n\n" + text
|
|
349
|
+
} catch {}
|
|
350
|
+
}
|
|
351
|
+
})
|
|
352
|
+
},
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
export default createPlugin()
|
package/package.json
CHANGED
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "opencode-tool-repair",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "OpenCode plugin that fixes tool calls local models send in another agent's format",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"exports": { ".": "./index.js" },
|
|
8
|
+
"files": ["index.js", "README.md", "LICENSE", "CHANGELOG.md"],
|
|
9
|
+
"scripts": { "test": "node --test" },
|
|
10
|
+
"keywords": ["opencode", "opencode-plugin", "agent", "ollama", "tool-calls"],
|
|
11
|
+
"engines": { "node": ">=20" },
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"repository": { "type": "git", "url": "git+https://github.com/amirhakimnejad/opencode-tool-repair.git" }
|
|
14
|
+
}
|