feat(embed): add widget, visitor chat, and management UI (#1657)
Add embed.html entrypoint, floating widget SDK, secure-mode snippets, management panel with preview, visitor chat composables, citation rendering, and integration docs for subdomain and server-side token exchange.
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,507 @@
|
||||
<template>
|
||||
<div class="embed-bot-msg" :class="{ 'is-embedded': embeddedMode }">
|
||||
<DocInfo
|
||||
v-if="session && !session.isAgentMode"
|
||||
:session="session"
|
||||
embedded-mode
|
||||
/>
|
||||
<AgentStreamDisplay
|
||||
v-if="session?.isAgentMode"
|
||||
:session="session"
|
||||
:session-id="sessionId"
|
||||
:user-query="userQuery"
|
||||
:embedded-mode="embeddedMode"
|
||||
:embed-channel-id="embedChannelId"
|
||||
:embed-token="embedToken"
|
||||
/>
|
||||
<template v-else-if="!session?.isAgentMode">
|
||||
<DeepThink v-if="session?.showThink" :deep-session="session" />
|
||||
<div v-if="!session?.hideContent" ref="parentMd">
|
||||
<div v-if="hasActualContent" class="content-wrapper">
|
||||
<div class="ai-markdown-template markdown-content" v-html="renderedHTML" />
|
||||
</div>
|
||||
<div v-if="hasActualContent && !session?.is_completed" class="loading-indicator">
|
||||
<div class="loading-typing">
|
||||
<span></span>
|
||||
<span></span>
|
||||
<span></span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
<Teleport to="body">
|
||||
<div
|
||||
v-if="citationFloat.visible"
|
||||
class="embed-citation-float"
|
||||
:style="{ top: `${citationFloat.top}px`, left: `${citationFloat.left}px` }"
|
||||
@mouseenter="cancelCitationClose"
|
||||
@mouseleave="scheduleCitationClose"
|
||||
>
|
||||
<template v-if="citationFloat.type === 'web'">
|
||||
<div class="embed-citation-float__title">{{ citationFloat.title || citationFloat.url }}</div>
|
||||
<a
|
||||
v-if="citationFloat.url"
|
||||
class="embed-citation-float__link"
|
||||
:href="citationFloat.url"
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
>{{ citationFloat.url }}</a>
|
||||
</template>
|
||||
<template v-else>
|
||||
<div class="embed-citation-float__title">{{ citationFloat.title }}</div>
|
||||
<div v-if="citationFloat.loading" class="embed-citation-float__muted">…</div>
|
||||
<div v-else-if="citationFloat.error" class="embed-citation-float__error">{{ citationFloat.error }}</div>
|
||||
<div v-else class="embed-citation-float__body">{{ citationFloat.content }}</div>
|
||||
</template>
|
||||
</div>
|
||||
</Teleport>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, defineAsyncComponent, nextTick, onMounted, onUpdated, ref, watch } from 'vue'
|
||||
import { marked } from 'marked'
|
||||
import markedKatex from 'marked-katex-extension'
|
||||
import 'katex/dist/katex.min.css'
|
||||
import {
|
||||
sanitizeHTML,
|
||||
safeMarkdownToHTML,
|
||||
createSafeImage,
|
||||
isValidImageURL,
|
||||
hydrateProtectedFileImages,
|
||||
} from '@/utils/security'
|
||||
import { replaceIncompleteImageWithPlaceholder } from '@/utils/chatMessageShared'
|
||||
import {
|
||||
createMermaidCodeRenderer,
|
||||
ensureMermaidInitialized,
|
||||
renderMermaidInContainer,
|
||||
} from '@/utils/mermaidShared'
|
||||
import {
|
||||
extractCitationHtmlPlaceholders,
|
||||
preserveCitationTags,
|
||||
restoreCitationHtmlPlaceholders,
|
||||
restoreCitationTags,
|
||||
} from '@/utils/citationMarkdown'
|
||||
import { useEmbedCitationPopover } from '@/composables/useEmbedCitationPopover'
|
||||
|
||||
const AgentStreamDisplay = defineAsyncComponent(
|
||||
() => import('@/views/chat/components/AgentStreamDisplay.vue'),
|
||||
)
|
||||
const DocInfo = defineAsyncComponent(
|
||||
() => import('@/views/chat/components/docInfo.vue'),
|
||||
)
|
||||
const DeepThink = defineAsyncComponent(
|
||||
() => import('@/views/chat/components/deepThink.vue'),
|
||||
)
|
||||
|
||||
marked.use({ breaks: true })
|
||||
marked.use(markedKatex({ throwOnError: false, nonStandard: true }))
|
||||
ensureMermaidInitialized()
|
||||
|
||||
const preprocessMathDelimiters = (rawText: string): string => {
|
||||
if (!rawText || typeof rawText !== 'string') return ''
|
||||
return rawText
|
||||
.replace(/\\\[([\s\S]*?)\\\]/g, '$$$$$1$$$$')
|
||||
.replace(/\\\(([\s\S]*?)\\\)/g, '$$$1$$')
|
||||
}
|
||||
|
||||
const customRenderer = new marked.Renderer()
|
||||
customRenderer.image = function ({ href, title, text }) {
|
||||
if (!isValidImageURL(href)) return ''
|
||||
return createSafeImage(href, text || '', title || '')
|
||||
}
|
||||
customRenderer.code = createMermaidCodeRenderer('mermaid-embed-botmsg')
|
||||
|
||||
const props = withDefaults(
|
||||
defineProps<{
|
||||
content?: string
|
||||
session?: Record<string, unknown>
|
||||
sessionId?: string
|
||||
userQuery?: string
|
||||
embeddedMode?: boolean
|
||||
embedChannelId?: string
|
||||
embedToken?: string
|
||||
}>(),
|
||||
{
|
||||
content: '',
|
||||
session: () => ({}),
|
||||
sessionId: '',
|
||||
userQuery: '',
|
||||
embeddedMode: true,
|
||||
embedChannelId: '',
|
||||
embedToken: '',
|
||||
},
|
||||
)
|
||||
|
||||
const parentMd = ref<HTMLElement | null>(null)
|
||||
|
||||
const embedChannelIdRef = computed(() => props.embedChannelId)
|
||||
const embedTokenRef = computed(() => props.embedToken)
|
||||
|
||||
const { float: citationFloat, rebind: rebindCitations } = useEmbedCitationPopover(
|
||||
parentMd,
|
||||
embedChannelIdRef,
|
||||
embedTokenRef,
|
||||
)
|
||||
|
||||
let citationCloseTimer: number | null = null
|
||||
const cancelCitationClose = () => {
|
||||
if (citationCloseTimer) {
|
||||
window.clearTimeout(citationCloseTimer)
|
||||
citationCloseTimer = null
|
||||
}
|
||||
}
|
||||
const scheduleCitationClose = () => {
|
||||
cancelCitationClose()
|
||||
citationCloseTimer = window.setTimeout(() => {
|
||||
citationFloat.value.visible = false
|
||||
}, 120)
|
||||
}
|
||||
|
||||
const renderedHTML = computed(() => {
|
||||
const text = String(props.content || props.session?.content || '')
|
||||
if (!text.trim()) return ''
|
||||
const { text: tagSafe, tags } = preserveCitationTags(text)
|
||||
const processed = replaceIncompleteImageWithPlaceholder(tagSafe)
|
||||
const mathSafe = preprocessMathDelimiters(processed)
|
||||
const restoredTags = restoreCitationTags(mathSafe, tags)
|
||||
const safeMarkdown = safeMarkdownToHTML(restoredTags)
|
||||
const { content: mdWithPlaceholders, htmlSnippets } = extractCitationHtmlPlaceholders(safeMarkdown)
|
||||
const html = marked.parse(mdWithPlaceholders, { renderer: customRenderer, breaks: true }) as string
|
||||
const withCitations = restoreCitationHtmlPlaceholders(html, htmlSnippets)
|
||||
return sanitizeHTML(withCitations)
|
||||
})
|
||||
|
||||
const hasActualContent = computed(() => {
|
||||
const text = String(props.content || props.session?.content || '')
|
||||
return text.trim().length > 0
|
||||
})
|
||||
|
||||
const hydrateImages = async () => {
|
||||
const embedCtx =
|
||||
props.embedChannelId && props.embedToken
|
||||
? { channelId: props.embedChannelId, token: props.embedToken }
|
||||
: undefined
|
||||
await hydrateProtectedFileImages(parentMd.value, embedCtx)
|
||||
}
|
||||
|
||||
const renderMermaidDiagrams = async () => {
|
||||
await renderMermaidInContainer(parentMd.value)
|
||||
}
|
||||
|
||||
watch(renderedHTML, () => {
|
||||
nextTick(async () => {
|
||||
rebindCitations()
|
||||
await hydrateImages()
|
||||
if (props.session?.is_completed) {
|
||||
await renderMermaidDiagrams()
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
onUpdated(() => {
|
||||
nextTick(async () => {
|
||||
await hydrateImages()
|
||||
if (props.session?.is_completed) {
|
||||
await renderMermaidDiagrams()
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
onMounted(() => {
|
||||
nextTick(async () => {
|
||||
await hydrateImages()
|
||||
await renderMermaidDiagrams()
|
||||
})
|
||||
})
|
||||
</script>
|
||||
|
||||
<style scoped lang="less">
|
||||
@import '../../components/css/markdown.less';
|
||||
|
||||
.embed-bot-msg {
|
||||
border-radius: 4px;
|
||||
color: var(--td-text-color-primary);
|
||||
font-size: 16px;
|
||||
margin-right: auto;
|
||||
max-width: 100%;
|
||||
box-sizing: border-box;
|
||||
|
||||
&.is-embedded {
|
||||
width: 100%;
|
||||
|
||||
:deep(.agent-stream-display) {
|
||||
width: 100%;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.content-wrapper {
|
||||
background: var(--td-bg-color-container);
|
||||
border-radius: 6px;
|
||||
padding: 8px 0;
|
||||
}
|
||||
|
||||
.ai-markdown-template {
|
||||
font-size: 15px;
|
||||
color: var(--td-text-color-primary);
|
||||
line-height: 1.6;
|
||||
}
|
||||
|
||||
.markdown-content {
|
||||
:deep(p) {
|
||||
margin: 6px 0;
|
||||
line-height: 1.6;
|
||||
}
|
||||
|
||||
:deep(code) {
|
||||
background: var(--td-bg-color-secondarycontainer);
|
||||
padding: 2px 5px;
|
||||
border-radius: 3px;
|
||||
font-family: var(--app-font-family-mono);
|
||||
font-size: 11px;
|
||||
}
|
||||
|
||||
:deep(pre) {
|
||||
background: var(--td-bg-color-secondarycontainer);
|
||||
padding: 10px;
|
||||
border-radius: 4px;
|
||||
overflow-x: auto;
|
||||
margin: 6px 0;
|
||||
|
||||
code {
|
||||
background: none;
|
||||
padding: 0;
|
||||
}
|
||||
}
|
||||
|
||||
:deep(ul),
|
||||
:deep(ol) {
|
||||
margin: 6px 0;
|
||||
padding-left: 20px;
|
||||
}
|
||||
|
||||
:deep(li) {
|
||||
margin: 3px 0;
|
||||
}
|
||||
|
||||
:deep(blockquote) {
|
||||
border-left: 2px solid var(--td-brand-color);
|
||||
padding-left: 10px;
|
||||
margin: 6px 0;
|
||||
color: var(--td-text-color-secondary);
|
||||
}
|
||||
|
||||
:deep(h1),
|
||||
:deep(h2),
|
||||
:deep(h3),
|
||||
:deep(h4),
|
||||
:deep(h5),
|
||||
:deep(h6) {
|
||||
margin: 10px 0 6px;
|
||||
font-weight: 600;
|
||||
color: var(--td-text-color-primary);
|
||||
}
|
||||
|
||||
:deep(a) {
|
||||
color: var(--td-brand-color);
|
||||
text-decoration: none;
|
||||
|
||||
&:hover {
|
||||
text-decoration: underline;
|
||||
}
|
||||
}
|
||||
|
||||
:deep(table) {
|
||||
border-collapse: collapse;
|
||||
margin: 6px 0;
|
||||
font-size: 11px;
|
||||
width: 100%;
|
||||
|
||||
th,
|
||||
td {
|
||||
border: 1px solid var(--td-component-stroke);
|
||||
padding: 5px 8px;
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
th {
|
||||
background: var(--td-bg-color-secondarycontainer);
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
tbody tr:nth-child(even) {
|
||||
background: var(--td-bg-color-secondarycontainer);
|
||||
}
|
||||
}
|
||||
|
||||
:deep(img) {
|
||||
max-width: 80%;
|
||||
max-height: 300px;
|
||||
width: auto;
|
||||
height: auto;
|
||||
border-radius: 8px;
|
||||
display: block;
|
||||
margin: 8px 0;
|
||||
border: 0.5px solid var(--td-component-stroke);
|
||||
object-fit: contain;
|
||||
}
|
||||
|
||||
:deep(.mermaid) {
|
||||
margin: 16px 0;
|
||||
padding: 16px;
|
||||
background: var(--td-bg-color-secondarycontainer);
|
||||
border-radius: 8px;
|
||||
overflow-x: auto;
|
||||
text-align: center;
|
||||
|
||||
svg {
|
||||
max-width: 100%;
|
||||
height: auto;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.loading-indicator {
|
||||
padding: 8px 0;
|
||||
}
|
||||
|
||||
.loading-typing {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 4px;
|
||||
|
||||
span {
|
||||
width: 6px;
|
||||
height: 6px;
|
||||
border-radius: 50%;
|
||||
background: var(--embed-primary, var(--td-brand-color));
|
||||
animation: typingBounce 1.4s ease-in-out infinite;
|
||||
|
||||
&:nth-child(1) {
|
||||
animation-delay: 0s;
|
||||
}
|
||||
|
||||
&:nth-child(2) {
|
||||
animation-delay: 0.2s;
|
||||
}
|
||||
|
||||
&:nth-child(3) {
|
||||
animation-delay: 0.4s;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@keyframes typingBounce {
|
||||
0%,
|
||||
60%,
|
||||
100% {
|
||||
transform: translateY(0);
|
||||
}
|
||||
|
||||
30% {
|
||||
transform: translateY(-8px);
|
||||
}
|
||||
}
|
||||
|
||||
.markdown-content {
|
||||
:deep(.citation) {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 4px;
|
||||
border-radius: 10px;
|
||||
padding: 2px 4px;
|
||||
font-size: 11px;
|
||||
line-height: 1.4;
|
||||
margin: 0 4px;
|
||||
}
|
||||
|
||||
:deep(.citation-web),
|
||||
:deep(.citation-kb) {
|
||||
background: color-mix(in srgb, var(--td-brand-color) 8%, transparent);
|
||||
color: var(--td-brand-color);
|
||||
border: 1px solid color-mix(in srgb, var(--td-brand-color) 20%, transparent);
|
||||
cursor: pointer;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
:deep(.citation-web:hover),
|
||||
:deep(.citation-kb:hover) {
|
||||
background: color-mix(in srgb, var(--td-brand-color) 12%, transparent);
|
||||
border-color: var(--td-brand-color);
|
||||
}
|
||||
|
||||
:deep(.citation .citation-icon) {
|
||||
display: inline-block;
|
||||
width: 14px;
|
||||
height: 14px;
|
||||
background-color: var(--embed-primary, var(--td-brand-color));
|
||||
mask-size: contain;
|
||||
mask-repeat: no-repeat;
|
||||
mask-position: center;
|
||||
-webkit-mask-size: contain;
|
||||
-webkit-mask-repeat: no-repeat;
|
||||
-webkit-mask-position: center;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
:deep(.citation .citation-icon.web) {
|
||||
mask-image: url('@/assets/img/websearch-globe.svg');
|
||||
-webkit-mask-image: url('@/assets/img/websearch-globe.svg');
|
||||
}
|
||||
|
||||
:deep(.citation .citation-icon.kb) {
|
||||
mask-image: url('@/assets/img/zhishiku.svg');
|
||||
-webkit-mask-image: url('@/assets/img/zhishiku.svg');
|
||||
}
|
||||
|
||||
:deep(.citation-tip) {
|
||||
display: none;
|
||||
}
|
||||
|
||||
:deep(a.wiki-content-link) {
|
||||
color: var(--td-brand-color);
|
||||
border-bottom: 1px dashed var(--td-brand-color);
|
||||
text-decoration: none;
|
||||
font-weight: 500;
|
||||
}
|
||||
}
|
||||
|
||||
.embed-citation-float {
|
||||
position: absolute;
|
||||
z-index: 10000;
|
||||
max-width: 320px;
|
||||
padding: 10px 12px;
|
||||
border-radius: 8px;
|
||||
background: var(--td-bg-color-container);
|
||||
box-shadow: 0 6px 18px rgba(0, 0, 0, 0.18);
|
||||
font-size: 12px;
|
||||
line-height: 1.5;
|
||||
color: var(--td-text-color-primary);
|
||||
|
||||
&__title {
|
||||
font-weight: 600;
|
||||
color: var(--td-brand-color);
|
||||
margin-bottom: 4px;
|
||||
}
|
||||
|
||||
&__link {
|
||||
color: var(--td-brand-color);
|
||||
word-break: break-all;
|
||||
}
|
||||
|
||||
&__body {
|
||||
max-height: 200px;
|
||||
overflow-y: auto;
|
||||
white-space: pre-wrap;
|
||||
}
|
||||
|
||||
&__muted {
|
||||
color: var(--td-text-color-placeholder);
|
||||
}
|
||||
|
||||
&__error {
|
||||
color: var(--td-error-color);
|
||||
}
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,490 @@
|
||||
<template>
|
||||
<div class="embed-chat">
|
||||
<div ref="scrollContainer" class="embed-chat__scroll" @scroll="handleScroll">
|
||||
<div class="embed-chat__messages">
|
||||
<div v-if="historyLoading && messagesList.length === 0 && !hasWelcomeText" class="msg-skeleton-list">
|
||||
<div class="msg-skeleton msg-skeleton-user"><div class="sk-line sk-line--short" /></div>
|
||||
<div class="msg-skeleton msg-skeleton-bot">
|
||||
<div class="sk-line" />
|
||||
<div class="sk-line" />
|
||||
<div class="sk-line sk-line--medium" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div
|
||||
v-if="showWelcome"
|
||||
class="embed-welcome-bubble"
|
||||
>
|
||||
<p class="embed-welcome-bubble__text">{{ welcomeText }}</p>
|
||||
</div>
|
||||
|
||||
<div
|
||||
v-if="showSuggestedBlock"
|
||||
class="embed-suggested"
|
||||
>
|
||||
<p v-if="suggestedQuestions.length > 0" class="embed-suggested__title">
|
||||
{{ t('chat.suggestedQuestions') }}
|
||||
</p>
|
||||
<div v-if="suggestedLoading && suggestedQuestions.length === 0" class="embed-suggested__grid">
|
||||
<div v-for="n in 4" :key="`sq-skel-${n}`" class="embed-suggested__card embed-suggested__card--skeleton" />
|
||||
</div>
|
||||
<div v-else-if="suggestedQuestions.length > 0" class="embed-suggested__grid">
|
||||
<button
|
||||
v-for="item in suggestedQuestions"
|
||||
:key="item.question"
|
||||
type="button"
|
||||
class="embed-suggested__card"
|
||||
@click="handleSuggestedClick(item.question)"
|
||||
>
|
||||
<span class="embed-suggested__text">{{ item.question }}</span>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div
|
||||
v-for="(session, index) in messagesList"
|
||||
:key="(session.id as string) || `${session.role}-${session.created_at}-${index}`"
|
||||
class="msg-item-wrapper"
|
||||
>
|
||||
<div v-if="session.role === 'user'">
|
||||
<EmbedUserMessage
|
||||
:content="String(session.content || '')"
|
||||
:mentioned_items="session.mentioned_items"
|
||||
:images="session.images"
|
||||
:attachments="session.attachments"
|
||||
:embeddedMode="true"
|
||||
:embed-channel-id="channelId"
|
||||
:embed-token="token"
|
||||
/>
|
||||
</div>
|
||||
<div v-if="session.role === 'assistant' && shouldRenderAssistantMessage(session)">
|
||||
<EmbedBotMessage
|
||||
:content="String(session.content || '')"
|
||||
:session="session"
|
||||
:session-id="sessionId"
|
||||
:user-query="getUserQuery(index)"
|
||||
:embedded-mode="true"
|
||||
:embed-channel-id="channelId"
|
||||
:embed-token="token"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div v-if="loading" class="embed-chat__typing">
|
||||
<div class="loading-typing">
|
||||
<span></span>
|
||||
<span></span>
|
||||
<span></span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<transition name="scroll-btn-fade">
|
||||
<div v-show="userHasScrolledUp" class="scroll-to-bottom-btn" @click="onClickScrollToBottom" aria-label="scroll to bottom">
|
||||
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" aria-hidden="true">
|
||||
<path d="M6 9l6 6 6-6" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" />
|
||||
</svg>
|
||||
</div>
|
||||
</transition>
|
||||
|
||||
<div class="embed-chat__input">
|
||||
<EmbedInputField
|
||||
:isReplying="isReplying"
|
||||
:show-web-search-toggle="showWebSearchToggle"
|
||||
v-model:web-search-enabled="webSearchEnabled"
|
||||
:show-file-upload-toggle="showFileUploadToggle"
|
||||
@send-msg="onSendMsg"
|
||||
@stop-generation="handleStopGeneration"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, onUnmounted, ref, toRef, watch } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import { getEmbedSuggestedQuestions, onEmbedHostOpenWithQuery, type SuggestedQuestion } from '@/api/embed'
|
||||
import EmbedInputField from '@/components/EmbedInputField.vue'
|
||||
import EmbedBotMessage from '@/views/embed/EmbedBotMessage.vue'
|
||||
import EmbedUserMessage from '@/views/embed/EmbedUserMessage.vue'
|
||||
import { useEmbedChatSession } from '@/composables/useEmbedChatSession'
|
||||
|
||||
const props = defineProps<{
|
||||
sessionId: string
|
||||
sessionSig: string
|
||||
channelId: string
|
||||
token: string
|
||||
agentId: string
|
||||
kbIds: string[]
|
||||
welcomeMessage?: string
|
||||
showSuggestedQuestions?: boolean
|
||||
allowWebSearch?: boolean
|
||||
agentWebSearchEnabled?: boolean
|
||||
allowFileUpload?: boolean
|
||||
agentImageUploadEnabled?: boolean
|
||||
useSessionHeaderTitle?: boolean
|
||||
hostContext?: Record<string, unknown>
|
||||
}>()
|
||||
|
||||
const emit = defineEmits<{
|
||||
(e: 'session-title', title: string): void
|
||||
(e: 'messages-state', hasMessages: boolean): void
|
||||
}>()
|
||||
|
||||
const { t } = useI18n()
|
||||
const sessionIdRef = toRef(props, 'sessionId')
|
||||
const sessionSigRef = toRef(props, 'sessionSig')
|
||||
const suggestedQuestions = ref<SuggestedQuestion[]>([])
|
||||
const suggestedLoading = ref(false)
|
||||
const hostContextRef = ref<Record<string, unknown>>(props.hostContext || {})
|
||||
|
||||
const embedWebSearchStorageKey = () => `weknora-embed-web-search:${props.channelId}`
|
||||
|
||||
const readStoredWebSearchEnabled = () => {
|
||||
if (typeof localStorage === 'undefined') return false
|
||||
return localStorage.getItem(embedWebSearchStorageKey()) === '1'
|
||||
}
|
||||
|
||||
const webSearchEnabled = ref(readStoredWebSearchEnabled())
|
||||
|
||||
const showWebSearchToggle = computed(
|
||||
() => props.allowWebSearch === true && props.agentWebSearchEnabled === true,
|
||||
)
|
||||
const showFileUploadToggle = computed(
|
||||
() => props.allowFileUpload === true && props.agentImageUploadEnabled === true,
|
||||
)
|
||||
|
||||
watch(webSearchEnabled, (enabled) => {
|
||||
if (!showWebSearchToggle.value) return
|
||||
if (typeof localStorage === 'undefined') return
|
||||
localStorage.setItem(embedWebSearchStorageKey(), enabled ? '1' : '0')
|
||||
})
|
||||
|
||||
watch(showWebSearchToggle, (visible) => {
|
||||
if (!visible) {
|
||||
webSearchEnabled.value = false
|
||||
}
|
||||
})
|
||||
|
||||
watch(() => props.hostContext, (ctx) => {
|
||||
hostContextRef.value = ctx || {}
|
||||
}, { deep: true })
|
||||
|
||||
const {
|
||||
messagesList,
|
||||
loading,
|
||||
isReplying,
|
||||
historyLoading,
|
||||
scrollContainer,
|
||||
userHasScrolledUp,
|
||||
shouldRenderAssistantMessage,
|
||||
getUserQuery,
|
||||
handleScroll,
|
||||
scrollToBottom,
|
||||
onClickScrollToBottom,
|
||||
sendMsg,
|
||||
handleStopGeneration,
|
||||
} = useEmbedChatSession({
|
||||
sessionId: sessionIdRef,
|
||||
sessionSig: sessionSigRef,
|
||||
channelId: props.channelId,
|
||||
token: props.token,
|
||||
agentId: props.agentId,
|
||||
kbIds: props.kbIds,
|
||||
allowWebSearch: props.allowWebSearch,
|
||||
allowFileUpload: props.allowFileUpload,
|
||||
hostContext: hostContextRef,
|
||||
onMessagesChange: (has) => emit('messages-state', has),
|
||||
onSessionTitle: (title) => {
|
||||
if (props.useSessionHeaderTitle) {
|
||||
emit('session-title', title)
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
const welcomeText = computed(() => props.welcomeMessage?.trim() || '')
|
||||
const hasWelcomeText = computed(() => welcomeText.value.length > 0)
|
||||
|
||||
const hasUserMessage = computed(() =>
|
||||
messagesList.some((m) => m.role === 'user'))
|
||||
|
||||
/** 访客未发言前始终展示欢迎语(含历史加载中),发送后隐藏 */
|
||||
const showWelcome = computed(() => hasWelcomeText.value && !hasUserMessage.value)
|
||||
|
||||
const showSuggestedBlock = computed(() =>
|
||||
props.showSuggestedQuestions
|
||||
&& !hasUserMessage.value
|
||||
&& !loading.value
|
||||
&& !historyLoading.value
|
||||
&& (suggestedLoading.value || suggestedQuestions.value.length > 0))
|
||||
|
||||
const fetchSuggestedQuestions = async () => {
|
||||
if (!props.showSuggestedQuestions || !props.channelId || !props.token) {
|
||||
suggestedQuestions.value = []
|
||||
return
|
||||
}
|
||||
suggestedLoading.value = true
|
||||
try {
|
||||
const res = await getEmbedSuggestedQuestions(props.channelId, props.token, 6)
|
||||
suggestedQuestions.value = res?.data?.questions || []
|
||||
} catch {
|
||||
suggestedQuestions.value = []
|
||||
} finally {
|
||||
suggestedLoading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
const onSendMsg = (query: string, imageFiles: File[] = [], attachmentFiles: File[] = []) => {
|
||||
void sendMsg(query, {
|
||||
webSearchEnabled: webSearchEnabled.value,
|
||||
imageFiles,
|
||||
attachmentFiles,
|
||||
})
|
||||
}
|
||||
|
||||
const handleSuggestedClick = (question: string) => {
|
||||
const text = question.trim()
|
||||
if (!text || isReplying.value) return
|
||||
void sendMsg(text, { webSearchEnabled: webSearchEnabled.value })
|
||||
}
|
||||
|
||||
let removeOpenQueryListener: (() => void) | null = null
|
||||
|
||||
onMounted(() => {
|
||||
fetchSuggestedQuestions()
|
||||
removeOpenQueryListener = onEmbedHostOpenWithQuery((query) => {
|
||||
if (isReplying.value) return
|
||||
void sendMsg(query, { webSearchEnabled: webSearchEnabled.value })
|
||||
})
|
||||
})
|
||||
|
||||
onUnmounted(() => {
|
||||
removeOpenQueryListener?.()
|
||||
})
|
||||
|
||||
watch(
|
||||
() => [props.showSuggestedQuestions, props.channelId, props.token] as const,
|
||||
() => { fetchSuggestedQuestions() },
|
||||
)
|
||||
</script>
|
||||
|
||||
<style scoped lang="less">
|
||||
.embed-chat {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
flex: 1;
|
||||
min-height: 0;
|
||||
width: 100%;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.embed-chat__scroll {
|
||||
flex: 1;
|
||||
min-height: 0;
|
||||
width: 100%;
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
.embed-chat__messages {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 16px;
|
||||
max-width: 800px;
|
||||
margin: 0 auto;
|
||||
width: 100%;
|
||||
padding: 12px 16px 0;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
.embed-suggested {
|
||||
width: 100%;
|
||||
|
||||
&__title {
|
||||
margin: 0 0 8px;
|
||||
font-size: 13px;
|
||||
font-weight: 500;
|
||||
color: var(--td-text-color-secondary);
|
||||
}
|
||||
|
||||
&__grid {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
&__card {
|
||||
display: block;
|
||||
width: 100%;
|
||||
padding: 10px 12px;
|
||||
border: 1px solid var(--td-component-stroke);
|
||||
border-radius: 10px;
|
||||
background: var(--td-bg-color-container);
|
||||
text-align: left;
|
||||
cursor: pointer;
|
||||
transition: border-color 0.15s ease, background 0.15s ease;
|
||||
|
||||
&:hover {
|
||||
border-color: var(--td-brand-color-3, var(--td-brand-color));
|
||||
background: color-mix(in srgb, var(--td-brand-color) 4%, var(--td-bg-color-container));
|
||||
}
|
||||
|
||||
&--skeleton {
|
||||
height: 40px;
|
||||
cursor: default;
|
||||
background: linear-gradient(90deg, #f0f0f0 25%, #e6e6e6 50%, #f0f0f0 75%);
|
||||
background-size: 200% 100%;
|
||||
animation: sk-shimmer 1.2s ease-in-out infinite;
|
||||
border: none;
|
||||
}
|
||||
}
|
||||
|
||||
&__text {
|
||||
font-size: 13px;
|
||||
line-height: 1.45;
|
||||
color: var(--td-text-color-primary);
|
||||
}
|
||||
}
|
||||
|
||||
.embed-welcome-bubble {
|
||||
display: flex;
|
||||
justify-content: flex-start;
|
||||
width: 100%;
|
||||
animation: welcome-in 0.28s ease both;
|
||||
|
||||
&__text {
|
||||
margin: 0;
|
||||
max-width: min(88%, 520px);
|
||||
padding: 10px 14px;
|
||||
font-size: 14px;
|
||||
line-height: 1.55;
|
||||
color: var(--td-text-color-primary);
|
||||
white-space: pre-wrap;
|
||||
word-break: break-word;
|
||||
background: color-mix(
|
||||
in srgb,
|
||||
var(--embed-primary, var(--td-brand-color)) 7%,
|
||||
var(--td-bg-color-container, #fff)
|
||||
);
|
||||
border: 1px solid color-mix(
|
||||
in srgb,
|
||||
var(--embed-primary, var(--td-brand-color)) 14%,
|
||||
var(--td-component-stroke, #e7e7e7)
|
||||
);
|
||||
border-radius: 4px 14px 14px 14px;
|
||||
box-shadow: 0 1px 2px rgba(15, 23, 42, 0.04);
|
||||
}
|
||||
}
|
||||
|
||||
@keyframes welcome-in {
|
||||
from {
|
||||
opacity: 0;
|
||||
transform: translateY(6px);
|
||||
}
|
||||
to {
|
||||
opacity: 1;
|
||||
transform: translateY(0);
|
||||
}
|
||||
}
|
||||
|
||||
.embed-chat__typing {
|
||||
height: 41px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
padding-left: 4px;
|
||||
}
|
||||
|
||||
.embed-chat__input {
|
||||
flex-shrink: 0;
|
||||
padding: 8px 16px 16px;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
.msg-skeleton-list {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 20px;
|
||||
padding: 8px 0;
|
||||
}
|
||||
|
||||
.msg-skeleton-user {
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
}
|
||||
|
||||
.msg-skeleton-bot {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 8px;
|
||||
padding-left: 4px;
|
||||
}
|
||||
|
||||
.sk-line {
|
||||
height: 14px;
|
||||
border-radius: 6px;
|
||||
background: linear-gradient(90deg, #f0f0f0 25%, #e6e6e6 50%, #f0f0f0 75%);
|
||||
background-size: 200% 100%;
|
||||
animation: sk-shimmer 1.2s ease-in-out infinite;
|
||||
}
|
||||
|
||||
.sk-line--short { width: 45%; height: 36px; }
|
||||
.sk-line--medium { width: 60%; }
|
||||
|
||||
@keyframes sk-shimmer {
|
||||
0% { background-position: 200% 0; }
|
||||
100% { background-position: -200% 0; }
|
||||
}
|
||||
|
||||
.loading-typing {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 4px;
|
||||
|
||||
span {
|
||||
width: 6px;
|
||||
height: 6px;
|
||||
border-radius: 50%;
|
||||
background: var(--embed-primary, #0052d9);
|
||||
animation: typingBounce 1.4s ease-in-out infinite;
|
||||
|
||||
&:nth-child(1) { animation-delay: 0s; }
|
||||
&:nth-child(2) { animation-delay: 0.2s; }
|
||||
&:nth-child(3) { animation-delay: 0.4s; }
|
||||
}
|
||||
}
|
||||
|
||||
@keyframes typingBounce {
|
||||
0%, 60%, 100% { transform: translateY(0); }
|
||||
30% { transform: translateY(-8px); }
|
||||
}
|
||||
|
||||
.scroll-to-bottom-btn {
|
||||
position: absolute;
|
||||
left: 50%;
|
||||
transform: translateX(-50%);
|
||||
bottom: 100px;
|
||||
z-index: 10;
|
||||
width: 36px;
|
||||
height: 36px;
|
||||
border-radius: 50%;
|
||||
background: var(--td-bg-color-container);
|
||||
border: 1px solid var(--td-component-stroke);
|
||||
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
cursor: pointer;
|
||||
color: var(--td-text-color-secondary);
|
||||
}
|
||||
|
||||
.scroll-btn-fade-enter-active,
|
||||
.scroll-btn-fade-leave-active {
|
||||
transition: opacity 0.2s ease, transform 0.2s ease;
|
||||
}
|
||||
|
||||
.scroll-btn-fade-enter-from,
|
||||
.scroll-btn-fade-leave-to {
|
||||
opacity: 0;
|
||||
transform: translateX(-50%) translateY(8px);
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,48 @@
|
||||
<template>
|
||||
<EmbedChatCore
|
||||
:session-id="sessionId"
|
||||
:session-sig="sessionSig"
|
||||
:channel-id="channelId"
|
||||
:token="token"
|
||||
:agent-id="agentId"
|
||||
:kb-ids="kbIds"
|
||||
:welcome-message="welcomeMessage"
|
||||
:show-suggested-questions="showSuggestedQuestions"
|
||||
:allow-web-search="allowWebSearch"
|
||||
:agent-web-search-enabled="agentWebSearchEnabled"
|
||||
:allow-file-upload="allowFileUpload"
|
||||
:agent-image-upload-enabled="agentImageUploadEnabled"
|
||||
:use-session-header-title="useSessionHeaderTitle"
|
||||
:host-context="hostContext"
|
||||
@session-title="(title: string) => emit('session-title', title)"
|
||||
@messages-state="(has: boolean) => emit('messages-state', has)"
|
||||
/>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { defineAsyncComponent } from 'vue'
|
||||
|
||||
const EmbedChatCore = defineAsyncComponent(() => import('@/views/embed/EmbedChatCore.vue'))
|
||||
|
||||
defineProps<{
|
||||
sessionId: string
|
||||
sessionSig: string
|
||||
channelId: string
|
||||
token: string
|
||||
agentId: string
|
||||
kbIds: string[]
|
||||
welcomeMessage?: string
|
||||
showSuggestedQuestions?: boolean
|
||||
allowWebSearch?: boolean
|
||||
agentWebSearchEnabled?: boolean
|
||||
allowFileUpload?: boolean
|
||||
agentImageUploadEnabled?: boolean
|
||||
useSessionHeaderTitle?: boolean
|
||||
hostContext?: Record<string, unknown>
|
||||
}>()
|
||||
|
||||
const emit = defineEmits<{
|
||||
(e: 'session-title', title: string): void
|
||||
(e: 'messages-state', hasMessages: boolean): void
|
||||
}>()
|
||||
</script>
|
||||
@@ -0,0 +1,256 @@
|
||||
<template>
|
||||
<div class="embed-page" :style="pageStyle">
|
||||
<div v-if="loadError" class="embed-error">{{ loadError }}</div>
|
||||
<template v-else-if="config">
|
||||
<header v-if="sessionId" class="embed-header">
|
||||
<span class="embed-header__badge" :style="badgeStyle">
|
||||
<span v-if="config.agent_avatar" class="embed-header__avatar">{{ config.agent_avatar }}</span>
|
||||
<t-icon v-else :name="headerIcon" size="18px" />
|
||||
</span>
|
||||
<div class="embed-header__text">
|
||||
<h1 class="embed-header__title">{{ headerTitle }}</h1>
|
||||
<p v-if="headerSubtitle" class="embed-header__subtitle">{{ headerSubtitle }}</p>
|
||||
</div>
|
||||
<t-button
|
||||
variant="text"
|
||||
shape="square"
|
||||
size="small"
|
||||
class="embed-header__action"
|
||||
:disabled="!chatHasMessages"
|
||||
:title="$t('embedPublish.newChat')"
|
||||
:aria-label="$t('embedPublish.newChat')"
|
||||
@click="handleNewChat"
|
||||
>
|
||||
<template #icon><t-icon name="add" /></template>
|
||||
</t-button>
|
||||
</header>
|
||||
|
||||
<EmbedChatView
|
||||
v-if="sessionId"
|
||||
:session-id="sessionId"
|
||||
:session-sig="sessionSig"
|
||||
:channel-id="channelId"
|
||||
:token="token"
|
||||
:agent-id="config.agent_id"
|
||||
:kb-ids="kbIds"
|
||||
:welcome-message="config.welcome_message"
|
||||
:show-suggested-questions="config.show_suggested_questions !== false"
|
||||
:allow-web-search="config.allow_web_search === true"
|
||||
:agent-web-search-enabled="config.agent_web_search_enabled === true"
|
||||
:allow-file-upload="config.allow_file_upload === true"
|
||||
:agent-image-upload-enabled="config.agent_image_upload_enabled === true"
|
||||
:use-session-header-title="useSessionHeaderTitle"
|
||||
:host-context="hostContext"
|
||||
@session-title="sessionTitle = $event"
|
||||
@messages-state="chatHasMessages = $event"
|
||||
/>
|
||||
<div v-else class="embed-loading">{{ $t('embedPublish.loading') }}</div>
|
||||
</template>
|
||||
<div v-else-if="awaitingToken" class="embed-loading">{{ $t('embedPublish.awaitingToken') }}</div>
|
||||
<div v-else-if="bootstrapping" class="embed-loading">{{ $t('embedPublish.loading') }}</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, ref, watch } from 'vue'
|
||||
import { useRoute } from 'vue-router'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import EmbedChatView from '@/views/embed/EmbedChatView.vue'
|
||||
import { useEmbedBridge } from '@/composables/useEmbedBridge'
|
||||
|
||||
const { t } = useI18n()
|
||||
const route = useRoute()
|
||||
const channelId = ref(String(route.params.channelId || ''))
|
||||
const sessionTitle = ref('')
|
||||
const chatHasMessages = ref(false)
|
||||
|
||||
const {
|
||||
token,
|
||||
config,
|
||||
sessionId,
|
||||
sessionSig,
|
||||
loadError,
|
||||
awaitingToken,
|
||||
bootstrapping,
|
||||
hostContext,
|
||||
startNewSession,
|
||||
} = useEmbedBridge(channelId)
|
||||
|
||||
const handleNewChat = () => {
|
||||
// The current session is already empty — reuse it instead of spawning yet
|
||||
// another blank session (which would otherwise pile up server-side).
|
||||
if (!chatHasMessages.value) return
|
||||
sessionTitle.value = ''
|
||||
startNewSession()
|
||||
}
|
||||
|
||||
const kbIds = computed(() => config.value?.knowledge_base_ids ?? [])
|
||||
|
||||
const pageStyle = computed(() => {
|
||||
const color = config.value?.primary_color
|
||||
if (!color) return {}
|
||||
return {
|
||||
'--embed-primary': color,
|
||||
'--td-brand-color': color,
|
||||
'--td-brand-color-hover': color,
|
||||
'--td-brand-color-active': color,
|
||||
} as Record<string, string>
|
||||
})
|
||||
|
||||
const badgeStyle = computed(() => {
|
||||
const color = config.value?.primary_color
|
||||
if (!color) return {}
|
||||
return {
|
||||
background: `color-mix(in srgb, ${color} 12%, transparent)`,
|
||||
color,
|
||||
} as Record<string, string>
|
||||
})
|
||||
|
||||
const channelDisplayTitle = computed(() => {
|
||||
const cfg = config.value
|
||||
if (!cfg) return ''
|
||||
return (
|
||||
cfg.display_title?.trim()
|
||||
|| cfg.page_title?.trim()
|
||||
|| cfg.name?.trim()
|
||||
|| cfg.agent_name?.trim()
|
||||
|| t('embedPublish.defaultChatTitle')
|
||||
)
|
||||
})
|
||||
|
||||
const useSessionHeaderTitle = computed(
|
||||
() => config.value?.header_title_mode === 'session',
|
||||
)
|
||||
|
||||
const headerTitle = computed(() => {
|
||||
if (useSessionHeaderTitle.value && sessionTitle.value.trim()) {
|
||||
return sessionTitle.value.trim()
|
||||
}
|
||||
return channelDisplayTitle.value
|
||||
})
|
||||
|
||||
const headerSubtitle = computed(() => {
|
||||
const cfg = config.value
|
||||
if (!cfg?.agent_name) return ''
|
||||
if (useSessionHeaderTitle.value && sessionTitle.value.trim()) {
|
||||
const fallback = channelDisplayTitle.value
|
||||
if (fallback && fallback !== sessionTitle.value.trim()) {
|
||||
return fallback
|
||||
}
|
||||
return cfg.agent_name
|
||||
}
|
||||
const channelName = cfg.name?.trim()
|
||||
if (!channelName || channelName === channelDisplayTitle.value) return ''
|
||||
return cfg.agent_name
|
||||
})
|
||||
|
||||
const headerIcon = computed(() => {
|
||||
const agentId = config.value?.agent_id || ''
|
||||
return agentId && agentId !== 'builtin-quick-answer' ? 'control-platform' : 'chat'
|
||||
})
|
||||
|
||||
watch(headerTitle, (title) => {
|
||||
if (title) document.title = title
|
||||
}, { immediate: true })
|
||||
</script>
|
||||
|
||||
<style scoped lang="less">
|
||||
.embed-page {
|
||||
height: 100vh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
background: var(--td-bg-color-container, #fff);
|
||||
overflow: hidden;
|
||||
/* 子组件(含 AgentStreamDisplay)内凡用 --td-brand-color 的 loading / 强调色均跟随渠道主题 */
|
||||
--td-brand-color: var(--embed-primary, var(--td-brand-color));
|
||||
--td-brand-color-hover: var(--embed-primary, var(--td-brand-color-hover));
|
||||
--td-brand-color-active: var(--embed-primary, var(--td-brand-color-active));
|
||||
|
||||
:deep(.t-button--theme-primary) {
|
||||
--td-brand-color: var(--embed-primary, var(--td-brand-color));
|
||||
--td-brand-color-hover: var(--embed-primary, var(--td-brand-color-hover));
|
||||
--td-brand-color-active: var(--embed-primary, var(--td-brand-color-active));
|
||||
}
|
||||
|
||||
:deep(.embed-input-box:focus-within) {
|
||||
border-color: var(--embed-primary, var(--td-brand-color));
|
||||
}
|
||||
|
||||
:deep(.embed-send-btn:not(.disabled)) {
|
||||
background: var(--embed-primary, var(--td-brand-color));
|
||||
}
|
||||
|
||||
:deep(.loading-typing span) {
|
||||
background: var(--embed-primary, var(--td-brand-color));
|
||||
}
|
||||
}
|
||||
|
||||
.embed-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
padding: 12px 16px;
|
||||
border-bottom: 1px solid var(--td-component-stroke);
|
||||
background: var(--td-bg-color-container);
|
||||
flex-shrink: 0;
|
||||
|
||||
&__badge {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
width: 36px;
|
||||
height: 36px;
|
||||
border-radius: 10px;
|
||||
flex-shrink: 0;
|
||||
background: color-mix(in srgb, var(--td-brand-color) 10%, transparent);
|
||||
color: var(--td-brand-color);
|
||||
}
|
||||
|
||||
&__avatar {
|
||||
font-size: 20px;
|
||||
line-height: 1;
|
||||
}
|
||||
|
||||
&__text {
|
||||
min-width: 0;
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
&__action {
|
||||
flex-shrink: 0;
|
||||
color: var(--td-text-color-secondary);
|
||||
|
||||
&:hover {
|
||||
color: var(--td-brand-color);
|
||||
}
|
||||
}
|
||||
|
||||
&__title {
|
||||
margin: 0;
|
||||
font-size: 15px;
|
||||
font-weight: 600;
|
||||
line-height: 1.35;
|
||||
color: var(--td-text-color-primary);
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
&__subtitle {
|
||||
margin: 2px 0 0;
|
||||
font-size: 12px;
|
||||
line-height: 1.4;
|
||||
color: var(--td-text-color-secondary);
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
}
|
||||
|
||||
.embed-error,
|
||||
.embed-loading {
|
||||
padding: 24px;
|
||||
text-align: center;
|
||||
color: var(--td-text-color-placeholder);
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,179 @@
|
||||
<template>
|
||||
<div ref="containerRef" class="embed-user-msg" :class="{ 'is-embedded': embeddedMode }">
|
||||
<div v-if="hasImages" class="user_images">
|
||||
<img
|
||||
v-for="(img, idx) in displayImages"
|
||||
:key="idx"
|
||||
:src="img.url"
|
||||
class="user_image_thumb"
|
||||
alt=""
|
||||
@click="previewImage($event)"
|
||||
/>
|
||||
</div>
|
||||
<div v-if="hasAttachments" class="user_attachments">
|
||||
<div v-for="(att, idx) in attachments" :key="idx" class="user_attachment_card">
|
||||
<div class="attachment_card_info">
|
||||
<div class="attachment_card_name">{{ att.file_name }}</div>
|
||||
<div v-if="att.file_size" class="attachment_card_meta">{{ formatFileSize(att.file_size) }}</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="user_msg">{{ content }}</div>
|
||||
<picturePreview :reviewImg="reviewImg" :reviewUrl="reviewUrl" @closePreImg="closePreImg" />
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, nextTick, onMounted, ref, watch } from 'vue'
|
||||
import { hydrateProtectedFileImages } from '@/utils/security'
|
||||
import picturePreview from '@/components/picture-preview.vue'
|
||||
|
||||
type EmbedImage = { url?: string; data?: string }
|
||||
type EmbedAttachment = { file_name: string; file_size?: number }
|
||||
|
||||
const props = withDefaults(
|
||||
defineProps<{
|
||||
content?: string
|
||||
mentioned_items?: unknown[]
|
||||
images?: EmbedImage[]
|
||||
attachments?: EmbedAttachment[]
|
||||
embeddedMode?: boolean
|
||||
embedChannelId?: string
|
||||
embedToken?: string
|
||||
}>(),
|
||||
{
|
||||
content: '',
|
||||
mentioned_items: () => [],
|
||||
images: () => [],
|
||||
attachments: () => [],
|
||||
embeddedMode: true,
|
||||
embedChannelId: '',
|
||||
embedToken: '',
|
||||
},
|
||||
)
|
||||
|
||||
const containerRef = ref<HTMLElement | null>(null)
|
||||
const reviewImg = ref(false)
|
||||
const reviewUrl = ref('')
|
||||
|
||||
const displayImages = computed(() =>
|
||||
(props.images || [])
|
||||
.map((img) => ({ url: String(img.url || img.data || '').trim() }))
|
||||
.filter((img) => img.url.length > 0),
|
||||
)
|
||||
|
||||
const hasImages = computed(() => displayImages.value.length > 0)
|
||||
const hasAttachments = computed(() => (props.attachments?.length ?? 0) > 0)
|
||||
|
||||
const hydrateImages = async () => {
|
||||
await nextTick()
|
||||
if (!props.embedChannelId || !props.embedToken) return
|
||||
await hydrateProtectedFileImages(containerRef.value, {
|
||||
channelId: props.embedChannelId,
|
||||
token: props.embedToken,
|
||||
})
|
||||
}
|
||||
|
||||
watch(() => props.images, hydrateImages, { deep: true })
|
||||
onMounted(hydrateImages)
|
||||
|
||||
const previewImage = (event: MouseEvent) => {
|
||||
const src = (event.target as HTMLImageElement | null)?.src
|
||||
if (!src) return
|
||||
reviewUrl.value = src
|
||||
reviewImg.value = true
|
||||
}
|
||||
|
||||
const closePreImg = () => {
|
||||
reviewImg.value = false
|
||||
reviewUrl.value = ''
|
||||
}
|
||||
|
||||
const formatFileSize = (bytes: number): string => {
|
||||
if (!bytes) return ''
|
||||
if (bytes < 1024) return `${bytes} B`
|
||||
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`
|
||||
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped lang="less">
|
||||
.embed-user-msg {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: flex-end;
|
||||
gap: 6px;
|
||||
width: 100%;
|
||||
|
||||
&.is-embedded .user_msg {
|
||||
max-width: 100%;
|
||||
}
|
||||
}
|
||||
|
||||
.user_msg {
|
||||
width: max-content;
|
||||
max-width: 776px;
|
||||
padding: 10px 12px;
|
||||
border-radius: 4px;
|
||||
background: #8ce97f;
|
||||
margin-left: auto;
|
||||
color: #000000e6;
|
||||
font-size: 15px;
|
||||
text-align: justify;
|
||||
word-break: break-all;
|
||||
box-sizing: border-box;
|
||||
white-space: pre-wrap;
|
||||
}
|
||||
|
||||
.user_images {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 6px;
|
||||
justify-content: flex-end;
|
||||
max-width: 100%;
|
||||
}
|
||||
|
||||
.user_image_thumb {
|
||||
width: 120px;
|
||||
height: 120px;
|
||||
object-fit: cover;
|
||||
border-radius: 6px;
|
||||
border: 1px solid var(--td-border-level-2-color, #e7e7e7);
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.user_attachments {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 8px;
|
||||
justify-content: flex-end;
|
||||
max-width: 100%;
|
||||
}
|
||||
|
||||
.user_attachment_card {
|
||||
padding: 8px 12px;
|
||||
border-radius: 8px;
|
||||
border: 1px solid var(--td-border-level-1-color, #e7e7e7);
|
||||
background: var(--td-bg-color-container, #fff);
|
||||
max-width: 260px;
|
||||
min-width: 120px;
|
||||
}
|
||||
|
||||
.attachment_card_name {
|
||||
font-size: 13px;
|
||||
font-weight: 500;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.attachment_card_meta {
|
||||
font-size: 11px;
|
||||
color: var(--td-text-color-secondary, #999);
|
||||
}
|
||||
|
||||
html[theme-mode='dark'] .user_msg {
|
||||
background: var(--td-brand-color-3);
|
||||
color: rgba(255, 255, 255, 0.9);
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user