mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-01-12 21:48:51 +01:00
* WIP: code ptc
* refactor: tool classification and calling logic
* 🔧 fix: Update @librechat/agents dependency to version 3.0.68
* chore: import order and correct renamed tool name for tool search
* refactor: streamline tool classification logic for local and programmatic tools
* feat: add per-tool configuration options for agents, including deferred loading and allowed callers
- Introduced `tool_options` in agent forms to manage tool behavior.
- Updated tool classification logic to prioritize agent-level configurations.
- Enhanced UI components to support tool deferral functionality.
- Added localization strings for new tool options and actions.
* feat: enhance agent schema with per-tool options for configuration
- Added `tool_options` schema to support per-tool configurations, including `defer_loading` and `allowed_callers`.
- Updated agent data model to incorporate new tool options, ensuring flexibility in tool behavior management.
- Modified type definitions to reflect the new `tool_options` structure for agents.
* feat: add tool_options parameter to loadTools and initializeAgent for enhanced agent configuration
* chore: update @librechat/agents dependency to version 3.0.71 and enhance agent tool loading logic
- Updated the @librechat/agents package to version 3.0.71 across multiple files.
- Added support for handling deferred loading of tools in agent initialization and execution processes.
- Improved the extraction of discovered tools from message history to optimize tool loading behavior.
* chore: update @librechat/agents dependency to version 3.0.72
* chore: update @librechat/agents dependency to version 3.0.75
* refactor: simplify tool defer loading logic in MCPTool component
- Removed local state management for deferred tools, relying on form state instead.
- Updated related functions to directly use form values for checking and toggling defer loading.
- Cleaned up code by eliminating unnecessary optimistic updates and local state dependencies.
* chore: remove deprecated localization strings for tool deferral in translation.json
- Eliminated unused strings related to deferred loading descriptions in the English translation file.
- Streamlined localization to reflect recent changes in tool loading logic.
* refactor: improve tool defer loading handling in MCPTool component
- Enhanced the logic for managing deferred loading of tools by simplifying the update process for tool options.
- Ensured that the state reflects the correct loading behavior based on the new deferred loading conditions.
- Cleaned up the code to remove unnecessary complexity in handling tool options.
* refactor: update agent mocks in callbacks test to use actual implementations
- Modified the agent mocks in the callbacks test to include actual implementations from the @librechat/agents module.
- This change enhances the accuracy of the tests by ensuring they reflect the real behavior of the agent functions.
225 lines
6.8 KiB
TypeScript
225 lines
6.8 KiB
TypeScript
import React, { useMemo, useState, useRef, useEffect } from 'react';
|
|
import { useRecoilValue } from 'recoil';
|
|
import type { TAttachment } from 'librechat-data-provider';
|
|
import ProgressText from '~/components/Chat/Messages/Content/ProgressText';
|
|
import MarkdownLite from '~/components/Chat/Messages/Content/MarkdownLite';
|
|
import { useProgress, useLocalize } from '~/hooks';
|
|
import { AttachmentGroup } from './Attachment';
|
|
import Stdout from './Stdout';
|
|
import { cn } from '~/utils';
|
|
import store from '~/store';
|
|
|
|
interface ParsedArgs {
|
|
lang?: string;
|
|
code?: string;
|
|
}
|
|
|
|
export function useParseArgs(args?: string): ParsedArgs | null {
|
|
return useMemo(() => {
|
|
let parsedArgs: ParsedArgs | string | undefined | null = args;
|
|
try {
|
|
parsedArgs = JSON.parse(args || '');
|
|
} catch {
|
|
// console.error('Failed to parse args:', e);
|
|
}
|
|
if (typeof parsedArgs === 'object') {
|
|
return parsedArgs;
|
|
}
|
|
const langMatch = args?.match(/"lang"\s*:\s*"(\w+)"/);
|
|
const codeMatch = args?.match(/"code"\s*:\s*"(.+?)(?="\s*,\s*"(session_id|args)"|"\s*})/s);
|
|
|
|
let code = '';
|
|
if (codeMatch) {
|
|
code = codeMatch[1];
|
|
if (code.endsWith('"}')) {
|
|
code = code.slice(0, -2);
|
|
}
|
|
code = code.replace(/\\n/g, '\n').replace(/\\"/g, '"').replace(/\\\\/g, '\\');
|
|
}
|
|
|
|
return {
|
|
lang: langMatch ? langMatch[1] : '',
|
|
code,
|
|
};
|
|
}, [args]);
|
|
}
|
|
|
|
export default function ExecuteCode({
|
|
isSubmitting,
|
|
initialProgress = 0.1,
|
|
args,
|
|
output = '',
|
|
attachments,
|
|
}: {
|
|
initialProgress: number;
|
|
isSubmitting: boolean;
|
|
args?: string;
|
|
output?: string;
|
|
attachments?: TAttachment[];
|
|
}) {
|
|
const localize = useLocalize();
|
|
const hasOutput = output.length > 0;
|
|
const outputRef = useRef<string>(output);
|
|
const codeContentRef = useRef<HTMLDivElement>(null);
|
|
const [isAnimating, setIsAnimating] = useState(false);
|
|
const showAnalysisCode = useRecoilValue(store.showCode);
|
|
const [showCode, setShowCode] = useState(showAnalysisCode);
|
|
const [contentHeight, setContentHeight] = useState<number | undefined>(0);
|
|
|
|
const prevShowCodeRef = useRef<boolean>(showCode);
|
|
const { lang = 'py', code } = useParseArgs(args) ?? ({} as ParsedArgs);
|
|
const progress = useProgress(initialProgress);
|
|
|
|
useEffect(() => {
|
|
if (output !== outputRef.current) {
|
|
outputRef.current = output;
|
|
|
|
if (showCode && codeContentRef.current) {
|
|
setTimeout(() => {
|
|
if (codeContentRef.current) {
|
|
const newHeight = codeContentRef.current.scrollHeight;
|
|
setContentHeight(newHeight);
|
|
}
|
|
}, 10);
|
|
}
|
|
}
|
|
}, [output, showCode]);
|
|
|
|
useEffect(() => {
|
|
if (showCode !== prevShowCodeRef.current) {
|
|
prevShowCodeRef.current = showCode;
|
|
|
|
if (showCode && codeContentRef.current) {
|
|
setIsAnimating(true);
|
|
requestAnimationFrame(() => {
|
|
if (codeContentRef.current) {
|
|
const height = codeContentRef.current.scrollHeight;
|
|
setContentHeight(height);
|
|
}
|
|
|
|
const timer = setTimeout(() => {
|
|
setIsAnimating(false);
|
|
}, 500);
|
|
|
|
return () => clearTimeout(timer);
|
|
});
|
|
} else if (!showCode) {
|
|
setIsAnimating(true);
|
|
setContentHeight(0);
|
|
|
|
const timer = setTimeout(() => {
|
|
setIsAnimating(false);
|
|
}, 500);
|
|
|
|
return () => clearTimeout(timer);
|
|
}
|
|
}
|
|
}, [showCode]);
|
|
|
|
useEffect(() => {
|
|
if (!codeContentRef.current) {
|
|
return;
|
|
}
|
|
|
|
const resizeObserver = new ResizeObserver((entries) => {
|
|
if (showCode && !isAnimating) {
|
|
for (const entry of entries) {
|
|
if (entry.target === codeContentRef.current) {
|
|
setContentHeight(entry.contentRect.height);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
resizeObserver.observe(codeContentRef.current);
|
|
|
|
return () => {
|
|
resizeObserver.disconnect();
|
|
};
|
|
}, [showCode, isAnimating]);
|
|
|
|
const cancelled = !isSubmitting && progress < 1;
|
|
|
|
return (
|
|
<>
|
|
<div className="relative my-2.5 flex size-5 shrink-0 items-center gap-2.5">
|
|
<ProgressText
|
|
progress={progress}
|
|
onClick={() => setShowCode((prev) => !prev)}
|
|
inProgressText={localize('com_ui_analyzing')}
|
|
finishedText={
|
|
cancelled ? localize('com_ui_cancelled') : localize('com_ui_analyzing_finished')
|
|
}
|
|
hasInput={!!code?.length}
|
|
isExpanded={showCode}
|
|
error={cancelled}
|
|
/>
|
|
</div>
|
|
<div
|
|
className="relative mb-2"
|
|
style={{
|
|
height: showCode ? contentHeight : 0,
|
|
overflow: 'hidden',
|
|
transition:
|
|
'height 0.4s cubic-bezier(0.16, 1, 0.3, 1), opacity 0.4s cubic-bezier(0.16, 1, 0.3, 1)',
|
|
opacity: showCode ? 1 : 0,
|
|
transformOrigin: 'top',
|
|
willChange: 'height, opacity',
|
|
perspective: '1000px',
|
|
backfaceVisibility: 'hidden',
|
|
WebkitFontSmoothing: 'subpixel-antialiased',
|
|
}}
|
|
>
|
|
<div
|
|
className={cn(
|
|
'code-analyze-block mt-0.5 overflow-hidden rounded-xl bg-surface-primary',
|
|
showCode && 'shadow-lg',
|
|
)}
|
|
ref={codeContentRef}
|
|
style={{
|
|
transform: showCode ? 'translateY(0) scale(1)' : 'translateY(-8px) scale(0.98)',
|
|
opacity: showCode ? 1 : 0,
|
|
transition:
|
|
'transform 0.4s cubic-bezier(0.16, 1, 0.3, 1), opacity 0.4s cubic-bezier(0.16, 1, 0.3, 1)',
|
|
}}
|
|
>
|
|
{showCode && (
|
|
<div
|
|
style={{
|
|
transform: showCode ? 'translateY(0)' : 'translateY(-4px)',
|
|
opacity: showCode ? 1 : 0,
|
|
transition:
|
|
'transform 0.35s cubic-bezier(0.16, 1, 0.3, 1), opacity 0.35s cubic-bezier(0.16, 1, 0.3, 1)',
|
|
}}
|
|
>
|
|
<MarkdownLite
|
|
content={code ? `\`\`\`${lang}\n${code}\n\`\`\`` : ''}
|
|
codeExecution={false}
|
|
/>
|
|
</div>
|
|
)}
|
|
{hasOutput && (
|
|
<div
|
|
className={cn(
|
|
'bg-surface-tertiary p-4 text-xs',
|
|
showCode ? 'border-t border-surface-primary-contrast' : '',
|
|
)}
|
|
style={{
|
|
transform: showCode ? 'translateY(0)' : 'translateY(-6px)',
|
|
opacity: showCode ? 1 : 0,
|
|
transition:
|
|
'transform 0.45s cubic-bezier(0.16, 1, 0.3, 1) 0.05s, opacity 0.45s cubic-bezier(0.19, 1, 0.22, 1) 0.05s',
|
|
boxShadow: showCode ? '0 -1px 0 rgba(0,0,0,0.05)' : 'none',
|
|
}}
|
|
>
|
|
<div className="prose flex flex-col-reverse">
|
|
<Stdout output={output} />
|
|
</div>
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
{attachments && attachments.length > 0 && <AttachmentGroup attachments={attachments} />}
|
|
</>
|
|
);
|
|
}
|