opencode.ts 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. import { Effect } from "effect"
  2. import { define } from "../internal"
  3. import { ProviderV2 } from "../../provider"
  4. import { Integration } from "../../integration"
  5. export const OpencodePlugin = define({
  6. id: "opencode",
  7. effect: Effect.fn(function* (ctx) {
  8. const integrations = yield* Integration.Service
  9. let hasKey = false
  10. yield* ctx.catalog.transform(
  11. Effect.fn(function* (evt) {
  12. const item = evt.provider.get(ProviderV2.ID.opencode)
  13. if (!item) return
  14. const integration = yield* integrations.get(Integration.ID.make(item.provider.id))
  15. hasKey = Boolean(
  16. process.env.OPENCODE_API_KEY || integration?.connections.length || item.provider.request.body.apiKey,
  17. )
  18. evt.provider.update(item.provider.id, (provider) => {
  19. if (!hasKey) provider.request.body.apiKey = "public"
  20. })
  21. if (hasKey) return
  22. for (const model of item.models.values()) {
  23. if (!model.cost.some((cost) => cost.input > 0)) continue
  24. evt.model.update(item.provider.id, model.id, (draft) => {
  25. draft.enabled = false
  26. })
  27. }
  28. }),
  29. )
  30. }),
  31. })