mirror of
https://github.com/khoaliber/khoj.git
synced 2026-03-02 13:18:18 +00:00
Simplify view file tool. Limit viewing upto 50 lines at a time
We were previously truncating by characters. Limiting by max lines allows model to control line ranges they request
This commit is contained in:
@@ -2916,38 +2916,34 @@ async def view_file_content(
|
||||
raw_text = file_object.raw_text
|
||||
|
||||
# Apply line range filtering if specified
|
||||
if start_line is None and end_line is None:
|
||||
filtered_text = raw_text
|
||||
else:
|
||||
lines = raw_text.split("\n")
|
||||
start_line = start_line or 1
|
||||
end_line = end_line or len(lines)
|
||||
lines = raw_text.split("\n")
|
||||
start_line = start_line or 1
|
||||
end_line = end_line or len(lines)
|
||||
|
||||
# Validate line range
|
||||
if start_line < 1 or end_line < 1 or start_line > end_line:
|
||||
error_msg = f"Invalid line range: {start_line}-{end_line}"
|
||||
logger.warning(error_msg)
|
||||
yield [{"query": query, "file": path, "compiled": error_msg}]
|
||||
return
|
||||
if start_line > len(lines):
|
||||
error_msg = f"Start line {start_line} exceeds total number of lines {len(lines)}"
|
||||
logger.warning(error_msg)
|
||||
yield [{"query": query, "file": path, "compiled": error_msg}]
|
||||
return
|
||||
# Validate line range
|
||||
if start_line < 1 or end_line < 1 or start_line > end_line:
|
||||
error_msg = f"Invalid line range: {start_line}-{end_line}"
|
||||
logger.warning(error_msg)
|
||||
yield [{"query": query, "file": path, "compiled": error_msg}]
|
||||
return
|
||||
if start_line > len(lines):
|
||||
error_msg = f"Start line {start_line} exceeds total number of lines {len(lines)}"
|
||||
logger.warning(error_msg)
|
||||
yield [{"query": query, "file": path, "compiled": error_msg}]
|
||||
return
|
||||
|
||||
# Convert from 1-based to 0-based indexing and ensure bounds
|
||||
start_idx = max(0, start_line - 1)
|
||||
end_idx = min(len(lines), end_line)
|
||||
# Convert from 1-based to 0-based indexing and ensure bounds
|
||||
start_idx = max(0, start_line - 1)
|
||||
end_idx = min(len(lines), end_line)
|
||||
|
||||
selected_lines = lines[start_idx:end_idx]
|
||||
filtered_text = "\n".join(selected_lines)
|
||||
# Limit to first 50 lines if more than 50 lines are requested
|
||||
truncation_message = ""
|
||||
if end_idx - start_idx > 50:
|
||||
truncation_message = "\n\n[Truncated after 50 lines! Use narrower line range to view complete section.]"
|
||||
end_idx = start_idx + 50
|
||||
|
||||
# Truncate the text if it's too long
|
||||
if len(filtered_text) > 10000:
|
||||
filtered_text = (
|
||||
filtered_text[:10000]
|
||||
+ "\n\n[Truncated after first 10K characters! Use narrower line range to view complete section.]"
|
||||
)
|
||||
selected_lines = lines[start_idx:end_idx]
|
||||
filtered_text = "\n".join(selected_lines) + truncation_message
|
||||
|
||||
# Format the result as a document reference
|
||||
document_results = [
|
||||
|
||||
@@ -567,7 +567,7 @@ tools_for_research_llm = {
|
||||
To view the contents of specific note or document in the user's personal knowledge base.
|
||||
Especially helpful if the question expects context from the user's notes or documents.
|
||||
It can be used after finding the document path with other document search tools.
|
||||
Specify a line range to view only specific sections of files. Especially useful to read large files.
|
||||
Specify a line range to efficiently read relevant sections of a file. You can view up to 50 lines at a time.
|
||||
"""
|
||||
).strip(),
|
||||
schema={
|
||||
|
||||
Reference in New Issue
Block a user