Skip to content

Commit 54adbe2

Browse files
committed
feature: more robust user text extraction, include selections
1 parent 29f9942 commit 54adbe2

File tree

1 file changed

+50
-4
lines changed

1 file changed

+50
-4
lines changed

src/export.py

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import re
22
import shutil
33
import os
4+
import json
45
from abc import ABC, abstractmethod
56
from typing import Any
67
from loguru import logger
8+
import traceback
79

810
class ChatFormatter(ABC):
911
@abstractmethod
@@ -41,26 +43,70 @@ def format(self, chat_data: dict[str, Any], image_dir: str | None = 'images') ->
4143

4244
for bubble in bubbles:
4345
if bubble['type'] == 'user':
44-
formatted_chat.append(f"## User:\n\n{bubble['delegate']['a']}\n")
46+
user_text = ["## User:\n\n"]
47+
48+
if "selections" in bubble and bubble["selections"]:
49+
user_text.append(f"[selections] \n{"\n".join([s["text"] for s in bubble['selections']])}")
50+
4551
if 'image' in bubble and tab_image_dir is not None:
4652
image_path = bubble['image']['path']
4753
image_filename = os.path.basename(image_path)
4854
new_image_path = os.path.join(tab_image_dir, image_filename)
4955
shutil.copy(image_path, new_image_path)
50-
formatted_chat.append(f"![User Image]({new_image_path})\n")
56+
user_text.append(f"[image] \n![User Image]({new_image_path})")
57+
58+
if "delegate" in bubble:
59+
if bubble["delegate"]:
60+
user_text_text = bubble['delegate']["a"]
61+
else:
62+
user_text_text = ""
63+
elif "text" in bubble:
64+
if bubble["text"]:
65+
user_text_text = bubble['text']
66+
else:
67+
user_text_text = ""
68+
elif "initText" in bubble:
69+
if bubble["initText"]:
70+
user_text_text = bubble['rawText']
71+
user_text_text = json.loads(bubble["initText"])['root']['children'][0]['children'][0]['text']
72+
else:
73+
user_text_text = ""
74+
elif "rawText" in bubble:
75+
if bubble["rawText"]:
76+
user_text_text = bubble['text']
77+
else:
78+
user_text_text = ""
79+
else:
80+
user_text_text = "[ERROR: no user text found]"
81+
logger.error(f"Couldn't find user text entry in one of the bubbles.")
82+
logger.debug(f"Bubble:\n{json.dumps(bubble, indent=4)}")
83+
if user_text_text:
84+
user_text.append(f"[text] \n{user_text_text}")
85+
86+
user_text.append("\n")
87+
88+
if len(user_text) > 2:
89+
formatted_chat.append("\n".join(user_text))
5190
elif bubble['type'] == 'ai':
5291
model_type = bubble.get('modelType', 'Unknown')
5392
raw_text = re.sub(r'```python:[^\n]+', '```python', bubble['rawText'])
5493
formatted_chat.append(f"## AI ({model_type}):\n\n{raw_text}\n")
5594

5695
formatted_chats.append("\n".join(formatted_chat))
5796

97+
logger.success("Chats formatted.")
5898
return formatted_chats
5999
except KeyError as e:
60-
logger.error(f"KeyError: {e}")
100+
logger.error(f"KeyError: Missing key {e} in chat data. Ensure the data structure is correct. Full error: {e}")
61101
return [f"Error: Missing key {e}"]
102+
except FileNotFoundError as e:
103+
logger.error(f"FileNotFoundError: {e}. Ensure the image paths are correct. Full error: {e}")
104+
return [f"Error: {e}"]
105+
except json.JSONDecodeError as e:
106+
logger.error(f"JSONDecodeError: {e}. Ensure the JSON data is correctly formatted. Full error: {e}")
107+
return [f"Error: {e}"]
62108
except Exception as e:
63-
logger.error(f"Unexpected error: {e}")
109+
logger.error(f"Unexpected error: {e}. Full traceback: {traceback.format_exc()}")
64110
return [f"Error: {e}"]
65111

66112
class FileSaver(ABC):

0 commit comments

Comments
 (0)