registry.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import { BashTool } from "./bash"
  2. import { EditTool } from "./edit"
  3. import { GlobTool } from "./glob"
  4. import { GrepTool } from "./grep"
  5. import { ListTool } from "./ls"
  6. import { PatchTool } from "./patch"
  7. import { ReadTool } from "./read"
  8. import { TaskTool } from "./task"
  9. import { TodoWriteTool, TodoReadTool } from "./todo"
  10. import { WebFetchTool } from "./webfetch"
  11. import { WriteTool } from "./write"
  12. import { InvalidTool } from "./invalid"
  13. import type { Agent } from "../agent/agent"
  14. import { Tool } from "./tool"
  15. import { Instance } from "../project/instance"
  16. import { Config } from "../config/config"
  17. import path from "path"
  18. import { type ToolDefinition } from "@opencode-ai/plugin"
  19. import z from "zod/v4"
  20. import { Plugin } from "../plugin"
  21. export namespace ToolRegistry {
  22. // Built-in tools that ship with opencode
  23. const BUILTIN = [
  24. InvalidTool,
  25. BashTool,
  26. EditTool,
  27. WebFetchTool,
  28. GlobTool,
  29. GrepTool,
  30. ListTool,
  31. PatchTool,
  32. ReadTool,
  33. WriteTool,
  34. TodoWriteTool,
  35. TodoReadTool,
  36. TaskTool,
  37. ]
  38. export const state = Instance.state(async () => {
  39. const custom = [] as Tool.Info[]
  40. const glob = new Bun.Glob("tool/*.{js,ts}")
  41. for (const dir of await Config.directories()) {
  42. for await (const match of glob.scan({ cwd: dir, absolute: true, followSymlinks: true, dot: true })) {
  43. const namespace = path.basename(match, path.extname(match))
  44. const mod = await import(match)
  45. for (const [id, def] of Object.entries<ToolDefinition>(mod)) {
  46. custom.push(fromPlugin(id === "default" ? namespace : `${namespace}_${id}`, def))
  47. }
  48. }
  49. }
  50. const plugins = await Plugin.list()
  51. for (const plugin of plugins) {
  52. for (const [id, def] of Object.entries(plugin.tool ?? {})) {
  53. custom.push(fromPlugin(id, def))
  54. }
  55. }
  56. return { custom }
  57. })
  58. function fromPlugin(id: string, def: ToolDefinition): Tool.Info {
  59. return {
  60. id,
  61. init: async () => ({
  62. parameters: z.object(def.args),
  63. description: def.description,
  64. execute: async (args, ctx) => {
  65. const result = await def.execute(args as any, ctx)
  66. return {
  67. title: "",
  68. output: result,
  69. metadata: {},
  70. }
  71. },
  72. }),
  73. }
  74. }
  75. export async function register(tool: Tool.Info) {
  76. const { custom } = await state()
  77. const idx = custom.findIndex((t) => t.id === tool.id)
  78. if (idx >= 0) {
  79. custom.splice(idx, 1, tool)
  80. return
  81. }
  82. custom.push(tool)
  83. }
  84. async function all(): Promise<Tool.Info[]> {
  85. const custom = await state().then((x) => x.custom)
  86. return [...BUILTIN, ...custom]
  87. }
  88. export async function ids() {
  89. return all().then((x) => x.map((t) => t.id))
  90. }
  91. export async function tools(_providerID: string, _modelID: string) {
  92. const tools = await all()
  93. const result = await Promise.all(
  94. tools.map(async (t) => ({
  95. id: t.id,
  96. ...(await t.init()),
  97. })),
  98. )
  99. return result
  100. }
  101. export async function enabled(
  102. _providerID: string,
  103. _modelID: string,
  104. agent: Agent.Info,
  105. ): Promise<Record<string, boolean>> {
  106. const result: Record<string, boolean> = {}
  107. result["patch"] = false
  108. if (agent.permission.edit === "deny") {
  109. result["edit"] = false
  110. result["patch"] = false
  111. result["write"] = false
  112. }
  113. if (agent.permission.bash["*"] === "deny" && Object.keys(agent.permission.bash).length === 1) {
  114. result["bash"] = false
  115. }
  116. if (agent.permission.webfetch === "deny") {
  117. result["webfetch"] = false
  118. }
  119. return result
  120. }
  121. }