From 45c97a34ae234cce639ee4fb24363b8219f840be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B8=D1=80=D0=B8=D0=BB=D0=BB=20=D0=9A=D1=83=D1=82?= =?UTF-8?q?=D0=BB=D0=B0=D1=85=D0=BC=D0=B5=D1=82=D0=BE=D0=B2?= Date: Tue, 26 May 2026 13:00:18 +0000 Subject: [PATCH] add tools.py --- tools.py | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 tools.py diff --git a/tools.py b/tools.py new file mode 100644 index 0000000..080e39f --- /dev/null +++ b/tools.py @@ -0,0 +1,63 @@ +import os +import json +from typing import Dict +from duckduckgo_search import DDGS + +# In‑memory virtual file system +VIRTUAL_FS: Dict[str, str] = {} + + +def web_search(query: str) -> str: + """Search the web via DuckDuckGo and return top‑5 results. + + Each result is formatted as ``Title`` + ``Snippet`` + ``URL``. + The function returns a single string with results separated by newlines. + """ + results = [] + with DDGS() as ddgs: + for r in ddgs.text(query, max_results=5): + title = r.get("title", "") + body = r.get("body", "") + href = r.get("href", "") + results.append(f"{title}\n{body}\n{href}") + return "\n\n".join(results) + + +def create_virtual_file(filename: str, content: str) -> str: + """Create or overwrite a file in the virtual file system. + + Parameters + ---------- + filename: str + Name of the file to create. + content: str + File content. + """ + VIRTUAL_FS[filename] = content + return f"File {filename} created with {len(content)} characters." + + +def list_virtual_files() -> str: + """Return a list of filenames stored in the virtual file system.""" + if not VIRTUAL_FS: + return "No files in virtual FS." + return "\n".join(sorted(VIRTUAL_FS.keys())) + + +def export_files(output_dir: str = "output") -> str: + """Export all virtual files to the local filesystem. + + Parameters + ---------- + output_dir: str + Directory where files will be written. The directory is created if it + does not exist. + """ + os.makedirs(output_dir, exist_ok=True) + for fname, content in VIRTUAL_FS.items(): + path = os.path.join(output_dir, fname) + with open(path, "w", encoding="utf-8") as f: + f.write(content) + return f"Exported {len(VIRTUAL_FS)} files to {output_dir}." + +# End of tools module