您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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) => {
  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. const varRegex = /\{\{([a-zA-Z_]\w*)\}\}/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. }
  94. // Set the value of basePath
  95. // example: /dify
  96. export const basePath = '/ss-dify'
  97. export function getMarketplaceUrl(path: string, params?: Record<string, string | undefined>) {
  98. const searchParams = new URLSearchParams({ source: encodeURIComponent(window.location.origin) })
  99. if (params) {
  100. Object.keys(params).forEach((key) => {
  101. const value = params[key]
  102. if (value !== undefined && value !== null)
  103. searchParams.append(key, value)
  104. })
  105. }
  106. return `${MARKETPLACE_URL_PREFIX}${path}?${searchParams.toString()}`
  107. }
  108. export const replaceSpaceWithUnderscreInVarNameInput = (input: HTMLInputElement) => {
  109. const start = input.selectionStart
  110. const end = input.selectionEnd
  111. input.value = input.value.replaceAll(' ', '_')
  112. if (start !== null && end !== null)
  113. input.setSelectionRange(start, end)
  114. }