File size: 1,375 Bytes
2e1ab99 9dcdd49 2e1ab99 9dcdd49 2e1ab99 |
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 |
import { memo } from 'react';
import { Markdown } from './Markdown';
import type { JSONValue } from 'ai';
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 }[];
const usage: {
completionTokens: number;
promptTokens: number;
totalTokens: number;
isCacheHit?: boolean;
isCacheMiss?: boolean;
} = filteredAnnotations.find((annotation) => annotation.type === 'usage')?.value ?? undefined;
const cacheHitMsg = usage?.isCacheHit ? ' [Cache Hit]' : '';
const cacheMissMsg = usage?.isCacheMiss ? ' [Cache Miss]' : '';
return (
<div className="overflow-hidden w-full">
{usage && (
<div className="text-sm text-bolt-elements-textSecondary mb-2">
Tokens: {usage.totalTokens} (prompt: {usage.promptTokens}, completion: {usage.completionTokens})
<span className="text-sm text-green-500 ml-1">{cacheHitMsg}</span>
<span className="text-sm text-red-500 ml-1">{cacheMissMsg}</span>
</div>
)}
<Markdown html>{content}</Markdown>
</div>
);
});
|