google.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import type { RouteDefaultsInput } from "../route/client"
  2. import { Auth } from "../route/auth"
  3. import type { ProviderAuthOption } from "../route/auth-options"
  4. import { ProviderID, type ModelID } from "../schema"
  5. import * as Gemini from "../protocols/gemini"
  6. export const id = ProviderID.make("google")
  7. export const routes = [Gemini.route]
  8. export type Config = RouteDefaultsInput & ProviderAuthOption<"optional"> & { readonly baseURL?: string }
  9. const auth = (options: ProviderAuthOption<"optional">) => {
  10. if ("auth" in options && options.auth) return options.auth
  11. return Auth.optional("apiKey" in options ? options.apiKey : undefined, "apiKey")
  12. .orElse(Auth.config("GOOGLE_GENERATIVE_AI_API_KEY"))
  13. .pipe(Auth.header("x-goog-api-key"))
  14. }
  15. const configuredRoute = (input: Config) => {
  16. const { apiKey: _, auth: _auth, baseURL, ...rest } = input
  17. return Gemini.route.with({ ...rest, endpoint: { baseURL }, auth: auth(input) })
  18. }
  19. export const configure = (input: Config = {}) => {
  20. const route = configuredRoute(input)
  21. return {
  22. id,
  23. model: (modelID: string | ModelID) => route.model({ id: modelID }),
  24. configure,
  25. }
  26. }
  27. export const provider = configure()
  28. export const model = provider.model