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

+32 -2
View File
@@ -8,6 +8,9 @@ export const CONNECTION_KEY = "pi2-connection";
export const ENDPOINT_KEY = "pi2-endpoint";
export const DEVICE_KEY = "pi2-device";
export const CONNECTED_AT_KEY = "pi2-connected-at";
export const SCOPES_KEY = "pi2-device-scopes";
/** Scope assumed for devices paired before scopes were persisted. */
export const DEFAULT_CONNECTION_SCOPES = ["read"] as const;
function read(key: string): string | null {
try {
@@ -33,6 +36,26 @@ function drop(key: string): void {
}
}
/** Persisted device scopes; defaults to `["read"]` when absent or unreadable. */
function readScopes(): string[] {
const raw = read(SCOPES_KEY);
if (!raw) return [...DEFAULT_CONNECTION_SCOPES];
try {
const parsed: unknown = JSON.parse(raw);
if (Array.isArray(parsed)) {
const scopes = parsed.filter((item): item is string => typeof item === "string" && item.trim() !== "");
return scopes.length ? scopes : [...DEFAULT_CONNECTION_SCOPES];
}
} catch {
/* fall through to the default */
}
return [...DEFAULT_CONNECTION_SCOPES];
}
function writeScopes(scopes: string[]): void {
write(SCOPES_KEY, JSON.stringify(scopes.length ? scopes : DEFAULT_CONNECTION_SCOPES));
}
export type PairingState = "paired" | "unpaired";
export interface ConnectionStore {
@@ -42,12 +65,14 @@ export interface ConnectionStore {
deviceName: string;
connectedAt: number;
token: string | null;
/** Device scopes granted by the daemon when this browser was paired. */
scopes: string[];
server: InfoResponse | null;
wsState: WsConnectionState;
isPaired: () => boolean;
refreshFromStorage: () => void;
connect: (options?: { endpoint?: string; deviceName?: string; token?: string | null }) => void;
connect: (options?: { endpoint?: string; deviceName?: string; token?: string | null; scopes?: string[] }) => void;
disconnect: () => void;
setServer: (server: InfoResponse | null) => void;
setWsState: (state: WsConnectionState) => void;
@@ -61,6 +86,7 @@ function initialState() {
deviceName: read(DEVICE_KEY) ?? "",
connectedAt: Number(read(CONNECTED_AT_KEY)) || 0,
token: getDeviceToken(),
scopes: readScopes(),
server: null as InfoResponse | null,
wsState: "idle" as WsConnectionState,
};
@@ -73,11 +99,12 @@ export const useConnectionStore = create<ConnectionStore>((set, get) => ({
refreshFromStorage: () => set(initialState()),
connect: ({ endpoint, deviceName, token } = {}) => {
connect: ({ endpoint, deviceName, token, scopes } = {}) => {
const now = Date.now();
if (endpoint) write(ENDPOINT_KEY, endpoint);
if (deviceName) write(DEVICE_KEY, deviceName);
if (token !== undefined) setDeviceToken(token);
if (scopes !== undefined) writeScopes(scopes);
write(CONNECTED_AT_KEY, String(now));
write(CONNECTION_KEY, "paired");
set({
@@ -86,6 +113,7 @@ export const useConnectionStore = create<ConnectionStore>((set, get) => ({
deviceName: deviceName ?? get().deviceName,
connectedAt: now,
token: getDeviceToken(),
scopes: scopes !== undefined ? readScopes() : get().scopes,
});
},
@@ -93,6 +121,7 @@ export const useConnectionStore = create<ConnectionStore>((set, get) => ({
drop(ENDPOINT_KEY);
drop(DEVICE_KEY);
drop(CONNECTED_AT_KEY);
drop(SCOPES_KEY);
setDeviceToken(null);
write(CONNECTION_KEY, "unpaired");
set({
@@ -101,6 +130,7 @@ export const useConnectionStore = create<ConnectionStore>((set, get) => ({
deviceName: "",
connectedAt: 0,
token: null,
scopes: [...DEFAULT_CONNECTION_SCOPES],
server: null,
wsState: "idle",
});
+29
View File
@@ -11,6 +11,8 @@ export interface SessionsStore {
setSessions: (workspaceId: string, sessions: SessionState[]) => void;
upsertSession: (session: SessionState) => void;
/** Re-key a session after the plugin migrates `tmp-N` → the real id. */
renameSessionId: (from: string, to: string) => void;
removeSession: (sessionId: string) => void;
selectSession: (sessionId: string | null) => void;
listFor: (workspaceId: string) => SessionState[];
@@ -44,6 +46,33 @@ export const useSessionsStore = create<SessionsStore>((set, get) => ({
return { byWorkspace: { ...state.byWorkspace, [session.workspaceId]: next } };
}),
renameSessionId: (from, to) =>
set((state) => {
if (!from || !to || from === to) return state;
const byWorkspace: Record<string, SessionState[]> = {};
let moved = false;
for (const [workspaceId, list] of Object.entries(state.byWorkspace)) {
const index = list.findIndex((session) => session.sessionId === from);
if (index === -1) {
byWorkspace[workspaceId] = list;
continue;
}
moved = true;
byWorkspace[workspaceId] = list
.map((session, position) =>
position === index ? { ...session, sessionId: to } : session,
)
// A row already cached under the new id would now duplicate the
// migrated one (the REST list can be ahead of the event).
.filter((session, position) => position === index || session.sessionId !== to);
}
if (!moved) return state;
return {
byWorkspace,
selectedSessionId: state.selectedSessionId === from ? to : state.selectedSessionId,
};
}),
removeSession: (sessionId) =>
set((state) => {
const byWorkspace: Record<string, SessionState[]> = {};