All files / src/analyze-document index.ts

72.81% Statements 233/320
59.52% Branches 25/42
60% Functions 3/5
72.81% Lines 233/320

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 3211x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 8x     8x 8x 8x 8x 8x               8x 8x 8x 8x 8x 8x 2x 2x 2x 2x 1x 1x 2x 2x 2x 6x 6x 8x 8x 8x               6x 6x 8x 8x               6x 8x 8x 8x 8x 8x 6x   8x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 4x 4x 4x 4x 4x 4x 4x 4x 4x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x   3x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                       8x 1x 1x 6x 6x 6x 6x       6x 6x     6x 1x 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 6x 1x 1x 1x 1x 1x 1x 1x 6x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 1x                                                                       1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x       1x  
import {
  app,
  HttpRequest,
  HttpResponseInit,
  InvocationContext,
} from "@azure/functions";
import {
  isPriorFingerprint,
  isScoringProfile,
  MAIN_TEXT_CAP,
  MIN_TEXT_CHARS,
  type AnalyzeJobRequest,
} from "../models/job";
import type { AuthenticatedUser } from "../models/user";
import type { ScoringProfile } from "../../../shared/types/profile";
import { pasteIdentity } from "../../../shared/pasteIdentity";
import { withAuth } from "../services/auth";
import {
  DocumentRejectedError,
  extractDocument,
  REJECTION_STATUS,
} from "../services/documentExtraction";
import { corsHeaders, errorResponse, requestOrigin } from "../services/http";
import {
  orchestrateJobAnalysis,
  JobSchemaError,
  ModelTimeoutError,
} from "../services/jobExtractionOrchestrator";
import {
  chargeForAnalysis,
  refundAfterFailure,
  settleCharge,
} from "../services/analysisMetering";
import { fingerprintForRequest } from "../services/profileFingerprint";
import { sha256Hex } from "../services/savedJobsRepository";
import { withRateLimit } from "../services/rateLimiter";
 
/** The posting a request carries: an uploaded file's text, or pasted text (FR-027). */
interface PostingSource {
  source: "document" | "paste";
  /** Title the model sees: the filename for a document, empty for a paste. */
  title: string;
  filename?: string;
  mainText: string;
  canonicalUrl: string;
}
 
/**
 * POST /api/analyze-document (contracts/analyze-document.md, 006
 * contracts/analyze-api.md): multipart → parse and validate every part →
 * extract a `file` part (research R7 of 004: reject before charging) or
 * length-check a `text` part → charge the counter research R6 picks →
 * synthetic AnalyzeJobRequest → orchestrateJobAnalysis → response with
 * saveKey.
 */
export async function analyzeDocumentHandler(
  request: HttpRequest,
  context: InvocationContext,
  user: AuthenticatedUser
): Promise<HttpResponseInit> {
  const origin = requestOrigin(request);
  if (request.method === "OPTIONS") {
    return { status: 204, headers: corsHeaders(origin) };
  }
 
  let formData: FormData;
  try {
    formData = await request.formData();
  } catch {
    return errorResponse(
      400,
      "INVALID_REQUEST",
      "Request must be multipart/form-data.",
      origin
    );
  }
 
  const file = formData.get("file");
  const text = formData.get("text");
  const hasFile = file instanceof Blob;
  const hasText = typeof text === "string";
  if (hasFile === hasText) {
    return errorResponse(
      400,
      "INVALID_REQUEST",
      hasFile
        ? "Send either a file or text, not both."
        : "Missing required file part.",
      origin
    );
  }
 
  const profileField = formData.get("profile");
  const profile = typeof profileField === "string" ? profileField : undefined;
  const profileData = parseProfileData(formData.get("profileData"));
  if (profileData === "invalid") {
    return errorResponse(
      400,
      "INVALID_REQUEST",
      "profileData must be JSON with text: string and dealbreakers: string[].",
      origin
    );
  }
  const priorField = formData.get("priorFingerprint");
  const priorFingerprint =
    typeof priorField === "string" && priorField ? priorField : null;
  if (!isPriorFingerprint(priorFingerprint)) {
    return errorResponse(
      400,
      "INVALID_REQUEST",
      "priorFingerprint must be 64 hex characters.",
      origin
    );
  }
  const intent =
    formData.get("rescore") === "true" ? { priorFingerprint } : undefined;
  const assumeJobPosting = formData.get("assumeJobPosting") === "true";
 
  // The both/neither check above guarantees `file` is a Blob without text.
  const posting = hasText
    ? await pastedPosting(text, origin)
    : await documentPosting(file as Blob, context, origin);
  if (!("mainText" in posting)) return posting;
 
  const syntheticRequest: AnalyzeJobRequest = {
    extract: {
      url: "",
      canonicalUrl: "",
      title: posting.title,
      jsonLd: [],
      mainText: posting.mainText,
      extractedAt: new Date().toISOString(),
    },
    profile,
    ...(profileData ? { profileData } : {}),
    assumeJobPosting,
  };
  const saveKey = sha256Hex(posting.canonicalUrl);
 
  // Only now — after every validation rejection has already returned —
  // does the request touch the caller's monthly allowance (research R7).
  const outcome = await chargeForAnalysis(
    {
      user,
      saveKey,
      intent,
      currentFingerprint: fingerprintForRequest(syntheticRequest),
    },
    context,
    origin
  );
  if (!outcome.ok) return outcome.response;
  const { charge } = outcome;
 
  try {
    const analysis = await orchestrateJobAnalysis(
      syntheticRequest,
      user.tier,
      (message) => context.warn(message),
      (usage) => context.log("model.usage", JSON.stringify(usage))
    );
    const settled = await settleCharge(
      { user, intent },
      charge,
      analysis,
      assumeJobPosting
    );
    return {
      status: 200,
      headers: corsHeaders(origin),
      jsonBody: {
        analysis,
        source: posting.source,
        ...(posting.filename !== undefined
          ? { filename: posting.filename }
          : {}),
        // The client cannot derive `doc:<hash>` itself (it never sees the
        // extracted text) — canonicalUrl must round-trip so PUT
        // /api/jobs/{saveKey} can pass saveJob's sha256Hex(canonicalUrl)
        // === key check (data-model.md §2).
        canonicalUrl: posting.canonicalUrl,
        saveKey,
        ...settled,
      },
    };
  } catch (err) {
    refundAfterFailure(user, charge, context);
    if (err instanceof ModelTimeoutError) {
      context.error("analyze-document model timeout:", err.message);
      return errorResponse(
        504,
        "MODEL_TIMEOUT",
        "The analysis took too long. Please try again.",
        origin
      );
    }
    if (err instanceof JobSchemaError) {
      context.error("analyze-document schema failure:", err.message);
      return errorResponse(
        502,
        "SCHEMA_PARSE_FAILED",
        "The analysis service returned an unusable result. Please try again.",
        origin
      );
    }
    // Includes ModelConfigError: a misconfigured app setting is a 500, refunded
    // like any other system failure (contracts/model-config.md).
    context.error("orchestrateJobAnalysis failed:", err);
    return errorResponse(
      500,
      "SERVICE_ERROR",
      "Analysis failed. Please try again.",
      origin
    );
  }
}
 
/** Absent → undefined; present but malformed → "invalid" (never a silent fallback). */
function parseProfileData(
  field: ReturnType<FormData["get"]>
): ScoringProfile | undefined | "invalid" {
  if (field === null) return undefined;
  if (typeof field !== "string") return "invalid";
  try {
    const parsed: unknown = JSON.parse(field);
    return isScoringProfile(parsed) ? parsed : "invalid";
  } catch {
    return "invalid";
  }
}
 
/**
 * The web's Paste text option (FR-027): the same bounds and messages as the
 * extension's paste composer, checked before charging, and the same
 * `paste:` identity, so a paste lands on the same saved entry on both
 * surfaces (research R7).
 */
async function pastedPosting(
  text: string,
  origin: string | null
): Promise<PostingSource | HttpResponseInit> {
  if (text.length < MIN_TEXT_CHARS) {
    return errorResponse(
      400,
      "TEXT_TOO_SHORT",
      "Paste more of the posting before analyzing.",
      origin
    );
  }
  if (text.length > MAIN_TEXT_CAP) {
    return errorResponse(
      413,
      "TEXT_TOO_LONG",
      "Trim it to the posting itself before analyzing.",
      origin
    );
  }
  return {
    source: "paste",
    title: "",
    mainText: text,
    canonicalUrl: await pasteIdentity(text),
  };
}
 
async function documentPosting(
  file: Blob,
  context: InvocationContext,
  origin: string | null
): Promise<PostingSource | HttpResponseInit> {
  const filename =
    "name" in file && typeof file.name === "string" ? file.name : "document";
  const bytes = Buffer.from(await file.arrayBuffer());
  try {
    const extracted = await extractDocument(bytes);
    return {
      source: "document",
      title: filename,
      filename,
      mainText: extracted.text.slice(0, MAIN_TEXT_CAP),
      canonicalUrl: `doc:${sha256Hex(extracted.text)}`,
    };
  } catch (err) {
    if (err instanceof DocumentRejectedError) {
      return errorResponse(
        REJECTION_STATUS[err.code],
        err.code,
        err.message,
        origin
      );
    }
    context.error("document extraction failed unexpectedly:", err);
    return errorResponse(
      422,
      "FILE_UNREADABLE",
      "This document could not be read. It may be corrupt.",
      origin
    );
  }
}
 
app.http("analyze-document", {
  methods: ["POST"],
  // withAuth (Google ID token) is the real gate; anonymous so the public
  // web SPA (whose bundle can never safely hold a function key) can call
  // this route too (contracts/web-auth.md).
  authLevel: "anonymous",
  route: "analyze-document",
  handler: withRateLimit("analyze", withAuth(analyzeDocumentHandler)),
});
 
app.http("analyze-document-preflight", {
  methods: ["OPTIONS"],
  authLevel: "anonymous",
  route: "analyze-document",
  handler: (request: HttpRequest) => ({
    status: 204,
    headers: corsHeaders(requestOrigin(request)),
  }),
});