From cd8833ba1d40a090061daf268bfdf2788958c380 Mon Sep 17 00:00:00 2001 From: shaohuzhang1 Date: Tue, 1 Dec 2026 11:15:07 +0800 Subject: [PATCH] fix: AI generated model switching does not take effect --- ui/src/workflow-canvas/WORKFLOW_README.md | 3 ++- ui/src/workflow-canvas/ai-generate/common.ts | 3 +-- .../ai-generate/create-agent.ts | 13 +++++++------ .../ai-generate/panel/AiGeneratePanel.vue | 5 ++--- ui/src/workflow-canvas/ai-generate/type.ts | 18 +++++++++++------- 5 files changed, 23 insertions(+), 19 deletions(-) diff --git a/ui/src/workflow-canvas/WORKFLOW_README.md b/ui/src/workflow-canvas/WORKFLOW_README.md index 4f8d34a0342..cf396ac7f89 100644 --- a/ui/src/workflow-canvas/WORKFLOW_README.md +++ b/ui/src/workflow-canvas/WORKFLOW_README.md @@ -580,7 +580,8 @@ AI 生成的 validate 工具复用同一入口(`collectWorkflowValidationError 当前用于应用、知识库、工具三类工作流及其循环体,根据用户需求规划、创建节点、连线与配置字段。 页面通过 WorkflowCanvas.getLogicFlow() 提供画布,生成结果只改图,不自动保存。 模型选择支持参数设置,不提供添加模型入口;优先沿用默认 LLM 的参数,切换模型加载对应默认参数。 -首轮生成时固定模型及参数,父 agent 和委派的子 agent 共用,并通过 `model_params_setting` 传入补全请求;新建会话后可重新配置。 +模型及参数经 `AgentBindings.getModel` 在每轮补全前读取,父 agent 和委派的子 agent 共用,并通过 `model_params_setting` 传入补全请求; +会话中切换模型保留上下文,下一轮补全即生效;生成运行中模型选择禁用。 面板从 `resourceWorkspaceId` 注入读取资源所属空间,以 `AgentBindings.getResourceWorkspaceId` 传给节点 agent, 资源查询经 `useWorkflowStore(resourceScope, getResourceWorkspaceId)` 与节点回显、选择弹窗保持同一查询范围。 diff --git a/ui/src/workflow-canvas/ai-generate/common.ts b/ui/src/workflow-canvas/ai-generate/common.ts index 9959b6bb766..4302958e29b 100644 --- a/ui/src/workflow-canvas/ai-generate/common.ts +++ b/ui/src/workflow-canvas/ai-generate/common.ts @@ -31,8 +31,7 @@ export function useAgentConfig(config: AgentConfig, bindings: AgentBindings): Ru prompt, messages, tools: config.tools, - modelId: bindings.modelId, - modelParams: bindings.modelParams, + getModel: bindings.getModel, resourceScope: bindings.resourceScope, call: bindings.call, position: bindings.position ?? [], diff --git a/ui/src/workflow-canvas/ai-generate/create-agent.ts b/ui/src/workflow-canvas/ai-generate/create-agent.ts index 5591ec2411b..757921c8eef 100644 --- a/ui/src/workflow-canvas/ai-generate/create-agent.ts +++ b/ui/src/workflow-canvas/ai-generate/create-agent.ts @@ -2,6 +2,7 @@ import type { ToolResult } from './tools/result' import type { AgentHandle, AgentMessage, + AgentModel, AgentPayload, AgentResult, AgentTool, @@ -44,7 +45,7 @@ function toExecutor(value: unknown): Executor { * 某工具返回 AgentSuccessResult / AgentErrorResult 结束、被停止或超过轮次。工具返回 ErrorResult 或抛错时把错误回喂模型自愈。 */ export function createAgent(agent: RunnableAgent): AgentHandle { - const { tools, modelId, modelParams, resourceScope, call, position, maxIterations } = agent + const { tools, getModel, resourceScope, call, position, maxIterations } = agent const controller = new AbortController() const stopCallbacks: Array<() => void> = [] const messages: AgentMessage[] = agent.messages @@ -65,7 +66,7 @@ export function createAgent(agent: RunnableAgent): AgentHandle { } async function run(requirement: string): Promise { - if (!modelId) return finish({ status: 'error', data: t('workflow.aiGeneration.errors.modelRequired') }) + if (!getModel().modelId) return finish({ status: 'error', data: t('workflow.aiGeneration.errors.modelRequired') }) logDebug({ kind: 'run', pos: position, requirement: clipForLog(requirement, 800) }) messages.push({ role: 'user', content: requirement }) @@ -75,7 +76,8 @@ export function createAgent(agent: RunnableAgent): AgentHandle { if (iteration > maxIterations) return finish({ status: 'error', data: t('workflow.aiGeneration.errors.iterationLimit', { max: maxIterations }) }) - const response = await runTurn(resourceScope, modelId, modelParams, messages, tools, emit, controller.signal) + // 每轮读取当前选中的模型:会话中切换模型后,下一轮补全即生效 + const response = await runTurn(resourceScope, getModel(), messages, tools, emit, controller.signal) const toolCalls = response.toolCalls ?? [] const assistantMessage: AgentMessage = { role: 'assistant', content: response.content || '' } // thinking 模型要求思考内容按原字段名回传,否则下一轮请求会被拒绝 @@ -126,8 +128,7 @@ export function createAgent(agent: RunnableAgent): AgentHandle { /** 跑一轮补全:把流式条目翻成事件下发,并返回本轮完整结果。 */ function runTurn( resourceScope: string, - modelId: string, - modelParams: Record | undefined, + model: AgentModel, messages: AgentMessage[], tools: AgentTool[], emit: (payload: AgentPayload) => void, @@ -158,7 +159,7 @@ function runTurn( else reject(new Error(t('workflow.aiGeneration.errors.incompleteTurn'))) }, } - chatCompletion(resourceScope, modelId, { messages, tools: toToolSchemas(tools), model_params_setting: modelParams }, sink, signal) + chatCompletion(resourceScope, model.modelId, { messages, tools: toToolSchemas(tools), model_params_setting: model.modelParams }, sink, signal) }) } diff --git a/ui/src/workflow-canvas/ai-generate/panel/AiGeneratePanel.vue b/ui/src/workflow-canvas/ai-generate/panel/AiGeneratePanel.vue index ab0870b113e..6f51ddbf66b 100644 --- a/ui/src/workflow-canvas/ai-generate/panel/AiGeneratePanel.vue +++ b/ui/src/workflow-canvas/ai-generate/panel/AiGeneratePanel.vue @@ -139,7 +139,7 @@ function resourceIdOf(): string | undefined { return typeof route.params[key] === 'string' ? (route.params[key] as string) : undefined } -/** 首轮装配父 agent,后续续聊复用同一实例以保留上下文 */ +/** 首轮装配父 agent,后续续聊复用同一实例以保留上下文;模型每轮补全前读取,切换后下一轮即生效 */ async function run(text: string) { const lf = props.getLogicFlow() if (!lf) return @@ -148,8 +148,7 @@ async function run(text: string) { agent = createAgent( useAgent(props.workflowMode, { rootLf: lf, - modelId: modelId.value, - modelParams: cloneDeep(modelParams.value), + getModel: () => ({ modelId: modelId.value, modelParams: cloneDeep(modelParams.value) }), call: { onNext: onEvent }, workflowMode: props.workflowMode, resourceScope, diff --git a/ui/src/workflow-canvas/ai-generate/type.ts b/ui/src/workflow-canvas/ai-generate/type.ts index bf7ac805da6..9ccd3796ea4 100644 --- a/ui/src/workflow-canvas/ai-generate/type.ts +++ b/ui/src/workflow-canvas/ai-generate/type.ts @@ -98,14 +98,19 @@ export interface AgentTool { resultLimit?: number } +/** 补全使用的模型及参数 */ +export interface AgentModel { + modelId: string + /** 补全请求的 model_params_setting */ + modelParams?: Record +} + /** 构造期绑定:装配 agent 与建工具时使用的上下文。 */ export interface AgentBindings { /** 主画布 LogicFlow 实例;循环体画布经 lf_id 从主画布展开 */ rootLf: LogicFlow - /** 模型 id(委派的子 agent 复用同一模型) */ - modelId: string - /** 生成及委派补全共用的模型参数 */ - modelParams?: Record + /** 读取当前选中的模型及参数;每轮补全前读取,委派的子 agent 共用 */ + getModel: () => AgentModel /** 对外事件出口 */ call: Call /** 主画布模式 */ @@ -137,9 +142,8 @@ export interface RunnableAgent { prompt: string messages: AgentMessage[] tools: AgentTool[] - modelId: string - /** 生成及委派补全共用的模型参数 */ - modelParams?: Record + /** 读取当前选中的模型及参数,每轮补全前读取 */ + getModel: () => AgentModel /** 画布资源范围,补全请求经 useWorkflowStore(resourceScope) 发起 */ resourceScope: string call: Call