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.
TypeScript ai-agentai-resume-analyzerapplication-trackerapplication-tracking-systematscareerjob-applicationjob-automationjob-huntingjob-searchjob-seeker-toolsjob-trackerjob-trackingjobsearchjobseekersmcpollamaresumeresume-reviewself-hosted
Refactor resume review feature to support markdown output and streaming
Khuram Niazd5d7f7982bf4
diff --git a/__tests__/AiResumeReviewSection.spec.tsx b/__tests__/AiResumeReviewSection.spec.tsxindex 30d6a79..d6181d4 100644--- a/__tests__/AiResumeReviewSection.spec.tsx+++ b/__tests__/AiResumeReviewSection.spec.tsx@@ -9,17 +9,9 @@ import {import AiResumeReviewSection from "@/components/profile/AiResumeReviewSection";import { Resume, SectionType } from "@/models/profile.model";-const mockSubmit = vi.fn();-const mockStop = vi.fn();-let mockUseObjectReturn = {- object: null as any,- submit: mockSubmit,- isLoading: false,- stop: mockStop,-};--vi.mock("@ai-sdk/react", () => ({- experimental_useObject: () => mockUseObjectReturn,+const mockStreamResumeReview = vi.fn();+vi.mock("@/utils/streamResumeReview.utils", () => ({+ streamResumeReview: (...args: any[]) => mockStreamResumeReview(...args),}));vi.mock("@/actions/userSettings.actions", () => ({@@ -56,7 +48,7 @@ vi.mock("@/hooks/useSlowResponseWarning", () => ({}));vi.mock("@/components/ui/sheet", () => ({- Sheet: ({ children, open, onOpenChange }: any) => (+ Sheet: ({ children, open }: any) => (<div data-testid="sheet" data-open={open}>{children}</div>@@ -67,7 +59,7 @@ vi.mock("@/components/ui/sheet", () => ({),SheetHeader: ({ children }: any) => <div>{children}</div>,SheetTitle: ({ children }: any) => <div>{children}</div>,- SheetTrigger: ({ children, asChild }: any) => (+ SheetTrigger: ({ children }: any) => (<div data-testid="sheet-trigger">{children}</div>),}));@@ -91,100 +83,55 @@ const makeResume = (sectionCount: number): Resume => ({})),});-describe("AiResumeReviewSection – Review button state", () => {+const getGenerateButton = () =>+ screen.queryAllByRole("button", { name: /generate ai review/i })[0];++describe("AiResumeReviewSection – Review trigger button state", () => {beforeEach(() => {vi.clearAllMocks();- mockUseObjectReturn = {- object: null,- submit: mockSubmit,- isLoading: false,- stop: mockStop,- };+ mockStreamResumeReview.mockResolvedValue({+ scores: { overall: 85, impact: 80, clarity: 82, atsCompatibility: 78 },+ body: "## Summary\nGreat resume",+ });});- it("is enabled when resume has fewer than 2 sections", () => {+ it("is enabled regardless of section count", () => {render(<AiResumeReviewSection resume={makeResume(1)} />);const trigger = within(screen.getByTestId("sheet-trigger")).getByRole("button",);expect(trigger).not.toBeDisabled();});-- it("is enabled when resume has no sections", () => {- render(<AiResumeReviewSection resume={makeResume(0)} />);- const trigger = within(screen.getByTestId("sheet-trigger")).getByRole(- "button",- );- expect(trigger).not.toBeDisabled();- });-- it("is enabled when resume has 2 or more sections", () => {- render(<AiResumeReviewSection resume={makeResume(2)} />);- const trigger = within(screen.getByTestId("sheet-trigger")).getByRole(- "button",- );- expect(trigger).not.toBeDisabled();- });-- it("is enabled when resume has 3 sections", () => {- render(<AiResumeReviewSection resume={makeResume(3)} />);- const trigger = within(screen.getByTestId("sheet-trigger")).getByRole(- "button",- );- expect(trigger).not.toBeDisabled();- });-- it("is disabled while AI is loading", () => {- mockUseObjectReturn = { ...mockUseObjectReturn, isLoading: true };- render(<AiResumeReviewSection resume={makeResume(3)} />);- const trigger = within(screen.getByTestId("sheet-trigger")).getByRole(- "button",- );- expect(trigger).toBeDisabled();- });});describe("AiResumeReviewSection – Generate AI Review button", () => {beforeEach(() => {vi.clearAllMocks();- mockUseObjectReturn = {- object: null,- submit: mockSubmit,- isLoading: false,- stop: mockStop,- };+ mockStreamResumeReview.mockResolvedValue({+ scores: { overall: 85, impact: 80, clarity: 82, atsCompatibility: 78 },+ body: "## Summary\nGreat resume",+ });});- it("shows toast error when resume has fewer than 2 sections and generate is called", () => {+ it("shows toast error when resume has fewer than 2 sections", () => {render(<AiResumeReviewSection resume={makeResume(1)} />);-- const generateButtons = screen.queryAllByRole("button", {- name: /generate ai review/i,- });- if (generateButtons.length > 0) {- fireEvent.click(generateButtons[0]);- expect(mockToast).toHaveBeenCalledWith(- expect.objectContaining({- variant: "destructive",- title: "Not enough content",- }),- );- }+ fireEvent.click(getGenerateButton());+ expect(mockToast).toHaveBeenCalledWith(+ expect.objectContaining({+ variant: "destructive",+ title: "Not enough content",+ }),+ );+ expect(mockStreamResumeReview).not.toHaveBeenCalled();});- it("calls submit with resume and model when generate is clicked with valid resume", async () => {+ it("calls streamResumeReview with the resume id when generate is clicked", () => {const resume = makeResume(3);render(<AiResumeReviewSection resume={resume} />);-- const generateButtons = screen.queryAllByRole("button", {- name: /generate ai review/i,- });- if (generateButtons.length > 0) {- fireEvent.click(generateButtons[0]);- expect(mockSubmit).toHaveBeenCalledWith(- expect.objectContaining({ resume }),- );- }+ fireEvent.click(getGenerateButton());+ expect(mockStreamResumeReview).toHaveBeenCalledWith(+ expect.objectContaining({ resumeId: resume.id }),+ );});});@@ -193,24 +140,21 @@ describe("AiResumeReviewSection – loading state", () => {vi.clearAllMocks();});- it("shows loading indicator while AI is streaming", () => {- mockUseObjectReturn = {- object: null,- submit: mockSubmit,- isLoading: true,- stop: mockStop,- };+ it("shows loading indicator while the review is streaming", async () => {+ // Pending stream keeps the component in the loading state.+ mockStreamResumeReview.mockReturnValue(new Promise(() => {}));render(<AiResumeReviewSection resume={makeResume(3)} />);- expect(screen.getByTestId("loading")).toBeInTheDocument();+ fireEvent.click(getGenerateButton());+ await waitFor(() =>+ expect(screen.getByTestId("loading")).toBeInTheDocument(),+ );});- it("shows review content after streaming completes", () => {- mockUseObjectReturn = {- object: { summary: "Great resume", scores: { overall: 85 } },- submit: mockSubmit,- isLoading: false,- stop: mockStop,- };+ it("shows review content when not loading", () => {+ mockStreamResumeReview.mockResolvedValue({+ scores: { overall: 85, impact: 80, clarity: 82, atsCompatibility: 78 },+ body: "## Summary\nGreat resume",+ });render(<AiResumeReviewSection resume={makeResume(3)} />);expect(screen.getByTestId("review-content")).toBeInTheDocument();expect(screen.queryByTestId("loading")).not.toBeInTheDocument();diff --git a/__tests__/auth.actions.spec.ts b/__tests__/auth.actions.spec.tsindex 375df3e..678a153 100644--- a/__tests__/auth.actions.spec.ts+++ b/__tests__/auth.actions.spec.ts@@ -427,7 +427,7 @@ describe("Auth Actions", () => {try {await authenticate("", mockFormData);- fail("Should have thrown");+ expect.fail("Should have thrown");} catch (err) {expect((err as Error).message).toBe("Database error");}diff --git a/__tests__/resume-pdf.spec.ts b/__tests__/resume-pdf.spec.tsindex 2561043..bd61053 100644--- a/__tests__/resume-pdf.spec.ts+++ b/__tests__/resume-pdf.spec.ts@@ -66,7 +66,8 @@ describe("htmlToPdfNodes", () => {it("produces a node for a <p> tag", () => {const nodes = htmlToPdfNodes("<p>Hello world</p>");expect(nodes).toHaveLength(1);- expect(nodes[0].props?.children).toContain("Hello world");+ const props = nodes[0].props as { children?: unknown };+ expect(props.children).toContain("Hello world");});it("applies both bold and italic for <strong><em> nesting", () => {diff --git a/evals/resume-review/assertions.ts b/evals/resume-review/assertions.tsindex 4b24a5a..9eacd16 100644--- a/evals/resume-review/assertions.ts+++ b/evals/resume-review/assertions.ts@@ -1,142 +1,100 @@type AssertionResult = { pass: boolean; score: number; reason: string };-// Models often wrap JSON in markdown code fences or add preamble text-function extractJson(output: string): string {- const trimmed = output.trim();- const start = trimmed.search(/[{[]/);- const end = Math.max(trimmed.lastIndexOf('}'), trimmed.lastIndexOf(']'));- if (start === -1 || end === -1) return trimmed;- return trimmed.slice(start, end + 1);-}+// Mirrors the client parsing in src/utils/streamResumeReview.utils.ts so the+// eval validates exactly what a user would see rendered.-export function assertRequiredFields(output: string): AssertionResult {- let parsed: Record<string, unknown>;- try {- parsed = JSON.parse(extractJson(output));- } catch {- return { pass: false, score: 0, reason: 'Output is not valid JSON' };- }+// Mirrors src/utils/streamResumeReview.utils.ts: match \d+ (not {1,3}) so an+// out-of-range score still parses instead of failing the whole line.+const SCORES_RE =+ /SCORES:\s*overall=(\d+)\s+impact=(\d+)\s+clarity=(\d+)\s+ats=(\d+)/i;- const required = ['achievements', 'keywords', 'actionVerbs', 'atsIssues', 'topImprovements', 'grammarAndSpelling', 'summary'];- for (const key of required) {- if (!(key in parsed)) {- return { pass: false, score: 0, reason: `Missing required field: ${key}` };- }- }- // Accept flat score keys as an alias — models occasionally omit the 'scores' wrapper- const hasScores = 'scores' in parsed || ('overall' in parsed && 'impact' in parsed);- if (!hasScores) {- return { pass: false, score: 0, reason: 'Missing required field: scores' };- }- // Accept 'sections' as an alias — models occasionally use it instead of 'sectionFeedback'- if (!('sectionFeedback' in parsed) && !('sections' in parsed)) {- return { pass: false, score: 0, reason: 'Missing required field: sectionFeedback' };- }- return { pass: true, score: 1, reason: 'All required top-level fields present' };-}+type ParsedScores = {+ overall: number;+ impact: number;+ clarity: number;+ ats: number;+};-export function assertValidJson(output: string): AssertionResult {- try {- JSON.parse(extractJson(output));- return { pass: true, score: 1, reason: 'Output is valid JSON' };- } catch (e) {- return { pass: false, score: 0, reason: `Output is not valid JSON: ${e}` };- }+function stripThinking(text: string): string {+ let out = text.replace(/<think>[\s\S]*?<\/think>/gi, '');+ const openIdx = out.search(/<think>/i);+ if (openIdx !== -1) out = out.slice(0, openIdx);+ return out;}-export function assertScoresInRange(output: string): AssertionResult {- let parsed: Record<string, unknown>;- try {- parsed = JSON.parse(extractJson(output));- } catch {- return { pass: false, score: 0, reason: 'Output is not valid JSON' };- }+function parseReview(output: string): { scores?: ParsedScores; body: string } {+ const text = stripThinking(output);+ const match = text.match(SCORES_RE);+ const scores: ParsedScores | undefined = match+ ? {+ overall: Number(match[1]),+ impact: Number(match[2]),+ clarity: Number(match[3]),+ ats: Number(match[4]),+ }+ : undefined;+ const body = (+ match+ ? text.replace(match[0], '')+ : text.replace(/^\s*SCORES:[^\n]*(\n|$)/i, '')+ ).trim();+ return { scores, body };+}- // Accept flat score keys as an alias — models occasionally omit the 'scores' wrapper- const scores = (parsed?.scores ?? parsed) as Record<string, unknown>;- const fields = ['overall', 'impact', 'clarity', 'atsCompatibility'] as const;- for (const field of fields) {- const val = scores?.[field];- if (typeof val !== 'number') {- return { pass: false, score: 0, reason: `scores.${field} is not a number (got ${typeof val})` };+export function assertScoresLine(output: string): AssertionResult {+ const { scores } = parseReview(output);+ if (!scores) {+ return { pass: false, score: 0, reason: 'No valid SCORES: line found' };+ }+ for (const [key, val] of Object.entries(scores)) {+ if (typeof val !== 'number' || Number.isNaN(val)) {+ return { pass: false, score: 0, reason: `score ${key} is not a number` };}if (val < 0 || val > 100) {- return { pass: false, score: 0, reason: `scores.${field} out of range: ${val}` };+ return { pass: false, score: 0, reason: `score ${key} out of range: ${val}` };}}- return { pass: true, score: 1, reason: 'All scores are numbers in range 0-100' };+ return { pass: true, score: 1, reason: 'SCORES line present, all values 0-100' };}-export function assertTopImprovementsCount(output: string): AssertionResult {- let parsed: Record<string, unknown>;- try {- parsed = JSON.parse(extractJson(output));- } catch {- return { pass: false, score: 0, reason: 'Output is not valid JSON' };+export function assertNotJson(output: string): AssertionResult {+ const trimmed = stripThinking(output).trim();+ if (trimmed.startsWith('{') || trimmed.startsWith('[')) {+ return { pass: false, score: 0, reason: 'Output looks like raw JSON; expected markdown' };}-- const items = parsed?.topImprovements;- if (!Array.isArray(items)) {- return { pass: false, score: 0, reason: 'topImprovements is not an array' };- }- if (items.length < 3 || items.length > 5) {- return { pass: false, score: 0, reason: `topImprovements has ${items.length} items (expected 3-5)` };+ if (/^```(json)?\s*[{[]/i.test(trimmed)) {+ return { pass: false, score: 0, reason: 'Output is wrapped in a JSON code fence' };}- return { pass: true, score: 1, reason: `topImprovements has ${items.length} items` };+ return { pass: true, score: 1, reason: 'Output is markdown, not JSON' };}-export function assertSummaryNonEmpty(output: string): AssertionResult {- let parsed: Record<string, unknown>;- try {- parsed = JSON.parse(extractJson(output));- } catch {- return { pass: false, score: 0, reason: 'Output is not valid JSON' };+export function assertMarkdownBodyNonEmpty(output: string): AssertionResult {+ const { body } = parseReview(output);+ if (body.length < 50) {+ return { pass: false, score: 0, reason: `Markdown body too short (${body.length} chars)` };}-- const summary = parsed?.summary;- if (typeof summary !== 'string' || summary.trim().length === 0) {- return { pass: false, score: 0, reason: 'summary is empty or not a string' };- }- return { pass: true, score: 1, reason: 'summary is a non-empty string' };+ return { pass: true, score: 1, reason: 'Markdown review body present' };}-export function assertSectionFeedbackStructure(output: string): AssertionResult {- let parsed: Record<string, unknown>;- try {- parsed = JSON.parse(extractJson(output));- } catch {- return { pass: false, score: 0, reason: 'Output is not valid JSON' };- }-- // Accept 'sections' as an alias — models occasionally use it instead of 'sectionFeedback'- const sf = parsed?.sectionFeedback ?? parsed?.sections;- if (!sf || (typeof sf !== 'object')) {- return { pass: false, score: 0, reason: 'sectionFeedback is missing or not an object' };- }-- // Normalize: Zod schema expects array [{section, status, feedback}];- // without schema enforcement models often return a dict {SectionName: {status, feedback}}- type SFItem = { section: string; status: unknown; feedback: unknown };- const items: SFItem[] = Array.isArray(sf)- ? (sf as Record<string, unknown>[]).map(item => ({ section: item.section as string, status: item.status, feedback: item.feedback }))- : Object.entries(sf as Record<string, Record<string, unknown>>).map(([k, v]) => ({ section: k, status: v.status, feedback: v.feedback }));-- if (items.length === 0) {- return { pass: false, score: 0, reason: 'sectionFeedback is empty' };- }-- const validStatuses = new Set(['good', 'needsWork', 'missing']);- for (let i = 0; i < items.length; i++) {- const { section, status, feedback } = items[i];- if (typeof section !== 'string' || section.trim().length === 0) {- return { pass: false, score: 0, reason: `sectionFeedback[${i}].section is empty or missing` };- }- if (!validStatuses.has(status as string)) {- return { pass: false, score: 0, reason: `sectionFeedback[${i}].status is invalid: "${status}"` };- }- if (typeof feedback !== 'string' || feedback.trim().length === 0) {- return { pass: false, score: 0, reason: `sectionFeedback[${i}].feedback is empty or missing` };- }- }- return { pass: true, score: 1, reason: `sectionFeedback has ${items.length} valid items` };+export function assertRequiredSections(output: string): AssertionResult {+ const { body } = parseReview(output);+ // Core sections that always apply (ATS / Grammar are conditional, so not+ // required here). Headings matched leniently against "## ..." lines.+ const required: { label: string; re: RegExp }[] = [+ { label: 'Summary', re: /^##\s+summary\b/im },+ { label: 'Top Improvements', re: /^##\s+top\s+improvements?/im },+ { label: 'Achievements', re: /^##\s+achievements?/im },+ { label: 'Keywords', re: /^##\s+keywords?/im },+ { label: 'Action Verbs', re: /^##\s+action\s+verbs?/im },+ { label: 'Section Feedback', re: /^##\s+section\s+feedback/im },+ ];+ const missing = required.filter((s) => !s.re.test(body)).map((s) => s.label);+ if (missing.length > 0) {+ return {+ pass: false,+ score: 1 - missing.length / required.length,+ reason: `Missing section headings: ${missing.join(', ')}`,+ };+ }+ return { pass: true, score: 1, reason: 'All core ## sections present' };}diff --git a/evals/resume-review/promptfooconfig.yaml b/evals/resume-review/promptfooconfig.yamlindex 87d3b1b..585b25e 100644--- a/evals/resume-review/promptfooconfig.yaml+++ b/evals/resume-review/promptfooconfig.yaml@@ -7,8 +7,6 @@ providers:apiKeyEnvar: DEEPSEEK_API_KEYtemperature: 0max_tokens: 8192- response_format:- type: json_objectprompts:- file://./prompt.ts@@ -19,31 +17,23 @@ tests:resumeText: file://./fixtures/strong-senior.txtassert:- type: javascript- value: file://./assertions.ts:assertValidJson+ value: file://./assertions.ts:assertNotJson- type: javascript- value: file://./assertions.ts:assertRequiredFields+ value: file://./assertions.ts:assertScoresLine- type: javascript- value: file://./assertions.ts:assertScoresInRange+ value: file://./assertions.ts:assertMarkdownBodyNonEmpty- type: javascript- value: file://./assertions.ts:assertTopImprovementsCount- - type: javascript- value: file://./assertions.ts:assertSummaryNonEmpty- - type: javascript- value: file://./assertions.ts:assertSectionFeedbackStructure+ value: file://./assertions.ts:assertRequiredSections- description: Weak/sparse resume - structural validationvars:resumeText: file://./fixtures/weak-sparse.txtassert:- type: javascript- value: file://./assertions.ts:assertValidJson- - type: javascript- value: file://./assertions.ts:assertRequiredFields- - type: javascript- value: file://./assertions.ts:assertScoresInRange+ value: file://./assertions.ts:assertNotJson- type: javascript- value: file://./assertions.ts:assertTopImprovementsCount+ value: file://./assertions.ts:assertScoresLine- type: javascript- value: file://./assertions.ts:assertSummaryNonEmpty+ value: file://./assertions.ts:assertMarkdownBodyNonEmpty- type: javascript- value: file://./assertions.ts:assertSectionFeedbackStructure+ value: file://./assertions.ts:assertRequiredSectionsdiff --git a/evals/resume-review/schemas/resume-review.json b/evals/resume-review/schemas/resume-review.jsondeleted file mode 100644index 123c98d..0000000--- a/evals/resume-review/schemas/resume-review.json+++ /dev/null@@ -1,7 +0,0 @@-{- "$ref": "#/definitions/ResumeReviewResponse",- "definitions": {- "ResumeReviewResponse": {}- },- "$schema": "http://json-schema.org/draft-07/schema#"-}\ No newline at end of filediff --git a/package-lock.json b/package-lock.jsonindex c627930..df14304 100644--- a/package-lock.json+++ b/package-lock.json@@ -49,6 +49,7 @@"date-fns": "^3.6.0","lucide-react": "^0.469.0","mammoth": "^1.12.0",+ "markdown-it": "^14.1.0","next": "^15.5.18","next-auth": "^5.0.0-beta.30","next-themes": "^0.4.4",@@ -72,6 +73,7 @@"@testing-library/react": "^16.1.0","@testing-library/user-event": "^14.5.2","@types/bcryptjs": "^2.4.6",+ "@types/markdown-it": "^14.1.2","@types/node": "^22.10.2","@types/node-cron": "^3.0.11","@types/papaparse": "^5.3.15",@@ -89,8 +91,7 @@"ts-node": "^10.9.2","tsx": "^4.22.4","typescript": "^5",- "vitest": "^4.1.3",- "zod-to-json-schema": "^3.25.2"+ "vitest": "^4.1.3"}},"node_modules/@adaline/anthropic": {diff --git a/package.json b/package.jsonindex d609097..87a58ad 100644--- a/package.json+++ b/package.json@@ -11,8 +11,7 @@"test:e2e": "npx playwright test","test:watch": "vitest","eval:resume-review": "promptfoo eval --config evals/resume-review/promptfooconfig.yaml",- "eval:view": "promptfoo view",- "eval:schema": "tsx scripts/generate-eval-schema.ts"+ "eval:view": "promptfoo view"},"dependencies": {"@ai-sdk/deepseek": "^2.0.4",@@ -56,6 +55,7 @@"date-fns": "^3.6.0","lucide-react": "^0.469.0","mammoth": "^1.12.0",+ "markdown-it": "^14.1.0","next": "^15.5.18","next-auth": "^5.0.0-beta.30","next-themes": "^0.4.4",@@ -79,6 +79,7 @@"@testing-library/react": "^16.1.0","@testing-library/user-event": "^14.5.2","@types/bcryptjs": "^2.4.6",+ "@types/markdown-it": "^14.1.2","@types/node": "^22.10.2","@types/node-cron": "^3.0.11","@types/papaparse": "^5.3.15",@@ -96,8 +97,7 @@"ts-node": "^10.9.2","tsx": "^4.22.4","typescript": "^5",- "vitest": "^4.1.3",- "zod-to-json-schema": "^3.25.2"+ "vitest": "^4.1.3"},"overrides": {"@types/react": "19.2.14",diff --git a/scripts/generate-eval-schema.ts b/scripts/generate-eval-schema.tsdeleted file mode 100644index a29db10..0000000--- a/scripts/generate-eval-schema.ts+++ /dev/null@@ -1,14 +0,0 @@-import { zodToJsonSchema } from 'zod-to-json-schema';-import { writeFileSync, mkdirSync } from 'fs';-import { join, dirname } from 'path';-import { ResumeReviewSchema } from '../src/models/ai.schemas';--const schema = zodToJsonSchema(ResumeReviewSchema, {- name: 'ResumeReviewResponse',- $refStrategy: 'none',-});--const out = join(__dirname, '../evals/resume-review/schemas/resume-review.json');-mkdirSync(dirname(out), { recursive: true });-writeFileSync(out, JSON.stringify(schema, null, 2));-console.log('Written:', out);diff --git a/src/app/api/ai/resume/review/route.ts b/src/app/api/ai/resume/review/route.tsindex 860e572..93e69c9 100644--- a/src/app/api/ai/resume/review/route.ts+++ b/src/app/api/ai/resume/review/route.ts@@ -2,18 +2,18 @@ import "server-only";import { auth } from "@/auth";import { NextRequest, NextResponse } from "next/server";-import { streamText, Output } from "ai";+import { streamText } from "ai";import { getModel } from "@/lib/ai/providers";import { checkRateLimit } from "@/lib/ai/rate-limiter";import { TEMPERATURES } from "@/lib/ai/config";import {- ResumeReviewSchema,RESUME_REVIEW_SYSTEM_PROMPT,buildResumeReviewPrompt,AIUnavailableError,preprocessResume,} from "@/lib/ai";-import { Resume } from "@/models/profile.model";+import { APP_CONSTANTS } from "@/lib/constants";+import { getResumeById } from "@/actions/profile.actions";import { AiModel } from "@/models/ai.model";/**@@ -25,7 +25,7 @@ export const POST = async (req: NextRequest) => {const userId = session?.user?.id;if (!session || !userId) {- return NextResponse.json({ message: "Not Authenticated" }, { status: 401 });+ return NextResponse.json({ error: "Not Authenticated" }, { status: 401 });}// Rate limiting@@ -41,20 +41,27 @@ export const POST = async (req: NextRequest) => {);}- const { selectedModel, resume } = (await req.json()) as {+ const { selectedModel, resumeId } = (await req.json()) as {selectedModel: AiModel;- resume: Resume;+ resumeId: string;};- if (!resume || !selectedModel) {+ if (!resumeId || !selectedModel) {return NextResponse.json(- { error: "Resume and model selection required" },+ { error: "resumeId and model selection required" },{ status: 400 },);}+ // Load the resume server-side, scoped to the caller (ownership check) — never+ // trust resume content sent by the client.+ const resumeResult = await getResumeById(resumeId);+ if (!resumeResult?.success || !resumeResult.data) {+ return NextResponse.json({ error: "Resume not found" }, { status: 404 });+ }+try {- const preprocessResult = await preprocessResume(resume);+ const preprocessResult = await preprocessResume(resumeResult.data);if (!preprocessResult.success) {return NextResponse.json({@@ -72,15 +79,33 @@ export const POST = async (req: NextRequest) => {userId,);- // Single comprehensive LLM call+ const controller = new AbortController();+ const timer = setTimeout(+ () => controller.abort(),+ APP_CONSTANTS.AI_RESUME_REVIEW_TIMEOUT_MS,+ );++ // Free-form markdown output (scores line + markdown body). Plain text means+ // toTextStreamResponse() is correct here — no structured-output parsing, so+ // none of the json/tool-call empty-textStream pitfalls apply.const result = streamText({model,- output: Output.object({- schema: ResumeReviewSchema,- }),system: RESUME_REVIEW_SYSTEM_PROMPT,prompt: buildResumeReviewPrompt(normalizedText),temperature: TEMPERATURES.FEEDBACK,+ abortSignal: controller.signal,+ providerOptions: {+ ollama: { options: { num_ctx: APP_CONSTANTS.AI_OLLAMA_NUM_CTX } },+ },+ onFinish: ({ finishReason, usage }) => {+ clearTimeout(timer);+ // "length" => hit num_ctx/output limit; "stop" => model completed.+ console.info(`Resume review finished: reason=${finishReason}`, usage);+ },+ onError: ({ error }) => {+ clearTimeout(timer);+ console.error("Resume review stream error:", error);+ },});return result.toTextStreamResponse();diff --git a/src/components/profile/AiResumeReviewResponseContent.tsx b/src/components/profile/AiResumeReviewResponseContent.tsxindex f7c11ee..02eba56 100644--- a/src/components/profile/AiResumeReviewResponseContent.tsx+++ b/src/components/profile/AiResumeReviewResponseContent.tsx@@ -1,30 +1,22 @@+"use client";++import { useMemo } from "react";+import MarkdownIt from "markdown-it";import { RadialChartComponent } from "../RadialChartSvg";-import { SheetDescription } from "../ui/sheet";-import { ResumeReviewResponse } from "@/models/ai.model";-import type { DeepPartial } from "ai";-import { Badge } from "../ui/badge";-import {- Accordion,- AccordionContent,- AccordionItem,- AccordionTrigger,-} from "../ui/accordion";-import { CheckCircle, XCircle, AlertTriangle, ArrowRight } from "lucide-react";+import { TipTapContentViewer } from "../TipTapContentViewer";+import { ResumeScores } from "@/models/ai.schemas";-// SECTION COMPONENTS+// html:false escapes any raw HTML in the model output; TipTapContentViewer+// further strips unrecognized tags, so the rendered review is safe.+const md = new MarkdownIt({ html: false, linkify: false, breaks: true });-const ScoresSection = ({- scores,-}: {- scores?: DeepPartial<ResumeReviewResponse["scores"]>;-}) => {+const ScoresSection = ({ scores }: { scores?: ResumeScores }) => {if (!scores) return null;- const scoreItems = [- { label: "Overall", value: scores.overall, color: "bg-blue-500" },- { label: "Impact", value: scores.impact, color: "bg-green-500" },- { label: "Clarity", value: scores.clarity, color: "bg-purple-500" },- { label: "ATS", value: scores.atsCompatibility, color: "bg-orange-500" },+ const items = [+ { label: "Impact", value: scores.impact },+ { label: "Clarity", value: scores.clarity },+ { label: "ATS", value: scores.atsCompatibility },];return (@@ -34,492 +26,40 @@ const ScoresSection = ({<RadialChartComponent score={scores.overall} /></div>)}- <div className="grid grid-cols-4 gap-2 mt-[-10px]">- {scoreItems.map(- (item) =>- item.value !== undefined && (- <div key={item.label} className="text-center">- <div className="text-xs text-muted-foreground">- {item.label}- </div>- <div className="font-semibold">{item.value}</div>- </div>- ),- )}+ <div className="grid grid-cols-3 gap-2 mt-[-10px]">+ {items.map((item) => (+ <div key={item.label} className="text-center">+ <div className="text-xs text-muted-foreground">{item.label}</div>+ <div className="font-semibold">{item.value}</div>+ </div>+ ))}</div></div>);};-const SummarySection = ({ summary }: { summary?: string }) => {- if (!summary) return null;- return (- <div className="pt-4">- <h2 className="font-semibold text-sm">Summary</h2>- <SheetDescription className="mt-1">{summary}</SheetDescription>- </div>- );-};--const AchievementsSection = ({- achievements,-}: {- achievements?: DeepPartial<ResumeReviewResponse["achievements"]>;-}) => {- if (!achievements) return null;- const { strong, weak, missingMetrics } = achievements;-- const hasContent =- (strong && strong.length > 0) ||- (weak && weak.length > 0) ||- (missingMetrics && missingMetrics.length > 0);-- if (!hasContent) return null;-- return (- <AccordionItem value="achievements">- <AccordionTrigger className="text-sm font-semibold">- Achievements- </AccordionTrigger>- <AccordionContent>- {strong && strong.length > 0 && (- <div className="mb-3">- <div className="flex items-center gap-1 text-green-600 text-xs font-medium mb-1">- <CheckCircle className="h-3 w-3" />- Strong Achievements- </div>- <ul className="text-sm text-muted-foreground space-y-1">- {strong.map((item, i) => item && <li key={i}>- {item}</li>)}- </ul>- </div>- )}- {weak && weak.length > 0 && (- <div className="mb-3">- <div className="flex items-center gap-1 text-amber-600 text-xs font-medium mb-1">- <AlertTriangle className="h-3 w-3" />- Needs Quantification- </div>- <ul className="text-sm text-muted-foreground space-y-1">- {weak.map((item, i) => item && <li key={i}>- {item}</li>)}- </ul>- </div>- )}- {missingMetrics && missingMetrics.length > 0 && (- <div>- <div className="text-xs font-medium text-muted-foreground mb-1">- Suggested Metrics to Add- </div>- <ul className="text-sm text-muted-foreground space-y-1">- {missingMetrics.map(- (item, i) => item && <li key={i}>- {item}</li>,- )}- </ul>- </div>- )}- </AccordionContent>- </AccordionItem>- );-};--const KeywordsSection = ({- keywords,-}: {- keywords?: DeepPartial<ResumeReviewResponse["keywords"]>;-}) => {- if (!keywords) return null;- const { found, missing, overused } = keywords;-- const hasContent =- (found && found.length > 0) ||- (missing && missing.length > 0) ||- (overused && overused.length > 0);-- if (!hasContent) return null;-- return (- <AccordionItem value="keywords">- <AccordionTrigger className="text-sm font-semibold">- Keywords- </AccordionTrigger>- <AccordionContent>- {found && found.length > 0 && (- <div className="mb-3">- <div className="text-xs font-medium text-green-600 mb-1">Found</div>- <div className="flex flex-wrap gap-1">- {found.map(- (kw, i) =>- kw && (- <Badge key={i} variant="secondary" className="text-xs">- {kw}- </Badge>- ),- )}- </div>- </div>- )}- {missing && missing.length > 0 && (- <div className="mb-3">- <div className="text-xs font-medium text-amber-600 mb-1">- Consider Adding- </div>- <div className="flex flex-wrap gap-1">- {missing.map(- (kw, i) =>- kw && (- <Badge key={i} variant="outline" className="text-xs">- {kw}- </Badge>- ),- )}- </div>- </div>- )}- {overused && overused.length > 0 && (- <div>- <div className="text-xs font-medium text-red-600 mb-1">- Overused- </div>- <div className="flex flex-wrap gap-1">- {overused.map(- (kw, i) =>- kw && (- <Badge key={i} variant="destructive" className="text-xs">- {kw}- </Badge>- ),- )}- </div>- </div>- )}- </AccordionContent>- </AccordionItem>- );-};--const ActionVerbsSection = ({- actionVerbs,-}: {- actionVerbs?: DeepPartial<ResumeReviewResponse["actionVerbs"]>;-}) => {- if (!actionVerbs) return null;- const { strong, weak, suggestions } = actionVerbs;-- const hasContent =- (strong && strong.length > 0) ||- (weak && weak.length > 0) ||- (suggestions && suggestions.length > 0);-- if (!hasContent) return null;-- return (- <AccordionItem value="verbs">- <AccordionTrigger className="text-sm font-semibold">- Action Verbs- </AccordionTrigger>- <AccordionContent>- {strong && strong.length > 0 && (- <div className="mb-3">- <div className="text-xs font-medium text-green-600 mb-1">- Strong Verbs- </div>- <div className="flex flex-wrap gap-1">- {strong.map(- (v, i) =>- v && (- <Badge key={i} variant="secondary" className="text-xs">- {v}- </Badge>- ),- )}- </div>- </div>- )}- {weak && weak.length > 0 && (- <div className="mb-3">- <div className="text-xs font-medium text-amber-600 mb-1">- Weak Verbs- </div>- <div className="flex flex-wrap gap-1">- {weak.map(- (v, i) =>- v && (- <Badge key={i} variant="outline" className="text-xs">- {v}- </Badge>- ),- )}- </div>- </div>- )}- {suggestions && suggestions.length > 0 && (- <div>- <div className="text-xs font-medium text-muted-foreground mb-1">- Suggestions- </div>- <ul className="text-sm text-muted-foreground space-y-1">- {suggestions.map(- (s, i) =>- s?.replace &&- s?.with && (- <li key={i} className="flex items-center gap-1">- <span className="line-through text-red-500">- {s.replace}- </span>- <ArrowRight className="h-3 w-3" />- <span className="text-green-600">{s.with}</span>- </li>- ),- )}- </ul>- </div>- )}- </AccordionContent>- </AccordionItem>- );-};--const SectionFeedbackSection = ({- sectionFeedback,-}: {- sectionFeedback?: DeepPartial<ResumeReviewResponse["sectionFeedback"]>;-}) => {- if (!sectionFeedback || sectionFeedback.length === 0)- return null;-- const getStatusIcon = (status?: string) => {- switch (status) {- case "good":- return <CheckCircle className="h-3 w-3 text-green-600" />;- case "needsWork":- return <AlertTriangle className="h-3 w-3 text-amber-600" />;- case "missing":- return <XCircle className="h-3 w-3 text-red-600" />;- default:- return null;- }- };-- return (- <AccordionItem value="sections">- <AccordionTrigger className="text-sm font-semibold">- Section Feedback- </AccordionTrigger>- <AccordionContent>- <div className="space-y-2">- {sectionFeedback.map(- (item, i) =>- item && (- <div key={item.section ?? i} className="border-b pb-2 last:border-0">- <div className="flex items-center gap-1 font-medium text-sm">- {getStatusIcon(item.status)}- {item.section}- </div>- {item.feedback && (- <p className="text-xs text-muted-foreground mt-1">- {item.feedback}- </p>- )}- </div>- ),- )}- </div>- </AccordionContent>- </AccordionItem>- );-};--const ATSIssuesSection = ({- atsIssues,-}: {- atsIssues?: (string | undefined)[];-}) => {- if (!atsIssues || atsIssues.length === 0) return null;-- return (- <AccordionItem value="ats">- <AccordionTrigger className="text-sm font-semibold">- ATS Issues- </AccordionTrigger>- <AccordionContent>- <ul className="text-sm text-muted-foreground space-y-1">- {atsIssues.map(- (issue, i) =>- issue && (- <li key={i} className="flex items-start gap-1">- <XCircle className="h-3 w-3 text-red-600 mt-0.5 flex-shrink-0" />- {issue}- </li>- ),- )}- </ul>- </AccordionContent>- </AccordionItem>- );-};--const TopImprovementsSection = ({- topImprovements,-}: {- topImprovements?: DeepPartial<ResumeReviewResponse["topImprovements"]>;-}) => {- if (!topImprovements || topImprovements.length === 0) return null;-- return (- <AccordionItem value="improvements">- <AccordionTrigger className="text-sm font-semibold">- Top Improvements- </AccordionTrigger>- <AccordionContent>- <div className="space-y-3">- {topImprovements.map(- (item, i) =>- item && (- <div key={i} className="border-l-2 border-primary pl-3">- <div className="flex items-center gap-2">- <Badge variant="outline" className="text-xs">- #{item.priority}- </Badge>- <span className="text-sm font-medium">{item.issue}</span>- </div>- {item.fix && (- <p className="text-xs text-muted-foreground mt-1">- {item.fix}- </p>- )}- </div>- ),- )}- </div>- </AccordionContent>- </AccordionItem>- );-};--const GrammarSection = ({- grammarAndSpelling,-}: {- grammarAndSpelling?: DeepPartial<ResumeReviewResponse["grammarAndSpelling"]>;-}) => {- if (!grammarAndSpelling) return null;- const { errors, punctuationIssues, consistencyIssues } = grammarAndSpelling;-- const hasContent =- (errors && errors.length > 0) ||- (punctuationIssues && punctuationIssues.length > 0) ||- (consistencyIssues && consistencyIssues.length > 0);-- if (!hasContent) return null;-- return (- <AccordionItem value="grammar">- <AccordionTrigger className="text-sm font-semibold">- Grammar & Spelling- </AccordionTrigger>- <AccordionContent>- {errors && errors.length > 0 && (- <div className="mb-3">- <div className="text-xs font-medium text-red-600 mb-1">Errors</div>- <div className="space-y-2">- {errors.map(- (err, i) =>- err && (- <div key={i} className="text-sm">- <span className="line-through text-red-500">- {err.text}- </span>- {err.correction && (- <>- <ArrowRight className="h-3 w-3 inline mx-1" />- <span className="text-green-600">- {err.correction}- </span>- </>- )}- {err.issue && (- <p className="text-xs text-muted-foreground">- {err.issue}- </p>- )}- </div>- ),- )}- </div>- </div>- )}- {punctuationIssues && punctuationIssues.length > 0 && (- <div className="mb-3">- <div className="text-xs font-medium text-amber-600 mb-1">- Punctuation- </div>- <ul className="text-sm text-muted-foreground space-y-1">- {punctuationIssues.map((p, i) => p && <li key={i}>- {p}</li>)}- </ul>- </div>- )}- {consistencyIssues && consistencyIssues.length > 0 && (- <div>- <div className="text-xs font-medium text-amber-600 mb-1">- Consistency- </div>- <ul className="text-sm text-muted-foreground space-y-1">- {consistencyIssues.map((c, i) => c && <li key={i}>- {c}</li>)}- </ul>- </div>- )}- </AccordionContent>- </AccordionItem>- );-};--// MAIN COMPONENT-export const AiResumeReviewResponseContent = ({- content,+ scores,+ body,isStreaming,}: {- content: DeepPartial<ResumeReviewResponse> | null | undefined;+ scores?: ResumeScores;+ body?: string;isStreaming?: boolean;}) => {- if (!content) return null;+ const html = useMemo(() => (body ? md.render(body) : ""), [body]);- const {- scores,- summary,- achievements,- keywords,- actionVerbs,- sectionFeedback,- atsIssues,- topImprovements,- grammarAndSpelling,- } = content;+ if (!scores && !html) return null;return (<div className="space-y-2"><ScoresSection scores={scores} />- <SummarySection summary={summary} />- <Accordion- type="multiple"- className="w-full"- defaultValue={[- "improvements",- "achievements",- "keywords",- "verbs",- "sections",- "ats",- "grammar",- ]}- >- <TopImprovementsSection topImprovements={topImprovements} />- <AchievementsSection achievements={achievements} />- <KeywordsSection keywords={keywords} />- <ActionVerbsSection actionVerbs={actionVerbs} />- <SectionFeedbackSection sectionFeedback={sectionFeedback} />- <ATSIssuesSection atsIssues={atsIssues} />- <GrammarSection grammarAndSpelling={grammarAndSpelling} />- </Accordion>+ {html && (+ <div className="mt-4 text-sm leading-relaxed [&_p]:mt-2 [&_ul]:mt-2 [&_ol]:mt-2 [&_h2]:mt-4 [&_h2]:font-semibold">+ <TipTapContentViewer content={html} />+ </div>+ )}{isStreaming && (<div className="flex items-center gap-2 text-muted-foreground text-sm mt-4 animate-pulse">diff --git a/src/components/profile/AiResumeReviewSection.tsx b/src/components/profile/AiResumeReviewSection.tsxindex ce4760e..b306b20 100644--- a/src/components/profile/AiResumeReviewSection.tsx+++ b/src/components/profile/AiResumeReviewSection.tsx@@ -1,6 +1,5 @@"use client";-import { experimental_useObject as useObject } from "@ai-sdk/react";import { Info, Sparkles, CheckCircle, XCircle } from "lucide-react";import { Button } from "../ui/button";import {@@ -12,11 +11,15 @@ import {SheetTrigger,} from "../ui/sheet";import Loading from "../Loading";-import { useState, useEffect, useCallback } from "react";+import { useState, useEffect, useCallback, useRef } from "react";import { toast } from "../ui/use-toast";import { Resume } from "@/models/profile.model";import { AiModel, AiProvider, defaultModel } from "@/models/ai.model";import { AiResumeReviewResponseContent } from "./AiResumeReviewResponseContent";+import {+ streamResumeReview,+ type ResumeReviewResult,+} from "@/utils/streamResumeReview.utils";import {Tooltip,TooltipContent,@@ -24,7 +27,6 @@ import {TooltipTrigger,} from "../ui/tooltip";import { checkOllamaConnection } from "@/utils/ai.utils";-import { ResumeReviewSchema } from "@/models/ai.schemas";import { getUserSettings } from "@/actions/userSettings.actions";import { useSlowResponseWarning } from "@/hooks/useSlowResponseWarning";import { SlowResponseWarning } from "../common/SlowResponseWarning";@@ -78,20 +80,17 @@ const AiResumeReviewSection = ({ resume }: AiSectionProps) => {fetchSettings();}, [aISectionOpen, checkConnectionStatus]);- // Standard single-agent mode- const { object, submit, isLoading, stop } = useObject({- api: "/api/ai/resume/review",- schema: ResumeReviewSchema,- onError: (err) => {- toast({- variant: "destructive",- title: "Error!",- description: err.message || "Failed to get AI review",- });- },- });+ const [result, setResult] = useState<ResumeReviewResult | undefined>();+ const [isLoading, setIsLoading] = useState(false);+ const abortRef = useRef<AbortController | null>(null);++ const stop = useCallback(() => {+ abortRef.current?.abort();+ abortRef.current = null;+ setIsLoading(false);+ }, []);- const getResumeReview = () => {+ const getResumeReview = async () => {if (!resume || (resume.ResumeSections?.length ?? 0) < 2) {toast({variant: "destructive",@@ -102,7 +101,31 @@ const AiResumeReviewSection = ({ resume }: AiSectionProps) => {return;}- submit({ selectedModel, resume });+ const controller = new AbortController();+ abortRef.current = controller;+ setResult(undefined);+ setIsLoading(true);++ try {+ await streamResumeReview({+ resumeId: resume.id!,+ selectedModel,+ signal: controller.signal,+ onUpdate: setResult,+ });+ } catch (err) {+ // Aborting (e.g. closing the sheet) is expected — don't toast.+ if (controller.signal.aborted) return;+ toast({+ variant: "destructive",+ title: "Error!",+ description:+ err instanceof Error ? err.message : "Failed to get AI review",+ });+ } finally {+ if (abortRef.current === controller) abortRef.current = null;+ setIsLoading(false);+ }};const triggerSheetChange = (openState: boolean) => {@@ -113,8 +136,7 @@ const AiResumeReviewSection = ({ resume }: AiSectionProps) => {};// Check if we have any content to show- const hasContent =- object && (object.scores?.overall !== undefined || object.summary);+ const hasContent = !!(result && (result.scores || result.body));const showSlowWarning = useSlowResponseWarning(isLoading, !!hasContent);@@ -199,7 +221,8 @@ const AiResumeReviewSection = ({ resume }: AiSectionProps) => {</div>) : (<AiResumeReviewResponseContent- content={object}+ scores={result?.scores}+ body={result?.body}isStreaming={isLoading}/>)}diff --git a/src/lib/ai/index.ts b/src/lib/ai/index.tsindex 1711af2..173432d 100644--- a/src/lib/ai/index.ts+++ b/src/lib/ai/index.ts@@ -1,8 +1,6 @@export { getModel, type ProviderType } from "./providers";export {- ResumeReviewSchema,JobMatchSchema,- type ResumeReviewResponse,type JobMatchResponse,} from "@/models/ai.schemas";diff --git a/src/lib/ai/prompts/resume-review/system.ts b/src/lib/ai/prompts/resume-review/system.tsindex 424fd0c..f32ca66 100644--- a/src/lib/ai/prompts/resume-review/system.ts+++ b/src/lib/ai/prompts/resume-review/system.ts@@ -1,6 +1,6 @@/*** Resume Review System Prompt- * Comprehensive single-call analysis with structured JSON output+ * Free-form markdown review with a small machine-readable scores header.*/export const RESUME_REVIEW_SYSTEM_PROMPT = `You are an expert resume reviewer with 15 years of recruiting experience across multiple industries.@@ -13,9 +13,8 @@ You combine ATS expertise with human recruiter psychology to provide accurate, a3. **Assess Quality**: Formatting, ATS compatibility, clarity, grammar4. **Provide Specific Feedback**: Reference actual text from the resume-## SCORING GUIDELINES+## SCORING GUIDELINES (0-100)-**Overall Score (0-100)**:- 85-100: Exceptional - Top 5%, ready for FAANG/top-tier- 70-84: Above Average - Strong, minor polish needed- 55-69: Average - Serviceable but doesn't stand out@@ -23,9 +22,9 @@ You combine ATS expertise with human recruiter psychology to provide accurate, a- 25-39: Weak - Fundamental issues- <25: Critical - Only for nearly blank resumes-**Impact Score**: Based on quantified achievements, measurable results, business value-**Clarity Score**: Readability, organization, STAR format, professional writing-**ATS Compatibility**: Standard formatting, keywords, no tables/graphics, proper sections+- **Impact**: quantified achievements, measurable results, business value+- **Clarity**: readability, organization, STAR format, professional writing+- **ATS**: standard formatting, keywords, no tables/graphics, proper sections## COGNITIVE RULES@@ -39,8 +38,39 @@ You combine ATS expertise with human recruiter psychology to provide accurate, a❌ DON'T: Give the same feedback to every resume❌ DON'T: Conflate "different from ideal" with "wrong"-## OUTPUT FORMAT+## OUTPUT FORMAT (FOLLOW EXACTLY)-Return a structured JSON response with all required fields.-Be specific and reference actual content from the resume in your feedback.-Every suggestion should be actionable with concrete examples.`;+The VERY FIRST line of your response MUST be the scores line, in this exact format and nothing else:++SCORES: overall=<0-100> impact=<0-100> clarity=<0-100> ats=<0-100>++Then a blank line, then the full review in GitHub-flavored Markdown using these+"##" sections in this order (omit a section only if it truly does not apply):++## Summary+2-3 sentences: overall impression, top strength, most impactful improvement.++## Top Improvements+A numbered list of the 3-5 highest-impact changes, each naming the issue and the concrete fix.++## Achievements+What is quantified well (quote it), and which vague statements need metrics (quote them).++## Keywords+Relevant keywords present; important keywords missing; any overused buzzwords.++## Action Verbs+Strong verbs used; weak phrasing ("Responsible for", "Helped with") with stronger replacements.++## Section Feedback+A short assessment per resume section (Summary, Experience, Skills, Education, Certifications, etc.).++## ATS Compatibility+Only real issues found (tables, graphics, unusual fonts, missing standard sections). If none, say so.++## Grammar & Spelling+Quote exact text with errors and give corrections; note tense/format inconsistencies.++Do NOT output JSON. Do NOT wrap the whole response in code fences. Reference+actual content from the resume. Every suggestion must be actionable with+concrete examples.`;diff --git a/src/lib/ai/prompts/resume-review/user.ts b/src/lib/ai/prompts/resume-review/user.tsindex 34c4be2..787372b 100644--- a/src/lib/ai/prompts/resume-review/user.ts+++ b/src/lib/ai/prompts/resume-review/user.ts@@ -1,66 +1,14 @@/*** Resume Review User Prompt- * Constructs the user prompt for comprehensive resume analysis+ * Constructs the user prompt for the markdown resume review.*/-/**- * Build user prompt for comprehensive resume review- */export function buildResumeReviewPrompt(resumeText: string): string {- return `Analyze this resume thoroughly and return a structured JSON response.--## RESUME TO ANALYZE:+ return `Review the following resume.+<resume>${resumeText}+</resume>-## ANALYSIS INSTRUCTIONS:--1. **Scores** - Assess the resume on multiple dimensions:- - overall: Overall quality score (0-100)- - impact: Achievement impact and measurable results- - clarity: Organization, readability, professional writing- - atsCompatibility: ATS-friendliness, proper formatting--2. **Achievements** - Categorize achievements found:- - strong: Quote achievements with numbers/metrics- - weak: Quote vague statements needing quantification- - missingMetrics: Suggest specific metrics to add--3. **Keywords** - Analyze keyword presence:- - found: Technical skills, tools, methodologies present- - missing: Important keywords for the role that should be added- - overused: Buzzwords or clichés used excessively--4. **Action Verbs** - Assess language strength:- - strong: Powerful verbs like Led, Architected, Delivered- - weak: Weak verbs like "Responsible for", "Helped with"- - suggestions: Specific replacement recommendations--5. **Section Feedback** - Evaluate each resume section:- - Key by section name (Summary, Experience, Skills, Education, Certifications, etc.)- - Provide status (good/needsWork/missing) and specific feedback- - For Certifications/Licenses: assess relevance to the target role and whether any key certs are missing--6. **ATS Issues** - Only list issues that ACTUALLY EXIST in this resume:- - Check for: tables, graphics, unusual fonts (only report if found)- - Check for: missing standard sections like Education, Skills (only report if truly missing)- - Check for: parsing problems (only report if present)- - If no ATS issues exist, return an empty array--7. **Top Improvements** - Prioritize 3-5 changes:- - Rank by impact (priority 1 = highest)- - Be specific about issue and fix--8. **Grammar and Spelling** - Note all errors:- - Quote exact text with errors- - Note punctuation inconsistencies- - Flag consistency issues (tense, formatting)--9. **Summary** - 2-3 sentence assessment:- - Mention overall score- - Highlight top strength- - Note most impactful improvement--Be specific and reference actual content from this resume.-Every suggestion must be actionable with concrete examples.`;+Start with the SCORES line, then the Markdown review, exactly as described in your instructions.`;}diff --git a/src/lib/constants.ts b/src/lib/constants.tsindex 96dd3e8..3bc6fbb 100644--- a/src/lib/constants.ts+++ b/src/lib/constants.ts@@ -36,6 +36,11 @@ export const APP_CONSTANTS = {// to produce; too low and the stream is cut mid-entry.AI_RESUME_IMPORT_TIMEOUT_MS: 180_000,+ // Resume review generation timeout. The review is a full markdown analysis+ // (scores line plus several sections), so a local model can take minutes;+ // too low cuts the stream mid-review.+ AI_RESUME_REVIEW_TIMEOUT_MS: 180_000,+// Automation manual run rate limitingAUTOMATION_MAX_MANUAL_RUNS_PER_HOUR: 5,AUTOMATION_RATE_LIMIT_WINDOW_MS: 60 * 60 * 1000, // 1 hourdiff --git a/src/models/ai.model.ts b/src/models/ai.model.tsindex bb11ba6..b033653 100644--- a/src/models/ai.model.ts+++ b/src/models/ai.model.ts@@ -1,6 +1,5 @@// Re-export types from schemasexport type {- ResumeReviewResponse,JobMatchResponse,RequirementMet,RequirementMissing,@@ -10,7 +9,7 @@ export type {KeywordsAnalysis,TailoringTip,} from "./ai.schemas";-export { ResumeReviewSchema, JobMatchSchema } from "./ai.schemas";+export { JobMatchSchema } from "./ai.schemas";// AI MODELdiff --git a/src/models/ai.schemas.ts b/src/models/ai.schemas.tsindex 4469e42..f3888c8 100644--- a/src/models/ai.schemas.ts+++ b/src/models/ai.schemas.ts@@ -1,137 +1,17 @@import { z } from "zod";-// COMPREHENSIVE RESUME REVIEW SCHEMA-// Single LLM call with detailed structured output+// RESUME REVIEW SCORES SCHEMA+// The review body is free-form markdown; only the four scores are structured+// (they drive the radial chart and score grid).-const ScoresSchema = z.object({- overall: z- .number()- .min(0)- .max(100)- .describe("Overall resume score 0-100. Entry-level: 35-50, Mid: 45-65, Senior: 55-75, Exceptional: >75"),- impact: z- .number()- .min(0)- .max(100)- .describe("Impact score based on quantified achievements, measurable results, and business value demonstrated"),- clarity: z- .number()- .min(0)- .max(100)- .describe("Clarity score based on readability, organization, STAR format usage, and professional writing"),- atsCompatibility: z- .number()- .min(0)- .max(100)- .describe("ATS compatibility score based on formatting, keywords, standard sections, and parsability"),-});--const AchievementsSchema = z.object({- strong: z- .array(z.string())- .describe("List achievements with measurable impact (numbers, %, $). Quote actual text from resume."),- weak: z- .array(z.string())- .describe("List vague statements that need quantification. Quote actual text that lacks metrics."),- missingMetrics: z- .array(z.string())- .describe("Suggest specific metrics that could be added (e.g., 'Add team size to Managed project' or 'Quantify cost savings')"),-});--const KeywordsSchema = z.object({- found: z- .array(z.string())- .describe("Relevant industry/role keywords present in resume (technologies, tools, methodologies, domain terms)"),- missing: z- .array(z.string())- .describe("Important keywords that should be added based on typical role requirements"),- overused: z- .array(z.string())- .describe("Buzzwords or clichés used too frequently (e.g., 'synergy', 'results-driven', 'team player')"),-});--const VerbSuggestionSchema = z.object({- replace: z.string().describe("Weak verb to replace"),- with: z.string().describe("Stronger alternative verb"),-});--const ActionVerbsSchema = z.object({- strong: z- .array(z.string())- .describe("Powerful verbs used effectively (e.g., Led, Architected, Spearheaded, Delivered, Transformed)"),- weak: z- .array(z.string())- .describe("Weak verbs that should be replaced (e.g., 'Responsible for', 'Helped with', 'Worked on')"),- suggestions: z- .array(VerbSuggestionSchema)- .describe("Specific verb replacement suggestions with context"),-});--const SectionFeedbackItemSchema = z.object({- section: z.string().describe("Section name (e.g., 'Summary', 'Experience', 'Skills', 'Education')"),- status: z.enum(["good", "needsWork", "missing"]).describe("Section status assessment"),- feedback: z.string().describe("Specific actionable advice for this section"),-});--const ImprovementSchema = z.object({- priority: z.number().min(1).max(5).describe("Priority ranking 1-5, where 1 is highest priority"),- issue: z.string().describe("What's wrong - be specific"),- fix: z.string().describe("How to fix it - be actionable"),-});--const GrammarErrorSchema = z.object({- text: z.string().describe("Exact text containing the error"),- issue: z.string().describe("What's wrong with it"),- correction: z.string().describe("Corrected version"),-});--const GrammarAndSpellingSchema = z.object({- errors: z- .array(GrammarErrorSchema)- .describe("Grammar and spelling errors found"),- punctuationIssues: z- .array(z.string())- .describe("Punctuation issues (e.g., inconsistent periods, comma problems)"),- consistencyIssues: z- .array(z.string())- .describe("Consistency issues (e.g., tense shifts, formatting inconsistencies, date format variations)"),-});--/**- * Comprehensive Resume Review Schema- * Single LLM call returns all analysis in structured format- */-export const ResumeReviewSchema = z.object({- scores: ScoresSchema.describe("Multiple score dimensions for nuanced assessment"),- achievements: AchievementsSchema.describe("Achievement quality assessment with specific examples"),- keywords: KeywordsSchema.describe("Keyword relevance analysis"),- actionVerbs: ActionVerbsSchema.describe("Action verb strength analysis with suggestions"),- sectionFeedback: z- .array(SectionFeedbackItemSchema)- .describe("Section-by-section feedback for each resume section"),- atsIssues: z- .array(z.string())- .describe("Formatting or content issues that may cause ATS rejection (tables, graphics, unusual fonts, missing sections)"),- topImprovements: z- .array(ImprovementSchema)- .min(3)- .max(5)- .describe("Top 3-5 prioritized improvement suggestions"),- grammarAndSpelling: GrammarAndSpellingSchema.describe("Grammar, spelling, and consistency analysis"),- summary: z- .string()- .describe("2-3 sentence overall assessment mentioning overall score, top strength, and most impactful improvement area"),+export const ResumeScoresSchema = z.object({+ overall: z.number().min(0).max(100),+ impact: z.number().min(0).max(100),+ clarity: z.number().min(0).max(100),+ atsCompatibility: z.number().min(0).max(100),});-export type ResumeReviewResponse = z.infer<typeof ResumeReviewSchema>;-export type ResumeScores = z.infer<typeof ScoresSchema>;-export type ResumeAchievements = z.infer<typeof AchievementsSchema>;-export type ResumeKeywords = z.infer<typeof KeywordsSchema>;-export type ResumeActionVerbs = z.infer<typeof ActionVerbsSchema>;-export type SectionFeedback = z.infer<typeof SectionFeedbackItemSchema>;-export type ResumeImprovement = z.infer<typeof ImprovementSchema>;-export type GrammarError = z.infer<typeof GrammarErrorSchema>;-export type GrammarAndSpelling = z.infer<typeof GrammarAndSpellingSchema>;+export type ResumeScores = z.infer<typeof ResumeScoresSchema>;// JOB MATCH SCHEMA// Single LLM call for comprehensive job-resume matchingdiff --git a/src/utils/streamResumeReview.utils.ts b/src/utils/streamResumeReview.utils.tsnew file mode 100644index 0000000..e49bd05--- /dev/null+++ b/src/utils/streamResumeReview.utils.ts@@ -0,0 +1,111 @@+import { ResumeScores } from "@/models/ai.schemas";+import { AiModel } from "@/models/ai.model";++export type ResumeReviewResult = {+ scores?: ResumeScores;+ // Markdown review body (scores line and any <think> blocks stripped out).+ body: string;+};++type StreamResumeReviewArgs = {+ resumeId: string;+ selectedModel: AiModel;+ // Called on each chunk with the parsed-so-far result for progressive render.+ onUpdate?: (result: ResumeReviewResult) => void;+ signal?: AbortSignal;+};++// Match greedily on digits (not {1,3}) so an out-of-range value like 1000+// still parses — clamp() below brings it back into 0-100 rather than failing+// the whole line and dropping every score.+const SCORES_RE =+ /SCORES:\s*overall=(\d+)\s+impact=(\d+)\s+clarity=(\d+)\s+ats=(\d+)/i;++// Reasoning models (e.g. qwen3 family) emit <think>...</think> in the text+// stream. Drop complete blocks, and drop an unterminated trailing block so a+// half-finished thought never flashes into the rendered review.+function stripThinking(text: string): string {+ let out = text.replace(/<think>[\s\S]*?<\/think>/gi, "");+ const openIdx = out.search(/<think>/i);+ if (openIdx !== -1) out = out.slice(0, openIdx);+ return out;+}++function parse(raw: string): ResumeReviewResult {+ const text = stripThinking(raw);+ const match = text.match(SCORES_RE);++ let scores: ResumeScores | undefined;+ if (match) {+ const clamp = (n: number) => Math.max(0, Math.min(100, n));+ scores = {+ overall: clamp(Number(match[1])),+ impact: clamp(Number(match[2])),+ clarity: clamp(Number(match[3])),+ atsCompatibility: clamp(Number(match[4])),+ };+ }++ // Remove the scores line from the body. Before it has fully streamed, also+ // hide a leading partial "SCORES:" line so it doesn't flash in the markdown.+ let body = match+ ? text.replace(match[0], "")+ : text.replace(/^\s*SCORES:[^\n]*(\n|$)/i, "");+ body = body.replace(/^\s+/, "");++ return { scores, body };+}++// Consumes the plain-text stream from /api/ai/resume/review and returns the+// parsed scores + markdown body. Salvages whatever arrived if the stream ends+// abnormally.+export async function streamResumeReview({+ resumeId,+ selectedModel,+ onUpdate,+ signal,+}: StreamResumeReviewArgs): Promise<ResumeReviewResult> {+ const res = await fetch("/api/ai/resume/review", {+ method: "POST",+ headers: { "Content-Type": "application/json" },+ body: JSON.stringify({ selectedModel, resumeId }),+ signal,+ });++ // Pre-generation failures (auth, rate limit, preprocessing) still return JSON.+ if (!res.ok || !res.body) {+ let message = "Failed to get AI review.";+ try {+ const err = await res.json();+ if (err?.error) message = err.error;+ } catch {+ // Non-JSON error body — keep the default message.+ }+ throw new Error(message);+ }++ const reader = res.body.getReader();+ const decoder = new TextDecoder();+ let raw = "";++ try {+ while (true) {+ const { done, value } = await reader.read();+ if (done) break;+ raw += decoder.decode(value, { stream: true });+ onUpdate?.(parse(raw));+ }+ } catch (err) {+ // Abnormal end (abort / incomplete chunked encoding): keep what we have.+ if (!raw) throw err;+ }++ const final = parse(raw);+ if (!final.body && !final.scores) {+ throw new Error(+ "The AI service returned no data. Please ensure it is running and try again.",+ );+ }+ onUpdate?.(final);+ return final;+}