index.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 [pkg, version] = plugin.split("@")
  36. plugin = await BunProc.install(pkg, version ?? "latest")
  37. }
  38. const mod = await import(plugin)
  39. for (const [_name, fn] of Object.entries<PluginInstance>(mod)) {
  40. const init = await fn(input)
  41. hooks.push(init)
  42. }
  43. }
  44. return {
  45. hooks,
  46. input,
  47. }
  48. })
  49. export async function trigger<
  50. Name extends Exclude<keyof Required<Hooks>, "auth" | "event" | "tool">,
  51. Input = Parameters<Required<Hooks>[Name]>[0],
  52. Output = Parameters<Required<Hooks>[Name]>[1],
  53. >(name: Name, input: Input, output: Output): Promise<Output> {
  54. if (!name) return output
  55. for (const hook of await state().then((x) => x.hooks)) {
  56. const fn = hook[name]
  57. if (!fn) continue
  58. // @ts-expect-error if you feel adventurous, please fix the typing, make sure to bump the try-counter if you
  59. // give up.
  60. // try-counter: 2
  61. await fn(input, output)
  62. }
  63. return output
  64. }
  65. export async function list() {
  66. return state().then((x) => x.hooks)
  67. }
  68. export async function init() {
  69. const hooks = await state().then((x) => x.hooks)
  70. const config = await Config.get()
  71. for (const hook of hooks) {
  72. await hook.config?.(config)
  73. }
  74. Bus.subscribeAll(async (input) => {
  75. const hooks = await state().then((x) => x.hooks)
  76. for (const hook of hooks) {
  77. hook["event"]?.({
  78. event: input,
  79. })
  80. }
  81. })
  82. }
  83. }