@aomi-labs/react 0.2.2 → 0.2.4

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/index.cjs CHANGED
@@ -42,10 +42,12 @@ __export(index_exports, {
42
42
  ControlContextProvider: () => ControlContextProvider,
43
43
  EventContextProvider: () => EventContextProvider,
44
44
  NotificationContextProvider: () => NotificationContextProvider,
45
+ SUPPORTED_CHAINS: () => SUPPORTED_CHAINS,
45
46
  ThreadContextProvider: () => ThreadContextProvider,
46
47
  UserContextProvider: () => UserContextProvider,
47
48
  cn: () => cn,
48
49
  formatAddress: () => formatAddress,
50
+ getChainInfo: () => getChainInfo,
49
51
  getNetworkName: () => getNetworkName,
50
52
  initThreadControl: () => initThreadControl,
51
53
  useAomiRuntime: () => useAomiRuntime,
@@ -301,11 +303,14 @@ var BackendApi = class {
301
303
  }
302
304
  return await response.json();
303
305
  }
304
- async postChatMessage(sessionId, message, namespace, publicKey, apiKey) {
306
+ async postChatMessage(sessionId, message, namespace, publicKey, apiKey, userState) {
305
307
  const payload = { message, namespace };
306
308
  if (publicKey) {
307
309
  payload.public_key = publicKey;
308
310
  }
311
+ if (userState) {
312
+ payload.user_state = JSON.stringify(userState);
313
+ }
309
314
  return postState(
310
315
  this.backendUrl,
311
316
  "/api/chat",
@@ -1391,6 +1396,15 @@ var getNetworkName = (chainId) => {
1391
1396
  }
1392
1397
  };
1393
1398
  var formatAddress = (addr) => addr ? `${addr.slice(0, 6)}...${addr.slice(-4)}` : "Connect Wallet";
1399
+ var SUPPORTED_CHAINS = [
1400
+ { id: 1, name: "Ethereum", ticker: "ETH" },
1401
+ { id: 137, name: "Polygon", ticker: "MATIC" },
1402
+ { id: 42161, name: "Arbitrum", ticker: "ARB" },
1403
+ { id: 8453, name: "Base", ticker: "BASE" },
1404
+ { id: 10, name: "Optimism", ticker: "OP" },
1405
+ { id: 11155111, name: "Sepolia", ticker: "SEP" }
1406
+ ];
1407
+ var getChainInfo = (chainId) => chainId === void 0 ? void 0 : SUPPORTED_CHAINS.find((c) => c.id === chainId);
1394
1408
 
1395
1409
  // packages/react/src/state/backend-state.ts
1396
1410
  function createBackendState() {
@@ -1464,7 +1478,7 @@ var MessageController = class {
1464
1478
  this.getThreadContextApi().setThreadMessages(threadId, threadMessages);
1465
1479
  }
1466
1480
  async outbound(message, threadId) {
1467
- var _a, _b, _c, _d, _e, _f;
1481
+ var _a, _b, _c, _d, _e, _f, _g, _h;
1468
1482
  const backendState = this.config.backendStateRef.current;
1469
1483
  const text = message.content.filter(
1470
1484
  (part) => part.type === "text"
@@ -1492,6 +1506,7 @@ var MessageController = class {
1492
1506
  const namespace = this.config.getNamespace();
1493
1507
  const publicKey = (_b = (_a = this.config).getPublicKey) == null ? void 0 : _b.call(_a);
1494
1508
  const apiKey = (_e = (_d = (_c = this.config).getApiKey) == null ? void 0 : _d.call(_c)) != null ? _e : void 0;
1509
+ const userState = (_g = (_f = this.config).getUserState) == null ? void 0 : _g.call(_f);
1495
1510
  try {
1496
1511
  this.markRunning(threadId, true);
1497
1512
  const response = await this.config.backendApiRef.current.postChatMessage(
@@ -1499,12 +1514,13 @@ var MessageController = class {
1499
1514
  text,
1500
1515
  namespace,
1501
1516
  publicKey,
1502
- apiKey
1517
+ apiKey,
1518
+ userState
1503
1519
  );
1504
1520
  if (response == null ? void 0 : response.messages) {
1505
1521
  this.inbound(threadId, response.messages);
1506
1522
  }
1507
- if (((_f = response == null ? void 0 : response.system_events) == null ? void 0 : _f.length) && this.config.onSyncEvents) {
1523
+ if (((_h = response == null ? void 0 : response.system_events) == null ? void 0 : _h.length) && this.config.onSyncEvents) {
1508
1524
  this.config.onSyncEvents(backendThreadId, response.system_events);
1509
1525
  }
1510
1526
  if (response == null ? void 0 : response.is_processing) {
@@ -1518,7 +1534,7 @@ var MessageController = class {
1518
1534
  }
1519
1535
  }
1520
1536
  async flushPendingChat(threadId) {
1521
- var _a, _b, _c, _d, _e;
1537
+ var _a, _b, _c, _d, _e, _f, _g;
1522
1538
  const backendState = this.config.backendStateRef.current;
1523
1539
  const pending = dequeuePendingChat(backendState, threadId);
1524
1540
  if (!pending.length) return;
@@ -1526,6 +1542,7 @@ var MessageController = class {
1526
1542
  const namespace = this.config.getNamespace();
1527
1543
  const publicKey = (_b = (_a = this.config).getPublicKey) == null ? void 0 : _b.call(_a);
1528
1544
  const apiKey = (_e = (_d = (_c = this.config).getApiKey) == null ? void 0 : _d.call(_c)) != null ? _e : void 0;
1545
+ const userState = (_g = (_f = this.config).getUserState) == null ? void 0 : _g.call(_f);
1529
1546
  for (const text of pending) {
1530
1547
  try {
1531
1548
  await this.config.backendApiRef.current.postChatMessage(
@@ -1533,7 +1550,8 @@ var MessageController = class {
1533
1550
  text,
1534
1551
  namespace,
1535
1552
  publicKey,
1536
- apiKey
1553
+ apiKey,
1554
+ userState
1537
1555
  );
1538
1556
  } catch (error) {
1539
1557
  console.error("Failed to send queued message:", error);
@@ -1688,6 +1706,7 @@ function useRuntimeOrchestrator(backendApi, options) {
1688
1706
  getPublicKey: options.getPublicKey,
1689
1707
  getNamespace: options.getNamespace,
1690
1708
  getApiKey: options.getApiKey,
1709
+ getUserState: options.getUserState,
1691
1710
  onSyncEvents: options.onSyncEvents
1692
1711
  });
1693
1712
  }
@@ -1783,7 +1802,8 @@ function buildThreadListAdapter({
1783
1802
  userAddress,
1784
1803
  setIsRunning,
1785
1804
  getNamespace,
1786
- getApiKey
1805
+ getApiKey,
1806
+ getUserState
1787
1807
  }) {
1788
1808
  const backendState = backendStateRef.current;
1789
1809
  const { regularThreads, archivedThreads } = buildThreadLists(
@@ -1876,6 +1896,7 @@ function buildThreadListAdapter({
1876
1896
  backendState.pendingChat.delete(uiThreadId);
1877
1897
  const namespace = getNamespace();
1878
1898
  const apiKey = (_b = getApiKey == null ? void 0 : getApiKey()) != null ? _b : void 0;
1899
+ const userState = getUserState == null ? void 0 : getUserState();
1879
1900
  for (const text of pendingMessages) {
1880
1901
  try {
1881
1902
  await backendApiRef.current.postChatMessage(
@@ -1883,7 +1904,8 @@ function buildThreadListAdapter({
1883
1904
  text,
1884
1905
  namespace,
1885
1906
  userAddress,
1886
- apiKey
1907
+ apiKey,
1908
+ userState
1887
1909
  );
1888
1910
  } catch (error) {
1889
1911
  console.error("Failed to send queued message:", error);
@@ -2146,7 +2168,8 @@ function AomiRuntimeCore({
2146
2168
  var _a, _b;
2147
2169
  return (_b = (_a = getCurrentThreadControl().namespace) != null ? _a : getControlState().defaultNamespace) != null ? _b : "default";
2148
2170
  },
2149
- getApiKey: () => getControlState().apiKey
2171
+ getApiKey: () => getControlState().apiKey,
2172
+ getUserState
2150
2173
  }),
2151
2174
  [
2152
2175
  backendApiRef,
@@ -2157,7 +2180,8 @@ function AomiRuntimeCore({
2157
2180
  threadContext,
2158
2181
  threadContext.currentThreadId,
2159
2182
  threadContext.allThreadsMetadata,
2160
- getControlState
2183
+ getControlState,
2184
+ getUserState
2161
2185
  ]
2162
2186
  );
2163
2187
  (0, import_react8.useEffect)(() => {
@@ -2542,10 +2566,12 @@ function useNotificationHandler({
2542
2566
  ControlContextProvider,
2543
2567
  EventContextProvider,
2544
2568
  NotificationContextProvider,
2569
+ SUPPORTED_CHAINS,
2545
2570
  ThreadContextProvider,
2546
2571
  UserContextProvider,
2547
2572
  cn,
2548
2573
  formatAddress,
2574
+ getChainInfo,
2549
2575
  getNetworkName,
2550
2576
  initThreadControl,
2551
2577
  useAomiRuntime,