Files
plugins/packages/agent-console/web/src/preview/open.ts
T
qingnian108 270b228a96 agent-console 1.2.0: Preview tab, chat links, file chips, font size/width settings, live magpie theme (#582)
agent-console 1.2.0 by qingnian108 (#582): Preview tab, chat links, file chips, font size/width settings, live magpie theme.

Reviewed and run at c459773107caa20f7063d12b2133c230f91c066c (mote, macOS, Node 22, sandbox HOME):
- npm ci --workspaces=false; npm run check ok; npm test: 48 files, 339 passed, 17 skipped; npm run verify:build 5/5 OK; bun scripts/check.mjs agent-console ok.
- Only test/v1/ws.test.ts changed since the last reviewed head 66d1070 (the new "preview.open returns host from previewHost()" test, through registerMethods).
- Broke by hand: methods.ts `return { ...r, host: previewHost() }` -> `return r;` turns that test red (1 of 5); restored, green. Changing previewHost's body alone stays green, as the test compares to previewHost(); fix582.test.ts covers its values.
- Earlier reviews' findings (per-listener key host, nonce/canary, gateway port + identity check, mentions path, Darwin major from the daemon) were checked in the earlier rounds. The proxy listens on 127.0.0.1 only and answers only the keyed Host.
- Not run: preview-attack.mjs browser half at this head, Windows box.
2026-11-04 11:22:25 +08:00

42 lines
1.6 KiB
TypeScript

// Opening links from chat (1.2): local addresses go to the Preview tab, everything http(s) else to the system browser
// (the sandboxed page can't open popups, so target=_blank did nothing). Only a user's click gets here.
import { ws } from "../main";
import { useUi } from "../stores/ui";
import { usePreview, threadKey } from "../stores/preview";
import { localHref } from "../util/localLinks";
import { copyText } from "../components/Markdown";
import { toast } from "../components/Toast";
import { t } from "../i18n/index.js";
export function openPreview(url: string): void {
const ui = useUi.getState();
const threadId = ui.currentThreadId;
ui.openPanel("preview");
void usePreview.getState().navigate((m, p) => ws.call(m, p), threadKey(threadId), threadId, url, { push: true });
}
export async function openExternal(url: string): Promise<void> {
try {
const r = (await ws.call("app.openExternal", { url })) as { ok?: boolean };
if (r?.ok) return;
} catch {}
await copyText(url);
toast(t("links.externalFailed"));
}
/** Delegated click handler for chat content: handles <a> clicks, never lets the frame navigate away */
export function onChatLinkClick(e: React.MouseEvent | MouseEvent): void {
const target = e.target as HTMLElement | null;
const a = target?.closest?.("a");
if (!a) return;
const href = a.getAttribute("href") ?? "";
e.preventDefault();
const local = (a as HTMLElement).dataset.acLocal || localHref(href);
const modified = e.ctrlKey || e.metaKey;
if (local && !modified) {
openPreview(local);
return;
}
if (/^https?:\/\//i.test(local || href)) void openExternal(local || href);
}