Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

fix(agent): the chat image tool runs the workflow assigned to the model (GH #192, C29)

purple2271a04da1c7
diff --git a/src/api/__tests__/chat-image-uses-the-assigned-workflow.test.ts b/src/api/__tests__/chat-image-uses-the-assigned-workflow.test.ts
new file mode 100644
index 00000000..b4ecfb36
--- /dev/null
+++ b/src/api/__tests__/chat-image-uses-the-assigned-workflow.test.ts
@@ -0,0 +1,57 @@
+/**
+ * GH #192 part 3 and C29: the image tool of the chat always built LU's own
+ * graph and never asked which workflow the user had assigned to the model,
+ * so a model that only runs with its own workflow (MOSS Image in the issue)
+ * could not be used from the chat. It now takes the assigned workflow as
+ * Create does, with the values set under "Workflow parameters", and sets it
+ * aside by the same loader check when it does not fit the model.
+ *
+ * Run: npx vitest run src/api/__tests__/chat-image-uses-the-assigned-workflow.test.ts
+ */
+import { describe, it, expect, beforeEach } from 'vitest'
+import { readFileSync } from 'node:fs'
+import { resolve } from 'node:path'
+import { ownWorkflowFor } from '../vram-handoff'
+import { useWorkflowStore } from '../../stores/workflowStore'
+
+const UNET_WF = {
+ id: 'wf-moss', name: 'MOSS Image (own)',
+ workflow: { '1': { class_type: 'UNETLoader', inputs: { unet_name: 'moss_image.safetensors' } }, '2': { class_type: 'KSampler', inputs: {} } },
+ parameterMap: { model: { nodeId: '1', inputKey: 'unet_name' } },
+}
+const CKPT_WF = {
+ ...UNET_WF, id: 'wf-ckpt', name: 'SDXL (own)',
+ workflow: { '1': { class_type: 'CheckpointLoaderSimple', inputs: {} } },
+}
+
+function assign(wf: unknown) {
+ useWorkflowStore.setState({
+ getWorkflowForModel: () => wf as ReturnType<ReturnType<typeof useWorkflowStore.getState>['getWorkflowForModel']>,
+ paramOverrides: { 'wf-moss': { '2.steps': 50 } } as never,
+ })
+}
+
+describe('#192 part 3: the chat image tool takes the workflow assigned to the model', () => {
+ beforeEach(() => assign(null))
+
+ it('a fitting assigned workflow is used, with the user\'s parameter values', async () => {
+ assign(UNET_WF)
+ const own = await ownWorkflowFor('moss_image.safetensors', 'unknown', 'diffusion_model')
+ expect(own?.name).toBe('MOSS Image (own)')
+ expect(own?.overrides).toEqual({ '2.steps': 50 })
+ })
+
+ it('one whose loader does not fit the model is set aside, as in Create', async () => {
+ assign(CKPT_WF)
+ expect(await ownWorkflowFor('moss_image.safetensors', 'unknown', 'diffusion_model')).toBeNull()
+ })
+
+ it('no assignment: LU builds its own graph as before', async () => {
+ expect(await ownWorkflowFor('juggernautXL.safetensors', 'sdxl', 'checkpoint')).toBeNull()
+ })
+
+ it('the image path injects into the assigned workflow before it falls back to the builder', () => {
+ const src = readFileSync(resolve(__dirname, '../vram-handoff.ts'), 'utf8')
+ expect(src).toMatch(/const own = await ownWorkflowFor\(model, type, listed\?\.source\)\s+const workflow = own\s+\? await injectParameters\(own\.workflow, own\.parameterMap, params, type, own\.overrides\)\s+: await buildFixable\('image', \(\) => buildDynamicWorkflow\(params, type\)\)/)
+ })
+})
diff --git a/src/api/vram-handoff.ts b/src/api/vram-handoff.ts
index 6b1e06d3..05d703c8 100644
--- a/src/api/vram-handoff.ts
+++ b/src/api/vram-handoff.ts
@@ -47,7 +47,10 @@
import { setChatBackendsRestore, holdChatBackends } from '../lib/chat-backends-gate'
import { gpuRolesSeparate } from '../lib/gpu-roles'
import { backendCall, ollamaUrl, localFetch, isOllamaLocal, isWindows, fetchLocalhostBytes } from './backend'
-import type { ComfyApiGraph } from '../types/comfy-graph'
+import { apiNodes, type ComfyApiGraph } from '../types/comfy-graph'
+import { injectParameters } from './workflows'
+import { ownWorkflowMisfit } from '../lib/own-workflow-fit'
+import type { ComfyModelSource } from '../types/models'
import { asNumber, asRecordArray, asString, prop } from '../types/json-guards'
import { listRunningModels, loadModel, unloadModel } from './ollama'
import { startBundledEngine } from './engine'
@@ -78,6 +81,7 @@ import {
isPromptQueued,
type VideoBackend,
} from './comfyui'
+import type { GenerateParams, ModelType } from './comfyui'
import type { ModelCapabilities } from './comfyui-nodes'
import { getActiveAgentModel } from './agent-context'
import { comfyWS, CLIENT_ID } from './comfyui-ws'
@@ -1494,8 +1498,7 @@ async function generateImage(
// with the same one.
const { useCreateStore } = await import('../stores/createStore')
const qwenTextEncoder = useCreateStore.getState().qwenTextEncoder
- const workflow = await buildFixable('image', () => buildDynamicWorkflow(
- {
+ const params: GenerateParams = {
prompt,
negativePrompt: typeof args.negativePrompt === 'string' ? args.negativePrompt : '',
model,
@@ -1531,9 +1534,17 @@ async function generateImage(
? { maskImage: stagedNames([a.maskImage])[0], growMaskBy: clampInt(a.growMaskBy, 6, 0, 64) }
: {}),
...(listed?.parts ? { modelParts: listed.parts } : {}),
- },
- type,
- ))
+ }
+ // GH #192 part 3, C29: a workflow the user assigned to this model runs in
+ // the chat as it does in Create (useCreate), with the values set under
+ // "Workflow parameters"; one whose loader does not fit the model is set
+ // aside the same way. Before, the chat always built LU's own graph, so a
+ // model that only runs with its own workflow could not be used here.
+ const own = await ownWorkflowFor(model, type, listed?.source)
+ const workflow = own
+ ? await injectParameters(own.workflow, own.parameterMap, params, type, own.overrides)
+ : await buildFixable('image', () => buildDynamicWorkflow(params, type))
+ if (own) log.info('vram_handoff.image.own_workflow', { model, workflow: own.name })
// Phase markers (chat-agent hang 2026-06-03): make it obvious in the log
// whether a stall is in the workflow build (/object_info) or the submit
// (/prompt). Both are now timeout-bounded, so neither can strand the
@@ -1550,6 +1561,24 @@ async function generateImage(
}
}
+/**
+ * The workflow the user assigned to `model` (workflowStore), when its loader
+ * fits the model (lib/own-workflow-fit, the same check as Create), with the
+ * values the user set under "Workflow parameters". Null: LU builds the graph.
+ */
+export async function ownWorkflowFor(model: string, type: ModelType, source?: ComfyModelSource) {
+ const { useWorkflowStore } = await import('../stores/workflowStore')
+ const store = useWorkflowStore.getState()
+ const wf = store.getWorkflowForModel(model, type)
+ if (!wf) return null
+ const misfit = ownWorkflowMisfit(apiNodes(wf.workflow).map(([, n]) => n.class_type), type, source)
+ if (misfit) {
+ log.info('vram_handoff.own_workflow_set_aside', { model, workflow: wf.name, misfit })
+ return null
+ }
+ return { ...wf, overrides: store.paramOverrides[wf.id] }
+}
+
/** Video path — buildTxt2VidWorkflow with MODEL_TYPE_DEFAULTS + snapToVideoGrid. */
async function generateVideo(
prompt: string,