new internal communication system for openai api

This commit is contained in:
Mic 2024-08-23 14:03:25 +02:00
parent 9698eb1d01
commit c104edbf93
2 changed files with 46 additions and 55 deletions

View file

@ -33,10 +33,11 @@ let worker = null;
switch (llm) { switch (llm) {
case "chatgpt_api": case "chatgpt_api":
browser.runtime.sendMessage({command: "openai_api_ready_" + call_id, window_id: (await browser.windows.getCurrent()).id});
worker = new Worker('model-worker-openai.js', { type: 'module' }); worker = new Worker('model-worker-openai.js', { type: 'module' });
break; break;
case "ollama_api": { case "ollama_api": {
browser.runtime.sendMessage({command: "ollama_api_ready_"+call_id}); browser.runtime.sendMessage({command: "ollama_api_ready_" + call_id, window_id: (await browser.windows.getCurrent()).id});
worker = new Worker('model-worker-ollama.js', { type: 'module' }); worker = new Worker('model-worker-ollama.js', { type: 'module' });
break; break;
} }

View file

@ -215,36 +215,22 @@ async function openChatGPT(promptText, action, curr_tabId, prompt_name = '', do_
case 'chatgpt_api': case 'chatgpt_api':
// We are using the ChatGPT API // We are using the ChatGPT API
let newWindow2 = await browser.windows.create({
url: browser.runtime.getURL('api_webchat/index.html?llm='+prefs.connection_type), let rand_call_id = '_openai_' + generateCallID();
await browser.windows.create({
url: browser.runtime.getURL('api_webchat/index.html?llm='+prefs.connection_type+'&call_id='+rand_call_id),
type: "popup", type: "popup",
width: prefs.chatgpt_win_width, width: prefs.chatgpt_win_width,
height: prefs.chatgpt_win_height height: prefs.chatgpt_win_height
}); });
createdWindowID = newWindow2.id; browser.runtime.onMessage.addListener(async (message, sender, sendResponse) => {
let createdTab2 = newWindow2.tabs[0];
// if(await isThunderbird128OrGreater()){ if (message.command === "openai_api_ready_"+rand_call_id) {
// // Wait for tab loaded.
// await new Promise(resolve => { let newWindow2 = await browser.windows.get(message.window_id);
// const tabIsLoaded2 = tab => { let createdTab2 = newWindow2.tabs[0];
// return tab.status == "complete" && tab.url != "about:blank";
// };
// const listener2 = (tabId, changeInfo, updatedTab) => {
// if (tabIsLoaded2(updatedTab)) {
// browser.tabs.onUpdated.removeListener(listener2);
// resolve();
// }
// }
// // Early exit if loaded already
// if (tabIsLoaded2(createdTab2)) {
// resolve();
// } else {
// browser.tabs.onUpdated.addListener(listener2);
// }
// });
// }
let mailMessage = await browser.messageDisplay.getDisplayedMessage(curr_tabId); let mailMessage = await browser.messageDisplay.getDisplayedMessage(curr_tabId);
let mailMessageId2 = -1; let mailMessageId2 = -1;
@ -274,6 +260,9 @@ async function openChatGPT(promptText, action, curr_tabId, prompt_name = '', do_
} }
await new Promise(resolve => setTimeout(resolve, 500)); await new Promise(resolve => setTimeout(resolve, 500));
} }
browser.runtime.onMessage.removeListener(arguments.callee);
}
});
break; // chatgpt_api break; // chatgpt_api
case 'ollama_api': case 'ollama_api':
@ -281,10 +270,10 @@ async function openChatGPT(promptText, action, curr_tabId, prompt_name = '', do_
taLog.log("Ollama API window opening..."); taLog.log("Ollama API window opening...");
let rand_call_id = '_ollama_' + generateCallID(); let rand_call_id2 = '_ollama_' + generateCallID();
await browser.windows.create({ await browser.windows.create({
url: browser.runtime.getURL('api_webchat/index.html?llm='+prefs.connection_type+'&call_id='+rand_call_id), url: browser.runtime.getURL('api_webchat/index.html?llm='+prefs.connection_type+'&call_id='+rand_call_id2),
type: "popup", type: "popup",
width: prefs.chatgpt_win_width, width: prefs.chatgpt_win_width,
height: prefs.chatgpt_win_height height: prefs.chatgpt_win_height
@ -292,11 +281,11 @@ async function openChatGPT(promptText, action, curr_tabId, prompt_name = '', do_
browser.runtime.onMessage.addListener(async (message, sender, sendResponse) => { browser.runtime.onMessage.addListener(async (message, sender, sendResponse) => {
if (message.command === "ollama_api_ready_"+rand_call_id) { if (message.command === "ollama_api_ready_"+rand_call_id2) {
taLog.log("Ollama API window ready."); taLog.log("Ollama API window ready.");
let newWindow = await browser.windows.get(message.window_id); let newWindow3 = await browser.windows.get(message.window_id);
let createdTab3 = newWindow.tabs[0]; let createdTab3 = newWindow3.tabs[0];
let mailMessage3 = await browser.messageDisplay.getDisplayedMessage(curr_tabId); let mailMessage3 = await browser.messageDisplay.getDisplayedMessage(curr_tabId);
let mailMessageId3 = -1; let mailMessageId3 = -1;
@ -326,6 +315,7 @@ async function openChatGPT(promptText, action, curr_tabId, prompt_name = '', do_
} }
await new Promise(resolve => setTimeout(resolve, 500)); await new Promise(resolve => setTimeout(resolve, 500));
} }
browser.runtime.onMessage.removeListener(arguments.callee);
} }
}); });
break; // ollama_api break; // ollama_api