llm-switcher 1.2.2 → 1.2.4
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/CHANGELOG.md +17 -0
- package/README.md +7 -3
- package/README.vi.md +3 -3
- package/blindfold/certs.mjs +156 -0
- package/blindfold/make-certs.sh +3 -1
- package/catalog.mjs +132 -42
- package/docs/codex-blindfold.md +6 -4
- package/mcp.mjs +2 -2
- package/package.json +1 -1
- package/proxy.mjs +32 -12
- package/scripts/run-tests.mjs +3 -1
- package/state.mjs +88 -6
- package/switch.mjs +34 -9
- package/tests/blindfold-e2e.test.mjs +2 -2
- package/tests/blindfold-task5.test.mjs +1 -1
- package/tests/blindfold.task3.test.mjs +2 -2
- package/tests/blindfold.wire.test.mjs +3 -1
- package/tests/catalog.test.mjs +117 -19
- package/tests/certs.test.mjs +86 -0
- package/tests/codex-daemon.test.mjs +92 -0
- package/tests/gateway.e2e.test.mjs +36 -0
- package/tests/lifecycle.test.mjs +90 -57
- package/tests/make-certs.test.mjs +22 -0
- package/tests/real-user-sim.test.mjs +3 -0
- package/tests/shim.test.mjs +8 -3
- package/tests/state.test.mjs +8 -3
- package/tests/switch.test.mjs +30 -24
- package/tests/ui.test.mjs +110 -0
- package/tests/version.test.mjs +78 -0
- package/ui.html +325 -82
- package/version.mjs +53 -0
package/version.mjs
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// The running version, and whether npm has a newer one.
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { fileURLToPath } from 'node:url';
|
|
5
|
+
|
|
6
|
+
const ROOT = path.dirname(fileURLToPath(import.meta.url));
|
|
7
|
+
|
|
8
|
+
export const CURRENT_VERSION = (() => {
|
|
9
|
+
try { return JSON.parse(fs.readFileSync(path.join(ROOT, 'package.json'), 'utf8')).version; } catch { return '0.0.0'; }
|
|
10
|
+
})();
|
|
11
|
+
|
|
12
|
+
const REGISTRY_URL = 'https://registry.npmjs.org/llm-switcher/latest';
|
|
13
|
+
const CACHE_FILE = 'version-check.json';
|
|
14
|
+
const MAX_AGE_MS = 12 * 60 * 60 * 1000;
|
|
15
|
+
|
|
16
|
+
const parse = (v) => {
|
|
17
|
+
const m = /^(\d+)\.(\d+)\.(\d+)$/.exec(String(v ?? ''));
|
|
18
|
+
return m ? m.slice(1).map(Number) : null;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/** True only for a release that is newer. A prerelease or an unparseable version is never an update. */
|
|
22
|
+
export function isNewer(latest, current) {
|
|
23
|
+
const a = parse(latest), b = parse(current);
|
|
24
|
+
if (!a || !b) return false;
|
|
25
|
+
for (let i = 0; i < 3; i++) if (a[i] !== b[i]) return a[i] > b[i];
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// A git checkout updates with git; an npm install with npm.
|
|
30
|
+
const updateCommand = () => (fs.existsSync(path.join(ROOT, '.git')) ? `git -C "${ROOT}" pull` : 'npm install -g llm-switcher@latest');
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Asks the registry at most once per 12 hours; a failed ask also waits, so an offline machine is not
|
|
34
|
+
* slowed on every call. Never throws: without an answer there is simply no notice.
|
|
35
|
+
*/
|
|
36
|
+
export async function checkForUpdate({ stateDir, url = process.env.LLM_SWITCHER_REGISTRY_URL || REGISTRY_URL } = {}) {
|
|
37
|
+
const cacheFile = stateDir ? path.join(stateDir, CACHE_FILE) : null;
|
|
38
|
+
let cache = null;
|
|
39
|
+
try { cache = JSON.parse(fs.readFileSync(cacheFile, 'utf8')); } catch {}
|
|
40
|
+
|
|
41
|
+
let latest = cache?.latest ?? null;
|
|
42
|
+
if (!cache || !(Date.now() - (cache.checkedAt || 0) < MAX_AGE_MS)) {
|
|
43
|
+
try {
|
|
44
|
+
const r = await fetch(url, { signal: AbortSignal.timeout(4000), headers: { accept: 'application/json' } });
|
|
45
|
+
if (r.ok) {
|
|
46
|
+
const v = (await r.json())?.version;
|
|
47
|
+
if (parse(v)) latest = v;
|
|
48
|
+
}
|
|
49
|
+
} catch {}
|
|
50
|
+
try { if (cacheFile) fs.writeFileSync(cacheFile, JSON.stringify({ checkedAt: Date.now(), latest })); } catch {}
|
|
51
|
+
}
|
|
52
|
+
return { current: CURRENT_VERSION, latest, updateAvailable: isNewer(latest, CURRENT_VERSION), updateCommand: updateCommand() };
|
|
53
|
+
}
|