LCOV - code coverage report
Current view: top level - src/platform/workspace/stores - partnerNodeGovernanceStore.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 34.8 % 89 31
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { defineStore } from 'pinia'
       2              : import { computed, ref, shallowRef, watch } from 'vue'
       3              : 
       4              : import { useFeatureFlags } from '@/composables/useFeatureFlags'
       5              : import {
       6              :   getPartnerNodePolicy,
       7              :   getPartnerProviders,
       8              :   PartnerNodePolicyApiError,
       9              :   updatePartnerNodePolicy
      10              : } from '@/platform/workspace/api/partnerNodePolicyApi'
      11              : import type {
      12              :   PartnerNodePolicy,
      13              :   PartnerProvider
      14              : } from '@/platform/workspace/api/partnerNodePolicyApi'
      15              : import { useTeamWorkspaceStore } from '@/platform/workspace/stores/teamWorkspaceStore'
      16              : 
      17              : export type PartnerNodePolicyStatus =
      18              :   | 'inactive'
      19              :   | 'loading'
      20              :   | 'unconfigured'
      21              :   | 'configured'
      22              :   | 'ineligible'
      23              :   | 'error'
      24              : 
      25         2355 : export const usePartnerNodeGovernanceStore = defineStore(
      26              :   'partnerNodeGovernance',
      27              :   () => {
      28          104 :     const { flags } = useFeatureFlags()
      29          104 :     const workspaceStore = useTeamWorkspaceStore()
      30              : 
      31          104 :     const providers = shallowRef<PartnerProvider[]>([])
      32          104 :     const policy = shallowRef<PartnerNodePolicy | null>(null)
      33          104 :     const status = ref<PartnerNodePolicyStatus>('inactive')
      34          104 :     const error = shallowRef<Error | null>(null)
      35          104 :     const activeSaveIdsByWorkspace = shallowRef(new Map<string, number>())
      36          104 :     let requestVersion = 0
      37          104 :     let nextSaveId = 0
      38              : 
      39          104 :     const governedWorkspaceId = computed(() =>
      40              :       flags.partnerNodeGovernanceEnabled
      41              :         ? (workspaceStore.activeWorkspace?.id ?? null)
      42              :         : null
      43              :     )
      44          104 :     const isSaving = computed(() => {
      45            0 :       const workspaceId = governedWorkspaceId.value
      46            0 :       return (
      47              :         workspaceId !== null && activeSaveIdsByWorkspace.value.has(workspaceId)
      48              :       )
      49              :     })
      50              : 
      51            0 :     function createInitialPolicy(): PartnerNodePolicy {
      52            0 :       return {
      53              :         enforcementEnabled: false,
      54              :         providers: providers.value.map(({ id }) => ({
      55              :           providerId: id,
      56              :           enabled: true
      57              :         }))
      58              :       }
      59              :     }
      60              : 
      61            0 :     function isProviderEnabled(providerId: string): boolean {
      62            0 :       if (!policy.value) return true
      63            0 :       return (
      64              :         policy.value.providers.find(
      65              :           (provider) => provider.providerId === providerId
      66              :         )?.enabled === true
      67              :       )
      68              :     }
      69              : 
      70          104 :     async function loadPolicy(): Promise<void> {
      71          104 :       const workspaceId = governedWorkspaceId.value
      72          104 :       const version = ++requestVersion
      73          104 :       if (!workspaceId) {
      74          104 :         providers.value = []
      75          104 :         policy.value = null
      76          104 :         status.value = 'inactive'
      77          104 :         error.value = null
      78          104 :         return
      79              :       }
      80              : 
      81            0 :       providers.value = []
      82            0 :       policy.value = null
      83            0 :       status.value = 'loading'
      84            0 :       error.value = null
      85              : 
      86            0 :       const [providersResult, policyResult] = await Promise.allSettled([
      87              :         getPartnerProviders(),
      88              :         getPartnerNodePolicy()
      89              :       ])
      90          104 :       if (
      91              :         version !== requestVersion ||
      92              :         governedWorkspaceId.value !== workspaceId
      93              :       ) {
      94            0 :         return
      95              :       }
      96              : 
      97          104 :       const loadError =
      98              :         policyResult.status === 'rejected'
      99              :           ? policyResult.reason
     100              :           : providersResult.status === 'rejected'
     101              :             ? providersResult.reason
     102              :             : null
     103          104 :       if (loadError === null) {
     104            0 :         const nextPolicy =
     105              :           policyResult.status === 'fulfilled' ? policyResult.value : null
     106            0 :         providers.value =
     107              :           providersResult.status === 'fulfilled' ? providersResult.value : []
     108            0 :         policy.value = nextPolicy
     109            0 :         status.value = nextPolicy ? 'configured' : 'unconfigured'
     110            0 :         return
     111              :       }
     112              : 
     113              :       // An ineligible workspace may still be allowed to read the catalog —
     114              :       // keep it so the panel can render the gated view.
     115          104 :       const ineligible =
     116              :         loadError instanceof PartnerNodePolicyApiError &&
     117              :         loadError.status === 403
     118          104 :       providers.value =
     119              :         ineligible && providersResult.status === 'fulfilled'
     120              :           ? providersResult.value
     121              :           : []
     122          104 :       policy.value = null
     123          104 :       error.value =
     124              :         loadError instanceof Error
     125              :           ? loadError
     126              :           : new Error('Failed to load partner provider policy')
     127          104 :       status.value = ineligible ? 'ineligible' : 'error'
     128              :     }
     129              : 
     130            0 :     async function savePolicy(nextPolicy: PartnerNodePolicy): Promise<void> {
     131            0 :       const workspaceId = governedWorkspaceId.value
     132            0 :       if (!workspaceId) return
     133            0 :       if (activeSaveIdsByWorkspace.value.has(workspaceId)) {
     134            0 :         console.error('Provider policy save already in progress')
     135            0 :         return
     136              :       }
     137              : 
     138            0 :       const version = ++requestVersion
     139            0 :       const saveId = ++nextSaveId
     140            0 :       activeSaveIdsByWorkspace.value = new Map(
     141              :         activeSaveIdsByWorkspace.value
     142              :       ).set(workspaceId, saveId)
     143            0 :       try {
     144            0 :         const savedPolicy = await updatePartnerNodePolicy(nextPolicy)
     145            0 :         if (
     146              :           version !== requestVersion ||
     147              :           governedWorkspaceId.value !== workspaceId
     148              :         ) {
     149            0 :           return
     150              :         }
     151              : 
     152            0 :         policy.value = savedPolicy
     153            0 :         status.value = 'configured'
     154            0 :         error.value = null
     155              :       } catch (saveError) {
     156            0 :         if (
     157              :           version !== requestVersion ||
     158              :           governedWorkspaceId.value !== workspaceId
     159              :         ) {
     160            0 :           return
     161              :         }
     162            0 :         if (
     163              :           saveError instanceof PartnerNodePolicyApiError &&
     164              :           saveError.status === 422
     165              :         ) {
     166            0 :           await loadPolicy()
     167              :         }
     168            0 :         throw saveError
     169              :       } finally {
     170            0 :         if (activeSaveIdsByWorkspace.value.get(workspaceId) === saveId) {
     171            0 :           const nextActiveSaveIds = new Map(activeSaveIdsByWorkspace.value)
     172            0 :           nextActiveSaveIds.delete(workspaceId)
     173            0 :           activeSaveIdsByWorkspace.value = nextActiveSaveIds
     174              :         }
     175              :       }
     176              :     }
     177              : 
     178            0 :     async function setProviderEnabled(
     179              :       providerId: string,
     180              :       enabled: boolean
     181              :     ): Promise<void> {
     182            0 :       if (!providers.value.some(({ id }) => id === providerId)) return
     183              : 
     184            0 :       const currentPolicy = policy.value ?? createInitialPolicy()
     185              :       const currentEntry = currentPolicy.providers.find(
     186              :         (provider) => provider.providerId === providerId
     187              :       )
     188            0 :       const nextProviders = currentEntry
     189              :         ? currentPolicy.providers.map((provider) =>
     190              :             provider.providerId === providerId
     191              :               ? { ...provider, enabled }
     192              :               : provider
     193              :           )
     194              :         : [...currentPolicy.providers, { providerId, enabled }]
     195              : 
     196            0 :       await savePolicy({
     197              :         ...currentPolicy,
     198              :         enforcementEnabled: currentPolicy.enforcementEnabled || !enabled,
     199              :         providers: nextProviders
     200              :       })
     201              :     }
     202              : 
     203            0 :     async function setProvidersEnabled(
     204              :       providerIds: string[],
     205              :       enabled: boolean
     206              :     ): Promise<void> {
     207            0 :       const targetIds = new Set(providerIds)
     208            0 :       if (!providers.value.some(({ id }) => targetIds.has(id))) return
     209              : 
     210            0 :       const currentPolicy = policy.value ?? createInitialPolicy()
     211            0 :       await savePolicy({
     212              :         ...currentPolicy,
     213              :         enforcementEnabled: currentPolicy.enforcementEnabled || !enabled,
     214              :         providers: providers.value.map(({ id }) =>
     215              :           targetIds.has(id)
     216              :             ? { providerId: id, enabled }
     217              :             : (currentPolicy.providers.find(
     218              :                 ({ providerId }) => providerId === id
     219              :               ) ?? { providerId: id, enabled: false })
     220              :         )
     221              :       })
     222              :     }
     223              : 
     224            0 :     async function setAllProvidersEnabled(enabled: boolean): Promise<void> {
     225            0 :       const currentPolicy = policy.value ?? createInitialPolicy()
     226            0 :       await savePolicy({
     227              :         ...currentPolicy,
     228              :         enforcementEnabled: currentPolicy.enforcementEnabled || !enabled,
     229              :         providers: providers.value.map(({ id }) => ({
     230              :           providerId: id,
     231              :           enabled
     232              :         }))
     233              :       })
     234              :     }
     235              : 
     236            0 :     async function setEnforcementEnabled(enabled: boolean): Promise<void> {
     237              :       const currentPolicy = policy.value ?? createInitialPolicy()
     238            0 :       await savePolicy({
     239              :         ...currentPolicy,
     240              :         enforcementEnabled: enabled
     241              :       })
     242              :     }
     243              : 
     244          104 :     watch(governedWorkspaceId, () => void loadPolicy(), { immediate: true })
     245              : 
     246          104 :     return {
     247              :       providers,
     248              :       policy,
     249              :       status,
     250              :       error,
     251              :       isSaving,
     252              :       governedWorkspaceId,
     253              :       isProviderEnabled,
     254              :       loadPolicy,
     255              :       setProviderEnabled,
     256              :       setProvidersEnabled,
     257              :       setAllProvidersEnabled,
     258              :       setEnforcementEnabled
     259              :     }
     260              :   }
     261              : )
        

Generated by: LCOV version 2.0-1