Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 20 additions & 15 deletions ui/src/workflow-canvas/nodes/llm-node/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ const params: JsonObjectSchema = {
type: 'object',
properties: {
materials: {
type: 'array',
items: {},
description: '传给模型的上下文素材:用户问题、检索结果、上游输出等',
type: 'any',
description: '传给模型作上下文的单个变量(上下文):可为字符串、数组或对象,如用户问题、检索结果、上游输出;绑定后写到 context_variable',
'x-bind': 'context_variable',
},
model_reference: {
Expand All @@ -56,7 +55,7 @@ const config: JsonObjectSchema = {
model_params_setting: { type: 'object', description: '模型参数(temperature 等),按所选模型的可用参数设置,不传则保持默认' },
system: { type: 'string', description: '系统提示词(角色设定),写清身份、目标、回答规范与限制' },
prompt: { type: 'string', description: '用户提示词,以 {{节点名称.字段}} 引用入参 materials' },
enable_context: { type: 'boolean', description: '是否携带已绑定入参 materials 作为上下文,绑定后默认开启' },
enable_context: { type: 'boolean', description: '是否携带入参 materials 作为上下文(写到 context_variable);绑定 materials 时默认开启,未绑定则不能开启' },
tools_location: { type: 'string', enum: ['customize', 'reference'], description: '工具声明来源:customize 用下方 tools 声明函数,reference 引用入参 tools_reference 的上游工具定义' },
tools: {
type: 'array',
Expand All @@ -72,6 +71,10 @@ const config: JsonObjectSchema = {
description: '模型可调用的工具声明(Function Calling),调用请求经 tool_calls 返回、不在本节点执行。传空数组表示清空',
},
is_result: { type: 'boolean', description: '是否把回答输出给用户:最终回答为 true,中间加工为 false' },
enable_exception: {
type: 'boolean',
description: '是否打开「异常捕获」:开启后节点多一个「异常输出」口,输出 exception_message,可接错误处理节点。默认关闭',
},
},
required: ['model_type'],
}
Expand Down Expand Up @@ -109,21 +112,14 @@ function useAgent(context: NodeAgentContext) {
: START_NODE_REFERENCES.question
return useNodeAgent(context, {
prompt: [
'llm-node(大语言模型)配置要点:',
'- model_type:default 用工作流默认模型(已配置时首选);custom 用 get_models 选模型 id;reference 引用上游模型变量(需绑定入参 model_reference)。',
'- system:角色设定,写清身份、目标、回答规范与限制(按需求生成完整内容,不要留占位)。',
'- prompt:用户提示词,按已绑定入参 materials 给出的 {{节点名称.字段}} 写法引用上下文素材。',
'- enable_context:携已绑定入参 materials 作上下文,绑定时默认开启,无需配置。',
'- 工具声明:需求要求模型可调用工具(Function Calling)时,在 tools 里以 {name,description,parameters} 声明函数(parameters 为 JSON Schema,写清字段与类型);调用请求经 tool_calls 返回、不在本节点执行。工具声明通过本 update 工具写入,属于节点配置,不需要父节点的入参。',
'- tools_location:默认 customize(在 tools 里直接声明);仅当父节点已绑定入参 tools_reference 提供工具定义时才用 reference。',
'- model_params_setting:按所选模型设置 temperature 等参数,未提及时保持默认。',
'- is_result:作为流程最终回答时为 true,作为中间加工步骤时为 false。',
'llm-node(大语言模型):单次调用大模型生成回答。model 选模型(默认用工作流默认模型,没有默认或要指定时 get_models 选 id,引用上游变量用 reference)。',
'要引用上游变量时用 get_field_options 查看可引用变量,在 prompt 里按它们给出的 {{节点名称.字段}} 写法引用;绑定的入参 materials 作为节点上下文自动写入 context_variable。需要模型能调用工具时在 tools 里声明函数。is_result:最终回答为 true,中间加工为 false。',
].join('\n'),
tools: [
{
name: 'update',
description:
'配置当前大语言模型节点:模型来源与模型参数、系统/用户提示词、是否携带上下文、工具声明、是否输出给用户。只传要设置的字段,其余保持不变。' +
'配置当前大语言模型节点:模型、系统/用户提示词、是否携带上下文、工具声明、是否输出给用户、是否开启异常捕获。只传要设置的字段,其余保持不变。' +
'写入后自动校验,通过即配置完成并交付本节点的输出字段;不通过返回 error 继续修。',
parameters: config,
apply: async (args) => {
Expand Down Expand Up @@ -164,17 +160,26 @@ function useAgent(context: NodeAgentContext) {
toolsFields.tools = { location: 'customize', reference: [], tools }
}

// 上下文:context_variable 是单个 [节点ID, 字段] 引用,来自入参 materials;与 enable_context 一起原子写入
const enableContext = typeof args.enable_context === 'boolean' ? args.enable_context : materialRefs.length ? true : Boolean(nodeData.enable_context)
const contextVariable = materialRefs.length ? [...materialRefs[0]!] : Array.isArray(nodeData.context_variable) ? [...nodeData.context_variable] : []
if (enableContext && !contextVariable.length) {
return new ErrorResult('开启「上下文」需要先绑定入参 materials(上游上下文变量),请让父节点传入该入参,或保持上下文关闭')
}

writeNodeProperties(node, {
node_data: {
...nodeData,
...modelFields,
...toolsFields,
system: typeof args.system === 'string' && args.system ? args.system : (nodeData.system ?? ''),
prompt: typeof args.prompt === 'string' && args.prompt ? args.prompt : defaultPrompt,
enable_context: typeof args.enable_context === 'boolean' ? args.enable_context : (materialRefs.length ? true : (nodeData.enable_context ?? false)),
enable_context: enableContext,
context_variable: contextVariable,
is_result: typeof args.is_result === 'boolean' ? args.is_result : (nodeData.is_result ?? true),
...(args.model_params_setting !== undefined ? { model_params_setting: args.model_params_setting } : {}),
},
enableException: typeof args.enable_exception === 'boolean' ? args.enable_exception : Boolean(node.properties.enableException),
})
if (defaultMissing) {
return new ErrorResult(

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review

1. Bugs / correctness

a) Hard failure on unrelated updates (regression risk)

if (enableContext && !contextVariable.length) {
  return new ErrorResult('开启「上下文」需要先绑定入参 materials ...')
}

enableContext falls back to Boolean(nodeData.enable_context). For any node that already has enable_context === true but no materials binding (legacy nodes, or nodes configured before this change), every update call now fails — including updates to prompt, tools, is_result, etc. — until the user either binds materials or explicitly passes enable_context: false. That breaks the documented contract of update ("只传要设置的字段,其余保持不变") and is a silent behavior break for existing workflows.

Fix: only validate when the user is changing context state, e.g.

const contextExplicit = typeof args.enable_context === 'boolean' || materialRefs.length > 0
if (contextExplicit && enableContext && !contextVariable.length) { ... }

or auto-disable instead of erroring:

const enableContext = materialRefs.length ? (args.enable_context ?? true) : false

b) Silent data loss with multiple materials refs

const contextVariable = materialRefs.length ? [...materialRefs[0]!] : ...

context_variable is a single [nodeId, field] reference, but materialRefs is a list. If the parent binds more than one, all but the first are dropped without warning. Either:

  • error/warn when materialRefs.length > 1, or
  • change context_variable to an array and store all refs.

c) Silent clearing of an existing context_variable

: Array.isArray(nodeData.context_variable) ? [...nodeData.context_variable] : []

If context_variable is currently a non-array shape (legacy/object/string), Array.isArray is false → it becomes [], i.e. the value is wiped on an unrelated update. Safer: only write the key when you actually have something to write:

const patch: Record<string, unknown> = {}
if (materialRefs.length) patch.context_variable = [...materialRefs[0]!]
// otherwise leave the existing value untouched

d) materials type changed to an invalid JSON Schema type

- type: 'array', items: {}, description: '...'
+ type: 'any',

"any" is not a valid JSON Schema type. With Ajv (or any spec-compliant validator) this throws schema is invalid: data/properties/materials/type must be equal to one of the allowed values at compile time — the whole update tool would break. Use one of:

materials: { description: '...', 'x-bind': 'context_variable' }                 // untyped
materials: { type: ['string', 'array', 'object'], description: '...' }          // draft-07 style
materials: { anyOf: [{ type: 'string' }, { type: 'array' }, { type: 'object' }] }

If your validator is custom and accepts 'any', at least document that — it's non-standard and will confuse anyone reading it as JSON Schema.

e) Schema/doc contradiction
materials now says "可为字符串、数组或对象 … 绑定后写到 context_variable", but the comment says context_variable is a single [节点ID, 字段] reference. The description implies the caller can pass a literal value or multiple entries, which the implementation cannot honor. Align the wording with the actual constraint (a single bound upstream variable).

2. Type-safety / robustness

  • [...materialRefs[0]!] — the non-null assertion is required by noUncheckedIndexedAccess, but note the spread produces string[], not a tuple. If the backend expects exactly [nodeId, field], a ref like [nodeId, 'a', 'b'] will produce an invalid 3-element value. Destructure explicitly and validate length.
  • node.properties.enableException — will throw if properties is ever undefined. Use node.properties?.enableException. Also verify the field really lives under node.properties (camelCase) rather than node_data: everything else is node_data + snake_case, and this asymmetry is a likely source of a "setting silently doesn't persist" bug. Consider a helper (readExceptionFlag(node) / writeExceptionFlag(node, v)) so read and write locations can't drift.
  • Boolean(nodeData.enable_context) vs the previous (nodeData.enable_context ?? false) is equivalent — no issue, just noise in the diff.
  • Nested ternary:
typeof args.enable_context === 'boolean' ? args.enable_context : materialRefs.length ? true : Boolean(...)

Correct but trips no-nested-ternary; prefer:

const enableContext =
  typeof args.enable_context === 'boolean' ? args.enable_context
  : materialRefs.length ? true
  : Boolean(nodeData.enable_context)
  • context_variable is written even when enable_context is false. Decide whether that's intentional (keeps binding for a later toggle) or should be cleared; document it either way.

3. Prompt / tool-description consistency

  • The condensed system prompt drops guidance for model_params_setting, tools_location, and enable_context. The model now has no instruction on those fields beyond the schema descriptions — acceptable, but for tools_location ("customize" vs "reference") and model_params_setting the schema text is terse; consider keeping a one-line hint or the model will guess.
  • The prompt references get_field_options, and the update description promises auto-validation — make sure get_field_options is actually registered in this agent's tools array (the diff only shows update). If it isn't, the model will call a non-existent tool.
  • enable_exception is added to the schema and to the update description, but the system prompt never mentions exception handling. Minor, but worth a clause so the model knows when to enable it.

4. Optimization suggestions

  1. Replace the ad-hoc boolean/normalization logic with small helpers, e.g. pickBoolean(args.x, fallback) and toContextRef(materialRefs).
  2. Build a single patch object and write once, so you can omit keys you don't intend to change (context_variable, model_params_setting) instead of always writing defaults — reduces accidental overwrites and diff noise.
  3. Add a console.warn/telemetry when materialRefs.length > 1 and only the first is used, so the truncation is observable.
  4. For enable_exception, prefer reading the same source you write to, and add a unit test asserting update({prompt}) does not mutate enableException or context_variable.
  5. Add tests for the four cases: (i) materials bound + no enable_context arg → on; (ii) materials unbound + enable_context: true → error; (iii) legacy node with enable_context: true, no materials, unrelated update → should succeed; (iv) multiple materials → defined behavior (error or all stored).
  6. Error message could include the concrete remedy ("bind materials or pass enable_context: false") — it partially does; make it explicit that false is allowed.

Expand Down
Loading