walidsobhie-code commited on
Commit
6807e9a
·
1 Parent(s): 31f5bef

Add persistent chat history to ~/.stack-2.9/chat_history.json

Browse files
Files changed (1) hide show
  1. src/stack-2.9-cli.py +44 -1
src/stack-2.9-cli.py CHANGED
@@ -25,6 +25,10 @@ from pattern_miner import PatternMiner
25
  from data_quality import DataQualityAnalyzer
26
 
27
 
 
 
 
 
28
  @dataclass
29
  class ChatMessage:
30
  """Chat message for display."""
@@ -32,6 +36,13 @@ class ChatMessage:
32
  content: str
33
  timestamp: str = ""
34
 
 
 
 
 
 
 
 
35
 
36
  class Stack29TUI:
37
  """Terminal User Interface for Stack 2.9"""
@@ -42,6 +53,30 @@ class Stack29TUI:
42
  self.model = os.environ.get("MODEL_NAME", "")
43
  self.chat_history: List[ChatMessage] = []
44
  self.pattern_miner = PatternMiner()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
  def clear_screen(self):
47
  """Clear terminal screen."""
@@ -152,9 +187,13 @@ class Stack29TUI:
152
  if not user_input:
153
  continue
154
  if user_input.lower() in ["exit", "quit"]:
 
155
  break
156
  if user_input.lower() == "clear":
157
  messages = [system_msg]
 
 
 
158
  self.print_header()
159
  print("\n💬 Chat Mode (cleared)")
160
  print("-" * 40)
@@ -162,6 +201,7 @@ class Stack29TUI:
162
 
163
  # Add user message
164
  messages.append(ChatMessage(role="user", content=user_input))
 
165
 
166
  # Generate response
167
  try:
@@ -174,7 +214,10 @@ class Stack29TUI:
174
  print(result.text)
175
 
176
  # Add assistant response
177
- messages.append(ChatMessage(role="assistant", content=result.text))
 
 
 
178
 
179
  except Exception as e:
180
  print(f"Error: {e}")
 
25
  from data_quality import DataQualityAnalyzer
26
 
27
 
28
+ HISTORY_DIR = Path.home() / ".stack-2.9"
29
+ HISTORY_FILE = HISTORY_DIR / "chat_history.json"
30
+
31
+
32
  @dataclass
33
  class ChatMessage:
34
  """Chat message for display."""
 
36
  content: str
37
  timestamp: str = ""
38
 
39
+ def to_dict(self) -> dict:
40
+ return {"role": self.role, "content": self.content, "timestamp": self.timestamp}
41
+
42
+ @classmethod
43
+ def from_dict(cls, d: dict) -> "ChatMessage":
44
+ return cls(role=d["role"], content=d["content"], timestamp=d.get("timestamp", ""))
45
+
46
 
47
  class Stack29TUI:
48
  """Terminal User Interface for Stack 2.9"""
 
53
  self.model = os.environ.get("MODEL_NAME", "")
54
  self.chat_history: List[ChatMessage] = []
55
  self.pattern_miner = PatternMiner()
56
+ self._ensure_history_dir()
57
+ self.load_history()
58
+
59
+ def _ensure_history_dir(self):
60
+ HISTORY_DIR.mkdir(parents=True, exist_ok=True)
61
+
62
+ def load_history(self):
63
+ """Load chat history from disk if available."""
64
+ if HISTORY_FILE.exists():
65
+ try:
66
+ with open(HISTORY_FILE, "r") as f:
67
+ data = json.load(f)
68
+ self.chat_history = [ChatMessage.from_dict(m) for m in data]
69
+ print(f"✓ Loaded {len(self.chat_history)} messages from history")
70
+ except Exception as e:
71
+ print(f"Warning: Could not load history: {e}")
72
+
73
+ def save_history(self):
74
+ """Save chat history to disk."""
75
+ try:
76
+ with open(HISTORY_FILE, "w") as f:
77
+ json.dump([m.to_dict() for m in self.chat_history], f, indent=2)
78
+ except Exception as e:
79
+ print(f"Warning: Could not save history: {e}")
80
 
81
  def clear_screen(self):
82
  """Clear terminal screen."""
 
187
  if not user_input:
188
  continue
189
  if user_input.lower() in ["exit", "quit"]:
190
+ self.save_history()
191
  break
192
  if user_input.lower() == "clear":
193
  messages = [system_msg]
194
+ self.chat_history = []
195
+ if HISTORY_FILE.exists():
196
+ HISTORY_FILE.unlink()
197
  self.print_header()
198
  print("\n💬 Chat Mode (cleared)")
199
  print("-" * 40)
 
201
 
202
  # Add user message
203
  messages.append(ChatMessage(role="user", content=user_input))
204
+ self.chat_history.append(ChatMessage(role="user", content=user_input))
205
 
206
  # Generate response
207
  try:
 
214
  print(result.text)
215
 
216
  # Add assistant response
217
+ assistant_msg = ChatMessage(role="assistant", content=result.text)
218
+ messages.append(assistant_msg)
219
+ self.chat_history.append(assistant_msg)
220
+ self.save_history()
221
 
222
  except Exception as e:
223
  print(f"Error: {e}")