fix(web-search): handle unconfigured state and improve tests

This commit is contained in:
pomelo-nwu
2025-11-05 11:37:56 +08:00
parent 2967bec11c
commit 7ff07fd88c
3 changed files with 96 additions and 8 deletions

View File

@@ -272,5 +272,41 @@ describe('WebSearchTool', () => {
expect(result.error?.message).toContain('Web search is disabled');
expect(result.llmContent).toContain('Web search is disabled');
});
it('should return descriptive message in getDescription when web search is not configured', () => {
(
mockConfig.getWebSearchConfig as ReturnType<typeof vi.fn>
).mockReturnValue(null);
const tool = new WebSearchTool(mockConfig);
const invocation = tool.build({ query: 'test query' });
const description = invocation.getDescription();
expect(description).toBe(
' (Web search is disabled - configure a provider in settings.json)',
);
});
it('should return provider name in getDescription when web search is configured', () => {
const webSearchConfig: WebSearchConfig = {
provider: [
{
type: 'tavily',
apiKey: 'test-key',
},
],
default: 'tavily',
};
(
mockConfig.getWebSearchConfig as ReturnType<typeof vi.fn>
).mockReturnValue(webSearchConfig);
const tool = new WebSearchTool(mockConfig);
const invocation = tool.build({ query: 'test query' });
const description = invocation.getDescription();
expect(description).toBe(' (Searching the web via tavily)');
});
});
});

View File

@@ -43,8 +43,10 @@ class WebSearchToolInvocation extends BaseToolInvocation<
}
override getDescription(): string {
// If tool is registered, config must exist with a default provider
const webSearchConfig = this.config.getWebSearchConfig()!;
const webSearchConfig = this.config.getWebSearchConfig();
if (!webSearchConfig) {
return ' (Web search is disabled - configure a provider in settings.json)';
}
const provider = this.params.provider || webSearchConfig.default;
return ` (Searching the web via ${provider})`;
}
@@ -209,8 +211,19 @@ class WebSearchToolInvocation extends BaseToolInvocation<
}
async execute(signal: AbortSignal): Promise<WebSearchToolResult> {
// If tool is registered, config must exist with providers and default
const webSearchConfig = this.config.getWebSearchConfig()!;
// Check if web search is configured
const webSearchConfig = this.config.getWebSearchConfig();
if (!webSearchConfig) {
return {
llmContent:
'Web search is disabled. Please configure a web search provider in your settings.',
returnDisplay: 'Web search is disabled.',
error: {
message: 'Web search is disabled',
type: ToolErrorType.EXECUTION_FAILED,
},
};
}
try {
// Create and select provider