File size: 8,707 Bytes
6b5c39c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { createServer } from "node:http";
import { readFile } from "node:fs/promises";
import path from "node:path";
import { fileURLToPath } from "node:url";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const PORT = Number(process.env.PORT || 3000);
const MODEL = process.env.GEMINI_MODEL || "gemini-2.5-flash";
const API_URL = `https://generativelanguage.googleapis.com/v1beta/models/${MODEL}:generateContent`;
const MAX_BODY_SIZE = 6 * 1024 * 1024;
const DEFAULT_ALLOWED_ORIGINS = [
  "http://127.0.0.1:3000",
  "http://localhost:3000",
];

const PROMPT_TEMPLATE = `You are a senior researcher reviewing a paper for a colleague who needs
to decide in 3 minutes whether this work is credible and worth citing.

Do not summarize. Do not be polite. Be useful.

Return a JSON object with exactly these five string fields:
- claim
- keyNumber
- assumption
- gap
- citeCheck

Write each field using these rules:
- claim: the single falsifiable claim this paper makes, one sentence, no hedging
- keyNumber: the one statistic or result the entire argument depends on; explain why it is load-bearing
- assumption: the hidden premise the authors treated as given but never tested or justified; be specific
- gap: the missing experiment, analysis, or robustness check that would most meaningfully confirm or break the conclusion; if one issue dominates, give one compact paragraph, but if there are multiple distinct load-bearing gaps, use a short list with up to 3 items separated by line breaks inside the same string
- citeCheck: start with exactly one of "SAFE TO CITE -", "CITE WITH CAUTION -", or "DO NOT CITE -" and then give the concrete reason

Every field must be non-empty. Do not include markdown fences. Do not include any keys other than the five above.

Paper text:
`;

const REVIEW_SCHEMA = {
  type: "object",
  properties: {
    claim: {
      type: "string",
      description: "The single falsifiable claim the paper makes.",
    },
    keyNumber: {
      type: "string",
      description: "The one load-bearing statistic or result and why it matters.",
    },
    assumption: {
      type: "string",
      description: "The hidden premise the authors never tested or justified.",
    },
    gap: {
      type: "string",
      description: "The main missing experiment, analysis, or a short list of up to 3 load-bearing gaps separated by line breaks when more than one is truly necessary.",
    },
    citeCheck: {
      type: "string",
      description: "A verdict starting with SAFE TO CITE -, CITE WITH CAUTION -, or DO NOT CITE - followed by the reason.",
    },
  },
  required: ["claim", "keyNumber", "assumption", "gap", "citeCheck"],
};

function sendJson(response, statusCode, payload) {
  response.writeHead(statusCode, {
    "content-type": "application/json; charset=utf-8",
    "cache-control": "no-store",
  });
  response.end(JSON.stringify(payload));
}

function sendText(response, statusCode, text) {
  response.writeHead(statusCode, {
    "content-type": "text/plain; charset=utf-8",
    "cache-control": "no-store",
  });
  response.end(text);
}

function normalizeField(value) {
  return typeof value === "string" ? value.trim() : "";
}

function formatReview(reviewObject) {
  const claim = normalizeField(reviewObject.claim);
  const keyNumber = normalizeField(reviewObject.keyNumber);
  const assumption = normalizeField(reviewObject.assumption);
  const gap = normalizeField(reviewObject.gap);
  const citeCheck = normalizeField(reviewObject.citeCheck);

  if (!claim || !keyNumber || !assumption || !gap || !citeCheck) {
    return "";
  }

  return [
    `CLAIM: ${claim}`,
    `KEY NUMBER: ${keyNumber}`,
    `ASSUMPTION: ${assumption}`,
    `GAP: ${gap}`,
    `CITE CHECK: ${citeCheck}`,
  ].join("\n\n");
}

function getAllowedOrigins() {
  const configuredOrigins = (process.env.ALLOWED_ORIGINS || "")
    .split(",")
    .map((origin) => origin.trim())
    .filter(Boolean);

  return new Set([...DEFAULT_ALLOWED_ORIGINS, ...configuredOrigins]);
}

function applyCorsHeaders(request, response) {
  const origin = request.headers.origin;
  if (!origin) {
    return;
  }

  const allowedOrigins = getAllowedOrigins();
  const isLocalOrigin = /^https?:\/\/(127\.0\.0\.1|localhost)(:\d+)?$/i.test(origin);

  if (!allowedOrigins.has(origin) && !isLocalOrigin) {
    return;
  }

  response.setHeader("access-control-allow-origin", origin);
  response.setHeader("vary", "Origin");
  response.setHeader("access-control-allow-methods", "GET, HEAD, POST, OPTIONS");
  response.setHeader("access-control-allow-headers", "Content-Type");
}

async function readJsonBody(request) {
  return new Promise((resolve, reject) => {
    const chunks = [];
    let size = 0;

    request.on("data", (chunk) => {
      size += chunk.length;

      if (size > MAX_BODY_SIZE) {
        reject(new Error("Request body too large."));
        request.destroy();
        return;
      }

      chunks.push(chunk);
    });

    request.on("end", () => {
      try {
        const raw = Buffer.concat(chunks).toString("utf8");
        resolve(raw ? JSON.parse(raw) : {});
      } catch {
        reject(new Error("Invalid JSON body."));
      }
    });

    request.on("error", reject);
  });
}

async function handleIndex(response) {
  const html = await readFile(path.join(__dirname, "index.html"), "utf8");
  response.writeHead(200, {
    "content-type": "text/html; charset=utf-8",
    "cache-control": "no-store",
  });
  response.end(html);
}

async function handleReview(request, response) {
  const apiKey = process.env.GEMINI_API_KEY;
  if (!apiKey) {
    sendJson(response, 500, {
      error: "Server is missing GEMINI_API_KEY.",
    });
    return;
  }

  const body = await readJsonBody(request);
  const paperText = typeof body.paperText === "string" ? body.paperText.trim() : "";

  if (!paperText) {
    sendJson(response, 400, {
      error: "Paper text is required.",
    });
    return;
  }

  const upstreamResponse = await fetch(API_URL, {
    method: "POST",
    headers: {
      "content-type": "application/json",
      "x-goog-api-key": apiKey,
    },
    body: JSON.stringify({
      contents: [
        {
          parts: [
            {
              text: `${PROMPT_TEMPLATE}${paperText}`,
            },
          ],
        },
      ],
      generationConfig: {
        temperature: 0,
        maxOutputTokens: 1400,
        thinkingConfig: {
          thinkingBudget: 0,
        },
        responseMimeType: "application/json",
        responseJsonSchema: REVIEW_SCHEMA,
      },
    }),
  });

  const payload = await upstreamResponse.json().catch(() => ({}));

  if (!upstreamResponse.ok) {
    const message =
      payload?.error?.message ||
      `Gemini request failed with status ${upstreamResponse.status}.`;

    sendJson(response, upstreamResponse.status, { error: message });
    return;
  }

  const reviewText = Array.isArray(payload.candidates)
    ? payload.candidates
        .flatMap((candidate) => candidate.content?.parts || [])
        .map((part) => part.text || "")
        .join("\n")
        .trim()
    : "";

  if (!reviewText) {
    sendJson(response, 502, {
      error: "Gemini returned an empty review.",
    });
    return;
  }

  let reviewObject;

  try {
    reviewObject = JSON.parse(reviewText);
  } catch {
    sendJson(response, 502, {
      error: "Gemini returned malformed structured output.",
    });
    return;
  }

  const review = formatReview(reviewObject);

  if (!review) {
    sendJson(response, 502, {
      error: "Gemini returned an incomplete review.",
    });
    return;
  }

  sendJson(response, 200, { review });
}

const server = createServer(async (request, response) => {
  try {
    const method = request.method || "GET";
    const url = new URL(request.url || "/", `http://${request.headers.host || "localhost"}`);

    applyCorsHeaders(request, response);

    if (method === "OPTIONS") {
      response.writeHead(204);
      response.end();
      return;
    }

    if (method === "GET" && url.pathname === "/") {
      await handleIndex(response);
      return;
    }

    if (method === "GET" && url.pathname === "/health") {
      sendJson(response, 200, { ok: true });
      return;
    }

    if (method === "POST" && url.pathname === "/api/review") {
      await handleReview(request, response);
      return;
    }

    sendText(response, 404, "Not found");
  } catch (error) {
    const message =
      error instanceof Error ? error.message : "Unexpected server error.";
    sendJson(response, 500, { error: message });
  }
});

server.listen(PORT, () => {
  console.log(`Peer Reviewer listening on http://localhost:${PORT}`);
});