diff --git a/ui/src/workflow-canvas/WORKFLOW_README.md b/ui/src/workflow-canvas/WORKFLOW_README.md index c49befefc9f..2f5776b45a7 100644 --- a/ui/src/workflow-canvas/WORKFLOW_README.md +++ b/ui/src/workflow-canvas/WORKFLOW_README.md @@ -336,11 +336,19 @@ AI 对话节点的 `component/resource-setting` 仅渲染技能卡片内的 MCP 工具和 Skills 的添加入口复用 `SelectToolDialog`,智能体入口使用 `SelectApplicationDialog`; 工具选择从 `resourceWorkspaceId` 注入读取详情所属空间,通过 `workspaceId` 传给弹窗,简易智能体与 AI 对话节点共用此流程。 System 共享资源不展示智能体分组,也不挂载智能体选择弹窗。 -Skills 仅查询 SKILL 类型,选择器排除当前路由对应的智能体或工具。确认后同时更新执行用的 -ID 数组及 `tool_list`、`skill_tool_list`、`application_list` 回显快照;移除时同步清理快照, -取消不更新节点,旧数据缺少快照时继续显示 ID。 +Skills 仅查询 SKILL 类型,选择器排除当前路由对应的智能体或工具。确认后只更新执行用的 +ID 数组,不在节点中保存回显快照;节点加载时清除旧数据遗留的 `tool_list`、`skill_tool_list`、 +`application_list`。取消不更新节点。 标题、输出执行过程开关与 `mk-white-card` 由节点入口维护。组件通过 `setting` 读取资源配置、通过 `update` 提交局部变更;资源选项由 Props 传入,未接入数据源时默认为空数组。 +回显按 ID 先查资源选项,再查本组件内记住的弹窗返回结果(不写入节点),都缺失时显示 ID。 + +AI 对话节点的回显选项来自工作流 Store,查询条件与选择弹窗一致:`getToolList` / `getSkillToolList` +通过各范围的 `getAllTool` 查询自有工具和 Skills(Workspace 带根目录 `folder_id`,System 资源管理带 +`workspace_id`,空间未就绪时返回空数组);`getSharedToolList` / `getSharedSkillToolList` 通过 +`getAllSharedTool` 查询共享目录,仅 Workspace 的专业版和企业版提供,其余返回空数组; +`getApplicationList` 查询已发布智能体,不支持的范围返回空数组。自有与共享分别缓存和存储, +传给资源设置时合并;节点在 `resourceWorkspaceId` 就绪或切换时重新加载。 AI 对话节点的提示词、历史记录、视觉理解和输出思考表单直接在节点入口维护,统一使用全局 `MdEditorMagnify` 和节点表单样式。AI 对话、LLM、图片理解和视频理解的系统提示词生成统一使用 diff --git a/ui/src/workflow-canvas/nodes/ai-chat-node/agent.ts b/ui/src/workflow-canvas/nodes/ai-chat-node/agent.ts index 95183b46956..80daaddf768 100644 --- a/ui/src/workflow-canvas/nodes/ai-chat-node/agent.ts +++ b/ui/src/workflow-canvas/nodes/ai-chat-node/agent.ts @@ -1,6 +1,6 @@ -import { pick } from 'lodash' -import type { AiChatNodeForm, ToolResourceOption } from './types' -import type { ToolItem } from '@/api/types' +import { uniqBy } from 'lodash' +import type { AiChatNodeForm } from './types' +import type { ApplicationDetail, ToolItem } from '@/api/types' import type { AgentTool, JsonObjectSchema, NodeAgentContext, NodeAgentModule } from '@/workflow-canvas/ai-generate/type' import { TOOL_TYPE } from '@/api/enums' import { getNode, readNodeData, validatePromptVariables, writeNodeProperties } from '@/workflow-canvas/ai-generate/tools/canvas' @@ -13,59 +13,75 @@ import { useNodeAgent, } from '@/workflow-canvas/ai-generate/tools/node' import { ErrorResult } from '@/workflow-canvas/ai-generate/tools/result' -import { useWorkflowStore } from '@/workflow-canvas/store' +import { NODE_TOOL_TYPES, useWorkflowStore } from '@/workflow-canvas/store' +import { WorkflowMode } from '@/workflow-canvas/types' /** * ai-chat-node(AI 对话)的 AI 生成配置;node_data 协议以节点视图 index.vue 的 defaultForm 为准。 * 入参(提示词素材、图片、视频)由父 agent 绑定;config 即 update 的参数,写入并校验通过后交付节点的输出字段(config.fields)。 - * 关联应用(application_ids)与自定义 MCP 服务(mcp_servers)暂不由 AI 配置。 + * 自定义 MCP 服务(mcp_servers)暂不由 AI 配置。 */ -/** 节点「工具」可选的工具类型,与资源设置的工具选择弹窗一致;Skills 与 MCP 各自单列 */ -const NODE_TOOL_TYPES: string[] = [TOOL_TYPE.CUSTOM, TOOL_TYPE.WORKFLOW, TOOL_TYPE.INTERNAL] - const ID_LIST_SCHEMA = (description: string) => ({ type: 'array', items: { type: 'string' }, description }) interface SkillResources { tools: ToolItem[] skills: ToolItem[] mcps: ToolItem[] + applications: ApplicationDetail[] +} + +/** 当前画布所属的资源 id(智能体或工具);资源不能挂载自身,与选择弹窗按路由排除一致 */ +function currentResourceIds(context: NodeAgentContext) { + const { workflowMode, resourceId } = context.bindings + const isApplication = workflowMode === WorkflowMode.Application || workflowMode === WorkflowMode.ApplicationLoop + const isTool = workflowMode === WorkflowMode.Tool || workflowMode === WorkflowMode.ToolLoop + return { applicationId: isApplication ? resourceId : undefined, toolId: isTool ? resourceId : undefined } } -/** 节点可挂载的技能资源:工具、Skills、MCP,均只取已启用的 */ +/** + * 节点可挂载的技能资源,查询与资源设置的选择弹窗及节点回显一致: + * 工具、Skills 取自有与共享目录合并,MCP 沿用 MCP 设置弹窗的查询;工具类只取已启用的,智能体只取已发布的,均排除当前资源。 + */ async function loadSkillResources(context: NodeAgentContext): Promise { const store = useWorkflowStore(context.bindings.resourceScope) - const [toolGroups, skills, mcps] = await Promise.all([ - Promise.all(NODE_TOOL_TYPES.map((toolType) => store.getToolListWithShared({ tool_type: toolType }))), - store.getToolListWithShared({ tool_type: TOOL_TYPE.SKILL }), + const [ownTools, sharedTools, ownSkills, sharedSkills, mcps, applications] = await Promise.all([ + store.getToolList(), + store.getSharedToolList(), + store.getSkillToolList(), + store.getSharedSkillToolList(), store.getToolListWithShared({ tool_type: TOOL_TYPE.MCP }), + store.getApplicationList(), ]) + const { applicationId, toolId } = currentResourceIds(context) const activeOf = (resources: ToolItem[], toolTypes: string[]) => - resources.filter((resource) => resource.is_active && toolTypes.includes(resource.tool_type)) + uniqBy(resources, 'id').filter((resource) => resource.is_active && toolTypes.includes(resource.tool_type) && resource.id !== toolId) return { - tools: activeOf(toolGroups.flat(), NODE_TOOL_TYPES), - skills: activeOf(skills, [TOOL_TYPE.SKILL]), + tools: activeOf([...ownTools, ...sharedTools], NODE_TOOL_TYPES), + skills: activeOf([...ownSkills, ...sharedSkills], [TOOL_TYPE.SKILL]), mcps: activeOf(mcps, [TOOL_TYPE.MCP]), + applications: applications.filter((application) => application.is_publish && application.id !== applicationId), } } -/** 把模型给的 id 列表对照真实资源,返回节点回显用的快照;有不存在的 id 时返回错误文本 */ -function resolveResources(ids: unknown, resources: ToolItem[], label: string): ToolResourceOption[] | string { +/** 把模型给的 id 列表对照真实资源,返回可挂载的 id;有不存在的 id 时返回错误文本 */ +function resolveResources(ids: unknown, resources: { id: string }[], label: string): string[] | string { if (!Array.isArray(ids)) return `${label} 应为 id 数组` const missingIds = ids.filter((id) => !resources.some((resource) => resource.id === id)) if (missingIds.length) return `${label} 中的 ${missingIds.join('、')} 不存在或未启用,先 get_skill_resources 查询` - return ids.map((id) => pick(resources.find((resource) => resource.id === id)!, ['desc', 'icon', 'id', 'name', 'source', 'tool_type'])) + return ids.map(String) } function skillResourcesTool(context: NodeAgentContext): AgentTool { return { name: 'get_skill_resources', - description: '列出可挂到本节点的技能资源:tools(工具)、skills(Skills)、mcps(MCP),均为 [{id,name,desc}]。', + description: + '列出可挂到本节点的技能资源:tools(工具)、skills(Skills)、mcps(MCP)、applications(已发布智能体),均为 [{id,name,desc}]。', parameters: { type: 'object', properties: {} }, apply: async () => { - const { tools, skills, mcps } = await loadSkillResources(context) - const brief = (resources: ToolItem[]) => resources.map(({ id, name, desc }) => ({ id, name, desc })) - return { tools: brief(tools), skills: brief(skills), mcps: brief(mcps) } + const { tools, skills, mcps, applications } = await loadSkillResources(context) + const brief = (resources: (ApplicationDetail | ToolItem)[]) => resources.map(({ id, name, desc }) => ({ id, name, desc })) + return { tools: brief(tools), skills: brief(skills), mcps: brief(mcps), applications: brief(applications) } }, } } @@ -93,6 +109,7 @@ const config: JsonObjectSchema = { tool_ids: ID_LIST_SCHEMA('挂载的工具 id 列表(get_skill_resources.tools),传空数组表示清空'), skill_tool_ids: ID_LIST_SCHEMA('挂载的 Skills id 列表(get_skill_resources.skills),传空数组表示清空'), mcp_tool_ids: ID_LIST_SCHEMA('挂载的 MCP id 列表(get_skill_resources.mcps),传空数组表示清空'), + application_ids: ID_LIST_SCHEMA('挂载的智能体 id 列表(get_skill_resources.applications),传空数组表示清空'), mcp_output_enable: { type: 'boolean', description: '是否输出技能的执行过程,默认 true' }, reasoning_content_enable: { type: 'boolean', description: '是否输出思考过程(仅思考模型需要)' }, }, @@ -119,14 +136,14 @@ function useAgent(context: NodeAgentContext) { '- dialogue_number / dialogue_type:携带的历史对话轮数(0 表示不带历史);历史来源 WORKFLOW 为整个工作流的问答,NODE 为本节点自己的问答。', '- is_result:是否把回答输出给用户;作为流程最终回答时为 true,作为中间加工步骤时为 false。', '- vision:图片 / 视频已由入参 images / videos 绑定,绑定时默认开启视觉,无需配置。', - '- tool_ids / skill_tool_ids / mcp_tool_ids:需求要求节点调用工具、Skills 或 MCP 时,先 get_skill_resources 取真实 id 再挂载;不需要时不传。', + '- tool_ids / skill_tool_ids / mcp_tool_ids / application_ids:需求要求节点调用工具、Skills、MCP 或其他智能体时,先 get_skill_resources 取真实 id 再挂载;不需要时不传。', '- mcp_output_enable:是否把技能的执行过程输出给用户,默认 true。', ].join('\n'), tools: [ { name: 'update', description: - '配置当前 AI 对话节点:模型、系统/用户提示词、历史对话、是否输出给用户、视觉、技能(工具 / Skills / MCP)、是否输出思考过程。只传要设置的字段,其余保持不变。' + + '配置当前 AI 对话节点:模型、系统/用户提示词、历史对话、是否输出给用户、视觉、技能(工具 / Skills / MCP / 智能体)、是否输出思考过程。只传要设置的字段,其余保持不变。' + '写入后自动校验,通过即配置完成并交付本节点的输出字段;不通过返回 error 继续修。', parameters: config, apply: async (args) => { @@ -146,24 +163,34 @@ function useAgent(context: NodeAgentContext) { vision: typeof args.vision === 'boolean' ? args.vision : (nodeData.vision || hasMedia), } - // 技能挂载:id 对照真实资源,同时写回节点回显所需的快照 + // 技能挂载:id 对照真实资源,只写回执行所需的 id const skillFields: Partial = {} - if (args.tool_ids !== undefined || args.skill_tool_ids !== undefined || args.mcp_tool_ids !== undefined) { + if ( + args.tool_ids !== undefined || + args.skill_tool_ids !== undefined || + args.mcp_tool_ids !== undefined || + args.application_ids !== undefined + ) { const resources = await loadSkillResources(context) if (args.tool_ids !== undefined) { const tools = resolveResources(args.tool_ids, resources.tools, 'tool_ids') if (typeof tools === 'string') return new ErrorResult(tools) - Object.assign(skillFields, { tool_ids: tools.map(({ id }) => id), tool_list: tools }) + Object.assign(skillFields, { tool_ids: tools }) } if (args.skill_tool_ids !== undefined) { const skills = resolveResources(args.skill_tool_ids, resources.skills, 'skill_tool_ids') if (typeof skills === 'string') return new ErrorResult(skills) - Object.assign(skillFields, { skill_tool_ids: skills.map(({ id }) => id), skill_tool_list: skills }) + Object.assign(skillFields, { skill_tool_ids: skills }) } if (args.mcp_tool_ids !== undefined) { const mcps = resolveResources(args.mcp_tool_ids, resources.mcps, 'mcp_tool_ids') if (typeof mcps === 'string') return new ErrorResult(mcps) - Object.assign(skillFields, { mcp_tool_ids: mcps.map(({ id }) => id), mcp_source: 'referencing' }) + Object.assign(skillFields, { mcp_tool_ids: mcps, mcp_source: 'referencing' }) + } + if (args.application_ids !== undefined) { + const applications = resolveResources(args.application_ids, resources.applications, 'application_ids') + if (typeof applications === 'string') return new ErrorResult(applications) + Object.assign(skillFields, { application_ids: applications }) } } @@ -202,7 +229,7 @@ function useAgent(context: NodeAgentContext) { export default { description: - '调用大模型生成回答,可流式输出给用户,可挂载工具 / Skills / MCP。面向用户的最终回答 is_result=true,中间加工步骤为 false;' + + '调用大模型生成回答,可流式输出给用户,可挂载工具 / Skills / MCP / 智能体。面向用户的最终回答 is_result=true,中间加工步骤为 false;' + '基于知识库回答时提示词同时引用检索结果 data 与用户问题,并要求仅依据已知信息作答;后一步加工时引用前一步的 answer', params, config, diff --git a/ui/src/workflow-canvas/nodes/ai-chat-node/component/resource-setting/index.vue b/ui/src/workflow-canvas/nodes/ai-chat-node/component/resource-setting/index.vue index 08afb414a98..a3f812cd9aa 100644 --- a/ui/src/workflow-canvas/nodes/ai-chat-node/component/resource-setting/index.vue +++ b/ui/src/workflow-canvas/nodes/ai-chat-node/component/resource-setting/index.vue @@ -57,15 +57,26 @@ function submitToolDialog(tools: (Partial & { id: string })[]) { else submitTools(tools) } -// 同时写回执行所需 ID 和回显快照,关闭后再次打开仍保留选择。 +// 节点只保存执行所需 ID;弹窗返回的资源仅在本组件内用于回显,补足选项列表尚未包含的新选资源。 +const pickedTools = ref<(Partial & { id: string })[]>([]) +const pickedApplications = ref<(Partial & { id: string })[]>([]) + +function rememberPicked(picked: T[], resources: T[]) { + const resourceIds = new Set(resources.map(({ id }) => id)) + return [...picked.filter(({ id }) => !resourceIds.has(id)), ...cloneDeep(resources)] +} + function submitTools(tools: (Partial & { id: string })[]) { - updateSetting({ tool_ids: tools.map(({ id }) => id), tool_list: cloneDeep(tools) }) + pickedTools.value = rememberPicked(pickedTools.value, tools) + updateSetting({ tool_ids: tools.map(({ id }) => id) }) } function submitSkills(tools: (Partial & { id: string })[]) { - updateSetting({ skill_tool_ids: tools.map(({ id }) => id), skill_tool_list: cloneDeep(tools) }) + pickedTools.value = rememberPicked(pickedTools.value, tools) + updateSetting({ skill_tool_ids: tools.map(({ id }) => id) }) } function submitApplications(applications: (Partial & { id: string })[]) { - updateSetting({ application_ids: applications.map(({ id }) => id), application_list: cloneDeep(applications) }) + pickedApplications.value = rememberPicked(pickedApplications.value, applications) + updateSetting({ application_ids: applications.map(({ id }) => id) }) } const loadedMcpOptions = ref([]) @@ -78,22 +89,18 @@ const selectedMcpTools = computed(() => ) const mcpCount = computed(() => props.setting.mcp_tool_ids.length + Number(Boolean(props.setting.mcp_servers))) -const selectedTools = computed(() => - props.setting.tool_ids.map((id) => { - const tool = props.toolOptions.find((option) => option.id === id) ?? props.setting.tool_list?.find((option) => option.id === id) - return { ...tool, id, icon: tool?.icon, tool_type: tool?.tool_type, name: tool?.name } - }), -) -const selectedSkills = computed(() => - props.setting.skill_tool_ids.map((id) => { - const tool = props.skillOptions.find((option) => option.id === id) ?? props.setting.skill_tool_list?.find((option) => option.id === id) - return { ...tool, id, icon: tool?.icon, tool_type: tool?.tool_type, name: tool?.name } - }), -) +// 按 ID 从资源选项中回显,找不到时退回本次弹窗返回的资源;均缺失时只保留 ID。 +function resolveTool(options: ToolResourceOption[], id: string): Partial & { id: string } { + const tool = options.find((option) => option.id === id) ?? pickedTools.value.find((option) => option.id === id) + return { ...tool, id, icon: tool?.icon, tool_type: tool?.tool_type, name: tool?.name } +} + +const selectedTools = computed(() => props.setting.tool_ids.map((id) => resolveTool(props.toolOptions, id))) +const selectedSkills = computed(() => props.setting.skill_tool_ids.map((id) => resolveTool(props.skillOptions, id))) const selectedApplications = computed(() => props.setting.application_ids.map((id) => { const application = - props.applicationOptions.find((option) => option.id === id) ?? props.setting.application_list?.find((option) => option.id === id) + props.applicationOptions.find((option) => option.id === id) ?? pickedApplications.value.find((option) => option.id === id) return { ...application, id, icon: application?.icon, name: application?.name } }), ) @@ -103,9 +110,6 @@ function updateSetting(changes: Partial) { } function removeId(field: 'application_ids' | 'mcp_tool_ids' | 'skill_tool_ids' | 'tool_ids', id: string) { - if (field === 'tool_ids') return submitTools(selectedTools.value.filter((resource) => resource.id !== id)) - if (field === 'skill_tool_ids') return submitSkills(selectedSkills.value.filter((resource) => resource.id !== id)) - if (field === 'application_ids') return submitApplications(selectedApplications.value.filter((resource) => resource.id !== id)) updateSetting({ [field]: props.setting[field].filter((resourceId) => resourceId !== id) }) } diff --git a/ui/src/workflow-canvas/nodes/ai-chat-node/index.vue b/ui/src/workflow-canvas/nodes/ai-chat-node/index.vue index a1eb9ceba2c..7f490f19cd6 100644 --- a/ui/src/workflow-canvas/nodes/ai-chat-node/index.vue +++ b/ui/src/workflow-canvas/nodes/ai-chat-node/index.vue @@ -1,12 +1,13 @@