Skip to content

Commit

Permalink
feat(tool)!: remove pagination from search tools
Browse files Browse the repository at this point in the history
Signed-off-by: Tomas Dvorak <[email protected]>
  • Loading branch information
Tomas2D committed Nov 6, 2024
1 parent 2529a0c commit f93d181
Show file tree
Hide file tree
Showing 5 changed files with 2 additions and 73 deletions.
1 change: 0 additions & 1 deletion docs/cache.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
1 change: 0 additions & 1 deletion examples/cache/toolCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 1 addition & 12 deletions src/tools/search/duckDuckGoSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
});
}

Expand All @@ -116,15 +106,14 @@ export class DuckDuckGoSearchTool extends Tool<
}

protected async _run(
{ query: input, page = 1 }: ToolInput<this>,
{ query: input }: ToolInput<this>,
options?: DuckDuckGoSearchToolRunOptions,
) {
const headers = new HeaderGenerator().getHeaders();

const { results } = await this.client(
input,
{
offset: this.options.maxResultsPerPage * (page - 1),
safeSearch: SafeSearchType.MODERATE,
...this.options.search,
...options?.search,
Expand Down
46 changes: 0 additions & 46 deletions src/tools/search/googleSearch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ describe("GoogleCustomSearch Tool", () => {
cx: "test-cse-id",
q: query,
num: 10,
start: 1,
safe: "active",
},
{
Expand Down Expand Up @@ -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",
Expand Down
14 changes: 1 addition & 13 deletions src/tools/search/googleSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
});
}

Expand Down Expand Up @@ -124,17 +114,15 @@ export class GoogleSearchTool extends Tool<
}

protected async _run(
{ query: input, page = 1 }: ToolInput<this>,
{ query: input }: ToolInput<this>,
_options: GoogleSearchToolRunOptions | undefined,
run: RunContext<this>,
) {
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",
},
{
Expand Down

0 comments on commit f93d181

Please sign in to comment.