This commit is contained in:
Dax Raad
2025-05-31 18:42:43 -04:00
parent 6d21525e71
commit e97ed735d9
12 changed files with 58 additions and 45 deletions

View File

@@ -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",

View File

@@ -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,
}

View File

@@ -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

View File

@@ -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
}