-
Notifications
You must be signed in to change notification settings - Fork 401
Expand file tree
/
Copy pathserverAnnotate.ts
More file actions
140 lines (125 loc) · 4.63 KB
/
serverAnnotate.ts
File metadata and controls
140 lines (125 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { createServer } from "node:http";
import { dirname, resolve as resolvePath } from "node:path";
import { contentHash, deleteDraft } from "../generated/draft.js";
import { saveConfig, detectGitUser, getServerConfig } from "../generated/config.js";
import {
handleDraftRequest,
handleFavicon,
handleImageRequest,
handleUploadRequest,
} from "./handlers.js";
import { html, json, parseBody, requestUrl } from "./helpers.js";
import { listenOnPort } from "./network.js";
import { getRepoInfo } from "./project.js";
import { handleDocRequest, handleFileBrowserRequest } from "./reference.js";
import { createExternalAnnotationHandler } from "./external-annotations.js";
export interface AnnotateServerResult {
port: number;
portSource: "env" | "remote-default" | "random";
url: string;
waitForDecision: () => Promise<{ feedback: string; annotations: unknown[] }>;
stop: () => void;
}
export async function startAnnotateServer(options: {
markdown: string;
filePath: string;
htmlContent: string;
origin?: string;
mode?: string;
folderPath?: string;
sharingEnabled?: boolean;
shareBaseUrl?: string;
pasteApiUrl?: string;
}): Promise<AnnotateServerResult> {
const gitUser = detectGitUser();
const sharingEnabled =
options.sharingEnabled ?? process.env.PLANNOTATOR_SHARE !== "disabled";
const shareBaseUrl =
(options.shareBaseUrl ?? process.env.PLANNOTATOR_SHARE_URL) || undefined;
const pasteApiUrl =
(options.pasteApiUrl ?? process.env.PLANNOTATOR_PASTE_URL) || undefined;
let resolveDecision!: (result: {
feedback: string;
annotations: unknown[];
}) => void;
const decisionPromise = new Promise<{
feedback: string;
annotations: unknown[];
}>((r) => {
resolveDecision = r;
});
// Draft key for annotation persistence
const draftKey = contentHash(options.markdown);
// Detect repo info (cached for this session)
const repoInfo = getRepoInfo();
const externalAnnotations = createExternalAnnotationHandler("plan");
const server = createServer(async (req, res) => {
const url = requestUrl(req);
if (await externalAnnotations.handle(req, res, url)) return;
if (url.pathname === "/api/plan" && req.method === "GET") {
json(res, {
plan: options.markdown,
origin: options.origin ?? "pi",
mode: options.mode || "annotate",
filePath: options.filePath,
sharingEnabled,
shareBaseUrl,
pasteApiUrl,
repoInfo,
projectRoot: options.folderPath || process.cwd(),
serverConfig: getServerConfig(gitUser),
});
} else if (url.pathname === "/api/config" && req.method === "POST") {
try {
const body = (await parseBody(req)) as { displayName?: string; diffOptions?: Record<string, unknown>; conventionalComments?: boolean };
const toSave: Record<string, unknown> = {};
if (body.displayName !== undefined) toSave.displayName = body.displayName;
if (body.diffOptions !== undefined) toSave.diffOptions = body.diffOptions;
if (body.conventionalComments !== undefined) toSave.conventionalComments = body.conventionalComments;
if (Object.keys(toSave).length > 0) saveConfig(toSave as Parameters<typeof saveConfig>[0]);
json(res, { ok: true });
} catch {
json(res, { error: "Invalid request" }, 400);
}
} else if (url.pathname === "/api/image") {
handleImageRequest(res, url);
} else if (url.pathname === "/api/upload" && req.method === "POST") {
await handleUploadRequest(req, res);
} else if (url.pathname === "/api/draft") {
await handleDraftRequest(req, res, draftKey);
} else if (url.pathname === "/api/doc" && req.method === "GET") {
// Inject source file's directory as base for relative path resolution
if (!url.searchParams.has("base") && options.filePath) {
url.searchParams.set("base", dirname(resolvePath(options.filePath)));
}
handleDocRequest(res, url);
} else if (url.pathname === "/api/reference/files" && req.method === "GET") {
handleFileBrowserRequest(res, url);
} else if (url.pathname === "/favicon.svg") {
handleFavicon(res);
} else if (url.pathname === "/api/feedback" && req.method === "POST") {
try {
const body = await parseBody(req);
deleteDraft(draftKey);
resolveDecision({
feedback: (body.feedback as string) || "",
annotations: (body.annotations as unknown[]) || [],
});
json(res, { ok: true });
} catch (err) {
const message = err instanceof Error ? err.message : "Failed to process feedback";
json(res, { error: message }, 500);
}
} else {
html(res, options.htmlContent);
}
});
const { port, portSource } = await listenOnPort(server);
return {
port,
portSource,
url: `http://localhost:${port}`,
waitForDecision: () => decisionPromise,
stop: () => server.close(),
};
}