fix(comment): fix reply UI race condition and normalize session routing on agent reply

This commit is contained in:
yiling
2026-05-22 10:57:55 +08:00
parent 96d05083fa
commit 71f2ebdd1a
4 changed files with 63 additions and 25 deletions
+29 -7
View File
@@ -726,6 +726,15 @@ describe('AssetService', () => {
expect(p2.sessionId).toBeUndefined() expect(p2.sessionId).toBeUndefined()
expect(p2.explicitMention).toBe(true) expect(p2.explicitMention).toBe(true)
// Create a root user comment
const userRoot = await prisma.assetComment.create({
data: {
assetId: file.id,
creatorId: user.id,
message: 'User root comment',
},
})
// Create an agent session first for the foreign key constraint // Create an agent session first for the foreign key constraint
await prisma.agentSession.create({ await prisma.agentSession.create({
data: { data: {
@@ -735,25 +744,32 @@ describe('AssetService', () => {
}, },
}) })
// Create an agent root comment with sessionId // Create an agent comment as a reply to userRoot with sessionId
const agentRoot = await prisma.assetComment.create({ const agentReply = await prisma.assetComment.create({
data: { data: {
assetId: file.id, assetId: file.id,
creatorId: botUser.id, creatorId: botUser.id,
message: 'Agent root comment', message: 'Agent reply comment',
replyToId: userRoot.id,
sessionId: 'test-session-rule3', sessionId: 'test-session-rule3',
}, },
}) })
// Rule 3a: any user creates a reply, root is agent comment, no explicit mention // Rule 3a: any user creates a reply directly to the agent comment, no explicit mention
const reply3a = await assetService.createComment({ const reply3a = await assetService.createComment({
assetId: file.id, assetId: file.id,
userId: user.id, userId: user.id,
message: 'Rule 3a: reply without mention', message: 'Rule 3a: reply without mention',
replyToId: agentRoot.id, replyToId: agentReply.id,
attachmentIds: [], attachmentIds: [],
}) })
// Verify replyToId of reply3a was normalized to the root comment userRoot.id
const dbReply3a = await prisma.assetComment.findUnique({
where: { id: reply3a.id },
})
expect(dbReply3a?.replyToId).toBe(userRoot.id)
const tasksRule3a = await prisma.workflowTask.findMany({ const tasksRule3a = await prisma.workflowTask.findMany({
where: { assetId: file.id, payload: { path: ['userCommentId'], equals: reply3a.id } }, where: { assetId: file.id, payload: { path: ['userCommentId'], equals: reply3a.id } },
}) })
@@ -764,15 +780,21 @@ describe('AssetService', () => {
expect(p3a.sessionId).toBe('test-session-rule3') expect(p3a.sessionId).toBe('test-session-rule3')
expect(p3a.explicitMention).toBe(false) expect(p3a.explicitMention).toBe(false)
// Rule 3b: any user creates a reply, root is agent comment, explicitly mentions agent // Rule 3b: any user creates a reply directly to the agent comment, explicitly mentions agent
const reply3b = await assetService.createComment({ const reply3b = await assetService.createComment({
assetId: file.id, assetId: file.id,
userId: user.id, userId: user.id,
message: `Rule 3b: reply with mention <@${botUser.id}>`, message: `Rule 3b: reply with mention <@${botUser.id}>`,
replyToId: agentRoot.id, replyToId: agentReply.id,
attachmentIds: [], attachmentIds: [],
}) })
// Verify replyToId of reply3b was normalized to the root comment userRoot.id
const dbReply3b = await prisma.assetComment.findUnique({
where: { id: reply3b.id },
})
expect(dbReply3b?.replyToId).toBe(userRoot.id)
const tasksRule3b = await prisma.workflowTask.findMany({ const tasksRule3b = await prisma.workflowTask.findMany({
where: { assetId: file.id, payload: { path: ['userCommentId'], equals: reply3b.id } }, where: { assetId: file.id, payload: { path: ['userCommentId'], equals: reply3b.id } },
}) })
+17 -10
View File
@@ -771,6 +771,18 @@ export class AssetService {
}) })
if (!a) throw new Error('Asset not found') if (!a) throw new Error('Asset not found')
let actualReplyToId = req.replyToId
let parentComment = null
if (req.replyToId) {
parentComment = await tx.assetComment.findUnique({
where: { id: req.replyToId },
include: { creator: true },
})
if (parentComment?.replyToId) {
actualReplyToId = parentComment.replyToId
}
}
const comment = await tx.assetComment.create({ const comment = await tx.assetComment.create({
data: { data: {
assetId: a.id, assetId: a.id,
@@ -778,7 +790,7 @@ export class AssetService {
message: req.message, message: req.message,
annotation: req.annotations, annotation: req.annotations,
second: req.second, second: req.second,
replyToId: req.replyToId, replyToId: actualReplyToId,
}, },
}) })
commentId = comment.id commentId = comment.id
@@ -802,15 +814,10 @@ export class AssetService {
const mentionedAgentIds = new Set(botMentionMatches.map((match) => match[1])) const mentionedAgentIds = new Set(botMentionMatches.map((match) => match[1]))
const handledAgentIds = new Set<string>() const handledAgentIds = new Set<string>()
if (req.replyToId && a.project) { if (parentComment && a.project) {
const rootComment = await tx.assetComment.findUnique({ const rootSessionId = parentComment.sessionId
where: { id: req.replyToId }, const isRootAgent = !!rootSessionId || parentComment.creator?.type === 'agent'
include: { creator: true }, const rootAgentId = parentComment.creatorId
})
const rootSessionId = rootComment?.sessionId
const isRootAgent =
!!rootComment && (!!rootSessionId || rootComment.creator?.type === 'agent')
const rootAgentId = rootComment?.creatorId
if (isRootAgent && rootAgentId) { if (isRootAgent && rootAgentId) {
const explicitMention = mentionedAgentIds.has(rootAgentId) const explicitMention = mentionedAgentIds.has(rootAgentId)
+7 -2
View File
@@ -35,7 +35,12 @@ type UploadingFile = {
interface ChatInputProps { interface ChatInputProps {
projectId: string projectId: string
onSendMessage: (text: string, attachmentIds: string[], annotations?: Annotation[]) => void onSendMessage: (
text: string,
attachmentIds: string[],
annotations?: Annotation[],
replyToId?: string | null,
) => void
replyingTo?: CommentInfo | null replyingTo?: CommentInfo | null
onCancelReply?: () => void onCancelReply?: () => void
users?: UserInfo[] users?: UserInfo[]
@@ -376,7 +381,7 @@ export const ChatInput = React.forwardRef<HTMLDivElement, ChatInputProps>(
// Allow sending if annotations exist, even if text is empty // Allow sending if annotations exist, even if text is empty
if (!processedText && successfulAttachmentIds.length === 0 && annotations.length === 0) return if (!processedText && successfulAttachmentIds.length === 0 && annotations.length === 0) return
onSendMessage(processedText, successfulAttachmentIds, annotations) onSendMessage(processedText, successfulAttachmentIds, annotations, replyingTo?.id)
// Reset editor // Reset editor
editorRef.current.innerHTML = '' editorRef.current.innerHTML = ''
@@ -52,6 +52,7 @@ export function FileViewerRightSidebar({
text: string text: string
attachmentIds: string[] attachmentIds: string[]
annotations?: Annotation[] annotations?: Annotation[]
replyToId?: string | null
} | null>(null) } | null>(null)
const { data: apiFields } = useQuery({ const { data: apiFields } = useQuery({
@@ -135,11 +136,13 @@ export function FileViewerRightSidebar({
text, text,
attachmentIds, attachmentIds,
annotations, annotations,
replyToId,
guestUserId, guestUserId,
}: { }: {
text: string text: string
attachmentIds: string[] attachmentIds: string[]
annotations?: Annotation[] annotations?: Annotation[]
replyToId?: string | null
guestUserId?: string guestUserId?: string
}) => { }) => {
if (isPublic) { if (isPublic) {
@@ -150,7 +153,7 @@ export function FileViewerRightSidebar({
json: { json: {
message: text, message: text,
attachmentIds: attachmentIds, attachmentIds: attachmentIds,
replyToId: replyingTo?.id, replyToId: replyToId ?? undefined,
annotations: annotations, annotations: annotations,
}, },
}, },
@@ -169,7 +172,7 @@ export function FileViewerRightSidebar({
json: { json: {
message: text, message: text,
attachmentIds: attachmentIds, attachmentIds: attachmentIds,
replyToId: replyingTo?.id, replyToId: replyToId ?? undefined,
annotations: annotations, annotations: annotations,
}, },
}) })
@@ -220,6 +223,7 @@ export function FileViewerRightSidebar({
text: string, text: string,
attachmentIds: string[], attachmentIds: string[],
annotations?: Annotation[], annotations?: Annotation[],
replyToId?: string | null,
) => { ) => {
if (!file?.id) return if (!file?.id) return
@@ -235,16 +239,16 @@ export function FileViewerRightSidebar({
console.log('[handleSendMessage] guestUserId from storage:', guestUserId) console.log('[handleSendMessage] guestUserId from storage:', guestUserId)
if (!guestUserId) { if (!guestUserId) {
console.log('[handleSendMessage] no guest ID, opening popup') console.log('[handleSendMessage] no guest ID, opening popup')
setPendingComment({ text, attachmentIds, annotations }) setPendingComment({ text, attachmentIds, annotations, replyToId })
setIsGuestPopupOpen(true) setIsGuestPopupOpen(true)
return return
} }
createComment({ text, attachmentIds, annotations, guestUserId }) createComment({ text, attachmentIds, annotations, replyToId, guestUserId })
} else { } else {
createComment({ text, attachmentIds, annotations }) createComment({ text, attachmentIds, annotations, replyToId })
} }
} else { } else {
createComment({ text, attachmentIds, annotations }) createComment({ text, attachmentIds, annotations, replyToId })
} }
setReplyingTo(null) setReplyingTo(null)
} }