Skip to content

The agent that grows with you

13.6 KB
Raw Blame
import { useStore } from '@nanostores/react'
import { type ReactElement, type ReactNode, useState } from 'react'

import { BrandMark } from '@/components/brand-mark'
import { Button } from '@/components/ui/button'
import { Codicon } from '@/components/ui/codicon'
import type { DesktopUpdateStatus, DesktopVersionInfo } from '@/global'
import { type Translations, useI18n } from '@/i18n'
import { AlertTriangle, CheckCircle2, ExternalLink, Loader2, RefreshCw } from '@/lib/icons'
import { rollbackRelease } from '@/lib/update-copy'
import { cn } from '@/lib/utils'
import { shortVersion } from '@/lib/version-label'
import {
  $backendUpdateApply,
  $backendUpdateChecking,
  $backendUpdateStatus,
  $updateApply,
  $updateChecking,
  $updateStatus,
  checkBackendUpdates,
  checkUpdates,
  openUpdateOverlayFor,
  startActiveUpdate,
  type UpdateApplyState,
  type UpdateTarget
} from '@/store/updates'

const RELEASE_NOTES_URL = 'https://github.com/NousResearch/hermes-agent/releases'
const INSTALLER_URL = 'https://hermes-agent.nousresearch.com/'

export type UpdateStatusTone = 'idle' | 'available' | 'error' | 'unsupported'

export interface UpdateStatusView {
  line: string
  tone: UpdateStatusTone
  error?: string
  updateAvailable: boolean
  applying: boolean
  supported: boolean
}

function retirementStatus(
  retirement: NonNullable<DesktopUpdateStatus['retirement']>,
  applying: boolean,
  supported: boolean,
  u: Translations['updates']
): UpdateStatusView {
  // Suffixed-identity build: nothing to download or migrate — just the notice.
  return {
    applying,
    supported,
    updateAvailable: false,
    tone: 'error',
    line: u.discontinuedTitle,
    error: u.discontinuedBody
  }
}

/**
 * One status derivation for every "am I up to date?" surface (About page,
 * updates overlay). Pure so the tone/copy contract is unit-testable.
 */
interface UpdateStatusInput {
  apply: UpdateApplyState
  checking: boolean
  status: DesktopUpdateStatus | null
  target: UpdateTarget
  u: Translations['updates']
  /** Names the pinned release when a source install is current on stable. */
  latestRelease?: (version: string) => string
  /** Names a move back to an older stable release the checkout already contains. */
  rollback?: (version: string) => string
  /** Names a stable release the updater refuses, so this install stays where it is. */
  held?: (version: string) => string
}

export function deriveUpdateStatus(input: UpdateStatusInput): UpdateStatusView {
  const { apply, status, target, u } = input

  if (target === 'client' && status?.retirement) {
    return retirementStatus(
      status.retirement,
      apply.applying || apply.stage === 'restart',
      status.supported !== false,
      u
    )
  }

  return ordinaryUpdateStatus(input)
}

/** The waiting update, or the older stable release it would move back to. */
function availableLine(
  status: DesktopUpdateStatus | null,
  behind: number,
  u: Translations['updates'],
  rollback?: (version: string) => string
): string {
  const back = rollbackRelease(status)

  if (rollback && back) {
    return rollback(back)
  }

  return behind > 0 ? u.updateReady(behind) : u.updateReadyUnknown
}

function ordinaryUpdateStatus({
  apply,
  checking,
  held,
  latestRelease,
  rollback,
  status,
  target,
  u
}: UpdateStatusInput): UpdateStatusView {
  const behind = status?.behind ?? 0
  // behind is null when the exact count is unknowable (shallow clone): the
  // backend flags that case via updateAvailable instead of a number.
  const updateAvailable = behind > 0 || Boolean(status?.updateAvailable)
  const supported = status?.supported !== false
  const applying = apply.applying || apply.stage === 'restart'

  if (!supported) {
    return { applying, line: status?.message ?? u.unsupportedMessage, supported, tone: 'unsupported', updateAvailable }
  }

  if (status?.error) {
    return {
      applying,
      error: [status.message, status.error].filter(l => !!l).join('\n'),
      line: u.cantReach,
      supported,
      tone: 'error',
      updateAvailable
    }
  }

  if (applying) {
    return { applying, line: u.installing, supported, tone: 'available', updateAvailable }
  }

  if (updateAvailable) {
    return {
      applying,
      line: availableLine(status, behind, u, rollback),
      supported,
      tone: 'available',
      updateAvailable
    }
  }

  if (status) {
    return {
      applying,
      line: currentLine(status, target, u, latestRelease, held),
      supported,
      tone: 'idle',
      updateAvailable
    }
  }

  return { applying, line: checking ? u.checking : u.tapCheck, supported, tone: 'idle', updateAvailable }
}

/** The line for an install with nothing to apply: a held release, the pinned stable release, or "latest". */
function currentLine(
  status: DesktopUpdateStatus,
  target: UpdateTarget,
  u: Translations['updates'],
  latestRelease?: (version: string) => string,
  held?: (version: string) => string
): string {
  if (held && status.held && status.sourceVersion) {
    return held(`v${status.sourceVersion}`)
  }

  if (target === 'backend') {
    return u.latestBodyBackend
  }

  // Stable pins to a release; name it. A checkout ahead of it (forward-only) is newer
  // than the release, so it keeps the generic line instead of claiming to be on it.
  const pinned =
    latestRelease &&
    status.channel === 'stable' &&
    status.sourceVersion &&
    // Older checkers omit the field and may also pin an ahead checkout to HEAD.
    status.aheadOfRelease === false &&
    status.targetSha &&
    status.currentSha === status.targetSha

  return pinned ? latestRelease(`v${status.sourceVersion}`) : u.latestBody
}

function relativeTime(ms: number | undefined, u: Translations['updates']): string {
  if (!ms) {
    return u.never
  }

  const diff = Date.now() - ms

  if (diff < 60_000) {
    return u.justNow
  }

  if (diff < 3_600_000) {
    return u.minAgo(Math.round(diff / 60_000))
  }

  if (diff < 86_400_000) {
    return u.hoursAgo(Math.round(diff / 3_600_000))
  }

  return u.daysAgo(Math.round(diff / 86_400_000))
}

/**
 * The "Hermes Desktop / version / brand mark" hero shared by the About page
 * and the updates overlay, including the bundle-out-of-sync warning. The
 * heading render is injectable so a dialog surface can emit a DialogTitle
 * for its accessible name while About keeps a plain h2.
 */
export function VersionHero({
  renderHeading,
  version
}: {
  renderHeading?: (heading: string) => ReactNode
  version: DesktopVersionInfo | null
}): ReactElement {
  const { t } = useI18n()
  const u = t.updates

  return (
    <div className="flex flex-col items-center gap-3 pt-6 pb-2 text-center">
      <BrandMark className="size-16" />
      <div>
        {renderHeading ? (
          renderHeading(u.appName)
        ) : (
          <h2 className="text-lg font-semibold tracking-tight">{u.appName}</h2>
        )}
        <p className="mt-1 text-xs text-muted-foreground">
          {version?.appVersion ? u.version(shortVersion(version.appVersion)) : u.versionUnavailable}
          {version?.channel
            ? ` · ${Object.entries(u.channels).find(([name]: [string, string]): boolean => name === version.channel)?.[1] ?? version.channel}`
            : ''}
        </p>
      </div>
      {(version?.bundleSwapPending || version?.bundleOutOfSync) && (
        <div className="mx-auto w-full max-w-2xl rounded-xl border border-amber-500/40 bg-amber-500/10 px-4 py-3 text-left text-sm">
          <div className="flex items-start gap-2">
            <AlertTriangle className="mt-0.5 size-4 shrink-0 text-amber-600 dark:text-amber-400" />
            <div className="min-w-0">
              <p className="font-medium">{version.bundleSwapPending ? u.bundleSwapPending : u.bundleOutOfSync}</p>
              <p className="mt-1 text-xs text-muted-foreground">
                {version.bundleSwapPending ? u.bundleSwapPendingDesc : u.bundleOutOfSyncDesc}
              </p>
              {version.bundleSwapPending ? (
                <Button
                  className="mt-2"
                  onClick={() => void window.hermesDesktop?.relaunchApp?.()}
                  size="sm"
                  variant="textStrong"
                >
                  <RefreshCw className="size-3" />
                  {u.bundleSwapPendingAction}
                </Button>
              ) : (
                <Button asChild className="mt-2" size="sm" variant="textStrong">
                  <a
                    href={INSTALLER_URL}
                    onClick={event => {
                      event.preventDefault()
                      void window.hermesDesktop?.openExternal?.(INSTALLER_URL)
                    }}
                    rel="noreferrer"
                    target="_blank"
                  >
                    <ExternalLink className="size-3" />
                    {u.bundleOutOfSyncAction}
                  </a>
                </Button>
              )}
            </div>
          </div>
        </div>
      )}
    </div>
  )
}

interface UpdateActionsProps {
  target: UpdateTarget
  u: Translations['updates']
  view: UpdateStatusView
}

function UpdateActions({ target, u, view }: UpdateActionsProps): ReactElement | null {
  if (view.applying) {
    return null
  }

  if (!view.updateAvailable || !view.supported) {
    return null
  }

  return (
    <>
      <Button onClick={() => startActiveUpdate(target)} size="sm">
        {u.updateNow}
      </Button>
      <Button onClick={() => openUpdateOverlayFor(target)} size="sm" variant="textStrong">
        {u.seeWhatsNew}
      </Button>
    </>
  )
}

/**
 * The bordered update-state card (status line, last-checked age, check /
 * update / release-notes actions) in the About page's visual language.
 * Target-aware: 'client' reads the desktop self-update atoms, 'backend' the
 * remote backend's. The actual apply flow stays in the updates overlay —
 * "Update now" opens it and starts the install there.
 */
export function UpdateStatusCard({
  showReleaseNotes = true,
  target,
  version
}: {
  showReleaseNotes?: boolean
  target: UpdateTarget
  version: DesktopVersionInfo | null
}): ReactElement {
  // An installed bundle waiting only for a restart owns the client status; a
  // stale check error beside it contradicts it (#128195).
  if (target === 'client' && version?.bundleSwapPending) {
    return <></>
  }

  return <UpdateStatusCardBody showReleaseNotes={showReleaseNotes} target={target} />
}

function UpdateStatusCardBody({
  showReleaseNotes,
  target
}: {
  showReleaseNotes: boolean
  target: UpdateTarget
}): ReactElement {
  const { t } = useI18n()
  const u = t.updates
  const isBackend = target === 'backend'
  const status = useStore(isBackend ? $backendUpdateStatus : $updateStatus)
  const checking = useStore(isBackend ? $backendUpdateChecking : $updateChecking)
  const apply = useStore(isBackend ? $backendUpdateApply : $updateApply)
  const [justChecked, setJustChecked] = useState<boolean>(false)

  const view = deriveUpdateStatus({
    apply,
    checking,
    held: t.settings.about.channel.heldBody,
    latestRelease: t.settings.about.channel.latestRelease,
    rollback: t.settings.about.channel.rollbackBody,
    status,
    target,
    u
  })

  const handleCheck = async (): Promise<void> => {
    setJustChecked(false)
    const next = await (isBackend ? checkBackendUpdates({ force: true }) : checkUpdates({ force: true }))
    setJustChecked(Boolean(next))
  }

  return (
    <div
      className={cn(
        'rounded-xl border px-4 py-3 text-sm',
        view.tone === 'available' && 'border-primary/30 bg-primary/5 text-foreground',
        view.tone === 'error' && 'border-destructive/35 bg-destructive/5 text-destructive',
        (view.tone === 'idle' || view.tone === 'unsupported') && 'border-border/70 bg-muted/20 text-foreground'
      )}
    >
      <div className="flex items-start gap-2">
        {view.tone === 'available' ? (
          <Codicon className="mt-0.5 size-4 shrink-0 text-primary" name="cloud-download" size="1rem" />
        ) : view.tone === 'error' || view.tone === 'unsupported' ? null : (
          <CheckCircle2 className="mt-0.5 size-4 shrink-0 text-emerald-600 dark:text-emerald-400" />
        )}
        <div className="min-w-0">
          <p className="font-medium">{view.line}</p>
          {view.error && <p className="mt-1 text-xs text-muted-foreground">{view.error}</p>}
          {view.tone !== 'unsupported' && (
            <p className="mt-1 text-xs text-muted-foreground">
              {u.lastChecked(relativeTime(status?.fetchedAt, u))}
              {justChecked && !checking ? u.justNowSuffix : ''}
            </p>
          )}
        </div>
      </div>

      {view.tone !== 'unsupported' && (
        <div className="mt-3 flex flex-wrap items-center gap-4">
          <Button
            disabled={checking || view.applying}
            onClick={() => void handleCheck()}
            size="sm"
            variant="textStrong"
          >
            {checking ? <Loader2 className="size-3 animate-spin" /> : <RefreshCw className="size-3" />}
            {checking ? u.checkingShort : u.checkNow}
          </Button>

          <UpdateActions target={target} u={u} view={view} />

          {showReleaseNotes && (
            <Button asChild className="ml-auto" size="sm" variant="text">
              <a
                href={RELEASE_NOTES_URL}
                onClick={event => {
                  event.preventDefault()
                  void window.hermesDesktop?.openExternal?.(RELEASE_NOTES_URL)
                }}
                rel="noreferrer"
                target="_blank"
              >
                <ExternalLink className="size-3" />
                {u.releaseNotes}
              </a>
            </Button>
          )}
        </div>
      )}
    </div>
  )
}