@dudko.dev/agent-web-react 0.0.11 → 0.0.12

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
@@ -752,21 +752,33 @@ interface UseWebLLMModelOptions extends WebLLMModelOptions {
752
752
  create?: WebLLMModelFactory;
753
753
  }
754
754
  interface UseWebLLMModelReturn {
755
- /** The built model once loaded — pass to `createAgent({ model })`. */
755
+ /**
756
+ * The built model, once **this** `modelId` is loaded — pass to
757
+ * `createAgent({ model })`. Undefined right after the id changes, until the
758
+ * new one is loaded.
759
+ */
756
760
  model: WebLLMModel | undefined;
757
- /** Start the download / engine init. Idempotent-ish: safe to call again to retry. */
761
+ /**
762
+ * Download and initialize the current `modelId`. Resolves with the model
763
+ * already loaded for it, if any. Loading another id first frees the previous
764
+ * model's GPU memory (one engine per hook); call again after an error to retry.
765
+ */
758
766
  load: () => Promise<WebLLMModel | undefined>;
759
- /** True while weights are downloading / the engine is initializing. */
767
+ /** Free the loaded model's GPU memory (and drop a load in flight). */
768
+ unload: () => Promise<void>;
769
+ /** The id of the model held in memory, whichever id is current. */
770
+ loadedModelId: string | undefined;
771
+ /** True while the current `modelId` is downloading / initializing. */
760
772
  loading: boolean;
761
- /** Load progress, 0..1. */
773
+ /** Load progress of the current `modelId`, 0..1. */
762
774
  progress: number;
763
775
  /** Human-readable progress text from WebLLM. */
764
776
  text: string;
765
- /** Error message if the load failed. */
777
+ /** Why the current `modelId` failed to load. */
766
778
  error: string | undefined;
767
779
  /** Whether WebGPU is available (required for local models). */
768
780
  supported: boolean;
769
- /** True once the model is ready. */
781
+ /** True once the current `modelId` is ready. */
770
782
  ready: boolean;
771
783
  }
772
784
  /**
@@ -776,6 +788,12 @@ interface UseWebLLMModelReturn {
776
788
  * the core's `preloadWebLLMModel`), so `ready` means "ready to chat" and
777
789
  * progress fills during the load rather than silently on the first message.
778
790
  *
791
+ * Everything it reports is about the **current** `modelId`: switch the id and
792
+ * `model` / `ready` / `progress` / `error` describe the new one (not loaded
793
+ * yet), while the previous model stays in memory — switching back is instant.
794
+ * Loading the new id frees the previous one first, so two models never hold
795
+ * GPU memory at once; `unload()` frees it on demand.
796
+ *
779
797
  * ```tsx
780
798
  * import { webLLM } from '@browser-ai/web-llm' // your app's optional peer
781
799
  * const create = (id: string, opts?: WebLLMModelOptions) => Promise.resolve(webLLM(id, opts))
@@ -949,8 +967,10 @@ interface AgentLabels {
949
967
  readOnly: string;
950
968
  think: (levels: string) => string;
951
969
  };
970
+ /** `off` = the provider's default; `none` = thinking switched off. */
952
971
  thinkingLevels: {
953
972
  off: string;
973
+ none: string;
954
974
  low: string;
955
975
  medium: string;
956
976
  high: string;
package/dist/index.d.ts CHANGED
@@ -752,21 +752,33 @@ interface UseWebLLMModelOptions extends WebLLMModelOptions {
752
752
  create?: WebLLMModelFactory;
753
753
  }
754
754
  interface UseWebLLMModelReturn {
755
- /** The built model once loaded — pass to `createAgent({ model })`. */
755
+ /**
756
+ * The built model, once **this** `modelId` is loaded — pass to
757
+ * `createAgent({ model })`. Undefined right after the id changes, until the
758
+ * new one is loaded.
759
+ */
756
760
  model: WebLLMModel | undefined;
757
- /** Start the download / engine init. Idempotent-ish: safe to call again to retry. */
761
+ /**
762
+ * Download and initialize the current `modelId`. Resolves with the model
763
+ * already loaded for it, if any. Loading another id first frees the previous
764
+ * model's GPU memory (one engine per hook); call again after an error to retry.
765
+ */
758
766
  load: () => Promise<WebLLMModel | undefined>;
759
- /** True while weights are downloading / the engine is initializing. */
767
+ /** Free the loaded model's GPU memory (and drop a load in flight). */
768
+ unload: () => Promise<void>;
769
+ /** The id of the model held in memory, whichever id is current. */
770
+ loadedModelId: string | undefined;
771
+ /** True while the current `modelId` is downloading / initializing. */
760
772
  loading: boolean;
761
- /** Load progress, 0..1. */
773
+ /** Load progress of the current `modelId`, 0..1. */
762
774
  progress: number;
763
775
  /** Human-readable progress text from WebLLM. */
764
776
  text: string;
765
- /** Error message if the load failed. */
777
+ /** Why the current `modelId` failed to load. */
766
778
  error: string | undefined;
767
779
  /** Whether WebGPU is available (required for local models). */
768
780
  supported: boolean;
769
- /** True once the model is ready. */
781
+ /** True once the current `modelId` is ready. */
770
782
  ready: boolean;
771
783
  }
772
784
  /**
@@ -776,6 +788,12 @@ interface UseWebLLMModelReturn {
776
788
  * the core's `preloadWebLLMModel`), so `ready` means "ready to chat" and
777
789
  * progress fills during the load rather than silently on the first message.
778
790
  *
791
+ * Everything it reports is about the **current** `modelId`: switch the id and
792
+ * `model` / `ready` / `progress` / `error` describe the new one (not loaded
793
+ * yet), while the previous model stays in memory — switching back is instant.
794
+ * Loading the new id frees the previous one first, so two models never hold
795
+ * GPU memory at once; `unload()` frees it on demand.
796
+ *
779
797
  * ```tsx
780
798
  * import { webLLM } from '@browser-ai/web-llm' // your app's optional peer
781
799
  * const create = (id: string, opts?: WebLLMModelOptions) => Promise.resolve(webLLM(id, opts))
@@ -949,8 +967,10 @@ interface AgentLabels {
949
967
  readOnly: string;
950
968
  think: (levels: string) => string;
951
969
  };
970
+ /** `off` = the provider's default; `none` = thinking switched off. */
952
971
  thinkingLevels: {
953
972
  off: string;
973
+ none: string;
954
974
  low: string;
955
975
  medium: string;
956
976
  high: string;
package/dist/index.js CHANGED
@@ -499,13 +499,18 @@ var useAgent = (config, options = {}) => {
499
499
  if (configMode !== modeRef.current) setApprovalMode(configMode);
500
500
  }, [configMode, setApprovalMode]);
501
501
  const reload = useCallback(() => setGeneration((g) => g + 1), []);
502
+ const buildStatusRef = useRef({ status: "idle" });
503
+ const setBuildStatus = useCallback((next) => {
504
+ buildStatusRef.current = next;
505
+ if (!abortRef.current) dispatch({ type: "status", ...next });
506
+ }, []);
502
507
  const { autoStart, deps } = options;
503
508
  useEffect(() => {
504
509
  agentRef.current = void 0;
505
510
  const cfg = configRef.current;
506
511
  const shouldBuild = (autoStart !== false || generation > 0) && Boolean(cfg.model);
507
512
  if (!shouldBuild) {
508
- dispatch({ type: "status", status: "idle" });
513
+ setBuildStatus({ status: "idle" });
509
514
  return;
510
515
  }
511
516
  const routeApprovals = optionsRef.current.approvals !== false && !cfg.toolApproval?.onRequest;
@@ -514,15 +519,15 @@ var useAgent = (config, options = {}) => {
514
519
  toolApproval: { ...cfg.toolApproval, mode: modeRef.current, onRequest: uiOnRequest }
515
520
  } : cfg;
516
521
  let cancelled = false;
517
- dispatch({ type: "status", status: "initializing" });
522
+ setBuildStatus({ status: "initializing" });
518
523
  createAgent(built).then((agent) => {
519
524
  if (cancelled) return;
520
525
  agent.setToolApprovalMode(modeRef.current);
521
526
  agentRef.current = agent;
522
- dispatch({ type: "status", status: "ready" });
527
+ setBuildStatus({ status: "ready" });
523
528
  }).catch((err) => {
524
529
  if (cancelled) return;
525
- dispatch({ type: "status", status: "error", error: errMessage(err) });
530
+ setBuildStatus({ status: "error", error: errMessage(err) });
526
531
  });
527
532
  return () => {
528
533
  cancelled = true;
@@ -574,7 +579,7 @@ var useAgent = (config, options = {}) => {
574
579
  } finally {
575
580
  abortRef.current = void 0;
576
581
  settleAll({ approved: false, reason: "the run ended" });
577
- dispatch({ type: "status", status: "ready" });
582
+ dispatch({ type: "status", ...buildStatusRef.current });
578
583
  }
579
584
  },
580
585
  [settleAll]
@@ -1611,56 +1616,96 @@ import { useCallback as useCallback8, useRef as useRef6, useState as useState8 }
1611
1616
  import {
1612
1617
  createWebLLMModel,
1613
1618
  isWebGPUAvailable,
1614
- preloadWebLLMModel
1619
+ preloadWebLLMModel,
1620
+ unloadWebLLMModel
1615
1621
  } from "@dudko.dev/agent-web";
1616
1622
  var useWebLLMModel = (modelId, options) => {
1617
- const [model, setModel] = useState8(void 0);
1618
- const [loading, setLoading] = useState8(false);
1619
- const [progress, setProgress] = useState8(0);
1620
- const [text, setText] = useState8("");
1621
- const [error, setError] = useState8(void 0);
1623
+ const [loaded, setLoaded] = useState8(void 0);
1624
+ const [pending, setPending] = useState8();
1625
+ const [failure, setFailure] = useState8();
1622
1626
  const optionsRef = useRef6(options);
1623
1627
  optionsRef.current = options;
1624
- const load = useCallback8(async () => {
1628
+ const loadedRef = useRef6(void 0);
1629
+ const inflightRef = useRef6(
1630
+ void 0
1631
+ );
1632
+ const seqRef = useRef6(0);
1633
+ const setHeld = (next) => {
1634
+ loadedRef.current = next;
1635
+ setLoaded(next);
1636
+ };
1637
+ const load = useCallback8(() => {
1638
+ const id = modelId;
1639
+ if (loadedRef.current?.id === id) return Promise.resolve(loadedRef.current.model);
1640
+ if (inflightRef.current?.id === id) return inflightRef.current.promise;
1625
1641
  if (!isWebGPUAvailable()) {
1626
- setError("WebGPU is not available in this browser.");
1627
- return void 0;
1642
+ setFailure({ id, message: "WebGPU is not available in this browser." });
1643
+ return Promise.resolve(void 0);
1628
1644
  }
1629
- setLoading(true);
1630
- setError(void 0);
1631
- setProgress(0);
1632
- setText("");
1633
- try {
1634
- const { create = createWebLLMModel, ...modelOptions } = optionsRef.current ?? {};
1635
- const built = await create(modelId, {
1636
- ...modelOptions,
1637
- // Drive preload here (below) so download progress is reported the same
1638
- // way whether `create` is the core's `createWebLLMModel` or an injected
1639
- // factory (e.g. a statically-imported `webLLM`, needed under bundlers).
1640
- preload: false,
1641
- initProgressCallback: (report) => {
1642
- setProgress(report.progress);
1643
- setText(report.text);
1644
- modelOptions.initProgressCallback?.(report);
1645
+ const seq = ++seqRef.current;
1646
+ const current = () => seq === seqRef.current;
1647
+ const promise = (async () => {
1648
+ setPending({ id, progress: 0, text: "" });
1649
+ setFailure(void 0);
1650
+ let built;
1651
+ try {
1652
+ const previous = loadedRef.current;
1653
+ if (previous) {
1654
+ setHeld(void 0);
1655
+ await unloadWebLLMModel(previous.model);
1645
1656
  }
1646
- });
1647
- await preloadWebLLMModel(built);
1648
- setModel(built);
1649
- return built;
1650
- } catch (err) {
1651
- setError(errMessage(err));
1652
- return void 0;
1653
- } finally {
1654
- setLoading(false);
1655
- }
1657
+ const { create = createWebLLMModel, ...modelOptions } = optionsRef.current ?? {};
1658
+ built = await create(id, {
1659
+ ...modelOptions,
1660
+ // Drive preload here (below) so download progress is reported the same
1661
+ // way whether `create` is the core's `createWebLLMModel` or an injected
1662
+ // factory (e.g. a statically-imported `webLLM`, needed under bundlers).
1663
+ preload: false,
1664
+ initProgressCallback: (report) => {
1665
+ if (current()) setPending({ id, progress: report.progress, text: report.text });
1666
+ modelOptions.initProgressCallback?.(report);
1667
+ }
1668
+ });
1669
+ await preloadWebLLMModel(built);
1670
+ if (!current()) {
1671
+ await unloadWebLLMModel(built);
1672
+ return void 0;
1673
+ }
1674
+ setHeld({ id, model: built });
1675
+ return built;
1676
+ } catch (err) {
1677
+ if (built) await unloadWebLLMModel(built);
1678
+ if (current()) setFailure({ id, message: errMessage(err) });
1679
+ return void 0;
1680
+ } finally {
1681
+ if (current()) {
1682
+ inflightRef.current = void 0;
1683
+ setPending(void 0);
1684
+ }
1685
+ }
1686
+ })();
1687
+ inflightRef.current = { id, promise };
1688
+ return promise;
1656
1689
  }, [modelId]);
1690
+ const unload = useCallback8(async () => {
1691
+ seqRef.current++;
1692
+ inflightRef.current = void 0;
1693
+ setPending(void 0);
1694
+ const previous = loadedRef.current;
1695
+ setHeld(void 0);
1696
+ if (previous) await unloadWebLLMModel(previous.model);
1697
+ }, []);
1698
+ const model = loaded?.id === modelId ? loaded.model : void 0;
1699
+ const progressing = pending?.id === modelId ? pending : void 0;
1657
1700
  return {
1658
1701
  model,
1659
1702
  load,
1660
- loading,
1661
- progress,
1662
- text,
1663
- error,
1703
+ unload,
1704
+ loadedModelId: loaded?.id,
1705
+ loading: progressing !== void 0,
1706
+ progress: progressing?.progress ?? 0,
1707
+ text: progressing?.text ?? "",
1708
+ error: failure?.id === modelId ? failure.message : void 0,
1664
1709
  supported: isWebGPUAvailable(),
1665
1710
  ready: model !== void 0
1666
1711
  };
@@ -1846,7 +1891,13 @@ var defaultLabels = {
1846
1891
  readOnly: "Only read; refuse changes",
1847
1892
  think: (levels) => `Set thinking: ${levels}`
1848
1893
  },
1849
- thinkingLevels: { off: "Default", low: "Low", medium: "Medium", high: "High" },
1894
+ thinkingLevels: {
1895
+ off: "Default",
1896
+ none: "No thinking",
1897
+ low: "Low",
1898
+ medium: "Medium",
1899
+ high: "High"
1900
+ },
1850
1901
  filesEmpty: "No files yet. Attachments and files the agent writes appear here.",
1851
1902
  upload: "Upload",
1852
1903
  download: "Download",
@@ -2033,6 +2084,7 @@ var DownloadIcon = ({ size, className }) => /* @__PURE__ */ jsx3("svg", { ...bas
2033
2084
  import { Fragment, jsx as jsx4, jsxs as jsxs2 } from "react/jsx-runtime";
2034
2085
  var thinkingDefaults = (l) => [
2035
2086
  { id: "off", label: l.thinkingLevels.off },
2087
+ { id: "none", label: l.thinkingLevels.none },
2036
2088
  { id: "low", label: l.thinkingLevels.low },
2037
2089
  { id: "medium", label: l.thinkingLevels.medium },
2038
2090
  { id: "high", label: l.thinkingLevels.high }
@@ -3889,7 +3941,7 @@ import {
3889
3941
  markReadOnly,
3890
3942
  createWebLLMModel as createWebLLMModel2,
3891
3943
  preloadWebLLMModel as preloadWebLLMModel2,
3892
- unloadWebLLMModel,
3944
+ unloadWebLLMModel as unloadWebLLMModel2,
3893
3945
  isWebGPUAvailable as isWebGPUAvailable2,
3894
3946
  VaultCredentialStore as VaultCredentialStore2,
3895
3947
  MemoryCredentialStore,
@@ -3962,7 +4014,7 @@ export {
3962
4014
  stripOAuthParams,
3963
4015
  supportsImages,
3964
4016
  supportsPdf,
3965
- unloadWebLLMModel,
4017
+ unloadWebLLMModel2 as unloadWebLLMModel,
3966
4018
  usageLine,
3967
4019
  useAgent,
3968
4020
  useAgentContext,