@hexdecimal16/nexo 0.4.8 → 0.4.10
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/cli/src/areas.js +3 -3
- package/cli/src/home-migrate.js +168 -0
- package/cli/src/main.js +3 -0
- package/cli/src/ui.js +1 -1
- package/core/src/engine/prompts.js +1 -1
- package/core/src/engine/run.js +2 -2
- package/core/src/index.js +1 -0
- package/core/src/learn/areas.js +22 -9
- package/core/src/learn/job.js +5 -4
- package/core/src/learn/wiki.js +12 -9
- package/core/src/optimize/optimize.js +1 -1
- package/core/src/optimize/scratchpad-wiki.js +7 -3
- package/core/src/providers/codex-p.js +378 -0
- package/core/src/providers/models.js +3 -1
- package/core/src/settings/settings.js +3 -2
- package/package.json +1 -1
- package/skills/bundled/wiki/SKILL.md +3 -3
- package/store/src/paths.js +2 -2
- package/tools/src/home-folders.js +36 -0
- package/tools/src/index.js +1 -0
- package/web/public/area.html +1 -1
- package/web/public/cost.html +1 -1
- package/web/public/home.html +1 -1
- package/web/public/index.html +1 -1
- package/web/public/judge.html +1 -1
- package/web/public/learning.html +1 -1
- package/web/public/optim.html +1 -1
- package/web/public/page.html +24 -0
- package/web/public/pages/area.ts +23 -13
- package/web/public/pages/home.ts +9 -3
- package/web/public/pages/page.ts +62 -0
- package/web/public/run.html +1 -1
- package/web/public/sessions.html +1 -1
- package/web/public/style.css +27 -0
- package/web/public/wikis.html +1 -1
- package/web/src/api.js +3 -3
- package/web/src/home-api.js +64 -7
- package/web/src/server.js +11 -1
package/web/public/pages/area.ts
CHANGED
|
@@ -3,9 +3,9 @@ import { formatTime } from "../lib/format.ts";
|
|
|
3
3
|
import { html, type SafeHtml } from "../lib/html.ts";
|
|
4
4
|
import { asErrorBody, el, getJson } from "./api.ts";
|
|
5
5
|
|
|
6
|
-
interface HomeWiki { name: string; summary?: string; pageCount: number }
|
|
6
|
+
interface HomeWiki { name: string; summary?: string; pageCount: number; path: string }
|
|
7
7
|
interface HomeArea { id: string; name: string; scope: string; wikis: HomeWiki[]; recent: number }
|
|
8
|
-
interface HomeFeedItem { at: string; text: string; areaId: string | null; wiki: string | null; sessionId: string | null; runId: string; verified: boolean }
|
|
8
|
+
interface HomeFeedItem { at: string; text: string; areaId: string | null; wiki: string | null; sessionId: string | null; runId: string; verified: boolean; pages?: string[] }
|
|
9
9
|
interface HomeDecision { at: string; choice: string; reason: string; areaId: string | null; wiki: string | null; sessionId: string | null; runId: string }
|
|
10
10
|
interface HomeSession { id: string; title: string; cwd: string; updatedAt: string; areaId: string | null }
|
|
11
11
|
interface Home { areas: HomeArea[]; feed: HomeFeedItem[]; decisions: HomeDecision[]; sessions: HomeSession[] }
|
|
@@ -14,8 +14,13 @@ const box = el<HTMLDivElement>("#area");
|
|
|
14
14
|
const title = el<HTMLHeadingElement>("#area-title");
|
|
15
15
|
const errorBox = el<HTMLDivElement>("#error");
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
const empty = (text: string): SafeHtml => html`<p class="muted">${text}</p>`;
|
|
18
|
+
const stat = (n: number, label: string): SafeHtml => html`<div class="area-stat"><b>${String(n)}</b><span class="muted">${label}</span></div>`;
|
|
19
|
+
|
|
20
|
+
function pageLinks(f: { pages?: string[]; wiki: string | null; areaId: string | null }): SafeHtml {
|
|
21
|
+
const pages = (f.pages ?? []).filter((p) => p.endsWith(".html") && p.includes("/") && !p.includes(".."));
|
|
22
|
+
if (pages.length > 0) return html`${pages.map((p) => html`<a class="feed-page" href="/page.html?path=${encodeURIComponent(p)}">${p.slice(p.lastIndexOf("/") + 1, -5)}</a> `)}`;
|
|
23
|
+
return f.wiki && f.areaId ? html`<a class="feed-page" href="/page.html?path=${encodeURIComponent(`${f.areaId}/${f.wiki}/index.html`)}">${f.wiki}</a>` : html``;
|
|
19
24
|
}
|
|
20
25
|
|
|
21
26
|
function render(d: Home, id: string): void {
|
|
@@ -27,18 +32,23 @@ function render(d: Home, id: string): void {
|
|
|
27
32
|
}
|
|
28
33
|
title.textContent = a.name;
|
|
29
34
|
document.title = `nexo · ${a.name}`;
|
|
30
|
-
const
|
|
31
|
-
const
|
|
35
|
+
const feed = d.feed.filter((f) => f.areaId === a.id).slice(0, 30);
|
|
36
|
+
const decisions = d.decisions.filter((x) => x.areaId === a.id);
|
|
37
|
+
const sessions = d.sessions.filter((s) => s.areaId === a.id);
|
|
38
|
+
const pages = a.wikis.reduce((n, w) => n + w.pageCount, 0);
|
|
39
|
+
const wikis = a.wikis.map((w) => html`<a class="area-card" href="/page.html?path=${encodeURIComponent(`${w.path}/index.html`)}"><strong>${w.name}</strong><span class="muted">${String(w.pageCount)} pages</span>${w.summary ? html`<span class="area-card-text">${w.summary}</span>` : ""}</a>`);
|
|
40
|
+
const timeline = feed.map((f) => {
|
|
32
41
|
const link = f.sessionId ? html`<a href="/?session=${encodeURIComponent(f.sessionId)}">session</a>` : html`<a href="/run.html?id=${encodeURIComponent(f.runId)}">run</a>`;
|
|
33
|
-
return html`<li><
|
|
42
|
+
return html`<li><time>${formatTime(f.at)}${f.wiki ? ` · ${f.wiki}` : ""}</time>${f.text}${f.verified ? "" : html` <span class="home-tag">unverified</span>`} ${pageLinks(f)} ${link}</li>`;
|
|
34
43
|
});
|
|
35
|
-
const
|
|
36
|
-
const
|
|
44
|
+
const decisionCards = decisions.map((x) => html`<div class="area-decision"><strong>${x.choice}</strong>${x.reason ? html`<span class="muted">Because ${x.reason}</span>` : ""}<time class="muted">${formatTime(x.at)}</time></div>`);
|
|
45
|
+
const rows = sessions.map((s) => html`<tr><td><a href="/?session=${encodeURIComponent(s.id)}">${s.title || s.id}</a></td><td class="mono muted">${s.cwd}</td><td class="muted">${formatTime(s.updatedAt)}</td></tr>`);
|
|
37
46
|
box.innerHTML = html`<p class="home-note">${a.scope}</p>
|
|
38
|
-
<
|
|
39
|
-
<section><h2>
|
|
40
|
-
<section><h2>
|
|
41
|
-
<section><h2>
|
|
47
|
+
<div class="area-stats">${stat(a.wikis.length, "wikis")}${stat(pages, "pages")}${stat(a.recent, "filed this week")}${stat(decisions.length, "decisions")}</div>
|
|
48
|
+
<section><h2>Wikis</h2>${wikis.length > 0 ? html`<div class="area-grid">${wikis}</div>` : empty("No wikis in this area yet.")}</section>
|
|
49
|
+
<section><h2>Recent</h2>${timeline.length > 0 ? html`<ol class="area-timeline">${timeline}</ol>` : empty("Nothing filed here yet. Finished tasks appear here after they are scored.")}</section>
|
|
50
|
+
<section><h2>Decisions</h2>${decisionCards.length > 0 ? decisionCards : empty("No decisions recorded here yet.")}</section>
|
|
51
|
+
<section><h2>Sessions</h2>${rows.length > 0 ? html`<table class="area-table"><thead><tr><th>Session</th><th>Folder</th><th>Updated</th></tr></thead><tbody>${rows}</tbody></table>` : empty("No linked sessions yet.")}</section>`.value;
|
|
42
52
|
}
|
|
43
53
|
|
|
44
54
|
const id = new URLSearchParams(location.search).get("id") ?? "";
|
package/web/public/pages/home.ts
CHANGED
|
@@ -3,9 +3,9 @@ import { formatTime } from "../lib/format.ts";
|
|
|
3
3
|
import { html, type SafeHtml } from "../lib/html.ts";
|
|
4
4
|
import { asErrorBody, el, getJson } from "./api.ts";
|
|
5
5
|
|
|
6
|
-
interface HomeWiki { name: string; summary?: string; pageCount: number }
|
|
6
|
+
interface HomeWiki { name: string; summary?: string; pageCount: number; path: string }
|
|
7
7
|
interface HomeArea { id: string; name: string; scope: string; wikis: HomeWiki[]; recent: number }
|
|
8
|
-
interface HomeFeedItem { at: string; text: string; areaId: string | null; wiki: string | null; sessionId: string | null; runId: string; verified: boolean }
|
|
8
|
+
interface HomeFeedItem { at: string; text: string; areaId: string | null; wiki: string | null; sessionId: string | null; runId: string; verified: boolean; pages?: string[] }
|
|
9
9
|
interface HomeDecision { at: string; choice: string; reason: string; areaId: string | null; wiki: string | null; sessionId: string | null; runId: string }
|
|
10
10
|
interface HomeSession { id: string; title: string; cwd: string; updatedAt: string; areaId: string | null }
|
|
11
11
|
interface Home { areas: HomeArea[]; proposed: { id: string; name: string }[]; unsorted: number; feed: HomeFeedItem[]; decisions: HomeDecision[]; sessions: HomeSession[] }
|
|
@@ -18,9 +18,15 @@ let data: Home | null = null;
|
|
|
18
18
|
const hit = (q: string, ...parts: (string | null | undefined)[]) => !q || parts.some((p) => (p ?? "").toLowerCase().includes(q));
|
|
19
19
|
const areaName = (id: string | null) => data?.areas.find((a) => a.id === id)?.name ?? "unsorted";
|
|
20
20
|
|
|
21
|
+
function pageLinks(f: { pages?: string[]; wiki: string | null; areaId: string | null }): SafeHtml {
|
|
22
|
+
const pages = (f.pages ?? []).filter((p) => p.endsWith(".html") && p.includes("/") && !p.includes(".."));
|
|
23
|
+
if (pages.length > 0) return html`${pages.map((p) => html`<a class="feed-page" href="/page.html?path=${encodeURIComponent(p)}">${p.slice(p.lastIndexOf("/") + 1, -5)}</a> `)}`;
|
|
24
|
+
return f.wiki && f.areaId ? html`<a class="feed-page" href="/page.html?path=${encodeURIComponent(`${f.areaId}/${f.wiki}/index.html`)}">${f.wiki}</a>` : html``;
|
|
25
|
+
}
|
|
26
|
+
|
|
21
27
|
function feedItem(f: HomeFeedItem): SafeHtml {
|
|
22
28
|
const link = f.sessionId ? html`<a href="/?session=${encodeURIComponent(f.sessionId)}">session</a>` : html`<a href="/run.html?id=${encodeURIComponent(f.runId)}">run</a>`;
|
|
23
|
-
return html`<li><span class="muted mono">${formatTime(f.at)}</span> ${f.text}${f.verified ? "" : html` <span class="home-tag">unverified</span>`} <span class="muted">· ${areaName(f.areaId)}${f.wiki ? ` · ${f.wiki}` : ""}</span> ${link}</li>`;
|
|
29
|
+
return html`<li><span class="muted mono">${formatTime(f.at)}</span> ${f.text}${f.verified ? "" : html` <span class="home-tag">unverified</span>`} <span class="muted">· ${areaName(f.areaId)}${f.wiki ? ` · ${f.wiki}` : ""}</span> ${pageLinks(f)} ${link}</li>`;
|
|
24
30
|
}
|
|
25
31
|
|
|
26
32
|
function list(items: SafeHtml[], empty: string): SafeHtml {
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { renderError } from "../lib/common-view.ts";
|
|
2
|
+
import { formatTime } from "../lib/format.ts";
|
|
3
|
+
import { asErrorBody, el, getJson } from "./api.ts";
|
|
4
|
+
|
|
5
|
+
interface HomePage { path: string; title: string; summary?: string; updated?: string; content: string; modifiedAt: string }
|
|
6
|
+
|
|
7
|
+
const crumbs = el<HTMLParagraphElement>("#page-crumbs");
|
|
8
|
+
const titleEl = el<HTMLHeadingElement>("#page-title");
|
|
9
|
+
const meta = el<HTMLParagraphElement>("#page-meta");
|
|
10
|
+
const body = el<HTMLDivElement>("#page-body");
|
|
11
|
+
const errorBox = el<HTMLDivElement>("#error");
|
|
12
|
+
const DROP = "script,style,iframe,object,embed,form,link,meta,base,title";
|
|
13
|
+
|
|
14
|
+
function link(text: string, href: string): HTMLAnchorElement {
|
|
15
|
+
const a = document.createElement("a");
|
|
16
|
+
a.href = href;
|
|
17
|
+
a.textContent = text;
|
|
18
|
+
return a;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Shows the page body inline: active content removed, links to other pages kept inside page.html. */
|
|
22
|
+
function mount(page: HomePage): void {
|
|
23
|
+
const doc = new DOMParser().parseFromString(page.content, "text/html");
|
|
24
|
+
const root = doc.querySelector("main") ?? doc.body;
|
|
25
|
+
root.querySelectorAll(DROP).forEach((n) => n.remove());
|
|
26
|
+
root.querySelectorAll(":scope > nav, :scope > p.meta, :scope > h1").forEach((n) => n.remove());
|
|
27
|
+
root.querySelectorAll("*").forEach((node) => {
|
|
28
|
+
for (const attr of Array.from(node.attributes)) {
|
|
29
|
+
const name = attr.name.toLowerCase();
|
|
30
|
+
if (name.startsWith("on") || name === "style") node.removeAttribute(attr.name);
|
|
31
|
+
else if ((name === "href" || name === "src" || name === "xlink:href") && /^\s*(javascript|data):/i.test(attr.value)) node.removeAttribute(attr.name);
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
const base = page.path.slice(0, page.path.lastIndexOf("/") + 1);
|
|
35
|
+
root.querySelectorAll<HTMLAnchorElement>("a[href]").forEach((a) => {
|
|
36
|
+
const href = a.getAttribute("href") ?? "";
|
|
37
|
+
if (/^(https?:|mailto:)/i.test(href)) {
|
|
38
|
+
a.target = "_blank";
|
|
39
|
+
a.rel = "noopener noreferrer";
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
if (href.startsWith("#")) return;
|
|
43
|
+
const target = decodeURIComponent(new URL(href, `https://home.invalid/${base}`).pathname.slice(1));
|
|
44
|
+
if (target.endsWith(".html")) a.setAttribute("href", `/page.html?path=${encodeURIComponent(target)}`);
|
|
45
|
+
else a.removeAttribute("href");
|
|
46
|
+
});
|
|
47
|
+
body.replaceChildren(...Array.from(root.childNodes).map((n) => document.importNode(n, true)));
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const path = new URLSearchParams(location.search).get("path") ?? "";
|
|
51
|
+
const [area = "", folder = ""] = path.split("/");
|
|
52
|
+
crumbs.replaceChildren(link("Home", "/home.html"), " / ", link(area, `/area.html?id=${encodeURIComponent(area)}`), " / ", link(folder, `/page.html?path=${encodeURIComponent(`${area}/${folder}/index.html`)}`));
|
|
53
|
+
getJson<HomePage>(`/api/home/page?path=${encodeURIComponent(path)}`)
|
|
54
|
+
.then((page) => {
|
|
55
|
+
titleEl.textContent = page.title;
|
|
56
|
+
document.title = `nexo · ${page.title}`;
|
|
57
|
+
meta.textContent = `Updated ${page.updated || formatTime(page.modifiedAt)}`;
|
|
58
|
+
mount(page);
|
|
59
|
+
})
|
|
60
|
+
.catch((error) => {
|
|
61
|
+
errorBox.innerHTML = renderError(asErrorBody(error)).value;
|
|
62
|
+
});
|
package/web/public/run.html
CHANGED
|
@@ -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="/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
|
|
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>
|
package/web/public/sessions.html
CHANGED
|
@@ -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="/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
|
|
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>
|
package/web/public/style.css
CHANGED
|
@@ -378,3 +378,30 @@ section[id] { scroll-margin-top: 96px; }
|
|
|
378
378
|
.home-note { border-left: 4px solid var(--pen); padding: 6px 12px; background: var(--bg); }
|
|
379
379
|
a.home-area { display: block; color: inherit; text-decoration: none; }
|
|
380
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; }
|
package/web/public/wikis.html
CHANGED
|
@@ -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="/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
|
|
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
|
@@ -574,7 +574,7 @@ function parseSummary(content) {
|
|
|
574
574
|
}
|
|
575
575
|
/** `GET /api/wikis`: lists all wiki directories under the wikis path. */
|
|
576
576
|
export function listWikisResponse(ctx) {
|
|
577
|
-
const dir = ctx.wikisDir ?? resolveNexoPaths().
|
|
577
|
+
const dir = ctx.wikisDir ?? resolveNexoPaths().homeRoot;
|
|
578
578
|
if (!existsSync(dir))
|
|
579
579
|
return { wikis: [] };
|
|
580
580
|
const wikis = [];
|
|
@@ -622,7 +622,7 @@ export function listWikisResponse(ctx) {
|
|
|
622
622
|
}
|
|
623
623
|
/** `GET /api/wikis/{name}/pages`: lists all markdown pages in a wiki with summary metadata. */
|
|
624
624
|
export function listWikiPagesResponse(ctx, wikiName) {
|
|
625
|
-
const root = ctx.wikisDir ?? resolveNexoPaths().
|
|
625
|
+
const root = ctx.wikisDir ?? resolveNexoPaths().homeRoot;
|
|
626
626
|
let wikiDir;
|
|
627
627
|
try {
|
|
628
628
|
wikiDir = resolveConfined(root, wikiName);
|
|
@@ -676,7 +676,7 @@ export function listWikiPagesResponse(ctx, wikiName) {
|
|
|
676
676
|
export function wikiPageResponse(ctx, wikiName, relPath) {
|
|
677
677
|
if (relPath.includes("\0"))
|
|
678
678
|
throw invalidRequest("Path contains a NUL byte");
|
|
679
|
-
const root = ctx.wikisDir ?? resolveNexoPaths().
|
|
679
|
+
const root = ctx.wikisDir ?? resolveNexoPaths().homeRoot;
|
|
680
680
|
let wikiDir;
|
|
681
681
|
try {
|
|
682
682
|
wikiDir = resolveConfined(root, wikiName);
|
package/web/src/home-api.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { existsSync, readdirSync, readFileSync, statSync } from "node:fs";
|
|
2
|
-
import { join } from "node:path";
|
|
1
|
+
import { existsSync, readdirSync, readFileSync, realpathSync, statSync } from "node:fs";
|
|
2
|
+
import { join, relative, sep } from "node:path";
|
|
3
3
|
import { listSessionRows, resolveNexoPaths } from "../../store/src/index.js";
|
|
4
|
-
import {
|
|
4
|
+
import { NexoError } from "../../telemetry/src/index.js";
|
|
5
|
+
import { listHomeFolders, parseWikiMeta, resolveConfined } from "../../tools/src/index.js";
|
|
5
6
|
const FEED_LIMIT = 50;
|
|
6
7
|
const DECISION_LIMIT = 50;
|
|
7
8
|
/** Reads a JSON-lines file newest first; unparseable lines are skipped. */
|
|
@@ -46,7 +47,7 @@ function countPages(dir) {
|
|
|
46
47
|
}
|
|
47
48
|
/** `GET /api/home`: approved areas with their wikis, the feed, decisions and recent sessions, read fresh on every call. */
|
|
48
49
|
export function homeResponse(ctx) {
|
|
49
|
-
const dir = ctx.wikisDir ?? resolveNexoPaths().
|
|
50
|
+
const dir = ctx.wikisDir ?? resolveNexoPaths().homeRoot;
|
|
50
51
|
let raw = {};
|
|
51
52
|
try {
|
|
52
53
|
raw = JSON.parse(readFileSync(join(dir, "areas.json"), "utf8"));
|
|
@@ -58,22 +59,23 @@ export function homeResponse(ctx) {
|
|
|
58
59
|
const active = all.filter((a) => a.status === "active");
|
|
59
60
|
const feed = readJsonl(join(dir, "feed.jsonl"), FEED_LIMIT).map((f) => ({ ...f, text: ctx.redact(String(f.text ?? "")) }));
|
|
60
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]));
|
|
61
63
|
const weekAgo = ctx.now().getTime() - 7 * 86_400_000;
|
|
62
64
|
const areas = active.map((a) => ({
|
|
63
65
|
id: a.id,
|
|
64
66
|
name: a.name,
|
|
65
67
|
scope: a.scope,
|
|
66
68
|
wikis: (a.wikis ?? [])
|
|
67
|
-
.filter((w) =>
|
|
69
|
+
.filter((w) => folders.has(w))
|
|
68
70
|
.map((w) => {
|
|
69
71
|
let summary;
|
|
70
72
|
try {
|
|
71
|
-
summary = parseWikiMeta(readFileSync(join(
|
|
73
|
+
summary = parseWikiMeta(readFileSync(join(folders.get(w), "overview.html"), "utf8")).summary;
|
|
72
74
|
}
|
|
73
75
|
catch {
|
|
74
76
|
summary = undefined;
|
|
75
77
|
}
|
|
76
|
-
return { name: w, summary, pageCount: countPages(
|
|
78
|
+
return { name: w, summary, pageCount: countPages(folders.get(w)), path: relative(dir, folders.get(w)).split(sep).join("/") };
|
|
77
79
|
}),
|
|
78
80
|
recent: feed.filter((f) => f.areaId === a.id && Date.parse(f.at) >= weekAgo).length,
|
|
79
81
|
}));
|
|
@@ -97,3 +99,58 @@ export function homeResponse(ctx) {
|
|
|
97
99
|
sessions,
|
|
98
100
|
};
|
|
99
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,4 +1,4 @@
|
|
|
1
|
-
import { homeResponse } from "./home-api.js";
|
|
1
|
+
import { homeFolderResponse, homePageResponse, homeResponse } from "./home-api.js";
|
|
2
2
|
import { randomBytes, timingSafeEqual } from "node:crypto";
|
|
3
3
|
import { createServer } from "node:http";
|
|
4
4
|
import { fileURLToPath } from "node:url";
|
|
@@ -261,6 +261,16 @@ export async function startWebServer(options) {
|
|
|
261
261
|
assertWritable();
|
|
262
262
|
return sendJson(res, 200, reviewEvalCaseResponse(ctx, m[1], m[2]));
|
|
263
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
|
+
}
|
|
264
274
|
if (path === "/api/home") {
|
|
265
275
|
if (method !== "GET")
|
|
266
276
|
throw methodNotAllowed(method, path, "GET");
|