refactor: simplify exec render state
This commit is contained in:
@@ -13,7 +13,6 @@ HEADER_SEP = " · "
|
|||||||
HARD_BREAK = " \n"
|
HARD_BREAK = " \n"
|
||||||
|
|
||||||
MAX_CMD_LEN = 40
|
MAX_CMD_LEN = 40
|
||||||
MAX_REASON_LEN = 80
|
|
||||||
MAX_QUERY_LEN = 60
|
MAX_QUERY_LEN = 60
|
||||||
MAX_PATH_LEN = 40
|
MAX_PATH_LEN = 40
|
||||||
MAX_PROGRESS_CHARS = 300
|
MAX_PROGRESS_CHARS = 300
|
||||||
@@ -54,12 +53,6 @@ def format_header(elapsed_s: float, turn: Optional[int], label: str) -> str:
|
|||||||
return f"{label}{HEADER_SEP}{elapsed}"
|
return f"{label}{HEADER_SEP}{elapsed}"
|
||||||
|
|
||||||
|
|
||||||
def format_reasoning(text: str) -> str:
|
|
||||||
if not text:
|
|
||||||
return ""
|
|
||||||
return f"_{truncate(text, MAX_REASON_LEN)}_"
|
|
||||||
|
|
||||||
|
|
||||||
def format_command(command: str) -> str:
|
def format_command(command: str) -> str:
|
||||||
command = truncate(command, MAX_CMD_LEN)
|
command = truncate(command, MAX_CMD_LEN)
|
||||||
if not command:
|
if not command:
|
||||||
@@ -151,10 +144,6 @@ def format_item_completed_line(item_id: Optional[int], item: dict[str, Any]) ->
|
|||||||
@dataclass
|
@dataclass
|
||||||
class ExecRenderState:
|
class ExecRenderState:
|
||||||
recent_actions: deque[str] = field(default_factory=lambda: deque(maxlen=5))
|
recent_actions: deque[str] = field(default_factory=lambda: deque(maxlen=5))
|
||||||
current_action: Optional[str] = None
|
|
||||||
current_action_id: Optional[int] = None
|
|
||||||
pending_reasoning: Optional[str] = None
|
|
||||||
current_reasoning: Optional[str] = None
|
|
||||||
last_turn: Optional[int] = None
|
last_turn: Optional[int] = None
|
||||||
|
|
||||||
|
|
||||||
@@ -164,37 +153,6 @@ def record_item(state: ExecRenderState, item: dict[str, Any]) -> None:
|
|||||||
state.last_turn = numeric_id
|
state.last_turn = numeric_id
|
||||||
|
|
||||||
|
|
||||||
def set_current_action(state: ExecRenderState, item_id: Optional[int], line: str) -> bool:
|
|
||||||
changed = False
|
|
||||||
if state.current_action != line or state.current_action_id != item_id:
|
|
||||||
state.current_action = line
|
|
||||||
state.current_action_id = item_id
|
|
||||||
if state.pending_reasoning:
|
|
||||||
state.current_reasoning = state.pending_reasoning
|
|
||||||
state.pending_reasoning = None
|
|
||||||
changed = True
|
|
||||||
return changed
|
|
||||||
|
|
||||||
|
|
||||||
def complete_action(state: ExecRenderState, item_id: Optional[int], line: str) -> bool:
|
|
||||||
changed = False
|
|
||||||
if state.current_reasoning:
|
|
||||||
if not state.recent_actions or state.recent_actions[-1] != state.current_reasoning:
|
|
||||||
state.recent_actions.append(state.current_reasoning)
|
|
||||||
changed = True
|
|
||||||
if line:
|
|
||||||
state.recent_actions.append(line)
|
|
||||||
changed = True
|
|
||||||
if item_id and state.current_action_id == item_id:
|
|
||||||
state.current_action = None
|
|
||||||
state.current_action_id = None
|
|
||||||
state.current_reasoning = None
|
|
||||||
changed = True
|
|
||||||
if not item_id and state.current_action_id is None:
|
|
||||||
state.current_reasoning = None
|
|
||||||
return changed
|
|
||||||
|
|
||||||
|
|
||||||
def render_event_cli(
|
def render_event_cli(
|
||||||
event: dict[str, Any],
|
event: dict[str, Any],
|
||||||
state: ExecRenderState,
|
state: ExecRenderState,
|
||||||
@@ -249,7 +207,6 @@ class ExecProgressRenderer:
|
|||||||
|
|
||||||
def note_event(self, event: dict[str, Any]) -> bool:
|
def note_event(self, event: dict[str, Any]) -> bool:
|
||||||
etype = event["type"]
|
etype = event["type"]
|
||||||
changed = False
|
|
||||||
|
|
||||||
if etype in {"thread.started", "turn.started"}:
|
if etype in {"thread.started", "turn.started"}:
|
||||||
return True
|
return True
|
||||||
@@ -260,43 +217,25 @@ class ExecProgressRenderer:
|
|||||||
itype = item["type"]
|
itype = item["type"]
|
||||||
item_id = extract_numeric_id(item["id"], self.state.last_turn)
|
item_id = extract_numeric_id(item["id"], self.state.last_turn)
|
||||||
|
|
||||||
if itype == "reasoning":
|
if itype == "agent_message":
|
||||||
reasoning = format_reasoning(item["text"])
|
return False
|
||||||
if reasoning:
|
|
||||||
reasoning_line = with_id(item_id, reasoning)
|
|
||||||
if self.state.current_action and not self.state.current_reasoning:
|
|
||||||
self.state.current_reasoning = reasoning_line
|
|
||||||
changed = True
|
|
||||||
else:
|
|
||||||
self.state.pending_reasoning = reasoning_line
|
|
||||||
return changed
|
|
||||||
|
|
||||||
action_line = format_item_action_line(etype, item_id, item)
|
action_line = format_item_action_line(etype, item_id, item)
|
||||||
if action_line is not None:
|
if action_line is not None:
|
||||||
if etype == "item.started":
|
self.state.recent_actions.append(action_line)
|
||||||
return set_current_action(self.state, item_id, action_line) or changed
|
return True
|
||||||
if etype == "item.completed":
|
|
||||||
return complete_action(self.state, item_id, action_line) or changed
|
|
||||||
return changed
|
|
||||||
|
|
||||||
if etype == "item.completed":
|
if etype == "item.completed":
|
||||||
completed_line = format_item_completed_line(item_id, item)
|
completed_line = format_item_completed_line(item_id, item)
|
||||||
if completed_line is not None:
|
if completed_line is not None:
|
||||||
return complete_action(self.state, item_id, completed_line) or changed
|
self.state.recent_actions.append(completed_line)
|
||||||
|
return True
|
||||||
|
|
||||||
return changed
|
return False
|
||||||
|
|
||||||
def render_progress(self, elapsed_s: float) -> str:
|
def render_progress(self, elapsed_s: float) -> str:
|
||||||
header = format_header(elapsed_s, self.state.last_turn, label="working")
|
header = format_header(elapsed_s, self.state.last_turn, label="working")
|
||||||
current_reasoning = self.state.current_reasoning
|
message = self._assemble(header, list(self.state.recent_actions))
|
||||||
current_action = self.state.current_action
|
|
||||||
lines = list(self.state.recent_actions)
|
|
||||||
if current_reasoning and current_action:
|
|
||||||
lines.append(current_reasoning)
|
|
||||||
if current_action:
|
|
||||||
lines.append(current_action)
|
|
||||||
|
|
||||||
message = self._assemble(header, lines)
|
|
||||||
if len(message) <= self.max_chars:
|
if len(message) <= self.max_chars:
|
||||||
return message
|
return message
|
||||||
return header
|
return header
|
||||||
|
|||||||
Reference in New Issue
Block a user