Compare commits

...

10 commits

Author SHA1 Message Date
mic
c270586071 logs improved 2024-10-03 19:06:47 +02:00
mic
6dc2638de0 version set to 2.2.0_i145_v4 2024-10-03 19:06:13 +02:00
mic
954e6c55d8 version set to 2.2.0_i145_v3 2024-10-02 23:26:59 +02:00
mic
e702ab9c1a init with api key 2024-10-02 23:26:36 +02:00
mic
e85ecc9e50 using openaicomp apikey 2024-10-02 23:24:46 +02:00
mic
589c8fef5b managing broken chunck 2024-10-02 23:23:57 +02:00
mic
a924f946a5 version set to 2.2.0_i145_v2 2024-10-01 22:48:00 +02:00
mic
2d02bc1bb8 trying to get the missing chunk part 2024-10-01 22:47:39 +02:00
mic
59b3bd5019 printing lines to the log 2024-10-01 21:51:55 +02:00
mic
b5efe247cf version set to 2.2.0_i145_v1 2024-10-01 21:49:24 +02:00
5 changed files with 45 additions and 16 deletions

View file

@ -97,13 +97,13 @@ switch (llm) {
break; break;
} }
case "openai_comp_api": { case "openai_comp_api": {
let prefs_api = await browser.storage.sync.get({openai_comp_host: '', openai_comp_model: '', openai_comp_chat_name: ''}); let prefs_api = await browser.storage.sync.get({openai_comp_host: '', openai_comp_model: '', openai_comp_api_key: '', openai_comp_chat_name: ''});
let i18nStrings = {}; let i18nStrings = {};
i18nStrings["OpenAIComp_api_request_failed"] = browser.i18n.getMessage('OpenAIComp_api_request_failed'); i18nStrings["OpenAIComp_api_request_failed"] = browser.i18n.getMessage('OpenAIComp_api_request_failed');
i18nStrings["error_connection_interrupted"] = browser.i18n.getMessage('error_connection_interrupted'); i18nStrings["error_connection_interrupted"] = browser.i18n.getMessage('error_connection_interrupted');
messageInput.setModel(prefs_api.openai_comp_model); messageInput.setModel(prefs_api.openai_comp_model);
messagesArea.setLLMName(prefs_api.openai_comp_chat_name); messagesArea.setLLMName(prefs_api.openai_comp_chat_name);
worker.postMessage({ type: 'init', openai_comp_host: prefs_api.openai_comp_host, openai_comp_model: prefs_api.openai_comp_model, i18nStrings: i18nStrings}); worker.postMessage({ type: 'init', openai_comp_host: prefs_api.openai_comp_host, openai_comp_model: prefs_api.openai_comp_model, openai_comp_api_key: prefs_api.openai_comp_api_key, i18nStrings: i18nStrings});
messagesArea.appendUserMessage(browser.i18n.getMessage("OpenAIComp_api_connecting") + " \"" + prefs_api.openai_comp_host + "\" " +browser.i18n.getMessage("AndModel") + " \"" + prefs_api.openai_comp_model + "\"...", "info"); messagesArea.appendUserMessage(browser.i18n.getMessage("OpenAIComp_api_connecting") + " \"" + prefs_api.openai_comp_host + "\" " +browser.i18n.getMessage("AndModel") + " \"" + prefs_api.openai_comp_model + "\"...", "info");
break; break;
} }

View file

@ -106,6 +106,7 @@ self.onmessage = async function(event) {
const reader = response.body.getReader(); const reader = response.body.getReader();
const decoder = new TextDecoder("utf-8"); const decoder = new TextDecoder("utf-8");
let chunk = '';
while (true) { while (true) {
if (stopStreaming) { if (stopStreaming) {
@ -124,12 +125,25 @@ self.onmessage = async function(event) {
break; break;
} }
// lots of low-level OpenAI response parsing stuff // lots of low-level OpenAI response parsing stuff
const chunk = decoder.decode(value); chunk += decoder.decode(value);
console.log(">>>>>>>>>>>>>> [ThunderAI] chunk: " + JSON.stringify(chunk));
const lines = chunk.split("\n"); const lines = chunk.split("\n");
const parsedLines = lines let parsedLines = [];
.map((line) => line.replace(/^data: /, "").trim()) // Remove the "data: " prefix try{
.filter((line) => line !== "" && line !== "[DONE]") // Remove empty lines and "[DONE]" parsedLines = lines
.map((line) => JSON.parse(line)); // Parse the JSON string .map((line) => line.replace(/^data: /, "").trim()) // Remove the "data: " prefix
.filter((line) => line !== "" && line !== "[DONE]") // Remove empty lines and "[DONE]"
// .map((line) => JSON.parse(line)); // Parse the JSON string
.map((line) => {
console.log(">>>>>>>>>>>>> [ThunderAI] line: " + JSON.stringify(line));
return JSON.parse(line);
});
chunk = chunk.substring(chunk.lastIndexOf('\n') + 1);
console.log(">>>>>>>>>>>>>> [ThunderAI] last chunk: " + JSON.stringify(chunk));
}catch(e){
console.log(">>>>>>>>>>>>>> [ThunderAI] last chunk: " + JSON.stringify(chunk));
console.error(">>>>>>>>>>>>> [ThunderAI] error: " + e);
}
for (const parsedLine of parsedLines) { for (const parsedLine of parsedLines) {
const { choices } = parsedLine; const { choices } = parsedLine;

View file

@ -24,6 +24,7 @@ import { OpenAIComp } from '../js/api/openai_comp.js';
let openai_comp_host = null; let openai_comp_host = null;
let openai_comp_model = ''; let openai_comp_model = '';
let openai_comp_api_key = '';
let openai_comp = null; let openai_comp = null;
let stopStreaming = false; let stopStreaming = false;
@ -34,11 +35,12 @@ self.onmessage = async function(event) {
if (event.data.type === 'init') { if (event.data.type === 'init') {
openai_comp_host = event.data.openai_comp_host; openai_comp_host = event.data.openai_comp_host;
openai_comp_model = event.data.openai_comp_model; openai_comp_model = event.data.openai_comp_model;
openai_comp = new OpenAIComp(openai_comp_host, openai_comp_model, true); openai_comp_api_key = event.data.openai_comp_api_key;
openai_comp = new OpenAIComp(openai_comp_host, openai_comp_model, openai_comp_api_key, true);
} else if (event.data.type === 'chatMessage') { } else if (event.data.type === 'chatMessage') {
conversationHistory.push({ role: 'user', content: event.data.message }); conversationHistory.push({ role: 'user', content: event.data.message });
const response = await openai.fetchResponse(conversationHistory); //4096); const response = await openai_comp.fetchResponse(conversationHistory); //4096);
postMessage({ type: 'messageSent' }); postMessage({ type: 'messageSent' });
if (!response.ok) { if (!response.ok) {
@ -58,6 +60,7 @@ self.onmessage = async function(event) {
const reader = response.body.getReader(); const reader = response.body.getReader();
const decoder = new TextDecoder("utf-8"); const decoder = new TextDecoder("utf-8");
let chunk = '';
while (true) { while (true) {
if (stopStreaming) { if (stopStreaming) {
@ -76,12 +79,24 @@ self.onmessage = async function(event) {
break; break;
} }
// lots of low-level OpenAI response parsing stuff // lots of low-level OpenAI response parsing stuff
const chunk = decoder.decode(value); chunk += decoder.decode(value);
console.log(">>>>>>>>>>>>>> [ThunderAI] chunk: " + JSON.stringify(chunk));
const lines = chunk.split("\n"); const lines = chunk.split("\n");
const parsedLines = lines let parsedLines = [];
.map((line) => line.replace(/^data: /, "").trim()) // Remove the "data: " prefix try{
.filter((line) => line !== "" && line !== "[DONE]") // Remove empty lines and "[DONE]" parsedLines = lines
.map((line) => JSON.parse(line)); // Parse the JSON string .map((line) => line.replace(/^data: /, "").trim()) // Remove the "data: " prefix
.filter((line) => line !== "" && line !== "[DONE]") // Remove empty lines and "[DONE]"
// .map((line) => JSON.parse(line)); // Parse the JSON string
.map((line) => {
console.log(">>>>>>>>>>>>> [ThunderAI] line: " + JSON.stringify(line));
return JSON.parse(line);
});
chunk = chunk.substring(chunk.lastIndexOf('\n') + 1);
console.log(">>>>>>>>>>>>>> [ThunderAI] last chunk: " + JSON.stringify(chunk));
}catch(e){
console.error(">>>>>>>>>>>>> [ThunderAI] error: " + JSON.stringify(e));
}
for (const parsedLine of parsedLines) { for (const parsedLine of parsedLines) {
const { choices } = parsedLine; const { choices } = parsedLine;

View file

@ -2,7 +2,7 @@
"manifest_version": 2, "manifest_version": 2,
"name": "ThunderAI", "name": "ThunderAI",
"description": "__MSG_extensionDescription__", "description": "__MSG_extensionDescription__",
"version": "2.2.0pre4", "version": "2.2.0_i145_v4",
"author": "Mic (m@micz.it)", "author": "Mic (m@micz.it)",
"homepage_url": "https://micz.it/thunderbird-addon-thunderai/", "homepage_url": "https://micz.it/thunderbird-addon-thunderai/",
"browser_specific_settings": { "browser_specific_settings": {

View file

@ -330,7 +330,7 @@ select_openai_comp_model.addEventListener("change", warn_OpenAIComp_HostEmpty);
document.getElementById('btnUpdateOpenAICompModels').addEventListener('click', async () => { document.getElementById('btnUpdateOpenAICompModels').addEventListener('click', async () => {
document.getElementById('openai_comp_model_fetch_loading').style.display = 'inline'; document.getElementById('openai_comp_model_fetch_loading').style.display = 'inline';
let openai_comp = new OpenAIComp(document.getElementById("openai_comp_host").value, true); let openai_comp = new OpenAIComp(document.getElementById("openai_comp_host").value , null, document.getElementById("openai_comp_api_key").value, true);
openai_comp.fetchModels().then((data) => { openai_comp.fetchModels().then((data) => {
if(!data.ok){ if(!data.ok){
let errorDetail = JSON.parse(data.error); let errorDetail = JSON.parse(data.error);