co-maintainer 0.6.1 → 0.6.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. package/THIRD_PARTY_NOTICES +161 -0
  2. package/dist/package.json +4 -2
  3. package/dist/src/ai/batch.js +7 -0
  4. package/dist/src/ai/oauth/ids.d.ts +10 -0
  5. package/dist/src/ai/oauth/ids.js +10 -0
  6. package/dist/src/ai/oauth/registry.d.ts +33 -0
  7. package/dist/src/ai/oauth/registry.js +40 -0
  8. package/dist/src/ai/oauth/sign-in-with-chatgpt.d.ts +56 -0
  9. package/dist/src/ai/oauth/sign-in-with-chatgpt.js +552 -0
  10. package/dist/src/ai/openai.d.ts +18 -1
  11. package/dist/src/ai/openai.js +68 -5
  12. package/dist/src/ai/provider.d.ts +3 -0
  13. package/dist/src/ai/provider.js +11 -2
  14. package/dist/src/cli/args.js +61 -21
  15. package/dist/src/cli/commands/oauth.d.ts +1 -0
  16. package/dist/src/cli/commands/oauth.js +73 -0
  17. package/dist/src/cli/commands/registry.js +79 -3
  18. package/dist/src/cli/commands/serve.js +31 -3
  19. package/dist/src/cli/commands/set.js +54 -0
  20. package/dist/src/cli/main.js +5 -0
  21. package/dist/src/cli/review_args.js +5 -0
  22. package/dist/src/config.d.ts +14 -0
  23. package/dist/src/github/client.js +13 -0
  24. package/dist/src/github/collect.d.ts +2 -2
  25. package/dist/src/github/collect.js +60 -23
  26. package/dist/src/github/permissions.js +8 -0
  27. package/dist/src/knowledge/types.d.ts +3 -4
  28. package/dist/src/pr/reviewer.js +5 -4
  29. package/dist/src/server/api/jobs.js +8 -1
  30. package/dist/src/server/api/repos.js +73 -11
  31. package/dist/src/server/api/settings.js +6 -0
  32. package/dist/src/server/app.d.ts +5 -0
  33. package/dist/src/server/app.js +7 -0
  34. package/dist/src/server/http.d.ts +3 -0
  35. package/dist/src/server/http.js +5 -0
  36. package/dist/src/server/pages/activity.js +412 -19
  37. package/dist/src/server/pages/add_repo.js +6 -1
  38. package/dist/src/server/pages/analytics.js +99 -31
  39. package/dist/src/server/pages/catalog.d.ts +1 -0
  40. package/dist/src/server/pages/catalog.js +145 -0
  41. package/dist/src/server/pages/client.d.ts +1 -1
  42. package/dist/src/server/pages/client.js +1 -1
  43. package/dist/src/server/pages/home.d.ts +14 -4
  44. package/dist/src/server/pages/home.js +83 -49
  45. package/dist/src/server/pages/icons.d.ts +3 -0
  46. package/dist/src/server/pages/icons.js +3 -0
  47. package/dist/src/server/pages/knowledge.js +10 -5
  48. package/dist/src/server/pages/layout.d.ts +19 -3
  49. package/dist/src/server/pages/layout.js +156 -33
  50. package/dist/src/server/pages/pr.js +4 -3
  51. package/dist/src/server/pages/rate_limit_waits.d.ts +5 -0
  52. package/dist/src/server/pages/rate_limit_waits.js +74 -0
  53. package/dist/src/server/pages/repo.js +100 -66
  54. package/dist/src/server/pages/repo_prs.js +4 -3
  55. package/dist/src/server/pages/repo_remote.js +4 -3
  56. package/dist/src/server/pages/repo_settings.d.ts +2 -2
  57. package/dist/src/server/pages/repo_settings.js +420 -46
  58. package/dist/src/server/pages/router.d.ts +5 -0
  59. package/dist/src/server/pages/router.js +82 -31
  60. package/dist/src/server/pages/settings.d.ts +10 -1
  61. package/dist/src/server/pages/settings.js +351 -207
  62. package/dist/src/server/pages/setup.d.ts +2 -5
  63. package/dist/src/server/pages/setup.js +21 -23
  64. package/dist/src/server/pages/styles.d.ts +1 -1
  65. package/dist/src/server/pages/styles.js +1 -1
  66. package/dist/src/server/pages/ui.d.ts +117 -0
  67. package/dist/src/server/pages/ui.js +166 -0
  68. package/dist/src/services/clear.js +2 -0
  69. package/dist/src/services/cli_repos.d.ts +2 -0
  70. package/dist/src/services/cli_repos.js +50 -0
  71. package/dist/src/services/concurrency.d.ts +39 -0
  72. package/dist/src/services/concurrency.js +88 -0
  73. package/dist/src/services/dashboard.d.ts +43 -1
  74. package/dist/src/services/dashboard.js +10 -2
  75. package/dist/src/services/jobs.js +5 -3
  76. package/dist/src/services/nav_counts.d.ts +8 -0
  77. package/dist/src/services/nav_counts.js +19 -0
  78. package/dist/src/services/rate_limit.d.ts +33 -0
  79. package/dist/src/services/rate_limit.js +78 -0
  80. package/dist/src/services/repo_list.d.ts +19 -0
  81. package/dist/src/services/repo_list.js +38 -0
  82. package/dist/src/services/review.d.ts +23 -1
  83. package/dist/src/services/review.js +104 -5
  84. package/dist/src/services/search.d.ts +13 -0
  85. package/dist/src/services/search.js +72 -0
  86. package/dist/src/services/setup.js +17 -16
  87. package/dist/src/services/setup_checklist.d.ts +1 -1
  88. package/dist/src/services/setup_checklist.js +26 -47
  89. package/dist/src/store/cache_db.js +1 -1
  90. package/dist/src/store/findings.d.ts +1 -1
  91. package/dist/src/store/findings.js +3 -3
  92. package/dist/src/store/migrations.js +7 -0
  93. package/dist/src/store/repos.d.ts +5 -0
  94. package/dist/src/store/repos.js +12 -0
  95. package/dist/src/store/reviews.d.ts +12 -1
  96. package/dist/src/store/reviews.js +34 -2
  97. package/dist/src/store/rows.d.ts +6 -0
  98. package/dist/src/types.d.ts +5 -1
  99. package/package.json +4 -2
@@ -1,9 +1,37 @@
1
1
  import { html, layout, skSlot, text } from "./layout.js";
2
2
  import { aiConfigured, resolveAppPrivateKey, resolvedAiToken, } from "../../config.js";
3
3
  import { VERSION } from "../../version.js";
4
+ import { appDbDir } from "../../store/app_db.js";
4
5
  import { TEMPLATE_INFO } from "../../services/review_policy.js";
5
6
  import { defaultAppName, manifestBlockedReason, } from "../../github/app_manifest.js";
6
7
  const SOURCE_URL = "https://github.com/GroophyLifefor/co-maintainer";
8
+ export const SETTINGS_SECTIONS = [
9
+ "ai",
10
+ "github",
11
+ "defaults",
12
+ "remote",
13
+ "server",
14
+ "sign-in",
15
+ "danger",
16
+ ];
17
+ const HEADING = {
18
+ ai: "AI models",
19
+ github: "GitHub",
20
+ defaults: "Review defaults",
21
+ remote: "Remote access",
22
+ server: "Server",
23
+ "sign-in": "Sign-in",
24
+ danger: "Danger zone",
25
+ };
26
+ const NAV_HREF = {
27
+ ai: "/settings/ai",
28
+ github: "/settings/github",
29
+ defaults: "/settings/defaults",
30
+ remote: "/settings/remote",
31
+ server: "/settings/server",
32
+ "sign-in": "/settings/sign-in",
33
+ danger: "/settings/danger",
34
+ };
7
35
  /** Which key the jobs will use. The environment wins over the saved key, the
8
36
  * same order `resolvedAiToken` follows, so the hint must check it first. */
9
37
  function keyHint(config) {
@@ -15,68 +43,65 @@ function keyHint(config) {
15
43
  return "Using the key from the server's environment";
16
44
  return config.token ? "A key is saved" : "No key saved";
17
45
  }
18
- export function renderSettings(username, config, webhookUrl, baseUrl) {
19
- const aiOk = aiConfigured(config);
20
- const ghOk = Boolean(config.auth === "gh" || (config.auth === "pat" && config.githubPat));
21
- const appOk = Boolean(config.githubAppId && resolveAppPrivateKey(config));
22
- let host = "localhost";
46
+ function hostOf(baseUrl) {
23
47
  try {
24
- host = new URL(baseUrl).hostname || host;
48
+ return new URL(baseUrl).hostname || "localhost";
25
49
  }
26
50
  catch {
27
- // baseUrl is built by the router from a valid Request URL; keep the
28
- // fallback rather than blanking the default App name.
51
+ return "localhost";
29
52
  }
30
- const manifestBlock = manifestBlockedReason(webhookUrl);
31
- return html(layout({
32
- title: "Settings · co-maintainer",
33
- username,
34
- active: "settings",
35
- body: `<div class="wrap side">
36
- <aside>
37
- <p class="lbl">Settings</p>
38
- <nav>
39
- <a href="/settings" class="on">Models and keys</a>
40
- <a href="#github">GitHub</a>
41
- <a href="#defaults">Defaults</a>
42
- <a href="#server">Server</a>
43
- <a href="#remote">Remote review</a>
44
- <a href="#access">Access</a>
45
- <a href="#about">About</a>
46
- </nav>
47
- </aside>
48
- <div>
49
- <div class="pagehead"><div><h1>Settings</h1>
50
- <p class="lead">Used by every repository unless it overrides them.</p></div></div>
51
- <div class="card" id="ai" data-async>
53
+ }
54
+ /** `sk-or-v1-••••••••428`: the provider prefix and the last three
55
+ * characters, enough to tell keys apart without showing one. A key too short
56
+ * to keep both safely shows only dots. */
57
+ function maskKey(key) {
58
+ const prefix = /^(?:[a-z0-9]+-){1,3}/i.exec(key)?.[0] ?? "";
59
+ if (prefix.length > 10 || key.length < prefix.length + 12)
60
+ return "••••••••";
61
+ return `${prefix}••••••••${key.slice(-3)}`;
62
+ }
63
+ function aiCard(config) {
64
+ const aiOk = aiConfigured(config);
65
+ return `<div class="card" id="ai" data-async>
52
66
  ${skSlot()}
53
67
  <div class="hd"><h2>Models and API key</h2>
54
68
  <span class="st ${aiOk ? "ok" : "warn"}" style="margin-left:auto">${aiOk ? "Configured" : "Not set"}</span></div>
55
69
  <div class="bd">
56
- <div class="field"><label>Provider</label>
57
- <select id="ai">
70
+ <div class="field"><label for="ai-provider">Provider</label>
71
+ <select id="ai-provider">
58
72
  <option value="none"${!config.ai || config.ai === "none" ? " selected" : ""}>None</option>
59
73
  <option value="openrouter"${config.ai === "openrouter" ? " selected" : ""}>OpenRouter</option>
60
74
  <option value="openai"${config.ai === "openai" ? " selected" : ""}>OpenAI</option>
61
75
  <option value="anthropic"${config.ai === "anthropic" ? " selected" : ""}>Anthropic</option>
62
76
  <option value="opencode-zen"${config.ai === "opencode-zen" ? " selected" : ""}>OpenCode Zen</option>
63
77
  <option value="opencode-go"${config.ai === "opencode-go" ? " selected" : ""}>OpenCode Go</option>
64
- </select></div>
65
- <div class="field"><label>API key</label>
66
- <input id="token" type="password" placeholder="Leave blank to keep the current key">
67
- <div class="hint">${keyHint(config)}</div></div>
78
+ </select>
79
+ <div class="cli"><code>--ai=${text(config.ai ?? "none")}</code></div></div>
80
+ <div class="field"><label for="token">API key</label>${config.token
81
+ ? `<div class="actions-row"><span class="inp mono key-mask">${text(maskKey(config.token))}</span><button type="button" class="btn gh sm" id="replace-key">Replace</button></div>`
82
+ : ""}
83
+ <input id="token" type="password" placeholder="${config.token ? "Paste the new key" : "Paste the API key"}"${config.token ? " hidden" : ""}>
84
+ <div class="hint">${keyHint(config)}</div>
85
+ <div class="cli"><code>--ai-key=…</code></div></div>
68
86
  <div class="two" style="max-width:none">
69
- <div class="field"><label>High model</label>
87
+ <div class="field"><label for="high">Review model</label>
70
88
  <input id="high" value="${text(config.highModel ?? "")}">
71
- <div class="hint">Writes reviews and synthesizes the guides</div></div>
72
- <div class="field"><label>Low model</label>
89
+ <div class="hint">Writes reviews and synthesizes the guides. High model in the CLI.</div>
90
+ <div class="cli"><code>--high-model=…</code></div></div>
91
+ <div class="field"><label for="low">Extraction model</label>
73
92
  <input id="low" value="${text(config.lowModel ?? "")}">
74
- <div class="hint">Extracts facts from history during init and sync</div></div>
93
+ <div class="hint">Extracts facts from history during init and sync. A cheap model is enough. Low model in the CLI.</div>
94
+ <div class="cli"><code>--low-model=…</code></div></div>
75
95
  </div>
76
96
  </div>
77
97
  <div class="ft"><button class="primary" id="save-ai">Save</button></div>
78
- </div>
79
- <div class="card" id="github" data-async>
98
+ </div>`;
99
+ }
100
+ function githubCards(config, webhookUrl, host) {
101
+ const ghOk = Boolean(config.auth === "gh" || (config.auth === "pat" && config.githubPat));
102
+ const appOk = Boolean(config.githubAppId && resolveAppPrivateKey(config));
103
+ const manifestBlock = manifestBlockedReason(webhookUrl);
104
+ return `<div class="card" id="github" data-async>
80
105
  ${skSlot()}
81
106
  <div class="hd"><h2>GitHub</h2>
82
107
  <span class="st ${ghOk ? "ok" : "warn"}" style="margin-left:auto">${ghOk ? "Configured" : "Not set"}</span></div>
@@ -106,8 +131,8 @@ export function renderSettings(username, config, webhookUrl, baseUrl) {
106
131
  </div>
107
132
  <button class="btn" id="create-app">Create GitHub App</button>
108
133
  <div class="hint" style="margin:6px 0 0">${manifestBlock
109
- ? `${text(manifestBlock)}`
110
- : "Opens GitHub, then fills App ID, private key, webhook secret, and OAuth below for you."}</div>
134
+ ? `${text(manifestBlock)}`
135
+ : "Opens GitHub, then fills App ID, private key, webhook secret, and OAuth below for you."}</div>
111
136
  </div>
112
137
  <div class="two" style="max-width:none">
113
138
  <div class="field"><label>App ID</label>
@@ -127,8 +152,10 @@ Leave blank to keep the current key"></textarea></div>
127
152
  <input id="hook-secret" type="password" placeholder="Leave blank to keep the current secret"></div>
128
153
  </div>
129
154
  <div class="ft"><button class="primary" id="save-app">Save</button></div>
130
- </div>
131
- <div class="card" id="defaults" data-async>
155
+ </div>`;
156
+ }
157
+ function defaultsCard(config) {
158
+ return `<div class="card" id="defaults" data-async>
132
159
  ${skSlot()}
133
160
  <div class="hd"><h2>Defaults for new repositories</h2></div>
134
161
  <div class="bd">
@@ -146,19 +173,35 @@ Leave blank to keep the current key"></textarea></div>
146
173
  <option value=""${config.reviewPolicy === undefined ? " selected" : ""}>Everyone except drafts and bots (the simple switches)</option>
147
174
  ${TEMPLATE_INFO.map((item) => `<option value="${text(item.name)}"${config.reviewPolicy === item.name ? " selected" : ""}>${text(item.label)}</option>`).join("")}
148
175
  ${typeof config.reviewPolicy === "object"
149
- ? `<option value="__custom" selected>Custom rules from a file</option>`
150
- : ""}
176
+ ? `<option value="__custom" selected>Custom rules from a file</option>`
177
+ : ""}
151
178
  </select>
152
179
  <div class="hint" id="def-policy-desc">${text(typeof config.reviewPolicy === "string"
153
- ? (TEMPLATE_INFO.find((item) => item.name === config.reviewPolicy)
154
- ?.description ?? "")
155
- : "")}</div>
180
+ ? (TEMPLATE_INFO.find((item) => item.name === config.reviewPolicy)
181
+ ?.description ?? "")
182
+ : "")}</div>
156
183
  <div class="hint">New repositories start with this. Each repository can change it in its own settings. Custom rules are set with <span class="mono">co-maintainer set --review-policy-file=policy.json</span>.</div>
157
184
  </div>
158
185
  </div>
159
186
  <div class="ft"><button class="primary" id="save-def">Save</button></div>
160
- </div>
161
- <div class="card" id="server" data-async>
187
+ </div>`;
188
+ }
189
+ function runtimeCard(runtime) {
190
+ const port = runtime.port === undefined ? "unknown" : String(runtime.port);
191
+ return `<div class="card" id="runtime">
192
+ <div class="hd"><h2>This server</h2></div>
193
+ <div class="bd">
194
+ <dl class="dl">
195
+ <div><dt>Version</dt><dd>${text(VERSION)}</dd></div>
196
+ <div><dt>Port</dt><dd>${text(port)}</dd></div>
197
+ <div><dt>Behind a proxy</dt><dd>${runtime.trustProxy ? "Yes" : "No"}</dd></div>
198
+ <div><dt>Data</dt><dd class="mono">${text(appDbDir())}</dd></div>
199
+ </dl>
200
+ </div>
201
+ </div>`;
202
+ }
203
+ function serverCard(config, runtime) {
204
+ return `${runtimeCard(runtime)}<div class="card" id="server" data-async>
162
205
  ${skSlot()}
163
206
  <div class="hd"><h2>Job queue</h2></div>
164
207
  <div class="bd">
@@ -175,8 +218,10 @@ Leave blank to keep the current key"></textarea></div>
175
218
  </div>
176
219
  </div>
177
220
  <div class="ft"><button class="primary" id="save-server">Save</button></div>
178
- </div>
179
- <div class="card" id="remote" data-async>
221
+ </div>`;
222
+ }
223
+ function remoteCard(config) {
224
+ return `<div class="card" id="remote" data-async>
180
225
  ${skSlot()}
181
226
  <div class="hd"><h2>Remote review tokens</h2></div>
182
227
  <div class="bd">
@@ -199,8 +244,10 @@ Leave blank to keep the current key"></textarea></div>
199
244
  <button class="primary" id="create-remote-token">Create token</button>
200
245
  <button class="btn" id="save-remote-byok">Save policy</button>
201
246
  </div>
202
- </div>
203
- <div class="card" id="access" data-async>
247
+ </div>`;
248
+ }
249
+ function signInCards(config) {
250
+ return `<div class="card" id="access" data-async>
204
251
  ${skSlot()}
205
252
  <div class="hd"><h2>Sign-in</h2></div>
206
253
  <div class="bd">
@@ -213,8 +260,8 @@ Leave blank to keep the current key"></textarea></div>
213
260
  <input id="oauth-client-id" value="${text(config.githubOAuthClientId ?? "")}"></div>
214
261
  <div class="field"><label>OAuth client secret</label>
215
262
  <input id="oauth-client-secret" type="password" placeholder="${config.githubOAuthClientSecret
216
- ? "Leave blank to keep the current secret"
217
- : ""}"></div>
263
+ ? "Leave blank to keep the current secret"
264
+ : ""}"></div>
218
265
  </div>
219
266
  <div class="field wide"><label>Allowed GitHub username</label>
220
267
  <input id="oauth-allowed-user" value="${text(config.githubOAuthAllowedUser ?? "")}">
@@ -238,8 +285,10 @@ Leave blank to keep the current key"></textarea></div>
238
285
  </div>
239
286
  </div>
240
287
  <div class="ft"><button class="primary" id="save-password">Change password</button></div>
241
- </div>
242
- <div class="card" id="danger" data-async>
288
+ </div>`;
289
+ }
290
+ function dangerCard() {
291
+ return `<div class="card" id="danger" data-async>
243
292
  ${skSlot()}
244
293
  <div class="hd"><h2>Clear all knowledge</h2></div>
245
294
  <div class="bd">
@@ -247,8 +296,10 @@ Leave blank to keep the current key"></textarea></div>
247
296
  <div class="field wide"><label><input type="checkbox" id="clear-all-cache"> Also delete the cached evidence: pull request listings, costs, clones and worktrees</label></div>
248
297
  </div>
249
298
  <div class="ft"><button id="clear-all" style="color:var(--red)">Clear all knowledge</button></div>
250
- </div>
251
- <div class="card" id="about">
299
+ </div>`;
300
+ }
301
+ function aboutCard() {
302
+ return `<div class="card" id="about">
252
303
  <div class="hd"><h2>About</h2></div>
253
304
  <div class="bd">
254
305
  <table style="margin:-18px -18px 0;width:calc(100% + 36px)">
@@ -262,85 +313,36 @@ Leave blank to keep the current key"></textarea></div>
262
313
  </tbody>
263
314
  </table>
264
315
  </div>
265
- </div>
266
- </div>
267
- </div>
268
- <script>
269
- function save(btn, body) {
270
- run(btn, btn.closest("[data-async]"), async function() {
271
- await api("PUT", "/api/settings", body);
272
- location.reload();
273
- });
316
+ </div>`;
274
317
  }
275
- document.getElementById("save-ai").addEventListener("click", function() {
276
- save(this, {
277
- ai: document.getElementById("ai").value,
278
- token: document.getElementById("token").value,
279
- highModel: document.getElementById("high").value,
280
- lowModel: document.getElementById("low").value
281
- });
282
- });
283
- document.getElementById("save-gh").addEventListener("click", function() {
284
- save(this, {
285
- auth: document.getElementById("auth").value,
286
- githubPat: document.getElementById("pat").value
287
- });
288
- });
289
- document.getElementById("save-app").addEventListener("click", function() {
290
- save(this, {
291
- githubAppId: document.getElementById("app-id").value,
292
- githubAppPrivateKey: document.getElementById("app-key").value,
293
- githubWebhookSecret: document.getElementById("hook-secret").value,
294
- webhookUrl: document.getElementById("webhook-url").value
295
- });
296
- });
297
- document.getElementById("create-app").addEventListener("click", function() {
298
- var card = this.closest("[data-async]");
299
- var name = document.getElementById("app-name").value.trim();
300
- if (!name) {
301
- fail(card, "Enter a name for the GitHub App.", function () {});
302
- return;
303
- }
304
- var form = document.createElement("form");
305
- form.method = "POST";
306
- form.action = "/github/app-manifest";
307
- form.style.display = "none";
308
- var field = document.createElement("input");
309
- field.type = "hidden";
310
- field.name = "name";
311
- field.value = name;
312
- form.appendChild(field);
313
- document.body.appendChild(form);
314
- form.submit();
315
- });
316
- document.getElementById("save-access").addEventListener("click", function() {
317
- save(this, {
318
- passwordAuthDisabled: !document.getElementById("password-auth").checked,
319
- githubAuthEnabled: document.getElementById("github-auth").checked,
320
- githubOAuthClientId: document.getElementById("oauth-client-id").value,
321
- githubOAuthClientSecret: document.getElementById("oauth-client-secret").value,
322
- githubOAuthAllowedUser: document.getElementById("oauth-allowed-user").value
323
- });
324
- });
325
- document.getElementById("save-password").addEventListener("click", function() {
326
- var btn = this;
318
+ function cardForSection(section, config, webhookUrl, host, runtime) {
319
+ switch (section) {
320
+ case "ai":
321
+ return aiCard(config);
322
+ case "github":
323
+ return githubCards(config, webhookUrl, host);
324
+ case "defaults":
325
+ return defaultsCard(config);
326
+ case "remote":
327
+ return remoteCard(config);
328
+ case "server":
329
+ return serverCard(config, runtime);
330
+ case "sign-in":
331
+ return signInCards(config);
332
+ case "danger":
333
+ return dangerCard();
334
+ }
335
+ }
336
+ /** The helpers shared by every settings script, so a page only needs the block
337
+ * for its own card. Kept in one place rather than copied per page. */
338
+ const SCRIPT_COMMON = `
339
+ function save(btn, body) {
327
340
  var card = btn.closest("[data-async]");
328
- var next = document.getElementById("pw-new").value;
329
- if (next !== document.getElementById("pw-repeat").value) {
330
- fail(card, "The two new passwords do not match.", function () {});
331
- return;
332
- }
333
341
  run(btn, card, async function() {
334
- await api("POST", "/api/settings/password", {
335
- currentPassword: document.getElementById("pw-current").value,
336
- newPassword: next
337
- });
338
- ["pw-current", "pw-new", "pw-repeat"].forEach(function(id) {
339
- document.getElementById(id).value = "";
340
- });
341
- toast("Password changed");
342
+ await api("PUT", "/api/settings", body);
343
+ saved(btn, card);
342
344
  });
343
- });
345
+ }
344
346
  function positiveOrNull(raw, label) {
345
347
  if (raw === "") return { ok: true, value: null };
346
348
  var n = Number(raw);
@@ -349,34 +351,49 @@ function positiveOrNull(raw, label) {
349
351
  }
350
352
  return { ok: true, value: n };
351
353
  }
352
- document.getElementById("save-server").addEventListener("click", function() {
353
- var card = this.closest("[data-async]");
354
- var jobs = positiveOrNull(document.getElementById("max-jobs").value.trim(), "Max concurrent jobs");
355
- if (!jobs.ok) { fail(card, jobs.message, function () {}); return; }
356
- var timeout = positiveOrNull(document.getElementById("remote-timeout").value.trim(), "Remote sync timeout");
357
- if (!timeout.ok) { fail(card, timeout.message, function () {}); return; }
358
- var perToken = positiveOrNull(document.getElementById("remote-per-token").value.trim(), "Max remote reviews per token");
359
- if (!perToken.ok) { fail(card, perToken.message, function () {}); return; }
360
- var toolChars = positiveOrNull(document.getElementById("remote-tool-chars").value.trim(), "Max tool output chars");
361
- if (!toolChars.ok) { fail(card, toolChars.message, function () {}); return; }
362
- save(this, {
363
- maxConcurrentJobs: jobs.value,
364
- remoteSyncTimeoutSeconds: timeout.value,
365
- maxConcurrentRemoteReviewsPerToken: perToken.value,
366
- remoteToolOutputMaxChars: toolChars.value
367
- });
368
- });
369
- document.getElementById("save-remote-byok").addEventListener("click", function() {
370
- var card = this.closest("[data-async]");
371
- save(this, {
372
- remoteByokPolicy: document.getElementById("remote-byok-policy").value
373
- });
374
- });
375
354
  function escHtml(s) {
376
355
  return String(s).replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
377
356
  }
357
+ function cardFor(btn) { return btn.closest("[data-async]"); }
358
+ function showSecret(token) {
359
+ var box = document.getElementById("remote-token-secret");
360
+ box.hidden = false;
361
+ box.replaceChildren();
362
+ var title = document.createElement("b");
363
+ title.textContent = "Copy this token now. It is not shown again.";
364
+ var row = document.createElement("div");
365
+ row.className = "secret-row";
366
+ var code = document.createElement("code");
367
+ code.textContent = token;
368
+ var copy = document.createElement("button");
369
+ copy.type = "button";
370
+ copy.className = "btn sm";
371
+ copy.textContent = "Copy";
372
+ copy.addEventListener("click", async function() {
373
+ try {
374
+ await navigator.clipboard.writeText(token);
375
+ copy.textContent = "Copied";
376
+ } catch (err) {
377
+ toast("Copy failed. Select the token and copy it by hand.");
378
+ }
379
+ });
380
+ var dismiss = document.createElement("button");
381
+ dismiss.type = "button";
382
+ dismiss.className = "btn sm";
383
+ dismiss.textContent = "I have saved it";
384
+ dismiss.addEventListener("click", function() {
385
+ box.hidden = true;
386
+ box.replaceChildren();
387
+ });
388
+ row.appendChild(code);
389
+ row.appendChild(copy);
390
+ row.appendChild(dismiss);
391
+ box.appendChild(title);
392
+ box.appendChild(row);
393
+ }
378
394
  async function loadRemoteTokens() {
379
395
  var el = document.getElementById("remote-token-list");
396
+ if (!el) return;
380
397
  try {
381
398
  var rows = await api("GET", "/api/remote-tokens");
382
399
  if (!rows.length) {
@@ -415,57 +432,60 @@ async function loadRemoteTokens() {
415
432
  el.textContent = "Could not load tokens.";
416
433
  }
417
434
  }
418
- function cardFor(btn) { return btn.closest("[data-async]"); }
419
- function showSecret(token) {
420
- var box = document.getElementById("remote-token-secret");
421
- box.hidden = false;
422
- box.replaceChildren();
423
- var title = document.createElement("b");
424
- title.textContent = "Copy this token now. It is not shown again.";
425
- var row = document.createElement("div");
426
- row.className = "secret-row";
427
- var code = document.createElement("code");
428
- code.textContent = token;
429
- var copy = document.createElement("button");
430
- copy.type = "button";
431
- copy.className = "btn sm";
432
- copy.textContent = "Copy";
433
- copy.addEventListener("click", async function() {
434
- try {
435
- await navigator.clipboard.writeText(token);
436
- copy.textContent = "Copied";
437
- } catch (err) {
438
- toast("Copy failed. Select the token and copy it by hand.");
439
- }
435
+ `;
436
+ const SCRIPT_AI = `
437
+ var replaceKey = document.getElementById("replace-key");
438
+ if (replaceKey) replaceKey.addEventListener("click", function() {
439
+ var input = document.getElementById("token");
440
+ input.hidden = false;
441
+ input.focus();
442
+ replaceKey.closest(".actions-row").hidden = true;
443
+ });
444
+ document.getElementById("save-ai").addEventListener("click", function() {
445
+ save(this, {
446
+ ai: document.getElementById("ai-provider").value,
447
+ token: document.getElementById("token").value,
448
+ highModel: document.getElementById("high").value,
449
+ lowModel: document.getElementById("low").value
440
450
  });
441
- var dismiss = document.createElement("button");
442
- dismiss.type = "button";
443
- dismiss.className = "btn sm";
444
- dismiss.textContent = "I have saved it";
445
- dismiss.addEventListener("click", function() {
446
- box.hidden = true;
447
- box.replaceChildren();
451
+ });
452
+ `;
453
+ const SCRIPT_GITHUB = `
454
+ document.getElementById("save-gh").addEventListener("click", function() {
455
+ save(this, {
456
+ auth: document.getElementById("auth").value,
457
+ githubPat: document.getElementById("pat").value
448
458
  });
449
- row.appendChild(code);
450
- row.appendChild(copy);
451
- row.appendChild(dismiss);
452
- box.appendChild(title);
453
- box.appendChild(row);
454
- }
455
- loadRemoteTokens();
456
- document.getElementById("create-remote-token").addEventListener("click", function() {
457
- var name = document.getElementById("remote-token-name").value.trim();
459
+ });
460
+ document.getElementById("save-app").addEventListener("click", function() {
461
+ save(this, {
462
+ githubAppId: document.getElementById("app-id").value,
463
+ githubAppPrivateKey: document.getElementById("app-key").value,
464
+ githubWebhookSecret: document.getElementById("hook-secret").value,
465
+ webhookUrl: document.getElementById("webhook-url").value
466
+ });
467
+ });
468
+ document.getElementById("create-app").addEventListener("click", function() {
469
+ var card = this.closest("[data-async]");
470
+ var name = document.getElementById("app-name").value.trim();
458
471
  if (!name) {
459
- fail(this.closest("[data-async]"), "Enter a name for the token.", function () {});
472
+ fail(card, "Enter a name for the GitHub App.", function () {});
460
473
  return;
461
474
  }
462
- run(this, this.closest("[data-async]"), async function() {
463
- var created = await api("POST", "/api/remote-tokens", { name: name });
464
- showSecret(created.token);
465
- document.getElementById("remote-token-name").value = "";
466
- await loadRemoteTokens();
467
- });
475
+ var form = document.createElement("form");
476
+ form.method = "POST";
477
+ form.action = "/github/app-manifest";
478
+ form.style.display = "none";
479
+ var field = document.createElement("input");
480
+ field.type = "hidden";
481
+ field.name = "name";
482
+ field.value = name;
483
+ form.appendChild(field);
484
+ document.body.appendChild(form);
485
+ form.submit();
468
486
  });
487
+ `;
488
+ const SCRIPT_DEFAULTS = `
469
489
  var POLICY_INFO = ${JSON.stringify(TEMPLATE_INFO)};
470
490
  document.getElementById("def-policy").addEventListener("change", function() {
471
491
  var value = this.value;
@@ -510,6 +530,78 @@ document.getElementById("save-def").addEventListener("click", function() {
510
530
  if (policy !== "__custom") body.reviewPolicy = policy === "" ? null : policy;
511
531
  save(this, body);
512
532
  });
533
+ `;
534
+ const SCRIPT_SERVER = `
535
+ document.getElementById("save-server").addEventListener("click", function() {
536
+ var card = this.closest("[data-async]");
537
+ var jobs = positiveOrNull(document.getElementById("max-jobs").value.trim(), "Max concurrent jobs");
538
+ if (!jobs.ok) { fail(card, jobs.message, function () {}); return; }
539
+ var timeout = positiveOrNull(document.getElementById("remote-timeout").value.trim(), "Remote sync timeout");
540
+ if (!timeout.ok) { fail(card, timeout.message, function () {}); return; }
541
+ var perToken = positiveOrNull(document.getElementById("remote-per-token").value.trim(), "Max remote reviews per token");
542
+ if (!perToken.ok) { fail(card, perToken.message, function () {}); return; }
543
+ var toolChars = positiveOrNull(document.getElementById("remote-tool-chars").value.trim(), "Max tool output chars");
544
+ if (!toolChars.ok) { fail(card, toolChars.message, function () {}); return; }
545
+ save(this, {
546
+ maxConcurrentJobs: jobs.value,
547
+ remoteSyncTimeoutSeconds: timeout.value,
548
+ maxConcurrentRemoteReviewsPerToken: perToken.value,
549
+ remoteToolOutputMaxChars: toolChars.value
550
+ });
551
+ });
552
+ `;
553
+ const SCRIPT_REMOTE = `
554
+ loadRemoteTokens();
555
+ document.getElementById("create-remote-token").addEventListener("click", function() {
556
+ var name = document.getElementById("remote-token-name").value.trim();
557
+ if (!name) {
558
+ fail(this.closest("[data-async]"), "Enter a name for the token.", function () {});
559
+ return;
560
+ }
561
+ run(this, this.closest("[data-async]"), async function() {
562
+ var created = await api("POST", "/api/remote-tokens", { name: name });
563
+ showSecret(created.token);
564
+ document.getElementById("remote-token-name").value = "";
565
+ await loadRemoteTokens();
566
+ });
567
+ });
568
+ document.getElementById("save-remote-byok").addEventListener("click", function() {
569
+ save(this, {
570
+ remoteByokPolicy: document.getElementById("remote-byok-policy").value
571
+ });
572
+ });
573
+ `;
574
+ const SCRIPT_SIGNIN = `
575
+ document.getElementById("save-access").addEventListener("click", function() {
576
+ save(this, {
577
+ passwordAuthDisabled: !document.getElementById("password-auth").checked,
578
+ githubAuthEnabled: document.getElementById("github-auth").checked,
579
+ githubOAuthClientId: document.getElementById("oauth-client-id").value,
580
+ githubOAuthClientSecret: document.getElementById("oauth-client-secret").value,
581
+ githubOAuthAllowedUser: document.getElementById("oauth-allowed-user").value
582
+ });
583
+ });
584
+ document.getElementById("save-password").addEventListener("click", function() {
585
+ var btn = this;
586
+ var card = btn.closest("[data-async]");
587
+ var next = document.getElementById("pw-new").value;
588
+ if (next !== document.getElementById("pw-repeat").value) {
589
+ fail(card, "The two new passwords do not match.", function () {});
590
+ return;
591
+ }
592
+ run(btn, card, async function() {
593
+ await api("POST", "/api/settings/password", {
594
+ currentPassword: document.getElementById("pw-current").value,
595
+ newPassword: next
596
+ });
597
+ ["pw-current", "pw-new", "pw-repeat"].forEach(function(id) {
598
+ document.getElementById(id).value = "";
599
+ });
600
+ toast("Password changed");
601
+ });
602
+ });
603
+ `;
604
+ const SCRIPT_DANGER = `
513
605
  document.getElementById("clear-all").addEventListener("click", function() {
514
606
  if (!confirm("Delete the guides and saved settings for every repository on this server?")) return;
515
607
  var includeCache = document.getElementById("clear-all-cache").checked;
@@ -519,6 +611,58 @@ document.getElementById("clear-all").addEventListener("click", function() {
519
611
  if (done !== false) location.reload();
520
612
  });
521
613
  });
522
- </script>`,
614
+ `;
615
+ const SCRIPT_BY_SECTION = {
616
+ ai: SCRIPT_AI,
617
+ github: SCRIPT_GITHUB,
618
+ defaults: SCRIPT_DEFAULTS,
619
+ remote: SCRIPT_REMOTE,
620
+ server: SCRIPT_SERVER,
621
+ "sign-in": SCRIPT_SIGNIN,
622
+ danger: SCRIPT_DANGER,
623
+ };
624
+ /** The combined `/settings` landing keeps every card (and the old bookmarks
625
+ * working) while each heading also has its own page. */
626
+ export function renderSettings(username, config, webhookUrl, baseUrl, runtime = {}) {
627
+ return settingsPage(username, config, webhookUrl, baseUrl, SETTINGS_SECTIONS, "AI models", "Settings · co-maintainer", NAV_HREF.ai, true, runtime);
628
+ }
629
+ export function renderSettingsPage(username, config, webhookUrl, baseUrl, section, runtime = {}) {
630
+ if (!SETTINGS_SECTIONS.includes(section))
631
+ return undefined;
632
+ return settingsPage(username, config, webhookUrl, baseUrl, [section], HEADING[section], `${HEADING[section]} · co-maintainer`, NAV_HREF[section], false, runtime);
633
+ }
634
+ function settingsPage(username, config, webhookUrl, baseUrl, sections, heading, title, active, combined, runtime) {
635
+ const host = hostOf(baseUrl);
636
+ const cards = sections
637
+ .map((section) => cardForSection(section, config, webhookUrl, host, runtime))
638
+ .join("");
639
+ const about = sections.includes("server") || combined ? aboutCard() : "";
640
+ const fragment = combined
641
+ ? `
642
+ (function() {
643
+ var map = {
644
+ ai: "/settings/ai", github: "/settings/github", defaults: "/settings/defaults",
645
+ remote: "/settings/remote", server: "/settings/server",
646
+ access: "/settings/sign-in", signin: "/settings/sign-in", password: "/settings/sign-in",
647
+ danger: "/settings/danger"
648
+ };
649
+ var id = location.hash.replace(/^#/, "").toLowerCase();
650
+ if (map[id]) location.replace(map[id]);
651
+ })();`
652
+ : "";
653
+ const script = SCRIPT_COMMON +
654
+ sections.map((section) => SCRIPT_BY_SECTION[section]).join("") +
655
+ fragment;
656
+ return html(layout({
657
+ title,
658
+ username,
659
+ active,
660
+ body: `<div class="wrap">
661
+ <div class="pagehead"><div><h1>${text(heading)}</h1>
662
+ <p class="lead">Used by every repository unless it overrides them.</p></div></div>
663
+ ${cards}
664
+ ${about}
665
+ </div>
666
+ <script>${script}</script>`,
523
667
  }));
524
668
  }