@trainheroic-unofficial/coach-mcp 2.0.1 → 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/server.mjs +448 -412
- package/package.json +3 -3
package/dist/server.mjs
CHANGED
|
@@ -317,13 +317,24 @@ const leaderboardSpecSchema = z.union([
|
|
|
317
317
|
instruction: z.string().optional()
|
|
318
318
|
})
|
|
319
319
|
]);
|
|
320
|
-
/**
|
|
320
|
+
/**
|
|
321
|
+
* A block (group of exercises); two exercises render as a superset.
|
|
322
|
+
* Empty `exercises` is allowed only with a non-empty `instruction` — that is TrainHeroic's
|
|
323
|
+
* free-text Circuit / Conditioning block (type 1): title + instruction blurb, no linked lifts.
|
|
324
|
+
*/
|
|
321
325
|
const blockSpecSchema = z.object({
|
|
322
326
|
title: z.string(),
|
|
323
327
|
type: z.number().optional(),
|
|
324
328
|
instruction: z.string().optional(),
|
|
325
329
|
leaderboard: leaderboardSpecSchema.optional(),
|
|
326
330
|
exercises: z.array(exerciseSpecSchema)
|
|
331
|
+
}).superRefine((block, ctx) => {
|
|
332
|
+
if (block.exercises.length > 0) return;
|
|
333
|
+
if ((block.instruction?.trim() ?? "") === "") ctx.addIssue({
|
|
334
|
+
code: "custom",
|
|
335
|
+
message: "A block with no exercises needs a non-empty instruction (text-only Circuit / Conditioning block). Otherwise provide at least one exercise.",
|
|
336
|
+
path: ["exercises"]
|
|
337
|
+
});
|
|
327
338
|
});
|
|
328
339
|
z.object({
|
|
329
340
|
blocks: z.array(blockSpecSchema),
|
|
@@ -1837,95 +1848,453 @@ async function logSessionForAthlete(client, args) {
|
|
|
1837
1848
|
sets
|
|
1838
1849
|
};
|
|
1839
1850
|
}
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
|
|
1846
|
-
|
|
1847
|
-
|
|
1848
|
-
|
|
1849
|
-
|
|
1851
|
+
//#endregion
|
|
1852
|
+
//#region ../js/src/workout-encode.ts
|
|
1853
|
+
const LEADERBOARD_TYPE = {
|
|
1854
|
+
completion: 0,
|
|
1855
|
+
"for completion": 0,
|
|
1856
|
+
weight: 1,
|
|
1857
|
+
lb: 1,
|
|
1858
|
+
load: 1,
|
|
1859
|
+
reps: 2,
|
|
1860
|
+
rep: 2,
|
|
1861
|
+
rounds: 3,
|
|
1862
|
+
round: 3,
|
|
1863
|
+
time: 4,
|
|
1864
|
+
yards: 5,
|
|
1865
|
+
yd: 5,
|
|
1866
|
+
meters: 6,
|
|
1867
|
+
m: 6,
|
|
1868
|
+
feet: 7,
|
|
1869
|
+
ft: 7,
|
|
1870
|
+
calories: 8,
|
|
1871
|
+
cal: 8,
|
|
1872
|
+
cals: 8,
|
|
1873
|
+
miles: 10,
|
|
1874
|
+
mi: 10,
|
|
1875
|
+
inches: 12,
|
|
1876
|
+
in: 12,
|
|
1877
|
+
watts: 15,
|
|
1878
|
+
w: 15,
|
|
1879
|
+
velocity: 17,
|
|
1880
|
+
"m/s": 17,
|
|
1881
|
+
seconds: 18,
|
|
1882
|
+
sec: 18,
|
|
1883
|
+
s: 18
|
|
1884
|
+
};
|
|
1885
|
+
const LEADERBOARD_LABEL = {
|
|
1886
|
+
0: "For Completion",
|
|
1887
|
+
1: "Weight",
|
|
1888
|
+
2: "Reps",
|
|
1889
|
+
3: "Rounds",
|
|
1890
|
+
4: "Time",
|
|
1891
|
+
5: "Yards",
|
|
1892
|
+
6: "Meters",
|
|
1893
|
+
7: "Feet",
|
|
1894
|
+
8: "Calories",
|
|
1895
|
+
10: "Miles",
|
|
1896
|
+
12: "Inches",
|
|
1897
|
+
13: "Other",
|
|
1898
|
+
15: "Watts",
|
|
1899
|
+
16: "Percent",
|
|
1900
|
+
17: "Velocity",
|
|
1901
|
+
18: "Seconds"
|
|
1902
|
+
};
|
|
1903
|
+
function resolveLeaderboard(block) {
|
|
1904
|
+
const lb = block.leaderboard;
|
|
1905
|
+
if (lb === void 0 || lb === null) return {
|
|
1906
|
+
isRedzone: null,
|
|
1907
|
+
redzoneType: 0,
|
|
1908
|
+
smallerIsBetter: null,
|
|
1909
|
+
redzoneInstruction: ""
|
|
1910
|
+
};
|
|
1911
|
+
let unit;
|
|
1912
|
+
let instruction = "";
|
|
1913
|
+
let lowest;
|
|
1914
|
+
if (typeof lb === "object") {
|
|
1915
|
+
unit = lb.unit ?? lb.type;
|
|
1916
|
+
instruction = lb.instruction ?? "";
|
|
1917
|
+
lowest = lb.lowest_wins;
|
|
1918
|
+
} else unit = lb;
|
|
1919
|
+
let rz;
|
|
1920
|
+
if (typeof unit === "string") {
|
|
1921
|
+
const found = LEADERBOARD_TYPE[unit.trim().toLowerCase()];
|
|
1922
|
+
if (found === void 0) throw new Error(`Unknown leaderboard unit '${unit}'. Use one of: ${Object.keys(LEADERBOARD_TYPE).join(", ")}.`);
|
|
1923
|
+
rz = found;
|
|
1924
|
+
} else if (typeof unit === "number") rz = Math.trunc(unit);
|
|
1925
|
+
else throw new Error("Leaderboard requires a unit.");
|
|
1926
|
+
if (lowest === void 0) lowest = rz === 4 || rz === 18;
|
|
1927
|
+
return {
|
|
1928
|
+
isRedzone: 1,
|
|
1929
|
+
redzoneType: rz,
|
|
1930
|
+
smallerIsBetter: lowest ? 1 : 0,
|
|
1931
|
+
redzoneInstruction: instruction
|
|
1932
|
+
};
|
|
1850
1933
|
}
|
|
1851
|
-
|
|
1852
|
-
* The coach's roster as id + display name, from /v5/athletes. One home for the id coercion and the
|
|
1853
|
-
* name-field choice (the API uses `fullName` / `firstName`+`lastName`), so the CLI, the MCP tools,
|
|
1854
|
-
* and the warehouse store map the roster identically. Includes the account owner and
|
|
1855
|
-
* demo/placeholder athletes (the raw roster does); pass `ids` to keep only a subset.
|
|
1856
|
-
*/
|
|
1857
|
-
async function fetchCoachRoster(client, ids) {
|
|
1858
|
-
const res = await client.request("GET", "/v5/athletes");
|
|
1859
|
-
if (!res.ok || !Array.isArray(res.data)) throw new Error(`List athletes failed (HTTP ${res.status}).`);
|
|
1860
|
-
const keep = ids && ids.length > 0 ? new Set(ids) : null;
|
|
1934
|
+
function slots(values, n = 10) {
|
|
1861
1935
|
const out = [];
|
|
1862
|
-
for (
|
|
1863
|
-
if (typeof row !== "object" || row === null) continue;
|
|
1864
|
-
const rec = row;
|
|
1865
|
-
const id = coerceInt(rec.id);
|
|
1866
|
-
if (id === null || id <= 0) continue;
|
|
1867
|
-
if (keep && !keep.has(id)) continue;
|
|
1868
|
-
out.push({
|
|
1869
|
-
id,
|
|
1870
|
-
name: rosterName(rec)
|
|
1871
|
-
});
|
|
1872
|
-
}
|
|
1936
|
+
for (let i = 0; i < n; i += 1) out.push(values && i < values.length ? values[i] ?? "" : "");
|
|
1873
1937
|
return out;
|
|
1874
1938
|
}
|
|
1939
|
+
function repsList(ex) {
|
|
1940
|
+
const reps = ex.reps;
|
|
1941
|
+
if (Array.isArray(reps)) return reps.map((r) => String(r));
|
|
1942
|
+
if (reps === void 0 || reps === null) return [];
|
|
1943
|
+
const sets = Math.max(1, Math.trunc(Number(ex.sets ?? 1)) || 1);
|
|
1944
|
+
return Array.from({ length: sets }, () => String(reps));
|
|
1945
|
+
}
|
|
1946
|
+
/** Build one saveWorkoutSetExercises payload entry with all ten param slots filled. */
|
|
1947
|
+
function makeExercise(ex, workoutSetId, order, key) {
|
|
1948
|
+
const reps = repsList(ex);
|
|
1949
|
+
let instruction = ex.instr ?? "";
|
|
1950
|
+
if (instruction === "" && ex.rpe !== void 0 && ex.rpe !== null) instruction = `RPE ${ex.rpe}`;
|
|
1951
|
+
const hasWeight = ex.weight !== void 0 && ex.weight !== null;
|
|
1952
|
+
const weightArr = Array.isArray(ex.weight) ? ex.weight : null;
|
|
1953
|
+
let count = reps.length;
|
|
1954
|
+
if (count === 0 && hasWeight) count = weightArr ? weightArr.length : Math.max(1, Math.trunc(Number(ex.sets ?? 1)) || 1);
|
|
1955
|
+
let param2Type;
|
|
1956
|
+
let param2Values;
|
|
1957
|
+
if (hasWeight) {
|
|
1958
|
+
param2Type = ex.param_2_type ?? 1;
|
|
1959
|
+
param2Values = (weightArr ?? Array.from({ length: count }, () => ex.weight)).map((v) => String(v));
|
|
1960
|
+
} else {
|
|
1961
|
+
param2Type = 0;
|
|
1962
|
+
param2Values = null;
|
|
1963
|
+
}
|
|
1964
|
+
const entry = {
|
|
1965
|
+
exercise_id: ex.id,
|
|
1966
|
+
workout_set_id: workoutSetId,
|
|
1967
|
+
set_id: workoutSetId,
|
|
1968
|
+
setKey: workoutSetId,
|
|
1969
|
+
title: ex.title ?? "",
|
|
1970
|
+
instruction,
|
|
1971
|
+
order,
|
|
1972
|
+
param_1_type: ex.param_1_type ?? 3,
|
|
1973
|
+
param_2_type: param2Type,
|
|
1974
|
+
workout_set_exercise_template_id: null,
|
|
1975
|
+
no_sets: 0,
|
|
1976
|
+
param_count: count,
|
|
1977
|
+
set_num: count,
|
|
1978
|
+
key,
|
|
1979
|
+
video_url: "",
|
|
1980
|
+
thumbnail_url: "",
|
|
1981
|
+
tags: [],
|
|
1982
|
+
eType: "e",
|
|
1983
|
+
use_count: 0
|
|
1984
|
+
};
|
|
1985
|
+
const p1 = slots(reps);
|
|
1986
|
+
const p2 = slots(param2Values);
|
|
1987
|
+
for (let i = 0; i < 10; i += 1) {
|
|
1988
|
+
entry[`param_1_data_${i + 1}`] = p1[i] ?? "";
|
|
1989
|
+
entry[`param_2_data_${i + 1}`] = p2[i] ?? "";
|
|
1990
|
+
}
|
|
1991
|
+
return entry;
|
|
1992
|
+
}
|
|
1875
1993
|
/**
|
|
1876
|
-
*
|
|
1877
|
-
*
|
|
1878
|
-
* (`POST /v5/athletes/inviteToTeam`). Throws with the API detail on failure.
|
|
1994
|
+
* Default block type: text-only (empty exercises) blocks are Conditioning / Circuit (type 1);
|
|
1995
|
+
* structured exercise blocks default to Hypertrophy (type 2). An explicit `type` always wins.
|
|
1879
1996
|
*/
|
|
1880
|
-
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
|
|
1884
|
-
|
|
1885
|
-
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
|
|
1997
|
+
function defaultBlockType(block) {
|
|
1998
|
+
if (block.type !== void 0) return block.type;
|
|
1999
|
+
return block.exercises.length === 0 ? 1 : 2;
|
|
2000
|
+
}
|
|
2001
|
+
function buildBlockPayload(blocks, workoutId) {
|
|
2002
|
+
return blocks.map((b, i) => {
|
|
2003
|
+
const lb = resolveLeaderboard(b);
|
|
2004
|
+
return {
|
|
2005
|
+
workout_id: workoutId,
|
|
2006
|
+
order: i + 1,
|
|
2007
|
+
type: defaultBlockType(b),
|
|
2008
|
+
instruction: b.instruction ?? "",
|
|
2009
|
+
is_redzone: lb.isRedzone,
|
|
2010
|
+
redzone_type: lb.redzoneType,
|
|
2011
|
+
smaller_is_better: lb.smallerIsBetter,
|
|
2012
|
+
redzone_instruction: lb.redzoneInstruction,
|
|
2013
|
+
exercises: [],
|
|
2014
|
+
exerciseKeys: [],
|
|
2015
|
+
key: `k::${workoutId}${i + 1}`,
|
|
2016
|
+
title: b.title
|
|
2017
|
+
};
|
|
2018
|
+
});
|
|
2019
|
+
}
|
|
2020
|
+
function unitOr(t) {
|
|
2021
|
+
return unitLabel(t) ?? "?";
|
|
2022
|
+
}
|
|
2023
|
+
/** Flag spec params the API will silently override to the exercise's fixed units. */
|
|
2024
|
+
function unitAdvisory(blockTitle, ex, defaults) {
|
|
2025
|
+
const notes = [];
|
|
2026
|
+
const warnings = [];
|
|
2027
|
+
const u = unitOr;
|
|
2028
|
+
const label = `${blockTitle} / ${ex.title ?? ex.id}`;
|
|
2029
|
+
if (Array.isArray(ex.reps) && ex.sets !== void 0 && ex.reps.length !== ex.sets) warnings.push(`${label}: reps array has ${ex.reps.length} entr${ex.reps.length === 1 ? "y" : "ies"}; sets:${ex.sets} is ignored — building ${ex.reps.length} set(s).`);
|
|
2030
|
+
const sentP1 = ex.param_1_type;
|
|
2031
|
+
if (sentP1 !== void 0 && sentP1 !== null && Math.trunc(Number(sentP1)) !== defaults.param1) {
|
|
2032
|
+
const sp1 = Math.trunc(Number(sentP1));
|
|
2033
|
+
warnings.push(`${label}: param_1_type ${sentP1} (${u(sp1)}) is ignored — this exercise is fixed to ${u(defaults.param1)}; values render as ${u(defaults.param1)}.`);
|
|
2034
|
+
} else if (defaults.param1 !== 3 && defaults.param1 !== null) notes.push(`${label}: values are in ${u(defaults.param1)} (the exercise's fixed primary unit).`);
|
|
2035
|
+
if (ex.weight !== void 0 && ex.weight !== null) {
|
|
2036
|
+
const sentP2 = Math.trunc(Number(ex.param_2_type ?? 1));
|
|
2037
|
+
const effP2 = defaults.param2 === 0 || defaults.param2 === null ? 1 : defaults.param2;
|
|
2038
|
+
if (sentP2 !== effP2) if (sentP2 === 2 || sentP2 === 14) warnings.push(`${label}: ${u(sentP2)} does not stick on this exercise — it renders as ${u(effP2)}. Put it in the exercise 'instr' text and leave load blank.`);
|
|
2039
|
+
else warnings.push(`${label}: load renders as ${u(effP2)}, not ${u(sentP2)} (this exercise's secondary unit is fixed).`);
|
|
1900
2040
|
}
|
|
1901
2041
|
return {
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
result: invite.data
|
|
2042
|
+
notes,
|
|
2043
|
+
warnings
|
|
1905
2044
|
};
|
|
1906
2045
|
}
|
|
1907
2046
|
/**
|
|
1908
|
-
*
|
|
1909
|
-
*
|
|
1910
|
-
*
|
|
2047
|
+
* Run unit advisories across a whole block list against an exercise index. Shared by the
|
|
2048
|
+
* MCP workout_build tool and the CLI so both surface the same notes/warnings. Ensures the
|
|
2049
|
+
* index is loaded first, otherwise `defaults` returns null on a cold index and every
|
|
2050
|
+
* advisory is silently dropped.
|
|
1911
2051
|
*/
|
|
1912
|
-
async function
|
|
1913
|
-
|
|
1914
|
-
const
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
2052
|
+
async function collectAdvisories(blocks, index) {
|
|
2053
|
+
await index.ensureFresh();
|
|
2054
|
+
const pairs = blocks.flatMap((b) => b.exercises.map((ex) => ({
|
|
2055
|
+
block: b,
|
|
2056
|
+
ex
|
|
2057
|
+
})));
|
|
2058
|
+
const defaults = await Promise.all(pairs.map((p) => {
|
|
2059
|
+
const id = Number(p.ex.id);
|
|
2060
|
+
return Number.isFinite(id) ? index.defaults(id) : Promise.resolve(null);
|
|
2061
|
+
}));
|
|
2062
|
+
const notes = [];
|
|
2063
|
+
const warnings = [];
|
|
2064
|
+
pairs.forEach((p, i) => {
|
|
2065
|
+
const def = defaults[i];
|
|
2066
|
+
if (!def) return;
|
|
2067
|
+
const advisory = unitAdvisory(p.block.title, p.ex, def);
|
|
2068
|
+
notes.push(...advisory.notes);
|
|
2069
|
+
warnings.push(...advisory.warnings);
|
|
2070
|
+
});
|
|
2071
|
+
return {
|
|
2072
|
+
notes,
|
|
2073
|
+
warnings
|
|
2074
|
+
};
|
|
2075
|
+
}
|
|
2076
|
+
//#endregion
|
|
2077
|
+
//#region ../js/src/workout-session.ts
|
|
2078
|
+
/**
|
|
2079
|
+
* Coach calendar writes (`createWorkoutForDay`, timeline create, `copyProgramWorkout`) 500/401
|
|
2080
|
+
* on personal calendars (Coach Plan / `personal_cal`). Shared by `buildSession` and
|
|
2081
|
+
* `copySession` so the hint stays in one place.
|
|
2082
|
+
*/
|
|
2083
|
+
function calendarWriteError(method, path, status, detail) {
|
|
2084
|
+
const base = `${method} ${path} failed (HTTP ${status}): ${detail}`;
|
|
2085
|
+
if (!(path.includes("/createWorkoutForDay/") || path.includes("/createWorkoutForTimelineDay/") || path.includes("/copyProgramWorkout")) || status !== 500 && status !== 401) return new Error(base);
|
|
2086
|
+
return /* @__PURE__ */ new Error(`${base}. This often means the program is another athlete's Coach Plan / personal calendar (personal_cal), which the coach calendar write endpoints do not support — workout_build and session_copy work on team/group programs (and the coach's own personal calendar), but there is currently no coach-facing tool to create sessions on another athlete's personal calendar.`);
|
|
2087
|
+
}
|
|
2088
|
+
async function req(client, method, path, body) {
|
|
2089
|
+
const res = await client.request(method, path, body === void 0 ? void 0 : { body });
|
|
2090
|
+
if (!res.ok) {
|
|
2091
|
+
const detail = typeof res.data === "string" ? res.data : JSON.stringify(res.data);
|
|
2092
|
+
throw calendarWriteError(method, path, res.status, detail);
|
|
2093
|
+
}
|
|
2094
|
+
return res.data;
|
|
2095
|
+
}
|
|
2096
|
+
function createPath(opts) {
|
|
2097
|
+
if (opts.timelineDay !== void 0) return `/2.0/coach/calendar/workout/createWorkoutForTimelineDay/${opts.programId}/${opts.timelineDay}/null`;
|
|
2098
|
+
if (!opts.date) throw new Error("workout build requires either date or timelineDay");
|
|
2099
|
+
const [y, m, d] = opts.date;
|
|
2100
|
+
return `/2.0/coach/calendar/workout/createWorkoutForDay/${opts.programId}/${y}/${m}/${d}/0`;
|
|
2101
|
+
}
|
|
2102
|
+
async function buildSession(client, opts) {
|
|
2103
|
+
const blocks = z.array(blockSpecSchema).parse(opts.blocks);
|
|
2104
|
+
const sess = await req(client, "POST", createPath(opts), {});
|
|
2105
|
+
checkResponse(sessionCreateResponseSchema, sess, "session create");
|
|
2106
|
+
const workoutId = Number(sess.workout_id);
|
|
2107
|
+
const pwId = Number(sess.id);
|
|
2108
|
+
const created = await req(client, "POST", "/2.0/coach/calendar/saveProgramWorkoutSets", buildBlockPayload(blocks, workoutId));
|
|
2109
|
+
const byOrder = new Map(created.map((b) => [b.order, b.id]));
|
|
2110
|
+
let counter = 0;
|
|
2111
|
+
const payloads = blocks.map((block, i) => {
|
|
2112
|
+
const wsid = byOrder.get(i + 1);
|
|
2113
|
+
if (wsid === void 0) throw new Error(`No saved block for order ${i + 1}.`);
|
|
2114
|
+
return block.exercises.map((ex, j) => {
|
|
2115
|
+
counter += 1;
|
|
2116
|
+
return makeExercise(ex, wsid, j + 1, `k::${workoutId}${String(counter).padStart(3, "0")}`);
|
|
2117
|
+
});
|
|
2118
|
+
});
|
|
2119
|
+
await Promise.all(payloads.filter((p) => p.length > 0).map((p) => req(client, "POST", "/2.0/coach/calendar/saveWorkoutSetExercises", p)));
|
|
2120
|
+
if (opts.instruction !== void 0 && opts.instruction !== "") {
|
|
2121
|
+
const blockIds = [...byOrder.entries()].sort((a, b) => a[0] - b[0]).map(([, id]) => id);
|
|
2122
|
+
await setSessionInstruction(client, workoutId, sess, opts.instruction, blockIds);
|
|
2123
|
+
}
|
|
2124
|
+
if (opts.publish ?? false) await req(client, "POST", "/2.0/coach/calendar/programWorkout/publish", [pwId]);
|
|
2125
|
+
return {
|
|
2126
|
+
pwId,
|
|
2127
|
+
workoutId
|
|
2128
|
+
};
|
|
2129
|
+
}
|
|
2130
|
+
/**
|
|
2131
|
+
* Set a session's Coach Instructions (the day-note at the top of a session). `pw` is the
|
|
2132
|
+
* programWorkout object (the create-time response or a day's edit-GET entry). The PUT wants
|
|
2133
|
+
* the whole object back with `instruction` set and `sets`/`setKeys` as a flat list of block
|
|
2134
|
+
* ids. This does NOT change publish state: `published` is sent exactly as it is on `pw`.
|
|
2135
|
+
*/
|
|
2136
|
+
async function setSessionInstruction(client, workoutId, pw, instruction, blockIds) {
|
|
2137
|
+
const body = {
|
|
2138
|
+
...pw,
|
|
2139
|
+
instruction,
|
|
2140
|
+
sets: blockIds,
|
|
2141
|
+
setKeys: blockIds
|
|
2142
|
+
};
|
|
2143
|
+
await req(client, "PUT", `/3.0/coach/workout/${workoutId}`, body);
|
|
2144
|
+
}
|
|
2145
|
+
async function removeSession(client, programId, pwId) {
|
|
2146
|
+
await req(client, "POST", "/2.0/coach/calendar/removeProgramWorkout", {
|
|
2147
|
+
programId,
|
|
2148
|
+
pwId
|
|
2149
|
+
});
|
|
2150
|
+
}
|
|
2151
|
+
async function publishSession(client, pwId) {
|
|
2152
|
+
await req(client, "POST", "/2.0/coach/calendar/programWorkout/publish", [pwId]);
|
|
2153
|
+
}
|
|
2154
|
+
function str(value) {
|
|
2155
|
+
return value === void 0 || value === null ? "" : String(value);
|
|
2156
|
+
}
|
|
2157
|
+
async function readSession(client, programId, date, pwId) {
|
|
2158
|
+
const [y, m, d] = date;
|
|
2159
|
+
const data = await req(client, "GET", `/1.0/coach/programs/edit/${programId}/${y}/${m}/${d}`);
|
|
2160
|
+
checkResponse(programsEditResponseSchema, data, "programs edit");
|
|
2161
|
+
const pw = (data.programWorkouts ?? []).find((p) => coerceInt(p.id) === pwId);
|
|
2162
|
+
if (!pw) throw new Error(`programWorkout ${pwId} not found on ${y}-${m}-${d}.`);
|
|
2163
|
+
const setsObj = pw.sets ?? {};
|
|
2164
|
+
const blocks = Object.values(setsObj).sort((a, b) => Number(a.order) - Number(b.order)).map((b) => readBlock(b));
|
|
2165
|
+
return {
|
|
2166
|
+
pwId,
|
|
2167
|
+
date: `${str(pw.year)}-${str(pw.month)}-${str(pw.day)}`,
|
|
2168
|
+
published: pw.published,
|
|
2169
|
+
instruction: str(pw.instruction),
|
|
2170
|
+
blocks
|
|
2171
|
+
};
|
|
2172
|
+
}
|
|
2173
|
+
function readBlock(b) {
|
|
2174
|
+
const rz = coerceInt(b.redzone_type);
|
|
2175
|
+
let leaderboard = null;
|
|
2176
|
+
if (rz && rz > 0) leaderboard = `FOR ${(LEADERBOARD_LABEL[rz] ?? `type ${rz}`).toUpperCase()}${b.smaller_is_better ? " (lowest wins)" : ""}`;
|
|
2177
|
+
const exercises = (Array.isArray(b.exercises) ? b.exercises : []).filter((ex) => coerceInt(ex.id) !== null || str(ex.title) !== "").sort((a, e) => Number(a.order) - Number(e.order)).map((ex) => readExercise(ex));
|
|
2178
|
+
return {
|
|
2179
|
+
order: Number(b.order),
|
|
2180
|
+
title: str(b.title),
|
|
2181
|
+
instruction: str(b.instruction),
|
|
2182
|
+
leaderboard,
|
|
2183
|
+
exercises
|
|
2184
|
+
};
|
|
2185
|
+
}
|
|
2186
|
+
function readExercise(ex) {
|
|
2187
|
+
const reps = [];
|
|
2188
|
+
const load = [];
|
|
2189
|
+
for (let i = 1; i <= 10; i += 1) {
|
|
2190
|
+
const r = str(ex[`param_1_data_${i}`]);
|
|
2191
|
+
if (r !== "") reps.push(r);
|
|
2192
|
+
const w = str(ex[`param_2_data_${i}`]);
|
|
2193
|
+
if (w !== "") load.push(w);
|
|
2194
|
+
}
|
|
2195
|
+
return {
|
|
2196
|
+
order: Number(ex.order),
|
|
2197
|
+
title: str(ex.title),
|
|
2198
|
+
reps,
|
|
2199
|
+
primaryUnit: unitLabel(coerceInt(ex.param_1_type)),
|
|
2200
|
+
load,
|
|
2201
|
+
loadUnit: unitLabel(coerceInt(ex.param_2_type)),
|
|
2202
|
+
instruction: str(ex.instruction)
|
|
2203
|
+
};
|
|
2204
|
+
}
|
|
2205
|
+
/** Normalize one-or-many emails into a deduped, trimmed list. */
|
|
2206
|
+
function emailList(emails) {
|
|
2207
|
+
return [...new Set(emails.map((e) => e.trim()).filter((e) => e.length > 0))];
|
|
2208
|
+
}
|
|
2209
|
+
/** The display name on a /v5/athletes row: `fullName` (e.g. "Cohen, A"), else first+last. */
|
|
2210
|
+
function rosterName(rec) {
|
|
2211
|
+
const full = typeof rec.fullName === "string" ? rec.fullName.trim() : "";
|
|
2212
|
+
if (full !== "") return full;
|
|
2213
|
+
const combined = `${typeof rec.firstName === "string" ? rec.firstName : ""} ${typeof rec.lastName === "string" ? rec.lastName : ""}`.trim();
|
|
2214
|
+
return combined !== "" ? combined : null;
|
|
2215
|
+
}
|
|
2216
|
+
/**
|
|
2217
|
+
* The coach's roster as id + display name, from /v5/athletes. One home for the id coercion and the
|
|
2218
|
+
* name-field choice (the API uses `fullName` / `firstName`+`lastName`), so the CLI, the MCP tools,
|
|
2219
|
+
* and the warehouse store map the roster identically. Includes the account owner and
|
|
2220
|
+
* demo/placeholder athletes (the raw roster does); pass `ids` to keep only a subset.
|
|
2221
|
+
*/
|
|
2222
|
+
async function fetchCoachRoster(client, ids) {
|
|
2223
|
+
const res = await client.request("GET", "/v5/athletes");
|
|
2224
|
+
if (!res.ok || !Array.isArray(res.data)) throw new Error(`List athletes failed (HTTP ${res.status}).`);
|
|
2225
|
+
const keep = ids && ids.length > 0 ? new Set(ids) : null;
|
|
2226
|
+
const out = [];
|
|
2227
|
+
for (const row of res.data) {
|
|
2228
|
+
if (typeof row !== "object" || row === null) continue;
|
|
2229
|
+
const rec = row;
|
|
2230
|
+
const id = coerceInt(rec.id);
|
|
2231
|
+
if (id === null || id <= 0) continue;
|
|
2232
|
+
if (keep && !keep.has(id)) continue;
|
|
2233
|
+
out.push({
|
|
2234
|
+
id,
|
|
2235
|
+
name: rosterName(rec)
|
|
2236
|
+
});
|
|
2237
|
+
}
|
|
2238
|
+
return out;
|
|
2239
|
+
}
|
|
2240
|
+
/**
|
|
2241
|
+
* Invite athletes to a team — TrainHeroic's two-step "create athlete" flow: validate the
|
|
2242
|
+
* addresses (`POST /v5/emails/validate`), then invite the validated ones
|
|
2243
|
+
* (`POST /v5/athletes/inviteToTeam`). Throws with the API detail on failure.
|
|
2244
|
+
*/
|
|
2245
|
+
async function inviteAthletes(client, args) {
|
|
2246
|
+
const list = emailList(args.emails);
|
|
2247
|
+
if (list.length === 0) throw new Error("Provide at least one email address to invite.");
|
|
2248
|
+
const validation = await client.request("POST", "/v5/emails/validate", { body: { emails: list.join(",") } });
|
|
2249
|
+
if (!validation.ok) {
|
|
2250
|
+
const detail = typeof validation.data === "string" ? validation.data : JSON.stringify(validation.data);
|
|
2251
|
+
throw new Error(`Email validation failed (HTTP ${validation.status}): ${detail}`);
|
|
2252
|
+
}
|
|
2253
|
+
const valid = Array.isArray(validation.data) ? validation.data : list;
|
|
2254
|
+
if (valid.length === 0) throw new Error(`No valid addresses among: ${list.join(", ")}. They may be malformed or already on the team.`);
|
|
2255
|
+
const invite = await client.request("POST", "/v5/athletes/inviteToTeam", { body: {
|
|
2256
|
+
teamType: 0,
|
|
2257
|
+
teamId: args.teamId,
|
|
2258
|
+
orgId: null,
|
|
2259
|
+
emails: valid,
|
|
2260
|
+
message: args.message ?? "Follow these steps and you'll be set up and ready to go!"
|
|
2261
|
+
} });
|
|
2262
|
+
if (!invite.ok) {
|
|
2263
|
+
const detail = typeof invite.data === "string" ? invite.data : JSON.stringify(invite.data);
|
|
2264
|
+
throw new Error(`Invite failed (HTTP ${invite.status}): ${detail}`);
|
|
2265
|
+
}
|
|
2266
|
+
return {
|
|
2267
|
+
teamId: args.teamId,
|
|
2268
|
+
invited: valid,
|
|
2269
|
+
result: invite.data
|
|
2270
|
+
};
|
|
2271
|
+
}
|
|
2272
|
+
/**
|
|
2273
|
+
* Copy/repeat a session to a target date on a program (`POST /2.0/coach/calendar/copyProgramWorkout`).
|
|
2274
|
+
* The API wants the target date as a structured object (with weekday and an isToday flag),
|
|
2275
|
+
* which is computed here from `toDate` (YYYY-M-D). Creates a new (unpublished) session.
|
|
2276
|
+
*/
|
|
2277
|
+
async function copySession(client, args) {
|
|
2278
|
+
const [year, month, day] = parseWorkoutDate(args.toDate);
|
|
2279
|
+
const dayOfWeek = new Date(Date.UTC(year, month - 1, day)).getUTCDay();
|
|
2280
|
+
const iso = `${year}-${String(month).padStart(2, "0")}-${String(day).padStart(2, "0")}`;
|
|
2281
|
+
const path = "/2.0/coach/calendar/copyProgramWorkout";
|
|
2282
|
+
const res = await client.request("POST", path, { body: {
|
|
2283
|
+
toProgramId: args.toProgramId,
|
|
2284
|
+
pwId: args.pwId,
|
|
2285
|
+
toDate: {
|
|
2286
|
+
date: iso,
|
|
2287
|
+
day,
|
|
2288
|
+
month,
|
|
1923
2289
|
year,
|
|
1924
2290
|
dayOfWeek,
|
|
1925
2291
|
isToday: false
|
|
1926
2292
|
}
|
|
1927
2293
|
} });
|
|
1928
|
-
if (!res.ok)
|
|
2294
|
+
if (!res.ok) {
|
|
2295
|
+
const detail = typeof res.data === "string" ? res.data : JSON.stringify(res.data);
|
|
2296
|
+
throw calendarWriteError("POST", path, res.status, detail);
|
|
2297
|
+
}
|
|
1929
2298
|
return res.data;
|
|
1930
2299
|
}
|
|
1931
2300
|
const ANALYTICS_METRIC_KEYS = [
|
|
@@ -2355,340 +2724,6 @@ function definedProps(obj) {
|
|
|
2355
2724
|
return result;
|
|
2356
2725
|
}
|
|
2357
2726
|
//#endregion
|
|
2358
|
-
//#region ../js/src/workout-encode.ts
|
|
2359
|
-
const LEADERBOARD_TYPE = {
|
|
2360
|
-
completion: 0,
|
|
2361
|
-
"for completion": 0,
|
|
2362
|
-
weight: 1,
|
|
2363
|
-
lb: 1,
|
|
2364
|
-
load: 1,
|
|
2365
|
-
reps: 2,
|
|
2366
|
-
rep: 2,
|
|
2367
|
-
rounds: 3,
|
|
2368
|
-
round: 3,
|
|
2369
|
-
time: 4,
|
|
2370
|
-
yards: 5,
|
|
2371
|
-
yd: 5,
|
|
2372
|
-
meters: 6,
|
|
2373
|
-
m: 6,
|
|
2374
|
-
feet: 7,
|
|
2375
|
-
ft: 7,
|
|
2376
|
-
calories: 8,
|
|
2377
|
-
cal: 8,
|
|
2378
|
-
cals: 8,
|
|
2379
|
-
miles: 10,
|
|
2380
|
-
mi: 10,
|
|
2381
|
-
inches: 12,
|
|
2382
|
-
in: 12,
|
|
2383
|
-
watts: 15,
|
|
2384
|
-
w: 15,
|
|
2385
|
-
velocity: 17,
|
|
2386
|
-
"m/s": 17,
|
|
2387
|
-
seconds: 18,
|
|
2388
|
-
sec: 18,
|
|
2389
|
-
s: 18
|
|
2390
|
-
};
|
|
2391
|
-
const LEADERBOARD_LABEL = {
|
|
2392
|
-
0: "For Completion",
|
|
2393
|
-
1: "Weight",
|
|
2394
|
-
2: "Reps",
|
|
2395
|
-
3: "Rounds",
|
|
2396
|
-
4: "Time",
|
|
2397
|
-
5: "Yards",
|
|
2398
|
-
6: "Meters",
|
|
2399
|
-
7: "Feet",
|
|
2400
|
-
8: "Calories",
|
|
2401
|
-
10: "Miles",
|
|
2402
|
-
12: "Inches",
|
|
2403
|
-
13: "Other",
|
|
2404
|
-
15: "Watts",
|
|
2405
|
-
16: "Percent",
|
|
2406
|
-
17: "Velocity",
|
|
2407
|
-
18: "Seconds"
|
|
2408
|
-
};
|
|
2409
|
-
function resolveLeaderboard(block) {
|
|
2410
|
-
const lb = block.leaderboard;
|
|
2411
|
-
if (lb === void 0 || lb === null) return {
|
|
2412
|
-
isRedzone: null,
|
|
2413
|
-
redzoneType: 0,
|
|
2414
|
-
smallerIsBetter: null,
|
|
2415
|
-
redzoneInstruction: ""
|
|
2416
|
-
};
|
|
2417
|
-
let unit;
|
|
2418
|
-
let instruction = "";
|
|
2419
|
-
let lowest;
|
|
2420
|
-
if (typeof lb === "object") {
|
|
2421
|
-
unit = lb.unit ?? lb.type;
|
|
2422
|
-
instruction = lb.instruction ?? "";
|
|
2423
|
-
lowest = lb.lowest_wins;
|
|
2424
|
-
} else unit = lb;
|
|
2425
|
-
let rz;
|
|
2426
|
-
if (typeof unit === "string") {
|
|
2427
|
-
const found = LEADERBOARD_TYPE[unit.trim().toLowerCase()];
|
|
2428
|
-
if (found === void 0) throw new Error(`Unknown leaderboard unit '${unit}'. Use one of: ${Object.keys(LEADERBOARD_TYPE).join(", ")}.`);
|
|
2429
|
-
rz = found;
|
|
2430
|
-
} else if (typeof unit === "number") rz = Math.trunc(unit);
|
|
2431
|
-
else throw new Error("Leaderboard requires a unit.");
|
|
2432
|
-
if (lowest === void 0) lowest = rz === 4 || rz === 18;
|
|
2433
|
-
return {
|
|
2434
|
-
isRedzone: 1,
|
|
2435
|
-
redzoneType: rz,
|
|
2436
|
-
smallerIsBetter: lowest ? 1 : 0,
|
|
2437
|
-
redzoneInstruction: instruction
|
|
2438
|
-
};
|
|
2439
|
-
}
|
|
2440
|
-
function slots(values, n = 10) {
|
|
2441
|
-
const out = [];
|
|
2442
|
-
for (let i = 0; i < n; i += 1) out.push(values && i < values.length ? values[i] ?? "" : "");
|
|
2443
|
-
return out;
|
|
2444
|
-
}
|
|
2445
|
-
function repsList(ex) {
|
|
2446
|
-
const reps = ex.reps;
|
|
2447
|
-
if (Array.isArray(reps)) return reps.map((r) => String(r));
|
|
2448
|
-
if (reps === void 0 || reps === null) return [];
|
|
2449
|
-
const sets = Math.max(1, Math.trunc(Number(ex.sets ?? 1)) || 1);
|
|
2450
|
-
return Array.from({ length: sets }, () => String(reps));
|
|
2451
|
-
}
|
|
2452
|
-
/** Build one saveWorkoutSetExercises payload entry with all ten param slots filled. */
|
|
2453
|
-
function makeExercise(ex, workoutSetId, order, key) {
|
|
2454
|
-
const reps = repsList(ex);
|
|
2455
|
-
let instruction = ex.instr ?? "";
|
|
2456
|
-
if (instruction === "" && ex.rpe !== void 0 && ex.rpe !== null) instruction = `RPE ${ex.rpe}`;
|
|
2457
|
-
const hasWeight = ex.weight !== void 0 && ex.weight !== null;
|
|
2458
|
-
const weightArr = Array.isArray(ex.weight) ? ex.weight : null;
|
|
2459
|
-
let count = reps.length;
|
|
2460
|
-
if (count === 0 && hasWeight) count = weightArr ? weightArr.length : Math.max(1, Math.trunc(Number(ex.sets ?? 1)) || 1);
|
|
2461
|
-
let param2Type;
|
|
2462
|
-
let param2Values;
|
|
2463
|
-
if (hasWeight) {
|
|
2464
|
-
param2Type = ex.param_2_type ?? 1;
|
|
2465
|
-
param2Values = (weightArr ?? Array.from({ length: count }, () => ex.weight)).map((v) => String(v));
|
|
2466
|
-
} else {
|
|
2467
|
-
param2Type = 0;
|
|
2468
|
-
param2Values = null;
|
|
2469
|
-
}
|
|
2470
|
-
const entry = {
|
|
2471
|
-
exercise_id: ex.id,
|
|
2472
|
-
workout_set_id: workoutSetId,
|
|
2473
|
-
set_id: workoutSetId,
|
|
2474
|
-
setKey: workoutSetId,
|
|
2475
|
-
title: ex.title ?? "",
|
|
2476
|
-
instruction,
|
|
2477
|
-
order,
|
|
2478
|
-
param_1_type: ex.param_1_type ?? 3,
|
|
2479
|
-
param_2_type: param2Type,
|
|
2480
|
-
workout_set_exercise_template_id: null,
|
|
2481
|
-
no_sets: 0,
|
|
2482
|
-
param_count: count,
|
|
2483
|
-
set_num: count,
|
|
2484
|
-
key,
|
|
2485
|
-
video_url: "",
|
|
2486
|
-
thumbnail_url: "",
|
|
2487
|
-
tags: [],
|
|
2488
|
-
eType: "e",
|
|
2489
|
-
use_count: 0
|
|
2490
|
-
};
|
|
2491
|
-
const p1 = slots(reps);
|
|
2492
|
-
const p2 = slots(param2Values);
|
|
2493
|
-
for (let i = 0; i < 10; i += 1) {
|
|
2494
|
-
entry[`param_1_data_${i + 1}`] = p1[i] ?? "";
|
|
2495
|
-
entry[`param_2_data_${i + 1}`] = p2[i] ?? "";
|
|
2496
|
-
}
|
|
2497
|
-
return entry;
|
|
2498
|
-
}
|
|
2499
|
-
function buildBlockPayload(blocks, workoutId) {
|
|
2500
|
-
return blocks.map((b, i) => {
|
|
2501
|
-
const lb = resolveLeaderboard(b);
|
|
2502
|
-
return {
|
|
2503
|
-
workout_id: workoutId,
|
|
2504
|
-
order: i + 1,
|
|
2505
|
-
type: b.type ?? 2,
|
|
2506
|
-
instruction: b.instruction ?? "",
|
|
2507
|
-
is_redzone: lb.isRedzone,
|
|
2508
|
-
redzone_type: lb.redzoneType,
|
|
2509
|
-
smaller_is_better: lb.smallerIsBetter,
|
|
2510
|
-
redzone_instruction: lb.redzoneInstruction,
|
|
2511
|
-
exercises: [],
|
|
2512
|
-
exerciseKeys: [],
|
|
2513
|
-
key: `k::${workoutId}${i + 1}`,
|
|
2514
|
-
title: b.title
|
|
2515
|
-
};
|
|
2516
|
-
});
|
|
2517
|
-
}
|
|
2518
|
-
function unitOr(t) {
|
|
2519
|
-
return unitLabel(t) ?? "?";
|
|
2520
|
-
}
|
|
2521
|
-
/** Flag spec params the API will silently override to the exercise's fixed units. */
|
|
2522
|
-
function unitAdvisory(blockTitle, ex, defaults) {
|
|
2523
|
-
const notes = [];
|
|
2524
|
-
const warnings = [];
|
|
2525
|
-
const u = unitOr;
|
|
2526
|
-
const label = `${blockTitle} / ${ex.title ?? ex.id}`;
|
|
2527
|
-
if (Array.isArray(ex.reps) && ex.sets !== void 0 && ex.reps.length !== ex.sets) warnings.push(`${label}: reps array has ${ex.reps.length} entr${ex.reps.length === 1 ? "y" : "ies"}; sets:${ex.sets} is ignored — building ${ex.reps.length} set(s).`);
|
|
2528
|
-
const sentP1 = ex.param_1_type;
|
|
2529
|
-
if (sentP1 !== void 0 && sentP1 !== null && Math.trunc(Number(sentP1)) !== defaults.param1) {
|
|
2530
|
-
const sp1 = Math.trunc(Number(sentP1));
|
|
2531
|
-
warnings.push(`${label}: param_1_type ${sentP1} (${u(sp1)}) is ignored — this exercise is fixed to ${u(defaults.param1)}; values render as ${u(defaults.param1)}.`);
|
|
2532
|
-
} else if (defaults.param1 !== 3 && defaults.param1 !== null) notes.push(`${label}: values are in ${u(defaults.param1)} (the exercise's fixed primary unit).`);
|
|
2533
|
-
if (ex.weight !== void 0 && ex.weight !== null) {
|
|
2534
|
-
const sentP2 = Math.trunc(Number(ex.param_2_type ?? 1));
|
|
2535
|
-
const effP2 = defaults.param2 === 0 || defaults.param2 === null ? 1 : defaults.param2;
|
|
2536
|
-
if (sentP2 !== effP2) if (sentP2 === 2 || sentP2 === 14) warnings.push(`${label}: ${u(sentP2)} does not stick on this exercise — it renders as ${u(effP2)}. Put it in the exercise 'instr' text and leave load blank.`);
|
|
2537
|
-
else warnings.push(`${label}: load renders as ${u(effP2)}, not ${u(sentP2)} (this exercise's secondary unit is fixed).`);
|
|
2538
|
-
}
|
|
2539
|
-
return {
|
|
2540
|
-
notes,
|
|
2541
|
-
warnings
|
|
2542
|
-
};
|
|
2543
|
-
}
|
|
2544
|
-
/**
|
|
2545
|
-
* Run unit advisories across a whole block list against an exercise index. Shared by the
|
|
2546
|
-
* MCP workout_build tool and the CLI so both surface the same notes/warnings. Ensures the
|
|
2547
|
-
* index is loaded first, otherwise `defaults` returns null on a cold index and every
|
|
2548
|
-
* advisory is silently dropped.
|
|
2549
|
-
*/
|
|
2550
|
-
async function collectAdvisories(blocks, index) {
|
|
2551
|
-
await index.ensureFresh();
|
|
2552
|
-
const pairs = blocks.flatMap((b) => b.exercises.map((ex) => ({
|
|
2553
|
-
block: b,
|
|
2554
|
-
ex
|
|
2555
|
-
})));
|
|
2556
|
-
const defaults = await Promise.all(pairs.map((p) => {
|
|
2557
|
-
const id = Number(p.ex.id);
|
|
2558
|
-
return Number.isFinite(id) ? index.defaults(id) : Promise.resolve(null);
|
|
2559
|
-
}));
|
|
2560
|
-
const notes = [];
|
|
2561
|
-
const warnings = [];
|
|
2562
|
-
pairs.forEach((p, i) => {
|
|
2563
|
-
const def = defaults[i];
|
|
2564
|
-
if (!def) return;
|
|
2565
|
-
const advisory = unitAdvisory(p.block.title, p.ex, def);
|
|
2566
|
-
notes.push(...advisory.notes);
|
|
2567
|
-
warnings.push(...advisory.warnings);
|
|
2568
|
-
});
|
|
2569
|
-
return {
|
|
2570
|
-
notes,
|
|
2571
|
-
warnings
|
|
2572
|
-
};
|
|
2573
|
-
}
|
|
2574
|
-
//#endregion
|
|
2575
|
-
//#region ../js/src/workout-session.ts
|
|
2576
|
-
async function req(client, method, path, body) {
|
|
2577
|
-
const res = await client.request(method, path, body === void 0 ? void 0 : { body });
|
|
2578
|
-
if (!res.ok) {
|
|
2579
|
-
const detail = typeof res.data === "string" ? res.data : JSON.stringify(res.data);
|
|
2580
|
-
throw new Error(`${method} ${path} failed (HTTP ${res.status}): ${detail}`);
|
|
2581
|
-
}
|
|
2582
|
-
return res.data;
|
|
2583
|
-
}
|
|
2584
|
-
function createPath(opts) {
|
|
2585
|
-
if (opts.timelineDay !== void 0) return `/2.0/coach/calendar/workout/createWorkoutForTimelineDay/${opts.programId}/${opts.timelineDay}/null`;
|
|
2586
|
-
if (!opts.date) throw new Error("workout build requires either date or timelineDay");
|
|
2587
|
-
const [y, m, d] = opts.date;
|
|
2588
|
-
return `/2.0/coach/calendar/workout/createWorkoutForDay/${opts.programId}/${y}/${m}/${d}/0`;
|
|
2589
|
-
}
|
|
2590
|
-
async function buildSession(client, opts) {
|
|
2591
|
-
const sess = await req(client, "POST", createPath(opts), {});
|
|
2592
|
-
checkResponse(sessionCreateResponseSchema, sess, "session create");
|
|
2593
|
-
const workoutId = Number(sess.workout_id);
|
|
2594
|
-
const pwId = Number(sess.id);
|
|
2595
|
-
const created = await req(client, "POST", "/2.0/coach/calendar/saveProgramWorkoutSets", buildBlockPayload(opts.blocks, workoutId));
|
|
2596
|
-
const byOrder = new Map(created.map((b) => [b.order, b.id]));
|
|
2597
|
-
let counter = 0;
|
|
2598
|
-
const payloads = opts.blocks.map((block, i) => {
|
|
2599
|
-
const wsid = byOrder.get(i + 1);
|
|
2600
|
-
if (wsid === void 0) throw new Error(`No saved block for order ${i + 1}.`);
|
|
2601
|
-
return block.exercises.map((ex, j) => {
|
|
2602
|
-
counter += 1;
|
|
2603
|
-
return makeExercise(ex, wsid, j + 1, `k::${workoutId}${String(counter).padStart(3, "0")}`);
|
|
2604
|
-
});
|
|
2605
|
-
});
|
|
2606
|
-
await Promise.all(payloads.map((p) => req(client, "POST", "/2.0/coach/calendar/saveWorkoutSetExercises", p)));
|
|
2607
|
-
if (opts.instruction !== void 0 && opts.instruction !== "") {
|
|
2608
|
-
const blockIds = [...byOrder.entries()].sort((a, b) => a[0] - b[0]).map(([, id]) => id);
|
|
2609
|
-
await setSessionInstruction(client, workoutId, sess, opts.instruction, blockIds);
|
|
2610
|
-
}
|
|
2611
|
-
if (opts.publish ?? false) await req(client, "POST", "/2.0/coach/calendar/programWorkout/publish", [pwId]);
|
|
2612
|
-
return {
|
|
2613
|
-
pwId,
|
|
2614
|
-
workoutId
|
|
2615
|
-
};
|
|
2616
|
-
}
|
|
2617
|
-
/**
|
|
2618
|
-
* Set a session's Coach Instructions (the day-note at the top of a session). `pw` is the
|
|
2619
|
-
* programWorkout object (the create-time response or a day's edit-GET entry). The PUT wants
|
|
2620
|
-
* the whole object back with `instruction` set and `sets`/`setKeys` as a flat list of block
|
|
2621
|
-
* ids. This does NOT change publish state: `published` is sent exactly as it is on `pw`.
|
|
2622
|
-
*/
|
|
2623
|
-
async function setSessionInstruction(client, workoutId, pw, instruction, blockIds) {
|
|
2624
|
-
const body = {
|
|
2625
|
-
...pw,
|
|
2626
|
-
instruction,
|
|
2627
|
-
sets: blockIds,
|
|
2628
|
-
setKeys: blockIds
|
|
2629
|
-
};
|
|
2630
|
-
await req(client, "PUT", `/3.0/coach/workout/${workoutId}`, body);
|
|
2631
|
-
}
|
|
2632
|
-
async function removeSession(client, programId, pwId) {
|
|
2633
|
-
await req(client, "POST", "/2.0/coach/calendar/removeProgramWorkout", {
|
|
2634
|
-
programId,
|
|
2635
|
-
pwId
|
|
2636
|
-
});
|
|
2637
|
-
}
|
|
2638
|
-
async function publishSession(client, pwId) {
|
|
2639
|
-
await req(client, "POST", "/2.0/coach/calendar/programWorkout/publish", [pwId]);
|
|
2640
|
-
}
|
|
2641
|
-
function str(value) {
|
|
2642
|
-
return value === void 0 || value === null ? "" : String(value);
|
|
2643
|
-
}
|
|
2644
|
-
async function readSession(client, programId, date, pwId) {
|
|
2645
|
-
const [y, m, d] = date;
|
|
2646
|
-
const data = await req(client, "GET", `/1.0/coach/programs/edit/${programId}/${y}/${m}/${d}`);
|
|
2647
|
-
checkResponse(programsEditResponseSchema, data, "programs edit");
|
|
2648
|
-
const pw = (data.programWorkouts ?? []).find((p) => coerceInt(p.id) === pwId);
|
|
2649
|
-
if (!pw) throw new Error(`programWorkout ${pwId} not found on ${y}-${m}-${d}.`);
|
|
2650
|
-
const setsObj = pw.sets ?? {};
|
|
2651
|
-
const blocks = Object.values(setsObj).sort((a, b) => Number(a.order) - Number(b.order)).map((b) => readBlock(b));
|
|
2652
|
-
return {
|
|
2653
|
-
pwId,
|
|
2654
|
-
date: `${str(pw.year)}-${str(pw.month)}-${str(pw.day)}`,
|
|
2655
|
-
published: pw.published,
|
|
2656
|
-
instruction: str(pw.instruction),
|
|
2657
|
-
blocks
|
|
2658
|
-
};
|
|
2659
|
-
}
|
|
2660
|
-
function readBlock(b) {
|
|
2661
|
-
const rz = coerceInt(b.redzone_type);
|
|
2662
|
-
let leaderboard = null;
|
|
2663
|
-
if (rz && rz > 0) leaderboard = `FOR ${(LEADERBOARD_LABEL[rz] ?? `type ${rz}`).toUpperCase()}${b.smaller_is_better ? " (lowest wins)" : ""}`;
|
|
2664
|
-
const exercises = (Array.isArray(b.exercises) ? b.exercises : []).sort((a, e) => Number(a.order) - Number(e.order)).map((ex) => readExercise(ex));
|
|
2665
|
-
return {
|
|
2666
|
-
order: Number(b.order),
|
|
2667
|
-
title: str(b.title),
|
|
2668
|
-
leaderboard,
|
|
2669
|
-
exercises
|
|
2670
|
-
};
|
|
2671
|
-
}
|
|
2672
|
-
function readExercise(ex) {
|
|
2673
|
-
const reps = [];
|
|
2674
|
-
const load = [];
|
|
2675
|
-
for (let i = 1; i <= 10; i += 1) {
|
|
2676
|
-
const r = str(ex[`param_1_data_${i}`]);
|
|
2677
|
-
if (r !== "") reps.push(r);
|
|
2678
|
-
const w = str(ex[`param_2_data_${i}`]);
|
|
2679
|
-
if (w !== "") load.push(w);
|
|
2680
|
-
}
|
|
2681
|
-
return {
|
|
2682
|
-
order: Number(ex.order),
|
|
2683
|
-
title: str(ex.title),
|
|
2684
|
-
reps,
|
|
2685
|
-
primaryUnit: unitLabel(coerceInt(ex.param_1_type)),
|
|
2686
|
-
load,
|
|
2687
|
-
loadUnit: unitLabel(coerceInt(ex.param_2_type)),
|
|
2688
|
-
instruction: str(ex.instruction)
|
|
2689
|
-
};
|
|
2690
|
-
}
|
|
2691
|
-
//#endregion
|
|
2692
2727
|
//#region ../js/src/messaging.ts
|
|
2693
2728
|
const BUCKETS = [
|
|
2694
2729
|
["team", "teams"],
|
|
@@ -3093,7 +3128,7 @@ function registerEntityReads(server, ctx) {
|
|
|
3093
3128
|
}));
|
|
3094
3129
|
server.registerTool("get_program", {
|
|
3095
3130
|
title: "Get program detail",
|
|
3096
|
-
description: "Full nested program structure (blocks + sessions) live from the API, by program id.",
|
|
3131
|
+
description: "Full nested program structure (blocks + sessions) live from the API, by program id. Team/group programs only — Coach Plan / personal_cal program ids typically return HTTP 401 Cannot access program.",
|
|
3097
3132
|
inputSchema: { programId: idParam },
|
|
3098
3133
|
annotations: READ
|
|
3099
3134
|
}, ({ programId }) => apiCall(ctx, "GET", `/3.0/coach/program/${enc(programId)}`, void 0, "This is a large, deep object. If it is truncated, fetch a narrower view (a specific session) instead."));
|
|
@@ -3539,7 +3574,7 @@ function registerExerciseTools(server, ctx) {
|
|
|
3539
3574
|
function registerBuild(server, ctx) {
|
|
3540
3575
|
server.registerTool("workout_build", {
|
|
3541
3576
|
title: "Build a workout session (draft)",
|
|
3542
|
-
description: "Build an UNPUBLISHED session from a spec (program -> session -> blocks -> exercises). Two exercises in one block become a superset. Add a block 'leaderboard' for a Red-Zone score, or a top-level 'instruction' for the session note (Coach Instructions). Returns the draft ids, a read-back, and unit advisories. Review, then workout_publish.",
|
|
3577
|
+
description: "Build an UNPUBLISHED session from a spec (program -> session -> blocks -> exercises). Works on team/group program calendars (and the authenticated coach's own personal calendar). Writing to another athlete's Coach Plan / personal_cal calendar is not supported (createWorkoutForDay returns HTTP 500). Two exercises in one block become a superset. A block with empty exercises and a non-empty block instruction is a text-only Circuit / Conditioning block (type 1). Add a block 'leaderboard' for a Red-Zone score, or a top-level 'instruction' for the session note (Coach Instructions). Returns the draft ids, a read-back, and unit advisories. Review, then workout_publish.",
|
|
3543
3578
|
inputSchema: {
|
|
3544
3579
|
programId: z.number(),
|
|
3545
3580
|
date: z.string().optional(),
|
|
@@ -3607,7 +3642,8 @@ function registerBuild(server, ctx) {
|
|
|
3607
3642
|
await publishSession(ctx.client, pwId);
|
|
3608
3643
|
return jsonResult({
|
|
3609
3644
|
published: pwId,
|
|
3610
|
-
readback: await readSession(ctx.client, programId, parseWorkoutDate(date), pwId)
|
|
3645
|
+
readback: await readSession(ctx.client, programId, parseWorkoutDate(date), pwId),
|
|
3646
|
+
note: "Published. Verify with workout_read (same programId/date/pwId). Do not use athlete_workouts to check a coach-published team session — that tool is the authenticated user's own athlete calendar, not a roster athlete's schedule. For a roster athlete's view use athlete_saved_workouts."
|
|
3611
3647
|
});
|
|
3612
3648
|
}));
|
|
3613
3649
|
}
|
|
@@ -3651,7 +3687,7 @@ function registerLifecycle(server, ctx) {
|
|
|
3651
3687
|
}));
|
|
3652
3688
|
server.registerTool("session_copy", {
|
|
3653
3689
|
title: "Copy a session to a date",
|
|
3654
|
-
description: "Copy/repeat a session to a target date on a program (POST .../copyProgramWorkout). toDate is YYYY-M-D. Creates a new session; review and publish it separately.",
|
|
3690
|
+
description: "Copy/repeat a session to a target date on a program (POST .../copyProgramWorkout). toDate is YYYY-M-D. Creates a new session; review and publish it separately. Team/group (and own personal) calendars only — copying onto another athlete's personal_cal / Coach Plan toProgramId fails with HTTP 500.",
|
|
3655
3691
|
inputSchema: {
|
|
3656
3692
|
toProgramId: z.number(),
|
|
3657
3693
|
pwId: z.number(),
|
|
@@ -3804,7 +3840,7 @@ var JsonFileLibraryCache = class {
|
|
|
3804
3840
|
};
|
|
3805
3841
|
//#endregion
|
|
3806
3842
|
//#region package.json
|
|
3807
|
-
var version = "2.0.
|
|
3843
|
+
var version = "2.0.2";
|
|
3808
3844
|
//#endregion
|
|
3809
3845
|
//#region src/server.ts
|
|
3810
3846
|
function main() {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trainheroic-unofficial/coach-mcp",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.2",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -21,8 +21,8 @@
|
|
|
21
21
|
"dependencies": {
|
|
22
22
|
"@modelcontextprotocol/server": "2.0.0",
|
|
23
23
|
"zod": "^4.4.3",
|
|
24
|
-
"@trainheroic-unofficial/core": "2.0.
|
|
25
|
-
"@trainheroic-unofficial/js": "2.0.
|
|
24
|
+
"@trainheroic-unofficial/core": "2.0.2",
|
|
25
|
+
"@trainheroic-unofficial/js": "2.0.2"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
28
|
"@types/node": "^26.1.2",
|