#!/usr/bin/env python3 """ Helper script to generate properly formatted outbox.yaml entries. Prevents schema errors by enforcing correct structure. """ import sys import yaml def generate_reply(target_id, text, annotation_text=None, quote=None, memory_handle=None, memory_facts=None): """Generate a reply action with proper schema.""" action = { "action": "reply", "target_id": target_id, "posts": [text] # Always use posts array } if annotation_text: action["annotation"] = {"text": annotation_text} if quote: action["annotation"]["quote"] = quote if memory_handle and memory_facts: action["memory_update"] = { "target_handle": memory_handle, "facts": memory_facts if isinstance(memory_facts, list) else [memory_facts] } return action def generate_post(texts, standalone=True, annotation_text=None): """Generate a post action (standalone or thread).""" if isinstance(texts, str): texts = [texts] action = { "action": "post", "posts": texts } if annotation_text: action["annotation"] = {"text": annotation_text} config = {"standalone_post": standalone} return config, action def generate_annotate(target_id, text, motivation="commenting", quote=None): """Generate a standalone annotation action.""" action = { "action": "annotate", "target_id": target_id, "text": text, "motivation": motivation } if quote: action["quote"] = quote return action def main(): if len(sys.argv) < 2: print("Usage: generate_outbox.py [args]") print("\nCommands:") print(" reply [annotation]") print(" post [annotation]") print(" thread ...") print(" annotate ") sys.exit(1) command = sys.argv[1] if command == "reply": if len(sys.argv) < 4: print("Usage: generate_outbox.py reply [annotation]") sys.exit(1) target_id = sys.argv[2] text = sys.argv[3] annotation = sys.argv[4] if len(sys.argv) > 4 else None action = generate_reply(target_id, text, annotation) outbox = {"actions": [action]} print(yaml.dump(outbox, sort_keys=False)) elif command == "post": if len(sys.argv) < 3: print("Usage: generate_outbox.py post [annotation]") sys.exit(1) text = sys.argv[2] annotation = sys.argv[3] if len(sys.argv) > 3 else None config, action = generate_post(text, standalone=True, annotation_text=annotation) outbox = {"config": config, "actions": [action]} print(yaml.dump(outbox, sort_keys=False)) elif command == "thread": if len(sys.argv) < 3: print("Usage: generate_outbox.py thread ...") sys.exit(1) texts = sys.argv[2:] config, action = generate_post(texts, standalone=True) outbox = {"config": config, "actions": [action]} print(yaml.dump(outbox, sort_keys=False)) elif command == "annotate": if len(sys.argv) < 4: print("Usage: generate_outbox.py annotate ") sys.exit(1) target_id = sys.argv[2] text = sys.argv[3] action = generate_annotate(target_id, text) outbox = {"actions": [action]} print(yaml.dump(outbox, sort_keys=False)) else: print(f"Unknown command: {command}") sys.exit(1) if __name__ == "__main__": main()