@hexdecimal16/nexo 0.0.0-stage → 0.2.1
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/LICENSE +75 -0
- package/README.md +75 -2
- package/cli/src/bin.js +12 -0
- package/cli/src/common.js +78 -0
- package/cli/src/doctor.js +27 -0
- package/cli/src/eval-cases.js +65 -0
- package/cli/src/eval.js +67 -0
- package/cli/src/init.js +186 -0
- package/cli/src/interactive.js +171 -0
- package/cli/src/learn.js +178 -0
- package/cli/src/main.js +266 -0
- package/cli/src/optimize.js +289 -0
- package/cli/src/prompter.js +47 -0
- package/cli/src/review.js +120 -0
- package/cli/src/runtime.js +2 -0
- package/cli/src/sessions.js +34 -0
- package/cli/src/settings.js +169 -0
- package/cli/src/ui.js +112 -0
- package/cli/src/version.js +19 -0
- package/core/src/approval/classifier.js +152 -0
- package/core/src/config/load.js +283 -0
- package/core/src/config/schema.js +186 -0
- package/core/src/cost/admission.js +90 -0
- package/core/src/cost/budget.js +67 -0
- package/core/src/cost/pricing.js +53 -0
- package/core/src/engine/charged-completion.js +79 -0
- package/core/src/engine/errors.js +77 -0
- package/core/src/engine/mcp.js +11 -0
- package/core/src/engine/messages.js +86 -0
- package/core/src/engine/prompts.js +32 -0
- package/core/src/engine/provider-gate.js +33 -0
- package/core/src/engine/recorder.js +61 -0
- package/core/src/engine/run.js +1115 -0
- package/core/src/engine/semaphore.js +45 -0
- package/core/src/engine/spawn-tool.js +119 -0
- package/core/src/engine/task-graph.js +153 -0
- package/core/src/index.js +32 -0
- package/core/src/learn/caller.js +171 -0
- package/core/src/learn/context.js +112 -0
- package/core/src/learn/embedder.js +216 -0
- package/core/src/learn/job.js +196 -0
- package/core/src/learn/wiki.js +116 -0
- package/core/src/learn/worker.js +141 -0
- package/core/src/optimize/adapter.js +250 -0
- package/core/src/optimize/climb.js +338 -0
- package/core/src/optimize/mine.js +95 -0
- package/core/src/optimize/optimize.js +377 -0
- package/core/src/optimize/optimizer-run.js +67 -0
- package/core/src/optimize/reflect.js +100 -0
- package/core/src/optimize/scratchpad-wiki.js +69 -0
- package/core/src/providers/claude-p.js +363 -0
- package/core/src/providers/kiro-p.js +408 -0
- package/core/src/providers/models.js +10 -0
- package/core/src/scoring/calibration.js +31 -0
- package/core/src/scoring/continuation.js +24 -0
- package/core/src/scoring/runner.js +322 -0
- package/core/src/scoring/sources.js +88 -0
- package/core/src/session/lock.js +88 -0
- package/core/src/session/session.js +647 -0
- package/core/src/session/shell.js +26 -0
- package/core/src/session/transcript.js +86 -0
- package/core/src/settings/config-edit.js +472 -0
- package/core/src/settings/credentials.js +120 -0
- package/core/src/settings/doctor.js +103 -0
- package/core/src/settings/settings.js +265 -0
- package/mcp/src/hub.js +184 -0
- package/mcp/src/index.js +1 -0
- package/memory/src/curate.js +121 -0
- package/memory/src/index.js +4 -0
- package/memory/src/prompts.js +143 -0
- package/memory/src/retrieval.js +91 -0
- package/memory/src/similarity.js +47 -0
- package/optimize/src/engine.js +206 -0
- package/optimize/src/index.js +8 -0
- package/optimize/src/merge.js +73 -0
- package/optimize/src/novelty.js +37 -0
- package/optimize/src/pareto.js +83 -0
- package/optimize/src/proposer.js +148 -0
- package/optimize/src/report.js +107 -0
- package/optimize/src/rng.js +34 -0
- package/optimize/src/target.js +34 -0
- package/package.json +47 -4
- package/router/src/effort.js +5 -0
- package/router/src/escalation.js +171 -0
- package/router/src/index.js +4 -0
- package/router/src/registry.js +21 -0
- package/router/src/resolve.js +117 -0
- package/scorers/src/deterministic.js +194 -0
- package/scorers/src/family.js +48 -0
- package/scorers/src/index.js +6 -0
- package/scorers/src/judge.js +284 -0
- package/scorers/src/regex-worker.js +3 -0
- package/scorers/src/spec.js +147 -0
- package/scorers/src/trajectory.js +22 -0
- package/scorers/src/types.js +1 -0
- package/sdk/src/index.js +3 -0
- package/sdk/src/nexo.js +94 -0
- package/sdk/src/runtime.js +39 -0
- package/skills/bundled/browser/SKILL.md +57 -0
- package/skills/bundled/wiki/SKILL.md +73 -0
- package/skills/src/frontmatter.js +25 -0
- package/skills/src/index.js +3 -0
- package/skills/src/load-skill-tool.js +41 -0
- package/skills/src/loader.js +95 -0
- package/store/src/artifacts.js +124 -0
- package/store/src/budget-ledger.js +18 -0
- package/store/src/cost-report.js +46 -0
- package/store/src/cursor.js +35 -0
- package/store/src/db.js +83 -0
- package/store/src/errors.js +9 -0
- package/store/src/eval-cases.js +255 -0
- package/store/src/feedback.js +154 -0
- package/store/src/goldens.js +61 -0
- package/store/src/ids.js +6 -0
- package/store/src/index.js +25 -0
- package/store/src/judge-calibration.js +182 -0
- package/store/src/learn-jobs.js +145 -0
- package/store/src/memories.js +261 -0
- package/store/src/migrations/001_init.sql +81 -0
- package/store/src/migrations/002_feedback.sql +9 -0
- package/store/src/migrations/003_sessions.sql +19 -0
- package/store/src/migrations/004_scoring.sql +22 -0
- package/store/src/migrations/005_judge_calibration.sql +27 -0
- package/store/src/migrations/006_learning.sql +77 -0
- package/store/src/migrations/007_optimize.sql +55 -0
- package/store/src/migrations/008_mined_cases_and_climb.sql +46 -0
- package/store/src/mine.js +51 -0
- package/store/src/optim.js +291 -0
- package/store/src/paths.js +22 -0
- package/store/src/playbooks.js +199 -0
- package/store/src/reindex.js +143 -0
- package/store/src/run-dir.js +151 -0
- package/store/src/runs.js +310 -0
- package/store/src/scores.js +92 -0
- package/store/src/sessions.js +304 -0
- package/store/src/span-tree.js +98 -0
- package/store/src/spans.js +84 -0
- package/store/src/sql.js +13 -0
- package/store/src/workspace-copy.js +80 -0
- package/telemetry/src/errors.js +32 -0
- package/telemetry/src/index.js +3 -0
- package/telemetry/src/logger.js +39 -0
- package/telemetry/src/redact.js +106 -0
- package/tools/src/builtin.js +269 -0
- package/tools/src/change-tracker.js +57 -0
- package/tools/src/confine.js +101 -0
- package/tools/src/index.js +7 -0
- package/tools/src/leases.js +57 -0
- package/tools/src/policy.js +162 -0
- package/tools/src/read-only.js +338 -0
- package/tools/src/session-rules.js +140 -0
- package/tui/src/app.js +965 -0
- package/tui/src/approvals.js +96 -0
- package/tui/src/commands.js +38 -0
- package/tui/src/components.js +248 -0
- package/tui/src/index.js +5 -0
- package/tui/src/picker.js +78 -0
- package/tui/src/session-picker.js +100 -0
- package/tui/src/settings-screen.js +346 -0
- package/tui/src/text.js +70 -0
- package/tui/src/theme.js +44 -0
- package/web/public/cost.html +25 -0
- package/web/public/fonts/GeistMono-400.woff2 +0 -0
- package/web/public/fonts/GeistMono-600.woff2 +0 -0
- package/web/public/fonts/InstrumentSans-400.woff2 +0 -0
- package/web/public/fonts/InstrumentSans-600.woff2 +0 -0
- package/web/public/index.html +39 -0
- package/web/public/judge.html +21 -0
- package/web/public/learning.html +46 -0
- package/web/public/lib/api-types.ts +616 -0
- package/web/public/lib/common-view.ts +47 -0
- package/web/public/lib/cost-view.ts +35 -0
- package/web/public/lib/files-view.ts +90 -0
- package/web/public/lib/format.ts +35 -0
- package/web/public/lib/html.ts +51 -0
- package/web/public/lib/judge-view.ts +62 -0
- package/web/public/lib/learning-view.ts +102 -0
- package/web/public/lib/optim-view.ts +148 -0
- package/web/public/lib/pager.ts +56 -0
- package/web/public/lib/poller.ts +79 -0
- package/web/public/lib/run-view.ts +61 -0
- package/web/public/lib/runs-view.ts +87 -0
- package/web/public/lib/score-view.ts +94 -0
- package/web/public/lib/sessions-view.ts +28 -0
- package/web/public/lib/span-view.ts +90 -0
- package/web/public/lib/theme.ts +60 -0
- package/web/public/lib/timeline.ts +97 -0
- package/web/public/lib/wikis-view.ts +257 -0
- package/web/public/optim.html +30 -0
- package/web/public/pages/api.ts +60 -0
- package/web/public/pages/cost.ts +36 -0
- package/web/public/pages/judge.ts +15 -0
- package/web/public/pages/learning.ts +92 -0
- package/web/public/pages/optim.ts +68 -0
- package/web/public/pages/paging.ts +18 -0
- package/web/public/pages/run.ts +221 -0
- package/web/public/pages/runs.ts +91 -0
- package/web/public/pages/sessions.ts +25 -0
- package/web/public/pages/wikis.ts +103 -0
- package/web/public/run.html +36 -0
- package/web/public/sessions.html +28 -0
- package/web/public/style.css +288 -0
- package/web/public/wikis.html +25 -0
- package/web/src/api.js +682 -0
- package/web/src/http-errors.js +60 -0
- package/web/src/index.js +5 -0
- package/web/src/server.js +292 -0
- package/web/src/static.js +86 -0
- package/web/src/strip.js +22 -0
package/web/src/api.js
ADDED
|
@@ -0,0 +1,682 @@
|
|
|
1
|
+
import { closeSync, constants, createReadStream, existsSync, fstatSync, lstatSync, openSync, readdirSync, readFileSync, readSync, statSync } from "node:fs";
|
|
2
|
+
import { extname, join, relative, sep } from "node:path";
|
|
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
|
+
import { NexoError } from "../../telemetry/src/index.js";
|
|
5
|
+
import { resolveConfined } from "../../tools/src/index.js";
|
|
6
|
+
import { SECURITY_HEADERS } from "./http-errors.js";
|
|
7
|
+
/** Files larger than this are cut unless `?full=1` is passed; the response carries `x-nexo-truncated: <size>`. */
|
|
8
|
+
export const MAX_FILE_PREVIEW_BYTES = 1024 * 1024;
|
|
9
|
+
function requireRun(db, id) {
|
|
10
|
+
const run = getRun(db, id);
|
|
11
|
+
if (!run)
|
|
12
|
+
throw runNotFound(id);
|
|
13
|
+
return run;
|
|
14
|
+
}
|
|
15
|
+
function optional(params, name) {
|
|
16
|
+
const value = params.get(name);
|
|
17
|
+
return value === null || value === "" ? undefined : value;
|
|
18
|
+
}
|
|
19
|
+
/** Vocabulary and mode for the pages: the run statuses and cost dimensions to offer, and whether writes are refused. */
|
|
20
|
+
export function metaResponse(ctx) {
|
|
21
|
+
return { runStatuses: [...RUN_STATUSES], costDimensions: [...COST_DIMENSIONS], readOnly: ctx.readOnly };
|
|
22
|
+
}
|
|
23
|
+
export function listRunsResponse(ctx, params) {
|
|
24
|
+
const query = {};
|
|
25
|
+
const scored = optional(params, "scored");
|
|
26
|
+
if (scored !== undefined) {
|
|
27
|
+
if (scored !== "true" && scored !== "false")
|
|
28
|
+
throw invalidRequest("scored must be true or false", { scored });
|
|
29
|
+
query.scored = scored === "true";
|
|
30
|
+
}
|
|
31
|
+
const status = optional(params, "status");
|
|
32
|
+
if (status !== undefined) {
|
|
33
|
+
if (!isRunStatus(status))
|
|
34
|
+
throw invalidRequest(`Unknown status "${status}"`, { status });
|
|
35
|
+
query.status = status;
|
|
36
|
+
}
|
|
37
|
+
const skill = optional(params, "skill");
|
|
38
|
+
if (skill !== undefined)
|
|
39
|
+
query.skill = skill;
|
|
40
|
+
const session = optional(params, "session");
|
|
41
|
+
if (session !== undefined)
|
|
42
|
+
query.sessionId = session;
|
|
43
|
+
const oneShot = optional(params, "oneShot");
|
|
44
|
+
if (oneShot !== undefined) {
|
|
45
|
+
if (oneShot !== "true" && oneShot !== "false")
|
|
46
|
+
throw invalidRequest("oneShot must be true or false", { oneShot });
|
|
47
|
+
query.oneShot = oneShot === "true";
|
|
48
|
+
}
|
|
49
|
+
const reviewed = optional(params, "reviewed");
|
|
50
|
+
if (reviewed !== undefined) {
|
|
51
|
+
if (reviewed !== "true" && reviewed !== "false")
|
|
52
|
+
throw invalidRequest("reviewed must be true or false", { reviewed });
|
|
53
|
+
query.reviewed = reviewed === "true";
|
|
54
|
+
}
|
|
55
|
+
const since = optional(params, "since");
|
|
56
|
+
if (since !== undefined)
|
|
57
|
+
query.since = since;
|
|
58
|
+
const limit = optional(params, "limit");
|
|
59
|
+
if (limit !== undefined)
|
|
60
|
+
query.limit = /^\d+$/.test(limit) ? Number(limit) : Number.NaN;
|
|
61
|
+
const cursor = optional(params, "cursor");
|
|
62
|
+
if (cursor !== undefined)
|
|
63
|
+
query.cursor = cursor;
|
|
64
|
+
const offset = optional(params, "offset");
|
|
65
|
+
if (offset !== undefined)
|
|
66
|
+
query.offset = /^\d+$/.test(offset) ? Number(offset) : Number.NaN;
|
|
67
|
+
return queryRuns(ctx.db, query);
|
|
68
|
+
}
|
|
69
|
+
/** `GET /api/sessions?limit=&offset=&cursor=`: one page of chat sessions, most recently updated first, with their turn aggregates. */
|
|
70
|
+
export function listSessionsResponse(ctx, params) {
|
|
71
|
+
const limit = intParam(params, "limit", 50);
|
|
72
|
+
if (limit < 1 || limit > MAX_RUN_PAGE)
|
|
73
|
+
throw invalidRequest(`limit must be an integer from 1 to ${MAX_RUN_PAGE}`, { limit });
|
|
74
|
+
return querySessionSummaries(ctx.db, { limit, cursor: optional(params, "cursor"), offset: intParam(params, "offset", 0) });
|
|
75
|
+
}
|
|
76
|
+
export function runDetailResponse(ctx, id) {
|
|
77
|
+
const found = getRunWithSummary(ctx.db, id);
|
|
78
|
+
if (!found)
|
|
79
|
+
throw runNotFound(id);
|
|
80
|
+
const { run, summary } = found;
|
|
81
|
+
const dirMissing = !existsSync(run.dirPath);
|
|
82
|
+
let runJson = null;
|
|
83
|
+
if (!dirMissing) {
|
|
84
|
+
try {
|
|
85
|
+
runJson = readRunJson(run.dirPath);
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
runJson = null;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
const children = listChildSummaries(ctx.db, id);
|
|
92
|
+
const childFeedback = {};
|
|
93
|
+
for (const child of children) {
|
|
94
|
+
const latest = latestFeedback(ctx.db, child.id);
|
|
95
|
+
if (latest)
|
|
96
|
+
childFeedback[child.id] = latest;
|
|
97
|
+
}
|
|
98
|
+
return {
|
|
99
|
+
run: { ...summary, instruction: runJson?.instruction ?? run.instruction },
|
|
100
|
+
runJson,
|
|
101
|
+
dirMissing,
|
|
102
|
+
children,
|
|
103
|
+
feedback: listFeedback(ctx.db, id),
|
|
104
|
+
childFeedback,
|
|
105
|
+
scores: listScores(ctx.db, id).map(({ details: { rubric: _rubric, ...details }, ...score }) => ({ ...score, details })),
|
|
106
|
+
escalations: listEscalations(ctx.db, id),
|
|
107
|
+
judgeDisagrees: judgeDisagrees(ctx.db, id),
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
/** The run's own `escalation` spans, oldest first. */
|
|
111
|
+
function listEscalations(db, runId) {
|
|
112
|
+
const text = (v) => (typeof v === "string" ? v : null);
|
|
113
|
+
const failure = (v) => {
|
|
114
|
+
if (typeof v !== "object" || v === null)
|
|
115
|
+
return null;
|
|
116
|
+
const { code, message } = v;
|
|
117
|
+
return typeof code === "string" && typeof message === "string" ? { code, message } : null;
|
|
118
|
+
};
|
|
119
|
+
return listSpans(db, runId)
|
|
120
|
+
.filter((s) => s.kind === "escalation")
|
|
121
|
+
.map((s) => ({
|
|
122
|
+
spanId: s.id,
|
|
123
|
+
at: s.startedAt,
|
|
124
|
+
trigger: text(s.attrs.trigger) ?? s.name,
|
|
125
|
+
reason: text(s.attrs.reason) ?? "",
|
|
126
|
+
phase: text(s.attrs.phase) ?? "",
|
|
127
|
+
applied: s.attrs.applied === true,
|
|
128
|
+
blockedBy: text(s.attrs.blockedBy),
|
|
129
|
+
blockedReason: text(s.attrs.blockedReason),
|
|
130
|
+
from: text(s.attrs.from),
|
|
131
|
+
to: text(s.attrs.to),
|
|
132
|
+
continuationError: failure(s.attrs.continuationError),
|
|
133
|
+
}));
|
|
134
|
+
}
|
|
135
|
+
/** Judge calibration (spec §7.4, §11): live agreement on current verdicts, stored `nexo eval judge` results and disagreements. Read-only. */
|
|
136
|
+
export function judgeCalibrationResponse(ctx) {
|
|
137
|
+
const key = ctx.judge.key;
|
|
138
|
+
const matrix = key === null ? null : confusionMatrix(judgeHumanPairs(ctx.db, key));
|
|
139
|
+
return {
|
|
140
|
+
judgeKey: key,
|
|
141
|
+
warnings: ctx.judge.warnings,
|
|
142
|
+
current: matrix ? { ...agreementStats(matrix), matrix } : null,
|
|
143
|
+
history: listCalibrations(ctx.db, key === null ? {} : { judgeKey: key }),
|
|
144
|
+
disagreements: listDisagreements(ctx.db, key === null ? {} : { judgeKey: key }),
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
export function spanTreeResponse(ctx, id) {
|
|
148
|
+
requireRun(ctx.db, id);
|
|
149
|
+
return loadSpanTree(ctx.db, id);
|
|
150
|
+
}
|
|
151
|
+
export function spanResponse(ctx, id, spanId) {
|
|
152
|
+
requireRun(ctx.db, id);
|
|
153
|
+
const span = getSpan(ctx.db, spanId);
|
|
154
|
+
if (!span || !descendantRunIds(ctx.db, id).includes(span.runId)) {
|
|
155
|
+
throw new NexoError({ code: "NOT_FOUND", message: `Span ${spanId} is not part of run ${id}`, context: { runId: id, spanId } });
|
|
156
|
+
}
|
|
157
|
+
return { span };
|
|
158
|
+
}
|
|
159
|
+
/** Throws STORE_IO (retryable, a 500) when the run.json mirror cannot be written; the feedback row is kept. */
|
|
160
|
+
export function feedbackResponse(ctx, id, body) {
|
|
161
|
+
if (typeof body !== "object" || body === null || Array.isArray(body))
|
|
162
|
+
throw invalidRequest("The request body must be a JSON object");
|
|
163
|
+
const { verdict, comment } = body;
|
|
164
|
+
return { feedback: recordFeedback(ctx.db, { runId: id, verdict, comment, nowIso: ctx.now().toISOString(), redact: ctx.redact, learn: ctx.learning.enabled }) };
|
|
165
|
+
}
|
|
166
|
+
/** `by` defaults to day here (the CLI defaults to model); `costReport` owns the `since` default and all validation. */
|
|
167
|
+
export function costResponse(ctx, params) {
|
|
168
|
+
return costReport(ctx.db, { by: optional(params, "by") ?? "day", since: optional(params, "since"), now: ctx.now() });
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Media types served under their own content type so the run page can play or show them. Only formats that cannot run
|
|
172
|
+
* script are listed: HTML, SVG and everything else stay `text/plain` or `application/octet-stream`.
|
|
173
|
+
*/
|
|
174
|
+
const MEDIA_TYPES = {
|
|
175
|
+
".mp4": "video/mp4",
|
|
176
|
+
".m4v": "video/mp4",
|
|
177
|
+
".webm": "video/webm",
|
|
178
|
+
".mov": "video/quicktime",
|
|
179
|
+
".mp3": "audio/mpeg",
|
|
180
|
+
".m4a": "audio/mp4",
|
|
181
|
+
".wav": "audio/wav",
|
|
182
|
+
".ogg": "audio/ogg",
|
|
183
|
+
".png": "image/png",
|
|
184
|
+
".jpg": "image/jpeg",
|
|
185
|
+
".jpeg": "image/jpeg",
|
|
186
|
+
".gif": "image/gif",
|
|
187
|
+
".webp": "image/webp",
|
|
188
|
+
};
|
|
189
|
+
export function mediaType(path) {
|
|
190
|
+
return MEDIA_TYPES[extname(path).toLowerCase()];
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Parses a single-range `Range` header against a file of `size` bytes. Returns null for no header or a multi-range
|
|
194
|
+
* request (served whole), the inclusive byte span for a satisfiable range, or "unsatisfiable".
|
|
195
|
+
*/
|
|
196
|
+
export function parseRange(header, size) {
|
|
197
|
+
if (header === undefined)
|
|
198
|
+
return null;
|
|
199
|
+
const m = /^bytes=(\d*)-(\d*)$/.exec(header.trim());
|
|
200
|
+
if (!m)
|
|
201
|
+
return /^bytes=[\d\s,-]+$/.test(header.trim()) && header.includes(",") ? null : "unsatisfiable";
|
|
202
|
+
const [, first, last] = m;
|
|
203
|
+
if (first === "") {
|
|
204
|
+
const suffix = Number(last);
|
|
205
|
+
if (last === "" || suffix === 0 || size === 0)
|
|
206
|
+
return "unsatisfiable";
|
|
207
|
+
return { start: Math.max(0, size - suffix), end: size - 1 };
|
|
208
|
+
}
|
|
209
|
+
const start = Number(first);
|
|
210
|
+
const end = last === "" ? size - 1 : Math.min(Number(last), size - 1);
|
|
211
|
+
if (start >= size || (last !== "" && Number(last) < start))
|
|
212
|
+
return "unsatisfiable";
|
|
213
|
+
return { start, end };
|
|
214
|
+
}
|
|
215
|
+
function looksBinary(sample) {
|
|
216
|
+
return sample.includes(0);
|
|
217
|
+
}
|
|
218
|
+
function entryKind(stat) {
|
|
219
|
+
if (stat.isSymbolicLink())
|
|
220
|
+
return "symlink";
|
|
221
|
+
if (stat.isDirectory())
|
|
222
|
+
return "dir";
|
|
223
|
+
return stat.isFile() ? "file" : "other";
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Streams `length` bytes from offset `start` of an open regular file into a response whose status line and
|
|
227
|
+
* content-length are already sent. A read error or a short read (the file shrank) destroys the connection, so the client sees a failed
|
|
228
|
+
* download instead of hanging on a body that never completes; neither can crash the server.
|
|
229
|
+
*/
|
|
230
|
+
export function pipeFileRange(res, fd, length, start = 0) {
|
|
231
|
+
let sent = 0;
|
|
232
|
+
const stream = createReadStream("", { fd, start, end: start + length - 1, autoClose: true });
|
|
233
|
+
stream.on("data", (chunk) => {
|
|
234
|
+
sent += chunk.length;
|
|
235
|
+
});
|
|
236
|
+
stream.on("error", (error) => res.destroy(error));
|
|
237
|
+
stream.on("end", () => {
|
|
238
|
+
if (sent < length)
|
|
239
|
+
res.destroy(new Error(`File ended after ${sent} of ${length} bytes`));
|
|
240
|
+
});
|
|
241
|
+
stream.pipe(res);
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Serves a file or a directory listing from a run directory. Paths go through `resolveConfined`, so `..` and
|
|
245
|
+
* symlinks cannot leave the run directory. Listings use lstat: a symlink is reported as `symlink` without following it.
|
|
246
|
+
* Only regular files are served (a FIFO or device would block the server); the file is opened non-blocking and checked
|
|
247
|
+
* with fstat, so it cannot be swapped between the check and the read. Media files (`mediaType`) are served whole under
|
|
248
|
+
* their own type with byte-range support so video can seek; every other body is `text/plain` or
|
|
249
|
+
* `application/octet-stream` with a sandbox CSP, so agent-written HTML or SVG never renders as a page.
|
|
250
|
+
*/
|
|
251
|
+
export function serveRunFile(ctx, res, id, relPath, full, range) {
|
|
252
|
+
const run = requireRun(ctx.db, id);
|
|
253
|
+
if (!existsSync(run.dirPath)) {
|
|
254
|
+
throw new NexoError({
|
|
255
|
+
code: "NOT_FOUND",
|
|
256
|
+
message: `The directory of run ${id} no longer exists`,
|
|
257
|
+
hint: "Run `nexo reindex` to drop index rows whose run directory was deleted",
|
|
258
|
+
context: { runId: id, dir: run.dirPath },
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
if (relPath.includes("\0"))
|
|
262
|
+
throw invalidRequest("Path contains a NUL byte");
|
|
263
|
+
let target;
|
|
264
|
+
try {
|
|
265
|
+
target = resolveConfined(run.dirPath, relPath === "" ? "." : relPath);
|
|
266
|
+
}
|
|
267
|
+
catch (error) {
|
|
268
|
+
if (error instanceof NexoError && error.code === "TOOL_DENIED") {
|
|
269
|
+
throw new NexoError({ code: "FORBIDDEN", message: `Path "${relPath}" is outside the run directory`, context: { runId: id, path: relPath } });
|
|
270
|
+
}
|
|
271
|
+
throw error;
|
|
272
|
+
}
|
|
273
|
+
let stat;
|
|
274
|
+
try {
|
|
275
|
+
stat = statSync(target);
|
|
276
|
+
}
|
|
277
|
+
catch {
|
|
278
|
+
throw new NexoError({ code: "NOT_FOUND", message: `No file "${relPath}" in run ${id}`, context: { runId: id, path: relPath } });
|
|
279
|
+
}
|
|
280
|
+
const root = resolveConfined(run.dirPath, ".");
|
|
281
|
+
const relOf = (abs) => relative(root, abs).split(sep).join("/");
|
|
282
|
+
if (stat.isDirectory()) {
|
|
283
|
+
const entries = [];
|
|
284
|
+
for (const name of readdirSync(target)) {
|
|
285
|
+
const abs = `${target}${sep}${name}`;
|
|
286
|
+
try {
|
|
287
|
+
const entry = lstatSync(abs);
|
|
288
|
+
const kind = entryKind(entry);
|
|
289
|
+
entries.push({ name, path: relOf(abs), kind, size: kind === "file" ? entry.size : 0 });
|
|
290
|
+
}
|
|
291
|
+
catch {
|
|
292
|
+
continue;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
entries.sort((a, b) => (a.kind === "dir") === (b.kind === "dir") ? a.name.localeCompare(b.name) : a.kind === "dir" ? -1 : 1);
|
|
296
|
+
const listing = { path: relOf(target), entries };
|
|
297
|
+
const text = JSON.stringify(listing);
|
|
298
|
+
res.writeHead(200, {
|
|
299
|
+
...SECURITY_HEADERS,
|
|
300
|
+
"content-type": "application/json; charset=utf-8",
|
|
301
|
+
"cache-control": "no-store",
|
|
302
|
+
"content-length": Buffer.byteLength(text),
|
|
303
|
+
});
|
|
304
|
+
res.end(text);
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
const notRegular = () => new NexoError({
|
|
308
|
+
code: "FORBIDDEN",
|
|
309
|
+
message: `"${relPath}" is not a regular file`,
|
|
310
|
+
hint: "Only regular files and directories can be viewed",
|
|
311
|
+
context: { runId: id, path: relPath },
|
|
312
|
+
});
|
|
313
|
+
if (!stat.isFile())
|
|
314
|
+
throw notRegular();
|
|
315
|
+
let fd;
|
|
316
|
+
try {
|
|
317
|
+
fd = openSync(target, constants.O_RDONLY | constants.O_NONBLOCK);
|
|
318
|
+
}
|
|
319
|
+
catch {
|
|
320
|
+
throw new NexoError({ code: "NOT_FOUND", message: `No file "${relPath}" in run ${id}`, context: { runId: id, path: relPath } });
|
|
321
|
+
}
|
|
322
|
+
let size;
|
|
323
|
+
let sample;
|
|
324
|
+
try {
|
|
325
|
+
const opened = fstatSync(fd);
|
|
326
|
+
if (!opened.isFile())
|
|
327
|
+
throw notRegular();
|
|
328
|
+
size = opened.size;
|
|
329
|
+
sample = Buffer.alloc(Math.min(size, 8192));
|
|
330
|
+
readSync(fd, sample, 0, sample.length, 0);
|
|
331
|
+
}
|
|
332
|
+
catch (error) {
|
|
333
|
+
closeSync(fd);
|
|
334
|
+
throw error;
|
|
335
|
+
}
|
|
336
|
+
const media = mediaType(target);
|
|
337
|
+
if (media !== undefined) {
|
|
338
|
+
serveMedia(res, fd, size, media, range);
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
const truncated = !full && size > MAX_FILE_PREVIEW_BYTES;
|
|
342
|
+
// The byte count is fixed when the file is opened: a file still being appended to (trajectory.jsonl of a live run)
|
|
343
|
+
// is served up to that length, which keeps content-length correct.
|
|
344
|
+
const length = truncated ? MAX_FILE_PREVIEW_BYTES : size;
|
|
345
|
+
res.writeHead(200, {
|
|
346
|
+
...SECURITY_HEADERS,
|
|
347
|
+
"content-type": looksBinary(sample) ? "application/octet-stream" : "text/plain; charset=utf-8",
|
|
348
|
+
"content-security-policy": "sandbox; default-src 'none'",
|
|
349
|
+
"cache-control": "no-store",
|
|
350
|
+
"content-length": length,
|
|
351
|
+
...(truncated ? { "x-nexo-truncated": String(size) } : {}),
|
|
352
|
+
});
|
|
353
|
+
if (length === 0) {
|
|
354
|
+
closeSync(fd);
|
|
355
|
+
res.end();
|
|
356
|
+
return;
|
|
357
|
+
}
|
|
358
|
+
pipeFileRange(res, fd, length);
|
|
359
|
+
}
|
|
360
|
+
/**
|
|
361
|
+
* Media bodies cannot run script, so they skip `sandbox`: a sandboxed media document has an opaque origin, and the
|
|
362
|
+
* same-origin resource policy would then block it from loading its own file when opened in a tab.
|
|
363
|
+
*/
|
|
364
|
+
const MEDIA_CSP = "default-src 'none'; media-src 'self'; img-src 'self'";
|
|
365
|
+
function serveMedia(res, fd, size, type, rangeHeader) {
|
|
366
|
+
const base = { ...SECURITY_HEADERS, "content-type": type, "content-security-policy": MEDIA_CSP, "cache-control": "no-store", "accept-ranges": "bytes" };
|
|
367
|
+
const range = parseRange(rangeHeader, size);
|
|
368
|
+
if (range === "unsatisfiable") {
|
|
369
|
+
closeSync(fd);
|
|
370
|
+
res.writeHead(416, { ...base, "content-range": `bytes */${size}`, "content-length": 0 });
|
|
371
|
+
res.end();
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
374
|
+
const start = range?.start ?? 0;
|
|
375
|
+
const length = range ? range.end - range.start + 1 : size;
|
|
376
|
+
res.writeHead(range ? 206 : 200, { ...base, "content-length": length, ...(range ? { "content-range": `bytes ${range.start}-${range.end}/${size}` } : {}) });
|
|
377
|
+
if (length === 0) {
|
|
378
|
+
closeSync(fd);
|
|
379
|
+
res.end();
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
382
|
+
pipeFileRange(res, fd, length, start);
|
|
383
|
+
}
|
|
384
|
+
function intParam(params, name, fallback) {
|
|
385
|
+
const raw = optional(params, name);
|
|
386
|
+
if (raw === undefined)
|
|
387
|
+
return fallback;
|
|
388
|
+
const value = /^\d+$/.test(raw) ? Number(raw) : Number.NaN;
|
|
389
|
+
if (!Number.isInteger(value))
|
|
390
|
+
throw invalidRequest(`${name} must be a positive integer`, { [name]: raw });
|
|
391
|
+
return value;
|
|
392
|
+
}
|
|
393
|
+
/** `GET /api/memories?scope=&type=&all=1&limit=&offset=&cursor=`: one page of live memories (all with `all=1`), newest first. */
|
|
394
|
+
export function memoriesResponse(ctx, params) {
|
|
395
|
+
return queryMemories(ctx.db, {
|
|
396
|
+
scope: optional(params, "scope"),
|
|
397
|
+
type: optional(params, "type"),
|
|
398
|
+
includeInactive: params.get("all") === "1",
|
|
399
|
+
limit: intParam(params, "limit", 200),
|
|
400
|
+
cursor: optional(params, "cursor"),
|
|
401
|
+
offset: intParam(params, "offset", 0),
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
export function memoryDetailResponse(ctx, id) {
|
|
405
|
+
const memory = getMemory(ctx.db, id);
|
|
406
|
+
if (!memory)
|
|
407
|
+
throw new NexoError({ code: "NOT_FOUND", message: `Memory ${id} does not exist`, context: { id } });
|
|
408
|
+
return { memory, history: memoryHistory(ctx.db, id) };
|
|
409
|
+
}
|
|
410
|
+
/** `POST /api/memories/{id}/retire`: a user action, so human and user memories can be retired too. */
|
|
411
|
+
export function retireMemoryResponse(ctx, id) {
|
|
412
|
+
return { memory: retireMemory(ctx.db, id, "user", ctx.now().toISOString()) };
|
|
413
|
+
}
|
|
414
|
+
function skillParam(name) {
|
|
415
|
+
if (!isSkillName(name))
|
|
416
|
+
throw invalidRequest("The playbook name must be a skill name", { name });
|
|
417
|
+
return name;
|
|
418
|
+
}
|
|
419
|
+
function versionView(a) {
|
|
420
|
+
return { id: a.id, version: a.version, origin: a.origin, status: a.status, createdAt: a.createdAt };
|
|
421
|
+
}
|
|
422
|
+
function itemView(ctx, item) {
|
|
423
|
+
return { ...item, history: playbookItemHistory(ctx.db, item.id) };
|
|
424
|
+
}
|
|
425
|
+
/** `GET /api/playbooks`: every skill with playbook items, with its active item count and version. */
|
|
426
|
+
export function playbooksResponse(ctx) {
|
|
427
|
+
return {
|
|
428
|
+
playbooks: listPlaybookSkills(ctx.db).map((skill) => {
|
|
429
|
+
const active = listArtifactVersions(ctx.db, "playbook", skill).find((a) => a.status === "active");
|
|
430
|
+
return { skill, activeItems: listPlaybookItems(ctx.db, skill).length, version: active?.version ?? null, origin: active?.origin ?? null };
|
|
431
|
+
}),
|
|
432
|
+
};
|
|
433
|
+
}
|
|
434
|
+
/** `GET /api/playbooks/{name}?all=1`: the items (retired ones with `all=1`) with their history, and every version. */
|
|
435
|
+
export function playbookResponse(ctx, name, params) {
|
|
436
|
+
const skill = skillParam(name);
|
|
437
|
+
return {
|
|
438
|
+
skill,
|
|
439
|
+
items: listPlaybookItems(ctx.db, skill, { includeRetired: params.get("all") === "1" }).map((i) => itemView(ctx, i)),
|
|
440
|
+
versions: listArtifactVersions(ctx.db, "playbook", skill).map(versionView),
|
|
441
|
+
};
|
|
442
|
+
}
|
|
443
|
+
/** `POST /api/playbooks/{name}/items/{key}/retire`: a user action; publishes a manual playbook version. */
|
|
444
|
+
export function retirePlaybookItemResponse(ctx, name, key) {
|
|
445
|
+
const skill = skillParam(name);
|
|
446
|
+
const nowIso = ctx.now().toISOString();
|
|
447
|
+
const version = transaction(ctx.db, () => {
|
|
448
|
+
retirePlaybookItem(ctx.db, skill, key, "user", nowIso);
|
|
449
|
+
return publishPlaybook(ctx.db, skill, "manual", nowIso);
|
|
450
|
+
});
|
|
451
|
+
const item = getPlaybookItem(ctx.db, skill, key);
|
|
452
|
+
return { item: itemView(ctx, item), version: version ? versionView(version) : null };
|
|
453
|
+
}
|
|
454
|
+
function optimItem(ctx, run) {
|
|
455
|
+
return {
|
|
456
|
+
...run,
|
|
457
|
+
baseVersion: getArtifact(ctx.db, run.baseArtifactId)?.version ?? null,
|
|
458
|
+
bestVersion: run.bestArtifactId ? (getArtifact(ctx.db, run.bestArtifactId)?.version ?? null) : null,
|
|
459
|
+
};
|
|
460
|
+
}
|
|
461
|
+
function requireOptimRun(ctx, id) {
|
|
462
|
+
const run = getOptimRun(ctx.db, id);
|
|
463
|
+
if (!run)
|
|
464
|
+
throw new NexoError({ code: "NOT_FOUND", message: `Optimization ${id} does not exist`, context: { optimRunId: id } });
|
|
465
|
+
return run;
|
|
466
|
+
}
|
|
467
|
+
/** The optimization's report.json; null when there is none or it cannot be read. */
|
|
468
|
+
function readOptimReport(run) {
|
|
469
|
+
if (!run.reportRef)
|
|
470
|
+
return null;
|
|
471
|
+
try {
|
|
472
|
+
return JSON.parse(readFileSync(join(run.dirPath, run.reportRef), "utf8"));
|
|
473
|
+
}
|
|
474
|
+
catch {
|
|
475
|
+
return null;
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
/** `GET /api/optim?limit=&offset=&cursor=`: one page of optimizations, newest first. */
|
|
479
|
+
export function optimListResponse(ctx, params) {
|
|
480
|
+
const { runs, nextCursor, total } = queryOptimRuns(ctx.db, { limit: intParam(params, "limit", 50), cursor: optional(params, "cursor"), offset: intParam(params, "offset", 0) });
|
|
481
|
+
return { runs: runs.map((run) => optimItem(ctx, run)), nextCursor, total };
|
|
482
|
+
}
|
|
483
|
+
/** `GET /api/optim/{id}`: the optimization, its candidates (lineage), its holdout report and whether it can be approved. */
|
|
484
|
+
export function optimDetailResponse(ctx, id) {
|
|
485
|
+
const run = requireOptimRun(ctx, id);
|
|
486
|
+
return {
|
|
487
|
+
run: optimItem(ctx, run),
|
|
488
|
+
candidates: listOptimCandidates(ctx.db, id).map((c) => {
|
|
489
|
+
const artifact = getArtifact(ctx.db, c.artifactId);
|
|
490
|
+
return { ...c, version: artifact?.version ?? null, artifactStatus: artifact?.status ?? null };
|
|
491
|
+
}),
|
|
492
|
+
report: readOptimReport(run),
|
|
493
|
+
promotion: checkPromotion(ctx.db, run, ctx.optimize.minJudgeKappa),
|
|
494
|
+
};
|
|
495
|
+
}
|
|
496
|
+
/** `POST /api/optim/{id}/approve`: a user action; refused with 409 (state) or 403 (judge-only evidence with low κ). */
|
|
497
|
+
export function approveOptimResponse(ctx, id) {
|
|
498
|
+
requireOptimRun(ctx, id);
|
|
499
|
+
const { promoted, replaced } = approveOptimRun(ctx.db, id, { minJudgeKappa: ctx.optimize.minJudgeKappa, nowIso: ctx.now().toISOString() });
|
|
500
|
+
return { promoted: versionView(promoted), replaced: replaced ? versionView(replaced) : null, run: optimItem(ctx, requireOptimRun(ctx, id)) };
|
|
501
|
+
}
|
|
502
|
+
/** `GET /api/learn/jobs?status=&limit=&offset=&cursor=`: job counts by status and one page of jobs, newest first. */
|
|
503
|
+
export function learnJobsResponse(ctx, params) {
|
|
504
|
+
const { jobs, nextCursor, total } = queryLearnJobs(ctx.db, { status: optional(params, "status"), limit: intParam(params, "limit", 50), cursor: optional(params, "cursor"), offset: intParam(params, "offset", 0) });
|
|
505
|
+
return {
|
|
506
|
+
counts: countLearnJobs(ctx.db),
|
|
507
|
+
jobs: jobs.map((j) => ({ id: j.id, runId: j.runId, kind: j.kind, trigger: j.trigger, status: j.status, attempts: j.attempts, lastError: j.lastError, createdAt: j.createdAt, updatedAt: j.updatedAt })),
|
|
508
|
+
nextCursor,
|
|
509
|
+
total,
|
|
510
|
+
};
|
|
511
|
+
}
|
|
512
|
+
function evalCaseItem(c) {
|
|
513
|
+
return { id: c.id, target: c.target, input: c.input, origin: c.origin, status: c.status, clusterSize: c.minedFrom?.length ?? 0, createdAt: c.createdAt };
|
|
514
|
+
}
|
|
515
|
+
/** `GET /api/eval-cases?status=proposed|active|dismissed`: cases in that status, newest first, at most 100. */
|
|
516
|
+
export function evalCasesResponse(ctx, params) {
|
|
517
|
+
const status = params.get("status") ?? "proposed";
|
|
518
|
+
if (status !== "proposed" && status !== "active" && status !== "dismissed") {
|
|
519
|
+
throw new NexoError({ code: "INVALID_REQUEST", message: `Unknown eval case status "${status}"`, hint: "Use proposed, active or dismissed", context: { status } });
|
|
520
|
+
}
|
|
521
|
+
return { cases: listEvalCasesByStatus(ctx.db, status, 100).map(evalCaseItem) };
|
|
522
|
+
}
|
|
523
|
+
/** `POST /api/eval-cases/{id}/accept|dismiss`: a user decision on a proposed case; 404 unknown, 409 not proposed. */
|
|
524
|
+
export function reviewEvalCaseResponse(ctx, id, decision) {
|
|
525
|
+
return { case: evalCaseItem(reviewProposedCase(ctx.db, id, decision, { redact: ctx.redact, nowIso: ctx.now().toISOString() })) };
|
|
526
|
+
}
|
|
527
|
+
function parseSummary(content) {
|
|
528
|
+
const head = content.slice(0, 2048);
|
|
529
|
+
const match = /^---\r?\n([\s\S]*?)\r?\n---/.exec(head);
|
|
530
|
+
if (!match || !match[1])
|
|
531
|
+
return undefined;
|
|
532
|
+
const lines = match[1].split("\n");
|
|
533
|
+
for (const line of lines) {
|
|
534
|
+
const m = /^summary:\s*(.*)$/i.exec(line.trim());
|
|
535
|
+
if (m && m[1])
|
|
536
|
+
return m[1].trim().replace(/^["']|["']$/g, "");
|
|
537
|
+
}
|
|
538
|
+
return undefined;
|
|
539
|
+
}
|
|
540
|
+
/** `GET /api/wikis`: lists all wiki directories under the wikis path. */
|
|
541
|
+
export function listWikisResponse(ctx) {
|
|
542
|
+
const dir = ctx.wikisDir ?? resolveNexoPaths().wikis;
|
|
543
|
+
if (!existsSync(dir))
|
|
544
|
+
return { wikis: [] };
|
|
545
|
+
const wikis = [];
|
|
546
|
+
for (const entry of readdirSync(dir)) {
|
|
547
|
+
const full = join(dir, entry);
|
|
548
|
+
try {
|
|
549
|
+
const st = statSync(full);
|
|
550
|
+
if (!st.isDirectory() || entry.startsWith("."))
|
|
551
|
+
continue;
|
|
552
|
+
let pageCount = 0;
|
|
553
|
+
let maxModified = st.mtime;
|
|
554
|
+
const scan = (d) => {
|
|
555
|
+
for (const f of readdirSync(d)) {
|
|
556
|
+
const fp = join(d, f);
|
|
557
|
+
try {
|
|
558
|
+
const fst = statSync(fp);
|
|
559
|
+
if (fst.isDirectory()) {
|
|
560
|
+
if (!f.startsWith(".") && f !== ".git")
|
|
561
|
+
scan(fp);
|
|
562
|
+
}
|
|
563
|
+
else if (f.endsWith(".md")) {
|
|
564
|
+
pageCount++;
|
|
565
|
+
if (fst.mtime > maxModified)
|
|
566
|
+
maxModified = fst.mtime;
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
catch {
|
|
570
|
+
// ignore
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
};
|
|
574
|
+
scan(full);
|
|
575
|
+
wikis.push({
|
|
576
|
+
name: entry,
|
|
577
|
+
pageCount,
|
|
578
|
+
modifiedAt: maxModified.toISOString(),
|
|
579
|
+
});
|
|
580
|
+
}
|
|
581
|
+
catch {
|
|
582
|
+
// ignore
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
wikis.sort((a, b) => a.name.localeCompare(b.name));
|
|
586
|
+
return { wikis };
|
|
587
|
+
}
|
|
588
|
+
/** `GET /api/wikis/{name}/pages`: lists all markdown pages in a wiki with summary metadata. */
|
|
589
|
+
export function listWikiPagesResponse(ctx, wikiName) {
|
|
590
|
+
const root = ctx.wikisDir ?? resolveNexoPaths().wikis;
|
|
591
|
+
let wikiDir;
|
|
592
|
+
try {
|
|
593
|
+
wikiDir = resolveConfined(root, wikiName);
|
|
594
|
+
}
|
|
595
|
+
catch (error) {
|
|
596
|
+
if (error instanceof NexoError && error.code === "TOOL_DENIED") {
|
|
597
|
+
throw new NexoError({ code: "FORBIDDEN", message: `Wiki "${wikiName}" is outside the wikis directory`, context: { wiki: wikiName } });
|
|
598
|
+
}
|
|
599
|
+
throw error;
|
|
600
|
+
}
|
|
601
|
+
if (!existsSync(wikiDir) || !statSync(wikiDir).isDirectory()) {
|
|
602
|
+
throw new NexoError({ code: "NOT_FOUND", message: `Wiki "${wikiName}" does not exist`, context: { wiki: wikiName } });
|
|
603
|
+
}
|
|
604
|
+
const pages = [];
|
|
605
|
+
const walk = (d) => {
|
|
606
|
+
for (const f of readdirSync(d)) {
|
|
607
|
+
if (f.startsWith(".") || f === ".git")
|
|
608
|
+
continue;
|
|
609
|
+
const fp = join(d, f);
|
|
610
|
+
try {
|
|
611
|
+
const st = statSync(fp);
|
|
612
|
+
if (st.isDirectory()) {
|
|
613
|
+
walk(fp);
|
|
614
|
+
}
|
|
615
|
+
else if (f.endsWith(".md")) {
|
|
616
|
+
const rel = relative(wikiDir, fp).split(sep).join("/");
|
|
617
|
+
const slug = rel.slice(0, -3);
|
|
618
|
+
const folder = rel.includes("/") ? rel.slice(0, rel.lastIndexOf("/")) : undefined;
|
|
619
|
+
const content = readFileSync(fp, "utf8");
|
|
620
|
+
const summary = parseSummary(content);
|
|
621
|
+
pages.push({
|
|
622
|
+
path: rel,
|
|
623
|
+
slug,
|
|
624
|
+
summary,
|
|
625
|
+
folder,
|
|
626
|
+
size: st.size,
|
|
627
|
+
modifiedAt: st.mtime.toISOString(),
|
|
628
|
+
});
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
catch {
|
|
632
|
+
// ignore
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
};
|
|
636
|
+
walk(wikiDir);
|
|
637
|
+
pages.sort((a, b) => a.path.localeCompare(b.path));
|
|
638
|
+
return { wiki: wikiName, pages };
|
|
639
|
+
}
|
|
640
|
+
/** `GET /api/wikis/{name}/page?path=`: returns the text and summary of a single wiki page. */
|
|
641
|
+
export function wikiPageResponse(ctx, wikiName, relPath) {
|
|
642
|
+
if (relPath.includes("\0"))
|
|
643
|
+
throw invalidRequest("Path contains a NUL byte");
|
|
644
|
+
const root = ctx.wikisDir ?? resolveNexoPaths().wikis;
|
|
645
|
+
let wikiDir;
|
|
646
|
+
try {
|
|
647
|
+
wikiDir = resolveConfined(root, wikiName);
|
|
648
|
+
}
|
|
649
|
+
catch (error) {
|
|
650
|
+
if (error instanceof NexoError && error.code === "TOOL_DENIED") {
|
|
651
|
+
throw new NexoError({ code: "FORBIDDEN", message: `Wiki "${wikiName}" is outside the wikis directory`, context: { wiki: wikiName } });
|
|
652
|
+
}
|
|
653
|
+
throw error;
|
|
654
|
+
}
|
|
655
|
+
if (!existsSync(wikiDir) || !statSync(wikiDir).isDirectory()) {
|
|
656
|
+
throw new NexoError({ code: "NOT_FOUND", message: `Wiki "${wikiName}" does not exist`, context: { wiki: wikiName } });
|
|
657
|
+
}
|
|
658
|
+
let target;
|
|
659
|
+
try {
|
|
660
|
+
target = resolveConfined(wikiDir, relPath);
|
|
661
|
+
}
|
|
662
|
+
catch (error) {
|
|
663
|
+
if (error instanceof NexoError && error.code === "TOOL_DENIED") {
|
|
664
|
+
throw new NexoError({ code: "FORBIDDEN", message: `Path "${relPath}" is outside the wiki directory`, context: { wiki: wikiName, path: relPath } });
|
|
665
|
+
}
|
|
666
|
+
throw error;
|
|
667
|
+
}
|
|
668
|
+
if (!existsSync(target) || !statSync(target).isFile()) {
|
|
669
|
+
throw new NexoError({ code: "NOT_FOUND", message: `No page "${relPath}" in wiki "${wikiName}"`, context: { wiki: wikiName, path: relPath } });
|
|
670
|
+
}
|
|
671
|
+
const content = ctx.redact(readFileSync(target, "utf8"));
|
|
672
|
+
const st = statSync(target);
|
|
673
|
+
const summary = parseSummary(content);
|
|
674
|
+
return {
|
|
675
|
+
wiki: wikiName,
|
|
676
|
+
path: relPath,
|
|
677
|
+
content,
|
|
678
|
+
summary,
|
|
679
|
+
size: st.size,
|
|
680
|
+
modifiedAt: st.mtime.toISOString(),
|
|
681
|
+
};
|
|
682
|
+
}
|