第 18 讲:TUI、Dashboard 与桌面应用

Hermes Agent 源码解析 · 第 18 讲

Hermes Agent 源码解析 第 18 讲:TUI、Dashboard 与桌面应用 基于 Hermes Agent v0.16.0 源码 · 2026-06-29 一、三端并行的 UI 架构 前几讲我们深入了 Hermes 的核心引擎。这一讲我们转向用户界面——Hermes 提供了 三种交互方式 :终端 TUI、Web Dashboard 和 Electron 桌面应用,三者共享同一套 Agent 核心。 📦 本讲核心文件 hermes_cli/curses_ui.py (872 行)— TUI 组件库 hermes_cli/subcommands/gui.py (63 行)— 桌面应用启动器 hermes_cli/subcommands/dashboard.py (143 行)— Web Dashboard apps/desktop/ — Electron 桌面应用源码 二、TUI:终端交互界面 curses_ui.py 是 Hermes 的终端 UI 组件库,基于 Python 标准库 curses 实现。它为 hermes tools 和 hermes skills 提供交互式多选列表,支持键盘导航和模糊搜索。 1. 模糊搜索引擎 TUI 的核心亮点是 模糊搜索 ——输入部分字符即可匹配,且匹配质量有精确的评分算法。这套算法是 TypeScript 版本 fuzzy.ts 的 Python 移植,确保三个界面(TUI、Web、Desktop)的搜索结果完全一致。 📄 curses_ui.py (第 59-110 行) def _token_score(orig: str, lower: str, token: str) -> float | None: """评分一个 token 与目标的匹配度 评分规则(与 TS 版本完全一致): - 连续字符匹配:+5 分 - 词边界匹配:+3 分(如 camelCase 的 O 在 gptO 中) - 前缀匹配:+5 分 - 精确匹配:+20 分 - 分散匹配:-1~3 分(惩罚) """ score = 0.0 prev = -1 search_from = 0 for ch in token: idx = lower.find(ch, search_from) if idx = 0 and idx == prev + 1: score += 5 # 连续字符加分 elif prev >= 0: score -= min(idx - prev - 1, 3) # 分散惩罚 if _is_boundary(orig, idx): score += 3 # 词边界加分 if idx == 0: score += 5 # 首字符加分 prev = idx search_from = idx + 1 # 前缀匹配加分 if positions[0] == 0 and positions[-1] == len(positions) - 1: score += 8 # 精确匹配最高分 if lower == token: score += 20 return score 2. 多选列表与键盘导航 TUI 的另一个核心组件是多选列表,支持上下键导航、空格选中、回车确认。当终端不支持 curses 时,自动降级为纯文本编号列表。 📄 curses_ui.py (第 163-200 行) @dataclass class _SearchState: """curses 选择器共享的搜索状态""" active: bool = False query: str = "" def _reconcile_cursor(filtered, cursor): """在过滤后的索引列表中恢复光标位置""" if not filtered: return cursor, 0 if cursor not in filtered: cursor = filtered[0] return cursor, filtered.index(cursor) def _move_filtered_cursor(filtered, cursor, pos, delta): """在过滤列表中移动光标,支持循环""" if not …

阅读全文:Hermes Agent 源码解析 第 18 讲