feat(webui): debug-console shared primitives, data layer, task manager

Phase 3 of the debug-console expansion — the Apple-HIG foundation the
new tabs build on (reusing the existing ui kit; new controls match its
idiom, no browser-native widgets).

API client + types:
- debug.invokeStream() reads /api/debug/invoke-stream via fetch + a body
  reader, parsing data:<json> SSE frames (header auth, cancellable).
- debug.upload() streams a file via XHR for real upload progress.
- DebugActionParam.role, DebugActionDoc.stream/invariants, FieldRole,
  DebugStreamFrame, DebugUploadResult.

Primitives:
- Tabs (sliding indicator + roving tabindex), Picker (searchable,
  windowed combobox with a raw-value escape hatch), JsonTree (collapsible
  response viewer, image-url preview, copy w/ http fallback, node cap),
  FileSource (URL / server-path / browser-upload tri-state), FaceGrid.

Infra:
- DebugTaskProvider + TaskBadge: app-level registry of in-flight upload/
  stream tasks so they survive in-app tab switches, with a global
  progress chip and a beforeunload guard; eviction never drops a running
  task. Wired into App.
- use-debug-contacts: lazy friend/group/member data layer, shared module
  cache + localStorage (version-stamped, avatars stripped to dodge quota)
  + TTL + request-id race guard, degrading to raw input on error.

Adversarially reviewed (no automated component tests): virtualization
math and SSE framing verified correct; fixed the stale-contacts-on-switch
member-selection hazard, the quota-kills-persistence issue, and several
low UX edges (active-row clamp, filter scroll reset, copy fallback).
This commit is contained in:
DrSmoothl committed 2026-06-30 16:53:43 +08:00
1 parent a7667f1cd5
commit c5611ed622
12 files changed
+1188 -2

No files matched your search

@@ -0,0 +1,110 @@
// DebugTaskContext — an app-level registry of in-flight debug-console tasks
// (file uploads, streaming invocations) so they survive tab/page switches
// WITHIN the app (their state lives above the page, not in the unmounting tab).
// A full page unload (refresh / close) can't be survived, so a beforeunload
// guard warns while any task runs. Finished tasks linger briefly for history.
import { createContext, useCallback, useContext, useEffect, useMemo, useState, type ReactNode } from 'react';
export type DebugTaskStatus = 'running' | 'done' | 'failed' | 'canceled';
export type DebugTaskKind = 'upload' | 'stream' | 'invoke';
export interface DebugTask {
id: string;
kind: DebugTaskKind;
label: string;
status: DebugTaskStatus;
/** 0..1 when determinate; undefined = indeterminate. */
progress?: number;
detail?: string;
startedAt: number;
endedAt?: number;
/** Cancel the underlying work (abort the fetch / xhr). */
cancel?: () => void;
}
interface DebugTaskContextValue {
tasks: DebugTask[];
/** Register a task; returns its id. */
start: (task: Omit<DebugTask, 'id' | 'status' | 'startedAt'> & { id?: string }) => string;
update: (id: string, patch: Partial<Omit<DebugTask, 'id'>>) => void;
/** Mark terminal (done/failed/canceled) and stamp endedAt. */
finish: (id: string, status: Exclude<DebugTaskStatus, 'running'>, detail?: string) => void;
remove: (id: string) => void;
clearFinished: () => void;
}
const Ctx = createContext<DebugTaskContextValue | null>(null);
let seq = 0;
const nextId = () => `dt-${Date.now().toString(36)}-${(seq++).toString(36)}`;
export function DebugTaskProvider({ children }: { children: ReactNode }) {
const [tasks, setTasks] = useState<DebugTask[]>([]);
const start = useCallback<DebugTaskContextValue['start']>((t) => {
const id = t.id ?? nextId();
setTasks((prev) => {
const next = [{ ...t, id, status: 'running' as const, startedAt: Date.now() }, ...prev];
if (next.length <= 50) return next;
// Over cap: evict the OLDEST finished tasks only — never drop a running
// task (that would orphan its finish()/cancel handle while it keeps going).
const running = next.filter((x) => x.status === 'running').length;
const finished = next.filter((x) => x.status !== 'running');
const drop = new Set(finished.slice(Math.max(0, 50 - running)).map((x) => x.id));
return next.filter((x) => !drop.has(x.id));
});
return id;
}, []);
const update = useCallback<DebugTaskContextValue['update']>((id, patch) => {
setTasks((prev) => prev.map((t) => (t.id === id ? { ...t, ...patch } : t)));
}, []);
const finish = useCallback<DebugTaskContextValue['finish']>((id, status, detail) => {
setTasks((prev) => prev.map((t) => (t.id === id ? { ...t, status, detail: detail ?? t.detail, endedAt: Date.now(), cancel: undefined } : t)));
}, []);
const remove = useCallback<DebugTaskContextValue['remove']>((id) => {
setTasks((prev) => prev.filter((t) => t.id !== id));
}, []);
const clearFinished = useCallback(() => {
setTasks((prev) => prev.filter((t) => t.status === 'running'));
}, []);
// beforeunload guard: a real page unload kills in-flight tasks, so warn while
// any task is still running (within-app navigation is safe — state is here).
useEffect(() => {
const anyRunning = tasks.some((t) => t.status === 'running');
if (!anyRunning) return;
const handler = (e: BeforeUnloadEvent) => { e.preventDefault(); e.returnValue = ''; };
window.addEventListener('beforeunload', handler);
return () => window.removeEventListener('beforeunload', handler);
}, [tasks]);
const value = useMemo<DebugTaskContextValue>(
() => ({ tasks, start, update, finish, remove, clearFinished }),
[tasks, start, update, finish, remove, clearFinished],
);
return <Ctx.Provider value={value}>{children}</Ctx.Provider>;
}
export function useDebugTasks(): DebugTaskContextValue {
const v = useContext(Ctx);
if (!v) throw new Error('useDebugTasks must be used within DebugTaskProvider');
return v;
}
/** Convenience: count + aggregate progress of running tasks, for the badge. */
export function useRunningTaskSummary(): { count: number; progress: number | null } {
const { tasks } = useDebugTasks();
return useMemo(() => {
const running = tasks.filter((t) => t.status === 'running');
if (running.length === 0) return { count: 0, progress: null };
const determinate = running.filter((t) => typeof t.progress === 'number');
const progress = determinate.length
? determinate.reduce((s, t) => s + (t.progress ?? 0), 0) / determinate.length
: null;
return { count: running.length, progress };
}, [tasks]);
}