feat: update message handling to use 'finish' property for step completion logic

This commit is contained in:
Bohdan Triapitsyn
2025-12-27 15:51:28 +02:00
parent 88e0cedcd3
commit 4d7340bf49
11 changed files with 60 additions and 134 deletions
@@ -287,6 +287,11 @@ const ChatMessage: React.FC<ChatMessageProps> = ({
return Boolean(messageCompletedAt && messageCompletedAt > 0);
}, [isUser, messageCompletedAt]);
const messageFinish = React.useMemo(() => {
const finish = (message.info as { finish?: string }).finish;
return typeof finish === 'string' ? finish : undefined;
}, [message.info]);
const visibleParts = React.useMemo(
() =>
filterVisibleParts(normalizedParts, {
@@ -382,23 +387,8 @@ const ChatMessage: React.FC<ChatMessageProps> = ({
return { name, token: rawValue } satisfies AgentMentionInfo;
}, [isUser, message.parts]);
const stepState = React.useMemo(() => {
let stepStarts = 0;
let stepFinishes = 0;
visibleParts.forEach((part) => {
if (part.type === 'step-start') {
stepStarts += 1;
} else if (part.type === 'step-finish') {
stepFinishes += 1;
}
});
return {
hasOpenStep: stepStarts > stepFinishes,
};
}, [visibleParts]);
const hasOpenStep = stepState.hasOpenStep;
// Message is considered to have an "open step" if info.finish is not yet present
const hasOpenStep = typeof messageFinish !== 'string';
const shouldCoordinateRendering = React.useMemo(() => {
if (isUser) {
@@ -756,6 +746,7 @@ const ChatMessage: React.FC<ChatMessageProps> = ({
parts={visibleParts}
isUser={isUser}
isMessageCompleted={isMessageCompleted}
messageFinish={messageFinish}
syntaxTheme={syntaxTheme}
isMobile={isMobile}
hasTouchInput={hasTouchInput}
@@ -797,6 +788,7 @@ const ChatMessage: React.FC<ChatMessageProps> = ({
parts={visibleParts}
isUser={isUser}
isMessageCompleted={isMessageCompleted}
messageFinish={messageFinish}
syntaxTheme={syntaxTheme}
isMobile={isMobile}
hasTouchInput={hasTouchInput}
@@ -108,22 +108,15 @@ export const detectTurns = (messages: ChatMessageEntry[]): Turn[] => {
const extractFinalAssistantText = (turn: Turn): string | undefined => {
for (const assistantMsg of turn.assistantMessages) {
const infoFinish = (assistantMsg.info as { finish?: string | null | undefined }).finish;
for (const part of assistantMsg.parts) {
if (part.type === 'step-finish') {
const finishReason = (part as { reason?: string | null | undefined }).reason;
const infoFinish = (assistantMsg.info as { finish?: string | null | undefined }).finish;
if (finishReason === 'stop' || infoFinish === 'stop') {
const textPart = assistantMsg.parts.find(p => p.type === 'text');
if (textPart) {
const textContent = (textPart as { text?: string | null | undefined }).text ??
(textPart as { content?: string | null | undefined }).content;
if (typeof textContent === 'string' && textContent.trim().length > 0) {
return textContent;
}
}
if (infoFinish === 'stop') {
const textPart = assistantMsg.parts.find(p => p.type === 'text');
if (textPart) {
const textContent = (textPart as { text?: string | null | undefined }).text ??
(textPart as { content?: string | null | undefined }).content;
if (typeof textContent === 'string' && textContent.trim().length > 0) {
return textContent;
}
}
}
@@ -194,12 +187,9 @@ const getTurnActivityInfo = (turn: Turn): TurnActivityInfo => {
turn.assistantMessages.forEach((msg) => {
const messageId = msg.info.id;
const infoFinish = (msg.info as { finish?: string | null | undefined }).finish;
const hasStopFinishInMessage = ENABLE_TEXT_JUSTIFICATION_ACTIVITY
? msg.parts.some((part) => {
if (part.type !== 'step-finish') return false;
const reason = (part as { reason?: string | null | undefined }).reason;
return reason === 'stop';
})
? infoFinish === 'stop'
: false;
msg.parts.forEach((part) => {
@@ -105,6 +105,7 @@ interface MessageBodyProps {
parts: Part[];
isUser: boolean;
isMessageCompleted: boolean;
messageFinish?: string;
syntaxTheme: { [key: string]: React.CSSProperties };
@@ -300,6 +301,7 @@ const AssistantMessageBody: React.FC<Omit<MessageBodyProps, 'isUser'>> = ({
messageId,
parts,
isMessageCompleted,
messageFinish,
syntaxTheme,
isMobile,
@@ -347,9 +349,7 @@ const AssistantMessageBody: React.FC<Omit<MessageBodyProps, 'isUser'>> = ({
const createSessionFromAssistantMessage = useSessionStore((state) => state.createSessionFromAssistantMessage);
const isLastAssistantInTurn = turnGroupingContext?.isLastAssistantInTurn ?? false;
const hasStopFinish = React.useMemo(() => {
return parts.some((part) => part.type === 'step-finish' && (part as { reason?: string | null | undefined }).reason === 'stop');
}, [parts]);
const hasStopFinish = messageFinish === 'stop';
const hasTools = toolParts.length > 0;
@@ -414,24 +414,8 @@ const AssistantMessageBody: React.FC<Omit<MessageBodyProps, 'isUser'>> = ({
});
}, [reasoningParts]);
const stepState = React.useMemo(() => {
let stepStarts = 0;
let stepFinishes = 0;
visibleParts.forEach((part) => {
if (part.type === 'step-start') {
stepStarts += 1;
} else if (part.type === 'step-finish') {
stepFinishes += 1;
}
});
return {
stepStarts,
stepFinishes,
hasOpenStep: stepStarts > stepFinishes,
};
}, [visibleParts]);
const hasOpenStep = stepState.hasOpenStep;
// Message is considered to have an "open step" if info.finish is not yet present
const hasOpenStep = typeof messageFinish !== 'string';
const shouldHoldForReasoning =
reasoningParts.length > 0 &&