host.ts 14 KB

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