-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tool)!: remove pagination functionality from GoogleSearch and Du…
…ckDuckGo (#152) Ref: #151 Signed-off-by: Tomas Dvorak <[email protected]>
- Loading branch information
Showing
9 changed files
with
203 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* Copyright 2024 IBM Corp. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { paginate, PaginateInput } from "@/internals/helpers/paginate.js"; | ||
|
||
describe("paginate", () => { | ||
it.each([ | ||
{ | ||
size: 1, | ||
chunkSize: 1, | ||
items: Array(100).fill(1), | ||
}, | ||
{ | ||
size: 10, | ||
chunkSize: 1, | ||
items: [], | ||
}, | ||
{ | ||
size: 11, | ||
chunkSize: 10, | ||
items: Array(100).fill(1), | ||
}, | ||
{ | ||
size: 25, | ||
chunkSize: 1, | ||
items: Array(20).fill(1), | ||
}, | ||
])("Works %#", async ({ size, items, chunkSize }) => { | ||
const fn: PaginateInput<number>["handler"] = vi.fn().mockImplementation(async ({ offset }) => { | ||
const chunk = items.slice(offset, offset + chunkSize); | ||
return { done: offset + chunk.length >= items.length, data: chunk }; | ||
}); | ||
|
||
const results = await paginate({ | ||
size, | ||
handler: fn, | ||
}); | ||
|
||
const maxItemsToBeRetrieved = Math.min(size, items.length); | ||
let expectedCalls = Math.ceil(maxItemsToBeRetrieved / chunkSize); | ||
if (expectedCalls === 0 && size > 0) { | ||
expectedCalls = 1; | ||
} | ||
expect(fn).toBeCalledTimes(expectedCalls); | ||
expect(results).toHaveLength(maxItemsToBeRetrieved); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* Copyright 2024 IBM Corp. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
export interface PaginateInput<T> { | ||
size: number; | ||
handler: (data: { offset: number; limit: number }) => Promise<{ data: T[]; done: boolean }>; | ||
} | ||
|
||
export async function paginate<T>(input: PaginateInput<T>): Promise<T[]> { | ||
const acc: T[] = []; | ||
|
||
while (acc.length < input.size) { | ||
const { data, done } = await input.handler({ | ||
offset: acc.length, | ||
limit: input.size - acc.length, | ||
}); | ||
acc.push(...data); | ||
|
||
if (done || data.length === 0) { | ||
break; | ||
} | ||
} | ||
|
||
if (acc.length > input.size) { | ||
acc.length = input.size; | ||
} | ||
|
||
return acc; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* Copyright 2024 IBM Corp. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { expect } from "vitest"; | ||
import { DuckDuckGoSearchTool } from "@/tools/search/duckDuckGoSearch.js"; | ||
|
||
describe("DuckDuckGo", () => { | ||
it("Retrieves data", async () => { | ||
const instance = new DuckDuckGoSearchTool(); | ||
const response = await instance.run({ query: "Bee Agent Framework" }); | ||
expect(response.results.length).toBeGreaterThan(0); | ||
}); | ||
}); |