host.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. import { Plugin } from "@opencode-ai/plugin/effect"
  2. import type { IntegrationMethodRegistration } from "@opencode-ai/plugin/effect/integration"
  3. import { Agent } from "@opencode-ai/core/agent"
  4. import { Catalog } from "@opencode-ai/core/catalog"
  5. import { Credential } from "@opencode-ai/core/credential"
  6. import { Integration } from "@opencode-ai/core/integration"
  7. import { Location } from "@opencode-ai/core/location"
  8. import { Model } from "@opencode-ai/core/model"
  9. import { Project } from "@opencode-ai/core/project"
  10. import { Provider } from "@opencode-ai/core/provider"
  11. import { AbsolutePath } from "@opencode-ai/core/schema"
  12. import { WebSearch } from "@opencode-ai/core/websearch"
  13. import { Effect, Stream } from "effect"
  14. type Overrides = Partial<Omit<Plugin.Context, "options" | "session">> & {
  15. readonly session?: Partial<Plugin.Context["session"]>
  16. }
  17. export function host(overrides: Overrides = {}): Plugin.Context {
  18. return {
  19. app: overrides.app ?? { name: "test", version: "test", channel: "test" },
  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. list: () => Effect.die("unused catalog.model.list"),
  37. default: () => Effect.die("unused catalog.model.default"),
  38. },
  39. transform: () => Effect.die("unused catalog.transform"),
  40. reload: () => Effect.die("unused catalog.reload"),
  41. },
  42. command: overrides.command ?? {
  43. list: () => Effect.die("unused command.list"),
  44. transform: () => Effect.die("unused command.transform"),
  45. reload: () => Effect.die("unused command.reload"),
  46. },
  47. event: overrides.event ?? {
  48. subscribe: () => Stream.empty,
  49. },
  50. integration: overrides.integration ?? {
  51. list: () => Effect.die("unused integration.list"),
  52. get: () => Effect.die("unused integration.get"),
  53. connect: {
  54. key: () => Effect.die("unused integration.connect.key"),
  55. },
  56. oauth: {
  57. connect: () => Effect.die("unused integration.oauth.connect"),
  58. status: () => Effect.die("unused integration.oauth.status"),
  59. complete: () => Effect.die("unused integration.oauth.complete"),
  60. cancel: () => Effect.die("unused integration.oauth.cancel"),
  61. },
  62. command: {
  63. connect: () => Effect.die("unused integration.command.connect"),
  64. status: () => Effect.die("unused integration.command.status"),
  65. cancel: () => Effect.die("unused integration.command.cancel"),
  66. },
  67. transform: () => Effect.die("unused integration.transform"),
  68. reload: () => Effect.die("unused integration.reload"),
  69. connection: {
  70. active: () => Effect.die("unused integration.connection.active"),
  71. resolve: () => Effect.die("unused integration.connection.resolve"),
  72. },
  73. },
  74. plugin: overrides.plugin ?? {
  75. list: () => Effect.die("unused plugin.list"),
  76. },
  77. reference: overrides.reference ?? {
  78. list: () => Effect.die("unused reference.list"),
  79. transform: () => Effect.die("unused reference.transform"),
  80. reload: () => Effect.die("unused reference.reload"),
  81. },
  82. skill: overrides.skill ?? {
  83. list: () => Effect.die("unused skill.list"),
  84. transform: () => Effect.die("unused skill.transform"),
  85. reload: () => Effect.die("unused skill.reload"),
  86. },
  87. shell: overrides.shell ?? {
  88. hook: () => Effect.die("unused shell.hook"),
  89. },
  90. tool: overrides.tool ?? {
  91. transform: () => Effect.die("unused tool.transform"),
  92. hook: () => Effect.die("unused tool.hook"),
  93. },
  94. websearch: overrides.websearch ?? {
  95. providers: () => Effect.die("unused websearch.providers"),
  96. query: () => Effect.die("unused websearch.query"),
  97. transform: () => Effect.die("unused websearch.transform"),
  98. reload: () => Effect.die("unused websearch.reload"),
  99. },
  100. session: {
  101. hook: overrides.session?.hook ?? (() => Effect.die("unused session.hook")),
  102. create: overrides.session?.create ?? (() => Effect.die("unused session.create")),
  103. get: overrides.session?.get ?? (() => Effect.die("unused session.get")),
  104. prompt: overrides.session?.prompt ?? (() => Effect.die("unused session.prompt")),
  105. generate: overrides.session?.generate ?? (() => Effect.die("unused session.generate")),
  106. command: overrides.session?.command ?? (() => Effect.die("unused session.command")),
  107. synthetic: overrides.session?.synthetic ?? (() => Effect.die("unused session.synthetic")),
  108. interrupt: overrides.session?.interrupt ?? (() => Effect.die("unused session.interrupt")),
  109. },
  110. }
  111. }
  112. export function agentHost(agent: Agent.Interface): Plugin.Context["agent"] {
  113. return {
  114. get: (input) =>
  115. agent.get(input.agentID).pipe(
  116. Effect.flatMap((value) =>
  117. value
  118. ? Effect.succeed({
  119. location: new Location.Info({
  120. directory: AbsolutePath.make("/"),
  121. project: {
  122. id: Project.ID.make("test"),
  123. directory: AbsolutePath.make("/"),
  124. canonical: AbsolutePath.make("/"),
  125. },
  126. }),
  127. data: agentInfo(value),
  128. })
  129. : Effect.fail(new Error(`Agent not found: ${input.agentID}`)),
  130. ),
  131. ),
  132. list: () => Effect.die("unused agent.list"),
  133. reload: agent.reload,
  134. transform: (callback) =>
  135. agent.transform((draft) =>
  136. callback({
  137. list: () => draft.list().map(agentInfo),
  138. get: (id) => {
  139. const value = draft.get(Agent.ID.make(id))
  140. return value && agentInfo(value)
  141. },
  142. default: (id) => draft.default(id === undefined ? undefined : Agent.ID.make(id)),
  143. update: (id, update) =>
  144. draft.update(Agent.ID.make(id), (value) => {
  145. const current = agentInfo(value)
  146. update(current)
  147. Object.assign(value, current, { id: Agent.ID.make(current.id) })
  148. }),
  149. remove: (id) => draft.remove(Agent.ID.make(id)),
  150. }),
  151. ),
  152. }
  153. }
  154. export function catalogHost(catalog: Catalog.Interface): Plugin.Context["catalog"] {
  155. return {
  156. provider: {
  157. list: () => Effect.die("unused catalog.provider.list"),
  158. get: () => Effect.die("unused catalog.provider.get"),
  159. },
  160. model: {
  161. list: () =>
  162. catalog.model.available().pipe(
  163. Effect.map((data) => ({
  164. location: new Location.Info({
  165. directory: AbsolutePath.make("/"),
  166. project: {
  167. id: Project.ID.make("test"),
  168. directory: AbsolutePath.make("/"),
  169. canonical: AbsolutePath.make("/"),
  170. },
  171. }),
  172. data: data.map(modelInfo),
  173. })),
  174. ),
  175. default: () => Effect.die("unused catalog.model.default"),
  176. },
  177. reload: catalog.reload,
  178. transform: (callback) =>
  179. catalog.transform((draft) =>
  180. callback({
  181. provider: {
  182. list: () =>
  183. draft.provider.list().map((value) => ({
  184. provider: providerInfo(value.provider),
  185. models: new Map(Array.from(value.models, ([id, model]) => [id, modelInfo(model)])),
  186. })),
  187. get: (id) => {
  188. const value = draft.provider.get(Provider.ID.make(id))
  189. return (
  190. value && {
  191. provider: providerInfo(value.provider),
  192. models: new Map(Array.from(value.models, ([id, model]) => [id, modelInfo(model)])),
  193. }
  194. )
  195. },
  196. update: (id, update) =>
  197. draft.provider.update(Provider.ID.make(id), (value) => {
  198. const current = providerInfo(value)
  199. update(current)
  200. Object.assign(value, current, { id: Provider.ID.make(current.id) })
  201. }),
  202. remove: (id) => draft.provider.remove(Provider.ID.make(id)),
  203. },
  204. model: {
  205. get: (providerID, modelID) => {
  206. const value = draft.model.get(Provider.ID.make(providerID), Model.ID.make(modelID))
  207. return value && modelInfo(value)
  208. },
  209. update: (providerID, modelID, update) =>
  210. draft.model.update(Provider.ID.make(providerID), Model.ID.make(modelID), (value) => {
  211. const current = modelInfo(value)
  212. update(current)
  213. Object.assign(value, current, {
  214. id: Model.ID.make(current.id),
  215. providerID: Provider.ID.make(current.providerID),
  216. family: current.family === undefined ? undefined : Model.Family.make(current.family),
  217. variants: current.variants?.map((variant) => ({
  218. ...variant,
  219. id: Model.VariantID.make(variant.id),
  220. })),
  221. })
  222. }),
  223. remove: (providerID, modelID) =>
  224. draft.model.remove(Provider.ID.make(providerID), Model.ID.make(modelID)),
  225. default: {
  226. get: () => {
  227. const value = draft.model.default.get()
  228. return value && { providerID: value.providerID, modelID: value.modelID }
  229. },
  230. set: (providerID, modelID) =>
  231. draft.model.default.set(Provider.ID.make(providerID), Model.ID.make(modelID)),
  232. },
  233. },
  234. }),
  235. ),
  236. }
  237. }
  238. export function integrationHost(integration: Integration.Interface): Plugin.Context["integration"] {
  239. return {
  240. list: () => Effect.die("unused integration.list"),
  241. get: () => Effect.die("unused integration.get"),
  242. connect: {
  243. key: () => Effect.die("unused integration.connect.key"),
  244. },
  245. oauth: {
  246. connect: () => Effect.die("unused integration.oauth.connect"),
  247. status: () => Effect.die("unused integration.oauth.status"),
  248. complete: () => Effect.die("unused integration.oauth.complete"),
  249. cancel: () => Effect.die("unused integration.oauth.cancel"),
  250. },
  251. command: {
  252. connect: () => Effect.die("unused integration.command.connect"),
  253. status: () => Effect.die("unused integration.command.status"),
  254. cancel: () => Effect.die("unused integration.command.cancel"),
  255. },
  256. reload: integration.reload,
  257. connection: {
  258. active: (id) => integration.connection.active(Integration.ID.make(id)),
  259. resolve: (connection) =>
  260. integration.connection.resolve(
  261. connection.type === "credential" ? { ...connection, id: Credential.ID.make(connection.id) } : connection,
  262. ),
  263. },
  264. transform: (callback) =>
  265. integration.transform((draft) =>
  266. callback({
  267. list: () => draft.list().map((value) => ({ id: value.id, name: value.name })),
  268. get: (id) => {
  269. const value = draft.get(Integration.ID.make(id))
  270. return value && { id: value.id, name: value.name }
  271. },
  272. update: (id, update) => draft.update(Integration.ID.make(id), update),
  273. remove: (id) => draft.remove(Integration.ID.make(id)),
  274. method: {
  275. list: (id) => draft.method.list(Integration.ID.make(id)).map(method),
  276. update: (input) => {
  277. if ("authorize" in input) {
  278. const methodID = Integration.MethodID.make(input.method.id)
  279. const refresh = input.refresh
  280. draft.method.update({
  281. integrationID: Integration.ID.make(input.integrationID),
  282. method: { ...input.method, id: methodID },
  283. authorize: (inputs) =>
  284. input.authorize(inputs).pipe(
  285. Effect.map((authorization) => {
  286. if (authorization.mode === "auto") {
  287. return {
  288. ...authorization,
  289. callback: authorization.callback.pipe(
  290. Effect.map((credential) =>
  291. Credential.OAuth.make({
  292. ...credential,
  293. methodID: Integration.MethodID.make(credential.methodID),
  294. }),
  295. ),
  296. ),
  297. }
  298. }
  299. return {
  300. ...authorization,
  301. callback: (code: string) =>
  302. authorization.callback(code).pipe(
  303. Effect.map((credential) =>
  304. Credential.OAuth.make({
  305. ...credential,
  306. methodID: Integration.MethodID.make(credential.methodID),
  307. }),
  308. ),
  309. ),
  310. }
  311. }),
  312. ),
  313. ...(refresh
  314. ? {
  315. refresh: (value: Credential.OAuth) =>
  316. refresh(value).pipe(
  317. Effect.map((next) =>
  318. Credential.OAuth.make({
  319. ...next,
  320. methodID: Integration.MethodID.make(next.methodID),
  321. }),
  322. ),
  323. ),
  324. }
  325. : {}),
  326. ...(input.label ? { label: input.label } : {}),
  327. })
  328. return
  329. }
  330. if (input.method.type === "env") {
  331. draft.method.update({
  332. integrationID: Integration.ID.make(input.integrationID),
  333. method: { ...input.method, names: [...input.method.names] },
  334. })
  335. return
  336. }
  337. if (input.method.type === "command") {
  338. draft.method.update({
  339. integrationID: Integration.ID.make(input.integrationID),
  340. method: {
  341. ...input.method,
  342. id: Integration.MethodID.make(input.method.id),
  343. command: [...input.method.command],
  344. },
  345. })
  346. return
  347. }
  348. draft.method.update({
  349. integrationID: Integration.ID.make(input.integrationID),
  350. method: input.method,
  351. })
  352. },
  353. remove: (id, item) => draft.method.remove(Integration.ID.make(id), internalMethod(item)),
  354. },
  355. }),
  356. ),
  357. }
  358. }
  359. export function webSearchHost(websearch: WebSearch.Interface): Plugin.Context["websearch"] {
  360. const location = Location.Info.make({
  361. directory: AbsolutePath.make("/tmp/websearch-test"),
  362. project: {
  363. id: Project.ID.make("websearch-test"),
  364. directory: AbsolutePath.make("/tmp/websearch-test"),
  365. canonical: AbsolutePath.make("/tmp/websearch-test"),
  366. },
  367. })
  368. return {
  369. providers: () => websearch.providers().pipe(Effect.map((data) => ({ location, data }))),
  370. query: (input) =>
  371. websearch
  372. .query({ query: input.query, providerID: input.providerID && WebSearch.ID.make(input.providerID) })
  373. .pipe(Effect.map((data) => ({ location, data }))),
  374. reload: websearch.reload,
  375. transform: (callback) =>
  376. websearch.transform((draft) => {
  377. callback({
  378. add: (definition) =>
  379. draft.add({
  380. id: WebSearch.ID.make(definition.id),
  381. name: definition.name,
  382. execute: definition.execute,
  383. }),
  384. default: {
  385. get: draft.default.get,
  386. set: (providerID) => draft.default.set(WebSearch.ID.make(providerID)),
  387. },
  388. })
  389. }),
  390. }
  391. }
  392. function oauthCredential(value: Credential.OAuth) {
  393. return Credential.OAuth.make({ ...value, methodID: Integration.MethodID.make(value.methodID) })
  394. }
  395. function method(value: Integration.Method) {
  396. if (value.type === "env") return { type: value.type, names: [...value.names] }
  397. if (value.type === "key") return { type: value.type, label: value.label }
  398. if (value.type === "command") return { ...value, command: [...value.command] }
  399. return {
  400. type: value.type,
  401. id: value.id,
  402. label: value.label,
  403. prompts: value.prompts?.map((prompt) => {
  404. if (prompt.type === "text") return { ...prompt }
  405. return { ...prompt, options: prompt.options.map((option) => ({ ...option })) }
  406. }),
  407. }
  408. }
  409. function internalMethod(
  410. value: IntegrationMethodRegistration["method"],
  411. ): Integration.Method {
  412. if (value.type === "env") return value
  413. if (value.type === "key") return value
  414. if (value.type === "command") {
  415. return {
  416. ...value,
  417. id: Integration.MethodID.make(value.id),
  418. command: [...value.command],
  419. }
  420. }
  421. return {
  422. ...value,
  423. id: Integration.MethodID.make(value.id),
  424. }
  425. }
  426. function agentInfo(value: Agent.Info) {
  427. return {
  428. ...value,
  429. model: value.model && { ...value.model },
  430. request: {
  431. settings: { ...value.request.settings },
  432. headers: { ...value.request.headers },
  433. body: { ...value.request.body },
  434. },
  435. permissions: value.permissions.map((permission) => ({ ...permission })),
  436. }
  437. }
  438. function providerInfo(value: Provider.MutableInfo) {
  439. return {
  440. ...value,
  441. settings: value.settings && { ...value.settings },
  442. headers: value.headers && { ...value.headers },
  443. body: value.body && { ...value.body },
  444. }
  445. }
  446. function modelInfo(value: Model.Info | Model.MutableInfo) {
  447. return {
  448. ...value,
  449. settings: value.settings && { ...value.settings },
  450. headers: value.headers && { ...value.headers },
  451. body: value.body && { ...value.body },
  452. capabilities: {
  453. ...value.capabilities,
  454. input: [...value.capabilities.input],
  455. output: [...value.capabilities.output],
  456. },
  457. variants: value.variants?.map((variant) => ({
  458. ...variant,
  459. settings: variant.settings && { ...variant.settings },
  460. headers: variant.headers && { ...variant.headers },
  461. body: variant.body && { ...variant.body },
  462. })),
  463. time: { ...value.time },
  464. cost: value.cost.map((cost) => ({ ...cost, tier: cost.tier && { ...cost.tier }, cache: { ...cost.cache } })),
  465. limit: { ...value.limit },
  466. }
  467. }