2026-04-26 04:55:08 +02:00
|
|
|
import { existsSync, readFileSync } from 'fs'
|
|
|
|
|
import { resolve } from 'path'
|
2026-04-21 12:35:48 +08:00
|
|
|
import * as hermesCli from '../services/hermes/hermes-cli'
|
|
|
|
|
|
|
|
|
|
declare const __APP_VERSION__: string
|
2026-04-26 04:55:08 +02:00
|
|
|
|
|
|
|
|
type PackageInfo = {
|
|
|
|
|
name: string
|
|
|
|
|
version: string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function readPackageInfo(): PackageInfo | null {
|
|
|
|
|
const candidatePaths = [
|
|
|
|
|
// ts-node dev: packages/server/src/controllers -> repo root
|
|
|
|
|
resolve(__dirname, '../../../../package.json'),
|
|
|
|
|
// bundled server: dist/server -> repo root/package root
|
|
|
|
|
resolve(__dirname, '../../package.json'),
|
|
|
|
|
// fallback for dev/test processes started at the repo root
|
|
|
|
|
resolve(process.cwd(), 'package.json'),
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
for (const packagePath of candidatePaths) {
|
|
|
|
|
if (!existsSync(packagePath)) continue
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const pkg = JSON.parse(readFileSync(packagePath, 'utf-8'))
|
|
|
|
|
if (pkg?.name && pkg?.version) {
|
|
|
|
|
return {
|
|
|
|
|
name: String(pkg.name),
|
|
|
|
|
version: String(pkg.version),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
// Try the next candidate path.
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const PACKAGE_INFO = readPackageInfo()
|
2026-04-21 12:35:48 +08:00
|
|
|
const LOCAL_VERSION = typeof __APP_VERSION__ !== 'undefined'
|
|
|
|
|
? __APP_VERSION__
|
2026-04-26 04:55:08 +02:00
|
|
|
: PACKAGE_INFO?.version || ''
|
2026-04-21 12:35:48 +08:00
|
|
|
|
|
|
|
|
let cachedLatestVersion = ''
|
|
|
|
|
|
|
|
|
|
export async function checkLatestVersion(): Promise<void> {
|
|
|
|
|
try {
|
2026-04-26 04:55:08 +02:00
|
|
|
const packageName = PACKAGE_INFO?.name || 'hermes-web-ui'
|
|
|
|
|
const registryName = encodeURIComponent(packageName)
|
|
|
|
|
const res = await fetch(`https://registry.npmjs.org/${registryName}/latest`, { signal: AbortSignal.timeout(10000) })
|
2026-04-21 12:35:48 +08:00
|
|
|
if (res.ok) {
|
|
|
|
|
const data = await res.json() as { version: string }
|
|
|
|
|
cachedLatestVersion = data.version
|
2026-04-26 04:55:08 +02:00
|
|
|
if (LOCAL_VERSION && cachedLatestVersion !== LOCAL_VERSION) {
|
2026-04-21 12:35:48 +08:00
|
|
|
console.log(`Update available: ${LOCAL_VERSION} → ${cachedLatestVersion}`)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch { /* ignore */ }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function startVersionCheck(): void {
|
|
|
|
|
setTimeout(checkLatestVersion, 5000)
|
|
|
|
|
setInterval(checkLatestVersion, 30 * 60 * 1000)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function healthCheck(ctx: any) {
|
|
|
|
|
const raw = await hermesCli.getVersion()
|
|
|
|
|
const hermesVersion = raw.split('\n')[0].replace('Hermes Agent ', '') || ''
|
|
|
|
|
ctx.body = {
|
2026-05-19 16:09:59 +08:00
|
|
|
status: 'ok',
|
2026-04-21 12:35:48 +08:00
|
|
|
platform: 'hermes-agent',
|
|
|
|
|
version: hermesVersion,
|
2026-05-19 16:09:59 +08:00
|
|
|
gateway: 'running',
|
2026-04-21 12:35:48 +08:00
|
|
|
webui_version: LOCAL_VERSION,
|
|
|
|
|
webui_latest: cachedLatestVersion,
|
2026-04-26 04:55:08 +02:00
|
|
|
webui_update_available: Boolean(LOCAL_VERSION && cachedLatestVersion && cachedLatestVersion !== LOCAL_VERSION),
|
2026-04-23 12:57:42 +08:00
|
|
|
node_version: process.versions.node,
|
2026-04-21 12:35:48 +08:00
|
|
|
}
|
|
|
|
|
}
|