constants.tsx 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import type { ValueSelector } from '../../workflow/types'
  2. export const CONTEXT_PLACEHOLDER_TEXT = '{{#context#}}'
  3. export const HISTORY_PLACEHOLDER_TEXT = '{{#histories#}}'
  4. export const QUERY_PLACEHOLDER_TEXT = '{{#query#}}'
  5. export const PRE_PROMPT_PLACEHOLDER_TEXT = '{{#pre_prompt#}}'
  6. export const UPDATE_DATASETS_EVENT_EMITTER = 'prompt-editor-context-block-update-datasets'
  7. export const UPDATE_HISTORY_EVENT_EMITTER = 'prompt-editor-history-block-update-role'
  8. export const checkHasContextBlock = (text: string) => {
  9. if (!text)
  10. return false
  11. return text.includes(CONTEXT_PLACEHOLDER_TEXT)
  12. }
  13. export const checkHasHistoryBlock = (text: string) => {
  14. if (!text)
  15. return false
  16. return text.includes(HISTORY_PLACEHOLDER_TEXT)
  17. }
  18. export const checkHasQueryBlock = (text: string) => {
  19. if (!text)
  20. return false
  21. return text.includes(QUERY_PLACEHOLDER_TEXT)
  22. }
  23. /*
  24. * {{#1711617514996.name#}} => [1711617514996, name]
  25. * {{#1711617514996.sys.query#}} => [sys, query]
  26. */
  27. export const getInputVars = (text: string): ValueSelector[] => {
  28. const allVars = text.match(/{{#([^#]*)#}}/g)
  29. if (allVars && allVars?.length > 0) {
  30. // {{#context#}}, {{#query#}} is not input vars
  31. const inputVars = allVars
  32. .filter(item => item.includes('.'))
  33. .map((item) => {
  34. const valueSelector = item.replace('{{#', '').replace('#}}', '').split('.')
  35. if (valueSelector[1] === 'sys' && /^\d+$/.test(valueSelector[0]))
  36. return valueSelector.slice(1)
  37. return valueSelector
  38. })
  39. return inputVars
  40. }
  41. return []
  42. }