From f93d181c3c9f3d535323a438b92a4a8e5a3f437a Mon Sep 17 00:00:00 2001 From: Tomas Dvorak Date: Wed, 6 Nov 2024 12:23:58 +0100 Subject: [PATCH] feat(tool)!: remove pagination from search tools Signed-off-by: Tomas Dvorak --- docs/cache.md | 1 - examples/cache/toolCache.ts | 1 - src/tools/search/duckDuckGoSearch.ts | 13 +------- src/tools/search/googleSearch.test.ts | 46 --------------------------- src/tools/search/googleSearch.ts | 14 +------- 5 files changed, 2 insertions(+), 73 deletions(-) diff --git a/docs/cache.md b/docs/cache.md index e1a226a0..414b4104 100644 --- a/docs/cache.md +++ b/docs/cache.md @@ -88,7 +88,6 @@ const ddg = new DuckDuckGoSearchTool({ const response = await ddg.run({ query: "the time of the fastest marathon run", - page: 1, }); // upcoming requests with the EXACTLY same input will be retrieved from the cache ``` diff --git a/examples/cache/toolCache.ts b/examples/cache/toolCache.ts index afc6f41e..4771eb93 100644 --- a/examples/cache/toolCache.ts +++ b/examples/cache/toolCache.ts @@ -10,6 +10,5 @@ const ddg = new DuckDuckGoSearchTool({ const response = await ddg.run({ query: "the time of the fastest marathon run", - page: 1, }); // upcoming requests with the EXACTLY same input will be retrieved from the cache diff --git a/src/tools/search/duckDuckGoSearch.ts b/src/tools/search/duckDuckGoSearch.ts index d1743c30..882b9c22 100644 --- a/src/tools/search/duckDuckGoSearch.ts +++ b/src/tools/search/duckDuckGoSearch.ts @@ -80,16 +80,6 @@ export class DuckDuckGoSearchTool extends Tool< inputSchema() { return z.object({ query: z.string({ description: `Search query` }).min(1).max(128), - page: z.coerce - .number() - .int() - .min(1) - .max(10) - .describe( - `Search result page (each page contains maximally ${this.options.maxResultsPerPage} results)`, - ) - .default(1) - .optional(), }); } @@ -116,7 +106,7 @@ export class DuckDuckGoSearchTool extends Tool< } protected async _run( - { query: input, page = 1 }: ToolInput, + { query: input }: ToolInput, options?: DuckDuckGoSearchToolRunOptions, ) { const headers = new HeaderGenerator().getHeaders(); @@ -124,7 +114,6 @@ export class DuckDuckGoSearchTool extends Tool< const { results } = await this.client( input, { - offset: this.options.maxResultsPerPage * (page - 1), safeSearch: SafeSearchType.MODERATE, ...this.options.search, ...options?.search, diff --git a/src/tools/search/googleSearch.test.ts b/src/tools/search/googleSearch.test.ts index c88414ad..785cc6f6 100644 --- a/src/tools/search/googleSearch.test.ts +++ b/src/tools/search/googleSearch.test.ts @@ -78,7 +78,6 @@ describe("GoogleCustomSearch Tool", () => { cx: "test-cse-id", q: query, num: 10, - start: 1, safe: "active", }, { @@ -106,51 +105,6 @@ describe("GoogleCustomSearch Tool", () => { ).toThrowError("validation failed"); }); - it("paginates correctly", async () => { - const query = "paginated search"; - const mockFirstPageResults = generateResults(10); - const mockSecondPageResults = generateResults(10); - - mockCustomSearchClient.cse.list - .mockResolvedValueOnce(mockFirstPageResults) - .mockResolvedValueOnce(mockSecondPageResults); - - const responsePage1 = await googleSearchTool.run({ query }); - const responsePage2 = await googleSearchTool.run({ query, page: 2 }); - - const combinedResults = [...responsePage1.results, ...responsePage2.results]; - - expect(combinedResults.length).toBe(20); - - expect(mockCustomSearchClient.cse.list).toHaveBeenCalledTimes(2); - expect(mockCustomSearchClient.cse.list).toHaveBeenNthCalledWith( - 1, - { - cx: "test-cse-id", - q: query, - num: 10, - start: 1, - safe: "active", - }, - { - signal: expect.any(AbortSignal), - }, - ); - expect(mockCustomSearchClient.cse.list).toHaveBeenNthCalledWith( - 2, - { - cx: "test-cse-id", - q: query, - num: 10, - start: 11, - safe: "active", - }, - { - signal: expect.any(AbortSignal), - }, - ); - }); - it("Serializes", async () => { const tool = new GoogleSearchTool({ apiKey: "test-api-key", diff --git a/src/tools/search/googleSearch.ts b/src/tools/search/googleSearch.ts index fbd98136..a8ef065f 100644 --- a/src/tools/search/googleSearch.ts +++ b/src/tools/search/googleSearch.ts @@ -71,16 +71,6 @@ export class GoogleSearchTool extends Tool< inputSchema() { return z.object({ query: z.string({ description: `Search query` }).min(1).max(2048), - page: z.coerce - .number() - .int() - .min(1) - .max(10) - .describe( - `Search result page (each page contains maximally ${this.options.maxResultsPerPage} results)`, - ) - .default(1) - .optional(), }); } @@ -124,17 +114,15 @@ export class GoogleSearchTool extends Tool< } protected async _run( - { query: input, page = 1 }: ToolInput, + { query: input }: ToolInput, _options: GoogleSearchToolRunOptions | undefined, run: RunContext, ) { - const startIndex = (page - 1) * this.options.maxResultsPerPage + 1; const response = await this.client.cse.list( { cx: this.cseId, q: input, num: this.options.maxResultsPerPage, - start: startIndex, safe: "active", }, {