mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-05 15:44:28 +00:00
fix(agent): use upsert in DatabaseSessionStorage appendEntry to handle existing entry IDs
This commit is contained in:
@@ -844,6 +844,40 @@ describe('Agent Database Activities Integration', () => {
|
|||||||
expect(mainPathIds).toContain(msg3.id)
|
expect(mainPathIds).toContain(msg3.id)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should safely upsert entry without unique constraint error when appendEntry is called for pre-existing userComment.id', async () => {
|
||||||
|
const msg3 = await prisma.assetComment.create({
|
||||||
|
data: { assetId: asset.id, message: '<@shumai-x1> what did i say', creatorId: user.id },
|
||||||
|
})
|
||||||
|
|
||||||
|
const sessionId = await initializeAgentSessionActivity({
|
||||||
|
teamId: team.id,
|
||||||
|
agentId: 'test-agent-id',
|
||||||
|
userCommentId: msg3.id,
|
||||||
|
userId: user.id,
|
||||||
|
})
|
||||||
|
|
||||||
|
const storage = new DatabaseSessionStorage(sessionId)
|
||||||
|
storage.nextEntryId = msg3.id
|
||||||
|
|
||||||
|
const entryId = await storage.createEntryId()
|
||||||
|
expect(entryId).toBe(msg3.id)
|
||||||
|
|
||||||
|
// appendEntry should upsert msg3.id without throwing unique constraint error
|
||||||
|
await expect(
|
||||||
|
storage.appendEntry({
|
||||||
|
id: entryId,
|
||||||
|
type: 'message',
|
||||||
|
parentId: null,
|
||||||
|
timestamp: new Date().toISOString(),
|
||||||
|
message: {
|
||||||
|
role: 'user',
|
||||||
|
content: [{ type: 'text', text: 'Updated content' }],
|
||||||
|
},
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- testing mock entry casting
|
||||||
|
} as any),
|
||||||
|
).resolves.not.toThrow()
|
||||||
|
})
|
||||||
|
|
||||||
it('should return user name and role for team member', async () => {
|
it('should return user name and role for team member', async () => {
|
||||||
const info = await getUserTeamInfoActivity({
|
const info = await getUserTeamInfoActivity({
|
||||||
userId: user.id,
|
userId: user.id,
|
||||||
|
|||||||
@@ -154,8 +154,9 @@ export class DatabaseSessionStorage implements SessionStorage<DatabaseSessionMet
|
|||||||
})
|
})
|
||||||
|
|
||||||
await prisma.$transaction([
|
await prisma.$transaction([
|
||||||
prisma.agentSessionEntry.create({
|
prisma.agentSessionEntry.upsert({
|
||||||
data: {
|
where: { id: entry.id },
|
||||||
|
create: {
|
||||||
id: entry.id,
|
id: entry.id,
|
||||||
sessionId: this.sessionId,
|
sessionId: this.sessionId,
|
||||||
assetId: session?.assetId || null,
|
assetId: session?.assetId || null,
|
||||||
@@ -164,6 +165,9 @@ export class DatabaseSessionStorage implements SessionStorage<DatabaseSessionMet
|
|||||||
createdAt: entry.timestamp ? new Date(entry.timestamp) : new Date(),
|
createdAt: entry.timestamp ? new Date(entry.timestamp) : new Date(),
|
||||||
data: payload as PrismaJson.PiSessionEntryData,
|
data: payload as PrismaJson.PiSessionEntryData,
|
||||||
},
|
},
|
||||||
|
update: {
|
||||||
|
data: payload as PrismaJson.PiSessionEntryData,
|
||||||
|
},
|
||||||
}),
|
}),
|
||||||
prisma.agentSession.update({
|
prisma.agentSession.update({
|
||||||
where: { id: this.sessionId },
|
where: { id: this.sessionId },
|
||||||
|
|||||||
Reference in New Issue
Block a user