Coverage for sources/agentsmgr/core.py: 38%

22 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-10-23 02:37 +0000

1# vim: set filetype=python fileencoding=utf-8: 

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

3 

4#============================================================================# 

5# # 

6# Licensed under the Apache License, Version 2.0 (the "License"); # 

7# you may not use this file except in compliance with the License. # 

8# You may obtain a copy of the License at # 

9# # 

10# http://www.apache.org/licenses/LICENSE-2.0 # 

11# # 

12# Unless required by applicable law or agreed to in writing, software # 

13# distributed under the License is distributed on an "AS IS" BASIS, # 

14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # 

15# See the License for the specific language governing permissions and # 

16# limitations under the License. # 

17# # 

18#============================================================================# 

19 

20 

21''' Core types and interfaces for agentsmgr. ''' 

22 

23 

24from . import __ 

25 

26 

27class Renderable( __.typx.Protocol ): 

28 ''' Protocol for objects that can be rendered as Markdown. ''' 

29 

30 def render_as_markdown( self ) -> tuple[ str, ... ]: 

31 ''' Renders object as Markdown lines for display. ''' 

32 ... 

33 

34 

35class Presentations( __.enum.Enum ): 

36 ''' Enumeration for CLI display presentation formats. ''' 

37 

38 Markdown = 'markdown' 

39 

40 

41class DisplayOptions( __.appcore_cli.DisplayOptions ): 

42 ''' Consolidated display configuration for CLI output. ''' 

43 

44 presentation: Presentations = Presentations.Markdown 

45 

46 

47class Globals( __.appcore.state.Globals ): 

48 ''' Agentsmgr-specific global state container. 

49 

50 Extends appcore.state.Globals with agentsmgr-specific display 

51 configuration. 

52 ''' 

53 

54 display: DisplayOptions = __.dcls.field( default_factory = DisplayOptions ) 

55 

56 

57async def render_and_print_result( 

58 result: Renderable, 

59 display: DisplayOptions, 

60 exits: __.ctxl.AsyncExitStack, 

61) -> None: 

62 ''' Centralizes result rendering logic with Rich formatting support. ''' 

63 stream = await display.provide_stream( exits ) 

64 match display.presentation: 

65 case Presentations.Markdown: 

66 lines = result.render_as_markdown( ) 

67 if display.determine_colorization( stream ): 

68 from rich.console import Console 

69 from rich.markdown import Markdown 

70 console = Console( file = stream, force_terminal = True ) 

71 markdown_obj = Markdown( '\n'.join( lines ) ) 

72 console.print( markdown_obj ) 

73 else: 

74 output = '\n'.join( lines ) 

75 print( output, file = stream )