model-config.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import type { UserInputFormItem } from '@/types/app'
  2. import type { PromptVariable } from '@/models/debug'
  3. export const userInputsFormToPromptVariables = (useInputs: UserInputFormItem[] | null, dataset_query_variable?: string) => {
  4. if (!useInputs)
  5. return []
  6. const promptVariables: PromptVariable[] = []
  7. useInputs.forEach((item: any) => {
  8. const isParagraph = !!item.paragraph
  9. const [type, content] = (() => {
  10. if (isParagraph)
  11. return ['paragraph', item.paragraph]
  12. if (item['text-input'])
  13. return ['string', item['text-input']]
  14. return ['select', item.select]
  15. })()
  16. const is_context_var = dataset_query_variable === content.variable
  17. if (type === 'string' || type === 'paragraph') {
  18. promptVariables.push({
  19. key: content.variable,
  20. name: content.label,
  21. required: content.required,
  22. type,
  23. max_length: content.max_length,
  24. options: [],
  25. is_context_var,
  26. })
  27. }
  28. else {
  29. promptVariables.push({
  30. key: content.variable,
  31. name: content.label,
  32. required: content.required,
  33. type: 'select',
  34. options: content.options,
  35. is_context_var,
  36. })
  37. }
  38. })
  39. return promptVariables
  40. }
  41. export const promptVariablesToUserInputsForm = (promptVariables: PromptVariable[]) => {
  42. const userInputs: UserInputFormItem[] = []
  43. promptVariables.filter(({ key, name }) => {
  44. if (key && key.trim() && name && name.trim())
  45. return true
  46. return false
  47. }).forEach((item: any) => {
  48. if (item.type === 'string' || item.type === 'paragraph') {
  49. userInputs.push({
  50. [item.type === 'string' ? 'text-input' : 'paragraph']: {
  51. label: item.name,
  52. variable: item.key,
  53. required: item.required !== false, // default true
  54. max_length: item.max_length,
  55. default: '',
  56. },
  57. } as any)
  58. }
  59. else {
  60. userInputs.push({
  61. select: {
  62. label: item.name,
  63. variable: item.key,
  64. required: item.required !== false, // default true
  65. options: item.options,
  66. default: '',
  67. },
  68. } as any)
  69. }
  70. })
  71. return userInputs
  72. }