Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions src/ai-assistant/src/components/ai-assistant-component.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
// Copyright contributors to the kepler.gl project

import React, {useEffect, useState} from 'react';
import React, {useEffect, useRef, useState} from 'react';
import {useDispatch, useSelector} from 'react-redux';
import styled from 'styled-components';
import {textColorLT, theme} from '@kepler.gl/styles';
Expand Down Expand Up @@ -59,6 +59,8 @@ export function AiAssistantComponent() {

const [ideas, setIdeas] = useState<{title: string; description: string}[]>([]);

const [restartKey, setRestartKey] = useState<number>(0);

// get dataset meta data and re-initialize assistant when datasets or layers change
useEffect(() => {
const metaData = getDatasetContext(visState?.datasets, visState?.layers || []);
Expand All @@ -70,7 +72,12 @@ export function AiAssistantComponent() {
const instructions = `${INSTRUCTIONS}\n\n${datasetMetaData}`;

// generate ideas from LLM
const {temporaryPrompt} = useAssistant({...assistantProps, instructions});
const {temporaryPrompt, restartChat: libraryRestartChat} = useAssistant({...assistantProps, instructions});

const restartChatRef = useRef(libraryRestartChat);
useEffect(() => {
restartChatRef.current = libraryRestartChat;
}, [libraryRestartChat]);

Copilot AI Dec 17, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The useEffect hook is unnecessary for updating the ref. Following React best practices and the pattern used elsewhere in the codebase (e.g., monaco-editor.tsx), refs can be updated via direct assignment in the component body. Replace the useEffect with direct assignment.

Suggested change
useEffect(() => {
restartChatRef.current = libraryRestartChat;
}, [libraryRestartChat]);
restartChatRef.current = libraryRestartChat;

Copilot uses AI. Check for mistakes.

const generateIdeas = async () => {
try {
Expand All @@ -97,9 +104,16 @@ export function AiAssistantComponent() {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [datasetMetaData]);

const onRestartAssistant = () => {
// clean up aiAssistant state
const onRestartAssistant = async () => {
dispatch(updateAiAssistantMessages([]));

try {
await restartChatRef.current();
} catch (e) {
console.error('Error restarting chat:', e);
}

setRestartKey(prev => prev + 1);
};

const onMessagesUpdated = (messages: MessageModel[]) => {
Expand All @@ -117,6 +131,7 @@ export function AiAssistantComponent() {
return (
<StyledAiAssistantComponent className="ai-assistant-component">
<AiAssistant
key={restartKey}
{...assistantProps}
instructions={instructions}
theme={theme.textColor === textColorLT ? 'light' : 'dark'}
Expand Down