mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-30 23:28:52 +01:00
* fix(processMessages): properly handle assistant file citations and add sources list * feat: improve file download UX by making any downloaded files accessible within the app post-download * refactor(processOpenAIImageOutput): correctly handle two different outputs for images since OpenAI generates a file in their storage, shares filepath for image rendering * refactor: create `addFileToCache` helper to use across frontend * refactor: add ImageFile parts to cache on processing content stream
83 lines
2.6 KiB
TypeScript
83 lines
2.6 KiB
TypeScript
import { useCallback, useMemo } from 'react';
|
|
import { ContentTypes } from 'librechat-data-provider';
|
|
import { useQueryClient } from '@tanstack/react-query';
|
|
|
|
import type {
|
|
Text,
|
|
TMessage,
|
|
ImageFile,
|
|
TSubmission,
|
|
ContentPart,
|
|
PartMetadata,
|
|
TContentData,
|
|
TMessageContentParts,
|
|
} from 'librechat-data-provider';
|
|
import { addFileToCache } from '~/utils';
|
|
|
|
type TUseContentHandler = {
|
|
setMessages: (messages: TMessage[]) => void;
|
|
getMessages: () => TMessage[] | undefined;
|
|
};
|
|
|
|
type TContentHandler = {
|
|
data: TContentData;
|
|
submission: TSubmission;
|
|
};
|
|
|
|
export default function useContentHandler({ setMessages, getMessages }: TUseContentHandler) {
|
|
const queryClient = useQueryClient();
|
|
const messageMap = useMemo(() => new Map<string, TMessage>(), []);
|
|
return useCallback(
|
|
({ data, submission }: TContentHandler) => {
|
|
const { type, messageId, thread_id, conversationId, index } = data;
|
|
|
|
const _messages = getMessages();
|
|
const messages =
|
|
_messages
|
|
?.filter((m) => m.messageId !== messageId)
|
|
?.map((msg) => ({ ...msg, thread_id })) ?? [];
|
|
const userMessage = messages[messages.length - 1];
|
|
|
|
const { initialResponse } = submission;
|
|
|
|
let response = messageMap.get(messageId);
|
|
if (!response) {
|
|
response = {
|
|
...initialResponse,
|
|
parentMessageId: userMessage?.messageId,
|
|
conversationId,
|
|
messageId,
|
|
thread_id,
|
|
};
|
|
messageMap.set(messageId, response);
|
|
}
|
|
|
|
// TODO: handle streaming for non-text
|
|
const textPart: Text | string | undefined = data[ContentTypes.TEXT];
|
|
const part: ContentPart =
|
|
textPart && typeof textPart === 'string' ? { value: textPart } : data[type];
|
|
|
|
if (type === ContentTypes.IMAGE_FILE) {
|
|
addFileToCache(queryClient, part as ImageFile & PartMetadata);
|
|
}
|
|
|
|
/* spreading the content array to avoid mutation */
|
|
response.content = [...(response.content ?? [])];
|
|
|
|
response.content[index] = { type, [type]: part } as TMessageContentParts;
|
|
|
|
if (
|
|
type !== ContentTypes.TEXT &&
|
|
initialResponse.content &&
|
|
((response.content[response.content.length - 1].type === ContentTypes.TOOL_CALL &&
|
|
response.content[response.content.length - 1][ContentTypes.TOOL_CALL].progress === 1) ||
|
|
response.content[response.content.length - 1].type === ContentTypes.IMAGE_FILE)
|
|
) {
|
|
response.content.push(initialResponse.content[0]);
|
|
}
|
|
|
|
setMessages([...messages, response]);
|
|
},
|
|
[queryClient, getMessages, messageMap, setMessages],
|
|
);
|
|
}
|