mirror of
https://github.com/lfnovo/open-notebook.git
synced 2026-04-28 19:40:50 +00:00
feat: message counting for chat sessions (#430)
This commit is contained in:
parent
5621066123
commit
9adf70d18d
3 changed files with 69 additions and 33 deletions
18
open_notebook/utils/graph_utils.py
Normal file
18
open_notebook/utils/graph_utils.py
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
from langchain_core.runnables import RunnableConfig
|
||||
from loguru import logger
|
||||
|
||||
async def get_session_message_count(graph, session_id: str) -> int:
|
||||
"""Get message count from LangGraph state, returns 0 on error."""
|
||||
try:
|
||||
thread_state = await graph.aget_state( # async version
|
||||
config=RunnableConfig(configurable={"thread_id": session_id})
|
||||
)
|
||||
if (
|
||||
thread_state
|
||||
and thread_state.values
|
||||
and "messages" in thread_state.values
|
||||
):
|
||||
return len(thread_state.values["messages"])
|
||||
except Exception as e:
|
||||
logger.warning(f"Could not fetch message count for session {session_id}: {e}")
|
||||
return 0
|
||||
Loading…
Add table
Add a link
Reference in a new issue