Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
SpinLoaderWithLabel,
} from '@mongodb-js/compass-components';
import React from 'react';
import { connect } from 'react-redux';
import FieldSelector from './schema-field-selector';
import FakerMappingSelector from './faker-mapping-selector';
import type {
Expand Down Expand Up @@ -172,7 +173,6 @@ const FakerSchemaEditorScreen = ({
onSchemaConfirmed,
fakerSchemaGenerationState,
}: {
isSchemaConfirmed: boolean;
onSchemaConfirmed: (isConfirmed: boolean) => void;
fakerSchemaGenerationState: MockDataGeneratorState;
}) => {
Expand Down Expand Up @@ -200,12 +200,12 @@ const FakerSchemaEditorScreen = ({
)}
{fakerSchemaGenerationState.status === 'completed' && (
<FakerSchemaEditorContent
fakerSchema={fakerSchemaGenerationState.fakerSchema}
fakerSchema={fakerSchemaGenerationState.editedFakerSchema}
onSchemaConfirmed={onSchemaConfirmed}
/>
)}
</div>
);
};

export default FakerSchemaEditorScreen;
export default connect()(FakerSchemaEditorScreen);
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,15 @@ describe('MockDataGeneratorModal', () => {
currentStep: MockDataGeneratorStep.GENERATE_DATA,
fakerSchemaGeneration: {
status: 'completed',
fakerSchema: {
originalLlmResponse: {
name: {
fakerMethod: 'person.firstName',
fakerArgs: [],
probability: 1.0,
mongoType: 'String',
},
},
editedFakerSchema: {
name: {
fakerMethod: 'person.firstName',
fakerArgs: [],
Expand All @@ -848,7 +856,15 @@ describe('MockDataGeneratorModal', () => {
currentStep: MockDataGeneratorStep.GENERATE_DATA,
fakerSchemaGeneration: {
status: 'completed',
fakerSchema: {
originalLlmResponse: {
name: {
fakerMethod: 'person.firstName',
fakerArgs: [],
probability: 1.0,
mongoType: 'String',
},
},
editedFakerSchema: {
name: {
fakerMethod: 'person.firstName',
fakerArgs: [],
Expand All @@ -872,7 +888,15 @@ describe('MockDataGeneratorModal', () => {
currentStep: MockDataGeneratorStep.GENERATE_DATA,
fakerSchemaGeneration: {
status: 'completed',
fakerSchema: {
originalLlmResponse: {
name: {
fakerMethod: 'person.firstName',
fakerArgs: [],
probability: 1.0,
mongoType: 'String',
},
},
editedFakerSchema: {
name: {
fakerMethod: 'person.firstName',
fakerArgs: [],
Expand Down Expand Up @@ -919,7 +943,15 @@ describe('MockDataGeneratorModal', () => {
connectionInfo: atlasConnectionInfo,
fakerSchemaGeneration: {
status: 'completed',
fakerSchema: {
originalLlmResponse: {
name: {
fakerMethod: 'person.firstName',
fakerArgs: [],
probability: 1.0,
mongoType: 'String',
},
},
editedFakerSchema: {
name: {
fakerMethod: 'person.firstName',
fakerArgs: [],
Expand Down Expand Up @@ -971,7 +1003,15 @@ describe('MockDataGeneratorModal', () => {
currentStep: MockDataGeneratorStep.GENERATE_DATA,
fakerSchemaGeneration: {
status: 'completed',
fakerSchema: {
originalLlmResponse: {
name: {
fakerMethod: 'person.firstName',
fakerArgs: [],
probability: 1.0,
mongoType: 'String',
},
},
editedFakerSchema: {
name: {
fakerMethod: 'person.firstName',
fakerArgs: [],
Expand Down Expand Up @@ -1001,7 +1041,21 @@ describe('MockDataGeneratorModal', () => {
currentStep: MockDataGeneratorStep.GENERATE_DATA,
fakerSchemaGeneration: {
status: 'completed',
fakerSchema: {
originalLlmResponse: {
name: {
fakerMethod: 'person.firstName',
fakerArgs: [],
probability: 1.0,
mongoType: 'String',
},
email: {
fakerMethod: 'internet.email',
fakerArgs: [],
probability: 1.0,
mongoType: 'String',
},
},
editedFakerSchema: {
name: {
fakerMethod: 'person.firstName',
fakerArgs: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ const MockDataGeneratorModal = ({
case MockDataGeneratorStep.SCHEMA_EDITOR:
return (
<FakerSchemaEditorScreen
isSchemaConfirmed={isSchemaConfirmed}
onSchemaConfirmed={setIsSchemaConfirmed}
fakerSchemaGenerationState={fakerSchemaGenerationState}
/>
Expand All @@ -100,7 +99,7 @@ const MockDataGeneratorModal = ({
<PreviewScreen
confirmedFakerSchema={
fakerSchemaGenerationState.status === 'completed'
? fakerSchemaGenerationState.fakerSchema
? fakerSchemaGenerationState.editedFakerSchema
: {}
}
/>
Expand All @@ -111,7 +110,6 @@ const MockDataGeneratorModal = ({
}, [
currentStep,
fakerSchemaGenerationState,
isSchemaConfirmed,
documentCount,
setDocumentCount,
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ const mapStateToProps = (state: CollectionState) => {
return {
fakerSchema:
fakerSchemaGeneration.status === 'completed'
? fakerSchemaGeneration.fakerSchema
? fakerSchemaGeneration.editedFakerSchema
: null,
namespace,
arrayLengthMap:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ type MockDataGeneratorInProgressState = {

type MockDataGeneratorCompletedState = {
status: 'completed';
fakerSchema: FakerSchema;
originalLlmResponse: Readonly<FakerSchema>; // Immutable LLM response
editedFakerSchema: FakerSchema; // User-modified version
requestId: string;
};

Expand Down
110 changes: 108 additions & 2 deletions packages/compass-collection/src/modules/collection-tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ import type { AtlasAiService } from '@mongodb-js/compass-generative-ai/provider'
import type { experimentationServiceLocator } from '@mongodb-js/compass-telemetry/provider';
import { type Logger, mongoLogId } from '@mongodb-js/compass-logging/provider';
import { type PreferencesAccess } from 'compass-preferences-model/provider';
import type { MockDataSchemaRequest } from '@mongodb-js/compass-generative-ai';
import type {
MockDataSchemaRequest,
MongoDBFieldType,
} from '@mongodb-js/compass-generative-ai';
import { isInternalFieldPath } from 'hadron-document';
import toNS from 'mongodb-ns';
import {
Expand Down Expand Up @@ -127,6 +130,8 @@ export enum CollectionActions {
FakerMappingGenerationStarted = 'compass-collection/FakerMappingGenerationStarted',
FakerMappingGenerationCompleted = 'compass-collection/FakerMappingGenerationCompleted',
FakerMappingGenerationFailed = 'compass-collection/FakerMappingGenerationFailed',
FakerFieldTypeChanged = 'compass-collection/FakerFieldTypeChanged',
FakerFieldMethodChanged = 'compass-collection/FakerFieldMethodChanged',
}

interface CollectionMetadataFetchedAction {
Expand Down Expand Up @@ -196,6 +201,18 @@ export interface FakerMappingGenerationFailedAction {
requestId: string;
}

export interface FakerFieldTypeChangedAction {
type: CollectionActions.FakerFieldTypeChanged;
fieldPath: string;
mongoType: MongoDBFieldType;
}

export interface FakerFieldMethodChangedAction {
type: CollectionActions.FakerFieldMethodChanged;
fieldPath: string;
fakerMethod: string;
}

const reducer: Reducer<CollectionState, Action> = (
state = {
// TODO(COMPASS-7782): use hook to get the workspace tab id instead
Expand Down Expand Up @@ -457,7 +474,8 @@ const reducer: Reducer<CollectionState, Action> = (
...state,
fakerSchemaGeneration: {
status: 'completed',
fakerSchema: action.fakerSchema,
originalLlmResponse: action.fakerSchema,
editedFakerSchema: action.fakerSchema, // Initially same as LLM response
requestId: action.requestId,
},
};
Expand Down Expand Up @@ -487,6 +505,72 @@ const reducer: Reducer<CollectionState, Action> = (
};
}

if (
isAction<FakerFieldTypeChangedAction>(
action,
CollectionActions.FakerFieldTypeChanged
)
) {
if (state.fakerSchemaGeneration.status !== 'completed') {
return state;
}

const { fieldPath, mongoType } = action;
const currentMapping =
state.fakerSchemaGeneration.editedFakerSchema[fieldPath];

if (!currentMapping) {
return state;
}

return {
...state,
fakerSchemaGeneration: {
...state.fakerSchemaGeneration,
editedFakerSchema: {
...state.fakerSchemaGeneration.editedFakerSchema,
[fieldPath]: {
...currentMapping,
mongoType,
},
},
},
};
}

if (
isAction<FakerFieldMethodChangedAction>(
action,
CollectionActions.FakerFieldMethodChanged
)
) {
if (state.fakerSchemaGeneration.status !== 'completed') {
return state;
}

const { fieldPath, fakerMethod } = action;
const currentMapping =
state.fakerSchemaGeneration.editedFakerSchema[fieldPath];

if (!currentMapping) {
return state;
}

return {
...state,
fakerSchemaGeneration: {
...state.fakerSchemaGeneration,
editedFakerSchema: {
...state.fakerSchemaGeneration.editedFakerSchema,
[fieldPath]: {
...currentMapping,
fakerMethod,
},
},
},
};
}

return state;
};

Expand Down Expand Up @@ -528,6 +612,28 @@ export const mockDataGeneratorPreviousButtonClicked = (): CollectionThunkAction<
};
};

export const fakerFieldTypeChanged = (
fieldPath: string,
mongoType: MongoDBFieldType
): FakerFieldTypeChangedAction => {
return {
type: CollectionActions.FakerFieldTypeChanged,
fieldPath,
mongoType,
};
};

export const fakerFieldMethodChanged = (
fieldPath: string,
fakerMethod: string
): FakerFieldMethodChangedAction => {
return {
type: CollectionActions.FakerFieldMethodChanged,
fieldPath,
fakerMethod,
};
};

export const selectTab = (
tabName: CollectionSubtab
): CollectionThunkAction<void> => {
Expand Down
Loading