ShtukbaseStandard
archivo del paquete→ .shtukbase/packages/blueprint-skilled-agent/actions/setup.ts

actions/setup.ts

67 líneas, 3.0 KB.

El archivo tal como está en el paquete: el código y sus comentarios no se traducen.

  1. // On install: the one start answer the shtuk tool cannot put into the pages by itself. The tool fills
  2. // {{fact:key}} when it lays the pages; this action replaces, in the blueprint's own pages (the ones the lock lists
  3. // for it), {{model:key}} with the default model of the harness package the fact names (its adapter.ts).
  4. // A marker in a page header must stand inside double quotes; the value is escaped for them.
  5. // Usage: bun actions/setup.ts [--id <package id>] (from the project root; the id defaults to this folder's name)
  6. import { existsSync, readFileSync, writeFileSync } from 'node:fs';
  7. import { basename, dirname, join } from 'node:path';
  8. const root = process.cwd();
  9. const at = process.argv.indexOf('--id');
  10. const id = at > 0 ? process.argv[at + 1] : basename(dirname(dirname(Bun.fileURLToPath(import.meta.url))));
  11. const yaml = (t: string): any => (Bun.YAML.parse(t) as any) ?? {};
  12. const header = (t: string): any => {
  13. const m = /^---\n([\s\S]*?)\n---/.exec(t);
  14. return m ? yaml(m[1]) : {};
  15. };
  16. const pkgs = join(root, 'base', 'packages.md');
  17. const facts: Record<string, unknown> = existsSync(pkgs) ? header(readFileSync(pkgs, 'utf8')).facts ?? {} : {};
  18. function fact(key: string): string {
  19. let cur: any = key in facts ? facts[key] : facts;
  20. if (!(key in facts)) for (const p of key.split('.')) cur = cur && typeof cur === 'object' && p in cur ? cur[p] : undefined;
  21. return cur == null ? '' : String(cur);
  22. }
  23. const models = new Map<string, string>();
  24. async function model(key: string): Promise<string> {
  25. const h = fact(key);
  26. if (!models.has(h)) {
  27. const f = join(root, '.shtukbase', 'packages', `harness-${h}`, 'adapter.ts');
  28. models.set(h, existsSync(f) ? String((await import(f)).adapter?.defaultModel ?? '') : '');
  29. }
  30. return models.get(h)!;
  31. }
  32. const pages = Object.keys(yaml(readFileSync(join(root, '.shtukbase', 'packages.lock'), 'utf8')).packages?.[id]?.pages ?? {});
  33. const MARK = /\{\{\s*model:([\w.-]+)\s*\}\}/g;
  34. const missing = new Set<string>();
  35. let changed = 0;
  36. async function fill(text: string, quoted: boolean): Promise<string> {
  37. let out = '', last = 0;
  38. for (const m of text.matchAll(MARK)) {
  39. const v = await model(m[1]);
  40. if (!v) missing.add(m[1]);
  41. out += text.slice(last, m.index) + (quoted ? v.replace(/\\/g, '\\\\').replace(/"/g, '\\"') : v);
  42. last = m.index! + m[0].length;
  43. }
  44. return out + text.slice(last);
  45. }
  46. for (const rel of pages) {
  47. const file = join(root, rel);
  48. if (!existsSync(file)) continue;
  49. const text = readFileSync(file, 'utf8');
  50. if (!text.includes('{{')) continue;
  51. const m = /^---\n[\s\S]*?\n---\n?/.exec(text);
  52. const head = m ? await fill(m[0], true) : '';
  53. const next = head + (await fill(text.slice(head ? m![0].length : 0), false));
  54. if (next !== text) {
  55. writeFileSync(file, next);
  56. changed++;
  57. }
  58. }
  59. console.log(`${id}: the model of the harness is written into ${changed} pages`);
  60. if (missing.size) console.log(` no model for the harness from ${[...missing].join(', ')} — an empty value was written, fix the pages by hand`);