Line data Source code
1 : import type { ResultItem } from '@comfyorg/api-client/executionTypes'
2 : import type { ResultItemType } from '@comfyorg/api-client/resultItemTypeSchema'
3 :
4 2356 : const IMPLICIT_ASSET_ROOT = 'input'
5 2356 : const ANNOTATION_SUFFIX = /\s*\[(input|output|temp)\]\s*$/i
6 :
7 2356 : const hasAnnotation = (filepath: string): boolean =>
8 : ANNOTATION_SUFFIX.test(filepath)
9 :
10 2356 : const createAnnotation = (
11 : filepath: string,
12 : rootFolder = IMPLICIT_ASSET_ROOT
13 : ): string =>
14 : !hasAnnotation(filepath) && rootFolder !== IMPLICIT_ASSET_ROOT
15 : ? ` [${rootFolder}]`
16 : : ''
17 :
18 2356 : const createPath = (filename: string, subfolder = ''): string =>
19 : subfolder ? `${subfolder}/${filename}` : filename
20 :
21 : type AnnotatedPathOptions = {
22 : rootFolder?: ResultItemType
23 : subfolder?: string
24 : }
25 :
26 : export function parseAnnotatedPath<Fallback extends string = 'input'>(
27 : filepath: string,
28 : fallbackRoot?: Fallback
29 : ): { filepath: string; rootFolder: ResultItemType | Fallback }
30 1900 : export function parseAnnotatedPath(
31 : filepath: string,
32 : fallbackRoot: string = IMPLICIT_ASSET_ROOT
33 : ): { filepath: string; rootFolder: string } {
34 1900 : const match = ANNOTATION_SUFFIX.exec(filepath)
35 1900 : if (!match) return { filepath, rootFolder: fallbackRoot }
36 :
37 193 : const annotation = match[1].toLowerCase()
38 1900 : return {
39 : filepath: filepath.slice(0, match.index),
40 : rootFolder:
41 : annotation === 'output' || annotation === 'temp'
42 : ? annotation
43 : : IMPLICIT_ASSET_ROOT
44 : }
45 : }
46 :
47 : /** Creates annotated filepath in format used by folder_paths.py */
48 : export function createAnnotatedPath(item: ResultItem): string
49 : export function createAnnotatedPath(
50 : item: string,
51 : options?: AnnotatedPathOptions
52 : ): string
53 1949 : export function createAnnotatedPath(
54 : item: string | ResultItem,
55 : options: AnnotatedPathOptions = {}
56 : ): string {
57 1949 : const { rootFolder = IMPLICIT_ASSET_ROOT, subfolder } = options
58 1949 : if (typeof item === 'string')
59 1943 : return `${createPath(item, subfolder)}${createAnnotation(item, rootFolder)}`
60 1949 : const filename = item.filename ?? ''
61 1949 : return `${createPath(filename, item.subfolder)}${createAnnotation(
62 : filename,
63 : item.type
64 : )}`
65 : }
|