Compare commits

...

1 commit

Author SHA1 Message Date
Mic
31914ada82 trying to use fetch from the apiwebchat and not the worker 2025-06-03 22:43:00 +02:00
3 changed files with 83 additions and 2 deletions

View file

@ -137,7 +137,7 @@ switch (llm) {
//let prefs_ph = await browser.storage.sync.get({placeholders_use_default_value: false}); //let prefs_ph = await browser.storage.sync.get({placeholders_use_default_value: false});
// Event listeners for worker messages // Event listeners for worker messages
worker.onmessage = function(event) { worker.onmessage = async function(event) {
const { type, payload } = event.data; const { type, payload } = event.data;
switch (type) { switch (type) {
case 'messageSent': case 'messageSent':
@ -155,6 +155,41 @@ worker.onmessage = function(event) {
messagesArea.appendBotMessage(payload,'error'); messagesArea.appendBotMessage(payload,'error');
messageInput.enableInput(); messageInput.enableInput();
break; break;
case 'proxy-fetch': {
const [url, options] = event.data.args;
const requestId = event.data.requestId;
try {
const res = await fetch(url, options);
const body = await res.text();
const headersObj = {};
for (const [key, value] of res.headers.entries()) {
headersObj[key.toLowerCase()] = value;
}
worker.postMessage({
type: 'proxy-fetch-response',
requestId,
response: {
body,
status: res.status,
statusText: res.statusText,
ok: res.ok,
headers: headersObj,
}
});
} catch (err) {
worker.postMessage({
type: 'proxy-fetch-response',
requestId,
response: {
body: err.message,
status: 500,
statusText: 'Internal Error',
ok: false,
headers: {},
}
});
}
}
default: default:
console.error('[ThunderAI] Unknown event type from API worker:', type); console.error('[ThunderAI] Unknown event type from API worker:', type);
} }

View file

@ -73,6 +73,7 @@ export class OpenAIComp {
if(this.apiKey !== '') curr_headers["Authorization"] = "Bearer "+ this.apiKey; if(this.apiKey !== '') curr_headers["Authorization"] = "Bearer "+ this.apiKey;
try { try {
console.log(">>>>>>>>>>> OpenAI API Comp request: ", this.host + (this.use_v1 ? "/v1" : "") + "/chat/completions");
const response = await fetch(this.host + (this.use_v1 ? "/v1" : "") + "/chat/completions", { const response = await fetch(this.host + (this.use_v1 ? "/v1" : "") + "/chat/completions", {
method: "POST", method: "POST",
headers: curr_headers, headers: curr_headers,

View file

@ -1,3 +1,48 @@
// --- Patch fetch in the worker to delegate to main thread with streaming support ---
let fetchCounter = 0;
self.fetch = (...args) => {
const requestId = 'fetch_' + (++fetchCounter);
postMessage({ type: 'proxy-fetch', requestId, args });
return new Promise((resolve) => {
if (!self._fetchResolvers) self._fetchResolvers = {};
self._fetchResolvers[requestId] = resolve;
});
};
onmessage = (e) => {
if (e.data.type === 'proxy-fetch-response') {
const { requestId, response } = e.data;
const resolve = self._fetchResolvers?.[requestId];
if (resolve) {
const stream = new ReadableStream({
start(controller) {
controller.enqueue(new TextEncoder().encode(response.body));
controller.close();
}
});
const fakeResponse = {
ok: response.ok,
status: response.status,
statusText: response.statusText,
headers: {
get: (name) => response.headers[name.toLowerCase()] || null
},
body: stream,
text: async () => response.body,
json: async () => JSON.parse(response.body)
};
resolve(fakeResponse);
delete self._fetchResolvers[requestId];
}
}
};
// --- End patch ---
/* /*
* ThunderAI [https://micz.it/thunderbird-addon-thunderai/] * ThunderAI [https://micz.it/thunderbird-addon-thunderai/]
* Copyright (C) 2024 - 2025 Mic (m@micz.it) * Copyright (C) 2024 - 2025 Mic (m@micz.it)