Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

3.9 KB
Raw Blame
// Compiles apps/desktop/help-knowledge/*.md into a generated TS module that the
// main process imports (main is Vite-bundled and can't read loose .md at runtime).
// Run after editing any help doc:  pnpm gen:help-kb   (commit the generated file)
// CI drift check (regenerate + compare, no write):  pnpm gen:help-kb:check
import { readdirSync, readFileSync, writeFileSync } from 'node:fs';
import { dirname, join, resolve } from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
import process from 'node:process';
import matter from 'gray-matter';

const here = dirname(fileURLToPath(import.meta.url));
const KB_DIR = resolve(here, '../help-knowledge');
const OUT = resolve(here, '../src/main/maker-ipc/helpKnowledge.generated.ts');
// Must stay in sync with HelpTabId in src/shared/helpTypes.ts.
const ALLOWED_TABS = new Set([
  'general',
  'appearance',
  'personalization',
  'api-keys',
  'providers',
  'voice-input',
  'import',
  'connections',
  'im-bot',
  'about',
  'ghosts',
  'remote-control',
]);

function fail(msg) {
  console.error(`[gen-help-kb] ${msg}`);
  process.exit(1);
}

/**
 * Read + validate help-knowledge/*.md and render the generated TS module.
 * Pure (no write) so both the writer (CLI) and the drift guard can share it.
 * Validation failures hard-exit (used by both paths).
 */
export function renderHelpKnowledgeModule() {
  const files = readdirSync(KB_DIR).filter((f) => f.endsWith('.md')).sort();
  if (files.length === 0) fail(`no .md files found in ${KB_DIR}`);

  const seen = new Set();
  const docs = [];
  for (const file of files) {
    const { data, content } = matter(readFileSync(join(KB_DIR, file), 'utf8'));
    const id = String(data.id ?? '').trim();
    const title = String(data.title ?? '').trim();
    const summary = String(data.summary ?? '').trim();
    const tab = data.tab === undefined ? undefined : String(data.tab).trim();
    // Normalize EOL so generated output is deterministic regardless of the
    // checkout's line endings (Windows autocrlf would otherwise embed \r\n).
    const body = content.replace(/\r\n/g, '\n').trim();
    if (!id) fail(`${file}: missing 'id'`);
    if (!/^[a-z0-9-]+$/.test(id)) fail(`${file}: id '${id}' must be kebab-case [a-z0-9-]`);
    if (seen.has(id)) fail(`${file}: duplicate id '${id}'`);
    if (!title) fail(`${file}: missing 'title'`);
    if (!summary) fail(`${file}: missing 'summary'`);
    if (tab !== undefined && !ALLOWED_TABS.has(tab)) {
      fail(`${file}: tab '${tab}' not in [${[...ALLOWED_TABS].join(', ')}]`);
    }
    if (!body) fail(`${file}: empty body`);
    seen.add(id);
    docs.push({ id, title, summary, tab, content: body });
  }

  docs.sort((a, b) => a.id.localeCompare(b.id));

  const entries = docs
    .map((d) => {
      const lines = [
        '  {',
        `    id: ${JSON.stringify(d.id)},`,
        `    title: ${JSON.stringify(d.title)},`,
        `    summary: ${JSON.stringify(d.summary)},`,
      ];
      if (d.tab !== undefined) lines.push(`    tab: ${JSON.stringify(d.tab)},`);
      lines.push(`    content: ${JSON.stringify(d.content)},`, '  },');
      return lines.join('\n');
    })
    .join('\n');

  const content = `// AUTO-GENERATED by scripts/gen-help-kb.mjs — do not edit by hand.
// Source: apps/desktop/help-knowledge/*.md  ·  Regenerate: pnpm gen:help-kb
import type { HelpTabId } from '../../shared/helpTypes.js';

export interface HelpKnowledgeDoc {
  id: string;
  title: string;
  summary: string;
  tab?: HelpTabId;
  content: string;
}

export const HELP_KNOWLEDGE: HelpKnowledgeDoc[] = [
${entries}
];
`;

  return { outPath: OUT, content, count: docs.length };
}

// CLI: write the generated file when run directly (not when imported by the guard).
if (import.meta.url === pathToFileURL(process.argv[1] ?? '').href) {
  const { outPath, content, count } = renderHelpKnowledgeModule();
  writeFileSync(outPath, content, 'utf8');
  console.log(`[gen-help-kb] wrote ${count} docs -> ${outPath}`);
}