From 30ad8f107db19cd47e7bbbb90cfa3ecc1056034d Mon Sep 17 00:00:00 2001 From: Enrico Ros Date: Tue, 9 Jul 2024 09:27:54 -0700 Subject: [PATCH] MP: improve sentinels --- .../composer/llmattachments/LLMAttachmentMenu.tsx | 4 ++-- .../fragments-attachment-doc/DocumentFragmentButton.tsx | 4 ++-- .../fragments-attachment-doc/DocumentFragmentEditor.tsx | 6 +++--- src/common/stores/chat/chat.fragments.ts | 8 ++++++-- src/common/stores/chat/chat.tokens.ts | 6 +++--- src/common/stores/chat/store-chats.ts | 4 ++-- 6 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/apps/chat/components/composer/llmattachments/LLMAttachmentMenu.tsx b/src/apps/chat/components/composer/llmattachments/LLMAttachmentMenu.tsx index d8a5231e9..85f99d24f 100644 --- a/src/apps/chat/components/composer/llmattachments/LLMAttachmentMenu.tsx +++ b/src/apps/chat/components/composer/llmattachments/LLMAttachmentMenu.tsx @@ -10,7 +10,7 @@ import VerticalAlignBottomIcon from '@mui/icons-material/VerticalAlignBottom'; import { showImageDataRefInNewTab } from '~/modules/blocks/image/RenderImageRefDBlob'; -import { DMessageAttachmentFragment, isImageRefPart } from '~/common/stores/chat/chat.fragments'; +import { DMessageAttachmentFragment, isDocPart, isImageRefPart } from '~/common/stores/chat/chat.fragments'; import { CloseableMenu } from '~/common/components/CloseableMenu'; import type { AttachmentDraftId } from '~/common/attachment-drafts/attachment.types'; @@ -162,7 +162,7 @@ export function LLMAttachmentMenu(props: { ); - } else if (part.pt === 'doc') { + } else if (isDocPart(part)) { return ( 🡒 text: {part.data.text.length.toLocaleString()} bytes diff --git a/src/apps/chat/components/message/fragments-attachment-doc/DocumentFragmentButton.tsx b/src/apps/chat/components/message/fragments-attachment-doc/DocumentFragmentButton.tsx index bbaeb3812..0ac17438f 100644 --- a/src/apps/chat/components/message/fragments-attachment-doc/DocumentFragmentButton.tsx +++ b/src/apps/chat/components/message/fragments-attachment-doc/DocumentFragmentButton.tsx @@ -10,7 +10,7 @@ import TelegramIcon from '@mui/icons-material/Telegram'; import TextFieldsIcon from '@mui/icons-material/TextFields'; import TextureIcon from '@mui/icons-material/Texture'; -import type { DMessageAttachmentFragment, DMessageFragmentId } from '~/common/stores/chat/chat.fragments'; +import { DMessageAttachmentFragment, DMessageFragmentId, isDocPart } from '~/common/stores/chat/chat.fragments'; import { ContentScaling, themeScalingMap } from '~/common/app.theme'; import { ellipsizeMiddle } from '~/common/util/textUtils'; @@ -51,7 +51,7 @@ export function DocumentFragmentButton(props: { const { fragment, isSelected, toggleSelected } = props; // only operate on doc fragments - if (fragment.part.pt !== 'doc') + if (!isDocPart(fragment.part)) throw new Error('Unexpected part type: ' + fragment.part.pt); // handlers diff --git a/src/apps/chat/components/message/fragments-attachment-doc/DocumentFragmentEditor.tsx b/src/apps/chat/components/message/fragments-attachment-doc/DocumentFragmentEditor.tsx index cec6ee61f..0c2705167 100644 --- a/src/apps/chat/components/message/fragments-attachment-doc/DocumentFragmentEditor.tsx +++ b/src/apps/chat/components/message/fragments-attachment-doc/DocumentFragmentEditor.tsx @@ -10,7 +10,7 @@ import { AutoBlocksRenderer } from '~/modules/blocks/AutoBlocksRenderer'; import type { ContentScaling } from '~/common/app.theme'; import type { DMessageRole } from '~/common/stores/chat/chat.message'; -import { createDMessageDataInlineText, createDocAttachmentFragment, DMessageAttachmentFragment, DMessageFragmentId } from '~/common/stores/chat/chat.fragments'; +import { createDMessageDataInlineText, createDocAttachmentFragment, DMessageAttachmentFragment, DMessageFragmentId, isDocPart } from '~/common/stores/chat/chat.fragments'; import { marshallWrapText } from '~/common/stores/chat/chat.tokens'; import { ContentPartTextEditor } from '../fragments-content/ContentPartTextEditor'; @@ -38,7 +38,7 @@ export function DocumentFragmentEditor(props: { const fragmentCaption = fragment.caption; const part = fragment.part; - if (part.pt !== 'doc') + if (!isDocPart(part)) throw new Error('Unexpected part type: ' + part.pt); // delete @@ -66,7 +66,7 @@ export function DocumentFragmentEditor(props: { return; // only edit DOCs - if (fragment.part.pt !== 'doc') { + if (!isDocPart(fragment.part)) { console.warn('handleEditApply: unexpected part type:', fragment.part.pt); return; } diff --git a/src/common/stores/chat/chat.fragments.ts b/src/common/stores/chat/chat.fragments.ts index e2badb999..6414487b6 100644 --- a/src/common/stores/chat/chat.fragments.ts +++ b/src/common/stores/chat/chat.fragments.ts @@ -130,14 +130,18 @@ export function isContentOrAttachmentFragment(fragment: DMessageFragment) { return fragment.ft === 'content' || fragment.ft === 'attachment'; } -export function isTextPart(part: DMessageContentFragment['part']) { - return part.pt === 'text'; +export function isDocPart(part: DMessageContentFragment['part'] | DMessageAttachmentFragment['part']) { + return part.pt === 'doc'; } export function isImageRefPart(part: DMessageContentFragment['part'] | DMessageAttachmentFragment['part']) { return part.pt === 'image_ref'; } +export function isTextPart(part: DMessageContentFragment['part']) { + return part.pt === 'text'; +} + /// Helpers - Fragments Creation diff --git a/src/common/stores/chat/chat.tokens.ts b/src/common/stores/chat/chat.tokens.ts index 0ea9ec4ba..be35809ce 100644 --- a/src/common/stores/chat/chat.tokens.ts +++ b/src/common/stores/chat/chat.tokens.ts @@ -2,7 +2,7 @@ import type { DLLM } from '~/modules/llms/store-llms'; import { textTokensForLLM } from '~/common/tokens/tokens.text'; -import { DMessageAttachmentFragment, DMessageFragment, isContentFragment, isContentOrAttachmentFragment } from '~/common/stores/chat/chat.fragments'; +import { DMessageAttachmentFragment, DMessageFragment, isAttachmentFragment, isContentFragment, isContentOrAttachmentFragment, isDocPart } from '~/common/stores/chat/chat.fragments'; import { imageTokensForLLM } from '~/common/tokens/tokens.image'; @@ -35,7 +35,7 @@ function _fragmentTokens(fragment: DMessageFragment, llm: DLLM, debugFrom: strin return 0; // attachment fragments - if (fragment.ft === 'attachment') { + if (isAttachmentFragment(fragment)) { const aPart = fragment.part; switch (aPart.pt) { case 'doc': @@ -87,7 +87,7 @@ export function marshallWrapDocFragments(initialText: string | null, fragments: let inlinedText = initialText || ''; for (const fragment of fragments) { // warn on non-text fragments, which are not handled - it's an API error to call this function to non-text-part fragments - if (fragment.part.pt !== 'doc') { + if (!isDocPart(fragment.part)) { console.warn('marshallWrapTextFragments: unhandled part type:', fragment.part.pt); continue; } diff --git a/src/common/stores/chat/store-chats.ts b/src/common/stores/chat/store-chats.ts index b6d53aedd..25100529a 100644 --- a/src/common/stores/chat/store-chats.ts +++ b/src/common/stores/chat/store-chats.ts @@ -12,7 +12,7 @@ import { backupIdbV3, idbStateStorage } from '~/common/util/idbUtils'; import type { DMessage, DMessageId, DMessageMetadata } from './chat.message'; import { conversationTitle, createDConversation, DConversation, DConversationId, duplicateCConversation } from './chat.conversation'; -import { createErrorContentFragment, DMessageFragment, DMessageFragmentId, isContentFragment } from './chat.fragments'; +import { createErrorContentFragment, DMessageFragment, DMessageFragmentId, isAttachmentFragment, isContentFragment } from './chat.fragments'; import { estimateTokensForFragments } from './chat.tokens'; @@ -398,7 +398,7 @@ export const useChatStore = create()(devtools( fragment.fId = agiId('chat-dfragment'); } // fixup, for attachments without date, use the message date - if (fragment.ft === 'attachment' && !fragment.created) { + if (isAttachmentFragment(fragment) && !fragment.created) { fragment.created = message.updated || message.created; } }