@timefoldai/viz-platform 0.0.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +69 -1
- package/dist/analytics/use-analytics-bridge.d.mts +6 -0
- package/dist/analytics/use-analytics-bridge.mjs +21 -0
- package/dist/assignability/assignability.d.mts +97 -0
- package/dist/assignability/assignability.mjs +51 -0
- package/dist/assignability/use-assignability.d.mts +32 -0
- package/dist/assignability/use-assignability.mjs +151 -0
- package/dist/config/credential-channel.d.mts +13 -0
- package/dist/config/credential-channel.mjs +33 -0
- package/dist/config/embedding-params.d.mts +30 -0
- package/dist/config/embedding-params.mjs +54 -0
- package/dist/config/security.d.mts +36 -0
- package/dist/config/security.mjs +64 -0
- package/dist/dataset-context/DataSetContext.d.mts +215 -0
- package/dist/dataset-context/DataSetContext.mjs +170 -0
- package/dist/dataset-context/DataSetContextProvider.d.mts +19 -0
- package/dist/dataset-context/DataSetContextProvider.mjs +627 -0
- package/dist/dataset-context/DataSetLoader.d.mts +68 -0
- package/dist/dataset-context/DataSetLoader.mjs +130 -0
- package/dist/dataset-context/data-set-key.d.mts +2 -0
- package/dist/dataset-context/data-set-key.mjs +14 -0
- package/dist/dataset-context/model-api-url.d.mts +13 -0
- package/dist/dataset-context/model-api-url.mjs +27 -0
- package/dist/dataset-context/use-data-set-load-state.d.mts +26 -0
- package/dist/dataset-context/use-data-set-load-state.mjs +29 -0
- package/dist/dataset-context/use-fetch-dataset-resource.d.mts +8 -0
- package/dist/dataset-context/use-fetch-dataset-resource.mjs +20 -0
- package/dist/dataset-context/use-fetch-waypoints.d.mts +8 -0
- package/dist/dataset-context/use-fetch-waypoints.mjs +20 -0
- package/dist/dataset-context/use-run-events.d.mts +34 -0
- package/dist/dataset-context/use-run-events.mjs +138 -0
- package/dist/demo-data/use-demo-data.d.mts +42 -0
- package/dist/demo-data/use-demo-data.mjs +187 -0
- package/dist/env.d.mts +2 -0
- package/dist/env.mjs +4 -0
- package/dist/feature-flags/feature-flags.d.mts +34 -0
- package/dist/feature-flags/feature-flags.mjs +51 -0
- package/dist/feature-flags/use-feature-flag.d.mts +11 -0
- package/dist/feature-flags/use-feature-flag.mjs +17 -0
- package/dist/http/describe-response.d.mts +9 -0
- package/dist/http/describe-response.mjs +13 -0
- package/dist/http/models-api-request.d.mts +3 -0
- package/dist/http/models-api-request.mjs +20 -0
- package/dist/iframe-bridge/iframe-bridge.d.mts +10 -0
- package/dist/iframe-bridge/iframe-bridge.mjs +24 -0
- package/dist/iframe-bridge/use-iframe-bridge.d.mts +32 -0
- package/dist/iframe-bridge/use-iframe-bridge.mjs +200 -0
- package/dist/index.d.mts +16 -0
- package/dist/index.mjs +14 -0
- package/dist/solve-control/use-solve-control.d.mts +16 -0
- package/dist/solve-control/use-solve-control.mjs +46 -0
- package/dist/theme/prefers-dark.d.mts +11 -0
- package/dist/theme/prefers-dark.mjs +43 -0
- package/dist/view-channel/view-channel.d.mts +17 -0
- package/dist/view-channel/view-channel.mjs +67 -0
- package/dist/visualization-provider/VisualizationProvider.d.mts +20 -0
- package/dist/visualization-provider/VisualizationProvider.mjs +26 -0
- package/package.json +36 -5
|
@@ -0,0 +1,627 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useCredentialChannel } from "../config/credential-channel.mjs";
|
|
3
|
+
import { parseDistanceUnitSystem, resolveEmbeddingParams } from "../config/embedding-params.mjs";
|
|
4
|
+
import { resolveAllowedApiHosts, resolveApiHost, resolveApiVersion } from "../config/security.mjs";
|
|
5
|
+
import { isDevBuild } from "../env.mjs";
|
|
6
|
+
import { KNOWN_FLAG_PARAMS, parseBooleanParam } from "../feature-flags/feature-flags.mjs";
|
|
7
|
+
import { useIframeBridge } from "../iframe-bridge/use-iframe-bridge.mjs";
|
|
8
|
+
import { applyHostColorScheme } from "../theme/prefers-dark.mjs";
|
|
9
|
+
import { DataSetControlsContext, DataSetDispatchContext, DataSetStateContext, DistanceUnitSystemContext, HostViewportHeightContext, createDataSetWriters, rejectInvalidInput, resolveSolveStatus } from "./DataSetContext.mjs";
|
|
10
|
+
import { fetchInputData, fetchOutputData, readDataSetMetadata, requestSolve, stopSolvingDataset } from "./DataSetLoader.mjs";
|
|
11
|
+
import { dataSetUrl, resolveRunId } from "./model-api-url.mjs";
|
|
12
|
+
import { SOLVE_POLL_INTERVAL_MS, useRunEvents } from "./use-run-events.mjs";
|
|
13
|
+
import { parseColorScheme } from "@timefoldai/viz-core";
|
|
14
|
+
import { startTransition, useCallback, useEffect, useMemo, useReducer, useRef, useState } from "react";
|
|
15
|
+
import { jsx } from "react/jsx-runtime";
|
|
16
|
+
// src/dataset-context/DataSetContextProvider.tsx
|
|
17
|
+
function mergedView(config, inputData, outputData) {
|
|
18
|
+
return config.mergeData ? config.mergeData(inputData, outputData) : inputData;
|
|
19
|
+
}
|
|
20
|
+
function readValidationErrors(validationResult) {
|
|
21
|
+
return validationResult?.errors ?? void 0;
|
|
22
|
+
}
|
|
23
|
+
const NO_LOADED_RUN = {
|
|
24
|
+
inputData: void 0,
|
|
25
|
+
outputData: void 0,
|
|
26
|
+
kpis: void 0,
|
|
27
|
+
mergedData: void 0,
|
|
28
|
+
score: "",
|
|
29
|
+
status: { phase: "idle" },
|
|
30
|
+
activeDataSet: null
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* The single owner of dataset load and solve state; exported for direct unit
|
|
34
|
+
* testing without mounting the provider.
|
|
35
|
+
*/
|
|
36
|
+
function dataSetReducer(state, action) {
|
|
37
|
+
switch (action.type) {
|
|
38
|
+
case "SET_INPUT": return {
|
|
39
|
+
...state,
|
|
40
|
+
dataSetId: action.source === "local" ? void 0 : state.dataSetId,
|
|
41
|
+
dataSetName: action.source === "local" ? void 0 : state.dataSetName,
|
|
42
|
+
inputData: action.inputData,
|
|
43
|
+
outputData: void 0,
|
|
44
|
+
kpis: void 0,
|
|
45
|
+
mergedData: mergedView(state.config, action.inputData, void 0),
|
|
46
|
+
score: "",
|
|
47
|
+
status: { phase: "idle" }
|
|
48
|
+
};
|
|
49
|
+
case "REJECT_INPUT": return {
|
|
50
|
+
...state,
|
|
51
|
+
inputData: void 0,
|
|
52
|
+
outputData: void 0,
|
|
53
|
+
kpis: void 0,
|
|
54
|
+
mergedData: void 0,
|
|
55
|
+
score: "",
|
|
56
|
+
status: {
|
|
57
|
+
phase: "error",
|
|
58
|
+
error: action.error
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
case "SET_OUTPUT": {
|
|
62
|
+
const { config, inputData } = state;
|
|
63
|
+
const mergedData = inputData === void 0 ? void 0 : mergedView(config, inputData, action.outputData);
|
|
64
|
+
return {
|
|
65
|
+
...state,
|
|
66
|
+
outputData: action.outputData,
|
|
67
|
+
kpis: action.kpis,
|
|
68
|
+
mergedData
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
case "SET_SCORE": return {
|
|
72
|
+
...state,
|
|
73
|
+
score: action.score
|
|
74
|
+
};
|
|
75
|
+
case "SET_STATUS": return {
|
|
76
|
+
...state,
|
|
77
|
+
status: action.status
|
|
78
|
+
};
|
|
79
|
+
case "START_SOLVE": return {
|
|
80
|
+
...state,
|
|
81
|
+
outputData: void 0,
|
|
82
|
+
kpis: void 0,
|
|
83
|
+
mergedData: state.inputData === void 0 ? void 0 : mergedView(state.config, state.inputData, void 0),
|
|
84
|
+
score: "",
|
|
85
|
+
status: {
|
|
86
|
+
phase: "solving",
|
|
87
|
+
solveId: "",
|
|
88
|
+
solverStatus: "SOLVING_SCHEDULED"
|
|
89
|
+
},
|
|
90
|
+
inAppSolveActive: true
|
|
91
|
+
};
|
|
92
|
+
case "SET_ACTIVE_DATASET": return {
|
|
93
|
+
...state,
|
|
94
|
+
activeDataSet: action.activeDataSet
|
|
95
|
+
};
|
|
96
|
+
case "SET_IN_APP_SOLVE_ACTIVE": return {
|
|
97
|
+
...state,
|
|
98
|
+
inAppSolveActive: action.active
|
|
99
|
+
};
|
|
100
|
+
case "SET_DATASET_ID": return {
|
|
101
|
+
...state,
|
|
102
|
+
dataSetId: action.dataSetId,
|
|
103
|
+
dataSetName: void 0
|
|
104
|
+
};
|
|
105
|
+
case "SET_DATASET_NAME": return {
|
|
106
|
+
...state,
|
|
107
|
+
dataSetName: action.name
|
|
108
|
+
};
|
|
109
|
+
case "SET_API_KEY": return {
|
|
110
|
+
...state,
|
|
111
|
+
apiKey: action.apiKey
|
|
112
|
+
};
|
|
113
|
+
case "SET_EMBEDDING_PARAMS": return {
|
|
114
|
+
...state,
|
|
115
|
+
...action.dataSetId === void 0 ? NO_LOADED_RUN : {},
|
|
116
|
+
isOnPlatform: true,
|
|
117
|
+
tenantId: action.tenantId,
|
|
118
|
+
dataSetId: action.dataSetId,
|
|
119
|
+
dataSetName: action.dataSetId !== void 0 && action.dataSetId === state.dataSetId ? state.dataSetName : void 0,
|
|
120
|
+
apiKey: action.apiKey,
|
|
121
|
+
config: state.config.host === action.host && state.config.apiVersion === action.apiVersion ? state.config : {
|
|
122
|
+
...state.config,
|
|
123
|
+
host: action.host,
|
|
124
|
+
apiVersion: action.apiVersion
|
|
125
|
+
},
|
|
126
|
+
embeddingError: action.embeddingError,
|
|
127
|
+
featureFlags: action.featureFlags
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
function joinErrors(...errors) {
|
|
132
|
+
const present = errors.filter((error) => error !== void 0);
|
|
133
|
+
return present.length > 0 ? present.join(" ") : void 0;
|
|
134
|
+
}
|
|
135
|
+
function resolveConnection({ configHost, configVersion, apiUrl, apiVersion, runId, allowlist }) {
|
|
136
|
+
const host = resolveApiHost({
|
|
137
|
+
configHost,
|
|
138
|
+
apiUrlParam: apiUrl,
|
|
139
|
+
currentOrigin: new URL(window.location.href).origin,
|
|
140
|
+
allowlist
|
|
141
|
+
});
|
|
142
|
+
const version = resolveApiVersion({
|
|
143
|
+
configVersion,
|
|
144
|
+
hostVersion: apiVersion
|
|
145
|
+
});
|
|
146
|
+
const run = resolveRunId(runId);
|
|
147
|
+
return {
|
|
148
|
+
host: host.host,
|
|
149
|
+
apiVersion: version.apiVersion,
|
|
150
|
+
runId: run.runId,
|
|
151
|
+
embeddingError: joinErrors(host.embeddingError, version.embeddingError, run.embeddingError)
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Provides the active dataset to descendant visualisation components.
|
|
156
|
+
*
|
|
157
|
+
* @category Providers
|
|
158
|
+
*/
|
|
159
|
+
function DataSetContextProvider({ config, initialState, unitSystem, children }) {
|
|
160
|
+
const [allowlist] = useState(() => resolveAllowedApiHosts(config.allowedApiHosts));
|
|
161
|
+
const [embeddingParams] = useState(() => resolveEmbeddingParams({
|
|
162
|
+
queryParams: new URLSearchParams(window.location.search),
|
|
163
|
+
devDefaults: config.devDefaults ?? {},
|
|
164
|
+
isDevBuild,
|
|
165
|
+
hostname: window.location.hostname
|
|
166
|
+
}));
|
|
167
|
+
const [state, dispatch] = useReducer(dataSetReducer, void 0, () => {
|
|
168
|
+
const connection = resolveConnection({
|
|
169
|
+
configHost: config.host,
|
|
170
|
+
configVersion: config.apiVersion,
|
|
171
|
+
apiUrl: embeddingParams.apiUrl,
|
|
172
|
+
apiVersion: embeddingParams.apiVersion,
|
|
173
|
+
runId: embeddingParams.runId,
|
|
174
|
+
allowlist
|
|
175
|
+
});
|
|
176
|
+
const seeded = {
|
|
177
|
+
config: {
|
|
178
|
+
...config,
|
|
179
|
+
host: connection.host,
|
|
180
|
+
apiVersion: connection.apiVersion
|
|
181
|
+
},
|
|
182
|
+
allowedApiHosts: allowlist,
|
|
183
|
+
embeddingError: connection.embeddingError,
|
|
184
|
+
isOnPlatform: embeddingParams.onPlatform,
|
|
185
|
+
isDevelopment: embeddingParams.isDevelopment,
|
|
186
|
+
apiKey: embeddingParams.apiKey,
|
|
187
|
+
dataSetId: connection.runId,
|
|
188
|
+
tenantId: embeddingParams.tenantId,
|
|
189
|
+
featureFlags: embeddingParams.featureFlags,
|
|
190
|
+
platformChrome: embeddingParams.platformChrome,
|
|
191
|
+
inputData: initialState?.inputData,
|
|
192
|
+
outputData: initialState?.outputData,
|
|
193
|
+
kpis: initialState?.kpis,
|
|
194
|
+
mergedData: initialState?.mergedData,
|
|
195
|
+
score: initialState?.score ?? "",
|
|
196
|
+
status: initialState?.status ?? { phase: "idle" },
|
|
197
|
+
activeDataSet: null,
|
|
198
|
+
inAppSolveActive: false
|
|
199
|
+
};
|
|
200
|
+
const input = initialState?.inputData;
|
|
201
|
+
const rejection = input === void 0 ? null : rejectInvalidInput(config, input);
|
|
202
|
+
if (rejection) return dataSetReducer(seeded, rejection);
|
|
203
|
+
return input === void 0 || seeded.mergedData !== void 0 ? seeded : {
|
|
204
|
+
...seeded,
|
|
205
|
+
mergedData: mergedView(config, input, seeded.outputData)
|
|
206
|
+
};
|
|
207
|
+
});
|
|
208
|
+
useEffect(() => {
|
|
209
|
+
const params = new URLSearchParams(window.location.search);
|
|
210
|
+
if (!params.has("apiKey")) return;
|
|
211
|
+
params.delete("apiKey");
|
|
212
|
+
const query = params.toString();
|
|
213
|
+
const newUrl = `${window.location.pathname}${query ? `?${query}` : ""}${window.location.hash}`;
|
|
214
|
+
window.history.replaceState(window.history.state, "", newUrl);
|
|
215
|
+
}, []);
|
|
216
|
+
const [hostUnitSystem, setHostUnitSystem] = useState(embeddingParams.unitSystem);
|
|
217
|
+
const onCredential = useCallback((apiKey) => {
|
|
218
|
+
dispatch({
|
|
219
|
+
type: "SET_API_KEY",
|
|
220
|
+
apiKey
|
|
221
|
+
});
|
|
222
|
+
}, []);
|
|
223
|
+
useCredentialChannel({
|
|
224
|
+
allowlist,
|
|
225
|
+
onCredential
|
|
226
|
+
});
|
|
227
|
+
const adoptedDataSetIdRef = useRef(initialState?.inputData === void 0 ? null : state.dataSetId ?? null);
|
|
228
|
+
const onInit = useCallback((data) => {
|
|
229
|
+
const connection = resolveConnection({
|
|
230
|
+
configHost: config.host,
|
|
231
|
+
configVersion: config.apiVersion,
|
|
232
|
+
apiUrl: data.apiUrl,
|
|
233
|
+
apiVersion: data.apiVersion ?? embeddingParams.apiVersion,
|
|
234
|
+
runId: data.runId,
|
|
235
|
+
allowlist
|
|
236
|
+
});
|
|
237
|
+
const featureFlags = new Set(state.featureFlags);
|
|
238
|
+
for (const name of KNOWN_FLAG_PARAMS) {
|
|
239
|
+
if (!(name in data)) continue;
|
|
240
|
+
if (parseBooleanParam(data[name] ?? null)) featureFlags.add(name);
|
|
241
|
+
else featureFlags.delete(name);
|
|
242
|
+
}
|
|
243
|
+
dispatch({
|
|
244
|
+
type: "SET_EMBEDDING_PARAMS",
|
|
245
|
+
tenantId: data.tenantId,
|
|
246
|
+
dataSetId: connection.runId,
|
|
247
|
+
apiKey: data.apiKey,
|
|
248
|
+
featureFlags,
|
|
249
|
+
host: connection.host,
|
|
250
|
+
apiVersion: connection.apiVersion,
|
|
251
|
+
embeddingError: connection.embeddingError
|
|
252
|
+
});
|
|
253
|
+
setHostUnitSystem(parseDistanceUnitSystem(data.unitSystem));
|
|
254
|
+
const colorScheme = parseColorScheme(data.colorScheme);
|
|
255
|
+
if (colorScheme !== void 0) applyHostColorScheme(colorScheme);
|
|
256
|
+
}, [
|
|
257
|
+
config.host,
|
|
258
|
+
allowlist,
|
|
259
|
+
config.apiVersion,
|
|
260
|
+
embeddingParams.apiVersion,
|
|
261
|
+
state.featureFlags
|
|
262
|
+
]);
|
|
263
|
+
const [hostViewportHeight, setHostViewportHeight] = useState();
|
|
264
|
+
useIframeBridge({
|
|
265
|
+
allowlist,
|
|
266
|
+
onInit,
|
|
267
|
+
onUnitSystem: setHostUnitSystem,
|
|
268
|
+
onColorScheme: applyHostColorScheme,
|
|
269
|
+
onViewport: setHostViewportHeight
|
|
270
|
+
});
|
|
271
|
+
const inAppSolveActiveRef = useRef(state.inAppSolveActive);
|
|
272
|
+
useEffect(() => {
|
|
273
|
+
inAppSolveActiveRef.current = state.inAppSolveActive;
|
|
274
|
+
}, [state.inAppSolveActive]);
|
|
275
|
+
const fetchRunInput = useCallback(async (signal) => {
|
|
276
|
+
if (!state.dataSetId) return void 0;
|
|
277
|
+
try {
|
|
278
|
+
return await fetchInputData(state.config, state.dataSetId, state.apiKey, signal);
|
|
279
|
+
} catch (error) {
|
|
280
|
+
if (error.name !== "AbortError") dispatch({
|
|
281
|
+
type: "SET_STATUS",
|
|
282
|
+
status: {
|
|
283
|
+
phase: "error",
|
|
284
|
+
error: error.message
|
|
285
|
+
}
|
|
286
|
+
});
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
}, [
|
|
290
|
+
state.dataSetId,
|
|
291
|
+
state.apiKey,
|
|
292
|
+
state.config
|
|
293
|
+
]);
|
|
294
|
+
const lastOutputSigRef = useRef(null);
|
|
295
|
+
const applyOutputEnvelope = useCallback((dataset) => {
|
|
296
|
+
if (inAppSolveActiveRef.current) return {
|
|
297
|
+
ok: true,
|
|
298
|
+
solving: false
|
|
299
|
+
};
|
|
300
|
+
const metadata = readDataSetMetadata(dataset);
|
|
301
|
+
const score = metadata?.score ?? "";
|
|
302
|
+
const solverStatus = metadata?.solverStatus;
|
|
303
|
+
const status = resolveSolveStatus(solverStatus, state.dataSetId ?? "", readValidationErrors(metadata?.validationResult));
|
|
304
|
+
const output = dataset.modelOutput;
|
|
305
|
+
const sig = `${score}|${solverStatus ?? ""}|${output == null ? "0" : "1"}`;
|
|
306
|
+
if (sig !== lastOutputSigRef.current) {
|
|
307
|
+
lastOutputSigRef.current = sig;
|
|
308
|
+
startTransition(() => {
|
|
309
|
+
if (output != null) dispatch({
|
|
310
|
+
type: "SET_OUTPUT",
|
|
311
|
+
outputData: output,
|
|
312
|
+
kpis: dataset.kpis
|
|
313
|
+
});
|
|
314
|
+
dispatch({
|
|
315
|
+
type: "SET_SCORE",
|
|
316
|
+
score
|
|
317
|
+
});
|
|
318
|
+
dispatch({
|
|
319
|
+
type: "SET_DATASET_NAME",
|
|
320
|
+
name: metadata?.name ?? ""
|
|
321
|
+
});
|
|
322
|
+
dispatch({
|
|
323
|
+
type: "SET_STATUS",
|
|
324
|
+
status
|
|
325
|
+
});
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
return {
|
|
329
|
+
ok: true,
|
|
330
|
+
solving: status.phase === "solving"
|
|
331
|
+
};
|
|
332
|
+
}, [state.dataSetId]);
|
|
333
|
+
const loadOutputData = useCallback(async (signal) => {
|
|
334
|
+
if (!state.dataSetId) return {
|
|
335
|
+
ok: true,
|
|
336
|
+
solving: false
|
|
337
|
+
};
|
|
338
|
+
try {
|
|
339
|
+
const dataset = await fetchOutputData(state.config, state.dataSetId, state.apiKey, signal);
|
|
340
|
+
return applyOutputEnvelope(dataset);
|
|
341
|
+
} catch (error) {
|
|
342
|
+
if (error.name === "AbortError") return {
|
|
343
|
+
ok: true,
|
|
344
|
+
solving: true
|
|
345
|
+
};
|
|
346
|
+
dispatch({
|
|
347
|
+
type: "SET_STATUS",
|
|
348
|
+
status: {
|
|
349
|
+
phase: "error",
|
|
350
|
+
error: error.message
|
|
351
|
+
}
|
|
352
|
+
});
|
|
353
|
+
dispatch({
|
|
354
|
+
type: "SET_DATASET_NAME",
|
|
355
|
+
name: ""
|
|
356
|
+
});
|
|
357
|
+
return {
|
|
358
|
+
ok: false,
|
|
359
|
+
solving: false
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
}, [
|
|
363
|
+
state.dataSetId,
|
|
364
|
+
state.apiKey,
|
|
365
|
+
state.config,
|
|
366
|
+
applyOutputEnvelope
|
|
367
|
+
]);
|
|
368
|
+
useEffect(() => {
|
|
369
|
+
const dataSetId = state.dataSetId;
|
|
370
|
+
if (dataSetId === adoptedDataSetIdRef.current) return;
|
|
371
|
+
adoptedDataSetIdRef.current = null;
|
|
372
|
+
if (!dataSetId) return;
|
|
373
|
+
const controller = new AbortController();
|
|
374
|
+
(async () => {
|
|
375
|
+
const outputResult = fetchOutputData(state.config, dataSetId, state.apiKey, controller.signal).then((envelope) => ({ envelope }), (error) => ({ error }));
|
|
376
|
+
const inputData = await fetchRunInput(controller.signal);
|
|
377
|
+
if (inputData === void 0 || controller.signal.aborted) return;
|
|
378
|
+
const applyOutputResult = (result) => {
|
|
379
|
+
if (controller.signal.aborted) return;
|
|
380
|
+
if ("envelope" in result) {
|
|
381
|
+
applyOutputEnvelope(result.envelope);
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
if (result.error.name === "AbortError") return;
|
|
385
|
+
dispatch({
|
|
386
|
+
type: "SET_STATUS",
|
|
387
|
+
status: {
|
|
388
|
+
phase: "error",
|
|
389
|
+
error: result.error.message
|
|
390
|
+
}
|
|
391
|
+
});
|
|
392
|
+
};
|
|
393
|
+
const settledOutput = await outputResult;
|
|
394
|
+
if (controller.signal.aborted) return;
|
|
395
|
+
const rejection = rejectInvalidInput(state.config, inputData);
|
|
396
|
+
if (rejection) {
|
|
397
|
+
dispatch(rejection);
|
|
398
|
+
return;
|
|
399
|
+
}
|
|
400
|
+
lastOutputSigRef.current = null;
|
|
401
|
+
startTransition(() => {
|
|
402
|
+
dispatch({
|
|
403
|
+
type: "SET_INPUT",
|
|
404
|
+
inputData
|
|
405
|
+
});
|
|
406
|
+
dispatch({
|
|
407
|
+
type: "SET_ACTIVE_DATASET",
|
|
408
|
+
activeDataSet: { modelInput: inputData }
|
|
409
|
+
});
|
|
410
|
+
applyOutputResult(settledOutput);
|
|
411
|
+
});
|
|
412
|
+
})();
|
|
413
|
+
return () => controller.abort();
|
|
414
|
+
}, [
|
|
415
|
+
state.dataSetId,
|
|
416
|
+
state.apiKey,
|
|
417
|
+
state.config,
|
|
418
|
+
fetchRunInput,
|
|
419
|
+
applyOutputEnvelope
|
|
420
|
+
]);
|
|
421
|
+
const solvePollTimeoutRef = useRef(null);
|
|
422
|
+
const pollRef = useRef(null);
|
|
423
|
+
const pollSolveOutput = useCallback(async (solveId, signal) => {
|
|
424
|
+
try {
|
|
425
|
+
const dataset = await fetchOutputData(state.config, solveId, state.apiKey, signal);
|
|
426
|
+
if (signal.aborted) return;
|
|
427
|
+
const metadata = readDataSetMetadata(dataset);
|
|
428
|
+
const score = metadata?.score ?? "";
|
|
429
|
+
const solverStatus = metadata?.solverStatus;
|
|
430
|
+
const nextStatus = resolveSolveStatus(solverStatus, solveId, readValidationErrors(metadata?.validationResult));
|
|
431
|
+
const output = dataset.modelOutput;
|
|
432
|
+
const sig = `${score}|${solverStatus ?? ""}|${output == null ? "0" : "1"}`;
|
|
433
|
+
if (sig !== lastOutputSigRef.current) {
|
|
434
|
+
lastOutputSigRef.current = sig;
|
|
435
|
+
startTransition(() => {
|
|
436
|
+
if (output != null) dispatch({
|
|
437
|
+
type: "SET_OUTPUT",
|
|
438
|
+
outputData: output,
|
|
439
|
+
kpis: dataset.kpis
|
|
440
|
+
});
|
|
441
|
+
dispatch({
|
|
442
|
+
type: "SET_SCORE",
|
|
443
|
+
score
|
|
444
|
+
});
|
|
445
|
+
dispatch({
|
|
446
|
+
type: "SET_DATASET_NAME",
|
|
447
|
+
name: metadata?.name ?? ""
|
|
448
|
+
});
|
|
449
|
+
dispatch({
|
|
450
|
+
type: "SET_STATUS",
|
|
451
|
+
status: nextStatus
|
|
452
|
+
});
|
|
453
|
+
});
|
|
454
|
+
}
|
|
455
|
+
if (nextStatus.phase === "solving") solvePollTimeoutRef.current = setTimeout(() => void pollRef.current?.(solveId, signal), SOLVE_POLL_INTERVAL_MS);
|
|
456
|
+
else startTransition(() => {
|
|
457
|
+
dispatch({
|
|
458
|
+
type: "SET_IN_APP_SOLVE_ACTIVE",
|
|
459
|
+
active: false
|
|
460
|
+
});
|
|
461
|
+
});
|
|
462
|
+
} catch (error) {
|
|
463
|
+
if (error.name === "AbortError") return;
|
|
464
|
+
dispatch({
|
|
465
|
+
type: "SET_STATUS",
|
|
466
|
+
status: {
|
|
467
|
+
phase: "error",
|
|
468
|
+
error: error.message
|
|
469
|
+
}
|
|
470
|
+
});
|
|
471
|
+
dispatch({
|
|
472
|
+
type: "SET_IN_APP_SOLVE_ACTIVE",
|
|
473
|
+
active: false
|
|
474
|
+
});
|
|
475
|
+
}
|
|
476
|
+
}, [state.config, state.apiKey]);
|
|
477
|
+
useEffect(() => {
|
|
478
|
+
pollRef.current = pollSolveOutput;
|
|
479
|
+
}, [pollSolveOutput]);
|
|
480
|
+
const solveAbortRef = useRef(null);
|
|
481
|
+
const solveRequestPendingRef = useRef(false);
|
|
482
|
+
const startSolve = useCallback(async () => {
|
|
483
|
+
if (solveRequestPendingRef.current) return;
|
|
484
|
+
solveAbortRef.current?.abort();
|
|
485
|
+
const controller = new AbortController();
|
|
486
|
+
solveAbortRef.current = controller;
|
|
487
|
+
solveRequestPendingRef.current = true;
|
|
488
|
+
lastOutputSigRef.current = null;
|
|
489
|
+
dispatch({ type: "START_SOLVE" });
|
|
490
|
+
let scheduleResponse;
|
|
491
|
+
try {
|
|
492
|
+
scheduleResponse = await requestSolve(state.config, state.dataSetId, state.activeDataSet, state.apiKey || "");
|
|
493
|
+
} catch (error) {
|
|
494
|
+
solveRequestPendingRef.current = false;
|
|
495
|
+
if (controller.signal.aborted) return;
|
|
496
|
+
dispatch({
|
|
497
|
+
type: "SET_STATUS",
|
|
498
|
+
status: {
|
|
499
|
+
phase: "error",
|
|
500
|
+
error: error.message
|
|
501
|
+
}
|
|
502
|
+
});
|
|
503
|
+
dispatch({
|
|
504
|
+
type: "SET_IN_APP_SOLVE_ACTIVE",
|
|
505
|
+
active: false
|
|
506
|
+
});
|
|
507
|
+
return;
|
|
508
|
+
}
|
|
509
|
+
solveRequestPendingRef.current = false;
|
|
510
|
+
const solveId = scheduleResponse.id ?? "";
|
|
511
|
+
if (controller.signal.aborted) {
|
|
512
|
+
if (solveId) stopSolvingDataset(state.config, solveId, state.apiKey || "");
|
|
513
|
+
return;
|
|
514
|
+
}
|
|
515
|
+
if (!solveId) {
|
|
516
|
+
dispatch({
|
|
517
|
+
type: "SET_STATUS",
|
|
518
|
+
status: {
|
|
519
|
+
phase: "error",
|
|
520
|
+
error: "The solve request returned no run id."
|
|
521
|
+
}
|
|
522
|
+
});
|
|
523
|
+
dispatch({
|
|
524
|
+
type: "SET_IN_APP_SOLVE_ACTIVE",
|
|
525
|
+
active: false
|
|
526
|
+
});
|
|
527
|
+
return;
|
|
528
|
+
}
|
|
529
|
+
adoptedDataSetIdRef.current = solveId;
|
|
530
|
+
dispatch({
|
|
531
|
+
type: "SET_DATASET_ID",
|
|
532
|
+
dataSetId: solveId
|
|
533
|
+
});
|
|
534
|
+
dispatch({
|
|
535
|
+
type: "SET_STATUS",
|
|
536
|
+
status: resolveSolveStatus(scheduleResponse.solverStatus, solveId, readValidationErrors(scheduleResponse.validationResult))
|
|
537
|
+
});
|
|
538
|
+
pollSolveOutput(solveId, controller.signal);
|
|
539
|
+
}, [
|
|
540
|
+
state.config,
|
|
541
|
+
state.dataSetId,
|
|
542
|
+
state.activeDataSet,
|
|
543
|
+
state.apiKey,
|
|
544
|
+
pollSolveOutput
|
|
545
|
+
]);
|
|
546
|
+
const statusRef = useRef(state.status);
|
|
547
|
+
useEffect(() => {
|
|
548
|
+
statusRef.current = state.status;
|
|
549
|
+
}, [state.status]);
|
|
550
|
+
const runUpdateAbortRef = useRef(null);
|
|
551
|
+
const startRunUpdate = useCallback(() => {
|
|
552
|
+
runUpdateAbortRef.current?.abort();
|
|
553
|
+
const controller = new AbortController();
|
|
554
|
+
runUpdateAbortRef.current = controller;
|
|
555
|
+
return controller.signal;
|
|
556
|
+
}, []);
|
|
557
|
+
const handleRunUpdate = useCallback(() => loadOutputData(startRunUpdate()), [loadOutputData, startRunUpdate]);
|
|
558
|
+
const stopSolve = useCallback(() => {
|
|
559
|
+
solveAbortRef.current?.abort();
|
|
560
|
+
if (solvePollTimeoutRef.current) {
|
|
561
|
+
clearTimeout(solvePollTimeoutRef.current);
|
|
562
|
+
solvePollTimeoutRef.current = null;
|
|
563
|
+
}
|
|
564
|
+
const status = statusRef.current;
|
|
565
|
+
if (status.phase === "solving" && status.solveId) {
|
|
566
|
+
const signal = startRunUpdate();
|
|
567
|
+
const loadFinalSolution = () => signal.aborted ? void 0 : loadOutputData(signal);
|
|
568
|
+
stopSolvingDataset(state.config, status.solveId, state.apiKey || "").then(loadFinalSolution, loadFinalSolution);
|
|
569
|
+
}
|
|
570
|
+
dispatch({
|
|
571
|
+
type: "SET_STATUS",
|
|
572
|
+
status: { phase: "idle" }
|
|
573
|
+
});
|
|
574
|
+
dispatch({
|
|
575
|
+
type: "SET_IN_APP_SOLVE_ACTIVE",
|
|
576
|
+
active: false
|
|
577
|
+
});
|
|
578
|
+
}, [
|
|
579
|
+
state.config,
|
|
580
|
+
state.apiKey,
|
|
581
|
+
startRunUpdate,
|
|
582
|
+
loadOutputData
|
|
583
|
+
]);
|
|
584
|
+
useEffect(() => {
|
|
585
|
+
return () => {
|
|
586
|
+
if (solvePollTimeoutRef.current) clearTimeout(solvePollTimeoutRef.current);
|
|
587
|
+
};
|
|
588
|
+
}, []);
|
|
589
|
+
useRunEvents({
|
|
590
|
+
dataSetUrl: dataSetUrl(state.config),
|
|
591
|
+
runId: state.dataSetId,
|
|
592
|
+
apiKey: state.apiKey,
|
|
593
|
+
isActivelySolving: state.inAppSolveActive,
|
|
594
|
+
runSolving: state.status.phase === "solving",
|
|
595
|
+
onOutput: handleRunUpdate
|
|
596
|
+
});
|
|
597
|
+
useEffect(() => () => runUpdateAbortRef.current?.abort(), [state.dataSetId]);
|
|
598
|
+
useEffect(() => {
|
|
599
|
+
if (state.inAppSolveActive) runUpdateAbortRef.current?.abort();
|
|
600
|
+
}, [state.inAppSolveActive]);
|
|
601
|
+
const controls = useMemo(() => ({
|
|
602
|
+
...createDataSetWriters(state.config, dispatch),
|
|
603
|
+
startSolve,
|
|
604
|
+
stopSolve
|
|
605
|
+
}), [
|
|
606
|
+
state.config,
|
|
607
|
+
startSolve,
|
|
608
|
+
stopSolve
|
|
609
|
+
]);
|
|
610
|
+
return /* @__PURE__ */ jsx(DataSetStateContext.Provider, {
|
|
611
|
+
value: state,
|
|
612
|
+
children: /* @__PURE__ */ jsx(DataSetDispatchContext.Provider, {
|
|
613
|
+
value: dispatch,
|
|
614
|
+
children: /* @__PURE__ */ jsx(DataSetControlsContext.Provider, {
|
|
615
|
+
value: controls,
|
|
616
|
+
children: /* @__PURE__ */ jsx(DistanceUnitSystemContext.Provider, {
|
|
617
|
+
value: unitSystem ?? hostUnitSystem,
|
|
618
|
+
children: /* @__PURE__ */ jsx(HostViewportHeightContext.Provider, {
|
|
619
|
+
value: hostViewportHeight,
|
|
620
|
+
children
|
|
621
|
+
})
|
|
622
|
+
})
|
|
623
|
+
})
|
|
624
|
+
})
|
|
625
|
+
});
|
|
626
|
+
}
|
|
627
|
+
export { DataSetContextProvider, dataSetReducer };
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { ActiveDataSet, SolvingStatus, VisualizationConfig } from "./DataSetContext.mjs";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
/** A dataset's validation result, in the Models API's `LegacyValidationResult` shape. */
|
|
4
|
+
export type ValidationResult = {
|
|
5
|
+
errors?: string[] | null;
|
|
6
|
+
};
|
|
7
|
+
/** A dataset's metadata block, as served alongside its solution. */
|
|
8
|
+
export type DataSetMetadata = {
|
|
9
|
+
name?: string | null;
|
|
10
|
+
score?: string | null;
|
|
11
|
+
solverStatus?: SolvingStatus | null;
|
|
12
|
+
validationResult?: ValidationResult | null;
|
|
13
|
+
};
|
|
14
|
+
export type OutputEnvelope<OutputType, KpisType = unknown> = {
|
|
15
|
+
metadata?: DataSetMetadata | null;
|
|
16
|
+
/** @deprecated The Models API's alias of {@link OutputEnvelope.metadata}. Read
|
|
17
|
+
* both through {@link readDataSetMetadata}. */
|
|
18
|
+
run?: DataSetMetadata | null;
|
|
19
|
+
modelOutput?: OutputType;
|
|
20
|
+
kpis?: KpisType | null;
|
|
21
|
+
};
|
|
22
|
+
/** The dataset's metadata, from whichever of the two aliases the API served. */
|
|
23
|
+
export declare function readDataSetMetadata<OutputType, KpisType>(envelope: OutputEnvelope<OutputType, KpisType>): DataSetMetadata | undefined;
|
|
24
|
+
export declare function fetchOutputData<InputType, OutputType, MergedType>(config: VisualizationConfig<InputType, OutputType, MergedType>, dataSetId: string, apiKey?: string, signal?: AbortSignal): Promise<OutputEnvelope<OutputType>>;
|
|
25
|
+
export declare function fetchInputData<InputType, OutputType, MergedType>(config: VisualizationConfig<InputType, OutputType, MergedType>, dataSetId: string, apiKey?: string, signal?: AbortSignal): Promise<InputType>;
|
|
26
|
+
/**
|
|
27
|
+
* Authenticated GET of a dataset sub-resource, reusing the same
|
|
28
|
+
* origin-allowlisted host + API key as {@link fetchOutputData}.
|
|
29
|
+
*/
|
|
30
|
+
export declare function fetchDataSetResource<T, InputType, OutputType, MergedType>(config: VisualizationConfig<InputType, OutputType, MergedType>, dataSetId: string, subpath: string, schema: z.ZodType<T>, apiKey?: string, signal?: AbortSignal): Promise<T>;
|
|
31
|
+
/** The road-geometry polylines of a run, one entry per route. */
|
|
32
|
+
export type Waypoints = {
|
|
33
|
+
id?: string | null;
|
|
34
|
+
waypoints?: number[][] | null;
|
|
35
|
+
}[];
|
|
36
|
+
/**
|
|
37
|
+
* Fetches the computed road-geometry waypoints for a run — the polylines the map draws instead of straight lines.
|
|
38
|
+
* Resolves empty on a 404, which the Models API answers for every run without a stored solution.
|
|
39
|
+
*/
|
|
40
|
+
export declare function fetchWaypoints<InputType, OutputType, MergedType>(config: VisualizationConfig<InputType, OutputType, MergedType>, dataSetId: string, apiKey?: string, signal?: AbortSignal): Promise<Waypoints>;
|
|
41
|
+
declare const scheduleResponseSchema: z.ZodObject<{
|
|
42
|
+
id: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
43
|
+
solverStatus: z.ZodOptional<z.ZodNullable<z.ZodEnum<{
|
|
44
|
+
DATASET_CREATED: "DATASET_CREATED";
|
|
45
|
+
DATASET_VALIDATED: "DATASET_VALIDATED";
|
|
46
|
+
DATASET_INVALID: "DATASET_INVALID";
|
|
47
|
+
DATASET_COMPUTED: "DATASET_COMPUTED";
|
|
48
|
+
SOLVING_SCHEDULED: "SOLVING_SCHEDULED";
|
|
49
|
+
SOLVING_STARTED: "SOLVING_STARTED";
|
|
50
|
+
SOLVING_ACTIVE: "SOLVING_ACTIVE";
|
|
51
|
+
SOLVING_COMPLETED: "SOLVING_COMPLETED";
|
|
52
|
+
SOLVING_INCOMPLETE: "SOLVING_INCOMPLETE";
|
|
53
|
+
SOLVING_FAILED: "SOLVING_FAILED";
|
|
54
|
+
SOLVING_STOPPED: "SOLVING_STOPPED";
|
|
55
|
+
}>>>;
|
|
56
|
+
validationResult: z.ZodOptional<z.ZodNullable<z.ZodObject<{
|
|
57
|
+
errors: z.ZodOptional<z.ZodNullable<z.ZodArray<z.ZodString>>>;
|
|
58
|
+
}, z.core.$strip>>>;
|
|
59
|
+
}, z.core.$strip>;
|
|
60
|
+
export type ScheduleResponse = z.infer<typeof scheduleResponseSchema>;
|
|
61
|
+
/**
|
|
62
|
+
* Starts a solve the way the platform's run actions do: without a run it creates one from `activeDataSet`,
|
|
63
|
+
* a computed run is solved in place, and any other run is re-run from its input as a child run.
|
|
64
|
+
*/
|
|
65
|
+
export declare function requestSolve<InputType, OutputType, MergedType>(config: VisualizationConfig<InputType, OutputType, MergedType>, dataSetId: string | undefined, activeDataSet: ActiveDataSet<InputType> | null, apiKey: string): Promise<ScheduleResponse>;
|
|
66
|
+
/** Stops (terminates) an in-flight solve. Returns false on any non-OK response. */
|
|
67
|
+
export declare function stopSolvingDataset<InputType, OutputType, MergedType>(config: VisualizationConfig<InputType, OutputType, MergedType>, id: string, apiKey: string): Promise<boolean>;
|
|
68
|
+
export {};
|