85 lines
2.2 KiB
Python
85 lines
2.2 KiB
Python
# tools.py
|
|
import os
|
|
import json
|
|
from pathlib import Path
|
|
from typing import List
|
|
from langchain_core.messages import ToolMessage
|
|
|
|
# Global in-memory file store
|
|
_virtual_files = {}
|
|
|
|
# Define the search tool (placeholder implementation)
|
|
# In a real scenario, this would query an external search API
|
|
# For demonstration, we return a static result
|
|
|
|
def search(query: str) -> str:
|
|
"""Simulate a web search and return a short result string."""
|
|
# For simplicity, just return a canned response
|
|
return f"Search result for '{query}': This is a placeholder summary."
|
|
|
|
# Define the write_file tool
|
|
|
|
def write_file(file_path: str, content: str) -> str:
|
|
"""Write content to an in-memory virtual file.
|
|
|
|
Parameters
|
|
----------
|
|
file_path: str
|
|
Path relative to the workspace root.
|
|
content: str
|
|
Text content to write.
|
|
|
|
Returns
|
|
-------
|
|
str
|
|
Confirmation message.
|
|
"""
|
|
_virtual_files[file_path] = content
|
|
return f"File '{file_path}' written successfully."
|
|
|
|
# Define the export_files tool
|
|
|
|
def export_files(export_dir: str) -> str:
|
|
"""Export all virtual files to the real filesystem.
|
|
|
|
Parameters
|
|
----------
|
|
export_dir: str
|
|
Directory path where files should be created.
|
|
|
|
Returns
|
|
-------
|
|
str
|
|
Summary of exported files.
|
|
"""
|
|
Path(export_dir).mkdir(parents=True, exist_ok=True)
|
|
exported = []
|
|
for file_path, content in _virtual_files.items():
|
|
full_path = Path(export_dir) / file_path
|
|
full_path.parent.mkdir(parents=True, exist_ok=True)
|
|
full_path.write_text(content, encoding="utf-8")
|
|
exported.append(str(full_path))
|
|
return f"Exported {len(exported)} files: {', '.join(exported)}"
|
|
|
|
# Expose tool metadata for LangGraph
|
|
search_tool = {
|
|
"name": "search",
|
|
"description": "Search the web for a query and return a short summary.",
|
|
"func": search,
|
|
}
|
|
|
|
write_file_tool = {
|
|
"name": "write_file",
|
|
"description": "Write content to a virtual file.",
|
|
"func": write_file,
|
|
}
|
|
|
|
export_files_tool = {
|
|
"name": "export_files",
|
|
"description": "Export all virtual files to the real filesystem.",
|
|
"func": export_files,
|
|
}
|
|
|
|
# List of tools for LangGraph
|
|
TOOLS = [search_tool, write_file_tool, export_files_tool]
|