feat(desktop): complete Q9 chat and stability increment
这个提交存在于:
父节点
92ad44d327
当前提交
ceb7512bf2
共有 25 个文件被更改,包括 2764 次插入 和 661 次删除
20
CHANGELOG.md
20
CHANGELOG.md
|
|
@ -1,10 +1,27 @@
|
||||||
# Changelog
|
# 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
|
## Unreleased
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|
||||||
- AstrBot OpenAPI adapter foundation: SSE mapper, dual-slot runtime skeleton, credential enrollment IPC, transport-aware profiles (ADR-0005).
|
- 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.
|
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.
|
- 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.
|
- 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.
|
- Required explicit approval for insecure HTTP and kept slots, connections, and credential enrollment outside P01.
|
||||||
|
|
||||||
|
|
|
||||||
6
Cargo.lock
自动生成
6
Cargo.lock
自动生成
|
|
@ -105,7 +105,7 @@ checksum = "7d902e3d592a523def97af8f317b08ce16b7ab854c1985a0c671e6f15cebc236"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "astrbot-desktop"
|
name = "astrbot-desktop"
|
||||||
version = "0.1.0-alpha.1"
|
version = "0.1.0-alpha.2"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"desktop-protocol",
|
"desktop-protocol",
|
||||||
"eventsource-stream",
|
"eventsource-stream",
|
||||||
|
|
@ -989,7 +989,7 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "desktop-protocol"
|
name = "desktop-protocol"
|
||||||
version = "0.1.0-alpha.1"
|
version = "0.1.0-alpha.2"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"schemars 1.2.1",
|
"schemars 1.2.1",
|
||||||
"serde",
|
"serde",
|
||||||
|
|
@ -2480,7 +2480,7 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mock-gateway"
|
name = "mock-gateway"
|
||||||
version = "0.1.0-alpha.1"
|
version = "0.1.0-alpha.2"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"async-stream",
|
"async-stream",
|
||||||
"axum",
|
"axum",
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ members = [
|
||||||
resolver = "3"
|
resolver = "3"
|
||||||
|
|
||||||
[workspace.package]
|
[workspace.package]
|
||||||
version = "0.1.0-alpha.1"
|
version = "0.1.0-alpha.2"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
rust-version = "1.97.1"
|
rust-version = "1.97.1"
|
||||||
license = "MIT OR Apache-2.0"
|
license = "MIT OR Apache-2.0"
|
||||||
|
|
|
||||||
|
|
@ -265,6 +265,7 @@ impl AstrBotStreamMapper {
|
||||||
}
|
}
|
||||||
"user_message_saved" => Vec::new(),
|
"user_message_saved" => Vec::new(),
|
||||||
"plain" => self.map_plain(frame),
|
"plain" => self.map_plain(frame),
|
||||||
|
"complete" => self.map_complete(frame),
|
||||||
"agent_stats" => {
|
"agent_stats" => {
|
||||||
let detail = frame.data.to_string();
|
let detail = frame.data.to_string();
|
||||||
vec![self.event(
|
vec![self.event(
|
||||||
|
|
@ -287,7 +288,7 @@ impl AstrBotStreamMapper {
|
||||||
"message_saved" | "end" => self.complete_if_needed(FinishReason::Stop),
|
"message_saved" | "end" => self.complete_if_needed(FinishReason::Stop),
|
||||||
"error" => {
|
"error" => {
|
||||||
let mut events = self.complete_if_needed(FinishReason::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());
|
.unwrap_or_else(|| "AstrBot stream error".to_owned());
|
||||||
events.push(self.event(
|
events.push(self.event(
|
||||||
frame.message_id,
|
frame.message_id,
|
||||||
|
|
@ -305,7 +306,7 @@ impl AstrBotStreamMapper {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn map_plain(&mut self, frame: AstrBotStreamFrame) -> Vec<GatewayEvent> {
|
fn map_plain(&mut self, frame: AstrBotStreamFrame) -> Vec<GatewayEvent> {
|
||||||
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) {
|
if chunk.is_empty() && frame.streaming.unwrap_or(false) {
|
||||||
// Keep empty streaming markers out of the UI unless they carry text.
|
// Keep empty streaming markers out of the UI unless they carry text.
|
||||||
return Vec::new();
|
return Vec::new();
|
||||||
|
|
@ -330,6 +331,37 @@ impl AstrBotStreamMapper {
|
||||||
self.emit_delta(chunk)
|
self.emit_delta(chunk)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn map_complete(&mut self, frame: AstrBotStreamFrame) -> Vec<GatewayEvent> {
|
||||||
|
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<GatewayEvent> {
|
fn emit_delta(&mut self, chunk: String) -> Vec<GatewayEvent> {
|
||||||
if chunk.is_empty() {
|
if chunk.is_empty() {
|
||||||
return Vec::new();
|
return Vec::new();
|
||||||
|
|
@ -397,6 +429,22 @@ fn string_from_value(value: &Value) -> Option<String> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn text_from_value(value: &Value) -> Option<String> {
|
||||||
|
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::<String>();
|
||||||
|
(!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 {
|
fn current_timestamp() -> String {
|
||||||
// Protocol timestamps are opaque strings for consumers; keep a stable UTC-like form
|
// Protocol timestamps are opaque strings for consumers; keep a stable UTC-like form
|
||||||
// without adding a time dependency to the protocol crate.
|
// 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]
|
#[test]
|
||||||
fn maps_error_frames() {
|
fn maps_error_frames() {
|
||||||
let mut mapper = AstrBotStreamMapper::new("astrbot-test");
|
let mut mapper = AstrBotStreamMapper::new("astrbot-test");
|
||||||
|
|
|
||||||
290
docs/progress.md
290
docs/progress.md
|
|
@ -1,72 +1,72 @@
|
||||||
# Development Progress
|
# Development Progress
|
||||||
|
|
||||||
## Current phase
|
## Current phase
|
||||||
|
|
||||||
P02 dual connections remains active on `feat/p02-dual-connections`.
|
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.
|
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.
|
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.
|
Full product chat UI activation and production identity enrollment remain open for acceptance.
|
||||||
|
|
||||||
## Completed
|
## Completed
|
||||||
|
|
||||||
### Prior phases
|
### Prior phases
|
||||||
|
|
||||||
- P00 bootstrap merged.
|
- P00 bootstrap merged.
|
||||||
- P01 foundation merged through Forgejo PR #2.
|
- P01 foundation merged through Forgejo PR #2.
|
||||||
- P02 first increment: persistent dual slot assignment (`bcc08cf`).
|
- P02 first increment: persistent dual slot assignment (`bcc08cf`).
|
||||||
- P02 second increment: auto-connect validation and operations switch guard (`34a563c`).
|
- P02 second increment: auto-connect validation and operations switch guard (`34a563c`).
|
||||||
|
|
||||||
### Real-protocol foundation (this session)
|
### Real-protocol foundation (this session)
|
||||||
|
|
||||||
- Reviewed local AstrBot clone and live probe target `http://192.168.31.210:26285`.
|
- 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.
|
- 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 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.
|
- Added `TransportKind` (`desktop-v1` | `astrbot-openapi`) plus `remote_username` / `sticky_session_id` profile fields.
|
||||||
- Migrated SQLite schema to v4.
|
- Migrated SQLite schema to v4.
|
||||||
- Added Rust `AstrBotClient` for probe/list/send chat stream.
|
- Added Rust `AstrBotClient` for probe/list/send chat stream.
|
||||||
- Added `DualSlotRuntime` with activate/probe/send/deactivate skeleton.
|
- 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 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.
|
- Added frontend transport fields and credential enrollment card.
|
||||||
- Recorded ADR-0005 and live probe report.
|
- Recorded ADR-0005 and live probe report.
|
||||||
|
|
||||||
## In progress
|
## In progress
|
||||||
|
|
||||||
- User acceptance of real-protocol foundation.
|
- User acceptance of real-protocol foundation.
|
||||||
- Next product increments: chat panel rendering, history persistence, JWT login path, stronger server identity UX.
|
- Next product increments: chat panel rendering, history persistence, JWT login path, stronger server identity UX.
|
||||||
|
|
||||||
## Remaining
|
## Remaining
|
||||||
|
|
||||||
- Wire auto-connect startup to DualSlotRuntime without weakening secret boundary.
|
- Wire auto-connect startup to DualSlotRuntime without weakening secret boundary.
|
||||||
- Dual chat UI panels consuming normalized runtime events.
|
- Dual chat UI panels consuming normalized runtime events.
|
||||||
- Message persistence for AstrBot mapped events.
|
- Message persistence for AstrBot mapped events.
|
||||||
- Dashboard JWT auth strategy.
|
- Dashboard JWT auth strategy.
|
||||||
- File attachment OpenAPI path.
|
- File attachment OpenAPI path.
|
||||||
- Certificate fingerprint binding.
|
- Certificate fingerprint binding.
|
||||||
- Full stage gate (fmt/clippy/typecheck/build/installer) before merge.
|
- Full stage gate (fmt/clippy/typecheck/build/installer) before merge.
|
||||||
|
|
||||||
## Blockers / reserved for user
|
## Blockers / reserved for user
|
||||||
|
|
||||||
- Production API Key rotation after shared test key exposure in chat.
|
- Production API Key rotation after shared test key exposure in chat.
|
||||||
- Optional second AstrBot container if true dual-server soak is required.
|
- Optional second AstrBot container if true dual-server soak is required.
|
||||||
- Product decision on companion/operations remote username naming convention.
|
- Product decision on companion/operations remote username naming convention.
|
||||||
- TLS-terminated HTTPS endpoint if pinning must be validated before release.
|
- TLS-terminated HTTPS endpoint if pinning must be validated before release.
|
||||||
|
|
||||||
## Test status
|
## Test status
|
||||||
|
|
||||||
- `desktop-protocol` lib tests: 4 passed (AstrBot mapper).
|
- `desktop-protocol` lib tests: 4 passed (AstrBot mapper).
|
||||||
- `astrbot-desktop` lib tests: 58 passed.
|
- `astrbot-desktop` lib tests: 58 passed.
|
||||||
- Vitest: 6 passed.
|
- Vitest: 6 passed.
|
||||||
- Live non-destructive probe artifacts retained under `docs/reports/` (secrets redacted).
|
- Live non-destructive probe artifacts retained under `docs/reports/` (secrets redacted).
|
||||||
- Full release build / Clippy gate / Playwright not re-run in this autonomous stretch.
|
- Full release build / Clippy gate / Playwright not re-run in this autonomous stretch.
|
||||||
|
|
||||||
## Current branch
|
## Current branch
|
||||||
|
|
||||||
- `feat/p02-dual-connections`
|
- `feat/p02-dual-connections`
|
||||||
|
|
||||||
## Next step after acceptance
|
## Next step after acceptance
|
||||||
|
|
||||||
1. Chat panel MVP against AstrBot adapter.
|
1. Chat panel MVP against AstrBot adapter.
|
||||||
2. Persist sticky session + message history under profile/server/session keys.
|
2. Persist sticky session + message history under profile/server/session keys.
|
||||||
3. Optional auto-connect activation using enrolled credential references only.
|
3. Optional auto-connect activation using enrolled credential references only.
|
||||||
|
|
||||||
## 2026-07-20: remote identity and runnable paper pet increment
|
## 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.
|
- 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.
|
- 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.
|
- Frontend checks pass; Rust test/build gate remains to be rerun after the final patch.
|
||||||
|
|
||||||
|
## Queue decision
|
||||||
## Queue decision
|
|
||||||
|
- Q1-Q3 complete the real AstrBot text-chat path and main UI before visual polish.
|
||||||
- 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.
|
||||||
- Q4 adds the draggable/resizable quick window using the same chat state.
|
- Q5 adds the status bubble and configurable click routing.
|
||||||
- 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.
|
||||||
- Q6-Q7 reserve pet assets, multi-monitor geometry, and game-mode policy without adding arbitrary desktop control.
|
|
||||||
|
## Autonomous queue increment
|
||||||
|
|
||||||
## Autonomous queue increment
|
Completed in this 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.
|
||||||
- Q1: Rust-owned bounded runtime event drain IPC and normalized event identity fields.
|
- Q3: main chat panel MVP with Companion/Operations switching, streaming event merge, send error state, and session history restore.
|
||||||
- Q2: SQLite schema v5 chat message persistence keyed by account/profile/slot/server/session/message.
|
- Q4: dynamic Tauri quick-chat window sharing the Rust runtime.
|
||||||
- Q3: main chat panel MVP with Companion/Operations switching, streaming event merge, send error state, and session history restore.
|
- Q5: non-default-topmost status bubble window with runtime status display.
|
||||||
- Q4: dynamic Tauri quick-chat window sharing the Rust runtime.
|
- Q6: pet state, safe PNG/WebP manifest schema, monitor geometry, and safe-edge placement model.
|
||||||
- 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.
|
||||||
|
|
||||||
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.
|
||||||
|
|
||||||
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 启动自动连接增量
|
||||||
|
|
||||||
## 2026-07-20:Q1 启动自动连接增量
|
- 已完成:在 Tauri 启动阶段读取已保存的 `auto_connect` Profile,并按 Companion / Operations 槽位独立激活。
|
||||||
|
- 已完成:启动失败保留在对应 Runtime 状态,不阻断另一槽位;凭据仍由 Rust 读取,React 不接触秘密值。
|
||||||
- 已完成:在 Tauri 启动阶段读取已保存的 `auto_connect` Profile,并按 Companion / Operations 槽位独立激活。
|
- 已完成:启动自动连接复用现有 AstrBot OpenAPI 与 Desktop Gateway 激活路径。
|
||||||
- 已完成:启动失败保留在对应 Runtime 状态,不阻断另一槽位;凭据仍由 Rust 读取,React 不接触秘密值。
|
- 验证:`cargo check --workspace --all-targets`、`cargo test --workspace`(59 项通过)、`cargo clippy --workspace --all-targets -- -D warnings`、`pnpm.cmd run typecheck` 通过。
|
||||||
- 已完成:启动自动连接复用现有 AstrBot OpenAPI 与 Desktop Gateway 激活路径。
|
- 未完成:前端启动连接状态提示、自动重连退避、JWT 登录、聊天布局和快速窗口共享状态完善。
|
||||||
- 验证:`cargo check --workspace --all-targets`、`cargo test --workspace`(59 项通过)、`cargo clippy --workspace --all-targets -- -D warnings`、`pnpm.cmd run typecheck` 通过。
|
- 当前工作区:本地未提交,等待下一项 Q1/Q2 增量完成后按用户指令提交。
|
||||||
- 未完成:前端启动连接状态提示、自动重连退避、JWT 登录、聊天布局和快速窗口共享状态完善。
|
|
||||||
- 当前工作区:本地未提交,等待下一项 Q1/Q2 增量完成后按用户指令提交。
|
## 2026-07-20??? 1-6 ??????
|
||||||
|
|
||||||
|
- Q1????????????????????????????
|
||||||
## 2026-07-20??? 1-6 ??????
|
- Q2??? SQLite schema v6 ???????????????????????60 ? Rust ?????
|
||||||
|
- Q3?????????????/????/?????????????? Hook ?????
|
||||||
- Q1????????????????????????????
|
- Q4???????????????? URL ???
|
||||||
- Q2??? SQLite schema v6 ???????????????????????60 ? Rust ?????
|
- Q5????????????/????????????????????????????
|
||||||
- Q3?????????????/????/?????????????? Hook ?????
|
- Q6????? JSON manifest ???? UI?????????????PNG/WebP ??????????????
|
||||||
- Q4???????????????? URL ???
|
- ?????`2e84ea5`?`5dfac59`?`e0b48c0`?`9f235a3`?`4f13581`?`1828c61` ???? Forgejo?
|
||||||
- Q5????????????/????????????????????????????
|
|
||||||
- Q6????? JSON manifest ???? UI?????????????PNG/WebP ??????????????
|
|
||||||
- ?????`2e84ea5`?`5dfac59`?`e0b48c0`?`9f235a3`?`4f13581`?`1828c61` ???? Forgejo?
|
|
||||||
|
|
||||||
## 2026-07-20??????????
|
## 2026-07-20??????????
|
||||||
|
|
||||||
|
|
@ -129,7 +126,6 @@ Still not claimed complete: automatic startup activation, real Tauri multi-windo
|
||||||
- ??????????????/?????????????????? Windows ?????
|
- ??????????????/?????????????????? Windows ?????
|
||||||
- ?????TypeScript?ESLint?Vitest 6/6?Vite production build ???
|
- ?????TypeScript?ESLint?Vitest 6/6?Vite production build ???
|
||||||
|
|
||||||
|
|
||||||
## 2026-07-20: chat extensibility contract
|
## 2026-07-20: chat extensibility contract
|
||||||
|
|
||||||
- Added Rust-owned AstrBot capability discovery, model configuration options, and slot-scoped IPC.
|
- 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.
|
- 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.
|
- 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
|
## 2026-07-20: live chat extensibility probe
|
||||||
|
|
||||||
- Both supplied containers returned status=ok for chat sessions and config discovery.
|
- 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.
|
- 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.
|
- 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
|
## 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.
|
- 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.
|
- 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.
|
- This validates the current model/config and attachment request contract against a real AstrBot instance.
|
||||||
|
|
||||||
|
|
||||||
## 2026-07-20: remote session navigation
|
## 2026-07-20: remote session navigation
|
||||||
|
|
||||||
- Added slot-scoped remote session discovery and sticky session switching IPC.
|
- 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.
|
- 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.
|
- 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
|
## 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.
|
- 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.
|
- Completed: added window bounds, monitor clamping, and database round-trip tests.
|
||||||
- Validation: Rust format, Clippy with warnings denied, and `astrbot-desktop` unit tests pass.
|
- 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.
|
- 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
|
## 2026-07-20: status bubble text cleanup
|
||||||
|
|
||||||
- Fixed the remaining mojibake in status-bubble state labels and accessibility text.
|
- Fixed the remaining mojibake in status-bubble state labels and accessibility text.
|
||||||
- Validation: TypeScript, ESLint, and Vitest (6 tests) pass.
|
- Validation: TypeScript, ESLint, and Vitest (6 tests) pass.
|
||||||
|
|
||||||
## 2026-07-20: quick-chat close behavior fix
|
## 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.
|
- 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 `<img>` spritesheet renderer for more reliable WebView2/asset-protocol loading.
|
- Replaced the floating pet CSS background-image crop with a clipped `<img>` 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.
|
- 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.
|
- 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
|
## 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.
|
- 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.
|
- 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
|
## 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.
|
- 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.
|
- 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
|
## 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.
|
- 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.
|
- 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
|
## 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.
|
- 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.
|
- 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
|
## 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.
|
- 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.
|
- 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.
|
- 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
|
## 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.
|
- 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.
|
- 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.
|
- 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.
|
||||||
|
|
|
||||||
34
docs/q9-local-acceptance.md
普通文件
34
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`
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "astrbot-desktop",
|
"name": "astrbot-desktop",
|
||||||
"version": "0.1.0-alpha.1",
|
"version": "0.1.0-alpha.2",
|
||||||
"private": true,
|
"private": true,
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"packageManager": "pnpm@11.9.0",
|
"packageManager": "pnpm@11.9.0",
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ use time::{OffsetDateTime, format_description::well_known::Rfc3339};
|
||||||
use rusqlite::{Connection, OptionalExtension, params};
|
use rusqlite::{Connection, OptionalExtension, params};
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
pub const CURRENT_SCHEMA_VERSION: u32 = 9;
|
pub const CURRENT_SCHEMA_VERSION: u32 = 10;
|
||||||
|
|
||||||
pub struct ApplicationDatabase {
|
pub struct ApplicationDatabase {
|
||||||
store: Mutex<ProfileMessageStore>,
|
store: Mutex<ProfileMessageStore>,
|
||||||
|
|
@ -70,29 +70,32 @@ impl ApplicationDatabase {
|
||||||
pub fn list_chat_sessions(
|
pub fn list_chat_sessions(
|
||||||
&self,
|
&self,
|
||||||
profile_id: &str,
|
profile_id: &str,
|
||||||
|
slot: &str,
|
||||||
server_instance_id: &str,
|
server_instance_id: &str,
|
||||||
) -> Result<Vec<StoredChatSession>, DatabaseError> {
|
) -> Result<Vec<StoredChatSession>, DatabaseError> {
|
||||||
self.store
|
self.store
|
||||||
.lock()
|
.lock()
|
||||||
.map_err(|_| DatabaseError::LockPoisoned)?
|
.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(
|
pub fn mark_chat_session_read(
|
||||||
&self,
|
&self,
|
||||||
profile_id: &str,
|
profile_id: &str,
|
||||||
|
slot: &str,
|
||||||
server_instance_id: &str,
|
server_instance_id: &str,
|
||||||
session_id: &str,
|
session_id: &str,
|
||||||
) -> Result<(), DatabaseError> {
|
) -> Result<(), DatabaseError> {
|
||||||
self.store
|
self.store
|
||||||
.lock()
|
.lock()
|
||||||
.map_err(|_| DatabaseError::LockPoisoned)?
|
.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(
|
pub fn list_chat_messages(
|
||||||
&self,
|
&self,
|
||||||
profile_id: &str,
|
profile_id: &str,
|
||||||
|
slot: &str,
|
||||||
server_instance_id: &str,
|
server_instance_id: &str,
|
||||||
session_id: &str,
|
session_id: &str,
|
||||||
limit: usize,
|
limit: usize,
|
||||||
|
|
@ -100,7 +103,7 @@ impl ApplicationDatabase {
|
||||||
self.store
|
self.store
|
||||||
.lock()
|
.lock()
|
||||||
.map_err(|_| DatabaseError::LockPoisoned)?
|
.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(
|
pub fn load_window_state(
|
||||||
&self,
|
&self,
|
||||||
|
|
@ -176,10 +179,11 @@ impl ProfileMessageStore {
|
||||||
pub fn list_chat_sessions(
|
pub fn list_chat_sessions(
|
||||||
&self,
|
&self,
|
||||||
profile_id: &str,
|
profile_id: &str,
|
||||||
|
slot: &str,
|
||||||
server_instance_id: &str,
|
server_instance_id: &str,
|
||||||
) -> Result<Vec<StoredChatSession>, DatabaseError> {
|
) -> Result<Vec<StoredChatSession>, 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 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, server_instance_id], |row| {
|
let rows = statement.query_map(params![profile_id, slot, server_instance_id], |row| {
|
||||||
Ok(StoredChatSession {
|
Ok(StoredChatSession {
|
||||||
account_id: row.get(0)?,
|
account_id: row.get(0)?,
|
||||||
profile_id: row.get(1)?,
|
profile_id: row.get(1)?,
|
||||||
|
|
@ -199,10 +203,11 @@ impl ProfileMessageStore {
|
||||||
pub fn mark_chat_session_read(
|
pub fn mark_chat_session_read(
|
||||||
&self,
|
&self,
|
||||||
profile_id: &str,
|
profile_id: &str,
|
||||||
|
slot: &str,
|
||||||
server_instance_id: &str,
|
server_instance_id: &str,
|
||||||
session_id: &str,
|
session_id: &str,
|
||||||
) -> Result<(), DatabaseError> {
|
) -> 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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -279,15 +284,22 @@ impl ProfileMessageStore {
|
||||||
let was_streaming: Option<bool> = self
|
let was_streaming: Option<bool> = self
|
||||||
.connection
|
.connection
|
||||||
.query_row(
|
.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",
|
"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, server_instance_id, session_id, message_id],
|
params![profile_id, slot, server_instance_id, session_id, message_id],
|
||||||
|row| row.get(0),
|
|row| row.get(0),
|
||||||
)
|
)
|
||||||
.optional()?;
|
.optional()?;
|
||||||
self.connection.execute(
|
if streaming {
|
||||||
"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",
|
self.connection.execute(
|
||||||
params![account_id, profile_id, slot, server_instance_id, remote_username, session_id, message_id, role, content, streaming, error, timestamp],
|
"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);
|
let unread = role != "user" && !streaming && !error && was_streaming != Some(false);
|
||||||
self.connection.execute(
|
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",
|
"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(
|
pub fn list_chat_messages(
|
||||||
&self,
|
&self,
|
||||||
profile_id: &str,
|
profile_id: &str,
|
||||||
|
slot: &str,
|
||||||
server_instance_id: &str,
|
server_instance_id: &str,
|
||||||
session_id: &str,
|
session_id: &str,
|
||||||
limit: usize,
|
limit: usize,
|
||||||
) -> Result<Vec<StoredChatMessage>, DatabaseError> {
|
) -> Result<Vec<StoredChatMessage>, DatabaseError> {
|
||||||
let limit = limit.clamp(1, 200) as i64;
|
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(
|
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| {
|
|row| {
|
||||||
Ok(StoredChatMessage {
|
Ok(StoredChatMessage {
|
||||||
account_id: row.get(0)?,
|
account_id: row.get(0)?,
|
||||||
|
|
@ -537,6 +550,14 @@ fn migrate(connection: &mut Connection) -> Result<(), DatabaseError> {
|
||||||
transaction.commit()?;
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -628,22 +649,96 @@ mod tests {
|
||||||
)
|
)
|
||||||
.expect("duplicate completion");
|
.expect("duplicate completion");
|
||||||
let sessions = database
|
let sessions = database
|
||||||
.list_chat_sessions("profile-a", "server-a")
|
.list_chat_sessions("profile-a", "companion", "server-a")
|
||||||
.expect("sessions");
|
.expect("sessions");
|
||||||
assert_eq!(sessions.len(), 1);
|
assert_eq!(sessions.len(), 1);
|
||||||
assert_eq!(sessions[0].unread_count, 1);
|
assert_eq!(sessions[0].unread_count, 1);
|
||||||
database
|
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");
|
.expect("mark read");
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
database
|
database
|
||||||
.list_chat_sessions("profile-a", "server-a")
|
.list_chat_sessions("profile-a", "companion", "server-a")
|
||||||
.expect("sessions after read")[0]
|
.expect("sessions after read")[0]
|
||||||
.unread_count,
|
.unread_count,
|
||||||
0
|
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::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,9 @@
|
||||||
use std::{fmt, net::SocketAddr, time::Duration};
|
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 struct NetworkTarget {
|
||||||
pub host: String,
|
pub host: String,
|
||||||
pub port: u16,
|
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 struct NetworkAttempt {
|
||||||
pub address: SocketAddr,
|
pub address: SocketAddr,
|
||||||
pub connected: bool,
|
pub connected: bool,
|
||||||
pub error_kind: Option<String>,
|
pub error_kind: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct NetworkCheckResult {
|
pub struct NetworkCheckResult {
|
||||||
pub resolved_addresses: usize,
|
pub resolved_addresses: usize,
|
||||||
pub connected_address: Option<SocketAddr>,
|
pub connected_address: Option<SocketAddr>,
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@ use quick_window::{
|
||||||
QuickWindowState,
|
QuickWindowState,
|
||||||
};
|
};
|
||||||
use runtime_guards::RuntimeGuards;
|
use runtime_guards::RuntimeGuards;
|
||||||
use serde::Serialize;
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use tauri::{
|
use tauri::{
|
||||||
AppHandle, Emitter, Manager, PhysicalPosition, PhysicalSize, WebviewUrl, WebviewWindowBuilder,
|
AppHandle, Emitter, Manager, PhysicalPosition, PhysicalSize, WebviewUrl, WebviewWindowBuilder,
|
||||||
|
|
@ -33,6 +33,114 @@ use tauri::{
|
||||||
};
|
};
|
||||||
|
|
||||||
const PET_WINDOW_ID: &str = "desktop-pet";
|
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<String>,
|
||||||
|
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<usize>,
|
||||||
|
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<u64>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Deserialize)]
|
||||||
|
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||||
|
struct ProcessDiagnosticRequest {
|
||||||
|
process_id: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn available_monitor_info(app: &AppHandle) -> Result<Vec<MonitorInfo>, 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)]
|
#[derive(Debug, Clone, Serialize)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
|
|
@ -160,10 +268,31 @@ fn save_quick_window_state(window: &tauri::Window, app: &AppHandle) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn exit_application(app: &AppHandle) {
|
fn exit_application(app: &AppHandle) {
|
||||||
|
if let Some(marker) = app.try_state::<SessionMarker>() {
|
||||||
|
let _ = std::fs::remove_file(&marker.0);
|
||||||
|
}
|
||||||
app.cleanup_before_exit();
|
app.cleanup_before_exit();
|
||||||
std::process::exit(0);
|
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]
|
#[tauri::command]
|
||||||
fn open_main_window(app: AppHandle) -> Result<(), String> {
|
fn open_main_window(app: AppHandle) -> Result<(), String> {
|
||||||
let window = app
|
let window = app
|
||||||
|
|
@ -179,9 +308,9 @@ fn open_pet_window(app: AppHandle) -> Result<(), String> {
|
||||||
window
|
window
|
||||||
.set_always_on_top(true)
|
.set_always_on_top(true)
|
||||||
.map_err(|error| error.to_string())?;
|
.map_err(|error| error.to_string())?;
|
||||||
window
|
if let Some(monitor) = available_monitor_info(&app)?.first() {
|
||||||
.set_position(PhysicalPosition::new(120, 120))
|
place_pet_on_monitor(&window, monitor, pet::PetPositionMode::Edge)?;
|
||||||
.map_err(|error| error.to_string())?;
|
}
|
||||||
window.show().map_err(|error| error.to_string())?;
|
window.show().map_err(|error| error.to_string())?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
@ -199,6 +328,66 @@ fn open_pet_window(app: AppHandle) -> Result<(), String> {
|
||||||
.map_err(|error| error.to_string())
|
.map_err(|error| error.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tauri::command]
|
||||||
|
fn list_monitor_geometries(app: AppHandle) -> Result<Vec<MonitorInfo>, 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::ForegroundWindowSummary, String>
|
||||||
|
{
|
||||||
|
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::ProcessSummary, String> {
|
||||||
|
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::NetworkCheckResult, String> {
|
||||||
|
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]
|
#[tauri::command]
|
||||||
fn close_pet_window(app: AppHandle) -> Result<(), String> {
|
fn close_pet_window(app: AppHandle) -> Result<(), String> {
|
||||||
if let Some(window) = app.get_webview_window(PET_WINDOW_ID) {
|
if let Some(window) = app.get_webview_window(PET_WINDOW_ID) {
|
||||||
|
|
@ -336,6 +525,12 @@ pub fn run() {
|
||||||
app.manage(ApplicationDatabase::open(
|
app.manage(ApplicationDatabase::open(
|
||||||
app_data_dir.join("astrbot-desktop.sqlite3"),
|
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(RuntimeGuards::new());
|
||||||
app.manage(DualSlotRuntime::new());
|
app.manage(DualSlotRuntime::new());
|
||||||
|
|
||||||
|
|
@ -346,9 +541,50 @@ pub fn run() {
|
||||||
auto_connect_configured_slots(&database, &runtime).await;
|
auto_connect_configured_slots(&database, &runtime).await;
|
||||||
});
|
});
|
||||||
|
|
||||||
let show = MenuItem::with_id(app, "show", "Show AstrBot Desktop", true, None::<&str>)?;
|
let event_handle = app.handle().clone();
|
||||||
let quit = MenuItem::with_id(app, "quit", "Quit", true, None::<&str>)?;
|
tauri::async_runtime::spawn(async move {
|
||||||
let menu = Menu::with_items(app, &[&show, &quit])?;
|
loop {
|
||||||
|
let runtime = event_handle.state::<DualSlotRuntime>();
|
||||||
|
if let Some(event) = runtime.try_recv_event().await {
|
||||||
|
let database = event_handle.state::<ApplicationDatabase>();
|
||||||
|
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::<DualSlotRuntime>().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")
|
TrayIconBuilder::with_id("main")
|
||||||
.menu(&menu)
|
.menu(&menu)
|
||||||
|
|
@ -359,6 +595,9 @@ pub fn run() {
|
||||||
let _ = window.set_focus();
|
let _ = window.set_focus();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
"quick" => {
|
||||||
|
let _ = open_quick_chat_window(app.clone(), None);
|
||||||
|
}
|
||||||
"quit" => exit_application(app),
|
"quit" => exit_application(app),
|
||||||
_ => {}
|
_ => {}
|
||||||
})
|
})
|
||||||
|
|
@ -396,6 +635,11 @@ pub fn run() {
|
||||||
open_pet_window,
|
open_pet_window,
|
||||||
close_pet_window,
|
close_pet_window,
|
||||||
show_pet_context_menu,
|
show_pet_context_menu,
|
||||||
|
list_monitor_geometries,
|
||||||
|
set_pet_window_preferences,
|
||||||
|
inspect_foreground_window,
|
||||||
|
inspect_process,
|
||||||
|
check_network_target,
|
||||||
connection_policy_status,
|
connection_policy_status,
|
||||||
profiles::list_connection_profiles,
|
profiles::list_connection_profiles,
|
||||||
profiles::create_connection_profile,
|
profiles::create_connection_profile,
|
||||||
|
|
|
||||||
|
|
@ -62,6 +62,7 @@ pub struct DrainConnectionRuntimeEventsRequest {
|
||||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||||
pub struct LoadChatMessagesRequest {
|
pub struct LoadChatMessagesRequest {
|
||||||
pub profile_id: String,
|
pub profile_id: String,
|
||||||
|
pub slot: SlotKind,
|
||||||
pub server_instance_id: String,
|
pub server_instance_id: String,
|
||||||
pub session_id: String,
|
pub session_id: String,
|
||||||
pub limit: Option<usize>,
|
pub limit: Option<usize>,
|
||||||
|
|
@ -71,6 +72,7 @@ pub struct LoadChatMessagesRequest {
|
||||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||||
pub struct ListChatSessionsRequest {
|
pub struct ListChatSessionsRequest {
|
||||||
pub profile_id: String,
|
pub profile_id: String,
|
||||||
|
pub slot: SlotKind,
|
||||||
pub server_instance_id: String,
|
pub server_instance_id: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -78,10 +80,18 @@ pub struct ListChatSessionsRequest {
|
||||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||||
pub struct MarkChatSessionReadRequest {
|
pub struct MarkChatSessionReadRequest {
|
||||||
pub profile_id: String,
|
pub profile_id: String,
|
||||||
|
pub slot: SlotKind,
|
||||||
pub server_instance_id: String,
|
pub server_instance_id: String,
|
||||||
pub session_id: String,
|
pub session_id: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn slot_name(slot: SlotKind) -> &'static str {
|
||||||
|
match slot {
|
||||||
|
SlotKind::Companion => "companion",
|
||||||
|
SlotKind::Operations => "operations",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub async fn connection_runtime_status(
|
pub async fn connection_runtime_status(
|
||||||
runtime: State<'_, DualSlotRuntime>,
|
runtime: State<'_, DualSlotRuntime>,
|
||||||
|
|
@ -230,7 +240,11 @@ pub async fn list_chat_sessions(
|
||||||
request: ListChatSessionsRequest,
|
request: ListChatSessionsRequest,
|
||||||
) -> Result<Vec<crate::database::StoredChatSession>, RuntimeCommandError> {
|
) -> Result<Vec<crate::database::StoredChatSession>, RuntimeCommandError> {
|
||||||
database
|
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 {
|
.map_err(|_| RuntimeCommandError {
|
||||||
code: "databaseUnavailable",
|
code: "databaseUnavailable",
|
||||||
message: "Chat sessions are unavailable.",
|
message: "Chat sessions are unavailable.",
|
||||||
|
|
@ -245,6 +259,7 @@ pub async fn mark_chat_session_read(
|
||||||
database
|
database
|
||||||
.mark_chat_session_read(
|
.mark_chat_session_read(
|
||||||
&request.profile_id,
|
&request.profile_id,
|
||||||
|
slot_name(request.slot),
|
||||||
&request.server_instance_id,
|
&request.server_instance_id,
|
||||||
&request.session_id,
|
&request.session_id,
|
||||||
)
|
)
|
||||||
|
|
@ -262,6 +277,7 @@ pub async fn load_chat_messages(
|
||||||
database
|
database
|
||||||
.list_chat_messages(
|
.list_chat_messages(
|
||||||
&request.profile_id,
|
&request.profile_id,
|
||||||
|
slot_name(request.slot),
|
||||||
&request.server_instance_id,
|
&request.server_instance_id,
|
||||||
&request.session_id,
|
&request.session_id,
|
||||||
request.limit.unwrap_or(100),
|
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 {
|
let Some(session_id) = event.session_id.as_deref() else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
@ -553,40 +572,60 @@ pub async fn send_connection_slot_message(
|
||||||
runtime: State<'_, DualSlotRuntime>,
|
runtime: State<'_, DualSlotRuntime>,
|
||||||
request: SendConnectionSlotMessageRequest,
|
request: SendConnectionSlotMessageRequest,
|
||||||
) -> Result<SlotRuntimeSnapshot, RuntimeCommandError> {
|
) -> Result<SlotRuntimeSnapshot, RuntimeCommandError> {
|
||||||
|
let message_for_history = request.message.clone();
|
||||||
let status = runtime.status().await;
|
let status = runtime.status().await;
|
||||||
let snapshot = match request.slot {
|
let initial_session = match request.slot {
|
||||||
SlotKind::Companion => status.companion,
|
SlotKind::Companion => status.companion,
|
||||||
SlotKind::Operations => status.operations,
|
SlotKind::Operations => status.operations,
|
||||||
};
|
};
|
||||||
if let (Some(profile_id), Some(server_instance_id), Some(session_id)) = (
|
if let (Some(profile_id), Some(server_instance_id), Some(session_id)) = (
|
||||||
snapshot.profile_id.as_deref(),
|
initial_session.profile_id.as_deref(),
|
||||||
snapshot.server_instance_id.as_deref(),
|
initial_session.server_instance_id.as_deref(),
|
||||||
snapshot.active_session_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(
|
let _ = database.upsert_chat_message(
|
||||||
"local-default",
|
"local-default",
|
||||||
profile_id,
|
profile_id,
|
||||||
slot,
|
slot_name(request.slot),
|
||||||
server_instance_id,
|
server_instance_id,
|
||||||
None,
|
None,
|
||||||
session_id,
|
session_id,
|
||||||
&format!("local-{}", uuid::Uuid::new_v4()),
|
&format!("local-{}", uuid::Uuid::new_v4()),
|
||||||
"user",
|
"user",
|
||||||
&request.message,
|
&message_for_history,
|
||||||
false,
|
false,
|
||||||
false,
|
false,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
runtime
|
let result = runtime
|
||||||
.send_slot_message(
|
.send_slot_message(
|
||||||
request.slot,
|
request.slot,
|
||||||
request.message,
|
request.message,
|
||||||
request.config_id,
|
request.config_id,
|
||||||
request.attachments,
|
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
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -83,6 +83,7 @@ impl EventDeduplicator {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
pub struct GatewayClient {
|
pub struct GatewayClient {
|
||||||
config: GatewayClientConfig,
|
config: GatewayClientConfig,
|
||||||
http: reqwest::Client,
|
http: reqwest::Client,
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ use desktop_protocol::{
|
||||||
};
|
};
|
||||||
use secrecy::SecretString;
|
use secrecy::SecretString;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use std::time::Duration;
|
use std::{sync::Arc, time::Duration};
|
||||||
use tokio::sync::{Mutex, mpsc};
|
use tokio::sync::{Mutex, mpsc};
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
|
|
@ -45,6 +45,7 @@ pub struct SlotActivationRequest {
|
||||||
pub expected_server_instance_id: Option<String>,
|
pub expected_server_instance_id: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
enum ActiveClient {
|
enum ActiveClient {
|
||||||
Desktop(GatewayClient),
|
Desktop(GatewayClient),
|
||||||
AstrBot(AstrBotClient),
|
AstrBot(AstrBotClient),
|
||||||
|
|
@ -53,11 +54,17 @@ enum ActiveClient {
|
||||||
struct SlotRuntime {
|
struct SlotRuntime {
|
||||||
snapshot: SlotRuntimeSnapshot,
|
snapshot: SlotRuntimeSnapshot,
|
||||||
client: Option<ActiveClient>,
|
client: Option<ActiveClient>,
|
||||||
cancellation: Option<tokio_util::sync::CancellationToken>,
|
connection_cancellation: Option<Arc<tokio_util::sync::CancellationToken>>,
|
||||||
|
message_cancellation: Option<Arc<tokio_util::sync::CancellationToken>>,
|
||||||
|
generation: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SlotRuntime {
|
impl SlotRuntime {
|
||||||
fn empty(slot: SlotKind) -> Self {
|
fn empty(slot: SlotKind) -> Self {
|
||||||
|
Self::empty_with_generation(slot, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn empty_with_generation(slot: SlotKind, generation: u64) -> Self {
|
||||||
Self {
|
Self {
|
||||||
snapshot: SlotRuntimeSnapshot {
|
snapshot: SlotRuntimeSnapshot {
|
||||||
slot,
|
slot,
|
||||||
|
|
@ -69,7 +76,9 @@ impl SlotRuntime {
|
||||||
last_error: None,
|
last_error: None,
|
||||||
},
|
},
|
||||||
client: 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.
|
/// Network activation is explicit and Rust-owned. React never receives secrets.
|
||||||
pub struct DualSlotRuntime {
|
pub struct DualSlotRuntime {
|
||||||
inner: Mutex<DualSlotRuntimeInner>,
|
inner: Arc<Mutex<DualSlotRuntimeInner>>,
|
||||||
event_tx: mpsc::Sender<BoundGatewayEvent>,
|
event_tx: mpsc::Sender<BoundGatewayEvent>,
|
||||||
event_rx: Mutex<mpsc::Receiver<BoundGatewayEvent>>,
|
event_rx: Mutex<mpsc::Receiver<BoundGatewayEvent>>,
|
||||||
}
|
}
|
||||||
|
|
@ -123,15 +132,14 @@ impl DualSlotRuntime {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
let (event_tx, event_rx) = mpsc::channel(256);
|
let (event_tx, event_rx) = mpsc::channel(256);
|
||||||
Self {
|
Self {
|
||||||
inner: Mutex::new(DualSlotRuntimeInner {
|
inner: Arc::new(Mutex::new(DualSlotRuntimeInner {
|
||||||
companion: SlotRuntime::empty(SlotKind::Companion),
|
companion: SlotRuntime::empty(SlotKind::Companion),
|
||||||
operations: SlotRuntime::empty(SlotKind::Operations),
|
operations: SlotRuntime::empty(SlotKind::Operations),
|
||||||
}),
|
})),
|
||||||
event_tx,
|
event_tx,
|
||||||
event_rx: Mutex::new(event_rx),
|
event_rx: Mutex::new(event_rx),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn status(&self) -> ConnectionRuntimeStatus {
|
pub async fn status(&self) -> ConnectionRuntimeStatus {
|
||||||
let guard = self.inner.lock().await;
|
let guard = self.inner.lock().await;
|
||||||
ConnectionRuntimeStatus {
|
ConnectionRuntimeStatus {
|
||||||
|
|
@ -157,11 +165,17 @@ impl DualSlotRuntime {
|
||||||
}
|
}
|
||||||
events
|
events
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn deactivate_slot(&self, slot: SlotKind) -> Result<(), RuntimeCommandError> {
|
pub async fn deactivate_slot(&self, slot: SlotKind) -> Result<(), RuntimeCommandError> {
|
||||||
let mut guard = self.inner.lock().await;
|
let mut guard = self.inner.lock().await;
|
||||||
let runtime = slot_mut(&mut guard, slot);
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -182,6 +196,13 @@ impl DualSlotRuntime {
|
||||||
|
|
||||||
let mut guard = self.inner.lock().await;
|
let mut guard = self.inner.lock().await;
|
||||||
let runtime = slot_mut(&mut guard, request.slot);
|
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 {
|
*runtime = SlotRuntime {
|
||||||
snapshot: SlotRuntimeSnapshot {
|
snapshot: SlotRuntimeSnapshot {
|
||||||
slot: request.slot,
|
slot: request.slot,
|
||||||
|
|
@ -193,7 +214,9 @@ impl DualSlotRuntime {
|
||||||
last_error: None,
|
last_error: None,
|
||||||
},
|
},
|
||||||
client: None,
|
client: None,
|
||||||
cancellation: None,
|
connection_cancellation: None,
|
||||||
|
message_cancellation: None,
|
||||||
|
generation,
|
||||||
};
|
};
|
||||||
|
|
||||||
match request.transport {
|
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<tokio_util::sync::CancellationToken>,
|
||||||
|
) {
|
||||||
|
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> {
|
pub async fn cancel_slot_message(&self, slot: SlotKind) -> Result<(), RuntimeCommandError> {
|
||||||
let guard = self.inner.lock().await;
|
let guard = self.inner.lock().await;
|
||||||
let runtime = slot_ref(&guard, slot);
|
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(
|
return Err(RuntimeCommandError::unavailable(
|
||||||
"No chat request is active.",
|
"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.
|
// 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 mut guard = self.inner.lock().await;
|
||||||
let runtime = slot_mut(&mut guard, slot);
|
let runtime = slot_mut(&mut guard, slot);
|
||||||
let client = runtime.client.take().ok_or_else(|| {
|
let client = runtime.client.take().ok_or_else(|| {
|
||||||
RuntimeCommandError::unavailable("The selected slot is not connected.")
|
RuntimeCommandError::unavailable("The selected slot is not connected.")
|
||||||
})?;
|
})?;
|
||||||
let cancellation = tokio_util::sync::CancellationToken::new();
|
let cancellation = Arc::new(tokio_util::sync::CancellationToken::new());
|
||||||
runtime.cancellation = Some(cancellation.clone());
|
runtime.message_cancellation = Some(cancellation.clone());
|
||||||
runtime.snapshot.state = ConnectionState::Syncing;
|
runtime.snapshot.state = ConnectionState::Syncing;
|
||||||
(
|
(
|
||||||
client,
|
client,
|
||||||
|
|
@ -454,6 +569,7 @@ impl DualSlotRuntime {
|
||||||
runtime.snapshot.server_instance_id.clone(),
|
runtime.snapshot.server_instance_id.clone(),
|
||||||
runtime.snapshot.active_session_id.clone(),
|
runtime.snapshot.active_session_id.clone(),
|
||||||
cancellation,
|
cancellation,
|
||||||
|
runtime.generation,
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -522,6 +638,13 @@ impl DualSlotRuntime {
|
||||||
|
|
||||||
let mut guard = self.inner.lock().await;
|
let mut guard = self.inner.lock().await;
|
||||||
let runtime = slot_mut(&mut guard, slot);
|
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 {
|
match send_result {
|
||||||
Ok((client, session)) => {
|
Ok((client, session)) => {
|
||||||
runtime.client = Some(client);
|
runtime.client = Some(client);
|
||||||
|
|
@ -541,10 +664,16 @@ impl DualSlotRuntime {
|
||||||
runtime.snapshot.active_session_id = session_id;
|
runtime.snapshot.active_session_id = session_id;
|
||||||
runtime.snapshot.state = if error.code == "authExpired" {
|
runtime.snapshot.state = if error.code == "authExpired" {
|
||||||
ConnectionState::AuthExpired
|
ConnectionState::AuthExpired
|
||||||
|
} else if error.code == "cancelled" {
|
||||||
|
ConnectionState::Connected
|
||||||
} else {
|
} else {
|
||||||
ConnectionState::ServerUnavailable
|
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)
|
Err(error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ use std::{
|
||||||
time::{Duration, Instant},
|
time::{Duration, Instant},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use serde::Serialize;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use super::process::{ProcessInspectError, process_creation_time};
|
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_WINDOW_TEXT_UNITS: usize = 1024;
|
||||||
const MAX_SELECTION_TTL: Duration = Duration::from_secs(5 * 60);
|
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 struct ForegroundWindowSummary {
|
||||||
pub selection_token: Uuid,
|
pub selection_token: Uuid,
|
||||||
pub title: String,
|
pub title: String,
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,9 @@
|
||||||
use std::{fmt, io};
|
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 struct ProcessSummary {
|
||||||
pub process_id: u32,
|
pub process_id: u32,
|
||||||
pub executable_name: String,
|
pub executable_name: String,
|
||||||
|
|
|
||||||
|
|
@ -1,58 +1,58 @@
|
||||||
{
|
{
|
||||||
"$schema": "https://schema.tauri.app/config/2",
|
"$schema": "https://schema.tauri.app/config/2",
|
||||||
"productName": "AstrBot Desktop",
|
"productName": "AstrBot Desktop",
|
||||||
"version": "0.1.0-alpha.1",
|
"version": "0.1.0-alpha.2",
|
||||||
"identifier": "com.yokiirobw.astrbot-desktop",
|
"identifier": "com.yokiirobw.astrbot-desktop",
|
||||||
"build": {
|
"build": {
|
||||||
"beforeDevCommand": "npm run dev",
|
"beforeDevCommand": "npm run dev",
|
||||||
"devUrl": "http://127.0.0.1:1420",
|
"devUrl": "http://127.0.0.1:1420",
|
||||||
"beforeBuildCommand": "npm run build",
|
"beforeBuildCommand": "npm run build",
|
||||||
"frontendDist": "../dist"
|
"frontendDist": "../dist"
|
||||||
},
|
},
|
||||||
"app": {
|
"app": {
|
||||||
"windows": [
|
"windows": [
|
||||||
{
|
{
|
||||||
"label": "main",
|
"label": "main",
|
||||||
"title": "AstrBot Desktop",
|
"title": "AstrBot Desktop",
|
||||||
"width": 1100,
|
"width": 1100,
|
||||||
"height": 760,
|
"height": 760,
|
||||||
"minWidth": 720,
|
"minWidth": 720,
|
||||||
"minHeight": 520,
|
"minHeight": 520,
|
||||||
"center": true,
|
"center": true,
|
||||||
"resizable": true
|
"resizable": true,
|
||||||
},
|
"decorations": false,
|
||||||
{
|
"shadow": false
|
||||||
"label": "desktop-pet",
|
},
|
||||||
"title": "AstrBot Pet",
|
{
|
||||||
"url": "index.html?view=pet",
|
"label": "desktop-pet",
|
||||||
"width": 180,
|
"title": "AstrBot Pet",
|
||||||
"height": 220,
|
"url": "index.html?view=pet",
|
||||||
"resizable": false,
|
"width": 180,
|
||||||
"decorations": false,
|
"height": 220,
|
||||||
"shadow": false,
|
"resizable": false,
|
||||||
"transparent": true,
|
"decorations": false,
|
||||||
"alwaysOnTop": true,
|
"shadow": false,
|
||||||
"skipTaskbar": true,
|
"transparent": true,
|
||||||
"visible": false,
|
"alwaysOnTop": true,
|
||||||
"x": 120,
|
"skipTaskbar": true,
|
||||||
"y": 120,
|
"visible": false,
|
||||||
"create": true
|
"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"
|
],
|
||||||
}
|
"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": [
|
"bundle": {
|
||||||
"nsis"
|
"active": true,
|
||||||
],
|
"targets": ["nsis"],
|
||||||
"icon": [
|
"icon": [
|
||||||
"icons/32x32.png",
|
"icons/32x32.png",
|
||||||
"icons/128x128.png",
|
"icons/128x128.png",
|
||||||
"icons/128x128@2x.png",
|
"icons/128x128@2x.png",
|
||||||
"icons/icon.ico"
|
"icons/icon.ico"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
781
src/App.tsx
781
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 { listen } from "@tauri-apps/api/event";
|
||||||
|
import { getCurrentWindow } from "@tauri-apps/api/window";
|
||||||
import { useCallback, useEffect, useRef, useState } from "react";
|
import { useCallback, useEffect, useRef, useState } from "react";
|
||||||
|
|
||||||
import { ChatPanel } from "./features/chat/ChatPanel";
|
import { ChatPanel } from "./features/chat/ChatPanel";
|
||||||
import { StatusBubble } from "./features/chat/StatusBubble";
|
import { StatusBubble } from "./features/chat/StatusBubble";
|
||||||
import { ProfileManager } from "./features/profiles/ProfileManager";
|
import { ProfileManager } from "./features/profiles/ProfileManager";
|
||||||
import { PetPackImporter } from "./features/pet/PetPackImporter";
|
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 aemeathSpritesheet from "./features/pet/assets/aemeath/aemeath-spritesheet.png?inline";
|
||||||
import { bootstrapStatusSchema, type BootstrapStatus } from "./types/bootstrap";
|
import { bootstrapStatusSchema, type BootstrapStatus } from "./types/bootstrap";
|
||||||
|
|
||||||
type PageKey = "chat" | "connections" | "desktop" | "pet" | "diagnostics";
|
type PageKey = "chat" | "connections" | "desktop" | "pet" | "diagnostics";
|
||||||
type ChatLayout = "single" | "focus-companion" | "focus-operations";
|
|
||||||
type ChatSlot = "companion" | "operations";
|
type ChatSlot = "companion" | "operations";
|
||||||
type PetDisplayMode = "app-ui" | "desktop-floating";
|
type PetDisplayMode = "app-ui" | "desktop-floating";
|
||||||
|
type ThemeMode = "dark" | "light";
|
||||||
|
|
||||||
const initialStatus: BootstrapStatus = {
|
const initialStatus: BootstrapStatus = {
|
||||||
appVersion: "unknown",
|
appVersion: "unknown",
|
||||||
|
|
@ -24,10 +28,16 @@ const initialStatus: BootstrapStatus = {
|
||||||
};
|
};
|
||||||
|
|
||||||
const AEMEATH_SPRITESHEET = aemeathSpritesheet;
|
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],
|
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],
|
resting: [48, 49, 50, 51, 52, 53],
|
||||||
speaking: [72, 73, 74, 75, 76, 77, 78, 79],
|
speaking: [72, 73, 74, 75, 76, 77, 78, 79],
|
||||||
error: [40, 41, 42, 43, 44, 45, 46, 47],
|
error: [40, 41, 42, 43, 44, 45, 46, 47],
|
||||||
|
|
@ -51,12 +61,17 @@ export function App() {
|
||||||
const view = params.get("view");
|
const view = params.get("view");
|
||||||
const isQuickView = view === "quick";
|
const isQuickView = view === "quick";
|
||||||
const isBubbleView = view === "bubble";
|
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 quickSlot = params.get("slot");
|
||||||
const [page, setPage] = useState<PageKey>("chat");
|
const [page, setPage] = useState<PageKey>("chat");
|
||||||
|
const [theme, setTheme] = useState<ThemeMode>(() =>
|
||||||
|
localStorage.getItem("astrbot.theme") === "light" ? "light" : "dark",
|
||||||
|
);
|
||||||
const [status, setStatus] = useState(initialStatus);
|
const [status, setStatus] = useState(initialStatus);
|
||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
const [chatLayout, setChatLayout] = useState<ChatLayout>("single");
|
|
||||||
const [quickTarget, setQuickTarget] = useState<ChatSlot>(() =>
|
const [quickTarget, setQuickTarget] = useState<ChatSlot>(() =>
|
||||||
localStorage.getItem("astrbot.quickTarget") === "operations"
|
localStorage.getItem("astrbot.quickTarget") === "operations"
|
||||||
? "operations"
|
? "operations"
|
||||||
|
|
@ -81,13 +96,21 @@ export function App() {
|
||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
document.documentElement.dataset.theme = theme;
|
||||||
|
localStorage.setItem("astrbot.theme", theme);
|
||||||
|
}, [theme]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
void refreshStatus();
|
void refreshStatus();
|
||||||
}, [refreshStatus]);
|
}, [refreshStatus]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (isBubbleView || isPetView || isQuickView) return;
|
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) => {
|
void invoke(command).catch((reason) => {
|
||||||
setError(reason instanceof Error ? reason.message : String(reason));
|
setError(reason instanceof Error ? reason.message : String(reason));
|
||||||
});
|
});
|
||||||
|
|
@ -113,93 +136,191 @@ export function App() {
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<main className="app-shell">
|
<main className="app-shell">
|
||||||
<aside className="app-sidebar">
|
<WindowChrome
|
||||||
<div className="brand-lockup">
|
theme={theme}
|
||||||
<div className="brand-mark">A</div>
|
onToggleTheme={() =>
|
||||||
<div>
|
setTheme((current) => (current === "dark" ? "light" : "dark"))
|
||||||
<strong>AstrBot Desktop</strong>
|
}
|
||||||
<span>Companion workspace</span>
|
/>
|
||||||
|
<div className="app-shell-body">
|
||||||
|
<aside className="app-sidebar">
|
||||||
|
<div className="brand-lockup">
|
||||||
|
<div className="brand-mark">A</div>
|
||||||
|
<div>
|
||||||
|
<strong>AstrBot Desktop</strong>
|
||||||
|
<span>Companion workspace</span>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
<nav className="primary-nav" aria-label="主导航">
|
||||||
<nav className="primary-nav" aria-label="主导航">
|
{navigation.map((item) => (
|
||||||
{navigation.map((item) => (
|
<button
|
||||||
<button
|
key={item.key}
|
||||||
key={item.key}
|
type="button"
|
||||||
type="button"
|
className={page === item.key ? "nav-item active" : "nav-item"}
|
||||||
className={page === item.key ? "nav-item active" : "nav-item"}
|
onClick={() => setPage(item.key)}
|
||||||
onClick={() => setPage(item.key)}
|
>
|
||||||
>
|
<span className="nav-item-icon">{navIcon(item.key)}</span>
|
||||||
<span className="nav-item-icon">{navIcon(item.key)}</span>
|
<span>
|
||||||
<span>
|
<strong>{item.label}</strong>
|
||||||
<strong>{item.label}</strong>
|
<small>{item.description}</small>
|
||||||
<small>{item.description}</small>
|
</span>
|
||||||
</span>
|
</button>
|
||||||
</button>
|
))}
|
||||||
))}
|
</nav>
|
||||||
</nav>
|
<div className="sidebar-footer">
|
||||||
<div className="sidebar-footer">
|
<span className="connection-dot" />
|
||||||
<span className="connection-dot" />
|
<span>本地客户端已启动</span>
|
||||||
<span>本地客户端已启动</span>
|
</div>
|
||||||
</div>
|
</aside>
|
||||||
</aside>
|
|
||||||
|
|
||||||
<section className="app-content">
|
<section
|
||||||
<PageHeader page={page} />
|
className={
|
||||||
{error ? <p className="error-banner">IPC 暂不可用:{error}</p> : null}
|
page === "chat" ? "app-content app-content-chat" : "app-content"
|
||||||
{page === "chat" ? (
|
}
|
||||||
<ChatPage
|
>
|
||||||
chatLayout={chatLayout}
|
{page === "chat" ? null : <PageHeader page={page} />}
|
||||||
quickTarget={quickTarget}
|
{error ? <p className="error-banner">IPC 暂不可用:{error}</p> : null}
|
||||||
onLayoutChange={setChatLayout}
|
{page === "chat" ? <ChatPage /> : null}
|
||||||
onOpenQuickChat={() =>
|
{page === "connections" ? <ProfileManager /> : null}
|
||||||
void invoke("open_quick_chat_window", { slot: quickTarget })
|
{page === "desktop" ? (
|
||||||
}
|
<DesktopBehaviorPage
|
||||||
/>
|
bubbleTarget={bubbleTarget}
|
||||||
) : null}
|
quickTarget={quickTarget}
|
||||||
{page === "connections" ? <ProfileManager /> : null}
|
onBubbleTargetChange={(value) => {
|
||||||
{page === "desktop" ? (
|
setBubbleTarget(value);
|
||||||
<DesktopBehaviorPage
|
localStorage.setItem("astrbot.bubbleTarget", value);
|
||||||
bubbleTarget={bubbleTarget}
|
}}
|
||||||
quickTarget={quickTarget}
|
onQuickTargetChange={(value) => {
|
||||||
onBubbleTargetChange={(value) => {
|
setQuickTarget(value);
|
||||||
setBubbleTarget(value);
|
localStorage.setItem("astrbot.quickTarget", value);
|
||||||
localStorage.setItem("astrbot.bubbleTarget", value);
|
}}
|
||||||
}}
|
onOpenQuickChat={() =>
|
||||||
onQuickTargetChange={(value) => {
|
void invoke("open_quick_chat_window", { slot: quickTarget })
|
||||||
setQuickTarget(value);
|
}
|
||||||
localStorage.setItem("astrbot.quickTarget", value);
|
/>
|
||||||
}}
|
) : null}
|
||||||
onOpenQuickChat={() =>
|
{page === "pet" ? (
|
||||||
void invoke("open_quick_chat_window", { slot: quickTarget })
|
<PetPage
|
||||||
}
|
displayMode={petDisplayMode}
|
||||||
/>
|
onDisplayModeChange={(value) => {
|
||||||
) : null}
|
setPetDisplayMode(value);
|
||||||
{page === "pet" ? (
|
localStorage.setItem("astrbot.petDisplayMode", value);
|
||||||
<PetPage
|
}}
|
||||||
displayMode={petDisplayMode}
|
/>
|
||||||
onDisplayModeChange={(value) => {
|
) : null}
|
||||||
setPetDisplayMode(value);
|
{page === "diagnostics" ? (
|
||||||
localStorage.setItem("astrbot.petDisplayMode", value);
|
<DiagnosticsPage
|
||||||
}}
|
status={status}
|
||||||
/>
|
onRefresh={() => void refreshStatus()}
|
||||||
) : null}
|
/>
|
||||||
{page === "diagnostics" ? (
|
) : null}
|
||||||
<DiagnosticsPage
|
</section>
|
||||||
status={status}
|
</div>
|
||||||
onRefresh={() => void refreshStatus()}
|
|
||||||
/>
|
|
||||||
) : null}
|
|
||||||
</section>
|
|
||||||
{petDisplayMode === "app-ui" ? <InAppPetOverlay /> : null}
|
{petDisplayMode === "app-ui" ? <InAppPetOverlay /> : null}
|
||||||
</main>
|
</main>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function WindowChrome({
|
||||||
|
theme,
|
||||||
|
onToggleTheme,
|
||||||
|
}: {
|
||||||
|
theme: ThemeMode;
|
||||||
|
onToggleTheme: () => void;
|
||||||
|
}) {
|
||||||
|
const [currentWindow, setCurrentWindow] = useState<ReturnType<
|
||||||
|
typeof getCurrentWindow
|
||||||
|
> | 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 (
|
||||||
|
<header
|
||||||
|
className="window-chrome"
|
||||||
|
data-tauri-drag-region
|
||||||
|
onDoubleClick={() => void toggleMaximize()}
|
||||||
|
onMouseDown={(event) => {
|
||||||
|
if (event.button === 0) void currentWindow?.startDragging();
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<span className="window-chrome-title">AstrBot Desktop</span>
|
||||||
|
<div className="window-chrome-actions" data-tauri-drag-region="false">
|
||||||
|
<button
|
||||||
|
aria-label={
|
||||||
|
theme === "dark" ? "Switch to light theme" : "Switch to dark theme"
|
||||||
|
}
|
||||||
|
className="window-theme-toggle"
|
||||||
|
onClick={onToggleTheme}
|
||||||
|
onMouseDown={(event) => event.stopPropagation()}
|
||||||
|
type="button"
|
||||||
|
>
|
||||||
|
{theme === "dark" ? "Light" : "Dark"}
|
||||||
|
</button>
|
||||||
|
<div className="window-controls" data-tauri-drag-region="false">
|
||||||
|
<button
|
||||||
|
aria-label="Minimize window"
|
||||||
|
className="window-control"
|
||||||
|
onClick={() => void currentWindow?.minimize()}
|
||||||
|
onMouseDown={(event) => event.stopPropagation()}
|
||||||
|
type="button"
|
||||||
|
>
|
||||||
|
<span
|
||||||
|
className="window-glyph window-glyph-minimize"
|
||||||
|
aria-hidden="true"
|
||||||
|
/>
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
aria-label={maximized ? "Restore window" : "Maximize window"}
|
||||||
|
className="window-control"
|
||||||
|
onClick={() => void toggleMaximize()}
|
||||||
|
onMouseDown={(event) => event.stopPropagation()}
|
||||||
|
type="button"
|
||||||
|
>
|
||||||
|
<span
|
||||||
|
className={`window-glyph window-glyph-maximize${maximized ? " is-restored" : ""}`}
|
||||||
|
aria-hidden="true"
|
||||||
|
/>
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
aria-label="Close window"
|
||||||
|
className="window-control window-control-close"
|
||||||
|
onClick={() => void currentWindow?.close()}
|
||||||
|
onMouseDown={(event) => event.stopPropagation()}
|
||||||
|
type="button"
|
||||||
|
>
|
||||||
|
<span
|
||||||
|
className="window-glyph window-glyph-close"
|
||||||
|
aria-hidden="true"
|
||||||
|
/>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</header>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
function InAppPetOverlay() {
|
function InAppPetOverlay() {
|
||||||
const [visible, setVisible] = useState(true);
|
const [visible, setVisible] = useState(true);
|
||||||
const [position, setPosition] = useState(() => {
|
const [position, setPosition] = useState(() => {
|
||||||
try {
|
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"
|
return saved && typeof saved.x === "number" && typeof saved.y === "number"
|
||||||
? saved
|
? saved
|
||||||
: { x: 24, y: 96 };
|
: { x: 24, y: 96 };
|
||||||
|
|
@ -207,7 +328,11 @@ function InAppPetOverlay() {
|
||||||
return { x: 24, y: 96 };
|
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 savePosition = (x: number, y: number) => {
|
||||||
const next = {
|
const next = {
|
||||||
|
|
@ -220,9 +345,11 @@ function InAppPetOverlay() {
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
let unlisten: (() => void) | undefined;
|
let unlisten: (() => void) | undefined;
|
||||||
void listen("pet-hide-requested", () => setVisible(false)).then((cleanup) => {
|
void listen("pet-hide-requested", () => setVisible(false)).then(
|
||||||
unlisten = cleanup;
|
(cleanup) => {
|
||||||
});
|
unlisten = cleanup;
|
||||||
|
},
|
||||||
|
);
|
||||||
return () => unlisten?.();
|
return () => unlisten?.();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
|
@ -251,7 +378,8 @@ function InAppPetOverlay() {
|
||||||
);
|
);
|
||||||
}}
|
}}
|
||||||
onPointerUp={(event) => {
|
onPointerUp={(event) => {
|
||||||
if (dragRef.current?.pointerId === event.pointerId) dragRef.current = null;
|
if (dragRef.current?.pointerId === event.pointerId)
|
||||||
|
dragRef.current = null;
|
||||||
}}
|
}}
|
||||||
onPointerCancel={() => {
|
onPointerCancel={() => {
|
||||||
dragRef.current = null;
|
dragRef.current = null;
|
||||||
|
|
@ -291,93 +419,14 @@ function PageHeader({ page }: { page: PageKey }) {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function ChatPage({
|
function ChatPage() {
|
||||||
chatLayout,
|
|
||||||
quickTarget,
|
|
||||||
onLayoutChange,
|
|
||||||
onOpenQuickChat,
|
|
||||||
}: {
|
|
||||||
chatLayout: ChatLayout;
|
|
||||||
quickTarget: ChatSlot;
|
|
||||||
onLayoutChange: (layout: ChatLayout) => void;
|
|
||||||
onOpenQuickChat: () => void;
|
|
||||||
}) {
|
|
||||||
return (
|
return (
|
||||||
<div className="page-stack">
|
<div className="chat-page">
|
||||||
<section className="welcome-card">
|
<ChatPanel />
|
||||||
<div>
|
|
||||||
<span className="section-kicker">Conversation hub</span>
|
|
||||||
<h2>今天想和哪个实例聊天?</h2>
|
|
||||||
<p>陪伴实例适合日常交流,运维实例用于经过确认的本机诊断提案。</p>
|
|
||||||
</div>
|
|
||||||
<button type="button" onClick={onOpenQuickChat}>
|
|
||||||
打开快速聊天
|
|
||||||
</button>
|
|
||||||
</section>
|
|
||||||
<section className="chat-workspace" aria-label="聊天工作区">
|
|
||||||
<div className="workspace-toolbar">
|
|
||||||
<div>
|
|
||||||
<span className="section-kicker">Workspace view</span>
|
|
||||||
<strong>对话布局</strong>
|
|
||||||
</div>
|
|
||||||
<div className="segmented-control">
|
|
||||||
<LayoutButton
|
|
||||||
active={chatLayout === "single"}
|
|
||||||
onClick={() => onLayoutChange("single")}
|
|
||||||
>
|
|
||||||
单面板
|
|
||||||
</LayoutButton>
|
|
||||||
<LayoutButton
|
|
||||||
active={chatLayout === "focus-companion"}
|
|
||||||
onClick={() => onLayoutChange("focus-companion")}
|
|
||||||
>
|
|
||||||
聚焦陪伴
|
|
||||||
</LayoutButton>
|
|
||||||
<LayoutButton
|
|
||||||
active={chatLayout === "focus-operations"}
|
|
||||||
onClick={() => onLayoutChange("focus-operations")}
|
|
||||||
>
|
|
||||||
聚焦运维
|
|
||||||
</LayoutButton>
|
|
||||||
</div>
|
|
||||||
<span className="toolbar-note">
|
|
||||||
快速聊天目标:{slotLabel(quickTarget)}
|
|
||||||
</span>
|
|
||||||
</div>
|
|
||||||
<ChatPanel
|
|
||||||
fixedSlot={
|
|
||||||
chatLayout === "focus-companion"
|
|
||||||
? "companion"
|
|
||||||
: chatLayout === "focus-operations"
|
|
||||||
? "operations"
|
|
||||||
: undefined
|
|
||||||
}
|
|
||||||
/>
|
|
||||||
</section>
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function LayoutButton({
|
|
||||||
active,
|
|
||||||
children,
|
|
||||||
onClick,
|
|
||||||
}: {
|
|
||||||
active: boolean;
|
|
||||||
children: React.ReactNode;
|
|
||||||
onClick: () => void;
|
|
||||||
}) {
|
|
||||||
return (
|
|
||||||
<button
|
|
||||||
type="button"
|
|
||||||
className={active ? "segment active" : "segment"}
|
|
||||||
onClick={onClick}
|
|
||||||
>
|
|
||||||
{children}
|
|
||||||
</button>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function DesktopBehaviorPage({
|
function DesktopBehaviorPage({
|
||||||
bubbleTarget,
|
bubbleTarget,
|
||||||
quickTarget,
|
quickTarget,
|
||||||
|
|
@ -391,6 +440,9 @@ function DesktopBehaviorPage({
|
||||||
onQuickTargetChange: (value: ChatSlot) => void;
|
onQuickTargetChange: (value: ChatSlot) => void;
|
||||||
onOpenQuickChat: () => void;
|
onOpenQuickChat: () => void;
|
||||||
}) {
|
}) {
|
||||||
|
const [quietHours, setQuietHours] = useState(
|
||||||
|
() => localStorage.getItem("astrbot.quietHours") ?? "off",
|
||||||
|
);
|
||||||
return (
|
return (
|
||||||
<div className="page-stack">
|
<div className="page-stack">
|
||||||
<section className="settings-grid">
|
<section className="settings-grid">
|
||||||
|
|
@ -450,6 +502,24 @@ function DesktopBehaviorPage({
|
||||||
本机能力层,不会与聊天状态混在一起。
|
本机能力层,不会与聊天状态混在一起。
|
||||||
</p>
|
</p>
|
||||||
</section>
|
</section>
|
||||||
|
<section className="settings-grid">
|
||||||
|
<SettingCard
|
||||||
|
title="主动消息静默时段"
|
||||||
|
description="静默时段只抑制气泡提醒,不会删除消息,也不会影响聊天未读数。"
|
||||||
|
>
|
||||||
|
<select
|
||||||
|
value={quietHours}
|
||||||
|
onChange={(event) => {
|
||||||
|
const next = event.target.value;
|
||||||
|
setQuietHours(next);
|
||||||
|
localStorage.setItem("astrbot.quietHours", next);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<option value="off">关闭</option>
|
||||||
|
<option value="night">每天 22:00–07:00</option>
|
||||||
|
</select>
|
||||||
|
</SettingCard>
|
||||||
|
</section>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
@ -461,6 +531,45 @@ function PetPage({
|
||||||
displayMode: PetDisplayMode;
|
displayMode: PetDisplayMode;
|
||||||
onDisplayModeChange: (value: PetDisplayMode) => void;
|
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<unknown>("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 (
|
return (
|
||||||
<div className="page-stack">
|
<div className="page-stack">
|
||||||
<PetPackImporter />
|
<PetPackImporter />
|
||||||
|
|
@ -480,10 +589,14 @@ function PetPage({
|
||||||
>
|
>
|
||||||
<select
|
<select
|
||||||
value={displayMode}
|
value={displayMode}
|
||||||
onChange={(event) => onDisplayModeChange(event.target.value as PetDisplayMode)}
|
onChange={(event) =>
|
||||||
|
onDisplayModeChange(event.target.value as PetDisplayMode)
|
||||||
|
}
|
||||||
>
|
>
|
||||||
<option value="app-ui">仅应用 UI 内</option>
|
<option value="app-ui">仅应用 UI 内</option>
|
||||||
<option value="desktop-floating">桌面浮动</option>
|
<option value="desktop-floating">
|
||||||
|
桌面浮动
|
||||||
|
</option>
|
||||||
</select>
|
</select>
|
||||||
</SettingCard>
|
</SettingCard>
|
||||||
<SettingCard
|
<SettingCard
|
||||||
|
|
@ -508,13 +621,80 @@ function PetPage({
|
||||||
<span className="form-note">应用内活动桌宠已显示,可直接拖动</span>
|
<span className="form-note">应用内活动桌宠已显示,可直接拖动</span>
|
||||||
)}
|
)}
|
||||||
</SettingCard>
|
</SettingCard>
|
||||||
|
<SettingCard
|
||||||
|
title="显示器与位置"
|
||||||
|
description="桌面版只会把桌宠放在屏幕安全边缘,不会覆盖游戏中心区域。"
|
||||||
|
>
|
||||||
|
<select
|
||||||
|
value={monitorIndex}
|
||||||
|
onChange={(event) => {
|
||||||
|
const next = Number(event.target.value);
|
||||||
|
setMonitorIndex(next);
|
||||||
|
applyPetPreferences({ monitorIndex: next });
|
||||||
|
}}
|
||||||
|
disabled={monitors.length === 0}
|
||||||
|
>
|
||||||
|
{monitors.map((monitor, index) => (
|
||||||
|
<option
|
||||||
|
value={index}
|
||||||
|
key={`${monitor.name ?? "monitor"}-${index}`}
|
||||||
|
>
|
||||||
|
{monitor.name ?? `显示器 ${index + 1}`}{" "}
|
||||||
|
{monitor.isPrimary ? "(主屏)" : ""}
|
||||||
|
</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
<select
|
||||||
|
value={positionMode}
|
||||||
|
onChange={(event) => {
|
||||||
|
const next = event.target.value;
|
||||||
|
setPositionMode(next);
|
||||||
|
applyPetPreferences({ positionMode: next });
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<option value="free">自由位置</option>
|
||||||
|
<option value="edge">安全边缘</option>
|
||||||
|
<option value="monitor">指定显示器</option>
|
||||||
|
</select>
|
||||||
|
</SettingCard>
|
||||||
|
<SettingCard
|
||||||
|
title="桌面行为"
|
||||||
|
description="置顶和游戏模式都可以随时关闭;权限或显示器不可用时自动回退。"
|
||||||
|
>
|
||||||
|
<label className="toggle-row">
|
||||||
|
<input
|
||||||
|
type="checkbox"
|
||||||
|
checked={alwaysOnTop}
|
||||||
|
onChange={(event) => {
|
||||||
|
const next = event.target.checked;
|
||||||
|
setAlwaysOnTop(next);
|
||||||
|
applyPetPreferences({ alwaysOnTop: next });
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
始终置顶
|
||||||
|
</label>
|
||||||
|
<label className="toggle-row">
|
||||||
|
<input
|
||||||
|
type="checkbox"
|
||||||
|
checked={gameMode}
|
||||||
|
onChange={(event) => {
|
||||||
|
const next = event.target.checked;
|
||||||
|
setGameMode(next);
|
||||||
|
applyPetPreferences({ gameMode: next });
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
游戏模式(安全边缘)
|
||||||
|
</label>
|
||||||
|
</SettingCard>
|
||||||
</section>
|
</section>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function DesktopPetView({ embedded = false }: { embedded?: boolean }) {
|
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 [frameIndex, setFrameIndex] = useState(0);
|
||||||
const [spriteReady, setSpriteReady] = useState(false);
|
const [spriteReady, setSpriteReady] = useState(false);
|
||||||
const [spriteError, setSpriteError] = useState<string | null>(null);
|
const [spriteError, setSpriteError] = useState<string | null>(null);
|
||||||
|
|
@ -528,12 +708,44 @@ function DesktopPetView({ embedded = false }: { embedded?: boolean }) {
|
||||||
return () => document.documentElement.classList.remove("pet-view");
|
return () => document.documentElement.classList.remove("pet-view");
|
||||||
}, [embedded]);
|
}, [embedded]);
|
||||||
|
|
||||||
|
const completionTimerRef = useRef<number | null>(null);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
let unlisten: (() => void) | undefined;
|
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;
|
unlisten = cleanup;
|
||||||
});
|
});
|
||||||
return () => unlisten?.();
|
void listen<unknown>("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(() => {
|
useEffect(() => {
|
||||||
|
|
@ -554,50 +766,68 @@ function DesktopPetView({ embedded = false }: { embedded?: boolean }) {
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
let cancelled = false;
|
let cancelled = false;
|
||||||
const refreshPetState = async () => {
|
void invoke<unknown>("connection_runtime_status")
|
||||||
try {
|
.then((value) => {
|
||||||
const result = connectionRuntimeStatusSchema.parse(
|
|
||||||
await invoke<unknown>("connection_runtime_status"),
|
|
||||||
);
|
|
||||||
if (cancelled) return;
|
if (cancelled) return;
|
||||||
const states = [result.companion.state, result.operations.state];
|
try {
|
||||||
if (states.includes("authExpired") || states.includes("fatalError")) {
|
const result = connectionRuntimeStatusSchema.parse(value);
|
||||||
setState("error");
|
const state = result.companion.state;
|
||||||
} else if (states.includes("syncing")) {
|
if (
|
||||||
setState("working");
|
state === "authExpired" ||
|
||||||
} else if (states.includes("reconnecting")) {
|
state === "fatalError" ||
|
||||||
setState("speaking");
|
state === "incompatible"
|
||||||
} else if (states.every((value) => value === "disconnected" || value === "offline")) {
|
) {
|
||||||
setState("resting");
|
setState("error");
|
||||||
} else {
|
} 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");
|
setState("idle");
|
||||||
}
|
}
|
||||||
} catch {
|
})
|
||||||
|
.catch(() => {
|
||||||
if (!cancelled) setState("idle");
|
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 () => {
|
return () => {
|
||||||
cancelled = true;
|
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(() => {
|
useEffect(() => {
|
||||||
setFrameIndex(0);
|
setFrameIndex(0);
|
||||||
const frames = AEMEATH_FRAMES[state];
|
const frames = AEMEATH_FRAMES[state];
|
||||||
const timer = window.setInterval(() => {
|
const timer = window.setInterval(
|
||||||
setFrameIndex((current) => (current + 1) % frames.length);
|
() => {
|
||||||
}, state === "resting" ? 180 : 125);
|
setFrameIndex((current) => (current + 1) % frames.length);
|
||||||
|
},
|
||||||
|
state === "resting" ? 180 : 125,
|
||||||
|
);
|
||||||
return () => window.clearInterval(timer);
|
return () => window.clearInterval(timer);
|
||||||
}, [state]);
|
}, [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 frameRow = Math.floor(frame / 8);
|
||||||
const frameColumn = frame % 8;
|
const frameColumn = frame % 8;
|
||||||
|
|
||||||
|
|
@ -607,13 +837,23 @@ function DesktopPetView({ embedded = false }: { embedded?: boolean }) {
|
||||||
if (!canvas || !image || !spriteReady) return;
|
if (!canvas || !image || !spriteReady) return;
|
||||||
const context = canvas.getContext("2d");
|
const context = canvas.getContext("2d");
|
||||||
if (!context) {
|
if (!context) {
|
||||||
setSpriteError("?? Canvas ????");
|
setSpriteError("???? Canvas ?????");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
context.clearRect(0, 0, 192, 208);
|
context.clearRect(0, 0, 192, 208);
|
||||||
context.imageSmoothingEnabled = false;
|
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) {
|
} catch (reason) {
|
||||||
setSpriteError(reason instanceof Error ? reason.message : "??????");
|
setSpriteError(reason instanceof Error ? reason.message : "??????");
|
||||||
}
|
}
|
||||||
|
|
@ -621,8 +861,13 @@ function DesktopPetView({ embedded = false }: { embedded?: boolean }) {
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<main
|
<main
|
||||||
className={`pet-window pet-window-${state}${gameMode ? " pet-window-game" : ""}`}
|
className={`pet-window pet-window-${state}${gameMode ? " pet-window-game" : ""}${embedded ? " pet-window-embedded" : ""}`}
|
||||||
aria-label="AstrBot ??"
|
aria-label="AstrBot ??"
|
||||||
|
data-tauri-drag-region={!embedded ? "true" : undefined}
|
||||||
|
onMouseDown={(event) => {
|
||||||
|
if (embedded || event.button !== 0) return;
|
||||||
|
void getCurrentWindow().startDragging();
|
||||||
|
}}
|
||||||
onContextMenu={(event) => {
|
onContextMenu={(event) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
void invoke("show_pet_context_menu");
|
void invoke("show_pet_context_menu");
|
||||||
|
|
@ -633,18 +878,6 @@ function DesktopPetView({ embedded = false }: { embedded?: boolean }) {
|
||||||
role="img"
|
role="img"
|
||||||
aria-label={`桌宠状态:${state}`}
|
aria-label={`桌宠状态:${state}`}
|
||||||
>
|
>
|
||||||
<img
|
|
||||||
className="aemeath-pet-sprite-sheet"
|
|
||||||
src={AEMEATH_SPRITESHEET}
|
|
||||||
alt=""
|
|
||||||
aria-hidden="true"
|
|
||||||
onLoad={() => setSpriteReady(true)}
|
|
||||||
onError={() => setSpriteError("????????")}
|
|
||||||
style={{
|
|
||||||
left: -(frameColumn * 192),
|
|
||||||
top: -(frameRow * 208),
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
<canvas
|
<canvas
|
||||||
ref={spriteCanvasRef}
|
ref={spriteCanvasRef}
|
||||||
className="aemeath-pet-sprite"
|
className="aemeath-pet-sprite"
|
||||||
|
|
@ -654,13 +887,110 @@ function DesktopPetView({ embedded = false }: { embedded?: boolean }) {
|
||||||
aria-label={`pet-${state}`}
|
aria-label={`pet-${state}`}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
{spriteError ? <span className="pet-render-error">{spriteError}</span> : null}
|
{spriteError ? (
|
||||||
{!spriteReady && !spriteError ? <span className="pet-render-loading">Loading pet...</span> : null}
|
<span className="pet-render-error">{spriteError}</span>
|
||||||
<span className="pet-window-status">{state === "working" ? "正在写写画画" : state === "resting" ? "休息中" : state === "speaking" ? "正在回应" : state === "error" ? "需要重新登录或检查连接" : "陪伴中"}</span>
|
) : null}
|
||||||
|
{!spriteReady && !spriteError ? (
|
||||||
|
<span className="pet-render-loading">Loading pet...</span>
|
||||||
|
) : null}
|
||||||
|
<span className="pet-window-status">
|
||||||
|
{state === "working"
|
||||||
|
? "正在写写画画"
|
||||||
|
: state === "resting"
|
||||||
|
? "休息中"
|
||||||
|
: state === "speaking"
|
||||||
|
? "正在回应"
|
||||||
|
: state === "error"
|
||||||
|
? "需要重新登录或检查连接"
|
||||||
|
: "陪伴中"}
|
||||||
|
</span>
|
||||||
</main>
|
</main>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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<string, unknown>) => {
|
||||||
|
try {
|
||||||
|
const value = await invoke<unknown>(
|
||||||
|
command,
|
||||||
|
request ? { request } : undefined,
|
||||||
|
);
|
||||||
|
setResult(JSON.stringify(value, null, 2));
|
||||||
|
} catch (reason) {
|
||||||
|
setResult(reason instanceof Error ? reason.message : String(reason));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<section className="settings-grid">
|
||||||
|
<SettingCard
|
||||||
|
title="前台窗口"
|
||||||
|
description="读取当前前台窗口的标题、类名和进程身份。"
|
||||||
|
>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => void run("inspect_foreground_window")}
|
||||||
|
>
|
||||||
|
检查前台窗口
|
||||||
|
</button>
|
||||||
|
</SettingCard>
|
||||||
|
<SettingCard
|
||||||
|
title="网络连通性"
|
||||||
|
description="只检查一个明确的主机和端口,不执行扫描。"
|
||||||
|
>
|
||||||
|
<input
|
||||||
|
value={host}
|
||||||
|
onChange={(event) => setHost(event.target.value)}
|
||||||
|
aria-label="网络主机"
|
||||||
|
/>
|
||||||
|
<input
|
||||||
|
value={port}
|
||||||
|
onChange={(event) => setPort(event.target.value)}
|
||||||
|
aria-label="网络端口"
|
||||||
|
inputMode="numeric"
|
||||||
|
/>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() =>
|
||||||
|
void run("check_network_target", {
|
||||||
|
host,
|
||||||
|
port: Number(port),
|
||||||
|
timeoutSeconds: 3,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
>
|
||||||
|
检查端口
|
||||||
|
</button>
|
||||||
|
</SettingCard>
|
||||||
|
<SettingCard
|
||||||
|
title="指定进程"
|
||||||
|
description="输入进程 ID 后读取有限的进程摘要,不枚举全部进程。"
|
||||||
|
>
|
||||||
|
<input
|
||||||
|
value={processId}
|
||||||
|
onChange={(event) => setProcessId(event.target.value)}
|
||||||
|
aria-label="进程 ID"
|
||||||
|
inputMode="numeric"
|
||||||
|
/>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() =>
|
||||||
|
void run("inspect_process", { processId: Number(processId) })
|
||||||
|
}
|
||||||
|
>
|
||||||
|
检查进程
|
||||||
|
</button>
|
||||||
|
</SettingCard>
|
||||||
|
</section>
|
||||||
|
{result ? <pre className="diagnostic-result">{result}</pre> : null}
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
function DiagnosticsPage({
|
function DiagnosticsPage({
|
||||||
status,
|
status,
|
||||||
onRefresh,
|
onRefresh,
|
||||||
|
|
@ -680,6 +1010,7 @@ function DiagnosticsPage({
|
||||||
value={String(status.databaseSchemaVersion)}
|
value={String(status.databaseSchemaVersion)}
|
||||||
/>
|
/>
|
||||||
</section>
|
</section>
|
||||||
|
<DiagnosticsTools />
|
||||||
<section className="boundaries">
|
<section className="boundaries">
|
||||||
<span className="section-kicker">Security boundary</span>
|
<span className="section-kicker">Security boundary</span>
|
||||||
<h2>本机能力保持可审计</h2>
|
<h2>本机能力保持可审计</h2>
|
||||||
|
|
@ -718,10 +1049,6 @@ function SettingCard({
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function slotLabel(slot: ChatSlot) {
|
|
||||||
return slot === "companion" ? "陪伴实例" : "运维实例";
|
|
||||||
}
|
|
||||||
|
|
||||||
interface StatusCardProps {
|
interface StatusCardProps {
|
||||||
label: string;
|
label: string;
|
||||||
value: string;
|
value: string;
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
import { invoke } from "@tauri-apps/api/core";
|
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 {
|
import {
|
||||||
astrbotAttachmentSchema,
|
astrbotAttachmentSchema,
|
||||||
|
|
@ -9,7 +10,6 @@ import {
|
||||||
connectionRuntimeStatusSchema,
|
connectionRuntimeStatusSchema,
|
||||||
type AstrBotAttachment,
|
type AstrBotAttachment,
|
||||||
type AstrBotCapabilitySnapshot,
|
type AstrBotCapabilitySnapshot,
|
||||||
type AstrBotModelOption,
|
|
||||||
type BoundRuntimeEvent,
|
type BoundRuntimeEvent,
|
||||||
remoteChatSessionSchema,
|
remoteChatSessionSchema,
|
||||||
type ChatMessage,
|
type ChatMessage,
|
||||||
|
|
@ -17,6 +17,7 @@ import {
|
||||||
type RemoteChatSession,
|
type RemoteChatSession,
|
||||||
type ConnectionRuntimeStatus,
|
type ConnectionRuntimeStatus,
|
||||||
storedChatMessageSchema,
|
storedChatMessageSchema,
|
||||||
|
slotRuntimeSnapshotSchema,
|
||||||
} from "./types";
|
} from "./types";
|
||||||
|
|
||||||
const emptyStatus: ConnectionRuntimeStatus = {
|
const emptyStatus: ConnectionRuntimeStatus = {
|
||||||
|
|
@ -37,6 +38,16 @@ export interface ChatPanelProps {
|
||||||
fixedSlot?: ChatSlot;
|
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) {
|
export function ChatPanel({ fixedSlot }: ChatPanelProps) {
|
||||||
const [status, setStatus] = useState(emptyStatus);
|
const [status, setStatus] = useState(emptyStatus);
|
||||||
const [messages, setMessages] = useState<ChatMessage[]>([]);
|
const [messages, setMessages] = useState<ChatMessage[]>([]);
|
||||||
|
|
@ -54,11 +65,15 @@ export function ChatPanel({ fixedSlot }: ChatPanelProps) {
|
||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
const [capabilities, setCapabilities] =
|
const [capabilities, setCapabilities] =
|
||||||
useState<AstrBotCapabilitySnapshot | null>(null);
|
useState<AstrBotCapabilitySnapshot | null>(null);
|
||||||
const [selectedModel, setSelectedModel] = useState("");
|
|
||||||
const [pendingAttachments, setPendingAttachments] = useState<
|
const [pendingAttachments, setPendingAttachments] = useState<
|
||||||
AstrBotAttachment[]
|
AstrBotAttachment[]
|
||||||
>([]);
|
>([]);
|
||||||
const [uploading, setUploading] = useState(false);
|
const [uploading, setUploading] = useState(false);
|
||||||
|
const [voiceListening, setVoiceListening] = useState(false);
|
||||||
|
const speechRecognition = useRef<{
|
||||||
|
start: () => void;
|
||||||
|
stop: () => void;
|
||||||
|
} | null>(null);
|
||||||
const [unreadCounts, setUnreadCounts] = useState<Record<ChatSlot, number>>({
|
const [unreadCounts, setUnreadCounts] = useState<Record<ChatSlot, number>>({
|
||||||
companion: 0,
|
companion: 0,
|
||||||
operations: 0,
|
operations: 0,
|
||||||
|
|
@ -73,30 +88,42 @@ export function ChatPanel({ fixedSlot }: ChatPanelProps) {
|
||||||
),
|
),
|
||||||
[messages, selectedSlot, status],
|
[messages, selectedSlot, status],
|
||||||
);
|
);
|
||||||
|
const visibleMessages = useMemo(
|
||||||
|
() =>
|
||||||
|
activeMessages.filter(
|
||||||
|
(message) => message.content.trim().length > 0 || message.streaming || message.error,
|
||||||
|
),
|
||||||
|
[activeMessages],
|
||||||
|
);
|
||||||
|
|
||||||
const refresh = useCallback(async () => {
|
const refresh = useCallback(async () => {
|
||||||
try {
|
try {
|
||||||
const result = await invoke<unknown>("connection_runtime_status");
|
const result = await invoke<unknown>("connection_runtime_status");
|
||||||
setStatus(connectionRuntimeStatusSchema.parse(result));
|
const parsed = connectionRuntimeStatusSchema.safeParse(result);
|
||||||
|
if (!parsed.success) {
|
||||||
|
setError("连接状态暂不可用,请检查连接配置。");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
setStatus(parsed.data);
|
||||||
setError(null);
|
setError(null);
|
||||||
return true;
|
return true;
|
||||||
} catch (reason) {
|
} catch {
|
||||||
setError(reason instanceof Error ? reason.message : String(reason));
|
setError("连接状态暂不可用,请检查本地运行时。");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const drainEvents = useCallback(async () => {
|
useEffect(() => {
|
||||||
try {
|
let unlisten: (() => void) | undefined;
|
||||||
const result = await invoke<unknown>("drain_connection_runtime_events", {
|
void listen<unknown>("connection-runtime-event", (event) => {
|
||||||
request: { limit: 64 },
|
const parsed = boundRuntimeEventSchema.safeParse(event.payload);
|
||||||
});
|
if (parsed.success) {
|
||||||
const events = boundRuntimeEventSchema.array().parse(result);
|
setMessages((current) => applyRuntimeEvents(current, [parsed.data]));
|
||||||
if (events.length > 0)
|
}
|
||||||
setMessages((current) => applyRuntimeEvents(current, events));
|
}).then((cleanup) => {
|
||||||
} catch {
|
unlisten = cleanup;
|
||||||
// Keep the browser preview usable without Rust IPC.
|
});
|
||||||
}
|
return () => unlisten?.();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|
@ -111,6 +138,7 @@ export function ChatPanel({ fixedSlot }: ChatPanelProps) {
|
||||||
void invoke<unknown>("list_chat_sessions", {
|
void invoke<unknown>("list_chat_sessions", {
|
||||||
request: {
|
request: {
|
||||||
profileId: snapshot.profileId,
|
profileId: snapshot.profileId,
|
||||||
|
slot: selectedSlot,
|
||||||
serverInstanceId: snapshot.serverInstanceId,
|
serverInstanceId: snapshot.serverInstanceId,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
@ -174,8 +202,7 @@ export function ChatPanel({ fixedSlot }: ChatPanelProps) {
|
||||||
transport: selectedTransport,
|
transport: selectedTransport,
|
||||||
};
|
};
|
||||||
setCapabilities(null);
|
setCapabilities(null);
|
||||||
setSelectedModel("");
|
if (!snapshot.profileId || snapshot.transport !== "astr-bot-open-api") return;
|
||||||
if (!snapshot.profileId || snapshot.transport !== "astrbot-openapi") return;
|
|
||||||
let cancelled = false;
|
let cancelled = false;
|
||||||
void invoke<unknown>("connection_slot_capabilities", {
|
void invoke<unknown>("connection_slot_capabilities", {
|
||||||
request: { slot: selectedSlot },
|
request: { slot: selectedSlot },
|
||||||
|
|
@ -184,11 +211,6 @@ export function ChatPanel({ fixedSlot }: ChatPanelProps) {
|
||||||
if (cancelled) return;
|
if (cancelled) return;
|
||||||
const next = astrbotCapabilitySnapshotSchema.parse(result);
|
const next = astrbotCapabilitySnapshotSchema.parse(result);
|
||||||
setCapabilities(next);
|
setCapabilities(next);
|
||||||
setSelectedModel(
|
|
||||||
next.models.find((model) => model.isDefault)?.id ??
|
|
||||||
next.models[0]?.id ??
|
|
||||||
"",
|
|
||||||
);
|
|
||||||
})
|
})
|
||||||
.catch(() => undefined);
|
.catch(() => undefined);
|
||||||
return () => {
|
return () => {
|
||||||
|
|
@ -218,6 +240,7 @@ export function ChatPanel({ fixedSlot }: ChatPanelProps) {
|
||||||
void invoke("mark_chat_session_read", {
|
void invoke("mark_chat_session_read", {
|
||||||
request: {
|
request: {
|
||||||
profileId: snapshot.profileId,
|
profileId: snapshot.profileId,
|
||||||
|
slot: selectedSlot,
|
||||||
serverInstanceId: snapshot.serverInstanceId,
|
serverInstanceId: snapshot.serverInstanceId,
|
||||||
sessionId: snapshot.activeSessionId,
|
sessionId: snapshot.activeSessionId,
|
||||||
},
|
},
|
||||||
|
|
@ -248,6 +271,7 @@ export function ChatPanel({ fixedSlot }: ChatPanelProps) {
|
||||||
void invoke<unknown>("load_chat_messages", {
|
void invoke<unknown>("load_chat_messages", {
|
||||||
request: {
|
request: {
|
||||||
profileId: snapshot.profileId,
|
profileId: snapshot.profileId,
|
||||||
|
slot: selectedSlot,
|
||||||
serverInstanceId: snapshot.serverInstanceId,
|
serverInstanceId: snapshot.serverInstanceId,
|
||||||
sessionId: snapshot.activeSessionId,
|
sessionId: snapshot.activeSessionId,
|
||||||
limit: 100,
|
limit: 100,
|
||||||
|
|
@ -256,9 +280,20 @@ export function ChatPanel({ fixedSlot }: ChatPanelProps) {
|
||||||
.then((result) => {
|
.then((result) => {
|
||||||
if (cancelled) return;
|
if (cancelled) return;
|
||||||
const records = storedChatMessageSchema.array().parse(result);
|
const records = storedChatMessageSchema.array().parse(result);
|
||||||
setMessages((current) =>
|
setMessages((current) => {
|
||||||
current
|
const localUserMessages = current.filter(
|
||||||
.filter((item) => item.slot !== selectedSlot)
|
(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(
|
.concat(
|
||||||
records.map((record) => ({
|
records.map((record) => ({
|
||||||
id: record.messageId,
|
id: record.messageId,
|
||||||
|
|
@ -269,8 +304,8 @@ export function ChatPanel({ fixedSlot }: ChatPanelProps) {
|
||||||
streaming: record.streaming,
|
streaming: record.streaming,
|
||||||
error: record.error,
|
error: record.error,
|
||||||
})),
|
})),
|
||||||
),
|
);
|
||||||
);
|
});
|
||||||
})
|
})
|
||||||
.catch(() => undefined);
|
.catch(() => undefined);
|
||||||
return () => {
|
return () => {
|
||||||
|
|
@ -290,13 +325,12 @@ export function ChatPanel({ fixedSlot }: ChatPanelProps) {
|
||||||
if (!available) return;
|
if (!available) return;
|
||||||
timer = window.setInterval(() => {
|
timer = window.setInterval(() => {
|
||||||
void refresh();
|
void refresh();
|
||||||
void drainEvents();
|
|
||||||
}, 500);
|
}, 500);
|
||||||
});
|
});
|
||||||
return () => {
|
return () => {
|
||||||
if (timer !== undefined) window.clearInterval(timer);
|
if (timer !== undefined) window.clearInterval(timer);
|
||||||
};
|
};
|
||||||
}, [drainEvents, refresh]);
|
}, [refresh]);
|
||||||
|
|
||||||
const uploadFiles = async (files: FileList | null) => {
|
const uploadFiles = async (files: FileList | null) => {
|
||||||
if (!files?.length) return;
|
if (!files?.length) return;
|
||||||
|
|
@ -345,14 +379,23 @@ export function ChatPanel({ fixedSlot }: ChatPanelProps) {
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
try {
|
try {
|
||||||
await invoke("send_connection_slot_message", {
|
const result = slotRuntimeSnapshotSchema.parse(
|
||||||
request: {
|
await invoke<unknown>("send_connection_slot_message", {
|
||||||
slot: selectedSlot,
|
request: {
|
||||||
message,
|
slot: selectedSlot,
|
||||||
configId: selectedModel || undefined,
|
message,
|
||||||
attachments,
|
attachments,
|
||||||
},
|
},
|
||||||
});
|
}),
|
||||||
|
);
|
||||||
|
setStatus((current) => ({ ...current, [selectedSlot]: result }));
|
||||||
|
setMessages((current) =>
|
||||||
|
current.map((item) =>
|
||||||
|
item.id === id
|
||||||
|
? { ...item, sessionId: result.activeSessionId }
|
||||||
|
: item,
|
||||||
|
),
|
||||||
|
);
|
||||||
setError(null);
|
setError(null);
|
||||||
} catch (reason) {
|
} catch (reason) {
|
||||||
setError(reason instanceof Error ? reason.message : String(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<ArrayLike<{ transcript?: string }>>;
|
||||||
|
};
|
||||||
|
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 (
|
return (
|
||||||
<section className="chat-panel" aria-label="聊天面板">
|
<section className="chat-panel" aria-label="聊天面板">
|
||||||
<div className="chat-heading">
|
<div className="chat-heading">
|
||||||
|
|
@ -403,7 +492,9 @@ export function ChatPanel({ fixedSlot }: ChatPanelProps) {
|
||||||
{status[selectedSlot].state === "authExpired" ? (
|
{status[selectedSlot].state === "authExpired" ? (
|
||||||
<div className="warning-banner" role="alert">
|
<div className="warning-banner" role="alert">
|
||||||
<strong>认证已过期</strong>
|
<strong>认证已过期</strong>
|
||||||
<span>请到“连接”设置重新登录 AstrBot,当前槽位不会继续发送消息。</span>
|
<span>
|
||||||
|
请到“连接”设置重新登录 AstrBot,当前槽位不会继续发送消息。
|
||||||
|
</span>
|
||||||
<button type="button" onClick={() => void invoke("open_main_window")}>
|
<button type="button" onClick={() => void invoke("open_main_window")}>
|
||||||
打开连接设置
|
打开连接设置
|
||||||
</button>
|
</button>
|
||||||
|
|
@ -439,25 +530,35 @@ export function ChatPanel({ fixedSlot }: ChatPanelProps) {
|
||||||
</div>
|
</div>
|
||||||
) : null}
|
) : null}
|
||||||
<div className="chat-message-list" aria-live="polite">
|
<div className="chat-message-list" aria-live="polite">
|
||||||
{activeMessages.length === 0 ? (
|
{visibleMessages.length === 0 ? (
|
||||||
<p className="chat-empty">
|
<p className="chat-empty">
|
||||||
当前槽位还没有消息。连接 Profile 后即可开始聊天。
|
当前槽位还没有消息。连接 Profile 后即可开始聊天。
|
||||||
</p>
|
</p>
|
||||||
) : (
|
) : (
|
||||||
activeMessages.map((message) => (
|
visibleMessages.map((message) => (
|
||||||
<article
|
<article
|
||||||
className={`chat-message ${message.role}${message.error ? " failed" : ""}`}
|
className={`chat-message-row ${message.role}${message.error ? " failed" : ""}`}
|
||||||
key={message.id}
|
key={message.id}
|
||||||
>
|
>
|
||||||
<span className="chat-message-role">
|
<div className="chat-avatar" aria-hidden="true">
|
||||||
{message.role === "user"
|
{message.role === "user" ? "我" : message.role === "assistant" ? "A" : "!"}
|
||||||
? "我"
|
</div>
|
||||||
: message.role === "assistant"
|
<div className="chat-bubble">
|
||||||
? "AstrBot"
|
<div className="chat-bubble-header">
|
||||||
: "系统"}
|
<span>{message.role === "user" ? "我" : message.role === "assistant" ? "AstrBot" : "系统"}</span>
|
||||||
</span>
|
{message.streaming ? <small>生成中…</small> : null}
|
||||||
<p>{message.content || "(空消息)"}</p>
|
</div>
|
||||||
{message.streaming ? <small>生成中…</small> : null}
|
<p>{message.content || "生成中…"}</p>
|
||||||
|
{message.role === "assistant" && message.content ? (
|
||||||
|
<button
|
||||||
|
className="chat-message-read"
|
||||||
|
type="button"
|
||||||
|
onClick={() => speakMessage(message.content)}
|
||||||
|
>
|
||||||
|
朗读
|
||||||
|
</button>
|
||||||
|
) : null}
|
||||||
|
</div>
|
||||||
</article>
|
</article>
|
||||||
))
|
))
|
||||||
)}
|
)}
|
||||||
|
|
@ -476,23 +577,15 @@ export function ChatPanel({ fixedSlot }: ChatPanelProps) {
|
||||||
disabled={uploading || !capabilities?.fileUpload}
|
disabled={uploading || !capabilities?.fileUpload}
|
||||||
/>
|
/>
|
||||||
</label>
|
</label>
|
||||||
<select
|
|
||||||
aria-label="模型配置"
|
|
||||||
value={selectedModel}
|
|
||||||
onChange={(event) => setSelectedModel(event.target.value)}
|
|
||||||
disabled={currentModels.length === 0}
|
|
||||||
>
|
|
||||||
{currentModels.length === 0 ? (
|
|
||||||
<option value="">使用服务端默认模型</option>
|
|
||||||
) : (
|
|
||||||
currentModels.map((model) => (
|
|
||||||
<option key={model.id} value={model.id}>
|
|
||||||
{model.name || model.id}
|
|
||||||
</option>
|
|
||||||
))
|
|
||||||
)}
|
|
||||||
</select>
|
|
||||||
{uploading ? <span className="toolbar-note">上传中…</span> : null}
|
{uploading ? <span className="toolbar-note">上传中…</span> : null}
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={toggleVoiceInput}
|
||||||
|
className={voiceListening ? "voice-button active" : "voice-button"}
|
||||||
|
>
|
||||||
|
{voiceListening ? "停止录音" : "语音输入"}
|
||||||
|
</button>
|
||||||
</div>
|
</div>
|
||||||
{pendingAttachments.length > 0 ? (
|
{pendingAttachments.length > 0 ? (
|
||||||
<div className="attachment-list">
|
<div className="attachment-list">
|
||||||
|
|
@ -559,7 +652,10 @@ function applyRuntimeEvents(
|
||||||
const delta =
|
const delta =
|
||||||
payloadString(event, "delta") || payloadString(event, "content") || "";
|
payloadString(event, "delta") || payloadString(event, "content") || "";
|
||||||
const existing = next.find(
|
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) {
|
if (existing) {
|
||||||
existing.content += delta;
|
existing.content += delta;
|
||||||
|
|
@ -577,16 +673,28 @@ function applyRuntimeEvents(
|
||||||
const id =
|
const id =
|
||||||
event.messageId || payloadString(event, "messageId") || event.eventId;
|
event.messageId || payloadString(event, "messageId") || event.eventId;
|
||||||
const existing = next.find(
|
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;
|
const completedContent = payloadString(event, "content")?.trim() ?? "";
|
||||||
else
|
if (existing) {
|
||||||
|
if (completedContent) {
|
||||||
|
existing.content = completedContent;
|
||||||
|
} else if (!existing.content.trim()) {
|
||||||
|
existing.content = "未收到有效回复";
|
||||||
|
existing.error = true;
|
||||||
|
}
|
||||||
|
existing.streaming = false;
|
||||||
|
} else
|
||||||
next.push({
|
next.push({
|
||||||
id,
|
id,
|
||||||
sessionId: event.sessionId,
|
sessionId: event.sessionId,
|
||||||
slot: event.slot,
|
slot: event.slot,
|
||||||
role: "assistant",
|
role: "assistant",
|
||||||
content: payloadString(event, "content") || "",
|
content: completedContent || "未收到有效回复",
|
||||||
|
error: !completedContent,
|
||||||
});
|
});
|
||||||
} else if (event.eventType === "error") {
|
} else if (event.eventType === "error") {
|
||||||
next.push({
|
next.push({
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
|
chatSessionSchema,
|
||||||
connectionRuntimeStatusSchema,
|
connectionRuntimeStatusSchema,
|
||||||
type ConnectionRuntimeStatus,
|
type ConnectionRuntimeStatus,
|
||||||
} from "./types";
|
} from "./types";
|
||||||
|
|
@ -13,11 +14,41 @@ const initialStatus: ConnectionRuntimeStatus = {
|
||||||
|
|
||||||
export function StatusBubble() {
|
export function StatusBubble() {
|
||||||
const [status, setStatus] = useState(initialStatus);
|
const [status, setStatus] = useState(initialStatus);
|
||||||
|
const [unread, setUnread] = useState(0);
|
||||||
|
const [quiet, setQuiet] = useState(false);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const refresh = () =>
|
const refresh = () => {
|
||||||
invoke<unknown>("connection_runtime_status")
|
const hour = new Date().getHours();
|
||||||
.then((value) => setStatus(connectionRuntimeStatusSchema.parse(value)))
|
setQuiet(
|
||||||
|
localStorage.getItem("astrbot.quietHours") === "night" &&
|
||||||
|
(hour >= 22 || hour < 7),
|
||||||
|
);
|
||||||
|
void invoke<unknown>("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<unknown>("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);
|
.catch(() => undefined);
|
||||||
|
};
|
||||||
refresh();
|
refresh();
|
||||||
const timer = window.setInterval(refresh, 1000);
|
const timer = window.setInterval(refresh, 1000);
|
||||||
return () => window.clearInterval(timer);
|
return () => window.clearInterval(timer);
|
||||||
|
|
@ -25,7 +56,11 @@ export function StatusBubble() {
|
||||||
const companion = status.companion.state;
|
const companion = status.companion.state;
|
||||||
const operations = status.operations.state;
|
const operations = status.operations.state;
|
||||||
const active = companion === "connected" || operations === "connected";
|
const active = companion === "connected" || operations === "connected";
|
||||||
const visibleState = active ? "在线" : stateLabel(companion, operations);
|
const visibleState = quiet
|
||||||
|
? "静默中"
|
||||||
|
: active
|
||||||
|
? "在线"
|
||||||
|
: stateLabel(companion, operations);
|
||||||
return (
|
return (
|
||||||
<main className="status-bubble-shell" aria-label="连接状态气泡">
|
<main className="status-bubble-shell" aria-label="连接状态气泡">
|
||||||
<button
|
<button
|
||||||
|
|
@ -49,6 +84,9 @@ export function StatusBubble() {
|
||||||
>
|
>
|
||||||
<span className="status-bubble-orb" />
|
<span className="status-bubble-orb" />
|
||||||
<span className="status-bubble-label">{visibleState}</span>
|
<span className="status-bubble-label">{visibleState}</span>
|
||||||
|
{unread > 0 && !quiet ? (
|
||||||
|
<span className="status-bubble-unread">{unread}</span>
|
||||||
|
) : null}
|
||||||
</button>
|
</button>
|
||||||
</main>
|
</main>
|
||||||
);
|
);
|
||||||
|
|
@ -58,12 +96,11 @@ function stateLabel(
|
||||||
companion: ConnectionRuntimeStatus["companion"]["state"],
|
companion: ConnectionRuntimeStatus["companion"]["state"],
|
||||||
operations: ConnectionRuntimeStatus["operations"]["state"],
|
operations: ConnectionRuntimeStatus["operations"]["state"],
|
||||||
): string {
|
): string {
|
||||||
if (companion === "connecting" || operations === "connecting") return "连接中";
|
if (companion === "connecting" || operations === "connecting")
|
||||||
if (companion === "authExpired" || operations === "authExpired") return "需要认证";
|
return "连接中";
|
||||||
if (
|
if (companion === "authExpired" || operations === "authExpired")
|
||||||
companion === "serverUnavailable" ||
|
return "需要认证";
|
||||||
operations === "serverUnavailable"
|
if (companion === "serverUnavailable" || operations === "serverUnavailable")
|
||||||
)
|
|
||||||
return "服务不可用";
|
return "服务不可用";
|
||||||
return "离线";
|
return "离线";
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,12 +9,12 @@ import {
|
||||||
export const slotRuntimeSnapshotSchema = z
|
export const slotRuntimeSnapshotSchema = z
|
||||||
.object({
|
.object({
|
||||||
slot: activeSlotKindSchema,
|
slot: activeSlotKindSchema,
|
||||||
profileId: z.uuid().optional(),
|
profileId: z.uuid().nullish(),
|
||||||
transport: z.enum(["desktop-v1", "astrbot-openapi"]).optional(),
|
transport: z.enum(["desktop-v1", "astr-bot-open-api"]).nullish(),
|
||||||
state: connectionStateSchema,
|
state: connectionStateSchema,
|
||||||
serverInstanceId: z.string().min(1).optional(),
|
serverInstanceId: z.string().min(1).nullish(),
|
||||||
activeSessionId: z.string().min(1).optional(),
|
activeSessionId: z.string().min(1).nullish(),
|
||||||
lastError: z.string().optional(),
|
lastError: z.string().nullish(),
|
||||||
})
|
})
|
||||||
.strict();
|
.strict();
|
||||||
|
|
||||||
|
|
@ -70,7 +70,7 @@ export type ChatSlot = ActiveSlotKind;
|
||||||
|
|
||||||
export interface ChatMessage {
|
export interface ChatMessage {
|
||||||
id: string;
|
id: string;
|
||||||
sessionId?: string;
|
sessionId?: string | null;
|
||||||
slot: ChatSlot;
|
slot: ChatSlot;
|
||||||
role: "user" | "assistant" | "system";
|
role: "user" | "assistant" | "system";
|
||||||
content: string;
|
content: string;
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ export const petDisplayModeSchema = z.enum(["app-ui", "desktop-floating"]);
|
||||||
export const petPositionModeSchema = z.enum([
|
export const petPositionModeSchema = z.enum([
|
||||||
"free",
|
"free",
|
||||||
"edge",
|
"edge",
|
||||||
"指定屏幕",
|
"monitor",
|
||||||
"follow-main-window",
|
"follow-main-window",
|
||||||
]);
|
]);
|
||||||
export const bubbleClickTargetSchema = z.enum(["main", "quick"]);
|
export const bubbleClickTargetSchema = z.enum(["main", "quick"]);
|
||||||
|
|
@ -27,7 +27,10 @@ export const petMovementSchema = z.enum([
|
||||||
|
|
||||||
export const petAnimationSchema = z
|
export const petAnimationSchema = z
|
||||||
.object({
|
.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,
|
state: companionStateSchema,
|
||||||
frames: z.array(z.number().int().nonnegative()).min(1),
|
frames: z.array(z.number().int().nonnegative()).min(1),
|
||||||
fps: z.number().positive().max(60),
|
fps: z.number().positive().max(60),
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ import {
|
||||||
type TransportKind,
|
type TransportKind,
|
||||||
type UpdateConnectionProfileRequest,
|
type UpdateConnectionProfileRequest,
|
||||||
} from "../../types/profile";
|
} from "../../types/profile";
|
||||||
|
import { connectionRuntimeStatusSchema } from "../chat/types";
|
||||||
import { CredentialEnrollmentCard } from "./CredentialEnrollmentCard";
|
import { CredentialEnrollmentCard } from "./CredentialEnrollmentCard";
|
||||||
|
|
||||||
interface ProfileFormState {
|
interface ProfileFormState {
|
||||||
|
|
@ -68,26 +69,43 @@ export function ProfileManager() {
|
||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
const [notice, setNotice] = useState<string | null>(null);
|
const [notice, setNotice] = useState<string | null>(null);
|
||||||
|
|
||||||
const loadProfiles = useCallback(async () => {
|
const loadProfiles = useCallback(async () => {
|
||||||
try {
|
try {
|
||||||
const [profileResult, slotResult, policyResult] = await Promise.all([
|
const [profileResult, slotResult, policyResult, runtimeResult] =
|
||||||
invoke<unknown>("list_connection_profiles"),
|
await Promise.all([
|
||||||
invoke<unknown>("list_connection_slots"),
|
invoke<unknown>("list_connection_profiles"),
|
||||||
invoke<unknown>("connection_policy_status"),
|
invoke<unknown>("list_connection_slots"),
|
||||||
]);
|
invoke<unknown>("connection_policy_status"),
|
||||||
setProfiles(connectionProfileListSchema.parse(profileResult));
|
invoke<unknown>("connection_runtime_status"),
|
||||||
setSlots(connectionSlotListSchema.parse(slotResult));
|
]);
|
||||||
setPolicy(connectionPolicyStatusSchema.parse(policyResult));
|
setProfiles(connectionProfileListSchema.parse(profileResult));
|
||||||
setError(null);
|
const storedSlots = connectionSlotListSchema.parse(slotResult);
|
||||||
} catch (reason) {
|
const runtime = connectionRuntimeStatusSchema.parse(runtimeResult);
|
||||||
setError(`Profile storage unavailable: ${errorMessage(reason)}`);
|
setSlots(mergeRuntimeSlots(storedSlots, runtime));
|
||||||
}
|
setPolicy(connectionPolicyStatusSchema.parse(policyResult));
|
||||||
}, []);
|
setError(null);
|
||||||
|
} catch (reason) {
|
||||||
useEffect(() => {
|
setError(`Profile storage unavailable: ${errorMessage(reason)}`);
|
||||||
void loadProfiles();
|
}
|
||||||
}, [loadProfiles]);
|
}, []);
|
||||||
|
|
||||||
|
const refreshRuntimeStatus = useCallback(async () => {
|
||||||
|
try {
|
||||||
|
const runtime = connectionRuntimeStatusSchema.parse(
|
||||||
|
await invoke<unknown>("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<HTMLFormElement>) {
|
async function submitProfile(event: FormEvent<HTMLFormElement>) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
setBusy(true);
|
setBusy(true);
|
||||||
|
|
@ -442,10 +460,10 @@ export function ProfileManager() {
|
||||||
}
|
}
|
||||||
>
|
>
|
||||||
<option value="desktop-v1">Desktop Mock/Gateway</option>
|
<option value="desktop-v1">Desktop Mock/Gateway</option>
|
||||||
<option value="astrbot-openapi">AstrBot OpenAPI</option>
|
<option value="astr-bot-open-api">AstrBot OpenAPI</option>
|
||||||
</select>
|
</select>
|
||||||
</label>
|
</label>
|
||||||
{form.transportKind === "astrbot-openapi" ? (
|
{form.transportKind === "astr-bot-open-api" ? (
|
||||||
<>
|
<>
|
||||||
<label>
|
<label>
|
||||||
AstrBot 用户名
|
AstrBot 用户名
|
||||||
|
|
@ -629,6 +647,20 @@ function toProfileInput(form: ProfileFormState): ConnectionProfileInput {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function mergeRuntimeSlots(
|
||||||
|
slots: ConnectionSlot[],
|
||||||
|
runtime: ReturnType<typeof connectionRuntimeStatusSchema.parse>,
|
||||||
|
) {
|
||||||
|
return slots.map((slot) => {
|
||||||
|
const snapshot = runtime[slot.slot];
|
||||||
|
return {
|
||||||
|
...slot,
|
||||||
|
state: snapshot.state,
|
||||||
|
serverInstanceId: snapshot.serverInstanceId ?? slot.serverInstanceId,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function sortProfiles(profiles: ConnectionProfile[]) {
|
function sortProfiles(profiles: ConnectionProfile[]) {
|
||||||
return [...profiles].sort((left, right) =>
|
return [...profiles].sort((left, right) =>
|
||||||
left.displayName.localeCompare(right.displayName, "zh-CN"),
|
left.displayName.localeCompare(right.displayName, "zh-CN"),
|
||||||
|
|
|
||||||
1028
src/styles.css
1028
src/styles.css
文件差异内容过多而无法显示
加载差异
|
|
@ -1,4 +1,9 @@
|
||||||
import "@testing-library/jest-dom/vitest";
|
import "@testing-library/jest-dom/vitest";
|
||||||
|
import { vi } from "vitest";
|
||||||
|
|
||||||
|
vi.mock("@tauri-apps/api/event", () => ({
|
||||||
|
listen: vi.fn(async () => () => undefined),
|
||||||
|
}));
|
||||||
|
|
||||||
import { cleanup } from "@testing-library/react";
|
import { cleanup } from "@testing-library/react";
|
||||||
import { afterEach } from "vitest";
|
import { afterEach } from "vitest";
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ export const profileRoleSchema = z.enum(["companion", "operations", "general"]);
|
||||||
|
|
||||||
export const authenticationTypeSchema = z.enum(["device-token", "api-key", "jwt"]);
|
export const authenticationTypeSchema = z.enum(["device-token", "api-key", "jwt"]);
|
||||||
|
|
||||||
export const transportKindSchema = z.enum(["desktop-v1", "astrbot-openapi"]);
|
export const transportKindSchema = z.enum(["desktop-v1", "astr-bot-open-api"]);
|
||||||
|
|
||||||
export const activeSlotKindSchema = z.enum(["companion", "operations"]);
|
export const activeSlotKindSchema = z.enum(["companion", "operations"]);
|
||||||
|
|
||||||
|
|
|
||||||
正在加载…
添加表格
在新工单中引用