Skip to content

Commit

Permalink
fix: dynamic prompt change
Browse files Browse the repository at this point in the history
  • Loading branch information
varshith15 committed Feb 13, 2025
1 parent 383105b commit c3009c6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
6 changes: 3 additions & 3 deletions server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions server/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 8 additions & 7 deletions ui/src/components/control-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<Record<string, NodeInfo>>({});

// Add ref to track last sent value and timeout
Expand Down Expand Up @@ -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");
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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 });
Expand Down

0 comments on commit c3009c6

Please sign in to comment.