Skip to content

Job application tracker and AI-powered job search assistant. Helps job seekers manage their search journey with AI resume review, job matching, task logging, and application analytics, all while keeping your data private.

feat(cover-letter): extract keywords and tailoring tips from match data

Khuram Niaz2f5a8ce85612
diff --git a/__tests__/matchGuidance.spec.ts b/__tests__/matchGuidance.spec.ts
new file mode 100644
index 0000000..5b6fdea
--- /dev/null
+++ b/__tests__/matchGuidance.spec.ts
@@ -0,0 +1,59 @@
+import { extractMatchGuidance } from "@/lib/ai/coverLetter/matchGuidance";
+
+const body = [
+ "## Summary",
+ "Decent fit overall.",
+ "",
+ "## Keywords",
+ "Found: React, TypeScript",
+ "Missing: Kubernetes",
+ "",
+ "## Deal Breakers",
+ "None.",
+ "",
+ "## Tailoring Tips",
+ "- Lead the summary with platform work",
+].join("\n");
+
+const matchData = JSON.stringify({ matchScore: 72, body });
+
+describe("extractMatchGuidance", () => {
+ it("returns null for empty input", () => {
+ expect(extractMatchGuidance(null)).toBeNull();
+ expect(extractMatchGuidance(undefined)).toBeNull();
+ expect(extractMatchGuidance("")).toBeNull();
+ });
+
+ it("returns null for malformed JSON", () => {
+ expect(extractMatchGuidance("{not json")).toBeNull();
+ });
+
+ it("returns null when there is no body", () => {
+ expect(extractMatchGuidance(JSON.stringify({ matchScore: 50 }))).toBeNull();
+ });
+
+ it("returns null when neither section is present", () => {
+ const raw = JSON.stringify({ body: "## Summary\nOnly a summary." });
+ expect(extractMatchGuidance(raw)).toBeNull();
+ });
+
+ it("extracts Keywords and Tailoring Tips only", () => {
+ const result = extractMatchGuidance(matchData);
+ expect(result).toContain("## Keywords");
+ expect(result).toContain("Missing: Kubernetes");
+ expect(result).toContain("## Tailoring Tips");
+ expect(result).toContain("Lead the summary with platform work");
+ expect(result).not.toContain("Deal Breakers");
+ expect(result).not.toContain("Decent fit overall");
+ });
+
+ it("matches headings case-insensitively", () => {
+ const raw = JSON.stringify({ body: "## keywords\nFound: Go" });
+ expect(extractMatchGuidance(raw)).toContain("Found: Go");
+ });
+
+ it("captures a trailing section that runs to end of string", () => {
+ const raw = JSON.stringify({ body: "## Tailoring Tips\n- Do the thing" });
+ expect(extractMatchGuidance(raw)).toContain("- Do the thing");
+ });
+});
diff --git a/src/lib/ai/coverLetter/matchGuidance.ts b/src/lib/ai/coverLetter/matchGuidance.ts
new file mode 100644
index 0000000..4cbfba2
--- /dev/null
+++ b/src/lib/ai/coverLetter/matchGuidance.ts
@@ -0,0 +1,38 @@
+const WANTED_SECTIONS = ["Keywords", "Tailoring Tips"];
+
+// Grabs a "## Heading" block up to the next "##" or end of string.
+const sectionFor = (body: string, heading: string): string | null => {
+ const pattern = new RegExp(
+ `^##\\s*${heading}\\s*$([\\s\\S]*?)(?=^##\\s|\\s*$(?![\\s\\S]))`,
+ "im",
+ );
+ const match = body.match(pattern);
+ if (!match) return null;
+ const content = match[1].trim();
+ return content ? `## ${heading}\n${content}` : null;
+};
+
+// Pulls only the two actionable sections out of a persisted match analysis.
+// The rest (scores, gaps, deal breakers) would push the letter toward
+// apologising for weaknesses rather than selling strengths.
+export function extractMatchGuidance(
+ matchDataRaw: string | null | undefined,
+): string | null {
+ if (!matchDataRaw) return null;
+
+ let body: unknown;
+ try {
+ body = (JSON.parse(matchDataRaw) as { body?: unknown }).body;
+ } catch {
+ return null;
+ }
+ if (typeof body !== "string" || !body.trim()) return null;
+
+ const sections = WANTED_SECTIONS.map((heading) =>
+ sectionFor(body as string, heading),
+ ).filter((section): section is string => section !== null);
+
+ if (sections.length === 0) return null;
+
+ return sections.join("\n\n");
+}