index.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. import type {
  2. Event,
  3. createOpencodeClient,
  4. Project,
  5. Model,
  6. Provider,
  7. Permission,
  8. UserMessage,
  9. Message,
  10. Part,
  11. Auth,
  12. Config,
  13. } from "@opencode-ai/sdk"
  14. import type { BunShell } from "./shell"
  15. import { type ToolDefinition } from "./tool"
  16. export * from "./tool"
  17. export type ProviderContext = {
  18. source: "env" | "config" | "custom" | "api"
  19. info: Provider
  20. options: Record<string, any>
  21. }
  22. export type PluginInput = {
  23. client: ReturnType<typeof createOpencodeClient>
  24. project: Project
  25. directory: string
  26. worktree: string
  27. serverUrl: URL
  28. $: BunShell
  29. }
  30. export type Plugin = (input: PluginInput) => Promise<Hooks>
  31. export type AuthHook = {
  32. provider: string
  33. loader?: (auth: () => Promise<Auth>, provider: Provider) => Promise<Record<string, any>>
  34. methods: (
  35. | {
  36. type: "oauth"
  37. label: string
  38. prompts?: Array<
  39. | {
  40. type: "text"
  41. key: string
  42. message: string
  43. placeholder?: string
  44. validate?: (value: string) => string | undefined
  45. condition?: (inputs: Record<string, string>) => boolean
  46. }
  47. | {
  48. type: "select"
  49. key: string
  50. message: string
  51. options: Array<{
  52. label: string
  53. value: string
  54. hint?: string
  55. }>
  56. condition?: (inputs: Record<string, string>) => boolean
  57. }
  58. >
  59. authorize(inputs?: Record<string, string>): Promise<AuthOuathResult>
  60. }
  61. | {
  62. type: "api"
  63. label: string
  64. prompts?: Array<
  65. | {
  66. type: "text"
  67. key: string
  68. message: string
  69. placeholder?: string
  70. validate?: (value: string) => string | undefined
  71. condition?: (inputs: Record<string, string>) => boolean
  72. }
  73. | {
  74. type: "select"
  75. key: string
  76. message: string
  77. options: Array<{
  78. label: string
  79. value: string
  80. hint?: string
  81. }>
  82. condition?: (inputs: Record<string, string>) => boolean
  83. }
  84. >
  85. authorize?(inputs?: Record<string, string>): Promise<
  86. | {
  87. type: "success"
  88. key: string
  89. provider?: string
  90. }
  91. | {
  92. type: "failed"
  93. }
  94. >
  95. }
  96. )[]
  97. }
  98. export type AuthOuathResult = { url: string; instructions: string } & (
  99. | {
  100. method: "auto"
  101. callback(): Promise<
  102. | ({
  103. type: "success"
  104. provider?: string
  105. } & (
  106. | {
  107. refresh: string
  108. access: string
  109. expires: number
  110. }
  111. | { key: string }
  112. ))
  113. | {
  114. type: "failed"
  115. }
  116. >
  117. }
  118. | {
  119. method: "code"
  120. callback(code: string): Promise<
  121. | ({
  122. type: "success"
  123. provider?: string
  124. } & (
  125. | {
  126. refresh: string
  127. access: string
  128. expires: number
  129. }
  130. | { key: string }
  131. ))
  132. | {
  133. type: "failed"
  134. }
  135. >
  136. }
  137. )
  138. export interface Hooks {
  139. event?: (input: { event: Event }) => Promise<void>
  140. config?: (input: Config) => Promise<void>
  141. tool?: {
  142. [key: string]: ToolDefinition
  143. }
  144. auth?: AuthHook
  145. /**
  146. * Called when a new message is received
  147. */
  148. "chat.message"?: (
  149. input: {
  150. sessionID: string
  151. agent?: string
  152. model?: { providerID: string; modelID: string }
  153. messageID?: string
  154. variant?: string
  155. },
  156. output: { message: UserMessage; parts: Part[] },
  157. ) => Promise<void>
  158. /**
  159. * Modify parameters sent to LLM
  160. */
  161. "chat.params"?: (
  162. input: { sessionID: string; agent: string; model: Model; provider: ProviderContext; message: UserMessage },
  163. output: { temperature: number; topP: number; topK: number; options: Record<string, any> },
  164. ) => Promise<void>
  165. "permission.ask"?: (input: Permission, output: { status: "ask" | "deny" | "allow" }) => Promise<void>
  166. "tool.execute.before"?: (
  167. input: { tool: string; sessionID: string; callID: string },
  168. output: { args: any },
  169. ) => Promise<void>
  170. "tool.execute.after"?: (
  171. input: { tool: string; sessionID: string; callID: string },
  172. output: {
  173. title: string
  174. output: string
  175. metadata: any
  176. },
  177. ) => Promise<void>
  178. "experimental.chat.messages.transform"?: (
  179. input: {},
  180. output: {
  181. messages: {
  182. info: Message
  183. parts: Part[]
  184. }[]
  185. },
  186. ) => Promise<void>
  187. "experimental.chat.system.transform"?: (
  188. input: {},
  189. output: {
  190. system: string[]
  191. },
  192. ) => Promise<void>
  193. /**
  194. * Called before session compaction starts. Allows plugins to customize
  195. * the compaction prompt.
  196. *
  197. * - `context`: Additional context strings appended to the default prompt
  198. * - `prompt`: If set, replaces the default compaction prompt entirely
  199. */
  200. "experimental.session.compacting"?: (
  201. input: { sessionID: string },
  202. output: { context: string[]; prompt?: string },
  203. ) => Promise<void>
  204. "experimental.text.complete"?: (
  205. input: { sessionID: string; messageID: string; partID: string },
  206. output: { text: string },
  207. ) => Promise<void>
  208. }