1
1
#include " chatllm.h"
2
2
3
- #include " bravesearch.h"
4
3
#include " chat.h"
5
4
#include " chatapi.h"
6
5
#include " localdocssearch.h"
@@ -893,36 +892,31 @@ bool ChatLLM::promptRecursive(const QList<QString> &toolContexts, const QString
893
892
const QString tool = toolCallDoc[" name" ].toString ();
894
893
const QJsonObject args = toolCallDoc[" parameters" ].toObject ();
895
894
896
- // FIXME: In the future this will try to match the tool call to a list of tools that are supported
897
- // according to MySettings, but for now only brave search is supported
898
- if (tool != " web_search" || !args.contains (" query" )) {
899
- // FIXME: Need to surface errors to the UI
900
- qWarning () << " ERROR: Could not find the tool and correct parameters for " << toolCall;
895
+ Tool *toolInstance = ToolModel::globalInstance ()->get (tool);
896
+ if (!toolInstance) {
897
+ qWarning () << " ERROR: Could not find the tool for " << toolCall;
901
898
return handleFailedToolCall (trimmed, totalTime);
902
899
}
903
900
904
- const QString query = args[" query" ].toString ();
901
+ // Inform the chat that we're executing a tool call
902
+ emit toolCalled (toolInstance->name ().toLower ());
905
903
906
- emit toolCalled (tr (" searching web..." ));
907
- const QString apiKey = MySettings::globalInstance ()->braveSearchAPIKey ();
908
- Q_ASSERT (apiKey != " " );
909
- BraveSearch brave;
910
-
911
- QJsonObject parameters;
912
- parameters.insert (" apiKey" , apiKey);
913
- parameters.insert (" query" , query);
914
- parameters.insert (" count" , 2 );
915
-
916
- // FIXME: Need to surface errors to the UI
917
- const QString braveResponse = brave.run (parameters, 2000 /* msecs to timeout*/ );
904
+ const QString response = toolInstance->run (args, 2000 /* msecs to timeout*/ );
905
+ if (toolInstance->error () != ToolEnums::Error::NoError) {
906
+ qWarning () << " ERROR: Tool call produced error:" << toolInstance->errorString ();
907
+ return handleFailedToolCall (trimmed, totalTime);
908
+ }
918
909
919
- QString parseError;
920
- QList<SourceExcerpt> sourceExcerpts = SourceExcerpt::fromJson (braveResponse, parseError);
921
- if (!parseError.isEmpty ()) {
922
- qWarning () << " ERROR: Could not parse source excerpts for brave response:" << parseError;
923
- } else if (!sourceExcerpts.isEmpty ()) {
924
- producedSourceExcerpts = true ;
925
- emit sourceExcerptsChanged (sourceExcerpts);
910
+ // If the tool supports excerpts then try to parse them here
911
+ if (toolInstance->excerpts ()) {
912
+ QString parseError;
913
+ QList<SourceExcerpt> sourceExcerpts = SourceExcerpt::fromJson (response, parseError);
914
+ if (!parseError.isEmpty ()) {
915
+ qWarning () << " ERROR: Could not parse source excerpts for response:" << parseError;
916
+ } else if (!sourceExcerpts.isEmpty ()) {
917
+ producedSourceExcerpts = true ;
918
+ emit sourceExcerptsChanged (sourceExcerpts);
919
+ }
926
920
}
927
921
928
922
m_promptResponseTokens = 0 ;
@@ -931,7 +925,7 @@ bool ChatLLM::promptRecursive(const QList<QString> &toolContexts, const QString
931
925
932
926
// This is a recursive call but isRecursiveCall is checked above to arrest infinite recursive
933
927
// tool calls
934
- return promptRecursive (QList<QString>()/* collectionList */ , braveResponse , toolTemplate,
928
+ return promptRecursive (QList<QString>()/* tool context */ , response , toolTemplate,
935
929
n_predict, top_k, top_p, min_p, temp, n_batch, repeat_penalty, repeat_penalty_tokens, totalTime,
936
930
producedSourceExcerpts, true /* isRecursiveCall*/ );
937
931
} else {
@@ -946,6 +940,7 @@ bool ChatLLM::promptRecursive(const QList<QString> &toolContexts, const QString
946
940
947
941
bool ChatLLM::handleFailedToolCall (const std::string &response, qint64 elapsed)
948
942
{
943
+ // FIXME: Need to surface errors to the UI
949
944
// Restore the strings that we excluded previously when detecting the tool call
950
945
m_response = " <tool_call>" + response + " </tool_call>" ;
951
946
emit responseChanged (QString::fromStdString (m_response));
0 commit comments