Open History to see the last commit
1import { getApiKeyFromEnv } from '@tanstack/ai-utils'
2import type { ClientOptions } from 'openai'
3
4export interface VercelGatewayClientConfig extends Omit<
5 ClientOptions,
6 'apiKey'
7> {
8 apiKey: string
9 httpReferer?: string
10 xTitle?: string
11}
12
13export function getVercelGatewayApiKeyFromEnv(): string {
14 try {
15 return getApiKeyFromEnv('AI_GATEWAY_API_KEY')
16 } catch {
17 try {
18 return getApiKeyFromEnv('VERCEL_OIDC_TOKEN')
19 } catch {
20 throw new Error(
21 'AI_GATEWAY_API_KEY or VERCEL_OIDC_TOKEN is required. Set one in the environment or pass apiKey to the factory.',
22 )
23 }
24 }
25}
26
27export function withVercelGatewayDefaults(
28 config: VercelGatewayClientConfig,
29): ClientOptions {
30 const { httpReferer, xTitle, defaultHeaders, ...rest } = config
31 return {
32 ...rest,
33 baseURL: config.baseURL || 'https://ai-gateway.vercel.sh/v1',
34 defaultHeaders: {
35 ...(httpReferer ? { 'http-referer': httpReferer } : {}),
36 ...(xTitle ? { 'x-title': xTitle } : {}),
37 ...defaultHeaders,
38 },
39 }
40}