From fccf415283c7658e931210199d370db76f2dc82a Mon Sep 17 00:00:00 2001 From: Xubin Ren <52506698+Re-bin@users.noreply.github.com> Date: Wed, 23 Sep 2026 14:04:30 +0800 Subject: [PATCH] feat(webui): add isolated website previews and link actions --- docs/webui.md | 36 +++++ webui/src/components/MarkdownTextRenderer.tsx | 9 +- webui/src/components/WebLink.tsx | 66 +++++++++ webui/src/components/WebPreviewPanel.tsx | 56 ++++++++ webui/src/components/thread/ThreadShell.tsx | 48 ++++++- webui/src/hooks/useFilePreviewState.ts | 10 +- webui/src/i18n/locales/en/common.json | 22 +++ webui/src/i18n/locales/es/common.json | 22 +++ webui/src/i18n/locales/fr/common.json | 22 +++ webui/src/i18n/locales/id/common.json | 22 +++ webui/src/i18n/locales/ja/common.json | 22 +++ webui/src/i18n/locales/ko/common.json | 22 +++ webui/src/i18n/locales/pt-BR/common.json | 22 +++ webui/src/i18n/locales/vi/common.json | 22 +++ webui/src/i18n/locales/zh-CN/common.json | 22 +++ webui/src/i18n/locales/zh-TW/common.json | 22 +++ webui/src/lib/web-preview.ts | 24 ++++ .../tests/temporary-chat-navigation.test.tsx | 39 +++++- webui/src/tests/web-preview.test.tsx | 131 ++++++++++++++++++ 19 files changed, 626 insertions(+), 13 deletions(-) create mode 100644 webui/src/components/WebLink.tsx create mode 100644 webui/src/components/WebPreviewPanel.tsx create mode 100644 webui/src/lib/web-preview.ts create mode 100644 webui/src/tests/web-preview.test.tsx diff --git a/docs/webui.md b/docs/webui.md index da558f17e..bf30b8e54 100644 --- a/docs/webui.md +++ b/docs/webui.md @@ -438,6 +438,42 @@ private, trusted network. ## Troubleshooting +### Links and website previews + +HTTP(S) links in replies have a **Link actions** menu: right-click the link, +press Shift+F10 while it is focused, or use its ellipsis button. You can copy the +link, open it in a browser tab, or preview it beside the current conversation. +Ordinary clicks still open links in a browser tab. File and session links keep +their own behavior. + +The sidebar shares space with file previews and has refresh, external-open and +close controls. Escape closes it when focus is in the nanobot page, unless a +menu or dialog handles Escape first. Once focus is inside a third-party page, +use the sidebar's close button. The opened address and width are remembered +per session for this app connection, not written to browser storage. Returning +to a session reloads the original address; it does not preserve the website's +DOM, navigation history or forms. The header shows the **original link**, not a +live address bar for navigation inside the embedded page. + +This is a restricted preview, not a full browser. It requires browser support +for credentialless iframes and is disabled in the native host until a separate +untrusted-content boundary is available. Unsupported browsers can still copy +links and open new tabs. Previews use an opaque-origin sandbox with scripts +but without same-origin access, forms, popups, downloads or top navigation. +No gateway token, host bridge or parent storage is passed into the frame. + +Sites may refuse embedding via CSP or X-Frame-Options, and sign-in or some +interactive features may not work. Use **Open in browser** if the frame is blank +or reports a failure. The browser does not reliably expose an embedding failure +to nanobot, so a frame load event is not presented as a success signal. Nanobot +does not proxy pages or bypass their headers. Same-origin nanobot URLs and +HTTP pages embedded from an HTTPS WebUI are not previewed. + +`localhost` and loopback addresses refer to the device running your browser, +not a remote nanobot gateway. This feature does not forward remote ports. + +### Connection checks + If the page does not open, check these in order: 1. `nanobot agent -m "Hello!"` works in the same Python environment. diff --git a/webui/src/components/MarkdownTextRenderer.tsx b/webui/src/components/MarkdownTextRenderer.tsx index 5f8849a09..1d65c74f2 100644 --- a/webui/src/components/MarkdownTextRenderer.tsx +++ b/webui/src/components/MarkdownTextRenderer.tsx @@ -15,6 +15,7 @@ import { Streamdown, type Components, type StreamdownProps } from "streamdown"; import { AttachmentTile } from "@/components/AttachmentTile"; import { CodeBlock } from "@/components/CodeBlock"; +import { WebLink } from "@/components/WebLink"; import { INLINE_TOKEN_HIGHLIGHT_COLOR, InlineTokenHighlight, @@ -451,7 +452,7 @@ function InlineLinkPreviewRow({ link }: { link: InlineLinkPreview }) { : link.title; return ( - {label} - + ); } @@ -672,7 +673,7 @@ export default function MarkdownTextRenderer({ return <>{markdownChildren}; } return ( - {markdownChildren} - + ); }, // Streamdown decorates emphasis with spans by default. Preserve native diff --git a/webui/src/components/WebLink.tsx b/webui/src/components/WebLink.tsx new file mode 100644 index 000000000..74e869e13 --- /dev/null +++ b/webui/src/components/WebLink.tsx @@ -0,0 +1,66 @@ +import { createContext, useContext, useEffect, useState, type ComponentPropsWithoutRef } from "react"; +import { Copy, ExternalLink, MoreHorizontal, PanelRight } from "lucide-react"; +import { useTranslation } from "react-i18next"; + +import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuLabel, DropdownMenuTrigger } from "@/components/ui/dropdown-menu"; +import { copyTextToClipboard } from "@/lib/clipboard"; +import { parseWebLink } from "@/lib/web-preview"; + +export const WebPreviewContext = createContext<((url: string) => void) | undefined>(undefined); + +export function WebLink({ href = "", children, ...props }: ComponentPropsWithoutRef<"a">) { + const { t } = useTranslation(); + const openPreview = useContext(WebPreviewContext); + const [open, setOpen] = useState(false); + const [feedback, setFeedback] = useState(""); + useEffect(() => { + if (!feedback) return; + const timer = window.setTimeout(() => setFeedback(""), 4000); + return () => window.clearTimeout(timer); + }, [feedback]); + const url = parseWebLink(href); + const anchor = {children}; + if (!url) { + // Keep the renderer's relative media/document and mail links unchanged. + const relative = !/^[a-z][a-z\d+.-]*:/i.test(href) + && !href.includes("\\") && !Array.from(href).some((char) => char.charCodeAt(0) < 32); + return relative || /^(mailto:|tel:|#)/i.test(href) ? anchor : <>{children}; + } + return ( + + { + event.preventDefault(); event.stopPropagation(); setOpen(true); + }} onKeyDown={(event) => { + if ((event.shiftKey && event.key === "F10") || event.key === "ContextMenu") { + event.preventDefault(); event.stopPropagation(); setOpen(true); + } + }}> + {anchor} + + + + + + {url.host} + {openPreview ? openPreview(url.href)}> + {t("webPreview.open")} + : null} + + + {t("webPreview.external")} + + + { + // Keep the menu/focus alive until the clipboard fallback finishes. + event.preventDefault(); + setFeedback(""); + void copyTextToClipboard(url.href).then((copied) => setFeedback(t(copied ? "webPreview.copied" : "webPreview.copyFailed"))); + }}>{t("webPreview.copy")} + {feedback ?
{feedback}
: null} +
+
+ ); +} diff --git a/webui/src/components/WebPreviewPanel.tsx b/webui/src/components/WebPreviewPanel.tsx new file mode 100644 index 000000000..32ffe5264 --- /dev/null +++ b/webui/src/components/WebPreviewPanel.tsx @@ -0,0 +1,56 @@ +import { useState, type CSSProperties, type PointerEvent } from "react"; +import { ExternalLink, RotateCw, X } from "lucide-react"; +import { useTranslation } from "react-i18next"; + +import { isLoopbackHost } from "@/lib/network"; +import { isNativeRuntime } from "@/lib/runtime"; +import { parseWebLink, webPreviewRestriction } from "@/lib/web-preview"; + +interface WebPreviewPanelProps { + url: string; + desktopWidth?: number; + onResizeStart?: (event: PointerEvent) => void; + onClose: () => void; +} + +export function WebPreviewPanel({ url: value, desktopWidth = 544, onResizeStart, onClose }: WebPreviewPanelProps) { + const { t } = useTranslation(); + const [revision, setRevision] = useState(0); + const url = parseWebLink(value); + const restriction = url ? webPreviewRestriction(url, new URL(window.location.href), isNativeRuntime(), + "credentialless" in HTMLIFrameElement.prototype) : "invalid"; + const buttonClass = "inline-flex h-8 w-8 shrink-0 items-center justify-center rounded-md text-muted-foreground hover:bg-muted hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"; + return ( +