-
Notifications
You must be signed in to change notification settings - Fork 27
347 lines (309 loc) · 13.2 KB
/
auto-advance-ball.yml
File metadata and controls
347 lines (309 loc) · 13.2 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
name: "Automation: Advance the Ball (6h)"
on:
schedule:
- cron: "0 */6 * * *"
workflow_dispatch:
inputs:
llm_provider:
description: "LLM provider"
required: false
default: ""
type: choice
options:
- ""
- openai
- gemini
- anthropic
llm_model:
description: "Model name (provider-specific)"
required: false
default: ""
type: string
permissions:
contents: read
issues: write
concurrency:
group: advance-ball-${{ github.repository }}
cancel-in-progress: true
jobs:
advance:
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Gather repo context
shell: bash
run: |
set -euo pipefail
{
echo "Repository: ${GITHUB_REPOSITORY}"
echo "Run URL: ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"
echo "Ref: ${GITHUB_REF_NAME}"
echo "SHA: ${GITHUB_SHA}"
echo ""
echo "Top-level files/directories:"
ls -la
echo ""
echo "Recent commits (last 20):"
git log --oneline -20 2>/dev/null || true
echo ""
echo "Recent changed files (best-effort):"
git diff --name-only HEAD~20..HEAD 2>/dev/null | head -n 200 || true
echo ""
echo "Recent open PRs/issues pointers:"
echo "- See: ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/pulls"
echo "- See: ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/issues"
} > /tmp/automation_context.txt
if [ -f "AUTOMATION.txt" ]; then
cat "AUTOMATION.txt" > /tmp/automation_guidance.txt
else
: > /tmp/automation_guidance.txt
fi
- name: Generate direction and write/update issue
uses: actions/github-script@v7
env:
LLM_PROVIDER: ${{ (github.event_name == 'workflow_dispatch' && github.event.inputs.llm_provider) || vars.AUTOMATION_LLM_PROVIDER || vars.LLM_PROVIDER || 'openai' }}
LLM_MODEL: ${{ (github.event_name == 'workflow_dispatch' && github.event.inputs.llm_model) || vars.AUTOMATION_LLM_MODEL || vars.LLM_MODEL || 'gpt-5.2' }}
OPENAI_BASE_URL: ${{ vars.OPENAI_BASE_URL || 'https://api.openai.com/v1' }}
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }}
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require("fs");
const owner = context.repo.owner;
const repo = context.repo.repo;
const provider = (process.env.LLM_PROVIDER || "openai").trim().toLowerCase();
const model = (process.env.LLM_MODEL || "").trim();
if (!model) {
core.setFailed("LLM model is required (set vars.AUTOMATION_LLM_MODEL or workflow_dispatch input llm_model).");
return;
}
const contextText = fs.readFileSync("/tmp/automation_context.txt", "utf8");
const guidanceText = fs.readFileSync("/tmp/automation_guidance.txt", "utf8").trim();
const marker = "<!-- advance-ball -->";
const now = new Date().toISOString();
const defaultInstruction = [
"Analyze this repository and propose a concrete direction.",
"If there is no AUTOMATION.txt guidance, decide a direction and proceed by producing an actionable plan.",
"Document your direction, assumptions, risks, and next steps.",
"Prefer small, safe changes that increase reliability and reduce toil.",
"Include suggested tests and a short checklist for verifying progress.",
].join(" ");
const systemPrompt = [
"You are an expert software engineer and technical lead.",
"You are producing an automation direction update for a repository.",
"Be specific, actionable, and concise.",
"Avoid hand-wavy advice; prefer concrete steps and file paths.",
].join(" ");
const userPromptParts = [];
userPromptParts.push(`Repository: ${owner}/${repo}`);
userPromptParts.push(`Timestamp: ${now}`);
userPromptParts.push("");
userPromptParts.push("Repository context:");
userPromptParts.push(contextText);
if (guidanceText) {
userPromptParts.push("");
userPromptParts.push("AUTOMATION.txt guidance (highest priority):");
userPromptParts.push(guidanceText.length > 6000 ? guidanceText.slice(0, 6000) + "\n...(truncated)..." : guidanceText);
} else {
userPromptParts.push("");
userPromptParts.push("No AUTOMATION.txt was found. Use this default instruction:");
userPromptParts.push(defaultInstruction);
}
userPromptParts.push("");
userPromptParts.push("Output format:");
userPromptParts.push("- Summary");
userPromptParts.push("- Direction (what and why)");
userPromptParts.push("- Plan (next 1-3 steps)");
userPromptParts.push("- Risks/unknowns");
userPromptParts.push("- Suggested tests");
const userPrompt = userPromptParts.join("\n");
async function callOpenAI({ apiKey, baseUrl, model, messages }) {
if (!apiKey) throw new Error("OPENAI_API_KEY is not set.");
const url = `${baseUrl.replace(new RegExp('/$'), "")}/chat/completions`;
const payload = { model, messages };
const gpt5Pattern = new RegExp('(gpt-?5|^o\\d|^o1)', 'i');
const isGpt5ish = gpt5Pattern.test(model);
if (isGpt5ish) {
payload.max_completion_tokens = 2048;
} else {
payload.max_tokens = 2048;
payload.temperature = 0.2;
}
const resp = await fetch(url, {
method: "POST",
headers: {
"Authorization": `Bearer ${apiKey}`,
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
});
if (!resp.ok) {
const text = await resp.text();
throw new Error(`OpenAI API error (${resp.status}): ${text}`);
}
const data = await resp.json();
const content = data?.choices?.[0]?.message?.content;
if (!content) throw new Error("OpenAI API returned no content.");
return content;
}
async function callGemini({ apiKey, model, prompt }) {
if (!apiKey) throw new Error("GEMINI_API_KEY is not set.");
const geminiModel = model || "gemini-1.5-pro";
const url = `https://generativelanguage.googleapis.com/v1beta/models/${encodeURIComponent(geminiModel)}:generateContent?key=${encodeURIComponent(apiKey)}`;
const payload = {
contents: [{ role: "user", parts: [{ text: prompt }] }],
generationConfig: { temperature: 0.2, maxOutputTokens: 2048 },
};
const resp = await fetch(url, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload),
});
if (!resp.ok) {
const text = await resp.text();
throw new Error(`Gemini API error (${resp.status}): ${text}`);
}
const data = await resp.json();
const parts = data?.candidates?.[0]?.content?.parts || [];
const text = parts.map(p => p.text || "").join("").trim();
if (!text) throw new Error("Gemini API returned no content.");
return text;
}
async function callAnthropic({ apiKey, model, system, prompt }) {
if (!apiKey) throw new Error("ANTHROPIC_API_KEY is not set.");
const anthropicModel = model || "claude-3-5-sonnet-latest";
const url = "https://api.anthropic.com/v1/messages";
const payload = {
model: anthropicModel,
max_tokens: 2048,
temperature: 0.2,
system,
messages: [{ role: "user", content: prompt }],
};
const resp = await fetch(url, {
method: "POST",
headers: {
"x-api-key": apiKey,
"anthropic-version": "2023-06-01",
"content-type": "application/json",
},
body: JSON.stringify(payload),
});
if (!resp.ok) {
const text = await resp.text();
throw new Error(`Anthropic API error (${resp.status}): ${text}`);
}
const data = await resp.json();
const text = (data?.content || []).map(p => p.text || "").join("").trim();
if (!text) throw new Error("Anthropic API returned no content.");
return text;
}
let direction = "";
try {
if (provider === "openai") {
direction = await callOpenAI({
apiKey: process.env.OPENAI_API_KEY,
baseUrl: process.env.OPENAI_BASE_URL,
model,
messages: [
{ role: "system", content: systemPrompt },
{ role: "user", content: userPrompt },
],
});
} else if (provider === "gemini") {
direction = await callGemini({
apiKey: process.env.GEMINI_API_KEY,
model,
prompt: `${systemPrompt}\n\n${userPrompt}`,
});
} else if (provider === "anthropic") {
direction = await callAnthropic({
apiKey: process.env.ANTHROPIC_API_KEY,
model,
system: systemPrompt,
prompt: userPrompt,
});
} else {
throw new Error(`Unsupported provider: ${provider}`);
}
} catch (e) {
core.setFailed(e.message || String(e));
return;
}
async function ensureLabel(name, color, description) {
try {
await github.rest.issues.getLabel({ owner, repo, name });
return;
} catch (e) {
// Not found -> create
}
await github.rest.issues.createLabel({
owner,
repo,
name,
color,
description,
});
}
const labelName = "automation";
try {
await ensureLabel(labelName, "5319E7", "Automation-generated direction and planning");
} catch (e) {
core.info(`Could not ensure label '${labelName}': ${e.message || e}`);
}
const issueTitle = "Automation: Direction";
const newBody = [
marker,
`Last generated: ${now}`,
`Provider: ${provider}`,
`Model: ${model}`,
"",
direction,
].join("\n");
// Find existing open issue with our marker
let existingIssueNumber = null;
try {
const search = await github.rest.search.issuesAndPullRequests({
q: `repo:${owner}/${repo} is:issue is:open in:body \"${marker}\"`,
per_page: 10,
});
const first = (search.data.items || [])[0];
if (first && typeof first.number === "number") existingIssueNumber = first.number;
} catch (e) {
// Search can be delayed; fall back to listing recent issues
}
if (!existingIssueNumber) {
const recent = await github.rest.issues.listForRepo({
owner,
repo,
state: "open",
per_page: 50,
});
const found = (recent.data || []).find(i => typeof i.body === "string" && i.body.includes(marker));
if (found) existingIssueNumber = found.number;
}
if (existingIssueNumber) {
await github.rest.issues.update({
owner,
repo,
issue_number: existingIssueNumber,
title: issueTitle,
body: newBody,
});
core.info(`Updated issue #${existingIssueNumber}`);
} else {
const created = await github.rest.issues.create({
owner,
repo,
title: issueTitle,
body: newBody,
labels: [labelName],
});
core.info(`Created issue #${created.data.number}`);
}