Files
gsfdc/demo-dify/dify/web/app/components/rag-pipeline/utils/nodes.ts
T
wuzilongandCursor c2cb845a32 feat(dify): 把本地控制台的中文与去外链改动入库
这些界面改动和专家服务草稿停在工作区,没有随上一笔主分支提交进入仓库。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-23 09:44:09 +08:00

150 lines
3.8 KiB
TypeScript

import type { Viewport } from 'reactflow'
import type { NoteNodeType } from '@/app/components/workflow/note-node/types'
import type { Node } from '@/app/components/workflow/types'
import {
CUSTOM_NODE,
NODE_WIDTH_X_OFFSET,
START_INITIAL_POSITION,
} from '@/app/components/workflow/constants'
import { CUSTOM_DATA_SOURCE_EMPTY_NODE } from '@/app/components/workflow/nodes/data-source-empty/constants'
import { CUSTOM_NOTE_NODE } from '@/app/components/workflow/note-node/constants'
import { NoteTheme } from '@/app/components/workflow/note-node/types'
import { BlockEnum } from '@/app/components/workflow/types'
import { generateNewNode } from '@/app/components/workflow/utils'
const blankPipelineNote = JSON.stringify({
root: {
children: [
{
children: [
{
detail: 0,
format: 1,
mode: 'normal',
style: 'font-size: 14px;',
text: '从空白流水线开始',
type: 'text',
version: 1,
},
],
direction: 'ltr',
format: '',
indent: 0,
type: 'paragraph',
version: 1,
textFormat: 1,
textStyle: 'font-size: 14px;',
},
{
children: [],
direction: 'ltr',
format: '',
indent: 0,
type: 'paragraph',
version: 1,
textFormat: 1,
textStyle: '',
},
{
children: [
{
detail: 0,
format: 0,
mode: 'normal',
style: '',
text: '知识流水线以数据源为起点,以知识库节点为终点。一般步骤是:从数据源导入文档 → 用提取器提取文档内容 → 拆分并清洗为结构化分段 → 存入知识库。',
type: 'text',
version: 1,
},
],
direction: 'ltr',
format: '',
indent: 0,
type: 'paragraph',
version: 1,
textFormat: 0,
textStyle: '',
},
],
direction: 'ltr',
format: '',
indent: 0,
type: 'root',
version: 1,
textFormat: 1,
textStyle: 'font-size: 14px;',
},
})
export const processNodesWithoutDataSource = (nodes: Node[], viewport?: Viewport) => {
let leftNode
for (let i = 0; i < nodes.length; i++) {
const node = nodes[i]
if (node!.data.type === BlockEnum.DataSource) {
return {
nodes,
viewport,
}
}
if (node!.type === CUSTOM_NODE && !leftNode) leftNode = node
if (node!.type === CUSTOM_NODE && leftNode && node!.position.x < leftNode.position.x)
leftNode = node
}
if (leftNode) {
const startX = leftNode.position.x - NODE_WIDTH_X_OFFSET
const startY = leftNode.position.y
const { newNode } = generateNewNode({
id: 'data-source-empty',
type: CUSTOM_DATA_SOURCE_EMPTY_NODE,
data: {
title: '',
desc: '',
type: BlockEnum.DataSourceEmpty,
width: 240,
_isTempNode: true,
},
position: {
x: startX,
y: startY,
},
})
const newNoteNode = generateNewNode({
id: 'note',
type: CUSTOM_NOTE_NODE,
data: {
title: '',
desc: '',
type: '' as any,
text: blankPipelineNote,
theme: NoteTheme.blue,
author: '',
showAuthor: true,
width: 240,
height: 220,
_isTempNode: true,
} as NoteNodeType,
position: {
x: startX,
y: startY + 100,
},
}).newNode
return {
nodes: [newNode, newNoteNode, ...nodes],
viewport: {
x: (START_INITIAL_POSITION.x - startX) * (viewport?.zoom || 1),
y: (START_INITIAL_POSITION.y - startY) * (viewport?.zoom || 1),
zoom: viewport?.zoom || 1,
},
}
}
return {
nodes,
viewport,
}
}