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
// This val creates a cover letter generator using OpenAI's GPT model
// It takes a resume (as a PDF file) and job description as input and returns a concise cover letter
import { OpenAI } from "https://esm.town/v/std/openai";
export default async function server(req: Request): Promise<Response> {
console.log("Request method:", req.method);
if (req.method === "GET") {
console.log("Serving GET request");
return new Response(`
<html>
<head>
<title>Cover Letter Generator</title>
<style>
body { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; line-height: 1.6; }
textarea { width: 100%; height: 200px; margin-bottom: 10px; }
input[type="submit"] { padding: 10px 20px; background-color: #4CAF50; color: white; border: none; cursor: pointer; }
</style>
</head>
<body>
<h1>Cover Letter Generator</h1>
<form method="POST" enctype="multipart/form-data">
<h2>Upload Resume (PDF)</h2>
<input type="file" name="resume" accept=".pdf" required>
<h2>Job Description</h2>
<p>Paste the job description below:</p>
<textarea name="jobDescription" placeholder="Paste the job description here" required></textarea>
<br>
<input type="submit" value="Generate Cover Letter">
</form>
<p><a href="${import.meta.url.replace("esm.town", "val.town")}">View Source</a></p>
</body>
</html>
`, {
headers: { "Content-Type": "text/html" },
});
} else if (req.method === "POST") {
console.log("Handling POST request");
const formData = await req.formData();
const resumeFile = formData.get("resume") as File;
const jobDescription = formData.get("jobDescription") as string;
console.log("Resume file name:", resumeFile?.name);
console.log("Job Description length:", jobDescription?.length);
if (!resumeFile || !jobDescription) {
return new Response("Both resume file and job description are required.", { status: 400 });
}
if (!resumeFile.name.toLowerCase().endsWith('.pdf')) {
return new Response("Resume must be a PDF file.", { status: 400 });
}
const resumeText = await extractTextFromPDF(resumeFile);
if (!resumeText) {
return new Response("Failed to extract text from the PDF. Please ensure it's a valid PDF file.", { status: 400 });
}
try {
console.log("Generating cover letter");
const coverLetter = await generateCoverLetter(resumeText, jobDescription);
console.log("Cover letter generated, length:", coverLetter.length);
return new Response(`
<html>
<head>
<title>Generated Cover Letter</title>
<style>
body { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; }
pre { white-space: pre-wrap; word-wrap: break-word; }
</style>
</head>
<body>
<h1>Generated Cover Letter</h1>
<pre>${coverLetter}</pre>
<p><a href="${req.url}">Generate Another Cover Letter</a></p>
</body>
</html>
`, {
headers: { "Content-Type": "text/html" },
});
} catch (error) {
console.error("Error generating cover letter:", error);
return new Response(`Error generating cover letter: ${error.message}`, { status: 500 });
}
} else {
return new Response("Method not allowed", { status: 405 });
}
}
async function extractTextFromPDF(file: File): Promise<string | null> {
try {
const { default: pdfjs } = await import("https://esm.sh/pdfjs-dist@3.4.120/build/pdf.min.js");
const arrayBuffer = await file.arrayBuffer();
const pdf = await pdfjs.getDocument({ data: arrayBuffer }).promise;
let text = '';
for (let i = 1; i <= pdf.numPages; i++) {
const page = await pdf.getPage(i);