MP: improve sentinels

This commit is contained in:
Enrico Ros
2024-07-09 09:27:54 -07:00
parent 2922b4c1dc
commit 30ad8f107d
6 changed files with 18 additions and 14 deletions
@@ -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: {
</Link>
</Typography>
);
} else if (part.pt === 'doc') {
} else if (isDocPart(part)) {
return (
<Typography key={index} level='body-sm'>
🡒 text: {part.data.text.length.toLocaleString()} bytes
@@ -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
@@ -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;
}
+6 -2
View File
@@ -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
+3 -3
View File
@@ -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;
}
+2 -2
View File
@@ -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<ConversationsStore>()(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;
}
}