Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

25.2 KB
Raw Blame
import { useState, useEffect, useCallback } from 'react';
import { useNavigate, useSearchParams } from 'react-router';
import { RefreshCw, Play, PauseCircle, Sparkles, AlertTriangle, Search } from 'lucide-react';
import toast from '../../ui/Toast';
import BrailleSpinner from '../../BrailleSpinner';
import ListLoadError from '../../ui/ListLoadError';
import useListResource from '../../../hooks/useListResource.js';
import CronInput from '../../CronInput';
import ToggleSwitch from '../../ToggleSwitch';
import AppProviderPin from '../../cos/AppProviderPin';
import * as api from '../../../services/api';
import { AGENT_OPTIONS, hasProviderPin, providerPinDivergesFromSchedule, toggleAppMetadataOverride, agentOptionButtonClass } from '../../cos/constants';
import { isCronExpression, describeCron } from '../../../utils/cronHelpers';
import { PROVIDER_TYPES, providerDisplayName } from '../../../utils/providers';
import AppTaskCard from '../../cos/tabs/schedule/AppTaskCard';
import CustomTasksSection from './CustomTasksSection';

const RUNNABLE_PROVIDER_TYPES = Object.values(PROVIDER_TYPES);

const INTERVAL_OPTIONS = [
  { value: null, label: 'Inherit Global' },
  { value: 'on-demand', label: 'On Demand' },
  { value: 'cron', label: 'Scheduled' }
];

// Order the catalogue with this app's switched-on tasks first, then alphabetical.
const orderTaskTypes = (taskTypes, overrides) => [...taskTypes].sort((a, b) => {
  const onA = overrides[a]?.enabled === true;
  const onB = overrides[b]?.enabled === true;
  return onA === onB ? a.localeCompare(b) : onA ? -1 : 1;
});

export default function AutomationTab({ appId, appName }) {
  const navigate = useNavigate();
  const [searchParams, setSearchParams] = useSearchParams();
  const showParam = searchParams.get('show');
  const query = searchParams.get('q') || '';
  const updateParam = (key, value, defaultValue = '') => {
    const params = new URLSearchParams(searchParams);
    if (!value || value === defaultValue) params.delete(key);
    else params.set(key, value);
    setSearchParams(params, { replace: true });
  };
  const [providers, setProviders] = useState([]);
  const [providersError, setProvidersError] = useState(null);
  const [providerCatalog, setProviderCatalog] = useState([]);
  const [activeProviderId, setActiveProviderId] = useState('');
  const [paused, setPaused] = useState(false);
  const [resuming, setResuming] = useState(false);
  const [loading, setLoading] = useState(true);
  const [saving, setSaving] = useState(false);
  const [cronEditing, setCronEditing] = useState({});
  // Only one Configure panel open at a time — a per-task disclosure holding the
  // per-app provider/model override (and, for layered-intelligence, a link to
  // the behavior config on the Edit App → Intelligence tab).
  const [expandedTaskType, setExpandedTaskType] = useState(null);

  // The overrides and the schedule are separate resources: a failed overrides
  // read must never render as every switch off, and a failed schedule read must
  // not render as an empty catalogue. Each is a one-element list so the shared
  // useListResource state machine (loading/ready/error + retry) applies.
  const fetchOverrides = useCallback(
    () => api.getAppTaskTypes(appId, { silent: true }).then(d => [d?.taskTypeOverrides || {}]),
    [appId],
  );
  const fetchSchedule = useCallback(() => api.getCosSchedule({ silent: true }).then(d => [d || {}]), []);
  const overridesResource = useListResource(fetchOverrides);
  const scheduleResource = useListResource(fetchSchedule);
  const overrides = overridesResource.items[0] || {};
  const schedule = scheduleResource.items[0] || null;
  const setOverrides = (update) => overridesResource.setItems(prev => [update(prev[0] || {})]);
  const overridesReady = overridesResource.state === 'ready';
  const scheduleReady = scheduleResource.state === 'ready';

  const fetchData = useCallback(async () => {
    const [statusData, providersData] = await Promise.all([
      api.getCosStatus().catch(() => null),
      api.getProviders({ silent: true }).catch((error) => ({ error }))
    ]);
    setPaused(statusData?.paused === true);
    // A failed provider read keeps the last good catalogue and shows a retry row.
    setProvidersError(providersData?.error || null);
    if (providersData?.error) { setLoading(false); return; }
    // Every ENABLED provider of a runnable type (cli/tui/api) — handler-backed
    // tasks dispatch on provider.type, so any of them is a valid override.
    setProviderCatalog(providersData?.providers || []);
    setActiveProviderId(providersData?.activeProvider || '');
    setProviders((providersData?.providers || []).filter(
      p => RUNNABLE_PROVIDER_TYPES.includes(p.type) && p.enabled !== false
    ));
    setLoading(false);
  }, [appId]);

  useEffect(() => {
    fetchData();
  }, [fetchData]);

  const handleResume = async () => {
    setResuming(true);
    const result = await api.resumeCos({ silent: true }).catch(err => {
      toast.error(err.message);
      return null;
    });
    setResuming(false);
    if (result?.success) {
      setPaused(false);
      toast.success('Scheduled automation resumed');
    }
  };

  const saveOverride = async (taskType, patch) => {
    setSaving(true);
    const result = await api.updateAppTaskTypeOverride(appId, taskType, patch, { silent: true }).catch(err => {
      toast.error(err.message);
      return null;
    });
    setSaving(false);
    if (!result) return false;
    setOverrides(prev => ({ ...prev, [taskType]: { ...prev[taskType], ...patch } }));
    return true;
  };

  const handleToggle = async (taskType, isEnabled) => {
    const newEnabled = !isEnabled;
    await saveOverride(taskType, { enabled: newEnabled });
  };

  const handleIntervalChange = async (taskType, interval) => {
    if (interval === 'cron') {
      // Open cron editor — don't save until user enters expression
      const existing = overrides[taskType]?.interval;
      setCronEditing(prev => ({ ...prev, [taskType]: isCronExpression(existing) ? existing : '0 7 * * *' }));
      return;
    }
    setCronEditing(prev => { const n = { ...prev }; delete n[taskType]; return n; });
    const value = interval === 'null' ? null : interval;
    await saveOverride(taskType, { interval: value });
  };

  const handleCronSave = async (taskType, expr) => {
    const saved = await saveOverride(taskType, { interval: expr });
    if (saved) setCronEditing(prev => { const n = { ...prev }; delete n[taskType]; return n; });
  };

  const handleMetaToggle = async (taskType, field, globalTaskMetadata) => {
    let taskMetadata = toggleAppMetadataOverride(overrides[taskType]?.taskMetadata, globalTaskMetadata, field);
    const globalConfig = schedule?.tasks?.[taskType] || {};
    const nextFileIssues = field === 'fileIssues'
      ? (taskMetadata?.fileIssues ?? globalTaskMetadata?.fileIssues ?? globalConfig.defaultFileIssues)
      : null;
    if (field === 'fileIssues' && nextFileIssues === true && taskMetadata) {
      taskMetadata = { ...taskMetadata, useWorktree: false, openPR: false, simplify: false };
    }
    await saveOverride(taskType, { taskMetadata });
  };

  // One mutation for the whole pin — AppProviderPin hands back an already
  // normalized { providerId, model }, so the clear rule lives in the control
  // rather than being re-derived here (#4783).
  const handlePinChange = async (taskType, patch) => {
    await saveOverride(taskType, patch);
  };

  const handleTrigger = async (taskType) => {
    const toastId = toast.loading(`Sending ${taskType} request for ${appName}…`);
    const result = await api.triggerCosOnDemandTask(taskType, appId, { silent: true }).catch(err => {
      toast.error(err.message);
      return null;
    }).finally(() => toast.dismiss(toastId));
    if (result?.success) {
      toast.success(`Queued ${taskType} request for ${appName} — checks and available capacity determine when an agent appears`);
    }
    return result?.success ? result : null;
  };

  const refreshAll = () => {
    overridesResource.retry();
    scheduleResource.retry();
    fetchData();
  };

  if (loading) {
    return <BrailleSpinner text="Loading automation settings" />;
  }

  const taskTypes = schedule?.tasks ? Object.keys(schedule.tasks).sort() : [];
  const enabledCount = taskTypes.filter(t => overrides[t]?.enabled === true).length;
  // A configured app opens on its live automation; an unconfigured one on the full catalogue.
  const defaultShow = enabledCount > 0 ? 'on' : 'all';
  const show = showParam === 'on' || showParam === 'all' ? showParam : defaultShow;
  const needle = query.trim().toLowerCase();
  const visibleTaskTypes = orderTaskTypes(taskTypes, overrides).filter(t => {
    if (show === 'on' && overrides[t]?.enabled !== true) return false;
    if (!needle) return true;
    return `${t} ${schedule.tasks[t]?.description || ''}`.toLowerCase().includes(needle);
  });
  const clearFilters = () => {
    const params = new URLSearchParams(searchParams);
    params.delete('q');
    params.set('show', 'all');
    if (defaultShow === 'all') params.delete('show');
    setSearchParams(params, { replace: true });
  };
  const allEnabled = taskTypes.length > 0 && taskTypes.every(t => (overrides[t] || {}).enabled === true);

  const handleToggleAll = async () => {
    setSaving(true);
    const newEnabled = !allEnabled;
    const result = await api.toggleAllAppTaskTypes(appId, newEnabled, { silent: true }).catch(err => {
      toast.error(err.message);
      return null;
    });
    setSaving(false);
    if (!result) return;
    setOverrides(prev => {
      const updated = { ...prev };
      for (const t of taskTypes) {
        updated[t] = { ...updated[t], enabled: newEnabled };
      }
      return updated;
    });
  };

  return (
    <div className="space-y-4">
      <div className="border-b border-port-border pb-4">
        <CustomTasksSection
          appId={appId}
          appName={appName}
          providerCatalog={providerCatalog}
          activeProviderId={activeProviderId}
        />
      </div>
      {paused && (
        <div className="bg-port-warning/10 border border-port-warning/40 rounded-lg p-3 flex items-start gap-3">
          <PauseCircle size={18} className="text-port-warning shrink-0 mt-0.5" />
          <div className="flex-1 min-w-0">
            <p className="text-sm font-medium text-port-warning">Scheduled automation is globally paused</p>
            <p className="text-xs text-gray-400 mt-0.5">
              Scheduled and autonomous tasks won&apos;t run until resumed. Any task can still be run once with <span className="text-gray-300">Run Now</span>.
            </p>
          </div>
          <button
            onClick={handleResume}
            disabled={resuming}
            className="px-3 py-1.5 bg-port-warning/20 text-port-warning hover:bg-port-warning/30 rounded-lg text-xs font-medium flex items-center gap-1 disabled:opacity-50 shrink-0"
          >
            <Play size={14} />
            {resuming ? 'Resuming…' : 'Resume'}
          </button>
        </div>
      )}
      <div className="flex flex-wrap items-center justify-between gap-3">
        <div className="flex items-center gap-4 min-w-0">
          <div className="min-w-0">
            <h3 className="text-lg font-semibold text-white">Scheduled Task Options</h3>
            <p className="text-sm text-gray-500 max-w-3xl">
              Each toggle turns that CoS scheduled task on or off for this app. The controls beside it are optional —
              leave one on <em>Inherit</em> and it follows the global schedule defaults.
            </p>
          </div>
          {overridesReady && scheduleReady && <ToggleSwitch enabled={allEnabled} onChange={handleToggleAll} size="sm" activeColor="bg-port-success" disabled={saving} ariaLabel={allEnabled ? 'Disable every scheduled task for this app' : 'Enable every scheduled task for this app'} />}
        </div>
        <button
          onClick={refreshAll}
          disabled={saving}
          className="px-3 py-1.5 bg-port-border hover:bg-port-border/80 text-white rounded-lg text-xs flex items-center gap-1"
        >
          <RefreshCw size={14} /> Refresh
        </button>
      </div>

      {providersError && (
        <ListLoadError what="the provider list" error={providersError} onRetry={fetchData} />
      )}
      {overridesResource.state === 'error' && (
        <ListLoadError what="this app's automation settings" error={overridesResource.error} onRetry={overridesResource.retry} />
      )}
      {scheduleResource.state === 'error' && (
        <ListLoadError what="the task schedule" error={scheduleResource.error} onRetry={scheduleResource.retry} />
      )}
      {(overridesResource.state === 'loading' || scheduleResource.state === 'loading') && (
        <BrailleSpinner text="Loading automation settings" />
      )}

      {!overridesReady || !scheduleReady ? null : taskTypes.length === 0 ? (
        <div className="bg-port-card border border-port-border rounded-lg p-6 text-center text-gray-500">
          No task types configured in the schedule
        </div>
      ) : (
        <>
        <div className="flex flex-wrap items-center gap-3">
          <div role="group" aria-label="Task visibility" className="inline-flex rounded-lg border border-port-border overflow-hidden">
            {[['on', `On for this app (${enabledCount})`], ['all', `All (${taskTypes.length})`]].map(([value, label]) => (
              <button
                key={value}
                type="button"
                aria-pressed={show === value}
                onClick={() => updateParam('show', value, defaultShow)}
                className={`px-3 py-1.5 text-xs font-medium ${show === value ? 'bg-port-accent/20 text-port-accent' : 'text-gray-400 hover:text-white'}`}
              >
                {label}
              </button>
            ))}
          </div>
          <div className="relative flex-1 min-w-[12rem] max-w-md">
            <Search size={14} className="absolute left-2.5 top-1/2 -translate-y-1/2 text-gray-500" aria-hidden="true" />
            <input
              type="search"
              aria-label="Search tasks"
              value={query}
              onChange={e => updateParam('q', e.target.value)}
              placeholder="Search task id or description…"
              className="w-full pl-8 pr-3 py-1.5 bg-port-bg border border-port-border rounded-lg text-xs text-white focus:border-port-accent focus:outline-hidden"
            />
          </div>
        </div>
        {visibleTaskTypes.length === 0 && (
          <div className="bg-port-card border border-port-border rounded-lg p-6 text-center text-gray-500 space-y-2">
            <p>
              No tasks match {show === 'on' ? '“On for this app”' : 'all tasks'}{needle ? ` and “${query.trim()}”` : ''}.
            </p>
            <button type="button" onClick={clearFilters} className="px-3 py-1.5 bg-port-border hover:bg-port-border/80 text-white rounded-lg text-xs">
              Show all tasks
            </button>
          </div>
        )}
        <div className="grid grid-cols-1 lg:grid-cols-2 2xl:grid-cols-3 gap-4 items-start">
          {visibleTaskTypes.map(taskType => {
            const override = overrides[taskType] || {};
            const globalConfig = schedule.tasks[taskType] || {};
            const isEnabled = override.enabled === true;
            const overrideInterval = override.interval || null;
            const effectiveLabel = isCronExpression(overrideInterval)
              ? describeCron(overrideInterval) || 'cron'
              : overrideInterval || (globalConfig.type || 'rotation');
            const intervalSuffix = !overrideInterval && globalConfig.intervalMs ? ` (${Math.round(globalConfig.intervalMs / 3600000)}h)` : '';
            const isExpanded = expandedTaskType === taskType;
            const isLayeredIntelligence = taskType === 'layered-intelligence';
            // The per-app pin outranks the task's Schedule pin at spawn for EVERY
            // task type (#4783), so it always counts toward "effective".
            const hasProviderOverride = hasProviderPin(override);
            const taskProviderName = globalConfig.providerId
              ? providerDisplayName(providers, globalConfig.providerId)
              : 'default (active provider)';
            const effectiveProviderName = override.providerId
              ? providerDisplayName(providers, override.providerId)
              : taskProviderName;
            // Surfaced collapsed (not just inside Configure): an app-level pin
            // silently wins over the Schedule pin at spawn (#4783), so a task
            // schedule showing one provider while this app's override names a
            // DIFFERENT one is exactly the "why did it run somewhere else"
            // confusion a user hits with only the Schedule page open.
            const providerDivergesFromSchedule = providerPinDivergesFromSchedule(override, globalConfig);

            return (
              <AppTaskCard
                key={`${appId}:${taskType}`}
                taskType={taskType}
                config={globalConfig}
                providers={providers}
                activeProviderId={activeProviderId}
                onConfigure={() => setExpandedTaskType(prev => prev === taskType ? null : taskType)}
                onTrigger={handleTrigger}
                appContext={{
                  enabled: isEnabled,
                  onToggle: () => handleToggle(taskType, isEnabled),
                  saving,
                  expanded: isExpanded,
                  interval: overrideInterval,
                  cadence: `${effectiveLabel}${intervalSuffix}`,
                  taskMetadata: override.taskMetadata,
                }}
              >
                {providerDivergesFromSchedule && (
                  <span className="inline-flex items-center gap-1 text-port-warning" title={`Runs on ${effectiveProviderName} — the schedule default is ${taskProviderName}`}>
                    <AlertTriangle size={11} />
                    <span className="text-[10px] uppercase tracking-wide">Provider override</span>
                  </span>
                )}
                {/* Row 2: interval + cron + agent options */}
                <div className="flex flex-wrap items-center gap-2">
                  <select
                    aria-label="Override interval"
                    value={cronEditing[taskType] !== undefined || isCronExpression(overrideInterval) ? 'cron' : (overrideInterval ?? 'null')}
                    onChange={e => handleIntervalChange(taskType, e.target.value)}
                    className="px-2 py-1 bg-port-bg border border-port-border rounded text-xs text-white focus:border-port-accent focus:outline-hidden"
                  >
                    {INTERVAL_OPTIONS.map(opt => (
                      <option key={String(opt.value)} value={String(opt.value)}>{opt.label}</option>
                    ))}
                  </select>
                  {cronEditing[taskType] !== undefined ? (
                    <CronInput
                      value={cronEditing[taskType]}
                      onSave={expr => handleCronSave(taskType, expr)}
                      onCancel={() => setCronEditing(prev => { const n = { ...prev }; delete n[taskType]; return n; })}
                    />
                  ) : isCronExpression(overrideInterval) ? (
                    <button
                      onClick={() => setCronEditing(prev => ({ ...prev, [taskType]: overrideInterval }))}
                      className="px-2 py-1 text-xs text-gray-400 font-mono bg-port-bg border border-port-border rounded hover:border-port-accent cursor-pointer"
                      title={describeCron(overrideInterval)}
                    >
                      {overrideInterval}
                    </button>
                  ) : null}
                  <div className="flex flex-wrap items-center gap-1">
                    {globalConfig.fileIssuesCapable && <label className="text-xs" htmlFor={`audit-depth-${taskType}`}>Depth
                      <select id={`audit-depth-${taskType}`} title="Deep spends more time investigating and fixing worthwhile issues in one run" disabled={saving} className="ml-1 bg-port-bg border border-port-border rounded"
                        value={override.taskMetadata?.auditDepth ?? globalConfig.taskMetadata?.auditDepth ?? 'quick'}
                        onChange={event => saveOverride(taskType, { taskMetadata: { ...override.taskMetadata, auditDepth: event.target.value } })}>
                        <option value="quick">Quick</option><option value="deep">Deep</option>
                      </select>
                    </label>}
                    {globalConfig.fileIssuesCapable && (() => {
                      const effective = override.taskMetadata?.fileIssues ?? globalConfig.taskMetadata?.fileIssues ?? globalConfig.defaultFileIssues === true;
                      const hasOverride = override.taskMetadata?.fileIssues !== undefined;
                      return (
                        <button
                          key="fileIssues"
                          onClick={() => handleMetaToggle(taskType, 'fileIssues', globalConfig.taskMetadata)}
                          aria-pressed={effective}
                          aria-label={`File issues only: ${effective ? 'on' : 'off'}${hasOverride ? ' (app override)' : ' (inherited)'}`}
                          className={`text-xs px-1.5 py-0.5 rounded transition-colors border ${agentOptionButtonClass(effective, hasOverride)}`}
                          title={`File issues only: ${effective ? 'on' : 'off'}${hasOverride ? ' (app override)' : ' (inherited)'}`}
                        >
                          Iss
                        </button>
                      );
                    })()}
                    {AGENT_OPTIONS.map(({ field, shortLabel, label }) => {
                      const effective = override.taskMetadata?.[field] ?? globalConfig.taskMetadata?.[field] ?? false;
                      const hasOverride = override.taskMetadata?.[field] !== undefined;
                      const fileIssuesOn = (override.taskMetadata?.fileIssues ?? globalConfig.taskMetadata?.fileIssues ?? globalConfig.defaultFileIssues) === true;
                      const managed = globalConfig.managedAgentOptions?.includes(field)
                        || (globalConfig.fileIssuesCapable && fileIssuesOn && ['useWorktree', 'openPR', 'simplify'].includes(field));
                      const titleText = managed
                        ? `${label}: managed internally by ${taskType}`
                        : `${label}: ${effective ? 'on' : 'off'}${hasOverride ? ' (app override)' : ' (inherited)'}`;
                      return (
                        <button
                          key={field}
                          onClick={() => handleMetaToggle(taskType, field, globalConfig.taskMetadata)}
                          disabled={managed}
                          aria-pressed={effective}
                          aria-label={managed
                            ? `${label}: managed by task`
                            : `${label}: ${effective ? 'on' : 'off'}${hasOverride ? ' (app override)' : ' (inherited)'}`}
                          className={`text-xs px-1.5 py-0.5 rounded transition-colors border ${agentOptionButtonClass(effective, hasOverride)} ${managed ? 'opacity-50 cursor-not-allowed' : ''}`}
                          title={titleText}
                        >
                          {shortLabel}
                        </button>
                      );
                    })}
                  </div>
                </div>
                {/* Expanded config: per-app provider/model override (+ LI behavior link) */}
                {isExpanded && (
                  <div className="border-t border-port-border pt-3 space-y-3">
                    <AppProviderPin
                      providers={providers}
                      providerId={override.providerId}
                      model={override.model}
                      onChange={patch => handlePinChange(taskType, patch)}
                      label="Provider override"
                      inheritLabel={`Inherit (${taskProviderName})`}
                      layout="stacked"
                    />
                    <p className="text-xs text-gray-500">
                      Effective provider: <span className="text-gray-300">{effectiveProviderName}</span>
                      {hasProviderOverride ? ' (app override, wins over the task default)' : globalConfig.providerId ? ' (task default)' : ''}
                    </p>
                    {isLayeredIntelligence && (
                      <div className="pt-1">
                        <button
                          onClick={() => navigate(`/apps/${appId}?edit=1&appTab=intelligence`)}
                          className="inline-flex items-center gap-1 text-xs text-port-accent hover:underline"
                        >
                          <Sparkles size={12} />
                          Configure behavior (sources, scopes, rules) →
                        </button>
                        <p className="text-xs text-gray-500 mt-1">
                          Telemetry sources, allowed scopes, and guidance rules live on the Edit App → Intelligence tab.
                        </p>
                      </div>
                    )}
                  </div>
                )}
              </AppTaskCard>
            );
          })}
        </div>
        </>
      )}


    </div>
  );
}