Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3ffe489e32 | ||
|
|
acbddf88d2 | ||
|
|
23ac6392bb | ||
|
|
d2fd454d6e |
@@ -31,6 +31,11 @@ exampleYmlFiles/stirling/
|
||||
/testing/file_snapshots
|
||||
SwaggerDoc.json
|
||||
|
||||
# Docker bind-mount volumes (local data)
|
||||
AI-Document-Generator-main/backend/data/
|
||||
AI-Document-Generator-main/backend/output/
|
||||
docker/compose/stirling/
|
||||
|
||||
# Frontend build artifacts copied to backend static resources
|
||||
# These are generated by npm build and should not be committed
|
||||
app/core/src/main/resources/static/assets/
|
||||
@@ -241,4 +246,3 @@ docs/type3/signatures/
|
||||
|
||||
# Type3 sample PDFs (development only)
|
||||
**/type3/samples/
|
||||
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
# Python
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
*$py.class
|
||||
*.so
|
||||
.Python
|
||||
env/
|
||||
venv/
|
||||
ENV/
|
||||
build/
|
||||
dist/
|
||||
*.egg-info/
|
||||
|
||||
# Node
|
||||
node_modules/
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
dist/
|
||||
.vite/
|
||||
|
||||
# Environment
|
||||
.env
|
||||
.env.local
|
||||
|
||||
# LaTeX outputs
|
||||
*.aux
|
||||
*.log
|
||||
*.out
|
||||
*.toc
|
||||
*.pdf
|
||||
*.tex
|
||||
backend/output/
|
||||
backend/data/user_styles.json
|
||||
|
||||
# IDE
|
||||
.vscode/
|
||||
.idea/
|
||||
*.swp
|
||||
*.swo
|
||||
*~
|
||||
|
||||
# OS
|
||||
.DS_Store
|
||||
Thumbs.db
|
||||
@@ -0,0 +1,32 @@
|
||||
# syntax=docker/dockerfile:1.5
|
||||
FROM python:3.11-slim
|
||||
|
||||
# Install full TeXLive so LLM outputs (siunitx, paracol, tikz, etc.) compile reliably.
|
||||
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
|
||||
apt-get update && \
|
||||
apt-get install -y --no-install-recommends \
|
||||
texlive-full \
|
||||
latexmk \
|
||||
ghostscript \
|
||||
poppler-utils \
|
||||
ca-certificates \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
# Set working directory
|
||||
WORKDIR /app
|
||||
|
||||
# Copy backend files
|
||||
COPY backend/requirements.txt .
|
||||
RUN --mount=type=cache,target=/root/.cache/pip \
|
||||
pip install -r requirements.txt
|
||||
|
||||
COPY backend/ .
|
||||
|
||||
# Create output directories
|
||||
RUN mkdir -p /app/output /app/data
|
||||
|
||||
# Expose port
|
||||
EXPOSE 5000
|
||||
|
||||
# Run the Flask app
|
||||
CMD ["python", "app.py"]
|
||||
@@ -0,0 +1,173 @@
|
||||
# LaTeX PDF Generator
|
||||
|
||||
AI-powered document generator using LaTeX. Creates professional documents (invoices, resumes, contracts, etc.) from natural language prompts with conversational editing.
|
||||
|
||||
## Features
|
||||
|
||||
- 🎨 ChatGPT-like interface with split-screen PDF preview (main window only)
|
||||
- 📝 Generates LaTeX documents from natural language and compiles to PDF in Docker
|
||||
- 🔄 Conversational editing with PDF regeneration on every turn
|
||||
- 💾 Style memory + template reuse per user/team and document type
|
||||
- 🗂️ Version history with per-iteration PDFs you can reopen
|
||||
- 📄 Supports: Invoices, Resumes, Contracts, Letters, Reports, poems, proposals, and more
|
||||
|
||||
## Quick Start
|
||||
|
||||
### Option 1: Docker (Recommended)
|
||||
|
||||
```bash
|
||||
# Build the Docker image
|
||||
docker build -t latex-generator .
|
||||
|
||||
# Run the backend
|
||||
docker run -p 5000:5000 latex-generator
|
||||
```
|
||||
|
||||
### Option 2: Local Development
|
||||
|
||||
#### Backend Setup
|
||||
|
||||
```bash
|
||||
cd backend
|
||||
|
||||
# Install Python dependencies
|
||||
pip install -r requirements.txt
|
||||
|
||||
# Install LaTeX (if not already installed)
|
||||
# On Ubuntu/Debian:
|
||||
sudo apt-get install texlive-latex-base texlive-latex-extra
|
||||
|
||||
# On Mac:
|
||||
brew install --cask mactex
|
||||
|
||||
# On Windows:
|
||||
# Download and install MiKTeX from https://miktex.org/download
|
||||
|
||||
# Run the backend
|
||||
python app.py
|
||||
```
|
||||
|
||||
Backend will run on `http://localhost:5000`
|
||||
|
||||
#### Frontend Setup
|
||||
|
||||
```bash
|
||||
cd frontend
|
||||
|
||||
# Install dependencies
|
||||
npm install
|
||||
|
||||
# Run the dev server
|
||||
npm run dev
|
||||
```
|
||||
|
||||
Frontend will run on `http://localhost:3000`
|
||||
|
||||
## Usage
|
||||
|
||||
1. Open `http://localhost:3000` in your browser
|
||||
2. Type a prompt like "Create an invoice for web development services"
|
||||
3. The AI generates LaTeX code and compiles it to PDF
|
||||
4. Continue chatting to refine the document
|
||||
5. Download the final PDF
|
||||
|
||||
## Example Prompts
|
||||
|
||||
- "Create a professional invoice for $1,500 in consulting services"
|
||||
- "Generate a resume for a senior software engineer with 5 years experience"
|
||||
- "Make a business letter to a client about project completion"
|
||||
- "Create a contract for freelance web development"
|
||||
|
||||
## Configuration
|
||||
|
||||
### OpenAI API (Optional)
|
||||
|
||||
To use real AI generation instead of mock templates:
|
||||
|
||||
1. Copy `.env.example` to `.env`
|
||||
2. Add your OpenAI API key: `OPENAI_API_KEY=sk-...`
|
||||
3. (Optional) Choose models:
|
||||
- Smart (full generation): `SMART_MODEL=gpt-5.1` (default)
|
||||
- Fast (intent/pre checks): `FAST_MODEL=gpt-4.1-nano` (default)
|
||||
3. (Legacy SDK) We pin `openai==0.28.1` in Docker to avoid client init issues. No code changes needed.
|
||||
|
||||
## Project Structure
|
||||
|
||||
```
|
||||
latex-pdf-generator/
|
||||
├── frontend/ # React + TypeScript + Tailwind
|
||||
│ ├── src/
|
||||
│ │ ├── components/
|
||||
│ │ │ ├── landing/ # Landing hero + CTA
|
||||
│ │ │ ├── modals/ # Reusable modal(s)
|
||||
│ │ │ ├── workspace/ # Chat panel, preview, history
|
||||
│ │ │ └── ui/ # Buttons, button groups, etc.
|
||||
│ │ ├── hooks/ # Workflow + speech capture hooks
|
||||
│ │ ├── types/ # Shared TypeScript interfaces
|
||||
│ │ ├── App.tsx # Thin orchestrator
|
||||
│ │ └── main.tsx
|
||||
│ ├── .eslintrc.cjs # ESLint config (React + TS)
|
||||
│ ├── package.json
|
||||
│ └── vite.config.ts
|
||||
├── backend/ # Flask + LaTeX
|
||||
│ ├── app.py # Routes + Flask app factory
|
||||
│ ├── ai_generation.py # OpenAI + mock generation helpers
|
||||
│ ├── briefs.py # Guided brief collection utilities
|
||||
│ ├── config.py # Logging + environment setup
|
||||
│ ├── document_types.py # Doc-type heuristics
|
||||
│ ├── latex_utils.py # LaTeX sanitizers + layout helpers
|
||||
│ ├── pdf_utils.py # PDF compilation + render helpers
|
||||
│ ├── storage.py # JSON persistence for users/templates
|
||||
│ ├── styles.py # Style preference heuristics
|
||||
│ ├── vision.py # Layout extraction via multimodal GPT
|
||||
│ ├── requirements.txt
|
||||
│ └── data/ # User style storage
|
||||
├── Dockerfile
|
||||
└── README.md
|
||||
```
|
||||
|
||||
### Linting
|
||||
|
||||
The frontend now ships with ESLint + TypeScript rules that keep the new modular structure tidy:
|
||||
|
||||
```bash
|
||||
cd frontend
|
||||
npm run lint
|
||||
```
|
||||
|
||||
Backend linting can be added with your preferred tool (e.g., ruff or flake8) by pointing it at the new small modules in `backend/`.
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### LaTeX compilation fails
|
||||
|
||||
- Ensure `pdflatex` is in your PATH
|
||||
- Check logs in the backend console
|
||||
- Verify LaTeX packages are installed
|
||||
|
||||
### CORS errors
|
||||
|
||||
- Make sure both frontend and backend are running
|
||||
- Frontend proxy is configured in `vite.config.ts`
|
||||
|
||||
### PDF not displaying
|
||||
|
||||
- Check browser console for errors
|
||||
- Ensure the backend `/output` endpoint is accessible
|
||||
- Try opening the PDF URL directly
|
||||
|
||||
## Development
|
||||
|
||||
### Mock Mode (Current)
|
||||
|
||||
The app currently uses mock LaTeX templates for quick testing. To enable real AI:
|
||||
|
||||
1. Get an OpenAI API key
|
||||
2. Set `OPENAI_API_KEY` and optionally override:
|
||||
- `SMART_MODEL` (default `gpt-5.1`)
|
||||
- `FAST_MODEL` (default `gpt-4.1-nano`)
|
||||
3. Restart the backend; it auto-detects whether to call the live model or the bundled mock templates
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
||||
@@ -0,0 +1,659 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import re
|
||||
from typing import Any, Dict, List, Optional
|
||||
import time
|
||||
|
||||
from config import CLIENT_MODE, SMART_MODEL, STREAMING_ENABLED, get_chat_model, logger
|
||||
from langchain_utils import to_lc_messages
|
||||
from storage import save_user_style
|
||||
from prompts import latex_system_prompt, latex_context_messages
|
||||
|
||||
|
||||
def generate_outline_with_llm(
|
||||
prompt: str,
|
||||
document_type: str,
|
||||
constraints: Optional[Dict[str, Any]] = None,
|
||||
) -> str:
|
||||
if CLIENT_MODE == "langchain":
|
||||
constraint_text = ""
|
||||
if constraints:
|
||||
tone = constraints.get("tone")
|
||||
audience = constraints.get("audience")
|
||||
pages = constraints.get("pageCount")
|
||||
constraint_text = f"Tone: {tone}. Audience: {audience}. Target pages: {pages}."
|
||||
system_prompt = (
|
||||
"You are an outline generator for document creation.\n"
|
||||
f"Document type: {document_type}\n"
|
||||
f"{constraint_text}\n"
|
||||
"Return a concise outline with section titles and short descriptions.\n"
|
||||
"Keep each description to roughly 6-12 words.\n"
|
||||
"Ensure the outline scope fits the target page count.\n"
|
||||
"Output plain text only, using a numbered list with 5-9 sections."
|
||||
)
|
||||
messages: List[Dict[str, Any]] = [
|
||||
{"role": "system", "content": system_prompt},
|
||||
{"role": "user", "content": prompt},
|
||||
]
|
||||
try:
|
||||
llm = get_chat_model(SMART_MODEL)
|
||||
if llm:
|
||||
start = time.perf_counter()
|
||||
response = llm.invoke(to_lc_messages(messages))
|
||||
elapsed = time.perf_counter() - start
|
||||
content = response.content or ""
|
||||
usage = getattr(response, "usage_metadata", None)
|
||||
logger.info(
|
||||
"[AI] outline model=%s elapsed=%.2fs chars=%s usage=%s",
|
||||
SMART_MODEL,
|
||||
elapsed,
|
||||
len(str(content)),
|
||||
usage,
|
||||
)
|
||||
if content:
|
||||
return str(content).strip()
|
||||
except Exception as exc:
|
||||
logger.error("[AI] Outline generation failed, falling back: %s", exc)
|
||||
|
||||
safe_prompt = prompt.strip() or "Document"
|
||||
return (
|
||||
"1) Introduction - Summary of the document goals.\n"
|
||||
"2) Background - Context and key assumptions.\n"
|
||||
"3) Main Content - Core points and supporting details.\n"
|
||||
"4) Evidence - Data, examples, or references.\n"
|
||||
"5) Conclusion - Wrap-up and next steps.\n"
|
||||
f"Notes: Tailor details to '{safe_prompt}'."
|
||||
)
|
||||
|
||||
|
||||
def _parse_outline_to_sections(outline_text: str) -> List[Dict[str, str]]:
|
||||
lines = [
|
||||
line.strip()
|
||||
for line in outline_text.split("\n")
|
||||
if line.strip() and not re.match(r"^(section|details)$", line.strip(), re.IGNORECASE)
|
||||
]
|
||||
sections: List[Dict[str, str]] = []
|
||||
i = 0
|
||||
while i < len(lines):
|
||||
cleaned = re.sub(r"^\d+[\).\s-]+", "", lines[i]).strip()
|
||||
if not cleaned:
|
||||
i += 1
|
||||
continue
|
||||
|
||||
split = re.split(r"[-–:]+", cleaned, maxsplit=1)
|
||||
if len(split) > 1:
|
||||
sections.append({"label": split[0].strip() or "Section", "value": split[1].strip()})
|
||||
i += 1
|
||||
continue
|
||||
|
||||
next_line = lines[i + 1].strip() if i + 1 < len(lines) else ""
|
||||
if next_line and not re.match(r"^\d+[\).\s-]+", next_line):
|
||||
sections.append({"label": cleaned, "value": next_line})
|
||||
i += 2
|
||||
continue
|
||||
|
||||
sections.append({"label": cleaned, "value": ""})
|
||||
i += 1
|
||||
|
||||
return sections
|
||||
|
||||
|
||||
def _extract_fields_from_prompt(prompt: str, fields: List[Dict[str, Any]]) -> List[Dict[str, str]]:
|
||||
lines = [line.strip() for line in prompt.split("\n") if line.strip()]
|
||||
kv_pairs: Dict[str, str] = {}
|
||||
for line in lines:
|
||||
match = re.match(r"^([^:]{2,40}):\s*(.+)$", line)
|
||||
if match:
|
||||
kv_pairs[match.group(1).strip().lower()] = match.group(2).strip()
|
||||
|
||||
email_match = re.search(r"[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}", prompt, re.IGNORECASE)
|
||||
phone_match = re.search(r"(\+?\d[\d\s().-]{7,})", prompt)
|
||||
date_match = re.search(r"\b\d{1,2}[\/.-]\d{1,2}[\/.-]\d{2,4}\b", prompt)
|
||||
money_match = re.search(r"\$\s?\d[\d,]*(?:\.\d{2})?", prompt)
|
||||
|
||||
filled: List[Dict[str, str]] = []
|
||||
for field in fields:
|
||||
label = str(field.get("label", "Field"))
|
||||
value = str(field.get("value", "") or "")
|
||||
if value.strip():
|
||||
filled.append({"label": label, "value": value})
|
||||
continue
|
||||
label_lower = label.lower()
|
||||
for key, val in kv_pairs.items():
|
||||
if key in label_lower:
|
||||
value = val
|
||||
break
|
||||
if not value and email_match and "email" in label_lower:
|
||||
value = email_match.group(0)
|
||||
if not value and phone_match and "phone" in label_lower:
|
||||
value = phone_match.group(0)
|
||||
if not value and date_match and ("date" in label_lower or "due" in label_lower):
|
||||
value = date_match.group(0)
|
||||
if not value and money_match and ("total" in label_lower or "amount" in label_lower):
|
||||
value = money_match.group(0)
|
||||
filled.append({"label": label, "value": value})
|
||||
return filled
|
||||
|
||||
|
||||
def generate_field_values(
|
||||
prompt: str,
|
||||
document_type: str,
|
||||
fields: List[Dict[str, Any]],
|
||||
constraints: Optional[Dict[str, Any]] = None,
|
||||
) -> List[Dict[str, str]]:
|
||||
if CLIENT_MODE == "langchain":
|
||||
constraint_text = ""
|
||||
if constraints:
|
||||
tone = constraints.get("tone")
|
||||
audience = constraints.get("audience")
|
||||
pages = constraints.get("pageCount")
|
||||
constraint_text = f"Tone: {tone}. Audience: {audience}. Target pages: {pages}."
|
||||
system_prompt = (
|
||||
"You are extracting field values from a user prompt.\n"
|
||||
"Return a JSON array of objects with keys: label, value.\n"
|
||||
"Only fill values that are explicitly stated or strongly implied.\n"
|
||||
"If unknown, return an empty string.\n"
|
||||
f"{constraint_text}\n"
|
||||
"Output JSON only."
|
||||
)
|
||||
messages: List[Dict[str, Any]] = [
|
||||
{"role": "system", "content": system_prompt},
|
||||
{"role": "user", "content": f"Document type: {document_type}"},
|
||||
{"role": "user", "content": f"Prompt:\n{prompt}"},
|
||||
{"role": "user", "content": f"Fields:\n{json.dumps(fields, ensure_ascii=True)}"},
|
||||
]
|
||||
try:
|
||||
llm = get_chat_model(SMART_MODEL)
|
||||
if llm:
|
||||
start = time.perf_counter()
|
||||
response = llm.invoke(to_lc_messages(messages))
|
||||
elapsed = time.perf_counter() - start
|
||||
content = response.content or ""
|
||||
usage = getattr(response, "usage_metadata", None)
|
||||
logger.info(
|
||||
"[AI] field-extract model=%s elapsed=%.2fs chars=%s usage=%s",
|
||||
SMART_MODEL,
|
||||
elapsed,
|
||||
len(str(content)),
|
||||
usage,
|
||||
)
|
||||
if content:
|
||||
parsed = _extract_json_array(str(content))
|
||||
if parsed:
|
||||
return [
|
||||
{
|
||||
"label": str(item.get("label", "Field")),
|
||||
"value": str(item.get("value", "")),
|
||||
}
|
||||
for item in parsed
|
||||
if isinstance(item, dict)
|
||||
]
|
||||
except Exception as exc:
|
||||
logger.error("[AI] Field extraction failed, falling back: %s", exc)
|
||||
|
||||
return _extract_fields_from_prompt(prompt, fields)
|
||||
|
||||
def _extract_json_array(payload: str) -> Optional[List[Dict[str, Any]]]:
|
||||
try:
|
||||
return json.loads(payload)
|
||||
except json.JSONDecodeError:
|
||||
match = re.search(r"\[[\s\S]*\]", payload)
|
||||
if not match:
|
||||
return None
|
||||
try:
|
||||
return json.loads(match.group(0))
|
||||
except json.JSONDecodeError:
|
||||
return None
|
||||
|
||||
|
||||
def generate_section_draft(
|
||||
prompt: str,
|
||||
document_type: str,
|
||||
outline_text: str,
|
||||
constraints: Optional[Dict[str, Any]] = None,
|
||||
) -> List[Dict[str, str]]:
|
||||
if CLIENT_MODE == "langchain":
|
||||
constraint_text = ""
|
||||
if constraints:
|
||||
tone = constraints.get("tone")
|
||||
audience = constraints.get("audience")
|
||||
pages = constraints.get("pageCount")
|
||||
constraint_text = f"Tone: {tone}. Audience: {audience}. Target pages: {pages}."
|
||||
system_prompt = (
|
||||
"You are generating section content for a document.\n"
|
||||
"Return a JSON array of objects with keys: label, value.\n"
|
||||
"Use the provided outline sections as labels; values should be polished draft text.\n"
|
||||
f"{constraint_text}\n"
|
||||
"Keep the total length appropriate to the target pages.\n"
|
||||
"Output JSON only."
|
||||
)
|
||||
messages: List[Dict[str, Any]] = [
|
||||
{"role": "system", "content": system_prompt},
|
||||
{"role": "user", "content": f"Document type: {document_type}"},
|
||||
{"role": "user", "content": f"Outline:\n{outline_text}"},
|
||||
{"role": "user", "content": f"Prompt:\n{prompt}"},
|
||||
]
|
||||
try:
|
||||
llm = get_chat_model(SMART_MODEL)
|
||||
if llm:
|
||||
start = time.perf_counter()
|
||||
response = llm.invoke(to_lc_messages(messages))
|
||||
elapsed = time.perf_counter() - start
|
||||
content = response.content or ""
|
||||
usage = getattr(response, "usage_metadata", None)
|
||||
logger.info(
|
||||
"[AI] section-draft model=%s elapsed=%.2fs chars=%s usage=%s",
|
||||
SMART_MODEL,
|
||||
elapsed,
|
||||
len(str(content)),
|
||||
usage,
|
||||
)
|
||||
if content:
|
||||
parsed = _extract_json_array(str(content))
|
||||
if parsed:
|
||||
return [
|
||||
{
|
||||
"label": str(item.get("label", "Section")),
|
||||
"value": str(item.get("value", "")),
|
||||
}
|
||||
for item in parsed
|
||||
if isinstance(item, dict)
|
||||
]
|
||||
except Exception as exc:
|
||||
logger.error("[AI] Section draft generation failed, falling back: %s", exc)
|
||||
|
||||
if outline_text.strip():
|
||||
return _parse_outline_to_sections(outline_text)
|
||||
|
||||
fallback_label = "Main Content"
|
||||
return [{"label": fallback_label, "value": prompt.strip() or "Draft content"}]
|
||||
|
||||
|
||||
def _fallback_template_fill(template_latex: str, outline_text: str, draft_text: Optional[str] = None) -> str:
|
||||
default_text = draft_text or outline_text or "Details pending."
|
||||
replacements = {
|
||||
"TITLE": "Project Overview",
|
||||
"SUBTITLE": "Executive Summary",
|
||||
"AUTHOR": "Jane Doe",
|
||||
"AUTHOR_LIST": "Jane Doe, John Smith",
|
||||
"AFFILIATIONS": "John Smith Consulting",
|
||||
"ABSTRACT": default_text,
|
||||
"KEYWORDS": "keyword1, keyword2, keyword3",
|
||||
"INTRODUCTION": default_text,
|
||||
"RELATED_WORK": default_text,
|
||||
"METHODOLOGY": default_text,
|
||||
"RESULTS": default_text,
|
||||
"DISCUSSION": default_text,
|
||||
"CONCLUSION": default_text,
|
||||
"REFERENCES": default_text,
|
||||
"MAIN_TEXT": default_text,
|
||||
"FIGURES_TABLES": default_text,
|
||||
"REPORT_TITLE": "Business Report",
|
||||
"DATE": "2025-01-01",
|
||||
"EXEC_SUMMARY": default_text,
|
||||
"BACKGROUND": default_text,
|
||||
"FINDINGS": default_text,
|
||||
"RECOMMENDATIONS": default_text,
|
||||
"APPENDIX": default_text,
|
||||
"NEWSLETTER_TITLE": "Doe Consulting Monthly",
|
||||
"TOP_STORY": default_text,
|
||||
"UPDATES": default_text,
|
||||
"SPOTLIGHT": default_text,
|
||||
"FOOTER": "Contact: info@example.com",
|
||||
"RECIPE_TITLE": "Recipe Title",
|
||||
"SERVINGS": "Serves 4",
|
||||
"TIME": "30 minutes",
|
||||
"INGREDIENTS": "\\\\begin{itemize}\\\\item Ingredient A\\\\item Ingredient B\\\\end{itemize}",
|
||||
"INSTRUCTIONS": default_text,
|
||||
"NOTES": "Notes and tips.",
|
||||
"BUSINESS_NAME": "John Smith Consulting",
|
||||
"BUSINESS_ADDRESS": "123 Example Street, Example City",
|
||||
"BUSINESS_CONTACT": "billing@example.com | (555) 000-0000",
|
||||
"INVOICE_NUMBER": "INV-1001",
|
||||
"ISSUE_DATE": "2025-01-01",
|
||||
"DUE_DATE": "2025-01-15",
|
||||
"CLIENT_NAME": "Doe Corporation",
|
||||
"CLIENT_ADDRESS": "456 Sample Avenue, Example City",
|
||||
"CLIENT_CONTACT": "ap@example.com",
|
||||
"LINE_ITEMS": "Service & 1 & $1000 & $1000 \\\\\\\\",
|
||||
"SUBTOTAL": "$1000",
|
||||
"TAXES": "$0",
|
||||
"TOTAL": "$1000",
|
||||
"PAYMENT_TERMS": "Net 15",
|
||||
"PAYMENT_METHODS": "Bank transfer, credit card",
|
||||
"STUDENT_NAME": "Jane Doe",
|
||||
"COURSE_NAME": "Business Communications",
|
||||
"INSTRUCTOR_NAME": "Dr. Rivera",
|
||||
"ASSIGNMENT_TITLE": "Market Analysis",
|
||||
"PROMPT": default_text,
|
||||
"RESPONSE": default_text,
|
||||
"CHAPTER_ONE_TITLE": "Chapter One",
|
||||
"CHAPTER_ONE": default_text,
|
||||
"CHAPTER_TWO_TITLE": "Chapter Two",
|
||||
"CHAPTER_TWO": default_text,
|
||||
"PREFACE": default_text,
|
||||
"PUBLISHER": "Doe Press",
|
||||
"NAME": "Jane Doe",
|
||||
"TITLE_PAGE": "Project Overview",
|
||||
"EMAIL": "jane.doe@example.com",
|
||||
"PHONE": "(555) 000-0000",
|
||||
"LOCATION": "Example City, USA",
|
||||
"SUMMARY": default_text,
|
||||
"EXPERIENCE": default_text,
|
||||
"EDUCATION": default_text,
|
||||
"SKILLS": default_text,
|
||||
"PROJECTS": default_text,
|
||||
"SUBJECT": "Subject",
|
||||
"BODY": default_text,
|
||||
"RECIPIENT_NAME": "John Smith",
|
||||
"RECIPIENT_TITLE": "Hiring Manager",
|
||||
"RECIPIENT_COMPANY": "Doe Corporation",
|
||||
"RECIPIENT_ADDRESS": "456 Sample Avenue, Example City",
|
||||
"SENDER_NAME": "Jane Doe",
|
||||
"SENDER_ADDRESS": "123 Example Street, Example City",
|
||||
"SENDER_EMAIL": "jane.doe@example.com",
|
||||
"MONTH_YEAR": "January 2025",
|
||||
"THEME": "Theme",
|
||||
"WEEK_ROWS": "1 & 2 & 3 & 4 & 5 & 6 & 7 \\\\\\\\ \\\\hline",
|
||||
"HEADLINE": "Launch Announcement",
|
||||
"SUBTEXT": "Introducing our latest release.",
|
||||
"CALL_TO_ACTION": "Visit example.com to learn more.",
|
||||
"CONTACT": "contact@example.com",
|
||||
"EXPERIMENT_TITLE": "Experiment",
|
||||
"OBJECTIVE": default_text,
|
||||
"MATERIALS": default_text,
|
||||
"PROCEDURE": default_text,
|
||||
"OBSERVATIONS": default_text,
|
||||
"INSTITUTION": "Doe Institute",
|
||||
"PRESENTER": "Jane Doe",
|
||||
"AGENDA": default_text,
|
||||
"KEY_POINTS": default_text,
|
||||
"DATA_VISUALS": default_text,
|
||||
}
|
||||
|
||||
def replace(match: re.Match[str]) -> str:
|
||||
key = match.group(1).strip()
|
||||
return replacements.get(key, default_text)
|
||||
|
||||
return re.sub(r"<<([A-Z0-9_]+)>>", replace, template_latex)
|
||||
|
||||
|
||||
def generate_template_fill_stream(
|
||||
template_latex: str,
|
||||
document_type: str,
|
||||
outline_text: str,
|
||||
draft_sections: Optional[List[Dict[str, str]]] = None,
|
||||
constraints: Optional[Dict[str, Any]] = None,
|
||||
style_profile: Optional[Dict[str, Any]] = None,
|
||||
):
|
||||
"""Fill a LaTeX template by replacing placeholders."""
|
||||
if CLIENT_MODE == "langchain":
|
||||
constraints_text = ""
|
||||
if constraints:
|
||||
tone = constraints.get("tone")
|
||||
audience = constraints.get("audience")
|
||||
pages = constraints.get("pageCount")
|
||||
constraints_text = f"Tone: {tone}. Audience: {audience}. Target pages: {pages}."
|
||||
style_text = ""
|
||||
if style_profile:
|
||||
font = style_profile.get("font_preference")
|
||||
layout = style_profile.get("layout_preference")
|
||||
accent = style_profile.get("color_accent")
|
||||
style_text = f"Style preferences: font={font}, layout={layout}, accent={accent}."
|
||||
if draft_sections:
|
||||
constraints_text = f"{constraints_text}\nUse the section content to inform placeholder values."
|
||||
system_prompt = (
|
||||
"You are a LaTeX template filler.\n"
|
||||
"Return the full LaTeX document with placeholders filled.\n"
|
||||
"Rules:\n"
|
||||
"1) Only replace placeholders like <<PLACEHOLDER>>.\n"
|
||||
"2) Do not change any other LaTeX layout/commands.\n"
|
||||
"3) Output ONLY LaTeX (no markdown).\n"
|
||||
"4) If you add color, use the accent token name 'accent'.\n"
|
||||
f"{constraints_text}\n"
|
||||
f"{style_text}\n"
|
||||
"Keep the final output within the target page count.\n"
|
||||
)
|
||||
messages: List[Dict[str, Any]] = [
|
||||
{"role": "system", "content": system_prompt},
|
||||
{"role": "user", "content": f"Document type: {document_type}"},
|
||||
{"role": "user", "content": f"Outline/context:\n{outline_text}"},
|
||||
{"role": "user", "content": f"Template:\n{template_latex}"},
|
||||
]
|
||||
if draft_sections:
|
||||
messages.append(
|
||||
{
|
||||
"role": "user",
|
||||
"content": f"Section content (JSON):\n{json.dumps(draft_sections, ensure_ascii=True)}",
|
||||
}
|
||||
)
|
||||
|
||||
try:
|
||||
llm = get_chat_model(SMART_MODEL, streaming=True)
|
||||
if llm:
|
||||
start = time.perf_counter()
|
||||
total_chars = 0
|
||||
chunk_count = 0
|
||||
first_chunk = None
|
||||
for chunk in llm.stream(to_lc_messages(messages)):
|
||||
if chunk.content:
|
||||
if first_chunk is None:
|
||||
first_chunk = time.perf_counter()
|
||||
chunk_count += 1
|
||||
total_chars += len(str(chunk.content))
|
||||
yield chunk.content
|
||||
elapsed = time.perf_counter() - start
|
||||
logger.info(
|
||||
"[AI] template-fill-stream model=%s elapsed=%.2fs first_chunk=%.2fs chunks=%s chars=%s",
|
||||
SMART_MODEL,
|
||||
elapsed,
|
||||
(first_chunk - start) if first_chunk else -1.0,
|
||||
chunk_count,
|
||||
total_chars,
|
||||
)
|
||||
return
|
||||
except Exception as exc:
|
||||
logger.error("[AI] Template fill failed, falling back: %s", exc)
|
||||
|
||||
draft_text = None
|
||||
if draft_sections:
|
||||
draft_text = "\n".join(
|
||||
f"{section.get('label', 'Section')}: {section.get('value', '')}"
|
||||
for section in draft_sections
|
||||
)
|
||||
filled = _fallback_template_fill(template_latex, outline_text, draft_text)
|
||||
chunk_size = 200
|
||||
for i in range(0, len(filled), chunk_size):
|
||||
yield filled[i : i + chunk_size]
|
||||
|
||||
|
||||
def generate_latex_with_llm(
|
||||
prompt: str,
|
||||
history: List[Dict[str, str]],
|
||||
style_profile: Dict[str, Any],
|
||||
document_type: str,
|
||||
template_hint: Optional[str] = None,
|
||||
current_latex: Optional[str] = None,
|
||||
structured_brief: Optional[str] = None,
|
||||
edit_mode: bool = False,
|
||||
) -> str:
|
||||
"""Call the LLM (when available) or fall back to deterministic templates."""
|
||||
if CLIENT_MODE == "langchain":
|
||||
messages: List[Dict[str, Any]] = [{"role": "system", "content": latex_system_prompt(style_profile, document_type, template_hint)}]
|
||||
messages.extend(history)
|
||||
messages.extend(latex_context_messages(template_hint, current_latex, structured_brief))
|
||||
messages.append({"role": "user", "content": prompt})
|
||||
|
||||
try:
|
||||
logger.info(
|
||||
"[AI] Using live model=%s doc_type=%s template_hint=%s",
|
||||
SMART_MODEL,
|
||||
document_type,
|
||||
"yes" if template_hint else "no",
|
||||
)
|
||||
llm = get_chat_model(SMART_MODEL)
|
||||
if llm:
|
||||
start = time.perf_counter()
|
||||
response = llm.invoke(to_lc_messages(messages))
|
||||
elapsed = time.perf_counter() - start
|
||||
content = response.content or ""
|
||||
usage = getattr(response, "usage_metadata", None)
|
||||
logger.info(
|
||||
"[AI] latex-generate model=%s elapsed=%.2fs chars=%s usage=%s",
|
||||
SMART_MODEL,
|
||||
elapsed,
|
||||
len(str(content)),
|
||||
usage,
|
||||
)
|
||||
return content
|
||||
except Exception as exc:
|
||||
logger.error("[AI] LangChain generation failed, falling back to mock: %s", exc)
|
||||
|
||||
lower_prompt = prompt.lower()
|
||||
|
||||
if "invoice" in lower_prompt:
|
||||
save_user_style("default_user", {"last_doc_type": "invoice"})
|
||||
details = structured_brief or prompt or "Invoice details provided by user."
|
||||
return r"""\documentclass{article}
|
||||
\usepackage[utf8]{inputenc}
|
||||
\usepackage{geometry}
|
||||
\geometry{a4paper, margin=1in}
|
||||
|
||||
\begin{document}
|
||||
|
||||
\begin{center}
|
||||
{\LARGE \textbf{INVOICE}}\\[0.5cm]
|
||||
\#1023\\
|
||||
\today
|
||||
\end{center}
|
||||
|
||||
\section*{Bill To:}
|
||||
Client Name \\
|
||||
123 Business Rd.
|
||||
|
||||
\section*{Items}
|
||||
\begin{tabular}{lr}
|
||||
\textbf{Service} & \textbf{Amount} \\
|
||||
\hline
|
||||
% Replace with your line items
|
||||
Description & \$0.00 \\
|
||||
Description & \$0.00 \\
|
||||
\hline
|
||||
\textbf{Total Due} & \textbf{\$0.00} \\
|
||||
\end{tabular}
|
||||
|
||||
\section*{Notes}
|
||||
""" + details + r"""
|
||||
|
||||
\end{document}"""
|
||||
|
||||
if "resume" in lower_prompt or "cv" in lower_prompt:
|
||||
save_user_style("default_user", {"last_doc_type": "resume"})
|
||||
details = structured_brief or prompt or "Resume details provided by user."
|
||||
return r"""\documentclass{article}
|
||||
\usepackage[utf8]{inputenc}
|
||||
\usepackage{geometry}
|
||||
\geometry{a4paper, margin=0.75in}
|
||||
|
||||
\begin{document}
|
||||
|
||||
\section*{Details}
|
||||
""" + details + r"""
|
||||
|
||||
\end{document}"""
|
||||
|
||||
base_doc = r"""\documentclass{article}
|
||||
\usepackage[utf8]{inputenc}
|
||||
\usepackage{geometry}
|
||||
\geometry{a4paper, margin=1in}
|
||||
|
||||
\begin{document}
|
||||
|
||||
"""
|
||||
content = structured_brief or prompt or ""
|
||||
base_doc += r"""\section*{Document}
|
||||
|
||||
""" + content + r"""
|
||||
|
||||
\end{document}"""
|
||||
|
||||
return base_doc
|
||||
|
||||
|
||||
def generate_latex_with_llm_stream(
|
||||
prompt: str,
|
||||
history: List[Dict[str, str]],
|
||||
style_profile: Dict[str, Any],
|
||||
document_type: str,
|
||||
template_hint: Optional[str] = None,
|
||||
current_latex: Optional[str] = None,
|
||||
structured_brief: Optional[str] = None,
|
||||
edit_mode: bool = False,
|
||||
):
|
||||
"""Stream LaTeX generation from LLM, yielding chunks as they arrive."""
|
||||
if not STREAMING_ENABLED:
|
||||
full_latex = generate_latex_with_llm(
|
||||
prompt, history, style_profile, document_type, template_hint, current_latex, structured_brief, edit_mode=edit_mode
|
||||
)
|
||||
chunk_size = 100
|
||||
for i in range(0, len(full_latex), chunk_size):
|
||||
yield full_latex[i:i + chunk_size]
|
||||
return
|
||||
if CLIENT_MODE == "langchain":
|
||||
messages: List[Dict[str, Any]] = [{"role": "system", "content": latex_system_prompt(style_profile, document_type, template_hint)}]
|
||||
messages.extend(history)
|
||||
messages.extend(latex_context_messages(template_hint, current_latex, structured_brief))
|
||||
messages.append({"role": "user", "content": prompt})
|
||||
|
||||
try:
|
||||
logger.info(
|
||||
"[AI] Streaming from model=%s doc_type=%s template_hint=%s",
|
||||
SMART_MODEL,
|
||||
document_type,
|
||||
"yes" if template_hint else "no",
|
||||
)
|
||||
llm = get_chat_model(SMART_MODEL, streaming=True)
|
||||
if llm:
|
||||
start = time.perf_counter()
|
||||
total_chars = 0
|
||||
chunk_count = 0
|
||||
first_chunk = None
|
||||
for chunk in llm.stream(to_lc_messages(messages)):
|
||||
if chunk.content:
|
||||
if first_chunk is None:
|
||||
first_chunk = time.perf_counter()
|
||||
chunk_count += 1
|
||||
total_chars += len(str(chunk.content))
|
||||
yield chunk.content
|
||||
elapsed = time.perf_counter() - start
|
||||
logger.info(
|
||||
"[AI] latex-stream model=%s elapsed=%.2fs first_chunk=%.2fs chunks=%s chars=%s",
|
||||
SMART_MODEL,
|
||||
elapsed,
|
||||
(first_chunk - start) if first_chunk else -1.0,
|
||||
chunk_count,
|
||||
total_chars,
|
||||
)
|
||||
return
|
||||
except Exception as exc:
|
||||
logger.error("[AI] LangChain streaming failed, falling back to mock: %s", exc)
|
||||
|
||||
# Fallback to non-streaming for mock mode
|
||||
full_latex = generate_latex_with_llm(
|
||||
prompt, history, style_profile, document_type, template_hint, current_latex, structured_brief, edit_mode=edit_mode
|
||||
)
|
||||
# Simulate streaming by yielding chunks
|
||||
chunk_size = 100
|
||||
for i in range(0, len(full_latex), chunk_size):
|
||||
yield full_latex[i:i + chunk_size]
|
||||
|
||||
|
||||
__all__ = [
|
||||
"generate_outline_with_llm",
|
||||
"generate_section_draft",
|
||||
"generate_field_values",
|
||||
"generate_latex_with_llm",
|
||||
"generate_latex_with_llm_stream",
|
||||
"generate_template_fill_stream",
|
||||
]
|
||||
@@ -0,0 +1,964 @@
|
||||
import os
|
||||
import mimetypes
|
||||
import subprocess
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
from datetime import datetime
|
||||
from typing import Any, Dict, List, Optional, Tuple
|
||||
import re
|
||||
import time
|
||||
import threading
|
||||
import queue
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
|
||||
from flask import Flask, jsonify, request, send_file, Response, stream_with_context
|
||||
from flask_cors import CORS
|
||||
import json
|
||||
|
||||
from ai_generation import (
|
||||
generate_latex_with_llm,
|
||||
generate_latex_with_llm_stream,
|
||||
generate_outline_with_llm,
|
||||
generate_section_draft,
|
||||
generate_field_values,
|
||||
generate_template_fill_stream,
|
||||
)
|
||||
from briefs import gather_brief, _preprocess_intent
|
||||
from config import (
|
||||
CLIENT_MODE,
|
||||
SMART_MODEL,
|
||||
OUTPUT_DIR,
|
||||
ASSETS_DIR,
|
||||
TEMPLATE_DIR,
|
||||
JAVA_BACKEND_URL,
|
||||
PREVIEW_MAX_INFLIGHT,
|
||||
get_chat_model,
|
||||
logger,
|
||||
)
|
||||
from langchain_utils import to_lc_messages
|
||||
from document_types import detect_document_type
|
||||
from latex_utils import apply_style_overrides, clean_generated_latex
|
||||
from pdf_utils import compile_latex_to_pdf, render_pdf_to_images
|
||||
from pdf_text_editor import convert_pdf_to_text_editor_document
|
||||
from storage import (
|
||||
load_user_style,
|
||||
load_user_templates,
|
||||
load_versions,
|
||||
save_user_style,
|
||||
save_user_template,
|
||||
save_version,
|
||||
)
|
||||
from styles import update_style_profile_from_prompt
|
||||
from vision import vision_layout_from_images
|
||||
from prompts import pdf_qa_system_prompt
|
||||
|
||||
|
||||
app = Flask(__name__)
|
||||
CORS(app)
|
||||
|
||||
|
||||
@app.before_request
|
||||
def log_job_request_sequence() -> None:
|
||||
job_id = request.headers.get("X-Job-Id")
|
||||
if not job_id:
|
||||
return
|
||||
seq = request.headers.get("X-Job-Seq", "?")
|
||||
total = request.headers.get("X-Job-Total", "?")
|
||||
logger.info("[HTTP] job_id=%s req=%s/%s %s %s", job_id, seq, total, request.method, request.path)
|
||||
|
||||
|
||||
def _json_body() -> Dict[str, Any]:
|
||||
return request.get_json(silent=True) or {}
|
||||
|
||||
def _require_ai_enabled() -> Optional[Any]:
|
||||
if CLIENT_MODE != "langchain":
|
||||
return jsonify({"error": "AI is disabled. Set OPENAI_API_KEY to enable AI features."}), 503
|
||||
return None
|
||||
|
||||
|
||||
def _java_url(path: str) -> str:
|
||||
base = JAVA_BACKEND_URL.rstrip("/")
|
||||
if not path.startswith("/"):
|
||||
path = "/" + path
|
||||
return f"{base}{path}"
|
||||
|
||||
|
||||
def _java_request_json(method: str, path: str, payload: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
|
||||
url = _java_url(path)
|
||||
data = None
|
||||
headers = {"Content-Type": "application/json"}
|
||||
if payload is not None:
|
||||
data = json.dumps(payload).encode("utf-8")
|
||||
req = urllib.request.Request(url, data=data, headers=headers, method=method)
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=10) as resp:
|
||||
body = resp.read().decode("utf-8")
|
||||
return json.loads(body) if body else {}
|
||||
except urllib.error.HTTPError as exc:
|
||||
detail = exc.read().decode("utf-8") if exc.fp else ""
|
||||
logger.error("[JAVA] %s %s failed status=%s detail=%s", method, path, exc.code, detail)
|
||||
raise
|
||||
|
||||
|
||||
def _fetch_ai_session(session_id: str) -> Dict[str, Any]:
|
||||
return _java_request_json("GET", f"/api/v1/ai/create/internal/sessions/{session_id}")
|
||||
|
||||
|
||||
def _update_ai_session(session_id: str, payload: Dict[str, Any]) -> None:
|
||||
_java_request_json("POST", f"/api/v1/ai/create/internal/sessions/{session_id}/update", payload)
|
||||
|
||||
|
||||
def _sanitize_doc_type(value: str) -> str:
|
||||
cleaned = re.sub(r"[^a-zA-Z0-9_]+", "", (value or "").lower())
|
||||
return cleaned or "miscellaneous"
|
||||
|
||||
|
||||
def _select_template(doc_type: str, template_id: Optional[str]) -> Optional[str]:
|
||||
safe_doc_type = _sanitize_doc_type(doc_type)
|
||||
base_dir = Path(TEMPLATE_DIR) / safe_doc_type
|
||||
if not base_dir.exists() or not base_dir.is_dir():
|
||||
return None
|
||||
|
||||
if template_id:
|
||||
safe_template = re.sub(r"[^a-zA-Z0-9_-]+", "", template_id)
|
||||
if safe_template:
|
||||
candidate = base_dir / f"{safe_template}.tex"
|
||||
if candidate.exists():
|
||||
return candidate.read_text(encoding="utf-8", errors="replace")
|
||||
|
||||
default_path = base_dir / "default.tex"
|
||||
if default_path.exists():
|
||||
return default_path.read_text(encoding="utf-8", errors="replace")
|
||||
|
||||
for tex_file in sorted(base_dir.glob("*.tex")):
|
||||
return tex_file.read_text(encoding="utf-8", errors="replace")
|
||||
return None
|
||||
|
||||
|
||||
@app.route("/api/intent/check", methods=["POST"])
|
||||
def intent_check() -> Any:
|
||||
try:
|
||||
data = _json_body()
|
||||
prompt: str = data.get("prompt", "")
|
||||
history: List[Dict[str, str]] = data.get("conversationHistory") or []
|
||||
current_latex: Optional[str] = data.get("currentLatex")
|
||||
current_pdf_url: Optional[str] = data.get("currentPdfUrl")
|
||||
doc_type = detect_document_type(prompt, current_latex)
|
||||
intent = _preprocess_intent(prompt, history, bool(current_pdf_url), current_latex)
|
||||
intent["documentType"] = doc_type
|
||||
intent["hasPdf"] = bool(current_pdf_url)
|
||||
return jsonify(intent)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logger.error("[INTENT] intent_check failed: %s", exc, exc_info=True)
|
||||
return jsonify({"wants_pdf": True, "has_enough_info": True, "allow_makeup": False, "reason": str(exc)}), 500
|
||||
|
||||
|
||||
@app.route("/api/pdf/answer", methods=["POST"])
|
||||
def pdf_answer() -> Any:
|
||||
data = _json_body()
|
||||
pdf_url = data.get("pdfUrl")
|
||||
question = data.get("question")
|
||||
if not pdf_url or not question:
|
||||
return jsonify({"error": "Missing pdfUrl or question"}), 400
|
||||
|
||||
filename = os.path.basename(pdf_url.split("?")[0])
|
||||
if not filename.lower().endswith(".pdf"):
|
||||
return jsonify({"error": "Invalid pdf file"}), 400
|
||||
|
||||
pdf_path = os.path.join(OUTPUT_DIR, filename)
|
||||
if not os.path.exists(pdf_path):
|
||||
return jsonify({"error": "PDF not found"}), 404
|
||||
|
||||
try:
|
||||
doc = convert_pdf_to_text_editor_document(pdf_path)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logger.error("[PDF-ANSWER] failed to parse pdf: %s", exc, exc_info=True)
|
||||
return jsonify({"error": "Failed to read PDF content"}), 500
|
||||
|
||||
pages = doc.get("document", {}).get("pages", []) if doc else []
|
||||
snippets: List[str] = []
|
||||
for page in pages:
|
||||
for elem in page.get("textElements", []) or []:
|
||||
text = elem.get("text")
|
||||
if text:
|
||||
snippets.append(str(text))
|
||||
if not snippets:
|
||||
return jsonify({"error": "No readable text in PDF"}), 400
|
||||
|
||||
# Normalize and limit context
|
||||
context = " ".join(snippets)
|
||||
context = " ".join(context.split()) # normalize whitespace
|
||||
max_context = 10000
|
||||
if len(context) > max_context:
|
||||
context = context[:max_context]
|
||||
|
||||
# Heuristic helpers
|
||||
def _sentences(text: str) -> List[str]:
|
||||
return [s.strip() for s in re.split(r"(?<=[.!?])\s+", text) if s.strip()]
|
||||
|
||||
def _heuristic_summary(text: str, limit: int = 480) -> str:
|
||||
sentences = _sentences(text)
|
||||
hits = [s for s in sentences if re.search(r"\b(difficult|challenge|problem|issue|hard)\b", s, re.IGNORECASE)]
|
||||
chosen = hits[:3] if hits else sentences[:3]
|
||||
summary = " ".join(chosen).strip()
|
||||
return summary[:limit] + ("…" if len(summary) > limit else "")
|
||||
|
||||
def _heuristic_first_difficulty(text: str) -> str:
|
||||
sentences = _sentences(text)
|
||||
for s in sentences:
|
||||
if re.search(r"\b(difficult|challenge|problem|issue|hard)\b", s, re.IGNORECASE):
|
||||
return s
|
||||
return sentences[0] if sentences else "No difficulty found in PDF text."
|
||||
|
||||
if CLIENT_MODE != "langchain":
|
||||
return jsonify({"error": "PDF Q&A unavailable (no AI client configured)."}), 503
|
||||
|
||||
model_name = SMART_MODEL
|
||||
system_prompt = pdf_qa_system_prompt()
|
||||
user_prompt = f"Question: {question}\n\nPDF text:\n{context}"
|
||||
try:
|
||||
llm = get_chat_model(model_name, max_tokens=220)
|
||||
if not llm:
|
||||
return jsonify({"error": "PDF Q&A unavailable (no AI client configured)."}), 503
|
||||
start = time.perf_counter()
|
||||
response = llm.invoke(
|
||||
to_lc_messages(
|
||||
[
|
||||
{"role": "system", "content": system_prompt},
|
||||
{"role": "user", "content": user_prompt},
|
||||
]
|
||||
)
|
||||
)
|
||||
elapsed = time.perf_counter() - start
|
||||
content = response.content or ""
|
||||
usage = getattr(response, "usage_metadata", None)
|
||||
logger.info(
|
||||
"[PDF-ANSWER] model=%s elapsed=%.2fs chars=%s usage=%s",
|
||||
model_name,
|
||||
elapsed,
|
||||
len(str(content)),
|
||||
usage,
|
||||
)
|
||||
answer = response.content
|
||||
if not answer or not str(answer).strip():
|
||||
answer = _heuristic_summary(context)
|
||||
# If model parrots title/metadata, replace with heuristic
|
||||
title_like = re.match(r"^why pdfs|^minimalist|^author:", answer.strip(), re.IGNORECASE) if answer else None
|
||||
normalized_answer = re.sub(r"\s+", " ", answer or "").strip().lower()
|
||||
normalized_context = re.sub(r"\s+", " ", context).strip().lower()
|
||||
copied_context = bool(normalized_answer) and normalized_answer in normalized_context
|
||||
if title_like or copied_context:
|
||||
answer = _heuristic_summary(context)
|
||||
return jsonify({"answer": answer, "mode": "model"})
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logger.error("[PDF-ANSWER] model failed: %s", exc, exc_info=True)
|
||||
answer = _heuristic_first_difficulty(context)
|
||||
return jsonify({"answer": answer, "mode": "heuristic"})
|
||||
|
||||
|
||||
@app.route("/api/generate", methods=["POST"])
|
||||
def generate() -> Any:
|
||||
"""Generate LaTeX + PDF in a single call."""
|
||||
data = _json_body()
|
||||
user_id = data.get("userId", "default_user")
|
||||
prompt: str = data.get("prompt", "")
|
||||
history: List[Dict[str, str]] = data.get("conversationHistory") or []
|
||||
current_latex: str | None = data.get("currentLatex")
|
||||
skip_template = bool(data.get("skipTemplate"))
|
||||
force_new_document: Optional[bool] = data.get("forceNewDocument")
|
||||
edit_mode = bool(current_latex) and not bool(force_new_document)
|
||||
latex_source = current_latex if edit_mode else None
|
||||
|
||||
style_profile = update_style_profile_from_prompt(user_id, prompt)
|
||||
|
||||
doc_type = detect_document_type(prompt, latex_source or current_latex)
|
||||
brief = gather_brief(doc_type, prompt, history)
|
||||
if brief.get("needsInfo"):
|
||||
logger.info("[REQ] brief incomplete doc_type=%s missing=%s", doc_type, brief.get("missing"))
|
||||
return jsonify(
|
||||
{
|
||||
"needsInfo": True,
|
||||
"message": brief.get("message"),
|
||||
"missing": brief.get("missing", []),
|
||||
"collected": brief.get("collected", {}),
|
||||
"documentType": doc_type,
|
||||
}
|
||||
)
|
||||
|
||||
templates = load_user_templates(user_id)
|
||||
template_hint = None if (skip_template or edit_mode) else templates.get(doc_type)
|
||||
|
||||
logger.info(
|
||||
"[REQ] user=%s doc_type=%s template=%s history_len=%s current_latex=%s skip_template=%s edit_mode=%s",
|
||||
user_id,
|
||||
doc_type,
|
||||
"yes" if template_hint else "no",
|
||||
len(history),
|
||||
bool(current_latex),
|
||||
skip_template,
|
||||
edit_mode,
|
||||
)
|
||||
|
||||
mode = "live" if CLIENT_MODE == "langchain" else "mock"
|
||||
latex_code_raw = generate_latex_with_llm(
|
||||
prompt,
|
||||
history,
|
||||
style_profile,
|
||||
doc_type,
|
||||
template_hint,
|
||||
latex_source,
|
||||
brief.get("structured_brief"),
|
||||
edit_mode=edit_mode,
|
||||
)
|
||||
latex_code = apply_style_overrides(clean_generated_latex(latex_code_raw), style_profile)
|
||||
|
||||
doc_type = detect_document_type(prompt, latex_code)
|
||||
save_user_style(user_id, {"last_doc_type": doc_type})
|
||||
if not skip_template and not edit_mode:
|
||||
save_user_template(user_id, doc_type, latex_code)
|
||||
elif skip_template:
|
||||
logger.info("[TEMPLATE] skip flag set; not persisting template for %s", doc_type)
|
||||
else:
|
||||
logger.info("[TEMPLATE] edit mode active; not updating template for %s", doc_type)
|
||||
template_used = bool(template_hint)
|
||||
|
||||
job_id = str(uuid.uuid4())
|
||||
pdf_path = compile_latex_to_pdf(latex_code, job_id)
|
||||
if pdf_path and os.path.exists(pdf_path):
|
||||
pdf_url = f"/output/{job_id}.pdf"
|
||||
version_entry = {
|
||||
"id": job_id,
|
||||
"prompt": prompt,
|
||||
"documentType": doc_type,
|
||||
"pdfUrl": pdf_url,
|
||||
"latex": latex_code,
|
||||
"createdAt": datetime.utcnow().isoformat() + "Z",
|
||||
"styleProfile": style_profile,
|
||||
"templateUsed": template_used,
|
||||
"editMode": edit_mode,
|
||||
}
|
||||
save_version(user_id, version_entry)
|
||||
|
||||
logger.info("[OK] job_id=%s doc_type=%s pdf_url=%s", job_id, doc_type, pdf_url)
|
||||
return jsonify(
|
||||
{
|
||||
"latex": latex_code,
|
||||
"pdfUrl": pdf_url,
|
||||
"documentType": doc_type,
|
||||
"message": f"Generated {doc_type} successfully! mode={mode}",
|
||||
"version": version_entry,
|
||||
"styleProfile": style_profile,
|
||||
"mode": mode,
|
||||
"templateUsed": template_used,
|
||||
"editingExisting": edit_mode,
|
||||
}
|
||||
)
|
||||
|
||||
logger.error("[FAIL] job_id=%s doc_type=%s compile_failed", job_id, doc_type)
|
||||
return jsonify(
|
||||
{
|
||||
"error": "LaTeX compilation failed. Check logs.",
|
||||
"latex": latex_code,
|
||||
"mode": mode,
|
||||
"editingExisting": edit_mode,
|
||||
}
|
||||
), 500
|
||||
|
||||
|
||||
@app.route("/api/generate_stream", methods=["POST"])
|
||||
def generate_stream() -> Any:
|
||||
"""Stream LaTeX generation and compile PDFs incrementally."""
|
||||
try:
|
||||
data = _json_body()
|
||||
user_id = data.get("userId", "default_user")
|
||||
prompt: str = data.get("prompt", "")
|
||||
history: List[Dict[str, str]] = data.get("conversationHistory") or []
|
||||
current_latex: str | None = data.get("currentLatex")
|
||||
skip_template = bool(data.get("skipTemplate"))
|
||||
force_new_document: Optional[bool] = data.get("forceNewDocument")
|
||||
edit_mode = bool(current_latex) and not bool(force_new_document)
|
||||
latex_source = current_latex if edit_mode else None
|
||||
style_profile = update_style_profile_from_prompt(user_id, prompt)
|
||||
doc_type = detect_document_type(prompt, latex_source or current_latex)
|
||||
brief = gather_brief(doc_type, prompt, history, current_latex, bool(current_latex))
|
||||
logger.info(
|
||||
"[STREAM] brief gate doc_type=%s needsInfo=%s missing=%s allowFabrication=%s",
|
||||
doc_type,
|
||||
brief.get("needsInfo"),
|
||||
brief.get("missing"),
|
||||
brief.get("allowFabrication"),
|
||||
)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logger.error("[STREAM] Failed to prepare generation request: %s", exc, exc_info=True)
|
||||
return jsonify({"error": "Failed to start generation", "detail": str(exc)}), 500
|
||||
|
||||
if brief.get("needsInfo"):
|
||||
# Return a normal 200 with guidance so the assistant can ask follow-up questions
|
||||
return jsonify(
|
||||
{
|
||||
"needsInfo": True,
|
||||
"message": brief.get("message"),
|
||||
"missing": brief.get("missing", []),
|
||||
"collected": brief.get("collected", {}),
|
||||
"documentType": doc_type,
|
||||
"allowFabrication": brief.get("allowFabrication", False),
|
||||
}
|
||||
)
|
||||
|
||||
templates = load_user_templates(user_id)
|
||||
template_hint = None if (skip_template or edit_mode) else templates.get(doc_type)
|
||||
|
||||
job_id = str(uuid.uuid4())
|
||||
accumulated_latex = ""
|
||||
last_compile_idx = 0
|
||||
compile_interval = 250 # Compile every ~250 characters for faster previews
|
||||
compile_time_budget = 2.0 # Or every ~2 seconds, whichever comes first
|
||||
last_compile_time = time.perf_counter()
|
||||
last_heartbeat_time = time.perf_counter()
|
||||
stream_start = time.perf_counter()
|
||||
first_chunk_time: Optional[float] = None
|
||||
last_chunk_time: Optional[float] = None
|
||||
total_chunk_chars = 0
|
||||
total_chunks = 0
|
||||
|
||||
preview_executor = ThreadPoolExecutor(max_workers=2)
|
||||
preview_tasks: Dict[str, Tuple[Any, int]] = {}
|
||||
chunk_queue: "queue.Queue[Tuple[str, Optional[str]]]" = queue.Queue()
|
||||
|
||||
def stream_latex():
|
||||
try:
|
||||
for chunk in generate_latex_with_llm_stream(
|
||||
prompt,
|
||||
history,
|
||||
style_profile,
|
||||
doc_type,
|
||||
template_hint,
|
||||
latex_source,
|
||||
brief.get("structured_brief"),
|
||||
edit_mode=edit_mode,
|
||||
):
|
||||
chunk_queue.put(("chunk", chunk))
|
||||
except Exception as exc:
|
||||
logger.error("[STREAM] LLM streaming failed: %s", exc, exc_info=True)
|
||||
chunk_queue.put(("error", str(exc)))
|
||||
finally:
|
||||
chunk_queue.put(("done", None))
|
||||
|
||||
def submit_preview(latex: str, progress: int) -> None:
|
||||
if len(preview_tasks) >= PREVIEW_MAX_INFLIGHT:
|
||||
return
|
||||
preview_job_id = f"{job_id}-preview-{progress}"
|
||||
|
||||
def _run_compile() -> Optional[str]:
|
||||
return compile_latex_to_pdf(latex, preview_job_id, log_errors=False)
|
||||
|
||||
fut = preview_executor.submit(_run_compile)
|
||||
preview_tasks[preview_job_id] = (fut, progress)
|
||||
|
||||
def drain_previews():
|
||||
nonlocal last_compile_idx, last_compile_time
|
||||
completed = []
|
||||
for pid, (fut, progress) in list(preview_tasks.items()):
|
||||
if fut.done():
|
||||
completed.append(pid)
|
||||
try:
|
||||
pdf_path = fut.result()
|
||||
if pdf_path and os.path.exists(pdf_path):
|
||||
pdf_url = f"/output/{pid}.pdf"
|
||||
yield f"data: {json.dumps({'type': 'pdf_update', 'pdfUrl': pdf_url, 'progress': progress})}\n\n"
|
||||
last_compile_idx = progress
|
||||
last_compile_time = time.perf_counter()
|
||||
except Exception as e: # noqa: BLE001
|
||||
logger.error("[STREAM] Preview compile failed (async): %s", e, exc_info=True)
|
||||
for pid in completed:
|
||||
preview_tasks.pop(pid, None)
|
||||
|
||||
def generate():
|
||||
nonlocal accumulated_latex, last_compile_time, last_heartbeat_time
|
||||
nonlocal total_chunk_chars, total_chunks, first_chunk_time, last_chunk_time
|
||||
|
||||
# Send initial metadata
|
||||
yield f"data: {json.dumps({'type': 'start', 'jobId': job_id, 'documentType': doc_type, 'editingExisting': edit_mode})}\n\n"
|
||||
|
||||
streamer = threading.Thread(target=stream_latex, daemon=True)
|
||||
streamer.start()
|
||||
|
||||
try:
|
||||
while True:
|
||||
try:
|
||||
item_type, payload = chunk_queue.get(timeout=0.5)
|
||||
except queue.Empty:
|
||||
now = time.perf_counter()
|
||||
if now - last_heartbeat_time >= 1.0:
|
||||
yield f"data: {json.dumps({'type': 'heartbeat', 'ts': now})}\n\n"
|
||||
last_heartbeat_time = now
|
||||
yield from drain_previews()
|
||||
continue
|
||||
|
||||
if item_type == "chunk":
|
||||
chunk = payload or ""
|
||||
accumulated_latex += chunk
|
||||
total_chunks += 1
|
||||
total_chunk_chars += len(chunk)
|
||||
last_chunk_time = time.perf_counter()
|
||||
if first_chunk_time is None:
|
||||
first_chunk_time = last_chunk_time
|
||||
yield f"data: {json.dumps({'type': 'latex_chunk', 'chunk': chunk, 'accumulated': accumulated_latex})}\n\n"
|
||||
|
||||
elapsed = time.perf_counter() - last_compile_time
|
||||
if (len(accumulated_latex) - last_compile_idx >= compile_interval) or elapsed >= compile_time_budget:
|
||||
compile_latex = accumulated_latex
|
||||
if "\\begin{document}" in compile_latex and "\\end{document}" not in compile_latex:
|
||||
if compile_latex.count("\\begin{") > compile_latex.count("\\end{"):
|
||||
temp_latex = compile_latex
|
||||
last_begin = compile_latex.rfind("\\begin{")
|
||||
if last_begin != -1:
|
||||
env_start = last_begin + len("\\begin{")
|
||||
env_end = compile_latex.find("}", env_start)
|
||||
if env_end != -1:
|
||||
env_name = compile_latex[env_start:env_end]
|
||||
temp_latex += f"\\end{{{env_name}}}\n"
|
||||
temp_latex += "\\end{document}\n"
|
||||
compile_latex = temp_latex
|
||||
else:
|
||||
compile_latex += "\\end{document}\n"
|
||||
|
||||
if "\\begin{document}" in compile_latex and "\\end{document}" in compile_latex:
|
||||
submit_preview(compile_latex, len(accumulated_latex))
|
||||
|
||||
now = time.perf_counter()
|
||||
if now - last_heartbeat_time >= 1.0:
|
||||
yield f"data: {json.dumps({'type': 'heartbeat', 'ts': now})}\n\n"
|
||||
last_heartbeat_time = now
|
||||
yield from drain_previews()
|
||||
|
||||
elif item_type == "error":
|
||||
message = payload or "Streaming failed"
|
||||
yield f"data: {json.dumps({'type': 'error', 'message': message})}\n\n"
|
||||
break
|
||||
elif item_type == "done":
|
||||
break
|
||||
|
||||
stream_end = time.perf_counter()
|
||||
if first_chunk_time is None:
|
||||
logger.info(
|
||||
"[STREAM] LLM finished with no chunks job_id=%s elapsed=%.2fs",
|
||||
job_id,
|
||||
stream_end - stream_start,
|
||||
)
|
||||
else:
|
||||
logger.info(
|
||||
"[STREAM] LLM stats job_id=%s chunks=%s chars=%s first_chunk=%.2fs last_chunk=%.2fs elapsed=%.2fs",
|
||||
job_id,
|
||||
total_chunks,
|
||||
total_chunk_chars,
|
||||
first_chunk_time - stream_start,
|
||||
(last_chunk_time or stream_end) - stream_start,
|
||||
stream_end - stream_start,
|
||||
)
|
||||
|
||||
# Final compilation with complete LaTeX
|
||||
latex_code = apply_style_overrides(clean_generated_latex(accumulated_latex), style_profile)
|
||||
final_doc_type = detect_document_type(prompt, latex_code)
|
||||
save_user_style(user_id, {"last_doc_type": final_doc_type})
|
||||
if not skip_template and not edit_mode:
|
||||
save_user_template(user_id, final_doc_type, latex_code)
|
||||
elif skip_template:
|
||||
logger.info("[TEMPLATE] skip flag set; not persisting template for %s", final_doc_type)
|
||||
else:
|
||||
logger.info("[TEMPLATE] edit mode active; not updating template for %s", final_doc_type)
|
||||
template_used = bool(template_hint)
|
||||
|
||||
pdf_path = compile_latex_to_pdf(latex_code, job_id)
|
||||
if pdf_path and os.path.exists(pdf_path):
|
||||
pdf_url = f"/output/{job_id}.pdf"
|
||||
version_entry = {
|
||||
"id": job_id,
|
||||
"prompt": prompt,
|
||||
"documentType": final_doc_type,
|
||||
"pdfUrl": pdf_url,
|
||||
"latex": latex_code,
|
||||
"createdAt": datetime.utcnow().isoformat() + "Z",
|
||||
"styleProfile": style_profile,
|
||||
"templateUsed": template_used,
|
||||
"editMode": edit_mode,
|
||||
}
|
||||
save_version(user_id, version_entry)
|
||||
|
||||
yield f"data: {json.dumps({'type': 'complete', 'pdfUrl': pdf_url, 'latex': latex_code, 'version': version_entry, 'documentType': final_doc_type, 'templateUsed': template_used, 'styleProfile': style_profile, 'editingExisting': edit_mode})}\n\n"
|
||||
else:
|
||||
logger.error("[STREAM] Final PDF compilation failed for job_id=%s", job_id)
|
||||
yield f"data: {json.dumps({'type': 'error', 'message': 'Final PDF compilation failed'})}\n\n"
|
||||
|
||||
except Exception as e: # noqa: BLE001
|
||||
logger.error("[STREAM] Generation error job_id=%s: %s", job_id, e, exc_info=True)
|
||||
yield f"data: {json.dumps({'type': 'error', 'message': str(e)})}\n\n"
|
||||
finally:
|
||||
try:
|
||||
preview_executor.shutdown(wait=False, cancel_futures=True)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
try:
|
||||
return Response(
|
||||
stream_with_context(generate()),
|
||||
mimetype="text/event-stream",
|
||||
headers={
|
||||
"Cache-Control": "no-cache",
|
||||
"X-Accel-Buffering": "no",
|
||||
},
|
||||
)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logger.error("[STREAM] Failed to start response: %s", exc, exc_info=True)
|
||||
return jsonify({"error": "Unable to start streaming response", "detail": str(exc)}), 500
|
||||
|
||||
|
||||
@app.route("/api/create/sessions/<session_id>/stream", methods=["GET"])
|
||||
def create_stream(session_id: str) -> Any:
|
||||
disabled = _require_ai_enabled()
|
||||
if disabled:
|
||||
return disabled
|
||||
phase = (request.args.get("phase") or "outline").strip().lower()
|
||||
try:
|
||||
session = _fetch_ai_session(session_id)
|
||||
except Exception: # noqa: BLE001
|
||||
return jsonify({"error": "Session not found"}), 404
|
||||
|
||||
user_id = session.get("userId", "default_user")
|
||||
prompt = session.get("promptLatest") or session.get("promptInitial") or ""
|
||||
doc_type = session.get("docType") or detect_document_type(prompt, None)
|
||||
template_id = session.get("templateId")
|
||||
outline_text = session.get("outlineText") or ""
|
||||
constraints = session.get("outlineConstraints")
|
||||
if isinstance(constraints, str) and constraints.strip():
|
||||
try:
|
||||
constraints = json.loads(constraints)
|
||||
except json.JSONDecodeError:
|
||||
constraints = None
|
||||
draft_sections_raw = session.get("draftSections")
|
||||
draft_sections = None
|
||||
if isinstance(draft_sections_raw, list):
|
||||
draft_sections = draft_sections_raw
|
||||
elif isinstance(draft_sections_raw, str) and draft_sections_raw.strip():
|
||||
try:
|
||||
draft_sections = json.loads(draft_sections_raw)
|
||||
except json.JSONDecodeError:
|
||||
draft_sections = None
|
||||
style_profile = load_user_style(user_id)
|
||||
|
||||
def sse(data: Dict[str, Any]) -> str:
|
||||
return f"data: {json.dumps(data)}\n\n"
|
||||
|
||||
def generate():
|
||||
yield sse({"type": "phase_changed", "phase": phase})
|
||||
|
||||
if phase == "outline":
|
||||
outline = generate_outline_with_llm(prompt, doc_type, constraints)
|
||||
_update_ai_session(
|
||||
session_id,
|
||||
{
|
||||
"outlineText": outline,
|
||||
"outlineConstraints": json.dumps(constraints, ensure_ascii=True) if constraints else None,
|
||||
"docType": doc_type,
|
||||
"status": "OUTLINE_PENDING",
|
||||
},
|
||||
)
|
||||
yield sse({"type": "outline_ready", "outlineText": outline})
|
||||
yield sse({"type": "phase_complete", "phase": "outline"})
|
||||
return
|
||||
|
||||
if phase == "draft":
|
||||
base_outline = outline_text or prompt
|
||||
sections = generate_section_draft(prompt, doc_type, base_outline, constraints)
|
||||
_update_ai_session(
|
||||
session_id,
|
||||
{
|
||||
"draftSections": json.dumps(sections, ensure_ascii=True),
|
||||
"outlineConstraints": json.dumps(constraints, ensure_ascii=True) if constraints else None,
|
||||
"docType": doc_type,
|
||||
"status": "DRAFT_READY",
|
||||
},
|
||||
)
|
||||
yield sse({"type": "draft_sections", "sections": sections})
|
||||
yield sse({"type": "phase_complete", "phase": "draft", "sections": sections})
|
||||
return
|
||||
|
||||
if phase == "polish":
|
||||
accumulated = ""
|
||||
template_latex = _select_template(doc_type, template_id)
|
||||
if template_latex:
|
||||
for chunk in generate_template_fill_stream(
|
||||
template_latex,
|
||||
doc_type,
|
||||
outline_text or prompt,
|
||||
draft_sections=draft_sections,
|
||||
constraints=constraints,
|
||||
style_profile=style_profile,
|
||||
):
|
||||
accumulated += chunk
|
||||
yield sse({"type": "latex_delta", "phase": "polish", "delta": chunk})
|
||||
else:
|
||||
section_text = ""
|
||||
if draft_sections:
|
||||
section_text = "\n".join(
|
||||
f"{section.get('label', 'Section')}: {section.get('value', '')}"
|
||||
for section in draft_sections
|
||||
)
|
||||
constraint_text = ""
|
||||
if constraints:
|
||||
tone = constraints.get("tone")
|
||||
audience = constraints.get("audience")
|
||||
pages = constraints.get("pageCount")
|
||||
constraint_text = f"Tone: {tone}. Audience: {audience}. Target pages: {pages}."
|
||||
polish_prompt = (
|
||||
f"Create a polished LaTeX document for a {doc_type}.\n"
|
||||
"Use the provided section content and keep the substance consistent.\n"
|
||||
f"{constraint_text}\n"
|
||||
"Keep the final document within the target page count.\n"
|
||||
)
|
||||
for chunk in generate_latex_with_llm_stream(
|
||||
polish_prompt,
|
||||
[],
|
||||
style_profile,
|
||||
doc_type,
|
||||
None,
|
||||
None,
|
||||
section_text or outline_text or prompt,
|
||||
edit_mode=True,
|
||||
):
|
||||
accumulated += chunk
|
||||
yield sse({"type": "latex_delta", "phase": "polish", "delta": chunk})
|
||||
|
||||
accumulated = apply_style_overrides(accumulated, style_profile)
|
||||
_update_ai_session(
|
||||
session_id,
|
||||
{"polishedLatex": accumulated, "docType": doc_type, "status": "POLISHED_READY"},
|
||||
)
|
||||
|
||||
pdf_job_id = f"{session_id}-polished"
|
||||
pdf_path = compile_latex_to_pdf(accumulated, pdf_job_id, log_errors=False)
|
||||
if pdf_path and os.path.exists(pdf_path):
|
||||
pdf_url = f"/output/{pdf_job_id}.pdf"
|
||||
yield sse({"type": "save_complete", "docId": session_id, "pdfUrl": pdf_url})
|
||||
|
||||
yield sse({"type": "phase_complete", "phase": "polish", "latex": accumulated})
|
||||
return
|
||||
|
||||
yield sse({"type": "error", "message": f"Unknown phase: {phase}"})
|
||||
|
||||
return Response(
|
||||
stream_with_context(generate()),
|
||||
mimetype="text/event-stream",
|
||||
headers={"Cache-Control": "no-cache", "X-Accel-Buffering": "no"},
|
||||
)
|
||||
|
||||
|
||||
@app.route("/api/create/sessions/<session_id>/fields", methods=["POST"])
|
||||
def fill_fields(session_id: str) -> Any:
|
||||
disabled = _require_ai_enabled()
|
||||
if disabled:
|
||||
return disabled
|
||||
try:
|
||||
logger.info("[AI create] fill_fields session_id=%s", session_id)
|
||||
session = _fetch_ai_session(session_id)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logger.warning("[AI create] fill_fields session lookup failed session_id=%s error=%s", session_id, exc)
|
||||
return jsonify({"error": "Session not found"}), 404
|
||||
|
||||
data = _json_body()
|
||||
fields = data.get("fields") or []
|
||||
extra_prompt = data.get("extraPrompt") or ""
|
||||
if not isinstance(fields, list):
|
||||
return jsonify({"error": "Fields must be a list"}), 400
|
||||
|
||||
prompt = session.get("promptLatest") or session.get("promptInitial") or ""
|
||||
if extra_prompt:
|
||||
prompt = f"{prompt}\n{extra_prompt}"
|
||||
doc_type = session.get("docType") or detect_document_type(prompt, None)
|
||||
constraints = session.get("outlineConstraints")
|
||||
if isinstance(constraints, str) and constraints.strip():
|
||||
try:
|
||||
constraints = json.loads(constraints)
|
||||
except json.JSONDecodeError:
|
||||
constraints = None
|
||||
|
||||
filled = generate_field_values(prompt, doc_type, fields, constraints)
|
||||
return jsonify({"fields": filled})
|
||||
|
||||
|
||||
|
||||
|
||||
@app.route("/api/progressive_render", methods=["POST"])
|
||||
def progressive_render() -> Any:
|
||||
"""Compile arbitrary LaTeX (partial or masked) for progressive previews."""
|
||||
data = _json_body()
|
||||
latex = data.get("latex")
|
||||
if not latex or not isinstance(latex, str):
|
||||
return jsonify({"error": "Missing LaTeX payload"}), 400
|
||||
|
||||
job_id = data.get("jobId") or str(uuid.uuid4())
|
||||
pdf_path = compile_latex_to_pdf(latex, job_id)
|
||||
if pdf_path and os.path.exists(pdf_path):
|
||||
return jsonify({"pdfUrl": f"/output/{job_id}.pdf"})
|
||||
|
||||
return jsonify({"error": "Progressive compilation failed"}), 500
|
||||
|
||||
|
||||
@app.route("/output/<path:filename>", methods=["GET"])
|
||||
def serve_output_file(filename: str) -> Any:
|
||||
"""Serve generated PDF files and stored assets."""
|
||||
file_path = os.path.join(OUTPUT_DIR, filename)
|
||||
if os.path.exists(file_path):
|
||||
mime_type, _ = mimetypes.guess_type(file_path)
|
||||
return send_file(file_path, mimetype=mime_type or "application/octet-stream")
|
||||
return jsonify({"error": "File not found"}), 404
|
||||
|
||||
|
||||
@app.route("/api/versions/<user_id>", methods=["GET"])
|
||||
def list_versions(user_id: str) -> Any:
|
||||
return jsonify({"versions": load_versions(user_id)})
|
||||
|
||||
|
||||
@app.route("/api/style/<user_id>", methods=["GET"])
|
||||
def get_style(user_id: str) -> Any:
|
||||
return jsonify({"style": load_user_style(user_id)})
|
||||
|
||||
|
||||
@app.route("/api/style/<user_id>", methods=["POST"])
|
||||
def update_style(user_id: str) -> Any:
|
||||
data = _json_body()
|
||||
if not isinstance(data, dict):
|
||||
return jsonify({"error": "Style payload must be an object"}), 400
|
||||
current = load_user_style(user_id) or {}
|
||||
merged = {**current, **data}
|
||||
save_user_style(user_id, merged)
|
||||
return jsonify({"style": merged})
|
||||
|
||||
|
||||
@app.route("/api/style/apply", methods=["POST"])
|
||||
def apply_style() -> Any:
|
||||
data = _json_body()
|
||||
latex = data.get("latex")
|
||||
style = data.get("style") or {}
|
||||
if not latex or not isinstance(latex, str):
|
||||
return jsonify({"error": "Missing LaTeX payload"}), 400
|
||||
if not isinstance(style, dict):
|
||||
return jsonify({"error": "Style payload must be an object"}), 400
|
||||
updated = apply_style_overrides(latex, style)
|
||||
return jsonify({"latex": updated})
|
||||
|
||||
|
||||
@app.route("/api/import_template", methods=["POST"])
|
||||
def import_template() -> Any:
|
||||
"""Accept a PDF upload, extract layout via vision model, and save as a template."""
|
||||
user_id = request.form.get("userId", "default_user")
|
||||
doc_type = request.form.get("docType", "document")
|
||||
file = request.files.get("file")
|
||||
|
||||
if not file:
|
||||
return jsonify({"error": "No file uploaded"}), 400
|
||||
|
||||
pdf_bytes = file.read()
|
||||
images = render_pdf_to_images(pdf_bytes, max_pages=2, dpi=170)
|
||||
if not images:
|
||||
return jsonify({"error": "Failed to render PDF"}), 400
|
||||
|
||||
layout_latex = vision_layout_from_images(images, doc_type) or ""
|
||||
if not layout_latex:
|
||||
layout_latex = f"""\\documentclass{{article}}
|
||||
\\usepackage[margin=1in]{{geometry}}
|
||||
\\usepackage{{tabularx}}
|
||||
\\usepackage{{multicol}}
|
||||
\\begin{{document}}
|
||||
% Fallback template for {doc_type}
|
||||
\\section*{{Title placeholder}}
|
||||
Body text goes here.
|
||||
\\end{{document}}
|
||||
"""
|
||||
|
||||
sanitized = clean_generated_latex(layout_latex)
|
||||
save_user_template(user_id, doc_type, sanitized)
|
||||
return jsonify({"message": "Template imported", "docType": doc_type, "pages": len(images)})
|
||||
|
||||
|
||||
@app.route("/api/assets/upload", methods=["POST"])
|
||||
def upload_asset() -> Any:
|
||||
file = request.files.get("file")
|
||||
if not file:
|
||||
return jsonify({"error": "Missing file"}), 400
|
||||
|
||||
_, ext = os.path.splitext(file.filename or "")
|
||||
ext = ext.lower()
|
||||
if ext not in {".png", ".jpg", ".jpeg", ".gif"}:
|
||||
return jsonify({"error": "Unsupported file type"}), 400
|
||||
|
||||
asset_id = f"{uuid.uuid4().hex}{ext}"
|
||||
output_path = os.path.join(ASSETS_DIR, asset_id)
|
||||
os.makedirs(ASSETS_DIR, exist_ok=True)
|
||||
file.save(output_path)
|
||||
|
||||
return jsonify(
|
||||
{
|
||||
"assetId": asset_id,
|
||||
"assetUrl": f"/output/assets/{asset_id}",
|
||||
"latexPath": f"assets/{asset_id}",
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@app.route("/api/pdf-editor/document", methods=["GET"])
|
||||
def pdf_editor_document() -> Any:
|
||||
"""Expose a JSON snapshot of the PDF for rich text editing."""
|
||||
pdf_url = request.args.get("pdfUrl")
|
||||
if not pdf_url:
|
||||
return jsonify({"error": "Missing pdfUrl"}), 400
|
||||
|
||||
filename = os.path.basename(pdf_url.split("?")[0])
|
||||
if not filename:
|
||||
return jsonify({"error": "Invalid pdf file"}), 400
|
||||
if not filename.lower().endswith(".pdf"):
|
||||
return jsonify({"error": "Invalid pdf file"}), 400
|
||||
|
||||
pdf_path = os.path.join(OUTPUT_DIR, filename)
|
||||
if not os.path.exists(pdf_path):
|
||||
return jsonify({"error": "PDF not found"}), 404
|
||||
|
||||
try:
|
||||
document = convert_pdf_to_text_editor_document(pdf_path)
|
||||
return jsonify(document)
|
||||
except FileNotFoundError:
|
||||
return jsonify({"error": "Conversion failed"}), 500
|
||||
except subprocess.CalledProcessError as exc:
|
||||
logger.error("[PDF-EDITOR] Conversion failed: %s", exc)
|
||||
return jsonify({"error": "Conversion failed"}), 500
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logger.error("[PDF-EDITOR] Unexpected conversion failure: %s", exc)
|
||||
return jsonify({"error": "Conversion failed"}), 500
|
||||
|
||||
|
||||
@app.route("/api/pdf-editor/upload", methods=["POST"])
|
||||
def pdf_editor_upload() -> Any:
|
||||
"""Accept an edited PDF and save it so the preview can refresh."""
|
||||
file = request.files.get("file")
|
||||
if not file:
|
||||
return jsonify({"error": "Missing file"}), 400
|
||||
|
||||
job_id = str(uuid.uuid4())
|
||||
filename = f"{job_id}-edited.pdf"
|
||||
output_path = os.path.join(OUTPUT_DIR, filename)
|
||||
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
||||
file.save(output_path)
|
||||
|
||||
logger.info("[PDF-EDITOR] uploaded edited PDF job_id=%s -> %s", job_id, filename)
|
||||
return jsonify({"pdfUrl": f"/output/{filename}"})
|
||||
|
||||
|
||||
@app.route("/health", methods=["GET"])
|
||||
def health() -> Any:
|
||||
return jsonify({"status": "ok", "engine": "pdflatex"})
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(host="0.0.0.0", port=5000, debug=True)
|
||||
@@ -0,0 +1,525 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
import json
|
||||
from typing import Any, Dict, List, Optional
|
||||
import time
|
||||
|
||||
from config import CLIENT_MODE, FAST_MODEL, SMART_MODEL, get_chat_model, logger
|
||||
from langchain_utils import to_lc_messages
|
||||
from prompts import brief_missing_info_system_prompt
|
||||
|
||||
|
||||
BRIEF_SCHEMAS: Dict[str, Dict[str, Any]] = {
|
||||
"resume": {
|
||||
"field_order": [
|
||||
"name",
|
||||
"contact",
|
||||
"location",
|
||||
"target_role",
|
||||
"summary",
|
||||
"work_history",
|
||||
"education",
|
||||
"skills",
|
||||
"achievements",
|
||||
"links",
|
||||
"constraints",
|
||||
],
|
||||
"labels": {
|
||||
"name": ["name", "full name"],
|
||||
"contact": ["contact", "contact info", "contact information", "email/phone"],
|
||||
"location": ["location", "city/country"],
|
||||
"target_role": ["target role", "role", "title", "headline"],
|
||||
"summary": ["summary", "objective", "about"],
|
||||
"work_history": ["experience", "work history", "roles"],
|
||||
"education": ["education", "studies"],
|
||||
"skills": ["skills", "stack"],
|
||||
"achievements": ["achievements", "certifications", "awards"],
|
||||
"links": ["links", "profiles", "linkedin/github"],
|
||||
"constraints": ["constraints", "tone/length/style"],
|
||||
},
|
||||
"questions": {
|
||||
"name": "What's your name as you'd like it on the page?",
|
||||
"contact": "How can someone reach you (email/phone)?",
|
||||
"location": "Where are you based (or remote)?",
|
||||
"target_role": "What role/title and industry are you aiming for?",
|
||||
"summary": "Give me a 1–2 sentence summary about you.",
|
||||
"work_history": "Recent roles: company, title, dates, location, and a few bullets with impact.",
|
||||
"education": "Degree(s), school, and graduation year?",
|
||||
"skills": "Key skills/stack (tech + relevant soft skills)?",
|
||||
"achievements": "Awards/certifications/major achievements?",
|
||||
"links": "Any LinkedIn/GitHub/portfolio links?",
|
||||
"constraints": "Any tone/length constraints (ATS, one-page, etc.)?",
|
||||
},
|
||||
"intro": "Hey! To build a strong resume, you can paste your old resume or just dump everything you remember—name, how to reach you, where you're based, what you're aiming for, your roles, education, skills, links. Share whatever you have and I'll work with it.",
|
||||
},
|
||||
"invoice": {
|
||||
"field_order": [
|
||||
"your_business",
|
||||
"client",
|
||||
"issue_date",
|
||||
"due_date",
|
||||
"line_items",
|
||||
"currency",
|
||||
"payment_terms",
|
||||
"notes",
|
||||
"constraints",
|
||||
],
|
||||
"labels": {
|
||||
"your_business": ["your business", "seller", "from"],
|
||||
"client": ["client", "bill to"],
|
||||
"issue_date": ["issue date", "invoice date"],
|
||||
"due_date": ["due date"],
|
||||
"line_items": ["line items", "services/items"],
|
||||
"currency": ["currency"],
|
||||
"payment_terms": ["payment terms"],
|
||||
"notes": ["notes"],
|
||||
"constraints": ["constraints", "layout/style"],
|
||||
},
|
||||
"questions": {
|
||||
"your_business": "Who is issuing the invoice (business name + contact)?",
|
||||
"client": "Who is being billed (name + contact)?",
|
||||
"issue_date": "Invoice issue date?",
|
||||
"due_date": "Due date?",
|
||||
"line_items": "Line items with description, qty, rate, tax (if any)?",
|
||||
"currency": "Currency?",
|
||||
"payment_terms": "Payment terms and payment methods?",
|
||||
"notes": "Notes to include (late fees, thank you, PO #)?",
|
||||
"constraints": "Branding/layout preferences?",
|
||||
},
|
||||
"intro": "I'll draft an accurate invoice if I know who is billing, who is paying, and the line items. Paste an old invoice or list the details.",
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def classify_intent_with_llm(prompt: str, history: List[Dict[str, str]], current_latex: Optional[str], has_pdf: bool) -> Optional[Dict[str, Any]]:
|
||||
"""
|
||||
Use a small model to classify intent instead of brittle regex.
|
||||
|
||||
Returns a dict like:
|
||||
{
|
||||
"documentType": "invoice|resume|contract|letter|report|form|document",
|
||||
"action": "new|edit|question",
|
||||
"allowFabrication": bool,
|
||||
"wantsPdf": bool,
|
||||
"hasEnoughInfo": bool,
|
||||
"missingFields": [str],
|
||||
"notes": str
|
||||
}
|
||||
"""
|
||||
if CLIENT_MODE != "langchain":
|
||||
logger.info("[INTENT] skip llm classify: client_mode=%s", CLIENT_MODE)
|
||||
return None
|
||||
|
||||
system = (
|
||||
"You classify user requests about documents. "
|
||||
"Output strict JSON. "
|
||||
"documentType must be one of: academic, agenda, brochure, business_card, case_study, checklist, "
|
||||
"contract, creative, datasheet, document, flyer, invoice, letter, manual, menu, minutes, newsletter, "
|
||||
"one_pager, poster, presentation, press_release, proposal, recipe, report, resume, timeline, whitepaper. "
|
||||
"action: 'new' (make/generate), 'edit' (modify existing), 'question' (asking about it). "
|
||||
"allowFabrication: true if the user invites making up/placeholder/dummy/random details "
|
||||
"OR asks you to use your knowledge about a fictional/real character (e.g., 'use what you know about James Bond', "
|
||||
"'make it for agent 007', 'create resume for Sherlock Holmes', etc.). "
|
||||
"Basically, if they're NOT providing their own personal details and expect you to fill in from common knowledge or imagination, set this to true. "
|
||||
"wantsPdf: true if they expect/gave permission to generate a PDF. "
|
||||
"hasEnoughInfo: true if there is enough info to proceed without asking questions (or if allowFabrication is true). "
|
||||
"missingFields: key details still needed (e.g., for invoice: seller, client, line items; resume: name, contact, work). "
|
||||
"notes: short free-form note."
|
||||
)
|
||||
|
||||
conversation = [{"role": "system", "content": system}]
|
||||
# Trim history to keep request small
|
||||
trimmed_history = history[-6:] if len(history) > 6 else history
|
||||
for msg in trimmed_history:
|
||||
if msg.get("content") and msg.get("role") in {"user", "assistant", "system"}:
|
||||
conversation.append({"role": msg["role"], "content": msg["content"]})
|
||||
conversation.append({"role": "user", "content": prompt})
|
||||
|
||||
try:
|
||||
llm = get_chat_model(
|
||||
FAST_MODEL or SMART_MODEL,
|
||||
max_tokens=800,
|
||||
model_kwargs={"response_format": {"type": "json_object"}},
|
||||
)
|
||||
if not llm:
|
||||
logger.info("[INTENT] skip llm classify: no LangChain client")
|
||||
return None
|
||||
start = time.perf_counter()
|
||||
response = llm.invoke(to_lc_messages(conversation))
|
||||
elapsed = time.perf_counter() - start
|
||||
content = response.content or ""
|
||||
usage = getattr(response, "usage_metadata", None)
|
||||
logger.info(
|
||||
"[INTENT] llm_classify model=%s elapsed=%.2fs chars=%s usage=%s",
|
||||
FAST_MODEL or SMART_MODEL,
|
||||
elapsed,
|
||||
len(str(content)),
|
||||
usage,
|
||||
)
|
||||
content = response.content
|
||||
if not content:
|
||||
logger.info("[INTENT] llm_classify empty content")
|
||||
return None
|
||||
data = json.loads(content)
|
||||
# Normalize
|
||||
doc_type = str(data.get("documentType") or "document").lower()
|
||||
allowed_types = {
|
||||
"academic", "agenda", "brochure", "business_card", "case_study", "checklist",
|
||||
"contract", "creative", "datasheet", "document", "flyer", "invoice", "letter",
|
||||
"manual", "menu", "minutes", "newsletter", "one_pager", "poster", "presentation",
|
||||
"press_release", "proposal", "recipe", "report", "resume", "timeline", "whitepaper"
|
||||
}
|
||||
if doc_type not in allowed_types:
|
||||
doc_type = "document"
|
||||
action = str(data.get("action") or "new").lower()
|
||||
if action not in {"new", "edit", "question"}:
|
||||
action = "new"
|
||||
result = {
|
||||
"documentType": doc_type,
|
||||
"action": action,
|
||||
"allowFabrication": bool(data.get("allowFabrication")),
|
||||
"wantsPdf": bool(data.get("wantsPdf", True)),
|
||||
"hasEnoughInfo": bool(data.get("hasEnoughInfo", True)),
|
||||
"missingFields": data.get("missingFields") or [],
|
||||
"notes": data.get("notes") or "",
|
||||
}
|
||||
logger.info(
|
||||
"[INTENT] llm_classify doc_type=%s action=%s allowFabrication=%s wantsPdf=%s hasEnoughInfo=%s missing=%s notes=%s",
|
||||
result["documentType"],
|
||||
result["action"],
|
||||
result["allowFabrication"],
|
||||
result["wantsPdf"],
|
||||
result["hasEnoughInfo"],
|
||||
result["missingFields"],
|
||||
(result["notes"] or "")[:120],
|
||||
)
|
||||
return result
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logger.error("[INTENT] LLM classify failed: %s", exc, exc_info=True)
|
||||
return None
|
||||
|
||||
|
||||
def detect_fabrication_opt_in(prompt: str, history: List[Dict[str, str]]) -> bool:
|
||||
"""
|
||||
Ask a small model to decide if the user has permitted invention of missing details.
|
||||
|
||||
Returns True when the user says to make things up / whatever is fine /
|
||||
no preference, even if they haven't provided concrete fields.
|
||||
"""
|
||||
if CLIENT_MODE != "langchain":
|
||||
return False
|
||||
|
||||
system = (
|
||||
"Decide if the user has explicitly permitted you to invent or make up missing details. "
|
||||
"Reply with strict JSON: {\"allowFabrication\": true|false}. "
|
||||
"Consider any user instruction like 'make it up', 'whatever you want', 'use dummy info', "
|
||||
"'fabricate the rest', 'fill in anything' as permission. "
|
||||
"Do not require specific keywords; infer intent from the conversation. "
|
||||
"If unclear, set allowFabrication to false."
|
||||
)
|
||||
|
||||
conversation = [{"role": "system", "content": system}]
|
||||
trimmed_history = history[-8:] if len(history) > 8 else history
|
||||
for msg in trimmed_history:
|
||||
if msg.get("content") and msg.get("role") in {"user", "assistant", "system"}:
|
||||
conversation.append({"role": msg["role"], "content": msg["content"]})
|
||||
conversation.append({"role": "user", "content": prompt})
|
||||
|
||||
try:
|
||||
llm = get_chat_model(
|
||||
FAST_MODEL or SMART_MODEL,
|
||||
max_tokens=100,
|
||||
model_kwargs={"response_format": {"type": "json_object"}},
|
||||
)
|
||||
if not llm:
|
||||
return False
|
||||
start = time.perf_counter()
|
||||
response = llm.invoke(to_lc_messages(conversation))
|
||||
elapsed = time.perf_counter() - start
|
||||
content = response.content or ""
|
||||
usage = getattr(response, "usage_metadata", None)
|
||||
logger.info(
|
||||
"[INTENT] fabrication-check model=%s elapsed=%.2fs chars=%s usage=%s",
|
||||
FAST_MODEL or SMART_MODEL,
|
||||
elapsed,
|
||||
len(str(content)),
|
||||
usage,
|
||||
)
|
||||
content = response.content
|
||||
if not content:
|
||||
return False
|
||||
data = json.loads(content)
|
||||
return bool(data.get("allowFabrication"))
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logger.error("[INTENT] fabrication opt-in check failed: %s", exc)
|
||||
return False
|
||||
|
||||
|
||||
def _preprocess_intent(
|
||||
prompt: str,
|
||||
history: List[Dict[str, str]],
|
||||
has_pdf: bool,
|
||||
current_latex: Optional[str],
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Lightweight intent classifier used by /api/intent/check.
|
||||
|
||||
It is intentionally heuristic-only to avoid extra model calls. The goal is to
|
||||
decide whether we should proceed with PDF generation and whether it's OK to
|
||||
fabricate placeholder content when the user explicitly asks for it.
|
||||
"""
|
||||
user_texts = [entry.get("content", "") for entry in history if entry.get("role") == "user"]
|
||||
user_texts.append(prompt or "")
|
||||
combined_text = " ".join([t for t in user_texts if t]).strip().lower()
|
||||
|
||||
llm = classify_intent_with_llm(prompt, history, current_latex, has_pdf)
|
||||
if llm:
|
||||
return {
|
||||
"wants_pdf": llm.get("wantsPdf", True),
|
||||
"has_enough_info": llm.get("hasEnoughInfo", True),
|
||||
"allow_makeup": llm.get("allowFabrication", False),
|
||||
"document_type": llm.get("documentType"),
|
||||
"missing_fields": llm.get("missingFields", []),
|
||||
}
|
||||
|
||||
# Fallback heuristics (only if no LLM)
|
||||
avoid_pdf = bool(re.search(r"\b(no pdf|text only|markdown only|dont (make|generate) pdf)\b", combined_text))
|
||||
wants_pdf = not avoid_pdf or bool(current_latex) or has_pdf
|
||||
has_meaningful_text = len(combined_text) > 20
|
||||
return {
|
||||
"wants_pdf": wants_pdf,
|
||||
"has_enough_info": bool(current_latex or has_pdf or has_meaningful_text),
|
||||
"allow_makeup": False,
|
||||
"document_type": None,
|
||||
"missing_fields": [],
|
||||
}
|
||||
|
||||
def _extract_structured_fields(text: str, schema: Dict[str, Any]) -> Dict[str, str]:
|
||||
"""Naively parse user text to pull schema fields."""
|
||||
found: Dict[str, str] = {}
|
||||
lower = text.lower()
|
||||
|
||||
if schema.get("field_order") == BRIEF_SCHEMAS["resume"]["field_order"]:
|
||||
work_matches = re.findall(
|
||||
r"(?:experience|work history|role|company|position)\s*[:\-]\s*(.+?)(?=\n\n|\Z)",
|
||||
text,
|
||||
flags=re.IGNORECASE | re.DOTALL,
|
||||
)
|
||||
if work_matches:
|
||||
found["work_history"] = "\n".join(work_matches[:3])
|
||||
education_matches = re.findall(
|
||||
r"(?:education|degree)\s*[:\-]\s*(.+?)(?=\n\n|\Z)",
|
||||
text,
|
||||
flags=re.IGNORECASE | re.DOTALL,
|
||||
)
|
||||
if education_matches:
|
||||
found["education"] = "\n".join(education_matches[:2])
|
||||
skills_match = re.search(r"(?:skills|stack)\s*[:\-]\s*(.+)", text, flags=re.IGNORECASE)
|
||||
if skills_match:
|
||||
found["skills"] = skills_match.group(1).strip()
|
||||
|
||||
for field, labels in schema.get("labels", {}).items():
|
||||
for label in labels:
|
||||
pattern = rf"{label}\s*[:\-]\s*(.+?)(?=\n[A-Z][a-zA-Z ]+[:\-]|\Z)"
|
||||
match = re.search(pattern, text, flags=re.IGNORECASE | re.DOTALL)
|
||||
if match:
|
||||
found[field] = match.group(1).strip()
|
||||
break
|
||||
if not found.get("summary") and len(lower) < 200:
|
||||
found["summary"] = text.strip()
|
||||
return {k: v for k, v in found.items() if v}
|
||||
|
||||
|
||||
def _format_missing_message(
|
||||
doc_type: str,
|
||||
schema: Dict[str, Any],
|
||||
collected: Dict[str, str],
|
||||
missing: List[str],
|
||||
preface: Optional[str] = None,
|
||||
) -> str:
|
||||
"""Fallback text asking the user for missing fields."""
|
||||
intro = schema.get("intro") or f"Need a few details to finish your {doc_type}."
|
||||
lines = [intro]
|
||||
if preface:
|
||||
lines.append(preface)
|
||||
if collected:
|
||||
lines.append("Already have:")
|
||||
for field, value in collected.items():
|
||||
label = schema.get("labels", {}).get(field, [field])[0]
|
||||
lines.append(f"- {label}: {value}")
|
||||
if missing:
|
||||
lines.append("Still need:")
|
||||
questions = schema.get("questions", {})
|
||||
for field in missing[:4]:
|
||||
ask = questions.get(field) or f"{field}?"
|
||||
lines.append(f"- {ask}")
|
||||
lines.append("Partial info is fine—share whatever you remember.")
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def _ai_missing_message(
|
||||
doc_type: str,
|
||||
schema: Dict[str, Any],
|
||||
collected: Dict[str, str],
|
||||
missing: List[str],
|
||||
) -> Optional[str]:
|
||||
"""Let the model craft clarifying questions when available."""
|
||||
if CLIENT_MODE != "langchain" or not missing:
|
||||
return None
|
||||
|
||||
collected_lines = [f"- {schema.get('labels', {}).get(field, [field])[0]}: {value}" for field, value in collected.items()]
|
||||
missing_labels = [schema.get("labels", {}).get(field, [field])[0] for field in missing]
|
||||
user_text = "We already have:\n" + "\n".join(collected_lines) if collected_lines else "We have nothing yet."
|
||||
user_text += "\nNeed to ask for: " + ", ".join(missing_labels)
|
||||
if not collected_lines:
|
||||
user_text += "\nInvite them to paste an old resume or dump all details if they have them."
|
||||
|
||||
system_prompt = brief_missing_info_system_prompt(doc_type)
|
||||
try:
|
||||
llm = get_chat_model(SMART_MODEL, max_tokens=400)
|
||||
if not llm:
|
||||
return None
|
||||
start = time.perf_counter()
|
||||
response = llm.invoke(
|
||||
to_lc_messages(
|
||||
[
|
||||
{"role": "system", "content": system_prompt},
|
||||
{"role": "user", "content": user_text},
|
||||
]
|
||||
)
|
||||
)
|
||||
elapsed = time.perf_counter() - start
|
||||
content = response.content or ""
|
||||
usage = getattr(response, "usage_metadata", None)
|
||||
logger.info(
|
||||
"[AI] missing-questions model=%s elapsed=%.2fs chars=%s usage=%s",
|
||||
SMART_MODEL,
|
||||
elapsed,
|
||||
len(str(content)),
|
||||
usage,
|
||||
)
|
||||
return response.content
|
||||
except Exception as exc:
|
||||
logger.error("[AI] missing-questions failed: %s", exc)
|
||||
return None
|
||||
|
||||
|
||||
def gather_brief(doc_type: str, prompt: str, history: List[Dict[str, str]], current_latex: Optional[str] = None, has_pdf: bool = False) -> Dict[str, Any]:
|
||||
"""
|
||||
Determine whether we have enough structured details to generate without fabricating.
|
||||
Returns needsInfo + a formatted message when details are missing, or a structured brief.
|
||||
"""
|
||||
classifier = classify_intent_with_llm(prompt, history, current_latex, has_pdf)
|
||||
if classifier:
|
||||
doc_type = classifier.get("documentType", doc_type)
|
||||
logger.info(
|
||||
"[BRIEF] using llm doc_type=%s allowFabrication=%s missing=%s hasEnoughInfo=%s wantsPdf=%s",
|
||||
doc_type,
|
||||
classifier.get("allowFabrication"),
|
||||
classifier.get("missingFields"),
|
||||
classifier.get("hasEnoughInfo"),
|
||||
classifier.get("wantsPdf"),
|
||||
)
|
||||
else:
|
||||
logger.info("[BRIEF] llm classifier unavailable, using fallback schema doc_type=%s", doc_type)
|
||||
|
||||
schema = BRIEF_SCHEMAS.get(doc_type)
|
||||
if not schema:
|
||||
return {"needsInfo": False, "structured_brief": None, "collected": {}, "missing": []}
|
||||
|
||||
user_texts = [entry.get("content", "") for entry in history if entry.get("role") == "user"]
|
||||
user_texts.append(prompt or "")
|
||||
combined_text = "\n".join(user_texts)
|
||||
collected = _extract_structured_fields(combined_text, schema)
|
||||
missing = [field for field in schema.get("field_order", []) if field not in collected]
|
||||
classifier_has_enough = bool(classifier.get("hasEnoughInfo")) if classifier else True
|
||||
allow_makeup = bool(classifier.get("allowFabrication")) if classifier else False
|
||||
if not allow_makeup:
|
||||
allow_makeup = detect_fabrication_opt_in(prompt, history)
|
||||
if classifier and classifier.get("missingFields"):
|
||||
# If the model provided missing fields, respect that list.
|
||||
missing = classifier.get("missingFields") or missing
|
||||
|
||||
# If the user gave no usable content, do not allow fabrication shortcuts.
|
||||
# Force the flow to ask for the required fields instead of silently proceeding.
|
||||
missing_all_fields = len(missing) == len(schema.get("field_order", []))
|
||||
low_signal_request = not collected and len(combined_text.strip()) < 12
|
||||
if missing_all_fields and low_signal_request:
|
||||
allow_makeup = False
|
||||
|
||||
def has_minimum_resume(data: Dict[str, str]) -> bool:
|
||||
has_name = bool(data.get("name"))
|
||||
has_core = any(data.get(key) for key in ["work_history", "education", "skills", "contact", "target_role"])
|
||||
return has_name and has_core
|
||||
|
||||
def has_minimum_invoice(data: Dict[str, str]) -> bool:
|
||||
has_parties = data.get("your_business") and data.get("client")
|
||||
has_items = bool(data.get("line_items"))
|
||||
return bool(has_parties and has_items)
|
||||
|
||||
has_minimum = True
|
||||
if doc_type == "resume":
|
||||
has_minimum = has_minimum_resume(collected)
|
||||
elif doc_type == "invoice":
|
||||
has_minimum = has_minimum_invoice(collected)
|
||||
|
||||
# Decide if we must pause to ask for details. Avoid regex "ready" guesses;
|
||||
# rely on the classifier's allowFabrication flag and collected data.
|
||||
must_ask_first = bool(missing) and ((not allow_makeup and not has_minimum) or not classifier_has_enough)
|
||||
if must_ask_first:
|
||||
preface = None
|
||||
if doc_type == "resume" and not has_minimum:
|
||||
preface = (
|
||||
"I only have a tiny bit so far. I need at least your name plus one of: contact, "
|
||||
"a role snippet, education, skills, or target role."
|
||||
)
|
||||
if doc_type == "invoice" and not has_minimum:
|
||||
preface = "Need who is billing, who is paying, and the line items so I don't invent details."
|
||||
logger.info(
|
||||
"[BRIEF] gating doc_type=%s missing=%s has_minimum=%s allowFabrication=%s",
|
||||
doc_type,
|
||||
missing,
|
||||
has_minimum,
|
||||
allow_makeup,
|
||||
)
|
||||
message = _ai_missing_message(doc_type, schema, collected, missing) or _format_missing_message(
|
||||
doc_type, schema, collected, missing, preface=preface
|
||||
)
|
||||
message += "\nIf you'd like me to invent anything you didn't share, just say so."
|
||||
return {
|
||||
"needsInfo": True,
|
||||
"message": message,
|
||||
"collected": collected,
|
||||
"missing": missing,
|
||||
"allowFabrication": allow_makeup,
|
||||
}
|
||||
|
||||
# If fabrication is allowed but fields are missing, let downstream generation
|
||||
# know which areas to fill in plausibly.
|
||||
fabrication_hint = ""
|
||||
if allow_makeup and missing:
|
||||
fabrication_hint = "\n\nIf details are absent, invent plausible, clearly fictional details for: " + ", ".join(missing) + "."
|
||||
|
||||
structured_lines = []
|
||||
for field in schema.get("field_order", []):
|
||||
value = collected.get(field)
|
||||
if value:
|
||||
label = schema.get("labels", {}).get(field, [field])[0]
|
||||
structured_lines.append(f"{label}: {value}")
|
||||
structured_brief = "\n".join(structured_lines)
|
||||
if fabrication_hint:
|
||||
structured_brief = (structured_brief + fabrication_hint).strip()
|
||||
if combined_text.strip():
|
||||
structured_brief = (structured_brief + "\n\nRaw user notes:\n" + combined_text).strip()
|
||||
|
||||
return {
|
||||
"needsInfo": False,
|
||||
"structured_brief": structured_brief or None,
|
||||
"collected": collected,
|
||||
"missing": missing,
|
||||
"allowFabrication": allow_makeup,
|
||||
}
|
||||
|
||||
|
||||
__all__ = ["gather_brief", "BRIEF_SCHEMAS", "_preprocess_intent"]
|
||||
@@ -0,0 +1,94 @@
|
||||
import logging
|
||||
import os
|
||||
from typing import Optional
|
||||
|
||||
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||
OUTPUT_DIR = os.path.join(BASE_DIR, "output")
|
||||
ASSETS_DIR = os.path.join(OUTPUT_DIR, "assets")
|
||||
DATA_DIR = os.path.join(BASE_DIR, "data")
|
||||
TEMPLATE_DIR = os.path.join(BASE_DIR, "templates")
|
||||
STYLE_DB_PATH = os.path.join(DATA_DIR, "user_styles.json")
|
||||
TEMPLATE_DB_PATH = os.path.join(DATA_DIR, "user_templates.json")
|
||||
VERSIONS_DB_PATH = os.path.join(DATA_DIR, "versions.json")
|
||||
|
||||
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
||||
os.makedirs(ASSETS_DIR, exist_ok=True)
|
||||
os.makedirs(DATA_DIR, exist_ok=True)
|
||||
os.makedirs(TEMPLATE_DIR, exist_ok=True)
|
||||
|
||||
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
|
||||
OPENAI_BASE_URL = os.environ.get("OPENAI_BASE_URL")
|
||||
JAVA_BACKEND_URL = os.environ.get("JAVA_BACKEND_URL", "http://localhost:8080")
|
||||
|
||||
if not OPENAI_API_KEY:
|
||||
raise RuntimeError("OPENAI_API_KEY is required to start the AI backend.")
|
||||
# Default to GPT-5.1 for full document generation (smart model).
|
||||
# Allow override via SMART_MODEL or legacy OPENAI_MODEL.
|
||||
SMART_MODEL = os.environ.get("SMART_MODEL") or os.environ.get("OPENAI_MODEL") or "gpt-5.1"
|
||||
# Default to the nano/ultra-fast tier for intent/pre checks (fast model).
|
||||
# Allow override via FAST_MODEL or legacy FAST_INTENT_MODEL.
|
||||
FAST_MODEL = os.environ.get("FAST_MODEL") or os.environ.get("FAST_INTENT_MODEL") or "gpt-4.1-nano"
|
||||
|
||||
CLIENT_MODE: Optional[str] = None
|
||||
LANGCHAIN_AVAILABLE = False
|
||||
_ChatOpenAI = None
|
||||
STREAMING_ENABLED = os.environ.get("AI_STREAMING", "true").lower() not in {"0", "false", "no"}
|
||||
if OPENAI_BASE_URL and "ollama" in OPENAI_BASE_URL and "AI_STREAMING" not in os.environ:
|
||||
STREAMING_ENABLED = False
|
||||
PREVIEW_MAX_INFLIGHT = int(os.environ.get("AI_PREVIEW_MAX_INFLIGHT", "3"))
|
||||
|
||||
if OPENAI_API_KEY:
|
||||
try:
|
||||
from langchain_openai import ChatOpenAI # type: ignore
|
||||
|
||||
_ChatOpenAI = ChatOpenAI
|
||||
LANGCHAIN_AVAILABLE = True
|
||||
CLIENT_MODE = "langchain"
|
||||
except Exception as client_exc: # pragma: no cover - import guard
|
||||
logger.warning("LangChain OpenAI init failed: %s", client_exc)
|
||||
|
||||
if CLIENT_MODE == "langchain":
|
||||
logger.info("AI mode: LIVE (fast_model=%s smart_model=%s)", FAST_MODEL, SMART_MODEL)
|
||||
else:
|
||||
logger.info("AI mode: MOCK (no OpenAI key or LangChain init failure)")
|
||||
|
||||
|
||||
def get_chat_model(
|
||||
model_name: str,
|
||||
streaming: bool = False,
|
||||
max_tokens: Optional[int] = None,
|
||||
model_kwargs: Optional[dict] = None,
|
||||
):
|
||||
if not LANGCHAIN_AVAILABLE or not _ChatOpenAI:
|
||||
return None
|
||||
kwargs = {"model": model_name, "api_key": OPENAI_API_KEY, "streaming": streaming}
|
||||
if max_tokens is not None:
|
||||
kwargs["max_tokens"] = max_tokens
|
||||
if model_kwargs:
|
||||
kwargs["model_kwargs"] = model_kwargs
|
||||
return _ChatOpenAI(**kwargs)
|
||||
|
||||
__all__ = [
|
||||
"logger",
|
||||
"OUTPUT_DIR",
|
||||
"ASSETS_DIR",
|
||||
"DATA_DIR",
|
||||
"TEMPLATE_DIR",
|
||||
"STYLE_DB_PATH",
|
||||
"TEMPLATE_DB_PATH",
|
||||
"VERSIONS_DB_PATH",
|
||||
"OPENAI_API_KEY",
|
||||
"OPENAI_BASE_URL",
|
||||
"JAVA_BACKEND_URL",
|
||||
"SMART_MODEL",
|
||||
"CLIENT_MODE",
|
||||
"LANGCHAIN_AVAILABLE",
|
||||
"get_chat_model",
|
||||
"FAST_MODEL",
|
||||
"STREAMING_ENABLED",
|
||||
"PREVIEW_MAX_INFLIGHT",
|
||||
]
|
||||
@@ -0,0 +1,57 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import List, Tuple
|
||||
|
||||
|
||||
KEYWORDS: List[Tuple[str, str]] = [
|
||||
("business card", "business_card"),
|
||||
("business-card", "business_card"),
|
||||
("card", "business_card"),
|
||||
("recipe", "recipe"),
|
||||
("cookbook", "recipe"),
|
||||
("menu", "menu"),
|
||||
("flyer", "flyer"),
|
||||
("brochure", "brochure"),
|
||||
("poster", "poster"),
|
||||
("slide", "presentation"),
|
||||
("deck", "presentation"),
|
||||
("presentation", "presentation"),
|
||||
("pitch", "presentation"),
|
||||
("whitepaper", "whitepaper"),
|
||||
("datasheet", "datasheet"),
|
||||
("case study", "case_study"),
|
||||
("press release", "press_release"),
|
||||
("agenda", "agenda"),
|
||||
("minutes", "minutes"),
|
||||
("checklist", "checklist"),
|
||||
("newsletter", "newsletter"),
|
||||
("proposal", "proposal"),
|
||||
("one-pager", "one_pager"),
|
||||
("one pager", "one_pager"),
|
||||
("invoice", "invoice"),
|
||||
("resume", "resume"),
|
||||
("cv", "resume"),
|
||||
("contract", "contract"),
|
||||
("agreement", "contract"),
|
||||
("letter", "letter"),
|
||||
("report", "report"),
|
||||
("paper", "academic"),
|
||||
("research", "academic"),
|
||||
("thesis", "academic"),
|
||||
("poem", "creative"),
|
||||
("manual", "manual"),
|
||||
("timeline", "timeline"),
|
||||
]
|
||||
|
||||
|
||||
def detect_document_type(prompt: str, latex_code: str | None = None) -> str:
|
||||
"""Heuristic classifier for document types based on prompt/latex text."""
|
||||
text = (prompt or "").lower()
|
||||
latex_text = (latex_code or "").lower()
|
||||
for keyword, label in KEYWORDS:
|
||||
if keyword in text or keyword in latex_text:
|
||||
return label
|
||||
return "document"
|
||||
|
||||
|
||||
__all__ = ["detect_document_type"]
|
||||
@@ -0,0 +1,22 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Dict, List
|
||||
|
||||
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage
|
||||
|
||||
|
||||
def to_lc_messages(messages: List[Dict[str, Any]]):
|
||||
lc_messages = []
|
||||
for msg in messages:
|
||||
role = msg.get("role")
|
||||
content = msg.get("content")
|
||||
if role == "system":
|
||||
lc_messages.append(SystemMessage(content=content))
|
||||
elif role == "assistant":
|
||||
lc_messages.append(AIMessage(content=content))
|
||||
else:
|
||||
lc_messages.append(HumanMessage(content=content))
|
||||
return lc_messages
|
||||
|
||||
|
||||
__all__ = ["to_lc_messages"]
|
||||
@@ -0,0 +1,547 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from functools import lru_cache
|
||||
from typing import List, Optional
|
||||
|
||||
ALLOWED_LATEX_PACKAGES = {
|
||||
"courier",
|
||||
"graphicx",
|
||||
"geometry",
|
||||
"helvet",
|
||||
"lmodern",
|
||||
"mathpazo",
|
||||
"xcolor",
|
||||
"tabularx",
|
||||
"paracol",
|
||||
"multicol",
|
||||
"longtable",
|
||||
"setspace",
|
||||
"enumitem",
|
||||
"titlesec",
|
||||
"array",
|
||||
"inputenc",
|
||||
"fontenc",
|
||||
"tikz",
|
||||
}
|
||||
|
||||
|
||||
def _strip_body_content(body: str) -> str:
|
||||
"""
|
||||
Remove user data while keeping layout/structure commands.
|
||||
Keeps \begin/\end blocks, command scaffolding, and drops plain text.
|
||||
"""
|
||||
lines: List[str] = []
|
||||
for line in body.splitlines():
|
||||
stripped = line.strip()
|
||||
if not stripped:
|
||||
lines.append("")
|
||||
continue
|
||||
if stripped.startswith("%"):
|
||||
continue
|
||||
if "\\begin" in stripped or "\\end" in stripped:
|
||||
lines.append(line)
|
||||
continue
|
||||
if stripped.startswith("\\"):
|
||||
line_no_comments = line.split("%", 1)[0]
|
||||
line_sections = re.sub(
|
||||
r"(\\(?:section|subsection|subsubsection|paragraph|subparagraph|chapter|part)\*?)\{[^}]*\}",
|
||||
r"\\1{}",
|
||||
line_no_comments,
|
||||
)
|
||||
line_items = re.sub(r"^\\item.*", r"\\item {}", line_sections)
|
||||
line_text_cmds = re.sub(
|
||||
r"\\text(?:bf|it|tt|sc|sf|normal|emph)\{[^}]*\}",
|
||||
lambda match: match.group(0).split("{")[0] + "{}",
|
||||
line_items,
|
||||
)
|
||||
cleaned = re.sub(r"(?<!\\)[A-Za-z][A-Za-z0-9 ,.;:'\"!?-]*", "", line_text_cmds).strip()
|
||||
if cleaned:
|
||||
lines.append(cleaned)
|
||||
continue
|
||||
cleaned = re.sub(r"[A-Za-z0-9]+", "", line).strip()
|
||||
if cleaned:
|
||||
lines.append(cleaned)
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def extract_layout_hint(latex_code: str, max_chars: Optional[int] = None) -> str:
|
||||
"""Keep layout-defining LaTeX while stripping user text."""
|
||||
if not latex_code:
|
||||
return ""
|
||||
|
||||
preamble, body = "", latex_code
|
||||
split_doc = latex_code.split(r"\begin{document}", 1)
|
||||
if len(split_doc) == 2:
|
||||
preamble, body = split_doc
|
||||
sanitized_body = _strip_body_content(body)
|
||||
|
||||
hint = f"{preamble}\n% --- layout only (data stripped) ---\n{sanitized_body}"
|
||||
return hint if max_chars is None else hint[:max_chars]
|
||||
|
||||
|
||||
@lru_cache(maxsize=64)
|
||||
def _word_to_int(word: str) -> Optional[int]:
|
||||
"""Convert simple English number words to int (0-100)."""
|
||||
words = {
|
||||
"zero": 0,
|
||||
"one": 1,
|
||||
"two": 2,
|
||||
"three": 3,
|
||||
"four": 4,
|
||||
"five": 5,
|
||||
"six": 6,
|
||||
"seven": 7,
|
||||
"eight": 8,
|
||||
"nine": 9,
|
||||
"ten": 10,
|
||||
"twenty": 20,
|
||||
"thirty": 30,
|
||||
"forty": 40,
|
||||
"fifty": 50,
|
||||
"sixty": 60,
|
||||
"seventy": 70,
|
||||
"eighty": 80,
|
||||
"ninety": 90,
|
||||
"hundred": 100,
|
||||
}
|
||||
return words.get(word.strip().lower())
|
||||
|
||||
|
||||
def _sanitize_color_mix(match: re.Match[str]) -> str:
|
||||
token = match.group(1)
|
||||
if token.isdigit():
|
||||
val = int(token)
|
||||
else:
|
||||
converted = _word_to_int(token)
|
||||
if converted is None:
|
||||
digits = "".join(ch for ch in token if ch.isdigit())
|
||||
val = int(digits) if digits else 80
|
||||
else:
|
||||
val = converted
|
||||
val = max(0, min(100, val))
|
||||
return f"!{val}!"
|
||||
|
||||
|
||||
def sanitize_latex(latex_code: str) -> str:
|
||||
"""Normalize invalid xcolor syntax like '!eighty!' to '!80!'."""
|
||||
if not latex_code:
|
||||
return latex_code
|
||||
return re.sub(r"!\s*([A-Za-z0-9]+)\s*!", _sanitize_color_mix, latex_code)
|
||||
|
||||
|
||||
def strip_missing_packages(latex_code: str) -> str:
|
||||
"""Remove packages unavailable in the runtime environment."""
|
||||
if not latex_code:
|
||||
return latex_code
|
||||
code = latex_code
|
||||
code = re.sub(r"^\\usepackage\{siunitx\}\s*$", "", code, flags=re.MULTILINE)
|
||||
code = re.sub(r"\\sisetup\{[^}]*\}", "", code, flags=re.DOTALL)
|
||||
code = re.sub(r"\\num\{([^}]*)\}", r"\\1", code)
|
||||
code = re.sub(
|
||||
r"^\\(setmainfont|setsansfont|setmonofont|newfontfamily)\b.*$",
|
||||
"",
|
||||
code,
|
||||
flags=re.MULTILINE,
|
||||
)
|
||||
|
||||
def _filter_packages(match: re.Match[str]) -> str:
|
||||
options = match.group(1) or ""
|
||||
packages = [pkg.strip() for pkg in match.group(2).split(",") if pkg.strip()]
|
||||
allowed = [pkg for pkg in packages if pkg in ALLOWED_LATEX_PACKAGES]
|
||||
if not allowed:
|
||||
return ""
|
||||
return f"\\usepackage{options}{{{', '.join(allowed)}}}"
|
||||
|
||||
return re.sub(
|
||||
r"^\\usepackage(\[[^\]]*\])?\{([^}]*)\}\s*$",
|
||||
_filter_packages,
|
||||
code,
|
||||
flags=re.MULTILINE,
|
||||
)
|
||||
|
||||
|
||||
def remove_leading_pagebreaks(latex_code: str) -> str:
|
||||
"""Strip explicit page breaks at the start of the document body."""
|
||||
if not latex_code:
|
||||
return latex_code
|
||||
parts = latex_code.split(r"\begin{document}", 1)
|
||||
if len(parts) == 2:
|
||||
preamble, body = parts
|
||||
cleaned_body = re.sub(
|
||||
r"^\s*(\\(newpage|clearpage|pagebreak|vfill)\b\s*)+",
|
||||
"\n",
|
||||
body,
|
||||
flags=re.IGNORECASE | re.MULTILINE,
|
||||
)
|
||||
return f"{preamble}\\begin{{document}}{cleaned_body}"
|
||||
return re.sub(
|
||||
r"^\s*(\\(newpage|clearpage|pagebreak|vfill)\b\s*)+",
|
||||
"\n",
|
||||
latex_code,
|
||||
flags=re.IGNORECASE | re.MULTILINE,
|
||||
)
|
||||
|
||||
|
||||
def strip_leading_pagebreaks(latex_code: str) -> str:
|
||||
"""Drop accidental leading page breaks that cause empty first pages."""
|
||||
if not latex_code:
|
||||
return latex_code
|
||||
parts = latex_code.split(r"\begin{document}", 1)
|
||||
if len(parts) == 2:
|
||||
preamble, body = parts
|
||||
cleaned_body = re.sub(
|
||||
r"^\s*(\\clearpage|\\newpage|\\pagebreak|\\vfill)+\s*",
|
||||
"",
|
||||
body,
|
||||
flags=re.MULTILINE,
|
||||
)
|
||||
return f"{preamble}\\begin{{document}}{cleaned_body}"
|
||||
return re.sub(
|
||||
r"^\s*(\\clearpage|\\newpage|\\pagebreak|\\vfill)+\s*",
|
||||
"",
|
||||
latex_code,
|
||||
flags=re.MULTILINE,
|
||||
)
|
||||
|
||||
|
||||
def fix_tabular_row_endings(latex_code: str) -> str:
|
||||
"""Ensure tabular environments close rows before \\end{tabular}."""
|
||||
pattern = re.compile(r"(&[^\n]*)\n\\end{tabular}", re.MULTILINE)
|
||||
return pattern.sub(r"\\1 \\\\ \n\\end{tabular}", latex_code)
|
||||
|
||||
def strip_placeholder_rules(latex_code: str) -> str:
|
||||
"""Remove placeholder boxes like \\rule/\\colorbox used as fake images."""
|
||||
code = re.sub(r"\\rule\s*\{\s*[\d\.]+[a-zA-Z]*\s*\}\s*\{\s*[\d\.]+[a-zA-Z]*\s*\}", "", latex_code)
|
||||
code = re.sub(r"\\fcolorbox\{[^}]*\}\{[^}]*\}\{[^}]*\}", "", code)
|
||||
code = re.sub(r"\\colorbox\{[^}]*\}\{[^}]*\}", "", code)
|
||||
# Strip simple tikz pictures that are just boxes/fills
|
||||
code = re.sub(
|
||||
r"\\begin\{tikzpicture\}[\s\S]*?\\end\{tikzpicture\}",
|
||||
"",
|
||||
code,
|
||||
flags=re.MULTILINE,
|
||||
)
|
||||
return code
|
||||
|
||||
|
||||
def strip_number_grouping_junk(latex_code: str) -> str:
|
||||
"""Remove stray siunitx options text that may leak into the document body."""
|
||||
if not latex_code:
|
||||
return latex_code
|
||||
# Drop standalone lines/paragraphs that look like siunitx option lists (common when chunks split)
|
||||
return re.sub(
|
||||
r"(?im)^\s*,\s*(group-minimum-digits|detect-all|table-number-alignment|round-mode|round-precision)\b.*$",
|
||||
"",
|
||||
latex_code,
|
||||
)
|
||||
|
||||
|
||||
def rebalance_invoice_tables(latex_code: str) -> str:
|
||||
"""Use wrapped columns for common invoice tables to avoid overflow."""
|
||||
if not latex_code:
|
||||
return latex_code
|
||||
|
||||
# Legacy 4-col invoices (Item, Desc, Price, Total)
|
||||
code = latex_code.replace(
|
||||
r"\\begin{tabularx}{\\textwidth}{@{}l l r r@{}}",
|
||||
r"\\begin{tabularx}{\\textwidth}{@{}>{\\raggedright\\arraybackslash}p{0.30\\textwidth}>{\\raggedright\\arraybackslash}X>{\\raggedleft\\arraybackslash}p{1.5cm}>{\\raggedleft\\arraybackslash}p{2.3cm}@{}}",
|
||||
)
|
||||
|
||||
# Current 5-col invoices (Item, Description, Qty, Unit, Line Total)
|
||||
wrapped_invoice_five = (
|
||||
r"@{}"
|
||||
r">{\\raggedright\\arraybackslash}p{0.16\\textwidth}"
|
||||
r">{\\raggedright\\arraybackslash}p{0.50\\textwidth}"
|
||||
r">{\\raggedleft\\arraybackslash}p{0.09\\textwidth}"
|
||||
r">{\\raggedleft\\arraybackslash}p{0.12\\textwidth}"
|
||||
r">{\\raggedleft\\arraybackslash}p{0.13\\textwidth}"
|
||||
r"@{}"
|
||||
)
|
||||
|
||||
five_col_patterns = [
|
||||
(
|
||||
# tabularx with first col l/c, X desc, then three p{} numeric cols (matches default invoice template)
|
||||
r"(\\begin{tabularx}\{\s*\\textwidth\s*\}\{)\s*@?\{\}?\s*[cl]\s+X\s+p\{[^}]+\}\s+p\{[^}]+\}\s+p\{[^}]+\}\s*@?\{\}?\s*(\})",
|
||||
r"\1" + wrapped_invoice_five + r"\2",
|
||||
),
|
||||
(
|
||||
# longtable version of the same layout (after upgrades)
|
||||
r"(\\begin{longtable}\{)\s*@?\{\}?\s*[cl]\s+X\s+p\{[^}]+\}\s+p\{[^}]+\}\s+p\{[^}]+\}\s*@?\{\}?\s*(\})",
|
||||
r"\1" + wrapped_invoice_five + r"\2",
|
||||
),
|
||||
]
|
||||
|
||||
for pattern, replacement in five_col_patterns:
|
||||
code = re.sub(pattern, replacement, code, flags=re.IGNORECASE)
|
||||
|
||||
return code
|
||||
|
||||
|
||||
def normalize_tabular_like_begins(latex_code: str) -> str:
|
||||
"""
|
||||
Fix common malformed tabular/tabularx/longtable begins where the colspec
|
||||
is not passed as a braced argument (e.g. `\\begin{tabularx}\\textwidth{...}`
|
||||
or `\\begin{tabularx}{\\textwidth}\\ItemsColSpec`).
|
||||
"""
|
||||
if not latex_code:
|
||||
return latex_code
|
||||
|
||||
code = latex_code
|
||||
|
||||
# \begin{tabularx}\textwidth{...} -> \begin{tabularx}{\textwidth}{...}
|
||||
code = re.sub(
|
||||
r"\\begin{tabularx}\s*\\textwidth\s*\{([^}]*)\}",
|
||||
lambda m: f"\\begin{{tabularx}}{{\\textwidth}}{{{m.group(1).strip()}}}",
|
||||
code,
|
||||
)
|
||||
|
||||
# \begin{tabularx}{\textwidth}\ItemsColSpec -> wrap colspec in braces
|
||||
code = re.sub(
|
||||
r"\\begin{tabularx}\s*\{\s*\\textwidth\s*\}\s*\\([A-Za-z@][\w@]*)",
|
||||
lambda m: f"\\begin{{tabularx}}{{\\textwidth}}{{\\{m.group(1)}}}",
|
||||
code,
|
||||
)
|
||||
|
||||
# \begin{tabularx}{\textwidth}\colspecliteral -> wrap literal spec
|
||||
code = re.sub(
|
||||
r"\\begin{tabularx}\s*\{\s*\\textwidth\s*\}\s*([@A-Za-z].*)",
|
||||
lambda m: f"\\begin{{tabularx}}{{\\textwidth}}{{{m.group(1).strip()}}}",
|
||||
code,
|
||||
)
|
||||
|
||||
# \begin{tabular}\colspecliteral OR \begin{longtable}\colspecliteral
|
||||
def _wrap_simple(env: str, text: str) -> str:
|
||||
return re.sub(
|
||||
rf"\\begin{{{env}}}\s*([@A-Za-z].*)",
|
||||
lambda m: f"\\begin{{{env}}}{{{m.group(1).strip()}}}",
|
||||
text,
|
||||
)
|
||||
|
||||
code = _wrap_simple("tabular", code)
|
||||
code = _wrap_simple("longtable", code)
|
||||
return code
|
||||
|
||||
|
||||
def ensure_longtable_support(latex_code: str) -> str:
|
||||
"""
|
||||
Guarantee longtable availability and default centering.
|
||||
|
||||
- Injects \\usepackage{longtable} if missing.
|
||||
- Sets \\LTleft/\\LTright to 0pt so longtable spans the text width without manual centering.
|
||||
"""
|
||||
if not latex_code:
|
||||
return latex_code
|
||||
|
||||
code = latex_code
|
||||
if r"\usepackage{longtable}" not in code:
|
||||
code = re.sub(
|
||||
r"(\\documentclass[^\n]*\n)",
|
||||
r"\1\\usepackage{longtable}\n",
|
||||
code,
|
||||
count=1,
|
||||
)
|
||||
if r"\setlength\LTleft" not in code:
|
||||
# Use a lambda so backslashes are treated literally (avoid \L escape errors).
|
||||
code = re.sub(
|
||||
r"(\\usepackage\{longtable\}[^\n]*\n)",
|
||||
lambda m: f"{m.group(1)}\\setlength\\LTleft{{0pt}}\n\\setlength\\LTright{{0pt}}\n",
|
||||
code,
|
||||
count=1,
|
||||
)
|
||||
return code
|
||||
|
||||
|
||||
def _normalize_alignment_to_wrapped_columns(spec: str) -> str:
|
||||
"""
|
||||
Convert simple l/c/r specs to wrapped p-columns that respect text width.
|
||||
Keeps existing p/m/b/X columns unchanged.
|
||||
"""
|
||||
if re.search(r"[pmb]\{|\bX\b", spec):
|
||||
return spec
|
||||
|
||||
cols = [ch for ch in spec if ch in ("l", "c", "r")]
|
||||
if not cols:
|
||||
return spec
|
||||
|
||||
width = max(0.05, min(0.98, 0.98 / len(cols)))
|
||||
parts: List[str] = []
|
||||
for ch in cols:
|
||||
if ch == "r":
|
||||
parts.append(r">{\raggedleft\arraybackslash}p{" + f"{width:.3f}\\textwidth" + "}")
|
||||
else:
|
||||
parts.append(r">{\raggedright\arraybackslash}p{" + f"{width:.3f}\\textwidth" + "}")
|
||||
return "@{}" + "".join(parts) + "@{}"
|
||||
|
||||
|
||||
def upgrade_tabular_tables_to_longtable(latex_code: str) -> str:
|
||||
"""
|
||||
Replace table+tabular blocks with longtable so large tables break across pages,
|
||||
stay centered, and repeat headers on each page.
|
||||
"""
|
||||
if not latex_code:
|
||||
return latex_code
|
||||
|
||||
pattern = re.compile(
|
||||
r"\\begin{table}.*?\\begin{tabular}\{([^}]*)\}(.*?)\\end{tabular}.*?\\end{table}",
|
||||
re.DOTALL,
|
||||
)
|
||||
|
||||
def _build_longtable(match: re.Match[str]) -> str:
|
||||
align_spec = match.group(1)
|
||||
body = match.group(2).strip()
|
||||
normalized_spec = _normalize_alignment_to_wrapped_columns(align_spec)
|
||||
|
||||
header_block = ""
|
||||
body_block = body
|
||||
|
||||
hline_split = re.split(r"\\hline", body, maxsplit=1)
|
||||
if len(hline_split) == 2:
|
||||
header_block = hline_split[0].strip() + r"\\\hline"
|
||||
body_block = hline_split[1].lstrip()
|
||||
else:
|
||||
first_row_split = re.split(r"\\\\", body, maxsplit=1)
|
||||
header_block = (first_row_split[0].strip() + r"\\") if first_row_split else ""
|
||||
body_block = first_row_split[1].lstrip() if len(first_row_split) == 2 else body
|
||||
|
||||
header_block = header_block.strip()
|
||||
body_block = body_block.strip()
|
||||
|
||||
return (
|
||||
"\n\\setlength\\LTleft{0pt}\n"
|
||||
"\\setlength\\LTright{0pt}\n"
|
||||
f"\\begin{{longtable}}{{{normalized_spec}}}\n"
|
||||
f"{header_block}\n"
|
||||
"\\endfirsthead\n"
|
||||
f"{header_block}\n"
|
||||
"\\endhead\n"
|
||||
f"{body_block}\n"
|
||||
"\\end{longtable}\n"
|
||||
)
|
||||
|
||||
return pattern.sub(_build_longtable, latex_code)
|
||||
|
||||
|
||||
def clean_generated_latex(latex_code: str) -> str:
|
||||
"""Apply all sanitizers used both for compilation and template storage."""
|
||||
return rebalance_invoice_tables(
|
||||
upgrade_tabular_tables_to_longtable(
|
||||
ensure_longtable_support(
|
||||
normalize_tabular_like_begins(
|
||||
strip_number_grouping_junk(
|
||||
fix_tabular_row_endings(
|
||||
strip_leading_pagebreaks(
|
||||
remove_leading_pagebreaks(
|
||||
strip_missing_packages(
|
||||
sanitize_latex(
|
||||
ensure_full_latex_document(latex_code)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def apply_style_overrides(latex_code: str, style_profile: dict) -> str:
|
||||
"""Apply deterministic font + accent styling without altering layout."""
|
||||
if not latex_code:
|
||||
return latex_code
|
||||
|
||||
code = latex_code
|
||||
font = (style_profile or {}).get("font_preference") or ""
|
||||
accent = (style_profile or {}).get("color_accent") or ""
|
||||
|
||||
font_map = {
|
||||
"serif": ("mathpazo", "\\renewcommand{\\familydefault}{\\rmdefault}"),
|
||||
"sans": ("helvet", "\\renewcommand{\\familydefault}{\\sfdefault}"),
|
||||
"helvet": ("helvet", "\\renewcommand{\\familydefault}{\\sfdefault}"),
|
||||
"mono": ("courier", "\\renewcommand{\\familydefault}{\\ttdefault}"),
|
||||
"modern": ("lmodern", None),
|
||||
}
|
||||
pkg = None
|
||||
family_cmd = None
|
||||
if isinstance(font, str):
|
||||
pkg, family_cmd = font_map.get(font.lower(), (None, None))
|
||||
|
||||
if pkg:
|
||||
code = re.sub(
|
||||
r"^\\usepackage\{(helvet|mathpazo|lmodern|courier)\}\s*$",
|
||||
"",
|
||||
code,
|
||||
flags=re.MULTILINE,
|
||||
)
|
||||
code = re.sub(
|
||||
r"^\\renewcommand\{\\familydefault\}\{\\(sfdefault|rmdefault|ttdefault)\}\s*$",
|
||||
"",
|
||||
code,
|
||||
flags=re.MULTILINE,
|
||||
)
|
||||
|
||||
def _ensure_package_and_command(text: str) -> str:
|
||||
if not pkg:
|
||||
return text
|
||||
insert = f"\\usepackage{{{pkg}}}\n"
|
||||
if family_cmd:
|
||||
insert += family_cmd + "\n"
|
||||
if r"\begin{document}" in text:
|
||||
return re.sub(r"(\\begin\{document\})", insert + r"\1", text, count=1)
|
||||
return insert + text
|
||||
|
||||
code = _ensure_package_and_command(code)
|
||||
|
||||
if accent:
|
||||
accent_hex_match = re.fullmatch(r"#?([0-9a-fA-F]{6})", str(accent).strip())
|
||||
if accent_hex_match:
|
||||
accent_line = f"\\definecolor{{accent}}{{HTML}}{{{accent_hex_match.group(1).upper()}}}"
|
||||
else:
|
||||
accent_name = re.sub(r"[^A-Za-z]+", "", str(accent)) or "blue"
|
||||
accent_line = f"\\colorlet{{accent}}{{{accent_name}}}"
|
||||
|
||||
if re.search(r"^\\definecolor\{accent\}|^\\colorlet\{accent\}", code, flags=re.MULTILINE):
|
||||
code = re.sub(r"^\\definecolor\{accent\}.*$", accent_line, code, flags=re.MULTILINE)
|
||||
code = re.sub(r"^\\colorlet\{accent\}.*$", accent_line, code, flags=re.MULTILINE)
|
||||
else:
|
||||
needs_xcolor = r"\usepackage{xcolor}" not in code
|
||||
insert = ""
|
||||
if needs_xcolor:
|
||||
insert += "\\usepackage{xcolor}\n"
|
||||
insert += accent_line + "\n"
|
||||
if r"\usepackage{xcolor}" in code:
|
||||
code = re.sub(r"(\\usepackage\{xcolor\}[^\n]*\n)", r"\1" + insert, code, count=1)
|
||||
elif r"\begin{document}" in code:
|
||||
code = re.sub(r"(\\begin\{document\})", insert + r"\1", code, count=1)
|
||||
else:
|
||||
code = insert + code
|
||||
|
||||
return code
|
||||
|
||||
|
||||
def ensure_full_latex_document(text: str) -> str:
|
||||
"""Trim output to a single LaTeX document starting at \\documentclass and ending at \\end{document}."""
|
||||
if not text:
|
||||
return text
|
||||
start = text.find(r"\documentclass")
|
||||
end = text.rfind(r"\end{document}")
|
||||
if start == -1 or end == -1:
|
||||
return text
|
||||
end += len(r"\end{document}")
|
||||
return text[start:end]
|
||||
|
||||
|
||||
__all__ = [
|
||||
"extract_layout_hint",
|
||||
"sanitize_latex",
|
||||
"strip_missing_packages",
|
||||
"remove_leading_pagebreaks",
|
||||
"strip_leading_pagebreaks",
|
||||
"fix_tabular_row_endings",
|
||||
"rebalance_invoice_tables",
|
||||
"clean_generated_latex",
|
||||
"apply_style_overrides",
|
||||
"ensure_full_latex_document",
|
||||
]
|
||||
@@ -0,0 +1,491 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
import tempfile
|
||||
import xml.etree.ElementTree as ET
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
from config import logger
|
||||
|
||||
|
||||
def _safe_float(value: Optional[str], fallback: float = 0.0) -> float:
|
||||
"""Convert an attribute value to float while handling bad input."""
|
||||
try:
|
||||
if value is None:
|
||||
return fallback
|
||||
return float(value)
|
||||
except (TypeError, ValueError):
|
||||
return fallback
|
||||
|
||||
|
||||
def _read_image_as_data_url(path: str) -> Optional[str]:
|
||||
"""Return a data URL for the image if it exists."""
|
||||
if not os.path.exists(path):
|
||||
return None
|
||||
mime = "image/png"
|
||||
_, ext = os.path.splitext(path)
|
||||
if ext.lower() in {".jpg", ".jpeg"}:
|
||||
mime = "image/jpeg"
|
||||
elif ext.lower() == ".gif":
|
||||
mime = "image/gif"
|
||||
try:
|
||||
with open(path, "rb") as img_handle:
|
||||
encoded = base64.b64encode(img_handle.read()).decode("ascii")
|
||||
return f"data:{mime};base64,{encoded}"
|
||||
except OSError as exc:
|
||||
logger.warning("[PDF-EDITOR] Failed to read image %s: %s", path, exc)
|
||||
return None
|
||||
|
||||
|
||||
def _parse_fonts(root: ET.Element) -> List[Dict[str, Any]]:
|
||||
fonts: List[Dict[str, Any]] = []
|
||||
for spec in root.findall(".//fontspec"):
|
||||
font_id = spec.attrib.get("id")
|
||||
base_name = spec.attrib.get("family")
|
||||
size = _safe_float(spec.attrib.get("size"), 12.0)
|
||||
color = spec.attrib.get("color")
|
||||
name_lower = (base_name or "").lower()
|
||||
flags = 0
|
||||
if "bold" in name_lower:
|
||||
flags |= 0x100 # ForceBold
|
||||
if "italic" in name_lower or "oblique" in name_lower:
|
||||
flags |= 0x40 # Italic
|
||||
fonts.append(
|
||||
{
|
||||
"id": font_id,
|
||||
"uid": font_id,
|
||||
"baseName": base_name,
|
||||
"embedded": True,
|
||||
"program": None,
|
||||
"programFormat": None,
|
||||
"webProgram": None,
|
||||
"webProgramFormat": None,
|
||||
"pdfProgram": None,
|
||||
"pdfProgramFormat": None,
|
||||
"ascent": size,
|
||||
"descent": -size * 0.25,
|
||||
"unitsPerEm": max(size, 1),
|
||||
"standard14Name": None,
|
||||
"color": color,
|
||||
"fontDescriptorFlags": flags or None,
|
||||
}
|
||||
)
|
||||
return fonts
|
||||
|
||||
|
||||
def _parse_color_components(color: Optional[str]) -> Optional[List[float]]:
|
||||
"""Convert a hex/rgb color string into normalized RGB components."""
|
||||
if not color:
|
||||
return None
|
||||
|
||||
color = color.strip()
|
||||
hex_match = re.fullmatch(r"#?([0-9a-fA-F]{6})", color)
|
||||
short_hex_match = re.fullmatch(r"#?([0-9a-fA-F]{3})", color)
|
||||
rgb_match = re.fullmatch(r"rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)", color, re.IGNORECASE)
|
||||
|
||||
if hex_match:
|
||||
hex_value = hex_match.group(1)
|
||||
r = int(hex_value[0:2], 16)
|
||||
g = int(hex_value[2:4], 16)
|
||||
b = int(hex_value[4:6], 16)
|
||||
return [r / 255.0, g / 255.0, b / 255.0]
|
||||
|
||||
if short_hex_match:
|
||||
hex_value = short_hex_match.group(1)
|
||||
r = int(hex_value[0] * 2, 16)
|
||||
g = int(hex_value[1] * 2, 16)
|
||||
b = int(hex_value[2] * 2, 16)
|
||||
return [r / 255.0, g / 255.0, b / 255.0]
|
||||
|
||||
if rgb_match:
|
||||
r = min(max(int(rgb_match.group(1)), 0), 255)
|
||||
g = min(max(int(rgb_match.group(2)), 0), 255)
|
||||
b = min(max(int(rgb_match.group(3)), 0), 255)
|
||||
return [r / 255.0, g / 255.0, b / 255.0]
|
||||
|
||||
return None
|
||||
|
||||
|
||||
# --------------------
|
||||
# Table normalization
|
||||
# --------------------
|
||||
|
||||
def _cluster(values: List[float], tol: float) -> List[List[float]]:
|
||||
clusters: List[List[float]] = []
|
||||
for v in sorted(values):
|
||||
if not clusters or abs(v - clusters[-1][-1]) > tol:
|
||||
clusters.append([v])
|
||||
else:
|
||||
clusters[-1].append(v)
|
||||
return clusters
|
||||
|
||||
def _dedupe_by_xy_text(elements: List[Dict[str, Any]], eps: float = 1.0) -> List[Dict[str, Any]]:
|
||||
"""
|
||||
Dedupe using quantized x,y and normalized text (ignores width/height jitter).
|
||||
Keeps the better scoring text; if equal text, keeps the first.
|
||||
"""
|
||||
if not elements:
|
||||
return elements
|
||||
|
||||
def _key(el: Dict[str, Any]) -> tuple[int, int, str]:
|
||||
x = el.get("x") or 0.0
|
||||
y = el.get("y") or 0.0
|
||||
t = (el.get("text") or "").strip().replace("\u00a0", " ")
|
||||
return (int(round(x / eps)), int(round(y / eps)), t)
|
||||
|
||||
def _score_text_global(t: str) -> tuple[int, int, int]:
|
||||
stripped = t.strip()
|
||||
has_currency = 1 if any(sym in stripped for sym in ("$", "€", "£", "¥")) else 0
|
||||
non_space = sum(1 for ch in stripped if not ch.isspace())
|
||||
digits = sum(1 for ch in stripped if ch.isdigit())
|
||||
return (has_currency, non_space, digits)
|
||||
|
||||
deduped: Dict[tuple[int, int, str], Dict[str, Any]] = {}
|
||||
for el in elements:
|
||||
key = _key(el)
|
||||
existing = deduped.get(key)
|
||||
if existing is None:
|
||||
deduped[key] = el
|
||||
continue
|
||||
t_new = key[2]
|
||||
t_old = (existing.get("text") or "").strip().replace("\u00a0", " ")
|
||||
score_new = _score_text_global(t_new)
|
||||
score_old = _score_text_global(t_old)
|
||||
if score_new > score_old:
|
||||
deduped[key] = el
|
||||
return list(deduped.values())
|
||||
|
||||
|
||||
def _detect_table_region(text_elements: List[Dict[str, Any]], page_width: float) -> Optional[Dict[str, Any]]:
|
||||
"""
|
||||
Header-agnostic table detection via x clustering.
|
||||
Returns dict with anchors, boundaries, observed_left/right, y_min/y_max.
|
||||
"""
|
||||
candidates = [
|
||||
el
|
||||
for el in text_elements
|
||||
if el.get("text") not in (None, "")
|
||||
and isinstance(el.get("x"), (int, float))
|
||||
and isinstance(el.get("y"), (int, float))
|
||||
and isinstance(el.get("height"), (int, float))
|
||||
]
|
||||
if len(candidates) < 8:
|
||||
return None
|
||||
|
||||
heights = [c["height"] for c in candidates if c.get("height")]
|
||||
if not heights:
|
||||
return None
|
||||
med_h = sorted(heights)[len(heights) // 2]
|
||||
short_candidates = [c for c in candidates if c["height"] <= med_h * 1.8]
|
||||
if len(short_candidates) < 8:
|
||||
return None
|
||||
|
||||
x_centers = [c["x"] + (c.get("width") or 0) * 0.5 for c in short_candidates]
|
||||
x_clusters = _cluster(x_centers, tol=12.0)
|
||||
x_clusters = [c for c in x_clusters if len(c) >= 3]
|
||||
if len(x_clusters) < 4:
|
||||
return None
|
||||
|
||||
anchors = [sum(c) / len(c) for c in x_clusters]
|
||||
anchors.sort()
|
||||
|
||||
y_vals = sorted(c["y"] for c in short_candidates)
|
||||
y_clusters = _cluster(y_vals, tol=3.0)
|
||||
if len(y_clusters) < 3:
|
||||
return None
|
||||
y_clusters.sort(key=lambda c: len(c), reverse=True)
|
||||
y_min = min(y_clusters[0])
|
||||
y_max = max(y_clusters[0])
|
||||
|
||||
min_anchor = min(anchors)
|
||||
max_anchor = max(anchors)
|
||||
PAD = 12.0
|
||||
|
||||
band_elems = []
|
||||
for el in short_candidates:
|
||||
cx = el["x"] + (el.get("width") or 0) * 0.5
|
||||
if y_min - PAD <= el["y"] <= y_max + PAD and (min_anchor - PAD) <= cx <= (max_anchor + PAD):
|
||||
band_elems.append(el)
|
||||
if not band_elems:
|
||||
return None
|
||||
observed_left = min(n["x"] for n in band_elems)
|
||||
observed_right = max(n["x"] + (n.get("width") or 0) for n in band_elems)
|
||||
|
||||
raw_bounds: List[float] = []
|
||||
for i, ax in enumerate(anchors):
|
||||
if i == 0:
|
||||
gap = anchors[1] - anchors[0]
|
||||
raw_bounds.append(ax - gap * 0.5)
|
||||
else:
|
||||
raw_bounds.append((anchors[i - 1] + ax) * 0.5)
|
||||
gap_last = anchors[-1] - anchors[-2] if len(anchors) > 1 else 40.0
|
||||
raw_bounds.append(anchors[-1] + gap_last * 0.5)
|
||||
|
||||
# translate boundaries to align left edge; no scaling to preserve spacing
|
||||
delta = observed_left - raw_bounds[0]
|
||||
boundaries = [b + delta for b in raw_bounds]
|
||||
|
||||
info = {
|
||||
"anchors": anchors,
|
||||
"boundaries": boundaries,
|
||||
"observed_left": observed_left,
|
||||
"observed_right": observed_right,
|
||||
"y_min": y_min,
|
||||
"y_max": y_max,
|
||||
}
|
||||
if os.getenv("PDF_EDITOR_TABLE_DEBUG"):
|
||||
logger.debug(
|
||||
"[PDF-EDITOR] table detected anchors=%s boundaries=%s y=(%.2f, %.2f) span=(%.2f, %.2f)",
|
||||
anchors,
|
||||
boundaries,
|
||||
y_min,
|
||||
y_max,
|
||||
observed_left,
|
||||
observed_right,
|
||||
)
|
||||
span = boundaries[-1] - boundaries[0]
|
||||
if span <= 0 or page_width <= 0:
|
||||
return None
|
||||
target_left = max(0.0, (page_width - span) * 0.5)
|
||||
offset = target_left - boundaries[0]
|
||||
info.update({"offset": offset, "page_width": page_width})
|
||||
return info
|
||||
|
||||
|
||||
def _snap_table_elements(text_elements: List[Dict[str, Any]], info: Dict[str, Any]) -> List[Dict[str, Any]]:
|
||||
anchors = info["anchors"]
|
||||
base_boundaries = info["boundaries"]
|
||||
offset = info.get("offset", 0.0)
|
||||
boundaries = [b + offset for b in base_boundaries]
|
||||
y_min = info["y_min"]
|
||||
y_max = info["y_max"]
|
||||
left = info["observed_left"] + offset
|
||||
right = info["observed_right"] + offset
|
||||
|
||||
def _assign_col(cx: float) -> int:
|
||||
return min(range(len(anchors)), key=lambda i: abs(anchors[i] - cx))
|
||||
|
||||
EPS = 1.0
|
||||
PAD = 6.0
|
||||
|
||||
for el in text_elements:
|
||||
x = el.get("x")
|
||||
w = el.get("width") or 0
|
||||
y = el.get("y")
|
||||
if not isinstance(x, (int, float)) or not isinstance(y, (int, float)):
|
||||
continue
|
||||
cx = x + w * 0.5 + offset
|
||||
if not (left - PAD <= cx <= right + PAD and y_min - PAD <= y <= y_max + PAD):
|
||||
continue
|
||||
|
||||
col = _assign_col(cx - offset)
|
||||
col_left = boundaries[col]
|
||||
col_right = boundaries[col + 1]
|
||||
|
||||
new_left = x + offset
|
||||
new_right = x + w + offset
|
||||
if new_left < col_left - EPS:
|
||||
new_left = col_left
|
||||
if new_right > col_right + EPS:
|
||||
new_right = col_right
|
||||
if new_right <= new_left:
|
||||
mid = (col_left + col_right) * 0.5
|
||||
new_left = mid - 0.5
|
||||
new_right = mid + 0.5
|
||||
|
||||
el["x"] = new_left
|
||||
el["width"] = max(1.0, new_right - new_left)
|
||||
el["textMatrix"] = [1, 0, 0, 1, el["x"], el["y"]]
|
||||
|
||||
return text_elements
|
||||
|
||||
|
||||
def _parse_page(page_elem: ET.Element, base_dir: str, font_colors: Dict[str, Optional[str]]) -> Dict[str, Any]:
|
||||
page_width = _safe_float(page_elem.attrib.get("width"), 612.0)
|
||||
page_height = _safe_float(page_elem.attrib.get("height"), 792.0)
|
||||
text_elements: List[Dict[str, Any]] = []
|
||||
image_elements: List[Dict[str, Any]] = []
|
||||
|
||||
# Cluster near-identical text draws on the same baseline and keep the best candidate.
|
||||
EPS = 1.0
|
||||
best_by_pos: Dict[tuple[int, int], Dict[str, Any]] = {}
|
||||
|
||||
def _q(value: float) -> int:
|
||||
try:
|
||||
return int(round(value / EPS))
|
||||
except Exception:
|
||||
return 0
|
||||
|
||||
def _score_text(t: str) -> tuple[int, int, int]:
|
||||
stripped = t.strip()
|
||||
has_currency = 1 if any(sym in stripped for sym in ("$", "€", "£", "¥")) else 0
|
||||
non_space = sum(1 for ch in stripped if not ch.isspace())
|
||||
digits = sum(1 for ch in stripped if ch.isdigit())
|
||||
return (has_currency, non_space, digits)
|
||||
|
||||
for index, text_elem in enumerate(page_elem.findall("text")):
|
||||
raw_text = "".join(text_elem.itertext()).replace("\u00A0", " ")
|
||||
text = raw_text.strip("\n")
|
||||
left = _safe_float(text_elem.attrib.get("left"))
|
||||
top = _safe_float(text_elem.attrib.get("top"))
|
||||
width = _safe_float(text_elem.attrib.get("width"))
|
||||
height = _safe_float(text_elem.attrib.get("height"))
|
||||
font_id = text_elem.attrib.get("font")
|
||||
font_color = font_colors.get(font_id) if font_id else None
|
||||
fill_components = _parse_color_components(font_color)
|
||||
|
||||
candidate = {
|
||||
"id": f"t-{index}",
|
||||
"text": text,
|
||||
"fontId": font_id,
|
||||
"fontSize": height if height > 0 else None,
|
||||
"x": left,
|
||||
"y": page_height - top,
|
||||
"width": width,
|
||||
"height": height,
|
||||
"textMatrix": [1, 0, 0, 1, left, page_height - top],
|
||||
"fillColor": {"colorSpace": "RGB", "components": fill_components} if fill_components else None,
|
||||
}
|
||||
|
||||
pos_key = (_q(left), _q(page_height - top))
|
||||
|
||||
existing = best_by_pos.get(pos_key)
|
||||
if existing is None:
|
||||
best_by_pos[pos_key] = candidate
|
||||
else:
|
||||
if _score_text(candidate["text"]) > _score_text(existing["text"]):
|
||||
best_by_pos[pos_key] = candidate
|
||||
|
||||
# Optional merge of adjacent runs on the same baseline (e.g., "$" + "38.00")
|
||||
merged: List[Dict[str, Any]] = []
|
||||
base_elements = _dedupe_by_xy_text(sorted(best_by_pos.values(), key=lambda i: (i["y"], i["x"])), eps=1.0)
|
||||
# Sort by baseline (y) then x to make merges stable and ordering deterministic
|
||||
for item in sorted(base_elements, key=lambda i: (i["y"], i["x"])):
|
||||
if not merged:
|
||||
merged.append(item)
|
||||
continue
|
||||
prev = merged[-1]
|
||||
same_line = _q(prev["y"]) == _q(item["y"])
|
||||
if not same_line:
|
||||
merged.append(item)
|
||||
continue
|
||||
prev_right = prev["x"] + (prev.get("width") or 0)
|
||||
gap = item["x"] - prev_right
|
||||
max_h = max(prev.get("height") or 0, item.get("height") or 0)
|
||||
allowed_gap = max(2.0, 0.25 * max_h)
|
||||
if gap <= allowed_gap and gap >= -allowed_gap:
|
||||
# Merge
|
||||
needs_space = (
|
||||
prev["text"].strip() != ""
|
||||
and item["text"].strip() != ""
|
||||
and not prev["text"].endswith(" ")
|
||||
and not item["text"].startswith(" ")
|
||||
and not prev["text"].rstrip().endswith(("$", "€", "£", "¥"))
|
||||
)
|
||||
merged_text = prev["text"] + (" " if needs_space else "") + item["text"]
|
||||
new_left = min(prev["x"], item["x"])
|
||||
new_right = max(prev_right, item["x"] + (item.get("width") or 0))
|
||||
prev.update(
|
||||
{
|
||||
"text": merged_text,
|
||||
"x": new_left,
|
||||
"width": new_right - new_left,
|
||||
"height": max_h,
|
||||
# keep y and fontId from the left-most run
|
||||
}
|
||||
)
|
||||
else:
|
||||
merged.append(item)
|
||||
|
||||
# Phase A: dedupe by xy+text to remove duplicate draws
|
||||
deduped = _dedupe_by_xy_text(merged, eps=1.0)
|
||||
|
||||
# Phase B: table detection + snapping (header-agnostic)
|
||||
table_info = _detect_table_region(deduped, page_width)
|
||||
if table_info:
|
||||
snapped = _snap_table_elements(deduped, table_info)
|
||||
text_elements = _dedupe_by_xy_text(snapped, eps=1.0)
|
||||
else:
|
||||
text_elements = deduped
|
||||
|
||||
for img_index, image_elem in enumerate(page_elem.findall("image")):
|
||||
left = _safe_float(image_elem.attrib.get("left"))
|
||||
top = _safe_float(image_elem.attrib.get("top"))
|
||||
width = _safe_float(image_elem.attrib.get("width"))
|
||||
height = _safe_float(image_elem.attrib.get("height"))
|
||||
src = image_elem.attrib.get("src")
|
||||
image_path = os.path.join(base_dir, src) if src else None
|
||||
data_url = _read_image_as_data_url(image_path) if image_path else None
|
||||
image_elements.append(
|
||||
{
|
||||
"id": src or f"image-{img_index}",
|
||||
"objectName": src,
|
||||
"x": left,
|
||||
"y": max(page_height - top - height, 0),
|
||||
"width": width,
|
||||
"height": height,
|
||||
"left": left,
|
||||
"top": top,
|
||||
"bottom": max(page_height - top, 0),
|
||||
"right": left + width,
|
||||
"imageData": data_url,
|
||||
"imageFormat": os.path.splitext(src)[1][1:] if src else None,
|
||||
}
|
||||
)
|
||||
|
||||
return {
|
||||
"width": page_width,
|
||||
"height": page_height,
|
||||
"pageNumber": _safe_float(page_elem.attrib.get("number"), 0),
|
||||
"textElements": text_elements,
|
||||
"imageElements": image_elements,
|
||||
}
|
||||
|
||||
|
||||
def convert_pdf_to_text_editor_document(pdf_path: str) -> Dict[str, Any]:
|
||||
"""Convert a PDF to a JSON payload usable by the PDF text editor."""
|
||||
if not os.path.exists(pdf_path):
|
||||
raise FileNotFoundError(pdf_path)
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
output_base = os.path.join(tmpdir, "doc")
|
||||
command = [
|
||||
"pdftohtml",
|
||||
"-xml",
|
||||
"-enc",
|
||||
"UTF-8",
|
||||
"-nodrm",
|
||||
"-q",
|
||||
pdf_path,
|
||||
output_base,
|
||||
]
|
||||
try:
|
||||
subprocess.run(command, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
except (FileNotFoundError, subprocess.CalledProcessError) as exc:
|
||||
logger.error("[PDF-EDITOR] pdftohtml failed for %s: %s", pdf_path, exc)
|
||||
raise
|
||||
|
||||
xml_path = f"{output_base}.xml"
|
||||
if not os.path.exists(xml_path):
|
||||
raise FileNotFoundError(xml_path)
|
||||
|
||||
tree = ET.parse(xml_path)
|
||||
root = tree.getroot()
|
||||
|
||||
fonts = _parse_fonts(root)
|
||||
font_colors = {font["id"]: font.get("color") for font in fonts if font.get("id")}
|
||||
pages = [_parse_page(page_elem, tmpdir, font_colors) for page_elem in root.findall("page")]
|
||||
|
||||
document: Dict[str, Any] = {
|
||||
"metadata": {"numberOfPages": len(pages)},
|
||||
"fonts": fonts,
|
||||
"pages": pages,
|
||||
"lazyImages": False,
|
||||
}
|
||||
return {"document": document}
|
||||
|
||||
|
||||
__all__ = ["convert_pdf_to_text_editor_document"]
|
||||
@@ -0,0 +1,147 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
import os
|
||||
import subprocess
|
||||
import tempfile
|
||||
import time
|
||||
from typing import List, Optional
|
||||
|
||||
from config import OUTPUT_DIR, logger
|
||||
|
||||
|
||||
_FONT_SPEC_MARKERS = (
|
||||
"\\usepackage{fontspec}",
|
||||
"\\setmainfont",
|
||||
"\\setsansfont",
|
||||
"\\setmonofont",
|
||||
"\\newfontfamily",
|
||||
)
|
||||
|
||||
|
||||
def _needs_unicode_engine(latex_code: str) -> bool:
|
||||
return any(marker in latex_code for marker in _FONT_SPEC_MARKERS)
|
||||
|
||||
|
||||
def _run_latex(engine: str, tex_filename: str) -> subprocess.CompletedProcess:
|
||||
return subprocess.run(
|
||||
[engine, "-interaction=nonstopmode", "-output-directory", OUTPUT_DIR, tex_filename],
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
timeout=30,
|
||||
cwd=OUTPUT_DIR,
|
||||
)
|
||||
|
||||
|
||||
def render_pdf_to_images(pdf_bytes: bytes, max_pages: int = 2, dpi: int = 160) -> List[str]:
|
||||
"""Render the first N pages of a PDF to base64-encoded PNG data URLs."""
|
||||
images: List[str] = []
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
pdf_path = os.path.join(tmpdir, "upload.pdf")
|
||||
with open(pdf_path, "wb") as handle:
|
||||
handle.write(pdf_bytes)
|
||||
|
||||
output_prefix = os.path.join(tmpdir, "page")
|
||||
try:
|
||||
subprocess.run(
|
||||
[
|
||||
"pdftoppm",
|
||||
"-png",
|
||||
"-r",
|
||||
str(dpi),
|
||||
"-f",
|
||||
"1",
|
||||
"-l",
|
||||
str(max_pages),
|
||||
pdf_path,
|
||||
output_prefix,
|
||||
],
|
||||
check=True,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
timeout=30,
|
||||
)
|
||||
except Exception as exc:
|
||||
logger.error("[IMPORT] pdftoppm failed: %s", exc)
|
||||
return images
|
||||
|
||||
for idx in range(1, max_pages + 1):
|
||||
img_path = f"{output_prefix}-{idx}.png"
|
||||
if os.path.exists(img_path):
|
||||
with open(img_path, "rb") as img_handle:
|
||||
encoded = base64.b64encode(img_handle.read()).decode("utf-8")
|
||||
images.append(f"data:image/png;base64,{encoded}")
|
||||
return images
|
||||
|
||||
|
||||
def compile_latex_to_pdf(
|
||||
latex_code: str,
|
||||
job_id: str,
|
||||
*,
|
||||
log_errors: bool = True,
|
||||
raise_on_error: bool = False,
|
||||
) -> Optional[str]:
|
||||
"""Compile a LaTeX document and return the PDF path."""
|
||||
tex_filename = os.path.join(OUTPUT_DIR, f"{job_id}.tex")
|
||||
pdf_filename = f"{job_id}.pdf"
|
||||
pdf_path = os.path.join(OUTPUT_DIR, pdf_filename)
|
||||
|
||||
with open(tex_filename, "w", encoding="utf-8") as handle:
|
||||
handle.write(latex_code)
|
||||
|
||||
try:
|
||||
t_start = time.perf_counter()
|
||||
engine = "xelatex" if _needs_unicode_engine(latex_code) else "pdflatex"
|
||||
first = _run_latex(engine, tex_filename)
|
||||
if first.returncode != 0 and engine == "pdflatex":
|
||||
error_output = first.stderr.decode() or first.stdout.decode()
|
||||
if "fontspec" in error_output and ("XeTeX" in error_output or "LuaTeX" in error_output):
|
||||
logger.info("[PDF] pdflatex failed due to fontspec; retrying with xelatex")
|
||||
engine = "xelatex"
|
||||
first = _run_latex(engine, tex_filename)
|
||||
t_first = time.perf_counter()
|
||||
if first.returncode == 0:
|
||||
_run_latex(engine, tex_filename)
|
||||
t_end = time.perf_counter()
|
||||
|
||||
if os.path.exists(pdf_path):
|
||||
logger.info(
|
||||
"[PDF] compiled job_id=%s -> %s (engine=%s first_pass=%.2fs total=%.2fs)",
|
||||
job_id,
|
||||
pdf_filename,
|
||||
engine,
|
||||
t_first - t_start,
|
||||
t_end - t_start,
|
||||
)
|
||||
return pdf_path
|
||||
|
||||
error_output = first.stderr.decode() or first.stdout.decode()
|
||||
message = error_output.strip() or f"{engine} failed without stderr output"
|
||||
if log_errors:
|
||||
logger.error(
|
||||
"[PDF] not generated job_id=%s code=%s engine=%s after %.2fs: %s",
|
||||
job_id,
|
||||
first.returncode,
|
||||
engine,
|
||||
t_first - t_start,
|
||||
message,
|
||||
)
|
||||
if raise_on_error:
|
||||
raise RuntimeError(message)
|
||||
return None
|
||||
except subprocess.TimeoutExpired:
|
||||
message = f"LaTeX compilation timed out for job_id={job_id}"
|
||||
if log_errors:
|
||||
logger.error(message)
|
||||
if raise_on_error:
|
||||
raise
|
||||
return None
|
||||
except Exception as exc:
|
||||
if log_errors:
|
||||
logger.error("LaTeX compilation failed: %s", exc)
|
||||
if raise_on_error:
|
||||
raise
|
||||
return None
|
||||
|
||||
|
||||
__all__ = ["render_pdf_to_images", "compile_latex_to_pdf"]
|
||||
@@ -0,0 +1,134 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from typing import Dict, List, Optional, Any
|
||||
|
||||
|
||||
# Shared rules
|
||||
ALLOWED_LATEX_PACKAGES = [
|
||||
"courier",
|
||||
"graphicx",
|
||||
"geometry",
|
||||
"helvet",
|
||||
"lmodern",
|
||||
"mathpazo",
|
||||
"xcolor",
|
||||
"tabularx",
|
||||
"paracol",
|
||||
"multicol",
|
||||
"longtable",
|
||||
"setspace",
|
||||
"enumitem",
|
||||
"titlesec",
|
||||
"array",
|
||||
"inputenc",
|
||||
"fontenc",
|
||||
"tikz",
|
||||
]
|
||||
|
||||
LATEX_RULES = [
|
||||
r"Output must start with \documentclass.",
|
||||
r"Output must contain exactly one \begin{document} and one \end{document}.",
|
||||
r"Do not output anything before \documentclass.",
|
||||
r"Do not output anything after \end{document}.",
|
||||
"Do not include commentary, apologies, or markdown fences.",
|
||||
"If uncertain, prefer simpler LaTeX over complex packages.",
|
||||
"Only use LaTeX packages from the allowlist in this prompt.",
|
||||
"Do not use fontspec or custom font commands.",
|
||||
]
|
||||
|
||||
|
||||
def latex_system_prompt(style_profile: Dict[str, Any], document_type: str, template_hint: Optional[str]) -> str:
|
||||
safe_style = {
|
||||
"font_preference": style_profile.get("font_preference", "default"),
|
||||
"tone": style_profile.get("tone", "professional"),
|
||||
"color_accent": style_profile.get("color_accent", "blue"),
|
||||
"layout_preference": style_profile.get("layout_preference", "clean"),
|
||||
}
|
||||
return (
|
||||
"You are a LaTeX document generator for PDFs.\n"
|
||||
f"User Style Profile (trimmed): {json.dumps(safe_style, sort_keys=True)}\n"
|
||||
f"Document Type: {document_type}\n"
|
||||
f"Template Hint present: {'yes' if template_hint else 'no'}\n"
|
||||
"Rules:\n"
|
||||
"1) Output ONLY valid LaTeX code.\n"
|
||||
f"- " + "\n- ".join(LATEX_RULES) + "\n"
|
||||
"2) Use only the allowlisted packages:\n"
|
||||
f"- " + "\n- ".join(ALLOWED_LATEX_PACKAGES) + "\n"
|
||||
f"3) Respect preferred font ({safe_style['font_preference']}), tone ({safe_style['tone']}), and color accent ({safe_style['color_accent']}). Use the accent token name 'accent' (e.g., \\color{{accent}} or \\textcolor{{accent}}{{...}}).\n"
|
||||
"4) If a template hint is provided, stay close to its layout and styling.\n"
|
||||
"5) Do NOT add placeholder images or black boxes; omit images entirely unless an explicit path or real image content is provided. Do not use \\rule, tikz, or colored rectangles as image stand-ins.\n"
|
||||
"6) Return a full compilable document."
|
||||
)
|
||||
|
||||
|
||||
def latex_context_messages(
|
||||
template_hint: Optional[str],
|
||||
current_latex: Optional[str],
|
||||
structured_brief: Optional[str],
|
||||
) -> List[Dict[str, str]]:
|
||||
messages: List[Dict[str, str]] = []
|
||||
if template_hint:
|
||||
messages.append(
|
||||
{
|
||||
"role": "user",
|
||||
"content": f"REFERENCE TEMPLATE (keep style/layout, do not copy data):\n---\n{template_hint[:2000]}\n---",
|
||||
}
|
||||
)
|
||||
if current_latex:
|
||||
messages.append(
|
||||
{
|
||||
"role": "user",
|
||||
"content": f"CURRENT LATEX DRAFT (keep structure, apply edits):\n---\n{current_latex[:2000]}\n---",
|
||||
}
|
||||
)
|
||||
if structured_brief:
|
||||
messages.append(
|
||||
{
|
||||
"role": "user",
|
||||
"content": (
|
||||
"Structured details gathered from the user (authoritative; do not invent beyond this):\n"
|
||||
f"---\n{structured_brief}\n---"
|
||||
),
|
||||
}
|
||||
)
|
||||
return messages
|
||||
|
||||
|
||||
def pdf_qa_system_prompt() -> str:
|
||||
return (
|
||||
"You are a helpful assistant. Read the provided PDF text and answer the user's question.\n"
|
||||
"Respond with:\n"
|
||||
"Answer: 2–4 sentences summarizing the answer from the text.\n"
|
||||
"Evidence: 1–3 short quotes/snippets from the provided text (must be exact substrings).\n"
|
||||
"If the answer is not in the text, say: 'Not found in the provided text.' and give a best-effort summary."
|
||||
)
|
||||
|
||||
|
||||
def brief_missing_info_system_prompt(doc_type: str) -> str:
|
||||
return (
|
||||
f"You are a brief-gathering assistant for generating a {doc_type}.\n"
|
||||
"Be conversational and concise. Ask at most 3 short questions; no multi-part questions.\n"
|
||||
"If the user hasn't given much, invite them to paste prior material or dump everything they remember.\n"
|
||||
"Do not invent data; only ask."
|
||||
)
|
||||
|
||||
|
||||
def vision_layout_system_prompt() -> str:
|
||||
return (
|
||||
"You are a LaTeX layout extractor. Given page images of a PDF, return a LaTeX skeleton matching the layout and styling while blanking user content.\n"
|
||||
"Do NOT copy any readable text from images. Replace all text with placeholders like TITLE HERE, LOREM, XXXX.\n"
|
||||
"Infer margins, columns, header/footer, tables. Use \\rule{width}{height} placeholders sized to match blocks.\n"
|
||||
"Use common packages (geometry, xcolor, tabularx, multicol, paracol, tikz). Replace text with placeholders and output a full compilable document.\n"
|
||||
"Output ONLY LaTeX."
|
||||
)
|
||||
|
||||
|
||||
__all__ = [
|
||||
"latex_system_prompt",
|
||||
"latex_context_messages",
|
||||
"pdf_qa_system_prompt",
|
||||
"brief_missing_info_system_prompt",
|
||||
"vision_layout_system_prompt",
|
||||
"LATEX_RULES",
|
||||
]
|
||||
@@ -0,0 +1,6 @@
|
||||
Flask==3.0.0
|
||||
Flask-CORS==4.0.0
|
||||
# Use modern OpenAI SDK (v1 interface)
|
||||
openai>=1.12.0
|
||||
langchain-core==1.2.5
|
||||
langchain-openai==1.1.6
|
||||
@@ -0,0 +1,18 @@
|
||||
from prompts import (
|
||||
pdf_qa_system_prompt,
|
||||
vision_layout_system_prompt,
|
||||
latex_system_prompt,
|
||||
)
|
||||
|
||||
|
||||
def main():
|
||||
assert pdf_qa_system_prompt(), "pdf_qa_system_prompt is empty"
|
||||
assert "Do NOT copy" in vision_layout_system_prompt(), "vision prompt missing no-copy rule"
|
||||
latex_prompt = latex_system_prompt({}, "document", None)
|
||||
assert "\\end{document}" in latex_prompt, "latex prompt missing end document mention"
|
||||
print("prompts OK")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
@@ -0,0 +1,262 @@
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
ROOT_DIR = Path(__file__).resolve().parents[2]
|
||||
TEMPLATE_ROOT = ROOT_DIR / "backend" / "templates"
|
||||
FRONTEND_PUBLIC = ROOT_DIR / "frontend" / "public" / "templates"
|
||||
FRONTEND_CATALOG = ROOT_DIR / "frontend" / "src" / "templateCatalog.ts"
|
||||
TIMEOUT_SEC = 60
|
||||
|
||||
LOREM_SENTENCE = (
|
||||
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt "
|
||||
"ut labore et dolore magna aliqua."
|
||||
)
|
||||
LOREM_PARAGRAPH = (
|
||||
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt "
|
||||
"ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco "
|
||||
"laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in "
|
||||
"voluptate velit esse cillum dolore eu fugiat nulla pariatur."
|
||||
)
|
||||
LOREM_SHORT = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
|
||||
|
||||
PLACEHOLDER_REPLACEMENTS = {
|
||||
"TITLE": "Sample Title",
|
||||
"SUBTITLE": "Sample Subtitle",
|
||||
"AUTHOR": "Sample Author",
|
||||
"AUTHOR_LIST": "Sample Author One, Sample Author Two",
|
||||
"AFFILIATIONS": "Sample Organization",
|
||||
"ABSTRACT": LOREM_PARAGRAPH,
|
||||
"KEYWORDS": "keyword1, keyword2, keyword3, keyword4",
|
||||
"INTRODUCTION": LOREM_PARAGRAPH,
|
||||
"RELATED_WORK": LOREM_PARAGRAPH,
|
||||
"METHODOLOGY": LOREM_PARAGRAPH,
|
||||
"RESULTS": LOREM_PARAGRAPH,
|
||||
"DISCUSSION": LOREM_PARAGRAPH,
|
||||
"CONCLUSION": LOREM_SHORT,
|
||||
"REFERENCES": "Doe, J. (2024). Example Reference. Journal of Examples.",
|
||||
"MAIN_TEXT": f"{LOREM_PARAGRAPH} {LOREM_PARAGRAPH}",
|
||||
"FIGURES_TABLES": "Figure 1: Example chart. Table 1: Summary of results.",
|
||||
"REPORT_TITLE": "Business Report",
|
||||
"DATE": "2025-01-01",
|
||||
"EXEC_SUMMARY": LOREM_PARAGRAPH,
|
||||
"BACKGROUND": LOREM_PARAGRAPH,
|
||||
"FINDINGS": f"{LOREM_SENTENCE} {LOREM_SENTENCE}",
|
||||
"RECOMMENDATIONS": "Recommendation 1: Improve efficiency. Recommendation 2: Reduce costs.",
|
||||
"APPENDIX": LOREM_SHORT,
|
||||
"NEWSLETTER_TITLE": "Monthly Newsletter",
|
||||
"TOP_STORY": LOREM_PARAGRAPH,
|
||||
"UPDATES": f"{LOREM_SENTENCE} {LOREM_SENTENCE}",
|
||||
"SPOTLIGHT": LOREM_PARAGRAPH,
|
||||
"FOOTER": "Contact: info@example.com | 123 Main Street",
|
||||
"RECIPE_TITLE": "Sample Recipe",
|
||||
"SERVINGS": "Serves 4",
|
||||
"TIME": "30 minutes",
|
||||
"INGREDIENTS": r"\begin{itemize}\item Ingredient A\item Ingredient B\item Ingredient C\end{itemize}",
|
||||
"INSTRUCTIONS": LOREM_PARAGRAPH,
|
||||
"NOTES": "Notes and tips: adjust seasoning to taste.",
|
||||
"BUSINESS_NAME": "Your Company",
|
||||
"BUSINESS_ADDRESS": "123 Main Street, Springfield",
|
||||
"BUSINESS_CONTACT": "email@example.com | (555) 555-5555",
|
||||
"INVOICE_NUMBER": "INV-001",
|
||||
"ISSUE_DATE": "2025-01-01",
|
||||
"DUE_DATE": "2025-01-15",
|
||||
"CLIENT_NAME": "Client Name",
|
||||
"CLIENT_ADDRESS": "456 Client Ave, Metropolis",
|
||||
"CLIENT_CONTACT": "client@example.com",
|
||||
"LINE_ITEMS": r"Design Services & 8 & 120 & 960 \\ Consulting & 4 & 150 & 600 \\",
|
||||
"SUBTOTAL": "1560",
|
||||
"TAXES": "124.80",
|
||||
"TOTAL": "1684.80",
|
||||
"PAYMENT_TERMS": "Net 15",
|
||||
"PAYMENT_METHODS": "Bank transfer, credit card",
|
||||
"STUDENT_NAME": "Student Name",
|
||||
"COURSE_NAME": "Course Name",
|
||||
"INSTRUCTOR_NAME": "Instructor Name",
|
||||
"ASSIGNMENT_TITLE": "Assignment Title",
|
||||
"PROMPT": LOREM_SHORT,
|
||||
"RESPONSE": LOREM_PARAGRAPH,
|
||||
"CHAPTER_ONE_TITLE": "Chapter One",
|
||||
"CHAPTER_ONE": LOREM_PARAGRAPH,
|
||||
"CHAPTER_TWO_TITLE": "Chapter Two",
|
||||
"CHAPTER_TWO": LOREM_PARAGRAPH,
|
||||
"PREFACE": LOREM_SHORT,
|
||||
"PUBLISHER": "Publisher",
|
||||
"NAME": "Name",
|
||||
"EMAIL": "email@example.com",
|
||||
"PHONE": "(555) 555-5555",
|
||||
"LOCATION": "City, Country",
|
||||
"SUMMARY": LOREM_SENTENCE,
|
||||
"EXPERIENCE": f"{LOREM_SENTENCE} {LOREM_SENTENCE}",
|
||||
"EDUCATION": "University Name, B.S. in Example Studies",
|
||||
"SKILLS": "Skills: Analysis, Design, Communication",
|
||||
"PROJECTS": LOREM_SHORT,
|
||||
"SUBJECT": "Subject",
|
||||
"BODY": LOREM_PARAGRAPH,
|
||||
"RECIPIENT_NAME": "Recipient Name",
|
||||
"RECIPIENT_TITLE": "Recipient Title",
|
||||
"RECIPIENT_COMPANY": "Recipient Company",
|
||||
"RECIPIENT_ADDRESS": "Recipient Address",
|
||||
"SENDER_NAME": "Sender Name",
|
||||
"SENDER_ADDRESS": "Sender Address",
|
||||
"SENDER_EMAIL": "sender@example.com",
|
||||
"MONTH_YEAR": "January 2025",
|
||||
"THEME": "Theme",
|
||||
"WEEK_ROWS": "1 & 2 & 3 & 4 & 5 & 6 & 7 \\\\\\\\ \\\\hline",
|
||||
"HEADLINE": "Headline",
|
||||
"SUBTEXT": "Supporting message with a clear benefit.",
|
||||
"CALL_TO_ACTION": "Call to action",
|
||||
"CONTACT": "contact@example.com",
|
||||
"EXPERIMENT_TITLE": "Experiment Title",
|
||||
"OBJECTIVE": LOREM_SHORT,
|
||||
"MATERIALS": "Materials list goes here.",
|
||||
"PROCEDURE": LOREM_PARAGRAPH,
|
||||
"OBSERVATIONS": LOREM_SHORT,
|
||||
"INSTITUTION": "Institution",
|
||||
"PRESENTER": "Presenter",
|
||||
"AGENDA": "Agenda goes here.",
|
||||
"KEY_POINTS": "Key points go here.",
|
||||
"DATA_VISUALS": "Data visuals go here.",
|
||||
"SUBTITLE": "Subtitle",
|
||||
"ORGANIZATION": "Organization",
|
||||
}
|
||||
|
||||
|
||||
def render_template_latex(raw_latex: str) -> str:
|
||||
def replace(match: re.Match[str]) -> str:
|
||||
key = match.group(1).strip()
|
||||
return PLACEHOLDER_REPLACEMENTS.get(key, key.replace("_", " ").title())
|
||||
|
||||
return re.sub(r"<<([A-Z0-9_]+)>>", replace, raw_latex)
|
||||
|
||||
|
||||
def find_converter() -> str | None:
|
||||
if shutil.which("pdftoppm"):
|
||||
return "pdftoppm"
|
||||
if shutil.which("magick"):
|
||||
return "magick"
|
||||
if shutil.which("convert"):
|
||||
return "convert"
|
||||
return None
|
||||
|
||||
|
||||
def pdf_to_jpg(pdf_path: Path, jpg_path: Path, converter: str) -> None:
|
||||
if converter == "pdftoppm":
|
||||
subprocess.run(
|
||||
["pdftoppm", "-jpeg", "-f", "1", "-singlefile", str(pdf_path), str(jpg_path.with_suffix(''))],
|
||||
check=True,
|
||||
stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL,
|
||||
timeout=TIMEOUT_SEC,
|
||||
)
|
||||
return
|
||||
if converter == "magick":
|
||||
subprocess.run(
|
||||
["magick", "convert", "-density", "150", str(pdf_path), "-quality", "90", str(jpg_path)],
|
||||
check=True,
|
||||
stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL,
|
||||
timeout=TIMEOUT_SEC,
|
||||
)
|
||||
return
|
||||
subprocess.run(
|
||||
["convert", "-density", "150", str(pdf_path), "-quality", "90", str(jpg_path)],
|
||||
check=True,
|
||||
stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL,
|
||||
timeout=TIMEOUT_SEC,
|
||||
)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
if not shutil.which("pdflatex"):
|
||||
raise SystemExit("pdflatex not found. Install TeX Live or MikTeX to generate thumbnails.")
|
||||
|
||||
converter = find_converter()
|
||||
if not converter:
|
||||
raise SystemExit("No PDF-to-image converter found. Install poppler-utils or ImageMagick.")
|
||||
|
||||
tex_files = list(TEMPLATE_ROOT.rglob("*.tex"))
|
||||
if not tex_files:
|
||||
raise SystemExit(f"No templates found in {TEMPLATE_ROOT}")
|
||||
|
||||
FRONTEND_PUBLIC.mkdir(parents=True, exist_ok=True)
|
||||
catalog: dict[str, list[str]] = {}
|
||||
|
||||
failures: list[str] = []
|
||||
for tex_file in tex_files:
|
||||
doc_type = tex_file.parent.name
|
||||
template_id = tex_file.stem
|
||||
target_dir = FRONTEND_PUBLIC / doc_type
|
||||
target_dir.mkdir(parents=True, exist_ok=True)
|
||||
target_jpg = target_dir / f"{template_id}.jpg"
|
||||
|
||||
catalog.setdefault(doc_type, [])
|
||||
if template_id not in catalog[doc_type]:
|
||||
catalog[doc_type].append(template_id)
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
tmpdir_path = Path(tmpdir)
|
||||
rendered = render_template_latex(tex_file.read_text(encoding="ascii"))
|
||||
tmp_tex = tmpdir_path / "template.tex"
|
||||
tmp_tex.write_text(rendered, encoding="ascii")
|
||||
|
||||
try:
|
||||
subprocess.run(
|
||||
["pdflatex", "-interaction=nonstopmode", "-halt-on-error", tmp_tex.name],
|
||||
check=True,
|
||||
cwd=tmpdir_path,
|
||||
stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL,
|
||||
timeout=TIMEOUT_SEC,
|
||||
)
|
||||
except (subprocess.CalledProcessError, subprocess.TimeoutExpired) as exc:
|
||||
failures.append(f"{tex_file}: pdflatex failed ({exc})")
|
||||
continue
|
||||
|
||||
pdf_path = tmpdir_path / "template.pdf"
|
||||
if not pdf_path.exists():
|
||||
failures.append(f"{tex_file}: PDF not generated")
|
||||
continue
|
||||
|
||||
try:
|
||||
pdf_to_jpg(pdf_path, target_jpg, converter)
|
||||
print(f"Wrote {target_jpg}")
|
||||
except (subprocess.CalledProcessError, subprocess.TimeoutExpired) as exc:
|
||||
failures.append(f"{tex_file}: image conversion failed ({exc})")
|
||||
continue
|
||||
|
||||
if failures:
|
||||
print("\nFailures:")
|
||||
for failure in failures:
|
||||
print(f"- {failure}")
|
||||
raise SystemExit("Template thumbnail generation completed with errors.")
|
||||
|
||||
entries = []
|
||||
for doc_type in sorted(catalog.keys()):
|
||||
templates = sorted(catalog[doc_type])
|
||||
if "default" in templates:
|
||||
templates = ["default"] + [t for t in templates if t != "default"]
|
||||
entries.append(
|
||||
f" {{ docType: '{doc_type}', templateCount: {len(templates)}, templates: {templates} }}"
|
||||
)
|
||||
|
||||
FRONTEND_CATALOG.write_text(
|
||||
"// Auto-generated by generate_template_thumbnails.py\n"
|
||||
"export type TemplateCatalogEntry = {\n"
|
||||
" docType: string\n"
|
||||
" templateCount: number\n"
|
||||
" templates: string[]\n"
|
||||
"}\n\n"
|
||||
"export const templateCatalog: TemplateCatalogEntry[] = [\n"
|
||||
+ ",\n".join(entries)
|
||||
+ "\n]\n",
|
||||
encoding="ascii",
|
||||
)
|
||||
print(f"Wrote catalog {FRONTEND_CATALOG}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,86 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
from typing import Any, Dict, List
|
||||
|
||||
from config import STYLE_DB_PATH, TEMPLATE_DB_PATH, VERSIONS_DB_PATH
|
||||
from latex_utils import clean_generated_latex, extract_layout_hint
|
||||
|
||||
|
||||
def _read_json(path: str) -> Dict[str, Any]:
|
||||
if not os.path.exists(path):
|
||||
return {}
|
||||
try:
|
||||
with open(path, "r", encoding="utf-8") as handle:
|
||||
return json.load(handle)
|
||||
except Exception:
|
||||
return {}
|
||||
|
||||
|
||||
def _write_json(path: str, data: Dict[str, Any]) -> None:
|
||||
with open(path, "w", encoding="utf-8") as handle:
|
||||
json.dump(data, handle, indent=2)
|
||||
|
||||
|
||||
def load_user_style(user_id: str) -> Dict[str, Any]:
|
||||
"""Load (or bootstrap) an individual user's preferred style."""
|
||||
data = _read_json(STYLE_DB_PATH)
|
||||
return data.get(
|
||||
user_id,
|
||||
{
|
||||
"layout_preference": "clean",
|
||||
"font_preference": "helvet",
|
||||
"tone": "professional",
|
||||
"color_accent": "blue",
|
||||
"last_doc_type": None,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def save_user_style(user_id: str, style_data: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""Persist style preferences for a user."""
|
||||
all_data = _read_json(STYLE_DB_PATH)
|
||||
current = all_data.get(user_id, load_user_style(user_id))
|
||||
current.update(style_data)
|
||||
all_data[user_id] = current
|
||||
_write_json(STYLE_DB_PATH, all_data)
|
||||
return current
|
||||
|
||||
|
||||
def load_user_templates(user_id: str) -> Dict[str, str]:
|
||||
templates = _read_json(TEMPLATE_DB_PATH)
|
||||
return templates.get(user_id, {})
|
||||
|
||||
|
||||
def save_user_template(user_id: str, doc_type: str, latex_code: str) -> None:
|
||||
"""Persist sanitized layout hints per doc type."""
|
||||
templates = _read_json(TEMPLATE_DB_PATH)
|
||||
user_templates = templates.get(user_id, {})
|
||||
sanitized = clean_generated_latex(latex_code)
|
||||
user_templates[doc_type] = extract_layout_hint(sanitized)
|
||||
templates[user_id] = user_templates
|
||||
_write_json(TEMPLATE_DB_PATH, templates)
|
||||
|
||||
|
||||
def load_versions(user_id: str) -> List[Dict[str, Any]]:
|
||||
data = _read_json(VERSIONS_DB_PATH)
|
||||
return data.get(user_id, [])
|
||||
|
||||
|
||||
def save_version(user_id: str, entry: Dict[str, Any]) -> None:
|
||||
data = _read_json(VERSIONS_DB_PATH)
|
||||
versions = data.get(user_id, [])
|
||||
versions.insert(0, entry)
|
||||
data[user_id] = versions[:20]
|
||||
_write_json(VERSIONS_DB_PATH, data)
|
||||
|
||||
|
||||
__all__ = [
|
||||
"load_user_style",
|
||||
"save_user_style",
|
||||
"load_user_templates",
|
||||
"save_user_template",
|
||||
"load_versions",
|
||||
"save_version",
|
||||
]
|
||||
@@ -0,0 +1,33 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Dict
|
||||
|
||||
from storage import load_user_style, save_user_style
|
||||
|
||||
|
||||
def update_style_profile_from_prompt(user_id: str, prompt: str) -> Dict[str, Any]:
|
||||
"""Simple heuristics to remember color/font/tone preferences from prompt text."""
|
||||
style = load_user_style(user_id)
|
||||
lower = (prompt or "").lower()
|
||||
|
||||
if "modern" in lower or "minimal" in lower:
|
||||
style["layout_preference"] = "modern"
|
||||
style["tone"] = "minimalist"
|
||||
if "classic" in lower or "formal" in lower:
|
||||
style["layout_preference"] = "classic"
|
||||
style["tone"] = "formal"
|
||||
if "serif" in lower:
|
||||
style["font_preference"] = "serif"
|
||||
if "sans" in lower:
|
||||
style["font_preference"] = "helvet"
|
||||
if "blue" in lower:
|
||||
style["color_accent"] = "blue"
|
||||
if "red" in lower:
|
||||
style["color_accent"] = "red"
|
||||
if "green" in lower:
|
||||
style["color_accent"] = "green"
|
||||
|
||||
return save_user_style(user_id, style)
|
||||
|
||||
|
||||
__all__ = ["update_style_profile_from_prompt"]
|
||||
@@ -0,0 +1,52 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, List, Optional
|
||||
import time
|
||||
|
||||
from config import CLIENT_MODE, SMART_MODEL, get_chat_model, logger
|
||||
from langchain_utils import to_lc_messages
|
||||
from prompts import vision_layout_system_prompt
|
||||
|
||||
|
||||
def vision_layout_from_images(image_urls: List[str], doc_type: str) -> Optional[str]:
|
||||
"""Call the multimodal model to recover a LaTeX skeleton from page images."""
|
||||
if CLIENT_MODE != "langchain" or not image_urls:
|
||||
return None
|
||||
|
||||
system_prompt = vision_layout_system_prompt()
|
||||
|
||||
user_content: List[Any] = [
|
||||
{"type": "text", "text": f"Extract layout for document type: {doc_type}. Return LaTeX skeleton only."}
|
||||
]
|
||||
for url in image_urls:
|
||||
user_content.append({"type": "image_url", "image_url": {"url": url}})
|
||||
|
||||
messages = [
|
||||
{"role": "system", "content": system_prompt},
|
||||
{"role": "user", "content": user_content},
|
||||
]
|
||||
|
||||
try:
|
||||
logger.info("[IMPORT] vision call pages=%s doc_type=%s", len(image_urls), doc_type)
|
||||
llm = get_chat_model(SMART_MODEL, max_tokens=2800)
|
||||
if not llm:
|
||||
return None
|
||||
start = time.perf_counter()
|
||||
response = llm.invoke(to_lc_messages(messages))
|
||||
elapsed = time.perf_counter() - start
|
||||
content = response.content or ""
|
||||
usage = getattr(response, "usage_metadata", None)
|
||||
logger.info(
|
||||
"[IMPORT] vision model=%s elapsed=%.2fs chars=%s usage=%s",
|
||||
SMART_MODEL,
|
||||
elapsed,
|
||||
len(str(content)),
|
||||
usage,
|
||||
)
|
||||
return response.content
|
||||
except Exception as exc:
|
||||
logger.error("[IMPORT] vision generation failed: %s", exc)
|
||||
return None
|
||||
|
||||
|
||||
__all__ = ["vision_layout_from_images"]
|
||||
@@ -0,0 +1,36 @@
|
||||
module.exports = {
|
||||
root: true,
|
||||
env: {
|
||||
browser: true,
|
||||
es2021: true,
|
||||
},
|
||||
extends: [
|
||||
'eslint:recommended',
|
||||
'plugin:react/recommended',
|
||||
'plugin:react-hooks/recommended',
|
||||
'plugin:@typescript-eslint/recommended',
|
||||
'prettier',
|
||||
],
|
||||
parser: '@typescript-eslint/parser',
|
||||
parserOptions: {
|
||||
ecmaVersion: 'latest',
|
||||
sourceType: 'module',
|
||||
ecmaFeatures: {
|
||||
jsx: true,
|
||||
},
|
||||
jsxPragma: null,
|
||||
},
|
||||
plugins: ['react', 'react-hooks', '@typescript-eslint'],
|
||||
settings: {
|
||||
react: {
|
||||
version: 'detect',
|
||||
runtime: 'automatic',
|
||||
},
|
||||
},
|
||||
ignorePatterns: ['dist', 'node_modules'],
|
||||
rules: {
|
||||
'react/prop-types': 'off',
|
||||
'react/react-in-jsx-scope': 'off',
|
||||
'@typescript-eslint/no-explicit-any': 'off',
|
||||
},
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<link rel="icon" type="image/x-icon" href="/favicon.ico" />
|
||||
<title>Stirling - Intelligent Document 1.0</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="root"></div>
|
||||
<script type="module" src="/src/main.tsx"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,34 @@
|
||||
{
|
||||
"name": "latex-pdf-generator-frontend",
|
||||
"version": "1.0.0",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "vite",
|
||||
"build": "vite build",
|
||||
"preview": "vite preview",
|
||||
"lint": "eslint \"src/**/*.{ts,tsx}\"",
|
||||
"typecheck": "tsc --noEmit"
|
||||
},
|
||||
"dependencies": {
|
||||
"@xenova/transformers": "^2.17.0",
|
||||
"pdfjs-dist": "^4.10.38",
|
||||
"pdf-lib": "^1.17.1",
|
||||
"react": "^18.2.0",
|
||||
"react-dom": "^18.2.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/react": "^18.2.43",
|
||||
"@types/react-dom": "^18.2.17",
|
||||
"@typescript-eslint/eslint-plugin": "^6.14.0",
|
||||
"@typescript-eslint/parser": "^6.14.0",
|
||||
"@vitejs/plugin-react": "^4.2.1",
|
||||
"autoprefixer": "^10.4.16",
|
||||
"eslint": "^8.56.0",
|
||||
"eslint-config-prettier": "^9.0.0",
|
||||
"eslint-plugin-react": "^7.34.1",
|
||||
"eslint-plugin-react-hooks": "^4.6.0",
|
||||
"postcss": "^8.4.32",
|
||||
"tailwindcss": "^3.4.0",
|
||||
"vite": "^5.0.8"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
export default {
|
||||
plugins: {
|
||||
tailwindcss: {},
|
||||
autoprefixer: {},
|
||||
},
|
||||
}
|
||||
|
After Width: | Height: | Size: 15 KiB |
|
After Width: | Height: | Size: 223 KiB |
|
After Width: | Height: | Size: 147 KiB |
|
After Width: | Height: | Size: 87 KiB |
|
After Width: | Height: | Size: 41 KiB |
|
After Width: | Height: | Size: 12 KiB |
|
After Width: | Height: | Size: 142 KiB |
|
After Width: | Height: | Size: 49 KiB |
|
After Width: | Height: | Size: 123 KiB |
|
After Width: | Height: | Size: 76 KiB |
|
After Width: | Height: | Size: 105 KiB |
|
After Width: | Height: | Size: 111 KiB |
|
After Width: | Height: | Size: 78 KiB |
|
After Width: | Height: | Size: 88 KiB |
|
After Width: | Height: | Size: 98 KiB |
|
After Width: | Height: | Size: 169 KiB |
|
After Width: | Height: | Size: 57 KiB |
|
After Width: | Height: | Size: 136 KiB |
|
After Width: | Height: | Size: 62 KiB |
|
After Width: | Height: | Size: 86 KiB |
|
After Width: | Height: | Size: 45 KiB |
|
After Width: | Height: | Size: 42 KiB |
|
After Width: | Height: | Size: 43 KiB |
@@ -0,0 +1,134 @@
|
||||
import { LandingView } from './components/landing/LandingView'
|
||||
import { ImportLayoutModal } from './components/modals/ImportLayoutModal'
|
||||
import { WorkspaceView } from './components/workspace/WorkspaceView'
|
||||
import { useDocumentWorkflow } from './hooks/useDocumentWorkflow'
|
||||
import { useSpeechCapture } from './hooks/useSpeechCapture'
|
||||
|
||||
function App() {
|
||||
const workflow = useDocumentWorkflow()
|
||||
const speech = useSpeechCapture({ appendPrompt: workflow.appendPrompt })
|
||||
|
||||
const importModal = (
|
||||
<ImportLayoutModal
|
||||
isOpen={workflow.showImportModal}
|
||||
docType={workflow.importDocType}
|
||||
onDocTypeChange={workflow.setImportDocType}
|
||||
onClose={() => workflow.setShowImportModal(false)}
|
||||
onFileSelected={workflow.handleImportTemplate}
|
||||
isImporting={workflow.isImporting}
|
||||
status={workflow.importStatus}
|
||||
/>
|
||||
)
|
||||
|
||||
if (workflow.view === 'landing') {
|
||||
return (
|
||||
<>
|
||||
<LandingView
|
||||
prompt={workflow.prompt}
|
||||
onPromptChange={workflow.setPrompt}
|
||||
onSubmit={workflow.handleInitialSubmit}
|
||||
onKeyDown={workflow.handleKeyDown}
|
||||
uploadedPdfFile={workflow.uploadedPdfFile}
|
||||
onFileSelect={workflow.setUploadedPdfFile}
|
||||
isImporting={workflow.isImporting}
|
||||
onToggleRecording={speech.toggleRecording}
|
||||
onCancelRecording={speech.cancelRecording}
|
||||
onAcceptRecording={speech.acceptRecording}
|
||||
isRecording={speech.isRecording}
|
||||
whisperStatus={speech.whisperStatus}
|
||||
waveformHistory={speech.waveformHistory}
|
||||
docTypes={workflow.docTypes}
|
||||
templateCounts={workflow.templateCounts}
|
||||
templateCatalog={workflow.templateCatalog}
|
||||
selectedDocType={workflow.selectedDocType}
|
||||
selectedTemplateId={workflow.selectedTemplateId}
|
||||
templatesForSelected={workflow.templatesForSelected}
|
||||
isTemplateLoading={workflow.isTemplateLoading}
|
||||
isTemplatePanelOpen={workflow.isTemplatePanelOpen}
|
||||
onToggleTemplatePanel={() =>
|
||||
workflow.setIsTemplatePanelOpen(!workflow.isTemplatePanelOpen)
|
||||
}
|
||||
onSelectTemplate={workflow.applyTemplateSelection}
|
||||
templateThumbnailUrl={workflow.templateThumbnailUrl}
|
||||
formatDocLabel={(value: string) =>
|
||||
value
|
||||
.split('_')
|
||||
.map((part) => part.charAt(0).toUpperCase() + part.slice(1))
|
||||
.join(' ')
|
||||
}
|
||||
/>
|
||||
{importModal}
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<WorkspaceView
|
||||
isGenerating={workflow.isGenerating}
|
||||
isLivePreviewing={workflow.isLivePreviewing}
|
||||
isStageLoading={workflow.isStageLoading}
|
||||
prompt={workflow.prompt}
|
||||
onPromptChange={workflow.setPrompt}
|
||||
onChatSubmit={workflow.handleChatSubmit}
|
||||
onKeyDown={workflow.handleKeyDown}
|
||||
currentDoc={workflow.currentDoc}
|
||||
onBack={() => workflow.setView('landing')}
|
||||
stage={workflow.stage}
|
||||
outlineRows={workflow.outlineRows}
|
||||
outlineSections={workflow.outlineSections}
|
||||
excludedFields={workflow.excludedFields}
|
||||
outlineConstraints={workflow.outlineConstraints}
|
||||
draftRows={workflow.draftRows}
|
||||
setOutlineRows={workflow.setOutlineRows}
|
||||
setOutlineSections={workflow.setOutlineSections}
|
||||
setExcludedFields={workflow.setExcludedFields}
|
||||
setOutlineConstraints={workflow.setOutlineConstraints}
|
||||
setDraftRows={workflow.setDraftRows}
|
||||
docTypes={workflow.docTypes}
|
||||
templateCounts={workflow.templateCounts}
|
||||
selectedDocType={workflow.selectedDocType}
|
||||
selectedTemplateId={workflow.selectedTemplateId}
|
||||
templatesForSelected={workflow.templatesForSelected}
|
||||
isTemplateLoading={workflow.isTemplateLoading}
|
||||
onSelectTemplate={workflow.applyTemplateSelection}
|
||||
templateThumbnailUrl={workflow.templateThumbnailUrl}
|
||||
approveOutline={workflow.approveOutline}
|
||||
onAiOutline={() => {
|
||||
workflow.fillFieldsFromAI()
|
||||
}}
|
||||
approveDraft={workflow.approveDraft}
|
||||
saveAndReview={workflow.saveAndReview}
|
||||
styleDraft={workflow.styleDraft}
|
||||
setStyleDraft={workflow.setStyleDraft}
|
||||
applyStyleAndRegenerate={workflow.applyStyleAndRegenerate}
|
||||
onAddPromptInfo={workflow.addPromptForFields}
|
||||
onStageSelect={(nextStage) => {
|
||||
if (workflow.isGenerating || workflow.isStageLoading) return
|
||||
if (nextStage === 'text' && workflow.stage === 'outline') {
|
||||
workflow.approveOutline()
|
||||
return
|
||||
}
|
||||
if (nextStage === 'styling' && workflow.stage === 'text') {
|
||||
workflow.approveDraft()
|
||||
return
|
||||
}
|
||||
if (nextStage === 'review' && workflow.stage === 'styling') {
|
||||
workflow.saveAndReview()
|
||||
return
|
||||
}
|
||||
workflow.setStage(nextStage)
|
||||
}}
|
||||
imagePlaceholdersCount={workflow.imagePlaceholdersCount}
|
||||
isAssetUploading={workflow.isAssetUploading}
|
||||
assetError={workflow.assetError}
|
||||
onAddPlaceholderImage={workflow.addImageToPlaceholders}
|
||||
onRemovePlaceholders={workflow.stripImagePlaceholders}
|
||||
onOpenImportTemplate={() => workflow.openImportTemplate(workflow.selectedDocType)}
|
||||
/>
|
||||
{importModal}
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
export default App
|
||||
@@ -0,0 +1,360 @@
|
||||
import { FormEvent, KeyboardEvent, useMemo, useState } from 'react'
|
||||
import { AudioWaveform } from '../ui/AudioWaveform'
|
||||
|
||||
interface LandingViewProps {
|
||||
prompt: string
|
||||
onPromptChange: (value: string) => void
|
||||
onSubmit: (event: FormEvent<HTMLFormElement>) => void
|
||||
onKeyDown: (event: KeyboardEvent<HTMLTextAreaElement>) => void
|
||||
uploadedPdfFile: File | null
|
||||
onFileSelect: (file: File | null) => void
|
||||
isImporting: boolean
|
||||
onToggleRecording: () => void
|
||||
onCancelRecording: () => void
|
||||
onAcceptRecording: () => void
|
||||
isRecording: boolean
|
||||
whisperStatus: string | null
|
||||
waveformHistory: number[][]
|
||||
docTypes: string[]
|
||||
templateCounts: Record<string, number>
|
||||
templateCatalog: { docType: string; templateCount: number; templates: string[] }[]
|
||||
selectedDocType: string
|
||||
selectedTemplateId: string
|
||||
templatesForSelected: string[]
|
||||
isTemplateLoading: boolean
|
||||
isTemplatePanelOpen: boolean
|
||||
onToggleTemplatePanel: () => void
|
||||
onSelectTemplate: (docType: string, templateId: string) => void
|
||||
templateThumbnailUrl: (docType: string, templateId: string) => string
|
||||
formatDocLabel: (value: string) => string
|
||||
}
|
||||
|
||||
export function LandingView({
|
||||
prompt,
|
||||
onPromptChange,
|
||||
onSubmit,
|
||||
onKeyDown,
|
||||
uploadedPdfFile,
|
||||
onFileSelect,
|
||||
isImporting,
|
||||
onToggleRecording,
|
||||
onCancelRecording,
|
||||
onAcceptRecording,
|
||||
isRecording,
|
||||
whisperStatus,
|
||||
waveformHistory,
|
||||
docTypes,
|
||||
templateCounts,
|
||||
templateCatalog,
|
||||
selectedDocType,
|
||||
selectedTemplateId,
|
||||
templatesForSelected,
|
||||
isTemplateLoading,
|
||||
isTemplatePanelOpen,
|
||||
onToggleTemplatePanel,
|
||||
onSelectTemplate,
|
||||
templateThumbnailUrl,
|
||||
formatDocLabel,
|
||||
}: LandingViewProps) {
|
||||
const [templateSearch, setTemplateSearch] = useState('')
|
||||
const [activeTemplateTab, setActiveTemplateTab] = useState<'popular' | 'legal' | 'financial' | 'academic' | 'marketing' | 'operations'>('popular')
|
||||
const [expandedDocType, setExpandedDocType] = useState<string | null>(null)
|
||||
const [hoveredTemplate, setHoveredTemplate] = useState<{ docType: string; templateId: string; x: number; y: number } | null>(null)
|
||||
|
||||
const popularDocTypes = new Set(['cvs_and_resumes', 'invoices', 'cover_letters', 'business_reports', 'presentations'])
|
||||
const legalDocTypes = new Set(['formal_letters', 'theses'])
|
||||
const financialDocTypes = new Set(['invoices', 'business_reports', 'calendars'])
|
||||
const academicDocTypes = new Set(['academic_articles', 'academic_journals', 'assignments', 'theses'])
|
||||
const marketingDocTypes = new Set(['newsletters', 'signs', 'presentations'])
|
||||
const operationsDocTypes = new Set(['laboratory_reports', 'laboratory_books', 'business_reports'])
|
||||
|
||||
const visibleDocTypes = useMemo(() => {
|
||||
const filtered = docTypes.filter((docType) =>
|
||||
formatDocLabel(docType).toLowerCase().includes(templateSearch.toLowerCase()),
|
||||
)
|
||||
if (activeTemplateTab === 'legal') {
|
||||
return filtered.filter((docType) => legalDocTypes.has(docType))
|
||||
}
|
||||
if (activeTemplateTab === 'financial') {
|
||||
return filtered.filter((docType) => financialDocTypes.has(docType))
|
||||
}
|
||||
if (activeTemplateTab === 'academic') {
|
||||
return filtered.filter((docType) => academicDocTypes.has(docType))
|
||||
}
|
||||
if (activeTemplateTab === 'marketing') {
|
||||
return filtered.filter((docType) => marketingDocTypes.has(docType))
|
||||
}
|
||||
if (activeTemplateTab === 'operations') {
|
||||
return filtered.filter((docType) => operationsDocTypes.has(docType))
|
||||
}
|
||||
return filtered.filter((docType) => popularDocTypes.has(docType))
|
||||
}, [
|
||||
academicDocTypes,
|
||||
activeTemplateTab,
|
||||
docTypes,
|
||||
financialDocTypes,
|
||||
formatDocLabel,
|
||||
legalDocTypes,
|
||||
marketingDocTypes,
|
||||
operationsDocTypes,
|
||||
popularDocTypes,
|
||||
templateSearch,
|
||||
])
|
||||
|
||||
const docTypeIcon = (docType: string) => {
|
||||
switch (docType) {
|
||||
case 'cvs_and_resumes':
|
||||
return '📄'
|
||||
case 'invoices':
|
||||
return '🧾'
|
||||
case 'cover_letters':
|
||||
return '✉️'
|
||||
case 'business_reports':
|
||||
return '📊'
|
||||
case 'formal_letters':
|
||||
return '📝'
|
||||
case 'theses':
|
||||
return '🎓'
|
||||
case 'presentations':
|
||||
return '🖥️'
|
||||
case 'recipes':
|
||||
return '🍲'
|
||||
default:
|
||||
return '📁'
|
||||
}
|
||||
}
|
||||
return (
|
||||
<div className="min-h-screen bg-gradient-to-b from-slate-50 to-slate-100 text-slate-900">
|
||||
<header className="flex items-center justify-between px-8 py-6">
|
||||
<div className="flex items-center gap-2 text-sm font-semibold text-slate-700">
|
||||
<span className="text-base">Stirling</span>
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<button className="rounded-full border border-slate-200 px-4 py-1.5 text-xs text-slate-500">
|
||||
Log in
|
||||
</button>
|
||||
<button className="rounded-full bg-slate-900 px-4 py-1.5 text-xs text-white">
|
||||
Get Stirling free
|
||||
</button>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<main className="flex flex-1 items-center justify-center px-6 pb-16">
|
||||
<div className="w-full max-w-4xl rounded-[32px] border border-slate-200 bg-white p-10 shadow-lg">
|
||||
<div className="flex flex-col items-center text-center gap-4">
|
||||
<div className="text-3xl font-semibold text-slate-900">Stirling PDF</div>
|
||||
<p className="text-sm text-slate-500">Create any PDF you can imagine with AI</p>
|
||||
</div>
|
||||
|
||||
<form onSubmit={onSubmit} className="mt-10">
|
||||
<div className="rounded-2xl border border-slate-200 bg-white px-5 py-4 shadow-sm">
|
||||
{isRecording ? (
|
||||
<div className="space-y-3">
|
||||
<AudioWaveform history={waveformHistory} />
|
||||
<div className="flex items-center justify-between text-xs text-slate-500">
|
||||
<span>{whisperStatus || 'Listening...'}</span>
|
||||
<div className="flex gap-2">
|
||||
<button
|
||||
type="button"
|
||||
className="rounded-full border border-slate-200 px-3 py-1"
|
||||
onClick={onCancelRecording}
|
||||
>
|
||||
Cancel
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
className="rounded-full bg-blue-600 px-3 py-1 text-white"
|
||||
onClick={onAcceptRecording}
|
||||
>
|
||||
Accept
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<>
|
||||
<textarea
|
||||
className="w-full resize-none text-sm text-slate-700 placeholder:text-slate-400 focus:outline-none"
|
||||
rows={1}
|
||||
value={prompt}
|
||||
onChange={(event) => onPromptChange(event.target.value)}
|
||||
onKeyDown={onKeyDown}
|
||||
placeholder="Make an invoice for me to bill a client for $1500 in consulting fees"
|
||||
/>
|
||||
{uploadedPdfFile && (
|
||||
<div className="mt-2 text-xs text-emerald-600">{uploadedPdfFile.name}</div>
|
||||
)}
|
||||
<div className="mt-4 flex items-center justify-between">
|
||||
<div className="flex items-center gap-2 relative">
|
||||
<label
|
||||
htmlFor="pdf-upload-landing"
|
||||
className="flex h-8 w-8 items-center justify-center rounded-full border border-slate-200 text-slate-400"
|
||||
>
|
||||
+
|
||||
</label>
|
||||
<input
|
||||
id="pdf-upload-landing"
|
||||
type="file"
|
||||
accept="application/pdf"
|
||||
onChange={(event) => onFileSelect(event.target.files?.[0] || null)}
|
||||
className="hidden"
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
className="rounded-full border border-slate-200 px-3 py-1 text-xs text-slate-500"
|
||||
onClick={onToggleTemplatePanel}
|
||||
>
|
||||
Template
|
||||
</button>
|
||||
<span className="text-xs text-slate-400">
|
||||
{formatDocLabel(selectedDocType)} · {formatDocLabel(selectedTemplateId)}
|
||||
</span>
|
||||
{isTemplatePanelOpen && (
|
||||
<div className="absolute left-2 top-10 w-80 rounded-2xl border border-slate-200 bg-white p-4 shadow-lg z-50">
|
||||
<div className="absolute -top-2 left-6 h-3 w-3 rotate-45 border border-slate-200 bg-white" />
|
||||
<div className="space-y-3">
|
||||
<div className="flex items-center gap-2 rounded-full border border-slate-200 px-3 py-2 text-xs text-slate-500">
|
||||
<span>🔎</span>
|
||||
<input
|
||||
className="w-full bg-transparent text-sm text-slate-600 focus:outline-none"
|
||||
placeholder="Search templates..."
|
||||
value={templateSearch}
|
||||
onChange={(event) => setTemplateSearch(event.target.value)}
|
||||
/>
|
||||
</div>
|
||||
<div className="flex gap-2 overflow-x-auto pb-1">
|
||||
{[
|
||||
{ id: 'popular', label: 'Popular' },
|
||||
{ id: 'legal', label: 'Legal' },
|
||||
{ id: 'financial', label: 'Financial' },
|
||||
{ id: 'academic', label: 'Academic' },
|
||||
{ id: 'marketing', label: 'Marketing' },
|
||||
{ id: 'operations', label: 'Operations' },
|
||||
].map((tab) => (
|
||||
<button
|
||||
type="button"
|
||||
key={tab.id}
|
||||
onClick={() => setActiveTemplateTab(tab.id as typeof activeTemplateTab)}
|
||||
className={`rounded-full px-3 py-1 text-xs ${
|
||||
activeTemplateTab === tab.id
|
||||
? 'bg-blue-100 text-blue-700'
|
||||
: 'bg-slate-100 text-slate-500'
|
||||
}`}
|
||||
>
|
||||
{tab.label}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="mt-4 space-y-2 max-h-64 overflow-y-auto pr-1 relative">
|
||||
{visibleDocTypes.map((docType) => {
|
||||
const isExpanded = expandedDocType === docType
|
||||
const templates =
|
||||
templateCatalog.find((entry) => entry.docType === docType)?.templates ||
|
||||
(docType === selectedDocType ? templatesForSelected : ['default'])
|
||||
return (
|
||||
<div key={docType} className="rounded-lg border border-slate-200 bg-white">
|
||||
<div
|
||||
className="flex w-full items-center justify-between px-3 py-2 text-sm text-slate-700"
|
||||
>
|
||||
<button
|
||||
type="button"
|
||||
className="flex flex-1 items-center gap-2 text-left"
|
||||
onClick={() => onSelectTemplate(docType, 'default')}
|
||||
>
|
||||
<span>{docTypeIcon(docType)}</span>
|
||||
<span>{formatDocLabel(docType)}</span>
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
className="text-slate-400 px-2"
|
||||
onClick={() => setExpandedDocType(isExpanded ? null : docType)}
|
||||
>
|
||||
{isExpanded ? '▾' : '▸'}
|
||||
</button>
|
||||
</div>
|
||||
{isExpanded && (
|
||||
<div className="border-t border-slate-200 px-3 py-2 space-y-2">
|
||||
{(templates || ['default']).map((templateId) => (
|
||||
<button
|
||||
type="button"
|
||||
key={`${docType}-${templateId}`}
|
||||
onClick={() => onSelectTemplate(docType, templateId)}
|
||||
className={`flex w-full items-center justify-between rounded-md px-2 py-1 text-sm ${
|
||||
selectedDocType === docType && selectedTemplateId === templateId
|
||||
? 'bg-blue-50 text-blue-700'
|
||||
: 'text-slate-600'
|
||||
}`}
|
||||
onMouseMove={(event) =>
|
||||
setHoveredTemplate({
|
||||
docType,
|
||||
templateId,
|
||||
x: event.clientX,
|
||||
y: event.clientY,
|
||||
})
|
||||
}
|
||||
onMouseLeave={() => setHoveredTemplate(null)}
|
||||
>
|
||||
<span>{formatDocLabel(templateId)}</span>
|
||||
<span className="text-xs text-slate-400">Select</span>
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
{!visibleDocTypes.length && (
|
||||
<div className="text-sm text-slate-400">No templates found.</div>
|
||||
)}
|
||||
{hoveredTemplate && (
|
||||
<div
|
||||
className="fixed z-50 w-36 rounded-lg border border-slate-200 bg-white shadow-lg p-2"
|
||||
style={{ left: hoveredTemplate.x + 12, top: hoveredTemplate.y + 12 }}
|
||||
>
|
||||
<div className="text-[10px] uppercase tracking-wide text-slate-400 mb-2">
|
||||
Preview
|
||||
</div>
|
||||
<div className="w-full bg-slate-50 rounded-md overflow-hidden" style={{ aspectRatio: '210 / 297' }}>
|
||||
<img
|
||||
src={templateThumbnailUrl(hoveredTemplate.docType, hoveredTemplate.templateId)}
|
||||
alt={`${hoveredTemplate.docType} ${hoveredTemplate.templateId} preview`}
|
||||
className="h-full w-full object-contain"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<button
|
||||
type="button"
|
||||
className="flex h-8 w-8 items-center justify-center rounded-full border border-slate-200 text-slate-500"
|
||||
onClick={onToggleRecording}
|
||||
aria-label="Voice input"
|
||||
>
|
||||
🎤
|
||||
</button>
|
||||
<button
|
||||
type="submit"
|
||||
disabled={isImporting || (!prompt.trim() && !uploadedPdfFile)}
|
||||
className="flex h-8 w-8 items-center justify-center rounded-full bg-slate-900 text-white disabled:opacity-40"
|
||||
aria-label="Generate"
|
||||
>
|
||||
➜
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</form>
|
||||
|
||||
</div>
|
||||
</main>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
interface ImportLayoutModalProps {
|
||||
isOpen: boolean
|
||||
docType: string
|
||||
onDocTypeChange: (value: string) => void
|
||||
onClose: () => void
|
||||
onFileSelected: (file: File) => void
|
||||
isImporting: boolean
|
||||
status: string | null
|
||||
}
|
||||
|
||||
export function ImportLayoutModal({
|
||||
isOpen,
|
||||
docType,
|
||||
onDocTypeChange,
|
||||
onClose,
|
||||
onFileSelected,
|
||||
isImporting,
|
||||
status,
|
||||
}: ImportLayoutModalProps) {
|
||||
if (!isOpen) return null
|
||||
|
||||
return (
|
||||
<div className="fixed inset-0 bg-slate-900/70 backdrop-blur-sm z-50 flex items-center justify-center p-4">
|
||||
<div className="bg-slate-900 border border-slate-700 rounded-xl shadow-2xl w-full max-w-md p-5 space-y-4">
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<div className="text-sm text-slate-300">Import layout from PDF</div>
|
||||
<div className="text-xs text-slate-500">First 2 pages only, uses vision model</div>
|
||||
</div>
|
||||
<button onClick={onClose} className="text-slate-400 hover:text-white text-sm">
|
||||
✕
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<label className="text-xs text-slate-400">Document type</label>
|
||||
<input
|
||||
type="text"
|
||||
value={docType}
|
||||
onChange={(e) => onDocTypeChange(e.target.value)}
|
||||
className="w-full bg-slate-800 border border-slate-700 rounded-lg px-3 py-2 text-slate-100 text-sm"
|
||||
placeholder="invoice, resume, report..."
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<label className="text-xs text-slate-400">PDF file</label>
|
||||
<input
|
||||
type="file"
|
||||
accept="application/pdf"
|
||||
onChange={(e) => {
|
||||
const file = e.target.files?.[0]
|
||||
if (file) onFileSelected(file)
|
||||
}}
|
||||
className="w-full text-sm text-slate-300"
|
||||
disabled={isImporting}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{status && (
|
||||
<div className="text-xs text-slate-300 bg-slate-800 border border-slate-700 rounded-lg px-3 py-2">
|
||||
{status}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="flex items-center justify-end gap-3 text-xs text-slate-400">
|
||||
<button onClick={onClose} className="px-3 py-1 rounded-lg border border-slate-700 hover:bg-slate-800">
|
||||
Close
|
||||
</button>
|
||||
{isImporting && <span>Processing...</span>}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,223 @@
|
||||
import { useCallback, useEffect, useMemo, useState } from 'react'
|
||||
import { PDFDocument, rgb, StandardFonts } from 'pdf-lib'
|
||||
import { GlobalWorkerOptions, getDocument } from 'pdfjs-dist'
|
||||
|
||||
GlobalWorkerOptions.workerSrc = new URL('pdfjs-dist/build/pdf.worker.min.mjs', import.meta.url).toString()
|
||||
|
||||
type PageTextItem = {
|
||||
id: string
|
||||
str: string
|
||||
x: number
|
||||
y: number
|
||||
width: number
|
||||
fontSize: number
|
||||
}
|
||||
|
||||
type PageData = {
|
||||
width: number
|
||||
height: number
|
||||
image: string
|
||||
items: PageTextItem[]
|
||||
}
|
||||
|
||||
interface PdfTextEditorLiteProps {
|
||||
pdfUrl: string
|
||||
onClose: () => void
|
||||
}
|
||||
|
||||
export function PdfTextEditorLite({ pdfUrl, onClose }: PdfTextEditorLiteProps) {
|
||||
const [pages, setPages] = useState<PageData[]>([])
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [saving, setSaving] = useState(false)
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
const [editedItems, setEditedItems] = useState<Record<string, string>>({})
|
||||
|
||||
const loadPdf = useCallback(async () => {
|
||||
setLoading(true)
|
||||
setError(null)
|
||||
try {
|
||||
const resp = await fetch(pdfUrl)
|
||||
const buffer = await resp.arrayBuffer()
|
||||
const pdf = await getDocument({ data: buffer }).promise
|
||||
const loaded: PageData[] = []
|
||||
|
||||
for (let i = 1; i <= pdf.numPages; i += 1) {
|
||||
const page = await pdf.getPage(i)
|
||||
const viewport = page.getViewport({ scale: 1.2 })
|
||||
|
||||
// Render page to image
|
||||
const canvas = document.createElement('canvas')
|
||||
const ctx = canvas.getContext('2d')
|
||||
if (!ctx) continue
|
||||
canvas.width = viewport.width
|
||||
canvas.height = viewport.height
|
||||
await page.render({ canvasContext: ctx, viewport }).promise
|
||||
const image = canvas.toDataURL('image/png')
|
||||
|
||||
// Extract text items
|
||||
const textContent = await page.getTextContent()
|
||||
const items: PageTextItem[] = textContent.items
|
||||
.map((raw, idx) => {
|
||||
const it = raw as any
|
||||
const transform: number[] = Array.isArray(it?.transform) ? it.transform : [1, 0, 0, 1, 0, 0]
|
||||
const [a, b, , , e, f] = transform
|
||||
const x = typeof e === 'number' ? e : 0
|
||||
const y = typeof f === 'number' ? f : 0
|
||||
const fontSize = Math.hypot(a || 0, b || 0) || it?.height || 12
|
||||
const width = ((it?.width as number | undefined) || fontSize) * viewport.scale
|
||||
const str = typeof it?.str === 'string' ? it.str : ''
|
||||
return { id: `${i}-${idx}`, str, x, y, width, fontSize }
|
||||
})
|
||||
.filter((it) => it.str.length > 0)
|
||||
|
||||
loaded.push({
|
||||
width: viewport.width,
|
||||
height: viewport.height,
|
||||
image,
|
||||
items,
|
||||
})
|
||||
}
|
||||
|
||||
setPages(loaded)
|
||||
const initialEdits: Record<string, string> = {}
|
||||
loaded.forEach((p) =>
|
||||
p.items.forEach((it) => {
|
||||
initialEdits[it.id] = it.str
|
||||
}),
|
||||
)
|
||||
setEditedItems(initialEdits)
|
||||
} catch (e: any) {
|
||||
setError(e?.message || 'Failed to load PDF')
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
}, [pdfUrl])
|
||||
|
||||
useEffect(() => {
|
||||
loadPdf()
|
||||
}, [loadPdf])
|
||||
|
||||
const handleChange = (id: string, value: string) => {
|
||||
setEditedItems((prev) => ({ ...prev, [id]: value }))
|
||||
}
|
||||
|
||||
const handleDownload = useCallback(async () => {
|
||||
setSaving(true)
|
||||
setError(null)
|
||||
try {
|
||||
const doc = await PDFDocument.create()
|
||||
const font = await doc.embedFont(StandardFonts.Helvetica)
|
||||
|
||||
for (const page of pages) {
|
||||
const pdfPage = doc.addPage([page.width, page.height])
|
||||
const bg = await doc.embedPng(page.image)
|
||||
pdfPage.drawImage(bg, { x: 0, y: 0, width: page.width, height: page.height })
|
||||
|
||||
page.items.forEach((item) => {
|
||||
const text = editedItems[item.id] ?? item.str
|
||||
const yPdf = page.height - item.y // flip coordinate to bottom-left origin
|
||||
pdfPage.drawText(text, {
|
||||
x: item.x,
|
||||
y: yPdf - item.fontSize,
|
||||
size: item.fontSize || 12,
|
||||
font,
|
||||
color: rgb(0, 0, 0),
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
const bytes = await doc.save()
|
||||
const arrayBuffer = new ArrayBuffer(bytes.byteLength)
|
||||
new Uint8Array(arrayBuffer).set(bytes)
|
||||
const blob = new Blob([arrayBuffer], { type: 'application/pdf' })
|
||||
const url = URL.createObjectURL(blob)
|
||||
const a = document.createElement('a')
|
||||
a.href = url
|
||||
a.download = 'edited.pdf'
|
||||
a.click()
|
||||
URL.revokeObjectURL(url)
|
||||
} catch (e: any) {
|
||||
setError(e?.message || 'Failed to generate PDF')
|
||||
} finally {
|
||||
setSaving(false)
|
||||
}
|
||||
}, [editedItems, pages])
|
||||
|
||||
const pageCount = useMemo(() => pages.length, [pages])
|
||||
|
||||
return (
|
||||
<div className="w-full h-full flex flex-col bg-slate-950">
|
||||
<div className="flex items-center justify-between px-4 py-3 border-b border-slate-800 bg-slate-900">
|
||||
<div className="flex items-center gap-3">
|
||||
<h3 className="text-slate-100 font-semibold">Text Editor (lite)</h3>
|
||||
<span className="text-xs text-slate-400">Pages: {pageCount || '—'}</span>
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<button
|
||||
onClick={handleDownload}
|
||||
disabled={saving || loading || pages.length === 0}
|
||||
className="px-3 py-1.5 rounded bg-blue-600 text-white text-sm disabled:opacity-50"
|
||||
>
|
||||
{saving ? 'Saving…' : 'Download edited PDF'}
|
||||
</button>
|
||||
<button
|
||||
onClick={onClose}
|
||||
className="px-3 py-1.5 rounded bg-slate-700 text-slate-100 text-sm hover:bg-slate-600"
|
||||
>
|
||||
Back to preview
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{error && <div className="px-4 py-2 text-sm text-amber-200 bg-amber-500/10 border border-amber-600">{error}</div>}
|
||||
|
||||
<div className="flex-1 overflow-y-auto p-4 space-y-8">
|
||||
{loading && <div className="text-slate-300 text-sm">Loading PDF…</div>}
|
||||
{!loading &&
|
||||
pages.map((page, pageIdx) => (
|
||||
<div
|
||||
key={page.image}
|
||||
className="relative bg-slate-900 border border-slate-800 rounded-lg p-2 shadow"
|
||||
style={{ width: page.width, minHeight: page.height }}
|
||||
>
|
||||
<div className="absolute inset-2">
|
||||
<img
|
||||
src={page.image}
|
||||
alt={`Page ${pageIdx + 1}`}
|
||||
className="w-full h-auto rounded border border-slate-800 shadow pointer-events-none select-none"
|
||||
/>
|
||||
<div className="absolute inset-0">
|
||||
{page.items.map((item) => {
|
||||
const yTop = page.height - item.y
|
||||
return (
|
||||
<div
|
||||
key={item.id}
|
||||
contentEditable
|
||||
suppressContentEditableWarning
|
||||
onInput={(e) => handleChange(item.id, (e.target as HTMLDivElement).innerText)}
|
||||
className="absolute bg-transparent outline-none focus:ring-1 focus:ring-blue-400 rounded px-0.5"
|
||||
style={{
|
||||
left: item.x,
|
||||
top: yTop - item.fontSize * 0.85,
|
||||
minWidth: Math.max(item.width, 4),
|
||||
fontSize: item.fontSize,
|
||||
lineHeight: '1.05',
|
||||
color: '#111827',
|
||||
}}
|
||||
>
|
||||
{editedItems[item.id] ?? item.str}
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
{!loading && pages.length === 0 && <div className="text-slate-400 text-sm">No pages loaded.</div>}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default PdfTextEditorLite
|
||||
|
||||
@@ -0,0 +1,223 @@
|
||||
import { useEffect, useState } from 'react'
|
||||
import { GlobalWorkerOptions, getDocument } from 'pdfjs-dist'
|
||||
|
||||
GlobalWorkerOptions.workerSrc = new URL('pdfjs-dist/build/pdf.worker.min.mjs', import.meta.url).toString()
|
||||
|
||||
type ThumbnailPage = {
|
||||
id: string
|
||||
dataUrl: string
|
||||
width: number
|
||||
height: number
|
||||
}
|
||||
|
||||
function PdfPreviewSkeleton() {
|
||||
return (
|
||||
<div className="flex justify-center py-8">
|
||||
<div
|
||||
className="relative rounded-2xl border border-slate-300/60 bg-white shadow-2xl overflow-hidden"
|
||||
style={{
|
||||
width: '860px',
|
||||
maxWidth: '90vw',
|
||||
aspectRatio: '1 / 1.414',
|
||||
}}
|
||||
>
|
||||
<div className="h-12 bg-slate-100 border-b border-slate-200 animate-pulse" />
|
||||
<div className="p-8 space-y-4">
|
||||
{[1, 2, 3, 4].map((line) => (
|
||||
<div
|
||||
key={`line-top-${line}`}
|
||||
className="h-4 rounded-full bg-slate-200 animate-pulse"
|
||||
style={{ width: `${78 - line * 10}%` }}
|
||||
/>
|
||||
))}
|
||||
<div className="h-40 rounded-xl bg-slate-100 border border-slate-200 animate-pulse" />
|
||||
{[5, 6, 7].map((line) => (
|
||||
<div
|
||||
key={`line-bottom-${line}`}
|
||||
className="h-4 rounded-full bg-slate-200 animate-pulse"
|
||||
style={{ width: `${70 - (line - 5) * 8}%` }}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
interface PdfThumbnailViewerProps {
|
||||
pdfUrl: string
|
||||
isLivePreviewing?: boolean
|
||||
}
|
||||
|
||||
export function PdfThumbnailViewer({ pdfUrl, isLivePreviewing = false }: PdfThumbnailViewerProps) {
|
||||
const [pages, setPages] = useState<ThumbnailPage[]>([])
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
const [zoom, setZoom] = useState(1)
|
||||
const [hasRenderedPage, setHasRenderedPage] = useState(false)
|
||||
|
||||
useEffect(() => {
|
||||
if (!pdfUrl) {
|
||||
setPages([])
|
||||
setHasRenderedPage(false)
|
||||
return
|
||||
}
|
||||
|
||||
let cancelled = false
|
||||
const controller = new AbortController()
|
||||
|
||||
const load = async () => {
|
||||
setLoading(true)
|
||||
setError(null)
|
||||
try {
|
||||
const response = await fetch(pdfUrl, { signal: controller.signal })
|
||||
if (!response.ok) {
|
||||
throw new Error('Unable to download PDF preview')
|
||||
}
|
||||
const buffer = await response.arrayBuffer()
|
||||
const pdf = await getDocument({ data: buffer }).promise
|
||||
const next: ThumbnailPage[] = []
|
||||
try {
|
||||
for (let pageNumber = 1; pageNumber <= pdf.numPages; pageNumber += 1) {
|
||||
const page = await pdf.getPage(pageNumber)
|
||||
const viewport = page.getViewport({ scale: 0.85 })
|
||||
const canvas = document.createElement('canvas')
|
||||
const ctx = canvas.getContext('2d')
|
||||
if (ctx) {
|
||||
canvas.width = viewport.width
|
||||
canvas.height = viewport.height
|
||||
await page.render({ canvasContext: ctx, viewport }).promise
|
||||
next.push({
|
||||
id: `page-${pageNumber}`,
|
||||
dataUrl: canvas.toDataURL('image/png'),
|
||||
width: viewport.width,
|
||||
height: viewport.height,
|
||||
})
|
||||
canvas.width = 0
|
||||
canvas.height = 0
|
||||
}
|
||||
page.cleanup?.()
|
||||
}
|
||||
} finally {
|
||||
try {
|
||||
pdf.cleanup?.()
|
||||
await pdf.destroy?.()
|
||||
} catch {
|
||||
// ignore cleanup errors
|
||||
}
|
||||
}
|
||||
if (!cancelled) {
|
||||
setPages(next)
|
||||
if (next.length > 0) {
|
||||
setHasRenderedPage(true)
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
if (!cancelled) {
|
||||
const message = err instanceof Error ? err.message : 'Failed to load PDF preview'
|
||||
const normalized = message.toLowerCase()
|
||||
if (
|
||||
isLivePreviewing &&
|
||||
(normalized.includes('zero bytes') || normalized.includes('file is empty') || normalized.includes('pdf is empty'))
|
||||
) {
|
||||
setError(null)
|
||||
} else {
|
||||
setError(message)
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
if (!cancelled) {
|
||||
setLoading(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
load()
|
||||
|
||||
return () => {
|
||||
cancelled = true
|
||||
controller.abort()
|
||||
}
|
||||
}, [pdfUrl, isLivePreviewing])
|
||||
|
||||
const shouldShowSkeleton = (loading && !hasRenderedPage && pages.length === 0) || (isLivePreviewing && pages.length === 0 && !error && !hasRenderedPage)
|
||||
|
||||
return (
|
||||
<div className="flex h-full flex-col bg-slate-900">
|
||||
{error && (
|
||||
<div className="border-b border-amber-700 bg-amber-500/15 px-4 py-3 text-sm text-amber-200">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="relative flex-1 overflow-y-auto px-6 py-6">
|
||||
<div className="sticky top-0 z-10 mb-4 flex items-center justify-end gap-3 rounded-lg border border-slate-800 bg-slate-850/80 px-3 py-2 backdrop-blur">
|
||||
<span className="text-xs text-slate-300">Zoom</span>
|
||||
<div className="flex items-center gap-2">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setZoom((z) => Math.max(0.5, Math.round((z - 0.1) * 100) / 100))}
|
||||
className="h-8 w-8 rounded border border-slate-700 bg-slate-800 text-slate-200 hover:bg-slate-750"
|
||||
>
|
||||
−
|
||||
</button>
|
||||
<input
|
||||
type="range"
|
||||
min={0.5}
|
||||
max={2}
|
||||
step={0.05}
|
||||
value={zoom}
|
||||
onChange={(e) => setZoom(Number(e.target.value))}
|
||||
className="h-2 w-32 accent-blue-500"
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setZoom((z) => Math.min(2, Math.round((z + 0.1) * 100) / 100))}
|
||||
className="h-8 w-8 rounded border border-slate-700 bg-slate-800 text-slate-200 hover:bg-slate-750"
|
||||
>
|
||||
+
|
||||
</button>
|
||||
<span className="w-12 text-right text-xs text-slate-300">{Math.round(zoom * 100)}%</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{shouldShowSkeleton && <PdfPreviewSkeleton />}
|
||||
|
||||
{!shouldShowSkeleton && pages.length === 0 && (
|
||||
<div className="flex h-full items-center justify-center text-sm text-slate-400">Preview unavailable.</div>
|
||||
)}
|
||||
|
||||
{!shouldShowSkeleton && pages.length > 0 && (
|
||||
<div className="mx-auto flex max-w-5xl flex-col items-center gap-8">
|
||||
{pages.map((page) => {
|
||||
const baseScale = Math.min(920 / page.width, 1)
|
||||
const displayScale = baseScale * zoom
|
||||
const displayWidth = page.width * displayScale
|
||||
const displayHeight = page.height * displayScale
|
||||
return (
|
||||
<div
|
||||
key={page.id}
|
||||
className="overflow-hidden rounded-lg border border-slate-800 bg-white shadow-2xl"
|
||||
style={{
|
||||
width: displayWidth,
|
||||
height: displayHeight,
|
||||
maxWidth: '100%',
|
||||
}}
|
||||
>
|
||||
<img
|
||||
src={page.dataUrl}
|
||||
alt={`PDF page ${page.id}`}
|
||||
className="block h-full w-full select-none object-contain"
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default PdfThumbnailViewer
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
import React from 'react'
|
||||
|
||||
type ZoomControlsProps = {
|
||||
value: number
|
||||
onChange: (next: number) => void
|
||||
min?: number
|
||||
max?: number
|
||||
step?: number
|
||||
disabled?: boolean
|
||||
className?: string
|
||||
}
|
||||
|
||||
const clamp = (value: number, min: number, max: number) => Math.min(Math.max(value, min), max)
|
||||
|
||||
const formatPercent = (value: number) => `${Math.round(value * 100)}%`
|
||||
|
||||
const ZoomControls: React.FC<ZoomControlsProps> = ({
|
||||
value,
|
||||
onChange,
|
||||
min = 0.2,
|
||||
max = 3,
|
||||
step = 0.1,
|
||||
disabled = false,
|
||||
className = '',
|
||||
}) => {
|
||||
const handleChange = (next: number) => {
|
||||
if (disabled) return
|
||||
const clamped = clamp(next, min, max)
|
||||
onChange(Number(clamped.toFixed(2)))
|
||||
}
|
||||
|
||||
return (
|
||||
<div className={`flex items-center gap-2 ${className}`}>
|
||||
<span className="hidden text-xs text-slate-400 sm:inline">Zoom</span>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => handleChange(value - step)}
|
||||
disabled={disabled}
|
||||
className="rounded border border-slate-700 bg-slate-800 px-2 py-1 text-sm text-slate-200 hover:bg-slate-700 disabled:cursor-not-allowed disabled:opacity-50"
|
||||
>
|
||||
−
|
||||
</button>
|
||||
<div className="min-w-[56px] rounded border border-slate-700 bg-slate-800 px-2 py-1 text-center text-xs font-medium text-slate-100">
|
||||
{formatPercent(value)}
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => handleChange(value + step)}
|
||||
disabled={disabled}
|
||||
className="rounded border border-slate-700 bg-slate-800 px-2 py-1 text-sm text-slate-200 hover:bg-slate-700 disabled:cursor-not-allowed disabled:opacity-50"
|
||||
>
|
||||
+
|
||||
</button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default ZoomControls
|
||||
|
||||
@@ -0,0 +1,421 @@
|
||||
import { PdfJsonDocument, PdfJsonFont } from './pdfTextEditorTypes';
|
||||
|
||||
export type FontStatus = 'perfect' | 'embedded-subset' | 'system-fallback' | 'missing' | 'unknown';
|
||||
|
||||
export interface FontAnalysis {
|
||||
fontId: string;
|
||||
baseName: string;
|
||||
status: FontStatus;
|
||||
embedded: boolean;
|
||||
isSubset: boolean;
|
||||
isStandard14: boolean;
|
||||
hasWebFormat: boolean;
|
||||
webFormat?: string;
|
||||
subtype?: string;
|
||||
encoding?: string;
|
||||
warnings: string[];
|
||||
suggestions: string[];
|
||||
}
|
||||
|
||||
export interface DocumentFontAnalysis {
|
||||
fonts: FontAnalysis[];
|
||||
canReproducePerfectly: boolean;
|
||||
hasWarnings: boolean;
|
||||
summary: {
|
||||
perfect: number;
|
||||
embeddedSubset: number;
|
||||
systemFallback: number;
|
||||
missing: number;
|
||||
unknown: number;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if a font name indicates it's a subset font.
|
||||
* Subset fonts typically have a 6-character prefix like "ABCDEE+"
|
||||
*/
|
||||
const isSubsetFont = (baseName: string | null | undefined): boolean => {
|
||||
if (!baseName) return false;
|
||||
// Check for common subset patterns: ABCDEF+FontName
|
||||
return /^[A-Z]{6}\+/.test(baseName);
|
||||
};
|
||||
|
||||
/**
|
||||
* Checks if a font is one of the standard 14 PDF fonts that are guaranteed
|
||||
* to be available on all PDF readers
|
||||
*/
|
||||
const isStandard14Font = (font: PdfJsonFont): boolean => {
|
||||
if (font.standard14Name) return true;
|
||||
|
||||
const baseName = (font.baseName || '').toLowerCase().replace(/[-_\s]/g, '');
|
||||
|
||||
const standard14Patterns = [
|
||||
'timesroman', 'timesbold', 'timesitalic', 'timesbolditalic',
|
||||
'helvetica', 'helveticabold', 'helveticaoblique', 'helveticaboldoblique',
|
||||
'courier', 'courierbold', 'courieroblique', 'courierboldoblique',
|
||||
'symbol', 'zapfdingbats'
|
||||
];
|
||||
|
||||
// Check exact matches or if the base name contains the pattern
|
||||
return standard14Patterns.some(pattern => {
|
||||
// Exact match
|
||||
if (baseName === pattern) return true;
|
||||
// Contains pattern (e.g., "ABCDEF+Helvetica" matches "helvetica")
|
||||
if (baseName.includes(pattern)) return true;
|
||||
return false;
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Checks if a font has a fallback available on the backend.
|
||||
* These fonts are embedded in the Stirling PDF backend and can be used
|
||||
* for PDF export even if not in the original PDF.
|
||||
*
|
||||
* Based on PdfJsonFallbackFontService.java
|
||||
*/
|
||||
const hasBackendFallbackFont = (font: PdfJsonFont): boolean => {
|
||||
const baseName = (font.baseName || '').toLowerCase().replace(/[-_\s]/g, '');
|
||||
|
||||
// Backend has these font families available (from PdfJsonFallbackFontService)
|
||||
const backendFonts = [
|
||||
// Liberation fonts (metric-compatible with MS core fonts)
|
||||
'arial', 'helvetica', 'arimo',
|
||||
'times', 'timesnewroman', 'tinos',
|
||||
'courier', 'couriernew', 'cousine',
|
||||
'liberation', 'liberationsans', 'liberationserif', 'liberationmono',
|
||||
// DejaVu fonts
|
||||
'dejavu', 'dejavusans', 'dejavuserif', 'dejavumono', 'dejavusansmono',
|
||||
// Noto fonts
|
||||
'noto', 'notosans'
|
||||
];
|
||||
|
||||
return backendFonts.some(pattern => {
|
||||
if (baseName === pattern) return true;
|
||||
if (baseName.includes(pattern)) return true;
|
||||
return false;
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Extracts the base font name from a subset font name
|
||||
* e.g., "ABCDEF+Arial" -> "Arial"
|
||||
*/
|
||||
const extractBaseFontName = (baseName: string | null | undefined): string | null => {
|
||||
if (!baseName) return null;
|
||||
const match = baseName.match(/^[A-Z]{6}\+(.+)$/);
|
||||
return match ? match[1] : baseName;
|
||||
};
|
||||
|
||||
/**
|
||||
* Analyzes a single font to determine if it can be reproduced perfectly
|
||||
* Takes allFonts to check if full versions of subset fonts are available
|
||||
*/
|
||||
export const analyzeFontReproduction = (font: PdfJsonFont, allFonts?: PdfJsonFont[]): FontAnalysis => {
|
||||
const fontId = font.id || font.uid || 'unknown';
|
||||
const baseName = font.baseName || 'Unknown Font';
|
||||
const isSubset = isSubsetFont(font.baseName);
|
||||
const isStandard14 = isStandard14Font(font);
|
||||
const hasBackendFallback = hasBackendFallbackFont(font);
|
||||
const embedded = font.embedded ?? false;
|
||||
|
||||
// Check available web formats (ordered by preference)
|
||||
const webFormats = [
|
||||
{ key: 'webProgram', format: font.webProgramFormat },
|
||||
{ key: 'pdfProgram', format: font.pdfProgramFormat },
|
||||
{ key: 'program', format: font.programFormat },
|
||||
];
|
||||
|
||||
const availableWebFormat = webFormats.find(f => f.format);
|
||||
const hasWebFormat = !!availableWebFormat;
|
||||
const webFormat = availableWebFormat?.format || undefined;
|
||||
|
||||
const warnings: string[] = [];
|
||||
const suggestions: string[] = [];
|
||||
let status: FontStatus = 'unknown';
|
||||
|
||||
// Check if we have the full font when this is a subset
|
||||
let hasFullFontVersion = false;
|
||||
if (isSubset && allFonts) {
|
||||
const baseFont = extractBaseFontName(font.baseName);
|
||||
if (baseFont) {
|
||||
// Look for a non-subset version of this font with a web format
|
||||
hasFullFontVersion = allFonts.some(f => {
|
||||
const otherBaseName = extractBaseFontName(f.baseName);
|
||||
const isNotSubset = !isSubsetFont(f.baseName);
|
||||
const hasFormat = !!(f.webProgramFormat || f.pdfProgramFormat || f.programFormat);
|
||||
const sameBase = otherBaseName?.toLowerCase() === baseFont.toLowerCase();
|
||||
return sameBase && isNotSubset && hasFormat && (f.embedded ?? false);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Analyze font status - focusing on PDF export quality
|
||||
if (isStandard14) {
|
||||
// Standard 14 fonts are always available in PDF readers - perfect for export!
|
||||
status = 'perfect';
|
||||
suggestions.push('Standard PDF font (Times, Helvetica, or Courier). Always available in PDF readers.');
|
||||
suggestions.push('Exported PDFs will render consistently across all PDF readers.');
|
||||
} else if (embedded && !isSubset) {
|
||||
// Perfect: Fully embedded with complete character set
|
||||
status = 'perfect';
|
||||
suggestions.push('Font is fully embedded. Exported PDFs will reproduce text perfectly, even with edits.');
|
||||
} else if (embedded && isSubset && (hasFullFontVersion || hasBackendFallback)) {
|
||||
// Subset but we have the full font or backend fallback - perfect!
|
||||
status = 'perfect';
|
||||
if (hasFullFontVersion) {
|
||||
suggestions.push('Full font version is also available in the document. Exported PDFs can reproduce all characters.');
|
||||
} else if (hasBackendFallback) {
|
||||
suggestions.push('Backend has the full font available. Exported PDFs can reproduce all characters, including new text.');
|
||||
}
|
||||
} else if (embedded && isSubset) {
|
||||
// Good, but subset: May have missing characters if user adds new text
|
||||
status = 'embedded-subset';
|
||||
warnings.push('This is a subset font - only specific characters are embedded in the PDF.');
|
||||
warnings.push('Exported PDFs may have missing characters if you add new text with this font.');
|
||||
suggestions.push('Existing text will export correctly. New characters may render as boxes (☐) or fallback glyphs.');
|
||||
} else if (!embedded && hasBackendFallback) {
|
||||
// Not embedded, but backend has it - perfect for export!
|
||||
status = 'perfect';
|
||||
suggestions.push('Backend has this font available. Exported PDFs will use the backend fallback font.');
|
||||
suggestions.push('Text will export correctly with consistent appearance.');
|
||||
} else if (!embedded) {
|
||||
// Not embedded - must rely on system fonts (risky for export)
|
||||
status = 'missing';
|
||||
warnings.push('Font is not embedded in the PDF.');
|
||||
warnings.push('Exported PDFs will substitute with a fallback font, which may look very different.');
|
||||
suggestions.push('Consider re-embedding fonts or accepting that the exported PDF will use fallback fonts.');
|
||||
} else if (embedded && !hasWebFormat) {
|
||||
// Embedded but no web format available (still okay for export)
|
||||
status = 'perfect';
|
||||
suggestions.push('Font is embedded in the PDF. Exported PDFs will reproduce correctly.');
|
||||
suggestions.push('Web preview may use a fallback font, but the final PDF export will be accurate.');
|
||||
}
|
||||
|
||||
// Additional warnings based on font properties
|
||||
if (font.subtype === 'Type0' && font.cidSystemInfo) {
|
||||
const registry = font.cidSystemInfo.registry || '';
|
||||
const ordering = font.cidSystemInfo.ordering || '';
|
||||
if (registry.includes('Adobe') && (ordering.includes('Identity') || ordering.includes('UCS'))) {
|
||||
// CID fonts with Identity encoding are common for Asian languages
|
||||
if (!embedded || !hasWebFormat) {
|
||||
warnings.push('This CID font may contain Asian or Unicode characters.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (font.encoding && !font.encoding.includes('WinAnsiEncoding') && !font.encoding.includes('MacRomanEncoding')) {
|
||||
// Custom encodings may cause issues
|
||||
if (font.encoding !== 'Identity-H' && font.encoding !== 'Identity-V') {
|
||||
warnings.push(`Custom encoding detected: ${font.encoding}`);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
fontId,
|
||||
baseName,
|
||||
status,
|
||||
embedded,
|
||||
isSubset,
|
||||
isStandard14,
|
||||
hasWebFormat,
|
||||
webFormat,
|
||||
subtype: font.subtype || undefined,
|
||||
encoding: font.encoding || undefined,
|
||||
warnings,
|
||||
suggestions,
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets fonts used on a specific page
|
||||
*/
|
||||
export const getFontsForPage = (
|
||||
document: PdfJsonDocument | null,
|
||||
pageIndex: number
|
||||
): PdfJsonFont[] => {
|
||||
if (!document?.fonts || !document?.pages || pageIndex < 0 || pageIndex >= document.pages.length) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const page = document.pages[pageIndex];
|
||||
if (!page?.textElements) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// Get unique font IDs used on this page
|
||||
const fontIdsOnPage = new Set<string>();
|
||||
page.textElements.forEach(element => {
|
||||
if (element?.fontId) {
|
||||
fontIdsOnPage.add(element.fontId);
|
||||
}
|
||||
});
|
||||
|
||||
// Filter fonts to only those used on this page
|
||||
const allFonts = document.fonts.filter((font): font is PdfJsonFont => font !== null && font !== undefined);
|
||||
|
||||
const fontsOnPage = allFonts.filter(font => {
|
||||
// Match by ID
|
||||
if (font.id && fontIdsOnPage.has(font.id)) {
|
||||
return true;
|
||||
}
|
||||
// Match by UID
|
||||
if (font.uid && fontIdsOnPage.has(font.uid)) {
|
||||
return true;
|
||||
}
|
||||
// Match by page-specific ID (pageNumber:id format)
|
||||
if (font.pageNumber === pageIndex + 1 && font.id) {
|
||||
const pageSpecificId = `${font.pageNumber}:${font.id}`;
|
||||
if (fontIdsOnPage.has(pageSpecificId) || fontIdsOnPage.has(font.id)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
// Deduplicate by base font name to avoid showing the same font multiple times
|
||||
const uniqueFonts = new Map<string, PdfJsonFont>();
|
||||
fontsOnPage.forEach(font => {
|
||||
const baseName = extractBaseFontName(font.baseName) || font.baseName || font.id || 'unknown';
|
||||
const key = baseName.toLowerCase();
|
||||
|
||||
// Keep the first occurrence, or prefer non-subset over subset
|
||||
const existing = uniqueFonts.get(key);
|
||||
if (!existing) {
|
||||
uniqueFonts.set(key, font);
|
||||
} else {
|
||||
// Prefer non-subset fonts over subset fonts
|
||||
const existingIsSubset = isSubsetFont(existing.baseName);
|
||||
const currentIsSubset = isSubsetFont(font.baseName);
|
||||
if (existingIsSubset && !currentIsSubset) {
|
||||
uniqueFonts.set(key, font);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return Array.from(uniqueFonts.values());
|
||||
};
|
||||
|
||||
/**
|
||||
* Analyzes all fonts in a PDF document (or just fonts for a specific page)
|
||||
*/
|
||||
export const analyzeDocumentFonts = (
|
||||
document: PdfJsonDocument | null,
|
||||
pageIndex?: number
|
||||
): DocumentFontAnalysis => {
|
||||
if (!document?.fonts || document.fonts.length === 0) {
|
||||
return {
|
||||
fonts: [],
|
||||
canReproducePerfectly: true,
|
||||
hasWarnings: false,
|
||||
summary: {
|
||||
perfect: 0,
|
||||
embeddedSubset: 0,
|
||||
systemFallback: 0,
|
||||
missing: 0,
|
||||
unknown: 0,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const allFonts = document.fonts.filter((font): font is PdfJsonFont => font !== null && font !== undefined);
|
||||
|
||||
// Filter to page-specific fonts if pageIndex is provided
|
||||
const fontsToAnalyze = pageIndex !== undefined
|
||||
? getFontsForPage(document, pageIndex)
|
||||
: allFonts;
|
||||
|
||||
if (fontsToAnalyze.length === 0) {
|
||||
return {
|
||||
fonts: [],
|
||||
canReproducePerfectly: true,
|
||||
hasWarnings: false,
|
||||
summary: {
|
||||
perfect: 0,
|
||||
embeddedSubset: 0,
|
||||
systemFallback: 0,
|
||||
missing: 0,
|
||||
unknown: 0,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const fontAnalyses = fontsToAnalyze.map(font => analyzeFontReproduction(font, allFonts));
|
||||
|
||||
// Calculate summary
|
||||
const summary = {
|
||||
perfect: fontAnalyses.filter(f => f.status === 'perfect').length,
|
||||
embeddedSubset: fontAnalyses.filter(f => f.status === 'embedded-subset').length,
|
||||
systemFallback: fontAnalyses.filter(f => f.status === 'system-fallback').length,
|
||||
missing: fontAnalyses.filter(f => f.status === 'missing').length,
|
||||
unknown: fontAnalyses.filter(f => f.status === 'unknown').length,
|
||||
};
|
||||
|
||||
// Can reproduce perfectly ONLY if all fonts are truly perfect (not subsets)
|
||||
const canReproducePerfectly = fontAnalyses.every(f => f.status === 'perfect');
|
||||
|
||||
// Has warnings if any font has issues (including subsets)
|
||||
const hasWarnings = fontAnalyses.some(
|
||||
f => f.warnings.length > 0 || f.status === 'missing' || f.status === 'system-fallback' || f.status === 'embedded-subset'
|
||||
);
|
||||
|
||||
return {
|
||||
fonts: fontAnalyses,
|
||||
canReproducePerfectly,
|
||||
hasWarnings,
|
||||
summary,
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets a human-readable description of the font status
|
||||
*/
|
||||
export const getFontStatusDescription = (status: FontStatus): string => {
|
||||
switch (status) {
|
||||
case 'perfect':
|
||||
return 'Fully embedded - perfect reproduction';
|
||||
case 'embedded-subset':
|
||||
return 'Embedded (subset) - existing text will render correctly';
|
||||
case 'system-fallback':
|
||||
return 'Using system font - appearance may differ';
|
||||
case 'missing':
|
||||
return 'Not embedded - will use fallback font';
|
||||
case 'unknown':
|
||||
return 'Unknown status';
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets a color indicator for the font status
|
||||
*/
|
||||
export const getFontStatusColor = (status: FontStatus): string => {
|
||||
switch (status) {
|
||||
case 'perfect':
|
||||
return 'green';
|
||||
case 'embedded-subset':
|
||||
return 'blue';
|
||||
case 'system-fallback':
|
||||
return 'yellow';
|
||||
case 'missing':
|
||||
return 'red';
|
||||
case 'unknown':
|
||||
return 'gray';
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets an icon indicator for the font status
|
||||
*/
|
||||
export const getFontStatusIcon = (status: FontStatus): string => {
|
||||
switch (status) {
|
||||
case 'perfect':
|
||||
return '✓';
|
||||
case 'embedded-subset':
|
||||
return '⚠';
|
||||
case 'system-fallback':
|
||||
return '⚠';
|
||||
case 'missing':
|
||||
return '✗';
|
||||
case 'unknown':
|
||||
return '?';
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,232 @@
|
||||
export interface PdfJsonFontCidSystemInfo {
|
||||
registry?: string | null;
|
||||
ordering?: string | null;
|
||||
supplement?: number | null;
|
||||
}
|
||||
|
||||
export interface PdfJsonTextColor {
|
||||
colorSpace?: string | null;
|
||||
components?: number[] | null;
|
||||
}
|
||||
|
||||
export interface PdfJsonCosValue {
|
||||
type?: string | null;
|
||||
value?: unknown;
|
||||
items?: PdfJsonCosValue[] | null;
|
||||
entries?: Record<string, PdfJsonCosValue | null> | null;
|
||||
stream?: PdfJsonStream | null;
|
||||
}
|
||||
|
||||
export interface PdfJsonFont {
|
||||
id?: string;
|
||||
pageNumber?: number | null;
|
||||
uid?: string | null;
|
||||
baseName?: string | null;
|
||||
color?: string | null;
|
||||
subtype?: string | null;
|
||||
encoding?: string | null;
|
||||
cidSystemInfo?: PdfJsonFontCidSystemInfo | null;
|
||||
embedded?: boolean | null;
|
||||
program?: string | null;
|
||||
programFormat?: string | null;
|
||||
webProgram?: string | null;
|
||||
webProgramFormat?: string | null;
|
||||
pdfProgram?: string | null;
|
||||
pdfProgramFormat?: string | null;
|
||||
toUnicode?: string | null;
|
||||
standard14Name?: string | null;
|
||||
fontDescriptorFlags?: number | null;
|
||||
ascent?: number | null;
|
||||
descent?: number | null;
|
||||
capHeight?: number | null;
|
||||
xHeight?: number | null;
|
||||
italicAngle?: number | null;
|
||||
unitsPerEm?: number | null;
|
||||
cosDictionary?: PdfJsonCosValue | null;
|
||||
}
|
||||
|
||||
export interface PdfJsonTextElement {
|
||||
text?: string | null;
|
||||
fontId?: string | null;
|
||||
fontSize?: number | null;
|
||||
fontMatrixSize?: number | null;
|
||||
fontSizeInPt?: number | null;
|
||||
characterSpacing?: number | null;
|
||||
wordSpacing?: number | null;
|
||||
spaceWidth?: number | null;
|
||||
zOrder?: number | null;
|
||||
horizontalScaling?: number | null;
|
||||
leading?: number | null;
|
||||
rise?: number | null;
|
||||
renderingMode?: number | null;
|
||||
x?: number | null;
|
||||
y?: number | null;
|
||||
width?: number | null;
|
||||
height?: number | null;
|
||||
textMatrix?: number[] | null;
|
||||
fillColor?: PdfJsonTextColor | null;
|
||||
strokeColor?: PdfJsonTextColor | null;
|
||||
charCodes?: number[] | null;
|
||||
fallbackUsed?: boolean | null;
|
||||
}
|
||||
|
||||
export interface PdfJsonImageElement {
|
||||
id?: string | null;
|
||||
objectName?: string | null;
|
||||
inlineImage?: boolean | null;
|
||||
nativeWidth?: number | null;
|
||||
nativeHeight?: number | null;
|
||||
x?: number | null;
|
||||
y?: number | null;
|
||||
width?: number | null;
|
||||
height?: number | null;
|
||||
left?: number | null;
|
||||
right?: number | null;
|
||||
top?: number | null;
|
||||
bottom?: number | null;
|
||||
transform?: number[] | null;
|
||||
zOrder?: number | null;
|
||||
imageData?: string | null;
|
||||
imageFormat?: string | null;
|
||||
}
|
||||
|
||||
export interface PdfJsonStream {
|
||||
dictionary?: Record<string, unknown> | null;
|
||||
rawData?: string | null;
|
||||
}
|
||||
|
||||
export interface PdfJsonPage {
|
||||
pageNumber?: number | null;
|
||||
width?: number | null;
|
||||
height?: number | null;
|
||||
rotation?: number | null;
|
||||
mediaBox?: number[] | null;
|
||||
cropBox?: number[] | null;
|
||||
textElements?: PdfJsonTextElement[] | null;
|
||||
imageElements?: PdfJsonImageElement[] | null;
|
||||
resources?: unknown;
|
||||
contentStreams?: PdfJsonStream[] | null;
|
||||
}
|
||||
|
||||
export interface PdfJsonMetadata {
|
||||
title?: string | null;
|
||||
author?: string | null;
|
||||
subject?: string | null;
|
||||
keywords?: string | null;
|
||||
creator?: string | null;
|
||||
producer?: string | null;
|
||||
creationDate?: string | null;
|
||||
modificationDate?: string | null;
|
||||
trapped?: string | null;
|
||||
numberOfPages?: number | null;
|
||||
}
|
||||
|
||||
export interface PdfJsonDocument {
|
||||
metadata?: PdfJsonMetadata | null;
|
||||
xmpMetadata?: string | null;
|
||||
fonts?: PdfJsonFont[] | null;
|
||||
pages?: PdfJsonPage[] | null;
|
||||
lazyImages?: boolean | null;
|
||||
}
|
||||
|
||||
export interface PdfJsonPageDimension {
|
||||
pageNumber?: number | null;
|
||||
width?: number | null;
|
||||
height?: number | null;
|
||||
rotation?: number | null;
|
||||
}
|
||||
|
||||
export interface PdfJsonDocumentMetadata {
|
||||
metadata?: PdfJsonMetadata | null;
|
||||
xmpMetadata?: string | null;
|
||||
fonts?: PdfJsonFont[] | null;
|
||||
pageDimensions?: PdfJsonPageDimension[] | null;
|
||||
formFields?: unknown[] | null;
|
||||
lazyImages?: boolean | null;
|
||||
}
|
||||
|
||||
export interface BoundingBox {
|
||||
left: number;
|
||||
right: number;
|
||||
top: number;
|
||||
bottom: number;
|
||||
}
|
||||
|
||||
export interface TextGroup {
|
||||
id: string;
|
||||
pageIndex: number;
|
||||
fontId?: string | null;
|
||||
fontSize?: number | null;
|
||||
fontMatrixSize?: number | null;
|
||||
lineSpacing?: number | null;
|
||||
lineElementCounts?: number[] | null;
|
||||
color?: string | null;
|
||||
fontWeight?: number | 'normal' | 'bold' | null;
|
||||
rotation?: number | null;
|
||||
anchor?: { x: number; y: number } | null;
|
||||
baselineLength?: number | null;
|
||||
baseline?: number | null;
|
||||
elements: PdfJsonTextElement[];
|
||||
originalElements: PdfJsonTextElement[];
|
||||
text: string;
|
||||
originalText: string;
|
||||
bounds: BoundingBox;
|
||||
childLineGroups?: TextGroup[] | null;
|
||||
}
|
||||
|
||||
export const DEFAULT_PAGE_WIDTH = 612;
|
||||
export const DEFAULT_PAGE_HEIGHT = 792;
|
||||
|
||||
export interface ConversionProgress {
|
||||
percent: number;
|
||||
stage: string;
|
||||
message: string;
|
||||
current?: number;
|
||||
total?: number;
|
||||
}
|
||||
|
||||
export interface PdfTextEditorViewData {
|
||||
document: PdfJsonDocument | null;
|
||||
groupsByPage: TextGroup[][];
|
||||
imagesByPage: PdfJsonImageElement[][];
|
||||
pagePreviews: Map<number, string>;
|
||||
selectedPage: number;
|
||||
dirtyPages: boolean[];
|
||||
hasDocument: boolean;
|
||||
hasVectorPreview: boolean;
|
||||
fileName: string;
|
||||
errorMessage: string | null;
|
||||
isGeneratingPdf: boolean;
|
||||
isConverting: boolean;
|
||||
conversionProgress: ConversionProgress | null;
|
||||
hasChanges: boolean;
|
||||
forceSingleTextElement: boolean;
|
||||
groupingMode: 'auto' | 'paragraph' | 'singleLine';
|
||||
requestPagePreview: (pageIndex: number, scale: number) => void;
|
||||
onSelectPage: (pageIndex: number) => void;
|
||||
onGroupEdit: (pageIndex: number, groupId: string, value: string) => void;
|
||||
onGroupDelete: (pageIndex: number, groupId: string) => void;
|
||||
onImageTransform: (
|
||||
pageIndex: number,
|
||||
imageId: string,
|
||||
next: {
|
||||
left: number;
|
||||
bottom: number;
|
||||
width: number;
|
||||
height: number;
|
||||
transform: number[];
|
||||
},
|
||||
) => void;
|
||||
onImageReset: (pageIndex: number, imageId: string) => void;
|
||||
onReset: () => void;
|
||||
onDownloadJson: () => void;
|
||||
onGeneratePdf: () => void;
|
||||
onGeneratePdfForNavigation: () => Promise<void>;
|
||||
onSaveToWorkbench: () => Promise<void>;
|
||||
isSavingToWorkbench: boolean;
|
||||
onForceSingleTextElementChange: (value: boolean) => void;
|
||||
onGroupingModeChange: (value: 'auto' | 'paragraph' | 'singleLine') => void;
|
||||
onMergeGroups: (pageIndex: number, groupIds: string[]) => boolean;
|
||||
onUngroupGroup: (pageIndex: number, groupId: string) => boolean;
|
||||
onLoadFile: (file: File) => void;
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
import { useRef, useEffect, useState } from 'react'
|
||||
|
||||
interface AudioWaveformProps {
|
||||
waveformHistory: number[][]
|
||||
isActive: boolean
|
||||
}
|
||||
|
||||
export function AudioWaveform({ waveformHistory, isActive }: AudioWaveformProps) {
|
||||
const containerRef = useRef<HTMLDivElement>(null)
|
||||
const [maxColumns, setMaxColumns] = useState(150)
|
||||
|
||||
// Measure container and calculate how many columns fit
|
||||
useEffect(() => {
|
||||
if (!containerRef.current) return
|
||||
|
||||
const updateMaxColumns = () => {
|
||||
if (!containerRef.current) return
|
||||
const width = containerRef.current.offsetWidth
|
||||
// Each column is 2px + 2px gap = 4px effective width
|
||||
const cols = Math.floor(width / 4)
|
||||
setMaxColumns(Math.max(cols, 50)) // minimum 50 columns
|
||||
}
|
||||
|
||||
updateMaxColumns()
|
||||
|
||||
const resizeObserver = new ResizeObserver(updateMaxColumns)
|
||||
resizeObserver.observe(containerRef.current)
|
||||
|
||||
return () => resizeObserver.disconnect()
|
||||
}, [])
|
||||
|
||||
if (!isActive) return null
|
||||
|
||||
// Use history if available, otherwise show placeholder
|
||||
const columns = waveformHistory || []
|
||||
|
||||
// Only take exactly what fits
|
||||
const visibleColumns = columns.slice(-maxColumns)
|
||||
|
||||
// Pad with empty columns at the start if we don't have enough history
|
||||
const paddingCount = Math.max(0, maxColumns - visibleColumns.length)
|
||||
|
||||
return (
|
||||
<div ref={containerRef} className="flex items-center h-6 w-full">
|
||||
<div className="flex items-center gap-0.5 w-full">
|
||||
{/* Padding columns (empty/minimal) */}
|
||||
{Array(paddingCount).fill(0).map((_, i) => (
|
||||
<div
|
||||
key={`pad-${i}`}
|
||||
className="flex flex-col items-center justify-center gap-0.5 flex-shrink-0"
|
||||
style={{ width: '2px', minWidth: '2px' }}
|
||||
>
|
||||
{Array(10).fill(0).map((_, bandIndex) => (
|
||||
<div
|
||||
key={bandIndex}
|
||||
className="w-full bg-blue-400/20 rounded-full"
|
||||
style={{ height: '1px', minHeight: '1px' }}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
))}
|
||||
|
||||
{/* Actual waveform columns */}
|
||||
{visibleColumns.map((column, colIndex) => {
|
||||
const numBands = 10
|
||||
const step = Math.floor(column.length / numBands)
|
||||
const bands = []
|
||||
for (let i = 0; i < numBands; i++) {
|
||||
const index = Math.min(i * step, column.length - 1)
|
||||
bands.push(column[index])
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
key={`col-${colIndex}`}
|
||||
className="flex flex-col items-center justify-center gap-0.5 flex-shrink-0"
|
||||
style={{ width: '2px', minWidth: '2px' }}
|
||||
>
|
||||
{bands.map((level, bandIndex) => {
|
||||
const height = Math.max(1, level * 20)
|
||||
return (
|
||||
<div
|
||||
key={bandIndex}
|
||||
className="w-full bg-blue-400 rounded-full"
|
||||
style={{
|
||||
height: `${height}px`,
|
||||
minHeight: '1px',
|
||||
}}
|
||||
/>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import { ButtonHTMLAttributes } from 'react'
|
||||
|
||||
type ButtonVariant = 'primary' | 'secondary' | 'ghost' | 'danger' | 'accent'
|
||||
type ButtonSize = 'sm' | 'md' | 'lg' | 'icon'
|
||||
|
||||
interface ButtonProps extends ButtonHTMLAttributes<HTMLButtonElement> {
|
||||
variant?: ButtonVariant
|
||||
size?: ButtonSize
|
||||
}
|
||||
|
||||
const base =
|
||||
'inline-flex items-center justify-center rounded-lg font-medium transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-offset-2 focus-visible:ring-blue-500 focus-visible:ring-offset-slate-950 disabled:opacity-50 disabled:cursor-not-allowed'
|
||||
|
||||
const variantStyles: Record<ButtonVariant, string> = {
|
||||
primary: 'bg-blue-600 hover:bg-blue-500 text-white',
|
||||
secondary: 'bg-slate-800 hover:bg-slate-700 text-slate-100 border border-slate-700',
|
||||
ghost: 'bg-transparent hover:bg-slate-900 text-slate-300 border border-transparent',
|
||||
danger: 'bg-rose-600 hover:bg-rose-500 text-white',
|
||||
accent: 'bg-emerald-600 hover:bg-emerald-500 text-white',
|
||||
}
|
||||
|
||||
const sizeStyles: Record<ButtonSize, string> = {
|
||||
sm: 'text-xs px-3 py-1.5',
|
||||
md: 'text-sm px-4 py-2',
|
||||
lg: 'text-base px-5 py-3',
|
||||
icon: 'p-2',
|
||||
}
|
||||
|
||||
export function Button({ variant = 'primary', size = 'md', className = '', ...props }: ButtonProps) {
|
||||
const classes = [base, variantStyles[variant], sizeStyles[size], className].filter(Boolean).join(' ')
|
||||
return <button className={classes} {...props} />
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import { PropsWithChildren } from 'react'
|
||||
|
||||
interface ButtonGroupProps extends PropsWithChildren {
|
||||
align?: 'start' | 'center' | 'end'
|
||||
}
|
||||
|
||||
export function ButtonGroup({ children, align = 'start' }: ButtonGroupProps) {
|
||||
const alignment =
|
||||
align === 'center' ? 'justify-center' : align === 'end' ? 'justify-end' : 'justify-start'
|
||||
return <div className={`flex flex-wrap gap-2 ${alignment}`}>{children}</div>
|
||||
}
|
||||
@@ -0,0 +1,255 @@
|
||||
import { FormEvent, KeyboardEvent, MutableRefObject, useRef, useEffect } from 'react'
|
||||
import { DocumentState, Message, StyleProfile } from '../../types'
|
||||
import { Button } from '../ui/Button'
|
||||
import { AudioWaveform } from '../ui/AudioWaveform'
|
||||
|
||||
interface ChatPanelProps {
|
||||
onBack: () => void
|
||||
styleProfile: StyleProfile | null
|
||||
messages: Message[]
|
||||
chatEndRef: MutableRefObject<HTMLDivElement | null>
|
||||
isGenerating: boolean
|
||||
prompt: string
|
||||
onPromptChange: (value: string) => void
|
||||
onSubmit: (event: FormEvent<HTMLFormElement>) => void
|
||||
onKeyDown: (event: KeyboardEvent<HTMLTextAreaElement>) => void
|
||||
skipTemplates: boolean
|
||||
onSkipTemplatesChange: (value: boolean) => void
|
||||
onClearSession: () => void
|
||||
onToggleRecording: () => void
|
||||
onCancelRecording: () => void
|
||||
onAcceptRecording: () => void
|
||||
isRecording: boolean
|
||||
whisperStatus: string | null
|
||||
waveformHistory: number[][]
|
||||
currentDoc: DocumentState | null
|
||||
onOpenHistory: () => void
|
||||
onOpenImport: () => void
|
||||
}
|
||||
|
||||
export function ChatPanel({
|
||||
onBack,
|
||||
styleProfile,
|
||||
messages,
|
||||
chatEndRef,
|
||||
isGenerating,
|
||||
prompt,
|
||||
onPromptChange,
|
||||
onSubmit,
|
||||
onKeyDown,
|
||||
skipTemplates,
|
||||
onSkipTemplatesChange,
|
||||
onClearSession,
|
||||
onToggleRecording,
|
||||
onCancelRecording,
|
||||
onAcceptRecording,
|
||||
isRecording,
|
||||
whisperStatus,
|
||||
waveformHistory,
|
||||
currentDoc,
|
||||
onOpenHistory,
|
||||
onOpenImport,
|
||||
}: ChatPanelProps) {
|
||||
const textareaRef = useRef<HTMLTextAreaElement>(null)
|
||||
const gradientRef = useRef<HTMLDivElement>(null)
|
||||
|
||||
const updateInputGradient = () => {
|
||||
const gradient = gradientRef.current
|
||||
if (!gradient) return
|
||||
// Hide the gradient entirely to avoid lingering blur while typing.
|
||||
gradient.style.opacity = '0'
|
||||
}
|
||||
|
||||
// Auto-resize textarea and update gradient visibility
|
||||
useEffect(() => {
|
||||
const textarea = textareaRef.current
|
||||
if (textarea) {
|
||||
textarea.style.height = 'auto'
|
||||
const newHeight = Math.min(textarea.scrollHeight, 200) // Max ~8 lines for the smaller panel
|
||||
textarea.style.height = `${newHeight}px`
|
||||
}
|
||||
updateInputGradient()
|
||||
}, [prompt])
|
||||
|
||||
return (
|
||||
<div className="w-1/3 min-w-[350px] flex flex-col border-r border-slate-800 bg-slate-900 z-10">
|
||||
<div className="p-4 border-b border-slate-800 flex items-center justify-between gap-3">
|
||||
<Button variant="ghost" size="sm" onClick={onBack}>
|
||||
← Back
|
||||
</Button>
|
||||
<div className="flex items-center gap-2">
|
||||
<Button variant="secondary" size="sm" onClick={onOpenHistory}>
|
||||
History & Layouts
|
||||
</Button>
|
||||
<Button variant="accent" size="sm" onClick={onOpenImport}>
|
||||
Import Layout (PDF)
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
{styleProfile && (
|
||||
<div className="px-4 py-2 border-b border-slate-800 flex items-center gap-3 text-xs text-slate-400">
|
||||
<span className="px-2 py-1 rounded-full bg-slate-800 border border-slate-700">
|
||||
{styleProfile.layout_preference || 'clean'}
|
||||
</span>
|
||||
<span className="px-2 py-1 rounded-full bg-slate-800 border border-slate-700">
|
||||
{styleProfile.font_preference || 'font'}
|
||||
</span>
|
||||
<span className="px-2 py-1 rounded-full bg-slate-800 border border-slate-700">
|
||||
{styleProfile.tone || 'tone'}
|
||||
</span>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="flex-1 overflow-y-auto p-4 space-y-6 relative">
|
||||
{messages.map((msg, idx) => (
|
||||
<div key={idx} className={`flex ${msg.role === 'user' ? 'justify-end' : 'justify-start'}`}>
|
||||
<div
|
||||
className={`max-w-[85%] rounded-2xl p-4 ${
|
||||
msg.role === 'user'
|
||||
? 'bg-blue-600 text-white rounded-br-none'
|
||||
: 'bg-slate-800 text-slate-200 rounded-bl-none border border-slate-700'
|
||||
}`}
|
||||
>
|
||||
<p className="whitespace-pre-wrap text-sm leading-relaxed" style={{ overflowWrap: 'anywhere', wordBreak: 'break-word' }}>{msg.text}</p>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
{isGenerating && (
|
||||
<div className="flex justify-start">
|
||||
<div className="bg-slate-800 rounded-2xl p-4 rounded-bl-none border border-slate-700">
|
||||
<div className="flex space-x-2">
|
||||
<div className="w-2 h-2 bg-slate-500 rounded-full animate-bounce"></div>
|
||||
<div className="w-2 h-2 bg-slate-500 rounded-full animate-bounce delay-100"></div>
|
||||
<div className="w-2 h-2 bg-slate-500 rounded-full animate-bounce delay-200"></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
{!messages.length && (
|
||||
<div className="text-xs text-slate-500">
|
||||
Ask for revisions, upload layouts, or describe the style you want. I'll keep the latest
|
||||
context.
|
||||
</div>
|
||||
)}
|
||||
<div ref={chatEndRef} />
|
||||
</div>
|
||||
|
||||
<div className="p-4 bg-slate-900 border-t border-slate-800 space-y-3">
|
||||
<div className="flex items-center justify-between text-xs text-slate-400">
|
||||
<label className="flex items-center gap-2 cursor-pointer select-none">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={skipTemplates}
|
||||
onChange={(e) => onSkipTemplatesChange(e.target.checked)}
|
||||
className="h-4 w-4 rounded border-slate-600 bg-slate-800 accent-blue-500"
|
||||
/>
|
||||
<span>Don't reuse saved layouts/templates</span>
|
||||
</label>
|
||||
<Button variant="secondary" size="sm" onClick={onClearSession}>
|
||||
Clear
|
||||
</Button>
|
||||
</div>
|
||||
<form onSubmit={onSubmit} className="relative">
|
||||
<div className="w-full bg-slate-800 rounded-xl border border-slate-700 focus-within:ring-2 focus-within:ring-blue-500/50">
|
||||
{/* Content area */}
|
||||
<div className="px-3 pt-3 pb-1">
|
||||
{isRecording ? (
|
||||
<div className="min-h-[20px]">
|
||||
<AudioWaveform waveformHistory={waveformHistory} isActive={isRecording} />
|
||||
</div>
|
||||
) : (
|
||||
<textarea
|
||||
ref={textareaRef}
|
||||
value={prompt}
|
||||
onChange={(e) => onPromptChange(e.target.value)}
|
||||
onKeyDown={onKeyDown}
|
||||
onScroll={updateInputGradient}
|
||||
placeholder={currentDoc ? 'Modify the document...' : 'Describe what to build...'}
|
||||
className="w-full bg-transparent text-white focus:outline-none resize-none overflow-y-auto text-sm"
|
||||
style={{ maxHeight: '200px' }}
|
||||
rows={1}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
<div
|
||||
ref={gradientRef}
|
||||
className="pointer-events-none absolute inset-x-0 bottom-0 h-6 bg-gradient-to-t from-slate-900/90 to-transparent transition-opacity duration-200"
|
||||
style={{ opacity: 0 }}
|
||||
/>
|
||||
<div className="mt-2 flex items-center justify-end gap-2">
|
||||
{isRecording ? (
|
||||
<>
|
||||
<button
|
||||
type="button"
|
||||
onClick={onCancelRecording}
|
||||
className="p-2 rounded-lg text-slate-400 hover:text-white hover:bg-slate-700 transition-colors"
|
||||
title="Cancel recording"
|
||||
>
|
||||
<svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
|
||||
</svg>
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={(e) => {
|
||||
e.preventDefault()
|
||||
e.stopPropagation()
|
||||
onAcceptRecording()
|
||||
}}
|
||||
className="p-2 rounded-full bg-blue-600 text-white hover:bg-blue-700 transition-colors"
|
||||
title="Accept and transcribe"
|
||||
>
|
||||
<svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M5 13l4 4L19 7" />
|
||||
</svg>
|
||||
</button>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<button
|
||||
type="button"
|
||||
onClick={onToggleRecording}
|
||||
className="p-2 rounded-lg text-slate-400 hover:text-white hover:bg-slate-700 transition-colors"
|
||||
title="Start voice input"
|
||||
>
|
||||
<svg className="w-4 h-4" fill="currentColor" viewBox="0 0 24 24">
|
||||
<path d="M12 14c1.66 0 3-1.34 3-3V5c0-1.66-1.34-3-3-3S9 3.34 9 5v6c0 1.66 1.34 3 3 3z" />
|
||||
<path d="M17 11c0 2.76-2.24 5-5 5s-5-2.24-5-5H5c0 3.53 2.61 6.43 6 6.92V21h2v-3.08c3.39-.49 6-3.39 6-6.92h-2z" />
|
||||
</svg>
|
||||
</button>
|
||||
<button
|
||||
type="submit"
|
||||
disabled={isGenerating}
|
||||
className="p-2 rounded-full bg-blue-600 text-white hover:bg-blue-700 disabled:opacity-50 disabled:cursor-not-allowed transition-colors"
|
||||
title="Send"
|
||||
>
|
||||
<svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M5 10l7-7m0 0l7 7m-7-7v18" />
|
||||
</svg>
|
||||
</button>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</form>
|
||||
<div className="mt-2 flex items-center gap-2 text-xs text-slate-400">
|
||||
<div className="relative group px-3 py-2 rounded-full bg-slate-800 border border-slate-700 font-medium text-slate-300 cursor-not-allowed select-none">
|
||||
<span className="flex items-center gap-2">
|
||||
Document type (auto)
|
||||
<svg className="w-3.5 h-3.5 text-slate-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M19 15l-7-7-7 7" />
|
||||
</svg>
|
||||
</span>
|
||||
<span className="pointer-events-none absolute -top-9 left-1/2 -translate-x-1/2 whitespace-nowrap rounded-md bg-slate-800 px-3 py-1 text-[11px] text-slate-200 border border-slate-700 opacity-0 transition-opacity duration-150 group-hover:opacity-100">
|
||||
We’ll pick the best document type for your prompt automatically using AI.
|
||||
</span>
|
||||
</div>
|
||||
<div className="px-3 py-2 rounded-full bg-slate-800 border border-slate-700 font-medium text-slate-300 cursor-not-allowed select-none">
|
||||
GPT 5.1
|
||||
</div>
|
||||
</div>
|
||||
{whisperStatus && <div className="text-[11px] text-slate-400 mt-1">{whisperStatus}</div>}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
import { VersionEntry } from '../../types'
|
||||
import { Button } from '../ui/Button'
|
||||
|
||||
interface HistoryPanelProps {
|
||||
versions: VersionEntry[]
|
||||
selectedVersionId: string | null
|
||||
onSelectVersion: (id: string) => void
|
||||
onClose: () => void
|
||||
onRefresh: () => void
|
||||
}
|
||||
|
||||
export function HistoryPanel({ versions, selectedVersionId, onSelectVersion, onClose, onRefresh }: HistoryPanelProps) {
|
||||
const uniqueTypes = Array.from(new Set(versions.map((v) => v.documentType)))
|
||||
|
||||
return (
|
||||
<div className="absolute inset-y-0 right-0 w-96 bg-slate-900 border-l border-slate-800 shadow-2xl flex flex-col z-20">
|
||||
<div className="p-4 border-b border-slate-800 flex items-center justify-between">
|
||||
<div>
|
||||
<div className="text-sm text-slate-400">Versions & Layouts</div>
|
||||
<div className="text-xs text-slate-500">Most recent first</div>
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<Button variant="ghost" size="sm" onClick={onRefresh}>
|
||||
Refresh
|
||||
</Button>
|
||||
<Button variant="secondary" size="sm" onClick={onClose}>
|
||||
Close
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
<div className="p-4 space-y-3 overflow-y-auto flex-1">
|
||||
<div className="text-xs uppercase text-slate-500">Saved layouts</div>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
{uniqueTypes.length === 0 && <span className="text-xs text-slate-500">None yet</span>}
|
||||
{uniqueTypes.map((type) => (
|
||||
<span
|
||||
key={type}
|
||||
className="px-2 py-1 text-xs bg-slate-800 rounded border border-slate-700 text-slate-300"
|
||||
>
|
||||
{type}
|
||||
</span>
|
||||
))}
|
||||
</div>
|
||||
|
||||
<div className="flex items-center justify-between pt-2">
|
||||
<span className="text-xs uppercase text-slate-500">History</span>
|
||||
<span className="text-[11px] text-slate-500">{versions.length} versions</span>
|
||||
</div>
|
||||
|
||||
{versions.length === 0 && <p className="text-sm text-slate-500">No versions yet</p>}
|
||||
{versions.slice(0, 30).map((version) => (
|
||||
<button
|
||||
key={version.id}
|
||||
onClick={() => onSelectVersion(version.id)}
|
||||
className={`w-full text-left p-3 rounded-xl border transition-colors ${
|
||||
selectedVersionId === version.id
|
||||
? 'border-blue-500 bg-blue-500/10'
|
||||
: 'border-slate-800 bg-slate-900'
|
||||
} hover:border-blue-500`}
|
||||
>
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="text-xs uppercase text-slate-400">{version.documentType}</div>
|
||||
<div className="text-[10px] text-slate-500">
|
||||
{version.createdAt ? new Date(version.createdAt).toLocaleString() : 'recent'}
|
||||
</div>
|
||||
</div>
|
||||
<div className="text-sm text-slate-100 line-clamp-2">{version.prompt || 'Generated document'}</div>
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
import { useMemo } from 'react'
|
||||
import { DocumentState } from '../../types'
|
||||
import PdfThumbnailViewer from '../pdfTextEditor/PdfThumbnailViewer'
|
||||
// import PdfTextEditorFull from '../pdfTextEditor/PdfTextEditorFull'
|
||||
|
||||
interface PreviewPanelProps {
|
||||
currentDoc: DocumentState | null
|
||||
isGenerating: boolean
|
||||
isLivePreviewing: boolean
|
||||
skipTemplates: boolean
|
||||
onPdfReplaced: (pdfUrl: string) => void
|
||||
}
|
||||
|
||||
export function PreviewPanel({
|
||||
currentDoc,
|
||||
isGenerating,
|
||||
isLivePreviewing,
|
||||
skipTemplates,
|
||||
onPdfReplaced,
|
||||
}: PreviewPanelProps) {
|
||||
// Keep around while editor is hidden so we don't lose the apply handler
|
||||
void onPdfReplaced
|
||||
|
||||
const hasDocumentStarted = useMemo(() => {
|
||||
const latex = currentDoc?.latex || ''
|
||||
return /\\begin\s*{document}/i.test(latex)
|
||||
}, [currentDoc?.latex])
|
||||
|
||||
const canRenderPdf = useMemo(() => {
|
||||
if (!currentDoc?.pdfUrl) return false
|
||||
// While live preview is streaming, wait until the document body starts
|
||||
if (isLivePreviewing) return hasDocumentStarted
|
||||
return true
|
||||
}, [currentDoc?.pdfUrl, hasDocumentStarted, isLivePreviewing])
|
||||
|
||||
const heading = useMemo(() => {
|
||||
if (isGenerating) return 'Generating ...'
|
||||
if (isLivePreviewing) return 'Live updating preview'
|
||||
if (currentDoc?.pdfUrl) return 'Preview ready'
|
||||
return 'PDF preview'
|
||||
}, [currentDoc?.pdfUrl, isGenerating, isLivePreviewing])
|
||||
|
||||
return (
|
||||
<div className="flex-1 flex flex-col bg-slate-950">
|
||||
<div className="h-14 bg-slate-900 border-b border-slate-800 flex items-center justify-between px-6">
|
||||
<div className="flex items-center space-x-4">
|
||||
<h2 className="font-semibold text-slate-200">{heading}</h2>
|
||||
{currentDoc && (
|
||||
<span className="text-xs text-slate-500 px-2 py-1 bg-slate-800 rounded border border-slate-700">
|
||||
{currentDoc.documentType}
|
||||
</span>
|
||||
)}
|
||||
{skipTemplates && (
|
||||
<span className="text-[11px] text-amber-200 px-2 py-1 rounded bg-amber-500/20 border border-amber-600">
|
||||
Skipping saved layouts
|
||||
</span>
|
||||
)}
|
||||
{isLivePreviewing && (
|
||||
<span className="text-[11px] text-blue-200 px-2 py-1 rounded bg-blue-600/20 border border-blue-500 animate-pulse">
|
||||
Rendering live
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<div className="flex items-center gap-3">
|
||||
{currentDoc?.pdfUrl && (
|
||||
<a
|
||||
href={currentDoc.pdfUrl}
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
className="px-4 py-2 bg-blue-600 hover:bg-blue-500 text-white text-sm font-medium rounded-lg flex items-center"
|
||||
>
|
||||
Download PDF
|
||||
</a>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex-1 overflow-hidden relative">
|
||||
{canRenderPdf ? (
|
||||
<div className="h-full">
|
||||
{/* Temporarily hiding the interactive editor; restore when edit mode returns */}
|
||||
{/*
|
||||
<PdfTextEditorFull
|
||||
pdfUrl={currentDoc?.pdfUrl || ''}
|
||||
onApply={(nextUrl: string) => {
|
||||
onPdfReplaced(nextUrl)
|
||||
}}
|
||||
/>
|
||||
*/}
|
||||
<PdfThumbnailViewer pdfUrl={currentDoc?.pdfUrl || ''} isLivePreviewing={isLivePreviewing} />
|
||||
</div>
|
||||
) : (
|
||||
<div className="flex-1 bg-slate-900 overflow-auto p-6">
|
||||
{isGenerating || isLivePreviewing ? (
|
||||
<div className="relative w-full h-full overflow-auto bg-slate-900 rounded-lg border border-slate-800 shadow-inner">
|
||||
<div className="flex justify-center py-8">
|
||||
<div
|
||||
className="relative rounded-2xl border border-slate-300/60 bg-white shadow-2xl overflow-hidden"
|
||||
style={{
|
||||
width: '860px',
|
||||
maxWidth: '90vw',
|
||||
aspectRatio: '1 / 1.414',
|
||||
}}
|
||||
>
|
||||
<div className="h-12 bg-slate-100 border-b border-slate-200 animate-pulse" />
|
||||
<div className="p-8 space-y-4">
|
||||
{[1, 2, 3, 4].map((line) => (
|
||||
<div
|
||||
key={`line-top-${line}`}
|
||||
className="h-4 rounded-full bg-slate-200 animate-pulse"
|
||||
style={{ width: `${78 - line * 10}%` }}
|
||||
/>
|
||||
))}
|
||||
<div className="h-40 rounded-xl bg-slate-100 border border-slate-200 animate-pulse" />
|
||||
{[5, 6, 7].map((line) => (
|
||||
<div
|
||||
key={`line-bottom-${line}`}
|
||||
className="h-4 rounded-full bg-slate-200 animate-pulse"
|
||||
style={{ width: `${70 - (line - 5) * 8}%` }}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<div className="flex items-center justify-center text-sm text-slate-500 h-full">
|
||||
Generate a PDF to view the preview.
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,757 @@
|
||||
import { FormEvent, KeyboardEvent, useEffect, useState } from 'react'
|
||||
import { DocumentState } from '../../types'
|
||||
import PdfThumbnailViewer from '../pdfTextEditor/PdfThumbnailViewer'
|
||||
|
||||
interface WorkspaceViewProps {
|
||||
isGenerating: boolean
|
||||
isLivePreviewing: boolean
|
||||
isStageLoading: boolean
|
||||
prompt: string
|
||||
onPromptChange: (value: string) => void
|
||||
onChatSubmit: (event: FormEvent<HTMLFormElement>) => void
|
||||
onKeyDown: (event: KeyboardEvent<HTMLTextAreaElement>) => void
|
||||
currentDoc: DocumentState | null
|
||||
onBack: () => void
|
||||
stage: 'outline' | 'text' | 'styling' | 'review'
|
||||
outlineRows: { section: string; details: string }[]
|
||||
outlineSections: string[]
|
||||
excludedFields: string[]
|
||||
outlineConstraints: { tone: string; audience: string; pageCount: number }
|
||||
draftRows: { label: string; value: string }[]
|
||||
setOutlineRows: (rows: { section: string; details: string }[]) => void
|
||||
setOutlineSections: (next: string[]) => void
|
||||
setExcludedFields: (next: string[]) => void
|
||||
setOutlineConstraints: (next: { tone: string; audience: string; pageCount: number }) => void
|
||||
setDraftRows: (rows: { label: string; value: string }[]) => void
|
||||
docTypes: string[]
|
||||
templateCounts: Record<string, number>
|
||||
selectedDocType: string
|
||||
selectedTemplateId: string
|
||||
templatesForSelected: string[]
|
||||
isTemplateLoading: boolean
|
||||
onSelectTemplate: (docType: string, templateId: string) => void
|
||||
templateThumbnailUrl: (docType: string, templateId: string) => string
|
||||
approveOutline: () => void
|
||||
onAiOutline: () => void
|
||||
approveDraft: () => void
|
||||
saveAndReview: () => void
|
||||
styleDraft: { layout_preference: string; font_preference: string; color_accent: string }
|
||||
setStyleDraft: (next: { layout_preference: string; font_preference: string; color_accent: string }) => void
|
||||
applyStyleAndRegenerate: () => void
|
||||
onAddPromptInfo: (value: string) => void
|
||||
onStageSelect: (stage: 'outline' | 'text' | 'styling' | 'review') => void
|
||||
imagePlaceholdersCount: number
|
||||
isAssetUploading: boolean
|
||||
assetError: string | null
|
||||
onAddPlaceholderImage: (file: File) => void
|
||||
onRemovePlaceholders: () => void
|
||||
onOpenImportTemplate: () => void
|
||||
}
|
||||
|
||||
export function WorkspaceView({
|
||||
isGenerating,
|
||||
isLivePreviewing,
|
||||
isStageLoading,
|
||||
prompt,
|
||||
onPromptChange,
|
||||
onChatSubmit,
|
||||
onKeyDown,
|
||||
currentDoc,
|
||||
onBack,
|
||||
stage,
|
||||
outlineRows,
|
||||
outlineSections,
|
||||
excludedFields,
|
||||
outlineConstraints,
|
||||
draftRows,
|
||||
setOutlineRows,
|
||||
setOutlineSections,
|
||||
setExcludedFields,
|
||||
setOutlineConstraints,
|
||||
setDraftRows,
|
||||
docTypes,
|
||||
templateCounts,
|
||||
selectedDocType,
|
||||
selectedTemplateId,
|
||||
templatesForSelected,
|
||||
isTemplateLoading,
|
||||
onSelectTemplate,
|
||||
templateThumbnailUrl,
|
||||
approveOutline,
|
||||
onAiOutline,
|
||||
approveDraft,
|
||||
saveAndReview,
|
||||
styleDraft,
|
||||
setStyleDraft,
|
||||
applyStyleAndRegenerate,
|
||||
onAddPromptInfo,
|
||||
onStageSelect,
|
||||
imagePlaceholdersCount,
|
||||
isAssetUploading,
|
||||
assetError,
|
||||
onAddPlaceholderImage,
|
||||
onRemovePlaceholders,
|
||||
onOpenImportTemplate,
|
||||
}: WorkspaceViewProps) {
|
||||
const [isTemplatePickerOpen, setIsTemplatePickerOpen] = useState(false)
|
||||
const [isDataPanelOpen, setIsDataPanelOpen] = useState(false)
|
||||
const [promptAddon, setPromptAddon] = useState('')
|
||||
const autoSize = (event: React.FormEvent<HTMLTextAreaElement>) => {
|
||||
const target = event.currentTarget
|
||||
target.style.height = 'auto'
|
||||
target.style.height = `${target.scrollHeight}px`
|
||||
}
|
||||
useEffect(() => {
|
||||
const nodes = document.querySelectorAll('textarea[data-autosize="true"]')
|
||||
nodes.forEach((node) => {
|
||||
const area = node as HTMLTextAreaElement
|
||||
area.style.height = 'auto'
|
||||
area.style.height = `${area.scrollHeight}px`
|
||||
})
|
||||
}, [outlineRows, draftRows, stage])
|
||||
|
||||
const formatDocLabel = (value: string) => {
|
||||
return value
|
||||
.split('_')
|
||||
.map((part) => part.charAt(0).toUpperCase() + part.slice(1))
|
||||
.join(' ')
|
||||
}
|
||||
|
||||
const docLabel = currentDoc?.documentType ? formatDocLabel(currentDoc.documentType) : 'Document'
|
||||
|
||||
const renderStageContent = () => {
|
||||
if (stage === 'outline') {
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<div>
|
||||
<h2 className="text-xl font-semibold text-slate-900">{docLabel} Inputs</h2>
|
||||
<p className="text-sm text-slate-500">Fill in the data you want the AI to use.</p>
|
||||
</div>
|
||||
<div className="rounded-2xl border border-slate-200 bg-white p-4 space-y-3">
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<div className="text-xs font-semibold uppercase tracking-wide text-slate-500">
|
||||
Selected Template
|
||||
</div>
|
||||
<div className="text-sm text-slate-700">
|
||||
{formatDocLabel(selectedDocType)} · {formatDocLabel(selectedTemplateId)}
|
||||
</div>
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
className="text-xs font-semibold text-blue-600"
|
||||
onClick={() => setIsTemplatePickerOpen((prev) => !prev)}
|
||||
>
|
||||
{isTemplatePickerOpen ? 'Hide' : 'Edit'}
|
||||
</button>
|
||||
</div>
|
||||
{isTemplatePickerOpen && (
|
||||
<div className="space-y-4 pt-2">
|
||||
<div className="space-y-2">
|
||||
<label className="text-xs font-semibold uppercase tracking-wide text-slate-500">
|
||||
Document Type
|
||||
</label>
|
||||
<select
|
||||
className="w-full rounded-lg border border-slate-200 bg-white px-3 py-2 text-sm text-slate-700"
|
||||
value={selectedDocType}
|
||||
onChange={(event) => onSelectTemplate(event.target.value, selectedTemplateId)}
|
||||
>
|
||||
{docTypes.map((docType) => (
|
||||
<option key={docType} value={docType}>
|
||||
{formatDocLabel(docType)} ({templateCounts[docType] ?? 1})
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<label className="text-xs font-semibold uppercase tracking-wide text-slate-500">
|
||||
Template Style
|
||||
</label>
|
||||
{isTemplateLoading ? (
|
||||
<div className="text-sm text-slate-400">Loading templates...</div>
|
||||
) : (
|
||||
<div className="grid gap-3 sm:grid-cols-2">
|
||||
{templatesForSelected.map((templateId) => (
|
||||
<button
|
||||
type="button"
|
||||
key={templateId}
|
||||
onClick={() => onSelectTemplate(selectedDocType, templateId)}
|
||||
className={`rounded-xl border text-left ${
|
||||
selectedTemplateId === templateId
|
||||
? 'border-blue-500 ring-2 ring-blue-200'
|
||||
: 'border-slate-200'
|
||||
}`}
|
||||
>
|
||||
<div
|
||||
className="w-full rounded-t-xl bg-slate-50 overflow-hidden h-36"
|
||||
style={{ aspectRatio: '210 / 297' }}
|
||||
>
|
||||
<img
|
||||
src={templateThumbnailUrl(selectedDocType, templateId)}
|
||||
alt={`${templateId} template`}
|
||||
className="h-full w-full object-contain"
|
||||
/>
|
||||
</div>
|
||||
<div className="px-3 py-2 text-sm text-slate-700">
|
||||
{formatDocLabel(templateId)}
|
||||
</div>
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<div className="rounded-2xl border border-slate-200 bg-white">
|
||||
<div className="flex items-center justify-between px-4 py-3">
|
||||
<div>
|
||||
<div className="text-xs font-semibold uppercase tracking-wider text-slate-500">Data to use</div>
|
||||
<div className="text-xs text-slate-400">
|
||||
{outlineRows.filter((row) => row.details.trim()).length} filled ·{' '}
|
||||
{outlineRows.filter((row) => !row.details.trim()).length} empty
|
||||
</div>
|
||||
{outlineRows.filter((row) => !row.details.trim()).length > outlineRows.length / 2 && (
|
||||
<div className="text-xs text-amber-500 mt-1">
|
||||
Add a bit more data for better results.
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
className="text-xs font-semibold text-blue-600"
|
||||
onClick={() => setIsDataPanelOpen((prev) => !prev)}
|
||||
>
|
||||
{isDataPanelOpen ? 'Hide' : 'Show'}
|
||||
</button>
|
||||
</div>
|
||||
<div className="border-t border-slate-200 px-6 py-4 space-y-3">
|
||||
<div className="text-xs font-semibold uppercase tracking-wider text-slate-500">
|
||||
Add info from prompt
|
||||
</div>
|
||||
<div className="flex flex-col gap-2 sm:flex-row">
|
||||
<input
|
||||
className="flex-1 rounded-lg border border-slate-200 bg-white px-3 py-2 text-sm text-slate-700"
|
||||
placeholder="Add more details (e.g., biller address, invoice number)..."
|
||||
value={promptAddon}
|
||||
onChange={(event) => setPromptAddon(event.target.value)}
|
||||
onKeyDown={(event) => {
|
||||
if (event.key === 'Enter') {
|
||||
event.preventDefault()
|
||||
onAddPromptInfo(promptAddon)
|
||||
setPromptAddon('')
|
||||
}
|
||||
}}
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
className="rounded-lg bg-slate-900 px-4 py-2 text-sm font-semibold text-white"
|
||||
onClick={() => {
|
||||
onAddPromptInfo(promptAddon)
|
||||
setPromptAddon('')
|
||||
}}
|
||||
>
|
||||
Add
|
||||
</button>
|
||||
</div>
|
||||
<div className="text-xs text-slate-400">
|
||||
This appends to the original prompt and re-runs auto-fill.
|
||||
</div>
|
||||
</div>
|
||||
{isDataPanelOpen && (
|
||||
<div className="border-t border-slate-200">
|
||||
<div className="grid grid-cols-[1.2fr,2fr,auto] bg-slate-50 px-6 py-3 text-xs font-semibold text-slate-500 uppercase tracking-wider">
|
||||
<div>Data name</div>
|
||||
<div>Value</div>
|
||||
<div />
|
||||
</div>
|
||||
{outlineRows.map((row, idx) => (
|
||||
<div
|
||||
key={`outline-${idx}`}
|
||||
className="grid grid-cols-[1.2fr,2fr,auto] px-6 py-3 border-t border-slate-100 gap-4 items-start"
|
||||
>
|
||||
<input
|
||||
className="text-sm text-slate-800 bg-transparent focus:outline-none"
|
||||
value={row.section}
|
||||
onChange={(event) => {
|
||||
const next = [...outlineRows]
|
||||
next[idx] = { ...row, section: event.target.value }
|
||||
setOutlineRows(next)
|
||||
}}
|
||||
/>
|
||||
<textarea
|
||||
rows={1}
|
||||
className="min-h-[56px] w-full resize-none overflow-hidden text-sm text-slate-500 bg-transparent focus:outline-none"
|
||||
value={row.details}
|
||||
onInput={autoSize}
|
||||
data-autosize="true"
|
||||
onChange={(event) => {
|
||||
const next = [...outlineRows]
|
||||
next[idx] = { ...row, details: event.target.value }
|
||||
setOutlineRows(next)
|
||||
}}
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
className="text-xs text-slate-400 hover:text-rose-500"
|
||||
onClick={() => {
|
||||
const next = outlineRows.filter((_, rowIndex) => rowIndex !== idx)
|
||||
const label = row.section.trim()
|
||||
if (label) {
|
||||
setExcludedFields([...excludedFields, label])
|
||||
}
|
||||
setOutlineRows(next)
|
||||
}}
|
||||
>
|
||||
Remove
|
||||
</button>
|
||||
</div>
|
||||
))}
|
||||
<div className="flex items-center justify-between px-6 py-3">
|
||||
<button
|
||||
type="button"
|
||||
className="text-xs font-semibold text-blue-600"
|
||||
onClick={() => setOutlineRows([...outlineRows, { section: 'New data', details: '' }])}
|
||||
>
|
||||
Add data
|
||||
</button>
|
||||
{excludedFields.length > 0 && (
|
||||
<div className="text-xs text-slate-400">
|
||||
Excluded: {excludedFields.join(', ')}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<div className="rounded-2xl border border-slate-200 bg-white p-4 space-y-4">
|
||||
<div className="text-xs font-semibold uppercase tracking-wider text-slate-500">
|
||||
AI Constraints
|
||||
</div>
|
||||
<div className="grid gap-4 sm:grid-cols-2">
|
||||
<div className="space-y-2">
|
||||
<label className="text-xs font-semibold uppercase tracking-wide text-slate-500">Tone</label>
|
||||
<select
|
||||
className="w-full rounded-lg border border-slate-200 bg-white px-3 py-2 text-sm text-slate-700"
|
||||
value={outlineConstraints.tone}
|
||||
onChange={(event) =>
|
||||
setOutlineConstraints({ ...outlineConstraints, tone: event.target.value })
|
||||
}
|
||||
>
|
||||
{['Professional', 'Formal', 'Friendly', 'Neutral', 'Academic', 'Technical', 'Narrative', 'Direct', 'Informative'].map((tone) => (
|
||||
<option key={tone} value={tone}>
|
||||
{tone}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<label className="text-xs font-semibold uppercase tracking-wide text-slate-500">Audience</label>
|
||||
<input
|
||||
className="w-full rounded-lg border border-slate-200 bg-white px-3 py-2 text-sm text-slate-700"
|
||||
value={outlineConstraints.audience}
|
||||
onChange={(event) =>
|
||||
setOutlineConstraints({ ...outlineConstraints, audience: event.target.value })
|
||||
}
|
||||
placeholder="Audience"
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<label className="text-xs font-semibold uppercase tracking-wide text-slate-500">Pages</label>
|
||||
<input
|
||||
type="number"
|
||||
min={1}
|
||||
className="w-full rounded-lg border border-slate-200 bg-white px-3 py-2 text-sm text-slate-700"
|
||||
value={outlineConstraints.pageCount}
|
||||
onChange={(event) =>
|
||||
setOutlineConstraints({
|
||||
...outlineConstraints,
|
||||
pageCount: Math.max(1, Number(event.target.value || 1)),
|
||||
})
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div className="rounded-2xl border border-slate-200 bg-white p-4 space-y-3">
|
||||
<div className="text-xs font-semibold uppercase tracking-wider text-slate-500">
|
||||
Sections
|
||||
</div>
|
||||
<div className="space-y-3">
|
||||
{outlineSections.map((section, idx) => (
|
||||
<div key={`section-${idx}`} className="flex items-center gap-3">
|
||||
<input
|
||||
className="flex-1 rounded-lg border border-slate-200 bg-white px-3 py-2 text-sm text-slate-700"
|
||||
value={section}
|
||||
onChange={(event) => {
|
||||
const next = [...outlineSections]
|
||||
next[idx] = event.target.value
|
||||
setOutlineSections(next)
|
||||
}}
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
className="text-xs text-slate-400 hover:text-rose-500"
|
||||
onClick={() => {
|
||||
const next = outlineSections.filter((_, rowIndex) => rowIndex !== idx)
|
||||
setOutlineSections(next)
|
||||
}}
|
||||
>
|
||||
Remove
|
||||
</button>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
className="text-xs font-semibold text-blue-600"
|
||||
onClick={() => setOutlineSections([...outlineSections, 'New section'])}
|
||||
>
|
||||
Add section
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
if (stage === 'text') {
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<div>
|
||||
<h2 className="text-xl font-semibold text-slate-900">{docLabel} Text</h2>
|
||||
<p className="text-sm text-slate-500">Edit each section draft. Polishing happens after approval.</p>
|
||||
</div>
|
||||
<div className="border border-slate-200 rounded-2xl overflow-hidden">
|
||||
<div className="grid grid-cols-[1.2fr,2fr] bg-slate-50 px-6 py-3 text-xs font-semibold text-slate-500 uppercase tracking-wider">
|
||||
<div>Section</div>
|
||||
<div>Content</div>
|
||||
</div>
|
||||
{draftRows.map((row, idx) => (
|
||||
<div
|
||||
key={`draft-${idx}`}
|
||||
className="grid grid-cols-[1.2fr,2fr] px-6 py-3 border-t border-slate-100 gap-4"
|
||||
>
|
||||
<input
|
||||
className="text-sm text-slate-800 bg-transparent focus:outline-none"
|
||||
value={row.label}
|
||||
onChange={(event) => {
|
||||
const next = [...draftRows]
|
||||
next[idx] = { ...row, label: event.target.value }
|
||||
setDraftRows(next)
|
||||
}}
|
||||
/>
|
||||
<textarea
|
||||
rows={1}
|
||||
className="min-h-[120px] w-full resize-none overflow-hidden text-sm text-slate-600 bg-transparent focus:outline-none"
|
||||
value={row.value}
|
||||
onInput={autoSize}
|
||||
data-autosize="true"
|
||||
onChange={(event) => {
|
||||
const next = [...draftRows]
|
||||
next[idx] = { ...row, value: event.target.value }
|
||||
setDraftRows(next)
|
||||
}}
|
||||
placeholder={isStageLoading ? 'Generating draft...' : 'Add content...'}
|
||||
/>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
if (stage === 'styling') {
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<div>
|
||||
<h2 className="text-xl font-semibold text-slate-900">{docLabel} Review</h2>
|
||||
<p className="text-sm text-slate-500">Check the generated preview before exporting.</p>
|
||||
</div>
|
||||
<div className="grid gap-4 lg:grid-cols-[1.1fr,2fr]">
|
||||
<div className="space-y-4 rounded-2xl border border-slate-200 bg-white p-4">
|
||||
<div className="space-y-2">
|
||||
<label className="text-xs font-semibold uppercase tracking-wide text-slate-500">Font</label>
|
||||
<select
|
||||
className="w-full rounded-lg border border-slate-200 bg-white px-3 py-2 text-sm text-slate-700"
|
||||
value={styleDraft.font_preference}
|
||||
onChange={(event) =>
|
||||
setStyleDraft({ ...styleDraft, font_preference: event.target.value })
|
||||
}
|
||||
>
|
||||
{['Serif', 'Sans', 'Modern', 'Classic', 'Minimal'].map((font) => (
|
||||
<option key={font} value={font}>
|
||||
{font}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<label className="text-xs font-semibold uppercase tracking-wide text-slate-500">Layout</label>
|
||||
<select
|
||||
className="w-full rounded-lg border border-slate-200 bg-white px-3 py-2 text-sm text-slate-700"
|
||||
value={styleDraft.layout_preference}
|
||||
onChange={(event) =>
|
||||
setStyleDraft({ ...styleDraft, layout_preference: event.target.value })
|
||||
}
|
||||
>
|
||||
{['Compact', 'Balanced', 'Spacious', 'Grid', 'Editorial'].map((layout) => (
|
||||
<option key={layout} value={layout}>
|
||||
{layout}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<label className="text-xs font-semibold uppercase tracking-wide text-slate-500">Accent</label>
|
||||
<input
|
||||
type="color"
|
||||
className="h-10 w-full rounded-lg border border-slate-200 bg-white px-2"
|
||||
value={styleDraft.color_accent}
|
||||
onChange={(event) =>
|
||||
setStyleDraft({ ...styleDraft, color_accent: event.target.value })
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
className="w-full rounded-lg bg-slate-900 px-4 py-2 text-sm font-semibold text-white disabled:opacity-50"
|
||||
onClick={applyStyleAndRegenerate}
|
||||
disabled={isGenerating || isStageLoading}
|
||||
>
|
||||
Apply Style & Regenerate
|
||||
</button>
|
||||
</div>
|
||||
<div className="border border-slate-200 rounded-2xl bg-white">
|
||||
{currentDoc?.pdfUrl ? (
|
||||
<div className="p-4">
|
||||
<PdfThumbnailViewer pdfUrl={currentDoc.pdfUrl} isLivePreviewing={isLivePreviewing} />
|
||||
</div>
|
||||
) : (
|
||||
<div className="p-12 text-sm text-slate-500 text-center">
|
||||
{isStageLoading || isGenerating ? 'Generating preview...' : 'Preview will appear here.'}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<div>
|
||||
<h2 className="text-xl font-semibold text-slate-900">{docLabel} Export</h2>
|
||||
<p className="text-sm text-slate-500">Download or export your final PDF.</p>
|
||||
</div>
|
||||
<div className="border border-slate-200 rounded-2xl bg-white">
|
||||
{currentDoc?.pdfUrl ? (
|
||||
<div className="p-4">
|
||||
<PdfThumbnailViewer pdfUrl={currentDoc.pdfUrl} isLivePreviewing={isLivePreviewing} />
|
||||
</div>
|
||||
) : (
|
||||
<div className="p-12 text-sm text-slate-500 text-center">Generating the final preview...</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
const StageItem = ({
|
||||
number,
|
||||
label,
|
||||
active,
|
||||
stageKey,
|
||||
}: {
|
||||
number: number
|
||||
label: string
|
||||
active: boolean
|
||||
stageKey: 'outline' | 'text' | 'styling' | 'review'
|
||||
}) => (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => onStageSelect(stageKey)}
|
||||
disabled={isGenerating || isStageLoading}
|
||||
className={`flex w-full items-center justify-between text-sm ${
|
||||
active ? 'text-slate-900' : 'text-slate-400'
|
||||
} disabled:opacity-50`}
|
||||
>
|
||||
<div className="flex items-center gap-2">
|
||||
<span
|
||||
className={`flex h-6 w-6 items-center justify-center rounded-full border text-xs font-semibold ${
|
||||
active ? 'border-blue-600 text-blue-600' : 'border-slate-300 text-slate-400'
|
||||
}`}
|
||||
>
|
||||
{number}
|
||||
</span>
|
||||
<span className="font-medium">{label}</span>
|
||||
</div>
|
||||
</button>
|
||||
)
|
||||
|
||||
return (
|
||||
<div className="relative flex h-screen bg-slate-100 text-slate-900 overflow-hidden">
|
||||
<aside className="w-72 bg-white border-r border-slate-200 flex flex-col px-5 py-6 gap-6">
|
||||
<div className="flex items-center justify-between">
|
||||
<button className="text-xs text-slate-500" onClick={onBack}>
|
||||
Stirling
|
||||
</button>
|
||||
<button className="text-xs text-slate-400">Create</button>
|
||||
</div>
|
||||
|
||||
<div className="space-y-4">
|
||||
<StageItem number={1} label="Outline" active={stage === 'outline'} stageKey="outline" />
|
||||
<StageItem number={2} label="Text" active={stage === 'text'} stageKey="text" />
|
||||
<StageItem number={3} label="Review" active={stage === 'styling'} stageKey="styling" />
|
||||
<StageItem number={4} label="Export" active={stage === 'review'} stageKey="review" />
|
||||
</div>
|
||||
|
||||
{stage === 'outline' && (
|
||||
<div className="mt-2 space-y-2">
|
||||
<button
|
||||
className="w-full rounded-lg border border-slate-300 px-4 py-2 text-sm font-semibold text-slate-700 hover:bg-slate-100 disabled:opacity-50"
|
||||
onClick={onAiOutline}
|
||||
disabled={isGenerating || isStageLoading}
|
||||
>
|
||||
Auto-fill Fields
|
||||
</button>
|
||||
<button
|
||||
className="w-full rounded-lg bg-blue-600 px-4 py-2 text-sm font-semibold text-white hover:bg-blue-700 disabled:opacity-50"
|
||||
onClick={approveOutline}
|
||||
disabled={isGenerating || isStageLoading}
|
||||
>
|
||||
Approve and Continue
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
{stage === 'text' && (
|
||||
<button
|
||||
className="mt-2 w-full rounded-lg bg-blue-600 px-4 py-2 text-sm font-semibold text-white hover:bg-blue-700 disabled:opacity-50"
|
||||
onClick={approveDraft}
|
||||
disabled={isGenerating || isStageLoading}
|
||||
>
|
||||
Approve and Continue
|
||||
</button>
|
||||
)}
|
||||
{stage === 'styling' && (
|
||||
<button
|
||||
className="mt-2 w-full rounded-lg bg-blue-600 px-4 py-2 text-sm font-semibold text-white hover:bg-blue-700 disabled:opacity-50"
|
||||
onClick={saveAndReview}
|
||||
disabled={isGenerating || isStageLoading}
|
||||
>
|
||||
Continue to Export
|
||||
</button>
|
||||
)}
|
||||
|
||||
{stage === 'review' && (
|
||||
<div className="space-y-2 text-xs text-slate-500">
|
||||
<div className="font-semibold text-slate-700">Export</div>
|
||||
<div className="rounded-lg border border-slate-200 px-3 py-2 text-slate-600">
|
||||
{docLabel} - 1 page
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
className="inline-flex w-full items-center justify-center rounded-lg border border-slate-300 px-3 py-2 text-[11px] text-slate-600"
|
||||
onClick={onOpenImportTemplate}
|
||||
>
|
||||
Import template from PDF
|
||||
</button>
|
||||
{imagePlaceholdersCount > 0 && (
|
||||
<div className="rounded-lg border border-amber-200 bg-amber-50 px-3 py-2 text-amber-700">
|
||||
<div className="font-semibold">Image placeholders detected</div>
|
||||
<p className="mt-1 text-[11px] text-amber-700">
|
||||
Add images now or remove placeholders before exporting.
|
||||
</p>
|
||||
<div className="mt-2 flex flex-col gap-2">
|
||||
<label className="inline-flex w-full cursor-pointer items-center justify-center rounded-md border border-amber-300 bg-white px-3 py-1 text-[11px] text-amber-700">
|
||||
{isAssetUploading ? 'Uploading...' : 'Add image'}
|
||||
<input
|
||||
type="file"
|
||||
accept="image/png,image/jpeg,image/jpg,image/gif"
|
||||
className="hidden"
|
||||
disabled={isAssetUploading}
|
||||
onChange={(event) => {
|
||||
const file = event.target.files?.[0]
|
||||
if (file) onAddPlaceholderImage(file)
|
||||
event.currentTarget.value = ''
|
||||
}}
|
||||
/>
|
||||
</label>
|
||||
<button
|
||||
type="button"
|
||||
className="inline-flex w-full items-center justify-center rounded-md border border-slate-300 px-3 py-1 text-[11px] text-slate-600"
|
||||
onClick={onRemovePlaceholders}
|
||||
disabled={isAssetUploading}
|
||||
>
|
||||
Remove placeholders
|
||||
</button>
|
||||
</div>
|
||||
{assetError && <div className="mt-2 text-[11px] text-amber-600">{assetError}</div>}
|
||||
</div>
|
||||
)}
|
||||
{currentDoc?.pdfUrl && (
|
||||
<a
|
||||
href={currentDoc.pdfUrl}
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
className="inline-flex w-full items-center justify-center rounded-lg border border-slate-300 px-3 py-2 text-slate-700"
|
||||
>
|
||||
Export and Close
|
||||
</a>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</aside>
|
||||
|
||||
<main className="flex-1 overflow-auto px-8 py-10">
|
||||
<div className="mx-auto max-w-4xl space-y-8">
|
||||
<form
|
||||
onSubmit={onChatSubmit}
|
||||
className="rounded-2xl border border-slate-200 bg-white p-4 shadow-sm"
|
||||
>
|
||||
<div className="flex items-center gap-3">
|
||||
<button
|
||||
type="button"
|
||||
className="flex h-9 w-9 items-center justify-center rounded-full border border-slate-200 text-slate-400"
|
||||
>
|
||||
+
|
||||
</button>
|
||||
<div className="flex-1">
|
||||
<input
|
||||
className="w-full text-sm text-slate-700 placeholder:text-slate-400 focus:outline-none"
|
||||
value={prompt}
|
||||
onChange={(event) => onPromptChange(event.target.value)}
|
||||
placeholder="Describe what you want to create..."
|
||||
onKeyDown={onKeyDown}
|
||||
/>
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
className="rounded-full border border-slate-200 px-3 py-1 text-xs text-slate-500"
|
||||
onClick={onOpenImportTemplate}
|
||||
>
|
||||
Template
|
||||
</button>
|
||||
<button
|
||||
type="submit"
|
||||
className="flex h-9 w-9 items-center justify-center rounded-full bg-slate-900 text-white"
|
||||
>
|
||||
^
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<div className="rounded-2xl border border-slate-200 bg-white p-8 shadow-sm">
|
||||
{renderStageContent()}
|
||||
</div>
|
||||
</div>
|
||||
</main>
|
||||
|
||||
<aside className="w-16 border-l border-slate-200 bg-slate-50 flex flex-col items-center py-6 gap-4 text-slate-400 text-xs">
|
||||
<div className="h-10 w-10 rounded-xl bg-slate-200" />
|
||||
<div className="h-10 w-10 rounded-xl bg-slate-200" />
|
||||
<div className="h-10 w-10 rounded-xl bg-slate-200" />
|
||||
<div className="h-10 w-10 rounded-xl bg-slate-200" />
|
||||
</aside>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,367 @@
|
||||
import { useCallback, useEffect, useRef, useState } from 'react'
|
||||
|
||||
interface UseSpeechCaptureOptions {
|
||||
appendPrompt: (text: string) => void
|
||||
}
|
||||
|
||||
// Diagnostic logging
|
||||
const log = (category: string, message: string, data?: any) => {
|
||||
const timestamp = new Date().toISOString().split('T')[1]
|
||||
console.log(`[${timestamp}] [${category}]`, message, data !== undefined ? data : '')
|
||||
}
|
||||
|
||||
export function useSpeechCapture({ appendPrompt }: UseSpeechCaptureOptions) {
|
||||
const [isRecording, setIsRecording] = useState(false)
|
||||
const [whisperStatus, setWhisperStatus] = useState<string | null>(null)
|
||||
const [waveformHistory, setWaveformHistory] = useState<number[][]>([])
|
||||
|
||||
// Refs for audio visualization
|
||||
const audioContextRef = useRef<AudioContext | null>(null)
|
||||
const analyserRef = useRef<AnalyserNode | null>(null)
|
||||
const streamRef = useRef<MediaStream | null>(null)
|
||||
const animationFrameRef = useRef<number | null>(null)
|
||||
const isRunningRef = useRef(false)
|
||||
const lastUpdateTimeRef = useRef(0)
|
||||
|
||||
// Refs for speech recognition
|
||||
const speechRecRef = useRef<any>(null)
|
||||
const allTranscriptsRef = useRef<string[]>([])
|
||||
const shouldTranscribeOnStopRef = useRef(false)
|
||||
const appendPromptRef = useRef(appendPrompt)
|
||||
|
||||
// Keep appendPrompt ref updated
|
||||
useEffect(() => {
|
||||
appendPromptRef.current = appendPrompt
|
||||
}, [appendPrompt])
|
||||
|
||||
// Animation loop function - defined outside to avoid recreation
|
||||
const runVisualization = useCallback(() => {
|
||||
const animate = () => {
|
||||
// Check if we should continue
|
||||
if (!isRunningRef.current) {
|
||||
log('ANIM', 'Animation stopped - isRunningRef is false')
|
||||
return
|
||||
}
|
||||
|
||||
// Schedule next frame immediately
|
||||
animationFrameRef.current = requestAnimationFrame(animate)
|
||||
|
||||
// Check if analyser exists
|
||||
if (!analyserRef.current) {
|
||||
log('ANIM', 'No analyser available')
|
||||
return
|
||||
}
|
||||
|
||||
// Throttle to ~15fps
|
||||
const now = Date.now()
|
||||
if (now - lastUpdateTimeRef.current < 66) {
|
||||
return
|
||||
}
|
||||
lastUpdateTimeRef.current = now
|
||||
|
||||
// Get frequency data
|
||||
const bufferLength = analyserRef.current.frequencyBinCount
|
||||
const dataArray = new Uint8Array(bufferLength)
|
||||
analyserRef.current.getByteFrequencyData(dataArray)
|
||||
|
||||
// Sample frequency data
|
||||
const levels: number[] = []
|
||||
const numSamples = 40
|
||||
const step = Math.floor(bufferLength / numSamples)
|
||||
for (let i = 0; i < numSamples; i++) {
|
||||
const index = i * step
|
||||
const value = dataArray[index] / 255
|
||||
levels.push(value)
|
||||
}
|
||||
|
||||
// Update waveform history - keep max 200 columns (enough for any reasonable width)
|
||||
setWaveformHistory((prev) => {
|
||||
const newHistory = [...prev, levels]
|
||||
return newHistory.slice(-200)
|
||||
})
|
||||
}
|
||||
|
||||
log('ANIM', 'Starting animation loop')
|
||||
isRunningRef.current = true
|
||||
lastUpdateTimeRef.current = 0
|
||||
animate()
|
||||
}, [])
|
||||
|
||||
// Stop visualization
|
||||
const stopVisualization = useCallback(() => {
|
||||
log('STOP_VIS', 'Stopping visualization')
|
||||
isRunningRef.current = false
|
||||
|
||||
if (animationFrameRef.current) {
|
||||
cancelAnimationFrame(animationFrameRef.current)
|
||||
animationFrameRef.current = null
|
||||
log('STOP_VIS', 'Cancelled animation frame')
|
||||
}
|
||||
|
||||
if (audioContextRef.current) {
|
||||
audioContextRef.current.close().catch(() => {})
|
||||
audioContextRef.current = null
|
||||
log('STOP_VIS', 'Closed audio context')
|
||||
}
|
||||
|
||||
if (streamRef.current) {
|
||||
streamRef.current.getTracks().forEach((track) => track.stop())
|
||||
streamRef.current = null
|
||||
log('STOP_VIS', 'Stopped stream tracks')
|
||||
}
|
||||
|
||||
analyserRef.current = null
|
||||
setWaveformHistory([])
|
||||
}, [])
|
||||
|
||||
// Start recording
|
||||
const startRecording = useCallback(async () => {
|
||||
log('START', 'startRecording called, isRecording:', isRecording)
|
||||
if (isRecording) {
|
||||
log('START', 'Already recording, returning')
|
||||
return
|
||||
}
|
||||
|
||||
// Set recording state immediately
|
||||
setIsRecording(true)
|
||||
setWhisperStatus('Listening...')
|
||||
|
||||
// Initialize with placeholder
|
||||
setWaveformHistory([Array(40).fill(0.1)])
|
||||
log('START', 'Set initial placeholder waveform')
|
||||
|
||||
// Set up audio visualization
|
||||
try {
|
||||
log('AUDIO', 'Requesting microphone access')
|
||||
const stream = await navigator.mediaDevices.getUserMedia({ audio: true })
|
||||
streamRef.current = stream
|
||||
log('AUDIO', 'Got microphone stream')
|
||||
|
||||
const AudioContextClass = window.AudioContext || (window as any).webkitAudioContext
|
||||
const audioContext = new AudioContextClass()
|
||||
log('AUDIO', 'Created audio context, state:', audioContext.state)
|
||||
|
||||
if (audioContext.state === 'suspended') {
|
||||
log('AUDIO', 'Resuming suspended audio context')
|
||||
await audioContext.resume()
|
||||
log('AUDIO', 'Audio context resumed, state:', audioContext.state)
|
||||
}
|
||||
|
||||
const analyser = audioContext.createAnalyser()
|
||||
analyser.fftSize = 256
|
||||
analyser.smoothingTimeConstant = 0.8
|
||||
|
||||
const source = audioContext.createMediaStreamSource(stream)
|
||||
source.connect(analyser)
|
||||
log('AUDIO', 'Connected analyser to stream')
|
||||
|
||||
audioContextRef.current = audioContext
|
||||
analyserRef.current = analyser
|
||||
|
||||
// Start visualization
|
||||
runVisualization()
|
||||
log('AUDIO', 'Started visualization')
|
||||
|
||||
} catch (err) {
|
||||
log('AUDIO', 'Failed to set up audio:', err)
|
||||
// Continue with placeholder even if audio fails
|
||||
isRunningRef.current = true
|
||||
}
|
||||
|
||||
// Set up speech recognition
|
||||
const SpeechRec = (window as any).SpeechRecognition || (window as any).webkitSpeechRecognition
|
||||
if (SpeechRec) {
|
||||
log('SPEECH', 'Setting up speech recognition')
|
||||
allTranscriptsRef.current = []
|
||||
|
||||
const rec = new SpeechRec()
|
||||
rec.continuous = true
|
||||
rec.interimResults = true
|
||||
rec.lang = 'en-US'
|
||||
|
||||
rec.onstart = () => {
|
||||
log('SPEECH', 'Speech recognition started')
|
||||
}
|
||||
|
||||
rec.onresult = (event: any) => {
|
||||
log('SPEECH', `Got result, resultIndex: ${event.resultIndex}, results count: ${event.results.length}`)
|
||||
|
||||
// Only process new results starting from resultIndex
|
||||
for (let i = event.resultIndex; i < event.results.length; i++) {
|
||||
const result = event.results[i]
|
||||
const transcript = result[0]?.transcript
|
||||
|
||||
// Only store final segments to avoid duplicates from interim results
|
||||
if (result.isFinal && transcript) {
|
||||
allTranscriptsRef.current = [...allTranscriptsRef.current, transcript]
|
||||
log('SPEECH', 'Added final transcript:', transcript)
|
||||
}
|
||||
}
|
||||
|
||||
log('SPEECH', 'Accumulated transcripts:', allTranscriptsRef.current.join(' | '))
|
||||
}
|
||||
|
||||
rec.onerror = (event: any) => {
|
||||
log('SPEECH', 'Speech recognition error:', event.error)
|
||||
// Don't stop on common errors
|
||||
if (event.error === 'no-speech' || event.error === 'aborted') {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
rec.onend = () => {
|
||||
log('SPEECH', `Speech recognition ended, isRunningRef: ${isRunningRef.current}, shouldTranscribe: ${shouldTranscribeOnStopRef.current}`)
|
||||
|
||||
// If we're intentionally stopping and should transcribe, do it now
|
||||
if (shouldTranscribeOnStopRef.current) {
|
||||
shouldTranscribeOnStopRef.current = false
|
||||
if (allTranscriptsRef.current.length > 0) {
|
||||
const transcript = allTranscriptsRef.current.join(' ').replace(/\s+/g, ' ').trim()
|
||||
log('SPEECH', 'Final transcript from onend:', transcript)
|
||||
if (transcript) {
|
||||
appendPromptRef.current(transcript)
|
||||
setWhisperStatus('Transcription ready')
|
||||
setTimeout(() => setWhisperStatus(null), 1500)
|
||||
}
|
||||
}
|
||||
allTranscriptsRef.current = []
|
||||
return // Don't restart
|
||||
}
|
||||
|
||||
// Restart if still recording
|
||||
if (isRunningRef.current) {
|
||||
log('SPEECH', 'Restarting speech recognition')
|
||||
setTimeout(() => {
|
||||
if (isRunningRef.current && speechRecRef.current) {
|
||||
try {
|
||||
// Create new instance
|
||||
const newRec = new SpeechRec()
|
||||
newRec.continuous = true
|
||||
newRec.interimResults = true
|
||||
newRec.lang = 'en-US'
|
||||
newRec.onstart = rec.onstart
|
||||
newRec.onresult = rec.onresult
|
||||
newRec.onerror = rec.onerror
|
||||
newRec.onend = rec.onend
|
||||
speechRecRef.current = newRec
|
||||
newRec.start()
|
||||
log('SPEECH', 'Speech recognition restarted')
|
||||
} catch (err) {
|
||||
log('SPEECH', 'Failed to restart speech recognition:', err)
|
||||
}
|
||||
}
|
||||
}, 100)
|
||||
}
|
||||
}
|
||||
|
||||
speechRecRef.current = rec
|
||||
try {
|
||||
rec.start()
|
||||
log('SPEECH', 'Speech recognition start() called')
|
||||
} catch (err) {
|
||||
log('SPEECH', 'Failed to start speech recognition:', err)
|
||||
}
|
||||
} else {
|
||||
log('SPEECH', 'Speech recognition not available')
|
||||
}
|
||||
|
||||
log('START', 'startRecording completed')
|
||||
}, [isRecording, runVisualization])
|
||||
|
||||
// Stop recording
|
||||
const stopRecording = useCallback((shouldTranscribe = true) => {
|
||||
log('STOP', 'stopRecording called, shouldTranscribe:', shouldTranscribe)
|
||||
|
||||
// Set flag for onend handler to know whether to transcribe
|
||||
shouldTranscribeOnStopRef.current = shouldTranscribe
|
||||
|
||||
// Stop visualization first
|
||||
stopVisualization()
|
||||
|
||||
// Stop speech recognition - onend will handle transcription
|
||||
if (speechRecRef.current) {
|
||||
try {
|
||||
speechRecRef.current.stop()
|
||||
log('STOP', 'Stopped speech recognition, waiting for onend')
|
||||
} catch (err) {
|
||||
log('STOP', 'Error stopping speech recognition:', err)
|
||||
// If stop fails, handle transcription here
|
||||
if (shouldTranscribe && allTranscriptsRef.current.length > 0) {
|
||||
const transcript = allTranscriptsRef.current.join(' ').replace(/\s+/g, ' ').trim()
|
||||
if (transcript) {
|
||||
appendPrompt(transcript)
|
||||
setWhisperStatus('Transcription ready')
|
||||
}
|
||||
}
|
||||
allTranscriptsRef.current = []
|
||||
}
|
||||
speechRecRef.current = null
|
||||
} else {
|
||||
// No speech recognition active, clear state
|
||||
allTranscriptsRef.current = []
|
||||
}
|
||||
|
||||
setIsRecording(false)
|
||||
|
||||
log('STOP', 'stopRecording completed')
|
||||
}, [appendPrompt, stopVisualization])
|
||||
|
||||
// Toggle recording
|
||||
const toggleRecording = useCallback(() => {
|
||||
log('TOGGLE', 'toggleRecording called, isRecording:', isRecording)
|
||||
if (isRecording) {
|
||||
stopRecording(true)
|
||||
} else {
|
||||
startRecording()
|
||||
}
|
||||
}, [isRecording, startRecording, stopRecording])
|
||||
|
||||
// Cancel recording
|
||||
const cancelRecording = useCallback(() => {
|
||||
log('CANCEL', 'cancelRecording called')
|
||||
if (isRecording) {
|
||||
stopRecording(false)
|
||||
}
|
||||
}, [isRecording, stopRecording])
|
||||
|
||||
// Accept recording
|
||||
const acceptRecording = useCallback(() => {
|
||||
log('ACCEPT', 'acceptRecording called')
|
||||
if (isRecording) {
|
||||
stopRecording(true)
|
||||
}
|
||||
}, [isRecording, stopRecording])
|
||||
|
||||
// Cleanup on unmount
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
log('CLEANUP', 'Component unmounting')
|
||||
isRunningRef.current = false
|
||||
if (animationFrameRef.current) {
|
||||
cancelAnimationFrame(animationFrameRef.current)
|
||||
}
|
||||
if (audioContextRef.current) {
|
||||
audioContextRef.current.close().catch(() => {})
|
||||
}
|
||||
if (streamRef.current) {
|
||||
streamRef.current.getTracks().forEach((track) => track.stop())
|
||||
}
|
||||
if (speechRecRef.current) {
|
||||
try {
|
||||
speechRecRef.current.stop()
|
||||
} catch {}
|
||||
}
|
||||
}
|
||||
}, [])
|
||||
|
||||
return {
|
||||
isRecording,
|
||||
whisperLoading: false, // Not used in speech recognition mode
|
||||
whisperStatus,
|
||||
toggleRecording,
|
||||
cancelRecording,
|
||||
acceptRecording,
|
||||
audioLevels: [], // Not used anymore
|
||||
waveformHistory,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
@tailwind base;
|
||||
@tailwind components;
|
||||
@tailwind utilities;
|
||||
|
||||
@keyframes loading {
|
||||
0% {
|
||||
transform: translateX(-100%);
|
||||
}
|
||||
50% {
|
||||
transform: translateX(50%);
|
||||
}
|
||||
100% {
|
||||
transform: translateX(200%);
|
||||
}
|
||||
}
|
||||
|
||||
* {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
body {
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', sans-serif;
|
||||
-webkit-font-smoothing: antialiased;
|
||||
-moz-osx-font-smoothing: grayscale;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import React from 'react'
|
||||
import ReactDOM from 'react-dom/client'
|
||||
import App from './App.tsx'
|
||||
import './index.css'
|
||||
|
||||
ReactDOM.createRoot(document.getElementById('root')!).render(
|
||||
<React.StrictMode>
|
||||
<App />
|
||||
</React.StrictMode>,
|
||||
)
|
||||
@@ -0,0 +1,31 @@
|
||||
// Auto-generated by generate_template_thumbnails.py
|
||||
export type TemplateCatalogEntry = {
|
||||
docType: string
|
||||
templateCount: number
|
||||
templates: string[]
|
||||
}
|
||||
|
||||
export const templateCatalog: TemplateCatalogEntry[] = [
|
||||
{ docType: 'academic_articles', templateCount: 1, templates: ['default'] },
|
||||
{ docType: 'academic_journals', templateCount: 1, templates: ['default'] },
|
||||
{ docType: 'assignments', templateCount: 1, templates: ['default'] },
|
||||
{ docType: 'books', templateCount: 1, templates: ['default'] },
|
||||
{ docType: 'business_cards', templateCount: 1, templates: ['default'] },
|
||||
{ docType: 'business_reports', templateCount: 1, templates: ['default'] },
|
||||
{ docType: 'calendars', templateCount: 1, templates: ['default'] },
|
||||
{ docType: 'conference_posters', templateCount: 1, templates: ['default'] },
|
||||
{ docType: 'cover_letters', templateCount: 1, templates: ['default'] },
|
||||
{ docType: 'cvs_and_resumes', templateCount: 1, templates: ['default'] },
|
||||
{ docType: 'essays', templateCount: 1, templates: ['default'] },
|
||||
{ docType: 'formal_letters', templateCount: 1, templates: ['default'] },
|
||||
{ docType: 'invoices', templateCount: 1, templates: ['default'] },
|
||||
{ docType: 'laboratory_books', templateCount: 1, templates: ['default'] },
|
||||
{ docType: 'laboratory_reports', templateCount: 1, templates: ['default'] },
|
||||
{ docType: 'miscellaneous', templateCount: 1, templates: ['default'] },
|
||||
{ docType: 'newsletters', templateCount: 1, templates: ['default'] },
|
||||
{ docType: 'presentations', templateCount: 1, templates: ['default'] },
|
||||
{ docType: 'recipes', templateCount: 1, templates: ['default'] },
|
||||
{ docType: 'signs', templateCount: 1, templates: ['default'] },
|
||||
{ docType: 'theses', templateCount: 1, templates: ['default'] },
|
||||
{ docType: 'title_pages', templateCount: 1, templates: ['default'] }
|
||||
]
|
||||
@@ -0,0 +1,30 @@
|
||||
export interface Message {
|
||||
role: 'user' | 'assistant'
|
||||
text: string
|
||||
}
|
||||
|
||||
export interface StyleProfile {
|
||||
layout_preference?: string
|
||||
font_preference?: string
|
||||
tone?: string
|
||||
color_accent?: string
|
||||
last_doc_type?: string | null
|
||||
}
|
||||
|
||||
export interface VersionEntry {
|
||||
id: string
|
||||
prompt: string
|
||||
latex: string
|
||||
pdfUrl: string
|
||||
documentType: string
|
||||
createdAt?: string
|
||||
styleProfile?: StyleProfile
|
||||
templateUsed?: boolean
|
||||
}
|
||||
|
||||
export interface DocumentState {
|
||||
latex: string
|
||||
pdfUrl: string
|
||||
documentType: string
|
||||
templateUsed?: boolean
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
/** @type {import('tailwindcss').Config} */
|
||||
export default {
|
||||
content: [
|
||||
"./index.html",
|
||||
"./src/**/*.{js,ts,jsx,tsx}",
|
||||
],
|
||||
theme: {
|
||||
extend: {},
|
||||
},
|
||||
plugins: [],
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2020",
|
||||
"useDefineForClassFields": true,
|
||||
"lib": ["ES2020", "DOM", "DOM.Iterable"],
|
||||
"module": "ESNext",
|
||||
"skipLibCheck": true,
|
||||
"moduleResolution": "bundler",
|
||||
"allowImportingTsExtensions": true,
|
||||
"resolveJsonModule": true,
|
||||
"isolatedModules": true,
|
||||
"noEmit": true,
|
||||
"jsx": "react-jsx",
|
||||
"strict": true,
|
||||
"noUnusedLocals": true,
|
||||
"noUnusedParameters": true,
|
||||
"noFallthroughCasesInSwitch": true
|
||||
},
|
||||
"include": ["src"],
|
||||
"references": [{ "path": "./tsconfig.node.json" }]
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"composite": true,
|
||||
"skipLibCheck": true,
|
||||
"module": "ESNext",
|
||||
"moduleResolution": "bundler",
|
||||
"allowSyntheticDefaultImports": true
|
||||
},
|
||||
"include": ["vite.config.ts"]
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import { defineConfig } from 'vite'
|
||||
import react from '@vitejs/plugin-react'
|
||||
|
||||
export default defineConfig({
|
||||
plugins: [react()],
|
||||
server: {
|
||||
port: 3000,
|
||||
host: true,
|
||||
allowedHosts: ['ai.froodleplex.com', 'ai-demo.stirling.com'],
|
||||
proxy: {
|
||||
'/api/v1/ai': {
|
||||
target: process.env.DOCKER_ENV ? 'http://backend:8080' : 'http://localhost:8080',
|
||||
changeOrigin: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
@@ -417,6 +417,7 @@ public class ApplicationProperties {
|
||||
private String frontendUrl; // Frontend URL for invite email links (e.g.
|
||||
|
||||
// 'https://app.example.com'). If not set, falls back to backendUrl.
|
||||
private String aiServiceBaseUrl; // Base URL for the AI document generator backend (e.g. 'http://localhost:5000').
|
||||
|
||||
public boolean isAnalyticsEnabled() {
|
||||
return this.getEnableAnalytics() != null && this.getEnableAnalytics();
|
||||
|
||||
@@ -0,0 +1,147 @@
|
||||
package stirling.software.SPDF.controller.api.ai;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.util.Optional;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import stirling.software.SPDF.service.ai.AiProxyService;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/ai")
|
||||
@Slf4j
|
||||
public class AiProxyController {
|
||||
|
||||
private final AiProxyService aiProxyService;
|
||||
|
||||
public AiProxyController(AiProxyService aiProxyService) {
|
||||
this.aiProxyService = aiProxyService;
|
||||
}
|
||||
|
||||
@PostMapping({"/generate", "/generate/generate"})
|
||||
public ResponseEntity<StreamingResponseBody> generate(HttpServletRequest request) {
|
||||
return proxy("POST", "/api/generate", request, false);
|
||||
}
|
||||
|
||||
@PostMapping(
|
||||
value = {"/generate_stream", "/generate/generate_stream"},
|
||||
produces = MediaType.TEXT_EVENT_STREAM_VALUE)
|
||||
public ResponseEntity<StreamingResponseBody> generateStream(HttpServletRequest request) {
|
||||
return proxy("POST", "/api/generate_stream", request, true);
|
||||
}
|
||||
|
||||
@PostMapping("/intent/check")
|
||||
public ResponseEntity<StreamingResponseBody> intentCheck(HttpServletRequest request) {
|
||||
return proxy("POST", "/api/intent/check", request, false);
|
||||
}
|
||||
|
||||
@PostMapping("/pdf/answer")
|
||||
public ResponseEntity<StreamingResponseBody> pdfAnswer(HttpServletRequest request) {
|
||||
return proxy("POST", "/api/pdf/answer", request, false);
|
||||
}
|
||||
|
||||
@PostMapping("/progressive_render")
|
||||
public ResponseEntity<StreamingResponseBody> progressiveRender(HttpServletRequest request) {
|
||||
return proxy("POST", "/api/progressive_render", request, false);
|
||||
}
|
||||
|
||||
@GetMapping("/versions/{userId}")
|
||||
public ResponseEntity<StreamingResponseBody> versions(
|
||||
@PathVariable("userId") String userId, HttpServletRequest request) {
|
||||
return proxy("GET", "/api/versions/" + userId, request, false);
|
||||
}
|
||||
|
||||
@GetMapping("/style/{userId}")
|
||||
public ResponseEntity<StreamingResponseBody> style(
|
||||
@PathVariable("userId") String userId, HttpServletRequest request) {
|
||||
return proxy("GET", "/api/style/" + userId, request, false);
|
||||
}
|
||||
|
||||
@PostMapping("/style/{userId}")
|
||||
public ResponseEntity<StreamingResponseBody> updateStyle(
|
||||
@PathVariable("userId") String userId, HttpServletRequest request) {
|
||||
return proxy("POST", "/api/style/" + userId, request, false);
|
||||
}
|
||||
|
||||
@PostMapping("/import_template")
|
||||
public ResponseEntity<StreamingResponseBody> importTemplate(HttpServletRequest request) {
|
||||
return proxy("POST", "/api/import_template", request, false);
|
||||
}
|
||||
|
||||
@GetMapping("/pdf-editor/document")
|
||||
public ResponseEntity<StreamingResponseBody> pdfEditorDocument(HttpServletRequest request) {
|
||||
return proxy("GET", "/api/pdf-editor/document", request, false);
|
||||
}
|
||||
|
||||
@PostMapping("/pdf-editor/upload")
|
||||
public ResponseEntity<StreamingResponseBody> pdfEditorUpload(HttpServletRequest request) {
|
||||
return proxy("POST", "/api/pdf-editor/upload", request, false);
|
||||
}
|
||||
|
||||
@GetMapping("/output/{filename}")
|
||||
public ResponseEntity<StreamingResponseBody> output(
|
||||
@PathVariable("filename") String filename, HttpServletRequest request) {
|
||||
return proxy("GET", "/output/" + filename, request, false);
|
||||
}
|
||||
|
||||
@GetMapping("/health")
|
||||
public ResponseEntity<StreamingResponseBody> health(HttpServletRequest request) {
|
||||
return proxy("GET", "/health", request, false);
|
||||
}
|
||||
|
||||
private ResponseEntity<StreamingResponseBody> proxy(
|
||||
String method, String path, HttpServletRequest request, boolean acceptEventStream) {
|
||||
try {
|
||||
HttpResponse<InputStream> response =
|
||||
aiProxyService.forward(method, path, request, acceptEventStream);
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
copyHeader(response, headers, HttpHeaders.CONTENT_TYPE);
|
||||
copyHeader(response, headers, HttpHeaders.CACHE_CONTROL);
|
||||
copyHeader(response, headers, "X-Accel-Buffering");
|
||||
copyHeader(response, headers, HttpHeaders.CONTENT_DISPOSITION);
|
||||
copyHeader(response, headers, HttpHeaders.CONTENT_LENGTH);
|
||||
if (acceptEventStream && !headers.containsKey(HttpHeaders.CONTENT_TYPE)) {
|
||||
headers.set(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_EVENT_STREAM_VALUE);
|
||||
}
|
||||
|
||||
StreamingResponseBody body =
|
||||
outputStream -> {
|
||||
try (InputStream inputStream = response.body()) {
|
||||
inputStream.transferTo(outputStream);
|
||||
}
|
||||
};
|
||||
HttpStatus status =
|
||||
Optional.ofNullable(HttpStatus.resolve(response.statusCode()))
|
||||
.orElse(HttpStatus.BAD_GATEWAY);
|
||||
return new ResponseEntity<>(body, headers, status);
|
||||
} catch (Exception exc) {
|
||||
log.error("AI proxy failed path={}", path, exc);
|
||||
StreamingResponseBody body =
|
||||
outputStream ->
|
||||
outputStream.write(
|
||||
"{\"error\":\"AI backend unavailable\"}".getBytes());
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
return new ResponseEntity<>(body, headers, HttpStatus.SERVICE_UNAVAILABLE);
|
||||
}
|
||||
}
|
||||
|
||||
private void copyHeader(
|
||||
HttpResponse<?> response, HttpHeaders headers, String headerName) {
|
||||
response.headers().firstValue(headerName).ifPresent(value -> headers.set(headerName, value));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package stirling.software.SPDF.service.ai;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import stirling.software.common.model.ApplicationProperties;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class AiProxyService {
|
||||
|
||||
private static final String DEFAULT_AI_BASE_URL = "http://localhost:5000";
|
||||
|
||||
private final ApplicationProperties applicationProperties;
|
||||
private final HttpClient httpClient;
|
||||
|
||||
public AiProxyService(ApplicationProperties applicationProperties) {
|
||||
this.applicationProperties = applicationProperties;
|
||||
this.httpClient = HttpClient.newBuilder().build();
|
||||
}
|
||||
|
||||
public HttpResponse<InputStream> forward(
|
||||
String method, String path, HttpServletRequest request, boolean acceptEventStream)
|
||||
throws IOException, InterruptedException {
|
||||
String targetUrl = buildTargetUrl(path, request.getQueryString());
|
||||
HttpRequest.Builder builder = HttpRequest.newBuilder(URI.create(targetUrl));
|
||||
|
||||
String contentType = request.getContentType();
|
||||
if (contentType != null && !contentType.isBlank()) {
|
||||
builder.header("Content-Type", contentType);
|
||||
}
|
||||
|
||||
String accept = request.getHeader("Accept");
|
||||
if (acceptEventStream) {
|
||||
builder.header("Accept", "text/event-stream");
|
||||
} else if (accept != null && !accept.isBlank()) {
|
||||
builder.header("Accept", accept);
|
||||
}
|
||||
|
||||
builder.method(method, buildBodyPublisher(method, request));
|
||||
log.debug("Proxying AI request {} {}", method, targetUrl);
|
||||
return httpClient.send(builder.build(), HttpResponse.BodyHandlers.ofInputStream());
|
||||
}
|
||||
|
||||
private String buildTargetUrl(String path, String queryString) {
|
||||
String baseUrl = applicationProperties.getSystem().getAiServiceBaseUrl();
|
||||
if (baseUrl == null || baseUrl.isBlank()) {
|
||||
baseUrl = DEFAULT_AI_BASE_URL;
|
||||
}
|
||||
baseUrl = baseUrl.trim();
|
||||
if (baseUrl.endsWith("/")) {
|
||||
baseUrl = baseUrl.substring(0, baseUrl.length() - 1);
|
||||
}
|
||||
if (!path.startsWith("/")) {
|
||||
path = "/" + path;
|
||||
}
|
||||
String url = baseUrl + path;
|
||||
if (queryString != null && !queryString.isBlank()) {
|
||||
url += "?" + queryString;
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
private HttpRequest.BodyPublisher buildBodyPublisher(String method, HttpServletRequest request) {
|
||||
if ("GET".equalsIgnoreCase(method) || "DELETE".equalsIgnoreCase(method)) {
|
||||
return HttpRequest.BodyPublishers.noBody();
|
||||
}
|
||||
return HttpRequest.BodyPublishers.ofInputStream(
|
||||
() -> {
|
||||
try {
|
||||
return request.getInputStream();
|
||||
} catch (IOException exc) {
|
||||
throw new UncheckedIOException(exc);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -145,6 +145,7 @@ system:
|
||||
corsAllowedOrigins: [] # List of allowed origins for CORS (e.g. ['http://localhost:5173', 'https://app.example.com']). Leave empty to disable CORS. For local development with frontend on port 5173, add 'http://localhost:5173'
|
||||
backendUrl: '' # Backend base URL for SAML/OAuth/API callbacks (e.g. 'http://localhost:8080' for dev, 'https://api.example.com' for production). REQUIRED for SSO authentication to work correctly. This is where your IdP will send SAML responses and OAuth callbacks. Leave empty to default to 'http://localhost:8080' in development.
|
||||
frontendUrl: '' # Frontend URL for invite email links (e.g. 'https://app.example.com'). Optional - if not set, will use backendUrl. This is the URL users click in invite emails.
|
||||
aiServiceBaseUrl: '' # Base URL for the AI document generator backend (e.g. 'http://localhost:5000'). Leave empty to default to 'http://localhost:5000' in development.
|
||||
serverCertificate:
|
||||
enabled: true # Enable server-side certificate for "Sign with Stirling-PDF" option
|
||||
organizationName: Stirling-PDF # Organization name for generated certificates
|
||||
|
||||
@@ -0,0 +1,227 @@
|
||||
package stirling.software.proprietary.controller.api.ai;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.util.Optional;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import stirling.software.proprietary.model.ai.AiCreateSession;
|
||||
import stirling.software.proprietary.service.ai.AiCreateProxyService;
|
||||
import stirling.software.proprietary.service.ai.AiCreateSessionService;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/ai/create")
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class AiCreateController {
|
||||
|
||||
private final AiCreateSessionService sessionService;
|
||||
private final AiCreateProxyService proxyService;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
@PostMapping("/sessions")
|
||||
public ResponseEntity<CreateSessionResponse> createSession(
|
||||
@RequestBody CreateSessionRequest request) {
|
||||
if (request.prompt() == null || request.prompt().isBlank()) {
|
||||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Prompt is required");
|
||||
}
|
||||
AiCreateSession session =
|
||||
sessionService.createSession(request.prompt(), request.docType(), request.templateId());
|
||||
log.info(
|
||||
"AI create session created sessionId={} userId={} docType={} templateId={}",
|
||||
session.getSessionId(),
|
||||
session.getUserId(),
|
||||
session.getDocType(),
|
||||
session.getTemplateId());
|
||||
return ResponseEntity.ok(new CreateSessionResponse(session.getSessionId()));
|
||||
}
|
||||
|
||||
@GetMapping("/sessions/{sessionId}")
|
||||
public ResponseEntity<AiCreateSessionResponse> getSession(
|
||||
@PathVariable String sessionId) {
|
||||
AiCreateSession session = sessionService.getSessionForCurrentUser(sessionId);
|
||||
return ResponseEntity.ok(AiCreateSessionResponse.from(session));
|
||||
}
|
||||
|
||||
@PostMapping("/sessions/{sessionId}/outline")
|
||||
public ResponseEntity<AiCreateSessionResponse> updateOutline(
|
||||
@PathVariable String sessionId, @RequestBody OutlineRequest request) {
|
||||
if (request.outlineText() == null || request.outlineText().isBlank()) {
|
||||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Outline text is required");
|
||||
}
|
||||
String constraintsPayload = null;
|
||||
if (request.constraints() != null) {
|
||||
try {
|
||||
constraintsPayload = objectMapper.writeValueAsString(request.constraints());
|
||||
} catch (JsonProcessingException exc) {
|
||||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Invalid constraints payload", exc);
|
||||
}
|
||||
}
|
||||
AiCreateSession session =
|
||||
sessionService.updateOutline(sessionId, request.outlineText(), constraintsPayload);
|
||||
return ResponseEntity.ok(AiCreateSessionResponse.from(session));
|
||||
}
|
||||
|
||||
@PostMapping("/sessions/{sessionId}/reprompt")
|
||||
public ResponseEntity<AiCreateSessionResponse> reprompt(
|
||||
@PathVariable String sessionId, @RequestBody RepromptRequest request) {
|
||||
if (request.prompt() == null || request.prompt().isBlank()) {
|
||||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Prompt is required");
|
||||
}
|
||||
AiCreateSession session = sessionService.reprompt(sessionId, request.prompt());
|
||||
return ResponseEntity.ok(AiCreateSessionResponse.from(session));
|
||||
}
|
||||
|
||||
@PostMapping("/sessions/{sessionId}/draft")
|
||||
public ResponseEntity<AiCreateSessionResponse> updateDraft(
|
||||
@PathVariable String sessionId, @RequestBody DraftRequest request) {
|
||||
if (request.draftSections() == null || request.draftSections().isEmpty()) {
|
||||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Draft sections are required");
|
||||
}
|
||||
String payload;
|
||||
try {
|
||||
payload = objectMapper.writeValueAsString(request.draftSections());
|
||||
} catch (JsonProcessingException exc) {
|
||||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Invalid draft sections payload", exc);
|
||||
}
|
||||
AiCreateSession session = sessionService.updateDraftSections(sessionId, payload);
|
||||
return ResponseEntity.ok(AiCreateSessionResponse.from(session));
|
||||
}
|
||||
|
||||
@PostMapping("/sessions/{sessionId}/template")
|
||||
public ResponseEntity<AiCreateSessionResponse> updateTemplate(
|
||||
@PathVariable String sessionId, @RequestBody TemplateRequest request) {
|
||||
if ((request.docType() == null || request.docType().isBlank())
|
||||
&& (request.templateId() == null || request.templateId().isBlank())) {
|
||||
throw new ResponseStatusException(
|
||||
HttpStatus.BAD_REQUEST, "docType or templateId is required");
|
||||
}
|
||||
AiCreateSession session =
|
||||
sessionService.updateTemplate(
|
||||
sessionId, request.docType(), request.templateId());
|
||||
return ResponseEntity.ok(AiCreateSessionResponse.from(session));
|
||||
}
|
||||
|
||||
@PostMapping("/sessions/{sessionId}/fields")
|
||||
public ResponseEntity<StreamingResponseBody> fillFields(
|
||||
@PathVariable String sessionId, HttpServletRequest request) {
|
||||
sessionService.getSessionForCurrentUser(sessionId);
|
||||
log.info("AI create fillFields sessionId={}", sessionId);
|
||||
return proxy("POST", "/api/create/sessions/" + sessionId + "/fields", request, false);
|
||||
}
|
||||
|
||||
@GetMapping(value = "/sessions/{sessionId}/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
|
||||
public ResponseEntity<StreamingResponseBody> stream(
|
||||
@PathVariable String sessionId, HttpServletRequest request) {
|
||||
sessionService.getSessionForCurrentUser(sessionId);
|
||||
return proxy("GET", "/api/create/sessions/" + sessionId + "/stream", request, true);
|
||||
}
|
||||
|
||||
private ResponseEntity<StreamingResponseBody> proxy(
|
||||
String method, String path, HttpServletRequest request, boolean acceptEventStream) {
|
||||
try {
|
||||
HttpResponse<InputStream> response =
|
||||
proxyService.forward(method, path, request, acceptEventStream);
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
copyHeader(response, headers, HttpHeaders.CONTENT_TYPE);
|
||||
copyHeader(response, headers, HttpHeaders.CACHE_CONTROL);
|
||||
copyHeader(response, headers, "X-Accel-Buffering");
|
||||
copyHeader(response, headers, HttpHeaders.CONTENT_DISPOSITION);
|
||||
copyHeader(response, headers, HttpHeaders.CONTENT_LENGTH);
|
||||
if (acceptEventStream && !headers.containsKey(HttpHeaders.CONTENT_TYPE)) {
|
||||
headers.set(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_EVENT_STREAM_VALUE);
|
||||
}
|
||||
|
||||
StreamingResponseBody body =
|
||||
outputStream -> {
|
||||
try (InputStream inputStream = response.body()) {
|
||||
inputStream.transferTo(outputStream);
|
||||
}
|
||||
};
|
||||
HttpStatus status =
|
||||
Optional.ofNullable(HttpStatus.resolve(response.statusCode()))
|
||||
.orElse(HttpStatus.BAD_GATEWAY);
|
||||
return new ResponseEntity<>(body, headers, status);
|
||||
} catch (Exception exc) {
|
||||
log.error("AI create proxy failed path={}", path, exc);
|
||||
StreamingResponseBody body =
|
||||
outputStream ->
|
||||
outputStream.write(
|
||||
"{\"error\":\"AI backend unavailable\"}".getBytes());
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
return new ResponseEntity<>(body, headers, HttpStatus.SERVICE_UNAVAILABLE);
|
||||
}
|
||||
}
|
||||
|
||||
private void copyHeader(
|
||||
HttpResponse<?> response, HttpHeaders headers, String headerName) {
|
||||
response.headers().firstValue(headerName).ifPresent(value -> headers.set(headerName, value));
|
||||
}
|
||||
|
||||
public record CreateSessionRequest(String prompt, String docType, String templateId) {}
|
||||
|
||||
public record CreateSessionResponse(String sessionId) {}
|
||||
|
||||
public record OutlineRequest(String outlineText, Map<String, Object> constraints) {}
|
||||
|
||||
public record RepromptRequest(String prompt) {}
|
||||
|
||||
public record DraftRequest(List<DraftSection> draftSections) {}
|
||||
|
||||
public record DraftSection(String label, String value) {}
|
||||
|
||||
public record TemplateRequest(String docType, String templateId) {}
|
||||
|
||||
public record AiCreateSessionResponse(
|
||||
String sessionId,
|
||||
String userId,
|
||||
String docType,
|
||||
String templateId,
|
||||
String promptInitial,
|
||||
String promptLatest,
|
||||
String outlineText,
|
||||
boolean outlineApproved,
|
||||
String outlineConstraints,
|
||||
String draftSections,
|
||||
String polishedLatex,
|
||||
String status) {
|
||||
public static AiCreateSessionResponse from(AiCreateSession session) {
|
||||
return new AiCreateSessionResponse(
|
||||
session.getSessionId(),
|
||||
session.getUserId(),
|
||||
session.getDocType(),
|
||||
session.getTemplateId(),
|
||||
session.getPromptInitial(),
|
||||
session.getPromptLatest(),
|
||||
session.getOutlineText(),
|
||||
session.isOutlineApproved(),
|
||||
session.getOutlineConstraints(),
|
||||
session.getDraftSections(),
|
||||
session.getPolishedLatex(),
|
||||
session.getStatus() != null ? session.getStatus().name() : null);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package stirling.software.proprietary.controller.api.ai;
|
||||
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import stirling.software.proprietary.model.ai.AiCreateSession;
|
||||
import stirling.software.proprietary.model.ai.AiCreateSessionStatus;
|
||||
import stirling.software.proprietary.service.ai.AiCreateSessionService;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/ai/create/internal")
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class AiCreateInternalController {
|
||||
|
||||
private final AiCreateSessionService sessionService;
|
||||
|
||||
@GetMapping("/sessions/{sessionId}")
|
||||
public ResponseEntity<AiCreateController.AiCreateSessionResponse> getSession(
|
||||
@PathVariable String sessionId) {
|
||||
log.info("AI create internal getSession sessionId={}", sessionId);
|
||||
AiCreateSession session = sessionService.getSession(sessionId);
|
||||
return ResponseEntity.ok(AiCreateController.AiCreateSessionResponse.from(session));
|
||||
}
|
||||
|
||||
@PostMapping("/sessions/{sessionId}/update")
|
||||
public ResponseEntity<AiCreateController.AiCreateSessionResponse> updateSession(
|
||||
@PathVariable String sessionId, @RequestBody UpdateSessionRequest request) {
|
||||
log.info("AI create internal updateSession sessionId={}", sessionId);
|
||||
AiCreateSession session =
|
||||
sessionService.applyInternalUpdate(
|
||||
sessionId,
|
||||
request.outlineText(),
|
||||
request.outlineApproved(),
|
||||
request.outlineConstraints(),
|
||||
request.draftSections(),
|
||||
request.polishedLatex(),
|
||||
request.docType(),
|
||||
request.templateId(),
|
||||
request.status());
|
||||
return ResponseEntity.ok(AiCreateController.AiCreateSessionResponse.from(session));
|
||||
}
|
||||
|
||||
public record UpdateSessionRequest(
|
||||
String outlineText,
|
||||
Boolean outlineApproved,
|
||||
String outlineConstraints,
|
||||
String draftSections,
|
||||
String polishedLatex,
|
||||
String docType,
|
||||
String templateId,
|
||||
AiCreateSessionStatus status) {}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package stirling.software.proprietary.model.ai;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.EnumType;
|
||||
import jakarta.persistence.Enumerated;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.Lob;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
import org.hibernate.annotations.CreationTimestamp;
|
||||
import org.hibernate.annotations.UpdateTimestamp;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Entity
|
||||
@Table(name = "ai_create_sessions")
|
||||
@Data
|
||||
public class AiCreateSession {
|
||||
@Id private String sessionId;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String userId;
|
||||
|
||||
private String docType;
|
||||
|
||||
private String templateId;
|
||||
|
||||
@Lob private String promptInitial;
|
||||
|
||||
@Lob private String promptLatest;
|
||||
|
||||
@Lob private String outlineText;
|
||||
|
||||
private boolean outlineApproved;
|
||||
|
||||
@Lob private String outlineConstraints;
|
||||
|
||||
@Lob private String draftSections;
|
||||
|
||||
@Lob private String polishedLatex;
|
||||
|
||||
@Enumerated(EnumType.STRING)
|
||||
@Column(nullable = false)
|
||||
private AiCreateSessionStatus status;
|
||||
|
||||
@CreationTimestamp private Instant createdAt;
|
||||
|
||||
@UpdateTimestamp private Instant updatedAt;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package stirling.software.proprietary.model.ai;
|
||||
|
||||
public enum AiCreateSessionStatus {
|
||||
OUTLINE_PENDING,
|
||||
OUTLINE_APPROVED,
|
||||
DRAFT_READY,
|
||||
POLISHED_READY,
|
||||
SAVED,
|
||||
SHARED
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package stirling.software.proprietary.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import stirling.software.proprietary.model.ai.AiCreateSession;
|
||||
|
||||
public interface AiCreateSessionRepository extends JpaRepository<AiCreateSession, String> {}
|
||||
@@ -0,0 +1,88 @@
|
||||
package stirling.software.proprietary.service.ai;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import stirling.software.common.model.ApplicationProperties;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class AiCreateProxyService {
|
||||
|
||||
private static final String DEFAULT_AI_BASE_URL = "http://localhost:5000";
|
||||
|
||||
private final ApplicationProperties applicationProperties;
|
||||
private final HttpClient httpClient;
|
||||
|
||||
public AiCreateProxyService(ApplicationProperties applicationProperties) {
|
||||
this.applicationProperties = applicationProperties;
|
||||
this.httpClient = HttpClient.newBuilder().build();
|
||||
}
|
||||
|
||||
public HttpResponse<InputStream> forward(
|
||||
String method, String path, HttpServletRequest request, boolean acceptEventStream)
|
||||
throws IOException, InterruptedException {
|
||||
String targetUrl = buildTargetUrl(path, request.getQueryString());
|
||||
HttpRequest.Builder builder = HttpRequest.newBuilder(URI.create(targetUrl));
|
||||
|
||||
String contentType = request.getContentType();
|
||||
if (contentType != null && !contentType.isBlank()) {
|
||||
builder.header("Content-Type", contentType);
|
||||
}
|
||||
|
||||
String accept = request.getHeader("Accept");
|
||||
if (acceptEventStream) {
|
||||
builder.header("Accept", "text/event-stream");
|
||||
} else if (accept != null && !accept.isBlank()) {
|
||||
builder.header("Accept", accept);
|
||||
}
|
||||
|
||||
builder.method(method, buildBodyPublisher(method, request));
|
||||
log.debug("Proxying AI create request {} {}", method, targetUrl);
|
||||
return httpClient.send(builder.build(), HttpResponse.BodyHandlers.ofInputStream());
|
||||
}
|
||||
|
||||
private String buildTargetUrl(String path, String queryString) {
|
||||
String baseUrl = applicationProperties.getSystem().getAiServiceBaseUrl();
|
||||
if (baseUrl == null || baseUrl.isBlank()) {
|
||||
baseUrl = DEFAULT_AI_BASE_URL;
|
||||
}
|
||||
baseUrl = baseUrl.trim();
|
||||
if (baseUrl.endsWith("/")) {
|
||||
baseUrl = baseUrl.substring(0, baseUrl.length() - 1);
|
||||
}
|
||||
if (!path.startsWith("/")) {
|
||||
path = "/" + path;
|
||||
}
|
||||
String url = baseUrl + path;
|
||||
if (queryString != null && !queryString.isBlank()) {
|
||||
url += "?" + queryString;
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
private HttpRequest.BodyPublisher buildBodyPublisher(String method, HttpServletRequest request) {
|
||||
if ("GET".equalsIgnoreCase(method) || "DELETE".equalsIgnoreCase(method)) {
|
||||
return HttpRequest.BodyPublishers.noBody();
|
||||
}
|
||||
return HttpRequest.BodyPublishers.ofInputStream(
|
||||
() -> {
|
||||
try {
|
||||
return request.getInputStream();
|
||||
} catch (IOException exc) {
|
||||
throw new UncheckedIOException(exc);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
package stirling.software.proprietary.service.ai;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import stirling.software.common.service.UserServiceInterface;
|
||||
import stirling.software.proprietary.model.ai.AiCreateSession;
|
||||
import stirling.software.proprietary.model.ai.AiCreateSessionStatus;
|
||||
import stirling.software.proprietary.repository.AiCreateSessionRepository;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class AiCreateSessionService {
|
||||
private static final String DEFAULT_USER_ID = "default_user";
|
||||
|
||||
private final AiCreateSessionRepository repository;
|
||||
|
||||
private final Optional<UserServiceInterface> userService;
|
||||
|
||||
public AiCreateSession createSession(String prompt, String docType, String templateId) {
|
||||
String userId = resolveUserId();
|
||||
AiCreateSession session = new AiCreateSession();
|
||||
session.setSessionId(UUID.randomUUID().toString());
|
||||
session.setUserId(userId);
|
||||
session.setDocType(docType);
|
||||
session.setTemplateId(templateId);
|
||||
session.setPromptInitial(prompt);
|
||||
session.setPromptLatest(prompt);
|
||||
session.setOutlineApproved(false);
|
||||
session.setStatus(AiCreateSessionStatus.OUTLINE_PENDING);
|
||||
return repository.save(session);
|
||||
}
|
||||
|
||||
public AiCreateSession getSession(String sessionId) {
|
||||
return repository
|
||||
.findById(sessionId)
|
||||
.orElseThrow(
|
||||
() ->
|
||||
new ResponseStatusException(
|
||||
HttpStatus.NOT_FOUND, "AI session not found"));
|
||||
}
|
||||
|
||||
public AiCreateSession getSessionForCurrentUser(String sessionId) {
|
||||
AiCreateSession session = getSession(sessionId);
|
||||
String userId = resolveUserId();
|
||||
if (!DEFAULT_USER_ID.equals(userId) && !userId.equals(session.getUserId())) {
|
||||
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "AI session not found");
|
||||
}
|
||||
return session;
|
||||
}
|
||||
|
||||
public AiCreateSession updateOutline(String sessionId, String outlineText, String outlineConstraints) {
|
||||
AiCreateSession session = getSessionForCurrentUser(sessionId);
|
||||
session.setOutlineText(outlineText);
|
||||
session.setOutlineApproved(true);
|
||||
if (outlineConstraints != null) {
|
||||
session.setOutlineConstraints(outlineConstraints);
|
||||
}
|
||||
session.setStatus(AiCreateSessionStatus.OUTLINE_APPROVED);
|
||||
return repository.save(session);
|
||||
}
|
||||
|
||||
public AiCreateSession updateDraftSections(String sessionId, String draftSections) {
|
||||
AiCreateSession session = getSessionForCurrentUser(sessionId);
|
||||
session.setDraftSections(draftSections);
|
||||
session.setStatus(AiCreateSessionStatus.DRAFT_READY);
|
||||
return repository.save(session);
|
||||
}
|
||||
|
||||
public AiCreateSession updateTemplate(String sessionId, String docType, String templateId) {
|
||||
AiCreateSession session = getSessionForCurrentUser(sessionId);
|
||||
if (docType != null && !docType.isBlank()) {
|
||||
session.setDocType(docType);
|
||||
}
|
||||
if (templateId != null && !templateId.isBlank()) {
|
||||
session.setTemplateId(templateId);
|
||||
}
|
||||
return repository.save(session);
|
||||
}
|
||||
|
||||
public AiCreateSession reprompt(String sessionId, String prompt) {
|
||||
AiCreateSession session = getSessionForCurrentUser(sessionId);
|
||||
session.setPromptLatest(prompt);
|
||||
session.setOutlineText(null);
|
||||
session.setOutlineApproved(false);
|
||||
session.setOutlineConstraints(null);
|
||||
session.setDraftSections(null);
|
||||
session.setPolishedLatex(null);
|
||||
session.setStatus(AiCreateSessionStatus.OUTLINE_PENDING);
|
||||
return repository.save(session);
|
||||
}
|
||||
|
||||
public AiCreateSession applyInternalUpdate(
|
||||
String sessionId,
|
||||
String outlineText,
|
||||
Boolean outlineApproved,
|
||||
String outlineConstraints,
|
||||
String draftSections,
|
||||
String polishedLatex,
|
||||
String docType,
|
||||
String templateId,
|
||||
AiCreateSessionStatus status) {
|
||||
AiCreateSession session = getSession(sessionId);
|
||||
if (outlineText != null) {
|
||||
session.setOutlineText(outlineText);
|
||||
}
|
||||
if (outlineApproved != null) {
|
||||
session.setOutlineApproved(outlineApproved);
|
||||
}
|
||||
if (outlineConstraints != null) {
|
||||
session.setOutlineConstraints(outlineConstraints);
|
||||
}
|
||||
if (draftSections != null) {
|
||||
session.setDraftSections(draftSections);
|
||||
}
|
||||
if (polishedLatex != null) {
|
||||
session.setPolishedLatex(polishedLatex);
|
||||
}
|
||||
if (docType != null) {
|
||||
session.setDocType(docType);
|
||||
}
|
||||
if (templateId != null) {
|
||||
session.setTemplateId(templateId);
|
||||
}
|
||||
if (status != null) {
|
||||
session.setStatus(status);
|
||||
}
|
||||
return repository.save(session);
|
||||
}
|
||||
|
||||
public String resolveUserId() {
|
||||
if (userService == null || userService.isEmpty()) {
|
||||
return DEFAULT_USER_ID;
|
||||
}
|
||||
try {
|
||||
String username = userService.get().getCurrentUsername();
|
||||
if (username != null
|
||||
&& !username.isBlank()
|
||||
&& !"anonymousUser".equals(username)) {
|
||||
return username;
|
||||
}
|
||||
} catch (Exception exc) {
|
||||
log.debug("Failed to resolve current username: {}", exc.getMessage());
|
||||
}
|
||||
return DEFAULT_USER_ID;
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
# syntax=docker/dockerfile:1.5
|
||||
# ==============================================================================
|
||||
# Multi-stage Dockerfile for Stirling-PDF – image with everything included
|
||||
# Includes: LibreOffice, Calibre, Tesseract, OCRmyPDF, unoserver, WeasyPrint, etc.
|
||||
@@ -15,7 +16,8 @@ COPY gradle gradle/
|
||||
COPY app/core/build.gradle core/.
|
||||
COPY app/common/build.gradle common/.
|
||||
COPY app/proprietary/build.gradle proprietary/.
|
||||
RUN ./gradlew build -x spotlessApply -x spotlessCheck -x test -x sonarqube || return 0
|
||||
RUN --mount=type=cache,target=/home/gradle/.gradle \
|
||||
./gradlew build -x spotlessApply -x spotlessCheck -x test -x sonarqube || return 0
|
||||
|
||||
# Set the working directory
|
||||
WORKDIR /app
|
||||
@@ -24,7 +26,8 @@ WORKDIR /app
|
||||
COPY . .
|
||||
|
||||
# Build the application (server-only JAR - no UI, includes security features controlled at runtime)
|
||||
RUN DISABLE_ADDITIONAL_FEATURES=false \
|
||||
RUN --mount=type=cache,target=/home/gradle/.gradle \
|
||||
DISABLE_ADDITIONAL_FEATURES=false \
|
||||
STIRLING_PDF_DESKTOP_UI=false \
|
||||
./gradlew clean build -x spotlessApply -x spotlessCheck -x test -x sonarqube
|
||||
|
||||
@@ -40,7 +43,8 @@ ENV DEBIAN_FRONTEND=noninteractive
|
||||
ENV TESS_BASE_PATH=/usr/share/tesseract-ocr/5/tessdata
|
||||
|
||||
# Install core runtime dependencies + tools required by Stirling-PDF features
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
|
||||
apt-get update && apt-get install -y --no-install-recommends \
|
||||
ca-certificates tzdata tini bash fontconfig \
|
||||
openjdk-21-jre-headless \
|
||||
ffmpeg poppler-utils ocrmypdf \
|
||||
@@ -146,13 +150,15 @@ ENV VERSION_TAG=$VERSION_TAG \
|
||||
# ==============================================================================
|
||||
# Python virtual environment for additional Python tools (WeasyPrint, OpenCV, etc.)
|
||||
# ==============================================================================
|
||||
RUN python3 -m venv /opt/venv --system-site-packages \
|
||||
&& /opt/venv/bin/pip install --no-cache-dir weasyprint pdf2image opencv-python-headless \
|
||||
RUN --mount=type=cache,target=/root/.cache/pip \
|
||||
python3 -m venv /opt/venv --system-site-packages \
|
||||
&& /opt/venv/bin/pip install weasyprint pdf2image opencv-python-headless \
|
||||
&& /opt/venv/bin/python -c "import cv2; print('OpenCV version:', cv2.__version__)"
|
||||
|
||||
# Separate venv for unoserver (keeps it isolated)
|
||||
RUN python3 -m venv /opt/unoserver-venv --system-site-packages \
|
||||
&& /opt/unoserver-venv/bin/pip install --no-cache-dir unoserver
|
||||
RUN --mount=type=cache,target=/root/.cache/pip \
|
||||
python3 -m venv /opt/unoserver-venv --system-site-packages \
|
||||
&& /opt/unoserver-venv/bin/pip install unoserver
|
||||
|
||||
# Make unoserver tools available in main venv PATH
|
||||
RUN ln -sf /opt/unoserver-venv/bin/unoconvert /opt/venv/bin/unoconvert \
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
services:
|
||||
backend:
|
||||
build:
|
||||
context: ../..
|
||||
dockerfile: docker/backend/Dockerfile
|
||||
container_name: stirling-pdf-backend
|
||||
restart: on-failure:5
|
||||
ports:
|
||||
- "8080:8080"
|
||||
environment:
|
||||
DISABLE_ADDITIONAL_FEATURES: "true"
|
||||
SECURITY_ENABLELOGIN: "false"
|
||||
SYSTEM_AISERVICEBASEURL: "http://ai-backend:5000"
|
||||
volumes:
|
||||
- ../../stirling/ai/data:/usr/share/tessdata:rw
|
||||
- ../../stirling/ai/config:/configs:rw
|
||||
- ../../stirling/ai/logs:/logs:rw
|
||||
depends_on:
|
||||
- ai-backend
|
||||
networks:
|
||||
- ai-stack
|
||||
|
||||
ai-backend:
|
||||
build:
|
||||
context: ../../AI-Document-Generator-main
|
||||
dockerfile: Dockerfile
|
||||
container_name: ai-document-generator-backend
|
||||
restart: on-failure:5
|
||||
ports:
|
||||
- "5000:5000"
|
||||
volumes:
|
||||
- ../../AI-Document-Generator-main/backend/data:/app/data:rw
|
||||
- ../../AI-Document-Generator-main/backend/output:/app/output:rw
|
||||
environment:
|
||||
- OPENAI_API_KEY=ollama
|
||||
- OPENAI_BASE_URL=http://ollama:11434/v1
|
||||
- SMART_MODEL=qwen3-vl:8b
|
||||
- FAST_MODEL=qwen3-vl:8b
|
||||
depends_on:
|
||||
- ollama
|
||||
networks:
|
||||
- ai-stack
|
||||
|
||||
ai-frontend:
|
||||
image: node:20-alpine
|
||||
container_name: ai-document-generator-frontend
|
||||
working_dir: /app
|
||||
ports:
|
||||
- "3000:3000"
|
||||
volumes:
|
||||
- ../../AI-Document-Generator-main/frontend:/app:rw
|
||||
- /app/node_modules
|
||||
command: sh -c "npm install && npm run dev -- --host"
|
||||
environment:
|
||||
DOCKER_ENV: "true"
|
||||
depends_on:
|
||||
- backend
|
||||
networks:
|
||||
- ai-stack
|
||||
|
||||
ollama:
|
||||
image: ollama/ollama:latest
|
||||
container_name: ollama
|
||||
restart: unless-stopped
|
||||
gpus: all
|
||||
ports:
|
||||
- "11434:11434"
|
||||
volumes:
|
||||
- ollama-data:/root/.ollama
|
||||
command: ["serve"]
|
||||
networks:
|
||||
- ai-stack
|
||||
|
||||
volumes:
|
||||
ollama-data:
|
||||
|
||||
networks:
|
||||
ai-stack:
|
||||
driver: bridge
|
||||
@@ -0,0 +1,61 @@
|
||||
services:
|
||||
backend:
|
||||
build:
|
||||
context: ../..
|
||||
dockerfile: docker/backend/Dockerfile
|
||||
container_name: stirling-pdf-backend
|
||||
restart: on-failure:5
|
||||
ports:
|
||||
- "8080:8080"
|
||||
environment:
|
||||
DISABLE_ADDITIONAL_FEATURES: "true"
|
||||
SECURITY_ENABLELOGIN: "false"
|
||||
SYSTEM_AISERVICEBASEURL: "http://ai-backend:5000"
|
||||
volumes:
|
||||
- ../../stirling/ai/data:/usr/share/tessdata:rw
|
||||
- ../../stirling/ai/config:/configs:rw
|
||||
- ../../stirling/ai/logs:/logs:rw
|
||||
depends_on:
|
||||
- ai-backend
|
||||
networks:
|
||||
- ai-stack
|
||||
|
||||
ai-backend:
|
||||
build:
|
||||
context: ../../AI-Document-Generator-main
|
||||
dockerfile: Dockerfile
|
||||
container_name: ai-document-generator-backend
|
||||
restart: on-failure:5
|
||||
ports:
|
||||
- "5000:5000"
|
||||
volumes:
|
||||
- ../../AI-Document-Generator-main/backend/data:/app/data:rw
|
||||
- ../../AI-Document-Generator-main/backend/output:/app/output:rw
|
||||
environment:
|
||||
- OPENAI_API_KEY=sk-proj-mVg... etc please insert
|
||||
- SMART_MODEL=gpt-5.1
|
||||
- FAST_MODEL=gpt-4.1-nano
|
||||
- JAVA_BACKEND_URL=http://backend:8080
|
||||
networks:
|
||||
- ai-stack
|
||||
|
||||
ai-frontend:
|
||||
image: node:20-alpine
|
||||
container_name: ai-document-generator-frontend
|
||||
working_dir: /app
|
||||
ports:
|
||||
- "3000:3000"
|
||||
volumes:
|
||||
- ../../AI-Document-Generator-main/frontend:/app:rw
|
||||
- /app/node_modules
|
||||
command: sh -c "npm install && npm run dev -- --host"
|
||||
environment:
|
||||
DOCKER_ENV: "true"
|
||||
depends_on:
|
||||
- backend
|
||||
networks:
|
||||
- ai-stack
|
||||
|
||||
networks:
|
||||
ai-stack:
|
||||
driver: bridge
|
||||
@@ -0,0 +1,192 @@
|
||||
# Stirling PDF AI: LaTeX-First Design Plan
|
||||
|
||||
## End Goal
|
||||
Build a "Create with AI" experience inside Stirling PDF that matches the UX in `AI.pdf`: a staged
|
||||
workflow with Outline -> Rough Draft -> Polished Template -> Share. The backend remains Java as the
|
||||
source of truth. A Python LangChain service runs alongside Java and generates LaTeX. The frontend is
|
||||
Vite React.
|
||||
|
||||
The system must:
|
||||
- Keep LaTeX templates as the output format (not JSON templates).
|
||||
- Store AI session state in Java (not in Python).
|
||||
- Stream "typing" behavior and stage transitions to the UI.
|
||||
- Provide a share-first flow (link by default, optional email).
|
||||
|
||||
## Core Architecture
|
||||
Java API (source of truth)
|
||||
- Authentication, tenancy, rate limits.
|
||||
- Session storage and state transitions.
|
||||
- Template registry and ownership.
|
||||
- File storage and "Saved" docs.
|
||||
- Share links and email.
|
||||
|
||||
Python LangChain service (AI orchestrator)
|
||||
- Runs a durable, resumable workflow using LangGraph.
|
||||
- Generates LaTeX for Outline, Draft, and Polished stages.
|
||||
- Uses Java endpoints as tools for saving, sharing, and template retrieval.
|
||||
- Streams SSE events to the frontend (via Java proxy).
|
||||
|
||||
Vite React frontend
|
||||
- Implements the UX from `AI.pdf`.
|
||||
- Consumes SSE for live typing and stage transitions.
|
||||
- Allows outline editing and "Approve and Continue."
|
||||
|
||||
## UX Stages (matches `AI.pdf`)
|
||||
Stage 1: Outline
|
||||
- UI shows outline with section titles and short details.
|
||||
- User can edit any section.
|
||||
- "Approve and Continue" triggers the next stage.
|
||||
- Input box remains visible for reprompt.
|
||||
- Edit/lock rules within stages are MVP-flexible and will be refined later.
|
||||
|
||||
Stage 2: Rough Draft
|
||||
- AI fills a full rough draft from the approved outline.
|
||||
- Typing animation is fast.
|
||||
- Input box is hidden at top per spec.
|
||||
|
||||
Stage 3: Polished Template
|
||||
- AI applies a LaTeX template for the chosen doc type.
|
||||
- Style edits and locking rules are TBD for MVP.
|
||||
- Company templates available (pro tier).
|
||||
|
||||
Stage 4: Share (post-MVP)
|
||||
- MVP can be download-only.
|
||||
- Share links and email are post-MVP.
|
||||
|
||||
## Data Model (Java)
|
||||
Session
|
||||
- session_id
|
||||
- user_id
|
||||
- team_id
|
||||
- doc_type
|
||||
- prompt_initial
|
||||
- outline_text
|
||||
- outline_approved: boolean
|
||||
- draft_latex
|
||||
- polished_latex
|
||||
- template_id
|
||||
- status: OUTLINE_PENDING | OUTLINE_APPROVED | DRAFT_READY | POLISHED_READY | SAVED | SHARED
|
||||
- created_at
|
||||
- updated_at
|
||||
|
||||
Templates
|
||||
- template_id
|
||||
- owner_id (team visibility rules TBD)
|
||||
- doc_type
|
||||
- latex_source
|
||||
- created_at
|
||||
- updated_at
|
||||
|
||||
## LaTeX Template Strategy
|
||||
Templates are pure LaTeX files with placeholder markers.
|
||||
- Example marker convention: `<<SECTION_NAME>>`.
|
||||
- "ApplyTemplate" step replaces placeholders using LLM or a strict prompt.
|
||||
- Draft output is minimal LaTeX.
|
||||
- Polished output is full template LaTeX.
|
||||
|
||||
LLM prompt rule:
|
||||
- "Only replace placeholders, do not alter layout commands unless explicitly allowed."
|
||||
|
||||
## API Contracts
|
||||
Frontend -> Java (public)
|
||||
- POST /ai/sessions
|
||||
body: { prompt, docType?, templateId? }
|
||||
returns: { sessionId }
|
||||
- GET /ai/sessions/:id/stream
|
||||
SSE proxy from Python
|
||||
- POST /ai/sessions/:id/outline
|
||||
body: { outlineText }
|
||||
- POST /ai/sessions/:id/reprompt
|
||||
body: { prompt }
|
||||
- POST /ai/sessions/:id/share (post-MVP)
|
||||
body: { email? }
|
||||
|
||||
Python -> Java (internal tools)
|
||||
- GET /internal/ai/templates/:docType
|
||||
returns: { templateId, latex }
|
||||
- POST /internal/ai/sessions/:id/update
|
||||
body: { phase, outlineText?, draftLatex?, polishedLatex? }
|
||||
- POST /internal/ai/sessions/:id/save
|
||||
body: { polishedLatex, docType }
|
||||
returns: { docId, shareLink }
|
||||
|
||||
## SSE Event Schema
|
||||
SSE events for UI animation and stage transitions.
|
||||
- phase_changed
|
||||
data: { phase: "outline" | "draft" | "polish" | "share" }
|
||||
- latex_delta
|
||||
data: { phase, delta }
|
||||
- outline_ready
|
||||
data: { outlineText }
|
||||
- phase_complete
|
||||
data: { phase, latex? }
|
||||
- save_complete
|
||||
data: { docId, shareLink }
|
||||
|
||||
Frontend behaviors:
|
||||
- "typing" uses latex_delta chunks.
|
||||
- phase transitions animate per `AI.pdf`.
|
||||
|
||||
## LangGraph Flow (Python)
|
||||
Nodes
|
||||
- ClassifyDocType
|
||||
- GenerateOutline
|
||||
- WaitForOutlineApproval
|
||||
- GenerateDraft
|
||||
- ApplyTemplate
|
||||
- SaveAndReturn
|
||||
Note: Node boundaries are provisional for MVP and may change.
|
||||
|
||||
State
|
||||
- sessionId
|
||||
- userId
|
||||
- docType
|
||||
- prompt
|
||||
- outlineText
|
||||
- draftLatex
|
||||
- polishedLatex
|
||||
- templateId
|
||||
|
||||
All persistence writes happen by calling Java.
|
||||
|
||||
## Security Model
|
||||
- Frontend authenticates only to Java.
|
||||
- Java proxies SSE and mints internal tokens for Python.
|
||||
- Python calls internal Java endpoints with internal auth.
|
||||
- Java validates permissions and ownership.
|
||||
|
||||
## Implementation Stages
|
||||
Phase 1: Skeleton
|
||||
- Stand up Python LangGraph service.
|
||||
- Implement create session + outline generation.
|
||||
- Add SSE streaming for outline stage.
|
||||
- Store sessions in Java.
|
||||
|
||||
Phase 2: Draft + Polish
|
||||
- Add draft generation from approved outline.
|
||||
- Add template application with LaTeX placeholders.
|
||||
- Stream typing for each stage.
|
||||
|
||||
Phase 3: Share + Save
|
||||
- Java stores polished LaTeX + PDF.
|
||||
- MVP can be download-only; share is post-MVP.
|
||||
|
||||
Phase 4: Hardening
|
||||
- Rate limits on AI endpoints.
|
||||
- Guardrails for documents that need factual accuracy.
|
||||
- Basic regression tests for outline/draft outputs.
|
||||
|
||||
## Migration Notes From Existing AI Folder
|
||||
The current AI-Document-Generator backend already streams LaTeX chunks and compiles PDFs. Keep that
|
||||
flow but rewire it into the staged LangGraph workflow and make Java the system of record.
|
||||
|
||||
## Non-Goals
|
||||
- JSON-based document templates.
|
||||
- Storing session state in Python.
|
||||
- Using a single-step prompt without stage gates.
|
||||
|
||||
## Success Criteria
|
||||
- UX matches the `AI.pdf` outline/draft/polish/share flow.
|
||||
- LaTeX templates drive final output.
|
||||
- Java owns all sessions and storage.
|
||||
- Streaming feels fluid and staged.
|
||||
@@ -0,0 +1,249 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Overleaf Template Scraper
|
||||
Downloads all templates from Overleaf gallery organized by category and license.
|
||||
|
||||
Requirements:
|
||||
pip install requests beautifulsoup4 lxml
|
||||
|
||||
Note: This scraper collects template metadata and GitHub links where available.
|
||||
For actual template downloads, you'll need Overleaf credentials due to their
|
||||
requirement to "Open as Template" before downloading.
|
||||
"""
|
||||
|
||||
import os
|
||||
import json
|
||||
import time
|
||||
import requests
|
||||
from bs4 import BeautifulSoup
|
||||
from pathlib import Path
|
||||
from urllib.parse import urljoin, urlparse
|
||||
from typing import Dict, List, Optional
|
||||
|
||||
|
||||
class OverleafTemplateScraper:
|
||||
def __init__(self, output_dir: str = "./overleaf_templates"):
|
||||
self.base_url = "https://www.overleaf.com"
|
||||
self.output_dir = Path(output_dir)
|
||||
self.output_dir.mkdir(exist_ok=True)
|
||||
self.session = requests.Session()
|
||||
self.session.headers.update({
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
|
||||
})
|
||||
|
||||
def get_template_pages(self, max_pages: Optional[int] = None) -> List[str]:
|
||||
"""Get all template gallery page URLs."""
|
||||
print("Discovering template pages...")
|
||||
pages = []
|
||||
|
||||
# Start with page 1
|
||||
for page_num in range(1, (max_pages or 911) + 1):
|
||||
page_url = f"{self.base_url}/latex/templates?page={page_num}"
|
||||
pages.append(page_url)
|
||||
|
||||
print(f"Found {len(pages)} pages to scrape")
|
||||
return pages
|
||||
|
||||
def scrape_template_list_page(self, page_url: str) -> List[Dict]:
|
||||
"""Scrape a single template listing page."""
|
||||
print(f"Scraping {page_url}")
|
||||
templates = []
|
||||
|
||||
try:
|
||||
response = self.session.get(page_url, timeout=30)
|
||||
response.raise_for_status()
|
||||
soup = BeautifulSoup(response.content, 'lxml')
|
||||
|
||||
# Find all template cards
|
||||
template_links = soup.find_all('a', href=lambda x: x and '/latex/templates/' in x and len(x.split('/')) >= 5)
|
||||
|
||||
for link in template_links:
|
||||
href = link.get('href')
|
||||
if href and '/latex/templates/' in href and href.count('/') >= 4:
|
||||
full_url = urljoin(self.base_url, href)
|
||||
if full_url not in [t['url'] for t in templates]:
|
||||
templates.append({
|
||||
'url': full_url,
|
||||
'title': link.get_text(strip=True) or 'Unknown'
|
||||
})
|
||||
|
||||
print(f" Found {len(templates)} templates on this page")
|
||||
|
||||
except Exception as e:
|
||||
print(f" Error scraping page: {e}")
|
||||
|
||||
return templates
|
||||
|
||||
def scrape_template_details(self, template_url: str) -> Dict:
|
||||
"""Scrape detailed information from a template page."""
|
||||
print(f" Fetching details: {template_url}")
|
||||
details = {
|
||||
'url': template_url,
|
||||
'title': '',
|
||||
'author': '',
|
||||
'license': '',
|
||||
'description': '',
|
||||
'last_updated': '',
|
||||
'tags': [],
|
||||
'github_url': None,
|
||||
'view_source_url': None
|
||||
}
|
||||
|
||||
try:
|
||||
response = self.session.get(template_url, timeout=30)
|
||||
response.raise_for_status()
|
||||
soup = BeautifulSoup(response.content, 'lxml')
|
||||
|
||||
# Extract title
|
||||
title = soup.find('h1')
|
||||
if title:
|
||||
details['title'] = title.get_text(strip=True)
|
||||
|
||||
# Extract license (look for CC BY, LPPL, etc.)
|
||||
license_text = soup.find(string=lambda x: x and ('CC BY' in x or 'LPPL' in x or 'MIT' in x or 'Public Domain' in x))
|
||||
if license_text:
|
||||
details['license'] = license_text.strip()
|
||||
|
||||
# Look for metadata section
|
||||
metadata = soup.find_all(['dt', 'dd'])
|
||||
for i in range(0, len(metadata)-1, 2):
|
||||
key = metadata[i].get_text(strip=True).lower()
|
||||
value = metadata[i+1].get_text(strip=True)
|
||||
|
||||
if 'author' in key:
|
||||
details['author'] = value
|
||||
elif 'license' in key:
|
||||
details['license'] = value
|
||||
elif 'updated' in key or 'modified' in key:
|
||||
details['last_updated'] = value
|
||||
|
||||
# Find GitHub link
|
||||
github_link = soup.find('a', href=lambda x: x and 'github.com' in x)
|
||||
if github_link:
|
||||
details['github_url'] = github_link.get('href')
|
||||
|
||||
# Find description/abstract
|
||||
abstract = soup.find(['p', 'div'], class_=lambda x: x and 'abstract' in x.lower() if x else False)
|
||||
if abstract:
|
||||
details['description'] = abstract.get_text(strip=True)
|
||||
|
||||
# Find tags/categories
|
||||
tags = soup.find_all('a', href=lambda x: x and '/latex/templates/tagged/' in x)
|
||||
details['tags'] = [tag.get_text(strip=True) for tag in tags]
|
||||
|
||||
except Exception as e:
|
||||
print(f" Error fetching template details: {e}")
|
||||
|
||||
return details
|
||||
|
||||
def save_template_metadata(self, template: Dict, category: str = "general"):
|
||||
"""Save template metadata to JSON file."""
|
||||
category_dir = self.output_dir / category
|
||||
category_dir.mkdir(exist_ok=True)
|
||||
|
||||
# Use template ID from URL as filename
|
||||
template_id = template['url'].split('/')[-1]
|
||||
filename = category_dir / f"{template_id}.json"
|
||||
|
||||
with open(filename, 'w', encoding='utf-8') as f:
|
||||
json.dump(template, f, indent=2, ensure_ascii=False)
|
||||
|
||||
def scrape_all_templates(self, max_pages: Optional[int] = 10, delay: float = 1.0):
|
||||
"""
|
||||
Scrape all templates from Overleaf.
|
||||
|
||||
Args:
|
||||
max_pages: Maximum number of gallery pages to scrape (None for all 911 pages)
|
||||
delay: Delay between requests in seconds
|
||||
"""
|
||||
print("Starting Overleaf template scraper...")
|
||||
print(f"Output directory: {self.output_dir}")
|
||||
|
||||
# Get all gallery pages
|
||||
gallery_pages = self.get_template_pages(max_pages)
|
||||
|
||||
all_templates = []
|
||||
templates_by_license = {
|
||||
'CC BY': [],
|
||||
'CC BY-SA': [],
|
||||
'CC BY-NC': [],
|
||||
'CC BY-NC-SA': [],
|
||||
'LPPL': [],
|
||||
'MIT': [],
|
||||
'Public Domain': [],
|
||||
'Other': []
|
||||
}
|
||||
|
||||
# Scrape each gallery page
|
||||
for page_url in gallery_pages:
|
||||
templates = self.scrape_template_list_page(page_url)
|
||||
|
||||
# Get details for each template
|
||||
for template_basic in templates:
|
||||
template_details = self.scrape_template_details(template_basic['url'])
|
||||
all_templates.append(template_details)
|
||||
|
||||
# Categorize by license
|
||||
license_key = 'Other'
|
||||
for key in templates_by_license.keys():
|
||||
if key in template_details.get('license', ''):
|
||||
license_key = key
|
||||
break
|
||||
|
||||
templates_by_license[license_key].append(template_details)
|
||||
|
||||
# Save individual template metadata
|
||||
self.save_template_metadata(template_details, license_key)
|
||||
|
||||
time.sleep(delay) # Be respectful
|
||||
|
||||
time.sleep(delay)
|
||||
|
||||
# Save summary
|
||||
summary = {
|
||||
'total_templates': len(all_templates),
|
||||
'by_license': {k: len(v) for k, v in templates_by_license.items()},
|
||||
'templates': all_templates
|
||||
}
|
||||
|
||||
summary_file = self.output_dir / 'summary.json'
|
||||
with open(summary_file, 'w', encoding='utf-8') as f:
|
||||
json.dump(summary, f, indent=2, ensure_ascii=False)
|
||||
|
||||
print("\n" + "="*60)
|
||||
print("SCRAPING COMPLETE!")
|
||||
print("="*60)
|
||||
print(f"Total templates: {summary['total_templates']}")
|
||||
print("\nBy license:")
|
||||
for license_type, count in summary['by_license'].items():
|
||||
if count > 0:
|
||||
print(f" {license_type}: {count}")
|
||||
print(f"\nCommercially usable (CC BY, CC BY-SA, LPPL, MIT, Public Domain): "
|
||||
f"{sum(summary['by_license'][k] for k in ['CC BY', 'CC BY-SA', 'LPPL', 'MIT', 'Public Domain'])}")
|
||||
print(f"\nMetadata saved to: {self.output_dir}")
|
||||
print("\nNOTE: To download actual template files, you'll need to:")
|
||||
print("1. Use the GitHub URLs where available")
|
||||
print("2. Or manually open templates in Overleaf with credentials")
|
||||
|
||||
return summary
|
||||
|
||||
|
||||
def main():
|
||||
import argparse
|
||||
|
||||
parser = argparse.ArgumentParser(description='Scrape Overleaf template gallery')
|
||||
parser.add_argument('--max-pages', type=int, default=10,
|
||||
help='Maximum number of pages to scrape (default: 10, use 911 for all)')
|
||||
parser.add_argument('--delay', type=float, default=1.0,
|
||||
help='Delay between requests in seconds (default: 1.0)')
|
||||
parser.add_argument('--output', type=str, default='./overleaf_templates',
|
||||
help='Output directory (default: ./overleaf_templates)')
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
scraper = OverleafTemplateScraper(output_dir=args.output)
|
||||
scraper.scrape_all_templates(max_pages=args.max_pages, delay=args.delay)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
After Width: | Height: | Size: 28 KiB |