AI Coach fixed

This commit is contained in:
AG
2025-11-26 23:09:42 +02:00
parent 94f0a9a17a
commit be2670ed12
3 changed files with 24 additions and 11 deletions

View File

@@ -1,7 +1,7 @@
import { WorkoutSession } from '../types';
import { api } from './api';
export const createFitnessChat = (history: WorkoutSession[]): any => {
export const createFitnessChat = (history: WorkoutSession[], lang: 'en' | 'ru' = 'ru'): any => {
// The original returned a Chat object.
// Now we need to return something that behaves like it or refactor the UI.
// The UI likely calls `chat.sendMessage(msg)`.
@@ -9,12 +9,12 @@ export const createFitnessChat = (history: WorkoutSession[]): any => {
// Summarize data to reduce token count while keeping relevant context
const summary = history.slice(0, 10).map(s => ({
date: new Date(s.startTime).toLocaleDateString('ru-RU'),
date: new Date(s.startTime).toLocaleDateString(lang === 'ru' ? 'ru-RU' : 'en-US'),
userWeight: s.userBodyWeight,
exercises: s.sets.map(set => `${set.exerciseName}: ${set.weight ? set.weight + 'кг' : ''}${set.reps ? ' x ' + set.reps + 'повт' : ''} ${set.distanceMeters ? set.distanceMeters + 'м' : ''}`).join(', ')
exercises: s.sets.map(set => `${set.exerciseName}: ${set.weight ? set.weight + (lang === 'ru' ? 'кг' : 'kg') : ''}${set.reps ? ' x ' + set.reps + (lang === 'ru' ? 'повт' : 'reps') : ''} ${set.distanceMeters ? set.distanceMeters + (lang === 'ru' ? 'м' : 'm') : ''}`).join(', ')
}));
const systemInstruction = `
const systemInstruction = lang === 'ru' ? `
Ты — опытный и поддерживающий фитнес-тренер.
Твоя задача — анализировать тренировки пользователя и давать краткие, полезные советы на русском языке.
@@ -25,6 +25,18 @@ export const createFitnessChat = (history: WorkoutSession[]): any => {
Если пользователь спрашивает о прогрессе, используй эти данные.
Отвечай емко, мотивирующе. Избегай длинных лекций, если не просили.
` : `
You are an experienced and supportive fitness coach.
Your task is to analyze the user's workouts and provide concise, helpful advice in English.
Consider the user's weight (userWeight in json), if provided, when analyzing progress in bodyweight exercises.
Here are the user's last 10 workouts (in JSON format):
${JSON.stringify(summary)}
If the user asks about progress, use this data.
Answer concisely and motivationally. Avoid long lectures unless asked.
ALWAYS answer in the language the user speaks to you, defaulting to English if unsure.
`;
return {
@@ -34,6 +46,7 @@ export const createFitnessChat = (history: WorkoutSession[]): any => {
userMessage
});
return {
text: res.response,
response: {
text: () => res.response
}