@baseera/react 0.2.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.cjs +142 -68
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +40 -3
- package/dist/index.d.ts +40 -3
- package/dist/index.js +121 -48
- package/dist/index.js.map +1 -1
- package/package.json +6 -3
package/dist/index.cjs
CHANGED
|
@@ -22,15 +22,16 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
22
22
|
var index_exports = {};
|
|
23
23
|
__export(index_exports, {
|
|
24
24
|
BaseeraProvider: () => BaseeraProvider,
|
|
25
|
-
BaseeraRejection: () =>
|
|
26
|
-
CHECK_FAILED_MESSAGE: () =>
|
|
27
|
-
FEEDBACK_REASON_MAX: () =>
|
|
25
|
+
BaseeraRejection: () => import_browser6.BaseeraRejection,
|
|
26
|
+
CHECK_FAILED_MESSAGE: () => import_browser6.CHECK_FAILED_MESSAGE,
|
|
27
|
+
FEEDBACK_REASON_MAX: () => import_browser7.FEEDBACK_REASON_MAX,
|
|
28
28
|
FOCUS_FIELD_TOOL: () => FOCUS_FIELD_TOOL,
|
|
29
29
|
PREFILL_DRAFT_TOOL: () => PREFILL_DRAFT_TOOL,
|
|
30
|
-
PROTOCOL_VERSION: () =>
|
|
30
|
+
PROTOCOL_VERSION: () => import_browser7.PROTOCOL_VERSION,
|
|
31
31
|
firstControl: () => firstControl,
|
|
32
32
|
useBaseeraAgentTheme: () => useBaseeraAgentTheme,
|
|
33
33
|
useBaseeraApprovals: () => useBaseeraApprovals,
|
|
34
|
+
useBaseeraAttentionNext: () => useBaseeraAttentionNext,
|
|
34
35
|
useBaseeraChat: () => useBaseeraChat,
|
|
35
36
|
useBaseeraContext: () => useBaseeraContext,
|
|
36
37
|
useBaseeraFeedback: () => useBaseeraFeedback,
|
|
@@ -46,10 +47,10 @@ __export(index_exports, {
|
|
|
46
47
|
module.exports = __toCommonJS(index_exports);
|
|
47
48
|
|
|
48
49
|
// src/provider.tsx
|
|
49
|
-
var
|
|
50
|
+
var import_browser3 = require("@baseera/core/browser");
|
|
50
51
|
|
|
51
52
|
// src/page-context.ts
|
|
52
|
-
var
|
|
53
|
+
var import_browser = require("@baseera/core/browser");
|
|
53
54
|
var MAX_TEXT = 200;
|
|
54
55
|
function isProduction() {
|
|
55
56
|
try {
|
|
@@ -101,11 +102,11 @@ function createContextStore() {
|
|
|
101
102
|
} catch {
|
|
102
103
|
warnOnce("unserializable", "[baseera] Context data could not be serialized to JSON, so it was left out of the turn.");
|
|
103
104
|
}
|
|
104
|
-
if (size !== null && size <=
|
|
105
|
+
if (size !== null && size <= import_browser.LIMITS.contextDataBytes) out.data = merged.data;
|
|
105
106
|
else if (size !== null) {
|
|
106
107
|
warnOnce(
|
|
107
108
|
`size:${size}`,
|
|
108
|
-
`[baseera] Context data is ${size} bytes serialized, over the ${
|
|
109
|
+
`[baseera] Context data is ${size} bytes serialized, over the ${import_browser.LIMITS.contextDataBytes} byte limit, so it was left out of the turn. Send less in \`data\`.`
|
|
109
110
|
);
|
|
110
111
|
}
|
|
111
112
|
}
|
|
@@ -118,7 +119,7 @@ function createContextStore() {
|
|
|
118
119
|
var import_react = require("react");
|
|
119
120
|
|
|
120
121
|
// src/ui-tools.ts
|
|
121
|
-
var
|
|
122
|
+
var import_browser2 = require("@baseera/core/browser");
|
|
122
123
|
var FOCUS_FIELD_TOOL = "ui.focusField";
|
|
123
124
|
var PREFILL_DRAFT_TOOL = "ui.prefillDraft";
|
|
124
125
|
var MAX_REASON = 500;
|
|
@@ -128,7 +129,7 @@ var isRecord = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
|
128
129
|
function readField(input) {
|
|
129
130
|
if (!isRecord(input)) return { ok: false, error: "input must be an object." };
|
|
130
131
|
const { field } = input;
|
|
131
|
-
if (typeof field !== "string" || field === "" || field.length >
|
|
132
|
+
if (typeof field !== "string" || field === "" || field.length > import_browser2.LIMITS.immersiveIdChars) return { ok: false, error: "`field` must be the name of a field." };
|
|
132
133
|
return { ok: true, field, rest: input };
|
|
133
134
|
}
|
|
134
135
|
function createUiTools(immersive) {
|
|
@@ -163,7 +164,7 @@ function createUiTools(immersive) {
|
|
|
163
164
|
type: "object",
|
|
164
165
|
properties: {
|
|
165
166
|
field: { type: "string", description: "The name of a field, as listed in the record." },
|
|
166
|
-
value: { description: `The proposed value as JSON (text, number, boolean, null, ...), at most ${
|
|
167
|
+
value: { description: `The proposed value as JSON (text, number, boolean, null, ...), at most ${import_browser2.LIMITS.immersiveFieldValueBytes / 1024} KB serialized. Match the field's kind.` },
|
|
167
168
|
reason: { type: "string", maxLength: MAX_REASON, description: "One short sentence for the user: where the value comes from." }
|
|
168
169
|
},
|
|
169
170
|
required: ["field", "value"],
|
|
@@ -186,7 +187,7 @@ function createUiTools(immersive) {
|
|
|
186
187
|
}
|
|
187
188
|
if (json === void 0) return { ok: false, error: "`value` must be JSON." };
|
|
188
189
|
const bytes = new TextEncoder().encode(json).length;
|
|
189
|
-
if (bytes >
|
|
190
|
+
if (bytes > import_browser2.LIMITS.immersiveFieldValueBytes) return { ok: false, error: `\`value\` is ${bytes} bytes; the limit is ${import_browser2.LIMITS.immersiveFieldValueBytes}.` };
|
|
190
191
|
const { reason } = rest;
|
|
191
192
|
if (reason !== void 0 && typeof reason !== "string") return { ok: false, error: "`reason` must be text." };
|
|
192
193
|
return { ok: true, value: { field, value: JSON.parse(json), ...reason ? { reason: reason.trim().slice(0, MAX_REASON) } : {} } };
|
|
@@ -204,7 +205,7 @@ function createUiTools(immersive) {
|
|
|
204
205
|
{ once: true }
|
|
205
206
|
);
|
|
206
207
|
const result = await decision;
|
|
207
|
-
if (!result.approved) throw new
|
|
208
|
+
if (!result.approved) throw new import_browser2.BaseeraRejection(result.reason);
|
|
208
209
|
return { ok: true, field, written: true };
|
|
209
210
|
}
|
|
210
211
|
};
|
|
@@ -288,7 +289,7 @@ function BaseeraProvider(props) {
|
|
|
288
289
|
onErrorRef.current = props.onError;
|
|
289
290
|
const [core] = (0, import_react.useState)(() => {
|
|
290
291
|
const registry = createToolRegistry();
|
|
291
|
-
const make = props.createClient ??
|
|
292
|
+
const make = props.createClient ?? import_browser3.createBaseeraClient;
|
|
292
293
|
const client = make({
|
|
293
294
|
publishableKey,
|
|
294
295
|
baseUrl,
|
|
@@ -297,18 +298,27 @@ function BaseeraProvider(props) {
|
|
|
297
298
|
const contextStore = createContextStore();
|
|
298
299
|
const immersiveSlot = { owner: null, nextOrder: 0 };
|
|
299
300
|
let base;
|
|
300
|
-
const trail = (0,
|
|
301
|
+
const trail = (0, import_browser3.createTrailBatcher)({
|
|
301
302
|
ensureSession: () => base.ensureSession(),
|
|
302
303
|
currentSessionId: () => base.getSnapshot().sessionId,
|
|
303
304
|
send: (sessionId, events, options) => client.appendImmersiveEvents(sessionId, events, options)
|
|
304
305
|
});
|
|
305
|
-
const immersive = (0,
|
|
306
|
+
const immersive = (0, import_browser3.createImmersiveController)({
|
|
306
307
|
recordFinish: async (request) => client.recordFinish(await base.ensureSession(), request),
|
|
307
308
|
// AI checks (GNA-82) run against the same session, starting one if needed.
|
|
308
309
|
runAiChecks: async (wire) => client.runAiChecks(await base.ensureSession(), wire),
|
|
309
|
-
|
|
310
|
+
// The escalation ladder (GNA-154, GNA-162): used only by a screen that sets `ladder`.
|
|
311
|
+
// A custom client without `runLadder` rejects, so a screen that opted in stays closed (fail closed).
|
|
312
|
+
runLadder: async (request) => {
|
|
313
|
+
if (!client.runLadder) throw new Error("This client has no runLadder.");
|
|
314
|
+
return client.runLadder(await base.ensureSession(), request);
|
|
315
|
+
},
|
|
316
|
+
trail,
|
|
317
|
+
// A screen that leaves `ladder` unset follows the agent (GNA-154). Until the answer is in, or if it never comes, it counts as
|
|
318
|
+
// on (fail closed); a client that cannot ask keeps every screen as it declared itself.
|
|
319
|
+
ladderDefault: typeof client.getAgentSettings === "function"
|
|
310
320
|
});
|
|
311
|
-
base = (0,
|
|
321
|
+
base = (0, import_browser3.createChatController)({
|
|
312
322
|
client,
|
|
313
323
|
getTools: registry.list,
|
|
314
324
|
storage: sessionStorageSafe,
|
|
@@ -327,6 +337,19 @@ function BaseeraProvider(props) {
|
|
|
327
337
|
(0, import_react.useEffect)(() => {
|
|
328
338
|
void core.controller.hydrate();
|
|
329
339
|
}, [core]);
|
|
340
|
+
(0, import_react.useEffect)(() => {
|
|
341
|
+
let cancelled = false;
|
|
342
|
+
void (async () => {
|
|
343
|
+
try {
|
|
344
|
+
const settings = await core.client.getAgentSettings?.();
|
|
345
|
+
if (!cancelled && settings && settings.ladder === false) core.immersive.setLadderDefault(false);
|
|
346
|
+
} catch {
|
|
347
|
+
}
|
|
348
|
+
})();
|
|
349
|
+
return () => {
|
|
350
|
+
cancelled = true;
|
|
351
|
+
};
|
|
352
|
+
}, [core]);
|
|
330
353
|
const [agentTheme, setAgentTheme] = (0, import_react.useState)(null);
|
|
331
354
|
const [logoUrl, setLogoUrl] = (0, import_react.useState)(null);
|
|
332
355
|
(0, import_react.useEffect)(() => {
|
|
@@ -420,14 +443,59 @@ function useBaseeraFeedback() {
|
|
|
420
443
|
return (0, import_react3.useMemo)(() => ({ rate, ratings }), [rate, ratings]);
|
|
421
444
|
}
|
|
422
445
|
|
|
423
|
-
// src/use-
|
|
446
|
+
// src/use-attention-next.ts
|
|
424
447
|
var import_react4 = require("react");
|
|
448
|
+
var LEVELS = /* @__PURE__ */ new Set(["now", "digest", "silent"]);
|
|
449
|
+
var isObject = (v) => !!v && typeof v === "object" && !Array.isArray(v);
|
|
450
|
+
function readItems(raw, limit) {
|
|
451
|
+
if (!Array.isArray(raw)) return [];
|
|
452
|
+
const out = [];
|
|
453
|
+
for (const r of raw) {
|
|
454
|
+
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;
|
|
455
|
+
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) : [];
|
|
456
|
+
out.push({ entityType: r.entityType, entityRef: r.entityRef, score: r.score, level: r.level, reasons });
|
|
457
|
+
}
|
|
458
|
+
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);
|
|
459
|
+
}
|
|
460
|
+
function useBaseeraAttentionNext(options = {}) {
|
|
461
|
+
const { controller, client } = useBaseeraInternals();
|
|
462
|
+
const enabled = options.enabled !== false;
|
|
463
|
+
const limit = Math.min(20, Math.max(1, Math.floor(options.limit ?? 20)));
|
|
464
|
+
const [items, setItems] = (0, import_react4.useState)([]);
|
|
465
|
+
const [state, setState] = (0, import_react4.useState)("idle");
|
|
466
|
+
const generation = (0, import_react4.useRef)(0);
|
|
467
|
+
const mounted = (0, import_react4.useRef)(true);
|
|
468
|
+
const refresh = (0, import_react4.useCallback)(async () => {
|
|
469
|
+
const mine = ++generation.current;
|
|
470
|
+
setState("loading");
|
|
471
|
+
try {
|
|
472
|
+
if (!client.attentionNext) throw new Error("This client has no attentionNext.");
|
|
473
|
+
const answer = await client.attentionNext(await controller.ensureSession());
|
|
474
|
+
if (!mounted.current || mine !== generation.current) return;
|
|
475
|
+
setItems(readItems(answer.items, limit));
|
|
476
|
+
setState("ok");
|
|
477
|
+
} catch {
|
|
478
|
+
if (mounted.current && mine === generation.current) setState("failed");
|
|
479
|
+
}
|
|
480
|
+
}, [client, controller, limit]);
|
|
481
|
+
(0, import_react4.useEffect)(() => {
|
|
482
|
+
mounted.current = true;
|
|
483
|
+
if (enabled) void refresh();
|
|
484
|
+
return () => {
|
|
485
|
+
mounted.current = false;
|
|
486
|
+
};
|
|
487
|
+
}, [enabled, refresh]);
|
|
488
|
+
return (0, import_react4.useMemo)(() => ({ items, state, refresh }), [items, state, refresh]);
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
// src/use-context.ts
|
|
492
|
+
var import_react5 = require("react");
|
|
425
493
|
function useBaseeraContext(input) {
|
|
426
494
|
const { contextStore } = useBaseeraInternals();
|
|
427
|
-
const idRef = (0,
|
|
495
|
+
const idRef = (0, import_react5.useRef)(null);
|
|
428
496
|
idRef.current ??= contextStore.claim();
|
|
429
497
|
const id = idRef.current;
|
|
430
|
-
const inputRef = (0,
|
|
498
|
+
const inputRef = (0, import_react5.useRef)(input);
|
|
431
499
|
inputRef.current = input;
|
|
432
500
|
let key;
|
|
433
501
|
try {
|
|
@@ -435,20 +503,20 @@ function useBaseeraContext(input) {
|
|
|
435
503
|
} catch {
|
|
436
504
|
key = String(Math.random());
|
|
437
505
|
}
|
|
438
|
-
(0,
|
|
506
|
+
(0, import_react5.useEffect)(() => {
|
|
439
507
|
contextStore.set(id, inputRef.current);
|
|
440
508
|
}, [contextStore, id, key]);
|
|
441
|
-
(0,
|
|
509
|
+
(0, import_react5.useEffect)(() => () => contextStore.remove(id), [contextStore, id]);
|
|
442
510
|
}
|
|
443
511
|
|
|
444
512
|
// src/use-approvals.ts
|
|
445
|
-
var
|
|
513
|
+
var import_react6 = require("react");
|
|
446
514
|
var EMPTY = { sessionId: null, messages: [], status: "idle", error: null, pendingApprovals: [] };
|
|
447
515
|
var getServerSnapshot2 = () => EMPTY;
|
|
448
516
|
function useBaseeraApprovals() {
|
|
449
517
|
const { controller } = useBaseeraInternals();
|
|
450
|
-
const state = (0,
|
|
451
|
-
return (0,
|
|
518
|
+
const state = (0, import_react6.useSyncExternalStore)(controller.subscribe, controller.getSnapshot, getServerSnapshot2);
|
|
519
|
+
return (0, import_react6.useMemo)(
|
|
452
520
|
() => ({
|
|
453
521
|
pending: state.pendingApprovals,
|
|
454
522
|
approve: controller.approve,
|
|
@@ -465,8 +533,8 @@ function useBaseeraApprovals() {
|
|
|
465
533
|
}
|
|
466
534
|
|
|
467
535
|
// src/use-immersive.ts
|
|
468
|
-
var
|
|
469
|
-
var
|
|
536
|
+
var import_browser4 = require("@baseera/core/browser");
|
|
537
|
+
var import_react7 = require("react");
|
|
470
538
|
function isProduction2() {
|
|
471
539
|
try {
|
|
472
540
|
return process.env.NODE_ENV === "production";
|
|
@@ -488,14 +556,14 @@ function valuesKey(def) {
|
|
|
488
556
|
}
|
|
489
557
|
function useBaseeraImmersive(def) {
|
|
490
558
|
const { immersive, immersiveSlot } = useBaseeraInternals();
|
|
491
|
-
const claimRef = (0,
|
|
559
|
+
const claimRef = (0, import_react7.useRef)(null);
|
|
492
560
|
claimRef.current ??= { order: immersiveSlot.nextOrder++, entity: null };
|
|
493
561
|
const claim = claimRef.current;
|
|
494
|
-
const defRef = (0,
|
|
562
|
+
const defRef = (0, import_react7.useRef)(def);
|
|
495
563
|
defRef.current = def;
|
|
496
564
|
const key = valuesKey(def);
|
|
497
|
-
const lastKey = (0,
|
|
498
|
-
(0,
|
|
565
|
+
const lastKey = (0, import_react7.useRef)(key);
|
|
566
|
+
(0, import_react7.useEffect)(() => {
|
|
499
567
|
const latest = defRef.current;
|
|
500
568
|
claim.entity = { type: latest.entity.type, ref: latest.entity.ref };
|
|
501
569
|
const current = immersiveSlot.owner;
|
|
@@ -516,25 +584,25 @@ function useBaseeraImmersive(def) {
|
|
|
516
584
|
immersive.stop();
|
|
517
585
|
};
|
|
518
586
|
}, [immersive, immersiveSlot, claim]);
|
|
519
|
-
(0,
|
|
587
|
+
(0, import_react7.useEffect)(() => {
|
|
520
588
|
if (immersiveSlot.owner === claim) immersive.update(def);
|
|
521
589
|
});
|
|
522
|
-
(0,
|
|
590
|
+
(0, import_react7.useEffect)(() => {
|
|
523
591
|
if (lastKey.current === key) return;
|
|
524
592
|
lastKey.current = key;
|
|
525
593
|
if (immersiveSlot.owner === claim) immersive.recheck();
|
|
526
594
|
}, [key, immersive, immersiveSlot, claim]);
|
|
527
595
|
}
|
|
528
|
-
var SERVER_SNAPSHOT = (0,
|
|
596
|
+
var SERVER_SNAPSHOT = (0, import_browser4.createImmersiveController)().getSnapshot();
|
|
529
597
|
var getServerSnapshot3 = () => SERVER_SNAPSHOT;
|
|
530
598
|
function useImmersiveSnapshot() {
|
|
531
599
|
const { immersive } = useBaseeraInternals();
|
|
532
|
-
return (0,
|
|
600
|
+
return (0, import_react7.useSyncExternalStore)(immersive.subscribe, immersive.getSnapshot, getServerSnapshot3);
|
|
533
601
|
}
|
|
534
602
|
function useBaseeraImmersiveState() {
|
|
535
603
|
const { immersive } = useBaseeraInternals();
|
|
536
|
-
const snap = (0,
|
|
537
|
-
return (0,
|
|
604
|
+
const snap = (0, import_react7.useSyncExternalStore)(immersive.subscribe, immersive.getSnapshot, getServerSnapshot3);
|
|
605
|
+
return (0, import_react7.useMemo)(() => {
|
|
538
606
|
const open = snap.results.filter((r) => r.status === "open");
|
|
539
607
|
return {
|
|
540
608
|
active: snap.active,
|
|
@@ -555,6 +623,11 @@ function useBaseeraImmersiveState() {
|
|
|
555
623
|
finishLabel: snap.finishLabel,
|
|
556
624
|
pendingDrafts: snap.pendingDrafts,
|
|
557
625
|
aiChecks: snap.aiChecks,
|
|
626
|
+
ladder: snap.ladder,
|
|
627
|
+
ladderState: snap.ladderState,
|
|
628
|
+
ladderCache: snap.ladderCache,
|
|
629
|
+
queueFound: snap.queueFound,
|
|
630
|
+
rerunLadder: immersive.rerunLadder,
|
|
558
631
|
fieldLabels: snap.fieldLabels,
|
|
559
632
|
boundFields: snap.boundFields,
|
|
560
633
|
lastApplied: snap.lastApplied,
|
|
@@ -574,22 +647,22 @@ function useBaseeraSignal() {
|
|
|
574
647
|
}
|
|
575
648
|
|
|
576
649
|
// src/use-knowledge-upload.ts
|
|
577
|
-
var
|
|
578
|
-
var
|
|
650
|
+
var import_browser5 = require("@baseera/core/browser");
|
|
651
|
+
var import_react8 = require("react");
|
|
579
652
|
var SERVER_STATE2 = { sources: null, uploads: [], loading: true, error: null, disabled: false };
|
|
580
653
|
function useBaseeraKnowledgeUpload(options) {
|
|
581
654
|
const { connection } = useBaseeraInternals();
|
|
582
|
-
const tokenRef = (0,
|
|
655
|
+
const tokenRef = (0, import_react8.useRef)(options.getUploadToken);
|
|
583
656
|
tokenRef.current = options.getUploadToken;
|
|
584
|
-
const [controller] = (0,
|
|
585
|
-
() => (0,
|
|
657
|
+
const [controller] = (0, import_react8.useState)(
|
|
658
|
+
() => (0, import_browser5.createTenantUploadController)({ publishableKey: connection.publishableKey, baseUrl: connection.baseUrl, getUploadToken: () => tokenRef.current(), ...options.pollMs ? { pollMs: options.pollMs } : {} })
|
|
586
659
|
);
|
|
587
|
-
const state = (0,
|
|
588
|
-
(0,
|
|
660
|
+
const state = (0, import_react8.useSyncExternalStore)(controller.subscribe, controller.getSnapshot, () => SERVER_STATE2);
|
|
661
|
+
(0, import_react8.useEffect)(() => {
|
|
589
662
|
controller.start();
|
|
590
663
|
return () => controller.stop();
|
|
591
664
|
}, [controller]);
|
|
592
|
-
return (0,
|
|
665
|
+
return (0, import_react8.useMemo)(
|
|
593
666
|
() => ({
|
|
594
667
|
...state,
|
|
595
668
|
upload: (files) => controller.upload(Array.from(files)),
|
|
@@ -601,7 +674,7 @@ function useBaseeraKnowledgeUpload(options) {
|
|
|
601
674
|
}
|
|
602
675
|
|
|
603
676
|
// src/use-field.ts
|
|
604
|
-
var
|
|
677
|
+
var import_react9 = require("react");
|
|
605
678
|
var MAX_REASON2 = 500;
|
|
606
679
|
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"])';
|
|
607
680
|
var SEVERITY_RANK = { info: 1, warning: 2, blocking: 3 };
|
|
@@ -619,14 +692,14 @@ function prefersReducedMotion() {
|
|
|
619
692
|
function useBaseeraField(name, options) {
|
|
620
693
|
const { immersive } = useBaseeraInternals();
|
|
621
694
|
const snap = useImmersiveSnapshot();
|
|
622
|
-
const elementRef = (0,
|
|
623
|
-
const onChangeRef = (0,
|
|
695
|
+
const elementRef = (0, import_react9.useRef)(null);
|
|
696
|
+
const onChangeRef = (0, import_react9.useRef)(options.onChange);
|
|
624
697
|
onChangeRef.current = options.onChange;
|
|
625
|
-
const focusControl = (0,
|
|
698
|
+
const focusControl = (0, import_react9.useCallback)(() => {
|
|
626
699
|
const el = elementRef.current;
|
|
627
700
|
if (el) firstControl(el)?.focus({ preventScroll: true });
|
|
628
701
|
}, []);
|
|
629
|
-
(0,
|
|
702
|
+
(0, import_react9.useEffect)(
|
|
630
703
|
() => immersive.bindField(name, {
|
|
631
704
|
write(value) {
|
|
632
705
|
onChangeRef.current(value);
|
|
@@ -640,12 +713,12 @@ function useBaseeraField(name, options) {
|
|
|
640
713
|
}),
|
|
641
714
|
[immersive, name]
|
|
642
715
|
);
|
|
643
|
-
const bindRef = (0,
|
|
716
|
+
const bindRef = (0, import_react9.useCallback)((element) => {
|
|
644
717
|
elementRef.current = element;
|
|
645
718
|
}, []);
|
|
646
719
|
const pending = snap.pendingDrafts.find((d) => d.field === name);
|
|
647
|
-
const draft = (0,
|
|
648
|
-
const flag = (0,
|
|
720
|
+
const draft = (0, import_react9.useMemo)(() => pending ? { value: pending.value, reason: pending.reason, source: pending.source } : null, [pending]);
|
|
721
|
+
const flag = (0, import_react9.useMemo)(() => {
|
|
649
722
|
let best = null;
|
|
650
723
|
for (const r of snap.results) {
|
|
651
724
|
if (r.status !== "open" || !r.fields.includes(name)) continue;
|
|
@@ -654,42 +727,42 @@ function useBaseeraField(name, options) {
|
|
|
654
727
|
return best;
|
|
655
728
|
}, [snap.results, name]);
|
|
656
729
|
const focusedAt = snap.focused?.field === name ? snap.focused.at : null;
|
|
657
|
-
const focused = (0,
|
|
658
|
-
const approve = (0,
|
|
730
|
+
const focused = (0, import_react9.useMemo)(() => focusedAt === null ? null : { at: focusedAt }, [focusedAt]);
|
|
731
|
+
const approve = (0, import_react9.useCallback)(() => {
|
|
659
732
|
const current = immersive.getSnapshot().pendingDrafts.find((d) => d.field === name);
|
|
660
733
|
if (!current) return;
|
|
661
734
|
const previous = immersive.readField(name);
|
|
662
735
|
onChangeRef.current(current.value);
|
|
663
736
|
immersive.resolveDraft(name, { approved: true }, { previous });
|
|
664
737
|
}, [immersive, name]);
|
|
665
|
-
const reject = (0,
|
|
738
|
+
const reject = (0, import_react9.useCallback)(
|
|
666
739
|
(reason) => {
|
|
667
740
|
const text = reason?.trim().slice(0, MAX_REASON2);
|
|
668
741
|
immersive.resolveDraft(name, { approved: false, ...text ? { reason: text } : {} });
|
|
669
742
|
},
|
|
670
743
|
[immersive, name]
|
|
671
744
|
);
|
|
672
|
-
return (0,
|
|
745
|
+
return (0, import_react9.useMemo)(() => ({ draft, flag, focused, approve, reject, bindRef, focusControl }), [draft, flag, focused, approve, reject, bindRef, focusControl]);
|
|
673
746
|
}
|
|
674
747
|
|
|
675
748
|
// src/index.ts
|
|
676
|
-
var
|
|
749
|
+
var import_browser6 = require("@baseera/core/browser");
|
|
677
750
|
|
|
678
751
|
// src/use-tool.ts
|
|
679
|
-
var
|
|
752
|
+
var import_react10 = require("react");
|
|
680
753
|
var import_zod = require("zod");
|
|
681
754
|
var NAME_PATTERN = /^[a-zA-Z0-9_.-]{1,64}$/;
|
|
682
755
|
var MAX_DESCRIPTION = 2e3;
|
|
683
756
|
var MAX_SCHEMA_BYTES = 16e3;
|
|
684
757
|
var MAX_ERROR = 1e3;
|
|
685
758
|
function useBaseeraToolDefinition(tool) {
|
|
686
|
-
const latest = (0,
|
|
759
|
+
const latest = (0, import_react10.useRef)(tool);
|
|
687
760
|
latest.current = tool;
|
|
688
761
|
useRegistration(tool.spec.name, latest);
|
|
689
762
|
}
|
|
690
763
|
function useRegistration(key, latest) {
|
|
691
764
|
const { registry } = useBaseeraInternals();
|
|
692
|
-
(0,
|
|
765
|
+
(0, import_react10.useEffect)(() => {
|
|
693
766
|
const registered = {
|
|
694
767
|
get spec() {
|
|
695
768
|
return latest.current.spec;
|
|
@@ -744,7 +817,7 @@ function summarize(error) {
|
|
|
744
817
|
}
|
|
745
818
|
function useBaseeraTool(options) {
|
|
746
819
|
const { name, description, input, kind } = options;
|
|
747
|
-
const spec = (0,
|
|
820
|
+
const spec = (0, import_react10.useMemo)(() => {
|
|
748
821
|
if (typeof name !== "string" || !NAME_PATTERN.test(name)) {
|
|
749
822
|
throw new Error(`useBaseeraTool: invalid name ${JSON.stringify(name)}. Use 1 to 64 letters, digits, "_", "." or "-".`);
|
|
750
823
|
}
|
|
@@ -753,11 +826,11 @@ function useBaseeraTool(options) {
|
|
|
753
826
|
}
|
|
754
827
|
return { name, description, kind, inputSchema: toWireSchema(name, input) };
|
|
755
828
|
}, [name, description, kind, input]);
|
|
756
|
-
const optionsRef = (0,
|
|
829
|
+
const optionsRef = (0, import_react10.useRef)(options);
|
|
757
830
|
optionsRef.current = options;
|
|
758
|
-
const specRef = (0,
|
|
831
|
+
const specRef = (0, import_react10.useRef)(spec);
|
|
759
832
|
specRef.current = spec;
|
|
760
|
-
const latest = (0,
|
|
833
|
+
const latest = (0, import_react10.useRef)(null);
|
|
761
834
|
latest.current ??= {
|
|
762
835
|
get spec() {
|
|
763
836
|
return specRef.current;
|
|
@@ -772,7 +845,7 @@ function useBaseeraTool(options) {
|
|
|
772
845
|
return optionsRef.current.undo ? (value, output) => optionsRef.current.undo?.(value, output) : void 0;
|
|
773
846
|
}
|
|
774
847
|
};
|
|
775
|
-
const key = (0,
|
|
848
|
+
const key = (0, import_react10.useMemo)(() => JSON.stringify([spec.name, spec.kind, spec.description]), [spec]);
|
|
776
849
|
useRegistration(`${key}:${schemaId(input)}`, latest);
|
|
777
850
|
}
|
|
778
851
|
var ids = /* @__PURE__ */ new WeakMap();
|
|
@@ -784,7 +857,7 @@ function schemaId(schema) {
|
|
|
784
857
|
}
|
|
785
858
|
|
|
786
859
|
// src/index.ts
|
|
787
|
-
var
|
|
860
|
+
var import_browser7 = require("@baseera/core/browser");
|
|
788
861
|
// Annotate the CommonJS export names for ESM import in node:
|
|
789
862
|
0 && (module.exports = {
|
|
790
863
|
BaseeraProvider,
|
|
@@ -797,6 +870,7 @@ var import_core7 = require("@baseera/core");
|
|
|
797
870
|
firstControl,
|
|
798
871
|
useBaseeraAgentTheme,
|
|
799
872
|
useBaseeraApprovals,
|
|
873
|
+
useBaseeraAttentionNext,
|
|
800
874
|
useBaseeraChat,
|
|
801
875
|
useBaseeraContext,
|
|
802
876
|
useBaseeraFeedback,
|