@picsart/ai-sdk 7.1.0 → 7.2.0

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.
Files changed (3) hide show
  1. package/index.d.ts +10 -0
  2. package/index.js +45 -2
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -1348,6 +1348,16 @@ interface GenerateOptions {
1348
1348
  interface InitModelCatalogOptions {
1349
1349
  /** Restrict what the catalog load requests — mode, provider, release, deprecated, search. */
1350
1350
  filtersOptions?: ModelCatalogFilters;
1351
+ /**
1352
+ * How often, in seconds, to re-pull the catalog in the background after the
1353
+ * first load so a long-lived process picks up new/changed models without
1354
+ * restarting. The timer arms only after the initial load succeeds and
1355
+ * re-fetches with the same `filtersOptions`. A refresh that fails leaves the
1356
+ * previously loaded catalog in place (the next tick retries).
1357
+ *
1358
+ * Default `-1` — no scheduled refresh (any value `<= 0` disables it).
1359
+ */
1360
+ refreshIntervalSeconds?: number;
1351
1361
  }
1352
1362
  /**
1353
1363
  * Non-text (image/video/audio) model id. Dynamic-only: ids are plain strings
package/index.js CHANGED
@@ -625,7 +625,23 @@ var CATALOG_PATH = "/gw-v2/v1/models-catalog";
625
625
  var _loadPromise = null;
626
626
  var _loaded = false;
627
627
  var _key = null;
628
+ var _refreshTimer = null;
628
629
  var keyOf = (apiUrl, filters) => `${apiUrl}::${JSON.stringify(filters ?? {})}`;
630
+ function clearRefreshSchedule() {
631
+ if (_refreshTimer) {
632
+ clearInterval(_refreshTimer);
633
+ _refreshTimer = null;
634
+ }
635
+ }
636
+ function scheduleRefresh(f, apiUrl, filters, seconds) {
637
+ clearRefreshSchedule();
638
+ if (!(seconds > 0)) return;
639
+ _refreshTimer = setInterval(() => {
640
+ void refreshModelCatalog(f, apiUrl, filters).catch(() => {
641
+ });
642
+ }, seconds * 1e3);
643
+ _refreshTimer.unref?.();
644
+ }
629
645
  function buildQuery(filters) {
630
646
  const query = new URLSearchParams();
631
647
  query.set("include", "schema,constraints,routing");
@@ -654,7 +670,7 @@ async function fetchAndInstall(f, apiUrl, filters) {
654
670
  const models = deserializeCatalog(await res.json());
655
671
  installCatalog(models);
656
672
  }
657
- function loadModelCatalog(f, apiUrl, filters) {
673
+ function loadModelCatalog(f, apiUrl, filters, refreshIntervalSeconds = -1) {
658
674
  const key = keyOf(apiUrl, filters);
659
675
  if (_loadPromise) {
660
676
  if (_key !== key) {
@@ -663,11 +679,13 @@ function loadModelCatalog(f, apiUrl, filters) {
663
679
  { status: 409, code: "catalog_conflict" }
664
680
  ));
665
681
  }
682
+ if (_loaded) scheduleRefresh(f, apiUrl, filters, refreshIntervalSeconds);
666
683
  return _loadPromise;
667
684
  }
668
685
  _key = key;
669
686
  _loadPromise = fetchAndInstall(f, apiUrl, filters).then(() => {
670
687
  _loaded = true;
688
+ scheduleRefresh(f, apiUrl, filters, refreshIntervalSeconds);
671
689
  }).catch((err) => {
672
690
  _loadPromise = null;
673
691
  _key = null;
@@ -675,6 +693,26 @@ function loadModelCatalog(f, apiUrl, filters) {
675
693
  });
676
694
  return _loadPromise;
677
695
  }
696
+ function refreshModelCatalog(f, apiUrl, filters) {
697
+ const key = keyOf(apiUrl, filters);
698
+ const prevPromise = _loadPromise;
699
+ const prevLoaded = _loaded;
700
+ const prevKey = _key;
701
+ const p2 = fetchAndInstall(f, apiUrl, filters).then(() => {
702
+ _loaded = true;
703
+ }).catch((err) => {
704
+ if (_loadPromise === p2) {
705
+ _loadPromise = prevPromise;
706
+ _loaded = prevLoaded;
707
+ _key = prevKey;
708
+ }
709
+ throw err;
710
+ });
711
+ _loadPromise = p2;
712
+ _loaded = false;
713
+ _key = key;
714
+ return p2;
715
+ }
678
716
  function whenModelCatalogLoaded() {
679
717
  return _loadPromise;
680
718
  }
@@ -3784,7 +3822,12 @@ function createClient(config) {
3784
3822
  code: "validation_error"
3785
3823
  });
3786
3824
  }
3787
- return loadModelCatalog(authedFetch, config.apiUrl, initOptions?.filtersOptions);
3825
+ return loadModelCatalog(
3826
+ authedFetch,
3827
+ config.apiUrl,
3828
+ initOptions?.filtersOptions,
3829
+ initOptions?.refreshIntervalSeconds
3830
+ );
3788
3831
  },
3789
3832
  /**
3790
3833
  * Validate params against the model's descriptors without generating.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@picsart/ai-sdk",
3
- "version": "7.1.0",
3
+ "version": "7.2.0",
4
4
  "type": "module",
5
5
  "description": "Type-safe SDK for 100+ AI models — image, video, audio, and text generation with Picsart",
6
6
  "license": "MIT",