@disintegrator/raven-paseo 0.0.2 → 0.0.3

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@disintegrator/raven-paseo",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "Guided code review with code navigation, as a Paseo workspace panel.",
5
5
  "license": "MIT",
6
6
  "type": "module",
package/server/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // packages/core/src/engine.ts
2
- import { createHash as createHash3 } from "node:crypto";
2
+ import { createHash as createHash3, randomUUID as randomUUID3 } from "node:crypto";
3
3
  import { existsSync as existsSync5 } from "node:fs";
4
4
  import { mkdir as mkdir5, readFile as readFile8, writeFile as writeFile5 } from "node:fs/promises";
5
5
  import { stat as stat4 } from "node:fs/promises";
@@ -1771,6 +1771,27 @@ var Canvas = z2.object({
1771
1771
  panes: z2.array(CanvasPane)
1772
1772
  });
1773
1773
  var emptyCanvas = () => ({ format: CANVAS_FORMAT, version: CANVAS_VERSION, mode: "outline", focus: "home", roots: [], panes: [] });
1774
+ var NoteStop = z2.object({
1775
+ id: z2.string(),
1776
+ path: z2.string(),
1777
+ line: z2.number().int().positive().optional(),
1778
+ endLine: z2.number().int().positive().optional(),
1779
+ body: z2.string(),
1780
+ author: z2.string().optional(),
1781
+ quote: z2.string().optional(),
1782
+ stale: z2.boolean().optional()
1783
+ });
1784
+ var Inquiry = z2.object({
1785
+ id: z2.string(),
1786
+ title: z2.string(),
1787
+ summary: z2.string().optional(),
1788
+ author: z2.string(),
1789
+ at: z2.string(),
1790
+ stops: z2.array(NoteStop)
1791
+ });
1792
+ var NOTES_FORMAT = "raven.notes";
1793
+ var NOTES_VERSION = 1;
1794
+ var Notes = z2.object({ format: z2.literal(NOTES_FORMAT), version: z2.literal(NOTES_VERSION), inquiries: z2.array(Inquiry) });
1774
1795
  var SNAPSHOT_FORMAT = "raven.snapshot";
1775
1796
  var SNAPSHOT_VERSION = 1;
1776
1797
  var Snapshot = z2.object({
@@ -1828,6 +1849,32 @@ var methods = {
1828
1849
  input: z3.object({ changeSetId: z3.string(), title: z3.string().optional(), pullRequest: z3.number().int().optional() }),
1829
1850
  output: Snapshot
1830
1851
  },
1852
+ "notes.get": { input: ChangeSetRef, output: z3.object({ inquiries: z3.array(Inquiry) }) },
1853
+ "notes.put": {
1854
+ input: z3.object({
1855
+ changeSetId: z3.string(),
1856
+ title: z3.string().min(1).max(200),
1857
+ summary: z3.string().max(4000).optional(),
1858
+ author: z3.string().max(80).optional(),
1859
+ stops: z3.array(z3.object({
1860
+ path: z3.string().min(1),
1861
+ line: z3.number().int().positive().optional(),
1862
+ endLine: z3.number().int().positive().optional(),
1863
+ body: z3.string().min(1).max(4000)
1864
+ })).min(1).max(40)
1865
+ }),
1866
+ output: Inquiry
1867
+ },
1868
+ "notes.add": {
1869
+ input: z3.object({
1870
+ changeSetId: z3.string(),
1871
+ inquiry: z3.string().optional(),
1872
+ title: z3.string().min(1).max(200).optional(),
1873
+ stop: z3.object({ path: z3.string().min(1), line: z3.number().int().positive().optional(), endLine: z3.number().int().positive().optional(), body: z3.string().min(1).max(4000) })
1874
+ }),
1875
+ output: Inquiry
1876
+ },
1877
+ "notes.remove": { input: z3.object({ changeSetId: z3.string(), id: z3.string(), stop: z3.string().optional() }), output: z3.object({}) },
1831
1878
  "comment.upsert": {
1832
1879
  input: z3.object({ changeSetId: z3.string(), id: z3.string().optional(), anchor: CommentAnchor, body: z3.string().min(1) }),
1833
1880
  output: Comment
@@ -7538,6 +7585,16 @@ async function makeSnapshot(engine, changeSetId, about = {}) {
7538
7585
  put("history.pullRequest", { number: commit.pr }, await engine.pullRequest(changeSetId, commit.pr).catch(() => ({ unavailable: "it could not be read when this review was shared" })));
7539
7586
  }
7540
7587
  };
7588
+ const inquiries = await engine.inquiries(changeSetId);
7589
+ put("notes.get", {}, { inquiries });
7590
+ const stop = async (path, line) => {
7591
+ const f = inChange.get(path);
7592
+ if (f && (f.newBlob || f.oldBlob))
7593
+ return file(path);
7594
+ const lines = placeLines(line);
7595
+ put("text.read", { path, ...lines }, await engine.read(changeSetId, path, lines.start, lines.end));
7596
+ };
7597
+ await Promise.all(inquiries.flatMap((q) => q.stops).map((s) => stop(s.path, s.line ?? 1).catch(() => {})));
7541
7598
  await Promise.all(canvas.panes.map((p) => pane(p).catch(() => {})));
7542
7599
  return {
7543
7600
  format: SNAPSHOT_FORMAT,
@@ -7970,10 +8027,35 @@ class ReviewStore {
7970
8027
  base;
7971
8028
  writes = Promise.resolve();
7972
8029
  canvasFile;
8030
+ notesFile;
7973
8031
  constructor(commonDir, reviewId, root = stateRoot()) {
7974
8032
  const name = join8(root, repoSlug(commonDir), reviewId.replace(/[^\w.-]/g, "_"));
7975
8033
  this.file = `${name}.jsonl`;
7976
8034
  this.canvasFile = `${name}.canvas.json`;
8035
+ this.notesFile = `${name}.notes.json`;
8036
+ }
8037
+ async inquiries() {
8038
+ if (!existsSync3(this.notesFile))
8039
+ return [];
8040
+ let record;
8041
+ try {
8042
+ record = JSON.parse(await readFile6(this.notesFile, "utf8"));
8043
+ } catch {
8044
+ return [];
8045
+ }
8046
+ if (versionOf(record, NOTES_FORMAT, NOTES_VERSION) === undefined)
8047
+ return [];
8048
+ const parsed = Notes.safeParse(record);
8049
+ return parsed.success ? parsed.data.inquiries : [];
8050
+ }
8051
+ async changeInquiries(change) {
8052
+ const done = this.writes.then(async () => {
8053
+ const inquiries = change(await this.inquiries());
8054
+ await writeWhole(this.notesFile, `${JSON.stringify({ format: NOTES_FORMAT, version: NOTES_VERSION, inquiries }, null, 1)}
8055
+ `);
8056
+ });
8057
+ this.writes = done.catch(() => {});
8058
+ await done;
7977
8059
  }
7978
8060
  load() {
7979
8061
  this.loaded ??= (async () => {
@@ -9168,6 +9250,83 @@ class ReviewEngine {
9168
9250
  this.sets.delete(changeSetId);
9169
9251
  return this.open(set.target);
9170
9252
  }
9253
+ async noteLines(set, stop) {
9254
+ const text = await this.headText(set, stop.path).catch(() => {
9255
+ return;
9256
+ });
9257
+ if (text === undefined)
9258
+ return;
9259
+ if (stop.line === undefined)
9260
+ return "";
9261
+ const lines = text.split(`
9262
+ `);
9263
+ if (lines.at(-1) === "")
9264
+ lines.pop();
9265
+ const end = stop.endLine ?? stop.line;
9266
+ return stop.line <= lines.length && end <= lines.length ? lines.slice(stop.line - 1, end).join(`
9267
+ `) : undefined;
9268
+ }
9269
+ async inquiries(changeSetId) {
9270
+ const set = this.get(changeSetId);
9271
+ const kept = await set.store.inquiries();
9272
+ return Promise.all(kept.map(async (inquiry) => ({
9273
+ ...inquiry,
9274
+ stops: await Promise.all(inquiry.stops.map(async (stop) => {
9275
+ const now = await this.noteLines(set, stop);
9276
+ return now === undefined || stop.line !== undefined && now !== stop.quote ? { ...stop, stale: true } : stop;
9277
+ }))
9278
+ })));
9279
+ }
9280
+ async putInquiry(changeSetId, input) {
9281
+ const set = this.get(changeSetId);
9282
+ const stops = [];
9283
+ for (const [i, s] of input.stops.entries())
9284
+ stops.push(await this.noteStop(set, s, `Stop ${i + 1}`));
9285
+ const title = input.title.trim();
9286
+ const summary = input.summary?.trim();
9287
+ const inquiry = { id: randomUUID3().slice(0, 8), title, ...summary ? { summary } : {}, author: input.author?.trim() || await this.me(set.repo) || "Somebody", at: new Date().toISOString(), stops };
9288
+ await set.store.changeInquiries((all) => {
9289
+ const at = all.findIndex((q) => q.title === title);
9290
+ if (at < 0)
9291
+ return [...all, inquiry];
9292
+ inquiry.id = all[at].id;
9293
+ return all.map((q, k) => k === at ? inquiry : q);
9294
+ });
9295
+ return inquiry;
9296
+ }
9297
+ async noteStop(set, s, name) {
9298
+ const where = `${name} (${s.path}${s.line ? `:${s.line}` : ""})`;
9299
+ if (s.endLine !== undefined && (s.line === undefined || s.endLine < s.line))
9300
+ throw new Error(`${where}: endLine needs a line, and comes after it.`);
9301
+ const stop = { path: s.path.replace(/^\.\//, ""), ...s.line !== undefined ? { line: s.line } : {}, ...s.endLine !== undefined && s.endLine !== s.line ? { endLine: s.endLine } : {} };
9302
+ const quote = await this.noteLines(set, stop);
9303
+ if (quote === undefined)
9304
+ throw new Error(`${where}: ${await this.headText(set, stop.path).then(() => true, () => false) ? "the file has no such lines" : "there is no such file in the repository as it stands"}. Paths are from the repository's root.`);
9305
+ return { id: randomUUID3().slice(0, 8), ...stop, body: s.body.trim(), ...stop.line !== undefined ? { quote } : {} };
9306
+ }
9307
+ async addNote(changeSetId, input) {
9308
+ const set = this.get(changeSetId);
9309
+ const author = await this.me(set.repo) ?? "The reviewer";
9310
+ const stop = { ...await this.noteStop(set, input.stop, "The note"), author };
9311
+ const title = input.title?.trim() || "Notes";
9312
+ let out;
9313
+ await set.store.changeInquiries((all) => {
9314
+ const at = all.findIndex((q) => input.inquiry ? q.id === input.inquiry : q.title === title);
9315
+ if (input.inquiry && at < 0)
9316
+ throw new Error("That line of inquiry is not in this review any more.");
9317
+ out = at < 0 ? { id: randomUUID3().slice(0, 8), title, author, at: new Date().toISOString(), stops: [stop] } : { ...all[at], at: new Date().toISOString(), stops: [...all[at].stops, stop] };
9318
+ return at < 0 ? [...all, out] : all.map((q, k) => k === at ? out : q);
9319
+ });
9320
+ return out;
9321
+ }
9322
+ async removeInquiry(changeSetId, id, stop) {
9323
+ await this.get(changeSetId).store.changeInquiries((all) => all.flatMap((q) => {
9324
+ if (q.id !== id)
9325
+ return [q];
9326
+ const stops = stop ? q.stops.filter((s) => s.id !== stop) : [];
9327
+ return stops.length ? [{ ...q, stops }] : [];
9328
+ }));
9329
+ }
9171
9330
  async upsertComment(changeSetId, input) {
9172
9331
  const text = this.text(changeSetId, input.anchor.path, input.anchor.side);
9173
9332
  const lines = text?.split(`
@@ -9274,6 +9433,10 @@ function handlers(engine) {
9274
9433
  "canvas.get": ({ changeSetId }) => engine.canvas(changeSetId),
9275
9434
  "canvas.set": async ({ changeSetId, canvas }) => (await engine.setCanvas(changeSetId, canvas), {}),
9276
9435
  "snapshot.make": ({ changeSetId, title, pullRequest }) => engine.snapshot(changeSetId, { ...title ? { title } : {}, ...pullRequest ? { pullRequest } : {} }),
9436
+ "notes.get": async ({ changeSetId }) => ({ inquiries: await engine.inquiries(changeSetId) }),
9437
+ "notes.put": ({ changeSetId, ...inquiry }) => engine.putInquiry(changeSetId, inquiry),
9438
+ "notes.add": ({ changeSetId, inquiry, title, stop }) => engine.addNote(changeSetId, { ...inquiry ? { inquiry } : {}, ...title ? { title } : {}, stop }),
9439
+ "notes.remove": async ({ changeSetId, id, stop }) => (await engine.removeInquiry(changeSetId, id, stop), {}),
9277
9440
  "comment.upsert": ({ changeSetId, id, anchor, body }) => engine.upsertComment(changeSetId, { ...id ? { id } : {}, anchor, body }),
9278
9441
  "comment.remove": async ({ changeSetId, id }) => (await engine.removeComment(changeSetId, id), {}),
9279
9442
  "text.read": ({ changeSetId, path, start, end }) => engine.read(changeSetId, path, start, end),
@@ -9440,5 +9603,5 @@ export {
9440
9603
  contribute as default
9441
9604
  };
9442
9605
 
9443
- //# debugId=2D1BD07D3EFAA3E464756E2164756E21
9606
+ //# debugId=41C195D884132EC864756E2164756E21
9444
9607
  //# sourceMappingURL=index.js.map