🔧 fix: Handle optional arguments in useParseArgs and improve tool call condition

This commit is contained in:
Danny Avila 2025-07-09 18:44:42 -04:00
parent 4918899c8d
commit edfc264b65
No known key found for this signature in database
GPG key ID: BF31EEB2C5CA0956
2 changed files with 11 additions and 11 deletions

View file

@ -85,7 +85,7 @@ const Part = memo(
const isToolCall = const isToolCall =
'args' in toolCall && (!toolCall.type || toolCall.type === ToolCallTypes.TOOL_CALL); 'args' in toolCall && (!toolCall.type || toolCall.type === ToolCallTypes.TOOL_CALL);
if (isToolCall && toolCall.name === Tools.execute_code) { if (isToolCall && toolCall.name === Tools.execute_code && toolCall.args) {
return ( return (
<ExecuteCode <ExecuteCode
args={typeof toolCall.args === 'string' ? toolCall.args : ''} args={typeof toolCall.args === 'string' ? toolCall.args : ''}

View file

@ -10,23 +10,23 @@ import { cn } from '~/utils';
import store from '~/store'; import store from '~/store';
interface ParsedArgs { interface ParsedArgs {
lang: string; lang?: string;
code: string; code?: string;
} }
export function useParseArgs(args: string): ParsedArgs { export function useParseArgs(args?: string): ParsedArgs | null {
return useMemo(() => { return useMemo(() => {
let parsedArgs: ParsedArgs | string = args; let parsedArgs: ParsedArgs | string | undefined | null = args;
try { try {
parsedArgs = JSON.parse(args); parsedArgs = JSON.parse(args || '');
} catch { } catch {
// console.error('Failed to parse args:', e); // console.error('Failed to parse args:', e);
} }
if (typeof parsedArgs === 'object') { if (typeof parsedArgs === 'object') {
return parsedArgs; return parsedArgs;
} }
const langMatch = args.match(/"lang"\s*:\s*"(\w+)"/); const langMatch = args?.match(/"lang"\s*:\s*"(\w+)"/);
const codeMatch = args.match(/"code"\s*:\s*"(.+?)(?="\s*,\s*"(session_id|args)"|"\s*})/s); const codeMatch = args?.match(/"code"\s*:\s*"(.+?)(?="\s*,\s*"(session_id|args)"|"\s*})/s);
let code = ''; let code = '';
if (codeMatch) { if (codeMatch) {
@ -51,7 +51,7 @@ export default function ExecuteCode({
attachments, attachments,
}: { }: {
initialProgress: number; initialProgress: number;
args: string; args?: string;
output?: string; output?: string;
attachments?: TAttachment[]; attachments?: TAttachment[];
}) { }) {
@ -65,7 +65,7 @@ export default function ExecuteCode({
const outputRef = useRef<string>(output); const outputRef = useRef<string>(output);
const prevShowCodeRef = useRef<boolean>(showCode); const prevShowCodeRef = useRef<boolean>(showCode);
const { lang, code } = useParseArgs(args); const { lang, code } = useParseArgs(args) ?? ({} as ParsedArgs);
const progress = useProgress(initialProgress); const progress = useProgress(initialProgress);
useEffect(() => { useEffect(() => {
@ -144,7 +144,7 @@ export default function ExecuteCode({
onClick={() => setShowCode((prev) => !prev)} onClick={() => setShowCode((prev) => !prev)}
inProgressText={localize('com_ui_analyzing')} inProgressText={localize('com_ui_analyzing')}
finishedText={localize('com_ui_analyzing_finished')} finishedText={localize('com_ui_analyzing_finished')}
hasInput={!!code.length} hasInput={!!code?.length}
isExpanded={showCode} isExpanded={showCode}
/> />
</div> </div>