dify/web/app/components/workflow/nodes/knowledge-retrieval-v2/default.ts
Jyong 57d03faa98 fix(workflow): let the KnowledgeFS retrieval node run on text or image alone
The KnowledgeFS retrieval node treated the query text as mandatory even though
a query image is a complete input on its own. Text and image are now
alternatives: the node needs at least one of them, never both.

- web: drop the required mark from the query text field; the checklist fails
  only when neither variable is bound (new i18n message in every locale).
- api: make `query_variable_selector` optional with a model-level rule that
  one of the two selectors must be set, and only map the query variable when
  it is bound. At runtime an empty text without images is a configuration
  error; an image-only query retrieves with an empty text, merges spaces on
  their own scores instead of a text reranker, skips automatic metadata
  extraction, and skips the text-based failed-query capture.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015zw5G5SX3HmVfnZof6YWAc
2026-09-03 01:52:19 -04:00

52 lines
1.9 KiB
TypeScript

import type { TFunction } from 'i18next'
import type { NodeDefault } from '../../types'
import type { KnowledgeRetrievalV2NodeType } from './types'
import { MetadataFilteringModeEnum } from '@/app/components/workflow/nodes/knowledge-retrieval/types'
import { BlockEnum } from '@/app/components/workflow/types'
import { genNodeMetaData } from '@/app/components/workflow/utils'
import { KNOWLEDGE_RETRIEVAL_V2_OUTPUT_STRUCT } from './constants'
const metaData = genNodeMetaData({
sort: 2.05,
type: BlockEnum.KnowledgeRetrievalV2,
})
const nodeDefault: NodeDefault<KnowledgeRetrievalV2NodeType> = {
metaData,
defaultValue: {
control_space_ids: [],
metadata_filtering_mode: MetadataFilteringModeEnum.disabled,
query_attachment_selector: [],
query_variable_selector: [],
score_threshold: null,
top_n: 10,
},
checkValid(payload, t: TFunction<['workflow', 'common']>) {
let errorMessage = ''
// Text and image are alternative query inputs: either one is enough, but not neither.
if (!payload.query_variable_selector?.length && !payload.query_attachment_selector?.length) {
errorMessage = t(($) => $['nodes.knowledgeRetrievalV2.queryInputRequired'], {
ns: 'workflow',
})
} else if (!payload.control_space_ids?.length) {
errorMessage = t(($) => $['errorMsg.fieldRequired'], {
ns: 'workflow',
field: t(($) => $['nodes.knowledgeRetrievalV2.knowledgeSpaces'], { ns: 'workflow' }),
})
} else if (
payload.metadata_filtering_mode === MetadataFilteringModeEnum.automatic &&
!payload.metadata_model_config?.provider
) {
errorMessage = t(($) => $['errorMsg.fieldRequired'], {
ns: 'workflow',
field: t(($) => $['modelProvider.systemReasoningModel.key'], { ns: 'common' }),
})
}
return { isValid: !errorMessage, errorMessage }
},
getOutputVars: () => KNOWLEDGE_RETRIEVAL_V2_OUTPUT_STRUCT,
}
export default nodeDefault