mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-18 14:08:21 +00:00
Some checks are pending
Backend Unit Tests / Build packages (push) Waiting to run
Backend Unit Tests / Codegraph select (push) Waiting to run
Backend Unit Tests / TypeScript type checks (push) Blocked by required conditions
Backend Unit Tests / Circular dependency checks (push) Waiting to run
Backend Unit Tests / Tests: api (shard 1/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: api (shard 2/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: api (shard 3/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: data-provider (push) Blocked by required conditions
Backend Unit Tests / Tests: data-schemas (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 1/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 2/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 3/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 4/4) (push) Blocked by required conditions
Codegraph E2E Votes / vote (full suite) (push) Waiting to run
Docker Dev Branch Images Build / publish (push) Waiting to run
Frontend Unit Tests / Client build recovery regression (push) Waiting to run
Frontend Unit Tests / Codegraph select (push) Waiting to run
Frontend Unit Tests / Build packages (push) Waiting to run
Frontend Unit Tests / TypeScript type checks (client) (push) Blocked by required conditions
Frontend Unit Tests / Tests: @librechat/client (push) Blocked by required conditions
Frontend Unit Tests / Tests: Ubuntu (shard 1/2) (push) Blocked by required conditions
Frontend Unit Tests / Tests: Ubuntu (shard 2/2) (push) Blocked by required conditions
Frontend Unit Tests / Vite build verification (push) Blocked by required conditions
Sync Locize Translations & Create Translation PR / Sync Translation Keys with Locize (push) Waiting to run
Sync Locize Translations & Create Translation PR / Create Translation PR on Version Published (push) Blocked by required conditions
* fix: Keep Reasoning Out of Every Read Aloud Path Read aloud spoke the Thoughts section along with the answer. The hover button flattened think parts into the string it handed to TTS, browser autoplay and the hooks parsed content without skipping reasoning, and server streaming spoke the stored text field, which an aborted turn persists with its reasoning included. Add getSpeechText to data-provider as the single source for spoken text: text parts only for structured content, and the legacy :::thinking block stripped from plain text, treating a still-open block as reasoning so streamed speech only ever grows. Route the hover button, browser autoplay and server streaming through it, and skip reasoning in the TTS hooks' own content parsing. * fix: Speak String Message Content Without Its Reasoning Imported or client-built messages can carry their answer as string content rather than parts; read it like plain text instead of falling back to an empty text field. * fix: Keep Answers Between Repeated Legacy Thinking Blocks Strip every closed :::thinking block before treating an unterminated one as reasoning, so a second closed block no longer swallows the answer that follows it. * fix: Skip Read Aloud and Autoplay When Only Reasoning Remains A turn stopped mid-reasoning has no answer to speak once reasoning is filtered. Gate autoplay on the same speech text it will play, so the external engine never opens a stream that can only time out, and hide the Read aloud action when there is nothing to read. Every speech path reads a finalized message, so speak an unmatched :::thinking marker as the answer text the UI shows instead of dropping the rest. * refactor: Move Speech Chunk Polling Into packages/api The decision of what text to stream as speech lived in the legacy CJS service. Move the chunk processor to packages/api as createSpeechChunkProcessor, taking the message cache and lookup from the caller, and leave streamAudio.js wiring them in. Its tests move with it onto an in-memory cache instead of module mocks. * fix: Speak Exactly the Answer the Message View Shows Share the legacy :::thinking parser between the message view and speech, so Read Aloud strips the same block the view collapses instead of a separate pattern drifting from it. Trim structured speech text so a whitespace-only answer is not speakable, and key autoplay's audio cache by the spoken text, as manual playback already is, so audio cached with reasoning before this change is never replayed. * fix: Fall Back to Text When String Content Is Empty
1011 lines
35 KiB
TypeScript
1011 lines
35 KiB
TypeScript
import {
|
|
parseConvo,
|
|
getSpeechText,
|
|
parseTextParts,
|
|
parseThinkingContent,
|
|
parseCompactConvo,
|
|
replaceSpecialVars,
|
|
getEphemeralSender,
|
|
getResponseSender,
|
|
isConfiguredSender,
|
|
encodeEphemeralAgentId,
|
|
parseEphemeralAgentId,
|
|
} from '../src/parsers';
|
|
import { specialVariables } from '../src/config';
|
|
import { EModelEndpoint, Providers } from '../src/schemas';
|
|
import { ContentTypes } from '../src/types/runs';
|
|
import type { TMessageContentParts } from '../src/types/content';
|
|
import type { TUser, TConversation } from '../src/types';
|
|
|
|
// Mock dayjs module with consistent date/time values regardless of environment
|
|
jest.mock('dayjs', () => {
|
|
const mockDayjs = (input?: unknown) => ({
|
|
format: (format: string) => {
|
|
if (input === '2023-12-31T23:59:58.000Z') {
|
|
if (format === 'YYYY-MM-DD') {
|
|
return '2023-12-31';
|
|
}
|
|
if (format === 'YYYY-MM-DD HH:mm:ss Z') {
|
|
return '2023-12-31 23:59:58 +00:00';
|
|
}
|
|
if (format === 'dddd') {
|
|
return 'Sunday';
|
|
}
|
|
}
|
|
if (format === 'YYYY-MM-DD') {
|
|
return '2024-04-29';
|
|
}
|
|
if (format === 'YYYY-MM-DD HH:mm:ss Z') {
|
|
return '2024-04-29 12:34:56 -04:00';
|
|
}
|
|
if (format === 'dddd') {
|
|
return 'Monday';
|
|
}
|
|
throw new Error(
|
|
`Unhandled dayjs().format() call in mock: "${format}". Update the mock in parsers.spec.ts`,
|
|
);
|
|
},
|
|
toISOString: () =>
|
|
input === '2023-12-31T23:59:58.000Z'
|
|
? '2023-12-31T23:59:58.000Z'
|
|
: '2024-04-29T16:34:56.000Z',
|
|
});
|
|
|
|
mockDayjs.extend = jest.fn();
|
|
|
|
return mockDayjs;
|
|
});
|
|
|
|
describe('replaceSpecialVars', () => {
|
|
// Create a partial user object for testing
|
|
const mockUser = {
|
|
name: 'Test User',
|
|
id: 'user123',
|
|
} as TUser;
|
|
|
|
beforeEach(() => {
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
test('should return the original text if text is empty', () => {
|
|
expect(replaceSpecialVars({ text: '' })).toBe('');
|
|
expect(replaceSpecialVars({ text: null as unknown as string })).toBe(null);
|
|
expect(replaceSpecialVars({ text: undefined as unknown as string })).toBe(undefined);
|
|
});
|
|
|
|
test('should replace {{current_date}} with the current date', () => {
|
|
const result = replaceSpecialVars({ text: 'Today is {{current_date}}' });
|
|
expect(result).toBe('Today is 2024-04-29 (Monday)');
|
|
});
|
|
|
|
test('should replace {{current_datetime}} with the current datetime', () => {
|
|
const result = replaceSpecialVars({ text: 'Now is {{current_datetime}}' });
|
|
expect(result).toBe('Now is 2024-04-29 12:34:56 -04:00 (Monday)');
|
|
});
|
|
|
|
test('should replace {{iso_datetime}} with the ISO datetime', () => {
|
|
const result = replaceSpecialVars({ text: 'ISO time: {{iso_datetime}}' });
|
|
expect(result).toBe('ISO time: 2024-04-29T16:34:56.000Z');
|
|
});
|
|
|
|
test('should use supplied anchor time for date variables', () => {
|
|
const result = replaceSpecialVars({
|
|
text: '{{current_date}} | {{current_datetime}} | {{iso_datetime}}',
|
|
now: '2023-12-31T23:59:58.000Z',
|
|
});
|
|
expect(result).toBe(
|
|
'2023-12-31 (Sunday) | 2023-12-31 23:59:58 +00:00 (Sunday) | 2023-12-31T23:59:58.000Z',
|
|
);
|
|
});
|
|
|
|
test('should replace special variables with surrounding whitespace', () => {
|
|
const result = replaceSpecialVars({
|
|
text: '{{ current_date }} | {{ current_user }}',
|
|
user: mockUser,
|
|
});
|
|
|
|
expect(result).toBe('2024-04-29 (Monday) | Test User');
|
|
});
|
|
|
|
test('should replace {{current_user}} with the user name if provided', () => {
|
|
const result = replaceSpecialVars({
|
|
text: 'Hello {{current_user}}!',
|
|
user: mockUser,
|
|
});
|
|
expect(result).toBe('Hello Test User!');
|
|
});
|
|
|
|
test('should not replace {{current_user}} if user is not provided', () => {
|
|
const result = replaceSpecialVars({
|
|
text: 'Hello {{current_user}}!',
|
|
});
|
|
expect(result).toBe('Hello {{current_user}}!');
|
|
});
|
|
|
|
test('should not replace {{current_user}} if user has no name', () => {
|
|
const result = replaceSpecialVars({
|
|
text: 'Hello {{current_user}}!',
|
|
user: { id: 'user123' } as TUser,
|
|
});
|
|
expect(result).toBe('Hello {{current_user}}!');
|
|
});
|
|
|
|
test('should handle multiple replacements in the same text', () => {
|
|
const result = replaceSpecialVars({
|
|
text: 'Hello {{current_user}}! Today is {{current_date}} and the time is {{current_datetime}}. ISO: {{iso_datetime}}',
|
|
user: mockUser,
|
|
});
|
|
expect(result).toBe(
|
|
'Hello Test User! Today is 2024-04-29 (Monday) and the time is 2024-04-29 12:34:56 -04:00 (Monday). ISO: 2024-04-29T16:34:56.000Z',
|
|
);
|
|
});
|
|
|
|
test('should be case-insensitive when replacing variables', () => {
|
|
const result = replaceSpecialVars({
|
|
text: 'Date: {{CURRENT_DATE}}, User: {{Current_User}}',
|
|
user: mockUser,
|
|
});
|
|
expect(result).toBe('Date: 2024-04-29 (Monday), User: Test User');
|
|
});
|
|
|
|
test('should confirm all specialVariables from config.ts get parsed', () => {
|
|
// Create a text that includes all special variables
|
|
const specialVarsText = Object.keys(specialVariables)
|
|
.map((key) => `{{${key}}}`)
|
|
.join(' ');
|
|
|
|
const result = replaceSpecialVars({
|
|
text: specialVarsText,
|
|
user: mockUser,
|
|
});
|
|
|
|
// Verify none of the original variable placeholders remain in the result
|
|
Object.keys(specialVariables).forEach((key) => {
|
|
const placeholder = `{{${key}}}`;
|
|
expect(result).not.toContain(placeholder);
|
|
});
|
|
|
|
// Verify the expected replacements
|
|
expect(result).toContain('2024-04-29 (Monday)'); // current_date
|
|
expect(result).toContain('2024-04-29 12:34:56 -04:00 (Monday)'); // current_datetime
|
|
expect(result).toContain('2024-04-29T16:34:56.000Z'); // iso_datetime
|
|
expect(result).toContain('Test User'); // current_user
|
|
});
|
|
});
|
|
|
|
describe('parseCompactConvo', () => {
|
|
describe('iconURL security sanitization', () => {
|
|
test('should strip iconURL from OpenAI endpoint conversation input', () => {
|
|
const maliciousIconURL = 'https://evil-tracker.example.com/pixel.png?user=victim';
|
|
const conversation: Partial<TConversation> = {
|
|
model: 'gpt-4',
|
|
iconURL: maliciousIconURL,
|
|
endpoint: EModelEndpoint.openAI,
|
|
};
|
|
|
|
const result = parseCompactConvo({
|
|
endpoint: EModelEndpoint.openAI,
|
|
conversation,
|
|
});
|
|
|
|
expect(result).not.toBeNull();
|
|
expect(result?.['iconURL']).toBeUndefined();
|
|
expect(result?.model).toBe('gpt-4');
|
|
});
|
|
|
|
test('should strip iconURL from agents endpoint conversation input', () => {
|
|
const maliciousIconURL = 'https://evil-tracker.example.com/pixel.png';
|
|
const conversation: Partial<TConversation> = {
|
|
agent_id: 'agent_123',
|
|
iconURL: maliciousIconURL,
|
|
endpoint: EModelEndpoint.agents,
|
|
};
|
|
|
|
const result = parseCompactConvo({
|
|
endpoint: EModelEndpoint.agents,
|
|
conversation,
|
|
});
|
|
|
|
expect(result).not.toBeNull();
|
|
expect(result?.['iconURL']).toBeUndefined();
|
|
expect(result?.agent_id).toBe('agent_123');
|
|
});
|
|
|
|
test('should strip iconURL from anthropic endpoint conversation input', () => {
|
|
const maliciousIconURL = 'https://tracker.malicious.com/beacon.gif';
|
|
const conversation: Partial<TConversation> = {
|
|
model: 'claude-3-opus',
|
|
iconURL: maliciousIconURL,
|
|
endpoint: EModelEndpoint.anthropic,
|
|
};
|
|
|
|
const result = parseCompactConvo({
|
|
endpoint: EModelEndpoint.anthropic,
|
|
conversation,
|
|
});
|
|
|
|
expect(result).not.toBeNull();
|
|
expect(result?.['iconURL']).toBeUndefined();
|
|
expect(result?.model).toBe('claude-3-opus');
|
|
});
|
|
|
|
test('should strip iconURL from google endpoint conversation input', () => {
|
|
const maliciousIconURL = 'https://tracking.example.com/spy.png';
|
|
const conversation: Partial<TConversation> = {
|
|
model: 'gemini-pro',
|
|
iconURL: maliciousIconURL,
|
|
endpoint: EModelEndpoint.google,
|
|
};
|
|
|
|
const result = parseCompactConvo({
|
|
endpoint: EModelEndpoint.google,
|
|
conversation,
|
|
});
|
|
|
|
expect(result).not.toBeNull();
|
|
expect(result?.['iconURL']).toBeUndefined();
|
|
expect(result?.model).toBe('gemini-pro');
|
|
});
|
|
|
|
test('should strip iconURL from assistants endpoint conversation input', () => {
|
|
const maliciousIconURL = 'https://evil.com/track.png';
|
|
const conversation: Partial<TConversation> = {
|
|
assistant_id: 'asst_123',
|
|
iconURL: maliciousIconURL,
|
|
endpoint: EModelEndpoint.assistants,
|
|
};
|
|
|
|
const result = parseCompactConvo({
|
|
endpoint: EModelEndpoint.assistants,
|
|
conversation,
|
|
});
|
|
|
|
expect(result).not.toBeNull();
|
|
expect(result?.['iconURL']).toBeUndefined();
|
|
expect(result?.assistant_id).toBe('asst_123');
|
|
});
|
|
|
|
test('should preserve other conversation properties while stripping iconURL', () => {
|
|
const conversation: Partial<TConversation> = {
|
|
model: 'gpt-4',
|
|
iconURL: 'https://malicious.com/track.png',
|
|
endpoint: EModelEndpoint.openAI,
|
|
temperature: 0.7,
|
|
top_p: 0.9,
|
|
promptPrefix: 'You are a helpful assistant.',
|
|
maxContextTokens: 4000,
|
|
};
|
|
|
|
const result = parseCompactConvo({
|
|
endpoint: EModelEndpoint.openAI,
|
|
conversation,
|
|
});
|
|
|
|
expect(result).not.toBeNull();
|
|
expect(result?.['iconURL']).toBeUndefined();
|
|
expect(result?.model).toBe('gpt-4');
|
|
expect(result?.temperature).toBe(0.7);
|
|
expect(result?.top_p).toBe(0.9);
|
|
expect(result?.promptPrefix).toBe('You are a helpful assistant.');
|
|
expect(result?.maxContextTokens).toBe(4000);
|
|
});
|
|
|
|
test('should handle conversation without iconURL (no error)', () => {
|
|
const conversation: Partial<TConversation> = {
|
|
model: 'gpt-4',
|
|
endpoint: EModelEndpoint.openAI,
|
|
};
|
|
|
|
const result = parseCompactConvo({
|
|
endpoint: EModelEndpoint.openAI,
|
|
conversation,
|
|
});
|
|
|
|
expect(result).not.toBeNull();
|
|
expect(result?.['iconURL']).toBeUndefined();
|
|
expect(result?.model).toBe('gpt-4');
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('parseConvo - defaultParamsEndpoint', () => {
|
|
test('should strip maxOutputTokens for custom endpoint without defaultParamsEndpoint', () => {
|
|
const conversation: Partial<TConversation> = {
|
|
model: 'anthropic/claude-opus-4.5',
|
|
temperature: 0.7,
|
|
maxOutputTokens: 8192,
|
|
maxContextTokens: 50000,
|
|
};
|
|
|
|
const result = parseConvo({
|
|
endpoint: 'MyCustomEndpoint' as EModelEndpoint,
|
|
endpointType: EModelEndpoint.custom,
|
|
conversation,
|
|
});
|
|
|
|
expect(result).not.toBeNull();
|
|
expect(result?.temperature).toBe(0.7);
|
|
expect(result?.maxContextTokens).toBe(50000);
|
|
expect(result?.maxOutputTokens).toBeUndefined();
|
|
});
|
|
|
|
test('should preserve maxOutputTokens when defaultParamsEndpoint is anthropic', () => {
|
|
const conversation: Partial<TConversation> = {
|
|
model: 'anthropic/claude-opus-4.5',
|
|
temperature: 0.7,
|
|
maxOutputTokens: 8192,
|
|
topP: 0.9,
|
|
topK: 40,
|
|
maxContextTokens: 50000,
|
|
};
|
|
|
|
const result = parseConvo({
|
|
endpoint: 'MyCustomEndpoint' as EModelEndpoint,
|
|
endpointType: EModelEndpoint.custom,
|
|
conversation,
|
|
defaultParamsEndpoint: EModelEndpoint.anthropic,
|
|
});
|
|
|
|
expect(result).not.toBeNull();
|
|
expect(result?.model).toBe('anthropic/claude-opus-4.5');
|
|
expect(result?.temperature).toBe(0.7);
|
|
expect(result?.maxOutputTokens).toBe(8192);
|
|
expect(result?.topP).toBe(0.9);
|
|
expect(result?.topK).toBe(40);
|
|
expect(result?.maxContextTokens).toBe(50000);
|
|
});
|
|
|
|
test('should strip OpenAI-specific fields when defaultParamsEndpoint is anthropic', () => {
|
|
const conversation: Partial<TConversation> = {
|
|
model: 'anthropic/claude-opus-4.5',
|
|
temperature: 0.7,
|
|
max_tokens: 4096,
|
|
top_p: 0.9,
|
|
presence_penalty: 0.5,
|
|
frequency_penalty: 0.3,
|
|
};
|
|
|
|
const result = parseConvo({
|
|
endpoint: 'MyCustomEndpoint' as EModelEndpoint,
|
|
endpointType: EModelEndpoint.custom,
|
|
conversation,
|
|
defaultParamsEndpoint: EModelEndpoint.anthropic,
|
|
});
|
|
|
|
expect(result).not.toBeNull();
|
|
expect(result?.temperature).toBe(0.7);
|
|
expect(result?.max_tokens).toBeUndefined();
|
|
expect(result?.top_p).toBeUndefined();
|
|
expect(result?.presence_penalty).toBeUndefined();
|
|
expect(result?.frequency_penalty).toBeUndefined();
|
|
});
|
|
|
|
test('should preserve max_tokens when defaultParamsEndpoint is not set (OpenAI default)', () => {
|
|
const conversation: Partial<TConversation> = {
|
|
model: 'gpt-4o',
|
|
temperature: 0.7,
|
|
max_tokens: 4096,
|
|
top_p: 0.9,
|
|
};
|
|
|
|
const result = parseConvo({
|
|
endpoint: 'MyCustomEndpoint' as EModelEndpoint,
|
|
endpointType: EModelEndpoint.custom,
|
|
conversation,
|
|
});
|
|
|
|
expect(result).not.toBeNull();
|
|
expect(result?.max_tokens).toBe(4096);
|
|
expect(result?.top_p).toBe(0.9);
|
|
});
|
|
|
|
test('should preserve Google-specific fields when defaultParamsEndpoint is google', () => {
|
|
const conversation: Partial<TConversation> = {
|
|
model: 'gemini-pro',
|
|
temperature: 0.7,
|
|
maxOutputTokens: 8192,
|
|
topP: 0.9,
|
|
topK: 40,
|
|
};
|
|
|
|
const result = parseConvo({
|
|
endpoint: 'MyCustomEndpoint' as EModelEndpoint,
|
|
endpointType: EModelEndpoint.custom,
|
|
conversation,
|
|
defaultParamsEndpoint: EModelEndpoint.google,
|
|
});
|
|
|
|
expect(result).not.toBeNull();
|
|
expect(result?.maxOutputTokens).toBe(8192);
|
|
expect(result?.topP).toBe(0.9);
|
|
expect(result?.topK).toBe(40);
|
|
});
|
|
|
|
test('should preserve promptCache when defaultParamsEndpoint is openrouter', () => {
|
|
const conversation: Partial<TConversation> = {
|
|
model: 'anthropic/claude-sonnet-4.6',
|
|
temperature: 0.7,
|
|
max_tokens: 8192,
|
|
promptCache: true,
|
|
};
|
|
|
|
const result = parseConvo({
|
|
endpoint: 'OpenRouter' as EModelEndpoint,
|
|
endpointType: EModelEndpoint.custom,
|
|
conversation,
|
|
defaultParamsEndpoint: Providers.OPENROUTER,
|
|
});
|
|
|
|
expect(result).not.toBeNull();
|
|
expect(result?.max_tokens).toBe(8192);
|
|
expect(result?.promptCache).toBe(true);
|
|
});
|
|
|
|
test('should not strip fields from non-custom endpoints that already have a schema', () => {
|
|
const conversation: Partial<TConversation> = {
|
|
model: 'gpt-4o',
|
|
temperature: 0.7,
|
|
max_tokens: 4096,
|
|
top_p: 0.9,
|
|
};
|
|
|
|
const result = parseConvo({
|
|
endpoint: EModelEndpoint.openAI,
|
|
conversation,
|
|
defaultParamsEndpoint: EModelEndpoint.anthropic,
|
|
});
|
|
|
|
expect(result).not.toBeNull();
|
|
expect(result?.max_tokens).toBe(4096);
|
|
expect(result?.top_p).toBe(0.9);
|
|
});
|
|
|
|
test('should not carry bedrock region to custom endpoint without defaultParamsEndpoint', () => {
|
|
const conversation: Partial<TConversation> = {
|
|
model: 'gpt-4o',
|
|
temperature: 0.7,
|
|
region: 'us-east-1',
|
|
};
|
|
|
|
const result = parseConvo({
|
|
endpoint: 'MyCustomEndpoint' as EModelEndpoint,
|
|
endpointType: EModelEndpoint.custom,
|
|
conversation,
|
|
});
|
|
|
|
expect(result).not.toBeNull();
|
|
expect(result?.temperature).toBe(0.7);
|
|
expect(result?.region).toBeUndefined();
|
|
});
|
|
|
|
test('should fall back to endpointType schema when defaultParamsEndpoint is invalid', () => {
|
|
const conversation: Partial<TConversation> = {
|
|
model: 'gpt-4o',
|
|
temperature: 0.7,
|
|
max_tokens: 4096,
|
|
maxOutputTokens: 8192,
|
|
};
|
|
|
|
const result = parseConvo({
|
|
endpoint: 'MyCustomEndpoint' as EModelEndpoint,
|
|
endpointType: EModelEndpoint.custom,
|
|
conversation,
|
|
defaultParamsEndpoint: 'nonexistent_endpoint',
|
|
});
|
|
|
|
expect(result).not.toBeNull();
|
|
expect(result?.max_tokens).toBe(4096);
|
|
expect(result?.maxOutputTokens).toBeUndefined();
|
|
});
|
|
});
|
|
|
|
describe('parseCompactConvo - defaultParamsEndpoint', () => {
|
|
test('should strip maxOutputTokens for custom endpoint without defaultParamsEndpoint', () => {
|
|
const conversation: Partial<TConversation> = {
|
|
model: 'anthropic/claude-opus-4.5',
|
|
temperature: 0.7,
|
|
maxOutputTokens: 8192,
|
|
};
|
|
|
|
const result = parseCompactConvo({
|
|
endpoint: 'MyCustomEndpoint' as EModelEndpoint,
|
|
endpointType: EModelEndpoint.custom,
|
|
conversation,
|
|
});
|
|
|
|
expect(result).not.toBeNull();
|
|
expect(result?.temperature).toBe(0.7);
|
|
expect(result?.maxOutputTokens).toBeUndefined();
|
|
});
|
|
|
|
test('should preserve maxOutputTokens when defaultParamsEndpoint is anthropic', () => {
|
|
const conversation: Partial<TConversation> = {
|
|
model: 'anthropic/claude-opus-4.5',
|
|
temperature: 0.7,
|
|
maxOutputTokens: 8192,
|
|
topP: 0.9,
|
|
maxContextTokens: 50000,
|
|
};
|
|
|
|
const result = parseCompactConvo({
|
|
endpoint: 'MyCustomEndpoint' as EModelEndpoint,
|
|
endpointType: EModelEndpoint.custom,
|
|
conversation,
|
|
defaultParamsEndpoint: EModelEndpoint.anthropic,
|
|
});
|
|
|
|
expect(result).not.toBeNull();
|
|
expect(result?.maxOutputTokens).toBe(8192);
|
|
expect(result?.topP).toBe(0.9);
|
|
expect(result?.maxContextTokens).toBe(50000);
|
|
});
|
|
|
|
test('should strip iconURL even when defaultParamsEndpoint is set', () => {
|
|
const conversation: Partial<TConversation> = {
|
|
model: 'anthropic/claude-opus-4.5',
|
|
iconURL: 'https://malicious.com/track.png',
|
|
maxOutputTokens: 8192,
|
|
};
|
|
|
|
const result = parseCompactConvo({
|
|
endpoint: 'MyCustomEndpoint' as EModelEndpoint,
|
|
endpointType: EModelEndpoint.custom,
|
|
conversation,
|
|
defaultParamsEndpoint: EModelEndpoint.anthropic,
|
|
});
|
|
|
|
expect(result).not.toBeNull();
|
|
expect(result?.['iconURL']).toBeUndefined();
|
|
expect(result?.maxOutputTokens).toBe(8192);
|
|
});
|
|
|
|
test('should preserve promptCache when compacting OpenRouter custom endpoints', () => {
|
|
const conversation: Partial<TConversation> = {
|
|
model: 'anthropic/claude-sonnet-4.6',
|
|
promptCache: true,
|
|
iconURL: 'https://example.com/icon.png',
|
|
};
|
|
|
|
const result = parseCompactConvo({
|
|
endpoint: 'OpenRouter' as EModelEndpoint,
|
|
endpointType: EModelEndpoint.custom,
|
|
conversation,
|
|
defaultParamsEndpoint: Providers.OPENROUTER,
|
|
});
|
|
|
|
expect(result).not.toBeNull();
|
|
expect(result?.promptCache).toBe(true);
|
|
expect(result?.['iconURL']).toBeUndefined();
|
|
});
|
|
|
|
test('should fall back to endpointType when defaultParamsEndpoint is null', () => {
|
|
const conversation: Partial<TConversation> = {
|
|
model: 'gpt-4o',
|
|
max_tokens: 4096,
|
|
maxOutputTokens: 8192,
|
|
};
|
|
|
|
const result = parseCompactConvo({
|
|
endpoint: 'MyCustomEndpoint' as EModelEndpoint,
|
|
endpointType: EModelEndpoint.custom,
|
|
conversation,
|
|
defaultParamsEndpoint: null,
|
|
});
|
|
|
|
expect(result).not.toBeNull();
|
|
expect(result?.max_tokens).toBe(4096);
|
|
expect(result?.maxOutputTokens).toBeUndefined();
|
|
});
|
|
});
|
|
|
|
describe('getSpeechText', () => {
|
|
test('speaks text parts and skips think parts', () => {
|
|
const content: TMessageContentParts[] = [
|
|
{ type: ContentTypes.THINK, think: 'internal reasoning' },
|
|
{ type: ContentTypes.TEXT, text: 'First.' },
|
|
{ type: ContentTypes.THINK, think: 'more reasoning' },
|
|
{ type: ContentTypes.TEXT, text: 'Second.' },
|
|
];
|
|
expect(getSpeechText({ content, text: 'ignored' })).toBe('First. Second.');
|
|
});
|
|
|
|
test('is empty for a response that only reasoned', () => {
|
|
const content: TMessageContentParts[] = [{ type: ContentTypes.THINK, think: 'reasoning' }];
|
|
expect(getSpeechText({ content })).toBe('');
|
|
});
|
|
|
|
test('never speaks steer parts', () => {
|
|
const content: TMessageContentParts[] = [
|
|
{ type: ContentTypes.STEER, steer: 'user words' } as TMessageContentParts,
|
|
{ type: ContentTypes.TEXT, text: 'answer' },
|
|
];
|
|
expect(getSpeechText({ content })).toBe('answer');
|
|
});
|
|
|
|
test('falls back to plain text when there are no content parts', () => {
|
|
expect(getSpeechText({ content: [], text: 'plain answer' })).toBe('plain answer');
|
|
expect(getSpeechText({ text: 'plain answer' })).toBe('plain answer');
|
|
expect(getSpeechText({ content: null, text: null })).toBe('');
|
|
});
|
|
|
|
test('speaks string content, as imported messages may carry it', () => {
|
|
const content = ':::thinking\nWork it out.\n:::\nThe answer is 4.';
|
|
expect(getSpeechText({ content, text: '' })).toBe('The answer is 4.');
|
|
});
|
|
|
|
test('falls back to text when string content is empty', () => {
|
|
expect(getSpeechText({ content: '', text: 'stored answer' })).toBe('stored answer');
|
|
});
|
|
|
|
test('strips a legacy thinking block from plain text', () => {
|
|
const text = ':::thinking\nWork it out.\n:::\nThe answer is 4.';
|
|
expect(getSpeechText({ text })).toBe('The answer is 4.');
|
|
});
|
|
|
|
test('speaks a repeated thinking block, which the message view shows as answer text', () => {
|
|
const text = ':::thinking\na\n:::\nAnswer 1\n:::thinking\nb\n:::\nAnswer 2';
|
|
expect(getSpeechText({ text })).toBe(parseThinkingContent(text).regularContent);
|
|
expect(getSpeechText({ text })).toBe('Answer 1\n:::thinking\nb\n:::\nAnswer 2');
|
|
});
|
|
|
|
test('trims structured answers so whitespace alone is not speakable', () => {
|
|
const content: TMessageContentParts[] = [
|
|
{ type: ContentTypes.THINK, think: 'reasoning' },
|
|
{ type: ContentTypes.TEXT, text: '\n' },
|
|
];
|
|
expect(getSpeechText({ content })).toBe('');
|
|
});
|
|
|
|
test('speaks an unmatched marker, which the UI shows as answer text', () => {
|
|
const text = 'Wrap reasoning in a :::thinking block.';
|
|
expect(getSpeechText({ text })).toBe(text);
|
|
});
|
|
|
|
test('keeps plain text that merely uses colons', () => {
|
|
expect(getSpeechText({ text: 'Ratio is 3:1 ::: done' })).toBe('Ratio is 3:1 ::: done');
|
|
});
|
|
});
|
|
|
|
describe('parseTextParts', () => {
|
|
test('should concatenate text parts', () => {
|
|
const parts: TMessageContentParts[] = [
|
|
{ type: ContentTypes.TEXT, text: 'Hello' },
|
|
{ type: ContentTypes.TEXT, text: 'World' },
|
|
];
|
|
expect(parseTextParts(parts)).toBe('Hello World');
|
|
});
|
|
|
|
test('should handle text parts with object-style text values', () => {
|
|
const parts: TMessageContentParts[] = [
|
|
{ type: ContentTypes.TEXT, text: { value: 'structured text' } },
|
|
];
|
|
expect(parseTextParts(parts)).toBe('structured text');
|
|
});
|
|
|
|
test('should include think parts by default', () => {
|
|
const parts: TMessageContentParts[] = [
|
|
{ type: ContentTypes.TEXT, text: 'Answer:' },
|
|
{ type: ContentTypes.THINK, think: 'reasoning step' },
|
|
];
|
|
expect(parseTextParts(parts)).toBe('Answer: reasoning step');
|
|
});
|
|
|
|
test('should skip think parts when skipReasoning is true', () => {
|
|
const parts: TMessageContentParts[] = [
|
|
{ type: ContentTypes.THINK, think: 'internal reasoning' },
|
|
{ type: ContentTypes.TEXT, text: 'visible answer' },
|
|
];
|
|
expect(parseTextParts(parts, true)).toBe('visible answer');
|
|
});
|
|
|
|
test('should skip non-text/think part types', () => {
|
|
const parts: TMessageContentParts[] = [
|
|
{ type: ContentTypes.TEXT, text: 'before' },
|
|
{ type: ContentTypes.IMAGE_FILE } as TMessageContentParts,
|
|
{ type: ContentTypes.TEXT, text: 'after' },
|
|
];
|
|
expect(parseTextParts(parts)).toBe('before after');
|
|
});
|
|
|
|
test('should handle undefined elements in the content parts array', () => {
|
|
const parts: Array<TMessageContentParts | undefined> = [
|
|
{ type: ContentTypes.TEXT, text: 'first' },
|
|
undefined,
|
|
{ type: ContentTypes.TEXT, text: 'third' },
|
|
];
|
|
expect(parseTextParts(parts)).toBe('first third');
|
|
});
|
|
|
|
test('should handle multiple consecutive undefined elements', () => {
|
|
const parts: Array<TMessageContentParts | undefined> = [
|
|
undefined,
|
|
undefined,
|
|
{ type: ContentTypes.TEXT, text: 'only text' },
|
|
undefined,
|
|
];
|
|
expect(parseTextParts(parts)).toBe('only text');
|
|
});
|
|
|
|
test('should handle an array of all undefined elements', () => {
|
|
const parts: Array<TMessageContentParts | undefined> = [undefined, undefined, undefined];
|
|
expect(parseTextParts(parts)).toBe('');
|
|
});
|
|
|
|
test('should handle parts with missing type property', () => {
|
|
const parts: Array<TMessageContentParts | undefined> = [
|
|
{ text: 'no type field' } as unknown as TMessageContentParts,
|
|
{ type: ContentTypes.TEXT, text: 'valid' },
|
|
];
|
|
expect(parseTextParts(parts)).toBe('valid');
|
|
});
|
|
|
|
test('should return empty string for empty array', () => {
|
|
expect(parseTextParts([])).toBe('');
|
|
});
|
|
|
|
test('should not add extra spaces when parts already have spacing', () => {
|
|
const parts: TMessageContentParts[] = [
|
|
{ type: ContentTypes.TEXT, text: 'Hello ' },
|
|
{ type: ContentTypes.TEXT, text: 'World' },
|
|
];
|
|
expect(parseTextParts(parts)).toBe('Hello World');
|
|
});
|
|
|
|
test('should exclude steer parts by default (generic extraction must not speak user words)', () => {
|
|
const parts: TMessageContentParts[] = [
|
|
{ type: ContentTypes.TEXT, text: 'assistant output' },
|
|
{ type: ContentTypes.STEER, steer: 'user mid-run words' },
|
|
{ type: ContentTypes.TEXT, text: 'more output' },
|
|
];
|
|
expect(parseTextParts(parts)).toBe('assistant output more output');
|
|
});
|
|
|
|
test('should include steer parts when includeSteer is set', () => {
|
|
const parts: TMessageContentParts[] = [
|
|
{ type: ContentTypes.TEXT, text: 'assistant output' },
|
|
{ type: ContentTypes.STEER, steer: 'user mid-run words' },
|
|
];
|
|
expect(parseTextParts(parts, false, { includeSteer: true })).toBe(
|
|
'assistant output user mid-run words',
|
|
);
|
|
});
|
|
|
|
test('should combine includeSteer with skipReasoning', () => {
|
|
const parts: TMessageContentParts[] = [
|
|
{ type: ContentTypes.THINK, think: 'internal reasoning' },
|
|
{ type: ContentTypes.TEXT, text: 'visible answer' },
|
|
{ type: ContentTypes.STEER, steer: 'steered words' },
|
|
];
|
|
expect(parseTextParts(parts, true, { includeSteer: true })).toBe(
|
|
'visible answer steered words',
|
|
);
|
|
});
|
|
});
|
|
|
|
describe('encodeEphemeralAgentId / parseEphemeralAgentId', () => {
|
|
test('round-trips endpoint and model without a sender', () => {
|
|
const id = encodeEphemeralAgentId({ endpoint: 'openAI', model: 'gpt-4o' });
|
|
expect(id).toBe('openAI__gpt-4o');
|
|
expect(parseEphemeralAgentId(id)).toEqual({
|
|
endpoint: 'openAI',
|
|
model: 'gpt-4o',
|
|
sender: undefined,
|
|
index: undefined,
|
|
});
|
|
});
|
|
|
|
test('round-trips a sender', () => {
|
|
const id = encodeEphemeralAgentId({
|
|
endpoint: 'Together AI',
|
|
model: 'Qwen/Qwen2.5-72B-Instruct',
|
|
sender: 'Fast Qwen',
|
|
});
|
|
expect(id).toBe('Together AI__Qwen/Qwen2.5-72B-Instruct___Fast Qwen');
|
|
expect(parseEphemeralAgentId(id)?.sender).toBe('Fast Qwen');
|
|
expect(parseEphemeralAgentId(id)?.model).toBe('Qwen/Qwen2.5-72B-Instruct');
|
|
});
|
|
|
|
test('round-trips a sender alongside an index suffix', () => {
|
|
const id = encodeEphemeralAgentId({
|
|
endpoint: 'openAI',
|
|
model: 'gpt-4o',
|
|
sender: 'GPT-4o',
|
|
index: 1,
|
|
});
|
|
expect(id).toBe('openAI__gpt-4o___GPT-4o____1');
|
|
expect(parseEphemeralAgentId(id)).toEqual({
|
|
endpoint: 'openAI',
|
|
model: 'gpt-4o',
|
|
sender: 'GPT-4o',
|
|
index: 1,
|
|
});
|
|
});
|
|
|
|
test('omits the sender segment for an empty sender, parsing back to undefined', () => {
|
|
const id = encodeEphemeralAgentId({ endpoint: 'openAI', model: 'gpt-4o', sender: '' });
|
|
expect(id).toBe('openAI__gpt-4o');
|
|
expect(parseEphemeralAgentId(id)?.sender).toBeUndefined();
|
|
});
|
|
|
|
test('restores colons in the endpoint, model, and sender', () => {
|
|
const id = encodeEphemeralAgentId({
|
|
endpoint: 'custom',
|
|
model: 'claude-3:opus',
|
|
sender: 'Label:With:Colons',
|
|
});
|
|
expect(parseEphemeralAgentId(id)).toEqual({
|
|
endpoint: 'custom',
|
|
model: 'claude-3:opus',
|
|
sender: 'Label:With:Colons',
|
|
index: undefined,
|
|
});
|
|
});
|
|
|
|
test('returns undefined for ids without the ephemeral format', () => {
|
|
expect(parseEphemeralAgentId('agent_abc123')).toBeUndefined();
|
|
});
|
|
|
|
/** Characterization of known format quirks (SiblingHeader and the persisted
|
|
* sender both decode this format, so lock the behavior rather than change it):
|
|
* the parser splits on the first `___` and keeps only the next segment, and
|
|
* restores every `__` in the sender to `:`. */
|
|
test('truncates a sender containing a triple underscore (known quirk)', () => {
|
|
const id = encodeEphemeralAgentId({ endpoint: 'openAI', model: 'gpt-4o', sender: 'A___B' });
|
|
expect(parseEphemeralAgentId(id)?.sender).toBe('A');
|
|
});
|
|
|
|
test('decodes a literal double underscore in a sender to a colon (known quirk)', () => {
|
|
const id = encodeEphemeralAgentId({ endpoint: 'openAI', model: 'gpt-4o', sender: 'My__Bot' });
|
|
expect(parseEphemeralAgentId(id)?.sender).toBe('My:Bot');
|
|
});
|
|
});
|
|
|
|
describe('getEphemeralSender', () => {
|
|
test('prefers modelLabel over the spec and endpoint labels', () => {
|
|
expect(
|
|
getEphemeralSender({
|
|
modelLabel: 'My Label',
|
|
specLabel: 'Spec Label',
|
|
modelDisplayLabel: 'Endpoint Label',
|
|
}),
|
|
).toBe('My Label');
|
|
});
|
|
|
|
test('falls back to the spec label, then the endpoint display label', () => {
|
|
expect(
|
|
getEphemeralSender({ specLabel: 'Spec Label', modelDisplayLabel: 'Endpoint Label' }),
|
|
).toBe('Spec Label');
|
|
expect(getEphemeralSender({ modelDisplayLabel: 'Endpoint Label' })).toBe('Endpoint Label');
|
|
});
|
|
|
|
test('returns an empty string when no label is set', () => {
|
|
expect(getEphemeralSender({})).toBe('');
|
|
expect(getEphemeralSender({ modelLabel: null, specLabel: null, modelDisplayLabel: null })).toBe(
|
|
'',
|
|
);
|
|
});
|
|
|
|
/** `??` chain: an empty-string label short-circuits, preserving the exact
|
|
* pre-consolidation behavior of every call site. */
|
|
test('an empty-string modelLabel short-circuits the chain', () => {
|
|
expect(getEphemeralSender({ modelLabel: '', specLabel: 'Spec Label' })).toBe('');
|
|
});
|
|
});
|
|
|
|
describe('isConfiguredSender', () => {
|
|
const gptSender = getResponseSender({ endpoint: EModelEndpoint.openAI, model: 'gpt-4o' });
|
|
|
|
test('is false without a sender to judge', () => {
|
|
expect(isConfiguredSender({ endpoint: EModelEndpoint.openAI, model: 'gpt-4o' })).toBe(false);
|
|
expect(isConfiguredSender({ sender: '', endpoint: EModelEndpoint.openAI })).toBe(false);
|
|
});
|
|
|
|
test('is false for the model-derived name the endpoint produces', () => {
|
|
expect(
|
|
isConfiguredSender({ sender: gptSender, endpoint: EModelEndpoint.openAI, model: 'gpt-4o' }),
|
|
).toBe(false);
|
|
expect(
|
|
isConfiguredSender({
|
|
sender: 'Claude',
|
|
endpoint: EModelEndpoint.anthropic,
|
|
model: 'claude-5',
|
|
}),
|
|
).toBe(false);
|
|
expect(
|
|
isConfiguredSender({ sender: 'Gemini', endpoint: EModelEndpoint.google, model: 'gemini-3' }),
|
|
).toBe(false);
|
|
});
|
|
|
|
test('is true for a label standing in for the model', () => {
|
|
expect(
|
|
isConfiguredSender({ sender: 'Acme', endpoint: EModelEndpoint.openAI, model: 'gpt-4o' }),
|
|
).toBe(true);
|
|
expect(
|
|
isConfiguredSender({ sender: 'Acme', endpoint: EModelEndpoint.anthropic, model: 'claude-5' }),
|
|
).toBe(true);
|
|
});
|
|
|
|
/* An endpoint that ignores the label writes the model-derived name as the sender, so
|
|
equality settles the gating without listing which endpoints honour what. */
|
|
test('follows the sender an endpoint actually wrote', () => {
|
|
const chatGptLabel = 'Acme';
|
|
const openAI = { endpoint: EModelEndpoint.openAI, model: 'gpt-4o', chatGptLabel };
|
|
const anthropic = { endpoint: EModelEndpoint.anthropic, model: 'claude-5', chatGptLabel };
|
|
|
|
expect(isConfiguredSender({ ...openAI, sender: getResponseSender(openAI) })).toBe(true);
|
|
expect(isConfiguredSender({ ...anthropic, sender: getResponseSender(anthropic) })).toBe(false);
|
|
});
|
|
|
|
/* A custom endpoint's `endpoint` is its own configured name, and `getResponseSender`
|
|
reaches its heuristics only through `endpointType`. */
|
|
test('reads an unrecognized endpoint as a custom one', () => {
|
|
expect(
|
|
isConfiguredSender({ sender: gptSender, endpoint: 'Together AI', model: 'gpt-4o' }),
|
|
).toBe(false);
|
|
expect(
|
|
isConfiguredSender({ sender: 'Together', endpoint: 'Together AI', model: 'gpt-4o' }),
|
|
).toBe(true);
|
|
});
|
|
|
|
/* An agent or assistant is named by its author and `getResponseSender` has no branch
|
|
for it, so the header shows that name whether or not the response stored a sender. */
|
|
test('is true for an agent or assistant, stored sender or not', () => {
|
|
expect(isConfiguredSender({ sender: 'My Agent', endpoint: EModelEndpoint.agents })).toBe(true);
|
|
expect(
|
|
isConfiguredSender({ sender: 'My Assistant', endpoint: EModelEndpoint.assistants }),
|
|
).toBe(true);
|
|
expect(isConfiguredSender({ endpoint: EModelEndpoint.agents, model: 'gpt-4o' })).toBe(true);
|
|
});
|
|
|
|
/* A user turn is headed by the person who wrote it: no model to withhold, and its
|
|
`User` sender would never match a derived name. */
|
|
test('is false for a user turn whatever it carries', () => {
|
|
expect(
|
|
isConfiguredSender({
|
|
sender: 'User',
|
|
endpoint: EModelEndpoint.openAI,
|
|
model: 'gpt-4o',
|
|
isCreatedByUser: true,
|
|
}),
|
|
).toBe(false);
|
|
expect(
|
|
isConfiguredSender({
|
|
sender: 'User',
|
|
endpoint: EModelEndpoint.agents,
|
|
isCreatedByUser: true,
|
|
}),
|
|
).toBe(false);
|
|
});
|
|
|
|
/* An endpoint that cannot be named says nothing either way, and reading that silence
|
|
as "configured" would withhold the model from every unlabelled row it reached — one
|
|
such row disables the hover for a whole shared transcript. */
|
|
test('is false when there is no endpoint to derive a name from', () => {
|
|
expect(isConfiguredSender({ sender: 'GPT-4o', model: 'gpt-4o' })).toBe(false);
|
|
expect(isConfiguredSender({ sender: 'Acme', model: 'gpt-4o' })).toBe(false);
|
|
});
|
|
|
|
/* The invariant the helper exists to hold: true exactly when the sender the app wrote
|
|
is one of the configured labels rather than a name derived from the model. */
|
|
test('agrees with the sender chain on every label source', () => {
|
|
const cases = [
|
|
{ endpoint: EModelEndpoint.openAI, model: 'gpt-4o' },
|
|
{ endpoint: EModelEndpoint.openAI, model: 'gpt-4o', modelLabel: 'Acme' },
|
|
{ endpoint: EModelEndpoint.openAI, model: 'gpt-4o', specLabel: 'Acme' },
|
|
{ endpoint: EModelEndpoint.openAI, model: 'gpt-4o', modelDisplayLabel: 'Acme' },
|
|
{ endpoint: EModelEndpoint.openAI, model: 'gpt-4o', chatGptLabel: 'Acme' },
|
|
{ endpoint: EModelEndpoint.anthropic, model: 'claude-5', chatGptLabel: 'Acme' },
|
|
{ endpoint: EModelEndpoint.anthropic, model: 'claude-5', modelLabel: 'Acme' },
|
|
{ endpoint: 'Together AI', endpointType: EModelEndpoint.custom, model: 'qwen' },
|
|
];
|
|
for (const endpointOption of cases) {
|
|
const { modelLabel, specLabel, modelDisplayLabel } = endpointOption as Record<string, string>;
|
|
/** Mirrors `resolveSender`: the label chain first, `getResponseSender` behind it. */
|
|
const sender =
|
|
getEphemeralSender({ modelLabel, specLabel, modelDisplayLabel }) ||
|
|
getResponseSender(endpointOption as never);
|
|
|
|
expect(isConfiguredSender({ ...(endpointOption as never), sender })).toBe(sender === 'Acme');
|
|
}
|
|
});
|
|
});
|