import {z} from 'zod' export type MachineStats = {machine_name?: string | undefined, os_version?: string | undefined, gpu_type?: string | undefined, cpu_capacity?: string | undefined, initial_cpu?: string | undefined, memory_capacity?: string | undefined, initial_ram?: string | undefined, vram_time_series?: Record | undefined, disk_capacity?: string | undefined, initial_disk?: string | undefined, pip_freeze?: string | undefined}; export type WorkflowRunStatus = 'WorkflowRunStatusStarted' | 'WorkflowRunStatusFailed' | 'WorkflowRunStatusCompleted'; export type CreateApiUploadArtifactArgs = {body: {repo: string, job_id: string, run_id: string, os: string, cuda_version?: string | undefined, bucket_name?: string | undefined, output_files_gcs_paths?: string | undefined, comfy_logs_gcs_path?: string | undefined, comfy_run_flags?: string | undefined, commit_hash: string, commit_time: string, commit_message: string, workflow_name: string, branch_name: string, start_time: number, end_time: number, avg_vram?: number | undefined, peak_vram?: number | undefined, pr_number: string, author: string, job_trigger_user: string, python_version: string, pytorch_version?: string | undefined, machine_stats?: MachineStats | undefined, status: WorkflowRunStatus}}; export type CreateApiUploadArtifactOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiUploadArtifactResponse = z.object({message: z.string().optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Receive artifacts (output files) from the ComfyUI GitHub Action * * POST /upload-artifact */ export const createApiUploadArtifact = async (args: CreateApiUploadArtifactArgs, options?: CreateApiUploadArtifactOptions) => { const {baseUrl = "https://api.comfy.org", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/upload-artifact${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiUploadArtifactResponse, await res.json()) };