// harnessed_reasoning/reasoning_console.js // JavaScript for the reasoning console UI. // Loaded automatically by Sage from wwwroot/. (function() { var reasoningWS = { ws: null, stepCount: 0, userId: null, connect: function() { var self = this; // Get user_id from session if available try { if (bricks.app && bricks.app.get_session) { this.userId = bricks.app.get_session(); } } catch(e) {} if (!this.userId) this.userId = 'anonymous'; var protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'; var url = protocol + '//' + window.location.host + '/wss/harnessed_reasoning/reasoning_console.wss'; this.ws = new WebSocket(url); this.ws.onopen = function() { self.updateStatus('connected', '已连接'); self.ws.send(JSON.stringify({cmd: 'connect', user_id: self.userId})); }; this.ws.onmessage = function(e) { try { self.handleMessage(JSON.parse(e.data)); } catch(err) {} }; this.ws.onerror = function() { self.updateStatus('disconnected', '连接错误'); }; this.ws.onclose = function() { self.updateStatus('disconnected', '已断开'); setTimeout(function() { self.connect(); }, 3000); }; }, updateStatus: function(state, text) { var statusWidget = bricks.getWidgetById('status_text'); var badgeWidget = bricks.getWidgetById('ws_status'); if (statusWidget) statusWidget.set_text(text); if (badgeWidget) { var labels = {connected: '已连接', connecting: '连接中', disconnected: '未连接'}; badgeWidget.set_text(labels[state] || state); } }, handleMessage: function(msg) { if (msg.type === 'connected' || msg.type === 'pong') return; if (msg.type === 'error') { this.addStep('error', msg.data ? msg.data.message : msg.message || '错误', msg.data || {}); var btn = bricks.getWidgetById('start_btn'); if (btn) btn.options.disabled = false; return; } var event = msg.event || msg.type; var data = msg.data || {}; var message = data.message || event; this.addStep(event, message, data); if (event === 'reasoning_complete' || event === 'execution_complete') { var btn = bricks.getWidgetById('start_btn'); if (btn) btn.options.disabled = false; } }, addStep: function(event, message, data) { var container = bricks.getWidgetById('steps_container'); if (!container) return; this.stepCount++; var typeClass = 'info', typeLabel = event; if (event.indexOf('context') >= 0) { typeClass = 'context'; typeLabel = '上下文'; } else if (event.indexOf('plan') >= 0 || event.indexOf('thinking') >= 0) { typeClass = 'plan'; typeLabel = '思考'; } else if (event.indexOf('tool_call') >= 0) { typeClass = 'tool'; typeLabel = data.success === false ? '工具失败' : '工具调用'; } else if (event.indexOf('step_') >= 0) { typeClass = 'execute'; typeLabel = '执行步骤'; } else if (event.indexOf('complete') >= 0) { typeClass = 'result'; typeLabel = '完成'; } else if (event.indexOf('error') >= 0) { typeClass = 'error'; typeLabel = '错误'; } else if (event.indexOf('safety') >= 0) { typeClass = 'plan'; typeLabel = '安全检查'; } var now = new Date(); var timeStr = now.getHours().toString().padStart(2,'0') + ':' + now.getMinutes().toString().padStart(2,'0') + ':' + now.getSeconds().toString().padStart(2,'0'); var detailsHtml = ''; if (data.request) detailsHtml += '
请求: ' + this.escapeHtml(data.request) + '
'; if (data.analysis) detailsHtml += '
分析: ' + this.escapeHtml(data.analysis) + '
'; if (data.tool) detailsHtml += '
工具: ' + data.tool + (data.parameters ? ' 参数: ' + JSON.stringify(data.parameters) : '') + '
'; if (data.result) detailsHtml += '
结果: ' + this.escapeHtml(String(data.result).substring(0, 500)) + '
'; var dotColor = typeClass === 'result' ? '4caf50' : typeClass === 'error' ? 'f44336' : typeClass === 'tool' ? 'ce93d8' : typeClass === 'execute' ? 'ffcc80' : typeClass === 'plan' ? '90caf9' : '2196F3'; var dotBg = typeClass === 'result' ? '4caf50' : typeClass === 'error' ? 'f44336' : typeClass === 'tool' ? '7b1fa2' : typeClass === 'execute' ? 'e65100' : typeClass === 'plan' ? '0d47a1' : '1565c0'; var badgeBg = typeClass === 'context' ? '#1b5e20' : typeClass === 'plan' ? '#0d47a1' : typeClass === 'tool' ? '#4a148c' : typeClass === 'execute' ? '#e65100' : typeClass === 'result' ? '#1b5e20' : typeClass === 'error' ? '#b71c1c' : '#1565c0'; var badgeColor = typeClass === 'context' ? '#a5d6a7' : typeClass === 'plan' ? '#90caf9' : typeClass === 'tool' ? '#ce93d8' : typeClass === 'execute' ? '#ffcc80' : typeClass === 'result' ? '#a5d6a7' : typeClass === 'error' ? '#ef9a9a' : '#bbdefb'; var stepHtml = '
' + '
' + '
' + '
' + '' + typeLabel + '' + '' + timeStr + '
' + '
' + this.escapeHtml(message) + '
' + detailsHtml + '
'; container.el.insertAdjacentHTML('beforeend', stepHtml); var currentWidget = bricks.getWidgetById('current_step'); if (currentWidget) currentWidget.set_text(message); container.el.scrollTop = container.el.scrollHeight; }, escapeHtml: function(text) { var div = document.createElement('div'); div.textContent = text; return div.innerHTML; } }; // Register functions for bricks RF bricks.RF.register('startReasoning', async function(params) { var ws = reasoningWS; if (!ws.ws || ws.ws.readyState !== WebSocket.OPEN) { // Try to connect first ws.connect(); // Wait a bit for connection await new Promise(function(resolve) { setTimeout(resolve, 1000); }); if (!ws.ws || ws.ws.readyState !== WebSocket.OPEN) { bricks.show_error_message && bricks.show_error_message('WebSocket 未连接'); return; } } var input = bricks.getWidgetById('reasoning_input'); if (!input || !input.options.text.trim()) { bricks.show_error_message && bricks.show_error_message('请输入推理请求'); return; } var btn = bricks.getWidgetById('start_btn'); if (btn) btn.options.disabled = true; ws.ws.send(JSON.stringify({cmd: 'start_reasoning', request: input.options.text.trim(), user_id: ws.userId})); }); bricks.RF.register('clearSteps', async function(params) { var container = bricks.getWidgetById('steps_container'); if (container) container.el.innerHTML = ''; reasoningWS.stepCount = 0; var currentWidget = bricks.getWidgetById('current_step'); if (currentWidget) currentWidget.set_text(''); var btn = bricks.getWidgetById('start_btn'); if (btn) btn.options.disabled = false; }); // Auto-connect when DOM is ready function tryConnect() { if (bricks.app) { reasoningWS.connect(); } else { setTimeout(tryConnect, 200); } } tryConnect(); // Expose for legacy script actiontype compatibility window.startReasoning = function() { bricks.RF.call('startReasoning', {}); }; window.clearSteps = function() { bricks.RF.call('clearSteps', {}); }; window.reasoningWS = reasoningWS; })();