LibreChat/api/server/controllers/agents/__tests__/modelEndHandler.spec.js
Marco Beretta b075605e98
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
Backend Unit Tests / backend (push) Blocked by required conditions
Channel Gateway / gateway (push) Waiting to run
Codegraph E2E Vote Gate / Gate (push) Waiting to run
Codegraph E2E Votes / vote (shard 1/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 10/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 2/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 3/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 4/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 5/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 6/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 7/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 8/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 9/10) (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/chat (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
Frontend Unit Tests / frontend (push) Blocked by required conditions
Playwright E2E Tests / Codegraph select (push) Waiting to run
Playwright E2E Tests / Shard timings (push) Waiting to run
Playwright E2E Tests / e2e () (push) Blocked by required conditions
Playwright E2E Tests / MCP list_changed (replica count 1) (push) Blocked by required conditions
Playwright E2E Tests / MCP list_changed (replica count 2) (push) Blocked by required conditions
Playwright E2E Tests / MCP Apps (browser and server) (push) Waiting to run
Playwright E2E Tests / e2e (push) Blocked by required conditions
Playwright Shard Timings / Record shard timings (push) Waiting to run
💧 feat: Add Gemini Hydrogen Support (#18159)
* feat: Add Gemini Hydrogen Model Support

* fix: Restore Google History For Reachable Agents

* fix: Preserve Mixed Google Signature History

* refactor: Inject Google billing configuration at application boundary

* fix: Preserve Call Signatures From Unsupported Google Output

* chore: Use Released Agents SDK 4.2.5

---------

Co-authored-by: Lia <lia@librechat.ai>
2026-11-16 10:26:55 -05:00

332 lines
11 KiB
JavaScript

jest.mock('@librechat/data-schemas', () => ({
logger: { error: jest.fn(), debug: jest.fn(), info: jest.fn() },
}));
jest.mock('@librechat/api', () => {
const {
getModelRefusalInfo,
getModelEndUsage,
captureResponseAccountingUsage,
collectGoogleThoughtSignatures,
} = jest.requireActual('@librechat/api');
return {
sendEvent: jest.fn(),
emitEvent: jest.fn(),
createToolExecuteHandler: jest.fn(),
markSummarizationUsage: (usage) => usage,
getModelRefusalInfo,
getModelEndUsage,
captureResponseAccountingUsage,
collectGoogleThoughtSignatures,
};
});
jest.mock('~/server/services/Files/Citations', () => ({
processFileCitations: jest.fn(),
}));
jest.mock('~/server/services/Files/Code/process', () => ({
processCodeOutput: jest.fn(),
runPreviewFinalize: jest.fn(),
}));
jest.mock('~/server/services/Files/process', () => ({
saveBase64Image: jest.fn(),
}));
const { ModelEndHandler, contextualizeModelUsage } = require('../callbacks');
const buildGraph = () => ({
getAgentContext: () => ({
provider: 'vertexai',
clientOptions: { model: 'gemini-3.1-flash-lite-preview' },
}),
});
describe('ModelEndHandler: Google thoughtSignature capture (issue #13006 follow-up)', () => {
it('leaves usage usable when graph context is unavailable', () => {
const usage = { input_tokens: 10, output_tokens: 5 };
expect(contextualizeModelUsage(usage, undefined, undefined)).toEqual({
...usage,
accountingIdentity: { modelSource: 'unknown' },
});
expect(contextualizeModelUsage(usage, undefined, null)).toEqual({
...usage,
accountingIdentity: { modelSource: 'unknown' },
});
});
it('captures configured provenance before billing fills the model and keeps it on repeated contextualization', () => {
const usage = { input_tokens: 10, output_tokens: 5 };
const context = { provider: 'bedrock', clientOptions: { model: 'configured-model' } };
const first = contextualizeModelUsage(usage, undefined, context);
expect(first.model).toBe('configured-model');
expect(first.accountingIdentity).toEqual({
model: 'configured-model',
provider: 'bedrock',
modelSource: 'configured',
});
expect(contextualizeModelUsage(first, undefined, context).accountingIdentity).toEqual(
first.accountingIdentity,
);
expect(usage).not.toHaveProperty('accountingIdentity');
});
it('prefers the actually invoked fallback provider and model', () => {
const usage = { input_tokens: 10, output_tokens: 5 };
const result = contextualizeModelUsage(
usage,
{
__invoked_provider: 'anthropic',
__invoked_model: 'claude-fallback',
},
{
provider: 'bedrock',
agentId: 'agent-1',
clientOptions: { model: 'configured-model' },
},
);
expect(result).toEqual({
...usage,
provider: 'anthropic',
model: 'claude-fallback',
agentId: 'agent-1',
accountingIdentity: {
model: 'claude-fallback',
provider: 'anthropic',
modelSource: 'runtime_reported',
},
});
});
it('prefers provider-reported model metadata over the invoked fallback model', () => {
expect(
contextualizeModelUsage(
{ input_tokens: 10, output_tokens: 5 },
{ ls_model_name: 'reported-model', __invoked_model: 'fallback-model' },
{ clientOptions: { model: 'configured-model' } },
).model,
).toBe('reported-model');
});
it('does not guess call ownership from ambiguous positional signatures', async () => {
const collectedUsage = [];
const collectedThoughtSignatures = {};
const handler = new ModelEndHandler(collectedUsage, collectedThoughtSignatures);
await handler.handle(
'on_chat_model_end',
{
output: {
usage_metadata: { input_tokens: 10, output_tokens: 5, total_tokens: 15 },
tool_calls: [
{ id: 'tc_a', name: 'a', args: {} },
{ id: 'tc_b', name: 'b', args: {} },
],
additional_kwargs: { signatures: ['SIG_A', '', 'SIG_B'] },
},
},
{ ls_model_name: 'gemini-3.1-flash-lite-preview', user_id: 'u1' },
buildGraph(),
);
expect(collectedThoughtSignatures).toEqual({});
expect(collectedUsage).toHaveLength(1);
});
it('accumulates per-id across multiple model_end events (multi-step tool turn)', async () => {
const collectedUsage = [];
const collectedThoughtSignatures = {};
const handler = new ModelEndHandler(collectedUsage, collectedThoughtSignatures);
await handler.handle(
'on_chat_model_end',
{
output: {
usage_metadata: { input_tokens: 5, output_tokens: 5, total_tokens: 10 },
tool_calls: [{ id: 'tc_step1', name: 'a', args: {} }],
additional_kwargs: {
__gemini_function_call_thought_signatures__: { tc_step1: 'SIG_step1' },
},
},
},
{ ls_model_name: 'g', user_id: 'u' },
buildGraph(),
);
await handler.handle(
'on_chat_model_end',
{
output: {
usage_metadata: { input_tokens: 5, output_tokens: 5, total_tokens: 10 },
tool_calls: [{ id: 'tc_step2', name: 'b', args: {} }],
additional_kwargs: {
__gemini_function_call_thought_signatures__: { tc_step2: 'SIG_step2' },
},
},
},
{ ls_model_name: 'g', user_id: 'u' },
buildGraph(),
);
expect(collectedThoughtSignatures).toEqual({
calls: { tc_step1: 'SIG_step1', tc_step2: 'SIG_step2' },
});
});
it('captures signed text even without token usage', async () => {
const { AIMessage } = require('@langchain/core/messages');
const { captureGeminiReplayParts } = require('@librechat/agents');
const output = new AIMessage('Signed answer');
captureGeminiReplayParts(output, [{ text: 'Signed answer', thoughtSignature: 'opaque' }]);
const collected = {};
await new ModelEndHandler([], collected).handle(
'on_chat_model_end',
{ output },
{},
buildGraph(),
);
expect(collected.turns[0][0].thoughtSignature).toBe('opaque');
});
it('is a no-op for signatures when collectedThoughtSignatures is null', async () => {
const collectedUsage = [];
const handler = new ModelEndHandler(collectedUsage, null);
await handler.handle(
'on_chat_model_end',
{
output: {
usage_metadata: { input_tokens: 5, output_tokens: 5, total_tokens: 10 },
tool_calls: [{ id: 'tc1', name: 'a', args: {} }],
additional_kwargs: { signatures: ['SIG'] },
},
},
{ ls_model_name: 'g', user_id: 'u' },
buildGraph(),
);
expect(collectedUsage).toHaveLength(1);
});
it('does not store anything when signatures field is missing (non-Vertex providers)', async () => {
const collectedUsage = [];
const collectedThoughtSignatures = {};
const handler = new ModelEndHandler(collectedUsage, collectedThoughtSignatures);
await handler.handle(
'on_chat_model_end',
{
output: {
usage_metadata: { input_tokens: 5, output_tokens: 5, total_tokens: 10 },
tool_calls: [{ id: 'tc1', name: 'a', args: {} }],
additional_kwargs: {},
},
},
{ ls_model_name: 'gpt-4', user_id: 'u' },
buildGraph(),
);
expect(collectedThoughtSignatures).toEqual({});
});
it('does not store anything when tool_calls is missing', async () => {
const collectedUsage = [];
const collectedThoughtSignatures = {};
const handler = new ModelEndHandler(collectedUsage, collectedThoughtSignatures);
await handler.handle(
'on_chat_model_end',
{
output: {
usage_metadata: { input_tokens: 5, output_tokens: 5, total_tokens: 10 },
additional_kwargs: { signatures: ['SIG_orphan'] },
},
},
{ ls_model_name: 'g', user_id: 'u' },
buildGraph(),
);
expect(collectedThoughtSignatures).toEqual({});
});
it('tags the producing agent on collected + emitted usage for per-endpoint pricing', async () => {
const collectedUsage = [];
const emitUsage = jest.fn();
const handler = new ModelEndHandler(collectedUsage, null, emitUsage);
const graph = {
getAgentContext: () => ({
provider: 'openai',
agentId: 'agent_sub',
clientOptions: { model: 'gpt-4' },
}),
};
await handler.handle(
'on_chat_model_end',
{ output: { usage_metadata: { input_tokens: 10, output_tokens: 5, total_tokens: 15 } } },
{ ls_model_name: 'gpt-4', run_id: 'r1', user_id: 'u1' },
graph,
);
expect(collectedUsage[0].agentId).toBe('agent_sub');
expect(collectedUsage[0].provider).toBe('openai');
expect(collectedUsage[0].model).toBe('gpt-4');
expect(emitUsage).toHaveBeenCalledWith(expect.objectContaining({ agentId: 'agent_sub' }));
});
it('leaves usage untagged when the graph context has no agentId (single-endpoint)', async () => {
const collectedUsage = [];
const emitUsage = jest.fn();
const handler = new ModelEndHandler(collectedUsage, null, emitUsage);
await handler.handle(
'on_chat_model_end',
{ output: { usage_metadata: { input_tokens: 10, output_tokens: 5, total_tokens: 15 } } },
{ ls_model_name: 'gemini-3.1-flash-lite-preview', run_id: 'r1', user_id: 'u1' },
buildGraph(),
);
expect(collectedUsage[0].agentId).toBeUndefined();
expect(emitUsage).toHaveBeenCalledWith(expect.objectContaining({ agentId: undefined }));
});
it('carries the Anthropic 1-hour cache write count into collected and emitted usage', async () => {
const collectedUsage = [];
const emitUsage = jest.fn();
const handler = new ModelEndHandler(collectedUsage, null, emitUsage);
await handler.handle(
'on_chat_model_end',
{
output: {
usage_metadata: {
input_tokens: 3510,
output_tokens: 40,
total_tokens: 3550,
input_token_details: { cache_creation: 3000, cache_read: 500 },
},
response_metadata: {
usage: {
cache_creation: { ephemeral_5m_input_tokens: 1000, ephemeral_1h_input_tokens: 2000 },
},
},
},
},
{ ls_model_name: 'claude-sonnet-5-5', run_id: 'r1', user_id: 'u1' },
buildGraph(),
);
expect(collectedUsage[0].input_token_details).toEqual({
cache_creation: 3000,
cache_creation_1h: 2000,
cache_read: 500,
});
expect(emitUsage).toHaveBeenCalledWith(
expect.objectContaining({
input_token_details: { cache_creation: 3000, cache_creation_1h: 2000, cache_read: 500 },
}),
);
});
it('throws when collectedUsage is not an array (existing contract)', () => {
expect(() => new ModelEndHandler(null)).toThrow('collectedUsage must be an array');
});
});