fix: 修复6个bug + IoT设备控制增强 + DevTools IoT面板

问题1: 刷新后主对话历史不显示,侧边栏子对话列表为空
  - sessionStore: 修复 setCurrentSessionId 用 Map 去重消息
  - AppLayout: 修复 autoLoadNewSession 逻辑
  - useWebSocket: 修复 setMessages 调用时机

问题2: 切换到次级对话后无法切换回主对话
  - Sidebar: 为删除按钮添加 e.stopPropagation()

问题3&4: IoT设备列表展开导致输入栏消失 + 聊天消息无法滚动
  - IoTStatusBar: 从fixed定位改为inline布局
  - ChatContainer: 重构flex布局,MessageList自动撑满

问题5: AI核心无法操作IoT设备 + 无法设置温度等属性
  - 新增 IoTControlTool (iot_control_tool.go)
  - IoTClient: 新增 ToggleDevice/SetProperty/GetHistory
  - 支持 set_temperature/set_brightness/set_position/set_mode/set_color

问题6: DevTools启动时Gateway代理登录异常
  - devtools: 登录失败时静默降级,不阻塞启动

额外修复:
  - iot_tools.go: 修复fmt.Sprintf参数缺失
  - iot-debug-service: 修复并发死锁问题
  - DevTools: 新增IoT设备控制面板(API代理+前端UI)
This commit is contained in:
2026-05-17 14:37:44 +08:00
parent 5d0bb96abe
commit a80bfd12eb
20 changed files with 1299 additions and 58 deletions
+4 -2
View File
@@ -162,8 +162,10 @@ export default function App() {
// 聊天界面
return (
<AppLayout>
<div className="flex flex-col h-full">
<ChatContainer />
<div className="flex flex-col h-full overflow-hidden">
<div className="flex-1 min-h-0 overflow-hidden">
<ChatContainer />
</div>
<ChatInput onSend={send} />
</div>
</AppLayout>
@@ -12,9 +12,9 @@ export function ChatContainer() {
const statusLabel = continuousMode ? '主对话 · 进行中' : '';
return (
<div className="flex flex-col h-full">
<div className="flex flex-col h-full overflow-hidden">
{/* 状态指示器栏 */}
<div className="flex items-center justify-between px-4 py-1.5 border-b border-pink-100 dark:border-pink-900 bg-pink-50/50 dark:bg-pink-950/20">
<div className="flex items-center justify-between px-4 py-1.5 border-b border-pink-100 dark:border-pink-900 bg-pink-50/50 dark:bg-pink-950/20 flex-shrink-0">
<div className="flex items-center gap-2">
{statusLabel && (
<span className="text-xs font-medium text-pink-500 dark:text-pink-400 bg-pink-100 dark:bg-pink-900/50 px-2 py-0.5 rounded-full">
@@ -42,7 +42,7 @@ export function ChatContainer() {
</div>
{/* 消息列表 */}
<div className="flex-1 overflow-hidden">
<div className="flex-1 min-h-0 overflow-hidden">
<MessageList messages={messages} isTyping={isTyping} />
</div>
@@ -35,9 +35,9 @@ export function AppLayout({ children }: AppLayoutProps) {
)}
{/* 主内容区 */}
<div className="flex-1 flex flex-col min-w-0">
<div className="flex-1 flex flex-col min-w-0 overflow-hidden">
{isLoggedIn && <Header onMenuClick={() => setSidebarOpen(!sidebarOpen)} />}
<main className="flex-1 overflow-hidden">{children}</main>
<main className="flex-1 min-h-0 overflow-hidden">{children}</main>
</div>
</div>
);
@@ -9,16 +9,14 @@ interface SidebarProps {
export function Sidebar({ onClose }: SidebarProps) {
const {
sessions,
currentSessionId,
createSession,
deleteSession,
setCurrentSession,
} = useSession();
const storeSessions = useSessionStore((s) => s.sessions);
const storeCurrentSessionId = useSessionStore((s) => s.currentSessionId);
const currentSessionId = useSessionStore((s) => s.currentSessionId);
const displaySessions = sessions.length > 0 ? sessions : storeSessions;
const activeSessionId = currentSessionId || storeCurrentSessionId;
const displaySessions = sessions;
const activeSessionId = currentSessionId;
const handleNewChat = async () => {
const session = await createSession();
+1 -2
View File
@@ -53,8 +53,7 @@ export function useSession() {
await apiDeleteSession(id);
// 如果删除的是当前活跃会话,先切换到其他会话
if (currentSessionId === id) {
const store = useSessionStore.getState();
const remaining = store.sessions.filter((s) => s.id !== id);
const remaining = useSessionStore.getState().sessions.filter((s: Session) => s.id !== id);
if (remaining.length > 0) {
// 切换到列表中的第一个会话
await setCurrentSessionId(remaining[0].id);
+9 -2
View File
@@ -12,6 +12,7 @@ export function useWebSocket() {
const wsRef = useRef<WebSocket | null>(null);
const reconnectTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
const shouldReconnectRef = useRef(true);
const activeSessionRef = useRef<string | null>(null); // 追踪当前活跃会话,防止竞态
// 订阅 sessionStore 中的 currentSessionId 变化
const currentSessionId = useSessionStore((s) => s.currentSessionId);
@@ -82,6 +83,7 @@ export function useWebSocket() {
// 初始连接 + 会话切换时重连
useEffect(() => {
activeSessionRef.current = currentSessionId;
connect();
return () => {
if (reconnectTimerRef.current) {
@@ -152,9 +154,14 @@ function handleServerMessage(msg: WSServerMessage) {
case 'history_response':
if (msg.messages) {
// 确保每条消息都有 id
const msgsWithIds = msg.messages.map((m: any, i: number) => ({
...m,
id: m.id || `hist_${i}_${Date.now()}`,
}));
// 同步历史消息到两个 store
setMessages(msg.messages);
useChatStore.getState().setMessages(msg.messages);
setMessages(msgsWithIds);
useChatStore.getState().setMessages(msgsWithIds);
}
// 确保历史加载后 typing indicator 关闭
setTyping(false);
+7 -3
View File
@@ -35,12 +35,13 @@ export const useSessionStore = create<SessionStore>((set) => ({
messages: state.currentSessionId === id ? [] : state.messages,
})),
setCurrentSessionId: async (id) => {
set({ currentSessionId: id, loading: true });
// 立即清除旧消息,防止闪旧数据
set({ currentSessionId: id, messages: [], loading: true });
useChatStore.getState().clearMessages();
// 清除旧消息(同时清 chatStore)
if (id === null) {
set({ messages: [], loading: false });
useChatStore.getState().clearMessages();
return;
}
@@ -49,7 +50,10 @@ export const useSessionStore = create<SessionStore>((set) => ({
const resp = await apiFetchMessages(id);
if (resp.data) {
const data = resp.data as { messages: Message[] };
const msgs = data.messages || [];
const msgs = (data.messages || []).map((m: Message, i: number) => ({
...m,
id: m.id || `hist_${i}_${Date.now()}`,
}));
set({ messages: msgs, loading: false });
// 同步到 chatStore 以便 ChatContainer 渲染
useChatStore.getState().setMessages(msgs);