feat(server): allow drop session (#13650)

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **Bug Fixes**
* Ensures deleted sessions and their messages are consistently cleaned
up, preventing lingering pinned or partially removed items.

* **Refactor**
* Streamlined session cleanup into a single bulk operation for improved
reliability and performance during deletions.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
DarkSky
2025-09-26 14:38:13 +08:00
committed by GitHub
parent 54498df247
commit c540400496

View File

@@ -459,27 +459,19 @@ export class CopilotSessionModel extends BaseModel {
docId: options.docId, docId: options.docId,
deletedAt: null, deletedAt: null,
}, },
select: { id: true, prompt: true }, select: { id: true },
}); });
const sessionIds = sessions.map(({ id }) => id); const sessionIds = sessions.map(({ id }) => id);
// cleanup all messages // cleanup all messages
await this.db.aiSessionMessage.deleteMany({ await this.db.aiSessionMessage.deleteMany({
where: { sessionId: { in: sessionIds } }, where: { sessionId: { in: sessionIds } },
}); });
// only mark action session as deleted
// chat session always can be reuse
const actionIds = sessions
.filter(({ prompt }) => !!prompt.action)
.map(({ id }) => id);
// 标记 action session 为已删除
if (actionIds.length > 0) {
await this.db.aiSession.updateMany({ await this.db.aiSession.updateMany({
where: { id: { in: actionIds } }, where: { id: { in: sessionIds } },
data: { pinned: false, deletedAt: new Date() }, data: { pinned: false, deletedAt: new Date() },
}); });
}
return sessionIds; return sessionIds;
} }