@ant-design/agentic-ui 2.28.2 → 2.28.5

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.
@@ -214,9 +214,7 @@ import { AceEditor, AceEditorContainer, CodeContainer, CodeToolbar, HtmlPreview,
214
214
  }
215
215
  // 只读模式下的思考块特殊渲染
216
216
  if (shouldRenderAsThinkBlock) {
217
- return /*#__PURE__*/ React.createElement(ThinkBlock, {
218
- element: props.element
219
- });
217
+ return /*#__PURE__*/ React.createElement(ThinkBlock, props);
220
218
  }
221
219
  // 主要的代码编辑器渲染
222
220
  if (shouldRenderAsCodeEditor) {
@@ -3,9 +3,35 @@
3
3
  * 只读模式下渲染思考类型的代码块
4
4
  */
5
5
  import React from 'react';
6
- import { CodeNode } from '../../../MarkdownEditor/el';
7
- interface ThinkBlockProps {
8
- element: CodeNode;
6
+ import { CodeNode, ElementProps } from '../../../MarkdownEditor/el';
7
+ type ThinkBlockProps = ElementProps<CodeNode>;
8
+ /**
9
+ * ThinkBlock Context 类型定义
10
+ */
11
+ export interface ThinkBlockContextType {
12
+ /** 受控的展开状态 */
13
+ expanded?: boolean;
14
+ /** 展开状态变更回调 */
15
+ onExpandedChange?: (expanded: boolean) => void;
9
16
  }
10
- export declare function ThinkBlock({ element }: ThinkBlockProps): React.JSX.Element;
17
+ /**
18
+ * ThinkBlock Context
19
+ */
20
+ export declare const ThinkBlockContext: React.Context<ThinkBlockContextType | null>;
21
+ /**
22
+ * ThinkBlock Provider 组件
23
+ *
24
+ * 用于全局控制所有 ThinkBlock 实例的展开/收起状态
25
+ *
26
+ * @example
27
+ * ```tsx
28
+ * <ThinkBlockProvider expanded={isExpanded} onExpandedChange={setIsExpanded}>
29
+ * <MarkdownEditor ... />
30
+ * </ThinkBlockProvider>
31
+ * ```
32
+ */
33
+ export declare const ThinkBlockProvider: React.FC<ThinkBlockContextType & {
34
+ children: React.ReactNode;
35
+ }>;
36
+ export declare function ThinkBlock(props: ThinkBlockProps): React.JSX.Element;
11
37
  export {};
@@ -48,11 +48,35 @@ function _unsupported_iterable_to_array(o, minLen) {
48
48
  if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
49
49
  }
50
50
  import { useMergedState } from "rc-util";
51
- import React, { useContext, useEffect } from "react";
51
+ import React, { createContext, useContext, useEffect, useMemo, useRef } from "react";
52
52
  import { MessagesContext } from "../../../Bubble/MessagesContent/BubbleContext";
53
53
  import { I18nContext } from "../../../I18n";
54
- import { EditorStoreContext } from "../../../MarkdownEditor/editor/store";
54
+ import { EditorStoreContext, useEditorStore } from "../../../MarkdownEditor/editor/store";
55
+ import { EditorUtils } from "../../../MarkdownEditor/editor/utils/editorUtils";
55
56
  import { ToolUseBarThink } from "../../../ToolUseBarThink";
57
+ /**
58
+ * ThinkBlock Context
59
+ */ export var ThinkBlockContext = /*#__PURE__*/ createContext(null);
60
+ /**
61
+ * ThinkBlock Provider 组件
62
+ *
63
+ * 用于全局控制所有 ThinkBlock 实例的展开/收起状态
64
+ *
65
+ * @example
66
+ * ```tsx
67
+ * <ThinkBlockProvider expanded={isExpanded} onExpandedChange={setIsExpanded}>
68
+ * <MarkdownEditor ... />
69
+ * </ThinkBlockProvider>
70
+ * ```
71
+ */ export var ThinkBlockProvider = function(param) {
72
+ var expanded = param.expanded, onExpandedChange = param.onExpandedChange, children = param.children;
73
+ return /*#__PURE__*/ React.createElement(ThinkBlockContext.Provider, {
74
+ value: {
75
+ expanded: expanded,
76
+ onExpandedChange: onExpandedChange
77
+ }
78
+ }, children);
79
+ };
56
80
  /**
57
81
  * 将特殊标记恢复为代码块格式
58
82
  * @param content - 包含特殊标记的内容
@@ -65,27 +89,87 @@ import { ToolUseBarThink } from "../../../ToolUseBarThink";
65
89
  return "```".concat(lang, "\n").concat(code, "\n```");
66
90
  });
67
91
  };
68
- export function ThinkBlock(param) {
69
- var element = param.element;
92
+ export function ThinkBlock(props) {
70
93
  var _editorProps_codeProps, _editorProps_codeProps1;
94
+ var element = props.element;
71
95
  var locale = useContext(I18nContext).locale;
72
96
  var editorProps = (useContext(EditorStoreContext) || {}).editorProps;
73
97
  var message = useContext(MessagesContext).message;
98
+ var markdownEditorRef = useEditorStore().markdownEditorRef;
99
+ var thinkBlockContext = useContext(ThinkBlockContext);
74
100
  // 获取当前 Bubble 的 isFinished 状态
75
101
  var bubbleIsFinished = message === null || message === void 0 ? void 0 : message.isFinished;
76
- var _useMergedState = _sliced_to_array(useMergedState(function() {
77
- return bubbleIsFinished ? undefined : false;
78
- }, {
79
- value: editorProps === null || editorProps === void 0 ? void 0 : (_editorProps_codeProps = editorProps.codeProps) === null || _editorProps_codeProps === void 0 ? void 0 : _editorProps_codeProps.alwaysExpandedDeepThink,
80
- defaultValue: editorProps === null || editorProps === void 0 ? void 0 : (_editorProps_codeProps1 = editorProps.codeProps) === null || _editorProps_codeProps1 === void 0 ? void 0 : _editorProps_codeProps1.alwaysExpandedDeepThink
102
+ // 获取当前元素的 path
103
+ var elementPath = useMemo(function() {
104
+ if (!markdownEditorRef.current) {
105
+ return null;
106
+ }
107
+ try {
108
+ return EditorUtils.findPath(markdownEditorRef.current, element);
109
+ } catch (error) {
110
+ console.error('Error finding element path:', error);
111
+ return null;
112
+ }
113
+ }, [
114
+ markdownEditorRef.current,
115
+ element
116
+ ]);
117
+ // 判断是否是最后一个节点
118
+ var isLastNode = useMemo(function() {
119
+ if (!markdownEditorRef.current || !elementPath) {
120
+ return false;
121
+ }
122
+ try {
123
+ return EditorUtils.checkSelEnd(markdownEditorRef.current, elementPath);
124
+ } catch (error) {
125
+ console.error('Error checking if last node:', error);
126
+ return false;
127
+ }
128
+ }, [
129
+ markdownEditorRef.current,
130
+ elementPath
131
+ ]);
132
+ var _editorProps_codeProps_alwaysExpandedDeepThink, _thinkBlockContext_expanded;
133
+ // 状态优先级(从高到低):
134
+ // 1. Context 提供的 expanded(受控模式)
135
+ // 2. editorProps?.codeProps?.alwaysExpandedDeepThink
136
+ // 3. 默认值(false)
137
+ var _useMergedState = _sliced_to_array(useMergedState((_editorProps_codeProps_alwaysExpandedDeepThink = editorProps === null || editorProps === void 0 ? void 0 : (_editorProps_codeProps = editorProps.codeProps) === null || _editorProps_codeProps === void 0 ? void 0 : _editorProps_codeProps.alwaysExpandedDeepThink) !== null && _editorProps_codeProps_alwaysExpandedDeepThink !== void 0 ? _editorProps_codeProps_alwaysExpandedDeepThink : false, {
138
+ value: (editorProps === null || editorProps === void 0 ? void 0 : (_editorProps_codeProps1 = editorProps.codeProps) === null || _editorProps_codeProps1 === void 0 ? void 0 : _editorProps_codeProps1.alwaysExpandedDeepThink) ? true : (_thinkBlockContext_expanded = thinkBlockContext === null || thinkBlockContext === void 0 ? void 0 : thinkBlockContext.expanded) !== null && _thinkBlockContext_expanded !== void 0 ? _thinkBlockContext_expanded : undefined,
139
+ defaultValue: true,
140
+ onChange: thinkBlockContext === null || thinkBlockContext === void 0 ? void 0 : thinkBlockContext.onExpandedChange
81
141
  }), 2), expanded = _useMergedState[0], setExpanded = _useMergedState[1];
142
+ // 当 bubble 完成时,自动展开
82
143
  useEffect(function() {
83
144
  if (bubbleIsFinished) {
84
- setExpanded(true);
145
+ setExpanded(false);
85
146
  }
86
147
  }, [
87
148
  bubbleIsFinished
88
149
  ]);
150
+ // 如果不是最后一个节点,自动收起
151
+ useEffect(function() {
152
+ if (!isLastNode) {
153
+ setExpanded(false);
154
+ }
155
+ }, [
156
+ isLastNode
157
+ ]);
158
+ // 跟踪 thinkBlockContext?.expanded 的之前值
159
+ var prevExpandedRef = useRef(thinkBlockContext === null || thinkBlockContext === void 0 ? void 0 : thinkBlockContext.expanded);
160
+ // 当 thinkBlockContext?.expanded 从其他值变成 undefined 时,自动收起
161
+ useEffect(function() {
162
+ var prevExpanded = prevExpandedRef.current;
163
+ var currentExpanded = thinkBlockContext === null || thinkBlockContext === void 0 ? void 0 : thinkBlockContext.expanded;
164
+ // 如果之前有值(true 或 false),现在变成 undefined,则收起
165
+ if (prevExpanded !== undefined && currentExpanded === undefined) {
166
+ setExpanded(false);
167
+ }
168
+ // 更新之前的值
169
+ prevExpandedRef.current = currentExpanded;
170
+ }, [
171
+ thinkBlockContext === null || thinkBlockContext === void 0 ? void 0 : thinkBlockContext.expanded
172
+ ]);
89
173
  var rawContent = (element === null || element === void 0 ? void 0 : element.value) !== null && (element === null || element === void 0 ? void 0 : element.value) !== undefined ? String(element.value).trim() : '';
90
174
  // 恢复内容中被转义的代码块
91
175
  var content = restoreCodeBlocks(rawContent);
@@ -11,4 +11,4 @@ export { CodeToolbar, type CodeToolbarProps } from './CodeToolbar';
11
11
  export { HtmlPreview } from './HtmlPreview';
12
12
  export { LanguageSelector, type LanguageSelectorProps, } from './LanguageSelector';
13
13
  export { LoadImage } from './LoadImage';
14
- export { ThinkBlock } from './ThinkBlock';
14
+ export { ThinkBlock, ThinkBlockProvider, type ThinkBlockContextType, } from './ThinkBlock';
@@ -10,4 +10,4 @@ export { CodeToolbar } from "./CodeToolbar";
10
10
  export { HtmlPreview } from "./HtmlPreview";
11
11
  export { LanguageSelector } from "./LanguageSelector";
12
12
  export { LoadImage } from "./LoadImage";
13
- export { ThinkBlock } from "./ThinkBlock";
13
+ export { ThinkBlock, ThinkBlockProvider } from "./ThinkBlock";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ant-design/agentic-ui",
3
- "version": "2.28.2",
3
+ "version": "2.28.5",
4
4
  "description": "面向智能体的 UI 组件库,提供多步推理可视化、工具调用展示、任务执行协同等 Agentic UI 能力",
5
5
  "repository": "git@github.com:ant-design/agentic-ui.git",
6
6
  "license": "MIT",