-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathgateway.ts
More file actions
63 lines (57 loc) · 1.71 KB
/
gateway.ts
File metadata and controls
63 lines (57 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { createGatewayProvider } from '@ai-sdk/gateway'
import { Models } from './constants'
import type { JSONValue } from 'ai'
import type { OpenAIResponsesProviderOptions } from '@ai-sdk/openai'
import type { LanguageModelV2 } from '@ai-sdk/provider'
export async function getAvailableModels() {
const gateway = gatewayInstance()
const response = await gateway.getAvailableModels()
return response.models.map((model) => ({ id: model.id, name: model.name }))
}
export interface ModelOptions {
model: LanguageModelV2
providerOptions?: Record<string, Record<string, JSONValue>>
headers?: Record<string, string>
}
export function getModelOptions(
modelId: string,
options?: { reasoningEffort?: 'minimal' | 'low' | 'medium' }
): ModelOptions {
const gateway = gatewayInstance()
if (modelId === Models.OpenAIGPT5) {
return {
model: gateway(modelId),
providerOptions: {
openai: {
include: ['reasoning.encrypted_content'],
reasoningEffort: options?.reasoningEffort ?? 'low',
reasoningSummary: 'auto',
serviceTier: 'priority',
} satisfies OpenAIResponsesProviderOptions,
},
}
}
if (
modelId === Models.AnthropicClaude4Sonnet ||
modelId === Models.AnthropicClaude45Sonnet ||
modelId === Models.AnthropicClaude45Haiku
) {
return {
model: gateway(modelId),
headers: { 'anthropic-beta': 'fine-grained-tool-streaming-2025-05-14' },
providerOptions: {
anthropic: {
cacheControl: { type: 'ephemeral' },
},
},
}
}
return {
model: gateway(modelId),
}
}
function gatewayInstance() {
return createGatewayProvider({
baseURL: process.env.AI_GATEWAY_BASE_URL,
})
}