orchbee 0.0.0-stage → 0.1.0-beta.10
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 +21 -0
- package/README.md +15 -2
- package/bin/orchbee.mjs +4 -0
- package/lib/launcher.mjs +438 -0
- package/package.json +26 -5
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Orchbee contributors
|
|
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/README.md
CHANGED
|
@@ -1,3 +1,16 @@
|
|
|
1
|
-
#
|
|
1
|
+
# orchbee
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Orchbee is a terminal that runs and orchestrates AI agents. This package installs its command,
|
|
4
|
+
`orchbee`, which starts the terminal interface.
|
|
5
|
+
|
|
6
|
+
```sh
|
|
7
|
+
npm install --global orchbee@next
|
|
8
|
+
orchbee
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
The first `orchbee` installs the program this package carries into your Orchbee folder and then
|
|
12
|
+
starts it; later starts run the installed program directly. A newer version of this package
|
|
13
|
+
installs its program the same way on its first start.
|
|
14
|
+
|
|
15
|
+
To remove Orchbee, run `orchbee uninstall` first, then `npm uninstall --global orchbee`, which
|
|
16
|
+
removes only this package.
|
package/bin/orchbee.mjs
ADDED
package/lib/launcher.mjs
ADDED
|
@@ -0,0 +1,438 @@
|
|
|
1
|
+
// The `orchbee` npm package's launcher. npm carries the signed release directory in the platform
|
|
2
|
+
// sub-package; on each start this finds it, installs it into the per-user store through the carried
|
|
3
|
+
// program's own `install --no-path` when the store has no active version or a lower one, and then runs
|
|
4
|
+
// the store's active program in place of itself. The store's pointer `active.json` alone names the
|
|
5
|
+
// active version. On Windows, where automatic updates run, it follows the relaunch protocol of
|
|
6
|
+
// `apps/orchbee-launcher` with the same rule. Nothing runs at install time.
|
|
7
|
+
//
|
|
8
|
+
// The launcher is plain JavaScript that runs on the person's own Node, so it uses only Node built-ins
|
|
9
|
+
// available since Node 18 and reads nothing beyond its own package, the sub-package, and the pointer.
|
|
10
|
+
import { spawn, spawnSync } from 'node:child_process';
|
|
11
|
+
import { readFileSync, statSync } from 'node:fs';
|
|
12
|
+
import { createRequire } from 'node:module';
|
|
13
|
+
import { constants } from 'node:os';
|
|
14
|
+
import path from 'node:path';
|
|
15
|
+
|
|
16
|
+
// SemVer 2.0.0 without build metadata, at most 64 characters, as the store admits it.
|
|
17
|
+
const identifier = String.raw`(?:0|[1-9]\d*|\d*[A-Za-z-][0-9A-Za-z-]*)`;
|
|
18
|
+
const semanticVersion = new RegExp(
|
|
19
|
+
String.raw`^(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-${identifier}(?:\.${identifier})*)?$`,
|
|
20
|
+
'u',
|
|
21
|
+
);
|
|
22
|
+
const manifestDigest = /^[0-9a-f]{64}$/u;
|
|
23
|
+
// The pointer is a few hundred bytes; anything far larger is not one.
|
|
24
|
+
const maximumPointer = 64 * 1024;
|
|
25
|
+
// A concurrent start holds `install.lock` for one install; the install's own deadline is 600 s.
|
|
26
|
+
const busyWaitMilliseconds = 600_000;
|
|
27
|
+
const busyRetryMilliseconds = 250;
|
|
28
|
+
|
|
29
|
+
// The relaunch protocol, the same as `apps/orchbee-launcher/src/relaunch.rs`: the variable carries the
|
|
30
|
+
// launcher's process id, and a Client uses the statuses below only when that process is its parent.
|
|
31
|
+
const relaunchVariable = 'ORCHBEE_RELAUNCH_V1';
|
|
32
|
+
const relaunchStatus = 75;
|
|
33
|
+
// A Client that could not join its Session; without the protocol it ends as a startup failure.
|
|
34
|
+
const joinFailedStatus = 76;
|
|
35
|
+
const startupFailureStatus = 1;
|
|
36
|
+
// Rust's status for a panic that unwinds out of `main`; no CLI exit code uses it.
|
|
37
|
+
const panicStatus = 101;
|
|
38
|
+
// Error-severity NTSTATUS values end a process that did not choose to end; STATUS_CONTROL_C_EXIT is a
|
|
39
|
+
// console Ctrl+C, Ctrl+Break or close.
|
|
40
|
+
const errorSeverity = 0xc0000000;
|
|
41
|
+
const consoleExitStatus = 0xc000013a;
|
|
42
|
+
const relaunchLimit = 3;
|
|
43
|
+
const relaunchWindowMilliseconds = 10_000;
|
|
44
|
+
// The release-build half of the local runtime's automatic-update switch, `IN_RELEASE_BUILDS` in
|
|
45
|
+
// `crates/local-runtime/src/automatic_updates.rs`: the change that turns one on turns on both.
|
|
46
|
+
const automaticUpdatesInReleaseBuilds = false;
|
|
47
|
+
|
|
48
|
+
const platformNames = { win32: 'Windows', darwin: 'macOS', linux: 'Linux' };
|
|
49
|
+
const architectureNames = { x64: 'x64', arm64: 'ARM64' };
|
|
50
|
+
|
|
51
|
+
const messages = {
|
|
52
|
+
'en-US': {
|
|
53
|
+
unsupported: (here, supported) => `Orchbee does not run on ${here}. It runs on ${supported}.`,
|
|
54
|
+
missing: (name) =>
|
|
55
|
+
`The package ${name} is missing. Reinstall orchbee without omitting optional dependencies.`,
|
|
56
|
+
installing: (version) => `Installing Orchbee ${version}…`,
|
|
57
|
+
installFailed: (version, code) => `Orchbee ${version} was not installed (${code}).`,
|
|
58
|
+
storeMismatch: (installed, expected) =>
|
|
59
|
+
`Orchbee was installed in ${installed}, but this launcher reads ${expected}. Reinstall orchbee.`,
|
|
60
|
+
startFailed: (program, code) => `Orchbee could not start ${program} (${code}).`,
|
|
61
|
+
},
|
|
62
|
+
'zh-CN': {
|
|
63
|
+
unsupported: (here, supported) => `Orchbee 不支持 ${here},支持的平台:${supported}。`,
|
|
64
|
+
missing: (name) => `缺少软件包 ${name}。请重新安装 orchbee,不要省略可选依赖。`,
|
|
65
|
+
installing: (version) => `正在安装 Orchbee ${version}…`,
|
|
66
|
+
installFailed: (version, code) => `Orchbee ${version} 未能安装(${code})。`,
|
|
67
|
+
storeMismatch: (installed, expected) =>
|
|
68
|
+
`Orchbee 安装到了 ${installed},但启动器读取的是 ${expected}。请重新安装 orchbee。`,
|
|
69
|
+
startFailed: (program, code) => `Orchbee 无法启动 ${program}(${code})。`,
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
/** The CLI's locale rule: the first non-empty of LC_ALL, LC_MESSAGES and LANG; `zh…` is zh-CN. */
|
|
74
|
+
export function selectLocale(environment) {
|
|
75
|
+
for (const name of ['LC_ALL', 'LC_MESSAGES', 'LANG']) {
|
|
76
|
+
const value = environment[name];
|
|
77
|
+
if (value === undefined || value === '') continue;
|
|
78
|
+
return value.slice(0, 2).toLowerCase() === 'zh' ? 'zh-CN' : 'en-US';
|
|
79
|
+
}
|
|
80
|
+
return 'en-US';
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Admitted numeric identifiers have no leading zero, so length orders them before digits do, at any
|
|
84
|
+
// size.
|
|
85
|
+
function compareNumeric(left, right) {
|
|
86
|
+
if (left.length !== right.length) return left.length - right.length;
|
|
87
|
+
if (left === right) return 0;
|
|
88
|
+
return left < right ? -1 : 1;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** SemVer 2.0.0 precedence of two admitted versions: negative, zero, or positive. */
|
|
92
|
+
export function compareVersions(left, right) {
|
|
93
|
+
const [leftCore, ...leftRest] = left.split('-');
|
|
94
|
+
const [rightCore, ...rightRest] = right.split('-');
|
|
95
|
+
const leftParts = leftCore.split('.');
|
|
96
|
+
const rightParts = rightCore.split('.');
|
|
97
|
+
for (let index = 0; index < 3; index++) {
|
|
98
|
+
const order = compareNumeric(leftParts[index], rightParts[index]);
|
|
99
|
+
if (order !== 0) return order;
|
|
100
|
+
}
|
|
101
|
+
const leftPre = leftRest.join('-');
|
|
102
|
+
const rightPre = rightRest.join('-');
|
|
103
|
+
if (leftPre === rightPre) return 0;
|
|
104
|
+
// A version without a pre-release part ranks above every pre-release of the same core.
|
|
105
|
+
if (leftPre === '') return 1;
|
|
106
|
+
if (rightPre === '') return -1;
|
|
107
|
+
const leftIds = leftPre.split('.');
|
|
108
|
+
const rightIds = rightPre.split('.');
|
|
109
|
+
for (let index = 0; index < Math.min(leftIds.length, rightIds.length); index++) {
|
|
110
|
+
const a = leftIds[index];
|
|
111
|
+
const b = rightIds[index];
|
|
112
|
+
if (a === b) continue;
|
|
113
|
+
const aNumeric = /^\d+$/u.test(a);
|
|
114
|
+
const bNumeric = /^\d+$/u.test(b);
|
|
115
|
+
if (aNumeric && bNumeric) return compareNumeric(a, b);
|
|
116
|
+
// Numeric identifiers rank below alphanumeric ones; the rest compare in ASCII order.
|
|
117
|
+
if (aNumeric) return -1;
|
|
118
|
+
if (bNumeric) return 1;
|
|
119
|
+
return a < b ? -1 : 1;
|
|
120
|
+
}
|
|
121
|
+
return leftIds.length - rightIds.length;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function validVersion(value) {
|
|
125
|
+
return typeof value === 'string' && value.length <= 64 && semanticVersion.test(value);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function plainObject(value) {
|
|
129
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function closedObject(value, required, optional) {
|
|
133
|
+
if (!plainObject(value)) return false;
|
|
134
|
+
const keys = Object.keys(value);
|
|
135
|
+
return (
|
|
136
|
+
required.every((key) => keys.includes(key)) &&
|
|
137
|
+
keys.every((key) => required.includes(key) || optional.includes(key))
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function validEntry(value) {
|
|
142
|
+
return (
|
|
143
|
+
closedObject(value, ['product', 'manifest'], []) &&
|
|
144
|
+
validVersion(value.product) &&
|
|
145
|
+
typeof value.manifest === 'string' &&
|
|
146
|
+
manifestDigest.test(value.manifest)
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* The active product version a format-1 pointer names, or null for any other bytes: a missing or
|
|
152
|
+
* unreadable pointer means no active version, which an install repairs.
|
|
153
|
+
*/
|
|
154
|
+
export function parsePointer(text) {
|
|
155
|
+
let value;
|
|
156
|
+
try {
|
|
157
|
+
value = JSON.parse(text);
|
|
158
|
+
} catch {
|
|
159
|
+
return null;
|
|
160
|
+
}
|
|
161
|
+
if (
|
|
162
|
+
!closedObject(value, ['version', 'active'], ['previous', 'extensions']) ||
|
|
163
|
+
value.version !== 1 ||
|
|
164
|
+
!validEntry(value.active) ||
|
|
165
|
+
(value.previous !== undefined && !validEntry(value.previous)) ||
|
|
166
|
+
(value.extensions !== undefined && !plainObject(value.extensions))
|
|
167
|
+
)
|
|
168
|
+
return null;
|
|
169
|
+
return value.active.product;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* The store root the carried program installs into, by the rule of `orchbee install`:
|
|
174
|
+
* `ORCHBEE_INSTALL_DIR` in every environment; otherwise on Windows `%LOCALAPPDATA%\Programs\Orchbee`
|
|
175
|
+
* for the release stem and `%LOCALAPPDATA%\Programs\<stem>` for a test stem, and elsewhere
|
|
176
|
+
* `~/.local/share/<stem>`. Null when no absolute root follows, which the install then refuses.
|
|
177
|
+
*/
|
|
178
|
+
export function storeRoot(stem, platform, environment) {
|
|
179
|
+
const paths = platform === 'win32' ? path.win32 : path.posix;
|
|
180
|
+
const configured = environment.ORCHBEE_INSTALL_DIR;
|
|
181
|
+
if (configured !== undefined) return paths.isAbsolute(configured) ? configured : null;
|
|
182
|
+
if (platform === 'win32') {
|
|
183
|
+
const local = environment.LOCALAPPDATA;
|
|
184
|
+
if (!local || !paths.isAbsolute(local)) return null;
|
|
185
|
+
return paths.join(local, 'Programs', stem === 'orchbee' ? 'Orchbee' : stem);
|
|
186
|
+
}
|
|
187
|
+
const home = environment.HOME;
|
|
188
|
+
return home && paths.isAbsolute(home) ? paths.join(home, '.local', 'share', stem) : null;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function readPointer(root) {
|
|
192
|
+
if (root === null) return null;
|
|
193
|
+
const file = path.join(root, 'active.json');
|
|
194
|
+
try {
|
|
195
|
+
if (statSync(file).size > maximumPointer) return null;
|
|
196
|
+
return parsePointer(readFileSync(file, 'utf8'));
|
|
197
|
+
} catch {
|
|
198
|
+
return null;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function samePath(left, right) {
|
|
203
|
+
const a = path.resolve(left);
|
|
204
|
+
const b = path.resolve(right);
|
|
205
|
+
return process.platform === 'win32' ? a.toLowerCase() === b.toLowerCase() : a === b;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function sleep(milliseconds) {
|
|
209
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, milliseconds);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function fail(line, status = 1) {
|
|
213
|
+
process.stderr.write(line + '\n');
|
|
214
|
+
process.exit(status);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Runs the carried program's `install --no-path` and reads its `orchbee.install` result. Its
|
|
219
|
+
* localized diagnostic is kept for a failure the launcher reports, not shown while it waits.
|
|
220
|
+
*/
|
|
221
|
+
function install(program) {
|
|
222
|
+
const result = spawnSync(program, ['install', '--no-path', '--format', 'json'], {
|
|
223
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
224
|
+
encoding: 'utf8',
|
|
225
|
+
maxBuffer: 1024 * 1024,
|
|
226
|
+
});
|
|
227
|
+
let document = null;
|
|
228
|
+
try {
|
|
229
|
+
document = JSON.parse(result.stdout ?? '');
|
|
230
|
+
} catch {
|
|
231
|
+
// Anything but the result document reads as an unknown failure below.
|
|
232
|
+
}
|
|
233
|
+
const outcome =
|
|
234
|
+
document !== null && document.schema === 'orchbee.install' ? document.result : null;
|
|
235
|
+
return {
|
|
236
|
+
status: result.status ?? 1,
|
|
237
|
+
error: outcome?.error ?? (result.error ? (result.error.code ?? 'spawn') : 'unknown'),
|
|
238
|
+
activated: outcome?.activated === true,
|
|
239
|
+
releasePath: typeof outcome?.releasePath === 'string' ? outcome.releasePath : null,
|
|
240
|
+
diagnostic: result.stderr ?? '',
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/** The installed program to run: the active one, after installing the carried version if needed. */
|
|
245
|
+
function resolveProgram({ root, programName, carriedProgram, carriedVersion, text }) {
|
|
246
|
+
const deadline = Date.now() + busyWaitMilliseconds;
|
|
247
|
+
let installed = false;
|
|
248
|
+
let announced = false;
|
|
249
|
+
for (;;) {
|
|
250
|
+
const active = readPointer(root);
|
|
251
|
+
if (active !== null && (installed || compareVersions(carriedVersion, active) <= 0))
|
|
252
|
+
return path.join(root, 'versions', active, programName);
|
|
253
|
+
if (installed) fail(text.installFailed(carriedVersion, 'activation'));
|
|
254
|
+
if (!announced) {
|
|
255
|
+
process.stderr.write(text.installing(carriedVersion) + '\n');
|
|
256
|
+
announced = true;
|
|
257
|
+
}
|
|
258
|
+
const outcome = install(carriedProgram);
|
|
259
|
+
if (outcome.activated && outcome.releasePath !== null) {
|
|
260
|
+
// The program reports where it published; a launcher that reads another root would
|
|
261
|
+
// install again on every start, so the two must agree.
|
|
262
|
+
const expected = root === null ? null : path.join(root, 'versions', carriedVersion);
|
|
263
|
+
if (expected === null || !samePath(outcome.releasePath, expected))
|
|
264
|
+
fail(text.storeMismatch(path.dirname(path.dirname(outcome.releasePath)), root ?? '-'));
|
|
265
|
+
installed = true;
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
268
|
+
// Another start holds `install.lock`: wait for it, then read the pointer again.
|
|
269
|
+
if (outcome.error === 'busy' && Date.now() < deadline) {
|
|
270
|
+
sleep(busyRetryMilliseconds);
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
if (outcome.error === 'cancelled') process.exit(outcome.status);
|
|
274
|
+
process.stderr.write(outcome.diagnostic);
|
|
275
|
+
fail(text.installFailed(carriedVersion, outcome.error), outcome.status || 1);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Whether automatic-update behaviors run for this stem, as the local runtime's switch answers for the
|
|
281
|
+
* same build: always for a development or test stem, and for the release stem once the switch is on.
|
|
282
|
+
*/
|
|
283
|
+
export function automaticUpdatesLive(stem) {
|
|
284
|
+
return (
|
|
285
|
+
stem.startsWith('ob-dev-') || stem.startsWith('ob-test-') || automaticUpdatesInReleaseBuilds
|
|
286
|
+
);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* How the protocol reads a Client's exit `code`, or null when no Client started: `relaunch`, `failed`
|
|
291
|
+
* for a failed start or join, or `normal` for any other end, CLI failures included.
|
|
292
|
+
*/
|
|
293
|
+
export function judgeClientExit(code) {
|
|
294
|
+
if (code === null) return 'failed';
|
|
295
|
+
if (code === relaunchStatus) return 'relaunch';
|
|
296
|
+
if (code === joinFailedStatus || code === panicStatus) return 'failed';
|
|
297
|
+
// Node reports a Windows status unsigned; either sign reads as the same 32-bit value.
|
|
298
|
+
const status = code >>> 0;
|
|
299
|
+
return status >= errorSeverity && status !== consoleExitStatus ? 'failed' : 'normal';
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* The step after a start (`first`, `relaunch`, or `return` to the version a failed relaunch
|
|
304
|
+
* replaced) ended with `code`, given the times of the earlier relaunches: `{ step: 'end', status }`,
|
|
305
|
+
* `{ step: 'unavailable' }`, `{ step: 'relaunch' }`, or `{ step: 'return' }`. A relaunched Client that
|
|
306
|
+
* fails to start or to join returns to the one it replaced; a fourth relaunch inside ten seconds is
|
|
307
|
+
* refused and ends as a startup failure, so no status outside the CLI exit codes reaches the caller.
|
|
308
|
+
*/
|
|
309
|
+
export function nextStart(start, code, relaunches, now) {
|
|
310
|
+
const verdict = judgeClientExit(code);
|
|
311
|
+
if (verdict === 'relaunch') {
|
|
312
|
+
const recent = relaunches.filter((at) => now - at < relaunchWindowMilliseconds).length;
|
|
313
|
+
return recent < relaunchLimit
|
|
314
|
+
? { step: 'relaunch' }
|
|
315
|
+
: { step: 'end', status: startupFailureStatus };
|
|
316
|
+
}
|
|
317
|
+
if (verdict === 'failed' && start === 'relaunch') return { step: 'return' };
|
|
318
|
+
if (code === null) return { step: 'unavailable' };
|
|
319
|
+
return { step: 'end', status: code === joinFailedStatus ? startupFailureStatus : code };
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/** Leaves with a program's exit status, or by the same signal, so the caller sees what it saw. */
|
|
323
|
+
function leave(code, signal) {
|
|
324
|
+
if (code !== null) process.exit(code);
|
|
325
|
+
process.removeAllListeners(signal);
|
|
326
|
+
process.kill(process.pid, signal);
|
|
327
|
+
process.exit(128 + (constants.signals[signal] ?? 0));
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* Runs `program` with this process's arguments, standard handles and environment and leaves with its
|
|
332
|
+
* exit status. Console signals (Ctrl+C, Ctrl+Break, close) and terminal signals reach the program
|
|
333
|
+
* directly, so the launcher only outlives them; a signal sent to the launcher alone is passed on.
|
|
334
|
+
*/
|
|
335
|
+
function run(program, text) {
|
|
336
|
+
const child = spawn(program, process.argv.slice(2), { stdio: 'inherit' });
|
|
337
|
+
for (const signal of forwardedSignals) process.on(signal, () => child.kill(signal));
|
|
338
|
+
child.on('error', (error) => fail(text.startFailed(program, error.code ?? error.message)));
|
|
339
|
+
child.on('exit', leave);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* `run` under the relaunch protocol, on Windows only: each start receives this process's id in the
|
|
344
|
+
* protocol variable, and `nextStart` decides after each one. A relaunch reads the pointer again; a
|
|
345
|
+
* pointer that names no version counts as a relaunched Client that did not start.
|
|
346
|
+
*/
|
|
347
|
+
function runWithRelaunch(program, root, programName, text) {
|
|
348
|
+
const environment = { ...process.env, [relaunchVariable]: String(process.pid) };
|
|
349
|
+
const relaunches = [];
|
|
350
|
+
let replaced = null;
|
|
351
|
+
function decide(current, start, code, error) {
|
|
352
|
+
const now = performance.now();
|
|
353
|
+
const next = nextStart(start, code, relaunches, now);
|
|
354
|
+
if (next.step === 'end') process.exit(next.status);
|
|
355
|
+
if (next.step === 'unavailable')
|
|
356
|
+
fail(text.startFailed(current ?? '-', error?.code ?? error?.message ?? 'ENOENT'));
|
|
357
|
+
if (next.step === 'return') {
|
|
358
|
+
startClient(replaced, 'return');
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
if (relaunches.length === relaunchLimit) relaunches.shift();
|
|
362
|
+
relaunches.push(now);
|
|
363
|
+
replaced = current;
|
|
364
|
+
const active = readPointer(root);
|
|
365
|
+
startClient(
|
|
366
|
+
active === null ? null : path.join(root, 'versions', active, programName),
|
|
367
|
+
'relaunch',
|
|
368
|
+
);
|
|
369
|
+
}
|
|
370
|
+
function startClient(current, start) {
|
|
371
|
+
if (current === null) {
|
|
372
|
+
decide(current, start, null, null);
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
const child = spawn(current, process.argv.slice(2), { stdio: 'inherit', env: environment });
|
|
376
|
+
let settled = false;
|
|
377
|
+
child.on('error', (error) => {
|
|
378
|
+
if (settled) return;
|
|
379
|
+
settled = true;
|
|
380
|
+
decide(current, start, null, error);
|
|
381
|
+
});
|
|
382
|
+
child.on('exit', (code, signal) => {
|
|
383
|
+
if (settled) return;
|
|
384
|
+
settled = true;
|
|
385
|
+
if (code === null) leave(code, signal);
|
|
386
|
+
else decide(current, start, code, null);
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
startClient(program, 'first');
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
// Signals the console or terminal delivers to every process attached to it: the program receives
|
|
393
|
+
// them itself, so the launcher ignores them instead of passing them on a second time.
|
|
394
|
+
const ignoredSignals =
|
|
395
|
+
process.platform === 'win32' ? ['SIGINT', 'SIGBREAK', 'SIGHUP'] : ['SIGINT', 'SIGQUIT'];
|
|
396
|
+
// Signals sent to the launcher's process alone.
|
|
397
|
+
const forwardedSignals = process.platform === 'win32' ? [] : ['SIGTERM', 'SIGHUP'];
|
|
398
|
+
|
|
399
|
+
export function main() {
|
|
400
|
+
const text = messages[selectLocale(process.env)];
|
|
401
|
+
// Registered first, so an install started below also receives Ctrl+C alone and is waited for.
|
|
402
|
+
for (const signal of ignoredSignals) process.on(signal, () => {});
|
|
403
|
+
const require = createRequire(import.meta.url);
|
|
404
|
+
const own = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8'));
|
|
405
|
+
const stem = own.name;
|
|
406
|
+
const subPackages = Object.keys(own.optionalDependencies ?? {});
|
|
407
|
+
const suffix = `-${process.platform}-${process.arch}`;
|
|
408
|
+
const subPackage = subPackages.find((name) => name.endsWith(`/cli${suffix}`));
|
|
409
|
+
if (subPackage === undefined) {
|
|
410
|
+
const describe = (platform, architecture) =>
|
|
411
|
+
`${platformNames[platform] ?? platform} ${architectureNames[architecture] ?? architecture}`;
|
|
412
|
+
const supported = subPackages
|
|
413
|
+
.map((name) => /\/cli-([a-z0-9]+)-([a-z0-9]+)$/u.exec(name))
|
|
414
|
+
.filter((match) => match !== null)
|
|
415
|
+
.map((match) => describe(match[1], match[2]))
|
|
416
|
+
.join(', ');
|
|
417
|
+
fail(text.unsupported(describe(process.platform, process.arch), supported));
|
|
418
|
+
}
|
|
419
|
+
let carriedPackage;
|
|
420
|
+
try {
|
|
421
|
+
carriedPackage = require.resolve(`${subPackage}/package.json`);
|
|
422
|
+
} catch {
|
|
423
|
+
fail(text.missing(subPackage));
|
|
424
|
+
}
|
|
425
|
+
const carried = JSON.parse(readFileSync(carriedPackage, 'utf8'));
|
|
426
|
+
if (!validVersion(carried.version)) fail(text.missing(subPackage));
|
|
427
|
+
const programName = process.platform === 'win32' ? `${stem}.exe` : stem;
|
|
428
|
+
const program = resolveProgram({
|
|
429
|
+
root: storeRoot(stem, process.platform, process.env),
|
|
430
|
+
programName,
|
|
431
|
+
carriedProgram: path.join(path.dirname(carriedPackage), 'release', programName),
|
|
432
|
+
carriedVersion: carried.version,
|
|
433
|
+
text,
|
|
434
|
+
});
|
|
435
|
+
if (process.platform === 'win32' && automaticUpdatesLive(stem))
|
|
436
|
+
runWithRelaunch(program, storeRoot(stem, process.platform, process.env), programName, text);
|
|
437
|
+
else run(program, text);
|
|
438
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,27 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "orchbee",
|
|
3
|
+
"version": "0.1.0-beta.10",
|
|
4
|
+
"description": "A terminal that runs and orchestrates AI agents",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/i-need-token/orchbee.git"
|
|
9
|
+
},
|
|
10
|
+
"bin": {
|
|
11
|
+
"orchbee": "bin/orchbee.mjs"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"bin",
|
|
15
|
+
"lib"
|
|
16
|
+
],
|
|
17
|
+
"engines": {
|
|
18
|
+
"node": ">=18"
|
|
19
|
+
},
|
|
20
|
+
"optionalDependencies": {
|
|
21
|
+
"@orchbee/cli-win32-x64": "0.1.0-beta.10"
|
|
22
|
+
},
|
|
23
|
+
"publishConfig": {
|
|
24
|
+
"access": "public",
|
|
25
|
+
"tag": "next"
|
|
26
|
+
}
|
|
27
|
+
}
|