agent86-web 0.0.0-stage → 0.2.0
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 +89 -2
- package/dist/agent-process.js +284 -0
- package/dist/agent86.js +77 -0
- package/dist/auth.js +96 -0
- package/dist/cli.js +156 -0
- package/dist/duration.js +9 -0
- package/dist/files.js +69 -0
- package/dist/policy.js +99 -0
- package/dist/public/agent86-logo.png +0 -0
- package/dist/public/assets/engine-uu4Pp8lX.js +6 -0
- package/dist/public/assets/index-B2J10lb0.css +2 -0
- package/dist/public/assets/index-sMw9P5mz.js +107 -0
- package/dist/public/favicon.png +0 -0
- package/dist/public/index.html +15 -0
- package/dist/server.js +324 -0
- package/dist/sessions.js +61 -0
- package/dist/static.js +66 -0
- package/dist/threads.js +79 -0
- package/dist/trust.js +102 -0
- package/dist/updates.js +70 -0
- package/dist/workspaces.js +24 -0
- package/package.json +50 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Maetek Technologies
|
|
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,90 @@
|
|
|
1
|
-
#
|
|
1
|
+
# agent86-web
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A browser interface for [agent86](https://github.com/Maetek-Technologies/agent86), the agentic command-line harness.
|
|
4
|
+
|
|
5
|
+
agent86-web runs a small server on your machine (loopback only) and opens a page in your browser. Each thread you open there is your own `agent86` running in a project folder, with your config, keys, sandbox, memory, and sessions. The browser gives you:
|
|
6
|
+
|
|
7
|
+
- **Conversations** with streamed Markdown and the model's reasoning, which you can fold away.
|
|
8
|
+
- **Approvals**, with the diff or command in view.
|
|
9
|
+
- **Saved sessions**, including ones started at a terminal (and `agent86 --resume <id>` continues a browser session at a terminal).
|
|
10
|
+
- **Several threads at once.**
|
|
11
|
+
- **Model and approval-mode pickers**, `/` commands, and `@` file mentions.
|
|
12
|
+
- **Thread names** you choose (saved in agent86 with 1.0.17 or newer), a resizable sessions panel, and a **Quit** button.
|
|
13
|
+
|
|
14
|
+
## Requirements
|
|
15
|
+
|
|
16
|
+
- Node.js 20.19 or newer
|
|
17
|
+
- [agent86](https://github.com/Maetek-Technologies/agent86) 1.0.15 or newer on your `PATH` (1.0.17 or newer recommended: it keeps thread names you give in the browser and lists each workspace's own sessions), with a model it can use:
|
|
18
|
+
|
|
19
|
+
```sh
|
|
20
|
+
npm install -g agent86
|
|
21
|
+
agent86 keys set openrouter # or anthropic, openai, …; or set the provider's environment variable
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Run it
|
|
25
|
+
|
|
26
|
+
```sh
|
|
27
|
+
npx agent86-web --root ~/code
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
It prints a link and opens it in your browser. The link signs the browser in. It works once and expires after five minutes. Run the command again for a new one.
|
|
31
|
+
|
|
32
|
+
To install the command instead:
|
|
33
|
+
|
|
34
|
+
```sh
|
|
35
|
+
npm install -g agent86-web
|
|
36
|
+
agent86-web --root ~/code
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Options
|
|
40
|
+
|
|
41
|
+
| Option | Default | Meaning |
|
|
42
|
+
| -------------------- | --------------------- | ------------------------------------------------------------------------------------------------------------------- |
|
|
43
|
+
| `--root <dir>` | the current directory | A folder that threads may run in, or in folders under it. Repeat for more. |
|
|
44
|
+
| `--port <n>` | any free port | The port to listen on |
|
|
45
|
+
| `--host <addr>` | `127.0.0.1` | A loopback address: `127.0.0.1`, `::1`, or `localhost` |
|
|
46
|
+
| `--agent86 <path>` | `agent86` on `PATH` | The agent86 to run. Pointing at the native binary instead of npm's launcher saves about 45 MB per thread. |
|
|
47
|
+
| `--agent-arg=<arg>` | — | An argument for agent86, placed before `acp`. Repeatable, e.g. `--agent-arg=--model --agent-arg=openrouter:<model>` |
|
|
48
|
+
| `--max-agents <n>` | `8` | The most agent86 processes running at once |
|
|
49
|
+
| `--idle-timeout <d>` | `15m` | Stop an agent86 that has no open tab and no running turn after this long (`90s`, `15m`, `1h`) |
|
|
50
|
+
| `--no-open` | — | Print the link without opening the browser |
|
|
51
|
+
| `--no-update-check` | — | Don't check npm for newer agent86 or agent86-web releases (also `AGENT86_WEB_NO_UPDATE_CHECK=1`) |
|
|
52
|
+
|
|
53
|
+
## Things to know
|
|
54
|
+
|
|
55
|
+
- **Workspaces.** A thread runs in one folder under a `--root`. Pick it with _Open a workspace…_. To work in another folder, open a new thread there.
|
|
56
|
+
- **Project settings need trust.** agent86 ignores the riskier settings in a project's `.agent86/config.toml`, such as MCP servers, providers, and approvals, until you trust the file. When it does, the thread shows a banner. _Review and trust_ shows the whole file. agent86-web remembers your decision for that exact file in `~/.agent86-web/trusted-projects.json`, and asks again if the file changes. Revoke it under _Settings & about_ (the agent86 version button in the top bar).
|
|
57
|
+
- **The approval mode new threads start in.** Choose it under _Settings & about_; it applies once, when a thread is first opened in that browser. To change agent86's own default everywhere, terminal included, set `approval = "auto"` (or `ask`, `deny`) under `[guardrails]` in `~/.agent86/config.toml`, or start agent86-web with `AGENT86_APPROVAL=auto` in its environment.
|
|
58
|
+
- **Stopping.** Press Ctrl+C in the terminal, or use ⏻ (Quit) in the top bar. Either way, every agent86 it started stops; sessions are saved.
|
|
59
|
+
- **Model setup and secrets stay at the terminal.** Add keys with `agent86 keys set <provider>` or `/config model`, and enter secrets with `/secret NAME` in `agent86`, choosing to remember them for the project. Browser threads then use them. The protocol agent86-web speaks does not allow asking for secrets in a form.
|
|
60
|
+
- **Using it from another device.** agent86-web listens only on loopback. Forward the port over SSH:
|
|
61
|
+
```sh
|
|
62
|
+
ssh -L 8686:127.0.0.1:8686 you@dev-machine # on dev-machine: agent86-web --port 8686 --no-open
|
|
63
|
+
```
|
|
64
|
+
Then open the printed link on the device.
|
|
65
|
+
|
|
66
|
+
## Security
|
|
67
|
+
|
|
68
|
+
agent86-web can make agent86 edit files and run commands, so it is locked down:
|
|
69
|
+
|
|
70
|
+
- It listens only on loopback.
|
|
71
|
+
- The browser signs in with a one-time token, then holds an `HttpOnly`, `SameSite=Strict` cookie.
|
|
72
|
+
- Every request is checked for the `Host` header, which stops DNS rebinding.
|
|
73
|
+
- Every WebSocket and change is checked for its `Origin` and a CSRF header.
|
|
74
|
+
- A strict Content-Security-Policy applies to the page.
|
|
75
|
+
- Threads can only run under the `--root` folders.
|
|
76
|
+
- A page cannot add MCP servers.
|
|
77
|
+
|
|
78
|
+
agent86's own approval gate, sandbox, and guardrails apply unchanged. The threat model is in [docs/SECURITY.md](https://github.com/Maetek-Technologies/Agent86-web/blob/main/docs/SECURITY.md).
|
|
79
|
+
|
|
80
|
+
## Troubleshooting
|
|
81
|
+
|
|
82
|
+
- **"agent86 was not found"**: install it (`npm install -g agent86`), or pass `--agent86 /path/to/agent86`.
|
|
83
|
+
- **"This sign-in link has already been used or has expired"**: run `agent86-web` again for a new link.
|
|
84
|
+
- **"agent86 could not start a session"**: its model has no API key. Run `agent86 keys set <provider>`, then choose _Try again_.
|
|
85
|
+
- **A project's MCP servers don't start**: trust the project from the banner in its thread.
|
|
86
|
+
- **agent86's own notes**: open _logs_ in the thread's header.
|
|
87
|
+
|
|
88
|
+
## License
|
|
89
|
+
|
|
90
|
+
MIT, © 2026 Maetek Technologies. agent86-web is a Maetek Technologies project, a browser interface for agent86. agent86 and its name, logo, and mascot belong to Maetek Technologies.
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
// One `agent86 acp` process, and its relay to whichever WebSocket is attached to its thread
|
|
2
|
+
// (docs/ARCHITECTURE.md §3.3–3.4).
|
|
3
|
+
//
|
|
4
|
+
// A process outlives its socket: a reload or a dropped connection reattaches to the same process.
|
|
5
|
+
// Each attached socket is a new generation. Request ids from the browser are rewritten per
|
|
6
|
+
// generation, so a reply meant for a socket that has gone is dropped instead of reaching the next
|
|
7
|
+
// one, whose ids start again from zero. A request from agent86 that no socket can answer (a
|
|
8
|
+
// permission request while detached) is declined.
|
|
9
|
+
import { spawn } from "node:child_process";
|
|
10
|
+
import { needsShell } from "./agent86.js";
|
|
11
|
+
import { filterClientMessage } from "./policy.js";
|
|
12
|
+
/** The largest message relayed in either direction (docs/SECURITY.md T12). */
|
|
13
|
+
export const MAX_MESSAGE_BYTES = 8 * 1024 * 1024;
|
|
14
|
+
/** How many stderr lines are kept for the logs drawer. */
|
|
15
|
+
export const STDERR_LINES = 500;
|
|
16
|
+
/** The bridge's own notification carrying one line of agent86's stderr (an ACP extension method). */
|
|
17
|
+
export const STDERR_METHOD = "_agent86web/stderr";
|
|
18
|
+
/**
|
|
19
|
+
* The most frames a page may send in `RATE_WINDOW_MS` before its socket is closed (docs/SECURITY.md
|
|
20
|
+
* T12). A person, even pasting and pressing keys fast, sends a handful per second.
|
|
21
|
+
*/
|
|
22
|
+
export const MAX_FRAMES_PER_WINDOW = 500;
|
|
23
|
+
export const RATE_WINDOW_MS = 10_000;
|
|
24
|
+
/** Pause agent86's stdout while this many frames wait to be sent to the browser. */
|
|
25
|
+
const MAX_PENDING_SENDS = 256;
|
|
26
|
+
function isId(value) {
|
|
27
|
+
return typeof value === "string" || typeof value === "number";
|
|
28
|
+
}
|
|
29
|
+
export class AgentProcess {
|
|
30
|
+
options;
|
|
31
|
+
child;
|
|
32
|
+
stderr = [];
|
|
33
|
+
exited;
|
|
34
|
+
/** When the browser or agent86 last sent anything, for idle reaping. */
|
|
35
|
+
lastActivity;
|
|
36
|
+
stdoutBuffer = "";
|
|
37
|
+
socket;
|
|
38
|
+
generation = 0;
|
|
39
|
+
nextId = 0;
|
|
40
|
+
pendingSends = 0;
|
|
41
|
+
/** Browser requests agent86 has not answered yet, by their rewritten id. */
|
|
42
|
+
inflight = new Map();
|
|
43
|
+
/** agent86's requests the attached socket has not answered yet. */
|
|
44
|
+
asked = new Map();
|
|
45
|
+
now;
|
|
46
|
+
/**
|
|
47
|
+
* agent86's answer to the first `initialize`. A page that reattaches sends `initialize` again,
|
|
48
|
+
* and agent86 refuses every request while a turn runs, so the bridge answers it from here.
|
|
49
|
+
*/
|
|
50
|
+
initResult;
|
|
51
|
+
constructor(options) {
|
|
52
|
+
this.options = options;
|
|
53
|
+
this.now = options.now ?? Date.now;
|
|
54
|
+
this.lastActivity = this.now();
|
|
55
|
+
this.child = spawn(options.command, [...options.args, "acp"], {
|
|
56
|
+
cwd: options.workspace,
|
|
57
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
58
|
+
shell: needsShell(options.command),
|
|
59
|
+
windowsHide: true,
|
|
60
|
+
});
|
|
61
|
+
this.exited = new Promise((resolve) => {
|
|
62
|
+
this.child.on("error", (error) => {
|
|
63
|
+
this.note(`could not start agent86: ${error.message}`);
|
|
64
|
+
resolve(null);
|
|
65
|
+
});
|
|
66
|
+
this.child.on("exit", (code) => resolve(code));
|
|
67
|
+
});
|
|
68
|
+
this.child.stdout.setEncoding("utf8").on("data", (chunk) => this.onStdout(chunk));
|
|
69
|
+
this.child.stderr.setEncoding("utf8").on("data", (chunk) => {
|
|
70
|
+
for (const line of chunk.split(/\r?\n/))
|
|
71
|
+
if (line)
|
|
72
|
+
this.note(line);
|
|
73
|
+
});
|
|
74
|
+
// A write to a process that has exited fails with EPIPE; the exit is handled elsewhere.
|
|
75
|
+
this.child.stdin.on("error", () => undefined);
|
|
76
|
+
void this.exited.then((code) => {
|
|
77
|
+
const socket = this.socket;
|
|
78
|
+
if (socket &&
|
|
79
|
+
(socket.readyState === socket.OPEN || socket.readyState === socket.CONNECTING)) {
|
|
80
|
+
socket.close(1011, `agent86 exited (status ${code ?? "unknown"})`);
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
get workspace() {
|
|
85
|
+
return this.options.workspace;
|
|
86
|
+
}
|
|
87
|
+
get alive() {
|
|
88
|
+
return this.child.exitCode === null && this.child.signalCode === null;
|
|
89
|
+
}
|
|
90
|
+
get attached() {
|
|
91
|
+
return this.socket !== undefined;
|
|
92
|
+
}
|
|
93
|
+
/** Whether a prompt turn is running (agent86 refuses other requests until it ends). */
|
|
94
|
+
get busy() {
|
|
95
|
+
for (const request of this.inflight.values())
|
|
96
|
+
if (request.method === "session/prompt")
|
|
97
|
+
return true;
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
/** Relay between this process and `socket`, replacing any socket attached before. */
|
|
101
|
+
attach(socket) {
|
|
102
|
+
const previous = this.socket;
|
|
103
|
+
if (previous) {
|
|
104
|
+
this.detach(previous);
|
|
105
|
+
previous.close(4000, "replaced by a newer connection to this thread");
|
|
106
|
+
}
|
|
107
|
+
this.generation++;
|
|
108
|
+
this.socket = socket;
|
|
109
|
+
this.lastActivity = this.now();
|
|
110
|
+
// The new page has not seen agent86's notes; replay them.
|
|
111
|
+
for (const line of this.stderr)
|
|
112
|
+
this.send(this.stderrFrame(line));
|
|
113
|
+
let windowStart = this.now();
|
|
114
|
+
let frames = 0;
|
|
115
|
+
socket.on("message", (data, isBinary) => {
|
|
116
|
+
if (this.socket !== socket)
|
|
117
|
+
return;
|
|
118
|
+
const now = this.now();
|
|
119
|
+
if (now - windowStart > RATE_WINDOW_MS) {
|
|
120
|
+
windowStart = now;
|
|
121
|
+
frames = 0;
|
|
122
|
+
}
|
|
123
|
+
if (++frames > MAX_FRAMES_PER_WINDOW) {
|
|
124
|
+
this.note("bridge closed a connection that sent too many messages");
|
|
125
|
+
socket.close(1008, "too many messages");
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
if (isBinary) {
|
|
129
|
+
socket.close(1003, "binary frames are not accepted");
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
this.fromClient(data.toString());
|
|
133
|
+
});
|
|
134
|
+
socket.on("close", () => this.detach(socket));
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* End the process: close its input, then kill it if it has not exited after a grace period. An
|
|
138
|
+
* attached socket is closed with `reason`, so its page knows to reconnect.
|
|
139
|
+
*/
|
|
140
|
+
stop(reason = "agent86 was stopped") {
|
|
141
|
+
const socket = this.socket;
|
|
142
|
+
if (socket) {
|
|
143
|
+
this.detach(socket);
|
|
144
|
+
socket.close(4001, reason);
|
|
145
|
+
}
|
|
146
|
+
if (this.alive) {
|
|
147
|
+
this.child.stdin.end();
|
|
148
|
+
const timer = setTimeout(() => this.child.kill(), 2_000);
|
|
149
|
+
void this.exited.then(() => clearTimeout(timer));
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
detach(socket) {
|
|
153
|
+
if (this.socket !== socket)
|
|
154
|
+
return;
|
|
155
|
+
this.socket = undefined;
|
|
156
|
+
this.lastActivity = this.now();
|
|
157
|
+
// Whatever agent86 asked that socket can no longer be answered by it: decline.
|
|
158
|
+
for (const [id, method] of this.asked)
|
|
159
|
+
this.declineRequest(id, method);
|
|
160
|
+
this.asked.clear();
|
|
161
|
+
}
|
|
162
|
+
fromClient(raw) {
|
|
163
|
+
this.lastActivity = this.now();
|
|
164
|
+
const verdict = filterClientMessage(raw, {
|
|
165
|
+
workspace: this.options.workspace,
|
|
166
|
+
realpath: this.options.realpath,
|
|
167
|
+
});
|
|
168
|
+
if (verdict.action === "reject") {
|
|
169
|
+
this.note(`bridge refused a message: ${verdict.reason}`);
|
|
170
|
+
if (verdict.error)
|
|
171
|
+
this.send(JSON.stringify(verdict.error));
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
const message = verdict.message;
|
|
175
|
+
if (message.method === "initialize" && isId(message.id) && this.initResult !== undefined) {
|
|
176
|
+
this.send(JSON.stringify({ jsonrpc: "2.0", id: message.id, result: this.initResult }));
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
if (typeof message.method === "string" && isId(message.id)) {
|
|
180
|
+
const rewritten = `w${this.generation}.${this.nextId++}`;
|
|
181
|
+
this.inflight.set(rewritten, {
|
|
182
|
+
generation: this.generation,
|
|
183
|
+
id: message.id,
|
|
184
|
+
method: message.method,
|
|
185
|
+
});
|
|
186
|
+
this.write({ ...message, id: rewritten });
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
if (message.method === undefined && isId(message.id)) {
|
|
190
|
+
// The answer to one of agent86's requests.
|
|
191
|
+
if (!this.asked.delete(message.id))
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
this.write(message);
|
|
195
|
+
}
|
|
196
|
+
write(message) {
|
|
197
|
+
if (!this.child.stdin.write(`${JSON.stringify(message)}\n`)) {
|
|
198
|
+
const socket = this.socket;
|
|
199
|
+
socket?.pause();
|
|
200
|
+
this.child.stdin.once("drain", () => socket?.resume());
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
fromAgent(line) {
|
|
204
|
+
this.lastActivity = this.now();
|
|
205
|
+
let message;
|
|
206
|
+
try {
|
|
207
|
+
message = JSON.parse(line);
|
|
208
|
+
}
|
|
209
|
+
catch {
|
|
210
|
+
this.note("agent86 wrote a line that is not JSON; dropped");
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
const { id, method } = message;
|
|
214
|
+
if (method === undefined && typeof id === "string" && this.inflight.has(id)) {
|
|
215
|
+
// A reply: restore the browser's id, or drop it if its socket has gone.
|
|
216
|
+
const request = this.inflight.get(id);
|
|
217
|
+
this.inflight.delete(id);
|
|
218
|
+
if (request.method === "initialize" && "result" in message)
|
|
219
|
+
this.initResult ??= message.result;
|
|
220
|
+
if (request.generation !== this.generation || !this.socket)
|
|
221
|
+
return;
|
|
222
|
+
this.send(JSON.stringify({ ...message, id: request.id }));
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
if (typeof method === "string" && isId(id)) {
|
|
226
|
+
// A request from agent86, such as a permission request.
|
|
227
|
+
if (!this.socket) {
|
|
228
|
+
this.declineRequest(id, method);
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
this.asked.set(id, method);
|
|
232
|
+
}
|
|
233
|
+
this.send(line);
|
|
234
|
+
}
|
|
235
|
+
/** Answer one of agent86's requests on the browser's behalf, declining it. */
|
|
236
|
+
declineRequest(id, method) {
|
|
237
|
+
const reply = method === "session/request_permission"
|
|
238
|
+
? { jsonrpc: "2.0", id, result: { outcome: { outcome: "cancelled" } } }
|
|
239
|
+
: {
|
|
240
|
+
jsonrpc: "2.0",
|
|
241
|
+
id,
|
|
242
|
+
error: { code: -32603, message: "No browser is connected to answer." },
|
|
243
|
+
};
|
|
244
|
+
this.write(reply);
|
|
245
|
+
}
|
|
246
|
+
stderrFrame(line) {
|
|
247
|
+
return JSON.stringify({ jsonrpc: "2.0", method: STDERR_METHOD, params: { line } });
|
|
248
|
+
}
|
|
249
|
+
note(line) {
|
|
250
|
+
this.stderr.push(line);
|
|
251
|
+
if (this.stderr.length > STDERR_LINES)
|
|
252
|
+
this.stderr.shift();
|
|
253
|
+
this.options.log?.(line);
|
|
254
|
+
this.send(this.stderrFrame(line));
|
|
255
|
+
}
|
|
256
|
+
onStdout(chunk) {
|
|
257
|
+
this.stdoutBuffer += chunk;
|
|
258
|
+
let newline;
|
|
259
|
+
while ((newline = this.stdoutBuffer.indexOf("\n")) >= 0) {
|
|
260
|
+
const line = this.stdoutBuffer.slice(0, newline).trim();
|
|
261
|
+
this.stdoutBuffer = this.stdoutBuffer.slice(newline + 1);
|
|
262
|
+
if (line)
|
|
263
|
+
this.fromAgent(line);
|
|
264
|
+
}
|
|
265
|
+
if (this.stdoutBuffer.length > MAX_MESSAGE_BYTES) {
|
|
266
|
+
this.note("agent86 sent a message larger than the bridge's limit; stopping it");
|
|
267
|
+
this.stdoutBuffer = "";
|
|
268
|
+
this.child.kill();
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
send(frame) {
|
|
272
|
+
const socket = this.socket;
|
|
273
|
+
if (!socket || socket.readyState !== socket.OPEN)
|
|
274
|
+
return;
|
|
275
|
+
this.pendingSends++;
|
|
276
|
+
if (this.pendingSends >= MAX_PENDING_SENDS)
|
|
277
|
+
this.child.stdout.pause();
|
|
278
|
+
socket.send(frame, () => {
|
|
279
|
+
this.pendingSends--;
|
|
280
|
+
if (this.pendingSends < MAX_PENDING_SENDS / 2)
|
|
281
|
+
this.child.stdout.resume();
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
}
|
package/dist/agent86.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
// Finding agent86 and checking its version. The bridge talks to agent86 only through
|
|
2
|
+
// `agent86 --version` and `agent86 acp` (docs/adr/0003-standalone-bridge.md).
|
|
3
|
+
import { spawn } from "node:child_process";
|
|
4
|
+
export const MIN_AGENT86_VERSION = "1.0.15";
|
|
5
|
+
/**
|
|
6
|
+
* npm installs `agent86` as a `.cmd` shim on Windows, which only a shell can start (Node refuses
|
|
7
|
+
* to spawn a `.cmd` or `.bat` file directly). A bare name there may resolve to such a shim, so it
|
|
8
|
+
* also goes through the shell. The shell joins the arguments, so on Windows an argument for
|
|
9
|
+
* agent86 must not contain spaces.
|
|
10
|
+
*/
|
|
11
|
+
export function needsShell(command) {
|
|
12
|
+
if (process.platform !== "win32")
|
|
13
|
+
return false;
|
|
14
|
+
return /\.(cmd|bat)$/i.test(command) || !/[\\/]/.test(command);
|
|
15
|
+
}
|
|
16
|
+
export class Agent86Error extends Error {
|
|
17
|
+
constructor(message) {
|
|
18
|
+
super(message);
|
|
19
|
+
this.name = "Agent86Error";
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
export function compareVersions(a, b) {
|
|
23
|
+
const parse = (v) => v
|
|
24
|
+
.split(/[-+]/)[0]
|
|
25
|
+
.split(".")
|
|
26
|
+
.map((part) => Number.parseInt(part, 10) || 0);
|
|
27
|
+
const pa = parse(a);
|
|
28
|
+
const pb = parse(b);
|
|
29
|
+
for (let i = 0; i < Math.max(pa.length, pb.length); i++) {
|
|
30
|
+
const d = (pa[i] ?? 0) - (pb[i] ?? 0);
|
|
31
|
+
if (d !== 0)
|
|
32
|
+
return Math.sign(d);
|
|
33
|
+
}
|
|
34
|
+
return 0;
|
|
35
|
+
}
|
|
36
|
+
/** Parse `agent86 --version` output, e.g. `agent86 1.0.15`. */
|
|
37
|
+
export function parseVersion(output) {
|
|
38
|
+
return /\bagent86\s+v?(\d+\.\d+\.\d+(?:[-+][\w.]+)?)/.exec(output)?.[1];
|
|
39
|
+
}
|
|
40
|
+
/** Run `<command> --version` and check it is an agent86 we support. */
|
|
41
|
+
export async function detectAgent86(command, timeoutMs = 15_000) {
|
|
42
|
+
const output = await new Promise((resolve, reject) => {
|
|
43
|
+
const child = spawn(command, ["--version"], {
|
|
44
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
45
|
+
shell: needsShell(command),
|
|
46
|
+
windowsHide: true,
|
|
47
|
+
});
|
|
48
|
+
let out = "";
|
|
49
|
+
child.stdout.setEncoding("utf8").on("data", (chunk) => (out += chunk));
|
|
50
|
+
child.stderr.setEncoding("utf8").on("data", (chunk) => (out += chunk));
|
|
51
|
+
const timer = setTimeout(() => {
|
|
52
|
+
child.kill();
|
|
53
|
+
reject(new Agent86Error(`\`${command} --version\` did not answer within ${timeoutMs / 1000}s.`));
|
|
54
|
+
}, timeoutMs);
|
|
55
|
+
child.on("error", (error) => {
|
|
56
|
+
clearTimeout(timer);
|
|
57
|
+
reject(new Agent86Error(error.code === "ENOENT"
|
|
58
|
+
? `agent86 was not found (looked for \`${command}\`). Install it with: npm install -g agent86`
|
|
59
|
+
: `Could not run \`${command}\`: ${error.message}`));
|
|
60
|
+
});
|
|
61
|
+
child.on("close", (code) => {
|
|
62
|
+
clearTimeout(timer);
|
|
63
|
+
if (code === 0)
|
|
64
|
+
resolve(out);
|
|
65
|
+
else
|
|
66
|
+
reject(new Agent86Error(`\`${command} --version\` exited with status ${code}: ${out.trim()}`));
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
const version = parseVersion(output);
|
|
70
|
+
if (!version) {
|
|
71
|
+
throw new Agent86Error(`\`${command} --version\` printed ${JSON.stringify(output.trim())}, not an agent86 version.`);
|
|
72
|
+
}
|
|
73
|
+
if (compareVersions(version, MIN_AGENT86_VERSION) < 0) {
|
|
74
|
+
throw new Agent86Error(`agent86 ${version} is too old; agent86-web needs ${MIN_AGENT86_VERSION} or newer. Update it with: npm install -g agent86@latest`);
|
|
75
|
+
}
|
|
76
|
+
return version;
|
|
77
|
+
}
|
package/dist/auth.js
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
// Launch-token login, the session cookie, and the Host and Origin checks.
|
|
2
|
+
// See docs/SECURITY.md T2–T6.
|
|
3
|
+
import { randomBytes, timingSafeEqual } from "node:crypto";
|
|
4
|
+
export const COOKIE_NAME = "agent86web_session";
|
|
5
|
+
export const CSRF_HEADER = "x-agent86-web";
|
|
6
|
+
export const TOKEN_LIFETIME_MS = 5 * 60 * 1000;
|
|
7
|
+
function secret() {
|
|
8
|
+
return randomBytes(32).toString("base64url");
|
|
9
|
+
}
|
|
10
|
+
function same(a, b) {
|
|
11
|
+
const ba = Buffer.from(a);
|
|
12
|
+
const bb = Buffer.from(b);
|
|
13
|
+
return ba.length === bb.length && timingSafeEqual(ba, bb);
|
|
14
|
+
}
|
|
15
|
+
export class Auth {
|
|
16
|
+
options;
|
|
17
|
+
/** The one-time launch token, or undefined once it has been used or has expired. */
|
|
18
|
+
token = secret();
|
|
19
|
+
issuedAt;
|
|
20
|
+
sessions = new Set();
|
|
21
|
+
now;
|
|
22
|
+
allowedHosts;
|
|
23
|
+
allowedOrigins;
|
|
24
|
+
constructor(options) {
|
|
25
|
+
this.options = options;
|
|
26
|
+
this.now = options.now ?? Date.now;
|
|
27
|
+
this.issuedAt = this.now();
|
|
28
|
+
const { port } = options;
|
|
29
|
+
this.allowedHosts = new Set([`127.0.0.1:${port}`, `localhost:${port}`, `[::1]:${port}`]);
|
|
30
|
+
const origins = [...this.allowedHosts].map((host) => `http://${host}`);
|
|
31
|
+
if (options.devOrigin)
|
|
32
|
+
origins.push(new URL(options.devOrigin).origin);
|
|
33
|
+
this.allowedOrigins = new Set(origins);
|
|
34
|
+
}
|
|
35
|
+
/** The launch token, for the URL the bridge prints. */
|
|
36
|
+
get launchToken() {
|
|
37
|
+
if (!this.token)
|
|
38
|
+
throw new Error("the launch token has already been used");
|
|
39
|
+
return this.token;
|
|
40
|
+
}
|
|
41
|
+
/** Exchange the launch token for a session id. The token works once, within its lifetime. */
|
|
42
|
+
login(candidate) {
|
|
43
|
+
const token = this.token;
|
|
44
|
+
if (!token || typeof candidate !== "string")
|
|
45
|
+
return undefined;
|
|
46
|
+
if (this.now() - this.issuedAt > TOKEN_LIFETIME_MS) {
|
|
47
|
+
this.token = undefined;
|
|
48
|
+
return undefined;
|
|
49
|
+
}
|
|
50
|
+
if (!same(candidate, token))
|
|
51
|
+
return undefined;
|
|
52
|
+
this.token = undefined;
|
|
53
|
+
const session = secret();
|
|
54
|
+
this.sessions.add(session);
|
|
55
|
+
return session;
|
|
56
|
+
}
|
|
57
|
+
/** Whether a request carries a valid session cookie. */
|
|
58
|
+
hasSession(req) {
|
|
59
|
+
const value = readCookie(req.headers.cookie, COOKIE_NAME);
|
|
60
|
+
if (!value)
|
|
61
|
+
return false;
|
|
62
|
+
for (const session of this.sessions)
|
|
63
|
+
if (same(value, session))
|
|
64
|
+
return true;
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
/** DNS rebinding defence (T3): only our own loopback names. */
|
|
68
|
+
hostAllowed(req) {
|
|
69
|
+
const host = req.headers.host;
|
|
70
|
+
return typeof host === "string" && this.allowedHosts.has(host.toLowerCase());
|
|
71
|
+
}
|
|
72
|
+
/** Cross-site defence (T2, T4): the Origin must be exactly ours. */
|
|
73
|
+
originAllowed(req) {
|
|
74
|
+
const origin = req.headers.origin;
|
|
75
|
+
return typeof origin === "string" && this.allowedOrigins.has(origin);
|
|
76
|
+
}
|
|
77
|
+
/** The CSRF header every state-changing request must carry (T4). */
|
|
78
|
+
static hasCsrfHeader(req) {
|
|
79
|
+
return req.headers[CSRF_HEADER] === "1";
|
|
80
|
+
}
|
|
81
|
+
static sessionCookie(session) {
|
|
82
|
+
return `${COOKIE_NAME}=${session}; HttpOnly; SameSite=Strict; Path=/`;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
export function readCookie(header, name) {
|
|
86
|
+
if (!header)
|
|
87
|
+
return undefined;
|
|
88
|
+
for (const part of header.split(";")) {
|
|
89
|
+
const eq = part.indexOf("=");
|
|
90
|
+
if (eq < 0)
|
|
91
|
+
continue;
|
|
92
|
+
if (part.slice(0, eq).trim() === name)
|
|
93
|
+
return part.slice(eq + 1).trim();
|
|
94
|
+
}
|
|
95
|
+
return undefined;
|
|
96
|
+
}
|