index.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. fetch: async (...args) => Server.App().fetch(...args),
  16. })
  17. const config = await Config.get()
  18. const hooks = []
  19. const input: PluginInput = {
  20. client,
  21. project: Instance.project,
  22. worktree: Instance.worktree,
  23. directory: Instance.directory,
  24. $: Bun.$,
  25. }
  26. const plugins = [...(config.plugin ?? [])]
  27. if (!Flag.OPENCODE_DISABLE_DEFAULT_PLUGINS) {
  28. plugins.push("[email protected]")
  29. plugins.push("[email protected]")
  30. }
  31. for (let plugin of plugins) {
  32. log.info("loading plugin", { path: plugin })
  33. if (!plugin.startsWith("file://")) {
  34. const [pkg, version] = plugin.split("@")
  35. plugin = await BunProc.install(pkg, version ?? "latest")
  36. }
  37. const mod = await import(plugin)
  38. for (const [_name, fn] of Object.entries<PluginInstance>(mod)) {
  39. const init = await fn(input)
  40. hooks.push(init)
  41. }
  42. }
  43. return {
  44. hooks,
  45. input,
  46. }
  47. })
  48. export async function trigger<
  49. Name extends Exclude<keyof Required<Hooks>, "auth" | "event" | "tool">,
  50. Input = Parameters<Required<Hooks>[Name]>[0],
  51. Output = Parameters<Required<Hooks>[Name]>[1],
  52. >(name: Name, input: Input, output: Output): Promise<Output> {
  53. if (!name) return output
  54. for (const hook of await state().then((x) => x.hooks)) {
  55. const fn = hook[name]
  56. if (!fn) continue
  57. // @ts-expect-error if you feel adventurous, please fix the typing, make sure to bump the try-counter if you
  58. // give up.
  59. // try-counter: 2
  60. await fn(input, output)
  61. }
  62. return output
  63. }
  64. export async function list() {
  65. return state().then((x) => x.hooks)
  66. }
  67. export async function init() {
  68. const hooks = await state().then((x) => x.hooks)
  69. const config = await Config.get()
  70. for (const hook of hooks) {
  71. await hook.config?.(config)
  72. }
  73. Bus.subscribeAll(async (input) => {
  74. const hooks = await state().then((x) => x.hooks)
  75. for (const hook of hooks) {
  76. hook["event"]?.({
  77. event: input,
  78. })
  79. }
  80. })
  81. }
  82. }