EternalAI/hermes-server/src/routes/profiles.js

89 lines
3.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Profile 管理 API — 查看、下载 profile 文件(供 Hermes Agent 和管理员使用)
const express = require('express');
const { adminAuthMiddleware } = require('../middleware/admin-auth');
const { listProfiles, getProfileMeta, getProfileFile, deleteProfile } = require('../lib/profile-store');
const router = express.Router();
// 获取所有 profile 列表(需管理员认证)
router.get('/', adminAuthMiddleware, (req, res) => {
try {
const profiles = listProfiles();
res.json({ profiles, total: profiles.length });
} catch (err) {
console.error('获取 profile 列表失败:', err);
res.status(500).json({ error: '服务器错误' });
}
});
// 获取 profile 详情(需管理员认证)
router.get('/:profileId', adminAuthMiddleware, (req, res) => {
try {
const meta = getProfileMeta(req.params.profileId);
if (!meta) {
return res.status(404).json({ error: 'Profile 不存在' });
}
res.json({ profile: meta });
} catch (err) {
console.error('获取 profile 详情失败:', err);
res.status(500).json({ error: '服务器错误' });
}
});
// 下载 SOUL.md供 Hermes Agent 使用,限制本机访问)
router.get('/:profileId/SOUL.md', (req, res) => {
try {
// P2 修复:限制本机访问
const clientIp = (req.socket.remoteAddress || '').replace(/^::ffff:/, '');
if (clientIp !== '127.0.0.1' && clientIp !== '::1' && clientIp !== '') {
return res.status(403).json({ error: '仅限本机访问' });
}
const content = getProfileFile(req.params.profileId, 'SOUL.md');
if (content === null) {
return res.status(404).json({ error: 'Profile 或 SOUL.md 不存在' });
}
// P3 修复:使用语义正确的 Content-Type
res.type('text/markdown').send(content);
} catch (err) {
console.error('获取 SOUL.md 失败:', err);
res.status(500).json({ error: '服务器错误' });
}
});
// 下载 config.yaml供 Hermes Agent 使用,限制本机访问)
router.get('/:profileId/config.yaml', (req, res) => {
try {
// P2 修复:限制本机访问
const clientIp = (req.socket.remoteAddress || '').replace(/^::ffff:/, '');
if (clientIp !== '127.0.0.1' && clientIp !== '::1' && clientIp !== '') {
return res.status(403).json({ error: '仅限本机访问' });
}
const content = getProfileFile(req.params.profileId, 'config.yaml');
if (content === null) {
return res.status(404).json({ error: 'Profile 或 config.yaml 不存在' });
}
// P3 修复:使用语义正确的 Content-Type
res.type('text/yaml').send(content);
} catch (err) {
console.error('获取 config.yaml 失败:', err);
res.status(500).json({ error: '服务器错误' });
}
});
// 删除 profile需管理员认证
router.delete('/:profileId', adminAuthMiddleware, (req, res) => {
try {
const deleted = deleteProfile(req.params.profileId);
if (!deleted) {
return res.status(404).json({ error: 'Profile 不存在' });
}
res.json({ message: 'Profile 已删除' });
} catch (err) {
console.error('删除 profile 失败:', err);
res.status(500).json({ error: '服务器错误' });
}
});
module.exports = router;