Skip to content

Commit 29edead

Browse files
committed
have a generation indicator
1 parent 71d5b94 commit 29edead

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

oneping/interface/textual.py

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,19 @@ def compose(self):
4646
## widgets
4747
##
4848

49+
def make_text(text, gen=False):
50+
return f'{text} {" ..." if gen else ""}'
51+
4952
class ChatMessage(Markdown):
50-
def __init__(self, title, text, **kwargs):
51-
super().__init__(text, **kwargs)
53+
generating = reactive(False)
54+
55+
def __init__(self, title, text, gen=False, **kwargs):
56+
text0 = make_text(text, gen)
57+
super().__init__(text0, **kwargs)
5258
self.border_title = title
5359
self.styles.border = ('round', role_colors[title])
5460
self._text = text
61+
self.generating = gen
5562

5663
def on_click(self, event):
5764
try:
@@ -60,11 +67,16 @@ def on_click(self, event):
6067
except Exception:
6168
pass
6269

63-
def update(self, text):
64-
if len(text.strip()) == 0:
65-
text = '...'
66-
self._text = text
67-
return super().update(text)
70+
def update_text(self, text=None):
71+
if text is None:
72+
text = self._text
73+
else:
74+
self._text = text
75+
disp = make_text(text, self.generating)
76+
return super().update(disp)
77+
78+
def watch_generating(self, generating):
79+
self.update_text()
6880

6981
# chat history widget
7082
class ChatHistory(VerticalScroll):
@@ -118,15 +130,22 @@ async def on_chat_input_submitted(self, message):
118130
await self.submit_query(message.text)
119131

120132
async def submit_query(self, query):
121-
# mount user query and start response
133+
# make new messages
134+
user = ChatMessage('user', query, gen=False)
135+
response = ChatMessage('assistant', '', gen=True)
136+
137+
# mount new messages
122138
history = self.query_one('ChatHistory')
123-
response = ChatMessage('assistant', '...')
124-
await history.mount(ChatMessage('user', query))
139+
await history.mount(user)
125140
await history.mount(response)
126141

127142
# make update method
128143
def update(reply):
129-
response.update(reply)
144+
if reply is None:
145+
self.log.debug('STREAM DONE')
146+
response.generating = False
147+
return
148+
response.update_text(reply)
130149
history.scroll_end(animate=False)
131150

132151
# send message
@@ -138,7 +157,7 @@ def update(reply):
138157
async def pipe_stream(self, generate, setter):
139158
async for reply in cumcat(generate):
140159
self.app.call_from_thread(setter, reply)
141-
self.log.debug('STREAM DONE')
160+
self.app.call_from_thread(setter, None)
142161

143162
class ConvoStore:
144163
def __init__(self, store):

0 commit comments

Comments
 (0)