diff --git a/gemini/gemini.py b/gemini/gemini.py index 6ba3d8f..807ed84 100644 --- a/gemini/gemini.py +++ b/gemini/gemini.py @@ -34,7 +34,6 @@ def main(): client = genai.Client() context_data = {"file_ids": [], "cache_id": None} - # Load existing context if a context file is specified and exists if args.context and os.path.exists(args.context): try: with open(args.context, "r") as f: @@ -68,11 +67,11 @@ def main(): print("Cleanup complete.") return - # Wrap the main execution in a try/finally to guarantee cleanup on transient runs try: # --------------------------------------------------------- # UPLOAD LOGIC # --------------------------------------------------------- + new_files_added = False if args.files: for file_path in args.files: if not os.path.exists(file_path): @@ -85,6 +84,7 @@ def main(): if uploaded_file.name not in context_data["file_ids"]: context_data["file_ids"].append(uploaded_file.name) + new_files_added = True if args.context: with open(args.context, "w") as f: @@ -94,15 +94,21 @@ def main(): # CACHE CREATION LOGIC # --------------------------------------------------------- system_instruction = ( -# "You are a strict data extraction tool. Output exactly what is requested " -# "(e.g., CSV). Never use markdown formatting blocks (like ```csv). " -# "Never include conversational text, greetings, or explanations. Output raw data only." - "You are a hybrid data extraction tool. If a specific format or file format output is requested " - "(e.g., CSV), then output exactly what was requested and never use markdown formatting blocks (like ```csv). " - "If a specific format was requested, never include conversational text, greetings, or explanations; " - "and output raw data only. If not specific data format is suggested, you can answer with conversational text." + "You are a hybrid data extraction tool. If a specific format or file format output is requested " + "(e.g., CSV), then output exactly what was requested and never use markdown formatting blocks (like ```csv). " + "If a specific format was requested, never include conversational text, greetings, or explanations; " + "and output raw data only. If not specific data format is suggested, you can answer with conversational text." ) + # If new files were added but a cache already exists, we must destroy the stale cache. + if new_files_added and context_data.get("cache_id"): + print("New files detected. Destroying stale cache to rebuild...") + try: + client.caches.delete(name=context_data["cache_id"]) + except Exception as e: + print(f"Warning: Could not delete stale cache. {e}", file=sys.stderr) + context_data["cache_id"] = None + cache_too_small = False file_objects = []