Line data Source code
1 : import { ref } from 'vue'
2 :
3 : import { useToast } from '@/components/ui/toast/toastStore'
4 : import { useAuthDialogs } from '@/composables/auth/useAuthDialogs'
5 : import { t } from '@/i18n'
6 : import { reportError } from '@comfyorg/telemetry/reportError'
7 : import { createInstallationBundle } from '@/platform/workflow/deploy/api/installationBundleApi'
8 : import type { BundleFailureReason } from '@/platform/workflow/deploy/api/installationBundleApi'
9 : import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore'
10 : import { useApiKeyAuthStore } from '@/stores/apiKeyAuthStore'
11 : import { useAuthStore } from '@/stores/authStore'
12 : import type { AuthHeader } from '@comfyorg/api-client/authTypes'
13 :
14 : type InstallationDeployState =
15 : | { status: 'idle' }
16 : | { status: 'uploading' }
17 : | { status: 'ready'; url: string }
18 :
19 : type DeployFailure = {
20 : ok: false
21 : reason: BundleFailureReason | 'signed_out'
22 : cause?: unknown
23 : }
24 :
25 : type TokenLookup =
26 : | { ok: true; token: string | undefined }
27 : | { ok: false; cause: unknown }
28 :
29 : type SignInPrompt =
30 : | { ok: true; signedIn: boolean }
31 : | { ok: false; cause: unknown }
32 :
33 : type CredentialLookup =
34 : | { ok: true; header: AuthHeader; owner: string | null }
35 : | DeployFailure
36 :
37 1 : const FAILURE_MESSAGE_KEY = {
38 : unauthorized: 'deployToComfyApi.bundleUnauthorized',
39 : too_many_bundles: 'deployToComfyApi.bundleTooMany',
40 : too_large: 'deployToComfyApi.bundleTooLarge',
41 : offline: 'deployToComfyApi.bundleUnavailable',
42 : busy: 'deployToComfyApi.bundleBusy',
43 : unavailable: 'deployToComfyApi.bundleUnavailable',
44 : invalid_request: 'deployToComfyApi.bundleRejected'
45 : } satisfies Record<BundleFailureReason, string>
46 :
47 1 : const REPORTED_REASONS: ReadonlySet<BundleFailureReason> = new Set([
48 : 'unavailable',
49 : 'invalid_request'
50 : ])
51 :
52 0 : export function useInstallationDeploy() {
53 0 : const authStore = useAuthStore()
54 0 : const apiKeyAuthStore = useApiKeyAuthStore()
55 0 : const workflowStore = useWorkflowStore()
56 0 : const { showSignInDialog } = useAuthDialogs()
57 0 : const toast = useToast()
58 0 : const state = ref<InstallationDeployState>({ status: 'idle' })
59 :
60 0 : function captureOpenWorkflow() {
61 0 : const workflow = workflowStore.activeWorkflow
62 0 : workflow?.changeTracker.prepareForSave()
63 0 : return workflow?.activeState
64 : }
65 :
66 0 : async function workspaceToken(): Promise<TokenLookup> {
67 0 : try {
68 0 : return { ok: true, token: await authStore.getWorkspaceAuthToken() }
69 : } catch (cause) {
70 0 : return { ok: false, cause }
71 : }
72 : }
73 :
74 0 : async function promptSignIn(): Promise<SignInPrompt> {
75 0 : try {
76 0 : return { ok: true, signedIn: await showSignInDialog() }
77 : } catch (cause) {
78 0 : return { ok: false, cause }
79 : }
80 : }
81 :
82 0 : async function deployCredential(): Promise<CredentialLookup> {
83 0 : const owner = authStore.currentUserIdentity()
84 0 : const workspace = await workspaceToken()
85 0 : if (!workspace.ok) {
86 0 : return { ok: false, reason: 'unavailable', cause: workspace.cause }
87 : }
88 0 : if (workspace.token) {
89 0 : return {
90 : ok: true,
91 : header: { Authorization: `Bearer ${workspace.token}` },
92 : owner
93 : }
94 : }
95 0 : if (authStore.isAuthenticated) return { ok: false, reason: 'unauthorized' }
96 0 : const apiKeyHeader = apiKeyAuthStore.getAuthHeader()
97 0 : return apiKeyHeader
98 : ? { ok: true, header: apiKeyHeader, owner }
99 : : { ok: false, reason: 'signed_out' }
100 : }
101 :
102 0 : async function signedInCredential(): Promise<CredentialLookup> {
103 0 : const credential = await deployCredential()
104 0 : if (credential.ok || credential.reason !== 'signed_out') return credential
105 0 : const prompt = await promptSignIn()
106 0 : if (!prompt.ok) {
107 0 : return { ok: false, reason: 'unavailable', cause: prompt.cause }
108 : }
109 0 : if (!prompt.signedIn) return credential
110 0 : const retried = await deployCredential()
111 0 : if (retried.ok || retried.reason !== 'signed_out') return retried
112 0 : return { ok: false, reason: 'unauthorized' }
113 : }
114 :
115 0 : function notifyFailure(reason: BundleFailureReason, cause: unknown) {
116 0 : if (REPORTED_REASONS.has(reason)) {
117 0 : reportError(cause ?? reason, {
118 : errorType: 'error_creating_installation_bundle',
119 : surface: 'platform',
120 : context: { reason }
121 : })
122 : }
123 0 : toast.error(t(FAILURE_MESSAGE_KEY[reason]))
124 : }
125 :
126 0 : function stopUploading() {
127 0 : state.value = { status: 'idle' }
128 : }
129 :
130 0 : function fail(failure: DeployFailure) {
131 0 : if (failure.reason !== 'signed_out') {
132 0 : notifyFailure(failure.reason, failure.cause)
133 : }
134 0 : stopUploading()
135 : }
136 :
137 0 : async function deploy() {
138 0 : const workflow = captureOpenWorkflow()
139 0 : state.value = { status: 'uploading' }
140 0 : const credential = await signedInCredential()
141 0 : if (!credential.ok) return fail(credential)
142 0 : const { owner } = credential
143 0 : if (owner !== authStore.currentUserIdentity()) return stopUploading()
144 0 : const bundle = await createInstallationBundle(credential.header, workflow)
145 0 : if (owner !== authStore.currentUserIdentity()) return stopUploading()
146 0 : if (!bundle.ok) return fail(bundle)
147 0 : state.value = { status: 'ready', url: bundle.url }
148 : }
149 :
150 0 : return { state, deploy }
151 : }
|