fix: add recent dialogue loader to conversation store
Some checks failed
Pre-commit / run (ubuntu-latest) (push) Has been cancelled
Deploy Sphinx documentation to Pages / build_en (ubuntu-latest, 3.10) (push) Has been cancelled
Deploy Sphinx documentation to Pages / build_zh (ubuntu-latest, 3.10) (push) Has been cancelled
Python Unittest Coverage / test (macos-15, 3.10) (push) Has been cancelled
Python Unittest Coverage / test (macos-15, 3.11) (push) Has been cancelled
Python Unittest Coverage / test (macos-15, 3.12) (push) Has been cancelled
Python Unittest Coverage / test (ubuntu-latest, 3.10) (push) Has been cancelled
Python Unittest Coverage / test (ubuntu-latest, 3.11) (push) Has been cancelled
Python Unittest Coverage / test (ubuntu-latest, 3.12) (push) Has been cancelled
Python Unittest Coverage / test (windows-latest, 3.10) (push) Has been cancelled
Python Unittest Coverage / test (windows-latest, 3.11) (push) Has been cancelled
Python Unittest Coverage / test (windows-latest, 3.12) (push) Has been cancelled
Some checks failed
Pre-commit / run (ubuntu-latest) (push) Has been cancelled
Deploy Sphinx documentation to Pages / build_en (ubuntu-latest, 3.10) (push) Has been cancelled
Deploy Sphinx documentation to Pages / build_zh (ubuntu-latest, 3.10) (push) Has been cancelled
Python Unittest Coverage / test (macos-15, 3.10) (push) Has been cancelled
Python Unittest Coverage / test (macos-15, 3.11) (push) Has been cancelled
Python Unittest Coverage / test (macos-15, 3.12) (push) Has been cancelled
Python Unittest Coverage / test (ubuntu-latest, 3.10) (push) Has been cancelled
Python Unittest Coverage / test (ubuntu-latest, 3.11) (push) Has been cancelled
Python Unittest Coverage / test (ubuntu-latest, 3.12) (push) Has been cancelled
Python Unittest Coverage / test (windows-latest, 3.10) (push) Has been cancelled
Python Unittest Coverage / test (windows-latest, 3.11) (push) Has been cancelled
Python Unittest Coverage / test (windows-latest, 3.12) (push) Has been cancelled
This commit is contained in:
@@ -249,3 +249,60 @@ class ConversationStore:
|
|||||||
conn.commit()
|
conn.commit()
|
||||||
finally:
|
finally:
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
|
def get_recent_dialogue(self, customer_key: str, limit: int = 24) -> list[dict[str, Any]]:
|
||||||
|
"""
|
||||||
|
拉取最近对话(按 customer_key 隔离,即 店铺+客户)。
|
||||||
|
仅返回 customer_message / assistant_message 两类事件。
|
||||||
|
"""
|
||||||
|
t_e = self.events_table
|
||||||
|
n = max(1, int(limit))
|
||||||
|
conn = self._conn()
|
||||||
|
try:
|
||||||
|
with conn.cursor() as c:
|
||||||
|
if self.backend == "mysql":
|
||||||
|
c.execute(
|
||||||
|
f"""
|
||||||
|
SELECT event, payload_json
|
||||||
|
FROM {t_e}
|
||||||
|
WHERE customer_key=%s
|
||||||
|
AND event IN ('customer_message','assistant_message')
|
||||||
|
ORDER BY id DESC
|
||||||
|
LIMIT %s
|
||||||
|
""",
|
||||||
|
(customer_key, n),
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
c.execute(
|
||||||
|
f"""
|
||||||
|
SELECT event, payload_json
|
||||||
|
FROM {t_e}
|
||||||
|
WHERE customer_key=?
|
||||||
|
AND event IN ('customer_message','assistant_message')
|
||||||
|
ORDER BY id DESC
|
||||||
|
LIMIT ?
|
||||||
|
""",
|
||||||
|
(customer_key, n),
|
||||||
|
)
|
||||||
|
rows = c.fetchall() or []
|
||||||
|
|
||||||
|
out: list[dict[str, Any]] = []
|
||||||
|
for row in reversed(rows):
|
||||||
|
if isinstance(row, dict):
|
||||||
|
ev = str(row.get("event", "") or "")
|
||||||
|
raw = row.get("payload_json")
|
||||||
|
else:
|
||||||
|
ev = str(row[0] or "")
|
||||||
|
raw = row[1]
|
||||||
|
try:
|
||||||
|
payload = raw if isinstance(raw, dict) else json.loads(raw or "{}")
|
||||||
|
except Exception:
|
||||||
|
payload = {}
|
||||||
|
msg = str(payload.get("msg", "") or "").strip()
|
||||||
|
if not msg:
|
||||||
|
continue
|
||||||
|
role = "user" if ev == "customer_message" else "assistant"
|
||||||
|
out.append({"role": role, "text": msg})
|
||||||
|
return out
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
|||||||
Reference in New Issue
Block a user