**Bridge 系统核心(src/lib/bridge/)** - bridge-manager: 生命周期编排、adapter 事件循环、会话级并发控制 - channel-adapter: 抽象基类 + 注册式 adapter 工厂 - channel-router: IM 地址 → CodePilot session 自动映射 - conversation-engine: 服务端消费 streamClaude() SSE 流 - permission-broker: 权限请求转发到 IM 内联按钮 - delivery-layer: 出站分片、限流、重试退避、HTML 降级 - telegram-adapter: Telegram 长轮询 + offset 安全水位 + 幂等去重 - security/: 路径校验、危险输入检测、滑动窗口限流 **Telegram 通知 Bot(src/lib/telegram-bot.ts)** - 独立通知模式,与 bridge 模式互斥 - bridgeModeActive 使用 globalThis 存储,避免 HMR 重置导致轮询冲突 **API 路由** - /api/bridge: GET 状态查询 + POST start/stop/auto-start - /api/bridge/channels: 活跃通道列表 - /api/bridge/settings: bridge 设置读写 - /api/settings/telegram: 凭据管理(含 allowed_users 白名单) - /api/settings/telegram/verify: 连接验证 + chat ID 自动检测 **UI 重构:Bridge 提升为一级导航** - NavRail 新增 Bridge 图标导航项 - /bridge 页面:BridgeLayout hash 路由,Bridge + Telegram 两个子 tab - BridgeSection: 总开关、状态、启停、通道开关、适配器状态、默认设置 - TelegramBridgeSection: 凭据配置 + 白名单 + Setup Guide(6 步) - Settings 移除 Telegram 和 Bridge tab,只保留 4 个核心 tab **Bug 修复** - bridge-manager: runAdapterLoop 在 state.running=true 之前被调用, while 条件同步求值为 false 导致消费者循环不启动。修复为先设 running 再启动 loop - telegram-bot: bridgeModeActive 改用 globalThis 防止 HMR 丢失状态 - settings/telegram route: TELEGRAM_KEYS 补充 telegram_bridge_allowed_users **Electron** - 窗口关闭时 bridge 活跃则最小化到系统托盘而非退出 - 启动时通过 auto-start API 恢复桥接 - preload 暴露 bridge 相关 IPC **文档** - docs/bridge-system.md: 完整架构文档 - docs/chat-sdk-integration-feasibility.md: Chat SDK 集成调研 - CLAUDE.md 添加 bridge 文档链接 chore: bump v0.21.0 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
94 lines
3.2 KiB
TypeScript
94 lines
3.2 KiB
TypeScript
"use client";
|
|
|
|
import { useState, useCallback, useSyncExternalStore } from "react";
|
|
import { HugeiconsIcon } from "@hugeicons/react";
|
|
import type { IconSvgElement } from "@hugeicons/react";
|
|
import { Wifi01Icon, TelegramIcon } from "@hugeicons/core-free-icons";
|
|
import { cn } from "@/lib/utils";
|
|
import { BridgeSection } from "./BridgeSection";
|
|
import { TelegramBridgeSection } from "./TelegramBridgeSection";
|
|
import { useTranslation } from "@/hooks/useTranslation";
|
|
import type { TranslationKey } from "@/i18n";
|
|
|
|
type Section = "bridge" | "telegram";
|
|
|
|
interface SidebarItem {
|
|
id: Section;
|
|
label: string;
|
|
icon: IconSvgElement;
|
|
}
|
|
|
|
const sidebarItems: SidebarItem[] = [
|
|
{ id: "bridge", label: "Bridge", icon: Wifi01Icon },
|
|
{ id: "telegram", label: "Telegram", icon: TelegramIcon },
|
|
];
|
|
|
|
function getSectionFromHash(): Section {
|
|
if (typeof window === "undefined") return "bridge";
|
|
const hash = window.location.hash.replace("#", "");
|
|
if (sidebarItems.some((item) => item.id === hash)) {
|
|
return hash as Section;
|
|
}
|
|
return "bridge";
|
|
}
|
|
|
|
function subscribeToHash(callback: () => void) {
|
|
window.addEventListener("hashchange", callback);
|
|
return () => window.removeEventListener("hashchange", callback);
|
|
}
|
|
|
|
export function BridgeLayout() {
|
|
const hashSection = useSyncExternalStore(subscribeToHash, getSectionFromHash, () => "bridge" as Section);
|
|
const [overrideSection, setOverrideSection] = useState<Section | null>(null);
|
|
const activeSection = overrideSection ?? hashSection;
|
|
|
|
const { t } = useTranslation();
|
|
|
|
const bridgeLabelKeys: Record<string, TranslationKey> = {
|
|
'Bridge': 'bridge.title',
|
|
'Telegram': 'bridge.telegramSettings',
|
|
};
|
|
|
|
const handleSectionChange = useCallback((section: Section) => {
|
|
setOverrideSection(section);
|
|
window.history.replaceState(null, "", `/bridge#${section}`);
|
|
queueMicrotask(() => setOverrideSection(null));
|
|
}, []);
|
|
|
|
return (
|
|
<div className="flex h-full flex-col">
|
|
<div className="border-b border-border/50 px-6 pt-4 pb-4">
|
|
<h1 className="text-xl font-semibold">{t('bridge.title')}</h1>
|
|
<p className="text-sm text-muted-foreground">
|
|
{t('bridge.description')}
|
|
</p>
|
|
</div>
|
|
|
|
<div className="flex min-h-0 flex-1">
|
|
<nav className="flex w-52 shrink-0 flex-col gap-1 border-r border-border/50 p-3">
|
|
{sidebarItems.map((item) => (
|
|
<button
|
|
key={item.id}
|
|
onClick={() => handleSectionChange(item.id)}
|
|
className={cn(
|
|
"flex items-center gap-3 rounded-md px-3 py-2 text-sm font-medium transition-colors text-left",
|
|
activeSection === item.id
|
|
? "bg-accent text-accent-foreground"
|
|
: "text-muted-foreground hover:bg-accent/50 hover:text-foreground"
|
|
)}
|
|
>
|
|
<HugeiconsIcon icon={item.icon} className="h-4 w-4 shrink-0" />
|
|
{t(bridgeLabelKeys[item.label])}
|
|
</button>
|
|
))}
|
|
</nav>
|
|
|
|
<div className="flex-1 overflow-auto p-6">
|
|
{activeSection === "bridge" && <BridgeSection />}
|
|
{activeSection === "telegram" && <TelegramBridgeSection />}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|