MCPcopy Create free account
hub / github.com/OpenRaiser/PaperFlow / save_chat_message

Function save_chat_message

skills/storage-helper/scripts/db_ops.py:329–410  ·  view source on GitHub ↗

Persist a GUI chat message and update its parent session summary.

(
    user_id: str,
    session_id: str,
    role: str,
    content: str,
    metadata: Optional[Dict[str, Any]] = None,
)

Source from the content-addressed store, hash-verified

327 conn = get_connection()
328 cursor = conn.cursor()
329 _create_chat_tables(cursor)
330 conn.commit()
331 conn.close()
332
333
334def _chat_now() -> str:
335 return datetime.now().isoformat(sep=" ", timespec="seconds")
336
337
338def _chat_title_from_content(content: str, max_chars: int = 48) -> str:
339 title = " ".join(str(content or "").split())
340 if not title:
341 return "新对话"
342 if len(title) <= max_chars:
343 return title
344 return title[: max(1, max_chars - 3)].rstrip() + "..."
345
346
347def _chat_session_row(row: sqlite3.Row) -> Dict[str, Any]:
348 return {
349 "session_id": row["session_id"],
350 "user_id": row["user_id"],
351 "title": row["title"],
352 "created_at": row["created_at"],
353 "updated_at": row["updated_at"],
354 "message_count": int(row["message_count"] or 0),
355 }
356
357
358def create_chat_session(
359 user_id: str,
360 title: str = "",
361 session_id: Optional[str] = None,
362) -> Dict[str, Any]:
363 """Create or return a persistent GUI chat session."""
364 normalized_user_id = _normalize_identifier(user_id)
365 if not normalized_user_id:
366 raise ValueError("user_id is required")
367
368 ensure_chat_tables()
369 normalized_session_id = _normalize_identifier(session_id) or f"chat_{datetime.now().strftime('%Y%m%d_%H%M%S')}_{uuid.uuid4().hex[:8]}"
370 normalized_title = _chat_title_from_content(title)
371 now = _chat_now()
372
373 conn = get_connection()
374 cursor = conn.cursor()
375 cursor.execute(
376 """
377 INSERT OR IGNORE INTO chat_sessions (session_id, user_id, title, created_at, updated_at, message_count)
378 VALUES (?, ?, ?, ?, ?, 0)
379 """,
380 (normalized_session_id, normalized_user_id, normalized_title, now, now),
381 )
382 cursor.execute(
383 """
384 SELECT session_id, user_id, title, created_at, updated_at, message_count
385 FROM chat_sessions
386 WHERE user_id = ? AND session_id = ?

Callers

nothing calls this directly

Calls 11

_normalize_identifierFunction · 0.85
ensure_chat_tablesFunction · 0.85
_chat_nowFunction · 0.85
_chat_title_from_contentFunction · 0.85
_load_json_metadataFunction · 0.85
cursorMethod · 0.80
getMethod · 0.80
create_chat_sessionFunction · 0.70
get_connectionFunction · 0.70
executeMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected