mirror of
https://github.com/wu736139669/hapi.git
synced 2026-11-28 05:57:13 +00:00
feat: add hapi resume command (#647)
This commit is contained in:
@@ -86,6 +86,25 @@ export function buildSessionMetadata(options: {
|
||||
}
|
||||
}
|
||||
|
||||
function pickExistingSessionMetadata(metadata: Metadata | null | undefined): Partial<Metadata> {
|
||||
if (!metadata) return {}
|
||||
|
||||
const preserved: Partial<Metadata> = {}
|
||||
|
||||
if (metadata.name !== undefined) preserved.name = metadata.name
|
||||
if (metadata.summary !== undefined) preserved.summary = metadata.summary
|
||||
if (metadata.claudeSessionId !== undefined) preserved.claudeSessionId = metadata.claudeSessionId
|
||||
if (metadata.codexSessionId !== undefined) preserved.codexSessionId = metadata.codexSessionId
|
||||
if (metadata.geminiSessionId !== undefined) preserved.geminiSessionId = metadata.geminiSessionId
|
||||
if (metadata.opencodeSessionId !== undefined) preserved.opencodeSessionId = metadata.opencodeSessionId
|
||||
if (metadata.cursorSessionId !== undefined) preserved.cursorSessionId = metadata.cursorSessionId
|
||||
if (metadata.tools !== undefined) preserved.tools = metadata.tools
|
||||
if (metadata.slashCommands !== undefined) preserved.slashCommands = metadata.slashCommands
|
||||
if (metadata.worktree !== undefined) preserved.worktree = metadata.worktree
|
||||
|
||||
return preserved
|
||||
}
|
||||
|
||||
async function getMachineIdOrExit(): Promise<string> {
|
||||
const settings = await readSettings()
|
||||
const machineId = settings?.machineId
|
||||
@@ -156,3 +175,53 @@ export async function bootstrapSession(options: SessionBootstrapOptions): Promis
|
||||
workingDirectory
|
||||
}
|
||||
}
|
||||
|
||||
export async function bootstrapExistingSession(options: {
|
||||
sessionId: string
|
||||
flavor: string
|
||||
startedBy?: SessionStartedBy
|
||||
workingDirectory: string
|
||||
metadataOverrides?: Partial<Metadata>
|
||||
}): Promise<SessionBootstrapResult> {
|
||||
const startedBy = options.startedBy ?? 'terminal'
|
||||
const api = await ApiClient.create()
|
||||
const machineId = await getMachineIdOrExit()
|
||||
|
||||
await api.getOrCreateMachine({
|
||||
machineId,
|
||||
metadata: buildMachineMetadata()
|
||||
})
|
||||
|
||||
const sessionInfo = await api.getSession(options.sessionId)
|
||||
const baseMetadata = buildSessionMetadata({
|
||||
flavor: options.flavor,
|
||||
startedBy,
|
||||
workingDirectory: options.workingDirectory,
|
||||
machineId
|
||||
})
|
||||
const metadata = {
|
||||
...baseMetadata,
|
||||
...pickExistingSessionMetadata(sessionInfo.metadata),
|
||||
...options.metadataOverrides
|
||||
}
|
||||
|
||||
const buildUpdatedMetadata = (current: Metadata): Metadata => ({
|
||||
...baseMetadata,
|
||||
...pickExistingSessionMetadata(current),
|
||||
...options.metadataOverrides
|
||||
})
|
||||
|
||||
const session = api.sessionSyncClient(sessionInfo)
|
||||
session.updateMetadata(buildUpdatedMetadata)
|
||||
await reportSessionStarted(sessionInfo.id, metadata)
|
||||
|
||||
return {
|
||||
api,
|
||||
session,
|
||||
sessionInfo,
|
||||
metadata,
|
||||
machineId,
|
||||
startedBy,
|
||||
workingDirectory: options.workingDirectory
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user