Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix: correct Search1API response parsing to match actual API format (#13207)

Arvin Xu48428594c35f
diff --git a/src/features/Electron/titlebar/layout.test.ts b/src/features/Electron/titlebar/layout.test.ts
index 5fc1f2666e0..980997f770a 100644
--- a/src/features/Electron/titlebar/layout.test.ts
+++ b/src/features/Electron/titlebar/layout.test.ts
@@ -1,12 +1,11 @@
import { describe, expect, it } from 'vitest';
import { getTitleBarLayoutConfig, TITLE_BAR_HORIZONTAL_PADDING } from './layout';
-import { WINDOW_CONTROL_WIDTH } from './WinControl';
describe('getTitleBarLayoutConfig', () => {
it('reserves right-side space for native Windows controls', () => {
expect(getTitleBarLayoutConfig('Windows')).toEqual({
- padding: `0 ${WINDOW_CONTROL_WIDTH + TITLE_BAR_HORIZONTAL_PADDING}px 0 ${TITLE_BAR_HORIZONTAL_PADDING}px`,
+ padding: `0 162px 0 ${TITLE_BAR_HORIZONTAL_PADDING}px`,
reserveNativeControlSpace: true,
showCustomWinControl: false,
});
diff --git a/src/server/services/search/impls/search1api/index.integration.test.ts b/src/server/services/search/impls/search1api/index.integration.test.ts
new file mode 100644
index 00000000000..ce399286c7e
--- /dev/null
+++ b/src/server/services/search/impls/search1api/index.integration.test.ts
@@ -0,0 +1,69 @@
+// @vitest-environment node
+import { describe, expect, it } from 'vitest';
+
+import { Search1APIImpl } from './index';
+
+// Integration test — requires SEARCH1API_API_KEY env var
+const apiKey = process.env.SEARCH1API_SEARCH_API_KEY;
+const describeIfKey = apiKey ? describe : describe.skip;
+
+describeIfKey('Search1APIImpl integration', { timeout: 30_000 }, () => {
+ const impl = new Search1APIImpl();
+
+ it('should return search results for a basic query', async () => {
+ const result = await impl.query('LobeHub');
+
+ expect(result.query).toBe('LobeHub');
+ expect(result.resultNumbers).toBeGreaterThan(0);
+ expect(result.results.length).toBeGreaterThan(0);
+ expect(result.costTime).toBeGreaterThan(0);
+
+ const firstResult = result.results[0];
+ expect(firstResult.url).toBeDefined();
+ expect(firstResult.title).toBeDefined();
+ expect(firstResult.parsedUrl).toBeDefined();
+ });
+
+ it('should respect searchTimeRange param', async () => {
+ const result = await impl.query('latest AI news', {
+ searchTimeRange: 'day',
+ });
+
+ expect(result.query).toBe('latest AI news');
+ expect(result.results).toBeDefined();
+ expect(Array.isArray(result.results)).toBe(true);
+ });
+
+ it('should support multiple search engines', async () => {
+ const result = await impl.query('TypeScript', {
+ searchEngines: ['google', 'bing'],
+ });
+
+ expect(result.query).toBe('TypeScript');
+ expect(result.resultNumbers).toBeGreaterThan(0);
+ expect(result.results.length).toBeGreaterThan(0);
+ });
+
+ it('should return valid URL structures in results', async () => {
+ const result = await impl.query('GitHub');
+
+ for (const item of result.results) {
+ expect(item.url).toMatch(/^https?:\/\//);
+ expect(item.parsedUrl).toBeTruthy();
+ expect(typeof item.title).toBe('string');
+ expect(typeof item.content).toBe('string');
+ expect(typeof item.score).toBe('number');
+ expect(item.category).toBe('general');
+ }
+ });
+
+ it('should handle single search engine param', async () => {
+ const result = await impl.query('OpenAI', {
+ searchEngines: ['google'],
+ });
+
+ expect(result.results.length).toBeGreaterThan(0);
+ // When single engine specified, engines field should contain it
+ expect(result.results[0].engines).toContain('google');
+ });
+});
diff --git a/src/server/services/search/impls/search1api/index.ts b/src/server/services/search/impls/search1api/index.ts
index 052ef466505..b6b2a10c092 100644
--- a/src/server/services/search/impls/search1api/index.ts
+++ b/src/server/services/search/impls/search1api/index.ts
@@ -8,7 +8,7 @@ import debug from 'debug';
import urlJoin from 'url-join';
import { type SearchServiceImpl } from '../type';
-import { type Search1ApiResponse } from './type';
+import { type Search1ApiRawResponse } from './type';
interface Search1APIQueryParams {
crawl_results?: 0 | 1;
@@ -118,19 +118,20 @@ export class Search1APIImpl implements SearchServiceImpl {
}
try {
- const search1ApiResponse = (await response.json()) as Search1ApiResponse[]; // Use a specific type if defined elsewhere
+ const rawResponse = (await response.json()) as Search1ApiRawResponse;
- log('Parsed Search1API response: %o', search1ApiResponse);
+ log('Parsed Search1API response: %o', rawResponse);
- const mappedResults = search1ApiResponse.flatMap((response) => {
- // Map Search1API response to SearchResponse
- return (response.results || []).map(
+ const mappedResults = (rawResponse.results || []).flatMap((item) => {
+ if (!item.success || !item.data) return [];
+ const { results = [], searchParameters } = item.data;
+ return results.map(
(result): UniformSearchResult => ({
- category: 'general', // Default category
- content: result.content || result.snippet || '', // Prioritize content, fallback to snippet
- engines: [response.searchParameters?.search_service || ''],
- parsedUrl: result.link ? new URL(result.link).hostname : '', // Basic URL parsing
- score: 1, // Default score
+ category: 'general',
+ content: result.content || result.snippet || '',
+ engines: [searchParameters?.search_service || ''],
+ parsedUrl: result.link ? new URL(result.link).hostname : '',
+ score: 1,
title: result.title || '',
url: result.link,
}),
diff --git a/src/server/services/search/impls/search1api/type.ts b/src/server/services/search/impls/search1api/type.ts
index 753e96c4d48..05e0b5ec5ca 100644
--- a/src/server/services/search/impls/search1api/type.ts
+++ b/src/server/services/search/impls/search1api/type.ts
@@ -65,8 +65,6 @@ export interface Search1ApiSearchParameters {
time_range?: TimeRange;
}
-// Define the Search1API specific response structure based on user input
-// Ideally, this would live in a dedicated types file (e.g., src/types/tool/search/search1api.ts)
interface Search1ApiResult {
content?: string;
link: string;
@@ -74,8 +72,23 @@ interface Search1ApiResult {
title?: string;
}
+export interface Search1ApiResponseItem {
+ data: {
+ results?: Search1ApiResult[];
+ searchParameters?: Search1ApiSearchParameters;
+ };
+ success: boolean;
+}
+
+/**
+ * @deprecated Use Search1ApiRawResponse instead
+ */
export interface Search1ApiResponse {
- // Keeping this generic for now
results?: Search1ApiResult[];
searchParameters?: Search1ApiSearchParameters;
}
+
+export interface Search1ApiRawResponse {
+ results: Search1ApiResponseItem[];
+ summary?: string;
+}