index.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import type {
  2. Event,
  3. createOpencodeClient,
  4. Project,
  5. Model,
  6. Provider,
  7. Permission,
  8. UserMessage,
  9. Part,
  10. Auth,
  11. Config,
  12. } from "@opencode-ai/sdk"
  13. import type { BunShell } from "./shell"
  14. import { type ToolDefinition } from "./tool"
  15. export * from "./tool"
  16. export type PluginInput = {
  17. client: ReturnType<typeof createOpencodeClient>
  18. project: Project
  19. directory: string
  20. worktree: string
  21. $: BunShell
  22. }
  23. export type Plugin = (input: PluginInput) => Promise<Hooks>
  24. export interface Hooks {
  25. event?: (input: { event: Event }) => Promise<void>
  26. config?: (input: Config) => Promise<void>
  27. tool?: {
  28. [key: string]: ToolDefinition
  29. }
  30. auth?: {
  31. provider: string
  32. loader?: (auth: () => Promise<Auth>, provider: Provider) => Promise<Record<string, any>>
  33. methods: (
  34. | {
  35. type: "oauth"
  36. label: string
  37. authorize(): Promise<
  38. { url: string; instructions: string } & (
  39. | {
  40. method: "auto"
  41. callback(): Promise<
  42. | ({
  43. type: "success"
  44. } & (
  45. | {
  46. refresh: string
  47. access: string
  48. expires: number
  49. }
  50. | { key: string }
  51. ))
  52. | {
  53. type: "failed"
  54. }
  55. >
  56. }
  57. | {
  58. method: "code"
  59. callback(code: string): Promise<
  60. | ({
  61. type: "success"
  62. } & (
  63. | {
  64. refresh: string
  65. access: string
  66. expires: number
  67. }
  68. | { key: string }
  69. ))
  70. | {
  71. type: "failed"
  72. }
  73. >
  74. }
  75. )
  76. >
  77. }
  78. | { type: "api"; label: string }
  79. )[]
  80. }
  81. /**
  82. * Called when a new message is received
  83. */
  84. "chat.message"?: (input: {}, output: { message: UserMessage; parts: Part[] }) => Promise<void>
  85. /**
  86. * Modify parameters sent to LLM
  87. */
  88. "chat.params"?: (
  89. input: { model: Model; provider: Provider; message: UserMessage },
  90. output: { temperature: number; topP: number; options: Record<string, any> },
  91. ) => Promise<void>
  92. "permission.ask"?: (input: Permission, output: { status: "ask" | "deny" | "allow" }) => Promise<void>
  93. "tool.execute.before"?: (
  94. input: { tool: string; sessionID: string; callID: string },
  95. output: { args: any },
  96. ) => Promise<void>
  97. "tool.execute.after"?: (
  98. input: { tool: string; sessionID: string; callID: string },
  99. output: {
  100. title: string
  101. output: string
  102. metadata: any
  103. },
  104. ) => Promise<void>
  105. }