diff --git a/content/content.js b/content/content.js index 733a686..f3da538 100644 --- a/content/content.js +++ b/content/content.js @@ -30,7 +30,7 @@ var queue = []; var pumping = false; var timer = null; - var pending = new Set(); + var pending = new Set(); var lastPath = location.pathname; function __onPathChange() { var p = location.pathname; if (p === lastPath) return; lastPath = p; queue.length = 0; pending.clear(); seenHashes.clear(); seenCallsHashes.clear(); pumping = false; if (UI) { UI.setPhase('idle'); if (UI.log) UI.log('chat changed ' + p); } try { window.dispatchEvent(new CustomEvent('dshmem:chat-change', { detail: p })); } catch (e) {} } window.addEventListener('popstate', __onPathChange); try { var __ps = history.pushState; var __rs = history.replaceState; history.pushState = function () { var r = __ps.apply(this, arguments); setTimeout(__onPathChange, 0); return r; }; history.replaceState = function () { var r = __rs.apply(this, arguments); setTimeout(__onPathChange, 0); return r; }; } catch (e) {} setInterval(__onPathChange, 1000); function hashText(s) { var h = 5381; diff --git a/injected/chat-logger.js b/injected/chat-logger.js index e21816d..ed9b03f 100644 --- a/injected/chat-logger.js +++ b/injected/chat-logger.js @@ -8,7 +8,7 @@ var seen = Object.create(null) var cfg = null var lastAccum = null - var SEP = String.fromCharCode(10) + String.fromCharCode(10) + var SEP = String.fromCharCode(10) + String.fromCharCode(10); try { window.addEventListener('dshmem:chat-change', function () { charsAccum = 0; lastAccum = null; seen = Object.create(null); }) } catch (e) {} function hash(s) { var h1 = 5381 @@ -159,9 +159,9 @@ return } if (!cfg.enabled) return - var messages = [] + var messages = []; var chatId = ''; var reqKeys = '' try { - var req = JSON.parse(entry.body) + var req = JSON.parse(entry.body); if (req) reqKeys = Object.keys(req).join(','); var ck = 'chat' + '_session' + '_id'; if (req) chatId = String(req[ck] || '') if (req && Array.isArray(req.messages)) messages = req.messages else if (req && typeof req.prompt === 'string' && req.prompt) messages = [{ role: 'user', content: req.prompt }] } catch (e) { return } @@ -204,7 +204,7 @@ accum: accum, delta: delta, chars: ctxChars, - est_tok: Math.round(ctxChars / K_RATIO), ctx_chars: charsAccum, ctx_tok: Math.round(charsAccum / K_RATIO), ctx_pct: Math.round(charsAccum / K_RATIO * 1000 / CONTEXT_LIMIT) / 10 + chat_id: chatId, req_keys: reqKeys, est_tok: Math.round(ctxChars / K_RATIO), ctx_chars: charsAccum, ctx_tok: Math.round(charsAccum / K_RATIO), ctx_pct: Math.round(charsAccum / K_RATIO * 1000 / CONTEXT_LIMIT) / 10 } lines.push(JSON.stringify(meta)); try { window.dispatchEvent(new CustomEvent('dshmem:metrics', { detail: JSON.stringify({ accum: accum, delta: delta, chars: ctxChars, est_tok: Math.round(ctxChars / K_RATIO), ctx_chars: charsAccum, ctx_tok: Math.round(charsAccum / K_RATIO), ctx_pct: Math.round(charsAccum / K_RATIO * 1000 / CONTEXT_LIMIT) / 10 }) })) } catch (e) { } } @@ -233,5 +233,5 @@ return origSend.apply(this, arguments) } - requestConfig() + requestConfig(); try { var __boot = Date.now(); var __nav = ''; try { var __ne = performance.getEntriesByType('navigation'); __nav = (__ne && __ne[0] && __ne[0].type) || 'unknown' } catch (e1) { __nav = 'err' } var __path = String(location.pathname || ''); var __cid = ''; var __parts = __path.split('/'); var __si = __parts.indexOf('s'); __cid = __si !== -1 && __parts[__si + 1] ? __parts[__si + 1] : ''; var __init = { mid: 'init-' + __boot, part: 0, total: 1, role: 'init', ts: __boot, inj: false, text: 'init', boot: __boot, nav: __nav, ready: document.readyState, path: __path, chat_id: __cid }; var __cfgReady = requestConfig(); __cfgReady.then(function (c) { if (!c || !c.token) return; var __h = { 'Content-Type': 'application/json' }; __h.Authorization = 'Bearer ' + c.token; fetch(BASE + '/log/append', { method: 'POST', headers: __h, body: JSON.stringify({ file: today() + '.jsonl', lines: [JSON.stringify(__init)] }) }).catch(function () { }) }); } catch (e2) { } })()