aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/chatbot/tools/SearchTool.ts
diff options
context:
space:
mode:
authorA.J. Shulman <Shulman.aj@gmail.com>2024-10-17 11:14:51 -0400
committerA.J. Shulman <Shulman.aj@gmail.com>2024-10-17 11:14:51 -0400
commit14f412611299fc350f13b6f96be913d59533cfb3 (patch)
tree79e05a3467a44b5a3a5db5baaec2febce72a2526 /src/client/views/nodes/chatbot/tools/SearchTool.ts
parent80d86bd5ae3e1d3dc70e7636f72a872a5fb2f01d (diff)
Removed awaits inside loops and made Parameters readonly for better type safety
Diffstat (limited to 'src/client/views/nodes/chatbot/tools/SearchTool.ts')
-rw-r--r--src/client/views/nodes/chatbot/tools/SearchTool.ts22
1 files changed, 13 insertions, 9 deletions
diff --git a/src/client/views/nodes/chatbot/tools/SearchTool.ts b/src/client/views/nodes/chatbot/tools/SearchTool.ts
index c5cf951e7..fd5144dd6 100644
--- a/src/client/views/nodes/chatbot/tools/SearchTool.ts
+++ b/src/client/views/nodes/chatbot/tools/SearchTool.ts
@@ -34,9 +34,9 @@ export class SearchTool extends BaseTool<SearchToolParamsType> {
async execute(args: ParametersType<SearchToolParamsType>): Promise<Observation[]> {
const queries = args.query;
- const allResults: Observation[] = [];
- for (const query of queries) {
+ // Create an array of promises, each one handling a search for a query
+ const searchPromises = queries.map(async query => {
try {
const { results } = await Networking.PostToServer('/getWebSearchResults', {
query,
@@ -49,16 +49,20 @@ export class SearchTool extends BaseTool<SearchToolParamsType> {
text: `<chunk chunk_id="${id}" chunk_type="text"><url>${result.url}</url><overview>${result.snippet}</overview></chunk>`,
};
});
- allResults.push(...data);
+ return data;
} catch (error) {
console.log(error);
- allResults.push({
- type: 'text',
- text: `An error occurred while performing the web search for query: ${query}`,
- });
+ return [
+ {
+ type: 'text',
+ text: `An error occurred while performing the web search for query: ${query}`,
+ },
+ ];
}
- }
+ });
+
+ const allResultsArrays = await Promise.all(searchPromises);
- return allResults;
+ return allResultsArrays.flat();
}
}