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

9.7 KB
Raw Blame
// From lib/, NOT services/apiCore.js: this file is imported by a node-env server
// test (appProcessTypes' DESKTOP_TYPES parity check), where apiCore's React
// dependency doesn't resolve.
import { PORTOS_APP_ID } from '../../lib/appIdentity.js';
import { APP_FEATURE_IDS, INSTANCE_FEATURES } from '../../../../server/lib/instanceFeatureRegistry.js';

export const NON_PM2_TYPES = new Set(['ios-native', 'macos-native', 'xcode', 'swift']);

// The app-config text input / select styling. Shared so a restyle of these forms
// is one edit rather than one per config surface. Must stay a COMPLETE literal
// class string — Tailwind only sees classes it can read in source.
export const INPUT_CLASS = 'w-full px-3 py-2 bg-port-bg border border-port-border rounded-lg text-white focus:border-port-accent focus:outline-hidden';

// Mirrors DESKTOP_TYPES in server/services/appProcessTypes.js — app types that run
// a GUI/desktop process with no HTTP port (a game binary). Kept in sync by hand
// (a parity test asserts the two Sets match); the server stays the source of truth
// for what the type MEANS (autorestart off, command-launched, exempt from
// auto-restart supervision). The client only needs it to decide whether Start
// should open the build-output panel.
export const DESKTOP_TYPES = new Set(['desktop']);

/** Whether an app type is a portless GUI/desktop process. */
export const isDesktopType = (type) => DESKTOP_TYPES.has(type);

/**
 * The process whose live output the launch panel should tail after a Start —
 * or `null` when no panel should open.
 *
 * The gate is on the PROCESS's own result, not on the response being truthy:
 * `POST /api/apps/:id/start` answers 200 `{ success: true, results }` even when
 * every entry in `results` reports `{ success: false }`, so truthiness would open
 * a panel that spins on "Building and importing assets" forever for a process
 * that never launched — the exact "reads as hung" failure the panel exists to
 * remove. A rejected request (`result` null) opens nothing either. A 200 that
 * carries no per-process detail is trusted, so an older server still gets a panel.
 *
 * @param {object|null} app The app record (needs `type` + `pm2ProcessNames`).
 * @param {object|null} result The start endpoint's parsed response, or null if it threw.
 * @returns {string|null} PM2 process name to tail, or null.
 */
export function resolveLaunchPanelProcess(app, result) {
  if (!isDesktopType(app?.type)) return null;
  const processName = app?.pm2ProcessNames?.[0];
  if (!processName || !result) return null;
  return result?.results?.[processName]?.success === false ? null : processName;
}

// Mirrors NON_NODE_TYPES / NON_STANDARDIZABLE_TYPES in
// server/services/appProcessTypes.js (parity tests assert the Sets match). The
// PM2 standardizer writes a NODE ecosystem config from a prompt that opens "You
// are analyzing a Node.js application", so a Python/Go/Docker/static repo must
// not be offered the flow. It's a DENY list, not an allowlist of Node types:
// `type` is a free-form string persisted on the app record, so records in the
// wild carry legacy values this file has never heard of, and an allowlist would
// silently withdraw the button from all of them. See the server's rationale.
export const NON_NODE_TYPES = new Set(['python', 'go', 'docker', 'static']);

export const NON_STANDARDIZABLE_TYPES = new Set([...NON_PM2_TYPES, ...NON_NODE_TYPES]);

/** Whether the PM2 standardizer (which writes a NODE ecosystem config) applies. */
export const isStandardizable = (type) => !NON_STANDARDIZABLE_TYPES.has(type);

// Every app type that can reach a UI label. Kept TOTAL (with a raw-type
// fallback) rather than a ternary chain ending in '🔨 Xcode' — that default
// meant any type not explicitly listed rendered as an Xcode project.
const APP_TYPE_LABELS = {
  'ios-native': '📱 iOS',
  'macos-native': '🖥️ macOS',
  swift: '🐦 Swift',
  xcode: '🔨 Xcode',
  python: '🐍 Python',
  go: '🐹 Go',
  docker: '🐳 Docker',
  static: '📄 Static',
  desktop: '🎮 Desktop',
  'vite+express': '⚡ Vite + Express',
  vite: '⚡ Vite',
  'single-node-server': '🟢 Node',
  express: '🟢 Express',
  nextjs: '▲ Next.js'
};

export const getAppTypeLabel = (type) => APP_TYPE_LABELS[type] || type || 'Unknown';

// Where an app's autonomous work items live. Mirrors WORK_TRACKERS +
// TRACKER_LABELS in server/lib/workTracker.js — shared by the Edit App picker
// and the /do:next run drawer so one vocabulary describes both.
export const WORK_TRACKER_OPTIONS = [
  { value: 'auto', label: 'Auto (detect from git origin)' },
  { value: 'plan', label: 'PLAN.md' },
  { value: 'github', label: 'GitHub Issues' },
  { value: 'gitlab', label: 'GitLab Issues' },
  { value: 'jira', label: 'JIRA' }
];

export const WORK_TRACKER_LABELS = Object.fromEntries(
  WORK_TRACKER_OPTIONS.map(o => [o.value, o.label])
);

/** The word a tracker uses for one work item — "issue", "ticket", "item". */
export const workItemNoun = (tracker) =>
  tracker === 'jira' ? 'ticket' : (tracker === 'github' || tracker === 'gitlab') ? 'issue' : 'item';

/**
 * Whether an app has been configured for a JIRA workflow.
 *
 * Either opt-in counts on its own:
 *   - `jira.enabled` — the integration is switched on (the JIRA tab's own config panel)
 *   - `workTracker === 'jira'` — the app's work items live in JIRA (Edit App → Workflow)
 *
 * The second clause remains the bootstrap path for existing app records: JIRA's
 * config panel lives on the JIRA detail tab, so selecting JIRA as the work
 * tracker is still an app-local signal even before integration config exists.
 * `'auto'` never resolves to JIRA (it classifies the git origin host →
 * github/gitlab/plan), so an app only lands here by an explicit choice.
 */
export const appUsesJira = (app) => !!app?.jira?.enabled || app?.workTracker === 'jira';

export const APP_FEATURES = Object.freeze(
  APP_FEATURE_IDS.map(id => INSTANCE_FEATURES.find(feature => feature.id === id)).filter(Boolean)
);

export { APP_FEATURE_IDS };

const hasOwn = (value, key) => Object.hasOwn(value, key);

// Read a new explicit override without collapsing an intentional null (inherit)
// into an absent key. Invalid values from hand-edited legacy records inherit so
// one malformed app flag cannot hide a tab forever.
const readFeatureOverride = (app, featureId) => {
  const overrides = app?.featureOverrides;
  if (!overrides || typeof overrides !== 'object' || Array.isArray(overrides) || !hasOwn(overrides, featureId)) {
    return undefined;
  }
  const value = overrides[featureId];
  return typeof value === 'boolean' || value === null ? value : null;
};

// Older app records used integration config as the only app-local signal. Keep
// those records meaningful while allowing a new null override to deliberately
// switch them back to the install-wide setting.
const readLegacyFeatureOverride = (app, featureId) => {
  if (featureId === 'datadog' && typeof app?.datadog?.enabled === 'boolean') {
    return app.datadog.enabled;
  }
  if (featureId === 'jira') {
    if (app?.workTracker === 'jira') return true;
    if (typeof app?.jira?.enabled === 'boolean') return app.jira.enabled;
  }
  return undefined;
};

/** The app-local choice, or null when this app inherits the global setting. */
export const getAppFeatureOverride = (app, featureId) => {
  const override = readFeatureOverride(app, featureId);
  if (override !== undefined) return override;
  return readLegacyFeatureOverride(app, featureId) ?? null;
};

/** Resolve an app tab against its app override and the install-wide value. */
export const isAppFeatureEnabled = (app, featureId, globalEnabled) => {
  const override = readFeatureOverride(app, featureId);
  if (override === true || override === false) return override;
  if (override === null) return globalEnabled !== false;
  const legacy = readLegacyFeatureOverride(app, featureId);
  return legacy ?? (globalEnabled !== false);
};

/** Whether launch videos are disabled for an Eidoverse Worlds app. */
export const isLaunchVideoDisabled = (app) => {
  if (!app) return false;
  if (app.id === 'eidoverse-worlds' || app.id === 'eidoverse') return true;
  if (Array.isArray(app.pm2ProcessNames) && app.pm2ProcessNames.includes('eidoverse-worlds')) return true;
  return false;
};

// Overview first, then alphabetical. Every id is a real route segment
// (`/apps/:appId/:tab`) so each tab is linkable, bookmarkable, and reachable
// from ⌘K — see the routing rules in client/src/AGENTS.md. Icons live in
// `APP_DETAIL_TAB_ICONS` (`AppDetailView.jsx`) so this module stays importable
// from node-env server tests.
//
// `feature` gates a tab against the install-wide feature setting, with an
// app-level `featureOverrides` choice taking precedence. `visibleWhen(app)` is
// reserved for structural conditions that are not feature participation.
export const APP_DETAIL_TABS = [
  { id: 'overview', label: 'Overview' },
  { id: 'automation', label: 'Automation' },
  { id: 'datadog', label: 'DataDog', feature: 'datadog' },
  { id: 'documents', label: 'Documents' },
  { id: 'git', label: 'Git' },
  { id: 'gsd', label: 'GSD', feature: 'gsd' },
  { id: 'issues', label: 'Issues' },
  { id: 'pull-requests', label: 'PRs / MRs' },
  { id: 'jira', label: 'JIRA', feature: 'jira' },
  { id: 'launch-video', label: 'Launch Video', visibleWhen: (app) => !isLaunchVideoDisabled(app) },
  { id: 'processes', label: 'Processes' },
  { id: 'quality', label: 'Quality' },
  { id: 'references', label: 'References' },
  // Only repos that declare submodules (a .gitmodules file) get the tab — most
  // managed apps have none, and an always-empty tab is just noise.
  { id: 'submodules', label: 'Submodules', visibleWhen: (app) => !!app?.hasSubmodules },
  { id: 'tasks', label: 'Tasks' },
  { id: 'update', label: 'Update', visibleWhen: (app) => app?.id === PORTOS_APP_ID },
];