Files
hack/tools/scripts/llm-key-hunter/results/_cache/content/aipoch_open-science.json
T

1 line
6.3 KiB
JSON

{"repo": "aipoch/open-science", "files": {"src/main/settings/list-models.ts": {"ef8de2bbe868ca12d836e7c6528f044830ce4a1e": {"t": 1785746048.126275, "n": 2959, "h": "bc9f525e3b7fca63", "c": "import { ANTHROPIC_VERSION } from './validate'\n\n// Fetches a provider's live model list from its dedicated model-list URL (from the registry). Both the\n// Anthropic and OpenAI model-list shapes are `{ data: [{ id }] }`, so one parser covers the compatible\n// gateways. Network and parsing are isolated here so the branch matrix stays unit-testable with an\n// injected fetch.\n\nconst DEFAULT_LIST_TIMEOUT_MS = 15_000\n\nexport type ListModelsResult = {\n ok: boolean\n models?: string[]\n status?: number\n message?: string\n}\n\nexport type ListModelsTarget = {\n // Full model-list endpoint URL (e.g. https://api.deepseek.com/v1/models).\n url: string\n // Plaintext key; sent as both bearer and x-api-key so either auth scheme is satisfied.\n key?: string\n}\n\nexport type ListModelsDeps = {\n fetchImpl?: typeof fetch\n timeoutMs?: number\n}\n\n// Extracts non-empty string model ids from a `{ data: [{ id }] }` payload.\nexport const parseModelIds = (body: unknown): string[] => {\n if (typeof body !== 'object' || body === null) return []\n\n const data = (body as { data?: unknown }).data\n\n if (!Array.isArray(data)) return []\n\n return data\n .map((entry) =>\n typeof entry === 'object' && entry !== null ? (entry as { id?: unknown }).id : undefined\n )\n .filter((id): id is string => typeof id === 'string' && id !== '')\n}\n\n// Requests the model catalog from a vendor's model-list URL. Sends both bearer and x-api-key auth so\n// it works against gateways that expect either. Returns ok=false (never throws) so the caller can fall\n// back to the bundled catalog and surface a message.\nexport const listProviderModels = async (\n target: ListModelsTarget,\n { fetchImpl = fetch, timeoutMs = DEFAULT_LIST_TIMEOUT_MS }: ListModelsDeps = {}\n): Promise<ListModelsResult> => {\n let url: string\n\n try {\n url = new URL(target.url).toString()\n } catch {\n return { ok: false, message: 'Invalid model-list URL.' }\n }\n\n const headers: Record<string, string> = { 'anthropic-version': ANTHROPIC_VERSION }\n\n if (target.key) {\n headers.authorization = `Bearer ${target.key}`\n headers['x-api-key'] = target.key\n }\n\n const controller = new AbortController()\n const timer = setTimeout(() => controller.abort(), timeoutMs)\n\n try {\n const response = await fetchImpl(url, { method: 'GET', headers, signal: controller.signal })\n\n if (response.status < 200 || response.status >= 300) {\n return {\n ok: false,\n status: response.status,\n message: `Model list request failed (${response.status}).`\n }\n }\n\n const models = parseModelIds((await response.json()) as unknown)\n\n if (models.length === 0) {\n return { ok: false, status: response.status, message: 'The vendor returned no models.' }\n }\n\n return { ok: true, models, status: response.status }\n } catch (error) {\n return { ok: false, message: error instanceof Error ? error.message : String(error) }\n } finally {\n clearTimeout(timer)\n }\n}\n"}, "fd48da8afcafb844cfe6ed1b059e43c55a166e97": {"t": 1785759194.9486032, "n": 2959, "h": "bc9f525e3b7fca63", "c": "import { ANTHROPIC_VERSION } from './validate'\n\n// Fetches a provider's live model list from its dedicated model-list URL (from the registry). Both the\n// Anthropic and OpenAI model-list shapes are `{ data: [{ id }] }`, so one parser covers the compatible\n// gateways. Network and parsing are isolated here so the branch matrix stays unit-testable with an\n// injected fetch.\n\nconst DEFAULT_LIST_TIMEOUT_MS = 15_000\n\nexport type ListModelsResult = {\n ok: boolean\n models?: string[]\n status?: number\n message?: string\n}\n\nexport type ListModelsTarget = {\n // Full model-list endpoint URL (e.g. https://api.deepseek.com/v1/models).\n url: string\n // Plaintext key; sent as both bearer and x-api-key so either auth scheme is satisfied.\n key?: string\n}\n\nexport type ListModelsDeps = {\n fetchImpl?: typeof fetch\n timeoutMs?: number\n}\n\n// Extracts non-empty string model ids from a `{ data: [{ id }] }` payload.\nexport const parseModelIds = (body: unknown): string[] => {\n if (typeof body !== 'object' || body === null) return []\n\n const data = (body as { data?: unknown }).data\n\n if (!Array.isArray(data)) return []\n\n return data\n .map((entry) =>\n typeof entry === 'object' && entry !== null ? (entry as { id?: unknown }).id : undefined\n )\n .filter((id): id is string => typeof id === 'string' && id !== '')\n}\n\n// Requests the model catalog from a vendor's model-list URL. Sends both bearer and x-api-key auth so\n// it works against gateways that expect either. Returns ok=false (never throws) so the caller can fall\n// back to the bundled catalog and surface a message.\nexport const listProviderModels = async (\n target: ListModelsTarget,\n { fetchImpl = fetch, timeoutMs = DEFAULT_LIST_TIMEOUT_MS }: ListModelsDeps = {}\n): Promise<ListModelsResult> => {\n let url: string\n\n try {\n url = new URL(target.url).toString()\n } catch {\n return { ok: false, message: 'Invalid model-list URL.' }\n }\n\n const headers: Record<string, string> = { 'anthropic-version': ANTHROPIC_VERSION }\n\n if (target.key) {\n headers.authorization = `Bearer ${target.key}`\n headers['x-api-key'] = target.key\n }\n\n const controller = new AbortController()\n const timer = setTimeout(() => controller.abort(), timeoutMs)\n\n try {\n const response = await fetchImpl(url, { method: 'GET', headers, signal: controller.signal })\n\n if (response.status < 200 || response.status >= 300) {\n return {\n ok: false,\n status: response.status,\n message: `Model list request failed (${response.status}).`\n }\n }\n\n const models = parseModelIds((await response.json()) as unknown)\n\n if (models.length === 0) {\n return { ok: false, status: response.status, message: 'The vendor returned no models.' }\n }\n\n return { ok: true, models, status: response.status }\n } catch (error) {\n return { ok: false, message: error instanceof Error ? error.message : String(error) }\n } finally {\n clearTimeout(timer)\n }\n}\n"}}}}