Merge pull request #58 from Askhz/fix/delete-session

fix: DELETE 历史 session 时删除磁盘 transcript 文件
This commit is contained in:
DoSun 2026-05-11 16:15:27 +08:00 committed by GitHub
commit ad2d4508c8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 34 additions and 20 deletions

View File

@ -322,29 +322,30 @@ export function createSessionRoutes(
.patch('/session/:sessionID', async c => {
const id = c.req.param('sessionID')
const handle = sessionManager.getSession(id)
if (!handle) throw notFound('session not found')
const body = await c.req.json<{
title?: string
model?: string
permission_mode?: string
time?: { archived?: number }
}>()
if (body.title) handle.setTitle(body.title)
if (body.model) await handle.setModel(body.model)
if (body.permission_mode) await handle.setPermissionMode(body.permission_mode)
if (handle) {
if (body.title) handle.setTitle(body.title)
if (body.model) await handle.setModel(body.model)
if (body.permission_mode) await handle.setPermissionMode(body.permission_mode)
}
return c.json({
session_id: id,
title: handle.title ?? body.title,
model: handle.model,
permission_mode: handle.permissionMode,
title: handle?.title ?? body.title,
model: handle?.model,
permission_mode: handle?.permissionMode,
})
})
.delete('/session/:sessionID', async c => {
const id = c.req.param('sessionID')
const deleted = await sessionManager.deleteSession(id)
if (!deleted) throw notFound('session not found')
await sessionManager.deleteSession(id)
return c.json({ deleted: true })
})
.post('/session/:sessionID/prompt', async c => {

View File

@ -1,5 +1,5 @@
import { readFile, writeFile, mkdir } from 'fs/promises'
import { existsSync, statSync } from 'fs'
import { existsSync, statSync, unlinkSync } from 'fs'
import { join, resolve, isAbsolute } from 'path'
import { getClaudeConfigHomeDir } from '../utils/envUtils.js'
import { logError } from '../utils/log.js'
@ -7,7 +7,7 @@ import type { EventBus } from './eventBus.js'
import { SessionHandle } from './sessionHandle.js'
import type { InitData } from './types.js'
import type { SessionIndex, SessionIndexEntry, SessionBusyStatus, SessionState } from './types.js'
import { canonicalizePath } from '../utils/sessionStoragePortable.js'
import { canonicalizePath, findProjectDir, resolveSessionFilePath } from '../utils/sessionStoragePortable.js'
const INDEX_FILE = 'server-sessions.json'
@ -264,16 +264,29 @@ export class SessionManager {
async deleteSession(id: string): Promise<boolean> {
const handle = this.sessions.get(id)
if (!handle) return false
const silent = handle.silent
handle.forceKill()
this.sessions.delete(id)
this.eventBus.unregisterSessionCwd(id)
if (!silent) {
this.eventBus.publishSessionEvent(id, 'deleted', { status: 'stopped' })
if (handle) {
const silent = handle.silent
handle.forceKill()
this.sessions.delete(id)
this.eventBus.unregisterSessionCwd(id)
if (!silent) {
this.eventBus.publishSessionEvent(id, 'deleted', { status: 'stopped' })
}
this.scheduleIndexSave()
return true
}
this.scheduleIndexSave()
return true
// 历史 session 不在内存中,删除磁盘 transcript 文件
try {
const resolved = await resolveSessionFilePath(id)
if (resolved) {
unlinkSync(resolved.filePath)
this.loadedIndex.delete(id)
this.scheduleIndexSave()
return true
}
} catch {}
return false
}
async destroyAll(): Promise<void> {