| 375 | } |
| 376 | |
| 377 | public async getModels( |
| 378 | config: LLMConfig |
| 379 | ): Promise<{ success: boolean; models?: string[]; error?: string }> { |
| 380 | try { |
| 381 | const response = await fetch(`${config.baseUrl.replace(/\/$/, '')}/models`, { |
| 382 | method: 'GET', |
| 383 | headers: { |
| 384 | Authorization: `Bearer ${config.apiKey}` |
| 385 | } |
| 386 | }) |
| 387 | |
| 388 | if (!response.ok) { |
| 389 | return { |
| 390 | success: false, |
| 391 | error: `HTTP ${response.status}: ${response.statusText}` |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | const data = await response.json() |
| 396 | const models = data.data?.map((model: { id: string }) => model.id) || [] |
| 397 | return { success: true, models } |
| 398 | } catch (error) { |
| 399 | return { |
| 400 | success: false, |
| 401 | error: error instanceof Error ? error.message : 'Failed to fetch models' |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | export const aiHandler = new AIHandler() |