Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
handle fle collections
  • Loading branch information
mabaasit committed Dec 9, 2025
commit febb55dfd6b0254544ae1b95c52a037c4939bf7d
7 changes: 7 additions & 0 deletions packages/compass-aggregations/src/modules/data-service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import type { DataService as OriginalDataService } from 'mongodb-data-service';

type FetchCollectionMetadataDataServiceMethods =
| 'collectionStats'
| 'collectionInfo'
| 'listCollections'
| 'isListSearchIndexesSupported';

export type RequiredDataServiceProps =
| FetchCollectionMetadataDataServiceMethods
| 'isCancelError'
| 'estimatedCount'
| 'aggregate'
Expand Down
2 changes: 2 additions & 0 deletions packages/compass-aggregations/src/modules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import type {
ConnectionScopedAppRegistry,
} from '@mongodb-js/compass-connections/provider';
import type { TrackFunction } from '@mongodb-js/compass-telemetry';
import type Collection from 'mongodb-collection-model';
/**
* The main application reducer.
*
Expand Down Expand Up @@ -110,6 +111,7 @@ export type PipelineBuilderExtraArgs = {
connectionScopedAppRegistry: ConnectionScopedAppRegistry<
'open-export' | 'view-edited' | 'agg-pipeline-out-executed'
>;
collection: Collection;
};

export type PipelineBuilderThunkDispatch<A extends Action = AnyAction> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ export const runAIPipelineGeneration = (
logger: { log, mongoLogId },
track,
connectionInfoRef,
collection,
}
) => {
const {
Expand Down Expand Up @@ -286,6 +287,9 @@ export const runAIPipelineGeneration = (
}
)) || [];
const schema = await getSimplifiedSchema(sampleDocuments);
const { isFLE } = await collection.fetchMetadata({
dataService: dataService!,
});

const { collection: collectionName, database: databaseName } =
toNS(namespace);
Expand All @@ -303,6 +307,7 @@ export const runAIPipelineGeneration = (
}
: undefined),
requestId,
enableStorage: !isFLE,
},
connectionInfo
);
Expand Down
1 change: 1 addition & 0 deletions packages/compass-aggregations/src/stores/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ export function activateAggregationsPlugin(
connectionInfoRef,
connectionScopedAppRegistry,
dataService,
collection: collectionModel,
})
)
);
Expand Down
1 change: 1 addition & 0 deletions packages/compass-generative-ai/src/atlas-ai-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type GenerativeAiInput = {
sampleDocuments?: Document[];
signal: AbortSignal;
requestId: string;
enableStorage: boolean;
};

// The size/token validation happens on the server, however, we do
Expand Down
57 changes: 41 additions & 16 deletions packages/compass-generative-ai/src/utils/gen-ai-prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export type PromptContextOptions = {
databaseName: string;
collectionName: string;
userId: string;
enableStorage: boolean;
schema?: unknown;
sampleDocuments?: unknown[];
};
Expand All @@ -82,7 +83,9 @@ function buildUserPromptForQuery({
collectionName,
schema,
sampleDocuments,
}: PromptContextOptions & { type: 'find' | 'aggregate' }): string {
}: Omit<PromptContextOptions, 'userId' | 'enableStorage'> & {
type: 'find' | 'aggregate';
}): string {
const messages = [];

const queryPrompt = [
Expand Down Expand Up @@ -156,50 +159,72 @@ export type AiQueryPrompt = {
metadata: {
instructions: string;
userId: string;
store: 'true' | 'false';
sensitiveStorage: 'sensitive';
};
} & (
| {
store: 'true';
sensitiveStorage: 'sensitive';
}
| {
store: 'false';
}
);
};

function buildMetadata({
instructions,
userId,
enableStorage,
}: {
instructions: string;
userId: string;
enableStorage: boolean;
}): AiQueryPrompt['metadata'] {
return {
instructions,
userId,
sensitiveStorage: 'sensitive',
store: 'true',
...(enableStorage
? {
sensitiveStorage: 'sensitive',
store: 'true',
}
: {
store: 'false',
}),
Comment on lines +205 to +212
Copy link

Copilot AI Dec 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The hardcoded string literals 'sensitive', 'true', and 'false' should be defined as named constants at the module level to prevent typos and improve maintainability. For example: const SENSITIVE_STORAGE = 'sensitive' as const.

Copilot uses AI. Check for mistakes.
};
}

export function buildFindQueryPrompt(
options: PromptContextOptions
): AiQueryPrompt {
export function buildFindQueryPrompt({
userId,
enableStorage,
...restOfTheOptions
}: PromptContextOptions): AiQueryPrompt {
const prompt = buildUserPromptForQuery({
type: 'find',
...options,
...restOfTheOptions,
});
const instructions = buildInstructionsForFindQuery();
return {
prompt,
metadata: buildMetadata({ instructions, userId: options.userId }),
metadata: buildMetadata({
instructions,
userId,
enableStorage,
}),
};
}

export function buildAggregateQueryPrompt(
options: PromptContextOptions
): AiQueryPrompt {
export function buildAggregateQueryPrompt({
userId,
enableStorage,
...restOfTheOptions
}: PromptContextOptions): AiQueryPrompt {
const prompt = buildUserPromptForQuery({
type: 'aggregate',
...options,
...restOfTheOptions,
});
const instructions = buildInstructionsForAggregateQuery();
return {
prompt,
metadata: buildMetadata({ instructions, userId: options.userId }),
metadata: buildMetadata({ instructions, userId, enableStorage }),
};
}
6 changes: 5 additions & 1 deletion packages/compass-query-bar/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import {
dataServiceLocator,
type DataServiceLocator,
} from '@mongodb-js/compass-connections/provider';
import { mongoDBInstanceLocator } from '@mongodb-js/compass-app-stores/provider';
import {
mongoDBInstanceLocator,
collectionModelLocator,
} from '@mongodb-js/compass-app-stores/provider';
import {
QueryBarComponentProvider,
useQueryBarComponent,
Expand Down Expand Up @@ -56,6 +59,7 @@ const QueryBarPlugin = registerCompassPlugin(
atlasAiService: atlasAiServiceLocator,
favoriteQueryStorageAccess: favoriteQueryStorageAccessLocator,
recentQueryStorageAccess: recentQueryStorageAccessLocator,
collection: collectionModelLocator,
}
);

Expand Down
3 changes: 3 additions & 0 deletions packages/compass-query-bar/src/stores/ai-query-reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ export const runAIQuery = (
logger: { log },
connectionInfoRef,
track,
collection,
}
) => {
const provideSampleDocuments =
Expand Down Expand Up @@ -218,6 +219,7 @@ export const runAIQuery = (
}
);
const schema = await getSimplifiedSchema(sampleDocuments);
const { isFLE } = await collection.fetchMetadata({ dataService });

const { collection: collectionName, database: databaseName } =
toNS(namespace);
Expand All @@ -235,6 +237,7 @@ export const runAIQuery = (
}
: undefined),
requestId,
enableStorage: !isFLE,
},
connectionInfo
);
Expand Down
20 changes: 18 additions & 2 deletions packages/compass-query-bar/src/stores/query-bar-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,18 @@ import type {
RecentQueryStorage,
} from '@mongodb-js/my-queries-storage/provider';
import type { TrackFunction } from '@mongodb-js/compass-telemetry';
import type Collection from 'mongodb-collection-model';

// Partial of DataService that mms shares with Compass.
type QueryBarDataService = Pick<DataService, 'sample' | 'getConnectionString'>;
type FetchCollectionMetadataDataServiceMethods =
| 'collectionStats'
| 'collectionInfo'
| 'listCollections'
| 'isListSearchIndexesSupported';
type QueryBarDataService = Pick<
DataService,
'sample' | 'getConnectionString' | FetchCollectionMetadataDataServiceMethods
>;

type QueryBarServices = {
instance: MongoDBInstance;
Expand All @@ -51,6 +60,7 @@ type QueryBarServices = {
atlasAiService: AtlasAiService;
favoriteQueryStorageAccess?: FavoriteQueryStorageAccess;
recentQueryStorageAccess?: RecentQueryStorageAccess;
collection: Collection;
};

// TODO(COMPASS-7412): this doesn't have service injector
Expand All @@ -73,14 +83,18 @@ export type RootState = ReturnType<typeof rootQueryBarReducer>;
export type QueryBarExtraArgs = {
globalAppRegistry: AppRegistry;
localAppRegistry: AppRegistry;
dataService: Pick<QueryBarDataService, 'sample'>;
dataService: Pick<
QueryBarDataService,
'sample' | FetchCollectionMetadataDataServiceMethods
>;
preferences: PreferencesAccess;
favoriteQueryStorage?: FavoriteQueryStorage;
recentQueryStorage?: RecentQueryStorage;
logger: Logger;
track: TrackFunction;
connectionInfoRef: ConnectionInfoRef;
atlasAiService: AtlasAiService;
collection: Collection;
};

export type QueryBarThunkDispatch<A extends AnyAction = AnyAction> =
Expand Down Expand Up @@ -126,6 +140,7 @@ export function activatePlugin(
atlasAiService,
favoriteQueryStorageAccess,
recentQueryStorageAccess,
collection,
} = services;

const favoriteQueryStorage = favoriteQueryStorageAccess?.getStorage();
Expand Down Expand Up @@ -158,6 +173,7 @@ export function activatePlugin(
track,
connectionInfoRef,
atlasAiService,
collection,
}
);

Expand Down
Loading