react-ai-renderer 0.1.15 → 0.1.16

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.cjs CHANGED
@@ -776,7 +776,6 @@ var MDXStreamingParser = /** @class */function () {
776
776
  // 这比手动转换 JavaScript 对象字面量为 JSON 更可靠
777
777
  props[propName] = new Function('return ' + rawValue)();
778
778
  } catch (e) {
779
- console.warn("Failed to parse prop value for ".concat(propName, ":"), value, e);
780
779
  // 如果 Function 构造器失败,尝试手动转换并解析为 JSON
781
780
  try {
782
781
  // 将 JavaScript 对象字面量转换为有效的 JSON
@@ -58467,7 +58466,7 @@ function renderMdx(mdxContent, scope, registeredComponents) {
58467
58466
  // 递归解析组件的函数 - 返回结构化对象
58468
58467
  function parseComponentRecursively(allComponentHandlers, item, scope) {
58469
58468
  return __awaiter(this, void 0, void 0, function () {
58470
- var componentHandler, props, propsString, childrenContent_1, parsedChildren_1, componentString, isComplete;
58469
+ var isComplete, componentHandler, props, propsString, childrenContent_1, parsedChildren_1, componentString;
58471
58470
  return __generator(this, function (_a) {
58472
58471
  switch (_a.label) {
58473
58472
  case 0:
@@ -58478,6 +58477,7 @@ function parseComponentRecursively(allComponentHandlers, item, scope) {
58478
58477
  }];
58479
58478
  }
58480
58479
  if (!(item.type === 'component')) return [3 /*break*/, 3];
58480
+ isComplete = item.isComplete === true;
58481
58481
  componentHandler = allComponentHandlers.find(function (c) {
58482
58482
  return c.name === item.value;
58483
58483
  });
@@ -58501,7 +58501,7 @@ function parseComponentRecursively(allComponentHandlers, item, scope) {
58501
58501
  _a.label = 2;
58502
58502
  case 2:
58503
58503
  componentString = (componentHandler === null || componentHandler === void 0 ? void 0 : componentHandler.selfClosing) ? "<".concat(item.value, " ").concat(propsString, " />") : ["<".concat(item.value, " ").concat(propsString, ">"), childrenContent_1, "</".concat(item.value, ">")].join('\n');
58504
- isComplete = item.isComplete === true;
58504
+ // 调用组件的渲染钩子
58505
58505
  if (isComplete) {
58506
58506
  // 渲染完成后的钩子
58507
58507
  if (componentHandler === null || componentHandler === void 0 ? void 0 : componentHandler.onRenderFinished) {
@@ -58628,15 +58628,17 @@ function MdxLayout(_a) {
58628
58628
  var ComponentPlaceholder = function (_a) {
58629
58629
  var componentName = _a.componentName,
58630
58630
  style = _a.style,
58631
- customPlaceholder = _a.customPlaceholder;
58632
- // 如果有自定义占位内容,优先使用
58633
- if (customPlaceholder) {
58631
+ loader = _a.loader;
58632
+ // 如果有自定义加载内容,优先使用
58633
+ if (loader) {
58634
+ // 如果是布尔值 true,使用默认样式
58635
+ if (typeof loader === 'boolean') ;
58634
58636
  // 如果是 React 元素,直接返回
58635
- if (/*#__PURE__*/React.isValidElement(customPlaceholder)) {
58636
- return customPlaceholder;
58637
+ else if (/*#__PURE__*/React.isValidElement(loader)) {
58638
+ return loader;
58637
58639
  }
58638
58640
  // 如果是字符串,显示字符串
58639
- if (typeof customPlaceholder === 'string') {
58641
+ else if (typeof loader === 'string') {
58640
58642
  return /*#__PURE__*/runtime.jsx("div", {
58641
58643
  style: __assign({
58642
58644
  border: '1px dashed #d1d5db',
@@ -58650,28 +58652,28 @@ var ComponentPlaceholder = function (_a) {
58650
58652
  justifyContent: 'center',
58651
58653
  minHeight: '60px'
58652
58654
  }, style),
58653
- children: customPlaceholder
58655
+ children: loader
58654
58656
  });
58655
58657
  }
58656
58658
  // 如果是函数,调用它
58657
- if (typeof customPlaceholder === 'function') {
58659
+ else if (typeof loader === 'function') {
58658
58660
  try {
58659
58661
  // 检查是否是 React 组件构造函数
58660
- var proto = customPlaceholder.prototype;
58662
+ var proto = loader.prototype;
58661
58663
  if (proto && (proto instanceof React.Component || proto.isReactComponent)) {
58662
58664
  // 是 React 组件类,创建元素
58663
- var Component = customPlaceholder;
58665
+ var Component = loader;
58664
58666
  return /*#__PURE__*/runtime.jsx(Component, {
58665
58667
  componentName: componentName
58666
58668
  });
58667
58669
  } else {
58668
58670
  // 是普通函数,调用它(类型断言为函数)
58669
- var func = customPlaceholder;
58671
+ var func = loader;
58670
58672
  return func(componentName);
58671
58673
  }
58672
58674
  } catch (e) {
58673
58675
  // 如果出错,尝试作为组件使用
58674
- var Component = customPlaceholder;
58676
+ var Component = loader;
58675
58677
  return /*#__PURE__*/runtime.jsx(Component, {
58676
58678
  componentName: componentName
58677
58679
  });
@@ -58767,6 +58769,51 @@ var FallbackView = /** @class */function (_super) {
58767
58769
  return FallbackView;
58768
58770
  }(React.Component);
58769
58771
 
58772
+ function isEnhancedComponentConfig(value) {
58773
+ return typeof value === 'object' && value !== null && 'value' in value && typeof value.value !== 'undefined';
58774
+ }
58775
+ /**
58776
+ * 从组件值中提取实际的组件
58777
+ */
58778
+ function extractComponent(value) {
58779
+ return isEnhancedComponentConfig(value) ? value.value : value;
58780
+ }
58781
+ /**
58782
+ * 将 components 转换为 ComponentHandler 数组
58783
+ */
58784
+ function convertComponentsToHandlers(components, componentHandlers) {
58785
+ return Object.entries(components).filter(function (_a) {
58786
+ var _b = __read(_a, 1),
58787
+ name = _b[0];
58788
+ return !componentHandlers.some(function (handler) {
58789
+ return handler.name === name;
58790
+ });
58791
+ }).map(function (_a) {
58792
+ var _b = __read(_a, 2),
58793
+ name = _b[0],
58794
+ componentValue = _b[1];
58795
+ if (isEnhancedComponentConfig(componentValue)) {
58796
+ // 增强模式:从配置对象中提取信息
58797
+ var config = componentValue;
58798
+ return {
58799
+ component: config.value,
58800
+ name: name,
58801
+ selfClosing: config.selfClosing !== undefined ? config.selfClosing : isSelfClosingComponent(config.value),
58802
+ onRenderFinished: config.onRenderFinished,
58803
+ onRenderProcess: config.onRenderProcess,
58804
+ loader: config.loader,
58805
+ label: config.label
58806
+ };
58807
+ } else {
58808
+ // 普通模式:直接使用组件
58809
+ return {
58810
+ component: componentValue,
58811
+ name: name,
58812
+ selfClosing: isSelfClosingComponent(componentValue)
58813
+ };
58814
+ }
58815
+ });
58816
+ }
58770
58817
  function ReactAIRenderer(_a) {
58771
58818
  var _this = this;
58772
58819
  var content = _a.content,
@@ -58789,24 +58836,16 @@ function ReactAIRenderer(_a) {
58789
58836
  });
58790
58837
  }
58791
58838
  content = content || children || '';
58792
- var allComponents = __assign(__assign({}, BuiltInComponents), components);
58793
- var allComponentHandlers = __spreadArray(__spreadArray([], __read(components ? Object.entries(components).filter(function (_a) {
58794
- var _b = __read(_a, 1),
58795
- name = _b[0];
58796
- return !componentHandlers.some(function (handler) {
58797
- return handler.name === name;
58798
- });
58799
- }).map(function (_a) {
58839
+ // 构建 allComponents:从增强配置中提取实际的组件
58840
+ var normalizedComponents = components ? Object.fromEntries(Object.entries(components).map(function (_a) {
58800
58841
  var _b = __read(_a, 2),
58801
58842
  name = _b[0],
58802
- component = _b[1];
58803
- var selfClosing = isSelfClosingComponent(component);
58804
- return {
58805
- component: component,
58806
- name: name,
58807
- selfClosing: selfClosing
58808
- };
58809
- }) : []), false), __read(componentHandlers), false);
58843
+ value = _b[1];
58844
+ return [name, extractComponent(value)];
58845
+ })) : {};
58846
+ var allComponents = __assign(__assign({}, BuiltInComponents), normalizedComponents);
58847
+ // 构建 allComponentHandlers:支持增强模式
58848
+ var allComponentHandlers = __spreadArray(__spreadArray([], __read(components ? convertComponentsToHandlers(components, componentHandlers) : []), false), __read(componentHandlers), false);
58810
58849
  var _e = __read(React.useState(null), 2),
58811
58850
  component = _e[0],
58812
58851
  setComponent = _e[1];
@@ -58814,6 +58853,8 @@ function ReactAIRenderer(_a) {
58814
58853
  var fallbackErrorRef = React.useRef({
58815
58854
  hasError: false
58816
58855
  });
58856
+ // 用于缓存已完成的组件,避免重复渲染
58857
+ var completedComponentsCacheRef = React.useRef(new Map());
58817
58858
  var parser = new MDXStreamingParser(allComponentHandlers);
58818
58859
  var mdxContent = content || '';
58819
58860
  // FallbackView 错误状态变化时的回调
@@ -58826,9 +58867,10 @@ function ReactAIRenderer(_a) {
58826
58867
  React.useEffect(function () {
58827
58868
  var parseMDX = function () {
58828
58869
  return __awaiter(_this, void 0, void 0, function () {
58829
- var ThinkComponent, ResultComponent, protectedContent, _a, thinkContent, resultContent, parsedData, parsedComponents, finalComponent, fallbackComponent, _loop_1, i, MDXComponent, parsedDataLength, lastItem;
58830
- return __generator(this, function (_b) {
58831
- switch (_b.label) {
58870
+ var ThinkComponent, ResultComponent, protectedContent, _a, thinkContent, resultContent, parsedData, parsedComponents, currentComponentCount, cacheKeys, cacheKeys_1, cacheKeys_1_1, key, indexMatch, index, finalComponent, fallbackComponent, _loop_1, i, MDXComponent, parsedDataLength, lastItem;
58871
+ var e_1, _b;
58872
+ return __generator(this, function (_c) {
58873
+ switch (_c.label) {
58832
58874
  case 0:
58833
58875
  ThinkComponent = null;
58834
58876
  ResultComponent = null;
@@ -58837,8 +58879,8 @@ function ReactAIRenderer(_a) {
58837
58879
  if (!thinkContent) return [3 /*break*/, 2];
58838
58880
  return [4 /*yield*/, renderMdx(thinkContent, scope, allComponents)];
58839
58881
  case 1:
58840
- ThinkComponent = _b.sent();
58841
- _b.label = 2;
58882
+ ThinkComponent = _c.sent();
58883
+ _c.label = 2;
58842
58884
  case 2:
58843
58885
  parsedData = [];
58844
58886
  if (!resultContent) return [3 /*break*/, 8];
@@ -58848,46 +58890,110 @@ function ReactAIRenderer(_a) {
58848
58890
  return parseComponentRecursively(allComponentHandlers, item, scope);
58849
58891
  }))];
58850
58892
  case 3:
58851
- parsedComponents = _b.sent();
58893
+ parsedComponents = _c.sent();
58894
+ currentComponentCount = parsedData.length;
58895
+ cacheKeys = Array.from(completedComponentsCacheRef.current.keys());
58896
+ try {
58897
+ for (cacheKeys_1 = __values(cacheKeys), cacheKeys_1_1 = cacheKeys_1.next(); !cacheKeys_1_1.done; cacheKeys_1_1 = cacheKeys_1.next()) {
58898
+ key = cacheKeys_1_1.value;
58899
+ indexMatch = key.match(/-(\d+)$/);
58900
+ if (indexMatch) {
58901
+ index = parseInt(indexMatch[1], 10);
58902
+ if (index >= currentComponentCount) {
58903
+ // 如果索引超出当前组件数量,删除缓存
58904
+ completedComponentsCacheRef.current.delete(key);
58905
+ }
58906
+ }
58907
+ }
58908
+ } catch (e_1_1) {
58909
+ e_1 = {
58910
+ error: e_1_1
58911
+ };
58912
+ } finally {
58913
+ try {
58914
+ if (cacheKeys_1_1 && !cacheKeys_1_1.done && (_b = cacheKeys_1.return)) _b.call(cacheKeys_1);
58915
+ } finally {
58916
+ if (e_1) throw e_1.error;
58917
+ }
58918
+ }
58852
58919
  finalComponent = [];
58853
58920
  fallbackComponent = [];
58854
58921
  _loop_1 = function (i) {
58855
- var item, _result, placeholderForFallback, componentNameMatch, componentName_1, componentHandler, displayName;
58856
- return __generator(this, function (_c) {
58857
- switch (_c.label) {
58922
+ var item, currentParsedItem, _result, placeholderForFallback, componentNameMatch, componentName_1, componentHandler, displayName, cacheKey;
58923
+ return __generator(this, function (_d) {
58924
+ switch (_d.label) {
58858
58925
  case 0:
58859
58926
  item = parsedComponents[i];
58927
+ currentParsedItem = parsedData[i];
58860
58928
  _result = void 0;
58861
58929
  placeholderForFallback = null;
58862
58930
  if (!(item.type === 'text')) return [3 /*break*/, 2];
58863
58931
  return [4 /*yield*/, renderText(item.value, scope, allComponents)];
58864
58932
  case 1:
58865
- _result = _c.sent();
58866
- return [3 /*break*/, 4];
58933
+ _result = _d.sent();
58934
+ finalComponent.push(_result);
58935
+ return [3 /*break*/, 11];
58867
58936
  case 2:
58868
- return [4 /*yield*/, renderMdx(item.value, scope, allComponents)];
58869
- case 3:
58870
- _result = _c.sent();
58871
58937
  componentNameMatch = item.value.match(/<([A-Z][A-Za-z0-9]*)/);
58872
- if (componentNameMatch) {
58873
- componentName_1 = componentNameMatch[1];
58874
- componentHandler = allComponentHandlers.find(function (c) {
58875
- return c.name === componentName_1;
58876
- });
58877
- displayName = componentName_1;
58878
- if (componentHandler === null || componentHandler === void 0 ? void 0 : componentHandler.label) {
58879
- displayName = componentHandler === null || componentHandler === void 0 ? void 0 : componentHandler.label;
58880
- }
58881
- placeholderForFallback = /*#__PURE__*/React.createElement(ComponentPlaceholder, {
58882
- componentName: displayName,
58883
- customPlaceholder: componentHandler === null || componentHandler === void 0 ? void 0 : componentHandler.placeholder,
58884
- key: "fallback-placeholder-".concat(i)
58885
- });
58938
+ if (!componentNameMatch) return [3 /*break*/, 9];
58939
+ componentName_1 = componentNameMatch[1];
58940
+ componentHandler = allComponentHandlers.find(function (c) {
58941
+ return c.name === componentName_1;
58942
+ });
58943
+ displayName = componentName_1;
58944
+ if (componentHandler === null || componentHandler === void 0 ? void 0 : componentHandler.label) {
58945
+ displayName = componentHandler === null || componentHandler === void 0 ? void 0 : componentHandler.label;
58886
58946
  }
58887
- _c.label = 4;
58947
+ placeholderForFallback = /*#__PURE__*/React.createElement(ComponentPlaceholder, {
58948
+ componentName: displayName,
58949
+ loader: componentHandler === null || componentHandler === void 0 ? void 0 : componentHandler.loader,
58950
+ key: "fallback-loader-".concat(componentName_1)
58951
+ });
58952
+ if (!(currentParsedItem.isComplete === true || !(componentHandler === null || componentHandler === void 0 ? void 0 : componentHandler.loader))) return [3 /*break*/, 6];
58953
+ cacheKey = "mdx-component-".concat(componentName_1, "-").concat(i);
58954
+ if (!completedComponentsCacheRef.current.has(cacheKey)) return [3 /*break*/, 3];
58955
+ // 使用缓存的组件
58956
+ _result = completedComponentsCacheRef.current.get(cacheKey);
58957
+ return [3 /*break*/, 5];
58958
+ case 3:
58959
+ return [4 /*yield*/, renderMdx(item.value, scope, allComponents)];
58888
58960
  case 4:
58961
+ // 缓存不存在,调用 renderMdx 并存入缓存
58962
+ _result = _d.sent();
58963
+ // 为组件设置 key,确保 React 能正确识别
58964
+ if (/*#__PURE__*/React.isValidElement(_result)) {
58965
+ _result = /*#__PURE__*/React.cloneElement(_result, {
58966
+ key: cacheKey
58967
+ });
58968
+ } else {
58969
+ _result = /*#__PURE__*/React.createElement(React.Fragment, {
58970
+ key: cacheKey
58971
+ }, _result);
58972
+ }
58973
+ // 存入缓存
58974
+ completedComponentsCacheRef.current.set(cacheKey, _result);
58975
+ _d.label = 5;
58976
+ case 5:
58977
+ finalComponent.push(_result);
58978
+ return [3 /*break*/, 8];
58979
+ case 6:
58980
+ return [4 /*yield*/, renderMdx(item.value, scope, allComponents)];
58981
+ case 7:
58982
+ // 组件未完成,正常渲染
58983
+ _result = _d.sent();
58984
+ finalComponent.push(placeholderForFallback);
58985
+ _d.label = 8;
58986
+ case 8:
58987
+ return [3 /*break*/, 11];
58988
+ case 9:
58989
+ return [4 /*yield*/, renderMdx(item.value, scope, allComponents)];
58990
+ case 10:
58991
+ // 没有匹配到组件名,正常渲染
58992
+ _result = _d.sent();
58889
58993
  finalComponent.push(_result);
58890
- // fallbackComponent 应该是 finalComponent 的前 i 个元素 + 当前 item 的 placeholder
58994
+ _d.label = 11;
58995
+ case 11:
58996
+ // fallbackComponent 应该是 finalComponent 的前 i 个元素 + 当前 item 的 loader
58891
58997
  fallbackComponent.length = 0;
58892
58998
  fallbackComponent.push.apply(fallbackComponent, __spreadArray([], __read(finalComponent.slice(0, i)), false));
58893
58999
  if (placeholderForFallback) {
@@ -58898,13 +59004,13 @@ function ReactAIRenderer(_a) {
58898
59004
  });
58899
59005
  };
58900
59006
  i = 0;
58901
- _b.label = 4;
59007
+ _c.label = 4;
58902
59008
  case 4:
58903
59009
  if (!(i < parsedComponents.length)) return [3 /*break*/, 7];
58904
59010
  return [5 /*yield**/, _loop_1(i)];
58905
59011
  case 5:
58906
- _b.sent();
58907
- _b.label = 6;
59012
+ _c.sent();
59013
+ _c.label = 6;
58908
59014
  case 6:
58909
59015
  i++;
58910
59016
  return [3 /*break*/, 4];
@@ -58919,7 +59025,7 @@ function ReactAIRenderer(_a) {
58919
59025
  children: finalComponent
58920
59026
  })
58921
59027
  });
58922
- _b.label = 8;
59028
+ _c.label = 8;
58923
59029
  case 8:
58924
59030
  MDXComponent = /*#__PURE__*/runtime.jsxs(React.Fragment, {
58925
59031
  children: [ThinkComponent, ResultComponent]