Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix(web): apply log annotations to the displayed branch (#43238)

Juhyuk choi688005d9fc61
diff --git a/web/app/components/app/log/__tests__/list-utils.spec.ts b/web/app/components/app/log/__tests__/list-utils.spec.ts
index 5d31b7fa61e..d3b152fd890 100644
--- a/web/app/components/app/log/__tests__/list-utils.spec.ts
+++ b/web/app/components/app/log/__tests__/list-utils.spec.ts
@@ -6,6 +6,7 @@ import {
applyAnnotationEdited,
applyAnnotationRemoved,
buildChatThreadState,
+ getAllChatItemIndex,
getCompletionMessageFiles,
getConversationRowValues,
getDetailVarList,
@@ -203,6 +204,24 @@ describe('log list utils', () => {
expect(items[1]!.annotation).toBeUndefined()
})
+ it('should map a thread index to allChatItems when the conversation has regenerated branches', () => {
+ const allChatItems = [
+ ...createChatItems(),
+ { id: 'question-3', content: 'next', isAnswer: false, parentMessageId: 'answer-1' },
+ { id: 'answer-3', content: 'regenerated', isAnswer: true, parentMessageId: 'question-3' },
+ ] as IChatItem[]
+ const { threadChatItems } = buildChatThreadState({ allChatItems, hasMore: false })
+
+ expect(threadChatItems.map((item) => item.id)).toEqual([
+ 'question-1',
+ 'answer-1',
+ 'question-3',
+ 'answer-3',
+ ])
+ expect(getAllChatItemIndex(allChatItems, threadChatItems, 3)).toBe(5)
+ expect(getAllChatItemIndex(allChatItems, threadChatItems, 4)).toBe(-1)
+ })
+
it('should derive urls, scroll thresholds, row values, and detail metadata', () => {
expect(
isNearTopLoadMore({
diff --git a/web/app/components/app/log/__tests__/list.spec.tsx b/web/app/components/app/log/__tests__/list.spec.tsx
index f0c5b567ed8..b5eb8716f86 100644
--- a/web/app/components/app/log/__tests__/list.spec.tsx
+++ b/web/app/components/app/log/__tests__/list.spec.tsx
@@ -433,6 +433,40 @@ it('adds, edits and removes an annotation from a conversation answer', async ()
})
})
+it('removes the annotation of the displayed answer when the conversation has regenerated answers', async () => {
+ const user = userEvent.setup()
+ api.messages.mockResolvedValue({
+ data: [
+ createChatMessage('message-1'),
+ createChatMessage('message-2', { parent_message_id: 'message-1' }),
+ createChatMessage('message-3', {
+ parent_message_id: 'message-1',
+ annotation: {
+ id: 'annotation-3',
+ content: 'Annotated answer',
+ account: { id: 'account-1', email: '[email protected]', name: 'Admin' },
+ created_at: 1710000100,
+ },
+ }),
+ ],
+ has_more: false,
+ limit: 10,
+ })
+ renderConversationList({ searchParams: '?conversation_id=conversation-1' })
+ await user.click(await screen.findByRole('button', { name: 'appDebug.feature.annotation.edit' }))
+ const editor = await screen.findByRole('dialog', { name: 'appAnnotation.editModal.title' })
+ await user.click(
+ within(editor).getByRole('button', { name: 'appAnnotation.editModal.removeThisCache' }),
+ )
+ const confirm = await screen.findByRole('alertdialog')
+ await user.click(within(confirm).getByRole('button', { name: 'common.operation.confirm' }))
+ await waitFor(() =>
+ expect(api.removeAnnotation).toHaveBeenCalledExactlyOnceWith({
+ params: { app_id: 'app-1', annotation_id: 'annotation-3' },
+ }),
+ )
+})
+
it('loads older messages at the top using the oldest answer as the pagination cursor', async () => {
api.messages
.mockResolvedValueOnce({
diff --git a/web/app/components/app/log/conversation-detail.tsx b/web/app/components/app/log/conversation-detail.tsx
index 7faaef76228..777ca994cd4 100644
--- a/web/app/components/app/log/conversation-detail.tsx
+++ b/web/app/components/app/log/conversation-detail.tsx
@@ -41,6 +41,7 @@ import {
applyAnnotationRemoved,
buildChatThreadState,
getAdminFeedback,
+ getAllChatItemIndex,
getCompletionMessageFiles,
getDetailVarList,
getFormattedChatList,
@@ -102,30 +103,35 @@ function DetailPanel({ appDetail, detail, onFeedback, initialMessages }: IDetail
const handleAnnotationEdited = useCallback(
(query: string, answer: string, index: number) => {
- setAllChatItems(applyAnnotationEdited(allChatItems, query, answer, index))
+ const itemIndex = getAllChatItemIndex(allChatItems, threadChatItems, index)
+ if (itemIndex < 0) return
+ setAllChatItems(applyAnnotationEdited(allChatItems, query, answer, itemIndex))
},
- [allChatItems],
+ [allChatItems, threadChatItems],
)
const handleAnnotationAdded = useCallback(
(annotationId: string, authorName: string, query: string, answer: string, index: number) => {
+ const itemIndex = getAllChatItemIndex(allChatItems, threadChatItems, index)
+ if (itemIndex < 0) return
setAllChatItems(
- applyAnnotationAdded(allChatItems, annotationId, authorName, query, answer, index),
+ applyAnnotationAdded(allChatItems, annotationId, authorName, query, answer, itemIndex),
)
},
- [allChatItems],
+ [allChatItems, threadChatItems],
)
const handleAnnotationRemoved = useCallback(
async (index: number): Promise<boolean> => {
- const annotation = allChatItems[index]?.annotation
+ const itemIndex = getAllChatItemIndex(allChatItems, threadChatItems, index)
+ const annotation = allChatItems[itemIndex]?.annotation
try {
- if (annotation?.id) {
- await consoleClient.apps.byAppId.annotations.byAnnotationId.delete({
- params: { app_id: appDetail.id, annotation_id: annotation.id },
- })
- }
+ if (!annotation?.id) throw new Error('Annotation not found')
- setAllChatItems(applyAnnotationRemoved(allChatItems, index))
+ await consoleClient.apps.byAppId.annotations.byAnnotationId.delete({
+ params: { app_id: appDetail.id, annotation_id: annotation.id },
+ })
+
+ setAllChatItems(applyAnnotationRemoved(allChatItems, itemIndex))
toast.success(t(($) => $['actionMsg.modifiedSuccessfully'], { ns: 'common' }))
return true
@@ -134,7 +140,7 @@ function DetailPanel({ appDetail, detail, onFeedback, initialMessages }: IDetail
return false
}
},
- [allChatItems, appDetail.id, t],
+ [allChatItems, appDetail.id, t, threadChatItems],
)
const [isLoading, setIsLoading] = useState(false)
diff --git a/web/app/components/app/log/list-utils.ts b/web/app/components/app/log/list-utils.ts
index 8aa8066caf7..f74cd53ba83 100644
--- a/web/app/components/app/log/list-utils.ts
+++ b/web/app/components/app/log/list-utils.ts
@@ -327,6 +327,19 @@ export const applyAnnotationRemoved = (items: IChatItem[], index: number) =>
return item
})
+
+// The chat reports indexes within the displayed thread, which can differ from
+// allChatItems when a conversation has regenerated branches or an opening statement.
+export const getAllChatItemIndex = (
+ allChatItems: IChatItem[],
+ threadChatItems: IChatItem[],
+ threadIndex: number,
+) => {
+ const itemId = threadChatItems[threadIndex]?.id
+ if (!itemId) return -1
+ return allChatItems.findIndex((item) => item.id === itemId)
+}
+
export const isNearTopLoadMore = ({
clientHeight,
scrollHeight,