list.tsx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useEffect, useState } from 'react'
  4. // import type { Log } from '@/models/log'
  5. import useSWR from 'swr'
  6. import {
  7. HandThumbDownIcon,
  8. HandThumbUpIcon,
  9. InformationCircleIcon,
  10. XMarkIcon,
  11. } from '@heroicons/react/24/outline'
  12. import { SparklesIcon } from '@heroicons/react/24/solid'
  13. import { get } from 'lodash-es'
  14. import InfiniteScroll from 'react-infinite-scroll-component'
  15. import dayjs from 'dayjs'
  16. import { createContext, useContext } from 'use-context-selector'
  17. import classNames from 'classnames'
  18. import { useTranslation } from 'react-i18next'
  19. import s from './style.module.css'
  20. import { randomString } from '@/utils'
  21. import { EditIconSolid } from '@/app/components/app/chat/icon-component'
  22. import type { FeedbackFunc, Feedbacktype, IChatItem, SubmitAnnotationFunc } from '@/app/components/app/chat/type'
  23. import type { Annotation, ChatConversationFullDetailResponse, ChatConversationGeneralDetail, ChatConversationsResponse, ChatMessage, ChatMessagesRequest, CompletionConversationFullDetailResponse, CompletionConversationGeneralDetail, CompletionConversationsResponse } from '@/models/log'
  24. import type { App } from '@/types/app'
  25. import Loading from '@/app/components/base/loading'
  26. import Drawer from '@/app/components/base/drawer'
  27. import Popover from '@/app/components/base/popover'
  28. import Chat from '@/app/components/app/chat'
  29. import Tooltip from '@/app/components/base/tooltip'
  30. import { ToastContext } from '@/app/components/base/toast'
  31. import { fetchChatConversationDetail, fetchChatMessages, fetchCompletionConversationDetail, updateLogMessageAnnotations, updateLogMessageFeedbacks } from '@/service/log'
  32. import { TONE_LIST } from '@/config'
  33. import ModelIcon from '@/app/components/app/configuration/config-model/model-icon'
  34. import ModelName from '@/app/components/app/configuration/config-model/model-name'
  35. type IConversationList = {
  36. logs?: ChatConversationsResponse | CompletionConversationsResponse
  37. appDetail?: App
  38. onRefresh: () => void
  39. }
  40. const defaultValue = 'N/A'
  41. const emptyText = '[Empty]'
  42. type IDrawerContext = {
  43. onClose: () => void
  44. appDetail?: App
  45. }
  46. const DrawerContext = createContext<IDrawerContext>({} as IDrawerContext)
  47. /**
  48. * Icon component with numbers
  49. */
  50. const HandThumbIconWithCount: FC<{ count: number; iconType: 'up' | 'down' }> = ({ count, iconType }) => {
  51. const classname = iconType === 'up' ? 'text-primary-600 bg-primary-50' : 'text-red-600 bg-red-50'
  52. const Icon = iconType === 'up' ? HandThumbUpIcon : HandThumbDownIcon
  53. return <div className={`inline-flex items-center w-fit rounded-md p-1 text-xs ${classname} mr-1 last:mr-0`}>
  54. <Icon className={'h-3 w-3 mr-0.5 rounded-md'} />
  55. {count > 0 ? count : null}
  56. </div>
  57. }
  58. const PARAM_MAP = {
  59. temperature: 'Temperature',
  60. top_p: 'Top P',
  61. presence_penalty: 'Presence Penalty',
  62. max_tokens: 'Max Token',
  63. stop: 'Stop',
  64. frequency_penalty: 'Frequency Penalty',
  65. }
  66. // Format interface data for easy display
  67. const getFormattedChatList = (messages: ChatMessage[]) => {
  68. const newChatList: IChatItem[] = []
  69. messages.forEach((item: ChatMessage) => {
  70. newChatList.push({
  71. id: `question-${item.id}`,
  72. content: item.inputs.query || item.inputs.default_input || item.query, // text generation: item.inputs.query; chat: item.query
  73. isAnswer: false,
  74. })
  75. newChatList.push({
  76. id: item.id,
  77. content: item.answer,
  78. feedback: item.feedbacks.find(item => item.from_source === 'user'), // user feedback
  79. adminFeedback: item.feedbacks.find(item => item.from_source === 'admin'), // admin feedback
  80. feedbackDisabled: false,
  81. isAnswer: true,
  82. more: {
  83. time: dayjs.unix(item.created_at).format('hh:mm A'),
  84. tokens: item.answer_tokens + item.message_tokens,
  85. latency: item.provider_response_latency.toFixed(2),
  86. },
  87. annotation: item.annotation,
  88. })
  89. })
  90. return newChatList
  91. }
  92. // const displayedParams = CompletionParams.slice(0, -2)
  93. const validatedParams = ['temperature', 'top_p', 'presence_penalty', 'frequency_penalty']
  94. type IDetailPanel<T> = {
  95. detail: T
  96. onFeedback: FeedbackFunc
  97. onSubmitAnnotation: SubmitAnnotationFunc
  98. }
  99. function DetailPanel<T extends ChatConversationFullDetailResponse | CompletionConversationFullDetailResponse>({ detail, onFeedback, onSubmitAnnotation }: IDetailPanel<T>) {
  100. const { onClose, appDetail } = useContext(DrawerContext)
  101. const { t } = useTranslation()
  102. const [items, setItems] = React.useState<IChatItem[]>([])
  103. const [hasMore, setHasMore] = useState(true)
  104. const fetchData = async () => {
  105. try {
  106. if (!hasMore)
  107. return
  108. const params: ChatMessagesRequest = {
  109. conversation_id: detail.id,
  110. limit: 4,
  111. }
  112. if (items?.[0]?.id)
  113. params.first_id = items?.[0]?.id.replace('question-', '')
  114. const messageRes = await fetchChatMessages({
  115. url: `/apps/${appDetail?.id}/chat-messages`,
  116. params,
  117. })
  118. const newItems = [...getFormattedChatList(messageRes.data), ...items]
  119. if (messageRes.has_more === false && detail?.model_config?.configs?.introduction) {
  120. newItems.unshift({
  121. id: 'introduction',
  122. isAnswer: true,
  123. isOpeningStatement: true,
  124. content: detail?.model_config?.configs?.introduction ?? 'hello',
  125. feedbackDisabled: true,
  126. })
  127. }
  128. setItems(newItems)
  129. setHasMore(messageRes.has_more)
  130. }
  131. catch (err) {
  132. console.error(err)
  133. }
  134. }
  135. useEffect(() => {
  136. if (appDetail?.id && detail.id && appDetail?.mode === 'chat')
  137. fetchData()
  138. }, [appDetail?.id, detail.id])
  139. const isChatMode = appDetail?.mode === 'chat'
  140. const targetTone = TONE_LIST.find((item) => {
  141. let res = true
  142. validatedParams.forEach((param) => {
  143. res = item.config?.[param] === detail.model_config?.configs?.completion_params?.[param]
  144. })
  145. return res
  146. })?.name ?? 'custom'
  147. return (<div className='rounded-xl border-[0.5px] border-gray-200 h-full flex flex-col overflow-auto'>
  148. {/* Panel Header */}
  149. <div className='border-b border-gray-100 py-4 px-6 flex items-center justify-between'>
  150. <div className='flex-1'>
  151. <span className='text-gray-500 text-[10px]'>{isChatMode ? t('appLog.detail.conversationId') : t('appLog.detail.time')}</span>
  152. <div className='text-gray-800 text-sm'>{isChatMode ? detail.id : dayjs.unix(detail.created_at).format(t('appLog.dateTimeFormat') as string)}</div>
  153. </div>
  154. <div className='mr-2 bg-gray-50 py-1.5 px-2.5 rounded-lg flex items-center text-[13px]'>
  155. <ModelIcon
  156. className={classNames('mr-1.5', 'w-5 h-5')}
  157. modelId={detail.model_config.model.name}
  158. providerName={detail.model_config.model.provider}
  159. />
  160. <ModelName modelId={detail.model_config.model.name} modelDisplayName={detail.model_config.model.name} />
  161. </div>
  162. <Popover
  163. position='br'
  164. className='!w-[280px]'
  165. btnClassName='mr-4 !bg-gray-50 !py-1.5 !px-2.5 border-none font-normal'
  166. btnElement={<>
  167. <span className='text-[13px]'>{targetTone}</span>
  168. <InformationCircleIcon className='h-4 w-4 text-gray-800 ml-1.5' />
  169. </>}
  170. htmlContent={<div className='w-[280px]'>
  171. <div className='flex justify-between py-2 px-4 font-medium text-sm text-gray-700'>
  172. <span>Tone of responses</span>
  173. <div>{targetTone}</div>
  174. </div>
  175. {['temperature', 'top_p', 'presence_penalty', 'max_tokens'].map((param: string, index: number) => {
  176. return <div className='flex justify-between py-2 px-4 bg-gray-50' key={index}>
  177. <span className='text-xs text-gray-700'>{PARAM_MAP[param as keyof typeof PARAM_MAP]}</span>
  178. <span className='text-gray-800 font-medium text-xs'>{detail?.model_config.model?.completion_params?.[param] || '-'}</span>
  179. </div>
  180. })}
  181. </div>}
  182. />
  183. <div className='w-6 h-6 rounded-lg flex items-center justify-center hover:cursor-pointer hover:bg-gray-100'>
  184. <XMarkIcon className='w-4 h-4 text-gray-500' onClick={onClose} />
  185. </div>
  186. </div>
  187. {/* Panel Body */}
  188. <div className='bg-gray-50 border border-gray-100 px-4 py-3 mx-6 my-4 rounded-lg'>
  189. <div className='text-gray-500 text-xs flex items-center'>
  190. <SparklesIcon className='h-3 w-3 mr-1' />{isChatMode ? t('appLog.detail.promptTemplateBeforeChat') : t('appLog.detail.promptTemplate')}
  191. </div>
  192. <div className='text-gray-700 font-medium text-sm mt-2'>{detail.model_config?.pre_prompt || emptyText}</div>
  193. </div>
  194. {!isChatMode
  195. ? <div className="px-2.5 py-4">
  196. <Chat
  197. chatList={getFormattedChatList([detail.message])}
  198. isHideSendInput={true}
  199. onFeedback={onFeedback}
  200. onSubmitAnnotation={onSubmitAnnotation}
  201. displayScene='console'
  202. />
  203. </div>
  204. : items.length < 8
  205. ? <div className="px-2.5 pt-4 mb-4">
  206. <Chat
  207. chatList={items}
  208. isHideSendInput={true}
  209. onFeedback={onFeedback}
  210. onSubmitAnnotation={onSubmitAnnotation}
  211. displayScene='console'
  212. />
  213. </div>
  214. : <div
  215. className="px-2.5 py-4"
  216. id="scrollableDiv"
  217. style={{
  218. height: 1000, // Specify a value
  219. overflow: 'auto',
  220. display: 'flex',
  221. flexDirection: 'column-reverse',
  222. }}>
  223. {/* Put the scroll bar always on the bottom */}
  224. <InfiniteScroll
  225. scrollableTarget="scrollableDiv"
  226. dataLength={items.length}
  227. next={fetchData}
  228. hasMore={hasMore}
  229. loader={<div className='text-center text-gray-400 text-xs'>{t('appLog.detail.loading')}...</div>}
  230. // endMessage={<div className='text-center'>Nothing more to show</div>}
  231. // below props only if you need pull down functionality
  232. refreshFunction={fetchData}
  233. pullDownToRefresh
  234. pullDownToRefreshThreshold={50}
  235. // pullDownToRefreshContent={
  236. // <div className='text-center'>Pull down to refresh</div>
  237. // }
  238. // releaseToRefreshContent={
  239. // <div className='text-center'>Release to refresh</div>
  240. // }
  241. // To put endMessage and loader to the top.
  242. style={{ display: 'flex', flexDirection: 'column-reverse' }}
  243. inverse={true}
  244. >
  245. <Chat
  246. chatList={items}
  247. isHideSendInput={true}
  248. onFeedback={onFeedback}
  249. onSubmitAnnotation={onSubmitAnnotation}
  250. displayScene='console'
  251. />
  252. </InfiniteScroll>
  253. </div>
  254. }
  255. </div>)
  256. }
  257. /**
  258. * Text App Conversation Detail Component
  259. */
  260. const CompletionConversationDetailComp: FC<{ appId?: string; conversationId?: string }> = ({ appId, conversationId }) => {
  261. // Text Generator App Session Details Including Message List
  262. const detailParams = ({ url: `/apps/${appId}/completion-conversations/${conversationId}` })
  263. const { data: conversationDetail, mutate: conversationDetailMutate } = useSWR(() => (appId && conversationId) ? detailParams : null, fetchCompletionConversationDetail)
  264. const { notify } = useContext(ToastContext)
  265. const { t } = useTranslation()
  266. const handleFeedback = async (mid: string, { rating }: Feedbacktype): Promise<boolean> => {
  267. try {
  268. await updateLogMessageFeedbacks({ url: `/apps/${appId}/feedbacks`, body: { message_id: mid, rating } })
  269. conversationDetailMutate()
  270. notify({ type: 'success', message: t('common.actionMsg.modifiedSuccessfully') })
  271. return true
  272. }
  273. catch (err) {
  274. notify({ type: 'error', message: t('common.actionMsg.modifiedUnsuccessfully') })
  275. return false
  276. }
  277. }
  278. const handleAnnotation = async (mid: string, value: string): Promise<boolean> => {
  279. try {
  280. await updateLogMessageAnnotations({ url: `/apps/${appId}/annotations`, body: { message_id: mid, content: value } })
  281. conversationDetailMutate()
  282. notify({ type: 'success', message: t('common.actionMsg.modifiedSuccessfully') })
  283. return true
  284. }
  285. catch (err) {
  286. notify({ type: 'error', message: t('common.actionMsg.modifiedUnsuccessfully') })
  287. return false
  288. }
  289. }
  290. if (!conversationDetail)
  291. return null
  292. return <DetailPanel<CompletionConversationFullDetailResponse>
  293. detail={conversationDetail}
  294. onFeedback={handleFeedback}
  295. onSubmitAnnotation={handleAnnotation}
  296. />
  297. }
  298. /**
  299. * Chat App Conversation Detail Component
  300. */
  301. const ChatConversationDetailComp: FC<{ appId?: string; conversationId?: string }> = ({ appId, conversationId }) => {
  302. const detailParams = { url: `/apps/${appId}/chat-conversations/${conversationId}` }
  303. const { data: conversationDetail } = useSWR(() => (appId && conversationId) ? detailParams : null, fetchChatConversationDetail)
  304. const { notify } = useContext(ToastContext)
  305. const { t } = useTranslation()
  306. const handleFeedback = async (mid: string, { rating }: Feedbacktype): Promise<boolean> => {
  307. try {
  308. await updateLogMessageFeedbacks({ url: `/apps/${appId}/feedbacks`, body: { message_id: mid, rating } })
  309. notify({ type: 'success', message: t('common.actionMsg.modifiedSuccessfully') })
  310. return true
  311. }
  312. catch (err) {
  313. notify({ type: 'error', message: t('common.actionMsg.modifiedUnsuccessfully') })
  314. return false
  315. }
  316. }
  317. const handleAnnotation = async (mid: string, value: string): Promise<boolean> => {
  318. try {
  319. await updateLogMessageAnnotations({ url: `/apps/${appId}/annotations`, body: { message_id: mid, content: value } })
  320. notify({ type: 'success', message: t('common.actionMsg.modifiedSuccessfully') })
  321. return true
  322. }
  323. catch (err) {
  324. notify({ type: 'error', message: t('common.actionMsg.modifiedUnsuccessfully') })
  325. return false
  326. }
  327. }
  328. if (!conversationDetail)
  329. return null
  330. return <DetailPanel<ChatConversationFullDetailResponse>
  331. detail={conversationDetail}
  332. onFeedback={handleFeedback}
  333. onSubmitAnnotation={handleAnnotation}
  334. />
  335. }
  336. /**
  337. * Conversation list component including basic information
  338. */
  339. const ConversationList: FC<IConversationList> = ({ logs, appDetail, onRefresh }) => {
  340. const { t } = useTranslation()
  341. const [showDrawer, setShowDrawer] = useState<boolean>(false) // Whether to display the chat details drawer
  342. const [currentConversation, setCurrentConversation] = useState<ChatConversationGeneralDetail | CompletionConversationGeneralDetail | undefined>() // Currently selected conversation
  343. const isChatMode = appDetail?.mode === 'chat' // Whether the app is a chat app
  344. // Annotated data needs to be highlighted
  345. const renderTdValue = (value: string | number | null, isEmptyStyle: boolean, isHighlight = false, annotation?: Annotation) => {
  346. return (
  347. <Tooltip
  348. htmlContent={
  349. <span className='text-xs text-gray-500 inline-flex items-center'>
  350. <EditIconSolid className='mr-1' />{`${t('appLog.detail.annotationTip', { user: annotation?.account?.name })} ${dayjs.unix(annotation?.created_at || dayjs().unix()).format('MM-DD hh:mm A')}`}
  351. </span>
  352. }
  353. className={(isHighlight && !isChatMode) ? '' : '!hidden'}
  354. selector={`highlight-${randomString(16)}`}
  355. >
  356. <div className={classNames(isEmptyStyle ? 'text-gray-400' : 'text-gray-700', !isHighlight ? '' : 'bg-orange-100', 'text-sm overflow-hidden text-ellipsis whitespace-nowrap')}>
  357. {value || '-'}
  358. </div>
  359. </Tooltip>
  360. )
  361. }
  362. const onCloseDrawer = () => {
  363. onRefresh()
  364. setShowDrawer(false)
  365. setCurrentConversation(undefined)
  366. }
  367. if (!logs)
  368. return <Loading />
  369. return (
  370. <>
  371. <table className={`w-full border-collapse border-0 text-sm mt-3 ${s.logTable}`}>
  372. <thead className="h-8 leading-8 border-b border-gray-200 text-gray-500 font-bold">
  373. <tr>
  374. <td className='w-[1.375rem]'></td>
  375. <td>{t('appLog.table.header.time')}</td>
  376. <td>{t('appLog.table.header.endUser')}</td>
  377. <td>{isChatMode ? t('appLog.table.header.summary') : t('appLog.table.header.input')}</td>
  378. <td>{isChatMode ? t('appLog.table.header.messageCount') : t('appLog.table.header.output')}</td>
  379. <td>{t('appLog.table.header.userRate')}</td>
  380. <td>{t('appLog.table.header.adminRate')}</td>
  381. </tr>
  382. </thead>
  383. <tbody className="text-gray-500">
  384. {logs.data.map((log) => {
  385. const endUser = log.from_end_user_session_id
  386. const leftValue = get(log, isChatMode ? 'summary' : 'message.inputs.query') || (!isChatMode ? (get(log, 'message.query') || get(log, 'message.inputs.default_input')) : '') || ''
  387. const rightValue = get(log, isChatMode ? 'message_count' : 'message.answer')
  388. return <tr
  389. key={log.id}
  390. className={`border-b border-gray-200 h-8 hover:bg-gray-50 cursor-pointer ${currentConversation?.id !== log.id ? '' : 'bg-gray-50'}`}
  391. onClick={() => {
  392. setShowDrawer(true)
  393. setCurrentConversation(log)
  394. }}>
  395. <td className='text-center align-middle'>{!log.read_at && <span className='inline-block bg-[#3F83F8] h-1.5 w-1.5 rounded'></span>}</td>
  396. <td className='w-[160px]'>{dayjs.unix(log.created_at).format(t('appLog.dateTimeFormat') as string)}</td>
  397. <td>{renderTdValue(endUser || defaultValue, !endUser)}</td>
  398. <td style={{ maxWidth: isChatMode ? 300 : 200 }}>
  399. {renderTdValue(leftValue || t('appLog.table.empty.noChat'), !leftValue, isChatMode && log.annotated)}
  400. </td>
  401. <td style={{ maxWidth: isChatMode ? 100 : 200 }}>
  402. {renderTdValue(rightValue === 0 ? 0 : (rightValue || t('appLog.table.empty.noOutput')), !rightValue, !isChatMode && !!log.annotation?.content, log.annotation)}
  403. </td>
  404. <td>
  405. {(!log.user_feedback_stats.like && !log.user_feedback_stats.dislike)
  406. ? renderTdValue(defaultValue, true)
  407. : <>
  408. {!!log.user_feedback_stats.like && <HandThumbIconWithCount iconType='up' count={log.user_feedback_stats.like} />}
  409. {!!log.user_feedback_stats.dislike && <HandThumbIconWithCount iconType='down' count={log.user_feedback_stats.dislike} />}
  410. </>
  411. }
  412. </td>
  413. <td>
  414. {(!log.admin_feedback_stats.like && !log.admin_feedback_stats.dislike)
  415. ? renderTdValue(defaultValue, true)
  416. : <>
  417. {!!log.admin_feedback_stats.like && <HandThumbIconWithCount iconType='up' count={log.admin_feedback_stats.like} />}
  418. {!!log.admin_feedback_stats.dislike && <HandThumbIconWithCount iconType='down' count={log.admin_feedback_stats.dislike} />}
  419. </>
  420. }
  421. </td>
  422. </tr>
  423. })}
  424. </tbody>
  425. </table>
  426. <Drawer
  427. isOpen={showDrawer}
  428. onClose={onCloseDrawer}
  429. mask={false}
  430. footer={null}
  431. panelClassname='mt-16 mr-2 mb-3 !p-0 !max-w-[640px] rounded-b-xl'
  432. >
  433. <DrawerContext.Provider value={{
  434. onClose: onCloseDrawer,
  435. appDetail,
  436. }}>
  437. {isChatMode
  438. ? <ChatConversationDetailComp appId={appDetail?.id} conversationId={currentConversation?.id} />
  439. : <CompletionConversationDetailComp appId={appDetail?.id} conversationId={currentConversation?.id} />
  440. }
  441. </DrawerContext.Provider>
  442. </Drawer>
  443. </>
  444. )
  445. }
  446. export default ConversationList