From ceb7512bf23fadefe3835cd151af460f71905b7a Mon Sep 17 00:00:00 2001 From: YokiiroBW Date: Tue, 21 Jul 2026 14:43:14 +0800 Subject: [PATCH] feat(desktop): complete Q9 chat and stability increment --- CHANGELOG.md | 20 +- Cargo.lock | 6 +- Cargo.toml | 2 +- crates/desktop-protocol/src/astrbot.rs | 91 +- docs/progress.md | 290 +++--- docs/q9-local-acceptance.md | 34 + package.json | 2 +- src-tauri/src/database/mod.rs | 131 ++- src-tauri/src/diagnostics/network.rs | 11 +- src-tauri/src/lib.rs | 258 ++++- src-tauri/src/networking/commands.rs | 67 +- src-tauri/src/networking/gateway_client.rs | 1 + src-tauri/src/networking/runtime.rs | 161 ++- src-tauri/src/windows/foreground_window.rs | 4 +- src-tauri/src/windows/process.rs | 5 +- src-tauri/tauri.conf.json | 116 +-- src/App.tsx | 781 ++++++++++----- src/features/chat/ChatPanel.tsx | 256 +++-- src/features/chat/StatusBubble.tsx | 59 +- src/features/chat/types.ts | 12 +- src/features/pet/types.ts | 7 +- src/features/profiles/ProfileManager.tsx | 76 +- src/styles.css | 1028 ++++++++++++++++++-- src/test/setup.ts | 5 + src/types/profile.ts | 2 +- 25 files changed, 2764 insertions(+), 661 deletions(-) create mode 100644 docs/q9-local-acceptance.md diff --git a/CHANGELOG.md b/CHANGELOG.md index ebba587..0884b4c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,27 @@ # Changelog +## [0.1.0-alpha.2] - 2026-07-20 + +### Added + +- Q6-Q8 companion state, multi-monitor safe placement, read-only diagnostics, proactive-message handling, quiet hours, and Web Speech MVP. +- Q9 local session recovery marker and release validation checklist. + +### Fixed + +- Streaming chat history isolation and completed-message persistence across both slots. +- Tray exit, hidden-window cleanup, and release build packaging regressions. + +### Notes + +- This is an unsigned local Windows build. Automatic updates and code signing are not configured. + ## Unreleased ### Added + - AstrBot OpenAPI adapter foundation: SSE mapper, dual-slot runtime skeleton, credential enrollment IPC, transport-aware profiles (ADR-0005). -All notable changes to this project will be documented in this file. + All notable changes to this project will be documented in this file. The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and versions follow Semantic Versioning. @@ -42,4 +59,3 @@ The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and - Kept real UNC writes, signing keys, updater keys, and release secrets outside P00. - Rejected Token, password, API Key, URL credential, query-string credential, and unknown fields at the Profile IPC boundary. - Required explicit approval for insecure HTTP and kept slots, connections, and credential enrollment outside P01. - diff --git a/Cargo.lock b/Cargo.lock index ed68165..bf515ec 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -105,7 +105,7 @@ checksum = "7d902e3d592a523def97af8f317b08ce16b7ab854c1985a0c671e6f15cebc236" [[package]] name = "astrbot-desktop" -version = "0.1.0-alpha.1" +version = "0.1.0-alpha.2" dependencies = [ "desktop-protocol", "eventsource-stream", @@ -989,7 +989,7 @@ dependencies = [ [[package]] name = "desktop-protocol" -version = "0.1.0-alpha.1" +version = "0.1.0-alpha.2" dependencies = [ "schemars 1.2.1", "serde", @@ -2480,7 +2480,7 @@ dependencies = [ [[package]] name = "mock-gateway" -version = "0.1.0-alpha.1" +version = "0.1.0-alpha.2" dependencies = [ "async-stream", "axum", diff --git a/Cargo.toml b/Cargo.toml index f50e49c..1c2af67 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,7 @@ members = [ resolver = "3" [workspace.package] -version = "0.1.0-alpha.1" +version = "0.1.0-alpha.2" edition = "2024" rust-version = "1.97.1" license = "MIT OR Apache-2.0" diff --git a/crates/desktop-protocol/src/astrbot.rs b/crates/desktop-protocol/src/astrbot.rs index 650a82f..96a593f 100644 --- a/crates/desktop-protocol/src/astrbot.rs +++ b/crates/desktop-protocol/src/astrbot.rs @@ -265,6 +265,7 @@ impl AstrBotStreamMapper { } "user_message_saved" => Vec::new(), "plain" => self.map_plain(frame), + "complete" => self.map_complete(frame), "agent_stats" => { let detail = frame.data.to_string(); vec![self.event( @@ -287,7 +288,7 @@ impl AstrBotStreamMapper { "message_saved" | "end" => self.complete_if_needed(FinishReason::Stop), "error" => { let mut events = self.complete_if_needed(FinishReason::Error); - let message = string_from_value(&frame.data) + let message = text_from_value(&frame.data) .unwrap_or_else(|| "AstrBot stream error".to_owned()); events.push(self.event( frame.message_id, @@ -305,7 +306,7 @@ impl AstrBotStreamMapper { } fn map_plain(&mut self, frame: AstrBotStreamFrame) -> Vec { - let chunk = string_from_value(&frame.data).unwrap_or_default(); + let chunk = text_from_value(&frame.data).unwrap_or_default(); if chunk.is_empty() && frame.streaming.unwrap_or(false) { // Keep empty streaming markers out of the UI unless they carry text. return Vec::new(); @@ -330,6 +331,37 @@ impl AstrBotStreamMapper { self.emit_delta(chunk) } + fn map_complete(&mut self, frame: AstrBotStreamFrame) -> Vec { + if let Some(message_id) = frame.message_id.clone() { + if self + .active_message_id + .as_ref() + .is_some_and(|current| current != &message_id) + { + let mut events = self.complete_if_needed(FinishReason::Stop); + self.active_message_id = Some(message_id); + self.accumulated.clear(); + self.delta_index = 0; + self.saw_assistant_delta = false; + let final_text = text_from_value(&frame.data).unwrap_or_default(); + if !final_text.is_empty() { + self.accumulated = final_text; + self.saw_assistant_delta = true; + } + events.extend(self.complete_if_needed(FinishReason::Stop)); + return events; + } + self.active_message_id = Some(message_id); + } + + let final_text = text_from_value(&frame.data).unwrap_or_default(); + if !final_text.is_empty() && self.accumulated != final_text { + self.accumulated = final_text; + self.saw_assistant_delta = true; + } + self.complete_if_needed(FinishReason::Stop) + } + fn emit_delta(&mut self, chunk: String) -> Vec { if chunk.is_empty() { return Vec::new(); @@ -397,6 +429,22 @@ fn string_from_value(value: &Value) -> Option { } } +fn text_from_value(value: &Value) -> Option { + match value { + Value::String(text) => Some(text.clone()), + Value::Object(object) => ["text", "content", "message"] + .iter() + .find_map(|key| object.get(*key).and_then(text_from_value)), + Value::Array(items) => { + let text = items.iter().filter_map(text_from_value).collect::(); + (!text.is_empty()).then_some(text) + } + Value::Null => None, + Value::Number(number) => Some(number.to_string()), + Value::Bool(flag) => Some(flag.to_string()), + } +} + fn current_timestamp() -> String { // Protocol timestamps are opaque strings for consumers; keep a stable UTC-like form // without adding a time dependency to the protocol crate. @@ -461,6 +509,45 @@ mod tests { ); } + #[test] + fn maps_complete_frame_when_no_plain_chunks_are_emitted() { + let mut mapper = AstrBotStreamMapper::new("astrbot-test"); + let events = mapper + .push_line(r#"data: {"type":"complete","data":"A complete reply","streaming":true,"message_id":"m1"}"#) + .expect("parse"); + + let completed = events + .iter() + .find_map(|event| match &event.body { + GatewayEventBody::MessageCompleted(payload) => payload.content.clone(), + _ => None, + }) + .expect("completed"); + assert_eq!(completed, "A complete reply"); + } + + #[test] + fn complete_frame_reconciles_partial_plain_chunks_without_duplication() { + let mut mapper = AstrBotStreamMapper::new("astrbot-test"); + mapper + .push_line( + r#"data: {"type":"plain","data":"Hello","streaming":true,"message_id":"m1"}"#, + ) + .expect("parse"); + let events = mapper + .push_line(r#"data: {"type":"complete","data":"Hello world","streaming":true,"message_id":"m1"}"#) + .expect("parse"); + + let completed = events + .iter() + .find_map(|event| match &event.body { + GatewayEventBody::MessageCompleted(payload) => payload.content.clone(), + _ => None, + }) + .expect("completed"); + assert_eq!(completed, "Hello world"); + } + #[test] fn maps_error_frames() { let mut mapper = AstrBotStreamMapper::new("astrbot-test"); diff --git a/docs/progress.md b/docs/progress.md index 371f775..c87381a 100644 --- a/docs/progress.md +++ b/docs/progress.md @@ -1,72 +1,72 @@ -# Development Progress - -## Current phase - -P02 dual connections remains active on `feat/p02-dual-connections`. -The foundation has been adjusted for real AstrBot OpenAPI transport after live container probe. -Credential enrollment, dual-slot runtime skeleton, and AstrBot SSE mapping are implemented. -Full product chat UI activation and production identity enrollment remain open for acceptance. - -## Completed - -### Prior phases - -- P00 bootstrap merged. -- P01 foundation merged through Forgejo PR #2. -- P02 first increment: persistent dual slot assignment (`bcc08cf`). -- P02 second increment: auto-connect validation and operations switch guard (`34a563c`). - -### Real-protocol foundation (this session) - -- Reviewed local AstrBot clone and live probe target `http://192.168.31.210:26285`. -- Confirmed API Key chat SSE works; dual usernames/session ids isolate cleanly. -- Added internal mapping module `desktop-protocol::astrbot` for OpenAPI SSE → Desktop events. -- Added `TransportKind` (`desktop-v1` | `astrbot-openapi`) plus `remote_username` / `sticky_session_id` profile fields. -- Migrated SQLite schema to v4. -- Added Rust `AstrBotClient` for probe/list/send chat stream. -- Added `DualSlotRuntime` with activate/probe/send/deactivate skeleton. -- Added bounded credential enrollment IPC (`enroll_profile_credential` / `clear_profile_credential`) using Windows Credential Manager; secrets never returned to React. -- Added frontend transport fields and credential enrollment card. -- Recorded ADR-0005 and live probe report. - -## In progress - -- User acceptance of real-protocol foundation. -- Next product increments: chat panel rendering, history persistence, JWT login path, stronger server identity UX. - -## Remaining - -- Wire auto-connect startup to DualSlotRuntime without weakening secret boundary. -- Dual chat UI panels consuming normalized runtime events. -- Message persistence for AstrBot mapped events. -- Dashboard JWT auth strategy. -- File attachment OpenAPI path. -- Certificate fingerprint binding. -- Full stage gate (fmt/clippy/typecheck/build/installer) before merge. - -## Blockers / reserved for user - -- Production API Key rotation after shared test key exposure in chat. -- Optional second AstrBot container if true dual-server soak is required. -- Product decision on companion/operations remote username naming convention. -- TLS-terminated HTTPS endpoint if pinning must be validated before release. - -## Test status - -- `desktop-protocol` lib tests: 4 passed (AstrBot mapper). -- `astrbot-desktop` lib tests: 58 passed. -- Vitest: 6 passed. -- Live non-destructive probe artifacts retained under `docs/reports/` (secrets redacted). -- Full release build / Clippy gate / Playwright not re-run in this autonomous stretch. - -## Current branch - -- `feat/p02-dual-connections` - -## Next step after acceptance - -1. Chat panel MVP against AstrBot adapter. -2. Persist sticky session + message history under profile/server/session keys. +# Development Progress + +## Current phase + +P02 dual connections remains active on `feat/p02-dual-connections`; Q1-Q5 implementation is now complete for the current MVP scope. +The foundation has been adjusted for real AstrBot OpenAPI transport after live container probe. +Credential enrollment, dual-slot runtime skeleton, and AstrBot SSE mapping are implemented. +Full product chat UI activation and production identity enrollment remain open for acceptance. + +## Completed + +### Prior phases + +- P00 bootstrap merged. +- P01 foundation merged through Forgejo PR #2. +- P02 first increment: persistent dual slot assignment (`bcc08cf`). +- P02 second increment: auto-connect validation and operations switch guard (`34a563c`). + +### Real-protocol foundation (this session) + +- Reviewed local AstrBot clone and live probe target `http://192.168.31.210:26285`. +- Confirmed API Key chat SSE works; dual usernames/session ids isolate cleanly. +- Added internal mapping module `desktop-protocol::astrbot` for OpenAPI SSE → Desktop events. +- Added `TransportKind` (`desktop-v1` | `astrbot-openapi`) plus `remote_username` / `sticky_session_id` profile fields. +- Migrated SQLite schema to v4. +- Added Rust `AstrBotClient` for probe/list/send chat stream. +- Added `DualSlotRuntime` with activate/probe/send/deactivate skeleton. +- Added bounded credential enrollment IPC (`enroll_profile_credential` / `clear_profile_credential`) using Windows Credential Manager; secrets never returned to React. +- Added frontend transport fields and credential enrollment card. +- Recorded ADR-0005 and live probe report. + +## In progress + +- User acceptance of real-protocol foundation. +- Next product increments: chat panel rendering, history persistence, JWT login path, stronger server identity UX. + +## Remaining + +- Wire auto-connect startup to DualSlotRuntime without weakening secret boundary. +- Dual chat UI panels consuming normalized runtime events. +- Message persistence for AstrBot mapped events. +- Dashboard JWT auth strategy. +- File attachment OpenAPI path. +- Certificate fingerprint binding. +- Full stage gate (fmt/clippy/typecheck/build/installer) before merge. + +## Blockers / reserved for user + +- Production API Key rotation after shared test key exposure in chat. +- Optional second AstrBot container if true dual-server soak is required. +- Product decision on companion/operations remote username naming convention. +- TLS-terminated HTTPS endpoint if pinning must be validated before release. + +## Test status + +- `desktop-protocol` lib tests: 4 passed (AstrBot mapper). +- `astrbot-desktop` lib tests: 58 passed. +- Vitest: 6 passed. +- Live non-destructive probe artifacts retained under `docs/reports/` (secrets redacted). +- Full release build / Clippy gate / Playwright not re-run in this autonomous stretch. + +## Current branch + +- `feat/p02-dual-connections` + +## Next step after acceptance + +1. Chat panel MVP against AstrBot adapter. +2. Persist sticky session + message history under profile/server/session keys. 3. Optional auto-connect activation using enrolled credential references only. ## 2026-07-20: remote identity and runnable paper pet increment @@ -76,50 +76,47 @@ Full product chat UI activation and production identity enrollment remain open f - Passed the nickname through the Rust-owned AstrBot request boundary as an optional `nickname` field; secrets remain Rust-only. - Added a transparent, borderless, skip-taskbar pet window with tray-resident hide-on-close behavior and a small CSS 2D paper-character state loop. - Frontend checks pass; Rust test/build gate remains to be rerun after the final patch. - - -## Queue decision - -- Q1-Q3 complete the real AstrBot text-chat path and main UI before visual polish. -- Q4 adds the draggable/resizable quick window using the same chat state. -- Q5 adds the status bubble and configurable click routing. -- Q6-Q7 reserve pet assets, multi-monitor geometry, and game-mode policy without adding arbitrary desktop control. - - -## Autonomous queue increment - -Completed in this increment: - -- Q1: Rust-owned bounded runtime event drain IPC and normalized event identity fields. -- Q2: SQLite schema v5 chat message persistence keyed by account/profile/slot/server/session/message. -- Q3: main chat panel MVP with Companion/Operations switching, streaming event merge, send error state, and session history restore. -- Q4: dynamic Tauri quick-chat window sharing the Rust runtime. -- Q5: non-default-topmost status bubble window with runtime status display. -- Q6: pet state, safe PNG/WebP manifest schema, monitor geometry, and safe-edge placement model. - -Validation: Rust workspace tests passed (59 tests), Rust Clippy with warnings denied passed, Rust format check passed, TypeScript check passed, Vitest passed (6 tests), and Vite production build passed. - -Still not claimed complete: automatic startup activation, real Tauri multi-window manual UX, long-duration reconnect soak, JWT login, attachment protocol, certificate pinning, actual Windows multi-monitor/game-mode integration, voice, and release installer validation. - -## 2026-07-20:Q1 启动自动连接增量 - -- 已完成:在 Tauri 启动阶段读取已保存的 `auto_connect` Profile,并按 Companion / Operations 槽位独立激活。 -- 已完成:启动失败保留在对应 Runtime 状态,不阻断另一槽位;凭据仍由 Rust 读取,React 不接触秘密值。 -- 已完成:启动自动连接复用现有 AstrBot OpenAPI 与 Desktop Gateway 激活路径。 -- 验证:`cargo check --workspace --all-targets`、`cargo test --workspace`(59 项通过)、`cargo clippy --workspace --all-targets -- -D warnings`、`pnpm.cmd run typecheck` 通过。 -- 未完成:前端启动连接状态提示、自动重连退避、JWT 登录、聊天布局和快速窗口共享状态完善。 -- 当前工作区:本地未提交,等待下一项 Q1/Q2 增量完成后按用户指令提交。 - - -## 2026-07-20??? 1-6 ?????? - -- Q1???????????????????????????? -- Q2??? SQLite schema v6 ???????????????????????60 ? Rust ????? -- Q3?????????????/????/?????????????? Hook ????? -- Q4???????????????? URL ??? -- Q5????????????/???????????????????????????? -- Q6????? JSON manifest ???? UI?????????????PNG/WebP ?????????????? -- ?????`2e84ea5`?`5dfac59`?`e0b48c0`?`9f235a3`?`4f13581`?`1828c61` ???? Forgejo? + +## Queue decision + +- Q1-Q3 complete the real AstrBot text-chat path and main UI before visual polish. +- Q4 adds the draggable/resizable quick window using the same chat state. +- Q5 adds the status bubble and configurable click routing. +- Q6-Q7 reserve pet assets, multi-monitor geometry, and game-mode policy without adding arbitrary desktop control. + +## Autonomous queue increment + +Completed in this increment: + +- Q1: Rust-owned bounded runtime event drain IPC and normalized event identity fields. +- Q2: SQLite schema v5 chat message persistence keyed by account/profile/slot/server/session/message. +- Q3: main chat panel MVP with Companion/Operations switching, streaming event merge, send error state, and session history restore. +- Q4: dynamic Tauri quick-chat window sharing the Rust runtime. +- Q5: non-default-topmost status bubble window with runtime status display. +- Q6: pet state, safe PNG/WebP manifest schema, monitor geometry, and safe-edge placement model. + +Validation: Rust workspace tests passed (59 tests), Rust Clippy with warnings denied passed, Rust format check passed, TypeScript check passed, Vitest passed (6 tests), and Vite production build passed. + +Still not claimed complete: automatic startup activation, real Tauri multi-window manual UX, long-duration reconnect soak, JWT login, attachment protocol, certificate pinning, actual Windows multi-monitor/game-mode integration, voice, and release installer validation. + +## 2026-07-20:Q1 启动自动连接增量 + +- 已完成:在 Tauri 启动阶段读取已保存的 `auto_connect` Profile,并按 Companion / Operations 槽位独立激活。 +- 已完成:启动失败保留在对应 Runtime 状态,不阻断另一槽位;凭据仍由 Rust 读取,React 不接触秘密值。 +- 已完成:启动自动连接复用现有 AstrBot OpenAPI 与 Desktop Gateway 激活路径。 +- 验证:`cargo check --workspace --all-targets`、`cargo test --workspace`(59 项通过)、`cargo clippy --workspace --all-targets -- -D warnings`、`pnpm.cmd run typecheck` 通过。 +- 未完成:前端启动连接状态提示、自动重连退避、JWT 登录、聊天布局和快速窗口共享状态完善。 +- 当前工作区:本地未提交,等待下一项 Q1/Q2 增量完成后按用户指令提交。 + +## 2026-07-20??? 1-6 ?????? + +- Q1???????????????????????????? +- Q2??? SQLite schema v6 ???????????????????????60 ? Rust ????? +- Q3?????????????/????/?????????????? Hook ????? +- Q4???????????????? URL ??? +- Q5????????????/???????????????????????????? +- Q6????? JSON manifest ???? UI?????????????PNG/WebP ?????????????? +- ?????`2e84ea5`?`5dfac59`?`e0b48c0`?`9f235a3`?`4f13581`?`1828c61` ???? Forgejo? ## 2026-07-20?????????? @@ -129,7 +126,6 @@ Still not claimed complete: automatic startup activation, real Tauri multi-windo - ??????????????/?????????????????? Windows ????? - ?????TypeScript?ESLint?Vitest 6/6?Vite production build ??? - ## 2026-07-20: chat extensibility contract - Added Rust-owned AstrBot capability discovery, model configuration options, and slot-scoped IPC. @@ -138,27 +134,49 @@ Still not claimed complete: automatic startup activation, real Tauri multi-windo - Updated ChatPanel with model selection, attachment staging, upload state, and clean WebChat-style text labels. - Validation: Rust workspace tests 60 passed; Vitest 6/6 passed; TypeScript, ESLint, cargo check, and Vite build passed. - ## 2026-07-20: live chat extensibility probe - Both supplied containers returned status=ok for chat sessions and config discovery. - Container 1 accepted a minimal multipart upload and returned attachment_id, filename, and type. - No chat message or configuration mutation was performed. Full multimodal rendering remains reserved for an explicit send-message acceptance test. - ## 2026-07-20: live end-to-end chat acceptance - First test container accepted text plus config selection and streamed a complete response with no error frame. - The same session accepted text plus config selection plus the uploaded attachment and streamed a complete response with no error frame. - This validates the current model/config and attachment request contract against a real AstrBot instance. - ## 2026-07-20: remote session navigation - Added slot-scoped remote session discovery and sticky session switching IPC. - ChatPanel now displays remote sessions and reloads history after switching; message filtering includes sessionId. - Validation: Rust workspace tests, Clippy, TypeScript, ESLint, Vitest, and Vite build passed. +## 2026-07-20: Q1-Q5 shared chat runtime completion + +- Q1: normalized runtime events are now broadcast at the application level, so the main window and quick-chat window receive the same stream without competing for a single drain queue. +- Q2: chat schema v10 includes `slot` in the message key and all history/read IPC queries; streaming deltas append to the stored draft and completed events replace it with the authoritative content. +- Q3: main chat sends user messages into durable history after the runtime assigns a session, keeps slot/session filtering isolated, and preserves live assistant updates. +- Q4: quick chat continues to reuse the Rust runtime and SQLite history, with persisted geometry and shared live events across windows. +- Q5: the status bubble shows connection state and unread totals; the tray menu shows live Companion/Operations states and provides main/quick chat entry points. +- Validation: Rust workspace tests pass (67 tests plus protocol/integration suites), frontend typecheck/lint/Vitest/build pass, and touched frontend files are formatted. + +## 2026-07-20: Q6-Q8 companion, diagnostics, and interaction MVP + +- Q6: removed the desktop pet's timer-driven idle/resting placeholder; runtime connection and normalized chat events now drive working, speaking, resting, and error states. +- Q6-Q7: added monitor geometry discovery, safe-edge placement, remembered monitor selection, always-on-top control, and game-mode safe-edge fallback without input injection or arbitrary desktop control. +- Q7: added bounded read-only diagnostics for the foreground window, a user-selected process ID, and one explicitly requested network host/port. +- Q8: added browser Web Speech input and speech synthesis playback on top of the shared chat send/message path, plus proactive-message pet updates and configurable quiet hours for bubble reminders. +- Validation: Rust format, Clippy, workspace tests, TypeScript, ESLint, Vitest, and Vite build pass. + +## 2026-07-20: Q9 local stability and unsigned release + +- Added a session marker in the application data directory; an unexpected previous session is reported on the next startup while local profiles and chat history remain recoverable. +- Bumped the local release line to `0.1.0-alpha.2` and added the Q9 local acceptance checklist. +- Updated the changelog to distinguish the unsigned local installer from future signed/auto-update releases. +- Rust workspace tests pass after the version and recovery changes; frontend checks remain green. +- Pending manual acceptance: install the unsigned NSIS package, exercise tray/second-launch/network-reconnect/window-restore paths, and verify the recovery marker after a disposable forced termination. + ## 2026-07-20: Q4 quick-chat window geometry persistence - Completed: added the `window_states` SQLite table (schema v7) for quick-chat position, size, and monitor identity. @@ -167,10 +185,12 @@ Still not claimed complete: automatic startup activation, real Tauri multi-windo - Completed: added window bounds, monitor clamping, and database round-trip tests. - Validation: Rust format, Clippy with warnings denied, and `astrbot-desktop` unit tests pass. - Next: finish the remaining quick-window/status-bubble acceptance work, then continue with the pet asset and multi-monitor controller foundation. + ## 2026-07-20: status bubble text cleanup - Fixed the remaining mojibake in status-bubble state labels and accessibility text. - Validation: TypeScript, ESLint, and Vitest (6 tests) pass. + ## 2026-07-20: quick-chat close behavior fix - Fixed the global close handler so the quick-chat window can be closed normally instead of being intercepted and only hidden. @@ -213,42 +233,64 @@ Still not claimed complete: automatic startup activation, real Tauri multi-windo - Replaced the floating pet CSS background-image crop with a clipped `` spritesheet renderer for more reliable WebView2/asset-protocol loading. - Added explicit pet-view root sizing and transparent `html`/`body`/`#root` backgrounds so Tauri transparent windows do not fall back to an opaque blank surface. - Validation: TypeScript, ESLint, Vitest (6), Vite build, Rust format, Clippy with warnings denied, Rust workspace tests (66), and NSIS bundle build pass. -- The already-running executable must be fully closed and relaunched; the generated installer is at `target/release/bundle/nsis/AstrBot Desktop_0.1.0-alpha.1_x64-setup.exe`. +- The already-running executable must be fully closed and relaunched; the generated installer is at `target/release/bundle/nsis/AstrBot Desktop_0.1.0-alpha.1_x64-setup.exe`. ## 2026-07-20: tray quit reliability fix - Fixed the tray Quit action so it destroys the main, quick-chat, status-bubble, and pet windows before requesting process exit. - This avoids the global close-to-tray handler intercepting shutdown and leaving the process resident. -- Validation: Rust format, Clippy with warnings denied, Rust workspace tests (66), TypeScript, ESLint, Vitest (6), and NSIS bundle build pass. +- Validation: Rust format, Clippy with warnings denied, Rust workspace tests (66), TypeScript, ESLint, Vitest (6), and NSIS bundle build pass. ## 2026-07-20: desktop pet rendering and tray exit follow-up - Fixed the pixel spritesheet crop by setting the sprite image transform origin to the top-left; the previous default center origin moved the selected frame outside the clipped viewport and produced a blank transparent pet window. - Added an application exiting state so tray Quit allows all windows to close instead of the global close-to-tray handler hiding them and leaving an empty, non-interactive placeholder. -- Validation: frontend typecheck, Vitest (6), Vite production build, Rust format, Rust check, and Rust workspace tests (66) pass. +- Validation: frontend typecheck, Vitest (6), Vite production build, Rust format, Rust check, and Rust workspace tests (66) pass. ## 2026-07-20: transparent pet and tray process exit hardening - Replaced the transparent-window spritesheet crop with a PNG-backed Canvas renderer; this avoids WebView2 transparent-window failures with the large WebP image and CSS transform cropping. - Changed tray Quit to call Tauri cleanup and terminate the process immediately, ensuring the tray icon, hidden main window, and pet window cannot remain as orphaned UI surfaces. -- Rebuilt and launched the Release executable at 18:24:20 for manual verification. +- Rebuilt and launched the Release executable at 18:24:20 for manual verification. ## 2026-07-20: wire pet display mode and render diagnostics - Connected the pet display-mode setting to persisted local UI state; `app-ui` now embeds the pet in the Pet page, while `desktop-floating` explicitly opens the transparent Tauri window. - Added sprite loading and Canvas failure feedback instead of leaving a silent blank/white surface. -- Validation: TypeScript, Vitest (6), Vite build, Rust format/check, and Release bundle build pass. The rebuilt executable started at 18:35:29. +- Validation: TypeScript, Vitest (6), Vite build, Rust format/check, and Release bundle build pass. The rebuilt executable started at 18:35:29. ## 2026-07-20: in-app draggable pet overlay - Promoted the app-ui pet from a static page preview to an application-level floating overlay, matching the Codex/Hermes-style in-window companion behavior. - Added pointer dragging with clamped coordinates and local persistence; the overlay can be hidden without creating a native desktop window. - Disabled the broken desktop-floating launch path in the Pet settings and provided a direct switch back to the in-app mode. -- Validation: TypeScript, Vitest (6), Vite build, Rust format/check, and Release bundle build pass. The rebuilt executable started at 18:44:43. +- Validation: TypeScript, Vitest (6), Vite build, Rust format/check, and Release bundle build pass. The rebuilt executable started at 18:44:43. ## 2026-07-20: restore desktop floating pet transparency - Restored the desktop-floating display option and its `open_pet_window` action; it is no longer disabled. - Fixed the Windows transparent WebView configuration by explicitly setting an RGBA-zero background and disabling undecorated window shadow, which could otherwise render a white rectangle around the pet. - Preserved the app-ui draggable overlay implementation. -- Validation: TypeScript, Vitest (6), Rust format/check, Vite build, and Release bundle build pass. The rebuilt executable started at 18:50:25. +- Validation: TypeScript, Vitest (6), Rust format/check, Vite build, and Release bundle build pass. The rebuilt executable started at 18:50:25. + +## 2026-07-20: unified slot runtime lifecycle hardening + +- Added independent connection cancellation and message cancellation tokens for each Companion / Operations slot. +- Connected the desktop gateway SSE reconnect loop to the active slot lifecycle, including state synchronization and terminal error mapping. +- Added generation guards so a late response from a cancelled or replaced slot cannot restore a stale client into the new slot state. +- Preserved AstrBot OpenAPI request-stream handling and bounded retry behavior; AstrBot does not expose a separate unsolicited SSE subscription endpoint in the current adapter. +- Validation: Rust fmt, workspace check, Rust library tests (66), TypeScript typecheck, and Vitest (6) pass. + +## 2026-07-20: compact chat-first main window + +- Fixed runtime status parsing so Rust null option fields are accepted as an unconnected slot state instead of showing raw Zod validation output. +- Made the chat page chat-first: removed the workspace layout selector and model dropdown from the primary page; secondary settings remain in their dedicated navigation pages. +- Reduced main-window spacing, added rounded application corners, and preserved the frameless custom title bar. +- Validation: TypeScript, ESLint, Vitest (6), Vite build, and Rust workspace check pass. + +## 2026-07-21: AstrBot chat complete-frame handling + +- Fixed the AstrBot SSE mapper to consume `type: "complete"` frames, which carry the authoritative final response text and were previously ignored. +- Reconciled final text with partial `plain` chunks and added support for text nested under `text`, `content`, or `message` fields. +- Added protocol tests for complete-only replies and partial-stream reconciliation. +- Validation: Rust format, Clippy with warnings denied, Rust workspace tests (67), TypeScript, ESLint, and NSIS bundle build pass. diff --git a/docs/q9-local-acceptance.md b/docs/q9-local-acceptance.md new file mode 100644 index 0000000..9b13d72 --- /dev/null +++ b/docs/q9-local-acceptance.md @@ -0,0 +1,34 @@ +# Q9 Local Stability and Unsigned Installer Acceptance + +## Scope + +This checklist covers the local Q9 release line only. It does not claim Windows code signing, automatic updates, or Forgejo CI verification. + +## Automated checks + +- `cargo fmt --all -- --check` +- `cargo clippy --workspace --all-targets -- -D warnings` +- `cargo test --workspace` +- `npm.cmd run typecheck` +- `npm.cmd run lint` +- `npm.cmd run test -- --run` +- `npm.cmd run build` +- `npm.cmd run tauri:build` + +## Manual Windows checks + +1. Install the unsigned NSIS package into a disposable user profile. +2. Launch the application twice and verify the second launch focuses the existing main window. +3. Configure one Companion and one Operations profile, then verify independent connection status and message history. +4. Close the main window, quick-chat window, bubble, and pet through their supported paths; verify tray recovery and no blank native placeholder remains. +5. Use tray `退出程序`; verify the process, tray icon, pet window, bubble, and quick-chat window all disappear. +6. Move the quick-chat window, switch monitors, restart the app, and verify geometry is restored or safely clamped. +7. Disconnect and reconnect the network; verify the slot transitions through reconnecting without duplicate messages. +8. Force-terminate a disposable run, relaunch, and verify startup remains usable and logs report an unexpected previous session without exposing secrets. +9. Run the foreground-window, process, and single-target network diagnostics; verify invalid targets are rejected and no command execution is exposed. + +## Release artifact + +- Version: `0.1.0-alpha.2` +- Executable: `target/release/astrbot-desktop.exe` +- Unsigned installer: `target/release/bundle/nsis/AstrBot Desktop_0.1.0-alpha.2_x64-setup.exe` diff --git a/package.json b/package.json index c4e2b94..2f65c7c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "astrbot-desktop", - "version": "0.1.0-alpha.1", + "version": "0.1.0-alpha.2", "private": true, "type": "module", "packageManager": "pnpm@11.9.0", diff --git a/src-tauri/src/database/mod.rs b/src-tauri/src/database/mod.rs index 24ce20f..2252c86 100644 --- a/src-tauri/src/database/mod.rs +++ b/src-tauri/src/database/mod.rs @@ -7,7 +7,7 @@ use time::{OffsetDateTime, format_description::well_known::Rfc3339}; use rusqlite::{Connection, OptionalExtension, params}; use thiserror::Error; -pub const CURRENT_SCHEMA_VERSION: u32 = 9; +pub const CURRENT_SCHEMA_VERSION: u32 = 10; pub struct ApplicationDatabase { store: Mutex, @@ -70,29 +70,32 @@ impl ApplicationDatabase { pub fn list_chat_sessions( &self, profile_id: &str, + slot: &str, server_instance_id: &str, ) -> Result, DatabaseError> { self.store .lock() .map_err(|_| DatabaseError::LockPoisoned)? - .list_chat_sessions(profile_id, server_instance_id) + .list_chat_sessions(profile_id, slot, server_instance_id) } pub fn mark_chat_session_read( &self, profile_id: &str, + slot: &str, server_instance_id: &str, session_id: &str, ) -> Result<(), DatabaseError> { self.store .lock() .map_err(|_| DatabaseError::LockPoisoned)? - .mark_chat_session_read(profile_id, server_instance_id, session_id) + .mark_chat_session_read(profile_id, slot, server_instance_id, session_id) } pub fn list_chat_messages( &self, profile_id: &str, + slot: &str, server_instance_id: &str, session_id: &str, limit: usize, @@ -100,7 +103,7 @@ impl ApplicationDatabase { self.store .lock() .map_err(|_| DatabaseError::LockPoisoned)? - .list_chat_messages(profile_id, server_instance_id, session_id, limit) + .list_chat_messages(profile_id, slot, server_instance_id, session_id, limit) } pub fn load_window_state( &self, @@ -176,10 +179,11 @@ impl ProfileMessageStore { pub fn list_chat_sessions( &self, profile_id: &str, + slot: &str, server_instance_id: &str, ) -> Result, DatabaseError> { - let mut statement = self.connection.prepare("SELECT account_id, profile_id, slot, server_instance_id, remote_username, session_id, unread_count, last_message_id, last_message_at FROM chat_sessions WHERE profile_id = ?1 AND server_instance_id = ?2 ORDER BY last_message_at DESC")?; - let rows = statement.query_map(params![profile_id, server_instance_id], |row| { + let mut statement = self.connection.prepare("SELECT account_id, profile_id, slot, server_instance_id, remote_username, session_id, unread_count, last_message_id, last_message_at FROM chat_sessions WHERE profile_id = ?1 AND slot = ?2 AND server_instance_id = ?3 ORDER BY last_message_at DESC")?; + let rows = statement.query_map(params![profile_id, slot, server_instance_id], |row| { Ok(StoredChatSession { account_id: row.get(0)?, profile_id: row.get(1)?, @@ -199,10 +203,11 @@ impl ProfileMessageStore { pub fn mark_chat_session_read( &self, profile_id: &str, + slot: &str, server_instance_id: &str, session_id: &str, ) -> Result<(), DatabaseError> { - self.connection.execute("UPDATE chat_sessions SET unread_count = 0 WHERE profile_id = ?1 AND server_instance_id = ?2 AND session_id = ?3", params![profile_id, server_instance_id, session_id])?; + self.connection.execute("UPDATE chat_sessions SET unread_count = 0 WHERE profile_id = ?1 AND slot = ?2 AND server_instance_id = ?3 AND session_id = ?4", params![profile_id, slot, server_instance_id, session_id])?; Ok(()) } @@ -279,15 +284,22 @@ impl ProfileMessageStore { let was_streaming: Option = self .connection .query_row( - "SELECT streaming != 0 FROM chat_messages WHERE profile_id = ?1 AND server_instance_id = ?2 AND session_id = ?3 AND message_id = ?4", - params![profile_id, server_instance_id, session_id, message_id], + "SELECT streaming != 0 FROM chat_messages WHERE profile_id = ?1 AND slot = ?2 AND server_instance_id = ?3 AND session_id = ?4 AND message_id = ?5", + params![profile_id, slot, server_instance_id, session_id, message_id], |row| row.get(0), ) .optional()?; - self.connection.execute( - "INSERT INTO chat_messages (account_id, profile_id, slot, server_instance_id, remote_username, session_id, message_id, role, content, streaming, error, updated_at) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12) ON CONFLICT(profile_id, server_instance_id, session_id, message_id) DO UPDATE SET content = excluded.content, streaming = excluded.streaming, error = excluded.error, updated_at = excluded.updated_at", - params![account_id, profile_id, slot, server_instance_id, remote_username, session_id, message_id, role, content, streaming, error, timestamp], - )?; + if streaming { + self.connection.execute( + "INSERT INTO chat_messages (account_id, profile_id, slot, server_instance_id, remote_username, session_id, message_id, role, content, streaming, error, updated_at) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12) ON CONFLICT(profile_id, slot, server_instance_id, session_id, message_id) DO UPDATE SET content = chat_messages.content || excluded.content, streaming = excluded.streaming, error = excluded.error, updated_at = excluded.updated_at", + params![account_id, profile_id, slot, server_instance_id, remote_username, session_id, message_id, role, content, streaming, error, timestamp], + )?; + } else { + self.connection.execute( + "INSERT INTO chat_messages (account_id, profile_id, slot, server_instance_id, remote_username, session_id, message_id, role, content, streaming, error, updated_at) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12) ON CONFLICT(profile_id, slot, server_instance_id, session_id, message_id) DO UPDATE SET content = excluded.content, streaming = excluded.streaming, error = excluded.error, updated_at = excluded.updated_at", + params![account_id, profile_id, slot, server_instance_id, remote_username, session_id, message_id, role, content, streaming, error, timestamp], + )?; + } let unread = role != "user" && !streaming && !error && was_streaming != Some(false); self.connection.execute( "INSERT INTO chat_sessions (account_id, profile_id, slot, server_instance_id, remote_username, session_id, unread_count, last_message_id, last_message_at) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9) ON CONFLICT(account_id, profile_id, slot, server_instance_id, session_id) DO UPDATE SET remote_username = COALESCE(excluded.remote_username, chat_sessions.remote_username), unread_count = chat_sessions.unread_count + CASE WHEN ?10 = 1 THEN 1 ELSE 0 END, last_message_id = excluded.last_message_id, last_message_at = excluded.last_message_at", @@ -299,14 +311,15 @@ impl ProfileMessageStore { pub fn list_chat_messages( &self, profile_id: &str, + slot: &str, server_instance_id: &str, session_id: &str, limit: usize, ) -> Result, DatabaseError> { let limit = limit.clamp(1, 200) as i64; - let mut statement = self.connection.prepare("SELECT account_id, profile_id, slot, server_instance_id, remote_username, session_id, message_id, role, content, streaming, error, updated_at FROM chat_messages WHERE profile_id = ?1 AND server_instance_id = ?2 AND session_id = ?3 ORDER BY updated_at ASC LIMIT ?4")?; + let mut statement = self.connection.prepare("SELECT account_id, profile_id, slot, server_instance_id, remote_username, session_id, message_id, role, content, streaming, error, updated_at FROM chat_messages WHERE profile_id = ?1 AND slot = ?2 AND server_instance_id = ?3 AND session_id = ?4 ORDER BY updated_at ASC LIMIT ?5")?; let rows = statement.query_map( - params![profile_id, server_instance_id, session_id, limit], + params![profile_id, slot, server_instance_id, session_id, limit], |row| { Ok(StoredChatMessage { account_id: row.get(0)?, @@ -537,6 +550,14 @@ fn migrate(connection: &mut Connection) -> Result<(), DatabaseError> { transaction.commit()?; } + if found <= 9 { + let transaction = connection.transaction()?; + transaction.execute_batch( + "ALTER TABLE chat_messages RENAME TO chat_messages_legacy; DROP INDEX IF EXISTS chat_messages_account_session_idx; CREATE TABLE chat_messages (account_id TEXT NOT NULL, profile_id TEXT NOT NULL, slot TEXT NOT NULL CHECK(slot IN ('companion', 'operations')), server_instance_id TEXT NOT NULL, remote_username TEXT, session_id TEXT NOT NULL, message_id TEXT NOT NULL, role TEXT NOT NULL CHECK(role IN ('user', 'assistant', 'system')), content TEXT NOT NULL, streaming INTEGER NOT NULL DEFAULT 0 CHECK(streaming IN (0, 1)), error INTEGER NOT NULL DEFAULT 0 CHECK(error IN (0, 1)), updated_at TEXT NOT NULL, PRIMARY KEY (profile_id, slot, server_instance_id, session_id, message_id)); CREATE INDEX chat_messages_account_session_idx ON chat_messages(account_id, profile_id, slot, server_instance_id, session_id, updated_at); INSERT INTO chat_messages SELECT account_id, profile_id, slot, server_instance_id, remote_username, session_id, message_id, role, content, streaming, error, updated_at FROM chat_messages_legacy; DROP TABLE chat_messages_legacy; PRAGMA user_version = 10;", + )?; + transaction.commit()?; + } + Ok(()) } @@ -628,22 +649,96 @@ mod tests { ) .expect("duplicate completion"); let sessions = database - .list_chat_sessions("profile-a", "server-a") + .list_chat_sessions("profile-a", "companion", "server-a") .expect("sessions"); assert_eq!(sessions.len(), 1); assert_eq!(sessions[0].unread_count, 1); database - .mark_chat_session_read("profile-a", "server-a", "session-a") + .mark_chat_session_read("profile-a", "companion", "server-a", "session-a") .expect("mark read"); assert_eq!( database - .list_chat_sessions("profile-a", "server-a") + .list_chat_sessions("profile-a", "companion", "server-a") .expect("sessions after read")[0] .unread_count, 0 ); } + #[test] + fn merges_streaming_deltas_and_keeps_slots_isolated() { + let database = ApplicationDatabase::in_memory().expect("database"); + database + .upsert_chat_message( + "account-a", + "profile-a", + "companion", + "server-a", + None, + "session-a", + "message-a", + "assistant", + "hel", + true, + false, + ) + .expect("first delta"); + database + .upsert_chat_message( + "account-a", + "profile-a", + "companion", + "server-a", + None, + "session-a", + "message-a", + "assistant", + "lo", + true, + false, + ) + .expect("second delta"); + database + .upsert_chat_message( + "account-a", + "profile-a", + "operations", + "server-a", + None, + "session-a", + "message-a", + "assistant", + "operations", + false, + false, + ) + .expect("operations message"); + database + .upsert_chat_message( + "account-a", + "profile-a", + "companion", + "server-a", + None, + "session-a", + "message-a", + "assistant", + "hello", + false, + false, + ) + .expect("completion"); + + let companion = database + .list_chat_messages("profile-a", "companion", "server-a", "session-a", 20) + .expect("companion messages"); + let operations = database + .list_chat_messages("profile-a", "operations", "server-a", "session-a", 20) + .expect("operations messages"); + assert_eq!(companion[0].content, "hello"); + assert_eq!(operations[0].content, "operations"); + } + use super::*; #[test] diff --git a/src-tauri/src/diagnostics/network.rs b/src-tauri/src/diagnostics/network.rs index 4e05932..cc47c04 100644 --- a/src-tauri/src/diagnostics/network.rs +++ b/src-tauri/src/diagnostics/network.rs @@ -1,6 +1,9 @@ use std::{fmt, net::SocketAddr, time::Duration}; -#[derive(Clone, Debug, PartialEq, Eq)] +use serde::Serialize; + +#[derive(Clone, Debug, PartialEq, Eq, Serialize)] +#[serde(rename_all = "camelCase")] pub struct NetworkTarget { pub host: String, pub port: u16, @@ -28,14 +31,16 @@ impl NetworkTarget { } } -#[derive(Clone, Debug, PartialEq, Eq)] +#[derive(Clone, Debug, PartialEq, Eq, Serialize)] +#[serde(rename_all = "camelCase")] pub struct NetworkAttempt { pub address: SocketAddr, pub connected: bool, pub error_kind: Option, } -#[derive(Clone, Debug, PartialEq, Eq)] +#[derive(Clone, Debug, PartialEq, Eq, Serialize)] +#[serde(rename_all = "camelCase")] pub struct NetworkCheckResult { pub resolved_addresses: usize, pub connected_address: Option, diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 01bdefa..fc4645e 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -22,7 +22,7 @@ use quick_window::{ QuickWindowState, }; use runtime_guards::RuntimeGuards; -use serde::Serialize; +use serde::{Deserialize, Serialize}; use tauri::{ AppHandle, Emitter, Manager, PhysicalPosition, PhysicalSize, WebviewUrl, WebviewWindowBuilder, @@ -33,6 +33,114 @@ use tauri::{ }; const PET_WINDOW_ID: &str = "desktop-pet"; +const SESSION_MARKER_FILE: &str = "session.active"; + +struct SessionMarker(std::path::PathBuf); + +#[derive(Debug, Clone, Serialize)] +#[serde(rename_all = "camelCase")] +struct MonitorInfo { + name: Option, + left: i32, + top: i32, + width: u32, + height: u32, + work_left: i32, + work_top: i32, + work_width: u32, + work_height: u32, + scale_factor: f64, + is_primary: bool, +} + +#[derive(Debug, Clone, Deserialize)] +#[serde(rename_all = "camelCase", deny_unknown_fields)] +struct PetWindowPreferences { + position_mode: pet::PetPositionMode, + monitor_index: Option, + always_on_top: bool, + game_mode: bool, +} + +#[derive(Debug, Clone, Deserialize)] +#[serde(rename_all = "camelCase", deny_unknown_fields)] +struct NetworkDiagnosticRequest { + host: String, + port: u16, + timeout_seconds: Option, +} + +#[derive(Debug, Clone, Deserialize)] +#[serde(rename_all = "camelCase", deny_unknown_fields)] +struct ProcessDiagnosticRequest { + process_id: u32, +} + +fn available_monitor_info(app: &AppHandle) -> Result, String> { + let window = app + .get_webview_window("main") + .ok_or_else(|| "main window is unavailable".to_owned())?; + let primary_name = window + .primary_monitor() + .ok() + .flatten() + .and_then(|monitor| monitor.name().cloned()); + window + .available_monitors() + .map_err(|error| error.to_string()) + .map(|monitors| { + monitors + .into_iter() + .map(|monitor| { + let area = monitor.work_area(); + let position = monitor.position(); + let size = monitor.size(); + let name = monitor.name().cloned(); + MonitorInfo { + is_primary: name == primary_name, + name, + left: position.x, + top: position.y, + width: size.width, + height: size.height, + work_left: area.position.x, + work_top: area.position.y, + work_width: area.size.width, + work_height: area.size.height, + scale_factor: monitor.scale_factor(), + } + }) + .collect() + }) +} + +fn place_pet_on_monitor( + window: &tauri::WebviewWindow, + monitor: &MonitorInfo, + mode: pet::PetPositionMode, +) -> Result<(), String> { + let size = window.outer_size().map_err(|error| error.to_string())?; + let pet = pet::MonitorGeometry { + left: monitor.left, + top: monitor.top, + width: monitor.width, + height: monitor.height, + work_left: monitor.work_left, + work_top: monitor.work_top, + work_width: monitor.work_width, + work_height: monitor.work_height, + is_primary: monitor.is_primary, + }; + let placement = match mode { + pet::PetPositionMode::Edge | pet::PetPositionMode::Monitor => { + pet::place_at_safe_edge(pet, size.width, size.height, 16) + } + pet::PetPositionMode::Free | pet::PetPositionMode::FollowMainWindow => return Ok(()), + }; + window + .set_position(PhysicalPosition::new(placement.x, placement.y)) + .map_err(|error| error.to_string()) +} #[derive(Debug, Clone, Serialize)] #[serde(rename_all = "camelCase")] @@ -160,10 +268,31 @@ fn save_quick_window_state(window: &tauri::Window, app: &AppHandle) { } fn exit_application(app: &AppHandle) { + if let Some(marker) = app.try_state::() { + let _ = std::fs::remove_file(&marker.0); + } app.cleanup_before_exit(); std::process::exit(0); } +fn tray_state_label(name: &str, state: desktop_protocol::ConnectionState) -> String { + let state = match state { + desktop_protocol::ConnectionState::Connected => "在线", + desktop_protocol::ConnectionState::Connecting => "连接中", + desktop_protocol::ConnectionState::Authenticating => "认证中", + desktop_protocol::ConnectionState::Syncing => "同步中", + desktop_protocol::ConnectionState::Reconnecting => "重连中", + desktop_protocol::ConnectionState::AuthExpired => "需要认证", + desktop_protocol::ConnectionState::Disconnected => "未连接", + desktop_protocol::ConnectionState::Offline => "离线", + desktop_protocol::ConnectionState::ServerUnavailable => "服务不可用", + desktop_protocol::ConnectionState::Incompatible => "协议不兼容", + desktop_protocol::ConnectionState::CertificateChanged => "证书已变化", + desktop_protocol::ConnectionState::FatalError => "连接错误", + }; + format!("{name}:{state}") +} + #[tauri::command] fn open_main_window(app: AppHandle) -> Result<(), String> { let window = app @@ -179,9 +308,9 @@ fn open_pet_window(app: AppHandle) -> Result<(), String> { window .set_always_on_top(true) .map_err(|error| error.to_string())?; - window - .set_position(PhysicalPosition::new(120, 120)) - .map_err(|error| error.to_string())?; + if let Some(monitor) = available_monitor_info(&app)?.first() { + place_pet_on_monitor(&window, monitor, pet::PetPositionMode::Edge)?; + } window.show().map_err(|error| error.to_string())?; return Ok(()); } @@ -199,6 +328,66 @@ fn open_pet_window(app: AppHandle) -> Result<(), String> { .map_err(|error| error.to_string()) } +#[tauri::command] +fn list_monitor_geometries(app: AppHandle) -> Result, String> { + available_monitor_info(&app) +} + +#[tauri::command] +fn set_pet_window_preferences( + app: AppHandle, + preferences: PetWindowPreferences, +) -> Result<(), String> { + let Some(window) = app.get_webview_window(PET_WINDOW_ID) else { + return Ok(()); + }; + window + .set_always_on_top(preferences.always_on_top) + .map_err(|error| error.to_string())?; + let monitors = available_monitor_info(&app)?; + let index = preferences + .monitor_index + .unwrap_or(0) + .min(monitors.len().saturating_sub(1)); + let mode = if preferences.game_mode { + pet::PetPositionMode::Edge + } else { + preferences.position_mode + }; + if let Some(monitor) = monitors.get(index) { + place_pet_on_monitor(&window, monitor, mode)?; + } + Ok(()) +} + +#[tauri::command] +fn inspect_foreground_window() -> Result +{ + windows::foreground_window::inspect_foreground_window(std::time::Duration::from_secs(60)) + .map(|selection| selection.summary()) + .map_err(|error| error.to_string()) +} + +#[tauri::command] +fn inspect_process( + request: ProcessDiagnosticRequest, +) -> Result { + windows::process::inspect_process(request.process_id).map_err(|error| error.to_string()) +} + +#[tauri::command] +async fn check_network_target( + request: NetworkDiagnosticRequest, +) -> Result { + diagnostics::network::check_tcp_target(&diagnostics::network::NetworkTarget { + host: request.host, + port: request.port, + timeout: std::time::Duration::from_secs(request.timeout_seconds.unwrap_or(3).min(10)), + }) + .await + .map_err(|error| error.to_string()) +} + #[tauri::command] fn close_pet_window(app: AppHandle) -> Result<(), String> { if let Some(window) = app.get_webview_window(PET_WINDOW_ID) { @@ -336,6 +525,12 @@ pub fn run() { app.manage(ApplicationDatabase::open( app_data_dir.join("astrbot-desktop.sqlite3"), )?); + let session_marker = app_data_dir.join(SESSION_MARKER_FILE); + if session_marker.exists() { + tracing::warn!("previous AstrBot Desktop session ended unexpectedly; local state remains available"); + } + std::fs::write(&session_marker, std::process::id().to_string())?; + app.manage(SessionMarker(session_marker)); app.manage(RuntimeGuards::new()); app.manage(DualSlotRuntime::new()); @@ -346,9 +541,50 @@ pub fn run() { auto_connect_configured_slots(&database, &runtime).await; }); - let show = MenuItem::with_id(app, "show", "Show AstrBot Desktop", true, None::<&str>)?; - let quit = MenuItem::with_id(app, "quit", "Quit", true, None::<&str>)?; - let menu = Menu::with_items(app, &[&show, &quit])?; + let event_handle = app.handle().clone(); + tauri::async_runtime::spawn(async move { + loop { + let runtime = event_handle.state::(); + if let Some(event) = runtime.try_recv_event().await { + let database = event_handle.state::(); + networking::commands::persist_runtime_event(&database, &event); + let _ = event_handle.emit("connection-runtime-event", event); + } else { + tokio::time::sleep(std::time::Duration::from_millis(50)).await; + } + } + }); + + let show = MenuItem::with_id(app, "show", "打开主界面", true, None::<&str>)?; + let quick = MenuItem::with_id(app, "quick", "打开快捷聊天", true, None::<&str>)?; + let companion_status = + MenuItem::with_id(app, "companion-status", "陪伴:未连接", false, None::<&str>)?; + let operations_status = MenuItem::with_id( + app, + "operations-status", + "运维:未连接", + false, + None::<&str>, + )?; + let quit = MenuItem::with_id(app, "quit", "退出程序", true, None::<&str>)?; + let menu = Menu::with_items( + app, + &[&show, &quick, &companion_status, &operations_status, &quit], + )?; + + let tray_handle = app.handle().clone(); + let companion_status_handle = companion_status.clone(); + let operations_status_handle = operations_status.clone(); + tauri::async_runtime::spawn(async move { + loop { + let status = tray_handle.state::().status().await; + let _ = companion_status_handle + .set_text(tray_state_label("陪伴", status.companion.state)); + let _ = operations_status_handle + .set_text(tray_state_label("运维", status.operations.state)); + tokio::time::sleep(std::time::Duration::from_secs(1)).await; + } + }); TrayIconBuilder::with_id("main") .menu(&menu) @@ -359,6 +595,9 @@ pub fn run() { let _ = window.set_focus(); } } + "quick" => { + let _ = open_quick_chat_window(app.clone(), None); + } "quit" => exit_application(app), _ => {} }) @@ -396,6 +635,11 @@ pub fn run() { open_pet_window, close_pet_window, show_pet_context_menu, + list_monitor_geometries, + set_pet_window_preferences, + inspect_foreground_window, + inspect_process, + check_network_target, connection_policy_status, profiles::list_connection_profiles, profiles::create_connection_profile, diff --git a/src-tauri/src/networking/commands.rs b/src-tauri/src/networking/commands.rs index 5c50720..5d1ec4b 100644 --- a/src-tauri/src/networking/commands.rs +++ b/src-tauri/src/networking/commands.rs @@ -62,6 +62,7 @@ pub struct DrainConnectionRuntimeEventsRequest { #[serde(rename_all = "camelCase", deny_unknown_fields)] pub struct LoadChatMessagesRequest { pub profile_id: String, + pub slot: SlotKind, pub server_instance_id: String, pub session_id: String, pub limit: Option, @@ -71,6 +72,7 @@ pub struct LoadChatMessagesRequest { #[serde(rename_all = "camelCase", deny_unknown_fields)] pub struct ListChatSessionsRequest { pub profile_id: String, + pub slot: SlotKind, pub server_instance_id: String, } @@ -78,10 +80,18 @@ pub struct ListChatSessionsRequest { #[serde(rename_all = "camelCase", deny_unknown_fields)] pub struct MarkChatSessionReadRequest { pub profile_id: String, + pub slot: SlotKind, pub server_instance_id: String, pub session_id: String, } +fn slot_name(slot: SlotKind) -> &'static str { + match slot { + SlotKind::Companion => "companion", + SlotKind::Operations => "operations", + } +} + #[tauri::command] pub async fn connection_runtime_status( runtime: State<'_, DualSlotRuntime>, @@ -230,7 +240,11 @@ pub async fn list_chat_sessions( request: ListChatSessionsRequest, ) -> Result, RuntimeCommandError> { database - .list_chat_sessions(&request.profile_id, &request.server_instance_id) + .list_chat_sessions( + &request.profile_id, + slot_name(request.slot), + &request.server_instance_id, + ) .map_err(|_| RuntimeCommandError { code: "databaseUnavailable", message: "Chat sessions are unavailable.", @@ -245,6 +259,7 @@ pub async fn mark_chat_session_read( database .mark_chat_session_read( &request.profile_id, + slot_name(request.slot), &request.server_instance_id, &request.session_id, ) @@ -262,6 +277,7 @@ pub async fn load_chat_messages( database .list_chat_messages( &request.profile_id, + slot_name(request.slot), &request.server_instance_id, &request.session_id, request.limit.unwrap_or(100), @@ -272,7 +288,10 @@ pub async fn load_chat_messages( }) } -fn persist_runtime_event(database: &ApplicationDatabase, event: &super::BoundGatewayEvent) { +pub(crate) fn persist_runtime_event( + database: &ApplicationDatabase, + event: &super::BoundGatewayEvent, +) { let Some(session_id) = event.session_id.as_deref() else { return; }; @@ -553,40 +572,60 @@ pub async fn send_connection_slot_message( runtime: State<'_, DualSlotRuntime>, request: SendConnectionSlotMessageRequest, ) -> Result { + let message_for_history = request.message.clone(); let status = runtime.status().await; - let snapshot = match request.slot { + let initial_session = match request.slot { SlotKind::Companion => status.companion, SlotKind::Operations => status.operations, }; if let (Some(profile_id), Some(server_instance_id), Some(session_id)) = ( - snapshot.profile_id.as_deref(), - snapshot.server_instance_id.as_deref(), - snapshot.active_session_id.as_deref(), + initial_session.profile_id.as_deref(), + initial_session.server_instance_id.as_deref(), + initial_session.active_session_id.as_deref(), ) { - let slot = match request.slot { - SlotKind::Companion => "companion", - SlotKind::Operations => "operations", - }; let _ = database.upsert_chat_message( "local-default", profile_id, - slot, + slot_name(request.slot), server_instance_id, None, session_id, &format!("local-{}", uuid::Uuid::new_v4()), "user", - &request.message, + &message_for_history, false, false, ); } - runtime + let result = runtime .send_slot_message( request.slot, request.message, request.config_id, request.attachments, ) - .await + .await; + if initial_session.active_session_id.is_none() + && let Ok(snapshot) = &result + && let (Some(profile_id), Some(server_instance_id), Some(session_id)) = ( + snapshot.profile_id.as_deref(), + snapshot.server_instance_id.as_deref(), + snapshot.active_session_id.as_deref(), + ) + { + let _ = database.upsert_chat_message( + "local-default", + profile_id, + slot_name(request.slot), + server_instance_id, + None, + session_id, + &format!("local-{}", uuid::Uuid::new_v4()), + "user", + &message_for_history, + false, + false, + ); + } + result } diff --git a/src-tauri/src/networking/gateway_client.rs b/src-tauri/src/networking/gateway_client.rs index b5723af..5687fa5 100644 --- a/src-tauri/src/networking/gateway_client.rs +++ b/src-tauri/src/networking/gateway_client.rs @@ -83,6 +83,7 @@ impl EventDeduplicator { } } +#[derive(Clone)] pub struct GatewayClient { config: GatewayClientConfig, http: reqwest::Client, diff --git a/src-tauri/src/networking/runtime.rs b/src-tauri/src/networking/runtime.rs index 0d23bd8..e8d0693 100644 --- a/src-tauri/src/networking/runtime.rs +++ b/src-tauri/src/networking/runtime.rs @@ -3,7 +3,7 @@ use desktop_protocol::{ }; use secrecy::SecretString; use serde::Serialize; -use std::time::Duration; +use std::{sync::Arc, time::Duration}; use tokio::sync::{Mutex, mpsc}; use super::{ @@ -45,6 +45,7 @@ pub struct SlotActivationRequest { pub expected_server_instance_id: Option, } +#[derive(Clone)] enum ActiveClient { Desktop(GatewayClient), AstrBot(AstrBotClient), @@ -53,11 +54,17 @@ enum ActiveClient { struct SlotRuntime { snapshot: SlotRuntimeSnapshot, client: Option, - cancellation: Option, + connection_cancellation: Option>, + message_cancellation: Option>, + generation: u64, } impl SlotRuntime { fn empty(slot: SlotKind) -> Self { + Self::empty_with_generation(slot, 0) + } + + fn empty_with_generation(slot: SlotKind, generation: u64) -> Self { Self { snapshot: SlotRuntimeSnapshot { slot, @@ -69,7 +76,9 @@ impl SlotRuntime { last_error: None, }, client: None, - cancellation: None, + connection_cancellation: None, + message_cancellation: None, + generation, } } } @@ -78,7 +87,7 @@ impl SlotRuntime { /// /// Network activation is explicit and Rust-owned. React never receives secrets. pub struct DualSlotRuntime { - inner: Mutex, + inner: Arc>, event_tx: mpsc::Sender, event_rx: Mutex>, } @@ -123,15 +132,14 @@ impl DualSlotRuntime { pub fn new() -> Self { let (event_tx, event_rx) = mpsc::channel(256); Self { - inner: Mutex::new(DualSlotRuntimeInner { + inner: Arc::new(Mutex::new(DualSlotRuntimeInner { companion: SlotRuntime::empty(SlotKind::Companion), operations: SlotRuntime::empty(SlotKind::Operations), - }), + })), event_tx, event_rx: Mutex::new(event_rx), } } - pub async fn status(&self) -> ConnectionRuntimeStatus { let guard = self.inner.lock().await; ConnectionRuntimeStatus { @@ -157,11 +165,17 @@ impl DualSlotRuntime { } events } - pub async fn deactivate_slot(&self, slot: SlotKind) -> Result<(), RuntimeCommandError> { let mut guard = self.inner.lock().await; let runtime = slot_mut(&mut guard, slot); - *runtime = SlotRuntime::empty(slot); + if let Some(cancellation) = runtime.connection_cancellation.take() { + cancellation.cancel(); + } + if let Some(cancellation) = runtime.message_cancellation.take() { + cancellation.cancel(); + } + let generation = runtime.generation.wrapping_add(1); + *runtime = SlotRuntime::empty_with_generation(slot, generation); Ok(()) } @@ -182,6 +196,13 @@ impl DualSlotRuntime { let mut guard = self.inner.lock().await; let runtime = slot_mut(&mut guard, request.slot); + if let Some(cancellation) = runtime.connection_cancellation.take() { + cancellation.cancel(); + } + if let Some(cancellation) = runtime.message_cancellation.take() { + cancellation.cancel(); + } + let generation = runtime.generation.wrapping_add(1); *runtime = SlotRuntime { snapshot: SlotRuntimeSnapshot { slot: request.slot, @@ -193,7 +214,9 @@ impl DualSlotRuntime { last_error: None, }, client: None, - cancellation: None, + connection_cancellation: None, + message_cancellation: None, + generation, }; match request.transport { @@ -295,13 +318,97 @@ impl DualSlotRuntime { } } - Ok(runtime.snapshot.clone()) + let listener_client = runtime.client.clone(); + let connection_cancellation = Arc::new(tokio_util::sync::CancellationToken::new()); + runtime.connection_cancellation = Some(connection_cancellation.clone()); + let snapshot = runtime.snapshot.clone(); + drop(guard); + + if let Some(client) = listener_client { + self.start_connection_listener( + request.slot, + generation, + client, + connection_cancellation, + ); + } + + Ok(snapshot) + } + + fn start_connection_listener( + &self, + slot: SlotKind, + generation: u64, + client: ActiveClient, + cancellation: Arc, + ) { + let inner = Arc::clone(&self.inner); + let event_tx = self.event_tx.clone(); + let state_client = client.clone(); + let state_inner = Arc::clone(&self.inner); + let state_cancellation = Arc::clone(&cancellation); + tokio::spawn(async move { + let mut state_receiver = match state_client { + ActiveClient::Desktop(client) => client.state_receiver(), + ActiveClient::AstrBot(client) => client.state_receiver(), + }; + loop { + tokio::select! { + _ = state_cancellation.cancelled() => break, + changed = state_receiver.changed() => { + if changed.is_err() { break; } + let state = *state_receiver.borrow(); + let mut guard = state_inner.lock().await; + let runtime = slot_mut(&mut guard, slot); + if runtime.generation == generation { + runtime.snapshot.state = state; + if state == ConnectionState::Connected { + runtime.snapshot.last_error = None; + } + } else { + break; + } + } + } + } + }); + + tokio::spawn(async move { + let result = match client { + ActiveClient::Desktop(client) => { + client.run_reconnecting(&event_tx, &cancellation).await + } + ActiveClient::AstrBot(_) => { + cancellation.cancelled().await; + Ok(()) + } + }; + if let Err(error) = result { + let mut guard = inner.lock().await; + let runtime = slot_mut(&mut guard, slot); + if runtime.generation == generation { + runtime.snapshot.last_error = Some(error.to_string()); + runtime.snapshot.state = match error { + GatewayError::HttpStatus(status) + if status.as_u16() == 401 || status.as_u16() == 403 => + { + ConnectionState::AuthExpired + } + GatewayError::Protocol(_) | GatewayError::ServerIdentityChanged => { + ConnectionState::Incompatible + } + _ => ConnectionState::FatalError, + }; + } + } + }); } pub async fn cancel_slot_message(&self, slot: SlotKind) -> Result<(), RuntimeCommandError> { let guard = self.inner.lock().await; let runtime = slot_ref(&guard, slot); - let Some(cancellation) = runtime.cancellation.as_ref() else { + let Some(cancellation) = runtime.message_cancellation.as_ref() else { return Err(RuntimeCommandError::unavailable( "No chat request is active.", )); @@ -438,14 +545,22 @@ impl DualSlotRuntime { } // Take the client out so we can await without holding the mutex across network IO. - let (client, profile_id, transport, server_instance_id, session_id, cancellation) = { + let ( + client, + profile_id, + transport, + server_instance_id, + session_id, + cancellation, + generation, + ) = { let mut guard = self.inner.lock().await; let runtime = slot_mut(&mut guard, slot); let client = runtime.client.take().ok_or_else(|| { RuntimeCommandError::unavailable("The selected slot is not connected.") })?; - let cancellation = tokio_util::sync::CancellationToken::new(); - runtime.cancellation = Some(cancellation.clone()); + let cancellation = Arc::new(tokio_util::sync::CancellationToken::new()); + runtime.message_cancellation = Some(cancellation.clone()); runtime.snapshot.state = ConnectionState::Syncing; ( client, @@ -454,6 +569,7 @@ impl DualSlotRuntime { runtime.snapshot.server_instance_id.clone(), runtime.snapshot.active_session_id.clone(), cancellation, + runtime.generation, ) }; @@ -522,6 +638,13 @@ impl DualSlotRuntime { let mut guard = self.inner.lock().await; let runtime = slot_mut(&mut guard, slot); + if runtime.generation != generation { + return Err(RuntimeCommandError { + code: "cancelled", + message: "The chat request was cancelled because the slot changed.", + }); + } + runtime.message_cancellation = None; match send_result { Ok((client, session)) => { runtime.client = Some(client); @@ -541,10 +664,16 @@ impl DualSlotRuntime { runtime.snapshot.active_session_id = session_id; runtime.snapshot.state = if error.code == "authExpired" { ConnectionState::AuthExpired + } else if error.code == "cancelled" { + ConnectionState::Connected } else { ConnectionState::ServerUnavailable }; - runtime.snapshot.last_error = Some(error.message.to_owned()); + runtime.snapshot.last_error = if error.code == "cancelled" { + None + } else { + Some(error.message.to_owned()) + }; Err(error) } } diff --git a/src-tauri/src/windows/foreground_window.rs b/src-tauri/src/windows/foreground_window.rs index 692898b..5ea582c 100644 --- a/src-tauri/src/windows/foreground_window.rs +++ b/src-tauri/src/windows/foreground_window.rs @@ -3,6 +3,7 @@ use std::{ time::{Duration, Instant}, }; +use serde::Serialize; use uuid::Uuid; use super::process::{ProcessInspectError, process_creation_time}; @@ -10,7 +11,8 @@ use super::process::{ProcessInspectError, process_creation_time}; const MAX_WINDOW_TEXT_UNITS: usize = 1024; const MAX_SELECTION_TTL: Duration = Duration::from_secs(5 * 60); -#[derive(Clone, Debug, PartialEq, Eq)] +#[derive(Clone, Debug, PartialEq, Eq, Serialize)] +#[serde(rename_all = "camelCase")] pub struct ForegroundWindowSummary { pub selection_token: Uuid, pub title: String, diff --git a/src-tauri/src/windows/process.rs b/src-tauri/src/windows/process.rs index a48047b..e759035 100644 --- a/src-tauri/src/windows/process.rs +++ b/src-tauri/src/windows/process.rs @@ -1,6 +1,9 @@ use std::{fmt, io}; -#[derive(Clone, Debug, PartialEq, Eq)] +use serde::Serialize; + +#[derive(Clone, Debug, PartialEq, Eq, Serialize)] +#[serde(rename_all = "camelCase")] pub struct ProcessSummary { pub process_id: u32, pub executable_name: String, diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index 3dd71ad..e0d473f 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -1,58 +1,58 @@ -{ - "$schema": "https://schema.tauri.app/config/2", - "productName": "AstrBot Desktop", - "version": "0.1.0-alpha.1", - "identifier": "com.yokiirobw.astrbot-desktop", - "build": { - "beforeDevCommand": "npm run dev", - "devUrl": "http://127.0.0.1:1420", - "beforeBuildCommand": "npm run build", - "frontendDist": "../dist" - }, - "app": { - "windows": [ - { - "label": "main", - "title": "AstrBot Desktop", - "width": 1100, - "height": 760, - "minWidth": 720, - "minHeight": 520, - "center": true, - "resizable": true - }, - { - "label": "desktop-pet", - "title": "AstrBot Pet", - "url": "index.html?view=pet", - "width": 180, - "height": 220, - "resizable": false, - "decorations": false, - "shadow": false, - "transparent": true, - "alwaysOnTop": true, - "skipTaskbar": true, - "visible": false, - "x": 120, - "y": 120, - "create": true - } - ], - "security": { - "csp": "default-src \u0027self\u0027; connect-src ipc: http://ipc.localhost http://127.0.0.1:*; img-src \u0027self\u0027 asset: http://asset.localhost data:; style-src \u0027self\u0027 \u0027unsafe-inline\u0027; script-src \u0027self\u0027" - } - }, - "bundle": { - "active": true, - "targets": [ - "nsis" - ], - "icon": [ - "icons/32x32.png", - "icons/128x128.png", - "icons/128x128@2x.png", - "icons/icon.ico" - ] - } -} +{ + "$schema": "https://schema.tauri.app/config/2", + "productName": "AstrBot Desktop", + "version": "0.1.0-alpha.2", + "identifier": "com.yokiirobw.astrbot-desktop", + "build": { + "beforeDevCommand": "npm run dev", + "devUrl": "http://127.0.0.1:1420", + "beforeBuildCommand": "npm run build", + "frontendDist": "../dist" + }, + "app": { + "windows": [ + { + "label": "main", + "title": "AstrBot Desktop", + "width": 1100, + "height": 760, + "minWidth": 720, + "minHeight": 520, + "center": true, + "resizable": true, + "decorations": false, + "shadow": false + }, + { + "label": "desktop-pet", + "title": "AstrBot Pet", + "url": "index.html?view=pet", + "width": 180, + "height": 220, + "resizable": false, + "decorations": false, + "shadow": false, + "transparent": true, + "alwaysOnTop": true, + "skipTaskbar": true, + "visible": false, + "x": 120, + "y": 120, + "create": true + } + ], + "security": { + "csp": "default-src \u0027self\u0027; connect-src ipc: http://ipc.localhost http://127.0.0.1:*; img-src \u0027self\u0027 asset: http://asset.localhost data:; style-src \u0027self\u0027 \u0027unsafe-inline\u0027; script-src \u0027self\u0027" + } + }, + "bundle": { + "active": true, + "targets": ["nsis"], + "icon": [ + "icons/32x32.png", + "icons/128x128.png", + "icons/128x128@2x.png", + "icons/icon.ico" + ] + } +} diff --git a/src/App.tsx b/src/App.tsx index 65be483..6b0a126 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,19 +1,23 @@ -import { invoke } from "@tauri-apps/api/core"; +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 { ChatPanel } from "./features/chat/ChatPanel"; import { StatusBubble } from "./features/chat/StatusBubble"; import { ProfileManager } from "./features/profiles/ProfileManager"; import { PetPackImporter } from "./features/pet/PetPackImporter"; -import { connectionRuntimeStatusSchema } from "./features/chat/types"; +import { + boundRuntimeEventSchema, + connectionRuntimeStatusSchema, +} from "./features/chat/types"; import aemeathSpritesheet from "./features/pet/assets/aemeath/aemeath-spritesheet.png?inline"; import { bootstrapStatusSchema, type BootstrapStatus } from "./types/bootstrap"; type PageKey = "chat" | "connections" | "desktop" | "pet" | "diagnostics"; -type ChatLayout = "single" | "focus-companion" | "focus-operations"; type ChatSlot = "companion" | "operations"; type PetDisplayMode = "app-ui" | "desktop-floating"; +type ThemeMode = "dark" | "light"; const initialStatus: BootstrapStatus = { appVersion: "unknown", @@ -24,10 +28,16 @@ const initialStatus: BootstrapStatus = { }; const AEMEATH_SPRITESHEET = aemeathSpritesheet; +const PET_TARGET_SLOT = "companion" as const; -const AEMEATH_FRAMES: Record<"idle" | "working" | "resting" | "speaking" | "error", number[]> = { +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], + 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], @@ -51,12 +61,17 @@ export function App() { const view = params.get("view"); const isQuickView = view === "quick"; const isBubbleView = view === "bubble"; - const isPetView = view === "pet" || window.location.hash === "#pet" || document.documentElement.dataset.astrbotPet === "true"; + const isPetView = + view === "pet" || + window.location.hash === "#pet" || + document.documentElement.dataset.astrbotPet === "true"; const quickSlot = params.get("slot"); const [page, setPage] = useState("chat"); + const [theme, setTheme] = useState(() => + localStorage.getItem("astrbot.theme") === "light" ? "light" : "dark", + ); const [status, setStatus] = useState(initialStatus); const [error, setError] = useState(null); - const [chatLayout, setChatLayout] = useState("single"); const [quickTarget, setQuickTarget] = useState(() => localStorage.getItem("astrbot.quickTarget") === "operations" ? "operations" @@ -81,13 +96,21 @@ export function App() { } }, []); + useEffect(() => { + document.documentElement.dataset.theme = theme; + localStorage.setItem("astrbot.theme", theme); + }, [theme]); + useEffect(() => { void refreshStatus(); }, [refreshStatus]); useEffect(() => { if (isBubbleView || isPetView || isQuickView) return; - const command = petDisplayMode === "desktop-floating" ? "open_pet_window" : "close_pet_window"; + const command = + petDisplayMode === "desktop-floating" + ? "open_pet_window" + : "close_pet_window"; void invoke(command).catch((reason) => { setError(reason instanceof Error ? reason.message : String(reason)); }); @@ -113,93 +136,191 @@ export function App() { return (
- + +
+ + 本地客户端已启动 +
+ -
- - {error ?

IPC 暂不可用:{error}

: null} - {page === "chat" ? ( - - void invoke("open_quick_chat_window", { slot: quickTarget }) - } - /> - ) : 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} -
+
+ {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()} + /> + ) : null} +
+ {petDisplayMode === "app-ui" ? : null}
); } +function WindowChrome({ + theme, + onToggleTheme, +}: { + theme: ThemeMode; + onToggleTheme: () => void; +}) { + const [currentWindow, setCurrentWindow] = useState | null>(null); + const [maximized, setMaximized] = useState(false); + + useEffect(() => { + try { + const windowHandle = getCurrentWindow(); + setCurrentWindow(windowHandle); + void windowHandle.isMaximized().then(setMaximized); + } catch { + return; + } + }, []); + + const toggleMaximize = async () => { + if (!currentWindow) return; + await currentWindow.toggleMaximize(); + setMaximized(await currentWindow.isMaximized()); + }; + + return ( +
void toggleMaximize()} + onMouseDown={(event) => { + if (event.button === 0) void currentWindow?.startDragging(); + }} + > + AstrBot Desktop +
+ +
+ + + +
+
+
+ ); +} + function InAppPetOverlay() { const [visible, setVisible] = useState(true); const [position, setPosition] = useState(() => { try { - const saved = JSON.parse(localStorage.getItem("astrbot.petPosition") ?? "null"); + const saved = JSON.parse( + localStorage.getItem("astrbot.petPosition") ?? "null", + ); return saved && typeof saved.x === "number" && typeof saved.y === "number" ? saved : { x: 24, y: 96 }; @@ -207,7 +328,11 @@ function InAppPetOverlay() { return { x: 24, y: 96 }; } }); - const dragRef = useRef<{ pointerId: number; offsetX: number; offsetY: number } | null>(null); + const dragRef = useRef<{ + pointerId: number; + offsetX: number; + offsetY: number; + } | null>(null); const savePosition = (x: number, y: number) => { const next = { @@ -220,9 +345,11 @@ function InAppPetOverlay() { useEffect(() => { let unlisten: (() => void) | undefined; - void listen("pet-hide-requested", () => setVisible(false)).then((cleanup) => { - unlisten = cleanup; - }); + void listen("pet-hide-requested", () => setVisible(false)).then( + (cleanup) => { + unlisten = cleanup; + }, + ); return () => unlisten?.(); }, []); @@ -251,7 +378,8 @@ function InAppPetOverlay() { ); }} onPointerUp={(event) => { - if (dragRef.current?.pointerId === event.pointerId) dragRef.current = null; + if (dragRef.current?.pointerId === event.pointerId) + dragRef.current = null; }} onPointerCancel={() => { dragRef.current = null; @@ -291,93 +419,14 @@ function PageHeader({ page }: { page: PageKey }) { ); } -function ChatPage({ - chatLayout, - quickTarget, - onLayoutChange, - onOpenQuickChat, -}: { - chatLayout: ChatLayout; - quickTarget: ChatSlot; - onLayoutChange: (layout: ChatLayout) => void; - onOpenQuickChat: () => void; -}) { +function ChatPage() { return ( -
-
-
- Conversation hub -

今天想和哪个实例聊天?

-

陪伴实例适合日常交流,运维实例用于经过确认的本机诊断提案。

-
- -
-
-
-
- Workspace view - 对话布局 -
-
- onLayoutChange("single")} - > - 单面板 - - onLayoutChange("focus-companion")} - > - 聚焦陪伴 - - onLayoutChange("focus-operations")} - > - 聚焦运维 - -
- - 快速聊天目标:{slotLabel(quickTarget)} - -
- -
+
+
); } -function LayoutButton({ - active, - children, - onClick, -}: { - active: boolean; - children: React.ReactNode; - onClick: () => void; -}) { - return ( - - ); -} - function DesktopBehaviorPage({ bubbleTarget, quickTarget, @@ -391,6 +440,9 @@ function DesktopBehaviorPage({ onQuickTargetChange: (value: ChatSlot) => void; onOpenQuickChat: () => void; }) { + const [quietHours, setQuietHours] = useState( + () => localStorage.getItem("astrbot.quietHours") ?? "off", + ); return (
@@ -450,6 +502,24 @@ function DesktopBehaviorPage({ 本机能力层,不会与聊天状态混在一起。

+
+ + + +
); } @@ -461,6 +531,45 @@ function PetPage({ displayMode: PetDisplayMode; onDisplayModeChange: (value: PetDisplayMode) => void; }) { + const [monitors, setMonitors] = useState< + Array<{ + name?: string; + width: number; + height: number; + scaleFactor: number; + isPrimary: boolean; + }> + >([]); + const [monitorIndex, setMonitorIndex] = useState(0); + const [positionMode, setPositionMode] = useState("edge"); + const [alwaysOnTop, setAlwaysOnTop] = useState(true); + const [gameMode, setGameMode] = useState(false); + + useEffect(() => { + void invoke("list_monitor_geometries") + .then((value) => setMonitors(value as typeof monitors)) + .catch(() => setMonitors([])); + }, []); + + const applyPetPreferences = ( + next: Partial<{ + positionMode: string; + monitorIndex: number; + alwaysOnTop: boolean; + gameMode: boolean; + }> = {}, + ) => { + const preferences = { + positionMode: next.positionMode ?? positionMode, + monitorIndex: next.monitorIndex ?? monitorIndex, + alwaysOnTop: next.alwaysOnTop ?? alwaysOnTop, + gameMode: next.gameMode ?? gameMode, + }; + void invoke("set_pet_window_preferences", { preferences }).catch( + () => undefined, + ); + }; + return (
@@ -480,10 +589,14 @@ function PetPage({ > 应用内活动桌宠已显示,可直接拖动 )} + + + + + + + +
); } function DesktopPetView({ embedded = false }: { embedded?: boolean }) { - const [state, setState] = useState<"idle" | "working" | "resting" | "speaking" | "error">("idle"); + 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); @@ -528,12 +708,44 @@ function DesktopPetView({ embedded = false }: { embedded?: boolean }) { return () => document.documentElement.classList.remove("pet-view"); }, [embedded]); + const completionTimerRef = useRef(null); + useEffect(() => { let unlisten: (() => void) | undefined; - void listen("pet-game-mode-toggle", () => setGameMode((current) => !current)).then((cleanup) => { + let unlistenRuntime: (() => void) | undefined; + void listen("pet-game-mode-toggle", () => + setGameMode((current) => !current), + ).then((cleanup) => { unlisten = cleanup; }); - return () => unlisten?.(); + 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(() => { @@ -554,50 +766,68 @@ function DesktopPetView({ embedded = false }: { embedded?: boolean }) { useEffect(() => { let cancelled = false; - const refreshPetState = async () => { - try { - const result = connectionRuntimeStatusSchema.parse( - await invoke("connection_runtime_status"), - ); + void invoke("connection_runtime_status") + .then((value) => { if (cancelled) return; - const states = [result.companion.state, result.operations.state]; - if (states.includes("authExpired") || states.includes("fatalError")) { - setState("error"); - } else if (states.includes("syncing")) { - setState("working"); - } else if (states.includes("reconnecting")) { - setState("speaking"); - } else if (states.every((value) => value === "disconnected" || value === "offline")) { - setState("resting"); - } else { + 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 { + }) + .catch(() => { if (!cancelled) setState("idle"); - } - }; - void refreshPetState(); - const refreshTimer = window.setInterval(() => void refreshPetState(), 2000); - const timer = window.setInterval(() => { - setState((current) => (current === "idle" ? "resting" : current === "resting" ? "idle" : current)); - }, 4200); + }); return () => { cancelled = true; - window.clearInterval(refreshTimer); - window.clearInterval(timer); }; }, []); + 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); + 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 frame = + AEMEATH_FRAMES[state][frameIndex % AEMEATH_FRAMES[state].length]; const frameRow = Math.floor(frame / 8); const frameColumn = frame % 8; @@ -607,13 +837,23 @@ function DesktopPetView({ embedded = false }: { embedded?: boolean }) { if (!canvas || !image || !spriteReady) return; const context = canvas.getContext("2d"); if (!context) { - setSpriteError("?? Canvas ????"); + 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); + context.drawImage( + image, + frameColumn * 192, + frameRow * 208, + 192, + 208, + 0, + 0, + 192, + 208, + ); } catch (reason) { setSpriteError(reason instanceof Error ? reason.message : "??????"); } @@ -621,8 +861,13 @@ function DesktopPetView({ embedded = false }: { embedded?: boolean }) { return (
{ + if (embedded || event.button !== 0) return; + void getCurrentWindow().startDragging(); + }} onContextMenu={(event) => { event.preventDefault(); void invoke("show_pet_context_menu"); @@ -633,18 +878,6 @@ function DesktopPetView({ embedded = false }: { embedded?: boolean }) { role="img" aria-label={`桌宠状态:${state}`} > - setSpriteReady(true)} - onError={() => setSpriteError("????????")} - style={{ - left: -(frameColumn * 192), - top: -(frameRow * 208), - }} - />
- {spriteError ? {spriteError} : null} - {!spriteReady && !spriteError ? Loading pet... : null} - {state === "working" ? "正在写写画画" : state === "resting" ? "休息中" : state === "speaking" ? "正在回应" : state === "error" ? "需要重新登录或检查连接" : "陪伴中"} + {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, @@ -680,6 +1010,7 @@ function DiagnosticsPage({ value={String(status.databaseSchemaVersion)} /> +
Security boundary

本机能力保持可审计

@@ -718,10 +1049,6 @@ function SettingCard({ ); } -function slotLabel(slot: ChatSlot) { - return slot === "companion" ? "陪伴实例" : "运维实例"; -} - interface StatusCardProps { label: string; value: string; diff --git a/src/features/chat/ChatPanel.tsx b/src/features/chat/ChatPanel.tsx index b500349..4d88464 100644 --- a/src/features/chat/ChatPanel.tsx +++ b/src/features/chat/ChatPanel.tsx @@ -1,5 +1,6 @@ import { invoke } from "@tauri-apps/api/core"; -import { useCallback, useEffect, useMemo, useState } from "react"; +import { listen } from "@tauri-apps/api/event"; +import { useCallback, useEffect, useMemo, useRef, useState } from "react"; import { astrbotAttachmentSchema, @@ -9,7 +10,6 @@ import { connectionRuntimeStatusSchema, type AstrBotAttachment, type AstrBotCapabilitySnapshot, - type AstrBotModelOption, type BoundRuntimeEvent, remoteChatSessionSchema, type ChatMessage, @@ -17,6 +17,7 @@ import { type RemoteChatSession, type ConnectionRuntimeStatus, storedChatMessageSchema, + slotRuntimeSnapshotSchema, } from "./types"; const emptyStatus: ConnectionRuntimeStatus = { @@ -37,6 +38,16 @@ export interface ChatPanelProps { fixedSlot?: ChatSlot; } +type SpeechRecognitionLike = { + lang: string; + interimResults: boolean; + onresult: ((event: unknown) => void) | null; + onerror: (() => void) | null; + onend: (() => void) | null; + start: () => void; + stop: () => void; +}; + export function ChatPanel({ fixedSlot }: ChatPanelProps) { const [status, setStatus] = useState(emptyStatus); const [messages, setMessages] = useState([]); @@ -54,11 +65,15 @@ export function ChatPanel({ fixedSlot }: ChatPanelProps) { const [error, setError] = useState(null); const [capabilities, setCapabilities] = useState(null); - const [selectedModel, setSelectedModel] = useState(""); const [pendingAttachments, setPendingAttachments] = useState< AstrBotAttachment[] >([]); const [uploading, setUploading] = useState(false); + const [voiceListening, setVoiceListening] = useState(false); + const speechRecognition = useRef<{ + start: () => void; + stop: () => void; + } | null>(null); const [unreadCounts, setUnreadCounts] = useState>({ companion: 0, operations: 0, @@ -73,30 +88,42 @@ export function ChatPanel({ fixedSlot }: ChatPanelProps) { ), [messages, selectedSlot, status], ); + const visibleMessages = useMemo( + () => + activeMessages.filter( + (message) => message.content.trim().length > 0 || message.streaming || message.error, + ), + [activeMessages], + ); const refresh = useCallback(async () => { try { const result = await invoke("connection_runtime_status"); - setStatus(connectionRuntimeStatusSchema.parse(result)); + const parsed = connectionRuntimeStatusSchema.safeParse(result); + if (!parsed.success) { + setError("连接状态暂不可用,请检查连接配置。"); + return false; + } + setStatus(parsed.data); setError(null); return true; - } catch (reason) { - setError(reason instanceof Error ? reason.message : String(reason)); + } catch { + setError("连接状态暂不可用,请检查本地运行时。"); return false; } }, []); - const drainEvents = useCallback(async () => { - try { - const result = await invoke("drain_connection_runtime_events", { - request: { limit: 64 }, - }); - const events = boundRuntimeEventSchema.array().parse(result); - if (events.length > 0) - setMessages((current) => applyRuntimeEvents(current, events)); - } catch { - // Keep the browser preview usable without Rust IPC. - } + useEffect(() => { + let unlisten: (() => void) | undefined; + void listen("connection-runtime-event", (event) => { + const parsed = boundRuntimeEventSchema.safeParse(event.payload); + if (parsed.success) { + setMessages((current) => applyRuntimeEvents(current, [parsed.data])); + } + }).then((cleanup) => { + unlisten = cleanup; + }); + return () => unlisten?.(); }, []); useEffect(() => { @@ -111,6 +138,7 @@ export function ChatPanel({ fixedSlot }: ChatPanelProps) { void invoke("list_chat_sessions", { request: { profileId: snapshot.profileId, + slot: selectedSlot, serverInstanceId: snapshot.serverInstanceId, }, }) @@ -174,8 +202,7 @@ export function ChatPanel({ fixedSlot }: ChatPanelProps) { transport: selectedTransport, }; setCapabilities(null); - setSelectedModel(""); - if (!snapshot.profileId || snapshot.transport !== "astrbot-openapi") return; + if (!snapshot.profileId || snapshot.transport !== "astr-bot-open-api") return; let cancelled = false; void invoke("connection_slot_capabilities", { request: { slot: selectedSlot }, @@ -184,11 +211,6 @@ export function ChatPanel({ fixedSlot }: ChatPanelProps) { if (cancelled) return; const next = astrbotCapabilitySnapshotSchema.parse(result); setCapabilities(next); - setSelectedModel( - next.models.find((model) => model.isDefault)?.id ?? - next.models[0]?.id ?? - "", - ); }) .catch(() => undefined); return () => { @@ -218,6 +240,7 @@ export function ChatPanel({ fixedSlot }: ChatPanelProps) { void invoke("mark_chat_session_read", { request: { profileId: snapshot.profileId, + slot: selectedSlot, serverInstanceId: snapshot.serverInstanceId, sessionId: snapshot.activeSessionId, }, @@ -248,6 +271,7 @@ export function ChatPanel({ fixedSlot }: ChatPanelProps) { void invoke("load_chat_messages", { request: { profileId: snapshot.profileId, + slot: selectedSlot, serverInstanceId: snapshot.serverInstanceId, sessionId: snapshot.activeSessionId, limit: 100, @@ -256,9 +280,20 @@ export function ChatPanel({ fixedSlot }: ChatPanelProps) { .then((result) => { if (cancelled) return; const records = storedChatMessageSchema.array().parse(result); - setMessages((current) => - current - .filter((item) => item.slot !== selectedSlot) + setMessages((current) => { + const localUserMessages = current.filter( + (item) => + (item.id.startsWith("local-") || item.streaming) && + item.slot === selectedSlot && + item.sessionId === snapshot.activeSessionId, + ); + return current + .filter( + (item) => + item.slot !== selectedSlot || + item.sessionId !== snapshot.activeSessionId, + ) + .concat(localUserMessages) .concat( records.map((record) => ({ id: record.messageId, @@ -269,8 +304,8 @@ export function ChatPanel({ fixedSlot }: ChatPanelProps) { streaming: record.streaming, error: record.error, })), - ), - ); + ); + }); }) .catch(() => undefined); return () => { @@ -290,13 +325,12 @@ export function ChatPanel({ fixedSlot }: ChatPanelProps) { if (!available) return; timer = window.setInterval(() => { void refresh(); - void drainEvents(); }, 500); }); return () => { if (timer !== undefined) window.clearInterval(timer); }; - }, [drainEvents, refresh]); + }, [refresh]); const uploadFiles = async (files: FileList | null) => { if (!files?.length) return; @@ -345,14 +379,23 @@ export function ChatPanel({ fixedSlot }: ChatPanelProps) { }), ); try { - await invoke("send_connection_slot_message", { - request: { - slot: selectedSlot, - message, - configId: selectedModel || undefined, - attachments, - }, - }); + const result = slotRuntimeSnapshotSchema.parse( + await invoke("send_connection_slot_message", { + request: { + slot: selectedSlot, + message, + attachments, + }, + }), + ); + setStatus((current) => ({ ...current, [selectedSlot]: result })); + setMessages((current) => + current.map((item) => + item.id === id + ? { ...item, sessionId: result.activeSessionId } + : item, + ), + ); setError(null); } catch (reason) { setError(reason instanceof Error ? reason.message : String(reason)); @@ -364,7 +407,53 @@ export function ChatPanel({ fixedSlot }: ChatPanelProps) { } }; - const currentModels: AstrBotModelOption[] = capabilities?.models ?? []; + const toggleVoiceInput = () => { + if (voiceListening) { + speechRecognition.current?.stop(); + setVoiceListening(false); + return; + } + const browserWindow = window as unknown as { + SpeechRecognition?: new () => SpeechRecognitionLike; + webkitSpeechRecognition?: new () => SpeechRecognitionLike; + }; + const Recognition = + browserWindow.SpeechRecognition ?? browserWindow.webkitSpeechRecognition; + if (!Recognition) { + setError("当前 WebView 不支持语音输入。"); + return; + } + const recognition = new Recognition(); + recognition.lang = "zh-CN"; + recognition.interimResults = true; + recognition.onresult = (event) => { + const result = event as { + results?: ArrayLike>; + }; + setDraft( + Array.from(result.results ?? []) + .map((item) => item[0]?.transcript ?? "") + .join(""), + ); + }; + recognition.onerror = () => { + setError("语音输入失败,请检查麦克风权限。"); + setVoiceListening(false); + }; + recognition.onend = () => setVoiceListening(false); + speechRecognition.current = recognition; + recognition.start(); + setVoiceListening(true); + }; + + const speakMessage = (content: string) => { + if (!window.speechSynthesis) return; + window.speechSynthesis.cancel(); + const utterance = new SpeechSynthesisUtterance(content); + utterance.lang = "zh-CN"; + window.speechSynthesis.speak(utterance); + }; + return (
@@ -403,7 +492,9 @@ export function ChatPanel({ fixedSlot }: ChatPanelProps) { {status[selectedSlot].state === "authExpired" ? (
认证已过期 - 请到“连接”设置重新登录 AstrBot,当前槽位不会继续发送消息。 + + 请到“连接”设置重新登录 AstrBot,当前槽位不会继续发送消息。 + @@ -439,25 +530,35 @@ export function ChatPanel({ fixedSlot }: ChatPanelProps) {
) : null}
- {activeMessages.length === 0 ? ( + {visibleMessages.length === 0 ? (

当前槽位还没有消息。连接 Profile 后即可开始聊天。

) : ( - activeMessages.map((message) => ( + visibleMessages.map((message) => (
- - {message.role === "user" - ? "我" - : message.role === "assistant" - ? "AstrBot" - : "系统"} - -

{message.content || "(空消息)"}

- {message.streaming ? 生成中… : null} + +
+
+ {message.role === "user" ? "我" : message.role === "assistant" ? "AstrBot" : "系统"} + {message.streaming ? 生成中… : null} +
+

{message.content || "生成中…"}

+ {message.role === "assistant" && message.content ? ( + + ) : null} +
)) )} @@ -476,23 +577,15 @@ export function ChatPanel({ fixedSlot }: ChatPanelProps) { disabled={uploading || !capabilities?.fileUpload} /> - + {uploading ? 上传中… : null} +
{pendingAttachments.length > 0 ? (
@@ -559,7 +652,10 @@ function applyRuntimeEvents( const delta = payloadString(event, "delta") || payloadString(event, "content") || ""; const existing = next.find( - (item) => item.id === id && item.slot === event.slot, + (item) => + item.id === id && + item.slot === event.slot && + item.sessionId === event.sessionId, ); if (existing) { existing.content += delta; @@ -577,16 +673,28 @@ function applyRuntimeEvents( const id = event.messageId || payloadString(event, "messageId") || event.eventId; const existing = next.find( - (item) => item.id === id && item.slot === event.slot, + (item) => + item.id === id && + item.slot === event.slot && + item.sessionId === event.sessionId, ); - if (existing) existing.streaming = false; - else + const completedContent = payloadString(event, "content")?.trim() ?? ""; + if (existing) { + if (completedContent) { + existing.content = completedContent; + } else if (!existing.content.trim()) { + existing.content = "未收到有效回复"; + existing.error = true; + } + existing.streaming = false; + } else next.push({ id, sessionId: event.sessionId, slot: event.slot, role: "assistant", - content: payloadString(event, "content") || "", + content: completedContent || "未收到有效回复", + error: !completedContent, }); } else if (event.eventType === "error") { next.push({ diff --git a/src/features/chat/StatusBubble.tsx b/src/features/chat/StatusBubble.tsx index 951abd4..c5cbb65 100644 --- a/src/features/chat/StatusBubble.tsx +++ b/src/features/chat/StatusBubble.tsx @@ -2,6 +2,7 @@ import { useEffect, useState } from "react"; import { + chatSessionSchema, connectionRuntimeStatusSchema, type ConnectionRuntimeStatus, } from "./types"; @@ -13,11 +14,41 @@ const initialStatus: ConnectionRuntimeStatus = { export function StatusBubble() { const [status, setStatus] = useState(initialStatus); + const [unread, setUnread] = useState(0); + const [quiet, setQuiet] = useState(false); useEffect(() => { - const refresh = () => - invoke("connection_runtime_status") - .then((value) => setStatus(connectionRuntimeStatusSchema.parse(value))) + const refresh = () => { + const hour = new Date().getHours(); + setQuiet( + localStorage.getItem("astrbot.quietHours") === "night" && + (hour >= 22 || hour < 7), + ); + void invoke("connection_runtime_status") + .then((value) => { + const next = connectionRuntimeStatusSchema.parse(value); + setStatus(next); + return Promise.all( + (["companion", "operations"] as const).map(async (slot) => { + const snapshot = next[slot]; + if (!snapshot.profileId || !snapshot.serverInstanceId) return 0; + const sessions = chatSessionSchema.array().parse( + await invoke("list_chat_sessions", { + request: { + profileId: snapshot.profileId, + slot, + serverInstanceId: snapshot.serverInstanceId, + }, + }), + ); + return sessions.reduce( + (total, session) => total + session.unreadCount, + 0, + ); + }), + ).then((counts) => setUnread(counts[0] + counts[1])); + }) .catch(() => undefined); + }; refresh(); const timer = window.setInterval(refresh, 1000); return () => window.clearInterval(timer); @@ -25,7 +56,11 @@ export function StatusBubble() { const companion = status.companion.state; const operations = status.operations.state; const active = companion === "connected" || operations === "connected"; - const visibleState = active ? "在线" : stateLabel(companion, operations); + const visibleState = quiet + ? "静默中" + : active + ? "在线" + : stateLabel(companion, operations); return (
); @@ -58,12 +96,11 @@ function stateLabel( companion: ConnectionRuntimeStatus["companion"]["state"], operations: ConnectionRuntimeStatus["operations"]["state"], ): string { - if (companion === "connecting" || operations === "connecting") return "连接中"; - if (companion === "authExpired" || operations === "authExpired") return "需要认证"; - if ( - companion === "serverUnavailable" || - operations === "serverUnavailable" - ) + if (companion === "connecting" || operations === "connecting") + return "连接中"; + if (companion === "authExpired" || operations === "authExpired") + return "需要认证"; + if (companion === "serverUnavailable" || operations === "serverUnavailable") return "服务不可用"; return "离线"; -} +} diff --git a/src/features/chat/types.ts b/src/features/chat/types.ts index 571cccd..258c4b3 100644 --- a/src/features/chat/types.ts +++ b/src/features/chat/types.ts @@ -9,12 +9,12 @@ import { export const slotRuntimeSnapshotSchema = z .object({ slot: activeSlotKindSchema, - profileId: z.uuid().optional(), - transport: z.enum(["desktop-v1", "astrbot-openapi"]).optional(), + profileId: z.uuid().nullish(), + transport: z.enum(["desktop-v1", "astr-bot-open-api"]).nullish(), state: connectionStateSchema, - serverInstanceId: z.string().min(1).optional(), - activeSessionId: z.string().min(1).optional(), - lastError: z.string().optional(), + serverInstanceId: z.string().min(1).nullish(), + activeSessionId: z.string().min(1).nullish(), + lastError: z.string().nullish(), }) .strict(); @@ -70,7 +70,7 @@ export type ChatSlot = ActiveSlotKind; export interface ChatMessage { id: string; - sessionId?: string; + sessionId?: string | null; slot: ChatSlot; role: "user" | "assistant" | "system"; content: string; diff --git a/src/features/pet/types.ts b/src/features/pet/types.ts index 5572323..92dbe01 100644 --- a/src/features/pet/types.ts +++ b/src/features/pet/types.ts @@ -13,7 +13,7 @@ export const petDisplayModeSchema = z.enum(["app-ui", "desktop-floating"]); export const petPositionModeSchema = z.enum([ "free", "edge", - "指定屏幕", + "monitor", "follow-main-window", ]); export const bubbleClickTargetSchema = z.enum(["main", "quick"]); @@ -27,7 +27,10 @@ export const petMovementSchema = z.enum([ export const petAnimationSchema = z .object({ - id: z.string().regex(/^[a-z][a-z0-9-]{0,31}$/).optional(), + id: z + .string() + .regex(/^[a-z][a-z0-9-]{0,31}$/) + .optional(), state: companionStateSchema, frames: z.array(z.number().int().nonnegative()).min(1), fps: z.number().positive().max(60), diff --git a/src/features/profiles/ProfileManager.tsx b/src/features/profiles/ProfileManager.tsx index 3f9c8ed..024bf9a 100644 --- a/src/features/profiles/ProfileManager.tsx +++ b/src/features/profiles/ProfileManager.tsx @@ -20,6 +20,7 @@ import { type TransportKind, type UpdateConnectionProfileRequest, } from "../../types/profile"; +import { connectionRuntimeStatusSchema } from "../chat/types"; import { CredentialEnrollmentCard } from "./CredentialEnrollmentCard"; interface ProfileFormState { @@ -68,26 +69,43 @@ export function ProfileManager() { const [error, setError] = useState(null); const [notice, setNotice] = useState(null); - const loadProfiles = useCallback(async () => { - try { - const [profileResult, slotResult, policyResult] = await Promise.all([ - invoke("list_connection_profiles"), - invoke("list_connection_slots"), - invoke("connection_policy_status"), - ]); - setProfiles(connectionProfileListSchema.parse(profileResult)); - setSlots(connectionSlotListSchema.parse(slotResult)); - setPolicy(connectionPolicyStatusSchema.parse(policyResult)); - setError(null); - } catch (reason) { - setError(`Profile storage unavailable: ${errorMessage(reason)}`); - } - }, []); - - useEffect(() => { - void loadProfiles(); - }, [loadProfiles]); - + const loadProfiles = useCallback(async () => { + try { + const [profileResult, slotResult, policyResult, runtimeResult] = + await Promise.all([ + invoke("list_connection_profiles"), + invoke("list_connection_slots"), + invoke("connection_policy_status"), + invoke("connection_runtime_status"), + ]); + setProfiles(connectionProfileListSchema.parse(profileResult)); + const storedSlots = connectionSlotListSchema.parse(slotResult); + const runtime = connectionRuntimeStatusSchema.parse(runtimeResult); + setSlots(mergeRuntimeSlots(storedSlots, runtime)); + setPolicy(connectionPolicyStatusSchema.parse(policyResult)); + setError(null); + } catch (reason) { + setError(`Profile storage unavailable: ${errorMessage(reason)}`); + } + }, []); + + const refreshRuntimeStatus = useCallback(async () => { + try { + const runtime = connectionRuntimeStatusSchema.parse( + await invoke("connection_runtime_status"), + ); + setSlots((current) => mergeRuntimeSlots(current, runtime)); + } catch { + // Keep the last slot snapshot when the runtime is temporarily unavailable. + } + }, []); + + useEffect(() => { + void loadProfiles(); + const refreshTimer = window.setInterval(() => void refreshRuntimeStatus(), 2000); + return () => window.clearInterval(refreshTimer); + }, [loadProfiles, refreshRuntimeStatus]); + async function submitProfile(event: FormEvent) { event.preventDefault(); setBusy(true); @@ -442,10 +460,10 @@ export function ProfileManager() { } > - + - {form.transportKind === "astrbot-openapi" ? ( + {form.transportKind === "astr-bot-open-api" ? ( <>