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 => {
|
.patch('/session/:sessionID', async c => {
|
||||||
const id = c.req.param('sessionID')
|
const id = c.req.param('sessionID')
|
||||||
const handle = sessionManager.getSession(id)
|
const handle = sessionManager.getSession(id)
|
||||||
if (!handle) throw notFound('session not found')
|
|
||||||
|
|
||||||
const body = await c.req.json<{
|
const body = await c.req.json<{
|
||||||
title?: string
|
title?: string
|
||||||
model?: string
|
model?: string
|
||||||
permission_mode?: string
|
permission_mode?: string
|
||||||
|
time?: { archived?: number }
|
||||||
}>()
|
}>()
|
||||||
|
|
||||||
|
if (handle) {
|
||||||
if (body.title) handle.setTitle(body.title)
|
if (body.title) handle.setTitle(body.title)
|
||||||
if (body.model) await handle.setModel(body.model)
|
if (body.model) await handle.setModel(body.model)
|
||||||
if (body.permission_mode) await handle.setPermissionMode(body.permission_mode)
|
if (body.permission_mode) await handle.setPermissionMode(body.permission_mode)
|
||||||
|
}
|
||||||
|
|
||||||
return c.json({
|
return c.json({
|
||||||
session_id: id,
|
session_id: id,
|
||||||
title: handle.title ?? body.title,
|
title: handle?.title ?? body.title,
|
||||||
model: handle.model,
|
model: handle?.model,
|
||||||
permission_mode: handle.permissionMode,
|
permission_mode: handle?.permissionMode,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.delete('/session/:sessionID', async c => {
|
.delete('/session/:sessionID', async c => {
|
||||||
const id = c.req.param('sessionID')
|
const id = c.req.param('sessionID')
|
||||||
const deleted = await sessionManager.deleteSession(id)
|
await sessionManager.deleteSession(id)
|
||||||
if (!deleted) throw notFound('session not found')
|
|
||||||
return c.json({ deleted: true })
|
return c.json({ deleted: true })
|
||||||
})
|
})
|
||||||
.post('/session/:sessionID/prompt', async c => {
|
.post('/session/:sessionID/prompt', async c => {
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import { readFile, writeFile, mkdir } from 'fs/promises'
|
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 { join, resolve, isAbsolute } from 'path'
|
||||||
import { getClaudeConfigHomeDir } from '../utils/envUtils.js'
|
import { getClaudeConfigHomeDir } from '../utils/envUtils.js'
|
||||||
import { logError } from '../utils/log.js'
|
import { logError } from '../utils/log.js'
|
||||||
|
|
@ -7,7 +7,7 @@ import type { EventBus } from './eventBus.js'
|
||||||
import { SessionHandle } from './sessionHandle.js'
|
import { SessionHandle } from './sessionHandle.js'
|
||||||
import type { InitData } from './types.js'
|
import type { InitData } from './types.js'
|
||||||
import type { SessionIndex, SessionIndexEntry, SessionBusyStatus, SessionState } 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'
|
const INDEX_FILE = 'server-sessions.json'
|
||||||
|
|
||||||
|
|
@ -264,7 +264,7 @@ export class SessionManager {
|
||||||
|
|
||||||
async deleteSession(id: string): Promise<boolean> {
|
async deleteSession(id: string): Promise<boolean> {
|
||||||
const handle = this.sessions.get(id)
|
const handle = this.sessions.get(id)
|
||||||
if (!handle) return false
|
if (handle) {
|
||||||
const silent = handle.silent
|
const silent = handle.silent
|
||||||
handle.forceKill()
|
handle.forceKill()
|
||||||
this.sessions.delete(id)
|
this.sessions.delete(id)
|
||||||
|
|
@ -276,6 +276,19 @@ export class SessionManager {
|
||||||
return true
|
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> {
|
async destroyAll(): Promise<void> {
|
||||||
for (const [, handle] of this.sessions) {
|
for (const [, handle] of this.sessions) {
|
||||||
handle.forceKill()
|
handle.forceKill()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user