🧩 refactor: Tool Context Builders for Web Search & Image Gen (#11644)

* fix: Web Search + Image Gen Tool Context

- Added `buildWebSearchContext` function to create a structured context for web search tools, including citation format instructions.
- Updated `loadTools` and `loadToolDefinitionsWrapper` functions to utilize the new web search context, improving tool initialization and response handling.
- Introduced logic to handle image editing tools with `buildImageToolContext`, enhancing the overall tool management capabilities.
- Refactored imports in `ToolService.js` to include the new context builders for better organization and maintainability.

* fix: Trim critical output escape sequence instructions in web toolkit

- Updated the critical output escape sequence instructions in the web toolkit to include a `.trim()` method, ensuring that unnecessary whitespace is removed from the output. This change enhances the consistency and reliability of the generated output.
This commit is contained in:
Danny Avila 2026-02-05 14:10:19 +01:00 committed by GitHub
parent 1ba5bf87b0
commit 24625f5693
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 62 additions and 19 deletions

View file

@ -17,6 +17,8 @@ const {
loadToolDefinitions,
GenerationJobManager,
isActionDomainAllowed,
buildWebSearchContext,
buildImageToolContext,
buildToolClassification,
} = require('@librechat/api');
const {
@ -28,6 +30,7 @@ const {
ContentTypes,
imageGenTools,
EModelEndpoint,
EToolResources,
actionDelimiter,
ImageVisionTool,
openapiToFunction,
@ -682,9 +685,14 @@ async function loadToolDefinitionsWrapper({ req, res, agent, streamId = null, to
/** @type {Record<string, string>} */
const toolContextMap = {};
const hasWebSearch = filteredTools.includes(Tools.web_search);
const hasFileSearch = filteredTools.includes(Tools.file_search);
const hasExecuteCode = filteredTools.includes(Tools.execute_code);
if (hasWebSearch) {
toolContextMap[Tools.web_search] = buildWebSearchContext();
}
if (hasExecuteCode && tool_resources) {
try {
const authValues = await loadAuthValues({
@ -722,6 +730,34 @@ async function loadToolDefinitionsWrapper({ req, res, agent, streamId = null, to
}
}
const imageFiles = tool_resources?.[EToolResources.image_edit]?.files ?? [];
if (imageFiles.length > 0) {
const hasOaiImageGen = filteredTools.includes('image_gen_oai');
const hasGeminiImageGen = filteredTools.includes('gemini_image_gen');
if (hasOaiImageGen) {
const toolContext = buildImageToolContext({
imageFiles,
toolName: `${EToolResources.image_edit}_oai`,
contextDescription: 'image editing',
});
if (toolContext) {
toolContextMap.image_edit_oai = toolContext;
}
}
if (hasGeminiImageGen) {
const toolContext = buildImageToolContext({
imageFiles,
toolName: 'gemini_image_gen',
contextDescription: 'image context',
});
if (toolContext) {
toolContextMap.gemini_image_gen = toolContext;
}
}
}
return {
toolRegistry,
userMCPAuthMap,