Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

2.8 KB
Raw Blame
import { useWorkflowRunStore } from './workflowRunStore'
import type { WaitState } from './workflowRunStore'

export interface WaitButtonModel {
  waitState:   WaitState | undefined
  canContinue: boolean
  isRunning:   boolean
  label:       'Retry' | 'Continue'
  buttonClass: string
  statusText:  string
  onContinue:  () => void
}

// Shared derivation for the Wait Continue/Retry control, used by both the
// canvas node (WaitNode) and the params panel (WaitParamRow). Keeps the two
// renderings in sync, they differ only in markup/sizing, not in logic.
export function useWaitButton(nodeId: string): WaitButtonModel {
  const waitState       = useWorkflowRunStore((s) => s.waitStates[nodeId])
  const runningBranchId = useWorkflowRunStore((s) => s.runningBranchId)
  const status          = useWorkflowRunStore((s) => s.runState.status)
  const continueRun     = useWorkflowRunStore((s) => s.continueRun)

  const otherBranchRunning = runningBranchId !== null && runningBranchId !== nodeId
  // Pre-phase: shared nodes (e.g. Generate Mesh) still running before any branch hands off.
  const inPrePhase  = status === 'running' && runningBranchId === null
  const isRunning   = waitState === 'running'
  // A pre-phase failure aborts the run before any handoff (status → 'error' while
  // this Wait is still 'pending'): it never became runnable, so no Continue.
  const runAborted  = status === 'error' && waitState === 'pending'
  const canContinue = (waitState === 'pending' || waitState === 'done' || waitState === 'error') && !otherBranchRunning && !inPrePhase && !runAborted
  const label: 'Retry' | 'Continue' = waitState === 'done' || waitState === 'error' ? 'Retry' : 'Continue'

  const buttonClass = waitState === 'error'
    ? 'bg-red-500/15 border-red-500/30 text-red-400 hover:bg-red-500/25'
    : waitState === 'done'
    ? 'bg-emerald-500/15 border-emerald-500/30 text-emerald-400 hover:bg-emerald-500/25'
    : 'bg-amber-500/15 border-amber-500/30 text-amber-400 hover:bg-amber-500/25'

  const statusText =
    waitState === 'blocked' ? 'Waiting for the previous Wait to finish…' :
    waitState === 'running' ? 'Branch in progress…' :
    waitState === 'done'    ? 'Branch finished, Retry to re-run.' :
    waitState === 'error'   ? 'Branch failed, Retry to re-run.' :
    waitState === 'pending' && runAborted ? 'Run failed upstream, fix the error and run again.' :
    waitState === 'pending' && inPrePhase ? 'Waiting for upstream nodes…' :
    waitState === 'pending' && otherBranchRunning ? 'Another branch is running…' :
    waitState === 'pending' ? 'Workflow paused, click Continue to run this branch.' :
    'Pauses the workflow until you click Continue.'

  return { waitState, canContinue, isRunning, label, buttonClass, statusText, onContinue: () => continueRun(nodeId) }
}