Skip to content

Commit

Permalink
Added Button to cancel run requests
Browse files Browse the repository at this point in the history
  • Loading branch information
jschm42 committed Feb 6, 2024
1 parent 816cd37 commit 37762cf
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Jean Schmitz.
* Copyright (c) 2023-2024 Jean Schmitz.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -183,6 +183,12 @@ public Run getRun(@PathVariable("threadId") String threadId,
return assistantService.retrieveRun(threadId, runId);
}

@PostMapping("/threads/{threadId}/runs/{runId}/cancel")
public Run cancelRun(@PathVariable("threadId") String threadId,
@PathVariable("runId") String runId) {
return assistantService.cancelRun(threadId, runId);
}

@PostMapping("/threads/{threadId}/messages/{messageId}/postprocess")
public ParsedMessageDto postProcessMessage(@PathVariable("threadId") String threadId,
@PathVariable("messageId") String messageId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Jean Schmitz.
* Copyright (c) 2023-2024 Jean Schmitz.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -239,6 +239,10 @@ public Run retrieveRun(String threadId, String runId) {
return this.openAIAssistantService.retrieveRun(threadId, runId);
}

public Run cancelRun(String threadId, String runId) {
return openAIAssistantService.cancelRun(threadId, runId);
}

private ThreadDto mapToDto(ThreadEntity threadEntity) {
return new ThreadDto(threadEntity.getId(), threadEntity.getTitle(),
threadEntity.getCreatedAt());
Expand Down
19 changes: 13 additions & 6 deletions frontend/src/components/chat/ChatControl.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@
<template>
<div class="p-2 m-1 rounded shadow">

<div class="row">
<div class="col-12">
<div class="form-check form-switch d-flex switch-panel">
<div class="d-flex flex-row">
<div class="flex-grow-1">
<div class="form-check form-switch d-flex switch-panel mt-3">
<input id="flexCheckDefault" v-model="localIsAutoSpeak"
class="form-check-input" role="switch" type="checkbox">
<label class="form-check-label mx-2" for="flexCheckDefault">
Auto speak
</label>
</div>
</div>
<button :hidden="isCancelRequestHidden" class="btn btn-outline-warning my-2"
@click="onCancelRun()">Cancel request
</button>
</div>
<div class="row">
<ChatMessageInput @submit-result-received="submitResultReceived"
Expand All @@ -48,9 +51,9 @@ export default {
isAutoSpeak: Boolean,
};
},
getters: {
isAutoSpeak() {
return this.store.autoSpeak;
computed: {
isCancelRequestHidden() {
return this.store.runId === '';
},
},
methods: {
Expand All @@ -62,6 +65,10 @@ export default {
console.log('Chat Control - Chunk Update Received');
this.$emit('chunkUpdateReceived');
},
onCancelRun() {
console.log('Chat Control - Cancel Run');
this.store.cancelCurrentRun();
},
},
setup() {
const store = useChatStore(); // Call useMyStore() inside the setup function
Expand Down
9 changes: 7 additions & 2 deletions frontend/src/components/chat/ChatMessage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ export default {
messageIndex: Number,
},
computed: {
errorMessage() {
return this.store.currentStatusMessage;
},
personaImage() {
// Find the assistant in the store
const assistantId = this.message.assistant_id;
Expand Down Expand Up @@ -131,8 +134,10 @@ export default {
<div v-if="getMessageStatusType() === 'running'" class="spinner-grow text-primary"
role="status">
</div>
<i v-else-if="getMessageStatusType() === 'error'"
class="bi bi-exclamation-lg bg-danger"></i>
<span v-else-if="getMessageStatusType() === 'error'">
<i class="bi bi-exclamation-lg bg-danger"></i>
{{ errorMessage }}
</span>
<div v-else class="card-text text-start" v-html="getContent()"></div>
</div>
</div>
Expand Down
6 changes: 5 additions & 1 deletion frontend/src/service/assistant.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Jean Schmitz.
* Copyright (c) 2023-2024 Jean Schmitz.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -193,6 +193,10 @@ class AssistantService {
return result.data;
}

cancelRun(threadId: string, id: string) {
return axios.post(`/api/v1/threads/${threadId}/runs/${id}/cancel`);
}

private async retrieveThread(threadId: string) {
const result = await axios.get(
`/api/v1/threads/${threadId}`,
Expand Down
18 changes: 15 additions & 3 deletions frontend/src/store/chat-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const useChatStore = defineStore('chat', {
threadMessages: [] as Array<ThreadMessage>,
parsedMessages: {} as any,
threadId: '',
runId: '',
threads: [] as Array<Thread>,
threadEditMode: false,
threadDeleteMode: false,
Expand Down Expand Up @@ -72,8 +73,7 @@ export const useChatStore = defineStore('chat', {
},
async retrieveMessages(threadId: string) {
const parsedMessageList = await assistantService.retrieveMessages(threadId);
console.log('Parsed message list', parsedMessageList);
// Make this.threadMessages a list of ThreadMessage objects

const list = parsedMessageList.message_list?.data?.map((message: any) => {
return new ThreadMessage(message.id, message.role, message.content[0].text.value,
message.assistant_id);
Expand Down Expand Up @@ -115,21 +115,33 @@ export const useChatStore = defineStore('chat', {
try {
const run = await assistantService.runConversation(this.threadId,
this.selectedAssistant.id);
this.runId = run.id;

pollingInterval = setInterval(async () => {
console.log('Polling for run status');
const runT = await assistantService.retrieveRun(this.threadId, run.id);
if (runT.status === 'completed') {
console.log('Run completed');
clearInterval(pollingInterval);
this.runId = '';
await this.handleResult();
} else if (runT.status === 'cancelled') {
console.log('Run cancelled');
clearInterval(pollingInterval);
this.runId = '';
this.updateStatus('Cancelled', 'error');
}
}, 2000);
} catch (e) {
console.error('Error while handling result', e);
if (pollingInterval) clearInterval(pollingInterval);
}
},

async cancelCurrentRun() {
if (this.runId.length > 0) {
await assistantService.cancelRun(this.threadId, this.runId);
}
},
// Usage of the refactored functions
async submitUserMessage(message: string) {
await this.createThreadIfNeeded();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Jean Schmitz.
* Copyright (c) 2023-2024 Jean Schmitz.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -168,6 +168,10 @@ private Request createPostRequest(String body, String path) {
return createRequest("POST", path, body);
}

private Request createPostRequest(String path) {
return createRequest("POST", path, null);
}

private Request createGetRequest(String path) {
return createRequest("GET", path, null);
}
Expand All @@ -180,7 +184,9 @@ private Request createRequest(String method, String path, String body) {
String apiUrl = openAIProperties.apiUrl() + path;

RequestBody requestBody = null;
if (body != null) {
if (body == null && method.equals("POST")) {
requestBody = RequestBody.create("", null);
} else if (body != null) {
requestBody = RequestBody.create(body, JSON);
}

Expand All @@ -201,4 +207,9 @@ public void deleteAssistant(String assistantId) {
Request request = createDeleteRequest("/assistants/" + assistantId);
executeRequest(request, Void.class);
}

public Run cancelRun(String threadId, String runId) {
Request request = createPostRequest("/threads/" + threadId + "/runs/" + runId + "/cancel");
return executeRequest(request, Run.class);
}
}

0 comments on commit 37762cf

Please sign in to comment.