@trainheroic-unofficial/cli 2.1.1 → 3.0.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.
package/dist/cli.mjs CHANGED
@@ -800,8 +800,9 @@ function sortRosterByRecency(rows) {
800
800
  async function fetchRosterActivity(client, athleteIds, useMetric = false) {
801
801
  const rows = [];
802
802
  const concurrency = 6;
803
- for (let i = 0; i < athleteIds.length; i += concurrency) {
804
- const chunk = athleteIds.slice(i, i + concurrency);
803
+ const uniqueAthleteIds = [...new Set(athleteIds)];
804
+ for (let i = 0; i < uniqueAthleteIds.length; i += concurrency) {
805
+ const chunk = uniqueAthleteIds.slice(i, i + concurrency);
805
806
  const settled = await Promise.all(chunk.map(async (id) => {
806
807
  try {
807
808
  return {
@@ -1979,7 +1980,7 @@ function prescribedExerciseLabels(workouts) {
1979
1980
  return labels;
1980
1981
  }
1981
1982
  /** Group resolved exercises by saved set and write each set via the given log target. */
1982
- async function logResolvedExercises(client, target, date, resolved) {
1983
+ async function logResolvedExercises(client, target, workouts, resolved) {
1983
1984
  const bySet = /* @__PURE__ */ new Map();
1984
1985
  for (const r of resolved) {
1985
1986
  const list = bySet.get(r.savedWorkoutSetId) ?? [];
@@ -1991,19 +1992,10 @@ async function logResolvedExercises(client, target, date, resolved) {
1991
1992
  }
1992
1993
  const out = [];
1993
1994
  for (const [savedWorkoutSetId, results] of bySet) {
1994
- const written = target.role === "coach" ? await logForAthlete(client, {
1995
- athleteId: target.athleteId,
1996
- date,
1997
- savedWorkoutSetId,
1998
- results
1999
- }) : await logAthleteSet(client, {
2000
- date,
2001
- savedWorkoutSetId,
2002
- results
2003
- });
1995
+ const written = await writeSetResults(client, target, workouts, savedWorkoutSetId, results, "log");
2004
1996
  out.push({
2005
1997
  savedWorkoutSetId: written.savedWorkoutSetId,
2006
- exercisesLogged: written.exercisesLogged
1998
+ exercisesLogged: written.exercisesWritten
2007
1999
  });
2008
2000
  }
2009
2001
  return out;
@@ -2041,7 +2033,7 @@ async function logAdHocSession(client, args) {
2041
2033
  sets: e.sets
2042
2034
  };
2043
2035
  });
2044
- const sets = await logResolvedExercises(client, { role: "athlete" }, args.date, resolved);
2036
+ const sets = await logResolvedExercises(client, { role: "athlete" }, await fetchAthleteWorkouts(client, args.date, args.date), resolved);
2045
2037
  const result = {
2046
2038
  date: args.date,
2047
2039
  created,
@@ -2079,7 +2071,7 @@ async function logSessionForAthlete(client, args) {
2079
2071
  const sets = await logResolvedExercises(client, {
2080
2072
  role: "coach",
2081
2073
  athleteId: args.athleteId
2082
- }, args.date, resolved);
2074
+ }, day, resolved);
2083
2075
  return {
2084
2076
  date: args.date,
2085
2077
  created: false,
@@ -2412,7 +2404,7 @@ function unitAdvisory(blockTitle, ex, defaults) {
2412
2404
  /**
2413
2405
  * Run unit advisories across a whole block list against an exercise index. Shared by the
2414
2406
  * MCP workout_build tool and the CLI so both surface the same notes/warnings. Ensures the
2415
- * index is loaded first, otherwise `defaults` returns null on a cold index and every
2407
+ * index is loaded first, otherwise the defaults map is empty on a cold index and every
2416
2408
  * advisory is silently dropped.
2417
2409
  */
2418
2410
  async function collectAdvisories(blocks, index) {
@@ -2421,14 +2413,12 @@ async function collectAdvisories(blocks, index) {
2421
2413
  block: b,
2422
2414
  ex
2423
2415
  })));
2424
- const defaults = await Promise.all(pairs.map((p) => {
2425
- const id = Number(p.ex.id);
2426
- return Number.isFinite(id) ? index.defaults(id) : Promise.resolve(null);
2427
- }));
2416
+ const ids = [...new Set(pairs.map((p) => Number(p.ex.id)).filter((id) => Number.isFinite(id)))];
2417
+ const defaultsById = await index.defaultsMany(ids);
2428
2418
  const notes = [];
2429
2419
  const warnings = [];
2430
- pairs.forEach((p, i) => {
2431
- const def = defaults[i];
2420
+ pairs.forEach((p) => {
2421
+ const def = defaultsById.get(Number(p.ex.id));
2432
2422
  if (!def) return;
2433
2423
  const advisory = unitAdvisory(p.block.title, p.ex, def);
2434
2424
  notes.push(...advisory.notes);
@@ -2441,6 +2431,7 @@ async function collectAdvisories(blocks, index) {
2441
2431
  }
2442
2432
  //#endregion
2443
2433
  //#region ../js/src/workout-session.ts
2434
+ const EXERCISE_SAVE_CONCURRENCY = 4;
2444
2435
  /**
2445
2436
  * Coach calendar writes (`createWorkoutForDay`, timeline create, `copyProgramWorkout`) 500/401
2446
2437
  * on personal calendars (Coach Plan / `personal_cal`). Shared by `buildSession` and
@@ -2474,15 +2465,14 @@ async function buildSession(client, opts) {
2474
2465
  const created = await req(client, "POST", "/2.0/coach/calendar/saveProgramWorkoutSets", buildBlockPayload(blocks, workoutId));
2475
2466
  const byOrder = new Map(created.map((b) => [b.order, b.id]));
2476
2467
  let counter = 0;
2477
- const payloads = blocks.map((block, i) => {
2468
+ await mapPool(blocks.map((block, i) => {
2478
2469
  const wsid = byOrder.get(i + 1);
2479
2470
  if (wsid === void 0) throw new Error(`No saved block for order ${i + 1}.`);
2480
2471
  return block.exercises.map((ex, j) => {
2481
2472
  counter += 1;
2482
2473
  return makeExercise(ex, wsid, j + 1, `k::${workoutId}${String(counter).padStart(3, "0")}`);
2483
2474
  });
2484
- });
2485
- await Promise.all(payloads.filter((p) => p.length > 0).map((p) => req(client, "POST", "/2.0/coach/calendar/saveWorkoutSetExercises", p)));
2475
+ }).filter((p) => p.length > 0), EXERCISE_SAVE_CONCURRENCY, (payload) => req(client, "POST", "/2.0/coach/calendar/saveWorkoutSetExercises", payload));
2486
2476
  if (opts.instruction !== void 0 && opts.instruction !== "") {
2487
2477
  const blockIds = [...byOrder.entries()].sort((a, b) => a[0] - b[0]).map(([, id]) => id);
2488
2478
  await setSessionInstruction(client, workoutId, sess, opts.instruction, blockIds);
@@ -2685,7 +2675,8 @@ async function copySession(client, args) {
2685
2675
  year,
2686
2676
  dayOfWeek,
2687
2677
  isToday: false
2688
- }
2678
+ },
2679
+ toTimelineDate: null
2689
2680
  } });
2690
2681
  if (!res.ok) {
2691
2682
  const detail = typeof res.data === "string" ? res.data : JSON.stringify(res.data);
@@ -3187,9 +3178,11 @@ async function deleteComment(client, streamId, commentId) {
3187
3178
  if (!res.ok) throw new Error(`Message delete failed (HTTP ${res.status}).`);
3188
3179
  return res.data;
3189
3180
  }
3190
- async function readLive(client, streamId, limit) {
3191
- const res = await client.request("GET", `/v5/messaging/streams/${streamId}/comments?lastCommentId=`);
3181
+ async function readLive(client, streamId, limit, afterCommentId) {
3182
+ const cursor = afterCommentId === void 0 ? "" : String(afterCommentId);
3183
+ const res = await client.request("GET", `/v5/messaging/streams/${streamId}/comments?lastCommentId=${cursor}`);
3192
3184
  if (!res.ok || !Array.isArray(res.data)) throw new Error(`Message read failed (HTTP ${res.status}).`);
3185
+ if (afterCommentId !== void 0) return limit !== void 0 && limit > 0 ? res.data.slice(0, limit) : res.data;
3193
3186
  return limit !== void 0 && limit > 0 ? res.data.slice(-limit) : res.data;
3194
3187
  }
3195
3188
  //#endregion
@@ -3293,12 +3286,16 @@ var ExerciseLibrary = class {
3293
3286
  if (!s) return null;
3294
3287
  return presentExercise(s.raw);
3295
3288
  }
3296
- async defaults(id) {
3297
- const s = this.#byId.get(id);
3298
- return s ? {
3299
- param1: s.param_1_type,
3300
- param2: s.param_2_type
3301
- } : null;
3289
+ async defaultsMany(ids) {
3290
+ const result = /* @__PURE__ */ new Map();
3291
+ for (const id of ids) {
3292
+ const s = this.#byId.get(id);
3293
+ if (s) result.set(id, {
3294
+ param1: s.param_1_type,
3295
+ param2: s.param_2_type
3296
+ });
3297
+ }
3298
+ return result;
3302
3299
  }
3303
3300
  async search(query, limit = 20) {
3304
3301
  await this.ensureFresh();
@@ -3316,21 +3313,12 @@ var ExerciseLibrary = class {
3316
3313
  }
3317
3314
  async resolve(name) {
3318
3315
  await this.ensureFresh();
3319
- let hit = this.#exact(name);
3316
+ const hit = this.#exact(name);
3320
3317
  if (hit) return {
3321
3318
  match: hit,
3322
3319
  candidates: [hit]
3323
3320
  };
3324
- let candidates = this.#searchOnly(name, 20);
3325
- if (candidates.length === 0) {
3326
- await this.refresh();
3327
- hit = this.#exact(name);
3328
- if (hit) return {
3329
- match: hit,
3330
- candidates: [hit]
3331
- };
3332
- candidates = this.#searchOnly(name, 20);
3333
- }
3321
+ const candidates = this.#searchOnly(name, 20);
3334
3322
  if (candidates.length === 1) return {
3335
3323
  match: candidates[0] ?? null,
3336
3324
  candidates
@@ -3518,7 +3506,7 @@ Coach — manage a roster (needs a coach account):
3518
3506
 
3519
3507
  messaging:
3520
3508
  coach message list
3521
- coach message read <streamId> [--limit N]
3509
+ coach message read <streamId> [--limit N] [--after <commentId>]
3522
3510
  coach message draft <streamId> <text> [--reply-to <id>]
3523
3511
  coach message send <streamId> <text> [--reply-to <id>] --yes
3524
3512
  coach message delete <streamId> <commentId> --yes
@@ -3753,8 +3741,11 @@ async function cmdMessage(client, rest) {
3753
3741
  userId: stream.userId
3754
3742
  })));
3755
3743
  case "read": {
3756
- const { values, positionals } = parse(a, { limit: { type: "string" } });
3757
- return out(await readLive(client, toInt(need(positionals[0], "coach message read <streamId> [--limit N]"), "streamId"), values.limit !== void 0 ? toInt(values.limit, "--limit") : 20));
3744
+ const { values, positionals } = parse(a, {
3745
+ limit: { type: "string" },
3746
+ after: { type: "string" }
3747
+ });
3748
+ return out(await readLive(client, toInt(need(positionals[0], "coach message read <streamId> [--limit N] [--after <commentId>]"), "streamId"), values.limit !== void 0 ? toInt(values.limit, "--limit") : 20, values.after !== void 0 ? toInt(values.after, "--after") : void 0));
3758
3749
  }
3759
3750
  case "draft": {
3760
3751
  const { values, positionals } = parse(a, { "reply-to": { type: "string" } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trainheroic-unofficial/cli",
3
- "version": "2.1.1",
3
+ "version": "3.0.1",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -21,8 +21,8 @@
21
21
  ],
22
22
  "dependencies": {
23
23
  "zod": "^4.4.3",
24
- "@trainheroic-unofficial/dto": "2.1.1",
25
- "@trainheroic-unofficial/js": "2.1.1"
24
+ "@trainheroic-unofficial/dto": "3.0.1",
25
+ "@trainheroic-unofficial/js": "3.0.1"
26
26
  },
27
27
  "devDependencies": {
28
28
  "@types/node": "^26.2.0",
@@ -1301,10 +1301,14 @@ Copies or repeats a session to a target date. Used for both "Copy" and "Repeat"
1301
1301
  "year": 2026,
1302
1302
  "dayOfWeek": 0,
1303
1303
  "isToday": false
1304
- }
1304
+ },
1305
+ "toTimelineDate": null
1305
1306
  }
1306
1307
  ```
1307
1308
 
1309
+ `toTimelineDate` must be present as an explicit `null` for a calendar-date copy. Omitting it
1310
+ causes this legacy endpoint to return HTTP 500.
1311
+
1308
1312
  #### `POST /2.0/coach/calendar/programWorkout/saveWorkoutAsTemplate/{workoutId}`
1309
1313
 
1310
1314
  Saves an existing session as a reusable template in the session library. No request body needed — uses the workout ID in the URL path.