Skip to content

feat(cli): 终端输出支持 Markdown 渲染(表格/代码块/列表) #121

Description

@GrantDuan

功能描述

CLI 模式下,助手的回复目前通过 print() 原样输出。当 LLM 返回 Markdown 格式的内容
(尤其是表格、代码块、列表)时,终端显示为原始标记文本,可读性差。

建议新增一个 Markdown 渲染层:检测到 Markdown 结构化语法时,通过
rich.markdown.Markdown 渲染输出;纯文本则保持原样打印。

用途和价值

  • 提升终端交互体验,表格和代码块一目了然
  • 仅影响 CLI 展示层,不改动 Agent 核心逻辑与数据流:
    • ACP 协议路径保持原文发送(由客户端负责渲染)
    • 会话历史保存原文,不受影响

预期使用场景

  • 助手回复中包含对比数据表格(LLM 倾向用 Markdown 表格呈现并列数据)
  • 回复包含代码片段(fenced code block)
  • 任务收尾的总结回复(列表/分节)

设计思路

新增 mini_agent/utils/markdown_renderer.py

  • has_markdown(text) -> bool:高精度启发式检测,宁可漏过、不可误判:
    • 只识别结构性、无歧义构造:表格(表头 + |---| 分隔行)、fenced code block
      (``` / ~~~)、ATX 标题(0-3 空格缩进)、列表项
    • 不检测行内标记(**加粗**`行内代码`__下划线__)——它们极易误判,
      __init__.py 会被当成加粗、2 ** 3 会吞掉运算符
    • 漏检的代价是按纯文本原样打印(无害),误检的代价是 rich 渲染破坏原文内容
  • display_assistant_text(text):检测命中则用 rich 渲染,否则 print()

接入点

  • agent.py 的 CLI 输出路径:print(response.content)display_assistant_text(response.content)

测试

  • 新增 tests/test_markdown_renderer.py(20 个用例):
    • 正例:表格 / fence / 标题 / 列表命中
    • 负例:__init__.py2 ** 3、孤立反引号、散文中的 |#1、缩进注释等不误判

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions