Repository navigation
Expand file tree
/
Copy pathopencode-autodiscovery.js
More file actions
237 lines (220 loc) · 8.03 KB
/
Copy pathopencode-autodiscovery.js
File metadata and controls
237 lines (220 loc) · 8.03 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
* Author: HarutoHiroki
* License: MIT
* Comment: some script i whipped up cuz opencode doesnt do auto discovery properly
*/
export default async () => {
const capitalize = (s) => s[0].toUpperCase() + s.slice(1).toLowerCase();
// Special formatting stuff, most models are first letter capitalized otherwise
const KNOWN_PREFIXES = {
wordslop: "WordSlop",
thinkingcap: "ThinkingCap",
};
const KNOWN_MODELS = {
glm: "GLM",
minimax: "MiniMax",
};
const KNOWN_TAGS = {
uncensored: "Uncensored",
flash: "Flash",
single: "Single",
instruct: "Instruct",
code: "Code",
moe: "MoE"
};
const beautifyModelName = (id) => {
const parts = id.split("-");
const segments = [];
let i = 0;
while (i < parts.length) {
const seg = parts[i].toLowerCase();
if (seg in KNOWN_PREFIXES) {
segments.push(KNOWN_PREFIXES[seg]);
i++;
} else if (seg in KNOWN_MODELS) {
segments.push(KNOWN_MODELS[seg]);
i++;
} else if (/^\d+$/.test(seg)) {
let version = seg;
while (i + 1 < parts.length && /^\d+$/.test(parts[i + 1])) {
i++;
version += "." + parts[i];
}
segments.push(version);
i++;
} else if (/^\d+[bkmg]$/i.test(seg)) {
segments.push(seg.replace(/\d+[bkmg]$/i, (m) => m.toUpperCase()));
i++;
} else if (/^a(\d+)([bmt]?)$/i.test(seg)) {
const match = seg.match(/^a(\d+)([bmt]?)$/i);
const num = match[1];
const suffix = match[2] ? match[2].toUpperCase() : "";
segments.push(`A${num}${suffix}`);
i++;
} else if (seg in KNOWN_TAGS) {
segments.push(KNOWN_TAGS[seg]);
i++;
} else if (/^[a-z]\d+$/i.test(seg)) {
if (i + 1 < parts.length && /^\d+$/.test(parts[i + 1])) {
segments.push(seg.toUpperCase() + "." + parts[i + 1]);
i += 2;
} else {
segments.push(seg.toUpperCase());
i++;
}
} else {
segments.push(
seg.length === 1 ? seg.toUpperCase() : capitalize(seg),
);
i++;
}
}
return segments.join(" ");
};
const positive = (value) => {
const parsed = typeof value === "string" ? Number(value) : value;
return typeof parsed === "number" && Number.isFinite(parsed) && parsed > 0 ? parsed : undefined;
};
const contextLimitOf = (m) => {
return positive(m.context_length) ?? positive(m.max_model_len) ?? positive(m.meta?.n_ctx);
};
const outputLimitOf = (m) => {
return positive(m.max_completion_tokens)
?? positive(m.top_provider?.max_completion_tokens)
?? positive(m.max_output_tokens);
};
const defaultOutputLimit = (contextLimit) => {
return Math.min(Math.floor(contextLimit / 4), 32000);
};
const reasoningOf = (m) => {
const raw = m.reasoning_effort;
if (!raw || !Array.isArray(raw.levels)) return undefined;
const levels = raw.levels.filter((l) => typeof l === "string" && l !== "");
if (levels.length === 0) return undefined;
return {
levels,
disable: typeof raw.disable === "string" ? raw.disable : undefined,
default: typeof raw.default === "string" && levels.includes(raw.default) ? raw.default : undefined,
};
};
// The router's disable keyword -> how thinking-off is encoded on the wire:
// "none" -> reasoning_effort "none"
// "lowest" -> the lowest available level
// "qwen" -> chat_template_kwargs.enable_thinking false
const disableVariantOf = (reasoning) => {
switch (reasoning.disable) {
case "none":
return { reasoningEffort: "none" };
case "lowest":
return { reasoningEffort: reasoning.levels[0] };
case "qwen":
return { chat_template_kwargs: { enable_thinking: false } };
default:
return undefined;
}
};
const discover = async (id, cfg) => {
let baseURL = cfg.options?.baseURL;
if (!baseURL) return;
baseURL = baseURL.replace(/\/+$/, "");
if (!baseURL.endsWith("/v1")) baseURL += "/v1";
const apiKey = cfg.options?.apiKey;
const headers = {};
if (apiKey && typeof apiKey === "string" && apiKey !== "") {
headers["Authorization"] = `Bearer ${apiKey}`;
}
const headerArg = Object.keys(headers).length > 0 ? headers : undefined;
const maxRetries = 5;
const timeoutMs = 5000;
const url = baseURL + "/models";
let lastError;
for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), timeoutMs);
const res = await fetch(url, {
headers: headerArg,
signal: controller.signal,
});
clearTimeout(timer);
const data = await res.json();
const existingModels = cfg.models || {};
const models = { ...existingModels };
for (const m of data.data || []) {
// Respect existing configured models
if (existingModels[m.id]) continue;
// Skip embedding models
if (m.id.toLowerCase().includes("embedding") || m.id.toLowerCase().includes("embed")) continue;
const contextLimit = contextLimitOf(m);
const outputLimit = outputLimitOf(m) ?? (contextLimit ? defaultOutputLimit(contextLimit) : undefined);
const inputModalities = (m.architecture?.input_modalities ?? m.modalities ?? ["text"]).map((s) => s.toLowerCase());
const outputModalities = (m.architecture?.output_modalities ?? ["text"]).map((s) => s.toLowerCase());
const params = Array.isArray(m.supported_parameters) ? m.supported_parameters : undefined;
const reasoning = reasoningOf(m);
const entry = {
name: beautifyModelName(m.id),
tool_call: params ? params.includes("tools") : true,
modalities: { input: inputModalities, output: outputModalities },
};
if (contextLimit && outputLimit) {
entry.limit = { context: contextLimit, output: outputLimit };
}
if (reasoning) {
entry.reasoning = true;
entry.variants = {};
for (const level of reasoning.levels) {
entry.variants[level] = { reasoningEffort: level };
}
if (reasoning.default) {
entry.options = { reasoningEffort: reasoning.default };
}
// Thinking-off variant per the router's disable keyword
const off = disableVariantOf(reasoning);
if (off) entry.variants.none = off;
}
const pricing = m.pricing;
const toNum = (v) => typeof v === "string" ? Number(v) : v;
const promptPrice = toNum(pricing?.prompt);
const completionPrice = toNum(pricing?.completion);
const cacheReadPrice = toNum(pricing?.input_cache_read);
const cacheWritePrice = toNum(pricing?.cache_write);
if (promptPrice || completionPrice) {
entry.cost = {
input: (promptPrice ?? 0) * 1000000,
output: (completionPrice ?? 0) * 1000000,
cache_read: (cacheReadPrice ?? 0) * 1000000,
cache_write: (cacheWritePrice ?? 0) * 1000000,
};
}
models[m.id] = entry;
}
if (Object.keys(models).length > 0) {
cfg.models = models;
if (cfg.options) cfg.options.baseURL = baseURL;
}
return;
} catch (e) {
lastError = e;
if (attempt < maxRetries) {
console.warn(
`openai-compatible-autodiscover: discovery attempt ${attempt}/${maxRetries} failed for "${id}": ${e.message}`
);
}
}
}
console.warn(
`openai-compatible-autodiscover: failed to discover models for "${id}" after ${maxRetries} attempts:`,
lastError?.message
);
};
return {
config: async (cfg) => {
const providers = cfg.provider || {};
for (const [id, p] of Object.entries(providers)) {
if (p.npm === "@ai-sdk/openai-compatible") {
await discover(id, p);
}
}
},
};
};