Enhance display_conversation method: add detailed argument for optional message details and improve formatting for in-memory display. Update export_conversation method for better handling of non-JSON file exports.

pull/866/head
harshalmore31 4 weeks ago
parent 5ceb433992
commit ea6e928934

@ -623,7 +623,11 @@ class Conversation(BaseStructure):
] ]
def display_conversation(self, detailed: bool = False): def display_conversation(self, detailed: bool = False):
"""Display the conversation history.""" """Display the conversation history.
Args:
detailed (bool, optional): Flag to display detailed information. Defaults to False.
"""
if self.backend_instance: if self.backend_instance:
try: try:
return self.backend_instance.display_conversation(detailed) return self.backend_instance.display_conversation(detailed)
@ -632,23 +636,28 @@ class Conversation(BaseStructure):
# Fallback to in-memory display # Fallback to in-memory display
pass pass
# Simple display implementation # In-memory display implementation with proper formatting
print("\n🗨️ Conversation History:") for message in self.conversation_history:
print("=" * 50)
for i, message in enumerate(self.conversation_history):
role = message.get("role", "Unknown")
content = message.get("content", "") content = message.get("content", "")
role = message.get("role", "Unknown")
# Format the message content
if isinstance(content, (dict, list)):
content = json.dumps(content, indent=2)
# Create the display string
display_str = f"{role}: {content}"
# Add details if requested
if detailed: if detailed:
token_count = message.get("token_count", "N/A") display_str += f"\nTimestamp: {message.get('timestamp', 'Unknown')}"
timestamp = message.get("timestamp", "N/A") display_str += f"\nMessage ID: {message.get('message_id', 'Unknown')}"
print(f"\n[{i}] {role}: {content}") if "token_count" in message:
print(f" Tokens: {token_count}, Time: {timestamp}") display_str += (
else: f"\nTokens: {message['token_count']}"
print(f"\n{role}: {content}") )
print("\n" + "=" * 50) formatter.print_panel(display_str)
def export_conversation(self, filename: str, *args, **kwargs): def export_conversation(self, filename: str, *args, **kwargs):
"""Export the conversation history to a file. """Export the conversation history to a file.
@ -664,11 +673,16 @@ class Conversation(BaseStructure):
logger.error(f"Backend export failed: {e}") logger.error(f"Backend export failed: {e}")
# Fallback to in-memory export # Fallback to in-memory export
pass pass
# In-memory export implementation
# If the filename ends with .json, use save_as_json # If the filename ends with .json, use save_as_json
if filename.endswith(".json"): if filename.endswith(".json"):
self.save_as_json(filename) self.save_as_json(filename)
else: else:
self.save_as_json(filename) # Simple text export for non-JSON files
with open(filename, "w",encoding="utf-8") as f:
for message in self.conversation_history:
f.write(f"{message['role']}: {message['content']}\n")
def import_conversation(self, filename: str): def import_conversation(self, filename: str):
"""Import a conversation history from a file. """Import a conversation history from a file.

Loading…
Cancel
Save