The preview panel worked by proxying a dev server through OpenChamber's own origin and rewriting the HTML that came back. Anything the rewriter did not anticipate broke, and pages that refuse to be embedded never loaded at all. This deletes the proxy (-1604 lines and its tests) and merges the preview and browser panels into one surface backed by a real Chromium view. What the panel is now - A `<webview>` in its own session partition: logins and cookies persist, hot reload works because nothing is rewritten, DevTools are one click away. - Annotation: pick one element, drag a region, or draw freehand, write a note, and it reaches chat with a screenshot of the visible page with the marks on it. - Toolbar: hard reload, page zoom, device sizes, a light/dark switch that applies to the page rather than the app, and cookie/cache clearing scoped to the panel alone. - Several pages at once, each tab showing the page's own favicon, and an address bar that suggests pages already visited in this project. - Dev servers are listed from what is actually listening on the machine, checked against what a project announced, so a server is offered no matter how it was started. One that is still starting is waited for instead of failing. Remote dev servers The desktop app binds a local port and pipes raw bytes to the OpenChamber host over the existing authenticated connection, so the page keeps its own origin at the root of its own host. The reachable set is exactly what discovery reports and is re-checked per connection, so an authenticated client cannot dial arbitrary local services on the host. Links and redirects to another loopback port stay on the machine that served the page. A tunnel that cannot be opened is reported; it is never replaced by the plain loopback URL, which would answer from the user's own machine under a remote address. Agent control Browser actions are a separate `openchamber_web` tool: open, snapshot, click, type, scroll, inspect computed styles, resize between mobile/tablet/desktop, and capture a screenshot into `.openchamber/screenshots/` in the project. The existing `openchamber` tool keeps sessions, worktrees and scheduled tasks. Each has its own setting in the new Settings -> General -> OpenChamber Tools section, and the plugin is not injected at all when both are off. Capability belongs to the connected client, not to configuration: a client declares on its event stream that it can drive a page, which only a Chromium host does. Exactly one client performs each request — it claims the request before acting, and the first claim wins — because deciding by whose result arrives first would be too late for a click that already happened. No client listening is answered immediately with an explanation rather than a timeout. Runtime boundaries Web tabs get a plain iframe that can display a page but not inspect one. The VS Code extension no longer offers the surface at all, since nothing that makes the panel worth having works there. Mobile is unaffected. Native boundary Camera, microphone, location and device-picker requests from panel pages are denied — Electron grants them by default when no handler is set, and the panel loads whatever address the user types. Page capture, appearance emulation and storage clearing verify that their target belongs to the panel's own session instead of trusting a web-contents id from the renderer. Persisted state Stored `preview` tabs migrate to `browser` (v13 -> v14). Context panel tab limits are now per surface, so filling one surface no longer evicts another's tabs. Address history is stored per project and per runtime. Documentation `preview.mdx` and `desktop-browser.mdx` rewritten across all locales, the agent tool settings path corrected, new `DOCUMENTATION.md` for the browser-control broker and the dev tunnel, and the `ui-api-decoupling` skill updated where it still described the deleted proxy.
155 lines
5.7 KiB
JavaScript
155 lines
5.7 KiB
JavaScript
/**
|
|
* Request/response broker between the agent tool and the in-app browser.
|
|
*
|
|
* The browser lives in the renderer, not the server, so the server cannot act
|
|
* on a page directly. It publishes a request over the existing OpenChamber
|
|
* event stream and waits for the client that owns the browser view to post the
|
|
* result back.
|
|
*
|
|
* The request goes to every client that could serve it, because the server
|
|
* cannot know which one is showing a page. Exactly one must act on it, so a
|
|
* client claims the request before touching anything and only the first claim
|
|
* is granted. Without that, two connected desktop clients would both click, and
|
|
* the losing one's late result would not undo what it had already done.
|
|
*
|
|
* Two failure modes matter and are handled explicitly rather than as timeouts:
|
|
*
|
|
* - No client is listening. The agent is told immediately that the browser is
|
|
* not open, instead of blocking for the full timeout and then reporting
|
|
* something ambiguous.
|
|
* - The client accepted the request and then went away. That still times out,
|
|
* because the alternative — assuming success — would be a lie.
|
|
*/
|
|
|
|
const DEFAULT_TIMEOUT_MS = 20_000;
|
|
const MAX_TIMEOUT_MS = 120_000;
|
|
|
|
export class BrowserControlError extends Error {
|
|
constructor(message, status = 400) {
|
|
super(message);
|
|
this.name = 'BrowserControlError';
|
|
this.status = status;
|
|
}
|
|
}
|
|
|
|
export const createBrowserControlBroker = ({
|
|
emitRequest,
|
|
createId,
|
|
setTimer = setTimeout,
|
|
clearTimer = clearTimeout,
|
|
} = {}) => {
|
|
if (typeof emitRequest !== 'function') {
|
|
throw new TypeError('emitRequest is required');
|
|
}
|
|
|
|
const pending = new Map();
|
|
|
|
const settle = (requestId, outcome) => {
|
|
const entry = pending.get(requestId);
|
|
if (!entry) return false;
|
|
pending.delete(requestId);
|
|
clearTimer(entry.timer);
|
|
entry.finish(outcome);
|
|
return true;
|
|
};
|
|
|
|
return {
|
|
/** Number of requests still awaiting a client response. */
|
|
get pendingCount() {
|
|
return pending.size;
|
|
},
|
|
|
|
/**
|
|
* Publishes one browser action and resolves with the client's result.
|
|
* Rejects with a BrowserControlError the agent can act on.
|
|
*/
|
|
request(action, parameters = {}, { timeoutMs = DEFAULT_TIMEOUT_MS, signal } = {}) {
|
|
const requestId = typeof createId === 'function' ? createId() : `browser-${Date.now()}-${pending.size}`;
|
|
const boundedTimeout = Math.min(Math.max(1_000, Number(timeoutMs) || DEFAULT_TIMEOUT_MS), MAX_TIMEOUT_MS);
|
|
|
|
const listenerCount = emitRequest({ requestId, action, parameters });
|
|
if (!listenerCount) {
|
|
// Written for the agent reading it, not the user: state what this
|
|
// environment can do, and leave deciding whether it matters to the
|
|
// caller rather than handing it an instruction it cannot carry out.
|
|
return Promise.reject(new BrowserControlError(
|
|
'No OpenChamber client connected here can control a page. Reading and '
|
|
+ 'interacting with a page works when OpenChamber runs as its desktop '
|
|
+ 'application; a web browser tab can display a page but cannot be '
|
|
+ 'driven. Nothing was changed. Mention this to the user only if it '
|
|
+ 'affects what they asked for.',
|
|
503,
|
|
));
|
|
}
|
|
|
|
return new Promise((resolve, reject) => {
|
|
const finish = (outcome) => {
|
|
if (signal && onAbort) signal.removeEventListener('abort', onAbort);
|
|
if (outcome.ok) resolve(outcome.data ?? null);
|
|
else reject(new BrowserControlError(outcome.message || 'Browser action failed', outcome.status || 400));
|
|
};
|
|
|
|
const onAbort = signal
|
|
? () => settle(requestId, { ok: false, message: 'Browser action was cancelled', status: 499 })
|
|
: null;
|
|
if (signal) {
|
|
if (signal.aborted) {
|
|
reject(new BrowserControlError('Browser action was cancelled', 499));
|
|
return;
|
|
}
|
|
signal.addEventListener('abort', onAbort, { once: true });
|
|
}
|
|
|
|
const timer = setTimer(() => {
|
|
settle(requestId, {
|
|
ok: false,
|
|
message: `The in-app browser did not respond within ${Math.round(boundedTimeout / 1000)}s`,
|
|
status: 504,
|
|
});
|
|
}, boundedTimeout);
|
|
|
|
pending.set(requestId, { finish, timer, claimed: false });
|
|
});
|
|
},
|
|
|
|
/**
|
|
* Grants the right to perform one request, to one client.
|
|
*
|
|
* The first caller wins; everyone else is told no and must do nothing. An
|
|
* unknown id is also a refusal: the request has already been settled, and
|
|
* acting on it now would change a page nobody is waiting on.
|
|
*/
|
|
claim(requestId) {
|
|
if (typeof requestId !== 'string' || !requestId) return false;
|
|
const entry = pending.get(requestId);
|
|
if (!entry || entry.claimed) return false;
|
|
entry.claimed = true;
|
|
return true;
|
|
},
|
|
|
|
/**
|
|
* Accepts a result posted by the client. Returns false for an unknown id,
|
|
* which is the normal outcome for a response that lost a race with the
|
|
* timeout and must not be treated as an error.
|
|
*/
|
|
resolve(requestId, result) {
|
|
if (typeof requestId !== 'string' || !requestId) return false;
|
|
if (result && result.ok === true) {
|
|
return settle(requestId, { ok: true, data: result.data ?? null });
|
|
}
|
|
return settle(requestId, {
|
|
ok: false,
|
|
message: typeof result?.error === 'string' && result.error ? result.error : 'Browser action failed',
|
|
status: 400,
|
|
});
|
|
},
|
|
|
|
/** Fails everything in flight, e.g. when the owning client disconnects. */
|
|
rejectAll(message) {
|
|
for (const requestId of [...pending.keys()]) {
|
|
settle(requestId, { ok: false, message, status: 503 });
|
|
}
|
|
},
|
|
};
|
|
};
|