@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/README.md +6 -2
- package/dist/index.cjs +63 -8
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +7 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +63 -8
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -10,12 +10,16 @@ npm install @aomi-labs/react @assistant-ui/react react react-dom
|
|
|
10
10
|
pnpm add @aomi-labs/react @assistant-ui/react react react-dom
|
|
11
11
|
```
|
|
12
12
|
|
|
13
|
-
Optional
|
|
13
|
+
Optional dependencies for advanced custom wallet adapters:
|
|
14
14
|
|
|
15
15
|
```bash
|
|
16
16
|
pnpm add wagmi viem
|
|
17
17
|
```
|
|
18
18
|
|
|
19
|
+
If you use the registry-installed `AomiFrame`, the default Para-backed
|
|
20
|
+
`AomiAdapterProvider` is already wired for you. Add `wagmi` only when you want
|
|
21
|
+
to bring your own adapter implementation.
|
|
22
|
+
|
|
19
23
|
## Quick Start
|
|
20
24
|
|
|
21
25
|
Wrap your app with `AomiRuntimeProvider`, then use `useAomiRuntime()` anywhere inside:
|
|
@@ -117,7 +121,7 @@ Returns an `AomiRuntimeApi` object with:
|
|
|
117
121
|
| `useControl()` | Model/namespace/API key state |
|
|
118
122
|
| `useNotification()` | Toast notification context |
|
|
119
123
|
| `useEventContext()` | Raw event system access |
|
|
120
|
-
| `useWalletHandler()` | Wallet request handler
|
|
124
|
+
| `useWalletHandler()` | Wallet request handler for custom adapter implementations |
|
|
121
125
|
| `useNotificationHandler()` | Notification event handler |
|
|
122
126
|
|
|
123
127
|
## Utilities
|
package/dist/index.cjs
CHANGED
|
@@ -840,6 +840,8 @@ function useUser() {
|
|
|
840
840
|
return {
|
|
841
841
|
user: context.user,
|
|
842
842
|
setUser: context.setUser,
|
|
843
|
+
addExtValue: context.addExtValue,
|
|
844
|
+
removeExtValue: context.removeExtValue,
|
|
843
845
|
getUserState: context.getUserState,
|
|
844
846
|
onUserStateChange: context.onUserStateChange
|
|
845
847
|
};
|
|
@@ -849,7 +851,8 @@ function UserContextProvider({ children }) {
|
|
|
849
851
|
isConnected: false,
|
|
850
852
|
address: void 0,
|
|
851
853
|
chainId: void 0,
|
|
852
|
-
ensName: void 0
|
|
854
|
+
ensName: void 0,
|
|
855
|
+
ext: void 0
|
|
853
856
|
});
|
|
854
857
|
const userRef = (0, import_react5.useRef)(user);
|
|
855
858
|
userRef.current = user;
|
|
@@ -865,6 +868,36 @@ function UserContextProvider({ children }) {
|
|
|
865
868
|
return next;
|
|
866
869
|
});
|
|
867
870
|
}, []);
|
|
871
|
+
const addExtValue = (0, import_react5.useCallback)((key, value) => {
|
|
872
|
+
setUserState((prev) => {
|
|
873
|
+
var _a;
|
|
874
|
+
const next = __spreadProps(__spreadValues({}, prev), {
|
|
875
|
+
ext: __spreadProps(__spreadValues({}, (_a = prev.ext) != null ? _a : {}), {
|
|
876
|
+
[key]: value
|
|
877
|
+
})
|
|
878
|
+
});
|
|
879
|
+
StateChangeCallbacks.current.forEach((callback) => {
|
|
880
|
+
callback(next);
|
|
881
|
+
});
|
|
882
|
+
return next;
|
|
883
|
+
});
|
|
884
|
+
}, []);
|
|
885
|
+
const removeExtValue = (0, import_react5.useCallback)((key) => {
|
|
886
|
+
setUserState((prev) => {
|
|
887
|
+
if (!prev.ext || !(key in prev.ext)) {
|
|
888
|
+
return prev;
|
|
889
|
+
}
|
|
890
|
+
const nextExt = __spreadValues({}, prev.ext);
|
|
891
|
+
delete nextExt[key];
|
|
892
|
+
const next = __spreadProps(__spreadValues({}, prev), {
|
|
893
|
+
ext: Object.keys(nextExt).length > 0 ? nextExt : void 0
|
|
894
|
+
});
|
|
895
|
+
StateChangeCallbacks.current.forEach((callback) => {
|
|
896
|
+
callback(next);
|
|
897
|
+
});
|
|
898
|
+
return next;
|
|
899
|
+
});
|
|
900
|
+
}, []);
|
|
868
901
|
const getUserState = (0, import_react5.useCallback)(() => userRef.current, []);
|
|
869
902
|
const onUserStateChange = (0, import_react5.useCallback)(
|
|
870
903
|
(callback) => {
|
|
@@ -881,6 +914,8 @@ function UserContextProvider({ children }) {
|
|
|
881
914
|
value: {
|
|
882
915
|
user,
|
|
883
916
|
setUser,
|
|
917
|
+
addExtValue,
|
|
918
|
+
removeExtValue,
|
|
884
919
|
getUserState,
|
|
885
920
|
onUserStateChange
|
|
886
921
|
},
|
|
@@ -1598,22 +1633,40 @@ function AomiRuntimeCore({
|
|
|
1598
1633
|
getApp: getCurrentThreadApp,
|
|
1599
1634
|
getApiKey: () => getControlState().apiKey
|
|
1600
1635
|
});
|
|
1636
|
+
const walletSnapshot = (0, import_react9.useCallback)(
|
|
1637
|
+
(nextUser) => ({
|
|
1638
|
+
address: nextUser.address,
|
|
1639
|
+
chainId: nextUser.chainId,
|
|
1640
|
+
isConnected: nextUser.isConnected,
|
|
1641
|
+
ensName: nextUser.ensName
|
|
1642
|
+
}),
|
|
1643
|
+
[getUserState]
|
|
1644
|
+
);
|
|
1645
|
+
const lastWalletStateRef = (0, import_react9.useRef)(walletSnapshot(getUserState()));
|
|
1601
1646
|
(0, import_react9.useEffect)(() => {
|
|
1647
|
+
lastWalletStateRef.current = walletSnapshot(getUserState());
|
|
1602
1648
|
const unsubscribe = onUserStateChange(async (newUser) => {
|
|
1649
|
+
const nextWalletState = walletSnapshot(newUser);
|
|
1650
|
+
const prevWalletState = lastWalletStateRef.current;
|
|
1651
|
+
if (prevWalletState.address === nextWalletState.address && prevWalletState.chainId === nextWalletState.chainId && prevWalletState.isConnected === nextWalletState.isConnected && prevWalletState.ensName === nextWalletState.ensName) {
|
|
1652
|
+
return;
|
|
1653
|
+
}
|
|
1654
|
+
lastWalletStateRef.current = nextWalletState;
|
|
1603
1655
|
const sessionId = threadContext.currentThreadId;
|
|
1604
1656
|
const message = JSON.stringify({
|
|
1605
1657
|
type: "wallet:state_changed",
|
|
1606
|
-
payload:
|
|
1607
|
-
address: newUser.address,
|
|
1608
|
-
chainId: newUser.chainId,
|
|
1609
|
-
isConnected: newUser.isConnected,
|
|
1610
|
-
ensName: newUser.ensName
|
|
1611
|
-
}
|
|
1658
|
+
payload: nextWalletState
|
|
1612
1659
|
});
|
|
1613
1660
|
await aomiClientRef.current.sendSystemMessage(sessionId, message);
|
|
1614
1661
|
});
|
|
1615
1662
|
return unsubscribe;
|
|
1616
|
-
}, [
|
|
1663
|
+
}, [
|
|
1664
|
+
onUserStateChange,
|
|
1665
|
+
aomiClientRef,
|
|
1666
|
+
threadContext.currentThreadId,
|
|
1667
|
+
getUserState,
|
|
1668
|
+
walletSnapshot
|
|
1669
|
+
]);
|
|
1617
1670
|
const threadContextRef = (0, import_react9.useRef)(threadContext);
|
|
1618
1671
|
threadContextRef.current = threadContext;
|
|
1619
1672
|
const currentThreadIdRef = (0, import_react9.useRef)(threadContext.currentThreadId);
|
|
@@ -1869,6 +1922,8 @@ function AomiRuntimeCore({
|
|
|
1869
1922
|
user: userContext.user,
|
|
1870
1923
|
getUserState: userContext.getUserState,
|
|
1871
1924
|
setUser: userContext.setUser,
|
|
1925
|
+
addExtValue: userContext.addExtValue,
|
|
1926
|
+
removeExtValue: userContext.removeExtValue,
|
|
1872
1927
|
onUserStateChange: userContext.onUserStateChange,
|
|
1873
1928
|
// Thread API
|
|
1874
1929
|
currentThreadId: threadContext.currentThreadId,
|