feat: opt-in state.db sync for /insights visibility (#92)
WebUI sessions were invisible to 'hermes /insights' because the WebUI bypasses the gateway and calls AIAgent.run_conversation() directly, never writing to state.db. New 'Sync usage to /insights' setting (default: off) that mirrors WebUI session metadata (tokens, cost, model, title) into state.db after each turn. Uses absolute token counts to avoid double-counting. Components: - api/state_sync.py: bridge module with sync_session_start() and sync_session_usage(). Uses ensure_session() (idempotent) and update_token_counts(absolute=True). All wrapped in try/except. - api/config.py: new 'sync_to_insights' boolean setting - api/streaming.py: calls sync_session_usage() after s.save() - api/routes.py: same for the non-streaming chat path - Settings UI: checkbox toggle with description Default off because: - Writing to state.db while CLI/gateway also writes could cause WAL lock contention on busy systems - Some users may not want WebUI sessions in /insights stats Closes #92 Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -652,6 +652,7 @@ _SETTINGS_DEFAULTS = {
|
||||
'send_key': 'enter', # 'enter' or 'ctrl+enter'
|
||||
'show_token_usage': False, # show input/output token badge below assistant messages
|
||||
'show_cli_sessions': False, # merge CLI sessions from state.db into the sidebar
|
||||
'sync_to_insights': False, # mirror WebUI token usage to state.db for /insights
|
||||
'password_hash': None, # SHA-256 hash; None = auth disabled
|
||||
}
|
||||
|
||||
@@ -671,7 +672,7 @@ _SETTINGS_ALLOWED_KEYS = set(_SETTINGS_DEFAULTS.keys()) - {'password_hash'}
|
||||
_SETTINGS_ENUM_VALUES = {
|
||||
'send_key': {'enter', 'ctrl+enter'},
|
||||
}
|
||||
_SETTINGS_BOOL_KEYS = {'show_token_usage', 'show_cli_sessions'}
|
||||
_SETTINGS_BOOL_KEYS = {'show_token_usage', 'show_cli_sessions', 'sync_to_insights'}
|
||||
|
||||
def save_settings(settings: dict) -> dict:
|
||||
"""Save settings to disk. Returns the merged settings. Ignores unknown keys."""
|
||||
|
||||
Reference in New Issue
Block a user