Update index.html

This commit is contained in:
2025-08-24 00:21:54 -07:00
committed by GitHub
parent 15ac08cf81
commit d84254f02e

View File

@@ -215,53 +215,76 @@ const WS_URL = "wss://orp.awww.workers.dev/ws";
class SingleRunWS { class SingleRunWS {
constructor({ payload, onDelta }) { constructor({ payload, onDelta }) {
this.payload=payload; this.onDelta=onDelta; this.payload = payload; // { apiKey, model, messages }
this.ws=null; this.lastSeq=-1; this.onDelta = onDelta;
this.done=false; this.manual=false; this.ws = null;
this.timer=null; this.backoff=300; this.lastSeq = -1;
this.done = false;
this.manual = false;
this.timer = null;
this.backoff = 300;
this.rid = Date.now().toString(36) + Math.random().toString(36).slice(2, 8);
} }
start(){ this.#connect("begin"); } start(){ this.connect("begin"); }
abort(){ abort(){
this.manual=true; this.done=true; this.manual = true; this.done = true;
try{ this.ws?.send(JSON.stringify({type:"stop"})) }catch{} try { this.ws?.send(JSON.stringify({ type: "stop", rid: this.rid })); } catch {}
try{ this.ws?.close() }catch{} try { this.ws?.close(); } catch {}
clearTimeout(this.timer); clearTimeout(this.timer);
} }
#connect(mode){ connect(mode){
if(this.done)return; if (this.done) return;
this.ws=new WebSocket(WS_URL); this.ws = new WebSocket(WS_URL);
this.ws.onopen=()=>{ this.ws.onopen = () => {
if(mode==="begin") this.ws.send(JSON.stringify({type:"begin",...this.payload,after:this.lastSeq})); if (mode === "begin") {
else this.ws.send(JSON.stringify({type:"resume",after:this.lastSeq})); this.ws.send(JSON.stringify({ type: "begin", rid: this.rid, ...this.payload, after: this.lastSeq }));
} else {
this.ws.send(JSON.stringify({ type: "resume", rid: this.rid, after: this.lastSeq }));
}
}; };
this.ws.onmessage=e=>{ this.ws.onmessage = (e) => {
let m; try{m=JSON.parse(e.data)}catch{return}; let m; try { m = JSON.parse(e.data); } catch { return; }
if(m.type==="delta"&&m.seq>this.lastSeq){this.lastSeq=m.seq;this.onDelta(m.text,false);} if (m.type === "delta" && typeof m.seq === "number" && m.seq > this.lastSeq) {
else if(m.type==="done"){this.done=true;this.onDelta("",true);this.#cleanup();} this.lastSeq = m.seq;
else if(m.type==="err"){this.done=true;this.onDelta("\\n\\n"+(m.message||"error"),true);this.#cleanup();} this.onDelta(m.text, false);
} else if (m.type === "done") {
this.done = true;
this.onDelta("", true);
this.cleanup();
} else if (m.type === "err") {
// stale_run/busy/etc. end this runner gracefully
this.done = true;
this.onDelta("\n\n" + (m.message || "error"), true);
this.cleanup();
}
}; };
this.ws.onclose=()=>{ if(!this.done&&!this.manual) this.#schedule(); }; this.ws.onclose = () => { if (!this.done && !this.manual) this.schedule(); };
this.ws.onerror = () => {};
} }
#schedule(){ schedule(){
clearTimeout(this.timer); clearTimeout(this.timer);
if(this.manual||this.done)return; if (this.manual || this.done) return;
this.timer=setTimeout(()=>{this.#connect("resume"); this.backoff=Math.min(this.backoff*1.5,5000)},this.backoff); this.timer = setTimeout(() => {
this.connect("resume");
this.backoff = Math.min(this.backoff * 1.5, 5000);
}, this.backoff);
} }
#cleanup(){ clearTimeout(this.timer); try{this.ws?.close()}catch{} } cleanup(){ clearTimeout(this.timer); try { this.ws?.close(); } catch {} }
} }
async function askViaDOStreaming(onDelta){ async function askViaDOStreaming(onDelta) {
const apiKey=store.apiKey, model=store.model; const apiKey = store.apiKey;
const msgs=[]; if(store.system_prompt) msgs.push({role:"system",content:[{type:"text",text:store.system_prompt}]}); const model = store.model;
msgs.push(...state.messages.filter(m=>m.role!=="system").map(m=>({role:m.role,content:m.content}))); const msgs = [];
if (store.system_prompt) msgs.push({ role: "system", content: [{ type: "text", text: store.system_prompt }] });
msgs.push(...state.messages.filter(m => m.role !== "system").map(m => ({ role: m.role, content: m.content })));
const runner=new SingleRunWS({payload:{apiKey,model,messages:msgs},onDelta}); const runner = new SingleRunWS({ payload: { apiKey, model, messages: msgs }, onDelta });
state.controller={abort:()=>runner.abort()}; state.controller = { abort: () => runner.abort() };
runner.start(); runner.start();
return {ok:true}; return { ok: true };
} }
askOpenRouterStreaming = askViaDOStreaming;
askOpenRouterStreaming=askViaDOStreaming;
</script> </script>
</body> </body>
</html> </html>