Compare commits
1 commit
main
...
3.5.2_issu
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
31914ada82 |
3 changed files with 83 additions and 2 deletions
|
|
@ -137,7 +137,7 @@ switch (llm) {
|
|||
//let prefs_ph = await browser.storage.sync.get({placeholders_use_default_value: false});
|
||||
|
||||
// Event listeners for worker messages
|
||||
worker.onmessage = function(event) {
|
||||
worker.onmessage = async function(event) {
|
||||
const { type, payload } = event.data;
|
||||
switch (type) {
|
||||
case 'messageSent':
|
||||
|
|
@ -155,6 +155,41 @@ worker.onmessage = function(event) {
|
|||
messagesArea.appendBotMessage(payload,'error');
|
||||
messageInput.enableInput();
|
||||
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:
|
||||
console.error('[ThunderAI] Unknown event type from API worker:', type);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -73,6 +73,7 @@ export class OpenAIComp {
|
|||
if(this.apiKey !== '') curr_headers["Authorization"] = "Bearer "+ this.apiKey;
|
||||
|
||||
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", {
|
||||
method: "POST",
|
||||
headers: curr_headers,
|
||||
|
|
|
|||
|
|
@ -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/]
|
||||
* Copyright (C) 2024 - 2025 Mic (m@micz.it)
|
||||
|
|
|
|||
Loading…
Reference in a new issue