forked from ericc-ch/copilot-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.ts
More file actions
147 lines (117 loc) · 3.47 KB
/
Copy pathmodels.ts
File metadata and controls
147 lines (117 loc) · 3.47 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import type { Model } from "~/services/copilot/get-models"
import { state } from "./state"
const normalizeModelId = (modelId: string): string =>
modelId
.trim()
.toLowerCase()
.replaceAll(/[\s._-]+/g, "")
const stripSnapshotSuffix = (modelId: string): string => {
if (modelId.startsWith("claude-sonnet-4-")) {
return "claude-sonnet-4"
}
if (/^claude-opus-4-7-\d{8}$/.test(modelId)) {
return "claude-opus-4.7"
}
if (modelId.startsWith("claude-opus-4-")) {
return "claude-opus-4"
}
return modelId
}
const getAliasCandidates = (modelId: string): Array<string> => {
const canonicalModelId = stripSnapshotSuffix(modelId.trim().toLowerCase())
const aliases = new Set<string>([canonicalModelId])
const familyMatch = canonicalModelId.match(
/^[a-z]+(?:-[a-z]+)*-\d+(?:\.\d+)?/,
)
if (familyMatch) {
aliases.add(familyMatch[0])
aliases.add(familyMatch[0].replace(/\.\d+$/, ""))
}
if (/^gpt-5(?:[.-]\d+)?$/i.test(canonicalModelId)) {
aliases.add("gpt-5")
}
return [...aliases]
}
const scoreModelCandidate = (model: Model): number => {
let score = 0
if (model.model_picker_enabled) {
score += 20
}
if (!model.preview) {
score += 10
}
if (/mini|nano|fast|flash|haiku/i.test(model.id)) {
score -= 15
}
return score - model.id.length / 1000
}
const pickBestModel = (models: Array<Model>): Model | undefined => {
return [...models].sort(
(left, right) => scoreModelCandidate(right) - scoreModelCandidate(left),
)[0]
}
const getBestPrefixMatches = (
models: Array<Model>,
aliasCandidates: Array<string>,
): Array<Model> => {
let bestMatchLength = 0
const matches: Array<Model> = []
for (const model of models) {
const normalizedModelId = normalizeModelId(model.id)
const matchedAliasLength = Math.max(
0,
...aliasCandidates.map((candidate) => {
const normalizedCandidate = normalizeModelId(candidate)
return (
normalizedCandidate.length >= 4
&& normalizedModelId.startsWith(normalizedCandidate)
) ?
normalizedCandidate.length
: 0
}),
)
if (matchedAliasLength === 0) {
continue
}
if (matchedAliasLength > bestMatchLength) {
bestMatchLength = matchedAliasLength
matches.length = 0
matches.push(model)
continue
}
if (matchedAliasLength === bestMatchLength) {
matches.push(model)
}
}
return matches
}
export const resolveModel = (
requestedModelId: string,
models: Array<Model> | undefined = state.models?.data,
): Model | undefined => {
if (!models || models.length === 0) {
return undefined
}
const exactMatch = models.find((model) => model.id === requestedModelId)
if (exactMatch) {
return exactMatch
}
const aliasCandidates = getAliasCandidates(requestedModelId)
const normalizedAliases = new Set(
aliasCandidates.map((candidate) => normalizeModelId(candidate)),
)
const normalizedExactMatches = models.filter((model) =>
normalizedAliases.has(normalizeModelId(model.id)),
)
if (normalizedExactMatches.length > 0) {
return pickBestModel(normalizedExactMatches)
}
const familyPrefixMatches = getBestPrefixMatches(models, aliasCandidates)
return pickBestModel(familyPrefixMatches)
}
export const resolveModelId = (
requestedModelId: string,
models: Array<Model> | undefined = state.models?.data,
): string =>
resolveModel(requestedModelId, models)?.id
?? stripSnapshotSuffix(requestedModelId)