feat(agent): move thread metadata to structured context XML (#373)

* feat(agent): move thread metadata to structured context XML

* feat(core): dynamically inject thread metadata in getSessionEntries
This commit is contained in:
Yiling-J
2026-09-01 17:42:56 +08:00
committed by GitHub
parent c2f477a391
commit 64b68e7d63
11 changed files with 410 additions and 64 deletions
+79
View File
@@ -338,6 +338,85 @@ describe('AgentService', () => {
expect(entries[0].id).toBe(entry1.id)
expect(entries[1].id).toBe(entry2.id)
})
test('getSessionEntries dynamically attaches details.thread on custom_message entries with replies', async () => {
const db = prisma
const svc = new AgentService()
const { agent, team } = await setupTestData(db)
const project = await db.project.create({
data: { name: 'Test Project', teamId: team.id },
})
const asset = await db.asset.create({
data: {
name: 'video.mp4',
type: 'file',
status: 'uploaded',
projectId: project.id,
},
})
const user = await db.user.create({
data: { name: 'Alice', email: `alice-${Date.now()}@example.com` },
})
const rootComment = await db.assetComment.create({
data: {
id: 'comment-root-entries',
assetId: asset.id,
message: 'Top-level comment',
creatorId: user.id,
},
})
// Add a reply
await db.assetComment.create({
data: {
id: 'comment-reply-entries',
assetId: asset.id,
message: 'Reply to root',
creatorId: user.id,
replyToId: rootComment.id,
},
})
const session = await db.agentSession.create({
data: {
agentId: agent.id,
type: 'comment',
cwd: '/tmp',
},
})
const entry = await db.agentSessionEntry.create({
data: {
id: rootComment.id,
sessionId: session.id,
type: 'custom_message',
parentId: null,
data: {
customType: 'shumai_message',
content: 'Top-level comment',
display: true,
details: { user: { id: user.id, name: user.name } },
},
},
})
await db.agentSession.update({
where: { id: session.id },
data: { leafId: entry.id },
})
const entries = await svc.getSessionEntries({ sessionId: session.id })
expect(entries).toHaveLength(1)
expect(entries[0].id).toBe(rootComment.id)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const entryDetails = (entries[0].entry as any).details
expect(entryDetails?.thread).toEqual({
id: rootComment.id,
replyCount: 1,
})
})
})
describe('listSessions', () => {
+33 -1
View File
@@ -549,21 +549,53 @@ export class AgentService {
ORDER BY depth DESC;
`
const entryIds = rows.map((r) => r.id)
const threadReplyCountMap = new Map<string, number>()
if (entryIds.length > 0) {
const threadCounts = await this.prismaClient.assetComment.groupBy({
by: ['replyToId'],
where: {
replyToId: { in: entryIds },
message: { not: '__CHAT__' },
},
_count: {
id: true,
},
})
for (const tc of threadCounts) {
if (tc.replyToId) {
threadReplyCountMap.set(tc.replyToId, tc._count.id)
}
}
}
return rows.map((r) => {
const payload = (r.data as Record<string, unknown>) || {}
const count = threadReplyCountMap.get(r.id)
let details = payload.details as Record<string, unknown> | undefined
if (count !== undefined && count > 0 && r.type === 'custom_message') {
details = {
...details,
thread: {
id: r.id,
replyCount: count,
},
}
}
const entryObj = {
id: r.id,
type: r.type || 'message',
parentId: r.parent_id,
timestamp: r.created_at.toISOString(),
...payload,
...(details ? { details } : {}),
}
return {
id: r.id,
sessionId: r.session_id || params.sessionId,
type: r.type,
parentId: r.parent_id,
data: r.data,
data: details !== payload.details ? { ...payload, details } : r.data,
createdAt: r.created_at,
entry: entryObj,
}