@aomi-labs/react 0.3.3 → 0.3.4

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 CHANGED
@@ -276,6 +276,7 @@ function ControlContextProvider({
276
276
  var _a, _b;
277
277
  const [state, setStateInternal] = (0, import_react.useState)(() => ({
278
278
  apiKey: null,
279
+ clientId: null,
279
280
  availableModels: [],
280
281
  authorizedApps: [],
281
282
  defaultModel: null,
@@ -296,6 +297,11 @@ function ControlContextProvider({
296
297
  const callbacks = (0, import_react.useRef)(/* @__PURE__ */ new Set());
297
298
  const currentThreadMetadata = getThreadMetadata(sessionId);
298
299
  const isProcessing = (_b = (_a = currentThreadMetadata == null ? void 0 : currentThreadMetadata.control) == null ? void 0 : _a.isProcessing) != null ? _b : false;
300
+ (0, import_react.useEffect)(() => {
301
+ var _a2, _b2, _c;
302
+ const clientId = (_c = (_b2 = (_a2 = globalThis.crypto) == null ? void 0 : _a2.randomUUID) == null ? void 0 : _b2.call(_a2)) != null ? _c : `client-${Date.now()}`;
303
+ setStateInternal((prev) => __spreadProps(__spreadValues({}, prev), { clientId }));
304
+ }, []);
299
305
  (0, import_react.useEffect)(() => {
300
306
  var _a2, _b2;
301
307
  try {
@@ -369,6 +375,24 @@ function ControlContextProvider({
369
375
  return next;
370
376
  });
371
377
  }, []);
378
+ const ingestSecrets = (0, import_react.useCallback)(
379
+ async (secrets) => {
380
+ const clientId = stateRef.current.clientId;
381
+ if (!clientId) throw new Error("clientId not initialized");
382
+ const { handles } = await aomiClientRef.current.ingestSecrets(
383
+ clientId,
384
+ secrets
385
+ );
386
+ return handles;
387
+ },
388
+ []
389
+ );
390
+ const clearSecrets = (0, import_react.useCallback)(async () => {
391
+ var _a2, _b2;
392
+ const clientId = stateRef.current.clientId;
393
+ if (!clientId) return;
394
+ await ((_b2 = (_a2 = aomiClientRef.current).clearSecrets) == null ? void 0 : _b2.call(_a2, clientId));
395
+ }, []);
372
396
  const getAvailableModels = (0, import_react.useCallback)(async () => {
373
397
  try {
374
398
  const models = await aomiClientRef.current.getModels(
@@ -549,6 +573,8 @@ function ControlContextProvider({
549
573
  value: {
550
574
  state,
551
575
  setApiKey,
576
+ ingestSecrets,
577
+ clearSecrets,
552
578
  getAvailableModels,
553
579
  getAuthorizedApps,
554
580
  getCurrentThreadControl,
@@ -1070,7 +1096,7 @@ var MessageController = class {
1070
1096
  this.getThreadContextApi().setThreadMessages(threadId, threadMessages);
1071
1097
  }
1072
1098
  async outbound(message, threadId) {
1073
- var _a, _b, _c, _d, _e, _f, _g, _h;
1099
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
1074
1100
  const backendState = this.config.backendStateRef.current;
1075
1101
  const text = message.content.filter(
1076
1102
  (part) => part.type === "text"
@@ -1093,18 +1119,19 @@ var MessageController = class {
1093
1119
  const app = this.config.getApp();
1094
1120
  const publicKey = (_b = (_a = this.config).getPublicKey) == null ? void 0 : _b.call(_a);
1095
1121
  const apiKey = (_e = (_d = (_c = this.config).getApiKey) == null ? void 0 : _d.call(_c)) != null ? _e : void 0;
1096
- const userState = (_g = (_f = this.config).getUserState) == null ? void 0 : _g.call(_f);
1122
+ const clientId = (_g = (_f = this.config).getClientId) == null ? void 0 : _g.call(_f);
1123
+ const userState = (_i = (_h = this.config).getUserState) == null ? void 0 : _i.call(_h);
1097
1124
  try {
1098
1125
  this.markRunning(threadId, true);
1099
1126
  const response = await this.config.aomiClientRef.current.sendMessage(
1100
1127
  backendThreadId,
1101
1128
  text,
1102
- { app, publicKey, apiKey, userState }
1129
+ { app, publicKey, apiKey, userState, clientId }
1103
1130
  );
1104
1131
  if (response == null ? void 0 : response.messages) {
1105
1132
  this.inbound(threadId, response.messages);
1106
1133
  }
1107
- if (((_h = response == null ? void 0 : response.system_events) == null ? void 0 : _h.length) && this.config.onSyncEvents) {
1134
+ if (((_j = response == null ? void 0 : response.system_events) == null ? void 0 : _j.length) && this.config.onSyncEvents) {
1108
1135
  this.config.onSyncEvents(backendThreadId, response.system_events);
1109
1136
  }
1110
1137
  if (response == null ? void 0 : response.is_processing) {
@@ -1163,7 +1190,7 @@ var PollingController = class {
1163
1190
  const backendThreadId = resolveThreadId(backendState, threadId);
1164
1191
  setThreadRunning(backendState, threadId, true);
1165
1192
  const tick = async () => {
1166
- var _a2, _b2;
1193
+ var _a2, _b2, _c, _d;
1167
1194
  if (!this.intervals.has(threadId)) return;
1168
1195
  try {
1169
1196
  console.log(
@@ -1171,9 +1198,11 @@ var PollingController = class {
1171
1198
  threadId
1172
1199
  );
1173
1200
  const userState = (_b2 = (_a2 = this.config).getUserState) == null ? void 0 : _b2.call(_a2);
1201
+ const clientId = (_d = (_c = this.config).getClientId) == null ? void 0 : _d.call(_c);
1174
1202
  const state = await this.config.aomiClientRef.current.fetchState(
1175
1203
  backendThreadId,
1176
- userState
1204
+ userState,
1205
+ clientId
1177
1206
  );
1178
1207
  if (!this.intervals.has(threadId)) return;
1179
1208
  this.handleState(threadId, state);
@@ -1240,6 +1269,7 @@ function useRuntimeOrchestrator(aomiClient, options) {
1240
1269
  },
1241
1270
  onSyncEvents: options.onSyncEvents,
1242
1271
  getUserState: options.getUserState,
1272
+ getClientId: options.getClientId,
1243
1273
  onStart: (threadId) => {
1244
1274
  if (threadContextRef.current.currentThreadId === threadId) {
1245
1275
  setIsRunning(true);
@@ -1262,29 +1292,32 @@ function useRuntimeOrchestrator(aomiClient, options) {
1262
1292
  getPublicKey: options.getPublicKey,
1263
1293
  getApp: options.getApp,
1264
1294
  getApiKey: options.getApiKey,
1295
+ getClientId: options.getClientId,
1265
1296
  getUserState: options.getUserState,
1266
1297
  onSyncEvents: options.onSyncEvents
1267
1298
  });
1268
1299
  }
1269
1300
  const ensureInitialState = (0, import_react6.useCallback)(async (threadId) => {
1270
- var _a, _b, _c, _d;
1301
+ var _a, _b, _c, _d, _e;
1271
1302
  if (pendingFetches.current.has(threadId)) return;
1272
1303
  const backendThreadId = resolveThreadId(backendStateRef.current, threadId);
1273
1304
  pendingFetches.current.add(threadId);
1274
1305
  try {
1275
1306
  const userState = (_a = options.getUserState) == null ? void 0 : _a.call(options);
1307
+ const clientId = (_b = options.getClientId) == null ? void 0 : _b.call(options);
1276
1308
  const state = await aomiClientRef.current.fetchState(
1277
1309
  backendThreadId,
1278
- userState
1310
+ userState,
1311
+ clientId
1279
1312
  );
1280
- (_b = messageControllerRef.current) == null ? void 0 : _b.inbound(threadId, state.messages);
1281
- if (((_c = state.system_events) == null ? void 0 : _c.length) && options.onSyncEvents) {
1313
+ (_c = messageControllerRef.current) == null ? void 0 : _c.inbound(threadId, state.messages);
1314
+ if (((_d = state.system_events) == null ? void 0 : _d.length) && options.onSyncEvents) {
1282
1315
  options.onSyncEvents(backendThreadId, state.system_events);
1283
1316
  }
1284
1317
  if (threadContextRef.current.currentThreadId === threadId) {
1285
1318
  if (state.is_processing) {
1286
1319
  setIsRunning(true);
1287
- (_d = pollingRef.current) == null ? void 0 : _d.start(threadId);
1320
+ (_e = pollingRef.current) == null ? void 0 : _e.start(threadId);
1288
1321
  } else {
1289
1322
  setIsRunning(false);
1290
1323
  }
@@ -1617,7 +1650,7 @@ function AomiRuntimeCore({
1617
1650
  const notificationContext = useNotification();
1618
1651
  const { dispatchInboundSystem: dispatchSystemEvents } = eventContext;
1619
1652
  const { user, onUserStateChange, getUserState } = useUser();
1620
- const { getControlState, getCurrentThreadApp } = useControl();
1653
+ const { getControlState, getCurrentThreadApp, clearSecrets } = useControl();
1621
1654
  const {
1622
1655
  backendStateRef,
1623
1656
  polling,
@@ -1631,7 +1664,11 @@ function AomiRuntimeCore({
1631
1664
  getPublicKey: () => getUserState().address,
1632
1665
  getUserState,
1633
1666
  getApp: getCurrentThreadApp,
1634
- getApiKey: () => getControlState().apiKey
1667
+ getApiKey: () => getControlState().apiKey,
1668
+ getClientId: () => {
1669
+ var _a;
1670
+ return (_a = getControlState().clientId) != null ? _a : void 0;
1671
+ }
1635
1672
  });
1636
1673
  const walletSnapshot = (0, import_react9.useCallback)(
1637
1674
  (nextUser) => ({
@@ -1858,8 +1895,9 @@ function AomiRuntimeCore({
1858
1895
  (0, import_react9.useEffect)(() => {
1859
1896
  return () => {
1860
1897
  polling.stopAll();
1898
+ void clearSecrets();
1861
1899
  };
1862
- }, [polling]);
1900
+ }, [polling, clearSecrets]);
1863
1901
  const userContext = useUser();
1864
1902
  const sendMessage = (0, import_react9.useCallback)(
1865
1903
  async (text) => {