Coverage for cli/display.py: 23%

1"""

2显示/渲染模块

3

4负责上下文状态栏等与纯展示相关的逻辑。

5"""

6 from rich.console import Console

7 from rich.text import Text

8 from rich.panel import Panel

9 from qrclaw.memory.session import Session

10 from qrclaw.config import _MODEL_MAX_TOKENS, COMPRESS_THRESHOLD

11

12

13 def show_context_usage(console: Console, session: Session) -> None:

14 """在控制台打印当前会话的上下文使用情况。"""

15 percentage = (

16 (session.prompt_tokens / _MODEL_MAX_TOKENS) * 100

17 if session.prompt_tokens > 0

18 else 0

19 )

20

21 if percentage < 50:

22 color = "green"

23 elif percentage < 70:

24 color = "yellow"

25 else:

26 color = "red"

27

28 text = Text()

29 text.append(f"[{session.session_id}] ", style="bold cyan")

30 text.append("上下文: ", style="dim")

31 text.append(f"{percentage:.1f}%", style=f"bold {color}")

32 text.append(f" ({session.prompt_tokens:,}/{_MODEL_MAX_TOKENS:,} tokens)", style="dim")

33

34 if session.prompt_tokens > COMPRESS_THRESHOLD:

35 text.append(" ⚠ 接近压缩阈值", style="bold red")

36

37 console.print(text)

38

39

40 def show_plan_progress(console: Console, session: Session) -> None:

41 """打印当前执行计划进度，无计划时不显示。"""

42 if not session.active_plan:

43 return

44

45 plan = session.active_plan

46 lines = Text()

47 lines.append(f"目标：{plan['goal']}\n", style="bold")

48

49 for step in plan["steps"]:

50 if step["done"]:

51 lines.append(f" ✅ Step {step['id']}: {step['description']}\n", style="dim green")

52 else:

53 lines.append(f" ⬜ Step {step['id']}: {step['description']}\n", style="white")

54

55 console.print(Panel(

56 lines,

57 title="[bold yellow]执行计划[/bold yellow]",

58 border_style="yellow",

59 expand=False,

60 ))