forked from ericc-ch/copilot-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalias.ts
More file actions
34 lines (31 loc) · 1.35 KB
/
alias.ts
File metadata and controls
34 lines (31 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { getConfig } from "./config-store"
type ModelMap = ReturnType<typeof getConfig>["models"]
/**
* Resolve a client-facing alias to the upstream model name.
* If no alias is configured for `input`, returns `input` unchanged (pass-through).
*
* Pass an explicit `models` snapshot to share one getConfig() call across
* ingress + egress rewrites within the same request (avoids inconsistency
* during hot-reloads and pays the structuredClone cost only once).
*/
export function resolveAlias(input: string, models?: ModelMap): string {
// Object.hasOwn guards against prototype-chain properties (e.g. "__proto__", "constructor")
const map = models ?? getConfig().models
if (!input || !Object.hasOwn(map, input)) return input
return map[input].upstream
}
/**
* Rewrite an upstream model name back to the client-facing alias.
* If no alias maps to `upstream`, returns `upstream` unchanged.
*
* Linear scan — acceptable for small alias counts (< ~100 entries).
* Prefer storing the original client alias from ingress and returning it
* directly on egress to avoid this scan and multi-alias ambiguity.
*/
export function resolveUpstream(upstream: string, models?: ModelMap): string {
const map = models ?? getConfig().models
for (const [alias, entry] of Object.entries(map)) {
if (entry.upstream === upstream) return alias
}
return upstream
}