host.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. import type { Context as PluginContext } from "@opencode-ai/plugin/v2/effect/plugin"
  2. import { AgentV2 } from "@opencode-ai/core/agent"
  3. import { Catalog } from "@opencode-ai/core/catalog"
  4. import { Credential } from "@opencode-ai/core/credential"
  5. import { Integration } from "@opencode-ai/core/integration"
  6. import { ModelV2 } from "@opencode-ai/core/model"
  7. import { ProviderV2 } from "@opencode-ai/core/provider"
  8. import type {
  9. IntegrationCommandMethod,
  10. IntegrationEnvMethod,
  11. IntegrationKeyMethod,
  12. IntegrationOAuthMethod,
  13. } from "@opencode-ai/sdk/v2/types"
  14. import { Effect, Stream } from "effect"
  15. type Overrides = Partial<Omit<PluginContext, "options" | "session">> & {
  16. readonly session?: Partial<PluginContext["session"]>
  17. }
  18. export function host(overrides: Overrides = {}): PluginContext {
  19. return {
  20. options: {},
  21. agent: overrides.agent ?? {
  22. get: () => Effect.die("unused agent.get"),
  23. list: () => Effect.die("unused agent.list"),
  24. transform: () => Effect.die("unused agent.transform"),
  25. reload: () => Effect.die("unused agent.reload"),
  26. },
  27. aisdk: overrides.aisdk ?? {
  28. hook: () => Effect.die("unused aisdk.hook"),
  29. },
  30. catalog: overrides.catalog ?? {
  31. provider: {
  32. list: () => Effect.die("unused catalog.provider.list"),
  33. get: () => Effect.die("unused catalog.provider.get"),
  34. },
  35. model: {
  36. get: () => Effect.die("unused catalog.model.get"),
  37. list: () => Effect.die("unused catalog.model.list"),
  38. default: () => Effect.die("unused catalog.model.default"),
  39. },
  40. transform: () => Effect.die("unused catalog.transform"),
  41. reload: () => Effect.die("unused catalog.reload"),
  42. },
  43. command: overrides.command ?? {
  44. list: () => Effect.die("unused command.list"),
  45. transform: () => Effect.die("unused command.transform"),
  46. reload: () => Effect.die("unused command.reload"),
  47. },
  48. event: overrides.event ?? {
  49. subscribe: () => Stream.empty,
  50. },
  51. integration: overrides.integration ?? {
  52. list: () => Effect.die("unused integration.list"),
  53. get: () => Effect.die("unused integration.get"),
  54. connect: {
  55. key: () => Effect.die("unused integration.connect.key"),
  56. },
  57. oauth: {
  58. connect: () => Effect.die("unused integration.oauth.connect"),
  59. status: () => Effect.die("unused integration.oauth.status"),
  60. complete: () => Effect.die("unused integration.oauth.complete"),
  61. cancel: () => Effect.die("unused integration.oauth.cancel"),
  62. },
  63. command: {
  64. connect: () => Effect.die("unused integration.command.connect"),
  65. status: () => Effect.die("unused integration.command.status"),
  66. cancel: () => Effect.die("unused integration.command.cancel"),
  67. },
  68. transform: () => Effect.die("unused integration.transform"),
  69. reload: () => Effect.die("unused integration.reload"),
  70. connection: {
  71. active: () => Effect.die("unused integration.connection.active"),
  72. resolve: () => Effect.die("unused integration.connection.resolve"),
  73. },
  74. },
  75. plugin: overrides.plugin ?? {
  76. list: () => Effect.die("unused plugin.list"),
  77. },
  78. reference: overrides.reference ?? {
  79. list: () => Effect.die("unused reference.list"),
  80. transform: () => Effect.die("unused reference.transform"),
  81. reload: () => Effect.die("unused reference.reload"),
  82. },
  83. skill: overrides.skill ?? {
  84. list: () => Effect.die("unused skill.list"),
  85. transform: () => Effect.die("unused skill.transform"),
  86. reload: () => Effect.die("unused skill.reload"),
  87. },
  88. tool: overrides.tool ?? {
  89. transform: () => Effect.die("unused tool.transform"),
  90. hook: () => Effect.die("unused tool.hook"),
  91. },
  92. session: {
  93. hook: overrides.session?.hook ?? (() => Effect.die("unused session.hook")),
  94. create: overrides.session?.create ?? (() => Effect.die("unused session.create")),
  95. get: overrides.session?.get ?? (() => Effect.die("unused session.get")),
  96. prompt: overrides.session?.prompt ?? (() => Effect.die("unused session.prompt")),
  97. command: overrides.session?.command ?? (() => Effect.die("unused session.command")),
  98. synthetic: overrides.session?.synthetic ?? (() => Effect.die("unused session.synthetic")),
  99. interrupt: overrides.session?.interrupt ?? (() => Effect.die("unused session.interrupt")),
  100. },
  101. }
  102. }
  103. export function agentHost(agent: AgentV2.Interface): PluginContext["agent"] {
  104. return {
  105. get: (id) => agent.get(AgentV2.ID.make(id)).pipe(Effect.map((value) => value && agentInfo(value))),
  106. list: () => Effect.die("unused agent.list"),
  107. reload: agent.reload,
  108. transform: (callback) =>
  109. agent.transform((draft) =>
  110. callback({
  111. list: () => draft.list().map(agentInfo),
  112. get: (id) => {
  113. const value = draft.get(AgentV2.ID.make(id))
  114. return value && agentInfo(value)
  115. },
  116. default: (id) => draft.default(id === undefined ? undefined : AgentV2.ID.make(id)),
  117. update: (id, update) =>
  118. draft.update(AgentV2.ID.make(id), (value) => {
  119. const current = agentInfo(value)
  120. update(current)
  121. Object.assign(value, current, { id: AgentV2.ID.make(current.id) })
  122. }),
  123. remove: (id) => draft.remove(AgentV2.ID.make(id)),
  124. }),
  125. ),
  126. }
  127. }
  128. export function catalogHost(catalog: Catalog.Interface): PluginContext["catalog"] {
  129. return {
  130. provider: {
  131. list: () => Effect.die("unused catalog.provider.list"),
  132. get: () => Effect.die("unused catalog.provider.get"),
  133. },
  134. model: {
  135. get: (providerID, modelID) =>
  136. catalog.model
  137. .get(ProviderV2.ID.make(providerID), ModelV2.ID.make(modelID))
  138. .pipe(Effect.map((value) => value && modelInfo(value))),
  139. list: () => Effect.die("unused catalog.model.list"),
  140. default: () => Effect.die("unused catalog.model.default"),
  141. },
  142. reload: catalog.reload,
  143. transform: (callback) =>
  144. catalog.transform((draft) =>
  145. callback({
  146. provider: {
  147. list: () =>
  148. draft.provider.list().map((value) => ({
  149. provider: providerInfo(value.provider),
  150. models: new Map(Array.from(value.models, ([id, model]) => [id, modelInfo(model)])),
  151. })),
  152. get: (id) => {
  153. const value = draft.provider.get(ProviderV2.ID.make(id))
  154. return (
  155. value && {
  156. provider: providerInfo(value.provider),
  157. models: new Map(Array.from(value.models, ([id, model]) => [id, modelInfo(model)])),
  158. }
  159. )
  160. },
  161. update: (id, update) =>
  162. draft.provider.update(ProviderV2.ID.make(id), (value) => {
  163. const current = providerInfo(value)
  164. update(current)
  165. Object.assign(value, current, { id: ProviderV2.ID.make(current.id) })
  166. }),
  167. remove: (id) => draft.provider.remove(ProviderV2.ID.make(id)),
  168. },
  169. model: {
  170. get: (providerID, modelID) => {
  171. const value = draft.model.get(ProviderV2.ID.make(providerID), ModelV2.ID.make(modelID))
  172. return value && modelInfo(value)
  173. },
  174. update: (providerID, modelID, update) =>
  175. draft.model.update(ProviderV2.ID.make(providerID), ModelV2.ID.make(modelID), (value) => {
  176. const current = modelInfo(value)
  177. update(current)
  178. Object.assign(value, current, {
  179. id: ModelV2.ID.make(current.id),
  180. providerID: ProviderV2.ID.make(current.providerID),
  181. family: current.family === undefined ? undefined : ModelV2.Family.make(current.family),
  182. variants: current.variants?.map((variant) => ({
  183. ...variant,
  184. id: ModelV2.VariantID.make(variant.id),
  185. })),
  186. })
  187. }),
  188. remove: (providerID, modelID) =>
  189. draft.model.remove(ProviderV2.ID.make(providerID), ModelV2.ID.make(modelID)),
  190. default: {
  191. get: () => {
  192. const value = draft.model.default.get()
  193. return value && { providerID: value.providerID, modelID: value.modelID }
  194. },
  195. set: (providerID, modelID) =>
  196. draft.model.default.set(ProviderV2.ID.make(providerID), ModelV2.ID.make(modelID)),
  197. },
  198. },
  199. }),
  200. ),
  201. }
  202. }
  203. export function integrationHost(integration: Integration.Interface): PluginContext["integration"] {
  204. return {
  205. list: () => Effect.die("unused integration.list"),
  206. get: () => Effect.die("unused integration.get"),
  207. connect: {
  208. key: () => Effect.die("unused integration.connect.key"),
  209. },
  210. oauth: {
  211. connect: () => Effect.die("unused integration.oauth.connect"),
  212. status: () => Effect.die("unused integration.oauth.status"),
  213. complete: () => Effect.die("unused integration.oauth.complete"),
  214. cancel: () => Effect.die("unused integration.oauth.cancel"),
  215. },
  216. command: {
  217. connect: () => Effect.die("unused integration.command.connect"),
  218. status: () => Effect.die("unused integration.command.status"),
  219. cancel: () => Effect.die("unused integration.command.cancel"),
  220. },
  221. reload: integration.reload,
  222. connection: {
  223. active: (id) => integration.connection.active(Integration.ID.make(id)),
  224. resolve: (connection) =>
  225. integration.connection.resolve(
  226. connection.type === "credential" ? { ...connection, id: Credential.ID.make(connection.id) } : connection,
  227. ),
  228. },
  229. transform: (callback) =>
  230. integration.transform((draft) =>
  231. callback({
  232. list: () => draft.list().map((value) => ({ id: value.id, name: value.name })),
  233. get: (id) => {
  234. const value = draft.get(Integration.ID.make(id))
  235. return value && { id: value.id, name: value.name }
  236. },
  237. update: (id, update) => draft.update(Integration.ID.make(id), update),
  238. remove: (id) => draft.remove(Integration.ID.make(id)),
  239. method: {
  240. list: (id) => draft.method.list(Integration.ID.make(id)).map(method),
  241. update: (input) => {
  242. if ("authorize" in input) {
  243. const methodID = Integration.MethodID.make(input.method.id)
  244. const refresh = input.refresh
  245. draft.method.update({
  246. integrationID: Integration.ID.make(input.integrationID),
  247. method: { ...input.method, id: methodID },
  248. authorize: (inputs) =>
  249. input.authorize(inputs).pipe(
  250. Effect.map((authorization) => {
  251. if (authorization.mode === "auto") {
  252. return {
  253. ...authorization,
  254. callback: authorization.callback.pipe(
  255. Effect.map((credential) =>
  256. Credential.OAuth.make({
  257. ...credential,
  258. methodID: Integration.MethodID.make(credential.methodID),
  259. }),
  260. ),
  261. ),
  262. }
  263. }
  264. return {
  265. ...authorization,
  266. callback: (code: string) =>
  267. authorization.callback(code).pipe(
  268. Effect.map((credential) =>
  269. Credential.OAuth.make({
  270. ...credential,
  271. methodID: Integration.MethodID.make(credential.methodID),
  272. }),
  273. ),
  274. ),
  275. }
  276. }),
  277. ),
  278. ...(refresh
  279. ? {
  280. refresh: (value: Credential.OAuth) =>
  281. refresh(value).pipe(
  282. Effect.map((next) =>
  283. Credential.OAuth.make({
  284. ...next,
  285. methodID: Integration.MethodID.make(next.methodID),
  286. }),
  287. ),
  288. ),
  289. }
  290. : {}),
  291. ...(input.label ? { label: input.label } : {}),
  292. })
  293. return
  294. }
  295. if (input.method.type === "env") {
  296. draft.method.update({
  297. integrationID: Integration.ID.make(input.integrationID),
  298. method: { ...input.method, names: [...input.method.names] },
  299. })
  300. return
  301. }
  302. if (input.method.type === "command") {
  303. draft.method.update({
  304. integrationID: Integration.ID.make(input.integrationID),
  305. method: {
  306. ...input.method,
  307. id: Integration.MethodID.make(input.method.id),
  308. command: [...input.method.command],
  309. },
  310. })
  311. return
  312. }
  313. draft.method.update({
  314. integrationID: Integration.ID.make(input.integrationID),
  315. method: input.method,
  316. })
  317. },
  318. remove: (id, item) => draft.method.remove(Integration.ID.make(id), internalMethod(item)),
  319. },
  320. }),
  321. ),
  322. }
  323. }
  324. function method(value: Integration.Method) {
  325. if (value.type === "env") return { type: value.type, names: [...value.names] }
  326. if (value.type === "key") return { type: value.type, label: value.label }
  327. if (value.type === "command") return { ...value, command: [...value.command] }
  328. return {
  329. type: value.type,
  330. id: value.id,
  331. label: value.label,
  332. prompts: value.prompts?.map((prompt) => {
  333. if (prompt.type === "text") return { ...prompt }
  334. return { ...prompt, options: prompt.options.map((option) => ({ ...option })) }
  335. }),
  336. }
  337. }
  338. function internalMethod(
  339. value: IntegrationOAuthMethod | IntegrationCommandMethod | IntegrationKeyMethod | IntegrationEnvMethod,
  340. ): Integration.Method {
  341. if (value.type === "env") return value
  342. if (value.type === "key") return value
  343. if (value.type === "command") {
  344. return {
  345. ...value,
  346. id: Integration.MethodID.make(value.id),
  347. command: [...value.command],
  348. }
  349. }
  350. return {
  351. ...value,
  352. id: Integration.MethodID.make(value.id),
  353. }
  354. }
  355. function agentInfo(value: AgentV2.Info) {
  356. return {
  357. ...value,
  358. model: value.model && { ...value.model },
  359. request: {
  360. settings: { ...value.request.settings },
  361. headers: { ...value.request.headers },
  362. body: { ...value.request.body },
  363. },
  364. permissions: value.permissions.map((permission) => ({ ...permission })),
  365. }
  366. }
  367. function providerInfo(value: ProviderV2.MutableInfo) {
  368. return {
  369. ...value,
  370. settings: value.settings && { ...value.settings },
  371. headers: value.headers && { ...value.headers },
  372. body: value.body && { ...value.body },
  373. }
  374. }
  375. function modelInfo(value: ModelV2.Info | ModelV2.MutableInfo) {
  376. return {
  377. ...value,
  378. settings: value.settings && { ...value.settings },
  379. headers: value.headers && { ...value.headers },
  380. body: value.body && { ...value.body },
  381. capabilities: {
  382. ...value.capabilities,
  383. input: [...value.capabilities.input],
  384. output: [...value.capabilities.output],
  385. },
  386. variants: value.variants?.map((variant) => ({
  387. ...variant,
  388. settings: variant.settings && { ...variant.settings },
  389. headers: variant.headers && { ...variant.headers },
  390. body: variant.body && { ...variant.body },
  391. })),
  392. time: { ...value.time },
  393. cost: value.cost.map((cost) => ({ ...cost, tier: cost.tier && { ...cost.tier }, cache: { ...cost.cache } })),
  394. limit: { ...value.limit },
  395. }
  396. }