auth-options.types.ts 13 KB

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