@rivet-dev/agentos-toolchain 0.0.0-nathan-agentos-package-resolution.efc374f
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 +191 -0
- package/README.md +76 -0
- package/dist/chunk-7CUVPD4O.js +272 -0
- package/dist/chunk-CSSRRHDX.js +256 -0
- package/dist/chunk-CTAUCHIN.js +243 -0
- package/dist/chunk-K4RHMOJ7.js +282 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +76 -0
- package/dist/index.d.ts +50 -0
- package/dist/index.js +14 -0
- package/package.json +34 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to the Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding any notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
Copyright 2025 Rivet Gaming, Inc.
|
|
180
|
+
|
|
181
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
182
|
+
you may not use this file except in compliance with the License.
|
|
183
|
+
You may obtain a copy of the License at
|
|
184
|
+
|
|
185
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
186
|
+
|
|
187
|
+
Unless required by applicable law or agreed to in writing, software
|
|
188
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
189
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
190
|
+
See the License for the specific language governing permissions and
|
|
191
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# @rivet-dev/agentos-toolchain
|
|
2
|
+
|
|
3
|
+
Build toolchain for **agentOS packages** — the only sanctioned way to turn an npm
|
|
4
|
+
package or a local script into a valid, self-contained agentOS package.
|
|
5
|
+
|
|
6
|
+
```bash
|
|
7
|
+
npx @rivet-dev/agentos-toolchain pack <npm-pkg | ./local-dir> [options]
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
## Why this lives in secure-exec
|
|
11
|
+
|
|
12
|
+
**Packaging is part of secure-exec's core behavior, so the tool that produces
|
|
13
|
+
packages lives here, next to the things it packages.** secure-exec owns the VM
|
|
14
|
+
runtime that *runs* packages — the kernel, the VFS, the `/opt/agentos` mount, the
|
|
15
|
+
`$PATH` command resolver, and the header/`binfmt` dispatch — and it owns the
|
|
16
|
+
package **definitions** themselves: the generic registry software (`registry/software/*`)
|
|
17
|
+
and the agent adapters (`registry/agent/*`). This toolchain is what builds those
|
|
18
|
+
definitions into the on-disk package format that the runtime resolves. Its
|
|
19
|
+
`header.ts` is the same `binfmt` table the sidecar enforces (`crates/sidecar`), so
|
|
20
|
+
keeping it in this repo keeps the producer and the consumer of the format in one
|
|
21
|
+
place.
|
|
22
|
+
|
|
23
|
+
agent-os (the product layer) only *consumes* finished packages via
|
|
24
|
+
`defineSoftware({ name, dir })`; it does not need to own the builder. The package
|
|
25
|
+
name stays `@rivet-dev/agentos-toolchain` so the documented `npx` entrypoint is
|
|
26
|
+
unchanged.
|
|
27
|
+
|
|
28
|
+
## `pack`
|
|
29
|
+
|
|
30
|
+
Produces `<out>/<name>/<version>/` — a package in the agentOS
|
|
31
|
+
[package format](https://agentos-sdk.dev/docs/architecture/packages-and-command-resolution):
|
|
32
|
+
|
|
33
|
+
The output is a **flat, self-contained package directory** — a plain npm dependency, no
|
|
34
|
+
agentOS-specific manifest and no symlinks:
|
|
35
|
+
|
|
36
|
+
```
|
|
37
|
+
<out>/ # the package dir itself (default ./<input-name>-package)
|
|
38
|
+
├── package.json # name, version, and the "bin" command map
|
|
39
|
+
└── node_modules/ # flat, self-contained dependency closure
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Commands are declared in `package.json` `"bin"` (command → a **real entry file**), so the
|
|
43
|
+
package ships cleanly via npm. The runtime builds the `/opt/agentos/bin` symlinks itself when
|
|
44
|
+
it mounts the package — they are never part of the shipped artifact.
|
|
45
|
+
|
|
46
|
+
### Steps
|
|
47
|
+
|
|
48
|
+
1. **Isolate** — a clean temp dir (no host pnpm/workspace bleed-through).
|
|
49
|
+
2. **Install flat** — `npm install <pkg> --omit=dev` (full closure, hoisted, no scripts).
|
|
50
|
+
3. **Ensure `package.json`** — `name`/`version` from the package plus a `"bin"` map (each entry
|
|
51
|
+
gets a `#!` shebang). No `agentos-package.json` — `package.json` is the only metadata.
|
|
52
|
+
4. **Lay out flat** — `package.json` + `node_modules` written directly into `--out`.
|
|
53
|
+
5. **Verify** — every `bin` entry has a recognized header (`#!` shebang or `\0asm`); **reject**
|
|
54
|
+
native `.node` addons (they can't run in V8) — the error names `--prune-native` as the escape.
|
|
55
|
+
|
|
56
|
+
### Options
|
|
57
|
+
|
|
58
|
+
| Flag | Meaning |
|
|
59
|
+
|---|---|
|
|
60
|
+
| `--agent <command>` | mark a `bin` command as the package's ACP entrypoint |
|
|
61
|
+
| `--out <dir>` | output dir for the package itself (flat; default `./<input-name>-package`) |
|
|
62
|
+
| `--prune-native` | delete unreachable native `.node` addons from the flat closure instead of failing |
|
|
63
|
+
|
|
64
|
+
### Examples
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
# package a local CLI → ./my-tool-package/
|
|
68
|
+
npx @rivet-dev/agentos-toolchain pack ./my-tool
|
|
69
|
+
|
|
70
|
+
# an agent whose SDK closure carries unreachable native addons → ./pi-package/
|
|
71
|
+
npx @rivet-dev/agentos-toolchain pack @agentos-software/pi --agent pi-sdk-acp --prune-native
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
The package is consumed by an agentOS host as `defineSoftware({ name, dir })` — add
|
|
75
|
+
`agent: { acpEntrypoint: "<bin>" }` for an agent. The host projects it under `/opt/agentos`,
|
|
76
|
+
and `createSession(name)` launches the adapter via `/opt/agentos/bin/<bin>`.
|
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
// src/bundle.ts
|
|
2
|
+
import { buildSync } from "esbuild";
|
|
3
|
+
import { chmodSync, readFileSync, writeFileSync } from "fs";
|
|
4
|
+
var SHEBANG = "#!/usr/bin/env node\n";
|
|
5
|
+
function bundleEntry(entryFile, outFile) {
|
|
6
|
+
const source = readFileSync(entryFile, "utf8");
|
|
7
|
+
const withoutShebang = source.startsWith("#!") ? source.slice(source.indexOf("\n") + 1) : source;
|
|
8
|
+
const META_SHIM = "const __agentos_import_meta_url = require('url').pathToFileURL(__filename).href;";
|
|
9
|
+
buildSync({
|
|
10
|
+
stdin: {
|
|
11
|
+
contents: withoutShebang,
|
|
12
|
+
resolveDir: entryFile.slice(0, entryFile.lastIndexOf("/")),
|
|
13
|
+
sourcefile: entryFile,
|
|
14
|
+
loader: "js"
|
|
15
|
+
},
|
|
16
|
+
bundle: true,
|
|
17
|
+
platform: "node",
|
|
18
|
+
format: "cjs",
|
|
19
|
+
target: "node20",
|
|
20
|
+
outfile: outFile,
|
|
21
|
+
banner: { js: `${SHEBANG.trimEnd()}
|
|
22
|
+
${META_SHIM}` },
|
|
23
|
+
define: { "import.meta.url": "__agentos_import_meta_url" },
|
|
24
|
+
logLevel: "silent"
|
|
25
|
+
});
|
|
26
|
+
chmodSync(outFile, 493);
|
|
27
|
+
const out = readFileSync(outFile, "utf8");
|
|
28
|
+
if (!out.startsWith(SHEBANG)) {
|
|
29
|
+
writeFileSync(outFile, `${SHEBANG}${out.replace(/^#![^\n]*\n?/, "")}`);
|
|
30
|
+
chmodSync(outFile, 493);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// src/header.ts
|
|
35
|
+
var WASM_MAGIC = Buffer.from([0, 97, 115, 109]);
|
|
36
|
+
var ELF_MAGIC = Buffer.from([127, 69, 76, 70]);
|
|
37
|
+
var MACHO_MAGICS = [
|
|
38
|
+
4277009102,
|
|
39
|
+
3472551422,
|
|
40
|
+
4277009103,
|
|
41
|
+
3489328638,
|
|
42
|
+
3405691582,
|
|
43
|
+
3199925962
|
|
44
|
+
];
|
|
45
|
+
function detectExecutableKind(head) {
|
|
46
|
+
if (head.length >= 2 && head[0] === 35 && head[1] === 33) {
|
|
47
|
+
return "shebang";
|
|
48
|
+
}
|
|
49
|
+
if (head.length >= 4 && head.subarray(0, 4).equals(WASM_MAGIC)) {
|
|
50
|
+
return "wasm";
|
|
51
|
+
}
|
|
52
|
+
if (head.length >= 4 && head.subarray(0, 4).equals(ELF_MAGIC)) {
|
|
53
|
+
return "native-elf";
|
|
54
|
+
}
|
|
55
|
+
if (head.length >= 4) {
|
|
56
|
+
const be = head.readUInt32BE(0);
|
|
57
|
+
if (be === 3405691582 && head.length >= 8) {
|
|
58
|
+
const next = head.readUInt32BE(4);
|
|
59
|
+
if (next >= 45) return "unknown";
|
|
60
|
+
}
|
|
61
|
+
if (MACHO_MAGICS.includes(be)) return "native-macho";
|
|
62
|
+
}
|
|
63
|
+
if (head.length >= 2 && head[0] === 77 && head[1] === 90) {
|
|
64
|
+
return "native-pe";
|
|
65
|
+
}
|
|
66
|
+
return "unknown";
|
|
67
|
+
}
|
|
68
|
+
function isNativeKind(kind) {
|
|
69
|
+
return kind === "native-elf" || kind === "native-macho" || kind === "native-pe";
|
|
70
|
+
}
|
|
71
|
+
function parseShebangInterpreter(head) {
|
|
72
|
+
if (!(head.length >= 2 && head[0] === 35 && head[1] === 33)) return null;
|
|
73
|
+
const nl = head.indexOf(10);
|
|
74
|
+
const line = head.subarray(2, nl < 0 ? head.length : nl).toString("utf8");
|
|
75
|
+
const trimmed = line.replace(/^\s+/, "");
|
|
76
|
+
const interp = trimmed.split(/\s+/, 1)[0] ?? "";
|
|
77
|
+
return interp.length > 0 ? interp : null;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// src/pack.ts
|
|
81
|
+
import { execFileSync } from "child_process";
|
|
82
|
+
import {
|
|
83
|
+
cpSync,
|
|
84
|
+
existsSync,
|
|
85
|
+
mkdirSync,
|
|
86
|
+
mkdtempSync,
|
|
87
|
+
openSync,
|
|
88
|
+
readFileSync as readFileSync2,
|
|
89
|
+
readSync,
|
|
90
|
+
closeSync,
|
|
91
|
+
readdirSync,
|
|
92
|
+
rmSync,
|
|
93
|
+
statSync,
|
|
94
|
+
symlinkSync,
|
|
95
|
+
writeFileSync as writeFileSync2
|
|
96
|
+
} from "fs";
|
|
97
|
+
import { tmpdir } from "os";
|
|
98
|
+
import { join, relative, resolve } from "path";
|
|
99
|
+
var HEAD_BYTES = 256;
|
|
100
|
+
function readHead(file) {
|
|
101
|
+
const fd = openSync(file, "r");
|
|
102
|
+
try {
|
|
103
|
+
const buf = Buffer.alloc(HEAD_BYTES);
|
|
104
|
+
const n = readSync(fd, buf, 0, HEAD_BYTES, 0);
|
|
105
|
+
return buf.subarray(0, n);
|
|
106
|
+
} finally {
|
|
107
|
+
closeSync(fd);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
function npmInstallFlat(source, into) {
|
|
111
|
+
mkdirSync(join(into, "node_modules"), { recursive: true });
|
|
112
|
+
execFileSync(
|
|
113
|
+
"npm",
|
|
114
|
+
[
|
|
115
|
+
"install",
|
|
116
|
+
source,
|
|
117
|
+
"--omit=dev",
|
|
118
|
+
"--ignore-scripts",
|
|
119
|
+
"--no-audit",
|
|
120
|
+
"--no-fund",
|
|
121
|
+
"--no-package-lock",
|
|
122
|
+
"--install-strategy=hoisted",
|
|
123
|
+
"--prefix",
|
|
124
|
+
into
|
|
125
|
+
],
|
|
126
|
+
{ stdio: "pipe" }
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
function installedPackageName(source) {
|
|
130
|
+
if (existsSync(source) && statSync(source).isDirectory()) {
|
|
131
|
+
const pkg = JSON.parse(readFileSync2(join(source, "package.json"), "utf8"));
|
|
132
|
+
return pkg.name;
|
|
133
|
+
}
|
|
134
|
+
const at = source.lastIndexOf("@");
|
|
135
|
+
return at > 0 ? source.slice(0, at) : source;
|
|
136
|
+
}
|
|
137
|
+
function binEntries(pkgDir) {
|
|
138
|
+
const pkg = JSON.parse(readFileSync2(join(pkgDir, "package.json"), "utf8"));
|
|
139
|
+
const bin = pkg.bin;
|
|
140
|
+
if (!bin) return {};
|
|
141
|
+
if (typeof bin === "string") {
|
|
142
|
+
return { [pkg.name.replace(/^@[^/]+\//, "")]: bin };
|
|
143
|
+
}
|
|
144
|
+
return bin;
|
|
145
|
+
}
|
|
146
|
+
function findNativeAddons(root) {
|
|
147
|
+
const hits = [];
|
|
148
|
+
const walk = (dir) => {
|
|
149
|
+
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
150
|
+
const p = join(dir, entry.name);
|
|
151
|
+
if (entry.isDirectory()) walk(p);
|
|
152
|
+
else if (entry.isFile() && entry.name.endsWith(".node")) hits.push(p);
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
if (existsSync(root)) walk(root);
|
|
156
|
+
return hits;
|
|
157
|
+
}
|
|
158
|
+
function verifyPackageDir(packageDir) {
|
|
159
|
+
const pkgJsonPath = join(packageDir, "package.json");
|
|
160
|
+
if (!existsSync(pkgJsonPath)) {
|
|
161
|
+
throw new Error(`missing required package.json in ${packageDir}`);
|
|
162
|
+
}
|
|
163
|
+
let version;
|
|
164
|
+
try {
|
|
165
|
+
version = JSON.parse(readFileSync2(pkgJsonPath, "utf8")).version;
|
|
166
|
+
} catch (error) {
|
|
167
|
+
throw new Error(
|
|
168
|
+
`package.json in ${packageDir} is not valid JSON: ${String(error)}`
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
if (typeof version !== "string" || version.length === 0) {
|
|
172
|
+
throw new Error(`package.json in ${packageDir} is missing a valid "version"`);
|
|
173
|
+
}
|
|
174
|
+
const binDir = join(packageDir, "bin");
|
|
175
|
+
if (!existsSync(binDir)) {
|
|
176
|
+
throw new Error(`package has no bin/ directory: ${packageDir}`);
|
|
177
|
+
}
|
|
178
|
+
for (const entry of readdirSync(binDir)) {
|
|
179
|
+
const target = resolve(binDir, entry);
|
|
180
|
+
const kind = detectExecutableKind(readHead(target));
|
|
181
|
+
if (isNativeKind(kind)) {
|
|
182
|
+
throw new Error(
|
|
183
|
+
`bin/${entry} is a native ${kind} binary, which cannot run in agentOS`
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
if (kind === "unknown") {
|
|
187
|
+
throw new Error(
|
|
188
|
+
`bin/${entry} has no recognized header \u2014 JS/script commands need a '#!' shebang`
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
const addons = findNativeAddons(join(packageDir, "node_modules"));
|
|
193
|
+
if (addons.length > 0) {
|
|
194
|
+
throw new Error(
|
|
195
|
+
`package contains native .node addons (won't load in V8): ${addons.map((a) => relative(packageDir, a)).join(", ")}`
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
function pack(options) {
|
|
200
|
+
const { source, out, agent, bundle, pruneNative } = options;
|
|
201
|
+
const tmp = mkdtempSync(join(tmpdir(), "agentos-pack-"));
|
|
202
|
+
try {
|
|
203
|
+
const absSource = existsSync(source) && statSync(source).isDirectory() ? resolve(source) : source;
|
|
204
|
+
npmInstallFlat(absSource, tmp);
|
|
205
|
+
const name = installedPackageName(source);
|
|
206
|
+
const installedDir = join(tmp, "node_modules", name);
|
|
207
|
+
const pkg = JSON.parse(readFileSync2(join(installedDir, "package.json"), "utf8"));
|
|
208
|
+
const version = pkg.version;
|
|
209
|
+
const bins = binEntries(installedDir);
|
|
210
|
+
const commands = Object.keys(bins);
|
|
211
|
+
if (commands.length === 0) {
|
|
212
|
+
throw new Error(`package "${name}" declares no bin commands`);
|
|
213
|
+
}
|
|
214
|
+
if (agent && !commands.includes(agent)) {
|
|
215
|
+
throw new Error(
|
|
216
|
+
`--agent "${agent}" is not one of the package's commands: ${commands.join(", ")}`
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
const packageDir = out;
|
|
220
|
+
rmSync(packageDir, { recursive: true, force: true });
|
|
221
|
+
mkdirSync(packageDir, { recursive: true });
|
|
222
|
+
const binDir = join(packageDir, "bin");
|
|
223
|
+
mkdirSync(binDir, { recursive: true });
|
|
224
|
+
if (bundle) {
|
|
225
|
+
for (const [cmd, entryRel] of Object.entries(bins)) {
|
|
226
|
+
const entryAbs = join(installedDir, entryRel);
|
|
227
|
+
bundleEntry(entryAbs, join(binDir, cmd));
|
|
228
|
+
}
|
|
229
|
+
} else {
|
|
230
|
+
cpSync(join(tmp, "node_modules"), join(packageDir, "node_modules"), {
|
|
231
|
+
recursive: true
|
|
232
|
+
});
|
|
233
|
+
for (const [cmd, entryRel] of Object.entries(bins)) {
|
|
234
|
+
const targetAbs = join(packageDir, "node_modules", name, entryRel);
|
|
235
|
+
symlinkSync(relative(binDir, targetAbs), join(binDir, cmd));
|
|
236
|
+
}
|
|
237
|
+
if (pruneNative) {
|
|
238
|
+
const addons = findNativeAddons(join(packageDir, "node_modules"));
|
|
239
|
+
for (const addon of addons) {
|
|
240
|
+
rmSync(addon, { force: true });
|
|
241
|
+
}
|
|
242
|
+
if (addons.length > 0) {
|
|
243
|
+
console.warn(
|
|
244
|
+
`[agentos-toolchain] pruned ${addons.length} native .node addon(s) from ${name} (--prune-native); they must be unreachable on the V8 code path`
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
const binMap = {};
|
|
250
|
+
for (const cmd of commands) {
|
|
251
|
+
binMap[cmd] = `bin/${cmd}`;
|
|
252
|
+
}
|
|
253
|
+
writeFileSync2(
|
|
254
|
+
join(packageDir, "package.json"),
|
|
255
|
+
`${JSON.stringify({ name, version, bin: binMap }, null, 2)}
|
|
256
|
+
`
|
|
257
|
+
);
|
|
258
|
+
verifyPackageDir(packageDir);
|
|
259
|
+
return { name, version, packageDir, commands };
|
|
260
|
+
} finally {
|
|
261
|
+
rmSync(tmp, { recursive: true, force: true });
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
export {
|
|
266
|
+
bundleEntry,
|
|
267
|
+
detectExecutableKind,
|
|
268
|
+
isNativeKind,
|
|
269
|
+
parseShebangInterpreter,
|
|
270
|
+
verifyPackageDir,
|
|
271
|
+
pack
|
|
272
|
+
};
|