2026-02-19 17:09:46 +09:00
|
|
|
import { tool, type ToolContext, type ToolDefinition } from "@opencode-ai/plugin/tool"
|
2026-02-22 14:13:59 +09:00
|
|
|
import { executeHashlineEditTool } from "./hashline-edit-executor"
|
2026-02-22 03:38:04 +09:00
|
|
|
import { HASHLINE_EDIT_DESCRIPTION } from "./tool-description"
|
2026-02-23 18:51:32 +09:00
|
|
|
import type { RawHashlineEdit } from "./normalize-edits"
|
2026-03-23 18:22:49 +09:00
|
|
|
import type { PluginContext } from "../../plugin/types"
|
2026-02-16 16:32:33 +09:00
|
|
|
|
|
|
|
|
interface HashlineEditArgs {
|
2026-02-19 17:09:46 +09:00
|
|
|
filePath: string
|
2026-02-23 18:51:32 +09:00
|
|
|
edits: RawHashlineEdit[]
|
2026-02-22 03:38:04 +09:00
|
|
|
delete?: boolean
|
|
|
|
|
rename?: string
|
2026-02-16 16:32:33 +09:00
|
|
|
}
|
|
|
|
|
|
2026-03-23 18:22:49 +09:00
|
|
|
export function createHashlineEditTool(ctx?: PluginContext): ToolDefinition {
|
2026-02-16 16:32:33 +09:00
|
|
|
return tool({
|
2026-02-22 03:38:04 +09:00
|
|
|
description: HASHLINE_EDIT_DESCRIPTION,
|
2026-02-16 16:32:33 +09:00
|
|
|
args: {
|
2026-02-19 17:09:46 +09:00
|
|
|
filePath: tool.schema.string().describe("Absolute path to the file to edit"),
|
2026-02-22 03:38:04 +09:00
|
|
|
delete: tool.schema.boolean().optional().describe("Delete file instead of editing"),
|
|
|
|
|
rename: tool.schema.string().optional().describe("Rename output file path after edits"),
|
2026-02-16 16:32:33 +09:00
|
|
|
edits: tool.schema
|
|
|
|
|
.array(
|
2026-02-23 18:51:32 +09:00
|
|
|
tool.schema.object({
|
2026-02-24 03:00:38 +09:00
|
|
|
op: tool.schema
|
2026-02-23 18:51:32 +09:00
|
|
|
.union([
|
|
|
|
|
tool.schema.literal("replace"),
|
|
|
|
|
tool.schema.literal("append"),
|
|
|
|
|
tool.schema.literal("prepend"),
|
|
|
|
|
])
|
2026-02-24 03:00:38 +09:00
|
|
|
.describe("Hashline edit operation mode"),
|
|
|
|
|
pos: tool.schema.string().optional().describe("Primary anchor in LINE#ID format"),
|
|
|
|
|
end: tool.schema.string().optional().describe("Range end anchor in LINE#ID format"),
|
|
|
|
|
lines: tool.schema
|
2026-03-17 16:40:40 +09:00
|
|
|
.union([tool.schema.array(tool.schema.string()), tool.schema.string(), tool.schema.null()])
|
2026-03-10 17:18:14 +09:00
|
|
|
.describe("Replacement or inserted lines as newline-delimited string. null deletes with replace"),
|
2026-02-23 18:51:32 +09:00
|
|
|
})
|
2026-02-16 16:32:33 +09:00
|
|
|
)
|
2026-02-22 03:38:04 +09:00
|
|
|
.describe("Array of edit operations to apply (empty when delete=true)"),
|
2026-02-16 16:32:33 +09:00
|
|
|
},
|
2026-03-23 18:22:49 +09:00
|
|
|
execute: async (args: HashlineEditArgs, context: ToolContext) => executeHashlineEditTool(args, context, ctx),
|
2026-02-16 16:32:33 +09:00
|
|
|
})
|
|
|
|
|
}
|