Spaces:
Runtime error
Runtime error
Update storage/storage_manager.py
Browse files- storage/storage_manager.py +40 -2
storage/storage_manager.py
CHANGED
|
@@ -32,6 +32,8 @@ class UserStorageManager:
|
|
| 32 |
chat_title = generate_chat_name(chat_data, claude_service)
|
| 33 |
|
| 34 |
chat_id = filename or f"chat_{formatted_timestamp}"
|
|
|
|
|
|
|
| 35 |
|
| 36 |
# Save chat data
|
| 37 |
chat_file = self.paths["chats"] / f"{chat_id}.json"
|
|
@@ -44,12 +46,48 @@ class UserStorageManager:
|
|
| 44 |
'title': chat_title or f"Trading Analysis {formatted_timestamp}",
|
| 45 |
'data': chat_data
|
| 46 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
def get_all_chats(self):
|
| 49 |
"""Get all user's chats with metadata"""
|
| 50 |
try:
|
| 51 |
chats = []
|
| 52 |
-
for
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
try:
|
| 54 |
with open(chat_file, "r") as f:
|
| 55 |
chat_data = json.load(f)
|
|
@@ -100,7 +138,7 @@ class UserStorageManager:
|
|
| 100 |
try:
|
| 101 |
context_file = self.paths["context"] / "context.json"
|
| 102 |
with open(context_file, "w") as f:
|
| 103 |
-
json.dump(context_data, f)
|
| 104 |
except Exception as e:
|
| 105 |
st.error(f"Error saving context: {str(e)}")
|
| 106 |
|
|
|
|
| 32 |
chat_title = generate_chat_name(chat_data, claude_service)
|
| 33 |
|
| 34 |
chat_id = filename or f"chat_{formatted_timestamp}"
|
| 35 |
+
if filename:
|
| 36 |
+
chat_id = filename.replace('.json', '')
|
| 37 |
|
| 38 |
# Save chat data
|
| 39 |
chat_file = self.paths["chats"] / f"{chat_id}.json"
|
|
|
|
| 46 |
'title': chat_title or f"Trading Analysis {formatted_timestamp}",
|
| 47 |
'data': chat_data
|
| 48 |
}
|
| 49 |
+
|
| 50 |
+
# Save chat metadata
|
| 51 |
+
with open(chat_file, "w") as f:
|
| 52 |
+
json.dump(chat_metadata, f, indent=4)
|
| 53 |
+
|
| 54 |
+
# Save associated images
|
| 55 |
+
if images:
|
| 56 |
+
image_dir = self.paths["images"] / chat_id
|
| 57 |
+
image_dir.mkdir(exist_ok=True)
|
| 58 |
+
|
| 59 |
+
if isinstance(images, list):
|
| 60 |
+
for idx, img_data in enumerate(images):
|
| 61 |
+
img_file = image_dir / f"image_{idx}.png"
|
| 62 |
+
with open(img_file, "wb") as f:
|
| 63 |
+
f.write(img_data)
|
| 64 |
+
else:
|
| 65 |
+
img_file = image_dir / "image_0.png"
|
| 66 |
+
with open(img_file, "wb") as f:
|
| 67 |
+
f.write(images)
|
| 68 |
+
|
| 69 |
+
# Update chat metadata with image references
|
| 70 |
+
chat_metadata['image_paths'] = [str(p) for p in image_dir.glob("*.png")]
|
| 71 |
+
with open(chat_file, "w") as f:
|
| 72 |
+
json.dump(chat_metadata, f, indent=4)
|
| 73 |
+
|
| 74 |
+
return chat_id
|
| 75 |
+
|
| 76 |
+
except Exception as e:
|
| 77 |
+
st.error(f"Error saving chat: {str(e)}")
|
| 78 |
+
return None
|
| 79 |
|
| 80 |
def get_all_chats(self):
|
| 81 |
"""Get all user's chats with metadata"""
|
| 82 |
try:
|
| 83 |
chats = []
|
| 84 |
+
# Sort chat files by modification time for most recent first
|
| 85 |
+
chat_files = sorted(
|
| 86 |
+
self.paths["chats"].glob("*.json"),
|
| 87 |
+
key=lambda x: x.stat().st_mtime,
|
| 88 |
+
reverse=True
|
| 89 |
+
)
|
| 90 |
+
for chat_file in chat_files:
|
| 91 |
try:
|
| 92 |
with open(chat_file, "r") as f:
|
| 93 |
chat_data = json.load(f)
|
|
|
|
| 138 |
try:
|
| 139 |
context_file = self.paths["context"] / "context.json"
|
| 140 |
with open(context_file, "w") as f:
|
| 141 |
+
json.dump(context_data, f, indent=4)
|
| 142 |
except Exception as e:
|
| 143 |
st.error(f"Error saving context: {str(e)}")
|
| 144 |
|