agent-panel.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import { apiInterceptors, getAppStrategy, getAppStrategyValues } from '@/client/api';
  2. import { Button, Input, Select } from 'antd';
  3. import { useEffect, useMemo, useState } from 'react';
  4. import { useTranslation } from 'react-i18next';
  5. import ResourceCard from './resource-card';
  6. interface IProps {
  7. resourceTypes: any;
  8. updateDetailsByAgentKey: (key: string, data: any) => void;
  9. detail: any;
  10. editResources?: any;
  11. }
  12. export default function AgentPanel(props: IProps) {
  13. const { resourceTypes, updateDetailsByAgentKey, detail, editResources } = props;
  14. const { t } = useTranslation();
  15. const [resources, setResources] = useState<any>([...(editResources ?? [])]);
  16. const [agent, setAgent] = useState<any>({ ...detail, resources: [] });
  17. const [strategyOptions, setStrategyOptions] = useState<any>([]);
  18. const [strategyValueOptions, setStrategyValueOptions] = useState<any>([]);
  19. const updateResourcesByIndex = (data: any, index: number) => {
  20. setResources((resources: any) => {
  21. const tempResources = [...resources];
  22. if (!data) {
  23. return tempResources.filter((_: any, indey) => index !== indey);
  24. }
  25. return tempResources.map((item: any, indey) => {
  26. if (index === indey) {
  27. return data;
  28. } else {
  29. return item;
  30. }
  31. });
  32. });
  33. };
  34. const getStrategy = async () => {
  35. const [_, data] = await apiInterceptors(getAppStrategy());
  36. if (data) {
  37. setStrategyOptions(data?.map(item => ({ label: item.name_cn, value: item.value })));
  38. }
  39. };
  40. const getStrategyValues = async (type: string) => {
  41. const [_, data] = await apiInterceptors(getAppStrategyValues(type));
  42. if (data) {
  43. setStrategyValueOptions(data.map(item => ({ label: item, value: item })) ?? []);
  44. }
  45. };
  46. const formatStrategyValue = (value: string) => {
  47. return !value ? [] : value.split(',');
  48. };
  49. useEffect(() => {
  50. getStrategy();
  51. getStrategyValues(detail.llm_strategy);
  52. }, []);
  53. useEffect(() => {
  54. updateAgent(resources, 'resources');
  55. }, [resources]);
  56. const updateAgent = (data: any, type: string) => {
  57. const tempAgent = { ...agent };
  58. tempAgent[type] = data;
  59. setAgent(tempAgent);
  60. updateDetailsByAgentKey(detail.key, tempAgent);
  61. };
  62. const handelAdd = () => {
  63. setResources([...resources, { name: '', type: '', introduce: '', value: '', is_dynamic: '' }]);
  64. };
  65. const resourceTypeOptions = useMemo(() => {
  66. return resourceTypes?.map((item: string) => {
  67. return {
  68. label: item,
  69. value: item,
  70. };
  71. });
  72. }, [resourceTypes]);
  73. return (
  74. <div>
  75. <div className='flex items-center mb-6 mt-6'>
  76. <div className='mr-2 w-16 text-center'>{t('Prompt')}:</div>
  77. <Input
  78. required
  79. className='mr-6 w-1/4'
  80. value={agent.prompt_template}
  81. onChange={e => {
  82. updateAgent(e.target.value, 'prompt_template');
  83. }}
  84. />
  85. <div className='mr-2'>{t('LLM_strategy')}:</div>
  86. <Select
  87. value={agent.llm_strategy}
  88. options={strategyOptions}
  89. className='w-1/6 mr-6'
  90. onChange={value => {
  91. updateAgent(value, 'llm_strategy');
  92. getStrategyValues(value);
  93. }}
  94. />
  95. {strategyValueOptions && strategyValueOptions.length > 0 && (
  96. <>
  97. <div className='mr-2'>{t('LLM_strategy_value')}:</div>
  98. <Select
  99. value={formatStrategyValue(agent.llm_strategy_value)}
  100. className='w-1/4'
  101. mode='multiple'
  102. options={strategyValueOptions}
  103. onChange={value => {
  104. if (!value || value?.length === 0) {
  105. updateAgent(null, 'llm_strategy_value');
  106. return null;
  107. }
  108. const curValue = value.reduce((pre: string, cur: string, index: number) => {
  109. if (index === 0) {
  110. return cur;
  111. } else {
  112. return `${pre},${cur}`;
  113. }
  114. }, '');
  115. updateAgent(curValue, 'llm_strategy_value');
  116. }}
  117. />
  118. </>
  119. )}
  120. </div>
  121. <div className='mb-3 text-lg font-bold'>{t('available_resources')}</div>
  122. {resources.map((resource: any, index: number) => {
  123. return (
  124. <ResourceCard
  125. resource={resource}
  126. key={index}
  127. index={index}
  128. updateResourcesByIndex={updateResourcesByIndex}
  129. resourceTypeOptions={resourceTypeOptions}
  130. />
  131. );
  132. })}
  133. <Button type='primary' className='mt-2' size='middle' onClick={handelAdd}>
  134. {t('add_resource')}
  135. </Button>
  136. </div>
  137. );
  138. }