feat(gui): Add auto-scroll, blinking history, and reactive API events
This commit is contained in:
281
gui_2.py
281
gui_2.py
@@ -109,7 +109,7 @@ class App:
|
||||
|
||||
ai_cfg = self.config.get("ai", {})
|
||||
self.current_provider: str = ai_cfg.get("provider", "gemini")
|
||||
self.current_model: str = ai_cfg.get("model", "gemini-2.5-flash")
|
||||
self.current_model: str = ai_cfg.get("model", "gemini-2.5-flash-lite")
|
||||
self.available_models: list[str] = []
|
||||
self.temperature: float = ai_cfg.get("temperature", 0.0)
|
||||
self.max_tokens: int = ai_cfg.get("max_tokens", 8192)
|
||||
@@ -192,6 +192,9 @@ class App:
|
||||
self._pending_comms: list[dict] = []
|
||||
self._pending_comms_lock = threading.Lock()
|
||||
|
||||
self._pending_tool_calls: list[tuple[str, str]] = []
|
||||
self._pending_tool_calls_lock = threading.Lock()
|
||||
|
||||
self._pending_history_adds: list[dict] = []
|
||||
self._pending_history_adds_lock = threading.Lock()
|
||||
|
||||
@@ -205,6 +208,8 @@ class App:
|
||||
self._script_blink_start_time = 0.0
|
||||
|
||||
self._scroll_disc_to_bottom = False
|
||||
self._scroll_comms_to_bottom = False
|
||||
self._scroll_tool_calls_to_bottom = False
|
||||
|
||||
# GUI Task Queue (thread-safe, for event handlers and hook server)
|
||||
self._pending_gui_tasks: list[dict] = []
|
||||
@@ -222,6 +227,9 @@ class App:
|
||||
# Discussion truncation
|
||||
self.ui_disc_truncate_pairs: int = 2
|
||||
|
||||
self.ui_auto_scroll_comms = True
|
||||
self.ui_auto_scroll_tool_calls = True
|
||||
|
||||
# Agent tools config
|
||||
agent_tools_cfg = self.project.get("agent", {}).get("tools", {})
|
||||
self.ui_agent_tools: dict[str, bool] = {t: agent_tools_cfg.get(t, True) for t in AGENT_TOOL_NAMES}
|
||||
@@ -270,6 +278,7 @@ class App:
|
||||
'current_provider': 'current_provider',
|
||||
'current_model': 'current_model',
|
||||
'token_budget_pct': '_token_budget_pct',
|
||||
'token_budget_current': '_token_budget_current',
|
||||
'token_budget_label': '_token_budget_label',
|
||||
'show_confirm_modal': 'show_confirm_modal'
|
||||
}
|
||||
@@ -379,6 +388,8 @@ class App:
|
||||
self.ui_project_system_prompt = proj.get("project", {}).get("system_prompt", "")
|
||||
self.ui_project_main_context = proj.get("project", {}).get("main_context", "")
|
||||
self.ui_auto_add_history = proj.get("discussion", {}).get("auto_add", False)
|
||||
self.ui_auto_scroll_comms = proj.get("project", {}).get("auto_scroll_comms", True)
|
||||
self.ui_auto_scroll_tool_calls = proj.get("project", {}).get("auto_scroll_tool_calls", True)
|
||||
self.ui_word_wrap = proj.get("project", {}).get("word_wrap", True)
|
||||
self.ui_summary_only = proj.get("project", {}).get("summary_only", False)
|
||||
|
||||
@@ -469,11 +480,14 @@ class App:
|
||||
|
||||
def _on_comms_entry(self, entry: dict):
|
||||
session_logger.log_comms(entry)
|
||||
entry["local_ts"] = time.time()
|
||||
with self._pending_comms_lock:
|
||||
self._pending_comms.append(entry)
|
||||
|
||||
def _on_tool_log(self, script: str, result: str):
|
||||
session_logger.log_tool_call(script, result, None)
|
||||
with self._pending_tool_calls_lock:
|
||||
self._pending_tool_calls.append((script, result, time.time()))
|
||||
|
||||
def _on_api_event(self, *args, **kwargs):
|
||||
payload = kwargs.get("payload", {})
|
||||
@@ -541,18 +555,20 @@ class App:
|
||||
print(f"Error executing GUI task: {e}")
|
||||
|
||||
def _handle_approve_script(self):
|
||||
"""Logic for approving a pending script."""
|
||||
if self.show_confirm_modal:
|
||||
self.show_confirm_modal = False
|
||||
if self.pending_script_callback:
|
||||
self.pending_script_callback(True)
|
||||
"""Logic for approving a pending script via API hooks."""
|
||||
with self._pending_dialog_lock:
|
||||
if self._pending_dialog:
|
||||
self._pending_dialog._approved = True
|
||||
self._pending_dialog._event.set()
|
||||
self._pending_dialog = None
|
||||
|
||||
def _handle_reject_script(self):
|
||||
"""Logic for rejecting a pending script."""
|
||||
if self.show_confirm_modal:
|
||||
self.show_confirm_modal = False
|
||||
if self.pending_script_callback:
|
||||
self.pending_script_callback(False)
|
||||
"""Logic for rejecting a pending script via API hooks."""
|
||||
with self._pending_dialog_lock:
|
||||
if self._pending_dialog:
|
||||
self._pending_dialog._approved = False
|
||||
self._pending_dialog._event.set()
|
||||
self._pending_dialog = None
|
||||
|
||||
def _handle_reset_session(self):
|
||||
"""Logic for resetting the AI session."""
|
||||
@@ -578,6 +594,8 @@ class App:
|
||||
self.last_md = md
|
||||
self.last_md_path = path
|
||||
self.ai_status = f"md written: {path.name}"
|
||||
# Refresh token budget metrics
|
||||
self._refresh_api_metrics({})
|
||||
except Exception as e:
|
||||
self.ai_status = f"error: {e}"
|
||||
|
||||
@@ -660,7 +678,7 @@ class App:
|
||||
|
||||
def fetch_stats():
|
||||
try:
|
||||
stats = ai_client.get_history_bleed_stats()
|
||||
stats = ai_client.get_history_bleed_stats(md_content=self.last_md)
|
||||
self._token_budget_pct = stats.get("percentage", 0.0) / 100.0
|
||||
self._token_budget_current = stats.get("current", 0)
|
||||
self._token_budget_limit = stats.get("limit", 0)
|
||||
@@ -706,6 +724,16 @@ class App:
|
||||
dialog = ConfirmDialog(script, base_dir)
|
||||
with self._pending_dialog_lock:
|
||||
self._pending_dialog = dialog
|
||||
|
||||
# Notify API hook subscribers
|
||||
if self.test_hooks_enabled and hasattr(self, '_api_event_queue'):
|
||||
with self._api_event_queue_lock:
|
||||
self._api_event_queue.append({
|
||||
"type": "script_confirmation_required",
|
||||
"script": str(script),
|
||||
"base_dir": str(base_dir),
|
||||
"ts": time.time()
|
||||
})
|
||||
|
||||
approved, final_script = dialog.wait()
|
||||
if not approved:
|
||||
@@ -739,6 +767,8 @@ class App:
|
||||
proj["project"]["main_context"] = self.ui_project_main_context
|
||||
proj["project"]["word_wrap"] = self.ui_word_wrap
|
||||
proj["project"]["summary_only"] = self.ui_summary_only
|
||||
proj["project"]["auto_scroll_comms"] = self.ui_auto_scroll_comms
|
||||
proj["project"]["auto_scroll_tool_calls"] = self.ui_auto_scroll_tool_calls
|
||||
|
||||
proj.setdefault("agent", {}).setdefault("tools", {})
|
||||
for t_name in AGENT_TOOL_NAMES:
|
||||
@@ -880,10 +910,19 @@ class App:
|
||||
|
||||
# Sync pending comms
|
||||
with self._pending_comms_lock:
|
||||
if self._pending_comms and self.ui_auto_scroll_comms:
|
||||
self._scroll_comms_to_bottom = True
|
||||
for c in self._pending_comms:
|
||||
self._comms_log.append(c)
|
||||
self._pending_comms.clear()
|
||||
|
||||
with self._pending_tool_calls_lock:
|
||||
if self._pending_tool_calls and self.ui_auto_scroll_tool_calls:
|
||||
self._scroll_tool_calls_to_bottom = True
|
||||
for tc in self._pending_tool_calls:
|
||||
self._tool_log.append(tc)
|
||||
self._pending_tool_calls.clear()
|
||||
|
||||
with self._pending_history_adds_lock:
|
||||
if self._pending_history_adds:
|
||||
self._scroll_disc_to_bottom = True
|
||||
@@ -1053,7 +1092,9 @@ class App:
|
||||
imgui.open_popup("Approve PowerShell Command")
|
||||
self._pending_dialog_open = True
|
||||
else:
|
||||
self._pending_dialog_open = False
|
||||
if self._pending_dialog_open:
|
||||
imgui.close_current_popup()
|
||||
self._pending_dialog_open = False
|
||||
|
||||
if imgui.begin_popup_modal("Approve PowerShell Command", None, imgui.WindowFlags_.always_auto_resize)[0]:
|
||||
if dlg:
|
||||
@@ -1233,6 +1274,8 @@ class App:
|
||||
|
||||
ch, self.ui_word_wrap = imgui.checkbox("Word-Wrap (Read-only panels)", self.ui_word_wrap)
|
||||
ch, self.ui_summary_only = imgui.checkbox("Summary Only (send file structure, not full content)", self.ui_summary_only)
|
||||
ch, self.ui_auto_scroll_comms = imgui.checkbox("Auto-scroll Comms History", self.ui_auto_scroll_comms)
|
||||
ch, self.ui_auto_scroll_tool_calls = imgui.checkbox("Auto-scroll Tool History", self.ui_auto_scroll_tool_calls)
|
||||
|
||||
if imgui.collapsing_header("Agent Tools"):
|
||||
for t_name in AGENT_TOOL_NAMES:
|
||||
@@ -1648,7 +1691,26 @@ class App:
|
||||
while clipper.step():
|
||||
for i_minus_one in range(clipper.display_start, clipper.display_end):
|
||||
i = i_minus_one + 1
|
||||
script, result = self._tool_log[i_minus_one]
|
||||
entry = self._tool_log[i_minus_one]
|
||||
# Handle both old (tuple) and new (tuple with ts) entries
|
||||
if len(entry) == 3:
|
||||
script, result, local_ts = entry
|
||||
else:
|
||||
script, result = entry
|
||||
local_ts = 0
|
||||
|
||||
# Blink effect
|
||||
blink_alpha = 0.0
|
||||
if local_ts > 0:
|
||||
elapsed = time.time() - local_ts
|
||||
if elapsed < 3.0:
|
||||
# Blink + fade
|
||||
blink_alpha = (1.0 - (elapsed / 3.0)) * 0.3 * (math.sin(elapsed * 10) * 0.5 + 0.5)
|
||||
|
||||
if blink_alpha > 0:
|
||||
imgui.push_style_color(imgui.Col_.child_bg, vec4(0, 255, 0, blink_alpha))
|
||||
imgui.begin_child(f"tc_entry_{i}", imgui.ImVec2(0, 0), True)
|
||||
|
||||
first_line = script.strip().splitlines()[0][:80] if script.strip() else "(empty)"
|
||||
imgui.text_colored(C_KEY, f"Call #{i}: {first_line}")
|
||||
|
||||
@@ -1688,7 +1750,16 @@ class App:
|
||||
imgui.input_text_multiline(f"##tc_res_val_{i}", result, imgui.ImVec2(-1, -1), imgui.InputTextFlags_.read_only)
|
||||
imgui.end_child()
|
||||
|
||||
if blink_alpha > 0:
|
||||
imgui.end_child()
|
||||
imgui.pop_style_color()
|
||||
|
||||
imgui.separator()
|
||||
|
||||
if self._scroll_tool_calls_to_bottom:
|
||||
imgui.set_scroll_here_y(1.0)
|
||||
self._scroll_tool_calls_to_bottom = False
|
||||
|
||||
imgui.end_child()
|
||||
|
||||
def _render_comms_history_panel(self):
|
||||
@@ -1741,113 +1812,105 @@ class App:
|
||||
for idx_minus_one in range(clipper.display_start, clipper.display_end):
|
||||
idx = idx_minus_one + 1
|
||||
entry = log_to_render[idx_minus_one]
|
||||
imgui.push_id(f"comms_{idx}")
|
||||
d = entry.get("direction", "IN")
|
||||
k = entry.get("kind", "response")
|
||||
local_ts = entry.get("local_ts", 0)
|
||||
|
||||
imgui.text_colored(vec4(160, 160, 160), f"#{idx}")
|
||||
imgui.same_line()
|
||||
imgui.text_colored(vec4(160, 160, 160), entry.get("ts", "00:00:00"))
|
||||
imgui.same_line()
|
||||
imgui.text_colored(DIR_COLORS.get(d, C_VAL), d)
|
||||
imgui.same_line()
|
||||
imgui.text_colored(KIND_COLORS.get(k, C_VAL), k)
|
||||
imgui.same_line()
|
||||
imgui.text_colored(C_LBL, f"{entry.get('provider', '?')}/{entry.get('model', '?')}")
|
||||
# Blink effect
|
||||
blink_alpha = 0.0
|
||||
if local_ts > 0 and not self.is_viewing_prior_session:
|
||||
elapsed = time.time() - local_ts
|
||||
if elapsed < 3.0:
|
||||
# Blink + fade
|
||||
blink_alpha = (1.0 - (elapsed / 3.0)) * 0.3 * (math.sin(elapsed * 10) * 0.5 + 0.5)
|
||||
|
||||
payload = entry.get("payload", {})
|
||||
|
||||
if k == "request":
|
||||
self._render_heavy_text("message", payload.get("message", ""))
|
||||
elif k == "response":
|
||||
imgui.text_colored(C_LBL, "round:")
|
||||
imgui.same_line()
|
||||
imgui.text_colored(C_VAL, str(payload.get("round", "")))
|
||||
if blink_alpha > 0:
|
||||
imgui.push_style_color(imgui.Col_.child_bg, vec4(0, 255, 0, blink_alpha))
|
||||
|
||||
if imgui.begin_child(f"comms_entry_{idx}", imgui.ImVec2(0, 0), True):
|
||||
d = entry.get("direction", "IN")
|
||||
k = entry.get("kind", "response")
|
||||
|
||||
imgui.text_colored(C_LBL, "stop_reason:")
|
||||
imgui.text_colored(vec4(160, 160, 160), f"#{idx}")
|
||||
imgui.same_line()
|
||||
imgui.text_colored(vec4(255, 200, 120), str(payload.get("stop_reason", "")))
|
||||
imgui.text_colored(vec4(160, 160, 160), entry.get("ts", "00:00:00"))
|
||||
imgui.same_line()
|
||||
imgui.text_colored(DIR_COLORS.get(d, C_VAL), d)
|
||||
imgui.same_line()
|
||||
imgui.text_colored(KIND_COLORS.get(k, C_VAL), k)
|
||||
imgui.same_line()
|
||||
imgui.text_colored(C_LBL, f"{entry.get('provider', '?')}/{entry.get('model', '?')}")
|
||||
|
||||
text = payload.get("text", "")
|
||||
if text:
|
||||
self._render_heavy_text("text", text)
|
||||
payload = entry.get("payload", {})
|
||||
|
||||
if k == "request":
|
||||
self._render_heavy_text("message", payload.get("message", ""))
|
||||
elif k == "response":
|
||||
imgui.text_colored(C_LBL, "round:")
|
||||
imgui.same_line()
|
||||
imgui.text_colored(C_VAL, str(payload.get("round", "")))
|
||||
|
||||
imgui.text_colored(C_LBL, "tool_calls:")
|
||||
tcs = payload.get("tool_calls", [])
|
||||
if not tcs:
|
||||
imgui.text_colored(C_VAL, " (none)")
|
||||
for i, tc in enumerate(tcs):
|
||||
imgui.text_colored(C_KEY, f" call[{i}] {tc.get('name', '?')}")
|
||||
if "id" in tc:
|
||||
imgui.text_colored(C_LBL, " id:")
|
||||
imgui.same_line()
|
||||
imgui.text_colored(C_VAL, str(tc["id"]))
|
||||
args = tc.get("args") or tc.get("input") or {}
|
||||
if isinstance(args, dict):
|
||||
for ak, av in args.items():
|
||||
self._render_heavy_text(f" {ak}", str(av))
|
||||
elif args:
|
||||
self._render_heavy_text(" args", str(args))
|
||||
|
||||
usage = payload.get("usage")
|
||||
if usage:
|
||||
imgui.text_colored(C_SUB, "usage:")
|
||||
for uk, uv in usage.items():
|
||||
imgui.text_colored(C_LBL, f" {uk.replace('_', ' ')}:")
|
||||
imgui.same_line()
|
||||
imgui.text_colored(C_NUM, str(uv))
|
||||
|
||||
elif k == "tool_call":
|
||||
imgui.text_colored(C_LBL, "name:")
|
||||
imgui.same_line()
|
||||
imgui.text_colored(C_VAL, str(payload.get("name", "")))
|
||||
if "id" in payload:
|
||||
imgui.text_colored(C_LBL, "id:")
|
||||
imgui.text_colored(C_LBL, "stop_reason:")
|
||||
imgui.same_line()
|
||||
imgui.text_colored(C_VAL, str(payload["id"]))
|
||||
if "script" in payload:
|
||||
self._render_heavy_text("script", payload.get("script", ""))
|
||||
elif "args" in payload:
|
||||
args = payload["args"]
|
||||
if isinstance(args, dict):
|
||||
for ak, av in args.items():
|
||||
self._render_heavy_text(ak, str(av))
|
||||
else:
|
||||
self._render_heavy_text("args", str(args))
|
||||
imgui.text_colored(vec4(255, 200, 120), str(payload.get("stop_reason", "")))
|
||||
|
||||
text = payload.get("text", "")
|
||||
if text:
|
||||
self._render_heavy_text("text", text)
|
||||
|
||||
elif k == "tool_result":
|
||||
imgui.text_colored(C_LBL, "name:")
|
||||
imgui.same_line()
|
||||
imgui.text_colored(C_VAL, str(payload.get("name", "")))
|
||||
if "id" in payload:
|
||||
imgui.text_colored(C_LBL, "id:")
|
||||
imgui.same_line()
|
||||
imgui.text_colored(C_VAL, str(payload["id"]))
|
||||
self._render_heavy_text("output", payload.get("output", ""))
|
||||
imgui.text_colored(C_LBL, "tool_calls:")
|
||||
tcs = payload.get("tool_calls", [])
|
||||
if not tcs:
|
||||
imgui.text_colored(C_VAL, " (none)")
|
||||
for tc_i, tc in enumerate(tcs):
|
||||
imgui.text_colored(C_KEY, f" call[{tc_i}] {tc.get('name', '?')}")
|
||||
if "id" in tc:
|
||||
imgui.text_colored(C_LBL, " id:")
|
||||
imgui.same_line()
|
||||
imgui.text_colored(C_VAL, tc["id"])
|
||||
if "args" in tc or "input" in tc:
|
||||
self._render_heavy_text(f"call_{tc_i}_args", str(tc.get("args") or tc.get("input")))
|
||||
|
||||
elif k == "tool_result_send":
|
||||
for i, r in enumerate(payload.get("results", [])):
|
||||
imgui.text_colored(C_KEY, f"result[{i}]")
|
||||
imgui.text_colored(C_LBL, " tool_use_id:")
|
||||
imgui.same_line()
|
||||
imgui.text_colored(C_VAL, str(r.get("tool_use_id", "")))
|
||||
self._render_heavy_text(" content", str(r.get("content", "")))
|
||||
else:
|
||||
for key, val in payload.items():
|
||||
vstr = json.dumps(val, ensure_ascii=False, indent=2) if isinstance(val, (dict, list)) else str(val)
|
||||
if key in HEAVY_KEYS:
|
||||
self._render_heavy_text(key, vstr)
|
||||
else:
|
||||
imgui.text_colored(C_LBL, f"{key}:")
|
||||
elif k == "tool_call":
|
||||
imgui.text_colored(C_KEY, payload.get("name", "?"))
|
||||
if "id" in payload:
|
||||
imgui.text_colored(C_LBL, " id:")
|
||||
imgui.same_line()
|
||||
imgui.text_colored(C_VAL, vstr)
|
||||
imgui.text_colored(C_VAL, payload["id"])
|
||||
if "script" in payload:
|
||||
self._render_heavy_text("script", payload["script"])
|
||||
if "args" in payload:
|
||||
self._render_heavy_text("args", str(payload["args"]))
|
||||
|
||||
elif k == "tool_result":
|
||||
imgui.text_colored(C_KEY, payload.get("name", "?"))
|
||||
if "id" in payload:
|
||||
imgui.text_colored(C_LBL, " id:")
|
||||
imgui.same_line()
|
||||
imgui.text_colored(C_VAL, payload["id"])
|
||||
if "output" in payload:
|
||||
self._render_heavy_text("output", payload["output"])
|
||||
if "results" in payload:
|
||||
# Multiple results from parallel tool calls
|
||||
for r_i, r in enumerate(payload["results"]):
|
||||
imgui.text_colored(C_LBL, f" Result[{r_i}]:")
|
||||
self._render_heavy_text(f"res_{r_i}", str(r))
|
||||
|
||||
if "usage" in payload:
|
||||
u = payload["usage"]
|
||||
u_str = f"In: {u.get('input_tokens', 0)} Out: {u.get('output_tokens', 0)}"
|
||||
if u.get("cache_read_input_tokens"):
|
||||
u_str += f" (Cache: {u['cache_read_input_tokens']})"
|
||||
imgui.text_colored(C_SUB, f" Usage: {u_str}")
|
||||
|
||||
imgui.end_child()
|
||||
|
||||
imgui.separator()
|
||||
imgui.pop_id()
|
||||
if blink_alpha > 0:
|
||||
imgui.pop_style_color()
|
||||
|
||||
if self._scroll_comms_to_bottom:
|
||||
imgui.set_scroll_here_y(1.0)
|
||||
self._scroll_comms_to_bottom = False
|
||||
|
||||
imgui.end_child()
|
||||
|
||||
if self.is_viewing_prior_session:
|
||||
imgui.pop_style_color()
|
||||
|
||||
def _render_system_prompts_panel(self):
|
||||
imgui.text("Global System Prompt (all projects)")
|
||||
|
||||
Reference in New Issue
Block a user