mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-20 15:02:02 +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
270 lines
8.6 KiB
TypeScript
270 lines
8.6 KiB
TypeScript
import type { TEndpointOption } from 'librechat-data-provider';
|
|
import type { AppConfig } from '@librechat/data-schemas';
|
|
import type {
|
|
AgentProjectContextRequest,
|
|
ConversationSnapshot,
|
|
ResolvedChatProjectContext,
|
|
ResolveChatProjectContextDeps,
|
|
} from './context';
|
|
import type { ContentPolicyError } from '../middleware/contentFilter';
|
|
import {
|
|
getChatProjectTenantId,
|
|
resolveChatProjectContext,
|
|
formatChatProjectInstructions,
|
|
CHAT_PROJECT_CONTEXT_UNAVAILABLE,
|
|
} from './context';
|
|
import { getContentFilterError, isContentFilterError } from '../middleware/contentFilter';
|
|
import { assertModelBoundContent } from '../middleware/modelBoundContent';
|
|
import { getSafeErrorMetadata } from '../utils/errors';
|
|
|
|
export const PROJECT_RESOURCES_CHANGED = 'PROJECT_RESOURCES_CHANGED';
|
|
export const CHAT_PROJECT_CONTEXT_UNAVAILABLE_RESPONSE: string = 'Conversation context unavailable';
|
|
|
|
/** Admitted project files moved between admission and runtime hydration; a new turn can retry. */
|
|
export class ChatProjectResourcesChangedError extends Error {
|
|
public readonly code: typeof PROJECT_RESOURCES_CHANGED = PROJECT_RESOURCES_CHANGED;
|
|
public readonly status = 409 as const;
|
|
public readonly retryable = true as const;
|
|
|
|
constructor() {
|
|
super('Project resources changed during initialization');
|
|
this.name = 'ChatProjectResourcesChangedError';
|
|
Object.setPrototypeOf(this, ChatProjectResourcesChangedError.prototype);
|
|
}
|
|
}
|
|
|
|
export interface ProjectLogger {
|
|
warn: (message: string, meta?: object) => void;
|
|
error: (message: string, meta?: object) => void;
|
|
}
|
|
|
|
export type ChatProjectTurnFailure =
|
|
| { status: 404; error: string }
|
|
| { status: 409; code: string; error: string; retryable: true }
|
|
| ({ status: number } & ContentPolicyError['body']);
|
|
|
|
/** Maps project-owned turn startup failures to the HTTP status and body sent before the ACK. */
|
|
export function getChatProjectTurnFailure(error: unknown): ChatProjectTurnFailure | null {
|
|
if (error instanceof ChatProjectResourcesChangedError) {
|
|
return {
|
|
status: 409,
|
|
code: PROJECT_RESOURCES_CHANGED,
|
|
error: 'Project files changed while this turn was starting. Please retry.',
|
|
retryable: true,
|
|
};
|
|
}
|
|
if (error instanceof Error && error.message === CHAT_PROJECT_CONTEXT_UNAVAILABLE) {
|
|
return { status: 404, error: CHAT_PROJECT_CONTEXT_UNAVAILABLE_RESPONSE };
|
|
}
|
|
if (!isContentFilterError(error)) {
|
|
return null;
|
|
}
|
|
const policyError = getContentFilterError(error) ?? error;
|
|
return { status: policyError.statusCode, ...policyError.body };
|
|
}
|
|
|
|
/**
|
|
* Inspects project guidance as agent instructions. Legacy `messageFilter.pii` is scoped to
|
|
* message, assembled-context and tool-argument sources, so it never applies to instructions.
|
|
*/
|
|
export function assertChatProjectInstructions({
|
|
context,
|
|
filters,
|
|
}: {
|
|
context: ResolvedChatProjectContext | null | undefined;
|
|
filters?: AppConfig['filters'];
|
|
}): void {
|
|
if (!context?.instructions.trim()) {
|
|
return;
|
|
}
|
|
assertModelBoundContent({ filters, agents: [{ instructions: context.instructions }] });
|
|
}
|
|
|
|
export function joinChatProjectInstructions(
|
|
base: string | null | undefined,
|
|
instructions: string,
|
|
): string {
|
|
return [base, instructions].filter(Boolean).join('\n\n');
|
|
}
|
|
|
|
export type ApiConversationProjectResult =
|
|
| {
|
|
ok: true;
|
|
conversation: ConversationSnapshot;
|
|
context: ResolvedChatProjectContext | null;
|
|
}
|
|
| {
|
|
ok: false;
|
|
status: 404 | 409 | 500;
|
|
reason: 'not_found' | 'read_only' | 'unavailable' | 'server_error';
|
|
message: string;
|
|
};
|
|
|
|
/**
|
|
* Loads the continued conversation for the OpenAI-compatible and Responses APIs and resolves
|
|
* its authoritative project guidance, mapping every failure to the response the caller sends.
|
|
*/
|
|
export async function resolveApiConversationProject(
|
|
{
|
|
userId,
|
|
tenantId,
|
|
conversationId,
|
|
rejectSubagentThread = false,
|
|
}: {
|
|
userId: string;
|
|
tenantId?: string | null;
|
|
conversationId: string;
|
|
rejectSubagentThread?: boolean;
|
|
},
|
|
deps: ResolveChatProjectContextDeps & { logger: ProjectLogger; logPrefix: string },
|
|
): Promise<ApiConversationProjectResult> {
|
|
try {
|
|
const conversation = await deps.getConvo(userId, conversationId);
|
|
if (!conversation) {
|
|
return { ok: false, status: 404, reason: 'not_found', message: 'Conversation not found' };
|
|
}
|
|
if (rejectSubagentThread && conversation.subagentThread != null) {
|
|
return { ok: false, status: 409, reason: 'read_only', message: 'Conversation is read-only' };
|
|
}
|
|
const context = await resolveChatProjectContext(
|
|
{
|
|
userId,
|
|
tenantId,
|
|
conversationId,
|
|
resolvedConversation: conversation,
|
|
includeResources: false,
|
|
},
|
|
deps,
|
|
);
|
|
return { ok: true, conversation, context };
|
|
} catch (error) {
|
|
deps.logger.error(
|
|
`${deps.logPrefix} Conversation context resolution failed`,
|
|
getSafeErrorMetadata(error),
|
|
);
|
|
const unavailable =
|
|
error instanceof Error && error.message === CHAT_PROJECT_CONTEXT_UNAVAILABLE;
|
|
return unavailable
|
|
? {
|
|
ok: false,
|
|
status: 404,
|
|
reason: 'unavailable',
|
|
message: CHAT_PROJECT_CONTEXT_UNAVAILABLE_RESPONSE,
|
|
}
|
|
: {
|
|
ok: false,
|
|
status: 500,
|
|
reason: 'server_error',
|
|
message: CHAT_PROJECT_CONTEXT_UNAVAILABLE_RESPONSE,
|
|
};
|
|
}
|
|
}
|
|
|
|
export type AssistantProjectTurn =
|
|
| { rejection: { status: number; body: object } }
|
|
| {
|
|
rejection?: undefined;
|
|
conversation: ConversationSnapshot | null;
|
|
context: ResolvedChatProjectContext | null;
|
|
instructions: string;
|
|
/** Membership written with the final conversation: seeded only on a new conversation. */
|
|
membershipProjectId: string | null | undefined;
|
|
};
|
|
|
|
/**
|
|
* Resolves project guidance for a hosted Assistants turn and inspects it before any provider
|
|
* side effect. Content-policy and unavailable-project outcomes return the response to send.
|
|
*/
|
|
export async function resolveAssistantProjectTurn(
|
|
{
|
|
userId,
|
|
tenantId,
|
|
conversationId,
|
|
requestedProjectId,
|
|
resolvedConversation,
|
|
filters,
|
|
}: {
|
|
userId: string;
|
|
tenantId?: string | null;
|
|
conversationId?: string | null;
|
|
requestedProjectId?: string | null;
|
|
resolvedConversation?: ConversationSnapshot | null;
|
|
filters?: AppConfig['filters'];
|
|
},
|
|
deps: ResolveChatProjectContextDeps,
|
|
): Promise<AssistantProjectTurn> {
|
|
let conversation = resolvedConversation;
|
|
if (conversation === undefined) {
|
|
conversation = conversationId ? await deps.getConvo(userId, conversationId) : null;
|
|
}
|
|
const existingConversation = conversation ?? null;
|
|
let context: ResolvedChatProjectContext | null;
|
|
try {
|
|
context = await resolveChatProjectContext(
|
|
{
|
|
userId,
|
|
tenantId,
|
|
conversationId,
|
|
requestedProjectId,
|
|
resolvedConversation: existingConversation,
|
|
includeResources: false,
|
|
},
|
|
deps,
|
|
);
|
|
assertChatProjectInstructions({ context, filters });
|
|
} catch (error) {
|
|
const failure = getChatProjectTurnFailure(error);
|
|
if (failure == null || failure.status === 409) {
|
|
throw error;
|
|
}
|
|
const { status, ...body } = failure;
|
|
return { rejection: { status, body } };
|
|
}
|
|
return {
|
|
conversation: existingConversation,
|
|
context,
|
|
instructions: formatChatProjectInstructions(context),
|
|
membershipProjectId:
|
|
existingConversation === null ? context?.projectId : existingConversation.chatProjectId,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Resolves guidance for an agent initialization that did not arrive with a resolved project
|
|
* context, reusing an already-resolved context (including an explicit `undefined`) as-is.
|
|
*/
|
|
export function resolveInitializationProjectContext(
|
|
{
|
|
req,
|
|
endpointOption,
|
|
conversationId,
|
|
conversationPromise,
|
|
}: {
|
|
req: AgentProjectContextRequest;
|
|
endpointOption: Partial<TEndpointOption>;
|
|
conversationId?: string | null;
|
|
conversationPromise: Promise<ConversationSnapshot | null | undefined>;
|
|
},
|
|
deps: ResolveChatProjectContextDeps,
|
|
): Promise<ResolvedChatProjectContext | null | undefined> {
|
|
if (Object.prototype.hasOwnProperty.call(req, 'chatProjectContext')) {
|
|
return Promise.resolve(req.chatProjectContext);
|
|
}
|
|
const requestedProjectId =
|
|
endpointOption.chatProjectId !== undefined
|
|
? endpointOption.chatProjectId
|
|
: req.body?.chatProjectId;
|
|
return conversationPromise.then((resolvedConversation) =>
|
|
resolveChatProjectContext(
|
|
{
|
|
userId: req.user.id,
|
|
tenantId: getChatProjectTenantId(req),
|
|
conversationId,
|
|
requestedProjectId,
|
|
resolvedConversation,
|
|
includeResources: false,
|
|
},
|
|
deps,
|
|
),
|
|
);
|
|
}
|