Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/opencode/src/tool/edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ export const EditTool = Tool.define(
return {
description: DESCRIPTION,
parameters: Parameters,
normalizeArguments: (args: unknown) =>
Tool.normalizeAliases(args, {
filePath: ["file_path", "path"],
oldString: ["old_string", "oldText"],
newString: ["new_string", "newText"],
replaceAll: ["replace_all"],
}),
execute: (params: Schema.Schema.Type<typeof Parameters>, ctx: Tool.Context) =>
Effect.gen(function* () {
if (!params.filePath) {
Expand Down
18 changes: 17 additions & 1 deletion packages/opencode/src/tool/tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export interface Def<
description: string
parameters: Parameters
jsonSchema?: JSONSchema7
normalizeArguments?(args: unknown): unknown
execute(args: Schema.Schema.Type<Parameters>, ctx: Context): Effect.Effect<ExecuteResult<M>>
formatValidationError?(error: unknown): string
}
Expand Down Expand Up @@ -94,6 +95,21 @@ export type InferDef<T> =
? Def<P, M>
: never

export function normalizeAliases(input: unknown, aliases: Record<string, string[]>): unknown {
if (!isRecord(input)) return input
const output = { ...input }
for (const [key, names] of Object.entries(aliases)) {
if (output[key] !== undefined) continue
const name = names.find((alias) => input[alias] !== undefined)
if (name) output[key] = input[name]
}
return output
}

function isRecord(input: unknown): input is Record<string, unknown> {
return typeof input === "object" && input !== null && !Array.isArray(input)
}

function wrap<Parameters extends Schema.Decoder<unknown>, Result extends Metadata>(
id: string,
init: Init<Parameters, Result>,
Expand All @@ -116,7 +132,7 @@ function wrap<Parameters extends Schema.Decoder<unknown>, Result extends Metadat
...(ctx.callID ? { "tool.call_id": ctx.callID } : {}),
}
return Effect.gen(function* () {
const decoded = yield* decode(args).pipe(
const decoded = yield* decode(toolInfo.normalizeArguments ? toolInfo.normalizeArguments(args) : args).pipe(
Effect.mapError(
(error) =>
new InvalidArgumentsError({
Expand Down
5 changes: 5 additions & 0 deletions packages/opencode/src/tool/write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ export const WriteTool = Tool.define(
return {
description: DESCRIPTION,
parameters: Parameters,
normalizeArguments: (args: unknown) =>
Tool.normalizeAliases(args, {
filePath: ["file_path", "path"],
content: ["fileContent", "contents"],
}),
execute: (params: { content: string; filePath: string }, ctx: Tool.Context) =>
Effect.gen(function* () {
const instance = yield* InstanceState.context
Expand Down
18 changes: 18 additions & 0 deletions packages/opencode/test/tool/edit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ const run = Effect.fn("EditToolTest.run")(function* (
return yield* tool.execute(args, next)
})

const runUnknown = Effect.fn("EditToolTest.runUnknown")(function* (args: unknown, next: Tool.Context = ctx) {
const tool = yield* init()
const execute = tool.execute as unknown as (args: unknown, ctx: Tool.Context) => ReturnType<typeof tool.execute>
return yield* execute(args, next)
})

const fail = Effect.fn("EditToolTest.fail")(function* (args: Tool.InferParameters<typeof EditTool>) {
const exit = yield* run(args).pipe(Effect.exit)
if (Exit.isFailure(exit)) {
Expand Down Expand Up @@ -157,6 +163,18 @@ describe("tool.edit", () => {
}),
)

it.instance("normalizes common model argument aliases", () =>
Effect.gen(function* () {
const test = yield* TestInstance
const filepath = path.join(test.directory, "alias.txt")
yield* put(filepath, "old content")

yield* runUnknown({ file_path: filepath, old_string: "old", new_string: "new" })

expect(yield* load(filepath)).toBe("new content")
}),
)

it.instance("replaces the first visible line in BOM files", () =>
Effect.gen(function* () {
const test = yield* TestInstance
Expand Down
18 changes: 18 additions & 0 deletions packages/opencode/test/tool/write.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ const run = Effect.fn("WriteToolTest.run")(function* (
return yield* tool.execute(args, next)
})

const runUnknown = Effect.fn("WriteToolTest.runUnknown")(function* (args: unknown, next: Tool.Context = ctx) {
const tool = yield* init()
const execute = tool.execute as unknown as (args: unknown, ctx: Tool.Context) => ReturnType<typeof tool.execute>
return yield* execute(args, next)
})

describe("tool.write", () => {
describe("new file creation", () => {
it.instance("writes content to new file", () =>
Expand All @@ -71,6 +77,18 @@ describe("tool.write", () => {
}),
)

it.instance("normalizes common model argument aliases", () =>
Effect.gen(function* () {
const test = yield* TestInstance
const filepath = path.join(test.directory, "alias.txt")

yield* runUnknown({ path: filepath, fileContent: "alias content" })

const content = yield* Effect.promise(() => fs.readFile(filepath, "utf-8"))
expect(content).toBe("alias content")
}),
)

it.instance("creates parent directories if needed", () =>
Effect.gen(function* () {
const test = yield* TestInstance
Expand Down
Loading