Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 { InputVarType } from '@/app/components/workflow/types'
  9. const otherAllowedRegex = /^\w+$/
  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, keys?: string[]) => {
  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 (/\d/.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. export const hasDuplicateStr = (strArr: string[]) => {
  75. const strObj: Record<string, number> = {}
  76. strArr.forEach((str) => {
  77. if (strObj[str])
  78. strObj[str] += 1
  79. else
  80. strObj[str] = 1
  81. })
  82. return !!Object.keys(strObj).find(key => strObj[key] > 1)
  83. }
  84. const varRegex = /\{\{([a-zA-Z_]\w*)\}\}/g
  85. export const getVars = (value: string) => {
  86. if (!value)
  87. return []
  88. const keys = value.match(varRegex)?.filter((item) => {
  89. return ![CONTEXT_PLACEHOLDER_TEXT, HISTORY_PLACEHOLDER_TEXT, QUERY_PLACEHOLDER_TEXT, PRE_PROMPT_PLACEHOLDER_TEXT].includes(item)
  90. }).map((item) => {
  91. return item.replace('{{', '').replace('}}', '')
  92. }).filter(key => key.length <= MAX_VAR_KEY_LENGTH) || []
  93. const keyObj: Record<string, boolean> = {}
  94. // remove duplicate keys
  95. const res: string[] = []
  96. keys.forEach((key) => {
  97. if (keyObj[key])
  98. return
  99. keyObj[key] = true
  100. res.push(key)
  101. })
  102. return res
  103. }
  104. // Set the value of basePath
  105. // example: /dify
  106. export const basePath = '/ss-dify'
  107. export function getMarketplaceUrl(path: string, params?: Record<string, string | undefined>) {
  108. const searchParams = new URLSearchParams({ source: encodeURIComponent(window.location.origin) })
  109. if (params) {
  110. Object.keys(params).forEach((key) => {
  111. const value = params[key]
  112. if (value !== undefined && value !== null)
  113. searchParams.append(key, value)
  114. })
  115. }
  116. return `${MARKETPLACE_URL_PREFIX}${path}?${searchParams.toString()}`
  117. }
  118. export const replaceSpaceWithUnderscoreInVarNameInput = (input: HTMLInputElement) => {
  119. const start = input.selectionStart
  120. const end = input.selectionEnd
  121. input.value = input.value.replaceAll(' ', '_')
  122. if (start !== null && end !== null)
  123. input.setSelectionRange(start, end)
  124. }