chapterhouse 0.8.2 → 0.9.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/dist/api/korg.js +2 -19
- package/dist/api/route-coverage.test.js +224 -0
- package/dist/api/server.js +238 -14
- package/dist/api/server.test.js +88 -2
- package/dist/shared/api-schemas.js +615 -0
- package/dist/store/db.js +1 -0
- package/dist/wiki/index-manager.js +1 -1
- package/package.json +1 -1
- package/web/dist/assets/{index-BbX9RKf3.js → index-iQrv3lQN.js} +155 -94
- package/web/dist/assets/index-iQrv3lQN.js.map +1 -0
- package/web/dist/index.html +1 -1
- package/dist/api/korg.test.js +0 -42
- package/web/dist/assets/index-BbX9RKf3.js.map +0 -1
package/dist/api/korg.js
CHANGED
|
@@ -1,28 +1,11 @@
|
|
|
1
|
-
import { sendToAgentSession } from "../copilot/orchestrator.js";
|
|
2
|
-
export async function routeKorgMessage(input) {
|
|
3
|
-
const message = input.message.trim();
|
|
4
|
-
const sessionId = input.session_id?.trim() || `korg-${Date.now()}`;
|
|
5
|
-
const sessionName = input.session_id?.trim() || message.slice(0, 80).trim() || sessionId;
|
|
6
|
-
const prompt = [
|
|
7
|
-
"Handle this request as Korg via the API.",
|
|
8
|
-
`Research session id: ${sessionId}`,
|
|
9
|
-
`Research session name: ${sessionName}`,
|
|
10
|
-
`When you ingest sources for this session, pass session_id: \"${sessionId}\" and session_name: \"${sessionName}\" to wiki_ingest_source.`,
|
|
11
|
-
"Reply directly to the user with the next best research action or synthesis.",
|
|
12
|
-
"",
|
|
13
|
-
message,
|
|
14
|
-
].join("\n");
|
|
15
|
-
const reply = await sendToAgentSession("korg", prompt);
|
|
16
|
-
return { ok: true, session_id: sessionId, reply };
|
|
17
|
-
}
|
|
18
1
|
export function listKorgResearchSessions(db) {
|
|
19
2
|
return db.prepare(`
|
|
20
3
|
SELECT
|
|
21
4
|
session_id AS id,
|
|
22
5
|
COALESCE(NULLIF(TRIM(session_name), ''), session_id) AS name,
|
|
23
6
|
COUNT(*) AS source_count,
|
|
24
|
-
0 AS
|
|
25
|
-
MAX(ingested_at) AS
|
|
7
|
+
0 AS open_questions_count,
|
|
8
|
+
MAX(ingested_at) AS last_activity_at
|
|
26
9
|
FROM wiki_sources
|
|
27
10
|
WHERE status = 'active'
|
|
28
11
|
AND session_id IS NOT NULL
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
// Route & SSE coverage — static analysis CI safeguard (P0 remediation, issue #369)
|
|
2
|
+
//
|
|
3
|
+
// Coverage status as of 2026-05-15:
|
|
4
|
+
// ──────────────────────────────────────────────────────────────────────────────
|
|
5
|
+
// GET /api/wiki/pages — tested (server.test.ts: coerced updated, frontmatter)
|
|
6
|
+
// GET /api/wiki/browser-pages — tested (server.test.ts: browser contract, orphan fallback)
|
|
7
|
+
// GET /api/wiki/sources — tested (server.test.ts: empty payload, page filter)
|
|
8
|
+
// GET /api/wiki/links — tested (server.test.ts: all links, empty, page filter)
|
|
9
|
+
// GET /api/wiki/page — tested (server.test.ts: CRUD, welcome page synthesis, 404)
|
|
10
|
+
// PUT /api/wiki/page — tested (server.test.ts: wiki CRUD create flow)
|
|
11
|
+
// DELETE /api/wiki/page — tested (server.test.ts: wiki CRUD delete flow)
|
|
12
|
+
// GET /api/wiki/search — NOT tested (add in Phase 3c)
|
|
13
|
+
// POST /api/wiki/update — static auth guard tested below
|
|
14
|
+
// POST /api/wiki/page/pin — auth + traversal guard tested
|
|
15
|
+
// POST /api/wiki/ingest — NOT tested (add in Phase 3c)
|
|
16
|
+
// GET /api/wiki/korg/sessions — tested (server.test.ts: grouped active sessions)
|
|
17
|
+
// ──────────────────────────────────────────────────────────────────────────────
|
|
18
|
+
import { describe, test } from "node:test";
|
|
19
|
+
import assert from "node:assert/strict";
|
|
20
|
+
import { readFileSync } from "node:fs";
|
|
21
|
+
import { join } from "node:path";
|
|
22
|
+
import { fileURLToPath } from "node:url";
|
|
23
|
+
const __dirname = fileURLToPath(new URL(".", import.meta.url));
|
|
24
|
+
const ROOT = join(__dirname, "..", "..");
|
|
25
|
+
const FRONTEND_API_PATH = join(ROOT, "web", "src", "api.ts");
|
|
26
|
+
const SERVER_TS_PATH = join(ROOT, "src", "api", "server.ts");
|
|
27
|
+
const WEB_SCHEMAS_PATH = join(ROOT, "src", "shared", "api-schemas.ts");
|
|
28
|
+
/**
|
|
29
|
+
* Normalise a URL path for comparison:
|
|
30
|
+
* - Replace complete template-literal expressions ${...} with :param
|
|
31
|
+
* - Strip incomplete trailing template expressions (e.g. `${query ` cut by `?`)
|
|
32
|
+
* - Replace Express named params :slug with :param
|
|
33
|
+
* - Strip trailing slashes
|
|
34
|
+
*/
|
|
35
|
+
function normalizePath(raw) {
|
|
36
|
+
return raw
|
|
37
|
+
.replace(/\$\{[^}]*\}/g, ":param") // complete ${...} → :param
|
|
38
|
+
.replace(/\$\{[^}]*$/, "") // trailing incomplete ${... (cut by `?` in regex)
|
|
39
|
+
.replace(/:[a-zA-Z_][a-zA-Z0-9_]*/g, ":param") // :slug → :param
|
|
40
|
+
.replace(/\/+$/, "") // strip trailing slash
|
|
41
|
+
.trim();
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Extract all unique normalised API paths called via authedFetch() in the
|
|
45
|
+
* frontend api.ts. Only static string / template-literal arguments are
|
|
46
|
+
* detected — calls that pass a pre-constructed variable are skipped.
|
|
47
|
+
*/
|
|
48
|
+
function extractFrontendPaths(src) {
|
|
49
|
+
// Matches authedFetch( then an opening quote/backtick then /api/...
|
|
50
|
+
// Stops at the same quote character, or at `?` (query string start).
|
|
51
|
+
const re = /authedFetch\(["'`](\/api\/[^"'`?]+)/g;
|
|
52
|
+
const paths = new Set();
|
|
53
|
+
for (const m of src.matchAll(re)) {
|
|
54
|
+
const normalised = normalizePath(m[1]);
|
|
55
|
+
if (normalised) {
|
|
56
|
+
paths.add(normalised);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return paths;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Extract all unique normalised API paths registered via app.METHOD() in
|
|
63
|
+
* server.ts. Only /api/ prefixed paths are considered.
|
|
64
|
+
*/
|
|
65
|
+
function extractServerPaths(src) {
|
|
66
|
+
const re = /app\.(get|post|put|delete|patch)\(["'`](\/api\/[^"'`?]+)/g;
|
|
67
|
+
const paths = new Set();
|
|
68
|
+
for (const m of src.matchAll(re)) {
|
|
69
|
+
const normalised = normalizePath(m[2]);
|
|
70
|
+
if (normalised) {
|
|
71
|
+
paths.add(normalised);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return paths;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Extract SSE type literals emitted inline via formatSseData({ type: "X", ... })
|
|
78
|
+
* in server.ts. Pass-through calls like formatSseData(event) are not detected.
|
|
79
|
+
*/
|
|
80
|
+
function extractSseDataTypes(src) {
|
|
81
|
+
// Allow up to ~200 chars between `formatSseData({` and `type: "X"` to
|
|
82
|
+
// handle multi-line object literals (queued, queue-advance, etc.).
|
|
83
|
+
const re = /formatSseData\(\{[\s\S]{0,200}?type:\s*["']([^"']+)["']/g;
|
|
84
|
+
const types = new Set();
|
|
85
|
+
for (const m of src.matchAll(re)) {
|
|
86
|
+
types.add(m[1]);
|
|
87
|
+
}
|
|
88
|
+
return types;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Extract the first argument (event name) from all formatSseEvent("name", ...)
|
|
92
|
+
* calls in server.ts.
|
|
93
|
+
*/
|
|
94
|
+
function extractSseEventNames(src) {
|
|
95
|
+
const re = /formatSseEvent\(["']([^"']+)["']/g;
|
|
96
|
+
const names = new Set();
|
|
97
|
+
for (const m of src.matchAll(re)) {
|
|
98
|
+
names.add(m[1]);
|
|
99
|
+
}
|
|
100
|
+
return names;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Extract the `type` literal values from StreamEventSchema in web/src/api-schemas.ts
|
|
104
|
+
* by scanning for `type: z.literal("X")` patterns.
|
|
105
|
+
*
|
|
106
|
+
* These are the only valid SSE frame types the frontend will parse via the
|
|
107
|
+
* default `message` EventSource handler.
|
|
108
|
+
*/
|
|
109
|
+
function extractStreamEventSchemaTypes(src) {
|
|
110
|
+
// Locate the StreamEventSchema block first so we don't accidentally pick up
|
|
111
|
+
// literals from other schemas.
|
|
112
|
+
const startIdx = src.indexOf("export const StreamEventSchema");
|
|
113
|
+
if (startIdx === -1) {
|
|
114
|
+
throw new Error("StreamEventSchema not found in src/shared/api-schemas.ts");
|
|
115
|
+
}
|
|
116
|
+
// Take a generous slice (10 000 chars) — the schema is large but finite.
|
|
117
|
+
const block = src.slice(startIdx, startIdx + 10_000);
|
|
118
|
+
const re = /type:\s*z\.literal\(["']([^"']+)["']\)/g;
|
|
119
|
+
const types = new Set();
|
|
120
|
+
for (const m of block.matchAll(re)) {
|
|
121
|
+
types.add(m[1]);
|
|
122
|
+
}
|
|
123
|
+
return types;
|
|
124
|
+
}
|
|
125
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
126
|
+
// Tests
|
|
127
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
128
|
+
describe("route coverage — static analysis", () => {
|
|
129
|
+
test("all frontend authedFetch paths have a matching server route registration", () => {
|
|
130
|
+
const frontendSrc = readFileSync(FRONTEND_API_PATH, "utf8");
|
|
131
|
+
const serverSrc = readFileSync(SERVER_TS_PATH, "utf8");
|
|
132
|
+
const frontendPaths = extractFrontendPaths(frontendSrc);
|
|
133
|
+
const serverPaths = extractServerPaths(serverSrc);
|
|
134
|
+
const missing = [];
|
|
135
|
+
for (const fp of [...frontendPaths].sort()) {
|
|
136
|
+
if (!serverPaths.has(fp)) {
|
|
137
|
+
missing.push(fp);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
assert.ok(missing.length === 0, `Frontend calls ${missing.length} route(s) with no matching server registration:\n` +
|
|
141
|
+
missing.map((p) => ` MISSING: ${p}`).join("\n") +
|
|
142
|
+
"\n\nAdd the missing route(s) to src/api/server.ts");
|
|
143
|
+
});
|
|
144
|
+
test("server routes cover every unique normalised path (no obvious duplicates leaked)", () => {
|
|
145
|
+
const serverSrc = readFileSync(SERVER_TS_PATH, "utf8");
|
|
146
|
+
const serverPaths = extractServerPaths(serverSrc);
|
|
147
|
+
// Sanity: the server must expose at least 30 /api/ routes.
|
|
148
|
+
assert.ok(serverPaths.size >= 30, `Expected ≥30 server routes, found ${serverPaths.size}. Check extractServerPaths regex.`);
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
describe("wiki write route safeguards — static analysis", () => {
|
|
152
|
+
test("wiki update and pin routes declare authMiddleware explicitly", () => {
|
|
153
|
+
const serverSrc = readFileSync(SERVER_TS_PATH, "utf8");
|
|
154
|
+
assert.match(serverSrc, /app\.post\("\/api\/wiki\/update",\s*authMiddleware,\s*async \(req: Request, res: Response\) => \{/);
|
|
155
|
+
assert.match(serverSrc, /app\.post\("\/api\/wiki\/page\/pin",\s*authMiddleware,\s*async \(req: Request, res: Response\) => \{/);
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
describe("SSE exhaustiveness — static analysis", () => {
|
|
159
|
+
// ───────────────────────────────────────────────────────────────────────────
|
|
160
|
+
// REGRESSION CANARY for issues #367 / #368:
|
|
161
|
+
//
|
|
162
|
+
// `formatSseEvent("status", payload)` generates an SSE *named event*:
|
|
163
|
+
// event: status\ndata: {...}\n\n
|
|
164
|
+
//
|
|
165
|
+
// The frontend's EventSource only handles the default `message` event.
|
|
166
|
+
// Named events are silently dropped — the frontend never sees them.
|
|
167
|
+
//
|
|
168
|
+
// If any future code adds `formatSseEvent("X", ...)` where "X" is also a
|
|
169
|
+
// type in StreamEventSchema, this test FAILS, preventing the regression.
|
|
170
|
+
// ───────────────────────────────────────────────────────────────────────────
|
|
171
|
+
test("formatSseEvent is not used with names that belong in the StreamEventSchema data channel", () => {
|
|
172
|
+
const serverSrc = readFileSync(SERVER_TS_PATH, "utf8");
|
|
173
|
+
const schemaSrc = readFileSync(WEB_SCHEMAS_PATH, "utf8");
|
|
174
|
+
const namedEventNames = extractSseEventNames(serverSrc);
|
|
175
|
+
const schemaTypes = extractStreamEventSchemaTypes(schemaSrc);
|
|
176
|
+
const conflicts = [];
|
|
177
|
+
for (const name of namedEventNames) {
|
|
178
|
+
if (schemaTypes.has(name)) {
|
|
179
|
+
conflicts.push(name);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
assert.ok(conflicts.length === 0, `formatSseEvent() is being used with name(s) that are also StreamEventSchema types:\n` +
|
|
183
|
+
conflicts.map((n) => ` CONFLICT: formatSseEvent("${n}", ...) — must use formatSseData({ type: "${n}", ... }) instead`).join("\n") +
|
|
184
|
+
"\n\nNamed SSE events are silently dropped by the frontend's default 'message' handler (issues #367/#368).");
|
|
185
|
+
});
|
|
186
|
+
test("all inline formatSseData type literals are known StreamEventSchema types or agent-stream-only types", () => {
|
|
187
|
+
const serverSrc = readFileSync(SERVER_TS_PATH, "utf8");
|
|
188
|
+
const schemaSrc = readFileSync(WEB_SCHEMAS_PATH, "utf8");
|
|
189
|
+
const emittedTypes = extractSseDataTypes(serverSrc);
|
|
190
|
+
const schemaTypes = extractStreamEventSchemaTypes(schemaSrc);
|
|
191
|
+
// Types emitted only on /api/agents/stream — not part of the main chat
|
|
192
|
+
// StreamEventSchema but intentional (received by a separate subscriber).
|
|
193
|
+
const agentStreamOnlyTypes = new Set(["agent_event"]);
|
|
194
|
+
const unknown = [];
|
|
195
|
+
for (const t of [...emittedTypes].sort()) {
|
|
196
|
+
if (!schemaTypes.has(t) && !agentStreamOnlyTypes.has(t)) {
|
|
197
|
+
unknown.push(t);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
assert.ok(unknown.length === 0, `formatSseData emits type(s) not present in StreamEventSchema:\n` +
|
|
201
|
+
unknown.map((t) => ` UNKNOWN: "${t}"`).join("\n") +
|
|
202
|
+
"\n\nEither add these to StreamEventSchema in src/shared/api-schemas.ts, " +
|
|
203
|
+
"or add them to the agentStreamOnlyTypes allowlist in this test.");
|
|
204
|
+
});
|
|
205
|
+
test("StreamEventSchema covers at least the core chat stream types", () => {
|
|
206
|
+
const schemaSrc = readFileSync(WEB_SCHEMAS_PATH, "utf8");
|
|
207
|
+
const schemaTypes = extractStreamEventSchemaTypes(schemaSrc);
|
|
208
|
+
const required = [
|
|
209
|
+
"connected",
|
|
210
|
+
"delta",
|
|
211
|
+
"message",
|
|
212
|
+
"cancelled",
|
|
213
|
+
"status",
|
|
214
|
+
"queued",
|
|
215
|
+
"activity",
|
|
216
|
+
"queue-advance",
|
|
217
|
+
"turn-interrupted",
|
|
218
|
+
];
|
|
219
|
+
const missing = required.filter((t) => !schemaTypes.has(t));
|
|
220
|
+
assert.ok(missing.length === 0, `StreamEventSchema is missing expected type(s): ${missing.join(", ")}\n` +
|
|
221
|
+
"These types were removed or renamed — update the schema and this test together.");
|
|
222
|
+
});
|
|
223
|
+
});
|
|
224
|
+
//# sourceMappingURL=route-coverage.test.js.map
|
package/dist/api/server.js
CHANGED
|
@@ -16,7 +16,7 @@ import { createAuthMiddleware, getBootstrapAuthResponse } from "./auth.js";
|
|
|
16
16
|
import { assertAgentEditAccess } from "./agent-edit-access.js";
|
|
17
17
|
import { createConcurrentConnectionLimiter, createFixedWindowRateLimiter } from "./rate-limit.js";
|
|
18
18
|
import { createTeamRouter } from "./team.js";
|
|
19
|
-
import { writePage, deletePage, pageExists, listPages, ensureWikiStructure, assertPagePath, getWikiDir, } from "../wiki/fs.js";
|
|
19
|
+
import { readPage, writePage, deletePage, pageExists, listPages, ensureWikiStructure, assertPagePath, getWikiDir, } from "../wiki/fs.js";
|
|
20
20
|
import { parseWikiFrontmatter } from "../wiki/frontmatter.js";
|
|
21
21
|
import { normalizeWikiPath } from "../wiki/path-utils.js";
|
|
22
22
|
import { loadRegistry, saveRegistry } from "../wiki/project-registry.js";
|
|
@@ -30,7 +30,7 @@ import { getCurrentRunId, getDb, getSessionMessages, getTaskEvents } from "../st
|
|
|
30
30
|
import { getTaskLogEvents, subscribeTaskLog } from "../copilot/task-event-log.js";
|
|
31
31
|
import { subscribeSession, getSessionEventsFromDb, getSessionMaxSeqFromDb, oldestSessionSeq, } from "../copilot/turn-event-log.js";
|
|
32
32
|
import { getStatus, onStatusChange } from "../status.js";
|
|
33
|
-
import { formatSseData
|
|
33
|
+
import { formatSseData } from "./sse.js";
|
|
34
34
|
import { assertAuthenticationConfigured, createHealthPayload, createPublicConfigPayload, getDisplayHost, resolveApiToken, shouldServeSpaPath, } from "./server-runtime.js";
|
|
35
35
|
import { BadRequestError, ForbiddenError, InternalServerError, NotFoundError, apiNotFoundHandler, asBadRequest, createApiErrorHandler, parseRequest, } from "./errors.js";
|
|
36
36
|
import { childLogger } from "../util/logger.js";
|
|
@@ -41,7 +41,8 @@ import { recordObservation } from "../memory/observations.js";
|
|
|
41
41
|
import { recordDecision } from "../memory/decisions.js";
|
|
42
42
|
import { upsertEntity } from "../memory/entities.js";
|
|
43
43
|
import { getInboxItem, listPendingInboxItems, resolveInboxItem } from "../memory/inbox.js";
|
|
44
|
-
import {
|
|
44
|
+
import { ingestSource } from "../wiki/ingest.js";
|
|
45
|
+
import { listKorgResearchSessions } from "./korg.js";
|
|
45
46
|
const log = childLogger("server");
|
|
46
47
|
const modeContext = new ModeContext(config);
|
|
47
48
|
void searchIndex; // re-exported by index-manager; reference here documents the dep
|
|
@@ -145,10 +146,6 @@ const prMergeHookSchema = z.object({
|
|
|
145
146
|
body: z.string().optional(),
|
|
146
147
|
files_changed: z.array(z.string()).optional(),
|
|
147
148
|
});
|
|
148
|
-
const korgRequestSchema = z.object({
|
|
149
|
-
message: requiredString("Missing 'message' in request body"),
|
|
150
|
-
session_id: z.string().trim().min(1).optional(),
|
|
151
|
-
}).strict();
|
|
152
149
|
const projectHardRulesSchema = z.object({
|
|
153
150
|
hardRules: z.object({
|
|
154
151
|
auto_pr: z.boolean({ error: "hardRules.auto_pr must be a boolean" }),
|
|
@@ -205,6 +202,84 @@ function coerceWikiPageUpdated(path, updated) {
|
|
|
205
202
|
return "unknown";
|
|
206
203
|
}
|
|
207
204
|
}
|
|
205
|
+
function wikiPathToBrowserSlug(path) {
|
|
206
|
+
return path.replace(/^pages\//, "").replace(/\.md$/, "");
|
|
207
|
+
}
|
|
208
|
+
function entityTypeFromPath(path) {
|
|
209
|
+
const rest = path.startsWith("pages/") ? path.slice("pages/".length) : path;
|
|
210
|
+
const segs = rest.split("/").filter(Boolean);
|
|
211
|
+
if (segs.length <= 1)
|
|
212
|
+
return (segs[0] || "pages").replace(/\.md$/i, "");
|
|
213
|
+
return segs[0];
|
|
214
|
+
}
|
|
215
|
+
function normalizeOptionalQueryParam(value) {
|
|
216
|
+
if (typeof value !== "string") {
|
|
217
|
+
return undefined;
|
|
218
|
+
}
|
|
219
|
+
const trimmed = value.trim();
|
|
220
|
+
return trimmed ? trimmed : undefined;
|
|
221
|
+
}
|
|
222
|
+
function matchesWikiBrowserFilters(page, filters) {
|
|
223
|
+
if (filters.type && page.type !== filters.type) {
|
|
224
|
+
return false;
|
|
225
|
+
}
|
|
226
|
+
if (!filters.q) {
|
|
227
|
+
return true;
|
|
228
|
+
}
|
|
229
|
+
const query = filters.q.toLowerCase();
|
|
230
|
+
return page.title.toLowerCase().includes(query) || page.summary.toLowerCase().includes(query);
|
|
231
|
+
}
|
|
232
|
+
function mapIndexEntryToBrowserPage(entry) {
|
|
233
|
+
return {
|
|
234
|
+
slug: wikiPathToBrowserSlug(entry.path),
|
|
235
|
+
title: entry.title,
|
|
236
|
+
summary: entry.summary,
|
|
237
|
+
type: entry.section,
|
|
238
|
+
last_updated: coerceWikiPageUpdated(entry.path, entry.updated),
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
function listFallbackWikiBrowserPages(filters) {
|
|
242
|
+
const entries = parseIndex();
|
|
243
|
+
const indexed = new Set(entries.map((entry) => entry.path));
|
|
244
|
+
const indexedResults = entries.map(mapIndexEntryToBrowserPage);
|
|
245
|
+
const orphanResults = listPages()
|
|
246
|
+
.filter((path) => !indexed.has(path))
|
|
247
|
+
.map((path) => ({
|
|
248
|
+
slug: wikiPathToBrowserSlug(path),
|
|
249
|
+
title: path,
|
|
250
|
+
summary: "",
|
|
251
|
+
type: "Unindexed",
|
|
252
|
+
last_updated: coerceWikiPageUpdated(path, undefined),
|
|
253
|
+
}));
|
|
254
|
+
return [...indexedResults, ...orphanResults].filter((page) => matchesWikiBrowserFilters(page, filters));
|
|
255
|
+
}
|
|
256
|
+
function listDbWikiBrowserPages(filters) {
|
|
257
|
+
const db = getDb();
|
|
258
|
+
const clauses = [];
|
|
259
|
+
const params = [];
|
|
260
|
+
if (filters.type) {
|
|
261
|
+
clauses.push("(entity_type = ? OR (entity_type IS NULL AND path LIKE ?))");
|
|
262
|
+
params.push(filters.type, `pages/${filters.type}/%`);
|
|
263
|
+
}
|
|
264
|
+
if (filters.q) {
|
|
265
|
+
clauses.push("(title LIKE ? COLLATE NOCASE OR COALESCE(summary, '') LIKE ? COLLATE NOCASE)");
|
|
266
|
+
params.push(`%${filters.q}%`, `%${filters.q}%`);
|
|
267
|
+
}
|
|
268
|
+
const rows = db.prepare(`
|
|
269
|
+
SELECT path, title, summary, entity_type, last_updated, pinned
|
|
270
|
+
FROM wiki_pages
|
|
271
|
+
${clauses.length > 0 ? `WHERE ${clauses.join(" AND ")}` : ""}
|
|
272
|
+
ORDER BY COALESCE(last_updated, '') DESC, title ASC
|
|
273
|
+
`).all(...params);
|
|
274
|
+
return rows.map((row) => ({
|
|
275
|
+
slug: wikiPathToBrowserSlug(row.path),
|
|
276
|
+
title: row.title,
|
|
277
|
+
summary: row.summary ?? "",
|
|
278
|
+
type: row.entity_type ?? entityTypeFromPath(row.path),
|
|
279
|
+
last_updated: coerceWikiPageUpdated(row.path, row.last_updated ?? undefined),
|
|
280
|
+
...(row.pinned ? { pinned: true } : {}),
|
|
281
|
+
}));
|
|
282
|
+
}
|
|
208
283
|
function parseWikiSourcePages(value) {
|
|
209
284
|
if (!value) {
|
|
210
285
|
return [];
|
|
@@ -838,11 +913,11 @@ app.get("/stream", (req, res) => {
|
|
|
838
913
|
}
|
|
839
914
|
sseClients.set(connectionId, res);
|
|
840
915
|
const unsubscribeStatus = onStatusChange((status, message) => {
|
|
841
|
-
res.write(
|
|
916
|
+
res.write(formatSseData({ type: "status", status, message }));
|
|
842
917
|
});
|
|
843
918
|
const currentStatus = getStatus();
|
|
844
919
|
if (currentStatus.status !== "idle") {
|
|
845
|
-
res.write(
|
|
920
|
+
res.write(formatSseData({ type: "status", ...currentStatus }));
|
|
846
921
|
}
|
|
847
922
|
const heartbeat = setInterval(() => {
|
|
848
923
|
res.write(`:ping\n\n`);
|
|
@@ -1544,6 +1619,19 @@ app.get("/api/wiki/pages", async (req, res) => {
|
|
|
1544
1619
|
}));
|
|
1545
1620
|
res.json([...indexedResults, ...orphanResults]);
|
|
1546
1621
|
});
|
|
1622
|
+
app.get("/api/wiki/browser-pages", async (req, res) => {
|
|
1623
|
+
ensureWikiStructure();
|
|
1624
|
+
const filters = {
|
|
1625
|
+
q: normalizeOptionalQueryParam(req.query.q),
|
|
1626
|
+
type: normalizeOptionalQueryParam(req.query.type),
|
|
1627
|
+
};
|
|
1628
|
+
const db = getDb();
|
|
1629
|
+
const { count } = db.prepare("SELECT COUNT(*) AS count FROM wiki_pages").get();
|
|
1630
|
+
const pages = count > 0
|
|
1631
|
+
? listDbWikiBrowserPages(filters)
|
|
1632
|
+
: listFallbackWikiBrowserPages(filters);
|
|
1633
|
+
res.json({ pages });
|
|
1634
|
+
});
|
|
1547
1635
|
app.get("/api/wiki/sources", async (req, res) => {
|
|
1548
1636
|
ensureWikiStructure();
|
|
1549
1637
|
res.json({ sources: listWikiSources(readOptionalPageFilter(req)) });
|
|
@@ -1553,6 +1641,33 @@ app.get("/api/wiki/links", async (req, res) => {
|
|
|
1553
1641
|
res.json({ links: listWikiLinks(readOptionalPageFilter(req)) });
|
|
1554
1642
|
});
|
|
1555
1643
|
app.get("/api/wiki/page", async (req, res) => {
|
|
1644
|
+
const slugParam = normalizeOptionalQueryParam(req.query.slug);
|
|
1645
|
+
if (slugParam) {
|
|
1646
|
+
const path = `pages/${slugParam}.md`;
|
|
1647
|
+
assertValidPagePath(path);
|
|
1648
|
+
const db = getDb();
|
|
1649
|
+
const row = db.prepare("SELECT title, entity_type, last_updated, pinned FROM wiki_pages WHERE path = ?").get(path);
|
|
1650
|
+
const authorizationHeader = typeof req.headers.authorization === "string"
|
|
1651
|
+
? req.headers.authorization
|
|
1652
|
+
: undefined;
|
|
1653
|
+
const rawContent = await readWikiPage(path, { authorizationHeader });
|
|
1654
|
+
if (rawContent === undefined) {
|
|
1655
|
+
throw new NotFoundError("Page not found");
|
|
1656
|
+
}
|
|
1657
|
+
const { parsed: frontmatter } = parseWikiFrontmatter(rawContent);
|
|
1658
|
+
res.json({
|
|
1659
|
+
page: {
|
|
1660
|
+
slug: slugParam,
|
|
1661
|
+
title: row?.title ?? slugParam,
|
|
1662
|
+
type: row?.entity_type ?? "topics",
|
|
1663
|
+
last_updated: row?.last_updated ?? coerceWikiPageUpdated(path, undefined),
|
|
1664
|
+
compiled_truth: rawContent,
|
|
1665
|
+
pinned: Boolean(row?.pinned),
|
|
1666
|
+
frontmatter,
|
|
1667
|
+
},
|
|
1668
|
+
});
|
|
1669
|
+
return;
|
|
1670
|
+
}
|
|
1556
1671
|
const path = assertValidPagePath(readPathParam(req));
|
|
1557
1672
|
const authorizationHeader = typeof req.headers.authorization === "string"
|
|
1558
1673
|
? req.headers.authorization
|
|
@@ -1577,19 +1692,128 @@ app.put("/api/wiki/page", async (req, res) => {
|
|
|
1577
1692
|
});
|
|
1578
1693
|
res.json({ ok: true, created, path });
|
|
1579
1694
|
});
|
|
1695
|
+
const wikiUpdateSchema = z.object({
|
|
1696
|
+
slug: requiredString("Missing 'slug' in request body"),
|
|
1697
|
+
compiled_truth: z.string().optional(),
|
|
1698
|
+
frontmatter: z.record(z.string(), z.unknown()).optional(),
|
|
1699
|
+
});
|
|
1700
|
+
app.post("/api/wiki/update", authMiddleware, async (req, res) => {
|
|
1701
|
+
const { slug, compiled_truth, frontmatter: newFrontmatter } = parseRequest(wikiUpdateSchema, req.body);
|
|
1702
|
+
const path = assertValidPagePath(`pages/${slug}.md`);
|
|
1703
|
+
let finalContent = compiled_truth;
|
|
1704
|
+
if (newFrontmatter !== undefined) {
|
|
1705
|
+
const existing = readPage(path);
|
|
1706
|
+
const base = finalContent ?? existing ?? "";
|
|
1707
|
+
const { parsed: existingFrontmatter, body } = parseWikiFrontmatter(base);
|
|
1708
|
+
const merged = { ...existingFrontmatter, ...newFrontmatter };
|
|
1709
|
+
const fmLines = Object.entries(merged).map(([k, v]) => `${k}: ${JSON.stringify(v)}`).join("\n");
|
|
1710
|
+
finalContent = `---\n${fmLines}\n---\n${body}`;
|
|
1711
|
+
}
|
|
1712
|
+
if (finalContent !== undefined) {
|
|
1713
|
+
await withWikiWrite(() => writePage(path, finalContent));
|
|
1714
|
+
}
|
|
1715
|
+
const db = getDb();
|
|
1716
|
+
const row = db.prepare("SELECT title, entity_type, last_updated, pinned FROM wiki_pages WHERE path = ?").get(path);
|
|
1717
|
+
const content = readPage(path) ?? finalContent ?? "";
|
|
1718
|
+
const { parsed: frontmatter } = parseWikiFrontmatter(content);
|
|
1719
|
+
res.json({
|
|
1720
|
+
ok: true,
|
|
1721
|
+
page: {
|
|
1722
|
+
slug,
|
|
1723
|
+
title: row?.title ?? slug,
|
|
1724
|
+
type: row?.entity_type ?? "topics",
|
|
1725
|
+
last_updated: row?.last_updated ?? coerceWikiPageUpdated(path, undefined),
|
|
1726
|
+
compiled_truth: content,
|
|
1727
|
+
pinned: Boolean(row?.pinned),
|
|
1728
|
+
frontmatter,
|
|
1729
|
+
},
|
|
1730
|
+
});
|
|
1731
|
+
});
|
|
1732
|
+
const wikiPinSchema = z.object({
|
|
1733
|
+
slug: requiredString("Missing 'slug' in request body"),
|
|
1734
|
+
pinned: z.boolean({ error: "Missing 'pinned' in request body" }),
|
|
1735
|
+
});
|
|
1736
|
+
app.post("/api/wiki/page/pin", authMiddleware, async (req, res) => {
|
|
1737
|
+
const { slug, pinned } = parseRequest(wikiPinSchema, req.body);
|
|
1738
|
+
const path = assertValidPagePath(`pages/${slug}.md`);
|
|
1739
|
+
const db = getDb();
|
|
1740
|
+
db.prepare("UPDATE wiki_pages SET pinned = ? WHERE path = ?").run(pinned ? 1 : 0, path);
|
|
1741
|
+
res.json({ ok: true, pinned: Boolean(pinned) });
|
|
1742
|
+
});
|
|
1580
1743
|
app.delete("/api/wiki/page", async (req, res) => {
|
|
1581
1744
|
const path = assertValidPagePath(readPathParam(req));
|
|
1582
1745
|
const removed = await withWikiWrite(() => deletePage(path));
|
|
1583
1746
|
res.json({ ok: removed, path });
|
|
1584
1747
|
});
|
|
1585
|
-
app.post("/api/wiki/korg", authMiddleware, async (req, res) => {
|
|
1586
|
-
const body = parseRequest(korgRequestSchema, req.body ?? {});
|
|
1587
|
-
const result = await routeKorgMessage(body);
|
|
1588
|
-
res.json(result);
|
|
1589
|
-
});
|
|
1590
1748
|
app.get("/api/wiki/korg/sessions", authMiddleware, (_req, res) => {
|
|
1591
1749
|
res.json({ sessions: listKorgResearchSessions(getDb()) });
|
|
1592
1750
|
});
|
|
1751
|
+
const wikiIngestSchema = z.object({
|
|
1752
|
+
source_url: z.string().optional(),
|
|
1753
|
+
path: z.string().optional(),
|
|
1754
|
+
topic: z.string().optional(),
|
|
1755
|
+
});
|
|
1756
|
+
app.post("/api/wiki/ingest", authMiddleware, async (req, res) => {
|
|
1757
|
+
const { source_url, path: ingestPath, topic } = parseRequest(wikiIngestSchema, req.body ?? {});
|
|
1758
|
+
const source = source_url ?? ingestPath;
|
|
1759
|
+
if (!source) {
|
|
1760
|
+
throw new BadRequestError("Missing 'source_url' or 'path' in request body");
|
|
1761
|
+
}
|
|
1762
|
+
const type = source_url ? "url" : "text";
|
|
1763
|
+
await withWikiWrite(() => ingestSource(source, type, topic));
|
|
1764
|
+
res.json({ ok: true });
|
|
1765
|
+
});
|
|
1766
|
+
const wikiSearchSchema = z.object({
|
|
1767
|
+
q: z.string().optional(),
|
|
1768
|
+
type: z.string().optional(),
|
|
1769
|
+
});
|
|
1770
|
+
app.get("/api/wiki/search", authMiddleware, async (req, res) => {
|
|
1771
|
+
ensureWikiStructure();
|
|
1772
|
+
const q = normalizeOptionalQueryParam(req.query.q);
|
|
1773
|
+
const type = normalizeOptionalQueryParam(req.query.type);
|
|
1774
|
+
const db = getDb();
|
|
1775
|
+
let rows;
|
|
1776
|
+
try {
|
|
1777
|
+
const clauses = ["wiki_pages_fts MATCH ?"];
|
|
1778
|
+
const params = [q ? `${q}*` : "*"];
|
|
1779
|
+
if (type) {
|
|
1780
|
+
clauses.push("entity_type = ?");
|
|
1781
|
+
params.push(type);
|
|
1782
|
+
}
|
|
1783
|
+
rows = db.prepare(`
|
|
1784
|
+
SELECT path, title, entity_type, last_updated
|
|
1785
|
+
FROM wiki_pages_fts
|
|
1786
|
+
WHERE ${clauses.join(" AND ")}
|
|
1787
|
+
ORDER BY rank
|
|
1788
|
+
LIMIT 50
|
|
1789
|
+
`).all(...params);
|
|
1790
|
+
}
|
|
1791
|
+
catch {
|
|
1792
|
+
const clauses = [];
|
|
1793
|
+
const params = [];
|
|
1794
|
+
if (q) {
|
|
1795
|
+
clauses.push("title LIKE ? COLLATE NOCASE");
|
|
1796
|
+
params.push(`%${q}%`);
|
|
1797
|
+
}
|
|
1798
|
+
if (type) {
|
|
1799
|
+
clauses.push("entity_type = ?");
|
|
1800
|
+
params.push(type);
|
|
1801
|
+
}
|
|
1802
|
+
rows = db.prepare(`
|
|
1803
|
+
SELECT path, title, entity_type, last_updated
|
|
1804
|
+
FROM wiki_pages
|
|
1805
|
+
${clauses.length > 0 ? `WHERE ${clauses.join(" AND ")}` : ""}
|
|
1806
|
+
LIMIT 50
|
|
1807
|
+
`).all(...params);
|
|
1808
|
+
}
|
|
1809
|
+
const pages = rows.map((row) => ({
|
|
1810
|
+
slug: wikiPathToBrowserSlug(row.path),
|
|
1811
|
+
title: row.title,
|
|
1812
|
+
type: row.entity_type ?? "topics",
|
|
1813
|
+
last_updated: coerceWikiPageUpdated(row.path, row.last_updated ?? undefined),
|
|
1814
|
+
}));
|
|
1815
|
+
res.json({ pages });
|
|
1816
|
+
});
|
|
1593
1817
|
// ---------------------------------------------------------------------------
|
|
1594
1818
|
// Skills
|
|
1595
1819
|
// ---------------------------------------------------------------------------
|