dsh-sendcomfy 0.0.0-stage → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CONTRACT.md +586 -0
- package/FEATURES.zh.md +244 -0
- package/LICENSE +21 -0
- package/README.md +137 -2
- package/bin/sendcomfy.mjs +74 -0
- package/client.js +2851 -0
- package/cordis.patch.yml +6 -0
- package/icon.svg +22 -0
- package/index.js +449 -0
- package/lib/actions.mjs +1193 -0
- package/lib/capabilities.mjs +476 -0
- package/lib/comfy.mjs +968 -0
- package/lib/http.mjs +302 -0
- package/lib/state.mjs +384 -0
- package/lib/workflow.mjs +1572 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +66 -4
package/cordis.patch.yml
ADDED
package/icon.svg
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64" width="64" height="64" role="img" aria-label="ComfyUI bridge">
|
|
2
|
+
<defs>
|
|
3
|
+
<linearGradient id="bg" x1="0" y1="0" x2="1" y2="1">
|
|
4
|
+
<stop offset="0" stop-color="#3b2f6b"/>
|
|
5
|
+
<stop offset="1" stop-color="#16213e"/>
|
|
6
|
+
</linearGradient>
|
|
7
|
+
<linearGradient id="spark" x1="0" y1="0" x2="1" y2="1">
|
|
8
|
+
<stop offset="0" stop-color="#ffd36e"/>
|
|
9
|
+
<stop offset="1" stop-color="#ff7a59"/>
|
|
10
|
+
</linearGradient>
|
|
11
|
+
</defs>
|
|
12
|
+
<rect x="2" y="2" width="60" height="60" rx="16" fill="url(#bg)"/>
|
|
13
|
+
<g fill="none" stroke="#8ec5ff" stroke-width="3" stroke-linecap="round">
|
|
14
|
+
<path d="M20 22h10M20 42h10M38 22v20"/>
|
|
15
|
+
<path d="M38 32h6"/>
|
|
16
|
+
</g>
|
|
17
|
+
<circle cx="20" cy="22" r="5" fill="#8ec5ff"/>
|
|
18
|
+
<circle cx="20" cy="42" r="5" fill="#8ec5ff"/>
|
|
19
|
+
<circle cx="38" cy="22" r="5" fill="#8ec5ff"/>
|
|
20
|
+
<circle cx="44" cy="32" r="4" fill="#8ec5ff"/>
|
|
21
|
+
<path d="M46 14l2.2 5.3L54 21.5l-5.8 2.2L46 29l-2.2-5.3L38 21.5l5.8-2.2z" fill="url(#spark)"/>
|
|
22
|
+
</svg>
|
package/index.js
ADDED
|
@@ -0,0 +1,449 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-sendcomfy —— Host 半端(Cordis 插件入口)
|
|
3
|
+
* ============================================================================
|
|
4
|
+
* 挂载两件事:
|
|
5
|
+
* 1. /api/sendcomfy/* —— 网页面板(client.js)的同源代理路由,浏览器不直连 ComfyUI;
|
|
6
|
+
* 2. comfy_* 工具 —— 会话里的文本模型拿它调用 ComfyUI 的模型/工作流:
|
|
7
|
+
* 填提示词、覆盖节点参数、等结果/查结果、导入工作流。
|
|
8
|
+
*
|
|
9
|
+
* 业务逻辑全在 lib/actions.mjs(网页与工具共用一份实现),本文件只做 Host 装配。
|
|
10
|
+
*/
|
|
11
|
+
import { readFile, stat } from 'node:fs/promises'
|
|
12
|
+
import { isAbsolute, join, resolve } from 'node:path'
|
|
13
|
+
|
|
14
|
+
import { createActions, humanizeError } from './lib/actions.mjs'
|
|
15
|
+
import { createApiHandler } from './lib/http.mjs'
|
|
16
|
+
import { createStateStore } from './lib/state.mjs'
|
|
17
|
+
|
|
18
|
+
export const name = 'dsh-sendcomfy'
|
|
19
|
+
export const inject = ['webServer', 'tools']
|
|
20
|
+
|
|
21
|
+
const ROUTE_PREFIX = '/api/sendcomfy'
|
|
22
|
+
const MAX_IMPORT_BYTES = 8 * 1024 * 1024
|
|
23
|
+
|
|
24
|
+
/* ───────────────────────── 工具定义辅助 ───────────────────────── */
|
|
25
|
+
|
|
26
|
+
/** 工具统一以 JSON 文本渲染结果(结果本身必须是无损 JSON)。 */
|
|
27
|
+
const textOutput = {
|
|
28
|
+
schema: { type: 'object', properties: {}, additionalProperties: true },
|
|
29
|
+
render(_args, value) {
|
|
30
|
+
return [{ type: 'text', text: typeof value === 'string' ? value : JSON.stringify(value, null, 2) }]
|
|
31
|
+
},
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function jsonSafe(value) {
|
|
35
|
+
if (value === undefined || value === null) return value
|
|
36
|
+
try {
|
|
37
|
+
return JSON.parse(JSON.stringify(value))
|
|
38
|
+
} catch {
|
|
39
|
+
return { ok: false, error: '工具结果无法序列化为 JSON' }
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function objectSchema(properties = {}, required = []) {
|
|
44
|
+
return { type: 'object', properties, required, additionalProperties: false }
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const stringParam = (description) => ({ type: 'string', description })
|
|
48
|
+
const numberParam = (description) => ({ type: 'number', description })
|
|
49
|
+
const booleanParam = (description) => ({ type: 'boolean', description })
|
|
50
|
+
/** 参数覆盖表:键是 `节点id.输入名`,值是标量。 */
|
|
51
|
+
const overridesParam = (description) => ({ type: 'object', description, additionalProperties: true })
|
|
52
|
+
|
|
53
|
+
/* ───────────────────────── Host 插件装配 ───────────────────────── */
|
|
54
|
+
|
|
55
|
+
export function apply(ctx) {
|
|
56
|
+
const logger = (...args) => {
|
|
57
|
+
try {
|
|
58
|
+
const sink = ctx.logger
|
|
59
|
+
if (sink && typeof sink.info === 'function') sink.info('[dsh-sendcomfy]', ...args)
|
|
60
|
+
} catch {
|
|
61
|
+
/* 日志不可用不影响功能 */
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const store = createStateStore()
|
|
66
|
+
const actions = createActions({ store, logger })
|
|
67
|
+
|
|
68
|
+
/* ── 1. 浏览器面板 API ─────────────────────────────────────── */
|
|
69
|
+
const handler = createApiHandler(actions, { prefix: ROUTE_PREFIX })
|
|
70
|
+
ctx.effect(
|
|
71
|
+
() => ctx.webServer.register({
|
|
72
|
+
kind: 'prefix',
|
|
73
|
+
path: ROUTE_PREFIX,
|
|
74
|
+
handler: async (req, res) => {
|
|
75
|
+
try {
|
|
76
|
+
await handler(req, res)
|
|
77
|
+
} catch (error) {
|
|
78
|
+
try {
|
|
79
|
+
res.writeHead(500, { 'Content-Type': 'application/json; charset=utf-8' })
|
|
80
|
+
res.end(JSON.stringify({ ok: false, error: humanizeError(error) }))
|
|
81
|
+
} catch {
|
|
82
|
+
/* 响应已结束 */
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
}),
|
|
87
|
+
'dsh-sendcomfy: /api/sendcomfy 路由',
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
/* ── 2. 会话工具 ───────────────────────────────────────────── */
|
|
91
|
+
|
|
92
|
+
/** 当前会话的工作目录(拿不到就返回 undefined,调用方降级为不落盘)。 */
|
|
93
|
+
async function workspaceOf(agent) {
|
|
94
|
+
try {
|
|
95
|
+
const service = typeof ctx.get === 'function' ? ctx.get('workingDirectory') : undefined
|
|
96
|
+
if (service && typeof service.ensure === 'function' && agent) {
|
|
97
|
+
return await service.ensure(agent)
|
|
98
|
+
}
|
|
99
|
+
} catch (error) {
|
|
100
|
+
logger('拿工作目录失败:', error?.message || error)
|
|
101
|
+
}
|
|
102
|
+
return undefined
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** 工具入参里的相对路径按会话工作目录解析。 */
|
|
106
|
+
async function resolveInputPath(agent, inputPath) {
|
|
107
|
+
const raw = String(inputPath || '').trim()
|
|
108
|
+
if (!raw) throw new Error('需要文件路径')
|
|
109
|
+
if (isAbsolute(raw)) return raw
|
|
110
|
+
const base = await workspaceOf(agent)
|
|
111
|
+
if (!base) throw new Error(`无法确定工作区目录,请给绝对路径:${raw}`)
|
|
112
|
+
return resolve(base, raw)
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** 运行输出落盘目录:<工作区>/comfy-output(actions 会再按 promptId 分子目录)。 */
|
|
116
|
+
async function outputDirOf(agent, saveOutputs) {
|
|
117
|
+
if (saveOutputs === false) return undefined
|
|
118
|
+
const base = await workspaceOf(agent)
|
|
119
|
+
return base ? join(base, 'comfy-output') : undefined
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** 把一条 warning 追加进动作结果(失败结果没有 warnings 也补上)。 */
|
|
123
|
+
function appendWarning(result, message) {
|
|
124
|
+
if (!result || typeof result !== 'object') return result
|
|
125
|
+
return {
|
|
126
|
+
...result,
|
|
127
|
+
warnings: [...(Array.isArray(result.warnings) ? result.warnings : []), message],
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const definitions = [
|
|
132
|
+
{
|
|
133
|
+
name: 'comfy_status',
|
|
134
|
+
description:
|
|
135
|
+
'看 ComfyUI 桥接的当前配置与连通性:ComfyUI 地址、是否连得上、模型分类计数、已保存的工作流清单、最近运行。'
|
|
136
|
+
+ '第一次用或运行失败后先调它。ComfyUI 地址由用户在侧边栏「ComfyUI 桥接」面板里配置。',
|
|
137
|
+
parameters: objectSchema(),
|
|
138
|
+
async execute() {
|
|
139
|
+
const state = await actions.getState()
|
|
140
|
+
const live = await actions.test({})
|
|
141
|
+
return jsonSafe({
|
|
142
|
+
...state,
|
|
143
|
+
live: {
|
|
144
|
+
reachable: live.reachable === true,
|
|
145
|
+
latencyMs: live.latencyMs,
|
|
146
|
+
comfyuiVersion: live.comfyuiVersion,
|
|
147
|
+
device: live.device,
|
|
148
|
+
error: live.error,
|
|
149
|
+
},
|
|
150
|
+
})
|
|
151
|
+
},
|
|
152
|
+
},
|
|
153
|
+
|
|
154
|
+
{
|
|
155
|
+
name: 'comfy_models',
|
|
156
|
+
description:
|
|
157
|
+
'列出 ComfyUI 里可用的模型文件(checkpoints / LoRAs / VAE / CLIP / UNET / ControlNet / Upscale 等),'
|
|
158
|
+
+ '用来选风格模型或确认某个工作流需要的模型在不在。模型名也可以直接作为 comfy_run 的 params 值写进工作流。',
|
|
159
|
+
parameters: objectSchema({
|
|
160
|
+
category: stringParam('只看某一类,如 checkpoints / loras / vae;省略=全部类别'),
|
|
161
|
+
keyword: stringParam('按文件名过滤的关键词(不区分大小写),如 sdxl、qwen'),
|
|
162
|
+
refresh: booleanParam('true=绕过 60 秒缓存,重新从 ComfyUI 拉取'),
|
|
163
|
+
}),
|
|
164
|
+
async execute(args) {
|
|
165
|
+
const result = await actions.listModels({ refresh: args?.refresh === true })
|
|
166
|
+
if (!result.ok) return jsonSafe(result)
|
|
167
|
+
const category = String(args?.category || '').toLowerCase()
|
|
168
|
+
const keyword = String(args?.keyword || '').toLowerCase()
|
|
169
|
+
let total = 0
|
|
170
|
+
const categories = []
|
|
171
|
+
for (const entry of result.categories || []) {
|
|
172
|
+
if (category && String(entry.id).toLowerCase() !== category && String(entry.label).toLowerCase() !== category) continue
|
|
173
|
+
const files = keyword
|
|
174
|
+
? (entry.files || []).filter((file) => String(file).toLowerCase().includes(keyword))
|
|
175
|
+
: (entry.files || [])
|
|
176
|
+
total += files.length
|
|
177
|
+
const limit = 200
|
|
178
|
+
categories.push({
|
|
179
|
+
id: entry.id,
|
|
180
|
+
label: entry.label,
|
|
181
|
+
count: files.length,
|
|
182
|
+
files: files.slice(0, limit),
|
|
183
|
+
...(files.length > limit ? { truncated: true } : {}),
|
|
184
|
+
})
|
|
185
|
+
}
|
|
186
|
+
return jsonSafe({ ok: true, baseUrl: result.baseUrl, total, categories })
|
|
187
|
+
},
|
|
188
|
+
},
|
|
189
|
+
|
|
190
|
+
{
|
|
191
|
+
name: 'comfy_workflows',
|
|
192
|
+
description:
|
|
193
|
+
'查看 ComfyUI 服务器上有哪些工作流(主用法,**不需要导入**)。返回每个工作流的 path / 名称 / 大小 / 修改时间。'
|
|
194
|
+
+ '用 comfy_workflow 传 path 看可调参数与提示词目标,再 comfy_run({workflow:"<path>"}) 直接运行'
|
|
195
|
+
+ '(运行前插件会自动从 ComfyUI 加载该工作流)。本地已保存的旧工作流也会一并列出,仅作兼容。',
|
|
196
|
+
parameters: objectSchema(),
|
|
197
|
+
async execute() {
|
|
198
|
+
const server = await actions.listServerWorkflows()
|
|
199
|
+
const local = await actions.listWorkflows()
|
|
200
|
+
return jsonSafe({
|
|
201
|
+
ok: true,
|
|
202
|
+
baseUrl: server.baseUrl || undefined,
|
|
203
|
+
serverReachable: server.ok === true,
|
|
204
|
+
serverWorkflows: server.ok ? server.items || [] : [],
|
|
205
|
+
...(server.ok ? {} : { serverError: server.error }),
|
|
206
|
+
localWorkflows: (local && local.workflows) || [],
|
|
207
|
+
hint: 'serverWorkflows[].path 可直接传给 comfy_run / comfy_workflow;不需要先导入。',
|
|
208
|
+
})
|
|
209
|
+
},
|
|
210
|
+
},
|
|
211
|
+
|
|
212
|
+
{
|
|
213
|
+
name: 'comfy_workflow',
|
|
214
|
+
description:
|
|
215
|
+
'看一个工作流的详情:可调参数清单(path 形如 `节点id.输入名`、类型、当前值、枚举 options、min/max/step)、'
|
|
216
|
+
+ '提示词目标(positive/negative 参数 path)和节点摘要。workflow 可填 comfy_workflows 返回的服务器 path'
|
|
217
|
+
+ '(如 "krea2.json",会按需从 ComfyUI 加载,不需要导入),也可填本地保存的 id / 名称。',
|
|
218
|
+
parameters: objectSchema({ workflow: stringParam('服务器工作流 path(如 krea2.json)或本地工作流 id/名称') }, ['workflow']),
|
|
219
|
+
async execute(args) {
|
|
220
|
+
return jsonSafe(await actions.getWorkflow(String(args?.workflow || '')))
|
|
221
|
+
},
|
|
222
|
+
},
|
|
223
|
+
|
|
224
|
+
{
|
|
225
|
+
name: 'comfy_run',
|
|
226
|
+
description:
|
|
227
|
+
'用 ComfyUI 工作流出图。workflow 可以直接填服务器 path(comfy_workflows 返回的 path,运行前自动从 ComfyUI 加载,'
|
|
228
|
+
+ '**不需要先导入**),也可以填本地保存的 id/名称。prompt 会自动写进检测到的正向提示词节点(negativePrompt 写负向);'
|
|
229
|
+
+ 'params 可按 `节点id.输入名` 覆盖任意可调参数(先用 comfy_workflow 拿 path)。默认等出图结束并返回图片路径;'
|
|
230
|
+
+ '长任务可传 wait=false 立刻拿 promptId,再用 comfy_result 查结果。',
|
|
231
|
+
parameters: objectSchema({
|
|
232
|
+
workflow: stringParam('工作流 id 或名称(先用 comfy_workflows 查)'),
|
|
233
|
+
prompt: stringParam('正向提示词;省略=沿用工作流里原来的提示词'),
|
|
234
|
+
negativePrompt: stringParam('负向提示词;省略=沿用原值'),
|
|
235
|
+
params: overridesParam('参数覆盖表:键为 comfy_workflow 返回的 path(如 "6.steps"),值为数字/字符串/布尔。不要在键里加引号。'),
|
|
236
|
+
wait: booleanParam('是否等出图结束(默认 true)。长任务设 false,然后用 comfy_result 轮询'),
|
|
237
|
+
timeoutSec: numberParam('本次最多等多少秒(默认用面板里配置的超时,通常 300)'),
|
|
238
|
+
saveOutputs: booleanParam('是否把输出图片下载到当前会话工作区的 comfy-output/ 下(默认 true;false 只返回代理 URL)'),
|
|
239
|
+
}, ['workflow']),
|
|
240
|
+
// 宿主 tool-call-timeout-policy 会按 timeoutMs 强杀;这里留出 3600s 可配等待 + 余量。
|
|
241
|
+
timeoutMs: 3_700_000,
|
|
242
|
+
async execute(args, exec) {
|
|
243
|
+
const wantSave = args?.saveOutputs !== false
|
|
244
|
+
const saveDir = wantSave ? await outputDirOf(exec?.agent, true) : undefined
|
|
245
|
+
const result = await actions.run({
|
|
246
|
+
workflow: String(args?.workflow || ''),
|
|
247
|
+
prompt: args?.prompt,
|
|
248
|
+
negativePrompt: args?.negativePrompt,
|
|
249
|
+
params: args?.params,
|
|
250
|
+
wait: args?.wait === false ? false : true,
|
|
251
|
+
timeoutSec: args?.timeoutSec,
|
|
252
|
+
saveDir,
|
|
253
|
+
signal: exec?.signal,
|
|
254
|
+
})
|
|
255
|
+
return jsonSafe(wantSave && !saveDir
|
|
256
|
+
? appendWarning(result, '没拿到会话工作目录,输出图片只返回代理 URL(未落盘到工作区)。')
|
|
257
|
+
: result)
|
|
258
|
+
},
|
|
259
|
+
},
|
|
260
|
+
|
|
261
|
+
{
|
|
262
|
+
name: 'comfy_result',
|
|
263
|
+
description: '按 promptId 查一次 ComfyUI 任务的最终结果(图片路径/URL/状态)。配合 comfy_run 的 wait=false 使用。',
|
|
264
|
+
parameters: objectSchema({
|
|
265
|
+
promptId: stringParam('comfy_run 返回的 promptId'),
|
|
266
|
+
timeoutSec: numberParam('本次最多等多少秒(默认用面板里配置的超时)'),
|
|
267
|
+
saveOutputs: booleanParam('是否把输出图片下载到当前会话工作区(默认 true)'),
|
|
268
|
+
}, ['promptId']),
|
|
269
|
+
timeoutMs: 3_700_000,
|
|
270
|
+
async execute(args, exec) {
|
|
271
|
+
const wantSave = args?.saveOutputs !== false
|
|
272
|
+
const saveDir = wantSave ? await outputDirOf(exec?.agent, true) : undefined
|
|
273
|
+
const result = await actions.pollResult({
|
|
274
|
+
promptId: String(args?.promptId || ''),
|
|
275
|
+
timeoutSec: args?.timeoutSec,
|
|
276
|
+
saveDir,
|
|
277
|
+
signal: exec?.signal,
|
|
278
|
+
})
|
|
279
|
+
return jsonSafe(wantSave && !saveDir
|
|
280
|
+
? appendWarning(result, '没拿到会话工作目录,输出图片只返回代理 URL(未落盘到工作区)。')
|
|
281
|
+
: result)
|
|
282
|
+
},
|
|
283
|
+
},
|
|
284
|
+
|
|
285
|
+
{
|
|
286
|
+
name: 'comfy_cancel',
|
|
287
|
+
description: '中断 ComfyUI 当前运行的任务(给了 promptId 就中断/删除那一条)。不影响已完成的图片。',
|
|
288
|
+
parameters: objectSchema({
|
|
289
|
+
promptId: stringParam('要取消的 promptId;省略=中断当前正在跑的任务并清空排队'),
|
|
290
|
+
}),
|
|
291
|
+
async execute(args) {
|
|
292
|
+
return jsonSafe(await actions.cancel({ promptId: args?.promptId }))
|
|
293
|
+
},
|
|
294
|
+
},
|
|
295
|
+
|
|
296
|
+
{
|
|
297
|
+
name: 'comfy_import_workflow',
|
|
298
|
+
description:
|
|
299
|
+
'把一个 ComfyUI 工作流 JSON 保存进插件(之后就能用 comfy_run 运行)。支持 ComfyUI 里「导出(UI 格式)」和「导出 API 格式」两种 JSON;'
|
|
300
|
+
+ 'json 与 path 二选一。UI 格式会自动转换成 ComfyUI /prompt 需要的 API 格式。',
|
|
301
|
+
parameters: objectSchema({
|
|
302
|
+
name: stringParam('保存用的工作流名称(重复会自动加后缀)'),
|
|
303
|
+
json: stringParam('工作流 JSON 文本(可直接把文件内容传进来)'),
|
|
304
|
+
path: stringParam('工作区内的 .json 文件路径(绝对路径或相对当前会话工作目录)'),
|
|
305
|
+
}, ['name']),
|
|
306
|
+
timeoutMs: 120_000,
|
|
307
|
+
async execute(args, exec) {
|
|
308
|
+
let raw = args?.json
|
|
309
|
+
if ((raw === undefined || raw === null || raw === '') && args?.path) {
|
|
310
|
+
const file = await resolveInputPath(exec?.agent, args.path)
|
|
311
|
+
const info = await stat(file).catch(() => null)
|
|
312
|
+
if (!info || !info.isFile()) return { ok: false, error: `文件不存在:${file}` }
|
|
313
|
+
if (info.size > MAX_IMPORT_BYTES) return { ok: false, error: `工作流文件过大(上限 ${MAX_IMPORT_BYTES} 字节)` }
|
|
314
|
+
raw = await readFile(file, 'utf8')
|
|
315
|
+
}
|
|
316
|
+
if (raw === undefined || raw === null || raw === '') {
|
|
317
|
+
return { ok: false, error: '需要 json(JSON 文本)或 path(工作区文件路径)之一' }
|
|
318
|
+
}
|
|
319
|
+
return jsonSafe(await actions.importWorkflow({ name: String(args?.name || ''), json: raw }))
|
|
320
|
+
},
|
|
321
|
+
},
|
|
322
|
+
|
|
323
|
+
{
|
|
324
|
+
name: 'comfy_server_workflows',
|
|
325
|
+
description:
|
|
326
|
+
'兼容旧用法:列出 ComfyUI 服务器工作流,或按 importPath 把其中一个导入本地库。'
|
|
327
|
+
+ '**通常不需要导入** —— 直接 comfy_run({workflow:"<path>"}) 即可,运行时会自动加载。',
|
|
328
|
+
parameters: objectSchema({
|
|
329
|
+
importPath: stringParam('要导入的服务器工作流路径(省略=只列出,不导入)'),
|
|
330
|
+
name: stringParam('导入后保存的名称(省略=用文件名)'),
|
|
331
|
+
}),
|
|
332
|
+
async execute(args) {
|
|
333
|
+
if (args?.importPath) {
|
|
334
|
+
return jsonSafe(await actions.importServerWorkflow({ path: String(args.importPath), name: args?.name }))
|
|
335
|
+
}
|
|
336
|
+
return jsonSafe(await actions.listServerWorkflows())
|
|
337
|
+
},
|
|
338
|
+
},
|
|
339
|
+
|
|
340
|
+
{
|
|
341
|
+
name: 'comfy_choose_workflow',
|
|
342
|
+
description:
|
|
343
|
+
'用户只说「想要什么」、没说用哪个工作流时先调它:按任务描述给 ComfyUI 服务器上的工作流打分排序(按文件名/目录猜能力),'
|
|
344
|
+
+ '挑最合适的一个直接 comfy_run({"workflow":"<sourcePath>"}) 运行 —— use 时自动从服务器加载,**不需要导入**。'
|
|
345
|
+
+ '返回每个候选的匹配分、原因、sourcePath 和下一步;服务器候选可用 comfy_workflow 传 path 看具体参数。'
|
|
346
|
+
+ '本地已保存的旧工作流默认不参与排序(includeLocal=true 才加入)。',
|
|
347
|
+
parameters: objectSchema({
|
|
348
|
+
task: stringParam('用户想要的效果/用途描述,尽量保留原话里的风格、用途词(比如「把它放大成高清头像」「画一只赛博朋克风格的猫」)'),
|
|
349
|
+
includeServer: booleanParam('是否把 ComfyUI 服务器工作流纳入候选(默认 true;服务器连不上会返回 warning 并用本地兜底)'),
|
|
350
|
+
includeLocal: booleanParam('是否也把本地已保存的旧工作流纳入排序(默认 false)'),
|
|
351
|
+
limit: numberParam('最多返回几个候选(1–10,默认 5)'),
|
|
352
|
+
}, ['task']),
|
|
353
|
+
async execute(args) {
|
|
354
|
+
return jsonSafe(await actions.chooseWorkflow({
|
|
355
|
+
task: String(args?.task || ''),
|
|
356
|
+
includeServer: args?.includeServer !== false,
|
|
357
|
+
includeLocal: args?.includeLocal === true,
|
|
358
|
+
limit: args?.limit,
|
|
359
|
+
}))
|
|
360
|
+
},
|
|
361
|
+
},
|
|
362
|
+
|
|
363
|
+
{
|
|
364
|
+
name: 'comfy_describe_workflow',
|
|
365
|
+
description:
|
|
366
|
+
'给一个已保存的工作流补上「用途说明 + 标签」,让下次 AI 选型更准。'
|
|
367
|
+
+ '例如用户说过「这个工作流是画二次元头像的」「那个是修复老照片的」,就把它记下来;之后 comfy_choose_workflow 会用到这些说明/标签,comfy_status 与 comfy_workflows 也会显示。',
|
|
368
|
+
parameters: objectSchema({
|
|
369
|
+
workflow: stringParam('工作流 id 或名称(先用 comfy_workflows 查)'),
|
|
370
|
+
description: stringParam('用途说明:一句话讲清什么时候该用它、它需要什么输入、输出什么(≤500 字)'),
|
|
371
|
+
tags: {
|
|
372
|
+
type: 'array',
|
|
373
|
+
items: { type: 'string' },
|
|
374
|
+
description: '标签(≤12 个,每个 ≤40 字),如 ["二次元", "头像", "不需要输入图"]',
|
|
375
|
+
},
|
|
376
|
+
}, ['workflow']),
|
|
377
|
+
async execute(args5) {
|
|
378
|
+
const result = await actions.updateWorkflowMeta({
|
|
379
|
+
id: String(args5?.workflow || ''),
|
|
380
|
+
description: args5?.description,
|
|
381
|
+
tags: args5?.tags,
|
|
382
|
+
})
|
|
383
|
+
const wf = result.workflow
|
|
384
|
+
return jsonSafe({
|
|
385
|
+
...result,
|
|
386
|
+
hint: wf ? `已保存:${wf.name}。下次选型会参考这段说明和标签。` : '没有更新内容(description / tags 至少给一个)',
|
|
387
|
+
})
|
|
388
|
+
},
|
|
389
|
+
},
|
|
390
|
+
|
|
391
|
+
{
|
|
392
|
+
name: 'comfy_upload_image',
|
|
393
|
+
description:
|
|
394
|
+
'把当前会话工作区里的一张图片上传到 ComfyUI 的 input 目录,返回可填进 LoadImage 节点 image 参数的文件名(img2img / 图生视频用)。',
|
|
395
|
+
parameters: objectSchema({
|
|
396
|
+
path: stringParam('工作区内图片路径(绝对路径或相对当前会话工作目录)'),
|
|
397
|
+
subfolder: stringParam('上传到 ComfyUI input 下的子目录(可选)'),
|
|
398
|
+
}, ['path']),
|
|
399
|
+
timeoutMs: 150_000,
|
|
400
|
+
async execute(args, exec) {
|
|
401
|
+
const file = await resolveInputPath(exec?.agent, args?.path)
|
|
402
|
+
const info = await stat(file).catch(() => null)
|
|
403
|
+
if (!info || !info.isFile()) return { ok: false, error: `文件不存在:${file}` }
|
|
404
|
+
if (info.size > 64 * 1024 * 1024) return { ok: false, error: '图片超过 64MB,拒绝上传' }
|
|
405
|
+
const data = await readFile(file)
|
|
406
|
+
const filename = file.replace(/\\/g, '/').split('/').pop() || 'image.png'
|
|
407
|
+
return jsonSafe(await actions.uploadImage({
|
|
408
|
+
data,
|
|
409
|
+
filename,
|
|
410
|
+
subfolder: args?.subfolder ? String(args.subfolder) : '',
|
|
411
|
+
}))
|
|
412
|
+
},
|
|
413
|
+
},
|
|
414
|
+
]
|
|
415
|
+
|
|
416
|
+
ctx.effect(() => {
|
|
417
|
+
const disposers = []
|
|
418
|
+
for (const definition of definitions) {
|
|
419
|
+
try {
|
|
420
|
+
const disposer = ctx.tools.register({
|
|
421
|
+
...definition,
|
|
422
|
+
output: textOutput,
|
|
423
|
+
async execute(args, exec) {
|
|
424
|
+
try {
|
|
425
|
+
return jsonSafe(await definition.execute(args ?? {}, exec))
|
|
426
|
+
} catch (error) {
|
|
427
|
+
return { ok: false, error: error?.message || String(error) }
|
|
428
|
+
}
|
|
429
|
+
},
|
|
430
|
+
})
|
|
431
|
+
disposers.push(disposer)
|
|
432
|
+
} catch (error) {
|
|
433
|
+
logger(`注册工具 ${definition.name} 失败:`, error?.message || error)
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
logger(`已注册 ${disposers.length}/${definitions.length} 个 comfy_* 工具`)
|
|
437
|
+
return () => {
|
|
438
|
+
while (disposers.length > 0) {
|
|
439
|
+
try {
|
|
440
|
+
disposers.pop()()
|
|
441
|
+
} catch {
|
|
442
|
+
/* 已释放 */
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
}, 'dsh-sendcomfy: comfy_* 工具')
|
|
447
|
+
|
|
448
|
+
logger('已挂载(面板:侧边栏 ComfyUI 桥接;工具:comfy_status / comfy_run …)')
|
|
449
|
+
}
|