auth-options.types.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import { Config } from "effect"
  2. import type { Auth } from "../src/route/auth"
  3. import type { ModelFactory } from "../src/route/auth-options"
  4. import { Auth as RuntimeAuth } from "../src/route/auth"
  5. import * as OpenAIChat from "../src/protocols/openai-chat"
  6. import * as AmazonBedrock from "../src/providers/amazon-bedrock"
  7. import * as Anthropic from "../src/providers/anthropic"
  8. import * as Azure from "../src/providers/azure"
  9. import * as Cloudflare from "../src/providers/cloudflare"
  10. import * as GitHubCopilot from "../src/providers/github-copilot"
  11. import * as Google from "../src/providers/google"
  12. import * as OpenAI from "../src/providers/openai"
  13. import * as OpenAICompatible from "../src/providers/openai-compatible"
  14. import * as OpenRouter from "../src/providers/openrouter"
  15. import * as XAI from "../src/providers/xai"
  16. type BaseOptions = {
  17. readonly baseURL?: string
  18. readonly headers?: Record<string, string>
  19. }
  20. type Model = {
  21. readonly id: string
  22. }
  23. declare const auth: Auth
  24. declare const optionalAuthModel: ModelFactory<BaseOptions, "optional", Model>
  25. declare const requiredAuthModel: ModelFactory<BaseOptions, "required", Model>
  26. const configApiKey = Config.redacted("OPENAI_API_KEY")
  27. OpenAIChat.route.model({ id: "gpt-4.1-mini" })
  28. // @ts-expect-error route model selection does not configure endpoints.
  29. OpenAIChat.route.model({ id: "gpt-4.1-mini", baseURL: "https://gateway.example.com/v1" })
  30. // @ts-expect-error route model selection does not configure query params.
  31. OpenAIChat.route.model({ id: "gpt-4.1-mini", queryParams: { debug: "1" } })
  32. // @ts-expect-error route model selection does not configure auth.
  33. OpenAIChat.route.model({ id: "gpt-4.1-mini", auth })
  34. // @ts-expect-error route model selection does not configure api keys.
  35. OpenAIChat.route.model({ id: "gpt-4.1-mini", apiKey: "sk-test" })
  36. optionalAuthModel("gpt-4.1-mini")
  37. optionalAuthModel("gpt-4.1-mini", {})
  38. optionalAuthModel("gpt-4.1-mini", { apiKey: "sk-test" })
  39. optionalAuthModel("gpt-4.1-mini", { apiKey: configApiKey })
  40. optionalAuthModel("gpt-4.1-mini", { auth })
  41. optionalAuthModel("gpt-4.1-mini", { auth, baseURL: "https://gateway.example.com/v1" })
  42. optionalAuthModel("gpt-4.1-mini", { apiKey: "sk-test", headers: { "x-source": "test" } })
  43. // @ts-expect-error auth is an override, so apiKey cannot be supplied with it.
  44. optionalAuthModel("gpt-4.1-mini", { apiKey: "sk-test", auth })
  45. requiredAuthModel("custom-model", { apiKey: "key" })
  46. requiredAuthModel("custom-model", { apiKey: configApiKey })
  47. requiredAuthModel("custom-model", { auth })
  48. requiredAuthModel("custom-model", { auth, headers: { "x-tenant-id": "tenant" } })
  49. // @ts-expect-error providers without config fallback need apiKey or auth.
  50. requiredAuthModel("custom-model")
  51. // @ts-expect-error providers without config fallback need apiKey or auth.
  52. requiredAuthModel("custom-model", {})
  53. // @ts-expect-error auth is an override, so apiKey cannot be supplied with it.
  54. requiredAuthModel("custom-model", { apiKey: "key", auth })
  55. OpenAI.responses("gpt-4.1-mini")
  56. OpenAI.configure({}).responses("gpt-4.1-mini")
  57. OpenAI.configure({ apiKey: "sk-test" }).responses("gpt-4.1-mini")
  58. OpenAI.configure({ apiKey: configApiKey }).responses("gpt-4.1-mini")
  59. OpenAI.configure({ auth: RuntimeAuth.bearer("oauth-token") }).responses("gpt-4.1-mini")
  60. OpenAI.configure({
  61. auth: RuntimeAuth.headers({ authorization: "Bearer gateway" }),
  62. baseURL: "https://gateway.example.com/v1",
  63. }).responses("gpt-4.1-mini")
  64. OpenAI.configure({
  65. generation: { maxTokens: 100 },
  66. providerOptions: { openai: { store: false } },
  67. }).responses("gpt-4.1-mini")
  68. // @ts-expect-error OpenAI model selectors only accept model ids.
  69. OpenAI.configure({ apiKey: "sk-test" }).responses("gpt-4.1-mini", {})
  70. // @ts-expect-error apiKey only accepts string, Redacted<string>, or Config<string | Redacted<string>>.
  71. OpenAI.configure({ apiKey: 123 })
  72. // @ts-expect-error provider helpers reject unknown top-level options.
  73. OpenAI.configure({ bogus: true })
  74. // @ts-expect-error common generation options remain typed.
  75. OpenAI.configure({ generation: { maxTokens: "many" } })
  76. // @ts-expect-error provider-native options remain typed.
  77. OpenAI.configure({ providerOptions: { openai: { store: "false" } } })
  78. // @ts-expect-error auth is an override, so OpenAI rejects apiKey with auth.
  79. OpenAI.configure({ apiKey: "sk-test", auth: RuntimeAuth.bearer("oauth-token") })
  80. OpenAI.chat("gpt-4.1-mini")
  81. OpenAI.configure({ apiKey: "sk-test" }).chat("gpt-4.1-mini")
  82. OpenAI.configure({ apiKey: configApiKey }).chat("gpt-4.1-mini")
  83. OpenAI.configure({ auth: RuntimeAuth.bearer("oauth-token") }).chat("gpt-4.1-mini")
  84. // @ts-expect-error OpenAI chat selectors only accept model ids.
  85. OpenAI.configure({ apiKey: "sk-test" }).chat("gpt-4.1-mini", {})
  86. // @ts-expect-error auth is an override, so OpenAI Chat rejects apiKey with auth.
  87. OpenAI.configure({ apiKey: "sk-test", auth: RuntimeAuth.bearer("oauth-token") })
  88. // @ts-expect-error Azure requires at least one of `resourceName` or `baseURL`.
  89. Azure.configure()
  90. Azure.configure({ apiKey: "azure-key", resourceName: "resource" }).responses("deployment")
  91. Azure.configure({ apiKey: configApiKey, resourceName: "resource" }).responses("deployment")
  92. Azure.configure({ auth: RuntimeAuth.header("api-key", "azure-key"), resourceName: "resource" }).responses("deployment")
  93. // @ts-expect-error Azure model selectors only accept deployment ids.
  94. Azure.configure({ apiKey: "azure-key", resourceName: "resource" }).responses("deployment", {})
  95. // @ts-expect-error auth is an override, so Azure rejects apiKey with auth.
  96. Azure.configure({ resourceName: "resource", apiKey: "azure-key", auth: RuntimeAuth.header("api-key", "override") })
  97. Azure.configure({ apiKey: "azure-key", resourceName: "resource" }).chat("deployment")
  98. Azure.configure({ apiKey: configApiKey, resourceName: "resource" }).chat("deployment")
  99. Azure.configure({ auth: RuntimeAuth.header("api-key", "azure-key"), resourceName: "resource" }).chat("deployment")
  100. // @ts-expect-error Azure chat model selectors only accept deployment ids.
  101. Azure.configure({ apiKey: "azure-key", resourceName: "resource" }).chat("deployment", {})
  102. // @ts-expect-error auth is an override, so Azure Chat rejects apiKey with auth.
  103. Azure.configure({ resourceName: "resource", apiKey: "azure-key", auth: RuntimeAuth.header("api-key", "override") })
  104. Anthropic.configure({ apiKey: "anthropic-key" }).model("claude-haiku")
  105. // @ts-expect-error Anthropic model selectors only accept model ids.
  106. Anthropic.configure({ apiKey: "anthropic-key" }).model("claude-haiku", {})
  107. Google.configure({ apiKey: "google-key" }).model("gemini-2.5-flash")
  108. // @ts-expect-error Google model selectors only accept model ids.
  109. Google.configure({ apiKey: "google-key" }).model("gemini-2.5-flash", {})
  110. AmazonBedrock.configure({ apiKey: "bedrock-key" }).model("anthropic.claude")
  111. // @ts-expect-error Bedrock model selectors only accept model ids.
  112. AmazonBedrock.configure({ apiKey: "bedrock-key" }).model("anthropic.claude", {})
  113. OpenRouter.configure({ apiKey: "openrouter-key" }).model("openai/gpt-4o-mini")
  114. // @ts-expect-error OpenRouter model selectors only accept model ids.
  115. OpenRouter.configure({ apiKey: "openrouter-key" }).model("openai/gpt-4o-mini", {})
  116. XAI.configure({ apiKey: "xai-key" }).responses("grok-4")
  117. XAI.configure({ apiKey: "xai-key" }).chat("grok-4")
  118. // @ts-expect-error xAI Responses selectors only accept model ids.
  119. XAI.configure({ apiKey: "xai-key" }).responses("grok-4", {})
  120. // @ts-expect-error xAI Chat selectors only accept model ids.
  121. XAI.configure({ apiKey: "xai-key" }).chat("grok-4", {})
  122. OpenAICompatible.deepseek.configure({ apiKey: "deepseek-key" }).model("deepseek-chat")
  123. // @ts-expect-error OpenAI-compatible family selectors only accept model ids.
  124. OpenAICompatible.deepseek.configure({ apiKey: "deepseek-key" }).model("deepseek-chat", {})
  125. Cloudflare.CloudflareWorkersAI.configure({ accountId: "account", apiKey: "cf-key" }).model("@cf/meta/llama")
  126. // @ts-expect-error Cloudflare Workers AI model selectors only accept model ids.
  127. Cloudflare.CloudflareWorkersAI.configure({ accountId: "account", apiKey: "cf-key" }).model("@cf/meta/llama", {})
  128. GitHubCopilot.configure({ baseURL: "https://copilot.test", apiKey: "copilot-key" }).model("gpt-4.1")
  129. // @ts-expect-error GitHub Copilot model selectors only accept model ids.
  130. GitHubCopilot.configure({ baseURL: "https://copilot.test", apiKey: "copilot-key" }).model("gpt-4.1", {})