provider.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. export * as ProviderV2 from "./provider"
  2. import { withStatics } from "./schema"
  3. import { Schema } from "effect"
  4. export const ID = Schema.String.pipe(
  5. Schema.brand("ProviderV2.ID"),
  6. withStatics((schema) => ({
  7. // Well-known providers
  8. opencode: schema.make("opencode"),
  9. anthropic: schema.make("anthropic"),
  10. openai: schema.make("openai"),
  11. google: schema.make("google"),
  12. googleVertex: schema.make("google-vertex"),
  13. githubCopilot: schema.make("github-copilot"),
  14. amazonBedrock: schema.make("amazon-bedrock"),
  15. azure: schema.make("azure"),
  16. openrouter: schema.make("openrouter"),
  17. mistral: schema.make("mistral"),
  18. gitlab: schema.make("gitlab"),
  19. })),
  20. )
  21. export type ID = typeof ID.Type
  22. const OpenAIResponses = Schema.Struct({
  23. type: Schema.Literal("openai/responses"),
  24. url: Schema.String,
  25. websocket: Schema.optional(Schema.Boolean),
  26. })
  27. const OpenAICompletions = Schema.Struct({
  28. type: Schema.Literal("openai/completions"),
  29. url: Schema.String,
  30. reasoning: Schema.Union([
  31. Schema.Struct({
  32. type: Schema.Literal("reasoning_content"),
  33. }),
  34. Schema.Struct({
  35. type: Schema.Literal("reasoning_details"),
  36. }),
  37. ]).pipe(Schema.optional),
  38. })
  39. export type OpenAICompletions = typeof OpenAICompletions.Type
  40. const AISDK = Schema.Struct({
  41. type: Schema.Literal("aisdk"),
  42. package: Schema.String,
  43. url: Schema.String.pipe(Schema.optional),
  44. })
  45. const AnthropicMessages = Schema.Struct({
  46. type: Schema.Literal("anthropic/messages"),
  47. url: Schema.String,
  48. })
  49. const UnknownEndpoint = Schema.Struct({
  50. type: Schema.Literal("unknown"),
  51. })
  52. export const Endpoint = Schema.Union([
  53. UnknownEndpoint,
  54. OpenAIResponses,
  55. OpenAICompletions,
  56. AnthropicMessages,
  57. AISDK,
  58. ]).pipe(Schema.toTaggedUnion("type"))
  59. export type Endpoint = typeof Endpoint.Type
  60. export const Options = Schema.Struct({
  61. headers: Schema.Record(Schema.String, Schema.String),
  62. body: Schema.Record(Schema.String, Schema.Any),
  63. aisdk: Schema.Struct({
  64. provider: Schema.Record(Schema.String, Schema.Any),
  65. request: Schema.Record(Schema.String, Schema.Any),
  66. }),
  67. })
  68. export type Options = typeof Options.Type
  69. export class Info extends Schema.Class<Info>("ProviderV2.Info")({
  70. id: ID,
  71. name: Schema.String,
  72. enabled: Schema.Union([
  73. Schema.Literal(false),
  74. Schema.Struct({
  75. via: Schema.Literal("env"),
  76. name: Schema.String,
  77. }),
  78. Schema.Struct({
  79. via: Schema.Literal("account"),
  80. service: Schema.String,
  81. }),
  82. Schema.Struct({
  83. via: Schema.Literal("custom"),
  84. data: Schema.Record(Schema.String, Schema.Any),
  85. }),
  86. ]),
  87. env: Schema.String.pipe(Schema.Array),
  88. endpoint: Endpoint,
  89. options: Options,
  90. }) {
  91. static empty(providerID: ID) {
  92. return new Info({
  93. id: providerID,
  94. name: providerID,
  95. enabled: false,
  96. env: [],
  97. endpoint: {
  98. type: "unknown",
  99. },
  100. options: {
  101. headers: {},
  102. body: {},
  103. aisdk: {
  104. provider: {},
  105. request: {},
  106. },
  107. },
  108. })
  109. }
  110. }