@aomi-labs/react 0.3.9 → 0.3.10
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 +108 -77
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +17 -24
- package/dist/index.d.ts +17 -24
- package/dist/index.js +101 -70
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { AomiClient, Session, WalletRequest } from '@aomi-labs/client';
|
|
2
|
-
export { AomiChatResponse, AomiClient, AomiClientOptions, AomiCreateThreadResponse, AomiInterruptResponse, AomiMessage, AomiSSEEvent, AomiStateResponse, AomiSystemEvent, AomiSystemResponse, AomiThread, WalletEip712Payload, WalletRequest, WalletTxPayload, toViemSignTypedDataArgs } from '@aomi-labs/client';
|
|
1
|
+
import { AomiClient, Session, WalletRequest, UserState } from '@aomi-labs/client';
|
|
2
|
+
export { AomiChatResponse, AomiClient, AomiClientOptions, AomiCreateThreadResponse, AomiInterruptResponse, AomiMessage, AomiSSEEvent, AomiStateResponse, AomiSystemEvent, AomiSystemResponse, AomiThread, UserState, WalletEip712Payload, WalletRequest, WalletTxPayload, toViemSignTypedDataArgs } from '@aomi-labs/client';
|
|
3
3
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
4
4
|
import { ReactNode, SetStateAction } from 'react';
|
|
5
5
|
import { ThreadMessageLike } from '@assistant-ui/react';
|
|
@@ -11,25 +11,6 @@ type AomiRuntimeProviderProps = {
|
|
|
11
11
|
};
|
|
12
12
|
declare function AomiRuntimeProvider({ children, backendUrl, }: Readonly<AomiRuntimeProviderProps>): react_jsx_runtime.JSX.Element;
|
|
13
13
|
|
|
14
|
-
type UserState = {
|
|
15
|
-
address?: string;
|
|
16
|
-
chainId?: number;
|
|
17
|
-
isConnected: boolean;
|
|
18
|
-
ensName?: string;
|
|
19
|
-
ext?: Record<string, unknown>;
|
|
20
|
-
};
|
|
21
|
-
declare function useUser(): {
|
|
22
|
-
user: UserState;
|
|
23
|
-
setUser: (data: Partial<UserState>) => void;
|
|
24
|
-
addExtValue: (key: string, value: unknown) => void;
|
|
25
|
-
removeExtValue: (key: string) => void;
|
|
26
|
-
getUserState: () => UserState;
|
|
27
|
-
onUserStateChange: (callback: (user: UserState) => void) => () => void;
|
|
28
|
-
};
|
|
29
|
-
declare function UserContextProvider({ children }: {
|
|
30
|
-
children: ReactNode;
|
|
31
|
-
}): react_jsx_runtime.JSX.Element;
|
|
32
|
-
|
|
33
14
|
type ThreadContext = {
|
|
34
15
|
currentThreadId: string;
|
|
35
16
|
setCurrentThreadId: (id: string) => void;
|
|
@@ -152,8 +133,8 @@ type WalletHandlerConfig = {
|
|
|
152
133
|
type WalletHandlerApi = {
|
|
153
134
|
/** All queued wallet requests (tx + eip712) */
|
|
154
135
|
pendingRequests: WalletRequest[];
|
|
155
|
-
/**
|
|
156
|
-
|
|
136
|
+
/** Replace pending requests with the session's authoritative snapshot. */
|
|
137
|
+
setRequests: (requests: WalletRequest[]) => void;
|
|
157
138
|
/** Complete a request successfully — sends response to backend via ClientSession */
|
|
158
139
|
resolveRequest: (id: string, result: WalletRequestResult) => void;
|
|
159
140
|
/** Fail a request — sends error to backend via ClientSession */
|
|
@@ -280,6 +261,18 @@ type NotificationApi = {
|
|
|
280
261
|
};
|
|
281
262
|
declare function useNotificationHandler({ onNotification, }?: NotificationHandlerConfig): NotificationApi;
|
|
282
263
|
|
|
264
|
+
declare function useUser(): {
|
|
265
|
+
user: UserState;
|
|
266
|
+
setUser: (data: Partial<UserState>) => void;
|
|
267
|
+
addExtValue: (key: string, value: unknown) => void;
|
|
268
|
+
removeExtValue: (key: string) => void;
|
|
269
|
+
getUserState: () => UserState;
|
|
270
|
+
onUserStateChange: (callback: (user: UserState) => void) => () => void;
|
|
271
|
+
};
|
|
272
|
+
declare function UserContextProvider({ children }: {
|
|
273
|
+
children: ReactNode;
|
|
274
|
+
}): react_jsx_runtime.JSX.Element;
|
|
275
|
+
|
|
283
276
|
/**
|
|
284
277
|
* Utility function to merge Tailwind CSS classes with conflict resolution.
|
|
285
278
|
* Combines clsx for conditional classes and tailwind-merge for deduplication.
|
|
@@ -385,4 +378,4 @@ type ControlContextProviderProps = {
|
|
|
385
378
|
};
|
|
386
379
|
declare function ControlContextProvider({ children, aomiClient, sessionId, publicKey, getThreadMetadata, updateThreadMetadata, }: ControlContextProviderProps): react_jsx_runtime.JSX.Element;
|
|
387
380
|
|
|
388
|
-
export { type AomiRuntimeApi, AomiRuntimeProvider, type AomiRuntimeProviderProps, type ChainInfo, type ControlContextApi, ControlContextProvider, type ControlContextProviderProps, type ControlState, type EventContext, EventContextProvider, type EventContextProviderProps, type EventSubscriber, type InboundEvent, type Notification$1 as Notification, type NotificationApi, NotificationContextProvider, type NotificationContextProviderProps, type NotificationContextApi as NotificationContextValue, type NotificationHandlerConfig, type NotificationType, type SSEStatus, SUPPORTED_CHAINS, type NotificationData as ShowNotificationParams, type StoredProviderKey, type ThreadContext, ThreadContextProvider, type ThreadControlState, type ThreadMetadata, type UserConfig, UserContextProvider, type
|
|
381
|
+
export { type AomiRuntimeApi, AomiRuntimeProvider, type AomiRuntimeProviderProps, type ChainInfo, type ControlContextApi, ControlContextProvider, type ControlContextProviderProps, type ControlState, type EventContext, EventContextProvider, type EventContextProviderProps, type EventSubscriber, type InboundEvent, type Notification$1 as Notification, type NotificationApi, NotificationContextProvider, type NotificationContextProviderProps, type NotificationContextApi as NotificationContextValue, type NotificationHandlerConfig, type NotificationType, type SSEStatus, SUPPORTED_CHAINS, type NotificationData as ShowNotificationParams, type StoredProviderKey, type ThreadContext, ThreadContextProvider, type ThreadControlState, type ThreadMetadata, type UserConfig, UserContextProvider, type WalletHandlerApi, type WalletHandlerConfig, type WalletRequestKind, type WalletRequestResult, type WalletRequestStatus, cn, formatAddress, getChainInfo, getNetworkName, initThreadControl, useAomiRuntime, useControl, useCurrentThreadMessages, useCurrentThreadMetadata, useEventContext, useNotification, useNotificationHandler, useThreadContext, useUser, useWalletHandler };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { AomiClient, Session, WalletRequest } from '@aomi-labs/client';
|
|
2
|
-
export { AomiChatResponse, AomiClient, AomiClientOptions, AomiCreateThreadResponse, AomiInterruptResponse, AomiMessage, AomiSSEEvent, AomiStateResponse, AomiSystemEvent, AomiSystemResponse, AomiThread, WalletEip712Payload, WalletRequest, WalletTxPayload, toViemSignTypedDataArgs } from '@aomi-labs/client';
|
|
1
|
+
import { AomiClient, Session, WalletRequest, UserState } from '@aomi-labs/client';
|
|
2
|
+
export { AomiChatResponse, AomiClient, AomiClientOptions, AomiCreateThreadResponse, AomiInterruptResponse, AomiMessage, AomiSSEEvent, AomiStateResponse, AomiSystemEvent, AomiSystemResponse, AomiThread, UserState, WalletEip712Payload, WalletRequest, WalletTxPayload, toViemSignTypedDataArgs } from '@aomi-labs/client';
|
|
3
3
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
4
4
|
import { ReactNode, SetStateAction } from 'react';
|
|
5
5
|
import { ThreadMessageLike } from '@assistant-ui/react';
|
|
@@ -11,25 +11,6 @@ type AomiRuntimeProviderProps = {
|
|
|
11
11
|
};
|
|
12
12
|
declare function AomiRuntimeProvider({ children, backendUrl, }: Readonly<AomiRuntimeProviderProps>): react_jsx_runtime.JSX.Element;
|
|
13
13
|
|
|
14
|
-
type UserState = {
|
|
15
|
-
address?: string;
|
|
16
|
-
chainId?: number;
|
|
17
|
-
isConnected: boolean;
|
|
18
|
-
ensName?: string;
|
|
19
|
-
ext?: Record<string, unknown>;
|
|
20
|
-
};
|
|
21
|
-
declare function useUser(): {
|
|
22
|
-
user: UserState;
|
|
23
|
-
setUser: (data: Partial<UserState>) => void;
|
|
24
|
-
addExtValue: (key: string, value: unknown) => void;
|
|
25
|
-
removeExtValue: (key: string) => void;
|
|
26
|
-
getUserState: () => UserState;
|
|
27
|
-
onUserStateChange: (callback: (user: UserState) => void) => () => void;
|
|
28
|
-
};
|
|
29
|
-
declare function UserContextProvider({ children }: {
|
|
30
|
-
children: ReactNode;
|
|
31
|
-
}): react_jsx_runtime.JSX.Element;
|
|
32
|
-
|
|
33
14
|
type ThreadContext = {
|
|
34
15
|
currentThreadId: string;
|
|
35
16
|
setCurrentThreadId: (id: string) => void;
|
|
@@ -152,8 +133,8 @@ type WalletHandlerConfig = {
|
|
|
152
133
|
type WalletHandlerApi = {
|
|
153
134
|
/** All queued wallet requests (tx + eip712) */
|
|
154
135
|
pendingRequests: WalletRequest[];
|
|
155
|
-
/**
|
|
156
|
-
|
|
136
|
+
/** Replace pending requests with the session's authoritative snapshot. */
|
|
137
|
+
setRequests: (requests: WalletRequest[]) => void;
|
|
157
138
|
/** Complete a request successfully — sends response to backend via ClientSession */
|
|
158
139
|
resolveRequest: (id: string, result: WalletRequestResult) => void;
|
|
159
140
|
/** Fail a request — sends error to backend via ClientSession */
|
|
@@ -280,6 +261,18 @@ type NotificationApi = {
|
|
|
280
261
|
};
|
|
281
262
|
declare function useNotificationHandler({ onNotification, }?: NotificationHandlerConfig): NotificationApi;
|
|
282
263
|
|
|
264
|
+
declare function useUser(): {
|
|
265
|
+
user: UserState;
|
|
266
|
+
setUser: (data: Partial<UserState>) => void;
|
|
267
|
+
addExtValue: (key: string, value: unknown) => void;
|
|
268
|
+
removeExtValue: (key: string) => void;
|
|
269
|
+
getUserState: () => UserState;
|
|
270
|
+
onUserStateChange: (callback: (user: UserState) => void) => () => void;
|
|
271
|
+
};
|
|
272
|
+
declare function UserContextProvider({ children }: {
|
|
273
|
+
children: ReactNode;
|
|
274
|
+
}): react_jsx_runtime.JSX.Element;
|
|
275
|
+
|
|
283
276
|
/**
|
|
284
277
|
* Utility function to merge Tailwind CSS classes with conflict resolution.
|
|
285
278
|
* Combines clsx for conditional classes and tailwind-merge for deduplication.
|
|
@@ -385,4 +378,4 @@ type ControlContextProviderProps = {
|
|
|
385
378
|
};
|
|
386
379
|
declare function ControlContextProvider({ children, aomiClient, sessionId, publicKey, getThreadMetadata, updateThreadMetadata, }: ControlContextProviderProps): react_jsx_runtime.JSX.Element;
|
|
387
380
|
|
|
388
|
-
export { type AomiRuntimeApi, AomiRuntimeProvider, type AomiRuntimeProviderProps, type ChainInfo, type ControlContextApi, ControlContextProvider, type ControlContextProviderProps, type ControlState, type EventContext, EventContextProvider, type EventContextProviderProps, type EventSubscriber, type InboundEvent, type Notification$1 as Notification, type NotificationApi, NotificationContextProvider, type NotificationContextProviderProps, type NotificationContextApi as NotificationContextValue, type NotificationHandlerConfig, type NotificationType, type SSEStatus, SUPPORTED_CHAINS, type NotificationData as ShowNotificationParams, type StoredProviderKey, type ThreadContext, ThreadContextProvider, type ThreadControlState, type ThreadMetadata, type UserConfig, UserContextProvider, type
|
|
381
|
+
export { type AomiRuntimeApi, AomiRuntimeProvider, type AomiRuntimeProviderProps, type ChainInfo, type ControlContextApi, ControlContextProvider, type ControlContextProviderProps, type ControlState, type EventContext, EventContextProvider, type EventContextProviderProps, type EventSubscriber, type InboundEvent, type Notification$1 as Notification, type NotificationApi, NotificationContextProvider, type NotificationContextProviderProps, type NotificationContextApi as NotificationContextValue, type NotificationHandlerConfig, type NotificationType, type SSEStatus, SUPPORTED_CHAINS, type NotificationData as ShowNotificationParams, type StoredProviderKey, type ThreadContext, ThreadContextProvider, type ThreadControlState, type ThreadMetadata, type UserConfig, UserContextProvider, type WalletHandlerApi, type WalletHandlerConfig, type WalletRequestKind, type WalletRequestResult, type WalletRequestStatus, cn, formatAddress, getChainInfo, getNetworkName, initThreadControl, useAomiRuntime, useControl, useCurrentThreadMessages, useCurrentThreadMetadata, useEventContext, useNotification, useNotificationHandler, useThreadContext, useUser, useWalletHandler };
|
package/dist/index.js
CHANGED
|
@@ -37,7 +37,7 @@ import { toViemSignTypedDataArgs } from "@aomi-labs/client";
|
|
|
37
37
|
|
|
38
38
|
// packages/react/src/runtime/aomi-runtime.tsx
|
|
39
39
|
import { useMemo as useMemo3 } from "react";
|
|
40
|
-
import { AomiClient } from "@aomi-labs/client";
|
|
40
|
+
import { AomiClient, UserState as UserState4 } from "@aomi-labs/client";
|
|
41
41
|
|
|
42
42
|
// packages/react/src/contexts/control-context.tsx
|
|
43
43
|
import {
|
|
@@ -880,18 +880,10 @@ import {
|
|
|
880
880
|
useRef as useRef4,
|
|
881
881
|
useState as useState3
|
|
882
882
|
} from "react";
|
|
883
|
+
import { UserState } from "@aomi-labs/client";
|
|
884
|
+
import { UserState as UserState2 } from "@aomi-labs/client";
|
|
883
885
|
import { jsx as jsx5 } from "react/jsx-runtime";
|
|
884
886
|
var UserContext = createContext5(void 0);
|
|
885
|
-
function normalizeUserState(next, data) {
|
|
886
|
-
if (data.isConnected === false) {
|
|
887
|
-
return __spreadProps(__spreadValues({}, next), {
|
|
888
|
-
address: void 0,
|
|
889
|
-
chainId: void 0,
|
|
890
|
-
ensName: void 0
|
|
891
|
-
});
|
|
892
|
-
}
|
|
893
|
-
return next;
|
|
894
|
-
}
|
|
895
887
|
function useUser() {
|
|
896
888
|
const context = useContext5(UserContext);
|
|
897
889
|
if (!context) {
|
|
@@ -908,10 +900,10 @@ function useUser() {
|
|
|
908
900
|
}
|
|
909
901
|
function UserContextProvider({ children }) {
|
|
910
902
|
const [user, setUserState] = useState3({
|
|
911
|
-
isConnected: false,
|
|
912
903
|
address: void 0,
|
|
913
|
-
|
|
914
|
-
|
|
904
|
+
chain_id: void 0,
|
|
905
|
+
is_connected: false,
|
|
906
|
+
ens_name: void 0,
|
|
915
907
|
ext: void 0
|
|
916
908
|
});
|
|
917
909
|
const userRef = useRef4(user);
|
|
@@ -921,7 +913,13 @@ function UserContextProvider({ children }) {
|
|
|
921
913
|
);
|
|
922
914
|
const setUser = useCallback4((data) => {
|
|
923
915
|
setUserState((prev) => {
|
|
924
|
-
|
|
916
|
+
var _a, _b, _c;
|
|
917
|
+
const normalizedData = (_a = UserState.normalize(data)) != null ? _a : {};
|
|
918
|
+
const next = normalizedData.is_connected === false ? __spreadProps(__spreadValues({}, (_b = UserState.normalize(__spreadValues(__spreadValues({}, prev), normalizedData))) != null ? _b : prev), {
|
|
919
|
+
address: void 0,
|
|
920
|
+
chain_id: void 0,
|
|
921
|
+
ens_name: void 0
|
|
922
|
+
}) : (_c = UserState.normalize(__spreadValues(__spreadValues({}, prev), normalizedData))) != null ? _c : prev;
|
|
925
923
|
StateChangeCallbacks.current.forEach((callback) => {
|
|
926
924
|
callback(next);
|
|
927
925
|
});
|
|
@@ -930,12 +928,7 @@ function UserContextProvider({ children }) {
|
|
|
930
928
|
}, []);
|
|
931
929
|
const addExtValue = useCallback4((key, value) => {
|
|
932
930
|
setUserState((prev) => {
|
|
933
|
-
|
|
934
|
-
const next = __spreadProps(__spreadValues({}, prev), {
|
|
935
|
-
ext: __spreadProps(__spreadValues({}, (_a = prev.ext) != null ? _a : {}), {
|
|
936
|
-
[key]: value
|
|
937
|
-
})
|
|
938
|
-
});
|
|
931
|
+
const next = UserState.withExt(prev, key, value);
|
|
939
932
|
StateChangeCallbacks.current.forEach((callback) => {
|
|
940
933
|
callback(next);
|
|
941
934
|
});
|
|
@@ -944,10 +937,11 @@ function UserContextProvider({ children }) {
|
|
|
944
937
|
}, []);
|
|
945
938
|
const removeExtValue = useCallback4((key) => {
|
|
946
939
|
setUserState((prev) => {
|
|
947
|
-
|
|
940
|
+
const ext = prev.ext;
|
|
941
|
+
if (typeof ext !== "object" || ext === null || Array.isArray(ext) || !(key in ext)) {
|
|
948
942
|
return prev;
|
|
949
943
|
}
|
|
950
|
-
const nextExt = __spreadValues({},
|
|
944
|
+
const nextExt = __spreadValues({}, ext);
|
|
951
945
|
delete nextExt[key];
|
|
952
946
|
const next = __spreadProps(__spreadValues({}, prev), {
|
|
953
947
|
ext: Object.keys(nextExt).length > 0 ? nextExt : void 0
|
|
@@ -990,6 +984,7 @@ import {
|
|
|
990
984
|
AssistantRuntimeProvider,
|
|
991
985
|
useExternalStoreRuntime
|
|
992
986
|
} from "@assistant-ui/react";
|
|
987
|
+
import { UserState as UserState3 } from "@aomi-labs/client";
|
|
993
988
|
|
|
994
989
|
// packages/react/src/runtime/orchestrator.ts
|
|
995
990
|
import { useCallback as useCallback5, useEffect as useEffect2, useRef as useRef5, useState as useState4 } from "react";
|
|
@@ -1183,16 +1178,13 @@ function useRuntimeOrchestrator(aomiClient, options) {
|
|
|
1183
1178
|
})
|
|
1184
1179
|
);
|
|
1185
1180
|
cleanups.push(
|
|
1186
|
-
session.on(
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
var _a2;
|
|
1194
|
-
return (_a2 = options.onWalletRequest) == null ? void 0 : _a2.call(options, req);
|
|
1195
|
-
})
|
|
1181
|
+
session.on(
|
|
1182
|
+
"wallet_requests_changed",
|
|
1183
|
+
(requests) => {
|
|
1184
|
+
var _a2;
|
|
1185
|
+
return (_a2 = options.onPendingRequestsChange) == null ? void 0 : _a2.call(options, requests);
|
|
1186
|
+
}
|
|
1187
|
+
)
|
|
1196
1188
|
);
|
|
1197
1189
|
cleanups.push(
|
|
1198
1190
|
session.on("title_changed", ({ title }) => {
|
|
@@ -1218,7 +1210,7 @@ function useRuntimeOrchestrator(aomiClient, options) {
|
|
|
1218
1210
|
);
|
|
1219
1211
|
const ensureInitialState = useCallback5(
|
|
1220
1212
|
async (threadId) => {
|
|
1221
|
-
var _a;
|
|
1213
|
+
var _a, _b;
|
|
1222
1214
|
if (pendingFetches.current.has(threadId)) return;
|
|
1223
1215
|
pendingFetches.current.add(threadId);
|
|
1224
1216
|
try {
|
|
@@ -1226,6 +1218,7 @@ function useRuntimeOrchestrator(aomiClient, options) {
|
|
|
1226
1218
|
const userState = (_a = options.getUserState) == null ? void 0 : _a.call(options);
|
|
1227
1219
|
if (userState) session.resolveUserState(userState);
|
|
1228
1220
|
await session.fetchCurrentState();
|
|
1221
|
+
(_b = options.onPendingRequestsChange) == null ? void 0 : _b.call(options, session.getPendingRequests());
|
|
1229
1222
|
if (threadContextRef.current.currentThreadId === threadId) {
|
|
1230
1223
|
setIsRunning(session.getIsProcessing());
|
|
1231
1224
|
}
|
|
@@ -1242,7 +1235,7 @@ function useRuntimeOrchestrator(aomiClient, options) {
|
|
|
1242
1235
|
);
|
|
1243
1236
|
const sendMessage = useCallback5(
|
|
1244
1237
|
async (text, threadId) => {
|
|
1245
|
-
var _a;
|
|
1238
|
+
var _a, _b;
|
|
1246
1239
|
const session = getSession(threadId);
|
|
1247
1240
|
const userState = (_a = options.getUserState) == null ? void 0 : _a.call(options);
|
|
1248
1241
|
if (userState) session.resolveUserState(userState);
|
|
@@ -1260,6 +1253,7 @@ function useRuntimeOrchestrator(aomiClient, options) {
|
|
|
1260
1253
|
lastActiveAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
1261
1254
|
});
|
|
1262
1255
|
await session.sendAsync(text);
|
|
1256
|
+
(_b = options.onPendingRequestsChange) == null ? void 0 : _b.call(options, session.getPendingRequests());
|
|
1263
1257
|
},
|
|
1264
1258
|
[getSession]
|
|
1265
1259
|
);
|
|
@@ -1447,9 +1441,9 @@ function useWalletHandler({
|
|
|
1447
1441
|
getSession
|
|
1448
1442
|
}) {
|
|
1449
1443
|
const [pendingRequests, setPendingRequests] = useState5([]);
|
|
1450
|
-
const requestsRef = useRef6(
|
|
1451
|
-
const
|
|
1452
|
-
requestsRef.current = [...
|
|
1444
|
+
const requestsRef = useRef6(pendingRequests);
|
|
1445
|
+
const setRequests = useCallback6((requests) => {
|
|
1446
|
+
requestsRef.current = [...requests];
|
|
1453
1447
|
setPendingRequests(requestsRef.current);
|
|
1454
1448
|
}, []);
|
|
1455
1449
|
const resolveRequest = useCallback6(
|
|
@@ -1459,13 +1453,12 @@ function useWalletHandler({
|
|
|
1459
1453
|
console.error("[wallet-handler] No session available to resolve request");
|
|
1460
1454
|
return;
|
|
1461
1455
|
}
|
|
1462
|
-
requestsRef.current
|
|
1463
|
-
setPendingRequests(requestsRef.current);
|
|
1456
|
+
setRequests(requestsRef.current.filter((request) => request.id !== id));
|
|
1464
1457
|
void session.resolve(id, result).catch((err) => {
|
|
1465
1458
|
console.error("[wallet-handler] Failed to resolve request:", err);
|
|
1466
1459
|
});
|
|
1467
1460
|
},
|
|
1468
|
-
[getSession]
|
|
1461
|
+
[getSession, setRequests]
|
|
1469
1462
|
);
|
|
1470
1463
|
const rejectRequest = useCallback6(
|
|
1471
1464
|
(id, error) => {
|
|
@@ -1474,17 +1467,16 @@ function useWalletHandler({
|
|
|
1474
1467
|
console.error("[wallet-handler] No session available to reject request");
|
|
1475
1468
|
return;
|
|
1476
1469
|
}
|
|
1477
|
-
requestsRef.current
|
|
1478
|
-
setPendingRequests(requestsRef.current);
|
|
1470
|
+
setRequests(requestsRef.current.filter((request) => request.id !== id));
|
|
1479
1471
|
void session.reject(id, error).catch((err) => {
|
|
1480
1472
|
console.error("[wallet-handler] Failed to reject request:", err);
|
|
1481
1473
|
});
|
|
1482
1474
|
},
|
|
1483
|
-
[getSession]
|
|
1475
|
+
[getSession, setRequests]
|
|
1484
1476
|
);
|
|
1485
1477
|
return {
|
|
1486
1478
|
pendingRequests,
|
|
1487
|
-
|
|
1479
|
+
setRequests,
|
|
1488
1480
|
resolveRequest,
|
|
1489
1481
|
rejectRequest
|
|
1490
1482
|
};
|
|
@@ -1518,11 +1510,7 @@ function AomiRuntimeCore({
|
|
|
1518
1510
|
cancelGeneration: orchestratorCancel,
|
|
1519
1511
|
aomiClientRef
|
|
1520
1512
|
} = useRuntimeOrchestrator(aomiClient, {
|
|
1521
|
-
getPublicKey: () =>
|
|
1522
|
-
var _a;
|
|
1523
|
-
const userState = getUserState();
|
|
1524
|
-
return userState.isConnected ? (_a = userState.address) != null ? _a : void 0 : void 0;
|
|
1525
|
-
},
|
|
1513
|
+
getPublicKey: () => UserState3.isConnected(getUserState()) ? UserState3.address(getUserState()) : void 0,
|
|
1526
1514
|
getUserState,
|
|
1527
1515
|
getApp: getCurrentThreadApp,
|
|
1528
1516
|
getApiKey: () => getControlState().apiKey,
|
|
@@ -1530,17 +1518,20 @@ function AomiRuntimeCore({
|
|
|
1530
1518
|
var _a;
|
|
1531
1519
|
return (_a = getControlState().clientId) != null ? _a : void 0;
|
|
1532
1520
|
},
|
|
1533
|
-
|
|
1521
|
+
onPendingRequestsChange: walletHandler.setRequests,
|
|
1534
1522
|
onEvent: (event) => eventContext.dispatch(event)
|
|
1535
1523
|
});
|
|
1536
1524
|
sessionManagerRef.current = sessionManager;
|
|
1537
1525
|
const walletSnapshot = useCallback7(
|
|
1538
|
-
(nextUser) =>
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1526
|
+
(nextUser) => {
|
|
1527
|
+
var _a;
|
|
1528
|
+
return {
|
|
1529
|
+
address: UserState3.address(nextUser),
|
|
1530
|
+
chain_id: UserState3.chainId(nextUser),
|
|
1531
|
+
is_connected: (_a = UserState3.isConnected(nextUser)) != null ? _a : false,
|
|
1532
|
+
ens_name: typeof nextUser.ens_name === "string" ? nextUser.ens_name : void 0
|
|
1533
|
+
};
|
|
1534
|
+
},
|
|
1544
1535
|
[getUserState]
|
|
1545
1536
|
);
|
|
1546
1537
|
const lastWalletStateRef = useRef7(walletSnapshot(getUserState()));
|
|
@@ -1549,7 +1540,7 @@ function AomiRuntimeCore({
|
|
|
1549
1540
|
const unsubscribe = onUserStateChange(async (newUser) => {
|
|
1550
1541
|
const nextWalletState = walletSnapshot(newUser);
|
|
1551
1542
|
const prevWalletState = lastWalletStateRef.current;
|
|
1552
|
-
if (prevWalletState.address === nextWalletState.address && prevWalletState.
|
|
1543
|
+
if (prevWalletState.address === nextWalletState.address && prevWalletState.chain_id === nextWalletState.chain_id && prevWalletState.is_connected === nextWalletState.is_connected && prevWalletState.ens_name === nextWalletState.ens_name) {
|
|
1553
1544
|
return;
|
|
1554
1545
|
}
|
|
1555
1546
|
lastWalletStateRef.current = nextWalletState;
|
|
@@ -1570,26 +1561,50 @@ function AomiRuntimeCore({
|
|
|
1570
1561
|
]);
|
|
1571
1562
|
const threadContextRef = useRef7(threadContext);
|
|
1572
1563
|
threadContextRef.current = threadContext;
|
|
1573
|
-
const
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1564
|
+
const remoteThreadIdsRef = useRef7(/* @__PURE__ */ new Set());
|
|
1565
|
+
const warmedThreadIdsRef = useRef7(/* @__PURE__ */ new Set());
|
|
1566
|
+
const warmThread = useCallback7(
|
|
1567
|
+
async (threadId) => {
|
|
1568
|
+
if (!remoteThreadIdsRef.current.has(threadId) || warmedThreadIdsRef.current.has(threadId)) {
|
|
1569
|
+
return;
|
|
1570
|
+
}
|
|
1571
|
+
const userState = getUserState();
|
|
1572
|
+
await aomiClientRef.current.createThread(
|
|
1573
|
+
threadId,
|
|
1574
|
+
UserState3.isConnected(userState) ? UserState3.address(userState) : void 0
|
|
1575
|
+
);
|
|
1576
|
+
warmedThreadIdsRef.current.add(threadId);
|
|
1577
|
+
},
|
|
1578
|
+
[aomiClientRef, getUserState]
|
|
1579
|
+
);
|
|
1577
1580
|
useEffect3(() => {
|
|
1578
1581
|
const unsubscribe = eventContext.subscribe(
|
|
1579
1582
|
"user_state_request",
|
|
1580
1583
|
() => {
|
|
1584
|
+
var _a, _b, _c;
|
|
1585
|
+
const session = (_b = (_a = sessionManagerRef.current) == null ? void 0 : _a.get(threadContext.currentThreadId)) != null ? _b : getSession(threadContext.currentThreadId);
|
|
1581
1586
|
eventContext.sendOutboundSystem({
|
|
1582
1587
|
type: "user_state_response",
|
|
1583
1588
|
sessionId: threadContext.currentThreadId,
|
|
1584
|
-
payload: getUserState()
|
|
1589
|
+
payload: (_c = session.getUserState()) != null ? _c : getUserState()
|
|
1585
1590
|
});
|
|
1586
1591
|
}
|
|
1587
1592
|
);
|
|
1588
1593
|
return unsubscribe;
|
|
1589
|
-
}, [eventContext, threadContext.currentThreadId, getUserState]);
|
|
1594
|
+
}, [eventContext, threadContext.currentThreadId, getSession, getUserState]);
|
|
1590
1595
|
useEffect3(() => {
|
|
1591
|
-
|
|
1592
|
-
|
|
1596
|
+
const threadId = threadContext.currentThreadId;
|
|
1597
|
+
let cancelled = false;
|
|
1598
|
+
void (async () => {
|
|
1599
|
+
await warmThread(threadId);
|
|
1600
|
+
if (!cancelled) {
|
|
1601
|
+
await ensureInitialState(threadId);
|
|
1602
|
+
}
|
|
1603
|
+
})();
|
|
1604
|
+
return () => {
|
|
1605
|
+
cancelled = true;
|
|
1606
|
+
};
|
|
1607
|
+
}, [ensureInitialState, threadContext.currentThreadId, warmThread]);
|
|
1593
1608
|
useEffect3(() => {
|
|
1594
1609
|
const threadId = threadContext.currentThreadId;
|
|
1595
1610
|
const currentMeta = threadContext.getThreadMetadata(threadId);
|
|
@@ -1605,16 +1620,22 @@ function AomiRuntimeCore({
|
|
|
1605
1620
|
threadContext.currentThreadId
|
|
1606
1621
|
);
|
|
1607
1622
|
useEffect3(() => {
|
|
1608
|
-
const userAddress =
|
|
1609
|
-
if (!userAddress)
|
|
1623
|
+
const userAddress = UserState3.isConnected(user) ? UserState3.address(user) : void 0;
|
|
1624
|
+
if (!userAddress) {
|
|
1625
|
+
remoteThreadIdsRef.current.clear();
|
|
1626
|
+
warmedThreadIdsRef.current.clear();
|
|
1627
|
+
return;
|
|
1628
|
+
}
|
|
1610
1629
|
const fetchThreadList = async () => {
|
|
1611
1630
|
var _a, _b, _c;
|
|
1612
1631
|
try {
|
|
1613
1632
|
const threadList = await aomiClientRef.current.listThreads(userAddress);
|
|
1614
1633
|
const currentContext = threadContextRef.current;
|
|
1634
|
+
const remoteThreadIds = /* @__PURE__ */ new Set();
|
|
1615
1635
|
const newMetadata = new Map(currentContext.allThreadsMetadata);
|
|
1616
1636
|
let maxChatNum = currentContext.threadCnt;
|
|
1617
1637
|
for (const thread of threadList) {
|
|
1638
|
+
remoteThreadIds.add(thread.session_id);
|
|
1618
1639
|
const rawTitle = (_a = thread.title) != null ? _a : "";
|
|
1619
1640
|
const title = isPlaceholderTitle(rawTitle) ? "" : rawTitle;
|
|
1620
1641
|
const lastActive = ((_b = newMetadata.get(thread.session_id)) == null ? void 0 : _b.lastActiveAt) || (/* @__PURE__ */ new Date()).toISOString();
|
|
@@ -1633,16 +1654,26 @@ function AomiRuntimeCore({
|
|
|
1633
1654
|
}
|
|
1634
1655
|
}
|
|
1635
1656
|
}
|
|
1657
|
+
remoteThreadIdsRef.current = remoteThreadIds;
|
|
1658
|
+
warmedThreadIdsRef.current = new Set(
|
|
1659
|
+
Array.from(warmedThreadIdsRef.current).filter(
|
|
1660
|
+
(threadId) => remoteThreadIds.has(threadId)
|
|
1661
|
+
)
|
|
1662
|
+
);
|
|
1636
1663
|
currentContext.setThreadMetadata(newMetadata);
|
|
1637
1664
|
if (maxChatNum > currentContext.threadCnt) {
|
|
1638
1665
|
currentContext.setThreadCnt(maxChatNum);
|
|
1639
1666
|
}
|
|
1667
|
+
if (remoteThreadIds.has(currentContext.currentThreadId)) {
|
|
1668
|
+
await warmThread(currentContext.currentThreadId);
|
|
1669
|
+
await ensureInitialState(currentContext.currentThreadId);
|
|
1670
|
+
}
|
|
1640
1671
|
} catch (error) {
|
|
1641
1672
|
console.error("Failed to fetch thread list:", error);
|
|
1642
1673
|
}
|
|
1643
1674
|
};
|
|
1644
1675
|
void fetchThreadList();
|
|
1645
|
-
}, [user
|
|
1676
|
+
}, [user, aomiClientRef, ensureInitialState, warmThread]);
|
|
1646
1677
|
const threadListAdapter = useMemo2(
|
|
1647
1678
|
() => buildThreadListAdapter({
|
|
1648
1679
|
aomiClientRef,
|
|
@@ -1845,7 +1876,7 @@ function AomiRuntimeInner({
|
|
|
1845
1876
|
{
|
|
1846
1877
|
aomiClient,
|
|
1847
1878
|
sessionId: threadContext.currentThreadId,
|
|
1848
|
-
publicKey:
|
|
1879
|
+
publicKey: UserState4.isConnected(user) ? (_a = UserState4.address(user)) != null ? _a : void 0 : void 0,
|
|
1849
1880
|
getThreadMetadata: threadContext.getThreadMetadata,
|
|
1850
1881
|
updateThreadMetadata: threadContext.updateThreadMetadata,
|
|
1851
1882
|
children: /* @__PURE__ */ jsx7(
|