refactor(agent): optimize ULID id sorting and exclude __CHAT__ placeholder comments

This commit is contained in:
yiling
2026-07-29 14:59:48 +08:00
parent a756f7dc2a
commit 1c7a87488b
4 changed files with 41 additions and 6 deletions
+32 -1
View File
@@ -597,7 +597,7 @@ describe('Agent Database Activities Integration', () => {
// Retrieve main session entries (contains top-level comments mainComment1 and mainComment2)
const mainEntries = await prisma.agentSessionEntry.findMany({
where: { sessionId: mainSession!.id },
orderBy: { createdAt: 'asc' },
orderBy: { id: 'asc' },
})
// Main session contains mainComment1 and mainComment2, but NOT threadReply1
@@ -878,6 +878,37 @@ describe('Agent Database Activities Integration', () => {
).resolves.not.toThrow()
})
it('should not tag Thread ID when only __CHAT__ placeholder comment exists under root comment', async () => {
const rootComment = await prisma.assetComment.create({
data: { assetId: asset.id, message: 'Root comment asking agent', creatorId: user.id },
})
// Create placeholder comment under rootComment
await prisma.assetComment.create({
data: {
assetId: asset.id,
message: '__CHAT__',
creatorId: user.id,
replyToId: rootComment.id,
},
})
const sessionId = await initializeAgentSessionActivity({
teamId: team.id,
agentId: 'test-agent-id',
userCommentId: rootComment.id,
userId: user.id,
})
const storage = new DatabaseSessionStorage(sessionId)
const pathEntries = await storage.getPathToRoot(rootComment.id)
const rootEntry = pathEntries.find((e) => e.id === rootComment.id)
expect(rootEntry).toBeDefined()
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- entry data structure parsing
const content = (rootEntry as any)?.message?.content?.[0]?.text || ''
expect(content).not.toContain(`[Thread ID: ${rootComment.id}]`)
})
it('should return user name and role for team member', async () => {
const info = await getUserTeamInfoActivity({
userId: user.id,
+3 -3
View File
@@ -547,14 +547,14 @@ export async function initializeAgentSessionActivity(params: {
})
}
// 1. Fetch top-level comments up to rootComment.createdAt
// 1. Fetch top-level comments up to rootComment.createdAt sorted by id
const topLevelComments = await prisma.assetComment.findMany({
where: {
assetId: userComment.assetId,
replyToId: null,
createdAt: { lte: rootComment.createdAt },
},
orderBy: { createdAt: 'asc' },
orderBy: { id: 'asc' },
include: { creator: true },
})
@@ -566,7 +566,7 @@ export async function initializeAgentSessionActivity(params: {
createdAt: { lte: userComment.createdAt },
id: { not: userComment.id },
},
orderBy: { createdAt: 'asc' },
orderBy: { id: 'asc' },
include: { creator: true },
})
: []
@@ -265,6 +265,7 @@ export class DatabaseSessionStorage implements SessionStorage<DatabaseSessionMet
by: ['replyToId'],
where: {
replyToId: { in: entryIds },
message: { not: '__CHAT__' },
},
})
const commentIdsWithThreads = new Set<string>()
+5 -2
View File
@@ -28,8 +28,11 @@ export const createReadThreadTool = (): AgentTool<
}
const replies = await prisma.assetComment.findMany({
where: { replyToId: rootComment.id },
orderBy: { createdAt: 'asc' },
where: {
replyToId: rootComment.id,
message: { not: '__CHAT__' },
},
orderBy: { id: 'asc' },
include: { creator: true },
})