mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-23 23:19:19 +08:00
feat: 用户消息中的实体引用展示为标签
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
<script lang="ts" setup>
|
||||
import { computed } from 'vue';
|
||||
|
||||
import {
|
||||
catalogCiteLabel,
|
||||
parseMessageCiteSegments,
|
||||
} from './catalog-cite';
|
||||
|
||||
/** 把消息正文里的实体引用渲染成标签(兼容 token / 展开 line)。 */
|
||||
const props = defineProps<{
|
||||
text: string;
|
||||
}>();
|
||||
|
||||
const segments = computed(() => parseMessageCiteSegments(props.text || ''));
|
||||
const hasCite = computed(() => segments.value.some((s) => s.type === 'cite'));
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<span class="cite-rich-text" :class="{ 'cite-rich-text--plain': !hasCite }">
|
||||
<template v-for="(seg, index) in segments" :key="index">
|
||||
<span v-if="seg.type === 'text'" class="cite-rich-text__text">{{
|
||||
seg.value
|
||||
}}</span>
|
||||
<span
|
||||
v-else
|
||||
class="cite-msg-chip"
|
||||
:title="seg.id != null ? `id=${seg.id}` : undefined"
|
||||
>
|
||||
<span class="cite-msg-chip__kind">{{ catalogCiteLabel(seg.kind) }}</span>
|
||||
<span class="cite-msg-chip__title">{{ seg.title }}</span>
|
||||
</span>
|
||||
</template>
|
||||
</span>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.cite-rich-text {
|
||||
display: inline;
|
||||
word-break: break-word;
|
||||
white-space: pre-wrap;
|
||||
}
|
||||
|
||||
.cite-msg-chip {
|
||||
display: inline-flex;
|
||||
max-width: 14rem;
|
||||
align-items: center;
|
||||
gap: 4px;
|
||||
margin: 0 2px;
|
||||
padding: 0 6px;
|
||||
vertical-align: baseline;
|
||||
font-size: 12px;
|
||||
line-height: 20px;
|
||||
color: hsl(var(--primary));
|
||||
white-space: nowrap;
|
||||
background: hsl(var(--primary) / 0.12);
|
||||
border-radius: 6px;
|
||||
}
|
||||
|
||||
.cite-msg-chip__kind {
|
||||
flex-shrink: 0;
|
||||
opacity: 0.7;
|
||||
}
|
||||
|
||||
.cite-msg-chip__title {
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
</style>
|
||||
@@ -190,6 +190,63 @@ export function composeMessageWithCites(text: string, cites: CatalogCite[]) {
|
||||
return trimmed ? `${trimmed}\n${block}` : block;
|
||||
}
|
||||
|
||||
/** 消息气泡展示用分段(token / 展开后的 line 都能识别) */
|
||||
export type MessageCiteSegment =
|
||||
| { type: 'text'; value: string }
|
||||
| { type: 'cite'; kind: CatalogCiteKind; id?: number; title: string };
|
||||
|
||||
/**
|
||||
* 把消息正文拆成文本 + 引用标签。
|
||||
* 支持 `@[店铺#1·名]` 与 `店铺「名」 id=1 平台=…` 两种形态。
|
||||
*/
|
||||
export function parseMessageCiteSegments(text: string): MessageCiteSegment[] {
|
||||
const source = text.replace(/^\s*\[引用\]\s*/m, '');
|
||||
if (!source) return [{ type: 'text', value: '' }];
|
||||
|
||||
// token 或已展开 line;attrs 吃掉 key=value,直到下一个引用
|
||||
const re =
|
||||
/@\[(店铺|商品|SKU)#(\d+)·([^\]]*)\]|(店铺|商品|SKU)「([^」]*)」\s+id=(\d+)((?:\s+[^\s「=]+=\S+)*)/g;
|
||||
|
||||
const segments: MessageCiteSegment[] = [];
|
||||
let last = 0;
|
||||
for (const m of source.matchAll(re)) {
|
||||
if (m.index == null) continue;
|
||||
if (m.index > last) {
|
||||
segments.push({ type: 'text', value: source.slice(last, m.index) });
|
||||
}
|
||||
if (m[1]) {
|
||||
const kind = LABEL_KIND[m[1]];
|
||||
if (kind) {
|
||||
segments.push({
|
||||
type: 'cite',
|
||||
kind,
|
||||
id: Number(m[2]),
|
||||
title: (m[3] || '').trim() || String(m[2]),
|
||||
});
|
||||
} else {
|
||||
segments.push({ type: 'text', value: m[0] });
|
||||
}
|
||||
} else {
|
||||
const kind = LABEL_KIND[m[4] || ''];
|
||||
if (kind) {
|
||||
segments.push({
|
||||
type: 'cite',
|
||||
kind,
|
||||
id: Number(m[6]),
|
||||
title: (m[5] || '').trim() || String(m[6]),
|
||||
});
|
||||
} else {
|
||||
segments.push({ type: 'text', value: m[0] });
|
||||
}
|
||||
}
|
||||
last = m.index + m[0].length;
|
||||
}
|
||||
if (last < source.length) {
|
||||
segments.push({ type: 'text', value: source.slice(last) });
|
||||
}
|
||||
return segments.length > 0 ? segments : [{ type: 'text', value: source }];
|
||||
}
|
||||
|
||||
/** 正文里仍存在的引用 key(用于删字后同步 cites)。 */
|
||||
export function inlineCiteKeysInText(text: string): Set<string> {
|
||||
const keys = new Set<string>();
|
||||
|
||||
@@ -41,6 +41,7 @@ import {
|
||||
} from '#/api/agent';
|
||||
|
||||
import CatalogCiteSender from './catalog-cite-sender.vue';
|
||||
import CatalogCiteRichText from './catalog-cite-rich-text.vue';
|
||||
import SessionSearchModal from './session-search-modal.vue';
|
||||
import {
|
||||
composeMessageWithCites,
|
||||
@@ -976,7 +977,11 @@ void loadEnabledModels().then(() => refreshSessions());
|
||||
]"
|
||||
:role="item.role"
|
||||
:name="item.name"
|
||||
:content="item.textLoading ? undefined : item.content"
|
||||
:content="
|
||||
item.textLoading || item.role === 'user'
|
||||
? undefined
|
||||
: item.content
|
||||
"
|
||||
:text-loading="false"
|
||||
variant="base"
|
||||
>
|
||||
@@ -984,6 +989,9 @@ void loadEnabledModels().then(() => refreshSessions());
|
||||
<template v-if="item.textLoading" #content>
|
||||
<ChatLoading animation="dots" />
|
||||
</template>
|
||||
<template v-else-if="item.role === 'user'" #content>
|
||||
<CatalogCiteRichText :text="item.content" />
|
||||
</template>
|
||||
</ChatItem>
|
||||
<div
|
||||
v-if="toolStatusActive && toolStatusLabel"
|
||||
|
||||
+9
-1
@@ -20,6 +20,7 @@ import {
|
||||
type AgentVO,
|
||||
} from '#/api/agent';
|
||||
import CatalogCiteSender from '#/views/agent/chat/catalog-cite-sender.vue';
|
||||
import CatalogCiteRichText from '#/views/agent/chat/catalog-cite-rich-text.vue';
|
||||
import {
|
||||
composeMessageWithCites,
|
||||
stripTrailingMention,
|
||||
@@ -594,13 +595,20 @@ onBeforeUnmount(() => abort?.abort());
|
||||
]"
|
||||
:role="item.role"
|
||||
:name="item.name"
|
||||
:content="item.textLoading ? undefined : item.content"
|
||||
:content="
|
||||
item.textLoading || item.role === 'user'
|
||||
? undefined
|
||||
: item.content
|
||||
"
|
||||
:text-loading="false"
|
||||
variant="base"
|
||||
>
|
||||
<template v-if="item.textLoading" #content>
|
||||
<ChatLoading animation="dots" />
|
||||
</template>
|
||||
<template v-else-if="item.role === 'user'" #content>
|
||||
<CatalogCiteRichText :text="item.content" />
|
||||
</template>
|
||||
</ChatItem>
|
||||
<div
|
||||
v-if="toolStatusActive && toolStatusLabel"
|
||||
|
||||
Reference in New Issue
Block a user