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,65 +215,54 @@ 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 => {
// const tabIsLoaded2 = tab => {
// 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 newWindow2 = await browser.windows.get(message.window_id);
let mailMessageId2 = -1; let createdTab2 = newWindow2.tabs[0];
if(mailMessage) mailMessageId2 = mailMessage.id;
// check if the config is present, or give a message error let mailMessage = await browser.messageDisplay.getDisplayedMessage(curr_tabId);
if (prefs.chatgpt_api_key == '') { let mailMessageId2 = -1;
browser.tabs.sendMessage(createdTab2.id, { command: "api_error", error: browser.i18n.getMessage('chatgpt_empty_apikey')}); if(mailMessage) mailMessageId2 = mailMessage.id;
return;
}
if (prefs.chatgpt_model == '') {
browser.tabs.sendMessage(createdTab2.id, { command: "api_error", error: browser.i18n.getMessage('chatgpt_empty_model')});
return;
}
let done2 = false; // check if the config is present, or give a message error
if (prefs.chatgpt_api_key == '') {
browser.tabs.sendMessage(createdTab2.id, { command: "api_error", error: browser.i18n.getMessage('chatgpt_empty_apikey')});
return;
}
if (prefs.chatgpt_model == '') {
browser.tabs.sendMessage(createdTab2.id, { command: "api_error", error: browser.i18n.getMessage('chatgpt_empty_model')});
return;
}
while(!done2){ let done2 = false;
try {
browser.tabs.sendMessage(createdTab2.id, { command: "api_send", prompt: promptText, action: action, tabId: curr_tabId, mailMessageId: mailMessageId2, do_custom_text: do_custom_text}); while(!done2){
taLog.log('[OpenAI ChatGPI] Connection succeded!'); try {
done2 = true; browser.tabs.sendMessage(createdTab2.id, { command: "api_send", prompt: promptText, action: action, tabId: curr_tabId, mailMessageId: mailMessageId2, do_custom_text: do_custom_text});
} catch (error) { taLog.log('[OpenAI ChatGPI] Connection succeded!');
taLog.warn('[OpenAI ChatGPI API] Error connecting to the window chat: ', error); done2 = true;
taLog.warn('[OpenAI ChatGPI API] Trying again...'); } catch (error) {
done2 = false; taLog.warn('[OpenAI ChatGPI API] Error connecting to the window chat: ', error);
taLog.warn('[OpenAI ChatGPI API] Trying again...');
done2 = false;
}
await new Promise(resolve => setTimeout(resolve, 500));
}
browser.runtime.onMessage.removeListener(arguments.callee);
} }
await new Promise(resolve => setTimeout(resolve, 500)); });
}
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,15 +281,15 @@ 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;
if(mailMessage3) mailMessageId3 = mailMessage3.id; if(mailMessage3) mailMessageId3 = mailMessage3.id;
// check if the config is present, or give a message error // check if the config is present, or give a message error
if (prefs.ollama_host == '') { if (prefs.ollama_host == '') {
@ -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