AI coach fixed: acquired access to context data

This commit is contained in:
aodulov
2025-11-27 07:51:36 +02:00
parent 4f3a889410
commit 1ef85d7a58
4 changed files with 186 additions and 46 deletions

View File

@@ -2,12 +2,14 @@
import React, { useState, useRef, useEffect } from 'react';
import { Send, Bot, User, Loader2, AlertTriangle } from 'lucide-react';
import { createFitnessChat } from '../services/geminiService';
import { WorkoutSession, Language } from '../types';
import { WorkoutSession, Language, UserProfile, WorkoutPlan } from '../types';
import { Chat, GenerateContentResponse } from '@google/genai';
import { t } from '../services/i18n';
interface AICoachProps {
history: WorkoutSession[];
userProfile?: UserProfile;
plans?: WorkoutPlan[];
lang: Language;
}
@@ -17,7 +19,7 @@ interface Message {
text: string;
}
const AICoach: React.FC<AICoachProps> = ({ history, lang }) => {
const AICoach: React.FC<AICoachProps> = ({ history, userProfile, plans, lang }) => {
const [messages, setMessages] = useState<Message[]>([
{ id: 'intro', role: 'model', text: t('ai_intro', lang) }
]);
@@ -29,7 +31,7 @@ const AICoach: React.FC<AICoachProps> = ({ history, lang }) => {
useEffect(() => {
try {
const chat = createFitnessChat(history, lang);
const chat = createFitnessChat(history, lang, userProfile, plans);
if (chat) {
chatSessionRef.current = chat;
} else {
@@ -38,7 +40,7 @@ const AICoach: React.FC<AICoachProps> = ({ history, lang }) => {
} catch (err) {
setError("Failed to initialize AI");
}
}, [history, lang]);
}, [history, lang, userProfile, plans]);
const scrollToBottom = () => {
messagesEndRef.current?.scrollIntoView({ behavior: "smooth" });
@@ -57,7 +59,7 @@ const AICoach: React.FC<AICoachProps> = ({ history, lang }) => {
setLoading(true);
try {
const result: GenerateContentResponse = await chatSessionRef.current.sendMessage({ message: userMsg.text });
const result: GenerateContentResponse = await chatSessionRef.current.sendMessage(userMsg.text);
const text = result.text;
const aiMsg: Message = {