mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-09-22 06:00:56 +02: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
231 lines
7.1 KiB
TypeScript
231 lines
7.1 KiB
TypeScript
import React, { useRef, useState, useMemo } from 'react';
|
|
import * as Ariakit from '@ariakit/react';
|
|
import { useRecoilState } from 'recoil';
|
|
import { FileSearch, ImageUpIcon, TerminalSquareIcon, FileType2Icon } from 'lucide-react';
|
|
import { EToolResources, EModelEndpoint, defaultAgentCapabilities } from 'librechat-data-provider';
|
|
import {
|
|
FileUpload,
|
|
TooltipAnchor,
|
|
DropdownPopup,
|
|
AttachmentIcon,
|
|
SharePointIcon,
|
|
} from '@librechat/client';
|
|
import type { EndpointFileConfig } from 'librechat-data-provider';
|
|
import {
|
|
useAgentToolPermissions,
|
|
useAgentCapabilities,
|
|
useGetAgentsConfig,
|
|
useFileHandling,
|
|
useLocalize,
|
|
} from '~/hooks';
|
|
import useSharePointFileHandling from '~/hooks/Files/useSharePointFileHandling';
|
|
import { SharePointPickerDialog } from '~/components/SharePoint';
|
|
import { useGetStartupConfig } from '~/data-provider';
|
|
import { ephemeralAgentByConvoId } from '~/store';
|
|
import { MenuItemProps } from '~/common';
|
|
import { cn } from '~/utils';
|
|
|
|
interface AttachFileMenuProps {
|
|
conversationId: string;
|
|
agentId?: string | null;
|
|
disabled?: boolean | null;
|
|
endpointFileConfig?: EndpointFileConfig;
|
|
}
|
|
|
|
const AttachFileMenu = ({
|
|
agentId,
|
|
disabled,
|
|
conversationId,
|
|
endpointFileConfig,
|
|
}: AttachFileMenuProps) => {
|
|
const localize = useLocalize();
|
|
const isUploadDisabled = disabled ?? false;
|
|
const inputRef = useRef<HTMLInputElement>(null);
|
|
const [isPopoverActive, setIsPopoverActive] = useState(false);
|
|
const [ephemeralAgent, setEphemeralAgent] = useRecoilState(
|
|
ephemeralAgentByConvoId(conversationId),
|
|
);
|
|
const [toolResource, setToolResource] = useState<EToolResources | undefined>();
|
|
const { handleFileChange } = useFileHandling({
|
|
overrideEndpoint: EModelEndpoint.agents,
|
|
overrideEndpointFileConfig: endpointFileConfig,
|
|
});
|
|
const { handleSharePointFiles, isProcessing, downloadProgress } = useSharePointFileHandling({
|
|
overrideEndpoint: EModelEndpoint.agents,
|
|
overrideEndpointFileConfig: endpointFileConfig,
|
|
toolResource,
|
|
});
|
|
const { data: startupConfig } = useGetStartupConfig();
|
|
const sharePointEnabled = startupConfig?.sharePointFilePickerEnabled;
|
|
|
|
const [isSharePointDialogOpen, setIsSharePointDialogOpen] = useState(false);
|
|
const { agentsConfig } = useGetAgentsConfig();
|
|
/** TODO: Ephemeral Agent Capabilities
|
|
* Allow defining agent capabilities on a per-endpoint basis
|
|
* Use definition for agents endpoint for ephemeral agents
|
|
* */
|
|
const capabilities = useAgentCapabilities(agentsConfig?.capabilities ?? defaultAgentCapabilities);
|
|
|
|
const { fileSearchAllowedByAgent, codeAllowedByAgent } = useAgentToolPermissions(
|
|
agentId,
|
|
ephemeralAgent,
|
|
);
|
|
|
|
const handleUploadClick = (isImage?: boolean) => {
|
|
if (!inputRef.current) {
|
|
return;
|
|
}
|
|
inputRef.current.value = '';
|
|
inputRef.current.accept = isImage === true ? 'image/*' : '';
|
|
inputRef.current.click();
|
|
inputRef.current.accept = '';
|
|
};
|
|
|
|
const dropdownItems = useMemo(() => {
|
|
const createMenuItems = (onAction: (isImage?: boolean) => void) => {
|
|
const items: MenuItemProps[] = [
|
|
{
|
|
label: localize('com_ui_upload_image_input'),
|
|
onClick: () => {
|
|
setToolResource(undefined);
|
|
onAction(true);
|
|
},
|
|
icon: <ImageUpIcon className="icon-md" />,
|
|
},
|
|
];
|
|
|
|
if (capabilities.contextEnabled) {
|
|
items.push({
|
|
label: localize('com_ui_upload_ocr_text'),
|
|
onClick: () => {
|
|
setToolResource(EToolResources.context);
|
|
onAction();
|
|
},
|
|
icon: <FileType2Icon className="icon-md" />,
|
|
});
|
|
}
|
|
|
|
if (capabilities.fileSearchEnabled && fileSearchAllowedByAgent) {
|
|
items.push({
|
|
label: localize('com_ui_upload_file_search'),
|
|
onClick: () => {
|
|
setToolResource(EToolResources.file_search);
|
|
setEphemeralAgent((prev) => ({
|
|
...prev,
|
|
[EToolResources.file_search]: true,
|
|
}));
|
|
onAction();
|
|
},
|
|
icon: <FileSearch className="icon-md" />,
|
|
});
|
|
}
|
|
|
|
if (capabilities.codeEnabled && codeAllowedByAgent) {
|
|
items.push({
|
|
label: localize('com_ui_upload_code_files'),
|
|
onClick: () => {
|
|
setToolResource(EToolResources.execute_code);
|
|
setEphemeralAgent((prev) => ({
|
|
...prev,
|
|
[EToolResources.execute_code]: true,
|
|
}));
|
|
onAction();
|
|
},
|
|
icon: <TerminalSquareIcon className="icon-md" />,
|
|
});
|
|
}
|
|
|
|
return items;
|
|
};
|
|
|
|
const localItems = createMenuItems(handleUploadClick);
|
|
|
|
if (sharePointEnabled) {
|
|
const sharePointItems = createMenuItems(() => {
|
|
setIsSharePointDialogOpen(true);
|
|
// Note: toolResource will be set by the specific item clicked
|
|
});
|
|
localItems.push({
|
|
label: localize('com_files_upload_sharepoint'),
|
|
onClick: () => {},
|
|
icon: <SharePointIcon className="icon-md" />,
|
|
subItems: sharePointItems,
|
|
});
|
|
return localItems;
|
|
}
|
|
|
|
return localItems;
|
|
}, [
|
|
capabilities,
|
|
localize,
|
|
setToolResource,
|
|
setEphemeralAgent,
|
|
sharePointEnabled,
|
|
codeAllowedByAgent,
|
|
fileSearchAllowedByAgent,
|
|
setIsSharePointDialogOpen,
|
|
]);
|
|
|
|
const menuTrigger = (
|
|
<TooltipAnchor
|
|
render={
|
|
<Ariakit.MenuButton
|
|
disabled={isUploadDisabled}
|
|
id="attach-file-menu-button"
|
|
aria-label="Attach File Options"
|
|
className={cn(
|
|
'flex size-9 items-center justify-center rounded-full p-1 transition-colors hover:bg-surface-hover focus:outline-none focus:ring-2 focus:ring-primary focus:ring-opacity-50',
|
|
)}
|
|
>
|
|
<div className="flex w-full items-center justify-center gap-2">
|
|
<AttachmentIcon />
|
|
</div>
|
|
</Ariakit.MenuButton>
|
|
}
|
|
id="attach-file-menu-button"
|
|
description={localize('com_sidepanel_attach_files')}
|
|
disabled={isUploadDisabled}
|
|
/>
|
|
);
|
|
const handleSharePointFilesSelected = async (sharePointFiles: any[]) => {
|
|
try {
|
|
await handleSharePointFiles(sharePointFiles);
|
|
setIsSharePointDialogOpen(false);
|
|
} catch (error) {
|
|
console.error('SharePoint file processing error:', error);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<>
|
|
<FileUpload
|
|
ref={inputRef}
|
|
handleFileChange={(e) => {
|
|
handleFileChange(e, toolResource);
|
|
}}
|
|
>
|
|
<DropdownPopup
|
|
menuId="attach-file-menu"
|
|
className="overflow-visible"
|
|
isOpen={isPopoverActive}
|
|
setIsOpen={setIsPopoverActive}
|
|
modal={true}
|
|
unmountOnHide={true}
|
|
trigger={menuTrigger}
|
|
items={dropdownItems}
|
|
iconClassName="mr-0"
|
|
/>
|
|
</FileUpload>
|
|
<SharePointPickerDialog
|
|
isOpen={isSharePointDialogOpen}
|
|
onOpenChange={setIsSharePointDialogOpen}
|
|
onFilesSelected={handleSharePointFilesSelected}
|
|
isDownloading={isProcessing}
|
|
downloadProgress={downloadProgress}
|
|
maxSelectionCount={endpointFileConfig?.fileLimit}
|
|
/>
|
|
</>
|
|
);
|
|
};
|
|
|
|
export default React.memo(AttachFileMenu);
|