From 23ddfbc51352c8fcc5623d7a7dfb544e9f32aa32 Mon Sep 17 00:00:00 2001 From: ankit25bcs10610 Date: Fri, 3 Jul 2026 02:30:34 +0530 Subject: [PATCH] fix(reka-ai): guard first-message access in chat transform The conversation_history transform dereferenced `messages[0]` before any element was pushed to the local array: - `if (media_url && messages[0].media_url)` throws when the first message is an image (messages is still empty at that point). - `if (messages[0].type !== 'human')` throws when the message list is empty (or fully filtered). Use optional chaining so an image-first or empty message list no longer crashes the transform. Add regression tests. Co-Authored-By: Claude Opus 4.8 (1M context) --- src/providers/reka-ai/chatComplete.test.ts | 33 ++++++++++++++++++++++ src/providers/reka-ai/chatComplete.ts | 4 +-- 2 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 src/providers/reka-ai/chatComplete.test.ts diff --git a/src/providers/reka-ai/chatComplete.test.ts b/src/providers/reka-ai/chatComplete.test.ts new file mode 100644 index 000000000..7faf4e493 --- /dev/null +++ b/src/providers/reka-ai/chatComplete.test.ts @@ -0,0 +1,33 @@ +import { Params } from '../../types/requestBody'; +import { ParameterConfig, ProviderConfig } from '../types'; +import { RekaAIChatCompleteConfig } from './chatComplete'; + +const transformMessages = (config: ProviderConfig) => { + const messagesConfig = config.messages as ParameterConfig; + return (params: Params) => messagesConfig.transform!(params, {} as any); +}; + +describe('RekaAIChatCompleteConfig messages transform', () => { + const transform = transformMessages(RekaAIChatCompleteConfig); + + it('does not throw when the first message is an image', () => { + expect(() => + transform({ + messages: [ + { + role: 'user', + content: [ + { type: 'image_url', image_url: { url: 'https://x/y.png' } }, + ], + }, + ], + } as Params) + ).not.toThrow(); + }); + + it('does not throw when there are no messages', () => { + expect(() => + transform({ messages: [] } as unknown as Params) + ).not.toThrow(); + }); +}); diff --git a/src/providers/reka-ai/chatComplete.ts b/src/providers/reka-ai/chatComplete.ts index 8b217b9f5..2c054c2cc 100644 --- a/src/providers/reka-ai/chatComplete.ts +++ b/src/providers/reka-ai/chatComplete.ts @@ -38,7 +38,7 @@ export const RekaAIChatCompleteConfig: ProviderConfig = { media_url?: string; }) => { // NOTE: can't have more than one image in conversation history - if (media_url && messages[0].media_url) { + if (media_url && messages[0]?.media_url) { return; } @@ -76,7 +76,7 @@ export const RekaAIChatCompleteConfig: ProviderConfig = { } }); - if (messages[0].type !== 'human') { + if (messages[0]?.type !== 'human') { messages.unshift({ type: 'human', text: 'Placeholder for alternation',