File size: 21,517 Bytes
0b2210a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 | document.addEventListener('DOMContentLoaded', () => {
const userInput = document.getElementById('userInput');
const chatLogs = document.querySelectorAll('.chat-log');
const leftChatLog = chatLogs[0];
const rightChatLog = chatLogs[1];
let currentLeftChatModel = 'chief';
let currentRightChatModel = 'ceo';
let currentSessionId = null;
let lastClickedSide = 'left';
let dynamicModelMapping = {};
// Загрузка названия текущего проекта
async function loadCurrentProjectName() {
try {
const response = await fetch('/api/project/current');
if (!response.ok) return;
const data = await response.json();
const projectNameEl = document.getElementById('current-project-name');
if (
projectNameEl &&
data.project_name &&
data.project_name !== 'Проект не выбран'
) {
projectNameEl.textContent = `| ${data.project_name}`;
projectNameEl.title = `Текущий проект: ${data.project_name}`;
}
} catch (error) {
console.error('Ошибка загрузки проекта:', error);
}
}
// Управление информ-окном
function showInfoPanel() {
const infoPanel = document.getElementById('info-panel');
const gcbWindow = document.getElementById('gcb-window');
if (infoPanel) infoPanel.classList.add('active');
if (gcbWindow) gcbWindow.classList.remove('active');
}
function showGCBQueue() {
const infoPanel = document.getElementById('info-panel');
const gcbWindow = document.getElementById('gcb-window');
if (infoPanel) infoPanel.classList.remove('active');
if (gcbWindow) gcbWindow.classList.add('active');
updateGCBQueue();
}
// Загрузка контента в информ-окно
async function loadInfoContent(infoType, additionalData = null) {
try {
const url = additionalData
? `/api/info/${infoType}?${new URLSearchParams(additionalData).toString()}`
: `/api/info/${infoType}`;
const response = await fetch(url);
if (!response.ok) throw new Error(`HTTP ${response.status}`);
const html = await response.text();
const infoPanel = document.getElementById('info-panel');
if (infoPanel) {
infoPanel.innerHTML = html;
showInfoPanel();
}
} catch (error) {
console.error(`Ошибка загрузки контента ${infoType}:`, error);
const infoPanel = document.getElementById('info-panel');
if (infoPanel) {
infoPanel.innerHTML = `<div style="padding: 20px; color: #e74c3c;">Ошибка загрузки: ${error.message}</div>`;
showInfoPanel();
}
}
}
let enterCount = 0;
let lastEnterTime = 0;
const mentionChiefBtn = document.getElementById('mentionChiefBtn');
const mentionCeoBtn = document.getElementById('mentionCeoBtn');
const topChiefBtn = document.querySelector('.chief-top');
const topCeoBtn = document.querySelector('.ceo-top');
const topGeminiBtn = document.querySelector('.gemini-top');
const topDoctorBtn = document.querySelector('.doctor-top');
const sendBtnLeft = document.getElementById('sendBtnLeft');
const sendBtnRight = document.getElementById('sendBtnRight');
function updateActiveUI(side, specificRole = null) {
lastClickedSide = side;
let roleToApply = specificRole;
if (roleToApply === null) {
const model =
side === 'left' ? currentLeftChatModel : currentRightChatModel;
if (model === 'chief') roleToApply = 'chief';
else if (model === 'gemini_direct') roleToApply = 'gemini';
else if (model === 'ceo') roleToApply = 'ceo';
else if (model === 'doctor') roleToApply = 'doctor';
}
// Сброс всех классов и теней
if (mentionChiefBtn)
mentionChiefBtn.classList.remove('chief-active', 'inactive');
if (mentionCeoBtn) mentionCeoBtn.classList.remove('ceo-active', 'inactive');
[topChiefBtn, topCeoBtn, topGeminiBtn, topDoctorBtn].forEach((btn) => {
if (btn) {
btn.style.boxShadow = 'none';
btn.style.background = '#95a5a6'; // Серый цвет для неактивных кнопок
}
});
// Логика подсветки конкретной роли
if (roleToApply === 'chief' && topChiefBtn) {
topChiefBtn.style.background = 'linear-gradient(135deg,#4a6fa5,#2e4a7a)';
topChiefBtn.style.boxShadow = '0 0 10px #4a6fa5';
if (mentionChiefBtn) mentionChiefBtn.classList.add('chief-active');
if (mentionCeoBtn) mentionCeoBtn.classList.add('inactive');
} else if (roleToApply === 'gemini' && topGeminiBtn) {
topGeminiBtn.style.background = 'linear-gradient(135deg,#4a6fa5,#2e4a7a)';
topGeminiBtn.style.boxShadow = '0 0 10px #4a6fa5';
if (mentionChiefBtn) mentionChiefBtn.classList.add('chief-active');
} else if (roleToApply === 'ceo' && topCeoBtn) {
topCeoBtn.style.background = 'linear-gradient(135deg,#2d8f4a,#1a5c2e)';
topCeoBtn.style.boxShadow = '0 0 10px #2d8f4a';
if (mentionCeoBtn) mentionCeoBtn.classList.add('ceo-active');
if (mentionChiefBtn) mentionChiefBtn.classList.add('inactive');
} else if (roleToApply === 'doctor' && topDoctorBtn) {
topDoctorBtn.style.background = 'linear-gradient(135deg,#2d8f4a,#1a5c2e)';
topDoctorBtn.style.boxShadow = '0 0 10px #2d8f4a';
if (mentionCeoBtn) mentionCeoBtn.classList.add('ceo-active');
}
}
async function loadDynamicModelMapping() {
try {
const response = await fetch('/api/model_mapping');
const data = await response.json();
if (data.model_mapping) {
dynamicModelMapping = data.model_mapping;
initDashboard();
}
} catch (error) {
console.error('Ошибка загрузки динамического маппинга моделей:', error);
}
}
function initDashboard() {
const visArea = document.getElementById('gcbQueue');
if (!visArea) return;
let html =
'<h3 style="margin-bottom:10px; color:#2c3e50; border-bottom:2px solid #4a6fa5; padding-bottom:5px;">👥 Состав лаборатории AI-Colab</h3>';
html +=
'<div style="display:grid; grid-template-columns: 1fr 1fr; gap:10px;">';
const depts = [
{ name: 'Отдел A Аналитика', models: ['Флик', 'Флок'], color: '#0d47a1' },
{
name: 'Отдел B Локализация',
models: ['Клик', 'Клак'],
color: '#1b5e20',
},
{ name: 'Отдел C Критика', models: ['Чек', 'Чок'], color: '#4a148c' },
{
name: 'Отдел D Интеграция',
models: ['Линк', 'Синх'],
color: '#e65100',
},
];
depts.forEach((d) => {
html += `<div style="border-left: 4px solid ${d.color}; padding:8px; background:#f8f9fa; border-radius:4px; box-shadow: 0 2px 4px rgba(0,0,0,0.05);">`;
html += `<strong style="color:${d.color}; font-size:12px;">${d.name}</strong><br>`;
d.models.forEach((m) => {
const modelInfo = dynamicModelMapping[m];
const modelId = modelInfo ? modelInfo.model_id : 'local';
const shortId = modelId.split('/').pop();
html += `<span style="font-size:11px; color:#444;">• ${m} <span style="color:#7f8c8d; font-size:10px;">(${shortId})</span></span><br>`;
});
html += '</div>';
});
html += '</div>';
visArea.innerHTML = html;
}
function addMessage(
chatLog,
sender,
message,
pairId = null,
modelData = null,
) {
const msgDiv = document.createElement('div');
msgDiv.className = 'msg-bubble';
msgDiv.style.marginBottom = '8px';
msgDiv.style.padding = '8px';
msgDiv.style.borderRadius = '4px';
msgDiv.style.background = sender.includes('Вы') ? '#e3f2fd' : '#f5f5f5';
let displayName = sender;
if (sender === 'AI' && modelData) {
const localModels = ['chief', 'ceo', 'doctor'];
if (localModels.includes(modelData.key)) {
displayName = modelData.roleName || modelData.key;
} else {
const shortModelId = modelData.modelId
? modelData.modelId.length > 30
? modelData.modelId.substring(0, 27) + '...'
: modelData.modelId
: '';
displayName = modelData.roleName
? `${modelData.roleName} (${shortModelId})`
: modelData.key;
}
}
// Простая обработка Markdown-подобных жирных шрифтов
const formattedMessage = message.replace(
/\*\*(.*?)\*\*/g,
'<strong>$1</strong>',
);
msgDiv.innerHTML = `
<div style="font-size:11px; color:#7f8c8d; margin-bottom:2px;">${displayName}</div>
<div style="white-space: pre-wrap;">${formattedMessage}</div>
${pairId ? `<button class="soul-btn" style="margin-top:5px;" onclick="analyzePair('${pairId}')">✨ Soul Layer Анализ</button>` : ''}
`;
chatLog.appendChild(msgDiv);
chatLog.scrollTop = chatLog.scrollHeight;
}
function handleSystemCommand(command, side) {
const cmd = command.trim().toLowerCase();
const chatLog = side === 'left' ? leftChatLog : rightChatLog;
switch (cmd) {
case '/help':
showHelpModal();
break;
case '/status':
addMessage(
chatLog,
'Система',
'Команда /status в разработке. Проверьте .PROJECT_STATE.md',
);
break;
case '/projects':
addMessage(
chatLog,
'Система',
'Используйте меню "Проекты" в нижней панели.',
);
break;
case '/clear':
chatLog.innerHTML = `<p class="system-msg">Чат очищен</p>`;
break;
case '/mode':
const model =
side === 'left' ? currentLeftChatModel : currentRightChatModel;
addMessage(
chatLog,
'Система',
`Текущая модель: ${model}. Режим работы: AI-Collaboration.`,
);
break;
default:
console.log('Неизвестная команда:', cmd);
}
}
function showModal(title, content) {
const modal = document.getElementById('soulModal');
const modalTitle = document.getElementById('modalTitle');
const modalBody = document.getElementById('modalBody');
if (!modal) return;
modal.style.display = 'block';
if (modalTitle) modalTitle.textContent = title;
if (modalBody) modalBody.innerHTML = content;
}
function showHelpModal() {
const helpContent = `
<div style="font-family: sans-serif;">
<h4 style="color: #2c3e50; border-bottom: 2px solid #3498db; padding-bottom: 5px;">🎮 Системные команды AI-Colab</h4>
<div style="margin-top: 15px;">
<strong>Формат сообщений:</strong>
<ul style="font-size: 13px;">
<li><code style="background: #eee; padding: 2px 4px;">[INTRO]</code> — роль модели</li>
<li><code style="background: #eee; padding: 2px 4px;">[TASK]</code> — задания</li>
</ul>
</div>
<div style="margin-top: 15px;">
<strong>Команды:</strong>
<table style="width: 100%; font-size: 13px; border-collapse: collapse;">
<tr><td style="padding: 3px;"><code>/help</code></td><td>Справка</td></tr>
<tr><td style="padding: 3px;"><code>/clear</code></td><td>Очистить</td></tr>
<tr><td style="padding: 3px;"><code>/mode</code></td><td>Режим</td></tr>
</table>
</div>
</div>`;
showModal('Справка', helpContent);
}
window.analyzePair = async function (pairId) {
if (!currentSessionId) return alert('Сессия не начата');
const modal = document.getElementById('soulModal');
const modalBody = document.getElementById('modalBody');
modal.style.display = 'block';
modalBody.innerHTML =
'<div class="system-msg">Анализ качества диалога...</div>';
try {
const response = await fetch(
`/api/session/${currentSessionId}/analyze/${pairId}`,
{ method: 'POST' },
);
const data = await response.json();
if (data.error) {
modalBody.innerHTML = `<div class="status-failed">${data.error}</div>`;
return;
}
modalBody.innerHTML = `
<div class="analysis-grid">
<div class="analysis-card">
<h4>Оценка: ${data.score || 0}/10</h4>
<div class="score-container"><div class="score-bar" style="width: ${data.score * 10}%"></div></div>
</div>
<div class="analysis-card">
<h4>Конструктивность: ${data.constructive_competition ? '✅' : '⚠️'}</h4>
</div>
</div>
<div class="analysis-card" style="margin-top:10px;">
<h4>Резюме ШЕФА:</h4>
<p>${data.summary || 'Нет данных'}</p>
</div>`;
} catch (e) {
modalBody.innerHTML = 'Ошибка связи с сервером.';
}
};
// WebSocket для Gemini MCP
let geminiWS = null;
function connectGeminiWS() {
if (geminiWS && geminiWS.readyState === WebSocket.OPEN) return;
addMessage(leftChatLog, 'Система', 'Подключение к Gemini MCP...');
geminiWS = new WebSocket('ws://127.0.0.1:8765');
geminiWS.onopen = () => {
addMessage(leftChatLog, 'Система', '✅ Прямой канал связи установлен.');
if (topGeminiBtn) topGeminiBtn.style.boxShadow = '0 0 15px #3498db';
};
geminiWS.onmessage = (event) => {
const data = JSON.parse(event.data);
if (data.type === 'execution_result') {
const res = data.result;
let out = res.success ? '✅ Выполнено' : '❌ Ошибка';
if (res.output)
out += `\n<pre style="font-size:10px; background:#eee; padding:5px;">${res.output}</pre>`;
addMessage(leftChatLog, 'Gemini MCP', out);
}
};
}
async function handleSend(side) {
updateActiveUI(side);
const rawValue = userInput.value.trim();
if (!rawValue) return;
if (rawValue.startsWith('/')) {
handleSystemCommand(rawValue, side);
userInput.value = '';
return;
}
// Очистка от введённых вручную маркеров
const cleanMessage = rawValue
.replace(/^\[INTRO\]\s*/i, '')
.replace(/^\[TASK\]\s*/i, '');
const fullMessage = '[INTRO] ' + cleanMessage;
const chatLog = side === 'left' ? leftChatLog : rightChatLog;
const modelName =
side === 'left' ? currentLeftChatModel : currentRightChatModel;
const senderLabel = `Вы (${modelName})`;
addMessage(chatLog, senderLabel, cleanMessage);
userInput.value = '';
let modelKey = modelName;
if (side === 'left' && modelKey === 'gemini_direct') {
if (!geminiWS || geminiWS.readyState !== WebSocket.OPEN) {
connectGeminiWS();
return;
}
// Для консоли отправляем чистую команду БЕЗ [INTRO]
geminiWS.send(
JSON.stringify({
directive: {
action: 'shell',
tool: 'direct_command',
parameters: { command: cleanMessage },
},
}),
);
return;
}
try {
const response = await fetch('/api/secretary', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
message: fullMessage,
side: side,
model: modelKey,
session_id: currentSessionId,
}),
});
const data = await response.json();
if (data.session_id) currentSessionId = data.session_id;
if (data.response)
addMessage(chatLog, 'AI', data.response, data.pair_id, data.modelInfo);
else if (data.error)
addMessage(chatLog, 'Система', 'Ошибка: ' + data.error);
} catch (e) {
addMessage(chatLog, 'Система', 'Ошибка связи с сервером');
}
}
if (sendBtnLeft)
sendBtnLeft.addEventListener('click', () => {
lastClickedSide = 'left';
handleSend('left');
});
if (sendBtnRight)
sendBtnRight.addEventListener('click', () => {
lastClickedSide = 'right';
handleSend('right');
});
userInput.addEventListener('keydown', (e) => {
if (e.key === 'Enter' && !e.shiftKey) {
const now = Date.now();
if (now - lastEnterTime < 700) enterCount++;
else enterCount = 1;
lastEnterTime = now;
if (enterCount === 2) {
e.preventDefault();
handleSend(lastClickedSide);
enterCount = 0;
}
}
});
if (topChiefBtn)
topChiefBtn.addEventListener('click', () => {
currentLeftChatModel = 'chief';
updateActiveUI('left', 'chief');
addMessage(leftChatLog, 'Система', 'Шеф на связи.');
});
if (topCeoBtn)
topCeoBtn.addEventListener('click', () => {
currentRightChatModel = 'ceo';
updateActiveUI('right', 'ceo');
addMessage(rightChatLog, 'Система', 'СЕО на связи.');
});
if (topGeminiBtn)
topGeminiBtn.addEventListener('click', () => {
currentLeftChatModel = 'gemini_direct';
updateActiveUI('left', 'gemini');
addMessage(leftChatLog, 'Система', '[CONSOLE] Готов к исполнению задач.');
showGCBQueue(); // Показываем очередь GCB
connectGeminiWS();
});
if (topDoctorBtn)
topDoctorBtn.addEventListener('click', () => {
currentRightChatModel = 'doctor';
updateActiveUI('right', 'doctor');
addMessage(
rightChatLog,
'Система',
'Доктор на связи. Провести внеплановый медосмотр моделей?',
);
});
// Инициализация
loadCurrentProjectName();
showInfoPanel(); // Показываем информ-окно по умолчанию
document.querySelectorAll('.dept-btn').forEach((btn) => {
btn.addEventListener('click', () => {
let id = btn.classList.contains('dept-a')
? 'A'
: btn.classList.contains('dept-b')
? 'B'
: btn.classList.contains('dept-c')
? 'C'
: 'D';
currentLeftChatModel = `dept-${id}`;
updateActiveUI('left');
});
});
document.querySelectorAll('.model-btn').forEach((btn) => {
btn.addEventListener('click', () => {
const modelName = btn.dataset.model;
currentRightChatModel = modelName;
updateActiveUI('right');
addMessage(
rightChatLog,
'Система',
`Модель ${modelName} выбрана для диалога.`,
);
});
});
document.querySelectorAll('.mention-content a').forEach((link) => {
link.addEventListener('click', (e) => {
e.preventDefault();
const role = link.dataset.role;
const isCeo = link
.closest('.mention-menu')
.querySelector('.mention-btn')
.textContent.includes('СЕО');
if (isCeo) {
currentRightChatModel = role;
updateActiveUI('right');
addMessage(
rightChatLog,
'Система',
`Модель ${role} выбрана из меню СЕО.`,
);
} else {
currentLeftChatModel = role;
updateActiveUI('left');
addMessage(leftChatLog, 'Система', `Роль ${role} выбрана из меню ШЕФ.`);
}
});
});
const modal = document.getElementById('soulModal');
const closeModal = document.getElementById('closeModal');
if (closeModal) closeModal.onclick = () => (modal.style.display = 'none');
window.onclick = (e) => {
if (e.target == modal) modal.style.display = 'none';
};
// Инициализация
loadDynamicModelMapping();
updateActiveUI('left');
connectGeminiWS(); // Автоматическое подключение MCP
updateGCBQueue();
setInterval(updateGCBQueue, 5000);
async function updateGCBQueue() {
try {
const response = await fetch('/api/gcb/queue');
const queue = await response.json();
const container = document.getElementById('gcbQueue');
if (!container || !Array.isArray(queue)) return;
if (queue.length === 0) {
initDashboard();
return;
}
container.innerHTML = queue
.reverse()
.map(
(item) => `
<div class="gcb-item">
<span class="gcb-status status-${item.status}">${item.status}</span>
<span class="gcb-cmd">> ${item.command}</span>
${item.status === 'pending' ? `<button class="exec-btn" onclick="executeGCB('${item.id}')">Run</button>` : ''}
</div>`,
)
.join('');
} catch (e) {}
}
window.executeGCB = async function (id) {
try {
await fetch(`/api/gcb/execute/${id}`, { method: 'POST' });
updateGCBQueue();
} catch (e) {}
};
});
|