@melaya/runner 1.1.31 → 1.1.32
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/dist/cli.js +21 -14
- package/dist/uvBootstrap.d.ts +28 -0
- package/dist/uvBootstrap.js +213 -0
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -52,26 +52,33 @@ async function main() {
|
|
|
52
52
|
.parse(process.argv);
|
|
53
53
|
const opts = program.opts();
|
|
54
54
|
console.log(BANNER);
|
|
55
|
-
// Detect Python
|
|
55
|
+
// Detect Python. Fast path: a supported host interpreter (3.10–3.12). If the
|
|
56
|
+
// host only has an unsupported/newer Python (Homebrew now ships 3.13/3.14 as
|
|
57
|
+
// `python3`) — or none at all — we PROVISION a managed CPython 3.12 via uv, so
|
|
58
|
+
// the runner needs zero Python setup on the box. `venv` can't do this (it only
|
|
59
|
+
// wraps an existing interpreter); uv downloads a standalone one.
|
|
60
|
+
let python;
|
|
56
61
|
const py = await findPython();
|
|
57
|
-
if (
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
62
|
+
if (py.ok) {
|
|
63
|
+
python = py.cmd;
|
|
64
|
+
console.log(chalk.green(` ✓ Python: ${python} (3.${py.minor})`));
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
console.log(chalk.yellow(` ⚠ No supported host Python (need 3.${PY_MIN_MINOR}–3.${PY_MAX_MINOR}` +
|
|
68
|
+
`${py.foundVersion ? `; host has ${py.foundVersion}` : ""}) — provisioning one automatically`));
|
|
69
|
+
const { provisionPython } = await import("./uvBootstrap.js");
|
|
70
|
+
const provisioned = await provisionPython(`3.${PY_MAX_MINOR}`, (m) => console.log(chalk.gray(` ${m}`)));
|
|
71
|
+
if (!provisioned) {
|
|
72
|
+
console.log(chalk.red(` ✗ Could not auto-provision Python 3.${PY_MAX_MINOR}`));
|
|
73
|
+
console.log(chalk.gray(" Install a supported Python manually, then restart the runner:"));
|
|
63
74
|
console.log(chalk.gray(" macOS: brew install python@3.12"));
|
|
64
75
|
console.log(chalk.gray(" Linux: sudo apt install python3.12 python3.12-venv (or: pyenv install 3.12)"));
|
|
65
76
|
console.log(chalk.gray(" Windows: winget install Python.Python.3.12"));
|
|
77
|
+
process.exit(1);
|
|
66
78
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
console.log(chalk.gray(" Install Python 3.12 from https://python.org"));
|
|
70
|
-
}
|
|
71
|
-
process.exit(1);
|
|
79
|
+
python = provisioned;
|
|
80
|
+
console.log(chalk.green(` ✓ Python: ${python} (auto-provisioned 3.${PY_MAX_MINOR})`));
|
|
72
81
|
}
|
|
73
|
-
const python = py.cmd;
|
|
74
|
-
console.log(chalk.green(` ✓ Python: ${python} (3.${py.minor})`));
|
|
75
82
|
// Detect local models
|
|
76
83
|
const models = await detectModels();
|
|
77
84
|
if (models.length === 0) {
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zero-setup Python provisioning.
|
|
3
|
+
*
|
|
4
|
+
* `python -m venv` does NOT install a Python — it creates a thin environment
|
|
5
|
+
* that symlinks back to an existing interpreter, frozen to that interpreter's
|
|
6
|
+
* version. So when the host has no supported Python (e.g. Homebrew now ships
|
|
7
|
+
* 3.13/3.14 as `python3`, which has no wheels for agentscope's native deps),
|
|
8
|
+
* the runner can't build a working venv no matter what.
|
|
9
|
+
*
|
|
10
|
+
* This module removes the requirement for a host Python entirely: it uses
|
|
11
|
+
* `uv` (a single static binary) to download a *managed, standalone* CPython of
|
|
12
|
+
* the supported version and returns its interpreter path. The existing venv
|
|
13
|
+
* pipeline then builds from that. `uv` itself is bootstrapped from its official
|
|
14
|
+
* GitHub release if it isn't already installed — no `brew install`, no manual
|
|
15
|
+
* steps. All failures degrade to `null` so the caller can fall back to a clear
|
|
16
|
+
* "install Python 3.12" message.
|
|
17
|
+
*/
|
|
18
|
+
type Log = (m: string) => void;
|
|
19
|
+
export declare function localUvPath(): string;
|
|
20
|
+
/**
|
|
21
|
+
* Provision a supported CPython with NO host Python required. Downloads a
|
|
22
|
+
* managed standalone CPython of `version` via uv (bootstrapping uv itself if
|
|
23
|
+
* needed) and returns its interpreter path — which the venv pipeline builds
|
|
24
|
+
* from. Returns null if provisioning isn't possible (offline, unsupported
|
|
25
|
+
* platform, tar missing), so the caller can show manual-install guidance.
|
|
26
|
+
*/
|
|
27
|
+
export declare function provisionPython(version: string, onLine: Log): Promise<string | null>;
|
|
28
|
+
export {};
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zero-setup Python provisioning.
|
|
3
|
+
*
|
|
4
|
+
* `python -m venv` does NOT install a Python — it creates a thin environment
|
|
5
|
+
* that symlinks back to an existing interpreter, frozen to that interpreter's
|
|
6
|
+
* version. So when the host has no supported Python (e.g. Homebrew now ships
|
|
7
|
+
* 3.13/3.14 as `python3`, which has no wheels for agentscope's native deps),
|
|
8
|
+
* the runner can't build a working venv no matter what.
|
|
9
|
+
*
|
|
10
|
+
* This module removes the requirement for a host Python entirely: it uses
|
|
11
|
+
* `uv` (a single static binary) to download a *managed, standalone* CPython of
|
|
12
|
+
* the supported version and returns its interpreter path. The existing venv
|
|
13
|
+
* pipeline then builds from that. `uv` itself is bootstrapped from its official
|
|
14
|
+
* GitHub release if it isn't already installed — no `brew install`, no manual
|
|
15
|
+
* steps. All failures degrade to `null` so the caller can fall back to a clear
|
|
16
|
+
* "install Python 3.12" message.
|
|
17
|
+
*/
|
|
18
|
+
import { existsSync, mkdirSync, writeFileSync, chmodSync, rmSync, readdirSync, statSync, copyFileSync } from "fs";
|
|
19
|
+
import { join } from "path";
|
|
20
|
+
import { homedir, platform, arch } from "os";
|
|
21
|
+
import { spawn, execSync } from "child_process";
|
|
22
|
+
const CACHE_DIR = join(homedir(), ".melaya-runner");
|
|
23
|
+
const BIN_DIR = join(CACHE_DIR, "bin");
|
|
24
|
+
const isWin = platform() === "win32";
|
|
25
|
+
function uvBinName() { return isWin ? "uv.exe" : "uv"; }
|
|
26
|
+
export function localUvPath() { return join(BIN_DIR, uvBinName()); }
|
|
27
|
+
// Release asset for this platform/arch, from github.com/astral-sh/uv/releases.
|
|
28
|
+
function assetName() {
|
|
29
|
+
const p = platform(), a = arch();
|
|
30
|
+
if (p === "darwin")
|
|
31
|
+
return a === "arm64" ? "uv-aarch64-apple-darwin.tar.gz" : "uv-x86_64-apple-darwin.tar.gz";
|
|
32
|
+
if (p === "linux") {
|
|
33
|
+
if (a === "arm64")
|
|
34
|
+
return "uv-aarch64-unknown-linux-gnu.tar.gz";
|
|
35
|
+
if (a === "x64")
|
|
36
|
+
return "uv-x86_64-unknown-linux-gnu.tar.gz";
|
|
37
|
+
return null; // unusual arch — fall back to manual install guidance
|
|
38
|
+
}
|
|
39
|
+
if (p === "win32")
|
|
40
|
+
return a === "arm64" ? "uv-aarch64-pc-windows-msvc.zip" : "uv-x86_64-pc-windows-msvc.zip";
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
function tryExec(cmd) {
|
|
44
|
+
try {
|
|
45
|
+
return execSync(cmd, { encoding: "utf-8", timeout: 8000, stdio: ["pipe", "pipe", "pipe"] }).trim() || null;
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
function findOnPath(bin) {
|
|
52
|
+
const out = tryExec(isWin ? `where ${bin}` : `command -v ${bin}`);
|
|
53
|
+
if (!out)
|
|
54
|
+
return null;
|
|
55
|
+
const first = out.split(/\r?\n/)[0].trim();
|
|
56
|
+
return first || null;
|
|
57
|
+
}
|
|
58
|
+
// Inactivity-bounded child runner (mirrors pythonEnv.runProc): a managed-CPython
|
|
59
|
+
// download legitimately takes a while but streams progress, so we bound silence.
|
|
60
|
+
function run(cmd, args, onLine, inactivityMs = 5 * 60_000) {
|
|
61
|
+
return new Promise((resolve) => {
|
|
62
|
+
const child = spawn(cmd, args, { stdio: ["ignore", "pipe", "pipe"], env: process.env });
|
|
63
|
+
let done = false;
|
|
64
|
+
let timer;
|
|
65
|
+
const finish = (c) => { if (done)
|
|
66
|
+
return; done = true; if (timer)
|
|
67
|
+
clearTimeout(timer); resolve(c); };
|
|
68
|
+
const bump = () => {
|
|
69
|
+
if (timer)
|
|
70
|
+
clearTimeout(timer);
|
|
71
|
+
timer = setTimeout(() => { try {
|
|
72
|
+
child.kill("SIGKILL");
|
|
73
|
+
}
|
|
74
|
+
catch { /* gone */ } onLine(`(no output for ${Math.round(inactivityMs / 60000)}m — killed ${cmd})`); finish(1); }, inactivityMs);
|
|
75
|
+
};
|
|
76
|
+
const chew = (b) => { bump(); for (const ln of b.toString().split("\n")) {
|
|
77
|
+
const t = ln.trimEnd();
|
|
78
|
+
if (t)
|
|
79
|
+
onLine(t);
|
|
80
|
+
} };
|
|
81
|
+
child.stdout?.on("data", chew);
|
|
82
|
+
child.stderr?.on("data", chew);
|
|
83
|
+
child.on("exit", (c) => finish(c ?? 1));
|
|
84
|
+
child.on("error", () => finish(1));
|
|
85
|
+
bump();
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
function capture(cmd, args) {
|
|
89
|
+
return new Promise((resolve) => {
|
|
90
|
+
let out = "";
|
|
91
|
+
const child = spawn(cmd, args, { stdio: ["ignore", "pipe", "pipe"], env: process.env });
|
|
92
|
+
child.stdout?.on("data", (b) => { out += b.toString(); });
|
|
93
|
+
child.on("exit", () => resolve(out.trim()));
|
|
94
|
+
child.on("error", () => resolve(""));
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
function findBinaryRecursive(dir, name) {
|
|
98
|
+
let entries;
|
|
99
|
+
try {
|
|
100
|
+
entries = readdirSync(dir);
|
|
101
|
+
}
|
|
102
|
+
catch {
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
for (const e of entries) {
|
|
106
|
+
const full = join(dir, e);
|
|
107
|
+
let st;
|
|
108
|
+
try {
|
|
109
|
+
st = statSync(full);
|
|
110
|
+
}
|
|
111
|
+
catch {
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
if (st.isDirectory()) {
|
|
115
|
+
const found = findBinaryRecursive(full, name);
|
|
116
|
+
if (found)
|
|
117
|
+
return found;
|
|
118
|
+
}
|
|
119
|
+
else if (e === name)
|
|
120
|
+
return full;
|
|
121
|
+
}
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
// Download + extract the uv binary from its latest GitHub release. The
|
|
125
|
+
// `/releases/latest/download/<asset>` path redirects to the newest asset, so we
|
|
126
|
+
// track upstream without pinning a possibly-nonexistent tag. tar handles the
|
|
127
|
+
// .tar.gz on macOS/Linux and (via bsdtar) the .zip on Windows.
|
|
128
|
+
async function downloadUv(onLine) {
|
|
129
|
+
const asset = assetName();
|
|
130
|
+
if (!asset) {
|
|
131
|
+
onLine(`no uv build available for ${platform()}/${arch()}`);
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
const url = `https://github.com/astral-sh/uv/releases/latest/download/${asset}`;
|
|
135
|
+
mkdirSync(BIN_DIR, { recursive: true });
|
|
136
|
+
const tmp = join(BIN_DIR, "_dl");
|
|
137
|
+
try {
|
|
138
|
+
rmSync(tmp, { recursive: true, force: true });
|
|
139
|
+
}
|
|
140
|
+
catch { /* fresh */ }
|
|
141
|
+
mkdirSync(tmp, { recursive: true });
|
|
142
|
+
const archivePath = join(tmp, asset);
|
|
143
|
+
onLine(`downloading uv (${asset})`);
|
|
144
|
+
try {
|
|
145
|
+
const res = await fetch(url, { redirect: "follow", signal: AbortSignal.timeout(120_000) });
|
|
146
|
+
if (!res.ok) {
|
|
147
|
+
onLine(`uv download failed: HTTP ${res.status}`);
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
writeFileSync(archivePath, Buffer.from(await res.arrayBuffer()));
|
|
151
|
+
}
|
|
152
|
+
catch (e) {
|
|
153
|
+
onLine(`uv download error: ${e?.message}`);
|
|
154
|
+
return null;
|
|
155
|
+
}
|
|
156
|
+
onLine("extracting uv");
|
|
157
|
+
const code = await run("tar", ["-xf", archivePath, "-C", tmp], onLine);
|
|
158
|
+
if (code !== 0) {
|
|
159
|
+
onLine("tar extraction of uv failed (is `tar` available?)");
|
|
160
|
+
return null;
|
|
161
|
+
}
|
|
162
|
+
const found = findBinaryRecursive(tmp, uvBinName());
|
|
163
|
+
if (!found) {
|
|
164
|
+
onLine("uv binary not found inside the downloaded archive");
|
|
165
|
+
return null;
|
|
166
|
+
}
|
|
167
|
+
const dest = localUvPath();
|
|
168
|
+
try {
|
|
169
|
+
copyFileSync(found, dest);
|
|
170
|
+
if (!isWin)
|
|
171
|
+
chmodSync(dest, 0o755);
|
|
172
|
+
}
|
|
173
|
+
catch (e) {
|
|
174
|
+
onLine(`could not install uv to ${dest}: ${e?.message}`);
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
177
|
+
try {
|
|
178
|
+
rmSync(tmp, { recursive: true, force: true });
|
|
179
|
+
}
|
|
180
|
+
catch { /* best-effort cleanup */ }
|
|
181
|
+
return dest;
|
|
182
|
+
}
|
|
183
|
+
async function ensureUv(onLine) {
|
|
184
|
+
if (existsSync(localUvPath()))
|
|
185
|
+
return localUvPath(); // previously downloaded
|
|
186
|
+
const onPath = findOnPath(uvBinName());
|
|
187
|
+
if (onPath)
|
|
188
|
+
return onPath; // operator already has uv
|
|
189
|
+
return downloadUv(onLine); // bootstrap it
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Provision a supported CPython with NO host Python required. Downloads a
|
|
193
|
+
* managed standalone CPython of `version` via uv (bootstrapping uv itself if
|
|
194
|
+
* needed) and returns its interpreter path — which the venv pipeline builds
|
|
195
|
+
* from. Returns null if provisioning isn't possible (offline, unsupported
|
|
196
|
+
* platform, tar missing), so the caller can show manual-install guidance.
|
|
197
|
+
*/
|
|
198
|
+
export async function provisionPython(version, onLine) {
|
|
199
|
+
const uv = await ensureUv(onLine);
|
|
200
|
+
if (!uv)
|
|
201
|
+
return null;
|
|
202
|
+
onLine(`installing a managed Python ${version} (one-time; ~30-60s)`);
|
|
203
|
+
const code = await run(uv, ["python", "install", version], onLine);
|
|
204
|
+
if (code !== 0) {
|
|
205
|
+
onLine(`uv python install ${version} failed`);
|
|
206
|
+
return null;
|
|
207
|
+
}
|
|
208
|
+
const path = await capture(uv, ["python", "find", version]);
|
|
209
|
+
if (path && existsSync(path))
|
|
210
|
+
return path;
|
|
211
|
+
onLine(`uv could not resolve a Python ${version} interpreter path after install`);
|
|
212
|
+
return null;
|
|
213
|
+
}
|