diff --git a/AGENTS.md b/AGENTS.md index 3e3cba4..f2ea2d9 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -34,6 +34,7 @@ There are currently no automated tests for this project. If you add tests in the ## Endpoint Notes Sortana targets the `/v1/completions` API. The endpoint value stored in settings is a base URL; the full request URL is constructed by appending `/v1/completions` (adding a slash when needed) and defaulting to `https://` if no scheme is provided. +The options page can query `/v1/models` from the same base URL to populate the Model dropdown; selecting **None** omits the `model` field from the request payload. Responses are expected to include a JSON object with `match` (or `matched`) plus a short `reason` string; the parser extracts the last JSON object in the response text and ignores any surrounding commentary. ## Documentation diff --git a/README.md b/README.md index 8779d1a..957e5e0 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ expecting a `match` (or `matched`) boolean plus a `reason` string. ## Features - **Configurable endpoint** – set the classification service base URL on the options page. +- **Model selection** – load available models from the endpoint and choose one (or omit the model field). - **Prompt templates** – choose between OpenAI/ChatML, Qwen, Mistral, Harmony (gpt-oss), or provide your own custom template. - **Custom system prompts** – tailor the instructions sent to the model for more precise results. - **Persistent result caching** – classification results and reasoning are saved to disk so messages aren't re-evaluated across restarts. @@ -79,7 +80,8 @@ Sortana is implemented entirely with standard WebExtension scripts—no custom e ## Usage 1. Open the add-on's options and set the base URL of your classification service - (Sortana will append `/v1/completions`). + (Sortana will append `/v1/completions`). Use the Model dropdown to load + `/v1/models` and select a model or choose **None** to omit the `model` field. 2. Use the **Classification Rules** section to add a criterion and optional actions such as tagging, moving, copying, forwarding, replying, deleting or archiving a message when it matches. Drag rules to diff --git a/background.js b/background.js index 64aad50..aef8cbb 100644 --- a/background.js +++ b/background.js @@ -484,7 +484,7 @@ async function clearCacheForMessages(idsInput) { } try { - const store = await storage.local.get(["endpoint", "templateName", "customTemplate", "customSystemPrompt", "aiParams", "debugLogging", "htmlToMarkdown", "stripUrlParams", "altTextImages", "collapseWhitespace", "tokenReduction", "aiRules", "theme", "showDebugTab"]); + const store = await storage.local.get(["endpoint", "model", "templateName", "customTemplate", "customSystemPrompt", "aiParams", "debugLogging", "htmlToMarkdown", "stripUrlParams", "altTextImages", "collapseWhitespace", "tokenReduction", "aiRules", "theme", "showDebugTab"]); logger.setDebug(store.debugLogging); await AiClassifier.setConfig(store); userTheme = store.theme || 'auto'; @@ -514,9 +514,10 @@ async function clearCacheForMessages(idsInput) { aiRules = normalizeRules(newRules); logger.aiLog("aiRules updated from storage change", { debug: true }, aiRules); } - if (changes.endpoint || changes.templateName || changes.customTemplate || changes.customSystemPrompt || changes.aiParams || changes.debugLogging) { + if (changes.endpoint || changes.model || changes.templateName || changes.customTemplate || changes.customSystemPrompt || changes.aiParams || changes.debugLogging) { const config = {}; if (changes.endpoint) config.endpoint = changes.endpoint.newValue; + if (changes.model) config.model = changes.model.newValue; if (changes.templateName) config.templateName = changes.templateName.newValue; if (changes.customTemplate) config.customTemplate = changes.customTemplate.newValue; if (changes.customSystemPrompt) config.customSystemPrompt = changes.customSystemPrompt.newValue; diff --git a/manifest.json b/manifest.json index e7cb9d8..a18e7cd 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 2, "name": "Sortana", - "version": "2.2.0", + "version": "2.4.0", "default_locale": "en-US", "applications": { "gecko": { diff --git a/modules/AiClassifier.js b/modules/AiClassifier.js index 603c088..cb68382 100644 --- a/modules/AiClassifier.js +++ b/modules/AiClassifier.js @@ -16,6 +16,7 @@ try { } const COMPLETIONS_PATH = "/v1/completions"; +const MODELS_PATH = "/v1/models"; const SYSTEM_PREFIX = `You are an email-classification assistant. Read the email below and the classification criterion provided by the user. @@ -38,6 +39,7 @@ let gCustomSystemPrompt = DEFAULT_CUSTOM_SYSTEM_PROMPT; let gTemplateText = ""; let gAiParams = Object.assign({}, DEFAULT_AI_PARAMS); +let gModel = ""; let gCache = new Map(); let gCacheLoaded = false; @@ -50,7 +52,7 @@ function normalizeEndpointBase(endpoint) { if (!base) { return ""; } - base = base.replace(/\/v1\/completions\/?$/i, ""); + base = base.replace(/\/v1\/(completions|models)\/?$/i, ""); return base; } @@ -61,7 +63,19 @@ function buildEndpointUrl(endpointBase) { } const withScheme = /^https?:\/\//i.test(base) ? base : `https://${base}`; const needsSlash = withScheme.endsWith("/"); - return `${withScheme}${needsSlash ? "" : "/"}v1/completions`; + const path = COMPLETIONS_PATH.replace(/^\//, ""); + return `${withScheme}${needsSlash ? "" : "/"}${path}`; +} + +function buildModelsUrl(endpointBase) { + const base = normalizeEndpointBase(endpointBase); + if (!base) { + return ""; + } + const withScheme = /^https?:\/\//i.test(base) ? base : `https://${base}`; + const needsSlash = withScheme.endsWith("/"); + const path = MODELS_PATH.replace(/^\//, ""); + return `${withScheme}${needsSlash ? "" : "/"}${path}`; } function sha256HexSync(str) { @@ -206,6 +220,9 @@ async function setConfig(config = {}) { } } } + if (typeof config.model === "string") { + gModel = config.model.trim(); + } if (typeof config.debugLogging === "boolean") { setDebug(config.debugLogging); } @@ -263,6 +280,9 @@ function buildPayload(text, criterion) { let payloadObj = Object.assign({ prompt: buildPrompt(text, criterion) }, gAiParams); + if (gModel) { + payloadObj.model = gModel; + } return JSON.stringify(payloadObj); } @@ -457,4 +477,4 @@ async function init() { await loadCache(); } -export { buildEndpointUrl, normalizeEndpointBase, classifyText, setConfig, removeCacheEntries, clearCache, getReason, getCachedResult, buildCacheKey, getCacheSize, init }; +export { buildEndpointUrl, buildModelsUrl, normalizeEndpointBase, classifyText, setConfig, removeCacheEntries, clearCache, getReason, getCachedResult, buildCacheKey, getCacheSize, init }; diff --git a/options/dataTransfer.js b/options/dataTransfer.js index b289c02..fdf096f 100644 --- a/options/dataTransfer.js +++ b/options/dataTransfer.js @@ -3,6 +3,7 @@ const storage = (globalThis.messenger ?? browser).storage; const KEY_GROUPS = { settings: [ 'endpoint', + 'model', 'templateName', 'customTemplate', 'customSystemPrompt', diff --git a/options/options.html b/options/options.html index fd8700c..4c1fd79 100644 --- a/options/options.html +++ b/options/options.html @@ -77,6 +77,21 @@
+