@aomi-labs/react 0.3.19 → 0.3.21
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 +349 -115
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +65 -31
- package/dist/index.d.ts +65 -31
- package/dist/index.js +356 -119
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -50,34 +50,34 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
50
50
|
// packages/react/src/index.ts
|
|
51
51
|
var index_exports = {};
|
|
52
52
|
__export(index_exports, {
|
|
53
|
-
AomiClient: () =>
|
|
53
|
+
AomiClient: () => import_client9.AomiClient,
|
|
54
54
|
AomiRuntimeProvider: () => AomiRuntimeProvider,
|
|
55
55
|
ControlContextProvider: () => ControlContextProvider,
|
|
56
|
-
DISABLED_PROVIDER_STATE: () =>
|
|
56
|
+
DISABLED_PROVIDER_STATE: () => import_client10.DISABLED_PROVIDER_STATE,
|
|
57
57
|
EventContextProvider: () => EventContextProvider,
|
|
58
|
-
|
|
58
|
+
ExtUserProvider: () => ExtUserProvider,
|
|
59
|
+
MAX_AUTO_FEE_WEI: () => import_client10.MAX_AUTO_FEE_WEI,
|
|
59
60
|
NotificationContextProvider: () => NotificationContextProvider,
|
|
60
61
|
RuntimeUserStateProvider: () => RuntimeUserStateProvider,
|
|
61
62
|
SUPPORTED_CHAINS: () => SUPPORTED_CHAINS,
|
|
62
63
|
ThreadContextProvider: () => ThreadContextProvider,
|
|
63
|
-
UserContextProvider: () => UserContextProvider,
|
|
64
64
|
UserState: () => import_client2.UserState,
|
|
65
|
-
aaModeFromExecutionKind: () =>
|
|
66
|
-
appendFeeCallToPayload: () =>
|
|
67
|
-
buildFeeAAWalletCall: () =>
|
|
65
|
+
aaModeFromExecutionKind: () => import_client10.aaModeFromExecutionKind,
|
|
66
|
+
appendFeeCallToPayload: () => import_client10.appendFeeCallToPayload,
|
|
67
|
+
buildFeeAAWalletCall: () => import_client10.buildFeeAAWalletCall,
|
|
68
68
|
cn: () => cn,
|
|
69
|
-
executeWalletCalls: () =>
|
|
69
|
+
executeWalletCalls: () => import_client10.executeWalletCalls,
|
|
70
70
|
formatAddress: () => formatAddress,
|
|
71
71
|
getChainInfo: () => getChainInfo,
|
|
72
72
|
getNetworkName: () => getNetworkName,
|
|
73
|
-
hydrateTxPayloadFromUserState: () =>
|
|
73
|
+
hydrateTxPayloadFromUserState: () => import_client10.hydrateTxPayloadFromUserState,
|
|
74
74
|
initThreadControl: () => initThreadControl,
|
|
75
|
-
normalizeSimulatedFee: () =>
|
|
76
|
-
parseChainId: () =>
|
|
75
|
+
normalizeSimulatedFee: () => import_client10.normalizeSimulatedFee,
|
|
76
|
+
parseChainId: () => import_client10.parseChainId,
|
|
77
77
|
resolveAutoModel: () => resolveAutoModel,
|
|
78
|
-
toAAWalletCall: () =>
|
|
79
|
-
toAAWalletCalls: () =>
|
|
80
|
-
toViemSignTypedDataArgs: () =>
|
|
78
|
+
toAAWalletCall: () => import_client10.toAAWalletCall,
|
|
79
|
+
toAAWalletCalls: () => import_client10.toAAWalletCalls,
|
|
80
|
+
toViemSignTypedDataArgs: () => import_client10.toViemSignTypedDataArgs,
|
|
81
81
|
useAomiRuntime: () => useAomiRuntime,
|
|
82
82
|
useControl: () => useControl,
|
|
83
83
|
useCurrentThreadMessages: () => useCurrentThreadMessages,
|
|
@@ -90,12 +90,12 @@ __export(index_exports, {
|
|
|
90
90
|
useWalletHandler: () => useWalletHandler
|
|
91
91
|
});
|
|
92
92
|
module.exports = __toCommonJS(index_exports);
|
|
93
|
-
var import_client8 = require("@aomi-labs/client");
|
|
94
93
|
var import_client9 = require("@aomi-labs/client");
|
|
94
|
+
var import_client10 = require("@aomi-labs/client");
|
|
95
95
|
|
|
96
96
|
// packages/react/src/runtime/aomi-runtime.tsx
|
|
97
97
|
var import_react12 = require("react");
|
|
98
|
-
var
|
|
98
|
+
var import_client8 = require("@aomi-labs/client");
|
|
99
99
|
|
|
100
100
|
// packages/react/src/contexts/control-context.tsx
|
|
101
101
|
var import_react = require("react");
|
|
@@ -299,6 +299,8 @@ var ThreadStore = class {
|
|
|
299
299
|
|
|
300
300
|
// packages/react/src/utils/model-selection.ts
|
|
301
301
|
var PREFERRED_DEFAULT_MODEL_PATTERNS = [
|
|
302
|
+
/^claude.*opus.*4[.-]?6/i,
|
|
303
|
+
/^claude.*4[.-]?6.*opus/i,
|
|
302
304
|
/^claude-4\.5-haiku/i,
|
|
303
305
|
/^claude.*haiku/i,
|
|
304
306
|
/^gpt-4o-mini/i,
|
|
@@ -342,14 +344,17 @@ function getControlSessionId(clientId, fallbackSessionId) {
|
|
|
342
344
|
|
|
343
345
|
// packages/react/src/contexts/control-context.tsx
|
|
344
346
|
var import_jsx_runtime = require("react/jsx-runtime");
|
|
345
|
-
var API_KEY_STORAGE_KEY = "
|
|
346
|
-
var
|
|
347
|
+
var API_KEY_STORAGE_KEY = "aomi_secret_key";
|
|
348
|
+
var BYOK_KEYS_STORAGE_KEY = "aomi_byok_keys";
|
|
347
349
|
var MODEL_SELECTION_STORAGE_KEY = "aomi_model_selection";
|
|
348
|
-
var
|
|
350
|
+
var BYOK_SECRET_PREFIX = "PROVIDER_KEY:";
|
|
349
351
|
function getDefaultApp(apps) {
|
|
350
352
|
var _a;
|
|
351
353
|
return apps.includes("default") ? "default" : (_a = apps[0]) != null ? _a : null;
|
|
352
354
|
}
|
|
355
|
+
function namesFromDescriptors(apps) {
|
|
356
|
+
return apps.map((a) => a.name);
|
|
357
|
+
}
|
|
353
358
|
function readStoredModelPreference() {
|
|
354
359
|
var _a;
|
|
355
360
|
try {
|
|
@@ -421,9 +426,10 @@ function ControlContextProvider({
|
|
|
421
426
|
clientId: getOrCreateClientId(),
|
|
422
427
|
availableModels: [],
|
|
423
428
|
authorizedApps: [],
|
|
429
|
+
appDescriptors: [],
|
|
424
430
|
defaultModel: null,
|
|
425
431
|
defaultApp: null,
|
|
426
|
-
|
|
432
|
+
byokKeys: {}
|
|
427
433
|
}));
|
|
428
434
|
const stateRef = (0, import_react.useRef)(state);
|
|
429
435
|
stateRef.current = state;
|
|
@@ -466,10 +472,10 @@ function ControlContextProvider({
|
|
|
466
472
|
(0, import_react.useEffect)(() => {
|
|
467
473
|
var _a2;
|
|
468
474
|
try {
|
|
469
|
-
const raw = (_a2 = globalThis.localStorage) == null ? void 0 : _a2.getItem(
|
|
475
|
+
const raw = (_a2 = globalThis.localStorage) == null ? void 0 : _a2.getItem(BYOK_KEYS_STORAGE_KEY);
|
|
470
476
|
if (raw) {
|
|
471
477
|
const parsed = JSON.parse(raw);
|
|
472
|
-
setStateInternal((prev) => __spreadProps(__spreadValues({}, prev), {
|
|
478
|
+
setStateInternal((prev) => __spreadProps(__spreadValues({}, prev), { byokKeys: parsed }));
|
|
473
479
|
}
|
|
474
480
|
} catch (e) {
|
|
475
481
|
}
|
|
@@ -488,50 +494,53 @@ function ControlContextProvider({
|
|
|
488
494
|
(0, import_react.useEffect)(() => {
|
|
489
495
|
var _a2, _b2;
|
|
490
496
|
try {
|
|
491
|
-
const keys = state.
|
|
497
|
+
const keys = state.byokKeys;
|
|
492
498
|
if (Object.keys(keys).length > 0) {
|
|
493
499
|
(_a2 = globalThis.localStorage) == null ? void 0 : _a2.setItem(
|
|
494
|
-
|
|
500
|
+
BYOK_KEYS_STORAGE_KEY,
|
|
495
501
|
JSON.stringify(keys)
|
|
496
502
|
);
|
|
497
503
|
} else {
|
|
498
|
-
(_b2 = globalThis.localStorage) == null ? void 0 : _b2.removeItem(
|
|
504
|
+
(_b2 = globalThis.localStorage) == null ? void 0 : _b2.removeItem(BYOK_KEYS_STORAGE_KEY);
|
|
499
505
|
}
|
|
500
506
|
} catch (e) {
|
|
501
507
|
}
|
|
502
|
-
}, [state.
|
|
508
|
+
}, [state.byokKeys]);
|
|
503
509
|
(0, import_react.useEffect)(() => {
|
|
504
510
|
if (!state.clientId) return;
|
|
505
|
-
const keys = stateRef.current.
|
|
511
|
+
const keys = stateRef.current.byokKeys;
|
|
506
512
|
if (Object.keys(keys).length === 0) return;
|
|
507
513
|
const secrets = {};
|
|
508
514
|
for (const [provider, entry] of Object.entries(keys)) {
|
|
509
|
-
secrets[`${
|
|
515
|
+
secrets[`${BYOK_SECRET_PREFIX}${provider}`] = entry.apiKey;
|
|
510
516
|
}
|
|
511
517
|
void aomiClientRef.current.ingestSecrets(getCurrentControlSessionId(), state.clientId, secrets).catch((err) => {
|
|
512
|
-
console.error("Failed to auto-ingest
|
|
518
|
+
console.error("Failed to auto-ingest BYOK keys:", err);
|
|
513
519
|
});
|
|
514
|
-
}, [getCurrentControlSessionId, state.clientId, state.
|
|
520
|
+
}, [getCurrentControlSessionId, state.clientId, state.byokKeys]);
|
|
515
521
|
(0, import_react.useEffect)(() => {
|
|
516
522
|
const fetchApps = async () => {
|
|
517
523
|
var _a2;
|
|
518
524
|
try {
|
|
519
|
-
const
|
|
525
|
+
const descriptors = await aomiClientRef.current.getApps(
|
|
520
526
|
getCurrentControlSessionId(),
|
|
521
527
|
{
|
|
522
528
|
publicKey: publicKeyRef.current,
|
|
523
529
|
apiKey: (_a2 = stateRef.current.apiKey) != null ? _a2 : void 0
|
|
524
530
|
}
|
|
525
531
|
);
|
|
526
|
-
const
|
|
532
|
+
const names = namesFromDescriptors(descriptors);
|
|
533
|
+
const defaultApp = getDefaultApp(names);
|
|
527
534
|
setStateInternal((prev) => __spreadProps(__spreadValues({}, prev), {
|
|
528
|
-
authorizedApps:
|
|
535
|
+
authorizedApps: names,
|
|
536
|
+
appDescriptors: descriptors,
|
|
529
537
|
defaultApp
|
|
530
538
|
}));
|
|
531
539
|
} catch (error) {
|
|
532
540
|
console.error("Failed to fetch apps:", error);
|
|
533
541
|
setStateInternal((prev) => __spreadProps(__spreadValues({}, prev), {
|
|
534
542
|
authorizedApps: ["default"],
|
|
543
|
+
appDescriptors: [{ name: "default" }],
|
|
535
544
|
defaultApp: "default"
|
|
536
545
|
}));
|
|
537
546
|
}
|
|
@@ -562,29 +571,53 @@ function ControlContextProvider({
|
|
|
562
571
|
});
|
|
563
572
|
}, []);
|
|
564
573
|
const ingestSecrets = (0, import_react.useCallback)(
|
|
565
|
-
async (secrets) => {
|
|
574
|
+
async (secrets, app) => {
|
|
566
575
|
const clientId = stateRef.current.clientId;
|
|
567
576
|
if (!clientId) throw new Error("clientId not initialized");
|
|
568
577
|
const { handles } = await aomiClientRef.current.ingestSecrets(
|
|
569
578
|
getCurrentControlSessionId(),
|
|
570
579
|
clientId,
|
|
571
|
-
secrets
|
|
580
|
+
secrets,
|
|
581
|
+
app
|
|
572
582
|
);
|
|
573
583
|
return handles;
|
|
574
584
|
},
|
|
575
585
|
[getCurrentControlSessionId]
|
|
576
586
|
);
|
|
577
|
-
const clearSecrets = (0, import_react.useCallback)(
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
_a2
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
587
|
+
const clearSecrets = (0, import_react.useCallback)(
|
|
588
|
+
async (app) => {
|
|
589
|
+
var _a2, _b2;
|
|
590
|
+
const clientId = stateRef.current.clientId;
|
|
591
|
+
if (!clientId) return;
|
|
592
|
+
await ((_b2 = (_a2 = aomiClientRef.current).clearSecrets) == null ? void 0 : _b2.call(
|
|
593
|
+
_a2,
|
|
594
|
+
getCurrentControlSessionId(),
|
|
595
|
+
clientId,
|
|
596
|
+
app
|
|
597
|
+
));
|
|
598
|
+
},
|
|
599
|
+
[getCurrentControlSessionId]
|
|
600
|
+
);
|
|
601
|
+
const deleteSecret = (0, import_react.useCallback)(
|
|
602
|
+
async (name, app) => {
|
|
603
|
+
const clientId = stateRef.current.clientId;
|
|
604
|
+
if (!clientId) return;
|
|
605
|
+
await aomiClientRef.current.deleteSecret(
|
|
606
|
+
getCurrentControlSessionId(),
|
|
607
|
+
clientId,
|
|
608
|
+
name,
|
|
609
|
+
app
|
|
610
|
+
);
|
|
611
|
+
},
|
|
612
|
+
[getCurrentControlSessionId]
|
|
613
|
+
);
|
|
614
|
+
const listSecrets = (0, import_react.useCallback)(async () => {
|
|
615
|
+
const { by_app } = await aomiClientRef.current.listSecrets(
|
|
616
|
+
getCurrentControlSessionId()
|
|
617
|
+
);
|
|
618
|
+
return by_app;
|
|
586
619
|
}, [getCurrentControlSessionId]);
|
|
587
|
-
const
|
|
620
|
+
const setByok = (0, import_react.useCallback)(
|
|
588
621
|
async (provider, apiKey, label) => {
|
|
589
622
|
const trimmed = apiKey.trim();
|
|
590
623
|
if (!trimmed) return;
|
|
@@ -595,7 +628,7 @@ function ControlContextProvider({
|
|
|
595
628
|
};
|
|
596
629
|
setStateInternal((prev) => {
|
|
597
630
|
const next = __spreadProps(__spreadValues({}, prev), {
|
|
598
|
-
|
|
631
|
+
byokKeys: __spreadProps(__spreadValues({}, prev.byokKeys), { [provider]: entry })
|
|
599
632
|
});
|
|
600
633
|
callbacks.current.forEach((cb) => cb(next));
|
|
601
634
|
return next;
|
|
@@ -607,41 +640,41 @@ function ControlContextProvider({
|
|
|
607
640
|
getCurrentControlSessionId(),
|
|
608
641
|
clientId,
|
|
609
642
|
{
|
|
610
|
-
[`${
|
|
643
|
+
[`${BYOK_SECRET_PREFIX}${provider}`]: trimmed
|
|
611
644
|
}
|
|
612
645
|
);
|
|
613
646
|
} catch (err) {
|
|
614
|
-
console.error("Failed to ingest
|
|
647
|
+
console.error("Failed to ingest BYOK key:", err);
|
|
615
648
|
}
|
|
616
649
|
}
|
|
617
650
|
},
|
|
618
651
|
[getCurrentControlSessionId]
|
|
619
652
|
);
|
|
620
|
-
const
|
|
653
|
+
const removeByok = (0, import_react.useCallback)(
|
|
621
654
|
async (provider) => {
|
|
622
655
|
const clientId = stateRef.current.clientId;
|
|
623
656
|
if (clientId) {
|
|
624
657
|
await aomiClientRef.current.deleteSecret(
|
|
625
658
|
getCurrentControlSessionId(),
|
|
626
659
|
clientId,
|
|
627
|
-
`${
|
|
660
|
+
`${BYOK_SECRET_PREFIX}${provider}`
|
|
628
661
|
);
|
|
629
662
|
}
|
|
630
663
|
setStateInternal((prev) => {
|
|
631
|
-
const _a2 = prev.
|
|
632
|
-
const next = __spreadProps(__spreadValues({}, prev), {
|
|
664
|
+
const _a2 = prev.byokKeys, { [provider]: _ } = _a2, rest = __objRest(_a2, [__restKey(provider)]);
|
|
665
|
+
const next = __spreadProps(__spreadValues({}, prev), { byokKeys: rest });
|
|
633
666
|
callbacks.current.forEach((cb) => cb(next));
|
|
634
667
|
return next;
|
|
635
668
|
});
|
|
636
669
|
},
|
|
637
670
|
[getCurrentControlSessionId]
|
|
638
671
|
);
|
|
639
|
-
const
|
|
640
|
-
() => stateRef.current.
|
|
672
|
+
const getByokKeys = (0, import_react.useCallback)(
|
|
673
|
+
() => stateRef.current.byokKeys,
|
|
641
674
|
[]
|
|
642
675
|
);
|
|
643
|
-
const
|
|
644
|
-
const keys = stateRef.current.
|
|
676
|
+
const hasByok = (0, import_react.useCallback)((provider) => {
|
|
677
|
+
const keys = stateRef.current.byokKeys;
|
|
645
678
|
if (provider) return provider in keys;
|
|
646
679
|
return Object.keys(keys).length > 0;
|
|
647
680
|
}, []);
|
|
@@ -663,23 +696,26 @@ function ControlContextProvider({
|
|
|
663
696
|
const getAuthorizedApps = (0, import_react.useCallback)(async () => {
|
|
664
697
|
var _a2;
|
|
665
698
|
try {
|
|
666
|
-
const
|
|
699
|
+
const descriptors = await aomiClientRef.current.getApps(
|
|
667
700
|
getCurrentControlSessionId(),
|
|
668
701
|
{
|
|
669
702
|
publicKey: publicKeyRef.current,
|
|
670
703
|
apiKey: (_a2 = stateRef.current.apiKey) != null ? _a2 : void 0
|
|
671
704
|
}
|
|
672
705
|
);
|
|
673
|
-
const
|
|
706
|
+
const names = namesFromDescriptors(descriptors);
|
|
707
|
+
const defaultApp = getDefaultApp(names);
|
|
674
708
|
setStateInternal((prev) => __spreadProps(__spreadValues({}, prev), {
|
|
675
|
-
authorizedApps:
|
|
709
|
+
authorizedApps: names,
|
|
710
|
+
appDescriptors: descriptors,
|
|
676
711
|
defaultApp
|
|
677
712
|
}));
|
|
678
|
-
return
|
|
713
|
+
return names;
|
|
679
714
|
} catch (error) {
|
|
680
715
|
console.error("Failed to fetch apps:", error);
|
|
681
716
|
setStateInternal((prev) => __spreadProps(__spreadValues({}, prev), {
|
|
682
717
|
authorizedApps: ["default"],
|
|
718
|
+
appDescriptors: [{ name: "default" }],
|
|
683
719
|
defaultApp: "default"
|
|
684
720
|
}));
|
|
685
721
|
return ["default"];
|
|
@@ -934,10 +970,12 @@ function ControlContextProvider({
|
|
|
934
970
|
setApiKey,
|
|
935
971
|
ingestSecrets,
|
|
936
972
|
clearSecrets,
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
973
|
+
deleteSecret,
|
|
974
|
+
listSecrets,
|
|
975
|
+
setByok,
|
|
976
|
+
removeByok,
|
|
977
|
+
getByokKeys,
|
|
978
|
+
hasByok,
|
|
941
979
|
getAvailableModels,
|
|
942
980
|
getAuthorizedApps,
|
|
943
981
|
getCurrentThreadControl,
|
|
@@ -1048,19 +1086,30 @@ function NotificationContextProvider({
|
|
|
1048
1086
|
children
|
|
1049
1087
|
}) {
|
|
1050
1088
|
const [notifications, setNotifications] = (0, import_react3.useState)([]);
|
|
1089
|
+
const paymentRequiredIdRef = (0, import_react3.useRef)(null);
|
|
1051
1090
|
const showNotification = (0, import_react3.useCallback)((params) => {
|
|
1091
|
+
if (params.kind === "payment_required" && paymentRequiredIdRef.current) {
|
|
1092
|
+
return paymentRequiredIdRef.current;
|
|
1093
|
+
}
|
|
1052
1094
|
const id = generateId();
|
|
1053
1095
|
const notification = __spreadProps(__spreadValues({}, params), {
|
|
1054
1096
|
id,
|
|
1055
1097
|
timestamp: Date.now()
|
|
1056
1098
|
});
|
|
1099
|
+
if (params.kind === "payment_required") {
|
|
1100
|
+
paymentRequiredIdRef.current = id;
|
|
1101
|
+
}
|
|
1057
1102
|
setNotifications((prev) => [notification, ...prev]);
|
|
1058
1103
|
return id;
|
|
1059
1104
|
}, []);
|
|
1060
1105
|
const dismissNotification = (0, import_react3.useCallback)((id) => {
|
|
1106
|
+
if (paymentRequiredIdRef.current === id) {
|
|
1107
|
+
paymentRequiredIdRef.current = null;
|
|
1108
|
+
}
|
|
1061
1109
|
setNotifications((prev) => prev.filter((n) => n.id !== id));
|
|
1062
1110
|
}, []);
|
|
1063
1111
|
const clearAll = (0, import_react3.useCallback)(() => {
|
|
1112
|
+
paymentRequiredIdRef.current = null;
|
|
1064
1113
|
setNotifications([]);
|
|
1065
1114
|
}, []);
|
|
1066
1115
|
const value = {
|
|
@@ -1116,7 +1165,7 @@ function useCurrentThreadMetadata() {
|
|
|
1116
1165
|
);
|
|
1117
1166
|
}
|
|
1118
1167
|
|
|
1119
|
-
// packages/react/src/contexts/user-context.tsx
|
|
1168
|
+
// packages/react/src/contexts/ext-user-context.tsx
|
|
1120
1169
|
var import_react5 = require("react");
|
|
1121
1170
|
var import_client = require("@aomi-labs/client");
|
|
1122
1171
|
var import_client2 = require("@aomi-labs/client");
|
|
@@ -1125,7 +1174,7 @@ var UserContext = (0, import_react5.createContext)(void 0);
|
|
|
1125
1174
|
function useUser() {
|
|
1126
1175
|
const context = (0, import_react5.useContext)(UserContext);
|
|
1127
1176
|
if (!context) {
|
|
1128
|
-
throw new Error("useUser must be used within
|
|
1177
|
+
throw new Error("useUser must be used within ExtUserProvider");
|
|
1129
1178
|
}
|
|
1130
1179
|
return {
|
|
1131
1180
|
user: context.user,
|
|
@@ -1136,7 +1185,14 @@ function useUser() {
|
|
|
1136
1185
|
onUserStateChange: context.onUserStateChange
|
|
1137
1186
|
};
|
|
1138
1187
|
}
|
|
1139
|
-
function
|
|
1188
|
+
function ExtUserProvider({ children }) {
|
|
1189
|
+
const parent = (0, import_react5.useContext)(UserContext);
|
|
1190
|
+
if (parent) {
|
|
1191
|
+
return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(import_jsx_runtime5.Fragment, { children });
|
|
1192
|
+
}
|
|
1193
|
+
return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(ExtUserProviderImpl, { children });
|
|
1194
|
+
}
|
|
1195
|
+
function ExtUserProviderImpl({ children }) {
|
|
1140
1196
|
const [user, setUserState] = (0, import_react5.useState)({
|
|
1141
1197
|
address: void 0,
|
|
1142
1198
|
chain_id: void 0,
|
|
@@ -1163,7 +1219,7 @@ function UserContextProvider({ children }) {
|
|
|
1163
1219
|
}, []);
|
|
1164
1220
|
const setUser = (0, import_react5.useCallback)((data) => {
|
|
1165
1221
|
setUserState((prev) => {
|
|
1166
|
-
var _a, _b
|
|
1222
|
+
var _a, _b;
|
|
1167
1223
|
const normalizedData = pruneUndefined((_a = import_client.UserState.normalize(data)) != null ? _a : {});
|
|
1168
1224
|
const nextPartial = __spreadValues({}, normalizedData);
|
|
1169
1225
|
if (nextPartial.is_connected === true && nextPartial.chain_id === void 0) {
|
|
@@ -1173,11 +1229,41 @@ function UserContextProvider({ children }) {
|
|
|
1173
1229
|
delete nextPartial.is_connected;
|
|
1174
1230
|
}
|
|
1175
1231
|
}
|
|
1176
|
-
const
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1232
|
+
const merged = (_b = import_client.UserState.normalize(__spreadValues(__spreadValues({}, prev), nextPartial))) != null ? _b : prev;
|
|
1233
|
+
let next;
|
|
1234
|
+
if (nextPartial.is_connected === false) {
|
|
1235
|
+
next = __spreadProps(__spreadValues({}, merged), {
|
|
1236
|
+
address: void 0,
|
|
1237
|
+
chain_id: void 0,
|
|
1238
|
+
ens_name: void 0,
|
|
1239
|
+
wallet_kind: void 0,
|
|
1240
|
+
aa_mode: void 0,
|
|
1241
|
+
smart_account_4337: void 0,
|
|
1242
|
+
delegation_7702: void 0,
|
|
1243
|
+
svm_address: void 0,
|
|
1244
|
+
wallet_provider: void 0,
|
|
1245
|
+
auth_method: void 0,
|
|
1246
|
+
sponsored: void 0,
|
|
1247
|
+
sponsor_provider: void 0,
|
|
1248
|
+
sponsor_account: void 0,
|
|
1249
|
+
pending_txs: void 0,
|
|
1250
|
+
pending_eip712s: void 0,
|
|
1251
|
+
pending_solana_txs: void 0
|
|
1252
|
+
});
|
|
1253
|
+
} else {
|
|
1254
|
+
const prevAddress = import_client.UserState.address(prev);
|
|
1255
|
+
const nextAddress = import_client.UserState.address(merged);
|
|
1256
|
+
const addressChanged = prevAddress !== void 0 && nextAddress !== void 0 && prevAddress.toLowerCase() !== nextAddress.toLowerCase();
|
|
1257
|
+
next = addressChanged ? __spreadProps(__spreadValues({}, merged), {
|
|
1258
|
+
aa_mode: void 0,
|
|
1259
|
+
smart_account_4337: void 0,
|
|
1260
|
+
delegation_7702: void 0,
|
|
1261
|
+
ens_name: void 0,
|
|
1262
|
+
pending_txs: void 0,
|
|
1263
|
+
pending_eip712s: void 0,
|
|
1264
|
+
pending_solana_txs: void 0
|
|
1265
|
+
}) : merged;
|
|
1266
|
+
}
|
|
1181
1267
|
notifyStateChange(next);
|
|
1182
1268
|
return next;
|
|
1183
1269
|
});
|
|
@@ -1233,11 +1319,11 @@ function UserContextProvider({ children }) {
|
|
|
1233
1319
|
// packages/react/src/runtime/core.tsx
|
|
1234
1320
|
var import_react10 = require("react");
|
|
1235
1321
|
var import_react11 = require("@assistant-ui/react");
|
|
1236
|
-
var
|
|
1322
|
+
var import_client7 = require("@aomi-labs/client");
|
|
1237
1323
|
|
|
1238
1324
|
// packages/react/src/runtime/orchestrator.ts
|
|
1239
1325
|
var import_react6 = require("react");
|
|
1240
|
-
var
|
|
1326
|
+
var import_client5 = require("@aomi-labs/client");
|
|
1241
1327
|
|
|
1242
1328
|
// packages/react/src/runtime/session-manager.ts
|
|
1243
1329
|
var import_client3 = require("@aomi-labs/client");
|
|
@@ -1297,6 +1383,7 @@ var SessionManager = class {
|
|
|
1297
1383
|
};
|
|
1298
1384
|
|
|
1299
1385
|
// packages/react/src/runtime/utils.ts
|
|
1386
|
+
var import_client4 = require("@aomi-labs/client");
|
|
1300
1387
|
var import_clsx = require("clsx");
|
|
1301
1388
|
var import_tailwind_merge = require("tailwind-merge");
|
|
1302
1389
|
function cn(...inputs) {
|
|
@@ -1324,7 +1411,7 @@ function toInboundMessage(msg) {
|
|
|
1324
1411
|
if (msg.sender === "system") return null;
|
|
1325
1412
|
const content = [];
|
|
1326
1413
|
const role = msg.sender === "user" ? "user" : "assistant";
|
|
1327
|
-
if (msg.content) {
|
|
1414
|
+
if (msg.content && msg.content.trim().length > 0) {
|
|
1328
1415
|
content.push({ type: "text", text: msg.content });
|
|
1329
1416
|
}
|
|
1330
1417
|
const [topic, toolContent] = (_a = parseToolPayload(msg)) != null ? _a : [];
|
|
@@ -1343,9 +1430,12 @@ function toInboundMessage(msg) {
|
|
|
1343
1430
|
})()
|
|
1344
1431
|
});
|
|
1345
1432
|
}
|
|
1433
|
+
if (content.length === 0 && role === "assistant" && !msg.is_streaming) {
|
|
1434
|
+
return null;
|
|
1435
|
+
}
|
|
1346
1436
|
const threadMessage = __spreadValues({
|
|
1347
1437
|
role,
|
|
1348
|
-
content
|
|
1438
|
+
content
|
|
1349
1439
|
}, msg.timestamp && { createdAt: new Date(msg.timestamp) });
|
|
1350
1440
|
return threadMessage;
|
|
1351
1441
|
}
|
|
@@ -1376,10 +1466,14 @@ var getNetworkName = (chainId) => {
|
|
|
1376
1466
|
return "optimism";
|
|
1377
1467
|
case 11155111:
|
|
1378
1468
|
return "sepolia";
|
|
1469
|
+
case 143:
|
|
1470
|
+
return "monad";
|
|
1471
|
+
case 10143:
|
|
1472
|
+
return "monad-testnet";
|
|
1379
1473
|
case 1337:
|
|
1380
1474
|
case 31337:
|
|
1381
1475
|
return "testnet";
|
|
1382
|
-
case
|
|
1476
|
+
case 59141:
|
|
1383
1477
|
return "linea-sepolia";
|
|
1384
1478
|
case 59144:
|
|
1385
1479
|
return "linea";
|
|
@@ -1388,18 +1482,37 @@ var getNetworkName = (chainId) => {
|
|
|
1388
1482
|
}
|
|
1389
1483
|
};
|
|
1390
1484
|
var formatAddress = (addr) => addr ? `${addr.slice(0, 6)}...${addr.slice(-4)}` : "Connect Wallet";
|
|
1391
|
-
var SUPPORTED_CHAINS = [
|
|
1392
|
-
{ id: 1, name: "Ethereum", ticker: "ETH" },
|
|
1393
|
-
{ id: 137, name: "Polygon", ticker: "MATIC" },
|
|
1394
|
-
{ id: 42161, name: "Arbitrum", ticker: "ARB" },
|
|
1395
|
-
{ id: 8453, name: "Base", ticker: "BASE" },
|
|
1396
|
-
{ id: 10, name: "Optimism", ticker: "OP" },
|
|
1397
|
-
{ id: 11155111, name: "Sepolia", ticker: "SEP" }
|
|
1398
|
-
];
|
|
1485
|
+
var SUPPORTED_CHAINS = [...import_client4.SUPPORTED_CHAINS];
|
|
1399
1486
|
var getChainInfo = (chainId) => chainId === void 0 ? void 0 : SUPPORTED_CHAINS.find((c) => c.id === chainId);
|
|
1400
1487
|
|
|
1401
1488
|
// packages/react/src/runtime/orchestrator.ts
|
|
1402
1489
|
var toErrorMessage = (error) => error instanceof Error ? error.message : "Message failed to send";
|
|
1490
|
+
var getHttpStatus = (error) => {
|
|
1491
|
+
const status = error == null ? void 0 : error.status;
|
|
1492
|
+
if (typeof status === "number") return status;
|
|
1493
|
+
const message = toErrorMessage(error);
|
|
1494
|
+
const match = /\bHTTP\s+(\d{3})\b/i.exec(message);
|
|
1495
|
+
return match ? Number(match[1]) : void 0;
|
|
1496
|
+
};
|
|
1497
|
+
var isPaymentRequiredError = (error) => getHttpStatus(error) === 402;
|
|
1498
|
+
var PAYMENT_REQUIRED_MESSAGE = "You're out of funds, please set up a payment method.";
|
|
1499
|
+
var buildPaymentRequiredMessage = () => ({
|
|
1500
|
+
id: `aomi-payment-required-${Date.now()}`,
|
|
1501
|
+
role: "assistant",
|
|
1502
|
+
content: [
|
|
1503
|
+
{
|
|
1504
|
+
type: "text",
|
|
1505
|
+
text: PAYMENT_REQUIRED_MESSAGE
|
|
1506
|
+
}
|
|
1507
|
+
],
|
|
1508
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
1509
|
+
metadata: {
|
|
1510
|
+
custom: {
|
|
1511
|
+
aomiNoticeKind: "payment_required",
|
|
1512
|
+
aomiNoticeTitle: "Credits needed"
|
|
1513
|
+
}
|
|
1514
|
+
}
|
|
1515
|
+
});
|
|
1403
1516
|
var getOptimisticStatus = (message) => {
|
|
1404
1517
|
var _a, _b;
|
|
1405
1518
|
const status = (_b = (_a = message.metadata) == null ? void 0 : _a.custom) == null ? void 0 : _b.aomiSendStatus;
|
|
@@ -1437,6 +1550,22 @@ var updateOptimisticMessage = (threadContext, threadId, messageId, status, error
|
|
|
1437
1550
|
threadContext.setThreadMessages(threadId, nextMessages);
|
|
1438
1551
|
}
|
|
1439
1552
|
};
|
|
1553
|
+
var appendPaymentRequiredMessage = (threadContext, threadId) => {
|
|
1554
|
+
var _a, _b;
|
|
1555
|
+
const messages = threadContext.getThreadMessages(threadId);
|
|
1556
|
+
let hasPaymentNotice = false;
|
|
1557
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
1558
|
+
const message = messages[i];
|
|
1559
|
+
if (message.role !== "assistant") continue;
|
|
1560
|
+
hasPaymentNotice = ((_b = (_a = message.metadata) == null ? void 0 : _a.custom) == null ? void 0 : _b.aomiNoticeKind) === "payment_required";
|
|
1561
|
+
break;
|
|
1562
|
+
}
|
|
1563
|
+
if (hasPaymentNotice) return;
|
|
1564
|
+
threadContext.setThreadMessages(threadId, [
|
|
1565
|
+
...messages,
|
|
1566
|
+
buildPaymentRequiredMessage()
|
|
1567
|
+
]);
|
|
1568
|
+
};
|
|
1440
1569
|
function useRuntimeOrchestrator(aomiClient, options) {
|
|
1441
1570
|
const threadContext = useThreadContext();
|
|
1442
1571
|
const threadContextRef = (0, import_react6.useRef)(threadContext);
|
|
@@ -1522,7 +1651,7 @@ function useRuntimeOrchestrator(aomiClient, options) {
|
|
|
1522
1651
|
publicKey: nextPublicKey,
|
|
1523
1652
|
apiKey: nextApiKey,
|
|
1524
1653
|
clientId: nextClientId,
|
|
1525
|
-
clientType:
|
|
1654
|
+
clientType: import_client5.CLIENT_TYPE_WEB_UI,
|
|
1526
1655
|
syncPendingTxRequestsFromUserState: false,
|
|
1527
1656
|
userState: nextUserState
|
|
1528
1657
|
});
|
|
@@ -1644,7 +1773,7 @@ function useRuntimeOrchestrator(aomiClient, options) {
|
|
|
1644
1773
|
);
|
|
1645
1774
|
const sendMessage = (0, import_react6.useCallback)(
|
|
1646
1775
|
async (text, threadId) => {
|
|
1647
|
-
var _a, _b, _c, _d;
|
|
1776
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
1648
1777
|
const existingMessages = threadContextRef.current.getThreadMessages(threadId);
|
|
1649
1778
|
const optimisticMessageId = String(existingMessages.length);
|
|
1650
1779
|
const userMessage = {
|
|
@@ -1673,6 +1802,7 @@ function useRuntimeOrchestrator(aomiClient, options) {
|
|
|
1673
1802
|
await ((_b = (_a = optionsRef.current).prepareThreadForSend) == null ? void 0 : _b.call(_a, threadId));
|
|
1674
1803
|
const session = getSession(threadId);
|
|
1675
1804
|
await session.sendAsync(text);
|
|
1805
|
+
(_d = (_c = optionsRef.current).onSendSuccess) == null ? void 0 : _d.call(_c, threadId);
|
|
1676
1806
|
if (threadContextRef.current.currentThreadId === threadId) {
|
|
1677
1807
|
setIsRunning(session.getIsProcessing());
|
|
1678
1808
|
}
|
|
@@ -1682,8 +1812,8 @@ function useRuntimeOrchestrator(aomiClient, options) {
|
|
|
1682
1812
|
optimisticMessageId,
|
|
1683
1813
|
"sent"
|
|
1684
1814
|
);
|
|
1685
|
-
(
|
|
1686
|
-
|
|
1815
|
+
(_f = (_e = optionsRef.current).onPendingRequestsChange) == null ? void 0 : _f.call(
|
|
1816
|
+
_e,
|
|
1687
1817
|
session.getPendingRequests()
|
|
1688
1818
|
);
|
|
1689
1819
|
} catch (error) {
|
|
@@ -1697,6 +1827,10 @@ function useRuntimeOrchestrator(aomiClient, options) {
|
|
|
1697
1827
|
"failed",
|
|
1698
1828
|
error
|
|
1699
1829
|
);
|
|
1830
|
+
if (isPaymentRequiredError(error)) {
|
|
1831
|
+
appendPaymentRequiredMessage(threadContextRef.current, threadId);
|
|
1832
|
+
}
|
|
1833
|
+
await ((_h = (_g = optionsRef.current).onSendError) == null ? void 0 : _h.call(_g, threadId, error));
|
|
1700
1834
|
throw error;
|
|
1701
1835
|
}
|
|
1702
1836
|
},
|
|
@@ -1994,7 +2128,7 @@ function useWalletHandler({
|
|
|
1994
2128
|
|
|
1995
2129
|
// packages/react/src/runtime/user-state-provider.tsx
|
|
1996
2130
|
var import_react9 = require("react");
|
|
1997
|
-
var
|
|
2131
|
+
var import_client6 = require("@aomi-labs/client");
|
|
1998
2132
|
var import_jsx_runtime6 = require("react/jsx-runtime");
|
|
1999
2133
|
var THREAD_PREFETCH_LIMIT = 5;
|
|
2000
2134
|
var PREFETCH_IDLE_TIMEOUT_MS = 1500;
|
|
@@ -2021,12 +2155,19 @@ function useWalletStateSync(context, sessions, remoteThreads) {
|
|
|
2021
2155
|
const { remoteThreadIdsRef } = remoteThreads;
|
|
2022
2156
|
const walletSnapshot = (0, import_react9.useCallback)(
|
|
2023
2157
|
(nextUser) => {
|
|
2024
|
-
var _a;
|
|
2158
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
2025
2159
|
return {
|
|
2026
|
-
address:
|
|
2027
|
-
chain_id:
|
|
2028
|
-
is_connected: (_a =
|
|
2029
|
-
ens_name: typeof nextUser.ens_name === "string" ? nextUser.ens_name : void 0
|
|
2160
|
+
address: import_client6.UserState.address(nextUser),
|
|
2161
|
+
chain_id: import_client6.UserState.chainId(nextUser),
|
|
2162
|
+
is_connected: (_a = import_client6.UserState.isConnected(nextUser)) != null ? _a : false,
|
|
2163
|
+
ens_name: typeof nextUser.ens_name === "string" ? nextUser.ens_name : void 0,
|
|
2164
|
+
wallet_provider: (_b = import_client6.UserState.walletProvider(nextUser)) != null ? _b : void 0,
|
|
2165
|
+
auth_method: (_c = import_client6.UserState.authMethod(nextUser)) != null ? _c : void 0,
|
|
2166
|
+
sponsored: (_d = import_client6.UserState.sponsored(nextUser)) != null ? _d : void 0,
|
|
2167
|
+
sponsor_provider: (_e = import_client6.UserState.sponsorProvider(nextUser)) != null ? _e : void 0,
|
|
2168
|
+
sponsor_account: (_f = import_client6.UserState.sponsorAccount(nextUser)) != null ? _f : void 0,
|
|
2169
|
+
smart_account_4337: (_g = import_client6.UserState.SmartAccount4337(nextUser)) != null ? _g : void 0,
|
|
2170
|
+
delegation_7702: (_h = import_client6.UserState.Delegation7702(nextUser)) != null ? _h : void 0
|
|
2030
2171
|
};
|
|
2031
2172
|
},
|
|
2032
2173
|
[getUserState]
|
|
@@ -2040,7 +2181,7 @@ function useWalletStateSync(context, sessions, remoteThreads) {
|
|
|
2040
2181
|
const prevWalletState = lastWalletStateRef.current;
|
|
2041
2182
|
const previousAddress = (_a = prevWalletState.address) == null ? void 0 : _a.toLowerCase();
|
|
2042
2183
|
const nextAddress = (_b = nextWalletState.address) == null ? void 0 : _b.toLowerCase();
|
|
2043
|
-
if (prevWalletState.address === nextWalletState.address && prevWalletState.chain_id === nextWalletState.chain_id && prevWalletState.is_connected === nextWalletState.is_connected && prevWalletState.ens_name === nextWalletState.ens_name) {
|
|
2184
|
+
if (prevWalletState.address === nextWalletState.address && prevWalletState.chain_id === nextWalletState.chain_id && prevWalletState.is_connected === nextWalletState.is_connected && prevWalletState.ens_name === nextWalletState.ens_name && prevWalletState.wallet_provider === nextWalletState.wallet_provider && prevWalletState.auth_method === nextWalletState.auth_method && prevWalletState.sponsored === nextWalletState.sponsored && prevWalletState.sponsor_provider === nextWalletState.sponsor_provider && prevWalletState.sponsor_account === nextWalletState.sponsor_account && prevWalletState.smart_account_4337 === nextWalletState.smart_account_4337 && prevWalletState.delegation_7702 === nextWalletState.delegation_7702) {
|
|
2044
2185
|
return;
|
|
2045
2186
|
}
|
|
2046
2187
|
lastWalletStateRef.current = nextWalletState;
|
|
@@ -2076,7 +2217,7 @@ function useUserStateRequestResponder(context, sessions) {
|
|
|
2076
2217
|
var _a, _b;
|
|
2077
2218
|
const sessionId = threadContextRef.current.currentThreadId;
|
|
2078
2219
|
const session = getSession(sessionId);
|
|
2079
|
-
const payload = (_b = (_a =
|
|
2220
|
+
const payload = (_b = (_a = import_client6.UserState.reconcile(session.getUserState(), getUserState())) != null ? _a : session.getUserState()) != null ? _b : getUserState();
|
|
2080
2221
|
eventContext.sendOutboundSystem({
|
|
2081
2222
|
type: "user_state_response",
|
|
2082
2223
|
sessionId,
|
|
@@ -2108,6 +2249,7 @@ function useRemoteThreadListSync(context, sessions, remoteThreads) {
|
|
|
2108
2249
|
warmedThreadIdsRef,
|
|
2109
2250
|
warmThread
|
|
2110
2251
|
} = remoteThreads;
|
|
2252
|
+
const connectedAddress = import_client6.UserState.isConnected(user) ? import_client6.UserState.address(user) : void 0;
|
|
2111
2253
|
const scheduleThreadPrefetch = (0, import_react9.useCallback)(
|
|
2112
2254
|
(threadIds) => {
|
|
2113
2255
|
var _a;
|
|
@@ -2146,7 +2288,7 @@ function useRemoteThreadListSync(context, sessions, remoteThreads) {
|
|
|
2146
2288
|
);
|
|
2147
2289
|
(0, import_react9.useEffect)(() => {
|
|
2148
2290
|
var _a, _b;
|
|
2149
|
-
const userAddress =
|
|
2291
|
+
const userAddress = connectedAddress;
|
|
2150
2292
|
const normalizedUserAddress = userAddress == null ? void 0 : userAddress.toLowerCase();
|
|
2151
2293
|
const previousAddress = lastConnectedAddressRef.current;
|
|
2152
2294
|
const walletChanged = previousAddress !== void 0 && normalizedUserAddress !== void 0 && previousAddress !== normalizedUserAddress;
|
|
@@ -2187,6 +2329,7 @@ function useRemoteThreadListSync(context, sessions, remoteThreads) {
|
|
|
2187
2329
|
getControlState().clientId,
|
|
2188
2330
|
resetThreadId != null ? resetThreadId : currentContext.currentThreadId
|
|
2189
2331
|
);
|
|
2332
|
+
await aomiClientRef.current.ensureAccount(controlSessionId, userAddress);
|
|
2190
2333
|
const threadList = await aomiClientRef.current.listThreads(
|
|
2191
2334
|
controlSessionId,
|
|
2192
2335
|
userAddress
|
|
@@ -2276,7 +2419,7 @@ function useRemoteThreadListSync(context, sessions, remoteThreads) {
|
|
|
2276
2419
|
sessionManager,
|
|
2277
2420
|
setIsThreadLoading,
|
|
2278
2421
|
threadContextRef,
|
|
2279
|
-
|
|
2422
|
+
connectedAddress,
|
|
2280
2423
|
warmPromisesRef,
|
|
2281
2424
|
warmedThreadIdsRef,
|
|
2282
2425
|
warmThread
|
|
@@ -2322,6 +2465,7 @@ function RuntimeUserStateProvider({
|
|
|
2322
2465
|
children,
|
|
2323
2466
|
sessionManager,
|
|
2324
2467
|
getUserState,
|
|
2468
|
+
setUser,
|
|
2325
2469
|
onUserStateChange
|
|
2326
2470
|
}) {
|
|
2327
2471
|
const lastSerializedStateRef = (0, import_react9.useRef)("");
|
|
@@ -2333,20 +2477,40 @@ function RuntimeUserStateProvider({
|
|
|
2333
2477
|
}
|
|
2334
2478
|
lastSerializedStateRef.current = serialized;
|
|
2335
2479
|
sessionManager.forEach((session) => {
|
|
2336
|
-
session.resolveUserState(next);
|
|
2480
|
+
session.resolveUserState(next, { skipEmit: true });
|
|
2337
2481
|
});
|
|
2338
2482
|
};
|
|
2483
|
+
const sessionListeners = [];
|
|
2484
|
+
sessionManager.forEach((session) => {
|
|
2485
|
+
const handler = (next) => {
|
|
2486
|
+
setUser(next);
|
|
2487
|
+
};
|
|
2488
|
+
session.on("user_state_updated", handler);
|
|
2489
|
+
sessionListeners.push(
|
|
2490
|
+
() => session.off("user_state_updated", handler)
|
|
2491
|
+
);
|
|
2492
|
+
});
|
|
2339
2493
|
applyToSessions(getUserState());
|
|
2340
2494
|
const unsubscribe = onUserStateChange((next) => {
|
|
2341
2495
|
applyToSessions(next);
|
|
2342
2496
|
});
|
|
2343
|
-
return
|
|
2344
|
-
|
|
2497
|
+
return () => {
|
|
2498
|
+
unsubscribe();
|
|
2499
|
+
sessionListeners.forEach((off) => off());
|
|
2500
|
+
};
|
|
2501
|
+
}, [getUserState, onUserStateChange, sessionManager, setUser]);
|
|
2345
2502
|
return /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(import_jsx_runtime6.Fragment, { children });
|
|
2346
2503
|
}
|
|
2347
2504
|
|
|
2348
2505
|
// packages/react/src/runtime/core.tsx
|
|
2349
2506
|
var import_jsx_runtime7 = require("react/jsx-runtime");
|
|
2507
|
+
var getHttpStatus2 = (error) => {
|
|
2508
|
+
const status = error == null ? void 0 : error.status;
|
|
2509
|
+
if (typeof status === "number") return status;
|
|
2510
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
2511
|
+
const match = /\bHTTP\s+(\d{3})\b/i.exec(message);
|
|
2512
|
+
return match ? Number(match[1]) : void 0;
|
|
2513
|
+
};
|
|
2350
2514
|
function AomiRuntimeCore({
|
|
2351
2515
|
children,
|
|
2352
2516
|
aomiClient
|
|
@@ -2381,7 +2545,7 @@ function AomiRuntimeCore({
|
|
|
2381
2545
|
closeAllSessions,
|
|
2382
2546
|
aomiClientRef
|
|
2383
2547
|
} = useRuntimeOrchestrator(aomiClient, {
|
|
2384
|
-
getPublicKey: () =>
|
|
2548
|
+
getPublicKey: () => import_client7.UserState.isConnected(getUserState()) ? import_client7.UserState.address(getUserState()) : void 0,
|
|
2385
2549
|
getUserState,
|
|
2386
2550
|
getApp: getCurrentThreadApp,
|
|
2387
2551
|
getApiKey: () => getControlState().apiKey,
|
|
@@ -2390,8 +2554,44 @@ function AomiRuntimeCore({
|
|
|
2390
2554
|
return (_a = getControlState().clientId) != null ? _a : void 0;
|
|
2391
2555
|
},
|
|
2392
2556
|
prepareThreadForSend: async (threadId) => {
|
|
2393
|
-
await ensureBackendThread(threadId);
|
|
2394
2557
|
await syncCurrentThreadControl();
|
|
2558
|
+
const wasCreated = await ensureBackendThread(threadId);
|
|
2559
|
+
if (wasCreated) {
|
|
2560
|
+
threadsMaterializedForSendRef.current.add(threadId);
|
|
2561
|
+
}
|
|
2562
|
+
},
|
|
2563
|
+
onSendSuccess: (threadId) => {
|
|
2564
|
+
const wasRemote = remoteThreadIdsRef.current.has(threadId);
|
|
2565
|
+
remoteThreadIdsRef.current.add(threadId);
|
|
2566
|
+
warmedThreadIdsRef.current.add(threadId);
|
|
2567
|
+
threadsMaterializedForSendRef.current.delete(threadId);
|
|
2568
|
+
if (!wasRemote && threadContextRef.current.currentThreadId === threadId) {
|
|
2569
|
+
void syncCurrentThreadControl().catch((error) => {
|
|
2570
|
+
console.error("Failed to sync thread controls:", error);
|
|
2571
|
+
});
|
|
2572
|
+
}
|
|
2573
|
+
},
|
|
2574
|
+
onSendError: async (threadId, error) => {
|
|
2575
|
+
const wasMaterializedForSend = threadsMaterializedForSendRef.current.has(threadId);
|
|
2576
|
+
threadsMaterializedForSendRef.current.delete(threadId);
|
|
2577
|
+
const httpStatus = getHttpStatus2(error);
|
|
2578
|
+
if (httpStatus === 402) {
|
|
2579
|
+
notificationContext.showNotification({
|
|
2580
|
+
type: "error",
|
|
2581
|
+
kind: "payment_required",
|
|
2582
|
+
title: "You're out of funds"
|
|
2583
|
+
});
|
|
2584
|
+
}
|
|
2585
|
+
if (httpStatus !== 402 || !wasMaterializedForSend) {
|
|
2586
|
+
return;
|
|
2587
|
+
}
|
|
2588
|
+
try {
|
|
2589
|
+
await aomiClientRef.current.deleteThread(threadId);
|
|
2590
|
+
remoteThreadIdsRef.current.delete(threadId);
|
|
2591
|
+
warmedThreadIdsRef.current.delete(threadId);
|
|
2592
|
+
} catch (deleteError) {
|
|
2593
|
+
console.error("Failed to delete quota-blocked thread:", deleteError);
|
|
2594
|
+
}
|
|
2395
2595
|
},
|
|
2396
2596
|
onPendingRequestsChange: walletHandler.setRequests,
|
|
2397
2597
|
onEvent: (event) => eventContext.dispatch(event)
|
|
@@ -2402,7 +2602,20 @@ function AomiRuntimeCore({
|
|
|
2402
2602
|
const remoteThreadIdsRef = (0, import_react10.useRef)(/* @__PURE__ */ new Set());
|
|
2403
2603
|
const warmedThreadIdsRef = (0, import_react10.useRef)(/* @__PURE__ */ new Set());
|
|
2404
2604
|
const warmPromisesRef = (0, import_react10.useRef)(/* @__PURE__ */ new Map());
|
|
2605
|
+
const threadsMaterializedForSendRef = (0, import_react10.useRef)(/* @__PURE__ */ new Set());
|
|
2606
|
+
const ensuredAccountPublicKeysRef = (0, import_react10.useRef)(/* @__PURE__ */ new Set());
|
|
2405
2607
|
const [isThreadLoading, setIsThreadLoading] = (0, import_react10.useState)(false);
|
|
2608
|
+
const ensureAccountForPublicKey = (0, import_react10.useCallback)(
|
|
2609
|
+
async (sessionId, publicKey) => {
|
|
2610
|
+
const normalizedPublicKey = publicKey.toLowerCase();
|
|
2611
|
+
if (ensuredAccountPublicKeysRef.current.has(normalizedPublicKey)) {
|
|
2612
|
+
return;
|
|
2613
|
+
}
|
|
2614
|
+
await aomiClientRef.current.ensureAccount(sessionId, publicKey);
|
|
2615
|
+
ensuredAccountPublicKeysRef.current.add(normalizedPublicKey);
|
|
2616
|
+
},
|
|
2617
|
+
[aomiClientRef]
|
|
2618
|
+
);
|
|
2406
2619
|
const warmThread = (0, import_react10.useCallback)(
|
|
2407
2620
|
async (threadId) => {
|
|
2408
2621
|
if (!remoteThreadIdsRef.current.has(threadId) || warmedThreadIdsRef.current.has(threadId)) {
|
|
@@ -2414,9 +2627,15 @@ function AomiRuntimeCore({
|
|
|
2414
2627
|
}
|
|
2415
2628
|
const warmPromise = (async () => {
|
|
2416
2629
|
const userState = getUserState();
|
|
2630
|
+
if (import_client7.UserState.isConnected(userState)) {
|
|
2631
|
+
const publicKey = import_client7.UserState.address(userState);
|
|
2632
|
+
if (publicKey) {
|
|
2633
|
+
await ensureAccountForPublicKey(threadId, publicKey);
|
|
2634
|
+
}
|
|
2635
|
+
}
|
|
2417
2636
|
await aomiClientRef.current.createThread(
|
|
2418
2637
|
threadId,
|
|
2419
|
-
|
|
2638
|
+
import_client7.UserState.isConnected(userState) ? import_client7.UserState.address(userState) : void 0
|
|
2420
2639
|
);
|
|
2421
2640
|
warmedThreadIdsRef.current.add(threadId);
|
|
2422
2641
|
})();
|
|
@@ -2427,20 +2646,27 @@ function AomiRuntimeCore({
|
|
|
2427
2646
|
warmPromisesRef.current.delete(threadId);
|
|
2428
2647
|
}
|
|
2429
2648
|
},
|
|
2430
|
-
[aomiClientRef, getUserState]
|
|
2649
|
+
[aomiClientRef, ensureAccountForPublicKey, getUserState]
|
|
2431
2650
|
);
|
|
2432
2651
|
const ensureBackendThread = (0, import_react10.useCallback)(
|
|
2433
2652
|
async (threadId) => {
|
|
2434
|
-
if (remoteThreadIdsRef.current.has(threadId)) return;
|
|
2653
|
+
if (remoteThreadIdsRef.current.has(threadId)) return false;
|
|
2435
2654
|
const userState = getUserState();
|
|
2655
|
+
if (import_client7.UserState.isConnected(userState)) {
|
|
2656
|
+
const publicKey = import_client7.UserState.address(userState);
|
|
2657
|
+
if (publicKey) {
|
|
2658
|
+
await ensureAccountForPublicKey(threadId, publicKey);
|
|
2659
|
+
}
|
|
2660
|
+
}
|
|
2436
2661
|
await aomiClientRef.current.createThread(
|
|
2437
2662
|
threadId,
|
|
2438
|
-
|
|
2663
|
+
import_client7.UserState.isConnected(userState) ? import_client7.UserState.address(userState) : void 0
|
|
2439
2664
|
);
|
|
2440
2665
|
remoteThreadIdsRef.current.add(threadId);
|
|
2441
2666
|
warmedThreadIdsRef.current.add(threadId);
|
|
2667
|
+
return true;
|
|
2442
2668
|
},
|
|
2443
|
-
[aomiClientRef, getUserState]
|
|
2669
|
+
[aomiClientRef, ensureAccountForPublicKey, getUserState]
|
|
2444
2670
|
);
|
|
2445
2671
|
const getRuntimeSession = (0, import_react10.useCallback)(
|
|
2446
2672
|
(threadId) => {
|
|
@@ -2538,6 +2764,9 @@ function AomiRuntimeCore({
|
|
|
2538
2764
|
const showToolNotification = (eventType) => (event) => {
|
|
2539
2765
|
const payload = event.payload;
|
|
2540
2766
|
const toolName = typeof (payload == null ? void 0 : payload.tool_name) === "string" ? payload.tool_name : void 0;
|
|
2767
|
+
if (eventType === "tool_complete" && toolName === "commit_txs") {
|
|
2768
|
+
return;
|
|
2769
|
+
}
|
|
2541
2770
|
const title = toolName ? `${eventType === "tool_update" ? "Tool update" : "Tool complete"}: ${toolName}` : eventType === "tool_update" ? "Tool update" : "Tool complete";
|
|
2542
2771
|
const message = typeof (payload == null ? void 0 : payload.message) === "string" ? payload.message : typeof (payload == null ? void 0 : payload.result) === "string" ? payload.result : void 0;
|
|
2543
2772
|
notificationContext.showNotification({
|
|
@@ -2574,7 +2803,11 @@ function AomiRuntimeCore({
|
|
|
2574
2803
|
(part) => part.type === "text"
|
|
2575
2804
|
).map((part) => part.text).join("\n");
|
|
2576
2805
|
if (text) {
|
|
2577
|
-
|
|
2806
|
+
try {
|
|
2807
|
+
await orchestratorSendMessage(text, threadContext.currentThreadId);
|
|
2808
|
+
} catch (error) {
|
|
2809
|
+
console.error("Failed to send message:", error);
|
|
2810
|
+
}
|
|
2578
2811
|
}
|
|
2579
2812
|
},
|
|
2580
2813
|
onCancel: async () => {
|
|
@@ -2720,6 +2953,7 @@ function AomiRuntimeCore({
|
|
|
2720
2953
|
{
|
|
2721
2954
|
sessionManager,
|
|
2722
2955
|
getUserState: userContext.getUserState,
|
|
2956
|
+
setUser: userContext.setUser,
|
|
2723
2957
|
onUserStateChange: userContext.onUserStateChange,
|
|
2724
2958
|
children: /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(import_react11.AssistantRuntimeProvider, { runtime, children })
|
|
2725
2959
|
}
|
|
@@ -2734,10 +2968,10 @@ function AomiRuntimeProvider({
|
|
|
2734
2968
|
clientOptions
|
|
2735
2969
|
}) {
|
|
2736
2970
|
const aomiClient = (0, import_react12.useMemo)(
|
|
2737
|
-
() => new
|
|
2971
|
+
() => new import_client8.AomiClient(__spreadValues({ baseUrl: backendUrl }, clientOptions)),
|
|
2738
2972
|
[backendUrl, clientOptions]
|
|
2739
2973
|
);
|
|
2740
|
-
return /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(ThreadContextProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(NotificationContextProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(
|
|
2974
|
+
return /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(ThreadContextProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(NotificationContextProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(ExtUserProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(AomiRuntimeInner, { aomiClient, children }) }) }) });
|
|
2741
2975
|
}
|
|
2742
2976
|
function AomiRuntimeInner({
|
|
2743
2977
|
children,
|
|
@@ -2751,7 +2985,7 @@ function AomiRuntimeInner({
|
|
|
2751
2985
|
{
|
|
2752
2986
|
aomiClient,
|
|
2753
2987
|
sessionId: threadContext.currentThreadId,
|
|
2754
|
-
publicKey:
|
|
2988
|
+
publicKey: import_client8.UserState.isConnected(user) ? (_a = import_client8.UserState.address(user)) != null ? _a : void 0 : void 0,
|
|
2755
2989
|
getThreadMetadata: threadContext.getThreadMetadata,
|
|
2756
2990
|
updateThreadMetadata: threadContext.updateThreadMetadata,
|
|
2757
2991
|
children: /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(
|
|
@@ -2814,12 +3048,12 @@ function useNotificationHandler({
|
|
|
2814
3048
|
ControlContextProvider,
|
|
2815
3049
|
DISABLED_PROVIDER_STATE,
|
|
2816
3050
|
EventContextProvider,
|
|
3051
|
+
ExtUserProvider,
|
|
2817
3052
|
MAX_AUTO_FEE_WEI,
|
|
2818
3053
|
NotificationContextProvider,
|
|
2819
3054
|
RuntimeUserStateProvider,
|
|
2820
3055
|
SUPPORTED_CHAINS,
|
|
2821
3056
|
ThreadContextProvider,
|
|
2822
|
-
UserContextProvider,
|
|
2823
3057
|
UserState,
|
|
2824
3058
|
aaModeFromExecutionKind,
|
|
2825
3059
|
appendFeeCallToPayload,
|