( platform: OpenPlatformDefinition, apiKey: string, fetchImpl: typeof fetch = fetch, signal?: AbortSignal, )
| 109 | } |
| 110 | |
| 111 | export async function fetchOpenPlatformModels( |
| 112 | platform: OpenPlatformDefinition, |
| 113 | apiKey: string, |
| 114 | fetchImpl: typeof fetch = fetch, |
| 115 | signal?: AbortSignal, |
| 116 | ): Promise<ManagedKimiCodeModelInfo[]> { |
| 117 | const res = await fetchImpl(`${platform.baseUrl.replace(/\/+$/, '')}/models`, { |
| 118 | headers: { |
| 119 | ...parseKimiCodeCustomHeaders(), |
| 120 | Authorization: `Bearer ${apiKey}`, |
| 121 | Accept: 'application/json', |
| 122 | }, |
| 123 | signal, |
| 124 | }); |
| 125 | if (!res.ok) { |
| 126 | throw new OpenPlatformApiError( |
| 127 | await readApiErrorMessage(res, `Failed to list models (HTTP ${res.status}).`), |
| 128 | res.status, |
| 129 | ); |
| 130 | } |
| 131 | const payload: unknown = await res.json(); |
| 132 | if (!isRecord(payload) || !Array.isArray(payload['data'])) { |
| 133 | throw new Error(`Unexpected models response for ${platform.baseUrl}.`); |
| 134 | } |
| 135 | return payload['data'] |
| 136 | .map((item) => toModelInfo(item)) |
| 137 | .filter((item): item is ManagedKimiCodeModelInfo => item !== undefined); |
| 138 | } |
| 139 | |
| 140 | export function filterModelsByPrefix( |
| 141 | models: ManagedKimiCodeModelInfo[], |
no test coverage detected