index.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 as SDKConfig,
  13. } from "@opencode-ai/sdk"
  14. import type { Provider as ProviderV2, Model as ModelV2 } from "@opencode-ai/sdk/v2"
  15. import type { BunShell } from "./shell.js"
  16. import { type ToolDefinition } from "./tool.js"
  17. export * from "./tool.js"
  18. export type ProviderContext = {
  19. source: "env" | "config" | "custom" | "api"
  20. info: Provider
  21. options: Record<string, any>
  22. }
  23. export type WorkspaceInfo = {
  24. id: string
  25. type: string
  26. name: string
  27. branch: string | null
  28. directory: string | null
  29. extra: unknown | null
  30. projectID: string
  31. }
  32. export type WorkspaceTarget =
  33. | {
  34. type: "local"
  35. directory: string
  36. }
  37. | {
  38. type: "remote"
  39. url: string | URL
  40. headers?: HeadersInit
  41. }
  42. export type WorkspaceAdaptor = {
  43. name: string
  44. description: string
  45. configure(config: WorkspaceInfo): WorkspaceInfo | Promise<WorkspaceInfo>
  46. create(config: WorkspaceInfo, env: Record<string, string | undefined>, from?: WorkspaceInfo): Promise<void>
  47. remove(config: WorkspaceInfo): Promise<void>
  48. target(config: WorkspaceInfo): WorkspaceTarget | Promise<WorkspaceTarget>
  49. }
  50. export type PluginInput = {
  51. client: ReturnType<typeof createOpencodeClient>
  52. project: Project
  53. directory: string
  54. worktree: string
  55. experimental_workspace: {
  56. register(type: string, adaptor: WorkspaceAdaptor): void
  57. }
  58. serverUrl: URL
  59. $: BunShell
  60. }
  61. export type PluginOptions = Record<string, unknown>
  62. export type Config = Omit<SDKConfig, "plugin"> & {
  63. plugin?: Array<string | [string, PluginOptions]>
  64. }
  65. export type Plugin = (input: PluginInput, options?: PluginOptions) => Promise<Hooks>
  66. export type PluginModule = {
  67. id?: string
  68. server: Plugin
  69. tui?: never
  70. }
  71. type Rule = {
  72. key: string
  73. op: "eq" | "neq"
  74. value: string
  75. }
  76. export type AuthHook = {
  77. provider: string
  78. loader?: (auth: () => Promise<Auth>, provider: Provider) => Promise<Record<string, any>>
  79. methods: (
  80. | {
  81. type: "oauth"
  82. label: string
  83. prompts?: Array<
  84. | {
  85. type: "text"
  86. key: string
  87. message: string
  88. placeholder?: string
  89. validate?: (value: string) => string | undefined
  90. /** @deprecated Use `when` instead */
  91. condition?: (inputs: Record<string, string>) => boolean
  92. when?: Rule
  93. }
  94. | {
  95. type: "select"
  96. key: string
  97. message: string
  98. options: Array<{
  99. label: string
  100. value: string
  101. hint?: string
  102. }>
  103. /** @deprecated Use `when` instead */
  104. condition?: (inputs: Record<string, string>) => boolean
  105. when?: Rule
  106. }
  107. >
  108. authorize(inputs?: Record<string, string>): Promise<AuthOAuthResult>
  109. }
  110. | {
  111. type: "api"
  112. label: string
  113. prompts?: Array<
  114. | {
  115. type: "text"
  116. key: string
  117. message: string
  118. placeholder?: string
  119. validate?: (value: string) => string | undefined
  120. /** @deprecated Use `when` instead */
  121. condition?: (inputs: Record<string, string>) => boolean
  122. when?: Rule
  123. }
  124. | {
  125. type: "select"
  126. key: string
  127. message: string
  128. options: Array<{
  129. label: string
  130. value: string
  131. hint?: string
  132. }>
  133. /** @deprecated Use `when` instead */
  134. condition?: (inputs: Record<string, string>) => boolean
  135. when?: Rule
  136. }
  137. >
  138. authorize?(inputs?: Record<string, string>): Promise<
  139. | {
  140. type: "success"
  141. key: string
  142. provider?: string
  143. }
  144. | {
  145. type: "failed"
  146. }
  147. >
  148. }
  149. )[]
  150. }
  151. export type AuthOAuthResult = { url: string; instructions: string } & (
  152. | {
  153. method: "auto"
  154. callback(): Promise<
  155. | ({
  156. type: "success"
  157. provider?: string
  158. } & (
  159. | {
  160. refresh: string
  161. access: string
  162. expires: number
  163. accountId?: string
  164. enterpriseUrl?: string
  165. }
  166. | { key: string }
  167. ))
  168. | {
  169. type: "failed"
  170. }
  171. >
  172. }
  173. | {
  174. method: "code"
  175. callback(code: string): Promise<
  176. | ({
  177. type: "success"
  178. provider?: string
  179. } & (
  180. | {
  181. refresh: string
  182. access: string
  183. expires: number
  184. accountId?: string
  185. enterpriseUrl?: string
  186. }
  187. | { key: string }
  188. ))
  189. | {
  190. type: "failed"
  191. }
  192. >
  193. }
  194. )
  195. export type ProviderHookContext = {
  196. auth?: Auth
  197. }
  198. export type ProviderHook = {
  199. id: string
  200. models?: (provider: ProviderV2, ctx: ProviderHookContext) => Promise<Record<string, ModelV2>>
  201. }
  202. /** @deprecated Use AuthOAuthResult instead. */
  203. export type AuthOuathResult = AuthOAuthResult
  204. export interface Hooks {
  205. event?: (input: { event: Event }) => Promise<void>
  206. config?: (input: Config) => Promise<void>
  207. tool?: {
  208. [key: string]: ToolDefinition
  209. }
  210. auth?: AuthHook
  211. provider?: ProviderHook
  212. /**
  213. * Called when a new message is received
  214. */
  215. "chat.message"?: (
  216. input: {
  217. sessionID: string
  218. agent?: string
  219. model?: { providerID: string; modelID: string }
  220. messageID?: string
  221. variant?: string
  222. },
  223. output: { message: UserMessage; parts: Part[] },
  224. ) => Promise<void>
  225. /**
  226. * Modify parameters sent to LLM
  227. */
  228. "chat.params"?: (
  229. input: { sessionID: string; agent: string; model: Model; provider: ProviderContext; message: UserMessage },
  230. output: {
  231. temperature: number
  232. topP: number
  233. topK: number
  234. maxOutputTokens: number | undefined
  235. options: Record<string, any>
  236. },
  237. ) => Promise<void>
  238. "chat.headers"?: (
  239. input: { sessionID: string; agent: string; model: Model; provider: ProviderContext; message: UserMessage },
  240. output: { headers: Record<string, string> },
  241. ) => Promise<void>
  242. "permission.ask"?: (input: Permission, output: { status: "ask" | "deny" | "allow" }) => Promise<void>
  243. "command.execute.before"?: (
  244. input: { command: string; sessionID: string; arguments: string },
  245. output: { parts: Part[] },
  246. ) => Promise<void>
  247. "tool.execute.before"?: (
  248. input: { tool: string; sessionID: string; callID: string },
  249. output: { args: any },
  250. ) => Promise<void>
  251. "shell.env"?: (
  252. input: { cwd: string; sessionID?: string; callID?: string },
  253. output: { env: Record<string, string> },
  254. ) => Promise<void>
  255. "tool.execute.after"?: (
  256. input: { tool: string; sessionID: string; callID: string; args: any },
  257. output: {
  258. title: string
  259. output: string
  260. metadata: any
  261. },
  262. ) => Promise<void>
  263. "experimental.chat.messages.transform"?: (
  264. input: {},
  265. output: {
  266. messages: {
  267. info: Message
  268. parts: Part[]
  269. }[]
  270. },
  271. ) => Promise<void>
  272. "experimental.chat.system.transform"?: (
  273. input: { sessionID?: string; model: Model },
  274. output: {
  275. system: string[]
  276. },
  277. ) => Promise<void>
  278. /**
  279. * Called before session compaction starts. Allows plugins to customize
  280. * the compaction prompt.
  281. *
  282. * - `context`: Additional context strings appended to the default prompt
  283. * - `prompt`: If set, replaces the default compaction prompt entirely
  284. */
  285. "experimental.session.compacting"?: (
  286. input: { sessionID: string },
  287. output: { context: string[]; prompt?: string },
  288. ) => Promise<void>
  289. /**
  290. * Called after compaction succeeds and before a synthetic user
  291. * auto-continue message is added.
  292. *
  293. * - `enabled`: Defaults to `true`. Set to `false` to skip the synthetic
  294. * user "continue" turn.
  295. */
  296. "experimental.compaction.autocontinue"?: (
  297. input: {
  298. sessionID: string
  299. agent: string
  300. model: Model
  301. provider: ProviderContext
  302. message: UserMessage
  303. overflow: boolean
  304. },
  305. output: { enabled: boolean },
  306. ) => Promise<void>
  307. "experimental.text.complete"?: (
  308. input: { sessionID: string; messageID: string; partID: string },
  309. output: { text: string },
  310. ) => Promise<void>
  311. /**
  312. * Modify tool definitions (description and parameters) sent to LLM
  313. */
  314. "tool.definition"?: (input: { toolID: string }, output: { description: string; parameters: any }) => Promise<void>
  315. }