_aborter set

This commit is contained in:
Jérôme Commaret 2025-01-10 23:46:58 +01:00
parent af85949561
commit 9abc3ad3dc

View file

@ -32,6 +32,7 @@ interface MistralChunk {
// Mistral
export const sendMistralMsg: _InternalSendLLMMessageFnType = async ({ messages, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter }) => {
let fullText = '';
let aborted = false;
const thisConfig = settingsOfProvider.mistral;
@ -44,6 +45,11 @@ export const sendMistralMsg: _InternalSendLLMMessageFnType = async ({ messages,
apiKey: thisConfig.apiKey
});
// Définir l'aborter avant de commencer le streaming
_setAborter(() => {
aborted = true;
});
try {
// Check if there are messages to process
if (!messages || messages.length === 0) {
@ -80,9 +86,12 @@ export const sendMistralMsg: _InternalSendLLMMessageFnType = async ({ messages,
maxTokens: 2048
});
_setAborter(() => { }); // Mistral does not provide an abort method
for await (const chunk of stream) {
// Vérifier si la requête a été abandonnée
if (aborted) {
return;
}
if (typeof chunk === 'object' && chunk && 'data' in chunk) {
const { data } = chunk as MistralChunk;
if (data.choices?.[0]?.delta?.content) {
@ -93,6 +102,11 @@ export const sendMistralMsg: _InternalSendLLMMessageFnType = async ({ messages,
}
}
// Vérifier une dernière fois si la requête a été abandonnée
if (aborted) {
return;
}
if (!fullText) {
onError({ message: 'No response received from Mistral.', fullError: new Error('No response content') });
return;