@mettlelabs/mettle 0.0.0-beta.2 → 0.0.0-beta.5

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 mettle
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/bin/mettle.exe ADDED
@@ -0,0 +1,5 @@
1
+ echo "Error: @mettlelabs/mettle's postinstall script was not run." >&2
2
+ echo "" >&2
3
+ echo "This occurs when installation scripts are disabled." >&2
4
+ echo "Run the package postinstall script or reinstall with scripts enabled." >&2
5
+ exit 1
package/package.json CHANGED
@@ -1,29 +1,38 @@
1
1
  {
2
2
  "name": "@mettlelabs/mettle",
3
- "version": "0.0.0-beta.2",
4
- "license": "UNLICENSED",
5
- "type": "module",
3
+ "version": "0.0.0-beta.5",
4
+ "license": "MIT",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/prolabsinc/mettle-cli.git"
8
+ },
6
9
  "bin": {
7
- "mettle": "bin/mettle.js"
10
+ "mettle": "./bin/mettle.exe"
8
11
  },
9
12
  "scripts": {
10
- "postinstall": "node bin/postinstall.js"
13
+ "postinstall": "node ./postinstall.mjs"
11
14
  },
12
- "files": [
13
- "bin"
15
+ "os": [
16
+ "darwin",
17
+ "linux",
18
+ "win32"
19
+ ],
20
+ "cpu": [
21
+ "arm64",
22
+ "x64"
14
23
  ],
15
- "engines": {
16
- "node": "24.x"
17
- },
18
- "repository": {
19
- "type": "git",
20
- "url": "git+https://github.com/prolabsinc/mettle-cli.git"
21
- },
22
24
  "optionalDependencies": {
23
- "@mettlelabs/mettle-darwin-arm64": "npm:@mettlelabs/mettle@0.0.0-beta.2-darwin-arm64",
24
- "@mettlelabs/mettle-darwin-x64": "npm:@mettlelabs/mettle@0.0.0-beta.2-darwin-x64",
25
- "@mettlelabs/mettle-linux-x64": "npm:@mettlelabs/mettle@0.0.0-beta.2-linux-x64",
26
- "@mettlelabs/mettle-linux-arm64": "npm:@mettlelabs/mettle@0.0.0-beta.2-linux-arm64",
27
- "@mettlelabs/mettle-win32-x64": "npm:@mettlelabs/mettle@0.0.0-beta.2-win32-x64"
25
+ "@mettlelabs/mettle-darwin-arm64": "npm:@mettlelabs/mettle@0.0.0-0.beta.5.darwin-arm64",
26
+ "@mettlelabs/mettle-darwin-x64": "npm:@mettlelabs/mettle@0.0.0-0.beta.5.darwin-x64",
27
+ "@mettlelabs/mettle-darwin-x64-baseline": "npm:@mettlelabs/mettle@0.0.0-0.beta.5.darwin-x64-baseline",
28
+ "@mettlelabs/mettle-linux-arm64": "npm:@mettlelabs/mettle@0.0.0-0.beta.5.linux-arm64",
29
+ "@mettlelabs/mettle-linux-arm64-musl": "npm:@mettlelabs/mettle@0.0.0-0.beta.5.linux-arm64-musl",
30
+ "@mettlelabs/mettle-linux-x64": "npm:@mettlelabs/mettle@0.0.0-0.beta.5.linux-x64",
31
+ "@mettlelabs/mettle-linux-x64-baseline": "npm:@mettlelabs/mettle@0.0.0-0.beta.5.linux-x64-baseline",
32
+ "@mettlelabs/mettle-linux-x64-baseline-musl": "npm:@mettlelabs/mettle@0.0.0-0.beta.5.linux-x64-baseline-musl",
33
+ "@mettlelabs/mettle-linux-x64-musl": "npm:@mettlelabs/mettle@0.0.0-0.beta.5.linux-x64-musl",
34
+ "@mettlelabs/mettle-windows-arm64": "npm:@mettlelabs/mettle@0.0.0-0.beta.5.windows-arm64",
35
+ "@mettlelabs/mettle-windows-x64": "npm:@mettlelabs/mettle@0.0.0-0.beta.5.windows-x64",
36
+ "@mettlelabs/mettle-windows-x64-baseline": "npm:@mettlelabs/mettle@0.0.0-0.beta.5.windows-x64-baseline"
28
37
  }
29
- }
38
+ }
@@ -0,0 +1,183 @@
1
+ #!/usr/bin/env node
2
+
3
+ import childProcess from "node:child_process"
4
+ import fs from "node:fs"
5
+ import { createRequire } from "node:module"
6
+ import os from "node:os"
7
+ import path from "node:path"
8
+ import { fileURLToPath } from "node:url"
9
+
10
+ const directory = path.dirname(fileURLToPath(import.meta.url))
11
+ const require = createRequire(import.meta.url)
12
+ const packageJson = JSON.parse(fs.readFileSync(path.join(directory, "package.json"), "utf8"))
13
+ const command = Object.keys(packageJson.bin ?? {})[0]
14
+ if (!command) throw new Error("mettle package does not declare a binary")
15
+ const sourceCommand = packageJson.mettleSourceBinary ?? command
16
+
17
+ const platform = { darwin: "darwin", linux: "linux", win32: "windows" }[os.platform()] ?? os.platform()
18
+ const arch = { x64: "x64", arm64: "arm64", arm: "arm" }[os.arch()] ?? os.arch()
19
+ const sourceBinary = platform === "windows" ? `${sourceCommand}.exe` : sourceCommand
20
+ const targetBinary = path.resolve(directory, packageJson.bin[command])
21
+ const dependencies = packageJson.optionalDependencies ?? {}
22
+ const base = Object.keys(dependencies).find((name) => name.endsWith(`-${platform}-${arch}`))
23
+ if (!base) throw new Error(`mettle does not provide a binary for ${platform}-${arch}`)
24
+
25
+ function supportsAvx2() {
26
+ if (arch !== "x64") return false
27
+ if (platform === "linux") {
28
+ try {
29
+ return /(^|\s)avx2(\s|$)/i.test(fs.readFileSync("/proc/cpuinfo", "utf8"))
30
+ } catch {
31
+ return false
32
+ }
33
+ }
34
+ if (platform === "darwin") {
35
+ try {
36
+ const result = childProcess.spawnSync("sysctl", ["-n", "hw.optional.avx2_0"], {
37
+ encoding: "utf8",
38
+ timeout: 1500,
39
+ })
40
+ return result.status === 0 && (result.stdout || "").trim() === "1"
41
+ } catch {
42
+ return false
43
+ }
44
+ }
45
+ if (platform === "windows") {
46
+ // PowerShell 7 reads the CPU flag from .NET directly. Windows PowerShell has to compile a
47
+ // kernel32 binding with Add-Type first, which can take several seconds on a cold machine;
48
+ // timing out would quietly install the slower baseline build.
49
+ const intrinsic = "[System.Runtime.Intrinsics.X86.Avx2]::IsSupported"
50
+ const kernel32 =
51
+ '(Add-Type -MemberDefinition "[DllImport(""kernel32.dll"")] public static extern bool IsProcessorFeaturePresent(int ProcessorFeature);" -Name Kernel32 -Namespace Win32 -PassThru)::IsProcessorFeaturePresent(40)'
52
+ for (const [executable, script] of [
53
+ ["pwsh.exe", intrinsic],
54
+ ["pwsh", intrinsic],
55
+ ["powershell.exe", kernel32],
56
+ ["powershell", kernel32],
57
+ ]) {
58
+ try {
59
+ const result = childProcess.spawnSync(executable, ["-NoProfile", "-NonInteractive", "-Command", script], {
60
+ encoding: "utf8",
61
+ timeout: 20_000,
62
+ windowsHide: true,
63
+ })
64
+ if (result.status !== 0) continue
65
+ const output = (result.stdout || "").trim().toLowerCase()
66
+ if (output === "true" || output === "1") return true
67
+ if (output === "false" || output === "0") return false
68
+ } catch {}
69
+ }
70
+ }
71
+ return false
72
+ }
73
+
74
+ function isMusl() {
75
+ if (platform !== "linux") return false
76
+ try {
77
+ if (fs.existsSync("/etc/alpine-release")) return true
78
+ const result = childProcess.spawnSync("ldd", ["--version"], { encoding: "utf8" })
79
+ return `${result.stdout || ""}${result.stderr || ""}`.toLowerCase().includes("musl")
80
+ } catch {
81
+ return false
82
+ }
83
+ }
84
+
85
+ function packageNames() {
86
+ const baseline = arch === "x64" && !supportsAvx2()
87
+ const names =
88
+ platform === "linux"
89
+ ? isMusl()
90
+ ? arch === "x64"
91
+ ? baseline
92
+ ? [`${base}-baseline-musl`, `${base}-musl`, `${base}-baseline`, base]
93
+ : [`${base}-musl`, `${base}-baseline-musl`, base, `${base}-baseline`]
94
+ : [`${base}-musl`, base]
95
+ : arch === "x64"
96
+ ? baseline
97
+ ? [`${base}-baseline`, base, `${base}-baseline-musl`, `${base}-musl`]
98
+ : [base, `${base}-baseline`, `${base}-musl`, `${base}-baseline-musl`]
99
+ : [base, `${base}-musl`]
100
+ : arch === "x64"
101
+ ? baseline
102
+ ? [`${base}-baseline`, base]
103
+ : [base, `${base}-baseline`]
104
+ : [base]
105
+ return names.filter((name) => dependencies[name])
106
+ }
107
+
108
+ function copyBinary(source) {
109
+ if (!fs.existsSync(source)) throw new Error(`Binary not found at ${source}`)
110
+ fs.mkdirSync(path.dirname(targetBinary), { recursive: true })
111
+ if (fs.existsSync(targetBinary)) {
112
+ try {
113
+ fs.unlinkSync(targetBinary)
114
+ } catch {}
115
+ }
116
+ try {
117
+ fs.linkSync(source, targetBinary)
118
+ } catch {
119
+ fs.copyFileSync(source, targetBinary)
120
+ }
121
+ fs.chmodSync(targetBinary, 0o755)
122
+ }
123
+
124
+ function resolveBinary(name) {
125
+ const packagePath = require.resolve(`${name}/package.json`)
126
+ return path.join(path.dirname(packagePath), "bin", sourceBinary)
127
+ }
128
+
129
+ function installPackage(name) {
130
+ const temp = fs.mkdtempSync(path.join(os.tmpdir(), "mettle-install-"))
131
+ try {
132
+ const result = childProcess.spawnSync(
133
+ "npm",
134
+ [
135
+ "install",
136
+ "--ignore-scripts",
137
+ "--no-save",
138
+ "--loglevel=error",
139
+ "--prefix",
140
+ temp,
141
+ `${name}@${dependencies[name]}`,
142
+ ],
143
+ { stdio: "inherit", windowsHide: true },
144
+ )
145
+ if (result.status !== 0) return false
146
+ copyBinary(path.join(temp, "node_modules", name, "bin", sourceBinary))
147
+ return true
148
+ } finally {
149
+ fs.rmSync(temp, { recursive: true, force: true })
150
+ }
151
+ }
152
+
153
+ function verifyBinary() {
154
+ return (
155
+ childProcess.spawnSync(targetBinary, ["--version"], {
156
+ stdio: "ignore",
157
+ windowsHide: true,
158
+ }).status === 0
159
+ )
160
+ }
161
+
162
+ function main() {
163
+ const names = packageNames()
164
+ for (const name of names) {
165
+ try {
166
+ copyBinary(resolveBinary(name))
167
+ if (verifyBinary()) return
168
+ } catch {
169
+ if (installPackage(name) && verifyBinary()) return
170
+ }
171
+ }
172
+
173
+ throw new Error(
174
+ `Failed to install mettle. Try manually installing ${names.map((name) => JSON.stringify(name)).join(" or ")}.`,
175
+ )
176
+ }
177
+
178
+ try {
179
+ main()
180
+ } catch (error) {
181
+ console.error(error instanceof Error ? error.message : String(error))
182
+ process.exit(1)
183
+ }
package/bin/mettle.js DELETED
@@ -1,53 +0,0 @@
1
- #!/usr/bin/env node
2
- // docs/scripts/src/launcher.md
3
-
4
- import { spawn } from "node:child_process";
5
- import { createRequire } from "node:module";
6
- import { constants } from "node:os";
7
- import { dirname, join } from "node:path";
8
-
9
- const require = createRequire(import.meta.url);
10
- const { name, bin, optionalDependencies } = require("../package.json");
11
- const target = `${process.platform}-${process.arch}`;
12
- const [command] = Object.keys(bin);
13
- const executable = process.platform === "win32" ? `${command}.exe` : command;
14
-
15
- let binary;
16
- try {
17
- binary = join(
18
- dirname(require.resolve(`${name}-${target}/package.json`)),
19
- "bin",
20
- executable,
21
- );
22
- } catch {
23
- const supported = Object.keys(optionalDependencies)
24
- .map((alias) => alias.slice(name.length + 1))
25
- .join(", ");
26
- process.stderr.write(
27
- `${name} has no build installed for ${target}. Builds exist for ${supported}. ` +
28
- "If yours is listed, reinstall without --omit=optional or --no-optional.\n",
29
- );
30
- process.exit(1);
31
- }
32
-
33
- const child = spawn(binary, process.argv.slice(2), { stdio: "inherit" });
34
-
35
- const ignored =
36
- process.platform === "win32" ? ["SIGINT"] : ["SIGINT", "SIGHUP", "SIGQUIT"];
37
- for (const signal of ignored) process.on(signal, () => {});
38
- process.on("SIGTERM", () => child.kill("SIGTERM"));
39
-
40
- child.on("error", (error) => {
41
- process.stderr.write(`${binary}: ${error.message}\n`);
42
- process.exit(1);
43
- });
44
-
45
- child.on("exit", (code, signal) => {
46
- if (signal) {
47
- process.removeAllListeners(signal);
48
- process.exitCode = 128 + constants.signals[signal];
49
- process.kill(process.pid, signal);
50
- return;
51
- }
52
- process.exit(code);
53
- });
@@ -1,28 +0,0 @@
1
- // docs/scripts/src/postinstall.md
2
-
3
- import { closeSync, openSync, readFileSync, writeSync } from "node:fs";
4
-
5
- const { engines } = JSON.parse(
6
- readFileSync(new URL("../package.json", import.meta.url), "utf8"),
7
- );
8
- const node = engines.node.replace(/\.x$/, "");
9
-
10
- const next = `
11
- Mettle CLI installed.
12
-
13
- Next:
14
- mettle login save your model provider key
15
- cd your-project the folder you want to investigate
16
- mettle open the chat
17
-
18
- Needs Docker running and Node.js ${node}.
19
-
20
- `;
21
-
22
- if (!process.env.CI) {
23
- try {
24
- const terminal = openSync("/dev/tty", "w");
25
- writeSync(terminal, next);
26
- closeSync(terminal);
27
- } catch {}
28
- }