mirror of
https://github.com/wu736139669/hapi.git
synced 2026-11-28 05:57:13 +00:00
refactor(cli): unify session bootstrap
This commit is contained in:
@@ -0,0 +1,143 @@
|
||||
import os from 'node:os'
|
||||
import { randomUUID } from 'node:crypto'
|
||||
import { resolve } from 'node:path'
|
||||
|
||||
import { ApiClient } from '@/api/api'
|
||||
import type { ApiSessionClient } from '@/api/apiSession'
|
||||
import type { AgentState, MachineMetadata, Metadata, Session } from '@/api/types'
|
||||
import { notifyDaemonSessionStarted } from '@/daemon/controlClient'
|
||||
import { readSettings } from '@/persistence'
|
||||
import { configuration } from '@/configuration'
|
||||
import { logger } from '@/ui/logger'
|
||||
import { runtimePath } from '@/projectPath'
|
||||
import { readWorktreeEnv } from '@/utils/worktreeEnv'
|
||||
import packageJson from '../../package.json'
|
||||
|
||||
export type SessionStartedBy = 'daemon' | 'terminal'
|
||||
|
||||
export type SessionBootstrapOptions = {
|
||||
flavor: string
|
||||
startedBy?: SessionStartedBy
|
||||
workingDirectory?: string
|
||||
tag?: string
|
||||
agentState?: AgentState | null
|
||||
}
|
||||
|
||||
export type SessionBootstrapResult = {
|
||||
api: ApiClient
|
||||
session: ApiSessionClient
|
||||
sessionInfo: Session
|
||||
metadata: Metadata
|
||||
machineId: string
|
||||
startedBy: SessionStartedBy
|
||||
workingDirectory: string
|
||||
}
|
||||
|
||||
export function buildMachineMetadata(): MachineMetadata {
|
||||
return {
|
||||
host: os.hostname(),
|
||||
platform: os.platform(),
|
||||
happyCliVersion: packageJson.version,
|
||||
homeDir: os.homedir(),
|
||||
happyHomeDir: configuration.happyHomeDir,
|
||||
happyLibDir: runtimePath()
|
||||
}
|
||||
}
|
||||
|
||||
export function buildSessionMetadata(options: {
|
||||
flavor: string
|
||||
startedBy: SessionStartedBy
|
||||
workingDirectory: string
|
||||
machineId: string
|
||||
now?: number
|
||||
}): Metadata {
|
||||
const happyLibDir = runtimePath()
|
||||
const worktreeInfo = readWorktreeEnv()
|
||||
const now = options.now ?? Date.now()
|
||||
|
||||
return {
|
||||
path: options.workingDirectory,
|
||||
host: os.hostname(),
|
||||
version: packageJson.version,
|
||||
os: os.platform(),
|
||||
machineId: options.machineId,
|
||||
homeDir: os.homedir(),
|
||||
happyHomeDir: configuration.happyHomeDir,
|
||||
happyLibDir,
|
||||
happyToolsDir: resolve(happyLibDir, 'tools', 'unpacked'),
|
||||
startedFromDaemon: options.startedBy === 'daemon',
|
||||
hostPid: process.pid,
|
||||
startedBy: options.startedBy,
|
||||
lifecycleState: 'running',
|
||||
lifecycleStateSince: now,
|
||||
flavor: options.flavor,
|
||||
worktree: worktreeInfo ?? undefined
|
||||
}
|
||||
}
|
||||
|
||||
async function getMachineIdOrExit(): Promise<string> {
|
||||
const settings = await readSettings()
|
||||
const machineId = settings?.machineId
|
||||
if (!machineId) {
|
||||
console.error(`[START] No machine ID found in settings, which is unexpected since authAndSetupMachineIfNeeded should have created it. Please report this issue on ${packageJson.bugs}`)
|
||||
process.exit(1)
|
||||
}
|
||||
logger.debug(`Using machineId: ${machineId}`)
|
||||
return machineId
|
||||
}
|
||||
|
||||
async function reportSessionStarted(sessionId: string, metadata: Metadata): Promise<void> {
|
||||
try {
|
||||
logger.debug(`[START] Reporting session ${sessionId} to daemon`)
|
||||
const result = await notifyDaemonSessionStarted(sessionId, metadata)
|
||||
if (result?.error) {
|
||||
logger.debug(`[START] Failed to report to daemon (may not be running):`, result.error)
|
||||
} else {
|
||||
logger.debug(`[START] Reported session ${sessionId} to daemon`)
|
||||
}
|
||||
} catch (error) {
|
||||
logger.debug('[START] Failed to report to daemon (may not be running):', error)
|
||||
}
|
||||
}
|
||||
|
||||
export async function bootstrapSession(options: SessionBootstrapOptions): Promise<SessionBootstrapResult> {
|
||||
const workingDirectory = options.workingDirectory ?? process.cwd()
|
||||
const startedBy = options.startedBy ?? 'terminal'
|
||||
const sessionTag = options.tag ?? randomUUID()
|
||||
const agentState = options.agentState === undefined ? {} : options.agentState
|
||||
|
||||
const api = await ApiClient.create()
|
||||
|
||||
const machineId = await getMachineIdOrExit()
|
||||
await api.getOrCreateMachine({
|
||||
machineId,
|
||||
metadata: buildMachineMetadata()
|
||||
})
|
||||
|
||||
const metadata = buildSessionMetadata({
|
||||
flavor: options.flavor,
|
||||
startedBy,
|
||||
workingDirectory,
|
||||
machineId
|
||||
})
|
||||
|
||||
const sessionInfo = await api.getOrCreateSession({
|
||||
tag: sessionTag,
|
||||
metadata,
|
||||
state: agentState
|
||||
})
|
||||
|
||||
const session = api.sessionSyncClient(sessionInfo)
|
||||
|
||||
await reportSessionStarted(sessionInfo.id, metadata)
|
||||
|
||||
return {
|
||||
api,
|
||||
session,
|
||||
sessionInfo,
|
||||
metadata,
|
||||
machineId,
|
||||
startedBy,
|
||||
workingDirectory
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user