Spaces:
Running
Running
Chat Fix v9: Maintain conversation context — remember last discussed product across messages
Browse files- chat-fix.js +93 -119
chat-fix.js
CHANGED
|
@@ -1,9 +1,13 @@
|
|
| 1 |
/**
|
| 2 |
-
* V.AI STUDIO — Chat Fix
|
| 3 |
-
*
|
| 4 |
-
*
|
| 5 |
-
*
|
| 6 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
* 5. Vision AI for design questions
|
| 8 |
*/
|
| 9 |
(function(){
|
|
@@ -12,6 +16,7 @@ var VISION_MODEL='meta-llama/Llama-4-Scout-17B-16E-Instruct';
|
|
| 12 |
var PROCESSED='data-vc';
|
| 13 |
var _currentCategory='';
|
| 14 |
var _currentProductSlug='';
|
|
|
|
| 15 |
|
| 16 |
function norm(s){return(s||'').normalize('NFD').replace(/[\u0300-\u036f]/g,'').replace(/[Đđ]/g,'d').toLowerCase();}
|
| 17 |
function fmt(n){if(!n||isNaN(n))return'LH';return Number(n).toLocaleString('vi-VN')+'đ';}
|
|
@@ -96,9 +101,6 @@ function injectLoop(){
|
|
| 96 |
}
|
| 97 |
}
|
| 98 |
|
| 99 |
-
/**
|
| 100 |
-
* Auto-fill chat when opened from product page — includes link
|
| 101 |
-
*/
|
| 102 |
function autoFillFromProductPage(){
|
| 103 |
var url=window.location.pathname;
|
| 104 |
var slugMatch=url.match(/\/san-pham\/([^/]+)/);
|
|
@@ -119,7 +121,7 @@ function autoFillFromProductPage(){
|
|
| 119 |
input.value='Tư vấn SP '+(sku||name.substring(0,25))+' '+link;
|
| 120 |
input.setAttribute('data-autofilled','1');
|
| 121 |
_currentCategory=detectCategory(name)||'';
|
| 122 |
-
//
|
| 123 |
input.dispatchEvent(new Event('input',{bubbles:true}));
|
| 124 |
clearInterval(t);return;
|
| 125 |
}
|
|
@@ -127,28 +129,17 @@ function autoFillFromProductPage(){
|
|
| 127 |
},1000);
|
| 128 |
}
|
| 129 |
|
| 130 |
-
/**
|
| 131 |
-
* SEARCH DROPDOWN: Show instant results below search bar
|
| 132 |
-
* Priority: exact SKU match > title match
|
| 133 |
-
* Include "Add to order" button
|
| 134 |
-
*/
|
| 135 |
function initSearchDropdown(){
|
| 136 |
-
// Find the main search input on the page
|
| 137 |
var searchInput=document.querySelector('#searchInput,input[type="search"],.search-input,input[placeholder*="Tìm"],input[placeholder*="tìm"]');
|
| 138 |
if(!searchInput)return;
|
| 139 |
if(searchInput.getAttribute('data-dropdown-init'))return;
|
| 140 |
searchInput.setAttribute('data-dropdown-init','1');
|
| 141 |
-
|
| 142 |
-
// Create dropdown container
|
| 143 |
var dropdown=document.createElement('div');
|
| 144 |
dropdown.id='vai-search-dropdown';
|
| 145 |
dropdown.style.cssText='position:absolute;left:0;right:0;top:100%;background:#fff;border:1px solid #e2e8f0;border-radius:0 0 12px 12px;box-shadow:0 8px 24px rgba(0,0,0,.12);max-height:400px;overflow-y:auto;z-index:9999;display:none;font-family:Inter,sans-serif';
|
| 146 |
-
|
| 147 |
-
// Make search input container relative
|
| 148 |
var parent=searchInput.parentElement;
|
| 149 |
if(parent)parent.style.position='relative';
|
| 150 |
if(parent)parent.appendChild(dropdown);
|
| 151 |
-
|
| 152 |
var debounceTimer=null;
|
| 153 |
searchInput.addEventListener('input',function(){
|
| 154 |
clearTimeout(debounceTimer);
|
|
@@ -156,110 +147,30 @@ function initSearchDropdown(){
|
|
| 156 |
if(q.length<2){dropdown.style.display='none';return;}
|
| 157 |
debounceTimer=setTimeout(function(){showResults(q);},250);
|
| 158 |
});
|
| 159 |
-
searchInput.addEventListener('focus',function(){
|
| 160 |
-
|
| 161 |
-
});
|
| 162 |
-
document.addEventListener('click',function(e){
|
| 163 |
-
if(!dropdown.contains(e.target)&&e.target!==searchInput)dropdown.style.display='none';
|
| 164 |
-
});
|
| 165 |
-
|
| 166 |
function showResults(query){
|
| 167 |
if(typeof D==='undefined'||!D||!D.length){dropdown.style.display='none';return;}
|
| 168 |
var q=norm(query).replace(/[.\-_ ]/g,'');
|
| 169 |
var words=norm(query).split(/\s+/).filter(function(w){return w.length>=2;});
|
| 170 |
if(!words.length){dropdown.style.display='none';return;}
|
| 171 |
-
|
| 172 |
-
var results=[];var seen={};
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
var p=D[i];if(!p)continue;
|
| 176 |
-
var sku=(p.sku||p.model||p.mod||'').replace(/[.\-_ ]/g,'').toLowerCase();
|
| 177 |
-
var slug=p.slug||'';if(seen[slug])continue;
|
| 178 |
-
if(sku&&sku.length>2&&(q===sku||sku.includes(q)||q.includes(sku))){
|
| 179 |
-
seen[slug]=true;results.push({p:p,score:200});
|
| 180 |
-
}
|
| 181 |
-
}
|
| 182 |
-
// Pass 2: Title/name keyword match
|
| 183 |
-
for(var j=0;j<D.length&&results.length<20;j++){
|
| 184 |
-
var p2=D[j];if(!p2)continue;
|
| 185 |
-
var slug2=p2.slug||'';if(seen[slug2])continue;
|
| 186 |
-
var nameN=norm(p2.name||p2.n||'');
|
| 187 |
-
var score=0;
|
| 188 |
-
for(var w=0;w<words.length;w++){if(nameN.includes(words[w]))score+=10;}
|
| 189 |
-
if(score>0){seen[slug2]=true;results.push({p:p2,score:score});}
|
| 190 |
-
}
|
| 191 |
-
results.sort(function(a,b){return b.score-a.score;});
|
| 192 |
-
results=results.slice(0,8);
|
| 193 |
-
|
| 194 |
if(!results.length){dropdown.style.display='none';return;}
|
| 195 |
-
|
| 196 |
var html='';
|
| 197 |
-
results.forEach(function(r){
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
var sku2=p.sku||p.model||p.mod||'';
|
| 201 |
-
var price=p.price||p.p||'LH';
|
| 202 |
-
var img=p.image||p.img||p.i||'';
|
| 203 |
-
var slug3=p.slug||'';
|
| 204 |
-
html+='<div class="vai-dd-item" style="padding:8px 12px;border-bottom:1px solid #f1f5f9;display:flex;align-items:center;gap:10px;cursor:pointer" data-slug="'+slug3+'">';
|
| 205 |
-
if(img)html+='<img src="'+img+'" style="width:40px;height:40px;object-fit:contain;border-radius:4px;flex-shrink:0" onerror="this.style.display=\'none\'">';
|
| 206 |
-
html+='<div style="flex:1;overflow:hidden"><div style="font-size:12px;font-weight:600;color:#003f62;white-space:nowrap;overflow:hidden;text-overflow:ellipsis">'+name.substring(0,50)+'</div><div style="font-size:10px;color:#64748b">'+sku2+(p.brand?' • '+p.brand:'')+'</div></div>';
|
| 207 |
-
html+='<div style="text-align:right;flex-shrink:0"><div style="font-size:11px;font-weight:700;color:#059669">'+price+'</div><button class="vai-dd-add" data-slug="'+slug3+'" style="margin-top:3px;font-size:9px;padding:3px 8px;background:#003f62;color:#fff;border:none;border-radius:4px;cursor:pointer;white-space:nowrap">+ Đơn</button></div></div>';
|
| 208 |
-
});
|
| 209 |
-
dropdown.innerHTML=html;
|
| 210 |
-
dropdown.style.display='block';
|
| 211 |
-
|
| 212 |
-
// Click item → go to product page
|
| 213 |
-
dropdown.querySelectorAll('.vai-dd-item').forEach(function(item){
|
| 214 |
-
item.addEventListener('click',function(e){
|
| 215 |
-
if(e.target.classList.contains('vai-dd-add'))return;
|
| 216 |
-
var sl=this.getAttribute('data-slug');
|
| 217 |
-
if(sl)window.location.href='/san-pham/'+sl+'/index.html';
|
| 218 |
-
dropdown.style.display='none';
|
| 219 |
-
});
|
| 220 |
-
});
|
| 221 |
-
|
| 222 |
-
// "Add to order" button
|
| 223 |
-
dropdown.querySelectorAll('.vai-dd-add').forEach(function(btn){
|
| 224 |
-
btn.addEventListener('click',function(e){
|
| 225 |
-
e.stopPropagation();
|
| 226 |
-
var sl=this.getAttribute('data-slug');
|
| 227 |
-
if(!sl)return;
|
| 228 |
-
// Find product and add to saved order
|
| 229 |
-
for(var k=0;k<D.length;k++){
|
| 230 |
-
if(D[k]&&D[k].slug===sl){
|
| 231 |
-
var prod=D[k];
|
| 232 |
-
// Use VAI_ORDERS if available
|
| 233 |
-
if(window.VAI_ORDERS&&window.VAI_ORDERS.getAll){
|
| 234 |
-
var orders=window.VAI_ORDERS.getAll();
|
| 235 |
-
if(orders.length>0){
|
| 236 |
-
// Add to most recent order
|
| 237 |
-
var latest=orders[0];
|
| 238 |
-
var name3=prod.name||prod.n||'';
|
| 239 |
-
var price3=prod.priceNum||prod.pn||0;
|
| 240 |
-
if(!price3)price3=parseInt((prod.price||prod.p||'').toString().replace(/[^\d]/g,''))||0;
|
| 241 |
-
latest.items=latest.items||[];
|
| 242 |
-
latest.items.push({name:name3,model:prod.sku||prod.model||prod.mod||'',image:prod.image||prod.img||prod.i||'',price:price3,discPrice:price3,qty:1,total:price3,note:''});
|
| 243 |
-
latest.grandTotal=(latest.grandTotal||0)+price3;
|
| 244 |
-
latest.savedAt=new Date().toISOString();
|
| 245 |
-
window.VAI_ORDERS.add(latest);
|
| 246 |
-
this.textContent='✓ Đã thêm';this.style.background='#059669';
|
| 247 |
-
}else{
|
| 248 |
-
this.textContent='Chưa có đơn';this.style.background='#94a3b8';
|
| 249 |
-
}
|
| 250 |
-
}else{
|
| 251 |
-
this.textContent='N/A';this.style.background='#94a3b8';
|
| 252 |
-
}
|
| 253 |
-
break;
|
| 254 |
-
}
|
| 255 |
-
}
|
| 256 |
-
});
|
| 257 |
-
});
|
| 258 |
}
|
| 259 |
}
|
| 260 |
|
| 261 |
function fixLinks(){document.addEventListener('click',function(e){var a=e.target.closest('a[href]');if(a&&a.href&&a.href.includes('/san-pham/')){a.target='_blank';a.rel='noopener';}},true);}
|
| 262 |
|
|
|
|
|
|
|
|
|
|
| 263 |
function hookAI(){
|
| 264 |
var origFetch=window.fetch;
|
| 265 |
window.fetch=function(url,options){
|
|
@@ -269,13 +180,77 @@ function hookAI(){
|
|
| 269 |
if(body.messages&&Array.isArray(body.messages)){
|
| 270 |
var userMsg=body.messages.filter(function(m){return m.role==='user';}).pop();
|
| 271 |
var userText=typeof userMsg.content==='string'?userMsg.content:(userMsg.content&&userMsg.content[0]?userMsg.content[0].text:'');
|
|
|
|
|
|
|
| 272 |
var product=null;
|
| 273 |
-
if(typeof D!=='undefined'&&D.length){
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 277 |
var sys=body.messages.find(function(m){return m.role==='system';});
|
| 278 |
-
if(sys){if(typeof sys.content==='string')sys.content+=strict;}
|
|
|
|
|
|
|
| 279 |
options.body=JSON.stringify(body);
|
| 280 |
}
|
| 281 |
}catch(e){}
|
|
@@ -287,12 +262,11 @@ function hookAI(){
|
|
| 287 |
function init(){
|
| 288 |
fixLinks();hookAI();autoFillFromProductPage();
|
| 289 |
setInterval(injectLoop,1500);
|
| 290 |
-
// Init search dropdown when D is ready
|
| 291 |
var ddTimer=setInterval(function(){
|
| 292 |
if(typeof D!=='undefined'&&D.length>100){clearInterval(ddTimer);initSearchDropdown();}
|
| 293 |
},2000);
|
| 294 |
setTimeout(function(){clearInterval(ddTimer);initSearchDropdown();},15000);
|
| 295 |
}
|
| 296 |
if(document.readyState==='loading')document.addEventListener('DOMContentLoaded',init);else init();
|
| 297 |
-
console.log('✅ Chat Fix
|
| 298 |
})();
|
|
|
|
| 1 |
/**
|
| 2 |
+
* V.AI STUDIO — Chat Fix v9
|
| 3 |
+
*
|
| 4 |
+
* FIX: Lưu ngữ cảnh SP đang tư vấn xuyên suốt cuộc trò chuyện.
|
| 5 |
+
* Khi user hỏi tiếp mà ko nhắc SP → dùng SP trước đó.
|
| 6 |
+
*
|
| 7 |
+
* 1. Cards 100% on every response
|
| 8 |
+
* 2. Chat from product page → auto-fill
|
| 9 |
+
* 3. Search dropdown
|
| 10 |
+
* 4. Strict no-hallucination AI + CONTEXT MEMORY
|
| 11 |
* 5. Vision AI for design questions
|
| 12 |
*/
|
| 13 |
(function(){
|
|
|
|
| 16 |
var PROCESSED='data-vc';
|
| 17 |
var _currentCategory='';
|
| 18 |
var _currentProductSlug='';
|
| 19 |
+
var _lastProduct=null; // GIỮ SP ĐANG TƯ VẤN
|
| 20 |
|
| 21 |
function norm(s){return(s||'').normalize('NFD').replace(/[\u0300-\u036f]/g,'').replace(/[Đđ]/g,'d').toLowerCase();}
|
| 22 |
function fmt(n){if(!n||isNaN(n))return'LH';return Number(n).toLocaleString('vi-VN')+'đ';}
|
|
|
|
| 101 |
}
|
| 102 |
}
|
| 103 |
|
|
|
|
|
|
|
|
|
|
| 104 |
function autoFillFromProductPage(){
|
| 105 |
var url=window.location.pathname;
|
| 106 |
var slugMatch=url.match(/\/san-pham\/([^/]+)/);
|
|
|
|
| 121 |
input.value='Tư vấn SP '+(sku||name.substring(0,25))+' '+link;
|
| 122 |
input.setAttribute('data-autofilled','1');
|
| 123 |
_currentCategory=detectCategory(name)||'';
|
| 124 |
+
_lastProduct=p; // Lưu context SP
|
| 125 |
input.dispatchEvent(new Event('input',{bubbles:true}));
|
| 126 |
clearInterval(t);return;
|
| 127 |
}
|
|
|
|
| 129 |
},1000);
|
| 130 |
}
|
| 131 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
function initSearchDropdown(){
|
|
|
|
| 133 |
var searchInput=document.querySelector('#searchInput,input[type="search"],.search-input,input[placeholder*="Tìm"],input[placeholder*="tìm"]');
|
| 134 |
if(!searchInput)return;
|
| 135 |
if(searchInput.getAttribute('data-dropdown-init'))return;
|
| 136 |
searchInput.setAttribute('data-dropdown-init','1');
|
|
|
|
|
|
|
| 137 |
var dropdown=document.createElement('div');
|
| 138 |
dropdown.id='vai-search-dropdown';
|
| 139 |
dropdown.style.cssText='position:absolute;left:0;right:0;top:100%;background:#fff;border:1px solid #e2e8f0;border-radius:0 0 12px 12px;box-shadow:0 8px 24px rgba(0,0,0,.12);max-height:400px;overflow-y:auto;z-index:9999;display:none;font-family:Inter,sans-serif';
|
|
|
|
|
|
|
| 140 |
var parent=searchInput.parentElement;
|
| 141 |
if(parent)parent.style.position='relative';
|
| 142 |
if(parent)parent.appendChild(dropdown);
|
|
|
|
| 143 |
var debounceTimer=null;
|
| 144 |
searchInput.addEventListener('input',function(){
|
| 145 |
clearTimeout(debounceTimer);
|
|
|
|
| 147 |
if(q.length<2){dropdown.style.display='none';return;}
|
| 148 |
debounceTimer=setTimeout(function(){showResults(q);},250);
|
| 149 |
});
|
| 150 |
+
searchInput.addEventListener('focus',function(){if(this.value.trim().length>=2)showResults(this.value.trim());});
|
| 151 |
+
document.addEventListener('click',function(e){if(!dropdown.contains(e.target)&&e.target!==searchInput)dropdown.style.display='none';});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
function showResults(query){
|
| 153 |
if(typeof D==='undefined'||!D||!D.length){dropdown.style.display='none';return;}
|
| 154 |
var q=norm(query).replace(/[.\-_ ]/g,'');
|
| 155 |
var words=norm(query).split(/\s+/).filter(function(w){return w.length>=2;});
|
| 156 |
if(!words.length){dropdown.style.display='none';return;}
|
| 157 |
+
var results=[],seen={};
|
| 158 |
+
for(var i=0;i<D.length&&results.length<20;i++){var p=D[i];if(!p)continue;var sku=(p.sku||p.model||p.mod||'').replace(/[.\-_ ]/g,'').toLowerCase();var slug=p.slug||'';if(seen[slug])continue;if(sku&&sku.length>2&&(q===sku||sku.includes(q)||q.includes(sku))){seen[slug]=true;results.push({p:p,score:200});}}
|
| 159 |
+
for(var j=0;j<D.length&&results.length<20;j++){var p2=D[j];if(!p2)continue;var slug2=p2.slug||'';if(seen[slug2])continue;var nameN=norm(p2.name||p2.n||'');var score=0;for(var w=0;w<words.length;w++){if(nameN.includes(words[w]))score+=10;}if(score>0){seen[slug2]=true;results.push({p:p2,score:score});}}
|
| 160 |
+
results.sort(function(a,b){return b.score-a.score;});results=results.slice(0,8);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
if(!results.length){dropdown.style.display='none';return;}
|
|
|
|
| 162 |
var html='';
|
| 163 |
+
results.forEach(function(r){var p=r.p;var name=p.name||p.n||'';var sku2=p.sku||p.model||p.mod||'';var price=p.price||p.p||'LH';var img=p.image||p.img||p.i||'';var slug3=p.slug||'';html+='<div class="vai-dd-item" style="padding:8px 12px;border-bottom:1px solid #f1f5f9;display:flex;align-items:center;gap:10px;cursor:pointer" data-slug="'+slug3+'"><div style="flex:1;overflow:hidden"><div style="font-size:12px;font-weight:600;color:#003f62;white-space:nowrap;overflow:hidden;text-overflow:ellipsis">'+name.substring(0,50)+'</div><div style="font-size:10px;color:#64748b">'+sku2+(p.brand?' • '+p.brand:'')+'</div></div><div style="font-size:11px;font-weight:700;color:#059669">'+price+'</div></div>';});
|
| 164 |
+
dropdown.innerHTML=html;dropdown.style.display='block';
|
| 165 |
+
dropdown.querySelectorAll('.vai-dd-item').forEach(function(item){item.addEventListener('click',function(){var sl=this.getAttribute('data-slug');if(sl)window.location.href='/san-pham/'+sl+'/index.html';dropdown.style.display='none';});});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
}
|
| 167 |
}
|
| 168 |
|
| 169 |
function fixLinks(){document.addEventListener('click',function(e){var a=e.target.closest('a[href]');if(a&&a.href&&a.href.includes('/san-pham/')){a.target='_blank';a.rel='noopener';}},true);}
|
| 170 |
|
| 171 |
+
/**
|
| 172 |
+
* hookAI — v9: LƯU CONTEXT SP XUYÊN SUỐT CUỘC TRÒ CHUYỆN
|
| 173 |
+
*/
|
| 174 |
function hookAI(){
|
| 175 |
var origFetch=window.fetch;
|
| 176 |
window.fetch=function(url,options){
|
|
|
|
| 180 |
if(body.messages&&Array.isArray(body.messages)){
|
| 181 |
var userMsg=body.messages.filter(function(m){return m.role==='user';}).pop();
|
| 182 |
var userText=typeof userMsg.content==='string'?userMsg.content:(userMsg.content&&userMsg.content[0]?userMsg.content[0].text:'');
|
| 183 |
+
|
| 184 |
+
// Detect SP từ TIN NHẮN HIỆN TẠI
|
| 185 |
var product=null;
|
| 186 |
+
if(typeof D!=='undefined'&&D.length){
|
| 187 |
+
var q=norm(userText).replace(/[.\-_ ]/g,'');
|
| 188 |
+
for(var i=0;i<D.length;i++){
|
| 189 |
+
var p=D[i];
|
| 190 |
+
var sku=(p.sku||p.model||p.mod||'').replace(/[.\-_ ]/g,'').toLowerCase();
|
| 191 |
+
if(sku&&sku.length>3&&q.includes(sku)){product=p;break;}
|
| 192 |
+
}
|
| 193 |
+
// Thử match bằng tên SP (ít nhất 8 ký tự liên tiếp)
|
| 194 |
+
if(!product){
|
| 195 |
+
var qn=norm(userText);
|
| 196 |
+
for(var j=0;j<D.length;j++){
|
| 197 |
+
var p2=D[j];
|
| 198 |
+
var nameN=norm(p2.name||p2.n||'');
|
| 199 |
+
if(nameN.length>8&&qn.includes(nameN)){product=p2;break;}
|
| 200 |
+
}
|
| 201 |
+
}
|
| 202 |
+
}
|
| 203 |
+
|
| 204 |
+
// NẾU KHÔNG DETECT ĐƯỢC → DÙNG SP TRƯỚC ĐÓ (context)
|
| 205 |
+
if(product){
|
| 206 |
+
_lastProduct=product; // Cập nhật context
|
| 207 |
+
}else if(_lastProduct){
|
| 208 |
+
product=_lastProduct; // Dùng context cũ
|
| 209 |
+
}
|
| 210 |
+
|
| 211 |
+
// Cũng thử detect từ TOÀN BỘ messages (conversation history)
|
| 212 |
+
if(!product&&body.messages.length>2){
|
| 213 |
+
for(var mi=body.messages.length-1;mi>=0&&!product;mi--){
|
| 214 |
+
var msg=body.messages[mi];
|
| 215 |
+
if(msg.role!=='user')continue;
|
| 216 |
+
var msgText=typeof msg.content==='string'?msg.content:(msg.content&&msg.content[0]?msg.content[0].text:'');
|
| 217 |
+
var mqn=norm(msgText).replace(/[.\-_ ]/g,'');
|
| 218 |
+
if(typeof D!=='undefined'){
|
| 219 |
+
for(var k=0;k<D.length;k++){
|
| 220 |
+
var p3=D[k];
|
| 221 |
+
var sku3=(p3.sku||p3.model||p3.mod||'').replace(/[.\-_ ]/g,'').toLowerCase();
|
| 222 |
+
if(sku3&&sku3.length>3&&mqn.includes(sku3)){product=p3;_lastProduct=p3;break;}
|
| 223 |
+
}
|
| 224 |
+
}
|
| 225 |
+
}
|
| 226 |
+
}
|
| 227 |
+
|
| 228 |
+
// Vision AI cho câu hỏi về thiết kế
|
| 229 |
+
if(product&&/thiết kế|kiểu dáng|màu sắc|design|phong cách|chất liệu/i.test(userText)){
|
| 230 |
+
var img=product.image||product.img||product.i||'';
|
| 231 |
+
if(img){body.model=VISION_MODEL;var lu=body.messages[body.messages.length-1];if(lu&&lu.role==='user'&&typeof lu.content==='string')lu.content=[{type:'text',text:lu.content},{type:'image_url',image_url:{url:img}}];}
|
| 232 |
+
}
|
| 233 |
+
|
| 234 |
+
// Build system prompt VỚI CONTEXT
|
| 235 |
+
var strict='\n\n[QUY TẮC]:\n- TUYỆT ĐỐI KHÔNG bịa thông số/tính năng/giá.\n- CHỈ dùng [DỮ LIỆU SP] bên dưới.\n- Nếu user hỏi tiếp về SP đã tư vấn → trả lời DỰA TRÊN DATA SP ĐÓ.\n- Không có data → "Em chưa có thông tin SP này ạ."';
|
| 236 |
+
|
| 237 |
+
if(product){
|
| 238 |
+
var specs=product.specs||{};var ss='';if(typeof specs==='object')for(var sk in specs)ss+=sk+':'+specs[sk]+'; ';
|
| 239 |
+
var feats=(product.feats||[]).slice(0,8).join('; ');
|
| 240 |
+
strict+='\n\n[DỮ LIỆU SP ĐANG TƯ VẤN]:\nTên: '+(product.name||product.n)+'\nGiá: '+(product.price||product.p||'LH')+'\nBrand: '+(product.brand||'')+'\nMã: '+(product.sku||product.mod||'');
|
| 241 |
+
if(ss)strict+='\nSpecs: '+ss;
|
| 242 |
+
if(feats)strict+='\nFeats: '+feats;
|
| 243 |
+
if(product.summary||product.sum)strict+='\nMô tả: '+(product.summary||product.sum||'').substring(0,400);
|
| 244 |
+
if(product.desc)strict+='\nChi tiết: '+(product.desc||'').substring(0,300);
|
| 245 |
+
strict+='\n\n→ Mọi câu hỏi tiếp theo của user (giá? bảo hành? kích thước? so sánh?) ĐỀU trả lời dựa trên SP trên.';
|
| 246 |
+
}else{
|
| 247 |
+
strict+='\n\n[KHÔNG TÌM THẤY SP → nói "Em chưa có thông tin SP này, anh/chị cho em mã SP hoặc tên cụ thể nhé ạ."]';
|
| 248 |
+
}
|
| 249 |
+
|
| 250 |
var sys=body.messages.find(function(m){return m.role==='system';});
|
| 251 |
+
if(sys){if(typeof sys.content==='string')sys.content+=strict;}
|
| 252 |
+
else body.messages.unshift({role:'system',content:'Bạn là tư vấn viên V.AI STUDIO — chuyên thiết bị bếp cao cấp. Xưng em, gọi khách là anh/chị. Trả lời ngắn gọn, chính xác.'+strict});
|
| 253 |
+
|
| 254 |
options.body=JSON.stringify(body);
|
| 255 |
}
|
| 256 |
}catch(e){}
|
|
|
|
| 262 |
function init(){
|
| 263 |
fixLinks();hookAI();autoFillFromProductPage();
|
| 264 |
setInterval(injectLoop,1500);
|
|
|
|
| 265 |
var ddTimer=setInterval(function(){
|
| 266 |
if(typeof D!=='undefined'&&D.length>100){clearInterval(ddTimer);initSearchDropdown();}
|
| 267 |
},2000);
|
| 268 |
setTimeout(function(){clearInterval(ddTimer);initSearchDropdown();},15000);
|
| 269 |
}
|
| 270 |
if(document.readyState==='loading')document.addEventListener('DOMContentLoaded',init);else init();
|
| 271 |
+
console.log('✅ Chat Fix v9: context memory + strict data + cards');
|
| 272 |
})();
|