* Prefer an explicit model, then a still-live configured model. Legacy * connections without a discovered inventory keep the historical * default/fallback order.
( connection: ConnectionEffectConnection, model: string | undefined, fallbackModels: readonly string[], )
| 39 | * default/fallback order. |
| 40 | */ |
| 41 | function resolveConnectionTestModel( |
| 42 | connection: ConnectionEffectConnection, |
| 43 | model: string | undefined, |
| 44 | fallbackModels: readonly string[], |
| 45 | ): string | undefined { |
| 46 | const explicitModel = model?.trim(); |
| 47 | if (explicitModel) return explicitModel; |
| 48 | |
| 49 | const hasAuthoritativeInventory = |
| 50 | connection.modelSource === 'fetched' && Array.isArray(connection.models); |
| 51 | const discoveredIds = |
| 52 | connection.models?.map(({ id }) => id.trim()).filter((id) => id.length > 0) ?? []; |
| 53 | const discovered = |
| 54 | hasAuthoritativeInventory || discoveredIds.length > 0 ? new Set(discoveredIds) : undefined; |
| 55 | const candidates = [ |
| 56 | ...connectionEnabledModelIds(connection), |
| 57 | ...fallbackModels, |
| 58 | ...discoveredIds, |
| 59 | ]; |
| 60 | for (const candidate of candidates) { |
| 61 | const id = candidate.trim(); |
| 62 | if (!id || (discovered && !discovered.has(id))) continue; |
| 63 | return id; |
| 64 | } |
| 65 | return undefined; |
| 66 | } |
| 67 | |
| 68 | export async function testConnection( |
| 69 | connection: LlmConnection, |
no test coverage detected