redlinify 0.0.0-stage → 0.4.0
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 +21 -0
- package/README.md +341 -2
- package/dist/_shared/atomicFs.d.ts +10 -0
- package/dist/_shared/atomicFs.d.ts.map +1 -0
- package/dist/_shared/atomicFs.js +86 -0
- package/dist/_shared/atomicFs.js.map +1 -0
- package/dist/_shared/browser.d.ts +7 -0
- package/dist/_shared/browser.d.ts.map +1 -0
- package/dist/_shared/browser.js +7 -0
- package/dist/_shared/browser.js.map +1 -0
- package/dist/_shared/canonical.d.ts +8 -0
- package/dist/_shared/canonical.d.ts.map +1 -0
- package/dist/_shared/canonical.js +28 -0
- package/dist/_shared/canonical.js.map +1 -0
- package/dist/_shared/diff.d.ts +78 -0
- package/dist/_shared/diff.d.ts.map +1 -0
- package/dist/_shared/diff.js +350 -0
- package/dist/_shared/diff.js.map +1 -0
- package/dist/_shared/errors.d.ts +29 -0
- package/dist/_shared/errors.d.ts.map +1 -0
- package/dist/_shared/errors.js +46 -0
- package/dist/_shared/errors.js.map +1 -0
- package/dist/_shared/fileLock.d.ts +11 -0
- package/dist/_shared/fileLock.d.ts.map +1 -0
- package/dist/_shared/fileLock.js +114 -0
- package/dist/_shared/fileLock.js.map +1 -0
- package/dist/_shared/guards.d.ts +20 -0
- package/dist/_shared/guards.d.ts.map +1 -0
- package/dist/_shared/guards.js +395 -0
- package/dist/_shared/guards.js.map +1 -0
- package/dist/_shared/hash.d.ts +12 -0
- package/dist/_shared/hash.d.ts.map +1 -0
- package/dist/_shared/hash.js +80 -0
- package/dist/_shared/hash.js.map +1 -0
- package/dist/_shared/index.d.ts +9 -0
- package/dist/_shared/index.d.ts.map +1 -0
- package/dist/_shared/index.js +9 -0
- package/dist/_shared/index.js.map +1 -0
- package/dist/_shared/schemas.d.ts +727 -0
- package/dist/_shared/schemas.d.ts.map +1 -0
- package/dist/_shared/schemas.js +352 -0
- package/dist/_shared/schemas.js.map +1 -0
- package/dist/_shared/storage.d.ts +174 -0
- package/dist/_shared/storage.d.ts.map +1 -0
- package/dist/_shared/storage.js +801 -0
- package/dist/_shared/storage.js.map +1 -0
- package/dist/_shared/threadStatus.d.ts +5 -0
- package/dist/_shared/threadStatus.d.ts.map +1 -0
- package/dist/_shared/threadStatus.js +18 -0
- package/dist/_shared/threadStatus.js.map +1 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +269 -0
- package/dist/cli.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/mcp.d.ts +19 -0
- package/dist/mcp.d.ts.map +1 -0
- package/dist/mcp.js +654 -0
- package/dist/mcp.js.map +1 -0
- package/dist/preferences.d.ts +5 -0
- package/dist/preferences.d.ts.map +1 -0
- package/dist/preferences.js +56 -0
- package/dist/preferences.js.map +1 -0
- package/dist/uiDaemon.d.ts +47 -0
- package/dist/uiDaemon.d.ts.map +1 -0
- package/dist/uiDaemon.js +351 -0
- package/dist/uiDaemon.js.map +1 -0
- package/dist/updater.d.ts +35 -0
- package/dist/updater.d.ts.map +1 -0
- package/dist/updater.js +336 -0
- package/dist/updater.js.map +1 -0
- package/dist/web.d.ts +29 -0
- package/dist/web.d.ts.map +1 -0
- package/dist/web.js +648 -0
- package/dist/web.js.map +1 -0
- package/package.json +51 -4
- package/skill/SKILL.md +36 -0
- package/ui-dist/assets/elkjs-DuIp8wh-.js +26 -0
- package/ui-dist/assets/index-DhMWEByx.css +1 -0
- package/ui-dist/assets/index-Dy_FwyNG.js +99 -0
- package/ui-dist/assets/vendor-icons-IOjDvVjt.js +261 -0
- package/ui-dist/assets/vendor-react-5-35apmZ.js +49 -0
- package/ui-dist/assets/vendor-xyflow-BnuhLJ6X.css +1 -0
- package/ui-dist/assets/vendor-xyflow-cY2i6Lf0.js +23 -0
- package/ui-dist/index.html +29 -0
package/dist/web.js
ADDED
|
@@ -0,0 +1,648 @@
|
|
|
1
|
+
import { promises as fs } from "node:fs";
|
|
2
|
+
import http from "node:http";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import chokidar from "chokidar";
|
|
5
|
+
import { WebSocket, WebSocketServer } from "ws";
|
|
6
|
+
import { z, ZodError } from "zod";
|
|
7
|
+
import { ConcurrencyConflictError, DesignError, DesignViewSchema, DiagramTypeSchema, EntityRefSchema, LockViolationError, NotFoundError, ThreadKindSchema, ThreadTargetTypeSchema, UserPreferencesSchema, ValidationError, ViewDirectionSchema, ViewLayoutSchema, WorkspaceTabSchema, computeSemanticDiff, idSlugSchema, } from "./_shared/index.js";
|
|
8
|
+
import { readUserPreferences, updateUserPreferences } from "./preferences.js";
|
|
9
|
+
const MAX_BODY_BYTES = 5 * 1024 * 1024;
|
|
10
|
+
const SELF_WRITE_TTL_MS = 1500;
|
|
11
|
+
const MIME_TYPES = {
|
|
12
|
+
".html": "text/html; charset=utf-8",
|
|
13
|
+
".js": "text/javascript; charset=utf-8",
|
|
14
|
+
".mjs": "text/javascript; charset=utf-8",
|
|
15
|
+
".css": "text/css; charset=utf-8",
|
|
16
|
+
".json": "application/json; charset=utf-8",
|
|
17
|
+
".svg": "image/svg+xml",
|
|
18
|
+
".png": "image/png",
|
|
19
|
+
".ico": "image/x-icon",
|
|
20
|
+
".woff2": "font/woff2",
|
|
21
|
+
".woff": "font/woff",
|
|
22
|
+
".txt": "text/plain; charset=utf-8",
|
|
23
|
+
};
|
|
24
|
+
function sendBuffer(res, status, contentType, body) {
|
|
25
|
+
res.writeHead(status, {
|
|
26
|
+
"Content-Type": contentType,
|
|
27
|
+
"Content-Length": String(body.length),
|
|
28
|
+
"Cache-Control": "no-store",
|
|
29
|
+
});
|
|
30
|
+
res.end(body);
|
|
31
|
+
}
|
|
32
|
+
function sendJson(res, status, data) {
|
|
33
|
+
const buf = Buffer.from(JSON.stringify(data), "utf8");
|
|
34
|
+
sendBuffer(res, status, "application/json; charset=utf-8", buf);
|
|
35
|
+
}
|
|
36
|
+
function sendEmpty(res, status) {
|
|
37
|
+
res.writeHead(status, { "Content-Length": "0" });
|
|
38
|
+
res.end();
|
|
39
|
+
}
|
|
40
|
+
async function readJsonBody(req) {
|
|
41
|
+
const chunks = [];
|
|
42
|
+
let totalBytes = 0;
|
|
43
|
+
for await (const chunk of req) {
|
|
44
|
+
const buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
|
|
45
|
+
totalBytes += buf.length;
|
|
46
|
+
if (totalBytes > MAX_BODY_BYTES) {
|
|
47
|
+
throw new ValidationError("Request body exceeds 5MB limit");
|
|
48
|
+
}
|
|
49
|
+
chunks.push(buf);
|
|
50
|
+
}
|
|
51
|
+
const raw = Buffer.concat(chunks).toString("utf8").trim();
|
|
52
|
+
if (!raw) {
|
|
53
|
+
return {};
|
|
54
|
+
}
|
|
55
|
+
try {
|
|
56
|
+
return JSON.parse(raw);
|
|
57
|
+
}
|
|
58
|
+
catch (err) {
|
|
59
|
+
throw new ValidationError(`Invalid JSON request body: ${err.message}`);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
function slugifyTitle(title) {
|
|
63
|
+
const slug = title
|
|
64
|
+
.trim()
|
|
65
|
+
.toLowerCase()
|
|
66
|
+
.replace(/[^a-z0-9]+/g, "_")
|
|
67
|
+
.replace(/^_+|_+$/g, "")
|
|
68
|
+
.slice(0, 64);
|
|
69
|
+
return slug || `view_${Date.now().toString(36)}`;
|
|
70
|
+
}
|
|
71
|
+
function handleApiError(res, err) {
|
|
72
|
+
if (err instanceof ZodError) {
|
|
73
|
+
sendJson(res, 400, {
|
|
74
|
+
error: "VALIDATION_ERROR",
|
|
75
|
+
message: err.message,
|
|
76
|
+
details: err.issues,
|
|
77
|
+
});
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
if (err instanceof ValidationError) {
|
|
81
|
+
sendJson(res, 400, {
|
|
82
|
+
error: err.code,
|
|
83
|
+
message: err.message,
|
|
84
|
+
details: err.details,
|
|
85
|
+
});
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
if (err instanceof NotFoundError) {
|
|
89
|
+
sendJson(res, 404, {
|
|
90
|
+
error: err.code,
|
|
91
|
+
message: err.message,
|
|
92
|
+
});
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
if (err instanceof ConcurrencyConflictError ||
|
|
96
|
+
err instanceof LockViolationError) {
|
|
97
|
+
sendJson(res, 409, {
|
|
98
|
+
error: err.code,
|
|
99
|
+
message: err.message,
|
|
100
|
+
details: err.details,
|
|
101
|
+
});
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
if (err instanceof DesignError) {
|
|
105
|
+
sendJson(res, 400, {
|
|
106
|
+
error: err.code,
|
|
107
|
+
message: err.message,
|
|
108
|
+
details: err.details,
|
|
109
|
+
});
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
sendJson(res, 500, {
|
|
113
|
+
error: "INTERNAL_ERROR",
|
|
114
|
+
message: err instanceof Error ? err.message : String(err),
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
async function serveStaticFile(res, uiDistDir, reqPath) {
|
|
118
|
+
const decoded = decodeURIComponent(reqPath);
|
|
119
|
+
const relPath = decoded === "/" ? "index.html" : decoded.replace(/^\/+/, "");
|
|
120
|
+
const distRoot = path.resolve(uiDistDir);
|
|
121
|
+
const candidate = path.resolve(distRoot, relPath);
|
|
122
|
+
if (candidate !== distRoot && !candidate.startsWith(`${distRoot}${path.sep}`)) {
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
let filePath = candidate;
|
|
126
|
+
let stat = await fs.stat(filePath).catch(() => null);
|
|
127
|
+
if (!stat || !stat.isFile()) {
|
|
128
|
+
if (path.extname(relPath)) {
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
filePath = path.join(distRoot, "index.html");
|
|
132
|
+
stat = await fs.stat(filePath).catch(() => null);
|
|
133
|
+
if (!stat || !stat.isFile()) {
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
const ext = path.extname(filePath).toLowerCase();
|
|
138
|
+
const mime = MIME_TYPES[ext] ?? "application/octet-stream";
|
|
139
|
+
const content = await fs.readFile(filePath);
|
|
140
|
+
res.writeHead(200, {
|
|
141
|
+
"Content-Type": mime,
|
|
142
|
+
"Content-Length": String(content.length),
|
|
143
|
+
"Cache-Control": ext === ".html" ? "no-cache" : "public, max-age=3600",
|
|
144
|
+
});
|
|
145
|
+
res.end(content);
|
|
146
|
+
return true;
|
|
147
|
+
}
|
|
148
|
+
export async function createWebServer(options) {
|
|
149
|
+
const { storage, uiDistDir } = options;
|
|
150
|
+
const host = options.host ?? "127.0.0.1";
|
|
151
|
+
const port = options.port ?? 4173;
|
|
152
|
+
const recentSelfWrites = new Map();
|
|
153
|
+
function markSelfWrite(...relPaths) {
|
|
154
|
+
const expiresAt = Date.now() + SELF_WRITE_TTL_MS;
|
|
155
|
+
for (const rel of relPaths) {
|
|
156
|
+
recentSelfWrites.set(rel.replace(/\\/g, "/"), expiresAt);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
function isSelfWrite(relPath) {
|
|
160
|
+
const normalized = relPath.replace(/\\/g, "/");
|
|
161
|
+
const now = Date.now();
|
|
162
|
+
for (const [key, exp] of recentSelfWrites.entries()) {
|
|
163
|
+
if (exp < now)
|
|
164
|
+
recentSelfWrites.delete(key);
|
|
165
|
+
}
|
|
166
|
+
const exp = recentSelfWrites.get(normalized);
|
|
167
|
+
return exp !== undefined && exp >= now;
|
|
168
|
+
}
|
|
169
|
+
const server = http.createServer(async (req, res) => {
|
|
170
|
+
try {
|
|
171
|
+
const method = req.method ?? "GET";
|
|
172
|
+
const parsedUrl = new URL(req.url ?? "/", `http://${req.headers.host ?? `${host}:${port}`}`);
|
|
173
|
+
const pathname = parsedUrl.pathname;
|
|
174
|
+
if (pathname.startsWith("/api/")) {
|
|
175
|
+
if (method === "GET" && pathname === "/api/health") {
|
|
176
|
+
const status = await storage.getWorkspaceStatus();
|
|
177
|
+
sendJson(res, 200, {
|
|
178
|
+
ok: true,
|
|
179
|
+
project_name: status.project_name,
|
|
180
|
+
initialized: status.initialized,
|
|
181
|
+
root_dir: path.resolve(storage.rootDir),
|
|
182
|
+
pid: process.pid,
|
|
183
|
+
});
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
if (method === "GET" && pathname === "/api/workspace") {
|
|
187
|
+
const status = await storage.getWorkspaceStatus();
|
|
188
|
+
const workspace = status.initialized
|
|
189
|
+
? await storage.readWorkspace()
|
|
190
|
+
: {
|
|
191
|
+
version: 1,
|
|
192
|
+
project_name: status.project_name,
|
|
193
|
+
tabs: [],
|
|
194
|
+
view_requests: [],
|
|
195
|
+
};
|
|
196
|
+
const pending_feedback = status.initialized
|
|
197
|
+
? await storage.getPendingFeedback()
|
|
198
|
+
: {
|
|
199
|
+
view_hashes: {},
|
|
200
|
+
view_diffs: [],
|
|
201
|
+
pending_threads: [],
|
|
202
|
+
view_requests: [],
|
|
203
|
+
};
|
|
204
|
+
sendJson(res, 200, {
|
|
205
|
+
workspace,
|
|
206
|
+
status,
|
|
207
|
+
pending_feedback,
|
|
208
|
+
root_dir: path.resolve(storage.rootDir),
|
|
209
|
+
});
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
if (method === "PUT" && pathname === "/api/workspace/tabs") {
|
|
213
|
+
const body = z
|
|
214
|
+
.object({
|
|
215
|
+
tabs: z.array(WorkspaceTabSchema),
|
|
216
|
+
})
|
|
217
|
+
.parse(await readJsonBody(req));
|
|
218
|
+
const current = await storage.readWorkspace();
|
|
219
|
+
const normalized = body.tabs.map((tab, idx) => ({
|
|
220
|
+
...tab,
|
|
221
|
+
order: idx,
|
|
222
|
+
}));
|
|
223
|
+
markSelfWrite("workspace.json");
|
|
224
|
+
const workspace = await storage.writeWorkspace({
|
|
225
|
+
...current,
|
|
226
|
+
tabs: normalized,
|
|
227
|
+
});
|
|
228
|
+
sendJson(res, 200, { workspace });
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
if (method === "POST" && pathname === "/api/workspace/view-requests") {
|
|
232
|
+
const body = z
|
|
233
|
+
.object({
|
|
234
|
+
id: idSlugSchema("view_request.id").optional(),
|
|
235
|
+
title: z.string().trim().min(1),
|
|
236
|
+
prompt: z.string().trim().min(1),
|
|
237
|
+
})
|
|
238
|
+
.parse(await readJsonBody(req));
|
|
239
|
+
markSelfWrite("workspace.json");
|
|
240
|
+
const request = await storage.addViewRequest(body);
|
|
241
|
+
sendJson(res, 201, { request });
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
const viewReqMatch = pathname.match(/^\/api\/workspace\/view-requests\/([^/]+)$/);
|
|
245
|
+
if (method === "DELETE" && viewReqMatch) {
|
|
246
|
+
const reqId = decodeURIComponent(viewReqMatch[1]);
|
|
247
|
+
markSelfWrite("workspace.json");
|
|
248
|
+
await storage.deleteViewRequest(reqId);
|
|
249
|
+
sendJson(res, 200, { deleted: true, id: reqId });
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
if (method === "GET" && pathname === "/api/views") {
|
|
253
|
+
const status = await storage.getWorkspaceStatus();
|
|
254
|
+
const allIds = status.initialized ? await storage.listViewIds() : [];
|
|
255
|
+
const tabOrder = new Map(status.tabs.map((tab, idx) => [tab.view_id, idx]));
|
|
256
|
+
const orderedIds = [...allIds].sort((a, b) => {
|
|
257
|
+
const oa = tabOrder.get(a);
|
|
258
|
+
const ob = tabOrder.get(b);
|
|
259
|
+
if (oa !== undefined && ob !== undefined)
|
|
260
|
+
return oa - ob;
|
|
261
|
+
if (oa !== undefined)
|
|
262
|
+
return -1;
|
|
263
|
+
if (ob !== undefined)
|
|
264
|
+
return 1;
|
|
265
|
+
return a.localeCompare(b);
|
|
266
|
+
});
|
|
267
|
+
const views = [];
|
|
268
|
+
for (const id of orderedIds) {
|
|
269
|
+
try {
|
|
270
|
+
const { view } = await storage.readView(id);
|
|
271
|
+
views.push(view);
|
|
272
|
+
}
|
|
273
|
+
catch {
|
|
274
|
+
// Skip unreadable or concurrently deleted view cleanly
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
sendJson(res, 200, { views });
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
if (method === "POST" && pathname === "/api/views") {
|
|
281
|
+
const body = z
|
|
282
|
+
.object({
|
|
283
|
+
id: idSlugSchema("view.id").optional(),
|
|
284
|
+
title: z.string().trim().min(1),
|
|
285
|
+
diagram_type: DiagramTypeSchema.default("general"),
|
|
286
|
+
direction: ViewDirectionSchema.default("TB"),
|
|
287
|
+
})
|
|
288
|
+
.parse(await readJsonBody(req));
|
|
289
|
+
const viewId = body.id ?? slugifyTitle(body.title);
|
|
290
|
+
idSlugSchema("view.id").parse(viewId);
|
|
291
|
+
const existingIds = await storage.listViewIds();
|
|
292
|
+
if (existingIds.includes(viewId)) {
|
|
293
|
+
throw new ConcurrencyConflictError(viewId, null, "", `View '${viewId}' already exists`);
|
|
294
|
+
}
|
|
295
|
+
markSelfWrite("workspace.json", `views/${viewId}.json`);
|
|
296
|
+
const created = await storage.upsertView({
|
|
297
|
+
view: {
|
|
298
|
+
id: viewId,
|
|
299
|
+
title: body.title,
|
|
300
|
+
diagram_type: body.diagram_type,
|
|
301
|
+
direction: body.direction,
|
|
302
|
+
created_by: "human",
|
|
303
|
+
nodes: [],
|
|
304
|
+
edges: [],
|
|
305
|
+
},
|
|
306
|
+
actor: "human",
|
|
307
|
+
});
|
|
308
|
+
sendJson(res, 201, created);
|
|
309
|
+
return;
|
|
310
|
+
}
|
|
311
|
+
const viewMatch = pathname.match(/^\/api\/views\/([^/]+)$/);
|
|
312
|
+
if (viewMatch) {
|
|
313
|
+
const viewId = idSlugSchema("view_id").parse(decodeURIComponent(viewMatch[1]));
|
|
314
|
+
if (method === "DELETE") {
|
|
315
|
+
markSelfWrite("workspace.json", `views/${viewId}.json`);
|
|
316
|
+
const deleted = await storage.deleteView(viewId);
|
|
317
|
+
sendJson(res, 200, deleted);
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
if (method === "GET") {
|
|
321
|
+
const [{ view, view_hash }, layout, threads] = await Promise.all([
|
|
322
|
+
storage.readView(viewId),
|
|
323
|
+
storage.readLayout(viewId),
|
|
324
|
+
storage.listThreads({ view_id: viewId }),
|
|
325
|
+
]);
|
|
326
|
+
const baseline = await storage.resolveCheckpointBaseline(viewId, view);
|
|
327
|
+
const diff = computeSemanticDiff(baseline, view);
|
|
328
|
+
sendJson(res, 200, {
|
|
329
|
+
view,
|
|
330
|
+
view_hash,
|
|
331
|
+
layout,
|
|
332
|
+
diff,
|
|
333
|
+
baseline,
|
|
334
|
+
threads,
|
|
335
|
+
});
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
if (method === "PUT") {
|
|
339
|
+
const rawBody = (await readJsonBody(req));
|
|
340
|
+
const body = z
|
|
341
|
+
.object({
|
|
342
|
+
view: DesignViewSchema,
|
|
343
|
+
base_hash: z.string().min(1).optional(),
|
|
344
|
+
})
|
|
345
|
+
.parse(rawBody);
|
|
346
|
+
if (body.view.id !== viewId) {
|
|
347
|
+
throw new ValidationError(`Path view_id '${viewId}' does not match body.view.id '${body.view.id}'`);
|
|
348
|
+
}
|
|
349
|
+
const headerHash = typeof req.headers["if-match"] === "string"
|
|
350
|
+
? req.headers["if-match"]
|
|
351
|
+
: undefined;
|
|
352
|
+
const baseHash = body.base_hash ?? headerHash;
|
|
353
|
+
markSelfWrite("workspace.json", `views/${viewId}.json`);
|
|
354
|
+
const saved = await storage.upsertView({
|
|
355
|
+
view: body.view,
|
|
356
|
+
base_hash: baseHash,
|
|
357
|
+
actor: "human",
|
|
358
|
+
});
|
|
359
|
+
const baseline = await storage.resolveCheckpointBaseline(viewId, saved.view);
|
|
360
|
+
const diff = computeSemanticDiff(baseline, saved.view);
|
|
361
|
+
sendJson(res, 200, {
|
|
362
|
+
view: saved.view,
|
|
363
|
+
view_hash: saved.view_hash,
|
|
364
|
+
diff,
|
|
365
|
+
baseline,
|
|
366
|
+
});
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
const layoutMatch = pathname.match(/^\/api\/layout\/([^/]+)$/);
|
|
371
|
+
if (layoutMatch) {
|
|
372
|
+
const viewId = idSlugSchema("view_id").parse(decodeURIComponent(layoutMatch[1]));
|
|
373
|
+
if (method === "GET") {
|
|
374
|
+
sendJson(res, 200, { layout: await storage.readLayout(viewId) });
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
if (method === "PUT") {
|
|
378
|
+
const layout = ViewLayoutSchema.parse(await readJsonBody(req));
|
|
379
|
+
const saved = await storage.writeLayout(viewId, layout);
|
|
380
|
+
sendJson(res, 200, { layout: saved });
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
if (method === "DELETE") {
|
|
384
|
+
await storage.clearLayout(viewId);
|
|
385
|
+
sendJson(res, 200, { cleared: true, view_id: viewId });
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
if (method === "GET" && pathname === "/api/threads") {
|
|
390
|
+
const statusParam = parsedUrl.searchParams.get("status");
|
|
391
|
+
const targetTypeParam = parsedUrl.searchParams.get("target_type");
|
|
392
|
+
const threads = await storage.listThreads({
|
|
393
|
+
view_id: parsedUrl.searchParams.get("view_id") ?? undefined,
|
|
394
|
+
target_type: targetTypeParam
|
|
395
|
+
? ThreadTargetTypeSchema.parse(targetTypeParam)
|
|
396
|
+
: undefined,
|
|
397
|
+
target_id: parsedUrl.searchParams.get("target_id") ?? undefined,
|
|
398
|
+
entity_ref: parsedUrl.searchParams.get("entity_ref") ?? undefined,
|
|
399
|
+
status: statusParam === "open" || statusParam === "resolved"
|
|
400
|
+
? statusParam
|
|
401
|
+
: undefined,
|
|
402
|
+
query: parsedUrl.searchParams.get("query") ?? undefined,
|
|
403
|
+
});
|
|
404
|
+
sendJson(res, 200, { threads });
|
|
405
|
+
return;
|
|
406
|
+
}
|
|
407
|
+
if (method === "POST" && pathname === "/api/threads") {
|
|
408
|
+
const body = z
|
|
409
|
+
.object({
|
|
410
|
+
target_type: ThreadTargetTypeSchema,
|
|
411
|
+
view_id: idSlugSchema("view_id").optional(),
|
|
412
|
+
target_id: idSlugSchema("target_id").optional(),
|
|
413
|
+
entity_ref: EntityRefSchema.optional(),
|
|
414
|
+
kind: ThreadKindSchema,
|
|
415
|
+
body: z.string().trim().min(1),
|
|
416
|
+
})
|
|
417
|
+
.parse(await readJsonBody(req));
|
|
418
|
+
const created = await storage.postThreadMessage({
|
|
419
|
+
...body,
|
|
420
|
+
author: "human",
|
|
421
|
+
});
|
|
422
|
+
markSelfWrite(`threads/${created.id}.json`);
|
|
423
|
+
sendJson(res, 201, { thread: created });
|
|
424
|
+
return;
|
|
425
|
+
}
|
|
426
|
+
const threadMsgMatch = pathname.match(/^\/api\/threads\/([^/]+)\/messages$/);
|
|
427
|
+
if (method === "POST" && threadMsgMatch) {
|
|
428
|
+
const threadId = idSlugSchema("thread_id").parse(decodeURIComponent(threadMsgMatch[1]));
|
|
429
|
+
const body = z
|
|
430
|
+
.object({
|
|
431
|
+
body: z.string().trim().min(1),
|
|
432
|
+
resolve: z.boolean().optional(),
|
|
433
|
+
reopen: z.boolean().optional(),
|
|
434
|
+
resolution_summary: z.string().optional(),
|
|
435
|
+
})
|
|
436
|
+
.parse(await readJsonBody(req));
|
|
437
|
+
markSelfWrite(`threads/${threadId}.json`);
|
|
438
|
+
const thread = await storage.postThreadMessage({
|
|
439
|
+
thread_id: threadId,
|
|
440
|
+
author: "human",
|
|
441
|
+
...body,
|
|
442
|
+
});
|
|
443
|
+
sendJson(res, 200, { thread });
|
|
444
|
+
return;
|
|
445
|
+
}
|
|
446
|
+
if (method === "GET" && pathname === "/api/pending-feedback") {
|
|
447
|
+
const viewId = parsedUrl.searchParams.get("view_id") ?? undefined;
|
|
448
|
+
sendJson(res, 200, await storage.getPendingFeedback(viewId));
|
|
449
|
+
return;
|
|
450
|
+
}
|
|
451
|
+
const crossLinkMatch = pathname.match(/^\/api\/cross-links\/([^/]+)$/);
|
|
452
|
+
if (method === "GET" && crossLinkMatch) {
|
|
453
|
+
const entityRef = EntityRefSchema.parse(decodeURIComponent(crossLinkMatch[1]));
|
|
454
|
+
const viewIds = await storage.listViewIds();
|
|
455
|
+
const occurrences = [];
|
|
456
|
+
for (const viewId of viewIds) {
|
|
457
|
+
const { view } = await storage.readView(viewId);
|
|
458
|
+
for (const node of view.nodes) {
|
|
459
|
+
if (node.entity_ref === entityRef) {
|
|
460
|
+
occurrences.push({
|
|
461
|
+
view_id: view.id,
|
|
462
|
+
view_title: view.title,
|
|
463
|
+
node_id: node.id,
|
|
464
|
+
node_title: node.title,
|
|
465
|
+
kind: node.kind,
|
|
466
|
+
status: node.status,
|
|
467
|
+
});
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
sendJson(res, 200, { entity_ref: entityRef, occurrences });
|
|
472
|
+
return;
|
|
473
|
+
}
|
|
474
|
+
if (method === "GET" && pathname === "/api/preferences") {
|
|
475
|
+
const preferences = await readUserPreferences(options.preferencesPath);
|
|
476
|
+
sendJson(res, 200, { preferences });
|
|
477
|
+
return;
|
|
478
|
+
}
|
|
479
|
+
if (method === "PUT" && pathname === "/api/preferences") {
|
|
480
|
+
const body = UserPreferencesSchema.parse(await readJsonBody(req));
|
|
481
|
+
const preferences = await updateUserPreferences(body, options.preferencesPath);
|
|
482
|
+
sendJson(res, 200, { preferences });
|
|
483
|
+
return;
|
|
484
|
+
}
|
|
485
|
+
sendJson(res, 404, {
|
|
486
|
+
error: "NOT_FOUND",
|
|
487
|
+
message: `Unknown API route ${method} ${pathname}`,
|
|
488
|
+
});
|
|
489
|
+
return;
|
|
490
|
+
}
|
|
491
|
+
if (uiDistDir && (method === "GET" || method === "HEAD")) {
|
|
492
|
+
const served = await serveStaticFile(res, uiDistDir, pathname);
|
|
493
|
+
if (served)
|
|
494
|
+
return;
|
|
495
|
+
}
|
|
496
|
+
if (pathname === "/favicon.ico") {
|
|
497
|
+
sendEmpty(res, 204);
|
|
498
|
+
return;
|
|
499
|
+
}
|
|
500
|
+
sendJson(res, 404, {
|
|
501
|
+
error: "NOT_FOUND",
|
|
502
|
+
message: `Resource not found: ${pathname}`,
|
|
503
|
+
});
|
|
504
|
+
}
|
|
505
|
+
catch (err) {
|
|
506
|
+
handleApiError(res, err);
|
|
507
|
+
}
|
|
508
|
+
});
|
|
509
|
+
let wss;
|
|
510
|
+
function broadcast(event) {
|
|
511
|
+
if (!wss)
|
|
512
|
+
return;
|
|
513
|
+
const payload = JSON.stringify(event);
|
|
514
|
+
for (const client of wss.clients) {
|
|
515
|
+
if (client.readyState === WebSocket.OPEN) {
|
|
516
|
+
client.send(payload);
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
let watcher;
|
|
521
|
+
let flushTimer;
|
|
522
|
+
const pendingPaths = new Set();
|
|
523
|
+
function flushWatcherEvents() {
|
|
524
|
+
flushTimer = undefined;
|
|
525
|
+
if (pendingPaths.size === 0)
|
|
526
|
+
return;
|
|
527
|
+
const batch = [...pendingPaths];
|
|
528
|
+
pendingPaths.clear();
|
|
529
|
+
let workspaceDirty = false;
|
|
530
|
+
let threadsDirty = false;
|
|
531
|
+
const viewIds = new Set();
|
|
532
|
+
const origins = [];
|
|
533
|
+
for (const rel of batch) {
|
|
534
|
+
const origin = isSelfWrite(rel) ? "ui" : "external";
|
|
535
|
+
if (rel === "workspace.json") {
|
|
536
|
+
workspaceDirty = true;
|
|
537
|
+
origins.push(origin);
|
|
538
|
+
}
|
|
539
|
+
else if (rel.startsWith("views/") && rel.endsWith(".json")) {
|
|
540
|
+
const viewId = rel.slice("views/".length, -".json".length);
|
|
541
|
+
if (idSlugSchema("view_id").safeParse(viewId).success) {
|
|
542
|
+
viewIds.add(viewId);
|
|
543
|
+
origins.push(origin);
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
else if (rel.startsWith("threads/") && rel.endsWith(".json")) {
|
|
547
|
+
threadsDirty = true;
|
|
548
|
+
origins.push(origin);
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
const batchOrigin = origins.includes("external") ? "external" : "ui";
|
|
552
|
+
if (workspaceDirty) {
|
|
553
|
+
broadcast({ type: "workspace_updated", origin: batchOrigin });
|
|
554
|
+
}
|
|
555
|
+
for (const view_id of viewIds) {
|
|
556
|
+
broadcast({ type: "view_updated", view_id, origin: batchOrigin });
|
|
557
|
+
}
|
|
558
|
+
if (threadsDirty) {
|
|
559
|
+
broadcast({ type: "threads_updated", origin: batchOrigin });
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
await new Promise((resolve, reject) => {
|
|
563
|
+
server.once("error", reject);
|
|
564
|
+
server.listen(port, host, () => {
|
|
565
|
+
server.off("error", reject);
|
|
566
|
+
resolve();
|
|
567
|
+
});
|
|
568
|
+
});
|
|
569
|
+
wss = new WebSocketServer({ server, path: "/ws" });
|
|
570
|
+
if (options.watch !== false) {
|
|
571
|
+
await fs.mkdir(storage.designDir, { recursive: true });
|
|
572
|
+
watcher = chokidar.watch(storage.designDir, {
|
|
573
|
+
ignoreInitial: true,
|
|
574
|
+
ignored: (watchedPath) => {
|
|
575
|
+
const rel = path
|
|
576
|
+
.relative(storage.designDir, watchedPath)
|
|
577
|
+
.replace(/\\/g, "/");
|
|
578
|
+
if (!rel || rel === ".")
|
|
579
|
+
return false;
|
|
580
|
+
const base = path.basename(rel);
|
|
581
|
+
if (base.startsWith(".tmp-") ||
|
|
582
|
+
base === ".lock" ||
|
|
583
|
+
base === ".gitignore" ||
|
|
584
|
+
rel === ".layout" ||
|
|
585
|
+
rel.startsWith(".layout/") ||
|
|
586
|
+
rel === ".checkpoint" ||
|
|
587
|
+
rel.startsWith(".checkpoint/")) {
|
|
588
|
+
return true;
|
|
589
|
+
}
|
|
590
|
+
return false;
|
|
591
|
+
},
|
|
592
|
+
});
|
|
593
|
+
const onFsChange = (changedPath) => {
|
|
594
|
+
const rel = path
|
|
595
|
+
.relative(storage.designDir, changedPath)
|
|
596
|
+
.replace(/\\/g, "/");
|
|
597
|
+
if (!rel.endsWith(".json"))
|
|
598
|
+
return;
|
|
599
|
+
pendingPaths.add(rel);
|
|
600
|
+
if (flushTimer)
|
|
601
|
+
clearTimeout(flushTimer);
|
|
602
|
+
flushTimer = setTimeout(flushWatcherEvents, 60);
|
|
603
|
+
};
|
|
604
|
+
watcher.on("add", onFsChange);
|
|
605
|
+
watcher.on("change", onFsChange);
|
|
606
|
+
watcher.on("unlink", onFsChange);
|
|
607
|
+
}
|
|
608
|
+
const addr = server.address();
|
|
609
|
+
const actualPort = addr && typeof addr === "object" ? addr.port : port;
|
|
610
|
+
return {
|
|
611
|
+
host,
|
|
612
|
+
port: actualPort,
|
|
613
|
+
url: `http://${host}:${actualPort}`,
|
|
614
|
+
close: async () => {
|
|
615
|
+
if (flushTimer)
|
|
616
|
+
clearTimeout(flushTimer);
|
|
617
|
+
if (watcher) {
|
|
618
|
+
await watcher.close();
|
|
619
|
+
}
|
|
620
|
+
if (wss) {
|
|
621
|
+
for (const client of wss.clients) {
|
|
622
|
+
client.terminate();
|
|
623
|
+
}
|
|
624
|
+
await new Promise((resolve, reject) => {
|
|
625
|
+
wss.close(() => {
|
|
626
|
+
server.close((err) => {
|
|
627
|
+
if (err)
|
|
628
|
+
reject(err);
|
|
629
|
+
else
|
|
630
|
+
resolve();
|
|
631
|
+
});
|
|
632
|
+
});
|
|
633
|
+
});
|
|
634
|
+
}
|
|
635
|
+
else {
|
|
636
|
+
await new Promise((resolve, reject) => {
|
|
637
|
+
server.close((err) => {
|
|
638
|
+
if (err)
|
|
639
|
+
reject(err);
|
|
640
|
+
else
|
|
641
|
+
resolve();
|
|
642
|
+
});
|
|
643
|
+
});
|
|
644
|
+
}
|
|
645
|
+
},
|
|
646
|
+
};
|
|
647
|
+
}
|
|
648
|
+
//# sourceMappingURL=web.js.map
|