MCPcopy Index your code
hub / github.com/ChatLab/ChatLab / fetchRemoteModels

Function fetchRemoteModels

packages/node-runtime/src/ai/remote-api.ts:33–130  ·  view source on GitHub ↗
(
  provider: string,
  apiKey: string,
  baseUrl?: string,
  apiFormat?: string,
  options?: RemoteApiOptions
)

Source from the content-addressed store, hash-verified

31}
32
33export async function fetchRemoteModels(
34 provider: string,
35 apiKey: string,
36 baseUrl?: string,
37 apiFormat?: string,
38 options?: RemoteApiOptions
39): Promise<FetchRemoteModelsResult> {
40 const effectiveApiFormat = apiFormat || 'openai-completions'
41
42 if (effectiveApiFormat === 'anthropic-messages') {
43 return { success: false, error: 'Anthropic does not support model listing via API' }
44 }
45
46 const providerDef = BUILTIN_PROVIDERS.find((p) => p.id === provider)
47 const rawBaseUrl = baseUrl || providerDef?.defaultBaseUrl || ''
48 if (!rawBaseUrl) {
49 return { success: false, error: 'No base URL provided' }
50 }
51
52 const abortController = new AbortController()
53 const timeout = setTimeout(() => abortController.abort(), 15000)
54
55 try {
56 let url: string
57 const headers: Record<string, string> = { ...options?.headers }
58
59 if (effectiveApiFormat === 'google-generative-ai') {
60 const trimmed = rawBaseUrl.replace(/\/+$/, '').replace(/\/v1(beta)?$/, '')
61 url = `${trimmed}/v1beta/models?key=${apiKey}`
62 } else {
63 const resolved = normalizeOpenAICompatibleBaseUrl(rawBaseUrl)
64 url = `${resolved}/models`
65 headers['Authorization'] = `Bearer ${apiKey}`
66 }
67
68 options?.onLog?.('info', 'LLM', 'Fetching remote models', {
69 url: url.replace(/key=[^&]+/, 'key=***'),
70 provider,
71 })
72
73 const response = await fetch(url, {
74 method: 'GET',
75 headers,
76 signal: abortController.signal,
77 })
78
79 if (!response.ok) {
80 const body = await response.text().catch(() => '')
81 return { success: false, error: `HTTP ${response.status}: ${body.slice(0, 200)}` }
82 }
83
84 const json = await response.json()
85
86 let models: RemoteModel[]
87
88 if (effectiveApiFormat === 'google-generative-ai') {
89 const geminiModels = (json.models || []) as Array<{
90 name?: string

Callers 1

registerAiLlmRoutesFunction · 0.90

Calls 5

setTimeoutFunction · 0.85
clearTimeoutFunction · 0.85
abortMethod · 0.80
textMethod · 0.80

Tested by

no test coverage detected