fix: DELETE 历史 session 时删除磁盘 transcript 文件
- PATCH 接口:历史 session 不在内存时不再 404,直接返回成功 - DELETE 接口:历史 session 通过 resolveSessionFilePath 定位并删除磁盘 .jsonl 文件 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
parent
3aab6fb889
commit
0a93873e3d
|
|
@ -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 => {
|
||||
|
|
|
|||
|
|
@ -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> {
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user