-
Notifications
You must be signed in to change notification settings - Fork 5.2k
/
Message.vue
113 lines (98 loc) · 2.54 KB
/
Message.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<script lang="ts" setup>
/* eslint-disable @typescript-eslint/naming-convention */
import type { PropType } from 'vue';
import { computed, toRefs } from 'vue';
import VueMarkdown from 'vue-markdown-render';
import hljs from 'highlight.js/lib/core';
import markdownLink from 'markdown-it-link-attributes';
import type MarkdownIt from 'markdown-it';
import type { ChatMessage } from '@n8n/chat/types';
const props = defineProps({
message: {
type: Object as PropType<ChatMessage>,
required: true,
},
});
const { message } = toRefs(props);
const messageText = computed(() => {
return message.value.text || '<Empty response>';
});
const classes = computed(() => {
return {
'chat-message-from-user': message.value.sender === 'user',
'chat-message-from-bot': message.value.sender === 'bot',
};
});
const linksNewTabPlugin = (vueMarkdownItInstance: MarkdownIt) => {
vueMarkdownItInstance.use(markdownLink, {
attrs: {
target: '_blank',
rel: 'noopener',
},
});
};
const markdownOptions = {
highlight(str: string, lang: string) {
if (lang && hljs.getLanguage(lang)) {
try {
return hljs.highlight(str, { language: lang }).value;
} catch {}
}
return ''; // use external default escaping
},
};
</script>
<template>
<div class="chat-message" :class="classes">
<slot>
<VueMarkdown
class="chat-message-markdown"
:source="messageText"
:options="markdownOptions"
:plugins="[linksNewTabPlugin]"
/>
</slot>
</div>
</template>
<style lang="scss">
.chat-message {
display: block;
max-width: 80%;
padding: var(--chat--message--padding, var(--chat--spacing));
border-radius: var(--chat--message--border-radius, var(--chat--border-radius));
+ .chat-message {
margin-top: var(--chat--message--margin-bottom, calc(var(--chat--spacing) * 0.5));
}
&.chat-message-from-bot {
background-color: var(--chat--message--bot--background);
color: var(--chat--message--bot--color);
border-bottom-left-radius: 0;
}
&.chat-message-from-user {
background-color: var(--chat--message--user--background);
color: var(--chat--message--user--color);
margin-left: auto;
border-bottom-right-radius: 0;
}
> .chat-message-markdown {
display: block;
box-sizing: border-box;
> *:first-child {
margin-top: 0;
}
> *:last-child {
margin-bottom: 0;
}
pre {
font-family: inherit;
font-size: inherit;
margin: 0;
white-space: pre-wrap;
box-sizing: border-box;
padding: var(--chat--spacing);
background: var(--chat--message--pre--background);
border-radius: var(--chat--border-radius);
}
}
}
</style>