Coverage for aipyapp/plugins/live_display.py: 0%

1#!/usr/bin/env python

2# -*- coding: utf-8 -*-

3

4 from rich.live import Live

5 from rich.text import Text

6

7 class LiveDisplay:

8 """传统的 Live 显示组件，专门负责实时显示流式内容"""

9

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

16

17 def __enter__(self):

18 self.live = Live(auto_refresh=False, vertical_overflow='crop', transient=True)

19 self.live.__enter__()

20 return self

21

22 def __exit__(self, exc_type, exc_val, exc_tb):

23 self.live.__exit__(exc_type, exc_val, exc_tb)

24 self.live = None

25

26 def update_display(self, lines, reason=False):

27 """更新显示内容"""

28 if self.quiet:

29 return

30

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

38

39 self.display_lines.extend(lines)

40

41 # 限制显示行数，保持最新的行

42 while len(self.display_lines) > self.max_lines:

43 self.display_lines.pop(0)

44

45 # 更新显示

46 display_content = '\n'.join(self.display_lines)

47 self.live.update(Text(display_content, style="dim color(240)"), refresh=True)