Execute the export command.
(self, **kwargs)
| 63 | ] |
| 64 | |
| 65 | async def execute(self, **kwargs) -> CommandResult: |
| 66 | """Execute the export command.""" |
| 67 | from mcp_cli.chat.exporters import MarkdownExporter, JSONExporter |
| 68 | |
| 69 | chat_context = kwargs.get("chat_context") |
| 70 | if not chat_context: |
| 71 | return CommandResult(success=False, error="No chat context available.") |
| 72 | |
| 73 | # Parse arguments |
| 74 | args = kwargs.get("args", "").strip().split() |
| 75 | export_format = args[0] if args else "markdown" |
| 76 | filename = args[1] if len(args) > 1 else None |
| 77 | |
| 78 | # Get messages from context |
| 79 | messages = [] |
| 80 | try: |
| 81 | raw_messages = chat_context.get_conversation_history() |
| 82 | messages = [ |
| 83 | m.to_dict() if hasattr(m, "to_dict") else m for m in raw_messages |
| 84 | ] |
| 85 | except Exception as e: |
| 86 | return CommandResult(success=False, error=f"Failed to get history: {e}") |
| 87 | |
| 88 | if not messages: |
| 89 | output.info("No messages to export.") |
| 90 | return CommandResult(success=True, output="No messages to export.") |
| 91 | |
| 92 | # Build metadata |
| 93 | metadata = { |
| 94 | "session_id": getattr(chat_context, "session_id", "unknown"), |
| 95 | "provider": getattr(chat_context, "provider", "unknown"), |
| 96 | "model": getattr(chat_context, "model", "unknown"), |
| 97 | } |
| 98 | |
| 99 | # Build token usage |
| 100 | token_usage = None |
| 101 | tracker = getattr(chat_context, "token_tracker", None) |
| 102 | if tracker and tracker.turn_count > 0: |
| 103 | token_usage = { |
| 104 | "total_input": tracker.total_input, |
| 105 | "total_output": tracker.total_output, |
| 106 | "total_tokens": tracker.total_tokens, |
| 107 | "turn_count": tracker.turn_count, |
| 108 | } |
| 109 | |
| 110 | # Export |
| 111 | session_id = getattr(chat_context, "session_id", "chat") |
| 112 | if export_format.startswith("json"): |
| 113 | content = JSONExporter.export(messages, metadata, token_usage) |
| 114 | if not filename: |
| 115 | filename = f"chat-{session_id}.json" |
| 116 | else: |
| 117 | content = MarkdownExporter.export(messages, metadata) |
| 118 | if not filename: |
| 119 | filename = f"chat-{session_id}.md" |
| 120 | |
| 121 | # Write to file |
| 122 | try: |
nothing calls this directly
no test coverage detected