diff --git a/packages/api/src/agents/__tests__/run-summarization.test.ts b/packages/api/src/agents/__tests__/run-summarization.test.ts index 90e96ee850..097291c6d5 100644 --- a/packages/api/src/agents/__tests__/run-summarization.test.ts +++ b/packages/api/src/agents/__tests__/run-summarization.test.ts @@ -1498,6 +1498,148 @@ describe.each([false, true])( }, ); +describe('Cyber direct endpoint summary routing', () => { + it.each([ + { sourceModel: 'gpt-6-cyber', path: 'chat/completions', responses: false }, + { sourceModel: 'gpt-4.1-mini', path: 'chat/completions', responses: false }, + { sourceModel: 'gpt-6-cyber', path: 'responses', responses: true }, + { sourceModel: 'gpt-4.1-mini', path: 'responses', responses: true }, + { sourceModel: 'gpt-6-cyber', path: 'chat/completions', summary: true, responses: true }, + { sourceModel: 'gpt-6-cyber', path: 'responses', summary: false, responses: false }, + { + sourceModel: 'gpt-4.1-mini', + path: 'chat/completions', + crossEndpoint: true, + responses: false, + }, + { sourceModel: 'gpt-4.1-mini', path: 'responses', crossEndpoint: true, responses: true }, + ])( + 'matches the exact endpoint API: %j', + async ({ sourceModel, path, summary, crossEndpoint, responses }) => { + const endpoint: TestCustomEndpoint = { + name: 'Daybreak', + apiKey: 'test-key', + baseURL: `https://api.openai.com/v1/${path}`, + directEndpoint: true, + }; + const user = { id: 'direct-user', tenantId: 'direct-tenant' } as IUser; + const sourceEndpoint = crossEndpoint ? EModelEndpoint.openAI : endpoint.name; + const { llmConfig, configOptions } = getOpenAIConfig( + endpoint.apiKey, + { + reverseProxyUrl: crossEndpoint ? undefined : endpoint.baseURL, + directEndpoint: !crossEndpoint, + modelOptions: { model: sourceModel }, + requestIdentity: { userId: user.id, tenantId: user.tenantId }, + }, + sourceEndpoint, + ); + const agents = await callAndCapture({ + agents: [ + makeReasoningAgent({ + provider: Providers.OPENAI, + endpoint: sourceEndpoint, + model: sourceModel, + model_parameters: { ...llmConfig, configuration: configOptions }, + }), + ], + user, + tenantId: user.tenantId, + appConfig: makeAppConfig([endpoint]), + summarizeOnly: true, + summarizationConfig: { + provider: endpoint.name, + model: 'gpt-6-cyber', + parameters: { + streaming: false, + ...(typeof summary === 'boolean' ? { useResponsesApi: summary } : {}), + }, + }, + }); + const { requests } = await compactSummary(agents); + expect(requests).toHaveLength(1); + expect(requests[0].body).toHaveProperty(responses ? 'input' : 'messages'); + expect(requests[0].body).not.toHaveProperty(responses ? 'messages' : 'input'); + expect(requests[0].body).toMatchObject({ + model: 'gpt-6-cyber', + access_programs: { cyber: 'daybreak_red' }, + safety_identifier: expect.stringMatching(/^[a-f0-9]{64}$/), + }); + }, + ); + + it.each([ + { agent: 'anthropic', path: '/chat/completions', responses: false }, + { agent: 'anthropic', path: '/responses', responses: true }, + { agent: 'anthropic', path: '/chat/completions', drop: true, responses: false }, + { agent: 'anthropic', path: '/responses', drop: true, responses: false }, + { agent: 'anthropic', path: '', drop: true, responses: false }, + { agent: 'openAI', path: '/chat/completions', drop: true, responses: false }, + { agent: 'endpoint', path: '/chat/completions', drop: true, responses: false }, + { agent: 'endpoint', path: '/chat/completions', inferred: true, responses: false }, + { agent: 'endpoint', path: '/responses', inferred: true, responses: true }, + ])( + 'keeps the target endpoint API decision: %j', + async ({ agent, path, drop, inferred, responses }) => { + const endpoint: TestCustomEndpoint = { + name: 'Daybreak', + apiKey: 'test-key', + baseURL: `https://api.openai.com/v1${path}`, + directEndpoint: path !== '', + dropParams: drop ? ['useResponsesApi'] : undefined, + }; + const user = { id: 'direct-user', tenantId: 'direct-tenant' } as IUser; + const onEndpoint = agent === 'endpoint'; + const { llmConfig, configOptions } = getOpenAIConfig( + endpoint.apiKey, + { + reverseProxyUrl: onEndpoint ? endpoint.baseURL : undefined, + directEndpoint: onEndpoint && endpoint.directEndpoint, + dropParams: onEndpoint ? endpoint.dropParams : undefined, + modelOptions: { model: 'gpt-4.1-mini' }, + requestIdentity: { userId: user.id, tenantId: user.tenantId }, + }, + onEndpoint ? endpoint.name : EModelEndpoint.openAI, + ); + const source = + agent === 'anthropic' + ? makeReasoningAgent({ + provider: Providers.ANTHROPIC, + endpoint: EModelEndpoint.anthropic, + model: 'claude-sonnet-4.6', + model_parameters: { model: 'claude-sonnet-4.6', apiKey: 'anthropic-key' }, + }) + : makeReasoningAgent({ + provider: Providers.OPENAI, + endpoint: onEndpoint ? endpoint.name : EModelEndpoint.openAI, + model: 'gpt-4.1-mini', + model_parameters: { ...llmConfig, configuration: configOptions }, + }); + const agents = await callAndCapture({ + agents: [source], + user, + tenantId: user.tenantId, + appConfig: makeAppConfig([endpoint]), + summarizeOnly: true, + summarizationConfig: { + ...(inferred ? {} : { provider: endpoint.name }), + model: 'gpt-6-cyber', + parameters: { streaming: false }, + }, + }); + const { requests } = await compactSummary(agents); + expect(requests).toHaveLength(1); + expect(requests[0].body).toHaveProperty(responses ? 'input' : 'messages'); + expect(requests[0].body).not.toHaveProperty(responses ? 'messages' : 'input'); + expect(requests[0].body).toMatchObject({ + model: 'gpt-6-cyber', + access_programs: { cyber: 'daybreak_red' }, + safety_identifier: expect.stringMatching(/^[a-f0-9]{64}$/), + }); + }, + ); +}); + describe('Cyber summary target shaping', () => { it.each([ { sourceModel: 'gpt-6-sol', baseURL: undefined, endpoint: EModelEndpoint.openAI }, diff --git a/packages/api/src/agents/run.ts b/packages/api/src/agents/run.ts index 4679913878..c18fa322bb 100644 --- a/packages/api/src/agents/run.ts +++ b/packages/api/src/agents/run.ts @@ -1366,6 +1366,14 @@ function resolveSummarizationProvider( * precedence). */ const provider = detectedProvider ?? overrideProvider; + /** + * `getOpenAIConfig` honors an administrator's `useResponsesApi` drop by removing the field. + * Keep the decision itself, so a target that defaults to the Responses API is not routed + * there again when its request is shaped. + */ + if (customEndpointConfig.dropParams?.includes('useResponsesApi')) { + clientOverrides.useResponsesApi = false; + } /** * On the agent's provider the SDK layers these over the agent's own client options, so this * different endpoint replaces the agent's API mode, first-party declaration, reasoning and @@ -1556,6 +1564,7 @@ function shapeSummarizationConfig( apiKey: '', streaming: false, ...nativeRequest, + directEndpoint: endpointConfig?.directEndpoint, requestIdentity: { userId: headerContext.user?.id, tenantId: headerContext.tenantId ?? headerContext.user?.tenantId, diff --git a/packages/api/src/endpoints/openai/config.ts b/packages/api/src/endpoints/openai/config.ts index bf6ce942a3..e7fcf7c7ae 100644 --- a/packages/api/src/endpoints/openai/config.ts +++ b/packages/api/src/endpoints/openai/config.ts @@ -202,6 +202,7 @@ export function getOpenAIConfig( apiKey, baseURL, endpoint, + directEndpoint, streaming, addParams, dropParams, diff --git a/packages/api/src/endpoints/openai/cyber.spec.ts b/packages/api/src/endpoints/openai/cyber.spec.ts index 4486e38ce0..2876b06a4b 100644 --- a/packages/api/src/endpoints/openai/cyber.spec.ts +++ b/packages/api/src/endpoints/openai/cyber.spec.ts @@ -1,3 +1,4 @@ +import fetch from 'node-fetch'; import { createHash } from 'node:crypto'; import { HumanMessage } from '@librechat/agents/langchain'; import { Providers, initializeModel } from '@librechat/agents'; @@ -7,6 +8,8 @@ import { initializeOpenAI } from './initialize'; import { getOpenAILLMConfig } from './llm'; import { getOpenAIConfig } from './config'; +jest.mock('node-fetch', () => jest.fn()); + const model = 'gpt-6-cyber'; const identity = { userId: 'authenticated-user', tenantId: 'tenant-a' }; const safetyId = (userId = identity.userId, tenantId = identity.tenantId) => @@ -434,3 +437,157 @@ describe('Cyber SDK-effective URL routing', () => { }, ); }); + +describe('Cyber exact endpoint contract', () => { + it.each([ + { path: 'chat/completions', responses: false }, + { path: 'responses', responses: true }, + ])('matches the exact $path API with the installed SDK', async ({ path, responses }) => { + const exactURL = `https://api.openai.com/v1/${path}?route=cyber`; + const urls: string[] = []; + const bodies: Record[] = []; + jest.mocked(fetch).mockImplementationOnce(async (url, init) => { + urls.push(String(url)); + const body = JSON.parse(String(init?.body)); + bodies.push(body); + const valid = responses ? Array.isArray(body.input) : Array.isArray(body.messages); + const { Response } = jest.requireActual('node-fetch'); + return new Response( + JSON.stringify( + valid + ? { + id: 'exact-cyber', + object: 'response', + status: 'completed', + model, + choices: [ + { + index: 0, + message: { role: 'assistant', content: 'OK' }, + finish_reason: 'stop', + }, + ], + output: [ + { + type: 'message', + id: 'msg_exact', + role: 'assistant', + status: 'completed', + content: [{ type: 'output_text', text: 'OK', annotations: [] }], + }, + ], + usage: { + prompt_tokens: 10, + completion_tokens: 2, + input_tokens: 10, + output_tokens: 2, + total_tokens: 12, + }, + } + : { + error: { + message: 'Wrong request schema for exact API path', + type: 'invalid_request_error', + }, + }, + ), + { status: valid ? 200 : 400, headers: { 'content-type': 'application/json' } }, + ); + }); + const { llmConfig, configOptions } = getOpenAIConfig( + 'test-key', + { + directEndpoint: true, + reverseProxyUrl: exactURL, + streaming: false, + requestIdentity: identity, + modelOptions: { model, max_tokens: 1024 }, + addParams: { maxRetries: 0, access_programs: { cyber: 'standard' } }, + }, + 'Daybreak', + ); + const llm = initializeModel({ + provider: Providers.OPENAI, + clientOptions: { + ...llmConfig, + verbosity: undefined, + configuration: configOptions, + }, + }); + await llm.invoke('Verify exact API routing'); + expect(urls).toEqual([exactURL]); + expect(bodies).toHaveLength(1); + expect(bodies[0]).toMatchObject({ + model, + access_programs: { cyber: 'standard' }, + safety_identifier: safetyId(), + [responses ? 'max_output_tokens' : 'max_completion_tokens']: 1024, + }); + expect(bodies[0]).not.toHaveProperty(responses ? 'messages' : 'input'); + expect(bodies[0]).not.toHaveProperty('user'); + }); +}); + +it.each([ + { options: {}, expected: false }, + { options: { addParams: { useResponsesApi: true } }, expected: true }, + { options: { modelOptions: { model, useResponsesApi: true } }, expected: true }, + { options: { defaultParams: { useResponsesApi: true } }, expected: true }, + { options: { dropParams: ['useResponsesApi'] }, expected: undefined }, + { + options: { addParams: { useResponsesApi: true }, dropParams: ['useResponsesApi'] }, + expected: undefined, + }, +])('preserves exact endpoint API overrides and drops: %j', ({ options, expected }) => { + const result = config({ + directEndpoint: true, + baseURL: 'https://api.openai.com/v1/chat/completions/?route=cyber', + ...options, + }); + expect(result.useResponsesApi).toBe(expected); + expect(result.modelKwargs).toMatchObject({ + access_programs: { cyber: 'daybreak_red' }, + safety_identifier: safetyId(), + }); +}); + +it.each([ + { baseURL: 'https://api.openai.com/v1/chat/completions', expected: false }, + { baseURL: 'https://api.openai.com/v1/responses', expected: true }, + { baseURL: 'https://api.openai.com/v1', expected: true }, + { baseURL: 'https://api.openai.com/v1/chat/completions/batch', expected: true }, + { + baseURL: 'https://api.openai.com/v1/responses', + addParams: { useResponsesApi: false }, + expected: false, + }, +])('defaults the API from the exact URL path: %j', ({ expected, ...options }) => { + const result = config({ endpoint: 'Daybreak', directEndpoint: true, ...options }); + expect(result.useResponsesApi).toBe(expected); + expect(result.modelKwargs).toMatchObject({ + access_programs: { cyber: 'daybreak_red' }, + safety_identifier: safetyId(), + }); +}); + +it.each([ + { baseURL: 'https://gateway.example/v1/chat/completions', directEndpoint: true }, + { baseURL: 'https://gateway.example/v1/responses', directEndpoint: true }, + { + baseURL: 'https://openrouter.ai/api/v1/chat/completions', + directEndpoint: true, + useOpenRouter: true, + }, +])('does not impose native Cyber routing or identity on exact gateways: %j', (options) => { + const result = config({ endpoint: 'Gateway', ...options }); + expect(result.useResponsesApi).toBeUndefined(); + expect(result.modelKwargs ?? {}).not.toHaveProperty('access_programs'); + expect(result.modelKwargs ?? {}).not.toHaveProperty('safety_identifier'); +}); + +it('does not infer an exact API schema when directEndpoint is disabled', () => { + expect( + config({ directEndpoint: false, baseURL: 'https://api.openai.com/v1/chat/completions' }) + .useResponsesApi, + ).toBe(true); +}); diff --git a/packages/api/src/endpoints/openai/llm.ts b/packages/api/src/endpoints/openai/llm.ts index 2477b8c4d3..e553f99ccb 100644 --- a/packages/api/src/endpoints/openai/llm.ts +++ b/packages/api/src/endpoints/openai/llm.ts @@ -185,6 +185,22 @@ function isCanonicalOpenAIBaseURL(baseURL?: string | null): boolean { } } +/** + * A `directEndpoint` sends every request to its configured URL, whichever API + * the SDK selected, so a URL naming Chat Completions fixes the request schema + * along with the destination. + */ +function isExactChatCompletionsURL(directEndpoint?: boolean, baseURL?: string | null): boolean { + if (directEndpoint !== true || !baseURL) { + return false; + } + try { + return /\/chat\/completions\/?$/.test(new URL(baseURL).pathname); + } catch { + return false; + } +} + export function isNativeCyberRequest({ model, endpoint, @@ -640,6 +656,7 @@ export function getOpenAILLMConfig({ defaultParams, inferenceParameters, useOpenRouter, + directEndpoint, requestIdentity, reasoningFormat = ReasoningParameterFormat.reasoningEffort, modelOptions: _modelOptions, @@ -655,6 +672,7 @@ export function getOpenAILLMConfig({ defaultParams?: Record; inferenceParameters?: Record; useOpenRouter?: boolean; + directEndpoint?: boolean; reasoningFormat?: ReasoningParameterFormat; azure?: false | t.AzureOptions; }): Pick & { @@ -1005,13 +1023,15 @@ export function getOpenAILLMConfig({ /** * Route GPT-6 to Responses before invocation, including unset effort, so tools * bound later cannot accidentally reach Chat Completions with default reasoning. + * An exact Chat Completions URL stays on Chat Completions, since the request + * reaches that URL whichever API is selected. */ if ( firstPartyResponsesModel && llmConfig.useResponsesApi == null && !responsesApiExplicitlyOptedOut ) { - llmConfig.useResponsesApi = true; + llmConfig.useResponsesApi = !isExactChatCompletionsURL(directEndpoint, baseURL); } /**