@trainheroic-unofficial/coach-mcp 2.1.0 → 3.0.0

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 +61 -21
  2. package/package.json +5 -5
package/dist/server.mjs CHANGED
@@ -583,6 +583,34 @@ function confirmGate(ctx, message, confirmArg) {
583
583
  */
584
584
  const SERVER_INSTRUCTIONS = "Speak to the user in plain, everyday language about their training. Describe what you are doing in the TrainHeroic app's own terms (for example, say you are creating a workout rather than naming a tool). Do not surface internal tool names (the snake_case identifiers such as athlete_session_create), raw parameter names, or numeric ids in your replies unless the user explicitly asks for them; they are implementation details. The tool descriptions cross-reference each other by name only so you can chain them correctly. Keep that wiring to yourself.";
585
585
  //#endregion
586
+ //#region ../js/src/http-error.ts
587
+ /**
588
+ * A final non-2xx response from TrainHeroic. The error deliberately carries only request
589
+ * metadata that is safe to send to telemetry: never the path, query string, request body,
590
+ * response body, credentials, or session token.
591
+ */
592
+ var TrainHeroicHttpError = class extends Error {
593
+ name = "TrainHeroicHttpError";
594
+ method;
595
+ status;
596
+ host;
597
+ constructor(method, url, status) {
598
+ const parsed = new URL(url);
599
+ const normalizedMethod = method.toUpperCase();
600
+ super(`TrainHeroic ${normalizedMethod} request failed with HTTP ${status}`);
601
+ this.method = normalizedMethod;
602
+ this.status = status;
603
+ this.host = parsed.host;
604
+ }
605
+ };
606
+ /** Call observability hooks without allowing them to change SDK behavior. */
607
+ function notifyHttpError(handler, method, url, status) {
608
+ if (!handler) return;
609
+ try {
610
+ Promise.resolve(handler(new TrainHeroicHttpError(method, url, status))).catch(() => {});
611
+ } catch {}
612
+ }
613
+ //#endregion
586
614
  //#region ../js/src/auth.ts
587
615
  const DEFAULT_AUTH_URL = "https://apis.trainheroic.com/auth";
588
616
  /**
@@ -604,8 +632,9 @@ function authUrl() {
604
632
  * the Phase 0 spike: no refresh_token, no api_token, no TTL). The 48-char session_id
605
633
  * is sent as the `session-token` header and works against both API hosts.
606
634
  */
607
- async function loginTrainHeroic(email, password) {
608
- const res = await fetch(authUrl(), {
635
+ async function loginTrainHeroic(email, password, options = {}) {
636
+ const url = authUrl();
637
+ const res = await fetch(url, {
609
638
  method: "POST",
610
639
  headers: {
611
640
  "content-type": "application/x-www-form-urlencoded",
@@ -616,7 +645,10 @@ async function loginTrainHeroic(email, password) {
616
645
  password
617
646
  }).toString()
618
647
  });
619
- if (!res.ok) return null;
648
+ if (!res.ok) {
649
+ notifyHttpError(options.onHttpError, "POST", url, res.status);
650
+ return null;
651
+ }
620
652
  const data = await res.json().catch(() => null);
621
653
  if (!data || typeof data.id !== "number" || !data.session_id) return null;
622
654
  return {
@@ -659,6 +691,7 @@ var TrainHeroicClient = class {
659
691
  #email;
660
692
  #password;
661
693
  #onSession;
694
+ #onHttpError;
662
695
  #sessionId;
663
696
  #loginInFlight = null;
664
697
  constructor(email, password, sessionId = null, options = {}) {
@@ -666,6 +699,7 @@ var TrainHeroicClient = class {
666
699
  this.#password = password;
667
700
  this.#sessionId = sessionId;
668
701
  this.#onSession = options.onSession;
702
+ this.#onHttpError = options.onHttpError;
669
703
  }
670
704
  get sessionId() {
671
705
  return this.#sessionId;
@@ -680,7 +714,7 @@ var TrainHeroicClient = class {
680
714
  }
681
715
  }
682
716
  async #login() {
683
- const session = await loginTrainHeroic(this.#email, this.#password);
717
+ const session = await loginTrainHeroic(this.#email, this.#password, this.#onHttpError ? { onHttpError: this.#onHttpError } : {});
684
718
  if (!session) throw new TrainHeroicAuthError("TrainHeroic login failed");
685
719
  this.#sessionId = session.sessionId;
686
720
  try {
@@ -697,6 +731,7 @@ var TrainHeroicClient = class {
697
731
  session = await this.#ensureSession();
698
732
  res = await this.#send(method, url, session, options.body);
699
733
  }
734
+ if (!res.ok) notifyHttpError(this.#onHttpError, method, url, res.status);
700
735
  const text = await res.text();
701
736
  let data = text;
702
737
  if (text.length > 0) try {
@@ -2081,8 +2116,10 @@ function unitAdvisory(blockTitle, ex, defaults) {
2081
2116
  if (ex.weight !== void 0 && ex.weight !== null) {
2082
2117
  const sentP2 = Math.trunc(Number(ex.param_2_type ?? 1));
2083
2118
  const effP2 = defaults.param2 === 0 || defaults.param2 === null ? 1 : defaults.param2;
2084
- 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.`);
2085
- else warnings.push(`${label}: load renders as ${u(effP2)}, not ${u(sentP2)} (this exercise's secondary unit is fixed).`);
2119
+ if (sentP2 !== effP2) {
2120
+ 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.`);
2121
+ else warnings.push(`${label}: load renders as ${u(effP2)}, not ${u(sentP2)} (this exercise's secondary unit is fixed).`);
2122
+ }
2086
2123
  }
2087
2124
  return {
2088
2125
  notes,
@@ -2092,7 +2129,7 @@ function unitAdvisory(blockTitle, ex, defaults) {
2092
2129
  /**
2093
2130
  * Run unit advisories across a whole block list against an exercise index. Shared by the
2094
2131
  * MCP workout_build tool and the CLI so both surface the same notes/warnings. Ensures the
2095
- * index is loaded first, otherwise `defaults` returns null on a cold index and every
2132
+ * index is loaded first, otherwise the defaults map is empty on a cold index and every
2096
2133
  * advisory is silently dropped.
2097
2134
  */
2098
2135
  async function collectAdvisories(blocks, index) {
@@ -2101,14 +2138,12 @@ async function collectAdvisories(blocks, index) {
2101
2138
  block: b,
2102
2139
  ex
2103
2140
  })));
2104
- const defaults = await Promise.all(pairs.map((p) => {
2105
- const id = Number(p.ex.id);
2106
- return Number.isFinite(id) ? index.defaults(id) : Promise.resolve(null);
2107
- }));
2141
+ const ids = [...new Set(pairs.map((p) => Number(p.ex.id)).filter((id) => Number.isFinite(id)))];
2142
+ const defaultsById = await index.defaultsMany(ids);
2108
2143
  const notes = [];
2109
2144
  const warnings = [];
2110
- pairs.forEach((p, i) => {
2111
- const def = defaults[i];
2145
+ pairs.forEach((p) => {
2146
+ const def = defaultsById.get(Number(p.ex.id));
2112
2147
  if (!def) return;
2113
2148
  const advisory = unitAdvisory(p.block.title, p.ex, def);
2114
2149
  notes.push(...advisory.notes);
@@ -2365,7 +2400,8 @@ async function copySession(client, args) {
2365
2400
  year,
2366
2401
  dayOfWeek,
2367
2402
  isToday: false
2368
- }
2403
+ },
2404
+ toTimelineDate: null
2369
2405
  } });
2370
2406
  if (!res.ok) {
2371
2407
  const detail = typeof res.data === "string" ? res.data : JSON.stringify(res.data);
@@ -2954,12 +2990,16 @@ var ExerciseLibrary = class {
2954
2990
  if (!s) return null;
2955
2991
  return presentExercise(s.raw);
2956
2992
  }
2957
- async defaults(id) {
2958
- const s = this.#byId.get(id);
2959
- return s ? {
2960
- param1: s.param_1_type,
2961
- param2: s.param_2_type
2962
- } : null;
2993
+ async defaultsMany(ids) {
2994
+ const result = /* @__PURE__ */ new Map();
2995
+ for (const id of ids) {
2996
+ const s = this.#byId.get(id);
2997
+ if (s) result.set(id, {
2998
+ param1: s.param_1_type,
2999
+ param2: s.param_2_type
3000
+ });
3001
+ }
3002
+ return result;
2963
3003
  }
2964
3004
  async search(query, limit = 20) {
2965
3005
  await this.ensureFresh();
@@ -3933,7 +3973,7 @@ var JsonFileLibraryCache = class {
3933
3973
  };
3934
3974
  //#endregion
3935
3975
  //#region package.json
3936
- var version = "2.1.0";
3976
+ var version = "3.0.0";
3937
3977
  //#endregion
3938
3978
  //#region src/server.ts
3939
3979
  function main() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trainheroic-unofficial/coach-mcp",
3
- "version": "2.1.0",
3
+ "version": "3.0.0",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -21,13 +21,13 @@
21
21
  "dependencies": {
22
22
  "@modelcontextprotocol/server": "2.0.0",
23
23
  "zod": "^4.4.3",
24
- "@trainheroic-unofficial/core": "2.1.0",
25
- "@trainheroic-unofficial/js": "2.1.0"
24
+ "@trainheroic-unofficial/js": "3.0.0",
25
+ "@trainheroic-unofficial/core": "3.0.0"
26
26
  },
27
27
  "devDependencies": {
28
- "@types/node": "^26.1.2",
28
+ "@types/node": "^26.2.0",
29
29
  "tsdown": "^0.22.14",
30
- "tsx": "^4.23.4",
30
+ "tsx": "^4.23.11",
31
31
  "typescript": "^7.0.2",
32
32
  "vitest": "^4.1.10"
33
33
  },