AumCoreAI commited on
Commit
5e12eac
·
verified ·
1 Parent(s): 09c18f6

Update core/memory_db.py

Browse files
Files changed (1) hide show
  1. core/memory_db.py +49 -10
core/memory_db.py CHANGED
@@ -29,6 +29,7 @@ class TiDBMemory:
29
  return
30
 
31
  with self.connection.cursor() as cursor:
 
32
  cursor.execute("""
33
  CREATE TABLE IF NOT EXISTS chat_history (
34
  id INT AUTO_INCREMENT PRIMARY KEY,
@@ -38,6 +39,16 @@ class TiDBMemory:
38
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
39
  )
40
  """)
 
 
 
 
 
 
 
 
 
 
41
  cursor.execute("""
42
  CREATE TABLE IF NOT EXISTS code_snippets (
43
  id INT AUTO_INCREMENT PRIMARY KEY,
@@ -50,16 +61,29 @@ class TiDBMemory:
50
  """)
51
  self.connection.commit()
52
 
53
- def save_chat(self, user_input, ai_response, language_mode):
54
  if not self.connection:
55
  return False
56
 
57
  try:
58
  with self.connection.cursor() as cursor:
59
- cursor.execute(
60
- "INSERT INTO chat_history (user_input, ai_response, language_mode) VALUES (%s, %s, %s)",
61
- (user_input, ai_response, language_mode)
62
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  self.connection.commit()
64
  return True
65
  except Exception as e:
@@ -72,11 +96,26 @@ class TiDBMemory:
72
 
73
  try:
74
  with self.connection.cursor() as cursor:
75
- cursor.execute(
76
- "SELECT user_input, ai_response, language_mode FROM chat_history ORDER BY created_at DESC LIMIT %s",
77
- (limit,)
78
- )
79
- return cursor.fetchall()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  except Exception as e:
81
  print(f"❌ Get chats error: {e}")
82
  return []
 
29
  return
30
 
31
  with self.connection.cursor() as cursor:
32
+ # Create chat_history table
33
  cursor.execute("""
34
  CREATE TABLE IF NOT EXISTS chat_history (
35
  id INT AUTO_INCREMENT PRIMARY KEY,
 
39
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
40
  )
41
  """)
42
+
43
+ # Check if language_mode column exists, if not add it
44
+ try:
45
+ cursor.execute("SHOW COLUMNS FROM chat_history LIKE 'language_mode'")
46
+ if not cursor.fetchone():
47
+ print("⚠️ Adding missing 'language_mode' column to chat_history")
48
+ cursor.execute("ALTER TABLE chat_history ADD COLUMN language_mode VARCHAR(20) DEFAULT 'en'")
49
+ except Exception as e:
50
+ print(f"⚠️ Column check failed: {e}")
51
+
52
  cursor.execute("""
53
  CREATE TABLE IF NOT EXISTS code_snippets (
54
  id INT AUTO_INCREMENT PRIMARY KEY,
 
61
  """)
62
  self.connection.commit()
63
 
64
+ def save_chat(self, user_input, ai_response, language_mode="en"):
65
  if not self.connection:
66
  return False
67
 
68
  try:
69
  with self.connection.cursor() as cursor:
70
+ # Try with language_mode column
71
+ try:
72
+ cursor.execute(
73
+ "INSERT INTO chat_history (user_input, ai_response, language_mode) VALUES (%s, %s, %s)",
74
+ (user_input, ai_response, language_mode)
75
+ )
76
+ except pymysql.err.OperationalError as e:
77
+ # If language_mode column doesn't exist, try without it
78
+ if "Unknown column 'language_mode'" in str(e):
79
+ print("⚠️ language_mode column missing, inserting without it")
80
+ cursor.execute(
81
+ "INSERT INTO chat_history (user_input, ai_response) VALUES (%s, %s)",
82
+ (user_input, ai_response)
83
+ )
84
+ else:
85
+ raise e
86
+
87
  self.connection.commit()
88
  return True
89
  except Exception as e:
 
96
 
97
  try:
98
  with self.connection.cursor() as cursor:
99
+ # Try with language_mode column
100
+ try:
101
+ cursor.execute(
102
+ "SELECT user_input, ai_response, language_mode FROM chat_history ORDER BY created_at DESC LIMIT %s",
103
+ (limit,)
104
+ )
105
+ return cursor.fetchall()
106
+ except pymysql.err.OperationalError as e:
107
+ # If language_mode column doesn't exist, select without it
108
+ if "Unknown column 'language_mode'" in str(e):
109
+ print("⚠️ language_mode column missing, selecting without it")
110
+ cursor.execute(
111
+ "SELECT user_input, ai_response FROM chat_history ORDER BY created_at DESC LIMIT %s",
112
+ (limit,)
113
+ )
114
+ # Add default language_mode for compatibility
115
+ rows = cursor.fetchall()
116
+ return [(row[0], row[1], "en") for row in rows]
117
+ else:
118
+ raise e
119
  except Exception as e:
120
  print(f"❌ Get chats error: {e}")
121
  return []