update type safety on message

This commit is contained in:
Nate Kelley 2025-03-04 10:19:59 -07:00
parent 24eb020566
commit dcf725684c
No known key found for this signature in database
GPG Key ID: FD90372AB8D98B4F
2 changed files with 26 additions and 15 deletions

View File

@ -1,6 +1,10 @@
import { useMemoizedFn } from 'ahooks'; import { useMemoizedFn } from 'ahooks';
import { useBusterChatContextSelector } from '../ChatProvider'; import { useBusterChatContextSelector } from '../ChatProvider';
import { BusterChat, BusterChatMessage_text } from '@/api/asset_interfaces'; import {
BusterChat,
BusterChatMessage_text,
BusterChatMessageReasoning
} from '@/api/asset_interfaces';
import { import {
ChatEvent_GeneratingReasoningMessage, ChatEvent_GeneratingReasoningMessage,
ChatEvent_GeneratingResponseMessage, ChatEvent_GeneratingResponseMessage,
@ -27,14 +31,14 @@ type ChatMessageResponseMessagesRef = Record<
//message_id //message_id
string, string,
//response_message_id //response_message_id
Record<string, ChatMessageResponseMessage> Record<string, ChatMessageResponseMessage> | undefined
>; >;
type ChatMessageReasoningMessageRef = Record< type ChatMessageReasoningMessageRef = Record<
//message_id //message_id
string, string,
//reasoning_message_id //reasoning_message_id
Record<string, ChatMessageReasoning> Record<string, ChatMessageReasoning> | undefined
>; >;
export const useChatStreamMessage = () => { export const useChatStreamMessage = () => {
@ -134,7 +138,7 @@ export const useChatStreamMessage = () => {
const { message_id, response_message, chat_id } = d; const { message_id, response_message, chat_id } = d;
const responseMessageId = response_message.id; const responseMessageId = response_message.id;
const foundResponseMessage: undefined | ChatMessageResponseMessage = const foundResponseMessage: undefined | ChatMessageResponseMessage =
chatMessageResponseMessagesRef.current[message_id][responseMessageId]; chatMessageResponseMessagesRef.current[message_id]?.[responseMessageId];
const isNewMessage = !foundResponseMessage; const isNewMessage = !foundResponseMessage;
const currentResponseMessages = chatMessagesRef.current[message_id]?.response_messages ?? []; const currentResponseMessages = chatMessagesRef.current[message_id]?.response_messages ?? [];
@ -147,13 +151,16 @@ export const useChatStreamMessage = () => {
} }
if (isNewMessage) { if (isNewMessage) {
chatMessageResponseMessagesRef.current[message_id][responseMessageId] = { chatMessageResponseMessagesRef.current[message_id] = {
...response_message, ...(chatMessageResponseMessagesRef.current[message_id] || {}),
index: currentResponseMessages.length [responseMessageId]: {
...response_message,
index: currentResponseMessages.length
}
}; };
} }
const messageToUse = chatMessageResponseMessagesRef.current[message_id][responseMessageId]; const messageToUse = chatMessageResponseMessagesRef.current[message_id]?.[responseMessageId]!;
onUpdateChatMessageTransition({ onUpdateChatMessageTransition({
id: message_id, id: message_id,
@ -173,20 +180,24 @@ export const useChatStreamMessage = () => {
const { message_id, reasoning, chat_id } = d; const { message_id, reasoning, chat_id } = d;
const reasoningMessageId = reasoning.id; const reasoningMessageId = reasoning.id;
const foundReasoningMessage: undefined | ChatMessageReasoning = const foundReasoningMessage: undefined | ChatMessageReasoning =
chatMessageReasoningMessageRef.current[message_id][reasoningMessageId]; chatMessageReasoningMessageRef.current[message_id]?.[reasoningMessageId];
const isNewMessage = !foundReasoningMessage; const isNewMessage = !foundReasoningMessage;
const currentReasoning = chatMessagesRef.current[message_id]?.reasoning ?? []; const currentReasoning = chatMessagesRef.current[message_id]?.reasoning ?? [];
if (isNewMessage) { if (isNewMessage) {
chatMessageReasoningMessageRef.current[message_id][reasoningMessageId] = { chatMessageReasoningMessageRef.current[message_id] = {
...reasoning, ...chatMessageReasoningMessageRef.current[message_id],
index: currentReasoning.length [reasoningMessageId]: {
...reasoning,
index: currentReasoning.length
}
}; };
} }
const messageToUse = chatMessageReasoningMessageRef.current[message_id][reasoningMessageId]; const messageToUse =
chatMessageReasoningMessageRef.current[message_id]?.[reasoningMessageId]!;
const updatedReasoning = isNewMessage const updatedReasoning: BusterChatMessageReasoning[] = isNewMessage
? [...currentReasoning, messageToUse] ? [...currentReasoning, messageToUse]
: [ : [
...currentReasoning.slice(0, foundReasoningMessage.index), ...currentReasoning.slice(0, foundReasoningMessage.index),

View File

@ -27,7 +27,7 @@ export const NewChatInput: React.FC<{}> = () => {
try { try {
setLoading(true); setLoading(true);
await onStartNewChat({ prompt: value }); await onStartNewChat({ prompt: value });
} finally { } catch (error) {
setLoading(false); setLoading(false);
} }
}); });