mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-26 10:55:27 +00:00
fix(agent): assign lower sortIndex to newer versions to ensure they are marked as latest
This commit is contained in:
@@ -567,5 +567,78 @@ describe('Database Activities', () => {
|
||||
expect(types).toContain('ai_metadata_autofill')
|
||||
expect(types).toContain('ai_embedding')
|
||||
})
|
||||
|
||||
it('should assign a lower sortIndex to newer versions so they are correctly marked as latest', async () => {
|
||||
const team = await prisma.team.create({ data: { name: 'Version Team' } })
|
||||
const user = await prisma.user.create({
|
||||
data: { name: 'Version User', email: 'vuser@example.com' },
|
||||
})
|
||||
await prisma.teamMember.create({
|
||||
data: { teamId: team.id, userId: user.id, role: 'owner' },
|
||||
})
|
||||
const project = await prisma.project.create({
|
||||
data: { name: 'Version Project', teamId: team.id },
|
||||
})
|
||||
const parentFolder = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'Parent Folder',
|
||||
type: 'folder',
|
||||
projectId: project.id,
|
||||
status: 'uploaded',
|
||||
},
|
||||
})
|
||||
|
||||
// Create file1 (V1)
|
||||
const file1 = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'file1.txt',
|
||||
type: 'file',
|
||||
parentId: parentFolder.id,
|
||||
projectId: project.id,
|
||||
status: 'uploaded',
|
||||
sizeByte: 100,
|
||||
},
|
||||
})
|
||||
|
||||
// 1. Create file2 (V2) - Creates a new stack
|
||||
const file2Result = await executeAgentToolActivity({
|
||||
taskId: 'vstack-task-1',
|
||||
toolName: 'create_version',
|
||||
args: {
|
||||
parent: file1.id,
|
||||
s3Key: 'file/file2-key/raw',
|
||||
name: 'file2.txt',
|
||||
size: 150,
|
||||
contentType: 'text/plain',
|
||||
},
|
||||
userId: user.id,
|
||||
})
|
||||
|
||||
// Fetch from DB to check sortIndex
|
||||
const updatedFile1 = await prisma.asset.findUnique({ where: { id: file1.id } })
|
||||
const updatedFile2 = await prisma.asset.findUnique({ where: { id: file2Result.id } })
|
||||
|
||||
// Newest version (file2) must have a strictly lower sortIndex than older version (file1)
|
||||
expect(updatedFile2!.sortIndex! < updatedFile1!.sortIndex!).toBe(true)
|
||||
|
||||
// 2. Create file3 (V3) - Appends to existing stack
|
||||
const file3Result = await executeAgentToolActivity({
|
||||
taskId: 'vstack-task-2',
|
||||
toolName: 'create_version',
|
||||
args: {
|
||||
parent: file2Result.id,
|
||||
s3Key: 'file/file3-key/raw',
|
||||
name: 'file3.txt',
|
||||
size: 200,
|
||||
contentType: 'text/plain',
|
||||
},
|
||||
userId: user.id,
|
||||
})
|
||||
|
||||
const updatedFile3 = await prisma.asset.findUnique({ where: { id: file3Result.id } })
|
||||
|
||||
// Newest version (file3) must have a strictly lower sortIndex than version 2 (file2)
|
||||
expect(updatedFile3!.sortIndex! < updatedFile2!.sortIndex!).toBe(true)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -625,11 +625,6 @@ export async function updateWorkflowTaskActivity(params: UpdateWorkflowTaskParam
|
||||
// Agent System Tools & Context Activities
|
||||
// ==========================================
|
||||
|
||||
function generateSortIndex(previous?: string | null): string {
|
||||
if (!previous) return generateKeyBetween(null, null)
|
||||
return generateKeyBetween(previous, null)
|
||||
}
|
||||
|
||||
export async function getAssetPathContextActivity(assetId: string): Promise<string> {
|
||||
const parts: { name: string; id: string }[] = []
|
||||
let currentId: string | null = assetId
|
||||
@@ -924,12 +919,12 @@ export async function executeAgentToolActivity(params: ExecuteAgentToolParams):
|
||||
creatorId: userId,
|
||||
})
|
||||
|
||||
// Generate sort index for new version inside the stack
|
||||
const lastChild = await prisma.asset.findFirst({
|
||||
// Generate sort index for new version inside the stack (newest gets the lowest sortIndex)
|
||||
const firstChild = await prisma.asset.findFirst({
|
||||
where: { parentId: stackId },
|
||||
orderBy: { sortIndex: 'desc' },
|
||||
orderBy: { sortIndex: 'asc' },
|
||||
})
|
||||
const newSortIndex = generateSortIndex(lastChild?.sortIndex)
|
||||
const newSortIndex = generateKeyBetween(null, firstChild?.sortIndex || null)
|
||||
|
||||
await prisma.$transaction(async (tx) => {
|
||||
// Assign sort index
|
||||
@@ -992,7 +987,7 @@ export async function executeAgentToolActivity(params: ExecuteAgentToolParams):
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const txVersionStackService = new VersionStackService(tx as any)
|
||||
await txVersionStackService.createVersionStack({
|
||||
fileIds: [parentFile.id, newFile.id],
|
||||
fileIds: [newFile.id, parentFile.id],
|
||||
projectId: parentFile.projectId!,
|
||||
creatorId: userId,
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user