You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

var.ts 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import { MARKETPLACE_URL_PREFIX, 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 type { InputVar } from '@/app/components/workflow/types'
  9. import { InputVarType } from '@/app/components/workflow/types'
  10. const otherAllowedRegex = /^\w+$/
  11. export const getNewVar = (key: string, type: string) => {
  12. const { ...rest } = VAR_ITEM_TEMPLATE
  13. if (type !== 'string') {
  14. return {
  15. ...rest,
  16. type: type || 'string',
  17. key,
  18. name: key.slice(0, getMaxVarNameLength(key)),
  19. }
  20. }
  21. return {
  22. ...VAR_ITEM_TEMPLATE,
  23. type: type || 'string',
  24. key,
  25. name: key.slice(0, getMaxVarNameLength(key)),
  26. }
  27. }
  28. export const getNewVarInWorkflow = (key: string, type = InputVarType.textInput): InputVar => {
  29. const { max_length, ...rest } = VAR_ITEM_TEMPLATE_IN_WORKFLOW
  30. if (type !== InputVarType.textInput) {
  31. return {
  32. ...rest,
  33. type,
  34. variable: key,
  35. label: key.slice(0, getMaxVarNameLength(key)),
  36. }
  37. }
  38. return {
  39. ...VAR_ITEM_TEMPLATE_IN_WORKFLOW,
  40. type,
  41. variable: key,
  42. label: key.slice(0, getMaxVarNameLength(key)),
  43. placeholder: '',
  44. default: '',
  45. hint: '',
  46. }
  47. }
  48. export const checkKey = (key: string, canBeEmpty?: boolean, keys?: string[]) => {
  49. if (key.length === 0 && !canBeEmpty)
  50. return 'canNoBeEmpty'
  51. if (canBeEmpty && key === '')
  52. return true
  53. if (key.length > MAX_VAR_KEY_LENGTH)
  54. return 'tooLong'
  55. if (otherAllowedRegex.test(key)) {
  56. if (/\d/.test(key[0]))
  57. return 'notStartWithNumber'
  58. return true
  59. }
  60. return 'notValid'
  61. }
  62. export const checkKeys = (keys: string[], canBeEmpty?: boolean) => {
  63. let isValid = true
  64. let errorKey = ''
  65. let errorMessageKey = ''
  66. keys.forEach((key) => {
  67. if (!isValid)
  68. return
  69. const res = checkKey(key, canBeEmpty)
  70. if (res !== true) {
  71. isValid = false
  72. errorKey = key
  73. errorMessageKey = res
  74. }
  75. })
  76. return { isValid, errorKey, errorMessageKey }
  77. }
  78. export const hasDuplicateStr = (strArr: string[]) => {
  79. const strObj: Record<string, number> = {}
  80. strArr.forEach((str) => {
  81. if (strObj[str])
  82. strObj[str] += 1
  83. else
  84. strObj[str] = 1
  85. })
  86. return !!Object.keys(strObj).find(key => strObj[key] > 1)
  87. }
  88. const varRegex = /\{\{([a-zA-Z_]\w*)\}\}/g
  89. export const getVars = (value: string) => {
  90. if (!value)
  91. return []
  92. const keys = value.match(varRegex)?.filter((item) => {
  93. return ![CONTEXT_PLACEHOLDER_TEXT, HISTORY_PLACEHOLDER_TEXT, QUERY_PLACEHOLDER_TEXT, PRE_PROMPT_PLACEHOLDER_TEXT].includes(item)
  94. }).map((item) => {
  95. return item.replace('{{', '').replace('}}', '')
  96. }).filter(key => key.length <= MAX_VAR_KEY_LENGTH) || []
  97. const keyObj: Record<string, boolean> = {}
  98. // remove duplicate keys
  99. const res: string[] = []
  100. keys.forEach((key) => {
  101. if (keyObj[key])
  102. return
  103. keyObj[key] = true
  104. res.push(key)
  105. })
  106. return res
  107. }
  108. // Set the value of basePath
  109. // example: /dify
  110. export const basePath = process.env.NEXT_PUBLIC_BASE_PATH || ''
  111. export function getMarketplaceUrl(path: string, params?: Record<string, string | undefined>) {
  112. const searchParams = new URLSearchParams({ source: encodeURIComponent(window.location.origin) })
  113. if (params) {
  114. Object.keys(params).forEach((key) => {
  115. const value = params[key]
  116. if (value !== undefined && value !== null)
  117. searchParams.append(key, value)
  118. })
  119. }
  120. return `${MARKETPLACE_URL_PREFIX}${path}?${searchParams.toString()}`
  121. }
  122. export const replaceSpaceWithUnderscoreInVarNameInput = (input: HTMLInputElement) => {
  123. const start = input.selectionStart
  124. const end = input.selectionEnd
  125. input.value = input.value.replaceAll(' ', '_')
  126. if (start !== null && end !== null)
  127. input.setSelectionRange(start, end)
  128. }