index.tsx 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. import {
  2. apiInterceptors,
  3. postDbgptsHubUpdate,
  4. postDbgptsInstall,
  5. postDbgptsMy,
  6. postDbgptsQuery,
  7. postDbgptsUninstall,
  8. } from '@/client/api';
  9. import BlurredCard, { ChatButton } from '@/new-components/common/blurredCard';
  10. import ConstructLayout from '@/new-components/layout/Construct';
  11. import { IAgentPlugin, PostAgentQueryParams } from '@/types/agent';
  12. import { ClearOutlined, DownloadOutlined, SearchOutlined, SyncOutlined } from '@ant-design/icons';
  13. import { useRequest } from 'ahooks';
  14. import { Button, Input, Segmented, SegmentedProps, Spin, Tag, message } from 'antd';
  15. import cls from 'classnames';
  16. import moment from 'moment';
  17. import { useCallback, useEffect, useMemo, useState } from 'react';
  18. import { useTranslation } from 'react-i18next';
  19. function Agent() {
  20. const { t } = useTranslation();
  21. const [searchValue, setSearchValue] = useState('');
  22. const [activeKey, setActiveKey] = useState<string>('market');
  23. const [uploading, setUploading] = useState(false);
  24. const [loading, setLoading] = useState(false);
  25. const [_error, setIsError] = useState(false); // _error is not used
  26. const [actionIndex, setActionIndex] = useState<number | undefined>();
  27. const [typeStr, setTypeStr] = useState('all');
  28. const pagination = useMemo<{ pageNo: number; pageSize: number }>(
  29. () => ({
  30. pageNo: 1,
  31. pageSize: 20,
  32. }),
  33. [],
  34. );
  35. const { data: agents = [], refresh } = useRequest<IAgentPlugin[], []>(
  36. async () => {
  37. setLoading(true);
  38. if (activeKey === 'my') {
  39. const [err, res] = await apiInterceptors(
  40. postDbgptsMy({
  41. name: searchValue || undefined,
  42. type: typeStr === 'all' ? undefined : typeStr,
  43. page_index: pagination.pageNo,
  44. page_size: pagination.pageSize,
  45. }),
  46. );
  47. setLoading(false);
  48. setIsError(!!err);
  49. return res?.items ?? [];
  50. }
  51. const queryParams: PostAgentQueryParams = {
  52. page_index: pagination.pageNo,
  53. page_size: pagination.pageSize,
  54. name: searchValue || undefined,
  55. type: typeStr === 'all' ? undefined : typeStr,
  56. };
  57. const [err, res] = await apiInterceptors(postDbgptsQuery(queryParams));
  58. setLoading(false);
  59. setIsError(!!err);
  60. return res?.items ?? [];
  61. },
  62. {
  63. manual: true,
  64. },
  65. );
  66. const updateFromGithub = async () => {
  67. try {
  68. setUploading(true);
  69. const [err] = await apiInterceptors(postDbgptsHubUpdate());
  70. if (err) return;
  71. message.success('success');
  72. refresh();
  73. } finally {
  74. setUploading(false);
  75. }
  76. };
  77. useEffect(() => {
  78. refresh();
  79. }, [activeKey, typeStr]);
  80. const pluginAction = useCallback(
  81. async (agent: { name: string; type: string }, index: number, isInstall: boolean) => {
  82. if (actionIndex) return;
  83. setActionIndex(index);
  84. setLoading(true);
  85. let errs = null;
  86. if (isInstall) {
  87. const [err] = await apiInterceptors(postDbgptsInstall(agent));
  88. errs = err;
  89. } else {
  90. const [err] = await apiInterceptors(
  91. postDbgptsUninstall({
  92. name: agent.name,
  93. type: agent.type,
  94. }),
  95. );
  96. errs = err;
  97. }
  98. setLoading(false);
  99. if (!errs) {
  100. message.success('success');
  101. refresh();
  102. }
  103. setActionIndex(undefined);
  104. },
  105. [actionIndex, refresh],
  106. );
  107. const items: SegmentedProps['options'] = [
  108. {
  109. value: 'market',
  110. label: t('community_dbgpts'),
  111. },
  112. {
  113. value: 'my',
  114. label: t('my_dbgpts'),
  115. },
  116. ];
  117. const typeItems: SegmentedProps['options'] = [
  118. {
  119. value: 'all',
  120. label: t('All'),
  121. },
  122. {
  123. value: 'workflow',
  124. label: t('workflow'),
  125. },
  126. {
  127. value: 'agents',
  128. label: 'Agent',
  129. },
  130. {
  131. value: 'resources',
  132. label: t('resources'),
  133. },
  134. {
  135. value: 'apps',
  136. label: t('app'),
  137. },
  138. {
  139. value: 'operators',
  140. label: t('operators'),
  141. },
  142. ];
  143. const logoFn = (type: string) => {
  144. switch (type) {
  145. case 'workflow':
  146. return '/pictures/flow.png';
  147. case 'resources':
  148. return '/pictures/database.png';
  149. case 'apps':
  150. return '/pictures/app.png';
  151. case 'operators':
  152. return '/pictures/knowledge.png';
  153. case 'agents':
  154. default:
  155. return '/pictures/agent.png';
  156. }
  157. };
  158. return (
  159. <ConstructLayout>
  160. <Spin spinning={loading}>
  161. <div className='h-screen w-full p-4 md:p-6 overflow-y-auto'>
  162. <div className='flex justify-between items-center mb-6'>
  163. <div className='flex items-center gap-4'>
  164. <Segmented
  165. className='backdrop-filter backdrop-blur-lg bg-white bg-opacity-30 border-2 border-white rounded-lg shadow p-1 dark:border-[#6f7f95] dark:bg-[#6f7f95] dark:bg-opacity-60'
  166. options={items}
  167. onChange={key => {
  168. setActiveKey(key as string);
  169. }}
  170. value={activeKey}
  171. />
  172. {/* <Input
  173. variant="filled"
  174. prefix={<SearchOutlined />}
  175. placeholder={t('please_enter_the_keywords')}
  176. value={searchValue}
  177. onChange={(e) => setSearchValue(e.target.value)}
  178. onPressEnter={refresh}
  179. allowClear
  180. className="w-[230px] h-[40px] border-1 border-white backdrop-filter backdrop-blur-lg bg-white bg-opacity-30 dark:border-[#6f7f95] dark:bg-[#6f7f95] dark:bg-opacity-60"
  181. /> */}
  182. </div>
  183. <div className='flex items-center gap-4'>
  184. <Button
  185. className={cls('border-none text-white bg-button-gradient h-full', {
  186. 'opacity-40': false,
  187. })}
  188. loading={uploading}
  189. icon={<SyncOutlined />}
  190. onClick={updateFromGithub}
  191. >
  192. {t('Refresh_dbgpts')}
  193. </Button>
  194. </div>
  195. </div>
  196. <div className='w-full flex flex-wrap pb-12 mx-[-8px]'>
  197. <Segmented
  198. className='backdrop-filter backdrop-blur-lg bg-white bg-opacity-30 border-2 border-white rounded-lg shadow p-1 dark:border-[#6f7f95] dark:bg-[#6f7f95] dark:bg-opacity-60'
  199. options={typeItems}
  200. onChange={key => {
  201. setTypeStr(key as string);
  202. }}
  203. value={typeStr}
  204. />
  205. <Input
  206. variant='filled'
  207. prefix={<SearchOutlined />}
  208. placeholder={t('please_enter_the_keywords')}
  209. value={searchValue}
  210. onChange={e => setSearchValue(e.target.value)}
  211. onPressEnter={refresh}
  212. allowClear
  213. className='w-[230px] h-[40px] border-1 border-white ml-4 backdrop-filter backdrop-blur-lg bg-white bg-opacity-30 dark:border-[#6f7f95] dark:bg-[#6f7f95] dark:bg-opacity-60'
  214. />
  215. </div>
  216. <div className='flex flex-wrap pb-12'>
  217. {agents.map((agent, index) => (
  218. <BlurredCard
  219. logo={logoFn(agent.type)}
  220. onClick={() => {
  221. window.open(`https://github.com/eosphoros-ai/dbgpts/tree/main/${agent.type}/${agent.name}`, '_blank');
  222. }}
  223. description={agent.description}
  224. name={agent.name}
  225. key={agent.id}
  226. Tags={
  227. <div>
  228. {agent.author && <Tag>{agent.author}</Tag>}
  229. {agent.version && <Tag>v{agent.version}</Tag>}
  230. {/* {agent.type && <Tag>Type {agent.type}</Tag>} */}
  231. {agent.storage_channel && <Tag>{agent.storage_channel}</Tag>}
  232. </div>
  233. }
  234. LeftBottom={
  235. <div className='flex gap-2'>
  236. {agent.author && <span>{agent.author}</span>}
  237. {agent.author && <span>•</span>}
  238. {agent?.gmt_created && <span>{moment(agent?.gmt_created).fromNow() + ' ' + t('update')}</span>}
  239. </div>
  240. }
  241. RightTop={agent.type && <Tag>{agent.type}</Tag>}
  242. rightTopHover={false}
  243. RightBottom={
  244. agent.installed || activeKey == 'my' ? (
  245. <ChatButton
  246. Icon={<ClearOutlined />}
  247. text='Uninstall'
  248. onClick={() => {
  249. pluginAction(agent, index, false);
  250. }}
  251. />
  252. ) : (
  253. <ChatButton
  254. Icon={<DownloadOutlined />}
  255. text='Install'
  256. onClick={() => {
  257. pluginAction(agent, index, true);
  258. }}
  259. />
  260. )
  261. }
  262. />
  263. ))}
  264. </div>
  265. {/* {activeKey !== 'market' ? <MyPlugins /> : <MarketPlugins />} */}
  266. </div>
  267. </Spin>
  268. </ConstructLayout>
  269. );
  270. }
  271. export default Agent;