Skip to content

Commit

Permalink
Merge pull request #26 from Libertai/reza/lib-130-add-possibility-to-…
Browse files Browse the repository at this point in the history
…stop-a-message-generation

feat: Button to stop message generation
  • Loading branch information
aliel authored Sep 16, 2024
2 parents f055bff + ea6226b commit f62b441
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions src/pages/Chat.vue
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@
<q-item-label class="tw-block">
<MarkdownRenderer
:class="message.author === 'user' ? '' : 'message-content'"
:content="message.content"
breaks
:content="
message.content +
(!message.stopped && isLoadingRef && message.content !== '' ? ' *[writing ...]*' : '')
"
/>
<!-- Display the loading spinner if the message is still loading -->
<q-spinner-bars v-if="!message.stopped && isLoadingRef" color="white" size="2em" />
Expand All @@ -78,6 +80,16 @@
>
<q-tooltip>Regenerate</q-tooltip>
</q-btn>
<q-btn
v-if="isLoadingRef && message.author === 'ai' && message_index === chatRef.messages.length - 1"
dense
flat
size="sm"
@click="stopGeneration()"
>
<ltai-icon name="stop" />
<q-tooltip>Stop</q-tooltip>
</q-btn>
<!-- Allow copying the message to the clipboard -->
<q-btn dense flat size="sm" @click="copyMessage(message)">
<ltai-icon name="svguse:icons.svg#copy" />
Expand Down Expand Up @@ -137,6 +149,7 @@ const knowledgeStore = useKnowledgeStore();
const isLoadingRef = ref(false);
const scrollAreaRef = ref<HTMLDivElement>();
const enableEditRef = ref(false);
const shouldStopGeneration = ref(false);
const chatRef = ref<Chat>();
Expand Down Expand Up @@ -252,10 +265,11 @@ async function generatePersonaMessage() {
username,
false,
)) {
const stopped = output.stopped;
const stopped = output.stopped || shouldStopGeneration.value;
let content = output.content;
if (!stopped) {
content += ' *[writing ...]*';
if (stopped) {
shouldStopGeneration.value = false;
break;
}
// Update the local state include updates
response.content = content;
Expand All @@ -266,7 +280,7 @@ async function generatePersonaMessage() {
scrollBottom();
}
// A successful response! Append the chat to long term storage.
chatsStore.appendModelResponse(chatId, response.content, [] /*searchResults*/);
chatsStore.appendModelResponse(chatId, response.content, []);
} catch (error) {
console.error('generatePersonaMessage error: ', error);
response.error = error;
Expand All @@ -277,7 +291,6 @@ async function generatePersonaMessage() {
}
}
// TODO: arbitrary message regeneration
// Regenerate the last message from the AI
async function regenerateMessage() {
if (chatRef.value === undefined) {
Expand All @@ -297,6 +310,14 @@ async function regenerateMessage() {
await generatePersonaMessage();
}
function stopGeneration() {
if (chatRef.value === undefined) {
return;
}
shouldStopGeneration.value = true;
}
async function sendMessage({ content, attachments }: SendMessageParams) {
if (chatRef.value === undefined) {
return;
Expand Down

0 comments on commit f62b441

Please sign in to comment.