feat: initial CodeBoard monorepo scaffold

Turborepo monorepo with npm workspaces:
- apps/web: Next.js 14 frontend with Tailwind v4, SSE progress, doc viewer
- apps/worker: BullMQ job processor (clone → parse → LLM generate)
- packages/shared: TypeScript types
- packages/parser: Babel-based AST parser (JS/TS) + regex (Python)
- packages/llm: OpenAI/Anthropic provider abstraction + prompt pipeline
- packages/diagrams: Mermaid architecture & dependency graph generators
- packages/database: Prisma schema (PostgreSQL)
- Docker multi-stage build (web + worker targets)

All packages compile successfully with tsc and next build.
This commit is contained in:
Vectry
2026-02-09 15:22:50 +00:00
parent efdc282da5
commit 79dad6124f
72 changed files with 10132 additions and 136 deletions

View File

@@ -0,0 +1,24 @@
{
"name": "@codeboard/database",
"version": "0.0.1",
"private": true,
"main": "./src/client.ts",
"types": "./src/client.ts",
"exports": {
".": "./src/client.ts"
},
"scripts": {
"build": "echo 'database package uses prisma generate'",
"db:generate": "prisma generate",
"db:push": "prisma db push",
"db:migrate": "prisma migrate dev",
"clean": "rm -rf generated"
},
"dependencies": {
"@prisma/client": "^6.3.0"
},
"devDependencies": {
"prisma": "^6.3.0",
"typescript": "^5.7"
}
}

View File

@@ -0,0 +1,50 @@
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
}
model Generation {
id String @id @default(cuid())
repoUrl String
repoName String
commitHash String
status Status @default(QUEUED)
progress Int @default(0)
result Json?
error String?
costUsd Float?
duration Int?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
userId String?
user User? @relation(fields: [userId], references: [id])
viewCount Int @default(0)
@@unique([repoUrl, commitHash])
@@index([repoUrl])
@@index([status])
}
model User {
id String @id @default(cuid())
githubId String @unique
login String
email String?
avatarUrl String?
createdAt DateTime @default(now())
generations Generation[]
}
enum Status {
QUEUED
CLONING
PARSING
GENERATING
RENDERING
COMPLETED
FAILED
}

View File

@@ -0,0 +1,12 @@
import { PrismaClient } from "@prisma/client";
const globalForPrisma = globalThis as unknown as { prisma: PrismaClient };
export const prisma =
globalForPrisma.prisma ?? new PrismaClient();
if (process.env.NODE_ENV !== "production") {
globalForPrisma.prisma = prisma;
}
export { PrismaClient } from "@prisma/client";

View File

@@ -0,0 +1,24 @@
{
"name": "@codeboard/diagrams",
"version": "0.0.1",
"private": true,
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
},
"scripts": {
"build": "tsc",
"clean": "rm -rf dist",
"dev": "tsc --watch"
},
"dependencies": {
"@codeboard/shared": "*"
},
"devDependencies": {
"typescript": "^5.7"
}
}

View File

@@ -0,0 +1,49 @@
import type { ModuleNode, DependencyEdge } from "@codeboard/shared";
function sanitizeId(name: string): string {
return name.replace(/[^a-zA-Z0-9]/g, "_");
}
function truncateLabel(name: string, max = 20): string {
return name.length > max ? name.slice(0, max - 1) + "\u2026" : name;
}
export function generateArchitectureDiagram(
modules: ModuleNode[],
deps: DependencyEdge[]
): string {
if (modules.length === 0) {
return "flowchart TD\n empty[No modules detected]";
}
const lines: string[] = ["flowchart TD"];
const moduleIds = new Map<string, string>();
for (const mod of modules) {
const id = sanitizeId(mod.name);
moduleIds.set(mod.path, id);
const fileCount = mod.files.length;
lines.push(` ${id}["${truncateLabel(mod.name)}\\n${fileCount} files"]`);
}
const edgeSet = new Set<string>();
for (const dep of deps) {
const sourceModule = modules.find((m) => m.files.includes(dep.source));
const targetModule = modules.find((m) => m.files.includes(dep.target));
if (!sourceModule || !targetModule) continue;
if (sourceModule.path === targetModule.path) continue;
const sourceId = moduleIds.get(sourceModule.path);
const targetId = moduleIds.get(targetModule.path);
if (!sourceId || !targetId) continue;
const edgeKey = `${sourceId}-${targetId}`;
if (edgeSet.has(edgeKey)) continue;
edgeSet.add(edgeKey);
lines.push(` ${sourceId} --> ${targetId}`);
}
return lines.join("\n");
}

View File

@@ -0,0 +1,48 @@
import type { FileNode, DependencyEdge } from "@codeboard/shared";
function sanitizeId(path: string): string {
return path.replace(/[^a-zA-Z0-9]/g, "_");
}
function shortenPath(path: string): string {
const parts = path.split("/");
if (parts.length <= 2) return path;
return parts.slice(-2).join("/");
}
export function generateDependencyGraph(
files: FileNode[],
deps: DependencyEdge[]
): string {
if (files.length === 0) {
return "graph LR\n empty[No files detected]";
}
const maxFiles = 30;
const topFiles = files.slice(0, maxFiles);
const topPaths = new Set(topFiles.map((f) => f.path));
const lines: string[] = ["graph LR"];
for (const file of topFiles) {
const id = sanitizeId(file.path);
const label = shortenPath(file.path);
lines.push(` ${id}["${label}"]`);
}
const edgeSet = new Set<string>();
for (const dep of deps) {
if (!topPaths.has(dep.source) || !topPaths.has(dep.target)) continue;
if (dep.source === dep.target) continue;
const edgeKey = `${dep.source}-${dep.target}`;
if (edgeSet.has(edgeKey)) continue;
edgeSet.add(edgeKey);
const sourceId = sanitizeId(dep.source);
const targetId = sanitizeId(dep.target);
lines.push(` ${sourceId} --> ${targetId}`);
}
return lines.join("\n");
}

View File

@@ -0,0 +1,2 @@
export { generateArchitectureDiagram } from "./architecture.js";
export { generateDependencyGraph } from "./dependency.js";

View File

@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "./dist",
"rootDir": "./src"
},
"include": ["src"]
}

26
packages/llm/package.json Normal file
View File

@@ -0,0 +1,26 @@
{
"name": "@codeboard/llm",
"version": "0.0.1",
"private": true,
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
},
"scripts": {
"build": "tsc",
"clean": "rm -rf dist",
"dev": "tsc --watch"
},
"dependencies": {
"@codeboard/shared": "*",
"openai": "^4.77.0",
"@anthropic-ai/sdk": "^0.36.0"
},
"devDependencies": {
"typescript": "^5.7"
}
}

View File

@@ -0,0 +1,72 @@
import type { FileNode } from "@codeboard/shared";
const APPROX_CHARS_PER_TOKEN = 4;
export function chunkCode(content: string, maxTokens: number): string[] {
const maxChars = maxTokens * APPROX_CHARS_PER_TOKEN;
if (content.length <= maxChars) return [content];
const lines = content.split("\n");
const chunks: string[] = [];
let current: string[] = [];
let currentLen = 0;
for (const line of lines) {
if (currentLen + line.length > maxChars && current.length > 0) {
chunks.push(current.join("\n"));
current = [];
currentLen = 0;
}
current.push(line);
currentLen += line.length + 1;
}
if (current.length > 0) {
chunks.push(current.join("\n"));
}
return chunks;
}
export function extractSignatures(fileNode: FileNode): string {
const parts: string[] = [];
parts.push(`File: ${fileNode.path} (${fileNode.language})`);
if (fileNode.imports.length > 0) {
parts.push("Imports:");
for (const imp of fileNode.imports) {
parts.push(` from "${imp.source}" import {${imp.specifiers.join(", ")}}`);
}
}
if (fileNode.exports.length > 0) {
parts.push("Exports:");
for (const exp of fileNode.exports) {
parts.push(` ${exp.isDefault ? "default " : ""}${exp.name}`);
}
}
for (const fn of fileNode.functions) {
const params = fn.params.join(", ");
const ret = fn.returnType ? `: ${fn.returnType}` : "";
const doc = fn.docstring ? `${fn.docstring.slice(0, 100)}` : "";
parts.push(`function ${fn.name}(${params})${ret}${doc}`);
}
for (const cls of fileNode.classes) {
parts.push(`class ${cls.name}`);
for (const method of cls.methods) {
parts.push(` method ${method.name}(${method.params.join(", ")})`);
}
for (const prop of cls.properties) {
parts.push(` property ${prop.name}${prop.type ? `: ${prop.type}` : ""}`);
}
}
return parts.join("\n");
}
export function estimateTokens(text: string): number {
return Math.ceil(text.length / APPROX_CHARS_PER_TOKEN);
}

View File

@@ -0,0 +1,4 @@
export { createProvider } from "./providers/factory.js";
export { generateDocumentation } from "./pipeline.js";
export { chunkCode, extractSignatures } from "./chunker.js";
export type { LLMProvider } from "./providers/base.js";

View File

@@ -0,0 +1,153 @@
import type { CodeStructure, GeneratedDocs, FileNode } from "@codeboard/shared";
import type { LLMProvider } from "./providers/base.js";
import { buildArchitecturePrompt } from "./prompts/architecture-overview.js";
import { buildModuleSummaryPrompt } from "./prompts/module-summary.js";
import { buildPatternsPrompt } from "./prompts/patterns-detection.js";
import { buildGettingStartedPrompt } from "./prompts/getting-started.js";
function parseSection(text: string, header: string): string {
const regex = new RegExp(`## ${header}\\s*\\n([\\s\\S]*?)(?=\\n## |$)`);
const match = regex.exec(text);
return match?.[1]?.trim() ?? "";
}
function parseMermaid(text: string): string {
const match = /```mermaid\s*\n([\s\S]*?)```/.exec(text);
return match?.[1]?.trim() ?? "flowchart TD\n A[No diagram generated]";
}
function parseList(text: string): string[] {
return text
.split("\n")
.map((l) => l.replace(/^[-*]\s*/, "").trim())
.filter(Boolean);
}
export async function generateDocumentation(
codeStructure: CodeStructure,
provider: LLMProvider,
onProgress?: (stage: string, progress: number) => void
): Promise<GeneratedDocs> {
onProgress?.("architecture", 10);
const archMessages = buildArchitecturePrompt(codeStructure);
const archResponse = await provider.chat(archMessages);
const architectureOverview = parseSection(archResponse, "Architecture Overview");
const techStackRaw = parseSection(archResponse, "Tech Stack");
const architectureDiagram = parseMermaid(archResponse);
const techStack = techStackRaw.split(",").map((s) => s.trim()).filter(Boolean);
onProgress?.("modules", 30);
const moduleLimit = Math.min(codeStructure.modules.length, 10);
const moduleSummaries = await Promise.all(
codeStructure.modules.slice(0, moduleLimit).map(async (mod) => {
const moduleFiles: FileNode[] = codeStructure.files.filter((f) =>
mod.files.includes(f.path)
);
if (moduleFiles.length === 0) {
return {
name: mod.name,
path: mod.path,
summary: "Empty module — no parseable files found.",
keyFiles: [],
publicApi: [],
dependsOn: [],
dependedBy: [],
};
}
const messages = buildModuleSummaryPrompt(mod, moduleFiles);
const response = await provider.chat(messages, { model: undefined });
const summary = parseSection(response, "Summary");
const keyFilesRaw = parseList(parseSection(response, "Key Files"));
const publicApi = parseList(parseSection(response, "Public API"));
const dependsOn = [
...new Set(
moduleFiles.flatMap((f) =>
f.imports
.map((imp) => imp.source)
.filter((s) => !s.startsWith("."))
)
),
].slice(0, 10);
const dependedBy = codeStructure.dependencies
.filter((d) => mod.files.includes(d.target))
.map((d) => d.source)
.filter((s) => !mod.files.includes(s))
.slice(0, 10);
return {
name: mod.name,
path: mod.path,
summary: summary || "Module analyzed but no summary generated.",
keyFiles: keyFilesRaw.map((kf) => ({ path: kf, purpose: "" })),
publicApi,
dependsOn,
dependedBy,
};
})
);
onProgress?.("patterns", 60);
const patternsMessages = buildPatternsPrompt(codeStructure);
const patternsResponse = await provider.chat(patternsMessages);
const conventions = parseList(parseSection(patternsResponse, "Coding Conventions"));
const designPatterns = parseList(parseSection(patternsResponse, "Design Patterns"));
const architecturalDecisions = parseList(parseSection(patternsResponse, "Architectural Decisions"));
onProgress?.("getting-started", 80);
const gsMessages = buildGettingStartedPrompt(
codeStructure,
architectureOverview
);
const gsResponse = await provider.chat(gsMessages);
const prerequisites = parseList(parseSection(gsResponse, "Prerequisites"));
const setupSteps = parseList(parseSection(gsResponse, "Setup Steps"));
const firstTask = parseSection(gsResponse, "Your First Task");
onProgress?.("complete", 100);
const languages = [...new Set(codeStructure.files.map((f) => f.language))];
return {
id: "",
repoUrl: "",
repoName: "",
generatedAt: new Date().toISOString(),
sections: {
overview: {
title: "Architecture Overview",
description: architectureOverview,
architectureDiagram,
techStack,
keyMetrics: {
files: codeStructure.files.length,
modules: codeStructure.modules.length,
languages,
},
},
modules: moduleSummaries,
patterns: {
conventions,
designPatterns,
architecturalDecisions,
},
gettingStarted: {
prerequisites,
setupSteps,
firstTask,
},
dependencyGraph: architectureDiagram,
},
};
}

View File

@@ -0,0 +1,51 @@
import type { LLMMessage, CodeStructure } from "@codeboard/shared";
export function buildArchitecturePrompt(
structure: CodeStructure
): LLMMessage[] {
const fileTree = structure.files
.map((f) => ` ${f.path} (${f.language}, ${f.functions.length} functions, ${f.classes.length} classes)`)
.join("\n");
const modules = structure.modules
.map((m) => ` ${m.name}/ (${m.files.length} files)`)
.join("\n");
const entryPoints = structure.entryPoints.join(", ") || "none detected";
return [
{
role: "system",
content: `You are an expert software architect analyzing a codebase. Generate a concise architecture overview and a Mermaid flowchart diagram.
Output format (use exactly these headers):
## Architecture Overview
[2-4 paragraphs describing the high-level architecture, key design decisions, and how components interact]
## Tech Stack
[comma-separated list of technologies detected]
## Mermaid Diagram
\`\`\`mermaid
[flowchart TD diagram showing modules and their relationships]
\`\`\``,
},
{
role: "user",
content: `Analyze this codebase structure:
FILE TREE:
${fileTree}
MODULES:
${modules}
ENTRY POINTS: ${entryPoints}
DEPENDENCIES (import edges):
${structure.dependencies.slice(0, 50).map((d) => ` ${d.source} -> ${d.target}`).join("\n")}
Generate the architecture overview with a Mermaid diagram.`,
},
];
}

View File

@@ -0,0 +1,43 @@
import type { LLMMessage, CodeStructure } from "@codeboard/shared";
export function buildGettingStartedPrompt(
structure: CodeStructure,
architectureOverview: string,
readmeContent?: string,
packageJsonContent?: string
): LLMMessage[] {
return [
{
role: "system",
content: `You are writing an onboarding guide for a new developer joining this project. Be specific and actionable.
Output format:
## Prerequisites
[list required tools, runtimes, and their versions]
## Setup Steps
[numbered list of concrete commands and actions to get the project running locally]
## Your First Task
[suggest a good first contribution — something small but meaningful that touches multiple parts of the codebase]`,
},
{
role: "user",
content: `Create an onboarding guide for this project.
ARCHITECTURE OVERVIEW:
${architectureOverview}
${readmeContent ? `README:\n${readmeContent.slice(0, 3000)}` : "README: not available"}
${packageJsonContent ? `PACKAGE.JSON:\n${packageJsonContent.slice(0, 2000)}` : ""}
LANGUAGES: ${[...new Set(structure.files.map((f) => f.language))].join(", ")}
ENTRY POINTS: ${structure.entryPoints.join(", ") || "none detected"}
TOTAL FILES: ${structure.files.length}
TOTAL MODULES: ${structure.modules.length}
Write a concrete, actionable onboarding guide.`,
},
];
}

View File

@@ -0,0 +1,42 @@
import type { LLMMessage, ModuleNode, FileNode } from "@codeboard/shared";
export function buildModuleSummaryPrompt(
module: ModuleNode,
files: FileNode[]
): LLMMessage[] {
const fileDetails = files
.map((f) => {
const fns = f.functions.map((fn) => ` ${fn.name}(${fn.params.join(", ")})`).join("\n");
const cls = f.classes.map((c) => ` class ${c.name}`).join("\n");
const exps = f.exports.map((e) => ` export ${e.isDefault ? "default " : ""}${e.name}`).join("\n");
return ` ${f.path}:\n${fns}\n${cls}\n${exps}`;
})
.join("\n\n");
return [
{
role: "system",
content: `You are analyzing a code module. Provide a concise summary.
Output format:
## Summary
[1-2 paragraphs explaining what this module does and its role in the project]
## Key Files
[list each important file with a one-line description]
## Public API
[list the main exported functions/classes and what they do]`,
},
{
role: "user",
content: `Module: ${module.name} (${module.path})
Files: ${module.files.length}
FILE DETAILS:
${fileDetails}
Summarize this module.`,
},
];
}

View File

@@ -0,0 +1,55 @@
import type { LLMMessage, CodeStructure } from "@codeboard/shared";
export function buildPatternsPrompt(structure: CodeStructure): LLMMessage[] {
const sampleFunctions = structure.files
.flatMap((f) => f.functions.map((fn) => `${f.path}: ${fn.name}(${fn.params.join(", ")})`))
.slice(0, 40)
.join("\n");
const sampleClasses = structure.files
.flatMap((f) => f.classes.map((c) => `${f.path}: class ${c.name} [${c.methods.map((m) => m.name).join(", ")}]`))
.slice(0, 20)
.join("\n");
const importSources = new Set<string>();
for (const f of structure.files) {
for (const imp of f.imports) {
importSources.add(imp.source);
}
}
return [
{
role: "system",
content: `You are a code reviewer identifying patterns and conventions in a codebase.
Output format:
## Coding Conventions
[list conventions like naming patterns, file organization, error handling approach]
## Design Patterns
[list design patterns detected: factory, singleton, observer, repository, etc.]
## Architectural Decisions
[list key architectural decisions: monorepo vs polyrepo, framework choices, state management, etc.]`,
},
{
role: "user",
content: `Analyze these code patterns:
FUNCTION SIGNATURES:
${sampleFunctions}
CLASS DEFINITIONS:
${sampleClasses}
EXTERNAL DEPENDENCIES:
${Array.from(importSources).filter((s) => !s.startsWith(".")).slice(0, 30).join(", ")}
DETECTED PATTERNS FROM AST:
${structure.patterns.map((p) => ` ${p.name}: ${p.description}`).join("\n") || " (none pre-detected)"}
Identify coding conventions, design patterns, and architectural decisions.`,
},
];
}

View File

@@ -0,0 +1,34 @@
import Anthropic from "@anthropic-ai/sdk";
import type { LLMMessage, LLMOptions } from "@codeboard/shared";
import type { LLMProvider } from "./base.js";
export class AnthropicProvider implements LLMProvider {
name = "anthropic";
private client: Anthropic;
private defaultModel: string;
constructor(apiKey: string, model?: string) {
this.client = new Anthropic({ apiKey });
this.defaultModel = model ?? "claude-sonnet-4-20250514";
}
async chat(messages: LLMMessage[], options?: LLMOptions): Promise<string> {
const systemMessage = messages.find((m) => m.role === "system");
const nonSystemMessages = messages
.filter((m) => m.role !== "system")
.map((m) => ({
role: m.role as "user" | "assistant",
content: m.content,
}));
const response = await this.client.messages.create({
model: options?.model ?? this.defaultModel,
max_tokens: options?.maxTokens ?? 4096,
system: systemMessage?.content,
messages: nonSystemMessages,
});
const textBlock = response.content.find((b) => b.type === "text");
return textBlock?.type === "text" ? textBlock.text : "";
}
}

View File

@@ -0,0 +1,6 @@
import type { LLMMessage, LLMOptions } from "@codeboard/shared";
export interface LLMProvider {
name: string;
chat(messages: LLMMessage[], options?: LLMOptions): Promise<string>;
}

View File

@@ -0,0 +1,15 @@
import type { LLMProviderConfig } from "@codeboard/shared";
import type { LLMProvider } from "./base.js";
import { OpenAIProvider } from "./openai.js";
import { AnthropicProvider } from "./anthropic.js";
export function createProvider(config: LLMProviderConfig): LLMProvider {
switch (config.provider) {
case "openai":
return new OpenAIProvider(config.apiKey, config.model, config.baseUrl);
case "anthropic":
return new AnthropicProvider(config.apiKey, config.model);
default:
throw new Error(`Unknown LLM provider: ${config.provider}`);
}
}

View File

@@ -0,0 +1,28 @@
import OpenAI from "openai";
import type { LLMMessage, LLMOptions } from "@codeboard/shared";
import type { LLMProvider } from "./base.js";
export class OpenAIProvider implements LLMProvider {
name = "openai";
private client: OpenAI;
private defaultModel: string;
constructor(apiKey: string, model?: string, baseUrl?: string) {
this.client = new OpenAI({
apiKey,
baseURL: baseUrl,
});
this.defaultModel = model ?? "gpt-4o";
}
async chat(messages: LLMMessage[], options?: LLMOptions): Promise<string> {
const response = await this.client.chat.completions.create({
model: options?.model ?? this.defaultModel,
messages: messages.map((m) => ({ role: m.role, content: m.content })),
temperature: options?.temperature ?? 0.3,
max_tokens: options?.maxTokens ?? 4096,
});
return response.choices[0]?.message?.content ?? "";
}
}

View File

@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "./dist",
"rootDir": "./src"
},
"include": ["src"]
}

View File

@@ -0,0 +1,29 @@
{
"name": "@codeboard/parser",
"version": "0.0.1",
"private": true,
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
},
"scripts": {
"build": "tsc",
"clean": "rm -rf dist",
"dev": "tsc --watch"
},
"dependencies": {
"@babel/parser": "^7.26.0",
"@babel/traverse": "^7.26.0",
"@babel/types": "^7.26.0",
"@codeboard/shared": "*",
"glob": "^11.0.0"
},
"devDependencies": {
"@types/babel__traverse": "^7.20.0",
"typescript": "^5.7"
}
}

View File

@@ -0,0 +1,150 @@
import { readFile } from "node:fs/promises";
import { dirname, basename } from "node:path";
import type {
CodeStructure,
FileNode,
ModuleNode,
DependencyEdge,
ExportNode,
} from "@codeboard/shared";
import { walkFiles } from "./file-walker.js";
import { typescriptParser } from "./languages/typescript.js";
import { pythonParser } from "./languages/python.js";
import type { LanguageParser } from "./languages/base.js";
const MAX_FILES = 200;
const parsers: LanguageParser[] = [typescriptParser, pythonParser];
function getParser(language: string): LanguageParser | null {
return (
parsers.find((p) =>
p.extensions.some((ext) => {
const langMap: Record<string, string[]> = {
typescript: [".ts", ".tsx"],
javascript: [".js", ".jsx", ".mjs", ".cjs"],
python: [".py"],
};
return langMap[language]?.includes(ext);
})
) ?? null
);
}
function buildModules(files: FileNode[]): ModuleNode[] {
const dirMap = new Map<string, string[]>();
for (const file of files) {
const dir = dirname(file.path);
const existing = dirMap.get(dir);
if (existing) {
existing.push(file.path);
} else {
dirMap.set(dir, [file.path]);
}
}
return Array.from(dirMap.entries()).map(([dirPath, filePaths]) => ({
name: basename(dirPath) || "root",
path: dirPath,
files: filePaths,
}));
}
function buildDependencies(files: FileNode[]): DependencyEdge[] {
const edges: DependencyEdge[] = [];
const filePathSet = new Set(files.map((f) => f.path));
for (const file of files) {
for (const imp of file.imports) {
let resolved = imp.source;
if (resolved.startsWith(".")) {
const dir = dirname(file.path);
const candidate = `${dir}/${resolved.replace(/^\.\//, "")}`;
const extensions = [".ts", ".tsx", ".js", ".jsx", ".py", ""];
for (const ext of extensions) {
if (filePathSet.has(candidate + ext)) {
resolved = candidate + ext;
break;
}
if (filePathSet.has(`${candidate}/index${ext}`)) {
resolved = `${candidate}/index${ext}`;
break;
}
}
}
edges.push({
source: file.path,
target: resolved,
type: "import",
});
}
}
return edges;
}
function detectEntryPoints(files: FileNode[]): string[] {
const entryNames = new Set([
"index",
"main",
"app",
"server",
"mod",
"lib",
"__init__",
]);
return files
.filter((f) => {
const name = basename(f.path).replace(/\.[^.]+$/, "");
return entryNames.has(name);
})
.map((f) => f.path);
}
function collectExports(files: FileNode[]): ExportNode[] {
const allExports: ExportNode[] = [];
for (const file of files) {
allExports.push(...file.exports);
}
return allExports;
}
export async function analyzeRepository(
repoPath: string
): Promise<CodeStructure> {
const walkedFiles = await walkFiles(repoPath);
const filesToAnalyze = walkedFiles.slice(0, MAX_FILES);
const parsedFiles: FileNode[] = [];
for (const walkedFile of filesToAnalyze) {
const parser = getParser(walkedFile.language);
if (!parser) continue;
try {
const content = await readFile(walkedFile.absolutePath, "utf-8");
const fileNode = parser.parse(content, walkedFile.relativePath);
parsedFiles.push(fileNode);
} catch {
continue;
}
}
const modules = buildModules(parsedFiles);
const dependencies = buildDependencies(parsedFiles);
const entryPoints = detectEntryPoints(parsedFiles);
const exports = collectExports(parsedFiles);
return {
files: parsedFiles,
modules,
entryPoints,
exports,
dependencies,
patterns: [],
};
}

View File

@@ -0,0 +1,121 @@
import { readdir, stat, readFile } from "node:fs/promises";
import { join, relative, extname, basename } from "node:path";
const IGNORED_DIRS = new Set([
"node_modules",
".git",
"dist",
"build",
"vendor",
"__pycache__",
".next",
".turbo",
"coverage",
".venv",
"venv",
".tox",
"target",
".cache",
".idea",
".vscode",
]);
const LANGUAGE_MAP: Record<string, string> = {
".ts": "typescript",
".tsx": "typescript",
".js": "javascript",
".jsx": "javascript",
".mjs": "javascript",
".cjs": "javascript",
".py": "python",
".go": "go",
".rs": "rust",
".java": "java",
".rb": "ruby",
".php": "php",
".cs": "csharp",
".cpp": "cpp",
".c": "c",
".h": "c",
".hpp": "cpp",
".swift": "swift",
".kt": "kotlin",
};
const ENTRY_POINT_NAMES = new Set([
"index",
"main",
"app",
"server",
"mod",
"lib",
"__init__",
"manage",
]);
export interface WalkedFile {
absolutePath: string;
relativePath: string;
language: string;
size: number;
isEntryPoint: boolean;
}
async function walkDir(
dir: string,
rootDir: string,
results: WalkedFile[]
): Promise<void> {
const entries = await readdir(dir, { withFileTypes: true });
for (const entry of entries) {
if (IGNORED_DIRS.has(entry.name)) continue;
if (entry.name.startsWith(".")) continue;
const fullPath = join(dir, entry.name);
if (entry.isDirectory()) {
await walkDir(fullPath, rootDir, results);
continue;
}
const ext = extname(entry.name);
const language = LANGUAGE_MAP[ext];
if (!language) continue;
const fileStat = await stat(fullPath);
if (fileStat.size > 500_000) continue;
const nameWithoutExt = basename(entry.name, ext);
const isEntryPoint = ENTRY_POINT_NAMES.has(nameWithoutExt);
results.push({
absolutePath: fullPath,
relativePath: relative(rootDir, fullPath),
language,
size: fileStat.size,
isEntryPoint,
});
}
}
export async function walkFiles(repoPath: string): Promise<WalkedFile[]> {
const results: WalkedFile[] = [];
await walkDir(repoPath, repoPath, results);
results.sort((a, b) => {
if (a.isEntryPoint && !b.isEntryPoint) return -1;
if (!a.isEntryPoint && b.isEntryPoint) return 1;
return a.relativePath.localeCompare(b.relativePath);
});
return results;
}
export async function readFileContent(filePath: string): Promise<string> {
return readFile(filePath, "utf-8");
}
export function detectLanguage(filePath: string): string | null {
return LANGUAGE_MAP[extname(filePath)] ?? null;
}

View File

@@ -0,0 +1,3 @@
export { analyzeRepository } from "./analyzer.js";
export { walkFiles } from "./file-walker.js";
export type { LanguageParser } from "./languages/base.js";

View File

@@ -0,0 +1,6 @@
import type { FileNode } from "@codeboard/shared";
export interface LanguageParser {
extensions: string[];
parse(content: string, filePath: string): FileNode;
}

View File

@@ -0,0 +1,157 @@
import type {
FileNode,
FunctionNode,
ClassNode,
ImportNode,
ExportNode,
} from "@codeboard/shared";
import type { LanguageParser } from "./base.js";
const FUNC_RE = /^(\s*)def\s+(\w+)\s*\(([^)]*)\)(?:\s*->\s*([^:]+))?\s*:/gm;
const CLASS_RE = /^(\s*)class\s+(\w+)(?:\(([^)]*)\))?\s*:/gm;
const IMPORT_RE = /^(?:from\s+([\w.]+)\s+)?import\s+(.+)$/gm;
const DOCSTRING_RE = /^\s*(?:"""([\s\S]*?)"""|'''([\s\S]*?)''')/;
function parseParams(raw: string): string[] {
if (!raw.trim()) return [];
return raw
.split(",")
.map((p) => p.trim().split(":")[0].split("=")[0].trim())
.filter((p) => p && p !== "self" && p !== "cls");
}
export const pythonParser: LanguageParser = {
extensions: [".py"],
parse(content: string, filePath: string): FileNode {
const lines = content.split("\n");
const functions: FunctionNode[] = [];
const classes: ClassNode[] = [];
const imports: ImportNode[] = [];
const exports: ExportNode[] = [];
let match: RegExpExecArray | null;
FUNC_RE.lastIndex = 0;
while ((match = FUNC_RE.exec(content)) !== null) {
const indent = match[1].length;
const name = match[2];
const params = parseParams(match[3]);
const returnType = match[4]?.trim();
const lineStart =
content.substring(0, match.index).split("\n").length;
let lineEnd = lineStart;
for (let i = lineStart; i < lines.length; i++) {
const line = lines[i];
if (
i > lineStart &&
line.trim() &&
!line.startsWith(" ".repeat(indent + 1)) &&
!line.startsWith("\t".repeat(indent === 0 ? 1 : indent))
) {
lineEnd = i;
break;
}
lineEnd = i + 1;
}
let docstring: string | undefined;
if (lineStart < lines.length) {
const bodyStart = lines.slice(lineStart, lineStart + 5).join("\n");
const docMatch = DOCSTRING_RE.exec(bodyStart);
if (docMatch) {
docstring = (docMatch[1] ?? docMatch[2]).trim();
}
}
if (indent === 0) {
functions.push({
name,
params,
returnType,
lineStart,
lineEnd,
docstring,
calls: [],
});
}
}
CLASS_RE.lastIndex = 0;
while ((match = CLASS_RE.exec(content)) !== null) {
const name = match[2];
const methods: FunctionNode[] = [];
const classLineStart =
content.substring(0, match.index).split("\n").length;
const classBody = content.substring(match.index + match[0].length);
const methodRe = /^\s{2,}def\s+(\w+)\s*\(([^)]*)\)(?:\s*->\s*([^:]+))?\s*:/gm;
let methodMatch: RegExpExecArray | null;
while ((methodMatch = methodRe.exec(classBody)) !== null) {
const methodLineStart =
classLineStart +
classBody.substring(0, methodMatch.index).split("\n").length;
methods.push({
name: methodMatch[1],
params: parseParams(methodMatch[2]),
returnType: methodMatch[3]?.trim(),
lineStart: methodLineStart,
lineEnd: methodLineStart + 1,
calls: [],
});
}
classes.push({ name, methods, properties: [] });
}
IMPORT_RE.lastIndex = 0;
while ((match = IMPORT_RE.exec(content)) !== null) {
const fromModule = match[1];
const importedNames = match[2]
.split(",")
.map((s) => s.trim().split(" as ")[0].trim())
.filter(Boolean);
if (fromModule) {
imports.push({ source: fromModule, specifiers: importedNames });
} else {
for (const name of importedNames) {
imports.push({ source: name, specifiers: [name] });
}
}
}
const allRe = /^__all__\s*=\s*\[([^\]]*)\]/m;
const allMatch = allRe.exec(content);
if (allMatch) {
const names = allMatch[1]
.split(",")
.map((s) => s.trim().replace(/['"]/g, ""))
.filter(Boolean);
for (const name of names) {
exports.push({ name, isDefault: false });
}
}
let complexity = 0;
for (const line of lines) {
const trimmed = line.trim();
if (trimmed.startsWith("if ") || trimmed.startsWith("elif ")) complexity++;
if (trimmed.startsWith("for ") || trimmed.startsWith("while ")) complexity++;
if (trimmed.startsWith("except")) complexity++;
if (trimmed.includes(" and ") || trimmed.includes(" or ")) complexity++;
}
return {
path: filePath,
language: "python",
size: content.length,
functions,
classes,
imports,
exports,
complexity,
};
},
};

View File

@@ -0,0 +1,227 @@
import { parse as babelParse } from "@babel/parser";
import _traverse from "@babel/traverse";
import type {
FileNode,
FunctionNode,
ClassNode,
ImportNode,
ExportNode,
} from "@codeboard/shared";
import type { LanguageParser } from "./base.js";
const traverse =
typeof _traverse === "function"
? _traverse
: (_traverse as unknown as { default: typeof _traverse }).default;
function extractFunctionParams(
params: Array<{ name?: string; left?: { name?: string }; type?: string }>
): string[] {
return params.map((p) => {
if (p.type === "AssignmentPattern" && p.left?.name) return p.left.name;
return p.name ?? "unknown";
});
}
export const typescriptParser: LanguageParser = {
extensions: [".ts", ".tsx", ".js", ".jsx", ".mjs", ".cjs"],
parse(content: string, filePath: string): FileNode {
const functions: FunctionNode[] = [];
const classes: ClassNode[] = [];
const imports: ImportNode[] = [];
const exports: ExportNode[] = [];
const calls: Set<string> = new Set();
let ast;
try {
ast = babelParse(content, {
sourceType: "module",
plugins: [
"typescript",
"jsx",
"decorators-legacy",
"classProperties",
"classPrivateProperties",
"classPrivateMethods",
"optionalChaining",
"nullishCoalescingOperator",
"dynamicImport",
],
errorRecovery: true,
});
} catch {
return {
path: filePath,
language: filePath.endsWith(".py") ? "python" : "typescript",
size: content.length,
functions: [],
classes: [],
imports: [],
exports: [],
complexity: 0,
};
}
traverse(ast, {
FunctionDeclaration(path) {
const node = path.node;
if (!node.id) return;
functions.push({
name: node.id.name,
params: extractFunctionParams(node.params as never[]),
returnType: node.returnType
? content.slice(node.returnType.start!, node.returnType.end!)
: undefined,
lineStart: node.loc?.start.line ?? 0,
lineEnd: node.loc?.end.line ?? 0,
calls: [],
});
},
ArrowFunctionExpression(path) {
const parent = path.parent;
if (
parent.type === "VariableDeclarator" &&
parent.id.type === "Identifier"
) {
const node = path.node;
functions.push({
name: parent.id.name,
params: extractFunctionParams(node.params as never[]),
returnType: node.returnType
? content.slice(node.returnType.start!, node.returnType.end!)
: undefined,
lineStart: node.loc?.start.line ?? 0,
lineEnd: node.loc?.end.line ?? 0,
calls: [],
});
}
},
ClassDeclaration(path) {
const node = path.node;
if (!node.id) return;
const methods: FunctionNode[] = [];
const properties: Array<{ name: string; type?: string }> = [];
for (const member of node.body.body) {
if (
member.type === "ClassMethod" &&
member.key.type === "Identifier"
) {
methods.push({
name: member.key.name,
params: extractFunctionParams(member.params as never[]),
lineStart: member.loc?.start.line ?? 0,
lineEnd: member.loc?.end.line ?? 0,
calls: [],
});
} else if (
member.type === "ClassProperty" &&
member.key.type === "Identifier"
) {
properties.push({
name: member.key.name,
type: member.typeAnnotation
? content.slice(
member.typeAnnotation.start!,
member.typeAnnotation.end!
)
: undefined,
});
}
}
classes.push({ name: node.id.name, methods, properties });
},
ImportDeclaration(path) {
const node = path.node;
const specifiers = node.specifiers.map((s) => s.local.name);
imports.push({ source: node.source.value, specifiers });
},
ExportDefaultDeclaration() {
exports.push({ name: "default", isDefault: true });
},
ExportNamedDeclaration(path) {
const node = path.node;
if (node.declaration) {
if (
node.declaration.type === "FunctionDeclaration" &&
node.declaration.id
) {
exports.push({
name: node.declaration.id.name,
isDefault: false,
});
} else if (
node.declaration.type === "ClassDeclaration" &&
node.declaration.id
) {
exports.push({
name: node.declaration.id.name,
isDefault: false,
});
} else if (node.declaration.type === "VariableDeclaration") {
for (const decl of node.declaration.declarations) {
if (decl.id.type === "Identifier") {
exports.push({ name: decl.id.name, isDefault: false });
}
}
}
}
if (node.specifiers) {
for (const spec of node.specifiers) {
if (spec.exported.type === "Identifier") {
exports.push({ name: spec.exported.name, isDefault: false });
}
}
}
},
CallExpression(path) {
const callee = path.node.callee;
if (callee.type === "Identifier") {
calls.add(callee.name);
} else if (
callee.type === "MemberExpression" &&
callee.property.type === "Identifier"
) {
calls.add(callee.property.name);
}
},
});
for (const fn of functions) {
fn.calls = Array.from(calls);
}
let complexity = 0;
traverse(ast, {
IfStatement() { complexity++; },
ForStatement() { complexity++; },
ForInStatement() { complexity++; },
ForOfStatement() { complexity++; },
WhileStatement() { complexity++; },
DoWhileStatement() { complexity++; },
SwitchCase() { complexity++; },
ConditionalExpression() { complexity++; },
LogicalExpression() { complexity++; },
CatchClause() { complexity++; },
});
return {
path: filePath,
language: filePath.match(/\.tsx?$/) ? "typescript" : "javascript",
size: content.length,
functions,
classes,
imports,
exports,
complexity,
};
},
};

View File

@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "./dist",
"rootDir": "./src"
},
"include": ["src"]
}

View File

@@ -0,0 +1,21 @@
{
"name": "@codeboard/shared",
"version": "0.0.1",
"private": true,
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
},
"scripts": {
"build": "tsc",
"clean": "rm -rf dist",
"dev": "tsc --watch"
},
"devDependencies": {
"typescript": "^5.7"
}
}

View File

@@ -0,0 +1,24 @@
export type {
CloneResult,
CloneMetadata,
FileNode,
FunctionNode,
ClassNode,
ClassProperty,
ImportNode,
ExportNode,
CodeStructure,
ModuleNode,
DetectedPattern,
DependencyEdge,
GeneratedDocs,
DocsOverview,
DocsModule,
DocsPatterns,
DocsGettingStarted,
Generation,
GenerationStatus,
LLMMessage,
LLMOptions,
LLMProviderConfig,
} from "./types.js";

View File

@@ -0,0 +1,185 @@
// ── Repository Cloning ──────────────────────────────────────────────
export interface CloneMetadata {
name: string;
description: string;
defaultBranch: string;
languages: Record<string, number>;
stars: number;
lastCommit: string;
totalFiles: number;
totalLines: number;
}
export interface CloneResult {
localPath: string;
metadata: CloneMetadata;
}
// ── AST Parsing ─────────────────────────────────────────────────────
export interface FunctionNode {
name: string;
params: string[];
returnType?: string;
lineStart: number;
lineEnd: number;
docstring?: string;
calls: string[];
}
export interface ClassProperty {
name: string;
type?: string;
}
export interface ClassNode {
name: string;
methods: FunctionNode[];
properties: ClassProperty[];
}
export interface ImportNode {
source: string;
specifiers: string[];
}
export interface ExportNode {
name: string;
isDefault: boolean;
}
export interface FileNode {
path: string;
language: string;
size: number;
functions: FunctionNode[];
classes: ClassNode[];
imports: ImportNode[];
exports: ExportNode[];
complexity: number;
}
export interface ModuleNode {
name: string;
path: string;
files: string[];
summary?: string;
}
export interface DetectedPattern {
name: string;
description: string;
examples: string[];
}
export interface DependencyEdge {
source: string;
target: string;
type: "import" | "call" | "extends";
}
export interface CodeStructure {
files: FileNode[];
modules: ModuleNode[];
entryPoints: string[];
exports: ExportNode[];
dependencies: DependencyEdge[];
patterns: DetectedPattern[];
}
// ── Generated Documentation ─────────────────────────────────────────
export interface DocsOverview {
title: string;
description: string;
architectureDiagram: string;
techStack: string[];
keyMetrics: {
files: number;
modules: number;
languages: string[];
};
}
export interface DocsModule {
name: string;
path: string;
summary: string;
keyFiles: Array<{ path: string; purpose: string }>;
publicApi: string[];
dependsOn: string[];
dependedBy: string[];
}
export interface DocsPatterns {
conventions: string[];
designPatterns: string[];
architecturalDecisions: string[];
}
export interface DocsGettingStarted {
prerequisites: string[];
setupSteps: string[];
firstTask: string;
}
export interface GeneratedDocs {
id: string;
repoUrl: string;
repoName: string;
generatedAt: string;
sections: {
overview: DocsOverview;
modules: DocsModule[];
patterns: DocsPatterns;
gettingStarted: DocsGettingStarted;
dependencyGraph: string;
};
}
// ── Generation State ────────────────────────────────────────────────
export type GenerationStatus =
| "QUEUED"
| "CLONING"
| "PARSING"
| "GENERATING"
| "RENDERING"
| "COMPLETED"
| "FAILED";
export interface Generation {
id: string;
repoUrl: string;
repoName: string;
commitHash: string;
status: GenerationStatus;
progress: number;
result: GeneratedDocs | null;
error: string | null;
costUsd: number | null;
duration: number | null;
createdAt: string;
viewCount: number;
}
// ── LLM Configuration ───────────────────────────────────────────────
export interface LLMMessage {
role: "system" | "user" | "assistant";
content: string;
}
export interface LLMOptions {
temperature?: number;
maxTokens?: number;
model?: string;
}
export interface LLMProviderConfig {
provider: "openai" | "anthropic";
apiKey: string;
model?: string;
baseUrl?: string;
}

View File

@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "./dist",
"rootDir": "./src"
},
"include": ["src"]
}