mirror of
https://github.com/aljazceru/opencode.git
synced 2025-12-24 19:24:22 +01:00
sync
This commit is contained in:
@@ -25,6 +25,7 @@
|
||||
"ai": "catalog:",
|
||||
"cac": "6.7.14",
|
||||
"decimal.js": "10.5.0",
|
||||
"diff": "8.0.2",
|
||||
"env-paths": "3.0.0",
|
||||
"hono": "4.7.10",
|
||||
"hono-openapi": "0.4.8",
|
||||
|
||||
@@ -3,6 +3,7 @@ import * as path from "path"
|
||||
import { Tool } from "./tool"
|
||||
import { FileTimes } from "./util/file-times"
|
||||
import { LSP } from "../lsp"
|
||||
import { diffLines } from "diff"
|
||||
|
||||
const DESCRIPTION = `Edits files by replacing text, creating new files, or deleting content. For moving or renaming files, use the Bash tool with the 'mv' command instead. For larger file edits, use the FileWrite tool to overwrite files.
|
||||
|
||||
@@ -70,8 +71,11 @@ export const EditTool = Tool.define({
|
||||
filePath = path.join(process.cwd(), filePath)
|
||||
}
|
||||
|
||||
let contentOld = ""
|
||||
let contentNew = ""
|
||||
await (async () => {
|
||||
if (params.oldString === "") {
|
||||
contentNew = params.newString
|
||||
await Bun.write(filePath, params.newString)
|
||||
return
|
||||
}
|
||||
@@ -91,26 +95,28 @@ export const EditTool = Tool.define({
|
||||
`File ${filePath} has been modified since it was last read.\nLast modification: ${read.toISOString()}\nLast read: ${stats.mtime.toISOString()}\n\nPlease read the file again before modifying it.`,
|
||||
)
|
||||
|
||||
const content = await file.text()
|
||||
const index = content.indexOf(params.oldString)
|
||||
contentOld = await file.text()
|
||||
const index = contentOld.indexOf(params.oldString)
|
||||
if (index === -1)
|
||||
throw new Error(
|
||||
`oldString not found in file. Make sure it matches exactly, including whitespace and line breaks`,
|
||||
)
|
||||
const lastIndex = content.lastIndexOf(params.oldString)
|
||||
const lastIndex = contentOld.lastIndexOf(params.oldString)
|
||||
if (index !== lastIndex)
|
||||
throw new Error(
|
||||
`oldString appears multiple times in the file. Please provide more context to ensure a unique match`,
|
||||
)
|
||||
|
||||
const newContent =
|
||||
content.substring(0, index) +
|
||||
contentNew =
|
||||
contentOld.substring(0, index) +
|
||||
params.newString +
|
||||
content.substring(index + params.oldString.length)
|
||||
contentOld.substring(index + params.oldString.length)
|
||||
|
||||
await file.write(newContent)
|
||||
await file.write(contentNew)
|
||||
})()
|
||||
|
||||
const changes = diffLines(contentOld, contentNew)
|
||||
|
||||
FileTimes.write(filePath)
|
||||
FileTimes.read(filePath)
|
||||
|
||||
@@ -129,6 +135,7 @@ export const EditTool = Tool.define({
|
||||
return {
|
||||
metadata: {
|
||||
diagnostics,
|
||||
changes,
|
||||
},
|
||||
output,
|
||||
}
|
||||
|
||||
@@ -53,6 +53,9 @@ export namespace Log {
|
||||
error(message?: any, extra?: Record<string, any>) {
|
||||
write.err(build(message, extra))
|
||||
},
|
||||
warn(message?: any, extra?: Record<string, any>) {
|
||||
write.err(build(message, extra))
|
||||
},
|
||||
tag(key: string, value: string) {
|
||||
if (tags) tags[key] = value
|
||||
return result
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
export const foo: string = "42"
|
||||
export const bar: number = 123
|
||||
|
||||
export function dummyFunction(): void {
|
||||
console.log("This is a dummy function")
|
||||
}
|
||||
|
||||
export function randomHelper(): boolean {
|
||||
return Math.random() > 0.5
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user