index.ts 2.8 KB

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