var.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { MAX_VAR_KEY_LENGTH, VAR_ITEM_TEMPLATE, VAR_ITEM_TEMPLATE_IN_WORKFLOW, getMaxVarNameLength } from '@/config'
  2. import { CONTEXT_PLACEHOLDER_TEXT, HISTORY_PLACEHOLDER_TEXT, PRE_PROMPT_PLACEHOLDER_TEXT, QUERY_PLACEHOLDER_TEXT } from '@/app/components/base/prompt-editor/constants'
  3. import { InputVarType } from '@/app/components/workflow/types'
  4. const otherAllowedRegex = /^[a-zA-Z0-9_]+$/
  5. export const getNewVar = (key: string, type: string) => {
  6. const { max_length, ...rest } = VAR_ITEM_TEMPLATE
  7. if (type !== 'string') {
  8. return {
  9. ...rest,
  10. type: type || 'string',
  11. key,
  12. name: key.slice(0, getMaxVarNameLength(key)),
  13. }
  14. }
  15. return {
  16. ...VAR_ITEM_TEMPLATE,
  17. type: type || 'string',
  18. key,
  19. name: key.slice(0, getMaxVarNameLength(key)),
  20. }
  21. }
  22. export const getNewVarInWorkflow = (key: string, type = InputVarType.textInput) => {
  23. const { max_length, ...rest } = VAR_ITEM_TEMPLATE_IN_WORKFLOW
  24. if (type !== InputVarType.textInput) {
  25. return {
  26. ...rest,
  27. type,
  28. variable: key,
  29. label: key.slice(0, getMaxVarNameLength(key)),
  30. }
  31. }
  32. return {
  33. ...VAR_ITEM_TEMPLATE_IN_WORKFLOW,
  34. type,
  35. variable: key,
  36. label: key.slice(0, getMaxVarNameLength(key)),
  37. }
  38. }
  39. export const checkKey = (key: string, canBeEmpty?: boolean) => {
  40. if (key.length === 0 && !canBeEmpty)
  41. return 'canNoBeEmpty'
  42. if (canBeEmpty && key === '')
  43. return true
  44. if (key.length > MAX_VAR_KEY_LENGTH)
  45. return 'tooLong'
  46. if (otherAllowedRegex.test(key)) {
  47. if (/[0-9]/.test(key[0]))
  48. return 'notStartWithNumber'
  49. return true
  50. }
  51. return 'notValid'
  52. }
  53. export const checkKeys = (keys: string[], canBeEmpty?: boolean) => {
  54. let isValid = true
  55. let errorKey = ''
  56. let errorMessageKey = ''
  57. keys.forEach((key) => {
  58. if (!isValid)
  59. return
  60. const res = checkKey(key, canBeEmpty)
  61. if (res !== true) {
  62. isValid = false
  63. errorKey = key
  64. errorMessageKey = res
  65. }
  66. })
  67. return { isValid, errorKey, errorMessageKey }
  68. }
  69. const varRegex = /\{\{([a-zA-Z_][a-zA-Z0-9_]*)\}\}/g
  70. export const getVars = (value: string) => {
  71. if (!value)
  72. return []
  73. const keys = value.match(varRegex)?.filter((item) => {
  74. return ![CONTEXT_PLACEHOLDER_TEXT, HISTORY_PLACEHOLDER_TEXT, QUERY_PLACEHOLDER_TEXT, PRE_PROMPT_PLACEHOLDER_TEXT].includes(item)
  75. }).map((item) => {
  76. return item.replace('{{', '').replace('}}', '')
  77. }).filter(key => key.length <= MAX_VAR_KEY_LENGTH) || []
  78. const keyObj: Record<string, boolean> = {}
  79. // remove duplicate keys
  80. const res: string[] = []
  81. keys.forEach((key) => {
  82. if (keyObj[key])
  83. return
  84. keyObj[key] = true
  85. res.push(key)
  86. })
  87. return res
  88. }