56 lines
2.7 KiB
TypeScript
56 lines
2.7 KiB
TypeScript
import { WorkoutSession } from '../types';
|
||
import { api } from './api';
|
||
|
||
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)`.
|
||
// So we return an object with `sendMessage`.
|
||
|
||
// Summarize data to reduce token count while keeping relevant context
|
||
const summary = history.slice(0, 10).map(s => ({
|
||
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 + (lang === 'ru' ? 'кг' : 'kg') : ''}${set.reps ? ' x ' + set.reps + (lang === 'ru' ? 'повт' : 'reps') : ''} ${set.distanceMeters ? set.distanceMeters + (lang === 'ru' ? 'м' : 'm') : ''}`).join(', ')
|
||
}));
|
||
|
||
const systemInstruction = lang === 'ru' ? `
|
||
Ты — опытный и поддерживающий фитнес-тренер.
|
||
Твоя задача — анализировать тренировки пользователя и давать краткие, полезные советы на русском языке.
|
||
|
||
Учитывай вес пользователя (userWeight в json), если он указан, при анализе прогресса в упражнениях с собственным весом.
|
||
|
||
Вот последние 10 тренировок пользователя (в формате JSON):
|
||
${JSON.stringify(summary)}
|
||
|
||
Если пользователь спрашивает о прогрессе, используй эти данные.
|
||
Отвечай емко, мотивирующе. Избегай длинных лекций, если не просили.
|
||
` : `
|
||
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 {
|
||
sendMessage: async (userMessage: string) => {
|
||
const res = await api.post('/ai/chat', {
|
||
systemInstruction,
|
||
userMessage
|
||
});
|
||
return {
|
||
text: res.response,
|
||
response: {
|
||
text: () => res.response
|
||
}
|
||
};
|
||
}
|
||
};
|
||
}; |