"use client"; import { useRef, useEffect, useState } from "react"; import { createPortal } from "react-dom"; import { motion, AnimatePresence } from "framer-motion"; import { Send, Paperclip, Menu, BookOpen, Users, StickyNote, RotateCcw, Check, Square, Pencil, Trash2, RefreshCw, Copy, Cpu, FileText, Volume2, Loader2, Sparkles, Moon, Save, Terminal, ClipboardList, Guitar, Image as ImageIcon, Shirt, Disc, ChevronUp, MoreHorizontal, Settings, ToggleLeft, ToggleRight, Skull, Binary, CloudRain, AlertTriangle, Clapperboard, Film } from "lucide-react"; import { ThinkingBlock } from "./Common"; import { CHARACTERS } from "../types"; import { WardrobeModal } from "./WardrobeModal"; import { MiniPlayer } from "./MiniPlayer"; import { MusicPlayerModal } from "./MusicPlayerModal"; import { useMusic } from "../context/MusicContext"; import ReactMarkdown from "react-markdown"; import { smartColorize } from "../lib/textFormatter"; import { parseMediaTag } from "../lib/parseDraw"; const DrawPlaceholder = ({ description, onClick, isGenerating, type = 'image' }: any) => { const isVideo = type === 'video'; const imageStyle = { bg: isGenerating ? "bg-purple-50/50 border-purple-200" : "bg-gray-50/40 border-gray-200 hover:bg-purple-50/30 hover:border-purple-300", iconBg: isGenerating ? "bg-purple-100" : "bg-gray-200 group-hover:bg-purple-100", iconColor: isGenerating ? "text-purple-500" : "text-gray-400 group-hover:text-purple-500", textTitle: isGenerating ? "text-purple-700" : "text-gray-500 group-hover:text-purple-600", label: isGenerating ? "Dream Weaver Working..." : "Click to Visualize" }; const videoStyle = { bg: isGenerating ? "bg-indigo-50/50 border-indigo-200" : "bg-gray-50/40 border-gray-200 hover:bg-indigo-50/30 hover:border-indigo-300", iconBg: isGenerating ? "bg-indigo-100" : "bg-gray-200 group-hover:bg-indigo-100", iconColor: isGenerating ? "text-indigo-500" : "text-gray-400 group-hover:text-indigo-500", textTitle: isGenerating ? "text-indigo-700" : "text-gray-500 group-hover:text-indigo-600", label: isGenerating ? "Director on Set..." : "Click to Animate" }; const style = isVideo ? videoStyle : imageStyle; return ( ); }; const renderContent = (text: string, onDraw?: (rawTag: string) => void, isGenerating: boolean = false, onMediaClick?: (media: any) => void) => { if (!text) return null; let workingText = text .replace(/[\s\S]*?<\/draw_log>/g, "") .replace(/[\s\S]*?<\/video_log>/g, "") .trim(); try { const segments = workingText.split(/(<(?:draw|video)>[\s\S]*?<\/(?:draw|video)>)/g); return (
{segments.map((segment, i) => { const cmd = parseMediaTag(segment); if (cmd && cmd.trigger) { return ( { if (onDraw) onDraw(segment); }} /> ); } const textParts = smartColorize(segment); return ( {textParts.map((part, j) => { if (part.type === 'colored') { return ( {part.text} ); } else { const imgParts = part.text.split(/(!\[.*?\]\(.*?\))/g); return ( {imgParts.map((subPart, k) => { const imgMatch = subPart.match(/!\[(.*?)\]\((.*?)\)/); if (imgMatch) { const alt = imgMatch[1]; const url = imgMatch[2]; if (alt === 'Video' || url.endsWith('.mp4') || url.endsWith('.webm')) { return (
Cinematic Sequence
Video RENDER
); } return (
Visual Memory
Generated onMediaClick && onMediaClick({ url, type: 'image' })} className="w-full rounded-xl object-cover border border-white/10 cursor-zoom-in" loading="lazy" />
); } return {subPart}; })}
); } })}
); })}
); } catch (error) { console.error("Render Error:", error); return {text}; } }; const COLOR_MAP: Record = {}; CHARACTERS.forEach(c => { const key = c.hex.toLowerCase(); COLOR_MAP[key] = c.id; }); type DialogueLine = { charId: string; charName: string; text: string; color: string; }; const parseDialogue = (fullText: string, defaultCharId: string): DialogueLine[] => { const lines: DialogueLine[] = []; const parts = fullText.split(/(\{\{\s*#?[a-fA-F0-9]{6}\s*\|\s*[\s\S]*?\s*\}\})/g); parts.forEach(part => { const match = part.match(/\{\{\s*(#?[a-fA-F0-9]{6})\s*\|\s*([\s\S]*?)\s*\}\}/); if (match) { let color = match[1].toLowerCase(); if (!color.startsWith("#")) color = "#" + color; const content = match[2].trim(); const charId = COLOR_MAP[color]; const charInfo = CHARACTERS.find(c => c.id === charId); if (charId && charInfo) { lines.push({ charId: charId, charName: charInfo.name, text: content, color: color }); } else { } } else { } }); if (lines.length === 0 && fullText.trim()) { const charInfo = CHARACTERS.find(c => c.id === defaultCharId); lines.push({ charId: defaultCharId, charName: charInfo?.name || "AI", text: fullText.trim(), color: charInfo?.hex || "#000" }); } return lines; }; const DialoguePlayer = ({ content, defaultCharId, onPlay, msgIndex, currentLock }: any) => { const lines = parseDialogue(content, defaultCharId); if (lines.length === 0) return No Dialogue; const handleClick = (text: string, charId: string, idx: number) => { if (currentLock) return; const charName = CHARACTERS.find(c => c.id === charId)?.name || charId; if (!window.confirm(`确定要生成 [${charName}] 的语音吗?`)) { return; } onPlay(text, charId, msgIndex, idx); }; return (
Voice Deck {currentLock ? 'GPU BUSY' : 'GPU READY'}
{lines.map((line, idx) => { const isThisLineLoading = currentLock?.msgId === msgIndex && currentLock?.lineIdx === idx; const isDisabled = currentLock !== null; return ( ); })}
); }; const Portal = ({ children }: { children: React.ReactNode }) => { const [mounted, setMounted] = useState(false); useEffect(() => setMounted(true), []); if (!mounted) return null; return createPortal(children, document.body); }; export const ChatInterface = ({ leftSidebarOpen, setLeftSidebarOpen, rightSidebarOpen, setRightSidebarOpen, setShowLocalWorld, setShowGlobalWorld, currentCharacter, currentSession, input, setInput, isLoading, selectedModel, customModelId, isMultimodal, selectedFile, setSelectedFile, fileInputRef, handleFileSelect, handleSend, stopGeneration, handleRegenerate, handleDeleteMessage, handleCopy, copiedIndex, openEditModal, voiceVariant, setVoiceVariant, handlePlayAudio, playingIndex, toggleMemoryMode, setSessions, useWebSearch, toggleWebSearch, toggleSaveModal, toggleCodeRepo, dbChars, useTTS, showToast, isGlobalGenerating, toggleWardrobe, apiProvider, setApiProvider, debugMode, setDebugMode, triggerEffect, currentOutfitId, setManualOutfitId, createNewSession, outfits, handleVisualCommand, setPreviewImage, }: any) => { const messagesEndRef = useRef(null); const [showFullPlayer, setShowFullPlayer] = useState(false); const { isPlaying } = useMusic(); const [showWardrobe, setShowWardrobe] = useState(false); const [showMobileTopMenu, setShowMobileTopMenu] = useState(false); const [showSettings, setShowSettings] = useState(false); const [consoleVisible, setConsoleVisible] = useState(true); const toggleDebugConsole = () => { const vcSwitch = document.querySelector('.vc-switch') as HTMLElement; if (vcSwitch) { const nextState = vcSwitch.style.display === 'none' ? 'block' : 'none'; vcSwitch.style.display = nextState; setConsoleVisible(nextState === 'block'); } else { showToast("Console not found (Mobile Only)"); } }; useEffect(() => { messagesEndRef.current?.scrollIntoView({ behavior: "smooth" }); }, [currentSession?.messages, currentSession?.id, isLoading]); const parseResponse = (text: string) => { const pattern = /([\s\S]*?)<\/thinking>/; const match = text.match(pattern); if (match) return { thought: match[1].trim(), answer: text.replace(pattern, "").trim() }; return { thought: null, answer: text }; }; const [ttsMenu, setTtsMenu] = useState<{ id: number; top: number; left: number } | null>(null); const [generatingLock, setGeneratingLock] = useState<{ msgId: number, lineIdx: number } | null>(null); const toggleTTSMenu = (e: React.MouseEvent, index: number) => { if (ttsMenu?.id === index) { setTtsMenu(null); return; } const rect = (e.currentTarget as HTMLElement).getBoundingClientRect(); setTtsMenu({ id: index, top: rect.top - 10, left: rect.right + 10 }); }; useEffect(() => { const closeMenu = () => setTtsMenu(null); window.addEventListener('click', closeMenu); return () => window.removeEventListener('click', closeMenu); }, []); const executeTTS = async (text: string, charId: string, msgId: number, lineIdx: number) => { if (generatingLock) return; setGeneratingLock({ msgId, lineIdx }); let targetId = charId; if (targetId === "soyo") targetId = voiceVariant === "down" ? "soyo_down" : "soyo_up"; if (targetId === "sakiko") targetId = voiceVariant === "white" ? "sakiko" : "oblivionis"; if (targetId === "mutsu") targetId = voiceVariant === "mortis" ? "mortis" : "mutsu"; if (showToast) showToast(`🎵 Requesting audio for ${targetId}...`); try { await handlePlayAudio(text, -1, targetId); } catch (e) { console.error(e); } finally { setGeneratingLock(null); } }; const ToolButtons = ({ isMobile = false }) => ( <> {currentSession?.memoryMode === 'sliding' && ( <>
)} {currentSession?.memoryMode === 'infinite' && ( <> {!isMobile && (
setShowWardrobe(false)} charName={currentCharacter.name} outfits={outfits} currentOutfit={currentOutfitId} onSelect={(id: string) => setManualOutfitId(id)} />
)} )} ); return (
{!leftSidebarOpen && ( )}
{currentSession?.memoryMode === 'sliding' ? ( 🎸 BanG Dream! ) : ( {currentCharacter.avatar} {currentCharacter.name} )} {customModelId || selectedModel.split('/').pop()}
{showSettings && (
Mobile Tools
System
Ver. Mutsu v7.0
Engine Next.js 15

Reveal hidden <tags> & logic

Dev Tools
)}
{showMobileTopMenu && (
)}
setShowFullPlayer(true)} rightSidebarOpen={rightSidebarOpen} />
{currentSession?.messages.map((msg: any, i: number) => { let txt = ""; let files: any[] = []; if (Array.isArray(msg.content)) { msg.content.forEach((x: any) => { if (x.type === 'text') txt += x.text; if (x.type === 'image_url') files.push(x.image_url.url) }) } else { txt = msg.content; } const { thought, answer } = msg.role === 'assistant' ? parseResponse(txt) : { thought: null, answer: txt }; const activeDrawMatch = txt.match(/([\s\S]*?)<\/draw>/); const logDrawMatch = txt.match(/([\s\S]*?)<\/draw_log>/); const rawDrawContent = (activeDrawMatch ? activeDrawMatch[1] : (logDrawMatch ? logDrawMatch[1] : null))?.trim(); const msgCharId = msg.characterId || currentSession.characterId; const msgChar = (dbChars?.length ? dbChars : CHARACTERS).find((c: any) => c.id === msgCharId) || currentCharacter; const isLast = i === currentSession.messages.length - 1; const isSecondLast = i === currentSession.messages.length - 2; const isLastUserMsg = msg.role === 'user' && (isLast || isSecondLast); if (msg.role === 'assistant' && !answer && !thought && !files.length) return null; return ( {msg.role === 'assistant' && (
{currentSession?.memoryMode === 'sliding' ? : msgChar.avatar}
)}
{files.length > 0 &&
{files.map((f: any, idx: number) => )}
} {thought && currentSession.memoryMode === 'infinite' && } {answer && (
{msg.role === 'assistant' ? ( renderContent(answer, handleVisualCommand, isGlobalGenerating, (media) => { if (media.type === 'image') setPreviewImage(media.url); }) ) : (
{answer}
)} {msg.role === 'assistant' && msg.modelName &&
{msg.modelName.split('/').pop()}
}
)} {debugMode && rawDrawContent && msg.role === 'assistant' && (
AI VISUAL INSTRUCTION RAW JSON
                                            {(() => {
                                                try { return JSON.stringify(JSON.parse(rawDrawContent), null, 2); }
                                                catch { return rawDrawContent; }
                                            })()}
                                        
)}
{msg.role === 'user' ? ( isLastUserMsg && ( <> ) ) : ( <> {useTTS && (
{ttsMenu?.id === i && (
setTtsMenu(null)} /> e.stopPropagation()} > c.type === 'text')?.text || "" : msg.content} defaultCharId={msgCharId} onPlay={executeTTS} msgIndex={i} currentLock={generatingLock || (isGlobalGenerating ? { msgId: -1, lineIdx: -1 } : null)} /> )}
)} {isLast && } )}
{msg.role === 'user' &&
} ) })}