@n8n/chat 0.19.0 → 0.19.1
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/README.md +2 -2
- package/dist/App.vue.d.ts +8 -0
- package/dist/__stories__/App.stories.d.ts +18 -0
- package/dist/__tests__/index.spec.d.ts +1 -0
- package/dist/__tests__/setup.d.ts +1 -0
- package/dist/__tests__/utils/create.d.ts +6 -0
- package/dist/__tests__/utils/fetch.d.ts +5 -0
- package/dist/__tests__/utils/selectors.d.ts +12 -0
- package/dist/api/generic.d.ts +6 -0
- package/dist/api/message.d.ts +4 -0
- package/dist/chat.bundle.es.js +11029 -0
- package/dist/chat.bundle.umd.js +37 -0
- package/dist/chat.es.js +6939 -0
- package/dist/chat.umd.js +18 -0
- package/dist/components/Button.vue.d.ts +11 -0
- package/dist/components/Chat.vue.d.ts +2 -0
- package/dist/components/ChatWindow.vue.d.ts +2 -0
- package/dist/components/GetStarted.vue.d.ts +2 -0
- package/dist/components/GetStartedFooter.vue.d.ts +2 -0
- package/dist/components/Input.vue.d.ts +2 -0
- package/dist/components/Layout.vue.d.ts +13 -0
- package/dist/components/Message.vue.d.ts +26 -0
- package/dist/components/MessageTyping.vue.d.ts +29 -0
- package/dist/components/MessagesList.vue.d.ts +17 -0
- package/dist/components/PoweredBy.vue.d.ts +2 -0
- package/dist/composables/useChat.d.ts +3 -0
- package/dist/composables/useI18n.d.ts +4 -0
- package/dist/composables/useOptions.d.ts +5 -0
- package/dist/constants/defaults.d.ts +4 -0
- package/dist/constants/localStorage.d.ts +2 -0
- package/dist/constants/symbols.d.ts +5 -0
- package/dist/event-buses/chatEventBus.d.ts +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/plugins/chat.d.ts +4 -0
- package/dist/style.css +1 -0
- package/dist/types/chat.d.ts +12 -0
- package/{src/types/messages.ts → dist/types/messages.d.ts} +10 -12
- package/dist/types/options.d.ts +31 -0
- package/dist/types/webhook.d.ts +16 -0
- package/dist/utils/event-bus.d.ts +8 -0
- package/dist/utils/mount.d.ts +1 -0
- package/package.json +45 -9
- package/.eslintignore +0 -2
- package/.eslintrc.cjs +0 -10
- package/.np-config.json +0 -5
- package/.storybook/main.ts +0 -4
- package/.storybook/preview.scss +0 -4
- package/.storybook/preview.ts +0 -15
- package/.vscode/extensions.json +0 -3
- package/build.config.js +0 -21
- package/env.d.ts +0 -1
- package/index.html +0 -13
- package/resources/images/fullscreen.png +0 -0
- package/resources/images/windowed.png +0 -0
- package/resources/workflow-manual.json +0 -238
- package/resources/workflow.json +0 -119
- package/scripts/pack.js +0 -11
- package/scripts/postbuild.js +0 -36
- package/src/App.vue +0 -23
- package/src/__stories__/App.stories.ts +0 -43
- package/src/__tests__/index.spec.ts +0 -218
- package/src/__tests__/setup.ts +0 -1
- package/src/__tests__/utils/create.ts +0 -16
- package/src/__tests__/utils/fetch.ts +0 -18
- package/src/__tests__/utils/selectors.ts +0 -53
- package/src/api/generic.ts +0 -63
- package/src/api/message.ts +0 -37
- package/src/components/Button.vue +0 -41
- package/src/components/Chat.vue +0 -96
- package/src/components/ChatWindow.vue +0 -123
- package/src/components/GetStarted.vue +0 -24
- package/src/components/GetStartedFooter.vue +0 -20
- package/src/components/Input.vue +0 -108
- package/src/components/Layout.vue +0 -99
- package/src/components/Message.vue +0 -137
- package/src/components/MessageTyping.vue +0 -110
- package/src/components/MessagesList.vue +0 -33
- package/src/components/PoweredBy.vue +0 -17
- package/src/composables/useChat.ts +0 -7
- package/src/composables/useI18n.ts +0 -21
- package/src/composables/useOptions.ts +0 -11
- package/src/constants/defaults.ts +0 -30
- package/src/constants/localStorage.ts +0 -2
- package/src/constants/symbols.ts +0 -8
- package/src/css/_tokens.scss +0 -38
- package/src/css/index.scss +0 -1
- package/src/event-buses/chatEventBus.ts +0 -3
- package/src/index.ts +0 -42
- package/src/main.scss +0 -5
- package/src/plugins/chat.ts +0 -115
- package/src/shims.d.ts +0 -6
- package/src/types/chat.ts +0 -12
- package/src/types/icons.d.ts +0 -5
- package/src/types/options.ts +0 -33
- package/src/types/webhook.ts +0 -18
- package/src/utils/event-bus.ts +0 -51
- package/src/utils/mount.ts +0 -16
- package/tsconfig.json +0 -24
- package/vite.config.mts +0 -53
- package/vitest.config.mts +0 -30
- /package/{src/__tests__/utils/index.ts → dist/__tests__/utils/index.d.ts} +0 -0
- /package/{src/api/index.ts → dist/api/index.d.ts} +0 -0
- /package/{src/components/index.ts → dist/components/index.d.ts} +0 -0
- /package/{src/composables/index.ts → dist/composables/index.d.ts} +0 -0
- /package/{src/constants/index.ts → dist/constants/index.d.ts} +0 -0
- /package/{src/event-buses/index.ts → dist/event-buses/index.d.ts} +0 -0
- /package/{public → dist}/favicon.ico +0 -0
- /package/{src/plugins/index.ts → dist/plugins/index.d.ts} +0 -0
- /package/{src/types/index.ts → dist/types/index.d.ts} +0 -0
- /package/{src/utils/index.ts → dist/utils/index.d.ts} +0 -0
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
<script lang="ts" setup>
|
|
2
|
-
import { computed } from 'vue';
|
|
3
|
-
import { Message } from './index';
|
|
4
|
-
import type { ChatMessage } from '@n8n/chat/types';
|
|
5
|
-
|
|
6
|
-
const props = withDefaults(
|
|
7
|
-
defineProps<{
|
|
8
|
-
animation?: 'bouncing' | 'scaling';
|
|
9
|
-
}>(),
|
|
10
|
-
{
|
|
11
|
-
animation: 'bouncing',
|
|
12
|
-
},
|
|
13
|
-
);
|
|
14
|
-
|
|
15
|
-
const message: ChatMessage = {
|
|
16
|
-
id: 'typing',
|
|
17
|
-
text: '',
|
|
18
|
-
sender: 'bot',
|
|
19
|
-
createdAt: '',
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
const classes = computed(() => {
|
|
23
|
-
return {
|
|
24
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
25
|
-
'chat-message-typing': true,
|
|
26
|
-
[`chat-message-typing-animation-${props.animation}`]: true,
|
|
27
|
-
};
|
|
28
|
-
});
|
|
29
|
-
</script>
|
|
30
|
-
<template>
|
|
31
|
-
<Message :class="classes" :message="message">
|
|
32
|
-
<div class="chat-message-typing-body">
|
|
33
|
-
<span class="chat-message-typing-circle"></span>
|
|
34
|
-
<span class="chat-message-typing-circle"></span>
|
|
35
|
-
<span class="chat-message-typing-circle"></span>
|
|
36
|
-
</div>
|
|
37
|
-
</Message>
|
|
38
|
-
</template>
|
|
39
|
-
<style lang="scss">
|
|
40
|
-
.chat-message-typing {
|
|
41
|
-
max-width: 80px;
|
|
42
|
-
|
|
43
|
-
&.chat-message-typing-animation-scaling .chat-message-typing-circle {
|
|
44
|
-
animation: chat-message-typing-animation-scaling 800ms ease-in-out infinite;
|
|
45
|
-
animation-delay: 3600ms;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
&.chat-message-typing-animation-bouncing .chat-message-typing-circle {
|
|
49
|
-
animation: chat-message-typing-animation-bouncing 800ms ease-in-out infinite;
|
|
50
|
-
animation-delay: 3600ms;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
.chat-message-typing-body {
|
|
54
|
-
display: flex;
|
|
55
|
-
justify-content: center;
|
|
56
|
-
align-items: center;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
.chat-message-typing-circle {
|
|
60
|
-
display: block;
|
|
61
|
-
height: 10px;
|
|
62
|
-
width: 10px;
|
|
63
|
-
border-radius: 50%;
|
|
64
|
-
background-color: var(--chat--color-typing);
|
|
65
|
-
margin: 3px;
|
|
66
|
-
|
|
67
|
-
&:nth-child(1) {
|
|
68
|
-
animation-delay: 0ms;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
&:nth-child(2) {
|
|
72
|
-
animation-delay: 333ms;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
&:nth-child(3) {
|
|
76
|
-
animation-delay: 666ms;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
@keyframes chat-message-typing-animation-scaling {
|
|
82
|
-
0% {
|
|
83
|
-
transform: scale(1);
|
|
84
|
-
}
|
|
85
|
-
33% {
|
|
86
|
-
transform: scale(1);
|
|
87
|
-
}
|
|
88
|
-
50% {
|
|
89
|
-
transform: scale(1.4);
|
|
90
|
-
}
|
|
91
|
-
100% {
|
|
92
|
-
transform: scale(1);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
@keyframes chat-message-typing-animation-bouncing {
|
|
97
|
-
0% {
|
|
98
|
-
transform: translateY(0);
|
|
99
|
-
}
|
|
100
|
-
33% {
|
|
101
|
-
transform: translateY(0);
|
|
102
|
-
}
|
|
103
|
-
50% {
|
|
104
|
-
transform: translateY(-10px);
|
|
105
|
-
}
|
|
106
|
-
100% {
|
|
107
|
-
transform: translateY(0);
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
</style>
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
<script lang="ts" setup>
|
|
2
|
-
import Message from '@n8n/chat/components/Message.vue';
|
|
3
|
-
import type { ChatMessage } from '@n8n/chat/types';
|
|
4
|
-
import MessageTyping from '@n8n/chat/components/MessageTyping.vue';
|
|
5
|
-
import { useChat } from '@n8n/chat/composables';
|
|
6
|
-
|
|
7
|
-
defineProps<{
|
|
8
|
-
messages: ChatMessage[];
|
|
9
|
-
}>();
|
|
10
|
-
|
|
11
|
-
const chatStore = useChat();
|
|
12
|
-
|
|
13
|
-
const { initialMessages, waitingForResponse } = chatStore;
|
|
14
|
-
</script>
|
|
15
|
-
<template>
|
|
16
|
-
<div class="chat-messages-list">
|
|
17
|
-
<Message
|
|
18
|
-
v-for="initialMessage in initialMessages"
|
|
19
|
-
:key="initialMessage.id"
|
|
20
|
-
:message="initialMessage"
|
|
21
|
-
/>
|
|
22
|
-
<Message v-for="message in messages" :key="message.id" :message="message" />
|
|
23
|
-
<MessageTyping v-if="waitingForResponse" />
|
|
24
|
-
</div>
|
|
25
|
-
</template>
|
|
26
|
-
|
|
27
|
-
<style lang="scss">
|
|
28
|
-
.chat-messages-list {
|
|
29
|
-
margin-top: auto;
|
|
30
|
-
display: block;
|
|
31
|
-
padding: var(--chat--messages-list--padding, var(--chat--spacing));
|
|
32
|
-
}
|
|
33
|
-
</style>
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
<template>
|
|
2
|
-
<div class="chat-powered-by">
|
|
3
|
-
Powered by
|
|
4
|
-
<a href="https://n8n.io?utm_source=n8n-external&utm_medium=widget-powered-by">n8n</a>
|
|
5
|
-
</div>
|
|
6
|
-
</template>
|
|
7
|
-
|
|
8
|
-
<style lang="scss">
|
|
9
|
-
.chat-powered-by {
|
|
10
|
-
text-align: center;
|
|
11
|
-
|
|
12
|
-
a {
|
|
13
|
-
color: var(--chat--color-primary);
|
|
14
|
-
text-decoration: none;
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
</style>
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { isRef } from 'vue';
|
|
2
|
-
import { useOptions } from '@n8n/chat/composables/useOptions';
|
|
3
|
-
|
|
4
|
-
export function useI18n() {
|
|
5
|
-
const { options } = useOptions();
|
|
6
|
-
const language = options?.defaultLanguage ?? 'en';
|
|
7
|
-
|
|
8
|
-
function t(key: string): string {
|
|
9
|
-
const val = options?.i18n?.[language]?.[key];
|
|
10
|
-
if (isRef(val)) {
|
|
11
|
-
return val.value as string;
|
|
12
|
-
}
|
|
13
|
-
return val ?? key;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
function te(key: string): boolean {
|
|
17
|
-
return !!options?.i18n?.[language]?.[key];
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
return { t, te };
|
|
21
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { inject } from 'vue';
|
|
2
|
-
import { ChatOptionsSymbol } from '@n8n/chat/constants';
|
|
3
|
-
import type { ChatOptions } from '@n8n/chat/types';
|
|
4
|
-
|
|
5
|
-
export function useOptions() {
|
|
6
|
-
const options = inject(ChatOptionsSymbol) as ChatOptions;
|
|
7
|
-
|
|
8
|
-
return {
|
|
9
|
-
options,
|
|
10
|
-
};
|
|
11
|
-
}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import type { ChatOptions } from '@n8n/chat/types';
|
|
2
|
-
|
|
3
|
-
export const defaultOptions: ChatOptions = {
|
|
4
|
-
webhookUrl: 'http://localhost:5678',
|
|
5
|
-
webhookConfig: {
|
|
6
|
-
method: 'POST',
|
|
7
|
-
headers: {},
|
|
8
|
-
},
|
|
9
|
-
target: '#n8n-chat',
|
|
10
|
-
mode: 'window',
|
|
11
|
-
loadPreviousSession: true,
|
|
12
|
-
chatInputKey: 'chatInput',
|
|
13
|
-
chatSessionKey: 'sessionId',
|
|
14
|
-
defaultLanguage: 'en',
|
|
15
|
-
showWelcomeScreen: false,
|
|
16
|
-
initialMessages: ['Hi there! 👋', 'My name is Nathan. How can I assist you today?'],
|
|
17
|
-
i18n: {
|
|
18
|
-
en: {
|
|
19
|
-
title: 'Hi there! 👋',
|
|
20
|
-
subtitle: "Start a chat. We're here to help you 24/7.",
|
|
21
|
-
footer: '',
|
|
22
|
-
getStarted: 'New Conversation',
|
|
23
|
-
inputPlaceholder: 'Type your question..',
|
|
24
|
-
closeButtonTooltip: 'Close chat',
|
|
25
|
-
},
|
|
26
|
-
},
|
|
27
|
-
theme: {},
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
export const defaultMountingTarget = '#n8n-chat';
|
package/src/constants/symbols.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import type { InjectionKey } from 'vue';
|
|
2
|
-
import type { Chat, ChatOptions } from '@n8n/chat/types';
|
|
3
|
-
|
|
4
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
5
|
-
export const ChatSymbol = 'Chat' as unknown as InjectionKey<Chat>;
|
|
6
|
-
|
|
7
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
8
|
-
export const ChatOptionsSymbol = 'ChatOptions' as unknown as InjectionKey<ChatOptions>;
|
package/src/css/_tokens.scss
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
:root {
|
|
2
|
-
--chat--color-primary: #e74266;
|
|
3
|
-
--chat--color-primary-shade-50: #db4061;
|
|
4
|
-
--chat--color-primary-shade-100: #cf3c5c;
|
|
5
|
-
--chat--color-secondary: #20b69e;
|
|
6
|
-
--chat--color-secondary-shade-50: #1ca08a;
|
|
7
|
-
--chat--color-white: #ffffff;
|
|
8
|
-
--chat--color-light: #f2f4f8;
|
|
9
|
-
--chat--color-light-shade-50: #e6e9f1;
|
|
10
|
-
--chat--color-light-shade-100: #c2c5cc;
|
|
11
|
-
--chat--color-medium: #d2d4d9;
|
|
12
|
-
--chat--color-dark: #101330;
|
|
13
|
-
--chat--color-disabled: #777980;
|
|
14
|
-
--chat--color-typing: #404040;
|
|
15
|
-
|
|
16
|
-
--chat--spacing: 1rem;
|
|
17
|
-
--chat--border-radius: 0.25rem;
|
|
18
|
-
--chat--transition-duration: 0.15s;
|
|
19
|
-
|
|
20
|
-
--chat--window--width: 400px;
|
|
21
|
-
--chat--window--height: 600px;
|
|
22
|
-
|
|
23
|
-
--chat--textarea--height: 50px;
|
|
24
|
-
|
|
25
|
-
--chat--message--bot--background: var(--chat--color-white);
|
|
26
|
-
--chat--message--bot--color: var(--chat--color-dark);
|
|
27
|
-
--chat--message--user--background: var(--chat--color-secondary);
|
|
28
|
-
--chat--message--user--color: var(--chat--color-white);
|
|
29
|
-
--chat--message--pre--background: rgba(0, 0, 0, 0.05);
|
|
30
|
-
|
|
31
|
-
--chat--toggle--background: var(--chat--color-primary);
|
|
32
|
-
--chat--toggle--hover--background: var(--chat--color-primary-shade-50);
|
|
33
|
-
--chat--toggle--active--background: var(--chat--color-primary-shade-100);
|
|
34
|
-
--chat--toggle--color: var(--chat--color-white);
|
|
35
|
-
--chat--toggle--size: 64px;
|
|
36
|
-
|
|
37
|
-
--chat--heading--font-size: 2em;
|
|
38
|
-
}
|
package/src/css/index.scss
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
@import 'tokens';
|
package/src/index.ts
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import './main.scss';
|
|
2
|
-
|
|
3
|
-
import { createApp } from 'vue';
|
|
4
|
-
import App from './App.vue';
|
|
5
|
-
import type { ChatOptions } from '@n8n/chat/types';
|
|
6
|
-
import { defaultMountingTarget, defaultOptions } from '@n8n/chat/constants';
|
|
7
|
-
import { createDefaultMountingTarget } from '@n8n/chat/utils';
|
|
8
|
-
import { ChatPlugin } from '@n8n/chat/plugins';
|
|
9
|
-
|
|
10
|
-
export function createChat(options?: Partial<ChatOptions>) {
|
|
11
|
-
const resolvedOptions: ChatOptions = {
|
|
12
|
-
...defaultOptions,
|
|
13
|
-
...options,
|
|
14
|
-
webhookConfig: {
|
|
15
|
-
...defaultOptions.webhookConfig,
|
|
16
|
-
...options?.webhookConfig,
|
|
17
|
-
},
|
|
18
|
-
i18n: {
|
|
19
|
-
...defaultOptions.i18n,
|
|
20
|
-
...options?.i18n,
|
|
21
|
-
en: {
|
|
22
|
-
...defaultOptions.i18n?.en,
|
|
23
|
-
...options?.i18n?.en,
|
|
24
|
-
},
|
|
25
|
-
},
|
|
26
|
-
theme: {
|
|
27
|
-
...defaultOptions.theme,
|
|
28
|
-
...options?.theme,
|
|
29
|
-
},
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
const mountingTarget = resolvedOptions.target ?? defaultMountingTarget;
|
|
33
|
-
if (typeof mountingTarget === 'string') {
|
|
34
|
-
createDefaultMountingTarget(mountingTarget);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
38
|
-
const app = createApp(App);
|
|
39
|
-
app.use(ChatPlugin, resolvedOptions);
|
|
40
|
-
app.mount(mountingTarget);
|
|
41
|
-
return app;
|
|
42
|
-
}
|
package/src/main.scss
DELETED
package/src/plugins/chat.ts
DELETED
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
import type { Plugin } from 'vue';
|
|
2
|
-
import { computed, nextTick, ref } from 'vue';
|
|
3
|
-
import { v4 as uuidv4 } from 'uuid';
|
|
4
|
-
import type { ChatMessage, ChatOptions } from '@n8n/chat/types';
|
|
5
|
-
import { chatEventBus } from '@n8n/chat/event-buses';
|
|
6
|
-
import * as api from '@n8n/chat/api';
|
|
7
|
-
import { ChatOptionsSymbol, ChatSymbol, localStorageSessionIdKey } from '@n8n/chat/constants';
|
|
8
|
-
|
|
9
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
10
|
-
export const ChatPlugin: Plugin<ChatOptions> = {
|
|
11
|
-
install(app, options) {
|
|
12
|
-
app.provide(ChatOptionsSymbol, options);
|
|
13
|
-
|
|
14
|
-
const messages = ref<ChatMessage[]>([]);
|
|
15
|
-
const currentSessionId = ref<string | null>(null);
|
|
16
|
-
const waitingForResponse = ref(false);
|
|
17
|
-
|
|
18
|
-
const initialMessages = computed<ChatMessage[]>(() =>
|
|
19
|
-
(options.initialMessages ?? []).map((text) => ({
|
|
20
|
-
id: uuidv4(),
|
|
21
|
-
text,
|
|
22
|
-
sender: 'bot',
|
|
23
|
-
createdAt: new Date().toISOString(),
|
|
24
|
-
})),
|
|
25
|
-
);
|
|
26
|
-
|
|
27
|
-
async function sendMessage(text: string) {
|
|
28
|
-
const sentMessage: ChatMessage = {
|
|
29
|
-
id: uuidv4(),
|
|
30
|
-
text,
|
|
31
|
-
sender: 'user',
|
|
32
|
-
createdAt: new Date().toISOString(),
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
messages.value.push(sentMessage);
|
|
36
|
-
waitingForResponse.value = true;
|
|
37
|
-
|
|
38
|
-
void nextTick(() => {
|
|
39
|
-
chatEventBus.emit('scrollToBottom');
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
const sendMessageResponse = await api.sendMessage(
|
|
43
|
-
text,
|
|
44
|
-
currentSessionId.value as string,
|
|
45
|
-
options,
|
|
46
|
-
);
|
|
47
|
-
|
|
48
|
-
let textMessage = sendMessageResponse.output ?? sendMessageResponse.text ?? '';
|
|
49
|
-
|
|
50
|
-
if (textMessage === '' && Object.keys(sendMessageResponse).length > 0) {
|
|
51
|
-
try {
|
|
52
|
-
textMessage = JSON.stringify(sendMessageResponse, null, 2);
|
|
53
|
-
} catch (e) {
|
|
54
|
-
// Failed to stringify the object so fallback to empty string
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const receivedMessage: ChatMessage = {
|
|
59
|
-
id: uuidv4(),
|
|
60
|
-
text: textMessage,
|
|
61
|
-
sender: 'bot',
|
|
62
|
-
createdAt: new Date().toISOString(),
|
|
63
|
-
};
|
|
64
|
-
messages.value.push(receivedMessage);
|
|
65
|
-
|
|
66
|
-
waitingForResponse.value = false;
|
|
67
|
-
|
|
68
|
-
void nextTick(() => {
|
|
69
|
-
chatEventBus.emit('scrollToBottom');
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
async function loadPreviousSession() {
|
|
74
|
-
if (!options.loadPreviousSession) {
|
|
75
|
-
return;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
const sessionId = localStorage.getItem(localStorageSessionIdKey) ?? uuidv4();
|
|
79
|
-
const previousMessagesResponse = await api.loadPreviousSession(sessionId, options);
|
|
80
|
-
const timestamp = new Date().toISOString();
|
|
81
|
-
|
|
82
|
-
messages.value = (previousMessagesResponse?.data || []).map((message, index) => ({
|
|
83
|
-
id: `${index}`,
|
|
84
|
-
text: message.kwargs.content,
|
|
85
|
-
sender: message.id.includes('HumanMessage') ? 'user' : 'bot',
|
|
86
|
-
createdAt: timestamp,
|
|
87
|
-
}));
|
|
88
|
-
|
|
89
|
-
if (messages.value.length) {
|
|
90
|
-
currentSessionId.value = sessionId;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
return sessionId;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
async function startNewSession() {
|
|
97
|
-
currentSessionId.value = uuidv4();
|
|
98
|
-
|
|
99
|
-
localStorage.setItem(localStorageSessionIdKey, currentSessionId.value);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
const chatStore = {
|
|
103
|
-
initialMessages,
|
|
104
|
-
messages,
|
|
105
|
-
currentSessionId,
|
|
106
|
-
waitingForResponse,
|
|
107
|
-
loadPreviousSession,
|
|
108
|
-
startNewSession,
|
|
109
|
-
sendMessage,
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
app.provide(ChatSymbol, chatStore);
|
|
113
|
-
app.config.globalProperties.$chat = chatStore;
|
|
114
|
-
},
|
|
115
|
-
};
|
package/src/shims.d.ts
DELETED
package/src/types/chat.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import type { Ref } from 'vue';
|
|
2
|
-
import type { ChatMessage } from '@n8n/chat/types/messages';
|
|
3
|
-
|
|
4
|
-
export interface Chat {
|
|
5
|
-
initialMessages: Ref<ChatMessage[]>;
|
|
6
|
-
messages: Ref<ChatMessage[]>;
|
|
7
|
-
currentSessionId: Ref<string | null>;
|
|
8
|
-
waitingForResponse: Ref<boolean>;
|
|
9
|
-
loadPreviousSession?: () => Promise<string | undefined>;
|
|
10
|
-
startNewSession?: () => Promise<void>;
|
|
11
|
-
sendMessage: (text: string) => Promise<void>;
|
|
12
|
-
}
|
package/src/types/icons.d.ts
DELETED
package/src/types/options.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import type { Component, Ref } from 'vue';
|
|
2
|
-
export interface ChatOptions {
|
|
3
|
-
webhookUrl: string;
|
|
4
|
-
webhookConfig?: {
|
|
5
|
-
method?: 'GET' | 'POST';
|
|
6
|
-
headers?: Record<string, string>;
|
|
7
|
-
};
|
|
8
|
-
target?: string | Element;
|
|
9
|
-
mode?: 'window' | 'fullscreen';
|
|
10
|
-
showWindowCloseButton?: boolean;
|
|
11
|
-
showWelcomeScreen?: boolean;
|
|
12
|
-
loadPreviousSession?: boolean;
|
|
13
|
-
chatInputKey?: string;
|
|
14
|
-
chatSessionKey?: string;
|
|
15
|
-
defaultLanguage?: 'en';
|
|
16
|
-
initialMessages?: string[];
|
|
17
|
-
metadata?: Record<string, unknown>;
|
|
18
|
-
i18n: Record<
|
|
19
|
-
string,
|
|
20
|
-
{
|
|
21
|
-
title: string;
|
|
22
|
-
subtitle: string;
|
|
23
|
-
footer: string;
|
|
24
|
-
getStarted: string;
|
|
25
|
-
inputPlaceholder: string;
|
|
26
|
-
closeButtonTooltip: string;
|
|
27
|
-
[message: string]: string;
|
|
28
|
-
}
|
|
29
|
-
>;
|
|
30
|
-
theme?: {};
|
|
31
|
-
messageComponents?: Record<string, Component>;
|
|
32
|
-
disabled?: Ref<boolean>;
|
|
33
|
-
}
|
package/src/types/webhook.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
export interface LoadPreviousSessionResponseItem {
|
|
2
|
-
id: string[];
|
|
3
|
-
kwargs: {
|
|
4
|
-
content: string;
|
|
5
|
-
additional_kwargs: Record<string, unknown>;
|
|
6
|
-
};
|
|
7
|
-
lc: number;
|
|
8
|
-
type: string;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export interface LoadPreviousSessionResponse {
|
|
12
|
-
data: LoadPreviousSessionResponseItem[];
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export interface SendMessageResponse {
|
|
16
|
-
output?: string;
|
|
17
|
-
text?: string;
|
|
18
|
-
}
|
package/src/utils/event-bus.ts
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
2
|
-
export type CallbackFn = Function;
|
|
3
|
-
export type UnregisterFn = () => void;
|
|
4
|
-
|
|
5
|
-
export interface EventBus {
|
|
6
|
-
on: (eventName: string, fn: CallbackFn) => UnregisterFn;
|
|
7
|
-
off: (eventName: string, fn: CallbackFn) => void;
|
|
8
|
-
emit: <T = Event>(eventName: string, event?: T) => void;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export function createEventBus(): EventBus {
|
|
12
|
-
const handlers = new Map<string, CallbackFn[]>();
|
|
13
|
-
|
|
14
|
-
function off(eventName: string, fn: CallbackFn) {
|
|
15
|
-
const eventFns = handlers.get(eventName);
|
|
16
|
-
|
|
17
|
-
if (eventFns) {
|
|
18
|
-
eventFns.splice(eventFns.indexOf(fn) >>> 0, 1);
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function on(eventName: string, fn: CallbackFn): UnregisterFn {
|
|
23
|
-
let eventFns = handlers.get(eventName);
|
|
24
|
-
|
|
25
|
-
if (!eventFns) {
|
|
26
|
-
eventFns = [fn];
|
|
27
|
-
} else {
|
|
28
|
-
eventFns.push(fn);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
handlers.set(eventName, eventFns);
|
|
32
|
-
|
|
33
|
-
return () => off(eventName, fn);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
function emit<T = Event>(eventName: string, event?: T) {
|
|
37
|
-
const eventFns = handlers.get(eventName);
|
|
38
|
-
|
|
39
|
-
if (eventFns) {
|
|
40
|
-
eventFns.slice().forEach(async (handler) => {
|
|
41
|
-
await handler(event);
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
return {
|
|
47
|
-
on,
|
|
48
|
-
off,
|
|
49
|
-
emit,
|
|
50
|
-
};
|
|
51
|
-
}
|
package/src/utils/mount.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
export function createDefaultMountingTarget(mountingTarget: string) {
|
|
2
|
-
const mountingTargetNode = document.querySelector(mountingTarget);
|
|
3
|
-
if (!mountingTargetNode) {
|
|
4
|
-
const generatedMountingTargetNode = document.createElement('div');
|
|
5
|
-
|
|
6
|
-
if (mountingTarget.startsWith('#')) {
|
|
7
|
-
generatedMountingTargetNode.id = mountingTarget.replace('#', '');
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
if (mountingTarget.startsWith('.')) {
|
|
11
|
-
generatedMountingTargetNode.classList.add(mountingTarget.replace('.', ''));
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
document.body.appendChild(generatedMountingTargetNode);
|
|
15
|
-
}
|
|
16
|
-
}
|
package/tsconfig.json
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": "../../../tsconfig.json",
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
"rootDir": ".",
|
|
5
|
-
"outDir": "dist",
|
|
6
|
-
"target": "esnext",
|
|
7
|
-
"module": "esnext",
|
|
8
|
-
"allowJs": true,
|
|
9
|
-
"importHelpers": true,
|
|
10
|
-
"incremental": false,
|
|
11
|
-
"allowSyntheticDefaultImports": true,
|
|
12
|
-
"resolveJsonModule": true,
|
|
13
|
-
"baseUrl": ".",
|
|
14
|
-
"types": ["vitest/globals", "unplugin-icons/types/vue"],
|
|
15
|
-
"paths": {
|
|
16
|
-
"@/*": ["src/*"],
|
|
17
|
-
"@n8n/chat/*": ["src/*"]
|
|
18
|
-
},
|
|
19
|
-
"lib": ["esnext", "dom", "dom.iterable", "scripthost"],
|
|
20
|
-
// TODO: remove all options below this line
|
|
21
|
-
"useUnknownInCatchVariables": false
|
|
22
|
-
},
|
|
23
|
-
"include": ["src/**/*.ts", "src/**/*.vue", "**/*.d.ts"]
|
|
24
|
-
}
|