import { Agent } from "thinkwell:agent";
import { CLAUDE_CODE } from "thinkwell:connectors";
import * as fs from "fs/promises";
/**
* Result of analyzing a codebase.
* @JSONSchema
*/
interface CodeAnalysis {
/** Main programming language detected */
language: string;
/** List of potential issues */
issues: Array<{
file: string;
line: number;
description: string;
severity: "low" | "medium" | "high";
}>;
/** Summary of the codebase */
summary: string;
}
/**
* Input for reading a file.
* @JSONSchema
*/
interface ReadFileInput {
/** Path to the file to read */
path: string;
}
async function analyzeProject(projectPath: string) {
const agent = await Agent.connect(CLAUDE_CODE);
try {
const analysis = await agent
.think(CodeAnalysis.Schema)
.cwd(projectPath)
.text(`
Analyze this codebase for potential issues.
Use the read_file tool to examine source files.
Focus on bugs, security issues, and code smells.
`)
.tool(
"read_file",
"Read the contents of a file in the project.",
ReadFileInput.Schema,
async (input) => {
const content = await fs.readFile(input.path, "utf-8");
return { content };
}
)
.tool(
"list_files",
"List all files in the project directory.",
async () => {
const files = await fs.readdir(projectPath, { recursive: true });
return { files };
}
)
.run();
return analysis;
} finally {
agent.close();
}
}