provider.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 AISDK = Schema.Struct({
  23. type: Schema.Literal("aisdk"),
  24. package: Schema.String,
  25. url: Schema.String.pipe(Schema.optional),
  26. settings: Schema.Record(Schema.String, Schema.Unknown).pipe(Schema.optional),
  27. })
  28. export const Native = Schema.Struct({
  29. type: Schema.Literal("native"),
  30. url: Schema.String.pipe(Schema.optional),
  31. settings: Schema.Record(Schema.String, Schema.Unknown),
  32. })
  33. export const Api = Schema.Union([AISDK, Native]).pipe(Schema.toTaggedUnion("type"))
  34. export type Api = typeof Api.Type
  35. export const Request = Schema.Struct({
  36. headers: Schema.Record(Schema.String, Schema.String),
  37. body: Schema.Record(Schema.String, Schema.Any),
  38. })
  39. export type Request = typeof Request.Type
  40. export class Info extends Schema.Class<Info>("ProviderV2.Info")({
  41. id: ID,
  42. name: Schema.String,
  43. enabled: Schema.Union([
  44. Schema.Literal(false),
  45. Schema.Struct({
  46. via: Schema.Literal("env"),
  47. name: Schema.String,
  48. }),
  49. Schema.Struct({
  50. via: Schema.Literal("account"),
  51. service: Schema.String,
  52. }),
  53. Schema.Struct({
  54. via: Schema.Literal("custom"),
  55. data: Schema.Record(Schema.String, Schema.Any),
  56. }),
  57. ]),
  58. env: Schema.String.pipe(Schema.Array),
  59. api: Api,
  60. request: Request,
  61. }) {
  62. static empty(providerID: ID): Info {
  63. return new Info({
  64. id: providerID,
  65. name: providerID,
  66. enabled: false,
  67. env: [],
  68. api: {
  69. type: "native",
  70. settings: {},
  71. },
  72. request: {
  73. headers: {},
  74. body: {},
  75. },
  76. })
  77. }
  78. }