-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessage.js
More file actions
159 lines (139 loc) · 5.48 KB
/
Copy pathMessage.js
File metadata and controls
159 lines (139 loc) · 5.48 KB
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
import { useState, useRef } from 'react';
import TokenProbabilities from './TokenProbabilities';
// Utility function to calculate background color based on probability
const getBackgroundColor = (tokenData) => {
if (!tokenData || !tokenData.top_logprobs) return 'transparent';
// Get the probability for this token (it's the highest probability in top_logprobs)
const highestLogprob = Math.max(...Object.values(tokenData.top_logprobs));
// Convert logprob to percentage
const percentage = Math.exp(highestLogprob) * 100;
// Define color stops
const colors = {
high: { r: 34, g: 197, b: 94 }, // Dark green (#22C55E)
mid: { r: 234, g: 179, b: 8 }, // Yellow (#EAB308)
low: { r: 139, g: 0, b: 0 } // Dark red (#8B0000)
};
let finalColor;
if (percentage >= 50) {
// Blend between high (100%) and mid (50%)
const ratio = (percentage - 50) / 50; // Will be 0 at 50% and 1 at 100%
finalColor = {
r: Math.round(colors.mid.r + (colors.high.r - colors.mid.r) * ratio),
g: Math.round(colors.mid.g + (colors.high.g - colors.mid.g) * ratio),
b: Math.round(colors.mid.b + (colors.high.b - colors.mid.b) * ratio)
};
} else {
// Blend between mid (50%) and low (0%)
const ratio = percentage / 50; // Will be 0 at 0% and 1 at 50%
finalColor = {
r: Math.round(colors.low.r + (colors.mid.r - colors.low.r) * ratio),
g: Math.round(colors.low.g + (colors.mid.g - colors.low.g) * ratio),
b: Math.round(colors.low.b + (colors.mid.b - colors.low.b) * ratio)
};
}
// Calculate opacity based on percentage (0.15 to 0.5)
const opacity = 0.15 + (percentage / 100) * 0.35;
// Return rgba color
return `rgba(${finalColor.r}, ${finalColor.g}, ${finalColor.b}, ${opacity})`;
};
export default function Message({ message, onToggle }) {
const { role, completions, activeIndex = 0, content } = message;
const [hoveredToken, setHoveredToken] = useState(null);
const [mousePosition, setMousePosition] = useState({ x: 0, y: 0 });
const [isFlipping, setIsFlipping] = useState(false);
const hoverTimeoutRef = useRef(null);
const handleTokenMouseEnter = (token, index, event) => {
if (hoverTimeoutRef.current) {
clearTimeout(hoverTimeoutRef.current);
}
hoverTimeoutRef.current = setTimeout(() => {
setHoveredToken({ token, index });
setMousePosition({ x: event.clientX, y: event.clientY });
}, 100);
};
const handleTokenMouseLeave = () => {
if (hoverTimeoutRef.current) {
clearTimeout(hoverTimeoutRef.current);
}
setHoveredToken(null);
};
const handleToggle = () => {
setIsFlipping(true);
onToggle();
// Reset flip state after animation completes
setTimeout(() => setIsFlipping(false), 300);
};
const renderContent = () => {
// Handle non-assistant messages or messages without completions
if (!completions || role !== 'assistant') {
return <div className="message-text">{content}</div>;
}
// Add safety checks for completions array
if (!Array.isArray(completions) || completions.length === 0) {
return <div className="message-text">{content || 'No response available'}</div>;
}
// Ensure activeIndex is within bounds
const safeIndex = Math.min(Math.max(activeIndex, 0), completions.length - 1);
const activeCompletion = completions[safeIndex];
if (!activeCompletion) {
return <div className="message-text">{content || 'No response available'}</div>;
}
const { text, tokenProbabilities } = activeCompletion;
if (!tokenProbabilities || tokenProbabilities.length === 0) {
return <div className="message-text">{text || content || 'No response available'}</div>;
}
return (
<div className="message-text">
{tokenProbabilities.map((tp, idx) => {
const backgroundColor = getBackgroundColor(tp);
return (
<span
key={idx}
className="token"
style={{ backgroundColor }}
onMouseEnter={(e) => handleTokenMouseEnter(tp.token, idx, e)}
onMouseLeave={handleTokenMouseLeave}
>
{tp.token}
</span>
);
})}
</div>
);
};
return (
<div className={`message ${role}-message ${isFlipping ? 'flipping' : ''}`}>
<div className="message-inner">
<div className="message-front">
<div className="message-header">
{completions && completions.length > 1 && (
<button
onClick={handleToggle}
className="toggle-completion-button"
title="Show alternative response"
>
<span className="toggle-icon">↻</span>
<span className="completion-counter">{activeIndex + 1}/{completions.length}</span>
</button>
)}
</div>
{renderContent()}
{message.timestamp && (
<div className="message-timestamp">
{new Date(message.timestamp).toLocaleTimeString()}
</div>
)}
</div>
</div>
{hoveredToken && completions && (
<TokenProbabilities
probabilities={completions[activeIndex]?.tokenProbabilities[hoveredToken.index]?.top_logprobs || {}}
position={mousePosition}
selectedToken={hoveredToken.token}
onMouseEnter={() => clearTimeout(hoverTimeoutRef.current)}
onMouseLeave={handleTokenMouseLeave}
/>
)}
</div>
);
}