Skip to content

Commit

Permalink
Beam: begin history rationalization
Browse files Browse the repository at this point in the history
  • Loading branch information
enricoros committed Dec 30, 2024
1 parent af78068 commit 8584a56
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 33 deletions.
24 changes: 15 additions & 9 deletions src/apps/chat/AppChat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { createDMessageFromFragments, createDMessagePlaceholderIncomplete, DMess
import { createErrorContentFragment, createTextContentFragment, DMessageAttachmentFragment, DMessageContentFragment, duplicateDMessageFragmentsNoVoid } from '~/common/stores/chat/chat.fragments';
import { gcChatImageAssets } from '~/common/stores/chat/chat.gc';
import { getChatLLMId } from '~/common/stores/llms/store-llms';
import { getConversation, getConversationSystemPurposeId, useConversation } from '~/common/stores/chat/store-chats';
import { getConversation, getConversationSystemPurposeId, isValidConversation, useConversation } from '~/common/stores/chat/store-chats';
import { optimaActions, optimaOpenModels, optimaOpenPreferences, useSetOptimaAppMenu } from '~/common/layout/optima/useOptima';
import { themeBgAppChatComposer } from '~/common/app.theme';
import { useChatLLM } from '~/common/stores/llms/llms.hooks';
Expand Down Expand Up @@ -265,14 +265,20 @@ export function AppChat() {

const handleMessageBeamLastInFocusedPane = React.useCallback(async () => {
// Ctrl + Shift + B
const focusedConversation = getConversation(focusedPaneConversationId);
if (focusedConversation?.messages?.length) {
const lastMessage = focusedConversation.messages[focusedConversation.messages.length - 1];
if (lastMessage.role === 'assistant')
ConversationsManager.getHandler(focusedConversation.id).beamInvoke(focusedConversation.messages.slice(0, -1), [lastMessage], lastMessage.id);
else if (lastMessage.role === 'user')
ConversationsManager.getHandler(focusedConversation.id).beamInvoke(focusedConversation.messages, [], null);
}
if (!focusedPaneConversationId) return;
const cHandler = ConversationsManager.getHandler(focusedPaneConversationId);
if (!cHandler.isValid()) return;
const inputHistory = cHandler.historyViewHead('chat-beam-shortcut');
if (!inputHistory.length) return;

// TODO: replace the Persona and Auto-Cache-hint in the history?

// replace the prompt in history
const lastMessage = inputHistory[inputHistory.length - 1];
if (lastMessage.role === 'assistant')
cHandler.beamInvoke(inputHistory.slice(0, -1), [lastMessage], lastMessage.id);
else if (lastMessage.role === 'user')
cHandler.beamInvoke(inputHistory, [], null);
}, [focusedPaneConversationId]);

const handleTextDiagram = React.useCallback((diagramConfig: DiagramConfig | null) => setDiagramConfig(diagramConfig), []);
Expand Down
45 changes: 24 additions & 21 deletions src/apps/chat/components/ChatMessageList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import { ShortcutKey, useGlobalShortcuts } from '~/common/components/shortcuts/u
import { convertFilesToDAttachmentFragments } from '~/common/attachment-drafts/attachment.pipeline';
import { createDMessageFromFragments, createDMessageTextContent, DMessage, DMessageId, DMessageUserFlag, DMetaReferenceItem, MESSAGE_FLAG_AIX_SKIP } from '~/common/stores/chat/chat.message';
import { createTextContentFragment, DMessageFragment, DMessageFragmentId } from '~/common/stores/chat/chat.fragments';
import { getConversation, useChatStore } from '~/common/stores/chat/store-chats';
import { openFileForAttaching } from '~/common/components/ButtonAttachFiles';
import { optimaOpenPreferences } from '~/common/layout/optima/useOptima';
import { useBrowserTranslationWarning } from '~/common/components/useIsBrowserTranslating';
import { useCapabilityElevenLabs } from '~/common/components/useCapabilities';
import { useChatOverlayStore } from '~/common/chat-overlay/store-perchat_vanilla';
import { useChatStore } from '~/common/stores/chat/store-chats';
import { useScrollToBottom } from '~/common/scroll-to-bottom/useScrollToBottom';

import { CMLZeroConversation } from './messages-list/CMLZeroConversation';
Expand Down Expand Up @@ -136,26 +136,29 @@ export function ChatMessageList(props: {
}, [conversationHandler, conversationId, onConversationExecuteHistory]);

const handleMessageBeam = React.useCallback(async (messageId: DMessageId) => {
// Right-click menu Beam
if (!conversationId || !props.conversationHandler) return;
const messages = getConversation(conversationId)?.messages;
if (messages?.length) {
const truncatedHistory = messages.slice(0, messages.findIndex(m => m.id === messageId) + 1);
const lastMessage = truncatedHistory[truncatedHistory.length - 1];
if (lastMessage) {
// assistant: do an in-place beam
if (lastMessage.role === 'assistant') {
if (truncatedHistory.length >= 2)
props.conversationHandler.beamInvoke(truncatedHistory.slice(0, -1), [lastMessage], lastMessage.id);
} else {
// user: truncate and append (but if the next message is an assistant message, import it)
const nextMessage = messages[truncatedHistory.length];
if (nextMessage?.role === 'assistant')
props.conversationHandler.beamInvoke(truncatedHistory, [nextMessage], null);
else
props.conversationHandler.beamInvoke(truncatedHistory, [], null);
}
}
// Message option menu Beam
if (!conversationId || !props.conversationHandler || !props.conversationHandler.isValid()) return;
const inputHistory = props.conversationHandler.historyViewHead('chat-beam-message');
if (!inputHistory.length) return;

// TODO: replace the Persona and Auto-Cache-hint in the history?

// truncate the history to the given message (may or may not have more after)
const truncatedHistory = inputHistory.slice(0, inputHistory.findIndex(m => m.id === messageId) + 1);
const lastTruncatedMessage = truncatedHistory[truncatedHistory.length - 1];
if (!lastTruncatedMessage) return;

// assistant: do an in-place beam
if (lastTruncatedMessage.role === 'assistant') {
if (truncatedHistory.length >= 2)
props.conversationHandler.beamInvoke(truncatedHistory.slice(0, -1), [lastTruncatedMessage], lastTruncatedMessage.id);
} else if (lastTruncatedMessage.role === 'user') {
// user: truncate and append (but if the next message is an assistant message, import it)
const possibleNextMessage = inputHistory[truncatedHistory.length];
if (possibleNextMessage?.role === 'assistant')
props.conversationHandler.beamInvoke(truncatedHistory, [possibleNextMessage], null);
else
props.conversationHandler.beamInvoke(truncatedHistory, [], null);
}
}, [conversationId, props.conversationHandler]);

Expand Down
5 changes: 3 additions & 2 deletions src/apps/chat/editors/_handleExecute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export async function _handleExecute(chatExecuteMode: ChatExecuteMode, conversat

const chatLLMId = getChatLLMId();
const cHandler = ConversationsManager.getHandler(conversationId);
const initialHistory = cHandler.historyViewHead(executeCallerNameDebug) as Readonly<DMessage[]>;
const initialHistory = cHandler.historyViewHead('chat-execute-' + executeCallerNameDebug) as Readonly<DMessage[]>;

// Update the system message from the active persona to the history
// NOTE: this does NOT call setMessages anymore (optimization). make sure to:
Expand Down Expand Up @@ -72,7 +72,8 @@ export async function _handleExecute(chatExecuteMode: ChatExecuteMode, conversat
return await runPersonaOnConversationHead(chatLLMId, conversationId);

case 'beam-content':
cHandler.beamInvoke(cHandler.historyViewHead('beam-content'), [], null);
const updatedInputHistory = cHandler.historyViewHead('chat-beam-execute');
cHandler.beamInvoke(updatedInputHistory, [], null);
return true;

case 'append-user':
Expand Down
6 changes: 5 additions & 1 deletion src/common/chat-overlay/ConversationHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { BeamStore, createBeamVanillaStore } from '~/modules/beam/store-beam_van

import type { DConversationId } from '~/common/stores/chat/chat.conversation';
import type { DLLMId } from '~/common/stores/llms/llms.types';
import { ChatActions, getConversationSystemPurposeId, useChatStore } from '~/common/stores/chat/store-chats';
import { ChatActions, getConversationSystemPurposeId, isValidConversation, useChatStore } from '~/common/stores/chat/store-chats';
import { createDMessageEmpty, createDMessageFromFragments, createDMessagePlaceholderIncomplete, createDMessageTextContent, DMessage, DMessageGenerator, DMessageId, DMessageUserFlag, MESSAGE_FLAG_VND_ANT_CACHE_AUTO, MESSAGE_FLAG_VND_ANT_CACHE_USER, messageHasUserFlag, messageSetUserFlag } from '~/common/stores/chat/chat.message';
import { createTextContentFragment, DMessageFragment, DMessageFragmentId } from '~/common/stores/chat/chat.fragments';
import { gcChatImageAssets } from '~/common/stores/chat/chat.gc';
Expand Down Expand Up @@ -122,6 +122,10 @@ export class ConversationHandler {
return _chatStoreActions.isIncognito(this.conversationId);
}

isValid(): boolean {
return isValidConversation(this.conversationId);
}


// Message Management

Expand Down

0 comments on commit 8584a56

Please sign in to comment.