@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/embed.cjs CHANGED
@@ -72,7 +72,10 @@ function parseBootstrapResponse(value, indexId, now) {
72
72
  if (!Number.isFinite(expiresAt) || expiresAt <= now) {
73
73
  throw new Error("Agent Runtime returned an expired access response.");
74
74
  }
75
- const refreshSkew = Math.min(MAX_REFRESH_SKEW_MS, Math.floor((expiresAt - now) / 10));
75
+ const refreshSkew = Math.min(
76
+ MAX_REFRESH_SKEW_MS,
77
+ Math.floor((expiresAt - now) / 10)
78
+ );
76
79
  return {
77
80
  accessToken: value.accessToken,
78
81
  refreshAt: expiresAt - refreshSkew
@@ -93,15 +96,28 @@ function createAgentRuntimeCapability(options) {
93
96
  let capability;
94
97
  let pendingBootstrap;
95
98
  const bootstrap = async () => {
96
- const response = await fetchImplementation(`${options.runtimeOrigin}/webless/v1/bootstrap`, {
97
- body: JSON.stringify({
98
- clientSessionId: options.visitorSessionId,
99
- indexId: options.indexId,
100
- version: options.version
101
- }),
102
- headers: { "content-type": "application/json" },
103
- method: "POST"
104
- });
99
+ let previewGrant;
100
+ if (options.version === "unpublished") {
101
+ previewGrant = (await options.getUnpublishedPreviewGrant?.())?.trim();
102
+ if (!previewGrant) {
103
+ throw new Error(
104
+ "An unpublished preview grant is required to use the Agent Runtime preview."
105
+ );
106
+ }
107
+ }
108
+ const response = await fetchImplementation(
109
+ `${options.runtimeOrigin}/webless/v1/bootstrap`,
110
+ {
111
+ body: JSON.stringify({
112
+ clientSessionId: options.visitorSessionId,
113
+ indexId: options.indexId,
114
+ ...previewGrant ? { previewGrant } : {},
115
+ version: options.version
116
+ }),
117
+ headers: { "content-type": "application/json" },
118
+ method: "POST"
119
+ }
120
+ );
105
121
  if (!response.ok) {
106
122
  throw new Error(await readBootstrapError(response));
107
123
  }
@@ -295,13 +311,14 @@ function mapStepLabel(event) {
295
311
  };
296
312
  }
297
313
  var AgentSession = class {
298
- constructor(indexId, version, runtimeOrigin, visitorSessionId, storeOptions) {
314
+ constructor(indexId, version, runtimeOrigin, visitorSessionId, storeOptions, getUnpublishedPreviewGrant) {
299
315
  this.indexId = indexId;
300
316
  this.version = version;
301
317
  this.runtimeOrigin = runtimeOrigin;
302
318
  this.visitorSessionId = visitorSessionId;
303
319
  this.storeOptions = storeOptions;
304
320
  this.capability = createAgentRuntimeCapability({
321
+ getUnpublishedPreviewGrant,
305
322
  indexId,
306
323
  runtimeOrigin,
307
324
  version,
@@ -364,7 +381,10 @@ var AgentSession = class {
364
381
  return this.client;
365
382
  }
366
383
  attachPersistedSession(client) {
367
- const persisted = loadPersistedAgentSession(this.visitorSessionId, this.storeOptions);
384
+ const persisted = loadPersistedAgentSession(
385
+ this.visitorSessionId,
386
+ this.storeOptions
387
+ );
368
388
  if (!persisted?.sessionId) return void 0;
369
389
  return client.sessions.attach(persisted.sessionId, {
370
390
  streamIndex: persisted.streamIndex
@@ -431,7 +451,10 @@ var AgentSession = class {
431
451
  return rendered.trim();
432
452
  }
433
453
  async resumeTurn(message, signal, handlers, initialText = "") {
434
- const persisted = loadPersistedAgentSession(this.visitorSessionId, this.storeOptions);
454
+ const persisted = loadPersistedAgentSession(
455
+ this.visitorSessionId,
456
+ this.storeOptions
457
+ );
435
458
  if (!persisted) return null;
436
459
  const client = this.ensureClient();
437
460
  const attached = client.sessions.attach(persisted.sessionId, {
@@ -470,7 +493,9 @@ var AgentSession = class {
470
493
  }
471
494
  if (snapshotBoundary) {
472
495
  if (snapshotBoundary.type === "session.failed") {
473
- throw new Error(snapshotBoundary.data.message || snapshotBoundary.data.code);
496
+ throw new Error(
497
+ snapshotBoundary.data.message || snapshotBoundary.data.code
498
+ );
474
499
  }
475
500
  handlers.onComplete?.();
476
501
  if (!rendered.trim()) throw new Error("Empty response from runtime");
@@ -527,7 +552,14 @@ function createAgentClient(options) {
527
552
  })
528
553
  };
529
554
  const visitorSessionId = options.visitorSessionId?.trim() || getOrCreateVisitorSessionId(storeOptions);
530
- const session = new AgentSession(indexId, version, runtimeOrigin, visitorSessionId, storeOptions);
555
+ const session = new AgentSession(
556
+ indexId,
557
+ version,
558
+ runtimeOrigin,
559
+ visitorSessionId,
560
+ storeOptions,
561
+ options.getUnpublishedPreviewGrant
562
+ );
531
563
  return {
532
564
  indexId,
533
565
  version,
@@ -668,24 +700,50 @@ function delay(ms, signal) {
668
700
  }
669
701
  async function runStatusSequence(signal, onStep) {
670
702
  for (const item of STATUS_SEQUENCE) {
671
- onStep({ id: item.id, label: item.label, detail: item.detail, state: "active" });
703
+ onStep({
704
+ id: item.id,
705
+ label: item.label,
706
+ detail: item.detail,
707
+ state: "active"
708
+ });
672
709
  await delay(item.ms, signal);
673
710
  }
674
711
  }
675
712
  function useAgentChat({
676
713
  customerId,
714
+ getUnpublishedPreviewGrant,
677
715
  indexId,
678
716
  version,
679
717
  runtimeOrigin,
680
718
  visitorSessionId,
681
719
  storageKeyPrefix
682
720
  }) {
721
+ const previewGrantProviderRef = (0, import_react.useRef)(getUnpublishedPreviewGrant);
722
+ previewGrantProviderRef.current = getUnpublishedPreviewGrant;
723
+ const resolveUnpublishedPreviewGrant = () => {
724
+ const provider = previewGrantProviderRef.current;
725
+ if (!provider) {
726
+ return Promise.reject(
727
+ new Error(
728
+ "An unpublished preview grant is required to use the Agent Runtime preview."
729
+ )
730
+ );
731
+ }
732
+ return provider();
733
+ };
683
734
  const resolvedStorageKeyPrefix = (0, import_react.useMemo)(
684
- () => storageKeyPrefix?.trim() || buildAgentStorageKeyPrefix({ customerId, indexId, version, runtimeOrigin }),
735
+ () => storageKeyPrefix?.trim() || buildAgentStorageKeyPrefix({
736
+ customerId,
737
+ indexId,
738
+ version,
739
+ runtimeOrigin
740
+ }),
685
741
  [customerId, indexId, runtimeOrigin, storageKeyPrefix, version]
686
742
  );
687
743
  const visitorId = (0, import_react.useMemo)(
688
- () => visitorSessionId?.trim() || getOrCreateVisitorSessionId({ storageKeyPrefix: resolvedStorageKeyPrefix }),
744
+ () => visitorSessionId?.trim() || getOrCreateVisitorSessionId({
745
+ storageKeyPrefix: resolvedStorageKeyPrefix
746
+ }),
689
747
  [resolvedStorageKeyPrefix, visitorSessionId]
690
748
  );
691
749
  const [state, setState] = (0, import_react.useState)(
@@ -697,6 +755,7 @@ function useAgentChat({
697
755
  const clientRef = (0, import_react.useRef)(
698
756
  createAgentClient({
699
757
  customerId,
758
+ getUnpublishedPreviewGrant: resolveUnpublishedPreviewGrant,
700
759
  indexId,
701
760
  version,
702
761
  runtimeOrigin,
@@ -715,6 +774,7 @@ function useAgentChat({
715
774
  runRef.current = null;
716
775
  clientRef.current = createAgentClient({
717
776
  customerId,
777
+ getUnpublishedPreviewGrant: resolveUnpublishedPreviewGrant,
718
778
  indexId,
719
779
  version,
720
780
  runtimeOrigin,
@@ -726,7 +786,15 @@ function useAgentChat({
726
786
  loadPersistedAgentConversation(resolvedStorageKeyPrefix, visitorId)
727
787
  )
728
788
  );
729
- }, [customerId, identityKey, indexId, runtimeOrigin, resolvedStorageKeyPrefix, version, visitorId]);
789
+ }, [
790
+ customerId,
791
+ identityKey,
792
+ indexId,
793
+ runtimeOrigin,
794
+ resolvedStorageKeyPrefix,
795
+ version,
796
+ visitorId
797
+ ]);
730
798
  (0, import_react.useEffect)(() => {
731
799
  if (!hasVisitorMessages(state.messages)) return;
732
800
  savePersistedAgentConversation(resolvedStorageKeyPrefix, visitorId, {
@@ -734,7 +802,13 @@ function useAgentChat({
734
802
  pending: isAgentBusy(state.phase),
735
803
  streamingText: state.streamingText
736
804
  });
737
- }, [resolvedStorageKeyPrefix, state.messages, state.phase, state.streamingText, visitorId]);
805
+ }, [
806
+ resolvedStorageKeyPrefix,
807
+ state.messages,
808
+ state.phase,
809
+ state.streamingText,
810
+ visitorId
811
+ ]);
738
812
  const reset = (0, import_react.useCallback)(() => {
739
813
  runRef.current?.abort();
740
814
  runRef.current = null;
@@ -752,7 +826,11 @@ function useAgentChat({
752
826
  let streamed = initialText;
753
827
  const planningPromise = resume ? Promise.resolve() : runStatusSequence(signal, (step) => {
754
828
  if (streamStarted || !isActiveRun()) return;
755
- setState((prev) => ({ ...prev, phase: "running-tools", toolSteps: [step] }));
829
+ setState((prev) => ({
830
+ ...prev,
831
+ phase: "running-tools",
832
+ toolSteps: [step]
833
+ }));
756
834
  });
757
835
  const handlers = {
758
836
  onStep: (label, detail) => {
@@ -760,7 +838,9 @@ function useAgentChat({
760
838
  setState((prev) => ({
761
839
  ...prev,
762
840
  phase: "running-tools",
763
- toolSteps: [{ id: `step-${label}`, label, detail, state: "active" }]
841
+ toolSteps: [
842
+ { id: `step-${label}`, label, detail, state: "active" }
843
+ ]
764
844
  }));
765
845
  },
766
846
  onDelta: (delta) => {
@@ -777,7 +857,11 @@ function useAgentChat({
777
857
  }));
778
858
  }
779
859
  streamed += delta;
780
- setState((prev) => ({ ...prev, phase: "streaming", streamingText: streamed }));
860
+ setState((prev) => ({
861
+ ...prev,
862
+ phase: "streaming",
863
+ streamingText: streamed
864
+ }));
781
865
  },
782
866
  onComplete: () => {
783
867
  if (!isActiveRun()) return;
@@ -791,7 +875,10 @@ function useAgentChat({
791
875
  signal
792
876
  }) : await clientRef.current.sendTurn(visitorText, { handlers, signal });
793
877
  if (resume && finalText === null) {
794
- finalText = await clientRef.current.sendTurn(visitorText, { handlers, signal });
878
+ finalText = await clientRef.current.sendTurn(visitorText, {
879
+ handlers,
880
+ signal
881
+ });
795
882
  }
796
883
  await planningPromise.catch(() => {
797
884
  });
@@ -813,7 +900,8 @@ function useAgentChat({
813
900
  }));
814
901
  runRef.current = null;
815
902
  } catch (error) {
816
- if (error instanceof DOMException && error.name === "AbortError") return;
903
+ if (error instanceof DOMException && error.name === "AbortError")
904
+ return;
817
905
  if (!isActiveRun()) return;
818
906
  const message = formatAgentError(error);
819
907
  if (!message) return;
@@ -847,7 +935,9 @@ function useAgentChat({
847
935
  ...prev,
848
936
  phase: "thinking",
849
937
  messages: [...prev.messages, visitorMessage],
850
- toolSteps: [{ id: "s1", label: "Starting Eve session", state: "active" }],
938
+ toolSteps: [
939
+ { id: "s1", label: "Starting Eve session", state: "active" }
940
+ ],
851
941
  journey: null,
852
942
  followUps: [],
853
943
  streamingText: "",
@@ -1355,6 +1445,7 @@ var import_jsx_runtime8 = require("react/jsx-runtime");
1355
1445
  function AgentWidget({
1356
1446
  indexId,
1357
1447
  customerId,
1448
+ getUnpublishedPreviewGrant,
1358
1449
  version,
1359
1450
  runtimeOrigin,
1360
1451
  placement: placementInput,
@@ -1367,6 +1458,7 @@ function AgentWidget({
1367
1458
  const [railExpanded, setRailExpanded] = (0, import_react5.useState)(false);
1368
1459
  const { state, submit } = useAgentChat({
1369
1460
  customerId,
1461
+ getUnpublishedPreviewGrant,
1370
1462
  indexId,
1371
1463
  version,
1372
1464
  runtimeOrigin