@aomi-labs/react 0.3.6 → 0.3.8

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
@@ -20,6 +20,19 @@ var __spreadValues = (a, b) => {
20
20
  return a;
21
21
  };
22
22
  var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
23
+ var __restKey = (key) => typeof key === "symbol" ? key : key + "";
24
+ var __objRest = (source, exclude) => {
25
+ var target = {};
26
+ for (var prop in source)
27
+ if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
28
+ target[prop] = source[prop];
29
+ if (source != null && __getOwnPropSymbols)
30
+ for (var prop of __getOwnPropSymbols(source)) {
31
+ if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
32
+ target[prop] = source[prop];
33
+ }
34
+ return target;
35
+ };
23
36
  var __export = (target, all) => {
24
37
  for (var name in all)
25
38
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -247,6 +260,25 @@ var ThreadStore = class {
247
260
  // packages/react/src/contexts/control-context.tsx
248
261
  var import_jsx_runtime = require("react/jsx-runtime");
249
262
  var API_KEY_STORAGE_KEY = "aomi_api_key";
263
+ var CLIENT_ID_STORAGE_KEY = "aomi_client_id";
264
+ var PROVIDER_KEYS_STORAGE_KEY = "aomi_provider_keys";
265
+ var PROVIDER_KEY_SECRET_PREFIX = "PROVIDER_KEY:";
266
+ function getOrCreateClientId() {
267
+ var _a, _b, _c, _d, _e;
268
+ try {
269
+ const storedClientId = (_a = globalThis.localStorage) == null ? void 0 : _a.getItem(CLIENT_ID_STORAGE_KEY);
270
+ if (storedClientId && storedClientId.trim().length > 0) {
271
+ return storedClientId;
272
+ }
273
+ } catch (e) {
274
+ }
275
+ const clientId = (_d = (_c = (_b = globalThis.crypto) == null ? void 0 : _b.randomUUID) == null ? void 0 : _c.call(_b)) != null ? _d : `client-${Date.now()}`;
276
+ try {
277
+ (_e = globalThis.localStorage) == null ? void 0 : _e.setItem(CLIENT_ID_STORAGE_KEY, clientId);
278
+ } catch (e) {
279
+ }
280
+ return clientId;
281
+ }
250
282
  function getDefaultApp(apps) {
251
283
  var _a;
252
284
  return apps.includes("default") ? "default" : (_a = apps[0]) != null ? _a : null;
@@ -276,11 +308,12 @@ function ControlContextProvider({
276
308
  var _a, _b;
277
309
  const [state, setStateInternal] = (0, import_react.useState)(() => ({
278
310
  apiKey: null,
279
- clientId: null,
311
+ clientId: getOrCreateClientId(),
280
312
  availableModels: [],
281
313
  authorizedApps: [],
282
314
  defaultModel: null,
283
- defaultApp: null
315
+ defaultApp: null,
316
+ providerKeys: {}
284
317
  }));
285
318
  const stateRef = (0, import_react.useRef)(state);
286
319
  stateRef.current = state;
@@ -298,10 +331,14 @@ function ControlContextProvider({
298
331
  const currentThreadMetadata = getThreadMetadata(sessionId);
299
332
  const isProcessing = (_b = (_a = currentThreadMetadata == null ? void 0 : currentThreadMetadata.control) == null ? void 0 : _a.isProcessing) != null ? _b : false;
300
333
  (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
- }, []);
334
+ var _a2;
335
+ try {
336
+ if (state.clientId) {
337
+ (_a2 = globalThis.localStorage) == null ? void 0 : _a2.setItem(CLIENT_ID_STORAGE_KEY, state.clientId);
338
+ }
339
+ } catch (e) {
340
+ }
341
+ }, [state.clientId]);
305
342
  (0, import_react.useEffect)(() => {
306
343
  var _a2, _b2;
307
344
  try {
@@ -312,6 +349,17 @@ function ControlContextProvider({
312
349
  } catch (e) {
313
350
  }
314
351
  }, []);
352
+ (0, import_react.useEffect)(() => {
353
+ var _a2;
354
+ try {
355
+ const raw = (_a2 = globalThis.localStorage) == null ? void 0 : _a2.getItem(PROVIDER_KEYS_STORAGE_KEY);
356
+ if (raw) {
357
+ const parsed = JSON.parse(raw);
358
+ setStateInternal((prev) => __spreadProps(__spreadValues({}, prev), { providerKeys: parsed }));
359
+ }
360
+ } catch (e) {
361
+ }
362
+ }, []);
315
363
  (0, import_react.useEffect)(() => {
316
364
  var _a2, _b2;
317
365
  try {
@@ -323,6 +371,33 @@ function ControlContextProvider({
323
371
  } catch (e) {
324
372
  }
325
373
  }, [state.apiKey]);
374
+ (0, import_react.useEffect)(() => {
375
+ var _a2, _b2;
376
+ try {
377
+ const keys = state.providerKeys;
378
+ if (Object.keys(keys).length > 0) {
379
+ (_a2 = globalThis.localStorage) == null ? void 0 : _a2.setItem(
380
+ PROVIDER_KEYS_STORAGE_KEY,
381
+ JSON.stringify(keys)
382
+ );
383
+ } else {
384
+ (_b2 = globalThis.localStorage) == null ? void 0 : _b2.removeItem(PROVIDER_KEYS_STORAGE_KEY);
385
+ }
386
+ } catch (e) {
387
+ }
388
+ }, [state.providerKeys]);
389
+ (0, import_react.useEffect)(() => {
390
+ if (!state.clientId) return;
391
+ const keys = stateRef.current.providerKeys;
392
+ if (Object.keys(keys).length === 0) return;
393
+ const secrets = {};
394
+ for (const [provider, entry] of Object.entries(keys)) {
395
+ secrets[`${PROVIDER_KEY_SECRET_PREFIX}${provider}`] = entry.apiKey;
396
+ }
397
+ void aomiClientRef.current.ingestSecrets(state.clientId, secrets).catch((err) => {
398
+ console.error("Failed to auto-ingest provider keys:", err);
399
+ });
400
+ }, [state.clientId, state.providerKeys]);
326
401
  (0, import_react.useEffect)(() => {
327
402
  const fetchApps = async () => {
328
403
  var _a2;
@@ -393,6 +468,65 @@ function ControlContextProvider({
393
468
  if (!clientId) return;
394
469
  await ((_b2 = (_a2 = aomiClientRef.current).clearSecrets) == null ? void 0 : _b2.call(_a2, clientId));
395
470
  }, []);
471
+ const setProviderKey = (0, import_react.useCallback)(
472
+ async (provider, apiKey, label) => {
473
+ const trimmed = apiKey.trim();
474
+ if (!trimmed) return;
475
+ const entry = {
476
+ apiKey: trimmed,
477
+ keyPrefix: trimmed.slice(0, 7),
478
+ label
479
+ };
480
+ setStateInternal((prev) => {
481
+ const next = __spreadProps(__spreadValues({}, prev), {
482
+ providerKeys: __spreadProps(__spreadValues({}, prev.providerKeys), { [provider]: entry })
483
+ });
484
+ callbacks.current.forEach((cb) => cb(next));
485
+ return next;
486
+ });
487
+ const clientId = stateRef.current.clientId;
488
+ if (clientId) {
489
+ try {
490
+ await aomiClientRef.current.ingestSecrets(clientId, {
491
+ [`${PROVIDER_KEY_SECRET_PREFIX}${provider}`]: trimmed
492
+ });
493
+ } catch (err) {
494
+ console.error("Failed to ingest provider key:", err);
495
+ }
496
+ }
497
+ },
498
+ []
499
+ );
500
+ const removeProviderKey = (0, import_react.useCallback)(
501
+ async (provider) => {
502
+ const clientId = stateRef.current.clientId;
503
+ if (clientId) {
504
+ await aomiClientRef.current.deleteSecret(
505
+ clientId,
506
+ `${PROVIDER_KEY_SECRET_PREFIX}${provider}`
507
+ );
508
+ }
509
+ setStateInternal((prev) => {
510
+ const _a2 = prev.providerKeys, { [provider]: _ } = _a2, rest = __objRest(_a2, [__restKey(provider)]);
511
+ const next = __spreadProps(__spreadValues({}, prev), { providerKeys: rest });
512
+ callbacks.current.forEach((cb) => cb(next));
513
+ return next;
514
+ });
515
+ },
516
+ []
517
+ );
518
+ const getProviderKeys = (0, import_react.useCallback)(
519
+ () => stateRef.current.providerKeys,
520
+ []
521
+ );
522
+ const hasProviderKey = (0, import_react.useCallback)(
523
+ (provider) => {
524
+ const keys = stateRef.current.providerKeys;
525
+ if (provider) return provider in keys;
526
+ return Object.keys(keys).length > 0;
527
+ },
528
+ []
529
+ );
396
530
  const getAvailableModels = (0, import_react.useCallback)(async () => {
397
531
  try {
398
532
  const models = await aomiClientRef.current.getModels(
@@ -451,7 +585,7 @@ function ControlContextProvider({
451
585
  )) != null ? _c : "default";
452
586
  }, []);
453
587
  const onModelSelect = (0, import_react.useCallback)(async (model) => {
454
- var _a2, _b2, _c, _d;
588
+ var _a2, _b2, _c, _d, _e;
455
589
  const threadId = sessionIdRef.current;
456
590
  const currentControl = (_b2 = (_a2 = getThreadMetadataRef.current(threadId)) == null ? void 0 : _a2.control) != null ? _b2 : initThreadControl();
457
591
  const isProcessing2 = currentControl.isProcessing;
@@ -492,7 +626,11 @@ function ControlContextProvider({
492
626
  const result = await aomiClientRef.current.setModel(
493
627
  threadId,
494
628
  model,
495
- { app, apiKey: (_d = stateRef.current.apiKey) != null ? _d : void 0 }
629
+ {
630
+ app,
631
+ apiKey: (_d = stateRef.current.apiKey) != null ? _d : void 0,
632
+ clientId: (_e = stateRef.current.clientId) != null ? _e : void 0
633
+ }
496
634
  );
497
635
  console.log("[control-context] onModelSelect backend result", result);
498
636
  } catch (err) {
@@ -575,6 +713,10 @@ function ControlContextProvider({
575
713
  setApiKey,
576
714
  ingestSecrets,
577
715
  clearSecrets,
716
+ setProviderKey,
717
+ removeProviderKey,
718
+ getProviderKeys,
719
+ hasProviderKey,
578
720
  getAvailableModels,
579
721
  getAuthorizedApps,
580
722
  getCurrentThreadControl,
@@ -1360,7 +1502,7 @@ function AomiRuntimeCore({
1360
1502
  const eventContext = useEventContext();
1361
1503
  const notificationContext = useNotification();
1362
1504
  const { user, onUserStateChange, getUserState } = useUser();
1363
- const { getControlState, getCurrentThreadApp, clearSecrets } = useControl();
1505
+ const { getControlState, getCurrentThreadApp } = useControl();
1364
1506
  const sessionManagerRef = (0, import_react9.useRef)(null);
1365
1507
  const walletHandler = useWalletHandler({
1366
1508
  getSession: () => {
@@ -1564,9 +1706,8 @@ function AomiRuntimeCore({
1564
1706
  (0, import_react9.useEffect)(() => {
1565
1707
  return () => {
1566
1708
  sessionManager.closeAll();
1567
- void clearSecrets();
1568
1709
  };
1569
- }, [sessionManager, clearSecrets]);
1710
+ }, [sessionManager]);
1570
1711
  const userContext = useUser();
1571
1712
  const sendMessage = (0, import_react9.useCallback)(
1572
1713
  async (text) => {