@agentscope-ai/chat 1.1.20 → 1.1.22
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/bin/client.js +1 -1
- package/components/AGUI/components/HelpModal/icons.tsx +68 -0
- package/components/AGUI/components/HelpModal/index.tsx +1 -0
- package/components/AGUI/components/HelpModal/modal.tsx +101 -0
- package/components/AGUI/components/chat/Button.tsx +18 -0
- package/components/AGUI/components/chat/Chat.tsx +780 -0
- package/components/AGUI/components/chat/ChatContext.tsx +248 -0
- package/components/AGUI/components/chat/CodeBlock.tsx +406 -0
- package/components/AGUI/components/chat/Header.tsx +22 -0
- package/components/AGUI/components/chat/Icons.tsx +237 -0
- package/components/AGUI/components/chat/ImageUploadQueue.tsx +77 -0
- package/components/AGUI/components/chat/Input.tsx +24 -0
- package/components/AGUI/components/chat/Markdown.tsx +134 -0
- package/components/AGUI/components/chat/Messages.tsx +259 -0
- package/components/AGUI/components/chat/Modal.tsx +133 -0
- package/components/AGUI/components/chat/Popup.tsx +57 -0
- package/components/AGUI/components/chat/PoweredByTag.tsx +29 -0
- package/components/AGUI/components/chat/Sidebar.tsx +74 -0
- package/components/AGUI/components/chat/Suggestion.tsx +132 -0
- package/components/AGUI/components/chat/Suggestions.tsx +20 -0
- package/components/AGUI/components/chat/Textarea.tsx +61 -0
- package/components/AGUI/components/chat/Window.tsx +152 -0
- package/components/AGUI/components/chat/index.tsx +11 -0
- package/components/AGUI/components/chat/messages/AssistantMessage.tsx +69 -0
- package/components/AGUI/components/chat/messages/RenderActionExecutionMessage.tsx +129 -0
- package/components/AGUI/components/chat/messages/RenderAgentStateMessage.tsx +116 -0
- package/components/AGUI/components/chat/messages/RenderImageMessage.tsx +64 -0
- package/components/AGUI/components/chat/messages/RenderResultMessage.tsx +26 -0
- package/components/AGUI/components/chat/messages/RenderTextMessage.tsx +51 -0
- package/components/AGUI/components/chat/messages/UserMessage.tsx +10 -0
- package/components/AGUI/components/chat/props.ts +186 -0
- package/components/AGUI/components/index.ts +1 -0
- package/components/AGUI/context/index.ts +1 -0
- package/components/AGUI/hooks/index.ts +1 -0
- package/components/AGUI/hooks/use-copilot-chat-suggestions.tsx +122 -0
- package/components/AGUI/hooks/use-copy-to-clipboard.tsx +29 -0
- package/components/AGUI/hooks/use-dark-mode.ts +10 -0
- package/components/AGUI/hooks/use-push-to-talk.tsx +166 -0
- package/components/AGUI/index.tsx +4 -0
- package/components/AGUI/lib/utils.test.ts +7 -0
- package/components/AGUI/lib/utils.ts +27 -0
- package/components/AGUI/styles.css +0 -0
- package/components/AGUI/types/css.ts +0 -0
- package/components/AGUI/types/index.ts +1 -0
- package/components/AGUI/types/suggestions.ts +6 -0
- package/components/Accordion/Accordion.tsx +203 -0
- package/components/Accordion/BodyContent.tsx +28 -0
- package/components/Accordion/DeepThinking.tsx +91 -0
- package/components/Accordion/SoftLightTitle.tsx +13 -0
- package/components/Accordion/demo/search.tsx +39 -0
- package/components/Accordion/demo/steps.tsx +135 -0
- package/components/Accordion/demo/thinking.tsx +62 -0
- package/components/Accordion/index.en-US.md +34 -0
- package/components/Accordion/index.tsx +3 -0
- package/components/Accordion/index.zh-CN.md +34 -0
- package/components/Accordion/style.ts +208 -0
- package/components/AgentScopeRuntimeWebUI/demo/index.tsx +4 -0
- package/components/AgentScopeRuntimeWebUI/index.tsx +3 -0
- package/components/AgentScopeRuntimeWebUI/lib/AgentScopeRuntime/Request/Builder.tsx +82 -0
- package/components/AgentScopeRuntimeWebUI/lib/AgentScopeRuntime/Request/Card.tsx +52 -0
- package/components/AgentScopeRuntimeWebUI/lib/AgentScopeRuntime/Response/Actions.tsx +39 -0
- package/components/AgentScopeRuntimeWebUI/lib/AgentScopeRuntime/Response/Builder.tsx +205 -0
- package/components/AgentScopeRuntimeWebUI/lib/AgentScopeRuntime/Response/Card.tsx +44 -0
- package/components/AgentScopeRuntimeWebUI/lib/AgentScopeRuntime/Response/Error.tsx +7 -0
- package/components/AgentScopeRuntimeWebUI/lib/AgentScopeRuntime/Response/Message.tsx +23 -0
- package/components/AgentScopeRuntimeWebUI/lib/AgentScopeRuntime/Response/Reasoning.tsx +16 -0
- package/components/AgentScopeRuntimeWebUI/lib/AgentScopeRuntime/Response/Tool.tsx +19 -0
- package/components/AgentScopeRuntimeWebUI/lib/AgentScopeRuntime/index.tsx +0 -0
- package/components/AgentScopeRuntimeWebUI/lib/AgentScopeRuntime/types.tsx +100 -0
- package/components/AgentScopeRuntimeWebUI/lib/Chat/Input/index.tsx +77 -0
- package/components/AgentScopeRuntimeWebUI/lib/Chat/Input/useAttachments.tsx +53 -0
- package/components/AgentScopeRuntimeWebUI/lib/Chat/MessageList/index.tsx +30 -0
- package/components/AgentScopeRuntimeWebUI/lib/Chat/Welcome/index.tsx +66 -0
- package/components/AgentScopeRuntimeWebUI/lib/Chat/Welcome/styles.ts +51 -0
- package/components/AgentScopeRuntimeWebUI/lib/Chat/hooks/index.tsx +5 -0
- package/components/AgentScopeRuntimeWebUI/lib/Chat/hooks/useChatController.tsx +130 -0
- package/components/AgentScopeRuntimeWebUI/lib/Chat/hooks/useChatMessageHandler.tsx +87 -0
- package/components/AgentScopeRuntimeWebUI/lib/Chat/hooks/useChatRequest.tsx +114 -0
- package/components/AgentScopeRuntimeWebUI/lib/Chat/hooks/useChatSessionHandler.tsx +50 -0
- package/components/AgentScopeRuntimeWebUI/lib/Chat/index.tsx +18 -0
- package/components/AgentScopeRuntimeWebUI/lib/Chat/styles.tsx +59 -0
- package/components/AgentScopeRuntimeWebUI/lib/ChatAnywhere/ComposedProvider.tsx +27 -0
- package/components/AgentScopeRuntimeWebUI/lib/ChatAnywhere/index.tsx +29 -0
- package/components/AgentScopeRuntimeWebUI/lib/Context/ChatAnywhereControl.tsx +0 -0
- package/components/AgentScopeRuntimeWebUI/lib/Context/ChatAnywhereInputContext.tsx +27 -0
- package/components/AgentScopeRuntimeWebUI/lib/Context/ChatAnywhereLayoutContext.tsx +27 -0
- package/components/AgentScopeRuntimeWebUI/lib/Context/ChatAnywhereMessagesContext.tsx +82 -0
- package/components/AgentScopeRuntimeWebUI/lib/Context/ChatAnywhereOptionsContext.tsx +76 -0
- package/components/AgentScopeRuntimeWebUI/lib/Context/ChatAnywhereSessionsContext.tsx +111 -0
- package/components/AgentScopeRuntimeWebUI/lib/Context/useChatAnywhereEventEmitter.tsx +30 -0
- package/components/AgentScopeRuntimeWebUI/lib/Header/index.tsx +45 -0
- package/components/AgentScopeRuntimeWebUI/lib/Header/styles.ts +23 -0
- package/components/AgentScopeRuntimeWebUI/lib/Layout/index.tsx +45 -0
- package/components/AgentScopeRuntimeWebUI/lib/Layout/styles.tsx +104 -0
- package/components/AgentScopeRuntimeWebUI/lib/Sessions/index.tsx +114 -0
- package/components/AgentScopeRuntimeWebUI/lib/Sessions/styles.tsx +0 -0
- package/components/AgentScopeRuntimeWebUI/lib/demo/OptionsPanel/FormItem.tsx +37 -0
- package/components/AgentScopeRuntimeWebUI/lib/demo/OptionsPanel/OptionsEditor.tsx +163 -0
- package/components/AgentScopeRuntimeWebUI/lib/demo/OptionsPanel/defaultConfig.ts +43 -0
- package/components/AgentScopeRuntimeWebUI/lib/demo/OptionsPanel/index.tsx +27 -0
- package/components/AgentScopeRuntimeWebUI/lib/demo/index.tsx +57 -0
- package/components/AgentScopeRuntimeWebUI/lib/demo/sessionApi/index.ts +51 -0
- package/components/AgentScopeRuntimeWebUI/lib/types/IChatAnywhere.ts +358 -0
- package/components/AgentScopeRuntimeWebUI/lib/types/IMessages.tsx +52 -0
- package/components/AgentScopeRuntimeWebUI/lib/types/ISessions.tsx +29 -0
- package/components/AgentScopeRuntimeWebUI/lib/types/index.tsx +3 -0
- package/components/AgentScopeRuntimeWebUI/starter/OptionsPanel/FormItem.tsx +37 -0
- package/components/AgentScopeRuntimeWebUI/starter/OptionsPanel/OptionsEditor.tsx +163 -0
- package/components/AgentScopeRuntimeWebUI/starter/OptionsPanel/defaultConfig.ts +43 -0
- package/components/AgentScopeRuntimeWebUI/starter/OptionsPanel/index.tsx +27 -0
- package/components/AgentScopeRuntimeWebUI/starter/index.tsx +57 -0
- package/components/AgentScopeRuntimeWebUI/starter/sessionApi/index.ts +51 -0
- package/components/Attachments/DropArea.tsx +97 -0
- package/components/Attachments/FileList/AudioIcon.tsx +20 -0
- package/components/Attachments/FileList/FileListCard.tsx +270 -0
- package/components/Attachments/FileList/Progress.tsx +30 -0
- package/components/Attachments/FileList/VideoIcon.tsx +20 -0
- package/components/Attachments/FileList/index.tsx +201 -0
- package/components/Attachments/PlaceholderUploader.tsx +124 -0
- package/components/Attachments/SilentUploader.tsx +39 -0
- package/components/Attachments/context.tsx +11 -0
- package/components/Attachments/demo/basic.tsx +71 -0
- package/components/Attachments/index.en-US.md.bk +10 -0
- package/components/Attachments/index.tsx +274 -0
- package/components/Attachments/index.zh-CN.md.bk +10 -0
- package/components/Attachments/style/fileCard.ts +163 -0
- package/components/Attachments/style/index.ts +232 -0
- package/components/Attachments/util.ts +56 -0
- package/components/Bubble/Avatar.tsx +39 -0
- package/components/Bubble/Bubble.tsx +131 -0
- package/components/Bubble/BubbleList.tsx +105 -0
- package/components/Bubble/Cards.tsx +61 -0
- package/components/Bubble/Footer.tsx +55 -0
- package/components/Bubble/Interrupted.tsx +82 -0
- package/components/Bubble/ScrollToBottom.tsx +42 -0
- package/components/Bubble/Spin.tsx +13 -0
- package/components/Bubble/demo/assistantWithCode.tsx +60 -0
- package/components/Bubble/demo/assistantWithErrorStatus.tsx +102 -0
- package/components/Bubble/demo/assistantWithImage.tsx +46 -0
- package/components/Bubble/demo/assistantWithProcess.tsx +103 -0
- package/components/Bubble/demo/assistantWithSearch.tsx +69 -0
- package/components/Bubble/demo/assistantWithThinking.tsx +54 -0
- package/components/Bubble/demo/basic.tsx +55 -0
- package/components/Bubble/demo/userWithFile.tsx +27 -0
- package/components/Bubble/demo/userWithImage.tsx +26 -0
- package/components/Bubble/hooks/useDisplayData.ts +0 -0
- package/components/Bubble/hooks/useListData.ts +0 -0
- package/components/Bubble/index.en-US.md +33 -0
- package/components/Bubble/index.tsx +21 -0
- package/components/Bubble/index.zh-CN.md +33 -0
- package/components/Bubble/interface.ts +71 -0
- package/components/Bubble/loading.tsx +15 -0
- package/components/Bubble/style/avatar.ts +43 -0
- package/components/Bubble/style/footer.ts +45 -0
- package/components/Bubble/style/index.ts +192 -0
- package/components/Bubble/style/list.ts +51 -0
- package/components/ChatAnywhere/Chat/Ref.tsx +20 -0
- package/components/ChatAnywhere/Chat/index.tsx +56 -0
- package/components/ChatAnywhere/Chat/style.ts +59 -0
- package/components/ChatAnywhere/Header/index.tsx +37 -0
- package/components/ChatAnywhere/Header/style.ts +27 -0
- package/components/ChatAnywhere/Input/index.tsx +158 -0
- package/components/ChatAnywhere/Input/style.ts +14 -0
- package/components/ChatAnywhere/Layout/index.tsx +70 -0
- package/components/ChatAnywhere/Layout/style.ts +51 -0
- package/components/ChatAnywhere/SessionList/index.tsx +122 -0
- package/components/ChatAnywhere/SessionList/style.ts +53 -0
- package/components/ChatAnywhere/hooks/ChatAnywhereProvider.tsx +219 -0
- package/components/ChatAnywhere/hooks/types.ts +333 -0
- package/components/ChatAnywhere/hooks/useInput.tsx +22 -0
- package/components/ChatAnywhere/hooks/useMessages.tsx +63 -0
- package/components/ChatAnywhere/hooks/useSessionList.tsx +123 -0
- package/components/ChatAnywhere/index.tsx +49 -0
- package/components/Conversations/GroupTitle.tsx +28 -0
- package/components/Conversations/Item.tsx +189 -0
- package/components/Conversations/demo/basic.tsx +107 -0
- package/components/Conversations/demo/timeline.tsx +111 -0
- package/components/Conversations/demo/timestamp.tsx +110 -0
- package/components/Conversations/hooks/useGroupable.ts +81 -0
- package/components/Conversations/index.en-US.md +32 -0
- package/components/Conversations/index.tsx +176 -0
- package/components/Conversations/index.zh-CN.md +31 -0
- package/components/Conversations/interface.ts +69 -0
- package/components/Conversations/style.ts +153 -0
- package/components/DefaultCards/DeepThinking/index.tsx +33 -0
- package/components/DefaultCards/Files/index.tsx +69 -0
- package/components/DefaultCards/Footer/index.tsx +16 -0
- package/components/DefaultCards/Images/index.tsx +53 -0
- package/components/DefaultCards/Interrupted/index.tsx +13 -0
- package/components/DefaultCards/Text/index.tsx +13 -0
- package/components/DefaultCards/index.tsx +6 -0
- package/components/DeviceAction/actionMap.tsx +100 -0
- package/components/DeviceAction/demo/index.tsx +132 -0
- package/components/DeviceAction/index.en-US.md +19 -0
- package/components/DeviceAction/index.tsx +75 -0
- package/components/DeviceAction/index.zh-CN.md +19 -0
- package/components/Disclaimer/demo/index.tsx +4 -0
- package/components/Disclaimer/demo/withLink.tsx +4 -0
- package/components/Disclaimer/index.en-US.md +23 -0
- package/components/Disclaimer/index.tsx +42 -0
- package/components/Disclaimer/index.zh-CN.md +24 -0
- package/components/Disclaimer/style.ts +17 -0
- package/components/ImageGenerator/demo/basic.tsx +23 -0
- package/components/ImageGenerator/demo/custom.tsx +56 -0
- package/components/ImageGenerator/demo/size.tsx +15 -0
- package/components/ImageGenerator/index.en-US.md +23 -0
- package/components/ImageGenerator/index.tsx +124 -0
- package/components/ImageGenerator/index.zh-CN.md +23 -0
- package/components/ImageGenerator/style.ts +99 -0
- package/components/Markdown/Markdown/AnimationNode.tsx +89 -0
- package/components/Markdown/Markdown/Markdown.tsx +61 -0
- package/components/Markdown/Markdown/core/Parser.ts +52 -0
- package/components/Markdown/Markdown/core/Renderer.ts +121 -0
- package/components/Markdown/Markdown/core/index.ts +4 -0
- package/components/Markdown/Markdown/defaultComponents/CodeBlock.tsx +113 -0
- package/components/Markdown/Markdown/defaultComponents/DisabledImage.tsx +3 -0
- package/components/Markdown/Markdown/defaultComponents/Media.tsx +71 -0
- package/components/Markdown/Markdown/hooks/index.ts +4 -0
- package/components/Markdown/Markdown/hooks/useAnimation.tsx +27 -0
- package/components/Markdown/Markdown/hooks/useCitationsData.tsx +36 -0
- package/components/Markdown/Markdown/hooks/useStreaming.ts +503 -0
- package/components/Markdown/Markdown/hooks/useTyping.ts +22 -0
- package/components/Markdown/Markdown/index.tsx +198 -0
- package/components/Markdown/Markdown/interface.ts +217 -0
- package/components/Markdown/Markdown/style.ts +152 -0
- package/components/Markdown/demo/basic.tsx +107 -0
- package/components/Markdown/demo/citations.tsx +47 -0
- package/components/Markdown/demo/cursor.tsx +9 -0
- package/components/Markdown/demo/latex.tsx +77 -0
- package/components/Markdown/demo/typing.tsx +82 -0
- package/components/Markdown/index.en-US.md +27 -0
- package/components/Markdown/index.ts +1 -0
- package/components/Markdown/index.zh-CN.md +28 -0
- package/components/Markdown/plugins/citations/CitationComponent.tsx +72 -0
- package/components/Markdown/plugins/citations/index.tsx +37 -0
- package/components/Markdown/plugins/cursor/Dot.tsx +106 -0
- package/components/Markdown/plugins/cursor/Underline.tsx +38 -0
- package/components/Markdown/plugins/cursor/index.tsx +59 -0
- package/components/Markdown/plugins/latex/index.ts +109 -0
- package/components/Markdown/plugins/type.ts +71 -0
- package/components/Mermaid/demo/basic.tsx +12 -0
- package/components/Mermaid/demo/class.tsx +31 -0
- package/components/Mermaid/demo/flowchart.tsx +13 -0
- package/components/Mermaid/demo/sequence.tsx +18 -0
- package/components/Mermaid/demo/state.tsx +16 -0
- package/components/Mermaid/demo/timeline.tsx +15 -0
- package/components/Mermaid/index.en-US.md +27 -0
- package/components/Mermaid/index.tsx +118 -0
- package/components/Mermaid/index.zh-CN.md +27 -0
- package/components/OperateCard/OperateCard.tsx +93 -0
- package/components/OperateCard/demo/index.tsx +35 -0
- package/components/OperateCard/demo/rag.tsx +19 -0
- package/components/OperateCard/demo/thinking.tsx +17 -0
- package/components/OperateCard/demo/todo.tsx +28 -0
- package/components/OperateCard/demo/toolCall.tsx +14 -0
- package/components/OperateCard/demo/webSearch.tsx +12 -0
- package/components/OperateCard/index.en-US.md +51 -0
- package/components/OperateCard/index.tsx +1 -0
- package/components/OperateCard/index.zh-CN.md +51 -0
- package/components/OperateCard/preset/Rag.tsx +90 -0
- package/components/OperateCard/preset/Thinking.tsx +45 -0
- package/components/OperateCard/preset/TodoList.tsx +73 -0
- package/components/OperateCard/preset/ToolCall.tsx +67 -0
- package/components/OperateCard/preset/WebSearch.tsx +65 -0
- package/components/OperateCard/preset/index.tsx +5 -0
- package/components/OperateCard/style.ts +268 -0
- package/components/Provider/CustomCardsProvider.tsx +17 -0
- package/components/Provider/GlobalProvider.tsx +17 -0
- package/components/Provider/index.tsx +33 -0
- package/components/Provider/types.ts +23 -0
- package/components/ResponsesAPI/index.tsx +0 -0
- package/components/Sender/ModeSelect/index.tsx +160 -0
- package/components/Sender/ModeSelect/styles.ts +0 -0
- package/components/Sender/SenderHeader.tsx +164 -0
- package/components/Sender/StopLoading.tsx +48 -0
- package/components/Sender/components/ActionButton.tsx +106 -0
- package/components/Sender/components/ClearButton.tsx +10 -0
- package/components/Sender/components/LoadingButton.tsx +26 -0
- package/components/Sender/components/SendButton.tsx +22 -0
- package/components/Sender/components/SpeechButton/RecordingIcon.tsx +68 -0
- package/components/Sender/components/SpeechButton/index.tsx +30 -0
- package/components/Sender/demo/asr.tsx +7 -0
- package/components/Sender/demo/basic.tsx +7 -0
- package/components/Sender/demo/loading.tsx +5 -0
- package/components/Sender/demo/moreMode.tsx +45 -0
- package/components/Sender/demo/morePrefixAction.tsx +40 -0
- package/components/Sender/demo/withFile.tsx +54 -0
- package/components/Sender/demo/withImage.tsx +52 -0
- package/components/Sender/index.en-US.md +29 -0
- package/components/Sender/index.tsx +491 -0
- package/components/Sender/index.zh-CN.md +30 -0
- package/components/Sender/style/index.ts +150 -0
- package/components/Sender/useSpeech.ts +133 -0
- package/components/StatusCard/demo/index.tsx +32 -0
- package/components/StatusCard/index.en-US.md +30 -0
- package/components/StatusCard/index.tsx +162 -0
- package/components/StatusCard/index.zh-CN.md +27 -0
- package/components/StatusCard/style.ts +119 -0
- package/components/Stream/index.ts +203 -0
- package/components/Util/hooks/use-proxy-imperative-handle.ts +25 -0
- package/components/Util/sleep.ts +3 -0
- package/components/Util/type.ts +1 -0
- package/components/Util/warning.ts +118 -0
- package/components/Version/index.ts +3 -0
- package/components/Voice/Recorder.tsx +3 -0
- package/components/Voice/demo/index.tsx +0 -0
- package/components/Voice/index.en-US.md +8 -0
- package/components/Voice/index.tsx +0 -0
- package/components/Voice/index.zh-CN.md +9 -0
- package/components/Welcome/demo/EyeFollower.tsx +161 -0
- package/components/Welcome/demo/demo0.tsx +14 -0
- package/components/Welcome/demo/demo1.tsx +16 -0
- package/components/Welcome/demo/demo2.tsx +30 -0
- package/components/Welcome/demo/demo3.tsx +16 -0
- package/components/Welcome/index.en-US.md +28 -0
- package/components/Welcome/index.tsx +72 -0
- package/components/Welcome/index.zh-CN.md +28 -0
- package/components/index.ts +88 -0
- package/components/overview.en-US.md +10 -0
- package/components/overview.zh-CN.md +10 -0
- package/lib/AgentScopeRuntimeWebUI/index.d.ts +1 -0
- package/lib/AgentScopeRuntimeWebUI/index.js +1 -0
- package/lib/AgentScopeRuntimeWebUI/starter/OptionsPanel/FormItem.d.ts +9 -0
- package/lib/AgentScopeRuntimeWebUI/starter/OptionsPanel/FormItem.js +33 -0
- package/lib/AgentScopeRuntimeWebUI/starter/OptionsPanel/OptionsEditor.d.ts +7 -0
- package/lib/AgentScopeRuntimeWebUI/starter/OptionsPanel/OptionsEditor.js +188 -0
- package/lib/AgentScopeRuntimeWebUI/starter/OptionsPanel/defaultConfig.d.ts +29 -0
- package/lib/AgentScopeRuntimeWebUI/starter/OptionsPanel/defaultConfig.js +35 -0
- package/lib/AgentScopeRuntimeWebUI/starter/OptionsPanel/index.d.ts +6 -0
- package/lib/AgentScopeRuntimeWebUI/starter/OptionsPanel/index.js +49 -0
- package/lib/AgentScopeRuntimeWebUI/starter/index.d.ts +1 -0
- package/lib/AgentScopeRuntimeWebUI/starter/index.js +70 -0
- package/lib/AgentScopeRuntimeWebUI/starter/sessionApi/index.d.ts +14 -0
- package/lib/AgentScopeRuntimeWebUI/starter/sessionApi/index.js +143 -0
- package/package.json +3 -2
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import React, { ReactNode } from 'react';
|
|
2
|
+
import Style from './style';
|
|
3
|
+
import { useProviderContext } from '../Provider';
|
|
4
|
+
import { ConfigProvider, Image } from 'antd';
|
|
5
|
+
import { Locale } from 'antd/es/locale';
|
|
6
|
+
import { SparkCheckCircleFill } from '@agentscope-ai/icons';
|
|
7
|
+
import Dot from '../Markdown/plugins/cursor/Dot';
|
|
8
|
+
|
|
9
|
+
export interface IImageGeneratorProps {
|
|
10
|
+
/**
|
|
11
|
+
* @description 生成图片的宽度,单位为像素
|
|
12
|
+
* @descriptionEn Width of the generated image in pixels
|
|
13
|
+
* @default 320
|
|
14
|
+
*/
|
|
15
|
+
width?: number;
|
|
16
|
+
/**
|
|
17
|
+
* @description 生成图片的高度,单位为像素
|
|
18
|
+
* @descriptionEn Height of the generated image in pixels
|
|
19
|
+
* @default 320
|
|
20
|
+
*/
|
|
21
|
+
height?: number;
|
|
22
|
+
/**
|
|
23
|
+
* @description 图片的URL地址,为空时显示加载状态
|
|
24
|
+
* @descriptionEn URL address of the image, shows loading state when empty
|
|
25
|
+
* @default ''
|
|
26
|
+
*/
|
|
27
|
+
src?: string;
|
|
28
|
+
/**
|
|
29
|
+
* @description 图片生成过程中的提示文本
|
|
30
|
+
* @descriptionEn Prompt text during image generation process
|
|
31
|
+
* @default 'Painting...'
|
|
32
|
+
*/
|
|
33
|
+
loadingText?: string;
|
|
34
|
+
/**
|
|
35
|
+
* @description 图片生成完成后的提示文本
|
|
36
|
+
* @descriptionEn Prompt text after image generation is completed
|
|
37
|
+
* @default 'Paint Completed'
|
|
38
|
+
*/
|
|
39
|
+
doneText?: string;
|
|
40
|
+
/**
|
|
41
|
+
* @description 自定义骨架屏组件,用于加载状态显示
|
|
42
|
+
* @descriptionEn Custom skeleton screen component for loading state display
|
|
43
|
+
* @default null
|
|
44
|
+
*/
|
|
45
|
+
skeleton?: ReactNode;
|
|
46
|
+
/**
|
|
47
|
+
* @description 自定义骨架屏组件的加载态提示
|
|
48
|
+
* @descriptionEn Custom skeleton screen component prompt text
|
|
49
|
+
* @default 'Painting...'
|
|
50
|
+
*/
|
|
51
|
+
skeletonText?: string;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* @description 是否为块级元素,使得图片宽高比为 width / height
|
|
55
|
+
* @descriptionEn Whether to be a block element, make the image width / height ratio
|
|
56
|
+
* @default false
|
|
57
|
+
*/
|
|
58
|
+
block?: boolean;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
const ImageGenerator: React.FC<IImageGeneratorProps> = (props) => {
|
|
63
|
+
const { getPrefixCls } = useProviderContext();
|
|
64
|
+
|
|
65
|
+
const prefixCls = getPrefixCls('image-generator');
|
|
66
|
+
const { block, skeletonText, width = 320, height = 320, src, loadingText = 'Painting...', doneText = 'Paint Completed' } = props;
|
|
67
|
+
|
|
68
|
+
const skeleton = props.skeleton || <div className={`${prefixCls}-default-skeleton`} style={{ width: '100%', height: '100%' }}>
|
|
69
|
+
<img
|
|
70
|
+
className={`${prefixCls}-default-skeleton-bg`}
|
|
71
|
+
src="https://img.alicdn.com/imgextra/i2/O1CN01jwLfwV1hPiHfJjQXM_!!6000000004270-2-tps-1155-763.png"
|
|
72
|
+
alt=""
|
|
73
|
+
/>
|
|
74
|
+
<div className={`${prefixCls}-default-skeleton-content`}>
|
|
75
|
+
<img
|
|
76
|
+
className={`${prefixCls}-default-skeleton-icon`}
|
|
77
|
+
src="https://img.alicdn.com/imgextra/i2/O1CN01M1X8yM1MWUC7u3Go5_!!6000000001442-54-tps-72-72.apng"
|
|
78
|
+
/>
|
|
79
|
+
|
|
80
|
+
{
|
|
81
|
+
skeletonText && <div className={`${prefixCls}-default-skeleton-text`}>{skeletonText}</div>
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
</div>
|
|
85
|
+
</div>;
|
|
86
|
+
|
|
87
|
+
const loading = !src;
|
|
88
|
+
|
|
89
|
+
const size: React.CSSProperties = block ? { aspectRatio: `${width}/${height}` } : { width, height };
|
|
90
|
+
|
|
91
|
+
return <>
|
|
92
|
+
<Style />
|
|
93
|
+
<div className={prefixCls}>
|
|
94
|
+
<div className={`${prefixCls}-text`}>
|
|
95
|
+
{
|
|
96
|
+
loading ? <Dot /> : <SparkCheckCircleFill className={`${prefixCls}-text-success`} />
|
|
97
|
+
}
|
|
98
|
+
{
|
|
99
|
+
loading ? <span style={{ paddingLeft: 20 }}>{loadingText}</span> : doneText
|
|
100
|
+
}
|
|
101
|
+
</div>
|
|
102
|
+
|
|
103
|
+
<div className={`${prefixCls}-wrapper`} style={size}>
|
|
104
|
+
{
|
|
105
|
+
loading ? skeleton : <ConfigProvider
|
|
106
|
+
locale={{
|
|
107
|
+
Image: { preview: '' }
|
|
108
|
+
} as Locale}
|
|
109
|
+
><Image
|
|
110
|
+
width={'100%'}
|
|
111
|
+
height={'100%'}
|
|
112
|
+
src={src}
|
|
113
|
+
/></ConfigProvider>
|
|
114
|
+
}
|
|
115
|
+
</div>
|
|
116
|
+
</div>
|
|
117
|
+
</>
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
export default ImageGenerator;
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
---
|
|
2
|
+
group:
|
|
3
|
+
title: 输出
|
|
4
|
+
order: 3
|
|
5
|
+
title: ImageGenerator
|
|
6
|
+
description: 生图类Query的过程展示
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
<DemoTitle title="ImageGenerator" desc="生图类Query的过程展示"></DemoTitle>
|
|
10
|
+
|
|
11
|
+
<code src="./demo/basic.tsx" height="auto">示例</code>
|
|
12
|
+
|
|
13
|
+
<Install>import { ImageGenerator } from '@agentscope-ai/chat'</Install>
|
|
14
|
+
|
|
15
|
+
#### 更多示例
|
|
16
|
+
|
|
17
|
+
<code src="./demo/size.tsx" height="auto">自定义尺寸</code>
|
|
18
|
+
|
|
19
|
+
<code src="./demo/custom.tsx" height="auto">自定义骨架 loading</code>
|
|
20
|
+
|
|
21
|
+
#### API
|
|
22
|
+
|
|
23
|
+
<ApiParser source="./index.tsx" id="IImageGeneratorProps"></ApiParser>
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { createGlobalStyle } from 'antd-style';
|
|
2
|
+
|
|
3
|
+
export default createGlobalStyle`
|
|
4
|
+
.${(p) => p.theme.prefixCls}-image-generator {
|
|
5
|
+
|
|
6
|
+
.${(p) => p.theme.prefixCls}-image {
|
|
7
|
+
border-radius: 8px;
|
|
8
|
+
filter: blur(20px);
|
|
9
|
+
animation: ${(p) => p.theme.prefixCls}clearBlur 1s ease forwards;
|
|
10
|
+
overflow: hidden;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
&-wrapper {
|
|
15
|
+
overflow: hidden;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
&-text {
|
|
20
|
+
position: relative;
|
|
21
|
+
display: flex;
|
|
22
|
+
gap: 8px;
|
|
23
|
+
height: 40px;
|
|
24
|
+
align-items: center;
|
|
25
|
+
font-size: 14px;
|
|
26
|
+
line-height: 26px;
|
|
27
|
+
color: ${(p) => p.theme.colorText};
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
&-success {
|
|
31
|
+
color: ${(p) => p.theme.colorSuccess};
|
|
32
|
+
font-size: 20px;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
&-default-skeleton {
|
|
37
|
+
position: relative;
|
|
38
|
+
overflow: hidden;
|
|
39
|
+
border-radius: 8px;
|
|
40
|
+
display: flex;
|
|
41
|
+
align-items: center;
|
|
42
|
+
justify-content: center;
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
&-bg {
|
|
46
|
+
position: absolute;
|
|
47
|
+
top: 50%;
|
|
48
|
+
left: 50%;
|
|
49
|
+
transform: translate(-50%, -50%) rotate(0deg);
|
|
50
|
+
width: 100vmax;
|
|
51
|
+
height: 100vmax;
|
|
52
|
+
object-fit: cover;
|
|
53
|
+
animation: ${(p) => p.theme.prefixCls}spin 6s linear infinite;
|
|
54
|
+
pointer-events: none;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
&-icon {
|
|
58
|
+
width: 32px;
|
|
59
|
+
height: 32px;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
&-content {
|
|
63
|
+
display: flex;
|
|
64
|
+
flex-direction: column;
|
|
65
|
+
gap: 4px;
|
|
66
|
+
align-items: center;
|
|
67
|
+
justify-content: center;
|
|
68
|
+
z-index: 1;
|
|
69
|
+
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
&-text {
|
|
73
|
+
margin-top: 8px;
|
|
74
|
+
font-size: 14px;
|
|
75
|
+
color: ${(p) => p.theme.colorBgBase};
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
@keyframes ${(p) => p.theme.prefixCls}clearBlur {
|
|
83
|
+
from {
|
|
84
|
+
filter: blur(20px);
|
|
85
|
+
}
|
|
86
|
+
to {
|
|
87
|
+
filter: blur(0);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
@keyframes ${(p) => p.theme.prefixCls}spin {
|
|
92
|
+
from {
|
|
93
|
+
transform: translate(-50%, -50%) rotate(0deg);
|
|
94
|
+
}
|
|
95
|
+
to {
|
|
96
|
+
transform: translate(-50%, -50%) rotate(360deg);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
`;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import type { DOMNode } from 'html-react-parser';
|
|
2
|
+
import React, { useEffect, useRef, useState } from 'react';
|
|
3
|
+
import { HTMLTag } from './hooks/useAnimation';
|
|
4
|
+
import { AnimationConfig } from './interface';
|
|
5
|
+
import { useGlobalContext, useProviderContext } from '@agentscope-ai/chat';
|
|
6
|
+
|
|
7
|
+
export interface AnimationNodeProps extends React.HTMLAttributes<HTMLElement> {
|
|
8
|
+
children?: React.ReactNode | React.ReactNode[];
|
|
9
|
+
nodeTag: HTMLTag;
|
|
10
|
+
animationConfig?: AnimationConfig;
|
|
11
|
+
streamStatus?: 'loading' | 'done';
|
|
12
|
+
domNode?: DOMNode;
|
|
13
|
+
[key: string]: unknown;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface AnimationTextProps {
|
|
17
|
+
text: string;
|
|
18
|
+
animationConfig?: AnimationConfig;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const AnimationText = React.memo<AnimationTextProps>((props) => {
|
|
22
|
+
const { text, animationConfig } = props;
|
|
23
|
+
const { fadeDuration = 200, easing = 'ease-in-out' } = animationConfig || {};
|
|
24
|
+
const [chunks, setChunks] = useState<string[]>([]);
|
|
25
|
+
const prevTextRef = useRef('');
|
|
26
|
+
const { getPrefixCls, } = useProviderContext();
|
|
27
|
+
const prefixCls = getPrefixCls('markdown');
|
|
28
|
+
|
|
29
|
+
useEffect(() => {
|
|
30
|
+
if (text === prevTextRef.current) return;
|
|
31
|
+
|
|
32
|
+
if (!(prevTextRef.current && text.indexOf(prevTextRef.current) === 0)) {
|
|
33
|
+
setChunks([text]);
|
|
34
|
+
prevTextRef.current = text;
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const newText = text.slice(prevTextRef.current.length);
|
|
39
|
+
if (!newText) return;
|
|
40
|
+
|
|
41
|
+
setChunks((prev) => [...prev, newText]);
|
|
42
|
+
prevTextRef.current = text;
|
|
43
|
+
}, [text]);
|
|
44
|
+
|
|
45
|
+
return (
|
|
46
|
+
<>
|
|
47
|
+
{chunks.map((text, index) => {
|
|
48
|
+
return (
|
|
49
|
+
<span
|
|
50
|
+
style={{
|
|
51
|
+
animation: `${prefixCls}-fadeIn ${fadeDuration}ms ${easing} forwards`,
|
|
52
|
+
}}
|
|
53
|
+
key={`${index}-${text}`}
|
|
54
|
+
>
|
|
55
|
+
{text}
|
|
56
|
+
</span>
|
|
57
|
+
);
|
|
58
|
+
})}
|
|
59
|
+
</>
|
|
60
|
+
);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
const AnimationNode: React.FC<AnimationNodeProps> = (props) => {
|
|
64
|
+
const { nodeTag, children, animationConfig, domNode, streamStatus, ...restProps } = props;
|
|
65
|
+
|
|
66
|
+
const renderChildren = (): React.ReactNode | React.ReactNode[] => {
|
|
67
|
+
if (!children) return null;
|
|
68
|
+
|
|
69
|
+
if (Array.isArray(children)) {
|
|
70
|
+
return children.map((child, index) =>
|
|
71
|
+
typeof child === 'string' ? (
|
|
72
|
+
<AnimationText key={index} animationConfig={animationConfig} text={child} />
|
|
73
|
+
) : (
|
|
74
|
+
child
|
|
75
|
+
),
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
return typeof children === 'string' ? (
|
|
79
|
+
<AnimationText text={children} animationConfig={animationConfig} />
|
|
80
|
+
) : (
|
|
81
|
+
children
|
|
82
|
+
);
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
return React.createElement(nodeTag, restProps, renderChildren());
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
export default AnimationNode;
|
|
89
|
+
export { AnimationText };
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import classnames from 'classnames';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { Parser, Renderer } from './core';
|
|
4
|
+
import { useAnimation, useStreaming } from './hooks';
|
|
5
|
+
import { MarkdownProps } from './interface';
|
|
6
|
+
import Style from './style';
|
|
7
|
+
|
|
8
|
+
const Markdown: React.FC<MarkdownProps> = (props) => {
|
|
9
|
+
|
|
10
|
+
const {
|
|
11
|
+
cursor,
|
|
12
|
+
baseFontSize,
|
|
13
|
+
baseLineHeight,
|
|
14
|
+
streaming,
|
|
15
|
+
config,
|
|
16
|
+
components,
|
|
17
|
+
paragraphTag = 'p',
|
|
18
|
+
content,
|
|
19
|
+
children,
|
|
20
|
+
rootClassName,
|
|
21
|
+
className,
|
|
22
|
+
style,
|
|
23
|
+
openLinksInNewTab,
|
|
24
|
+
dompurifyConfig,
|
|
25
|
+
} = props;
|
|
26
|
+
const mergedCls = classnames(rootClassName, className);
|
|
27
|
+
const mergedStyle: React.CSSProperties = {
|
|
28
|
+
fontSize: baseFontSize,
|
|
29
|
+
lineHeight: baseLineHeight,
|
|
30
|
+
...style,
|
|
31
|
+
};
|
|
32
|
+
const displayContent = useStreaming(content || children || '', streaming, cursor);
|
|
33
|
+
const animationComponents = useAnimation(streaming);
|
|
34
|
+
|
|
35
|
+
if (!displayContent) return null;
|
|
36
|
+
|
|
37
|
+
const parser = new Parser({
|
|
38
|
+
markedConfig: config,
|
|
39
|
+
paragraphTag,
|
|
40
|
+
openLinksInNewTab,
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
const renderComponents = { ...animationComponents, ...(components || {}) };
|
|
44
|
+
const renderer = new Renderer({
|
|
45
|
+
components: renderComponents,
|
|
46
|
+
dompurifyConfig,
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
const htmlString = parser.parse(displayContent);
|
|
50
|
+
return (
|
|
51
|
+
<>
|
|
52
|
+
<Style />
|
|
53
|
+
<div className={mergedCls} style={mergedStyle}>
|
|
54
|
+
{renderer.render(htmlString)}
|
|
55
|
+
</div>
|
|
56
|
+
</>
|
|
57
|
+
);
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
export default Markdown;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { Marked, Renderer, Tokens } from 'marked';
|
|
2
|
+
import { MarkdownProps } from '../interface';
|
|
3
|
+
|
|
4
|
+
type ParserOptions = {
|
|
5
|
+
markedConfig?: MarkdownProps['config'];
|
|
6
|
+
paragraphTag?: string;
|
|
7
|
+
openLinksInNewTab?: boolean;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
class Parser {
|
|
11
|
+
options: ParserOptions;
|
|
12
|
+
markdownInstance: Marked;
|
|
13
|
+
|
|
14
|
+
constructor(options: ParserOptions = {}) {
|
|
15
|
+
const { markedConfig = {}, openLinksInNewTab = true } = options;
|
|
16
|
+
this.options = options;
|
|
17
|
+
this.markdownInstance = new Marked(markedConfig);
|
|
18
|
+
this.configureRenderer(openLinksInNewTab);
|
|
19
|
+
this.configureParagraph();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
private configureRenderer(openLinksInNewTab?: boolean) {
|
|
23
|
+
if (!openLinksInNewTab) return;
|
|
24
|
+
|
|
25
|
+
const renderer = {
|
|
26
|
+
link(this: Renderer, { href, title, tokens }: Tokens.Link) {
|
|
27
|
+
const text = this.parser.parseInline(tokens);
|
|
28
|
+
const titleAttr = title ? ` title="${title}"` : '';
|
|
29
|
+
return `<a href="${href}"${titleAttr} target="_blank" rel="noopener noreferrer">${text}</a>`;
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
this.markdownInstance.use({ renderer });
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
public configureParagraph() {
|
|
36
|
+
const { paragraphTag } = this.options;
|
|
37
|
+
if (!paragraphTag) return;
|
|
38
|
+
|
|
39
|
+
const renderer = {
|
|
40
|
+
paragraph(this: Renderer, { tokens }: Tokens.Paragraph) {
|
|
41
|
+
return `<${paragraphTag}>${this.parser.parseInline(tokens)}</${paragraphTag}>\n`;
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
this.markdownInstance.use({ renderer });
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
public parse(content: string) {
|
|
48
|
+
return this.markdownInstance.parse(content) as string;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export default Parser;
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import type { Config as DOMPurifyConfig } from 'dompurify';
|
|
2
|
+
import DOMPurify from 'dompurify';
|
|
3
|
+
import parseHtml, { DOMNode, domToReact, Element } from 'html-react-parser';
|
|
4
|
+
import React, { ReactNode } from 'react';
|
|
5
|
+
import type { ComponentProps, MarkdownProps } from '../interface';
|
|
6
|
+
|
|
7
|
+
interface RendererOptions {
|
|
8
|
+
components?: MarkdownProps['components'];
|
|
9
|
+
dompurifyConfig?: DOMPurifyConfig;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
class Renderer {
|
|
13
|
+
private readonly options: RendererOptions;
|
|
14
|
+
|
|
15
|
+
constructor(options: RendererOptions) {
|
|
16
|
+
this.options = options;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Detect unclosed tags using regular expressions
|
|
21
|
+
*/
|
|
22
|
+
private detectUnclosedTags(htmlString: string): Set<string> {
|
|
23
|
+
const unclosedTags = new Set<string>();
|
|
24
|
+
const stack: string[] = [];
|
|
25
|
+
const tagRegex = /<\/?([a-zA-Z][a-zA-Z0-9-]*)(?:\s[^>]*)?>/g;
|
|
26
|
+
|
|
27
|
+
let match = tagRegex.exec(htmlString);
|
|
28
|
+
while (match !== null) {
|
|
29
|
+
const [fullMatch, tagName] = match;
|
|
30
|
+
const isClosing = fullMatch.startsWith('</');
|
|
31
|
+
const isSelfClosing = fullMatch.endsWith('/>');
|
|
32
|
+
|
|
33
|
+
if (this.options.components?.[tagName.toLowerCase()]) {
|
|
34
|
+
if (isClosing) {
|
|
35
|
+
// Found closing tag, pop from stack
|
|
36
|
+
const lastIndex = stack.lastIndexOf(tagName.toLowerCase());
|
|
37
|
+
if (lastIndex !== -1) {
|
|
38
|
+
stack.splice(lastIndex, 1);
|
|
39
|
+
}
|
|
40
|
+
} else if (!isSelfClosing) {
|
|
41
|
+
// Found opening tag, push to stack
|
|
42
|
+
stack.push(tagName.toLowerCase());
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
match = tagRegex.exec(htmlString);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Remaining tags in stack are unclosed
|
|
49
|
+
stack.forEach((tag) => {
|
|
50
|
+
unclosedTags.add(tag);
|
|
51
|
+
});
|
|
52
|
+
return unclosedTags;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Configure DOMPurify to preserve components and target attributes, filter everything else
|
|
57
|
+
*/
|
|
58
|
+
private configureDOMPurify(): DOMPurifyConfig {
|
|
59
|
+
const customComponents = Object.keys(this.options.components || {});
|
|
60
|
+
const userConfig = this.options.dompurifyConfig || {};
|
|
61
|
+
|
|
62
|
+
return {
|
|
63
|
+
...userConfig,
|
|
64
|
+
ADD_TAGS: [...new Set([...customComponents, ...(userConfig.ALLOWED_TAGS || [])])],
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
private replaceElement(unclosedTags?: Set<string>) {
|
|
69
|
+
return (domNode: DOMNode) => {
|
|
70
|
+
if (!('name' in domNode)) return;
|
|
71
|
+
|
|
72
|
+
const { name, attribs, children } = domNode as Element;
|
|
73
|
+
const renderElement = this.options.components?.[name];
|
|
74
|
+
|
|
75
|
+
if (renderElement) {
|
|
76
|
+
const streamStatus = unclosedTags?.has(name) ? 'loading' : 'done';
|
|
77
|
+
const props: ComponentProps = {
|
|
78
|
+
domNode,
|
|
79
|
+
streamStatus,
|
|
80
|
+
...attribs,
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
// Handle class and className merging
|
|
84
|
+
const classes = [props.className, props.classname, props.class]
|
|
85
|
+
.filter(Boolean)
|
|
86
|
+
.join(' ')
|
|
87
|
+
.trim();
|
|
88
|
+
props.className = classes || '';
|
|
89
|
+
|
|
90
|
+
if (children) {
|
|
91
|
+
props.children = this.processChildren(children as DOMNode[], unclosedTags);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return React.createElement(renderElement, props);
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
private processChildren(children: DOMNode[], unclosedTags?: Set<string>): ReactNode {
|
|
100
|
+
return domToReact(children as DOMNode[], {
|
|
101
|
+
replace: this.replaceElement(unclosedTags),
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
public processHtml(htmlString: string): React.ReactNode {
|
|
106
|
+
const unclosedTags = this.detectUnclosedTags(htmlString);
|
|
107
|
+
// Use DOMPurify to clean HTML while preserving custom components and target attributes
|
|
108
|
+
// const purifyConfig = this.configureDOMPurify();
|
|
109
|
+
// const cleanHtml = DOMPurify.sanitize(htmlString, purifyConfig);
|
|
110
|
+
|
|
111
|
+
return parseHtml(htmlString, {
|
|
112
|
+
replace: this.replaceElement(unclosedTags),
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
public render(html: string): ReactNode | null {
|
|
117
|
+
return this.processHtml(html);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export default Renderer;
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import React, { useMemo, useState } from 'react';
|
|
2
|
+
import type { FC } from 'react';
|
|
3
|
+
import { CodeBlock as Code, CodeBlockLangExtensionsMap, } from '@agentscope-ai/design';
|
|
4
|
+
import { createStyles } from 'antd-style';
|
|
5
|
+
import { useProviderContext, Mermaid } from '@agentscope-ai/chat';
|
|
6
|
+
import { theme as AntdTheme } from 'antd'
|
|
7
|
+
import { SparkCopyLine, SparkTrueLine } from '@agentscope-ai/icons';
|
|
8
|
+
|
|
9
|
+
const supportedLanguages = Object.keys(CodeBlockLangExtensionsMap);
|
|
10
|
+
|
|
11
|
+
const countLines = (str: string): number => {
|
|
12
|
+
const regex = /\n/g;
|
|
13
|
+
const matches = str.match(regex);
|
|
14
|
+
return matches ? matches.length : 1;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const useCode = (raw: any) => {
|
|
18
|
+
if (!raw) return {};
|
|
19
|
+
|
|
20
|
+
const { children, className } = raw.props;
|
|
21
|
+
|
|
22
|
+
if (!children) return {};
|
|
23
|
+
|
|
24
|
+
const content = Array.isArray(children) ? (children[0] as string) : children;
|
|
25
|
+
const lang = className?.replace('language-', '') || 'txt';
|
|
26
|
+
const isSingleLine = countLines(content) <= 1 && content.length <= 32;
|
|
27
|
+
|
|
28
|
+
return {
|
|
29
|
+
content,
|
|
30
|
+
isSingleLine,
|
|
31
|
+
lang,
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
interface CodeBlockProps {
|
|
36
|
+
children: any;
|
|
37
|
+
enableMermaid?: boolean;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const CodeBlock: FC<CodeBlockProps> = ({ children, enableMermaid }) => {
|
|
41
|
+
const code = useCode(children);
|
|
42
|
+
const { styles, cx } = useStyles();
|
|
43
|
+
const { getPrefixCls, theme } = useProviderContext();
|
|
44
|
+
const isDarkMode = theme?.algorithm === AntdTheme.darkAlgorithm;
|
|
45
|
+
const [copied, setCopied] = useState(false);
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
const { content: _content = '', lang = '' } = code;
|
|
49
|
+
const content = useMemo(() => {
|
|
50
|
+
return _content.replace(/ :(dot|underline):/g, '');
|
|
51
|
+
}, [_content])
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
if (!content) return null;
|
|
55
|
+
if (lang === 'mermaid') {
|
|
56
|
+
return (
|
|
57
|
+
<Mermaid content={content} width={"50%"} />
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
if (supportedLanguages.indexOf(lang) === -1) return <pre><code>{content}</code></pre>;
|
|
61
|
+
|
|
62
|
+
return <div className={styles.container}>
|
|
63
|
+
<div className={styles.header}>
|
|
64
|
+
<div className={styles.lang}>{lang}</div>
|
|
65
|
+
{
|
|
66
|
+
copied ? <SparkTrueLine className={styles.copied} /> : <SparkCopyLine className={styles.icon} onClick={() => {
|
|
67
|
+
navigator.clipboard.writeText(content);
|
|
68
|
+
setCopied(true);
|
|
69
|
+
setTimeout(() => {
|
|
70
|
+
setCopied(false);
|
|
71
|
+
}, 1000);
|
|
72
|
+
}} />
|
|
73
|
+
}
|
|
74
|
+
</div>
|
|
75
|
+
<Code value={content} language={lang} theme={isDarkMode ? 'dark' : 'light'} readOnly={true}></Code>
|
|
76
|
+
</div>
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const useStyles = createStyles(({ css, token }) => ({
|
|
80
|
+
copied: css`
|
|
81
|
+
color: ${token.colorSuccess};
|
|
82
|
+
cursor: pointer;
|
|
83
|
+
font-size: 16px;
|
|
84
|
+
`,
|
|
85
|
+
container: css`
|
|
86
|
+
border: 1px solid ${token.colorBorderSecondary};
|
|
87
|
+
border-radius: 6px;
|
|
88
|
+
overflow: hidden;
|
|
89
|
+
font-size: 0.8571428571428571em;
|
|
90
|
+
margin: 1em 0;
|
|
91
|
+
`,
|
|
92
|
+
header: css`
|
|
93
|
+
display: flex;
|
|
94
|
+
justify-content: space-between;
|
|
95
|
+
background: ${token.colorFillSecondary};
|
|
96
|
+
height: 28px;
|
|
97
|
+
line-height: 28px;
|
|
98
|
+
border-radius: 4px 4px 0 0;
|
|
99
|
+
align-items: center;
|
|
100
|
+
user-select: none;
|
|
101
|
+
position: relative;
|
|
102
|
+
padding: 0 12px;
|
|
103
|
+
`,
|
|
104
|
+
icon: css`
|
|
105
|
+
fontsize: 16px;
|
|
106
|
+
cursor: pointer;
|
|
107
|
+
`,
|
|
108
|
+
lang: {
|
|
109
|
+
textTransform: 'capitalize'
|
|
110
|
+
}
|
|
111
|
+
}));
|
|
112
|
+
|
|
113
|
+
export default CodeBlock;
|