shensuming commited on
Commit
eaefeb2
·
verified ·
1 Parent(s): 609d0ba

Update agent/memory_sync.py

Browse files
Files changed (1) hide show
  1. agent/memory_sync.py +43 -0
agent/memory_sync.py CHANGED
@@ -41,3 +41,46 @@ def get_conversation_messages(conversation_id: str) -> list:
41
  finally:
42
  cur.close()
43
  conn.close()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  finally:
42
  cur.close()
43
  conn.close()
44
+
45
+ import uuid
46
+
47
+ def init_conversation(user_id: str, view: str, intent: str, title: str) -> str:
48
+ """物理建档:生成合法的 UUID 并存入 conversations 表"""
49
+ conn = psycopg2.connect("dbname=cerebratex user=ubuntu")
50
+ cur = conn.cursor()
51
+ new_id = str(uuid.uuid4())
52
+ try:
53
+ cur.execute("""
54
+ INSERT INTO conversations (conversation_id, user_id, view, intent, title)
55
+ VALUES (%s, %s, %s, %s, %s)
56
+ """, (new_id, user_id, view, intent, (title or "新推演")[:50]))
57
+ conn.commit()
58
+ return new_id
59
+ except Exception as e:
60
+ conn.rollback()
61
+ print(f"Init conv error: {e}")
62
+ return new_id
63
+ finally:
64
+ cur.close()
65
+ conn.close()
66
+
67
+ def save_chat_message(conversation_id: str, role: str, content: str):
68
+ """物理落库:将单条消息存入 messages,并刷新会话时间"""
69
+ if not conversation_id or len(str(conversation_id)) < 30: return
70
+ conn = psycopg2.connect("dbname=cerebratex user=ubuntu")
71
+ cur = conn.cursor()
72
+ try:
73
+ cur.execute("""
74
+ INSERT INTO messages (conversation_id, role, content)
75
+ VALUES (%s, %s, %s)
76
+ """, (conversation_id, role, content))
77
+ cur.execute("""
78
+ UPDATE conversations SET updated_at = NOW() WHERE conversation_id = %s
79
+ """, (conversation_id,))
80
+ conn.commit()
81
+ except Exception as e:
82
+ conn.rollback()
83
+ print(f"Save msg error: {e}")
84
+ finally:
85
+ cur.close()
86
+ conn.close()