2024-09-08 13:14:11 +08:00
|
|
|
import { MAX_VAR_KEY_LENGTH, VAR_ITEM_TEMPLATE, VAR_ITEM_TEMPLATE_IN_WORKFLOW, getMaxVarNameLength } from '@/config'
|
2023-10-12 23:14:28 +08:00
|
|
|
import { CONTEXT_PLACEHOLDER_TEXT, HISTORY_PLACEHOLDER_TEXT, PRE_PROMPT_PLACEHOLDER_TEXT, QUERY_PLACEHOLDER_TEXT } from '@/app/components/base/prompt-editor/constants'
|
2024-04-08 18:51:46 +08:00
|
|
|
import { InputVarType } from '@/app/components/workflow/types'
|
2024-01-29 09:58:29 +08:00
|
|
|
|
2023-09-27 08:54:52 +08:00
|
|
|
const otherAllowedRegex = /^[a-zA-Z0-9_]+$/
|
2024-01-29 09:58:29 +08:00
|
|
|
|
2024-01-23 19:31:56 +08:00
|
|
|
export const getNewVar = (key: string, type: string) => {
|
2024-01-29 09:58:29 +08:00
|
|
|
const { max_length, ...rest } = VAR_ITEM_TEMPLATE
|
|
|
|
if (type !== 'string') {
|
|
|
|
return {
|
|
|
|
...rest,
|
|
|
|
type: type || 'string',
|
|
|
|
key,
|
|
|
|
name: key.slice(0, getMaxVarNameLength(key)),
|
|
|
|
}
|
|
|
|
}
|
2023-05-15 08:51:32 +08:00
|
|
|
return {
|
|
|
|
...VAR_ITEM_TEMPLATE,
|
2024-01-23 19:31:56 +08:00
|
|
|
type: type || 'string',
|
2023-05-15 08:51:32 +08:00
|
|
|
key,
|
|
|
|
name: key.slice(0, getMaxVarNameLength(key)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-08 18:51:46 +08:00
|
|
|
export const getNewVarInWorkflow = (key: string, type = InputVarType.textInput) => {
|
|
|
|
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)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const checkKey = (key: string, canBeEmpty?: boolean) => {
|
2023-09-27 08:54:52 +08:00
|
|
|
if (key.length === 0 && !canBeEmpty)
|
2023-05-15 08:51:32 +08:00
|
|
|
return 'canNoBeEmpty'
|
2023-09-27 08:54:52 +08:00
|
|
|
|
|
|
|
if (canBeEmpty && key === '')
|
2023-05-15 08:51:32 +08:00
|
|
|
return true
|
2023-09-27 08:54:52 +08:00
|
|
|
|
2024-09-08 13:14:11 +08:00
|
|
|
if (key.length > MAX_VAR_KEY_LENGTH)
|
2023-05-15 08:51:32 +08:00
|
|
|
return 'tooLong'
|
2023-09-27 08:54:52 +08:00
|
|
|
|
2023-05-15 08:51:32 +08:00
|
|
|
if (otherAllowedRegex.test(key)) {
|
2023-09-27 08:54:52 +08:00
|
|
|
if (/[0-9]/.test(key[0]))
|
2023-05-15 08:51:32 +08:00
|
|
|
return 'notStartWithNumber'
|
2023-09-27 08:54:52 +08:00
|
|
|
|
2023-05-15 08:51:32 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return 'notValid'
|
|
|
|
}
|
|
|
|
|
|
|
|
export const checkKeys = (keys: string[], canBeEmpty?: boolean) => {
|
|
|
|
let isValid = true
|
|
|
|
let errorKey = ''
|
|
|
|
let errorMessageKey = ''
|
|
|
|
keys.forEach((key) => {
|
2023-09-27 08:54:52 +08:00
|
|
|
if (!isValid)
|
2023-05-15 08:51:32 +08:00
|
|
|
return
|
2023-09-27 08:54:52 +08:00
|
|
|
|
2023-05-15 08:51:32 +08:00
|
|
|
const res = checkKey(key, canBeEmpty)
|
|
|
|
if (res !== true) {
|
|
|
|
isValid = false
|
|
|
|
errorKey = key
|
|
|
|
errorMessageKey = res
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return { isValid, errorKey, errorMessageKey }
|
2023-09-27 08:54:52 +08:00
|
|
|
}
|
2023-10-12 23:14:28 +08:00
|
|
|
|
|
|
|
const varRegex = /\{\{([a-zA-Z_][a-zA-Z0-9_]*)\}\}/g
|
|
|
|
export const getVars = (value: string) => {
|
2023-12-11 10:06:01 +08:00
|
|
|
if (!value)
|
|
|
|
return []
|
|
|
|
|
2023-10-12 23:14:28 +08:00
|
|
|
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('}}', '')
|
2024-09-08 13:14:11 +08:00
|
|
|
}).filter(key => key.length <= MAX_VAR_KEY_LENGTH) || []
|
2023-10-12 23:14:28 +08:00
|
|
|
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
|
|
|
|
}
|