Skip to content

Commit 16c599f

Browse files
committed
fix model configuration reloading related stuff
1 parent ee195b1 commit 16c599f

File tree

3 files changed

+77
-14
lines changed

3 files changed

+77
-14
lines changed

src/app/dfSlice.tsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ export interface ModelConfig {
4545
}
4646

4747
// Define model slot types
48-
export type ModelSlotType = 'generation' | 'hint';
48+
export const MODEL_SLOT_TYPES = ['generation', 'hint'] as const;
49+
export type ModelSlotType = typeof MODEL_SLOT_TYPES[number];
4950

5051
export interface ModelSlots {
5152
generation?: string; // model id assigned to generation tasks
@@ -271,7 +272,7 @@ export const dataFormulatorSlice = createSlice({
271272
// avoid resetting inputted models
272273
// state.oaiModels = state.oaiModels.filter((m: any) => m.endpoint != 'default');
273274

274-
state.modelSlots = {};
275+
// state.modelSlots = {};
275276
state.testedModels = [];
276277

277278
state.tables = [];
@@ -763,8 +764,12 @@ export const dataFormulatorSlice = createSlice({
763764
...state.testedModels.filter(t => !defaultModels.map((m: ModelConfig) => m.id).includes(t.id))
764765
]
765766

766-
if (state.modelSlots.generation == undefined && defaultModels.length > 0) {
767-
state.modelSlots.generation = defaultModels[0].id;
767+
if (defaultModels.length > 0) {
768+
for (const slotType of MODEL_SLOT_TYPES) {
769+
if (state.modelSlots[slotType] == undefined) {
770+
state.modelSlots[slotType] = defaultModels[0].id;
771+
}
772+
}
768773
}
769774

770775
// console.log("load model complete");
@@ -796,7 +801,7 @@ export const dfSelectors = {
796801
return modelId ? state.models.find(m => m.id === modelId) : undefined;
797802
},
798803
getAllSlotTypes: () : ModelSlotType[] => {
799-
return ['generation', 'hint'];
804+
return [...MODEL_SLOT_TYPES];
800805
},
801806
getActiveBaseTableIds: (state: DataFormulatorState) => {
802807
let focusedTableId = state.focusedTableId;

src/views/DataFormulator.tsx

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { useDispatch, useSelector } from "react-redux"; /* code change */
88
import {
99
DataFormulatorState,
1010
dfActions,
11+
dfSelectors,
1112
} from '../app/dfSlice'
1213

1314
import _ from 'lodash';
@@ -43,6 +44,7 @@ import exampleImageTable from "../assets/example-image-table.png";
4344
import { ModelSelectionButton } from './ModelSelectionDialog';
4445
import { DBTableSelectionDialog } from './DBTableManager';
4546
import { connectToSSE } from './SSEClient';
47+
import { getUrls } from '../app/utils';
4648

4749
//type AppProps = ConnectedProps<typeof connector>;
4850

@@ -51,14 +53,62 @@ export const DataFormulatorFC = ({ }) => {
5153
const displayPanelSize = useSelector((state: DataFormulatorState) => state.displayPanelSize);
5254
const visPaneSize = useSelector((state: DataFormulatorState) => state.visPaneSize);
5355
const tables = useSelector((state: DataFormulatorState) => state.tables);
54-
const selectedModelId = useSelector((state: DataFormulatorState) => state.selectedModelId);
56+
57+
const models = useSelector((state: DataFormulatorState) => state.models);
58+
const modelSlots = useSelector((state: DataFormulatorState) => state.modelSlots);
59+
const testedModels = useSelector((state: DataFormulatorState) => state.testedModels);
60+
61+
const noBrokenModelSlots= useSelector((state: DataFormulatorState) => {
62+
const slotTypes = dfSelectors.getAllSlotTypes();
63+
return slotTypes.every(
64+
slotType => state.modelSlots[slotType] !== undefined && state.testedModels.find(t => t.id == state.modelSlots[slotType])?.status != 'error');
65+
});
5566

5667
const dispatch = useDispatch();
5768

5869
useEffect(() => {
5970
document.title = toolName;
6071
}, []);
6172

73+
useEffect(() => {
74+
const findWorkingModel = async () => {
75+
let assignedModels = models.filter(m => Object.values(modelSlots).includes(m.id));
76+
let unassignedModels = models.filter(m => !Object.values(modelSlots).includes(m.id));
77+
78+
// Combine both arrays: assigned models first, then unassigned models
79+
let allModelsToTest = [...assignedModels, ...unassignedModels];
80+
81+
for (let i = 0; i < allModelsToTest.length; i++) {
82+
let model = allModelsToTest[i];
83+
let isAssignedModel = i < assignedModels.length;
84+
85+
const message = {
86+
method: 'POST',
87+
headers: { 'Content-Type': 'application/json', },
88+
body: JSON.stringify({
89+
model: model,
90+
}),
91+
};
92+
try {
93+
const response = await fetch(getUrls().TEST_MODEL, {...message });
94+
const data = await response.json();
95+
const status = data["status"] || 'error';
96+
dispatch(dfActions.updateModelStatus({id: model.id, status, message: data["message"] || ""}));
97+
// For unassigned models, break when we find a working one
98+
if (!isAssignedModel && status == 'ok') {
99+
break;
100+
}
101+
} catch (error) {
102+
dispatch(dfActions.updateModelStatus({id: model.id, status: 'error', message: (error as Error).message || 'Failed to test model'}));
103+
}
104+
}
105+
};
106+
107+
if (models.length > 0) {
108+
findWorkingModel();
109+
}
110+
}, []);
111+
62112
let conceptEncodingPanel = (
63113
<Box sx={{display: "flex", flexDirection: "row", width: '100%', flexGrow: 1, overflow: "hidden"}}>
64114
<ConceptShelf />
@@ -173,7 +223,7 @@ Totals (7 entries) 5 5 5 15
173223
return (
174224
<Box sx={{ display: 'block', width: "100%", height: 'calc(100% - 49px)' }}>
175225
<DndProvider backend={HTML5Backend}>
176-
{selectedModelId == undefined ? modelSelectionDialogBox : (tables.length > 0 ? fixedSplitPane : dataUploadRequestBox)}
226+
{!noBrokenModelSlots ? modelSelectionDialogBox : (tables.length > 0 ? fixedSplitPane : dataUploadRequestBox)}
177227
</DndProvider>
178228
</Box>);
179229
}

src/views/ModelSelectionDialog.tsx

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,17 @@ export const ModelSelectionButton: React.FC<{}> = ({ }) => {
104104

105105
useEffect(() => {
106106
const findWorkingModel = async () => {
107-
for (let i = 0; i < models.length; i++) {
108-
if (testedModels.find(t => t.id == models[i].id)) {
109-
continue;
110-
}
111-
const model = models[i];
107+
let assignedModels = models.filter(m => Object.values(modelSlots).includes(m.id));
108+
let unassignedModels = models.filter(m => !Object.values(modelSlots).includes(m.id));
109+
110+
// Combine both arrays: assigned models first, then unassigned models
111+
let allModelsToTest = [...assignedModels, ...unassignedModels];
112+
113+
114+
for (let i = 0; i < allModelsToTest.length; i++) {
115+
let model = allModelsToTest[i];
116+
let isAssignedModel = i < assignedModels.length;
117+
112118
const message = {
113119
method: 'POST',
114120
headers: { 'Content-Type': 'application/json', },
@@ -121,7 +127,9 @@ export const ModelSelectionButton: React.FC<{}> = ({ }) => {
121127
const data = await response.json();
122128
const status = data["status"] || 'error';
123129
updateModelStatus(model, status, data["message"] || "");
124-
if (status === 'ok') {
130+
131+
// For unassigned models, break when we find a working one
132+
if (!isAssignedModel && status == 'ok') {
125133
break;
126134
}
127135
} catch (error) {
@@ -130,7 +138,7 @@ export const ModelSelectionButton: React.FC<{}> = ({ }) => {
130138
}
131139
};
132140

133-
if (models.length > 0 && testedModels.filter(t => t.status == 'ok').length == 0) {
141+
if (models.length > 0) {
134142
findWorkingModel();
135143
}
136144
}, []);

0 commit comments

Comments
 (0)