index.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. serverUrl: Server.url(),
  26. $: Bun.$,
  27. }
  28. const plugins = [...(config.plugin ?? [])]
  29. if (!Flag.OPENCODE_DISABLE_DEFAULT_PLUGINS) {
  30. plugins.push("[email protected]")
  31. plugins.push("[email protected]")
  32. }
  33. for (let plugin of plugins) {
  34. log.info("loading plugin", { path: plugin })
  35. if (!plugin.startsWith("file://")) {
  36. const lastAtIndex = plugin.lastIndexOf("@")
  37. const pkg = lastAtIndex > 0 ? plugin.substring(0, lastAtIndex) : plugin
  38. const version = lastAtIndex > 0 ? plugin.substring(lastAtIndex + 1) : "latest"
  39. plugin = await BunProc.install(pkg, version)
  40. }
  41. const mod = await import(plugin)
  42. for (const [_name, fn] of Object.entries<PluginInstance>(mod)) {
  43. const init = await fn(input)
  44. hooks.push(init)
  45. }
  46. }
  47. return {
  48. hooks,
  49. input,
  50. }
  51. })
  52. export async function trigger<
  53. Name extends Exclude<keyof Required<Hooks>, "auth" | "event" | "tool">,
  54. Input = Parameters<Required<Hooks>[Name]>[0],
  55. Output = Parameters<Required<Hooks>[Name]>[1],
  56. >(name: Name, input: Input, output: Output): Promise<Output> {
  57. if (!name) return output
  58. for (const hook of await state().then((x) => x.hooks)) {
  59. const fn = hook[name]
  60. if (!fn) continue
  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. }
  66. return output
  67. }
  68. export async function list() {
  69. return state().then((x) => x.hooks)
  70. }
  71. export async function init() {
  72. const hooks = await state().then((x) => x.hooks)
  73. const config = await Config.get()
  74. for (const hook of hooks) {
  75. await hook.config?.(config)
  76. }
  77. Bus.subscribeAll(async (input) => {
  78. const hooks = await state().then((x) => x.hooks)
  79. for (const hook of hooks) {
  80. hook["event"]?.({
  81. event: input,
  82. })
  83. }
  84. })
  85. }
  86. }