Skip to content

Commit 777c768

Browse files
authored
feat(gen-ai): wrap prompt in tags for storage COMPASS-10083 (#7636)
* wrap prompt in tags * send metadata to api * assertions in e2e test * clean up * handle fle collections * clean up and fix tests * types fix * pass request id in headers * query-bar fixes * aggregation fixes * fix tests and check * fix tests * fix tests * escape user input * escape user_prompt tag from input * allow headers for cors in tests * hash user id * fix test
1 parent f232bba commit 777c768

File tree

24 files changed

+345
-81
lines changed

24 files changed

+345
-81
lines changed

package-lock.json

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/compass-aggregations/src/modules/data-service.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import type { DataService as OriginalDataService } from 'mongodb-data-service';
22

3+
type FetchCollectionMetadataDataServiceMethods =
4+
| 'collectionStats'
5+
| 'collectionInfo'
6+
| 'listCollections'
7+
| 'isListSearchIndexesSupported';
8+
39
export type RequiredDataServiceProps =
10+
| FetchCollectionMetadataDataServiceMethods
411
| 'isCancelError'
512
| 'estimatedCount'
613
| 'aggregate'

packages/compass-aggregations/src/modules/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import type {
4949
ConnectionScopedAppRegistry,
5050
} from '@mongodb-js/compass-connections/provider';
5151
import type { TrackFunction } from '@mongodb-js/compass-telemetry';
52+
import type Collection from 'mongodb-collection-model';
5253
/**
5354
* The main application reducer.
5455
*
@@ -110,6 +111,7 @@ export type PipelineBuilderExtraArgs = {
110111
connectionScopedAppRegistry: ConnectionScopedAppRegistry<
111112
'open-export' | 'view-edited' | 'agg-pipeline-out-executed'
112113
>;
114+
collection: Collection;
113115
};
114116

115117
export type PipelineBuilderThunkDispatch<A extends Action = AnyAction> =

packages/compass-aggregations/src/modules/pipeline-builder/pipeline-ai.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ export const runAIPipelineGeneration = (
225225
logger: { log, mongoLogId },
226226
track,
227227
connectionInfoRef,
228+
collection,
228229
}
229230
) => {
230231
const {
@@ -286,6 +287,9 @@ export const runAIPipelineGeneration = (
286287
}
287288
)) || [];
288289
const schema = await getSimplifiedSchema(sampleDocuments);
290+
const { isFLE } = await collection.fetchMetadata({
291+
dataService: dataService!,
292+
});
289293

290294
const { collection: collectionName, database: databaseName } =
291295
toNS(namespace);
@@ -303,6 +307,7 @@ export const runAIPipelineGeneration = (
303307
}
304308
: undefined),
305309
requestId,
310+
enableStorage: !isFLE,
306311
},
307312
connectionInfo
308313
);

packages/compass-aggregations/src/modules/pipeline-builder/stage-editor.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,11 @@ function createStore({
141141
dataService: {} as any,
142142
connectionInfoRef,
143143
connectionScopedAppRegistry,
144+
collection: {
145+
fetchMetadata() {
146+
return Promise.resolve({ isFLE: false });
147+
},
148+
} as any,
144149
})
145150
)
146151
);

packages/compass-aggregations/src/stores/store.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ export function activateAggregationsPlugin(
188188
connectionInfoRef,
189189
connectionScopedAppRegistry,
190190
dataService,
191+
collection: collectionModel,
191192
})
192193
)
193194
);

packages/compass-aggregations/test/configure-store.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ function getMockedPluginArgs(
3232
CompassAggregationsPlugin.provider.withMockServices({
3333
atlasAiService,
3434
collection: {
35+
fetchMetadata: () => ({}),
3536
toJSON: () => ({}),
3637
on: () => {},
3738
removeListener: () => {},

packages/compass-crud/test/render-with-query-bar.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@ export const MockQueryBarPlugin: typeof QueryBarPlugin =
1818
getConnectionString() {
1919
return { hosts: [] } as any;
2020
},
21-
},
21+
} as any,
2222
instance: { on() {}, removeListener() {} } as any,
2323
favoriteQueryStorageAccess: compassFavoriteQueryStorageAccess,
2424
recentQueryStorageAccess: compassRecentQueryStorageAccess,
2525
atlasAiService: {} as any,
26+
collection: {
27+
fetchMetadata: () => Promise.resolve({} as any),
28+
} as any,
2629
});
2730

2831
export const renderWithQueryBar = (

packages/compass-e2e-tests/helpers/assistant-service.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,16 +156,16 @@ export async function startMockAssistantServer(
156156
getResponse: () => MockAssistantResponse;
157157
setResponse: (response: MockAssistantResponse) => void;
158158
getRequests: () => {
159-
content: any;
160-
req: any;
159+
content: Record<string, any>;
160+
req: http.IncomingMessage;
161161
}[];
162162
endpoint: string;
163163
server: http.Server;
164164
stop: () => Promise<void>;
165165
}> {
166166
let requests: {
167-
content: any;
168-
req: any;
167+
content: Record<string, any>;
168+
req: http.IncomingMessage;
169169
}[] = [];
170170
let response = _response;
171171
const server = http
@@ -174,7 +174,7 @@ export async function startMockAssistantServer(
174174
res.setHeader('Access-Control-Allow-Methods', 'POST, OPTIONS');
175175
res.setHeader(
176176
'Access-Control-Allow-Headers',
177-
'Content-Type, Authorization, X-Request-Origin, User-Agent, X-CSRF-Token, X-CSRF-Time'
177+
'Content-Type, Authorization, X-Request-Origin, User-Agent, X-CSRF-Token, X-CSRF-Time, Entrypoint, X-Client-Request-Id'
178178
);
179179
res.setHeader('Access-Control-Allow-Credentials', 'true');
180180

packages/compass-e2e-tests/tests/collection-ai-query.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,16 @@ describe('Collection ai query with chatbot (with mocked backend)', function () {
265265
expect(requests.length).to.equal(1);
266266

267267
const queryRequest = requests[0];
268+
expect(queryRequest.req.headers).to.have.property('x-client-request-id');
268269
// TODO(COMPASS-10125): Switch the model to `mongodb-slim-latest` when
269270
// enabling this feature.
270271
expect(queryRequest.content.model).to.equal('mongodb-chat-latest');
271272
expect(queryRequest.content.instructions).to.be.string;
273+
expect(queryRequest.content.metadata).to.have.property('userId');
274+
expect(queryRequest.content.metadata.store).to.have.equal('true');
275+
expect(queryRequest.content.metadata.sensitiveStorage).to.have.equal(
276+
'sensitive'
277+
);
272278
expect(queryRequest.content.input).to.be.an('array').of.length(1);
273279

274280
const message = queryRequest.content.input[0];

0 commit comments

Comments
 (0)