fix(chat): correct indentation in useChat guest fallback
Some checks failed
ci/woodpecker/push/ci Pipeline failed
Some checks failed
ci/woodpecker/push/ci Pipeline failed
This commit is contained in:
@@ -343,63 +343,64 @@ export function useChat(options: UseChatOptions = {}): UseChatReturn {
|
|||||||
error: err instanceof Error ? err : new Error(String(err)),
|
error: err instanceof Error ? err : new Error(String(err)),
|
||||||
});
|
});
|
||||||
|
|
||||||
setMessages((prev) => {
|
|
||||||
const withoutPlaceholder = prev.filter((m) => m.id !== assistantMessageId);
|
|
||||||
messagesRef.current = withoutPlaceholder;
|
|
||||||
return withoutPlaceholder;
|
|
||||||
});
|
|
||||||
setIsStreaming(false);
|
|
||||||
|
|
||||||
try {
|
|
||||||
const response = await sendChatMessage(request);
|
|
||||||
|
|
||||||
const assistantMessage: Message = {
|
|
||||||
id: `assistant-${Date.now().toString()}`,
|
|
||||||
role: "assistant",
|
|
||||||
content: response.message.content,
|
|
||||||
createdAt: new Date().toISOString(),
|
|
||||||
model: response.model,
|
|
||||||
promptTokens: response.promptEvalCount ?? 0,
|
|
||||||
completionTokens: response.evalCount ?? 0,
|
|
||||||
totalTokens: (response.promptEvalCount ?? 0) + (response.evalCount ?? 0),
|
|
||||||
};
|
|
||||||
|
|
||||||
setMessages((prev) => {
|
setMessages((prev) => {
|
||||||
const updated = [...prev, assistantMessage];
|
const withoutPlaceholder = prev.filter((m) => m.id !== assistantMessageId);
|
||||||
messagesRef.current = updated;
|
messagesRef.current = withoutPlaceholder;
|
||||||
return updated;
|
return withoutPlaceholder;
|
||||||
});
|
});
|
||||||
|
setIsStreaming(false);
|
||||||
|
|
||||||
streamingSucceeded = true;
|
try {
|
||||||
} catch (fallbackErr: unknown) {
|
const response = await sendChatMessage(request);
|
||||||
const errorMsg =
|
|
||||||
fallbackErr instanceof Error ? fallbackErr.message : "Failed to send message";
|
|
||||||
setError("Unable to send message. Please try again.");
|
|
||||||
onError?.(fallbackErr instanceof Error ? fallbackErr : new Error(errorMsg));
|
|
||||||
console.error("Failed to send chat message", {
|
|
||||||
error: fallbackErr,
|
|
||||||
errorType: "LLM_ERROR",
|
|
||||||
conversationId: conversationIdRef.current,
|
|
||||||
messageLength: content.length,
|
|
||||||
messagePreview: content.substring(0, 50),
|
|
||||||
model,
|
|
||||||
messageCount: messagesRef.current.length,
|
|
||||||
timestamp: new Date().toISOString(),
|
|
||||||
});
|
|
||||||
|
|
||||||
const errorMessage: Message = {
|
const assistantMessage: Message = {
|
||||||
id: `error-${String(Date.now())}`,
|
id: `assistant-${Date.now().toString()}`,
|
||||||
role: "assistant",
|
role: "assistant",
|
||||||
content: "Something went wrong. Please try again.",
|
content: response.message.content,
|
||||||
createdAt: new Date().toISOString(),
|
createdAt: new Date().toISOString(),
|
||||||
};
|
model: response.model,
|
||||||
setMessages((prev) => {
|
promptTokens: response.promptEvalCount ?? 0,
|
||||||
const updated = [...prev, errorMessage];
|
completionTokens: response.evalCount ?? 0,
|
||||||
messagesRef.current = updated;
|
totalTokens: (response.promptEvalCount ?? 0) + (response.evalCount ?? 0),
|
||||||
return updated;
|
};
|
||||||
});
|
|
||||||
setIsLoading(false);
|
setMessages((prev) => {
|
||||||
return;
|
const updated = [...prev, assistantMessage];
|
||||||
|
messagesRef.current = updated;
|
||||||
|
return updated;
|
||||||
|
});
|
||||||
|
|
||||||
|
streamingSucceeded = true;
|
||||||
|
} catch (fallbackErr: unknown) {
|
||||||
|
const errorMsg =
|
||||||
|
fallbackErr instanceof Error ? fallbackErr.message : "Failed to send message";
|
||||||
|
setError("Unable to send message. Please try again.");
|
||||||
|
onError?.(fallbackErr instanceof Error ? fallbackErr : new Error(errorMsg));
|
||||||
|
console.error("Failed to send chat message", {
|
||||||
|
error: fallbackErr,
|
||||||
|
errorType: "LLM_ERROR",
|
||||||
|
conversationId: conversationIdRef.current,
|
||||||
|
messageLength: content.length,
|
||||||
|
messagePreview: content.substring(0, 50),
|
||||||
|
model,
|
||||||
|
messageCount: messagesRef.current.length,
|
||||||
|
timestamp: new Date().toISOString(),
|
||||||
|
});
|
||||||
|
|
||||||
|
const errorMessage: Message = {
|
||||||
|
id: `error-${String(Date.now())}`,
|
||||||
|
role: "assistant",
|
||||||
|
content: "Something went wrong. Please try again.",
|
||||||
|
createdAt: new Date().toISOString(),
|
||||||
|
};
|
||||||
|
setMessages((prev) => {
|
||||||
|
const updated = [...prev, errorMessage];
|
||||||
|
messagesRef.current = updated;
|
||||||
|
return updated;
|
||||||
|
});
|
||||||
|
setIsLoading(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user