Skip to content

Commit a8c18db

Browse files
committed
fix(core): expose Instance.directory to custom tools
1 parent 6cf2c3e commit a8c18db

4 files changed

Lines changed: 15 additions & 7 deletions

File tree

‎packages/opencode/src/tool/read.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export const ReadTool = Tool.define("read", {
2424
async execute(params, ctx) {
2525
let filepath = params.filePath
2626
if (!path.isAbsolute(filepath)) {
27-
filepath = path.join(Instance.directory, filepath)
27+
filepath = path.resolve(Instance.directory, filepath)
2828
}
2929
const title = path.relative(Instance.worktree, filepath)
3030

‎packages/opencode/src/tool/registry.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { Tool } from "./tool"
1616
import { Instance } from "../project/instance"
1717
import { Config } from "../config/config"
1818
import path from "path"
19-
import { type ToolDefinition } from "@opencode-ai/plugin"
19+
import { type ToolContext as PluginToolContext, type ToolDefinition } from "@opencode-ai/plugin"
2020
import z from "zod"
2121
import { Plugin } from "../plugin"
2222
import { WebSearchTool } from "./websearch"
@@ -67,7 +67,8 @@ export namespace ToolRegistry {
6767
parameters: z.object(def.args),
6868
description: def.description,
6969
execute: async (args, ctx) => {
70-
const result = await def.execute(args as any, ctx)
70+
const pluginCtx = { ...ctx, directory: Instance.directory } as unknown as PluginToolContext
71+
const result = await def.execute(args as any, pluginCtx)
7172
const out = await Truncate.output(result, {}, initCtx?.agent)
7273
return {
7374
title: "",

‎packages/plugin/src/tool.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ export type ToolContext = {
44
sessionID: string
55
messageID: string
66
agent: string
7+
/**
8+
* Current project directory for this session.
9+
* Prefer this over process.cwd() when resolving relative paths.
10+
*/
11+
directory: string
712
abort: AbortSignal
813
metadata(input: { title?: string; metadata?: { [key: string]: any } }): void
914
ask(input: AskInput): Promise<void>

‎packages/web/src/content/docs/custom-tools.mdx‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ export default tool({
120120
args: {},
121121
async execute(args, context) {
122122
// Access context information
123-
const { agent, sessionID, messageID } = context
124-
return `Agent: ${agent}, Session: ${sessionID}, Message: ${messageID}`
123+
const { agent, sessionID, messageID, directory } = context
124+
return `Agent: ${agent}, Session: ${sessionID}, Message: ${messageID}, Directory: ${directory}`
125125
},
126126
})
127127
```
@@ -148,15 +148,17 @@ Then create the tool definition that invokes it:
148148

149149
```ts title=".opencode/tools/python-add.ts" {10}
150150
import { tool } from "@opencode-ai/plugin"
151+
import path from "path"
151152

152153
export default tool({
153154
description: "Add two numbers using Python",
154155
args: {
155156
a: tool.schema.number().describe("First number"),
156157
b: tool.schema.number().describe("Second number"),
157158
},
158-
async execute(args) {
159-
const result = await Bun.$`python3 .opencode/tools/add.py ${args.a} ${args.b}`.text()
159+
async execute(args, context) {
160+
const script = path.join(context.directory, ".opencode/tools/add.py")
161+
const result = await Bun.$`python3 ${script} ${args.a} ${args.b}`.text()
160162
return result.trim()
161163
},
162164
})

0 commit comments

Comments
 (0)