Repository navigation
Expand file tree
/
Copy pathbytecode-api
More file actions
executable file
·56 lines (50 loc) · 1.67 KB
/
Copy pathbytecode-api
File metadata and controls
executable file
·56 lines (50 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env php
<?php
declare(strict_types=1);
/*
* Launcher for the Rust `bytecode-api` server: a locked-down local HTTP server
* that exposes every bytecode tool for agentic control. Requires
* BYTECODE_API_TOKEN (bearer auth) and binds loopback only unless
* BYTECODE_API_ALLOW_REMOTE=1. This PHP wrapper keeps the server discoverable
* alongside the other php/bin tools; it resolves and execs the compiled Rust
* binary (build it with `cd rust && cargo build --release`).
*
* Usage: bytecode-api [addr] (default 127.0.0.1:8787)
*/
$root = getenv('BYTECODE_ROOT') ?: dirname(__DIR__, 2);
$candidates = [];
if ($explicit = getenv('BYTECODE_API_RS')) {
$candidates[] = $explicit;
}
$candidates[] = $root . '/rust/target/release/bytecode-api';
$candidates[] = __DIR__ . '/bytecode-api-rs';
$bin = null;
foreach ($candidates as $cand) {
if ($cand !== '' && is_file($cand) && is_executable($cand)) {
$bin = $cand;
break;
}
}
if ($bin === null) {
fwrite(STDERR, "bytecode-api: Rust server binary not found.\n");
fwrite(STDERR, "Build it with: (cd {$root}/rust && cargo build --release)\n");
fwrite(STDERR, "Or set BYTECODE_API_RS to its path.\n");
exit(127);
}
if (getenv('BYTECODE_ROOT') === false) {
putenv('BYTECODE_ROOT=' . dirname(__DIR__, 2));
}
if (!function_exists('proc_open')) {
fwrite(STDERR, "bytecode-api: proc_open is disabled; cannot launch server.\n");
exit(1);
}
$proc = proc_open(
array_merge([$bin], array_slice($argv, 1)),
[0 => STDIN, 1 => STDOUT, 2 => STDERR],
$pipes
);
if (!is_resource($proc)) {
fwrite(STDERR, "bytecode-api: failed to launch {$bin}\n");
exit(127);
}
exit(proc_close($proc));