Skip to content
Open
Show file tree
Hide file tree
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
46 changes: 41 additions & 5 deletions src/llm/openai/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1061,7 +1061,10 @@ type ResponsesAnnotationsBoundaryEvent = {
export function ensureResponsesOutputAnnotations(
event: ResponsesAnnotationsBoundaryEvent
): void {
if (event.type !== 'response.completed' && event.type !== 'response.incomplete') {
if (
event.type !== 'response.completed' &&
event.type !== 'response.incomplete'
) {
return;
}
const output = event.response?.output;
Expand Down Expand Up @@ -1330,6 +1333,34 @@ async function completionWithFilteredOpenAIStream(
);
}

/** Mistral streams `delta.content` as typed `thinking`/`text` chunks; flatten them so the
* stream, which skips non-string content, keeps their text and tool calls. */
function flattenTypedDeltaContent(
delta: Record<string, unknown>
): Record<string, unknown> {
if (!Array.isArray(delta.content)) {
return delta;
}
let text = '';
let reasoning = '';
for (const block of delta.content as Array<Record<string, unknown>>) {
if (block.type === 'text' && typeof block.text === 'string') {
text += block.text;
} else if (block.type === 'thinking' && Array.isArray(block.thinking)) {
for (const part of block.thinking as Array<Record<string, unknown>>) {
if (typeof part.text === 'string') {
reasoning += part.text;
}
}
}
}
return {
...delta,
content: text,
...(reasoning !== '' && delta.reasoning == null ? { reasoning } : {}),
};
}

function attachLibreChatDeltaFields(
chunk: BaseMessageChunk,
delta: Record<string, unknown>
Expand Down Expand Up @@ -1937,13 +1968,14 @@ class LibreChatOpenAICompletions extends OriginalChatOpenAICompletions {
rawResponse: OpenAIClient.Chat.Completions.ChatCompletionChunk,
defaultRole?: OpenAIClient.Chat.ChatCompletionRole
): BaseMessageChunk {
const flatDelta = flattenTypedDeltaContent(delta);
const message = attachLibreChatDeltaFields(
super._convertCompletionsDeltaToBaseMessageChunk(
delta,
flatDelta,
rawResponse,
defaultRole
),
delta
flatDelta
);
if (isOfficialOpenAIBaseURL(this.clientConfig.baseURL)) {
return stampSequentialStreamedToolCallAdapter(message);
Expand Down Expand Up @@ -2347,7 +2379,9 @@ class LibreChatOpenAIResponses extends OriginalChatOpenAIResponses {
cache_control: cacheControl,
}),
};
if (shouldIncludeEncryptedReasoning(this.model, params, this.astraRulesApply)) {
if (
shouldIncludeEncryptedReasoning(this.model, params, this.astraRulesApply)
) {
params.include = [
...new Set([
...(params.include ?? []),
Expand Down Expand Up @@ -2630,7 +2664,9 @@ class LibreChatAzureOpenAIResponses extends OriginalAzureChatOpenAIResponses {
promptCacheExplicit: this.promptCacheExplicit,
safetyIdentifier: this.safetyIdentifier,
});
if (shouldIncludeEncryptedReasoning(this.model, params, this.astraRulesApply)) {
if (
shouldIncludeEncryptedReasoning(this.model, params, this.astraRulesApply)
) {
params.include = [
...new Set([
...(params.include ?? []),
Expand Down
81 changes: 81 additions & 0 deletions src/llm/openai/mistralContentChunks.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { describe, expect, test } from '@jest/globals';
import { AIMessageChunk } from '@langchain/core/messages';
import type { BaseMessageChunk } from '@langchain/core/messages';
import { ChatOpenAI } from './index';

type DeltaConverter = {
_convertCompletionsDeltaToBaseMessageChunk(
delta: Record<string, unknown>,
rawResponse: Record<string, unknown>
): BaseMessageChunk;
};

const rawResponse = {
id: 'chatcmpl-1',
object: 'chat.completion.chunk',
created: 1,
model: 'mistral-large-4',
choices: [],
};

function convertDelta(delta: Record<string, unknown>): AIMessageChunk {
const model = new ChatOpenAI({
model: 'mistral-large-4',
apiKey: 'test',
configuration: { baseURL: 'https://gateway.example.com/v1' },
});
const converter = (model as unknown as { completions: DeltaConverter })
.completions;
const message = converter._convertCompletionsDeltaToBaseMessageChunk(
{ role: 'assistant', ...delta },
rawResponse
);
expect(message).toBeInstanceOf(AIMessageChunk);
return message as AIMessageChunk;
}

function thinking(text: string): Record<string, unknown> {
return {
type: 'thinking',
thinking: [{ type: 'text', text }],
closed: true,
};
}

describe('Mistral content chunks on Chat Completions streams', () => {
test('keeps the first words of the answer that share a chunk with the end of the reasoning', () => {
const message = convertDelta({
content: [thinking(' done.'), { type: 'text', text: 'A freelancer' }],
});

expect(message.content).toBe('A freelancer');
expect(message.additional_kwargs.reasoning_content).toBe(' done.');
});

test('keeps a tool call that shares a chunk with the reasoning', () => {
const message = convertDelta({
content: [thinking('.')],
tool_calls: [
{
index: 0,
id: 'chatcmpl-tool-1',
type: 'function',
function: { name: 'read_page', arguments: '{"page_id": "' },
},
],
});

expect(message.content).toBe('');
expect(message.tool_call_chunks?.[0]).toMatchObject({
id: 'chatcmpl-tool-1',
name: 'read_page',
});
});

test('streams a reasoning-only chunk as reasoning with empty text', () => {
const message = convertDelta({ content: [thinking('The user wants')] });

expect(message.content).toBe('');
expect(message.additional_kwargs.reasoning_content).toBe('The user wants');
});
});