import { memo } from 'react'; import { Markdown } from './Markdown'; import type { JSONValue } from 'ai'; import type { ProgressAnnotation } from '~/types/context'; import Popover from '~/components/ui/Popover'; interface AssistantMessageProps { content: string; annotations?: JSONValue[]; } export const AssistantMessage = memo(({ content, annotations }: AssistantMessageProps) => { const filteredAnnotations = (annotations?.filter( (annotation: JSONValue) => annotation && typeof annotation === 'object' && Object.keys(annotation).includes('type'), ) || []) as { type: string; value: any } & { [key: string]: any }[]; let progressAnnotation: ProgressAnnotation[] = filteredAnnotations.filter( (annotation) => annotation.type === 'progress', ) as ProgressAnnotation[]; progressAnnotation = progressAnnotation.sort((a, b) => b.value - a.value); const usage: { completionTokens: number; promptTokens: number; totalTokens: number; } = filteredAnnotations.find((annotation) => annotation.type === 'usage')?.value; return (
<>
{progressAnnotation.length > 0 && ( }>{progressAnnotation[0].message} )} {usage && (
Tokens: {usage.totalTokens} (prompt: {usage.promptTokens}, completion: {usage.completionTokens})
)}
{content}
); });