Fix Magistral latest reasoning extraction Fixes #4339

d21afa1ae58e5ab2c1e848d538eb94369de6db98

Cohee <18619528+Cohee1207@users.noreply.github.com>

4 files changed, +14 -1Showing whitespace changes
public/index.html+1 -1
@@ -2065,7 +2065,7 @@
20652065 </span>
20662066 </div>
20672067 </div>
20682068 <div class="range-block" data-source="deepseek,aimlapi,openrouter,custom,claude,xai,makersuite,vertexai,pollinations,moonshot,mistralai">
20692069 <label for="openai_show_thoughts" class="checkbox_label widthFreeExpand">
20702070 <input id="openai_show_thoughts" type="checkbox" />
20712071 <span data-i18n="Request model reasoning">Request model reasoning</span>
public/script.js+5 -0
@@ -5317,6 +5317,7 @@ function parseAndSaveLogprobs(data, continueFrom) {
53175317 * @returns {string} Extracted message
53185318 */
53195319export function extractMessageFromData(data, activeApi = null) {
5320+ function getResult() {
53205321 if (typeof data === 'string') {
53215322 return data;
53225323 }
@@ -5341,6 +5342,10 @@ export function extractMessageFromData(data, activeApi = null) {
53415342 }
53425343 }
53435344
5345+ const result = getResult();
5346+ return Array.isArray(result) ? result.map(x => x.text).filter(x => x).join('') : result;
5347+}
5348+
53445349/**
53455350 * Extracts JSON from the response data.
53465351 * @param {object} data Response data
public/scripts/openai.js+6 -0
@@ -2399,6 +2399,12 @@ export function getStreamingReply(data, state, { chatCompletionSource = null, ov
23992399 '';
24002400 }
24012401 return data.choices?.[0]?.delta?.content ?? data.choices?.[0]?.message?.content ?? data.choices?.[0]?.text ?? '';
2402+ } else if (chat_completion_source === chat_completion_sources.MISTRALAI) {
2403+ if (show_thoughts) {
2404+ state.reasoning += (data.choices?.filter(x => x?.delta?.content?.[0]?.thinking)?.[0]?.delta?.content?.[0]?.thinking?.[0]?.text || '');
2405+ }
2406+ const content = data.choices?.[0]?.delta?.content ?? data.choices?.[0]?.message?.content ?? data.choices?.[0]?.text ?? '';
2407+ return Array.isArray(content) ? content.map(x => x.text).filter(x => x).join('') : content;
24022408 } else {
24032409 return data.choices?.[0]?.delta?.content ?? data.choices?.[0]?.message?.content ?? data.choices?.[0]?.text ?? '';
24042410 }
public/scripts/reasoning.js+2 -0
@@ -118,6 +118,8 @@ export function extractReasoningFromData(data, {
118118 return data?.responseContent?.parts?.filter(part => part.thought)?.map(part => part.text)?.join('\n\n') ?? '';
119119 case chat_completion_sources.CLAUDE:
120120 return data?.content?.find(part => part.type === 'thinking')?.thinking ?? '';
121+ case chat_completion_sources.MISTRALAI:
122+ return data?.choices?.[0]?.message?.content?.[0]?.thinking?.map(part => part.text)?.filter(x => x)?.join('\n\n') ?? '';
121123 case chat_completion_sources.AIMLAPI:
122124 case chat_completion_sources.POLLINATIONS:
123125 case chat_completion_sources.MOONSHOT: