fix: use ctx.directory instead of process.cwd() in tools for Desktop app support
Convert grep, glob, ast-grep, and session-manager tools from static exports to factory functions that receive PluginInput context. This allows them to use ctx.directory instead of process.cwd(), fixing issue #658 where tools search from wrong directory in OpenCode Desktop app. Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
@@ -1,3 +1 @@
|
||||
import { grep } from "./tools"
|
||||
|
||||
export { grep }
|
||||
export { createGrepTools } from "./tools"
|
||||
|
||||
+40
-36
@@ -1,42 +1,46 @@
|
||||
import type { PluginInput } from "@opencode-ai/plugin"
|
||||
import { tool, type ToolDefinition } from "@opencode-ai/plugin/tool"
|
||||
import { runRg } from "./cli"
|
||||
import { formatGrepResult } from "./utils"
|
||||
|
||||
export const grep: ToolDefinition = tool({
|
||||
description:
|
||||
"Fast content search tool with safety limits (60s timeout, 10MB output). " +
|
||||
"Searches file contents using regular expressions. " +
|
||||
"Supports full regex syntax (eg. \"log.*Error\", \"function\\s+\\w+\", etc.). " +
|
||||
"Filter files by pattern with the include parameter (eg. \"*.js\", \"*.{ts,tsx}\"). " +
|
||||
"Returns file paths with matches sorted by modification time.",
|
||||
args: {
|
||||
pattern: tool.schema.string().describe("The regex pattern to search for in file contents"),
|
||||
include: tool.schema
|
||||
.string()
|
||||
.optional()
|
||||
.describe("File pattern to include in the search (e.g. \"*.js\", \"*.{ts,tsx}\")"),
|
||||
path: tool.schema
|
||||
.string()
|
||||
.optional()
|
||||
.describe("The directory to search in. Defaults to the current working directory."),
|
||||
},
|
||||
execute: async (args) => {
|
||||
try {
|
||||
const globs = args.include ? [args.include] : undefined
|
||||
// Use process.cwd() as the default search path when no path is provided
|
||||
const searchPath = args.path ?? process.cwd()
|
||||
const paths = [searchPath]
|
||||
export function createGrepTools(ctx: PluginInput): Record<string, ToolDefinition> {
|
||||
const grep: ToolDefinition = tool({
|
||||
description:
|
||||
"Fast content search tool with safety limits (60s timeout, 10MB output). " +
|
||||
"Searches file contents using regular expressions. " +
|
||||
"Supports full regex syntax (eg. \"log.*Error\", \"function\\s+\\w+\", etc.). " +
|
||||
"Filter files by pattern with the include parameter (eg. \"*.js\", \"*.{ts,tsx}\"). " +
|
||||
"Returns file paths with matches sorted by modification time.",
|
||||
args: {
|
||||
pattern: tool.schema.string().describe("The regex pattern to search for in file contents"),
|
||||
include: tool.schema
|
||||
.string()
|
||||
.optional()
|
||||
.describe("File pattern to include in the search (e.g. \"*.js\", \"*.{ts,tsx}\")"),
|
||||
path: tool.schema
|
||||
.string()
|
||||
.optional()
|
||||
.describe("The directory to search in. Defaults to the current working directory."),
|
||||
},
|
||||
execute: async (args) => {
|
||||
try {
|
||||
const globs = args.include ? [args.include] : undefined
|
||||
const searchPath = args.path ?? ctx.directory
|
||||
const paths = [searchPath]
|
||||
|
||||
const result = await runRg({
|
||||
pattern: args.pattern,
|
||||
paths,
|
||||
globs,
|
||||
context: 0,
|
||||
})
|
||||
const result = await runRg({
|
||||
pattern: args.pattern,
|
||||
paths,
|
||||
globs,
|
||||
context: 0,
|
||||
})
|
||||
|
||||
return formatGrepResult(result)
|
||||
} catch (e) {
|
||||
return `Error: ${e instanceof Error ? e.message : String(e)}`
|
||||
}
|
||||
},
|
||||
})
|
||||
return formatGrepResult(result)
|
||||
} catch (e) {
|
||||
return `Error: ${e instanceof Error ? e.message : String(e)}`
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
return { grep }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user