@@ -852,32 +852,29 @@ std::string ChatLLM::applyJinjaTemplate(std::span<const ChatItem> items) const
852
852
}
853
853
854
854
auto ChatLLM::promptInternalChat (const QStringList &enabledCollections, const LLModel::PromptContext &ctx,
855
- std::optional<QList<ChatItem >> chat ) -> ChatPromptResult
855
+ std::optional<std::pair< int , int >> subrange ) -> ChatPromptResult
856
856
{
857
857
Q_ASSERT (isModelLoaded ());
858
858
Q_ASSERT (m_chatModel);
859
859
860
+ // Return a (ChatModelAccessor, std::span) pair where the span represents the relevant messages for this chat.
861
+ // "subrange" is used to select only local server messages from the current chat session.
862
+ auto getChat = [&]() {
863
+ auto items = m_chatModel->chatItems (); // holds lock
864
+ std::span view (items);
865
+ if (subrange)
866
+ view = view.subspan (subrange->first , subrange->second );
867
+ Q_ASSERT (view.size () >= 2 );
868
+ return std::pair (std::move (items), view);
869
+ };
870
+
860
871
// copy messages for safety (since we can't hold the lock the whole time)
861
872
std::optional<std::pair<int , QString>> query;
862
- std::vector<ChatItem> chatItems;
863
873
{
864
- std::optional<ChatModelAccessor> items;
865
- std::span<const ChatItem> view;
866
- if (chat) {
867
- view = *chat;
868
- } else {
869
- items = m_chatModel->chatItems (); // holds lock
870
- Q_ASSERT (!items->empty ());
871
- view = *items;
872
- }
873
- Q_ASSERT (view.size () >= 2 ); // should be prompt/response pairs
874
-
875
874
// Find the prompt that represents the query. Server chats are flexible and may not have one.
876
- auto response = view. end () - 1 ;
877
- if (auto peer = m_chatModel->getPeer (view, response))
875
+ auto [_, view] = getChat (); // holds lock
876
+ if (auto peer = m_chatModel->getPeer (view, view. end () - 1 )) // peer of response
878
877
query = { *peer - view.begin (), (*peer)->value };
879
-
880
- chatItems.assign (view.begin (), view.end () - 1 ); // exclude last
881
878
}
882
879
883
880
QList<ResultInfo> databaseResults;
@@ -889,6 +886,13 @@ auto ChatLLM::promptInternalChat(const QStringList &enabledCollections, const LL
889
886
emit databaseResultsChanged (databaseResults);
890
887
}
891
888
889
+ // copy messages for safety (since we can't hold the lock the whole time)
890
+ std::vector<ChatItem> chatItems;
891
+ {
892
+ auto [_, view] = getChat (); // holds lock
893
+ chatItems.assign (view.begin (), view.end () - 1 ); // exclude new response
894
+ }
895
+
892
896
auto result = promptInternal (chatItems, ctx, !databaseResults.isEmpty ());
893
897
return {
894
898
/* PromptResult*/ {
0 commit comments