multica/apps/web/app/hooks/use-messages.ts
Naiyuan Qing c5bf56282f feat(web): add gateway, hub, active-agent hooks and real message state
- use-gateway: GatewayClient WebSocket lifecycle with auto-connect/disconnect
- use-hub: REST polling for Hub status and agent CRUD operations
- use-active-agent: Zustand store for cross-component selected agent state
- use-messages: replace mock data with real addUser/addAssistant/clear API

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-30 22:40:50 +08:00

25 lines
776 B
TypeScript

import { useState, useCallback } from "react"
import { v7 as uuidv7 } from "uuid"
export interface Message {
id: string
role: "user" | "assistant"
content: string
agentId: string
}
export function useMessages() {
const [messages, setMessages] = useState<Message[]>([])
const addUserMessage = useCallback((content: string, agentId: string) => {
setMessages(prev => [...prev, { id: uuidv7(), role: "user", content, agentId }])
}, [])
const addAssistantMessage = useCallback((content: string, agentId: string) => {
setMessages(prev => [...prev, { id: uuidv7(), role: "assistant", content, agentId }])
}, [])
const clearMessages = useCallback(() => setMessages([]), [])
return { messages, addUserMessage, addAssistantMessage, clearMessages }
}