361 lines
8.4 KiB
Vue
361 lines
8.4 KiB
Vue
<template>
|
|
<div ref="messageList" class="message-list" role="log" :aria-label="props.ariaLabel" aria-live="polite">
|
|
<div v-if="props.loading" class="list-state loading-state">
|
|
<t-loading size="1.5rem" />
|
|
<span v-text="props.loadingText" />
|
|
</div>
|
|
<slot
|
|
v-else-if="!props.messages.length"
|
|
name="welcome"
|
|
/>
|
|
|
|
<template v-for="(message, index) in props.messages" v-else :key="message.id">
|
|
<slot
|
|
name="message"
|
|
:message="message"
|
|
:index="index"
|
|
:is-generating="props.isGenerating"
|
|
>
|
|
<article class="message-row" :class="message.role">
|
|
<div class="message-header">
|
|
<div class="avatar">
|
|
<slot
|
|
:name="message.role === 'assistant' ? 'assistant-icon' : 'user-icon'"
|
|
:message="message"
|
|
:role="message.role"
|
|
>
|
|
<t-icon :name="message.role === 'assistant' ? 'robot-2' : 'user'" />
|
|
</slot>
|
|
</div>
|
|
<slot name="role-name" :message="message" :role="message.role">
|
|
<div class="role-name" v-text="getRoleName(message)" />
|
|
</slot>
|
|
</div>
|
|
<div class="message-main">
|
|
<div class="bubble" :class="message.role">
|
|
<template v-if="message.role === 'assistant'">
|
|
<slot
|
|
v-for="thinking in message.thinkingList"
|
|
:key="thinking.id"
|
|
name="thinking"
|
|
:message="message"
|
|
:thinking="thinking"
|
|
:is-generating="props.isGenerating && thinking.id === message.thinkingList[message.thinkingList.length - 1]?.id"
|
|
>
|
|
<ai-chat-thinking
|
|
:content="thinking.content"
|
|
:title="props.thinkingTitle"
|
|
:is-generating="props.isGenerating && thinking.id === message.thinkingList[message.thinkingList.length - 1]?.id"
|
|
/>
|
|
</slot>
|
|
</template>
|
|
<div v-if="message.content && message.role === 'assistant'" class="content markdown-content">
|
|
<markdown-view :content="message.content" />
|
|
</div>
|
|
<div v-else-if="message.content" class="content" v-text="message.content" />
|
|
<div v-else-if="message.role === 'assistant' && props.isGenerating" class="typing">
|
|
<span /><span /><span />
|
|
</div>
|
|
<div v-if="$slots.citation && message.role === 'assistant' && message.citationList.length" class="citation-list">
|
|
<template v-for="(citation, citationIndex) in message.citationList" :key="`${message.id}-${citationIndex}`">
|
|
<slot
|
|
name="citation"
|
|
:citation="citation"
|
|
:message="message"
|
|
:index="citationIndex"
|
|
/>
|
|
</template>
|
|
</div>
|
|
<div
|
|
v-if="message.role === 'assistant' && (message.statusText || (!props.isGenerating && message.content))"
|
|
class="message-status"
|
|
>
|
|
<slot name="message-status" :message="message" :text="getMessageStatus(message)">
|
|
<span v-text="getMessageStatus(message)" />
|
|
</slot>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</article>
|
|
</slot>
|
|
</template>
|
|
</div>
|
|
</template>
|
|
|
|
<script setup lang="ts">
|
|
import { nextTick, useTemplateRef } from "vue";
|
|
import { MarkdownView } from "timi-web";
|
|
import AiChatThinking from "./AiChatThinking.vue";
|
|
import type {
|
|
AiChatMessage,
|
|
AiChatMessageStatusFormatter,
|
|
AiChatRoleNameFormatter,
|
|
AiChatRoleNames
|
|
} from "./types";
|
|
|
|
defineOptions({
|
|
name: "AiChatMessageList"
|
|
});
|
|
|
|
export interface Props {
|
|
messages: readonly AiChatMessage[];
|
|
isGenerating: boolean;
|
|
loading?: boolean;
|
|
loadingText?: string;
|
|
ariaLabel?: string;
|
|
roleNames?: AiChatRoleNames;
|
|
roleNameFormatter?: AiChatRoleNameFormatter;
|
|
thinkingTitle?: string;
|
|
createdAtLabel?: string;
|
|
durationLabel?: string;
|
|
formatMessageStatus?: AiChatMessageStatusFormatter;
|
|
}
|
|
|
|
const props = withDefaults(defineProps<Props>(), {
|
|
loading: false,
|
|
loadingText: "正在加载…",
|
|
ariaLabel: "聊天消息",
|
|
roleNames: () => ({ assistant: "助手", user: "我" }),
|
|
thinkingTitle: "正在思考…",
|
|
createdAtLabel: "生成时间:",
|
|
durationLabel: "耗时:"
|
|
});
|
|
|
|
const messageListRef = useTemplateRef<HTMLElement>("messageList");
|
|
|
|
async function scrollToBottom(): Promise<void> {
|
|
await nextTick();
|
|
const element = messageListRef.value;
|
|
if (element) {
|
|
element.scrollTop = element.scrollHeight;
|
|
}
|
|
}
|
|
|
|
function getRoleName(message: AiChatMessage): string {
|
|
return props.roleNameFormatter?.(message.role, message)
|
|
|| props.roleNames[message.role]
|
|
|| message.role;
|
|
}
|
|
|
|
function getMessageStatus(message: AiChatMessage): string {
|
|
if (props.formatMessageStatus) {
|
|
return props.formatMessageStatus(message);
|
|
}
|
|
if (message.statusText) {
|
|
return message.statusText;
|
|
}
|
|
const timeText = `${props.createdAtLabel}${formatDateTime(message.createdAt)}`;
|
|
if (message.duration !== undefined) {
|
|
return `${timeText} · ${props.durationLabel}${formatDuration(message.duration)}`;
|
|
}
|
|
return timeText;
|
|
}
|
|
|
|
function formatDateTime(timestamp: number): string {
|
|
return new Date(timestamp).toLocaleString("zh-CN", { hour12: false });
|
|
}
|
|
|
|
function formatDuration(duration: number): string {
|
|
if (duration < 1000) {
|
|
return `${duration} 毫秒`;
|
|
}
|
|
return `${(duration / 1000).toFixed(1)} 秒`;
|
|
}
|
|
|
|
defineExpose({
|
|
scrollToBottom
|
|
});
|
|
</script>
|
|
|
|
<style scoped lang="less">
|
|
.message-list {
|
|
gap: 1rem;
|
|
flex: none;
|
|
height: 100%;
|
|
display: flex;
|
|
padding: var(--ai-msg-padding-top, .5rem) 1rem calc(var(--ai-chat-bottom-area-height, 4.5rem) + 1.5rem + var(--tui-safe-bottom, 0px));
|
|
min-width: 0;
|
|
min-height: 0;
|
|
box-sizing: border-box;
|
|
overflow-y: auto;
|
|
overflow-x: hidden;
|
|
flex-direction: column;
|
|
|
|
.list-state {
|
|
gap: .5rem;
|
|
margin: auto 0;
|
|
padding: 1rem;
|
|
color: var(--td-text-color-secondary);
|
|
display: flex;
|
|
font-size: .8125rem;
|
|
text-align: center;
|
|
align-items: center;
|
|
justify-content: center;
|
|
}
|
|
|
|
.message-row {
|
|
gap: .5rem;
|
|
display: flex;
|
|
width: 100%;
|
|
min-width: 0;
|
|
align-items: stretch;
|
|
flex-direction: column;
|
|
|
|
&.user {
|
|
.message-header {
|
|
justify-content: flex-end;
|
|
}
|
|
|
|
.role-name {
|
|
order: 1;
|
|
}
|
|
|
|
.avatar {
|
|
order: 2;
|
|
}
|
|
}
|
|
|
|
.message-header {
|
|
gap: .5rem;
|
|
width: 100%;
|
|
display: flex;
|
|
align-items: center;
|
|
}
|
|
|
|
.avatar {
|
|
flex: none;
|
|
color: var(--ai-chat-accent, #0f766e);
|
|
width: 2rem;
|
|
height: 2rem;
|
|
display: flex;
|
|
background: var(--ai-chat-accent-soft, #ccfbf1);
|
|
align-items: center;
|
|
justify-content: center;
|
|
}
|
|
|
|
&.user .avatar {
|
|
color: var(--ai-chat-user-accent, #2563eb);
|
|
background: var(--ai-chat-user-accent-soft, #dbeafe);
|
|
}
|
|
|
|
.message-main {
|
|
width: 100%;
|
|
min-width: 0;
|
|
max-width: none;
|
|
}
|
|
|
|
&.user .message-main {
|
|
text-align: right;
|
|
}
|
|
|
|
.role-name {
|
|
color: var(--td-text-color-secondary);
|
|
margin: 0;
|
|
font-size: .6875rem;
|
|
}
|
|
|
|
.bubble {
|
|
color: var(--td-text-color-primary);
|
|
width: 100%;
|
|
padding: .75rem;
|
|
min-width: 0;
|
|
max-width: 100%;
|
|
box-sizing: border-box;
|
|
border-radius: .25rem 1rem 1rem 1rem;
|
|
background: var(--ai-chat-bubble, #f5f6f8);
|
|
text-align: left;
|
|
box-shadow: 0 .25rem 1rem rgba(15, 23, 42, .05);
|
|
|
|
.thinking + .thinking {
|
|
margin-top: .75rem;
|
|
}
|
|
|
|
&.user {
|
|
color: white;
|
|
border-radius: 1rem .25rem 1rem 1rem;
|
|
background: var(--ai-chat-accent, #0f766e);
|
|
}
|
|
|
|
.content {
|
|
font-size: .875rem;
|
|
line-height: 1.65;
|
|
white-space: pre-wrap;
|
|
word-break: break-word;
|
|
}
|
|
|
|
.markdown-content {
|
|
white-space: normal;
|
|
|
|
:deep(.tui-markdown-view) {
|
|
width: auto;
|
|
max-width: 100%;
|
|
font-size: .875rem;
|
|
line-height: 1.65;
|
|
word-break: break-word;
|
|
overflow-wrap: anywhere;
|
|
overflow-x: hidden;
|
|
}
|
|
|
|
:deep(.tui-markdown-view pre),
|
|
:deep(.tui-markdown-view code) {
|
|
white-space: pre-wrap;
|
|
word-break: break-word;
|
|
overflow-wrap: anywhere;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
.typing {
|
|
gap: .25rem;
|
|
display: flex;
|
|
padding: .25rem .125rem;
|
|
align-items: center;
|
|
|
|
span {
|
|
width: .375rem;
|
|
height: .375rem;
|
|
border-radius: 50%;
|
|
background: var(--ai-chat-accent, #0f766e);
|
|
animation: typing 1.2s infinite ease-in-out;
|
|
|
|
&:nth-child(2) {
|
|
animation-delay: .16s;
|
|
}
|
|
|
|
&:nth-child(3) {
|
|
animation-delay: .32s;
|
|
}
|
|
}
|
|
}
|
|
|
|
.citation-list {
|
|
gap: .375rem;
|
|
margin-top: .75rem;
|
|
padding-top: .625rem;
|
|
display: flex;
|
|
border-top: 1px solid var(--td-component-border);
|
|
flex-direction: column;
|
|
}
|
|
|
|
.message-status {
|
|
color: var(--td-text-color-secondary);
|
|
margin-top: .5rem;
|
|
font-size: .6875rem;
|
|
line-height: 1.4;
|
|
}
|
|
}
|
|
|
|
@keyframes typing {
|
|
0%, 60%, 100% {
|
|
opacity: .35;
|
|
transform: translateY(0);
|
|
}
|
|
30% {
|
|
opacity: 1;
|
|
transform: translateY(-.1875rem);
|
|
}
|
|
}
|
|
|
|
</style>
|