feat: implement service management API for VLM control

- Add /api/v1/mbe/health endpoint for status checks
- Add /api/v2/service/start to start llama-server
- Add /api/v2/service/stop to stop llama-server
- Use TCP connection test for port detection (more reliable than lsof)
- Add ps aux fallback for PID detection
- Implement structured bilingual VLM tags
- Add VLM integration documentation for Studio team

Endpoints tested and working with Momentry Studio AdminView UI.
This commit is contained in:
Accusys
2026-10-24 02:37:53 +08:00
parent d14cd9f3de
commit c151b49334
33 changed files with 3175 additions and 229 deletions
+104
View File
@@ -0,0 +1,104 @@
function monitorApp() {
return {
// Data
lastUpdate: 'Never',
queue: { pending: 0, running: 0, completed: 0, failed: 0 },
runningJobs: [],
worker: {
cpu_percent: 0,
gpu_percent: 0,
memory_mb: 0,
memory_percent: 0,
active_processors: 0,
max_processors: 6,
processors: []
},
// Initialize
init() {
this.refresh();
setInterval(() => this.refresh(), 5000);
},
// Refresh all data
async refresh() {
await Promise.all([
this.fetchQueueStats(),
this.fetchRunningJobs(),
this.fetchWorkerStatus()
]);
this.lastUpdate = new Date().toLocaleTimeString();
},
// Fetch queue stats
async fetchQueueStats() {
try {
const res = await fetch('/api/v1/monitor/queue_stats');
this.queue = await res.json();
} catch (e) {
console.error('Failed to fetch queue stats:', e);
}
},
// Fetch running jobs
async fetchRunningJobs() {
try {
const res = await fetch('/api/v1/monitor/running_jobs');
const data = await res.json();
this.runningJobs = data.jobs;
} catch (e) {
console.error('Failed to fetch running jobs:', e);
}
},
// Fetch worker status
async fetchWorkerStatus() {
try {
const res = await fetch('/api/v1/monitor/worker_status');
this.worker = await res.json();
} catch (e) {
console.error('Failed to fetch worker status:', e);
}
},
// Helper functions
getPhaseName(phase) {
const names = {
1: 'Foundation',
2: 'Core Processing',
3: 'Deep Analysis',
4: 'Knowledge Building'
};
return names[phase] || '';
},
getProcessorIcon(status) {
const icons = {
'completed': '✅',
'running': '🔄',
'pending': '⏸️',
'deferred': '⏸️',
'failed': '❌'
};
return icons[status] || '❓';
},
getProcessorColor(status) {
const colors = {
'completed': 'bg-green-500',
'running': 'bg-blue-500',
'pending': 'bg-gray-500',
'deferred': 'bg-yellow-500',
'failed': 'bg-red-500'
};
return colors[status] || 'bg-gray-500';
},
formatDuration(secs) {
if (!secs) return '0s';
const m = Math.floor(secs / 60);
const s = Math.floor(secs % 60);
return m > 0 ? `${m}m ${s}s` : `${s}s`;
}
}
}
+167
View File
@@ -0,0 +1,167 @@
<!DOCTYPE html>
<html lang="zh-TW">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Momentry Monitor</title>
<script src="https://cdn.tailwindcss.com"></script>
<script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script>
</head>
<body class="bg-gray-900 text-white min-h-screen">
<div x-data="monitorApp()" x-init="init()" class="container mx-auto px-4 py-8">
<!-- Header -->
<header class="mb-8 flex justify-between items-center">
<div>
<h1 class="text-3xl font-bold">Momentry Monitor</h1>
<p class="text-gray-400 text-sm mt-1">
Last updated: <span x-text="lastUpdate"></span>
</p>
</div>
<button @click="refresh()" class="bg-blue-600 hover:bg-blue-700 px-4 py-2 rounded">
Refresh
</button>
</header>
<!-- Queue Overview -->
<div class="bg-gray-800 rounded-lg p-6 mb-6">
<h2 class="text-xl font-semibold mb-4">Queue Overview</h2>
<div class="grid grid-cols-4 gap-4 text-center">
<div>
<div class="text-3xl font-bold text-yellow-400" x-text="queue.pending"></div>
<div class="text-sm text-gray-400">Pending</div>
</div>
<div>
<div class="text-3xl font-bold text-blue-400" x-text="queue.running"></div>
<div class="text-sm text-gray-400">Running</div>
</div>
<div>
<div class="text-3xl font-bold text-green-400" x-text="queue.completed"></div>
<div class="text-sm text-gray-400">Completed</div>
</div>
<div>
<div class="text-3xl font-bold text-red-400" x-text="queue.failed"></div>
<div class="text-sm text-gray-400">Failed</div>
</div>
</div>
<div class="mt-4 text-sm text-gray-400">
Currently processing: <span class="text-blue-400 font-semibold" x-text="queue.running"></span> jobs
</div>
</div>
<!-- Running Jobs -->
<div class="bg-gray-800 rounded-lg p-6 mb-6">
<h2 class="text-xl font-semibold mb-4">Running Jobs</h2>
<div class="space-y-4">
<template x-for="job in runningJobs" :key="job.job_id">
<div class="border border-gray-700 rounded-lg p-4">
<div class="flex justify-between items-start mb-3">
<div>
<div class="flex items-center gap-2">
<span class="text-lg font-semibold">Job #<span x-text="job.job_id"></span></span>
<span class="px-2 py-1 bg-blue-500/20 text-blue-400 rounded text-sm">RUNNING</span>
</div>
<div class="text-sm text-gray-400 mt-1">
<span x-text="job.file_name"></span> | UUID: <span x-text="job.file_uuid"></span>
</div>
</div>
<div class="text-right">
<div class="text-sm text-gray-400">Duration</div>
<div class="font-semibold" x-text="formatDuration(job.duration_secs)"></div>
</div>
</div>
<div class="space-y-3">
<template x-for="phase in [1,2,3,4]" :key="phase">
<div>
<div class="text-sm font-semibold mb-2"
x-text="'Phase ' + phase + ': ' + getPhaseName(phase)"></div>
<div class="grid grid-cols-4 gap-2">
<template x-for="proc in job.processors.filter(p => p.phase === phase)" :key="proc.name">
<div class="bg-gray-700 rounded p-2">
<div class="flex items-center justify-between mb-1">
<span class="text-sm" x-text="proc.name"></span>
<span x-text="getProcessorIcon(proc.status)"></span>
</div>
<div class="flex items-center gap-2">
<div class="flex-1 bg-gray-600 rounded-full h-1.5">
<div class="h-1.5 rounded-full"
:class="getProcessorColor(proc.status)"
:style="'width: ' + proc.progress + '%'"></div>
</div>
<span class="text-xs text-gray-400" x-text="proc.progress + '%'"></span>
</div>
</div>
</template>
</div>
</div>
</template>
</div>
</div>
</template>
</div>
<div x-show="runningJobs.length === 0" class="text-center text-gray-500 py-8">
No jobs currently running
</div>
</div>
<!-- Worker Resources -->
<div class="bg-gray-800 rounded-lg p-6 mb-6">
<h2 class="text-xl font-semibold mb-4">Worker Resources</h2>
<div class="grid grid-cols-1 md:grid-cols-3 gap-4 mb-4">
<div>
<div class="flex justify-between mb-1">
<span>CPU</span>
<span x-text="worker.cpu_percent.toFixed(1) + '%'"></span>
</div>
<div class="w-full bg-gray-700 rounded-full h-2">
<div class="bg-blue-500 h-2 rounded-full"
:style="'width: ' + worker.cpu_percent + '%'"></div>
</div>
</div>
<div>
<div class="flex justify-between mb-1">
<span>GPU</span>
<span x-text="worker.gpu_percent.toFixed(1) + '%'"></span>
</div>
<div class="w-full bg-gray-700 rounded-full h-2">
<div class="bg-green-500 h-2 rounded-full"
:style="'width: ' + worker.gpu_percent + '%'"></div>
</div>
</div>
<div>
<div class="flex justify-between mb-1">
<span>Memory</span>
<span x-text="worker.memory_percent.toFixed(1) + '%'"></span>
</div>
<div class="w-full bg-gray-700 rounded-full h-2">
<div class="bg-purple-500 h-2 rounded-full"
:style="'width: ' + worker.memory_percent + '%'"></div>
</div>
</div>
</div>
<div class="text-sm text-gray-400">
Active Processors: <span x-text="worker.active_processors"></span> / <span x-text="worker.max_processors"></span>
</div>
<div x-show="worker.processors.length > 0" class="mt-4">
<div class="text-sm font-semibold mb-2">Active Processors:</div>
<div class="space-y-1">
<template x-for="proc in worker.processors" :key="proc.name + proc.job_id">
<div class="text-sm text-gray-400">
<span class="font-medium" x-text="proc.name"></span> (Job #<span x-text="proc.job_id"></span>)
</div>
</template>
</div>
</div>
</div>
</div>
<script src="app.js"></script>
</body>
</html>
+72
View File
@@ -0,0 +1,72 @@
<!DOCTYPE html>
<html lang="zh-TW">
<head>
<meta charset="UTF-8">
<title>Momentry Monitor (Simple)</title>
<style>
body { font-family: monospace; background: #1a1a1a; color: #fff; padding: 20px; }
.job { background: #2a2a2a; padding: 15px; margin: 10px 0; border-radius: 8px; }
.processor { background: #3a3a3a; padding: 5px 10px; margin: 5px 0; border-radius: 4px; }
.completed { color: #0f0; }
.running { color: #0ff; }
.failed { color: #f00; }
</style>
</head>
<body>
<h1>Momentry Monitor</h1>
<div id="queue">Loading queue stats...</div>
<div id="jobs">Loading running jobs...</div>
<div id="worker">Loading worker status...</div>
<script>
async function refresh() {
try {
// Queue Stats
const queueRes = await fetch('/api/v1/monitor/queue_stats');
const queue = await queueRes.json();
document.getElementById('queue').innerHTML =
`<h2>Queue</h2>
<div>Pending: ${queue.pending} | Running: ${queue.running} | Completed: ${queue.completed} | Failed: ${queue.failed}</div>`;
// Running Jobs
const jobsRes = await fetch('/api/v1/monitor/running_jobs');
const jobsData = await jobsRes.json();
let jobsHtml = '<h2>Running Jobs</h2>';
if (jobsData.jobs.length === 0) {
jobsHtml += '<div>No jobs running</div>';
} else {
jobsData.jobs.forEach(job => {
jobsHtml += `
<div class="job">
<div><strong>Job #${job.job_id}</strong> - ${job.file_name}</div>
<div>UUID: ${job.file_uuid}</div>
<div>Duration: ${job.duration_secs}s</div>
<div style="margin-top: 10px;">
${job.processors.map(p =>
`<div class="processor ${p.status}">${p.name}: ${p.status} (${p.progress}%)</div>`
).join('')}
</div>
</div>`;
});
}
document.getElementById('jobs').innerHTML = jobsHtml;
// Worker Status
const workerRes = await fetch('/api/v1/monitor/worker_status');
const worker = await workerRes.json();
document.getElementById('worker').innerHTML =
`<h2>Worker Resources</h2>
<div>CPU: ${worker.cpu_percent.toFixed(1)}% | GPU: ${worker.gpu_percent.toFixed(1)}% | Memory: ${worker.memory_mb}MB (${worker.memory_percent.toFixed(1)}%)</div>
<div>Active Processors: ${worker.active_processors}/${worker.max_processors}</div>`;
} catch (e) {
console.error('Error:', e);
document.getElementById('jobs').innerHTML = '<div style="color: red;">Error loading data: ' + e.message + '</div>';
}
}
refresh();
setInterval(refresh, 5000);
</script>
</body>
</html>
+244
View File
@@ -0,0 +1,244 @@
<!DOCTYPE html>
<html lang="zh-TW">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Momentry Monitor</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
background: #0f172a;
color: #e2e8f0;
padding: 20px;
}
.container { max-width: 1200px; margin: 0 auto; }
h1 { font-size: 2rem; margin-bottom: 1rem; }
h2 { font-size: 1.5rem; margin: 1rem 0; }
.card {
background: #1e293b;
border-radius: 8px;
padding: 1.5rem;
margin-bottom: 1rem;
}
.grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 1rem; text-align: center; }
.stat { padding: 1rem; }
.stat-value { font-size: 2rem; font-weight: bold; }
.stat-label { color: #94a3b8; font-size: 0.875rem; }
.text-yellow { color: #fbbf24; }
.text-blue { color: #60a5fa; }
.text-green { color: #34d399; }
.text-red { color: #f87171; }
.job { border: 1px solid #334155; border-radius: 8px; padding: 1rem; margin: 0.5rem 0; }
.processor {
background: #334155;
border-radius: 4px;
padding: 0.5rem;
margin: 0.25rem 0;
font-size: 0.875rem;
}
.status-completed { color: #34d399; }
.status-running { color: #60a5fa; }
.status-pending { color: #fbbf24; }
.status-failed { color: #f87171; }
.progress-bar {
background: #475569;
border-radius: 9999px;
height: 8px;
overflow: hidden;
margin: 0.25rem 0;
}
.progress-fill { height: 100%; transition: width 0.3s; }
.refresh-btn {
background: #3b82f6;
color: white;
border: none;
padding: 0.5rem 1rem;
border-radius: 6px;
cursor: pointer;
}
.refresh-btn:hover { background: #2563eb; }
</style>
</head>
<body>
<div class="container">
<header style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 2rem;">
<div>
<h1>Momentry Monitor</h1>
<p style="color: #94a3b8; font-size: 0.875rem;">
Last updated: <span id="lastUpdate">Never</span>
</p>
</div>
<button class="refresh-btn" onclick="refresh()">Refresh</button>
</header>
<div class="card">
<h2>Queue Overview</h2>
<div class="grid">
<div class="stat">
<div class="stat-value text-yellow" id="pending">0</div>
<div class="stat-label">Pending</div>
</div>
<div class="stat">
<div class="stat-value text-blue" id="running">0</div>
<div class="stat-label">Running</div>
</div>
<div class="stat">
<div class="stat-value text-green" id="completed">0</div>
<div class="stat-label">Completed</div>
</div>
<div class="stat">
<div class="stat-value text-red" id="failed">0</div>
<div class="stat-label">Failed</div>
</div>
</div>
</div>
<div class="card">
<h2>Running Jobs</h2>
<div id="jobs">Loading...</div>
</div>
<div class="card">
<h2>Worker Resources</h2>
<div>
<div>CPU: <span id="cpu">0</span>%</div>
<div class="progress-bar">
<div class="progress-fill" id="cpu-bar" style="width: 0%; background: #3b82f6;"></div>
</div>
</div>
<div style="margin-top: 0.5rem;">
<div>GPU: <span id="gpu">0</span>%</div>
<div class="progress-bar">
<div class="progress-fill" id="gpu-bar" style="width: 0%; background: #34d399;"></div>
</div>
</div>
<div style="margin-top: 0.5rem;">
<div>Memory: <span id="memory">0</span> MB (<span id="memory-pct">0</span>%)</div>
<div class="progress-bar">
<div class="progress-fill" id="memory-bar" style="width: 0%; background: #a855f7;"></div>
</div>
</div>
<div style="margin-top: 1rem;">
Active Processors: <span id="active-proc">0</span> / <span id="max-proc">6</span>
</div>
</div>
</div>
<script>
const phaseNames = {
1: 'Foundation',
2: 'Core Processing',
3: 'Deep Analysis',
4: 'Knowledge Building'
};
const statusIcons = {
'completed': '✓',
'running': '🔄',
'pending': '⏸',
'failed': '✗'
};
async function refresh() {
try {
// Queue Stats
const queueRes = await fetch('/api/v1/monitor/queue_stats');
const queue = await queueRes.json();
document.getElementById('pending').textContent = queue.pending;
document.getElementById('running').textContent = queue.running;
document.getElementById('completed').textContent = queue.completed;
document.getElementById('failed').textContent = queue.failed;
// Running Jobs
const jobsRes = await fetch('/api/v1/monitor/running_jobs');
const jobsData = await jobsRes.json();
let jobsHtml = '';
if (jobsData.jobs.length === 0) {
jobsHtml = '<div style="color: #94a3b8; padding: 2rem; text-align: center;">No jobs currently running</div>';
} else {
jobsData.jobs.forEach(job => {
const duration = formatDuration(job.duration_secs);
// Group processors by phase
const phases = {};
job.processors.forEach(p => {
const phase = p.phase || 0;
if (!phases[phase]) phases[phase] = [];
phases[phase].push(p);
});
jobsHtml += `
<div class="job">
<div style="display: flex; justify-content: space-between; margin-bottom: 1rem;">
<div>
<div style="font-size: 1.25rem; font-weight: bold;">
Job #${job.job_id}
</div>
<div style="color: #94a3b8; font-size: 0.875rem;">
${job.file_name || 'Unknown file'} | UUID: ${job.file_uuid}
</div>
</div>
<div style="text-align: right;">
<div style="color: #94a3b8; font-size: 0.875rem;">Duration</div>
<div style="font-weight: bold;">${duration}</div>
</div>
</div>
<div>
${Object.entries(phases).sort(([a], [b]) => a - b).map(([phase, procs]) => `
<div style="margin: 0.5rem 0;">
<div style="font-weight: bold; margin-bottom: 0.25rem;">
Phase ${phase}: ${phaseNames[phase] || 'Other'}
</div>
<div style="display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 0.5rem;">
${procs.map(p => `
<div class="processor status-${p.status}">
${statusIcons[p.status] || '?'} ${p.name}: ${p.status} (${p.progress}%)
</div>
`).join('')}
</div>
</div>
`).join('')}
</div>
</div>
`;
});
}
document.getElementById('jobs').innerHTML = jobsHtml;
// Worker Status
const workerRes = await fetch('/api/v1/monitor/worker_status');
const worker = await workerRes.json();
document.getElementById('cpu').textContent = worker.cpu_percent.toFixed(1);
document.getElementById('cpu-bar').style.width = worker.cpu_percent + '%';
document.getElementById('gpu').textContent = worker.gpu_percent.toFixed(1);
document.getElementById('gpu-bar').style.width = worker.gpu_percent + '%';
document.getElementById('memory').textContent = worker.memory_mb;
document.getElementById('memory-pct').textContent = worker.memory_percent.toFixed(1);
document.getElementById('memory-bar').style.width = worker.memory_percent + '%';
document.getElementById('active-proc').textContent = worker.active_processors;
document.getElementById('max-proc').textContent = worker.max_processors;
document.getElementById('lastUpdate').textContent = new Date().toLocaleTimeString();
} catch (e) {
console.error('Error:', e);
}
}
function formatDuration(secs) {
if (!secs) return '0s';
const m = Math.floor(secs / 60);
const s = Math.floor(secs % 60);
return m > 0 ? `${m}m ${s}s` : `${s}s`;
}
refresh();
setInterval(refresh, 5000);
</script>
</body>
</html>
+23
View File
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html>
<head>
<title>Monitor Test</title>
</head>
<body>
<h1>Monitor API Test</h1>
<pre id="output">Loading...</pre>
<script>
async function test() {
try {
const res = await fetch('/api/v1/monitor/running_jobs');
const data = await res.json();
document.getElementById('output').textContent = JSON.stringify(data, null, 2);
} catch (e) {
document.getElementById('output').textContent = 'Error: ' + e.message;
}
}
test();
setInterval(test, 5000);
</script>
</body>
</html>