index.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import type { Hooks, PluginInput, Plugin as PluginInstance } from "@opencode-ai/plugin"
  2. import { Config } from "../config/config"
  3. import { Bus } from "../bus"
  4. import { Log } from "../util/log"
  5. import { createOpencodeClient } from "@opencode-ai/sdk"
  6. import { Server } from "../server/server"
  7. import { BunProc } from "../bun"
  8. import { Instance } from "../project/instance"
  9. import { Flag } from "../flag/flag"
  10. export namespace Plugin {
  11. const log = Log.create({ service: "plugin" })
  12. const state = Instance.state(async () => {
  13. const client = createOpencodeClient({
  14. baseUrl: "http://localhost:4096",
  15. // @ts-ignore - fetch type incompatibility
  16. fetch: async (...args) => Server.App().fetch(...args),
  17. })
  18. const config = await Config.get()
  19. const hooks = []
  20. const input: PluginInput = {
  21. client,
  22. project: Instance.project,
  23. worktree: Instance.worktree,
  24. directory: Instance.directory,
  25. $: Bun.$,
  26. }
  27. const plugins = [...(config.plugin ?? [])]
  28. if (!Flag.OPENCODE_DISABLE_DEFAULT_PLUGINS) {
  29. plugins.push("[email protected]")
  30. plugins.push("[email protected]")
  31. }
  32. for (let plugin of plugins) {
  33. log.info("loading plugin", { path: plugin })
  34. if (!plugin.startsWith("file://")) {
  35. const lastAtIndex = plugin.lastIndexOf("@")
  36. const pkg = lastAtIndex > 0 ? plugin.substring(0, lastAtIndex) : plugin
  37. const version = lastAtIndex > 0 ? plugin.substring(lastAtIndex + 1) : "latest"
  38. plugin = await BunProc.install(pkg, version)
  39. }
  40. const mod = await import(plugin)
  41. for (const [_name, fn] of Object.entries<PluginInstance>(mod)) {
  42. const init = await fn(input)
  43. hooks.push(init)
  44. }
  45. }
  46. return {
  47. hooks,
  48. input,
  49. }
  50. })
  51. export async function trigger<
  52. Name extends Exclude<keyof Required<Hooks>, "auth" | "event" | "tool">,
  53. Input = Parameters<Required<Hooks>[Name]>[0],
  54. Output = Parameters<Required<Hooks>[Name]>[1],
  55. >(name: Name, input: Input, output: Output): Promise<Output> {
  56. if (!name) return output
  57. for (const hook of await state().then((x) => x.hooks)) {
  58. const fn = hook[name]
  59. if (!fn) continue
  60. // @ts-expect-error if you feel adventurous, please fix the typing, make sure to bump the try-counter if you
  61. // give up.
  62. // try-counter: 2
  63. await fn(input, output)
  64. }
  65. return output
  66. }
  67. export async function list() {
  68. return state().then((x) => x.hooks)
  69. }
  70. export async function init() {
  71. const hooks = await state().then((x) => x.hooks)
  72. const config = await Config.get()
  73. for (const hook of hooks) {
  74. await hook.config?.(config)
  75. }
  76. Bus.subscribeAll(async (input) => {
  77. const hooks = await state().then((x) => x.hooks)
  78. for (const hook of hooks) {
  79. hook["event"]?.({
  80. event: input,
  81. })
  82. }
  83. })
  84. }
  85. }