@buoy-gg/jotai 7.0.41 → 7.0.43

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.
Files changed (54) hide show
  1. package/README.md +11 -80
  2. package/lib/commonjs/index.js +1 -112
  3. package/lib/commonjs/jotai/components/JotaiAtomBrowser.js +1 -199
  4. package/lib/commonjs/jotai/components/JotaiAtomChangeItem.js +1 -95
  5. package/lib/commonjs/jotai/components/JotaiAtomDetailContent.js +1 -715
  6. package/lib/commonjs/jotai/components/JotaiAtomValueDetailContent.js +1 -444
  7. package/lib/commonjs/jotai/components/JotaiEventFilterView.js +1 -43
  8. package/lib/commonjs/jotai/components/JotaiIcon.js +1 -18
  9. package/lib/commonjs/jotai/components/JotaiModal.js +1 -743
  10. package/lib/commonjs/jotai/hooks/useJotaiAtomChanges.js +1 -69
  11. package/lib/commonjs/jotai/sync/jotaiSyncAdapter.js +1 -306
  12. package/lib/commonjs/jotai/types/index.js +1 -1
  13. package/lib/commonjs/jotai/utils/jotaiStateStore.js +1 -321
  14. package/lib/commonjs/jotai/utils/snapshotProvider.js +1 -215
  15. package/lib/commonjs/jotai/utils/testIDs.js +1 -90
  16. package/lib/commonjs/jotai/utils/watchAtoms.js +1 -143
  17. package/lib/commonjs/jotai/utils/writeSemantics.js +1 -47
  18. package/lib/commonjs/preset.js +1 -90
  19. package/lib/module/index.js +1 -85
  20. package/lib/module/jotai/components/JotaiAtomBrowser.js +1 -195
  21. package/lib/module/jotai/components/JotaiAtomChangeItem.js +1 -91
  22. package/lib/module/jotai/components/JotaiAtomDetailContent.js +1 -710
  23. package/lib/module/jotai/components/JotaiAtomValueDetailContent.js +1 -442
  24. package/lib/module/jotai/components/JotaiEventFilterView.js +1 -39
  25. package/lib/module/jotai/components/JotaiIcon.js +1 -11
  26. package/lib/module/jotai/components/JotaiModal.js +1 -738
  27. package/lib/module/jotai/hooks/useJotaiAtomChanges.js +1 -65
  28. package/lib/module/jotai/sync/jotaiSyncAdapter.js +1 -303
  29. package/lib/module/jotai/types/index.js +1 -1
  30. package/lib/module/jotai/utils/jotaiStateStore.js +1 -317
  31. package/lib/module/jotai/utils/snapshotProvider.js +1 -214
  32. package/lib/module/jotai/utils/testIDs.js +1 -82
  33. package/lib/module/jotai/utils/watchAtoms.js +1 -138
  34. package/lib/module/jotai/utils/writeSemantics.js +1 -43
  35. package/lib/module/preset.js +1 -87
  36. package/lib/web/index.mjs +2 -3
  37. package/package.json +4 -7
  38. package/lib/typescript/index.d.ts.map +0 -1
  39. package/lib/typescript/jotai/components/JotaiAtomBrowser.d.ts.map +0 -1
  40. package/lib/typescript/jotai/components/JotaiAtomChangeItem.d.ts.map +0 -1
  41. package/lib/typescript/jotai/components/JotaiAtomDetailContent.d.ts.map +0 -1
  42. package/lib/typescript/jotai/components/JotaiAtomValueDetailContent.d.ts.map +0 -1
  43. package/lib/typescript/jotai/components/JotaiEventFilterView.d.ts.map +0 -1
  44. package/lib/typescript/jotai/components/JotaiIcon.d.ts.map +0 -1
  45. package/lib/typescript/jotai/components/JotaiModal.d.ts.map +0 -1
  46. package/lib/typescript/jotai/hooks/useJotaiAtomChanges.d.ts.map +0 -1
  47. package/lib/typescript/jotai/sync/jotaiSyncAdapter.d.ts.map +0 -1
  48. package/lib/typescript/jotai/types/index.d.ts.map +0 -1
  49. package/lib/typescript/jotai/utils/jotaiStateStore.d.ts.map +0 -1
  50. package/lib/typescript/jotai/utils/snapshotProvider.d.ts.map +0 -1
  51. package/lib/typescript/jotai/utils/testIDs.d.ts.map +0 -1
  52. package/lib/typescript/jotai/utils/watchAtoms.d.ts.map +0 -1
  53. package/lib/typescript/jotai/utils/writeSemantics.d.ts.map +0 -1
  54. package/lib/typescript/preset.d.ts.map +0 -1
@@ -1,65 +1 @@
1
- "use strict";
2
-
3
- /**
4
- * Hook for consuming Jotai atom changes from the store
5
- *
6
- * The subscribe / filter / pause / clear plumbing is shared `useChangeLog`;
7
- * this file owns the jotai-specific filter, stats and atom registry.
8
- */
9
-
10
- import { useCallback, useMemo } from "react";
11
- import { useChangeLog, useSubscribedValue } from "@buoy-gg/shared-ui";
12
- import { jotaiStateStore } from "../utils/jotaiStateStore";
13
- function filterChanges(changes, filter) {
14
- let filtered = changes;
15
- if (filter.searchText) {
16
- const search = filter.searchText.toLowerCase();
17
- filtered = filtered.filter(c => c.atomLabel.toLowerCase().includes(search) || c.valuePreview.toLowerCase().includes(search) || c.changedKeys.some(k => k.toLowerCase().includes(search)));
18
- }
19
- if (filter.atomLabels && filter.atomLabels.length > 0) {
20
- filtered = filtered.filter(c => filter.atomLabels.includes(c.atomLabel));
21
- }
22
- if (filter.onlyWithChanges) {
23
- filtered = filtered.filter(c => c.hasValueChange);
24
- }
25
- return filtered;
26
- }
27
- const readAtoms = () => jotaiStateStore.getAtoms();
28
- const subscribeToAtoms = listener => jotaiStateStore.subscribeToAtoms(listener);
29
- export function useJotaiAtomChanges() {
30
- const log = useChangeLog(jotaiStateStore, {
31
- filterItems: filterChanges
32
- });
33
- const atoms = useSubscribedValue(readAtoms, subscribeToAtoms);
34
- const stats = useMemo(() => {
35
- const total = log.items.length;
36
- const withChanges = log.items.filter(c => c.hasValueChange).length;
37
- return {
38
- totalChanges: total,
39
- changesWithValueChange: withChanges,
40
- changesWithoutValueChange: total - withChanges,
41
- atomCount: atoms.length
42
- };
43
- }, [log.items, atoms]);
44
- const atomLabels = useMemo(() => {
45
- return jotaiStateStore.getUniqueAtomLabels();
46
- }, [atoms]);
47
- const getChangeById = useCallback(id => {
48
- return jotaiStateStore.getAtomChangeById(id);
49
- }, []);
50
- return {
51
- atomChanges: log.items,
52
- filteredChanges: log.filteredItems,
53
- filter: log.filter,
54
- setFilter: log.setFilter,
55
- stats,
56
- atoms,
57
- // Not the shared log.clear: the store also owns each atom's change
58
- // count, and only clearAtomChanges resets those with the list.
59
- clearChanges: jotaiStateStore.clearAtomChanges.bind(jotaiStateStore),
60
- isEnabled: log.isEnabled,
61
- toggleCapture: log.toggleCapture,
62
- atomLabels,
63
- getChangeById
64
- };
65
- }
1
+ "use strict";import{useCallback as h,useMemo as i}from"react";import{useChangeLog as m,useSubscribedValue as u}from"@buoy-gg/shared-ui";import{jotaiStateStore as o}from"../utils/jotaiStateStore";function c(e,t){let s=e;if(t.searchText){const a=t.searchText.toLowerCase();s=s.filter(r=>r.atomLabel.toLowerCase().includes(a)||r.valuePreview.toLowerCase().includes(a)||r.changedKeys.some(n=>n.toLowerCase().includes(a)))}if(t.atomLabels&&t.atomLabels.length>0){s=s.filter(a=>t.atomLabels.includes(a.atomLabel))}if(t.onlyWithChanges){s=s.filter(a=>a.hasValueChange)}return s}const C=()=>o.getAtoms();const b=e=>o.subscribeToAtoms(e);export function useJotaiAtomChanges(){const e=m(o,{filterItems:c});const t=u(C,b);const s=i(()=>{const n=e.items.length;const l=e.items.filter(g=>g.hasValueChange).length;return{totalChanges:n,changesWithValueChange:l,changesWithoutValueChange:n-l,atomCount:t.length}},[e.items,t]);const a=i(()=>{return o.getUniqueAtomLabels()},[t]);const r=h(n=>{return o.getAtomChangeById(n)},[]);return{atomChanges:e.items,filteredChanges:e.filteredItems,filter:e.filter,setFilter:e.setFilter,stats:s,atoms:t,clearChanges:o.clearAtomChanges.bind(o),isEnabled:e.isEnabled,toggleCapture:e.toggleCapture,atomLabels:a,getChangeById:r}}
@@ -1,303 +1 @@
1
- "use strict";
2
-
3
- import { jotaiStateStore } from "../utils/jotaiStateStore";
4
- import { WIRE_DETAIL_LIMIT_BYTES, createSyncAdapter, capDetail, detailByIdAction, expandKeyedArrays, memoByIdentity, onDeviceMarker, patchFromPath, readParams, readPath, refusalText, shapeSummary, suggestedPatch, toWire, typedEditViolations, unshapedLists } from "@buoy-gg/shared-ui";
5
-
6
- /**
7
- * Sentinel used in WIRE-FORM snapshots in place of raw prevValue/nextValue.
8
- * Same hazard as redux/zustand: up to 200 changes × 2 values on every
9
- * snapshot. Dashboards fetch values via `getChangeDetail`.
10
- */
11
- export const VALUE_ON_DEVICE = onDeviceMarker("Value", "Atom values stay on the device — fetched on demand via getChangeDetail / getAtomValue.");
12
- const isPlainObj = v => !!v && typeof v === "object" && !Array.isArray(v);
13
-
14
- /**
15
- * Merge a patch into an atom's value.
16
- *
17
- * An atom is not always an object — plenty hold a number, a string or a list —
18
- * so a merge only means anything when BOTH sides are plain objects. Anything
19
- * else replaces, which is what a caller asking to merge a scalar can only have
20
- * meant. Arrays replace too: the id-addressed form has already been expanded to
21
- * a whole array by the time this runs, so merging element-wise here would undo
22
- * the deletion half of that expansion.
23
- */
24
- function deepMergeInto(base, patch) {
25
- if (!isPlainObj(base) || !isPlainObj(patch)) return patch;
26
- const out = {
27
- ...base
28
- };
29
- for (const [k, v] of Object.entries(patch)) out[k] = k in base ? deepMergeInto(base[k], v) : v;
30
- return out;
31
- }
32
-
33
- /**
34
- * Does this patch write into the list at `path`? Only then is an
35
- * unchecked-shape warning about that list worth saying.
36
- */
37
- function touchesList(patch, path) {
38
- let cur = patch;
39
- for (const seg of path.split(".")) {
40
- if (!isPlainObj(cur)) return false;
41
- if (!(seg in cur)) return false;
42
- cur = cur[seg];
43
- }
44
- return Array.isArray(cur) ? cur.length > 0 : !!cur && typeof cur === "object";
45
- }
46
-
47
- /**
48
- * Per-change wire cache — the store prepends and never mutates a recorded
49
- * change, so identity is a correct, self-invalidating key.
50
- */
51
- const toWireChange = memoByIdentity(change => ({
52
- ...change,
53
- prevValue: VALUE_ON_DEVICE,
54
- nextValue: VALUE_ON_DEVICE
55
- }));
56
- const toWireValue = value => toWire(value, VALUE_ON_DEVICE);
57
-
58
- /**
59
- * Sync adapter for the jotai tool, consumed by @buoy-gg/external-sync's
60
- * `useExternalSync`.
61
- *
62
- * Atom changes are captured by watchAtoms(), independent of whether a
63
- * dashboard is watching — subscribing here only streams what the store
64
- * already records. The snapshot carries both the change timeline and the
65
- * atom registry (with each atom's current value) so the dashboard can render
66
- * the atom browser.
67
- *
68
- * v2: change-log prevValue/nextValue are replaced with on-device markers.
69
- * Dashboards fetch values via `getChangeDetail`. Oversized / unserializable
70
- * `atoms[].currentValue` uses the same marker and is fetched via `getAtomValue`.
71
- */
72
- export const jotaiSyncAdapter = createSyncAdapter({
73
- version: 2,
74
- getSnapshot: () => ({
75
- changes: jotaiStateStore.getAtomChanges().map(toWireChange),
76
- atoms: jotaiStateStore.getAtomSnapshots().map(atom => ({
77
- ...atom,
78
- currentValue: toWireValue(atom.currentValue)
79
- }))
80
- }),
81
- subscribe: [onChange => jotaiStateStore.subscribe(onChange), onChange => jotaiStateStore.subscribeToAtoms(onChange)],
82
- actions: {
83
- clearEvents: () => {
84
- jotaiStateStore.clearAtomChanges();
85
- },
86
- /**
87
- * On-demand detail: one change's real prevValue/nextValue.
88
- * The per-snapshot stream stays value-free; this is the explicit,
89
- * size-guarded channel for the detail pane.
90
- */
91
- getChangeDetail: detailByIdAction(id => jotaiStateStore.getAtomChangeById(id), (change, cap) => ({
92
- prevValue: cap(change.prevValue),
93
- nextValue: cap(change.nextValue)
94
- })),
95
- /**
96
- * On-demand current value for one atom. Wire-form snapshots replace
97
- * oversized / unserializable `currentValue` with VALUE_ON_DEVICE.
98
- */
99
- getAtomValue: params => {
100
- const {
101
- label,
102
- path
103
- } = readParams(params);
104
- if (!label) return {
105
- found: false,
106
- reason: "missing label"
107
- };
108
- const atom = jotaiStateStore.getAtom(label);
109
- if (!atom) return {
110
- found: false,
111
- reason: "unknown label"
112
- };
113
- let currentValue;
114
- try {
115
- currentValue = atom.getValue();
116
- } catch {
117
- currentValue = undefined;
118
- }
119
- try {
120
- JSON.stringify(currentValue);
121
- } catch {
122
- return {
123
- found: true,
124
- label,
125
- currentValue: VALUE_ON_DEVICE
126
- };
127
- }
128
- // One slice instead of the whole atom, when asked. The reason to want it
129
- // is the engine's 24,000-character result cap: a caller that never saw the
130
- // field it means to edit is left inventing a shape.
131
- if (path) {
132
- const at = readPath(currentValue, path);
133
- if (!at.ok) return {
134
- found: false,
135
- reason: at.error
136
- };
137
- return {
138
- found: true,
139
- label,
140
- path,
141
- shape: shapeSummary(at.value),
142
- currentValue: at.value
143
- };
144
- }
145
- // `shape` BEFORE `currentValue`: a result is truncated by slicing the
146
- // encoded string, so what is serialised first is what survives the cut,
147
- // and the sketch does not grow with the data.
148
- return {
149
- found: true,
150
- label,
151
- ...(shapeSummary(currentValue) ? {
152
- shape: shapeSummary(currentValue)
153
- } : {}),
154
- currentValue: capDetail(currentValue, WIRE_DETAIL_LIMIT_BYTES)
155
- };
156
- },
157
- /**
158
- * Compact current-value reader for a remote driver (MCP/LLM): registered
159
- * atoms with light metadata; each atom's `currentValue` stays on the device
160
- * unless `includeValues` is set. `writable` tells the caller which atoms
161
- * can be set via `setAtom`.
162
- */
163
- listAtoms: params => {
164
- const {
165
- includeValues = false,
166
- limit
167
- } = readParams(params);
168
- const all = jotaiStateStore.getAtoms();
169
- const capped = typeof limit === "number" && limit > 0 ? all.slice(0, limit) : all;
170
- const atoms = capped.map(a => {
171
- let value;
172
- if (includeValues) {
173
- try {
174
- value = a.getValue();
175
- } catch {
176
- value = undefined;
177
- }
178
- }
179
- // The compact view is what a caller reaches for precisely because it
180
- // does not want the data; the item shape of a list is the one thing it
181
- // cannot infer without it, and it costs a line.
182
- let shape;
183
- try {
184
- shape = shapeSummary(a.getValue());
185
- } catch {
186
- shape = undefined;
187
- }
188
- return {
189
- label: a.label,
190
- changes: a.changeCount,
191
- writable: typeof a.setValue === "function",
192
- ...(shape ? {
193
- shape
194
- } : {}),
195
- ...(includeValues ? {
196
- currentValue: value
197
- } : {})
198
- };
199
- });
200
- return {
201
- atoms,
202
- total: all.length,
203
- returned: atoms.length,
204
- includedValues: includeValues
205
- };
206
- },
207
- /**
208
- * Write to a writable atom (throws for unknown/read-only atoms).
209
- *
210
- * THIS USED TO BE FOUR LINES WITH NO GUARD. `setAtom` took whatever it was
211
- * given and wrote it: a string over a number, `null` over a list the screen
212
- * maps, a one-item array over a fifty-item one — every failure mode the
213
- * zustand and react-query adapters refuse, jotai accepted silently. It was
214
- * the only writable state tool with no protection at all, and it had never
215
- * had an eval case, so nothing had ever measured it.
216
- *
217
- * It now takes the same three forms as `zustand.setState`, backed by the
218
- * same shared implementations:
219
- * { value } replace (guarded unless force)
220
- * { value, merge: true } merge a patch, id-addressed lists allowed
221
- * { path, value } set one value; no depth to get wrong
222
- */
223
- setAtom: params => {
224
- const {
225
- label,
226
- value,
227
- merge,
228
- path,
229
- force
230
- } = readParams(params);
231
- if (!label) throw new Error("setAtom requires a `label`");
232
- const atom = jotaiStateStore.getAtom(label);
233
- let current;
234
- try {
235
- current = atom?.getValue();
236
- } catch {
237
- current = undefined;
238
- }
239
-
240
- // A replace with no guard is the old behaviour; keep it reachable via
241
- // `force`, and keep it the ONLY way to get it.
242
- let effective = value;
243
- let keyedNotes = [];
244
- let unchecked = [];
245
- const merging = merge === true || path !== undefined;
246
- if (path !== undefined) {
247
- if (value === undefined) {
248
- return {
249
- ok: false,
250
- error: `Refused: sent \`path\` without \`value\`. Say what to set "${path}" to.`
251
- };
252
- }
253
- const built = patchFromPath(current, path, value);
254
- if (!built.ok) return {
255
- ok: false,
256
- error: `Refused: ${built.error}`
257
- };
258
- effective = built.patch;
259
- }
260
- if (!force && merging && current !== undefined) {
261
- const expanded = expandKeyedArrays(current, effective);
262
- // See keyedArrays.ts: an added item whose id disagrees with the key it
263
- // was sent under is ambiguous, and only this layer can say so.
264
- if (expanded.error) {
265
- return {
266
- ok: false,
267
- error: `Refused: ${expanded.error}`
268
- };
269
- }
270
- effective = expanded.patch;
271
- keyedNotes = expanded.notes;
272
- unchecked = unshapedLists(current).filter(p => touchesList(effective, p));
273
- const violations = typedEditViolations(current, effective, {
274
- noun: "atom"
275
- });
276
- if (violations.length) {
277
- const suggestion = suggestedPatch(effective, violations);
278
- return {
279
- ok: false,
280
- error: refusalText(violations, "atom", suggestion),
281
- violations,
282
- ...(suggestion ? {
283
- suggestedValue: suggestion
284
- } : {})
285
- };
286
- }
287
- }
288
- jotaiStateStore.setAtomValue(label, merging ? deepMergeInto(current, effective) : effective);
289
- return {
290
- ok: true,
291
- ...(path !== undefined ? {
292
- path
293
- } : {}),
294
- ...(keyedNotes.length ? {
295
- keyed: keyedNotes
296
- } : {}),
297
- ...(unchecked.length ? {
298
- unchecked: unchecked.map(p => `\`${p}\` was empty, so nothing knows what its items look like and the shape of what you added was NOT checked.`)
299
- } : {})
300
- };
301
- }
302
- }
303
- });
1
+ "use strict";import{jotaiStateStore as l}from"../utils/jotaiStateStore";import{WIRE_DETAIL_LIMIT_BYTES as k,createSyncAdapter as b,capDetail as v,detailByIdAction as w,expandKeyedArrays as S,memoByIdentity as E,onDeviceMarker as I,patchFromPath as x,readParams as p,readPath as C,refusalText as j,shapeSummary as h,suggestedPatch as D,toWire as T,typedEditViolations as L,unshapedLists as O}from"@buoy-gg/shared-ui";export const VALUE_ON_DEVICE=I("Value","Atom values stay on the device \u2014 fetched on demand via getChangeDetail / getAtomValue.");const y=e=>!!e&&typeof e==="object"&&!Array.isArray(e);function A(e,r){if(!y(e)||!y(r))return r;const t={...e};for(const[a,n]of Object.entries(r))t[a]=a in e?A(e[a],n):n;return t}function P(e,r){let t=e;for(const a of r.split(".")){if(!y(t))return false;if(!(a in t))return false;t=t[a]}return Array.isArray(t)?t.length>0:!!t&&typeof t==="object"}const _=E(e=>({...e,prevValue:VALUE_ON_DEVICE,nextValue:VALUE_ON_DEVICE}));const B=e=>T(e,VALUE_ON_DEVICE);export const jotaiSyncAdapter=b({version:2,getSnapshot:()=>({changes:l.getAtomChanges().map(_),atoms:l.getAtomSnapshots().map(e=>({...e,currentValue:B(e.currentValue)}))}),subscribe:[e=>l.subscribe(e),e=>l.subscribeToAtoms(e)],actions:{clearEvents:()=>{l.clearAtomChanges()},getChangeDetail:w(e=>l.getAtomChangeById(e),(e,r)=>({prevValue:r(e.prevValue),nextValue:r(e.nextValue)})),getAtomValue:e=>{const{label:r,path:t}=p(e);if(!r)return{found:false,reason:"missing label"};const a=l.getAtom(r);if(!a)return{found:false,reason:"unknown label"};let n;try{n=a.getValue()}catch{n=void 0}try{JSON.stringify(n)}catch{return{found:true,label:r,currentValue:VALUE_ON_DEVICE}}if(t){const i=C(n,t);if(!i.ok)return{found:false,reason:i.error};return{found:true,label:r,path:t,shape:h(i.value),currentValue:i.value}}return{found:true,label:r,...h(n)?{shape:h(n)}:{},currentValue:v(n,k)}},listAtoms:e=>{const{includeValues:r=false,limit:t}=p(e);const a=l.getAtoms();const n=typeof t==="number"&&t>0?a.slice(0,t):a;const i=n.map(c=>{let u;if(r){try{u=c.getValue()}catch{u=void 0}}let o;try{o=h(c.getValue())}catch{o=void 0}return{label:c.label,changes:c.changeCount,writable:typeof c.setValue==="function",...o?{shape:o}:{},...r?{currentValue:u}:{}}});return{atoms:i,total:a.length,returned:i.length,includedValues:r}},setAtom:e=>{const{label:r,value:t,merge:a,path:n,force:i}=p(e);if(!r)throw new Error("setAtom requires a `label`");const c=l.getAtom(r);let u;try{u=c?.getValue()}catch{u=void 0}let o=t;let g=[];let m=[];const V=a===true||n!==void 0;if(n!==void 0){if(t===void 0){return{ok:false,error:`Refused: sent \`path\` without \`value\`. Say what to set "${n}" to.`}}const s=x(u,n,t);if(!s.ok)return{ok:false,error:`Refused: ${s.error}`};o=s.patch}if(!i&&V&&u!==void 0){const s=S(u,o);if(s.error){return{ok:false,error:`Refused: ${s.error}`}}o=s.patch;g=s.notes;m=O(u).filter(f=>P(o,f));const d=L(u,o,{noun:"atom"});if(d.length){const f=D(o,d);return{ok:false,error:j(d,"atom",f),violations:d,...f?{suggestedValue:f}:{}}}}l.setAtomValue(r,V?A(u,o):o);return{ok:true,...n!==void 0?{path:n}:{},...g.length?{keyed:g}:{},...m.length?{unchecked:m.map(s=>`\`${s}\` was empty, so nothing knows what its items look like and the shape of what you added was NOT checked.`)}:{}}}}});
@@ -1 +1 @@
1
- "use strict";
1
+ "use strict";