import sys import os import base64 import subprocess if len(sys.argv) < 2: print("Usage: python3 img2json.py '/path/to/image.png' [TX_ID] ['Agent Name']") sys.exit(1) file_path = sys.argv[1] if not os.path.exists(file_path): print(f"File not found: {file_path}") sys.exit(1) raw_tx = "" if len(sys.argv) >= 3: # Strip everything down to just the raw alphanumeric part raw_tx = sys.argv[2].replace('[', '').replace(']', '').replace('TX_ID:', '').replace('TX_', '').strip() # Force it into the exact format we need raw_tx = f"TX_{raw_tx}" agent_name = sys.argv[3] if len(sys.argv) >= 4 else None # If agent name is provided, write directly to SSD cache if agent_name and raw_tx: # Explicitly target the external SSD first cache_dir = "/Volumes/FACTORY_SSD/warezfactory/image_cache" # Fallback to local drive if the SSD isn't mounted if not os.path.exists(cache_dir): factory_home = os.environ.get("FACTORY_HOME", os.path.expanduser("~/warezfactory")) cache_dir = os.path.join(factory_home, "image_cache") if not os.path.exists(cache_dir): os.makedirs(cache_dir) cache_file = os.path.join(cache_dir, f"chat_img_{agent_name}_{raw_tx}_0.dat") with open(file_path, "rb") as f_in, open(cache_file, "wb") as f_out: f_out.write(f_in.read()) print(f"✅ Image restored to cache: {cache_file}") print("You can now run Wormhole Sync. The engine will find the image naturally and distribute it!") sys.exit(0) # (Fallback: Copy to clipboard if no agent name is provided) ext = os.path.splitext(file_path)[1].lower() if ext in ['.jpg', '.jpeg']: mime_type = "image/jpeg" elif ext == '.webp': mime_type = "image/webp" elif ext == '.gif': mime_type = "image/gif" else: mime_type = "image/png" with open(file_path, "rb") as img_file: b64_string = base64.b64encode(img_file.read()).decode('utf-8') tx_id_part = "" if raw_tx: tx_id_part = f""",\n "parts": [\n {{ "text": "[TX_ID: {raw_tx}]" }}\n ]""" json_output = f"""{{ "role": "user", "inlineImage": {{ "data": "{b64_string}", "mimeType": "{mime_type}" }}{tx_id_part} }},""" print("✅ JSON chunk generated. Attempting to copy to clipboard...") try: process = subprocess.Popen('pbcopy', env={'LANG': 'en_US.UTF-8'}, stdin=subprocess.PIPE) process.communicate(json_output.encode('utf-8')) print("📋 Successfully copied to clipboard!") except Exception as e: print(json_output)