电子书馆搜索增加流式输出功能

This commit is contained in:
mtvpls
2026-05-16 11:17:07 +08:00
parent 0d3731f07c
commit beefbe181d
3 changed files with 340 additions and 29 deletions
+14 -5
View File
@@ -484,17 +484,26 @@ export class OPDSClient {
};
}
async getSearchSources(sourceId?: string): Promise<BookSource[]> {
return sourceId ? [await getSourceById(sourceId)] : (await resolveOPDSConfig()).sources;
}
async searchBooksSource(q: string, source: BookSource): Promise<{ source: BookSource; results: BookListItem[] }> {
const targetUrl = await resolveSearchTargetUrl(source, q);
if (!targetUrl) throw new Error('未配置可用的搜索地址');
const feed = await getFeed(source, targetUrl);
return { source, results: feed.entries.map((entry) => mapEntryToItem(source, entry)) };
}
async searchBooks(q: string, sourceId?: string): Promise<BookSearchResult> {
const sources = sourceId ? [await getSourceById(sourceId)] : (await resolveOPDSConfig()).sources;
const sources = await this.getSearchSources(sourceId);
const results: BookListItem[] = [];
const failedSources: BookSearchFailure[] = [];
await Promise.all(sources.map(async (source) => {
try {
const targetUrl = await resolveSearchTargetUrl(source, q);
if (!targetUrl) throw new Error('未配置可用的搜索地址');
const feed = await getFeed(source, targetUrl);
results.push(...feed.entries.map((entry) => mapEntryToItem(source, entry)));
const sourceResult = await this.searchBooksSource(q, source);
results.push(...sourceResult.results);
} catch (error) {
failedSources.push({ sourceId: source.id, sourceName: source.name, error: (error as Error).message });
}