File size: 12,242 Bytes
abcf568 | 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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 | import { memo, useCallback, useEffect, useRef } from 'react'
import { useI18n } from '../../../i18n'
import { stripStudioReferenceImages } from '../../reference-images'
import { debugStudioMessages } from '../../agent-response/debug'
import type { StudioMessage } from '../../protocol/studio-agent-types'
import { StudioMarkdown } from '../StudioMarkdown'
import { selectRowView } from './selectors'
import type { StudioCommandPanelStore } from './store'
import { useCommandStoreSelector } from './use-command-store-selector'
interface StudioCommandMessageRowProps {
messageId: string
store: StudioCommandPanelStore
variant?: 'default' | 't-layout-bottom' | 'pure-minimal-bottom'
}
const animatedMessageIds = new Set<string>()
export const StudioCommandMessageRow = memo(function StudioCommandMessageRow({
messageId,
store,
variant = 'default',
}: StudioCommandMessageRowProps) {
const selector = useCallback(
(snapshot: ReturnType<StudioCommandPanelStore['getSnapshot']>) => selectRowView(snapshot, messageId),
[messageId],
)
const rowView = useCommandStoreSelector(store, selector, areRowViewsEqual)
const renderCountRef = useRef(0)
const prevRowViewRef = useRef(rowView)
const shouldAnimateEnter = !animatedMessageIds.has(messageId)
renderCountRef.current += 1
useEffect(() => {
animatedMessageIds.add(messageId)
debugStudioMessages('command-row-mounted', {
messageId,
role: rowView.message?.role ?? 'missing',
shouldAnimateEnter,
})
return () => {
debugStudioMessages('command-row-unmounted', {
messageId,
})
}
}, [messageId, rowView.message?.role, shouldAnimateEnter])
useEffect(() => {
const previous = prevRowViewRef.current
debugStudioMessages('command-row-rendered', {
messageId,
renderCount: renderCountRef.current,
role: rowView.message?.role ?? 'missing',
changed: describeRowViewChange(previous, rowView),
isStreamingTarget: rowView.isStreamingTarget,
showCaret: rowView.showCaret,
streamedLength: rowView.streamedText.length,
})
prevRowViewRef.current = rowView
}, [messageId, rowView])
if (!rowView.message) {
return null
}
if (rowView.message.role === 'user') {
return (
<UserMessageItem
message={rowView.message}
shouldAnimateEnter={shouldAnimateEnter}
minimal={variant === 'pure-minimal-bottom'}
/>
)
}
return (
<AssistantMessageItem
message={rowView.message}
shouldAnimateEnter={shouldAnimateEnter}
isStreamingTarget={rowView.isStreamingTarget}
streamedText={rowView.streamedText}
showCaret={rowView.showCaret}
minimal={variant === 'pure-minimal-bottom'}
/>
)
})
const UserMessageItem = memo(function UserMessageItem({
message,
shouldAnimateEnter,
minimal,
}: {
message: Extract<StudioMessage, { role: 'user' }>
shouldAnimateEnter: boolean
minimal: boolean
}) {
const { t } = useI18n()
if (minimal) {
return (
<div className={`${shouldAnimateEnter ? 'animate-message-enter ' : ''}mb-1`}>
<div className="flex items-baseline gap-4">
<span className="block w-4 shrink-0 text-center text-[11px] font-semibold leading-loose text-accent/72">{'>'}</span>
<StudioMarkdown
content={stripStudioReferenceImages(message.text)}
className="studio-markdown-inline min-w-0 flex-1 text-[13px] leading-loose text-accent/80"
/>
</div>
</div>
)
}
return (
<div className={`${shouldAnimateEnter ? 'animate-message-enter ' : ''}group mb-6`}>
<div className="rounded-2xl bg-bg-secondary/30 px-6 py-5 transition-colors group-hover:bg-bg-secondary/50">
<div className="mb-3 flex items-center gap-3">
<span className="font-mono text-[9px] font-bold uppercase tracking-[0.3em] text-accent/40">{t('studio.inputUser')}</span>
<div className="h-px flex-1 bg-border/5" />
</div>
<StudioMarkdown
content={stripStudioReferenceImages(message.text)}
className="text-[14px] font-medium leading-7 text-text-primary/80"
/>
</div>
</div>
)
})
const AssistantMessageItem = memo(function AssistantMessageItem({
message,
shouldAnimateEnter,
isStreamingTarget,
streamedText,
showCaret,
minimal,
}: {
message: Extract<StudioMessage, { role: 'assistant' }>
shouldAnimateEnter: boolean
isStreamingTarget: boolean
streamedText: string
showCaret: boolean
minimal: boolean
}) {
const { t } = useI18n()
const textParts = message.parts.filter((part) => part.type === 'text' || part.type === 'reasoning')
const toolParts = message.parts.filter((part) => part.type === 'tool')
const hasStreamedText = streamedText.length > 0
const hasRenderableText = textParts.some((part) => part.text.trim())
if (minimal) {
return (
<div className={`${!isStreamingTarget && shouldAnimateEnter ? 'animate-message-enter ' : ''}mb-1`}>
<div className="flex items-baseline gap-4">
<span className="block w-4 shrink-0 text-center text-[10px] font-semibold leading-loose text-accent/54">{'•'}</span>
<div className="min-w-0 flex-1 space-y-2">
{isStreamingTarget && hasStreamedText ? (
<StudioMarkdown
content={streamedText}
className="studio-markdown-inline text-[13px] leading-loose text-accent/70"
showCaret={showCaret}
/>
) : isStreamingTarget && !hasRenderableText ? (
<div className="flex items-center gap-3">
<span className="text-[12px] uppercase tracking-[0.2em] text-accent/40">{t('studio.thinking')}</span>
<span className="studio-thinking-dots" aria-hidden="true">
<span />
<span />
<span />
</span>
</div>
) : textParts.map((part, i) => {
const text = part.text.trim()
if (!text) return null
return (
<StudioMarkdown
key={`text-${i}`}
content={text}
className="studio-markdown-inline text-[13px] leading-loose text-accent/70"
/>
)
})}
{!isStreamingTarget && !hasRenderableText && (
<div className="text-[12px] text-accent/30">
{t('studio.noResponseOutput')}
</div>
)}
{toolParts.length > 0 && (
<div className="space-y-1 pt-1">
{toolParts.map((part, i) => {
const status = part.state.status === 'error' ? '!' : part.state.status === 'completed' ? '->' : '...'
const args = 'input' in part.state ? truncateArgs(part.state.input) : ''
return (
<div key={i} className="space-y-1">
<div className={`flex items-center gap-3 font-mono text-[10px] tracking-tight ${neutralToolTone(part.state.status)}`}>
<span className="w-4 shrink-0 text-center opacity-75">{status}</span>
<span className="uppercase tracking-[0.18em] opacity-72">{part.tool}</span>
<span className="truncate opacity-28">({args})</span>
</div>
{part.state.status === 'error' && (
<div className="pl-7 text-[11px] leading-5 text-rose-600/85 dark:text-rose-300/85 break-words">
{part.state.error}
</div>
)}
</div>
)
})}
</div>
)}
</div>
</div>
</div>
)
}
return (
<div className={`${!isStreamingTarget && shouldAnimateEnter ? 'animate-message-enter ' : ''}group mb-6`}>
<div className="rounded-2xl bg-bg-tertiary/40 px-6 py-6 transition-colors group-hover:bg-bg-tertiary/60">
<div className="mb-4 flex items-center gap-3">
<span className="font-mono text-[9px] font-bold uppercase tracking-[0.3em] text-text-primary/45">{t('studio.outputAgent')}</span>
<div className="h-px flex-1 bg-border/10" />
</div>
<div className="space-y-6">
{isStreamingTarget && hasStreamedText ? (
<StudioMarkdown
content={streamedText}
className="text-[15px] font-medium leading-8 text-text-primary/90"
showCaret={showCaret}
/>
) : isStreamingTarget && !hasRenderableText ? (
<div className="ml-1 flex items-center gap-4 border-l border-accent/10 pl-1">
<span className="text-[13px] font-mono tracking-widest text-text-secondary/40">{t('studio.thinking')}</span>
<span className="studio-thinking-dots" aria-hidden="true">
<span />
<span />
<span />
</span>
</div>
) : textParts.map((part, i) => {
const text = part.text.trim()
if (!text) return null
return (
<StudioMarkdown
key={`text-${i}`}
content={text}
className="text-[15px] font-medium leading-8 text-text-primary/90"
/>
)
})}
{!isStreamingTarget && !hasRenderableText && (
<div className="text-[13px] text-text-secondary/30">
{t('studio.noResponseOutput')}
</div>
)}
{toolParts.length > 0 && (
<div className="space-y-2.5 border-t border-border/10 pt-4">
{toolParts.map((part, i) => {
const status = part.state.status === 'error' ? '!' : part.state.status === 'completed' ? '->' : '...'
const args = 'input' in part.state ? truncateArgs(part.state.input) : ''
return (
<div key={i} className="space-y-1.5">
<div className={`font-mono text-[10px] tracking-tight ${neutralToolTone(part.state.status)} flex items-center gap-3`}>
<span className="flex h-4 w-4 items-center justify-center bg-text-primary/5 font-bold">{status}</span>
<span className="font-bold uppercase tracking-wider">{part.tool}</span>
<span className="truncate opacity-30">({args})</span>
</div>
{part.state.status === 'error' && (
<div className="pl-7 text-[12px] leading-6 text-rose-600/85 dark:text-rose-300/85 break-words">
{part.state.error}
</div>
)}
</div>
)
})}
</div>
)}
</div>
</div>
</div>
)
})
function neutralToolTone(status: string) {
switch (status) {
case 'error':
return 'text-rose-500/70'
case 'completed':
return 'text-text-primary/40'
default:
return 'text-amber-500/70'
}
}
function truncateArgs(input?: Record<string, unknown>) {
if (!input) return ''
const str = JSON.stringify(input)
return str.length > 60 ? `${str.slice(0, 57)}...` : str
}
function areRowViewsEqual(
left: ReturnType<typeof selectRowView>,
right: ReturnType<typeof selectRowView>,
) {
return left.message === right.message
&& left.isStreamingTarget === right.isStreamingTarget
&& left.streamedText === right.streamedText
&& left.showCaret === right.showCaret
}
function describeRowViewChange(
previous: ReturnType<typeof selectRowView>,
next: ReturnType<typeof selectRowView>,
) {
const reasons: string[] = []
if (previous.message !== next.message) {
reasons.push('message-ref')
}
if (previous.isStreamingTarget !== next.isStreamingTarget) {
reasons.push('stream-target')
}
if (previous.streamedText !== next.streamedText) {
reasons.push('stream-text')
}
if (previous.showCaret !== next.showCaret) {
reasons.push('caret')
}
return reasons.length > 0 ? reasons : ['no-diff']
}
|