Coverage for sources/mimeogram/prompt.py: 29%

31 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-02-17 22:34 +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''' Mimeogram prompt text for LLMs. ''' 

22# TODO? Use BSD sysexits. 

23 

24 

25from __future__ import annotations 

26 

27from . import __ 

28from . import interfaces as _interfaces 

29 

30 

31_scribe = __.produce_scribe( __name__ ) 

32 

33 

34class Command( 

35 _interfaces.CliCommand, 

36 decorators = ( __.standard_dataclass, __.standard_tyro_class ), 

37): 

38 ''' Provides LLM prompt text for mimeogram format. ''' 

39 

40 clip: __.typx.Annotated[ 

41 __.typx.Optional[ bool ], 

42 __.tyro.conf.arg( 

43 aliases = ( '--clipboard', '--to-clipboard' ), 

44 help = "Copy prompt to clipboard." ), 

45 ] = None 

46 

47 async def __call__( self, auxdata: __.Globals ) -> None: 

48 ''' Executes command to provide prompt text. ''' 

49 await provide_prompt( auxdata ) 

50 

51 def provide_configuration_edits( self ) -> __.DictionaryEdits: 

52 ''' Provides edits against configuration from options. ''' 

53 edits: list[ __.DictionaryEdit ] = [ ] 

54 if None is not self.clip: 

55 edits.append( __.SimpleDictionaryEdit( # pyright: ignore 

56 address = ( 'prompt', 'to-clipboard' ), value = self.clip ) ) 

57 return tuple( edits ) 

58 

59 

60async def acquire_prompt( auxdata: __.Globals ) -> str: 

61 ''' Acquires prompt text from package data. ''' 

62 location = ( 

63 auxdata.distribution.provide_data_location( 

64 'prompts', 'mimeogram.md' ) ) 

65 return await __.acquire_text_file_async( location ) 

66 

67 

68async def provide_prompt( auxdata: __.Globals ) -> None: 

69 ''' Provides mimeogram prompt text. ''' 

70 try: prompt = await acquire_prompt( auxdata ) 

71 except Exception as exc: 

72 _scribe.exception( "Could not acquire prompt text." ) 

73 raise SystemExit( 1 ) from exc 

74 options = auxdata.configuration.get( 'prompt', { } ) 

75 if options.get( 'to-clipboard', False ): 

76 from pyperclip import copy 

77 try: copy( prompt ) 

78 except Exception as exc: 

79 _scribe.exception( "Could not copy prompt to clipboard." ) 

80 raise SystemExit( 1 ) from exc 

81 _scribe.info( "Copied prompt to clipboard." ) 

82 else: print( prompt ) # TODO? Use output stream from configuration. 

83 raise SystemExit( 0 )