Blank session cleanup
This commit is contained in:
@@ -8,9 +8,11 @@ import {
|
|||||||
activeSessionId,
|
activeSessionId,
|
||||||
agents,
|
agents,
|
||||||
clearSessionDraftPrompt,
|
clearSessionDraftPrompt,
|
||||||
|
getChildSessions,
|
||||||
|
isBlankSession,
|
||||||
messagesLoaded,
|
messagesLoaded,
|
||||||
providers,
|
|
||||||
pruneDraftPrompts,
|
pruneDraftPrompts,
|
||||||
|
providers,
|
||||||
setActiveSessionId,
|
setActiveSessionId,
|
||||||
setAgents,
|
setAgents,
|
||||||
setMessagesLoaded,
|
setMessagesLoaded,
|
||||||
@@ -228,6 +230,10 @@ async function createSession(instanceId: string, agent?: string): Promise<Sessio
|
|||||||
return next
|
return next
|
||||||
})
|
})
|
||||||
|
|
||||||
|
getSessionIndex(instanceId, session.id)
|
||||||
|
|
||||||
|
await cleanupBlankSessions(instanceId, session.id)
|
||||||
|
|
||||||
return session
|
return session
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Failed to create session:", error)
|
console.error("Failed to create session:", error)
|
||||||
@@ -401,6 +407,27 @@ async function deleteSession(instanceId: string, sessionId: string): Promise<voi
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function cleanupBlankSessions(instanceId: string, excludeSessionId?: string): Promise<void> {
|
||||||
|
const instanceSessions = sessions().get(instanceId)
|
||||||
|
if (!instanceSessions) return
|
||||||
|
|
||||||
|
const deletionPromises: Promise<void>[] = []
|
||||||
|
|
||||||
|
for (const [sessionId, session] of instanceSessions) {
|
||||||
|
if (sessionId === excludeSessionId) continue
|
||||||
|
if (!isBlankSession(session, instanceId)) continue
|
||||||
|
|
||||||
|
deletionPromises.push(deleteSession(instanceId, sessionId).catch((error) => {
|
||||||
|
console.error(`Failed to delete blank session ${sessionId}:`, error)
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
if (deletionPromises.length > 0) {
|
||||||
|
console.log(`Cleaning up ${deletionPromises.length} blank sessions`)
|
||||||
|
await Promise.all(deletionPromises)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function fetchAgents(instanceId: string): Promise<void> {
|
async function fetchAgents(instanceId: string): Promise<void> {
|
||||||
const instance = instances().get(instanceId)
|
const instance = instances().get(instanceId)
|
||||||
if (!instance || !instance.client) {
|
if (!instance || !instance.client) {
|
||||||
|
|||||||
@@ -221,6 +221,49 @@ function getSessionInfo(instanceId: string, sessionId: string): SessionInfo | un
|
|||||||
return sessionInfoByInstance().get(instanceId)?.get(sessionId)
|
return sessionInfoByInstance().get(instanceId)?.get(sessionId)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isBlankSession(session: Session, instanceId: string): boolean {
|
||||||
|
if (session.parentId === null) {
|
||||||
|
// Parent session is only blank if actually blank AND has no children
|
||||||
|
|
||||||
|
const sessionInfo = getSessionInfo(instanceId, session.id)
|
||||||
|
const hasChildren = getChildSessions(instanceId, session.id).length > 0
|
||||||
|
|
||||||
|
// Only consider blank if we have loaded info, it shows 0 tokens, and no children
|
||||||
|
return sessionInfo !== undefined && sessionInfo.tokens === 0 && !hasChildren
|
||||||
|
} else if (session.title?.includes("subagent")) {
|
||||||
|
// Subagent
|
||||||
|
|
||||||
|
const loadedSet = messagesLoaded().get(instanceId) || new Set()
|
||||||
|
if (!loadedSet.has(session.id)) return false
|
||||||
|
|
||||||
|
if (session.messages.length === 0) return true
|
||||||
|
|
||||||
|
const hasStreamingMessage = session.messages.some((msg) => msg.status === "streaming")
|
||||||
|
const isWaitingForPermission = session.pendingPermission === true
|
||||||
|
|
||||||
|
// If streaming or waiting for permission, not blank
|
||||||
|
if (hasStreamingMessage || isWaitingForPermission) return false
|
||||||
|
|
||||||
|
// Check if last message was a tool call
|
||||||
|
const lastMessage = session.messages[session.messages.length - 1]
|
||||||
|
const lastMessageWasToolCall = lastMessage.parts.some((part) => part.type === "tool")
|
||||||
|
|
||||||
|
// Subagent is blank if last message was NOT a tool call
|
||||||
|
return !lastMessageWasToolCall
|
||||||
|
} else if (session.revert?.messageID) {
|
||||||
|
// Fork
|
||||||
|
const loadedSet = messagesLoaded().get(instanceId) || new Set()
|
||||||
|
if (!loadedSet.has(session.id)) return false
|
||||||
|
|
||||||
|
if (session.messages.length === 0) return true
|
||||||
|
|
||||||
|
const lastMessage = session.messages[session.messages.length - 1]
|
||||||
|
return lastMessage.id === session.revert.messageID
|
||||||
|
}
|
||||||
|
|
||||||
|
return false // default to not saying it's blank, just to be safe
|
||||||
|
}
|
||||||
|
|
||||||
export {
|
export {
|
||||||
sessions,
|
sessions,
|
||||||
setSessions,
|
setSessions,
|
||||||
@@ -259,4 +302,5 @@ export {
|
|||||||
isSessionBusy,
|
isSessionBusy,
|
||||||
isSessionMessagesLoading,
|
isSessionMessagesLoading,
|
||||||
getSessionInfo,
|
getSessionInfo,
|
||||||
|
isBlankSession,
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user