mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-16 21:08:17 +00:00
* 🎒 feat: Carry Project Context Across Conversations
* fix: fail a project attach whose upload hold cannot be released and keep file deletes independent of project cleanup
- Attaching a project file now fails, with nothing attached, when the file's
temporary upload hold cannot be released, instead of attaching a file that
can still expire.
- A failed project cleanup after a file delete is logged rather than failing a
delete that already happened, and a user-wide delete clears that user's
project references without reading every file first.
- Both resize handles share the orientation-aware grip, so a primary handle in
a vertical group renders a rotated grip like the alternate one.
* fix: treat an empty tenant id as no tenant in the project file picker
* fix: attach nothing when a project file is deleted while the attach is in flight
* fix: localize project file association failures instead of showing server text
* test: expect a localized reason for a rejected project file association
* fix: scope project file listing to the caller's tenant and name the file limit when attaching an existing file
- Listing a project's attached files now returns not found when the project
belongs to another tenant, matching the available-file listing and the
chat context resolver.
- Choosing an existing file for a full project now names the project file
limit instead of the generic attach error, like the upload path.
* fix: treat an empty tenant id as no tenant in every project tenant check
A caller whose tenant id is an empty string was refused its own tenantless
project when listing attached or available files, because the handlers
compared '' with null. The project handlers and the chat context resolver now
share one comparison that treats an empty tenant id as no tenant, the rule the
project file queries already follow.
* test: wait for the workspace layout swap and the mobile drawer before acting in the long-metadata spec
The desktop title was measured while the phone layout could still be the one
rendered, and the sidebar row menu was opened before the mobile drawer
committed its open state, which closes a menu opened ahead of it. The spec
now waits for the workspace's phone-only sidebar toggle to leave and for the
drawer to drop inert, the same drawer signal the agents helpers use.
* fix: keep the agent context fingerprint of a turn outside any project unchanged
Every turn now passes a project context key into the agent context
fingerprint, and a turn outside any project passed the no-project sentinel,
which changed the digest of every existing no-project checkpoint. An agent
event conversation written before this change then cold-started and reloaded
its history on its next event. The fingerprint now hashes the no-project key
as absent, so those checkpoints keep matching.
* test: load the long-metadata spec at phone width before driving the mobile drawer
The spec crossed back from desktop to phone width without a reload, which
leaves the drawer on the desktop toggle path whose delayed focus handoff to
the close button dismissed the row menu roughly 200ms after it opened. The
spec now reloads at phone width, as a phone arrives, and waits for the
drawer's own focus handoff before opening the menu.
(cherry picked from commit 4062bc3c00)
Original-PR: https://github.com/LibreChat-AI/LibreChat/pull/15684
241 lines
7.7 KiB
JavaScript
241 lines
7.7 KiB
JavaScript
jest.mock('@librechat/data-schemas', () => ({
|
|
logger: { error: jest.fn(), debug: jest.fn(), info: jest.fn() },
|
|
}));
|
|
jest.mock('@librechat/api', () => {
|
|
const { getModelRefusalInfo } = jest.requireActual('@librechat/api');
|
|
return {
|
|
sendEvent: jest.fn(),
|
|
emitEvent: jest.fn(),
|
|
createToolExecuteHandler: jest.fn(),
|
|
markSummarizationUsage: (usage) => usage,
|
|
getModelRefusalInfo,
|
|
};
|
|
});
|
|
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 — Vertex 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);
|
|
expect(contextualizeModelUsage(usage, undefined, null)).toEqual(usage);
|
|
});
|
|
|
|
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',
|
|
});
|
|
});
|
|
|
|
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('maps non-empty signatures onto tool_call_ids in order', 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({ tc_a: 'SIG_A', tc_b: 'SIG_B' });
|
|
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: { signatures: ['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: { signatures: ['SIG_step2'] },
|
|
},
|
|
},
|
|
{ ls_model_name: 'g', user_id: 'u' },
|
|
buildGraph(),
|
|
);
|
|
|
|
expect(collectedThoughtSignatures).toEqual({
|
|
tc_step1: 'SIG_step1',
|
|
tc_step2: 'SIG_step2',
|
|
});
|
|
});
|
|
|
|
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('throws when collectedUsage is not an array (existing contract)', () => {
|
|
expect(() => new ModelEndHandler(null)).toThrow('collectedUsage must be an array');
|
|
});
|
|
});
|