@webless/agent 0.2.11 → 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,14 +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
- }),
85
- headers: { "content-type": "application/json" },
86
- method: "POST"
87
- });
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
+ );
88
105
  if (!response.ok) {
89
106
  throw new Error(await readBootstrapError(response));
90
107
  }
@@ -278,15 +295,17 @@ function mapStepLabel(event) {
278
295
  };
279
296
  }
280
297
  var AgentSession = class {
281
- constructor(indexId, version, runtimeOrigin, visitorSessionId, storeOptions) {
298
+ constructor(indexId, version, runtimeOrigin, visitorSessionId, storeOptions, getUnpublishedPreviewGrant) {
282
299
  this.indexId = indexId;
283
300
  this.version = version;
284
301
  this.runtimeOrigin = runtimeOrigin;
285
302
  this.visitorSessionId = visitorSessionId;
286
303
  this.storeOptions = storeOptions;
287
304
  this.capability = createAgentRuntimeCapability({
305
+ getUnpublishedPreviewGrant,
288
306
  indexId,
289
307
  runtimeOrigin,
308
+ version,
290
309
  visitorSessionId
291
310
  });
292
311
  }
@@ -346,7 +365,10 @@ var AgentSession = class {
346
365
  return this.client;
347
366
  }
348
367
  attachPersistedSession(client) {
349
- const persisted = loadPersistedAgentSession(this.visitorSessionId, this.storeOptions);
368
+ const persisted = loadPersistedAgentSession(
369
+ this.visitorSessionId,
370
+ this.storeOptions
371
+ );
350
372
  if (!persisted?.sessionId) return void 0;
351
373
  return client.sessions.attach(persisted.sessionId, {
352
374
  streamIndex: persisted.streamIndex
@@ -413,7 +435,10 @@ var AgentSession = class {
413
435
  return rendered.trim();
414
436
  }
415
437
  async resumeTurn(message, signal, handlers, initialText = "") {
416
- const persisted = loadPersistedAgentSession(this.visitorSessionId, this.storeOptions);
438
+ const persisted = loadPersistedAgentSession(
439
+ this.visitorSessionId,
440
+ this.storeOptions
441
+ );
417
442
  if (!persisted) return null;
418
443
  const client = this.ensureClient();
419
444
  const attached = client.sessions.attach(persisted.sessionId, {
@@ -452,7 +477,9 @@ var AgentSession = class {
452
477
  }
453
478
  if (snapshotBoundary) {
454
479
  if (snapshotBoundary.type === "session.failed") {
455
- throw new Error(snapshotBoundary.data.message || snapshotBoundary.data.code);
480
+ throw new Error(
481
+ snapshotBoundary.data.message || snapshotBoundary.data.code
482
+ );
456
483
  }
457
484
  handlers.onComplete?.();
458
485
  if (!rendered.trim()) throw new Error("Empty response from runtime");
@@ -509,7 +536,14 @@ function createAgentClient(options) {
509
536
  })
510
537
  };
511
538
  const visitorSessionId = options.visitorSessionId?.trim() || getOrCreateVisitorSessionId(storeOptions);
512
- 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
+ );
513
547
  return {
514
548
  indexId,
515
549
  version,
@@ -650,24 +684,50 @@ function delay(ms, signal) {
650
684
  }
651
685
  async function runStatusSequence(signal, onStep) {
652
686
  for (const item of STATUS_SEQUENCE) {
653
- 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
+ });
654
693
  await delay(item.ms, signal);
655
694
  }
656
695
  }
657
696
  function useAgentChat({
658
697
  customerId,
698
+ getUnpublishedPreviewGrant,
659
699
  indexId,
660
700
  version,
661
701
  runtimeOrigin,
662
702
  visitorSessionId,
663
703
  storageKeyPrefix
664
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
+ };
665
718
  const resolvedStorageKeyPrefix = (0, import_react.useMemo)(
666
- () => storageKeyPrefix?.trim() || buildAgentStorageKeyPrefix({ customerId, indexId, version, runtimeOrigin }),
719
+ () => storageKeyPrefix?.trim() || buildAgentStorageKeyPrefix({
720
+ customerId,
721
+ indexId,
722
+ version,
723
+ runtimeOrigin
724
+ }),
667
725
  [customerId, indexId, runtimeOrigin, storageKeyPrefix, version]
668
726
  );
669
727
  const visitorId = (0, import_react.useMemo)(
670
- () => visitorSessionId?.trim() || getOrCreateVisitorSessionId({ storageKeyPrefix: resolvedStorageKeyPrefix }),
728
+ () => visitorSessionId?.trim() || getOrCreateVisitorSessionId({
729
+ storageKeyPrefix: resolvedStorageKeyPrefix
730
+ }),
671
731
  [resolvedStorageKeyPrefix, visitorSessionId]
672
732
  );
673
733
  const [state, setState] = (0, import_react.useState)(
@@ -679,6 +739,7 @@ function useAgentChat({
679
739
  const clientRef = (0, import_react.useRef)(
680
740
  createAgentClient({
681
741
  customerId,
742
+ getUnpublishedPreviewGrant: resolveUnpublishedPreviewGrant,
682
743
  indexId,
683
744
  version,
684
745
  runtimeOrigin,
@@ -697,6 +758,7 @@ function useAgentChat({
697
758
  runRef.current = null;
698
759
  clientRef.current = createAgentClient({
699
760
  customerId,
761
+ getUnpublishedPreviewGrant: resolveUnpublishedPreviewGrant,
700
762
  indexId,
701
763
  version,
702
764
  runtimeOrigin,
@@ -708,7 +770,15 @@ function useAgentChat({
708
770
  loadPersistedAgentConversation(resolvedStorageKeyPrefix, visitorId)
709
771
  )
710
772
  );
711
- }, [customerId, identityKey, indexId, runtimeOrigin, resolvedStorageKeyPrefix, version, visitorId]);
773
+ }, [
774
+ customerId,
775
+ identityKey,
776
+ indexId,
777
+ runtimeOrigin,
778
+ resolvedStorageKeyPrefix,
779
+ version,
780
+ visitorId
781
+ ]);
712
782
  (0, import_react.useEffect)(() => {
713
783
  if (!hasVisitorMessages(state.messages)) return;
714
784
  savePersistedAgentConversation(resolvedStorageKeyPrefix, visitorId, {
@@ -716,7 +786,13 @@ function useAgentChat({
716
786
  pending: isAgentBusy(state.phase),
717
787
  streamingText: state.streamingText
718
788
  });
719
- }, [resolvedStorageKeyPrefix, state.messages, state.phase, state.streamingText, visitorId]);
789
+ }, [
790
+ resolvedStorageKeyPrefix,
791
+ state.messages,
792
+ state.phase,
793
+ state.streamingText,
794
+ visitorId
795
+ ]);
720
796
  const reset = (0, import_react.useCallback)(() => {
721
797
  runRef.current?.abort();
722
798
  runRef.current = null;
@@ -734,7 +810,11 @@ function useAgentChat({
734
810
  let streamed = initialText;
735
811
  const planningPromise = resume ? Promise.resolve() : runStatusSequence(signal, (step) => {
736
812
  if (streamStarted || !isActiveRun()) return;
737
- setState((prev) => ({ ...prev, phase: "running-tools", toolSteps: [step] }));
813
+ setState((prev) => ({
814
+ ...prev,
815
+ phase: "running-tools",
816
+ toolSteps: [step]
817
+ }));
738
818
  });
739
819
  const handlers = {
740
820
  onStep: (label, detail) => {
@@ -742,7 +822,9 @@ function useAgentChat({
742
822
  setState((prev) => ({
743
823
  ...prev,
744
824
  phase: "running-tools",
745
- toolSteps: [{ id: `step-${label}`, label, detail, state: "active" }]
825
+ toolSteps: [
826
+ { id: `step-${label}`, label, detail, state: "active" }
827
+ ]
746
828
  }));
747
829
  },
748
830
  onDelta: (delta) => {
@@ -759,7 +841,11 @@ function useAgentChat({
759
841
  }));
760
842
  }
761
843
  streamed += delta;
762
- setState((prev) => ({ ...prev, phase: "streaming", streamingText: streamed }));
844
+ setState((prev) => ({
845
+ ...prev,
846
+ phase: "streaming",
847
+ streamingText: streamed
848
+ }));
763
849
  },
764
850
  onComplete: () => {
765
851
  if (!isActiveRun()) return;
@@ -773,7 +859,10 @@ function useAgentChat({
773
859
  signal
774
860
  }) : await clientRef.current.sendTurn(visitorText, { handlers, signal });
775
861
  if (resume && finalText === null) {
776
- finalText = await clientRef.current.sendTurn(visitorText, { handlers, signal });
862
+ finalText = await clientRef.current.sendTurn(visitorText, {
863
+ handlers,
864
+ signal
865
+ });
777
866
  }
778
867
  await planningPromise.catch(() => {
779
868
  });
@@ -795,7 +884,8 @@ function useAgentChat({
795
884
  }));
796
885
  runRef.current = null;
797
886
  } catch (error) {
798
- if (error instanceof DOMException && error.name === "AbortError") return;
887
+ if (error instanceof DOMException && error.name === "AbortError")
888
+ return;
799
889
  if (!isActiveRun()) return;
800
890
  const message = formatAgentError(error);
801
891
  if (!message) return;
@@ -829,7 +919,9 @@ function useAgentChat({
829
919
  ...prev,
830
920
  phase: "thinking",
831
921
  messages: [...prev.messages, visitorMessage],
832
- toolSteps: [{ id: "s1", label: "Starting Eve session", state: "active" }],
922
+ toolSteps: [
923
+ { id: "s1", label: "Starting Eve session", state: "active" }
924
+ ],
833
925
  journey: null,
834
926
  followUps: [],
835
927
  streamingText: "",
@@ -1346,6 +1438,7 @@ var import_jsx_runtime8 = require("react/jsx-runtime");
1346
1438
  function AgentWidget({
1347
1439
  indexId,
1348
1440
  customerId,
1441
+ getUnpublishedPreviewGrant,
1349
1442
  version,
1350
1443
  runtimeOrigin,
1351
1444
  placement: placementInput,
@@ -1358,6 +1451,7 @@ function AgentWidget({
1358
1451
  const [railExpanded, setRailExpanded] = (0, import_react5.useState)(false);
1359
1452
  const { state, submit } = useAgentChat({
1360
1453
  customerId,
1454
+ getUnpublishedPreviewGrant,
1361
1455
  indexId,
1362
1456
  version,
1363
1457
  runtimeOrigin