bolt.diy/app/components/chat/AssistantMessage.tsx
Brian Hackett c3e1764da3
Merge pull request #1 from replayio/recording-button
Add button to save recording, assorted other UX changes
2025-01-07 05:56:18 -10:00

38 lines
1.2 KiB
TypeScript

import { memo } from 'react';
import { Markdown } from './Markdown';
import type { JSONValue } from 'ai';
interface AssistantMessageProps {
content: string;
annotations?: JSONValue[];
}
export function getAnnotationsTokensUsage(annotations: JSONValue[] | undefined) {
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;
} = filteredAnnotations.find((annotation) => annotation.type === 'usage')?.value;
return usage;
}
export const AssistantMessage = memo(({ content, annotations }: AssistantMessageProps) => {
const usage = getAnnotationsTokensUsage(annotations);
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})
</div>
)}
<Markdown html>{content}</Markdown>
</div>
);
});