Merge pull request #106 from wagesj45/codex/remove-context-length-option-and-adjust-truncation

Update context trimming logic
This commit is contained in:
Jordan Wages 2025-07-19 18:42:57 -05:00 committed by GitHub
commit 3d5050ded0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 15 additions and 23 deletions

View file

@ -19,8 +19,7 @@
"options.stripUrlParams": { "message": "Remove URL tracking parameters" }, "options.stripUrlParams": { "message": "Remove URL tracking parameters" },
"options.altTextImages": { "message": "Replace images with alt text" }, "options.altTextImages": { "message": "Replace images with alt text" },
"options.collapseWhitespace": { "message": "Collapse long whitespace" }, "options.collapseWhitespace": { "message": "Collapse long whitespace" },
"options.tokenReduction": { "message": "Aggressive token reduction" }, "options.tokenReduction": { "message": "Aggressive token reduction" }
"options.contextLength": { "message": "Context length" }
,"action.read": { "message": "read" } ,"action.read": { "message": "read" }
,"action.flag": { "message": "flag" } ,"action.flag": { "message": "flag" }
,"action.copy": { "message": "copy" } ,"action.copy": { "message": "copy" }

View file

@ -27,7 +27,7 @@ let stripUrlParams = false;
let altTextImages = false; let altTextImages = false;
let collapseWhitespace = false; let collapseWhitespace = false;
let tokenReduction = false; let tokenReduction = false;
let contextLength = 16384; let maxTokens = 4096;
let TurndownService = null; let TurndownService = null;
let userTheme = 'auto'; let userTheme = 'auto';
let currentTheme = 'light'; let currentTheme = 'light';
@ -262,8 +262,8 @@ async function processMessage(id) {
try { try {
const full = await messenger.messages.getFull(id); const full = await messenger.messages.getFull(id);
let text = buildEmailText(full); let text = buildEmailText(full);
if (tokenReduction && contextLength > 0) { if (tokenReduction && maxTokens > 0) {
const limit = Math.floor(contextLength * 0.9); const limit = Math.floor(maxTokens * 0.9);
if (text.length > limit) { if (text.length > limit) {
text = text.slice(0, limit); text = text.slice(0, limit);
} }
@ -425,7 +425,7 @@ async function clearCacheForMessages(idsInput) {
} }
try { try {
const store = await storage.local.get(["endpoint", "templateName", "customTemplate", "customSystemPrompt", "aiParams", "debugLogging", "htmlToMarkdown", "stripUrlParams", "altTextImages", "collapseWhitespace", "tokenReduction", "contextLength", "aiRules", "theme", "errorPending"]); const store = await storage.local.get(["endpoint", "templateName", "customTemplate", "customSystemPrompt", "aiParams", "debugLogging", "htmlToMarkdown", "stripUrlParams", "altTextImages", "collapseWhitespace", "tokenReduction", "aiRules", "theme", "errorPending"]);
logger.setDebug(store.debugLogging); logger.setDebug(store.debugLogging);
await AiClassifier.setConfig(store); await AiClassifier.setConfig(store);
userTheme = store.theme || 'auto'; userTheme = store.theme || 'auto';
@ -436,7 +436,9 @@ async function clearCacheForMessages(idsInput) {
altTextImages = store.altTextImages === true; altTextImages = store.altTextImages === true;
collapseWhitespace = store.collapseWhitespace === true; collapseWhitespace = store.collapseWhitespace === true;
tokenReduction = store.tokenReduction === true; tokenReduction = store.tokenReduction === true;
contextLength = parseInt(store.contextLength) || contextLength; if (store.aiParams && typeof store.aiParams.max_tokens !== 'undefined') {
maxTokens = parseInt(store.aiParams.max_tokens) || maxTokens;
}
errorPending = store.errorPending === true; errorPending = store.errorPending === true;
const savedStats = await storage.local.get('classifyStats'); const savedStats = await storage.local.get('classifyStats');
if (savedStats.classifyStats && typeof savedStats.classifyStats === 'object') { if (savedStats.classifyStats && typeof savedStats.classifyStats === 'object') {
@ -459,7 +461,12 @@ async function clearCacheForMessages(idsInput) {
if (changes.templateName) config.templateName = changes.templateName.newValue; if (changes.templateName) config.templateName = changes.templateName.newValue;
if (changes.customTemplate) config.customTemplate = changes.customTemplate.newValue; if (changes.customTemplate) config.customTemplate = changes.customTemplate.newValue;
if (changes.customSystemPrompt) config.customSystemPrompt = changes.customSystemPrompt.newValue; if (changes.customSystemPrompt) config.customSystemPrompt = changes.customSystemPrompt.newValue;
if (changes.aiParams) config.aiParams = changes.aiParams.newValue; if (changes.aiParams) {
config.aiParams = changes.aiParams.newValue;
if (changes.aiParams.newValue && typeof changes.aiParams.newValue.max_tokens !== 'undefined') {
maxTokens = parseInt(changes.aiParams.newValue.max_tokens) || maxTokens;
}
}
if (changes.debugLogging) { if (changes.debugLogging) {
config.debugLogging = changes.debugLogging.newValue === true; config.debugLogging = changes.debugLogging.newValue === true;
logger.setDebug(config.debugLogging); logger.setDebug(config.debugLogging);
@ -487,10 +494,6 @@ async function clearCacheForMessages(idsInput) {
tokenReduction = changes.tokenReduction.newValue === true; tokenReduction = changes.tokenReduction.newValue === true;
logger.aiLog("tokenReduction updated from storage change", { debug: true }, tokenReduction); logger.aiLog("tokenReduction updated from storage change", { debug: true }, tokenReduction);
} }
if (changes.contextLength) {
contextLength = parseInt(changes.contextLength.newValue) || contextLength;
logger.aiLog("contextLength updated from storage change", { debug: true }, contextLength);
}
if (changes.errorPending) { if (changes.errorPending) {
errorPending = changes.errorPending.newValue === true; errorPending = changes.errorPending.newValue === true;
updateActionIcon(); updateActionIcon();

View file

@ -149,12 +149,6 @@
<input type="checkbox" id="token-reduction"> Aggressive token reduction <input type="checkbox" id="token-reduction"> Aggressive token reduction
</label> </label>
</div> </div>
<div class="field">
<label class="label" for="context-length">Context length</label>
<div class="control">
<input class="input" type="number" id="context-length">
</div>
</div>
<div class="field"> <div class="field">
<label class="label" for="max_tokens">Max tokens</label> <label class="label" for="max_tokens">Max tokens</label>
<div class="control"> <div class="control">

View file

@ -17,7 +17,6 @@ document.addEventListener('DOMContentLoaded', async () => {
'altTextImages', 'altTextImages',
'collapseWhitespace', 'collapseWhitespace',
'tokenReduction', 'tokenReduction',
'contextLength',
'aiRules', 'aiRules',
'aiCache', 'aiCache',
'theme' 'theme'
@ -120,8 +119,6 @@ document.addEventListener('DOMContentLoaded', async () => {
const tokenReductionToggle = document.getElementById('token-reduction'); const tokenReductionToggle = document.getElementById('token-reduction');
tokenReductionToggle.checked = defaults.tokenReduction === true; tokenReductionToggle.checked = defaults.tokenReduction === true;
const contextLengthInput = document.getElementById('context-length');
contextLengthInput.value = defaults.contextLength || 16384;
const aiParams = Object.assign({}, DEFAULT_AI_PARAMS, defaults.aiParams || {}); const aiParams = Object.assign({}, DEFAULT_AI_PARAMS, defaults.aiParams || {});
for (const [key, val] of Object.entries(aiParams)) { for (const [key, val] of Object.entries(aiParams)) {
@ -800,9 +797,8 @@ document.addEventListener('DOMContentLoaded', async () => {
const altTextImages = altTextToggle.checked; const altTextImages = altTextToggle.checked;
const collapseWhitespace = collapseWhitespaceToggle.checked; const collapseWhitespace = collapseWhitespaceToggle.checked;
const tokenReduction = tokenReductionToggle.checked; const tokenReduction = tokenReductionToggle.checked;
const contextLength = parseInt(contextLengthInput.value) || 0;
const theme = themeSelect.value; const theme = themeSelect.value;
await storage.local.set({ endpoint, templateName, customTemplate: customTemplateText, customSystemPrompt, aiParams: aiParamsSave, debugLogging, htmlToMarkdown, stripUrlParams, altTextImages, collapseWhitespace, tokenReduction, contextLength, aiRules: rules, theme }); await storage.local.set({ endpoint, templateName, customTemplate: customTemplateText, customSystemPrompt, aiParams: aiParamsSave, debugLogging, htmlToMarkdown, stripUrlParams, altTextImages, collapseWhitespace, tokenReduction, aiRules: rules, theme });
await applyTheme(theme); await applyTheme(theme);
try { try {
await AiClassifier.setConfig({ endpoint, templateName, customTemplate: customTemplateText, customSystemPrompt, aiParams: aiParamsSave, debugLogging }); await AiClassifier.setConfig({ endpoint, templateName, customTemplate: customTemplateText, customSystemPrompt, aiParams: aiParamsSave, debugLogging });