@drakonkat/ai-helper 1.1.0 → 1.2.2
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/ARCHITECTURE.md +14 -3
- package/CHANGELOG.md +15 -0
- package/README.md +417 -24
- package/docs/benchmark.md +273 -0
- package/package.json +17 -5
- package/scripts/bench-proxy.js +107 -0
- package/scripts/build.js +16 -0
- package/src/benchmark/fixtures.js +150 -0
- package/src/benchmark/live.js +78 -0
- package/src/benchmark/measurement.js +180 -0
- package/src/benchmark/quality.js +144 -0
- package/src/benchmark/report.js +141 -0
- package/src/benchmark/runner.js +269 -0
- package/src/codex-config.js +247 -0
- package/src/codex-routing.js +132 -0
- package/src/config.js +11 -1
- package/src/index.js +112 -22
- package/src/manager.js +126 -13
- package/src/proxy-interceptor.example.mjs +33 -0
- package/src/proxy-presets.js +226 -0
- package/src/proxy-stats.js +64 -0
- package/src/proxy-worker.js +17 -0
- package/src/proxy.js +313 -0
- package/src/tui.js +694 -0
- package/src/update.js +41 -0
- package/src/utils/metrics.js +315 -0
- package/src/utils/process.js +120 -140
- package/src/utils/versions.js +171 -0
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { readFile, realpath } from "node:fs/promises";
|
|
3
|
+
import { delimiter, dirname, isAbsolute, join, resolve } from "node:path";
|
|
4
|
+
import { baseUrl, fetchJson } from "./metrics.js";
|
|
5
|
+
|
|
6
|
+
const PACKAGES = {
|
|
7
|
+
pxpipe: { name: "pxpipe-proxy", bin: "pxpipe" },
|
|
8
|
+
ocx: { name: "@bitkyc08/opencodex", bin: "ocx", health: "/healthz", service: "opencodex" },
|
|
9
|
+
agentmemory: { name: "@agentmemory/agentmemory", bin: "agentmemory", health: "/agentmemory/health", service: "agentmemory" },
|
|
10
|
+
headroom: { name: "headroom-ai", bin: "headroom", health: "/health", pypi: true },
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
function parseVersion(value) {
|
|
14
|
+
if (typeof value !== "string") return null;
|
|
15
|
+
const match = value.trim().match(/^v?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/);
|
|
16
|
+
if (!match) return null;
|
|
17
|
+
if (match[4]?.split(".").some(part => /^0\d+$/.test(part))) return null;
|
|
18
|
+
return { core: match.slice(1, 4).map(BigInt), pre: match[4]?.split(".") || [] };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function cleanVersion(value) {
|
|
22
|
+
return parseVersion(value) ? value.trim().replace(/^v/, "") : null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Returns -1, 0, 1, or null when either version cannot be compared safely. */
|
|
26
|
+
export function compareVersions(left, right) {
|
|
27
|
+
const a = parseVersion(left);
|
|
28
|
+
const b = parseVersion(right);
|
|
29
|
+
if (!a || !b) return null;
|
|
30
|
+
for (let i = 0; i < 3; i++) {
|
|
31
|
+
if (a.core[i] !== b.core[i]) return a.core[i] < b.core[i] ? -1 : 1;
|
|
32
|
+
}
|
|
33
|
+
if (!a.pre.length || !b.pre.length) return a.pre.length === b.pre.length ? 0 : a.pre.length ? -1 : 1;
|
|
34
|
+
for (let i = 0; i < Math.max(a.pre.length, b.pre.length); i++) {
|
|
35
|
+
const x = a.pre[i];
|
|
36
|
+
const y = b.pre[i];
|
|
37
|
+
if (x === y) continue;
|
|
38
|
+
if (x === undefined || y === undefined) return x === undefined ? -1 : 1;
|
|
39
|
+
const xn = /^\d+$/.test(x);
|
|
40
|
+
const yn = /^\d+$/.test(y);
|
|
41
|
+
if (xn && yn) {
|
|
42
|
+
if (BigInt(x) !== BigInt(y)) return BigInt(x) < BigInt(y) ? -1 : 1;
|
|
43
|
+
} else if (xn !== yn) return xn ? -1 : 1;
|
|
44
|
+
else return x < y ? -1 : 1;
|
|
45
|
+
}
|
|
46
|
+
return 0;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Only bounded, read-only commands are used; never invoke npx or an updater. */
|
|
50
|
+
export function runVersionCommand(command, args, timeoutMs = 3000) {
|
|
51
|
+
return new Promise(resolveResult => {
|
|
52
|
+
execFile(command, args, { timeout: timeoutMs, maxBuffer: 256 * 1024, windowsHide: true }, (error, stdout) => {
|
|
53
|
+
resolveResult(error ? null : String(stdout).trim());
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async function getProcessCommands(statuses) {
|
|
59
|
+
const pids = [...new Set(statuses.filter(s => s.status === "running").map(s => Number(s.pid)))]
|
|
60
|
+
.filter(pid => Number.isSafeInteger(pid) && pid > 0);
|
|
61
|
+
if (!pids.length) return {};
|
|
62
|
+
if (process.platform === "win32") {
|
|
63
|
+
const filter = pids.map(pid => `ProcessId = ${pid}`).join(" OR ");
|
|
64
|
+
const output = await runVersionCommand("powershell.exe", ["-NoProfile", "-NonInteractive", "-Command",
|
|
65
|
+
`Get-CimInstance Win32_Process -Filter '${filter}' | Select-Object ProcessId,CommandLine | ConvertTo-Json -Compress`]);
|
|
66
|
+
try {
|
|
67
|
+
const parsed = JSON.parse(output);
|
|
68
|
+
return Object.fromEntries((Array.isArray(parsed) ? parsed : [parsed]).filter(Boolean).map(p => [p.ProcessId, p.CommandLine]));
|
|
69
|
+
} catch { return {}; }
|
|
70
|
+
}
|
|
71
|
+
const output = await runVersionCommand("ps", ["-p", pids.join(","), "-o", "pid=,args="]);
|
|
72
|
+
return Object.fromEntries((output || "").split("\n").flatMap(line => {
|
|
73
|
+
const match = line.trim().match(/^(\d+)\s+(.+)$/);
|
|
74
|
+
return match ? [[match[1], match[2]]] : [];
|
|
75
|
+
}));
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async function readPackageVersion(path, name) {
|
|
79
|
+
try {
|
|
80
|
+
const pkg = JSON.parse(await readFile(path, "utf8"));
|
|
81
|
+
return pkg.name === name ? cleanVersion(pkg.version) : null;
|
|
82
|
+
} catch { return null; }
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Read the package referenced by this process, not an arbitrary npx cache entry. */
|
|
86
|
+
export async function readNpmVersionFromCommand(command, name) {
|
|
87
|
+
for (const match of (command || "").matchAll(/"([^"]+)"|'([^']+)'|(\S+)/g)) {
|
|
88
|
+
const token = match[1] || match[2] || match[3];
|
|
89
|
+
if (!isAbsolute(token)) continue;
|
|
90
|
+
const path = resolve(token).replace(/\\/g, "/");
|
|
91
|
+
const marker = `/node_modules/${name}/`;
|
|
92
|
+
const index = path.lastIndexOf(marker);
|
|
93
|
+
if (index < 0) continue;
|
|
94
|
+
const version = await readPackageVersion(`${path.slice(0, index + marker.length)}package.json`, name);
|
|
95
|
+
if (version) return version;
|
|
96
|
+
}
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async function getInstalledVersion(status, command) {
|
|
101
|
+
const pkg = PACKAGES[status.id];
|
|
102
|
+
if (pkg.pypi) {
|
|
103
|
+
// Headroom's Click CLI explicitly implements --version without starting a proxy.
|
|
104
|
+
const output = await runVersionCommand("headroom", ["--version"]);
|
|
105
|
+
return cleanVersion(output?.match(/^headroom, version\s+(\S+)$/im)?.[1]);
|
|
106
|
+
}
|
|
107
|
+
const running = await readNpmVersionFromCommand(command, pkg.name);
|
|
108
|
+
if (running) return running;
|
|
109
|
+
// A running service may be an older cached install, not the package on PATH.
|
|
110
|
+
if (status.status === "running") return null;
|
|
111
|
+
for (const directory of (process.env.PATH || "").split(delimiter).filter(Boolean)) {
|
|
112
|
+
// npm's Windows shims live alongside node_modules; Unix bins are symlinks.
|
|
113
|
+
for (const candidate of [join(directory, "node_modules", pkg.name, "package.json"),
|
|
114
|
+
join(directory, "..", "lib", "node_modules", pkg.name, "package.json")]) {
|
|
115
|
+
const version = await readPackageVersion(candidate, pkg.name);
|
|
116
|
+
if (version) return version;
|
|
117
|
+
}
|
|
118
|
+
try {
|
|
119
|
+
const bin = await realpath(join(directory, pkg.bin));
|
|
120
|
+
const version = await readNpmVersionFromCommand(JSON.stringify(bin), pkg.name);
|
|
121
|
+
if (version) return version;
|
|
122
|
+
// Bun/pnpm links may resolve outside node_modules; only trust named metadata.
|
|
123
|
+
for (let path = dirname(bin), i = 0; i < 3; path = dirname(path), i++) {
|
|
124
|
+
const version = await readPackageVersion(join(path, "package.json"), pkg.name);
|
|
125
|
+
if (version) return version;
|
|
126
|
+
}
|
|
127
|
+
} catch {}
|
|
128
|
+
}
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Installed/running and latest published versions. Unknowns stay null, including
|
|
134
|
+
* updateAvailable: a failed registry check is not evidence of being up to date.
|
|
135
|
+
* Optional readers keep tests offline and independent of installed services.
|
|
136
|
+
*/
|
|
137
|
+
export async function getServiceVersions(statuses, {
|
|
138
|
+
request = fetchJson,
|
|
139
|
+
processCommands = getProcessCommands,
|
|
140
|
+
installedVersion = getInstalledVersion,
|
|
141
|
+
} = {}) {
|
|
142
|
+
const services = (statuses || []).filter(status => PACKAGES[status.id]);
|
|
143
|
+
let commands;
|
|
144
|
+
const readCommands = () => commands ||= Promise.resolve().then(() => processCommands(services)).catch(() => ({}));
|
|
145
|
+
return Object.fromEntries(await Promise.all(services.map(async status => {
|
|
146
|
+
const pkg = PACKAGES[status.id];
|
|
147
|
+
const latest = (async () => {
|
|
148
|
+
try {
|
|
149
|
+
const url = pkg.pypi ? `https://pypi.org/pypi/${pkg.name}/json` : `https://registry.npmjs.org/${encodeURIComponent(pkg.name)}/latest`;
|
|
150
|
+
const data = await request(url, 3000);
|
|
151
|
+
return cleanVersion(pkg.pypi ? data?.info?.version : data?.version);
|
|
152
|
+
} catch { return null; }
|
|
153
|
+
})();
|
|
154
|
+
const installed = (async () => {
|
|
155
|
+
if (status.status === "running" && pkg.health && baseUrl(status.url)) {
|
|
156
|
+
try {
|
|
157
|
+
const data = await request(`${baseUrl(status.url)}${pkg.health}`, 2000);
|
|
158
|
+
const version = cleanVersion(data?.version);
|
|
159
|
+
if (version && (!pkg.service || data?.service === pkg.service)) return version;
|
|
160
|
+
} catch {}
|
|
161
|
+
}
|
|
162
|
+
try {
|
|
163
|
+
const command = status.status === "running" ? (await readCommands())[status.pid] : null;
|
|
164
|
+
return cleanVersion(await installedVersion(status, command));
|
|
165
|
+
} catch { return null; }
|
|
166
|
+
})();
|
|
167
|
+
const [version, latestVersion] = await Promise.all([installed, latest]);
|
|
168
|
+
const comparison = compareVersions(version, latestVersion);
|
|
169
|
+
return [status.id, { version, latestVersion, updateAvailable: comparison === null ? null : comparison < 0 }];
|
|
170
|
+
})));
|
|
171
|
+
}
|