fex:尝试修复流式显示工具调用不穿插显示的问题

This commit is contained in:
XiaoFeng
2025-12-17 10:03:40 +08:00
parent c21ad95963
commit 6c5d470bad
3 changed files with 305 additions and 41 deletions

View File

@ -116,33 +116,31 @@ async function handleUserMessageWithBackend(
currentSession!.sendMessage(text, {
onText: (fullText, isStreaming) => {
if (isStreaming) {
// 流式更新状态为"生成中"
// 流式更新消息
panel.webview.postMessage({
command: "updateStatus",
text: "生成中...",
type: "working",
});
} else {
// 完成时发送消息并隐藏状态
console.log('[MessageHandler] 发送最终消息, 文本长度:', fullText.length);
panel.webview.postMessage({
command: "hideStatus",
});
panel.webview.postMessage({
command: "receiveMessage",
command: "updateStreamingMessage",
text: fullText,
});
}
// 注意:完成时通过 onComplete 发送分段消息
},
onToolStart: (toolName) => {
// 实时显示工具状态
panel.webview.postMessage({
command: "toolStart",
toolName,
});
// 同时更新状态栏
panel.webview.postMessage({
command: "updateStatus",
text: `正在执行 ${toolName}...`,
type: "working",
});
},
onToolComplete: (toolName, result) => {
// 实时更新工具状态
panel.webview.postMessage({
command: "toolComplete",
toolName,
@ -151,6 +149,7 @@ async function handleUserMessageWithBackend(
},
onToolError: (toolName, error) => {
// 实时显示工具错误
panel.webview.postMessage({
command: "toolError",
toolName,
@ -159,22 +158,30 @@ async function handleUserMessageWithBackend(
},
onQuestion: (askId, question, options) => {
// 问题会在分段消息中显示,这里只更新状态栏
panel.webview.postMessage({
command: "showQuestion",
askId,
question,
options,
command: "updateStatus",
text: "等待用户回答...",
type: "working",
});
},
onComplete: async () => {
// 隐藏加载状态
onComplete: async (segments) => {
// 隐藏状态
panel.webview.postMessage({
command: "hideLoading",
command: "hideStatus",
});
// 记录到历史(如果有累积文本)
// 注意:实际文本已通过 onText 发送
// 发送分段消息
console.log('[MessageHandler] 发送分段消息, 段落数:', segments.length);
console.log('[MessageHandler] segments 内容:', JSON.stringify(segments));
const result = await panel.webview.postMessage({
command: "receiveSegments",
segments: segments,
});
console.log('[MessageHandler] postMessage 返回值:', result);
resolve();
},