#!/usr/bin/env bun /** * Generate Tool Descriptions * * 将 descriptions 目录下的 .txt 文件转换为 TypeScript 模块 * 用于 Bun 打包时内联工具描述 */ import * as fs from 'fs'; import * as path from 'path'; const DESCRIPTIONS_DIR = path.join(__dirname, '../src/tools/descriptions'); const OUTPUT_FILE = path.join(__dirname, '../src/tools/descriptions.generated.ts'); interface DescriptionEntry { category: string; name: string; content: string; } function collectDescriptions(): DescriptionEntry[] { const entries: DescriptionEntry[] = []; const categories = fs.readdirSync(DESCRIPTIONS_DIR); for (const category of categories) { const categoryPath = path.join(DESCRIPTIONS_DIR, category); const stat = fs.statSync(categoryPath); if (stat.isDirectory()) { const files = fs.readdirSync(categoryPath); for (const file of files) { if (file.endsWith('.txt')) { const name = file.replace('.txt', ''); const content = fs.readFileSync(path.join(categoryPath, file), 'utf-8').trim(); entries.push({ category, name, content }); } } } else if (category.endsWith('.txt')) { const name = category.replace('.txt', ''); const content = fs.readFileSync(categoryPath, 'utf-8').trim(); entries.push({ category: '', name, content }); } } return entries; } function escapeString(str: string): string { return str .replace(/\\/g, '\\\\') .replace(/`/g, '\\`') .replace(/\$/g, '\\$'); } function generateCode(entries: DescriptionEntry[]): string { const lines: string[] = [ '// Auto-generated by scripts/generate-descriptions.ts', '// Do not edit manually', '', 'export const TOOL_DESCRIPTIONS: Record = {', ]; for (const entry of entries) { const escapedContent = escapeString(entry.content); lines.push(` '${entry.name}': \`${escapedContent}\`,`); lines.push(''); } lines.push('};'); lines.push(''); return lines.join('\n'); } function main() { console.log('Collecting tool descriptions...'); const entries = collectDescriptions(); console.log(`Found ${entries.length} descriptions`); console.log('Generating TypeScript code...'); const code = generateCode(entries); console.log(`Writing to ${OUTPUT_FILE}...`); fs.writeFileSync(OUTPUT_FILE, code, 'utf-8'); console.log('Done!'); } main();