{"version":3,"file":"runInContainerStore.cjs","names":["dashboardStateFor","sessionStatusLine","setWorkspaceSandbox"],"sources":["../../../src/web/stores/runInContainerStore.ts"],"sourcesContent":["import type { Store } from '@tanstack/store';\n\nimport type { WorkspaceSummary } from '../../types/hub';\nimport { setWorkspaceSandbox, type WorkspaceSandboxRequest } from '../lib/hubApi';\nimport { sessionStatusLine } from '../lib/sessionSummary';\nimport { dashboardStateFor } from './dashboardStateStore';\nimport type { SessionMeta } from './sessionsStore';\nimport { applyWorkspaceUpsert } from './workspacesStore';\n\n/**\n * The \"Run in container\" switch on a workspace's Defaults page: what the host\n * reports, mapped to the approved states, plus this page's own pending request,\n * its failure, and which confirmation is open.\n */\n\n/** What the switch shows. */\nexport type RunInContainerState =\n  | { kind: 'off' }\n  | { kind: 'on' }\n  | { kind: 'unavailable'; reason: string }\n  | { kind: 'on-unavailable'; reason: string }\n  | { kind: 'starting' }\n  | { kind: 'stopping' }\n  | { kind: 'error'; message: string };\n\n/** The confirmation the switch opened; the off dialog picks its copy from the running sessions. */\nexport type RunInContainerDialog = 'on' | 'off' | undefined;\n\nexport interface RunningContainerSession {\n  id: string;\n  name: string;\n  status: string;\n}\n\nexport interface RunInContainerLocal {\n  /** The PATCH in flight, by target. */\n  pending?: WorkspaceSandboxRequest;\n  /** The last PATCH this page sent, which Try again re-sends. */\n  last?: WorkspaceSandboxRequest;\n  /** Why the last PATCH failed. */\n  error?: string;\n  dialog?: RunInContainerDialog;\n}\n\nconst CONTAINER = 'container';\nconst PROVIDER_UNAVAILABLE =\n  'The container provider is not available. Turn on Help mode and ask Po to check the sandbox setup.';\nconst FOLDER_UNAVAILABLE = 'The workspace folder could not be opened. Check that it still exists and Po can read it.';\n// The host records the switch before the container starts, so this does not promise where new sessions run.\nconst DID_NOT_START = 'The container could not start.';\nconst DID_NOT_STOP = 'The container did not stop, so new sessions still start in it.';\nconst STOPPED_WORKING = 'The container stopped, so new sessions cannot start in it.';\n\nexport function runInContainerStore(workspaceId: string): Store<RunInContainerLocal> {\n  return dashboardStateFor<RunInContainerLocal>(JSON.stringify(['run-in-container', workspaceId]), {});\n}\n\nfunction sentence(lead: string, detail: string | undefined): string {\n  return detail === undefined ? lead : `${lead} ${detail}`;\n}\n\n/** Server state first, then this page's request on top: a pending PATCH shows its target, a failed one stays red. */\nexport function runInContainerStateOf(workspace: WorkspaceSummary, local: RunInContainerLocal): RunInContainerState {\n  if (local.pending !== undefined) return { kind: local.pending === CONTAINER ? 'starting' : 'stopping' };\n  if (local.error !== undefined) return { kind: 'error', message: local.error };\n  const sandboxed = workspace.sandbox !== undefined;\n  const reason = workspace.sandboxReason;\n  if (workspace.available === false && !sandboxed) return { kind: 'unavailable', reason: FOLDER_UNAVAILABLE };\n  switch (workspace.sandboxState) {\n    case 'starting':\n      return { kind: 'starting' };\n    case 'stopping':\n      return { kind: 'stopping' };\n    case 'unavailable':\n      return sandboxed\n        ? { kind: 'on-unavailable', reason: reason ?? PROVIDER_UNAVAILABLE }\n        : { kind: 'unavailable', reason: reason ?? PROVIDER_UNAVAILABLE };\n    case 'failed':\n      return { kind: 'error', message: sentence(sandboxed ? STOPPED_WORKING : DID_NOT_START, reason) };\n    default:\n      return { kind: sandboxed ? 'on' : 'off' };\n  }\n}\n\n/**\n * The sessions the off dialog lists. While a workspace is sandboxed, every\n * session the hub lists for it runs in the container; a dormant one is not\n * running, so it is left out.\n */\nexport function runningContainerSessions(\n  byId: Readonly<Record<string, SessionMeta>>,\n  order: readonly string[],\n  workspaceId: string,\n  now: number,\n): RunningContainerSession[] {\n  return order.flatMap((id) => {\n    const meta = byId[id];\n    if (meta === undefined) return [];\n    const { summary } = meta;\n    if (summary.workspaceId !== workspaceId || summary.dormant === true) return [];\n    const status = sessionStatusLine(\n      {\n        attach: meta.attach,\n        phase: summary.phase,\n        phaseSince: summary.phaseSince,\n        awaitingInput: summary.awaitingInput,\n        everPrompted: summary.everPrompted,\n        backgroundWorkActive: summary.backgroundWorkActive,\n        lastSettledAt: summary.lastSettledAt,\n        ...(summary.activity === undefined ? {} : { activity: summary.activity }),\n      },\n      now,\n    );\n    return [{ id, name: summary.name, status }];\n  });\n}\n\n/** The switch only asks; nothing changes until the person confirms. */\nexport function askRunInContainer(workspaceId: string, checked: boolean): void {\n  const store = runInContainerStore(workspaceId);\n  if (store.state.pending !== undefined) return;\n  store.setState((state) => ({ ...state, dialog: checked ? 'on' : 'off' }));\n}\n\nexport function cancelRunInContainer(workspaceId: string): void {\n  runInContainerStore(workspaceId).setState((state) => ({ ...state, dialog: undefined }));\n}\n\nasync function send(workspaceId: string, sandbox: WorkspaceSandboxRequest): Promise<void> {\n  const store = runInContainerStore(workspaceId);\n  if (store.state.pending !== undefined) return;\n  store.setState(() => ({ pending: sandbox, last: sandbox }));\n  const result = await setWorkspaceSandbox(workspaceId, sandbox);\n  if ('workspace' in result) {\n    applyWorkspaceUpsert({ workspace: result.workspace });\n    store.setState((state) => ({ last: state.last }));\n    return;\n  }\n  store.setState((state) => ({\n    last: state.last,\n    error: sentence(sandbox === CONTAINER ? DID_NOT_START : DID_NOT_STOP, result.error),\n  }));\n}\n\n/** Sends the PATCH the open dialog asked for. */\nexport async function confirmRunInContainer(workspaceId: string): Promise<void> {\n  const dialog = runInContainerStore(workspaceId).state.dialog;\n  if (dialog === undefined) return;\n  await send(workspaceId, dialog === 'on' ? CONTAINER : null);\n}\n\n/**\n * Try again re-sends the last PATCH. After a reload there is none, and a host\n * failure is always the container not running, so it asks for the container again.\n */\nexport async function retryRunInContainer(workspaceId: string): Promise<void> {\n  const last = runInContainerStore(workspaceId).state.last;\n  await send(workspaceId, last === undefined ? CONTAINER : last);\n}\n"],"mappings":";;;;;AA4CA,MAAM,YAAY;AAClB,MAAM,uBACJ;AACF,MAAM,qBAAqB;AAE3B,MAAM,gBAAgB;AACtB,MAAM,eAAe;AACrB,MAAM,kBAAkB;AAExB,SAAgB,oBAAoB,aAAiD;CACnF,OAAOA,4BAAAA,kBAAuC,KAAK,UAAU,CAAC,oBAAoB,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