@aomi-labs/react 0.3.1 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -1,5 +1,5 @@
1
- import { AomiSystemEvent, AomiClient } from '@aomi-labs/client';
2
- export { AomiChatResponse, AomiClient, AomiClientOptions, AomiCreateThreadResponse, AomiInterruptResponse, AomiMessage, AomiSSEEvent, AomiStateResponse, AomiSystemEvent, AomiSystemResponse, AomiThread } from '@aomi-labs/client';
1
+ import { WalletTxPayload, WalletEip712Payload, AomiSystemEvent, AomiClient } from '@aomi-labs/client';
2
+ export { AomiChatResponse, AomiClient, AomiClientOptions, AomiCreateThreadResponse, AomiInterruptResponse, AomiMessage, AomiSSEEvent, AomiStateResponse, AomiSystemEvent, AomiSystemResponse, AomiThread, WalletEip712Payload, 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';
@@ -123,26 +123,6 @@ type NotificationContextProviderProps = {
123
123
  declare function NotificationContextProvider({ children, }: NotificationContextProviderProps): react_jsx_runtime.JSX.Element;
124
124
 
125
125
  type WalletRequestKind = "transaction" | "eip712_sign";
126
- type WalletTxPayload = {
127
- to: string;
128
- value?: string;
129
- data?: string;
130
- chainId?: number;
131
- };
132
- type WalletEip712Payload = {
133
- typed_data?: {
134
- domain?: {
135
- chainId?: number | string;
136
- };
137
- types?: Record<string, Array<{
138
- name: string;
139
- type: string;
140
- }>>;
141
- primaryType?: string;
142
- message?: Record<string, unknown>;
143
- };
144
- description?: string;
145
- };
146
126
  type WalletRequestStatus = "pending" | "processing";
147
127
  type WalletRequest = {
148
128
  id: string;
@@ -358,6 +338,8 @@ type ControlContextApi = {
358
338
  getAuthorizedApps: () => Promise<string[]>;
359
339
  /** Get current thread's control state */
360
340
  getCurrentThreadControl: () => ThreadControlState;
341
+ /** Get the current thread's effective app after auth fallback */
342
+ getCurrentThreadApp: () => string;
361
343
  /** Select a model for the current thread (updates metadata + calls backend) */
362
344
  onModelSelect: (model: string) => Promise<void>;
363
345
  /** Select an app for the current thread (updates metadata only) */
@@ -389,4 +371,4 @@ type ControlContextProviderProps = {
389
371
  };
390
372
  declare function ControlContextProvider({ children, aomiClient, sessionId, publicKey, getThreadMetadata, updateThreadMetadata, }: ControlContextProviderProps): react_jsx_runtime.JSX.Element;
391
373
 
392
- export { type AomiRuntimeApi, AomiRuntimeProvider, type AomiRuntimeProviderProps, type ChainInfo, type ControlContextApi, ControlContextProvider, type ControlContextProviderProps, type ControlState, type EventBuffer, 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 OutboundEvent, type SSEStatus, SUPPORTED_CHAINS, type NotificationData as ShowNotificationParams, type ThreadContext, ThreadContextProvider, type ThreadControlState, type ThreadMetadata, type UserConfig, UserContextProvider, type UserState, type WalletBuffer, type WalletEip712Payload, type WalletHandlerApi, type WalletHandlerConfig, type WalletRequest, type WalletRequestKind, type WalletRequestResult, type WalletRequestStatus, type WalletTxPayload, cn, formatAddress, getChainInfo, getNetworkName, initThreadControl, useAomiRuntime, useControl, useCurrentThreadMessages, useCurrentThreadMetadata, useEventContext, useNotification, useNotificationHandler, useThreadContext, useUser, useWalletHandler };
374
+ export { type AomiRuntimeApi, AomiRuntimeProvider, type AomiRuntimeProviderProps, type ChainInfo, type ControlContextApi, ControlContextProvider, type ControlContextProviderProps, type ControlState, type EventBuffer, 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 OutboundEvent, type SSEStatus, SUPPORTED_CHAINS, type NotificationData as ShowNotificationParams, type ThreadContext, ThreadContextProvider, type ThreadControlState, type ThreadMetadata, type UserConfig, UserContextProvider, type UserState, type WalletBuffer, type WalletHandlerApi, type WalletHandlerConfig, type WalletRequest, 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 { AomiSystemEvent, AomiClient } from '@aomi-labs/client';
2
- export { AomiChatResponse, AomiClient, AomiClientOptions, AomiCreateThreadResponse, AomiInterruptResponse, AomiMessage, AomiSSEEvent, AomiStateResponse, AomiSystemEvent, AomiSystemResponse, AomiThread } from '@aomi-labs/client';
1
+ import { WalletTxPayload, WalletEip712Payload, AomiSystemEvent, AomiClient } from '@aomi-labs/client';
2
+ export { AomiChatResponse, AomiClient, AomiClientOptions, AomiCreateThreadResponse, AomiInterruptResponse, AomiMessage, AomiSSEEvent, AomiStateResponse, AomiSystemEvent, AomiSystemResponse, AomiThread, WalletEip712Payload, 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';
@@ -123,26 +123,6 @@ type NotificationContextProviderProps = {
123
123
  declare function NotificationContextProvider({ children, }: NotificationContextProviderProps): react_jsx_runtime.JSX.Element;
124
124
 
125
125
  type WalletRequestKind = "transaction" | "eip712_sign";
126
- type WalletTxPayload = {
127
- to: string;
128
- value?: string;
129
- data?: string;
130
- chainId?: number;
131
- };
132
- type WalletEip712Payload = {
133
- typed_data?: {
134
- domain?: {
135
- chainId?: number | string;
136
- };
137
- types?: Record<string, Array<{
138
- name: string;
139
- type: string;
140
- }>>;
141
- primaryType?: string;
142
- message?: Record<string, unknown>;
143
- };
144
- description?: string;
145
- };
146
126
  type WalletRequestStatus = "pending" | "processing";
147
127
  type WalletRequest = {
148
128
  id: string;
@@ -358,6 +338,8 @@ type ControlContextApi = {
358
338
  getAuthorizedApps: () => Promise<string[]>;
359
339
  /** Get current thread's control state */
360
340
  getCurrentThreadControl: () => ThreadControlState;
341
+ /** Get the current thread's effective app after auth fallback */
342
+ getCurrentThreadApp: () => string;
361
343
  /** Select a model for the current thread (updates metadata + calls backend) */
362
344
  onModelSelect: (model: string) => Promise<void>;
363
345
  /** Select an app for the current thread (updates metadata only) */
@@ -389,4 +371,4 @@ type ControlContextProviderProps = {
389
371
  };
390
372
  declare function ControlContextProvider({ children, aomiClient, sessionId, publicKey, getThreadMetadata, updateThreadMetadata, }: ControlContextProviderProps): react_jsx_runtime.JSX.Element;
391
373
 
392
- export { type AomiRuntimeApi, AomiRuntimeProvider, type AomiRuntimeProviderProps, type ChainInfo, type ControlContextApi, ControlContextProvider, type ControlContextProviderProps, type ControlState, type EventBuffer, 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 OutboundEvent, type SSEStatus, SUPPORTED_CHAINS, type NotificationData as ShowNotificationParams, type ThreadContext, ThreadContextProvider, type ThreadControlState, type ThreadMetadata, type UserConfig, UserContextProvider, type UserState, type WalletBuffer, type WalletEip712Payload, type WalletHandlerApi, type WalletHandlerConfig, type WalletRequest, type WalletRequestKind, type WalletRequestResult, type WalletRequestStatus, type WalletTxPayload, cn, formatAddress, getChainInfo, getNetworkName, initThreadControl, useAomiRuntime, useControl, useCurrentThreadMessages, useCurrentThreadMetadata, useEventContext, useNotification, useNotificationHandler, useThreadContext, useUser, useWalletHandler };
374
+ export { type AomiRuntimeApi, AomiRuntimeProvider, type AomiRuntimeProviderProps, type ChainInfo, type ControlContextApi, ControlContextProvider, type ControlContextProviderProps, type ControlState, type EventBuffer, 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 OutboundEvent, type SSEStatus, SUPPORTED_CHAINS, type NotificationData as ShowNotificationParams, type ThreadContext, ThreadContextProvider, type ThreadControlState, type ThreadMetadata, type UserConfig, UserContextProvider, type UserState, type WalletBuffer, type WalletHandlerApi, type WalletHandlerConfig, type WalletRequest, 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
@@ -20,6 +20,7 @@ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
20
 
21
21
  // packages/react/src/index.ts
22
22
  import { AomiClient as AomiClient2 } from "@aomi-labs/client";
23
+ import { toViemSignTypedDataArgs } from "@aomi-labs/client";
23
24
 
24
25
  // packages/react/src/runtime/aomi-runtime.tsx
25
26
  import { useMemo as useMemo3 } from "react";
@@ -209,6 +210,16 @@ var ThreadStore = class {
209
210
  // packages/react/src/contexts/control-context.tsx
210
211
  import { jsx } from "react/jsx-runtime";
211
212
  var API_KEY_STORAGE_KEY = "aomi_api_key";
213
+ function getDefaultApp(apps) {
214
+ var _a;
215
+ return apps.includes("default") ? "default" : (_a = apps[0]) != null ? _a : null;
216
+ }
217
+ function resolveAuthorizedApp(app, authorizedApps, defaultApp) {
218
+ if (app && authorizedApps.includes(app)) {
219
+ return app;
220
+ }
221
+ return defaultApp;
222
+ }
212
223
  var ControlContext = createContext(null);
213
224
  function useControl() {
214
225
  const ctx = useContext(ControlContext);
@@ -271,13 +282,16 @@ function ControlContextProvider({
271
282
  }, [state.apiKey]);
272
283
  useEffect(() => {
273
284
  const fetchApps = async () => {
274
- var _a2, _b2;
285
+ var _a2;
275
286
  try {
276
287
  const apps = await aomiClientRef.current.getApps(
277
288
  sessionIdRef.current,
278
- { publicKey: publicKeyRef.current, apiKey: (_a2 = stateRef.current.apiKey) != null ? _a2 : void 0 }
289
+ {
290
+ publicKey: publicKeyRef.current,
291
+ apiKey: (_a2 = stateRef.current.apiKey) != null ? _a2 : void 0
292
+ }
279
293
  );
280
- const defaultApp = apps.includes("default") ? "default" : (_b2 = apps[0]) != null ? _b2 : null;
294
+ const defaultApp = getDefaultApp(apps);
281
295
  setStateInternal((prev) => __spreadProps(__spreadValues({}, prev), {
282
296
  authorizedApps: apps,
283
297
  defaultApp
@@ -291,7 +305,7 @@ function ControlContextProvider({
291
305
  }
292
306
  };
293
307
  void fetchApps();
294
- }, [state.apiKey]);
308
+ }, [state.apiKey, publicKey, sessionId]);
295
309
  useEffect(() => {
296
310
  const fetchModels = async () => {
297
311
  try {
@@ -337,13 +351,16 @@ function ControlContextProvider({
337
351
  }
338
352
  }, []);
339
353
  const getAuthorizedApps = useCallback(async () => {
340
- var _a2, _b2;
354
+ var _a2;
341
355
  try {
342
356
  const apps = await aomiClientRef.current.getApps(
343
357
  sessionIdRef.current,
344
- { publicKey: publicKeyRef.current, apiKey: (_a2 = stateRef.current.apiKey) != null ? _a2 : void 0 }
358
+ {
359
+ publicKey: publicKeyRef.current,
360
+ apiKey: (_a2 = stateRef.current.apiKey) != null ? _a2 : void 0
361
+ }
345
362
  );
346
- const defaultApp = apps.includes("default") ? "default" : (_b2 = apps[0]) != null ? _b2 : null;
363
+ const defaultApp = getDefaultApp(apps);
347
364
  setStateInternal((prev) => __spreadProps(__spreadValues({}, prev), {
348
365
  authorizedApps: apps,
349
366
  defaultApp
@@ -363,8 +380,17 @@ function ControlContextProvider({
363
380
  const metadata = getThreadMetadataRef.current(sessionIdRef.current);
364
381
  return (_a2 = metadata == null ? void 0 : metadata.control) != null ? _a2 : initThreadControl();
365
382
  }, []);
383
+ const getCurrentThreadApp = useCallback(() => {
384
+ var _a2, _b2, _c;
385
+ const currentControl = (_b2 = (_a2 = getThreadMetadataRef.current(sessionIdRef.current)) == null ? void 0 : _a2.control) != null ? _b2 : initThreadControl();
386
+ return (_c = resolveAuthorizedApp(
387
+ currentControl.app,
388
+ stateRef.current.authorizedApps,
389
+ stateRef.current.defaultApp
390
+ )) != null ? _c : "default";
391
+ }, []);
366
392
  const onModelSelect = useCallback(async (model) => {
367
- var _a2, _b2, _c, _d, _e;
393
+ var _a2, _b2, _c, _d;
368
394
  const threadId = sessionIdRef.current;
369
395
  const currentControl = (_b2 = (_a2 = getThreadMetadataRef.current(threadId)) == null ? void 0 : _a2.control) != null ? _b2 : initThreadControl();
370
396
  const isProcessing2 = currentControl.isProcessing;
@@ -377,7 +403,11 @@ function ControlContextProvider({
377
403
  console.warn("[control-context] Cannot switch model while processing");
378
404
  return;
379
405
  }
380
- const app = (_d = (_c = currentControl.app) != null ? _c : stateRef.current.defaultApp) != null ? _d : "default";
406
+ const app = (_c = resolveAuthorizedApp(
407
+ currentControl.app,
408
+ stateRef.current.authorizedApps,
409
+ stateRef.current.defaultApp
410
+ )) != null ? _c : "default";
381
411
  console.log("[control-context] onModelSelect updating metadata", {
382
412
  threadId,
383
413
  model,
@@ -401,7 +431,7 @@ function ControlContextProvider({
401
431
  const result = await aomiClientRef.current.setModel(
402
432
  threadId,
403
433
  model,
404
- { app, apiKey: (_e = stateRef.current.apiKey) != null ? _e : void 0 }
434
+ { app, apiKey: (_d = stateRef.current.apiKey) != null ? _d : void 0 }
405
435
  );
406
436
  console.log("[control-context] onModelSelect backend result", result);
407
437
  } catch (err) {
@@ -425,6 +455,10 @@ function ControlContextProvider({
425
455
  );
426
456
  return;
427
457
  }
458
+ if (stateRef.current.authorizedApps.length > 0 && !stateRef.current.authorizedApps.includes(app)) {
459
+ console.warn("[control-context] Cannot select unauthorized app", { app });
460
+ return;
461
+ }
428
462
  console.log("[control-context] onAppSelect updating metadata", {
429
463
  threadId,
430
464
  app,
@@ -481,6 +515,7 @@ function ControlContextProvider({
481
515
  getAvailableModels,
482
516
  getAuthorizedApps,
483
517
  getCurrentThreadControl,
518
+ getCurrentThreadApp,
484
519
  onModelSelect,
485
520
  onAppSelect,
486
521
  isProcessing,
@@ -1382,6 +1417,10 @@ function useAomiRuntime() {
1382
1417
 
1383
1418
  // packages/react/src/handlers/wallet-handler.ts
1384
1419
  import { useCallback as useCallback6, useEffect as useEffect3, useRef as useRef6, useState as useState6 } from "react";
1420
+ import {
1421
+ normalizeEip712Payload,
1422
+ normalizeTxPayload
1423
+ } from "@aomi-labs/client";
1385
1424
 
1386
1425
  // packages/react/src/state/wallet-buffer.ts
1387
1426
  function createWalletBuffer() {
@@ -1414,68 +1453,6 @@ function getAll(buffer) {
1414
1453
  }
1415
1454
 
1416
1455
  // packages/react/src/handlers/wallet-handler.ts
1417
- function asRecord(value) {
1418
- if (!value || typeof value !== "object" || Array.isArray(value)) return void 0;
1419
- return value;
1420
- }
1421
- function getToolArgs(payload) {
1422
- const root = asRecord(payload);
1423
- const nestedArgs = asRecord(root == null ? void 0 : root.args);
1424
- return nestedArgs != null ? nestedArgs : root != null ? root : {};
1425
- }
1426
- function parseChainId(value) {
1427
- if (typeof value === "number" && Number.isFinite(value)) return value;
1428
- if (typeof value !== "string") return void 0;
1429
- const trimmed = value.trim();
1430
- if (!trimmed) return void 0;
1431
- if (trimmed.startsWith("0x")) {
1432
- const parsedHex = Number.parseInt(trimmed.slice(2), 16);
1433
- return Number.isFinite(parsedHex) ? parsedHex : void 0;
1434
- }
1435
- const parsed = Number.parseInt(trimmed, 10);
1436
- return Number.isFinite(parsed) ? parsed : void 0;
1437
- }
1438
- function normalizeTxPayload(payload) {
1439
- var _a, _b, _c;
1440
- const root = asRecord(payload);
1441
- const args = getToolArgs(payload);
1442
- const ctx = asRecord(root == null ? void 0 : root.ctx);
1443
- const to = typeof args.to === "string" ? args.to : void 0;
1444
- if (!to) return null;
1445
- const valueRaw = args.value;
1446
- const value = typeof valueRaw === "string" ? valueRaw : typeof valueRaw === "number" && Number.isFinite(valueRaw) ? String(Math.trunc(valueRaw)) : void 0;
1447
- const data = typeof args.data === "string" ? args.data : void 0;
1448
- const chainId = (_c = (_b = (_a = parseChainId(args.chainId)) != null ? _a : parseChainId(args.chain_id)) != null ? _b : parseChainId(ctx == null ? void 0 : ctx.user_chain_id)) != null ? _c : parseChainId(ctx == null ? void 0 : ctx.userChainId);
1449
- return {
1450
- to,
1451
- value,
1452
- data,
1453
- chainId
1454
- };
1455
- }
1456
- function normalizeEip712Payload(payload) {
1457
- var _a;
1458
- const args = getToolArgs(payload);
1459
- const typedDataRaw = (_a = args.typed_data) != null ? _a : args.typedData;
1460
- let typedData;
1461
- if (typeof typedDataRaw === "string") {
1462
- try {
1463
- const parsed = JSON.parse(typedDataRaw);
1464
- if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
1465
- typedData = parsed;
1466
- }
1467
- } catch (e) {
1468
- typedData = void 0;
1469
- }
1470
- } else if (typedDataRaw && typeof typedDataRaw === "object" && !Array.isArray(typedDataRaw)) {
1471
- typedData = typedDataRaw;
1472
- }
1473
- const description = typeof args.description === "string" ? args.description : void 0;
1474
- return {
1475
- typed_data: typedData,
1476
- description
1477
- };
1478
- }
1479
1456
  function useWalletHandler({
1480
1457
  sessionId,
1481
1458
  onRequestComplete
@@ -1603,7 +1580,7 @@ function AomiRuntimeCore({
1603
1580
  const notificationContext = useNotification();
1604
1581
  const { dispatchInboundSystem: dispatchSystemEvents } = eventContext;
1605
1582
  const { user, onUserStateChange, getUserState } = useUser();
1606
- const { getControlState, getCurrentThreadControl } = useControl();
1583
+ const { getControlState, getCurrentThreadApp } = useControl();
1607
1584
  const {
1608
1585
  backendStateRef,
1609
1586
  polling,
@@ -1616,10 +1593,7 @@ function AomiRuntimeCore({
1616
1593
  onSyncEvents: dispatchSystemEvents,
1617
1594
  getPublicKey: () => getUserState().address,
1618
1595
  getUserState,
1619
- getApp: () => {
1620
- var _a, _b;
1621
- return (_b = (_a = getCurrentThreadControl().app) != null ? _a : getControlState().defaultApp) != null ? _b : "default";
1622
- },
1596
+ getApp: getCurrentThreadApp,
1623
1597
  getApiKey: () => getControlState().apiKey
1624
1598
  });
1625
1599
  useEffect4(() => {
@@ -1733,10 +1707,7 @@ function AomiRuntimeCore({
1733
1707
  polling,
1734
1708
  userAddress: user.address,
1735
1709
  setIsRunning,
1736
- getApp: () => {
1737
- var _a, _b;
1738
- return (_b = (_a = getCurrentThreadControl().app) != null ? _a : getControlState().defaultApp) != null ? _b : "default";
1739
- },
1710
+ getApp: getCurrentThreadApp,
1740
1711
  getApiKey: () => getControlState().apiKey,
1741
1712
  getUserState
1742
1713
  }),
@@ -1750,6 +1721,7 @@ function AomiRuntimeCore({
1750
1721
  threadContext.currentThreadId,
1751
1722
  threadContext.allThreadsMetadata,
1752
1723
  getControlState,
1724
+ getCurrentThreadApp,
1753
1725
  getUserState
1754
1726
  ]
1755
1727
  );
@@ -2040,6 +2012,7 @@ export {
2040
2012
  getChainInfo,
2041
2013
  getNetworkName,
2042
2014
  initThreadControl,
2015
+ toViemSignTypedDataArgs,
2043
2016
  useAomiRuntime,
2044
2017
  useControl,
2045
2018
  useCurrentThreadMessages,