@react-grab/ami 0.0.88 → 0.0.90

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/client.js CHANGED
@@ -17362,11 +17362,60 @@ var q = async (e) => {
17362
17362
  return { success: o2.success, patches: t2.patches, error: o2.error };
17363
17363
  };
17364
17364
 
17365
+ // ../utils/dist/client.js
17366
+ var CONNECTION_CHECK_TTL_MS = 5e3;
17367
+ var STORAGE_KEY = "react-grab:agent-sessions";
17368
+ var isRecord = (value) => typeof value === "object" && value !== null;
17369
+ var getStoredAgentContext = (storage, sessionId, storageKey = STORAGE_KEY) => {
17370
+ const rawSessions = storage.getItem(storageKey);
17371
+ if (!rawSessions) throw new Error("No sessions to resume");
17372
+ let parsed;
17373
+ try {
17374
+ parsed = JSON.parse(rawSessions);
17375
+ } catch {
17376
+ throw new Error("Failed to parse stored sessions");
17377
+ }
17378
+ if (!isRecord(parsed)) throw new Error("Invalid stored sessions");
17379
+ const storedSession = parsed[sessionId];
17380
+ if (!isRecord(storedSession)) {
17381
+ throw new Error(`Session ${sessionId} not found`);
17382
+ }
17383
+ const context = storedSession.context;
17384
+ if (!isRecord(context)) throw new Error(`Session ${sessionId} is invalid`);
17385
+ const content = context.content;
17386
+ const prompt = context.prompt;
17387
+ if (typeof content !== "string" || typeof prompt !== "string") {
17388
+ throw new Error(`Session ${sessionId} is invalid`);
17389
+ }
17390
+ const options = context.options;
17391
+ const storedSessionId = context.sessionId;
17392
+ return {
17393
+ content,
17394
+ prompt,
17395
+ options,
17396
+ sessionId: typeof storedSessionId === "string" ? storedSessionId : void 0
17397
+ };
17398
+ };
17399
+ var createCachedConnectionChecker = (checkConnection, ttlMs = CONNECTION_CHECK_TTL_MS) => {
17400
+ let cache = null;
17401
+ return async () => {
17402
+ const now = Date.now();
17403
+ if (cache && now - cache.timestamp < ttlMs) return cache.result;
17404
+ try {
17405
+ const result = await checkConnection();
17406
+ cache = { result, timestamp: now };
17407
+ return result;
17408
+ } catch {
17409
+ cache = { result: false, timestamp: now };
17410
+ return false;
17411
+ }
17412
+ };
17413
+ };
17414
+
17365
17415
  // src/constants.ts
17366
17416
  var COMPLETED_STATUS = "COMPLETED_STATUS";
17367
17417
 
17368
17418
  // src/client.ts
17369
- var STORAGE_KEY = "react-grab:agent-sessions";
17370
17419
  var TOKEN_STORAGE_KEY = "react-grab:ami-token";
17371
17420
  var BRIDGE_TOKEN_STORAGE_KEY = "react-grab:ami-bridge-token";
17372
17421
  var loadCachedAuth = () => {
@@ -17491,9 +17540,8 @@ ${context.content}`;
17491
17540
  return { status: COMPLETED_STATUS, messages, chatId };
17492
17541
  }
17493
17542
  };
17494
- var CONNECTION_CHECK_TTL_MS = 5e3;
17543
+ var isReactGrabApi = (value) => typeof value === "object" && value !== null && "setAgent" in value;
17495
17544
  var createAmiAgentProvider = (projectId) => {
17496
- let connectionCache = null;
17497
17545
  let lastAgentMessages = null;
17498
17546
  const sessionData = /* @__PURE__ */ new Map();
17499
17547
  const getLatestProjectId = async (token) => {
@@ -17615,16 +17663,10 @@ var createAmiAgentProvider = (projectId) => {
17615
17663
  },
17616
17664
  resume: async function* (sessionId, signal, storage) {
17617
17665
  const startTime = Date.now();
17618
- const savedSessions = storage.getItem(STORAGE_KEY);
17619
- if (!savedSessions) {
17620
- throw new Error("No sessions to resume");
17621
- }
17622
- const sessionsObject = JSON.parse(savedSessions);
17623
- const session = sessionsObject[sessionId];
17624
- if (!session) {
17625
- throw new Error(`Session ${sessionId} not found`);
17626
- }
17627
- const context = session.context;
17666
+ const storedContext = getStoredAgentContext(storage, sessionId);
17667
+ const context = {
17668
+ content: storedContext.content,
17669
+ prompt: storedContext.prompt};
17628
17670
  const auth = await getOrCreateAuth();
17629
17671
  yield "Resuming...";
17630
17672
  const statusQueue = [];
@@ -17722,21 +17764,10 @@ var createAmiAgentProvider = (projectId) => {
17722
17764
  },
17723
17765
  supportsResume: true,
17724
17766
  supportsFollowUp: true,
17725
- checkConnection: async () => {
17726
- const now = Date.now();
17727
- if (connectionCache && now - connectionCache.timestamp < CONNECTION_CHECK_TTL_MS) {
17728
- return connectionCache.result;
17729
- }
17730
- try {
17731
- const response = await fetch(l, { method: "HEAD" });
17732
- const result = response.ok;
17733
- connectionCache = { result, timestamp: now };
17734
- return result;
17735
- } catch {
17736
- connectionCache = { result: false, timestamp: now };
17737
- return false;
17738
- }
17739
- },
17767
+ checkConnection: createCachedConnectionChecker(async () => {
17768
+ const response = await fetch(l, { method: "HEAD" });
17769
+ return response.ok;
17770
+ }, CONNECTION_CHECK_TTL_MS),
17740
17771
  undo: async () => {
17741
17772
  if (!lastAgentMessages) return;
17742
17773
  try {
@@ -17753,24 +17784,25 @@ var createAmiAgentProvider = (projectId) => {
17753
17784
  var attachAgent = async () => {
17754
17785
  if (typeof window === "undefined") return;
17755
17786
  const provider = createAmiAgentProvider();
17756
- const attach = (api2) => {
17757
- api2.setAgent({ provider, storage: sessionStorage });
17787
+ const attach = (api) => {
17788
+ api.setAgent({ provider, storage: sessionStorage });
17758
17789
  };
17759
- const api = window.__REACT_GRAB__;
17760
- if (api) {
17761
- attach(api);
17790
+ const existingApi = window.__REACT_GRAB__;
17791
+ if (isReactGrabApi(existingApi)) {
17792
+ attach(existingApi);
17762
17793
  return;
17763
17794
  }
17764
17795
  window.addEventListener(
17765
17796
  "react-grab:init",
17766
17797
  (event) => {
17767
- const customEvent = event;
17768
- attach(customEvent.detail);
17798
+ if (!(event instanceof CustomEvent)) return;
17799
+ if (!isReactGrabApi(event.detail)) return;
17800
+ attach(event.detail);
17769
17801
  },
17770
17802
  { once: true }
17771
17803
  );
17772
17804
  const apiAfterListener = window.__REACT_GRAB__;
17773
- if (apiAfterListener) {
17805
+ if (isReactGrabApi(apiAfterListener)) {
17774
17806
  attach(apiAfterListener);
17775
17807
  }
17776
17808
  };
package/dist/server.cjs CHANGED
@@ -7,9 +7,11 @@ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
7
7
  var pc__default = /*#__PURE__*/_interopDefault(pc);
8
8
 
9
9
  // src/server.ts
10
- var VERSION = "0.0.88";
10
+ var VERSION = "0.0.90";
11
11
  try {
12
- fetch(`https://www.react-grab.com/api/version?source=ami&t=${Date.now()}`).catch(() => {
12
+ fetch(
13
+ `https://www.react-grab.com/api/version?source=ami&t=${Date.now()}`
14
+ ).catch(() => {
13
15
  });
14
16
  } catch {
15
17
  }
package/dist/server.js CHANGED
@@ -1,9 +1,11 @@
1
1
  import pc from 'picocolors';
2
2
 
3
3
  // src/server.ts
4
- var VERSION = "0.0.88";
4
+ var VERSION = "0.0.90";
5
5
  try {
6
- fetch(`https://www.react-grab.com/api/version?source=ami&t=${Date.now()}`).catch(() => {
6
+ fetch(
7
+ `https://www.react-grab.com/api/version?source=ami&t=${Date.now()}`
8
+ ).catch(() => {
7
9
  });
8
10
  } catch {
9
11
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-grab/ami",
3
- "version": "0.0.88",
3
+ "version": "0.0.90",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "react-grab-ami": "./dist/cli.cjs"
@@ -22,12 +22,13 @@
22
22
  ],
23
23
  "devDependencies": {
24
24
  "@types/node": "^22.10.7",
25
- "tsup": "^8.4.0"
25
+ "tsup": "^8.4.0",
26
+ "@react-grab/utils": "0.0.90"
26
27
  },
27
28
  "dependencies": {
28
29
  "ami-sdk": "^0.0.6",
29
30
  "picocolors": "^1.1.1",
30
- "react-grab": "0.0.88"
31
+ "react-grab": "0.0.90"
31
32
  },
32
33
  "scripts": {
33
34
  "dev": "tsup --watch",