react-ai-renderer 0.1.14 → 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.d.ts CHANGED
@@ -20,11 +20,28 @@ interface ComponentHandler extends BaseComponentHandler {
20
20
  onRenderFinished?: (item: ComponentData$1, scope: any) => void;
21
21
  /** @deprecated 使用 onRenderProcess 替代 */
22
22
  onRender?: (item: ComponentData$1, scope: any) => void;
23
- /** 自定义占位组件,可以是 React 组件、React 元素、字符串或返回 React 元素的函数 */
24
- placeholder?: React.ComponentType<any> | React.ReactElement | string | ((componentName: string) => React.ReactElement);
23
+ /** 自定义加载组件,可以是 React 组件、React 元素、字符串、布尔值或返回 React 元素的函数 */
24
+ loader?: React.ComponentType<any> | React.ReactElement | string | boolean | ((componentName: string) => React.ReactElement);
25
25
  /** 组件的显示标签,用于在占位组件中显示 */
26
26
  label?: string;
27
27
  }
28
+ /** 增强模式的组件配置 */
29
+ interface EnhancedComponentConfig {
30
+ /** 组件本身 */
31
+ value: React.ComponentType<any>;
32
+ /** 组件的显示标签 */
33
+ label?: string;
34
+ /** 自定义加载组件,可以是 React 组件、React 元素、字符串、布尔值或返回 React 元素的函数 */
35
+ loader?: React.ComponentType<any> | React.ReactElement | string | boolean | ((componentName: string) => React.ReactElement);
36
+ /** 组件渲染完成后的钩子 */
37
+ onRenderFinished?: (item: ComponentData$1, scope: any) => void;
38
+ /** 组件渲染过程中的钩子 */
39
+ onRenderProcess?: (item: ComponentData$1, scope: any) => void;
40
+ /** 是否自闭合 */
41
+ selfClosing?: boolean;
42
+ }
43
+ /** 组件值类型:可以是组件本身,也可以是增强配置对象 */
44
+ type ComponentValue = React.ComponentType<any> | EnhancedComponentConfig;
28
45
  interface ParserState {
29
46
  position: number;
30
47
  stack: ComponentData$1[];
@@ -49,8 +66,8 @@ interface ReactAIRendererProps {
49
66
  content?: string;
50
67
  /** 作用域对象,用于传递变量和函数给组件 */
51
68
  scope?: Record<string, any>;
52
- /** 自定义组件映射 */
53
- components?: Record<string, React.ComponentType<any>>;
69
+ /** 自定义组件映射,支持普通模式(组件)和增强模式(配置对象) */
70
+ components?: Record<string, ComponentValue>;
54
71
  /** 子元素,作为 content 的替代方案 */
55
72
  children?: string;
56
73
  /** 组件处理器数组 */
@@ -121,4 +138,4 @@ declare class MDXStreamingParser {
121
138
 
122
139
  declare function ReactAIRenderer({ content, scope, components, children, componentHandlers, useGithubStyles, mdxLayoutClassName, mdxLayoutStyle }: ReactAIRendererProps): React.JSX.Element;
123
140
 
124
- export { ComponentData$1 as ComponentData, ComponentHandler, MDXStreamingParser, MdxLayout, MdxLayoutProps, ParseResult, ParserState, ReactAIRendererProps, ReactMarkdownProps, Think, ThinkProps, ToolCall, ToolCallProps, ReactAIRenderer as default };
141
+ export { ComponentData$1 as ComponentData, ComponentHandler, ComponentValue, EnhancedComponentConfig, MDXStreamingParser, MdxLayout, MdxLayoutProps, ParseResult, ParserState, ReactAIRendererProps, ReactMarkdownProps, Think, ThinkProps, ToolCall, ToolCallProps, ReactAIRenderer as default };
package/dist/index.js CHANGED
@@ -754,7 +754,6 @@ var MDXStreamingParser = /** @class */function () {
754
754
  // 这比手动转换 JavaScript 对象字面量为 JSON 更可靠
755
755
  props[propName] = new Function('return ' + rawValue)();
756
756
  } catch (e) {
757
- console.warn("Failed to parse prop value for ".concat(propName, ":"), value, e);
758
757
  // 如果 Function 构造器失败,尝试手动转换并解析为 JSON
759
758
  try {
760
759
  // 将 JavaScript 对象字面量转换为有效的 JSON
@@ -58367,7 +58366,8 @@ function preprocessMdxContent(content) {
58367
58366
  // 将其他自闭合 HTML 标签转换为 JSX 格式
58368
58367
  .replace(/<(hr|img|input)([^>]*)(?<!\/)>/gi, '<$1$2 />');
58369
58368
  // 转义非标准格式的内容
58370
- return processedContent.replace(/<(?!\/|[A-Z][a-zA-Z]*\b|[a-z][a-zA-Z0-9]*\b)/g, '&lt;');
58369
+ // 支持以下划线开头的组件名(如 _THINK)
58370
+ return processedContent.replace(/<(?!\/|[A-Z_][a-zA-Z0-9_]*\b|[a-z][a-zA-Z0-9]*\b)/g, '&lt;');
58371
58371
  }
58372
58372
  // 辅助函数:格式化props
58373
58373
  function formatProps(props) {
@@ -58444,7 +58444,7 @@ function renderMdx(mdxContent, scope, registeredComponents) {
58444
58444
  // 递归解析组件的函数 - 返回结构化对象
58445
58445
  function parseComponentRecursively(allComponentHandlers, item, scope) {
58446
58446
  return __awaiter(this, void 0, void 0, function () {
58447
- var componentHandler, props, propsString, childrenContent_1, parsedChildren_1, componentString, isComplete;
58447
+ var isComplete, componentHandler, props, propsString, childrenContent_1, parsedChildren_1, componentString;
58448
58448
  return __generator(this, function (_a) {
58449
58449
  switch (_a.label) {
58450
58450
  case 0:
@@ -58455,6 +58455,7 @@ function parseComponentRecursively(allComponentHandlers, item, scope) {
58455
58455
  }];
58456
58456
  }
58457
58457
  if (!(item.type === 'component')) return [3 /*break*/, 3];
58458
+ isComplete = item.isComplete === true;
58458
58459
  componentHandler = allComponentHandlers.find(function (c) {
58459
58460
  return c.name === item.value;
58460
58461
  });
@@ -58478,7 +58479,7 @@ function parseComponentRecursively(allComponentHandlers, item, scope) {
58478
58479
  _a.label = 2;
58479
58480
  case 2:
58480
58481
  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');
58481
- isComplete = item.isComplete === true;
58482
+ // 调用组件的渲染钩子
58482
58483
  if (isComplete) {
58483
58484
  // 渲染完成后的钩子
58484
58485
  if (componentHandler === null || componentHandler === void 0 ? void 0 : componentHandler.onRenderFinished) {
@@ -58605,15 +58606,17 @@ function MdxLayout(_a) {
58605
58606
  var ComponentPlaceholder = function (_a) {
58606
58607
  var componentName = _a.componentName,
58607
58608
  style = _a.style,
58608
- customPlaceholder = _a.customPlaceholder;
58609
- // 如果有自定义占位内容,优先使用
58610
- if (customPlaceholder) {
58609
+ loader = _a.loader;
58610
+ // 如果有自定义加载内容,优先使用
58611
+ if (loader) {
58612
+ // 如果是布尔值 true,使用默认样式
58613
+ if (typeof loader === 'boolean') ;
58611
58614
  // 如果是 React 元素,直接返回
58612
- if (/*#__PURE__*/React__default.isValidElement(customPlaceholder)) {
58613
- return customPlaceholder;
58615
+ else if (/*#__PURE__*/React__default.isValidElement(loader)) {
58616
+ return loader;
58614
58617
  }
58615
58618
  // 如果是字符串,显示字符串
58616
- if (typeof customPlaceholder === 'string') {
58619
+ else if (typeof loader === 'string') {
58617
58620
  return /*#__PURE__*/jsx("div", {
58618
58621
  style: __assign({
58619
58622
  border: '1px dashed #d1d5db',
@@ -58627,28 +58630,28 @@ var ComponentPlaceholder = function (_a) {
58627
58630
  justifyContent: 'center',
58628
58631
  minHeight: '60px'
58629
58632
  }, style),
58630
- children: customPlaceholder
58633
+ children: loader
58631
58634
  });
58632
58635
  }
58633
58636
  // 如果是函数,调用它
58634
- if (typeof customPlaceholder === 'function') {
58637
+ else if (typeof loader === 'function') {
58635
58638
  try {
58636
58639
  // 检查是否是 React 组件构造函数
58637
- var proto = customPlaceholder.prototype;
58640
+ var proto = loader.prototype;
58638
58641
  if (proto && (proto instanceof React__default.Component || proto.isReactComponent)) {
58639
58642
  // 是 React 组件类,创建元素
58640
- var Component = customPlaceholder;
58643
+ var Component = loader;
58641
58644
  return /*#__PURE__*/jsx(Component, {
58642
58645
  componentName: componentName
58643
58646
  });
58644
58647
  } else {
58645
58648
  // 是普通函数,调用它(类型断言为函数)
58646
- var func = customPlaceholder;
58649
+ var func = loader;
58647
58650
  return func(componentName);
58648
58651
  }
58649
58652
  } catch (e) {
58650
58653
  // 如果出错,尝试作为组件使用
58651
- var Component = customPlaceholder;
58654
+ var Component = loader;
58652
58655
  return /*#__PURE__*/jsx(Component, {
58653
58656
  componentName: componentName
58654
58657
  });
@@ -58744,6 +58747,51 @@ var FallbackView = /** @class */function (_super) {
58744
58747
  return FallbackView;
58745
58748
  }(React__default.Component);
58746
58749
 
58750
+ function isEnhancedComponentConfig(value) {
58751
+ return typeof value === 'object' && value !== null && 'value' in value && typeof value.value !== 'undefined';
58752
+ }
58753
+ /**
58754
+ * 从组件值中提取实际的组件
58755
+ */
58756
+ function extractComponent(value) {
58757
+ return isEnhancedComponentConfig(value) ? value.value : value;
58758
+ }
58759
+ /**
58760
+ * 将 components 转换为 ComponentHandler 数组
58761
+ */
58762
+ function convertComponentsToHandlers(components, componentHandlers) {
58763
+ return Object.entries(components).filter(function (_a) {
58764
+ var _b = __read(_a, 1),
58765
+ name = _b[0];
58766
+ return !componentHandlers.some(function (handler) {
58767
+ return handler.name === name;
58768
+ });
58769
+ }).map(function (_a) {
58770
+ var _b = __read(_a, 2),
58771
+ name = _b[0],
58772
+ componentValue = _b[1];
58773
+ if (isEnhancedComponentConfig(componentValue)) {
58774
+ // 增强模式:从配置对象中提取信息
58775
+ var config = componentValue;
58776
+ return {
58777
+ component: config.value,
58778
+ name: name,
58779
+ selfClosing: config.selfClosing !== undefined ? config.selfClosing : isSelfClosingComponent(config.value),
58780
+ onRenderFinished: config.onRenderFinished,
58781
+ onRenderProcess: config.onRenderProcess,
58782
+ loader: config.loader,
58783
+ label: config.label
58784
+ };
58785
+ } else {
58786
+ // 普通模式:直接使用组件
58787
+ return {
58788
+ component: componentValue,
58789
+ name: name,
58790
+ selfClosing: isSelfClosingComponent(componentValue)
58791
+ };
58792
+ }
58793
+ });
58794
+ }
58747
58795
  function ReactAIRenderer(_a) {
58748
58796
  var _this = this;
58749
58797
  var content = _a.content,
@@ -58766,24 +58814,16 @@ function ReactAIRenderer(_a) {
58766
58814
  });
58767
58815
  }
58768
58816
  content = content || children || '';
58769
- var allComponents = __assign(__assign({}, BuiltInComponents), components);
58770
- var allComponentHandlers = __spreadArray(__spreadArray([], __read(components ? Object.entries(components).filter(function (_a) {
58771
- var _b = __read(_a, 1),
58772
- name = _b[0];
58773
- return !componentHandlers.some(function (handler) {
58774
- return handler.name === name;
58775
- });
58776
- }).map(function (_a) {
58817
+ // 构建 allComponents:从增强配置中提取实际的组件
58818
+ var normalizedComponents = components ? Object.fromEntries(Object.entries(components).map(function (_a) {
58777
58819
  var _b = __read(_a, 2),
58778
58820
  name = _b[0],
58779
- component = _b[1];
58780
- var selfClosing = isSelfClosingComponent(component);
58781
- return {
58782
- component: component,
58783
- name: name,
58784
- selfClosing: selfClosing
58785
- };
58786
- }) : []), false), __read(componentHandlers), false);
58821
+ value = _b[1];
58822
+ return [name, extractComponent(value)];
58823
+ })) : {};
58824
+ var allComponents = __assign(__assign({}, BuiltInComponents), normalizedComponents);
58825
+ // 构建 allComponentHandlers:支持增强模式
58826
+ var allComponentHandlers = __spreadArray(__spreadArray([], __read(components ? convertComponentsToHandlers(components, componentHandlers) : []), false), __read(componentHandlers), false);
58787
58827
  var _e = __read(useState(null), 2),
58788
58828
  component = _e[0],
58789
58829
  setComponent = _e[1];
@@ -58791,6 +58831,8 @@ function ReactAIRenderer(_a) {
58791
58831
  var fallbackErrorRef = useRef({
58792
58832
  hasError: false
58793
58833
  });
58834
+ // 用于缓存已完成的组件,避免重复渲染
58835
+ var completedComponentsCacheRef = useRef(new Map());
58794
58836
  var parser = new MDXStreamingParser(allComponentHandlers);
58795
58837
  var mdxContent = content || '';
58796
58838
  // FallbackView 错误状态变化时的回调
@@ -58803,9 +58845,10 @@ function ReactAIRenderer(_a) {
58803
58845
  useEffect(function () {
58804
58846
  var parseMDX = function () {
58805
58847
  return __awaiter(_this, void 0, void 0, function () {
58806
- var ThinkComponent, ResultComponent, protectedContent, _a, thinkContent, resultContent, parsedData, parsedComponents, finalComponent, fallbackComponent, _loop_1, i, MDXComponent, parsedDataLength, lastItem;
58807
- return __generator(this, function (_b) {
58808
- switch (_b.label) {
58848
+ 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;
58849
+ var e_1, _b;
58850
+ return __generator(this, function (_c) {
58851
+ switch (_c.label) {
58809
58852
  case 0:
58810
58853
  ThinkComponent = null;
58811
58854
  ResultComponent = null;
@@ -58814,8 +58857,8 @@ function ReactAIRenderer(_a) {
58814
58857
  if (!thinkContent) return [3 /*break*/, 2];
58815
58858
  return [4 /*yield*/, renderMdx(thinkContent, scope, allComponents)];
58816
58859
  case 1:
58817
- ThinkComponent = _b.sent();
58818
- _b.label = 2;
58860
+ ThinkComponent = _c.sent();
58861
+ _c.label = 2;
58819
58862
  case 2:
58820
58863
  parsedData = [];
58821
58864
  if (!resultContent) return [3 /*break*/, 8];
@@ -58825,46 +58868,110 @@ function ReactAIRenderer(_a) {
58825
58868
  return parseComponentRecursively(allComponentHandlers, item, scope);
58826
58869
  }))];
58827
58870
  case 3:
58828
- parsedComponents = _b.sent();
58871
+ parsedComponents = _c.sent();
58872
+ currentComponentCount = parsedData.length;
58873
+ cacheKeys = Array.from(completedComponentsCacheRef.current.keys());
58874
+ try {
58875
+ for (cacheKeys_1 = __values(cacheKeys), cacheKeys_1_1 = cacheKeys_1.next(); !cacheKeys_1_1.done; cacheKeys_1_1 = cacheKeys_1.next()) {
58876
+ key = cacheKeys_1_1.value;
58877
+ indexMatch = key.match(/-(\d+)$/);
58878
+ if (indexMatch) {
58879
+ index = parseInt(indexMatch[1], 10);
58880
+ if (index >= currentComponentCount) {
58881
+ // 如果索引超出当前组件数量,删除缓存
58882
+ completedComponentsCacheRef.current.delete(key);
58883
+ }
58884
+ }
58885
+ }
58886
+ } catch (e_1_1) {
58887
+ e_1 = {
58888
+ error: e_1_1
58889
+ };
58890
+ } finally {
58891
+ try {
58892
+ if (cacheKeys_1_1 && !cacheKeys_1_1.done && (_b = cacheKeys_1.return)) _b.call(cacheKeys_1);
58893
+ } finally {
58894
+ if (e_1) throw e_1.error;
58895
+ }
58896
+ }
58829
58897
  finalComponent = [];
58830
58898
  fallbackComponent = [];
58831
58899
  _loop_1 = function (i) {
58832
- var item, _result, placeholderForFallback, componentNameMatch, componentName_1, componentHandler, displayName;
58833
- return __generator(this, function (_c) {
58834
- switch (_c.label) {
58900
+ var item, currentParsedItem, _result, placeholderForFallback, componentNameMatch, componentName_1, componentHandler, displayName, cacheKey;
58901
+ return __generator(this, function (_d) {
58902
+ switch (_d.label) {
58835
58903
  case 0:
58836
58904
  item = parsedComponents[i];
58905
+ currentParsedItem = parsedData[i];
58837
58906
  _result = void 0;
58838
58907
  placeholderForFallback = null;
58839
58908
  if (!(item.type === 'text')) return [3 /*break*/, 2];
58840
58909
  return [4 /*yield*/, renderText(item.value, scope, allComponents)];
58841
58910
  case 1:
58842
- _result = _c.sent();
58843
- return [3 /*break*/, 4];
58911
+ _result = _d.sent();
58912
+ finalComponent.push(_result);
58913
+ return [3 /*break*/, 11];
58844
58914
  case 2:
58845
- return [4 /*yield*/, renderMdx(item.value, scope, allComponents)];
58846
- case 3:
58847
- _result = _c.sent();
58848
58915
  componentNameMatch = item.value.match(/<([A-Z][A-Za-z0-9]*)/);
58849
- if (componentNameMatch) {
58850
- componentName_1 = componentNameMatch[1];
58851
- componentHandler = allComponentHandlers.find(function (c) {
58852
- return c.name === componentName_1;
58853
- });
58854
- displayName = componentName_1;
58855
- if (componentHandler === null || componentHandler === void 0 ? void 0 : componentHandler.label) {
58856
- displayName = componentHandler === null || componentHandler === void 0 ? void 0 : componentHandler.label;
58857
- }
58858
- placeholderForFallback = /*#__PURE__*/React__default.createElement(ComponentPlaceholder, {
58859
- componentName: displayName,
58860
- customPlaceholder: componentHandler === null || componentHandler === void 0 ? void 0 : componentHandler.placeholder,
58861
- key: "fallback-placeholder-".concat(i)
58862
- });
58916
+ if (!componentNameMatch) return [3 /*break*/, 9];
58917
+ componentName_1 = componentNameMatch[1];
58918
+ componentHandler = allComponentHandlers.find(function (c) {
58919
+ return c.name === componentName_1;
58920
+ });
58921
+ displayName = componentName_1;
58922
+ if (componentHandler === null || componentHandler === void 0 ? void 0 : componentHandler.label) {
58923
+ displayName = componentHandler === null || componentHandler === void 0 ? void 0 : componentHandler.label;
58863
58924
  }
58864
- _c.label = 4;
58925
+ placeholderForFallback = /*#__PURE__*/React__default.createElement(ComponentPlaceholder, {
58926
+ componentName: displayName,
58927
+ loader: componentHandler === null || componentHandler === void 0 ? void 0 : componentHandler.loader,
58928
+ key: "fallback-loader-".concat(componentName_1)
58929
+ });
58930
+ if (!(currentParsedItem.isComplete === true || !(componentHandler === null || componentHandler === void 0 ? void 0 : componentHandler.loader))) return [3 /*break*/, 6];
58931
+ cacheKey = "mdx-component-".concat(componentName_1, "-").concat(i);
58932
+ if (!completedComponentsCacheRef.current.has(cacheKey)) return [3 /*break*/, 3];
58933
+ // 使用缓存的组件
58934
+ _result = completedComponentsCacheRef.current.get(cacheKey);
58935
+ return [3 /*break*/, 5];
58936
+ case 3:
58937
+ return [4 /*yield*/, renderMdx(item.value, scope, allComponents)];
58865
58938
  case 4:
58939
+ // 缓存不存在,调用 renderMdx 并存入缓存
58940
+ _result = _d.sent();
58941
+ // 为组件设置 key,确保 React 能正确识别
58942
+ if (/*#__PURE__*/React__default.isValidElement(_result)) {
58943
+ _result = /*#__PURE__*/React__default.cloneElement(_result, {
58944
+ key: cacheKey
58945
+ });
58946
+ } else {
58947
+ _result = /*#__PURE__*/React__default.createElement(React__default.Fragment, {
58948
+ key: cacheKey
58949
+ }, _result);
58950
+ }
58951
+ // 存入缓存
58952
+ completedComponentsCacheRef.current.set(cacheKey, _result);
58953
+ _d.label = 5;
58954
+ case 5:
58955
+ finalComponent.push(_result);
58956
+ return [3 /*break*/, 8];
58957
+ case 6:
58958
+ return [4 /*yield*/, renderMdx(item.value, scope, allComponents)];
58959
+ case 7:
58960
+ // 组件未完成,正常渲染
58961
+ _result = _d.sent();
58962
+ finalComponent.push(placeholderForFallback);
58963
+ _d.label = 8;
58964
+ case 8:
58965
+ return [3 /*break*/, 11];
58966
+ case 9:
58967
+ return [4 /*yield*/, renderMdx(item.value, scope, allComponents)];
58968
+ case 10:
58969
+ // 没有匹配到组件名,正常渲染
58970
+ _result = _d.sent();
58866
58971
  finalComponent.push(_result);
58867
- // fallbackComponent 应该是 finalComponent 的前 i 个元素 + 当前 item 的 placeholder
58972
+ _d.label = 11;
58973
+ case 11:
58974
+ // fallbackComponent 应该是 finalComponent 的前 i 个元素 + 当前 item 的 loader
58868
58975
  fallbackComponent.length = 0;
58869
58976
  fallbackComponent.push.apply(fallbackComponent, __spreadArray([], __read(finalComponent.slice(0, i)), false));
58870
58977
  if (placeholderForFallback) {
@@ -58875,13 +58982,13 @@ function ReactAIRenderer(_a) {
58875
58982
  });
58876
58983
  };
58877
58984
  i = 0;
58878
- _b.label = 4;
58985
+ _c.label = 4;
58879
58986
  case 4:
58880
58987
  if (!(i < parsedComponents.length)) return [3 /*break*/, 7];
58881
58988
  return [5 /*yield**/, _loop_1(i)];
58882
58989
  case 5:
58883
- _b.sent();
58884
- _b.label = 6;
58990
+ _c.sent();
58991
+ _c.label = 6;
58885
58992
  case 6:
58886
58993
  i++;
58887
58994
  return [3 /*break*/, 4];
@@ -58896,7 +59003,7 @@ function ReactAIRenderer(_a) {
58896
59003
  children: finalComponent
58897
59004
  })
58898
59005
  });
58899
- _b.label = 8;
59006
+ _c.label = 8;
58900
59007
  case 8:
58901
59008
  MDXComponent = /*#__PURE__*/jsxs(React__default.Fragment, {
58902
59009
  children: [ThinkComponent, ResultComponent]