var.ts 2.6 KB

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