Line data Source code
1 : import { partition, uniq } from 'es-toolkit'
2 :
3 : import { isModelUrlAllowlisted } from '@/platform/workflowErrors/missingModel/modelUrlAllowlist'
4 : import type { WorkspaceDeployment } from '@/platform/workspace/api/workspaceApi'
5 : import {
6 : BUILD_RECIPE_SECTION,
7 : CUT_AND_REPORT_SECTION
8 : } from '@/platform/workspace/customDeploymentBrief'
9 : import { parseUrl } from '@/utils/urlSafety'
10 :
11 : /** Node types a deployment lacks, under their pack, or null if none is known. */
12 : export interface MissingBuildPack {
13 : packId: string | null
14 : nodeTypes: readonly string[]
15 : /** Set when the Build has the pack but its release cannot load it. */
16 : loadFailure?: { cause: string }
17 : }
18 :
19 : /** A model a deployment lacks, with its models/ folder and download link. */
20 : export interface MissingBuildModel {
21 : name: string
22 : directory: string | null
23 : url?: string
24 : }
25 :
26 2355 : const UNLISTED_MODELS = [
27 : 'The editor cannot see the models those custom nodes load, so they are not',
28 : "listed: find them in the workflow or the packs' docs, or ask the user."
29 : ]
30 :
31 0 : function inline(text: string): string {
32 0 : return text
33 : .replace(/[\p{Cf}\p{Default_Ignorable_Code_Point}]/gu, '')
34 : .replace(/[\p{Cc}\p{Zl}\p{Zp}`]/gu, ' ')
35 : }
36 :
37 0 : function packLine({ packId, nodeTypes }: MissingBuildPack): string {
38 0 : const nodes = uniq(nodeTypes)
39 : .map((type) => `\`${inline(type)}\``)
40 : .join(', ')
41 0 : return packId
42 : ? `- Pack \`${inline(packId)}\`: ${nodes}`
43 : : `- Pack unknown: ${nodes}`
44 : }
45 :
46 0 : function failingPackLine(pack: MissingBuildPack): string {
47 0 : const cause = pack.loadFailure?.cause
48 0 : return cause
49 : ? `${packLine(pack)} (fails with: ${inline(cause)})`
50 : : packLine(pack)
51 : }
52 :
53 0 : function downloadLink(url: string | undefined): string | undefined {
54 0 : if (!url || !isModelUrlAllowlisted(url)) return
55 0 : return parseUrl(url)?.href
56 : }
57 :
58 0 : function modelLine({ name, directory, url }: MissingBuildModel): string {
59 0 : const folder = directory ? ` in \`models/${inline(directory)}\`` : ''
60 0 : const link = downloadLink(url)
61 0 : const source = link ? `, from ${link}` : ''
62 0 : return `- \`${inline(name)}\`${folder}${source}`
63 : }
64 :
65 0 : function listSection(title: string, lines: readonly string[]): string[] {
66 0 : return lines.length > 0 ? [`## ${title}`, '', ...lines, ''] : []
67 : }
68 :
69 0 : function failingPackSection(packs: readonly MissingBuildPack[]): string[] {
70 0 : if (packs.length === 0) return []
71 0 : return [
72 : '## Packs the Build has but cannot load',
73 : '',
74 : 'The Build already has these packs, so do not add them again. Find out why',
75 : 'each one fails to load, and fix that.',
76 : '',
77 : ...packs.map(failingPackLine),
78 : ''
79 : ]
80 : }
81 :
82 0 : function missingSections(
83 : packs: readonly MissingBuildPack[],
84 : models: readonly MissingBuildModel[]
85 : ): string[] {
86 0 : const [failingPacks, absentPacks] = partition(
87 : packs,
88 : (pack) => pack.loadFailure !== undefined
89 : )
90 0 : return [
91 : ...listSection('Custom nodes', absentPacks.map(packLine)),
92 : ...failingPackSection(failingPacks),
93 : ...listSection('Models', models.map(modelLine))
94 : ]
95 : }
96 :
97 0 : function buildReference(deployment: WorkspaceDeployment): string {
98 0 : if (!deployment.build_id) return 'its Build'
99 : const name = deployment.build_name ?? deployment.build_id
100 0 : return `the Build **${name}** (\`${deployment.build_id}\`)`
101 : }
102 :
103 : /** Brief an agent follows to add what a deployment lacks to the Build it runs. */
104 0 : export function deploymentBuildBrief(
105 : deployment: WorkspaceDeployment,
106 : packs: readonly MissingBuildPack[],
107 : models: readonly MissingBuildModel[]
108 : ): string {
109 0 : const deploymentName = deployment.deployment_name ?? deployment.deployment_id
110 0 : return [
111 : '# Add custom nodes and models to a Comfy Cloud deployment',
112 : '',
113 : `The user's ComfyUI workflow runs on the deployment **${deploymentName}**`,
114 : `(\`${deployment.deployment_id}\`). It runs release \`${deployment.release_id}\` of`,
115 : `${buildReference(deployment)} on the Comfy developer platform. That release`,
116 : 'lacks these custom nodes and models the workflow uses:',
117 : '',
118 : ...missingSections(packs, models),
119 : BUILD_RECIPE_SECTION,
120 : '',
121 : '## Where the user decides',
122 : '',
123 : 'Find the pack for any node under an unknown pack, and a download link for',
124 : 'any model without one.',
125 : ...UNLISTED_MODELS,
126 : 'Show the user what you will change in the Build, and wait for a yes. Then',
127 : 'cut a new release of it, as the recipe says.',
128 : '',
129 : '## When you are done',
130 : '',
131 : 'Report the release id, and whether it is deployable. Ask the user before',
132 : `you deploy it to **${deploymentName}**.`
133 : ].join('\n')
134 : }
135 :
136 : /** Agent brief for a custom deployment that runs a workflow Cloud can't. */
137 0 : export function workflowDeploymentBrief(
138 : packs: readonly MissingBuildPack[],
139 : models: readonly MissingBuildModel[]
140 : ): string {
141 0 : return [
142 : '# Create a custom deployment that runs a workflow',
143 : '',
144 : 'The user opened a ComfyUI workflow on Comfy Cloud that uses custom nodes',
145 : 'and models Comfy Cloud does not have. Help the user create a **custom',
146 : 'deployment** on the Comfy developer platform that runs it. Once it is',
147 : 'deployed, it appears under **Run on** in the ComfyUI editor for everyone in',
148 : "the user's workspace.",
149 : '',
150 : ...missingSections(packs, models),
151 : BUILD_RECIPE_SECTION,
152 : '',
153 : '## Where the user decides',
154 : '',
155 : 'Find the pack for any node under an unknown pack, and a download link for',
156 : 'any model without one.',
157 : ...UNLISTED_MODELS,
158 : 'Ask the user which GPU the deployment should run on.',
159 : CUT_AND_REPORT_SECTION
160 : ].join('\n')
161 : }
|