From 1798dfd2b174d20ec94790f292145c7bca309256 Mon Sep 17 00:00:00 2001 From: shatfield4 Date: Thu, 6 Nov 2025 15:16:54 -0800 Subject: [PATCH 1/3] enhance web-browsing agent tool with current date for recency queries --- .../agents/aibitat/plugins/web-browsing.js | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/server/utils/agents/aibitat/plugins/web-browsing.js b/server/utils/agents/aibitat/plugins/web-browsing.js index 2825b106815..97d64502ccd 100644 --- a/server/utils/agents/aibitat/plugins/web-browsing.js +++ b/server/utils/agents/aibitat/plugins/web-browsing.js @@ -61,6 +61,15 @@ const webBrowsing = { * https://programmablesearchengine.google.com/controlpanel/create */ search: async function (query) { + // Augment query with date context if it's asking for recent information + if (this.detectRecentQuery(query)) { + const originalQuery = query; + query = this.augmentQueryWithDate(query); + this.super.introspect( + `${this.caller}: Enhanced query for recency: "${originalQuery}" → "${query}"` + ); + } + const provider = (await SystemSettings.get({ label: "agent_search_provider" })) ?.value ?? "unknown"; @@ -111,6 +120,62 @@ const webBrowsing = { return `${str.slice(0, length)}...${str.slice(-length)}`; }, + /** + * Detects if search query asks for recent/latest/current information + * @param {string} query - Search query to analyze + * @returns {boolean} True if the query indicates need for recent results + */ + detectRecentQuery(query) { + const recentKeywords = [ + "today", + "latest", + "recent", + "current", + "this week", + "this month", + "breaking", + "new", + "updated", + "updates", + ]; + const lowerQuery = query.toLowerCase(); + return recentKeywords.some((keyword) => + lowerQuery.includes(keyword) + ); + }, + + /** + * Strips date references that LLMs may have added based on their training cutoff date + * @param {string} query - Search query + * @returns {string} Search query with date references removed + */ + stripDateReferences(query) { + // Remove date patterns "January 2025", "2024", "December 2024", etc + return query + .replace( + /\b(January|February|March|April|May|June|July|August|September|October|November|December)\s+\d{4}\b/gi, + "" + ) + .replace(/\b\d{4}\b/g, "") + .replace(/\s{2,}/g, " ") + .trim(); + }, + + /** + * Augment query with current date for better recency results + * @param {string} query - The original search query + * @returns {string} Query with date context appended + */ + augmentQueryWithDate(query) { + const cleanQuery = this.stripDateReferences(query); + const now = new Date(); + const monthYear = now.toLocaleDateString("en-US", { + month: "long", + year: "numeric", + }); + return `${cleanQuery} as of ${monthYear}`; + }, + /** * Use Google Custom Search Engines * Free to set up, easy to use, 100 calls/day From 49c11fc8964eee90f1235d090a696271db505830 Mon Sep 17 00:00:00 2001 From: shatfield4 Date: Thu, 6 Nov 2025 18:34:36 -0800 Subject: [PATCH 2/3] simplify implementation --- .../agents/aibitat/plugins/web-browsing.js | 42 +++++++------------ 1 file changed, 16 insertions(+), 26 deletions(-) diff --git a/server/utils/agents/aibitat/plugins/web-browsing.js b/server/utils/agents/aibitat/plugins/web-browsing.js index 97d64502ccd..d95e9af4a78 100644 --- a/server/utils/agents/aibitat/plugins/web-browsing.js +++ b/server/utils/agents/aibitat/plugins/web-browsing.js @@ -61,10 +61,10 @@ const webBrowsing = { * https://programmablesearchengine.google.com/controlpanel/create */ search: async function (query) { - // Augment query with date context if it's asking for recent information - if (this.detectRecentQuery(query)) { - const originalQuery = query; - query = this.augmentQueryWithDate(query); + const originalQuery = query; + query = this.enhanceQueryForRecency(query); + + if (query !== originalQuery) { this.super.introspect( `${this.caller}: Enhanced query for recency: "${originalQuery}" → "${query}"` ); @@ -121,11 +121,11 @@ const webBrowsing = { }, /** - * Detects if search query asks for recent/latest/current information - * @param {string} query - Search query to analyze - * @returns {boolean} True if the query indicates need for recent results + * Enhances search queries asking for recent information by adding current date context + * @param {string} query - Original search query + * @returns {string} Enhanced query with current date, or original query if no enhancement needed */ - detectRecentQuery(query) { + enhanceQueryForRecency(query) { const recentKeywords = [ "today", "latest", @@ -138,20 +138,16 @@ const webBrowsing = { "updated", "updates", ]; + const lowerQuery = query.toLowerCase(); - return recentKeywords.some((keyword) => + const needsRecency = recentKeywords.some((keyword) => lowerQuery.includes(keyword) ); - }, - /** - * Strips date references that LLMs may have added based on their training cutoff date - * @param {string} query - Search query - * @returns {string} Search query with date references removed - */ - stripDateReferences(query) { - // Remove date patterns "January 2025", "2024", "December 2024", etc - return query + if (!needsRecency) return query; + + // Strip LLM added date references (ex: "January 2025", "2024", "December 2024", etc) + const cleanQuery = query .replace( /\b(January|February|March|April|May|June|July|August|September|October|November|December)\s+\d{4}\b/gi, "" @@ -159,20 +155,14 @@ const webBrowsing = { .replace(/\b\d{4}\b/g, "") .replace(/\s{2,}/g, " ") .trim(); - }, - /** - * Augment query with current date for better recency results - * @param {string} query - The original search query - * @returns {string} Query with date context appended - */ - augmentQueryWithDate(query) { - const cleanQuery = this.stripDateReferences(query); + // Get date in month year format const now = new Date(); const monthYear = now.toLocaleDateString("en-US", { month: "long", year: "numeric", }); + return `${cleanQuery} as of ${monthYear}`; }, From 14c47be0127c3b964df76647a9b8c783cf95ed56 Mon Sep 17 00:00:00 2001 From: shatfield4 Date: Thu, 6 Nov 2025 18:39:54 -0800 Subject: [PATCH 3/3] move introspect to enhanceQueryForRecency function --- .../utils/agents/aibitat/plugins/web-browsing.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/server/utils/agents/aibitat/plugins/web-browsing.js b/server/utils/agents/aibitat/plugins/web-browsing.js index d95e9af4a78..f95f0a58e63 100644 --- a/server/utils/agents/aibitat/plugins/web-browsing.js +++ b/server/utils/agents/aibitat/plugins/web-browsing.js @@ -61,15 +61,8 @@ const webBrowsing = { * https://programmablesearchengine.google.com/controlpanel/create */ search: async function (query) { - const originalQuery = query; query = this.enhanceQueryForRecency(query); - if (query !== originalQuery) { - this.super.introspect( - `${this.caller}: Enhanced query for recency: "${originalQuery}" → "${query}"` - ); - } - const provider = (await SystemSettings.get({ label: "agent_search_provider" })) ?.value ?? "unknown"; @@ -163,7 +156,12 @@ const webBrowsing = { year: "numeric", }); - return `${cleanQuery} as of ${monthYear}`; + const enhancedQuery = `${cleanQuery} as of ${monthYear}`; + this.super.introspect( + `${this.caller}: Enhanced query for recency: "${query}" → "${enhancedQuery}"` + ); + + return enhancedQuery; }, /**