import { useEffect } from 'react'; import { useShallow } from 'zustand/react/shallow'; import { FileIcon, FolderIcon, RefreshCwIcon, TrashIcon } from 'lucide-react'; import { useChatDevStore } from '../store'; import { useCodeGraphStore } from '@/pages/code-graph/store'; export const OpencodeChat = () => { const { question, projectInfo, sessionId, isLoading, saveSessionInfo, loadSessionInfo, fetchSession, fetchMessages, clearSession, setData, } = useChatDevStore( useShallow((s) => ({ question: s.question, projectInfo: s.projectInfo, setData: s.setData, sessionId: s.sessionId, isLoading: s.isLoading, saveSessionInfo: s.saveSessionInfo, loadSessionInfo: s.loadSessionInfo, fetchSession: s.fetchSession, fetchMessages: s.fetchMessages, clearSession: s.clearSession, })), ); const codeGraphStore = useCodeGraphStore(useShallow((s) => ({ createQuestion: s.createQuestion, url: s.url, }))); // 初始化后尝试从 sessionStorage 恢复 opencode session 并加载历史消息 useEffect(() => { if (!codeGraphStore.url) return; const info = loadSessionInfo(); if (info) { fetchSession(info.sessionId); fetchMessages(info.sessionId); } }, [codeGraphStore.url]); const relativePath = projectInfo ? (projectInfo.filepath || '').replace((projectInfo.projectPath || '') + '/', '') || '/' : null; const onSend = async () => { const res = await codeGraphStore.createQuestion({ question, projectPath: projectInfo?.projectPath, engine: 'opencode', sessionId: sessionId || undefined, }); console.log(res); if (res?.code === 200 && res?.data) { const { sessionId: newSessionId, messageId: newMessageId } = res.data as any; if (newSessionId) { saveSessionInfo(newSessionId, newMessageId || ''); fetchMessages(newSessionId); } } }; return (
{/* Session 信息栏 */} {sessionId && (
Session: {sessionId.slice(0, 8)}...
)} {/* 内容区 */}
{/* 节点信息 */} {relativePath && (
{relativePath}
)} {projectInfo?.projectPath && !relativePath && (
{projectInfo.projectPath}
)} {/* 问题输入区 */}