state
code_context_agent.consumer.state ¶
Mutable display state for agent event rendering.
This module provides Pydantic models for tracking the current state of agent execution, used by consumers to render live updates.
ToolCallState ¶
Bases: StrictModel
State for a single tool call.
Attributes:
| Name | Type | Description |
|---|---|---|
tool_call_id | str | Unique identifier for the tool call. |
tool_name | str | Name of the tool being executed. |
args_buffer | str | Accumulated tool arguments (streaming). |
result | Any | Tool execution result (when complete). |
status | str | Current status ("running", "completed", "error"). |
AgentDisplayState ¶
Bases: StrictModel
Mutable state for agent display rendering.
This class tracks all state needed to render the agent's progress in a terminal or UI. It accumulates streaming text, tracks active and completed tool calls, and maintains error state.
Attributes:
| Name | Type | Description |
|---|---|---|
current_phase | str | Description of current analysis phase. |
text_buffer | str | Accumulated streaming text from agent. |
active_message_id | str | None | ID of currently streaming message. |
active_tool | ToolCallState | None | Currently executing tool state (if any). |
completed_tools | list[ToolCallState] | List of completed tool executions. |
state_snapshot | dict[str, Any] | Latest state snapshot from agent. |
error | str | None | Error message if run failed. |
completed | bool | Whether the run has finished. |
thread_id | str | None | Current thread identifier. |
run_id | str | None | Current run identifier. |
Example
state = AgentDisplayState() state.text_buffer += "Analyzing repository..." state.active_tool = ToolCallState(tool_call_id="t1", tool_name="rg_search")
clear_text_buffer ¶
Clear and return the text buffer.
Returns:
| Type | Description |
|---|---|
str | The text buffer contents before clearing. |
complete_active_tool ¶
Mark the active tool as completed and move to history.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result | Any | Optional result to store with the tool call. | None |
Source code in src/code_context_agent/consumer/state.py
get_recent_tools ¶
Get the most recent completed tools.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
count | int | Maximum number of tools to return. | 5 |
Returns:
| Type | Description |
|---|---|
list[ToolCallState] | List of recently completed tool states. |
Source code in src/code_context_agent/consumer/state.py
get_tool_stats ¶
Get tool call counts grouped by prefix.
Groups tools by their name prefix (e.g., lsp_, code_graph_) for display in the TUI dashboard.
Returns:
| Type | Description |
|---|---|
dict[str, int] | Dictionary mapping tool prefixes to call counts. |
Source code in src/code_context_agent/consumer/state.py
get_elapsed_seconds ¶
Get elapsed time since run started.
Returns:
| Type | Description |
|---|---|
float | Elapsed seconds, or 0.0 if not started. |
Source code in src/code_context_agent/consumer/state.py
get_tool_elapsed_seconds ¶
Get elapsed time for current tool.
Returns:
| Type | Description |
|---|---|
float | Elapsed seconds for active tool, or 0.0 if no active tool. |
Source code in src/code_context_agent/consumer/state.py
get_success_count ¶
Get count of successful tool calls.
Returns:
| Type | Description |
|---|---|
int | Number of completed tools minus errors. |
advance_phase ¶
Advance to a new phase if it's higher than the current one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
phase | Any | AnalysisPhase enum value to advance to. | required |
Source code in src/code_context_agent/consumer/state.py
add_discovery ¶
Add a discovery event, evicting oldest if at capacity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event | Any | DiscoveryEvent to add. | required |
Source code in src/code_context_agent/consumer/state.py
reset ¶
Reset state for a new run.