Line data Source code
1 : import type { AxiosRequestConfig } from 'axios'
2 :
3 : import type {
4 : AcceptInviteResponse,
5 : CreateInviteRequest,
6 : CreateWorkspaceRequest,
7 : CurrentWorkspaceResponse,
8 : DeploymentCompatibility,
9 : DeploymentCompatibilityRequest,
10 : ListInvitesResponse,
11 : ListMembersResponse,
12 : ListWorkspacesResponse,
13 : Member as GeneratedMember,
14 : PendingInvite,
15 : PickWorkspaceDeploymentRequest,
16 : ReleaseUnsupportedPacks,
17 : StartWorkspaceDeploymentResult,
18 : UpdateWorkspaceRequest,
19 : WorkspaceDeployment,
20 : WorkspaceDeploymentCpuServer,
21 : WorkspaceDeploymentList,
22 : WorkspaceWithRole
23 : } from '@comfyorg/ingest-types'
24 : import {
25 : zDeploymentCompatibility,
26 : zReleaseUnsupportedPacks,
27 : zStartWorkspaceDeploymentResult,
28 : zWorkspaceDeploymentCpuServer
29 : } from '@comfyorg/ingest-types/zod'
30 : import {
31 : DEPLOYMENT_LISTING_TIMEOUT_MS,
32 : DEPLOYMENT_START_TIMEOUT_MS
33 : } from './deploymentListingTimeouts'
34 : import type {
35 : WorkspaceId,
36 : WorkspaceInviteId
37 : } from '@/platform/workspace/workspaceTypes'
38 : import type { UserId } from '@comfyorg/api-client/authTypes'
39 : import {
40 : ingestApiClient as workspaceApiClient,
41 : requestAuth,
42 : requireIngestApiAuth as requireWorkspaceApiAuth,
43 : handleAxiosError
44 : } from '@/platform/remote/ingest/ingestApi'
45 : import { ingestApiUrl as ingestApiUrl } from '@/platform/remote/ingest/ingestApiUrl'
46 : export type WorkspaceType = 'personal' | 'team'
47 : export type WorkspaceRole = 'owner' | 'member'
48 :
49 : export type Member = GeneratedMember & {
50 : credits_used_this_month?: number
51 : monthly_credit_limit?: number | null
52 : }
53 :
54 : export interface ListMembersParams {
55 : offset?: number
56 : limit?: number
57 : }
58 :
59 : export type { PendingInvite }
60 :
61 : export type { WorkspaceWithRole }
62 : export type { ListWorkspacesResponse }
63 : export type { CurrentWorkspaceResponse }
64 :
65 : export type { WorkspaceDeployment }
66 : export type { WorkspaceDeploymentList }
67 :
68 : export type { DeploymentCompatibility }
69 :
70 : export type { WorkspaceDeploymentCpuServer }
71 :
72 2356 : const CPU_SERVER_READ_TIMEOUT_MS = 15_000
73 :
74 : /** For callers that already retry on their own schedule. */
75 : interface ReadOptions {
76 : skipStatusRetry?: boolean
77 : }
78 :
79 18 : function withReadOptions(
80 : auth: AxiosRequestConfig,
81 : { skipStatusRetry }: ReadOptions
82 : ): AxiosRequestConfig {
83 18 : return skipStatusRetry ? { ...auth, __skipStatusRetry: true } : auth
84 : }
85 :
86 2356 : export const workspaceApi = {
87 : async list(options: ReadOptions = {}): Promise<ListWorkspacesResponse> {
88 31 : const auth = await requestAuth()
89 31 : try {
90 : const response = await workspaceApiClient.get<ListWorkspacesResponse>(
91 : ingestApiUrl('/workspaces'),
92 : withReadOptions(auth, options)
93 : )
94 19 : return response.data
95 : } catch (err) {
96 0 : handleAxiosError(err, 'list')
97 : }
98 : },
99 : async getCurrentWorkspace(
100 : options: ReadOptions = {}
101 : ): Promise<CurrentWorkspaceResponse> {
102 0 : const auth = await requestAuth()
103 0 : try {
104 : const response = await workspaceApiClient.get<CurrentWorkspaceResponse>(
105 : ingestApiUrl('/workspaces/current'),
106 : withReadOptions(auth, options)
107 : )
108 0 : return response.data
109 : } catch (err) {
110 0 : handleAxiosError(err, 'getCurrentWorkspace')
111 : }
112 : },
113 : async create(payload: CreateWorkspaceRequest): Promise<WorkspaceWithRole> {
114 0 : const auth = await requestAuth()
115 0 : try {
116 : const response = await workspaceApiClient.post<WorkspaceWithRole>(
117 : ingestApiUrl('/workspaces'),
118 : payload,
119 : auth
120 : )
121 0 : return response.data
122 : } catch (err) {
123 0 : handleAxiosError(err, 'create')
124 : }
125 : },
126 : async update(
127 : workspaceId: WorkspaceId,
128 : payload: UpdateWorkspaceRequest
129 : ): Promise<WorkspaceWithRole> {
130 0 : const auth = await requestAuth()
131 0 : try {
132 : const response = await workspaceApiClient.patch<WorkspaceWithRole>(
133 : ingestApiUrl(`/workspaces/${workspaceId}`),
134 : payload,
135 : auth
136 : )
137 0 : return response.data
138 : } catch (err) {
139 0 : handleAxiosError(err, 'update')
140 : }
141 : },
142 : async listDeployments(
143 : workspaceId: WorkspaceId,
144 : startedDeploymentIds: readonly string[]
145 : ): Promise<WorkspaceDeploymentList> {
146 0 : const auth = await requestAuth()
147 0 : try {
148 : const response = await workspaceApiClient.get<WorkspaceDeploymentList>(
149 : ingestApiUrl(`/workspaces/${workspaceId}/deployments`),
150 : {
151 : ...auth,
152 : params: { started_deployment_id: startedDeploymentIds },
153 : paramsSerializer: { indexes: null },
154 : timeout: DEPLOYMENT_LISTING_TIMEOUT_MS,
155 : __skipStatusRetry: true
156 : }
157 : )
158 0 : return response.data
159 : } catch (err) {
160 0 : handleAxiosError(err, 'listDeployments')
161 : }
162 : },
163 : async pickDeployment(
164 : workspaceId: WorkspaceId,
165 : payload: PickWorkspaceDeploymentRequest,
166 : signal?: AbortSignal
167 : ): Promise<void> {
168 0 : const auth = await requestAuth()
169 0 : signal?.throwIfAborted()
170 0 : try {
171 0 : await workspaceApiClient.put(
172 : ingestApiUrl(`/workspaces/${workspaceId}/deployment`),
173 : payload,
174 : { ...auth, ...(signal ? { signal } : {}) }
175 : )
176 : } catch (err) {
177 0 : handleAxiosError(err, 'pickDeployment')
178 : }
179 : },
180 : async startDeployment(
181 : workspaceId: WorkspaceId,
182 : deploymentId: string
183 : ): Promise<StartWorkspaceDeploymentResult> {
184 0 : const auth = await requestAuth()
185 0 : try {
186 0 : const response = await workspaceApiClient.post<unknown>(
187 : ingestApiUrl(
188 : `/workspaces/${workspaceId}/deployments/${encodeURIComponent(deploymentId)}/start`
189 : ),
190 : undefined,
191 : { ...auth, timeout: DEPLOYMENT_START_TIMEOUT_MS }
192 : )
193 0 : return zStartWorkspaceDeploymentResult.parse(response.data)
194 : } catch (err) {
195 0 : handleAxiosError(err, 'startDeployment')
196 : }
197 : },
198 : async clearDeployment(
199 : workspaceId: WorkspaceId,
200 : options: { follow?: 'workspace'; signal?: AbortSignal } = {}
201 : ): Promise<void> {
202 0 : const auth = await requestAuth()
203 0 : options.signal?.throwIfAborted()
204 0 : try {
205 0 : await workspaceApiClient.delete(
206 : ingestApiUrl(`/workspaces/${workspaceId}/deployment`),
207 : {
208 : ...auth,
209 : params: options.follow ? { follow: options.follow } : undefined,
210 : ...(options.signal ? { signal: options.signal } : {})
211 : }
212 : )
213 : } catch (err) {
214 0 : handleAxiosError(err, 'clearDeployment')
215 : }
216 : },
217 : async setDefaultDeployment(
218 : workspaceId: WorkspaceId,
219 : payload: PickWorkspaceDeploymentRequest
220 : ): Promise<void> {
221 0 : const auth = await requestAuth()
222 0 : try {
223 0 : await workspaceApiClient.put(
224 : ingestApiUrl(`/workspaces/${workspaceId}/default-deployment`),
225 : payload,
226 : auth
227 : )
228 : } catch (err) {
229 0 : handleAxiosError(err, 'setDefaultDeployment')
230 : }
231 : },
232 : async clearDefaultDeployment(workspaceId: WorkspaceId): Promise<void> {
233 0 : const auth = await requestAuth()
234 0 : try {
235 0 : await workspaceApiClient.delete(
236 : ingestApiUrl(`/workspaces/${workspaceId}/default-deployment`),
237 : auth
238 : )
239 : } catch (err) {
240 0 : handleAxiosError(err, 'clearDefaultDeployment')
241 : }
242 : },
243 : async getDeploymentCpuServer(
244 : workspaceId: WorkspaceId
245 : ): Promise<WorkspaceDeploymentCpuServer> {
246 0 : const auth = await requestAuth()
247 0 : try {
248 0 : const response = await workspaceApiClient.get<unknown>(
249 : ingestApiUrl(`/workspaces/${workspaceId}/deployment/cpu-server`),
250 : { ...auth, timeout: CPU_SERVER_READ_TIMEOUT_MS }
251 : )
252 0 : return zWorkspaceDeploymentCpuServer.parse(response.data)
253 : } catch (err) {
254 0 : handleAxiosError(err, 'getDeploymentCpuServer')
255 : }
256 : },
257 : async getReleaseUnsupportedPacks(): Promise<ReleaseUnsupportedPacks> {
258 0 : const auth = await requestAuth()
259 0 : try {
260 0 : const response = await workspaceApiClient.get<unknown>(
261 : ingestApiUrl('/release/unsupported-packs'),
262 : auth
263 : )
264 0 : return zReleaseUnsupportedPacks.parse(response.data)
265 : } catch (err) {
266 0 : handleAxiosError(err, 'getReleaseUnsupportedPacks')
267 : }
268 : },
269 : async checkDeploymentCompatibility(
270 : workspaceId: WorkspaceId,
271 : nodeTypes: string[]
272 : ): Promise<DeploymentCompatibility> {
273 0 : const auth = await requestAuth()
274 0 : try {
275 0 : const response = await workspaceApiClient.post<unknown>(
276 : ingestApiUrl(`/workspaces/${workspaceId}/deployments/compatibility`),
277 : { node_types: nodeTypes } satisfies DeploymentCompatibilityRequest,
278 : auth
279 : )
280 0 : return zDeploymentCompatibility.parse(response.data)
281 : } catch (err) {
282 0 : handleAxiosError(err, 'checkDeploymentCompatibility')
283 : }
284 : },
285 : async delete(workspaceId: WorkspaceId): Promise<void> {
286 0 : const auth = await requestAuth()
287 0 : try {
288 0 : await workspaceApiClient.delete(
289 : ingestApiUrl(`/workspaces/${workspaceId}`),
290 : auth
291 : )
292 : } catch (err) {
293 0 : handleAxiosError(err, 'delete')
294 : }
295 : },
296 : async leave(): Promise<void> {
297 0 : const auth = await requestAuth()
298 0 : try {
299 0 : await workspaceApiClient.post(
300 : ingestApiUrl('/workspace/leave'),
301 : null,
302 : auth
303 : )
304 : } catch (err) {
305 0 : handleAxiosError(err, 'leave')
306 : }
307 : },
308 : async listMembers(params?: ListMembersParams): Promise<ListMembersResponse> {
309 7 : const auth = await requestAuth()
310 7 : try {
311 : const response = await workspaceApiClient.get<ListMembersResponse>(
312 : ingestApiUrl('/workspace/members'),
313 : { ...auth, params }
314 : )
315 7 : return response.data
316 : } catch (err) {
317 0 : handleAxiosError(err, 'listMembers')
318 : }
319 : },
320 : async removeMember(userId: UserId): Promise<void> {
321 0 : const auth = await requestAuth()
322 0 : try {
323 0 : await workspaceApiClient.delete(
324 : ingestApiUrl(`/workspace/members/${userId}`),
325 : auth
326 : )
327 : } catch (err) {
328 0 : handleAxiosError(err, 'removeMember')
329 : }
330 : },
331 : async updateMemberRole(userId: UserId, role: WorkspaceRole): Promise<Member> {
332 0 : const auth = await requestAuth()
333 0 : try {
334 : const response = await workspaceApiClient.patch<Member>(
335 : ingestApiUrl(`/workspace/members/${userId}`),
336 : { role },
337 : auth
338 : )
339 0 : return response.data
340 : } catch (err) {
341 0 : handleAxiosError(err, 'updateMemberRole')
342 : }
343 : },
344 : async listInvites(): Promise<ListInvitesResponse> {
345 0 : const auth = await requestAuth()
346 0 : try {
347 : const response = await workspaceApiClient.get<ListInvitesResponse>(
348 : ingestApiUrl('/workspace/invites'),
349 : auth
350 : )
351 0 : return response.data
352 : } catch (err) {
353 0 : handleAxiosError(err, 'listInvites')
354 : }
355 : },
356 : async createInvite(payload: CreateInviteRequest): Promise<PendingInvite> {
357 0 : const auth = await requestAuth()
358 0 : try {
359 : const response = await workspaceApiClient.post<PendingInvite>(
360 : ingestApiUrl('/workspace/invites'),
361 : payload,
362 : auth
363 : )
364 0 : return response.data
365 : } catch (err) {
366 0 : handleAxiosError(err, 'createInvite')
367 : }
368 : },
369 : async revokeInvite(inviteId: WorkspaceInviteId): Promise<void> {
370 0 : const auth = await requestAuth()
371 0 : try {
372 0 : await workspaceApiClient.delete(
373 : ingestApiUrl(`/workspace/invites/${inviteId}`),
374 : auth
375 : )
376 : } catch (err) {
377 0 : handleAxiosError(err, 'revokeInvite')
378 : }
379 : },
380 : async resendInvite(inviteId: WorkspaceInviteId): Promise<PendingInvite> {
381 0 : const auth = await requestAuth()
382 0 : try {
383 : const response = await workspaceApiClient.post<PendingInvite>(
384 : ingestApiUrl(
385 : `/workspace/invites/${encodeURIComponent(inviteId)}/resend`
386 : ),
387 : null,
388 : auth
389 : )
390 0 : return response.data
391 : } catch (err) {
392 0 : handleAxiosError(err, 'resendInvite')
393 : }
394 : },
395 : async acceptInvite(token: string): Promise<AcceptInviteResponse> {
396 0 : const headers = await requireWorkspaceApiAuth().getFirebaseAuthHeader()
397 0 : try {
398 : const response = await workspaceApiClient.post<AcceptInviteResponse>(
399 : ingestApiUrl(`/invites/${token}/accept`),
400 : null,
401 : { headers, __skipUnifiedRemint: true }
402 : )
403 0 : return response.data
404 : } catch (err) {
405 0 : handleAxiosError(err, 'acceptInvite')
406 : }
407 : }
408 : }
|