Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Button to stop message generation #26

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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