4
4
5
5
import typer
6
6
from click import BadArgumentUsage
7
+ from rich .console import Console
8
+ from rich .markdown import Markdown
7
9
8
10
from ..config import cfg
9
11
from ..role import DefaultRoles , SystemRole
@@ -107,15 +109,15 @@ def __init__(self, chat_id: str, role: SystemRole) -> None:
107
109
def initiated (self ) -> bool :
108
110
return self .chat_session .exists (self .chat_id )
109
111
110
- @property
111
- def initial_message (self ) -> str :
112
- chat_history = self .chat_session .get_messages (self .chat_id )
113
- return chat_history [0 ] if chat_history else ""
114
-
115
112
@property
116
113
def is_same_role (self ) -> bool :
117
114
# TODO: Should be optimized for REPL mode.
118
- return self .role .same_role (self .initial_message )
115
+ return self .role .same_role (self .initial_message (self .chat_id ))
116
+
117
+ @classmethod
118
+ def initial_message (cls , chat_id : str ) -> str :
119
+ chat_history = cls .chat_session .get_messages (chat_id )
120
+ return chat_history [0 ] if chat_history else ""
119
121
120
122
@classmethod
121
123
@option_callback
@@ -126,7 +128,15 @@ def list_ids(cls, value: str) -> None:
126
128
127
129
@classmethod
128
130
def show_messages (cls , chat_id : str ) -> None :
129
- # Prints all messages from a specified chat ID to the console.
131
+ if "APPLY MARKDOWN" in cls .initial_message (chat_id ):
132
+ for message in cls .chat_session .get_messages (chat_id ):
133
+ if message .startswith ("assistant:" ):
134
+ Console ().print (Markdown (message ))
135
+ else :
136
+ typer .secho (message , fg = cfg .get ("DEFAULT_COLOR" ))
137
+ typer .echo ()
138
+ return
139
+
130
140
for index , message in enumerate (cls .chat_session .get_messages (chat_id )):
131
141
color = "magenta" if index % 2 == 0 else "green"
132
142
typer .secho (message , fg = color )
@@ -138,7 +148,7 @@ def show_messages_callback(cls, chat_id: str) -> None:
138
148
139
149
def validate (self ) -> None :
140
150
if self .initiated :
141
- chat_role_name = self .role .get_role_name (self .initial_message )
151
+ chat_role_name = self .role .get_role_name (self .initial_message ( self . chat_id ) )
142
152
if not chat_role_name :
143
153
raise BadArgumentUsage (
144
154
f'Could not determine chat role of "{ self .chat_id } "'
0 commit comments