- Serve each computer to its owner under /computers/<id>/ (HTTP and websockets proxied to the AIO sandbox) and show it in a shared ComputerView with browser, terminal and code modes, opened from the computers modal or a take-over button in the thread. - ask_user_to_take_over lets a bot hand a computer to the user for a login or captcha; the message gets a take-over button and needs-you, and the viewer hands it back with a chat message. - While a task runs the thread shows "<bot> is working..." with the task's latest step; steps read as plain words, not tool names. - A coordinator with no workers no longer claims to delegate, and bots describe started tasks in the first person. Co-Authored-By: Claude Opus 5.5 <[email protected]>
70 lines
2.1 KiB
TypeScript
70 lines
2.1 KiB
TypeScript
import type { ServerWebSocket } from "bun";
|
|
import type { WebSocketSession } from "@/src/websocket/types";
|
|
|
|
type Socket = ServerWebSocket<WebSocketSession>;
|
|
|
|
/**
|
|
* Bridges a user's websocket on `/computers/<id>/...` to the same path on
|
|
* the computer (a terminal shell, the browser's CDP, the code editor).
|
|
* Messages sent before the computer's side opens are held, and either
|
|
* side closing closes the other.
|
|
*/
|
|
export const computerProxySocket = {
|
|
open(ws: Socket) {
|
|
const proxy = ws.data.proxy;
|
|
|
|
if (!proxy) return;
|
|
|
|
const upstream = new WebSocket(proxy.url, {
|
|
headers: proxy.headers,
|
|
protocols: proxy.protocols,
|
|
} as any);
|
|
|
|
upstream.binaryType = "arraybuffer";
|
|
proxy.upstream = upstream;
|
|
proxy.pending = [];
|
|
|
|
upstream.onopen = () => {
|
|
for (const message of proxy.pending || []) upstream.send(message as string | Uint8Array<ArrayBuffer>);
|
|
proxy.pending = undefined;
|
|
};
|
|
|
|
upstream.onmessage = (event) => {
|
|
ws.send(typeof event.data == "string" ? event.data : new Uint8Array(event.data));
|
|
};
|
|
|
|
upstream.onclose = (event) => {
|
|
ws.close(closeCode({ code: event.code }), event.reason);
|
|
};
|
|
|
|
upstream.onerror = () => {
|
|
ws.close(1011, "The computer isn't answering");
|
|
};
|
|
},
|
|
|
|
message(ws: Socket, message: string | Buffer) {
|
|
const proxy = ws.data.proxy;
|
|
|
|
if (!proxy?.upstream) return;
|
|
|
|
if (proxy.pending) proxy.pending.push(message);
|
|
else if (proxy.upstream.readyState == WebSocket.OPEN) {
|
|
proxy.upstream.send(message as string | Uint8Array<ArrayBuffer>);
|
|
}
|
|
},
|
|
|
|
close(ws: Socket) {
|
|
const upstream = ws.data.proxy?.upstream;
|
|
|
|
if (upstream && upstream.readyState < WebSocket.CLOSING) upstream.close();
|
|
},
|
|
};
|
|
|
|
/**
|
|
* Codes a server may not send itself (no status, abnormal) become a
|
|
* normal close.
|
|
*/
|
|
function closeCode({ code }: { code: number }) {
|
|
return code == 1005 || code == 1006 || code == 1015 ? 1000 : code;
|
|
}
|