fix(web): make the web console work end-to-end

- Pair through the public POST /api/v1/pair and persist the device scopes;
  keep the allow-listed next deep link. Rename the manual field to pairing code.
- Authenticate the WebSocket with the raw device token (strip Bearer) and
  recover from a 401 by clearing the connection once.
- Fix chat payloads: setModel/setThinking, attention/diff responses, subagent
  dispatch items, queue ids/mode, task output and WS resume since.
- Render exactly one assistant message per turn: one-to-one snapshot/history
  merge plus a stale-delta guard after message_end (fixes PONGONG).
- Fix dialogs (content above the overlay), unknown-session deep links
  (error + retry, no silent fallback), device list/revoke, status identity and
  live pairing code, reconnect banner and Playwright selectors.
This commit is contained in:
yuez committed 2026-10-21 20:42:19 +08:00
1 parent 5572ac0d84
commit 0ef019f58d
57 files changed
+3549 -684

No files matched your search

+83 -4
View File
@@ -1,4 +1,4 @@
import { useEffect } from "react";
import { useEffect, useRef } from "react";
import { useTranslation } from "react-i18next";
import { Icon, type IconName } from "@/components/icons";
@@ -6,11 +6,14 @@ import { LangMenu } from "@/components/shared/lang-menu";
import { ServerChip } from "@/components/shared/server-chip";
import { ThemeMenu } from "@/components/shared/theme-menu";
import { SidebarItem } from "@/components/ui/sidebar-item";
import { api } from "@/lib/api";
import { clearPairMeta } from "@/features/connect/pairing";
import { api, getDeviceToken, onUnauthorized } from "@/lib/api";
import { CONNECT_PATH, safeNext } from "@/lib/routing";
import { defaultWsUrl, PiWsClient, type WsConnectionState } from "@/lib/ws";
import { useConnectionStore } from "@/stores/connection";
import { useSessionsStore } from "@/stores/sessions";
import { useWorkspacesStore } from "@/stores/workspaces";
import { Link, Outlet, useRouterState } from "@tanstack/react-router";
import { Link, Outlet, useNavigate, useRouterState } from "@tanstack/react-router";
import { useQuery } from "@tanstack/react-query";
interface NavItem {
@@ -106,11 +109,19 @@ function NavLinks({ pathname }: { pathname: string }) {
export function AppShell() {
const { t } = useTranslation();
const navigate = useNavigate();
const pathname = useRouterState({ select: (state) => state.location.pathname });
const href = useRouterState({ select: (state) => state.location.href });
const paired = useConnectionStore((state) => state.paired);
const setServer = useConnectionStore((state) => state.setServer);
const wsState = useConnectionStore((state) => state.wsState);
const setWsState = useConnectionStore((state) => state.setWsState);
const disconnect = useConnectionStore((state) => state.disconnect);
const isConnect = pathname.startsWith("/connect");
// The chat page owns its own socket and already publishes wsState.
const ownsSocket = !pathname.startsWith("/chat");
const page = pageName(pathname);
const redirectingRef = useRef(false);
const info = useQuery({
queryKey: ["info"],
@@ -128,8 +139,62 @@ export function AppShell() {
document.body.dataset.page = page;
}, [page]);
// A fresh pairing re-arms the one-shot unauthorized redirect.
useEffect(() => {
if (paired) redirectingRef.current = false;
}, [paired]);
/**
* The daemon rejected the stored device token: drop the local connection
* (token + paired flags) and bounce once to `/connect` with the current page
* as `next`. The ref keeps a burst of parallel 401s from looping.
*/
useEffect(() => {
return onUnauthorized(() => {
if (redirectingRef.current) return;
redirectingRef.current = true;
disconnect();
clearPairMeta();
void navigate({ href: `${CONNECT_PATH}?next=${encodeURIComponent(safeNext(href))}`, replace: true });
});
}, [disconnect, href, navigate]);
/**
* Lightweight socket probe so `wsState` (shell banner, status card) is
* populated on pages that do not open a socket themselves. Chat skips it
* because `useChatSocket` already publishes the same state.
*/
useEffect(() => {
if (!paired || isConnect || !ownsSocket) return;
let active = true;
const client = new PiWsClient({
url: defaultWsUrl(),
token: getDeviceToken(),
handlers: {
onState: (state: WsConnectionState) => {
if (active) setWsState(state);
},
onError: (error) => {
if ((error as { code?: string }).code !== "E_UNAUTHORIZED") return;
if (redirectingRef.current) return;
redirectingRef.current = true;
disconnect();
clearPairMeta();
void navigate({ href: `${CONNECT_PATH}?next=${encodeURIComponent(safeNext(href))}`, replace: true });
},
},
});
client.connect();
return () => {
active = false;
client.close();
};
}, [disconnect, href, isConnect, navigate, ownsSocket, paired, setWsState]);
const connectionLost = paired && !isConnect && (wsState === "reconnecting" || wsState === "closed");
return (
<div className="shell bg-background text-foreground">
<div className="shell bg-background text-foreground" data-ws-state={wsState} data-paired={paired ? "true" : "false"}>
<header className="topbar">
<Link to="/workspaces" className="brand">
<img
@@ -151,6 +216,20 @@ export function AppShell() {
</div>
</header>
{connectionLost ? (
<div className="reconnect-banner" role="status" data-testid="reconnect-banner">
<span className="ico">
<Icon name="alert" />
</span>
<span className="reconnect-banner__text">
{t(wsState === "closed" ? "shell.connectionLost" : "shell.reconnecting")}
</span>
<Link to="/connect" className="btn btn--ghost btn--sm">
{t("connect.reconnect")}
</Link>
</div>
) : null}
<div className="body">
{isConnect ? null : (
<>
+13 -5
View File
@@ -38,10 +38,17 @@ export function Modal({
return (
<DialogPrimitive.Root open={open} onOpenChange={onOpenChange}>
<DialogPrimitive.Portal>
<DialogPrimitive.Overlay className={cn("modal-backdrop", className)} />
<DialogPrimitive.Content
className={cn("modal", size === "wide" && "modal--wide", "modal-content", contentClassName)}
>
{/*
* The content is a child of the backdrop: the backdrop is
* `position: fixed; z-index: 60`, so a sibling rendered after it
* still paints underneath and swallows every pointer event
* ("modal-backdrop intercepts pointer events"). Nesting keeps the
* flex centering and puts the panel above the scrim.
*/}
<DialogPrimitive.Overlay className={cn("modal-backdrop", className)}>
<DialogPrimitive.Content
className={cn("modal", "modal-content", size === "wide" && "modal--wide", contentClassName)}
>
<DialogPrimitive.Title asChild>
<h3>
<span>{title}</span>
@@ -57,7 +64,8 @@ export function Modal({
) : null}
{children}
{footer ? <div className="modal-actions">{footer}</div> : null}
</DialogPrimitive.Content>
</DialogPrimitive.Content>
</DialogPrimitive.Overlay>
</DialogPrimitive.Portal>
</DialogPrimitive.Root>
);