Add optional context parameter to source_file calls.

This commit is contained in:
Bartosz Taudul
2026-01-07 16:24:34 +01:00
parent 3c82b63046
commit f00694fae0
3 changed files with 11 additions and 6 deletions

View File

@@ -117,7 +117,7 @@
"type": "function",
"function": {
"name": "source_file",
"description": "Retrieve the source file contents.",
"description": "Retrieve the source file contents around given line.",
"parameters": {
"type": "object",
"properties": {
@@ -127,7 +127,11 @@
},
"line": {
"type": "integer",
"description": "Line number that should be retrieved."
"description": "Target line number."
},
"context": {
"type": "integer",
"description": "How many lines to display before and after the target line."
}
},
"required": ["file", "line"]

View File

@@ -163,7 +163,7 @@ TracyLlmTools::ToolReply TracyLlmTools::HandleToolCalls( const std::string& tool
}
else if( tool == "source_file" )
{
return { .reply = SourceFile( Param( "file" ), ParamU32( "line" ) ) };
return { .reply = SourceFile( Param( "file" ), ParamU32( "line" ), ParamOptU32( "context", 5 ) ) };
}
return { .reply = "Unknown tool call: " + tool };
}
@@ -760,7 +760,7 @@ std::string TracyLlmTools::SearchManual( const std::string& query, TracyLlmApi&
return json.dump( 2, ' ', false, nlohmann::json::error_handler_t::replace );
}
std::string TracyLlmTools::SourceFile( const std::string& file, uint32_t line ) const
std::string TracyLlmTools::SourceFile( const std::string& file, uint32_t line, uint32_t context ) const
{
if( line == 0 ) return "Error: Source file line number must be greater than 0.";
@@ -777,7 +777,7 @@ std::string TracyLlmTools::SourceFile( const std::string& file, uint32_t line )
uint32_t minLine = line;
uint32_t maxLine = line+1;
while( minLine > 0 || maxLine < lines.size() )
while( context > 0 && ( minLine > 0 || maxLine < lines.size() ) )
{
if( minLine > 0 )
{
@@ -791,6 +791,7 @@ std::string TracyLlmTools::SourceFile( const std::string& file, uint32_t line )
if( size >= maxSize ) break;
maxLine++;
}
context--;
}
nlohmann::json json = {

View File

@@ -61,7 +61,7 @@ private:
std::string SearchWeb( std::string query );
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 ) const;
std::string SourceFile( const std::string& file, uint32_t line, uint32_t context ) const;
void ManualEmbeddingsWorker( TracyLlmApi& api );