fix ai activity plan conversion and immediate goals refresh
This commit is contained in:
parent
e119f0923c
commit
c877577fba
@ -66,11 +66,13 @@ function inferGoalType(text: string): ParsedPlanItem["goalType"] {
|
|||||||
|
|
||||||
function parseActivityPlanToItems(activityPlan: string): ParsedPlanItem[] {
|
function parseActivityPlanToItems(activityPlan: string): ParsedPlanItem[] {
|
||||||
const lines = activityPlan
|
const lines = activityPlan
|
||||||
.split("\n")
|
.replace(/\r\n/g, "\n")
|
||||||
|
.split(/\n+/)
|
||||||
|
.flatMap((line) => line.split(/(?<=[.!?])\s+(?=[A-Z0-9])/g))
|
||||||
.map((line) => line.trim())
|
.map((line) => line.trim())
|
||||||
.filter(Boolean)
|
.filter(Boolean)
|
||||||
.map((line) => line.replace(/^[-*•\d.)\s]+/, "").trim())
|
.map((line) => line.replace(/^[-*•\d.)\s]+/, "").trim())
|
||||||
.filter((line) => line.length > 4)
|
.filter((line) => line.length > 10)
|
||||||
.slice(0, 8);
|
.slice(0, 8);
|
||||||
|
|
||||||
const uniqueLines = Array.from(new Set(lines));
|
const uniqueLines = Array.from(new Set(lines));
|
||||||
@ -83,6 +85,21 @@ function parseActivityPlanToItems(activityPlan: string): ParsedPlanItem[] {
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getDefaultPlanItems(): ParsedPlanItem[] {
|
||||||
|
const defaults = [
|
||||||
|
"Complete 3 strength sessions this week with progressive overload.",
|
||||||
|
"Add 2 cardio sessions of 25-30 minutes for endurance.",
|
||||||
|
"Do a 10-minute mobility routine daily after training.",
|
||||||
|
];
|
||||||
|
|
||||||
|
return defaults.map((line) => ({
|
||||||
|
id: crypto.randomUUID(),
|
||||||
|
title: line.length > 72 ? `${line.slice(0, 69)}...` : line,
|
||||||
|
description: line,
|
||||||
|
goalType: inferGoalType(line),
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
export async function POST() {
|
export async function POST() {
|
||||||
try {
|
try {
|
||||||
const { userId } = await auth();
|
const { userId } = await auth();
|
||||||
@ -266,9 +283,21 @@ export async function POST() {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
const planItems = parseActivityPlanToItems(
|
let planItems = parseActivityPlanToItems(parsedResponse.activityPlan || "");
|
||||||
parsedResponse.activityPlan || "",
|
|
||||||
);
|
if (planItems.length === 0 && parsedResponse.recommendationText) {
|
||||||
|
planItems = parseActivityPlanToItems(parsedResponse.recommendationText);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (planItems.length === 0) {
|
||||||
|
planItems = getDefaultPlanItems();
|
||||||
|
}
|
||||||
|
|
||||||
|
log.debug("AI plan parsed into goal items", {
|
||||||
|
recommendationId: recommendation.id,
|
||||||
|
userId,
|
||||||
|
parsedItems: planItems.length,
|
||||||
|
});
|
||||||
|
|
||||||
const createdGoals = await Promise.all(
|
const createdGoals = await Promise.all(
|
||||||
planItems.map((item) =>
|
planItems.map((item) =>
|
||||||
|
|||||||
@ -200,10 +200,10 @@ export default function GoalsScreen() {
|
|||||||
try {
|
try {
|
||||||
setIsGeneratingPlan(true);
|
setIsGeneratingPlan(true);
|
||||||
await generateSelfPlan();
|
await generateSelfPlan();
|
||||||
await refetchRecommendations();
|
await Promise.all([refetchRecommendations(), refetchGoals()]);
|
||||||
Alert.alert(
|
Alert.alert(
|
||||||
"Plan Ready",
|
"Plan Ready",
|
||||||
"Your new AI activity plan has been generated.",
|
"Your new AI activity plan has been generated and active goals were added.",
|
||||||
);
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const message =
|
const message =
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user