2026-05-07 02:41:01 +08:00
|
|
|
import { useState, useEffect, useCallback, useRef } from 'react';
|
|
|
|
|
import { chatSocket } from '@/lib/websocket';
|
|
|
|
|
import type { Message } from '@/types';
|
|
|
|
|
|
2026-05-07 04:09:00 +08:00
|
|
|
interface PipelineState {
|
|
|
|
|
phase: string;
|
|
|
|
|
progress: number;
|
|
|
|
|
currentItem?: number;
|
|
|
|
|
totalItems?: number;
|
|
|
|
|
status?: string;
|
2026-05-07 03:22:15 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-07 02:41:01 +08:00
|
|
|
export function useChat(conversationId: string | null) {
|
|
|
|
|
const [messages, setMessages] = useState<Message[]>([]);
|
|
|
|
|
const [connected, setConnected] = useState(false);
|
2026-05-07 03:22:15 +08:00
|
|
|
const [thinking, setThinking] = useState(false);
|
|
|
|
|
const [toolStatus, setToolStatus] = useState<string | null>(null);
|
2026-05-07 04:09:00 +08:00
|
|
|
const [pipeline, setPipeline] = useState<PipelineState | null>(null);
|
2026-05-07 02:41:01 +08:00
|
|
|
const pendingRef = useRef(false);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
chatSocket.connect();
|
|
|
|
|
|
|
|
|
|
chatSocket.on('connected', () => setConnected(true));
|
2026-05-07 03:22:15 +08:00
|
|
|
|
2026-05-07 02:41:01 +08:00
|
|
|
chatSocket.on('history', (data) => {
|
|
|
|
|
setMessages((data.messages as Message[]) || []);
|
|
|
|
|
});
|
2026-05-07 03:22:15 +08:00
|
|
|
|
2026-05-07 02:41:01 +08:00
|
|
|
chatSocket.on('message', (data) => {
|
|
|
|
|
setMessages((prev) => [...prev, data as unknown as Message]);
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-07 04:09:00 +08:00
|
|
|
// Streaming
|
2026-05-07 03:22:15 +08:00
|
|
|
chatSocket.on('status', (data) => {
|
|
|
|
|
if (data.status === 'thinking') setThinking(true);
|
2026-05-07 04:09:00 +08:00
|
|
|
if (data.status === 'done') { setThinking(false); setToolStatus(null); }
|
2026-05-07 03:22:15 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
chatSocket.on('message_start', (data) => {
|
|
|
|
|
setThinking(false);
|
|
|
|
|
setMessages((prev) => [...prev, {
|
|
|
|
|
id: data.id as string,
|
|
|
|
|
role: 'assistant',
|
|
|
|
|
content: '',
|
|
|
|
|
created_at: new Date().toISOString(),
|
|
|
|
|
conversation_id: '',
|
|
|
|
|
} as Message]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
chatSocket.on('text_delta', (data) => {
|
|
|
|
|
setMessages((prev) => prev.map((m) =>
|
|
|
|
|
m.id === data.id ? { ...m, content: m.content + (data.text as string) } : m
|
|
|
|
|
));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
chatSocket.on('message_end', () => {
|
|
|
|
|
setThinking(false);
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-07 04:09:00 +08:00
|
|
|
// Tools
|
2026-05-07 03:22:15 +08:00
|
|
|
chatSocket.on('tool_start', (data) => {
|
2026-05-07 04:09:00 +08:00
|
|
|
setToolStatus(`执行: ${data.tool}...`);
|
2026-05-07 03:22:15 +08:00
|
|
|
});
|
|
|
|
|
|
2026-05-07 04:09:00 +08:00
|
|
|
chatSocket.on('tool_result', (data) => {
|
2026-05-07 03:22:15 +08:00
|
|
|
setToolStatus(null);
|
|
|
|
|
setThinking(true);
|
2026-05-07 04:09:00 +08:00
|
|
|
// Save tool result as a tool-type message for inline display
|
|
|
|
|
setMessages((prev) => [...prev, {
|
|
|
|
|
id: `tool-${Date.now()}`,
|
|
|
|
|
role: 'tool' as const,
|
|
|
|
|
content: JSON.stringify({ tool: data.tool, result: data.result }),
|
|
|
|
|
created_at: new Date().toISOString(),
|
|
|
|
|
conversation_id: '',
|
|
|
|
|
}]);
|
2026-05-07 03:22:15 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
chatSocket.on('tool_error', (data) => {
|
2026-05-07 04:09:00 +08:00
|
|
|
setToolStatus(`失败: ${data.tool}`);
|
|
|
|
|
setTimeout(() => setToolStatus(null), 4000);
|
2026-05-07 03:22:15 +08:00
|
|
|
});
|
|
|
|
|
|
2026-05-07 04:09:00 +08:00
|
|
|
// Pipeline progress
|
|
|
|
|
chatSocket.on('pipeline_progress', (data) => {
|
|
|
|
|
setPipeline({
|
|
|
|
|
phase: data.phase as string,
|
|
|
|
|
progress: data.progress as number,
|
|
|
|
|
currentItem: data.currentItem as number,
|
|
|
|
|
totalItems: data.totalItems as number,
|
|
|
|
|
status: data.status as string,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return () => { chatSocket.disconnect(); };
|
2026-05-07 02:41:01 +08:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (conversationId && connected && !pendingRef.current) {
|
|
|
|
|
pendingRef.current = true;
|
|
|
|
|
chatSocket.send('init', { conversationId });
|
|
|
|
|
}
|
|
|
|
|
if (!conversationId) {
|
|
|
|
|
pendingRef.current = false;
|
|
|
|
|
setMessages([]);
|
|
|
|
|
}
|
|
|
|
|
}, [conversationId, connected]);
|
|
|
|
|
|
|
|
|
|
const send = useCallback((content: string) => {
|
|
|
|
|
chatSocket.send('chat', { content });
|
|
|
|
|
}, []);
|
|
|
|
|
|
2026-05-07 04:09:00 +08:00
|
|
|
const stop = useCallback(() => {
|
|
|
|
|
chatSocket.stop();
|
|
|
|
|
}, []);
|
|
|
|
|
|
2026-05-07 02:41:01 +08:00
|
|
|
const createConversation = useCallback((title: string, accountId?: string) => {
|
|
|
|
|
chatSocket.send('create_conversation', { title, accountId });
|
|
|
|
|
}, []);
|
|
|
|
|
|
2026-05-07 04:09:00 +08:00
|
|
|
return { messages, connected, thinking, toolStatus, pipeline, send, stop, createConversation };
|
2026-05-07 02:41:01 +08:00
|
|
|
}
|