prompt log

This commit is contained in:
JzoNg 2024-03-15 19:54:50 +08:00
parent e33260d2e2
commit 9908a8bf1f
5 changed files with 59 additions and 40 deletions

View File

@ -1,6 +1,6 @@
'use client' 'use client'
import type { FC, ReactNode } from 'react' import type { FC, ReactNode } from 'react'
import React, { useRef, useState } from 'react' import React, { useEffect, useRef, useState } from 'react'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import { UserCircleIcon } from '@heroicons/react/24/solid' import { UserCircleIcon } from '@heroicons/react/24/solid'
import cn from 'classnames' import cn from 'classnames'
@ -27,6 +27,7 @@ import type { Emoji } from '@/app/components/tools/types'
import type { VisionFile } from '@/types/app' import type { VisionFile } from '@/types/app'
import ImageGallery from '@/app/components/base/image-gallery' import ImageGallery from '@/app/components/base/image-gallery'
import Log from '@/app/components/app/chat/log' import Log from '@/app/components/app/chat/log'
import PromptLogModal from '@/app/components/base/prompt-log-modal'
const IconWrapper: FC<{ children: React.ReactNode | string }> = ({ children }) => { const IconWrapper: FC<{ children: React.ReactNode | string }> = ({ children }) => {
return <div className={'rounded-lg h-6 w-6 flex items-center justify-center hover:bg-gray-100'}> return <div className={'rounded-lg h-6 w-6 flex items-center justify-center hover:bg-gray-100'}>
@ -233,7 +234,19 @@ const Answer: FC<IAnswerProps> = ({
</div> </div>
) )
const ref = useRef(null) const [showPromptLogModal, setShowPromptLogModal] = useState(false)
const [width, setWidth] = useState(0)
const ref = useRef<HTMLDivElement>(null)
const adjustModalWidth = () => {
if (ref.current)
setWidth(document.body.clientWidth - (ref.current?.clientWidth + 56 + 16))
}
useEffect(() => {
adjustModalWidth()
}, [])
return ( return (
// data-id for debug the item message is right // data-id for debug the item message is right
@ -323,7 +336,7 @@ const Answer: FC<IAnswerProps> = ({
{((isShowPromptLog && !isResponding) || (!item.isOpeningStatement && isShowTextToSpeech)) && ( {((isShowPromptLog && !isResponding) || (!item.isOpeningStatement && isShowTextToSpeech)) && (
<div className='hidden group-hover:flex items-center h-[28px] p-0.5 rounded-lg bg-white border-[0.5px] border-gray-100 shadow-md'> <div className='hidden group-hover:flex items-center h-[28px] p-0.5 rounded-lg bg-white border-[0.5px] border-gray-100 shadow-md'>
{isShowPromptLog && !isResponding && ( {isShowPromptLog && !isResponding && (
<Log runID={item.workflow_run_id} log={item.log!} containerRef={ref} /> <Log runID={item.workflow_run_id} setShowModal={setShowPromptLogModal} />
)} )}
{!item.isOpeningStatement && isShowTextToSpeech && ( {!item.isOpeningStatement && isShowTextToSpeech && (
<> <>
@ -373,7 +386,13 @@ const Answer: FC<IAnswerProps> = ({
{!feedbackDisabled && renderFeedbackRating(feedback?.rating, !isHideFeedbackEdit, displayScene !== 'console')} {!feedbackDisabled && renderFeedbackRating(feedback?.rating, !isHideFeedbackEdit, displayScene !== 'console')}
</div> </div>
</div> </div>
{showPromptLogModal && (
<PromptLogModal
width={width}
log={item.log || []}
onCancel={() => setShowPromptLogModal(false)}
/>
)}
{more && <MoreInfo className='invisible group-hover:visible' more={more} isQuestion={false} />} {more && <MoreInfo className='invisible group-hover:visible' more={more} isQuestion={false} />}
</div> </div>
</div> </div>

View File

@ -1,8 +1,6 @@
import type { Dispatch, FC, ReactNode, RefObject, SetStateAction } from 'react' import type { Dispatch, FC, ReactNode, SetStateAction } from 'react'
import { useEffect, useState } from 'react'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import { File02 } from '@/app/components/base/icons/src/vender/line/files' import { File02 } from '@/app/components/base/icons/src/vender/line/files'
import PromptLogModal from '@/app/components/base/prompt-log-modal'
export type LogData = { export type LogData = {
role: string role: string
@ -10,29 +8,16 @@ export type LogData = {
} }
type LogProps = { type LogProps = {
containerRef: RefObject<HTMLElement>
log: LogData[]
runID?: string runID?: string
setShowModal: Dispatch<SetStateAction<boolean>>
children?: (v: Dispatch<SetStateAction<boolean>>) => ReactNode children?: (v: Dispatch<SetStateAction<boolean>>) => ReactNode
} }
const Log: FC<LogProps> = ({ const Log: FC<LogProps> = ({
containerRef,
children, children,
log,
runID, runID,
setShowModal,
}) => { }) => {
const { t } = useTranslation() const { t } = useTranslation()
const [showModal, setShowModal] = useState(false)
const [width, setWidth] = useState(0)
const adjustModalWidth = () => {
if (containerRef.current)
setWidth(document.body.clientWidth - (containerRef.current?.clientWidth + 56 + 16))
}
useEffect(() => {
adjustModalWidth()
}, [])
return ( return (
<> <>
@ -52,15 +37,6 @@ const Log: FC<LogProps> = ({
</div> </div>
) )
} }
{
showModal && (
<PromptLogModal
width={width}
log={log}
onCancel={() => setShowModal(false)}
/>
)
}
</> </>
) )
} }

View File

@ -2,7 +2,7 @@ import type {
FC, FC,
ReactNode, ReactNode,
} from 'react' } from 'react'
import { memo, useRef } from 'react' import { memo, useEffect, useRef, useState } from 'react'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import type { import type {
ChatConfig, ChatConfig,
@ -18,6 +18,7 @@ import LoadingAnim from '@/app/components/app/chat/loading-anim'
import Citation from '@/app/components/app/chat/citation' import Citation from '@/app/components/app/chat/citation'
import { EditTitle } from '@/app/components/app/annotation/edit-annotation-modal/edit-item' import { EditTitle } from '@/app/components/app/annotation/edit-annotation-modal/edit-item'
import type { Emoji } from '@/app/components/tools/types' import type { Emoji } from '@/app/components/tools/types'
import PromptLogModal from '@/app/components/base/prompt-log-modal'
type AnswerProps = { type AnswerProps = {
item: ChatItem item: ChatItem
@ -40,7 +41,6 @@ const Answer: FC<AnswerProps> = ({
showPromptLog, showPromptLog,
}) => { }) => {
const { t } = useTranslation() const { t } = useTranslation()
const ref = useRef(null)
const { const {
content, content,
citation, citation,
@ -50,6 +50,20 @@ const Answer: FC<AnswerProps> = ({
} = item } = item
const hasAgentThoughts = !!agent_thoughts?.length const hasAgentThoughts = !!agent_thoughts?.length
const [showPromptLogModal, setShowPromptLogModal] = useState(false)
const [width, setWidth] = useState(0)
const ref = useRef<HTMLDivElement>(null)
const adjustModalWidth = () => {
if (ref.current)
setWidth(document.body.clientWidth - (ref.current?.clientWidth + 56 + 16))
}
useEffect(() => {
adjustModalWidth()
}, [])
return ( return (
<div className='flex mb-2 last:mb-0' ref={ref}> <div className='flex mb-2 last:mb-0' ref={ref}>
<div className='shrink-0 relative w-10 h-10'> <div className='shrink-0 relative w-10 h-10'>
@ -79,7 +93,7 @@ const Answer: FC<AnswerProps> = ({
question={question} question={question}
index={index} index={index}
showPromptLog={showPromptLog} showPromptLog={showPromptLog}
containerRef={ref} setShowPromptLogModal={setShowPromptLogModal}
/> />
) )
} }
@ -122,6 +136,13 @@ const Answer: FC<AnswerProps> = ({
</div> </div>
<More more={more} /> <More more={more} />
</div> </div>
{showPromptLogModal && (
<PromptLogModal
width={width}
log={item.log || []}
onCancel={() => setShowPromptLogModal(false)}
/>
)}
</div> </div>
) )
} }

View File

@ -1,4 +1,4 @@
import type { FC, RefObject } from 'react' import type { Dispatch, FC, SetStateAction } from 'react'
import { import {
memo, memo,
useMemo, useMemo,
@ -24,14 +24,14 @@ type OperationProps = {
question: string question: string
index: number index: number
showPromptLog?: boolean showPromptLog?: boolean
containerRef: RefObject<HTMLElement> setShowPromptLogModal: Dispatch<SetStateAction<boolean>>
} }
const Operation: FC<OperationProps> = ({ const Operation: FC<OperationProps> = ({
item, item,
question, question,
index, index,
showPromptLog, showPromptLog,
containerRef, setShowPromptLogModal,
}) => { }) => {
const { t } = useTranslation() const { t } = useTranslation()
const { const {
@ -79,7 +79,7 @@ const Operation: FC<OperationProps> = ({
<div className='hidden group-hover:flex items-center h-[28px] p-0.5 rounded-lg bg-white border-[0.5px] border-gray-100 shadow-md'> <div className='hidden group-hover:flex items-center h-[28px] p-0.5 rounded-lg bg-white border-[0.5px] border-gray-100 shadow-md'>
{showPromptLog && ( {showPromptLog && (
<Log runID={item.workflow_run_id} log={item.log!} containerRef={containerRef} /> <Log runID={item.workflow_run_id} setShowModal={setShowPromptLogModal} />
)} )}
{(!isOpeningStatement && config?.text_to_speech?.enabled) && ( {(!isOpeningStatement && config?.text_to_speech?.enabled) && (
<> <>

View File

@ -4,9 +4,10 @@ import { useClickAway } from 'ahooks'
import Card from './card' import Card from './card'
import { CopyFeedbackNew } from '@/app/components/base/copy-feedback' import { CopyFeedbackNew } from '@/app/components/base/copy-feedback'
import { XClose } from '@/app/components/base/icons/src/vender/line/general' import { XClose } from '@/app/components/base/icons/src/vender/line/general'
import type { VisionFile } from '@/types/app'
type PromptLogModalProps = { type PromptLogModalProps = {
log: { role: string; text: string }[] log: { role: string; text: string; files?: VisionFile[] }[]
width: number width: number
onCancel: () => void onCancel: () => void
} }
@ -19,8 +20,10 @@ const PromptLogModal: FC<PromptLogModalProps> = ({
const [mounted, setMounted] = useState(false) const [mounted, setMounted] = useState(false)
useClickAway(() => { useClickAway(() => {
if (mounted) if (mounted) {
console.log(111)
onCancel() onCancel()
}
}, ref) }, ref)
useEffect(() => { useEffect(() => {