Improve profile runtime controls (#868)

* Improve profile runtime controls

* Restore profile selector test id

* Update profile switch e2e flow
This commit is contained in:
ekko
2026-05-20 12:59:34 +08:00
committed by GitHub
parent 479fef8a84
commit 663afb61ff
15 changed files with 864 additions and 40 deletions
@@ -4,6 +4,7 @@ export interface HermesProfile {
name: string
active: boolean
model: string
gatewayStatus?: string
alias: string
}
@@ -17,6 +18,31 @@ export interface HermesProfileDetail {
hasSoulMd: boolean
}
export interface ProfileRuntimeStatus {
profile: string
bridge: {
running: boolean
profile: string
mode?: string
reachable?: boolean
error?: string
}
gateway: {
profile: string
running: boolean
pid?: number
port?: number
host?: string
url?: string
error?: string
diagnostics?: {
health_url?: string
reason?: string
health_ok?: boolean
}
}
}
export async function fetchProfiles(): Promise<HermesProfile[]> {
const res = await request<{ profiles: HermesProfile[] }>('/api/hermes/profiles')
return res.profiles
@@ -27,6 +53,31 @@ export async function fetchProfileDetail(name: string): Promise<HermesProfileDet
return res.profile
}
export async function fetchProfileRuntimeStatus(name: string): Promise<ProfileRuntimeStatus> {
return request<ProfileRuntimeStatus>(`/api/hermes/profiles/${encodeURIComponent(name)}/runtime-status`)
}
export async function fetchProfileRuntimeStatuses(): Promise<ProfileRuntimeStatus[]> {
const res = await request<{ profiles: ProfileRuntimeStatus[] }>('/api/hermes/profiles/runtime-statuses')
return res.profiles
}
export async function restartProfileGateway(name: string): Promise<ProfileRuntimeStatus['gateway']> {
const res = await request<{ success: boolean; gateway: ProfileRuntimeStatus['gateway'] }>(
`/api/hermes/profiles/${encodeURIComponent(name)}/gateway/restart`,
{ method: 'POST' },
)
return res.gateway
}
export async function restartProfileRuntime(name: string): Promise<ProfileRuntimeStatus> {
const res = await request<{ success: boolean; status: ProfileRuntimeStatus }>(
`/api/hermes/profiles/${encodeURIComponent(name)}/restart`,
{ method: 'POST' },
)
return res.status
}
export interface CreateProfileResult {
success: boolean
/** clone=true 时被清理的独占平台凭据 KEY 名 */
@@ -1,32 +1,104 @@
<script setup lang="ts">
import { computed, onMounted } from 'vue'
import { NSelect, useMessage } from 'naive-ui'
import { computed, onMounted, ref } from 'vue'
import { NButton, NModal, NSpin, useMessage } from 'naive-ui'
import multiavatar from '@multiavatar/multiavatar'
import { useProfilesStore } from '@/stores/hermes/profiles'
import {
fetchProfileRuntimeStatuses,
restartProfileGateway,
restartProfileRuntime,
type ProfileRuntimeStatus,
} from '@/api/hermes/profiles'
import { useI18n } from 'vue-i18n'
const { t } = useI18n()
const message = useMessage()
const profilesStore = useProfilesStore()
const options = computed(() =>
profilesStore.profiles.map(p => ({
label: p.name,
value: p.name,
})),
)
const activeName = computed(() => profilesStore.activeProfileName ?? '')
const displayName = computed(() => activeName.value || 'default')
const avatarSvg = computed(() => multiavatar(displayName.value))
const runtimeStatuses = ref<ProfileRuntimeStatus[]>([])
const runtimeLoading = ref(false)
const showProfileModal = ref(false)
const gatewayRestarting = ref<Record<string, boolean>>({})
const profileRestarting = ref<Record<string, boolean>>({})
const profileSwitching = ref<Record<string, boolean>>({})
const statusByProfile = computed(() => new Map(runtimeStatuses.value.map(status => [status.profile, status])))
async function handleChange(value: string | number | Array<string | number>) {
if (typeof value === 'string' && value !== activeName.value) {
const ok = await profilesStore.switchProfile(value)
if (ok) {
message.success(t('profiles.switchSuccess', { name: value }))
// Reload to refresh all profile-dependent data
window.location.reload()
} else {
message.error(t('profiles.switchFailed'))
function avatarFor(name: string) {
return multiavatar(name || 'default')
}
async function loadRuntimeStatuses() {
runtimeLoading.value = true
try {
runtimeStatuses.value = await fetchProfileRuntimeStatuses()
} catch {
runtimeStatuses.value = []
} finally {
runtimeLoading.value = false
}
}
function openProfileModal() {
showProfileModal.value = true
void loadRuntimeStatuses()
}
function gatewayStatusText(running?: boolean) {
return running ? t('profiles.runtime.running') : t('profiles.runtime.stopped')
}
function bridgeStatusText(running?: boolean) {
return running ? t('profiles.runtime.active') : t('profiles.runtime.idle')
}
async function handleRestartGateway(name: string) {
gatewayRestarting.value = { ...gatewayRestarting.value, [name]: true }
try {
const gateway = await restartProfileGateway(name)
const current = statusByProfile.value.get(name)
if (current) {
runtimeStatuses.value = runtimeStatuses.value.map(status => (
status.profile === name ? { ...status, gateway } : status
))
}
message.success(t('profiles.runtime.gatewayRestarted', { name }))
} catch (err: any) {
message.error(err?.message || t('profiles.runtime.gatewayRestartFailed'))
} finally {
gatewayRestarting.value = { ...gatewayRestarting.value, [name]: false }
}
}
async function handleRestartProfile(name: string) {
profileRestarting.value = { ...profileRestarting.value, [name]: true }
try {
const status = await restartProfileRuntime(name)
runtimeStatuses.value = runtimeStatuses.value.map(item => (
item.profile === name ? status : item
))
message.success(t('profiles.runtime.profileRestarted', { name }))
} catch (err: any) {
message.error(err?.message || t('profiles.runtime.profileRestartFailed'))
} finally {
profileRestarting.value = { ...profileRestarting.value, [name]: false }
}
}
async function handleSwitchProfile(name: string) {
if (name === displayName.value) return
profileSwitching.value = { ...profileSwitching.value, [name]: true }
try {
const ok = await profilesStore.switchProfile(name)
if (!ok) throw new Error(t('profiles.switchFailed'))
message.success(t('profiles.switchSuccess', { name }))
window.location.reload()
} catch (err: any) {
message.error(err?.message || t('profiles.switchFailed'))
} finally {
profileSwitching.value = { ...profileSwitching.value, [name]: false }
}
}
@@ -40,14 +112,97 @@ onMounted(() => {
<template>
<div class="profile-selector">
<div class="selector-label">{{ t('sidebar.profiles') }}</div>
<NSelect
data-testid="profile-selector-select"
:value="activeName"
:options="options"
:loading="profilesStore.switching"
size="small"
@update:value="handleChange"
/>
<div class="profile-display" data-testid="profile-selector-select" @click="openProfileModal">
<span class="profile-avatar" v-html="avatarSvg" />
<span class="profile-name">{{ displayName }}</span>
</div>
<NModal
v-model:show="showProfileModal"
preset="card"
:bordered="false"
:style="{ width: '720px', maxWidth: 'calc(100vw - 32px)' }"
class="profile-manager-modal"
>
<template #header>
<div class="profile-modal-header">
<div class="profile-popover-title">
<span class="profile-popover-name">{{ t('sidebar.profiles') }}</span>
<span class="profile-popover-subtitle">{{ t('profiles.runtime.activeProfile', { name: displayName }) }}</span>
</div>
</div>
</template>
<NSpin :show="runtimeLoading" size="small">
<div class="profile-runtime-list">
<div
v-for="profile in profilesStore.profiles"
:key="profile.name"
class="profile-runtime-item"
:class="{ active: profile.name === displayName }"
>
<div class="profile-runtime-main">
<span class="profile-runtime-avatar" v-html="avatarFor(profile.name)" />
<div class="profile-runtime-info">
<div class="profile-runtime-name-row">
<span class="profile-runtime-name">{{ profile.name }}</span>
<span v-if="profile.name === displayName" class="active-badge">{{ t('profiles.runtime.activeTag') }}</span>
</div>
<div class="runtime-status-grid">
<div class="runtime-row compact">
<span class="runtime-label">{{ t('profiles.runtime.bridgeWorker') }}</span>
<span class="runtime-value" :class="{ running: statusByProfile.get(profile.name)?.bridge.running }">
<span class="runtime-dot" />
{{ bridgeStatusText(statusByProfile.get(profile.name)?.bridge.running) }}
</span>
</div>
<div class="runtime-row compact">
<span class="runtime-label">{{ t('profiles.runtime.gateway') }}</span>
<span class="runtime-value" :class="{ running: statusByProfile.get(profile.name)?.gateway.running }">
<span class="runtime-dot" />
{{ gatewayStatusText(statusByProfile.get(profile.name)?.gateway.running) }}
</span>
</div>
</div>
<div
v-if="!statusByProfile.get(profile.name)?.gateway.running && (statusByProfile.get(profile.name)?.gateway.diagnostics?.reason || statusByProfile.get(profile.name)?.gateway.error)"
class="runtime-detail"
>
{{ statusByProfile.get(profile.name)?.gateway.diagnostics?.reason || statusByProfile.get(profile.name)?.gateway.error }}
</div>
</div>
</div>
<div class="profile-runtime-actions">
<NButton
size="small"
type="primary"
:loading="gatewayRestarting[profile.name]"
@click="handleRestartGateway(profile.name)"
>
{{ t('profiles.runtime.restartGateway') }}
</NButton>
<NButton
size="small"
type="primary"
:loading="profileRestarting[profile.name]"
@click="handleRestartProfile(profile.name)"
>
{{ t('profiles.runtime.restartProfile') }}
</NButton>
<NButton
size="small"
type="primary"
:disabled="profile.name === displayName"
:loading="profileSwitching[profile.name]"
@click="handleSwitchProfile(profile.name)"
>
{{ t('profiles.runtime.switchProfile') }}
</NButton>
</div>
</div>
</div>
</NSpin>
</NModal>
</div>
</template>
@@ -67,4 +222,232 @@ onMounted(() => {
letter-spacing: 0.5px;
margin-bottom: 6px;
}
.profile-display {
display: flex;
align-items: center;
gap: 8px;
min-width: 0;
height: 34px;
padding: 4px 6px;
border-radius: 8px;
background: $bg-secondary;
border: 1px solid $border-color;
cursor: pointer;
}
.profile-avatar {
width: 24px;
height: 24px;
border-radius: 50%;
overflow: hidden;
flex: 0 0 auto;
background: $bg-card;
:deep(svg) {
width: 100%;
height: 100%;
display: block;
}
}
.profile-name {
min-width: 0;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
font-size: 13px;
font-weight: 600;
color: $text-primary;
}
.profile-popover {
display: flex;
flex-direction: column;
gap: 12px;
}
.profile-popover-header {
display: flex;
align-items: center;
gap: 10px;
min-width: 0;
}
.profile-popover-avatar {
width: 36px;
height: 36px;
border-radius: 50%;
overflow: hidden;
background: $bg-secondary;
flex: 0 0 auto;
:deep(svg) {
width: 100%;
height: 100%;
display: block;
}
}
.profile-popover-title {
min-width: 0;
display: flex;
flex-direction: column;
gap: 2px;
}
.profile-popover-name {
min-width: 0;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
font-size: 14px;
font-weight: 700;
color: $text-primary;
}
.profile-popover-subtitle,
.runtime-label,
.runtime-detail {
font-size: 12px;
color: $text-muted;
}
.runtime-list {
display: flex;
flex-direction: column;
gap: 8px;
min-height: 62px;
}
.profile-runtime-list {
display: flex;
flex-direction: column;
gap: 8px;
max-height: 420px;
min-height: 96px;
overflow-y: auto;
}
.profile-runtime-item {
display: flex;
flex-direction: column;
gap: 8px;
padding: 10px;
border: 1px solid $border-color;
border-radius: 8px;
background: $bg-card;
&.active {
border-color: $accent-muted;
background: $bg-card-hover;
}
}
.profile-runtime-main {
display: flex;
gap: 10px;
min-width: 0;
}
.profile-runtime-avatar {
width: 34px;
height: 34px;
border-radius: 50%;
overflow: hidden;
flex: 0 0 auto;
background: $bg-secondary;
:deep(svg) {
width: 100%;
height: 100%;
display: block;
}
}
.profile-runtime-info {
min-width: 0;
flex: 1;
display: flex;
flex-direction: column;
gap: 5px;
}
.profile-runtime-name-row {
display: flex;
align-items: center;
gap: 6px;
min-width: 0;
}
.profile-runtime-name {
min-width: 0;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
font-size: 13px;
font-weight: 700;
color: $text-primary;
}
.active-badge {
flex: 0 0 auto;
padding: 1px 5px;
border-radius: 999px;
background: color-mix(in srgb, $success 16%, transparent);
color: $success;
font-size: 10px;
font-weight: 700;
}
.profile-runtime-actions {
display: grid;
grid-template-columns: repeat(3, minmax(88px, max-content));
justify-content: end;
gap: 6px;
:deep(.n-button) {
min-width: 88px;
}
}
.runtime-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
&.compact {
gap: 8px;
}
}
.runtime-value {
display: inline-flex;
align-items: center;
gap: 6px;
color: $text-secondary;
font-size: 12px;
font-weight: 600;
&.running {
color: $success;
.runtime-dot {
background: $success;
}
}
}
.runtime-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: $text-muted;
}
.runtime-detail {
line-height: 1.4;
word-break: break-word;
}
</style>
+17
View File
@@ -695,6 +695,23 @@ export default {
hasEnv: 'Has .env',
hasSoulMd: 'Has soul.md',
noProfiles: 'No profiles found. Create one to get started.',
runtime: {
activeProfile: 'Active: {name}',
bridgeWorker: 'Bridge worker',
gateway: 'Gateway',
active: 'Active',
activeTag: 'Active',
idle: 'Idle',
running: 'Running',
stopped: 'Stopped',
restartGateway: 'Restart Gateway',
restartProfile: 'Restart Profile',
switchProfile: 'Switch Profile',
gatewayRestarted: 'Gateway restarted: {name}',
gatewayRestartFailed: 'Failed to restart gateway',
profileRestarted: 'Profile restarted: {name}',
profileRestartFailed: 'Failed to restart profile',
},
},
// Logs
+17
View File
@@ -687,6 +687,23 @@ export default {
hasEnv: '有 .env',
hasSoulMd: '有 soul.md',
noProfiles: '暂无配置,创建一个开始吧。',
runtime: {
activeProfile: '当前:{name}',
bridgeWorker: '桥接状态',
gateway: '网关',
active: '活跃',
activeTag: '当前',
idle: '空闲',
running: '运行中',
stopped: '已停止',
restartGateway: '重启网关',
restartProfile: '重启配置',
switchProfile: '切换配置',
gatewayRestarted: '网关已重启:{name}',
gatewayRestartFailed: '重启网关失败',
profileRestarted: '配置已重启:{name}',
profileRestartFailed: '重启配置失败',
},
},
// 日志