@webless/agent 0.2.12 → 0.2.13

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/react.cjs CHANGED
@@ -56,7 +56,10 @@ function parseBootstrapResponse(value, indexId, now) {
56
56
  if (!Number.isFinite(expiresAt) || expiresAt <= now) {
57
57
  throw new Error("Agent Runtime returned an expired access response.");
58
58
  }
59
- const refreshSkew = Math.min(MAX_REFRESH_SKEW_MS, Math.floor((expiresAt - now) / 10));
59
+ const refreshSkew = Math.min(
60
+ MAX_REFRESH_SKEW_MS,
61
+ Math.floor((expiresAt - now) / 10)
62
+ );
60
63
  return {
61
64
  accessToken: value.accessToken,
62
65
  refreshAt: expiresAt - refreshSkew
@@ -77,15 +80,28 @@ function createAgentRuntimeCapability(options) {
77
80
  let capability;
78
81
  let pendingBootstrap;
79
82
  const bootstrap = async () => {
80
- const response = await fetchImplementation(`${options.runtimeOrigin}/webless/v1/bootstrap`, {
81
- body: JSON.stringify({
82
- clientSessionId: options.visitorSessionId,
83
- indexId: options.indexId,
84
- version: options.version
85
- }),
86
- headers: { "content-type": "application/json" },
87
- method: "POST"
88
- });
83
+ let previewGrant;
84
+ if (options.version === "unpublished") {
85
+ previewGrant = (await options.getUnpublishedPreviewGrant?.())?.trim();
86
+ if (!previewGrant) {
87
+ throw new Error(
88
+ "An unpublished preview grant is required to use the Agent Runtime preview."
89
+ );
90
+ }
91
+ }
92
+ const response = await fetchImplementation(
93
+ `${options.runtimeOrigin}/webless/v1/bootstrap`,
94
+ {
95
+ body: JSON.stringify({
96
+ clientSessionId: options.visitorSessionId,
97
+ indexId: options.indexId,
98
+ ...previewGrant ? { previewGrant } : {},
99
+ version: options.version
100
+ }),
101
+ headers: { "content-type": "application/json" },
102
+ method: "POST"
103
+ }
104
+ );
89
105
  if (!response.ok) {
90
106
  throw new Error(await readBootstrapError(response));
91
107
  }
@@ -279,13 +295,14 @@ function mapStepLabel(event) {
279
295
  };
280
296
  }
281
297
  var AgentSession = class {
282
- constructor(indexId, version, runtimeOrigin, visitorSessionId, storeOptions) {
298
+ constructor(indexId, version, runtimeOrigin, visitorSessionId, storeOptions, getUnpublishedPreviewGrant) {
283
299
  this.indexId = indexId;
284
300
  this.version = version;
285
301
  this.runtimeOrigin = runtimeOrigin;
286
302
  this.visitorSessionId = visitorSessionId;
287
303
  this.storeOptions = storeOptions;
288
304
  this.capability = createAgentRuntimeCapability({
305
+ getUnpublishedPreviewGrant,
289
306
  indexId,
290
307
  runtimeOrigin,
291
308
  version,
@@ -348,7 +365,10 @@ var AgentSession = class {
348
365
  return this.client;
349
366
  }
350
367
  attachPersistedSession(client) {
351
- const persisted = loadPersistedAgentSession(this.visitorSessionId, this.storeOptions);
368
+ const persisted = loadPersistedAgentSession(
369
+ this.visitorSessionId,
370
+ this.storeOptions
371
+ );
352
372
  if (!persisted?.sessionId) return void 0;
353
373
  return client.sessions.attach(persisted.sessionId, {
354
374
  streamIndex: persisted.streamIndex
@@ -415,7 +435,10 @@ var AgentSession = class {
415
435
  return rendered.trim();
416
436
  }
417
437
  async resumeTurn(message, signal, handlers, initialText = "") {
418
- const persisted = loadPersistedAgentSession(this.visitorSessionId, this.storeOptions);
438
+ const persisted = loadPersistedAgentSession(
439
+ this.visitorSessionId,
440
+ this.storeOptions
441
+ );
419
442
  if (!persisted) return null;
420
443
  const client = this.ensureClient();
421
444
  const attached = client.sessions.attach(persisted.sessionId, {
@@ -454,7 +477,9 @@ var AgentSession = class {
454
477
  }
455
478
  if (snapshotBoundary) {
456
479
  if (snapshotBoundary.type === "session.failed") {
457
- throw new Error(snapshotBoundary.data.message || snapshotBoundary.data.code);
480
+ throw new Error(
481
+ snapshotBoundary.data.message || snapshotBoundary.data.code
482
+ );
458
483
  }
459
484
  handlers.onComplete?.();
460
485
  if (!rendered.trim()) throw new Error("Empty response from runtime");
@@ -511,7 +536,14 @@ function createAgentClient(options) {
511
536
  })
512
537
  };
513
538
  const visitorSessionId = options.visitorSessionId?.trim() || getOrCreateVisitorSessionId(storeOptions);
514
- const session = new AgentSession(indexId, version, runtimeOrigin, visitorSessionId, storeOptions);
539
+ const session = new AgentSession(
540
+ indexId,
541
+ version,
542
+ runtimeOrigin,
543
+ visitorSessionId,
544
+ storeOptions,
545
+ options.getUnpublishedPreviewGrant
546
+ );
515
547
  return {
516
548
  indexId,
517
549
  version,
@@ -652,24 +684,50 @@ function delay(ms, signal) {
652
684
  }
653
685
  async function runStatusSequence(signal, onStep) {
654
686
  for (const item of STATUS_SEQUENCE) {
655
- onStep({ id: item.id, label: item.label, detail: item.detail, state: "active" });
687
+ onStep({
688
+ id: item.id,
689
+ label: item.label,
690
+ detail: item.detail,
691
+ state: "active"
692
+ });
656
693
  await delay(item.ms, signal);
657
694
  }
658
695
  }
659
696
  function useAgentChat({
660
697
  customerId,
698
+ getUnpublishedPreviewGrant,
661
699
  indexId,
662
700
  version,
663
701
  runtimeOrigin,
664
702
  visitorSessionId,
665
703
  storageKeyPrefix
666
704
  }) {
705
+ const previewGrantProviderRef = (0, import_react.useRef)(getUnpublishedPreviewGrant);
706
+ previewGrantProviderRef.current = getUnpublishedPreviewGrant;
707
+ const resolveUnpublishedPreviewGrant = () => {
708
+ const provider = previewGrantProviderRef.current;
709
+ if (!provider) {
710
+ return Promise.reject(
711
+ new Error(
712
+ "An unpublished preview grant is required to use the Agent Runtime preview."
713
+ )
714
+ );
715
+ }
716
+ return provider();
717
+ };
667
718
  const resolvedStorageKeyPrefix = (0, import_react.useMemo)(
668
- () => storageKeyPrefix?.trim() || buildAgentStorageKeyPrefix({ customerId, indexId, version, runtimeOrigin }),
719
+ () => storageKeyPrefix?.trim() || buildAgentStorageKeyPrefix({
720
+ customerId,
721
+ indexId,
722
+ version,
723
+ runtimeOrigin
724
+ }),
669
725
  [customerId, indexId, runtimeOrigin, storageKeyPrefix, version]
670
726
  );
671
727
  const visitorId = (0, import_react.useMemo)(
672
- () => visitorSessionId?.trim() || getOrCreateVisitorSessionId({ storageKeyPrefix: resolvedStorageKeyPrefix }),
728
+ () => visitorSessionId?.trim() || getOrCreateVisitorSessionId({
729
+ storageKeyPrefix: resolvedStorageKeyPrefix
730
+ }),
673
731
  [resolvedStorageKeyPrefix, visitorSessionId]
674
732
  );
675
733
  const [state, setState] = (0, import_react.useState)(
@@ -681,6 +739,7 @@ function useAgentChat({
681
739
  const clientRef = (0, import_react.useRef)(
682
740
  createAgentClient({
683
741
  customerId,
742
+ getUnpublishedPreviewGrant: resolveUnpublishedPreviewGrant,
684
743
  indexId,
685
744
  version,
686
745
  runtimeOrigin,
@@ -699,6 +758,7 @@ function useAgentChat({
699
758
  runRef.current = null;
700
759
  clientRef.current = createAgentClient({
701
760
  customerId,
761
+ getUnpublishedPreviewGrant: resolveUnpublishedPreviewGrant,
702
762
  indexId,
703
763
  version,
704
764
  runtimeOrigin,
@@ -710,7 +770,15 @@ function useAgentChat({
710
770
  loadPersistedAgentConversation(resolvedStorageKeyPrefix, visitorId)
711
771
  )
712
772
  );
713
- }, [customerId, identityKey, indexId, runtimeOrigin, resolvedStorageKeyPrefix, version, visitorId]);
773
+ }, [
774
+ customerId,
775
+ identityKey,
776
+ indexId,
777
+ runtimeOrigin,
778
+ resolvedStorageKeyPrefix,
779
+ version,
780
+ visitorId
781
+ ]);
714
782
  (0, import_react.useEffect)(() => {
715
783
  if (!hasVisitorMessages(state.messages)) return;
716
784
  savePersistedAgentConversation(resolvedStorageKeyPrefix, visitorId, {
@@ -718,7 +786,13 @@ function useAgentChat({
718
786
  pending: isAgentBusy(state.phase),
719
787
  streamingText: state.streamingText
720
788
  });
721
- }, [resolvedStorageKeyPrefix, state.messages, state.phase, state.streamingText, visitorId]);
789
+ }, [
790
+ resolvedStorageKeyPrefix,
791
+ state.messages,
792
+ state.phase,
793
+ state.streamingText,
794
+ visitorId
795
+ ]);
722
796
  const reset = (0, import_react.useCallback)(() => {
723
797
  runRef.current?.abort();
724
798
  runRef.current = null;
@@ -736,7 +810,11 @@ function useAgentChat({
736
810
  let streamed = initialText;
737
811
  const planningPromise = resume ? Promise.resolve() : runStatusSequence(signal, (step) => {
738
812
  if (streamStarted || !isActiveRun()) return;
739
- setState((prev) => ({ ...prev, phase: "running-tools", toolSteps: [step] }));
813
+ setState((prev) => ({
814
+ ...prev,
815
+ phase: "running-tools",
816
+ toolSteps: [step]
817
+ }));
740
818
  });
741
819
  const handlers = {
742
820
  onStep: (label, detail) => {
@@ -744,7 +822,9 @@ function useAgentChat({
744
822
  setState((prev) => ({
745
823
  ...prev,
746
824
  phase: "running-tools",
747
- toolSteps: [{ id: `step-${label}`, label, detail, state: "active" }]
825
+ toolSteps: [
826
+ { id: `step-${label}`, label, detail, state: "active" }
827
+ ]
748
828
  }));
749
829
  },
750
830
  onDelta: (delta) => {
@@ -761,7 +841,11 @@ function useAgentChat({
761
841
  }));
762
842
  }
763
843
  streamed += delta;
764
- setState((prev) => ({ ...prev, phase: "streaming", streamingText: streamed }));
844
+ setState((prev) => ({
845
+ ...prev,
846
+ phase: "streaming",
847
+ streamingText: streamed
848
+ }));
765
849
  },
766
850
  onComplete: () => {
767
851
  if (!isActiveRun()) return;
@@ -775,7 +859,10 @@ function useAgentChat({
775
859
  signal
776
860
  }) : await clientRef.current.sendTurn(visitorText, { handlers, signal });
777
861
  if (resume && finalText === null) {
778
- finalText = await clientRef.current.sendTurn(visitorText, { handlers, signal });
862
+ finalText = await clientRef.current.sendTurn(visitorText, {
863
+ handlers,
864
+ signal
865
+ });
779
866
  }
780
867
  await planningPromise.catch(() => {
781
868
  });
@@ -797,7 +884,8 @@ function useAgentChat({
797
884
  }));
798
885
  runRef.current = null;
799
886
  } catch (error) {
800
- if (error instanceof DOMException && error.name === "AbortError") return;
887
+ if (error instanceof DOMException && error.name === "AbortError")
888
+ return;
801
889
  if (!isActiveRun()) return;
802
890
  const message = formatAgentError(error);
803
891
  if (!message) return;
@@ -831,7 +919,9 @@ function useAgentChat({
831
919
  ...prev,
832
920
  phase: "thinking",
833
921
  messages: [...prev.messages, visitorMessage],
834
- toolSteps: [{ id: "s1", label: "Starting Eve session", state: "active" }],
922
+ toolSteps: [
923
+ { id: "s1", label: "Starting Eve session", state: "active" }
924
+ ],
835
925
  journey: null,
836
926
  followUps: [],
837
927
  streamingText: "",
@@ -1348,6 +1438,7 @@ var import_jsx_runtime8 = require("react/jsx-runtime");
1348
1438
  function AgentWidget({
1349
1439
  indexId,
1350
1440
  customerId,
1441
+ getUnpublishedPreviewGrant,
1351
1442
  version,
1352
1443
  runtimeOrigin,
1353
1444
  placement: placementInput,
@@ -1360,6 +1451,7 @@ function AgentWidget({
1360
1451
  const [railExpanded, setRailExpanded] = (0, import_react5.useState)(false);
1361
1452
  const { state, submit } = useAgentChat({
1362
1453
  customerId,
1454
+ getUnpublishedPreviewGrant,
1363
1455
  indexId,
1364
1456
  version,
1365
1457
  runtimeOrigin