Line data Source code
1 : import type { InjectionKey, Ref } from 'vue'
2 : import { z } from 'zod'
3 :
4 : import { zResultItem } from '@comfyorg/api-client/executionTypes'
5 :
6 : import { assetItemSchema } from './assetSchema'
7 :
8 2356 : export const zMediaKindSchema = z.enum([
9 : 'video',
10 : 'audio',
11 : 'image',
12 : '3D',
13 : 'text',
14 : 'other'
15 : ])
16 : export type MediaKind = z.infer<typeof zMediaKindSchema>
17 :
18 2356 : const zDimensionsSchema = z.object({
19 : width: z.number().positive(),
20 : height: z.number().positive()
21 : })
22 :
23 : // Extend the base asset schema with media-specific fields
24 2356 : const zMediaAssetDisplayItemSchema = assetItemSchema.extend({
25 : // New required fields
26 : kind: zMediaKindSchema,
27 : src: z.url(),
28 :
29 : // New optional fields
30 : duration: z.number().nonnegative().optional(),
31 : dimensions: zDimensionsSchema.optional()
32 : })
33 :
34 : // Asset context schema
35 2356 : const zAssetContextSchema = z.object({
36 : type: z.enum(['input', 'output']),
37 : outputCount: z.number().positive().optional() // Only for output context
38 : })
39 :
40 : // Export the inferred types
41 : export type AssetMeta = z.infer<typeof zMediaAssetDisplayItemSchema>
42 : export type AssetContext = z.infer<typeof zAssetContextSchema>
43 :
44 : // Injection key for MediaAsset provide/inject pattern
45 : interface MediaAssetProviderValue {
46 : asset: Ref<AssetMeta | undefined>
47 : context: Ref<AssetContext>
48 : }
49 :
50 2356 : export const MediaAssetKey: InjectionKey<MediaAssetProviderValue> =
51 : Symbol('mediaAsset')
52 :
53 2356 : export const MIME_ASSET_INFO = 'application/x-comfy-asset-info'
54 :
55 2356 : const zDraggedAssetInfo = zResultItem.and(
56 : z.object({
57 : attachment_ref: z.string().min(1).optional(),
58 : media_kind: zMediaKindSchema.optional(),
59 : preview_url: z.url().optional(),
60 : media_url: z.url().optional()
61 : })
62 : )
63 :
64 6 : export function parseAssetInfo(dataTransfer: DataTransfer) {
65 6 : const assetString = dataTransfer.getData(MIME_ASSET_INFO)
66 6 : try {
67 6 : return zDraggedAssetInfo.safeParse(JSON.parse(assetString)).data
68 : } catch {
69 : // output was not parsable, allow fallthrough and return undefined
70 : }
71 : }
|