From f483a626b8cd6f1e62c52017256494a4c75500f8 Mon Sep 17 00:00:00 2001 From: Debanjum Date: Tue, 19 Aug 2025 16:37:32 -0700 Subject: [PATCH] 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 --- src/khoj/routers/helpers.py | 52 +++++++++++++++++-------------------- src/khoj/utils/helpers.py | 2 +- 2 files changed, 25 insertions(+), 29 deletions(-) diff --git a/src/khoj/routers/helpers.py b/src/khoj/routers/helpers.py index 292309b4..927f73ef 100644 --- a/src/khoj/routers/helpers.py +++ b/src/khoj/routers/helpers.py @@ -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 = [ diff --git a/src/khoj/utils/helpers.py b/src/khoj/utils/helpers.py index 8f50e3b7..23c6b6ce 100644 --- a/src/khoj/utils/helpers.py +++ b/src/khoj/utils/helpers.py @@ -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={