@dudko.dev/agent-web-react 0.0.11 → 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
@@ -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]
@@ -1607,62 +1612,150 @@ var useMcpServers = (options = {}) => {
1607
1612
  };
1608
1613
 
1609
1614
  // src/hooks/use-webllm-model.ts
1610
- import { useCallback as useCallback8, useRef as useRef6, useState as useState8 } from "react";
1611
1615
  import {
1612
1616
  createWebLLMModel,
1613
1617
  isWebGPUAvailable,
1614
- preloadWebLLMModel
1618
+ preloadWebLLMModel,
1619
+ unloadWebLLMModel,
1620
+ webLLMContextWindow
1615
1621
  } from "@dudko.dev/agent-web";
1616
- 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);
1622
- const optionsRef = useRef6(options);
1623
- optionsRef.current = options;
1624
- const load = useCallback8(async () => {
1625
- if (!isWebGPUAvailable()) {
1626
- setError("WebGPU is not available in this browser.");
1627
- return void 0;
1628
- }
1629
- setLoading(true);
1630
- setError(void 0);
1631
- setProgress(0);
1632
- setText("");
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) => {
1627
+ const [loaded, setLoaded] = useState8(void 0);
1628
+ const [pending, setPending] = useState8();
1629
+ const [failure, setFailure] = useState8();
1630
+ const engineRef = useRef6(engine);
1631
+ engineRef.current = engine;
1632
+ const loadedRef = useRef6(void 0);
1633
+ const inflightRef = useRef6(
1634
+ void 0
1635
+ );
1636
+ const windowTokens = options?.contextWindowTokens;
1637
+ const key = keyOf(modelId, windowTokens);
1638
+ const seqRef = useRef6(0);
1639
+ const setHeld = (next) => {
1640
+ loadedRef.current = next;
1641
+ setLoaded(next);
1642
+ };
1643
+ const freeWith = (eng, model2) => async () => {
1633
1644
  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
- }
1645
+ await eng.unload?.(model2);
1646
+ } catch {
1647
+ }
1648
+ };
1649
+ const load = useCallback8(() => {
1650
+ const id = modelId;
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."
1646
1658
  });
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);
1659
+ return Promise.resolve(void 0);
1655
1660
  }
1656
- }, [modelId]);
1661
+ const seq = ++seqRef.current;
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
+ };
1669
+ const promise = (async () => {
1670
+ setPending({ key, progress: 0, text: "" });
1671
+ setFailure(void 0);
1672
+ let built;
1673
+ try {
1674
+ const previous = loadedRef.current;
1675
+ if (previous) {
1676
+ setHeld(void 0);
1677
+ await previous.free();
1678
+ }
1679
+ built = await eng.create(id, ctx);
1680
+ await eng.warmUp?.(built, ctx);
1681
+ if (!current()) {
1682
+ await freeWith(eng, built)();
1683
+ return void 0;
1684
+ }
1685
+ setHeld({ id, key, model: built, free: freeWith(eng, built) });
1686
+ return built;
1687
+ } catch (err) {
1688
+ if (built) await freeWith(eng, built)();
1689
+ if (current()) setFailure({ key, message: errMessage(err) });
1690
+ return void 0;
1691
+ } finally {
1692
+ if (current()) {
1693
+ inflightRef.current = void 0;
1694
+ setPending(void 0);
1695
+ }
1696
+ }
1697
+ })();
1698
+ inflightRef.current = { key, promise };
1699
+ return promise;
1700
+ }, [modelId, key]);
1701
+ const unload = useCallback8(async () => {
1702
+ seqRef.current++;
1703
+ inflightRef.current = void 0;
1704
+ setPending(void 0);
1705
+ const previous = loadedRef.current;
1706
+ setHeld(void 0);
1707
+ if (previous) await previous.free();
1708
+ }, []);
1709
+ const model = loaded?.key === key ? loaded.model : void 0;
1710
+ const progressing = pending?.key === key ? pending : void 0;
1657
1711
  return {
1658
1712
  model,
1659
1713
  load,
1660
- loading,
1661
- progress,
1662
- text,
1663
- error,
1664
- supported: isWebGPUAvailable(),
1665
- ready: model !== void 0
1714
+ unload,
1715
+ loadedModelId: loaded?.id,
1716
+ loading: progressing !== void 0,
1717
+ progress: progressing?.progress ?? 0,
1718
+ text: progressing?.text ?? "",
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
1666
1759
  };
1667
1760
  };
1668
1761
 
@@ -1846,7 +1939,13 @@ var defaultLabels = {
1846
1939
  readOnly: "Only read; refuse changes",
1847
1940
  think: (levels) => `Set thinking: ${levels}`
1848
1941
  },
1849
- thinkingLevels: { off: "Default", low: "Low", medium: "Medium", high: "High" },
1942
+ thinkingLevels: {
1943
+ off: "Default",
1944
+ none: "No thinking",
1945
+ low: "Low",
1946
+ medium: "Medium",
1947
+ high: "High"
1948
+ },
1850
1949
  filesEmpty: "No files yet. Attachments and files the agent writes appear here.",
1851
1950
  upload: "Upload",
1852
1951
  download: "Download",
@@ -2033,6 +2132,7 @@ var DownloadIcon = ({ size, className }) => /* @__PURE__ */ jsx3("svg", { ...bas
2033
2132
  import { Fragment, jsx as jsx4, jsxs as jsxs2 } from "react/jsx-runtime";
2034
2133
  var thinkingDefaults = (l) => [
2035
2134
  { id: "off", label: l.thinkingLevels.off },
2135
+ { id: "none", label: l.thinkingLevels.none },
2036
2136
  { id: "low", label: l.thinkingLevels.low },
2037
2137
  { id: "medium", label: l.thinkingLevels.medium },
2038
2138
  { id: "high", label: l.thinkingLevels.high }
@@ -3889,7 +3989,7 @@ import {
3889
3989
  markReadOnly,
3890
3990
  createWebLLMModel as createWebLLMModel2,
3891
3991
  preloadWebLLMModel as preloadWebLLMModel2,
3892
- unloadWebLLMModel,
3992
+ unloadWebLLMModel as unloadWebLLMModel2,
3893
3993
  isWebGPUAvailable as isWebGPUAvailable2,
3894
3994
  VaultCredentialStore as VaultCredentialStore2,
3895
3995
  MemoryCredentialStore,
@@ -3939,6 +4039,7 @@ export {
3939
4039
  createFileTools,
3940
4040
  createInitialAgentState,
3941
4041
  createSubagentTool,
4042
+ createWebLLMEngine,
3942
4043
  createWebLLMModel2 as createWebLLMModel,
3943
4044
  defaultLabels,
3944
4045
  defineSkill,
@@ -3962,13 +4063,14 @@ export {
3962
4063
  stripOAuthParams,
3963
4064
  supportsImages,
3964
4065
  supportsPdf,
3965
- unloadWebLLMModel,
4066
+ unloadWebLLMModel2 as unloadWebLLMModel,
3966
4067
  usageLine,
3967
4068
  useAgent,
3968
4069
  useAgentContext,
3969
4070
  useChatHistory,
3970
4071
  useCredentials,
3971
4072
  useLabels,
4073
+ useLocalModel,
3972
4074
  useMcp,
3973
4075
  useMcpServers,
3974
4076
  useOptionalAgentContext,