-
Notifications
You must be signed in to change notification settings - Fork 2
/
use-prompt.mjs
executable file
·295 lines (262 loc) · 8.62 KB
/
use-prompt.mjs
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
const PROMPTS_FILE = "node_modules/.swc-plugin-use-prompt/prompts";
const CHANGES_FILE = "node_modules/.swc-plugin-use-prompt/changes";
import env from "@next/env";
env.loadEnvConfig(process.cwd());
/** @typedef {import('@swc/core').Module} Module */
/** @typedef {import('@swc/core').FunctionDeclaration} FunctionDeclaration */
/** @typedef {import('@swc/core').FunctionExpression} FunctionExpression */
/** @typedef {import('@swc/core').Span} Span */
import crypto from "node:crypto";
import fs from "node:fs/promises";
import path from "node:path";
import { Compiler } from "@swc/core";
import { Command, Option } from "commander";
import watch from "glob-watcher";
import OpenAI from "openai";
/**
* really bad not-visitor-pattern, but im def not coding every single variant
* for something as unhinged as this.
* @param {object} root
* @returns {(FunctionDeclaration | FunctionExpression)[]}
*/
function findFunctionDecls(root) {
const found = [];
const stack = [root];
while (stack.length) {
const current = stack.pop();
if (Array.isArray(current)) {
stack.push(...current);
continue;
}
if (typeof current !== "object" || current === null) continue;
if (
current.type === "FunctionDeclaration" ||
current.type === "FunctionExpression"
)
found.push(current);
stack.push(...Object.values(current));
}
return found;
}
const compiler = new Compiler();
/**
* Find all uses of the `use prompt` directive in the given source.
* Extract their associated positions and prompts.
* @param {string} source
* @returns {Promise<null | {span: Span, prompt: string, signature: string}[]>}
*/
async function identifyPromptRequests(source) {
/** @type {Module} */
let module;
try {
module = compiler.parseSync(source, {
syntax: "typescript",
tsx: true,
isModule: true,
});
} catch (e) {
console.error(e);
return null;
}
const decls = findFunctionDecls(module).filter((decl) => decl.body);
const prompts = decls
.map((decl) => {
decl.span.start -= module.span.start - 1;
decl.span.end -= module.span.start - 1;
decl.body.span.start -= module.span.start - 1;
decl.body.span.end -= module.span.start - 1;
const prologue = [];
for (let expr of decl.body.stmts) {
if (expr.type !== "ExpressionStatement") break;
if (expr.expression.type !== "StringLiteral") break;
prologue.push(expr.expression.value);
}
for (let string of prologue) {
if (!string.startsWith("use prompt:")) continue;
const prompt = string.slice(11).trim();
if (!prompt.length) continue;
return { decl, prompt };
}
})
.filter(Boolean);
const requests = prompts.map(({ decl, prompt }) => {
const headerStart = decl.span.start - 1;
const headerEnd = decl.body.span.start - 1;
const signature = source.slice(headerStart, headerEnd) + "{}";
return {
span: decl.span,
prompt,
signature,
};
});
return requests;
}
const SYSTEM_PROMPT = `You are an expert React developer. Generate code that strictly adheres to the function signature provided. Do not generate additional functions. Exclude the function header from your response.
If you require any additional imports, include that in your response specifying exactly which functions and components need to be imported. Do not include an import for the React global.
The codebase is a NextJS project using the App Router. There is no support for Tailwind.`;
/**
* Call OpenAI for a given prompt and parse out the results.
* @param {import('openai').OpenAI} openai
* @param {string} source
* @param {string} prompt
* @param {string} signature
* @returns {Promise<null | {code:string, imports: string | null}>}
*/
async function getPromptResults(openai, source, prompt, signature) {
try {
const rawResponse = await openai.chat.completions.create({
model: "gpt-4o",
messages: [
{ role: "system", content: [{ type: "text", text: SYSTEM_PROMPT }] },
{
role: "user",
content: [
{
type: "text",
text:
"This is the full source file:\n\n" +
"```tsx\n" +
"import React from 'react';\n" +
source +
"\n```\n\n" +
`Generate code to meet these requirements: ${prompt}` +
"\n\n" +
`It must strictly follow this function header: \`${signature}\`.` +
"\n\n" +
`Make sure to include the required library imports in a separate key. Do not include the function header your response.`,
},
],
},
],
temperature: 1,
max_tokens: 2048,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
response_format: {
type: "json_schema",
json_schema: {
name: "generated_code",
strict: true,
schema: {
type: "object",
required: ["code", "imports"],
properties: {
code: {
type: "string",
description:
"The generated code as a string, excluding the function header.",
},
imports: {
type: "string",
description: "Imports required, or an empty string if none.",
},
},
additionalProperties: false,
},
},
},
});
const res = rawResponse.choices[0];
if (res.finish_reason !== "stop") return null;
let { code, imports } = JSON.parse(res.message.content);
return { code, imports };
} catch (e) {
console.error(e);
return null;
}
}
/**
* Identify all `use prompt`s in the changed file, then call OpenAI to run
* codegen
* @param {string} filePath
* @param {import('openai').OpenAI} openai
* @returns {Promise<null | {span: Span, prompt: string, value: {code: string, imports: null | string}}}
*/
async function getUpdatedCodegen(filePath, openai) {
const source = (await fs.readFile(filePath)).toString();
if (!(await shouldHandleFile(filePath, source))) return;
console.log(`🤖 ${filePath} ...`);
const requests = await identifyPromptRequests(source);
if (!requests) return null;
return (
await Promise.all(
requests.map(async ({ prompt, signature, span }) => ({
span,
prompt,
value: await getPromptResults(openai, source, prompt, signature),
})),
)
).filter(Boolean);
}
function hashDigest(contents) {
return crypto.createHash("sha256").update(contents, "utf8").digest("hex");
}
let changes;
try {
changes = JSON.parse(fsC.readFile(CHANGES_FILE).toString());
} catch (e) {
changes = {};
}
async function shouldHandleFile(filePath, contents) {
const hash = hashDigest(contents);
// console.log(filePath, hash);
let should = false;
if (changes[filePath] !== hash) {
changes[filePath] = hash;
should = true;
await fs.writeFile(CHANGES_FILE, JSON.stringify(changes));
}
return should;
}
let prompts;
try {
prompts = JSON.parse(fsC.readFile(PROMPTS_FILE).toString());
} catch (e) {
prompts = {};
}
async function updatePromptsFile(updates) {
for (let { span, prompt, value } of updates) {
// console.log(span, prompt, value);
if (!prompts[span.start]) prompts[span.start] = {};
if (!prompts[span.start][span.end]) prompts[span.start][span.end] = {};
prompts[span.start][span.end][prompt] = value;
}
await fs.writeFile(PROMPTS_FILE, JSON.stringify(prompts));
}
async function initialize({ patterns, apiKey }) {
console.log(`👀 Watching ${patterns.join(", ")}`);
await fs.mkdir(path.dirname(PROMPTS_FILE), { recursive: true });
const openai = new OpenAI({ apiKey });
const watcher = watch(patterns, { ignoreInitial: false });
async function processPath(path) {
const updates = await getUpdatedCodegen(path, openai);
if (!updates) {
return;
}
await updatePromptsFile(updates);
await fs.utimes(path, new Date(), new Date());
console.log(`✅ ${path} is ✨ e n h a n c e d ✨`);
}
watcher.on("add", processPath);
watcher.on("change", processPath);
}
const program = new Command();
program
.name("use-prompt")
.description(
"Compile-time GenAI!\n\nPre-build step to parse prompts and generate an intermediate cache file.",
)
.addOption(
new Option("-k, --api-key <string>", "openai api key").env(
"OPENAI_API_KEY",
),
)
.addOption(
new Option("-p, --patterns <patterns...>", "patterns to watch").default([
"app/**/*.{tsx,jsx,ts,js}",
"src/**/*.{tsx,jsx,ts,js}",
]),
)
.action(initialize);
program.parse();