fix profile-aware session history actions (#1011)

This commit is contained in:
ekko
2026-05-25 12:32:42 +08:00
committed by GitHub
parent bbb8b1d536
commit 56c6cf3e2d
7 changed files with 211 additions and 58 deletions
@@ -74,6 +74,11 @@ interface HermesDeleteResult {
error?: string
}
interface BatchDeleteTarget {
id: string
profile?: string | null
}
function hasProfileOnDisk(profile: string): boolean {
return listProfileNamesFromDisk().includes(profile || 'default')
}
@@ -292,15 +297,31 @@ export async function remove(ctx: any) {
}
export async function batchRemove(ctx: any) {
const { ids } = ctx.request.body as { ids?: string[] }
if (!ids || !Array.isArray(ids) || ids.length === 0) {
const { ids, sessions } = ctx.request.body as { ids?: string[]; sessions?: BatchDeleteTarget[] }
const rawTargets = Array.isArray(sessions) && sessions.length > 0 ? sessions : ids
if (!rawTargets || !Array.isArray(rawTargets) || rawTargets.length === 0) {
ctx.status = 400
ctx.body = { error: 'ids is required and must be a non-empty array' }
return
}
const validIds = ids.filter(id => typeof id === 'string' && id.trim() !== '')
if (validIds.length === 0) {
const targets = rawTargets
.map((target): BatchDeleteTarget | null => {
if (typeof target === 'string') {
const id = target.trim()
return id ? { id } : null
}
if (!target || typeof target.id !== 'string') return null
const id = target.id.trim()
if (!id) return null
const profile = typeof target.profile === 'string' && target.profile.trim()
? target.profile.trim()
: undefined
return { id, profile }
})
.filter((target): target is BatchDeleteTarget => Boolean(target))
if (targets.length === 0) {
ctx.status = 400
ctx.body = { error: 'No valid session ids provided' }
return
@@ -315,14 +336,22 @@ export async function batchRemove(ctx: any) {
hermesErrors: [] as Array<{ id: string; profile?: string; error: string }>
}
for (const id of validIds) {
for (const target of targets) {
const { id } = target
const existing = localGetSession(id)
if (existing && !canAccessProfile(ctx, existing.profile)) {
const targetProfile = target.profile || existing?.profile
if (targetProfile && !canAccessProfile(ctx, targetProfile)) {
results.failed++
results.errors.push({ id, error: `Profile "${targetProfile || 'default'}" is not available for this user` })
continue
}
if (!targetProfile && existing && !canAccessProfile(ctx, existing.profile)) {
results.failed++
results.errors.push({ id, error: `Profile "${existing.profile || 'default'}" is not available for this user` })
continue
}
const hermes = await deleteHermesSessionIfPresent(id, existing?.profile)
const hermes = await deleteHermesSessionIfPresent(id, targetProfile)
if (hermes.deleted) {
results.hermesDeleted++
} else if (hermes.attempted && hermes.error) {
@@ -330,13 +359,21 @@ export async function batchRemove(ctx: any) {
results.hermesErrors.push({ id, profile: hermes.profile, error: hermes.error })
}
const ok = localDeleteSession(id)
if (ok) {
deleteUsage(id)
const shouldDeleteLocal = Boolean(existing && (!targetProfile || existing.profile === targetProfile))
if (shouldDeleteLocal) {
const ok = localDeleteSession(id)
if (ok) {
deleteUsage(id)
results.deleted++
} else {
results.failed++
results.errors.push({ id, error: 'Failed to delete session' })
}
} else if (hermes.deleted) {
results.deleted++
} else {
results.failed++
results.errors.push({ id, error: 'Failed to delete session' })
results.errors.push({ id, error: 'Session not found' })
}
}