@rivet-dev/agentos-toolchain 0.0.0-nathan-agentos-package-resolution.efc374f → 0.2.5-rc.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.
@@ -1,256 +0,0 @@
1
- // src/header.ts
2
- var WASM_MAGIC = Buffer.from([0, 97, 115, 109]);
3
- var ELF_MAGIC = Buffer.from([127, 69, 76, 70]);
4
- var MACHO_MAGICS = [
5
- 4277009102,
6
- 3472551422,
7
- 4277009103,
8
- 3489328638,
9
- 3405691582,
10
- 3199925962
11
- ];
12
- function detectExecutableKind(head) {
13
- if (head.length >= 2 && head[0] === 35 && head[1] === 33) {
14
- return "shebang";
15
- }
16
- if (head.length >= 4 && head.subarray(0, 4).equals(WASM_MAGIC)) {
17
- return "wasm";
18
- }
19
- if (head.length >= 4 && head.subarray(0, 4).equals(ELF_MAGIC)) {
20
- return "native-elf";
21
- }
22
- if (head.length >= 4) {
23
- const be = head.readUInt32BE(0);
24
- if (be === 3405691582 && head.length >= 8) {
25
- const next = head.readUInt32BE(4);
26
- if (next >= 45) return "unknown";
27
- }
28
- if (MACHO_MAGICS.includes(be)) return "native-macho";
29
- }
30
- if (head.length >= 2 && head[0] === 77 && head[1] === 90) {
31
- return "native-pe";
32
- }
33
- return "unknown";
34
- }
35
- function isNativeKind(kind) {
36
- return kind === "native-elf" || kind === "native-macho" || kind === "native-pe";
37
- }
38
- function parseShebangInterpreter(head) {
39
- if (!(head.length >= 2 && head[0] === 35 && head[1] === 33)) return null;
40
- const nl = head.indexOf(10);
41
- const line = head.subarray(2, nl < 0 ? head.length : nl).toString("utf8");
42
- const trimmed = line.replace(/^\s+/, "");
43
- const interp = trimmed.split(/\s+/, 1)[0] ?? "";
44
- return interp.length > 0 ? interp : null;
45
- }
46
-
47
- // src/pack.ts
48
- import { execFileSync } from "child_process";
49
- import {
50
- cpSync,
51
- existsSync,
52
- mkdirSync,
53
- mkdtempSync,
54
- openSync,
55
- readFileSync,
56
- readSync,
57
- closeSync,
58
- readdirSync,
59
- rmSync,
60
- statSync,
61
- symlinkSync,
62
- writeFileSync
63
- } from "fs";
64
- import { tmpdir } from "os";
65
- import { join, relative, resolve } from "path";
66
- var HEAD_BYTES = 256;
67
- function readHead(file) {
68
- const fd = openSync(file, "r");
69
- try {
70
- const buf = Buffer.alloc(HEAD_BYTES);
71
- const n = readSync(fd, buf, 0, HEAD_BYTES, 0);
72
- return buf.subarray(0, n);
73
- } finally {
74
- closeSync(fd);
75
- }
76
- }
77
- function npmInstallFlat(source, into) {
78
- mkdirSync(join(into, "node_modules"), { recursive: true });
79
- execFileSync(
80
- "npm",
81
- [
82
- "install",
83
- source,
84
- "--omit=dev",
85
- "--ignore-scripts",
86
- "--no-audit",
87
- "--no-fund",
88
- "--no-package-lock",
89
- "--install-strategy=hoisted",
90
- "--prefix",
91
- into
92
- ],
93
- { stdio: "pipe" }
94
- );
95
- }
96
- function resolveInstallSpec(source, scratch) {
97
- if (!(existsSync(source) && statSync(source).isDirectory())) return source;
98
- const dest = join(scratch, "tarball");
99
- mkdirSync(dest, { recursive: true });
100
- const out = execFileSync(
101
- "npm",
102
- ["pack", resolve(source), "--pack-destination", dest, "--ignore-scripts", "--silent"],
103
- { encoding: "utf8" }
104
- );
105
- const file = out.trim().split("\n").pop()?.trim();
106
- if (!file) throw new Error(`npm pack produced no tarball for ${source}`);
107
- return join(dest, file);
108
- }
109
- function installedPackageName(source) {
110
- if (existsSync(source) && statSync(source).isDirectory()) {
111
- const pkg = JSON.parse(readFileSync(join(source, "package.json"), "utf8"));
112
- return pkg.name;
113
- }
114
- const at = source.lastIndexOf("@");
115
- return at > 0 ? source.slice(0, at) : source;
116
- }
117
- function binEntries(pkgDir) {
118
- const pkg = JSON.parse(readFileSync(join(pkgDir, "package.json"), "utf8"));
119
- const bin = pkg.bin;
120
- if (!bin) return {};
121
- if (typeof bin === "string") {
122
- return { [pkg.name.replace(/^@[^/]+\//, "")]: bin };
123
- }
124
- return bin;
125
- }
126
- function findNativeAddons(root) {
127
- const hits = [];
128
- const walk = (dir) => {
129
- for (const entry of readdirSync(dir, { withFileTypes: true })) {
130
- const p = join(dir, entry.name);
131
- if (entry.isDirectory()) walk(p);
132
- else if (entry.isFile() && entry.name.endsWith(".node")) hits.push(p);
133
- }
134
- };
135
- if (existsSync(root)) walk(root);
136
- return hits;
137
- }
138
- function verifyPackageDir(packageDir) {
139
- const pkgJsonPath = join(packageDir, "package.json");
140
- if (!existsSync(pkgJsonPath)) {
141
- throw new Error(`missing required package.json in ${packageDir}`);
142
- }
143
- let version;
144
- try {
145
- version = JSON.parse(readFileSync(pkgJsonPath, "utf8")).version;
146
- } catch (error) {
147
- throw new Error(
148
- `package.json in ${packageDir} is not valid JSON: ${String(error)}`
149
- );
150
- }
151
- if (typeof version !== "string" || version.length === 0) {
152
- throw new Error(`package.json in ${packageDir} is missing a valid "version"`);
153
- }
154
- const binDir = join(packageDir, "bin");
155
- if (!existsSync(binDir)) {
156
- throw new Error(`package has no bin/ directory: ${packageDir}`);
157
- }
158
- for (const entry of readdirSync(binDir)) {
159
- const target = resolve(binDir, entry);
160
- const kind = detectExecutableKind(readHead(target));
161
- if (isNativeKind(kind)) {
162
- throw new Error(
163
- `bin/${entry} is a native ${kind} binary, which cannot run in agentOS`
164
- );
165
- }
166
- if (kind === "unknown") {
167
- throw new Error(
168
- `bin/${entry} has no recognized header \u2014 JS/script commands need a '#!' shebang`
169
- );
170
- }
171
- }
172
- const addons = findNativeAddons(join(packageDir, "node_modules"));
173
- if (addons.length > 0) {
174
- throw new Error(
175
- `package contains native .node addon(s) that won't load in V8: ${addons.map((a) => relative(packageDir, a)).join(", ")}; re-run with --prune-native to drop them if they are unreachable on the V8 code path`
176
- );
177
- }
178
- }
179
- function resolveBinTarget(closureModules, name, entryRel) {
180
- const nested = join(closureModules, name, entryRel);
181
- if (existsSync(nested)) return nested;
182
- const hoistedMatch = entryRel.match(/^\.?\/?node_modules\/(.+)$/);
183
- if (hoistedMatch) {
184
- const hoisted = join(closureModules, hoistedMatch[1]);
185
- if (existsSync(hoisted)) return hoisted;
186
- }
187
- throw new Error(
188
- `pack: cannot resolve bin target "${entryRel}" for ${name} \u2014 not found nested (${nested}) or hoisted. The declaring package's bin path does not match the installed (hoisted) layout.`
189
- );
190
- }
191
- function pack(options) {
192
- const { source, out, agent, pruneNative } = options;
193
- const tmp = mkdtempSync(join(tmpdir(), "agentos-pack-"));
194
- try {
195
- npmInstallFlat(resolveInstallSpec(source, tmp), tmp);
196
- const name = installedPackageName(source);
197
- const installedDir = join(tmp, "node_modules", name);
198
- const pkg = JSON.parse(readFileSync(join(installedDir, "package.json"), "utf8"));
199
- const version = pkg.version;
200
- const bins = binEntries(installedDir);
201
- const commands = Object.keys(bins);
202
- if (commands.length === 0) {
203
- throw new Error(`package "${name}" declares no bin commands`);
204
- }
205
- if (agent && !commands.includes(agent)) {
206
- throw new Error(
207
- `--agent "${agent}" is not one of the package's commands: ${commands.join(", ")}`
208
- );
209
- }
210
- const packageDir = out;
211
- rmSync(packageDir, { recursive: true, force: true });
212
- mkdirSync(packageDir, { recursive: true });
213
- const binDir = join(packageDir, "bin");
214
- mkdirSync(binDir, { recursive: true });
215
- cpSync(join(tmp, "node_modules"), join(packageDir, "node_modules"), {
216
- recursive: true
217
- });
218
- const closureModules = join(packageDir, "node_modules");
219
- for (const [cmd, entryRel] of Object.entries(bins)) {
220
- const targetAbs = resolveBinTarget(closureModules, name, entryRel);
221
- symlinkSync(relative(binDir, targetAbs), join(binDir, cmd));
222
- }
223
- if (pruneNative) {
224
- const addons = findNativeAddons(join(packageDir, "node_modules"));
225
- for (const addon of addons) {
226
- rmSync(addon, { force: true });
227
- }
228
- if (addons.length > 0) {
229
- console.warn(
230
- `[agentos-toolchain] pruned ${addons.length} native .node addon(s) from ${name} (--prune-native); they must be unreachable on the V8 code path`
231
- );
232
- }
233
- }
234
- const binMap = {};
235
- for (const cmd of commands) {
236
- binMap[cmd] = `bin/${cmd}`;
237
- }
238
- writeFileSync(
239
- join(packageDir, "package.json"),
240
- `${JSON.stringify({ name, version, bin: binMap }, null, 2)}
241
- `
242
- );
243
- verifyPackageDir(packageDir);
244
- return { name, version, packageDir, commands };
245
- } finally {
246
- rmSync(tmp, { recursive: true, force: true });
247
- }
248
- }
249
-
250
- export {
251
- detectExecutableKind,
252
- isNativeKind,
253
- parseShebangInterpreter,
254
- verifyPackageDir,
255
- pack
256
- };
@@ -1,243 +0,0 @@
1
- // src/header.ts
2
- var WASM_MAGIC = Buffer.from([0, 97, 115, 109]);
3
- var ELF_MAGIC = Buffer.from([127, 69, 76, 70]);
4
- var MACHO_MAGICS = [
5
- 4277009102,
6
- 3472551422,
7
- 4277009103,
8
- 3489328638,
9
- 3405691582,
10
- 3199925962
11
- ];
12
- function detectExecutableKind(head) {
13
- if (head.length >= 2 && head[0] === 35 && head[1] === 33) {
14
- return "shebang";
15
- }
16
- if (head.length >= 4 && head.subarray(0, 4).equals(WASM_MAGIC)) {
17
- return "wasm";
18
- }
19
- if (head.length >= 4 && head.subarray(0, 4).equals(ELF_MAGIC)) {
20
- return "native-elf";
21
- }
22
- if (head.length >= 4) {
23
- const be = head.readUInt32BE(0);
24
- if (be === 3405691582 && head.length >= 8) {
25
- const next = head.readUInt32BE(4);
26
- if (next >= 45) return "unknown";
27
- }
28
- if (MACHO_MAGICS.includes(be)) return "native-macho";
29
- }
30
- if (head.length >= 2 && head[0] === 77 && head[1] === 90) {
31
- return "native-pe";
32
- }
33
- return "unknown";
34
- }
35
- function isNativeKind(kind) {
36
- return kind === "native-elf" || kind === "native-macho" || kind === "native-pe";
37
- }
38
- function parseShebangInterpreter(head) {
39
- if (!(head.length >= 2 && head[0] === 35 && head[1] === 33)) return null;
40
- const nl = head.indexOf(10);
41
- const line = head.subarray(2, nl < 0 ? head.length : nl).toString("utf8");
42
- const trimmed = line.replace(/^\s+/, "");
43
- const interp = trimmed.split(/\s+/, 1)[0] ?? "";
44
- return interp.length > 0 ? interp : null;
45
- }
46
-
47
- // src/pack.ts
48
- import { execFileSync } from "child_process";
49
- import {
50
- cpSync,
51
- existsSync,
52
- mkdirSync,
53
- mkdtempSync,
54
- openSync,
55
- readFileSync,
56
- readSync,
57
- closeSync,
58
- readdirSync,
59
- rmSync,
60
- statSync,
61
- symlinkSync,
62
- writeFileSync
63
- } from "fs";
64
- import { tmpdir } from "os";
65
- import { join, relative, resolve } from "path";
66
- var HEAD_BYTES = 256;
67
- function readHead(file) {
68
- const fd = openSync(file, "r");
69
- try {
70
- const buf = Buffer.alloc(HEAD_BYTES);
71
- const n = readSync(fd, buf, 0, HEAD_BYTES, 0);
72
- return buf.subarray(0, n);
73
- } finally {
74
- closeSync(fd);
75
- }
76
- }
77
- function npmInstallFlat(source, into) {
78
- mkdirSync(join(into, "node_modules"), { recursive: true });
79
- execFileSync(
80
- "npm",
81
- [
82
- "install",
83
- source,
84
- "--omit=dev",
85
- "--ignore-scripts",
86
- "--no-audit",
87
- "--no-fund",
88
- "--no-package-lock",
89
- "--install-strategy=hoisted",
90
- "--prefix",
91
- into
92
- ],
93
- { stdio: "pipe" }
94
- );
95
- }
96
- function resolveInstallSpec(source, scratch) {
97
- if (!(existsSync(source) && statSync(source).isDirectory())) return source;
98
- const dest = join(scratch, "tarball");
99
- mkdirSync(dest, { recursive: true });
100
- const out = execFileSync(
101
- "npm",
102
- ["pack", resolve(source), "--pack-destination", dest, "--ignore-scripts", "--silent"],
103
- { encoding: "utf8" }
104
- );
105
- const file = out.trim().split("\n").pop()?.trim();
106
- if (!file) throw new Error(`npm pack produced no tarball for ${source}`);
107
- return join(dest, file);
108
- }
109
- function installedPackageName(source) {
110
- if (existsSync(source) && statSync(source).isDirectory()) {
111
- const pkg = JSON.parse(readFileSync(join(source, "package.json"), "utf8"));
112
- return pkg.name;
113
- }
114
- const at = source.lastIndexOf("@");
115
- return at > 0 ? source.slice(0, at) : source;
116
- }
117
- function binEntries(pkgDir) {
118
- const pkg = JSON.parse(readFileSync(join(pkgDir, "package.json"), "utf8"));
119
- const bin = pkg.bin;
120
- if (!bin) return {};
121
- if (typeof bin === "string") {
122
- return { [pkg.name.replace(/^@[^/]+\//, "")]: bin };
123
- }
124
- return bin;
125
- }
126
- function findNativeAddons(root) {
127
- const hits = [];
128
- const walk = (dir) => {
129
- for (const entry of readdirSync(dir, { withFileTypes: true })) {
130
- const p = join(dir, entry.name);
131
- if (entry.isDirectory()) walk(p);
132
- else if (entry.isFile() && entry.name.endsWith(".node")) hits.push(p);
133
- }
134
- };
135
- if (existsSync(root)) walk(root);
136
- return hits;
137
- }
138
- function verifyPackageDir(packageDir) {
139
- const pkgJsonPath = join(packageDir, "package.json");
140
- if (!existsSync(pkgJsonPath)) {
141
- throw new Error(`missing required package.json in ${packageDir}`);
142
- }
143
- let version;
144
- try {
145
- version = JSON.parse(readFileSync(pkgJsonPath, "utf8")).version;
146
- } catch (error) {
147
- throw new Error(
148
- `package.json in ${packageDir} is not valid JSON: ${String(error)}`
149
- );
150
- }
151
- if (typeof version !== "string" || version.length === 0) {
152
- throw new Error(`package.json in ${packageDir} is missing a valid "version"`);
153
- }
154
- const binDir = join(packageDir, "bin");
155
- if (!existsSync(binDir)) {
156
- throw new Error(`package has no bin/ directory: ${packageDir}`);
157
- }
158
- for (const entry of readdirSync(binDir)) {
159
- const target = resolve(binDir, entry);
160
- const kind = detectExecutableKind(readHead(target));
161
- if (isNativeKind(kind)) {
162
- throw new Error(
163
- `bin/${entry} is a native ${kind} binary, which cannot run in agentOS`
164
- );
165
- }
166
- if (kind === "unknown") {
167
- throw new Error(
168
- `bin/${entry} has no recognized header \u2014 JS/script commands need a '#!' shebang`
169
- );
170
- }
171
- }
172
- const addons = findNativeAddons(join(packageDir, "node_modules"));
173
- if (addons.length > 0) {
174
- throw new Error(
175
- `package contains native .node addon(s) that won't load in V8: ${addons.map((a) => relative(packageDir, a)).join(", ")}; re-run with --prune-native to drop them if they are unreachable on the V8 code path`
176
- );
177
- }
178
- }
179
- function pack(options) {
180
- const { source, out, agent, pruneNative } = options;
181
- const tmp = mkdtempSync(join(tmpdir(), "agentos-pack-"));
182
- try {
183
- npmInstallFlat(resolveInstallSpec(source, tmp), tmp);
184
- const name = installedPackageName(source);
185
- const installedDir = join(tmp, "node_modules", name);
186
- const pkg = JSON.parse(readFileSync(join(installedDir, "package.json"), "utf8"));
187
- const version = pkg.version;
188
- const bins = binEntries(installedDir);
189
- const commands = Object.keys(bins);
190
- if (commands.length === 0) {
191
- throw new Error(`package "${name}" declares no bin commands`);
192
- }
193
- if (agent && !commands.includes(agent)) {
194
- throw new Error(
195
- `--agent "${agent}" is not one of the package's commands: ${commands.join(", ")}`
196
- );
197
- }
198
- const packageDir = out;
199
- rmSync(packageDir, { recursive: true, force: true });
200
- mkdirSync(packageDir, { recursive: true });
201
- const binDir = join(packageDir, "bin");
202
- mkdirSync(binDir, { recursive: true });
203
- cpSync(join(tmp, "node_modules"), join(packageDir, "node_modules"), {
204
- recursive: true
205
- });
206
- for (const [cmd, entryRel] of Object.entries(bins)) {
207
- const targetAbs = join(packageDir, "node_modules", name, entryRel);
208
- symlinkSync(relative(binDir, targetAbs), join(binDir, cmd));
209
- }
210
- if (pruneNative) {
211
- const addons = findNativeAddons(join(packageDir, "node_modules"));
212
- for (const addon of addons) {
213
- rmSync(addon, { force: true });
214
- }
215
- if (addons.length > 0) {
216
- console.warn(
217
- `[agentos-toolchain] pruned ${addons.length} native .node addon(s) from ${name} (--prune-native); they must be unreachable on the V8 code path`
218
- );
219
- }
220
- }
221
- const binMap = {};
222
- for (const cmd of commands) {
223
- binMap[cmd] = `bin/${cmd}`;
224
- }
225
- writeFileSync(
226
- join(packageDir, "package.json"),
227
- `${JSON.stringify({ name, version, bin: binMap }, null, 2)}
228
- `
229
- );
230
- verifyPackageDir(packageDir);
231
- return { name, version, packageDir, commands };
232
- } finally {
233
- rmSync(tmp, { recursive: true, force: true });
234
- }
235
- }
236
-
237
- export {
238
- detectExecutableKind,
239
- isNativeKind,
240
- parseShebangInterpreter,
241
- verifyPackageDir,
242
- pack
243
- };