I write a opencode custom tool that using semble as a builtin tool.
Name it semble_search.ts and put in under .opencode/tools, restart opencode and it will work.
Of course you need install semble first.
import { tool } from "@opencode-ai/plugin";
export default tool({
description: "使用 Semble 进行快速且准确的代码库语义和词法搜索(支持自然语言或代码查询)。",
args: {
query: tool.schema
.string()
.describe("用于搜索的自然语言描述或代码片段(例如:'authentication flow' 或 'save_pretrained')"),
path: tool.schema
.string()
.optional()
.describe("要搜索的本地路径或 Git 仓库 URL。如果未提供,默认使用当前项目的工作目录。"),
top_k: tool.schema
.number()
.optional()
.describe("返回的搜索结果数量。默认为 5。"),
include_text_files: tool.schema
.boolean()
.optional()
.describe("是否同时索引和搜索非代码文本文件(如 .md、.yaml、.json 等)。"),
},
async execute(args, context) {
const cmdArgs: string[] = ["search"];
// 处理可选参数 -k / --top-k
if (args.top_k !== undefined) {
cmdArgs.push("-k", args.top_k.toString());
}
// 处理可选参数 --include-text-files
if (args.include_text_files) {
cmdArgs.push("--include-text-files");
}
// 添加必填的 query 参数
cmdArgs.push(args.query);
// 确定搜索路径:优先使用用户传入的 path,其次使用 OpenCode 当前会话的目录
const searchPath = args.path || context.directory;
if (searchPath) {
cmdArgs.push(searchPath);
}
try {
// 使用 Bun.$ 安全地执行 shell 命令,Bun 会自动对数组参数进行转义处理以防止注入攻击
const output = await Bun.$`semble ${cmdArgs}`.text();
return output;
} catch (error: any) {
// 捕获可能发生的错误(如 semble 未安装或执行失败)
const stderr = error.stderr?.toString();
const message = error.message || error;
return `执行 'semble search' 失败。\n错误详情: ${stderr || message}\n请确保已通过 'uv tool install semble' 安装了 semble。`;
}
},
});
I write a opencode custom tool that using semble as a builtin tool.
Name it
semble_search.tsand put in under.opencode/tools, restart opencode and it will work.Of course you need install semble first.