Skip to content

Commit 97fe419

Browse files
committed
fix various issues around using fields from multiple tables
1 parent 3d55c05 commit 97fe419

File tree

4 files changed

+43
-49
lines changed

4 files changed

+43
-49
lines changed

py-src/data_formulator/agents/agent_data_transform_v2.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,10 @@ def process_gpt_response(self, input_tables, messages, response):
226226
if len(code_blocks) > 0:
227227
code_str = code_blocks[-1]
228228

229+
for table in input_tables:
230+
logger.info(f"Table: {table['name']}")
231+
logger.info(table['rows'])
232+
229233
try:
230234
result = py_sandbox.run_transform_in_sandbox2020(code_str, [t['rows'] for t in input_tables])
231235
result['code'] = code_str

src/components/ComponentType.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ export const duplicateField = (field: FieldItem) => {
4444
}
4545

4646
export interface Trigger {
47-
tableId: string,
47+
tableId: string, // on which table this action is triggered
48+
49+
sourceTableIds: string[], // which tables are used in the trigger
4850

4951
chartRef?: string, // what's the intented chart from the user when running formulation
5052
instruction: string

src/views/EncodingShelfCard.tsx

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,28 +52,38 @@ export interface EncodingShelfCardProps {
5252
noBorder?: boolean;
5353
}
5454

55-
let selectBaseTables = (activeFields: FieldItem[], conceptShelfItems: FieldItem[], tables: DictTable[]) : DictTable[] => {
55+
let selectBaseTables = (activeFields: FieldItem[], conceptShelfItems: FieldItem[], tables: DictTable[], currentTable: DictTable) : DictTable[] => {
5656

57-
// if there is no active fields at all!!
58-
if (activeFields.length == 0) {
59-
return [tables[0]];
60-
}
57+
let baseTables = [];
6158

62-
let activeBaseFields = conceptShelfItems.filter((field) => {
63-
return activeFields.map(f => f.source == "derived" ? findBaseFields(f, conceptShelfItems).map(f2 => f2.id) : [f.id]).flat().includes(field.id);
64-
});
59+
if (currentTable.derive) {
60+
baseTables = currentTable.derive.source.map(t => tables.find(t2 => t2.id == t) as DictTable);
61+
} else {
62+
baseTables.push(currentTable);
63+
}
6564

66-
let activeOriginalFields = activeBaseFields.filter(field => field.source == "original");
67-
let activeCustomFields = activeBaseFields.filter(field => field.source == "custom");
68-
let activeDerivedFields = activeFields.filter(f => f.source == "derived");
65+
// if there is no active fields at all!!
66+
if (activeFields.length == 0) {
67+
return [currentTable];
68+
} else {
69+
// find what are other tables that was used to derive the active fields
70+
let activeBaseFields = conceptShelfItems.filter((field) => {
71+
return activeFields.map(f => f.source == "derived" ? findBaseFields(f, conceptShelfItems).map(f2 => f2.id) : [f.id]).flat().includes(field.id);
72+
});
73+
74+
let activeOriginalFields = activeBaseFields.filter(field => field.source == "original");
75+
76+
if (activeOriginalFields.length == 0 && activeFields.length > 0 && tables.length > 0) {
77+
return [currentTable];
78+
}
79+
80+
// find all tables that contains the active original fields
81+
let tablesToAdd = tables.filter(t => activeOriginalFields.map(f => f.tableRef as string).includes(t.id));
6982

70-
if (activeOriginalFields.length == 0 && activeFields.length > 0 && tables.length > 0) {
71-
return [tables[0]];
83+
baseTables.push(...tablesToAdd.filter(t => !baseTables.map(t2 => t2.id).includes(t.id)));
7284
}
7385

74-
let baseTables = tables.filter(t => activeOriginalFields.map(f => f.tableRef as string).includes(t.id));
75-
76-
return baseTables
86+
return baseTables;
7787
}
7888

7989
export const TriggerCard: FC<{className?: string, trigger: Trigger, hideFields?: boolean, label?: string}> = function ({ label, className, trigger, hideFields }) {
@@ -104,7 +114,7 @@ export const TriggerCard: FC<{className?: string, trigger: Trigger, hideFields?:
104114
sx={{color:'inherit', maxWidth: '110px', marginLeft: "2px", height: 18, fontSize: 12, borderRadius: '4px',
105115
border: '1px solid rgb(250 235 215)', background: 'rgb(250 235 215 / 70%)',
106116
'& .MuiChip-label': { paddingLeft: '6px', paddingRight: '6px' }}}
107-
label={`${field.name}`} />]
117+
label={`${field?.name}`} />]
108118
})
109119
}
110120

@@ -190,6 +200,7 @@ export const EncodingShelfCard: FC<EncodingShelfCardProps> = function ({ chartId
190200
const tables = useSelector((state: DataFormulatorState) => state.tables);
191201
const charts = useSelector((state: DataFormulatorState) => state.charts);
192202
const betaMode = useSelector((state: DataFormulatorState) => state.betaMode);
203+
193204
let activeModel = useSelector(dfSelectors.getActiveModel);
194205

195206
let [prompt, setPrompt] = useState<string>(trigger?.instruction || "");
@@ -233,7 +244,7 @@ export const EncodingShelfCard: FC<EncodingShelfCardProps> = function ({ chartId
233244
let deriveNewData = (overrideTableId?: string) => {
234245

235246
let mode = 'formulate';
236-
let baseTables = selectBaseTables(activeFields, conceptShelfItems, tables);
247+
let baseTables = selectBaseTables(activeFields, conceptShelfItems, tables, currentTable);
237248

238249
let instruction = (chart.chartType == 'Auto' && prompt == "") ? "let's get started" : prompt;
239250

@@ -352,6 +363,7 @@ export const EncodingShelfCard: FC<EncodingShelfCardProps> = function ({ chartId
352363
let triggerChartSpec = duplicateChart(chart);
353364
let currentTrigger: Trigger = {
354365
tableId: currentTable.id,
366+
sourceTableIds: baseTables.map(t => t.id),
355367
instruction: instruction,
356368
chartRef: triggerChartSpec.id,
357369
resultTableId: candidateTableId

src/views/EncodingShelfThread.tsx

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -148,30 +148,6 @@ export let ChartElementFC: FC<{chart: Chart, tableRows: any[], boxWidth?: number
148148
return element;
149149
}
150150

151-
let selectBaseTables = (activeFields: FieldItem[], conceptShelfItems: FieldItem[], tables: DictTable[]) : DictTable[] => {
152-
153-
// if there is no active fields at all!!
154-
if (activeFields.length == 0) {
155-
return [tables[0]];
156-
}
157-
158-
let activeBaseFields = conceptShelfItems.filter((field) => {
159-
return activeFields.map(f => f.source == "derived" ? findBaseFields(f, conceptShelfItems).map(f2 => f2.id) : [f.id]).flat().includes(field.id);
160-
});
161-
162-
let activeOriginalFields = activeBaseFields.filter(field => field.source == "original");
163-
let activeCustomFields = activeBaseFields.filter(field => field.source == "custom");
164-
let activeDerivedFields = activeFields.filter(f => f.source == "derived");
165-
166-
if (activeOriginalFields.length == 0 && activeFields.length > 0 && tables.length > 0) {
167-
return [tables[0]];
168-
}
169-
170-
let baseTables = tables.filter(t => activeOriginalFields.map(f => f.tableRef as string).includes(t.id));
171-
172-
return baseTables
173-
}
174-
175151
export const EncodingShelfThread: FC<EncodingShelfThreadProps> = function ({ chartId }) {
176152

177153
// reference to states
@@ -204,11 +180,11 @@ export const EncodingShelfThread: FC<EncodingShelfThreadProps> = function ({ cha
204180

205181
let mode = 'formulate';
206182

207-
let sourceTable = tables.find(t => t.id == trigger.tableId) as DictTable;
183+
let triggerTable = tables.find(t => t.id == trigger.tableId) as DictTable;
184+
185+
let baseTables = tables.filter(t => trigger.sourceTableIds.includes(t.id));
208186
let overrideTableId = trigger.resultTableId;
209187

210-
let baseTableIds = sourceTable.derive?.source || [sourceTable.id];
211-
let baseTables = tables.filter(t => baseTableIds.includes(t.id));
212188
if (baseTables.length == 0) {
213189
return;
214190
}
@@ -243,16 +219,16 @@ export const EncodingShelfThread: FC<EncodingShelfThreadProps> = function ({ cha
243219
let engine = getUrls().SERVER_DERIVE_DATA_URL;
244220

245221
console.log("current log")
246-
console.log(sourceTable.derive?.dialog)
222+
console.log(triggerTable.derive?.dialog)
247223

248224

249-
if (mode == "formulate" && sourceTable.derive?.dialog) {
225+
if (mode == "formulate" && triggerTable.derive?.dialog) {
250226
messageBody = JSON.stringify({
251227
token: token,
252228
mode,
253229
input_tables: baseTables.map(t => {return { name: t.id.replace(/\.[^/.]+$/ , ""), rows: t.rows }}),
254230
output_fields: activeBaseFields.map(f => { return {name: f.name } }),
255-
dialog: sourceTable.derive?.dialog,
231+
dialog: triggerTable.derive?.dialog,
256232
new_instruction: prompt,
257233
model: activeModel
258234
})

0 commit comments

Comments
 (0)