@aomi-labs/react 0.3.8 → 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/README.md +4 -4
- package/dist/index.cjs +108 -63
- 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 -56
- 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,6 +880,8 @@ 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
887
|
function useUser() {
|
|
@@ -898,10 +900,10 @@ function useUser() {
|
|
|
898
900
|
}
|
|
899
901
|
function UserContextProvider({ children }) {
|
|
900
902
|
const [user, setUserState] = useState3({
|
|
901
|
-
isConnected: false,
|
|
902
903
|
address: void 0,
|
|
903
|
-
|
|
904
|
-
|
|
904
|
+
chain_id: void 0,
|
|
905
|
+
is_connected: false,
|
|
906
|
+
ens_name: void 0,
|
|
905
907
|
ext: void 0
|
|
906
908
|
});
|
|
907
909
|
const userRef = useRef4(user);
|
|
@@ -911,7 +913,13 @@ function UserContextProvider({ children }) {
|
|
|
911
913
|
);
|
|
912
914
|
const setUser = useCallback4((data) => {
|
|
913
915
|
setUserState((prev) => {
|
|
914
|
-
|
|
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;
|
|
915
923
|
StateChangeCallbacks.current.forEach((callback) => {
|
|
916
924
|
callback(next);
|
|
917
925
|
});
|
|
@@ -920,12 +928,7 @@ function UserContextProvider({ children }) {
|
|
|
920
928
|
}, []);
|
|
921
929
|
const addExtValue = useCallback4((key, value) => {
|
|
922
930
|
setUserState((prev) => {
|
|
923
|
-
|
|
924
|
-
const next = __spreadProps(__spreadValues({}, prev), {
|
|
925
|
-
ext: __spreadProps(__spreadValues({}, (_a = prev.ext) != null ? _a : {}), {
|
|
926
|
-
[key]: value
|
|
927
|
-
})
|
|
928
|
-
});
|
|
931
|
+
const next = UserState.withExt(prev, key, value);
|
|
929
932
|
StateChangeCallbacks.current.forEach((callback) => {
|
|
930
933
|
callback(next);
|
|
931
934
|
});
|
|
@@ -934,10 +937,11 @@ function UserContextProvider({ children }) {
|
|
|
934
937
|
}, []);
|
|
935
938
|
const removeExtValue = useCallback4((key) => {
|
|
936
939
|
setUserState((prev) => {
|
|
937
|
-
|
|
940
|
+
const ext = prev.ext;
|
|
941
|
+
if (typeof ext !== "object" || ext === null || Array.isArray(ext) || !(key in ext)) {
|
|
938
942
|
return prev;
|
|
939
943
|
}
|
|
940
|
-
const nextExt = __spreadValues({},
|
|
944
|
+
const nextExt = __spreadValues({}, ext);
|
|
941
945
|
delete nextExt[key];
|
|
942
946
|
const next = __spreadProps(__spreadValues({}, prev), {
|
|
943
947
|
ext: Object.keys(nextExt).length > 0 ? nextExt : void 0
|
|
@@ -980,6 +984,7 @@ import {
|
|
|
980
984
|
AssistantRuntimeProvider,
|
|
981
985
|
useExternalStoreRuntime
|
|
982
986
|
} from "@assistant-ui/react";
|
|
987
|
+
import { UserState as UserState3 } from "@aomi-labs/client";
|
|
983
988
|
|
|
984
989
|
// packages/react/src/runtime/orchestrator.ts
|
|
985
990
|
import { useCallback as useCallback5, useEffect as useEffect2, useRef as useRef5, useState as useState4 } from "react";
|
|
@@ -1173,16 +1178,13 @@ function useRuntimeOrchestrator(aomiClient, options) {
|
|
|
1173
1178
|
})
|
|
1174
1179
|
);
|
|
1175
1180
|
cleanups.push(
|
|
1176
|
-
session.on(
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
var _a2;
|
|
1184
|
-
return (_a2 = options.onWalletRequest) == null ? void 0 : _a2.call(options, req);
|
|
1185
|
-
})
|
|
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
|
+
)
|
|
1186
1188
|
);
|
|
1187
1189
|
cleanups.push(
|
|
1188
1190
|
session.on("title_changed", ({ title }) => {
|
|
@@ -1208,7 +1210,7 @@ function useRuntimeOrchestrator(aomiClient, options) {
|
|
|
1208
1210
|
);
|
|
1209
1211
|
const ensureInitialState = useCallback5(
|
|
1210
1212
|
async (threadId) => {
|
|
1211
|
-
var _a;
|
|
1213
|
+
var _a, _b;
|
|
1212
1214
|
if (pendingFetches.current.has(threadId)) return;
|
|
1213
1215
|
pendingFetches.current.add(threadId);
|
|
1214
1216
|
try {
|
|
@@ -1216,6 +1218,7 @@ function useRuntimeOrchestrator(aomiClient, options) {
|
|
|
1216
1218
|
const userState = (_a = options.getUserState) == null ? void 0 : _a.call(options);
|
|
1217
1219
|
if (userState) session.resolveUserState(userState);
|
|
1218
1220
|
await session.fetchCurrentState();
|
|
1221
|
+
(_b = options.onPendingRequestsChange) == null ? void 0 : _b.call(options, session.getPendingRequests());
|
|
1219
1222
|
if (threadContextRef.current.currentThreadId === threadId) {
|
|
1220
1223
|
setIsRunning(session.getIsProcessing());
|
|
1221
1224
|
}
|
|
@@ -1232,7 +1235,7 @@ function useRuntimeOrchestrator(aomiClient, options) {
|
|
|
1232
1235
|
);
|
|
1233
1236
|
const sendMessage = useCallback5(
|
|
1234
1237
|
async (text, threadId) => {
|
|
1235
|
-
var _a;
|
|
1238
|
+
var _a, _b;
|
|
1236
1239
|
const session = getSession(threadId);
|
|
1237
1240
|
const userState = (_a = options.getUserState) == null ? void 0 : _a.call(options);
|
|
1238
1241
|
if (userState) session.resolveUserState(userState);
|
|
@@ -1250,6 +1253,7 @@ function useRuntimeOrchestrator(aomiClient, options) {
|
|
|
1250
1253
|
lastActiveAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
1251
1254
|
});
|
|
1252
1255
|
await session.sendAsync(text);
|
|
1256
|
+
(_b = options.onPendingRequestsChange) == null ? void 0 : _b.call(options, session.getPendingRequests());
|
|
1253
1257
|
},
|
|
1254
1258
|
[getSession]
|
|
1255
1259
|
);
|
|
@@ -1437,9 +1441,9 @@ function useWalletHandler({
|
|
|
1437
1441
|
getSession
|
|
1438
1442
|
}) {
|
|
1439
1443
|
const [pendingRequests, setPendingRequests] = useState5([]);
|
|
1440
|
-
const requestsRef = useRef6(
|
|
1441
|
-
const
|
|
1442
|
-
requestsRef.current = [...
|
|
1444
|
+
const requestsRef = useRef6(pendingRequests);
|
|
1445
|
+
const setRequests = useCallback6((requests) => {
|
|
1446
|
+
requestsRef.current = [...requests];
|
|
1443
1447
|
setPendingRequests(requestsRef.current);
|
|
1444
1448
|
}, []);
|
|
1445
1449
|
const resolveRequest = useCallback6(
|
|
@@ -1449,13 +1453,12 @@ function useWalletHandler({
|
|
|
1449
1453
|
console.error("[wallet-handler] No session available to resolve request");
|
|
1450
1454
|
return;
|
|
1451
1455
|
}
|
|
1452
|
-
requestsRef.current
|
|
1453
|
-
setPendingRequests(requestsRef.current);
|
|
1456
|
+
setRequests(requestsRef.current.filter((request) => request.id !== id));
|
|
1454
1457
|
void session.resolve(id, result).catch((err) => {
|
|
1455
1458
|
console.error("[wallet-handler] Failed to resolve request:", err);
|
|
1456
1459
|
});
|
|
1457
1460
|
},
|
|
1458
|
-
[getSession]
|
|
1461
|
+
[getSession, setRequests]
|
|
1459
1462
|
);
|
|
1460
1463
|
const rejectRequest = useCallback6(
|
|
1461
1464
|
(id, error) => {
|
|
@@ -1464,17 +1467,16 @@ function useWalletHandler({
|
|
|
1464
1467
|
console.error("[wallet-handler] No session available to reject request");
|
|
1465
1468
|
return;
|
|
1466
1469
|
}
|
|
1467
|
-
requestsRef.current
|
|
1468
|
-
setPendingRequests(requestsRef.current);
|
|
1470
|
+
setRequests(requestsRef.current.filter((request) => request.id !== id));
|
|
1469
1471
|
void session.reject(id, error).catch((err) => {
|
|
1470
1472
|
console.error("[wallet-handler] Failed to reject request:", err);
|
|
1471
1473
|
});
|
|
1472
1474
|
},
|
|
1473
|
-
[getSession]
|
|
1475
|
+
[getSession, setRequests]
|
|
1474
1476
|
);
|
|
1475
1477
|
return {
|
|
1476
1478
|
pendingRequests,
|
|
1477
|
-
|
|
1479
|
+
setRequests,
|
|
1478
1480
|
resolveRequest,
|
|
1479
1481
|
rejectRequest
|
|
1480
1482
|
};
|
|
@@ -1508,7 +1510,7 @@ function AomiRuntimeCore({
|
|
|
1508
1510
|
cancelGeneration: orchestratorCancel,
|
|
1509
1511
|
aomiClientRef
|
|
1510
1512
|
} = useRuntimeOrchestrator(aomiClient, {
|
|
1511
|
-
getPublicKey: () => getUserState().address,
|
|
1513
|
+
getPublicKey: () => UserState3.isConnected(getUserState()) ? UserState3.address(getUserState()) : void 0,
|
|
1512
1514
|
getUserState,
|
|
1513
1515
|
getApp: getCurrentThreadApp,
|
|
1514
1516
|
getApiKey: () => getControlState().apiKey,
|
|
@@ -1516,17 +1518,20 @@ function AomiRuntimeCore({
|
|
|
1516
1518
|
var _a;
|
|
1517
1519
|
return (_a = getControlState().clientId) != null ? _a : void 0;
|
|
1518
1520
|
},
|
|
1519
|
-
|
|
1521
|
+
onPendingRequestsChange: walletHandler.setRequests,
|
|
1520
1522
|
onEvent: (event) => eventContext.dispatch(event)
|
|
1521
1523
|
});
|
|
1522
1524
|
sessionManagerRef.current = sessionManager;
|
|
1523
1525
|
const walletSnapshot = useCallback7(
|
|
1524
|
-
(nextUser) =>
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
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
|
+
},
|
|
1530
1535
|
[getUserState]
|
|
1531
1536
|
);
|
|
1532
1537
|
const lastWalletStateRef = useRef7(walletSnapshot(getUserState()));
|
|
@@ -1535,7 +1540,7 @@ function AomiRuntimeCore({
|
|
|
1535
1540
|
const unsubscribe = onUserStateChange(async (newUser) => {
|
|
1536
1541
|
const nextWalletState = walletSnapshot(newUser);
|
|
1537
1542
|
const prevWalletState = lastWalletStateRef.current;
|
|
1538
|
-
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) {
|
|
1539
1544
|
return;
|
|
1540
1545
|
}
|
|
1541
1546
|
lastWalletStateRef.current = nextWalletState;
|
|
@@ -1556,26 +1561,50 @@ function AomiRuntimeCore({
|
|
|
1556
1561
|
]);
|
|
1557
1562
|
const threadContextRef = useRef7(threadContext);
|
|
1558
1563
|
threadContextRef.current = threadContext;
|
|
1559
|
-
const
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
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
|
+
);
|
|
1563
1580
|
useEffect3(() => {
|
|
1564
1581
|
const unsubscribe = eventContext.subscribe(
|
|
1565
1582
|
"user_state_request",
|
|
1566
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);
|
|
1567
1586
|
eventContext.sendOutboundSystem({
|
|
1568
1587
|
type: "user_state_response",
|
|
1569
1588
|
sessionId: threadContext.currentThreadId,
|
|
1570
|
-
payload: getUserState()
|
|
1589
|
+
payload: (_c = session.getUserState()) != null ? _c : getUserState()
|
|
1571
1590
|
});
|
|
1572
1591
|
}
|
|
1573
1592
|
);
|
|
1574
1593
|
return unsubscribe;
|
|
1575
|
-
}, [eventContext, threadContext.currentThreadId, getUserState]);
|
|
1594
|
+
}, [eventContext, threadContext.currentThreadId, getSession, getUserState]);
|
|
1576
1595
|
useEffect3(() => {
|
|
1577
|
-
|
|
1578
|
-
|
|
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]);
|
|
1579
1608
|
useEffect3(() => {
|
|
1580
1609
|
const threadId = threadContext.currentThreadId;
|
|
1581
1610
|
const currentMeta = threadContext.getThreadMetadata(threadId);
|
|
@@ -1591,16 +1620,22 @@ function AomiRuntimeCore({
|
|
|
1591
1620
|
threadContext.currentThreadId
|
|
1592
1621
|
);
|
|
1593
1622
|
useEffect3(() => {
|
|
1594
|
-
const userAddress = user.address;
|
|
1595
|
-
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
|
+
}
|
|
1596
1629
|
const fetchThreadList = async () => {
|
|
1597
1630
|
var _a, _b, _c;
|
|
1598
1631
|
try {
|
|
1599
1632
|
const threadList = await aomiClientRef.current.listThreads(userAddress);
|
|
1600
1633
|
const currentContext = threadContextRef.current;
|
|
1634
|
+
const remoteThreadIds = /* @__PURE__ */ new Set();
|
|
1601
1635
|
const newMetadata = new Map(currentContext.allThreadsMetadata);
|
|
1602
1636
|
let maxChatNum = currentContext.threadCnt;
|
|
1603
1637
|
for (const thread of threadList) {
|
|
1638
|
+
remoteThreadIds.add(thread.session_id);
|
|
1604
1639
|
const rawTitle = (_a = thread.title) != null ? _a : "";
|
|
1605
1640
|
const title = isPlaceholderTitle(rawTitle) ? "" : rawTitle;
|
|
1606
1641
|
const lastActive = ((_b = newMetadata.get(thread.session_id)) == null ? void 0 : _b.lastActiveAt) || (/* @__PURE__ */ new Date()).toISOString();
|
|
@@ -1619,16 +1654,26 @@ function AomiRuntimeCore({
|
|
|
1619
1654
|
}
|
|
1620
1655
|
}
|
|
1621
1656
|
}
|
|
1657
|
+
remoteThreadIdsRef.current = remoteThreadIds;
|
|
1658
|
+
warmedThreadIdsRef.current = new Set(
|
|
1659
|
+
Array.from(warmedThreadIdsRef.current).filter(
|
|
1660
|
+
(threadId) => remoteThreadIds.has(threadId)
|
|
1661
|
+
)
|
|
1662
|
+
);
|
|
1622
1663
|
currentContext.setThreadMetadata(newMetadata);
|
|
1623
1664
|
if (maxChatNum > currentContext.threadCnt) {
|
|
1624
1665
|
currentContext.setThreadCnt(maxChatNum);
|
|
1625
1666
|
}
|
|
1667
|
+
if (remoteThreadIds.has(currentContext.currentThreadId)) {
|
|
1668
|
+
await warmThread(currentContext.currentThreadId);
|
|
1669
|
+
await ensureInitialState(currentContext.currentThreadId);
|
|
1670
|
+
}
|
|
1626
1671
|
} catch (error) {
|
|
1627
1672
|
console.error("Failed to fetch thread list:", error);
|
|
1628
1673
|
}
|
|
1629
1674
|
};
|
|
1630
1675
|
void fetchThreadList();
|
|
1631
|
-
}, [user
|
|
1676
|
+
}, [user, aomiClientRef, ensureInitialState, warmThread]);
|
|
1632
1677
|
const threadListAdapter = useMemo2(
|
|
1633
1678
|
() => buildThreadListAdapter({
|
|
1634
1679
|
aomiClientRef,
|
|
@@ -1831,7 +1876,7 @@ function AomiRuntimeInner({
|
|
|
1831
1876
|
{
|
|
1832
1877
|
aomiClient,
|
|
1833
1878
|
sessionId: threadContext.currentThreadId,
|
|
1834
|
-
publicKey: (_a =
|
|
1879
|
+
publicKey: UserState4.isConnected(user) ? (_a = UserState4.address(user)) != null ? _a : void 0 : void 0,
|
|
1835
1880
|
getThreadMetadata: threadContext.getThreadMetadata,
|
|
1836
1881
|
updateThreadMetadata: threadContext.updateThreadMetadata,
|
|
1837
1882
|
children: /* @__PURE__ */ jsx7(
|