| 343 | } |
| 344 | |
| 345 | public async testConnection(config: LLMConfig): Promise<{ success: boolean; error?: string }> { |
| 346 | try { |
| 347 | const response = await fetch(`${config.baseUrl.replace(/\/$/, '')}/chat/completions`, { |
| 348 | method: 'POST', |
| 349 | headers: { |
| 350 | 'Content-Type': 'application/json', |
| 351 | Authorization: `Bearer ${config.apiKey}` |
| 352 | }, |
| 353 | body: JSON.stringify({ |
| 354 | model: config.modelName, |
| 355 | messages: [{ role: 'user', content: 'Hi' }], |
| 356 | max_tokens: 5 |
| 357 | }) |
| 358 | }) |
| 359 | |
| 360 | if (response.ok) { |
| 361 | return { success: true } |
| 362 | } |
| 363 | |
| 364 | const errorText = await response.text() |
| 365 | return { |
| 366 | success: false, |
| 367 | error: `HTTP ${response.status}: ${errorText || response.statusText}` |
| 368 | } |
| 369 | } catch (error) { |
| 370 | return { |
| 371 | success: false, |
| 372 | error: error instanceof Error ? error.message : 'Connection failed' |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | public async getModels( |
| 378 | config: LLMConfig |