@hexdecimal16/nexo 0.4.7 → 0.4.9

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 (52) hide show
  1. package/cli/src/areas.js +49 -0
  2. package/cli/src/common.js +1 -1
  3. package/cli/src/home-migrate.js +168 -0
  4. package/cli/src/main.js +6 -0
  5. package/cli/src/optimize.js +2 -5
  6. package/cli/src/ui.js +1 -1
  7. package/core/src/config/schema.js +1 -0
  8. package/core/src/engine/charged-completion.js +9 -4
  9. package/core/src/engine/errors.js +29 -2
  10. package/core/src/engine/prompts.js +1 -1
  11. package/core/src/engine/run.js +11 -6
  12. package/core/src/index.js +1 -0
  13. package/core/src/learn/areas.js +192 -0
  14. package/core/src/learn/job.js +15 -1
  15. package/core/src/learn/wiki.js +26 -18
  16. package/core/src/optimize/climb.js +0 -2
  17. package/core/src/optimize/optimize.js +2 -5
  18. package/core/src/optimize/scratchpad-wiki.js +7 -3
  19. package/optimize/src/engine.js +1 -22
  20. package/optimize/src/index.js +0 -1
  21. package/optimize/src/report.js +0 -2
  22. package/package.json +1 -1
  23. package/skills/bundled/wiki/SKILL.md +23 -13
  24. package/store/src/eval-cases.js +12 -6
  25. package/store/src/paths.js +2 -2
  26. package/tools/src/home-folders.js +36 -0
  27. package/tools/src/index.js +2 -0
  28. package/tools/src/wiki-page.js +78 -0
  29. package/tui/src/questions.js +31 -0
  30. package/web/public/area.html +23 -0
  31. package/web/public/cost.html +1 -1
  32. package/web/public/home.html +23 -0
  33. package/web/public/index.html +1 -1
  34. package/web/public/judge.html +1 -1
  35. package/web/public/learning.html +1 -1
  36. package/web/public/lib/api-types.ts +0 -2
  37. package/web/public/lib/optim-view.ts +1 -1
  38. package/web/public/lib/wikis-view.ts +3 -1
  39. package/web/public/optim.html +1 -1
  40. package/web/public/page.html +24 -0
  41. package/web/public/pages/area.ts +59 -0
  42. package/web/public/pages/home.ts +66 -0
  43. package/web/public/pages/page.ts +62 -0
  44. package/web/public/pages/wikis.ts +43 -6
  45. package/web/public/run.html +1 -1
  46. package/web/public/sessions.html +1 -1
  47. package/web/public/style.css +58 -0
  48. package/web/public/wikis.html +1 -1
  49. package/web/src/api.js +14 -9
  50. package/web/src/home-api.js +156 -0
  51. package/web/src/server.js +16 -0
  52. package/optimize/src/merge.js +0 -73
@@ -10,7 +10,7 @@ const pageBox = el<HTMLDivElement>("#wiki-page-container");
10
10
 
11
11
  let currentWikis: string[] = [];
12
12
  let activeWiki: string | null = null;
13
- let activePath: string = "index.md";
13
+ let activePath: string = "index";
14
14
 
15
15
  function showError(error: unknown): void {
16
16
  errorBox.innerHTML = renderError(asErrorBody(error)).value;
@@ -18,14 +18,13 @@ function showError(error: unknown): void {
18
18
 
19
19
  function parseHash(): { wiki: string | null; pagePath: string } {
20
20
  const hash = window.location.hash.replace(/^#\/?/, "");
21
- if (!hash) return { wiki: null, pagePath: "index.md" };
21
+ if (!hash) return { wiki: null, pagePath: "index" };
22
22
  const slash = hash.indexOf("/");
23
23
  if (slash === -1) {
24
- return { wiki: decodeURIComponent(hash), pagePath: "index.md" };
24
+ return { wiki: decodeURIComponent(hash), pagePath: "index" };
25
25
  }
26
26
  const wiki = decodeURIComponent(hash.slice(0, slash));
27
- let page = decodeURIComponent(hash.slice(slash + 1));
28
- if (!page.endsWith(".md")) page += ".md";
27
+ const page = decodeURIComponent(hash.slice(slash + 1));
29
28
  return { wiki, pagePath: page };
30
29
  }
31
30
 
@@ -41,6 +40,7 @@ async function loadPage(wiki: string, path: string): Promise<void> {
41
40
  ]);
42
41
  treeBox.innerHTML = renderWikiTree(pagesData.pages, wiki, path).value;
43
42
  pageBox.innerHTML = renderWikiPage(pageData, wiki).value;
43
+ mountHtmlPage(pageData, wiki);
44
44
  } catch (error) {
45
45
  // If index.md doesn't exist, try loading the first available page
46
46
  try {
@@ -51,6 +51,7 @@ async function loadPage(wiki: string, path: string): Promise<void> {
51
51
  activePath = firstPage.path;
52
52
  const pageData = await getJson<WikiPageResponse>(`/api/wikis/${encodeURIComponent(wiki)}/page?path=${encodeURIComponent(firstPage.path)}`);
53
53
  pageBox.innerHTML = renderWikiPage(pageData, wiki).value;
54
+ mountHtmlPage(pageData, wiki);
54
55
  } else {
55
56
  pageBox.innerHTML = '<p class="muted">No pages found in this wiki.</p>';
56
57
  }
@@ -60,7 +61,7 @@ async function loadPage(wiki: string, path: string): Promise<void> {
60
61
  }
61
62
  }
62
63
 
63
- async function loadWiki(wiki: string, pagePath = "index.md"): Promise<void> {
64
+ async function loadWiki(wiki: string, pagePath = "index"): Promise<void> {
64
65
  activeWiki = wiki;
65
66
  await loadPage(wiki, pagePath);
66
67
  }
@@ -100,4 +101,40 @@ selectorBox.addEventListener("change", (event) => {
100
101
  }
101
102
  });
102
103
 
104
+ const DROP_TAGS = "script,style,iframe,object,embed,form,link,meta,base";
105
+
106
+ /** Renders an HTML wiki page inline: keeps the page body, removes active content, and rewrites relative links to viewer hashes. */
107
+ function mountHtmlPage(page: WikiPageResponse, wiki: string): void {
108
+ if (!page.path.endsWith(".html")) return;
109
+ const host = pageBox.querySelector<HTMLDivElement>("#wiki-html-body");
110
+ if (!host) return;
111
+ const doc = new DOMParser().parseFromString(page.content, "text/html");
112
+ const root = doc.querySelector("main") ?? doc.body;
113
+ root.querySelectorAll(DROP_TAGS).forEach((n) => n.remove());
114
+ root.querySelectorAll(":scope > nav, :scope > p.meta").forEach((n) => n.remove());
115
+ root.querySelectorAll("*").forEach((el) => {
116
+ for (const attr of Array.from(el.attributes)) {
117
+ const name = attr.name.toLowerCase();
118
+ if (name.startsWith("on") || name === "style") el.removeAttribute(attr.name);
119
+ else if ((name === "href" || name === "src" || name === "xlink:href") && /^\s*(javascript|data):/i.test(attr.value)) el.removeAttribute(attr.name);
120
+ }
121
+ });
122
+ const base = page.path.includes("/") ? page.path.slice(0, page.path.lastIndexOf("/") + 1) : "";
123
+ root.querySelectorAll<HTMLAnchorElement>("a[href]").forEach((a) => {
124
+ const href = a.getAttribute("href") ?? "";
125
+ if (/^(https?:|mailto:)/i.test(href)) {
126
+ a.target = "_blank";
127
+ a.rel = "noopener noreferrer";
128
+ return;
129
+ }
130
+ if (href.startsWith("#")) return;
131
+ const url = new URL(href, `https://wiki.invalid/${encodeURIComponent(wiki)}/${base}`);
132
+ const parts = url.pathname.split("/").filter(Boolean).map(decodeURIComponent);
133
+ const targetWiki = parts.shift() ?? wiki;
134
+ const slug = parts.join("/").replace(/\.(html|md)$/, "");
135
+ a.setAttribute("href", `#${encodeURIComponent(targetWiki)}/${slug.split("/").map(encodeURIComponent).join("/")}`);
136
+ });
137
+ host.replaceChildren(...Array.from(root.childNodes).map((n) => document.importNode(n, true)));
138
+ }
139
+
103
140
  loadAll().catch(showError);
@@ -12,7 +12,7 @@
12
12
  </head>
13
13
  <body>
14
14
  <a class="skip" href="#main">Skip to content</a>
15
- <nav class="top" aria-label="Primary"><a href="/" class="brand">nexo</a><a href="/">Runs</a><a href="/sessions.html">Sessions</a><a href="/cost.html">Cost</a><a href="/learning.html">Learning</a><a href="/judge.html">Judge</a><a href="/optim.html">Optimization</a><a href="/wikis.html">Wikis</a></nav>
15
+ <nav class="top" aria-label="Primary"><a href="/" class="brand">nexo</a><a href="/home.html">Home</a><a href="/">Runs</a><a href="/sessions.html">Sessions</a><a href="/cost.html">Cost</a><a href="/learning.html">Learning</a><a href="/judge.html">Judge</a><a href="/optim.html">Optimization</a></nav>
16
16
  <main id="main" tabindex="-1">
17
17
  <nav class="run-nav" aria-label="Run sections"><a href="#sec-timeline">Timeline</a><a href="#sec-spans">Spans</a><a href="#sec-scores">Scores</a><a href="#sec-memories">Memories</a><a href="#sec-subagents">Subagents</a><a href="#sec-files">Files</a><a href="#sec-review">Review</a></nav>
18
18
  <div id="error"></div>
@@ -12,7 +12,7 @@
12
12
  </head>
13
13
  <body>
14
14
  <a class="skip" href="#main">Skip to content</a>
15
- <nav class="top" aria-label="Primary"><a href="/" class="brand">nexo</a><a href="/">Runs</a><a href="/sessions.html">Sessions</a><a href="/cost.html">Cost</a><a href="/learning.html">Learning</a><a href="/judge.html">Judge</a><a href="/optim.html">Optimization</a><a href="/wikis.html">Wikis</a></nav>
15
+ <nav class="top" aria-label="Primary"><a href="/" class="brand">nexo</a><a href="/home.html">Home</a><a href="/">Runs</a><a href="/sessions.html">Sessions</a><a href="/cost.html">Cost</a><a href="/learning.html">Learning</a><a href="/judge.html">Judge</a><a href="/optim.html">Optimization</a></nav>
16
16
  <main id="main" tabindex="-1">
17
17
  <h1>Sessions</h1>
18
18
  <p class="muted">Each chat message is a turn. Open a session to see its turns and review them.</p>
@@ -347,3 +347,61 @@ section[id] { scroll-margin-top: 96px; }
347
347
  .nx-reply:empty { display: none; }
348
348
  .nx-answer { margin: 6px 0; overflow-wrap: anywhere; white-space: pre-wrap; }
349
349
  .nx-links { margin: 0 0 6px; padding-left: 18px; }
350
+
351
+ /* HTML wiki pages rendered inline (packages/web/public/pages/wikis.ts mountHtmlPage) */
352
+ .wiki-html .tldr { background: var(--bg); border: 1px solid var(--line); border-left: 4px solid var(--pen); padding: 12px 16px; border-radius: 4px; margin: 12px 0 16px; }
353
+ .wiki-html table { border-collapse: collapse; width: 100%; margin: 12px 0; font-size: 13px; }
354
+ .wiki-html th, .wiki-html td { border: 1px solid var(--line); padding: 6px 10px; text-align: left; vertical-align: top; }
355
+ .wiki-html th { background: var(--bg); font-weight: 600; }
356
+ .wiki-html pre { margin: 12px 0; padding: 10px 14px; background: var(--bg); border: 1px solid var(--line); border-radius: 2px; overflow-x: auto; }
357
+ .wiki-html pre code { background: none; border: none; padding: 0; }
358
+ .wiki-html details { border: 1px solid var(--line); border-radius: 4px; padding: 8px 12px; margin: 8px 0; }
359
+ .wiki-html summary { cursor: pointer; font-weight: 600; }
360
+ .wiki-html svg { max-width: 100%; height: auto; }
361
+ .wiki-html a { color: var(--pen); text-decoration: underline; text-underline-offset: 3px; }
362
+ .wiki-html .dead { color: var(--muted); border-bottom: 1px dotted var(--muted); }
363
+ .wiki-html .note { border-left: 4px solid var(--line); padding: 8px 14px; background: var(--bg); }
364
+ .wiki-html figure { margin: 16px 0; }
365
+ .wiki-html figure svg { background: #fff; border: 1px solid var(--line); border-radius: 4px; }
366
+ .wiki-html figcaption { color: var(--muted); font-size: 12px; margin-top: 6px; }
367
+
368
+ /* Home page (packages/web/public/pages/home.ts) */
369
+ .home-search { width: 100%; max-width: 640px; padding: 8px 12px; margin: 8px 0 16px; border: 1px solid var(--line); border-radius: 4px; font: inherit; background: var(--bg); color: var(--fg); }
370
+ .home-areas { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 12px; }
371
+ .home-area { border: 1px solid var(--line); border-radius: 6px; padding: 12px 14px; }
372
+ .home-area summary { cursor: pointer; }
373
+ .home-area h3 { font-size: 13px; margin: 14px 0 4px; }
374
+ .home-scope { display: block; font-size: 12px; margin-top: 4px; }
375
+ .home-list { margin: 4px 0; padding-left: 18px; }
376
+ .home-list li { margin: 4px 0; }
377
+ .home-tag { font-size: 11px; border: 1px solid var(--line); border-radius: 3px; padding: 0 4px; color: var(--muted); }
378
+ .home-note { border-left: 4px solid var(--pen); padding: 6px 12px; background: var(--bg); }
379
+ a.home-area { display: block; color: inherit; text-decoration: none; }
380
+ a.home-area:hover, a.home-area:focus-visible { border-color: var(--pen); }
381
+
382
+ /* Area page (pages/area.ts) */
383
+ .area-stats { display: flex; flex-wrap: wrap; gap: 12px; margin: 14px 0 6px; }
384
+ .area-stat { border: 1px solid var(--line); border-radius: 6px; padding: 10px 16px; min-width: 120px; }
385
+ .area-stat b { display: block; font-size: 22px; line-height: 28px; }
386
+ .area-stat span { font-size: 12px; }
387
+ .area-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 12px; }
388
+ a.area-card { display: block; border: 1px solid var(--line); border-radius: 6px; padding: 12px 14px; color: inherit; text-decoration: none; }
389
+ a.area-card:hover, a.area-card:focus-visible { border-color: var(--pen); }
390
+ .area-card .muted { display: block; font-size: 12px; margin-top: 2px; }
391
+ .area-card-text { display: block; font-size: 13px; margin-top: 6px; }
392
+ .area-timeline { list-style: none; margin: 8px 0 8px 6px; padding: 0; border-left: 2px solid var(--line); }
393
+ .area-timeline li { position: relative; padding: 2px 0 14px 18px; }
394
+ .area-timeline li::before { content: ""; position: absolute; left: -7px; top: 6px; width: 12px; height: 12px; border-radius: 50%; background: var(--pen); }
395
+ .area-timeline time { display: block; font-size: 12px; color: var(--muted); }
396
+ .area-decision { border: 1px solid var(--line); border-left: 4px solid var(--ok); border-radius: 4px; padding: 10px 14px; margin: 8px 0; }
397
+ .area-decision span, .area-decision time { display: block; font-size: 13px; margin-top: 2px; }
398
+ .area-table { border-collapse: collapse; width: 100%; font-size: 13px; }
399
+ .area-table th, .area-table td { border-bottom: 1px solid var(--line); padding: 6px 10px; text-align: left; }
400
+
401
+ /* Converted wiki pages: top-level bullets as cards, clearer sections */
402
+ .wiki-html h2 { border-top: 1px solid var(--line); padding-top: 16px; margin-top: 24px; }
403
+ .wiki-html > ul { list-style: none; padding-left: 0; }
404
+ .wiki-html > ul > li { border: 1px solid var(--line); border-left: 3px solid var(--pen); border-radius: 4px; padding: 8px 12px; margin: 8px 0; background: var(--bg); }
405
+ .wiki-html > ul > li > ul { margin-top: 6px; }
406
+ .wiki-html tbody tr:nth-child(even) td { background: var(--bg); }
407
+ .feed-page { font-size: 12px; border: 1px solid var(--line); border-radius: 3px; padding: 0 5px; text-decoration: none; }
@@ -12,7 +12,7 @@
12
12
  </head>
13
13
  <body>
14
14
  <a class="skip" href="#main">Skip to content</a>
15
- <nav class="top" aria-label="Primary"><a href="/" class="brand">nexo</a><a href="/">Runs</a><a href="/sessions.html">Sessions</a><a href="/cost.html">Cost</a><a href="/learning.html">Learning</a><a href="/judge.html">Judge</a><a href="/optim.html">Optimization</a><a href="/wikis.html" aria-current="page">Wikis</a></nav>
15
+ <nav class="top" aria-label="Primary"><a href="/" class="brand">nexo</a><a href="/home.html">Home</a><a href="/">Runs</a><a href="/sessions.html">Sessions</a><a href="/cost.html">Cost</a><a href="/learning.html">Learning</a><a href="/judge.html">Judge</a><a href="/optim.html">Optimization</a></nav>
16
16
  <main id="main" tabindex="-1">
17
17
  <h1>Wikis</h1>
18
18
  <div id="error"></div>
package/web/src/api.js CHANGED
@@ -2,7 +2,7 @@ import { closeSync, constants, createReadStream, existsSync, fstatSync, lstatSyn
2
2
  import { extname, join, relative, sep } from "node:path";
3
3
  import { agreementStats, approveOptimRun, listEvalCasesByStatus, reviewProposedCase, checkPromotion, getArtifact, getOptimRun, listOptimCandidates, queryOptimRuns, confusionMatrix, COST_DIMENSIONS, costReport, descendantRunIds, getRun, getRunWithSummary, getSpan, invalidRequest, isRunStatus, judgeDisagrees, judgeHumanPairs, latestFeedback, listCalibrations, listChildSummaries, listDisagreements, listFeedback, listScores, querySessionSummaries, MAX_RUN_PAGE, listSpans, loadSpanTree, queryRuns, RUN_STATUSES, readRunJson, recordFeedback, runNotFound, countLearnJobs, getMemory, getPlaybookItem, isSkillName, listArtifactVersions, queryLearnJobs, queryMemories, listPlaybookItems, listPlaybookSkills, memoryHistory, playbookItemHistory, publishPlaybook, retireMemory, retirePlaybookItem, transaction, resolveNexoPaths, } from "../../store/src/index.js";
4
4
  import { NexoError } from "../../telemetry/src/index.js";
5
- import { resolveConfined } from "../../tools/src/index.js";
5
+ import { resolveConfined, isWikiPageFile, parseWikiMeta, resolveWikiPagePath, wikiPageSlug } from "../../tools/src/index.js";
6
6
  import { SECURITY_HEADERS } from "./http-errors.js";
7
7
  /** Files larger than this are cut unless `?full=1` is passed; the response carries `x-nexo-truncated: <size>`. */
8
8
  export const MAX_FILE_PREVIEW_BYTES = 1024 * 1024;
@@ -557,6 +557,9 @@ export function reviewEvalCaseResponse(ctx, id, decision) {
557
557
  return { case: evalCaseItem(reviewProposedCase(ctx.db, id, decision, { redact: ctx.redact, nowIso: ctx.now().toISOString() })) };
558
558
  }
559
559
  function parseSummary(content) {
560
+ const metaSummary = parseWikiMeta(content).summary;
561
+ if (metaSummary)
562
+ return metaSummary;
560
563
  const head = content.slice(0, 2048);
561
564
  const match = /^---\r?\n([\s\S]*?)\r?\n---/.exec(head);
562
565
  if (!match || !match[1])
@@ -571,7 +574,7 @@ function parseSummary(content) {
571
574
  }
572
575
  /** `GET /api/wikis`: lists all wiki directories under the wikis path. */
573
576
  export function listWikisResponse(ctx) {
574
- const dir = ctx.wikisDir ?? resolveNexoPaths().wikis;
577
+ const dir = ctx.wikisDir ?? resolveNexoPaths().homeRoot;
575
578
  if (!existsSync(dir))
576
579
  return { wikis: [] };
577
580
  const wikis = [];
@@ -592,7 +595,7 @@ export function listWikisResponse(ctx) {
592
595
  if (!f.startsWith(".") && f !== ".git")
593
596
  scan(fp);
594
597
  }
595
- else if (f.endsWith(".md")) {
598
+ else if (isWikiPageFile(f)) {
596
599
  pageCount++;
597
600
  if (fst.mtime > maxModified)
598
601
  maxModified = fst.mtime;
@@ -619,7 +622,7 @@ export function listWikisResponse(ctx) {
619
622
  }
620
623
  /** `GET /api/wikis/{name}/pages`: lists all markdown pages in a wiki with summary metadata. */
621
624
  export function listWikiPagesResponse(ctx, wikiName) {
622
- const root = ctx.wikisDir ?? resolveNexoPaths().wikis;
625
+ const root = ctx.wikisDir ?? resolveNexoPaths().homeRoot;
623
626
  let wikiDir;
624
627
  try {
625
628
  wikiDir = resolveConfined(root, wikiName);
@@ -644,9 +647,9 @@ export function listWikiPagesResponse(ctx, wikiName) {
644
647
  if (st.isDirectory()) {
645
648
  walk(fp);
646
649
  }
647
- else if (f.endsWith(".md")) {
650
+ else if (isWikiPageFile(f)) {
648
651
  const rel = relative(wikiDir, fp).split(sep).join("/");
649
- const slug = rel.slice(0, -3);
652
+ const slug = wikiPageSlug(rel);
650
653
  const folder = rel.includes("/") ? rel.slice(0, rel.lastIndexOf("/")) : undefined;
651
654
  const content = readFileSync(fp, "utf8");
652
655
  const summary = parseSummary(content);
@@ -673,7 +676,7 @@ export function listWikiPagesResponse(ctx, wikiName) {
673
676
  export function wikiPageResponse(ctx, wikiName, relPath) {
674
677
  if (relPath.includes("\0"))
675
678
  throw invalidRequest("Path contains a NUL byte");
676
- const root = ctx.wikisDir ?? resolveNexoPaths().wikis;
679
+ const root = ctx.wikisDir ?? resolveNexoPaths().homeRoot;
677
680
  let wikiDir;
678
681
  try {
679
682
  wikiDir = resolveConfined(root, wikiName);
@@ -688,8 +691,10 @@ export function wikiPageResponse(ctx, wikiName, relPath) {
688
691
  throw new NexoError({ code: "NOT_FOUND", message: `Wiki "${wikiName}" does not exist`, context: { wiki: wikiName } });
689
692
  }
690
693
  let target;
694
+ let resolvedPath = relPath;
691
695
  try {
692
- target = resolveConfined(wikiDir, relPath);
696
+ resolvedPath = resolveWikiPagePath(wikiDir, relPath);
697
+ target = resolveConfined(wikiDir, resolvedPath);
693
698
  }
694
699
  catch (error) {
695
700
  if (error instanceof NexoError && error.code === "TOOL_DENIED") {
@@ -705,7 +710,7 @@ export function wikiPageResponse(ctx, wikiName, relPath) {
705
710
  const summary = parseSummary(content);
706
711
  return {
707
712
  wiki: wikiName,
708
- path: relPath,
713
+ path: resolvedPath,
709
714
  content,
710
715
  summary,
711
716
  size: st.size,
@@ -0,0 +1,156 @@
1
+ import { existsSync, readdirSync, readFileSync, realpathSync, statSync } from "node:fs";
2
+ import { join, relative, sep } from "node:path";
3
+ import { listSessionRows, resolveNexoPaths } from "../../store/src/index.js";
4
+ import { NexoError } from "../../telemetry/src/index.js";
5
+ import { listHomeFolders, parseWikiMeta, resolveConfined } from "../../tools/src/index.js";
6
+ const FEED_LIMIT = 50;
7
+ const DECISION_LIMIT = 50;
8
+ /** Reads a JSON-lines file newest first; unparseable lines are skipped. */
9
+ function readJsonl(file, limit) {
10
+ if (!existsSync(file))
11
+ return [];
12
+ const out = [];
13
+ for (const line of readFileSync(file, "utf8").split("\n").reverse()) {
14
+ if (!line.trim())
15
+ continue;
16
+ try {
17
+ out.push(JSON.parse(line));
18
+ }
19
+ catch {
20
+ // skip a torn or hand-edited line
21
+ }
22
+ if (out.length >= limit)
23
+ break;
24
+ }
25
+ return out;
26
+ }
27
+ function countPages(dir) {
28
+ let n = 0;
29
+ const walk = (d) => {
30
+ for (const f of readdirSync(d)) {
31
+ if (f.startsWith(".") || f === "raw")
32
+ continue;
33
+ const p = join(d, f);
34
+ if (statSync(p).isDirectory())
35
+ walk(p);
36
+ else if (f.endsWith(".html") || f.endsWith(".md"))
37
+ n++;
38
+ }
39
+ };
40
+ try {
41
+ walk(dir);
42
+ }
43
+ catch {
44
+ // unreadable wiki: report what was counted
45
+ }
46
+ return n;
47
+ }
48
+ /** `GET /api/home`: approved areas with their wikis, the feed, decisions and recent sessions, read fresh on every call. */
49
+ export function homeResponse(ctx) {
50
+ const dir = ctx.wikisDir ?? resolveNexoPaths().homeRoot;
51
+ let raw = {};
52
+ try {
53
+ raw = JSON.parse(readFileSync(join(dir, "areas.json"), "utf8"));
54
+ }
55
+ catch {
56
+ raw = {};
57
+ }
58
+ const all = Array.isArray(raw.areas) ? raw.areas : [];
59
+ const active = all.filter((a) => a.status === "active");
60
+ const feed = readJsonl(join(dir, "feed.jsonl"), FEED_LIMIT).map((f) => ({ ...f, text: ctx.redact(String(f.text ?? "")) }));
61
+ const decisions = readJsonl(join(dir, "decisions.jsonl"), DECISION_LIMIT).map((d) => ({ ...d, choice: ctx.redact(String(d.choice ?? "")), reason: ctx.redact(String(d.reason ?? "")) }));
62
+ const folders = new Map(listHomeFolders(dir).map((f) => [f.name, f.dir]));
63
+ const weekAgo = ctx.now().getTime() - 7 * 86_400_000;
64
+ const areas = active.map((a) => ({
65
+ id: a.id,
66
+ name: a.name,
67
+ scope: a.scope,
68
+ wikis: (a.wikis ?? [])
69
+ .filter((w) => folders.has(w))
70
+ .map((w) => {
71
+ let summary;
72
+ try {
73
+ summary = parseWikiMeta(readFileSync(join(folders.get(w), "overview.html"), "utf8")).summary;
74
+ }
75
+ catch {
76
+ summary = undefined;
77
+ }
78
+ return { name: w, summary, pageCount: countPages(folders.get(w)), path: relative(dir, folders.get(w)).split(sep).join("/") };
79
+ }),
80
+ recent: feed.filter((f) => f.areaId === a.id && Date.parse(f.at) >= weekAgo).length,
81
+ }));
82
+ const sessionArea = new Map();
83
+ for (const f of [...feed].reverse())
84
+ if (f.sessionId && f.areaId)
85
+ sessionArea.set(f.sessionId, f.areaId);
86
+ const sessions = listSessionRows(ctx.db, { limit: 15 }).map((s) => ({
87
+ id: s.id,
88
+ title: ctx.redact(s.title ?? ""),
89
+ cwd: s.cwd,
90
+ updatedAt: s.updatedAt,
91
+ areaId: sessionArea.get(s.id) ?? null,
92
+ }));
93
+ return {
94
+ areas,
95
+ proposed: all.filter((a) => a.status === "proposed").map((a) => ({ id: a.id, name: a.name })),
96
+ unsorted: Array.isArray(raw.unsorted) ? raw.unsorted.length : 0,
97
+ feed,
98
+ decisions,
99
+ sessions,
100
+ };
101
+ }
102
+ const SEGMENT = /^[a-z0-9][a-z0-9-]{0,63}$/;
103
+ function confined(root, rel) {
104
+ try {
105
+ return resolveConfined(root, rel);
106
+ }
107
+ catch {
108
+ throw new NexoError({ code: "FORBIDDEN", message: `Path "${rel}" is outside the home folder`, context: { path: rel } });
109
+ }
110
+ }
111
+ function titleOf(content, fallback) {
112
+ const m = /<title>([^<]*)<\/title>/i.exec(content.slice(0, 4096));
113
+ return m?.[1]?.trim() || fallback;
114
+ }
115
+ /** `GET /api/home/folder?area=&name=`: the HTML pages of one folder, paths relative to the home root. */
116
+ export function homeFolderResponse(ctx, area, name) {
117
+ if (!SEGMENT.test(area) || !SEGMENT.test(name))
118
+ throw new NexoError({ code: "INVALID_REQUEST", message: "area and name must be lowercase slugs", context: { area, name } });
119
+ const root = ctx.wikisDir ?? resolveNexoPaths().homeRoot;
120
+ const dir = confined(root, `${area}/${name}`);
121
+ if (!existsSync(dir) || !statSync(dir).isDirectory())
122
+ throw new NexoError({ code: "NOT_FOUND", message: `No folder ${area}/${name}`, context: { area, name } });
123
+ const realRoot = realpathSync(root);
124
+ const pages = [];
125
+ const walk = (d) => {
126
+ for (const f of readdirSync(d)) {
127
+ if (f.startsWith(".") || f === "raw")
128
+ continue;
129
+ const p = join(d, f);
130
+ const st = statSync(p);
131
+ if (st.isDirectory())
132
+ walk(p);
133
+ else if (f.endsWith(".html")) {
134
+ const content = readFileSync(p, "utf8");
135
+ pages.push({ path: relative(realRoot, p).split(sep).join("/"), title: ctx.redact(titleOf(content, f.slice(0, -5))), summary: parseWikiMeta(content).summary, modifiedAt: st.mtime.toISOString() });
136
+ }
137
+ }
138
+ };
139
+ walk(dir);
140
+ pages.sort((a, b) => a.path.localeCompare(b.path));
141
+ return { area, name, pages };
142
+ }
143
+ /** `GET /api/home/page?path=<area>/<folder>/<page>.html`: one HTML page, redacted. */
144
+ export function homePageResponse(ctx, relPath) {
145
+ const segs = relPath.split("/");
146
+ if (!/^[A-Za-z0-9._/-]+\.html$/.test(relPath) || segs.length < 3 || segs.includes("..") || segs.includes(".")) {
147
+ throw new NexoError({ code: "INVALID_REQUEST", message: "path must be <area>/<folder>/<page>.html", context: { path: relPath } });
148
+ }
149
+ const root = ctx.wikisDir ?? resolveNexoPaths().homeRoot;
150
+ const file = confined(root, relPath);
151
+ if (!existsSync(file) || !statSync(file).isFile())
152
+ throw new NexoError({ code: "NOT_FOUND", message: `No page ${relPath}`, context: { path: relPath } });
153
+ const content = ctx.redact(readFileSync(file, "utf8"));
154
+ const meta = parseWikiMeta(content);
155
+ return { path: relPath, title: titleOf(content, (segs[segs.length - 1] ?? relPath).replace(/[.]html$/, "")), summary: meta.summary, updated: meta.updated, content, modifiedAt: statSync(file).mtime.toISOString() };
156
+ }
package/web/src/server.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { homeFolderResponse, homePageResponse, homeResponse } from "./home-api.js";
1
2
  import { randomBytes, timingSafeEqual } from "node:crypto";
2
3
  import { createServer } from "node:http";
3
4
  import { fileURLToPath } from "node:url";
@@ -260,6 +261,21 @@ export async function startWebServer(options) {
260
261
  assertWritable();
261
262
  return sendJson(res, 200, reviewEvalCaseResponse(ctx, m[1], m[2]));
262
263
  }
264
+ if (path === "/api/home/folder") {
265
+ if (method !== "GET")
266
+ throw methodNotAllowed(method, path, "GET");
267
+ return sendJson(res, 200, homeFolderResponse(ctx, url.searchParams.get("area") ?? "", url.searchParams.get("name") ?? ""));
268
+ }
269
+ if (path === "/api/home/page") {
270
+ if (method !== "GET")
271
+ throw methodNotAllowed(method, path, "GET");
272
+ return sendJson(res, 200, homePageResponse(ctx, url.searchParams.get("path") ?? ""));
273
+ }
274
+ if (path === "/api/home") {
275
+ if (method !== "GET")
276
+ throw methodNotAllowed(method, path, "GET");
277
+ return sendJson(res, 200, homeResponse(ctx));
278
+ }
263
279
  if (ROUTES.wikis.test(path)) {
264
280
  if (method !== "GET")
265
281
  throw methodNotAllowed(method, path, "GET");
@@ -1,73 +0,0 @@
1
- /** Every ancestor of `id` (parents, their parents, …), not `id` itself. */
2
- export function ancestors(id, nodes) {
3
- const seen = new Set();
4
- const stack = [...(nodes.get(id)?.parentIds ?? [])];
5
- while (stack.length > 0) {
6
- const next = stack.pop();
7
- if (seen.has(next))
8
- continue;
9
- seen.add(next);
10
- stack.push(...(nodes.get(next)?.parentIds ?? []));
11
- }
12
- return seen;
13
- }
14
- /** The most recent common ancestor of `a` and `b` (the latest in `nodes` order), or null. */
15
- export function commonAncestor(a, b, nodes) {
16
- const ofA = ancestors(a, nodes);
17
- const ofB = ancestors(b, nodes);
18
- let found = null;
19
- for (const id of nodes.keys())
20
- if (ofA.has(id) && ofB.has(id))
21
- found = id;
22
- return found;
23
- }
24
- /**
25
- * System-aware merge (GEPA): each component comes from whichever side changed it since `ancestor`. Returns null when
26
- * both sides changed the same component differently, or when the result would equal one side (nothing to combine).
27
- */
28
- export function mergeTexts(ancestor, a, b) {
29
- const merged = {};
30
- let fromA = false;
31
- let fromB = false;
32
- for (const component of Object.keys(ancestor)) {
33
- const base = ancestor[component] ?? "";
34
- const textA = a[component] ?? base;
35
- const textB = b[component] ?? base;
36
- const changedA = textA !== base;
37
- const changedB = textB !== base;
38
- if (changedA && changedB && textA !== textB)
39
- return null;
40
- if (changedA)
41
- fromA = true;
42
- if (changedB && !changedA)
43
- fromB = true;
44
- merged[component] = changedA ? textA : textB;
45
- }
46
- return fromA && fromB ? merged : null;
47
- }
48
- /**
49
- * The first pair of Pareto members (in order) that is not ancestor and descendant, not tried before, has a common
50
- * ancestor, and merges without a conflict. `tried` holds `a|b` keys of pairs already merged or attempted.
51
- */
52
- export function findMerge(members, nodes, texts, tried) {
53
- for (let i = 0; i < members.length; i++) {
54
- for (let j = i + 1; j < members.length; j++) {
55
- const a = members[i];
56
- const b = members[j];
57
- if (tried.has(`${a}|${b}`))
58
- continue;
59
- if (ancestors(a, nodes).has(b) || ancestors(b, nodes).has(a))
60
- continue;
61
- const ancestor = commonAncestor(a, b, nodes);
62
- const base = ancestor ? texts.get(ancestor) : undefined;
63
- const ta = texts.get(a);
64
- const tb = texts.get(b);
65
- if (!ancestor || !base || !ta || !tb)
66
- continue;
67
- const merged = mergeTexts(base, ta, tb);
68
- if (merged)
69
- return { a, b, ancestor, texts: merged };
70
- }
71
- }
72
- return null;
73
- }