index.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. import { CodexAuthPlugin } from "./codex"
  11. import { Session } from "../session"
  12. import { NamedError } from "@opencode-ai/util/error"
  13. import { CopilotAuthPlugin } from "./copilot"
  14. export namespace Plugin {
  15. const log = Log.create({ service: "plugin" })
  16. const BUILTIN = ["[email protected]", "@gitlab/[email protected]"]
  17. // Built-in plugins that are directly imported (not installed from npm)
  18. const INTERNAL_PLUGINS: PluginInstance[] = [CodexAuthPlugin, CopilotAuthPlugin]
  19. const state = Instance.state(async () => {
  20. const client = createOpencodeClient({
  21. baseUrl: "http://localhost:4096",
  22. // @ts-ignore - fetch type incompatibility
  23. fetch: async (...args) => Server.App().fetch(...args),
  24. })
  25. const config = await Config.get()
  26. const hooks: Hooks[] = []
  27. const input: PluginInput = {
  28. client,
  29. project: Instance.project,
  30. worktree: Instance.worktree,
  31. directory: Instance.directory,
  32. serverUrl: Server.url(),
  33. $: Bun.$,
  34. }
  35. for (const plugin of INTERNAL_PLUGINS) {
  36. log.info("loading internal plugin", { name: plugin.name })
  37. const init = await plugin(input)
  38. hooks.push(init)
  39. }
  40. const plugins = [...(config.plugin ?? [])]
  41. if (!Flag.OPENCODE_DISABLE_DEFAULT_PLUGINS) {
  42. plugins.push(...BUILTIN)
  43. }
  44. for (let plugin of plugins) {
  45. // ignore old codex plugin since it is supported first party now
  46. if (plugin.includes("opencode-openai-codex-auth") || plugin.includes("opencode-copilot-auth")) continue
  47. log.info("loading plugin", { path: plugin })
  48. if (!plugin.startsWith("file://")) {
  49. const lastAtIndex = plugin.lastIndexOf("@")
  50. const pkg = lastAtIndex > 0 ? plugin.substring(0, lastAtIndex) : plugin
  51. const version = lastAtIndex > 0 ? plugin.substring(lastAtIndex + 1) : "latest"
  52. const builtin = BUILTIN.some((x) => x.startsWith(pkg + "@"))
  53. plugin = await BunProc.install(pkg, version).catch((err) => {
  54. if (!builtin) throw err
  55. const message = err instanceof Error ? err.message : String(err)
  56. log.error("failed to install builtin plugin", {
  57. pkg,
  58. version,
  59. error: message,
  60. })
  61. Bus.publish(Session.Event.Error, {
  62. error: new NamedError.Unknown({
  63. message: `Failed to install built-in plugin ${pkg}@${version}: ${message}`,
  64. }).toObject(),
  65. })
  66. return ""
  67. })
  68. if (!plugin) continue
  69. }
  70. const mod = await import(plugin)
  71. // Prevent duplicate initialization when plugins export the same function
  72. // as both a named export and default export (e.g., `export const X` and `export default X`).
  73. // Object.entries(mod) would return both entries pointing to the same function reference.
  74. const seen = new Set<PluginInstance>()
  75. for (const [_name, fn] of Object.entries<PluginInstance>(mod)) {
  76. if (seen.has(fn)) continue
  77. seen.add(fn)
  78. const init = await fn(input)
  79. hooks.push(init)
  80. }
  81. }
  82. return {
  83. hooks,
  84. input,
  85. }
  86. })
  87. export async function trigger<
  88. Name extends Exclude<keyof Required<Hooks>, "auth" | "event" | "tool">,
  89. Input = Parameters<Required<Hooks>[Name]>[0],
  90. Output = Parameters<Required<Hooks>[Name]>[1],
  91. >(name: Name, input: Input, output: Output): Promise<Output> {
  92. if (!name) return output
  93. for (const hook of await state().then((x) => x.hooks)) {
  94. const fn = hook[name]
  95. if (!fn) continue
  96. // @ts-expect-error if you feel adventurous, please fix the typing, make sure to bump the try-counter if you
  97. // give up.
  98. // try-counter: 2
  99. await fn(input, output)
  100. }
  101. return output
  102. }
  103. export async function list() {
  104. return state().then((x) => x.hooks)
  105. }
  106. export async function init() {
  107. const hooks = await state().then((x) => x.hooks)
  108. const config = await Config.get()
  109. for (const hook of hooks) {
  110. // @ts-expect-error this is because we haven't moved plugin to sdk v2
  111. await hook.config?.(config)
  112. }
  113. Bus.subscribeAll(async (input) => {
  114. const hooks = await state().then((x) => x.hooks)
  115. for (const hook of hooks) {
  116. hook["event"]?.({
  117. event: input,
  118. })
  119. }
  120. })
  121. }
  122. }