From 46887d1d9f8552c1190ee82fee6e20dbfbb5aa73 Mon Sep 17 00:00:00 2001 From: Enrico Ros Date: Tue, 25 Jun 2024 01:28:57 -0700 Subject: [PATCH] MP: reuse more fragment functions --- .../message/fragments-content/ContentFragments.tsx | 6 +++--- src/apps/chat/editors/_handleExecute.ts | 6 +++--- src/apps/chat/editors/_handleExecuteCommand.ts | 4 ++-- src/common/chats/ConversationHandler.ts | 6 +++++- src/common/stores/chat/chat.fragments.ts | 12 ++++++------ src/common/stores/chat/chat.message.ts | 6 +++--- 6 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/apps/chat/components/message/fragments-content/ContentFragments.tsx b/src/apps/chat/components/message/fragments-content/ContentFragments.tsx index ef7f5d1e5..76a7fe015 100644 --- a/src/apps/chat/components/message/fragments-content/ContentFragments.tsx +++ b/src/apps/chat/components/message/fragments-content/ContentFragments.tsx @@ -5,7 +5,7 @@ import { Box } from '@mui/joy'; import type { ContentScaling } from '~/common/app.theme'; import type { DMessageRole } from '~/common/stores/chat/chat.message'; -import { DMessageContentFragment, DMessageFragment, DMessageFragmentId, isContentFragment } from '~/common/stores/chat/chat.fragments'; +import { DMessageContentFragment, DMessageFragment, DMessageFragmentId, isContentFragment, isTextPart } from '~/common/stores/chat/chat.fragments'; import type { ChatMessageTextPartEditState } from '../ChatMessage'; import { ContentPartImageRef } from './ContentPartImageRef'; @@ -78,11 +78,11 @@ export function ContentFragments(props: { return null; // editing for text parts - if (props.textEditsState && (fragment.part.pt === 'text' || fragment.part.pt === 'error')) { + if (props.textEditsState && (isTextPart(fragment.part) || fragment.part.pt === 'error')) { return ( | ((message: DMessage) => Partial), messageComplete: boolean, touch: boolean) { this.chatActions.editMessage(this.conversationId, messageId, update, messageComplete, touch); } @@ -156,7 +160,7 @@ export class ConversationHandler { newMessage.originLLM = llmId; newMessage.purposeId = getConversationSystemPurposeId(this.conversationId) ?? undefined; // TODO: put the other rays in the metadata?! (reqby @Techfren) - this.replaceMessages([...viewHistory, newMessage]); + this.messageAppend(newMessage); } // close beam diff --git a/src/common/stores/chat/chat.fragments.ts b/src/common/stores/chat/chat.fragments.ts index b16350be8..e2badb999 100644 --- a/src/common/stores/chat/chat.fragments.ts +++ b/src/common/stores/chat/chat.fragments.ts @@ -130,6 +130,10 @@ export function isContentOrAttachmentFragment(fragment: DMessageFragment) { return fragment.ft === 'content' || fragment.ft === 'attachment'; } +export function isTextPart(part: DMessageContentFragment['part']) { + return part.pt === 'text'; +} + export function isImageRefPart(part: DMessageContentFragment['part'] | DMessageAttachmentFragment['part']) { return part.pt === 'image_ref'; } @@ -175,17 +179,13 @@ export function createImageAttachmentFragment(title: string, caption: string, da } export function specialContentPartToDocAttachmentFragment(title: string, caption: string, contentPart: DMessageContentFragment['part'], ref: string, docMeta?: DMessageDocMeta): DMessageAttachmentFragment { - if (contentPart.pt === 'text') + if (isTextPart(contentPart)) return createDocAttachmentFragment(title, caption, 'text/plain', createDMessageDataInlineText(contentPart.text), ref, docMeta); - if (contentPart.pt === 'image_ref') + if (isImageRefPart(contentPart)) return createImageAttachmentFragment(title, caption, _duplicateDataReference(contentPart.dataRef), contentPart.altText, contentPart.width, contentPart.height); return createDocAttachmentFragment('Error', 'Content to Attachment', 'text/plain', createDMessageDataInlineText(`Conversion of '${contentPart.pt}' is not supported yet.`), ref, docMeta); } -export function specialShallowReplaceDocData(part: DMessageDocPart, newData: DMessageDataInline): DMessageDocPart { - return createDMessageDocPart(part.type, newData, part.ref, part.meta); -} - function _createSentinelFragment(): _DMessageSentinelFragment { return { ft: '_ft_sentinel', fId: agiId('chat-dfragment' /* -_sentinel */) }; diff --git a/src/common/stores/chat/chat.message.ts b/src/common/stores/chat/chat.message.ts index 94140b0a0..879152317 100644 --- a/src/common/stores/chat/chat.message.ts +++ b/src/common/stores/chat/chat.message.ts @@ -1,5 +1,5 @@ import { agiUuid } from '~/common/util/idUtils'; -import { createPlaceholderContentFragment, createTextContentFragment, DMessageContentFragment, DMessageFragment, DMessageFragmentId, duplicateDMessageFragments, isAttachmentFragment, isContentFragment, isContentOrAttachmentFragment, specialShallowReplaceTextContentFragment } from '~/common/stores/chat/chat.fragments'; +import { createPlaceholderContentFragment, createTextContentFragment, DMessageContentFragment, DMessageFragment, DMessageFragmentId, duplicateDMessageFragments, isAttachmentFragment, isContentFragment, isContentOrAttachmentFragment, isTextPart, specialShallowReplaceTextContentFragment } from '~/common/stores/chat/chat.fragments'; // Message @@ -180,14 +180,14 @@ export function messageFragmentsReduceText(fragments: DMessageFragment[], fragme export function messageFragmentsReplaceLastContentText(fragments: Readonly, newText: string, appendText?: boolean): DMessageFragment[] { // if there's no text fragment, create it - const lastTextFragment = fragments.findLast(f => isContentFragment(f) && f.part.pt === 'text') as DMessageContentFragment | undefined; + const lastTextFragment = fragments.findLast(f => isContentFragment(f) && isTextPart(f.part)) as DMessageContentFragment | undefined; if (!lastTextFragment) return [...fragments, createTextContentFragment(newText)]; // append/replace the last text fragment return fragments.map(fragment => (fragment === lastTextFragment) - ? specialShallowReplaceTextContentFragment(lastTextFragment, (appendText && lastTextFragment.part.pt === 'text') ? lastTextFragment.part.text + newText : newText) + ? specialShallowReplaceTextContentFragment(lastTextFragment, (appendText && isTextPart(lastTextFragment.part)) ? lastTextFragment.part.text + newText : newText) : fragment, ); }