Line data Source code
1 : import {
2 : zAsset as zIngestAsset,
3 : zListAssetsResponse
4 : } from '@comfyorg/ingest-types/zod'
5 : import { z } from 'zod'
6 :
7 : // Zod schemas for asset API validation matching ComfyUI Assets REST API spec
8 2356 : const zAsset = z.object({
9 : ...zIngestAsset.shape,
10 : created_at: z.iso.datetime({ local: true }),
11 : hash: z.string().optional(),
12 : id: z.string(),
13 : last_access_time: z.iso.datetime({ local: true }).optional(),
14 : preview_url: z.string().optional(),
15 : size: zIngestAsset.shape.size.unwrap().transform(Number).optional(),
16 : tags: zIngestAsset.shape.tags.default([]),
17 : thumbnail_url: z.string().optional(),
18 : updated_at: z.iso.datetime({ local: true }),
19 : // The API sends `null` for unset metadata, which the generated `.optional()`
20 : // shape rejects outright; normalize it to the one absent shape consumers know.
21 : user_metadata: zIngestAsset.shape.user_metadata
22 : .nullable()
23 : .transform((value) => value ?? undefined)
24 : .optional()
25 : })
26 :
27 2356 : const zAssetResponse = zListAssetsResponse
28 : .pick({ total: true, has_more: true, next_cursor: true })
29 : .extend({
30 : assets: z.array(zAsset)
31 : })
32 :
33 : // Zod schema for ModelFile to align with interface
34 2356 : const zModelFile = z.object({
35 : name: z.string(),
36 : pathIndex: z.number()
37 : })
38 :
39 2356 : const zValidationError = z.object({
40 : code: z.string(),
41 : message: z.string(),
42 : field: z.string()
43 : })
44 :
45 2356 : const zValidationResult = z.object({
46 : is_valid: z.boolean(),
47 : errors: z.array(zValidationError).optional(),
48 : warnings: z.array(zValidationError).optional()
49 : })
50 :
51 2356 : const zAssetMetadata = z.object({
52 : content_length: z.number(),
53 : final_url: z.string(),
54 : content_type: z.string().optional(),
55 : filename: z.string().optional(),
56 : name: z.string().optional(),
57 : tags: z.array(z.string()).optional(),
58 : preview_url: z.string().optional(),
59 : preview_image: z.string().optional(),
60 : validation: zValidationResult.optional()
61 : })
62 :
63 2356 : const zAsyncUploadTask = z.object({
64 : task_id: z.string(),
65 : status: z.enum(['created', 'running', 'completed', 'failed']),
66 : message: z.string().optional()
67 : })
68 :
69 2356 : const zAsyncUploadResponse = z.discriminatedUnion('type', [
70 : z.object({ type: z.literal('sync'), asset: zAsset }),
71 : z.object({ type: z.literal('async'), task: zAsyncUploadTask })
72 : ])
73 :
74 : // Filename validation schema. Trim runs FIRST so the anchored checks see the
75 : // trimmed value — trailing-position trim let ' /etc/passwd' bypass the
76 : // absolute-path block.
77 2356 : export const assetFilenameSchema = z
78 : .string()
79 : .trim()
80 : .min(1, 'Filename cannot be empty')
81 : .regex(/^[^\\:*?"<>|]+$/, 'Invalid filename characters') // Allow forward slashes, block backslashes and other unsafe chars
82 : .regex(/^(?!\/)/, 'Path must not be absolute')
83 : // Traversal check is segment-aware: `..` as a whole path segment is
84 : // blocked, while double dots inside a filename (`flux..v2.safetensors`)
85 : // stay valid.
86 : .refine(
87 : (value) => !value.split('/').includes('..'),
88 : 'Path must not contain ".." segments'
89 : )
90 :
91 : // Export schemas following repository patterns
92 2356 : export const assetItemSchema = zAsset
93 2356 : export const assetResponseSchema = zAssetResponse
94 2356 : export const asyncUploadResponseSchema = zAsyncUploadResponse
95 :
96 : /**
97 : * Identifier for a single asset record.
98 : *
99 : * Backed by `AssetItem.id` which the API serialises as a string. This alias
100 : * names that primitive at use sites (services, stores, composables) without
101 : * changing structural typing.
102 : */
103 : export type AssetId = string
104 :
105 : // Export types derived from Zod schemas
106 : export type AssetItem = z.infer<typeof zAsset>
107 : export type ModelBrowserAsset = AssetItem & {
108 : isWorkspaceLibrary?: boolean
109 : browserCategories?: string[]
110 : }
111 : export type AssetResponse = z.infer<typeof zAssetResponse>
112 : export type AssetMetadata = z.infer<typeof zAssetMetadata>
113 : export type AsyncUploadResponse = z.infer<typeof zAsyncUploadResponse>
114 : export type ModelFile = z.infer<typeof zModelFile>
115 :
116 : /** Payload for updating an asset via PUT /assets/:id */
117 : export type AssetUpdatePayload = Partial<
118 : Pick<AssetItem, 'name' | 'tags' | 'user_metadata' | 'preview_id'>
119 : >
120 :
121 : /** User-editable metadata fields for model assets */
122 2356 : const zAssetUserMetadata = z.object({
123 : name: z.string().optional(),
124 : base_model: z.array(z.string()).optional(),
125 : additional_tags: z.array(z.string()).optional(),
126 : user_description: z.string().optional()
127 : })
128 :
129 : export type AssetUserMetadata = z.infer<typeof zAssetUserMetadata>
130 :
131 2356 : export const tagsOperationResultSchema = z.object({
132 : total_tags: z.array(z.string()),
133 : added: z.array(z.string()).optional(),
134 : removed: z.array(z.string()).optional(),
135 : already_present: z.array(z.string()).optional(),
136 : not_present: z.array(z.string()).optional()
137 : })
138 :
139 : export type TagsOperationResult = z.infer<typeof tagsOperationResultSchema>
|