mirror of
https://github.com/enricoros/big-AGI.git
synced 2026-05-10 21:50:14 -07:00
MP: reuse more fragment functions
This commit is contained in:
@@ -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 (
|
||||
<ContentPartTextEditor
|
||||
key={'edit-' + fragment.fId}
|
||||
textPartText={fragment.part.pt === 'text' ? fragment.part.text : fragment.part.error}
|
||||
textPartText={isTextPart(fragment.part) ? fragment.part.text : fragment.part.error}
|
||||
fragmentId={fragment.fId}
|
||||
contentScaling={props.contentScaling}
|
||||
editedText={props.textEditsState[fragment.fId]}
|
||||
|
||||
@@ -4,7 +4,7 @@ import { updateHistoryForReplyTo } from '~/modules/aifn/replyto/replyTo';
|
||||
import type { DConversationId } from '~/common/stores/chat/chat.conversation';
|
||||
import type { DMessage } from '~/common/stores/chat/chat.message';
|
||||
import { ConversationsManager } from '~/common/chats/ConversationsManager';
|
||||
import { createTextContentFragment } from '~/common/stores/chat/chat.fragments';
|
||||
import { createTextContentFragment, isContentFragment, isTextPart } from '~/common/stores/chat/chat.fragments';
|
||||
import { getConversationSystemPurposeId } from '~/common/stores/chat/store-chats';
|
||||
import { getUXLabsHighPerformance } from '~/common/state/store-ux-labs';
|
||||
|
||||
@@ -82,7 +82,7 @@ export async function _handleExecute(chatModeId: ChatModeId, conversationId: DCo
|
||||
|
||||
case 'generate-image':
|
||||
// verify we were called with a single DMessageTextContent
|
||||
if (firstFragment.ft !== 'content' || firstFragment.part.pt !== 'text')
|
||||
if (!isContentFragment(firstFragment) || !isTextPart(firstFragment.part))
|
||||
return false;
|
||||
const imagePrompt = firstFragment.part.text;
|
||||
cHandler.messageFragmentReplace(lastMessage.id, firstFragment.fId, createTextContentFragment(textToDrawCommand(imagePrompt)), true);
|
||||
@@ -90,7 +90,7 @@ export async function _handleExecute(chatModeId: ChatModeId, conversationId: DCo
|
||||
|
||||
case 'generate-react':
|
||||
// verify we were called with a single DMessageTextContent
|
||||
if (firstFragment.ft !== 'content' || firstFragment.part.pt !== 'text')
|
||||
if (!isContentFragment(firstFragment) || !isTextPart(firstFragment.part))
|
||||
return false;
|
||||
const reactPrompt = firstFragment.part.text;
|
||||
cHandler.messageFragmentReplace(lastMessage.id, firstFragment.fId, createTextContentFragment(textToDrawCommand(reactPrompt)), true);
|
||||
|
||||
@@ -2,7 +2,7 @@ import type { DLLMId } from '~/modules/llms/store-llms';
|
||||
|
||||
import type { DMessageId } from '~/common/stores/chat/chat.message';
|
||||
import { ConversationHandler } from '~/common/chats/ConversationHandler';
|
||||
import { createTextContentFragment, DMessageFragment } from '~/common/stores/chat/chat.fragments';
|
||||
import { createTextContentFragment, DMessageFragment, isContentFragment, isTextPart } from '~/common/stores/chat/chat.fragments';
|
||||
|
||||
import { extractChatCommand, helpPrettyChatCommands } from '../commands/commands.registry';
|
||||
import { runBrowseGetPageUpdatingState } from './browse-load';
|
||||
@@ -16,7 +16,7 @@ export const RET_NO_CMD = 'no-cmd';
|
||||
export async function _handleExecuteCommand(lastMessageId: DMessageId, lastMessageFirstFragment: DMessageFragment, cHandler: ConversationHandler, chatLLMId: DLLMId) {
|
||||
|
||||
// commands must have a first Content DMessageTextPart
|
||||
if (lastMessageFirstFragment.ft !== 'content' || lastMessageFirstFragment.part.pt !== 'text')
|
||||
if (!isContentFragment(lastMessageFirstFragment) || !isTextPart(lastMessageFirstFragment.part))
|
||||
return RET_NO_CMD;
|
||||
|
||||
// check if we have a command
|
||||
|
||||
@@ -92,6 +92,10 @@ export class ConversationHandler {
|
||||
return { assistantMessageId: assistantMessage.id, placeholderFragmentId };
|
||||
}
|
||||
|
||||
messageAppend(message: DMessage) {
|
||||
this.chatActions.appendMessage(this.conversationId, message);
|
||||
}
|
||||
|
||||
messageEdit(messageId: string, update: Partial<DMessage> | ((message: DMessage) => Partial<DMessage>), 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
|
||||
|
||||
@@ -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 */) };
|
||||
|
||||
@@ -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<DMessageFragment[]>, 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,
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user