@aomi-labs/react 0.3.2 → 0.3.3

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.d.cts CHANGED
@@ -16,10 +16,13 @@ type UserState = {
16
16
  chainId?: number;
17
17
  isConnected: boolean;
18
18
  ensName?: string;
19
+ ext?: Record<string, unknown>;
19
20
  };
20
21
  declare function useUser(): {
21
22
  user: UserState;
22
23
  setUser: (data: Partial<UserState>) => void;
24
+ addExtValue: (key: string, value: unknown) => void;
25
+ removeExtValue: (key: string) => void;
23
26
  getUserState: () => UserState;
24
27
  onUserStateChange: (callback: (user: UserState) => void) => () => void;
25
28
  };
@@ -166,6 +169,10 @@ type AomiRuntimeApi = {
166
169
  getUserState: () => UserState;
167
170
  /** Update user state (partial updates merged with existing state) */
168
171
  setUser: (data: Partial<UserState>) => void;
172
+ /** Add or overwrite a value in user_state.ext */
173
+ addExtValue: (key: string, value: unknown) => void;
174
+ /** Remove a value from user_state.ext */
175
+ removeExtValue: (key: string) => void;
169
176
  /** Subscribe to user state changes. Returns unsubscribe function. */
170
177
  onUserStateChange: (callback: (user: UserState) => void) => () => void;
171
178
  /** ID of the currently active thread */
package/dist/index.d.ts CHANGED
@@ -16,10 +16,13 @@ type UserState = {
16
16
  chainId?: number;
17
17
  isConnected: boolean;
18
18
  ensName?: string;
19
+ ext?: Record<string, unknown>;
19
20
  };
20
21
  declare function useUser(): {
21
22
  user: UserState;
22
23
  setUser: (data: Partial<UserState>) => void;
24
+ addExtValue: (key: string, value: unknown) => void;
25
+ removeExtValue: (key: string) => void;
23
26
  getUserState: () => UserState;
24
27
  onUserStateChange: (callback: (user: UserState) => void) => () => void;
25
28
  };
@@ -166,6 +169,10 @@ type AomiRuntimeApi = {
166
169
  getUserState: () => UserState;
167
170
  /** Update user state (partial updates merged with existing state) */
168
171
  setUser: (data: Partial<UserState>) => void;
172
+ /** Add or overwrite a value in user_state.ext */
173
+ addExtValue: (key: string, value: unknown) => void;
174
+ /** Remove a value from user_state.ext */
175
+ removeExtValue: (key: string) => void;
169
176
  /** Subscribe to user state changes. Returns unsubscribe function. */
170
177
  onUserStateChange: (callback: (user: UserState) => void) => () => void;
171
178
  /** ID of the currently active thread */
package/dist/index.js CHANGED
@@ -832,6 +832,8 @@ function useUser() {
832
832
  return {
833
833
  user: context.user,
834
834
  setUser: context.setUser,
835
+ addExtValue: context.addExtValue,
836
+ removeExtValue: context.removeExtValue,
835
837
  getUserState: context.getUserState,
836
838
  onUserStateChange: context.onUserStateChange
837
839
  };
@@ -841,7 +843,8 @@ function UserContextProvider({ children }) {
841
843
  isConnected: false,
842
844
  address: void 0,
843
845
  chainId: void 0,
844
- ensName: void 0
846
+ ensName: void 0,
847
+ ext: void 0
845
848
  });
846
849
  const userRef = useRef4(user);
847
850
  userRef.current = user;
@@ -857,6 +860,36 @@ function UserContextProvider({ children }) {
857
860
  return next;
858
861
  });
859
862
  }, []);
863
+ const addExtValue = useCallback4((key, value) => {
864
+ setUserState((prev) => {
865
+ var _a;
866
+ const next = __spreadProps(__spreadValues({}, prev), {
867
+ ext: __spreadProps(__spreadValues({}, (_a = prev.ext) != null ? _a : {}), {
868
+ [key]: value
869
+ })
870
+ });
871
+ StateChangeCallbacks.current.forEach((callback) => {
872
+ callback(next);
873
+ });
874
+ return next;
875
+ });
876
+ }, []);
877
+ const removeExtValue = useCallback4((key) => {
878
+ setUserState((prev) => {
879
+ if (!prev.ext || !(key in prev.ext)) {
880
+ return prev;
881
+ }
882
+ const nextExt = __spreadValues({}, prev.ext);
883
+ delete nextExt[key];
884
+ const next = __spreadProps(__spreadValues({}, prev), {
885
+ ext: Object.keys(nextExt).length > 0 ? nextExt : void 0
886
+ });
887
+ StateChangeCallbacks.current.forEach((callback) => {
888
+ callback(next);
889
+ });
890
+ return next;
891
+ });
892
+ }, []);
860
893
  const getUserState = useCallback4(() => userRef.current, []);
861
894
  const onUserStateChange = useCallback4(
862
895
  (callback) => {
@@ -873,6 +906,8 @@ function UserContextProvider({ children }) {
873
906
  value: {
874
907
  user,
875
908
  setUser,
909
+ addExtValue,
910
+ removeExtValue,
876
911
  getUserState,
877
912
  onUserStateChange
878
913
  },
@@ -1596,22 +1631,40 @@ function AomiRuntimeCore({
1596
1631
  getApp: getCurrentThreadApp,
1597
1632
  getApiKey: () => getControlState().apiKey
1598
1633
  });
1634
+ const walletSnapshot = useCallback7(
1635
+ (nextUser) => ({
1636
+ address: nextUser.address,
1637
+ chainId: nextUser.chainId,
1638
+ isConnected: nextUser.isConnected,
1639
+ ensName: nextUser.ensName
1640
+ }),
1641
+ [getUserState]
1642
+ );
1643
+ const lastWalletStateRef = useRef7(walletSnapshot(getUserState()));
1599
1644
  useEffect4(() => {
1645
+ lastWalletStateRef.current = walletSnapshot(getUserState());
1600
1646
  const unsubscribe = onUserStateChange(async (newUser) => {
1647
+ const nextWalletState = walletSnapshot(newUser);
1648
+ const prevWalletState = lastWalletStateRef.current;
1649
+ if (prevWalletState.address === nextWalletState.address && prevWalletState.chainId === nextWalletState.chainId && prevWalletState.isConnected === nextWalletState.isConnected && prevWalletState.ensName === nextWalletState.ensName) {
1650
+ return;
1651
+ }
1652
+ lastWalletStateRef.current = nextWalletState;
1601
1653
  const sessionId = threadContext.currentThreadId;
1602
1654
  const message = JSON.stringify({
1603
1655
  type: "wallet:state_changed",
1604
- payload: {
1605
- address: newUser.address,
1606
- chainId: newUser.chainId,
1607
- isConnected: newUser.isConnected,
1608
- ensName: newUser.ensName
1609
- }
1656
+ payload: nextWalletState
1610
1657
  });
1611
1658
  await aomiClientRef.current.sendSystemMessage(sessionId, message);
1612
1659
  });
1613
1660
  return unsubscribe;
1614
- }, [onUserStateChange, aomiClientRef, threadContext.currentThreadId]);
1661
+ }, [
1662
+ onUserStateChange,
1663
+ aomiClientRef,
1664
+ threadContext.currentThreadId,
1665
+ getUserState,
1666
+ walletSnapshot
1667
+ ]);
1615
1668
  const threadContextRef = useRef7(threadContext);
1616
1669
  threadContextRef.current = threadContext;
1617
1670
  const currentThreadIdRef = useRef7(threadContext.currentThreadId);
@@ -1867,6 +1920,8 @@ function AomiRuntimeCore({
1867
1920
  user: userContext.user,
1868
1921
  getUserState: userContext.getUserState,
1869
1922
  setUser: userContext.setUser,
1923
+ addExtValue: userContext.addExtValue,
1924
+ removeExtValue: userContext.removeExtValue,
1870
1925
  onUserStateChange: userContext.onUserStateChange,
1871
1926
  // Thread API
1872
1927
  currentThreadId: threadContext.currentThreadId,