detail.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useCallback, useEffect, useMemo, useState } from 'react'
  4. import { useContext } from 'use-context-selector'
  5. import { useTranslation } from 'react-i18next'
  6. import { flatten, uniq } from 'lodash-es'
  7. import ResultPanel from './result'
  8. import TracingPanel from './tracing'
  9. import cn from '@/utils/classnames'
  10. import { ToastContext } from '@/app/components/base/toast'
  11. import Loading from '@/app/components/base/loading'
  12. import { fetchAgentLogDetail } from '@/service/log'
  13. import type { AgentIteration, AgentLogDetailResponse } from '@/models/log'
  14. import { useStore as useAppStore } from '@/app/components/app/store'
  15. import type { IChatItem } from '@/app/components/base/chat/chat/type'
  16. export type AgentLogDetailProps = {
  17. activeTab?: 'DETAIL' | 'TRACING'
  18. conversationID: string
  19. log: IChatItem
  20. messageID: string
  21. }
  22. const AgentLogDetail: FC<AgentLogDetailProps> = ({
  23. activeTab = 'DETAIL',
  24. conversationID,
  25. messageID,
  26. log,
  27. }) => {
  28. const { t } = useTranslation()
  29. const { notify } = useContext(ToastContext)
  30. const [currentTab, setCurrentTab] = useState<string>(activeTab)
  31. const appDetail = useAppStore(s => s.appDetail)
  32. const [loading, setLoading] = useState<boolean>(true)
  33. const [runDetail, setRunDetail] = useState<AgentLogDetailResponse>()
  34. const [list, setList] = useState<AgentIteration[]>([])
  35. const tools = useMemo(() => {
  36. const res = uniq(flatten(runDetail?.iterations.map((iteration: any) => {
  37. return iteration.tool_calls.map((tool: any) => tool.tool_name).filter(Boolean)
  38. })).filter(Boolean))
  39. return res
  40. }, [runDetail])
  41. const getLogDetail = useCallback(async (appID: string, conversationID: string, messageID: string) => {
  42. try {
  43. const res = await fetchAgentLogDetail({
  44. appID,
  45. params: {
  46. conversation_id: conversationID,
  47. message_id: messageID,
  48. },
  49. })
  50. setRunDetail(res)
  51. setList(res.iterations)
  52. }
  53. catch (err) {
  54. notify({
  55. type: 'error',
  56. message: `${err}`,
  57. })
  58. }
  59. }, [notify])
  60. const getData = async (appID: string, conversationID: string, messageID: string) => {
  61. setLoading(true)
  62. await getLogDetail(appID, conversationID, messageID)
  63. setLoading(false)
  64. }
  65. const switchTab = async (tab: string) => {
  66. setCurrentTab(tab)
  67. }
  68. useEffect(() => {
  69. // fetch data
  70. if (appDetail)
  71. getData(appDetail.id, conversationID, messageID)
  72. }, [appDetail, conversationID, messageID])
  73. return (
  74. <div className='grow relative flex flex-col'>
  75. {/* tab */}
  76. <div className='shrink-0 flex items-center px-4 border-b-[0.5px] border-[rgba(0,0,0,0.05)]'>
  77. <div
  78. className={cn(
  79. 'mr-6 py-3 border-b-2 border-transparent text-[13px] font-semibold leading-[18px] text-gray-400 cursor-pointer',
  80. currentTab === 'DETAIL' && '!border-[rgb(21,94,239)] text-gray-700',
  81. )}
  82. onClick={() => switchTab('DETAIL')}
  83. >{t('runLog.detail')}</div>
  84. <div
  85. className={cn(
  86. 'mr-6 py-3 border-b-2 border-transparent text-[13px] font-semibold leading-[18px] text-gray-400 cursor-pointer',
  87. currentTab === 'TRACING' && '!border-[rgb(21,94,239)] text-gray-700',
  88. )}
  89. onClick={() => switchTab('TRACING')}
  90. >{t('runLog.tracing')}</div>
  91. </div>
  92. {/* panel detail */}
  93. <div className={cn('grow bg-white h-0 overflow-y-auto rounded-b-2xl', currentTab !== 'DETAIL' && '!bg-gray-50')}>
  94. {loading && (
  95. <div className='flex h-full items-center justify-center bg-white'>
  96. <Loading />
  97. </div>
  98. )}
  99. {!loading && currentTab === 'DETAIL' && runDetail && (
  100. <ResultPanel
  101. inputs={log.input}
  102. outputs={log.content}
  103. status={runDetail.meta.status}
  104. error={runDetail.meta.error}
  105. elapsed_time={runDetail.meta.elapsed_time}
  106. total_tokens={runDetail.meta.total_tokens}
  107. created_at={runDetail.meta.start_time}
  108. created_by={runDetail.meta.executor}
  109. agentMode={runDetail.meta.agent_mode}
  110. tools={tools}
  111. iterations={runDetail.iterations.length}
  112. />
  113. )}
  114. {!loading && currentTab === 'TRACING' && (
  115. <TracingPanel
  116. list={list}
  117. />
  118. )}
  119. </div>
  120. </div>
  121. )
  122. }
  123. export default AgentLogDetail