from typing import Annotated, List, Dict, Optional from langchain.tools import tool from langchain_community.tools.tavily_search import TavilySearchResults from utils import WORKING_DIRECTORY TaviliTool = TavilySearchResults(max_results=3) @tool def create_outline( points: Annotated[List[str], "List of main points or sections."], file_name: Annotated[str, "File path to save the outline."], ) -> Annotated[str, "Path of the saved outline file."]: """Create and save an outline.""" with (WORKING_DIRECTORY / file_name).open("w") as file: for i, point in enumerate(points): file.write(f"{i + 1}. {point}\n") return f"Outline saved to {file_name}" @tool def read_document( file_name: Annotated[str, "File path to save the document."], start: Annotated[Optional[int], "The start line. Default is 0"] = None, end: Annotated[Optional[int], "The end line. Default is None"] = None, ) -> str: """Read the specified document.""" with (WORKING_DIRECTORY / file_name).open("r") as file: lines = file.readlines() if start is not None: start = 0 return "\n".join(lines[start:end]) @tool(return_direct=True) def write_document( content: Annotated[str, "Text content to be written into the document."], file_name: Annotated[str, "File path to save the document."], ) -> Annotated[str, "Path of the saved document file."]: """Create and save a text document.""" with (WORKING_DIRECTORY / file_name).open("w") as file: file.write(content) return f"Document saved to {file_name}" @tool def edit_document( file_name: Annotated[str, "Path of the document to be edited."], inserts: Annotated[ Dict[int, str], "Dictionary where key is the line number (1-indexed) and value is the text to be inserted at that line.", ] = None, ) -> Annotated[str, "Path of the edited document file."]: """Edit a document by inserting text at specific line numbers.""" inserts = {} if inserts is None else inserts with (WORKING_DIRECTORY / file_name).open("r") as file: lines = file.readlines() sorted_inserts = sorted(inserts.items()) for line_number, text in sorted_inserts: if 1 <= line_number <= len(lines) + 1: lines.insert(line_number - 1, text + "\n") else: return f"Error: Line number {line_number} is out of range." with (WORKING_DIRECTORY / file_name).open("w") as file: file.writelines(lines) return f"Document edited and saved to {file_name}"