(function() { 'use strict'; function wdfKonumToWdfClass(k) { if (!k) return 'wdf-alt-sag'; if (k.indexOf('wdf-') === 0) return k; var m = { sag_ust:'wdf-ust-sag', sol_ust:'wdf-ust-sol', sag_orta:'wdf-orta-sag', sol_orta:'wdf-orta-sol', sag_alt:'wdf-alt-sag', sol_alt:'wdf-alt-sol' }; return m[k] || 'wdf-alt-sag'; } /** dugme.css: kutu golge + perspective; orta konumlarda sinif yok */ function wdfKonumTo3dClass(konum) { if (!konum) return ''; var m = { 'wdf-alt-sag': 'wdf-3d-alt-sag', 'wdf-alt-sol': 'wdf-3d-alt-sol', 'wdf-ust-sag': 'wdf-3d-ust-sag', 'wdf-ust-sol': 'wdf-3d-ust-sol' }; return m[konum] || ''; } function wdfSekilToClass(s) { if (!s) return 'wdf-sekil-yuvarlak'; if (s.indexOf('wdf-sekil-') === 0) { if (s === 'wdf-sekil-ucgen' || s === 'wdf-sekil-koseli') return 'wdf-sekil-yuvarlak'; return s; } return s === 'kare' ? 'wdf-sekil-kare' : 'wdf-sekil-yuvarlak'; } function wdfRenkIsHex(r) { return r && typeof r === 'string' && r.charAt(0) === '#' && /^#[0-9A-Fa-f]{6}$/.test(r); } if (!window._wdf_ai) { window._wdf_ai = {}; } /* async/defer ile yuklenince document.currentScript null olur; script etiketi DOM'dan bulunur */ var _wdfScriptAi = document.currentScript; if (!_wdfScriptAi || !_wdfScriptAi.getAttribute('data-id')) { var _scripts = document.getElementsByTagName('script'); for (var _si = _scripts.length - 1; _si >= 0; _si--) { if (_scripts[_si].src && _scripts[_si].src.indexOf('ai.js') !== -1 && _scripts[_si].getAttribute('data-id')) { _wdfScriptAi = _scripts[_si]; break; } } } var _wdfScriptSrc = _wdfScriptAi ? _wdfScriptAi.src : ''; _wdf_ai.siteId = _wdf_ai.siteId || (_wdfScriptAi ? _wdfScriptAi.getAttribute('data-id') : null); _wdf_ai.konumAttr = _wdfScriptAi ? _wdfScriptAi.getAttribute('data-konum') : ''; _wdf_ai.renkAttr = _wdfScriptAi ? _wdfScriptAi.getAttribute('data-renk') : ''; _wdf_ai.sekilAttr = _wdfScriptAi ? _wdfScriptAi.getAttribute('data-sekil') : ''; _wdf_ai.metinAttr = _wdfScriptAi ? _wdfScriptAi.getAttribute('data-metin') : ''; _wdf_ai.modAttr = _wdfScriptAi ? _wdfScriptAi.getAttribute('data-ai-mod') : ''; _wdf_ai.mfAttr = _wdfScriptAi ? _wdfScriptAi.getAttribute('data-mf') : ''; /** Ust-sag/sol yuvarlak sesli: cercevenin alt cizgisine gore dikey ortala (fixed widget) */ _wdf_ai._applyHeaderStraddle = function() { var div = document.getElementById('wdfAiWidget'); if (!div || _wdf_ai.mod !== 'sesli') return; var hh = _wdfScriptAi && _wdfScriptAi.getAttribute('data-header-h'); if (hh && /^\d+(\.\d+)?$/.test(String(hh).trim())) { div.style.setProperty('--wdf-header-alt', String(hh).trim() + 'px'); return; } var auto = _wdfScriptAi && (_wdfScriptAi.getAttribute('data-header-auto') === '1' || _wdfScriptAi.getAttribute('data-header-auto') === 'true'); if (!auto) return; var el = document.querySelector('header') || document.querySelector('.navbar') || document.querySelector('[role="banner"]'); if (!el) return; div.style.setProperty('--wdf-header-alt', el.getBoundingClientRect().bottom + 'px'); }; (function() { var idx = _wdfScriptSrc.indexOf('/usr/soket/'); if (idx >= 0) { _wdf_ai.baseUrl = _wdfScriptSrc.substring(0, idx); } else if (_wdfScriptSrc) { try { var u = new URL(_wdfScriptSrc, typeof window !== 'undefined' ? window.location.href : undefined); _wdf_ai.baseUrl = u.origin; } catch (e0) { _wdf_ai.baseUrl = ''; } } else { _wdf_ai.baseUrl = ''; } if (!_wdf_ai.baseUrl && typeof window !== 'undefined' && window.location && window.location.origin) { _wdf_ai.baseUrl = window.location.origin; } })(); _wdf_ai._isDebug = function() { return typeof window !== 'undefined' && window._WDF_AI_DEBUG === true; }; _wdf_ai.enableDebug = function() { if (typeof window !== 'undefined') window._WDF_AI_DEBUG = true; }; _wdf_ai.disableDebug = function() { if (typeof window !== 'undefined') window._WDF_AI_DEBUG = false; }; _wdf_ai._dbg = function() { if (!_wdf_ai._isDebug()) return; var a = ['[WDF-AI]'].concat(Array.prototype.slice.call(arguments)); console.log.apply(console, a); }; _wdf_ai._dbgErr = function() { if (!_wdf_ai._isDebug()) return; var a = ['[WDF-AI]'].concat(Array.prototype.slice.call(arguments)); console.error.apply(console, a); }; _wdf_ai._dbg('loader', 'script src=', _wdfScriptSrc || '(bos)', 'data-id=', _wdf_ai.siteId || '(yok)', 'currentScript=', document.currentScript ? 'var' : 'yok (async normal)'); _wdf_ai._dbg('loader', 'baseUrl=', _wdf_ai.baseUrl || '(bos)', 'sayfa=', typeof window !== 'undefined' && window.location ? window.location.href : ''); _wdf_ai._telScriptYukle = function(cb) { if (typeof window !== 'undefined' && window._wdfTel) { cb(); return; } var s = document.createElement('script'); s.src = (_wdf_ai.baseUrl || '') + '/usr/soket/wdf_tel.js?v=2'; s.onload = cb; s.onerror = function() { window._wdfTel = { mobilMi: function() { return false; }, contactPickerDestekleniyor: function() { return false; }, mobilNormalizeTr: function() { return ''; }, rehberSec: function() {} }; cb(); }; document.head.appendChild(s); }; _wdf_ai._pcm16 = function(float32Array) { var buffer = new ArrayBuffer(float32Array.length * 2); var view = new DataView(buffer); for (var i = 0; i < float32Array.length; i++) { var s = Math.max(-1, Math.min(1, float32Array[i])); view.setInt16(i * 2, s < 0 ? s * 0x8000 : s * 0x7fff, true); } return buffer; }; /** wdf-renk-hex: ornek btn-kapsul --k1-* (ornek_dg.css ile ayni degiskenler) */ _wdf_ai._ornekK1FromHex = function(el, hex) { if (!el || !hex || typeof hex !== 'string' || !/^#[0-9A-Fa-f]{6}$/.test(hex)) return; var r = parseInt(hex.slice(1, 3), 16); var g = parseInt(hex.slice(3, 5), 16); var b = parseInt(hex.slice(5, 7), 16); function mix(ch, t, u) { return Math.round(Math.min(255, Math.max(0, ch * t + u * (1 - t)))); } el.style.setProperty('--k1-mid', hex); el.style.setProperty('--k1-light', 'rgb(' + mix(r, 0.55, 255) + ',' + mix(g, 0.55, 255) + ',' + mix(b, 0.55, 255) + ')'); el.style.setProperty('--k1-dark', 'rgb(' + mix(r, 0.72, 0) + ',' + mix(g, 0.72, 0) + ',' + mix(b, 0.72, 0) + ')'); el.style.setProperty('--k1-lip', 'rgb(' + mix(r, 0.5, 0) + ',' + mix(g, 0.5, 0) + ',' + mix(b, 0.5, 0) + ')'); el.style.setProperty('--k1-sh1', 'rgba(' + r + ',' + g + ',' + b + ',0.5)'); el.style.setProperty('--k1-sh2', 'rgba(' + r + ',' + g + ',' + b + ',0.55)'); el.style.setProperty('--wdf-dalga-rgb', r + ',' + g + ',' + b); }; _wdf_ai._b64 = function(buffer) { var bytes = new Uint8Array(buffer); var binary = ''; for (var i = 0; i < bytes.byteLength; i++) { binary += String.fromCharCode(bytes[i]); } return btoa(binary); }; _wdf_ai._dalgaUpdate = function() { var div = document.getElementById('wdfAiWidget'); var dalga = document.getElementById('wdfAiDalga'); if (!div || !dalga || !div.classList.contains('wdf-ai-canli') || !_wdf_ai.analyser || !_wdf_ai.freqData) { _wdf_ai.rafId = null; return; } _wdf_ai.analyser.getByteFrequencyData(_wdf_ai.freqData); var bars = dalga.querySelectorAll('span'); var n = bars.length; var step = Math.max(1, Math.floor(_wdf_ai.freqData.length / n)); for (var i = 0; i < n; i++) { var sum = 0; for (var j = 0; j < step; j++) { sum += _wdf_ai.freqData[i * step + j] || 0; } var v = sum / step / 255; var h = 4 + Math.round(v * 22); bars[i].style.height = h + 'px'; } _wdf_ai.rafId = requestAnimationFrame(_wdf_ai._dalgaUpdate); }; _wdf_ai._telGateAiHtml = function() { return '
' + '
Cep numaranizi girin, SMS ile dogrulanacaktir. 6698 KVKK.
' + '' + '
' + '' + '' + '' + '' + '' + '
'; }; _wdf_ai._mobilNorm = function(s) { return (window._wdfTel && typeof window._wdfTel.mobilNormalizeTr === 'function') ? window._wdfTel.mobilNormalizeTr(s) : ''; }; _wdf_ai._normalizeAiIletisimFromUi = function() { var hid = document.getElementById('wdfAiIletisim'); var man = document.getElementById('wdfAiTelManuel'); if (!hid || !man || man.getAttribute('data-wdf-faz') === 'kod') return; var raw = ''; if (man.style.display !== 'none' && String(man.value).trim()) raw = String(man.value).trim(); else if (String(hid.value).trim()) raw = String(hid.value).trim(); var n = _wdf_ai._mobilNorm(raw); if (n) { hid.value = n; man.value = n; } }; _wdf_ai._otpAiNumaraModunaSifirla = function() { var tm = document.getElementById('wdfAiTelManuel'); var ka = document.getElementById('wdfAiKodAlani'); var kg = document.getElementById('wdfAiKodGonder'); if (tm) { tm.setAttribute('data-wdf-faz', 'numara'); tm.type = 'tel'; tm.value = ''; tm.placeholder = '05xx xxx xx xx'; tm.removeAttribute('maxlength'); tm.setAttribute('inputmode', 'numeric'); tm.setAttribute('autocomplete', 'tel'); tm.classList.remove('wdf-ai-inp-kod'); tm.classList.add('wdf-ai-inp-tel'); } if (ka) ka.style.display = 'none'; if (kg) kg.style.display = ''; _wdf_ai._otpSubmitting = false; if (_wdf_ai._otpKodTimer) { clearTimeout(_wdf_ai._otpKodTimer); _wdf_ai._otpKodTimer = null; } }; /** KVKK panelini kapat (mockup: Vazgec yerine X) */ _wdf_ai._sesliKvkkPanelKapat = function() { _wdf_ai._otpAiNumaraModunaSifirla(); var divI = document.getElementById('wdfAiWidget'); if (divI) divI.classList.remove('wdfaktif'); var dogI = document.getElementById('wdfAiDogrulama'); if (dogI) dogI.style.display = 'none'; var mesajI = document.getElementById('wdfAiMesaj'); if (mesajI) { mesajI.textContent = 'Sesli destek icin iletisim ve onay gereklidir. Hizmet baslatilmadi.'; mesajI.style.color = '#666'; } var nkI = document.getElementById('wdfAiNumaraKaynak'); if (nkI) nkI.value = ''; var hiI = document.getElementById('wdfAiIletisim'); if (hiI) hiI.value = ''; var tgI = document.getElementById('wdfAiTelGoster'); if (tgI) { tgI.textContent = 'Numara secilmedi'; tgI.classList.add('wdf-ai-tel-yok'); } var tmI = document.getElementById('wdfAiTelManuel'); if (tmI) tmI.value = ''; }; _wdf_ai._otpAiKodModunaGec = function() { var tm = document.getElementById('wdfAiTelManuel'); var ka = document.getElementById('wdfAiKodAlani'); var kg = document.getElementById('wdfAiKodGonder'); if (!tm) return; tm.setAttribute('data-wdf-faz', 'kod'); tm.type = 'text'; tm.value = ''; tm.placeholder = 'Dogrulama kodu'; tm.setAttribute('maxlength', '6'); tm.setAttribute('inputmode', 'numeric'); tm.setAttribute('autocomplete', 'one-time-code'); tm.classList.add('wdf-ai-inp-kod'); tm.classList.remove('wdf-ai-inp-tel'); tm.style.display = 'block'; if (ka) ka.style.display = 'block'; if (kg) kg.style.display = 'none'; try { tm.focus(); } catch (eF) { /* ignore */ } }; _wdf_ai._otpAiKodInputHandler = function() { var tm = document.getElementById('wdfAiTelManuel'); if (!tm || tm.getAttribute('data-wdf-faz') !== 'kod') return; var kod = String(tm.value).replace(/\D/g, ''); if (kod.length > 6) kod = kod.slice(0, 6); tm.value = kod; if (_wdf_ai._otpKodTimer) { clearTimeout(_wdf_ai._otpKodTimer); _wdf_ai._otpKodTimer = null; } if (kod.length === 6) { _wdf_ai._otpKodTimer = setTimeout(function() { _wdf_ai._otpAiKodDogrulaSubmit(); }, 280); } }; _wdf_ai._otpAiKodDogrulaSubmit = function() { _wdf_ai._otpAiKodDogrulaSubmitWithTik(); }; _wdf_ai._syncAiTelHidden = function() { var man = document.getElementById('wdfAiTelManuel'); var hid = document.getElementById('wdfAiIletisim'); if (!man || !hid || man.getAttribute('data-wdf-faz') === 'kod') return; if (man.style.display !== 'none' && String(man.value).trim()) { var n = _wdf_ai._mobilNorm(String(man.value).trim()); if (n) { man.value = n; hid.value = n; } else { hid.value = String(man.value).trim(); } } }; _wdf_ai._setupAiTelPanel = function() { _wdf_ai._sesliTelAlanlariniDoldur(); if (!window._wdfTel) return; var T = window._wdfTel; var tm = document.getElementById('wdfAiTelManuel'); var gosterim = document.getElementById('wdfAiTelGosterim'); var kvkk = document.getElementById('wdfAiKvkkOnay'); var btnSms = document.getElementById('wdfAiKodGonder'); var btnDogrula = document.getElementById('wdfAiKodDogrula'); var kodAlani = document.getElementById('wdfAiKodAlani'); var mesaj = document.getElementById('wdfAiMesaj'); var hid = document.getElementById('wdfAiIletisim'); if (!tm || !btnSms) return; var gecerli = false; var normDeger = ''; function kontrol() { if (tm.getAttribute('data-wdf-faz') === 'kod') { _wdf_ai._otpAiKodInputHandler(); return; } normDeger = T.mobilNormalizeTr(tm.value); if (tm.value.replace(/\D/g, '').length < 3) { if (gosterim) gosterim.textContent = ''; if (mesaj) mesaj.textContent = ''; gecerli = false; } else if (normDeger) { if (gosterim) gosterim.textContent = T.mobilGosterim ? T.mobilGosterim(normDeger) : ('+' + normDeger); if (mesaj) mesaj.textContent = ''; gecerli = true; } else { if (gosterim) gosterim.textContent = ''; if (mesaj) { mesaj.textContent = 'Gecerli bir cep telefonu numarasi girin'; mesaj.style.color = '#C52F32'; } gecerli = false; } var aktif = gecerli && kvkk && kvkk.checked; btnSms.disabled = !aktif; btnSms.style.opacity = aktif ? '1' : '0.5'; } tm.addEventListener('input', kontrol); if (kvkk) kvkk.addEventListener('change', kontrol); // SMS kodu gonder btnSms.addEventListener('click', function() { if (!gecerli || !kvkk || !kvkk.checked || !normDeger) return; btnSms.disabled = true; btnSms.textContent = 'Gonderiliyor...'; if (hid) hid.value = normDeger; var fd = new FormData(); fd.append('m', 'widget_dogrula'); fd.append('islem', 'kod_gonder'); fd.append('site_id', _wdf_ai.siteId); fd.append('iletisim', normDeger); fd.append('tel_gate', '1'); fd.append('kvkk_onay', '1'); fd.append('sesli_kvkk', '1'); fd.append('numara_kaynak', 'sms_dogrulama'); fd.append('hizmet', _wdf_ai.mod === 'sohbet' ? 'ai_sohbet' : 'ai_sesli'); fetch(_wdf_ai.baseUrl + '/index.php', { method: 'POST', body: fd, credentials: 'include' }) .then(function(r) { return r.text(); }) .then(function(t) { var p = new URLSearchParams(t); if (p.get('st') === '1') { _wdf_ai._otpAiKodModunaGec(); if (mesaj) { mesaj.textContent = 'Kod gonderildi'; mesaj.style.color = '#349E4A'; } } else { if (mesaj) { mesaj.textContent = p.get('msg') || 'Hata'; mesaj.style.color = '#C52F32'; } btnSms.disabled = false; btnSms.textContent = 'SMS kodu gonder'; btnSms.style.opacity = '1'; } }) .catch(function() { if (mesaj) { mesaj.textContent = 'Baglanti hatasi'; mesaj.style.color = '#C52F32'; } btnSms.disabled = false; btnSms.textContent = 'SMS kodu gonder'; btnSms.style.opacity = '1'; }); }); // Kod dogrula butonu if (btnDogrula) { btnDogrula.addEventListener('click', function() { _wdf_ai._otpAiKodDogrulaSubmitWithTik(); }); } }; /** OTP dogrulama sonrasi tik UPSERT + gorusme baslat */ _wdf_ai._otpAiKodDogrulaSubmitWithTik = function() { var tm = document.getElementById('wdfAiTelManuel'); if (!tm || tm.getAttribute('data-wdf-faz') !== 'kod' || _wdf_ai._otpSubmitting) return; var kod = String(tm.value).replace(/\D/g, ''); if (kod.length !== 6) return; _wdf_ai._otpSubmitting = true; var fd = new FormData(); fd.append('m', 'widget_dogrula'); fd.append('islem', 'kod_dogrula_ai'); fd.append('kod', kod); fd.append('site_id', _wdf_ai.siteId); fetch(_wdf_ai.baseUrl + '/index.php', { method: 'POST', body: fd, credentials: 'include' }) .then(function(r) { return r.text(); }) .then(function(t) { var p = new URLSearchParams(t); if (p.get('st') === '1') { var signedUrl = p.get('signed_url'); var hidOk = document.getElementById('wdfAiIletisim'); var telKayit = hidOk ? String(hidOk.value).trim() : ''; // tik tablosuna UPSERT var T = window._wdfTel; if (telKayit && T) { var fdTik = new FormData(); fdTik.append('m', 'widget_dogrula'); fdTik.append('islem', 'numara_kaydet'); fdTik.append('site_id', _wdf_ai.siteId); fdTik.append('telefon', telKayit); fdTik.append('kvkk_onay', '1'); fetch(_wdf_ai.baseUrl + '/index.php', { method: 'POST', body: fdTik, credentials: 'include' }) .then(function(r2) { return r2.text(); }) .then(function(t2) { var p2 = new URLSearchParams(t2); if (p2.get('st') === '1') { var token = p2.get('cerez_token') || ''; if (T.cerezYaz) T.cerezYaz(T.cerezAdi(_wdf_ai.siteId), token, 365); } }) .catch(function() {}); } _wdf_ai._sesliOnayKaydet(telKayit); var dogEl = document.getElementById('wdfAiDogrulama'); if (dogEl) dogEl.style.display = 'none'; var gorusmeEl = document.getElementById('wdfAiGorusme'); if (gorusmeEl) gorusmeEl.style.display = 'none'; var divW = document.getElementById('wdfAiWidget'); if (divW) divW.classList.remove('wdfaktif'); _wdf_ai._aiTalimat = p.get('ai_talimat') || ''; _wdf_ai._crmVar = p.get('crm_var') === '1'; _wdf_ai._crmAd = p.get('crm_ad') || ''; _wdf_ai._crmDetay = p.get('crm_detay') || ''; _wdf_ai._gorusmeBaslat(signedUrl); } else { var mesaj = document.getElementById('wdfAiMesaj'); _wdf_ai._sesliOnaySil(); if (mesaj) { mesaj.textContent = p.get('msg') || 'Gecersiz kod'; mesaj.style.color = '#C52F32'; } tm.value = ''; _wdf_ai._otpSubmitting = false; } }) .catch(function() { _wdf_ai._otpSubmitting = false; var mesajE = document.getElementById('wdfAiMesaj'); if (mesajE) { mesajE.textContent = 'Baglanti hatasi'; mesajE.style.color = '#C52F32'; } }); }; _wdf_ai.init = function() { var apiUrl = (_wdf_ai.baseUrl || '') + '/usr/api/index.php'; if (!_wdf_ai.siteId) { _wdf_ai._dbgErr('init iptal: data-id yok (script etiketi bulunamadi veya async sorunu)'); return; } if (!_wdf_ai.baseUrl) { _wdf_ai._dbgErr('init iptal: baseUrl bos'); return; } _wdf_ai._dbg('init', 'POST', apiUrl, 'site_id=', _wdf_ai.siteId); var fd = new FormData(); fd.append('islem', 'widget'); fd.append('site_id', _wdf_ai.siteId); fetch(apiUrl, { method: 'POST', body: fd }) .then(function(r) { _wdf_ai._dbg('widget API', 'http=', r.status, r.ok ? 'ok' : 'hata'); return r.text(); }) .then(function(t) { var ozet = t.length > 200 ? t.substring(0, 200) + '...' : t; _wdf_ai._dbg('widget API yanit', ozet); var p = new URLSearchParams(t); if (p.get('st') === '1') { _wdf_ai.renk = p.get('ai_renk') || _wdf_ai.renkAttr || '#349E4A'; _wdf_ai.konum = wdfKonumToWdfClass(p.get('ai_konum') || _wdf_ai.konumAttr || 'wdf-alt-sag'); _wdf_ai.sekil = wdfSekilToClass(p.get('ai_sekil') || _wdf_ai.sekilAttr || 'wdf-sekil-yuvarlak'); _wdf_ai.metin = _wdf_ai.metinAttr !== null && _wdf_ai.metinAttr !== undefined ? _wdf_ai.metinAttr : (p.get('ai_metin') || 'Sesli Destek'); if (_wdf_ai.renk === 'wdf-webdiafon' || _wdf_ai.konum === 'wdf-webdiafon' || _wdf_ai.sekil === 'wdf-webdiafon') { _wdf_ai.konum = 'wdf-ust-sag'; _wdf_ai.renk = '#C52F32'; _wdf_ai.sekil = 'wdf-sekil-yuvarlak'; _wdf_ai.metin = ''; } // Sunucu (abonelik) kaynakli ai_mod oncelikli; embed data-ai-mod yanlis OTP acmasin var srvMod = p.get('ai_mod'); if (srvMod === 'sohbet') { _wdf_ai.mod = 'sohbet'; } else { _wdf_ai.mod = 'sesli'; } _wdf_ai.widgetTipi = p.get('ai_widget_tipi') || ''; _wdf_ai.dugmeYuzey = p.get('ai_dugme_yuzey') === 'cam' ? 'cam' : 'dolgu'; _wdf_ai._dbg('widget OK', 'mod=', _wdf_ai.mod, 'konum=', _wdf_ai.konum, 'renk=', _wdf_ai.renk, 'sekil=', _wdf_ai.sekil, 'yuzey=', _wdf_ai.dugmeYuzey); _wdf_ai._telScriptYukle(function() { _wdf_ai._yukleCss(); _wdf_ai._widgetOlustur(); }); } else { _wdf_ai._dbgErr('widget API st!=1', 'msg=', p.get('msg'), 'ham=', ozet); } }) .catch(function(err) { _wdf_ai._dbgErr('widget API fetch hata', err && err.message ? err.message : err); }); }; _wdf_ai._yukleCss = function() { var link = document.createElement('link'); link.rel = 'stylesheet'; var useMf = false; var mfForced = false; if (typeof window !== 'undefined') { if (_wdf_ai.mfAttr === '1' || _wdf_ai.mfAttr === 'true') { useMf = true; mfForced = true; } else if (_wdf_ai.mfAttr === '0' || _wdf_ai.mfAttr === 'false') { useMf = false; mfForced = true; } else { useMf = window._WDF_MF_CSS === true; } if (!useMf && !mfForced) { try { useMf = window.localStorage && window.localStorage.getItem('wdf_mf_css') === '1'; } catch (e4) { /* ignore */ } } } link.href = _wdf_ai.baseUrl + (useMf ? '/usr/soket/dugme-mf.css?v=20260414' : '/usr/soket/dugme.css?v=20260414'); document.head.appendChild(link); if (_wdf_ai.mod === 'sesli' && _wdf_ai.sekil === 'wdf-sekil-yuvarlak') { var lkOrnek = document.createElement('link'); lkOrnek.rel = 'stylesheet'; lkOrnek.href = (_wdf_ai.baseUrl || '') + '/usr/butonlar/ornek_dg.css?v=202604042'; document.head.appendChild(lkOrnek); } }; _wdf_ai._widgetOlustur = function() { if (document.getElementById('wdfWidget')) { _wdf_ai._dbg('uyari', 'Sayfada sip.js (#wdfWidget) zaten var; iki dugme gorunuyorsa sip.js veya ai.js embedinden birini kaldirin.'); } var div = document.createElement('div'); div.id = 'wdfAiWidget'; var renkHexAi = wdfRenkIsHex(_wdf_ai.renk); var renkClsAi = renkHexAi ? 'wdf-renk-hex' : (_wdf_ai.renk || 'wdf-yesil'); var webdiafonOzel = (_wdf_ai.widgetTipi === 'webdiafon_ozel') || ( ((_wdf_ai.renk === '#C52F32' || _wdf_ai.renk === 'wdf-kirmizi') && _wdf_ai.konum === 'wdf-ust-sag' && (_wdf_ai.sekil === 'wdf-sekil-yuvarlak' || _wdf_ai.sekil === 'wdf-sekil-ucgen') && (_wdf_ai.metin || '') === '') ); var ornekKapsul = (_wdf_ai.mod === 'sesli' && _wdf_ai.sekil === 'wdf-sekil-yuvarlak'); var yuzeyCam = ornekKapsul && (_wdf_ai.dugmeYuzey === 'cam'); var cls3dAi = ornekKapsul ? '' : wdfKonumTo3dClass(_wdf_ai.konum); div.className = 'wdfcont wdf-ai ' + (_wdf_ai.mod === 'sesli' ? 'wdf-ai-mod-sesli ' : 'wdf-ai-mod-sohbet ') + (ornekKapsul ? 'wdf-ai-ornek-kapsul ' : '') + (yuzeyCam ? 'wdf-ai-yuzey-cam ' : '') + (webdiafonOzel ? 'wdf-ai-webdiafon ' : '') + _wdf_ai.konum + ' ' + renkClsAi + ' ' + _wdf_ai.sekil + (cls3dAi ? ' ' + cls3dAi : ''); div.innerHTML = '
' + '
' + '' + '
' + '
' + '' + '' + '
' + '
' + '
' + '
' + '
' + '' + '' + '' + '
' + '
' + '' + '
' + '
' + 'Web Diafon' + '
' + '
'; document.body.appendChild(div); if (ornekKapsul && renkHexAi && !yuzeyCam) { _wdf_ai._ornekK1FromHex(div, _wdf_ai.renk); } _wdf_ai._applyHeaderStraddle(); if (typeof window !== 'undefined') { window.addEventListener('resize', function() { _wdf_ai._applyHeaderStraddle(); }); } var hAi = document.getElementById('wdfAiHeader'); /* Kapali ornek: ornek_dg !important seffaf; acik panelde hex renk */ if (hAi && renkHexAi) hAi.style.backgroundColor = _wdf_ai.renk; var titleEl = document.getElementById('wdfAiTitle'); titleEl.textContent = _wdf_ai.metin || ''; _wdf_ai.metinOrijinal = _wdf_ai.metin || ''; var dalga = document.getElementById('wdfAiDalga'); for (var b = 0; b < 5; b++) { var sp = document.createElement('span'); dalga.appendChild(sp); } if (_wdf_ai.mod === 'sesli' || _wdf_ai.mod === 'sohbet') { var dogTel = document.getElementById('wdfAiDogrulama'); if (dogTel) { dogTel.innerHTML = _wdf_ai._telGateAiHtml(); _wdf_ai._setupAiTelPanel(); var formEls = dogTel.querySelectorAll('input, button, label, textarea, select'); for (var fe = 0; fe < formEls.length; fe++) { (function(el) { el.addEventListener('mousedown', function(e0) { e0.stopPropagation(); }); el.addEventListener('touchstart', function(e1) { e1.stopPropagation(); }, { passive: true }); el.addEventListener('click', function(e2) { e2.stopPropagation(); }); })(formEls[fe]); } if (_wdf_ai.mod === 'sesli') { dogTel.style.display = 'none'; } } } document.getElementById('wdfAiHeader').addEventListener('click', function(ev) { if (_wdf_ai.mod === 'sesli') { if (div.classList.contains('wdf-ai-canli')) { _wdf_ai._dbg('tikla', 'header -> gorusmeyi bitir'); _wdf_ai._gorusmeBitir(); return; } ev.preventDefault(); if (!_wdf_ai._sesliOnayVarMi()) { _wdf_ai._dbg('tikla', 'header -> sesli KVKK paneli'); div.classList.add('wdfaktif'); var dogAc = document.getElementById('wdfAiDogrulama'); if (dogAc) dogAc.style.display = 'block'; var mesajAc = document.getElementById('wdfAiMesaj'); if (mesajAc) mesajAc.textContent = ''; return; } _wdf_ai._dbg('tikla', 'header -> sesli baslat (onayli)'); _wdf_ai._sesliTiklaBaslat(div); return; } _wdf_ai._dbg('tikla', 'header -> sohbet panel ac/kapa'); div.classList.toggle('wdfaktif'); }); document.getElementById('wdfAiHeadBtn').addEventListener('click', function(e) { e.stopPropagation(); if (_wdf_ai.mod === 'sesli' && !div.classList.contains('wdf-ai-canli')) { if (div.classList.contains('wdfaktif') && !_wdf_ai._sesliOnayVarMi()) { _wdf_ai._sesliKvkkPanelKapat(); return; } if (!_wdf_ai._sesliOnayVarMi()) { _wdf_ai._dbg('tikla', 'mikrofon -> KVKK paneli'); div.classList.add('wdfaktif'); var dogMic = document.getElementById('wdfAiDogrulama'); if (dogMic) dogMic.style.display = 'block'; var mesajMic = document.getElementById('wdfAiMesaj'); if (mesajMic) mesajMic.textContent = ''; return; } _wdf_ai._dbg('tikla', 'mikrofon butonu -> sesli baslat'); _wdf_ai._sesliTiklaBaslat(div); return; } if (div.classList.contains('wdf-ai-canli')) { _wdf_ai._gorusmeBitir(); } }); document.getElementById('wdfAiBitir').addEventListener('click', function() { _wdf_ai._dbg('tikla', 'gorusmeyi bitir'); _wdf_ai._gorusmeBitir(); }); _wdf_ai._dbg('widget DOM', '#wdfAiWidget eklendi', 'mod=', _wdf_ai.mod); }; _wdf_ai._sesliUcgenBaslik = function(metin, geciciSaniye) { var div = document.getElementById('wdfAiWidget'); var titleEl = document.getElementById('wdfAiTitle'); if (titleEl) titleEl.textContent = metin || ''; if (div && (div.classList.contains('wdf-ust-sag-ucgen') || div.classList.contains('wdf-ust-sol-ucgen') || div.classList.contains('wdf-alt-sag-ucgen') || div.classList.contains('wdf-alt-sol-ucgen'))) { div.classList.add('wdf-ai-sesli-baslik-ucgen'); } if (_wdf_ai._sesliBaslikTimer) { clearTimeout(_wdf_ai._sesliBaslikTimer); _wdf_ai._sesliBaslikTimer = null; } if (geciciSaniye > 0 && !_wdf_ai._sesliCanliMi()) { _wdf_ai._sesliBaslikTimer = setTimeout(function() { _wdf_ai._sesliBaslikSifirla(); }, geciciSaniye * 1000); } }; _wdf_ai._sesliCanliMi = function() { var div = document.getElementById('wdfAiWidget'); return div && div.classList.contains('wdf-ai-canli'); }; _wdf_ai._sesliStorageKey = function() { return 'wdf_sesli_ok_' + (_wdf_ai.siteId || ''); }; _wdf_ai._sesliStorageTsKey = function() { return _wdf_ai._sesliStorageKey() + '_ts'; }; _wdf_ai._sesliTelStorageKey = function() { return 'wdf_sesli_tel_' + (_wdf_ai.siteId || ''); }; /** Cerez adi (site_id guvenli karakter) — onay + tel yedegi */ _wdf_ai._sesliCookieName = function() { return 'wdf_sg_' + String(_wdf_ai.siteId || '0').replace(/[^a-zA-Z0-9_]/g, '_').slice(0, 48); }; _wdf_ai._sesliCookieSet = function(val, maxAgeSec) { try { if (typeof document === 'undefined') return; var n = _wdf_ai._sesliCookieName(); var sec = (typeof location !== 'undefined' && location.protocol === 'https:') ? ';Secure' : ''; document.cookie = n + '=' + encodeURIComponent(val || '') + ';path=/;max-age=' + String(maxAgeSec || 0) + ';SameSite=Lax' + sec; } catch (e0) { /* ignore */ } }; _wdf_ai._sesliCookieGet = function() { try { if (typeof document === 'undefined') return ''; var n = _wdf_ai._sesliCookieName(); var all = ';' + document.cookie; var i = all.indexOf(';' + n + '='); if (i === -1) return ''; var from = i + n.length + 2; var j = all.indexOf(';', from); return decodeURIComponent(j === -1 ? all.slice(from) : all.slice(from, j)); } catch (e1) { return ''; } }; _wdf_ai._sesliTelOku = function() { try { var k = _wdf_ai._sesliTelStorageKey(); if (window.localStorage) { var t = window.localStorage.getItem(k); if (t && /^90\d{10}$/.test(t)) return t; } var c = _wdf_ai._sesliCookieGet(); if (c) { var bit = c.split('|'); if (bit[2] && /^90\d{10}$/.test(bit[2])) return bit[2]; } } catch (e2) { /* ignore */ } return ''; }; _wdf_ai._sesliTelAlanlariniDoldur = function() { if (_wdf_ai._sesliOnayVarMi()) return; var t = _wdf_ai._sesliTelOku(); if (!t) return; var hid = document.getElementById('wdfAiIletisim'); var man = document.getElementById('wdfAiTelManuel'); if (hid) hid.value = t; if (man && man.getAttribute('data-wdf-faz') !== 'kod') man.value = t; }; _wdf_ai._sesliOnayVarMi = function() { try { if (typeof window === 'undefined') return false; var key = _wdf_ai._sesliStorageKey(); var tsKey = _wdf_ai._sesliStorageTsKey(); var telKey = _wdf_ai._sesliTelStorageKey(); var maxMs = 30 * 24 * 60 * 60 * 1000; var now = Date.now(); var hit = false; if (window.localStorage && window.localStorage.getItem(key) === '1') { var ts = parseInt(window.localStorage.getItem(tsKey) || '0', 10); if (ts > 0 && (now - ts) <= maxMs) { hit = true; } else { window.localStorage.removeItem(key); window.localStorage.removeItem(tsKey); window.localStorage.removeItem(telKey); _wdf_ai._sesliCookieSet('', 0); } } if (!hit) { var c = _wdf_ai._sesliCookieGet(); if (c) { var bit = c.split('|'); var tsC = parseInt(bit[0] || '0', 10); if (bit[1] === '1' && tsC > 0 && (now - tsC) <= maxMs) { hit = true; try { if (window.localStorage) { window.localStorage.setItem(key, '1'); window.localStorage.setItem(tsKey, String(tsC)); if (bit[2] && /^90\d{10}$/.test(bit[2])) { window.localStorage.setItem(telKey, bit[2]); } } } catch (eR) { /* ignore */ } } else if (c) { _wdf_ai._sesliCookieSet('', 0); } } } if (!hit && window.sessionStorage && window.sessionStorage.getItem(key) === '1') { hit = true; } return hit; } catch (e1) { return false; } }; _wdf_ai._sesliOnayKaydet = function(telOpt) { try { var key = _wdf_ai._sesliStorageKey(); var tsKey = _wdf_ai._sesliStorageTsKey(); var telKey = _wdf_ai._sesliTelStorageKey(); var now = String(Date.now()); var telNorm = (telOpt && _wdf_ai._mobilNorm(String(telOpt))) || ''; if (typeof window !== 'undefined' && window.localStorage) { window.localStorage.setItem(key, '1'); window.localStorage.setItem(tsKey, now); if (telNorm && /^90\d{10}$/.test(telNorm)) { window.localStorage.setItem(telKey, telNorm); } } if (typeof window !== 'undefined' && window.sessionStorage) { window.sessionStorage.setItem(key, '1'); } _wdf_ai._sesliCookieSet(now + '|1|' + telNorm, 2592000); } catch (e2) { /* ignore */ } }; _wdf_ai._sesliOnaySil = function() { try { if (typeof window === 'undefined') return; var key = _wdf_ai._sesliStorageKey(); var tsKey = _wdf_ai._sesliStorageTsKey(); var telKey = _wdf_ai._sesliTelStorageKey(); if (window.localStorage) { window.localStorage.removeItem(key); window.localStorage.removeItem(tsKey); window.localStorage.removeItem(telKey); } if (window.sessionStorage) { window.sessionStorage.removeItem(key); } _wdf_ai._sesliCookieSet('', 0); } catch (e3) { /* ignore */ } }; _wdf_ai._sesliBaslikSifirla = function() { var div = document.getElementById('wdfAiWidget'); var titleEl = document.getElementById('wdfAiTitle'); if (_wdf_ai._sesliBaslikTimer) { clearTimeout(_wdf_ai._sesliBaslikTimer); _wdf_ai._sesliBaslikTimer = null; } if (titleEl) titleEl.textContent = _wdf_ai.metinOrijinal || _wdf_ai.metin || ''; if (div) div.classList.remove('wdf-ai-sesli-baslik-ucgen'); }; _wdf_ai._sesliTiklaBaslat = function(div) { if (div.classList.contains('wdf-ai-canli')) return; // Eski AudioContext varsa temizle (carpisma onleme) if (_wdf_ai.audioCtx) { try { _wdf_ai.audioCtx.close(); } catch (e) {} _wdf_ai.audioCtx = null; } if (_wdf_ai.mediaStream) { try { _wdf_ai.mediaStream.getTracks().forEach(function(t) { t.stop(); }); } catch (e) {} _wdf_ai.mediaStream = null; } if (_wdf_ai.ws) { try { _wdf_ai.ws.close(); } catch (e) {} _wdf_ai.ws = null; } var aiUrl = (_wdf_ai.baseUrl || '') + '/usr/api/ai-url.php'; _wdf_ai._dbg('ai-url', 'POST', aiUrl); _wdf_ai._sesliUcgenBaslik('Baglaniyor...', 0); var fd = new FormData(); fd.append('site_id', _wdf_ai.siteId); if (_wdf_ai.mod === 'sohbet') { fd.append('ai_mod', 'sohbet'); } var telSakli = _wdf_ai._sesliTelOku() || ''; _wdf_ai._sesliOnayTel = telSakli; if (telSakli) { fd.append('telefon', telSakli); } fetch(aiUrl, { method: 'POST', body: fd }) .then(function(r) { _wdf_ai._dbg('ai-url', 'http=', r.status, r.ok ? 'ok' : 'hata'); return r.text(); }) .then(function(t) { var oz = t.length > 120 ? t.substring(0, 120) + '...' : t; _wdf_ai._dbg('ai-url yanit', oz); var p = new URLSearchParams(t); if (p.get('st') !== '1') { _wdf_ai._dbgErr('ai-url st!=1', p.get('msg') || ''); _wdf_ai._sesliUcgenBaslik(p.get('msg') || 'Baglanti kurulamadi', 6); return; } var signedUrl = p.get('signed_url'); _wdf_ai._aiTalimat = p.get('ai_talimat') || ''; _wdf_ai._crmVar = p.get('crm_var') === '1'; _wdf_ai._crmAd = p.get('crm_ad') || ''; _wdf_ai._crmDetay = p.get('crm_detay') || ''; var su = signedUrl ? (signedUrl.substring(0, 72) + (signedUrl.length > 72 ? '...' : '')) : '(yok)'; _wdf_ai._dbg('ai-url OK', 'signed_url=', su, 'crm=', _wdf_ai._crmVar); _wdf_ai._gorusmeBaslat(signedUrl); }) .catch(function(err) { _wdf_ai._dbgErr('ai-url fetch', err && err.message ? err.message : err); _wdf_ai._sesliUcgenBaslik('Ag hatasi', 6); }); }; _wdf_ai._playNextAudio = function() { if (!_wdf_ai.audioQueue || _wdf_ai.audioQueue.length === 0) { _wdf_ai.isPlaying = false; return; } _wdf_ai.isPlaying = true; var base64Audio = _wdf_ai.audioQueue.shift(); var binaryString = atob(base64Audio); var bytes = new Uint8Array(binaryString.length); for (var i = 0; i < binaryString.length; i++) { bytes[i] = binaryString.charCodeAt(i); } var ctx = new (window.AudioContext || window.webkitAudioContext)({ sampleRate: 16000 }); var audioBuffer = ctx.createBuffer(1, bytes.length / 2, 16000); var channelData = audioBuffer.getChannelData(0); var dv = new DataView(bytes.buffer); for (var j = 0; j < channelData.length; j++) { channelData[j] = dv.getInt16(j * 2, true) / 32768.0; } var src = ctx.createBufferSource(); src.buffer = audioBuffer; src.connect(ctx.destination); src.onended = function() { ctx.close(); setTimeout(function() { _wdf_ai._playNextAudio(); }, 30); }; src.start(0); }; _wdf_ai._gorusmeBaslat = function(signedUrl) { var div = document.getElementById('wdfAiWidget'); var durumDiv = document.getElementById('wdfAiDurum'); var titleEl = document.getElementById('wdfAiTitle'); var sesli = (_wdf_ai.mod === 'sesli'); _wdf_ai._dbg('gorusme', 'baslat', 'sesli=', sesli); if (!signedUrl) { if (sesli) { _wdf_ai._sesliUcgenBaslik('Baglanti adresi yok', 6); } else if (durumDiv) { durumDiv.textContent = 'Baglanti adresi yok'; } return; } _wdf_ai.audioQueue = []; _wdf_ai.isPlaying = false; navigator.mediaDevices.getUserMedia({ audio: true }).then(function(stream) { _wdf_ai._dbg('mikrofon', 'izin verildi'); _wdf_ai.mediaStream = stream; _wdf_ai.audioCtx = new (window.AudioContext || window.webkitAudioContext)({ sampleRate: 16000 }); var source = _wdf_ai.audioCtx.createMediaStreamSource(stream); _wdf_ai.analyser = _wdf_ai.audioCtx.createAnalyser(); _wdf_ai.analyser.fftSize = 256; _wdf_ai.freqData = new Uint8Array(_wdf_ai.analyser.frequencyBinCount); source.connect(_wdf_ai.analyser); if (sesli) { div.classList.remove('wdfaktif'); if (titleEl) titleEl.textContent = _wdf_ai.metinOrijinal || _wdf_ai.metin || ''; div.classList.remove('wdf-ai-sesli-baslik-ucgen'); } else if (durumDiv) { durumDiv.textContent = 'Baglaniyor...'; } var dalgaEl = document.getElementById('wdfAiDalga'); if (dalgaEl) dalgaEl.style.display = 'flex'; div.classList.add('wdf-ai-canli'); var headBtn = document.getElementById('wdfAiHeadBtn'); if (headBtn) headBtn.setAttribute('aria-label', 'Gorusmeyi bitir'); if (!_wdf_ai.rafId) { _wdf_ai.rafId = requestAnimationFrame(_wdf_ai._dalgaUpdate); } _wdf_ai.ws = new WebSocket(signedUrl); _wdf_ai._dbg('websocket', 'baglaniyor...'); function wdfAiMicScriptFallback() { var processor = _wdf_ai.audioCtx.createScriptProcessor(4096, 1, 1); _wdf_ai.processor = processor; source.connect(processor); var gainNode = _wdf_ai.audioCtx.createGain(); gainNode.gain.value = 0; processor.connect(gainNode); gainNode.connect(_wdf_ai.audioCtx.destination); processor.onaudioprocess = function(e) { if (_wdf_ai.ws && _wdf_ai.ws.readyState === WebSocket.OPEN) { var audioData = e.inputBuffer.getChannelData(0); _wdf_ai.ws.send(JSON.stringify({ user_audio_chunk: _wdf_ai._b64(_wdf_ai._pcm16(audioData)) })); } }; } _wdf_ai.ws.onopen = function() { _wdf_ai._dbg('websocket', 'acildi'); if (sesli) { if (titleEl) titleEl.textContent = 'Konusabilirsiniz'; } else if (durumDiv) { durumDiv.textContent = 'Baglandi - Konusabilirsiniz'; } // Prompt enjeksiyonu: ai_talimat hemen, CRM verisi varsa hemen yoksa arka planda var ctx = ''; if (_wdf_ai._crmVar && _wdf_ai._crmAd) { ctx += 'Ilk cumlende mutlaka "Merhaba ' + _wdf_ai._crmAd + '," diye basla. '; ctx += 'Kullanici CRM kaydindan bulundu. Ad soyad: ' + _wdf_ai._crmAd + '. '; if (_wdf_ai._crmDetay) { ctx += 'Detay: ' + _wdf_ai._crmDetay + '. '; } ctx += 'Ilk karsilama cumlesinde ad soyad ile hitap et. Turkce konus.\n\n'; } if (_wdf_ai._aiTalimat) { ctx += _wdf_ai._aiTalimat; } if (ctx) { try { _wdf_ai.ws.send(JSON.stringify({ type: 'contextual_update', text: ctx })); _wdf_ai._dbg('contextual_update', 'gonderildi', ctx.length, 'karakter'); } catch (eCtx) { _wdf_ai._dbgErr('contextual_update', eCtx); } } // CRM detay yoksa arka planda sorgula ve gelince enjekte et if ((!_wdf_ai._crmDetay || _wdf_ai._crmDetay === '') && _wdf_ai._sesliOnayTel) { var crmFd = new FormData(); crmFd.append('m', 'widget_dogrula'); crmFd.append('islem', 'crm_lookup'); crmFd.append('site_id', _wdf_ai.siteId); crmFd.append('telefon', _wdf_ai._sesliOnayTel); fetch(_wdf_ai.baseUrl + '/index.php', { method: 'POST', body: crmFd, credentials: 'include' }) .then(function(r) { return r.text(); }) .then(function(t) { var p = new URLSearchParams(t); if (p.get('crm_var') === '1' && _wdf_ai.ws && _wdf_ai.ws.readyState === WebSocket.OPEN) { var crmCtx = ''; if (p.get('crm_ad')) { crmCtx += 'Kullanici CRM kaydindan bulundu. Ad soyad: ' + p.get('crm_ad') + '. '; } if (p.get('crm_detay')) { crmCtx += 'CRM gecmisi ve detay bilgisi: ' + p.get('crm_detay') + '. '; crmCtx += 'Bu bilgileri kullanarak kullaniciya onceki aramalari ve konulari hakkinda bilgi verebilirsin. '; } if (crmCtx !== '') { crmCtx += 'Turkce konus.'; try { _wdf_ai.ws.send(JSON.stringify({ type: 'contextual_update', text: crmCtx })); _wdf_ai._dbg('crm_lookup async', 'enjekte edildi', (p.get('crm_ad') || ''), 'detay:', (p.get('crm_detay') || '').length, 'kar'); } catch (e) { _wdf_ai._dbgErr('crm_lookup async send', e); } } } }) .catch(function() {}); } var capUrl = (_wdf_ai.baseUrl || '') + '/usr/soket/wdf_ai_cap.js'; if (_wdf_ai.audioCtx.audioWorklet) { _wdf_ai.audioCtx.audioWorklet.addModule(capUrl).then(function() { if (!_wdf_ai.ws || _wdf_ai.ws.readyState !== WebSocket.OPEN || !_wdf_ai.audioCtx) { return; } var node = new AudioWorkletNode(_wdf_ai.audioCtx, 'wdf-ai-capture', { numberOfInputs: 1, numberOfOutputs: 1, outputChannelCount: [1], channelCount: 1, channelCountMode: 'explicit' }); _wdf_ai.processor = node; source.connect(node); var gainW = _wdf_ai.audioCtx.createGain(); gainW.gain.value = 0; node.connect(gainW); gainW.connect(_wdf_ai.audioCtx.destination); node.port.onmessage = function(ev) { if (!_wdf_ai.ws || _wdf_ai.ws.readyState !== WebSocket.OPEN) { return; } var f32 = ev.data && ev.data.f32; if (!f32) { return; } _wdf_ai.ws.send(JSON.stringify({ user_audio_chunk: _wdf_ai._b64(_wdf_ai._pcm16(f32)) })); }; }).catch(function() { wdfAiMicScriptFallback(); }); } else { wdfAiMicScriptFallback(); } }; _wdf_ai.ws.onmessage = function(event) { var data; try { data = JSON.parse(event.data); } catch (parseErr) { return; } var tr = document.getElementById('wdfAiTranscript'); if (data.type === 'user_transcript' && data.user_transcription_event && tr && !sesli) { var line = document.createElement('div'); line.textContent = 'Siz: ' + (data.user_transcription_event.user_transcript || ''); tr.appendChild(line); tr.scrollTop = tr.scrollHeight; } if (data.type === 'agent_response' && data.agent_response_event && tr && !sesli) { var line2 = document.createElement('div'); line2.textContent = 'AI: ' + (data.agent_response_event.agent_response || ''); tr.appendChild(line2); tr.scrollTop = tr.scrollHeight; } if (data.type === 'audio' && data.audio_event && data.audio_event.audio_base_64) { _wdf_ai.audioQueue.push(data.audio_event.audio_base_64); if (!_wdf_ai.isPlaying) { _wdf_ai._playNextAudio(); } } if (data.type === 'ping' && data.ping_event) { setTimeout(function() { if (_wdf_ai.ws && _wdf_ai.ws.readyState === WebSocket.OPEN) { _wdf_ai.ws.send(JSON.stringify({ type: 'pong', event_id: data.ping_event.event_id })); } }, data.ping_event.ping_ms || 0); } }; _wdf_ai.ws.onerror = function() { _wdf_ai._dbgErr('websocket', 'onerror'); if (sesli) { _wdf_ai._sesliUcgenBaslik('Baglanti hatasi', 6); } else if (durumDiv) { durumDiv.textContent = 'Baglanti hatasi'; } }; _wdf_ai.ws.onclose = function() { _wdf_ai._dbg('websocket', 'kapandi'); _wdf_ai.ws = null; _wdf_ai._cleanupGorusmeKaynaklari(); }; }).catch(function(err) { _wdf_ai._dbgErr('mikrofon', err && err.message ? err.message : err); if (sesli) { _wdf_ai._sesliUcgenBaslik('Mikrofon: ' + (err && err.message ? err.message : 'izin gerekli'), 8); } else if (durumDiv) { durumDiv.textContent = 'Mikrofon: ' + (err && err.message ? err.message : 'izin gerekli'); } }); }; _wdf_ai._cleanupGorusmeKaynaklari = function() { if (_wdf_ai.rafId) { cancelAnimationFrame(_wdf_ai.rafId); _wdf_ai.rafId = null; } if (_wdf_ai.processor) { try { _wdf_ai.processor.disconnect(); } catch (e) { /* ignore */ } _wdf_ai.processor = null; } if (_wdf_ai.mediaStream) { _wdf_ai.mediaStream.getTracks().forEach(function(t) { t.stop(); }); _wdf_ai.mediaStream = null; } if (_wdf_ai.audioCtx) { try { _wdf_ai.audioCtx.close(); } catch (e3) { /* ignore */ } _wdf_ai.audioCtx = null; } _wdf_ai.analyser = null; _wdf_ai.freqData = null; _wdf_ai.audioQueue = []; _wdf_ai.isPlaying = false; var div = document.getElementById('wdfAiWidget'); if (div) div.classList.remove('wdf-ai-canli'); var headBtnCl = document.getElementById('wdfAiHeadBtn'); if (headBtnCl) headBtnCl.setAttribute('aria-label', 'Sesli destek'); var dalgaEl = document.getElementById('wdfAiDalga'); if (dalgaEl) { dalgaEl.style.display = 'none'; var spans = dalgaEl.querySelectorAll('span'); for (var i = 0; i < spans.length; i++) spans[i].style.height = '4px'; } if (_wdf_ai.mod === 'sesli') { _wdf_ai._sesliGorusmeSonu(); return; } var durumDiv = document.getElementById('wdfAiDurum'); if (durumDiv) durumDiv.textContent = 'Gorusme sonlandi'; setTimeout(function() { var d = document.getElementById('wdfAiDogrulama'); var g = document.getElementById('wdfAiGorusme'); if (d) d.style.display = 'block'; if (g) g.style.display = 'none'; }, 2000); }; _wdf_ai._sesliGorusmeSonu = function() { var div = document.getElementById('wdfAiWidget'); var g = document.getElementById('wdfAiGorusme'); var d = document.getElementById('wdfAiDogrulama'); if (div) div.classList.remove('wdfaktif'); if (g) g.style.display = 'none'; if (d) d.style.display = 'none'; _wdf_ai._sesliBaslikSifirla(); }; _wdf_ai._gorusmeBitir = function() { if (_wdf_ai.ws) { try { _wdf_ai.ws.onclose = null; _wdf_ai.ws.close(); } catch (e2) { /* ignore */ } _wdf_ai.ws = null; } _wdf_ai._cleanupGorusmeKaynaklari(); }; _wdf_ai.init(); })();