class SemanticSearchTool(Tool[SemanticSearchToolSchema], schema=SemanticSearchToolSchema):
# Set a token limit for files to surface, and filter out low relevance files
score_threshold: float = 0.5
token_limit: int = 30_000
def execute_impl(
self,
tool_input: SemanticSearchToolSchema,
context: Context,
) -> str:
try:
client = RelaceClient()
# Get list of files from repo, ignoring config/data/binary/generated files
files = [
{
"filename": str(path),
"content": path.read_text()
}
for path in context.repo.list_tracked_files(
ignore_tracked=(
# Ignore non-code files like:
# - Config files (yaml, env)
# - Lock files (package-lock.json)
# - Data files (csv, json)
# - Binary files (images)
# - Build output
)
)
]
# Call reranker with files, conversation context, and query
ranked = client.rank_files(
codebase=files,
query=(
f"<conversation>{context.history.get_summary()}</conversation>\n"
f"<query>{tool_input.query}</query>"
),
token_limit=self.token_limit,
threshold=self.score_threshold,
)
# Construct response
return "\n\n".join(
f"{file_path.relative_to(context.repo.root_path)}\n"
f"```\n{file_path.read_text()}\n```"
for file_path in ranked
)
except Exception as e:
raise ToolError(f"Error finding files: {str(e)}") from e