1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
import { useState, useEffect, useRef, memo } from 'react';
import ReactMarkdown from 'react-markdown';
import remarkGfm from 'remark-gfm';
import Stage1 from './Stage1';
import Stage2 from './Stage2';
import Stage3 from './Stage3';
import './ChatInterface.css';
const remarkPlugins = [remarkGfm];
// Only memoize user messages (they never change once sent)
const UserMessage = memo(function UserMessage({ content }) {
return (
<div className="message-group">
<div className="user-message">
<div className="message-label">You</div>
<div className="message-content">
<div className="markdown-content">
<ReactMarkdown remarkPlugins={remarkPlugins}>{content}</ReactMarkdown>
</div>
</div>
</div>
</div>
);
});
// Memoize completed assistant messages, but skip memo for the active (last) one
const AssistantMessage = memo(function AssistantMessage({ msg, isActive }) {
return (
<div className="message-group">
<div className="assistant-message">
<div className="message-label">LLM Council</div>
{/* Stage 1 */}
{msg.loading?.stage1 && (
<div className="stage-loading">
<div className="spinner"></div>
<span>Running Stage 1: Collecting individual responses...</span>
</div>
)}
{msg.stage1 && <Stage1 responses={msg.stage1} />}
{/* Stage 2 */}
{msg.loading?.stage2 && (
<div className="stage-loading">
<div className="spinner"></div>
<span>Running Stage 2: Peer rankings...</span>
</div>
)}
{msg.stage2 && (
<Stage2
rankings={msg.stage2}
labelToModel={msg.metadata?.label_to_model}
aggregateRankings={msg.metadata?.aggregate_rankings}
/>
)}
{/* Stage 3 */}
{msg.loading?.stage3 && (
<div className="stage-loading">
<div className="spinner"></div>
<span>Running Stage 3: Final synthesis...</span>
</div>
)}
{msg.stage3 && <Stage3 finalResponse={msg.stage3} />}
</div>
</div>
);
}, (prevProps, nextProps) => {
// If active (streaming), always re-render
if (prevProps.isActive || nextProps.isActive) return false;
// Otherwise skip re-render (completed messages don't change)
return true;
});
export default function ChatInterface({
conversation,
onSendMessage,
onStopGeneration,
isLoading,
pendingInput,
onPendingInputConsumed,
}) {
const [input, setInput] = useState('');
const textareaRef = useRef(null);
const messagesEndRef = useRef(null);
useEffect(() => {
messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' });
}, [conversation, isLoading]);
// Recover input from stopped generation
useEffect(() => {
if (pendingInput !== null) {
setInput(pendingInput);
onPendingInputConsumed();
setTimeout(() => textareaRef.current?.focus(), 0);
}
}, [pendingInput]);
const handleSubmit = (e) => {
e.preventDefault();
if (input.trim() && !isLoading) {
onSendMessage(input);
setInput('');
}
};
const handleKeyDown = (e) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
handleSubmit(e);
}
};
if (!conversation) {
return (
<div className="chat-interface">
<div className="empty-state">
<h2>Welcome to LLM Council</h2>
<p>Create a new conversation to get started</p>
</div>
</div>
);
}
return (
<div className="chat-interface">
<div className="messages-container">
{conversation.messages.length === 0 ? (
<div className="empty-state">
<h2>Start a conversation</h2>
<p>Ask a question to consult the LLM Council</p>
</div>
) : (
conversation.messages.map((msg, index) => {
if (msg.role === 'user') {
return <UserMessage key={index} content={msg.content} />;
}
const isLastAssistant = isLoading && index === conversation.messages.length - 1;
return <AssistantMessage key={index} msg={msg} isActive={isLastAssistant} />;
})
)}
{isLoading && (
<div className="loading-indicator">
<div className="spinner"></div>
<span>Consulting the council...</span>
</div>
)}
<div ref={messagesEndRef} />
</div>
<form className="input-form" onSubmit={handleSubmit}>
<textarea
ref={textareaRef}
className="message-input"
placeholder="Ask your question... (Shift+Enter for new line, Enter to send)"
value={input}
onChange={(e) => setInput(e.target.value)}
onKeyDown={handleKeyDown}
disabled={isLoading}
rows={3}
/>
{isLoading ? (
<button
type="button"
className="stop-button"
onClick={onStopGeneration}
>
Stop
</button>
) : (
<button
type="submit"
className="send-button"
disabled={!input.trim()}
>
Send
</button>
)}
</form>
</div>
);
}
|