mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 01:56:46 +00:00
feat: Abort AI builder requests on chat stop (#17854)
This commit is contained in:
@@ -36,6 +36,7 @@ export const useBuilderStore = defineStore(STORES.BUILDER, () => {
|
||||
const chatWindowOpen = ref<boolean>(false);
|
||||
const streaming = ref<boolean>(false);
|
||||
const assistantThinkingMessage = ref<string | undefined>();
|
||||
const streamingAbortController = ref<AbortController | null>(null);
|
||||
|
||||
// Store dependencies
|
||||
const settings = useSettingsStore();
|
||||
@@ -51,6 +52,7 @@ export const useBuilderStore = defineStore(STORES.BUILDER, () => {
|
||||
const {
|
||||
processAssistantMessages,
|
||||
createUserMessage,
|
||||
createAssistantMessage,
|
||||
createErrorMessage,
|
||||
clearMessages,
|
||||
mapAssistantMessageToUI,
|
||||
@@ -151,6 +153,10 @@ export const useBuilderStore = defineStore(STORES.BUILDER, () => {
|
||||
|
||||
function stopStreaming() {
|
||||
streaming.value = false;
|
||||
if (streamingAbortController.value) {
|
||||
streamingAbortController.value.abort();
|
||||
streamingAbortController.value = null;
|
||||
}
|
||||
}
|
||||
|
||||
// Error handling
|
||||
@@ -166,11 +172,19 @@ export const useBuilderStore = defineStore(STORES.BUILDER, () => {
|
||||
stopStreaming();
|
||||
assistantThinkingMessage.value = undefined;
|
||||
|
||||
if (e.name === 'AbortError') {
|
||||
// Handle abort errors as they are expected when stopping streaming
|
||||
const userMsg = createAssistantMessage('[Task aborted]', 'aborted-streaming');
|
||||
chatMessages.value = [...chatMessages.value, userMsg];
|
||||
return;
|
||||
}
|
||||
|
||||
const errorMessage = createErrorMessage(
|
||||
locale.baseText('aiAssistant.serviceError.message', { interpolate: { message: e.message } }),
|
||||
id,
|
||||
retry,
|
||||
);
|
||||
|
||||
chatMessages.value = [...chatMessages.value, errorMessage];
|
||||
|
||||
telemetry.track('Workflow generation errored', {
|
||||
@@ -247,6 +261,12 @@ export const useBuilderStore = defineStore(STORES.BUILDER, () => {
|
||||
});
|
||||
const retry = createRetryHandler(messageId, async () => sendChatMessage(options));
|
||||
|
||||
// Abort previous streaming request if any
|
||||
if (streamingAbortController.value) {
|
||||
streamingAbortController.value.abort();
|
||||
}
|
||||
|
||||
streamingAbortController.value = new AbortController();
|
||||
try {
|
||||
chatWithBuilder(
|
||||
rootStore.restApiContext,
|
||||
@@ -269,6 +289,7 @@ export const useBuilderStore = defineStore(STORES.BUILDER, () => {
|
||||
},
|
||||
() => stopStreaming(),
|
||||
(e) => handleServiceError(e, messageId, retry),
|
||||
streamingAbortController.value?.signal,
|
||||
);
|
||||
} catch (e: unknown) {
|
||||
handleServiceError(e, messageId, retry);
|
||||
@@ -393,9 +414,11 @@ export const useBuilderStore = defineStore(STORES.BUILDER, () => {
|
||||
toolMessages,
|
||||
workflowMessages,
|
||||
trackingSessionId,
|
||||
streamingAbortController,
|
||||
|
||||
// Methods
|
||||
updateWindowWidth,
|
||||
stopStreaming,
|
||||
closeChat,
|
||||
openChat,
|
||||
resetBuilderChat,
|
||||
|
||||
Reference in New Issue
Block a user