File size: 5,922 Bytes
4304c6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import type { FC } from 'react'
import React from 'react'
import { useTranslation } from 'react-i18next'
import {
  PlayIcon,
} from '@heroicons/react/24/solid'
import Select from '@/app/components/base/select'
import type { SiteInfo } from '@/models/share'
import type { PromptConfig } from '@/models/debug'
import Button from '@/app/components/base/button'
import { DEFAULT_VALUE_MAX_LEN } from '@/config'
import TextGenerationImageUploader from '@/app/components/base/image-uploader/text-generation-image-uploader'
import type { VisionFile, VisionSettings } from '@/types/app'

export type IRunOnceProps = {
  siteInfo: SiteInfo
  promptConfig: PromptConfig
  inputs: Record<string, any>
  onInputsChange: (inputs: Record<string, any>) => void
  onSend: () => void
  visionConfig: VisionSettings
  onVisionFilesChange: (files: VisionFile[]) => void
}
const RunOnce: FC<IRunOnceProps> = ({

  promptConfig,

  inputs,

  onInputsChange,

  onSend,

  visionConfig,

  onVisionFilesChange,

}) => {
  const { t } = useTranslation()

  const onClear = () => {
    const newInputs: Record<string, any> = {}
    promptConfig.prompt_variables.forEach((item) => {
      newInputs[item.key] = ''
    })
    onInputsChange(newInputs)
  }

  return (
    <div className="">

      <section>

        {/* input form */}

        <form>

          {promptConfig.prompt_variables.map(item => (

            <div className='w-full mt-4' key={item.key}>

              <label className='text-gray-900 text-sm font-medium'>{item.name}</label>

              <div className='mt-2'>

                {item.type === 'select' && (

                  <Select

                    className='w-full'

                    defaultValue={inputs[item.key]}

                    onSelect={(i) => { onInputsChange({ ...inputs, [item.key]: i.value }) }}

                    items={(item.options || []).map(i => ({ name: i, value: i }))}

                    allowSearch={false}

                    bgClassName='bg-gray-50'

                  />

                )}

                {item.type === 'string' && (

                  <input

                    type="text"

                    className="block w-full p-2 text-gray-900 border border-gray-300 rounded-lg bg-gray-50 sm:text-xs focus:ring-blue-500 focus:border-blue-500 "

                    placeholder={`${item.name}${!item.required ? `(${t('appDebug.variableTable.optional')})` : ''}`}

                    value={inputs[item.key]}

                    onChange={(e) => { onInputsChange({ ...inputs, [item.key]: e.target.value }) }}

                    onKeyDown={(e) => {

                      if (e.key === 'Enter') {

                        e.preventDefault()

                        onSend()

                      }

                    }}

                    maxLength={item.max_length || DEFAULT_VALUE_MAX_LEN}

                  />

                )}

                {item.type === 'paragraph' && (

                  <textarea

                    className="block w-full h-[104px] p-2 text-gray-900 border border-gray-300 rounded-lg bg-gray-50 sm:text-xs focus:ring-blue-500 focus:border-blue-500 "

                    placeholder={`${item.name}${!item.required ? `(${t('appDebug.variableTable.optional')})` : ''}`}

                    value={inputs[item.key]}

                    onChange={(e) => { onInputsChange({ ...inputs, [item.key]: e.target.value }) }}

                  />

                )}

                {item.type === 'number' && (

                  <input

                    type="number"

                    className="block w-full p-2 text-gray-900 border border-gray-300 rounded-lg bg-gray-50 sm:text-xs focus:ring-blue-500 focus:border-blue-500 "

                    placeholder={`${item.name}${!item.required ? `(${t('appDebug.variableTable.optional')})` : ''}`}

                    value={inputs[item.key]}

                    onChange={(e) => { onInputsChange({ ...inputs, [item.key]: e.target.value }) }}

                  />

                )}

              </div>

            </div>

          ))}

          {

            visionConfig?.enabled && (

              <div className="w-full mt-4">

                <div className="text-gray-900 text-sm font-medium">{t('common.imageUploader.imageUpload')}</div>

                <div className='mt-2'>

                  <TextGenerationImageUploader

                    settings={visionConfig}

                    onFilesChange={files => onVisionFilesChange(files.filter(file => file.progress !== -1).map(fileItem => ({

                      type: 'image',

                      transfer_method: fileItem.type,

                      url: fileItem.url,

                      upload_file_id: fileItem.fileId,

                    })))}

                  />

                </div>

              </div>

            )

          }

          {promptConfig.prompt_variables.length > 0 && (

            <div className='mt-4 h-[1px] bg-gray-100'></div>

          )}

          <div className='w-full mt-4'>

            <div className="flex items-center justify-between">

              <Button

                className='!h-8 !p-3'

                onClick={onClear}

                disabled={false}

              >

                <span className='text-[13px]'>{t('common.operation.clear')}</span>

              </Button>

              <Button

                type="primary"

                className='!h-8 !pl-3 !pr-4'

                onClick={onSend}

                disabled={false}

              >

                <PlayIcon className="shrink-0 w-4 h-4 mr-1" aria-hidden="true" />

                <span className='text-[13px]'>{t('share.generation.run')}</span>

              </Button>

            </div>

          </div>

        </form>

      </section>

    </div>
  )
}
export default React.memo(RunOnce)