fix: sync message_count to state.db for /insights (#163) (#164)

* fix: sync message_count to state.db for /insights (#163)

sync_session_usage() didn't write message_count to state.db, so
/insights showed 0 messages for all WebUI sessions even with
sync_to_insights enabled.

Added message_count parameter to sync_session_usage() and pass
len(s.messages) from both the streaming and non-streaming chat paths.

Fixes #163

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix: use callable pattern for _execute_write in sync_session_usage

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Nathan Esquenazi
2026-04-06 22:56:27 -07:00
committed by GitHub
parent 4d2887531d
commit 8aa1c9684d
3 changed files with 15 additions and 1 deletions

View File

@@ -1094,6 +1094,7 @@ def _handle_chat_sync(handler, body):
estimated_cost=s.estimated_cost,
model=s.model,
title=s.title,
message_count=len(s.messages),
)
except Exception:
pass

View File

@@ -66,7 +66,8 @@ def sync_session_start(session_id: str, model=None) -> None:
def sync_session_usage(session_id: str, input_tokens: int=0, output_tokens: int=0,
estimated_cost=None, model=None, title: str=None) -> None:
estimated_cost=None, model=None, title: str=None,
message_count: int=None) -> None:
"""Update token usage and title for a WebUI session in state.db.
Called after each turn completes. Uses absolute=True to set totals
(the WebUI Session already accumulates across turns).
@@ -92,6 +93,17 @@ def sync_session_usage(session_id: str, input_tokens: int=0, output_tokens: int=
db.set_session_title(session_id, title)
except Exception:
pass
# Update message count
if message_count is not None:
try:
def _set_msg_count(conn):
conn.execute(
"UPDATE sessions SET message_count = ? WHERE id = ?",
(message_count, session_id),
)
db._execute_write(_set_msg_count)
except Exception:
pass
except Exception:
pass # never crash the WebUI for sync failures
finally:

View File

@@ -366,6 +366,7 @@ def _run_agent_streaming(session_id, msg_text, model, workspace, stream_id, atta
estimated_cost=s.estimated_cost,
model=model,
title=s.title,
message_count=len(s.messages),
)
except Exception:
pass # never crash the stream for sync failures