Skip to content

Commit b510315

Browse files
committed
refactor(frontend/hooks): enhance use-chat with backend state management
Updated useChat hook to utilize serializedState from backend response for thread management, ensuring correct state persistence during streaming interactions. Modified files (2): - use-chat.ts: Adjusted state handling for threadId and serializedState - chat-service.ts: Added content filter handling for streaming messages
1 parent 4d3446b commit b510315

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

src/frontend/task-agent-web/hooks/use-chat.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,11 @@ export function useChat(options: UseChatOptions = {}): UseChatReturn {
254254
pageSize: PAGINATION.CONVERSATION_PAGE_SIZE,
255255
});
256256

257-
// Update current thread ID and serializedState
257+
// Update current thread ID and serializedState from backend response
258+
// CRITICAL: Use serializedState from response, NOT the threadId directly
259+
// The serializedState is a full AgentThread JSON required by the streaming service
258260
setCurrentThreadId(threadId);
259-
setSerializedState(threadId); // ThreadDbKey is used as serializedState
261+
setSerializedState(response.serializedState ?? null);
260262

261263
// Convert backend messages to frontend format
262264
const loadedMessages: ChatMessage[] = (response.messages || []).map((msg) => ({

src/frontend/task-agent-web/lib/api/chat-service.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,19 @@ export async function sendMessageWithStreaming(
224224
callbacks.onToolCallResult?.(event.toolCallId || "", event.result || "");
225225
}
226226

227-
// ✅ Handle THREAD_STATE event (serializedState for next request)
227+
// Handle CONTENT_FILTER event (Azure OpenAI content policy violation)
228+
// This provides a ChatGPT-like UX where the blocked message appears in chat
229+
if (event.type === "CONTENT_FILTER") {
230+
// Update streaming message with the content filter message
231+
const filterMessage = event.message || "I'm unable to assist with that request as it may violate content policies.";
232+
fullMessage = filterMessage;
233+
messageId = event.messageId || `cf-${Date.now()}`;
234+
// Trigger text chunk callback so UI updates immediately
235+
callbacks.onTextChunk?.(filterMessage, filterMessage);
236+
// Don't throw - let stream continue to get thread state for persistence
237+
}
238+
239+
// Handle THREAD_STATE event (serializedState for next request)
228240
if (event.type === "THREAD_STATE" && event.serializedState) {
229241
serializedState = event.serializedState;
230242
// Callback: Stream complete with state

0 commit comments

Comments
 (0)