mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 19:09:19 +08:00
feat: 工具调用改为独立 SSE 事件并以中文状态条展示
This commit is contained in:
@@ -84,6 +84,12 @@ export function streamAgentChat(options: {
|
||||
threadId: string;
|
||||
content: string;
|
||||
onToken: (text: string) => void;
|
||||
/** 工具状态(不进正文);phase=start 显示 label,end 可清除 */
|
||||
onTool?: (payload: {
|
||||
phase: string;
|
||||
name?: string;
|
||||
label: string;
|
||||
}) => void;
|
||||
onDone?: () => void;
|
||||
onError?: (message: string) => void;
|
||||
}): AbortController {
|
||||
@@ -135,6 +141,23 @@ export function streamAgentChat(options: {
|
||||
} catch {
|
||||
options.onToken(data);
|
||||
}
|
||||
} else if (event === 'tool') {
|
||||
try {
|
||||
const parsed = JSON.parse(data) as {
|
||||
phase?: string;
|
||||
name?: string;
|
||||
label?: string;
|
||||
};
|
||||
if (parsed.phase && parsed.label) {
|
||||
options.onTool?.({
|
||||
phase: parsed.phase,
|
||||
name: parsed.name,
|
||||
label: parsed.label,
|
||||
});
|
||||
}
|
||||
} catch {
|
||||
/* ignore malformed tool events */
|
||||
}
|
||||
} else if (event === 'error') {
|
||||
try {
|
||||
const parsed = JSON.parse(data) as { message?: string };
|
||||
|
||||
@@ -67,6 +67,9 @@ const composingNew = ref(false);
|
||||
const messages = ref<AgentVO.Message[]>([]);
|
||||
/** 当前流式助手正文(与 messages 末条同步) */
|
||||
const streamingText = ref('');
|
||||
/** 流式工具状态(中文 label,不进正文) */
|
||||
const toolStatusLabel = ref('');
|
||||
const toolStatusActive = ref(false);
|
||||
const draft = ref('');
|
||||
/** 本条消息附带的店铺/商品/SKU,发送时写成 [引用] 块 */
|
||||
const citations = ref<CatalogCite[]>([]);
|
||||
@@ -107,20 +110,28 @@ const bubbleViews = computed<BubbleView[]>(() =>
|
||||
sending.value &&
|
||||
idx === messages.value.length - 1 &&
|
||||
m.role === 'assistant';
|
||||
const text = isStreamingAssistant
|
||||
const raw = isStreamingAssistant
|
||||
? streamingText.value || m.content || ''
|
||||
: m.content || '';
|
||||
const text = stripLegacyToolMarkers(raw);
|
||||
const role = m.role === 'user' ? 'user' : 'assistant';
|
||||
return {
|
||||
id: m.id || `${m.seq}-${idx}`,
|
||||
role,
|
||||
name: role === 'user' ? '我' : '智能助手',
|
||||
content: text,
|
||||
textLoading: isStreamingAssistant && !text,
|
||||
textLoading: isStreamingAssistant && !text && !toolStatusActive.value,
|
||||
};
|
||||
}),
|
||||
);
|
||||
|
||||
/** 清洗历史里曾混入正文的「[调用工具: …]」标记 */
|
||||
function stripLegacyToolMarkers(content: string) {
|
||||
return content
|
||||
.replace(/\n?\[调用工具:[^\]]*\]\n?/g, '')
|
||||
.replace(/^\s+|\s+$/g, '');
|
||||
}
|
||||
|
||||
function scrollChatToBottom() {
|
||||
const el = messagePaneRef.value;
|
||||
if (!el) return;
|
||||
@@ -270,6 +281,10 @@ async function saveSettings() {
|
||||
}
|
||||
|
||||
function appendStreamToken(t: string) {
|
||||
// 开始出正文后收起工具状态条
|
||||
if (toolStatusActive.value) {
|
||||
clearToolStatus();
|
||||
}
|
||||
streamingText.value += t;
|
||||
const idx = messages.value.length - 1;
|
||||
const last = messages.value[idx];
|
||||
@@ -279,6 +294,26 @@ function appendStreamToken(t: string) {
|
||||
void nextTick().then(scrollChatToBottom);
|
||||
}
|
||||
|
||||
function onStreamTool(payload: {
|
||||
phase: string;
|
||||
name?: string;
|
||||
label: string;
|
||||
}) {
|
||||
if (payload.phase === 'start') {
|
||||
toolStatusActive.value = true;
|
||||
toolStatusLabel.value = payload.label;
|
||||
void nextTick().then(scrollChatToBottom);
|
||||
} else if (payload.phase === 'end') {
|
||||
// 同轮可能多个工具;最后一个 end 后仍保持到有 token / done 再清也可
|
||||
toolStatusLabel.value = payload.label;
|
||||
}
|
||||
}
|
||||
|
||||
function clearToolStatus() {
|
||||
toolStatusActive.value = false;
|
||||
toolStatusLabel.value = '';
|
||||
}
|
||||
|
||||
async function send(textOverride?: string) {
|
||||
const raw = textOverride ?? draft.value;
|
||||
const text = composeMessageWithCites(stripTrailingMention(raw), citations.value);
|
||||
@@ -303,6 +338,7 @@ async function send(textOverride?: string) {
|
||||
}
|
||||
|
||||
streamingText.value = '';
|
||||
clearToolStatus();
|
||||
draft.value = '';
|
||||
citations.value = [];
|
||||
messages.value.push({
|
||||
@@ -323,6 +359,7 @@ async function send(textOverride?: string) {
|
||||
threadId,
|
||||
content: text,
|
||||
onToken: appendStreamToken,
|
||||
onTool: onStreamTool,
|
||||
onDone: () => {
|
||||
const finalText = streamingText.value;
|
||||
const idx = messages.value.length - 1;
|
||||
@@ -332,6 +369,7 @@ async function send(textOverride?: string) {
|
||||
}
|
||||
sending.value = false;
|
||||
streamingText.value = '';
|
||||
clearToolStatus();
|
||||
void refreshSessions();
|
||||
},
|
||||
onError: (msg) => {
|
||||
@@ -342,6 +380,7 @@ async function send(textOverride?: string) {
|
||||
messages.value[idx] = { ...last, content: `错误:${msg}` };
|
||||
}
|
||||
streamingText.value = '';
|
||||
clearToolStatus();
|
||||
message.error(msg);
|
||||
},
|
||||
});
|
||||
@@ -359,6 +398,7 @@ function applyStarterPrompt(text: string) {
|
||||
function onSenderStop() {
|
||||
abort?.abort();
|
||||
sending.value = false;
|
||||
clearToolStatus();
|
||||
}
|
||||
|
||||
onBeforeUnmount(() => abort?.abort());
|
||||
@@ -497,6 +537,15 @@ void refreshSessions();
|
||||
variant="base"
|
||||
animation="gradient"
|
||||
/>
|
||||
<div
|
||||
v-if="toolStatusActive && toolStatusLabel"
|
||||
class="text-muted-foreground mx-auto flex max-w-3xl items-center gap-2 px-4 py-1 text-xs"
|
||||
>
|
||||
<span
|
||||
class="border-primary/40 inline-block size-3 animate-spin rounded-full border-2 border-t-transparent"
|
||||
/>
|
||||
<span>{{ toolStatusLabel }}…</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="shrink-0 border-t border-border p-3">
|
||||
<CatalogCiteSender
|
||||
|
||||
Reference in New Issue
Block a user