From 83695999eaa0a663cf33463f0a8265a36abefcea Mon Sep 17 00:00:00 2001 From: Joel Date: Mon, 29 Jan 2024 13:33:15 +0800 Subject: [PATCH] feat: conversation support show thought --- .../[conversationId]/name/route.ts | 1 - app/components/index.tsx | 6 ++++- utils/tools.ts | 26 +++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 utils/tools.ts diff --git a/app/api/conversations/[conversationId]/name/route.ts b/app/api/conversations/[conversationId]/name/route.ts index 0b7cffe..d9dd327 100644 --- a/app/api/conversations/[conversationId]/name/route.ts +++ b/app/api/conversations/[conversationId]/name/route.ts @@ -15,6 +15,5 @@ export async function POST(request: NextRequest, { params }: { // auto generate name const { data } = await client.renameConversation(conversationId, name, user, auto_generate) - console.log(conversationId, name, user, auto_generate) return NextResponse.json(data) } diff --git a/app/components/index.tsx b/app/components/index.tsx index 9cc3f41..111e81d 100644 --- a/app/components/index.tsx +++ b/app/components/index.tsx @@ -21,6 +21,7 @@ import { replaceVarWithValues, userInputsFormToPromptVariables } from '@/utils/p import AppUnavailable from '@/app/components/app-unavailable' import { API_KEY, APP_ID, APP_INFO, isShowPrompt, promptTemplate } from '@/config' import type { Annotation as AnnotationType } from '@/types/log' +import { addFileInfos, sortAgentSorts } from '@/utils/tools' const Main: FC = () => { const { t } = useTranslation() @@ -130,13 +131,16 @@ const Main: FC = () => { id: `question-${item.id}`, content: item.query, isAnswer: false, - message_files: item.message_files, + message_files: item.message_files?.filter((file: any) => file.belongs_to === 'user') || [], + }) newChatList.push({ id: item.id, content: item.answer, + agent_thoughts: addFileInfos(item.agent_thoughts ? sortAgentSorts(item.agent_thoughts) : item.agent_thoughts, item.message_files), feedback: item.feedback, isAnswer: true, + message_files: item.message_files?.filter((file: any) => file.belongs_to === 'assistant') || [], }) }) setChatList(newChatList) diff --git a/utils/tools.ts b/utils/tools.ts new file mode 100644 index 0000000..a6ecb93 --- /dev/null +++ b/utils/tools.ts @@ -0,0 +1,26 @@ +import type { ThoughtItem } from '@/app/components/chat/type' +import type { VisionFile } from '@/types/app' + +export const sortAgentSorts = (list: ThoughtItem[]) => { + if (!list) + return list + if (list.some(item => item.position === undefined)) + return list + const temp = [...list] + temp.sort((a, b) => a.position - b.position) + return temp +} + +export const addFileInfos = (list: ThoughtItem[], messageFiles: VisionFile[]) => { + if (!list || !messageFiles) + return list + return list.map((item) => { + if (item.files && item.files?.length > 0) { + return { + ...item, + message_files: item.files.map(fileId => messageFiles.find(file => file.id === fileId)) as VisionFile[], + } + } + return item + }) +}