mirror of https://github.com/buster-so/buster.git
Update auto append messages
This commit is contained in:
parent
6c53cf5cb0
commit
ee6f3f4e50
|
@ -1,64 +1,60 @@
|
||||||
import type {
|
|
||||||
BusterChatMessageReasoning,
|
|
||||||
BusterChatMessageReasoning_pills
|
|
||||||
} from '@/api/asset_interfaces';
|
|
||||||
import { useMemoizedFn } from 'ahooks';
|
import { useMemoizedFn } from 'ahooks';
|
||||||
import sample from 'lodash/sample';
|
import sample from 'lodash/sample';
|
||||||
import { useBusterChatContextSelector } from '../ChatProvider';
|
import { useBusterChatContextSelector } from '../ChatProvider';
|
||||||
import random from 'lodash/random';
|
import random from 'lodash/random';
|
||||||
import last from 'lodash/last';
|
import last from 'lodash/last';
|
||||||
import { timeout } from '@/lib/timeout';
|
import { useRef, useState } from 'react';
|
||||||
import { useState } from 'react';
|
import { IBusterChatMessage } from '../interfaces';
|
||||||
|
import { ChatEvent_GeneratingReasoningMessage } from '@/api/buster_socket/chats';
|
||||||
|
|
||||||
export const useBlackBoxMessage = () => {
|
export const useBlackBoxMessage = () => {
|
||||||
const [boxBoxMessages, setBoxBoxMessages] = useState<Record<string, string>>({});
|
const [boxBoxMessages, setBoxBoxMessages] = useState<Record<string, string | null>>({});
|
||||||
|
const timeoutRef = useRef<Record<string, NodeJS.Timeout>>({});
|
||||||
const onUpdateChatMessage = useBusterChatContextSelector((x) => x.onUpdateChatMessage);
|
|
||||||
const getChatMessageMemoized = useBusterChatContextSelector((x) => x.getChatMessageMemoized);
|
const getChatMessageMemoized = useBusterChatContextSelector((x) => x.getChatMessageMemoized);
|
||||||
|
|
||||||
const removeAutoThoughts = useMemoizedFn(() => {
|
const removeAutoThought = useMemoizedFn(({ messageId }: { messageId: string }) => {
|
||||||
// return reasoningMessages.filter((rm) => rm.id !== AUTO_THOUGHT_ID);
|
if (timeoutRef.current[messageId]) {
|
||||||
|
clearTimeout(timeoutRef.current[messageId]);
|
||||||
|
delete timeoutRef.current[messageId];
|
||||||
|
}
|
||||||
|
setBoxBoxMessages((x) => ({ ...x, [messageId]: null }));
|
||||||
});
|
});
|
||||||
|
|
||||||
const autoAppendThought = useMemoizedFn(({ messageId }: { messageId: string }) => {
|
const addAutoThought = useMemoizedFn(({ messageId }: { messageId: string }) => {
|
||||||
//
|
const randomThought = getRandomThought();
|
||||||
// const lastReasoningMessage = reasoningMessages[reasoningMessages.length - 1];
|
setBoxBoxMessages((x) => ({ ...x, [messageId]: randomThought }));
|
||||||
// const lastMessageIsCompleted =
|
|
||||||
// !lastReasoningMessage || lastReasoningMessage?.status === 'completed';
|
|
||||||
|
|
||||||
// if (lastMessageIsCompleted) {
|
|
||||||
// _loopAutoThought(chatId);
|
|
||||||
|
|
||||||
// return [...reasoningMessages, createAutoThought()];
|
|
||||||
// }
|
|
||||||
|
|
||||||
return removeAutoThoughts();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const _loopAutoThought = useMemoizedFn(async (chatId: string) => {
|
const checkAutoThought = useMemoizedFn(
|
||||||
|
(message: IBusterChatMessage, event: ChatEvent_GeneratingReasoningMessage) => {
|
||||||
|
const isFinishedReasoningMessage = event.progress === 'completed';
|
||||||
|
if (isFinishedReasoningMessage) {
|
||||||
|
addAutoThought({ messageId: message.id });
|
||||||
|
_loopAutoThought({ messageId: message.id });
|
||||||
|
} else {
|
||||||
|
removeAutoThought({ messageId: message.id });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
const _loopAutoThought = useMemoizedFn(async ({ messageId }: { messageId: string }) => {
|
||||||
const randomDelay = random(3000, 5000);
|
const randomDelay = random(3000, 5000);
|
||||||
await timeout(randomDelay);
|
timeoutRef.current[messageId] = setTimeout(() => {
|
||||||
// const chatMessages = getChatMessagesMemoized(chatId);
|
const message = getChatMessageMemoized(messageId);
|
||||||
// const lastMessage = last(chatMessages);
|
if (!message) return;
|
||||||
// const isCompletedStream = !!lastMessage?.isCompletedStream;
|
const isMessageCompletedStream = !!message?.isCompletedStream;
|
||||||
// const lastReasoningMessageId = last(lastMessage?.reasoning_message_ids) || '';
|
const lastReasoningMessageId = last(message?.reasoning_message_ids) || '';
|
||||||
// const lastReasoningMessage = lastMessage?.reasoning_messages[lastReasoningMessageId];
|
const lastReasoningMessage = message?.reasoning_messages[lastReasoningMessageId];
|
||||||
// const lastReasoningMessageIsAutoAppended =
|
const isLastReasoningMessageCompleted = lastReasoningMessage?.status === 'completed';
|
||||||
// !lastReasoningMessage || lastReasoningMessage?.id === AUTO_THOUGHT_ID;
|
|
||||||
// if (!isCompletedStream && lastReasoningMessageIsAutoAppended && lastMessage) {
|
if (!isMessageCompletedStream && isLastReasoningMessageCompleted) {
|
||||||
// const lastMessageId = lastMessage?.id!;
|
addAutoThought({ messageId });
|
||||||
// const lastReasoningMessageIndex = lastMessage?.reasoning.length - 1;
|
_loopAutoThought({ messageId });
|
||||||
// const updatedReasoning = lastMessage?.reasoning.slice(0, lastReasoningMessageIndex);
|
}
|
||||||
// const newReasoningMessages = [...updatedReasoning, createAutoThought()];
|
}, randomDelay);
|
||||||
// onUpdateChatMessage({
|
|
||||||
// id: lastMessageId,
|
|
||||||
// reasoning: newReasoningMessages,
|
|
||||||
// isCompletedStream: false
|
|
||||||
// });
|
|
||||||
// _loopAutoThought(chatId);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return { autoAppendThought, removeAutoThoughts };
|
return { checkAutoThought, removeAutoThought };
|
||||||
};
|
};
|
||||||
|
|
||||||
const getRandomThought = (currentThought?: string): string => {
|
const getRandomThought = (currentThought?: string): string => {
|
||||||
|
|
|
@ -38,7 +38,7 @@ export const useChatStreamMessage = () => {
|
||||||
const chatRefMessages = useRef<Record<string, IBusterChatMessage>>({});
|
const chatRefMessages = useRef<Record<string, IBusterChatMessage>>({});
|
||||||
const [isPending, startTransition] = useTransition();
|
const [isPending, startTransition] = useTransition();
|
||||||
|
|
||||||
const { autoAppendThought } = useBlackBoxMessage();
|
const { checkAutoThought } = useBlackBoxMessage();
|
||||||
|
|
||||||
const onUpdateChatMessageTransition = useMemoizedFn(
|
const onUpdateChatMessageTransition = useMemoizedFn(
|
||||||
(chatMessage: Parameters<typeof onUpdateChatMessage>[0]) => {
|
(chatMessage: Parameters<typeof onUpdateChatMessage>[0]) => {
|
||||||
|
@ -135,6 +135,10 @@ export const useChatStreamMessage = () => {
|
||||||
const currentMessage = chatRefMessages.current[message_id];
|
const currentMessage = chatRefMessages.current[message_id];
|
||||||
const updatedMessage = updateReasoningMessage(message_id, currentMessage, reasoning);
|
const updatedMessage = updateReasoningMessage(message_id, currentMessage, reasoning);
|
||||||
|
|
||||||
|
checkAutoThought(updatedMessage, d);
|
||||||
|
|
||||||
|
//TRIGGER
|
||||||
|
|
||||||
onUpdateChatMessageTransition({
|
onUpdateChatMessageTransition({
|
||||||
id: message_id,
|
id: message_id,
|
||||||
reasoning_messages: updatedMessage?.reasoning_messages,
|
reasoning_messages: updatedMessage?.reasoning_messages,
|
||||||
|
|
Loading…
Reference in New Issue