@@ -32,11 +32,15 @@ class AzureAISearch extends Tool {
32
32
fields . AZURE_AI_SEARCH_SERVICE_ENDPOINT ,
33
33
'AZURE_AI_SEARCH_SERVICE_ENDPOINT' ,
34
34
) ;
35
- this . indexName = this . _initializeField (
35
+ // Get the indexes as a comma-separated string
36
+ this . indexNames = this . _initializeField (
36
37
fields . AZURE_AI_SEARCH_INDEX_NAME ,
37
38
'AZURE_AI_SEARCH_INDEX_NAME' ,
38
39
) ;
39
- this . apiKey = this . _initializeField ( fields . AZURE_AI_SEARCH_API_KEY , 'AZURE_AI_SEARCH_API_KEY' ) ;
40
+ this . apiKey = this . _initializeField (
41
+ fields . AZURE_AI_SEARCH_API_KEY ,
42
+ 'AZURE_AI_SEARCH_API_KEY' ,
43
+ ) ;
40
44
this . apiVersion = this . _initializeField (
41
45
fields . AZURE_AI_SEARCH_API_VERSION ,
42
46
'AZURE_AI_SEARCH_API_VERSION' ,
@@ -58,7 +62,7 @@ class AzureAISearch extends Tool {
58
62
) ;
59
63
60
64
// Check for required fields
61
- if ( ! this . override && ( ! this . serviceEndpoint || ! this . indexName || ! this . apiKey ) ) {
65
+ if ( ! this . override && ( ! this . serviceEndpoint || ! this . indexNames || ! this . apiKey ) ) {
62
66
throw new Error (
63
67
'Missing AZURE_AI_SEARCH_SERVICE_ENDPOINT, AZURE_AI_SEARCH_INDEX_NAME, or AZURE_AI_SEARCH_API_KEY environment variable.' ,
64
68
) ;
@@ -68,12 +72,25 @@ class AzureAISearch extends Tool {
68
72
return ;
69
73
}
70
74
71
- // Create SearchClient
72
- this . client = new SearchClient (
73
- this . serviceEndpoint ,
74
- this . indexName ,
75
- new AzureKeyCredential ( this . apiKey ) ,
76
- { apiVersion : this . apiVersion } ,
75
+ // Split the indexNames by comma to support multiple indexes, trim whitespace,
76
+ // convert to lowercase, and filter out any empty strings.
77
+ const indexes = this . indexNames
78
+ . split ( ',' )
79
+ . map ( index => index . trim ( ) . toLowerCase ( ) )
80
+ . filter ( index => index . length > 0 ) ;
81
+
82
+ if ( indexes . length === 0 ) {
83
+ throw new Error ( 'No valid index names provided in AZURE_AI_SEARCH_INDEX_NAME.' ) ;
84
+ }
85
+
86
+ // Create a client for each index.
87
+ this . clients = indexes . map ( index =>
88
+ new SearchClient (
89
+ this . serviceEndpoint ,
90
+ index ,
91
+ new AzureKeyCredential ( this . apiKey ) ,
92
+ { apiVersion : this . apiVersion } ,
93
+ ) ,
77
94
) ;
78
95
}
79
96
@@ -88,17 +105,26 @@ class AzureAISearch extends Tool {
88
105
if ( this . select ) {
89
106
searchOption . select = this . select . split ( ',' ) ;
90
107
}
91
- const searchResults = await this . client . search ( query , searchOption ) ;
92
- const resultDocuments = [ ] ;
93
- for await ( const result of searchResults . results ) {
94
- resultDocuments . push ( result . document ) ;
95
- }
96
- return JSON . stringify ( resultDocuments ) ;
108
+
109
+ // Query all indexes concurrently
110
+ const searchPromises = this . clients . map ( async ( client ) => {
111
+ const resultDocuments = [ ] ;
112
+ const searchResults = await client . search ( query , searchOption ) ;
113
+ for await ( const result of searchResults . results ) {
114
+ resultDocuments . push ( result . document ) ;
115
+ }
116
+ return resultDocuments ;
117
+ } ) ;
118
+
119
+ // Wait for all search promises to complete and flatten the results
120
+ const resultsByIndex = await Promise . all ( searchPromises ) ;
121
+ const combinedResults = resultsByIndex . flat ( ) ;
122
+ return JSON . stringify ( combinedResults ) ;
97
123
} catch ( error ) {
98
124
logger . error ( 'Azure AI Search request failed' , error ) ;
99
125
return 'There was an error with Azure AI Search.' ;
100
126
}
101
127
}
102
128
}
103
129
104
- module . exports = AzureAISearch ;
130
+ module . exports = AzureAISearch ;
0 commit comments