From 02dfb618b98db0bf2526ca8a2ba7632ecc192b6e Mon Sep 17 00:00:00 2001 From: YokiiroBW Date: Wed, 22 Jul 2026 00:27:06 +0800 Subject: [PATCH] refactor(ui): complete desktop frontend redesign --- docs/progress.md | 37 + docs/reports/ui-audit-2026-07-21.md | 62 ++ docs/ui-redesign-plan.md | 65 ++ src/App.test.tsx | 19 +- src/App.tsx | 621 +++----------- src/features/chat/ChatPanel.tsx | 33 +- src/features/chat/StatusBubble.tsx | 36 +- src/features/pet/DesktopPetView.tsx | 242 ++++++ src/features/pet/PetPackImporter.tsx | 21 +- .../profiles/CredentialEnrollmentCard.tsx | 187 ++++- src/pages/SettingsPages.tsx | 277 ++++++ src/styles.css | 791 ++++++++++++++++++ tests/e2e/bootstrap.spec.ts | 18 +- tests/e2e/ui-polish.spec.ts | 43 + 14 files changed, 1884 insertions(+), 568 deletions(-) create mode 100644 docs/reports/ui-audit-2026-07-21.md create mode 100644 docs/ui-redesign-plan.md create mode 100644 src/features/pet/DesktopPetView.tsx create mode 100644 src/pages/SettingsPages.tsx create mode 100644 tests/e2e/ui-polish.spec.ts diff --git a/docs/progress.md b/docs/progress.md index 7e7388d..fb4d404 100644 --- a/docs/progress.md +++ b/docs/progress.md @@ -308,3 +308,40 @@ Still not claimed complete: automatic startup activation, real Tauri multi-windo - Added responsive runtime metadata and chat layout refinements; preserved the existing pet event bus for Q13 state synchronization. - Updated frontend test mocks for runtime status schema changes. - Validation: Vitest (6), TypeScript, ESLint, and targeted Prettier checks pass. +## 2026-07-21: frontend UI audit and chat-page visual increment + +- Completed the Impeccable audit and recorded the P0-P3 findings in `docs/reports/ui-audit-2026-07-21.md`. +- Recorded the P0-P2 redesign queue and preserved contracts in `docs/ui-redesign-plan.md`. +- Refined the chat page visual layer in `src/styles.css` only: added shared UI tokens, visible focus states, clearer slot/runtime hierarchy, calmer surfaces, responsive composer layout, and reduced-motion fallback. +- No IPC command, event name, route key, profile/slot/session/message schema, or security boundary changed. +- Frontend validation after the increment: typecheck passed, lint passed, Vitest 6 passed, Vite build passed; build still reports the existing large JS chunk warning. +- Browser preview at 1280x720 confirmed no horizontal overflow on the chat page. In-browser IPC fallback remains expected outside Tauri. + +## 2026-07-21: chat page accessibility and polish increment + +- Added chat slot tab relationships, keyboard left/right switching, selected-tab focus order, message tabpanel semantics, busy state, and alert semantics for send/runtime errors. +- Kept the existing chat state, invoke/listen calls, session switching, attachment upload, voice input, retry, copy, read-aloud, and stop-generation interactions unchanged. +- Refined chat-only typography, scroll behavior, focus treatment, disabled states, voice active state, file input focusability, touch targets, and narrow-window layout without adding motion. +- Frontend validation passed: Prettier on `src/features/chat/ChatPanel.tsx`, typecheck, lint, Vitest 6 tests, and Vite production build. +- Browser preview passed at 1280x720 with no horizontal overflow; slot arrow-key switching selected the adjacent tab. Existing pet overlay/navigation overlap remains outside this batch. + +## 2026-07-21: P1-P3 frontend hardening increment + +- Added lazy-loaded chunks for ProfileManager and PetPackImporter, with a status fallback and async test waiting. +- Added visible diagnostic field labels, global IPC alert semantics, and a safe desktop-pet left boundary that keeps the main navigation clear on desktop widths. +- Consolidated the visual layer through shared UI tokens, Windows-native typography, reduced card shadows, calmer surfaces, consistent focus/disabled/error states, and narrow-window session layout. +- Preserved existing routes, page keys, IPC command/event names, connection slots, profile/session/message data, and security boundaries. +- Validation: typecheck, lint, Vitest 6 tests, targeted Prettier checks, Vite build, and 1280x720 browser preview passed. Build now emits ProfileManager and PetPackImporter async chunks; the existing large main JS chunk warning remains. +- Normalized persisted in-app pet coordinates on mount and window resize so stale positions cannot remain over the sidebar after an upgrade or window-size change. +- Final frontend validation after the normalization: targeted Prettier, typecheck, lint, Vitest 6, `git diff --check`, and Vite production build passed. Node 24.14.0 still emits the repository's existing engine warning for the `>=24.17.0` requirement. + +## 2026-07-22: complete frontend UI redesign queue P1-P3 + +- Completed P1 core-page work: ProfileManager credential form labels and feedback, StatusBubble semantic status tones, DesktopBehaviorPage grouping, DiagnosticsPage field labels and async feedback. +- Completed P2 code splitting: moved DesktopBehaviorPage and DiagnosticsPage to `src/pages/SettingsPages.tsx`; moved DesktopPetView and sprite loading to `src/features/pet/DesktopPetView.tsx`; kept ProfileManager and PetPackImporter lazy-loaded. +- Reduced the initial JavaScript chunk from approximately 5.54 MB raw / 4.02 MB gzip to approximately 305.93 kB raw / 91.43 kB gzip. The pet renderer remains an intentional lazy chunk because its inline sprite asset is approximately 5.23 MB. +- Completed P2 consistency work: added shared status colors, numeric typography, responsive 1280/1024/680px rules, reduced-motion fallback, async diagnostic status feedback, visible focus and disabled states, and bounded diagnostic result rendering. +- Added `tests/e2e/ui-polish.spec.ts` for desktop shell overflow checks at 1280px, 1024px, and 680px plus diagnostic keyboard accessibility. +- Completed P3 polish pass for desktop shell hierarchy, status messaging, profile credentials, diagnostics, desktop pet boundary, empty/error/loading states, and dark/light token usage without adding marketing-style animation. +- Validation: targeted Prettier, ESLint, TypeScript, Vitest 6/6, Vite production build, Playwright 5/5, Rust fmt, Clippy with warnings denied, Rust workspace tests (67), and Rust workspace check all passed. +- Known non-blocking warnings: Node is `24.14.0` while the package requires `>=24.17.0`; Vite reports the intentional lazy `DesktopPetView` chunk above 500 kB. diff --git a/docs/reports/ui-audit-2026-07-21.md b/docs/reports/ui-audit-2026-07-21.md new file mode 100644 index 0000000..bd76119 --- /dev/null +++ b/docs/reports/ui-audit-2026-07-21.md @@ -0,0 +1,62 @@ +# AstrBot Desktop 前端 UI 审计 + +日期:2026-07-21 +范围:`src/App.tsx`、`src/styles.css`、`src/features/**`、前端测试与构建配置 + +## 审计方式 + +- 读取 `docs/01_Architecture_Design.md`、`docs/02_Codex_Implementation_Tasks.md` 和现有前端实现。 +- 按 Impeccable technical audit 的 Accessibility、Performance、Theming、Responsive、Anti-patterns 五个维度检查。 +- 运行本地 detector:`node C:\Users\Administrator\.agents\skills\impeccable\scripts\detect.mjs --json src/App.tsx src/styles.css src/features`;未返回结构化命中项,因此以下结论以源码和基线检查为准。 +- 基线:`pnpm run typecheck`、`pnpm run lint`、`pnpm run test`、`pnpm run build` 均通过;Node 版本有 `24.14.0` vs 要求 `>=24.17.0` 的警告。 + +## Health score + +| 维度 | 分数 | 结论 | +| ------------- | --------: | --------------------------------------------------------------------------------------- | +| Accessibility | 2/4 | 已有主导航、部分 ARIA 和 live region,但焦点、键盘 tab 语义、表单标签及窗口控件仍不完整 | +| Performance | 2/4 | 聊天状态集中在大组件,生产 JS gzip 约 4.0 MB,存在按页面拆分和减少无关重渲染空间 | +| Theming | 2/4 | 有 dark/light 主题,但大量颜色、渐变和阴影直接写在 CSS,主题 token 未形成单一来源 | +| Responsive | 2/4 | 有 900/760/680px 断点,但聊天会话列表、工具栏、桌面窗口尺寸和 44px 触控目标仍需验证 | +| Anti-patterns | 2/4 | 不是营销页式 AI slop,但存在渐变背景、重复卡片、过度圆角/阴影和全局大标题层级偏重 | +| **总分** | **10/20** | **Acceptable:需要系统性重构,但不需要改写业务架构** | + +## P0:阻塞项 + +当前没有确认会阻塞任务完成的 P0 功能问题。 + +## P1:发布前必须处理 + +1. **聊天页信息层级不稳定** — `src/features/chat/ChatPanel.tsx:577`;运行时状态、槽位切换、会话列表、消息流和输入区堆在同一面板,主任务入口不够突出。 +2. **主题令牌不完整** — `src/styles.css:1`、`src/styles.css:1804`;深浅色主题依赖大量重复选择器和硬编码色值,新增页面容易产生视觉漂移。 +3. **焦点和键盘状态不完整** — `src/styles.css:246` 只覆盖输入框/选择框;导航按钮、槽位 tab、消息操作和窗口控件缺少统一 `:focus-visible` 样式。 +4. **表单标签与可读性不一致** — `src/App.tsx:945`、`src/App.tsx:950`、`src/App.tsx:973` 使用 `aria-label` 代替可见标签;`CredentialEnrollmentCard` 的 label 结构也需要统一间距和错误提示。 +5. **聊天构建体积过大** — `pnpm run build` 输出 `index-D8Ql9rJN.js` 约 5.6 MB(gzip 约 4.0 MB);页面级懒加载和资源边界应纳入 P2,若继续增加桌宠/诊断功能则应提前拆分。 +6. **状态反馈缺少统一视觉语义** — 连接、加载、错误、空会话和上传状态分散在多个 class,颜色和文案没有统一 token/状态等级。 +7. **桌宠浮层遮挡主导航** — 浏览器预览中 pp-pet-overlay 覆盖左侧导航项,桌宠可见时会降低页面可操作性;应限制浮层层级/停靠区,或提供不遮挡导航的安全边界。 + +## P2:下一批处理 + +1. 统一桌面工作区的密度、间距、圆角和阴影,减少重复 `card` 包裹。 +2. 修正 680px 以下布局:会话列表、聊天工具条、输入框按钮和长文本溢出。 +3. 给所有异步按钮补充进行中、失败、恢复动作和 `aria-live` 反馈。 +4. 给聊天消息操作、附件移除、窗口控制补充 tooltip/可见焦点,不依赖符号字符表达含义。 +5. 引入 `text-wrap: balance/pretty`、`font-variant-numeric: tabular-nums` 和统一字体回退链。 +6. 增加 `prefers-reduced-motion` 覆盖,保留桌宠状态动画但避免普通 UI 无限动画扩散。 + +## 不可改变的产品边界 + +- 技术栈继续使用 Tauri 2、React、TypeScript、Vite、Rust、SQLite、REST + SSE。 +- 保留页面 key:`chat`、`connections`、`desktop`、`pet`、`diagnostics`。 +- 不改变 Tauri IPC command/event 名称、Rust 权限边界、profile/slot/session/message 数据结构。 +- 不改变 `companion` 与 `operations` 两个槽位的隔离规则,不把 `conversation_id` 单独作为本地唯一键。 +- 不让 React 直接读取凭据、任意文件或执行 Shell;不修改后端协议和数据库迁移。 + +## 首批建议 + +先重构 `ChatPage` / `ChatPanel` 的视觉层:保留所有 state、invoke/listen、session、attachment、voice 和发送逻辑,只调整信息分组、CSS token、状态展示和响应式布局。 + +## 外部技能可用性 + +- 已使用本地 Impeccable audit 规范与 detector。 +- 已读取本机 `redesign-existing-projects` 与 `design-taste-frontend` 技能说明,并将其约束应用于方案;它们没有独立可执行命令或额外 CLI。 diff --git a/docs/ui-redesign-plan.md b/docs/ui-redesign-plan.md new file mode 100644 index 0000000..c206c4b --- /dev/null +++ b/docs/ui-redesign-plan.md @@ -0,0 +1,65 @@ +# AstrBot Desktop UI 重构方案 + +## Design read + +这是一个 Windows 桌面工具型应用,面向需要同时管理陪伴实例和运维实例的用户;视觉应偏安静、可信、信息密度可控的工作台,而不是营销网站或 SaaS 落地页。 + +## 设计原则 + +- 保留业务逻辑和交互路径,先重构视觉层和布局层。 +- 采用单一冷色中性 token 系统,蓝色只承担交互和连接状态,不使用紫蓝渐变。 +- 主任务优先:聊天输入和当前槽位最突出,诊断/连接元信息作为次级层。 +- 让错误、离线、空会话、上传中、重连中都能在同一位置理解和恢复。 +- 桌面优先,窄窗口可收缩;不把移动端营销布局套进桌面工具。 +- 普通 UI 不增加动画;桌宠既有状态动画保留,并加入 reduced-motion 兜底。 + +## 不变合同 + +| 范畴 | 保留内容 | +| ---- | ---------------------------------------------------------------------- | +| 页面 | `chat`、`connections`、`desktop`、`pet`、`diagnostics` | +| 连接 | `companion` / `operations` 独立 REST、SSE、Token、会话、消息、未读状态 | +| 数据 | `profile_id`、`server_instance_id`、`conversation_id` 及现有 schema | +| IPC | 现有 `invoke()` commands 和 `listen()` event names | +| 安全 | Rust 持有凭据和本机权限;React 不获得任意文件/命令执行能力 | +| 测试 | 保留现有 Vitest、Playwright、Rust workspace 检查入口 | + +## 分阶段队列 + +### P0 — 先稳定工作台骨架 + +- `src/styles.css`:抽取颜色、边框、间距、状态和 z-index tokens;补全统一 `:focus-visible`、disabled、error、loading 语义。 +- `src/App.tsx`:保持页面 key 和事件逻辑不变,清理页面头部层级与导航可读性。 +- `src/features/chat/ChatPanel.tsx`:把运行时状态、槽位 tabs、会话列表、消息流、composer 分成明确的语义区域。 +- 验证:Vitest、typecheck、lint、Vite build;聊天页新增关键交互回归断言。 + +### P1 — 完善核心页面 + +- `src/features/profiles/ProfileManager.tsx`、`CredentialEnrollmentCard.tsx`:统一表单布局、字段标签、错误/成功反馈和敏感信息说明。 +- `src/features/chat/StatusBubble.tsx`:统一连接状态颜色、读屏文本和小窗口窄尺寸。 +- `src/App.tsx` 的 `DesktopBehaviorPage`、`DiagnosticsPage`:减少重复卡片,建立明确的设置/诊断分组。 +- 验证:表单与槽位隔离测试、键盘导航检查、窄窗口 Playwright。 + +### P2 — 性能与一致性 + +- 页面级拆分 `App.tsx` 中非聊天视图,降低首屏 JS 体积。 +- 清理重复主题覆盖,保留 dark/light 两套可验证 token。 +- 统一消息、状态、按钮、表单的可访问名称与 tooltip;补齐 reduced motion。 +- 验证:生产构建体积对比、Playwright 截图/溢出检查、完整 Rust + frontend gate。 + +## 首批实施范围 + +只改聊天页视觉层,具体文件: + +- `src/features/chat/ChatPanel.tsx`:仅在需要时补充语义属性或可见标签,不改变数据处理。 +- `src/styles.css`:新增/调整 chat 相关 token 与布局规则,保留现有 class 兼容。 +- `src/App.tsx`:如需调整聊天页外壳,只改 `ChatPage` 的呈现结构,不改导航和 IPC。 +- `src/App.test.tsx`:补充聊天页视觉相关的可访问名称/状态回归,不重写现有业务测试。 + +## 完成标准 + +- 聊天发送、切槽位、切会话、附件、语音、重连、停止生成、打开快捷窗口行为不变。 +- 主聊天区域在 1280px、1024px、680px 窗口下无横向溢出。 +- 键盘能到达导航、槽位 tabs、会话、消息操作、输入和发送按钮,焦点可见。 +- 深浅色主题对比度满足 WCAG AA;错误和连接状态不只依赖颜色。 +- 前端 typecheck、lint、unit test、build 通过。 diff --git a/src/App.test.tsx b/src/App.test.tsx index 439c7d1..4bd6cad 100644 --- a/src/App.test.tsx +++ b/src/App.test.tsx @@ -1,4 +1,4 @@ -import { invoke } from "@tauri-apps/api/core"; +import { invoke } from "@tauri-apps/api/core"; import { fireEvent, render, screen, waitFor } from "@testing-library/react"; import { beforeEach, describe, expect, it, vi } from "vitest"; @@ -46,12 +46,13 @@ const emptyPolicy: ConnectionPolicyStatus = { duplicateServerInstanceWarnings: [], }; -function openConnectionsPage() { - fireEvent.click(screen.getByRole("button", { name: /连接/ })); +async function openConnectionsPage() { + fireEvent.click(screen.getByRole("button", { name: /◎连接/ })); + await screen.findByRole("heading", { level: 2 }, { timeout: 5000 }); } function openDiagnosticsPage() { - fireEvent.click(screen.getByRole("button", { name: /诊断与设置/ })); + fireEvent.click(screen.getByRole("button", { name: /⚙诊断与设置/ })); } function mockBaseCommands(options?: { @@ -140,7 +141,7 @@ describe("App", () => { openDiagnosticsPage(); expect(await screen.findByText("ready")).toBeInTheDocument(); expect(screen.getByText("desktop-v1alpha1")).toBeInTheDocument(); - openConnectionsPage(); + await openConnectionsPage(); expect(screen.getByText("连接 Profile")).toBeInTheDocument(); }); @@ -223,7 +224,7 @@ describe("App", () => { vi.spyOn(window, "confirm").mockReturnValue(true); render(); - openConnectionsPage(); + await openConnectionsPage(); expect(await screen.findByText("尚未保存 Profile。")).toBeInTheDocument(); fireEvent.change(screen.getByLabelText("显示名称"), { @@ -310,7 +311,7 @@ describe("App", () => { mockBaseCommands({ profiles }); render(); - openConnectionsPage(); + await openConnectionsPage(); fireEvent.change(await screen.findByLabelText("陪伴 Profile"), { target: { value: profiles[0].id }, @@ -364,7 +365,7 @@ describe("App", () => { }); render(); - openConnectionsPage(); + await openConnectionsPage(); expect(await screen.findByText("尚未保存 Profile。")).toBeInTheDocument(); fireEvent.change(screen.getByLabelText("显示名称"), { @@ -465,7 +466,7 @@ describe("App", () => { }); render(); - openConnectionsPage(); + await openConnectionsPage(); expect( await screen.findByLabelText("重复服务器实例提示"), diff --git a/src/App.tsx b/src/App.tsx index 6b0a126..f4ec5ca 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,17 +1,42 @@ import { invoke } from "@tauri-apps/api/core"; import { listen } from "@tauri-apps/api/event"; import { getCurrentWindow } from "@tauri-apps/api/window"; -import { useCallback, useEffect, useRef, useState } from "react"; +import { + lazy, + Suspense, + useCallback, + useEffect, + useRef, + useState, +} from "react"; import { ChatPanel } from "./features/chat/ChatPanel"; import { StatusBubble } from "./features/chat/StatusBubble"; -import { ProfileManager } from "./features/profiles/ProfileManager"; -import { PetPackImporter } from "./features/pet/PetPackImporter"; -import { - boundRuntimeEventSchema, - connectionRuntimeStatusSchema, -} from "./features/chat/types"; -import aemeathSpritesheet from "./features/pet/assets/aemeath/aemeath-spritesheet.png?inline"; +const ProfileManager = lazy(() => + import("./features/profiles/ProfileManager").then(({ ProfileManager }) => ({ + default: ProfileManager, + })), +); +const PetPackImporter = lazy(() => + import("./features/pet/PetPackImporter").then(({ PetPackImporter }) => ({ + default: PetPackImporter, + })), +); +const DesktopBehaviorPage = lazy(() => + import("./pages/SettingsPages").then(({ DesktopBehaviorPage }) => ({ + default: DesktopBehaviorPage, + })), +); +const DesktopPetView = lazy(() => + import("./features/pet/DesktopPetView").then(({ DesktopPetView }) => ({ + default: DesktopPetView, + })), +); +const DiagnosticsPage = lazy(() => + import("./pages/SettingsPages").then(({ DiagnosticsPage }) => ({ + default: DiagnosticsPage, + })), +); import { bootstrapStatusSchema, type BootstrapStatus } from "./types/bootstrap"; type PageKey = "chat" | "connections" | "desktop" | "pet" | "diagnostics"; @@ -27,22 +52,6 @@ const initialStatus: BootstrapStatus = { databaseSchemaVersion: 4, }; -const AEMEATH_SPRITESHEET = aemeathSpritesheet; -const PET_TARGET_SLOT = "companion" as const; - -const AEMEATH_FRAMES: Record< - "idle" | "working" | "resting" | "speaking" | "error", - number[] -> = { - idle: [0, 1, 2, 3, 4, 5], - working: [ - 80, 81, 82, 83, 84, 85, 86, 87, 104, 105, 106, 107, 108, 109, 110, 111, - ], - resting: [48, 49, 50, 51, 52, 53], - speaking: [72, 73, 74, 75, 76, 77, 78, 79], - error: [40, 41, 42, 43, 44, 45, 46, 47], -}; - const navigation: Array<{ key: PageKey; label: string; description: string }> = [ { key: "chat", label: "聊天", description: "陪伴与运维对话" }, @@ -179,44 +188,54 @@ export function App() { } > {page === "chat" ? null : } - {error ?

IPC 暂不可用:{error}

: null} - {page === "chat" ? : null} - {page === "connections" ? : null} - {page === "desktop" ? ( - { - setBubbleTarget(value); - localStorage.setItem("astrbot.bubbleTarget", value); - }} - onQuickTargetChange={(value) => { - setQuickTarget(value); - localStorage.setItem("astrbot.quickTarget", value); - }} - onOpenQuickChat={() => - void invoke("open_quick_chat_window", { slot: quickTarget }) - } - /> - ) : null} - {page === "pet" ? ( - { - setPetDisplayMode(value); - localStorage.setItem("astrbot.petDisplayMode", value); - }} - /> - ) : null} - {page === "diagnostics" ? ( - void refreshStatus()} - /> + {error ? ( +

+ IPC 暂不可用:{error} +

) : null} + }> + {page === "chat" ? : null} + {page === "connections" ? : null} + {page === "desktop" ? ( + { + setBubbleTarget(value); + localStorage.setItem("astrbot.bubbleTarget", value); + }} + onQuickTargetChange={(value) => { + setQuickTarget(value); + localStorage.setItem("astrbot.quickTarget", value); + }} + onOpenQuickChat={() => + void invoke("open_quick_chat_window", { slot: quickTarget }) + } + /> + ) : null} + {page === "pet" ? ( + { + setPetDisplayMode(value); + localStorage.setItem("astrbot.petDisplayMode", value); + }} + /> + ) : null} + {page === "diagnostics" ? ( + void refreshStatus()} + /> + ) : null} + - {petDisplayMode === "app-ui" ? : null} + {petDisplayMode === "app-ui" ? ( + + + + ) : null} ); } @@ -323,9 +342,9 @@ function InAppPetOverlay() { ); return saved && typeof saved.x === "number" && typeof saved.y === "number" ? saved - : { x: 24, y: 96 }; + : { x: window.innerWidth >= 720 ? 280 : 24, y: 96 }; } catch { - return { x: 24, y: 96 }; + return { x: window.innerWidth >= 720 ? 280 : 24, y: 96 }; } }); const dragRef = useRef<{ @@ -334,15 +353,34 @@ function InAppPetOverlay() { offsetY: number; } | null>(null); - const savePosition = (x: number, y: number) => { - const next = { - x: Math.max(0, Math.min(x, window.innerWidth - 188)), + const clampPosition = useCallback((x: number, y: number) => { + const safeLeft = window.innerWidth >= 720 ? 248 : 0; + const maxX = Math.max(safeLeft, window.innerWidth - 188); + return { + x: Math.max(safeLeft, Math.min(x, maxX)), y: Math.max(0, Math.min(y, window.innerHeight - 226)), }; + }, []); + + const savePosition = (x: number, y: number) => { + const next = clampPosition(x, y); setPosition(next); localStorage.setItem("astrbot.petPosition", JSON.stringify(next)); }; + useEffect(() => { + const normalizePosition = () => { + const next = clampPosition(position.x, position.y); + if (next.x === position.x && next.y === position.y) return; + setPosition(next); + localStorage.setItem("astrbot.petPosition", JSON.stringify(next)); + }; + + normalizePosition(); + window.addEventListener("resize", normalizePosition); + return () => window.removeEventListener("resize", normalizePosition); + }, [clampPosition, position.x, position.y]); + useEffect(() => { let unlisten: (() => void) | undefined; void listen("pet-hide-requested", () => setVisible(false)).then( @@ -400,6 +438,14 @@ function navIcon(page: PageKey) { }[page]; } +function PageLoading() { + return ( +

+ Loading? +

+ ); +} + function PageHeader({ page }: { page: PageKey }) { const current = navigation.find((item) => item.key === page) ?? navigation[0]; return ( @@ -427,103 +473,6 @@ function ChatPage() { ); } -function DesktopBehaviorPage({ - bubbleTarget, - quickTarget, - onBubbleTargetChange, - onQuickTargetChange, - onOpenQuickChat, -}: { - bubbleTarget: "main" | "quick"; - quickTarget: ChatSlot; - onBubbleTargetChange: (value: "main" | "quick") => void; - onQuickTargetChange: (value: ChatSlot) => void; - onOpenQuickChat: () => void; -}) { - const [quietHours, setQuietHours] = useState( - () => localStorage.getItem("astrbot.quietHours") ?? "off", - ); - return ( -
-
- - - - - - -
- - 气泡状态显示已预留 -
-
-
-
- Desktop companion -

后续桌面能力

-
- 托盘与单实例 - 窗口置顶开关 - 多显示器策略 - 游戏模式靠边活动 -
-

- 这些能力先保留为独立设置区,后续接入 Rust - 本机能力层,不会与聊天状态混在一起。 -

-
-
- - - -
-
- ); -} - function PetPage({ displayMode, onDisplayModeChange, @@ -691,343 +640,6 @@ function PetPage({ ); } -function DesktopPetView({ embedded = false }: { embedded?: boolean }) { - const [state, setState] = useState< - "idle" | "working" | "resting" | "speaking" | "error" - >("idle"); - const [frameIndex, setFrameIndex] = useState(0); - const [spriteReady, setSpriteReady] = useState(false); - const [spriteError, setSpriteError] = useState(null); - const [gameMode, setGameMode] = useState(false); - const spriteImageRef = useRef(null); - const spriteCanvasRef = useRef(null); - - useEffect(() => { - if (embedded) return; - document.documentElement.classList.add("pet-view"); - return () => document.documentElement.classList.remove("pet-view"); - }, [embedded]); - - const completionTimerRef = useRef(null); - - useEffect(() => { - let unlisten: (() => void) | undefined; - let unlistenRuntime: (() => void) | undefined; - void listen("pet-game-mode-toggle", () => - setGameMode((current) => !current), - ).then((cleanup) => { - unlisten = cleanup; - }); - void listen("connection-runtime-event", (event) => { - const parsed = boundRuntimeEventSchema.safeParse(event.payload); - if (!parsed.success || parsed.data.slot !== PET_TARGET_SLOT) return; - if (completionTimerRef.current !== null) { - window.clearTimeout(completionTimerRef.current); - completionTimerRef.current = null; - } - if (parsed.data.eventType === "message.delta") setState("working"); - if (parsed.data.eventType === "message.proactive") setState("speaking"); - if (parsed.data.eventType === "message.completed") { - setState("speaking"); - completionTimerRef.current = window.setTimeout(() => { - completionTimerRef.current = null; - setState("idle"); - }, 2200); - } - if (parsed.data.eventType === "error") setState("error"); - }).then((cleanup) => { - unlistenRuntime = cleanup; - }); - return () => { - unlisten?.(); - unlistenRuntime?.(); - if (completionTimerRef.current !== null) { - window.clearTimeout(completionTimerRef.current); - completionTimerRef.current = null; - } - }; - }, []); - - useEffect(() => { - const image = new Image(); - image.onload = () => { - spriteImageRef.current = image; - setSpriteError(null); - setSpriteReady(true); - }; - image.onerror = () => { - setSpriteError("????????"); - }; - image.src = AEMEATH_SPRITESHEET; - return () => { - image.onload = null; - }; - }, []); - - useEffect(() => { - let cancelled = false; - void invoke("connection_runtime_status") - .then((value) => { - if (cancelled) return; - try { - const result = connectionRuntimeStatusSchema.parse(value); - const state = result.companion.state; - if ( - state === "authExpired" || - state === "fatalError" || - state === "incompatible" - ) { - setState("error"); - } else if ( - state === "syncing" || - state === "connecting" || - state === "authenticating" - ) { - setState("working"); - } else if (state === "reconnecting") { - setState("speaking"); - } else if (state === "disconnected" || state === "offline") { - setState("resting"); - } else { - setState("idle"); - } - } catch { - setState("idle"); - } - }) - .catch(() => { - if (!cancelled) setState("idle"); - }); - return () => { - cancelled = true; - }; - }, []); - - useEffect(() => { - void invoke("set_pet_window_preferences", { - preferences: { - positionMode: "edge", - monitorIndex: 0, - alwaysOnTop: true, - gameMode, - }, - }).catch(() => undefined); - }, [gameMode]); - - useEffect(() => { - setFrameIndex(0); - const frames = AEMEATH_FRAMES[state]; - const timer = window.setInterval( - () => { - setFrameIndex((current) => (current + 1) % frames.length); - }, - state === "resting" ? 180 : 125, - ); - return () => window.clearInterval(timer); - }, [state]); - - const frame = - AEMEATH_FRAMES[state][frameIndex % AEMEATH_FRAMES[state].length]; - const frameRow = Math.floor(frame / 8); - const frameColumn = frame % 8; - - useEffect(() => { - const canvas = spriteCanvasRef.current; - const image = spriteImageRef.current; - if (!canvas || !image || !spriteReady) return; - const context = canvas.getContext("2d"); - if (!context) { - setSpriteError("???? Canvas ?????"); - return; - } - try { - context.clearRect(0, 0, 192, 208); - context.imageSmoothingEnabled = false; - context.drawImage( - image, - frameColumn * 192, - frameRow * 208, - 192, - 208, - 0, - 0, - 192, - 208, - ); - } catch (reason) { - setSpriteError(reason instanceof Error ? reason.message : "??????"); - } - }, [frameColumn, frameRow, spriteReady]); - - return ( -
{ - if (embedded || event.button !== 0) return; - void getCurrentWindow().startDragging(); - }} - onContextMenu={(event) => { - event.preventDefault(); - void invoke("show_pet_context_menu"); - }} - > -
- -
- {spriteError ? ( - {spriteError} - ) : null} - {!spriteReady && !spriteError ? ( - Loading pet... - ) : null} - - {state === "working" - ? "正在写写画画" - : state === "resting" - ? "休息中" - : state === "speaking" - ? "正在回应" - : state === "error" - ? "需要重新登录或检查连接" - : "陪伴中"} - -
- ); -} - -function DiagnosticsTools() { - const [result, setResult] = useState(""); - const [host, setHost] = useState("127.0.0.1"); - const [port, setPort] = useState("80"); - const [processId, setProcessId] = useState("0"); - const run = async (command: string, request?: Record) => { - try { - const value = await invoke( - command, - request ? { request } : undefined, - ); - setResult(JSON.stringify(value, null, 2)); - } catch (reason) { - setResult(reason instanceof Error ? reason.message : String(reason)); - } - }; - return ( - <> -
- - - - - setHost(event.target.value)} - aria-label="网络主机" - /> - setPort(event.target.value)} - aria-label="网络端口" - inputMode="numeric" - /> - - - - setProcessId(event.target.value)} - aria-label="进程 ID" - inputMode="numeric" - /> - - -
- {result ?
{result}
: null} - - ); -} - -function DiagnosticsPage({ - status, - onRefresh, -}: { - status: BootstrapStatus; - onRefresh: () => void; -}) { - return ( -
-
- - - - - -
- -
- Security boundary -

本机能力保持可审计

-
    -
  • 凭据只在 Rust 与 Windows 安全存储之间流动。
  • -
  • 两个 REST/SSE 槽位保持独立状态、会话、消息与未读数。
  • -
  • 本机操作必须经过白名单、角色、路径和用户授权。
  • -
  • 文件事务默认保留源文件,不静默覆盖目标。
  • -
-
- -
- ); -} - function SettingCard({ title, description, @@ -1048,16 +660,3 @@ function SettingCard({ ); } - -interface StatusCardProps { - label: string; - value: string; -} -function StatusCard({ label, value }: StatusCardProps) { - return ( -
- {label} - {value} -
- ); -} diff --git a/src/features/chat/ChatPanel.tsx b/src/features/chat/ChatPanel.tsx index 3a1a538..5aa03c9 100644 --- a/src/features/chat/ChatPanel.tsx +++ b/src/features/chat/ChatPanel.tsx @@ -592,6 +592,22 @@ export function ChatPanel({ fixedSlot }: ChatPanelProps) { onClick={() => setActiveSlot(slot)} role="tab" aria-selected={activeSlot === slot} + + aria-controls={`chat-panel-${slot}`} + + tabIndex={fixedSlot || activeSlot === slot ? 0 : -1} + + onKeyDown={(event) => { + if (event.key !== "ArrowRight" && event.key !== "ArrowLeft") + return; + + event.preventDefault(); + + setActiveSlot( + slot === "companion" ? "operations" : "companion", + ); + }} + type="button" > {slot === "companion" ? "陪伴" : "运维"} @@ -682,7 +698,14 @@ export function ChatPanel({ fixedSlot }: ChatPanelProps) { ))} ) : null} -
+
{visibleMessages.length === 0 ? (

No messages in this slot yet. Connect a Profile to start chatting. @@ -756,11 +779,16 @@ export function ChatPanel({ fixedSlot }: ChatPanelProps) { }) )}

- {error ?

{error}

: null} + {error ? ( +

+ {error} +

+ ) : null}