diff --git a/packages/visualizer/src/component/prompt-input/index.tsx b/packages/visualizer/src/component/prompt-input/index.tsx index 51bfe5b7f9..61c2ec0e84 100644 --- a/packages/visualizer/src/component/prompt-input/index.tsx +++ b/packages/visualizer/src/component/prompt-input/index.tsx @@ -97,13 +97,22 @@ export const PromptInput: React.FC = ({ [history, selectedType], ); + const actionMap = useMemo(() => { + const map = new Map>(); + if (actionSpace) { + for (const action of actionSpace) { + if (action.name) map.set(action.name, action); + if (action.interfaceAlias) map.set(action.interfaceAlias, action); + } + } + return map; + }, [actionSpace]); + // Check if current method needs structured parameters (dynamic based on actionSpace) const needsStructuredParams = useMemo(() => { if (actionSpace) { // Use actionSpace to determine if method needs structured params - const action = actionSpace.find( - (a) => a.interfaceAlias === selectedType || a.name === selectedType, - ); + const action = actionMap.get(selectedType); if (!action?.paramSchema) return false; @@ -119,15 +128,13 @@ export const PromptInput: React.FC = ({ return true; } return false; - }, [selectedType, actionSpace]); + }, [selectedType, actionMap, actionSpace]); // Check if current method needs any input (either prompt or parameters) const needsAnyInput = useMemo(() => { if (actionSpace && actionSpace.length > 0) { // Use actionSpace to determine if method needs any input - const action = actionSpace.find( - (a) => a.interfaceAlias === selectedType || a.name === selectedType, - ); + const action = actionMap.get(selectedType); // If action exists in actionSpace, check if it has required parameters if (action) { @@ -179,9 +186,7 @@ export const PromptInput: React.FC = ({ if (actionSpace) { // Use actionSpace to determine if method supports deep locate - const action = actionSpace.find( - (a) => a.interfaceAlias === selectedType || a.name === selectedType, - ); + const action = actionMap.get(selectedType); if (action?.paramSchema && isZodObjectSchema(action.paramSchema)) { const schema = action.paramSchema as ZodObjectSchema; @@ -279,9 +284,7 @@ export const PromptInput: React.FC = ({ if (!needsStructuredParams || !actionSpace) { return {}; } - const action = actionSpace.find( - (a) => a.interfaceAlias === selectedType || a.name === selectedType, - ); + const action = actionMap.get(selectedType); if (action?.paramSchema && isZodObjectSchema(action.paramSchema)) { const defaultParams: FormParams = {}; @@ -444,9 +447,7 @@ export const PromptInput: React.FC = ({ if (!needsStructuredParams || !actionSpace) { return false; } - const action = actionSpace.find( - (a) => a.interfaceAlias === selectedType || a.name === selectedType, - ); + const action = actionMap.get(selectedType); if (action?.paramSchema && isZodObjectSchema(action.paramSchema)) { const schema = action.paramSchema as ZodObjectSchema; @@ -454,7 +455,7 @@ export const PromptInput: React.FC = ({ return Object.keys(shape).length === 1; } return false; - }, [selectedType, needsStructuredParams, actionSpace]); + }, [selectedType, needsStructuredParams, actionMap, actionSpace]); // Calculate if run button should be enabled const isRunButtonEnabled = useMemo( @@ -463,7 +464,7 @@ export const PromptInput: React.FC = ({ runButtonEnabled, !!needsStructuredParams, params, - actionSpace, + actionMap, selectedType, promptValue, ), @@ -471,7 +472,7 @@ export const PromptInput: React.FC = ({ runButtonEnabled, needsStructuredParams, selectedType, - actionSpace, + actionMap, promptValue, params, ], @@ -488,9 +489,7 @@ export const PromptInput: React.FC = ({ // For structured params, create a display string for history - dynamically let historyPrompt = ''; if (needsStructuredParams && values.params && actionSpace) { - const action = actionSpace.find( - (a) => a.interfaceAlias === selectedType || a.name === selectedType, - ); + const action = actionMap.get(selectedType); if (action?.paramSchema && isZodObjectSchema(action.paramSchema)) { // Separate locate field from other fields for legacy format compatibility @@ -617,9 +616,7 @@ export const PromptInput: React.FC = ({ // Try to get action from actionSpace first if (actionSpace) { - const action = actionSpace.find( - (a) => a.interfaceAlias === selectedType || a.name === selectedType, - ); + const action = actionMap.get(selectedType); if (action?.paramSchema && isZodObjectSchema(action.paramSchema)) { const schema = action.paramSchema as ZodObjectSchema; @@ -651,10 +648,7 @@ export const PromptInput: React.FC = ({ // Try to get from action's paramSchema directly if (actionSpace) { - const action = actionSpace.find( - (a) => - a.interfaceAlias === selectedType || a.name === selectedType, - ); + const action = actionMap.get(selectedType); if ( action?.paramSchema && typeof action.paramSchema === 'object' && @@ -736,10 +730,7 @@ export const PromptInput: React.FC = ({ // Try to get from action's paramSchema directly if (actionSpace) { - const action = actionSpace.find( - (a) => - a.interfaceAlias === selectedType || a.name === selectedType, - ); + const action = actionMap.get(selectedType); if ( action?.paramSchema && typeof action.paramSchema === 'object' && diff --git a/packages/visualizer/src/utils/playground-utils.ts b/packages/visualizer/src/utils/playground-utils.ts index 9599002f40..1d6b782856 100644 --- a/packages/visualizer/src/utils/playground-utils.ts +++ b/packages/visualizer/src/utils/playground-utils.ts @@ -89,7 +89,7 @@ export const isRunButtonEnabled = ( runButtonEnabled: boolean, needsStructuredParams: boolean, params: any, - actionSpace: any[] | undefined, + actionMap: Map | undefined, selectedType: string, promptValue: string, ) => { @@ -97,15 +97,12 @@ export const isRunButtonEnabled = ( return false; } + const action = actionMap?.get(selectedType); + // Check if this method needs any input const needsAnyInput = (() => { - if (actionSpace) { - // Use actionSpace to determine if method needs any input - const action = actionSpace.find( - (a) => a.interfaceAlias === selectedType || a.name === selectedType, - ); - - // If action exists in actionSpace, check if it has paramSchema with actual fields + if (actionMap) { + // If action exists in actionMap, check if it has paramSchema with actual fields if (action) { if (!action.paramSchema) return false; @@ -125,7 +122,7 @@ export const isRunButtonEnabled = ( return true; } - // If not found in actionSpace, assume most methods need input + // If not found in actionMap, assume most methods need input return true; } @@ -140,9 +137,7 @@ export const isRunButtonEnabled = ( if (needsStructuredParams) { const currentParams = params || {}; - const action = actionSpace?.find( - (a) => a.interfaceAlias === selectedType || a.name === selectedType, - ); + if (action?.paramSchema && isZodObjectSchema(action.paramSchema)) { // Check if all required fields are filled const schema = action.paramSchema as unknown as ZodObjectSchema;