auth-options.types.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 Azure from "../src/providers/azure"
  6. import * as OpenAI from "../src/providers/openai"
  7. type BaseOptions = {
  8. readonly baseURL?: string
  9. readonly headers?: Record<string, string>
  10. }
  11. type Model = {
  12. readonly id: string
  13. }
  14. declare const auth: Auth
  15. declare const optionalAuthModel: ModelFactory<BaseOptions, "optional", Model>
  16. declare const requiredAuthModel: ModelFactory<BaseOptions, "required", Model>
  17. const configApiKey = Config.redacted("OPENAI_API_KEY")
  18. optionalAuthModel("gpt-4.1-mini")
  19. optionalAuthModel("gpt-4.1-mini", {})
  20. optionalAuthModel("gpt-4.1-mini", { apiKey: "sk-test" })
  21. optionalAuthModel("gpt-4.1-mini", { apiKey: configApiKey })
  22. optionalAuthModel("gpt-4.1-mini", { auth })
  23. optionalAuthModel("gpt-4.1-mini", { auth, baseURL: "https://gateway.example.com/v1" })
  24. optionalAuthModel("gpt-4.1-mini", { apiKey: "sk-test", headers: { "x-source": "test" } })
  25. // @ts-expect-error auth is an override, so apiKey cannot be supplied with it.
  26. optionalAuthModel("gpt-4.1-mini", { apiKey: "sk-test", auth })
  27. requiredAuthModel("custom-model", { apiKey: "key" })
  28. requiredAuthModel("custom-model", { apiKey: configApiKey })
  29. requiredAuthModel("custom-model", { auth })
  30. requiredAuthModel("custom-model", { auth, headers: { "x-tenant-id": "tenant" } })
  31. // @ts-expect-error providers without config fallback need apiKey or auth.
  32. requiredAuthModel("custom-model")
  33. // @ts-expect-error providers without config fallback need apiKey or auth.
  34. requiredAuthModel("custom-model", {})
  35. // @ts-expect-error auth is an override, so apiKey cannot be supplied with it.
  36. requiredAuthModel("custom-model", { apiKey: "key", auth })
  37. OpenAI.responses("gpt-4.1-mini")
  38. OpenAI.responses("gpt-4.1-mini", {})
  39. OpenAI.responses("gpt-4.1-mini", { apiKey: "sk-test" })
  40. OpenAI.responses("gpt-4.1-mini", { apiKey: configApiKey })
  41. OpenAI.responses("gpt-4.1-mini", { auth: RuntimeAuth.bearer("oauth-token") })
  42. OpenAI.responses("gpt-4.1-mini", {
  43. auth: RuntimeAuth.headers({ authorization: "Bearer gateway" }),
  44. baseURL: "https://gateway.example.com/v1",
  45. })
  46. OpenAI.responses("gpt-4.1-mini", {
  47. generation: { maxTokens: 100 },
  48. providerOptions: { openai: { store: false } },
  49. })
  50. // @ts-expect-error apiKey only accepts string, Redacted<string>, or Config<string | Redacted<string>>.
  51. OpenAI.responses("gpt-4.1-mini", { apiKey: 123 })
  52. // @ts-expect-error provider helpers reject unknown top-level options.
  53. OpenAI.responses("gpt-4.1-mini", { bogus: true })
  54. // @ts-expect-error common generation options remain typed.
  55. OpenAI.responses("gpt-4.1-mini", { generation: { maxTokens: "many" } })
  56. // @ts-expect-error provider-native options remain typed.
  57. OpenAI.responses("gpt-4.1-mini", { providerOptions: { openai: { store: "false" } } })
  58. // @ts-expect-error auth is an override, so OpenAI rejects apiKey with auth.
  59. OpenAI.responses("gpt-4.1-mini", { apiKey: "sk-test", auth: RuntimeAuth.bearer("oauth-token") })
  60. OpenAI.chat("gpt-4.1-mini")
  61. OpenAI.chat("gpt-4.1-mini", { apiKey: "sk-test" })
  62. OpenAI.chat("gpt-4.1-mini", { apiKey: configApiKey })
  63. OpenAI.chat("gpt-4.1-mini", { auth: RuntimeAuth.bearer("oauth-token") })
  64. // @ts-expect-error auth is an override, so OpenAI Chat rejects apiKey with auth.
  65. OpenAI.chat("gpt-4.1-mini", { apiKey: "sk-test", auth: RuntimeAuth.bearer("oauth-token") })
  66. // @ts-expect-error Azure requires at least one of `resourceName` or `baseURL`.
  67. Azure.responses("deployment")
  68. Azure.responses("deployment", { apiKey: "azure-key", resourceName: "resource" })
  69. Azure.responses("deployment", { apiKey: configApiKey, resourceName: "resource" })
  70. Azure.responses("deployment", { auth: RuntimeAuth.header("api-key", "azure-key"), resourceName: "resource" })
  71. // @ts-expect-error auth is an override, so Azure rejects apiKey with auth.
  72. Azure.responses("deployment", { apiKey: "azure-key", auth: RuntimeAuth.header("api-key", "override") })
  73. // @ts-expect-error Azure requires at least one of `resourceName` or `baseURL`.
  74. Azure.chat("deployment")
  75. Azure.chat("deployment", { apiKey: "azure-key", resourceName: "resource" })
  76. Azure.chat("deployment", { apiKey: configApiKey, resourceName: "resource" })
  77. Azure.chat("deployment", { auth: RuntimeAuth.header("api-key", "azure-key"), resourceName: "resource" })
  78. // @ts-expect-error auth is an override, so Azure Chat rejects apiKey with auth.
  79. Azure.chat("deployment", { apiKey: "azure-key", auth: RuntimeAuth.header("api-key", "override") })