@dudko.dev/agent-web-react 0.0.10 → 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/README.md CHANGED
@@ -223,6 +223,12 @@ function LocalAgent() {
223
223
  > not silently on the first message) and `ready` flips only once the model can
224
224
  > actually answer. The `create` option is what makes it work under a bundler —
225
225
  > see below.
226
+ >
227
+ > Change the `modelId` and everything the hook reports — `model`, `ready`,
228
+ > `loading`, `progress`, `error` — is about the new one, which is not loaded
229
+ > yet. The previous model stays in memory (`loadedModelId`; switching back is
230
+ > instant) until the new one is loaded — that frees it first, so two models
231
+ > never share the GPU — or until `unload()`.
226
232
 
227
233
  ## Models in a bundler (Vite, Next, CRA)
228
234
 
package/dist/index.cjs CHANGED
@@ -605,13 +605,18 @@ var useAgent = (config, options = {}) => {
605
605
  if (configMode !== modeRef.current) setApprovalMode(configMode);
606
606
  }, [configMode, setApprovalMode]);
607
607
  const reload = (0, import_react.useCallback)(() => setGeneration((g) => g + 1), []);
608
+ const buildStatusRef = (0, import_react.useRef)({ status: "idle" });
609
+ const setBuildStatus = (0, import_react.useCallback)((next) => {
610
+ buildStatusRef.current = next;
611
+ if (!abortRef.current) dispatch({ type: "status", ...next });
612
+ }, []);
608
613
  const { autoStart, deps } = options;
609
614
  (0, import_react.useEffect)(() => {
610
615
  agentRef.current = void 0;
611
616
  const cfg = configRef.current;
612
617
  const shouldBuild = (autoStart !== false || generation > 0) && Boolean(cfg.model);
613
618
  if (!shouldBuild) {
614
- dispatch({ type: "status", status: "idle" });
619
+ setBuildStatus({ status: "idle" });
615
620
  return;
616
621
  }
617
622
  const routeApprovals = optionsRef.current.approvals !== false && !cfg.toolApproval?.onRequest;
@@ -620,15 +625,15 @@ var useAgent = (config, options = {}) => {
620
625
  toolApproval: { ...cfg.toolApproval, mode: modeRef.current, onRequest: uiOnRequest }
621
626
  } : cfg;
622
627
  let cancelled = false;
623
- dispatch({ type: "status", status: "initializing" });
628
+ setBuildStatus({ status: "initializing" });
624
629
  (0, import_agent_web.createAgent)(built).then((agent) => {
625
630
  if (cancelled) return;
626
631
  agent.setToolApprovalMode(modeRef.current);
627
632
  agentRef.current = agent;
628
- dispatch({ type: "status", status: "ready" });
633
+ setBuildStatus({ status: "ready" });
629
634
  }).catch((err) => {
630
635
  if (cancelled) return;
631
- dispatch({ type: "status", status: "error", error: errMessage(err) });
636
+ setBuildStatus({ status: "error", error: errMessage(err) });
632
637
  });
633
638
  return () => {
634
639
  cancelled = true;
@@ -680,7 +685,7 @@ var useAgent = (config, options = {}) => {
680
685
  } finally {
681
686
  abortRef.current = void 0;
682
687
  settleAll({ approved: false, reason: "the run ended" });
683
- dispatch({ type: "status", status: "ready" });
688
+ dispatch({ type: "status", ...buildStatusRef.current });
684
689
  }
685
690
  },
686
691
  [settleAll]
@@ -1716,53 +1721,92 @@ var useMcpServers = (options = {}) => {
1716
1721
  var import_react8 = require("react");
1717
1722
  var import_agent_web3 = require("@dudko.dev/agent-web");
1718
1723
  var useWebLLMModel = (modelId, options) => {
1719
- const [model, setModel] = (0, import_react8.useState)(void 0);
1720
- const [loading, setLoading] = (0, import_react8.useState)(false);
1721
- const [progress, setProgress] = (0, import_react8.useState)(0);
1722
- const [text, setText] = (0, import_react8.useState)("");
1723
- const [error, setError] = (0, import_react8.useState)(void 0);
1724
+ const [loaded, setLoaded] = (0, import_react8.useState)(void 0);
1725
+ const [pending, setPending] = (0, import_react8.useState)();
1726
+ const [failure, setFailure] = (0, import_react8.useState)();
1724
1727
  const optionsRef = (0, import_react8.useRef)(options);
1725
1728
  optionsRef.current = options;
1726
- const load = (0, import_react8.useCallback)(async () => {
1729
+ const loadedRef = (0, import_react8.useRef)(void 0);
1730
+ const inflightRef = (0, import_react8.useRef)(
1731
+ void 0
1732
+ );
1733
+ const seqRef = (0, import_react8.useRef)(0);
1734
+ const setHeld = (next) => {
1735
+ loadedRef.current = next;
1736
+ setLoaded(next);
1737
+ };
1738
+ const load = (0, import_react8.useCallback)(() => {
1739
+ const id = modelId;
1740
+ if (loadedRef.current?.id === id) return Promise.resolve(loadedRef.current.model);
1741
+ if (inflightRef.current?.id === id) return inflightRef.current.promise;
1727
1742
  if (!(0, import_agent_web3.isWebGPUAvailable)()) {
1728
- setError("WebGPU is not available in this browser.");
1729
- return void 0;
1743
+ setFailure({ id, message: "WebGPU is not available in this browser." });
1744
+ return Promise.resolve(void 0);
1730
1745
  }
1731
- setLoading(true);
1732
- setError(void 0);
1733
- setProgress(0);
1734
- setText("");
1735
- try {
1736
- const { create = import_agent_web3.createWebLLMModel, ...modelOptions } = optionsRef.current ?? {};
1737
- const built = await create(modelId, {
1738
- ...modelOptions,
1739
- // Drive preload here (below) so download progress is reported the same
1740
- // way whether `create` is the core's `createWebLLMModel` or an injected
1741
- // factory (e.g. a statically-imported `webLLM`, needed under bundlers).
1742
- preload: false,
1743
- initProgressCallback: (report) => {
1744
- setProgress(report.progress);
1745
- setText(report.text);
1746
- modelOptions.initProgressCallback?.(report);
1746
+ const seq = ++seqRef.current;
1747
+ const current = () => seq === seqRef.current;
1748
+ const promise = (async () => {
1749
+ setPending({ id, progress: 0, text: "" });
1750
+ setFailure(void 0);
1751
+ let built;
1752
+ try {
1753
+ const previous = loadedRef.current;
1754
+ if (previous) {
1755
+ setHeld(void 0);
1756
+ await (0, import_agent_web3.unloadWebLLMModel)(previous.model);
1747
1757
  }
1748
- });
1749
- await (0, import_agent_web3.preloadWebLLMModel)(built);
1750
- setModel(built);
1751
- return built;
1752
- } catch (err) {
1753
- setError(errMessage(err));
1754
- return void 0;
1755
- } finally {
1756
- setLoading(false);
1757
- }
1758
+ const { create = import_agent_web3.createWebLLMModel, ...modelOptions } = optionsRef.current ?? {};
1759
+ built = await create(id, {
1760
+ ...modelOptions,
1761
+ // Drive preload here (below) so download progress is reported the same
1762
+ // way whether `create` is the core's `createWebLLMModel` or an injected
1763
+ // factory (e.g. a statically-imported `webLLM`, needed under bundlers).
1764
+ preload: false,
1765
+ initProgressCallback: (report) => {
1766
+ if (current()) setPending({ id, progress: report.progress, text: report.text });
1767
+ modelOptions.initProgressCallback?.(report);
1768
+ }
1769
+ });
1770
+ await (0, import_agent_web3.preloadWebLLMModel)(built);
1771
+ if (!current()) {
1772
+ await (0, import_agent_web3.unloadWebLLMModel)(built);
1773
+ return void 0;
1774
+ }
1775
+ setHeld({ id, model: built });
1776
+ return built;
1777
+ } catch (err) {
1778
+ if (built) await (0, import_agent_web3.unloadWebLLMModel)(built);
1779
+ if (current()) setFailure({ id, message: errMessage(err) });
1780
+ return void 0;
1781
+ } finally {
1782
+ if (current()) {
1783
+ inflightRef.current = void 0;
1784
+ setPending(void 0);
1785
+ }
1786
+ }
1787
+ })();
1788
+ inflightRef.current = { id, promise };
1789
+ return promise;
1758
1790
  }, [modelId]);
1791
+ const unload = (0, import_react8.useCallback)(async () => {
1792
+ seqRef.current++;
1793
+ inflightRef.current = void 0;
1794
+ setPending(void 0);
1795
+ const previous = loadedRef.current;
1796
+ setHeld(void 0);
1797
+ if (previous) await (0, import_agent_web3.unloadWebLLMModel)(previous.model);
1798
+ }, []);
1799
+ const model = loaded?.id === modelId ? loaded.model : void 0;
1800
+ const progressing = pending?.id === modelId ? pending : void 0;
1759
1801
  return {
1760
1802
  model,
1761
1803
  load,
1762
- loading,
1763
- progress,
1764
- text,
1765
- error,
1804
+ unload,
1805
+ loadedModelId: loaded?.id,
1806
+ loading: progressing !== void 0,
1807
+ progress: progressing?.progress ?? 0,
1808
+ text: progressing?.text ?? "",
1809
+ error: failure?.id === modelId ? failure.message : void 0,
1766
1810
  supported: (0, import_agent_web3.isWebGPUAvailable)(),
1767
1811
  ready: model !== void 0
1768
1812
  };
@@ -1948,7 +1992,13 @@ var defaultLabels = {
1948
1992
  readOnly: "Only read; refuse changes",
1949
1993
  think: (levels) => `Set thinking: ${levels}`
1950
1994
  },
1951
- thinkingLevels: { off: "Default", low: "Low", medium: "Medium", high: "High" },
1995
+ thinkingLevels: {
1996
+ off: "Default",
1997
+ none: "No thinking",
1998
+ low: "Low",
1999
+ medium: "Medium",
2000
+ high: "High"
2001
+ },
1952
2002
  filesEmpty: "No files yet. Attachments and files the agent writes appear here.",
1953
2003
  upload: "Upload",
1954
2004
  download: "Download",
@@ -2135,6 +2185,7 @@ var DownloadIcon = ({ size, className }) => /* @__PURE__ */ (0, import_jsx_runti
2135
2185
  var import_jsx_runtime4 = require("react/jsx-runtime");
2136
2186
  var thinkingDefaults = (l) => [
2137
2187
  { id: "off", label: l.thinkingLevels.off },
2188
+ { id: "none", label: l.thinkingLevels.none },
2138
2189
  { id: "low", label: l.thinkingLevels.low },
2139
2190
  { id: "medium", label: l.thinkingLevels.medium },
2140
2191
  { id: "high", label: l.thinkingLevels.high }