Files
my-vault/.claude/scripts/test-summarization.cjs
T

152 lines
4.5 KiB
JavaScript
Raw Normal View History

2026-01-08 09:34:22 +08:00
#!/usr/bin/env node
/**
* Test Context File Summarization
*
* This script tests the file summarization functionality
*/
const path = require('path');
const fs = require('fs');
// Load the helper functions from main.js
const mainPath = path.join(__dirname, '..', '.obsidian', 'plugins', 'claudian', 'main.js');
// Extract YAML frontmatter
function extractYAMLFrontmatter(content) {
// Normalize line endings to \n
const normalized = content.replace(/\r\n/g, "\n");
const frontmatterMatch = normalized.match(/^---\n([\s\S]*?)\n---/);
if (!frontmatterMatch) return {};
const yaml = frontmatterMatch[1];
const result = {};
// Simple YAML parsing (key: value)
const lines = yaml.split("\n");
for (const line of lines) {
const match = line.match(/^(\w+):\s*(.+)$/);
if (match) {
const [, key, value] = match;
// Handle arrays
if (value.startsWith("[") && value.endsWith("]")) {
result[key] = value
.slice(1, -1)
.split(",")
.map(v => v.trim().replace(/^["']|["']$/g, ""));
} else {
result[key] = value.replace(/^["']|["']$/g, "");
}
}
}
return result;
}
// Extract markdown headings
function extractMarkdownHeadings(content) {
const headings = [];
// Normalize line endings
const lines = content.replace(/\r\n/g, "\n").split("\n");
for (const line of lines) {
const match = line.match(/^#{1,6}\s+(.+)$/);
if (match) {
headings.push(match[1].trim());
}
}
return headings;
}
// Generate file summary
async function generateFileSummary(filePath) {
const content = fs.readFileSync(filePath, "utf-8");
const fileName = path.basename(filePath);
// Extract YAML frontmatter
const frontmatter = extractYAMLFrontmatter(content);
// Extract markdown headings
const headings = extractMarkdownHeadings(content);
// Extract first paragraph (skip frontmatter)
// Normalize line endings first
const normalized = content.replace(/\r\n/g, "\n");
const textContent = normalized.replace(/^---\n[\s\S]*?\n---\n/, "");
const firstPara = textContent.trim().split("\n\n")[0] || "";
const preview = firstPara.slice(0, 300);
// Format summary
const parts = [];
parts.push(`📄 **${fileName}** _(Summary - ${(content.length / 1024).toFixed(1)}KB)_`);
parts.push("");
if (frontmatter.type || frontmatter.tags || frontmatter.created) {
parts.push("**Metadata:**");
if (frontmatter.type) parts.push(`- Type: ${frontmatter.type}`);
if (frontmatter.tags && frontmatter.tags.length > 0) {
parts.push(`- Tags: ${frontmatter.tags.join(", ")}`);
}
if (frontmatter.created) parts.push(`- Created: ${frontmatter.created}`);
parts.push("");
}
if (headings.length > 0) {
parts.push(`**Structure:** ${headings.slice(0, 5).join(" > ")}`);
if (headings.length > 5) parts.push(`_(+${headings.length - 5} more sections)_`);
parts.push("");
}
if (preview) {
parts.push("**Preview:**");
parts.push(preview + (firstPara.length > 300 ? "..." : ""));
parts.push("");
}
parts.push(`_📖 Use Read tool to view full content: \`${filePath}\`_`);
return parts.join("\n");
}
// Main test
async function main() {
console.log("=== Testing Context File Summarization ===\n");
const testFile = path.join(__dirname, '..', 'test-large-file.md');
console.log(`Test file: ${testFile}`);
const stats = fs.statSync(testFile);
console.log(`File size: ${stats.size} bytes (${(stats.size / 1024).toFixed(1)}KB)\n`);
// Debug: Check frontmatter extraction
const content = fs.readFileSync(testFile, "utf-8");
const frontmatter = extractYAMLFrontmatter(content);
const headings = extractMarkdownHeadings(content);
console.log("DEBUG - Extracted frontmatter:", JSON.stringify(frontmatter, null, 2));
console.log("DEBUG - Extracted headings (first 10):", headings.slice(0, 10));
console.log("");
console.log("Generating summary...\n");
const summary = await generateFileSummary(testFile);
console.log("=== SUMMARY OUTPUT ===\n");
console.log(summary);
console.log("\n=== END SUMMARY ===");
// Calculate token savings
const originalTokens = Math.ceil(stats.size / 4);
const summaryTokens = Math.ceil(summary.length / 4);
const savings = ((1 - summaryTokens / originalTokens) * 100).toFixed(1);
console.log(`\n=== Token Analysis ===`);
console.log(`Original size: ~${originalTokens} tokens`);
console.log(`Summary size: ~${summaryTokens} tokens`);
console.log(`Token savings: ${savings}%`);
}
main().catch(console.error);