@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/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,14 +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
- }),
101
- headers: { "content-type": "application/json" },
102
- method: "POST"
103
- });
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
+ );
104
121
  if (!response.ok) {
105
122
  throw new Error(await readBootstrapError(response));
106
123
  }
@@ -294,15 +311,17 @@ function mapStepLabel(event) {
294
311
  };
295
312
  }
296
313
  var AgentSession = class {
297
- constructor(indexId, version, runtimeOrigin, visitorSessionId, storeOptions) {
314
+ constructor(indexId, version, runtimeOrigin, visitorSessionId, storeOptions, getUnpublishedPreviewGrant) {
298
315
  this.indexId = indexId;
299
316
  this.version = version;
300
317
  this.runtimeOrigin = runtimeOrigin;
301
318
  this.visitorSessionId = visitorSessionId;
302
319
  this.storeOptions = storeOptions;
303
320
  this.capability = createAgentRuntimeCapability({
321
+ getUnpublishedPreviewGrant,
304
322
  indexId,
305
323
  runtimeOrigin,
324
+ version,
306
325
  visitorSessionId
307
326
  });
308
327
  }
@@ -362,7 +381,10 @@ var AgentSession = class {
362
381
  return this.client;
363
382
  }
364
383
  attachPersistedSession(client) {
365
- const persisted = loadPersistedAgentSession(this.visitorSessionId, this.storeOptions);
384
+ const persisted = loadPersistedAgentSession(
385
+ this.visitorSessionId,
386
+ this.storeOptions
387
+ );
366
388
  if (!persisted?.sessionId) return void 0;
367
389
  return client.sessions.attach(persisted.sessionId, {
368
390
  streamIndex: persisted.streamIndex
@@ -429,7 +451,10 @@ var AgentSession = class {
429
451
  return rendered.trim();
430
452
  }
431
453
  async resumeTurn(message, signal, handlers, initialText = "") {
432
- const persisted = loadPersistedAgentSession(this.visitorSessionId, this.storeOptions);
454
+ const persisted = loadPersistedAgentSession(
455
+ this.visitorSessionId,
456
+ this.storeOptions
457
+ );
433
458
  if (!persisted) return null;
434
459
  const client = this.ensureClient();
435
460
  const attached = client.sessions.attach(persisted.sessionId, {
@@ -468,7 +493,9 @@ var AgentSession = class {
468
493
  }
469
494
  if (snapshotBoundary) {
470
495
  if (snapshotBoundary.type === "session.failed") {
471
- throw new Error(snapshotBoundary.data.message || snapshotBoundary.data.code);
496
+ throw new Error(
497
+ snapshotBoundary.data.message || snapshotBoundary.data.code
498
+ );
472
499
  }
473
500
  handlers.onComplete?.();
474
501
  if (!rendered.trim()) throw new Error("Empty response from runtime");
@@ -525,7 +552,14 @@ function createAgentClient(options) {
525
552
  })
526
553
  };
527
554
  const visitorSessionId = options.visitorSessionId?.trim() || getOrCreateVisitorSessionId(storeOptions);
528
- 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
+ );
529
563
  return {
530
564
  indexId,
531
565
  version,
@@ -666,24 +700,50 @@ function delay(ms, signal) {
666
700
  }
667
701
  async function runStatusSequence(signal, onStep) {
668
702
  for (const item of STATUS_SEQUENCE) {
669
- 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
+ });
670
709
  await delay(item.ms, signal);
671
710
  }
672
711
  }
673
712
  function useAgentChat({
674
713
  customerId,
714
+ getUnpublishedPreviewGrant,
675
715
  indexId,
676
716
  version,
677
717
  runtimeOrigin,
678
718
  visitorSessionId,
679
719
  storageKeyPrefix
680
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
+ };
681
734
  const resolvedStorageKeyPrefix = (0, import_react.useMemo)(
682
- () => storageKeyPrefix?.trim() || buildAgentStorageKeyPrefix({ customerId, indexId, version, runtimeOrigin }),
735
+ () => storageKeyPrefix?.trim() || buildAgentStorageKeyPrefix({
736
+ customerId,
737
+ indexId,
738
+ version,
739
+ runtimeOrigin
740
+ }),
683
741
  [customerId, indexId, runtimeOrigin, storageKeyPrefix, version]
684
742
  );
685
743
  const visitorId = (0, import_react.useMemo)(
686
- () => visitorSessionId?.trim() || getOrCreateVisitorSessionId({ storageKeyPrefix: resolvedStorageKeyPrefix }),
744
+ () => visitorSessionId?.trim() || getOrCreateVisitorSessionId({
745
+ storageKeyPrefix: resolvedStorageKeyPrefix
746
+ }),
687
747
  [resolvedStorageKeyPrefix, visitorSessionId]
688
748
  );
689
749
  const [state, setState] = (0, import_react.useState)(
@@ -695,6 +755,7 @@ function useAgentChat({
695
755
  const clientRef = (0, import_react.useRef)(
696
756
  createAgentClient({
697
757
  customerId,
758
+ getUnpublishedPreviewGrant: resolveUnpublishedPreviewGrant,
698
759
  indexId,
699
760
  version,
700
761
  runtimeOrigin,
@@ -713,6 +774,7 @@ function useAgentChat({
713
774
  runRef.current = null;
714
775
  clientRef.current = createAgentClient({
715
776
  customerId,
777
+ getUnpublishedPreviewGrant: resolveUnpublishedPreviewGrant,
716
778
  indexId,
717
779
  version,
718
780
  runtimeOrigin,
@@ -724,7 +786,15 @@ function useAgentChat({
724
786
  loadPersistedAgentConversation(resolvedStorageKeyPrefix, visitorId)
725
787
  )
726
788
  );
727
- }, [customerId, identityKey, indexId, runtimeOrigin, resolvedStorageKeyPrefix, version, visitorId]);
789
+ }, [
790
+ customerId,
791
+ identityKey,
792
+ indexId,
793
+ runtimeOrigin,
794
+ resolvedStorageKeyPrefix,
795
+ version,
796
+ visitorId
797
+ ]);
728
798
  (0, import_react.useEffect)(() => {
729
799
  if (!hasVisitorMessages(state.messages)) return;
730
800
  savePersistedAgentConversation(resolvedStorageKeyPrefix, visitorId, {
@@ -732,7 +802,13 @@ function useAgentChat({
732
802
  pending: isAgentBusy(state.phase),
733
803
  streamingText: state.streamingText
734
804
  });
735
- }, [resolvedStorageKeyPrefix, state.messages, state.phase, state.streamingText, visitorId]);
805
+ }, [
806
+ resolvedStorageKeyPrefix,
807
+ state.messages,
808
+ state.phase,
809
+ state.streamingText,
810
+ visitorId
811
+ ]);
736
812
  const reset = (0, import_react.useCallback)(() => {
737
813
  runRef.current?.abort();
738
814
  runRef.current = null;
@@ -750,7 +826,11 @@ function useAgentChat({
750
826
  let streamed = initialText;
751
827
  const planningPromise = resume ? Promise.resolve() : runStatusSequence(signal, (step) => {
752
828
  if (streamStarted || !isActiveRun()) return;
753
- setState((prev) => ({ ...prev, phase: "running-tools", toolSteps: [step] }));
829
+ setState((prev) => ({
830
+ ...prev,
831
+ phase: "running-tools",
832
+ toolSteps: [step]
833
+ }));
754
834
  });
755
835
  const handlers = {
756
836
  onStep: (label, detail) => {
@@ -758,7 +838,9 @@ function useAgentChat({
758
838
  setState((prev) => ({
759
839
  ...prev,
760
840
  phase: "running-tools",
761
- toolSteps: [{ id: `step-${label}`, label, detail, state: "active" }]
841
+ toolSteps: [
842
+ { id: `step-${label}`, label, detail, state: "active" }
843
+ ]
762
844
  }));
763
845
  },
764
846
  onDelta: (delta) => {
@@ -775,7 +857,11 @@ function useAgentChat({
775
857
  }));
776
858
  }
777
859
  streamed += delta;
778
- setState((prev) => ({ ...prev, phase: "streaming", streamingText: streamed }));
860
+ setState((prev) => ({
861
+ ...prev,
862
+ phase: "streaming",
863
+ streamingText: streamed
864
+ }));
779
865
  },
780
866
  onComplete: () => {
781
867
  if (!isActiveRun()) return;
@@ -789,7 +875,10 @@ function useAgentChat({
789
875
  signal
790
876
  }) : await clientRef.current.sendTurn(visitorText, { handlers, signal });
791
877
  if (resume && finalText === null) {
792
- finalText = await clientRef.current.sendTurn(visitorText, { handlers, signal });
878
+ finalText = await clientRef.current.sendTurn(visitorText, {
879
+ handlers,
880
+ signal
881
+ });
793
882
  }
794
883
  await planningPromise.catch(() => {
795
884
  });
@@ -811,7 +900,8 @@ function useAgentChat({
811
900
  }));
812
901
  runRef.current = null;
813
902
  } catch (error) {
814
- if (error instanceof DOMException && error.name === "AbortError") return;
903
+ if (error instanceof DOMException && error.name === "AbortError")
904
+ return;
815
905
  if (!isActiveRun()) return;
816
906
  const message = formatAgentError(error);
817
907
  if (!message) return;
@@ -845,7 +935,9 @@ function useAgentChat({
845
935
  ...prev,
846
936
  phase: "thinking",
847
937
  messages: [...prev.messages, visitorMessage],
848
- toolSteps: [{ id: "s1", label: "Starting Eve session", state: "active" }],
938
+ toolSteps: [
939
+ { id: "s1", label: "Starting Eve session", state: "active" }
940
+ ],
849
941
  journey: null,
850
942
  followUps: [],
851
943
  streamingText: "",
@@ -1353,6 +1445,7 @@ var import_jsx_runtime8 = require("react/jsx-runtime");
1353
1445
  function AgentWidget({
1354
1446
  indexId,
1355
1447
  customerId,
1448
+ getUnpublishedPreviewGrant,
1356
1449
  version,
1357
1450
  runtimeOrigin,
1358
1451
  placement: placementInput,
@@ -1365,6 +1458,7 @@ function AgentWidget({
1365
1458
  const [railExpanded, setRailExpanded] = (0, import_react5.useState)(false);
1366
1459
  const { state, submit } = useAgentChat({
1367
1460
  customerId,
1461
+ getUnpublishedPreviewGrant,
1368
1462
  indexId,
1369
1463
  version,
1370
1464
  runtimeOrigin