import { describeWorkflowTrigger as describeTriggerFromConfig, getWorkflowTriggerConfig, } from '../../toolWidgets/workflowWidgetUtils'; import type { SuperagentWorkflow, SuperagentWorkflowStatus } from '../../types'; /** * Pure formatting/derivation helpers for the workflows ("Tasks") list. Kept out * of the panel component so the branching is unit-testable. The trigger summary * reuses the package's existing workflow-trigger describer (shared with the * create_or_update_workflow chat widget) rather than re-deriving cron/interval * humanization — one source of truth across the package. */ export function isWorkflowArchived(workflow: SuperagentWorkflow): boolean { return workflow.status === 'archived'; } export function isWorkflowActive(workflow: SuperagentWorkflow): boolean { return workflow.status === 'active'; } /** Status badge label. `inactive` reads as "Paused" (parity with the web row menu). */ export function formatWorkflowStatus(workflow: SuperagentWorkflow): string { const status = workflow.status as SuperagentWorkflowStatus; if (status === 'archived') return 'Archived'; if (status === 'active') return 'Active'; return 'Paused'; } /** True when a workflow was auto-deactivated after repeated failures (UI hint). */ export function isDeactivatedByFailures(workflow: SuperagentWorkflow): boolean { return workflow.status === 'inactive' && workflow.status_reason === 'consecutive_failures'; } /** * One-line trigger summary for a workflow row, or null when the trigger is * absent/unknown. Delegates to the shared describer so scheduled workflows get * humanized cron ("Daily at 9:00 AM") instead of a raw expression. */ export function describeWorkflowTrigger(workflow: SuperagentWorkflow): string | null { const resolved = getWorkflowTriggerConfig({ trigger: workflow.trigger ?? undefined }); if (!resolved) return null; return describeTriggerFromConfig(resolved.type, resolved.config); } /** Newest-first: created date, falling back to last run. */ export function sortWorkflows(workflows: SuperagentWorkflow[]): SuperagentWorkflow[] { return [...workflows].sort((a, b) => getWorkflowTime(b) - getWorkflowTime(a)); } export function getWorkflowTime(workflow: SuperagentWorkflow): number { const value = workflow.created_date ?? workflow.last_run_at; if (!value) return 0; const timestamp = Date.parse(value); return Number.isNaN(timestamp) ? 0 : timestamp; } export function formatWorkflowRuns(workflow: SuperagentWorkflow): string { const total = workflow.total_runs ?? 0; return total === 1 ? '1 run' : `${total} runs`; }