* feat: init @librechat/client * feat: Add common types and interfaces for accessibility, agents, artifacts, assistants, and tools * feat: Add jotai as a peer dependency * fix build client package * feat: cleanup unused types from common/index.ts - Remove 104 unused type exports from packages/client/src/common/index.ts - Keep only 7 actually used exports (93% reduction) - Add cleanup script with enhanced import pattern detection - Support both named imports and namespace imports (* as t) - Create automatic backups and comprehensive documentation - Maintain type safety with build verification - No breaking changes to existing code Kept exports: - TShowToast, Option, OptionWithIcon, DropdownValueSetter - MentionOption, NotificationSeverity, MenuItemProps Scripts: cleanup-common-types-safe.js, README-CLEANUP.md * fix: cleanup * fix: package; refactor: tsconfig * feat: add back `recoil` * fix: move dependencies to peerDependencies in client package * feat: add @librechat/client as a dependency in package.json and package-lock.json * feat: update client package configuration and dependencies - Added new dependencies for Rollup plugins and updated existing ones in package.json and package-lock.json. - Introduced a new Rollup configuration file for building the client package. - Refactored build scripts to include a dedicated build command for the client. - Updated TypeScript configuration for improved module resolution and type declaration output. - Integrated a Toast component from the client package into the main App component. * feat: enhance Rollup configuration for client package - Updated terser plugin settings to preserve directives like 'use client'. - Added custom warning handler to ignore "use client" directive warnings during the build process. * chore: rename package/client build script command * feat: update client package dependencies and Rollup configuration - Added rollup-plugin-postcss to package.json and updated package-lock.json. - Enhanced Rollup configuration to include postcss plugin for CSS handling. - Updated index.ts to export all components from the components directory for better modularity. * feat: add client package directory to update configuration - Included the 'client' package directory in the update.js configuration to ensure it is recognized during updates. * feat: export Toast component in client package - Added export for the Toast component in index.ts to enhance modularity and accessibility of components. * feat: /client transition to @librechat/client * chore: fixed formatting issues * fix: update peer dependencies in @librechat/client to prevent bundling them * fix: correct useSprings implementation in SplitText component * fix: circular dependencies in DataTable * fix: add remaining peer dependencies and match actual versions previously used in `client/package.json` * fix: correct frontend:ci script to include client package build * chore: enhance unused package detection for @librechat/client and improve dependency extraction * fix: add missing peer dependency for @radix-ui/react-collapsible * chore: include "packages/client" in unused i18next keys detection * test: update AgentFooter tests to use document.querySelector for spinner checks test: mock window.matchMedia in setupTests.js for consistent test environment * feat: add react-hook-form dependency and update FormInput component to use its types * chore: linting * refactor: remove unused defaultSelectedValues prop from MCPSelect and MultiSelect components * chore: linting * feat: update GitHub Actions workflow to publish @librechat/client * chore: update GitHub Actions workflow to install and build data-provider and client dependencies * chore: add missing @testing-library/react dependency to client package * chore: update tsconfig.json to exclude additional test files * chore: fix build issues, resolve latest LC changes * chore: move MCP components outside of `~/components/ui` * feat: implement dynamic theme system with environment variable support and Tailwind CSS integration * chore: remove unnecessary logging of sttExternal and ttsExternal in Speech component * chore: squashed cleanup commits chore: move @tanstack/react-virtual to dependencies and remove recoil from package.json chore: move dependencies to peerDependencies in package.json feat: update package.json and rollup.config.js to include jotai and enhance bundling configuration feat: update package.json and rollup.config.js to include jotai and enhance bundling configuration refactor: reorganize exports in index.ts for improved clarity refactor: remove unused types and interfaces from common files refactor: update peer dependencies and improve component typings - Removed duplicate peer dependencies from package.json and organized them. - Updated rollup.config.js to disable TypeScript checking during the build process. - Modified AnimatedTabs component to use React.ReactNode for label and content types, and added TypeScript workarounds for compatibility. - Enhanced Label and Separator components to accept an optional className prop and improved prop spreading. - Updated Slider component to include an optional className prop and refined prop handling for better type safety. refactor: clean up client workflow and update package dependencies refactor: update package dependencies and improve PostCSS and Rollup configurations chore: bump version to 0.1.2 in package.json chore: bump client version to 0.1.2 in package-lock.json chore: bump client version to 0.1.3 and update dependencies chore: bump client version to 0.1.4 and update @react-spring dependencies chore: update package version to 0.1.5 and adjust peer dependencies - Bump version in package.json from 0.1.4 to 0.1.5. - Update peer dependency for @tanstack/react-query to allow version 5.0.0. - Add @tanstack/react-table and @tanstack/react-virtual as dependencies. - Update various dependencies to their latest compatible versions. - Simplify postcss.config.js by removing unnecessary options. - Clean up rollup.config.js by removing ignored PostCSS warnings. - Update CheckboxButton component to cast icon as React JSX element. - Adjust Combobox component's class names for better styling. - Change DropdownPopup component to use React's namespace import. - Modify InputOTP component to use 'any' type for OTPInputContext. - Ensure displayLabel and value in ModelParameters are converted to strings. - Update MultiSearch component's placeholder to ensure it's a string. - Cast selectIcon in MultiSelect as React JSX element for consistency. - Update OGDialogTemplate to cast selectText as React JSX element. - Initialize animationRef in PixelCard with undefined for clarity. - Add TypeScript ignore comments in Select and SelectDropDown components for Radix UI type conflicts. - Ensure title in SelectDropDown is a string and adjust rendering of options. - Update useLocalize hook to cast options as any for compatibility. refactor: code structure; chore: translations cleanup chore: remove unused imports and clean up code in NewChat component refactor: enhance Menu component to support custom render functions for menu items style: update itemClassName in ToolsDropdown for improved UI consistency fix: merge conflicts chore: update @radix-ui/react-accordion to version 1.2.11 * refactor: remove unnecessary TypeScript type assertions in AnimatedTabs, Label, Separator, and Slider components * feat: enhance theme system with localStorage persistence and new theme atoms * chore: bump version of @librechat/client to 0.1.7 * chore: fix ci/cd warnings/errors related to linting and unused localization keys * chore: update dependencies for class-variance-authority, clsx, and match-sorter * chore: bump @librechat/client to v0.1.8 * feat: add utility colors for theme customization and remove unused tailwindConfig * v0.1.9 --------- Co-authored-by: Marco Beretta <81851188+berry-13@users.noreply.github.com>
405 lines
12 KiB
TypeScript
405 lines
12 KiB
TypeScript
import React, {
|
|
memo,
|
|
useRef,
|
|
useMemo,
|
|
useState,
|
|
useEffect,
|
|
forwardRef,
|
|
useReducer,
|
|
useCallback,
|
|
} from 'react';
|
|
import { Badge } from '@librechat/client';
|
|
import { useRecoilValue, useRecoilCallback } from 'recoil';
|
|
import type { LucideIcon } from 'lucide-react';
|
|
import CodeInterpreter from './CodeInterpreter';
|
|
import { BadgeRowProvider } from '~/Providers';
|
|
import ToolsDropdown from './ToolsDropdown';
|
|
import type { BadgeItem } from '~/common';
|
|
import { useChatBadges } from '~/hooks';
|
|
import ToolDialogs from './ToolDialogs';
|
|
import FileSearch from './FileSearch';
|
|
import Artifacts from './Artifacts';
|
|
import MCPSelect from './MCPSelect';
|
|
import WebSearch from './WebSearch';
|
|
import store from '~/store';
|
|
|
|
interface BadgeRowProps {
|
|
showEphemeralBadges?: boolean;
|
|
onChange: (badges: Pick<BadgeItem, 'id'>[]) => void;
|
|
onToggle?: (badgeId: string, currentActive: boolean) => void;
|
|
conversationId?: string | null;
|
|
isSubmitting?: boolean;
|
|
isInChat: boolean;
|
|
}
|
|
|
|
interface BadgeWrapperProps {
|
|
badge: BadgeItem;
|
|
isEditing: boolean;
|
|
isInChat: boolean;
|
|
onToggle: (badge: BadgeItem) => void;
|
|
onDelete: (id: string) => void;
|
|
onMouseDown: (e: React.MouseEvent, badge: BadgeItem, isActive: boolean) => void;
|
|
badgeRefs: React.MutableRefObject<Record<string, HTMLDivElement>>;
|
|
}
|
|
|
|
const BadgeWrapper = React.memo(
|
|
forwardRef<HTMLDivElement, BadgeWrapperProps>(
|
|
({ badge, isEditing, isInChat, onToggle, onDelete, onMouseDown, badgeRefs }, ref) => {
|
|
const atomBadge = useRecoilValue(badge.atom);
|
|
const isActive = badge.atom ? atomBadge : false;
|
|
|
|
return (
|
|
<div
|
|
ref={(el) => {
|
|
if (el) {
|
|
badgeRefs.current[badge.id] = el;
|
|
}
|
|
if (typeof ref === 'function') {
|
|
ref(el);
|
|
} else if (ref) {
|
|
ref.current = el;
|
|
}
|
|
}}
|
|
onMouseDown={(e) => onMouseDown(e, badge, isActive)}
|
|
className={isEditing ? 'ios-wiggle badge-icon h-full' : 'badge-icon h-full'}
|
|
>
|
|
<Badge
|
|
id={badge.id}
|
|
icon={badge.icon as LucideIcon}
|
|
label={badge.label}
|
|
isActive={isActive}
|
|
isEditing={isEditing}
|
|
isAvailable={badge.isAvailable}
|
|
isInChat={isInChat}
|
|
onToggle={() => onToggle(badge)}
|
|
onBadgeAction={() => onDelete(badge.id)}
|
|
/>
|
|
</div>
|
|
);
|
|
},
|
|
),
|
|
(prevProps, nextProps) =>
|
|
prevProps.badge.id === nextProps.badge.id &&
|
|
prevProps.isEditing === nextProps.isEditing &&
|
|
prevProps.isInChat === nextProps.isInChat &&
|
|
prevProps.onToggle === nextProps.onToggle &&
|
|
prevProps.onDelete === nextProps.onDelete &&
|
|
prevProps.onMouseDown === nextProps.onMouseDown &&
|
|
prevProps.badgeRefs === nextProps.badgeRefs,
|
|
);
|
|
|
|
BadgeWrapper.displayName = 'BadgeWrapper';
|
|
|
|
interface DragState {
|
|
draggedBadge: BadgeItem | null;
|
|
mouseX: number;
|
|
offsetX: number;
|
|
insertIndex: number | null;
|
|
draggedBadgeActive: boolean;
|
|
}
|
|
|
|
type DragAction =
|
|
| {
|
|
type: 'START_DRAG';
|
|
badge: BadgeItem;
|
|
mouseX: number;
|
|
offsetX: number;
|
|
insertIndex: number;
|
|
isActive: boolean;
|
|
}
|
|
| { type: 'UPDATE_POSITION'; mouseX: number; insertIndex: number }
|
|
| { type: 'END_DRAG' };
|
|
|
|
const dragReducer = (state: DragState, action: DragAction): DragState => {
|
|
switch (action.type) {
|
|
case 'START_DRAG':
|
|
return {
|
|
draggedBadge: action.badge,
|
|
mouseX: action.mouseX,
|
|
offsetX: action.offsetX,
|
|
insertIndex: action.insertIndex,
|
|
draggedBadgeActive: action.isActive,
|
|
};
|
|
case 'UPDATE_POSITION':
|
|
return {
|
|
...state,
|
|
mouseX: action.mouseX,
|
|
insertIndex: action.insertIndex,
|
|
};
|
|
case 'END_DRAG':
|
|
return {
|
|
draggedBadge: null,
|
|
mouseX: 0,
|
|
offsetX: 0,
|
|
insertIndex: null,
|
|
draggedBadgeActive: false,
|
|
};
|
|
default:
|
|
return state;
|
|
}
|
|
};
|
|
|
|
function BadgeRow({
|
|
showEphemeralBadges,
|
|
conversationId,
|
|
isSubmitting,
|
|
onChange,
|
|
onToggle,
|
|
isInChat,
|
|
}: BadgeRowProps) {
|
|
const [orderedBadges, setOrderedBadges] = useState<BadgeItem[]>([]);
|
|
const [dragState, dispatch] = useReducer(dragReducer, {
|
|
draggedBadge: null,
|
|
mouseX: 0,
|
|
offsetX: 0,
|
|
insertIndex: null,
|
|
draggedBadgeActive: false,
|
|
});
|
|
|
|
const badgeRefs = useRef<Record<string, HTMLDivElement>>({});
|
|
const containerRef = useRef<HTMLDivElement>(null);
|
|
const animationFrame = useRef<number | null>(null);
|
|
const containerRectRef = useRef<DOMRect | null>(null);
|
|
|
|
const allBadges = useChatBadges();
|
|
const isEditing = useRecoilValue(store.isEditingBadges);
|
|
|
|
const badges = useMemo(
|
|
() => allBadges.filter((badge) => badge.isAvailable !== false),
|
|
[allBadges],
|
|
);
|
|
|
|
const toggleBadge = useRecoilCallback(
|
|
({ snapshot, set }) =>
|
|
async (badgeAtom: any) => {
|
|
const current = await snapshot.getPromise(badgeAtom);
|
|
set(badgeAtom, !current);
|
|
},
|
|
[],
|
|
);
|
|
|
|
useEffect(() => {
|
|
setOrderedBadges((prev) => {
|
|
const currentIds = new Set(prev.map((b) => b.id));
|
|
const newBadges = badges.filter((b) => !currentIds.has(b.id));
|
|
return newBadges.length > 0 ? [...prev, ...newBadges] : prev;
|
|
});
|
|
}, [badges]);
|
|
|
|
const tempBadges = dragState.draggedBadge
|
|
? orderedBadges.filter((b) => b.id !== dragState.draggedBadge?.id)
|
|
: orderedBadges;
|
|
const ghostBadge = dragState.draggedBadge || null;
|
|
|
|
const calculateInsertIndex = useCallback(
|
|
(currentMouseX: number): number => {
|
|
if (!dragState.draggedBadge || !containerRef.current || !containerRectRef.current) {
|
|
return 0;
|
|
}
|
|
const relativeMouseX = currentMouseX - containerRectRef.current.left;
|
|
const refs = tempBadges.map((b) => badgeRefs.current[b.id]).filter(Boolean);
|
|
if (refs.length === 0) {
|
|
return 0;
|
|
}
|
|
let idx = 0;
|
|
for (let i = 0; i < refs.length; i++) {
|
|
const rect = refs[i].getBoundingClientRect();
|
|
const relativeLeft = rect.left - containerRectRef.current.left;
|
|
const relativeCenter = relativeLeft + rect.width / 2;
|
|
if (relativeMouseX < relativeCenter) {
|
|
break;
|
|
}
|
|
idx = i + 1;
|
|
}
|
|
return idx;
|
|
},
|
|
[dragState.draggedBadge, tempBadges],
|
|
);
|
|
|
|
const handleMouseDown = useCallback(
|
|
(e: React.MouseEvent, badge: BadgeItem, isActive: boolean) => {
|
|
if (!isEditing || !containerRef.current) {
|
|
return;
|
|
}
|
|
const el = badgeRefs.current[badge.id];
|
|
if (!el) {
|
|
return;
|
|
}
|
|
const rect = el.getBoundingClientRect();
|
|
const offsetX = e.clientX - rect.left;
|
|
const mouseX = e.clientX;
|
|
const initialIndex = orderedBadges.findIndex((b) => b.id === badge.id);
|
|
containerRectRef.current = containerRef.current.getBoundingClientRect();
|
|
dispatch({
|
|
type: 'START_DRAG',
|
|
badge,
|
|
mouseX,
|
|
offsetX,
|
|
insertIndex: initialIndex,
|
|
isActive,
|
|
});
|
|
},
|
|
[isEditing, orderedBadges],
|
|
);
|
|
|
|
const handleMouseMove = useCallback(
|
|
(e: MouseEvent) => {
|
|
if (!dragState.draggedBadge) {
|
|
return;
|
|
}
|
|
if (animationFrame.current) {
|
|
cancelAnimationFrame(animationFrame.current);
|
|
}
|
|
animationFrame.current = requestAnimationFrame(() => {
|
|
const newMouseX = e.clientX;
|
|
const newInsertIndex = calculateInsertIndex(newMouseX);
|
|
if (newInsertIndex !== dragState.insertIndex) {
|
|
dispatch({ type: 'UPDATE_POSITION', mouseX: newMouseX, insertIndex: newInsertIndex });
|
|
} else {
|
|
dispatch({
|
|
type: 'UPDATE_POSITION',
|
|
mouseX: newMouseX,
|
|
insertIndex: dragState.insertIndex,
|
|
});
|
|
}
|
|
});
|
|
},
|
|
[dragState.draggedBadge, dragState.insertIndex, calculateInsertIndex],
|
|
);
|
|
|
|
const handleMouseUp = useCallback(() => {
|
|
if (dragState.draggedBadge && dragState.insertIndex !== null) {
|
|
const otherBadges = orderedBadges.filter((b) => b.id !== dragState.draggedBadge?.id);
|
|
const newBadges = [
|
|
...otherBadges.slice(0, dragState.insertIndex),
|
|
dragState.draggedBadge,
|
|
...otherBadges.slice(dragState.insertIndex),
|
|
];
|
|
setOrderedBadges(newBadges);
|
|
onChange(newBadges.map((badge) => ({ id: badge.id })));
|
|
}
|
|
dispatch({ type: 'END_DRAG' });
|
|
containerRectRef.current = null;
|
|
}, [dragState.draggedBadge, dragState.insertIndex, orderedBadges, onChange]);
|
|
|
|
const handleDelete = useCallback(
|
|
(badgeId: string) => {
|
|
const newBadges = orderedBadges.filter((b) => b.id !== badgeId);
|
|
setOrderedBadges(newBadges);
|
|
onChange(newBadges.map((badge) => ({ id: badge.id })));
|
|
},
|
|
[orderedBadges, onChange],
|
|
);
|
|
|
|
const handleBadgeToggle = useCallback(
|
|
(badge: BadgeItem) => {
|
|
if (badge.atom) {
|
|
toggleBadge(badge.atom);
|
|
}
|
|
if (onToggle) {
|
|
onToggle(badge.id, !!badge.atom);
|
|
}
|
|
},
|
|
[toggleBadge, onToggle],
|
|
);
|
|
|
|
useEffect(() => {
|
|
if (!dragState.draggedBadge) {
|
|
return;
|
|
}
|
|
document.addEventListener('mousemove', handleMouseMove);
|
|
document.addEventListener('mouseup', handleMouseUp);
|
|
return () => {
|
|
document.removeEventListener('mousemove', handleMouseMove);
|
|
document.removeEventListener('mouseup', handleMouseUp);
|
|
if (animationFrame.current) {
|
|
cancelAnimationFrame(animationFrame.current);
|
|
animationFrame.current = null;
|
|
}
|
|
};
|
|
}, [dragState.draggedBadge, handleMouseMove, handleMouseUp]);
|
|
|
|
return (
|
|
<BadgeRowProvider conversationId={conversationId} isSubmitting={isSubmitting}>
|
|
<div ref={containerRef} className="relative flex flex-wrap items-center gap-2">
|
|
{showEphemeralBadges === true && <ToolsDropdown />}
|
|
{tempBadges.map((badge, index) => (
|
|
<React.Fragment key={badge.id}>
|
|
{dragState.draggedBadge && dragState.insertIndex === index && ghostBadge && (
|
|
<div className="badge-icon h-full">
|
|
<Badge
|
|
id={ghostBadge.id}
|
|
icon={ghostBadge.icon as LucideIcon}
|
|
label={ghostBadge.label}
|
|
isActive={dragState.draggedBadgeActive}
|
|
isEditing={isEditing}
|
|
isAvailable={ghostBadge.isAvailable}
|
|
isInChat={isInChat}
|
|
/>
|
|
</div>
|
|
)}
|
|
<BadgeWrapper
|
|
badge={badge}
|
|
isEditing={isEditing}
|
|
isInChat={isInChat}
|
|
onToggle={handleBadgeToggle}
|
|
onDelete={handleDelete}
|
|
onMouseDown={handleMouseDown}
|
|
badgeRefs={badgeRefs}
|
|
/>
|
|
</React.Fragment>
|
|
))}
|
|
{dragState.draggedBadge && dragState.insertIndex === tempBadges.length && ghostBadge && (
|
|
<div className="badge-icon h-full">
|
|
<Badge
|
|
id={ghostBadge.id}
|
|
icon={ghostBadge.icon as LucideIcon}
|
|
label={ghostBadge.label}
|
|
isActive={dragState.draggedBadgeActive}
|
|
isEditing={isEditing}
|
|
isAvailable={ghostBadge.isAvailable}
|
|
isInChat={isInChat}
|
|
/>
|
|
</div>
|
|
)}
|
|
{showEphemeralBadges === true && (
|
|
<>
|
|
<WebSearch />
|
|
<CodeInterpreter />
|
|
<FileSearch />
|
|
<Artifacts />
|
|
<MCPSelect />
|
|
</>
|
|
)}
|
|
{ghostBadge && (
|
|
<div
|
|
className="ghost-badge h-full"
|
|
style={{
|
|
position: 'absolute',
|
|
top: 0,
|
|
left: 0,
|
|
transform: `translateX(${dragState.mouseX - dragState.offsetX - (containerRectRef.current?.left || 0)}px)`,
|
|
zIndex: 10,
|
|
pointerEvents: 'none',
|
|
}}
|
|
>
|
|
<Badge
|
|
id={ghostBadge.id}
|
|
icon={ghostBadge.icon as LucideIcon}
|
|
label={ghostBadge.label}
|
|
isActive={dragState.draggedBadgeActive}
|
|
isAvailable={ghostBadge.isAvailable}
|
|
isInChat={isInChat}
|
|
isEditing
|
|
isDragging
|
|
/>
|
|
</div>
|
|
)}
|
|
</div>
|
|
<ToolDialogs />
|
|
</BadgeRowProvider>
|
|
);
|
|
}
|
|
|
|
export default memo(BadgeRow);
|