@@ -106,6 +106,65 @@ export { type MetabasePageType } from '../defaultState'
106106
107107export type MetabaseAppState = MetabaseAppStateSQLEditor | MetabaseAppStateDashboard | MetabaseSemanticQueryAppState | MetabaseAppStateMBQLEditor ;
108108
109+ async function getRelevantEntitiesWithFields ( sqlQuery : string ) : Promise < FormattedTable [ ] > {
110+ const appSettings = RPCs . getAppSettings ( ) ;
111+
112+ // Early return if conditions not met
113+ if ( ! appSettings . analystMode || ! appSettings . manuallyLimitContext ) {
114+ return [ ] ;
115+ }
116+
117+ const dbId = await getSelectedDbId ( ) ;
118+ const selectedDatabaseInfo = dbId ? await getDatabaseInfo ( dbId ) : undefined ;
119+ const defaultSchema = selectedDatabaseInfo ?. default_schema ;
120+
121+ const sqlTables = getTablesFromSqlRegex ( sqlQuery ) ;
122+ const selectedCatalogObj = find ( appSettings . availableCatalogs , { name : appSettings . selectedCatalog } ) ;
123+ const selectedCatalog = get ( selectedCatalogObj , 'content' ) ;
124+
125+ // Apply default schema to tables if needed
126+ if ( defaultSchema ) {
127+ sqlTables . forEach ( ( table ) => {
128+ if ( table . schema === undefined || table . schema === '' ) {
129+ table . schema = defaultSchema ;
130+ }
131+ } ) ;
132+ }
133+
134+ let relevantTablesWithFields = await getTablesWithFields ( appSettings . tableDiff , appSettings . drMode , ! ! selectedCatalog , sqlTables , [ ] ) ;
135+
136+ // Add defaultSchema back to relevantTablesWithFields
137+ relevantTablesWithFields = relevantTablesWithFields . map ( table => {
138+ if ( table . schema === undefined || table . schema === '' ) {
139+ table . schema = defaultSchema || 'unknown' ;
140+ }
141+ return table ;
142+ } ) ;
143+
144+ const allModels = dbId ? await getAllRelevantModelsForSelectedDb ( dbId ) : [ ] ;
145+ const relevantModels = await getSelectedAndRelevantModels ( sqlQuery || "" , appSettings . selectedModels , allModels ) ;
146+ const relevantModelsWithFields = await getModelsWithFields ( relevantModels ) ;
147+
148+ // Transform and combine tables and models with type annotations
149+ const relevantTablesWithFieldsAndType = relevantTablesWithFields . map ( table => ( {
150+ ...table ,
151+ type : 'table' ,
152+ } ) ) ;
153+
154+ const relevantModelsWithFieldsAndType = relevantModelsWithFields . map ( model => ( {
155+ ...model ,
156+ type : 'model' ,
157+ name : model . modelName || '' ,
158+ id : model . modelId || 0 ,
159+ schema : '' ,
160+ table : undefined ,
161+ modelId : undefined ,
162+ modelName : undefined ,
163+ } ) ) ;
164+
165+ return [ ...relevantTablesWithFieldsAndType , ...relevantModelsWithFieldsAndType ] as FormattedTable [ ] ;
166+ }
167+
109168export async function convertDOMtoStateSQLQueryV2 ( ) : Promise < MetabaseAppStateSQLEditorV2 > {
110169 const [ metabaseUrl , currentCardRaw , outputMarkdown , parameterValues ] = await Promise . all ( [
111170 RPCs . queryURL ( ) ,
@@ -116,7 +175,9 @@ export async function convertDOMtoStateSQLQueryV2() : Promise<MetabaseAppStateSQ
116175 const currentCard = processCard ( currentCardRaw ) ;
117176 const metabaseOrigin = new URL ( metabaseUrl ) . origin ;
118177 const isEmbedded = getParsedIframeInfo ( ) . isEmbedded
119- const limitedEntities : MetabaseTableOrModel [ ] = [ ]
178+ const limitedEntities = await getRelevantEntitiesWithFields (
179+ get ( currentCard , 'dataset_query.native.query' , '' ) || ''
180+ ) ;
120181 return {
121182 type : MetabaseAppStateType . SQLEditor ,
122183 version : '2' ,
@@ -268,7 +329,7 @@ export async function convertDOMtoState() {
268329// return await semanticQueryState();
269330// }
270331 const appSettings = RPCs . getAppSettings ( )
271- if ( appSettings . useV2States ) {
332+ if ( appSettings . useV2States && appSettings . analystMode ) {
272333 return await convertDOMtoStateSQLQueryV2 ( ) ;
273334 }
274335 return await convertDOMtoStateSQLQuery ( ) ;
0 commit comments