-
Notifications
You must be signed in to change notification settings - Fork 107
refactor: decompose ChatPanel into focused modules #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SweetSophia
wants to merge
12
commits into
MiniMax-AI:main
Choose a base branch
from
SweetSophia:refactor/extract-chat-engine
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a1bbb19
refactor: extract ChatPanel into focused modules
SweetSophia 5c3fa86
fix: consolidate duplicate ModManager imports in useConversationEngin…
Copilot 12446ac
fix: stabilize runConversation identity to prevent listener churn
SweetSophia dd2af55
fix: address code review findings across ChatPanel modules
SweetSophia 516b135
fix: address review feedback on types, timeout cleanup, and config guard
Copilot b7ada0c
fix: address Copilot review — tool loop, save snapshot, layer cleanup
SweetSophia 47e1921
fix: Prettier import style, stale memory guard, remove redundant ref
SweetSophia d9a1efc
fix: flush debounced save on cleanup, remove stale dep in handleReset…
SweetSophia b85e83a
fix(ChatPanel): useLayoutEffect for setSessionPath; fix debounced sav…
Copilot 9585391
refactor: extract PendingSaveSnapshot type from inline ref type
Copilot 2d12322
fix: ModManager value import, sessionPath out of save deps, redact se…
Copilot bee5bd7
fix: only break loop when respond_to_user is sole tool call
SweetSophia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
198 changes: 198 additions & 0 deletions
198
apps/webuiapps/src/components/ChatPanel/ChatSubComponents.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,198 @@ | ||
| /** | ||
| * Helper sub-components extracted from ChatPanel | ||
| * | ||
| * StageIndicator, ActionsTaken, CharacterAvatar, renderMessageContent | ||
| */ | ||
|
|
||
| import React, { useState, useEffect, useCallback, memo, useRef } from 'react'; | ||
| import { ChevronDown, ChevronRight } from 'lucide-react'; | ||
| import type { CharacterConfig } from '@/lib/characterManager'; | ||
| import { resolveEmotionMedia } from '@/lib/characterManager'; | ||
| import type { ModManager } from '@/lib/modManager'; | ||
| import styles from './index.module.scss'; | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Render message content — formats (action text) as styled spans | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| export function renderMessageContent(content: string): React.ReactNode { | ||
| const parts = content.split(/(\([^)]+\))/g); | ||
| return parts.map((part, i) => { | ||
| if (/^\([^)]+\)$/.test(part)) { | ||
| return ( | ||
| <span key={i} className={styles.emotion}> | ||
| {part} | ||
| </span> | ||
| ); | ||
| } | ||
| return part; | ||
| }); | ||
| } | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Stage Indicator | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| export const StageIndicator: React.FC<{ modManager: ModManager | null }> = ({ modManager }) => { | ||
| if (!modManager) return null; | ||
|
|
||
| const total = modManager.stageCount; | ||
| const current = modManager.currentStageIndex; | ||
| const finished = modManager.isFinished; | ||
|
|
||
| return ( | ||
| <div className={styles.stageIndicator}> | ||
| <span className={styles.stageText}> | ||
| Stage {finished ? total : current + 1}/{total} | ||
| </span> | ||
| <div className={styles.stageDots}> | ||
| {Array.from({ length: total }, (_, i) => ( | ||
| <div | ||
| key={i} | ||
| className={`${styles.stageDot} ${ | ||
| i < current || finished | ||
| ? styles.stageDotCompleted | ||
| : i === current | ||
| ? styles.stageDotCurrent | ||
| : '' | ||
| }`} | ||
| /> | ||
| ))} | ||
| </div> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Actions Taken (collapsible) | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| export const ActionsTaken: React.FC<{ calls: string[] }> = ({ calls }) => { | ||
| const [open, setOpen] = useState(false); | ||
| if (calls.length === 0) return null; | ||
|
|
||
| return ( | ||
| <div className={styles.actionsTaken}> | ||
| <button className={styles.actionsTakenToggle} onClick={() => setOpen(!open)}> | ||
| Actions taken | ||
| {open ? <ChevronDown size={12} /> : <ChevronRight size={12} />} | ||
| </button> | ||
| {open && ( | ||
| <div className={styles.actionsTakenList}> | ||
| {calls.map((c, i) => ( | ||
| <div key={i}>{c}</div> | ||
| ))} | ||
| </div> | ||
| )} | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // CharacterAvatar – crossfade between emotion media without flashing | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| interface AvatarLayer { | ||
| url: string; | ||
| type: 'video' | 'image'; | ||
| active: boolean; | ||
| } | ||
|
|
||
| export const CharacterAvatar: React.FC<{ | ||
| character: CharacterConfig; | ||
| emotion?: string; | ||
| onEmotionEnd: () => void; | ||
| }> = memo(({ character, emotion, onEmotionEnd }) => { | ||
| const isIdle = !emotion; | ||
| const media = resolveEmotionMedia(character, emotion || 'default'); | ||
|
|
||
| const [layers, setLayers] = useState<AvatarLayer[]>(() => | ||
| media ? [{ url: media.url, type: media.type, active: true }] : [], | ||
| ); | ||
| const activeUrl = layers.find((l) => l.active)?.url; | ||
| const cleanupRef = useRef<ReturnType<typeof setTimeout> | null>(null); | ||
|
|
||
| useEffect(() => { | ||
| return () => { | ||
| if (cleanupRef.current) clearTimeout(cleanupRef.current); | ||
| }; | ||
| }, []); | ||
|
|
||
| useEffect(() => { | ||
| if (!media) { | ||
| setLayers([]); | ||
| return; | ||
| } | ||
| if (media.url === activeUrl) return; | ||
| setLayers((prev) => { | ||
| // If the URL already exists (possibly inactive), reactivate it | ||
| const existing = prev.find((l) => l.url === media.url); | ||
| if (existing) { | ||
| // Cancel any pending cleanup that might remove this layer | ||
| if (cleanupRef.current) { | ||
| clearTimeout(cleanupRef.current); | ||
| cleanupRef.current = null; | ||
| } | ||
| return prev.map((l) => ({ | ||
| ...l, | ||
| active: l.url === media.url, | ||
| })); | ||
| } | ||
| return [...prev, { url: media.url, type: media.type, active: false }]; | ||
| }); | ||
| }, [media?.url, activeUrl]); | ||
|
|
||
| const handleMediaReady = useCallback((readyUrl: string) => { | ||
| setLayers((prev) => { | ||
| const staleUrls = prev.filter((l) => l.url !== readyUrl).map((l) => l.url); | ||
| if (cleanupRef.current) clearTimeout(cleanupRef.current); | ||
| cleanupRef.current = setTimeout(() => { | ||
| setLayers((curr) => curr.filter((l) => !staleUrls.includes(l.url))); | ||
| }, 300); | ||
| return prev.map((l) => ({ ...l, active: l.url === readyUrl })); | ||
| }); | ||
| }, []); | ||
|
|
||
| if (layers.length === 0) { | ||
| return <div className={styles.avatarPlaceholder}>{character.character_name.charAt(0)}</div>; | ||
| } | ||
|
|
||
| return ( | ||
| <> | ||
| {layers.map((layer) => { | ||
| const layerStyle: React.CSSProperties = { | ||
| position: 'absolute', | ||
| inset: 0, | ||
| opacity: layer.active ? 1 : 0, | ||
| transition: 'opacity 0.25s ease-out', | ||
| }; | ||
| if (layer.type === 'video') { | ||
| return ( | ||
| <video | ||
| key={layer.url} | ||
| className={styles.avatarImage} | ||
| style={layerStyle} | ||
| src={layer.url} | ||
| autoPlay | ||
| loop={layer.active ? isIdle : false} | ||
| muted | ||
| playsInline | ||
| onCanPlay={!layer.active ? () => handleMediaReady(layer.url) : undefined} | ||
| onEnded={layer.active && !isIdle ? onEmotionEnd : undefined} | ||
| /> | ||
| ); | ||
| } | ||
| return ( | ||
| <img | ||
| key={layer.url} | ||
| className={styles.avatarImage} | ||
| style={layerStyle} | ||
| src={layer.url} | ||
| alt={character.character_name} | ||
| onLoad={!layer.active ? () => handleMediaReady(layer.url) : undefined} | ||
| /> | ||
| ); | ||
| })} | ||
| </> | ||
| ); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
handleMediaReadyalways activates the layer that finished loading (readyUrl). If the user switches emotions quickly, an old/inactive layer can finish loading later and incorrectly become active, causing the avatar to “jump back” to the wrong emotion. Consider tracking the latest desiredmedia.urlin a ref and ignoringreadyUrlevents that don’t match it (and/or storing a per-layer generation token) before promoting a layer to active.