fix(core): expose Instance.directory to custom tools
This commit is contained in:
@@ -24,7 +24,7 @@ export const ReadTool = Tool.define("read", {
|
|||||||
async execute(params, ctx) {
|
async execute(params, ctx) {
|
||||||
let filepath = params.filePath
|
let filepath = params.filePath
|
||||||
if (!path.isAbsolute(filepath)) {
|
if (!path.isAbsolute(filepath)) {
|
||||||
filepath = path.join(Instance.directory, filepath)
|
filepath = path.resolve(Instance.directory, filepath)
|
||||||
}
|
}
|
||||||
const title = path.relative(Instance.worktree, filepath)
|
const title = path.relative(Instance.worktree, filepath)
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ import { Tool } from "./tool"
|
|||||||
import { Instance } from "../project/instance"
|
import { Instance } from "../project/instance"
|
||||||
import { Config } from "../config/config"
|
import { Config } from "../config/config"
|
||||||
import path from "path"
|
import path from "path"
|
||||||
import { type ToolDefinition } from "@opencode-ai/plugin"
|
import { type ToolContext as PluginToolContext, type ToolDefinition } from "@opencode-ai/plugin"
|
||||||
import z from "zod"
|
import z from "zod"
|
||||||
import { Plugin } from "../plugin"
|
import { Plugin } from "../plugin"
|
||||||
import { WebSearchTool } from "./websearch"
|
import { WebSearchTool } from "./websearch"
|
||||||
@@ -67,7 +67,8 @@ export namespace ToolRegistry {
|
|||||||
parameters: z.object(def.args),
|
parameters: z.object(def.args),
|
||||||
description: def.description,
|
description: def.description,
|
||||||
execute: async (args, ctx) => {
|
execute: async (args, ctx) => {
|
||||||
const result = await def.execute(args as any, ctx)
|
const pluginCtx = { ...ctx, directory: Instance.directory } as unknown as PluginToolContext
|
||||||
|
const result = await def.execute(args as any, pluginCtx)
|
||||||
const out = await Truncate.output(result, {}, initCtx?.agent)
|
const out = await Truncate.output(result, {}, initCtx?.agent)
|
||||||
return {
|
return {
|
||||||
title: "",
|
title: "",
|
||||||
|
|||||||
@@ -4,6 +4,11 @@ export type ToolContext = {
|
|||||||
sessionID: string
|
sessionID: string
|
||||||
messageID: string
|
messageID: string
|
||||||
agent: string
|
agent: string
|
||||||
|
/**
|
||||||
|
* Current project directory for this session.
|
||||||
|
* Prefer this over process.cwd() when resolving relative paths.
|
||||||
|
*/
|
||||||
|
directory: string
|
||||||
abort: AbortSignal
|
abort: AbortSignal
|
||||||
metadata(input: { title?: string; metadata?: { [key: string]: any } }): void
|
metadata(input: { title?: string; metadata?: { [key: string]: any } }): void
|
||||||
ask(input: AskInput): Promise<void>
|
ask(input: AskInput): Promise<void>
|
||||||
|
|||||||
@@ -120,8 +120,8 @@ export default tool({
|
|||||||
args: {},
|
args: {},
|
||||||
async execute(args, context) {
|
async execute(args, context) {
|
||||||
// Access context information
|
// Access context information
|
||||||
const { agent, sessionID, messageID } = context
|
const { agent, sessionID, messageID, directory } = context
|
||||||
return `Agent: ${agent}, Session: ${sessionID}, Message: ${messageID}`
|
return `Agent: ${agent}, Session: ${sessionID}, Message: ${messageID}, Directory: ${directory}`
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
@@ -148,6 +148,7 @@ Then create the tool definition that invokes it:
|
|||||||
|
|
||||||
```ts title=".opencode/tools/python-add.ts" {10}
|
```ts title=".opencode/tools/python-add.ts" {10}
|
||||||
import { tool } from "@opencode-ai/plugin"
|
import { tool } from "@opencode-ai/plugin"
|
||||||
|
import path from "path"
|
||||||
|
|
||||||
export default tool({
|
export default tool({
|
||||||
description: "Add two numbers using Python",
|
description: "Add two numbers using Python",
|
||||||
@@ -155,8 +156,9 @@ export default tool({
|
|||||||
a: tool.schema.number().describe("First number"),
|
a: tool.schema.number().describe("First number"),
|
||||||
b: tool.schema.number().describe("Second number"),
|
b: tool.schema.number().describe("Second number"),
|
||||||
},
|
},
|
||||||
async execute(args) {
|
async execute(args, context) {
|
||||||
const result = await Bun.$`python3 .opencode/tools/add.py ${args.a} ${args.b}`.text()
|
const script = path.join(context.directory, ".opencode/tools/add.py")
|
||||||
|
const result = await Bun.$`python3 ${script} ${args.a} ${args.b}`.text()
|
||||||
return result.trim()
|
return result.trim()
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user