index.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. try {
  61. // @ts-expect-error if you feel adventurous, please fix the typing, make sure to bump the try-counter if you
  62. // give up.
  63. // try-counter: 2
  64. await fn(input, output)
  65. } catch (e) {
  66. log.error("failed to trigger hook", { name, error: e })
  67. }
  68. }
  69. return output
  70. }
  71. export async function list() {
  72. return state().then((x) => x.hooks)
  73. }
  74. export async function init() {
  75. const hooks = await state().then((x) => x.hooks)
  76. const config = await Config.get()
  77. for (const hook of hooks) {
  78. await hook.config?.(config)
  79. }
  80. Bus.subscribeAll(async (input) => {
  81. const hooks = await state().then((x) => x.hooks)
  82. for (const hook of hooks) {
  83. hook["event"]?.({
  84. event: input,
  85. })
  86. }
  87. })
  88. }
  89. }