@baseera/react 0.1.0 → 0.2.1

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/dist/index.js CHANGED
@@ -1,10 +1,10 @@
1
1
  "use client";
2
2
 
3
3
  // src/provider.tsx
4
- import { createBaseeraClient, createChatController, createImmersiveController, createTrailBatcher } from "@baseera/core";
4
+ import { createBaseeraClient, createChatController, createImmersiveController, createTrailBatcher } from "@baseera/core/browser";
5
5
 
6
6
  // src/page-context.ts
7
- import { LIMITS } from "@baseera/core";
7
+ import { LIMITS } from "@baseera/core/browser";
8
8
  var MAX_TEXT = 200;
9
9
  function isProduction() {
10
10
  try {
@@ -73,7 +73,7 @@ function createContextStore() {
73
73
  import { createContext, useContext, useEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
74
74
 
75
75
  // src/ui-tools.ts
76
- import { BaseeraRejection, LIMITS as LIMITS2 } from "@baseera/core";
76
+ import { BaseeraRejection, LIMITS as LIMITS2 } from "@baseera/core/browser";
77
77
  var FOCUS_FIELD_TOOL = "ui.focusField";
78
78
  var PREFILL_DRAFT_TOOL = "ui.prefillDraft";
79
79
  var MAX_REASON = 500;
@@ -261,7 +261,16 @@ function BaseeraProvider(props) {
261
261
  recordFinish: async (request) => client.recordFinish(await base.ensureSession(), request),
262
262
  // AI checks (GNA-82) run against the same session, starting one if needed.
263
263
  runAiChecks: async (wire) => client.runAiChecks(await base.ensureSession(), wire),
264
- trail
264
+ // The escalation ladder (GNA-154, GNA-162): used only by a screen that sets `ladder`.
265
+ // A custom client without `runLadder` rejects, so a screen that opted in stays closed (fail closed).
266
+ runLadder: async (request) => {
267
+ if (!client.runLadder) throw new Error("This client has no runLadder.");
268
+ return client.runLadder(await base.ensureSession(), request);
269
+ },
270
+ trail,
271
+ // A screen that leaves `ladder` unset follows the agent (GNA-154). Until the answer is in, or if it never comes, it counts as
272
+ // on (fail closed); a client that cannot ask keeps every screen as it declared itself.
273
+ ladderDefault: typeof client.getAgentSettings === "function"
265
274
  });
266
275
  base = createChatController({
267
276
  client,
@@ -282,6 +291,19 @@ function BaseeraProvider(props) {
282
291
  useEffect(() => {
283
292
  void core.controller.hydrate();
284
293
  }, [core]);
294
+ useEffect(() => {
295
+ let cancelled = false;
296
+ void (async () => {
297
+ try {
298
+ const settings = await core.client.getAgentSettings?.();
299
+ if (!cancelled && settings && settings.ladder === false) core.immersive.setLadderDefault(false);
300
+ } catch {
301
+ }
302
+ })();
303
+ return () => {
304
+ cancelled = true;
305
+ };
306
+ }, [core]);
285
307
  const [agentTheme, setAgentTheme] = useState(null);
286
308
  const [logoUrl, setLogoUrl] = useState(null);
287
309
  useEffect(() => {
@@ -375,14 +397,59 @@ function useBaseeraFeedback() {
375
397
  return useMemo3(() => ({ rate, ratings }), [rate, ratings]);
376
398
  }
377
399
 
400
+ // src/use-attention-next.ts
401
+ import { useCallback as useCallback2, useEffect as useEffect2, useMemo as useMemo4, useRef as useRef2, useState as useState3 } from "react";
402
+ var LEVELS = /* @__PURE__ */ new Set(["now", "digest", "silent"]);
403
+ var isObject = (v) => !!v && typeof v === "object" && !Array.isArray(v);
404
+ function readItems(raw, limit) {
405
+ if (!Array.isArray(raw)) return [];
406
+ const out = [];
407
+ for (const r of raw) {
408
+ if (!isObject(r) || typeof r.entityType !== "string" || typeof r.entityRef !== "string" || typeof r.score !== "number" || !Number.isFinite(r.score) || !LEVELS.has(String(r.level))) continue;
409
+ const reasons = Array.isArray(r.reasons) ? r.reasons.filter((x) => isObject(x) && typeof x.code === "string" && typeof x.contribution === "number" && Number.isFinite(x.contribution)).slice(0, 20) : [];
410
+ out.push({ entityType: r.entityType, entityRef: r.entityRef, score: r.score, level: r.level, reasons });
411
+ }
412
+ return out.map((x, i) => ({ x, i })).sort((a, b) => b.x.score - a.x.score || a.i - b.i).map((e) => e.x).slice(0, limit);
413
+ }
414
+ function useBaseeraAttentionNext(options = {}) {
415
+ const { controller, client } = useBaseeraInternals();
416
+ const enabled = options.enabled !== false;
417
+ const limit = Math.min(20, Math.max(1, Math.floor(options.limit ?? 20)));
418
+ const [items, setItems] = useState3([]);
419
+ const [state, setState] = useState3("idle");
420
+ const generation = useRef2(0);
421
+ const mounted = useRef2(true);
422
+ const refresh = useCallback2(async () => {
423
+ const mine = ++generation.current;
424
+ setState("loading");
425
+ try {
426
+ if (!client.attentionNext) throw new Error("This client has no attentionNext.");
427
+ const answer = await client.attentionNext(await controller.ensureSession());
428
+ if (!mounted.current || mine !== generation.current) return;
429
+ setItems(readItems(answer.items, limit));
430
+ setState("ok");
431
+ } catch {
432
+ if (mounted.current && mine === generation.current) setState("failed");
433
+ }
434
+ }, [client, controller, limit]);
435
+ useEffect2(() => {
436
+ mounted.current = true;
437
+ if (enabled) void refresh();
438
+ return () => {
439
+ mounted.current = false;
440
+ };
441
+ }, [enabled, refresh]);
442
+ return useMemo4(() => ({ items, state, refresh }), [items, state, refresh]);
443
+ }
444
+
378
445
  // src/use-context.ts
379
- import { useEffect as useEffect2, useRef as useRef2 } from "react";
446
+ import { useEffect as useEffect3, useRef as useRef3 } from "react";
380
447
  function useBaseeraContext(input) {
381
448
  const { contextStore } = useBaseeraInternals();
382
- const idRef = useRef2(null);
449
+ const idRef = useRef3(null);
383
450
  idRef.current ??= contextStore.claim();
384
451
  const id = idRef.current;
385
- const inputRef = useRef2(input);
452
+ const inputRef = useRef3(input);
386
453
  inputRef.current = input;
387
454
  let key;
388
455
  try {
@@ -390,20 +457,20 @@ function useBaseeraContext(input) {
390
457
  } catch {
391
458
  key = String(Math.random());
392
459
  }
393
- useEffect2(() => {
460
+ useEffect3(() => {
394
461
  contextStore.set(id, inputRef.current);
395
462
  }, [contextStore, id, key]);
396
- useEffect2(() => () => contextStore.remove(id), [contextStore, id]);
463
+ useEffect3(() => () => contextStore.remove(id), [contextStore, id]);
397
464
  }
398
465
 
399
466
  // src/use-approvals.ts
400
- import { useMemo as useMemo4, useSyncExternalStore as useSyncExternalStore3 } from "react";
467
+ import { useMemo as useMemo5, useSyncExternalStore as useSyncExternalStore3 } from "react";
401
468
  var EMPTY = { sessionId: null, messages: [], status: "idle", error: null, pendingApprovals: [] };
402
469
  var getServerSnapshot2 = () => EMPTY;
403
470
  function useBaseeraApprovals() {
404
471
  const { controller } = useBaseeraInternals();
405
472
  const state = useSyncExternalStore3(controller.subscribe, controller.getSnapshot, getServerSnapshot2);
406
- return useMemo4(
473
+ return useMemo5(
407
474
  () => ({
408
475
  pending: state.pendingApprovals,
409
476
  approve: controller.approve,
@@ -420,8 +487,8 @@ function useBaseeraApprovals() {
420
487
  }
421
488
 
422
489
  // src/use-immersive.ts
423
- import { createImmersiveController as createImmersiveController2 } from "@baseera/core";
424
- import { useEffect as useEffect3, useMemo as useMemo5, useRef as useRef3, useSyncExternalStore as useSyncExternalStore4 } from "react";
490
+ import { createImmersiveController as createImmersiveController2 } from "@baseera/core/browser";
491
+ import { useEffect as useEffect4, useMemo as useMemo6, useRef as useRef4, useSyncExternalStore as useSyncExternalStore4 } from "react";
425
492
  function isProduction2() {
426
493
  try {
427
494
  return process.env.NODE_ENV === "production";
@@ -443,14 +510,14 @@ function valuesKey(def) {
443
510
  }
444
511
  function useBaseeraImmersive(def) {
445
512
  const { immersive, immersiveSlot } = useBaseeraInternals();
446
- const claimRef = useRef3(null);
513
+ const claimRef = useRef4(null);
447
514
  claimRef.current ??= { order: immersiveSlot.nextOrder++, entity: null };
448
515
  const claim = claimRef.current;
449
- const defRef = useRef3(def);
516
+ const defRef = useRef4(def);
450
517
  defRef.current = def;
451
518
  const key = valuesKey(def);
452
- const lastKey = useRef3(key);
453
- useEffect3(() => {
519
+ const lastKey = useRef4(key);
520
+ useEffect4(() => {
454
521
  const latest = defRef.current;
455
522
  claim.entity = { type: latest.entity.type, ref: latest.entity.ref };
456
523
  const current = immersiveSlot.owner;
@@ -471,10 +538,10 @@ function useBaseeraImmersive(def) {
471
538
  immersive.stop();
472
539
  };
473
540
  }, [immersive, immersiveSlot, claim]);
474
- useEffect3(() => {
541
+ useEffect4(() => {
475
542
  if (immersiveSlot.owner === claim) immersive.update(def);
476
543
  });
477
- useEffect3(() => {
544
+ useEffect4(() => {
478
545
  if (lastKey.current === key) return;
479
546
  lastKey.current = key;
480
547
  if (immersiveSlot.owner === claim) immersive.recheck();
@@ -489,7 +556,7 @@ function useImmersiveSnapshot() {
489
556
  function useBaseeraImmersiveState() {
490
557
  const { immersive } = useBaseeraInternals();
491
558
  const snap = useSyncExternalStore4(immersive.subscribe, immersive.getSnapshot, getServerSnapshot3);
492
- return useMemo5(() => {
559
+ return useMemo6(() => {
493
560
  const open = snap.results.filter((r) => r.status === "open");
494
561
  return {
495
562
  active: snap.active,
@@ -510,6 +577,11 @@ function useBaseeraImmersiveState() {
510
577
  finishLabel: snap.finishLabel,
511
578
  pendingDrafts: snap.pendingDrafts,
512
579
  aiChecks: snap.aiChecks,
580
+ ladder: snap.ladder,
581
+ ladderState: snap.ladderState,
582
+ ladderCache: snap.ladderCache,
583
+ queueFound: snap.queueFound,
584
+ rerunLadder: immersive.rerunLadder,
513
585
  fieldLabels: snap.fieldLabels,
514
586
  boundFields: snap.boundFields,
515
587
  lastApplied: snap.lastApplied,
@@ -529,22 +601,22 @@ function useBaseeraSignal() {
529
601
  }
530
602
 
531
603
  // src/use-knowledge-upload.ts
532
- import { createTenantUploadController } from "@baseera/core";
533
- import { useEffect as useEffect4, useMemo as useMemo6, useRef as useRef4, useState as useState3, useSyncExternalStore as useSyncExternalStore5 } from "react";
604
+ import { createTenantUploadController } from "@baseera/core/browser";
605
+ import { useEffect as useEffect5, useMemo as useMemo7, useRef as useRef5, useState as useState4, useSyncExternalStore as useSyncExternalStore5 } from "react";
534
606
  var SERVER_STATE2 = { sources: null, uploads: [], loading: true, error: null, disabled: false };
535
607
  function useBaseeraKnowledgeUpload(options) {
536
608
  const { connection } = useBaseeraInternals();
537
- const tokenRef = useRef4(options.getUploadToken);
609
+ const tokenRef = useRef5(options.getUploadToken);
538
610
  tokenRef.current = options.getUploadToken;
539
- const [controller] = useState3(
611
+ const [controller] = useState4(
540
612
  () => createTenantUploadController({ publishableKey: connection.publishableKey, baseUrl: connection.baseUrl, getUploadToken: () => tokenRef.current(), ...options.pollMs ? { pollMs: options.pollMs } : {} })
541
613
  );
542
614
  const state = useSyncExternalStore5(controller.subscribe, controller.getSnapshot, () => SERVER_STATE2);
543
- useEffect4(() => {
615
+ useEffect5(() => {
544
616
  controller.start();
545
617
  return () => controller.stop();
546
618
  }, [controller]);
547
- return useMemo6(
619
+ return useMemo7(
548
620
  () => ({
549
621
  ...state,
550
622
  upload: (files) => controller.upload(Array.from(files)),
@@ -556,7 +628,7 @@ function useBaseeraKnowledgeUpload(options) {
556
628
  }
557
629
 
558
630
  // src/use-field.ts
559
- import { useCallback as useCallback2, useEffect as useEffect5, useMemo as useMemo7, useRef as useRef5 } from "react";
631
+ import { useCallback as useCallback3, useEffect as useEffect6, useMemo as useMemo8, useRef as useRef6 } from "react";
560
632
  var MAX_REASON2 = 500;
561
633
  var FOCUSABLE = 'input:not([type="hidden"]):not([disabled]), select:not([disabled]), textarea:not([disabled]), button:not([disabled]), a[href], [contenteditable="true"], [tabindex]:not([tabindex="-1"])';
562
634
  var SEVERITY_RANK = { info: 1, warning: 2, blocking: 3 };
@@ -574,14 +646,14 @@ function prefersReducedMotion() {
574
646
  function useBaseeraField(name, options) {
575
647
  const { immersive } = useBaseeraInternals();
576
648
  const snap = useImmersiveSnapshot();
577
- const elementRef = useRef5(null);
578
- const onChangeRef = useRef5(options.onChange);
649
+ const elementRef = useRef6(null);
650
+ const onChangeRef = useRef6(options.onChange);
579
651
  onChangeRef.current = options.onChange;
580
- const focusControl = useCallback2(() => {
652
+ const focusControl = useCallback3(() => {
581
653
  const el = elementRef.current;
582
654
  if (el) firstControl(el)?.focus({ preventScroll: true });
583
655
  }, []);
584
- useEffect5(
656
+ useEffect6(
585
657
  () => immersive.bindField(name, {
586
658
  write(value) {
587
659
  onChangeRef.current(value);
@@ -595,12 +667,12 @@ function useBaseeraField(name, options) {
595
667
  }),
596
668
  [immersive, name]
597
669
  );
598
- const bindRef = useCallback2((element) => {
670
+ const bindRef = useCallback3((element) => {
599
671
  elementRef.current = element;
600
672
  }, []);
601
673
  const pending = snap.pendingDrafts.find((d) => d.field === name);
602
- const draft = useMemo7(() => pending ? { value: pending.value, reason: pending.reason, source: pending.source } : null, [pending]);
603
- const flag = useMemo7(() => {
674
+ const draft = useMemo8(() => pending ? { value: pending.value, reason: pending.reason, source: pending.source } : null, [pending]);
675
+ const flag = useMemo8(() => {
604
676
  let best = null;
605
677
  for (const r of snap.results) {
606
678
  if (r.status !== "open" || !r.fields.includes(name)) continue;
@@ -609,42 +681,42 @@ function useBaseeraField(name, options) {
609
681
  return best;
610
682
  }, [snap.results, name]);
611
683
  const focusedAt = snap.focused?.field === name ? snap.focused.at : null;
612
- const focused = useMemo7(() => focusedAt === null ? null : { at: focusedAt }, [focusedAt]);
613
- const approve = useCallback2(() => {
684
+ const focused = useMemo8(() => focusedAt === null ? null : { at: focusedAt }, [focusedAt]);
685
+ const approve = useCallback3(() => {
614
686
  const current = immersive.getSnapshot().pendingDrafts.find((d) => d.field === name);
615
687
  if (!current) return;
616
688
  const previous = immersive.readField(name);
617
689
  onChangeRef.current(current.value);
618
690
  immersive.resolveDraft(name, { approved: true }, { previous });
619
691
  }, [immersive, name]);
620
- const reject = useCallback2(
692
+ const reject = useCallback3(
621
693
  (reason) => {
622
694
  const text = reason?.trim().slice(0, MAX_REASON2);
623
695
  immersive.resolveDraft(name, { approved: false, ...text ? { reason: text } : {} });
624
696
  },
625
697
  [immersive, name]
626
698
  );
627
- return useMemo7(() => ({ draft, flag, focused, approve, reject, bindRef, focusControl }), [draft, flag, focused, approve, reject, bindRef, focusControl]);
699
+ return useMemo8(() => ({ draft, flag, focused, approve, reject, bindRef, focusControl }), [draft, flag, focused, approve, reject, bindRef, focusControl]);
628
700
  }
629
701
 
630
702
  // src/index.ts
631
- import { BaseeraRejection as BaseeraRejection2, CHECK_FAILED_MESSAGE } from "@baseera/core";
703
+ import { BaseeraRejection as BaseeraRejection2, CHECK_FAILED_MESSAGE } from "@baseera/core/browser";
632
704
 
633
705
  // src/use-tool.ts
634
- import { useEffect as useEffect6, useMemo as useMemo8, useRef as useRef6 } from "react";
706
+ import { useEffect as useEffect7, useMemo as useMemo9, useRef as useRef7 } from "react";
635
707
  import { z } from "zod";
636
708
  var NAME_PATTERN = /^[a-zA-Z0-9_.-]{1,64}$/;
637
709
  var MAX_DESCRIPTION = 2e3;
638
710
  var MAX_SCHEMA_BYTES = 16e3;
639
711
  var MAX_ERROR = 1e3;
640
712
  function useBaseeraToolDefinition(tool) {
641
- const latest = useRef6(tool);
713
+ const latest = useRef7(tool);
642
714
  latest.current = tool;
643
715
  useRegistration(tool.spec.name, latest);
644
716
  }
645
717
  function useRegistration(key, latest) {
646
718
  const { registry } = useBaseeraInternals();
647
- useEffect6(() => {
719
+ useEffect7(() => {
648
720
  const registered = {
649
721
  get spec() {
650
722
  return latest.current.spec;
@@ -699,7 +771,7 @@ function summarize(error) {
699
771
  }
700
772
  function useBaseeraTool(options) {
701
773
  const { name, description, input, kind } = options;
702
- const spec = useMemo8(() => {
774
+ const spec = useMemo9(() => {
703
775
  if (typeof name !== "string" || !NAME_PATTERN.test(name)) {
704
776
  throw new Error(`useBaseeraTool: invalid name ${JSON.stringify(name)}. Use 1 to 64 letters, digits, "_", "." or "-".`);
705
777
  }
@@ -708,11 +780,11 @@ function useBaseeraTool(options) {
708
780
  }
709
781
  return { name, description, kind, inputSchema: toWireSchema(name, input) };
710
782
  }, [name, description, kind, input]);
711
- const optionsRef = useRef6(options);
783
+ const optionsRef = useRef7(options);
712
784
  optionsRef.current = options;
713
- const specRef = useRef6(spec);
785
+ const specRef = useRef7(spec);
714
786
  specRef.current = spec;
715
- const latest = useRef6(null);
787
+ const latest = useRef7(null);
716
788
  latest.current ??= {
717
789
  get spec() {
718
790
  return specRef.current;
@@ -727,7 +799,7 @@ function useBaseeraTool(options) {
727
799
  return optionsRef.current.undo ? (value, output) => optionsRef.current.undo?.(value, output) : void 0;
728
800
  }
729
801
  };
730
- const key = useMemo8(() => JSON.stringify([spec.name, spec.kind, spec.description]), [spec]);
802
+ const key = useMemo9(() => JSON.stringify([spec.name, spec.kind, spec.description]), [spec]);
731
803
  useRegistration(`${key}:${schemaId(input)}`, latest);
732
804
  }
733
805
  var ids = /* @__PURE__ */ new WeakMap();
@@ -739,7 +811,7 @@ function schemaId(schema) {
739
811
  }
740
812
 
741
813
  // src/index.ts
742
- import { FEEDBACK_REASON_MAX, PROTOCOL_VERSION } from "@baseera/core";
814
+ import { FEEDBACK_REASON_MAX, PROTOCOL_VERSION } from "@baseera/core/browser";
743
815
  export {
744
816
  BaseeraProvider,
745
817
  BaseeraRejection2 as BaseeraRejection,
@@ -751,6 +823,7 @@ export {
751
823
  firstControl,
752
824
  useBaseeraAgentTheme,
753
825
  useBaseeraApprovals,
826
+ useBaseeraAttentionNext,
754
827
  useBaseeraChat,
755
828
  useBaseeraContext,
756
829
  useBaseeraFeedback,