mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-01-22 02:06:12 +01:00
* WIP: conversion of `ocr` to `context` * refactor: make `primeResources` backwards-compatible for `ocr` tool_resources * refactor: Convert legacy `ocr` tool resource to `context` in agent updates - Implemented conversion logic to replace `ocr` with `context` in both incoming updates and existing agent data. - Merged file IDs and files from `ocr` into `context` while ensuring deduplication. - Updated tools array to reflect the change from `ocr` to `context`. * refactor: Enhance context file handling in agent processing - Updated the logic for managing context files by consolidating file IDs from both `ocr` and `context` resources. - Improved backwards compatibility by ensuring that context files are correctly populated and handled. - Simplified the iteration over context files for better readability and maintainability. * refactor: Enhance tool_resources handling in primeResources - Added tests to verify the deletion behavior of tool_resources fields, ensuring original objects remain unchanged. - Implemented logic to delete `ocr` and `context` fields after fetching and re-categorizing files. - Preserved context field when the context capability is disabled, ensuring correct behavior in various scenarios. * refactor: Replace `ocrEnabled` with `contextEnabled` in AgentConfig * refactor: Adjust legacy tool handling order for improved clarity * refactor: Implement OCR to context conversion functions and remove original conversion logic in update agent handling * refactor: Move contextEnabled declaration to maintain consistent order in capabilities * refactor: Update localization keys for file context to improve clarity and accuracy * chore: Update localization key for file context information to improve clarity
141 lines
5.3 KiB
TypeScript
141 lines
5.3 KiB
TypeScript
import { EToolResources } from 'librechat-data-provider';
|
|
import type { AgentToolResources, TFile } from 'librechat-data-provider';
|
|
|
|
/**
|
|
* Converts OCR tool resource to context tool resource in place.
|
|
* This modifies the input object directly (used for updateData in the handler).
|
|
*
|
|
* @param data - Object containing tool_resources and/or tools to convert
|
|
* @returns void - modifies the input object directly
|
|
*/
|
|
export function convertOcrToContextInPlace(data: {
|
|
tool_resources?: AgentToolResources;
|
|
tools?: string[];
|
|
}): void {
|
|
// Convert OCR to context in tool_resources
|
|
if (data.tool_resources?.ocr) {
|
|
if (!data.tool_resources.context) {
|
|
data.tool_resources.context = data.tool_resources.ocr;
|
|
} else {
|
|
// Merge OCR into existing context
|
|
if (data.tool_resources.ocr?.file_ids?.length) {
|
|
const existingFileIds = data.tool_resources.context.file_ids || [];
|
|
const ocrFileIds = data.tool_resources.ocr.file_ids || [];
|
|
data.tool_resources.context.file_ids = [...new Set([...existingFileIds, ...ocrFileIds])];
|
|
}
|
|
if (data.tool_resources.ocr?.files?.length) {
|
|
const existingFiles = data.tool_resources.context.files || [];
|
|
const ocrFiles = data.tool_resources.ocr.files || [];
|
|
const filesMap = new Map<string, TFile>();
|
|
[...existingFiles, ...ocrFiles].forEach((file) => {
|
|
if (file?.file_id) {
|
|
filesMap.set(file.file_id, file);
|
|
}
|
|
});
|
|
data.tool_resources.context.files = Array.from(filesMap.values());
|
|
}
|
|
}
|
|
delete data.tool_resources.ocr;
|
|
}
|
|
|
|
// Convert OCR to context in tools array
|
|
if (data.tools?.includes(EToolResources.ocr)) {
|
|
data.tools = data.tools.map((tool) =>
|
|
tool === EToolResources.ocr ? EToolResources.context : tool,
|
|
);
|
|
data.tools = [...new Set(data.tools)];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Merges tool resources from existing agent with incoming update data,
|
|
* converting OCR to context and handling deduplication.
|
|
* Used when existing agent has OCR that needs to be converted and merged with updateData.
|
|
*
|
|
* @param existingAgent - The existing agent data
|
|
* @param updateData - The incoming update data
|
|
* @returns Object with merged tool_resources and tools
|
|
*/
|
|
export function mergeAgentOcrConversion(
|
|
existingAgent: { tool_resources?: AgentToolResources; tools?: string[] },
|
|
updateData: { tool_resources?: AgentToolResources; tools?: string[] },
|
|
): { tool_resources?: AgentToolResources; tools?: string[] } {
|
|
if (!existingAgent.tool_resources?.ocr) {
|
|
return {};
|
|
}
|
|
|
|
const result: { tool_resources?: AgentToolResources; tools?: string[] } = {};
|
|
|
|
// Convert existing agent's OCR to context
|
|
result.tool_resources = { ...existingAgent.tool_resources };
|
|
|
|
if (!result.tool_resources.context) {
|
|
// Simple case: no context exists, just move ocr to context
|
|
result.tool_resources.context = result.tool_resources.ocr;
|
|
} else {
|
|
// Merge case: context already exists, merge both file_ids and files arrays
|
|
|
|
// Merge file_ids if they exist
|
|
if (result.tool_resources.ocr?.file_ids?.length) {
|
|
const existingFileIds = result.tool_resources.context.file_ids || [];
|
|
const ocrFileIds = result.tool_resources.ocr.file_ids || [];
|
|
result.tool_resources.context.file_ids = [...new Set([...existingFileIds, ...ocrFileIds])];
|
|
}
|
|
|
|
// Merge files array if it exists (already fetched files)
|
|
if (result.tool_resources.ocr?.files?.length) {
|
|
const existingFiles = result.tool_resources.context.files || [];
|
|
const ocrFiles = result.tool_resources.ocr?.files || [];
|
|
// Merge and deduplicate by file_id
|
|
const filesMap = new Map<string, TFile>();
|
|
[...existingFiles, ...ocrFiles].forEach((file) => {
|
|
if (file?.file_id) {
|
|
filesMap.set(file.file_id, file);
|
|
}
|
|
});
|
|
result.tool_resources.context.files = Array.from(filesMap.values());
|
|
}
|
|
}
|
|
|
|
// Remove the deprecated ocr resource
|
|
delete result.tool_resources.ocr;
|
|
|
|
// Update tools array: replace 'ocr' with 'context'
|
|
if (existingAgent.tools?.includes(EToolResources.ocr)) {
|
|
result.tools = existingAgent.tools.map((tool) =>
|
|
tool === EToolResources.ocr ? EToolResources.context : tool,
|
|
);
|
|
// Remove duplicates if context already existed
|
|
result.tools = [...new Set(result.tools)];
|
|
}
|
|
|
|
// Merge with any context that might already be in updateData (from incoming OCR conversion)
|
|
if (updateData.tool_resources?.context && result.tool_resources.context) {
|
|
// Merge the contexts
|
|
const mergedContext = { ...result.tool_resources.context };
|
|
|
|
// Merge file_ids
|
|
if (updateData.tool_resources.context.file_ids?.length) {
|
|
const existingIds = mergedContext.file_ids || [];
|
|
const newIds = updateData.tool_resources.context.file_ids || [];
|
|
mergedContext.file_ids = [...new Set([...existingIds, ...newIds])];
|
|
}
|
|
|
|
// Merge files
|
|
if (updateData.tool_resources.context.files?.length) {
|
|
const existingFiles = mergedContext.files || [];
|
|
const newFiles = updateData.tool_resources.context.files || [];
|
|
const filesMap = new Map<string, TFile>();
|
|
[...existingFiles, ...newFiles].forEach((file) => {
|
|
if (file?.file_id) {
|
|
filesMap.set(file.file_id, file);
|
|
}
|
|
});
|
|
mergedContext.files = Array.from(filesMap.values());
|
|
}
|
|
|
|
result.tool_resources.context = mergedContext;
|
|
}
|
|
|
|
return result;
|
|
}
|