1
0

auth-options.types.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 AnthropicCompatible from "../src/providers/anthropic-compatible"
  9. import * as Azure from "../src/providers/azure"
  10. import * as Cloudflare from "../src/providers/cloudflare"
  11. import * as GitHubCopilot from "../src/providers/github-copilot"
  12. import * as Google from "../src/providers/google"
  13. import * as GoogleVertex from "../src/providers/google-vertex"
  14. import * as GoogleVertexChat from "../src/providers/google-vertex-chat"
  15. import * as GoogleVertexMessages from "../src/providers/google-vertex-messages"
  16. import * as GoogleVertexResponses from "../src/providers/google-vertex-responses"
  17. import * as OpenAI from "../src/providers/openai"
  18. import * as OpenAICompatible from "../src/providers/openai-compatible"
  19. import * as OpenRouter from "../src/providers/openrouter"
  20. import * as XAI from "../src/providers/xai"
  21. type BaseOptions = {
  22. readonly baseURL?: string
  23. readonly headers?: Record<string, string>
  24. }
  25. type Model = {
  26. readonly id: string
  27. }
  28. declare const auth: Auth
  29. declare const optionalAuthModel: ModelFactory<BaseOptions, "optional", Model>
  30. declare const requiredAuthModel: ModelFactory<BaseOptions, "required", Model>
  31. const configApiKey = Config.redacted("OPENAI_API_KEY")
  32. OpenAIChat.route.model({ id: "gpt-4.1-mini" })
  33. // @ts-expect-error route model selection does not configure endpoints.
  34. OpenAIChat.route.model({ id: "gpt-4.1-mini", baseURL: "https://gateway.example.com/v1" })
  35. // @ts-expect-error route model selection does not configure query params.
  36. OpenAIChat.route.model({ id: "gpt-4.1-mini", queryParams: { debug: "1" } })
  37. // @ts-expect-error route model selection does not configure auth.
  38. OpenAIChat.route.model({ id: "gpt-4.1-mini", auth })
  39. // @ts-expect-error route model selection does not configure api keys.
  40. OpenAIChat.route.model({ id: "gpt-4.1-mini", apiKey: "sk-test" })
  41. optionalAuthModel("gpt-4.1-mini")
  42. optionalAuthModel("gpt-4.1-mini", {})
  43. optionalAuthModel("gpt-4.1-mini", { apiKey: "sk-test" })
  44. optionalAuthModel("gpt-4.1-mini", { apiKey: configApiKey })
  45. optionalAuthModel("gpt-4.1-mini", { auth })
  46. optionalAuthModel("gpt-4.1-mini", { auth, baseURL: "https://gateway.example.com/v1" })
  47. optionalAuthModel("gpt-4.1-mini", { apiKey: "sk-test", headers: { "x-source": "test" } })
  48. // @ts-expect-error auth is an override, so apiKey cannot be supplied with it.
  49. optionalAuthModel("gpt-4.1-mini", { apiKey: "sk-test", auth })
  50. requiredAuthModel("custom-model", { apiKey: "key" })
  51. requiredAuthModel("custom-model", { apiKey: configApiKey })
  52. requiredAuthModel("custom-model", { auth })
  53. requiredAuthModel("custom-model", { auth, headers: { "x-tenant-id": "tenant" } })
  54. // @ts-expect-error providers without config fallback need apiKey or auth.
  55. requiredAuthModel("custom-model")
  56. // @ts-expect-error providers without config fallback need apiKey or auth.
  57. requiredAuthModel("custom-model", {})
  58. // @ts-expect-error auth is an override, so apiKey cannot be supplied with it.
  59. requiredAuthModel("custom-model", { apiKey: "key", auth })
  60. OpenAI.responses("gpt-4.1-mini")
  61. OpenAI.configure({}).responses("gpt-4.1-mini")
  62. OpenAI.configure({ apiKey: "sk-test" }).responses("gpt-4.1-mini")
  63. OpenAI.configure({ apiKey: configApiKey }).responses("gpt-4.1-mini")
  64. OpenAI.configure({ auth: RuntimeAuth.bearer("oauth-token") }).responses("gpt-4.1-mini")
  65. OpenAI.configure({
  66. auth: RuntimeAuth.headers({ authorization: "Bearer gateway" }),
  67. baseURL: "https://gateway.example.com/v1",
  68. }).responses("gpt-4.1-mini")
  69. OpenAI.configure({
  70. generation: { maxTokens: 100 },
  71. providerOptions: { openai: { store: false } },
  72. }).responses("gpt-4.1-mini")
  73. // @ts-expect-error OpenAI model selectors only accept model ids.
  74. OpenAI.configure({ apiKey: "sk-test" }).responses("gpt-4.1-mini", {})
  75. // @ts-expect-error apiKey only accepts string, Redacted<string>, or Config<string | Redacted<string>>.
  76. OpenAI.configure({ apiKey: 123 })
  77. // @ts-expect-error provider helpers reject unknown top-level options.
  78. OpenAI.configure({ bogus: true })
  79. // @ts-expect-error common generation options remain typed.
  80. OpenAI.configure({ generation: { maxTokens: "many" } })
  81. // @ts-expect-error provider-native options remain typed.
  82. OpenAI.configure({ providerOptions: { openai: { store: "false" } } })
  83. // @ts-expect-error auth is an override, so OpenAI rejects apiKey with auth.
  84. OpenAI.configure({ apiKey: "sk-test", auth: RuntimeAuth.bearer("oauth-token") })
  85. OpenAI.chat("gpt-4.1-mini")
  86. OpenAI.configure({ apiKey: "sk-test" }).chat("gpt-4.1-mini")
  87. OpenAI.configure({ apiKey: configApiKey }).chat("gpt-4.1-mini")
  88. OpenAI.configure({ auth: RuntimeAuth.bearer("oauth-token") }).chat("gpt-4.1-mini")
  89. // @ts-expect-error OpenAI chat selectors only accept model ids.
  90. OpenAI.configure({ apiKey: "sk-test" }).chat("gpt-4.1-mini", {})
  91. // @ts-expect-error auth is an override, so OpenAI Chat rejects apiKey with auth.
  92. OpenAI.configure({ apiKey: "sk-test", auth: RuntimeAuth.bearer("oauth-token") })
  93. // @ts-expect-error Azure requires at least one of `resourceName` or `baseURL`.
  94. Azure.configure()
  95. Azure.configure({ apiKey: "azure-key", resourceName: "resource" }).responses("deployment")
  96. Azure.configure({ apiKey: configApiKey, resourceName: "resource" }).responses("deployment")
  97. Azure.configure({ auth: RuntimeAuth.header("api-key", "azure-key"), resourceName: "resource" }).responses("deployment")
  98. // @ts-expect-error Azure model selectors only accept deployment ids.
  99. Azure.configure({ apiKey: "azure-key", resourceName: "resource" }).responses("deployment", {})
  100. // @ts-expect-error auth is an override, so Azure rejects apiKey with auth.
  101. Azure.configure({ resourceName: "resource", apiKey: "azure-key", auth: RuntimeAuth.header("api-key", "override") })
  102. Azure.configure({ apiKey: "azure-key", resourceName: "resource" }).chat("deployment")
  103. Azure.configure({ apiKey: configApiKey, resourceName: "resource" }).chat("deployment")
  104. Azure.configure({ auth: RuntimeAuth.header("api-key", "azure-key"), resourceName: "resource" }).chat("deployment")
  105. // @ts-expect-error Azure chat model selectors only accept deployment ids.
  106. Azure.configure({ apiKey: "azure-key", resourceName: "resource" }).chat("deployment", {})
  107. // @ts-expect-error auth is an override, so Azure Chat rejects apiKey with auth.
  108. Azure.configure({ resourceName: "resource", apiKey: "azure-key", auth: RuntimeAuth.header("api-key", "override") })
  109. Anthropic.configure({ apiKey: "anthropic-key" }).model("claude-haiku")
  110. // @ts-expect-error Anthropic model selectors only accept model ids.
  111. Anthropic.configure({ apiKey: "anthropic-key" }).model("claude-haiku", {})
  112. // @ts-expect-error Anthropic package settings accept only one auth source.
  113. Anthropic.model("claude-sonnet-4-6", { apiKey: "anthropic-key", authToken: "anthropic-token" })
  114. AnthropicCompatible.configure({
  115. apiKey: "messages-key",
  116. baseURL: "https://messages.example.com/v1",
  117. provider: "example",
  118. }).model("compatible-model")
  119. // @ts-expect-error Anthropic-compatible providers require a base URL.
  120. AnthropicCompatible.configure({ apiKey: "messages-key" })
  121. // @ts-expect-error Anthropic-compatible model selectors only accept model ids.
  122. AnthropicCompatible.configure({ baseURL: "https://messages.example.com/v1" }).model("compatible-model", {})
  123. // @ts-expect-error Anthropic-compatible package settings accept only one auth source.
  124. AnthropicCompatible.model("compatible-model", {
  125. apiKey: "messages-key",
  126. authToken: "messages-token",
  127. baseURL: "https://messages.example.com/v1",
  128. })
  129. Google.configure({ apiKey: "google-key" }).model("gemini-2.5-flash")
  130. // @ts-expect-error Google model selectors only accept model ids.
  131. Google.configure({ apiKey: "google-key" }).model("gemini-2.5-flash", {})
  132. GoogleVertex.configure({ apiKey: "vertex-key" }).model("gemini-3.5-flash")
  133. GoogleVertex.configure({ accessToken: "vertex-token", project: "project" }).model("gemini-3.5-flash")
  134. GoogleVertex.configure({ auth: RuntimeAuth.bearer("vertex-token"), project: "project" }).model("gemini-3.5-flash")
  135. // @ts-expect-error Vertex Gemini model selectors only accept model ids.
  136. GoogleVertex.configure({ apiKey: "vertex-key" }).model("gemini-3.5-flash", {})
  137. // @ts-expect-error Vertex Gemini config accepts only one auth source.
  138. GoogleVertex.configure({ accessToken: "vertex-token", apiKey: "vertex-key", project: "project" })
  139. // @ts-expect-error Vertex Gemini package settings accept only one auth source.
  140. GoogleVertex.model("gemini-3.5-flash", { accessToken: "vertex-token", apiKey: "vertex-key", project: "project" })
  141. GoogleVertexChat.configure({ accessToken: "vertex-token", project: "project" }).model("deepseek-ai/deepseek-v3.2-maas")
  142. GoogleVertexChat.configure({ auth: RuntimeAuth.bearer("vertex-token"), project: "project" }).model(
  143. "deepseek-ai/deepseek-v3.2-maas",
  144. )
  145. // @ts-expect-error Vertex Chat package settings do not accept API keys.
  146. GoogleVertexChat.model("deepseek-ai/deepseek-v3.2-maas", { apiKey: "vertex-key", project: "project" })
  147. GoogleVertexChat.configure({ accessToken: "vertex-token", project: "project" }).model(
  148. "deepseek-ai/deepseek-v3.2-maas",
  149. // @ts-expect-error Vertex Chat model selectors only accept model ids.
  150. {},
  151. )
  152. GoogleVertexChat.configure({
  153. accessToken: "vertex-token",
  154. // @ts-expect-error Vertex Chat config accepts only one auth source.
  155. auth: RuntimeAuth.bearer("vertex-token"),
  156. project: "project",
  157. })
  158. GoogleVertexResponses.configure({ accessToken: "vertex-token", project: "project" }).model("xai/grok-4.20-reasoning")
  159. GoogleVertexResponses.configure({ auth: RuntimeAuth.bearer("vertex-token"), project: "project" }).model(
  160. "xai/grok-4.20-reasoning",
  161. )
  162. // @ts-expect-error Vertex Responses package settings do not accept API keys.
  163. GoogleVertexResponses.model("xai/grok-4.20-reasoning", { apiKey: "vertex-key", project: "project" })
  164. GoogleVertexResponses.configure({ accessToken: "vertex-token", project: "project" }).model(
  165. "xai/grok-4.20-reasoning",
  166. // @ts-expect-error Vertex Responses model selectors only accept model ids.
  167. {},
  168. )
  169. GoogleVertexResponses.configure({
  170. accessToken: "vertex-token",
  171. // @ts-expect-error Vertex Responses config accepts only one auth source.
  172. auth: RuntimeAuth.bearer("vertex-token"),
  173. project: "project",
  174. })
  175. GoogleVertexMessages.configure({ accessToken: "vertex-token", project: "project" }).model("claude-sonnet-4-6")
  176. // @ts-expect-error Vertex Messages package settings do not accept API keys.
  177. GoogleVertexMessages.model("claude-sonnet-4-6", { apiKey: "vertex-key", project: "project" })
  178. GoogleVertexMessages.configure({ auth: RuntimeAuth.bearer("vertex-token"), project: "project" }).model(
  179. "claude-sonnet-4-6",
  180. )
  181. GoogleVertexMessages.configure({ accessToken: "vertex-token", project: "project" }).model(
  182. "claude-sonnet-4-6",
  183. // @ts-expect-error Vertex Messages model selectors only accept model ids.
  184. {},
  185. )
  186. GoogleVertexMessages.configure({
  187. accessToken: "vertex-token",
  188. // @ts-expect-error Vertex Messages config accepts only one auth source.
  189. auth: RuntimeAuth.bearer("vertex-token"),
  190. project: "project",
  191. })
  192. AmazonBedrock.configure({ apiKey: "bedrock-key" }).model("anthropic.claude")
  193. // @ts-expect-error Bedrock model selectors only accept model ids.
  194. AmazonBedrock.configure({ apiKey: "bedrock-key" }).model("anthropic.claude", {})
  195. OpenRouter.configure({ apiKey: "openrouter-key" }).model("openai/gpt-4o-mini")
  196. // @ts-expect-error OpenRouter model selectors only accept model ids.
  197. OpenRouter.configure({ apiKey: "openrouter-key" }).model("openai/gpt-4o-mini", {})
  198. XAI.configure({ apiKey: "xai-key" }).responses("grok-4")
  199. XAI.configure({ apiKey: "xai-key" }).chat("grok-4")
  200. // @ts-expect-error xAI Responses selectors only accept model ids.
  201. XAI.configure({ apiKey: "xai-key" }).responses("grok-4", {})
  202. // @ts-expect-error xAI Chat selectors only accept model ids.
  203. XAI.configure({ apiKey: "xai-key" }).chat("grok-4", {})
  204. OpenAICompatible.deepseek.configure({ apiKey: "deepseek-key" }).model("deepseek-chat")
  205. // @ts-expect-error OpenAI-compatible family selectors only accept model ids.
  206. OpenAICompatible.deepseek.configure({ apiKey: "deepseek-key" }).model("deepseek-chat", {})
  207. Cloudflare.CloudflareWorkersAI.configure({ accountId: "account", apiKey: "cf-key" }).model("@cf/meta/llama")
  208. // @ts-expect-error Cloudflare Workers AI model selectors only accept model ids.
  209. Cloudflare.CloudflareWorkersAI.configure({ accountId: "account", apiKey: "cf-key" }).model("@cf/meta/llama", {})
  210. GitHubCopilot.configure({ baseURL: "https://copilot.test", apiKey: "copilot-key" }).model("gpt-4.1")
  211. // @ts-expect-error GitHub Copilot model selectors only accept model ids.
  212. GitHubCopilot.configure({ baseURL: "https://copilot.test", apiKey: "copilot-key" }).model("gpt-4.1", {})