Skip to content

Commit be2f579

Browse files
author
shijiashuai
committed
feat: update dialogue orchestrator and digital human store
1 parent cd7705b commit be2f579

2 files changed

Lines changed: 57 additions & 23 deletions

File tree

src/core/dialogue/dialogueOrchestrator.ts

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ export interface DialogueTurnOptions extends DialogueHandleOptions {
1515
setLoading?: (loading: boolean) => void;
1616
}
1717

18+
let pendingTurn: Promise<ChatResponsePayload | undefined> | null = null;
19+
1820
export async function runDialogueTurn(
1921
userText: string,
2022
options: DialogueTurnOptions = {}
@@ -24,6 +26,11 @@ export async function runDialogueTurn(
2426
return undefined;
2527
}
2628

29+
if (pendingTurn) {
30+
console.warn('对话请求被忽略:上一轮对话仍在进行中');
31+
return undefined;
32+
}
33+
2734
const store = useDigitalHumanStore.getState();
2835
const {
2936
sessionId = store.sessionId,
@@ -43,28 +50,34 @@ export async function runDialogueTurn(
4350
setLoading?.(true);
4451
store.setBehavior('thinking');
4552

46-
try {
47-
const response = await sendUserInput({
48-
sessionId,
49-
userText: content,
50-
meta,
51-
});
52-
53-
await handleDialogueResponse(response, {
54-
isMuted,
55-
speakWith,
56-
addAssistantMessage,
57-
});
58-
59-
return response;
60-
} finally {
61-
store.setLoading(false);
62-
setLoading?.(false);
63-
64-
if (useDigitalHumanStore.getState().currentBehavior === 'thinking') {
65-
useDigitalHumanStore.getState().setBehavior('idle');
53+
const execute = async (): Promise<ChatResponsePayload | undefined> => {
54+
try {
55+
const response = await sendUserInput({
56+
sessionId,
57+
userText: content,
58+
meta,
59+
});
60+
61+
await handleDialogueResponse(response, {
62+
isMuted,
63+
speakWith,
64+
addAssistantMessage,
65+
});
66+
67+
return response;
68+
} finally {
69+
store.setLoading(false);
70+
setLoading?.(false);
71+
pendingTurn = null;
72+
73+
if (useDigitalHumanStore.getState().currentBehavior === 'thinking') {
74+
useDigitalHumanStore.getState().setBehavior('idle');
75+
}
6676
}
67-
}
77+
};
78+
79+
pendingTurn = execute();
80+
return pendingTurn;
6881
}
6982

7083
export async function handleDialogueResponse(

src/store/digitalHumanStore.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ const generateSessionId = (): string => {
8282
let nextChatMessageId = 0;
8383
let recordingTimeoutId: ReturnType<typeof setTimeout> | null = null;
8484

85+
const ERROR_THROTTLE_MS = 2000;
86+
8587
const generateChatMessageId = (): number => {
8688
nextChatMessageId += 1;
8789
return Date.now() + nextChatMessageId;
@@ -158,7 +160,18 @@ export const useDigitalHumanStore = create<DigitalHumanState>((set, get) => ({
158160
isConnected: status === 'connected'
159161
}),
160162
setLoading: (loading) => set({ isLoading: loading }),
161-
setError: (error) => set({ error, lastErrorTime: error ? Date.now() : null }),
163+
setError: (error) => {
164+
if (!error) {
165+
set({ error: null, lastErrorTime: null });
166+
return;
167+
}
168+
const { error: prevError, lastErrorTime } = get();
169+
const now = Date.now();
170+
if (prevError === error && lastErrorTime && now - lastErrorTime < ERROR_THROTTLE_MS) {
171+
return;
172+
}
173+
set({ error, lastErrorTime: now });
174+
},
162175
clearError: () => set({ error: null, lastErrorTime: null }),
163176

164177
// 会话管理
@@ -168,7 +181,15 @@ export const useDigitalHumanStore = create<DigitalHumanState>((set, get) => ({
168181
if (storage) {
169182
storage.setItem('metahuman_session_id', newId);
170183
}
171-
set({ sessionId: newId, chatHistory: [] });
184+
set({
185+
sessionId: newId,
186+
chatHistory: [],
187+
error: null,
188+
lastErrorTime: null,
189+
connectionStatus: 'connected',
190+
isConnected: true,
191+
isLoading: false,
192+
});
172193
},
173194

174195
addChatMessage: (role, text) => set((state) => {

0 commit comments

Comments
 (0)