Skip to content

Commit 64525a3

Browse files
authored
Merge pull request #358 from FlowiseAI/feat/add-clear-chat-functionality
Add functionality to clear chat externally
2 parents 7b63134 + 73d17cf commit 64525a3

File tree

10 files changed

+168772
-17
lines changed

10 files changed

+168772
-17
lines changed

README.md

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,33 @@ To enable full screen, add `margin: 0` to <code>body</code> style. The default h
8282

8383
`height` and `width` accept a **number** (pixels) or a **CSS string**:
8484

85-
| Value | Behaviour |
86-
|---|---|
87-
| `height: 700` | Fixed 700px, automatically shrinks on smaller screens |
88-
| `height: '700px'` | Same as above via string |
89-
| `height: '80dvh'` | Responsive — 80% of viewport height on all screen sizes |
90-
| `height: 'min(700px, 80dvh)'` | Caps at 700px on large screens, shrinks proportionally on small screens |
91-
| `height: '100%'` | Relative to the `<flowise-fullchatbot>` host element — only works if the host has an explicit height set (e.g. via CSS). Use `'100dvh'` or omit `height` for full-viewport behaviour instead. |
85+
| Value | Behaviour |
86+
| ----------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
87+
| `height: 700` | Fixed 700px, automatically shrinks on smaller screens |
88+
| `height: '700px'` | Same as above via string |
89+
| `height: '80dvh'` | Responsive — 80% of viewport height on all screen sizes |
90+
| `height: 'min(700px, 80dvh)'` | Caps at 700px on large screens, shrinks proportionally on small screens |
91+
| `height: '100%'` | Relative to the `<flowise-fullchatbot>` host element — only works if the host has an explicit height set (e.g. via CSS). Use `'100dvh'` or omit `height` for full-viewport behaviour instead. |
9292

9393
The same options apply to `width`.
9494

95+
### Clear Chat
96+
97+
```html
98+
<script type="module">
99+
import Chatbot from 'https://cdn.jsdelivr.net/npm/flowise-embed/dist/web.js';
100+
Chatbot.initFull({
101+
chatflowid: '<chatflowid>',
102+
apiHost: 'http://localhost:3000',
103+
id: 'my-chatbot',
104+
});
105+
</script>
106+
107+
<!-- Call clearChat after the bot has mounted, e.g. from a button -->
108+
<button onclick="Chatbot.clearChat()">Clear All Chats</button>
109+
<button onclick="Chatbot.clearChat('my-chatbot')">Clear Specific Chat</button>
110+
```
111+
95112
## Configuration
96113

97114
You can also customize chatbot with different configuration

dist/components/Bot.d.ts.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/web.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ declare const chatbot: {
2020
theme?: import(".").BubbleTheme | undefined;
2121
}) => void;
2222
destroy: () => void;
23+
clearChat: (id?: string | undefined) => void;
2324
};
2425
export default chatbot;
2526
//# sourceMappingURL=web.d.ts.map

dist/web.d.ts.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/web.js

Lines changed: 84356 additions & 1 deletion
Large diffs are not rendered by default.

dist/web.umd.js

Lines changed: 84364 additions & 1 deletion
Large diffs are not rendered by default.

dist/window.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,20 @@ export declare const initFull: (props: BotProps & {
1414
}) => void;
1515
export declare const init: (props: BotProps) => void;
1616
export declare const destroy: () => void;
17+
export declare const clearChat: (id?: string) => void;
1718
type Chatbot = {
1819
initFull: typeof initFull;
1920
init: typeof init;
2021
destroy: typeof destroy;
22+
clearChat: typeof clearChat;
2123
};
2224
export declare const parseChatbot: () => {
2325
initFull: (props: BotProps & {
2426
id?: string;
2527
}) => void;
2628
init: (props: BotProps) => void;
2729
destroy: () => void;
30+
clearChat: (id?: string) => void;
2831
};
2932
export declare const injectChatbotInWindow: (bot: Chatbot) => void;
3033
export {};

dist/window.d.ts.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/Bot.tsx

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,19 @@ export const Bot = (botProps: BotProps & { class?: string }) => {
594594
chatContainer?.addEventListener('scroll', handleScroll, { passive: true });
595595
onCleanup(() => chatContainer?.removeEventListener('scroll', handleScroll));
596596

597+
const handleExternalClearChat = async (e: Event) => {
598+
const targetId = (e as CustomEvent).detail?.id;
599+
if (targetId) {
600+
const root = chatContainer?.getRootNode();
601+
const hostEl = root instanceof ShadowRoot ? root.host : chatContainer?.closest(`#${CSS.escape(targetId)}`);
602+
if (!hostEl || hostEl.id !== targetId) return;
603+
}
604+
if (loading()) await handleAbort();
605+
clearChat();
606+
};
607+
document.addEventListener('flowise-clear-chat', handleExternalClearChat);
608+
onCleanup(() => document.removeEventListener('flowise-clear-chat', handleExternalClearChat));
609+
597610
// Expose programmatic scroll guard to outer scope
598611
let guardTimeout: ReturnType<typeof setTimeout> | null = null;
599612
programmaticScrollGuard = (fn: () => void) => {
@@ -1365,18 +1378,15 @@ export const Bot = (botProps: BotProps & { class?: string }) => {
13651378
setMessages(messages);
13661379
setShowScrollButton(false);
13671380
} catch (error: any) {
1368-
const errorData = error.response.data || `${error.response.status}: ${error.response.statusText}`;
1369-
console.error(`error: ${errorData}`);
1381+
console.error('clearChat failed:', error);
13701382
}
13711383
};
13721384

13731385
onMount(() => {
13741386
if (props.clearChatOnReload) {
13751387
clearChat();
13761388
window.addEventListener('beforeunload', clearChat);
1377-
return () => {
1378-
window.removeEventListener('beforeunload', clearChat);
1379-
};
1389+
onCleanup(() => window.removeEventListener('beforeunload', clearChat));
13801390
}
13811391
});
13821392

src/window.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,15 @@ export const destroy = () => {
4747
elementUsed?.remove();
4848
};
4949

50+
export const clearChat = (id?: string) => {
51+
document.dispatchEvent(new CustomEvent('flowise-clear-chat', id ? { detail: { id } } : undefined));
52+
};
53+
5054
type Chatbot = {
5155
initFull: typeof initFull;
5256
init: typeof init;
5357
destroy: typeof destroy;
58+
clearChat: typeof clearChat;
5459
};
5560

5661
declare const window:
@@ -63,6 +68,7 @@ export const parseChatbot = () => ({
6368
initFull,
6469
init,
6570
destroy,
71+
clearChat,
6672
});
6773

6874
export const injectChatbotInWindow = (bot: Chatbot) => {

0 commit comments

Comments
 (0)