monitoring_db#
- class baf.db.monitoring_db.MonitoringDB[source]#
Bases:
objectThis class is an interface to connect to a database where user interactions with the agent are stored to monitor the agent for later analysis.
- conn#
The connection to the monitoring database
- Type:
sqlalchemy.Connection
- connect_to_db(agent)[source]#
Connect to the monitoring database.
- Parameters:
agent (Agent) – The agent that contains the database-related properties.
- delete_session(session)[source]#
Deletes the session information, chat messages, and transitions related to the given session from the monitoring database.
- Parameters:
session (Session) – The session to delete.
- get_last_state_of_session(agent_name, platform_name, session_id)[source]#
Retrieves the last dest_state for a given session from the transition table.
- get_table(table_name)[source]#
Gets all the content of a database table (i.e., SELECT * FROM table_name).
- Parameters:
table_name – the name of the table
- Returns:
the table in a dataframe
- Return type:
- insert_chat(session, message)[source]#
Insert a new record into the chat table of the monitoring database.
- insert_event(session, event)[source]#
Insert a new record into the event table of the monitoring database.
- insert_intent_prediction(session, state, predicted_intent)[source]#
Insert a new intent prediction record into the intent predictions table of the monitoring database.
- Parameters:
session (Session) – the session containing the predicted intent to insert into the database
state (State) – the state where the intent prediction took place (the session’s current state may have changed since the intent prediction, so we need it as argument)
predicted_intent (IntentClassifierPrediction) – the intent prediction
- insert_reasoning_event(session, kind, payload)[source]#
Insert a row into the reasoning_step table.
- Parameters:
session (Session) – the session that produced the event.
kind (str) – one of
REASONING_KIND_STEP/REASONING_KIND_TASK_LIST_UPDATE.payload (Any) – for
REASONING_KIND_STEPtheReasoningStepdict ({"kind", "step", "summary", "details"}); forREASONING_KIND_TASK_LIST_UPDATEthe list of task dicts ([{"id", "description", "status", "result"}, ...]).
- Returns:
the inserted row id, or None on failure.
- Return type:
int | None
- insert_session(session)[source]#
Insert a new session record into the sessions table of the monitoring database.
- Parameters:
session (Session) – the session to insert into the database
- insert_transition(session, transition)[source]#
Insert a new transition record into the transitions table of the monitoring database.
- Parameters:
session (Session) – the session the transition belongs to
transition (Transition) – the transition to insert into the database
- link_pending_reasoning_events(session)[source]#
Attach all reasoning events with chat_id IS NULL to the most recent chat row for the session.
Best-effort: if the reasoning_step or chat tables don’t exist yet, or no chat row exists for the session, the method does nothing and returns 0. Called once per reasoning loop after the final
session.reply().- Returns:
number of rows linked.
- Return type:
- load_session_variables(session)[source]#
Loads the session variables from the monitoring database, transforms the JSON string into a dictionary, and sets each key-value pair in the session using session.set(key, value).
- Parameters:
session (Session) – The session whose variables should be loaded.
- run_statement(stmt)[source]#
Executes a SQL statement.
- Parameters:
stmt (sqlalchemy.Executable) – the SQL statement
- Returns:
the result of the SQL statement
- Return type:
sqlalchemy.CursorResult[Any] | None
- select_chat(session, n=None, until_timestamp=None)[source]#
Retrieves chat records from the chat table of the database for a given session.
- Parameters:
- Returns:
the chat records for the given session
- Return type:
- select_reasoning_events(session, until_timestamp=None)[source]#
Retrieve all reasoning-state events for a session, in chronological order.
- Parameters:
session (Session) – the session whose events to fetch.
until_timestamp (Optional[datetime]) – if provided, only events at or before this timestamp are returned.
- Returns:
rows with columns
id,session_id,chat_id,kind,step_kind,step,summary,payload,timestamp. Empty if the table doesn’t exist (e.g. older DB without the migration applied).- Return type:
- select_session(session)[source]#
Retrieves a session record from the sessions table of the database.
- Parameters:
session (Session) – the session to get from the database
- Returns:
the session record, should be a 1 row DataFrame
- Return type:
- baf.db.monitoring_db.REASONING_KIND_STEP = 'reasoning_step'#
kind value for a reasoning step event (LLM tool call, tool result, push-back, task add/complete/skip, max_steps, reasoning_started/finished).
- baf.db.monitoring_db.REASONING_KIND_TASK_LIST_UPDATE = 'task_list_update'#
kind value for a task list snapshot.
- baf.db.monitoring_db.TABLE_CHAT = 'chat'#
The name of the database table that contains the chat records
- baf.db.monitoring_db.TABLE_EVENT = 'event'#
The name of the database table that contains the event records
- baf.db.monitoring_db.TABLE_INTENT_PREDICTION = 'intent_prediction'#
The name of the database table that contains the intent prediction records
- baf.db.monitoring_db.TABLE_PARAMETER = 'parameter'#
The name of the database table that contains the parameter records
- baf.db.monitoring_db.TABLE_REASONING_STEP = 'reasoning_step'#
The name of the database table that contains reasoning-state step events and task-list snapshots (a single per-session timeline of everything that happens inside the reasoning loop).
- baf.db.monitoring_db.TABLE_SESSION = 'session'#
The name of the database table that contains the session records
- baf.db.monitoring_db.TABLE_TRANSITION = 'transition'#
The name of the database table that contains the transition records