📦 EqualifyEverything / equalify-iris

📄 sessions.ts · 275 lines
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
275import { Router } from "express";
import type { Request, Response, NextFunction } from "express";
import multer from "multer";
import { writeFileSync, readFileSync, existsSync, rmSync } from "node:fs";
import { join } from "node:path";
import { ulid } from "ulid";
import type { IrisConfig } from "../config.ts";
import type { Store, SessionRecord } from "../store/db.ts";
import { Paths } from "../store/paths.ts";
import { runPipeline } from "../pipeline/orchestrator.ts";
import type { AuthedRequest } from "../auth/middleware.ts";
import { sendError } from "./errors.ts";
import { summarizeRun } from "../diagnostics.ts";
import { rasterizePdf, PdfTooLargeError, MAX_PDF_PAGES } from "../util/pdf.ts";
import { outputBasenameFromUploads, convertedHtmlFilename } from "../util/outputNames.ts";

const upload = multer({ storage: multer.memoryStorage(), limits: { fileSize: 50 * 1024 * 1024 } });

// Wrap multer so its errors (e.g. file too large) become clean 400s.
function uploadImages(req: Request, res: Response, next: NextFunction): void {
  upload.array("images")(req, res, (err: unknown) => {
    if (err) {
      sendError(res, 400, "invalid_request", `Upload failed: ${(err as Error).message}`);
      return;
    }
    next();
  });
}

const IMAGE_EXT = /\.(png|jpe?g|tiff?|webp)$/i;
const PDF_EXT = /\.pdf$/i;
const MAX_TOTAL_PAGES = MAX_PDF_PAGES; // overall cap across all uploaded files

// One-line axe-core summary for the PR description (from sessions/<id>/lint.json).
function sessionSummary(s: SessionRecord) {
  return {
    session_id: s.session_id,
    status: s.status,
    image_count: s.image_count,
    created_at: s.created_at,
    updated_at: s.updated_at,
  };
}

// Owned-by-caller lookup. Returns undefined (caller sends 404) when missing or
// owned by another user, so a token cannot probe others' sessions (§9.1).
function ownedSession(store: Store, id: string, userId: number): SessionRecord | undefined {
  const s = store.getSession(id);
  if (!s || s.github_user_id !== userId) return undefined;
  return s;
}

export function sessionsRouter(cfg: IrisConfig, store: Store): Router {
  const r = Router();
  const paths = new Paths(cfg);

  // GET /v1/sessions — list this user's sessions, newest first.
  r.get("/", (req: AuthedRequest, res) => {
    const limit = Math.min(parseInt(String(req.query.limit ?? "20"), 10) || 20, 100);
    const status = req.query.status ? String(req.query.status) : undefined;
    const cursor = req.query.cursor ? String(req.query.cursor) : undefined;
    const rows = store.listSessions(req.user!.github_user_id, { limit: limit + 1, status, cursor });
    const page = rows.slice(0, limit);
    const next = rows.length > limit ? page[page.length - 1].created_at : null;
    res.json({ sessions: page.map(sessionSummary), next_cursor: next });
  });

  // POST /v1/sessions — create a session. Accepts images and/or PDFs; PDFs are
  // rasterized to one image per page, expanded in submitted order (§9.2).
  r.post("/", uploadImages, async (req: AuthedRequest, res) => {
    const files = (req.files as Express.Multer.File[] | undefined) ?? [];
    if (files.length === 0) {
      sendError(res, 400, "invalid_request", "At least one file part named 'images' is required (image or PDF)");
      return;
    }
    for (const f of files) {
      if (!IMAGE_EXT.test(f.originalname) && !PDF_EXT.test(f.originalname)) {
        sendError(res, 400, "invalid_request", `Unsupported file type: ${f.originalname} (allowed: PNG, JPEG, TIFF, WebP, PDF)`);
        return;
      }
    }

    let maxIter = req.user!.max_review_iterations;
    const configPart = (req.body as { config?: string } | undefined)?.config;
    if (configPart) {
      try {
        const parsed = JSON.parse(configPart) as { max_review_iterations?: number };
        if (typeof parsed.max_review_iterations === "number") maxIter = parsed.max_review_iterations;
      } catch {
        sendError(res, 400, "invalid_request", "config part is not valid JSON");
        return;
      }
    }

    // Expand uploads into ordered page images (PDF -> one PNG per page).
    const pages: { name: string; buffer: Buffer }[] = [];
    try {
      for (const f of files) {
        if (PDF_EXT.test(f.originalname)) {
          pages.push(...(await rasterizePdf(f.buffer, f.originalname)));
        } else {
          pages.push({ name: f.originalname, buffer: f.buffer });
        }
      }
    } catch (e) {
      if (e instanceof PdfTooLargeError) {
        sendError(res, 400, "invalid_request", e.message);
      } else {
        sendError(res, 422, "pdf_conversion_failed", `Could not process a PDF: ${(e as Error).message}`);
      }
      return;
    }

    if (pages.length === 0) {
      sendError(res, 400, "invalid_request", "No pages found in the uploaded files");
      return;
    }
    if (pages.length > MAX_TOTAL_PAGES) {
      sendError(res, 400, "invalid_request", `Too many pages (${pages.length}); the maximum is ${MAX_TOTAL_PAGES}.`);
      return;
    }

    const sessionId = `ses_${ulid()}`;
    paths.initSession(sessionId);
    // Remember the source basename so the output title + download filename
    // mirror the upload (e.g. report.pdf -> report_converted.html).
    writeFileSync(paths.sessionSourceName(sessionId), outputBasenameFromUploads(files));
    // Persist page images with an order prefix so submitted order survives (§9.2).
    pages.forEach((p, i) => {
      const order = String(i + 1).padStart(4, "0");
      writeFileSync(join(paths.sessionInput(sessionId), `${order}__${p.name}`), p.buffer);
    });

    const record = store.createSession({
      session_id: sessionId,
      github_user_id: req.user!.github_user_id,
      image_count: pages.length,
      iterations_max: maxIter,
    });

    // Kick off the pipeline asynchronously; clients poll GET /v1/sessions/{id}.
    void runPipeline({ cfg, store, sessionId, maxReviewIterations: maxIter, githubToken: req.token });

    res.status(201).json({
      session_id: record.session_id,
      status: record.status,
      image_count: record.image_count,
      created_at: record.created_at,
    });
  });

  // GET /v1/sessions/{id} — status.
  r.get("/:id", (req: AuthedRequest, res) => {
    const s = ownedSession(store, req.params.id, req.user!.github_user_id);
    if (!s) {
      sendError(res, 404, "session_not_found", "No such session");
      return;
    }
    const body: Record<string, unknown> = {
      session_id: s.session_id,
      status: s.status,
      phase: s.phase,
      iterations_completed: s.iterations_completed,
      iterations_max: s.iterations_max,
      image_count: s.image_count,
      created_at: s.created_at,
      updated_at: s.updated_at,
    };
    if (s.status === "failed" && s.error) body.error = s.error;
    res.json(body);
  });

  // GET /v1/sessions/{id}/output — the HTML document.
  r.get("/:id/output", (req: AuthedRequest, res) => {
    const s = ownedSession(store, req.params.id, req.user!.github_user_id);
    if (!s) {
      sendError(res, 404, "session_not_found", "No such session");
      return;
    }
    if (s.status !== "ready_for_review" && s.status !== "closed") {
      sendError(res, 409, "invalid_state", "Output not available until session is ready_for_review");
      return;
    }
    const outPath = paths.sessionOutput(s.session_id);
    if (!existsSync(outPath)) {
      sendError(res, 409, "invalid_state", "Output not available");
      return;
    }
    // Title + download filename mirror the uploaded file's name.
    const base = existsSync(paths.sessionSourceName(s.session_id))
      ? readFileSync(paths.sessionSourceName(s.session_id), "utf8").trim() || "document"
      : "document";
    const html = readFileSync(outPath, "utf8").replace(
      /<title>[^<]*<\/title>/,
      `<title>${base.replace(/&/g, "&amp;").replace(/</g, "&lt;")}</title>`,
    );
    res.setHeader("Content-Disposition", `inline; filename="${convertedHtmlFilename(base)}"`);
    res.type("text/html").send(html);
  });

  // GET /v1/sessions/{id}/logs — the run log as ndjson.
  r.get("/:id/logs", (req: AuthedRequest, res) => {
    const s = ownedSession(store, req.params.id, req.user!.github_user_id);
    if (!s) {
      sendError(res, 404, "session_not_found", "No such session");
      return;
    }
    const logPath = paths.sessionLog(s.session_id);
    res.type("application/x-ndjson").send(existsSync(logPath) ? readFileSync(logPath, "utf8") : "");
  });

  // GET /v1/sessions/{id}/diagnostics — machine-readable timing/health summary
  // for maintainers (human or AI): phase + per-call durations, the slowest
  // calls, and any in-flight call (the likely culprit when a run seems hung).
  r.get("/:id/diagnostics", (req: AuthedRequest, res) => {
    const s = ownedSession(store, req.params.id, req.user!.github_user_id);
    if (!s) {
      sendError(res, 404, "session_not_found", "No such session");
      return;
    }
    const logPath = paths.sessionLog(s.session_id);
    const text = existsSync(logPath) ? readFileSync(logPath, "utf8") : "";
    res.json(summarizeRun(text, { sessionId: s.session_id, status: s.status, phase: s.phase, now: Date.now() }));
  });

  // POST /v1/sessions/{id}/feedback — re-run within the same session (§7.12).
  r.post("/:id/feedback", (req: AuthedRequest, res) => {
    const s = ownedSession(store, req.params.id, req.user!.github_user_id);
    if (!s) {
      sendError(res, 404, "session_not_found", "No such session");
      return;
    }
    if (s.status !== "ready_for_review") {
      sendError(res, 409, "invalid_state", "Feedback can only be submitted when ready_for_review");
      return;
    }
    const feedback = (req.body as { feedback?: string } | undefined)?.feedback;
    if (!feedback || typeof feedback !== "string") {
      sendError(res, 400, "invalid_request", "feedback (string) is required");
      return;
    }
    store.updateSession(s.session_id, { status: "running", phase: "extraction" });
    void runPipeline({
      cfg,
      store,
      sessionId: s.session_id,
      maxReviewIterations: s.iterations_max,
      feedback,
      githubToken: req.token,
    });
    res.status(202).json({ session_id: s.session_id, status: "running", phase: "extraction" });
  });

  // POST /v1/sessions/{id}/close — finalize the session and clean tmp (§9.2).
  // Agent contributions are auto-filed as GitHub issues during the run (see
  // pipeline/contribute.ts), so close no longer opens PRs.
  r.post("/:id/close", (req: AuthedRequest, res) => {
    const s = ownedSession(store, req.params.id, req.user!.github_user_id);
    if (!s) {
      sendError(res, 404, "session_not_found", "No such session");
      return;
    }
    if (s.status !== "ready_for_review") {
      sendError(res, 409, "invalid_state", "Session is not ready_for_review");
      return;
    }
    const tmp = paths.tmpDir(s.session_id);
    if (existsSync(tmp)) rmSync(tmp, { recursive: true, force: true });
    store.updateSession(s.session_id, { status: "closed" });
    res.json({ session_id: s.session_id, status: "closed" });
  });

  return r;
}