# AI Search Phase 2 DOCX Extraction

Use this prompt to implement deterministic DOCX text extraction for AI indexing only.

## Scope

- Work in `src/lib/search/ai/extraction`; do not use `src/lib/content-extraction.ts`.
- Preserve existing text-like and PDF extraction behavior.
- Support `.docx` and `application/vnd.openxmlformats-officedocument.wordprocessingml.document`, including parameterized MIME values.
- Keep legacy `.doc`, ODT, RTF, Google-native formats, PPTX, XLSX, OCR, and embedded image extraction out of this phase.
- Use `mammoth.extractRawText` for plain text and paragraph breaks where Mammoth provides them.
- Skip images, charts, SmartArt, diagrams, tracked-change/comment metadata, and complex formatting preservation.

## Implementation Notes

- Add `mammoth` if it is not already present. Mammoth 1.12 ships `lib/index.d.ts`; `@types/mammoth` is not published.
- Add a dedicated DOCX parser module with a typed parser error carrying sanitized messages, `cause`, and parser context.
- Route DOCX inputs through the DOCX extractor before generic UTF decoding.
- Keep unsupported document policies for legacy `.doc`, ODT, RTF, and Google-native formats.
- Use existing normalization and byte-limit enforcement.
- Route BackendIndexingRunner telemetry with `extractionMethod: "docx"`.
- Use sanitized taxonomy messages for corrupt and encrypted DOCX failures.

## Required Tests

- Valid DOCX with text.
- Empty DOCX returns empty extractor output.
- Corrupt DOCX fails with the DOCX parse taxonomy.
- Password-protected/encrypted DOCX fails or skips with the DOCX encrypted taxonomy.
- DOCX with embedded images extracts text only.
- BackendIndexingRunner routes DOCX to the DOCX extractor and records `docx`.

## Verification

```powershell
pnpm env:guard
pnpm vitest run src/lib/search/ai/extraction/docx-text.test.ts
pnpm vitest run src/lib/search/ai/indexing/backend-indexing-runner-docx.test.ts
pnpm vitest run src/lib/search/ai/extraction/extract-document.test.ts
pnpm vitest run src/lib/search/ai/indexing/error-taxonomy.test.ts
pnpm typecheck
pnpm exec eslint --config .eslintrc.json src/lib/search/ai/extraction src/lib/search/ai/indexing
pnpm check
pnpm build
git diff --check
pnpm exec prettier --check src/lib/search/ai/extraction src/lib/search/ai/indexing
```
