bolt.diy/app/components/chat/AssistantMessage.tsx

46 lines
1.6 KiB
TypeScript
Raw Normal View History

2024-07-10 16:44:39 +00:00
import { memo } from 'react';
import { Markdown } from './Markdown';
2024-12-16 14:17:18 +00:00
import type { JSONValue } from 'ai';
import type { ProgressAnnotation } from '~/types/context';
import Popover from '~/components/ui/Popover';
2024-07-10 16:44:39 +00:00
interface AssistantMessageProps {
content: string;
2024-12-16 14:17:18 +00:00
annotations?: JSONValue[];
2024-07-10 16:44:39 +00:00
}
2024-12-16 14:17:18 +00:00
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);
2024-12-16 14:17:18 +00:00
const usage: {
completionTokens: number;
promptTokens: number;
totalTokens: number;
} = filteredAnnotations.find((annotation) => annotation.type === 'usage')?.value;
2024-12-16 09:01:41 +00:00
2024-07-10 16:44:39 +00:00
return (
<div className="overflow-hidden w-full">
<>
<div className=" flex gap-2 items-center text-sm text-bolt-elements-textSecondary mb-2">
{progressAnnotation.length > 0 && (
<Popover trigger={<div className="i-ph:info" />}>{progressAnnotation[0].message}</Popover>
)}
{usage && (
<div>
Tokens: {usage.totalTokens} (prompt: {usage.promptTokens}, completion: {usage.completionTokens})
</div>
)}
2024-12-16 09:01:41 +00:00
</div>
</>
2024-12-16 14:17:18 +00:00
<Markdown html>{content}</Markdown>
2024-07-10 16:44:39 +00:00
</div>
);
});