|
1 | 1 | "use client"; |
2 | 2 |
|
3 | | -import { useCallback, useEffect, useState } from "react"; |
| 3 | +import { useCallback, useEffect, useState, useRef } from "react"; |
4 | 4 |
|
5 | 5 | import { AssistantRuntimeProvider } from "@assistant-ui/react"; |
6 | 6 | import { useAISDKRuntime } from "@assistant-ui/react-ai-sdk"; |
@@ -34,15 +34,37 @@ export function DocsAssistant({ pageContext }: DocsAssistantProps) { |
34 | 34 | function DocsAssistantInner({ pageContext }: DocsAssistantProps) { |
35 | 35 | const { provider, openaiApiKey, geminiApiKey } = useAssistantSettings(); |
36 | 36 |
|
| 37 | + // Use refs to ensure we always get the latest values |
| 38 | + const providerRef = useRef(provider); |
| 39 | + const openaiApiKeyRef = useRef(openaiApiKey); |
| 40 | + const geminiApiKeyRef = useRef(geminiApiKey); |
| 41 | + |
| 42 | + // Update refs whenever the values change |
| 43 | + providerRef.current = provider; |
| 44 | + openaiApiKeyRef.current = openaiApiKey; |
| 45 | + geminiApiKeyRef.current = geminiApiKey; |
| 46 | + |
37 | 47 | const chat = useChat({ |
38 | 48 | transport: new DefaultChatTransport({ |
39 | 49 | api: "/api/chat", |
40 | 50 | body: () => { |
41 | | - const apiKey = provider === "openai" ? openaiApiKey : geminiApiKey; |
| 51 | + // Use refs to get the current values at request time |
| 52 | + const currentProvider = providerRef.current; |
| 53 | + const currentApiKey = |
| 54 | + currentProvider === "openai" |
| 55 | + ? openaiApiKeyRef.current |
| 56 | + : geminiApiKeyRef.current; |
| 57 | + |
| 58 | + console.log("[DocsAssistant] useChat body function called with:", { |
| 59 | + provider: currentProvider, |
| 60 | + apiKeyLength: currentApiKey.length, |
| 61 | + hasApiKey: currentApiKey.trim().length > 0, |
| 62 | + }); |
| 63 | + |
42 | 64 | return { |
43 | 65 | pageContext, |
44 | | - provider, |
45 | | - apiKey, |
| 66 | + provider: currentProvider, |
| 67 | + apiKey: currentApiKey, |
46 | 68 | }; |
47 | 69 | }, |
48 | 70 | }), |
|
0 commit comments