@demon-utils/playwright 0.1.3 → 0.1.6

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/src/review.ts ADDED
@@ -0,0 +1,260 @@
1
+ export type SpawnFn = (
2
+ cmd: string[],
3
+ ) => { exitCode: Promise<number>; stdout: ReadableStream<Uint8Array> };
4
+
5
+ export interface InvokeClaudeOptions {
6
+ agent?: string;
7
+ spawn?: SpawnFn;
8
+ }
9
+
10
+ const GIT_DIFF_MAX_CHARS = 50_000;
11
+
12
+ export interface BuildReviewPromptOptions {
13
+ filenames: string[];
14
+ stepsMap: Record<string, Array<{ text: string; timestampSeconds: number }>>;
15
+ gitDiff?: string;
16
+ guidelines?: string[];
17
+ }
18
+
19
+ export function buildReviewPrompt(options: BuildReviewPromptOptions): string {
20
+ const { filenames, stepsMap, gitDiff, guidelines } = options;
21
+
22
+ const demoEntries = filenames.map((f) => {
23
+ const steps = stepsMap[f] ?? [];
24
+ const stepLines = steps
25
+ .map((s) => `- [${s.timestampSeconds}s] ${s.text}`)
26
+ .join("\n");
27
+ return `Video: ${f}\nRecorded steps:\n${stepLines || "(no steps recorded)"}`;
28
+ });
29
+
30
+ const sections: string[] = [];
31
+
32
+ if (guidelines && guidelines.length > 0) {
33
+ sections.push(`## Coding Guidelines\n\n${guidelines.join("\n\n")}`);
34
+ }
35
+
36
+ if (gitDiff) {
37
+ let diff = gitDiff;
38
+ if (diff.length > GIT_DIFF_MAX_CHARS) {
39
+ diff = diff.slice(0, GIT_DIFF_MAX_CHARS) + "\n\n... (diff truncated at 50k characters)";
40
+ }
41
+ sections.push(`## Git Diff\n\n\`\`\`diff\n${diff}\n\`\`\``);
42
+ }
43
+
44
+ sections.push(`## Demo Recordings\n\n${demoEntries.join("\n\n")}`);
45
+
46
+ return `You are a code reviewer. You are given a git diff, coding guidelines, and demo recordings that show the feature in action.
47
+
48
+ ${sections.join("\n\n")}
49
+
50
+ ## Task
51
+
52
+ Review the code changes and demo recordings. Generate a JSON object matching this exact schema:
53
+
54
+ {
55
+ "demos": [
56
+ {
57
+ "file": "<filename>",
58
+ "summary": "<a meaningful sentence describing what this demo showcases based on the steps>"
59
+ }
60
+ ],
61
+ "review": {
62
+ "summary": "<2-3 sentence overview of the changes>",
63
+ "highlights": ["<positive aspect 1>", "<positive aspect 2>"],
64
+ "verdict": "approve" | "request_changes",
65
+ "verdictReason": "<one sentence justifying the verdict>",
66
+ "issues": [
67
+ {
68
+ "severity": "major" | "minor" | "nit",
69
+ "description": "<what the issue is and how to fix it>"
70
+ }
71
+ ]
72
+ }
73
+ }
74
+
75
+ Rules:
76
+ - Return ONLY the JSON object, no markdown fences or extra text.
77
+ - Include one entry in "demos" for each filename, in the same order.
78
+ - "file" must exactly match the provided filename.
79
+ - "verdict" must be exactly "approve" or "request_changes".
80
+ - Use "request_changes" if there are any "major" issues.
81
+ - "severity" must be exactly "major", "minor", or "nit".
82
+ - "major": bugs, security issues, broken functionality, guideline violations.
83
+ - "minor": code quality, readability, missing edge cases.
84
+ - "nit": style, naming, trivial improvements.
85
+ - "highlights" must have at least one entry.
86
+ - "issues" can be an empty array if there are no issues.
87
+ - Verify that demo steps demonstrate the acceptance criteria being met.`;
88
+ }
89
+
90
+ export async function invokeClaude(
91
+ prompt: string,
92
+ options?: InvokeClaudeOptions,
93
+ ): Promise<string> {
94
+ const spawnFn = options?.spawn ?? defaultSpawn;
95
+ const agent = options?.agent ?? "claude";
96
+ const proc = spawnFn([agent, "-p", prompt]);
97
+
98
+ const reader = proc.stdout.getReader();
99
+ const chunks: Uint8Array[] = [];
100
+ for (;;) {
101
+ const { done, value } = await reader.read();
102
+ if (done) break;
103
+ chunks.push(value);
104
+ }
105
+
106
+ const exitCode = await proc.exitCode;
107
+ const output = new TextDecoder().decode(
108
+ concatUint8Arrays(chunks),
109
+ );
110
+
111
+ if (exitCode !== 0) {
112
+ throw new Error(
113
+ `claude process exited with code ${exitCode}: ${output.trim()}`,
114
+ );
115
+ }
116
+
117
+ return output.trim();
118
+ }
119
+
120
+ import type { IssueSeverity, ReviewVerdict } from "./review-types.ts";
121
+
122
+ export interface LlmReviewResponse {
123
+ demos: Array<{ file: string; summary: string }>;
124
+ review: {
125
+ summary: string;
126
+ highlights: string[];
127
+ verdict: ReviewVerdict;
128
+ verdictReason: string;
129
+ issues: Array<{ severity: IssueSeverity; description: string }>;
130
+ };
131
+ }
132
+
133
+ const VALID_VERDICTS: ReadonlySet<string> = new Set(["approve", "request_changes"]);
134
+ const VALID_SEVERITIES: ReadonlySet<string> = new Set(["major", "minor", "nit"]);
135
+
136
+ export function extractJson(raw: string): string {
137
+ // Try raw string first
138
+ try {
139
+ JSON.parse(raw);
140
+ return raw;
141
+ } catch {
142
+ // look for first { and last }
143
+ }
144
+
145
+ const start = raw.indexOf("{");
146
+ const end = raw.lastIndexOf("}");
147
+ if (start === -1 || end === -1 || end <= start) {
148
+ throw new Error(`No JSON object found in LLM response: ${raw.slice(0, 200)}`);
149
+ }
150
+
151
+ return raw.slice(start, end + 1);
152
+ }
153
+
154
+ export function parseLlmResponse(raw: string): LlmReviewResponse {
155
+ const jsonStr = extractJson(raw);
156
+
157
+ let parsed: unknown;
158
+ try {
159
+ parsed = JSON.parse(jsonStr);
160
+ } catch {
161
+ throw new Error(`Invalid JSON from LLM: ${raw.slice(0, 200)}`);
162
+ }
163
+
164
+ if (typeof parsed !== "object" || parsed === null || !("demos" in parsed)) {
165
+ throw new Error("Missing 'demos' array in review metadata");
166
+ }
167
+
168
+ const obj = parsed as Record<string, unknown>;
169
+ if (!Array.isArray(obj["demos"])) {
170
+ throw new Error("'demos' must be an array");
171
+ }
172
+
173
+ for (const demo of obj["demos"] as unknown[]) {
174
+ if (typeof demo !== "object" || demo === null) {
175
+ throw new Error("Each demo must be an object");
176
+ }
177
+ const d = demo as Record<string, unknown>;
178
+
179
+ if (typeof d["file"] !== "string") {
180
+ throw new Error("Each demo must have a 'file' string");
181
+ }
182
+ if (typeof d["summary"] !== "string") {
183
+ throw new Error("Each demo must have a 'summary' string");
184
+ }
185
+ }
186
+
187
+ if (typeof obj["review"] !== "object" || obj["review"] === null) {
188
+ throw new Error("Missing 'review' object in response");
189
+ }
190
+
191
+ const review = obj["review"] as Record<string, unknown>;
192
+
193
+ if (typeof review["summary"] !== "string") {
194
+ throw new Error("review.summary must be a string");
195
+ }
196
+
197
+ if (!Array.isArray(review["highlights"])) {
198
+ throw new Error("review.highlights must be an array");
199
+ }
200
+ if (review["highlights"].length === 0) {
201
+ throw new Error("review.highlights must not be empty");
202
+ }
203
+ for (const h of review["highlights"]) {
204
+ if (typeof h !== "string") {
205
+ throw new Error("Each highlight must be a string");
206
+ }
207
+ }
208
+
209
+ if (typeof review["verdict"] !== "string" || !VALID_VERDICTS.has(review["verdict"])) {
210
+ throw new Error("review.verdict must be 'approve' or 'request_changes'");
211
+ }
212
+
213
+ if (typeof review["verdictReason"] !== "string") {
214
+ throw new Error("review.verdictReason must be a string");
215
+ }
216
+
217
+ if (!Array.isArray(review["issues"])) {
218
+ throw new Error("review.issues must be an array");
219
+ }
220
+
221
+ for (const issue of review["issues"] as unknown[]) {
222
+ if (typeof issue !== "object" || issue === null) {
223
+ throw new Error("Each issue must be an object");
224
+ }
225
+ const i = issue as Record<string, unknown>;
226
+ if (typeof i["severity"] !== "string" || !VALID_SEVERITIES.has(i["severity"])) {
227
+ throw new Error("Each issue severity must be 'major', 'minor', or 'nit'");
228
+ }
229
+ if (typeof i["description"] !== "string") {
230
+ throw new Error("Each issue must have a 'description' string");
231
+ }
232
+ }
233
+
234
+ return parsed as LlmReviewResponse;
235
+ }
236
+
237
+ function defaultSpawn(
238
+ cmd: string[],
239
+ ): { exitCode: Promise<number>; stdout: ReadableStream<Uint8Array> } {
240
+ const [command, ...args] = cmd;
241
+ const proc = Bun.spawn([command!, ...args], {
242
+ stdout: "pipe",
243
+ stderr: "pipe",
244
+ });
245
+ return {
246
+ exitCode: proc.exited,
247
+ stdout: proc.stdout as unknown as ReadableStream<Uint8Array>,
248
+ };
249
+ }
250
+
251
+ function concatUint8Arrays(arrays: Uint8Array[]): Uint8Array {
252
+ const totalLength = arrays.reduce((sum, a) => sum + a.length, 0);
253
+ const result = new Uint8Array(totalLength);
254
+ let offset = 0;
255
+ for (const a of arrays) {
256
+ result.set(a, offset);
257
+ offset += a.length;
258
+ }
259
+ return result;
260
+ }