Coverage for sources/mimeogram/prompt.py: 33%
26 statements
« prev ^ index » next coverage.py v7.7.1, created at 2025-03-29 22:32 +0000
« prev ^ index » next coverage.py v7.7.1, created at 2025-03-29 22:32 +0000
1# vim: set filetype=python fileencoding=utf-8:
2# -*- coding: utf-8 -*-
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#============================================================================#
21''' Mimeogram prompt text for LLMs. '''
22# TODO? Use BSD sysexits.
25from __future__ import annotations
27from . import __
28from . import interfaces as _interfaces
31_scribe = __.produce_scribe( __name__ )
34class Command(
35 _interfaces.CliCommand,
36 decorators = ( __.standard_dataclass, __.standard_tyro_class ),
37):
38 ''' Provides LLM prompt text for mimeogram format. '''
40 clip: __.typx.Annotated[
41 __.typx.Optional[ bool ],
42 __.typx.Doc( ''' Copy prompt to clipboard. ''' ),
43 __.tyro.conf.arg( aliases = ( '--clipboard', '--to-clipboard' ) ),
44 ] = None
46 async def __call__( self, auxdata: __.Globals ) -> None:
47 ''' Executes command to provide prompt text. '''
48 await provide_prompt( auxdata )
50 def provide_configuration_edits( self ) -> __.DictionaryEdits:
51 ''' Provides edits against configuration from options. '''
52 edits: list[ __.DictionaryEdit ] = [ ]
53 if None is not self.clip:
54 edits.append( __.SimpleDictionaryEdit( # pyright: ignore
55 address = ( 'prompt', 'to-clipboard' ), value = self.clip ) )
56 return tuple( edits )
59async def acquire_prompt( auxdata: __.Globals ) -> str:
60 ''' Acquires prompt text from package data. '''
61 location = (
62 auxdata.distribution.provide_data_location(
63 'prompts', 'mimeogram.md' ) )
64 return await __.acquire_text_file_async( location )
67async def provide_prompt( auxdata: __.Globals ) -> None:
68 ''' Provides mimeogram prompt text. '''
69 with __.report_exceptions( _scribe, "Could not acquire prompt text." ):
70 prompt = await acquire_prompt( auxdata )
71 options = auxdata.configuration.get( 'prompt', { } )
72 if options.get( 'to-clipboard', False ):
73 from pyperclip import copy
74 with __.report_exceptions(
75 _scribe, "Could not copy prompt to clipboard."
76 ): copy( prompt )
77 _scribe.info( "Copied prompt to clipboard." )
78 else: print( prompt ) # TODO? Use output stream from configuration.
79 raise SystemExit( 0 )