Separate skills state completely per user
- All skill toggles are now per-user (except multi-agent-orchestrator which remains global). Admin toggle without a target username defaults to their own per-user state instead of the global file. - GET /api/skills defaults to the requesting user's own state (previously returned global state when no ?username= param was given). - Add SkillsManager.initUserSkillsState(): creates per-user skills_state.json from global defaults if missing — called on user creation and on every login as a safety net for existing accounts. - Create initial per-user skills_state.json for cherry and jasmine. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
2d3e33b6b3
commit
22bfc3ac27
5 files changed
+66
-33
No files matched your search
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"lawyer": false,
|
||||
"psychiatrist": false,
|
||||
"musician": false,
|
||||
"accountant": false,
|
||||
"investor": false,
|
||||
"counselor": false,
|
||||
"meteorologist": true,
|
||||
"presenter": true
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"lawyer": false,
|
||||
"psychiatrist": false,
|
||||
"musician": false,
|
||||
"accountant": false,
|
||||
"investor": true,
|
||||
"counselor": false,
|
||||
"meteorologist": true,
|
||||
"presenter": false
|
||||
}
|
||||
+26
-29
@@ -8577,6 +8577,7 @@ app.post('/api/auth/login', (req, res) => {
|
||||
}
|
||||
const sessionToken = crypto.randomBytes(32).toString('hex');
|
||||
activeSessions.set(sessionToken, { username, role, createdAt: Date.now() });
|
||||
skillsManager.initUserSkillsState(getUserWorkspace(username));
|
||||
res.setHeader('Set-Cookie', `${AUTH_COOKIE}=${sessionToken}; HttpOnly; Path=/; SameSite=Lax${req.secure ? '; Secure' : ''}; Max-Age=604800`);
|
||||
res.json({ success: true, username, role, token: sessionToken });
|
||||
});
|
||||
@@ -8676,6 +8677,7 @@ app.post('/api/users', (req, res) => {
|
||||
const users = listUsers();
|
||||
users.push(username);
|
||||
saveUserList(users);
|
||||
skillsManager.initUserSkillsState(getUserWorkspace(username));
|
||||
// Optional channel mappings at creation time
|
||||
const telegramId = String(req.body?.telegramId || '').trim();
|
||||
if (/^\d{6,20}$/.test(telegramId)) {
|
||||
@@ -9819,8 +9821,8 @@ app.get('/api/skills', async (req, res) => {
|
||||
const session = (req as any).user as { username: string; role: string } | undefined;
|
||||
const GLOBAL_ONLY = new Set(['multi-agent-orchestrator']);
|
||||
|
||||
// ?username= : admin can query any user, regular user can only query themselves.
|
||||
let targetUser = (req.query.username as string) || null;
|
||||
// Default to the requesting user's own state; ?username= lets admin query other users.
|
||||
let targetUser = (req.query.username as string) || session?.username || null;
|
||||
if (targetUser && session?.role !== 'admin' && session?.username !== targetUser) {
|
||||
targetUser = session?.username ?? null;
|
||||
}
|
||||
@@ -9933,42 +9935,37 @@ app.post('/api/skills/:id/toggle', async (req, res) => {
|
||||
|
||||
const session = (req as any).user as { username: string; role: string } | undefined;
|
||||
const GLOBAL_ONLY = new Set(['multi-agent-orchestrator']);
|
||||
const targetUser = (req.body?.username as string) || null;
|
||||
|
||||
// Per-user toggle: admin can target any user; regular user can only target themselves.
|
||||
if (targetUser && !GLOBAL_ONLY.has(skillId)) {
|
||||
if (session?.role !== 'admin' && session?.username !== targetUser) {
|
||||
res.status(403).json({ success: false, error: 'Forbidden' }); return;
|
||||
// GLOBAL_ONLY skills (multi-agent-orchestrator) keep the existing global toggle path.
|
||||
if (GLOBAL_ONLY.has(skillId)) {
|
||||
if (skillId === 'multi-agent-orchestrator' && !current.enabled) {
|
||||
const eligibility = await checkOrchestrationEligibility();
|
||||
if (!eligibility.eligible) {
|
||||
res.status(409).json({ success: false, error: eligibility.reason || 'Configure a valid secondary model first.' });
|
||||
return;
|
||||
}
|
||||
}
|
||||
const userDir = getUserWorkspace(targetUser);
|
||||
const curState = skillsManager.getUserState(userDir);
|
||||
const curEnabled = (curState && skillId in curState) ? curState[skillId] : current.enabled;
|
||||
const skill = skillsManager.setUserSkillEnabled(userDir, skillId, !curEnabled);
|
||||
const skill = skillsManager.toggle(skillId);
|
||||
if (!skill) { res.status(404).json({ success: false, error: 'Skill not found' }); return; }
|
||||
res.json({ success: true, skill: { id: skill.id, name: skill.name, enabled: skill.enabled }, targetUser });
|
||||
if (skillId === 'multi-agent-orchestrator') setOrchestrationEnabled(skill.enabled);
|
||||
res.json({ success: true, skill: { id: skill.id, name: skill.name, enabled: skill.enabled } });
|
||||
return;
|
||||
}
|
||||
|
||||
// Global toggle (existing behavior).
|
||||
if (skillId === 'multi-agent-orchestrator' && !current.enabled) {
|
||||
const eligibility = await checkOrchestrationEligibility();
|
||||
if (!eligibility.eligible) {
|
||||
res.status(409).json({
|
||||
success: false,
|
||||
error: eligibility.reason || 'Configure a valid secondary model first.',
|
||||
});
|
||||
return;
|
||||
}
|
||||
// All other skills: always per-user.
|
||||
// body.username lets admin target another user; default is the requesting user.
|
||||
const targetUser = (req.body?.username as string) || session?.username || null;
|
||||
if (!targetUser) { res.status(400).json({ success: false, error: 'Not authenticated' }); return; }
|
||||
if (session?.role !== 'admin' && session?.username !== targetUser) {
|
||||
res.status(403).json({ success: false, error: 'Forbidden' }); return;
|
||||
}
|
||||
|
||||
const skill = skillsManager.toggle(skillId);
|
||||
const userDir = getUserWorkspace(targetUser);
|
||||
const curState = skillsManager.getUserState(userDir);
|
||||
const curEnabled = (curState && skillId in curState) ? curState[skillId] : current.enabled;
|
||||
const skill = skillsManager.setUserSkillEnabled(userDir, skillId, !curEnabled);
|
||||
if (!skill) { res.status(404).json({ success: false, error: 'Skill not found' }); return; }
|
||||
|
||||
if (skillId === 'multi-agent-orchestrator') {
|
||||
setOrchestrationEnabled(skill.enabled);
|
||||
}
|
||||
|
||||
res.json({ success: true, skill: { id: skill.id, name: skill.name, enabled: skill.enabled } });
|
||||
res.json({ success: true, skill: { id: skill.id, name: skill.name, enabled: skill.enabled }, targetUser });
|
||||
});
|
||||
|
||||
app.post('/api/skills', (req, res) => {
|
||||
|
||||
@@ -215,6 +215,22 @@ export class SkillsManager {
|
||||
}
|
||||
}
|
||||
|
||||
/** 유저별 skills_state.json이 없으면 글로벌 defaults로 초기화. 이미 있으면 no-op. */
|
||||
initUserSkillsState(userDir: string): void {
|
||||
const p = path.join(userDir, '..', 'skills_state.json');
|
||||
if (fs.existsSync(p)) return;
|
||||
const state = Object.fromEntries(
|
||||
Object.entries(this.enabledState).filter(([k]) => !this.GLOBAL_ONLY_SKILLS.has(k)),
|
||||
);
|
||||
try {
|
||||
fs.mkdirSync(path.dirname(p), { recursive: true });
|
||||
fs.writeFileSync(p, JSON.stringify(state, null, 2), 'utf-8');
|
||||
console.log(`[Skills] Initialized per-user state for ${path.basename(path.dirname(p))}`);
|
||||
} catch (err) {
|
||||
console.error('[Skills] Failed to init user state:', err);
|
||||
}
|
||||
}
|
||||
|
||||
/** 유저별 스킬 상태 저장. 첫 저장 시 글로벌 state를 baseline으로 복사. */
|
||||
setUserSkillEnabled(userDir: string, skillId: string, enabled: boolean): Skill | null {
|
||||
if (this.GLOBAL_ONLY_SKILLS.has(skillId)) return null;
|
||||
|
||||
+4
-4
@@ -6917,7 +6917,7 @@ async function loadInstalledSkills() {
|
||||
const el = document.getElementById('skills-list');
|
||||
el.innerHTML = '<div style="padding:12px;color:var(--muted);font-size:11px"><span class="spinner"></span> Loading...</div>';
|
||||
try {
|
||||
const effectiveUser = skillsTargetUser || (currentUser?.role !== 'admin' ? currentUser?.username : null);
|
||||
const effectiveUser = skillsTargetUser || currentUser?.username || null;
|
||||
const url = effectiveUser ? `/api/skills?username=${encodeURIComponent(effectiveUser)}` : '/api/skills';
|
||||
const data = await api(url);
|
||||
const skills = data.skills || [];
|
||||
@@ -6958,13 +6958,13 @@ async function toggleSkill(id) {
|
||||
addProcessEntry('error', skillMeta.eligibleReason || 'This skill is currently unavailable.');
|
||||
return;
|
||||
}
|
||||
// globalOnly 스킬은 per-user 모드에서 토글 불가
|
||||
if (skillMeta?.globalOnly && (skillsTargetUser || currentUser?.role !== 'admin')) {
|
||||
// globalOnly 스킬은 per-user 모드에서 토글 불가 (서버가 전역으로 처리)
|
||||
if (skillMeta?.globalOnly && skillsTargetUser) {
|
||||
addProcessEntry('error', `"${skillMeta.name}" 스킬은 글로벌 전용입니다.`);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const effectiveUser = skillsTargetUser || (currentUser?.role !== 'admin' ? currentUser?.username : null);
|
||||
const effectiveUser = skillsTargetUser || currentUser?.username || null;
|
||||
const body = effectiveUser ? JSON.stringify({ username: effectiveUser }) : undefined;
|
||||
const res = await api(`/api/skills/${encodeURIComponent(id)}/toggle`, { method: 'POST', body });
|
||||
if (!res.success) {
|
||||
|
||||
Reference in new issue
Block a user