@bfrs/agentic-components 0.8.3 → 0.8.5

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.
@@ -1,34 +1,56 @@
1
- import { createSaveView as H, DEFAULT_SAVE_ENDPOINT as Q, deleteView as Y, updateView as Z, isUnauthorizedViewApiError as N, resolveApiToken as tt, listViews as et } from "./saveViewApi.js";
2
- import { viewCacheKey as it, invalidateViewCache as A, readViewCache as z, evictViewCacheMemory as rt, writeViewCache as nt, fingerprintToken as st } from "./viewCache.js";
3
- const ot = "bfrs.savedViews.", C = 1, P = (t) => `${ot}${encodeURIComponent(t)}`;
4
- function O() {
1
+ import { createSaveView as R, DEFAULT_SAVE_ENDPOINT as Z, deleteView as tt, updateView as et, isUnauthorizedViewApiError as nt, resolveApiToken as it, listViews as rt } from "./saveViewApi.js";
2
+ import { viewCacheKey as st, invalidateViewCache as J, readViewCache as z, evictViewCacheMemory as ot, writeViewCache as at, fingerprintToken as ct } from "./viewCache.js";
3
+ const j = "bfrs.savedViews.", F = 1, dt = "ngStorage-USER", b = "anon";
4
+ function U() {
5
5
  try {
6
6
  return typeof window < "u" && !!window.localStorage;
7
7
  } catch {
8
8
  return !1;
9
9
  }
10
10
  }
11
- function J(t) {
12
- const s = { version: C, views: [] };
13
- if (!O()) return s;
11
+ function ft() {
12
+ if (!U()) return { userId: b, companyId: b };
14
13
  try {
15
- const d = window.localStorage.getItem(P(t));
16
- if (!d) return s;
17
- const f = JSON.parse(d);
18
- return !f || !Array.isArray(f.views) ? s : { version: C, views: f.views, fingerprint: f.fingerprint };
14
+ const t = window.localStorage.getItem(dt), r = t ? JSON.parse(t) : null, a = (f) => (f == null ? "" : String(f).trim()) || b;
15
+ return { userId: a(r == null ? void 0 : r.id), companyId: a(r == null ? void 0 : r.company_id) };
19
16
  } catch {
20
- return s;
17
+ return { userId: b, companyId: b };
21
18
  }
22
19
  }
23
- function L(t, s, d) {
24
- if (O())
20
+ const P = (t) => {
21
+ const { userId: r, companyId: a } = ft();
22
+ return `${j}${encodeURIComponent(a)}.${encodeURIComponent(r)}.${encodeURIComponent(t)}`;
23
+ }, ut = (t) => `${j}${encodeURIComponent(t)}`;
24
+ function D(t) {
25
+ if (!t) return null;
26
+ try {
27
+ const r = JSON.parse(t);
28
+ return !r || !Array.isArray(r.views) ? null : { version: F, views: r.views, fingerprint: r.fingerprint };
29
+ } catch {
30
+ return null;
31
+ }
32
+ }
33
+ function k(t) {
34
+ const r = { version: F, views: [] };
35
+ if (!U()) return r;
36
+ try {
37
+ const a = D(window.localStorage.getItem(P(t)));
38
+ if (a) return a;
39
+ const f = ut(t), u = D(window.localStorage.getItem(f));
40
+ return u ? (C(t, u.views, u.fingerprint), window.localStorage.removeItem(f), u) : r;
41
+ } catch {
42
+ return r;
43
+ }
44
+ }
45
+ function C(t, r, a) {
46
+ if (U())
25
47
  try {
26
- const f = { version: C, views: s, ...d ? { fingerprint: d } : {} };
48
+ const f = { version: F, views: r, ...a ? { fingerprint: a } : {} };
27
49
  window.localStorage.setItem(P(t), JSON.stringify(f));
28
50
  } catch {
29
51
  }
30
52
  }
31
- function K() {
53
+ function q() {
32
54
  try {
33
55
  const t = globalThis.crypto;
34
56
  if (t && typeof t.randomUUID == "function")
@@ -37,117 +59,117 @@ function K() {
37
59
  }
38
60
  return `view_${Math.random().toString(36).slice(2)}${Math.random().toString(36).slice(2)}`;
39
61
  }
40
- function x(t, s) {
62
+ function $(t, r) {
41
63
  return {
42
- ...s,
64
+ ...r,
43
65
  ...t,
44
- view_type: s.view_type,
45
- tab: (t == null ? void 0 : t.tab) ?? s.tab,
46
- columns: (t == null ? void 0 : t.columns) ?? s.columns,
47
- filters: (t == null ? void 0 : t.filters) ?? s.filters,
48
- priority: (t == null ? void 0 : t.priority) ?? s.priority,
49
- is_default: !!(t != null && t.is_default || s.is_default),
50
- is_system_defined: (t == null ? void 0 : t.is_system_defined) ?? s.is_system_defined
66
+ view_type: r.view_type,
67
+ tab: (t == null ? void 0 : t.tab) ?? r.tab,
68
+ columns: (t == null ? void 0 : t.columns) ?? r.columns,
69
+ filters: (t == null ? void 0 : t.filters) ?? r.filters,
70
+ priority: (t == null ? void 0 : t.priority) ?? r.priority,
71
+ is_default: !!(t != null && t.is_default || r.is_default),
72
+ is_system_defined: (t == null ? void 0 : t.is_system_defined) ?? r.is_system_defined
51
73
  };
52
74
  }
53
- function at(t, s) {
54
- return JSON.stringify(t) === JSON.stringify(s);
75
+ function lt(t, r) {
76
+ return JSON.stringify(t) === JSON.stringify(r);
55
77
  }
56
- const ct = /* @__PURE__ */ new Set(["default"]);
57
- function dt(t) {
58
- return ct.has(t.trim().toLowerCase());
78
+ const yt = /* @__PURE__ */ new Set(["default"]);
79
+ function wt(t) {
80
+ return yt.has(t.trim().toLowerCase());
59
81
  }
60
- function D(t) {
61
- if (dt(t))
82
+ function G(t) {
83
+ if (wt(t))
62
84
  throw new Error(`"${t.trim()}" is a reserved view name. Choose a different name.`);
63
85
  }
64
- function ft(t) {
86
+ function pt(t) {
65
87
  return !!(t.saveEndpoint || t.endpoint);
66
88
  }
67
- function k(t) {
89
+ function W(t) {
68
90
  return !!(t != null && t.viewType && t.tab);
69
91
  }
70
- const m = /* @__PURE__ */ new Map();
71
- let q = !1;
72
- const $ = /* @__PURE__ */ new Map();
73
- function ut(t) {
74
- var s;
75
- if (rt(t.key), t.key === null) {
76
- m.forEach((d) => d.forEach((f) => f()));
92
+ const h = /* @__PURE__ */ new Map();
93
+ let X = !1;
94
+ const T = /* @__PURE__ */ new Map();
95
+ function _t(t) {
96
+ var r;
97
+ if (ot(t.key), t.key === null) {
98
+ h.forEach((a) => a.forEach((f) => f()));
77
99
  return;
78
100
  }
79
- (s = m.get(t.key)) == null || s.forEach((d) => d());
101
+ (r = h.get(t.key)) == null || r.forEach((a) => a());
80
102
  }
81
- function lt() {
82
- q || typeof window > "u" || (window.addEventListener("storage", ut), q = !0);
103
+ function mt() {
104
+ X || typeof window > "u" || (window.addEventListener("storage", _t), X = !0);
83
105
  }
84
- function W(t) {
85
- var s;
86
- (s = m.get(t)) == null || s.forEach((d) => d());
106
+ function Y(t) {
107
+ var r;
108
+ (r = h.get(t)) == null || r.forEach((a) => a());
87
109
  }
88
- function wt(t, s = {}) {
89
- return R(t, s, !1);
110
+ function gt(t, r = {}) {
111
+ return H(t, r, !1);
90
112
  }
91
- function pt(t, s = {}) {
92
- return R(t, s, !0);
113
+ function Et(t, r = {}) {
114
+ return H(t, r, !0);
93
115
  }
94
- function R(t, s = {}, d) {
95
- const f = P(t), l = s.saveApi, o = d && k(l) ? l : void 0, U = s.remoteSave ?? (d && l && (ft(l) || k(l)) ? H({ ...l, saveEndpoint: l.saveEndpoint ?? l.endpoint ?? Q }) : void 0), T = o ? () => et(o) : void 0, S = o ? (i) => Z({ ...o, payload: i }) : void 0, b = o ? (i) => Y({ ...o, viewId: i }) : void 0, p = o ? { viewType: o.viewType, tab: o.tab } : void 0, u = p ? it(p) : void 0, h = () => st(o ? tt(o) : ""), _ = () => {
96
- const i = J(t);
97
- return !o || i.fingerprint === void 0 || i.fingerprint === h() ? i.views : (L(t, [], h()), []);
98
- }, X = () => {
116
+ function H(t, r = {}, a) {
117
+ const f = () => P(t), u = r.saveApi, o = a && W(u) ? u : void 0, A = r.remoteSave ?? (a && u && (pt(u) || W(u)) ? R({ ...u, saveEndpoint: u.saveEndpoint ?? u.endpoint ?? Z }) : void 0), L = o ? () => rt(o) : void 0, v = o ? (n) => et({ ...o, payload: n }) : void 0, x = o ? (n) => tt({ ...o, viewId: n }) : void 0, m = o ? { viewType: o.viewType, tab: o.tab } : void 0, l = m ? st(m) : void 0, g = () => ct(o ? it(o) : ""), _ = () => {
118
+ const n = k(t);
119
+ return !o || n.fingerprint === void 0 || n.fingerprint === g() ? n.views : (C(t, [], g()), []);
120
+ }, N = () => {
99
121
  if (!o) return;
100
- const i = J(t);
101
- i.fingerprint !== void 0 && i.fingerprint !== h() && L(t, [], h());
102
- }, v = (i, r = !0) => {
103
- L(t, i, o ? h() : void 0), r && W(f);
104
- }, j = (i) => (
122
+ const n = k(t);
123
+ n.fingerprint !== void 0 && n.fingerprint !== g() && C(t, [], g());
124
+ }, E = (n, i = !0) => {
125
+ C(t, n, o ? g() : void 0), i && Y(f());
126
+ }, Q = (n) => (
105
127
  // No forced primary here: absence of a backend-marked default is a valid,
106
128
  // stable state (the selector's "Default"/no-view state), not something to
107
129
  // paper over by promoting whichever view happens to come first.
108
- i.map(
109
- (r, e) => x(r, {
110
- view_id: r.view_id || K(),
111
- name: r.name || `View ${e + 1}`,
130
+ n.map(
131
+ (i, e) => $(i, {
132
+ view_id: i.view_id || q(),
133
+ name: i.name || `View ${e + 1}`,
112
134
  view_type: t,
113
135
  tab: o == null ? void 0 : o.tab,
114
- columns: r.columns ?? [],
115
- filters: r.filters ?? [],
116
- priority: r.priority ?? e,
117
- is_default: !!r.is_default,
118
- is_system_defined: !!r.is_system_defined
136
+ columns: i.columns ?? [],
137
+ filters: i.filters ?? [],
138
+ priority: i.priority ?? e,
139
+ is_default: !!i.is_default,
140
+ is_system_defined: !!i.is_system_defined
119
141
  })
120
142
  )
121
- ), F = (i, r = !1) => {
122
- const e = _(), n = j(i);
123
- return at(e, n) || v(n, r), n;
124
- }, B = () => {
125
- if (!T || !u || !p) return Promise.resolve(_());
126
- const i = $.get(u);
127
- if (i) return i;
128
- const r = h(), e = T().then((n) => {
129
- const a = F(n, !0);
130
- return nt(u, { scope: p, fingerprint: r, views: a }), a;
131
- }).catch((n) => {
132
- if (N(n))
133
- throw A(u), n;
134
- const a = z(u, p, r);
135
- return a.status === "miss" ? _() : a.views;
143
+ ), K = (n, i = !1) => {
144
+ const e = _(), s = Q(n);
145
+ return lt(e, s) || E(s, i), s;
146
+ }, M = () => {
147
+ if (!L || !l || !m) return Promise.resolve(_());
148
+ const n = T.get(l);
149
+ if (n) return n;
150
+ const i = g(), e = L().then((s) => {
151
+ const c = K(s, !0);
152
+ return at(l, { scope: m, fingerprint: i, views: c }), c;
153
+ }).catch((s) => {
154
+ if (nt(s))
155
+ throw J(l), s;
156
+ const c = z(l, m, i);
157
+ return c.status === "miss" ? _() : c.views;
136
158
  }).finally(() => {
137
- $.get(u) === e && $.delete(u);
159
+ T.get(l) === e && T.delete(l);
138
160
  });
139
- return $.set(u, e), e;
140
- }, I = () => {
141
- if (X(), !T || !u || !p) return Promise.resolve(_());
142
- const i = z(u, p, h());
143
- return i.status === "hit" ? Promise.resolve(F(i.views, !1)) : B();
144
- }, g = () => {
145
- u && A(u);
146
- }, M = async (i, r) => {
147
- const e = (await I()).find((c) => c.view_id === i);
148
- if (!e) throw new Error(`View "${i}" not found for screen "${t}"`);
149
- if (S) {
150
- await S({
161
+ return T.set(l, e), e;
162
+ }, B = () => {
163
+ if (N(), !L || !l || !m) return Promise.resolve(_());
164
+ const n = z(l, m, g());
165
+ return n.status === "hit" ? Promise.resolve(K(n.views, !1)) : M();
166
+ }, S = () => {
167
+ l && J(l);
168
+ }, O = async (n, i) => {
169
+ const e = (await B()).find((d) => d.view_id === n);
170
+ if (!e) throw new Error(`View "${n}" not found for screen "${t}"`);
171
+ if (v) {
172
+ await v({
151
173
  view_id: e.view_id,
152
174
  view_type: t,
153
175
  tab: o == null ? void 0 : o.tab,
@@ -155,127 +177,132 @@ function R(t, s = {}, d) {
155
177
  columns: e.columns,
156
178
  filters: e.filters,
157
179
  priority: e.priority,
158
- is_default: r
159
- }), g();
160
- const w = (await B()).find((y) => y.view_id === i);
161
- if (!w || w.is_default !== r)
180
+ is_default: i
181
+ }), S();
182
+ const y = (await M()).find((w) => w.view_id === n);
183
+ if (!y || y.is_default !== i)
162
184
  throw new Error(
163
- `The backend did not persist is_default=${r} for view "${e.name}". Saved views still show it as ${w ? `is_default=${w.is_default}` : "missing"}.`
185
+ `The backend did not persist is_default=${i} for view "${e.name}". Saved views still show it as ${y ? `is_default=${y.is_default}` : "missing"}.`
164
186
  );
165
- return w;
187
+ return y;
166
188
  }
167
- const n = _(), a = r ? n.map((c) => ({ ...c, is_default: c.view_id === i })) : n.map((c) => c.view_id === i ? { ...c, is_default: !1 } : c);
168
- return v(a), { ...e, is_default: r };
189
+ const s = _(), c = i ? s.map((d) => ({ ...d, is_default: d.view_id === n })) : s.map((d) => d.view_id === n ? { ...d, is_default: !1 } : d);
190
+ return E(c), { ...e, is_default: i };
169
191
  };
170
192
  return {
171
193
  async list() {
172
- return I();
194
+ return B();
173
195
  },
174
- async get(i) {
175
- return (await this.list()).find((e) => e.view_id === i) ?? null;
196
+ async get(n) {
197
+ return (await this.list()).find((e) => e.view_id === n) ?? null;
176
198
  },
177
199
  async getActive() {
178
- return (await this.list()).find((r) => r.is_default) ?? null;
200
+ return (await this.list()).find((i) => i.is_default) ?? null;
179
201
  },
180
- async save(i) {
181
- D(i.name);
182
- const r = {
183
- ...i,
202
+ async save(n) {
203
+ G(n.name);
204
+ const i = {
205
+ ...n,
184
206
  view_type: t,
185
- tab: (d ? l == null ? void 0 : l.tab : void 0) ?? i.tab,
186
- filters: i.filters ?? []
187
- }, e = U ? await U(r) : void 0, n = _(), a = n.findIndex(
188
- (y) => y.name.trim() === r.name.trim() || (e == null ? void 0 : e.view.view_id) && y.view_id === e.view.view_id
207
+ tab: (a ? u == null ? void 0 : u.tab : void 0) ?? n.tab,
208
+ filters: n.filters ?? []
209
+ }, e = A ? await A(i) : void 0, s = _(), c = s.findIndex(
210
+ (p) => p.name.trim() === i.name.trim() || (e == null ? void 0 : e.view.view_id) && p.view_id === e.view.view_id
189
211
  );
190
- if (a >= 0) {
191
- const y = {
192
- ...n[a],
193
- name: r.name,
212
+ if (c >= 0) {
213
+ const p = {
214
+ ...s[c],
215
+ name: i.name,
194
216
  view_type: t,
195
- tab: r.tab,
196
- columns: r.columns,
197
- filters: r.filters ?? [],
198
- priority: r.priority ?? n[a].priority
199
- }, E = x(e == null ? void 0 : e.view, y), V = n.slice();
200
- return V[a] = E, g(), v(V), { view: E, alreadyExists: (e == null ? void 0 : e.alreadyExists) ?? !0, message: e == null ? void 0 : e.message };
217
+ tab: i.tab,
218
+ columns: i.columns,
219
+ filters: i.filters ?? [],
220
+ priority: i.priority ?? s[c].priority
221
+ }, V = $(e == null ? void 0 : e.view, p), I = s.slice();
222
+ return I[c] = V, S(), E(I), { view: V, alreadyExists: (e == null ? void 0 : e.alreadyExists) ?? !0, message: e == null ? void 0 : e.message };
201
223
  }
202
- const c = {
203
- view_id: K(),
204
- name: r.name,
224
+ const d = s.length === 0, y = {
225
+ view_id: q(),
226
+ name: i.name,
205
227
  view_type: t,
206
- tab: r.tab,
207
- columns: r.columns,
208
- filters: r.filters ?? [],
209
- priority: r.priority ?? 0,
210
- // First view for a screen becomes primary so there is always an active view.
211
- is_default: n.length === 0,
228
+ tab: i.tab,
229
+ columns: i.columns,
230
+ filters: i.filters ?? [],
231
+ priority: i.priority ?? 0,
232
+ is_default: d,
212
233
  is_system_defined: !1
213
- }, w = x(e == null ? void 0 : e.view, c);
214
- return g(), v([...n, w]), { view: w, alreadyExists: (e == null ? void 0 : e.alreadyExists) ?? !1, message: e == null ? void 0 : e.message };
234
+ }, w = $(e == null ? void 0 : e.view, y);
235
+ if (S(), E([...s, w]), d && v)
236
+ try {
237
+ return { view: await O(w.view_id, !0), alreadyExists: (e == null ? void 0 : e.alreadyExists) ?? !1, message: e == null ? void 0 : e.message };
238
+ } catch {
239
+ return { view: w, alreadyExists: (e == null ? void 0 : e.alreadyExists) ?? !1, message: e == null ? void 0 : e.message };
240
+ }
241
+ return { view: w, alreadyExists: (e == null ? void 0 : e.alreadyExists) ?? !1, message: e == null ? void 0 : e.message };
215
242
  },
216
- async rename(i, r) {
217
- D(r);
218
- const e = S ? await this.get(i) : null, n = _(), a = n.findIndex((G) => G.view_id === i);
219
- if (a < 0 && !e) throw new Error(`View "${i}" not found for screen "${t}"`);
220
- const c = e ?? n[a], w = { ...c, name: r };
221
- let y = null;
222
- S && (y = await S({
223
- view_id: c.view_id || i,
243
+ async rename(n, i) {
244
+ G(i);
245
+ const e = v ? await this.get(n) : null, s = _(), c = s.findIndex((I) => I.view_id === n);
246
+ if (c < 0 && !e) throw new Error(`View "${n}" not found for screen "${t}"`);
247
+ const d = e ?? s[c], y = { ...d, name: i };
248
+ let w = null;
249
+ v && (w = await v({
250
+ view_id: d.view_id || n,
224
251
  view_type: t,
225
252
  tab: o == null ? void 0 : o.tab,
226
- name: r,
227
- columns: c.columns,
228
- filters: c.filters,
229
- priority: c.priority,
230
- is_default: c.is_default
253
+ name: i,
254
+ columns: d.columns,
255
+ filters: d.filters,
256
+ priority: d.priority,
257
+ is_default: d.is_default
231
258
  }));
232
- const E = x(y ?? void 0, w), V = a >= 0 ? n.slice() : [...n, E];
233
- return a >= 0 && (V[a] = E), g(), v(V), E;
259
+ const p = $(w ?? void 0, y), V = c >= 0 ? s.slice() : [...s, p];
260
+ return c >= 0 && (V[c] = p), S(), E(V), p;
234
261
  },
235
- async remove(i) {
236
- const r = _(), e = r.find((a) => a.view_id === i);
262
+ async remove(n) {
263
+ const i = _(), e = i.find((c) => c.view_id === n);
237
264
  if (!e) return;
238
- b && await b(e.view_id);
239
- const n = r.filter((a) => a.view_id !== i);
240
- g(), v(n);
265
+ x && await x(e.view_id);
266
+ const s = i.filter((c) => c.view_id !== n);
267
+ S(), E(s);
241
268
  },
242
- async setPrimary(i) {
243
- return M(i, !0);
269
+ async setPrimary(n) {
270
+ return O(n, !0);
244
271
  },
245
- async unsetPrimary(i) {
246
- return M(i, !1);
272
+ async unsetPrimary(n) {
273
+ return O(n, !1);
247
274
  },
248
275
  async reset() {
249
- if (b && await b(), g(), O())
276
+ if (x && await x(), S(), U())
250
277
  try {
251
- window.localStorage.removeItem(f);
278
+ window.localStorage.removeItem(f());
252
279
  } catch {
253
280
  }
254
- W(f);
281
+ Y(f());
255
282
  },
256
- subscribe(i) {
257
- lt();
258
- const r = u ? [f, u] : [f];
259
- return r.forEach((e) => {
260
- let n = m.get(e);
261
- n || (n = /* @__PURE__ */ new Set(), m.set(e, n)), n.add(i);
283
+ subscribe(n) {
284
+ mt();
285
+ const i = l ? [f(), l] : [f()];
286
+ return i.forEach((e) => {
287
+ let s = h.get(e);
288
+ s || (s = /* @__PURE__ */ new Set(), h.set(e, s)), s.add(n);
262
289
  }), () => {
263
- r.forEach((e) => {
264
- const n = m.get(e);
265
- n && (n.delete(i), n.size === 0 && m.delete(e));
290
+ i.forEach((e) => {
291
+ const s = h.get(e);
292
+ s && (s.delete(n), s.size === 0 && h.delete(e));
266
293
  });
267
294
  };
268
295
  }
269
296
  };
270
297
  }
271
- function ht(t, s) {
272
- const d = wt(t, s);
273
- return (f) => d.save(f);
298
+ function St(t, r) {
299
+ const a = gt(t, r);
300
+ return (f) => a.save(f);
274
301
  }
275
302
  export {
276
- ht as createLocalSaveView,
277
- wt as createLocalViewStore,
278
- pt as createTableViewSelectorStore,
279
- dt as isReservedViewName
303
+ St as createLocalSaveView,
304
+ gt as createLocalViewStore,
305
+ Et as createTableViewSelectorStore,
306
+ wt as isReservedViewName
280
307
  };
281
308
  //# sourceMappingURL=viewStore.js.map