agentsw 0.9.1 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +42 -1
- package/README.zh-CN.md +38 -1
- package/dist/app-paths.js +127 -0
- package/dist/apps.js +11 -15
- package/dist/commands.js +404 -212
- package/dist/discover.js +21 -11
- package/dist/gateway.js +5 -4
- package/dist/i18n.js +160 -3
- package/dist/index.js +14 -2
- package/dist/menu.js +141 -121
- package/dist/metadata.js +69 -20
- package/dist/modelsdev.js +51 -21
- package/dist/progress.js +116 -0
- package/dist/provider-actions.js +29 -9
- package/dist/remove.js +15 -30
- package/dist/rename.js +17 -32
- package/dist/report.js +67 -0
- package/dist/store.js +25 -0
- package/dist/targets/claudecode.js +12 -9
- package/dist/targets/codex.js +22 -13
- package/dist/targets/dsh.js +19 -34
- package/dist/targets/hermes.js +11 -20
- package/dist/targets/omp.js +15 -7
- package/dist/targets/opencode.js +42 -25
- package/dist/targets/pistyle.js +9 -20
- package/dist/targets/workbuddy.js +6 -4
- package/dist/ui.js +157 -0
- package/package.json +2 -2
package/dist/menu.js
CHANGED
|
@@ -9,8 +9,9 @@ import { cmdRemoveProvider, cmdRename } from "./provider-actions.js";
|
|
|
9
9
|
import { listRemovableProviders } from "./remove.js";
|
|
10
10
|
import { providerIdFromBaseUrl } from "./slug.js";
|
|
11
11
|
import { targets } from "./targets/index.js";
|
|
12
|
+
import { ActionCancelled, out, setInMenu } from "./ui.js";
|
|
12
13
|
function bye() {
|
|
13
|
-
|
|
14
|
+
out(pc.dim(`\n${t("menu.bye")}`));
|
|
14
15
|
process.exit(0);
|
|
15
16
|
}
|
|
16
17
|
const cancel = { onCancel: () => bye() };
|
|
@@ -34,14 +35,14 @@ async function chooseLanguage() {
|
|
|
34
35
|
const store = loadStore();
|
|
35
36
|
store.language = language;
|
|
36
37
|
saveStore(store);
|
|
37
|
-
|
|
38
|
+
out(pc.green(t("language.saved")));
|
|
38
39
|
}
|
|
39
40
|
/** select an existing provider; prints a hint and returns undefined when none configured */
|
|
40
41
|
async function pickProvider(message) {
|
|
41
42
|
const store = loadStore();
|
|
42
43
|
const ids = Object.keys(store.providers);
|
|
43
44
|
if (ids.length === 0) {
|
|
44
|
-
|
|
45
|
+
out(pc.yellow(t("menu.noProvidersHint")));
|
|
45
46
|
return undefined;
|
|
46
47
|
}
|
|
47
48
|
const { id } = await prompts({
|
|
@@ -49,15 +50,18 @@ async function pickProvider(message) {
|
|
|
49
50
|
name: "id",
|
|
50
51
|
message,
|
|
51
52
|
hint: t("menu.selectInstructions"),
|
|
52
|
-
choices:
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
53
|
+
choices: [
|
|
54
|
+
{ title: t("menu.back"), value: "__back__" },
|
|
55
|
+
...ids.map((pid) => {
|
|
56
|
+
const p = store.providers[pid];
|
|
57
|
+
return {
|
|
58
|
+
title: `${pid} · ${p.protocol} · ${t("menu.defaultModel")} ${p.defaultModel}${store.active === pid ? ` (${t("menu.active")})` : ""}`,
|
|
59
|
+
value: pid,
|
|
60
|
+
};
|
|
61
|
+
}),
|
|
62
|
+
],
|
|
59
63
|
}, cancel);
|
|
60
|
-
if (!id)
|
|
64
|
+
if (!id || id === "__back__")
|
|
61
65
|
return undefined;
|
|
62
66
|
const p = store.providers[id];
|
|
63
67
|
return { id, defaultModel: p.defaultModel, models: p.models.map((m) => m.id) };
|
|
@@ -151,7 +155,7 @@ async function removeFromMenu() {
|
|
|
151
155
|
? listRemovableProviders(app)
|
|
152
156
|
: Object.values(loadStore().providers).map((provider) => ({ id: provider.id, name: provider.name }));
|
|
153
157
|
if (!entries.length) {
|
|
154
|
-
|
|
158
|
+
out(pc.dim(t("menu.noRemovable")));
|
|
155
159
|
return;
|
|
156
160
|
}
|
|
157
161
|
const { selected } = await prompts({
|
|
@@ -159,10 +163,13 @@ async function removeFromMenu() {
|
|
|
159
163
|
name: "selected",
|
|
160
164
|
message: t("menu.removeProvider"),
|
|
161
165
|
hint: t("menu.selectInstructions"),
|
|
162
|
-
choices:
|
|
163
|
-
title:
|
|
164
|
-
|
|
165
|
-
|
|
166
|
+
choices: [
|
|
167
|
+
{ title: t("menu.back"), value: "__back__" },
|
|
168
|
+
...entries.map((entry, index) => ({
|
|
169
|
+
title: `${entry.id} · ${entry.app ?? "agentsw"}${entry.name && entry.name !== entry.id ? ` · ${entry.name}` : ""}`,
|
|
170
|
+
value: index,
|
|
171
|
+
})),
|
|
172
|
+
],
|
|
166
173
|
}, cancel);
|
|
167
174
|
const picked = entries[selected];
|
|
168
175
|
if (!picked)
|
|
@@ -179,16 +186,19 @@ async function removeFromMenu() {
|
|
|
179
186
|
}
|
|
180
187
|
}
|
|
181
188
|
export async function cmdMenu() {
|
|
182
|
-
|
|
189
|
+
let store = loadStore();
|
|
190
|
+
if (!store.language) {
|
|
183
191
|
await chooseLanguage();
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
192
|
+
store = loadStore(); // chooseLanguage persists the preference, so re-read before trusting the rest
|
|
193
|
+
}
|
|
194
|
+
out(pc.bold("agentsw") + pc.dim(t("menu.title")));
|
|
195
|
+
if (Object.keys(store.providers).length === 0) {
|
|
196
|
+
out(pc.yellow(t("menu.noProviders")));
|
|
187
197
|
if (await askToggle(t("menu.firstScan"), true))
|
|
188
198
|
await cmdImport({});
|
|
189
199
|
}
|
|
190
200
|
for (;;) {
|
|
191
|
-
|
|
201
|
+
out("");
|
|
192
202
|
const { action } = await prompts({
|
|
193
203
|
type: "select",
|
|
194
204
|
name: "action",
|
|
@@ -214,114 +224,124 @@ export async function cmdMenu() {
|
|
|
214
224
|
}, cancel);
|
|
215
225
|
if (action === undefined || action === "quit")
|
|
216
226
|
return;
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
227
|
+
// One guard for every action: a cancelled prompt unwinds to here, and any other failure reports itself
|
|
228
|
+
// and returns to the menu instead of ending the session.
|
|
229
|
+
setInMenu(true);
|
|
230
|
+
try {
|
|
231
|
+
if (action === "quickAdd") {
|
|
232
|
+
await cmdQuickAdd({});
|
|
233
|
+
}
|
|
234
|
+
else if (action === "add") {
|
|
235
|
+
const { src } = await prompts({
|
|
236
|
+
type: "select",
|
|
237
|
+
name: "src",
|
|
238
|
+
message: t("menu.modelSource"),
|
|
239
|
+
hint: t("menu.selectInstructions"),
|
|
240
|
+
choices: [
|
|
241
|
+
{ title: t("menu.modelDiscover"), value: "discover" },
|
|
242
|
+
{ title: t("menu.modelManual"), value: "manual" },
|
|
243
|
+
],
|
|
244
|
+
}, cancel);
|
|
245
|
+
await cmdAdd({ discover: src === "discover" });
|
|
246
|
+
}
|
|
247
|
+
else if (action === "import") {
|
|
248
|
+
await cmdImport({});
|
|
249
|
+
}
|
|
250
|
+
else if (action === "use") {
|
|
251
|
+
const picked = await pickProvider(t("menu.pickProvider"));
|
|
252
|
+
if (!picked)
|
|
253
|
+
continue;
|
|
254
|
+
const apps = await pickApps(t("menu.pickApps"));
|
|
255
|
+
const { model } = await prompts({
|
|
256
|
+
type: picked.models.length > 1 ? "select" : null,
|
|
257
|
+
name: "model",
|
|
258
|
+
message: t("menu.defaultModel"),
|
|
259
|
+
hint: t("menu.selectInstructions"),
|
|
260
|
+
choices: [
|
|
261
|
+
{ title: t("menu.keepDefault", { model: picked.defaultModel }), value: "" },
|
|
262
|
+
...picked.models.filter((m) => m !== picked.defaultModel).map((m) => ({ title: m, value: m })),
|
|
263
|
+
],
|
|
264
|
+
}, cancel);
|
|
265
|
+
await cmdUse(picked.id, { model: model || undefined, apps });
|
|
266
|
+
}
|
|
267
|
+
else if (action === "status") {
|
|
268
|
+
cmdStatus();
|
|
269
|
+
}
|
|
270
|
+
else if (action === "list") {
|
|
271
|
+
cmdList();
|
|
272
|
+
}
|
|
273
|
+
else if (action === "sync") {
|
|
274
|
+
await cmdSync({ apps: await pickApps(t("menu.pickApps")) });
|
|
275
|
+
}
|
|
276
|
+
else if (action === "discover") {
|
|
277
|
+
const picked = await pickProvider(t("menu.discoverFor"));
|
|
278
|
+
if (!picked)
|
|
279
|
+
continue;
|
|
280
|
+
const sync = await askToggle(t("menu.pushRefresh"));
|
|
281
|
+
await cmdDiscover(picked.id, { sync, apps: sync ? await pickApps(t("menu.pickApps")) : undefined });
|
|
282
|
+
}
|
|
283
|
+
else if (action === "metadata") {
|
|
284
|
+
const picked = await pickProvider(t("menu.metadataProvider"));
|
|
285
|
+
if (!picked)
|
|
286
|
+
continue;
|
|
287
|
+
const current = loadStore().providers[picked.id];
|
|
288
|
+
const { metadataMode } = await prompts({
|
|
289
|
+
type: "select",
|
|
290
|
+
name: "metadataMode",
|
|
291
|
+
message: t("menu.metadataMode"),
|
|
292
|
+
hint: t("menu.selectInstructions"),
|
|
293
|
+
initial: ["auto", "on", "off"].indexOf(getMetadataMode(current)),
|
|
294
|
+
choices: [
|
|
295
|
+
{ title: t("menu.metadataAuto"), description: t("menu.metadataAutoHelp"), value: "auto" },
|
|
296
|
+
{ title: t("menu.metadataOn"), description: t("menu.metadataOnHelp"), value: "on" },
|
|
297
|
+
{ title: t("menu.metadataOff"), description: t("menu.metadataOffHelp"), value: "off" },
|
|
298
|
+
],
|
|
299
|
+
}, cancel);
|
|
300
|
+
await cmdRefreshMeta({ provider: picked.id, metadataMode });
|
|
301
|
+
}
|
|
302
|
+
else if (action === "rename" || action === "remove") {
|
|
290
303
|
if (action === "rename")
|
|
291
304
|
await renameFromMenu();
|
|
292
305
|
else
|
|
293
306
|
await removeFromMenu();
|
|
294
307
|
}
|
|
295
|
-
|
|
296
|
-
|
|
308
|
+
else if (action === "apps") {
|
|
309
|
+
await cmdApps();
|
|
310
|
+
if (await askToggle(t("menu.upgrade")))
|
|
311
|
+
await cmdUpgrade([]);
|
|
297
312
|
}
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
313
|
+
else if (action === "install") {
|
|
314
|
+
const installable = appPackages
|
|
315
|
+
.filter((a) => !installedVersion(a) && appCommand(a, "install"))
|
|
316
|
+
.map((a) => ({ title: `${a.id} · ${a.name}`, value: a.id }));
|
|
317
|
+
if (installable.length === 0) {
|
|
318
|
+
out(pc.dim(t("menu.allInstalled")));
|
|
319
|
+
continue;
|
|
320
|
+
}
|
|
321
|
+
const { appId } = await prompts({
|
|
322
|
+
type: "select",
|
|
323
|
+
name: "appId",
|
|
324
|
+
message: t("menu.pickApp"),
|
|
325
|
+
hint: t("menu.selectInstructions"),
|
|
326
|
+
choices: installable,
|
|
327
|
+
}, cancel);
|
|
328
|
+
if (!appId)
|
|
329
|
+
continue;
|
|
330
|
+
await cmdInstall(appId);
|
|
311
331
|
}
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
332
|
+
else if (action === "language") {
|
|
333
|
+
await chooseLanguage();
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
catch (error) {
|
|
337
|
+
// Ctrl-C inside a sub-prompt means "abandon this action", not "quit the app".
|
|
338
|
+
if (error instanceof ActionCancelled)
|
|
339
|
+
out(pc.dim(t("menu.cancelledAction")));
|
|
340
|
+
else
|
|
341
|
+
console.error(pc.red(error instanceof Error ? error.message : String(error)));
|
|
322
342
|
}
|
|
323
|
-
|
|
324
|
-
|
|
343
|
+
finally {
|
|
344
|
+
setInMenu(false);
|
|
325
345
|
}
|
|
326
346
|
}
|
|
327
347
|
}
|
package/dist/metadata.js
CHANGED
|
@@ -105,25 +105,42 @@ function equalValue(a, b) {
|
|
|
105
105
|
function indexCatalog(catalog) {
|
|
106
106
|
const exact = new Map();
|
|
107
107
|
const canonical = new Map();
|
|
108
|
+
const identities = new Map();
|
|
109
|
+
const push = (map, key, entry) => {
|
|
110
|
+
const matches = map.get(key);
|
|
111
|
+
if (matches)
|
|
112
|
+
matches.push(entry);
|
|
113
|
+
else
|
|
114
|
+
map.set(key, [entry]);
|
|
115
|
+
};
|
|
108
116
|
for (const provider of Object.values(catalog)) {
|
|
109
117
|
for (const [key, model] of Object.entries(provider.models)) {
|
|
110
118
|
const modelId = model.id.includes("/") ? model.id : `${provider.id}/${model.id}`;
|
|
111
|
-
const entry = { provider: provider.id, modelId, model };
|
|
112
|
-
for (const id of new Set([key, model.id]))
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
else
|
|
117
|
-
exact.set(id, [entry]);
|
|
118
|
-
}
|
|
119
|
-
const matches = canonical.get(modelId);
|
|
120
|
-
if (matches)
|
|
121
|
-
matches.push(entry);
|
|
122
|
-
else
|
|
123
|
-
canonical.set(modelId, [entry]);
|
|
119
|
+
const entry = { provider: provider.id, modelId, identity: model.canonical_model_id ?? modelId, model };
|
|
120
|
+
for (const id of new Set([key, model.id]))
|
|
121
|
+
push(exact, id, entry);
|
|
122
|
+
push(canonical, modelId, entry);
|
|
123
|
+
push(identities, entry.identity, entry);
|
|
124
124
|
}
|
|
125
125
|
}
|
|
126
|
-
return { exact, canonical };
|
|
126
|
+
return { exact, canonical, identities };
|
|
127
|
+
}
|
|
128
|
+
/** Creator identity of a catalog model id, or undefined when its rows disagree. */
|
|
129
|
+
function catalogIdentity(index, modelId) {
|
|
130
|
+
const identities = new Set(index?.canonical.get(modelId)?.map((entry) => entry.identity));
|
|
131
|
+
return identities?.size === 1 ? [...identities][0] : undefined;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* The creator's own listing for an identity, when models.dev carries one.
|
|
135
|
+
* Gateways also key their rows by the canonical id, and those rows carry the
|
|
136
|
+
* gateway's own limits and prices, so only a row from the creator's own
|
|
137
|
+
* namespace counts as authoritative.
|
|
138
|
+
*/
|
|
139
|
+
function creatorEntry(index, identity) {
|
|
140
|
+
const slash = identity.indexOf("/");
|
|
141
|
+
if (slash < 1)
|
|
142
|
+
return undefined;
|
|
143
|
+
return index?.identities.get(identity)?.find((entry) => entry.modelId === identity && entry.provider === identity.slice(0, slash));
|
|
127
144
|
}
|
|
128
145
|
function indexGatewayBareIds(gateway) {
|
|
129
146
|
const result = new Map();
|
|
@@ -147,10 +164,15 @@ function selectExact(index, id, hint, aliases, gateway, gatewayBareIds) {
|
|
|
147
164
|
const candidate = gatewayBareIds?.get(id);
|
|
148
165
|
const evidence = index?.exact.get(id);
|
|
149
166
|
const hintedEvidence = hint ? evidence?.filter((entry) => entry.provider === hint) : undefined;
|
|
150
|
-
const identities = new Set((hintedEvidence?.length ? hintedEvidence : evidence)?.map((entry) => entry.
|
|
167
|
+
const identities = new Set((hintedEvidence?.length ? hintedEvidence : evidence)?.map((entry) => entry.identity));
|
|
168
|
+
// Gateway names the creator while models.dev names the listing provider, so
|
|
169
|
+
// the two agree only once both are reduced to a creator identity. A gateway
|
|
170
|
+
// id the catalog has no row for stands for itself.
|
|
171
|
+
const candidateIdentity = candidate ? catalogIdentity(index, candidate) ?? candidate : undefined;
|
|
151
172
|
// A unique exact basename is safe only with absent or agreeing primary evidence.
|
|
152
173
|
// Explicit provider hints can resolve primary ambiguity, but never Gateway ambiguity.
|
|
153
|
-
if (candidate &&
|
|
174
|
+
if (candidate && candidateIdentity !== undefined
|
|
175
|
+
&& (identities.size === 0 || (identities.size === 1 && identities.has(candidateIdentity))))
|
|
154
176
|
gatewayId = candidate;
|
|
155
177
|
}
|
|
156
178
|
}
|
|
@@ -159,10 +181,10 @@ function selectExact(index, id, hint, aliases, gateway, gatewayBareIds) {
|
|
|
159
181
|
: (bare && hint ? index?.canonical.get(`${hint}/${id}`) : undefined)
|
|
160
182
|
?? index?.canonical.get(id) ?? index?.exact.get(id);
|
|
161
183
|
const hinted = candidates?.find((entry) => entry.provider === hint);
|
|
162
|
-
const identities = new Set(candidates?.map((entry) => entry.
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
184
|
+
const identities = new Set(candidates?.map((entry) => entry.identity));
|
|
185
|
+
// Reseller rows for one model share an identity but disagree on limits and
|
|
186
|
+
// prices, so only the creator's own row is authoritative.
|
|
187
|
+
const primary = hinted ?? (identities.size === 1 ? creatorEntry(index, [...identities][0]) : undefined);
|
|
166
188
|
return { primary, gatewayId, alias };
|
|
167
189
|
}
|
|
168
190
|
function legacyPrimary(catalog, id, hint) {
|
|
@@ -398,3 +420,30 @@ export async function enrichProviderModels(catalog, ids, settings, options = {})
|
|
|
398
420
|
// bounds must be resolved with the supplement against the original values.
|
|
399
421
|
return gateway ? enrich(gateway) : primaryOnly ?? enrich();
|
|
400
422
|
}
|
|
423
|
+
/**
|
|
424
|
+
* Why a model id stayed bare after enrichment, so callers can tell the user
|
|
425
|
+
* what is worth acting on: "ambiguous" ids are listed under more than one
|
|
426
|
+
* creator and an explicit mapping resolves them; "unknown" ids no creator
|
|
427
|
+
* lists at all, so no mapping helps; "noCreatorRow" ids have one unambiguous
|
|
428
|
+
* creator but models.dev carries no authoritative row for it, so only Gateway
|
|
429
|
+
* can fill them.
|
|
430
|
+
*/
|
|
431
|
+
export function classifyUnresolved(catalog, ids, hint) {
|
|
432
|
+
const ambiguous = [];
|
|
433
|
+
const unknown = [];
|
|
434
|
+
const noCreatorRow = [];
|
|
435
|
+
if (!catalog)
|
|
436
|
+
return { ambiguous, unknown: [...ids], noCreatorRow };
|
|
437
|
+
const index = indexCatalog(catalog);
|
|
438
|
+
for (const id of ids) {
|
|
439
|
+
const hinted = hint ? index.canonical.get(`${hint}/${id}`) : undefined;
|
|
440
|
+
const candidates = hinted ?? index.canonical.get(id) ?? index.exact.get(id);
|
|
441
|
+
if (!candidates?.length)
|
|
442
|
+
unknown.push(id);
|
|
443
|
+
else if (new Set(candidates.map((entry) => entry.identity)).size > 1)
|
|
444
|
+
ambiguous.push(id);
|
|
445
|
+
else if (!creatorEntry(index, [...new Set(candidates.map((entry) => entry.identity))][0]))
|
|
446
|
+
noCreatorRow.push(id);
|
|
447
|
+
}
|
|
448
|
+
return { ambiguous, unknown, noCreatorRow };
|
|
449
|
+
}
|
package/dist/modelsdev.js
CHANGED
|
@@ -2,6 +2,7 @@ import path from "node:path";
|
|
|
2
2
|
import fs from "node:fs";
|
|
3
3
|
import { configDir } from "./store.js";
|
|
4
4
|
import { readJsonIfExists, writeFileAtomic } from "./fsutil.js";
|
|
5
|
+
import { warn } from "./ui.js";
|
|
5
6
|
const API_URL = "https://models.dev/api.json";
|
|
6
7
|
const CACHE_FILE = path.join(configDir, "models-dev.json");
|
|
7
8
|
const CACHE_TTL_MS = 24 * 60 * 60 * 1000;
|
|
@@ -10,29 +11,56 @@ const catalogFetchedAt = new WeakMap();
|
|
|
10
11
|
export function getCatalogFetchedAt(catalog) {
|
|
11
12
|
return catalogFetchedAt.get(catalog);
|
|
12
13
|
}
|
|
14
|
+
/**
|
|
15
|
+
* models.dev maps provider ids to providers, each carrying a model dictionary.
|
|
16
|
+
* A 200 response that is not shaped like a catalog (an error document from a proxy
|
|
17
|
+
* or mirror, say) must never be cached: later runs would then re-read the broken
|
|
18
|
+
* file on every load, long after the network recovered.
|
|
19
|
+
*/
|
|
20
|
+
function isCatalog(value) {
|
|
21
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
22
|
+
return false;
|
|
23
|
+
return Object.values(value).every((provider) => {
|
|
24
|
+
if (!provider || typeof provider !== "object" || Array.isArray(provider))
|
|
25
|
+
return false;
|
|
26
|
+
const models = provider.models;
|
|
27
|
+
return !!models && typeof models === "object" && !Array.isArray(models);
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
/** A cache file that is missing, unreadable or malformed is simply not a catalog. */
|
|
13
31
|
function readCachedCatalog() {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
32
|
+
let parsed;
|
|
33
|
+
try {
|
|
34
|
+
parsed = readJsonIfExists(CACHE_FILE);
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
if (!isCatalog(parsed))
|
|
40
|
+
return undefined;
|
|
41
|
+
const catalog = parsed;
|
|
42
|
+
try {
|
|
43
|
+
catalogFetchedAt.set(catalog, fs.statSync(CACHE_FILE).mtime.toISOString());
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
// A concurrently removed cache still supplies useful metadata without a fetch time.
|
|
22
47
|
}
|
|
23
48
|
return catalog;
|
|
24
49
|
}
|
|
50
|
+
function cacheIsFresh() {
|
|
51
|
+
try {
|
|
52
|
+
return Date.now() - fs.statSync(CACHE_FILE).mtimeMs < CACHE_TTL_MS;
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
25
58
|
export async function loadCatalog(opts = {}) {
|
|
26
59
|
if (!opts.refresh) {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
return readCachedCatalog();
|
|
32
|
-
}
|
|
33
|
-
catch {
|
|
34
|
-
/* no cache */
|
|
35
|
-
}
|
|
60
|
+
const cached = readCachedCatalog();
|
|
61
|
+
// A malformed cache falls through to a fetch rather than failing the command.
|
|
62
|
+
if (cached && (opts.offline || cacheIsFresh()))
|
|
63
|
+
return cached;
|
|
36
64
|
}
|
|
37
65
|
if (opts.offline)
|
|
38
66
|
return readCachedCatalog();
|
|
@@ -41,17 +69,19 @@ export async function loadCatalog(opts = {}) {
|
|
|
41
69
|
if (!res.ok)
|
|
42
70
|
throw new Error(`models.dev responded ${res.status}`);
|
|
43
71
|
const text = await res.text();
|
|
44
|
-
const
|
|
72
|
+
const parsed = JSON.parse(text);
|
|
73
|
+
if (!isCatalog(parsed))
|
|
74
|
+
throw new Error("models.dev returned an unexpected catalog shape");
|
|
45
75
|
writeFileAtomic(CACHE_FILE, text);
|
|
46
|
-
catalogFetchedAt.set(
|
|
47
|
-
return
|
|
76
|
+
catalogFetchedAt.set(parsed, new Date().toISOString());
|
|
77
|
+
return parsed;
|
|
48
78
|
}
|
|
49
79
|
catch (err) {
|
|
50
80
|
// network failure: fall back to stale cache if present
|
|
51
81
|
const cached = readCachedCatalog();
|
|
52
82
|
if (cached)
|
|
53
83
|
return cached;
|
|
54
|
-
|
|
84
|
+
warn(`could not fetch models.dev catalog: ${err.message}`);
|
|
55
85
|
return undefined;
|
|
56
86
|
}
|
|
57
87
|
}
|