@json-render/solid 0.12.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/LICENSE +201 -0
- package/README.md +243 -0
- package/dist/chunk-XTSB6FIM.mjs +87 -0
- package/dist/chunk-XTSB6FIM.mjs.map +1 -0
- package/dist/index.d.mts +602 -0
- package/dist/index.d.ts +602 -0
- package/dist/index.js +1853 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1727 -0
- package/dist/index.mjs.map +1 -0
- package/dist/schema.d.mts +102 -0
- package/dist/schema.d.ts +102 -0
- package/dist/schema.js +112 -0
- package/dist/schema.js.map +1 -0
- package/dist/schema.mjs +9 -0
- package/dist/schema.mjs.map +1 -0
- package/package.json +69 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,1727 @@
|
|
|
1
|
+
import {
|
|
2
|
+
elementTreeSchema,
|
|
3
|
+
schema
|
|
4
|
+
} from "./chunk-XTSB6FIM.mjs";
|
|
5
|
+
|
|
6
|
+
// src/contexts/state.tsx
|
|
7
|
+
import { createComponent as _$createComponent } from "solid-js/web";
|
|
8
|
+
import { createContext, useContext, createSignal, createEffect, createMemo, onCleanup } from "solid-js";
|
|
9
|
+
import { getByPath, createStateStore } from "@json-render/core";
|
|
10
|
+
import { flattenToPointers } from "@json-render/core/store-utils";
|
|
11
|
+
var StateContext = createContext(null);
|
|
12
|
+
function computeInitialFlat(isControlled, initialState) {
|
|
13
|
+
if (isControlled) return null;
|
|
14
|
+
if (Object.keys(initialState).length === 0) return {};
|
|
15
|
+
return flattenToPointers(initialState);
|
|
16
|
+
}
|
|
17
|
+
function StateProvider(props) {
|
|
18
|
+
let internalStore;
|
|
19
|
+
if (!props.store) {
|
|
20
|
+
internalStore = createStateStore(props.initialState ?? {});
|
|
21
|
+
}
|
|
22
|
+
const store = () => props.store ?? internalStore;
|
|
23
|
+
const initialMode = props.store ? "controlled" : "uncontrolled";
|
|
24
|
+
let modeWarned = false;
|
|
25
|
+
if (typeof globalThis !== "undefined" && globalThis.process?.env?.NODE_ENV !== "production") {
|
|
26
|
+
createEffect(() => {
|
|
27
|
+
const currentMode = props.store ? "controlled" : "uncontrolled";
|
|
28
|
+
if (currentMode !== initialMode && !modeWarned) {
|
|
29
|
+
modeWarned = true;
|
|
30
|
+
console.warn(`StateProvider: switching from ${initialMode} to ${currentMode} mode is not supported.`);
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
let prevInitialState = props.initialState;
|
|
35
|
+
let prevFlat = computeInitialFlat(!!props.store, props.initialState ?? {});
|
|
36
|
+
createEffect(() => {
|
|
37
|
+
if (props.store) return;
|
|
38
|
+
const initialState = props.initialState ?? {};
|
|
39
|
+
if (initialState === prevInitialState) return;
|
|
40
|
+
prevInitialState = initialState;
|
|
41
|
+
const nextFlat = initialState && Object.keys(initialState).length > 0 ? flattenToPointers(initialState) : {};
|
|
42
|
+
const prevFlatObj = prevFlat ?? {};
|
|
43
|
+
const allKeys = /* @__PURE__ */ new Set([...Object.keys(prevFlatObj), ...Object.keys(nextFlat)]);
|
|
44
|
+
const updates = {};
|
|
45
|
+
for (const key of allKeys) {
|
|
46
|
+
if (prevFlatObj[key] !== nextFlat[key]) {
|
|
47
|
+
updates[key] = key in nextFlat ? nextFlat[key] : void 0;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
prevFlat = nextFlat;
|
|
51
|
+
if (Object.keys(updates).length > 0) {
|
|
52
|
+
store().update(updates);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
const [state, setState] = createSignal(store().getSnapshot(), {
|
|
56
|
+
equals: false
|
|
57
|
+
});
|
|
58
|
+
const changeListeners = /* @__PURE__ */ new Set();
|
|
59
|
+
const subscribeChanges = (listener) => {
|
|
60
|
+
changeListeners.add(listener);
|
|
61
|
+
return () => {
|
|
62
|
+
changeListeners.delete(listener);
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
const notifyChanges = (changes) => {
|
|
66
|
+
for (const listener of changeListeners) {
|
|
67
|
+
listener(changes);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
createEffect(() => {
|
|
71
|
+
const s = store();
|
|
72
|
+
setState(s.getSnapshot());
|
|
73
|
+
const unsubscribe = s.subscribe(() => {
|
|
74
|
+
setState(s.getSnapshot());
|
|
75
|
+
});
|
|
76
|
+
onCleanup(unsubscribe);
|
|
77
|
+
});
|
|
78
|
+
const set = (path, value) => {
|
|
79
|
+
const s = store();
|
|
80
|
+
const prev = s.getSnapshot();
|
|
81
|
+
const prevValue = getByPath(prev, path);
|
|
82
|
+
s.set(path, value);
|
|
83
|
+
if (prevValue !== value) {
|
|
84
|
+
const changes = [{
|
|
85
|
+
path,
|
|
86
|
+
value
|
|
87
|
+
}];
|
|
88
|
+
notifyChanges(changes);
|
|
89
|
+
if (!props.store && s.getSnapshot() !== prev) {
|
|
90
|
+
props.onStateChange?.(changes);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
const update = (updates) => {
|
|
95
|
+
const s = store();
|
|
96
|
+
const prev = s.getSnapshot();
|
|
97
|
+
s.update(updates);
|
|
98
|
+
const changes = [];
|
|
99
|
+
for (const [path, value] of Object.entries(updates)) {
|
|
100
|
+
if (getByPath(prev, path) !== value) {
|
|
101
|
+
changes.push({
|
|
102
|
+
path,
|
|
103
|
+
value
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (changes.length > 0) {
|
|
108
|
+
notifyChanges(changes);
|
|
109
|
+
if (!props.store && s.getSnapshot() !== prev) {
|
|
110
|
+
props.onStateChange?.(changes);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
const get = (path) => store().get(path);
|
|
115
|
+
const getSnapshot = () => store().getSnapshot();
|
|
116
|
+
const ctx = {
|
|
117
|
+
get state() {
|
|
118
|
+
return state();
|
|
119
|
+
},
|
|
120
|
+
get,
|
|
121
|
+
set,
|
|
122
|
+
update,
|
|
123
|
+
getSnapshot,
|
|
124
|
+
subscribeChanges
|
|
125
|
+
};
|
|
126
|
+
return _$createComponent(StateContext.Provider, {
|
|
127
|
+
value: ctx,
|
|
128
|
+
get children() {
|
|
129
|
+
return props.children;
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
function useStateStore() {
|
|
134
|
+
const ctx = useContext(StateContext);
|
|
135
|
+
if (!ctx) {
|
|
136
|
+
throw new Error("useStateStore must be used within a StateProvider");
|
|
137
|
+
}
|
|
138
|
+
return ctx;
|
|
139
|
+
}
|
|
140
|
+
function useStateValue(path) {
|
|
141
|
+
const store = useStateStore();
|
|
142
|
+
return createMemo(() => getByPath(store.state, path));
|
|
143
|
+
}
|
|
144
|
+
function useStateBinding(path) {
|
|
145
|
+
const store = useStateStore();
|
|
146
|
+
const value = createMemo(() => getByPath(store.state, path));
|
|
147
|
+
const setValue = (newValue) => store.set(path, newValue);
|
|
148
|
+
return [value, setValue];
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// src/contexts/visibility.tsx
|
|
152
|
+
import { createComponent as _$createComponent2 } from "solid-js/web";
|
|
153
|
+
import { createContext as createContext2, useContext as useContext2 } from "solid-js";
|
|
154
|
+
import { evaluateVisibility } from "@json-render/core";
|
|
155
|
+
var VisibilityContext = createContext2(null);
|
|
156
|
+
function VisibilityProvider(props) {
|
|
157
|
+
const stateStore = useStateStore();
|
|
158
|
+
const visibilityCtx = {
|
|
159
|
+
get stateModel() {
|
|
160
|
+
return stateStore.state;
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
const value = {
|
|
164
|
+
isVisible: (condition) => evaluateVisibility(condition, visibilityCtx),
|
|
165
|
+
ctx: visibilityCtx
|
|
166
|
+
};
|
|
167
|
+
return _$createComponent2(VisibilityContext.Provider, {
|
|
168
|
+
value,
|
|
169
|
+
get children() {
|
|
170
|
+
return props.children;
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
function useVisibility() {
|
|
175
|
+
const ctx = useContext2(VisibilityContext);
|
|
176
|
+
if (!ctx) {
|
|
177
|
+
throw new Error("useVisibility must be used within a VisibilityProvider");
|
|
178
|
+
}
|
|
179
|
+
return ctx;
|
|
180
|
+
}
|
|
181
|
+
function useIsVisible(condition) {
|
|
182
|
+
const {
|
|
183
|
+
isVisible
|
|
184
|
+
} = useVisibility();
|
|
185
|
+
return isVisible(condition);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// src/contexts/actions.tsx
|
|
189
|
+
import { template as _$template } from "solid-js/web";
|
|
190
|
+
import { delegateEvents as _$delegateEvents } from "solid-js/web";
|
|
191
|
+
import { setStyleProperty as _$setStyleProperty } from "solid-js/web";
|
|
192
|
+
import { effect as _$effect } from "solid-js/web";
|
|
193
|
+
import { memo as _$memo } from "solid-js/web";
|
|
194
|
+
import { insert as _$insert } from "solid-js/web";
|
|
195
|
+
import { addEventListener as _$addEventListener } from "solid-js/web";
|
|
196
|
+
import { createComponent as _$createComponent4 } from "solid-js/web";
|
|
197
|
+
import { createContext as createContext4, useContext as useContext4, createSignal as createSignal3 } from "solid-js";
|
|
198
|
+
import { resolveAction, executeAction } from "@json-render/core";
|
|
199
|
+
|
|
200
|
+
// src/contexts/validation.tsx
|
|
201
|
+
import { createComponent as _$createComponent3 } from "solid-js/web";
|
|
202
|
+
import { createContext as createContext3, useContext as useContext3, createSignal as createSignal2, createEffect as createEffect2, createMemo as createMemo2 } from "solid-js";
|
|
203
|
+
import { runValidation } from "@json-render/core";
|
|
204
|
+
var ValidationContext = createContext3(null);
|
|
205
|
+
function dynamicArgsEqual(a, b) {
|
|
206
|
+
if (a === b) return true;
|
|
207
|
+
if (!a || !b) return false;
|
|
208
|
+
const keysA = Object.keys(a);
|
|
209
|
+
const keysB = Object.keys(b);
|
|
210
|
+
if (keysA.length !== keysB.length) return false;
|
|
211
|
+
for (const key of keysA) {
|
|
212
|
+
const va = a[key];
|
|
213
|
+
const vb = b[key];
|
|
214
|
+
if (va === vb) continue;
|
|
215
|
+
if (typeof va === "object" && va !== null && typeof vb === "object" && vb !== null) {
|
|
216
|
+
const sa = va.$state;
|
|
217
|
+
const sb = vb.$state;
|
|
218
|
+
if (typeof sa === "string" && sa === sb) continue;
|
|
219
|
+
}
|
|
220
|
+
return false;
|
|
221
|
+
}
|
|
222
|
+
return true;
|
|
223
|
+
}
|
|
224
|
+
function validationConfigEqual(a, b) {
|
|
225
|
+
if (a === b) return true;
|
|
226
|
+
if (a.validateOn !== b.validateOn) return false;
|
|
227
|
+
const ac = a.checks ?? [];
|
|
228
|
+
const bc = b.checks ?? [];
|
|
229
|
+
if (ac.length !== bc.length) return false;
|
|
230
|
+
for (let i = 0; i < ac.length; i++) {
|
|
231
|
+
const ca = ac[i];
|
|
232
|
+
const cb = bc[i];
|
|
233
|
+
if (ca.type !== cb.type) return false;
|
|
234
|
+
if (ca.message !== cb.message) return false;
|
|
235
|
+
if (!dynamicArgsEqual(ca.args, cb.args)) return false;
|
|
236
|
+
}
|
|
237
|
+
return true;
|
|
238
|
+
}
|
|
239
|
+
function ValidationProvider(props) {
|
|
240
|
+
const {
|
|
241
|
+
getSnapshot
|
|
242
|
+
} = useStateStore();
|
|
243
|
+
const customFunctions = () => props.customFunctions ?? {};
|
|
244
|
+
const [fieldStates, setFieldStates] = createSignal2({});
|
|
245
|
+
let fieldStatesRef = {};
|
|
246
|
+
const [fieldConfigs, setFieldConfigs] = createSignal2({});
|
|
247
|
+
const registerField = (path, config) => {
|
|
248
|
+
setFieldConfigs((prev) => {
|
|
249
|
+
const existing = prev[path];
|
|
250
|
+
if (existing && validationConfigEqual(existing, config)) {
|
|
251
|
+
return prev;
|
|
252
|
+
}
|
|
253
|
+
return {
|
|
254
|
+
...prev,
|
|
255
|
+
[path]: config
|
|
256
|
+
};
|
|
257
|
+
});
|
|
258
|
+
};
|
|
259
|
+
const validate = (path, config) => {
|
|
260
|
+
const currentState = getSnapshot();
|
|
261
|
+
const segments = path.split("/").filter(Boolean);
|
|
262
|
+
let value2 = currentState;
|
|
263
|
+
for (const seg of segments) {
|
|
264
|
+
if (value2 != null && typeof value2 === "object") {
|
|
265
|
+
value2 = value2[seg];
|
|
266
|
+
} else {
|
|
267
|
+
value2 = void 0;
|
|
268
|
+
break;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
const result = runValidation(config, {
|
|
272
|
+
value: value2,
|
|
273
|
+
stateModel: currentState,
|
|
274
|
+
customFunctions: customFunctions()
|
|
275
|
+
});
|
|
276
|
+
const newFieldState = {
|
|
277
|
+
touched: fieldStatesRef[path]?.touched ?? true,
|
|
278
|
+
validated: true,
|
|
279
|
+
result
|
|
280
|
+
};
|
|
281
|
+
fieldStatesRef = {
|
|
282
|
+
...fieldStatesRef,
|
|
283
|
+
[path]: newFieldState
|
|
284
|
+
};
|
|
285
|
+
setFieldStates(fieldStatesRef);
|
|
286
|
+
return result;
|
|
287
|
+
};
|
|
288
|
+
const touch = (path) => {
|
|
289
|
+
fieldStatesRef = {
|
|
290
|
+
...fieldStatesRef,
|
|
291
|
+
[path]: {
|
|
292
|
+
...fieldStatesRef[path],
|
|
293
|
+
touched: true,
|
|
294
|
+
validated: fieldStatesRef[path]?.validated ?? false,
|
|
295
|
+
result: fieldStatesRef[path]?.result ?? null
|
|
296
|
+
}
|
|
297
|
+
};
|
|
298
|
+
setFieldStates(fieldStatesRef);
|
|
299
|
+
};
|
|
300
|
+
const clear = (path) => {
|
|
301
|
+
const {
|
|
302
|
+
[path]: _,
|
|
303
|
+
...rest
|
|
304
|
+
} = fieldStatesRef;
|
|
305
|
+
fieldStatesRef = rest;
|
|
306
|
+
setFieldStates(rest);
|
|
307
|
+
};
|
|
308
|
+
const validateAll = () => {
|
|
309
|
+
let allValid = true;
|
|
310
|
+
for (const [path, config] of Object.entries(fieldConfigs())) {
|
|
311
|
+
const result = validate(path, config);
|
|
312
|
+
if (!result.valid) {
|
|
313
|
+
allValid = false;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
return allValid;
|
|
317
|
+
};
|
|
318
|
+
const value = {
|
|
319
|
+
get customFunctions() {
|
|
320
|
+
return customFunctions();
|
|
321
|
+
},
|
|
322
|
+
get fieldStates() {
|
|
323
|
+
fieldStates();
|
|
324
|
+
return fieldStatesRef;
|
|
325
|
+
},
|
|
326
|
+
validate,
|
|
327
|
+
touch,
|
|
328
|
+
clear,
|
|
329
|
+
validateAll,
|
|
330
|
+
registerField
|
|
331
|
+
};
|
|
332
|
+
return _$createComponent3(ValidationContext.Provider, {
|
|
333
|
+
value,
|
|
334
|
+
get children() {
|
|
335
|
+
return props.children;
|
|
336
|
+
}
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
function useValidation() {
|
|
340
|
+
const ctx = useContext3(ValidationContext);
|
|
341
|
+
if (!ctx) {
|
|
342
|
+
throw new Error("useValidation must be used within a ValidationProvider");
|
|
343
|
+
}
|
|
344
|
+
return ctx;
|
|
345
|
+
}
|
|
346
|
+
function useOptionalValidation() {
|
|
347
|
+
return useContext3(ValidationContext);
|
|
348
|
+
}
|
|
349
|
+
function useFieldValidation(path, config) {
|
|
350
|
+
const validation = useValidation();
|
|
351
|
+
createEffect2(() => {
|
|
352
|
+
if (path && config) {
|
|
353
|
+
validation.registerField(path, config);
|
|
354
|
+
}
|
|
355
|
+
});
|
|
356
|
+
const state = createMemo2(() => {
|
|
357
|
+
const current = validation.fieldStates[path];
|
|
358
|
+
return current ?? {
|
|
359
|
+
touched: false,
|
|
360
|
+
validated: false,
|
|
361
|
+
result: null
|
|
362
|
+
};
|
|
363
|
+
});
|
|
364
|
+
const validate = () => validation.validate(path, config ?? {
|
|
365
|
+
checks: []
|
|
366
|
+
});
|
|
367
|
+
const touch = () => validation.touch(path);
|
|
368
|
+
const clear = () => validation.clear(path);
|
|
369
|
+
const errors = createMemo2(() => state().result?.errors ?? []);
|
|
370
|
+
const isValid = createMemo2(() => state().result?.valid ?? true);
|
|
371
|
+
return {
|
|
372
|
+
state,
|
|
373
|
+
validate,
|
|
374
|
+
touch,
|
|
375
|
+
clear,
|
|
376
|
+
errors,
|
|
377
|
+
isValid
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
// src/contexts/actions.tsx
|
|
382
|
+
var _tmpl$ = /* @__PURE__ */ _$template(`<div style="position:fixed;inset:0;background-color:rgba(0, 0, 0, 0.5);display:flex;align-items:center;justify-content:center;z-index:50"><div style="background-color:white;border-radius:8px;padding:24px;max-width:400px;width:100%;box-shadow:0 20px 25px -5px rgba(0, 0, 0, 0.1)"><h3 style="margin:0 0 8px 0;font-size:18px;font-weight:600"></h3><p style="margin:0 0 24px 0;color:#6b7280"></p><div style=display:flex;gap:12px;justify-content:flex-end><button style="padding:8px 16px;border-radius:6px;border:1px solid #d1d5db;background-color:white;cursor:pointer"></button><button style="padding:8px 16px;border-radius:6px;border:none;color:white;cursor:pointer">`);
|
|
383
|
+
var idCounter = 0;
|
|
384
|
+
function generateUniqueId() {
|
|
385
|
+
idCounter += 1;
|
|
386
|
+
return `${Date.now()}-${idCounter}`;
|
|
387
|
+
}
|
|
388
|
+
function deepResolveValue(value, get) {
|
|
389
|
+
if (value === null || value === void 0) return value;
|
|
390
|
+
if (value === "$id") {
|
|
391
|
+
return generateUniqueId();
|
|
392
|
+
}
|
|
393
|
+
if (typeof value === "object" && !Array.isArray(value)) {
|
|
394
|
+
const obj = value;
|
|
395
|
+
const keys = Object.keys(obj);
|
|
396
|
+
if (keys.length === 1 && typeof obj.$state === "string") {
|
|
397
|
+
return get(obj.$state);
|
|
398
|
+
}
|
|
399
|
+
if (keys.length === 1 && "$id" in obj) {
|
|
400
|
+
return generateUniqueId();
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
if (Array.isArray(value)) {
|
|
404
|
+
return value.map((item) => deepResolveValue(item, get));
|
|
405
|
+
}
|
|
406
|
+
if (typeof value === "object") {
|
|
407
|
+
const resolved = {};
|
|
408
|
+
for (const [key, val] of Object.entries(value)) {
|
|
409
|
+
resolved[key] = deepResolveValue(val, get);
|
|
410
|
+
}
|
|
411
|
+
return resolved;
|
|
412
|
+
}
|
|
413
|
+
return value;
|
|
414
|
+
}
|
|
415
|
+
var ActionContext = createContext4(null);
|
|
416
|
+
function ActionProvider(props) {
|
|
417
|
+
const {
|
|
418
|
+
get,
|
|
419
|
+
set,
|
|
420
|
+
getSnapshot
|
|
421
|
+
} = useStateStore();
|
|
422
|
+
const validation = useOptionalValidation();
|
|
423
|
+
const [handlers, setHandlers] = createSignal3(props.handlers ?? {});
|
|
424
|
+
const [loadingActions, setLoadingActions] = createSignal3(/* @__PURE__ */ new Set());
|
|
425
|
+
const [pendingConfirmation, setPendingConfirmation] = createSignal3(null);
|
|
426
|
+
const registerHandler = (name, handler) => {
|
|
427
|
+
setHandlers((prev) => ({
|
|
428
|
+
...prev,
|
|
429
|
+
[name]: handler
|
|
430
|
+
}));
|
|
431
|
+
};
|
|
432
|
+
const execute = async (binding) => {
|
|
433
|
+
const resolved = resolveAction(binding, getSnapshot());
|
|
434
|
+
if (resolved.action === "setState" && resolved.params) {
|
|
435
|
+
const statePath = resolved.params.statePath;
|
|
436
|
+
const value = resolved.params.value;
|
|
437
|
+
if (statePath) {
|
|
438
|
+
set(statePath, value);
|
|
439
|
+
}
|
|
440
|
+
return;
|
|
441
|
+
}
|
|
442
|
+
if (resolved.action === "pushState" && resolved.params) {
|
|
443
|
+
const statePath = resolved.params.statePath;
|
|
444
|
+
const rawValue = resolved.params.value;
|
|
445
|
+
if (statePath) {
|
|
446
|
+
const resolvedValue = deepResolveValue(rawValue, get);
|
|
447
|
+
const arr = get(statePath) ?? [];
|
|
448
|
+
set(statePath, [...arr, resolvedValue]);
|
|
449
|
+
const clearStatePath = resolved.params.clearStatePath;
|
|
450
|
+
if (clearStatePath) {
|
|
451
|
+
set(clearStatePath, "");
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
return;
|
|
455
|
+
}
|
|
456
|
+
if (resolved.action === "removeState" && resolved.params) {
|
|
457
|
+
const statePath = resolved.params.statePath;
|
|
458
|
+
const index = resolved.params.index;
|
|
459
|
+
if (statePath !== void 0 && index !== void 0) {
|
|
460
|
+
const arr = get(statePath) ?? [];
|
|
461
|
+
set(statePath, arr.filter((_, i) => i !== index));
|
|
462
|
+
}
|
|
463
|
+
return;
|
|
464
|
+
}
|
|
465
|
+
if (resolved.action === "push" && resolved.params) {
|
|
466
|
+
const screen = resolved.params.screen;
|
|
467
|
+
if (screen) {
|
|
468
|
+
const currentScreen = get("/currentScreen");
|
|
469
|
+
const navStack = get("/navStack") ?? [];
|
|
470
|
+
if (currentScreen) {
|
|
471
|
+
set("/navStack", [...navStack, currentScreen]);
|
|
472
|
+
} else {
|
|
473
|
+
set("/navStack", [...navStack, ""]);
|
|
474
|
+
}
|
|
475
|
+
set("/currentScreen", screen);
|
|
476
|
+
}
|
|
477
|
+
return;
|
|
478
|
+
}
|
|
479
|
+
if (resolved.action === "pop") {
|
|
480
|
+
const navStack = get("/navStack") ?? [];
|
|
481
|
+
if (navStack.length > 0) {
|
|
482
|
+
const previousScreen = navStack[navStack.length - 1];
|
|
483
|
+
set("/navStack", navStack.slice(0, -1));
|
|
484
|
+
if (previousScreen) {
|
|
485
|
+
set("/currentScreen", previousScreen);
|
|
486
|
+
} else {
|
|
487
|
+
set("/currentScreen", void 0);
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
return;
|
|
491
|
+
}
|
|
492
|
+
if (resolved.action === "validateForm") {
|
|
493
|
+
const validateAll = validation?.validateAll;
|
|
494
|
+
if (!validateAll) {
|
|
495
|
+
console.warn("validateForm action was dispatched but no ValidationProvider is connected. Ensure ValidationProvider is rendered inside the provider tree.");
|
|
496
|
+
return;
|
|
497
|
+
}
|
|
498
|
+
const valid = validateAll();
|
|
499
|
+
const errors = {};
|
|
500
|
+
for (const [path, fs] of Object.entries(validation.fieldStates)) {
|
|
501
|
+
if (fs.result && !fs.result.valid) {
|
|
502
|
+
errors[path] = fs.result.errors;
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
const statePath = resolved.params?.statePath || "/formValidation";
|
|
506
|
+
set(statePath, {
|
|
507
|
+
valid,
|
|
508
|
+
errors
|
|
509
|
+
});
|
|
510
|
+
return;
|
|
511
|
+
}
|
|
512
|
+
const handler = handlers()[resolved.action];
|
|
513
|
+
if (!handler) {
|
|
514
|
+
console.warn(`No handler registered for action: ${resolved.action}`);
|
|
515
|
+
return;
|
|
516
|
+
}
|
|
517
|
+
if (resolved.confirm) {
|
|
518
|
+
return new Promise((resolve, reject) => {
|
|
519
|
+
setPendingConfirmation({
|
|
520
|
+
action: resolved,
|
|
521
|
+
handler,
|
|
522
|
+
resolve: () => {
|
|
523
|
+
setPendingConfirmation(null);
|
|
524
|
+
resolve();
|
|
525
|
+
},
|
|
526
|
+
reject: () => {
|
|
527
|
+
setPendingConfirmation(null);
|
|
528
|
+
reject(new Error("Action cancelled"));
|
|
529
|
+
}
|
|
530
|
+
});
|
|
531
|
+
}).then(async () => {
|
|
532
|
+
setLoadingActions((prev) => new Set(prev).add(resolved.action));
|
|
533
|
+
try {
|
|
534
|
+
await executeAction({
|
|
535
|
+
action: resolved,
|
|
536
|
+
handler,
|
|
537
|
+
setState: set,
|
|
538
|
+
navigate: props.navigate,
|
|
539
|
+
executeAction: async (name) => {
|
|
540
|
+
const subBinding = {
|
|
541
|
+
action: name
|
|
542
|
+
};
|
|
543
|
+
await execute(subBinding);
|
|
544
|
+
}
|
|
545
|
+
});
|
|
546
|
+
} finally {
|
|
547
|
+
setLoadingActions((prev) => {
|
|
548
|
+
const next = new Set(prev);
|
|
549
|
+
next.delete(resolved.action);
|
|
550
|
+
return next;
|
|
551
|
+
});
|
|
552
|
+
}
|
|
553
|
+
});
|
|
554
|
+
}
|
|
555
|
+
setLoadingActions((prev) => new Set(prev).add(resolved.action));
|
|
556
|
+
try {
|
|
557
|
+
await executeAction({
|
|
558
|
+
action: resolved,
|
|
559
|
+
handler,
|
|
560
|
+
setState: set,
|
|
561
|
+
navigate: props.navigate,
|
|
562
|
+
executeAction: async (name) => {
|
|
563
|
+
const subBinding = {
|
|
564
|
+
action: name
|
|
565
|
+
};
|
|
566
|
+
await execute(subBinding);
|
|
567
|
+
}
|
|
568
|
+
});
|
|
569
|
+
} finally {
|
|
570
|
+
setLoadingActions((prev) => {
|
|
571
|
+
const next = new Set(prev);
|
|
572
|
+
next.delete(resolved.action);
|
|
573
|
+
return next;
|
|
574
|
+
});
|
|
575
|
+
}
|
|
576
|
+
};
|
|
577
|
+
const confirm = () => {
|
|
578
|
+
pendingConfirmation()?.resolve();
|
|
579
|
+
};
|
|
580
|
+
const cancel = () => {
|
|
581
|
+
pendingConfirmation()?.reject();
|
|
582
|
+
};
|
|
583
|
+
const ctx = {
|
|
584
|
+
get handlers() {
|
|
585
|
+
return handlers();
|
|
586
|
+
},
|
|
587
|
+
get loadingActions() {
|
|
588
|
+
return loadingActions();
|
|
589
|
+
},
|
|
590
|
+
get pendingConfirmation() {
|
|
591
|
+
return pendingConfirmation();
|
|
592
|
+
},
|
|
593
|
+
execute,
|
|
594
|
+
confirm,
|
|
595
|
+
cancel,
|
|
596
|
+
registerHandler
|
|
597
|
+
};
|
|
598
|
+
return _$createComponent4(ActionContext.Provider, {
|
|
599
|
+
value: ctx,
|
|
600
|
+
get children() {
|
|
601
|
+
return props.children;
|
|
602
|
+
}
|
|
603
|
+
});
|
|
604
|
+
}
|
|
605
|
+
function useActions() {
|
|
606
|
+
const ctx = useContext4(ActionContext);
|
|
607
|
+
if (!ctx) {
|
|
608
|
+
throw new Error("useActions must be used within an ActionProvider");
|
|
609
|
+
}
|
|
610
|
+
return ctx;
|
|
611
|
+
}
|
|
612
|
+
function useAction(binding) {
|
|
613
|
+
const actions = useActions();
|
|
614
|
+
return {
|
|
615
|
+
execute: () => actions.execute(binding),
|
|
616
|
+
get isLoading() {
|
|
617
|
+
return actions.loadingActions.has(binding.action);
|
|
618
|
+
}
|
|
619
|
+
};
|
|
620
|
+
}
|
|
621
|
+
function ConfirmDialog(props) {
|
|
622
|
+
const isDanger = () => props.confirm.variant === "danger";
|
|
623
|
+
return (() => {
|
|
624
|
+
var _el$ = _tmpl$(), _el$2 = _el$.firstChild, _el$3 = _el$2.firstChild, _el$4 = _el$3.nextSibling, _el$5 = _el$4.nextSibling, _el$6 = _el$5.firstChild, _el$7 = _el$6.nextSibling;
|
|
625
|
+
_$addEventListener(_el$, "click", props.onCancel, true);
|
|
626
|
+
_el$2.$$click = (e) => e.stopPropagation();
|
|
627
|
+
_$insert(_el$3, () => props.confirm.title);
|
|
628
|
+
_$insert(_el$4, () => props.confirm.message);
|
|
629
|
+
_$addEventListener(_el$6, "click", props.onCancel, true);
|
|
630
|
+
_$insert(_el$6, () => props.confirm.cancelLabel ?? "Cancel");
|
|
631
|
+
_$addEventListener(_el$7, "click", props.onConfirm, true);
|
|
632
|
+
_$insert(_el$7, () => props.confirm.confirmLabel ?? "Confirm");
|
|
633
|
+
_$effect((_$p) => _$setStyleProperty(_el$7, "background-color", isDanger() ? "#dc2626" : "#3b82f6"));
|
|
634
|
+
return _el$;
|
|
635
|
+
})();
|
|
636
|
+
}
|
|
637
|
+
_$delegateEvents(["click"]);
|
|
638
|
+
|
|
639
|
+
// src/contexts/repeat-scope.tsx
|
|
640
|
+
import { createComponent as _$createComponent5 } from "solid-js/web";
|
|
641
|
+
import { createContext as createContext5, useContext as useContext5 } from "solid-js";
|
|
642
|
+
var RepeatScopeContext = createContext5(null);
|
|
643
|
+
function RepeatScopeProvider(props) {
|
|
644
|
+
return _$createComponent5(RepeatScopeContext.Provider, {
|
|
645
|
+
get value() {
|
|
646
|
+
return {
|
|
647
|
+
item: props.item,
|
|
648
|
+
index: props.index,
|
|
649
|
+
basePath: props.basePath
|
|
650
|
+
};
|
|
651
|
+
},
|
|
652
|
+
get children() {
|
|
653
|
+
return props.children;
|
|
654
|
+
}
|
|
655
|
+
});
|
|
656
|
+
}
|
|
657
|
+
function useRepeatScope() {
|
|
658
|
+
return useContext5(RepeatScopeContext);
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
// src/index.ts
|
|
662
|
+
import { createStateStore as createStateStore2 } from "@json-render/core";
|
|
663
|
+
|
|
664
|
+
// src/renderer.tsx
|
|
665
|
+
import { memo as _$memo2 } from "solid-js/web";
|
|
666
|
+
import { createComponent as _$createComponent6 } from "solid-js/web";
|
|
667
|
+
import { createContext as createContext6, useContext as useContext6, createMemo as createMemo3, createEffect as createEffect3, onCleanup as onCleanup2, ErrorBoundary, Show, For } from "solid-js";
|
|
668
|
+
import { resolveElementProps, resolveBindings, resolveActionParam, evaluateVisibility as evaluateVisibility2, getByPath as getByPath2 } from "@json-render/core";
|
|
669
|
+
var EMPTY_FUNCTIONS = {};
|
|
670
|
+
var FunctionsContext = createContext6(EMPTY_FUNCTIONS);
|
|
671
|
+
function useFunctions() {
|
|
672
|
+
return useContext6(FunctionsContext) ?? EMPTY_FUNCTIONS;
|
|
673
|
+
}
|
|
674
|
+
function ElementRenderer(props) {
|
|
675
|
+
const repeatScope = useRepeatScope();
|
|
676
|
+
const {
|
|
677
|
+
ctx
|
|
678
|
+
} = useVisibility();
|
|
679
|
+
const {
|
|
680
|
+
execute
|
|
681
|
+
} = useActions();
|
|
682
|
+
const stateStore = useStateStore();
|
|
683
|
+
const getSnapshot = () => stateStore.getSnapshot();
|
|
684
|
+
const functions = useFunctions();
|
|
685
|
+
const fullCtx = createMemo3(() => {
|
|
686
|
+
const repeatItem = repeatScope?.item;
|
|
687
|
+
const repeatIndex = repeatScope?.index;
|
|
688
|
+
const repeatBasePath = repeatScope?.basePath;
|
|
689
|
+
return {
|
|
690
|
+
get stateModel() {
|
|
691
|
+
return ctx.stateModel;
|
|
692
|
+
},
|
|
693
|
+
...repeatItem !== void 0 ? {
|
|
694
|
+
repeatItem
|
|
695
|
+
} : {},
|
|
696
|
+
...repeatIndex !== void 0 ? {
|
|
697
|
+
repeatIndex
|
|
698
|
+
} : {},
|
|
699
|
+
...repeatBasePath !== void 0 ? {
|
|
700
|
+
repeatBasePath
|
|
701
|
+
} : {},
|
|
702
|
+
functions
|
|
703
|
+
};
|
|
704
|
+
});
|
|
705
|
+
const isVisible = createMemo3(() => props.element.visible === void 0 ? true : evaluateVisibility2(props.element.visible, fullCtx()));
|
|
706
|
+
const emit = async (eventName) => {
|
|
707
|
+
const onBindings = props.element.on;
|
|
708
|
+
const binding = onBindings?.[eventName];
|
|
709
|
+
if (!binding) return;
|
|
710
|
+
const actionBindings = Array.isArray(binding) ? binding : [binding];
|
|
711
|
+
for (const b of actionBindings) {
|
|
712
|
+
if (!b.params) {
|
|
713
|
+
await execute(b);
|
|
714
|
+
continue;
|
|
715
|
+
}
|
|
716
|
+
const liveCtx = {
|
|
717
|
+
...fullCtx(),
|
|
718
|
+
stateModel: getSnapshot()
|
|
719
|
+
};
|
|
720
|
+
const resolved = {};
|
|
721
|
+
for (const [key, val] of Object.entries(b.params)) {
|
|
722
|
+
resolved[key] = resolveActionParam(val, liveCtx);
|
|
723
|
+
}
|
|
724
|
+
await execute({
|
|
725
|
+
...b,
|
|
726
|
+
params: resolved
|
|
727
|
+
});
|
|
728
|
+
}
|
|
729
|
+
};
|
|
730
|
+
const on = (eventName) => {
|
|
731
|
+
const onBindings = props.element.on;
|
|
732
|
+
const binding = onBindings?.[eventName];
|
|
733
|
+
if (!binding) {
|
|
734
|
+
return {
|
|
735
|
+
emit: () => {
|
|
736
|
+
},
|
|
737
|
+
shouldPreventDefault: false,
|
|
738
|
+
bound: false
|
|
739
|
+
};
|
|
740
|
+
}
|
|
741
|
+
const actionBindings = Array.isArray(binding) ? binding : [binding];
|
|
742
|
+
const shouldPreventDefault = actionBindings.some((b) => b.preventDefault);
|
|
743
|
+
return {
|
|
744
|
+
emit: () => emit(eventName),
|
|
745
|
+
shouldPreventDefault,
|
|
746
|
+
bound: true
|
|
747
|
+
};
|
|
748
|
+
};
|
|
749
|
+
createEffect3(() => {
|
|
750
|
+
const watchConfig = props.element.watch;
|
|
751
|
+
if (!watchConfig) return;
|
|
752
|
+
const paths = Object.keys(watchConfig);
|
|
753
|
+
if (paths.length === 0) return;
|
|
754
|
+
const unsubscribe = stateStore.subscribeChanges((changes) => {
|
|
755
|
+
const changedPaths = new Set(changes.map((change) => change.path));
|
|
756
|
+
void (async () => {
|
|
757
|
+
for (const path of paths) {
|
|
758
|
+
if (!changedPaths.has(path)) continue;
|
|
759
|
+
const binding = watchConfig[path];
|
|
760
|
+
if (!binding) continue;
|
|
761
|
+
const bindings = Array.isArray(binding) ? binding : [binding];
|
|
762
|
+
for (const b of bindings) {
|
|
763
|
+
if (!b.params) {
|
|
764
|
+
await execute(b);
|
|
765
|
+
continue;
|
|
766
|
+
}
|
|
767
|
+
const liveCtx = {
|
|
768
|
+
...fullCtx(),
|
|
769
|
+
stateModel: getSnapshot()
|
|
770
|
+
};
|
|
771
|
+
const resolved = {};
|
|
772
|
+
for (const [key, val] of Object.entries(b.params)) {
|
|
773
|
+
resolved[key] = resolveActionParam(val, liveCtx);
|
|
774
|
+
}
|
|
775
|
+
await execute({
|
|
776
|
+
...b,
|
|
777
|
+
params: resolved
|
|
778
|
+
});
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
})().catch(console.error);
|
|
782
|
+
});
|
|
783
|
+
onCleanup2(() => {
|
|
784
|
+
unsubscribe();
|
|
785
|
+
});
|
|
786
|
+
});
|
|
787
|
+
const elementBindings = createMemo3(() => {
|
|
788
|
+
const rawProps = props.element.props;
|
|
789
|
+
return resolveBindings(rawProps, fullCtx());
|
|
790
|
+
});
|
|
791
|
+
const resolvedElement = createMemo3(() => {
|
|
792
|
+
const rawProps = props.element.props;
|
|
793
|
+
const resolvedProps = resolveElementProps(rawProps, fullCtx());
|
|
794
|
+
return resolvedProps !== props.element.props ? {
|
|
795
|
+
...props.element,
|
|
796
|
+
props: resolvedProps
|
|
797
|
+
} : props.element;
|
|
798
|
+
});
|
|
799
|
+
return _$createComponent6(Show, {
|
|
800
|
+
get when() {
|
|
801
|
+
return isVisible();
|
|
802
|
+
},
|
|
803
|
+
get children() {
|
|
804
|
+
return _$createComponent6(ErrorBoundary, {
|
|
805
|
+
fallback: (err) => {
|
|
806
|
+
console.error(`[json-render] Rendering error in <${props.element.type}>:`, err);
|
|
807
|
+
return null;
|
|
808
|
+
},
|
|
809
|
+
get children() {
|
|
810
|
+
return _$createComponent6(ElementRendererContent, {
|
|
811
|
+
get resolvedElement() {
|
|
812
|
+
return resolvedElement();
|
|
813
|
+
},
|
|
814
|
+
get spec() {
|
|
815
|
+
return props.spec;
|
|
816
|
+
},
|
|
817
|
+
get registry() {
|
|
818
|
+
return props.registry;
|
|
819
|
+
},
|
|
820
|
+
get loading() {
|
|
821
|
+
return props.loading;
|
|
822
|
+
},
|
|
823
|
+
get fallback() {
|
|
824
|
+
return props.fallback;
|
|
825
|
+
},
|
|
826
|
+
emit,
|
|
827
|
+
on,
|
|
828
|
+
get bindings() {
|
|
829
|
+
return elementBindings();
|
|
830
|
+
}
|
|
831
|
+
});
|
|
832
|
+
}
|
|
833
|
+
});
|
|
834
|
+
}
|
|
835
|
+
});
|
|
836
|
+
}
|
|
837
|
+
function ElementRendererContent(props) {
|
|
838
|
+
const Comp = () => props.registry[props.resolvedElement.type] ?? props.fallback;
|
|
839
|
+
return _$createComponent6(Show, {
|
|
840
|
+
get when() {
|
|
841
|
+
return Comp();
|
|
842
|
+
},
|
|
843
|
+
get fallback() {
|
|
844
|
+
console.warn(`No renderer for component type: ${props.resolvedElement.type}`);
|
|
845
|
+
return null;
|
|
846
|
+
},
|
|
847
|
+
children: (ComponentFn) => {
|
|
848
|
+
const Component = ComponentFn();
|
|
849
|
+
const children = () => props.resolvedElement.repeat ? _$createComponent6(RepeatChildren, {
|
|
850
|
+
get element() {
|
|
851
|
+
return props.resolvedElement;
|
|
852
|
+
},
|
|
853
|
+
get spec() {
|
|
854
|
+
return props.spec;
|
|
855
|
+
},
|
|
856
|
+
get registry() {
|
|
857
|
+
return props.registry;
|
|
858
|
+
},
|
|
859
|
+
get loading() {
|
|
860
|
+
return props.loading;
|
|
861
|
+
},
|
|
862
|
+
get fallback() {
|
|
863
|
+
return props.fallback;
|
|
864
|
+
}
|
|
865
|
+
}) : _$createComponent6(For, {
|
|
866
|
+
get each() {
|
|
867
|
+
return props.resolvedElement.children ?? [];
|
|
868
|
+
},
|
|
869
|
+
children: (childKey) => {
|
|
870
|
+
const childElement = () => props.spec.elements[childKey];
|
|
871
|
+
return _$createComponent6(Show, {
|
|
872
|
+
get when() {
|
|
873
|
+
return childElement();
|
|
874
|
+
},
|
|
875
|
+
get fallback() {
|
|
876
|
+
if (!props.loading) {
|
|
877
|
+
console.warn(`[json-render] Missing element "${childKey}" referenced as child of "${props.resolvedElement.type}". This element will not render.`);
|
|
878
|
+
}
|
|
879
|
+
return null;
|
|
880
|
+
},
|
|
881
|
+
children: (el) => _$createComponent6(ElementRenderer, {
|
|
882
|
+
get element() {
|
|
883
|
+
return el();
|
|
884
|
+
},
|
|
885
|
+
get spec() {
|
|
886
|
+
return props.spec;
|
|
887
|
+
},
|
|
888
|
+
get registry() {
|
|
889
|
+
return props.registry;
|
|
890
|
+
},
|
|
891
|
+
get loading() {
|
|
892
|
+
return props.loading;
|
|
893
|
+
},
|
|
894
|
+
get fallback() {
|
|
895
|
+
return props.fallback;
|
|
896
|
+
}
|
|
897
|
+
})
|
|
898
|
+
});
|
|
899
|
+
}
|
|
900
|
+
});
|
|
901
|
+
return _$createComponent6(Component, {
|
|
902
|
+
get element() {
|
|
903
|
+
return props.resolvedElement;
|
|
904
|
+
},
|
|
905
|
+
get emit() {
|
|
906
|
+
return props.emit;
|
|
907
|
+
},
|
|
908
|
+
get on() {
|
|
909
|
+
return props.on;
|
|
910
|
+
},
|
|
911
|
+
get bindings() {
|
|
912
|
+
return props.bindings;
|
|
913
|
+
},
|
|
914
|
+
get loading() {
|
|
915
|
+
return props.loading;
|
|
916
|
+
},
|
|
917
|
+
get children() {
|
|
918
|
+
return children();
|
|
919
|
+
}
|
|
920
|
+
});
|
|
921
|
+
}
|
|
922
|
+
});
|
|
923
|
+
}
|
|
924
|
+
function RepeatChildren(props) {
|
|
925
|
+
const stateStore = useStateStore();
|
|
926
|
+
const repeat = () => props.element.repeat;
|
|
927
|
+
const statePath = () => repeat().statePath;
|
|
928
|
+
const items = () => getByPath2(stateStore.state, statePath()) ?? [];
|
|
929
|
+
return _$createComponent6(For, {
|
|
930
|
+
get each() {
|
|
931
|
+
return items();
|
|
932
|
+
},
|
|
933
|
+
children: (itemValue, index) => {
|
|
934
|
+
const key = () => {
|
|
935
|
+
const r = repeat();
|
|
936
|
+
return r.key && typeof itemValue === "object" && itemValue !== null ? String(itemValue[r.key] ?? index()) : String(index());
|
|
937
|
+
};
|
|
938
|
+
return _$createComponent6(RepeatScopeProvider, {
|
|
939
|
+
item: itemValue,
|
|
940
|
+
get index() {
|
|
941
|
+
return index();
|
|
942
|
+
},
|
|
943
|
+
get basePath() {
|
|
944
|
+
return `${statePath()}/${index()}`;
|
|
945
|
+
},
|
|
946
|
+
get children() {
|
|
947
|
+
return _$createComponent6(For, {
|
|
948
|
+
get each() {
|
|
949
|
+
return props.element.children ?? [];
|
|
950
|
+
},
|
|
951
|
+
children: (childKey) => {
|
|
952
|
+
const childElement = () => props.spec.elements[childKey];
|
|
953
|
+
return _$createComponent6(Show, {
|
|
954
|
+
get when() {
|
|
955
|
+
return childElement();
|
|
956
|
+
},
|
|
957
|
+
get fallback() {
|
|
958
|
+
if (!props.loading) {
|
|
959
|
+
console.warn(`[json-render] Missing element "${childKey}" referenced as child of "${props.element.type}" (repeat). This element will not render.`);
|
|
960
|
+
}
|
|
961
|
+
return null;
|
|
962
|
+
},
|
|
963
|
+
children: (el) => _$createComponent6(ElementRenderer, {
|
|
964
|
+
get element() {
|
|
965
|
+
return el();
|
|
966
|
+
},
|
|
967
|
+
get spec() {
|
|
968
|
+
return props.spec;
|
|
969
|
+
},
|
|
970
|
+
get registry() {
|
|
971
|
+
return props.registry;
|
|
972
|
+
},
|
|
973
|
+
get loading() {
|
|
974
|
+
return props.loading;
|
|
975
|
+
},
|
|
976
|
+
get fallback() {
|
|
977
|
+
return props.fallback;
|
|
978
|
+
}
|
|
979
|
+
})
|
|
980
|
+
});
|
|
981
|
+
}
|
|
982
|
+
});
|
|
983
|
+
}
|
|
984
|
+
});
|
|
985
|
+
}
|
|
986
|
+
});
|
|
987
|
+
}
|
|
988
|
+
function Renderer(props) {
|
|
989
|
+
const rootElement = createMemo3(() => {
|
|
990
|
+
const spec = props.spec;
|
|
991
|
+
if (!spec || !spec.root) return void 0;
|
|
992
|
+
return spec.elements[spec.root];
|
|
993
|
+
});
|
|
994
|
+
return _$createComponent6(Show, {
|
|
995
|
+
get when() {
|
|
996
|
+
return rootElement();
|
|
997
|
+
},
|
|
998
|
+
children: (el) => _$createComponent6(ElementRenderer, {
|
|
999
|
+
get element() {
|
|
1000
|
+
return el();
|
|
1001
|
+
},
|
|
1002
|
+
get spec() {
|
|
1003
|
+
return props.spec;
|
|
1004
|
+
},
|
|
1005
|
+
get registry() {
|
|
1006
|
+
return props.registry;
|
|
1007
|
+
},
|
|
1008
|
+
get loading() {
|
|
1009
|
+
return props.loading;
|
|
1010
|
+
},
|
|
1011
|
+
get fallback() {
|
|
1012
|
+
return props.fallback;
|
|
1013
|
+
}
|
|
1014
|
+
})
|
|
1015
|
+
});
|
|
1016
|
+
}
|
|
1017
|
+
function JSONUIProvider(props) {
|
|
1018
|
+
return _$createComponent6(StateProvider, {
|
|
1019
|
+
get store() {
|
|
1020
|
+
return props.store;
|
|
1021
|
+
},
|
|
1022
|
+
get initialState() {
|
|
1023
|
+
return props.initialState;
|
|
1024
|
+
},
|
|
1025
|
+
get onStateChange() {
|
|
1026
|
+
return props.onStateChange;
|
|
1027
|
+
},
|
|
1028
|
+
get children() {
|
|
1029
|
+
return _$createComponent6(VisibilityProvider, {
|
|
1030
|
+
get children() {
|
|
1031
|
+
return _$createComponent6(ValidationProvider, {
|
|
1032
|
+
get customFunctions() {
|
|
1033
|
+
return props.validationFunctions;
|
|
1034
|
+
},
|
|
1035
|
+
get children() {
|
|
1036
|
+
return _$createComponent6(ActionProvider, {
|
|
1037
|
+
get handlers() {
|
|
1038
|
+
return props.handlers;
|
|
1039
|
+
},
|
|
1040
|
+
get navigate() {
|
|
1041
|
+
return props.navigate;
|
|
1042
|
+
},
|
|
1043
|
+
get children() {
|
|
1044
|
+
return _$createComponent6(FunctionsContext.Provider, {
|
|
1045
|
+
get value() {
|
|
1046
|
+
return props.functions ?? EMPTY_FUNCTIONS;
|
|
1047
|
+
},
|
|
1048
|
+
get children() {
|
|
1049
|
+
return [_$memo2(() => props.children), _$createComponent6(ConfirmationDialogManager, {})];
|
|
1050
|
+
}
|
|
1051
|
+
});
|
|
1052
|
+
}
|
|
1053
|
+
});
|
|
1054
|
+
}
|
|
1055
|
+
});
|
|
1056
|
+
}
|
|
1057
|
+
});
|
|
1058
|
+
}
|
|
1059
|
+
});
|
|
1060
|
+
}
|
|
1061
|
+
function ConfirmationDialogManager() {
|
|
1062
|
+
const {
|
|
1063
|
+
pendingConfirmation,
|
|
1064
|
+
confirm,
|
|
1065
|
+
cancel
|
|
1066
|
+
} = useActions();
|
|
1067
|
+
return _$createComponent6(Show, {
|
|
1068
|
+
get when() {
|
|
1069
|
+
return pendingConfirmation?.action.confirm;
|
|
1070
|
+
},
|
|
1071
|
+
children: (confirmConfig) => _$createComponent6(ConfirmDialog, {
|
|
1072
|
+
get confirm() {
|
|
1073
|
+
return confirmConfig();
|
|
1074
|
+
},
|
|
1075
|
+
onConfirm: confirm,
|
|
1076
|
+
onCancel: cancel
|
|
1077
|
+
})
|
|
1078
|
+
});
|
|
1079
|
+
}
|
|
1080
|
+
function defineRegistry(_catalog, options) {
|
|
1081
|
+
const registry = {};
|
|
1082
|
+
if (options.components) {
|
|
1083
|
+
for (const [name, componentFn] of Object.entries(options.components)) {
|
|
1084
|
+
registry[name] = (renderProps) => {
|
|
1085
|
+
return componentFn({
|
|
1086
|
+
get props() {
|
|
1087
|
+
return renderProps.element.props;
|
|
1088
|
+
},
|
|
1089
|
+
get children() {
|
|
1090
|
+
return renderProps.children;
|
|
1091
|
+
},
|
|
1092
|
+
emit: renderProps.emit,
|
|
1093
|
+
on: renderProps.on,
|
|
1094
|
+
get bindings() {
|
|
1095
|
+
return renderProps.bindings;
|
|
1096
|
+
},
|
|
1097
|
+
get loading() {
|
|
1098
|
+
return renderProps.loading;
|
|
1099
|
+
}
|
|
1100
|
+
});
|
|
1101
|
+
};
|
|
1102
|
+
}
|
|
1103
|
+
}
|
|
1104
|
+
const actionMap = options.actions ? Object.entries(options.actions) : [];
|
|
1105
|
+
const handlers = (getSetState, getState) => {
|
|
1106
|
+
const result = {};
|
|
1107
|
+
for (const [name, actionFn] of actionMap) {
|
|
1108
|
+
result[name] = async (params) => {
|
|
1109
|
+
const setState = getSetState();
|
|
1110
|
+
const state = getState();
|
|
1111
|
+
if (setState) {
|
|
1112
|
+
await actionFn(params, setState, state);
|
|
1113
|
+
}
|
|
1114
|
+
};
|
|
1115
|
+
}
|
|
1116
|
+
return result;
|
|
1117
|
+
};
|
|
1118
|
+
const executeAction2 = async (actionName, params, setState, state = {}) => {
|
|
1119
|
+
const entry = actionMap.find(([name]) => name === actionName);
|
|
1120
|
+
if (entry) {
|
|
1121
|
+
await entry[1](params, setState, state);
|
|
1122
|
+
} else {
|
|
1123
|
+
console.warn(`Unknown action: ${actionName}`);
|
|
1124
|
+
}
|
|
1125
|
+
};
|
|
1126
|
+
return {
|
|
1127
|
+
registry,
|
|
1128
|
+
handlers,
|
|
1129
|
+
executeAction: executeAction2
|
|
1130
|
+
};
|
|
1131
|
+
}
|
|
1132
|
+
function createRenderer(catalog, components) {
|
|
1133
|
+
const registry = components;
|
|
1134
|
+
return function CatalogRenderer(props) {
|
|
1135
|
+
const actionHandlers = () => props.onAction ? new Proxy({}, {
|
|
1136
|
+
get: (_target, prop) => {
|
|
1137
|
+
return (params) => props.onAction(prop, params);
|
|
1138
|
+
},
|
|
1139
|
+
has: () => true
|
|
1140
|
+
}) : void 0;
|
|
1141
|
+
return _$createComponent6(StateProvider, {
|
|
1142
|
+
get store() {
|
|
1143
|
+
return props.store;
|
|
1144
|
+
},
|
|
1145
|
+
get initialState() {
|
|
1146
|
+
return props.state;
|
|
1147
|
+
},
|
|
1148
|
+
get onStateChange() {
|
|
1149
|
+
return props.onStateChange;
|
|
1150
|
+
},
|
|
1151
|
+
get children() {
|
|
1152
|
+
return _$createComponent6(VisibilityProvider, {
|
|
1153
|
+
get children() {
|
|
1154
|
+
return _$createComponent6(ValidationProvider, {
|
|
1155
|
+
get children() {
|
|
1156
|
+
return _$createComponent6(ActionProvider, {
|
|
1157
|
+
get handlers() {
|
|
1158
|
+
return actionHandlers();
|
|
1159
|
+
},
|
|
1160
|
+
get children() {
|
|
1161
|
+
return _$createComponent6(FunctionsContext.Provider, {
|
|
1162
|
+
get value() {
|
|
1163
|
+
return props.functions ?? EMPTY_FUNCTIONS;
|
|
1164
|
+
},
|
|
1165
|
+
get children() {
|
|
1166
|
+
return [_$createComponent6(Renderer, {
|
|
1167
|
+
get spec() {
|
|
1168
|
+
return props.spec;
|
|
1169
|
+
},
|
|
1170
|
+
registry,
|
|
1171
|
+
get loading() {
|
|
1172
|
+
return props.loading;
|
|
1173
|
+
},
|
|
1174
|
+
get fallback() {
|
|
1175
|
+
return props.fallback;
|
|
1176
|
+
}
|
|
1177
|
+
}), _$createComponent6(ConfirmationDialogManager, {})];
|
|
1178
|
+
}
|
|
1179
|
+
});
|
|
1180
|
+
}
|
|
1181
|
+
});
|
|
1182
|
+
}
|
|
1183
|
+
});
|
|
1184
|
+
}
|
|
1185
|
+
});
|
|
1186
|
+
}
|
|
1187
|
+
});
|
|
1188
|
+
};
|
|
1189
|
+
}
|
|
1190
|
+
|
|
1191
|
+
// src/hooks.ts
|
|
1192
|
+
import { createSignal as createSignal4, onCleanup as onCleanup3, createMemo as createMemo4 } from "solid-js";
|
|
1193
|
+
import {
|
|
1194
|
+
setByPath,
|
|
1195
|
+
getByPath as getByPath3,
|
|
1196
|
+
removeByPath,
|
|
1197
|
+
createMixedStreamParser,
|
|
1198
|
+
applySpecPatch,
|
|
1199
|
+
nestedToFlat,
|
|
1200
|
+
SPEC_DATA_PART_TYPE
|
|
1201
|
+
} from "@json-render/core";
|
|
1202
|
+
function parseLine(line) {
|
|
1203
|
+
try {
|
|
1204
|
+
const trimmed = line.trim();
|
|
1205
|
+
if (!trimmed || trimmed.startsWith("//")) {
|
|
1206
|
+
return null;
|
|
1207
|
+
}
|
|
1208
|
+
const parsed = JSON.parse(trimmed);
|
|
1209
|
+
if (parsed.__meta === "usage") {
|
|
1210
|
+
return {
|
|
1211
|
+
type: "usage",
|
|
1212
|
+
usage: {
|
|
1213
|
+
promptTokens: parsed.promptTokens ?? 0,
|
|
1214
|
+
completionTokens: parsed.completionTokens ?? 0,
|
|
1215
|
+
totalTokens: parsed.totalTokens ?? 0
|
|
1216
|
+
}
|
|
1217
|
+
};
|
|
1218
|
+
}
|
|
1219
|
+
return { type: "patch", patch: parsed };
|
|
1220
|
+
} catch {
|
|
1221
|
+
return null;
|
|
1222
|
+
}
|
|
1223
|
+
}
|
|
1224
|
+
function setSpecValue(newSpec, path, value) {
|
|
1225
|
+
if (path === "/root") {
|
|
1226
|
+
newSpec.root = value;
|
|
1227
|
+
return;
|
|
1228
|
+
}
|
|
1229
|
+
if (path === "/state") {
|
|
1230
|
+
newSpec.state = value;
|
|
1231
|
+
return;
|
|
1232
|
+
}
|
|
1233
|
+
if (path.startsWith("/state/")) {
|
|
1234
|
+
if (!newSpec.state) newSpec.state = {};
|
|
1235
|
+
const statePath = path.slice("/state".length);
|
|
1236
|
+
setByPath(newSpec.state, statePath, value);
|
|
1237
|
+
return;
|
|
1238
|
+
}
|
|
1239
|
+
if (path.startsWith("/elements/")) {
|
|
1240
|
+
const pathParts = path.slice("/elements/".length).split("/");
|
|
1241
|
+
const elementKey = pathParts[0];
|
|
1242
|
+
if (!elementKey) return;
|
|
1243
|
+
if (pathParts.length === 1) {
|
|
1244
|
+
newSpec.elements[elementKey] = value;
|
|
1245
|
+
} else {
|
|
1246
|
+
const element = newSpec.elements[elementKey];
|
|
1247
|
+
if (element) {
|
|
1248
|
+
const propPath = "/" + pathParts.slice(1).join("/");
|
|
1249
|
+
const newElement = { ...element };
|
|
1250
|
+
setByPath(
|
|
1251
|
+
newElement,
|
|
1252
|
+
propPath,
|
|
1253
|
+
value
|
|
1254
|
+
);
|
|
1255
|
+
newSpec.elements[elementKey] = newElement;
|
|
1256
|
+
}
|
|
1257
|
+
}
|
|
1258
|
+
}
|
|
1259
|
+
}
|
|
1260
|
+
function removeSpecValue(newSpec, path) {
|
|
1261
|
+
if (path === "/state") {
|
|
1262
|
+
delete newSpec.state;
|
|
1263
|
+
return;
|
|
1264
|
+
}
|
|
1265
|
+
if (path.startsWith("/state/") && newSpec.state) {
|
|
1266
|
+
const statePath = path.slice("/state".length);
|
|
1267
|
+
removeByPath(newSpec.state, statePath);
|
|
1268
|
+
return;
|
|
1269
|
+
}
|
|
1270
|
+
if (path.startsWith("/elements/")) {
|
|
1271
|
+
const pathParts = path.slice("/elements/".length).split("/");
|
|
1272
|
+
const elementKey = pathParts[0];
|
|
1273
|
+
if (!elementKey) return;
|
|
1274
|
+
if (pathParts.length === 1) {
|
|
1275
|
+
const { [elementKey]: _, ...rest } = newSpec.elements;
|
|
1276
|
+
newSpec.elements = rest;
|
|
1277
|
+
} else {
|
|
1278
|
+
const element = newSpec.elements[elementKey];
|
|
1279
|
+
if (element) {
|
|
1280
|
+
const propPath = "/" + pathParts.slice(1).join("/");
|
|
1281
|
+
const newElement = { ...element };
|
|
1282
|
+
removeByPath(
|
|
1283
|
+
newElement,
|
|
1284
|
+
propPath
|
|
1285
|
+
);
|
|
1286
|
+
newSpec.elements[elementKey] = newElement;
|
|
1287
|
+
}
|
|
1288
|
+
}
|
|
1289
|
+
}
|
|
1290
|
+
}
|
|
1291
|
+
function getSpecValue(spec, path) {
|
|
1292
|
+
if (path === "/root") return spec.root;
|
|
1293
|
+
if (path === "/state") return spec.state;
|
|
1294
|
+
if (path.startsWith("/state/") && spec.state) {
|
|
1295
|
+
const statePath = path.slice("/state".length);
|
|
1296
|
+
return getByPath3(spec.state, statePath);
|
|
1297
|
+
}
|
|
1298
|
+
return getByPath3(spec, path);
|
|
1299
|
+
}
|
|
1300
|
+
function applyPatch(spec, patch) {
|
|
1301
|
+
const newSpec = {
|
|
1302
|
+
...spec,
|
|
1303
|
+
elements: { ...spec.elements },
|
|
1304
|
+
...spec.state ? { state: { ...spec.state } } : {}
|
|
1305
|
+
};
|
|
1306
|
+
switch (patch.op) {
|
|
1307
|
+
case "add":
|
|
1308
|
+
case "replace": {
|
|
1309
|
+
setSpecValue(newSpec, patch.path, patch.value);
|
|
1310
|
+
break;
|
|
1311
|
+
}
|
|
1312
|
+
case "remove": {
|
|
1313
|
+
removeSpecValue(newSpec, patch.path);
|
|
1314
|
+
break;
|
|
1315
|
+
}
|
|
1316
|
+
case "move": {
|
|
1317
|
+
if (!patch.from) break;
|
|
1318
|
+
const moveValue = getSpecValue(newSpec, patch.from);
|
|
1319
|
+
removeSpecValue(newSpec, patch.from);
|
|
1320
|
+
setSpecValue(newSpec, patch.path, moveValue);
|
|
1321
|
+
break;
|
|
1322
|
+
}
|
|
1323
|
+
case "copy": {
|
|
1324
|
+
if (!patch.from) break;
|
|
1325
|
+
const copyValue = getSpecValue(newSpec, patch.from);
|
|
1326
|
+
setSpecValue(newSpec, patch.path, copyValue);
|
|
1327
|
+
break;
|
|
1328
|
+
}
|
|
1329
|
+
case "test": {
|
|
1330
|
+
break;
|
|
1331
|
+
}
|
|
1332
|
+
}
|
|
1333
|
+
return newSpec;
|
|
1334
|
+
}
|
|
1335
|
+
function useUIStream(options) {
|
|
1336
|
+
const [spec, setSpec] = createSignal4(null);
|
|
1337
|
+
const [isStreaming, setIsStreaming] = createSignal4(false);
|
|
1338
|
+
const [error, setError] = createSignal4(null);
|
|
1339
|
+
const [usage, setUsage] = createSignal4(null);
|
|
1340
|
+
const [rawLines, setRawLines] = createSignal4([]);
|
|
1341
|
+
let abortController = null;
|
|
1342
|
+
const clear = () => {
|
|
1343
|
+
setSpec(null);
|
|
1344
|
+
setError(null);
|
|
1345
|
+
};
|
|
1346
|
+
const send = async (prompt, context) => {
|
|
1347
|
+
abortController?.abort();
|
|
1348
|
+
abortController = new AbortController();
|
|
1349
|
+
setIsStreaming(true);
|
|
1350
|
+
setError(null);
|
|
1351
|
+
setUsage(null);
|
|
1352
|
+
setRawLines([]);
|
|
1353
|
+
const previousSpec = context?.previousSpec;
|
|
1354
|
+
let currentSpec = previousSpec && previousSpec.root ? { ...previousSpec, elements: { ...previousSpec.elements } } : { root: "", elements: {} };
|
|
1355
|
+
setSpec(currentSpec);
|
|
1356
|
+
try {
|
|
1357
|
+
const response = await fetch(options.api, {
|
|
1358
|
+
method: "POST",
|
|
1359
|
+
headers: { "Content-Type": "application/json" },
|
|
1360
|
+
body: JSON.stringify({
|
|
1361
|
+
prompt,
|
|
1362
|
+
context,
|
|
1363
|
+
currentSpec
|
|
1364
|
+
}),
|
|
1365
|
+
signal: abortController.signal
|
|
1366
|
+
});
|
|
1367
|
+
if (!response.ok) {
|
|
1368
|
+
let errorMessage = `HTTP error: ${response.status}`;
|
|
1369
|
+
try {
|
|
1370
|
+
const errorData = await response.json();
|
|
1371
|
+
if (errorData.message) {
|
|
1372
|
+
errorMessage = errorData.message;
|
|
1373
|
+
} else if (errorData.error) {
|
|
1374
|
+
errorMessage = errorData.error;
|
|
1375
|
+
}
|
|
1376
|
+
} catch {
|
|
1377
|
+
}
|
|
1378
|
+
throw new Error(errorMessage);
|
|
1379
|
+
}
|
|
1380
|
+
const reader = response.body?.getReader();
|
|
1381
|
+
if (!reader) {
|
|
1382
|
+
throw new Error("No response body");
|
|
1383
|
+
}
|
|
1384
|
+
const decoder = new TextDecoder();
|
|
1385
|
+
let buffer = "";
|
|
1386
|
+
while (true) {
|
|
1387
|
+
const { done, value } = await reader.read();
|
|
1388
|
+
if (done) break;
|
|
1389
|
+
buffer += decoder.decode(value, { stream: true });
|
|
1390
|
+
const lines = buffer.split("\n");
|
|
1391
|
+
buffer = lines.pop() ?? "";
|
|
1392
|
+
for (const line of lines) {
|
|
1393
|
+
const trimmed = line.trim();
|
|
1394
|
+
if (!trimmed) continue;
|
|
1395
|
+
const result = parseLine(trimmed);
|
|
1396
|
+
if (!result) continue;
|
|
1397
|
+
if (result.type === "usage") {
|
|
1398
|
+
setUsage(result.usage);
|
|
1399
|
+
} else {
|
|
1400
|
+
setRawLines((prev) => [...prev, trimmed]);
|
|
1401
|
+
currentSpec = applyPatch(currentSpec, result.patch);
|
|
1402
|
+
setSpec({ ...currentSpec });
|
|
1403
|
+
}
|
|
1404
|
+
}
|
|
1405
|
+
}
|
|
1406
|
+
if (buffer.trim()) {
|
|
1407
|
+
const trimmed = buffer.trim();
|
|
1408
|
+
const result = parseLine(trimmed);
|
|
1409
|
+
if (result) {
|
|
1410
|
+
if (result.type === "usage") {
|
|
1411
|
+
setUsage(result.usage);
|
|
1412
|
+
} else {
|
|
1413
|
+
setRawLines((prev) => [...prev, trimmed]);
|
|
1414
|
+
currentSpec = applyPatch(currentSpec, result.patch);
|
|
1415
|
+
setSpec({ ...currentSpec });
|
|
1416
|
+
}
|
|
1417
|
+
}
|
|
1418
|
+
}
|
|
1419
|
+
options.onComplete?.(currentSpec);
|
|
1420
|
+
} catch (err) {
|
|
1421
|
+
if (err.name === "AbortError") {
|
|
1422
|
+
return;
|
|
1423
|
+
}
|
|
1424
|
+
const resolvedError = err instanceof Error ? err : new Error(String(err));
|
|
1425
|
+
setError(resolvedError);
|
|
1426
|
+
options.onError?.(resolvedError);
|
|
1427
|
+
} finally {
|
|
1428
|
+
setIsStreaming(false);
|
|
1429
|
+
}
|
|
1430
|
+
};
|
|
1431
|
+
onCleanup3(() => {
|
|
1432
|
+
abortController?.abort();
|
|
1433
|
+
});
|
|
1434
|
+
return {
|
|
1435
|
+
get spec() {
|
|
1436
|
+
return spec();
|
|
1437
|
+
},
|
|
1438
|
+
get isStreaming() {
|
|
1439
|
+
return isStreaming();
|
|
1440
|
+
},
|
|
1441
|
+
get error() {
|
|
1442
|
+
return error();
|
|
1443
|
+
},
|
|
1444
|
+
get usage() {
|
|
1445
|
+
return usage();
|
|
1446
|
+
},
|
|
1447
|
+
get rawLines() {
|
|
1448
|
+
return rawLines();
|
|
1449
|
+
},
|
|
1450
|
+
send,
|
|
1451
|
+
clear
|
|
1452
|
+
};
|
|
1453
|
+
}
|
|
1454
|
+
function flatToTree(elements) {
|
|
1455
|
+
const elementMap = {};
|
|
1456
|
+
let root = "";
|
|
1457
|
+
for (const element of elements) {
|
|
1458
|
+
elementMap[element.key] = {
|
|
1459
|
+
type: element.type,
|
|
1460
|
+
props: element.props,
|
|
1461
|
+
children: [],
|
|
1462
|
+
visible: element.visible
|
|
1463
|
+
};
|
|
1464
|
+
}
|
|
1465
|
+
for (const element of elements) {
|
|
1466
|
+
if (element.parentKey) {
|
|
1467
|
+
const parent = elementMap[element.parentKey];
|
|
1468
|
+
if (parent) {
|
|
1469
|
+
if (!parent.children) {
|
|
1470
|
+
parent.children = [];
|
|
1471
|
+
}
|
|
1472
|
+
parent.children.push(element.key);
|
|
1473
|
+
}
|
|
1474
|
+
} else {
|
|
1475
|
+
root = element.key;
|
|
1476
|
+
}
|
|
1477
|
+
}
|
|
1478
|
+
return { root, elements: elementMap };
|
|
1479
|
+
}
|
|
1480
|
+
function useBoundProp(propValue, bindingPath) {
|
|
1481
|
+
const { set } = useStateStore();
|
|
1482
|
+
const setValue = (value) => {
|
|
1483
|
+
if (bindingPath) set(bindingPath, value);
|
|
1484
|
+
};
|
|
1485
|
+
return [propValue, setValue];
|
|
1486
|
+
}
|
|
1487
|
+
function isSpecDataPart(data) {
|
|
1488
|
+
if (typeof data !== "object" || data === null) return false;
|
|
1489
|
+
const obj = data;
|
|
1490
|
+
switch (obj.type) {
|
|
1491
|
+
case "patch":
|
|
1492
|
+
return typeof obj.patch === "object" && obj.patch !== null;
|
|
1493
|
+
case "flat":
|
|
1494
|
+
case "nested":
|
|
1495
|
+
return typeof obj.spec === "object" && obj.spec !== null;
|
|
1496
|
+
default:
|
|
1497
|
+
return false;
|
|
1498
|
+
}
|
|
1499
|
+
}
|
|
1500
|
+
function buildSpecFromParts(parts) {
|
|
1501
|
+
const spec = { root: "", elements: {} };
|
|
1502
|
+
let hasSpec = false;
|
|
1503
|
+
for (const part of parts) {
|
|
1504
|
+
if (part.type === SPEC_DATA_PART_TYPE) {
|
|
1505
|
+
if (!isSpecDataPart(part.data)) continue;
|
|
1506
|
+
const payload = part.data;
|
|
1507
|
+
if (payload.type === "patch") {
|
|
1508
|
+
hasSpec = true;
|
|
1509
|
+
applySpecPatch(spec, payload.patch);
|
|
1510
|
+
} else if (payload.type === "flat") {
|
|
1511
|
+
hasSpec = true;
|
|
1512
|
+
Object.assign(spec, payload.spec);
|
|
1513
|
+
} else if (payload.type === "nested") {
|
|
1514
|
+
hasSpec = true;
|
|
1515
|
+
const flat = nestedToFlat(payload.spec);
|
|
1516
|
+
Object.assign(spec, flat);
|
|
1517
|
+
}
|
|
1518
|
+
}
|
|
1519
|
+
}
|
|
1520
|
+
return hasSpec ? spec : null;
|
|
1521
|
+
}
|
|
1522
|
+
function getTextFromParts(parts) {
|
|
1523
|
+
return parts.filter(
|
|
1524
|
+
(p) => p.type === "text" && typeof p.text === "string"
|
|
1525
|
+
).map((p) => p.text.trim()).filter(Boolean).join("\n\n");
|
|
1526
|
+
}
|
|
1527
|
+
function useJsonRenderMessage(getParts) {
|
|
1528
|
+
const result = createMemo4(() => {
|
|
1529
|
+
const parts = getParts();
|
|
1530
|
+
return {
|
|
1531
|
+
spec: buildSpecFromParts(parts),
|
|
1532
|
+
text: getTextFromParts(parts)
|
|
1533
|
+
};
|
|
1534
|
+
});
|
|
1535
|
+
return {
|
|
1536
|
+
get spec() {
|
|
1537
|
+
return result().spec;
|
|
1538
|
+
},
|
|
1539
|
+
get text() {
|
|
1540
|
+
return result().text;
|
|
1541
|
+
},
|
|
1542
|
+
get hasSpec() {
|
|
1543
|
+
const s = result().spec;
|
|
1544
|
+
return s !== null && Object.keys(s.elements || {}).length > 0;
|
|
1545
|
+
}
|
|
1546
|
+
};
|
|
1547
|
+
}
|
|
1548
|
+
var chatMessageIdCounter = 0;
|
|
1549
|
+
function generateChatId() {
|
|
1550
|
+
if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
|
|
1551
|
+
return crypto.randomUUID();
|
|
1552
|
+
}
|
|
1553
|
+
chatMessageIdCounter += 1;
|
|
1554
|
+
return `msg-${Date.now()}-${chatMessageIdCounter}`;
|
|
1555
|
+
}
|
|
1556
|
+
function useChatUI(options) {
|
|
1557
|
+
const [messages, setMessages] = createSignal4([]);
|
|
1558
|
+
const [isStreaming, setIsStreaming] = createSignal4(false);
|
|
1559
|
+
const [error, setError] = createSignal4(null);
|
|
1560
|
+
let abortController = null;
|
|
1561
|
+
const clear = () => {
|
|
1562
|
+
setMessages([]);
|
|
1563
|
+
setError(null);
|
|
1564
|
+
};
|
|
1565
|
+
const send = async (text) => {
|
|
1566
|
+
if (!text.trim()) return;
|
|
1567
|
+
abortController?.abort();
|
|
1568
|
+
abortController = new AbortController();
|
|
1569
|
+
const userMessage = {
|
|
1570
|
+
id: generateChatId(),
|
|
1571
|
+
role: "user",
|
|
1572
|
+
text: text.trim(),
|
|
1573
|
+
spec: null
|
|
1574
|
+
};
|
|
1575
|
+
const assistantId = generateChatId();
|
|
1576
|
+
const assistantMessage = {
|
|
1577
|
+
id: assistantId,
|
|
1578
|
+
role: "assistant",
|
|
1579
|
+
text: "",
|
|
1580
|
+
spec: null
|
|
1581
|
+
};
|
|
1582
|
+
setMessages((prev) => [...prev, userMessage, assistantMessage]);
|
|
1583
|
+
setIsStreaming(true);
|
|
1584
|
+
setError(null);
|
|
1585
|
+
const historyForApi = [
|
|
1586
|
+
...messages().filter((m) => m.id !== userMessage.id && m.id !== assistantId).map((m) => ({
|
|
1587
|
+
role: m.role,
|
|
1588
|
+
content: m.text
|
|
1589
|
+
})),
|
|
1590
|
+
{ role: "user", content: text.trim() }
|
|
1591
|
+
];
|
|
1592
|
+
let accumulatedText = "";
|
|
1593
|
+
let currentSpec = { root: "", elements: {} };
|
|
1594
|
+
let hasSpec = false;
|
|
1595
|
+
try {
|
|
1596
|
+
const response = await fetch(options.api, {
|
|
1597
|
+
method: "POST",
|
|
1598
|
+
headers: { "Content-Type": "application/json" },
|
|
1599
|
+
body: JSON.stringify({ messages: historyForApi }),
|
|
1600
|
+
signal: abortController.signal
|
|
1601
|
+
});
|
|
1602
|
+
if (!response.ok) {
|
|
1603
|
+
let errorMessage = `HTTP error: ${response.status}`;
|
|
1604
|
+
try {
|
|
1605
|
+
const errorData = await response.json();
|
|
1606
|
+
if (errorData.message) {
|
|
1607
|
+
errorMessage = errorData.message;
|
|
1608
|
+
} else if (errorData.error) {
|
|
1609
|
+
errorMessage = errorData.error;
|
|
1610
|
+
}
|
|
1611
|
+
} catch {
|
|
1612
|
+
}
|
|
1613
|
+
throw new Error(errorMessage);
|
|
1614
|
+
}
|
|
1615
|
+
const reader = response.body?.getReader();
|
|
1616
|
+
if (!reader) {
|
|
1617
|
+
throw new Error("No response body");
|
|
1618
|
+
}
|
|
1619
|
+
const decoder = new TextDecoder();
|
|
1620
|
+
const parser = createMixedStreamParser({
|
|
1621
|
+
onPatch(patch) {
|
|
1622
|
+
hasSpec = true;
|
|
1623
|
+
applySpecPatch(currentSpec, patch);
|
|
1624
|
+
setMessages(
|
|
1625
|
+
(prev) => prev.map(
|
|
1626
|
+
(m) => m.id === assistantId ? {
|
|
1627
|
+
...m,
|
|
1628
|
+
spec: {
|
|
1629
|
+
root: currentSpec.root,
|
|
1630
|
+
elements: { ...currentSpec.elements },
|
|
1631
|
+
...currentSpec.state ? { state: { ...currentSpec.state } } : {}
|
|
1632
|
+
}
|
|
1633
|
+
} : m
|
|
1634
|
+
)
|
|
1635
|
+
);
|
|
1636
|
+
},
|
|
1637
|
+
onText(line) {
|
|
1638
|
+
accumulatedText += (accumulatedText ? "\n" : "") + line;
|
|
1639
|
+
setMessages(
|
|
1640
|
+
(prev) => prev.map(
|
|
1641
|
+
(m) => m.id === assistantId ? { ...m, text: accumulatedText } : m
|
|
1642
|
+
)
|
|
1643
|
+
);
|
|
1644
|
+
}
|
|
1645
|
+
});
|
|
1646
|
+
while (true) {
|
|
1647
|
+
const { done, value } = await reader.read();
|
|
1648
|
+
if (done) break;
|
|
1649
|
+
parser.push(decoder.decode(value, { stream: true }));
|
|
1650
|
+
}
|
|
1651
|
+
parser.flush();
|
|
1652
|
+
const finalMessage = {
|
|
1653
|
+
id: assistantId,
|
|
1654
|
+
role: "assistant",
|
|
1655
|
+
text: accumulatedText,
|
|
1656
|
+
spec: hasSpec ? {
|
|
1657
|
+
root: currentSpec.root,
|
|
1658
|
+
elements: { ...currentSpec.elements },
|
|
1659
|
+
...currentSpec.state ? { state: { ...currentSpec.state } } : {}
|
|
1660
|
+
} : null
|
|
1661
|
+
};
|
|
1662
|
+
options.onComplete?.(finalMessage);
|
|
1663
|
+
} catch (err) {
|
|
1664
|
+
if (err.name === "AbortError") {
|
|
1665
|
+
return;
|
|
1666
|
+
}
|
|
1667
|
+
const resolvedError = err instanceof Error ? err : new Error(String(err));
|
|
1668
|
+
setError(resolvedError);
|
|
1669
|
+
setMessages(
|
|
1670
|
+
(prev) => prev.filter((m) => m.id !== assistantId || m.text.length > 0)
|
|
1671
|
+
);
|
|
1672
|
+
options.onError?.(resolvedError);
|
|
1673
|
+
} finally {
|
|
1674
|
+
setIsStreaming(false);
|
|
1675
|
+
}
|
|
1676
|
+
};
|
|
1677
|
+
onCleanup3(() => {
|
|
1678
|
+
abortController?.abort();
|
|
1679
|
+
});
|
|
1680
|
+
return {
|
|
1681
|
+
get messages() {
|
|
1682
|
+
return messages();
|
|
1683
|
+
},
|
|
1684
|
+
get isStreaming() {
|
|
1685
|
+
return isStreaming();
|
|
1686
|
+
},
|
|
1687
|
+
get error() {
|
|
1688
|
+
return error();
|
|
1689
|
+
},
|
|
1690
|
+
send,
|
|
1691
|
+
clear
|
|
1692
|
+
};
|
|
1693
|
+
}
|
|
1694
|
+
export {
|
|
1695
|
+
ActionProvider,
|
|
1696
|
+
ConfirmDialog,
|
|
1697
|
+
JSONUIProvider,
|
|
1698
|
+
Renderer,
|
|
1699
|
+
RepeatScopeProvider,
|
|
1700
|
+
StateProvider,
|
|
1701
|
+
ValidationProvider,
|
|
1702
|
+
VisibilityProvider,
|
|
1703
|
+
buildSpecFromParts,
|
|
1704
|
+
createRenderer,
|
|
1705
|
+
createStateStore2 as createStateStore,
|
|
1706
|
+
defineRegistry,
|
|
1707
|
+
elementTreeSchema,
|
|
1708
|
+
flatToTree,
|
|
1709
|
+
getTextFromParts,
|
|
1710
|
+
schema,
|
|
1711
|
+
useAction,
|
|
1712
|
+
useActions,
|
|
1713
|
+
useBoundProp,
|
|
1714
|
+
useChatUI,
|
|
1715
|
+
useFieldValidation,
|
|
1716
|
+
useIsVisible,
|
|
1717
|
+
useJsonRenderMessage,
|
|
1718
|
+
useOptionalValidation,
|
|
1719
|
+
useRepeatScope,
|
|
1720
|
+
useStateBinding,
|
|
1721
|
+
useStateStore,
|
|
1722
|
+
useStateValue,
|
|
1723
|
+
useUIStream,
|
|
1724
|
+
useValidation,
|
|
1725
|
+
useVisibility
|
|
1726
|
+
};
|
|
1727
|
+
//# sourceMappingURL=index.mjs.map
|