-
-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathgenerateDocumentation.ts
More file actions
246 lines (208 loc) · 6.71 KB
/
Copy pathgenerateDocumentation.ts
File metadata and controls
246 lines (208 loc) · 6.71 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
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
import * as fs from "fs";
import * as path from "path";
import * as https from "https";
const DOCUMENTATION_DIR = path.join(__dirname, "documentation");
const OUTPUT_FILE = path.join(__dirname, "constants", "documentation.ts");
const ENV_FILE = path.join(__dirname, ".env.local");
interface Metadata {
title: string;
description: string;
}
interface DocumentationEntry {
title: string;
description: string;
vector: number[];
text: string;
}
interface Documentation {
[key: string]: DocumentationEntry;
}
function parseMetadata(content: string): { metadata: Metadata; text: string } {
const metadataStart = content.indexOf("===METADATA===");
const metadataEnd = content.indexOf("===END METADATA===");
if (metadataStart === -1 || metadataEnd === -1) {
// No metadata found, return defaults
return {
metadata: { title: "", description: "" },
text: content.trim(),
};
}
const metadataSection = content.slice(
metadataStart + "===METADATA===".length,
metadataEnd,
);
const textAfterMetadata = content
.slice(metadataEnd + "===END METADATA===".length)
.trim();
const metadata: Metadata = { title: "", description: "" };
for (const line of metadataSection.split("\n")) {
const trimmed = line.trim();
if (trimmed.startsWith("title:")) {
metadata.title = trimmed.slice("title:".length).trim();
} else if (trimmed.startsWith("description:")) {
metadata.description = trimmed.slice("description:".length).trim();
}
}
return { metadata, text: textAfterMetadata };
}
function loadEnvFile(): Record<string, string> {
const envContent = fs.readFileSync(ENV_FILE, "utf-8");
const env: Record<string, string> = {};
for (const line of envContent.split("\n")) {
const trimmed = line.trim();
if (trimmed && !trimmed.startsWith("#")) {
const eqIndex = trimmed.indexOf("=");
if (eqIndex !== -1) {
const key = trimmed.slice(0, eqIndex).trim();
let value = trimmed.slice(eqIndex + 1).trim();
// Remove quotes if present
if (
(value.startsWith('"') && value.endsWith('"')) ||
(value.startsWith("'") && value.endsWith("'"))
) {
value = value.slice(1, -1);
}
env[key] = value;
}
}
}
return env;
}
function getEmbedding(text: string, apiKey: string): Promise<number[]> {
return new Promise((resolve, reject) => {
const requestBody = JSON.stringify({
model: "text-embedding-3-small",
input: text,
});
const options: https.RequestOptions = {
hostname: "api.openai.com",
port: 443,
path: "/v1/embeddings",
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${apiKey}`,
"Content-Length": Buffer.byteLength(requestBody),
},
};
const req = https.request(options, (res) => {
let data = "";
res.on("data", (chunk) => {
data += chunk;
});
res.on("end", () => {
try {
const response = JSON.parse(data);
if (response.error) {
reject(new Error(response.error.message));
} else {
resolve(response.data[0].embedding);
}
} catch (_e) {
reject(new Error(`Failed to parse response: ${data}`));
}
});
});
req.on("error", (e) => {
reject(e);
});
req.write(requestBody);
req.end();
});
}
function getDocumentationFiles(): string[] {
const files = fs.readdirSync(DOCUMENTATION_DIR);
return files.filter((file) => file.endsWith(".md"));
}
function escapeString(str: string): string {
return str
.replace(/\\/g, "\\\\")
.replace(/`/g, "\\`")
.replace(/\$/g, "\\$")
.replace(/"/g, '\\"')
.replace(/'/g, "\\'");
}
async function processBatch(
files: string[],
apiKey: string,
startIndex: number,
batchSize: number,
): Promise<{ name: string; entry: DocumentationEntry }[]> {
const batch = files.slice(startIndex, startIndex + batchSize);
const promises = batch.map(async (file) => {
const filePath = path.join(DOCUMENTATION_DIR, file);
const rawContent = fs.readFileSync(filePath, "utf-8").trim();
const name = path.basename(file, ".md");
const { metadata, text } = parseMetadata(rawContent);
try {
const vector = await getEmbedding(text, apiKey);
return {
name,
entry: {
title: metadata.title,
description: metadata.description,
vector,
text,
},
};
} catch (error) {
throw new Error(`Failed to get embedding for ${name}: ${error}`);
}
});
return Promise.all(promises);
}
async function main() {
console.log("Loading environment variables...");
const env = loadEnvFile();
const apiKey = env["OPEN_AI_KEY"];
if (!apiKey) {
console.error("OPEN_AI_KEY not found in .env.local");
process.exit(1);
}
console.log("Reading documentation files...");
const files = getDocumentationFiles();
console.log(`Found ${files.length} documentation files`);
const documentation: Documentation = {};
const batchSize = 10;
let processed = 0;
for (let i = 0; i < files.length; i += batchSize) {
const batchNum = Math.floor(i / batchSize) + 1;
const totalBatches = Math.ceil(files.length / batchSize);
console.log(`Processing batch ${batchNum}/${totalBatches}...`);
try {
const results = await processBatch(files, apiKey, i, batchSize);
for (const { name, entry } of results) {
documentation[name] = entry;
}
processed += results.length;
console.log(`Progress: ${processed}/${files.length} files processed`);
} catch (error) {
console.error(`Error processing batch ${batchNum}:`, error);
process.exit(1);
}
}
console.log("\nGenerating output file...");
let output = "// Auto-generated file - do not edit manually\n";
output += "// Generated by generateDocumentation.ts\n\n";
output += "export const DOCUMENTATION = {\n";
const entries = Object.entries(documentation);
for (let i = 0; i < entries.length; i++) {
const [name, { title, description, vector, text }] = entries[i];
const isLast = i === entries.length - 1;
output += ` ${name}: {\n`;
output += ` key: "${name}",\n`;
output += ` title: "${escapeString(title)}",\n`;
output += ` description: "${escapeString(description)}",\n`;
output += ` vector: [${vector.join(", ")}],\n`;
output += ` text: \`${escapeString(text)}\`,\n`;
output += ` }${isLast ? "" : ","}\n`;
}
output += "};\n";
fs.writeFileSync(OUTPUT_FILE, output, "utf-8");
console.log(`✓ Written to ${OUTPUT_FILE}`);
console.log("Done!");
}
main().catch((error) => {
console.error("Fatal error:", error);
process.exit(1);
});