mirror of
https://github.com/QwenLM/qwen-code.git
synced 2025-12-21 17:27:54 +00:00
feat: Optimize the code
This commit is contained in:
@@ -9,10 +9,11 @@ import type {
|
||||
WebSearchResult,
|
||||
WebSearchResultItem,
|
||||
} from './types.js';
|
||||
import { getErrorMessage } from '../../utils/errors.js';
|
||||
import { WebSearchError } from './errors.js';
|
||||
|
||||
/**
|
||||
* Base implementation for web search providers.
|
||||
* Provides common functionality for error handling and result formatting.
|
||||
*/
|
||||
export abstract class BaseWebSearchProvider implements WebSearchProvider {
|
||||
abstract readonly name: string;
|
||||
@@ -41,16 +42,19 @@ export abstract class BaseWebSearchProvider implements WebSearchProvider {
|
||||
*/
|
||||
async search(query: string, signal: AbortSignal): Promise<WebSearchResult> {
|
||||
if (!this.isAvailable()) {
|
||||
throw new Error(
|
||||
`${this.name} provider is not available. Please check your configuration.`,
|
||||
throw new WebSearchError(
|
||||
this.name,
|
||||
'Provider is not available. Please check your configuration.',
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
return await this.performSearch(query, signal);
|
||||
} catch (error: unknown) {
|
||||
const errorMessage = getErrorMessage(error);
|
||||
throw new Error(`Error during ${this.name} search: ${errorMessage}`);
|
||||
if (error instanceof WebSearchError) {
|
||||
throw error;
|
||||
}
|
||||
throw new WebSearchError(this.name, 'Search failed', error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,6 +62,7 @@ export abstract class BaseWebSearchProvider implements WebSearchProvider {
|
||||
* Format search results into a consistent format.
|
||||
* @param results Raw results from the provider
|
||||
* @param query The original search query
|
||||
* @param answer Optional answer from the provider
|
||||
* @returns Formatted search results
|
||||
*/
|
||||
protected formatResults(
|
||||
@@ -71,31 +76,4 @@ export abstract class BaseWebSearchProvider implements WebSearchProvider {
|
||||
results,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a formatted source list for display.
|
||||
* @param results Search result items
|
||||
* @returns Formatted source list
|
||||
*/
|
||||
protected createSourceList(results: WebSearchResultItem[]): string {
|
||||
return results
|
||||
.map((r, i) => `[${i + 1}] ${r.title || 'Untitled'} (${r.url})`)
|
||||
.join('\n');
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a concise summary from search results.
|
||||
* @param results Search result items
|
||||
* @param maxResults Maximum number of results to include
|
||||
* @returns Concise summary string
|
||||
*/
|
||||
protected buildSummary(
|
||||
results: WebSearchResultItem[],
|
||||
maxResults: number = 3,
|
||||
): string {
|
||||
return results
|
||||
.slice(0, maxResults)
|
||||
.map((r, i) => `${i + 1}. ${r.title} - ${r.url}`)
|
||||
.join('\n');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user