2
0

index.ts 2.7 KB

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