Merge pull request #29 from wagesj45/codex/fix-errors-loading-newest-add-on-version

Fix logger initialization and remove Services dependency
This commit is contained in:
Jordan Wages 2025-06-25 01:51:59 -05:00 committed by GitHub
commit af83ec1e55
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 47 additions and 18 deletions

View file

@ -18,26 +18,28 @@ async function sha256Hex(str) {
const buf = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(str)); const buf = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(str));
return Array.from(new Uint8Array(buf), b => b.toString(16).padStart(2, '0')).join(''); return Array.from(new Uint8Array(buf), b => b.toString(16).padStart(2, '0')).join('');
} }
// Startup
(async () => { (async () => {
logger = await import(browser.runtime.getURL("logger.js")); logger = await import(browser.runtime.getURL("logger.js"));
logger.aiLog("background.js loaded ready to classify", {debug: true});
try { try {
AiClassifier = await import(browser.runtime.getURL('modules/AiClassifier.js')); AiClassifier = await import(browser.runtime.getURL("modules/AiClassifier.js"));
logger.aiLog("AiClassifier imported", {debug: true}); logger.aiLog("AiClassifier imported", {debug: true});
} catch (e) { } catch (e) {
logger.aiLog("failed to import AiClassifier", {level: 'error'}, e); console.error("failed to import AiClassifier", e);
return;
} }
try { try {
const store = await browser.storage.local.get(["endpoint", "templateName", "customTemplate", "customSystemPrompt", "aiParams", "debugLogging", "aiRules"]); const store = await browser.storage.local.get(["endpoint", "templateName", "customTemplate", "customSystemPrompt", "aiParams", "debugLogging", "aiRules"]);
logger.setDebug(store.debugLogging); logger.setDebug(store.debugLogging);
AiClassifier.setConfig(store); await AiClassifier.setConfig(store);
aiRules = Array.isArray(store.aiRules) ? store.aiRules : []; aiRules = Array.isArray(store.aiRules) ? store.aiRules : [];
logger.aiLog("configuration loaded", {debug: true}, store); logger.aiLog("configuration loaded", {debug: true}, store);
} catch (err) { } catch (err) {
logger.aiLog("failed to load config", {level: 'error'}, err); logger.aiLog("failed to load config", {level: 'error'}, err);
} }
})();
logger.aiLog("background.js loaded ready to classify", {debug: true});
// Listen for messages from UI/devtools // Listen for messages from UI/devtools
browser.runtime.onMessage.addListener(async (msg) => { browser.runtime.onMessage.addListener(async (msg) => {
@ -109,3 +111,5 @@ browser.runtime.onInstalled.addListener(async ({ reason }) => {
await browser.runtime.openOptionsPage(); await browser.runtime.openOptionsPage();
} }
}); });
})();

View file

@ -1,6 +1,16 @@
"use strict"; "use strict";
import { aiLog, setDebug } from "../logger.js"; import { aiLog, setDebug } from "../logger.js";
const { Services } = globalThis || ChromeUtils.importESModule("resource://gre/modules/Services.sys.mjs");
let Services;
try {
if (typeof globalThis !== "undefined" && globalThis.Services) {
Services = globalThis.Services;
} else if (typeof ChromeUtils !== "undefined" && ChromeUtils.importESModule) {
({ Services } = ChromeUtils.importESModule("resource://gre/modules/Services.sys.mjs"));
}
} catch (e) {
Services = undefined;
}
const SYSTEM_PREFIX = `You are an email-classification assistant. const SYSTEM_PREFIX = `You are an email-classification assistant.
Read the email below and the classification criterion provided by the user. Read the email below and the classification criterion provided by the user.
@ -62,6 +72,9 @@ async function loadCache() {
function loadCacheSync() { function loadCacheSync() {
if (!gCacheLoaded) { if (!gCacheLoaded) {
if (!Services?.tm?.spinEventLoopUntil) {
throw new Error("loadCacheSync requires Services");
}
let done = false; let done = false;
loadCache().finally(() => { done = true; }); loadCache().finally(() => { done = true; });
Services.tm.spinEventLoopUntil(() => done); Services.tm.spinEventLoopUntil(() => done);
@ -95,6 +108,9 @@ async function loadTemplate(name) {
} }
function loadTemplateSync(name) { function loadTemplateSync(name) {
if (!Services?.tm?.spinEventLoopUntil) {
throw new Error("loadTemplateSync requires Services");
}
let text = ""; let text = "";
let done = false; let done = false;
loadTemplate(name).then(t => { text = t; }).catch(() => {}).finally(() => { done = true; }); loadTemplate(name).then(t => { text = t; }).catch(() => {}).finally(() => { done = true; });
@ -102,7 +118,7 @@ function loadTemplateSync(name) {
return text; return text;
} }
function setConfig(config = {}) { async function setConfig(config = {}) {
if (config.endpoint) { if (config.endpoint) {
gEndpoint = config.endpoint; gEndpoint = config.endpoint;
} }
@ -125,7 +141,13 @@ function setConfig(config = {}) {
if (typeof config.debugLogging === "boolean") { if (typeof config.debugLogging === "boolean") {
setDebug(config.debugLogging); setDebug(config.debugLogging);
} }
gTemplateText = gTemplateName === "custom" ? gCustomTemplate : loadTemplateSync(gTemplateName); if (gTemplateName === "custom") {
gTemplateText = gCustomTemplate;
} else if (Services?.tm?.spinEventLoopUntil) {
gTemplateText = loadTemplateSync(gTemplateName);
} else {
gTemplateText = await loadTemplate(gTemplateName);
}
aiLog(`[AiClassifier] Endpoint set to ${gEndpoint}`, {debug: true}); aiLog(`[AiClassifier] Endpoint set to ${gEndpoint}`, {debug: true});
aiLog(`[AiClassifier] Template set to ${gTemplateName}`, {debug: true}); aiLog(`[AiClassifier] Template set to ${gTemplateName}`, {debug: true});
} }
@ -180,6 +202,9 @@ function cacheResult(cacheKey, matched) {
} }
function classifyTextSync(text, criterion, cacheKey = null) { function classifyTextSync(text, criterion, cacheKey = null) {
if (!Services?.tm?.spinEventLoopUntil) {
throw new Error("classifyTextSync requires Services");
}
const cached = getCachedResult(cacheKey); const cached = getCachedResult(cacheKey);
if (cached !== null) { if (cached !== null) {
return cached; return cached;

View file

@ -147,7 +147,7 @@ document.addEventListener('DOMContentLoaded', async () => {
})).filter(r => r.criterion); })).filter(r => r.criterion);
await browser.storage.local.set({ endpoint, templateName, customTemplate: customTemplateText, customSystemPrompt, aiParams: aiParamsSave, debugLogging, aiRules: rules }); await browser.storage.local.set({ endpoint, templateName, customTemplate: customTemplateText, customSystemPrompt, aiParams: aiParamsSave, debugLogging, aiRules: rules });
try { try {
AiClassifier.setConfig({ endpoint, templateName, customTemplate: customTemplateText, customSystemPrompt, aiParams: aiParamsSave, debugLogging }); await AiClassifier.setConfig({ endpoint, templateName, customTemplate: customTemplateText, customSystemPrompt, aiParams: aiParamsSave, debugLogging });
logger.setDebug(debugLogging); logger.setDebug(debugLogging);
} catch (e) { } catch (e) {
logger.aiLog('[options] failed to apply config', {level: 'error'}, e); logger.aiLog('[options] failed to apply config', {level: 'error'}, e);