tool-call.tsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. 'use client'
  2. import type { FC } from 'react'
  3. import { useState } from 'react'
  4. import cn from 'classnames'
  5. import { useContext } from 'use-context-selector'
  6. import BlockIcon from '@/app/components/workflow/block-icon'
  7. import CodeEditor from '@/app/components/workflow/nodes/_base/components/editor/code-editor'
  8. import { CodeLanguage } from '@/app/components/workflow/nodes/code/types'
  9. import { AlertCircle } from '@/app/components/base/icons/src/vender/line/alertsAndFeedback'
  10. import { CheckCircle } from '@/app/components/base/icons/src/vender/line/general'
  11. import { ChevronRight } from '@/app/components/base/icons/src/vender/line/arrows'
  12. import type { ToolCall } from '@/models/log'
  13. import { BlockEnum } from '@/app/components/workflow/types'
  14. import I18n from '@/context/i18n'
  15. type Props = {
  16. toolCall: ToolCall
  17. isLLM: boolean
  18. isFinal?: boolean
  19. tokens?: number
  20. observation?: any
  21. finalAnswer?: any
  22. }
  23. const ToolCallItem: FC<Props> = ({ toolCall, isLLM = false, isFinal, tokens, observation, finalAnswer }) => {
  24. const [collapseState, setCollapseState] = useState<boolean>(true)
  25. const { locale } = useContext(I18n)
  26. const toolName = isLLM ? 'LLM' : (toolCall.tool_label[locale] || toolCall.tool_label[locale.replaceAll('-', '_')])
  27. const getTime = (time: number) => {
  28. if (time < 1)
  29. return `${(time * 1000).toFixed(3)} ms`
  30. if (time > 60)
  31. return `${parseInt(Math.round(time / 60).toString())} m ${(time % 60).toFixed(3)} s`
  32. return `${time.toFixed(3)} s`
  33. }
  34. const getTokenCount = (tokens: number) => {
  35. if (tokens < 1000)
  36. return tokens
  37. if (tokens >= 1000 && tokens < 1000000)
  38. return `${parseFloat((tokens / 1000).toFixed(3))}K`
  39. if (tokens >= 1000000)
  40. return `${parseFloat((tokens / 1000000).toFixed(3))}M`
  41. }
  42. return (
  43. <div className={cn('py-1')}>
  44. <div className={cn('group transition-all bg-white border border-gray-100 rounded-2xl shadow-xs hover:shadow-md')}>
  45. <div
  46. className={cn(
  47. 'flex items-center py-3 pl-[6px] pr-3 cursor-pointer',
  48. !collapseState && '!pb-2',
  49. )}
  50. onClick={() => setCollapseState(!collapseState)}
  51. >
  52. <ChevronRight
  53. className={cn(
  54. 'shrink-0 w-3 h-3 mr-1 text-gray-400 transition-all group-hover:text-gray-500',
  55. !collapseState && 'rotate-90',
  56. )}
  57. />
  58. <BlockIcon className={cn('shrink-0 mr-2')} type={isLLM ? BlockEnum.LLM : BlockEnum.Tool} toolIcon={toolCall.tool_icon} />
  59. <div className={cn(
  60. 'grow text-gray-700 text-[13px] leading-[16px] font-semibold truncate',
  61. )} title={toolName}>{toolName}</div>
  62. <div className='shrink-0 text-gray-500 text-xs leading-[18px]'>
  63. {toolCall.time_cost && (
  64. <span>{getTime(toolCall.time_cost || 0)}</span>
  65. )}
  66. {isLLM && (
  67. <span>{`${getTokenCount(tokens || 0)} tokens`}</span>
  68. )}
  69. </div>
  70. {toolCall.status === 'success' && (
  71. <CheckCircle className='shrink-0 ml-2 w-3.5 h-3.5 text-[#12B76A]' />
  72. )}
  73. {toolCall.status === 'error' && (
  74. <AlertCircle className='shrink-0 ml-2 w-3.5 h-3.5 text-[#F04438]' />
  75. )}
  76. </div>
  77. {!collapseState && (
  78. <div className='pb-2'>
  79. <div className={cn('px-[10px] py-1')}>
  80. {toolCall.status === 'error' && (
  81. <div className='px-3 py-[10px] bg-[#fef3f2] rounded-lg border-[0.5px] border-[rbga(0,0,0,0.05)] text-xs leading-[18px] text-[#d92d20] shadow-xs'>{toolCall.error}</div>
  82. )}
  83. </div>
  84. {toolCall.tool_input && (
  85. <div className={cn('px-[10px] py-1')}>
  86. <CodeEditor
  87. readOnly
  88. title={<div>INPUT</div>}
  89. language={CodeLanguage.json}
  90. value={toolCall.tool_input}
  91. isJSONStringifyBeauty
  92. />
  93. </div>
  94. )}
  95. {toolCall.tool_output && (
  96. <div className={cn('px-[10px] py-1')}>
  97. <CodeEditor
  98. readOnly
  99. title={<div>OUTPUT</div>}
  100. language={CodeLanguage.json}
  101. value={toolCall.tool_output}
  102. isJSONStringifyBeauty
  103. />
  104. </div>
  105. )}
  106. {isLLM && (
  107. <div className={cn('px-[10px] py-1')}>
  108. <CodeEditor
  109. readOnly
  110. title={<div>OBSERVATION</div>}
  111. language={CodeLanguage.json}
  112. value={observation}
  113. isJSONStringifyBeauty
  114. />
  115. </div>
  116. )}
  117. {isLLM && (
  118. <div className={cn('px-[10px] py-1')}>
  119. <CodeEditor
  120. readOnly
  121. title={<div>{isFinal ? 'FINAL ANSWER' : 'THOUGHT'}</div>}
  122. language={CodeLanguage.json}
  123. value={finalAnswer}
  124. isJSONStringifyBeauty
  125. />
  126. </div>
  127. )}
  128. </div>
  129. )}
  130. </div>
  131. </div>
  132. )
  133. }
  134. export default ToolCallItem