Skip to content

Commit 620331c

Browse files
committed
fix: Improve error handling and dynamic configuration for HuggingFace OAuth and chat memory
1 parent b2069e9 commit 620331c

File tree

4 files changed

+84
-8
lines changed

4 files changed

+84
-8
lines changed

src/lib/chat/memory.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export class DexieChatMemory extends BaseChatMessageHistory {
5757
const chatModel = CHAT_MODELS.find((m) => m.model === this.config.default_chat_model);
5858
const embeddingModel = EMBEDDING_MODELS.find((m) => m.model === this.config.default_embedding_model);
5959

60-
if (!chatModel || !embeddingModel) {
60+
if (!chatModel) {
6161
throw new Error("Chat or embedding models are not configured.");
6262
}
6363

@@ -67,7 +67,7 @@ export class DexieChatMemory extends BaseChatMessageHistory {
6767
createdAt: Date.now(),
6868
updatedAt: Date.now(),
6969
model: chatModel.model,
70-
embedding_model: embeddingModel.model,
70+
embedding_model: embeddingModel?.model || '',
7171
enabled_tools: [],
7272
messages: [],
7373
};

src/pages/chat/components/Messages.tsx

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,17 @@ export const Messages = React.memo(({
2323
const { id } = useParams();
2424
const viewportRef = React.useRef<HTMLDivElement>(null);
2525
const [showScrollToBottom, setShowScrollToBottom] = React.useState(false);
26+
const initialLoadRef = React.useRef(true);
2627

2728
const handleScroll = React.useCallback((event: Event) => {
2829
const viewport = event.target as HTMLDivElement;
2930
const isNotAtBottom = viewport.scrollHeight - viewport.scrollTop - viewport.clientHeight > 10;
31+
console.log('Scroll position:', {
32+
scrollHeight: viewport.scrollHeight,
33+
scrollTop: viewport.scrollTop,
34+
clientHeight: viewport.clientHeight,
35+
isNotAtBottom
36+
});
3037
setShowScrollToBottom(isNotAtBottom);
3138
}, []);
3239

@@ -39,15 +46,84 @@ export const Messages = React.memo(({
3946
});
4047
}, []);
4148

49+
// Effect for handling scroll events
4250
React.useEffect(() => {
4351
const viewport = viewportRef.current;
4452
if (!viewport) return;
4553

4654
viewport.addEventListener('scroll', handleScroll);
4755
// Initial check for scroll position
4856
handleScroll({ target: viewport } as unknown as Event);
49-
return () => viewport.removeEventListener('scroll', handleScroll);
50-
}, [handleScroll]);
57+
58+
// Check scroll position after a short delay to account for content rendering
59+
const checkTimeout = setTimeout(() => {
60+
handleScroll({ target: viewport } as unknown as Event);
61+
}, 100);
62+
63+
return () => {
64+
viewport.removeEventListener('scroll', handleScroll);
65+
clearTimeout(checkTimeout);
66+
};
67+
}, [handleScroll, messages, streamingAIMessageChunks]);
68+
69+
// Effect for initial scroll to bottom on page load
70+
React.useEffect(() => {
71+
if (initialLoadRef.current && messages && messages.length > 0 && viewportRef.current) {
72+
// Use a timeout to ensure content is rendered before scrolling
73+
const initialScrollTimeout = setTimeout(() => {
74+
if (viewportRef.current) {
75+
viewportRef.current.scrollTo({
76+
top: viewportRef.current.scrollHeight,
77+
behavior: 'smooth'
78+
});
79+
initialLoadRef.current = false;
80+
}
81+
}, 100);
82+
83+
return () => clearTimeout(initialScrollTimeout);
84+
}
85+
}, [messages]);
86+
87+
// Reset initialLoadRef when chat ID changes
88+
React.useEffect(() => {
89+
// Reset the initial load flag when the chat ID changes
90+
initialLoadRef.current = true;
91+
92+
// Attempt to scroll to bottom after a short delay
93+
if (id && id !== "new") {
94+
const resetScrollTimeout = setTimeout(() => {
95+
if (viewportRef.current && messages && messages.length > 0) {
96+
viewportRef.current.scrollTo({
97+
top: viewportRef.current.scrollHeight,
98+
behavior: 'smooth'
99+
});
100+
}
101+
}, 200);
102+
103+
return () => clearTimeout(resetScrollTimeout);
104+
}
105+
}, [id]);
106+
107+
// Scroll to bottom when streaming messages change
108+
React.useEffect(() => {
109+
// Only auto-scroll if we're already near the bottom or if this is the first message chunk
110+
if (viewportRef.current && streamingAIMessageChunks.length > 0) {
111+
const viewport = viewportRef.current;
112+
const isNearBottom = viewport.scrollHeight - viewport.scrollTop - viewport.clientHeight < 100;
113+
114+
if (isNearBottom || streamingAIMessageChunks.length === 1) {
115+
// Use requestAnimationFrame to ensure smooth scrolling during streaming
116+
requestAnimationFrame(() => {
117+
if (viewportRef.current) {
118+
viewportRef.current.scrollTo({
119+
top: viewportRef.current.scrollHeight,
120+
behavior: streamingAIMessageChunks.length === 1 ? 'smooth' : 'auto'
121+
});
122+
}
123+
});
124+
}
125+
}
126+
}, [streamingAIMessageChunks]);
51127

52128
if (id === "new" || !messages) {
53129
return <div className="flex-1 min-h-0"><ScrollArea className="h-full" /></div>;
@@ -99,7 +175,7 @@ export const Messages = React.memo(({
99175
<Button
100176
variant="secondary"
101177
size="icon"
102-
className="absolute left-1/2 -translate-x-1/2 z-50 bottom-4 rounded-full shadow-md hover:bg-accent bg-background/80 backdrop-blur-sm"
178+
className="absolute z-50 bottom-4 left-1/2 -translate-x-1/2 rounded-full shadow-md hover:bg-accent bg-background/80 backdrop-blur-sm"
103179
onClick={scrollToBottom}
104180
>
105181
<ArrowDown className="h-4 w-4" />

src/pages/home/components/Providers.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ export const Providers = ({ config }: ProvidersProps) => {
2424

2525
const handleHuggingFaceOAuth = () => {
2626
const clientId = import.meta.env.VITE_HF_CLIENT_ID;
27-
const redirectUri = import.meta.env.VITE_HF_REDIRECT_URI;
27+
const redirectUri = window.location.origin + '/integrations/huggingface-callback';
2828

29-
if (!clientId || !redirectUri) {
29+
if (!clientId) {
3030
toast.error("HuggingFace OAuth configuration is missing");
3131
return;
3232
}

src/pages/integrations/huggingface-callback.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export function HuggingFaceCallback() {
3333
grant_type: "authorization_code",
3434
client_id: import.meta.env.VITE_HF_CLIENT_ID,
3535
client_secret: import.meta.env.VITE_HF_CLIENT_SECRET,
36-
redirect_uri: import.meta.env.VITE_HF_REDIRECT_URI,
36+
redirect_uri: window.location.origin + '/integrations/huggingface-callback',
3737
code,
3838
}),
3939
});

0 commit comments

Comments
 (0)