Skip to content

Commit 0aaab0a

Browse files
authored
Feature/filter params v2 (#280)
* Enable useV2States for everyone in analyst mode * Add web & ext commit ID to requests * Add relevantEntitiesWithFields for state v2
1 parent 4b97566 commit 0aaab0a

File tree

5 files changed

+91
-14
lines changed

5 files changed

+91
-14
lines changed

apps/src/metabase/helpers/DOMToState.ts

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,65 @@ export { type MetabasePageType } from '../defaultState'
106106

107107
export 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+
109168
export 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();

web/src/helpers/LLM/index.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { planActionsOpenAI } from "./OpenAI";
22
import { getState } from '../../state/store'
33
import { planActionsRemote } from "./remote";
44
import { Tasks, ToolCalls } from "../../state/chat/reducer";
5-
import { LLMContext, LLMResponse, LLMSettings } from "./types";
5+
import { LLMContext, LLMResponse, LLMSettings, LLMMetadata } from "./types";
66

77
export type researchMode = 'simple' | 'deepResearchPlanner' | 'deepResearchTool'
88

@@ -14,10 +14,7 @@ export type PlanActionsParams = {
1414
deepResearch: researchMode,
1515
tasks: Tasks,
1616
conversationID: string,
17-
meta?: {
18-
timeDelta: number;
19-
threadTimeDelta?: number;
20-
},
17+
meta?: LLMMetadata,
2118
}
2219
export async function planActions(params: PlanActionsParams ) : Promise<LLMResponse> {
2320
const { isLocal } = getState().settings

web/src/helpers/LLM/types.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ import { Tasks, ToolCalls } from "../../state/chat/reducer";
33

44
export type LLMContext = Array<ChatCompletionMessageParam>
55

6+
export type LLMMetadata = {
7+
timeDelta: number; // Time difference in milliseconds between current and last message
8+
threadTimeDelta?: number; // Time difference from last user message in previous thread (when timeDelta is 0)
9+
gitCommitId?: string; // Git commit ID from iframe props (extension/host)
10+
webGitCommitId?: string; // Git commit ID from web package
11+
}
12+
613
export type LLMContextWithMeta = {
714
context: LLMContext;
8-
meta: {
9-
timeDelta: number; // Time difference in milliseconds between current and last message
10-
threadTimeDelta?: number; // Time difference from last user message in previous thread (when timeDelta is 0)
11-
};
15+
meta: LLMMetadata;
1216
}
1317
export type LLMResponse = {
1418
tool_calls: ToolCalls,

web/src/planner/utils.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11

2-
import { LLMContext, LLMContextWithMeta } from '../helpers/LLM/types';
2+
import { LLMContext, LLMContextWithMeta, LLMMetadata } from '../helpers/LLM/types';
33
import { ChatMessage, UserChatMessage } from '../state/chat/reducer';
44
import { renderString } from '../helpers/templatize';
55
import { formatLLMMessageHistory } from '../helpers/LLM/context';
66
import _ from 'lodash';
77
import { AppState } from 'apps/types';
88
import { getState } from '../state/store';
9+
import { getParsedIframeInfo } from '../helpers/origin';
910

1011

1112
type LLMPrompts = {
@@ -107,13 +108,22 @@ export function getLLMContextFromState(
107108
...context,
108109
];
109110

110-
const meta: { timeDelta: number; threadTimeDelta?: number } = {
111+
const iframeInfo = getParsedIframeInfo();
112+
const meta: LLMMetadata = {
111113
timeDelta
112114
};
113115

114116
if (threadTimeDelta !== undefined) {
115117
meta.threadTimeDelta = threadTimeDelta;
116118
}
119+
120+
if (iframeInfo.gitCommitId) {
121+
meta.gitCommitId = iframeInfo.gitCommitId;
122+
}
123+
124+
if (iframeInfo.webGitCommitId) {
125+
meta.webGitCommitId = iframeInfo.webGitCommitId;
126+
}
117127

118128
return {
119129
context: llmContext,

web/src/state/store.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,11 @@ const migrations = {
509509
newState.settings.currentEmail = newState.auth?.email
510510
return newState
511511
},
512+
51: (state: RootState) => {
513+
let newState = {...state}
514+
newState.settings.useV2States = true
515+
return newState
516+
}
512517
}
513518

514519
const BLACKLIST = ['billing', 'cache', userStateApi.reducerPath]
@@ -519,7 +524,7 @@ if (isEmbedded) {
519524

520525
const persistConfig = {
521526
key: 'root',
522-
version: 50,
527+
version: 51,
523528
storage,
524529
blacklist: BLACKLIST,
525530
// @ts-ignore

0 commit comments

Comments
 (0)