Skip to content

Commit 3e7ad89

Browse files
committed
fix: Fix parsing of markdown messages in chat history
1 parent 20ec29a commit 3e7ad89

File tree

4 files changed

+49
-5
lines changed

4 files changed

+49
-5
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025 Tan Jin (tjtanjin)
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"peerDependencies": {
4646
"react": "^18.3.1",
4747
"react-dom": "^18.3.1",
48-
"react-chatbotify": "^2.0.0-beta.29"
48+
"react-chatbotify": "^2.0.0-beta.31"
4949
},
5050
"devDependencies": {
5151
"@eslint/js": "^9.20.0",

src/core/useRcbPlugin.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import {
66
RcbPreInjectMessageEvent,
77
Plugin,
88
RcbStartStreamMessageEvent,
9+
useMessages,
10+
useChatHistory,
11+
useSettings,
912
} from "react-chatbotify";
1013

1114
import MarkdownWrapper from "../components/MarkdownWrapper";
@@ -21,12 +24,28 @@ import { shouldRenderMarkdown } from "../utils/renderConditionHelper";
2124
const useRcbPlugin = (pluginConfig?: PluginConfig) => {
2225
const { getBotId } = useBotId();
2326
const { getFlow } = useFlow();
27+
const { messages, replaceMessages } = useMessages();
28+
const { settings } = useSettings();
29+
const { hasChatHistoryLoaded } = useChatHistory();
2430

2531
const mergedPluginConfig = { ...pluginConfig, ...DefaultPluginConfig };
2632

2733
// if custom component provided, use it; otherwise defaults to react-markdown
2834
const component = mergedPluginConfig.markdownComponent ? mergedPluginConfig.markdownComponent : MarkdownWrapper;
2935

36+
useEffect(() => {
37+
if (hasChatHistoryLoaded) {
38+
const messagesCopy = [...messages];
39+
for (let i = 0; i < messagesCopy.length && i < (settings.chatHistory?.maxEntries ?? 30); i++) {
40+
const message = messagesCopy[i];
41+
if (message.tags?.includes("rcb-markdown-renderer-plugin:parsed")) {
42+
message.contentWrapper = component;
43+
}
44+
}
45+
replaceMessages(messagesCopy);
46+
}
47+
}, [hasChatHistoryLoaded]);
48+
3049
useEffect(() => {
3150
/**
3251
* Handles message events and adds wrapper to render markdown if applicable.
@@ -45,6 +64,10 @@ const useRcbPlugin = (pluginConfig?: PluginConfig) => {
4564

4665

4766
event.data.message.contentWrapper = component;
67+
if (!event.data.message.tags) {
68+
event.data.message.tags = [];
69+
}
70+
event.data.message.tags.push("rcb-markdown-renderer-plugin:parsed");
4871
};
4972

5073
// adds required events

0 commit comments

Comments
 (0)