Coach: der Kern, den beide Laufzeiten laden koennen
payload, validate, parse, die Prompt-Montage, der Plan-Fingerprint und die invoke→parse→validate→repair-Schleife ziehen nach api/coach/core/ — ohne node:*, damit das Handy sie spaeter genauso importiert wie der Server. Die Bibliothek und die Prompts werden als ES-Module generiert (scripts/build-coach-assets.mjs), weil weder createRequire noch ?raw unter beiden Laufzeiten laden. Das Pseudonym wird uebergeben statt abgeleitet (handle.js behaelt den HMAC). check-core-loadable prueft unter blankem node, was vitest strukturell nicht pruefen kann.
This commit is contained in:
1 parent
4a9e1e3456
commit
697f1b1269
27 files changed
+402
-204
No files matched your search
@@ -0,0 +1,69 @@
|
||||
#!/usr/bin/env node
|
||||
/* Generates the two committed artefacts under api/coach/core/ that both runtimes import:
|
||||
*
|
||||
* api/coach/core/library-data.js the exercise catalogue, from frontend/src/lib/exercises-data.js
|
||||
* api/coach/core/prompts.js the prompt texts, from api/coach/prompts/*.md
|
||||
*
|
||||
* node scripts/build-coach-assets.mjs # write
|
||||
* node scripts/build-coach-assets.mjs --check # fail if stale (CI)
|
||||
*
|
||||
* The API needs the catalogue for two things the client cannot do for it: validating that
|
||||
* every exercise id a proposal references actually exists (FR-16), and building payloads for
|
||||
* scheduled reviews, which run with nobody's browser open. Rather than teach the api build to
|
||||
* reach into frontend/, the index is generated and committed — the same arrangement the
|
||||
* translated instruction packs in src/instr/ already use.
|
||||
*
|
||||
* Only the fields the Coach reasons over are kept: an id, a name, and the three taxonomy
|
||||
* fields it filters on. Instructions, images and secondary muscles stay out — they would
|
||||
* quadruple a payload the model has no use for.
|
||||
*
|
||||
* The prompts are generated for a different reason: the server runs under bare node and the
|
||||
* phone under Vite, and neither `fs.readFileSync` nor a `?raw` import works in both. A plain
|
||||
* ES module does. The .md files stay the thing you edit.
|
||||
*/
|
||||
import { readdirSync, readFileSync, writeFileSync } from 'node:fs';
|
||||
import { dirname, join } from 'node:path';
|
||||
import { fileURLToPath, pathToFileURL } from 'node:url';
|
||||
|
||||
const root = join(dirname(fileURLToPath(import.meta.url)), '..');
|
||||
const core = join(root, 'api', 'coach', 'core');
|
||||
const check = process.argv.includes('--check');
|
||||
|
||||
const outputs = [];
|
||||
|
||||
// --- library ---
|
||||
{
|
||||
const src = join(root, 'frontend', 'src', 'lib', 'exercises-data.js');
|
||||
const { EXDB } = await import(pathToFileURL(src).href);
|
||||
const index = EXDB.map(e => ({ id: e.id, n: e.n, bp: e.bp, tg: e.tg, eq: e.eq }));
|
||||
const body = '/* GENERATED by scripts/build-coach-assets.mjs from frontend/src/lib/exercises-data.js — do not edit. */\n' +
|
||||
`export const GENERATED_FROM = 'frontend/src/lib/exercises-data.js';\n` +
|
||||
`export const COUNT = ${index.length};\n` +
|
||||
'export const EXERCISES = ' + JSON.stringify(index) + ';\n';
|
||||
outputs.push({ file: join(core, 'library-data.js'), body, note: `${index.length} exercises` });
|
||||
}
|
||||
|
||||
// --- prompts ---
|
||||
{
|
||||
const dir = join(root, 'api', 'coach', 'prompts');
|
||||
const names = readdirSync(dir).filter(f => f.endsWith('.md')).sort();
|
||||
const entries = names.map(f => ` ${JSON.stringify(f.replace(/\.md$/, ''))}: ${JSON.stringify(readFileSync(join(dir, f), 'utf8'))}`);
|
||||
const body = '/* GENERATED by scripts/build-coach-assets.mjs from api/coach/prompts/*.md — edit those, not this. */\n' +
|
||||
'export const PROMPTS = Object.freeze({\n' + entries.join(',\n') + '\n});\n';
|
||||
outputs.push({ file: join(core, 'prompts.js'), body, note: `${names.length} prompts` });
|
||||
}
|
||||
|
||||
let stale = 0;
|
||||
for (const { file, body, note } of outputs) {
|
||||
const rel = file.slice(root.length + 1);
|
||||
if (check) {
|
||||
let current = null;
|
||||
try { current = readFileSync(file, 'utf8'); } catch { /* missing counts as stale */ }
|
||||
if (current !== body) { stale++; console.error(`${rel} is out of date — run: node scripts/build-coach-assets.mjs`); }
|
||||
else console.log(`${rel} in sync (${note}).`);
|
||||
} else {
|
||||
writeFileSync(file, body);
|
||||
console.log(`Wrote ${rel} — ${note}, ${(body.length / 1024).toFixed(0)} KB.`);
|
||||
}
|
||||
}
|
||||
if (stale) process.exit(1);
|
||||
@@ -1,40 +1,4 @@
|
||||
#!/usr/bin/env node
|
||||
/* Generates api/coach/library.json from the frontend's exercise dataset.
|
||||
*
|
||||
* node scripts/build-coach-library.mjs # write
|
||||
* node scripts/build-coach-library.mjs --check # fail if stale (CI)
|
||||
*
|
||||
* The API needs the catalogue for two things the client cannot do for it: validating that
|
||||
* every exercise id a proposal references actually exists (FR-16), and building payloads for
|
||||
* scheduled reviews, which run with nobody's browser open. Rather than teach the api build to
|
||||
* reach into frontend/, the index is generated and committed — the same arrangement the
|
||||
* translated instruction packs in src/instr/ already use.
|
||||
*
|
||||
* Only the fields the Coach reasons over are kept: an id, a name, and the three taxonomy
|
||||
* fields it filters on. Instructions, images and secondary muscles stay out — they would
|
||||
* quadruple a payload the model has no use for.
|
||||
*/
|
||||
import { readFileSync, writeFileSync } from 'node:fs';
|
||||
import { dirname, join } from 'node:path';
|
||||
import { fileURLToPath, pathToFileURL } from 'node:url';
|
||||
|
||||
const root = join(dirname(fileURLToPath(import.meta.url)), '..');
|
||||
const src = join(root, 'frontend', 'src', 'lib', 'exercises-data.js');
|
||||
const out = join(root, 'api', 'coach', 'library.json');
|
||||
|
||||
const { EXDB } = await import(pathToFileURL(src).href);
|
||||
const index = EXDB.map(e => ({ id: e.id, n: e.n, bp: e.bp, tg: e.tg, eq: e.eq }));
|
||||
const json = JSON.stringify({ generated_from: 'frontend/src/lib/exercises-data.js', count: index.length, exercises: index }) + '\n';
|
||||
|
||||
if (process.argv.includes('--check')) {
|
||||
let current = null;
|
||||
try { current = readFileSync(out, 'utf8'); } catch { /* missing counts as stale */ }
|
||||
if (current !== json) {
|
||||
console.error('api/coach/library.json is out of date — run: node scripts/build-coach-library.mjs');
|
||||
process.exit(1);
|
||||
}
|
||||
console.log(`api/coach/library.json in sync (${index.length} exercises).`);
|
||||
} else {
|
||||
writeFileSync(out, json);
|
||||
console.log(`Wrote ${out} — ${index.length} exercises, ${(json.length / 1024).toFixed(0)} KB.`);
|
||||
}
|
||||
/* Kept as an alias so existing CI steps and habits keep working. The generator now also
|
||||
* produces the prompt module and lives in build-coach-assets.mjs; both flags pass through. */
|
||||
await import('./build-coach-assets.mjs');
|
||||
Reference in new issue
Block a user