Add optional file globbing to SourceSearch.

This commit is contained in:
Bartosz Taudul
2026-01-25 02:41:00 +01:00
parent 63c6e48562
commit a4324d595d
3 changed files with 36 additions and 3 deletions
+4
View File
@@ -153,6 +153,10 @@
"case_insensitive": {
"type": "boolean",
"description": "Case insensitive search."
},
"glob": {
"type": "string",
"description": "File path match."
}
},
"required": ["query"]
+31 -2
View File
@@ -7,6 +7,7 @@
#include <tidy.h>
#include <tidybuffio.h>
#include <time.h>
#include <regex>
#include "TracyConfig.hpp"
#include "TracyLlmApi.hpp"
@@ -179,7 +180,8 @@ TracyLlmTools::ToolReply TracyLlmTools::HandleToolCalls( const std::string& tool
}
else if( tool == "source_search" )
{
return { .reply = SourceSearch( Param( "query" ), ParamOptBool( "case_insensitive", false ) ) };
std::string empty;
return { .reply = SourceSearch( Param( "query" ), ParamOptBool( "case_insensitive", false ), ParamOptString( "glob", empty ) ) };
}
return { .reply = "Unknown tool call: " + tool };
}
@@ -823,7 +825,33 @@ std::string TracyLlmTools::SourceFile( const std::string& file, uint32_t line, u
return json.dump( 2, ' ', false, nlohmann::json::error_handler_t::replace );
}
std::string TracyLlmTools::SourceSearch( std::string query, bool caseInsensitive ) const
static bool GlobMatch( const std::string& pattern, const std::string& text )
{
std::string rx;
for( char c : pattern )
{
if( c == '*' )
{
rx += ".*";
}
else if( c == '?' )
{
rx += ".";
}
else if( c == '.' || c == '+' || c == '^' || c == '$' || c == '|' || c == '(' || c == ')' || c == '[' || c == ']' || c == '{' || c == '}' )
{
rx += '\\';
rx += c;
}
else
{
rx += c;
}
}
return std::regex_search( text, std::regex( rx ) );
}
std::string TracyLlmTools::SourceSearch( std::string query, bool caseInsensitive, const std::string& glob ) const
{
auto& cache = m_worker.GetSourceFileCache();
nlohmann::json json = {};
@@ -832,6 +860,7 @@ std::string TracyLlmTools::SourceSearch( std::string query, bool caseInsensitive
for( auto& item : cache )
{
if( IsFrameExternal( item.first, nullptr ) ) continue;
if( !glob.empty() && !GlobMatch( glob, item.first ) ) continue;
char* tmp = nullptr;
auto& mem = item.second;
+1 -1
View File
@@ -62,7 +62,7 @@ private:
std::string GetWebpage( const std::string& url );
std::string SearchManual( const std::string& query, TracyLlmApi& api, bool hasEmbeddingsModel );
std::string SourceFile( const std::string& file, uint32_t line, uint32_t context ) const;
std::string SourceSearch( std::string query, bool caseInsensitive ) const;
std::string SourceSearch( std::string query, bool caseInsensitive, const std::string& glob ) const;
void ManualEmbeddingsWorker( TracyLlmApi& api );