dify/web/utils/var.ts
-LAN- 85cda47c70
feat: knowledge pipeline (#25360)
Signed-off-by: -LAN- <laipz8200@outlook.com>
Co-authored-by: twwu <twwu@dify.ai>
Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com>
Co-authored-by: jyong <718720800@qq.com>
Co-authored-by: Wu Tianwei <30284043+WTW0313@users.noreply.github.com>
Co-authored-by: QuantumGhost <obelisk.reg+git@gmail.com>
Co-authored-by: lyzno1 <yuanyouhuilyz@gmail.com>
Co-authored-by: quicksand <quicksandzn@gmail.com>
Co-authored-by: Jyong <76649700+JohnJyong@users.noreply.github.com>
Co-authored-by: lyzno1 <92089059+lyzno1@users.noreply.github.com>
Co-authored-by: zxhlyh <jasonapring2015@outlook.com>
Co-authored-by: Yongtao Huang <yongtaoh2022@gmail.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Joel <iamjoel007@gmail.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: nite-knite <nkCoding@gmail.com>
Co-authored-by: Hanqing Zhao <sherry9277@gmail.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: Harry <xh001x@hotmail.com>
2025-09-18 12:49:10 +08:00

148 lines
3.9 KiB
TypeScript

import { MARKETPLACE_URL_PREFIX, MAX_VAR_KEY_LENGTH, VAR_ITEM_TEMPLATE, VAR_ITEM_TEMPLATE_IN_WORKFLOW, getMaxVarNameLength } from '@/config'
import {
CONTEXT_PLACEHOLDER_TEXT,
HISTORY_PLACEHOLDER_TEXT,
PRE_PROMPT_PLACEHOLDER_TEXT,
QUERY_PLACEHOLDER_TEXT,
} from '@/app/components/base/prompt-editor/constants'
import type { InputVar } from '@/app/components/workflow/types'
import { InputVarType } from '@/app/components/workflow/types'
const otherAllowedRegex = /^\w+$/
export const getNewVar = (key: string, type: string) => {
const { ...rest } = VAR_ITEM_TEMPLATE
if (type !== 'string') {
return {
...rest,
type: type || 'string',
key,
name: key.slice(0, getMaxVarNameLength(key)),
}
}
return {
...VAR_ITEM_TEMPLATE,
type: type || 'string',
key,
name: key.slice(0, getMaxVarNameLength(key)),
}
}
export const getNewVarInWorkflow = (key: string, type = InputVarType.textInput): InputVar => {
const { max_length, ...rest } = VAR_ITEM_TEMPLATE_IN_WORKFLOW
if (type !== InputVarType.textInput) {
return {
...rest,
type,
variable: key,
label: key.slice(0, getMaxVarNameLength(key)),
}
}
return {
...VAR_ITEM_TEMPLATE_IN_WORKFLOW,
type,
variable: key,
label: key.slice(0, getMaxVarNameLength(key)),
placeholder: '',
default: '',
hint: '',
}
}
export const checkKey = (key: string, canBeEmpty?: boolean, keys?: string[]) => {
if (key.length === 0 && !canBeEmpty)
return 'canNoBeEmpty'
if (canBeEmpty && key === '')
return true
if (key.length > MAX_VAR_KEY_LENGTH)
return 'tooLong'
if (otherAllowedRegex.test(key)) {
if (/\d/.test(key[0]))
return 'notStartWithNumber'
return true
}
return 'notValid'
}
export const checkKeys = (keys: string[], canBeEmpty?: boolean) => {
let isValid = true
let errorKey = ''
let errorMessageKey = ''
keys.forEach((key) => {
if (!isValid)
return
const res = checkKey(key, canBeEmpty)
if (res !== true) {
isValid = false
errorKey = key
errorMessageKey = res
}
})
return { isValid, errorKey, errorMessageKey }
}
export const hasDuplicateStr = (strArr: string[]) => {
const strObj: Record<string, number> = {}
strArr.forEach((str) => {
if (strObj[str])
strObj[str] += 1
else
strObj[str] = 1
})
return !!Object.keys(strObj).find(key => strObj[key] > 1)
}
const varRegex = /\{\{([a-zA-Z_]\w*)\}\}/g
export const getVars = (value: string) => {
if (!value)
return []
const keys = value.match(varRegex)?.filter((item) => {
return ![CONTEXT_PLACEHOLDER_TEXT, HISTORY_PLACEHOLDER_TEXT, QUERY_PLACEHOLDER_TEXT, PRE_PROMPT_PLACEHOLDER_TEXT].includes(item)
}).map((item) => {
return item.replace('{{', '').replace('}}', '')
}).filter(key => key.length <= MAX_VAR_KEY_LENGTH) || []
const keyObj: Record<string, boolean> = {}
// remove duplicate keys
const res: string[] = []
keys.forEach((key) => {
if (keyObj[key])
return
keyObj[key] = true
res.push(key)
})
return res
}
// Set the value of basePath
// example: /dify
export const basePath = process.env.NEXT_PUBLIC_BASE_PATH || ''
export function getMarketplaceUrl(path: string, params?: Record<string, string | undefined>) {
const searchParams = new URLSearchParams({ source: encodeURIComponent(window.location.origin) })
if (params) {
Object.keys(params).forEach((key) => {
const value = params[key]
if (value !== undefined && value !== null)
searchParams.append(key, value)
})
}
return `${MARKETPLACE_URL_PREFIX}${path}?${searchParams.toString()}`
}
export const replaceSpaceWithUnderscoreInVarNameInput = (input: HTMLInputElement) => {
const start = input.selectionStart
const end = input.selectionEnd
input.value = input.value.replaceAll(' ', '_')
if (start !== null && end !== null)
input.setSelectionRange(start, end)
}