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
send metadata to api
  • Loading branch information
mabaasit committed Dec 9, 2025
commit 3aa3d8e6ac66b22ee929671be95ae624e95de816
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,7 @@ describe('AtlasAiService', function () {
const mockAtlasService = new MockAtlasService();
await preferences.savePreferences({
enableChatbotEndpointForGenAI: true,
telemetryAtlasUserId: '1234',
});
atlasAiService = new AtlasAiService({
apiURLPreset: 'cloud',
Expand Down Expand Up @@ -1047,7 +1048,11 @@ describe('AtlasAiService', function () {
const requestBody = JSON.parse(args[1].body as string);

expect(requestBody.model).to.equal('mongodb-chat-latest');
expect(requestBody.store).to.equal(false);
expect(requestBody.metadata).to.deep.equal({
userId: '1234',
store: 'true',
sensitiveStorage: 'sensitive',
});
expect(requestBody.instructions).to.be.a('string');
expect(requestBody.input).to.be.an('array');

Expand Down
27 changes: 24 additions & 3 deletions packages/compass-generative-ai/src/atlas-ai-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,15 @@ export type MockDataSchemaResponse = z.infer<
typeof MockDataSchemaResponseShape
>;

// TODO: Evaluate this
function getActiveUserId(preferences: PreferencesAccess): string {
const { currentUserId, telemetryAnonymousId, telemetryAtlasUserId } =
preferences.getPreferences();
return (
currentUserId || telemetryAnonymousId || telemetryAtlasUserId || 'unknown'
);
}
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 fallback chain uses truthiness which could return 'unknown' for empty strings. Consider using nullish coalescing (??) instead: currentUserId ?? telemetryAnonymousId ?? telemetryAtlasUserId ?? 'unknown' to only fallback on null/undefined values.

Copilot generated this review using guidance from repository custom instructions.

/**
* The type of resource from the natural language query REST API
*/
Expand Down Expand Up @@ -304,7 +313,13 @@ export class AtlasAiService {
PLACEHOLDER_BASE_URL,
this.atlasService.assistantApiEndpoint()
);
return this.atlasService.authenticatedFetch(uri, init);
return this.atlasService.authenticatedFetch(uri, {
...init,
headers: {
...(init?.headers ?? {}),
entrypoint: 'natural-language-to-mql',
},
});
},
// TODO(COMPASS-10125): Switch the model to `mongodb-slim-latest` when
// enabling this feature (to use edu-chatbot for GenAI).
Expand Down Expand Up @@ -445,7 +460,10 @@ export class AtlasAiService {
connectionInfo: ConnectionInfo
) {
if (this.preferences.getPreferences().enableChatbotEndpointForGenAI) {
const message = buildAggregateQueryPrompt(input);
const message = buildAggregateQueryPrompt({
...input,
userId: getActiveUserId(this.preferences),
});
return this.generateQueryUsingChatbot(
message,
validateAIAggregationResponse,
Expand All @@ -467,7 +485,10 @@ export class AtlasAiService {
connectionInfo: ConnectionInfo
) {
if (this.preferences.getPreferences().enableChatbotEndpointForGenAI) {
const message = buildFindQueryPrompt(input);
const message = buildFindQueryPrompt({
...input,
userId: getActiveUserId(this.preferences),
});
return this.generateQueryUsingChatbot(message, validateAIQueryResponse, {
signal: input.signal,
});
Expand Down
5 changes: 3 additions & 2 deletions packages/compass-generative-ai/src/utils/gen-ai-response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ export async function getAiQueryResponse(
message: AiQueryPrompt,
abortSignal: AbortSignal
): Promise<string> {
const { instructions, ...restOfMetadata } = message.metadata;
const response = streamText({
model,
messages: [{ role: 'user', content: message.prompt }],
providerOptions: {
openai: {
store: false,
instructions: message.metadata.instructions,
instructions,
metadata: restOfMetadata,
},
},
abortSignal,
Expand Down