Coverage for aipyapp/plugins/live_display.py: 0%
30 statements
« prev ^ index » next coverage.py v7.10.3, created at 2025-08-11 12:02 +0200
« prev ^ index » next coverage.py v7.10.3, created at 2025-08-11 12:02 +0200
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
4from rich.live import Live
5from rich.text import Text
7class LiveDisplay:
8 """传统的 Live 显示组件,专门负责实时显示流式内容"""
10 def __init__(self, quiet=False):
11 self.reason_started = False
12 self.display_lines = []
13 self.max_lines = 10
14 self.quiet = quiet
15 self.live = None
17 def __enter__(self):
18 self.live = Live(auto_refresh=False, vertical_overflow='crop', transient=True)
19 self.live.__enter__()
20 return self
22 def __exit__(self, exc_type, exc_val, exc_tb):
23 self.live.__exit__(exc_type, exc_val, exc_tb)
24 self.live = None
26 def update_display(self, lines, reason=False):
27 """更新显示内容"""
28 if self.quiet:
29 return
31 # 处理思考状态的开始和结束
32 if reason and not self.reason_started:
33 self.display_lines.append("<think>")
34 self.reason_started = True
35 elif not reason and self.reason_started:
36 self.display_lines.append("</think>")
37 self.reason_started = False
39 self.display_lines.extend(lines)
41 # 限制显示行数,保持最新的行
42 while len(self.display_lines) > self.max_lines:
43 self.display_lines.pop(0)
45 # 更新显示
46 display_content = '\n'.join(self.display_lines)
47 self.live.update(Text(display_content, style="dim color(240)"), refresh=True)