Browse Source

fix: render enough messages when selecting context in chat blocks

Signed-off-by: Dorra Jaouad <dorra.jaoued7@gmail.com>
pull/16359/head
Dorra Jaouad 2 weeks ago
parent
commit
df31d605e5
  1. 2
      src/composables/useGetMessages.ts
  2. 35
      src/stores/chat.ts

2
src/composables/useGetMessages.ts

@ -280,7 +280,7 @@ export function useGetMessagesProvider() {
messageId = nearestContextMessageId
}
if (messageId === firstContextMessageId) {
if (messageId === firstContextMessageId || !chatStore.hasEnoughMessages(token, { messageId, threadId })) {
// message is the first one in the block, try to get some messages above
isInitialisingMessages.value = true
await getOldMessages(token, false, { messageId, threadId })

35
src/stores/chat.ts

@ -196,6 +196,40 @@ export const useChatStore = defineStore('chat', () => {
return Math.min(...filterNumericIds(contextBlock))
}
/**
*
* Check whether there are enough messages to render in the selected context, in particular
* when there are other blocks, it means there is likely more history to load
*
* @param token The conversation token
* @param data The data object containing messageId and threadId
* @param data.messageId The message id
* @param data.threadId The thread id
*/
function hasEnoughMessages(
token: string,
{ messageId = 0, threadId = 0 }: GetMessagesListOptions = { messageId: 0, threadId: 0 },
): boolean {
let contextBlock: Set<number>
const numBlocks = (threadId ? threadBlocks[token][threadId]?.length : chatBlocks[token]?.length) ?? 0
if (numBlocks <= 1) {
// If only one block, we cannot assume there is more history to load
return true
}
if (threadId) {
contextBlock = (messageId <= 0)
? threadBlocks[token][threadId][0]
: threadBlocks[token][threadId].find((set) => set.has(messageId)) ?? threadBlocks[token][threadId][0]
} else {
contextBlock = (messageId <= 0)
? chatBlocks[token][0]
: chatBlocks[token].find((set) => set.has(messageId)) ?? chatBlocks[token][0]
}
return contextBlock.size > 10
}
/**
* Returns last known message id, belonging to current context. Defaults to given messageId
*
@ -554,5 +588,6 @@ export const useChatStore = defineStore('chat', () => {
removeMessagesFromChatBlocks,
clearMessagesHistory,
purgeChatStore,
hasEnoughMessages,
}
})
Loading…
Cancel
Save