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

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.js CHANGED
@@ -1612,70 +1612,81 @@ var useMcpServers = (options = {}) => {
1612
1612
  };
1613
1613
 
1614
1614
  // src/hooks/use-webllm-model.ts
1615
- import { useCallback as useCallback8, useRef as useRef6, useState as useState8 } from "react";
1616
1615
  import {
1617
1616
  createWebLLMModel,
1618
1617
  isWebGPUAvailable,
1619
1618
  preloadWebLLMModel,
1620
- unloadWebLLMModel
1619
+ unloadWebLLMModel,
1620
+ webLLMContextWindow
1621
1621
  } from "@dudko.dev/agent-web";
1622
- var useWebLLMModel = (modelId, options) => {
1622
+
1623
+ // src/hooks/use-local-model.ts
1624
+ import { useCallback as useCallback8, useRef as useRef6, useState as useState8 } from "react";
1625
+ var keyOf = (id, tokens) => tokens ? `${id}@${tokens}` : id;
1626
+ var useLocalModel = (modelId, engine, options) => {
1623
1627
  const [loaded, setLoaded] = useState8(void 0);
1624
1628
  const [pending, setPending] = useState8();
1625
1629
  const [failure, setFailure] = useState8();
1626
- const optionsRef = useRef6(options);
1627
- optionsRef.current = options;
1630
+ const engineRef = useRef6(engine);
1631
+ engineRef.current = engine;
1628
1632
  const loadedRef = useRef6(void 0);
1629
1633
  const inflightRef = useRef6(
1630
1634
  void 0
1631
1635
  );
1636
+ const windowTokens = options?.contextWindowTokens;
1637
+ const key = keyOf(modelId, windowTokens);
1632
1638
  const seqRef = useRef6(0);
1633
1639
  const setHeld = (next) => {
1634
1640
  loadedRef.current = next;
1635
1641
  setLoaded(next);
1636
1642
  };
1643
+ const freeWith = (eng, model2) => async () => {
1644
+ try {
1645
+ await eng.unload?.(model2);
1646
+ } catch {
1647
+ }
1648
+ };
1637
1649
  const load = useCallback8(() => {
1638
1650
  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;
1641
- if (!isWebGPUAvailable()) {
1642
- setFailure({ id, message: "WebGPU is not available in this browser." });
1651
+ if (loadedRef.current?.key === key) return Promise.resolve(loadedRef.current.model);
1652
+ if (inflightRef.current?.key === key) return inflightRef.current.promise;
1653
+ const eng = engineRef.current;
1654
+ if (eng.supported && !eng.supported()) {
1655
+ setFailure({
1656
+ key,
1657
+ message: eng.unsupportedMessage ?? "This browser cannot run this model."
1658
+ });
1643
1659
  return Promise.resolve(void 0);
1644
1660
  }
1645
1661
  const seq = ++seqRef.current;
1646
1662
  const current = () => seq === seqRef.current;
1663
+ const ctx = {
1664
+ onProgress: (report) => {
1665
+ if (current()) setPending({ key, progress: report.progress, text: report.text });
1666
+ },
1667
+ ...windowTokens ? { contextWindowTokens: windowTokens } : {}
1668
+ };
1647
1669
  const promise = (async () => {
1648
- setPending({ id, progress: 0, text: "" });
1670
+ setPending({ key, progress: 0, text: "" });
1649
1671
  setFailure(void 0);
1650
1672
  let built;
1651
1673
  try {
1652
1674
  const previous = loadedRef.current;
1653
1675
  if (previous) {
1654
1676
  setHeld(void 0);
1655
- await unloadWebLLMModel(previous.model);
1677
+ await previous.free();
1656
1678
  }
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);
1679
+ built = await eng.create(id, ctx);
1680
+ await eng.warmUp?.(built, ctx);
1670
1681
  if (!current()) {
1671
- await unloadWebLLMModel(built);
1682
+ await freeWith(eng, built)();
1672
1683
  return void 0;
1673
1684
  }
1674
- setHeld({ id, model: built });
1685
+ setHeld({ id, key, model: built, free: freeWith(eng, built) });
1675
1686
  return built;
1676
1687
  } catch (err) {
1677
- if (built) await unloadWebLLMModel(built);
1678
- if (current()) setFailure({ id, message: errMessage(err) });
1688
+ if (built) await freeWith(eng, built)();
1689
+ if (current()) setFailure({ key, message: errMessage(err) });
1679
1690
  return void 0;
1680
1691
  } finally {
1681
1692
  if (current()) {
@@ -1684,19 +1695,19 @@ var useWebLLMModel = (modelId, options) => {
1684
1695
  }
1685
1696
  }
1686
1697
  })();
1687
- inflightRef.current = { id, promise };
1698
+ inflightRef.current = { key, promise };
1688
1699
  return promise;
1689
- }, [modelId]);
1700
+ }, [modelId, key]);
1690
1701
  const unload = useCallback8(async () => {
1691
1702
  seqRef.current++;
1692
1703
  inflightRef.current = void 0;
1693
1704
  setPending(void 0);
1694
1705
  const previous = loadedRef.current;
1695
1706
  setHeld(void 0);
1696
- if (previous) await unloadWebLLMModel(previous.model);
1707
+ if (previous) await previous.free();
1697
1708
  }, []);
1698
- const model = loaded?.id === modelId ? loaded.model : void 0;
1699
- const progressing = pending?.id === modelId ? pending : void 0;
1709
+ const model = loaded?.key === key ? loaded.model : void 0;
1710
+ const progressing = pending?.key === key ? pending : void 0;
1700
1711
  return {
1701
1712
  model,
1702
1713
  load,
@@ -1705,9 +1716,46 @@ var useWebLLMModel = (modelId, options) => {
1705
1716
  loading: progressing !== void 0,
1706
1717
  progress: progressing?.progress ?? 0,
1707
1718
  text: progressing?.text ?? "",
1708
- error: failure?.id === modelId ? failure.message : void 0,
1709
- supported: isWebGPUAvailable(),
1710
- ready: model !== void 0
1719
+ error: failure?.key === key ? failure.message : void 0,
1720
+ supported: engine.supported ? engine.supported() : true,
1721
+ ready: model !== void 0,
1722
+ contextWindow: model ? engine.contextWindowOf?.(model) : void 0
1723
+ };
1724
+ };
1725
+
1726
+ // src/hooks/use-webllm-model.ts
1727
+ var useWebLLMModel = (modelId, options) => useLocalModel(modelId, createWebLLMEngine(options), {
1728
+ contextWindowTokens: options?.contextWindowTokens
1729
+ });
1730
+ var createWebLLMEngine = (options) => {
1731
+ const {
1732
+ create = createWebLLMModel,
1733
+ contextWindowTokens: _perLoad,
1734
+ ...modelOptions
1735
+ } = options ?? {};
1736
+ return {
1737
+ create: (id, ctx) => create(id, {
1738
+ ...modelOptions,
1739
+ ...ctx.contextWindowTokens ? { contextWindowTokens: ctx.contextWindowTokens } : {},
1740
+ // Drive preload in warmUp (below) so download progress is reported the
1741
+ // same way whether `create` is the core's `createWebLLMModel` or an
1742
+ // injected factory (e.g. a statically-imported `webLLM`, needed under
1743
+ // bundlers).
1744
+ preload: false,
1745
+ initProgressCallback: (report) => {
1746
+ ctx.onProgress({ progress: report.progress, text: report.text });
1747
+ modelOptions.initProgressCallback?.(report);
1748
+ }
1749
+ }),
1750
+ // Download the weights + init the engine now via the core's helper (a
1751
+ // 1-token warm-up). WebLLM builds are otherwise lazy — the ~GB download
1752
+ // would only start on the first `run()`, long after we told the UI the
1753
+ // model is "ready". Fast + idempotent once the weights are cached.
1754
+ warmUp: (model) => preloadWebLLMModel(model),
1755
+ unload: unloadWebLLMModel,
1756
+ supported: isWebGPUAvailable,
1757
+ unsupportedMessage: "WebGPU is not available in this browser.",
1758
+ contextWindowOf: webLLMContextWindow
1711
1759
  };
1712
1760
  };
1713
1761
 
@@ -3991,6 +4039,7 @@ export {
3991
4039
  createFileTools,
3992
4040
  createInitialAgentState,
3993
4041
  createSubagentTool,
4042
+ createWebLLMEngine,
3994
4043
  createWebLLMModel2 as createWebLLMModel,
3995
4044
  defaultLabels,
3996
4045
  defineSkill,
@@ -4021,6 +4070,7 @@ export {
4021
4070
  useChatHistory,
4022
4071
  useCredentials,
4023
4072
  useLabels,
4073
+ useLocalModel,
4024
4074
  useMcp,
4025
4075
  useMcpServers,
4026
4076
  useOptionalAgentContext,