harnessed_reasoning/wwwroot/reasoning_console.js
yumoqing c6df7f1829 fix: move JS logic from Html widget to external .js file
- Html widget's innerHTML doesn't execute <script> tags, causing
  'startReasoning is not defined' error
- Extract all JS (reasoningWS, startReasoning, clearSteps) into
  reasoning_console.js which is auto-loaded by Sage's header.tmpl
- Switch Button binds from actiontype='script' to
  actiontype='registerfunction' with rfname
- Fix widgettype 'Input' -> 'KeyinText' (Input is not registered)
- Fix widgettype 'Scroll' -> 'VBox' with CSS overflow (Scroll not registered)
- Remove duplicate WebSocket widget (JS handles WS connection directly)
- Clean up failed fix script attempts
2026-05-23 12:05:42 +08:00

172 lines
8.5 KiB
JavaScript

// 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 + '/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.app.find_widget_by_id('status_text');
var badgeWidget = bricks.app.find_widget_by_id('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.app.find_widget_by_id('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.app.find_widget_by_id('start_btn');
if (btn) btn.options.disabled = false;
}
},
addStep: function(event, message, data) {
var container = bricks.app.find_widget_by_id('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 += '<div style="margin-top:6px;padding:6px 10px;background:#0f1923;border-radius:3px;font-size:12px;color:#78909c;word-break:break-all;">请求: ' + this.escapeHtml(data.request) + '</div>';
if (data.analysis) detailsHtml += '<div style="margin-top:6px;padding:6px 10px;background:#0f1923;border-radius:3px;font-size:12px;color:#78909c;word-break:break-all;">分析: ' + this.escapeHtml(data.analysis) + '</div>';
if (data.tool) detailsHtml += '<div style="margin-top:6px;padding:6px 10px;background:#0f1923;border-radius:3px;font-size:12px;color:#78909c;word-break:break-all;">工具: ' + data.tool + (data.parameters ? ' 参数: ' + JSON.stringify(data.parameters) : '') + '</div>';
if (data.result) detailsHtml += '<div style="margin-top:6px;padding:6px 10px;background:#0f1923;border-radius:3px;font-size:12px;color:#78909c;word-break:break-all;max-height:100px;overflow-y:auto;">结果: ' + this.escapeHtml(String(data.result).substring(0, 500)) + '</div>';
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 = '<div style="position:relative;margin-bottom:10px;padding-left:20px;">' +
'<div style="position:absolute;left:4px;top:10px;width:10px;height:10px;border-radius:50%;border:2px solid #' + dotColor + ';background:' + dotBg + '"></div>' +
'<div style="background:#1a2733;border:1px solid #2d4a5e;border-radius:6px;padding:10px 14px;">' +
'<div style="display:flex;align-items:center;justify-content:space-between;margin-bottom:4px;">' +
'<span style="font-size:10px;padding:2px 6px;border-radius:3px;font-weight:600;text-transform:uppercase;background:' + badgeBg + ';color:' + badgeColor + '">' + typeLabel + '</span>' +
'<span style="font-size:10px;color:#546e7a;">' + timeStr + '</span></div>' +
'<div style="font-size:13px;color:#b0bec5;line-height:1.5;">' + this.escapeHtml(message) + '</div>' +
detailsHtml + '</div></div>';
container.el.insertAdjacentHTML('beforeend', stepHtml);
var currentWidget = bricks.app.find_widget_by_id('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.app.find_widget_by_id('reasoning_input');
if (!input || !input.options.text.trim()) {
bricks.show_error_message && bricks.show_error_message('请输入推理请求');
return;
}
var btn = bricks.app.find_widget_by_id('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.app.find_widget_by_id('steps_container');
if (container) container.el.innerHTML = '';
reasoningWS.stepCount = 0;
var currentWidget = bricks.app.find_widget_by_id('current_step');
if (currentWidget) currentWidget.set_text('');
var btn = bricks.app.find_widget_by_id('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;
})();