seqmachines commited on
Commit
f48fffa
·
verified ·
1 Parent(s): 1e6bc82

Deploy Seqcolyte Studio

Browse files
studio/src/app/api/projects/[id]/inputs/upload/route.ts CHANGED
@@ -63,11 +63,44 @@ async function streamToFile(
63
  }
64
 
65
  /**
66
- * Raw-streaming upload. The client POSTs the file as the request body
67
- * (`application/octet-stream`) with the original name in the `x-filename`
68
- * header — no multipart envelope to buffer. FASTQ mates land in `inputs/fastq/`
69
- * and are tracked on the manifest; protocol docs kick off extraction; design
70
- * tables are recorded.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  */
72
  export async function POST(req: Request, ctx: { params: Promise<{ id: string }> }) {
73
  const { id } = await ctx.params;
@@ -78,16 +111,11 @@ export async function POST(req: Request, ctx: { params: Promise<{ id: string }>
78
  return NextResponse.json({ error: "project not found" }, { status: 404 });
79
  }
80
 
81
- const rawName = req.headers.get("x-filename");
82
- if (!rawName) {
83
- return NextResponse.json({ error: "missing x-filename header" }, { status: 400 });
84
- }
85
- let filename: string;
86
- try {
87
- filename = sanitize(decodeURIComponent(rawName));
88
- } catch {
89
- filename = sanitize(rawName);
90
  }
 
91
  const ext = path.extname(filename).toLowerCase();
92
 
93
  if (BLOCKED_EXT.has(ext)) {
@@ -102,14 +130,11 @@ export async function POST(req: Request, ctx: { params: Promise<{ id: string }>
102
  if (!kind) {
103
  return NextResponse.json({ error: `unsupported file type ${ext || "(none)"}` }, { status: 400 });
104
  }
105
- if (!req.body) {
106
- return NextResponse.json({ error: "empty request body" }, { status: 400 });
107
- }
108
 
109
  const cap = isFastq ? MAX_FASTQ_BYTES : MAX_DOC_BYTES;
110
- // Cheap up-front reject when the browser declares the size (it does for a File body).
111
  const declared = Number(req.headers.get("content-length"));
112
- if (Number.isFinite(declared) && declared > cap) {
113
  return NextResponse.json(
114
  { error: `file too large (${(declared / 1e6) | 0} MB > ${(cap / 1e6) | 0} MB) — subsample it first` },
115
  { status: 413 },
@@ -118,7 +143,7 @@ export async function POST(req: Request, ctx: { params: Promise<{ id: string }>
118
 
119
  const rel = isFastq ? `inputs/fastq/${filename}` : `inputs/${filename}`;
120
  await fs.mkdir(inProject(id, path.dirname(rel)), { recursive: true });
121
- const streamed = await streamToFile(req.body, inProject(id, rel), cap);
122
  if ("error" in streamed) {
123
  if (streamed.error === "TOO_LARGE") {
124
  return NextResponse.json(
 
63
  }
64
 
65
  /**
66
+ * Pull the filename + a body stream from either upload format:
67
+ * - raw streaming (current client): `x-filename` header + the octet-stream body;
68
+ * - multipart/form-data (older cached clients, `curl -F`, other callers): the
69
+ * `file` part.
70
+ * Accepting both means a deploy that changes the client wire format can never
71
+ * strand a browser running stale JS — the exact failure that made uploads look
72
+ * like "nothing happened". `multipart` lets the caller skip the Content-Length
73
+ * pre-check (that length includes the envelope, and the body is already read).
74
+ */
75
+ async function readUpload(
76
+ req: Request,
77
+ ): Promise<
78
+ { filename: string; body: ReadableStream<Uint8Array>; multipart: boolean } | { error: string }
79
+ > {
80
+ const ct = req.headers.get("content-type") ?? "";
81
+ if (ct.includes("multipart/form-data")) {
82
+ const form = await req.formData().catch(() => null);
83
+ const file = form?.get("file");
84
+ if (!(file instanceof File)) return { error: "no file in form data" };
85
+ return { filename: file.name, body: file.stream() as ReadableStream<Uint8Array>, multipart: true };
86
+ }
87
+ const rawName = req.headers.get("x-filename");
88
+ if (!rawName) return { error: "missing file (send x-filename header or multipart form-data)" };
89
+ if (!req.body) return { error: "empty request body" };
90
+ let filename: string;
91
+ try {
92
+ filename = decodeURIComponent(rawName);
93
+ } catch {
94
+ filename = rawName;
95
+ }
96
+ return { filename, body: req.body, multipart: false };
97
+ }
98
+
99
+ /**
100
+ * Upload endpoint. Streams the file straight to disk (flat memory) from either a
101
+ * raw octet body or a multipart form. FASTQ mates land in `inputs/fastq/` and are
102
+ * tracked on the manifest; protocol docs kick off extraction; design tables are
103
+ * recorded.
104
  */
105
  export async function POST(req: Request, ctx: { params: Promise<{ id: string }> }) {
106
  const { id } = await ctx.params;
 
111
  return NextResponse.json({ error: "project not found" }, { status: 404 });
112
  }
113
 
114
+ const src = await readUpload(req);
115
+ if ("error" in src) {
116
+ return NextResponse.json({ error: src.error }, { status: 400 });
 
 
 
 
 
 
117
  }
118
+ const filename = sanitize(src.filename);
119
  const ext = path.extname(filename).toLowerCase();
120
 
121
  if (BLOCKED_EXT.has(ext)) {
 
130
  if (!kind) {
131
  return NextResponse.json({ error: `unsupported file type ${ext || "(none)"}` }, { status: 400 });
132
  }
 
 
 
133
 
134
  const cap = isFastq ? MAX_FASTQ_BYTES : MAX_DOC_BYTES;
135
+ // Cheap up-front reject when the size is declared (raw File body sets Content-Length).
136
  const declared = Number(req.headers.get("content-length"));
137
+ if (!src.multipart && Number.isFinite(declared) && declared > cap) {
138
  return NextResponse.json(
139
  { error: `file too large (${(declared / 1e6) | 0} MB > ${(cap / 1e6) | 0} MB) — subsample it first` },
140
  { status: 413 },
 
143
 
144
  const rel = isFastq ? `inputs/fastq/${filename}` : `inputs/${filename}`;
145
  await fs.mkdir(inProject(id, path.dirname(rel)), { recursive: true });
146
+ const streamed = await streamToFile(src.body, inProject(id, rel), cap);
147
  if ("error" in streamed) {
148
  if (streamed.error === "TOO_LARGE") {
149
  return NextResponse.json(
studio/src/app/projects/[id]/page.tsx CHANGED
@@ -3,6 +3,7 @@ import Link from "next/link";
3
  import { notFound } from "next/navigation";
4
  import { ArrowLeft } from "lucide-react";
5
  import { getProject, listRuns, projectExists } from "@/lib/store";
 
6
  import { inProject } from "@/lib/paths";
7
  import { Badge } from "@/components/ui/badge";
8
  import { Workspace } from "@/components/workspace";
@@ -47,9 +48,15 @@ export default async function ProjectPage({
47
  </div>
48
  </div>
49
  <div className="flex shrink-0 items-center gap-2">
50
- <Badge variant="secondary" className="font-mono text-xs">
51
- {project.specId}
52
- </Badge>
 
 
 
 
 
 
53
  <ModeToggle />
54
  </div>
55
  </header>
 
3
  import { notFound } from "next/navigation";
4
  import { ArrowLeft } from "lucide-react";
5
  import { getProject, listRuns, projectExists } from "@/lib/store";
6
+ import { NEW_SPEC_ID } from "@/lib/config";
7
  import { inProject } from "@/lib/paths";
8
  import { Badge } from "@/components/ui/badge";
9
  import { Workspace } from "@/components/workspace";
 
48
  </div>
49
  </div>
50
  <div className="flex shrink-0 items-center gap-2">
51
+ {project.specId === NEW_SPEC_ID ? (
52
+ <Badge variant="outline" className="text-muted-foreground text-xs">
53
+ new
54
+ </Badge>
55
+ ) : (
56
+ <Badge variant="secondary" className="font-mono text-xs">
57
+ {project.specId}
58
+ </Badge>
59
+ )}
60
  <ModeToggle />
61
  </div>
62
  </header>
studio/src/lib/config.ts CHANGED
@@ -42,3 +42,11 @@ export const assets = {
42
 
43
  export const DEFAULT_ASSAY = "10x Chromium Single Cell 3' Gene Expression";
44
  export const DEFAULT_SPEC_ID = "10x_3p_v3";
 
 
 
 
 
 
 
 
 
42
 
43
  export const DEFAULT_ASSAY = "10x Chromium Single Cell 3' Gene Expression";
44
  export const DEFAULT_SPEC_ID = "10x_3p_v3";
45
+
46
+ /**
47
+ * A brand-new project has no identified library structure yet. It stays labelled
48
+ * "new" until a protocol is extracted; only then does it adopt a known
49
+ * technology's id (or fall back to "new" for a custom structure).
50
+ */
51
+ export const NEW_ASSAY = "New library — describe or upload a protocol";
52
+ export const NEW_SPEC_ID = "new";
studio/src/lib/extractRunner.ts CHANGED
@@ -1,12 +1,37 @@
1
  import { promises as fs } from "node:fs";
 
2
  import { spawn } from "node:child_process";
3
  import { CLAUDE_BIN, DEFAULT_MODEL, DEFAULT_SPEC_ID, PYTHON, REPO_ROOT } from "./config";
4
- import { inProject } from "./paths";
 
5
  import { getProject, updateProject } from "./store";
6
  import { appendConversation, readConversation } from "./chat";
7
  import { spawnLogged } from "./spawn";
8
  import type { ProjectManifest, SpecDoc } from "./types";
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  /**
11
  * Extract a spec from an uploaded protocol doc via `python -m extract from-doc`.
12
  * A one-off (not the QC RunRecord model): mirrors runner.ts's spawn/log/update
@@ -262,7 +287,9 @@ async function driveExtract(
262
  specConfirmed: false,
263
  };
264
  if (spec.assay) patch.assay = spec.assay;
265
- if (spec.spec_id) patch.specId = spec.spec_id;
 
 
266
 
267
  // Auto-name only while still "Untitled": "{sample} · {assay}" when the user
268
  // described a sample, else the assay alone.
 
1
  import { promises as fs } from "node:fs";
2
+ import path from "node:path";
3
  import { spawn } from "node:child_process";
4
  import { CLAUDE_BIN, DEFAULT_MODEL, DEFAULT_SPEC_ID, PYTHON, REPO_ROOT } from "./config";
5
+ import { assertSafeId, inProject } from "./paths";
6
+ import { techSpecPath } from "./technologies";
7
  import { getProject, updateProject } from "./store";
8
  import { appendConversation, readConversation } from "./chat";
9
  import { spawnLogged } from "./spawn";
10
  import type { ProjectManifest, SpecDoc } from "./types";
11
 
12
+ /**
13
+ * Is this spec id a *known* library structure — one the /technologies gallery
14
+ * lists, or a packaged reference spec? If so a project adopts it as its tag;
15
+ * otherwise the extracted structure is custom and the project stays "new".
16
+ */
17
+ async function isKnownStructure(specId: string): Promise<boolean> {
18
+ const exists = async (p: string) => {
19
+ try {
20
+ await fs.access(p);
21
+ return true;
22
+ } catch {
23
+ return false;
24
+ }
25
+ };
26
+ try {
27
+ if (await exists(techSpecPath(specId))) return true; // spec/technologies/{id}.json
28
+ if (await exists(path.join(REPO_ROOT, "spec", `${assertSafeId(specId)}.json`))) return true;
29
+ } catch {
30
+ /* assertSafeId rejected an unsafe id → treat as unknown */
31
+ }
32
+ return false;
33
+ }
34
+
35
  /**
36
  * Extract a spec from an uploaded protocol doc via `python -m extract from-doc`.
37
  * A one-off (not the QC RunRecord model): mirrors runner.ts's spawn/log/update
 
287
  specConfirmed: false,
288
  };
289
  if (spec.assay) patch.assay = spec.assay;
290
+ // Adopt the identified id only when it's a known structure; a custom library
291
+ // keeps the neutral "new" tag rather than inventing a fake catalog id.
292
+ if (spec.spec_id && (await isKnownStructure(spec.spec_id))) patch.specId = spec.spec_id;
293
 
294
  // Auto-name only while still "Untitled": "{sample} · {assay}" when the user
295
  // described a sample, else the assay alone.
studio/src/lib/store.ts CHANGED
@@ -1,7 +1,7 @@
1
  import { promises as fs } from "node:fs";
2
  import crypto from "node:crypto";
3
  import path from "node:path";
4
- import { DEFAULT_ASSAY, DEFAULT_SPEC_ID, STORE_ROOT } from "./config";
5
  import { assertSafeId, projectDir, runDir } from "./paths";
6
  import type { ProjectManifest, RunRecord } from "./types";
7
 
@@ -82,8 +82,8 @@ export async function createProject(input: { name: string }): Promise<ProjectMan
82
  schemaVersion: "studio.project.v1",
83
  id,
84
  name: input.name.trim() || id,
85
- assay: DEFAULT_ASSAY,
86
- specId: DEFAULT_SPEC_ID,
87
  createdAt: now,
88
  updatedAt: now,
89
  activeSpecPath: null,
 
1
  import { promises as fs } from "node:fs";
2
  import crypto from "node:crypto";
3
  import path from "node:path";
4
+ import { NEW_ASSAY, NEW_SPEC_ID, STORE_ROOT } from "./config";
5
  import { assertSafeId, projectDir, runDir } from "./paths";
6
  import type { ProjectManifest, RunRecord } from "./types";
7
 
 
82
  schemaVersion: "studio.project.v1",
83
  id,
84
  name: input.name.trim() || id,
85
+ assay: NEW_ASSAY,
86
+ specId: NEW_SPEC_ID,
87
  createdAt: now,
88
  updatedAt: now,
89
  activeSpecPath: null,
studio/tsconfig.tsbuildinfo CHANGED
The diff for this file is too large to render. See raw diff