MCPcopy Create free account
hub / github.com/firebase/quickstart-js / ChatView

Function ChatView

ai/ai-react-app/src/views/ChatView.tsx:65–549  ·  view source on GitHub ↗
({
  onUsageMetadataChange,
  currentParams,
  aiInstance,
  activeMode,
})

Source from the content-addressed store, hash-verified

63};
64
65const ChatView: React.FC<ChatViewProps> = ({
66 onUsageMetadataChange,
67 currentParams,
68 aiInstance,
69 activeMode,
70}) => {
71 const [chatHistory, setChatHistory] = useState<Content[]>([]);
72 const [currentInput, setCurrentInput] = useState("");
73 const [selectedFile, setSelectedFile] = useState<File | null>(null);
74 const [isLoading, setIsLoading] = useState(false);
75 const [error, setError] = useState<string | null>(null);
76 const [lastResponseParsedJson, setLastResponseParsedJson] = useState<
77 object | null
78 >(null);
79 const [lastGroundingMetadata, setLastGroundingMetadata] =
80 useState<GroundingMetadata | null>(null);
81
82 const chatContainerRef = useRef<HTMLDivElement>(null);
83 const chatSessionRef = useRef<ChatSession | null>(null);
84 const sessionParamsRef = useRef<ModelParams | null>(null);
85
86 const initializeChatSession = useCallback(
87 (params: ModelParams) => {
88 try {
89 console.log(
90 "[ChatView] Initializing new chat session with params:",
91 params,
92 );
93 const model = getGenerativeModel(aiInstance, params);
94 const newSession = model.startChat({
95 history: [],
96 safetySettings: params.safetySettings,
97 generationConfig: params.generationConfig,
98 tools: params.tools,
99 toolConfig: params.toolConfig,
100 systemInstruction: params.systemInstruction,
101 });
102 chatSessionRef.current = newSession;
103 // Store a deep copy of the params used for this session
104 sessionParamsRef.current = JSON.parse(JSON.stringify(params));
105 setChatHistory([]);
106 setError(null);
107 onUsageMetadataChange(null);
108 setLastGroundingMetadata(null);
109 console.log("[ChatView] New chat session initialized successfully.");
110 } catch (initError: unknown) {
111 console.error("[ChatView] Error initializing chat session:", initError);
112 setError(`Failed to initialize chat session`);
113 chatSessionRef.current = null;
114 sessionParamsRef.current = null;
115 }
116 },
117 [onUsageMetadataChange, aiInstance],
118 );
119
120 useEffect(() => {
121 if (aiInstance) {
122 initializeChatSession(currentParams);

Callers

nothing calls this directly

Calls 3

handleFunctionExecutionFunction · 0.90
fileToGenerativePartFunction · 0.90
modelParamsChangedFunction · 0.85

Tested by

no test coverage detected