修复chatbox 轮询最新消息

This commit is contained in:
北枳 2025-08-24 15:31:52 +08:00
parent 628ff70a0c
commit c49b17418f
4 changed files with 31 additions and 12 deletions

View File

@ -204,7 +204,9 @@ const MOCK_MESSAGES: RealApiMessage[] = [
created_at: '2024-03-20T10:10:00Z',
function_name: 'generate_video',
custom_data: {
core_atmosphere: '欢欣、胜利',
prompt_json: {
core_atmosphere: '欢欣、胜利',
},
urls: ['https://cdn.qikongjian.com/faces/1755798635_facefusion_output_1755798635.mp4'],
completed_count: 1,
total_count: 1
@ -266,7 +268,7 @@ function isShotSketchGeneration(data: any): data is ShotSketchGeneration {
}
function isShotVideoGeneration(data: any): data is ShotVideoGeneration {
return data && 'core_atmosphere' in data && 'urls' in data && 'completed_count' in data && 'total_count' in data;
return data && 'prompt_json' in data && 'urls' in data && 'completed_count' in data && 'total_count' in data;
}
/**
@ -362,7 +364,7 @@ function transformSystemMessage(
if (isShotVideoGeneration(customData)) {
blocks = [{
type: 'text',
text: `🎬 分镜视频生成 \n核心氛围${customData.core_atmosphere}`
text: `🎬 分镜视频生成 \n核心氛围${customData.prompt_json.core_atmosphere}`
}, {
type: 'video',
url: customData.urls[0] || ''
@ -414,6 +416,8 @@ function transformMessage(apiMessage: RealApiMessage): ChatMessage {
} else if (role === 'system' && function_name && custom_data) {
// 处理系统消息
message.blocks = transformSystemMessage(function_name, content, custom_data);
} else {
message.blocks.push({ type: "text", text: content });
}
// 如果没有有效的 blocks至少添加一个文本块
@ -470,13 +474,13 @@ export async function fetchMessages(
}
// 转换消息并按时间排序
if (response.data.messages.length === 0) {
return {
messages: MOCK_MESSAGES.map(transformMessage),
hasMore: false,
totalCount: 0
};
}
// if (response.data.messages.length === 0) {
// return {
// messages: MOCK_MESSAGES.map(transformMessage),
// hasMore: false,
// totalCount: 0
// };
// }
return {
messages: response.data.messages
.map(transformMessage)

View File

@ -114,7 +114,9 @@ export interface ShotSketchGeneration {
}
// 分镜视频生成
export interface ShotVideoGeneration {
core_atmosphere: string; // 核心氛围
prompt_json: {
core_atmosphere: string; // 核心氛围
};
urls: string[]; // 分镜视频
completed_count: number; // 生成数量
total_count: number; // 总数量

View File

@ -139,6 +139,11 @@ export function useMessages({ config, onMessagesUpdate }: UseMessagesProps): [Me
setIsLoading(true);
setError(null);
// 暂停轮询
if (timeoutIdRef.current) {
clearTimeout(timeoutIdRef.current);
}
try {
// 立即添加用户消息(临时显示)
const userMessage: ChatMessage = {
@ -164,6 +169,14 @@ export function useMessages({ config, onMessagesUpdate }: UseMessagesProps): [Me
setError(err instanceof Error ? err : new Error("发送消息失败"));
} finally {
setIsLoading(false);
// 恢复轮询
if (systemPush) {
timeoutIdRef.current = setTimeout(() => {
isPollingRef.current = false;
updateMessages(false);
}, POLLING_INTERVAL);
}
}
}, [updateMessages, mergeMessages]);

View File

@ -226,7 +226,7 @@ const WorkFlow = React.memo(function WorkFlow() {
isSmartChatBoxOpen={isSmartChatBoxOpen}
setIsSmartChatBoxOpen={setIsSmartChatBoxOpen}
projectId={episodeId}
userId={userId}
userId={51}
/>
</Drawer>