@trainheroic-unofficial/coach-mcp 3.5.0 → 3.5.1

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.
Files changed (2) hide show
  1. package/dist/server.mjs +92 -11
  2. package/package.json +3 -3
package/dist/server.mjs CHANGED
@@ -701,6 +701,12 @@ const exerciseSpecSchema = z.object({
701
701
  instr: z.string().optional(),
702
702
  param_1_type: z.number().optional(),
703
703
  param_2_type: z.number().optional()
704
+ }).superRefine((exercise, ctx) => {
705
+ if (Array.isArray(exercise.reps) && Array.isArray(exercise.weight) && exercise.reps.length !== exercise.weight.length) ctx.addIssue({
706
+ code: "custom",
707
+ message: `Per-set reps and weight arrays must have the same length; received ${exercise.reps.length} reps and ${exercise.weight.length} weights.`,
708
+ path: ["weight"]
709
+ });
704
710
  });
705
711
  /** A block's Red-Zone leaderboard: a unit string/number, or an object with options. */
706
712
  const leaderboardSpecSchema = z.union([
@@ -2460,9 +2466,75 @@ function repsList(ex) {
2460
2466
  const reps = ex.reps;
2461
2467
  if (Array.isArray(reps)) return reps.map((r) => String(r));
2462
2468
  if (reps === void 0 || reps === null) return [];
2463
- const sets = Math.max(1, Math.trunc(Number(ex.sets ?? 1)) || 1);
2469
+ const sets = Array.isArray(ex.weight) ? ex.weight.length : Math.max(1, Math.trunc(Number(ex.sets ?? 1)) || 1);
2464
2470
  return Array.from({ length: sets }, () => String(reps));
2465
2471
  }
2472
+ /** Number of sets that the encoder will write for an exercise. */
2473
+ function exerciseSetCount(ex) {
2474
+ if (Array.isArray(ex.reps) && ex.reps.length > 0) return ex.reps.length;
2475
+ if (Array.isArray(ex.weight)) return ex.weight.length;
2476
+ if (!Array.isArray(ex.reps) && ex.reps !== void 0 && ex.reps !== null || ex.weight !== void 0 && ex.weight !== null) return Math.max(1, Math.trunc(Number(ex.sets ?? 1)) || 1);
2477
+ return 0;
2478
+ }
2479
+ function findSetSplits(blocks) {
2480
+ return blocks.flatMap((block) => block.exercises.flatMap((exercise) => {
2481
+ const setCount = exerciseSetCount(exercise);
2482
+ if (setCount <= 10) return [];
2483
+ return [{
2484
+ blockTitle: block.title,
2485
+ exerciseTitle: exercise.title ?? String(exercise.id),
2486
+ setCount,
2487
+ blockCount: Math.ceil(setCount / 10)
2488
+ }];
2489
+ }));
2490
+ }
2491
+ function setSplitSummary(blocks) {
2492
+ const splits = findSetSplits(blocks);
2493
+ if (splits.length === 0) return null;
2494
+ return `TrainHeroic supports at most 10 sets per exercise block. The following will be split into consecutive same-titled blocks: ${splits.map(({ blockTitle, exerciseTitle, setCount, blockCount }) => `${blockTitle} / ${exerciseTitle}: ${setCount} sets into ${blockCount} blocks`).join("; ")}.`;
2495
+ }
2496
+ function sliceExercise(exercise, setCount, start) {
2497
+ if (start > 0 && setCount <= start) return null;
2498
+ const chunkCount = setCount === 0 ? 0 : Math.min(10, Math.max(0, setCount - start));
2499
+ const chunk = { ...exercise };
2500
+ const hasPerSetReps = Array.isArray(exercise.reps) && exercise.reps.length > 0;
2501
+ const hasScalarReps = !Array.isArray(exercise.reps) && exercise.reps !== void 0 && exercise.reps !== null;
2502
+ if (Array.isArray(exercise.reps) && exercise.reps.length > 0) {
2503
+ chunk.reps = exercise.reps.slice(start, start + 10);
2504
+ delete chunk.sets;
2505
+ } else if (hasScalarReps) chunk.sets = chunkCount;
2506
+ if (Array.isArray(exercise.weight)) {
2507
+ chunk.weight = exercise.weight.slice(start, start + 10);
2508
+ if (!hasPerSetReps && !hasScalarReps) delete chunk.sets;
2509
+ } else if (!hasPerSetReps && exercise.weight !== void 0 && exercise.weight !== null) chunk.sets = chunkCount;
2510
+ return chunk;
2511
+ }
2512
+ /**
2513
+ * Split blocks so no encoded exercise exceeds TrainHeroic's ten parameter slots.
2514
+ * Exercises in a superset stay aligned by set range; shorter exercises appear only in
2515
+ * chunks where they still have sets.
2516
+ */
2517
+ function splitOversizedBlocks(blocks) {
2518
+ return blocks.flatMap((block) => {
2519
+ const setCounts = block.exercises.map(exerciseSetCount);
2520
+ const largestSetCount = Math.max(0, ...setCounts);
2521
+ const chunkCount = Math.max(1, Math.ceil(largestSetCount / 10));
2522
+ if (chunkCount === 1) return [block];
2523
+ return Array.from({ length: chunkCount }, (_, index) => {
2524
+ const start = index * 10;
2525
+ const exercises = block.exercises.map((exercise, exerciseIndex) => sliceExercise(exercise, setCounts[exerciseIndex] ?? 0, start)).filter((exercise) => exercise !== null);
2526
+ if (index === 0) return {
2527
+ ...block,
2528
+ exercises
2529
+ };
2530
+ return {
2531
+ title: block.title,
2532
+ ...block.type === void 0 ? {} : { type: block.type },
2533
+ exercises
2534
+ };
2535
+ });
2536
+ });
2537
+ }
2466
2538
  /** Build one saveWorkoutSetExercises payload entry with all ten param slots filled. */
2467
2539
  function makeExercise(ex, workoutSetId, order, key) {
2468
2540
  const reps = repsList(ex);
@@ -2470,8 +2542,7 @@ function makeExercise(ex, workoutSetId, order, key) {
2470
2542
  if (instruction === "" && ex.rpe !== void 0 && ex.rpe !== null) instruction = `RPE ${ex.rpe}`;
2471
2543
  const hasWeight = ex.weight !== void 0 && ex.weight !== null;
2472
2544
  const weightArr = Array.isArray(ex.weight) ? ex.weight : null;
2473
- let count = reps.length;
2474
- if (count === 0 && hasWeight) count = weightArr ? weightArr.length : Math.max(1, Math.trunc(Number(ex.sets ?? 1)) || 1);
2545
+ const count = exerciseSetCount(ex);
2475
2546
  let param2Type;
2476
2547
  let param2Values;
2477
2548
  if (hasWeight) {
@@ -2502,8 +2573,8 @@ function makeExercise(ex, workoutSetId, order, key) {
2502
2573
  eType: "e",
2503
2574
  use_count: 0
2504
2575
  };
2505
- const p1 = slots(reps);
2506
- const p2 = slots(param2Values);
2576
+ const p1 = slots(reps, 10);
2577
+ const p2 = slots(param2Values, 10);
2507
2578
  for (let i = 0; i < 10; i += 1) {
2508
2579
  entry[`param_1_data_${i + 1}`] = p1[i] ?? "";
2509
2580
  entry[`param_2_data_${i + 1}`] = p2[i] ?? "";
@@ -2621,7 +2692,10 @@ function createPath(opts) {
2621
2692
  return `/2.0/coach/calendar/workout/createWorkoutForDay/${opts.programId}/${y}/${m}/${d}/0`;
2622
2693
  }
2623
2694
  async function buildSession(client, opts) {
2624
- const blocks = z.array(blockSpecSchema).parse(opts.blocks);
2695
+ const validatedBlocks = z.array(blockSpecSchema).parse(opts.blocks);
2696
+ const splitSummary = setSplitSummary(validatedBlocks);
2697
+ if (splitSummary !== null && opts.confirmSetSplit !== true) throw new Error(`${splitSummary} Set confirmSetSplit:true to allow this change.`);
2698
+ const blocks = splitSummary === null ? validatedBlocks : splitOversizedBlocks(validatedBlocks);
2625
2699
  const sess = await req(client, "POST", createPath(opts), {});
2626
2700
  checkResponse(sessionCreateResponseSchema, sess, "session create");
2627
2701
  const workoutId = Number(sess.workout_id);
@@ -4456,30 +4530,37 @@ function registerExerciseTools(server, ctx) {
4456
4530
  function registerBuild(server, ctx) {
4457
4531
  server.registerTool("workout_build", {
4458
4532
  title: "Build a workout session (draft)",
4459
- description: "Build an UNPUBLISHED session from a spec (program -> session -> blocks -> exercises). Pass programId for a team/group calendar (list_teams → group_program), or athleteId to write on that roster athlete's coach calendar (resolved via /v5/calendars/athletes/{id} — the same calendar the coach web app opens from My Athletes). athleteId requires date. 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.",
4533
+ description: "Build an UNPUBLISHED session from a spec (program -> session -> blocks -> exercises). Pass programId for a team/group calendar (list_teams → group_program), or athleteId to write on that roster athlete's coach calendar (resolved via /v5/calendars/athletes/{id} — the same calendar the coach web app opens from My Athletes). athleteId requires date. 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. Exercises above 10 sets are split into consecutive same-titled blocks and require confirmation (elicitation, or confirm:true). Review, then workout_publish.",
4460
4534
  inputSchema: {
4461
4535
  programId: z.number().optional(),
4462
4536
  athleteId: idParam.optional(),
4463
4537
  date: z.string().optional(),
4464
4538
  timelineDay: z.number().optional(),
4465
4539
  blocks: z.array(blockSpecSchema),
4466
- instruction: z.string().optional()
4540
+ instruction: z.string().optional(),
4541
+ confirm: z.boolean().optional()
4467
4542
  },
4468
4543
  outputSchema: toolOutputSchema(workoutBuildOutputSchema),
4469
4544
  annotations: ADDITIVE
4470
- }, ({ programId, athleteId, date, timelineDay, blocks, instruction }) => attempt(async () => {
4545
+ }, ({ programId, athleteId, date, timelineDay, blocks, instruction, confirm }, extra) => attempt(async () => {
4471
4546
  if (date === void 0 && timelineDay === void 0) return errorResult("Provide either date (YYYY-M-D) or timelineDay.");
4547
+ const typed = blocks;
4548
+ const splitSummary = setSplitSummary(typed);
4549
+ if (splitSummary !== null) {
4550
+ const blocked = confirmGate(extra, `${splitSummary} Continue?`, confirm);
4551
+ if (blocked) return blocked;
4552
+ }
4472
4553
  const resolveArgs = {};
4473
4554
  if (programId !== void 0) resolveArgs.programId = programId;
4474
4555
  if (athleteId !== void 0) resolveArgs.athleteId = toId(athleteId);
4475
4556
  if (date !== void 0) resolveArgs.date = date;
4476
4557
  const resolved = await resolveBuildProgramId(ctx.client, resolveArgs);
4477
- const typed = blocks;
4478
4558
  const opts = {
4479
4559
  programId: resolved,
4480
4560
  blocks: typed,
4481
4561
  publish: false
4482
4562
  };
4563
+ if (splitSummary !== null) opts.confirmSetSplit = true;
4483
4564
  if (date !== void 0) opts.date = parseWorkoutDate(date);
4484
4565
  if (timelineDay !== void 0) opts.timelineDay = timelineDay;
4485
4566
  if (instruction !== void 0) opts.instruction = instruction;
@@ -4751,7 +4832,7 @@ var JsonFileLibraryCache = class {
4751
4832
  };
4752
4833
  //#endregion
4753
4834
  //#region package.json
4754
- var version = "3.5.0";
4835
+ var version = "3.5.1";
4755
4836
  //#endregion
4756
4837
  //#region src/server.ts
4757
4838
  function main() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trainheroic-unofficial/coach-mcp",
3
- "version": "3.5.0",
3
+ "version": "3.5.1",
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": "3.5.0",
25
- "@trainheroic-unofficial/js": "3.5.0"
24
+ "@trainheroic-unofficial/core": "3.5.1",
25
+ "@trainheroic-unofficial/js": "3.5.1"
26
26
  },
27
27
  "devDependencies": {
28
28
  "@types/node": "^26.3.0",