mirror of
https://github.com/multipleof4/sune.git
synced 2026-01-14 08:38:00 +00:00
Update sw.js
This commit is contained in:
265
public/sw.js
265
public/sw.js
@@ -1,42 +1,101 @@
|
|||||||
// /sw.js
|
// /sw.js
|
||||||
// Enhanced: tracks whether streaming fetches are being teed and reports status on ping.
|
// Service worker that tees streaming responses and continuously overwrites the latest
|
||||||
// Drop this at the root: /sw.js
|
// thread in localForage (key: 'threads_v1') with the accumulating assistant text.
|
||||||
|
// Keeps ping/pong and PING_STATUS support and broadcasts live events.
|
||||||
|
//
|
||||||
|
// Requirements: place this at root (/sw.js). No changes to index required.
|
||||||
|
|
||||||
const TARGET_SUBSTRING = 'openrouter.ai/api/v1/chat/completions'; // adjust if different
|
importScripts('https://cdn.jsdelivr.net/npm/localforage@1.10.0/dist/localforage.min.js');
|
||||||
const STATE_TTL_MS = 24 * 60 * 60 * 1000;
|
|
||||||
|
|
||||||
const state = {
|
const TARGET_SUBSTRING = 'openrouter.ai/api/v1/chat/completions'; // change if needed
|
||||||
totalIntercepted: 0,
|
const THREADS_KEY = 'threads_v1';
|
||||||
activeStreams: {}, // streamId -> { url, startedAt, bytes, lastProgressAt, status }
|
const SAVE_BYTES_THRESHOLD = 8 * 1024; // flush every ~8KB of new text
|
||||||
lastStreamSummary: null // summary of last finished stream
|
const SAVE_TIME_THRESHOLD = 1000; // or at least every 1s
|
||||||
};
|
const BROADCAST_THROTTLE_MS = 700; // throttle progress broadcasts
|
||||||
|
|
||||||
|
/* --- Utilities --- */
|
||||||
const gid = () => Math.random().toString(36).slice(2,9) + '-' + Date.now().toString(36);
|
const gid = () => Math.random().toString(36).slice(2,9) + '-' + Date.now().toString(36);
|
||||||
|
const now = () => Date.now();
|
||||||
|
|
||||||
|
async function readThreads() {
|
||||||
|
try {
|
||||||
|
const v = await localforage.getItem(THREADS_KEY);
|
||||||
|
return Array.isArray(v) ? v : [];
|
||||||
|
} catch (e) {
|
||||||
|
console.error('sw: readThreads error', e);
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
async function writeThreads(arr) {
|
||||||
|
try {
|
||||||
|
await localforage.setItem(THREADS_KEY, arr);
|
||||||
|
} catch (e) {
|
||||||
|
console.error('sw: writeThreads error', e);
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* pick last thread heuristic: newest updatedAt, fallback to first */
|
||||||
|
function pickLastThread(threads) {
|
||||||
|
if (!threads || threads.length === 0) return null;
|
||||||
|
let sorted = [...threads].sort((a,b) => (b.updatedAt||0) - (a.updatedAt||0));
|
||||||
|
return sorted[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Upsert assistant message in a thread by sw_streamId (overwrite content) */
|
||||||
|
function upsertAssistantInThreadObj(threadObj, streamId, text) {
|
||||||
|
threadObj.updatedAt = now();
|
||||||
|
// look for existing message with sw_streamId (search from end)
|
||||||
|
for (let i = threadObj.messages.length - 1; i >= 0; i--) {
|
||||||
|
const m = threadObj.messages[i];
|
||||||
|
if (m && m.sw_streamId === streamId) {
|
||||||
|
m.content = text;
|
||||||
|
m.contentParts = [{ type: 'text', text }];
|
||||||
|
m.updatedAt = now();
|
||||||
|
m._sw_savedAt = now();
|
||||||
|
return threadObj;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// not found: append a new assistant message
|
||||||
|
const msg = {
|
||||||
|
id: 'swmsg-' + gid(),
|
||||||
|
role: 'assistant',
|
||||||
|
content: text,
|
||||||
|
contentParts: [{ type: 'text', text }],
|
||||||
|
kind: 'assistant',
|
||||||
|
sw_saved: true,
|
||||||
|
sw_streamId: streamId,
|
||||||
|
createdAt: now(),
|
||||||
|
updatedAt: now(),
|
||||||
|
_sw_savedAt: now()
|
||||||
|
};
|
||||||
|
threadObj.messages.push(msg);
|
||||||
|
return threadObj;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Broadcast helpers */
|
||||||
async function broadcast(msg) {
|
async function broadcast(msg) {
|
||||||
try {
|
try {
|
||||||
const clientsList = await self.clients.matchAll({ includeUncontrolled: true, type: 'window' });
|
const cl = await self.clients.matchAll({ includeUncontrolled: true, type: 'window' });
|
||||||
for (const c of clientsList) {
|
for (const c of cl) {
|
||||||
try { c.postMessage(msg); } catch (e) { /* ignore client errors */ }
|
try { c.postMessage(msg); } catch (e) { /* ignore */ }
|
||||||
}
|
}
|
||||||
} catch (e) { /* ignore */ }
|
} catch (e) { /* ignore */ }
|
||||||
}
|
}
|
||||||
|
|
||||||
function cleanupState() {
|
/* --- Worker lifecycle --- */
|
||||||
const now = Date.now();
|
self.addEventListener('install', e => self.skipWaiting());
|
||||||
for (const k of Object.keys(state.activeStreams)) {
|
self.addEventListener('activate', e => e.waitUntil(self.clients.claim()));
|
||||||
if ((now - (state.activeStreams[k].lastProgressAt || state.activeStreams[k].startedAt)) > STATE_TTL_MS) {
|
|
||||||
delete state.activeStreams[k];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// install/activate
|
/* --- Stream tracking state (in-memory) --- */
|
||||||
self.addEventListener('install', (e) => { self.skipWaiting(); });
|
const state = {
|
||||||
self.addEventListener('activate', (e) => { e.waitUntil(self.clients.claim()); });
|
totalIntercepted: 0,
|
||||||
|
activeStreams: {}, // streamId => meta
|
||||||
|
lastStream: null
|
||||||
|
};
|
||||||
|
|
||||||
// fetch: attempt to tee target streaming responses and track progress
|
/* --- Main fetch handler: tee + continuously overwrite latest thread --- */
|
||||||
self.addEventListener('fetch', (event) => {
|
self.addEventListener('fetch', event => {
|
||||||
try {
|
try {
|
||||||
const url = String(event.request.url || '');
|
const url = String(event.request.url || '');
|
||||||
if (!url.includes(TARGET_SUBSTRING)) return; // not our target
|
if (!url.includes(TARGET_SUBSTRING)) return; // not our target
|
||||||
@@ -44,117 +103,148 @@ self.addEventListener('fetch', (event) => {
|
|||||||
event.respondWith((async () => {
|
event.respondWith((async () => {
|
||||||
const upstream = await fetch(event.request);
|
const upstream = await fetch(event.request);
|
||||||
|
|
||||||
// if there's no body (or not a readable stream), just forward
|
// nothing to do if no stream body
|
||||||
if (!upstream || !upstream.body) return upstream;
|
if (!upstream || !upstream.body) return upstream;
|
||||||
|
|
||||||
// create a stream id and register active stream
|
|
||||||
const streamId = 'sw-' + gid();
|
const streamId = 'sw-' + gid();
|
||||||
const meta = { url, startedAt: Date.now(), bytes: 0, lastProgressAt: Date.now(), status: 'started' };
|
const meta = { url, startedAt: now(), bytes: 0, status: 'started' };
|
||||||
state.totalIntercepted = (state.totalIntercepted || 0) + 1;
|
state.totalIntercepted = (state.totalIntercepted || 0) + 1;
|
||||||
state.activeStreams[streamId] = meta;
|
state.activeStreams[streamId] = meta;
|
||||||
// notify clients
|
|
||||||
broadcast({ type: 'sw-intercept-start', streamId, meta });
|
broadcast({ type: 'sw-intercept-start', streamId, meta });
|
||||||
|
|
||||||
// tee the body: one goes to client, one we consume in SW
|
// tee the stream
|
||||||
const [clientStream, swStream] = upstream.body.tee();
|
const [clientStream, swStream] = upstream.body.tee();
|
||||||
|
|
||||||
|
// background saving task (continually overwrite latest thread)
|
||||||
const savePromise = (async () => {
|
const savePromise = (async () => {
|
||||||
try {
|
|
||||||
const reader = swStream.getReader();
|
const reader = swStream.getReader();
|
||||||
const decoder = new TextDecoder('utf-8');
|
const decoder = new TextDecoder('utf-8');
|
||||||
let decodedSoFar = '';
|
let accumulated = ''; // full text accumulated for this stream
|
||||||
let chunkCount = 0;
|
let sinceLastSaveBytes = 0;
|
||||||
let lastBroadcastMs = 0;
|
let lastSaveAt = 0;
|
||||||
const BROADCAST_THROTTLE_MS = 800;
|
let lastBroadcastAt = 0;
|
||||||
const BROADCAST_BYTES = 16 * 1024; // also broadcast every ~16KB
|
|
||||||
|
|
||||||
while (true) {
|
// Helper to save accumulated text into last thread
|
||||||
const { value, done } = await reader.read();
|
async function flushToLastThread(force = false) {
|
||||||
if (done) break;
|
try {
|
||||||
chunkCount++;
|
const nowMs = now();
|
||||||
// count bytes
|
if (!force && sinceLastSaveBytes < SAVE_BYTES_THRESHOLD && (nowMs - lastSaveAt) < SAVE_TIME_THRESHOLD) return;
|
||||||
const bytes = value ? value.byteLength || 0 : 0;
|
// read latest threads
|
||||||
meta.bytes += bytes;
|
const threads = await readThreads();
|
||||||
meta.lastProgressAt = Date.now();
|
let thread = pickLastThread(threads);
|
||||||
|
const createdAt = nowMs;
|
||||||
// append decoded snippet for quick preview
|
if (!thread) {
|
||||||
try { decodedSoFar += decoder.decode(value, { stream: true }); } catch (e) { /* ignore decode */ }
|
// create fallback thread if none exists
|
||||||
|
thread = {
|
||||||
// occasional broadcasts (throttle)
|
id: 'sw-thread-' + gid(),
|
||||||
const now = Date.now();
|
title: 'Missed while backgrounded',
|
||||||
if (now - lastBroadcastMs > BROADCAST_THROTTLE_MS || meta.bytes >= (meta._lastBroadcastBytes || 0) + BROADCAST_BYTES) {
|
pinned: false,
|
||||||
meta._lastBroadcastBytes = meta.bytes;
|
updatedAt: createdAt,
|
||||||
lastBroadcastMs = now;
|
messages: []
|
||||||
|
};
|
||||||
|
threads.unshift(thread);
|
||||||
|
}
|
||||||
|
// upsert message
|
||||||
|
upsertAssistantInThreadObj(thread, streamId, accumulated);
|
||||||
|
// write back (this will overwrite whole array, which matches page reading expectation)
|
||||||
|
await writeThreads(threads);
|
||||||
|
sinceLastSaveBytes = 0;
|
||||||
|
lastSaveAt = nowMs;
|
||||||
|
// broadcast progress summary
|
||||||
|
const now2 = Date.now();
|
||||||
|
if (now2 - lastBroadcastAt > BROADCAST_THROTTLE_MS) {
|
||||||
|
lastBroadcastAt = now2;
|
||||||
broadcast({
|
broadcast({
|
||||||
type: 'sw-intercept-progress',
|
type: 'sw-intercept-progress',
|
||||||
streamId,
|
streamId,
|
||||||
meta: { bytes: meta.bytes, lastProgressAt: meta.lastProgressAt, snippet: decodedSoFar.slice(-1024) }
|
meta: { bytes: meta.bytes, savedAt: lastSaveAt, snippet: accumulated.slice(-1024) }
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.error('sw: flushToLastThread error', err);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// finalize
|
try {
|
||||||
|
while (true) {
|
||||||
|
const { value, done } = await reader.read();
|
||||||
|
if (done) break;
|
||||||
|
// value is Uint8Array (may be chunked). decode and append
|
||||||
|
let chunkText = '';
|
||||||
|
try {
|
||||||
|
chunkText = decoder.decode(value, { stream: true });
|
||||||
|
} catch (e) {
|
||||||
|
// fallback: best-effort text conversion
|
||||||
|
try { chunkText = String(value); } catch (ee) { chunkText = ''; }
|
||||||
|
}
|
||||||
|
accumulated += chunkText;
|
||||||
|
const bytes = value ? (value.byteLength || 0) : chunkText.length;
|
||||||
|
meta.bytes += bytes;
|
||||||
|
meta.lastProgressAt = now();
|
||||||
|
|
||||||
|
// accumulate for thresholded saves
|
||||||
|
sinceLastSaveBytes += bytes;
|
||||||
|
// flush condition: size or time
|
||||||
|
await flushToLastThread(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
// final flush and finalize
|
||||||
|
await flushToLastThread(true);
|
||||||
|
|
||||||
meta.status = 'finished';
|
meta.status = 'finished';
|
||||||
meta.endedAt = Date.now();
|
meta.endedAt = now();
|
||||||
state.lastStreamSummary = {
|
state.lastStream = { streamId, url: meta.url, startedAt: meta.startedAt, endedAt: meta.endedAt, totalBytes: meta.bytes };
|
||||||
streamId, url, startedAt: meta.startedAt, endedAt: meta.endedAt, totalBytes: meta.bytes
|
|
||||||
};
|
|
||||||
// remove from active
|
|
||||||
delete state.activeStreams[streamId];
|
delete state.activeStreams[streamId];
|
||||||
broadcast({ type: 'sw-intercept-end', streamId, meta: { totalBytes: meta.bytes, endedAt: meta.endedAt } });
|
broadcast({ type: 'sw-intercept-end', streamId, meta: { totalBytes: meta.bytes, endedAt: meta.endedAt } });
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
meta.status = 'error';
|
meta.status = 'error';
|
||||||
meta.error = String(err && err.message ? err.message : err);
|
meta.error = String(err && err.message ? err.message : err);
|
||||||
meta.lastProgressAt = Date.now();
|
|
||||||
delete state.activeStreams[streamId];
|
delete state.activeStreams[streamId];
|
||||||
broadcast({ type: 'sw-intercept-error', streamId, meta: { error: meta.error } });
|
broadcast({ type: 'sw-intercept-error', streamId, meta: { error: meta.error } });
|
||||||
console.error('sw: stream save error', err);
|
console.error('sw: savePromise error', err);
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
|
|
||||||
// keep the SW alive while we process the swStream
|
// keep SW alive while saving
|
||||||
event.waitUntil(savePromise);
|
event.waitUntil(savePromise);
|
||||||
|
|
||||||
// forward the response to the page using the clientStream
|
// respond to page
|
||||||
return new Response(clientStream, {
|
return new Response(clientStream, {
|
||||||
status: upstream.status,
|
status: upstream.status,
|
||||||
statusText: upstream.statusText,
|
statusText: upstream.statusText,
|
||||||
headers: upstream.headers
|
headers: upstream.headers
|
||||||
});
|
});
|
||||||
})());
|
})());
|
||||||
} catch (e) {
|
} catch (err) {
|
||||||
console.error('sw: fetch handler error', e);
|
console.error('sw: fetch handler error', err);
|
||||||
} finally {
|
|
||||||
cleanupState();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// message handler: support PING (simple) and PING_STATUS (detailed)
|
/* --- Messaging: PING / PING_STATUS / GET_STATE --- */
|
||||||
self.addEventListener('message', (event) => {
|
self.addEventListener('message', event => {
|
||||||
const data = event.data || {};
|
const data = event.data || {};
|
||||||
try {
|
try {
|
||||||
|
// simple ping (original behavior)
|
||||||
if (data && data.type === 'PING') {
|
if (data && data.type === 'PING') {
|
||||||
// original ping behavior: support MessageChannel
|
|
||||||
if (event.ports && event.ports[0]) {
|
if (event.ports && event.ports[0]) {
|
||||||
event.ports[0].postMessage({ type: 'PONG', ts: Date.now(), ok: true });
|
event.ports[0].postMessage({ type: 'PONG', ts: now(), ok: true });
|
||||||
} else if (event.source && typeof event.source.postMessage === 'function') {
|
} else if (event.source && typeof event.source.postMessage === 'function') {
|
||||||
try { event.source.postMessage({ type: 'PONG', ts: Date.now(), ok: true }); } catch(e) {}
|
try { event.source.postMessage({ type: 'PONG', ts: now(), ok: true }); } catch(e) {}
|
||||||
} else {
|
} else {
|
||||||
broadcast({ type: 'PONG', ts: Date.now(), ok: true });
|
broadcast({ type: 'PONG', ts: now(), ok: true });
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// status ping that returns internal state
|
||||||
if (data && data.type === 'PING_STATUS') {
|
if (data && data.type === 'PING_STATUS') {
|
||||||
// return current SW status: activeStreams summary + lastStreamSummary + totalIntercepted
|
|
||||||
const reply = {
|
const reply = {
|
||||||
type: 'PONG_STATUS',
|
type: 'PONG_STATUS',
|
||||||
ts: Date.now(),
|
ts: now(),
|
||||||
totalIntercepted: state.totalIntercepted || 0,
|
totalIntercepted: state.totalIntercepted || 0,
|
||||||
activeStreams: Object.entries(state.activeStreams).map(([id,m]) => ({ streamId: id, url: m.url, bytes: m.bytes, status: m.status, startedAt: m.startedAt })),
|
activeStreams: Object.entries(state.activeStreams).map(([id,m]) => ({ streamId: id, url: m.url, bytes: m.bytes, status: m.status, startedAt: m.startedAt })),
|
||||||
lastStreamSummary: state.lastStreamSummary || null
|
lastStream: state.lastStream || null
|
||||||
};
|
};
|
||||||
// reply on MessageChannel port if present, else try source, else broadcast
|
|
||||||
if (event.ports && event.ports[0]) {
|
if (event.ports && event.ports[0]) {
|
||||||
event.ports[0].postMessage(reply);
|
event.ports[0].postMessage(reply);
|
||||||
} else if (event.source && typeof event.source.postMessage === 'function') {
|
} else if (event.source && typeof event.source.postMessage === 'function') {
|
||||||
@@ -165,11 +255,20 @@ self.addEventListener('message', (event) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// support a request for the sw to return its current state (no port)
|
// optional: client requests list of SW-saved streams/messages
|
||||||
if (data && data.type === 'GET_STATE') {
|
if (data && data.type === 'LIST_SW_SAVED') {
|
||||||
const snapshot = { totalIntercepted: state.totalIntercepted || 0, activeCount: Object.keys(state.activeStreams).length, last: state.lastStreamSummary || null };
|
(async () => {
|
||||||
if (event.ports && event.ports[0]) event.ports[0].postMessage({ type:'STATE', snapshot });
|
const threads = await readThreads();
|
||||||
else if (event.source && event.source.postMessage) event.source.postMessage({ type:'STATE', snapshot });
|
const found = [];
|
||||||
|
for (const t of (threads || [])) {
|
||||||
|
for (const m of (t.messages || [])) {
|
||||||
|
if (m && m.sw_streamId) found.push({ threadId: t.id, threadTitle: t.title, messageId: m.id, sw_streamId: m.sw_streamId, snippet: (m.content||'').slice(0,200), updatedAt: m.updatedAt });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (event.ports && event.ports[0]) event.ports[0].postMessage({ type: 'LIST_SW_SAVED_RESULT', streams: found });
|
||||||
|
else if (event.source && typeof event.source.postMessage === 'function') event.source.postMessage({ type: 'LIST_SW_SAVED_RESULT', streams: found });
|
||||||
|
else broadcast({ type: 'LIST_SW_SAVED_RESULT', streams: found });
|
||||||
|
})();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|||||||
Reference in New Issue
Block a user