453 行
14 KiB
TypeScript
453 行
14 KiB
TypeScript
import { invoke } from "@tauri-apps/api/core";
|
||
import { useCallback, useEffect, useState } from "react";
|
||
|
||
import { ChatPanel } from "./features/chat/ChatPanel";
|
||
import { StatusBubble } from "./features/chat/StatusBubble";
|
||
import { ProfileManager } from "./features/profiles/ProfileManager";
|
||
import { PetPackImporter } from "./features/pet/PetPackImporter";
|
||
import { bootstrapStatusSchema, type BootstrapStatus } from "./types/bootstrap";
|
||
|
||
type PageKey = "chat" | "connections" | "desktop" | "pet" | "diagnostics";
|
||
type ChatLayout = "single" | "focus-companion" | "focus-operations";
|
||
type ChatSlot = "companion" | "operations";
|
||
|
||
const initialStatus: BootstrapStatus = {
|
||
appVersion: "unknown",
|
||
platform: "unknown",
|
||
rustCore: "pending",
|
||
protocolVersion: "desktop-v1alpha1",
|
||
databaseSchemaVersion: 4,
|
||
};
|
||
|
||
const navigation: Array<{ key: PageKey; label: string; description: string }> =
|
||
[
|
||
{ key: "chat", label: "聊天", description: "陪伴与运维对话" },
|
||
{ key: "connections", label: "连接", description: "实例与槽位管理" },
|
||
{ key: "desktop", label: "桌面行为", description: "快速聊天与状态气泡" },
|
||
{ key: "pet", label: "桌宠", description: "形象与活动范围" },
|
||
{
|
||
key: "diagnostics",
|
||
label: "诊断与设置",
|
||
description: "运行状态与安全边界",
|
||
},
|
||
];
|
||
|
||
export function App() {
|
||
const params = new URLSearchParams(window.location.search);
|
||
const view = params.get("view");
|
||
const isQuickView = view === "quick";
|
||
const isBubbleView = view === "bubble";
|
||
const quickSlot = params.get("slot");
|
||
const [page, setPage] = useState<PageKey>("chat");
|
||
const [status, setStatus] = useState(initialStatus);
|
||
const [error, setError] = useState<string | null>(null);
|
||
const [chatLayout, setChatLayout] = useState<ChatLayout>("single");
|
||
const [quickTarget, setQuickTarget] = useState<ChatSlot>(() =>
|
||
localStorage.getItem("astrbot.quickTarget") === "operations"
|
||
? "operations"
|
||
: "companion",
|
||
);
|
||
const [bubbleTarget, setBubbleTarget] = useState<"main" | "quick">(() =>
|
||
localStorage.getItem("astrbot.bubbleTarget") === "quick" ? "quick" : "main",
|
||
);
|
||
|
||
const refreshStatus = useCallback(async () => {
|
||
try {
|
||
const result = await invoke<unknown>("bootstrap_status");
|
||
setStatus(bootstrapStatusSchema.parse(result));
|
||
setError(null);
|
||
} catch (reason) {
|
||
setError(reason instanceof Error ? reason.message : String(reason));
|
||
}
|
||
}, []);
|
||
|
||
useEffect(() => {
|
||
void refreshStatus();
|
||
}, [refreshStatus]);
|
||
|
||
if (isBubbleView) return <StatusBubble />;
|
||
if (isQuickView) {
|
||
return (
|
||
<main className="quick-app-shell">
|
||
<ChatPanel
|
||
fixedSlot={
|
||
quickSlot === "operations"
|
||
? "operations"
|
||
: quickSlot === "companion"
|
||
? "companion"
|
||
: undefined
|
||
}
|
||
/>
|
||
</main>
|
||
);
|
||
}
|
||
|
||
return (
|
||
<main className="app-shell">
|
||
<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>
|
||
<nav className="primary-nav" aria-label="主导航">
|
||
{navigation.map((item) => (
|
||
<button
|
||
key={item.key}
|
||
type="button"
|
||
className={page === item.key ? "nav-item active" : "nav-item"}
|
||
onClick={() => setPage(item.key)}
|
||
>
|
||
<span className="nav-item-icon">{navIcon(item.key)}</span>
|
||
<span>
|
||
<strong>{item.label}</strong>
|
||
<small>{item.description}</small>
|
||
</span>
|
||
</button>
|
||
))}
|
||
</nav>
|
||
<div className="sidebar-footer">
|
||
<span className="connection-dot" />
|
||
<span>本地客户端已启动</span>
|
||
</div>
|
||
</aside>
|
||
|
||
<section className="app-content">
|
||
<PageHeader page={page} />
|
||
{error ? <p className="error-banner">IPC 暂不可用:{error}</p> : null}
|
||
{page === "chat" ? (
|
||
<ChatPage
|
||
chatLayout={chatLayout}
|
||
quickTarget={quickTarget}
|
||
onLayoutChange={setChatLayout}
|
||
onOpenQuickChat={() =>
|
||
void invoke("open_quick_chat_window", { slot: quickTarget })
|
||
}
|
||
/>
|
||
) : null}
|
||
{page === "connections" ? <ProfileManager /> : null}
|
||
{page === "desktop" ? (
|
||
<DesktopBehaviorPage
|
||
bubbleTarget={bubbleTarget}
|
||
quickTarget={quickTarget}
|
||
onBubbleTargetChange={(value) => {
|
||
setBubbleTarget(value);
|
||
localStorage.setItem("astrbot.bubbleTarget", value);
|
||
}}
|
||
onQuickTargetChange={(value) => {
|
||
setQuickTarget(value);
|
||
localStorage.setItem("astrbot.quickTarget", value);
|
||
}}
|
||
onOpenQuickChat={() =>
|
||
void invoke("open_quick_chat_window", { slot: quickTarget })
|
||
}
|
||
/>
|
||
) : null}
|
||
{page === "pet" ? <PetPage /> : null}
|
||
{page === "diagnostics" ? (
|
||
<DiagnosticsPage
|
||
status={status}
|
||
onRefresh={() => void refreshStatus()}
|
||
/>
|
||
) : null}
|
||
</section>
|
||
</main>
|
||
);
|
||
}
|
||
|
||
function navIcon(page: PageKey) {
|
||
return {
|
||
chat: "⌁",
|
||
connections: "◎",
|
||
desktop: "◈",
|
||
pet: "✦",
|
||
diagnostics: "⚙",
|
||
}[page];
|
||
}
|
||
|
||
function PageHeader({ page }: { page: PageKey }) {
|
||
const current = navigation.find((item) => item.key === page) ?? navigation[0];
|
||
return (
|
||
<header className="page-header">
|
||
<div>
|
||
<p className="eyebrow">AstrBot Desktop</p>
|
||
<h1>{current.label}</h1>
|
||
<p className="subtitle">
|
||
{current.description},所有本机能力都经过客户端权限边界。
|
||
</p>
|
||
</div>
|
||
<div className="header-status-pill">
|
||
<span className="connection-dot" />
|
||
双槽位架构
|
||
</div>
|
||
</header>
|
||
);
|
||
}
|
||
|
||
function ChatPage({
|
||
chatLayout,
|
||
quickTarget,
|
||
onLayoutChange,
|
||
onOpenQuickChat,
|
||
}: {
|
||
chatLayout: ChatLayout;
|
||
quickTarget: ChatSlot;
|
||
onLayoutChange: (layout: ChatLayout) => void;
|
||
onOpenQuickChat: () => void;
|
||
}) {
|
||
return (
|
||
<div className="page-stack">
|
||
<section className="welcome-card">
|
||
<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>
|
||
);
|
||
}
|
||
|
||
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({
|
||
bubbleTarget,
|
||
quickTarget,
|
||
onBubbleTargetChange,
|
||
onQuickTargetChange,
|
||
onOpenQuickChat,
|
||
}: {
|
||
bubbleTarget: "main" | "quick";
|
||
quickTarget: ChatSlot;
|
||
onBubbleTargetChange: (value: "main" | "quick") => void;
|
||
onQuickTargetChange: (value: ChatSlot) => void;
|
||
onOpenQuickChat: () => void;
|
||
}) {
|
||
return (
|
||
<div className="page-stack">
|
||
<section className="settings-grid">
|
||
<SettingCard
|
||
title="快速聊天"
|
||
description="设置气泡或快捷入口打开哪个实例。"
|
||
>
|
||
<label>
|
||
<span>默认目标槽位</span>
|
||
<select
|
||
value={quickTarget}
|
||
onChange={(event) =>
|
||
onQuickTargetChange(event.target.value as ChatSlot)
|
||
}
|
||
>
|
||
<option value="companion">陪伴实例</option>
|
||
<option value="operations">运维实例</option>
|
||
</select>
|
||
</label>
|
||
<button type="button" onClick={onOpenQuickChat}>
|
||
立即打开快速聊天
|
||
</button>
|
||
</SettingCard>
|
||
<SettingCard
|
||
title="状态气泡"
|
||
description="气泡显示连接状态,并根据这里的设置响应点击。"
|
||
>
|
||
<label>
|
||
<span>点击气泡时</span>
|
||
<select
|
||
value={bubbleTarget}
|
||
onChange={(event) =>
|
||
onBubbleTargetChange(event.target.value as "main" | "quick")
|
||
}
|
||
>
|
||
<option value="main">打开主界面</option>
|
||
<option value="quick">打开快速聊天</option>
|
||
</select>
|
||
</label>
|
||
<div className="status-preview">
|
||
<span className="connection-dot" />
|
||
气泡状态显示已预留
|
||
</div>
|
||
</SettingCard>
|
||
</section>
|
||
<section className="info-card">
|
||
<span className="section-kicker">Desktop companion</span>
|
||
<h2>后续桌面能力</h2>
|
||
<div className="feature-list">
|
||
<span>托盘与单实例</span>
|
||
<span>窗口置顶开关</span>
|
||
<span>多显示器策略</span>
|
||
<span>游戏模式靠边活动</span>
|
||
</div>
|
||
<p>
|
||
这些能力先保留为独立设置区,后续接入 Rust
|
||
本机能力层,不会与聊天状态混在一起。
|
||
</p>
|
||
</section>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
function PetPage() {
|
||
return (
|
||
<div className="page-stack">
|
||
<PetPackImporter />
|
||
<section className="settings-grid">
|
||
<SettingCard
|
||
title="显示范围"
|
||
description="决定桌宠只在应用内显示,还是允许在整个桌面活动。"
|
||
>
|
||
<select defaultValue="app">
|
||
<option value="app">仅应用 UI 内</option>
|
||
<option value="desktop">整个桌面</option>
|
||
</select>
|
||
</SettingCard>
|
||
<SettingCard
|
||
title="活动策略"
|
||
description="为后续游戏模式和双屏行为预留配置入口。"
|
||
>
|
||
<select defaultValue="smart">
|
||
<option value="smart">智能选择显示器</option>
|
||
<option value="primary">固定主屏</option>
|
||
<option value="secondary">优先副屏</option>
|
||
</select>
|
||
</SettingCard>
|
||
</section>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
function DiagnosticsPage({
|
||
status,
|
||
onRefresh,
|
||
}: {
|
||
status: BootstrapStatus;
|
||
onRefresh: () => void;
|
||
}) {
|
||
return (
|
||
<div className="page-stack">
|
||
<section className="status-grid" aria-label="bootstrap status">
|
||
<StatusCard label="Rust Core" value={status.rustCore} />
|
||
<StatusCard label="Platform" value={status.platform} />
|
||
<StatusCard label="App Version" value={status.appVersion} />
|
||
<StatusCard label="Protocol" value={status.protocolVersion} />
|
||
<StatusCard
|
||
label="Database Schema"
|
||
value={String(status.databaseSchemaVersion)}
|
||
/>
|
||
</section>
|
||
<section className="boundaries">
|
||
<span className="section-kicker">Security boundary</span>
|
||
<h2>本机能力保持可审计</h2>
|
||
<ul>
|
||
<li>凭据只在 Rust 与 Windows 安全存储之间流动。</li>
|
||
<li>两个 REST/SSE 槽位保持独立状态、会话、消息与未读数。</li>
|
||
<li>本机操作必须经过白名单、角色、路径和用户授权。</li>
|
||
<li>文件事务默认保留源文件,不静默覆盖目标。</li>
|
||
</ul>
|
||
</section>
|
||
<button type="button" onClick={onRefresh}>
|
||
刷新 IPC 状态
|
||
</button>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
function SettingCard({
|
||
title,
|
||
description,
|
||
children,
|
||
}: {
|
||
title: string;
|
||
description: string;
|
||
children: React.ReactNode;
|
||
}) {
|
||
return (
|
||
<section className="setting-card">
|
||
<div>
|
||
<span className="section-kicker">Settings</span>
|
||
<h2>{title}</h2>
|
||
<p>{description}</p>
|
||
</div>
|
||
<div className="setting-card-controls">{children}</div>
|
||
</section>
|
||
);
|
||
}
|
||
|
||
function slotLabel(slot: ChatSlot) {
|
||
return slot === "companion" ? "陪伴实例" : "运维实例";
|
||
}
|
||
|
||
interface StatusCardProps {
|
||
label: string;
|
||
value: string;
|
||
}
|
||
function StatusCard({ label, value }: StatusCardProps) {
|
||
return (
|
||
<article className="status-card">
|
||
<span>{label}</span>
|
||
<strong>{value}</strong>
|
||
</article>
|
||
);
|
||
}
|