mirror of
https://github.com/QwenLM/qwen-code.git
synced 2025-12-19 09:33:53 +00:00
fix(web-search): handle unconfigured state and improve tests
This commit is contained in:
@@ -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)');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user