This commit is contained in:
2026-10-24 16:34:02 +08:00
parent 99e5687ce0
commit 7119e04a65
24 changed files with 553 additions and 39 deletions
@@ -188,7 +188,7 @@ function profileAvatarImagePath(name: string, file = 'avatar.bin'): string {
return join(profileMetadataDir(name), file)
}
function readProfileAvatar(name: string): ProfileAvatarResponse | null {
function readProfileAvatar(name: string, includeImage = true): ProfileAvatarResponse | null {
const metaPath = profileAvatarMetaPath(name)
if (!existsSync(metaPath)) return null
try {
@@ -203,6 +203,7 @@ function readProfileAvatar(name: string): ProfileAvatarResponse | null {
if (meta.type === 'image' && meta.file && meta.mime) {
const imagePath = profileAvatarImagePath(name, meta.file)
if (!existsSync(imagePath)) return null
if (!includeImage) return { type: 'image', updatedAt: meta.updatedAt }
const data = readFileSync(imagePath).toString('base64')
return {
type: 'image',
@@ -219,7 +220,7 @@ function readProfileAvatar(name: string): ProfileAvatarResponse | null {
function attachProfileAvatars<T extends HermesProfile>(profiles: T[]): Array<T & { avatar: ProfileAvatarResponse | null }> {
return profiles.map(profile => ({
...profile,
avatar: readProfileAvatar(profile.name),
avatar: readProfileAvatar(profile.name, false),
}))
}
@@ -376,18 +377,7 @@ function scheduleRuntimeStatusRefresh(): void {
export async function list(ctx: any) {
try {
let profiles: HermesProfile[]
try {
profiles = await hermesCli.listProfiles()
} catch (err: any) {
const { getActiveProfileName } = await import('../../services/hermes/hermes-profile')
const activeProfileName = getActiveProfileName()
if (!isForbiddenProfileName(activeProfileName)) throw err
logger.warn(err, '[listProfiles] active_profile "%s" is invalid/reserved; resetting to default and listing profiles from disk', activeProfileName)
writeFileSync(getActiveProfileFile(), 'default\n', 'utf-8')
profiles = listProfilesFromDisk('default')
}
let profiles = hermesCli.listProfilesSnapshot()
const activeProfileName = requestedProfileName(ctx)
@@ -485,6 +475,12 @@ export async function get(ctx: any) {
}
}
export async function getAvatar(ctx: any) {
const name = String(ctx.params.name || '').trim()
if (denyProfile(ctx, name)) return
ctx.body = { avatar: readProfileAvatar(name) }
}
export async function updateAvatar(ctx: any) {
const name = String(ctx.params.name || '').trim() || 'default'
if (denyProfile(ctx, name)) return
@@ -13,6 +13,7 @@ profileRoutes.post('/api/hermes/profiles/restart-all', ctrl.restartAllProfiles)
profileRoutes.get('/api/hermes/profiles/:name/runtime-status', ctrl.runtimeStatus)
profileRoutes.post('/api/hermes/profiles/:name/restart', ctrl.restartProfileRuntime)
profileRoutes.post('/api/hermes/profiles/:name/gateway/restart', ctrl.restartGatewayForProfile)
profileRoutes.get('/api/hermes/profiles/:name/avatar', ctrl.getAvatar)
profileRoutes.put('/api/hermes/profiles/:name/avatar', ctrl.updateAvatar)
profileRoutes.delete('/api/hermes/profiles/:name/avatar', ctrl.deleteAvatar)
profileRoutes.get('/api/hermes/profiles/:name', ctrl.get)
@@ -591,6 +591,16 @@ function readProfileDefaultModel(name: string): string {
/**
* List all profiles
*/
export function listProfilesSnapshot(): HermesProfile[] {
const activeProfileName = getActiveProfileName()
return listProfileNamesFromDisk().map(name => ({
name,
active: name === activeProfileName,
model: readProfileDefaultModel(name),
alias: '',
}))
}
export async function listProfiles(): Promise<HermesProfile[]> {
const profileNames = listProfileNamesFromDisk()
const activeProfileName = getActiveProfileName()