ShtukbaseStandard
archivo del paquete→ .shtukbase/packages/blueprint-content-factory/actions/setup.ts

actions/setup.ts

98 líneas, 6.1 KB.

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

  1. // `shtuk run blueprint-content-factory setup` (also on install): carries the answers of the start questions into the
  2. // project. The first topic and the platforms become records (an existing record is left alone), the language and
  3. // tone go to /content/voice.md, the harness's model to the two agent pages, the editor's schedule to its automation
  4. // (30 minutes after the author), and a first idea waits for a person's approval. The harness and the author's
  5. // schedule the shtuk tool puts into the pages itself ({{fact:factory.harness}}, {{fact:content.schedule}}).
  6. import { existsSync } from 'node:fs';
  7. import { dirname, join } from 'node:path';
  8. import { readPage } from '../../collections/checks/lib/pages.ts';
  9. import { create, slug, update } from '../../agents/lib/write.ts';
  10. import { facts, list, str } from '../lib/facts.ts';
  11. const root = process.cwd();
  12. const here = dirname(import.meta.dir);
  13. const f = facts(here, root);
  14. const at = (p: string) => join(root, p.replace(/^\/+/, ''));
  15. const say: string[] = [];
  16. /** Writes only the fields whose value differs, so a page the answers do not change stays as the tool laid it. */
  17. function set(path: string, fields: Record<string, unknown>): void {
  18. const fm = readPage(path).fm;
  19. const diff = Object.fromEntries(Object.entries(fields).filter(([k, v]) => JSON.stringify(fm[k]) !== JSON.stringify(v)));
  20. if (Object.keys(diff).length) update(path, diff as any);
  21. }
  22. /** Platform presets by name; anything else is a plain post up to 2000 characters. */
  23. const PRESETS: Record<string, { title: string; channel: string; format: string; minChars: number; maxChars: number; rules: string[] }> = {
  24. telegram: { title: 'Telegram', channel: 'Telegram channel', format: 'post', minChars: 300, maxChars: 2000,
  25. rules: ['The first line is the point, it is visible in the feed.', 'Paragraphs of 1-3 sentences.', 'At most one link, emoji only where they help.'] },
  26. blog: { title: 'Blog', channel: 'Blog on the site', format: 'article', minChars: 2000, maxChars: 12000,
  27. rules: ['A title and 3-6 subheadings.', 'The introduction says why to read, in two or three sentences.', 'A short conclusion at the end.'] },
  28. linkedin: { title: 'LinkedIn', channel: 'LinkedIn', format: 'post', minChars: 300, maxChars: 3000,
  29. rules: ['The first two lines hook: they are visible before "see more".', 'Experience and a conclusion, no advertising.', 'At most three hashtags, at the end.'] },
  30. x: { title: 'X', channel: 'X (Twitter)', format: 'post', minChars: 40, maxChars: 280,
  31. rules: ['One thought.', 'No hashtags in the middle.'] },
  32. newsletter: { title: 'Newsletter', channel: 'Letter to subscribers', format: 'newsletter', minChars: 1500, maxChars: 8000,
  33. rules: ['The subject of the letter is the title of the record.', 'Address one reader.', 'One main call to action.'] },
  34. };
  35. // 1. topic
  36. const topic = str(f['content.topic']);
  37. const topicPath = `/content/topics/${slug(topic)}.md`;
  38. if (topic && !existsSync(at(topicPath))) {
  39. create(root, '/content/topics', slug(topic), { type: 'Topic', title: topic, status: 'active' }, 'Who we write for and what about: complete this yourself.');
  40. say.push(`topic ${topicPath}`);
  41. }
  42. // 2. platforms
  43. const language = str(f['content.language']) || 'en';
  44. const tone = str(f['content.tone']);
  45. const platforms: string[] = [];
  46. for (const name of list(f['content.platforms'])) {
  47. const key = slug(name);
  48. const pre = PRESETS[key] ?? { title: name, channel: name, format: 'post', minChars: 0, maxChars: 2000, rules: ['The style rules of the platform: write them yourself.'] };
  49. const path = `/content/platforms/${key}.md`;
  50. platforms.push(path);
  51. if (existsSync(at(path))) continue;
  52. create(root, '/content/platforms', key, {
  53. type: 'Platform', title: pre.title, channel: pre.channel, format: pre.format, minChars: pre.minChars, maxChars: pre.maxChars,
  54. language, tone, status: 'active',
  55. }, `Style rules of the platform:\n\n${pre.rules.map((r) => `- ${r}`).join('\n')}`);
  56. say.push(`platform ${path}`);
  57. }
  58. // 3. voice
  59. if (existsSync(at('/content/voice.md'))) {
  60. set(at('/content/voice.md'), { language, ...(tone ? { tone } : {}) });
  61. say.push(`voice: ${language}${tone ? `, ${tone}` : ''}`);
  62. }
  63. // 4. the model of the agents (the harness itself is {{fact:factory.harness}} in their pages)
  64. const harness = str(f['factory.harness']) || 'claude';
  65. const MODELS: Record<string, string> = { claude: 'haiku', codex: 'gpt-6-luna', hermes: 'deepseek-v4.1-flash' };
  66. for (const a of ['/agents/content-author.md', '/agents/content-editor.md'])
  67. if (existsSync(at(a))) set(at(a), { model: MODELS[harness] ?? 'haiku' });
  68. say.push(`agents: ${harness}, ${MODELS[harness] ?? 'haiku'}`);
  69. // 5. schedule: the editor 30 minutes after the author when the minute is a plain number (the author's is {{fact:content.schedule}})
  70. const cron = str(f['content.schedule']) || '0 9 * * 1-5';
  71. const parts = cron.split(/\s+/);
  72. let editor = cron;
  73. if (parts.length === 5 && /^\d+$/.test(parts[0])) {
  74. const m = Number(parts[0]) + 30;
  75. if (m < 60) editor = [String(m), ...parts.slice(1)].join(' ');
  76. else if (/^\d+$/.test(parts[1])) editor = [String(m - 60), String((Number(parts[1]) + 1) % 24), ...parts.slice(2)].join(' ');
  77. }
  78. if (existsSync(at('/automations/content-editor.md'))) set(at('/automations/content-editor.md'), { trigger: { kind: 'schedule', cron: editor } });
  79. say.push(`schedule: author "${cron}", editor "${editor}"`);
  80. // 6. a first idea, waiting for a person
  81. if (!existsSync(at('/content/ideas')) || !(await Array.fromAsync(new Bun.Glob('*.md').scan(at('/content/ideas')))).length) {
  82. const p = create(root, '/content/ideas', 'first-idea', {
  83. type: 'Idea', title: `Why we write about "${topic || 'our work'}"`, status: 'new',
  84. ...(existsSync(at(topicPath)) ? { topic: topicPath.replace(/\.md$/, '') } : {}),
  85. ...(platforms[0] ? { platform: platforms[0].replace(/\.md$/, '') } : {}), proposedBy: 'blueprint-content-factory',
  86. }, 'The first text: who we are, what we will write about and why it is useful to the reader. Approve the idea (status: approved, approvedBy: your name) and the author agent will write a draft on its schedule.');
  87. say.push(`idea ${p} waits for approval`);
  88. }
  89. console.log(`content factory: ${say.join('; ')}`);