| """Parse Hermes messages into a human-readable activity feed.""" |
| import json |
| import re |
| from dataclasses import dataclass, field |
|
|
| from agent_gui.message_sanitize import strip_injected_prefix |
|
|
| from agent_gui.db import Message |
|
|
| TOOL_ICONS = { |
| "bash": "β‘", "execute_command": "β‘", "terminal": "β‘", "run_command": "β‘", |
| "write_file": "π", "create_file": "π", "file_write": "π", |
| "str_replace_editor": "βοΈ", "edit_file": "βοΈ", |
| "read_file": "π", "file_read": "π", |
| "web_search": "π", "search": "π", |
| "browser": "π", "web_fetch": "π", |
| "memory": "π§ ", "remember": "π§ ", |
| "compress": "ποΈ", "summarize": "ποΈ", |
| "delegate": "π₯", "spawn_agent": "π₯", "subagent": "π₯", |
| "skill_view": "π", "skill_run": "π", |
| "python": "π", |
| |
| |
| "Read": "π", "Write": "π", "Edit": "βοΈ", "MultiEdit": "βοΈ", "NotebookEdit": "βοΈ", |
| "Bash": "β‘", "BashOutput": "β‘", "KillShell": "β‘", |
| "Glob": "π", "Grep": "π", "WebSearch": "π", "WebFetch": "π", |
| "Task": "π₯", "TodoWrite": "π", "ExitPlanMode": "π", |
| "default": "π§", |
| } |
|
|
| |
| |
| |
| |
| |
| MIN_MESSAGE_LEN = 5 |
|
|
|
|
| @dataclass |
| class ActivityEvent: |
| timestamp: str |
| event_type: str |
| icon: str |
| title: str |
| detail: str |
| tool_name: str = "" |
| is_error: bool = False |
| files_touched: list[str] = field(default_factory=list) |
| |
| |
| |
| time_exact: bool = False |
|
|
|
|
| def _truncate(text: str, n: int = 160) -> str: |
| text = str(text).strip().replace("\n", " ") |
| return text[:n] + "β¦" if len(text) > n else text |
|
|
| def _clean(text: str) -> str: |
| """Return text stripped but with newlines preserved (for message display).""" |
| return str(text).strip() |
|
|
|
|
| def _tool_detail(tool_name: str, tool_input: dict) -> str: |
| if tool_name in ("bash", "execute_command", "run_command", "terminal", "Bash"): |
| return _truncate(tool_input.get("command", tool_input.get("cmd", str(tool_input)))) |
| if tool_name in ("write_file", "create_file", "file_write", "str_replace_editor", |
| "edit_file", "read_file", "file_read", |
| "Read", "Write", "Edit", "MultiEdit", "NotebookEdit"): |
| path = (tool_input.get("path") or tool_input.get("file_path") |
| or tool_input.get("filename") or tool_input.get("notebook_path") or "") |
| return path or _truncate(str(tool_input)) |
| if tool_name in ("web_search", "search", "WebSearch"): |
| return _truncate(tool_input.get("query", str(tool_input))) |
| if tool_name in ("Grep", "Glob"): |
| return _truncate(tool_input.get("pattern", str(tool_input))) |
| if tool_name in ("web_fetch", "WebFetch"): |
| return _truncate(tool_input.get("url", str(tool_input))) |
| return _truncate(str(tool_input)) |
|
|
|
|
| def _files_from_tool(tool_name: str, tool_input: dict) -> list[str]: |
| for key in ("path", "file_path", "filename", "filepath", "notebook_path"): |
| val = tool_input.get(key) |
| if val and isinstance(val, str): |
| return [val] |
| return [] |
|
|
|
|
| def parse_activity(messages: list[Message]) -> list[ActivityEvent]: |
| events: list[ActivityEvent] = [] |
| tool_call_map: dict[str, dict] = {} |
|
|
| for msg in messages: |
| ts = msg.timestamp |
|
|
| |
| if msg.role == "user": |
| if msg.content and msg.content.strip(): |
| clean = strip_injected_prefix(msg.content) |
| if clean: |
| events.append(ActivityEvent( |
| timestamp=ts, |
| event_type="user_message", |
| icon="π€", |
| title="User", |
| detail=_clean(clean), |
| )) |
|
|
| |
| elif msg.role == "assistant": |
| |
| |
| |
| reasoning = (msg.reasoning_content or "").strip() |
| if reasoning: |
| events.append(ActivityEvent( |
| timestamp=ts, |
| event_type="thinking_start", |
| icon="π", |
| title="Reasoning", |
| detail=reasoning, |
| )) |
|
|
| |
| |
| |
| |
| |
| |
| if msg.content and msg.content.strip() and len(msg.content.strip()) > MIN_MESSAGE_LEN: |
| text = msg.content.strip() |
| is_compression = any(kw in text.lower() for kw in ("compressing", "context compressed", "summarizing context")) |
| events.append(ActivityEvent( |
| timestamp=ts, |
| event_type="compression" if is_compression else "message", |
| icon="ποΈ" if is_compression else "π€", |
| title="Context compressed" if is_compression else "Agent", |
| detail=_truncate(text, 300) if is_compression else _clean(text), |
| )) |
|
|
| for tc in msg.tool_calls: |
| if tc.get("type") != "function": |
| continue |
| fn = tc.get("function", {}) |
| tool_name = fn.get("name", "unknown") |
| args_raw = fn.get("arguments", "{}") |
| try: |
| tool_input = json.loads(args_raw) if isinstance(args_raw, str) else args_raw |
| except Exception: |
| tool_input = {} |
|
|
| tool_call_map[tc.get("id", "")] = {"name": tool_name, "input": tool_input} |
| icon = TOOL_ICONS.get(tool_name, TOOL_ICONS["default"]) |
| files = _files_from_tool(tool_name, tool_input) |
|
|
| events.append(ActivityEvent( |
| timestamp=ts, |
| event_type="tool_call", |
| icon=icon, |
| title=f"calling {tool_name}", |
| detail=_tool_detail(tool_name, tool_input), |
| tool_name=tool_name, |
| files_touched=files, |
| )) |
|
|
| |
| elif msg.role == "tool": |
| call = tool_call_map.get(msg.tool_call_id or "", {}) |
| tool_name = msg.tool_name or call.get("name", "tool") |
| content = msg.content or "" |
|
|
| is_error = False |
| try: |
| result = json.loads(content) |
| if isinstance(result, dict): |
| is_error = bool(result.get("error")) and not result.get("success", True) |
| except Exception: |
| pass |
|
|
| icon = TOOL_ICONS.get(tool_name, TOOL_ICONS["default"]) |
| events.append(ActivityEvent( |
| timestamp=ts, |
| event_type="tool_result", |
| icon="β" if is_error else icon, |
| title=f"{tool_name} {'failed' if is_error else 'done'}", |
| detail=_truncate(content, 200), |
| tool_name=tool_name, |
| is_error=is_error, |
| )) |
|
|
| return events |
|
|