-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathanalyze-repo.ts
More file actions
199 lines (168 loc) · 6.47 KB
/
analyze-repo.ts
File metadata and controls
199 lines (168 loc) · 6.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import { query } from "@anthropic-ai/claude-agent-sdk";
import { metadata, schemaTask } from "@trigger.dev/sdk";
import { z } from "zod";
import { mkdtemp, rm } from "fs/promises";
import { tmpdir } from "os";
import { join } from "path";
import { exec } from "child_process";
import { promisify } from "util";
import { agentStream } from "./agent-stream";
const execAsync = promisify(exec);
// Schema for the task payload
const analyzeRepoSchema = z.object({
repoUrl: z.string().url(),
question: z.string().min(1),
});
export const analyzeRepo = schemaTask({
id: "analyze-repo",
schema: analyzeRepoSchema,
maxDuration: 600, // 10 minutes max
run: async ({ repoUrl, question }, { signal }) => {
const abortController = new AbortController();
signal.addEventListener("abort", () => {
abortController.abort();
});
// Update progress metadata
metadata.set("status", "Starting analysis...");
metadata.set("progress", 10);
// Validate GitHub URL
if (!repoUrl.includes("github.com")) {
throw new Error("Please provide a valid GitHub repository URL");
}
// Extract repo info
const repoName = repoUrl.split("/").slice(-2).join("/").replace(".git", "");
metadata.set("repository", repoName);
// Create temp directory for repo clone
metadata.set("status", "Creating temporary directory...");
metadata.set("progress", 20);
const tempDir = await mkdtemp(join(tmpdir(), "repo-"));
console.log(`Created temp directory: ${tempDir}`);
try {
// Clone repository
metadata.set("status", "Cloning repository...");
metadata.set("progress", 30);
console.log(`Cloning repository: ${repoName}`);
// Shallow clone repo (depth=1, single branch)
const cloneCmd =
`git clone --depth=1 --single-branch "${repoUrl}" "${tempDir}/repo"`;
try {
await execAsync(cloneCmd, {
timeout: 120000, // 2 minute timeout for clone
signal: abortController.signal,
});
console.log(`Successfully cloned ${repoName}`);
} catch (cloneError: any) {
// Cleanup temp dir on clone failure
await rm(tempDir, { recursive: true, force: true });
if (
cloneError.code === 128 || cloneError.stderr?.includes("not found")
) {
throw new Error(
`Failed to clone repository. It may be private, not exist, or the URL is invalid.`,
);
}
throw new Error(`Clone failed: ${cloneError.message}`);
}
metadata.set("status", "Analyzing codebase...");
metadata.set("progress", 50);
// Get repository size for warning
const { stdout: sizeOutput } = await execAsync(
`du -sh "${tempDir}/repo" | cut -f1`,
{ signal: abortController.signal },
);
metadata.set("repoSize", sizeOutput.trim());
// Prepare the prompt for Claude
const systemPrompt =
`You are analyzing the ${repoName} repository that has been cloned to your current working directory.
Your task is to answer the user's question about this repository. Use the available tools (Bash, Read, Grep, Glob) to explore the codebase as needed, but DO NOT narrate your exploration process. The user cannot see your tool usage.
After exploring the repository, provide:
1. A brief 2-3 sentence overview of what this repository contains and its main purpose
2. A detailed answer to the user's specific question
User's question: ${question}
Important: Only include your final analysis in your response. Do not include phrases like "Let me explore", "Let me check", "Now let me examine", etc. The user only wants to see the final answer.`;
// Use Claude Agent SDK to analyze the repository
const result = query({
prompt: systemPrompt,
options: {
model: "claude-sonnet-4-20250514",
cwd: join(tempDir, "repo"),
maxTurns: 30,
permissionMode: "acceptEdits",
abortController,
includePartialMessages: true, // Enable incremental text streaming
allowedTools: [
// "Bash",
// "Glob",
"Grep",
"Read",
// Not allowing Edit/Write since we're just analyzing
],
},
});
metadata.set("status", "Generating response...");
metadata.set("progress", 80);
// Stream text using writer API
const { waitUntilComplete } = agentStream.writer({
execute: async ({ write }) => {
for await (const message of result) {
// During tool use phase - update metadata with tool names
if (message.type === "assistant" && message.message?.content) {
const toolNames: string[] = [];
for (const block of message.message.content) {
if (block.type === "tool_use") {
toolNames.push(block.name);
}
}
if (toolNames.length > 0) {
metadata.set("status", `Analyzing: ${toolNames.join(", ")}`);
}
}
// Handle incremental text deltas from stream events
if (message.type === "stream_event") {
const event = message.event;
if (
event.type === "content_block_delta" &&
event.delta.type === "text_delta"
) {
metadata.set("status", "Streaming response...");
write(event.delta.text);
}
}
}
},
});
await waitUntilComplete();
metadata.set("status", "Completed");
metadata.set("progress", 100);
// Cleanup temp directory
console.log("[analyze-repo] Cleaning up temp directory...");
try {
await rm(tempDir, { recursive: true, force: true });
console.log("[analyze-repo] Cleanup successful");
} catch (e) {
console.error("[analyze-repo] Cleanup error:", e);
}
return {
repoName,
question,
status: "completed",
};
} catch (error: any) {
// Ensure cleanup on any error
try {
await rm(tempDir, { recursive: true, force: true });
} catch (e) {
console.error("[analyze-repo] Error cleanup failed:", e);
}
metadata.set("status", "Failed");
metadata.set("error", error.message);
// Stream error message as plain text
agentStream.writer({
execute: async ({ write }) => {
write(`Error: ${error.message}`);
},
});
throw error;
}
},
});