@@ -23,9 +23,6 @@ type GenerativeAiInput = {
2323// want to ensure we're not uploading massive documents (some folks have documents > 1mb).
2424const AI_MAX_REQUEST_SIZE = 5120000 ;
2525const AI_MIN_SAMPLE_DOCUMENTS = 1 ;
26- const USER_AI_URI = ( userId : string ) => `unauth/ai/api/v1/hello/${ userId } ` ;
27- const AGGREGATION_URI = 'ai/api/v1/mql-aggregation' ;
28- const QUERY_URI = 'ai/api/v1/mql-query' ;
2926
3027type AIAggregation = {
3128 content : {
@@ -192,14 +189,49 @@ export function validateAIAggregationResponse(
192189 }
193190}
194191
192+ export const aiURLConfig = {
193+ // There are two different sets of endpoints we use for our requests.
194+ // Down the line we'd like to only use the admin api, however,
195+ // we cannot currently call that from the Atlas UI. Pending CLOUDP-251201
196+ 'admin-api' : {
197+ 'user-access' : ( userId : string ) => `unauth/ai/api/v1/hello/${ userId } ` ,
198+ aggregation : 'ai/api/v1/mql-aggregation' ,
199+ query : 'ai/api/v1/mql-query' ,
200+ } ,
201+ cloud : {
202+ 'user-access' : ( groupId : string , userId : string ) =>
203+ `/ai/v1/groups/${ groupId } /hello/${ userId } ` ,
204+ aggregation : ( groupId : string ) =>
205+ `/ai/v1/groups/${ groupId } /mql-aggregation` ,
206+ query : ( groupId : string ) => `/ai/v1/groups/${ groupId } /mql-query` ,
207+ } ,
208+ } as const ;
209+ export type AIEndpoint = 'user-access' | 'query' | 'aggregation' ;
210+
195211export class AtlasAiService {
196212 private initPromise : Promise < void > | null = null ;
197213
198- constructor (
199- private atlasService : AtlasService ,
200- private preferences : PreferencesAccess ,
201- private logger : Logger
202- ) {
214+ private atlasService : AtlasService ;
215+ private getUrlForEndpoint : ( urlId : AIEndpoint ) => string ;
216+ private preferences : PreferencesAccess ;
217+ private logger : Logger ;
218+
219+ constructor ( {
220+ atlasService,
221+ getUrlForEndpoint,
222+ preferences,
223+ logger,
224+ } : {
225+ atlasService : AtlasService ;
226+ getUrlForEndpoint : ( urlId : AIEndpoint ) => string ;
227+ preferences : PreferencesAccess ;
228+ logger : Logger ;
229+ } ) {
230+ this . atlasService = atlasService ;
231+ this . getUrlForEndpoint = getUrlForEndpoint ;
232+ this . preferences = preferences ;
233+ this . logger = logger ;
234+
203235 this . initPromise = this . setupAIAccess ( ) ;
204236 }
205237
@@ -215,8 +247,7 @@ export class AtlasAiService {
215247 }
216248
217249 private async getAIFeatureEnablement ( ) : Promise < AIFeatureEnablement > {
218- const userId = this . preferences . getPreferencesUser ( ) . id ;
219- const url = this . atlasService . adminApiEndpoint ( USER_AI_URI ( userId ) ) ;
250+ const url = this . getUrlForEndpoint ( 'user-access' ) ;
220251 const res = await this . atlasService . fetch ( url , {
221252 headers : {
222253 Accept : 'application/json' ,
@@ -261,7 +292,7 @@ export class AtlasAiService {
261292 }
262293
263294 private getQueryOrAggregationFromUserInput = async < T > (
264- uri : string ,
295+ urlId : 'query' | 'aggregation' ,
265296 input : GenerativeAiInput ,
266297 validationFn : ( res : any ) => asserts res is T
267298 ) : Promise < T > => {
@@ -271,14 +302,15 @@ export class AtlasAiService {
271302 const { signal, requestId, ...rest } = input ;
272303 const msgBody = buildQueryOrAggregationMessageBody ( rest ) ;
273304
274- const url = this . atlasService . adminApiEndpoint ( uri , requestId ) ;
305+ const url = new URL ( this . getUrlForEndpoint ( urlId ) ) ;
306+ url . searchParams . append ( 'request_id' , encodeURIComponent ( requestId ) ) ;
275307
276308 this . logger . log . info (
277309 this . logger . mongoLogId ( 1_001_000_308 ) ,
278310 'AtlasAIService' ,
279311 'Running AI query generation request' ,
280312 {
281- url,
313+ url : url . toString ( ) ,
282314 userInput : input . userInput ,
283315 collectionName : input . collectionName ,
284316 databaseName : input . databaseName ,
@@ -287,7 +319,7 @@ export class AtlasAiService {
287319 }
288320 ) ;
289321
290- const res = await this . atlasService . authenticatedFetch ( url , {
322+ const res = await this . atlasService . authenticatedFetch ( url . toString ( ) , {
291323 signal,
292324 method : 'POST' ,
293325 body : msgBody ,
@@ -328,15 +360,15 @@ export class AtlasAiService {
328360
329361 async getAggregationFromUserInput ( input : GenerativeAiInput ) {
330362 return this . getQueryOrAggregationFromUserInput (
331- AGGREGATION_URI ,
363+ 'aggregation' ,
332364 input ,
333365 validateAIAggregationResponse
334366 ) ;
335367 }
336368
337369 async getQueryFromUserInput ( input : GenerativeAiInput ) {
338370 return this . getQueryOrAggregationFromUserInput (
339- QUERY_URI ,
371+ 'query' ,
340372 input ,
341373 validateAIQueryResponse
342374 ) ;
0 commit comments