index.ts 2.8 KB

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