NativeApp.tsx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import { apiInterceptors, getAppStrategyValues, getNativeAppScenes, getResource } from '@/client/api';
  2. import AppDefaultIcon from '@/new-components/common/AppDefaultIcon';
  3. import { ParamNeed } from '@/types/app';
  4. import { useRequest } from 'ahooks';
  5. import { Form, InputNumber, Select, Tooltip } from 'antd';
  6. import cls from 'classnames';
  7. import React, { useEffect, useMemo } from 'react';
  8. import { useTranslation } from 'react-i18next';
  9. interface TeamContext {
  10. scene_name?: string;
  11. chat_scene?: string;
  12. }
  13. interface InitValueProps {
  14. team_context: TeamContext;
  15. param_need: ParamNeed[];
  16. }
  17. interface FormProps {
  18. chat_scene?: string;
  19. bind_value?: string;
  20. model?: string;
  21. temperature?: number;
  22. }
  23. const NativeApp: React.FC<{
  24. updateData: (data: [boolean, [TeamContext, ParamNeed[]]]) => void;
  25. classNames?: string;
  26. initValue?: InitValueProps;
  27. }> = ({ classNames, initValue, updateData }) => {
  28. const { t } = useTranslation();
  29. const [form] = Form.useForm<FormProps>();
  30. const chatScene = Form.useWatch('chat_scene', form);
  31. const bindValue = Form.useWatch('bind_value', form);
  32. const model = Form.useWatch('model', form);
  33. const temperature = Form.useWatch('temperature', form);
  34. const { team_context, param_need } = initValue || {};
  35. // 获取应用类型&模型
  36. const { data, loading } = useRequest(async () => {
  37. const res = await Promise.all([
  38. apiInterceptors(getNativeAppScenes()),
  39. apiInterceptors(getAppStrategyValues('priority')),
  40. ]);
  41. const [types, models] = res;
  42. form.setFieldValue('chat_scene', team_context?.chat_scene);
  43. form.setFieldValue('model', param_need?.find(param => param.type === 'model')?.value);
  44. form.setFieldValue('temperature', param_need?.find(param => param.type === 'temperature')?.value);
  45. await run(param_need?.find(param => param.type === 'resource')?.value || '');
  46. return [types, models] ?? [];
  47. });
  48. // 获取资源类型参数列表
  49. const {
  50. data: options,
  51. loading: paramsLoading,
  52. run,
  53. } = useRequest(
  54. async (type: string) => {
  55. const [, res] = await apiInterceptors(getResource({ type }));
  56. if (chatScene === team_context?.chat_scene && param_need?.find(param => param.type === 'resource')?.bind_value) {
  57. form.setFieldsValue({
  58. bind_value: param_need?.find(param => param.type === 'resource')?.bind_value,
  59. });
  60. }
  61. return (
  62. res?.map(item => {
  63. return {
  64. ...item,
  65. value: item.key,
  66. };
  67. }) ?? []
  68. );
  69. },
  70. { manual: true },
  71. );
  72. // 应用类型选项
  73. const appTypeOptions = useMemo(() => {
  74. const types = data?.[0]?.[1];
  75. return (
  76. types?.map((type: any) => {
  77. return {
  78. ...type,
  79. label: (
  80. <div className='flex items-center gap-1'>
  81. <AppDefaultIcon width={4} height={4} scene={type.chat_scene} />
  82. <Tooltip title={`资源类型${type.param_need.find((param: any) => param.type === 'resource')?.value}`}>
  83. <span className='text-[#525964] dark:text-[rgba(255,255,255,0.65)] ml-1'>{type.scene_name}</span>
  84. </Tooltip>
  85. </div>
  86. ),
  87. value: type.chat_scene,
  88. };
  89. }) || []
  90. );
  91. }, [data]);
  92. // 将数据实时返回给消费组件
  93. useEffect(() => {
  94. const rawVal = form.getFieldsValue();
  95. updateData([
  96. loading,
  97. [
  98. {
  99. chat_scene: rawVal.chat_scene,
  100. scene_name: appTypeOptions.find(type => type.chat_scene === rawVal.chat_scene)?.scene_name,
  101. },
  102. [
  103. { type: 'model', value: rawVal.model },
  104. { type: 'temperature', value: rawVal.temperature },
  105. {
  106. type: 'resource',
  107. value: appTypeOptions
  108. .find(type => type.chat_scene === rawVal.chat_scene)
  109. ?.param_need?.find((param: any) => param.type === 'resource')?.value,
  110. bind_value: rawVal.bind_value,
  111. },
  112. ],
  113. ],
  114. ]);
  115. }, [form, chatScene, bindValue, model, temperature, updateData, appTypeOptions, loading]);
  116. useEffect(() => {
  117. const type = (data?.[0]?.[1]?.find((type: any) => type.chat_scene === chatScene) as any)?.param_need?.find(
  118. (param: any) => param.type === 'resource',
  119. )?.value;
  120. run(type || '');
  121. }, [chatScene, data, run]);
  122. return (
  123. <div className={cls(classNames)}>
  124. <Form<FormProps>
  125. form={form}
  126. autoComplete='off'
  127. style={{ width: '100%' }}
  128. labelCol={{ span: 3 }}
  129. wrapperCol={{ span: 21 }}
  130. >
  131. <Form.Item label={t('native_type')} tooltip name='chat_scene'>
  132. <Select
  133. className='w-1/2'
  134. options={appTypeOptions}
  135. placeholder={t('app_type_select')}
  136. onChange={() => form.setFieldsValue({ bind_value: undefined })}
  137. />
  138. </Form.Item>
  139. {chatScene !== 'chat_excel' && (
  140. <Form.Item label={t('Arguments')} name='bind_value'>
  141. <Select
  142. placeholder={t('please_select_param')}
  143. allowClear
  144. className='w-1/2'
  145. options={options}
  146. loading={paramsLoading}
  147. />
  148. </Form.Item>
  149. )}
  150. <Form.Item label={t('model')} tooltip name='model'>
  151. <Select
  152. placeholder={t('please_select_model')}
  153. allowClear
  154. options={data?.[1]?.[1]?.map(item => ({
  155. label: item,
  156. value: item,
  157. }))}
  158. className='w-1/2'
  159. />
  160. </Form.Item>
  161. <Form.Item label={t('temperature')} tooltip name='temperature'>
  162. <InputNumber className='w-1/5 h-8' max={1} min={0} step={0.1} placeholder={t('please_input_temperature')} />
  163. </Form.Item>
  164. </Form>
  165. </div>
  166. );
  167. };
  168. export default NativeApp;