Files
OmniRoute/src/lib/db/providerNodeSelect.ts
Diego Rodrigues de Sa e Souza ee24eb52d4 Release v3.8.33 (#4515)
Release v3.8.33 — full CHANGELOG in the PR body. Blocking gates green (Build, Lint, Unit Tests 8/8, Package Artifact, Quality Gates, Quality Ratchet, Docs Sync, PR Test Policy, test-vitest). Admin-merged over a Node 26 future-compat timer flake (1/4) + an E2E UI flake (3/9) — both verified non-deterministic; full test:unit validated locally (16936 pass).
2026-06-22 03:17:02 -03:00

49 lines
1.8 KiB
TypeScript

/**
* Pure provider-node selection helper (#4421).
*
* Kept dependency-free (no DB import) so it can be unit-tested without opening a
* SQLite connection.
*/
export interface SelectableNode {
id?: unknown;
[key: string]: unknown;
}
const TRAILING_UUID =
/-[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
/**
* Recover the derived TYPE of a provider node from its id. Node ids are
* "<type>-<uuid>" (e.g. "openai-compatible-responses-1715ed0f-..."), so stripping a
* trailing UUID yields the type ("openai-compatible-responses"). Ids without a UUID
* suffix are returned unchanged.
*/
export function nodeTypeFromId(id: unknown): string {
const s = String(id ?? "");
return TRAILING_UUID.test(s) ? s.replace(TRAILING_UUID, "") : s;
}
/**
* Resolve the provider node a new connection should bind to, given either:
* - the concrete node id ("<type>-<uuid>", what the dashboard sends), or
* - the bare derived type ("openai-compatible-responses", what callers using the
* /api/providers API directly often pass instead — #4421).
*
* When the exact id is not present, fall back to the SOLE node whose derived type
* equals `idOrType` — but only when exactly one such node exists, so an ambiguous type
* never silently picks the wrong node (returns null instead, preserving the 404). The
* type is matched precisely (UUID-stripped), so "openai-compatible" never matches an
* "openai-compatible-responses" node.
*/
export function selectProviderNodeForConnection<T extends SelectableNode>(
idOrType: string,
nodes: T[]
): T | null {
if (!idOrType) return null;
const exact = nodes.find((n) => n.id === idOrType);
if (exact) return exact;
const ofType = nodes.filter((n) => typeof n.id === "string" && nodeTypeFromId(n.id) === idOrType);
return ofType.length === 1 ? ofType[0] : null;
}