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
|
import {
getAnthropic,
MODEL_SONNET,
extractText,
isAiAvailable,
} from "./ai-client";
import {
getReadme,
getTree,
getBlob,
listCommits,
getDefaultBranch,
} from "../git/repository";
export interface ChatMessage {
role: "user" | "assistant";
content: string;
}
export interface ChatResponse {
reply: string;
citedFiles: string[];
}
async function buildRepoContext(
owner: string,
repo: string,
mentionedFiles: string[]
): Promise<{ context: string; files: string[] }> {
const branch = (await getDefaultBranch(owner, repo)) || "main";
const citedFiles: string[] = [];
const parts: string[] = [];
parts.push(`# Repository: ${owner}/${repo}\nDefault branch: ${branch}\n`);
const readme = await getReadme(owner, repo, branch);
if (readme) {
parts.push(`## README\n${readme.slice(0, 8000)}\n`);
}
const tree = await getTree(owner, repo, branch);
if (tree.length > 0) {
parts.push(
`## Top-level files\n${tree
.slice(0, 60)
.map((e) => `- ${e.type === "tree" ? e.name + "/" : e.name}`)
.join("\n")}\n`
);
}
const commits = await listCommits(owner, repo, branch, 15);
if (commits.length > 0) {
parts.push(
`## Recent commits\n${commits
.map((c) => `- ${c.sha.slice(0, 7)} ${c.message.split("\n")[0]} — ${c.author}`)
.join("\n")}\n`
);
}
for (const file of mentionedFiles.slice(0, 8)) {
try {
const blob = await getBlob(owner, repo, branch, file);
if (blob && !blob.isBinary) {
citedFiles.push(file);
parts.push(`## File: ${file}\n\`\`\`\n${blob.content.slice(0, 12000)}\n\`\`\`\n`);
}
} catch {
}
}
return { context: parts.join("\n"), files: citedFiles };
}
function extractFileMentions(text: string): string[] {
const matches = text.match(/@([A-Za-z0-9._/-]+\.[A-Za-z0-9]+)/g);
if (!matches) return [];
return Array.from(new Set(matches.map((m) => m.slice(1))));
}
export async function chat(
owner: string,
repo: string | null,
history: ChatMessage[],
userMessage: string
): Promise<ChatResponse> {
if (!isAiAvailable()) {
return {
reply:
"AI chat is not available — the server needs an ANTHROPIC_API_KEY to be configured.",
citedFiles: [],
};
}
const client = getAnthropic();
const mentioned = extractFileMentions(userMessage);
const { context: repoContext, files } = repo
? await buildRepoContext(owner, repo, mentioned)
: { context: "", files: [] };
const system = repo
? `You are GlueCron's AI assistant. You help developers understand and work with the repository ${owner}/${repo}. Be concise, accurate, and reference specific files and line numbers when relevant. If the user asks about something not in your context, say so.`
: `You are GlueCron's AI assistant. You help developers navigate the GlueCron platform — a git host with green-gate enforcement, AI code review, and auto-repair. Keep answers concise.`;
const messages: ChatMessage[] = [
...history,
{
role: "user",
content: repoContext
? `${repoContext}\n\n---\n\nUser question: ${userMessage}`
: userMessage,
},
];
const response = await client.messages.create({
model: MODEL_SONNET,
max_tokens: 2048,
system,
messages: messages.map((m) => ({ role: m.role, content: m.content })),
});
return {
reply: extractText(response).trim(),
citedFiles: files,
};
}
export async function explainFile(
owner: string,
repo: string,
filePath: string,
content: string
): Promise<string> {
if (!isAiAvailable()) {
return "AI explanations are not available — server needs ANTHROPIC_API_KEY.";
}
const client = getAnthropic();
const message = await client.messages.create({
model: MODEL_SONNET,
max_tokens: 1024,
messages: [
{
role: "user",
content: `Explain this file from ${owner}/${repo} in plain English.
Structure:
1. **Purpose** — one sentence
2. **Key exports / APIs** — bulleted list
3. **How it works** — 2-4 sentences
4. **Gotchas / caveats** — only if any
Be concise.
File: ${filePath}
\`\`\`
${content.slice(0, 40000)}
\`\`\``,
},
],
});
return extractText(message).trim();
}
|