bep40 commited on
Commit
95ff900
·
verified ·
1 Parent(s): 905f24b

Upload ai_runtime_patch_fast.py

Browse files
Files changed (1) hide show
  1. ai_runtime_patch_fast.py +49 -1
ai_runtime_patch_fast.py CHANGED
@@ -138,6 +138,54 @@ button[onclick*="rewriteCurrentArticle"]{display:none!important}
138
  function esc(s){return String(s||'').replace(/[&<>"']/g,m=>({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[m]));}
139
  fetch('/api/storage_status').then(r=>r.json()).then(j=>{if(!j.persistent){let h=document.getElementById('view-home');if(h){let w=document.createElement('div');w.className='storage-warn';w.innerHTML='⚠️ <b>Persistent Storage chưa bật.</b> Bật: Space Settings → Persistent Storage → Small.';h.prepend(w);}}});
140
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141
  // === Short AI Slide on homepage (same as Dantri shorts) ===
142
  async function renderShortAISlide(){let home=document.getElementById('view-home');if(!home)return;document.getElementById('short-ai-final-slide')?.remove();let wall=(await fetch('/api/ai_wall').then(r=>r.json()).catch(()=>({posts:[]}))).posts||[];let vids=wall.filter(p=>p.video);if(!vids.length)return;let wrap=document.createElement('div');wrap.id='short-ai-final-slide';wrap.className='slider-wrap';wrap.innerHTML='<div class="slider-header"><span class="slider-label">🎬 Short AI</span></div><div class="slider-track">'+vids.slice(0,30).map((p,i)=>`<div class="slider-item shorts-item" onclick="openAIShortFeed(${i})"><div class="slider-thumb shorts-thumb"><video src="${p.video}" muted preload="metadata" style="width:100%;height:100%;object-fit:cover"></video><div class="card-play">▶</div></div><div class="slider-title">${esc(p.title)}</div></div>`).join('')+'</div>';let comp=home.querySelector('.ai-compose');if(comp&&comp.nextSibling)comp.parentNode.insertBefore(wrap,comp.nextSibling);else home.prepend(wrap);}
143
  setTimeout(renderShortAISlide,2500);
@@ -185,4 +233,4 @@ async def _index():
185
  body=getattr(rt.old,'PATCH_INJECT','')+f5.f4.f3.f2.f1.FINAL_INJECT+f5.f4.f3.FINAL3_INJECT+f5.f4.FINAL4_INJECT+f5.FINAL5_INJECT
186
  body+=getattr(f6,'FINAL6_INJECT','');body+=getattr(f6,'FINAL6_FAST_HOME_INJECT','');body+=getattr(f6,'FINAL6E_INJECT','')
187
  body+=PATCH_INJECT
188
- return HTMLResponse(html.replace('</body>',body+'\n</body>') if '</body>' in html else html+body)
 
138
  function esc(s){return String(s||'').replace(/[&<>"']/g,m=>({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[m]));}
139
  fetch('/api/storage_status').then(r=>r.json()).then(j=>{if(!j.persistent){let h=document.getElementById('view-home');if(h){let w=document.createElement('div');w.className='storage-warn';w.innerHTML='⚠️ <b>Persistent Storage chưa bật.</b> Bật: Space Settings → Persistent Storage → Small.';h.prepend(w);}}});
140
 
141
+ // === SSE Auto-update listener ===
142
+ let _sseSource = null;
143
+ function connectSSE(){
144
+ try{
145
+ _sseSource = new EventSource('/api/events');
146
+ _sseSource.onmessage = function(e){
147
+ try{
148
+ const data = JSON.parse(e.data);
149
+ if(data.type==='new_post' || data.type==='new_short'){
150
+ console.log('[SSE] Update received:', data.type);
151
+ fetch('/api/ai_wall').then(r=>r.json()).then(j=>{
152
+ _wallPosts = j.posts || [];
153
+ if(typeof renderShortAISlide==='function') renderShortAISlide();
154
+ const track=document.getElementById('ai-wall-track');
155
+ if(track && _wallPosts.length){
156
+ track.innerHTML = _wallPosts.slice(0,20).map((p,i)=>makeWallItem(p,i)).join('');
157
+ }
158
+ });
159
+ }
160
+ }catch(err){}
161
+ };
162
+ _sseSource.onerror = function(){
163
+ setTimeout(connectSSE, 5000);
164
+ };
165
+ }catch(err){}
166
+ }
167
+
168
+ // Fallback polling (30s)
169
+ let _lastWallLen = 0;
170
+ setInterval(function(){
171
+ if(typeof _wallPosts==='undefined') return;
172
+ fetch('/api/ai_wall').then(r=>r.json()).then(j=>{
173
+ const wall = j.posts || [];
174
+ if(wall.length !== _lastWallLen){
175
+ _lastWallLen = wall.length;
176
+ _wallPosts = wall;
177
+ const track=document.getElementById('ai-wall-track');
178
+ if(track && wall.length){
179
+ track.innerHTML = wall.slice(0,20).map((p,i)=>makeWallItem(p,i)).join('');
180
+ }
181
+ if(typeof renderShortAISlide==='function') renderShortAISlide();
182
+ }
183
+ }).catch(()=>{});
184
+ }, 30000);
185
+
186
+ // Start SSE
187
+ connectSSE();
188
+
189
  // === Short AI Slide on homepage (same as Dantri shorts) ===
190
  async function renderShortAISlide(){let home=document.getElementById('view-home');if(!home)return;document.getElementById('short-ai-final-slide')?.remove();let wall=(await fetch('/api/ai_wall').then(r=>r.json()).catch(()=>({posts:[]}))).posts||[];let vids=wall.filter(p=>p.video);if(!vids.length)return;let wrap=document.createElement('div');wrap.id='short-ai-final-slide';wrap.className='slider-wrap';wrap.innerHTML='<div class="slider-header"><span class="slider-label">🎬 Short AI</span></div><div class="slider-track">'+vids.slice(0,30).map((p,i)=>`<div class="slider-item shorts-item" onclick="openAIShortFeed(${i})"><div class="slider-thumb shorts-thumb"><video src="${p.video}" muted preload="metadata" style="width:100%;height:100%;object-fit:cover"></video><div class="card-play">▶</div></div><div class="slider-title">${esc(p.title)}</div></div>`).join('')+'</div>';let comp=home.querySelector('.ai-compose');if(comp&&comp.nextSibling)comp.parentNode.insertBefore(wrap,comp.nextSibling);else home.prepend(wrap);}
191
  setTimeout(renderShortAISlide,2500);
 
233
  body=getattr(rt.old,'PATCH_INJECT','')+f5.f4.f3.f2.f1.FINAL_INJECT+f5.f4.f3.FINAL3_INJECT+f5.f4.FINAL4_INJECT+f5.FINAL5_INJECT
234
  body+=getattr(f6,'FINAL6_INJECT','');body+=getattr(f6,'FINAL6_FAST_HOME_INJECT','');body+=getattr(f6,'FINAL6E_INJECT','')
235
  body+=PATCH_INJECT
236
+ return HTMLResponse(html.replace('</body>',body+'\n</body>') if '</body>' in html else html+body)