provider.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. export const ModelID = Schema.String.pipe(Schema.brand("ModelID"))
  23. export type ModelID = typeof ModelID.Type
  24. const OpenAIResponses = Schema.Struct({
  25. type: Schema.Literal("openai/responses"),
  26. url: Schema.String,
  27. websocket: Schema.optional(Schema.Boolean),
  28. })
  29. const OpenAICompletions = Schema.Struct({
  30. type: Schema.Literal("openai/completions"),
  31. url: Schema.String,
  32. reasoning: Schema.Union([
  33. Schema.Struct({
  34. type: Schema.Literal("reasoning_content"),
  35. }),
  36. Schema.Struct({
  37. type: Schema.Literal("reasoning_details"),
  38. }),
  39. ]).pipe(Schema.optional),
  40. })
  41. export type OpenAICompletions = typeof OpenAICompletions.Type
  42. const AISDK = Schema.Struct({
  43. type: Schema.Literal("aisdk"),
  44. package: Schema.String,
  45. url: Schema.String.pipe(Schema.optional),
  46. })
  47. const AnthropicMessages = Schema.Struct({
  48. type: Schema.Literal("anthropic/messages"),
  49. url: Schema.String,
  50. })
  51. const UnknownEndpoint = Schema.Struct({
  52. type: Schema.Literal("unknown"),
  53. })
  54. export const Endpoint = Schema.Union([
  55. UnknownEndpoint,
  56. OpenAIResponses,
  57. OpenAICompletions,
  58. AnthropicMessages,
  59. AISDK,
  60. ]).pipe(Schema.toTaggedUnion("type"))
  61. export type Endpoint = typeof Endpoint.Type
  62. export const Options = Schema.Struct({
  63. headers: Schema.Record(Schema.String, Schema.String),
  64. body: Schema.Record(Schema.String, Schema.Any),
  65. aisdk: Schema.Struct({
  66. provider: Schema.Record(Schema.String, Schema.Any),
  67. request: Schema.Record(Schema.String, Schema.Any),
  68. }),
  69. })
  70. export type Options = typeof Options.Type
  71. export class Info extends Schema.Class<Info>("ProviderV2.Info")({
  72. id: ID,
  73. name: Schema.String,
  74. enabled: Schema.Union([
  75. Schema.Literal(false),
  76. Schema.Struct({
  77. via: Schema.Literal("env"),
  78. name: Schema.String,
  79. }),
  80. Schema.Struct({
  81. via: Schema.Literal("account"),
  82. service: Schema.String,
  83. }),
  84. Schema.Struct({
  85. via: Schema.Literal("custom"),
  86. data: Schema.Record(Schema.String, Schema.Any),
  87. }),
  88. ]),
  89. env: Schema.String.pipe(Schema.Array),
  90. endpoint: Endpoint,
  91. options: Options,
  92. }) {
  93. static empty(providerID: ID): Info {
  94. return {
  95. id: providerID,
  96. name: providerID,
  97. enabled: false,
  98. env: [],
  99. endpoint: {
  100. type: "unknown",
  101. },
  102. options: {
  103. headers: {},
  104. body: {},
  105. aisdk: {
  106. provider: {},
  107. request: {},
  108. },
  109. },
  110. }
  111. }
  112. }