-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathindex.js
More file actions
415 lines (381 loc) · 14.2 KB
/
Copy pathindex.js
File metadata and controls
415 lines (381 loc) · 14.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
(() => {
const form = document.getElementById("submit-form");
const btn = document.getElementById("submit-btn");
const banner = document.getElementById("error-banner");
const prEl = document.getElementById("pr");
const commentEl = document.getElementById("comment");
const providerEl = document.getElementById("llm-provider");
const modelEl = document.getElementById("llm-model");
const modelSelectEl = document.getElementById("llm-model-select");
const baseUrlEl = document.getElementById("llm-base-url");
const customBaseRow = document.getElementById("custom-base-row");
const providerHint = document.getElementById("provider-hint");
const jobsSection = document.getElementById("jobs-section");
const jobsTbody = document.getElementById("jobs-tbody");
const jobsCount = document.getElementById("jobs-count");
const LLM_PREF_COOKIE = "serge_llm_prefs";
// Per-provider default model. Seeded with a hardcoded fallback for
// Anthropic so the form still works if /llm-options can't be reached;
// loadLlmOptions() overwrites entries with whatever the server reports
// (which folds in cfg.llm_model for the system-default provider).
const providerDefaultModels = {
anthropic: "claude-opus-4-6",
};
const PRESETS = {
"first-pass":
"@askserge first-pass review. Flag clear correctness, security, and " +
"API-shape problems; skip style nits and speculative concerns. " +
"Use the browse tools to verify any claim before flagging it.",
"new-model":
"@askserge this PR adds a new model. Verify the modular file structure, " +
"tokenizer/config/processor wiring, that tests exist and cover the " +
"model meaningfully, and that the docs entry is in place. Be strict " +
"about consistency with sibling model implementations — use the " +
"browse tools to compare.",
"bugfix":
"@askserge this is a bug fix. Verify the change actually addresses the " +
"root cause (not just the symptom), confirm a regression test was " +
"added (or call out clearly that one is missing), and flag any " +
"unrelated changes that slipped in.",
"docs":
"@askserge this is a documentation change. Focus on accuracy, clarity, " +
"and whether code samples are runnable. Ignore changes outside of " +
"docs and docstrings; do not nit on prose style unless it's actively " +
"misleading.",
};
function showError(msg) {
banner.textContent = msg;
banner.classList.add("visible");
}
async function errorMessage(r) {
const body = await r.text();
try {
const parsed = JSON.parse(body);
if (typeof parsed.detail === "string") return `${r.status}: ${parsed.detail}`;
} catch {
// Fall through to the raw body.
}
return `${r.status}: ${body}`;
}
function cookieOptions() {
const secure = window.location.protocol === "https:" ? "; Secure" : "";
return `Path=/; Max-Age=31536000; SameSite=Lax${secure}`;
}
function readCookie(name) {
const prefix = `${name}=`;
for (const part of document.cookie.split(";")) {
const item = part.trim();
if (item.startsWith(prefix)) return item.slice(prefix.length);
}
return "";
}
function loadSavedLlmPrefs() {
const raw = readCookie(LLM_PREF_COOKIE);
if (!raw) return null;
try {
const parsed = JSON.parse(decodeURIComponent(raw));
return parsed && typeof parsed === "object" ? parsed : null;
} catch {
return null;
}
}
function saveLlmPrefs() {
const prefs = {
provider: providerEl.value,
model: modelEl.value.trim(),
base_url: baseUrlEl.value.trim(),
};
document.cookie = `${LLM_PREF_COOKIE}=${encodeURIComponent(JSON.stringify(prefs))}; ${cookieOptions()}`;
}
function applySavedLlmPrefs() {
const prefs = loadSavedLlmPrefs();
if (!prefs) return;
if (["hf", "openai", "anthropic", "custom"].includes(prefs.provider)) {
providerEl.value = prefs.provider;
}
if (typeof prefs.model === "string") modelEl.value = prefs.model;
if (typeof prefs.base_url === "string") baseUrlEl.value = prefs.base_url;
}
function updateProviderFields() {
const isCustom = providerEl.value === "custom";
customBaseRow.style.display = isCustom ? "" : "none";
baseUrlEl.required = isCustom;
}
function applyProviderModelDefault() {
const defaultModel = providerDefaultModels[providerEl.value];
if (defaultModel && !modelEl.value.trim()) {
modelEl.value = defaultModel;
}
}
function resetModelToProviderDefault() {
// Used when the user changes the provider dropdown: always overwrite
// the model field, since the previous model name almost certainly
// doesn't exist on the new provider. Clears the field when the new
// provider has no registered default.
modelEl.value = providerDefaultModels[providerEl.value] || "";
}
// HF Router model catalogue, lazily fetched once and cached. null until
// loaded; [] when the endpoint is unreachable (we then fall back to the
// free-text input rather than an empty dropdown).
let hfModels = null;
async function ensureHfModels() {
if (hfModels !== null) return hfModels;
try {
const r = await fetch("/llm-options/hf-models");
const data = r.ok ? await r.json() : {};
hfModels = Array.isArray(data.models) ? data.models : [];
} catch {
hfModels = [];
}
return hfModels;
}
function populateModelSelect() {
// The text input stays the source of truth for the submitted value;
// the dropdown mirrors it. Keep the current value selectable even if
// the router doesn't list it (e.g. a config default not yet live), so
// switching to HF never silently drops a configured model.
const current = modelEl.value.trim();
const options = [...(hfModels || [])];
if (current && !options.includes(current)) options.unshift(current);
modelSelectEl.replaceChildren();
for (const m of options) {
const opt = document.createElement("option");
opt.value = m;
opt.textContent = m;
modelSelectEl.appendChild(opt);
}
if (current && options.includes(current)) {
modelSelectEl.value = current;
} else if (options.length) {
modelSelectEl.value = options[0];
modelEl.value = options[0];
}
}
// Show a dropdown of HF Router models when the HF provider is selected;
// every other provider keeps the free-text input. Falls back to the text
// input when the model list can't be fetched.
async function updateModelControl() {
if (providerEl.value === "hf") {
const models = await ensureHfModels();
// Guard against the provider changing while the fetch was in flight.
if (providerEl.value === "hf" && models.length) {
populateModelSelect();
modelSelectEl.style.display = "";
modelEl.style.display = "none";
modelEl.required = false;
return;
}
}
modelSelectEl.style.display = "none";
modelEl.style.display = "";
modelEl.required = true;
}
function ingestProviderDefaults(providers) {
if (!Array.isArray(providers)) return;
for (const p of providers) {
if (
p &&
typeof p.id === "string" &&
typeof p.default_model === "string" &&
p.default_model
) {
providerDefaultModels[p.id] = p.default_model;
}
}
}
async function loadLlmOptions() {
try {
const r = await fetch("/llm-options");
if (!r.ok) return;
const data = await r.json();
providerEl.value = data.default_provider || "hf";
modelEl.value = data.default_model || "";
baseUrlEl.value = data.custom_base_url || "";
ingestProviderDefaults(data.providers);
applySavedLlmPrefs();
applyProviderModelDefault();
updateProviderFields();
await updateModelControl();
} catch {
applySavedLlmPrefs();
applyProviderModelDefault();
updateProviderFields();
await updateModelControl();
}
}
function relTime(epochSeconds) {
const delta = Date.now() / 1000 - epochSeconds;
if (delta < 60) return `${Math.floor(delta)}s ago`;
if (delta < 3600) return `${Math.floor(delta / 60)}m ago`;
if (delta < 86400) return `${Math.floor(delta / 3600)}h ago`;
return `${Math.floor(delta / 86400)}d ago`;
}
function renderJobs(jobs) {
jobsTbody.replaceChildren();
jobsCount.textContent = jobs.length ? `(${jobs.length})` : "";
if (!jobs.length) {
jobsSection.style.display = "none";
return;
}
for (const j of jobs) {
const tr = document.createElement("tr");
const tdStatus = document.createElement("td");
const badge = document.createElement("span");
badge.className = `status-badge ${j.status}`;
badge.textContent = j.status;
tdStatus.appendChild(badge);
const tdPr = document.createElement("td");
const link = document.createElement("a");
link.href = j.url;
link.textContent = `${j.owner}/${j.repo}#${j.number}`;
tdPr.appendChild(link);
const tdAgo = document.createElement("td");
tdAgo.className = "ago";
tdAgo.textContent = relTime(j.created_at);
tdAgo.title = new Date(j.created_at * 1000).toLocaleString();
tr.appendChild(tdStatus);
tr.appendChild(tdPr);
tr.appendChild(tdAgo);
jobsTbody.appendChild(tr);
}
jobsSection.style.display = "";
}
async function loadJobs() {
try {
const r = await fetch("/reviews");
if (!r.ok) return;
const data = await r.json();
renderJobs(data.jobs || []);
} catch {
// Non-fatal — the form still works.
}
}
// Mirrors the server-side _parse_pr_ref shapes: full GitHub URL,
// "owner/repo#NN", or "owner/repo/pull/NN". Returns null when the
// string isn't yet a parseable PR reference (e.g. half-typed).
function parseOwnerRepo(raw) {
const s = (raw || "").trim();
if (!s) return null;
const name = "[A-Za-z0-9._-]+";
let m;
m = s.match(new RegExp(`github\\.com/(${name})/(${name})/(?:pull|pulls)/\\d+`));
if (m) return { owner: m[1], repo: m[2] };
m = s.match(new RegExp(`^(${name})/(${name})#\\d+`));
if (m) return { owner: m[1], repo: m[2] };
m = s.match(new RegExp(`^(${name})/(${name})/pull/\\d+`));
if (m) return { owner: m[1], repo: m[2] };
return null;
}
let lookupTimer = 0;
let lastLookupKey = "";
function scheduleProviderLookup() {
clearTimeout(lookupTimer);
lookupTimer = setTimeout(runProviderLookup, 250);
}
async function runProviderLookup() {
const parsed = parseOwnerRepo(prEl.value);
if (!parsed) {
providerHint.textContent = "";
lastLookupKey = "";
return;
}
const key = `${parsed.owner}/${parsed.repo}`.toLowerCase();
if (key === lastLookupKey) return;
lastLookupKey = key;
try {
const qs = new URLSearchParams({ owner: parsed.owner, repo: parsed.repo });
const r = await fetch(`/reviews/lookup-provider?${qs}`);
if (!r.ok) {
providerHint.textContent = "";
return;
}
const data = await r.json();
if (!data.match) {
providerHint.textContent = `No provider config matches ${parsed.owner}/${parsed.repo}. Add one at /admin or your submission will be refused.`;
return;
}
applyMatchedConfig(data.match, parsed);
} catch {
// Non-fatal — the user can still submit; the server will reject
// with the same message if no config matches.
providerHint.textContent = "";
}
}
function applyMatchedConfig(match, parsed) {
providerEl.value = match.provider;
if (match.default_model) {
modelEl.value = match.default_model;
}
if (match.provider === "custom" && match.api_base) {
baseUrlEl.value = match.api_base;
}
updateProviderFields();
updateModelControl();
const modelPart = match.default_model ? ` · model ${match.default_model}` : "";
const scope =
match.repo_pattern && match.repo_pattern !== `${parsed.owner}/${parsed.repo}`
? ` (via ${match.repo_pattern})`
: "";
providerHint.textContent = `Using ${match.provider}${modelPart}${scope}.`;
}
form.addEventListener("submit", async (e) => {
e.preventDefault();
btn.disabled = true;
banner.classList.remove("visible");
const pr = document.getElementById("pr").value.trim();
const comment = document.getElementById("comment").value.trim();
const llm_provider = providerEl.value;
const llm_model = modelEl.value.trim();
const llm_base_url = baseUrlEl.value.trim();
try {
saveLlmPrefs();
const r = await fetch("/reviews", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ pr, comment, llm_provider, llm_model, llm_base_url }),
});
if (!r.ok) {
throw new Error(await errorMessage(r));
}
const { url } = await r.json();
window.location.href = url;
} catch (err) {
showError(`Could not start review — ${err.message}`);
btn.disabled = false;
}
});
for (const b of document.querySelectorAll("button.preset")) {
b.addEventListener("click", () => {
const text = PRESETS[b.dataset.preset];
if (!text) return;
commentEl.value = text;
commentEl.focus();
});
}
providerEl.addEventListener("change", () => {
updateProviderFields();
resetModelToProviderDefault();
updateModelControl();
saveLlmPrefs();
});
modelEl.addEventListener("change", saveLlmPrefs);
modelSelectEl.addEventListener("change", () => {
modelEl.value = modelSelectEl.value;
saveLlmPrefs();
});
baseUrlEl.addEventListener("change", saveLlmPrefs);
// Auto-fill provider/model from the matching DB config whenever the
// PR field changes. Debounced so we don't hit the endpoint on every
// keystroke, and short-circuited by lastLookupKey when the
// owner/repo hasn't actually changed.
prEl.addEventListener("input", scheduleProviderLookup);
prEl.addEventListener("change", runProviderLookup);
loadLlmOptions();
loadJobs();
// Catch the case where the input already has a value at load time
// (e.g. browser autofill or a prefilled paste).
runProviderLookup();
// Soft-refresh every 5s so running reviews tick over to "done" /
// "published" without the user having to reload.
setInterval(loadJobs, 5000);
})();