From c3009c69d7102e4cdd0d5817600566609daea200 Mon Sep 17 00:00:00 2001 From: Varshith B Date: Thu, 13 Feb 2025 18:49:26 +0530 Subject: [PATCH] fix: dynamic prompt change --- server/app.py | 6 +++--- server/pipeline.py | 6 ++++++ ui/src/components/control-panel.tsx | 15 ++++++++------- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/server/app.py b/server/app.py index 1cb5bc20..11f20999 100644 --- a/server/app.py +++ b/server/app.py @@ -153,11 +153,11 @@ async def on_message(message): "nodes": nodes_info } channel.send(json.dumps(response)) - elif params.get("type") == "update_prompt": - if "prompt" not in params: + elif params.get("type") == "update_prompts": + if "prompts" not in params: logger.warning("[Control] Missing prompt in update_prompt message") return - await pipeline.update_prompts(params["prompt"]) + await pipeline.update_prompts(params["prompts"]) response = { "type": "prompts_updated", "success": True diff --git a/server/pipeline.py b/server/pipeline.py index 84415f0f..3eeafab0 100644 --- a/server/pipeline.py +++ b/server/pipeline.py @@ -38,6 +38,12 @@ async def set_prompts(self, prompts: Union[Dict[Any, Any], List[Dict[Any, Any]]] else: await self.client.set_prompts([prompts]) + async def update_prompts(self, prompts: Union[Dict[Any, Any], List[Dict[Any, Any]]]): + if isinstance(prompts, list): + await self.client.update_prompts(prompts) + else: + await self.client.update_prompts([prompts]) + async def put_video_frame(self, frame: av.VideoFrame): inp_tensor = self.video_preprocess(frame) self.client.put_video_input(inp_tensor) diff --git a/ui/src/components/control-panel.tsx b/ui/src/components/control-panel.tsx index 2afff7da..ebda6894 100644 --- a/ui/src/components/control-panel.tsx +++ b/ui/src/components/control-panel.tsx @@ -109,7 +109,7 @@ const InputControl = ({ export const ControlPanel = ({ panelState, onStateChange }: ControlPanelProps) => { const { controlChannel } = usePeerContext(); - const { currentPrompt, setCurrentPrompt } = usePrompt(); + const { currentPrompts, setCurrentPrompts } = usePrompt(); const [availableNodes, setAvailableNodes] = useState>({}); // Add ref to track last sent value and timeout @@ -139,7 +139,7 @@ export const ControlPanel = ({ panelState, onStateChange }: ControlPanelProps) = const data = JSON.parse(event.data); if (data.type === "nodes_info") { setAvailableNodes(data.nodes); - } else if (data.type === "prompt_updated") { + } else if (data.type === "prompts_updated") { if (!data.success) { console.error("[ControlPanel] Failed to update prompt"); } @@ -171,7 +171,7 @@ export const ControlPanel = ({ panelState, onStateChange }: ControlPanelProps) = // Modify the effect that sends updates with debouncing useEffect(() => { const currentInput = panelState.nodeId && panelState.fieldName ? availableNodes[panelState.nodeId]?.inputs[panelState.fieldName] : null; - if (!currentInput || !currentPrompt) return; + if (!currentInput || !currentPrompts) return; let isValidValue = true; let processedValue: any = panelState.value; @@ -218,6 +218,7 @@ export const ControlPanel = ({ panelState, onStateChange }: ControlPanelProps) = // Set a new timeout for the update updateTimeoutRef.current = setTimeout(() => { // Create updated prompt while maintaining current structure + const currentPrompt = currentPrompts[0]; const updatedPrompt = JSON.parse(JSON.stringify(currentPrompt)); // Deep clone if (updatedPrompt[panelState.nodeId] && updatedPrompt[panelState.nodeId].inputs) { updatedPrompt[panelState.nodeId].inputs[panelState.fieldName] = processedValue; @@ -231,17 +232,17 @@ export const ControlPanel = ({ panelState, onStateChange }: ControlPanelProps) = // Send the full prompt update const message = JSON.stringify({ - type: "update_prompt", - prompt: updatedPrompt + type: "update_prompts", + prompts: [updatedPrompt] }); controlChannel.send(message); // Only update current prompt after sending - setCurrentPrompt(updatedPrompt); + setCurrentPrompts([updatedPrompt]); } }, currentInput.type.toLowerCase() === 'number' ? 100 : 300); // Shorter delay for numbers, longer for text } - }, [panelState.value, panelState.nodeId, panelState.fieldName, panelState.isAutoUpdateEnabled, controlChannel, availableNodes, currentPrompt, setCurrentPrompt]); + }, [panelState.value, panelState.nodeId, panelState.fieldName, panelState.isAutoUpdateEnabled, controlChannel, availableNodes, currentPrompts, setCurrentPrompts]); const toggleAutoUpdate = () => { onStateChange({ isAutoUpdateEnabled: !panelState.isAutoUpdateEnabled });