openreynolds 0.1.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 +90 -0
- package/bin/openreynolds.js +536 -0
- package/package.json +41 -0
package/README.md
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# openreynolds (npm launcher)
|
|
2
|
+
|
|
3
|
+
Agentic OpenFOAM CFD: describe a flow problem, get an evidence-backed answer.
|
|
4
|
+
|
|
5
|
+
This npm package is a **launcher**, not the program. The real thing is the
|
|
6
|
+
[`openreynolds` Python package](https://pypi.org/project/openreynolds/); everything
|
|
7
|
+
about what it does, what it needs (an Anthropic key, a compute backend) and how to use
|
|
8
|
+
it is in the
|
|
9
|
+
[project README](https://github.com/InviscidAI/OpenReynolds#readme).
|
|
10
|
+
`npm install -g openreynolds` exists so people who live in Node get the same
|
|
11
|
+
one-command install as everyone else.
|
|
12
|
+
|
|
13
|
+
## Install
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
npm install -g openreynolds
|
|
17
|
+
openreynolds "flow over a cylinder at Re=100"
|
|
18
|
+
|
|
19
|
+
# or without installing
|
|
20
|
+
npx openreynolds --help
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Prefer the Python-native route if you already have `uv` or `pipx`:
|
|
24
|
+
|
|
25
|
+
```sh
|
|
26
|
+
uvx openreynolds # run without installing
|
|
27
|
+
pipx install openreynolds
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## What the launcher does
|
|
31
|
+
|
|
32
|
+
On every run, in order, saying what it is doing before it does it:
|
|
33
|
+
|
|
34
|
+
1. **Finds `uv`** (`OPENREYNOLDS_UV`, then `PATH`, then the usual install directories).
|
|
35
|
+
If `uv` is missing it offers the official installer
|
|
36
|
+
(`curl -LsSf https://astral.sh/uv/install.sh | sh` on macOS/Linux,
|
|
37
|
+
`irm https://astral.sh/uv/install.ps1 | iex` on Windows). It asks first on a
|
|
38
|
+
terminal; pass `--yes` or set `OPENREYNOLDS_YES=1` to skip the question. In a
|
|
39
|
+
non-interactive shell without either it prints the instructions and exits with
|
|
40
|
+
code 2. Nothing is ever installed silently.
|
|
41
|
+
2. **Installs the Python package once**, with `uv tool install "openreynolds>=0.1,<0.2"`
|
|
42
|
+
(the range is pinned in the launcher; `uv` fetches a Python 3.10+ if you have
|
|
43
|
+
none). Skipped when `uv tool list` already shows it.
|
|
44
|
+
3. **Execs the installed `openreynolds`** with all of your arguments, inheriting
|
|
45
|
+
stdin/stdout/stderr and the environment. Exit codes are passed through; Ctrl-C
|
|
46
|
+
goes to the Python process, which does its own cleanup.
|
|
47
|
+
|
|
48
|
+
Node is only involved for those few milliseconds of bookkeeping.
|
|
49
|
+
|
|
50
|
+
## Launcher-only commands and flags
|
|
51
|
+
|
|
52
|
+
Everything not listed here is forwarded to the Python CLI untouched. Launcher flags
|
|
53
|
+
must come before any forwarded arguments.
|
|
54
|
+
|
|
55
|
+
| Command / flag | Meaning |
|
|
56
|
+
| --- | --- |
|
|
57
|
+
| `openreynolds upgrade` | Upgrade the Python package within the launcher's pinned range (`uv tool install --upgrade`). Update the launcher itself with `npm update -g openreynolds`. |
|
|
58
|
+
| `openreynolds --launcher-version` | Print the npm launcher version and the Python spec it pins. |
|
|
59
|
+
| `--yes` | Run the uv installer without asking. |
|
|
60
|
+
| `--from-path <dir>` | Developers: install a local checkout in editable mode (`uv tool install --editable <dir>`) instead of the PyPI release. |
|
|
61
|
+
|
|
62
|
+
## Environment variables
|
|
63
|
+
|
|
64
|
+
| Variable | Effect |
|
|
65
|
+
| --- | --- |
|
|
66
|
+
| `OPENREYNOLDS_UV` | Absolute path to the `uv` executable to use. |
|
|
67
|
+
| `OPENREYNOLDS_YES=1` | Same as `--yes`. |
|
|
68
|
+
| `OPENREYNOLDS_PYTHON_SPEC` | Override the pinned requirement, e.g. `openreynolds==0.1.3` or `openreynolds>=0.2.0a1`. |
|
|
69
|
+
| `OPENREYNOLDS_ASCII=1` | Plain ASCII output (no box-drawing glyphs). |
|
|
70
|
+
| `NO_COLOR` / `FORCE_COLOR` | Disable / force ANSI color in the launcher's own output. |
|
|
71
|
+
|
|
72
|
+
Anything else (`ANTHROPIC_API_KEY`, backend configuration, ...) belongs to the Python
|
|
73
|
+
program and is passed through unchanged.
|
|
74
|
+
|
|
75
|
+
## Uninstall
|
|
76
|
+
|
|
77
|
+
```sh
|
|
78
|
+
npm uninstall -g openreynolds
|
|
79
|
+
uv tool uninstall openreynolds
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## Note on the `reynolds` npm package
|
|
83
|
+
|
|
84
|
+
`reynolds` on npm (v0.1.x) is the earlier, different product: a bundled terminal app
|
|
85
|
+
with its own hosted-compute setup. It is not this launcher and does not install
|
|
86
|
+
`openreynolds`.
|
|
87
|
+
|
|
88
|
+
## License
|
|
89
|
+
|
|
90
|
+
MIT. Source: https://github.com/InviscidAI/OpenReynolds/tree/main/launcher
|
|
@@ -0,0 +1,536 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* `openreynolds` -- the npm launcher for the openreynolds Python package.
|
|
6
|
+
*
|
|
7
|
+
* npm install -g openreynolds # or: npx openreynolds
|
|
8
|
+
* openreynolds "flow over a cylinder at Re=100"
|
|
9
|
+
*
|
|
10
|
+
* The real program lives on PyPI. This file exists so people who live in Node get a
|
|
11
|
+
* one-command install; it owns exactly three jobs and then gets out of the way:
|
|
12
|
+
*
|
|
13
|
+
* 1. find `uv` (offering the official installer when it is missing),
|
|
14
|
+
* 2. `uv tool install` the Python package once, pinned to PYTHON_PACKAGE_SPEC,
|
|
15
|
+
* 3. hand the terminal to the installed `openreynolds` binary.
|
|
16
|
+
*
|
|
17
|
+
* It is dependency-free on purpose: it is the first thing a new user runs, so it has to
|
|
18
|
+
* work on a bare Node 18 with nothing else present. Every side effect is announced on
|
|
19
|
+
* one line before it happens, and nothing is installed without saying so first.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
const { spawn, spawnSync } = require("node:child_process");
|
|
23
|
+
const fs = require("node:fs");
|
|
24
|
+
const os = require("node:os");
|
|
25
|
+
const path = require("node:path");
|
|
26
|
+
|
|
27
|
+
// Bump this range with every launcher release that tracks a new Python minor. The
|
|
28
|
+
// launcher deliberately pins a range rather than an exact version so `openreynolds
|
|
29
|
+
// upgrade` can pick up patch releases without an npm update.
|
|
30
|
+
const PYTHON_PACKAGE = "openreynolds";
|
|
31
|
+
const PYTHON_PACKAGE_SPEC = "openreynolds>=0.1,<0.2";
|
|
32
|
+
const UV_DOCS = "https://docs.astral.sh/uv/";
|
|
33
|
+
const REPO = "https://github.com/InviscidAI/OpenReynolds";
|
|
34
|
+
|
|
35
|
+
// -NoProfile: a user's PowerShell profile can fail loudly (OneDrive-redirected
|
|
36
|
+
// Documents whose provider is offline) and that noise would look like our failure.
|
|
37
|
+
const uvInstallCommand = (platform) =>
|
|
38
|
+
platform === "win32"
|
|
39
|
+
? 'powershell -NoProfile -ExecutionPolicy Bypass -c "irm https://astral.sh/uv/install.ps1 | iex"'
|
|
40
|
+
: "curl -LsSf https://astral.sh/uv/install.sh | sh";
|
|
41
|
+
|
|
42
|
+
// --- theme --------------------------------------------------------------------------
|
|
43
|
+
|
|
44
|
+
const DEPTH = { NONE: 0, ANSI16: 1, ANSI256: 2, TRUECOLOR: 3 };
|
|
45
|
+
const BRAND = [0x38, 0xbd, 0xf8];
|
|
46
|
+
const OK = [0x34, 0xd3, 0x99];
|
|
47
|
+
const WARN = [0xfb, 0xbf, 0x24];
|
|
48
|
+
const ERR = [0xf8, 0x71, 0x71];
|
|
49
|
+
const MUTED = [0x8b, 0x98, 0xa5];
|
|
50
|
+
const HEADING = [0xe2, 0xe8, 0xf0];
|
|
51
|
+
|
|
52
|
+
const UNICODE_GLYPHS = {
|
|
53
|
+
tl: "╭", tr: "╮", bl: "╰", br: "╯", h: "─", v: "│",
|
|
54
|
+
ok: "✓", warn: "▲", bad: "✕", arrow: "→", ellipsis: "…",
|
|
55
|
+
};
|
|
56
|
+
const ASCII_GLYPHS = {
|
|
57
|
+
tl: "+", tr: "+", bl: "+", br: "+", h: "-", v: "|",
|
|
58
|
+
ok: "+", warn: "!", bad: "x", arrow: "->", ellipsis: "...",
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const ANSI_RE = /\x1b\[[0-9;]*m/g;
|
|
62
|
+
const visibleWidth = (value) => String(value).replace(ANSI_RE, "").length;
|
|
63
|
+
const CUBE = [0, 95, 135, 175, 215, 255];
|
|
64
|
+
const cubeIndex = (v) =>
|
|
65
|
+
CUBE.reduce((best, level, i) => (Math.abs(level - v) < Math.abs(CUBE[best] - v) ? i : best), 0);
|
|
66
|
+
|
|
67
|
+
function rgbTo256([r, g, b]) {
|
|
68
|
+
if (Math.abs(r - g) < 12 && Math.abs(g - b) < 12) {
|
|
69
|
+
const gray = Math.round((r + g + b) / 3);
|
|
70
|
+
return gray < 8 ? 16 : gray > 248 ? 231 : 232 + Math.round(((gray - 8) / 247) * 23);
|
|
71
|
+
}
|
|
72
|
+
return 16 + 36 * cubeIndex(r) + 6 * cubeIndex(g) + cubeIndex(b);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function rgbTo16([r, g, b]) {
|
|
76
|
+
const base = (r > 110 ? 1 : 0) + (g > 110 ? 2 : 0) + (b > 110 ? 4 : 0);
|
|
77
|
+
return (Math.max(r, g, b) > 170 ? 90 : 30) + base;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function detectDepth(env, isTty) {
|
|
81
|
+
if (env.NO_COLOR) return DEPTH.NONE;
|
|
82
|
+
if (!isTty && !env.FORCE_COLOR) return DEPTH.NONE;
|
|
83
|
+
const term = (env.TERM || "").toLowerCase();
|
|
84
|
+
if (term === "dumb") return DEPTH.NONE;
|
|
85
|
+
const colorterm = (env.COLORTERM || "").toLowerCase();
|
|
86
|
+
if (colorterm.includes("truecolor") || colorterm.includes("24bit") || env.WT_SESSION) return DEPTH.TRUECOLOR;
|
|
87
|
+
const program = (env.TERM_PROGRAM || "").toLowerCase();
|
|
88
|
+
if (["vscode", "iterm.app", "wezterm", "ghostty", "hyper", "tabby"].includes(program)) return DEPTH.TRUECOLOR;
|
|
89
|
+
return term ? DEPTH.ANSI256 : DEPTH.ANSI16;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Node does not expose the console code page, so on Windows we need positive evidence
|
|
93
|
+
// of a modern terminal before emitting anything outside ASCII. OPENREYNOLDS_ASCII=1
|
|
94
|
+
// forces the fallback everywhere.
|
|
95
|
+
function detectUnicode(env, stream, platform) {
|
|
96
|
+
if (env.OPENREYNOLDS_ASCII) return false;
|
|
97
|
+
const encoding = String(stream.encoding || "").toLowerCase();
|
|
98
|
+
if (encoding && !/utf-?8/.test(encoding)) return false;
|
|
99
|
+
if (platform !== "win32") return true;
|
|
100
|
+
return Boolean(env.WT_SESSION || env.TERM_PROGRAM || env.MSYSTEM || env.TERM || env.ConEmuANSI);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function makeTheme(stream, env, platform) {
|
|
104
|
+
const isTty = Boolean(stream.isTTY);
|
|
105
|
+
const depth = detectDepth(env, isTty);
|
|
106
|
+
const glyphs = detectUnicode(env, stream, platform) ? UNICODE_GLYPHS : ASCII_GLYPHS;
|
|
107
|
+
// Piped output has no column limit; the install command must never be clipped there.
|
|
108
|
+
const width = isTty ? Math.max(52, Math.min(104, (stream.columns || 88) - 1)) : 120;
|
|
109
|
+
const fg = (value, color) => {
|
|
110
|
+
if (depth === DEPTH.NONE || !value) return value;
|
|
111
|
+
const code =
|
|
112
|
+
depth === DEPTH.TRUECOLOR ? `38;2;${color.join(";")}`
|
|
113
|
+
: depth === DEPTH.ANSI256 ? `38;5;${rgbTo256(color)}`
|
|
114
|
+
: String(rgbTo16(color));
|
|
115
|
+
return `\x1b[${code}m${value}\x1b[39m`;
|
|
116
|
+
};
|
|
117
|
+
const sgr = (value, code) => (depth === DEPTH.NONE || !value ? value : `\x1b[${code}m${value}\x1b[0m`);
|
|
118
|
+
return {
|
|
119
|
+
glyphs, width, isTty, fg,
|
|
120
|
+
bold: (v) => sgr(v, "1"),
|
|
121
|
+
brand: (v) => fg(v, BRAND),
|
|
122
|
+
good: (v) => fg(v, OK),
|
|
123
|
+
warning: (v) => fg(v, WARN),
|
|
124
|
+
bad: (v) => fg(v, ERR),
|
|
125
|
+
heading: (v) => sgr(fg(v, HEADING), "1"),
|
|
126
|
+
muted: (v) => sgr(fg(v, MUTED), "2"),
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function panel(theme, rows, { title = "", color = BRAND } = {}) {
|
|
131
|
+
const g = theme.glyphs;
|
|
132
|
+
const inner = Math.min(Math.max(...rows.map(visibleWidth), visibleWidth(title) + 4, 24), theme.width - 4);
|
|
133
|
+
const side = theme.fg(g.v, color);
|
|
134
|
+
const head = title ? ` ${theme.bold(theme.fg(title, color))} ` : "";
|
|
135
|
+
const bar = g.h.repeat(Math.max(0, inner + 2 - visibleWidth(head) - (title ? 1 : 0)));
|
|
136
|
+
const top = theme.fg(g.tl + (title ? g.h : ""), color) + head + theme.fg(bar + g.tr, color);
|
|
137
|
+
const bottom = theme.fg(g.bl + g.h.repeat(inner + 2) + g.br, color);
|
|
138
|
+
const clip = (row) =>
|
|
139
|
+
visibleWidth(row) <= inner ? row : String(row).replace(ANSI_RE, "").slice(0, inner - g.ellipsis.length) + g.ellipsis;
|
|
140
|
+
const body = rows.map((row) => `${side} ${clip(row)}${" ".repeat(Math.max(0, inner - visibleWidth(clip(row))))} ${side}`);
|
|
141
|
+
return [top, ...body, bottom].join("\n");
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// --- process helpers ----------------------------------------------------------------
|
|
145
|
+
|
|
146
|
+
// Quote one argv entry for cmd.exe; Node does not quote for you when `shell: true`.
|
|
147
|
+
const winQuote = (value) =>
|
|
148
|
+
/[\s"&|<>^()%!]/.test(value) ? `"${String(value).replace(/"/g, '""')}"` : String(value);
|
|
149
|
+
|
|
150
|
+
// A freshly installed uv.exe has reported `spawn UNKNOWN` on a clean Windows machine:
|
|
151
|
+
// CreateProcess refused the image directly while launching through cmd.exe worked.
|
|
152
|
+
// ENOENT/EACCES also cover .cmd/.bat shims.
|
|
153
|
+
const RETRYABLE_SPAWN = new Set(["UNKNOWN", "ENOENT", "EACCES", "EINVAL"]);
|
|
154
|
+
const shouldRetryInShell = (error, platform) =>
|
|
155
|
+
platform === "win32" && Boolean(error) && RETRYABLE_SPAWN.has(error.code);
|
|
156
|
+
|
|
157
|
+
const sleepSync = (ms) => Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
|
|
158
|
+
|
|
159
|
+
/** Run to completion, capturing output. Never throws. */
|
|
160
|
+
function runCapture(command, args, { env = process.env, timeout } = {}) {
|
|
161
|
+
const once = (cmd, argv, shell) => {
|
|
162
|
+
const r = spawnSync(cmd, argv, { env, shell, timeout, windowsHide: true, encoding: "utf8", stdio: ["ignore", "pipe", "pipe"] });
|
|
163
|
+
return { status: r.status, stdout: r.stdout || "", stderr: r.stderr || "", error: r.error || null };
|
|
164
|
+
};
|
|
165
|
+
const first = once(command, args, false);
|
|
166
|
+
if (!shouldRetryInShell(first.error, process.platform)) return first;
|
|
167
|
+
return once(winQuote(command), args.map(winQuote), true);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** Run with the terminal attached (installers). Never throws. */
|
|
171
|
+
function runInherit(command, args, { env = process.env } = {}) {
|
|
172
|
+
const once = (cmd, argv, shell) => spawnSync(cmd, argv, { env, shell, stdio: "inherit" });
|
|
173
|
+
const first = once(command, args, false);
|
|
174
|
+
if (!shouldRetryInShell(first.error, process.platform)) return first;
|
|
175
|
+
return once(winQuote(command), args.map(winQuote), true);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Hand the terminal to the real program and resolve with its exit code.
|
|
180
|
+
*
|
|
181
|
+
* Ctrl-C is delivered by the terminal to the whole foreground process group, so the
|
|
182
|
+
* child already receives it; the launcher must neither die first (which would orphan
|
|
183
|
+
* the Python process mid-cleanup) nor forward a second SIGINT on top. SIGTERM/SIGHUP
|
|
184
|
+
* target one pid, so those are relayed.
|
|
185
|
+
*/
|
|
186
|
+
function execInherit(command, args, { env = process.env } = {}) {
|
|
187
|
+
return new Promise((resolve) => {
|
|
188
|
+
const start = (cmd, argv, shell) => {
|
|
189
|
+
const child = spawn(cmd, argv, { env, shell, stdio: "inherit" });
|
|
190
|
+
const relay = (signal) => () => { try { child.kill(signal); } catch { /* already gone */ } };
|
|
191
|
+
const swallow = () => {};
|
|
192
|
+
const handlers = [["SIGINT", swallow], ["SIGTERM", relay("SIGTERM")], ["SIGHUP", relay("SIGHUP")]];
|
|
193
|
+
for (const [signal, handler] of handlers) process.on(signal, handler);
|
|
194
|
+
const done = (code) => {
|
|
195
|
+
for (const [signal, handler] of handlers) process.removeListener(signal, handler);
|
|
196
|
+
resolve(code);
|
|
197
|
+
};
|
|
198
|
+
child.on("error", (error) => {
|
|
199
|
+
if (!shell && shouldRetryInShell(error, process.platform)) {
|
|
200
|
+
for (const [signal, handler] of handlers) process.removeListener(signal, handler);
|
|
201
|
+
start(winQuote(cmd), argv.map(winQuote), true);
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
process.stderr.write(`openreynolds: could not start ${cmd}: ${error.message}\n`);
|
|
205
|
+
done(1);
|
|
206
|
+
});
|
|
207
|
+
child.on("exit", (code, signal) => {
|
|
208
|
+
if (signal) done(128 + ((os.constants.signals || {})[signal] || 2));
|
|
209
|
+
else done(code === null ? 1 : code);
|
|
210
|
+
});
|
|
211
|
+
};
|
|
212
|
+
start(command, args, false);
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// --- prompts ------------------------------------------------------------------------
|
|
217
|
+
|
|
218
|
+
class AbortError extends Error {}
|
|
219
|
+
|
|
220
|
+
/** Blocking y/N prompt. Only ever used when both ends are a real terminal. */
|
|
221
|
+
function confirmSync(theme, question) {
|
|
222
|
+
process.stdout.write(` ${theme.warning("?")} ${question} ${theme.muted("[y/N]")} `);
|
|
223
|
+
const buffer = Buffer.alloc(1);
|
|
224
|
+
let value = "";
|
|
225
|
+
for (let guard = 0; guard < 60000; guard += 1) {
|
|
226
|
+
let n = 0;
|
|
227
|
+
try { n = fs.readSync(0, buffer, 0, 1, null); }
|
|
228
|
+
catch (error) {
|
|
229
|
+
if (error.code === "EAGAIN") { sleepSync(10); continue; }
|
|
230
|
+
if (error.code === "EOF") break;
|
|
231
|
+
throw error;
|
|
232
|
+
}
|
|
233
|
+
if (n === 0) break;
|
|
234
|
+
const ch = buffer.toString("utf8");
|
|
235
|
+
if (ch === "\n" || ch === "\x04") break;
|
|
236
|
+
if (ch === "\x03") throw new AbortError();
|
|
237
|
+
if (ch !== "\r") value += ch;
|
|
238
|
+
}
|
|
239
|
+
return /^(y|yes)$/i.test(value.trim());
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// --- injectable environment ---------------------------------------------------------
|
|
243
|
+
|
|
244
|
+
/** Everything with a side effect goes through here so the tests can script it. */
|
|
245
|
+
function defaultDeps() {
|
|
246
|
+
return {
|
|
247
|
+
env: process.env,
|
|
248
|
+
platform: process.platform,
|
|
249
|
+
home: os.homedir(),
|
|
250
|
+
stdout: process.stdout,
|
|
251
|
+
isFile: (p) => { try { return fs.statSync(p).isFile(); } catch { return false; } },
|
|
252
|
+
isDir: (p) => { try { return fs.statSync(p).isDirectory(); } catch { return false; } },
|
|
253
|
+
interactive: () => Boolean(process.stdin.isTTY && process.stdout.isTTY),
|
|
254
|
+
confirm: confirmSync,
|
|
255
|
+
runCapture,
|
|
256
|
+
runInherit,
|
|
257
|
+
exec: execInherit,
|
|
258
|
+
launcherVersion: () => {
|
|
259
|
+
try { return require(path.join(__dirname, "..", "package.json")).version; } catch { return "0.0.0"; }
|
|
260
|
+
},
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
const say = (deps, line = "") => deps.stdout.write(`${line}\n`);
|
|
265
|
+
// Path rules follow the platform being *described*, so the tests can script win32 on a mac.
|
|
266
|
+
const pathFor = (deps) => (deps.platform === "win32" ? path.win32 : path.posix);
|
|
267
|
+
|
|
268
|
+
// --- uv -----------------------------------------------------------------------------
|
|
269
|
+
|
|
270
|
+
function findOnPath(name, deps) {
|
|
271
|
+
const exts = deps.platform === "win32" ? (deps.env.PATHEXT || ".EXE;.CMD;.BAT;.COM").split(";") : [""];
|
|
272
|
+
const P = pathFor(deps);
|
|
273
|
+
for (const raw of (deps.env.PATH || "").split(P.delimiter)) {
|
|
274
|
+
const dir = raw.replace(/^"|"$/g, "").trim();
|
|
275
|
+
if (!dir) continue;
|
|
276
|
+
for (const ext of exts) {
|
|
277
|
+
const candidate = P.join(dir, name + ext);
|
|
278
|
+
if (deps.isFile(candidate)) return candidate;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
return null;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// Where the official installer (and Homebrew/winget) put uv when it is not on PATH yet.
|
|
285
|
+
function uvInstallDirs(deps) {
|
|
286
|
+
const P = pathFor(deps);
|
|
287
|
+
const dirs = [P.join(deps.home, ".local", "bin"), P.join(deps.home, ".cargo", "bin")];
|
|
288
|
+
if (deps.platform === "win32") {
|
|
289
|
+
const local = deps.env.LOCALAPPDATA || P.join(deps.home, "AppData", "Local");
|
|
290
|
+
dirs.push(P.join(local, "Microsoft", "WinGet", "Links"));
|
|
291
|
+
} else {
|
|
292
|
+
dirs.push("/opt/homebrew/bin", "/usr/local/bin");
|
|
293
|
+
}
|
|
294
|
+
return dirs;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
function findUv(deps) {
|
|
298
|
+
const explicit = deps.env.OPENREYNOLDS_UV;
|
|
299
|
+
if (explicit) {
|
|
300
|
+
if (deps.isFile(explicit)) return explicit;
|
|
301
|
+
say(deps, ` openreynolds: OPENREYNOLDS_UV=${explicit} is not a file; ignoring it.`);
|
|
302
|
+
}
|
|
303
|
+
const onPath = findOnPath("uv", deps);
|
|
304
|
+
if (onPath) return onPath;
|
|
305
|
+
const names = deps.platform === "win32" ? ["uv.exe", "uv"] : ["uv"];
|
|
306
|
+
for (const dir of uvInstallDirs(deps)) {
|
|
307
|
+
for (const name of names) {
|
|
308
|
+
const candidate = pathFor(deps).join(dir, name);
|
|
309
|
+
if (deps.isFile(candidate)) return candidate;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
return null;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function uvMissingPanel(theme, platform) {
|
|
316
|
+
return panel(theme, [
|
|
317
|
+
theme.heading("uv is required and was not found."),
|
|
318
|
+
"",
|
|
319
|
+
"openreynolds is a Python program; uv installs it (and a Python 3.10+ if needed).",
|
|
320
|
+
"Install uv with:",
|
|
321
|
+
theme.brand(uvInstallCommand(platform)),
|
|
322
|
+
"",
|
|
323
|
+
theme.muted(`Then re-run \`openreynolds\`. Non-interactive? Pass --yes or set OPENREYNOLDS_YES=1`),
|
|
324
|
+
theme.muted(`to let the launcher run that installer for you. Docs: ${UV_DOCS}`),
|
|
325
|
+
], { title: "missing dependency", color: ERR });
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
function installUv(theme, deps) {
|
|
329
|
+
say(deps, ` ${theme.glyphs.arrow} Installing uv with the official installer${theme.glyphs.ellipsis}`);
|
|
330
|
+
say(deps, ` ${theme.muted(`$ ${uvInstallCommand(deps.platform)}`)}`);
|
|
331
|
+
say(deps);
|
|
332
|
+
const result = deps.platform === "win32"
|
|
333
|
+
? deps.runInherit("powershell", ["-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-c", "irm https://astral.sh/uv/install.ps1 | iex"])
|
|
334
|
+
: deps.runInherit("/bin/sh", ["-c", "curl -LsSf https://astral.sh/uv/install.sh | sh"]);
|
|
335
|
+
say(deps);
|
|
336
|
+
if (result.error || result.status !== 0) return null;
|
|
337
|
+
// The installer edits the *persistent* PATH; this process still has the old one.
|
|
338
|
+
const { delimiter } = pathFor(deps);
|
|
339
|
+
for (const dir of uvInstallDirs(deps).slice(0, 2).reverse()) {
|
|
340
|
+
if (!(deps.env.PATH || "").split(delimiter).includes(dir)) deps.env.PATH = `${dir}${delimiter}${deps.env.PATH || ""}`;
|
|
341
|
+
}
|
|
342
|
+
return findUv(deps);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/** Returns the uv path, or an exit code when the launcher cannot go on. */
|
|
346
|
+
function ensureUv(theme, deps, { yes }) {
|
|
347
|
+
let uv = findUv(deps);
|
|
348
|
+
if (uv) return { uv };
|
|
349
|
+
const g = theme.glyphs;
|
|
350
|
+
say(deps, ` ${theme.bad(g.bad)} uv not found ${theme.muted("(PATH, ~/.local/bin, ~/.cargo/bin)")}`);
|
|
351
|
+
let agreed = yes;
|
|
352
|
+
if (!agreed && deps.interactive()) {
|
|
353
|
+
say(deps);
|
|
354
|
+
say(deps, ` ${theme.heading("openreynolds needs uv to install and run its Python package.")}`);
|
|
355
|
+
say(deps, ` ${theme.muted(uvInstallCommand(deps.platform))}`);
|
|
356
|
+
say(deps);
|
|
357
|
+
agreed = deps.confirm(theme, "Run the official uv installer now?");
|
|
358
|
+
}
|
|
359
|
+
if (!agreed) {
|
|
360
|
+
say(deps);
|
|
361
|
+
say(deps, uvMissingPanel(theme, deps.platform));
|
|
362
|
+
return { exit: 2 };
|
|
363
|
+
}
|
|
364
|
+
uv = installUv(theme, deps);
|
|
365
|
+
if (!uv) {
|
|
366
|
+
say(deps, ` ${theme.bad(g.bad)} the installer did not leave a usable uv on this machine`);
|
|
367
|
+
say(deps, uvMissingPanel(theme, deps.platform));
|
|
368
|
+
return { exit: 1 };
|
|
369
|
+
}
|
|
370
|
+
// A binary written seconds ago can be briefly unlaunchable while antivirus scans it
|
|
371
|
+
// or a cloud-sync filter materializes it; a short retry turns that into a non-event.
|
|
372
|
+
let probe = deps.runCapture(uv, ["--version"], { env: deps.env, timeout: 15000 });
|
|
373
|
+
for (let attempt = 1; attempt < 3 && (probe.error || probe.status !== 0); attempt += 1) {
|
|
374
|
+
sleepSync(400 * attempt);
|
|
375
|
+
probe = deps.runCapture(uv, ["--version"], { env: deps.env, timeout: 15000 });
|
|
376
|
+
}
|
|
377
|
+
if (probe.error || probe.status !== 0) {
|
|
378
|
+
say(deps, panel(theme, [
|
|
379
|
+
theme.heading("uv is installed but this process cannot start it."),
|
|
380
|
+
theme.muted(` path: ${uv}`),
|
|
381
|
+
theme.muted(` error: ${probe.error ? probe.error.code || probe.error.message : `exit ${probe.status}`}`),
|
|
382
|
+
"",
|
|
383
|
+
theme.muted("Open a NEW terminal and run openreynolds again -- the installer edits PATH and a"),
|
|
384
|
+
theme.muted("brand-new binary can stay locked briefly while antivirus scans it. If that fails,"),
|
|
385
|
+
theme.muted("verify by hand:"),
|
|
386
|
+
theme.brand(` "${uv}" --version`),
|
|
387
|
+
], { title: "uv not runnable", color: ERR }));
|
|
388
|
+
return { exit: 1 };
|
|
389
|
+
}
|
|
390
|
+
say(deps, ` ${theme.good(g.ok)} uv ${theme.muted(`${probe.stdout.trim()} ${uv}`)}`);
|
|
391
|
+
return { uv };
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
// --- Python package -----------------------------------------------------------------
|
|
395
|
+
|
|
396
|
+
/** `uv tool list` prints one `name vX.Y.Z` header per tool, entry points indented below. */
|
|
397
|
+
function isToolInstalled(listOutput, name = PYTHON_PACKAGE) {
|
|
398
|
+
const pattern = new RegExp(`^${name.replace(/[-_]/g, "[-_]")}\\s+v\\S+`, "im");
|
|
399
|
+
return pattern.test(String(listOutput || ""));
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
function resolveSpec(env) {
|
|
403
|
+
const override = String(env.OPENREYNOLDS_PYTHON_SPEC || "").trim();
|
|
404
|
+
return override || PYTHON_PACKAGE_SPEC;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/** Install the package if `uv tool list` does not show it. Returns an exit code or 0. */
|
|
408
|
+
function ensurePackage(theme, deps, uv, { spec, fromPath, upgrade }) {
|
|
409
|
+
const g = theme.glyphs;
|
|
410
|
+
let args;
|
|
411
|
+
if (fromPath) {
|
|
412
|
+
const dir = path.resolve(fromPath);
|
|
413
|
+
if (!deps.isDir(dir)) {
|
|
414
|
+
say(deps, ` ${theme.bad(g.bad)} --from-path ${dir} is not a directory`);
|
|
415
|
+
return 1;
|
|
416
|
+
}
|
|
417
|
+
say(deps, ` ${g.arrow} Installing the openreynolds Python package from ${dir} (editable) with uv${g.ellipsis}`);
|
|
418
|
+
args = ["tool", "install", "--editable", dir];
|
|
419
|
+
} else {
|
|
420
|
+
const listed = deps.runCapture(uv, ["tool", "list"], { env: deps.env, timeout: 30000 });
|
|
421
|
+
const installed = isToolInstalled(`${listed.stdout}\n${listed.stderr}`);
|
|
422
|
+
if (installed && !upgrade) return 0;
|
|
423
|
+
// `uv tool upgrade` keeps the requirement recorded at install time, so a launcher
|
|
424
|
+
// whose pin moved to a new minor would never get there; re-installing with the
|
|
425
|
+
// current spec and --upgrade covers both "newer patch" and "new range".
|
|
426
|
+
say(deps, upgrade
|
|
427
|
+
? ` ${g.arrow} Upgrading the openreynolds Python package with uv (${spec})${g.ellipsis}`
|
|
428
|
+
: ` ${g.arrow} Installing the openreynolds Python package with uv (one time)${g.ellipsis}`);
|
|
429
|
+
args = upgrade ? ["tool", "install", "--upgrade", spec] : ["tool", "install", spec];
|
|
430
|
+
}
|
|
431
|
+
say(deps, ` ${theme.muted(`$ uv ${args.map(winQuote).join(" ")}`)}`);
|
|
432
|
+
const result = deps.runInherit(uv, args, { env: deps.env });
|
|
433
|
+
if (result.error || result.status !== 0) {
|
|
434
|
+
say(deps);
|
|
435
|
+
say(deps, panel(theme, [
|
|
436
|
+
theme.heading("uv could not install the openreynolds Python package."),
|
|
437
|
+
theme.muted(result.error ? result.error.message : `uv exited with ${result.status}`),
|
|
438
|
+
"",
|
|
439
|
+
"Try it by hand to see the full output:",
|
|
440
|
+
theme.brand(` uv ${args.map(winQuote).join(" ")}`),
|
|
441
|
+
theme.muted("A Python 3.10+ is fetched by uv automatically; `uv python install 3.12` forces one."),
|
|
442
|
+
theme.muted(`Still stuck? ${REPO}/issues`),
|
|
443
|
+
], { title: "install failed", color: ERR }));
|
|
444
|
+
return 1;
|
|
445
|
+
}
|
|
446
|
+
say(deps, ` ${theme.good(g.ok)} ${upgrade ? "upgraded" : "installed"} ${theme.muted(`(uv tool ${PYTHON_PACKAGE})`)}`);
|
|
447
|
+
return 0;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
/** The binary uv just installed. `uv tool dir --bin` honors UV_TOOL_BIN_DIR for us. */
|
|
451
|
+
function findToolBinary(deps, uv) {
|
|
452
|
+
const P = pathFor(deps);
|
|
453
|
+
const name = deps.platform === "win32" ? `${PYTHON_PACKAGE}.exe` : PYTHON_PACKAGE;
|
|
454
|
+
const dirs = [];
|
|
455
|
+
const asked = deps.runCapture(uv, ["tool", "dir", "--bin"], { env: deps.env, timeout: 15000 });
|
|
456
|
+
if (asked.status === 0 && asked.stdout.trim()) dirs.push(asked.stdout.trim());
|
|
457
|
+
dirs.push(P.join(deps.home, ".local", "bin"));
|
|
458
|
+
for (const dir of dirs) {
|
|
459
|
+
const candidate = P.join(dir, name);
|
|
460
|
+
if (deps.isFile(candidate)) return candidate;
|
|
461
|
+
}
|
|
462
|
+
return null;
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
// --- entry point --------------------------------------------------------------------
|
|
466
|
+
|
|
467
|
+
/** Launcher-owned argv; everything else is forwarded verbatim to the Python CLI. */
|
|
468
|
+
function parseArgs(argv) {
|
|
469
|
+
const out = { yes: false, fromPath: null, launcherVersion: false, upgrade: false, passthrough: [] };
|
|
470
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
471
|
+
const arg = argv[i];
|
|
472
|
+
if (arg === "--launcher-version") out.launcherVersion = true;
|
|
473
|
+
else if (arg === "--yes" && out.passthrough.length === 0) out.yes = true;
|
|
474
|
+
else if (arg === "--from-path" && i + 1 < argv.length) out.fromPath = argv[++i];
|
|
475
|
+
else if (arg.startsWith("--from-path=")) out.fromPath = arg.slice("--from-path=".length);
|
|
476
|
+
else if (arg === "upgrade" && out.passthrough.length === 0) out.upgrade = true;
|
|
477
|
+
else out.passthrough.push(arg);
|
|
478
|
+
}
|
|
479
|
+
return out;
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
async function main(argv, deps = defaultDeps()) {
|
|
483
|
+
const env = argv.includes("--no-color") ? { ...deps.env, NO_COLOR: "1" } : deps.env;
|
|
484
|
+
const theme = makeTheme(deps.stdout, env, deps.platform);
|
|
485
|
+
const args = parseArgs(argv);
|
|
486
|
+
|
|
487
|
+
if (args.launcherVersion) {
|
|
488
|
+
say(deps, `${theme.brand("openreynolds")} launcher ${deps.launcherVersion()} ${theme.muted(`(python spec ${resolveSpec(deps.env)}; node ${process.versions.node} ${deps.platform}-${process.arch})`)}`);
|
|
489
|
+
return 0;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
const yes = args.yes || /^(1|true|yes)$/i.test(String(deps.env.OPENREYNOLDS_YES || ""));
|
|
493
|
+
const found = ensureUv(theme, deps, { yes });
|
|
494
|
+
if (!found.uv) return found.exit;
|
|
495
|
+
|
|
496
|
+
const installed = ensurePackage(theme, deps, found.uv, {
|
|
497
|
+
spec: resolveSpec(deps.env), fromPath: args.fromPath, upgrade: args.upgrade,
|
|
498
|
+
});
|
|
499
|
+
if (installed !== 0) return installed;
|
|
500
|
+
if (args.upgrade) return 0;
|
|
501
|
+
|
|
502
|
+
const binary = findToolBinary(deps, found.uv);
|
|
503
|
+
if (!binary) {
|
|
504
|
+
say(deps, panel(theme, [
|
|
505
|
+
theme.heading("The openreynolds Python package is installed but its binary was not found."),
|
|
506
|
+
theme.muted("Ask uv where it puts tool binaries and check that directory:"),
|
|
507
|
+
theme.brand(" uv tool dir --bin"),
|
|
508
|
+
theme.muted(`Report it at ${REPO}/issues if it is really missing.`),
|
|
509
|
+
], { title: "cannot start", color: ERR }));
|
|
510
|
+
return 1;
|
|
511
|
+
}
|
|
512
|
+
// Prepend uv's tool bin dir so the child's own shell-outs resolve even when the
|
|
513
|
+
// user's PATH never picked up uv's edits.
|
|
514
|
+
const P = pathFor(deps);
|
|
515
|
+
const childEnv = { ...deps.env, PATH: `${P.dirname(binary)}${P.delimiter}${deps.env.PATH || ""}` };
|
|
516
|
+
return deps.exec(binary, args.passthrough, { env: childEnv });
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
module.exports = {
|
|
520
|
+
PYTHON_PACKAGE, PYTHON_PACKAGE_SPEC, uvInstallCommand, AbortError,
|
|
521
|
+
makeTheme, panel, parseArgs, findOnPath, findUv, uvInstallDirs, isToolInstalled,
|
|
522
|
+
resolveSpec, ensureUv, ensurePackage, findToolBinary, defaultDeps, main,
|
|
523
|
+
};
|
|
524
|
+
|
|
525
|
+
if (require.main === module) {
|
|
526
|
+
process.stdout.on("error", (error) => { if (error && error.code === "EPIPE") process.exit(0); });
|
|
527
|
+
main(process.argv.slice(2)).then(
|
|
528
|
+
(status) => process.exit(status),
|
|
529
|
+
(error) => {
|
|
530
|
+
if (error instanceof AbortError) { process.stdout.write("\n"); process.exit(130); }
|
|
531
|
+
const theme = makeTheme(process.stdout, process.env, process.platform);
|
|
532
|
+
process.stdout.write(`\n${panel(theme, [theme.heading("openreynolds launcher failed."), theme.muted(String(error && error.message ? error.message : error))], { title: "unexpected error", color: ERR })}\n`);
|
|
533
|
+
process.exit(1);
|
|
534
|
+
},
|
|
535
|
+
);
|
|
536
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "openreynolds",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Agentic OpenFOAM CFD: describe a flow problem, get an evidence-backed answer. Launcher for the openreynolds Python package.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "InviscidAI <kabir@inviscidai.com>",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/InviscidAI/OpenReynolds",
|
|
10
|
+
"directory": "launcher"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://tryreynolds.com",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/InviscidAI/OpenReynolds/issues"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"cfd",
|
|
18
|
+
"openfoam",
|
|
19
|
+
"agent",
|
|
20
|
+
"llm",
|
|
21
|
+
"simulation",
|
|
22
|
+
"fluid-dynamics",
|
|
23
|
+
"cli"
|
|
24
|
+
],
|
|
25
|
+
"bin": {
|
|
26
|
+
"openreynolds": "bin/openreynolds.js"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"bin/",
|
|
30
|
+
"README.md"
|
|
31
|
+
],
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">=18"
|
|
34
|
+
},
|
|
35
|
+
"publishConfig": {
|
|
36
|
+
"access": "public"
|
|
37
|
+
},
|
|
38
|
+
"scripts": {
|
|
39
|
+
"test": "node --test"
|
|
40
|
+
}
|
|
41
|
+
}
|