From f7b42be23bb4525a60df7a980f64232de5053b59 Mon Sep 17 00:00:00 2001 From: Paul Bakaus Date: Tue, 16 Dec 2025 14:54:13 -0800 Subject: [PATCH] Debug: Inline API logic with error handling and logging MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Inlined the file reading logic directly in API handlers to eliminate import issues. Added try/catch with JSON error responses to diagnose the timeout issue. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- api/commands.js | 44 ++++++++++++++++++++++++++-- api/patterns.js | 76 +++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 114 insertions(+), 6 deletions(-) diff --git a/api/commands.js b/api/commands.js index fe06c613f..86a66b087 100644 --- a/api/commands.js +++ b/api/commands.js @@ -1,7 +1,45 @@ -import { getCommands } from "../server/lib/api-handlers.js"; +import { readdir, readFile } from "fs/promises"; +import { join, dirname } from "path"; +import { fileURLToPath } from "url"; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); +const PROJECT_ROOT = join(__dirname, ".."); export default async function handler(request) { - const commands = await getCommands(); - return Response.json(commands); + try { + const sourceDir = join(PROJECT_ROOT, "source"); + const commandsDir = join(sourceDir, "commands"); + + console.log("PROJECT_ROOT:", PROJECT_ROOT); + console.log("commandsDir:", commandsDir); + + const files = await readdir(commandsDir); + const commands = []; + + for (const file of files) { + if (file.endsWith(".md")) { + const content = await readFile(join(commandsDir, file), "utf-8"); + const frontmatterMatch = content.match(/^---\n([\s\S]+?)\n---/); + + if (frontmatterMatch) { + const frontmatter = frontmatterMatch[1]; + const nameMatch = frontmatter.match(/name:\s*(.+)/); + const descMatch = frontmatter.match(/description:\s*(.+)/); + + commands.push({ + id: file.replace(".md", ""), + name: nameMatch?.[1]?.trim() || file.replace(".md", ""), + description: descMatch?.[1]?.trim() || "No description available", + }); + } + } + } + + return Response.json(commands); + } catch (error) { + console.error("Error in /api/commands:", error); + return Response.json({ error: error.message, stack: error.stack }, { status: 500 }); + } } diff --git a/api/patterns.js b/api/patterns.js index cca824433..7fec7e4d6 100644 --- a/api/patterns.js +++ b/api/patterns.js @@ -1,6 +1,76 @@ -import { getPatterns } from "../server/lib/api-handlers.js"; +import { readFile } from "fs/promises"; +import { join, dirname } from "path"; +import { fileURLToPath } from "url"; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); +const PROJECT_ROOT = join(__dirname, ".."); export default async function handler(request) { - const patterns = await getPatterns(); - return Response.json(patterns); + try { + const sourceDir = join(PROJECT_ROOT, "source"); + const filePath = join(sourceDir, "patterns.md"); + + console.log("Reading patterns from:", filePath); + + const content = await readFile(filePath, "utf-8"); + const frontmatterMatch = content.match(/^---\n([\s\S]+?)\n---/); + + if (!frontmatterMatch) { + return Response.json({ patterns: [], antipatterns: [] }); + } + + const frontmatterText = frontmatterMatch[1]; + const patterns = []; + const antipatterns = []; + const lines = frontmatterText.split('\n'); + let currentSection = null; + let currentCategory = null; + let inItems = false; + + for (const line of lines) { + const trimmed = line.trim(); + if (!trimmed) continue; + + const indent = line.length - line.trimStart().length; + + if (indent === 0 && trimmed === 'patterns:') { + currentSection = 'patterns'; + currentCategory = null; + inItems = false; + continue; + } + if (indent === 0 && trimmed === 'antipatterns:') { + currentSection = 'antipatterns'; + currentCategory = null; + inItems = false; + continue; + } + + if (trimmed.startsWith('- name:') && currentSection) { + currentCategory = { name: trimmed.slice(7).trim(), items: [] }; + if (currentSection === 'patterns') { + patterns.push(currentCategory); + } else { + antipatterns.push(currentCategory); + } + inItems = false; + continue; + } + + if (trimmed === 'items:' && currentCategory) { + inItems = true; + continue; + } + + if (trimmed.startsWith('- ') && inItems && currentCategory && indent >= 6) { + currentCategory.items.push(trimmed.slice(2).trim()); + } + } + + return Response.json({ patterns, antipatterns }); + } catch (error) { + console.error("Error in /api/patterns:", error); + return Response.json({ error: error.message, stack: error.stack }, { status: 500 }); + } }