critiqs-react 0.0.0-stage → 0.3.0

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 (64) hide show
  1. package/AGENT_SETUP.md +190 -0
  2. package/LICENSE +21 -0
  3. package/README.md +341 -2
  4. package/bin/cli.d.mts +21 -0
  5. package/bin/cli.mjs +293 -0
  6. package/bin/critiqs.mjs +4 -0
  7. package/bin/env.d.mts +41 -0
  8. package/bin/env.mjs +384 -0
  9. package/bin/init.d.mts +126 -0
  10. package/bin/init.mjs +965 -0
  11. package/bin/mcp.d.mts +20 -0
  12. package/bin/mcp.mjs +314 -0
  13. package/bin/setup.d.mts +183 -0
  14. package/bin/setup.mjs +1054 -0
  15. package/dist/Critiqs.d.ts +9 -0
  16. package/dist/backend/convex/_generated/api.d.ts +95 -0
  17. package/dist/backend/convex/_generated/api.js +23 -0
  18. package/dist/backend/convex/_generated/dataModel.d.ts +60 -0
  19. package/dist/backend/convex/_generated/server.d.ts +162 -0
  20. package/dist/backend/convex/_generated/server.js +101 -0
  21. package/dist/backend/convex/ai.ts +93 -0
  22. package/dist/backend/convex/aiSettings.ts +43 -0
  23. package/dist/backend/convex/auth.config.ts +18 -0
  24. package/dist/backend/convex/captureHttp.ts +96 -0
  25. package/dist/backend/convex/captures.ts +167 -0
  26. package/dist/backend/convex/crons.ts +14 -0
  27. package/dist/backend/convex/deviceSignIn.ts +151 -0
  28. package/dist/backend/convex/grouping.ts +41 -0
  29. package/dist/backend/convex/http.ts +724 -0
  30. package/dist/backend/convex/lib/ai.ts +6 -0
  31. package/dist/backend/convex/lib/links.ts +12 -0
  32. package/dist/backend/convex/lib/pages.ts +207 -0
  33. package/dist/backend/convex/lib/r2.ts +75 -0
  34. package/dist/backend/convex/lib/store.ts +509 -0
  35. package/dist/backend/convex/lib/tokens.ts +101 -0
  36. package/dist/backend/convex/lib/validators.ts +73 -0
  37. package/dist/backend/convex/lib/vendor/aws4fetch.js +289 -0
  38. package/dist/backend/convex/lib/vendor/critiqs_ai.js +49081 -0
  39. package/dist/backend/convex/lib/vendor/critiqs_core.js +385 -0
  40. package/dist/backend/convex/lib/vendor/critiqs_server.js +3108 -0
  41. package/dist/backend/convex/notificationEmails.ts +61 -0
  42. package/dist/backend/convex/notifications.ts +182 -0
  43. package/dist/backend/convex/plans.ts +67 -0
  44. package/dist/backend/convex/projects.ts +46 -0
  45. package/dist/backend/convex/schema.ts +343 -0
  46. package/dist/backend/convex/search.ts +26 -0
  47. package/dist/backend/convex/signIn.ts +368 -0
  48. package/dist/backend/convex/tags.ts +68 -0
  49. package/dist/backend/convex/threads.ts +146 -0
  50. package/dist/backend/convex/users.ts +13 -0
  51. package/dist/constants.d.ts +4 -0
  52. package/dist/core/types.d.ts +72 -0
  53. package/dist/host.d.ts +66 -0
  54. package/dist/index.d.ts +4 -0
  55. package/dist/index.js +8076 -0
  56. package/dist/server/chunks/driver-n9zpr0k3.js +21 -0
  57. package/dist/server/chunks/index-9bg7tmnq.js +522 -0
  58. package/dist/server/chunks/index-askcfn7t.js +148 -0
  59. package/dist/server/chunks/providers-rv184xvj.js +48444 -0
  60. package/dist/server/chunks/token-kzddms7r.js +63 -0
  61. package/dist/server/chunks/token-util-k8bee1wc.js +4 -0
  62. package/dist/server/index.d.ts +87 -0
  63. package/dist/server/index.js +4889 -0
  64. package/package.json +77 -4
@@ -0,0 +1,6 @@
1
+ import { aiDeploymentFromEnv, type AiDeployment } from "./vendor/critiqs_server.js";
2
+
3
+ /** This deployment's AI, from its environment: Critiqs-managed AI, a default provider, the key that encrypts owners' keys, and allowed endpoints. */
4
+ export function deployment(): AiDeployment {
5
+ return aiDeploymentFromEnv(process.env);
6
+ }
@@ -0,0 +1,12 @@
1
+ import { internal } from "../_generated/api";
2
+ import type { ActionCtx } from "../_generated/server";
3
+
4
+ /** The key that signs mute and unsubscribe links, generated once per deployment so it needs no setup. */
5
+ export async function linkSecret(ctx: Pick<ActionCtx, "runQuery" | "runMutation">): Promise<string> {
6
+ const existing = await ctx.runQuery(internal.notifications.secretValue, {});
7
+ if (existing !== null) return existing;
8
+ const bytes = crypto.getRandomValues(new Uint8Array(32));
9
+ return await ctx.runMutation(internal.notifications.saveSecret, {
10
+ value: btoa(String.fromCharCode(...bytes)),
11
+ });
12
+ }
@@ -0,0 +1,207 @@
1
+ import { randomSecret, type ReviewerProfile } from "./tokens";
2
+
3
+ /** What a sign-in is for, as the pages name it. Matches `DEVICE_SITE` in deviceSignIn.ts. */
4
+ const DEVICE_SITE = "Critiqs setup on another device";
5
+
6
+ function purpose(origin: string): string {
7
+ return origin === DEVICE_SITE ? "approve Critiqs setup started on another device" : `review <strong>${escape(origin)}</strong>`;
8
+ }
9
+
10
+ // Pages shown in Critiqs's sign-in popup. They are never framed, load nothing
11
+ // external, and post forms only back to this deployment.
12
+
13
+ const STYLE = `
14
+ body { margin: 0; font: 15px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif; color: #1a1a1a; background: #f5f5f4; }
15
+ main { max-width: 360px; margin: 48px auto; padding: 28px; background: #fff; border-radius: 12px; box-shadow: 0 1px 3px rgb(0 0 0 / 0.12); }
16
+ h1 { font-size: 18px; margin: 0 0 12px; }
17
+ p { margin: 0 0 12px; color: #444; }
18
+ strong { color: #1a1a1a; word-break: break-all; }
19
+ label { display: block; margin: 12px 0 4px; font-weight: 600; font-size: 13px; }
20
+ input { box-sizing: border-box; width: 100%; padding: 8px 10px; font: inherit; border: 1px solid #ccc; border-radius: 6px; }
21
+ .actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 20px; }
22
+ button { font: inherit; font-weight: 600; padding: 8px 14px; border-radius: 6px; border: 1px solid #ccc; background: #fff; cursor: pointer; }
23
+ button.primary { background: #0d63f3; border-color: #0d63f3; color: #fff; }
24
+ .warning { padding: 8px 10px; border-radius: 6px; background: #fff4d6; color: #6b4e00; font-size: 13px; }
25
+ .error { padding: 8px 10px; border-radius: 6px; background: #fdecec; color: #a61b1b; font-size: 13px; }
26
+ .note { margin-top: 16px; font-size: 13px; color: #666; }
27
+ .secondary { margin-top: 12px; }
28
+ .secondary button { border: 0; background: none; color: #0d63f3; padding: 0; font-weight: 500; }
29
+ `;
30
+
31
+ function escape(text: string): string {
32
+ return text.replace(/[&<>"']/g, (char) => `&#${char.charCodeAt(0)};`);
33
+ }
34
+
35
+ function page(title: string, body: string, nonce?: string): Response {
36
+ const script = nonce === undefined ? "" : ` script-src 'nonce-${nonce}';`;
37
+ return new Response(
38
+ `<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>${escape(title)}</title><style>${STYLE}</style></head><body><main>${body}</main></body></html>`,
39
+ {
40
+ headers: {
41
+ "Content-Type": "text/html; charset=utf-8",
42
+ "Cache-Control": "no-store",
43
+ "X-Frame-Options": "DENY",
44
+ "Referrer-Policy": "no-referrer",
45
+ "Content-Security-Policy": `default-src 'none'; style-src 'unsafe-inline'; img-src https: data:; form-action 'self'; frame-ancestors 'none';${script}`,
46
+ },
47
+ },
48
+ );
49
+ }
50
+
51
+ export function messagePage(title: string, message: string, status = 400): Response {
52
+ const response = page(title, `<h1>${escape(title)}</h1><p>${escape(message)}</p>`);
53
+ return new Response(response.body, { status, headers: response.headers });
54
+ }
55
+
56
+ const DEV_WARNING = `<p class="warning">Development sign-in: no email is sent, and the code appears below. Never enable this on a shared deployment.</p>`;
57
+
58
+ function attemptField(attemptKey: string): string {
59
+ return `<input type="hidden" name="attempt" value="${escape(attemptKey)}">`;
60
+ }
61
+
62
+ /** First step: the reviewer's email address. The same page serves new and returning reviewers. */
63
+ export function emailPage(attemptKey: string, origin: string, options: { development: boolean; error?: string; email?: string }): Response {
64
+ return page(
65
+ "Sign in to Critiqs",
66
+ `<h1>Sign in to Critiqs</h1>
67
+ ${options.development ? DEV_WARNING : ""}
68
+ <p>To ${purpose(origin)}, enter your email. We'll send you a code; there's no password.</p>
69
+ ${options.error === undefined ? "" : `<p class="error">${escape(options.error)}</p>`}
70
+ <form method="post" action="/auth/email">
71
+ ${attemptField(attemptKey)}
72
+ <label for="email">Email</label>
73
+ <input id="email" name="email" type="email" autocomplete="email" required autofocus value="${escape(options.email ?? "")}">
74
+ <div class="actions"><button class="primary" type="submit">Send code</button></div>
75
+ </form>`,
76
+ );
77
+ }
78
+
79
+ /** Second step: the code. It never says whether the address already has an account. */
80
+ export function codePage(
81
+ attemptKey: string,
82
+ origin: string,
83
+ email: string,
84
+ options: { development: boolean; developmentCode?: string; error?: string; canResend: boolean },
85
+ ): Response {
86
+ return page(
87
+ "Enter your code",
88
+ `<h1>Check your email</h1>
89
+ ${options.development ? DEV_WARNING : ""}
90
+ <p>If <strong>${escape(email)}</strong> can receive email, a 6-digit code is on its way. It works for 10 minutes.</p>
91
+ ${options.developmentCode === undefined ? "" : `<p>Development code: <strong id="development-code">${escape(options.developmentCode)}</strong></p>`}
92
+ ${options.error === undefined ? "" : `<p class="error">${escape(options.error)}</p>`}
93
+ <form method="post" action="/auth/code">
94
+ ${attemptField(attemptKey)}
95
+ <label for="code">Code</label>
96
+ <input id="code" name="code" inputmode="numeric" autocomplete="one-time-code" maxlength="12" required autofocus>
97
+ <div class="actions"><button class="primary" type="submit">Sign in</button></div>
98
+ </form>
99
+ ${
100
+ options.canResend
101
+ ? `<form method="post" action="/auth/email" class="secondary">${attemptField(attemptKey)}<input type="hidden" name="email" value="${escape(email)}"><button type="submit">Send a new code</button></form>`
102
+ : ""
103
+ }
104
+ <p class="note">Signing in to ${purpose(origin).replace(/<\/?strong>/g, "")}. Only type the code here.</p>`,
105
+ );
106
+ }
107
+
108
+ /** For a new reviewer after their address is verified: the name others will see. */
109
+ export function namePage(attemptKey: string, options: { error?: string }): Response {
110
+ return page(
111
+ "Choose your name",
112
+ `<h1>Choose your display name</h1>
113
+ <p>Everyone in the projects you review sees this name on your comments. Your email address is never shown. You can change it later.</p>
114
+ ${options.error === undefined ? "" : `<p class="error">${escape(options.error)}</p>`}
115
+ <form method="post" action="/auth/name">
116
+ ${attemptField(attemptKey)}
117
+ <label for="name">Display name</label>
118
+ <input id="name" name="name" autocomplete="name" maxlength="60" required autofocus>
119
+ <div class="actions"><button class="primary" type="submit">Continue</button></div>
120
+ </form>`,
121
+ );
122
+ }
123
+
124
+ /** Asked the first time a reviewer signs in from a site, so another site cannot borrow their access. */
125
+ export function consentPage(attemptKey: string, consentToken: string, origin: string, profile: ReviewerProfile): Response {
126
+ const field = `<input type="hidden" name="attempt" value="${escape(attemptKey)}"><input type="hidden" name="consent" value="${escape(consentToken)}">`;
127
+ return page(
128
+ "Allow this site?",
129
+ `<h1>Allow this site to use Critiqs?</h1>
130
+ <p><strong>${escape(origin)}</strong> wants to show and post feedback as
131
+ <strong>${escape(profile.name)}</strong> (${escape(profile.email)}).</p>
132
+ <p>Only allow sites you use for reviewing prototypes. You will not be asked again for this site.</p>
133
+ <div class="actions">
134
+ <form method="post" action="/auth/consent">${field}<input type="hidden" name="decision" value="cancel"><button type="submit">Cancel</button></form>
135
+ <form method="post" action="/auth/consent">${field}<input type="hidden" name="decision" value="allow"><button class="primary" type="submit">Allow this site</button></form>
136
+ </div>`,
137
+ );
138
+ }
139
+
140
+ /**
141
+ * Hands the result to the component that opened the popup. `postMessage` names
142
+ * the allowed origin, so the browser delivers it to no other site.
143
+ */
144
+ export function deliverPage(origin: string, message: Record<string, unknown>): Response {
145
+ const nonce = randomSecret(16);
146
+ const data = JSON.stringify({ message, origin }).replace(/</g, "\\u003c");
147
+ return page(
148
+ "Signing in…",
149
+ `<h1>Returning to the prototype…</h1>
150
+ <p>You can close this window if it stays open.</p>
151
+ <script nonce="${nonce}">
152
+ const { message, origin } = ${data};
153
+ if (window.opener) window.opener.postMessage(message, origin);
154
+ window.close();
155
+ </script>`,
156
+ nonce,
157
+ );
158
+ }
159
+
160
+ /**
161
+ * Confirms a mute or unsubscribe link before changing anything, so a mail
162
+ * scanner that opens links can't change a reviewer's choices.
163
+ */
164
+ export function preferencePage(token: string, description: string): Response {
165
+ return page(
166
+ "Email notifications",
167
+ `<h1>Email notifications</h1>
168
+ <p>${escape(description)}</p>
169
+ <form method="post" action="/notifications/preferences">
170
+ <input type="hidden" name="token" value="${escape(token)}">
171
+ <div class="actions"><button class="primary" type="submit">Confirm</button></div>
172
+ </form>`,
173
+ );
174
+ }
175
+
176
+ /** Where a person approves setup running on another device: first, the code it shows. */
177
+ export function deviceCodePage(options: { error?: string; status?: number } = {}): Response {
178
+ const response = page(
179
+ "Approve Critiqs setup",
180
+ `<h1>Approve Critiqs setup</h1>
181
+ <p>Enter the code shown where you started setting up Critiqs, such as a terminal or a coding agent. You'll sign in next.</p>
182
+ ${options.error === undefined ? "" : `<p class="error">${escape(options.error)}</p>`}
183
+ <form method="post" action="/auth/device">
184
+ <label for="user-code">Setup code</label>
185
+ <input id="user-code" name="user_code" autocomplete="off" autocapitalize="characters" spellcheck="false" maxlength="20" placeholder="XXXX-XXXX" required autofocus>
186
+ <div class="actions"><button class="primary" type="submit">Continue</button></div>
187
+ </form>
188
+ <p class="note">Only enter a code from setup you started yourself. Critiqs never asks for it by email or chat.</p>`,
189
+ );
190
+ return new Response(response.body, { status: options.status ?? 200, headers: response.headers });
191
+ }
192
+
193
+ /** After sign-in: approve or deny the setup, as this reviewer. Asked every time. */
194
+ export function deviceApprovalPage(attemptKey: string, consentToken: string, profile: ReviewerProfile): Response {
195
+ const field = `<input type="hidden" name="attempt" value="${escape(attemptKey)}"><input type="hidden" name="consent" value="${escape(consentToken)}">`;
196
+ return page(
197
+ "Approve Critiqs setup?",
198
+ `<h1>Approve Critiqs setup?</h1>
199
+ <p>Setup on another device will act as <strong>${escape(profile.name)}</strong> (${escape(profile.email)})
200
+ for up to 15 minutes, to create or connect a Critiqs project.</p>
201
+ <p>Approve only if you started this setup yourself just now.</p>
202
+ <div class="actions">
203
+ <form method="post" action="/auth/device/decision">${field}<input type="hidden" name="decision" value="deny"><button type="submit">Deny</button></form>
204
+ <form method="post" action="/auth/device/decision">${field}<input type="hidden" name="decision" value="approve"><button class="primary" type="submit">Approve</button></form>
205
+ </div>`,
206
+ );
207
+ }
@@ -0,0 +1,75 @@
1
+ import { AwsClient } from "./vendor/aws4fetch.js";
2
+
3
+ export interface R2Config {
4
+ accountId: string;
5
+ bucket: string;
6
+ accessKeyId: string;
7
+ secretAccessKey: string;
8
+ }
9
+
10
+ /** Only operators choose the provider. Partial R2 setup must never fall back. */
11
+ export function captureProvider(env = process.env): "convex" | "r2" {
12
+ const provider = env.CRITIQS_CAPTURE_STORAGE ?? "convex";
13
+ if (provider !== "convex" && provider !== "r2") throw new Error("CRITIQS_CAPTURE_STORAGE must be convex or r2.");
14
+ return provider;
15
+ }
16
+
17
+ export function r2Config(env = process.env): R2Config {
18
+ const accountId = env.CRITIQS_R2_ACCOUNT_ID ?? "";
19
+ const bucket = env.CRITIQS_R2_BUCKET ?? "";
20
+ const accessKeyId = env.CRITIQS_R2_ACCESS_KEY_ID ?? "";
21
+ const secretAccessKey = env.CRITIQS_R2_SECRET_ACCESS_KEY ?? "";
22
+ if (!/^[a-f0-9]{32}$/.test(accountId) || !/^[a-z0-9][a-z0-9-]{1,61}[a-z0-9]$/.test(bucket) || !accessKeyId || !secretAccessKey) {
23
+ throw new Error("R2 screenshot storage is not configured. Set CRITIQS_R2_ACCOUNT_ID, CRITIQS_R2_BUCKET, CRITIQS_R2_ACCESS_KEY_ID, and CRITIQS_R2_SECRET_ACCESS_KEY.");
24
+ }
25
+ return { accountId, bucket, accessKeyId, secretAccessKey };
26
+ }
27
+
28
+ /** Public diagnostics contain no bucket credentials or signed URLs. */
29
+ export function captureStorageHealth() {
30
+ try {
31
+ const provider = captureProvider();
32
+ if (provider === "r2") r2Config();
33
+ return { provider, configured: true };
34
+ } catch {
35
+ const provider = process.env.CRITIQS_CAPTURE_STORAGE;
36
+ return { provider: provider === "r2" || provider === "convex" ? provider : "invalid", configured: false };
37
+ }
38
+ }
39
+
40
+ /** Server-only S3 client. The production endpoint is derived, never supplied by a browser. */
41
+ export function r2Storage(config: R2Config, fetchFn: typeof fetch = fetch) {
42
+ const client = new AwsClient({ ...config, service: "s3", region: "auto", retries: 0 });
43
+ async function request(location: { accountId: string; bucket: string; key: string }, method: string, body?: ArrayBuffer, contentType?: string) {
44
+ if (location.accountId !== config.accountId) throw new Error("This capture belongs to another R2 account.");
45
+ const path = [location.bucket, ...location.key.split("/")].map(encodeURIComponent).join("/");
46
+ const url = `https://${config.accountId}.r2.cloudflarestorage.com/${path}`;
47
+ const signed = await client.sign(url, {
48
+ method,
49
+ ...(body === undefined ? {} : { body }),
50
+ headers: {
51
+ ...(contentType === undefined ? {} : { "Content-Type": contentType }),
52
+ // Convex streams signed Request bodies; R2 requires their explicit size.
53
+ ...(body === undefined ? {} : { "Content-Length": String(body.byteLength) }),
54
+ },
55
+ });
56
+ // Never follow a redirect with a signed request, or expose its response body in errors.
57
+ return await fetchFn(signed, { redirect: "error", signal: AbortSignal.timeout(30_000) });
58
+ }
59
+ return {
60
+ put: async (location: { accountId: string; bucket: string; key: string }, body: ArrayBuffer, contentType: string) => {
61
+ const response = await request(location, "PUT", body, contentType);
62
+ if (!response.ok) throw new Error("R2 could not save the screenshot.");
63
+ },
64
+ get: async (location: { accountId: string; bucket: string; key: string }) => {
65
+ const response = await request(location, "GET");
66
+ if (response.status === 404) return null;
67
+ if (!response.ok) throw new Error("R2 could not load the screenshot.");
68
+ return response;
69
+ },
70
+ remove: async (location: { accountId: string; bucket: string; key: string }) => {
71
+ const response = await request(location, "DELETE");
72
+ if (!response.ok && response.status !== 404) throw new Error("R2 could not remove the unused screenshot.");
73
+ },
74
+ };
75
+ }