class TokenOptimizer extends HTMLElement {
connectedCallback() {
this.attachShadow({ mode: 'open' });
this.shadowRoot.innerHTML = `
`;
// Initialize elements
const userInput = this.shadowRoot.getElementById('userInput');
const personaToggle = this.shadowRoot.getElementById('personaToggle');
const formatToggle = this.shadowRoot.getElementById('formatToggle');
const chainToggle = this.shadowRoot.getElementById('chainToggle');
const tempSlider = this.shadowRoot.getElementById('tempSlider');
const tempValue = this.shadowRoot.getElementById('tempValue');
const specSlider = this.shadowRoot.getElementById('specSlider');
const specValue = this.shadowRoot.getElementById('specValue');
const outputPanel = this.shadowRoot.getElementById('outputPanel');
const outputContent = this.shadowRoot.getElementById('outputContent');
// Update output when controls change
const updateOutput = () => {
const inputText = userInput.value || 'Wifi is broken';
const temp = parseFloat(tempSlider.value);
const spec = parseInt(specSlider.value);
// Update displayed values
tempValue.textContent = temp.toFixed(1);
specValue.textContent = ['Low', 'Medium', 'High'][spec];
// Generate output based on controls
let output = '';
if (personaToggle.checked) {
output += `[System Persona: Senior IT Support Engineer with 10+ years experience]\n\n`;
}
if (inputText.toLowerCase().includes('wifi')) {
if (spec === 0) {
output += `This appears to be a network connectivity issue. Try restarting your router and device.`;
} else if (spec === 1) {
output += `1. First, verify if other devices can connect to the same WiFi network\n`;
output += `2. Check if the router lights indicate normal operation (usually solid green)\n`;
output += `3. Try restarting both your device and the router\n`;
output += `4. If issue persists, check for IP address conflicts (ipconfig /release, /renew)`;
} else {
output += `1. [Triage] Confirm scope:\n - Single device or multiple affected?\n - Wired connections working?\n`;
output += `2. [Diagnostics]:\n - Run 'ping 8.8.8.8' to test basic connectivity\n - Check 'ipconfig /all' for valid DHCP lease\n`;
output += `3. [Resolution Paths]:\n - If DHCP issue: ipconfig /release && /renew\n - If DNS: try flushing cache (ipconfig /flushdns)\n`;
output += `4. [Escalation Points]:\n - Contact ISP if modem lights abnormal\n - Check enterprise RADIUS logs if applicable`;
}
} else {
output += `Please describe your IT issue in more detail for specific troubleshooting steps.`;
}
if (chainToggle.checked) {
output += `\n\n[Reasoning Chain]\n1. User reports "${inputText}"\n`;
output += `2. Most common causes for this symptom are...\n`;
output += `3. Recommended diagnostic steps prioritize quickest resolution first\n`;
output += `4. Final recommendation based on most probable root cause`;
}
if (formatToggle.checked) {
output = output.replace(/\n/g, '\n\n');
output = output.replace(/(\d+\.)/g, '\n$1');
}
// Apply highlighting based on specificity
outputContent.innerHTML = output;
const textNodes = outputContent.childNodes;
if (spec === 0) {
outputContent.innerHTML = output.replace(/(network|try|check|issue)/gi,
match => `${match}`);
} else if (spec === 2) {
outputContent.innerHTML = output.replace(/(Triage|Diagnostics|Resolution|Escalation|ipconfig|ping)/gi,
match => `${match}`);
}
// Check if optimized (all toggles on)
if (personaToggle.checked && formatToggle.checked && chainToggle.checked) {
outputPanel.classList.add('optimized');
} else {
outputPanel.classList.remove('optimized');
}
};
// Add event listeners
[userInput, personaToggle, formatToggle, chainToggle, tempSlider, specSlider].forEach(el => {
el.addEventListener('input', updateOutput);
el.addEventListener('change', updateOutput);
});
// Initial update
updateOutput();
}
}
customElements.define('token-optimizer', TokenOptimizer);