LCOV - code coverage report
Current view: top level - src/platform/workspace/stores - deploymentPickStore.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 18.0 % 217 39
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import type { ReleaseUnsupportedPack } from '@comfyorg/ingest-types'
       2              : import type { WorkspaceDeployment } from '@/platform/workspace/api/workspaceApi'
       3              : import { defineStore } from 'pinia'
       4              : import { computed, shallowRef, watch } from 'vue'
       5              : 
       6              : import { reportError } from '@comfyorg/telemetry/reportError'
       7              : import { pendingServerFact } from '@/platform/serverFacts/pendingServerFact'
       8              : import {
       9              :   DEPLOYMENT_LISTING_BACKSTOP_MS,
      10              :   DEPLOYMENT_LISTING_RETRY_DELAYS_MS
      11              : } from '@/platform/workspace/api/deploymentListingTimeouts'
      12              : import { WorkspaceApiError } from '@/platform/remote/ingest/ingestApiError'
      13              : import { workspaceApi } from '@/platform/workspace/api/workspaceApi'
      14              : import {
      15              :   hasDeploymentLink,
      16              :   readDeploymentHint,
      17              :   setDeploymentHint,
      18              :   setUnconfirmedCloudHint
      19              : } from '@/platform/workspace/deploymentHint'
      20              : import type {
      21              :   DeploymentPickEvent,
      22              :   DeploymentPickSource,
      23              :   DeploymentPickState
      24              : } from '@/platform/workspace/deploymentPickState'
      25              : import {
      26              :   changeBetween,
      27              :   loadedEvent,
      28              :   reduceDeploymentPick
      29              : } from '@/platform/workspace/deploymentPickState'
      30              : import { reloadForDeploymentSwitch } from '@/platform/workspace/deploymentSwitchReload'
      31              : import { useTeamWorkspaceStore } from '@/platform/workspace/stores/teamWorkspaceStore'
      32              : 
      33              : type LinkPickOutcome =
      34              :   | { kind: 'picked' }
      35              :   | { kind: 'unchanged' }
      36              :   | { kind: 'timedOut' }
      37              :   | { kind: 'refused'; message: string }
      38              : 
      39              : /**
      40              :  * The deployment this browser runs on and the deployments it may pick
      41              :  * (FE-2434), plus the workspace's default deployment an owner sets for
      42              :  * members with no pick of their own (BE-17480). A deployment keeps its URL
      43              :  * while its owner updates it to new Releases, so a pick follows those updates.
      44              :  *
      45              :  * A pick changes what the editor is served at boot (the node
      46              :  * catalog of the Release the deployment runs and its packs' JavaScript), so a successful pick or clear reloads
      47              :  * the page rather than patching the running editor.
      48              :  */
      49         2356 : export const useDeploymentPickStore = defineStore('deploymentPick', () => {
      50         1975 :   const workspaceStore = useTeamWorkspaceStore()
      51              : 
      52         1975 :   const state = shallowRef<DeploymentPickState>({ phase: 'idle' })
      53              : 
      54            9 :   function dispatch(event: DeploymentPickEvent) {
      55            9 :     state.value = reduceDeploymentPick(state.value, event)
      56              :   }
      57              : 
      58         1975 :   const deployments = computed(() =>
      59              :     'deployments' in state.value ? state.value.deployments : []
      60              :   )
      61         1975 :   const pickedDeploymentId = computed(() =>
      62              :     'pickedDeploymentId' in state.value ? state.value.pickedDeploymentId : null
      63              :   )
      64         1975 :   const pickedDeployment = computed(
      65              :     () =>
      66              :       deployments.value.find(
      67              :         (d) => d.deployment_id === pickedDeploymentId.value
      68              :       ) ?? null
      69              :   )
      70         1975 :   const pickSource = computed<DeploymentPickSource | null>(() =>
      71              :     'pickSource' in state.value ? state.value.pickSource : null
      72              :   )
      73              :   /**
      74              :    * The workspace's default deployment when it is gone: still recorded, so
      75              :    * an owner can clear it, but members on it run on Comfy Cloud.
      76              :    */
      77         1975 :   const goneDefaultDeploymentId = computed(() =>
      78              :     'goneDefaultDeploymentId' in state.value
      79              :       ? state.value.goneDefaultDeploymentId
      80              :       : null
      81              :   )
      82              :   /**
      83              :    * Set once a listing says ingest found this browser's own pick gone and
      84              :    * cleared it. Only that one listing says so, so the page keeps it until a
      85              :    * later listing shows a deployment picked again.
      86              :    */
      87         1975 :   const sawPickGone = shallowRef(false)
      88         1975 :   const goneDeployment = computed<'pick' | 'default' | null>(() => {
      89            0 :     if (sawPickGone.value) return 'pick'
      90            0 :     if (
      91              :       goneDefaultDeploymentId.value !== null &&
      92              :       pickSource.value !== 'browser'
      93              :     ) {
      94            0 :       return 'default'
      95              :     }
      96            0 :     return null
      97              :   })
      98         1975 :   const defaultDeploymentId = computed(() =>
      99              :     'defaultDeploymentId' in state.value
     100              :       ? state.value.defaultDeploymentId
     101              :       : null
     102              :   )
     103         1975 :   const defaultDeployment = computed(
     104              :     () =>
     105              :       deployments.value.find(
     106              :         (d) => d.deployment_id === defaultDeploymentId.value
     107              :       ) ?? null
     108              :   )
     109              :   /**
     110              :    * True when this browser has no pick of its own and so runs on whatever
     111              :    * the workspace says: its default deployment, or Comfy Cloud without one.
     112              :    */
     113         1975 :   const followsWorkspace = computed(() => pickSource.value !== 'browser')
     114         1975 :   const canSetDefault = computed(() =>
     115              :     pendingServerFact(
     116              :       'BE-20674',
     117              :       workspaceStore.activeWorkspace?.role === 'owner'
     118              :     )
     119              :   )
     120              :   /**
     121              :    * True once a listing has answered, and while a reload of it is in flight.
     122              :    * False before any listing answers, when the account cannot pick, and when
     123              :    * the load failed with no listing to show. A listing that answered is shown
     124              :    * even when a newer load fails or never answers.
     125              :    */
     126         1975 :   const isVisible = computed(() => 'deployments' in state.value)
     127         1975 :   const isSwitching = computed(() => state.value.phase === 'switching')
     128         1975 :   const listingRefused = shallowRef(false)
     129              :   /**
     130              :    * The deployment this page booted on, from the first listing that
     131              :    * answered: null for Comfy Cloud (a gone pick, a rollout refusal, an ingest
     132              :    * without these routes, or a boot listing the editor gave up on; see
     133              :    * `bootOnComfyCloud`), undefined before the model source is known. The
     134              :    * editor's node catalog is that deployment's Release as it was at boot,
     135              :    * since a pick or a workspace switch reloads the page.
     136              :    */
     137         1975 :   const bootDeployment = shallowRef<WorkspaceDeployment | null | undefined>(
     138              :     undefined
     139              :   )
     140              :   /**
     141              :    * A change made elsewhere since this page booted that its nodes do not
     142              :    * show, read from the latest listing: `release` when its deployment now
     143              :    * runs another Release, `deployment` when it now runs on another
     144              :    * deployment or Comfy Cloud with other nodes (an owner changed the
     145              :    * default it follows, or its pick is gone). Jobs already run on the new
     146              :    * one; only a reload brings its nodes. Null when nothing changed.
     147              :    */
     148         1975 :   const changeSinceBoot = computed(() => {
     149            0 :     const boot = bootDeployment.value
     150            0 :     if (boot === undefined || !isVisible.value) return null
     151            0 :     return changeBetween(boot, pickedDeployment.value)
     152              :   })
     153         1975 :   const unsupportedPacks = shallowRef<ReleaseUnsupportedPack[]>([])
     154              : 
     155         1975 :   watch(bootDeployment, async (boot) => {
     156            0 :     if (!boot || !workspaceStore.workspaceId) return
     157            0 :     try {
     158            0 :       const answer = await workspaceApi.getReleaseUnsupportedPacks()
     159            0 :       if (answer.release_id === boot.release_id) {
     160            0 :         unsupportedPacks.value = answer.packs
     161              :       }
     162              :     } catch (err) {
     163            0 :       reportError(err, {
     164              :         surface: 'workspace',
     165              :         errorType: 'error_loading_release_unsupported_packs',
     166              :         level: 'warning'
     167              :       })
     168              :     }
     169              :   })
     170              : 
     171              :   /**
     172              :    * The pack the boot deployment's build could not load for this Comfy
     173              :    * Registry id, matched on its registry id, then its folder.
     174              :    */
     175            2 :   function unsupportedPackFor(
     176              :     cnrId: string
     177              :   ): ReleaseUnsupportedPack | undefined {
     178            2 :     const packs = unsupportedPacks.value
     179            2 :     return (
     180              :       packs.find((pack) => pack.registry_id === cnrId) ??
     181              :       packs.find((pack) => pack.folder === cnrId)
     182              :     )
     183              :   }
     184         1975 :   const switcherOpenAsks = shallowRef(0)
     185              : 
     186            0 :   function requestSwitcherOpen() {
     187            0 :     switcherOpenAsks.value++
     188              :   }
     189              : 
     190              :   /**
     191              :    * Orders the answers. Each `load()` takes the next number (`latestRequest`);
     192              :    * `lastApplied` is the number of the newest answer put on screen. A listing
     193              :    * or a 403 applies when it is newer than that, so a listing that answered
     194              :    * stays shown when a newer load fails or never answers; an older listing
     195              :    * still passes on a gone pick it reports. A plain failure
     196              :    * applies only when it is the latest load asked for and nothing newer has
     197              :    * applied, so it never replaces a listing that may still arrive. A default
     198              :    * the server took moves `lastApplied` up to `latestRequest`, which drops
     199              :    * every listing asked for before the change.
     200              :    */
     201         1975 :   let latestRequest = 0
     202         1975 :   let lastApplied = 0
     203         1975 :   let reportedLoadFailure = false
     204         1975 :   let requestBeforeStart: ReadonlyMap<string, number> = new Map()
     205              : 
     206            0 :   function load(): Promise<void> {
     207            0 :     return loadListing([])
     208              :   }
     209              : 
     210              :   /**
     211              :    * Name a deployment whose start the server took on every listing, until one
     212              :    * asked for after the start offers Start on it again or no longer lists it.
     213              :    */
     214            0 :   function followStart(deploymentId: string) {
     215            0 :     requestBeforeStart = new Map(requestBeforeStart).set(
     216              :       deploymentId,
     217              :       latestRequest
     218              :     )
     219              :   }
     220              : 
     221            0 :   function forgetSettledStarts(
     222              :     request: number,
     223              :     deployments: readonly WorkspaceDeployment[]
     224              :   ) {
     225            0 :     requestBeforeStart = new Map(
     226              :       [...requestBeforeStart].filter(([deploymentId, beforeStart]) => {
     227            0 :         if (request <= beforeStart) return true
     228              :         const listed = deployments.find((d) => d.deployment_id === deploymentId)
     229            0 :         return listed?.can_start === false
     230              :       })
     231              :     )
     232              :   }
     233              : 
     234            0 :   async function loadListing(retryDelaysMs: readonly number[]): Promise<void> {
     235            0 :     const id = workspaceStore.workspaceId
     236            0 :     if (!id) return
     237            0 :     const request = ++latestRequest
     238            0 :     const event = await listingEvent(
     239              :       id,
     240              :       [...requestBeforeStart.keys()],
     241              :       retryDelaysMs,
     242              :       () => workspaceStore.workspaceId === id,
     243              :       (err) => noteLoadFailure(id, err)
     244              :     )
     245              :     const workspaceChangedMeanwhile = workspaceStore.workspaceId !== id
     246            0 :     if (workspaceChangedMeanwhile) return
     247            0 :     if (event.type === 'loaded') forgetSettledStarts(request, event.deployments)
     248            0 :     applyAnswer(id, request, event)
     249              :   }
     250              : 
     251            0 :   function applyAnswer(
     252              :     workspaceId: string,
     253              :     request: number,
     254              :     event: DeploymentPickEvent
     255              :   ) {
     256            0 :     if (request <= lastApplied) {
     257            0 :       if (event.type === 'loaded') keepPickGone(event.gonePickedDeploymentId)
     258            0 :       return
     259              :     }
     260            0 :     if (event.type === 'loadFailed') {
     261            0 :       if (request === latestRequest) dispatch(event)
     262            0 :       return
     263              :     }
     264            0 :     lastApplied = request
     265            0 :     dispatch(event)
     266            0 :     listingRefused.value = event.type === 'loadRefused'
     267            0 :     if (event.type === 'loaded') noteListing(event.gonePickedDeploymentId)
     268            0 :     if (event.type === 'loadRefused' && bootDeployment.value === undefined) {
     269            0 :       bootDeployment.value = null
     270              :     }
     271            0 :     rememberBootTarget(workspaceId)
     272              :   }
     273              : 
     274            0 :   function noteLoadFailure(workspaceId: string, err: unknown) {
     275            0 :     if (
     276              :       isTransient(err) &&
     277              :       readDeploymentHint(workspaceId) === 'unknown' &&
     278              :       !hasDeploymentLink()
     279              :     ) {
     280            0 :       setUnconfirmedCloudHint(workspaceId)
     281              :     }
     282            0 :     if (reportedLoadFailure || workspaceStore.workspaceId !== workspaceId)
     283            0 :       return
     284            0 :     reportedLoadFailure = true
     285            0 :     reportError(err, {
     286              :       surface: 'workspace',
     287              :       errorType: 'error_loading_deployment_listing',
     288              :       ...(isSessionRefused(err) && {
     289              :         level: 'warning',
     290              :         tags: { status: 401, outcome: 'denied' }
     291              :       })
     292              :     })
     293              :   }
     294              : 
     295            0 :   function rememberBootTarget(workspaceId: string) {
     296            0 :     setDeploymentHint(
     297              :       workspaceId,
     298              :       pickedDeployment.value?.deployment_id ?? null
     299              :     )
     300              :   }
     301              : 
     302            0 :   function reloadOnto(workspaceId: string, deploymentId: string | null) {
     303            0 :     setDeploymentHint(workspaceId, deploymentId)
     304            0 :     reloadForDeploymentSwitch(() => {
     305            0 :       dispatch({ type: 'switchFailed' })
     306            0 :       void load()
     307              :     })
     308              :   }
     309              : 
     310            0 :   function keepPickGone(gonePickedDeploymentId: string | null) {
     311            0 :     if (gonePickedDeploymentId !== null && pickedDeployment.value === null) {
     312            0 :       sawPickGone.value = true
     313              :     }
     314              :   }
     315              : 
     316            0 :   function noteListing(gonePickedDeploymentId: string | null) {
     317            0 :     if (gonePickedDeploymentId !== null) sawPickGone.value = true
     318            0 :     else if (pickedDeployment.value !== null) sawPickGone.value = false
     319            0 :     if (bootDeployment.value === undefined) {
     320            0 :       bootDeployment.value = pickedDeployment.value
     321              :     }
     322              :   }
     323              : 
     324         1975 :   let firstLoad: Promise<void> | null = null
     325              : 
     326              :   /**
     327              :    * Another workspace, or another account (signed in without a reload), has
     328              :    * its own deployments: drop the old listing at once rather than show it
     329              :    * until the new one answers, and drop any listing still in flight. The
     330              :    * deployment this page booted on stays, since its nodes are still the
     331              :    * editor's until a reload.
     332              :    */
     333         1975 :   watch(
     334              :     () => workspaceStore.workspaceId,
     335              :     () => {
     336            9 :       dispatch({ type: 'workspaceChanged' })
     337            9 :       sawPickGone.value = false
     338            9 :       listingRefused.value = false
     339            9 :       lastApplied = latestRequest
     340            9 :       firstLoad = null
     341              :     },
     342              :     { flush: 'sync' }
     343              :   )
     344              : 
     345              :   /**
     346              :    * The page's first load, started at boot so the editor learns which
     347              :    * deployment it booted on; later calls wait on that same load. It retries
     348              :    * a failure with no definite answer (`DEPLOYMENT_LISTING_RETRY_DELAYS_MS`)
     349              :    * and reports only the last one.
     350              :    */
     351            0 :   function loadOnce(): Promise<void> {
     352            0 :     if (!firstLoad && workspaceStore.workspaceId) {
     353            0 :       firstLoad = loadListing(DEPLOYMENT_LISTING_RETRY_DELAYS_MS)
     354              :     }
     355            0 :     return firstLoad ?? Promise.resolve()
     356              :   }
     357              : 
     358              :   /**
     359              :    * The editor starts on Comfy Cloud's models when the boot listing failed
     360              :    * or did not answer in time. A listing that answers later does not change
     361              :    * that; it shows as `changeSinceBoot` when it names a deployment.
     362              :    */
     363            0 :   function bootOnComfyCloud() {
     364            0 :     if (bootDeployment.value === undefined) bootDeployment.value = null
     365              :   }
     366              : 
     367              :   /**
     368              :    * Pick a deployment for this browser, or null for Comfy Cloud, then reload so
     369              :    * the editor boots on it. Resolves to the server's refusal, or null when
     370              :    * the pick took (the page is reloading) or there was nothing to do.
     371              :    * Choosing what this browser already runs on does nothing, so a browser
     372              :    * that follows the workspace default keeps following it. Picking Comfy
     373              :    * Cloud while following a default is this browser's own choice: it wins
     374              :    * over the default, until `followWorkspace` drops it.
     375              :    */
     376            0 :   async function pick(deploymentId: string | null): Promise<string | null> {
     377            0 :     const id = workspaceStore.workspaceId
     378            0 :     if (!id || state.value.phase !== 'ready') return null
     379            0 :     if (deploymentId === pickedDeploymentId.value) return null
     380            0 :     return pickAndReload(id, deploymentId)
     381              :   }
     382              : 
     383            0 :   function runsOnComfyCloud(): boolean {
     384            0 :     if (listingRefused.value) return true
     385            0 :     return isVisible.value && pickedDeploymentId.value === null
     386              :   }
     387              : 
     388            0 :   async function pickFromLink(
     389              :     deploymentId: string | null
     390              :   ): Promise<LinkPickOutcome> {
     391            0 :     const id = workspaceStore.workspaceId
     392            0 :     if (!id || state.value.phase === 'switching') return { kind: 'unchanged' }
     393            0 :     let timeout: ReturnType<typeof setTimeout> | undefined
     394              :     const completed = await Promise.race([
     395              :       loadOnce().then(() => true),
     396              :       new Promise<false>((resolve) => {
     397            0 :         timeout = setTimeout(
     398              :           () => resolve(false),
     399              :           DEPLOYMENT_LISTING_BACKSTOP_MS
     400              :         )
     401              :       })
     402              :     ]).finally(() => clearTimeout(timeout))
     403            0 :     if (!completed) return { kind: 'timedOut' }
     404            0 :     if (workspaceStore.workspaceId !== id || isSwitching.value) {
     405            0 :       return { kind: 'unchanged' }
     406              :     }
     407              :     const alreadyOn =
     408              :       deploymentId === null
     409              :         ? runsOnComfyCloud()
     410              :         : deploymentId === pickedDeploymentId.value
     411            0 :     if (alreadyOn) return { kind: 'unchanged' }
     412            0 :     return pickLinkAndReload(id, deploymentId)
     413              :   }
     414              : 
     415            0 :   async function pickLinkAndReload(
     416              :     workspaceId: string,
     417              :     deploymentId: string | null
     418              :   ): Promise<LinkPickOutcome> {
     419            0 :     dispatch({ type: 'switchStarted', target: deploymentId })
     420            0 :     const controller = new AbortController()
     421            0 :     let timeout: ReturnType<typeof setTimeout> | undefined
     422              :     const request =
     423              :       deploymentId === null
     424              :         ? workspaceApi.clearDeployment(workspaceId, {
     425              :             signal: controller.signal
     426              :           })
     427              :         : workspaceApi.pickDeployment(
     428              :             workspaceId,
     429              :             { deployment_id: deploymentId },
     430              :             controller.signal
     431              :           )
     432            0 :     const selection = request.then<LinkPickOutcome, LinkPickOutcome>(
     433              :       () => ({ kind: 'picked' }),
     434              :       (error: unknown) => ({ kind: 'refused', message: errorMessage(error) })
     435              :     )
     436            0 :     const outcome = await Promise.race([
     437              :       selection,
     438              :       new Promise<LinkPickOutcome>((resolve) => {
     439            0 :         timeout = setTimeout(() => {
     440            0 :           resolve({ kind: 'timedOut' })
     441            0 :           controller.abort()
     442              :         }, DEPLOYMENT_LISTING_BACKSTOP_MS)
     443              :       })
     444              :     ]).finally(() => clearTimeout(timeout))
     445            0 :     if (workspaceStore.workspaceId !== workspaceId) return { kind: 'unchanged' }
     446            0 :     if (outcome.kind === 'picked') reloadOnto(workspaceId, deploymentId)
     447            0 :     else dispatch({ type: 'switchFailed' })
     448            0 :     return outcome
     449              :   }
     450              : 
     451            0 :   async function pickAndReload(
     452              :     workspaceId: string,
     453              :     deploymentId: string | null
     454              :   ): Promise<string | null> {
     455            0 :     dispatch({ type: 'switchStarted', target: deploymentId })
     456            0 :     try {
     457            0 :       if (deploymentId === null) {
     458            0 :         await workspaceApi.clearDeployment(workspaceId)
     459              :       } else {
     460            0 :         await workspaceApi.pickDeployment(workspaceId, {
     461              :           deployment_id: deploymentId
     462              :         })
     463              :       }
     464              :     } catch (err) {
     465            0 :       dispatch({ type: 'switchFailed' })
     466            0 :       return errorMessage(err)
     467              :     }
     468            0 :     reloadOnto(workspaceId, deploymentId)
     469            0 :     return null
     470              :   }
     471              : 
     472            0 :   async function followInPlace(workspaceId: string): Promise<string | null> {
     473            0 :     dispatch({ type: 'switchStarted', target: pickedDeploymentId.value })
     474            0 :     try {
     475            0 :       await workspaceApi.clearDeployment(workspaceId, { follow: 'workspace' })
     476              :     } catch (err) {
     477            0 :       dispatch({ type: 'switchFailed' })
     478            0 :       return errorMessage(err)
     479              :     }
     480            0 :     lastApplied = latestRequest
     481            0 :     dispatch({ type: 'followedInPlace' })
     482            0 :     await load()
     483            0 :     return null
     484              :   }
     485              : 
     486              :   /**
     487              :    * Drop this browser's own pick so it runs on whatever the workspace says
     488              :    * (its default deployment, or Comfy Cloud), then reload. Resolves like `pick`.
     489              :    */
     490            0 :   async function followWorkspace(): Promise<string | null> {
     491            0 :     const id = workspaceStore.workspaceId
     492            0 :     if (!id || state.value.phase !== 'ready') return null
     493            0 :     if (followsWorkspace.value) return null
     494            0 :     if (defaultDeploymentId.value === pickedDeploymentId.value) {
     495            0 :       return followInPlace(id)
     496              :     }
     497            0 :     dispatch({ type: 'switchStarted', target: defaultDeploymentId.value })
     498            0 :     try {
     499            0 :       await workspaceApi.clearDeployment(id, { follow: 'workspace' })
     500              :     } catch (err) {
     501            0 :       dispatch({ type: 'switchFailed' })
     502            0 :       return errorMessage(err)
     503              :     }
     504            0 :     reloadOnto(id, defaultDeploymentId.value)
     505            0 :     return null
     506              :   }
     507              : 
     508              :   /** True when this default would change what this browser runs on. */
     509            0 :   function defaultMovesThisBrowser(deploymentId: string | null): boolean {
     510            0 :     return followsWorkspace.value && deploymentId !== pickedDeploymentId.value
     511              :   }
     512              : 
     513              :   /**
     514              :    * Set the workspace's default deployment (owner only), or clear it with null.
     515              :    * Members with no pick of their own move to it at their next boot. This
     516              :    * browser reloads only when it has no pick of its own, since only then
     517              :    * does the new default change what it runs on. Otherwise the new default
     518              :    * shows at once and the listing is refreshed to confirm it; a failed
     519              :    * refresh keeps the new default on screen, since the server has it, and a
     520              :    * listing asked for before the change is dropped when it lands.
     521              :    * Resolves to the server's refusal, or null when the default took.
     522              :    */
     523            0 :   async function setDefault(
     524              :     deploymentId: string | null
     525              :   ): Promise<string | null> {
     526            0 :     const id = workspaceStore.workspaceId
     527            0 :     if (!id || state.value.phase !== 'ready') return null
     528              :     const recordedDefault =
     529              :       defaultDeploymentId.value ?? goneDefaultDeploymentId.value
     530            0 :     if (deploymentId === recordedDefault) return null
     531            0 :     dispatch({ type: 'switchStarted', target: pickedDeploymentId.value })
     532            0 :     try {
     533            0 :       if (deploymentId === null) {
     534            0 :         await workspaceApi.clearDefaultDeployment(id)
     535              :       } else {
     536            0 :         await workspaceApi.setDefaultDeployment(id, {
     537              :           deployment_id: deploymentId
     538              :         })
     539              :       }
     540              :     } catch (err) {
     541            0 :       dispatch({ type: 'switchFailed' })
     542            0 :       return errorMessage(err)
     543              :     }
     544            0 :     if (defaultMovesThisBrowser(deploymentId)) {
     545            0 :       reloadOnto(id, deploymentId)
     546            0 :       return null
     547              :     }
     548            0 :     lastApplied = latestRequest
     549            0 :     dispatch({ type: 'defaultChanged', defaultDeploymentId: deploymentId })
     550            0 :     await load()
     551            0 :     return null
     552              :   }
     553              : 
     554         1975 :   return {
     555              :     state,
     556              :     deployments,
     557              :     pickedDeploymentId,
     558              :     pickedDeployment,
     559              :     goneDeployment,
     560              :     pickSource,
     561              :     defaultDeploymentId,
     562              :     goneDefaultDeploymentId,
     563              :     defaultDeployment,
     564              :     followsWorkspace,
     565              :     canSetDefault,
     566              :     isVisible,
     567              :     isSwitching,
     568              :     listingRefused,
     569              :     bootDeployment,
     570              :     changeSinceBoot,
     571              :     unsupportedPackFor,
     572              :     switcherOpenAsks,
     573              :     load,
     574              :     loadOnce,
     575              :     followStart,
     576              :     bootOnComfyCloud,
     577              :     pick,
     578              :     pickFromLink,
     579              :     followWorkspace,
     580              :     setDefault,
     581              :     defaultMovesThisBrowser,
     582              :     requestSwitcherOpen
     583              :   }
     584              : })
     585              : 
     586            0 : async function listingEvent(
     587              :   workspaceId: string,
     588              :   startedDeploymentIds: readonly string[],
     589              :   retryDelaysMs: readonly number[],
     590              :   stillWanted: () => boolean,
     591              :   onFailure: (err: unknown) => void
     592              : ): Promise<DeploymentPickEvent> {
     593            0 :   for (let attempt = 0; ; attempt++) {
     594            0 :     try {
     595            0 :       return loadedEvent(
     596              :         await workspaceApi.listDeployments(workspaceId, startedDeploymentIds)
     597              :       )
     598              :     } catch (err) {
     599              :       const retriesLeft = attempt < retryDelaysMs.length
     600            0 :       if (!retriesLeft || !isTransient(err) || !stillWanted()) {
     601            0 :         return loadFailure(err, onFailure)
     602              :       }
     603            0 :       await new Promise((resolve) =>
     604              :         setTimeout(resolve, retryDelaysMs[attempt])
     605              :       )
     606            0 :       if (!stillWanted()) return { type: 'loadFailed' }
     607              :     }
     608              :   }
     609              : }
     610              : 
     611              : /**
     612              :  * A listing request that got no definite answer: no response (a network
     613              :  * error or the request timeout), a 408 or 429, or a 5xx. A failure before
     614              :  * the request (the sign-in step) is not retried.
     615              :  */
     616            0 : function isTransient(err: unknown): boolean {
     617            0 :   if (!(err instanceof WorkspaceApiError)) return false
     618            0 :   const { status } = err
     619            0 :   return (
     620              :     status === undefined || status === 408 || status === 429 || status >= 500
     621              :   )
     622              : }
     623              : 
     624            0 : function isSessionRefused(err: unknown): boolean {
     625            0 :   return err instanceof WorkspaceApiError && err.status === 401
     626              : }
     627              : 
     628            0 : function loadFailure(
     629              :   err: unknown,
     630              :   onFailure: (err: unknown) => void
     631              : ): DeploymentPickEvent {
     632            0 :   if (
     633              :     err instanceof WorkspaceApiError &&
     634              :     (err.status === 403 || err.status === 404)
     635              :   ) {
     636            0 :     return { type: 'loadRefused' }
     637              :   }
     638            0 :   onFailure(err)
     639            0 :   return { type: 'loadFailed' }
     640              : }
     641              : 
     642            0 : function errorMessage(err: unknown): string {
     643            0 :   return err instanceof Error ? err.message : String(err)
     644              : }
        

Generated by: LCOV version 2.0-1