Compare commits

...
Author SHA1 Message Date
Anthony Stirling 8ad3b347ef Add visually-hidden h1 headings to login and signup pages 2026-06-14 20:56:43 +01:00
085ad6c784 build(taskfile): use platform-specific Gradle wrapper command (#6355)
Co-authored-by: James Brunton <jbrunton96@gmail.com>
2026-06-14 15:50:31 +01:00
LudyandGitHub cb13102117 chore(frontend): make Vite allowed hosts configurable (#6354) 2026-06-14 15:49:45 +01:00
LudyandGitHub 1fa1293b39 chore(build): upgrade Gradle wrapper and Docker build images to 9.5.1 (#6501) 2026-06-14 15:48:44 +01:00
LudyandGitHub 1ce765ab1e refactor: replace legacy Paths usage with Path.of (#6441) 2026-06-14 15:48:28 +01:00
LudyandGitHub dad2425c27 docs: Document pluralization suffix usage for translations (#6650) 2026-06-14 15:47:57 +01:00
Anthony StirlingandGitHub 946c032fb5 Change default language to en-US and add US language (#6621) 2026-06-11 20:36:23 +01:00
88adb7adad create agent (#6520)
Added the create agent. Use [these
prompts](https://github.com/Stirling-Tools/Stirling-PDF-SaaS/blob/main/docgen/backend/default_templates/sample_prompts.md)
to test or try your own :)

Here’s the one I use

```
Hey, I need to generate an employee expense report for reimbursement.
Company: Summit Consulting Partners Company address: 88 Riverside Plaza, Suite 1400, New York, NY 10069 Accounting department email: expenses@example.com
Employee details:
* Employee Name: Michael Tran
* Employee ID: EMP-1047
* Department: Client Services
* Report Date: January 20th, 2026
* Reporting Period: January 5th, 2026 – January 16th, 2026
* Manager Approver: Laura Simmons
Trip purpose: Client onsite meetings with Atlantic Energy Solutions in Boston, MA.
Expense items:
* Flight (NYC to Boston roundtrip) — $325.40 — January 5th, 2026 — Airline ticket
* Hotel (3 nights at Harborview Hotel) — $822.75 — January 5th-8th, 2026
* Taxi from airport to hotel — $48.00 — January 5th, 2026
* Client dinner (3 attendees) — $186.20 — January 6th, 2026
* Parking at JFK Airport — $72.00 — January 5th-8th, 2026
* Breakfast (per diem not used) — $18.50 — January 7th, 2026
* Uber to client office — $22.10 — January 7th, 2026
* Printing + presentation materials — $46.90 — January 8th, 2026
* Lunch with client — $39.75 — January 8th, 2026
* Office supplies (notebooks, pens) — $27.60 — January 10th, 2026
* Mileage reimbursement (client visit in NJ, 42 miles @ $0.67/mile) — $28.14 — January 14th, 2026
* Team lunch meeting (internal) — $64.30 — January 15th, 2026
Reimbursement method should be direct deposit.
Add a notes section stating: "All receipts attached. Expenses are business-related and comply with company travel policy."
```

---------

Co-authored-by: Anthony Stirling <77850077+frooodle@users.noreply.github.com>
2026-06-11 14:18:13 +00:00
137 changed files with 10778 additions and 567 deletions
+13 -1
View File
@@ -1,6 +1,8 @@
build: &build
- build.gradle
- app/(common|core|proprietary)/build.gradle
- Taskfile.yml
- .taskfiles/backend.yml
openapi: &openapi
- *build
@@ -38,6 +40,9 @@ project: &project
- frontend/**
- docker/**
- scripts/RestartHelper.java
- Taskfile.yml
- .taskfiles/backend.yml
- .taskfiles/docker.yml
- scripts/db-migration/**
- .github/workflows/db-migration-test.yml
@@ -55,6 +60,9 @@ frontend: &frontend
- scripts/summarize_type3_signatures.py
- scripts/type3_to_cff.py
- scripts/update_type3_library.py
- Taskfile.yml
- .taskfiles/frontend.yml
- .taskfiles/e2e.yml
# Files that affect the Tauri desktop bundle. Gate the multi-OS Tauri build
# job on changes to any of these.
@@ -66,6 +74,8 @@ tauri: &tauri
- frontend/package-lock.json
- frontend/editor/vite.config.ts
- .github/workflows/tauri-build.yml
- Taskfile.yml
- .taskfiles/desktop.yml
# Files that affect the AI engine (Python tool models, fixers, tests). Gate
# the engine validation job on changes to engine sources or to the Java
@@ -74,6 +84,8 @@ engine: &engine
- engine/**
- app/(common|core|proprietary)/src/main/java/**
- .github/workflows/ai-engine.yml
- Taskfile.yml
- .taskfiles/engine.yml
licenses-frontend: &licenses-frontend
- ".github/workflows/frontend-backend-licenses-update.yml"
@@ -102,4 +114,4 @@ proprietary: &proprietary
- configs/settings.yml.template
- build.gradle
- app/proprietary/build.gradle
- .github/workflows/build-enterprise.yml
- .github/workflows/build-enterprise.yml
+3 -3
View File
@@ -13,7 +13,7 @@ Usage:
"""
# Sample for Windows:
# python .github/scripts/check_language_toml.py --reference-file frontend/editor/public/locales/en-GB/translation.toml --branch "" --files frontend/editor/public/locales/de-DE/translation.toml frontend/editor/public/locales/fr-FR/translation.toml
# python .github/scripts/check_language_toml.py --reference-file frontend/editor/public/locales/en-US/translation.toml --branch "" --files frontend/editor/public/locales/de-DE/translation.toml frontend/editor/public/locales/fr-FR/translation.toml
import argparse
import glob
@@ -211,7 +211,7 @@ def check_for_differences(reference_file, file_list, branch, actor):
)
continue
if basename_current_file == basename_reference_file and locale_dir == "en-GB":
if basename_current_file == basename_reference_file and locale_dir == "en-US":
continue
if (
@@ -308,7 +308,7 @@ def check_for_differences(reference_file, file_list, branch, actor):
report.append("## ❌ Overall Check Status: **_Failed_**")
report.append("")
report.append(
f"@{actor} please check your translation if it conforms to the standard. Follow the format of [en-GB/translation.toml](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/frontend/editor/public/locales/en-GB/translation.toml)"
f"@{actor} please check your translation if it conforms to the standard. Follow the format of [en-US/translation.toml](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/frontend/editor/public/locales/en-US/translation.toml)"
)
else:
report.append("## ✅ Overall Check Status: **_Success_**")
+1 -1
View File
@@ -293,7 +293,7 @@ jobs:
SECURITY_ENABLELOGIN: "true"
SECURITY_INITIALLOGIN_USERNAME: "${{ secrets.TEST_LOGIN_USERNAME }}"
SECURITY_INITIALLOGIN_PASSWORD: "${{ secrets.TEST_LOGIN_PASSWORD }}"
SYSTEM_DEFAULTLOCALE: en-GB
SYSTEM_DEFAULTLOCALE: en-US
UI_APPNAME: "Stirling-PDF V2 PR#${{ needs.check-pr.outputs.pr_number }}"
UI_HOMEDESCRIPTION: "V2 PR#${{ needs.check-pr.outputs.pr_number }} - Embedded Architecture"
UI_APPNAMENAVBAR: "V2 PR#${{ needs.check-pr.outputs.pr_number }}"
@@ -222,7 +222,7 @@ jobs:
- name: Setup Gradle
uses: gradle/actions/setup-gradle@50e97c2cd7a37755bbfafc9c5b7cafaece252f6e # v6.1.0
with:
gradle-version: 9.3.1
gradle-version: 9.5.1
- name: Install Task
uses: go-task/setup-task@3be4020d41929789a01026e0e427a4321ce0ad44 # v2.0.0
@@ -388,7 +388,7 @@ jobs:
environment:
DISABLE_ADDITIONAL_FEATURES: "${DISABLE_ADDITIONAL_FEATURES}"
SECURITY_ENABLELOGIN: "${LOGIN_SECURITY}"
SYSTEM_DEFAULTLOCALE: en-GB
SYSTEM_DEFAULTLOCALE: en-US
UI_APPNAME: "Stirling-PDF PR#${PR_NUMBER}"
UI_HOMEDESCRIPTION: "PR#${PR_NUMBER} for Stirling-PDF Latest"
UI_APPNAMENAVBAR: "PR#${PR_NUMBER}"
+1 -1
View File
@@ -43,7 +43,7 @@ jobs:
- name: Setup Gradle
uses: gradle/actions/setup-gradle@50e97c2cd7a37755bbfafc9c5b7cafaece252f6e # v6.1.0
with:
gradle-version: 9.3.1
gradle-version: 9.5.1
- name: Install Task
uses: go-task/setup-task@3be4020d41929789a01026e0e427a4321ce0ad44 # v2.0.0
+1 -1
View File
@@ -58,7 +58,7 @@ jobs:
- name: Setup Gradle
uses: gradle/actions/setup-gradle@50e97c2cd7a37755bbfafc9c5b7cafaece252f6e # v6.1.0
with:
gradle-version: 9.3.1
gradle-version: 9.5.1
cache-disabled: true
- name: Install Task
+1 -1
View File
@@ -40,7 +40,7 @@ jobs:
- name: Setup Gradle
uses: gradle/actions/setup-gradle@50e97c2cd7a37755bbfafc9c5b7cafaece252f6e # v6.1.0
with:
gradle-version: 9.3.1
gradle-version: 9.5.1
cache-disabled: true
- name: Install Task
+1 -1
View File
@@ -45,7 +45,7 @@ jobs:
- name: Setup Gradle
uses: gradle/actions/setup-gradle@50e97c2cd7a37755bbfafc9c5b7cafaece252f6e # v6.1.0
with:
gradle-version: 9.3.1
gradle-version: 9.5.1
cache-disabled: true
- name: Install Task
+6 -6
View File
@@ -166,16 +166,16 @@ jobs:
// Determine reference file
let referenceFilePath;
if (changedFiles.includes("frontend/editor/public/locales/en-GB/translation.toml")) {
if (changedFiles.includes("frontend/editor/public/locales/en-US/translation.toml")) {
console.log("Using PR branch reference file.");
const { data: fileContent } = await github.rest.repos.getContent({
owner: prRepoOwner,
repo: prRepoName,
path: "frontend/editor/public/locales/en-GB/translation.toml",
path: "frontend/editor/public/locales/en-US/translation.toml",
ref: branch,
});
referenceFilePath = "pr-branch-translation-en-GB.toml";
referenceFilePath = "pr-branch-translation-en-US.toml";
const content = Buffer.from(fileContent.content, "base64").toString("utf-8");
fs.writeFileSync(referenceFilePath, content);
} else {
@@ -183,11 +183,11 @@ jobs:
const { data: fileContent } = await github.rest.repos.getContent({
owner: repoOwner,
repo: repoName,
path: "frontend/editor/public/locales/en-GB/translation.toml",
path: "frontend/editor/public/locales/en-US/translation.toml",
ref: "main",
});
referenceFilePath = "main-branch-translation-en-GB.toml";
referenceFilePath = "main-branch-translation-en-US.toml";
const content = Buffer.from(fileContent.content, "base64").toString("utf-8");
fs.writeFileSync(referenceFilePath, content);
}
@@ -293,6 +293,6 @@ jobs:
run: |
echo "Cleaning up temporary files..."
rm -rf pr-branch
rm -f pr-branch-translation-en-GB.toml main-branch-translation-en-GB.toml changed_files.txt result.txt
rm -f pr-branch-translation-en-US.toml main-branch-translation-en-US.toml changed_files.txt result.txt
echo "Cleanup complete."
continue-on-error: true # Ensure cleanup runs even if previous steps fail
+1 -1
View File
@@ -48,7 +48,7 @@ jobs:
- name: Setup Gradle
uses: gradle/actions/setup-gradle@50e97c2cd7a37755bbfafc9c5b7cafaece252f6e # v6.1.0
with:
gradle-version: 9.3.1
gradle-version: 9.5.1
cache-disabled: true
# No `-PnoSpotless` here yet because the upstream cache layer matches the
+1 -1
View File
@@ -188,7 +188,7 @@ jobs:
environment:
DISABLE_ADDITIONAL_FEATURES: "true"
SECURITY_ENABLELOGIN: "false"
SYSTEM_DEFAULTLOCALE: en-GB
SYSTEM_DEFAULTLOCALE: en-US
UI_APPNAME: "Stirling-PDF V2"
UI_HOMEDESCRIPTION: "V2 Frontend/Backend Split"
UI_APPNAMENAVBAR: "V2 Deployment"
+1 -1
View File
@@ -61,7 +61,7 @@ jobs:
- name: Setup Gradle
uses: gradle/actions/setup-gradle@50e97c2cd7a37755bbfafc9c5b7cafaece252f6e # v6.1.0
with:
gradle-version: 9.3.1
gradle-version: 9.5.1
cache-disabled: true
- name: Set up Docker Buildx
@@ -349,7 +349,7 @@ jobs:
- name: Setup Gradle
uses: gradle/actions/setup-gradle@50e97c2cd7a37755bbfafc9c5b7cafaece252f6e # v6.1.0
with:
gradle-version: 9.3.1
gradle-version: 9.5.1
- name: Install Task
uses: go-task/setup-task@3be4020d41929789a01026e0e427a4321ce0ad44 # v2.0.0
+3 -3
View File
@@ -73,7 +73,7 @@ jobs:
- name: Setup Gradle
uses: gradle/actions/setup-gradle@50e97c2cd7a37755bbfafc9c5b7cafaece252f6e # v6.1.0
with:
gradle-version: 9.3.1
gradle-version: 9.5.1
- name: Install Task
uses: go-task/setup-task@3be4020d41929789a01026e0e427a4321ce0ad44 # v2.0.0
@@ -148,7 +148,7 @@ jobs:
- name: Setup Gradle
uses: gradle/actions/setup-gradle@50e97c2cd7a37755bbfafc9c5b7cafaece252f6e # v6.1.0
with:
gradle-version: 9.3.1
gradle-version: 9.5.1
- name: Setup Node.js
if: matrix.variant.build_frontend == true
@@ -252,7 +252,7 @@ jobs:
- name: Setup Gradle
uses: gradle/actions/setup-gradle@50e97c2cd7a37755bbfafc9c5b7cafaece252f6e # v6.1.0
with:
gradle-version: 9.3.1
gradle-version: 9.5.1
- name: Install Task
uses: go-task/setup-task@3be4020d41929789a01026e0e427a4321ce0ad44 # v2.0.0
+1 -1
View File
@@ -78,7 +78,7 @@ jobs:
- name: Setup Gradle
uses: gradle/actions/setup-gradle@50e97c2cd7a37755bbfafc9c5b7cafaece252f6e # v6.1.0
with:
gradle-version: 9.3.1
gradle-version: 9.5.1
- name: Set up Docker Buildx
id: buildx
+1 -1
View File
@@ -48,7 +48,7 @@ jobs:
- name: Setup Gradle
uses: gradle/actions/setup-gradle@50e97c2cd7a37755bbfafc9c5b7cafaece252f6e # v6.1.0
with:
gradle-version: 9.3.1
gradle-version: 9.5.1
- name: Generate Swagger documentation
run: ./gradlew :stirling-pdf:generateOpenApiDocs
+2 -2
View File
@@ -62,7 +62,7 @@ jobs:
- name: Sync translation TOML files
run: |
python .github/scripts/check_language_toml.py --reference-file "frontend/editor/public/locales/en-GB/translation.toml" --branch main
python .github/scripts/check_language_toml.py --reference-file "frontend/editor/public/locales/en-US/translation.toml" --branch main
- name: pre-commit run
run: |
@@ -100,7 +100,7 @@ jobs:
This Pull Request was automatically generated to synchronize updates to translation files and documentation. Below are the details of the changes made:
#### **1. Synchronization of Translation Files**
- Updated translation files (`frontend/editor/public/locales/*/translation.toml`) to reflect changes in the reference file `en-GB/translation.toml`.
- Updated translation files (`frontend/editor/public/locales/*/translation.toml`) to reflect changes in the reference file `en-US/translation.toml`.
- Ensured consistency and synchronization across all supported language files.
- Highlighted any missing or incomplete translations.
- **Format**: TOML
+1 -1
View File
@@ -136,7 +136,7 @@ jobs:
- name: Setup Gradle
uses: gradle/actions/setup-gradle@50e97c2cd7a37755bbfafc9c5b7cafaece252f6e # v6.1.0
with:
gradle-version: 9.3.1
gradle-version: 9.5.1
- name: Setup Task
uses: go-task/setup-task@3be4020d41929789a01026e0e427a4321ce0ad44 # v2.0.0
+1 -1
View File
@@ -106,7 +106,7 @@ jobs:
- name: Setup Gradle
uses: gradle/actions/setup-gradle@50e97c2cd7a37755bbfafc9c5b7cafaece252f6e # v6.1.0
with:
gradle-version: 9.3.1
gradle-version: 9.5.1
cache-disabled: true
- name: Install Task
+2 -2
View File
@@ -51,7 +51,7 @@ jobs:
- name: Setup Gradle
uses: gradle/actions/setup-gradle@50e97c2cd7a37755bbfafc9c5b7cafaece252f6e # v6.1.0
with:
gradle-version: 9.3.1
gradle-version: 9.5.1
- name: Build with Gradle
run: ./gradlew build
@@ -129,7 +129,7 @@ jobs:
environment:
DISABLE_ADDITIONAL_FEATURES: "true"
SECURITY_ENABLELOGIN: "false"
SYSTEM_DEFAULTLOCALE: en-GB
SYSTEM_DEFAULTLOCALE: en-US
UI_APPNAME: "Stirling-PDF Test"
UI_HOMEDESCRIPTION: "Test Deployment"
UI_APPNAMENAVBAR: "Test"
+9 -8
View File
@@ -127,14 +127,15 @@ tasks:
cmds:
- rm -rf runtime/jre
- mkdir -p runtime
- >-
jlink
--add-modules {{.JLINK_MODULES}}
--strip-debug
--compress=zip-6
--no-header-files
--no-man-pages
--output runtime/jre
- |
JLINK_COMPRESS="$(jlink --help 2>&1 | grep -q 'zip-\[0-9\]' && echo zip-6 || echo 2)"
jlink \
--add-modules {{.JLINK_MODULES}} \
--strip-debug \
--compress="$JLINK_COMPRESS" \
--no-header-files \
--no-man-pages \
--output runtime/jre
# jlink emits its files mode 444 (read-only). Tauri's build-script
# resource copier preserves source permissions when staging
# `runtime/jre/**/*` into `target/<profile>/runtime/jre/...`, so the
+3 -3
View File
@@ -200,9 +200,9 @@ const [ToolName] = (props: BaseToolProps) => {
```
## 5. Add Translations
Update translation files. **Important: Only update `en-GB` files** - other languages are handled separately.
Update translation files. **Important: Only update `en-US` files** - other languages are handled separately.
**File to update:** `frontend/editor/public/locales/en-GB/translation.toml`
**File to update:** `frontend/editor/public/locales/en-US/translation.toml`
**Required Translation Keys**:
```toml
@@ -251,7 +251,7 @@ Update translation files. **Important: Only update `en-GB` files** - other langu
```
**Translation Notes:**
- **Only update `en-GB/translation.toml`** - other locale files are managed separately
- **Only update `en-US/translation.toml`** - other locale files are managed separately
- Use descriptive keys that match your component's `t()` calls
- Include tooltip translations if you created tooltip hooks
- Add `options.*` keys if your tool has settings with descriptions
+1 -1
View File
@@ -426,7 +426,7 @@ The frontend is organized with a clear separation of concerns:
## Translation Rules
- **CRITICAL**: Always update translations in `en-GB` only, never `en-US`
- **CRITICAL**: Always update translations in `en-US` only - all other languages (including `en-GB`) are handled separately
- Translation files are located in `frontend/editor/public/locales/`
## Important Notes
+12 -1
View File
@@ -52,6 +52,17 @@ This guide focuses on developing for Stirling 2.0, including both the React fron
- Rust and Cargo (required for Tauri desktop app development)
- Tauri CLI (install with `cargo install tauri-cli`)
### Optional System Dependencies
These are not required to run the app but enable specific features. The app detects them at startup and disables the relevant features if they are missing.
| Dependency | Feature | Install |
|---|---|---|
| LibreOffice | File-to-PDF conversions | `brew install libreoffice` / `apt install libreoffice` |
| Tesseract | OCR | `brew install tesseract` / `apt install tesseract-ocr` |
| WeasyPrint | AI document creation | `brew install weasyprint` / `apt install weasyprint` |
| qpdf | PDF optimisation | `brew install qpdf` / `apt install qpdf` |
### Setup Steps
1. Clone the repository:
@@ -576,7 +587,7 @@ When adding a new feature or modifying existing ones in Stirling-PDF, you'll nee
Find the existing `messages.properties` files in the `stirling-pdf/src/main/resources` directory. You'll see files like:
- `messages.properties` (default, usually English)
- `messages_en_GB.properties`
- `messages_en_US.properties`
- `messages_fr_FR.properties`
- `messages_de_DE.properties`
- etc.
@@ -3,7 +3,6 @@ package stirling.software.common.configuration;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Locale;
import java.util.Properties;
@@ -122,17 +121,17 @@ public class AppConfig {
@Bean(name = "RunningInDocker")
public boolean runningInDocker() {
return Files.exists(Paths.get("/.dockerenv"));
return Files.exists(Path.of("/.dockerenv"));
}
@Bean(name = "configDirMounted")
public boolean isRunningInDockerWithConfig() {
Path dockerEnv = Paths.get("/.dockerenv");
Path dockerEnv = Path.of("/.dockerenv");
// default to true if not docker
if (!Files.exists(dockerEnv)) {
return true;
}
Path mountInfo = Paths.get("/proc/1/mountinfo");
Path mountInfo = Path.of("/proc/1/mountinfo");
// this should always exist, if not some unknown usecase
if (!Files.exists(mountInfo)) {
return true;
@@ -7,7 +7,6 @@ import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.List;
@@ -27,7 +26,7 @@ public class ConfigInitializer {
public void ensureConfigExists() throws IOException, URISyntaxException {
// 1) If settings file doesn't exist, create from template
Path destPath = Paths.get(InstallationPathConfig.getSettingsPath());
Path destPath = Path.of(InstallationPathConfig.getSettingsPath());
boolean settingsFileExists = Files.exists(destPath);
@@ -39,7 +38,7 @@ public class ConfigInitializer {
if (settingsFileExists) {
// move settings.yml to settings.yml.{timestamp}.bak
Path backupPath =
Paths.get(
Path.of(
InstallationPathConfig.getSettingsPath()
+ "."
+ System.currentTimeMillis()
@@ -96,7 +95,7 @@ public class ConfigInitializer {
}
// 3) Ensure custom settings file exists
Path customSettingsPath = Paths.get(InstallationPathConfig.getCustomSettingsPath());
Path customSettingsPath = Path.of(InstallationPathConfig.getCustomSettingsPath());
if (Files.notExists(customSettingsPath)) {
Files.createFile(customSettingsPath);
log.info("Created custom_settings file: {}", customSettingsPath);
@@ -3,7 +3,6 @@ package stirling.software.common.configuration;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;
@@ -201,7 +200,7 @@ public class RuntimePathConfig {
try {
// Normalize to absolute path
Path path = Paths.get(pathStr.trim()).toAbsolutePath().normalize();
Path path = Path.of(pathStr.trim()).toAbsolutePath().normalize();
String normalizedPath = path.toString();
// Check for duplicates
@@ -224,9 +223,9 @@ public class RuntimePathConfig {
private void detectOverlappingPaths(List<String> paths) {
for (int i = 0; i < paths.size(); i++) {
Path path1 = Paths.get(paths.get(i));
Path path1 = Path.of(paths.get(i));
for (int j = i + 1; j < paths.size(); j++) {
Path path2 = Paths.get(paths.get(j));
Path path2 = Path.of(paths.get(j));
// Check if one path is a parent of the other
if (path1.startsWith(path2)) {
@@ -246,10 +245,10 @@ public class RuntimePathConfig {
private void validatePipelinePaths() {
try {
Path finishedPath = Paths.get(pipelineFinishedFoldersPath).toAbsolutePath().normalize();
Path finishedPath = Path.of(pipelineFinishedFoldersPath).toAbsolutePath().normalize();
for (String watchedPathStr : pipelineWatchedFoldersPaths) {
Path watchedPath = Paths.get(watchedPathStr).toAbsolutePath().normalize();
Path watchedPath = Path.of(watchedPathStr).toAbsolutePath().normalize();
// Check if watched folder is same as finished folder
if (watchedPath.equals(finishedPath)) {
@@ -1,7 +1,6 @@
package stirling.software.common.model;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
@@ -24,7 +23,7 @@ public class FileInfo {
// Converts the file path string to a Path object.
public Path getFilePathAsPath() {
return Paths.get(filePath);
return Path.of(filePath);
}
// Formats the file size into a human-readable string.
@@ -97,6 +97,17 @@ public class InternalApiClient {
headers.add("X-API-KEY", apiKey);
}
// A no-file ai/tools call (e.g. create-pdf-from-html-agent) sends only string params, so
// without this RestTemplate would use urlencoded instead of the multipart the controller
// expects. File-bearing calls get the right multipart content-type from RestTemplate.
boolean isAiTool = endpointPath.startsWith("/api/v1/ai/tools/");
boolean hasFilePart =
body.values().stream()
.flatMap(java.util.List::stream)
.anyMatch(v -> v instanceof Resource);
if (isAiTool && !hasFilePart) {
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
}
HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<>(body, headers);
RequestCallback requestCallback = restTemplate.httpEntityCallback(entity, Resource.class);
@@ -3,7 +3,6 @@ package stirling.software.common.service;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -35,7 +34,7 @@ public class MobileScannerService {
public MobileScannerService() throws IOException {
// Create temp directory for mobile scanner uploads
this.tempDirectory =
Paths.get(System.getProperty("java.io.tmpdir"), "stirling-mobile-scanner");
Path.of(System.getProperty("java.io.tmpdir"), "stirling-mobile-scanner");
Files.createDirectories(tempDirectory);
log.info("Mobile scanner temp directory: {}", tempDirectory);
}
@@ -8,7 +8,7 @@ import java.lang.management.OperatingSystemMXBean;
import java.lang.management.RuntimeMXBean;
import java.lang.management.ThreadMXBean;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
@@ -160,7 +160,7 @@ public class PostHogService {
}
private boolean isRunningInDocker() {
return Files.exists(Paths.get("/.dockerenv"));
return Files.exists(Path.of("/.dockerenv"));
}
private Map<String, Object> getDockerMetrics() {
@@ -255,7 +255,7 @@ public class GeneralUtils {
String pattern = locationPattern;
if (pattern.startsWith("file:")) {
String rawPath = pattern.substring(5).replace("\\*", "").replace("/*", "");
Path normalizePath = Paths.get(rawPath).normalize();
Path normalizePath = Path.of(rawPath).normalize();
pattern = "file:" + normalizePath.toString().replace("\\", "/") + "/*";
}
return ResourcePatternUtils.getResourcePatternResolver(resourceLoader)
@@ -837,7 +837,7 @@ public class GeneralUtils {
}
public boolean createDir(String path) {
Path folder = Paths.get(path);
Path folder = Path.of(path);
if (!Files.exists(folder)) {
try {
Files.createDirectories(folder);
@@ -867,7 +867,7 @@ public class GeneralUtils {
public void saveKeyToSettings(String key, Object newValue) throws IOException {
String[] keyArray = key.split("\\.");
Path settingsPath = Paths.get(InstallationPathConfig.getSettingsPath());
Path settingsPath = Path.of(InstallationPathConfig.getSettingsPath());
YamlHelper settingsYaml = new YamlHelper(settingsPath);
settingsYaml.updateValue(Arrays.asList(keyArray), newValue);
settingsYaml.saveOverride(settingsPath);
@@ -888,7 +888,7 @@ public class GeneralUtils {
return;
}
Path settingsPath = Paths.get(InstallationPathConfig.getSettingsPath());
Path settingsPath = Path.of(InstallationPathConfig.getSettingsPath());
YamlHelper settingsYaml = new YamlHelper(settingsPath);
// Apply all updates to the same YamlHelper instance
@@ -974,11 +974,11 @@ public class GeneralUtils {
*/
public void extractPipeline() throws IOException {
Path pipelineDir =
Paths.get(InstallationPathConfig.getPipelinePath(), DEFAULT_WEBUI_CONFIGS_DIR);
Path.of(InstallationPathConfig.getPipelinePath(), DEFAULT_WEBUI_CONFIGS_DIR);
Files.createDirectories(pipelineDir);
for (String name : DEFAULT_VALID_PIPELINE) {
if (!Paths.get(name).getFileName().toString().equals(name)) {
if (!Path.of(name).getFileName().toString().equals(name)) {
log.error("Invalid pipeline file name: {}", name);
throw new IllegalArgumentException("Invalid pipeline file name: " + name);
}
@@ -1014,7 +1014,7 @@ public class GeneralUtils {
throw new IllegalArgumentException(
"scriptName must not contain path traversal characters");
}
if (!Paths.get(scriptName).getFileName().toString().equals(scriptName)) {
if (!Path.of(scriptName).getFileName().toString().equals(scriptName)) {
throw new IllegalArgumentException(
"scriptName must not contain path traversal characters");
}
@@ -1024,7 +1024,7 @@ public class GeneralUtils {
"scriptName must be either 'png_to_webp.py' or 'split_photos.py'");
}
Path scriptsDir = Paths.get(InstallationPathConfig.getScriptsPath(), PYTHON_SCRIPTS_DIR);
Path scriptsDir = Path.of(InstallationPathConfig.getScriptsPath(), PYTHON_SCRIPTS_DIR);
Files.createDirectories(scriptsDir);
Path target = scriptsDir.resolve(scriptName);
@@ -4,7 +4,6 @@ import java.io.File;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import lombok.extern.slf4j.Slf4j;
@@ -20,7 +19,7 @@ public class JarPathUtil {
public static Path currentJar() {
try {
Path jar =
Paths.get(
Path.of(
JarPathUtil.class
.getProtectionDomain()
.getCodeSource()
@@ -61,14 +60,14 @@ public class JarPathUtil {
}
// Location 2: ./build/libs/ (development build)
possibleLocations[1] = Paths.get("build", "libs", "restart-helper.jar").toAbsolutePath();
possibleLocations[1] = Path.of("build", "libs", "restart-helper.jar").toAbsolutePath();
// Location 3: app/common/build/libs/ (multi-module build)
possibleLocations[2] =
Paths.get("app", "common", "build", "libs", "restart-helper.jar").toAbsolutePath();
Path.of("app", "common", "build", "libs", "restart-helper.jar").toAbsolutePath();
// Location 4: Current working directory
possibleLocations[3] = Paths.get("restart-helper.jar").toAbsolutePath();
possibleLocations[3] = Path.of("restart-helper.jar").toAbsolutePath();
// Check each location
for (Path location : possibleLocations) {
@@ -2,7 +2,7 @@ package stirling.software.common.model;
import static org.junit.jupiter.api.Assertions.*;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@@ -33,16 +33,15 @@ class ApplicationPropertiesLogicTest {
@Test
void tempFileManagement_defaults_and_overrides() {
Function<String, String> normalize = s -> Paths.get(s).normalize().toString();
Function<String, String> normalize = s -> Path.of(s).normalize().toString();
ApplicationProperties.TempFileManagement tfm =
new ApplicationProperties.TempFileManagement();
String expectedBase =
Paths.get(java.lang.System.getProperty("java.io.tmpdir"), "stirling-pdf")
.toString();
Path.of(java.lang.System.getProperty("java.io.tmpdir"), "stirling-pdf").toString();
assertEquals(expectedBase, tfm.getBaseTmpDir());
String expectedLibre = Paths.get(expectedBase, "libreoffice").toString();
String expectedLibre = Path.of(expectedBase, "libreoffice").toString();
assertEquals(expectedLibre, tfm.getLibreofficeDir());
tfm.setBaseTmpDir("/custom/base");
@@ -4,7 +4,6 @@ import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@@ -75,7 +74,7 @@ public class SPDFApplication {
Map<String, String> propertyFiles = new HashMap<>();
// External config files
Path settingsPath = Paths.get(InstallationPathConfig.getSettingsPath());
Path settingsPath = Path.of(InstallationPathConfig.getSettingsPath());
log.info("Settings file: {}", settingsPath.toString());
if (Files.exists(settingsPath)) {
propertyFiles.put(
@@ -84,7 +83,7 @@ public class SPDFApplication {
log.warn("External configuration file '{}' does not exist.", settingsPath.toString());
}
Path customSettingsPath = Paths.get(InstallationPathConfig.getCustomSettingsPath());
Path customSettingsPath = Path.of(InstallationPathConfig.getCustomSettingsPath());
log.info("Custom settings file: {}", customSettingsPath.toString());
if (Files.exists(customSettingsPath)) {
String existingLocation =
@@ -95,7 +95,7 @@ public class ExternalAppDepConfig {
checkDependencyAndDisableGroup(cmd);
return null;
})
.collect(Collectors.toList());
.toList();
invokeAllWithTimeout(tasks, DEFAULT_TIMEOUT.plusSeconds(3));
// Python / OpenCV special handling
@@ -37,8 +37,8 @@ public class LocaleConfiguration implements WebMvcConfigurer {
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
String appLocaleEnv = applicationProperties.getSystem().getDefaultLocale();
Locale defaultLocale = // Fallback to UK locale if environment variable is not set
Locale.UK;
Locale defaultLocale = // Fallback to US locale if environment variable is not set
Locale.US;
if (appLocaleEnv != null && !appLocaleEnv.isEmpty()) {
Locale tempLocale = Locale.forLanguageTag(appLocaleEnv);
String tempLanguageTag = tempLocale.toLanguageTag();
@@ -51,7 +51,7 @@ public class LocaleConfiguration implements WebMvcConfigurer {
defaultLocale = tempLocale;
} else {
System.err.println(
"Invalid SYSTEM_DEFAULTLOCALE environment variable value. Falling back to default en-GB.");
"Invalid SYSTEM_DEFAULTLOCALE environment variable value. Falling back to default en-US.");
}
}
}
@@ -48,7 +48,7 @@ public class AdditionalLanguageJsController {
}
}
// Fallback
return "en_GB";
return "en_US";
}
""");
writer.flush();
@@ -5,7 +5,6 @@ import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Stream;
@@ -115,7 +114,7 @@ public class UIDataController {
if (new java.io.File(runtimePathConfig.getPipelineDefaultWebUiConfigs()).exists()) {
try (Stream<Path> paths =
Files.walk(Paths.get(runtimePathConfig.getPipelineDefaultWebUiConfigs()))) {
Files.walk(Path.of(runtimePathConfig.getPipelineDefaultWebUiConfigs()))) {
List<Path> jsonFiles =
paths.filter(Files::isRegularFile)
.filter(p -> p.toString().endsWith(".json"))
@@ -9,7 +9,6 @@ import java.awt.print.PrinterJob;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Arrays;
import java.util.Locale;
@@ -54,7 +53,7 @@ public class PrintFileController {
MultipartFile file = request.getFileInput();
String originalFilename = file.getOriginalFilename();
if (originalFilename != null
&& (originalFilename.contains("..") || Paths.get(originalFilename).isAbsolute())) {
&& (originalFilename.contains("..") || Path.of(originalFilename).isAbsolute())) {
throw ExceptionUtils.createIllegalArgumentException(
"error.invalid.filepath", "Invalid file path detected: " + originalFilename);
}
@@ -7,7 +7,6 @@ import java.nio.file.FileVisitOption;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.BasicFileAttributes;
@@ -82,7 +81,7 @@ public class PipelineDirectoryProcessor {
try {
for (String watchedFoldersDir : watchedFoldersDirs) {
scanWatchedFolder(Paths.get(watchedFoldersDir).toAbsolutePath());
scanWatchedFolder(Path.of(watchedFoldersDir).toAbsolutePath());
}
} finally {
// Clean up ThreadLocal to prevent memory leaks
@@ -442,7 +441,7 @@ public class PipelineDirectoryProcessor {
.replace("{outputFolder}", finishedFoldersDir)
.replace("{folderName}", dir.toString()))
.replaceAll("");
return Paths.get(outputDir).isAbsolute() ? Paths.get(outputDir) : Paths.get(".", outputDir);
return Path.of(outputDir).isAbsolute() ? Path.of(outputDir) : Path.of(".", outputDir);
}
private void deleteOriginalFiles(List<File> filesToProcess, Path processingDir)
@@ -5,7 +5,6 @@ import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
@@ -326,12 +325,12 @@ public class PipelineProcessor {
}
List<Resource> outputFiles = new ArrayList<>();
for (File file : files) {
Path normalizedPath = Paths.get(file.getName()).normalize();
Path normalizedPath = Path.of(file.getName()).normalize();
if (normalizedPath.startsWith("..")) {
throw new SecurityException(
"Potential path traversal attempt in file name: " + file.getName());
}
Path path = Paths.get(file.getAbsolutePath());
Path path = Path.of(file.getAbsolutePath());
// debug statement
log.info("Reading file: {}", path);
if (Files.exists(path)) {
@@ -5,7 +5,6 @@ import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.regex.Pattern;
import org.springframework.beans.factory.annotation.Value;
@@ -66,7 +65,7 @@ public class ReactRoutingController {
}
// Check for external index.html first (customFiles/static/)
Path externalIndexPath = Paths.get(InstallationPathConfig.getStaticPath(), "index.html");
Path externalIndexPath = Path.of(InstallationPathConfig.getStaticPath(), "index.html");
log.debug("Checking for custom index.html at: {}", externalIndexPath);
if (Files.exists(externalIndexPath) && Files.isReadable(externalIndexPath)) {
log.info("Using custom index.html from: {}", externalIndexPath);
@@ -136,7 +135,7 @@ public class ReactRoutingController {
private Resource getIndexHtmlResource() {
// Check external location first
Path externalIndexPath = Paths.get(InstallationPathConfig.getStaticPath(), "index.html");
Path externalIndexPath = Path.of(InstallationPathConfig.getStaticPath(), "index.html");
if (Files.exists(externalIndexPath) && Files.isReadable(externalIndexPath)) {
return new FileSystemResource(externalIndexPath.toFile());
}
@@ -588,7 +588,7 @@ public class PdfJsonConversionService {
.replaceAll("");
return String.format("%s (%s)", cleanName, subtype);
})
.collect(java.util.stream.Collectors.toList());
.toList();
long type3Fonts =
responseFonts.stream().filter(f -> "Type3".equals(f.getSubtype())).count();
@@ -1554,7 +1554,7 @@ public class PdfJsonConversionService {
.glyphName(outline.getGlyphName())
.unicode(outline.getUnicode())
.build())
.collect(Collectors.toList());
.toList();
}
} catch (Exception ex) {
log.debug(
@@ -4,7 +4,6 @@ import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Base64;
@@ -41,8 +40,8 @@ public class SharedSignatureService {
public boolean hasAccessToFile(String username, String fileName) throws IOException {
validateFileName(fileName);
// Check if file exists in user's personal folder or ALL_USERS folder
Path userPath = Paths.get(SIGNATURE_BASE_PATH, username, fileName);
Path allUsersPath = Paths.get(SIGNATURE_BASE_PATH, ALL_USERS_FOLDER, fileName);
Path userPath = Path.of(SIGNATURE_BASE_PATH, username, fileName);
Path allUsersPath = Path.of(SIGNATURE_BASE_PATH, ALL_USERS_FOLDER, fileName);
return Files.exists(userPath) || Files.exists(allUsersPath);
}
@@ -52,7 +51,7 @@ public class SharedSignatureService {
// Get signatures from user's personal folder
if (StringUtils.hasText(username)) {
Path userFolder = Paths.get(SIGNATURE_BASE_PATH, username);
Path userFolder = Path.of(SIGNATURE_BASE_PATH, username);
if (Files.exists(userFolder)) {
try {
signatures.addAll(getSignaturesFromFolder(userFolder, "Personal"));
@@ -63,7 +62,7 @@ public class SharedSignatureService {
}
// Get signatures from ALL_USERS folder
Path allUsersFolder = Paths.get(SIGNATURE_BASE_PATH, ALL_USERS_FOLDER);
Path allUsersFolder = Path.of(SIGNATURE_BASE_PATH, ALL_USERS_FOLDER);
if (Files.exists(allUsersFolder)) {
try {
signatures.addAll(getSignaturesFromFolder(allUsersFolder, "Shared"));
@@ -90,7 +89,7 @@ public class SharedSignatureService {
*/
public byte[] getSharedSignatureBytes(String fileName) throws IOException {
validateFileName(fileName);
Path allUsersPath = Paths.get(SIGNATURE_BASE_PATH, ALL_USERS_FOLDER, fileName);
Path allUsersPath = Path.of(SIGNATURE_BASE_PATH, ALL_USERS_FOLDER, fileName);
if (!Files.exists(allUsersPath)) {
throw new FileNotFoundException("Shared signature file not found");
}
@@ -142,7 +141,7 @@ public class SharedSignatureService {
}
String folderName = "shared".equals(scope) ? ALL_USERS_FOLDER : username;
Path targetFolder = Paths.get(SIGNATURE_BASE_PATH, folderName);
Path targetFolder = Path.of(SIGNATURE_BASE_PATH, folderName);
Files.createDirectories(targetFolder);
long timestamp = System.currentTimeMillis();
@@ -193,7 +192,7 @@ public class SharedSignatureService {
List<SavedSignatureResponse> signatures = new ArrayList<>();
// Load personal signatures
Path personalFolder = Paths.get(SIGNATURE_BASE_PATH, username);
Path personalFolder = Path.of(SIGNATURE_BASE_PATH, username);
if (Files.exists(personalFolder)) {
try (Stream<Path> stream = Files.list(personalFolder)) {
stream.filter(this::isImageFile)
@@ -224,7 +223,7 @@ public class SharedSignatureService {
}
// Load shared signatures
Path sharedFolder = Paths.get(SIGNATURE_BASE_PATH, ALL_USERS_FOLDER);
Path sharedFolder = Path.of(SIGNATURE_BASE_PATH, ALL_USERS_FOLDER);
if (Files.exists(sharedFolder)) {
try (Stream<Path> stream = Files.list(sharedFolder)) {
stream.filter(this::isImageFile)
@@ -262,7 +261,7 @@ public class SharedSignatureService {
validateFileName(signatureId);
// Try to find and delete image file in personal folder
Path personalFolder = Paths.get(SIGNATURE_BASE_PATH, username);
Path personalFolder = Path.of(SIGNATURE_BASE_PATH, username);
boolean deleted = false;
if (Files.exists(personalFolder)) {
@@ -283,7 +282,7 @@ public class SharedSignatureService {
// Try shared folder if not found in personal
if (!deleted) {
Path sharedFolder = Paths.get(SIGNATURE_BASE_PATH, ALL_USERS_FOLDER);
Path sharedFolder = Path.of(SIGNATURE_BASE_PATH, ALL_USERS_FOLDER);
if (Files.exists(sharedFolder)) {
try (Stream<Path> stream = Files.list(sharedFolder)) {
List<Path> matchingFiles =
@@ -10,7 +10,6 @@ import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.font.PDType3Font;
@@ -268,7 +267,7 @@ public class Type3FontLibrary {
.filter(Objects::nonNull)
.map(String::trim)
.filter(s -> !s.isEmpty())
.collect(Collectors.toList());
.toList();
}
private String normalizeAlias(String alias) {
@@ -3,7 +3,6 @@ package stirling.software.SPDF.service.pdfjson.type3.tool;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
@@ -285,9 +284,9 @@ public final class Type3SignatureTool {
for (int i = 0; i < args.length; i++) {
String arg = args[i];
if ("--pdf".equals(arg) && i + 1 < args.length) {
pdf = Paths.get(args[++i]);
pdf = Path.of(args[++i]);
} else if ("--output".equals(arg) && i + 1 < args.length) {
output = Paths.get(args[++i]);
output = Path.of(args[++i]);
} else if ("--pretty".equals(arg)) {
pretty = true;
} else if ("--help".equals(arg) || "-h".equals(arg)) {
@@ -8,7 +8,6 @@ import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
@@ -411,7 +410,7 @@ public class TelegramPipelineBot extends TelegramLongPollingBot {
private Path getInboxFolder(Long chatId) throws IOException {
Path baseInbox =
Paths.get(
Path.of(
runtimePathConfig.getPipelineWatchedFoldersPath(),
telegramProperties.getPipelineInboxFolder());
@@ -445,7 +444,7 @@ public class TelegramPipelineBot extends TelegramLongPollingBot {
private List<Path> waitForPipelineOutputs(PipelineFileInfo info) throws IOException {
Path finishedDir = Paths.get(runtimePathConfig.getPipelineFinishedFoldersPath());
Path finishedDir = Path.of(runtimePathConfig.getPipelineFinishedFoldersPath());
Files.createDirectories(finishedDir);
Instant start = info.savedAt();
@@ -167,7 +167,7 @@ legal:
impressum: "" # URL to the impressum of your application (e.g. https://example.com/impressum). Empty string to disable or filename to load from local file in static folder
system:
defaultLocale: en-US # set the default language (e.g. 'de-DE', 'fr-FR', etc)
defaultLocale: "" # force a default language for new users (e.g. 'en-US', 'de-DE'). Empty string auto-detects from the browser, falling back to en-US
googlevisibility: false # 'true' to allow Google visibility (via robots.txt), 'false' to disallow
enableAlphaFunctionality: false # set to enable functionality which might need more testing before it fully goes live (this feature might make no changes)
showUpdate: false # see when a new update is available
@@ -23,7 +23,7 @@ class AdditionalLanguageJsControllerTest {
LanguageService lang = mock(LanguageService.class);
// LinkedHashSet for deterministic order in the array
when(lang.getSupportedLanguages())
.thenReturn(new LinkedHashSet<>(List.of("de_DE", "en_GB")));
.thenReturn(new LinkedHashSet<>(List.of("de_DE", "en_US")));
MockMvc mvc =
MockMvcBuilders.standaloneSetup(new AdditionalLanguageJsController(lang)).build();
@@ -36,9 +36,9 @@ class AdditionalLanguageJsControllerTest {
.string(
containsString(
"const supportedLanguages ="
+ " [\"de_DE\",\"en_GB\"];")))
+ " [\"de_DE\",\"en_US\"];")))
.andExpect(content().string(containsString("function getDetailedLanguageCode()")))
.andExpect(content().string(containsString("return \"en_GB\";")));
.andExpect(content().string(containsString("return \"en_US\";")));
verify(lang, times(1)).getSupportedLanguages();
}
@@ -3,7 +3,7 @@ package stirling.software.SPDF.controller.api.misc;
import static org.junit.jupiter.api.Assertions.*;
import java.io.IOException;
import java.nio.file.Paths;
import java.nio.file.Path;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@@ -34,9 +34,9 @@ class PrintFileControllerTest {
@Test
void printFile_absolutePath_throwsException() {
PrintFileRequest request = new PrintFileRequest();
String absPath = Paths.get("/etc/passwd").toString();
String absPath = Path.of("/etc/passwd").toString();
// Only test on systems where /etc/passwd is absolute
if (Paths.get(absPath).isAbsolute()) {
if (Path.of(absPath).isAbsolute()) {
MockMultipartFile file =
new MockMultipartFile(
"fileInput", absPath, "application/pdf", "data".getBytes());
@@ -207,16 +207,14 @@ public class AuditDashboardController {
// Include standard enum types in case they're not in the database yet
List<String> enumTypes =
Arrays.stream(AuditEventType.values())
.map(AuditEventType::name)
.collect(Collectors.toList());
Arrays.stream(AuditEventType.values()).map(AuditEventType::name).toList();
// Combine both sources, remove duplicates, and sort
Set<String> combinedTypes = new HashSet<>();
combinedTypes.addAll(dbTypes);
combinedTypes.addAll(enumTypes);
return combinedTypes.stream().sorted().collect(Collectors.toList());
return combinedTypes.stream().sorted().toList();
}
/** Export audit data as CSV. */
@@ -118,7 +118,7 @@ public class AuditRestController {
// Convert to response format expected by frontend
List<AuditEventDto> eventDtos =
events.getContent().stream().map(this::convertToDto).collect(Collectors.toList());
events.getContent().stream().map(this::convertToDto).toList();
AuditEventsResponse response =
AuditEventsResponse.builder()
@@ -191,19 +191,13 @@ public class AuditRestController {
ChartData eventsByTypeChart =
ChartData.builder()
.labels(new ArrayList<>(eventsByType.keySet()))
.values(
eventsByType.values().stream()
.map(Long::intValue)
.collect(Collectors.toList()))
.values(eventsByType.values().stream().map(Long::intValue).toList())
.build();
ChartData eventsByUserChart =
ChartData.builder()
.labels(new ArrayList<>(eventsByUser.keySet()))
.values(
eventsByUser.values().stream()
.map(Long::intValue)
.collect(Collectors.toList()))
.values(eventsByUser.values().stream().map(Long::intValue).toList())
.build();
// Sort events by day for time series
@@ -211,10 +205,7 @@ public class AuditRestController {
ChartData eventsOverTimeChart =
ChartData.builder()
.labels(new ArrayList<>(sortedEventsByDay.keySet()))
.values(
sortedEventsByDay.values().stream()
.map(Long::intValue)
.collect(Collectors.toList()))
.values(sortedEventsByDay.values().stream().map(Long::intValue).toList())
.build();
AuditChartsData chartsData =
@@ -239,16 +230,14 @@ public class AuditRestController {
// Include standard enum types in case they're not in the database yet
List<String> enumTypes =
Arrays.stream(AuditEventType.values())
.map(AuditEventType::name)
.collect(Collectors.toList());
Arrays.stream(AuditEventType.values()).map(AuditEventType::name).toList();
// Combine both sources, remove duplicates, and sort
Set<String> combinedTypes = new HashSet<>();
combinedTypes.addAll(dbTypes);
combinedTypes.addAll(enumTypes);
List<String> result = combinedTypes.stream().sorted().collect(Collectors.toList());
List<String> result = combinedTypes.stream().sorted().toList();
return ResponseEntity.ok(result);
}
@@ -263,11 +252,7 @@ public class AuditRestController {
// Use the countByPrincipal query to get unique principals
List<Object[]> principalCounts = auditRepository.countByPrincipal();
List<String> users =
principalCounts.stream()
.map(arr -> (String) arr[0])
.sorted()
.collect(Collectors.toList());
List<String> users = principalCounts.stream().map(arr -> (String) arr[0]).sorted().toList();
return ResponseEntity.ok(users);
}
@@ -0,0 +1,144 @@
package stirling.software.proprietary.controller.api;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import io.github.pixee.security.Filenames;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import stirling.software.common.configuration.RuntimePathConfig;
import stirling.software.common.service.CustomPDFDocumentFactory;
import stirling.software.common.util.ProcessExecutor;
import stirling.software.common.util.TempFile;
import stirling.software.common.util.TempFileManager;
import stirling.software.common.util.WebResponseUtils;
/**
* Dispatchable tool that converts an AI-generated HTML string to a PDF via WeasyPrint.
*
* <p>Called by {@link stirling.software.proprietary.service.AiWorkflowService} when the engine
* emits a {@code CREATE_PDF_FROM_HTML_AGENT} plan step. The HTML comes from a trusted Jinja
* template so sanitization is intentionally skipped.
*/
@Slf4j
@Hidden
@RestController
@RequestMapping("/api/v1/ai/tools")
@RequiredArgsConstructor
@Tag(name = "AI Tools", description = "Dispatchable AI-backed tools.")
public class CreatePdfAgentController {
private final TempFileManager tempFileManager;
private final CustomPDFDocumentFactory pdfDocumentFactory;
private final RuntimePathConfig runtimePathConfig;
/**
* Returns true only when WeasyPrint is definitively unavailable — either the binary could not
* be launched at all, or it launched but immediately failed to load a required system library.
* Other conversion failures (bad HTML, output errors, etc.) return false so they surface as
* real errors rather than a misleading "dependency missing" message.
*/
private static boolean isMissingDependencyError(IOException e) {
String msg = e.getMessage();
if (msg == null) return false;
// OS could not start the process — binary not on PATH or not at the configured path.
if (msg.contains("Cannot run program")) return true;
// Process started but crashed immediately loading a shared library.
// "cannot load library" — Python/cffi error (Linux and macOS via pip)
// "Library not loaded" / "image not found" — macOS dyld error (Homebrew installs)
String lower = msg.toLowerCase();
if (lower.contains("cannot load library")) return true;
if (lower.contains("library not loaded")) return true;
if (lower.contains("image not found")) return true;
return false;
}
@PostMapping(
value = "/create-pdf-from-html-agent",
consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@Operation(
summary = "Convert AI-generated HTML to a PDF",
description =
"Accepts an HTML document as a plain-text parameter and returns a PDF."
+ " This endpoint is dispatched by the AI workflow orchestrator as a"
+ " plan step; it is not intended for direct client use.")
public ResponseEntity<Resource> createPdfFromHtml(
@RequestParam("htmlContent") String htmlContent,
@RequestParam("filename") String filename)
throws Exception {
log.info(
"[create-pdf-agent] converting HTML to PDF via WeasyPrint — html_bytes={}",
htmlContent.length());
try (TempFile htmlFile = tempFileManager.createManagedTempFile(".html");
TempFile pdfFile = tempFileManager.createManagedTempFile(".pdf")) {
Files.writeString(htmlFile.getPath(), htmlContent, StandardCharsets.UTF_8);
List<String> command = new ArrayList<>();
command.add(runtimePathConfig.getWeasyPrintPath());
command.add("-e");
command.add("utf-8");
command.add("-v");
// SSRF: the HTML is self-contained and the engine validates style colours, so no
// external url() reaches WeasyPrint. For full isolation, run it network-isolated.
command.add(htmlFile.getAbsolutePath());
command.add(pdfFile.getAbsolutePath());
try {
ProcessExecutor.getInstance(ProcessExecutor.Processes.WEASYPRINT)
.runCommandWithOutputHandling(command);
} catch (IOException e) {
if (isMissingDependencyError(e)) {
throw new IOException(
"AI document creation is not available on this server because a required"
+ " system dependency is not installed. Please contact your"
+ " system administrator.");
}
throw e;
}
String safeFilename = Filenames.toSimpleFileName(filename);
if (safeFilename == null || safeFilename.isBlank() || !safeFilename.endsWith(".pdf")) {
safeFilename = "generated-document.pdf";
}
// Stamp the standard Stirling metadata onto the WeasyPrint output and write the result
// straight to the response temp file. Loading from the file and saving to the file
// avoids materialising the whole document as a byte[] twice (read-all + re-serialise),
// which matters for large generated documents.
TempFile tempOut = tempFileManager.createManagedTempFile(".pdf");
try (PDDocument document = pdfDocumentFactory.load(pdfFile.getPath())) {
document.save(tempOut.getPath().toFile());
} catch (Exception e) {
tempOut.close();
throw e;
}
log.info(
"[create-pdf-agent] PDF ready — filename={} bytes={}",
safeFilename,
Files.size(tempOut.getPath()));
return WebResponseUtils.pdfFileToWebResponse(tempOut, safeFilename);
}
}
}
@@ -3,7 +3,6 @@ package stirling.software.proprietary.controller.api;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
@@ -183,7 +182,7 @@ public class SignatureController {
*/
private boolean deleteFromSharedFolder(String signatureId) throws IOException {
String signatureBasePath = InstallationPathConfig.getSignaturesPath();
Path sharedFolder = Paths.get(signatureBasePath, ALL_USERS_FOLDER);
Path sharedFolder = Path.of(signatureBasePath, ALL_USERS_FOLDER);
boolean deleted = false;
if (Files.exists(sharedFolder)) {
@@ -3,7 +3,6 @@ package stirling.software.proprietary.controller.api;
import java.time.Duration;
import java.time.Instant;
import java.util.*;
import java.util.stream.Collectors;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
@@ -85,7 +84,7 @@ public class UsageRestController {
.build();
})
.sorted(Comparator.comparingInt(EndpointStatistic::getVisits).reversed())
.collect(Collectors.toList());
.toList();
// Apply limit if specified
if (limit != null && limit > 0 && statistics.size() > limit) {
@@ -3,7 +3,6 @@ package stirling.software.proprietary.security.configuration.ee;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.annotation.Lazy;
@@ -104,7 +103,7 @@ public class LicenseKeyChecker {
if (keyOrFilePath.startsWith(FILE_PREFIX)) {
String filePath = keyOrFilePath.substring(FILE_PREFIX.length());
try {
Path path = Paths.get(filePath);
Path path = Path.of(filePath);
if (!Files.exists(path)) {
log.error("License file does not exist: {}", filePath);
return null;
@@ -4,7 +4,6 @@ import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.HashMap;
import java.util.Map;
@@ -329,7 +328,7 @@ public class AdminLicenseController {
}
// Get config directory and target path
Path configPath = Paths.get(InstallationPathConfig.getConfigPath());
Path configPath = Path.of(InstallationPathConfig.getConfigPath());
Path configPathAbs = configPath.toAbsolutePath().normalize();
Path targetPath = configPathAbs.resolve(filename).normalize();
log.info(
@@ -702,7 +702,7 @@ public class AdminSettingsController {
if (path != null && !path.trim().isEmpty()) {
try {
java.nio.file.Path normalized =
java.nio.file.Paths.get(path.trim()).toAbsolutePath().normalize();
java.nio.file.Path.of(path.trim()).toAbsolutePath().normalize();
String normalizedStr = normalized.toString();
// Check for duplicates
@@ -719,9 +719,9 @@ public class AdminSettingsController {
// Check for overlapping paths
java.util.List<String> pathList = new java.util.ArrayList<>(normalizedPaths);
for (int i = 0; i < pathList.size(); i++) {
java.nio.file.Path path1 = java.nio.file.Paths.get(pathList.get(i));
java.nio.file.Path path1 = java.nio.file.Path.of(pathList.get(i));
for (int j = i + 1; j < pathList.size(); j++) {
java.nio.file.Path path2 = java.nio.file.Paths.get(pathList.get(j));
java.nio.file.Path path2 = java.nio.file.Path.of(pathList.get(j));
if (path1.startsWith(path2) || path2.startsWith(path1)) {
return "Overlapping paths detected: " + path1 + " and " + path2;
}
@@ -3,7 +3,6 @@ package stirling.software.proprietary.security.controller.api;
import java.security.Principal;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
@@ -280,7 +279,7 @@ public class InviteLinkController {
"expiresAt", invite.getExpiresAt().toString());
return inviteMap;
})
.collect(Collectors.toList());
.toList();
return ResponseEntity.ok(Map.of("invites", inviteList));
@@ -331,7 +330,7 @@ public class InviteLinkController {
List<InviteToken> expiredInvites =
inviteTokenRepository.findAll().stream()
.filter(invite -> !invite.isValid())
.collect(Collectors.toList());
.toList();
int count = expiredInvites.size();
inviteTokenRepository.deleteAll(expiredInvites);
@@ -6,7 +6,6 @@ import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.*;
import java.util.regex.Pattern;
@@ -53,7 +52,7 @@ public class UIDataTessdataController {
TessdataLanguagesResponse response = new TessdataLanguagesResponse();
response.setInstalled(getAvailableTesseractLanguages());
response.setAvailable(getRemoteTessdataLanguages());
response.setWritable(isWritableDirectory(Paths.get(runtimePathConfig.getTessDataPath())));
response.setWritable(isWritableDirectory(Path.of(runtimePathConfig.getTessDataPath())));
return ResponseEntity.ok(response);
}
@@ -67,7 +66,7 @@ public class UIDataTessdataController {
.body(Map.of("message", "No languages provided for download"));
}
Path tessdataDir = Paths.get(runtimePathConfig.getTessDataPath());
Path tessdataDir = Path.of(runtimePathConfig.getTessDataPath());
try {
Files.createDirectories(tessdataDir);
} catch (IOException e) {
@@ -994,7 +994,7 @@ public class UserController {
userRepository.findAll().stream()
.filter(User::isEnabled)
.map(this::toUserSummaryDTO)
.collect(java.util.stream.Collectors.toList());
.toList();
return ResponseEntity.ok(users);
}
@@ -4,7 +4,6 @@ import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.security.MessageDigest;
@@ -23,7 +22,6 @@ import java.util.Comparator;
import java.util.List;
import java.util.UUID;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import javax.sql.DataSource;
@@ -100,7 +98,7 @@ public class DatabaseService implements DatabaseServiceInterface {
ApplicationProperties.Datasource datasourceProps,
DataSource dataSource,
DatabaseNotificationServiceInterface backupNotificationService) {
this.BACKUP_DIR = Paths.get(InstallationPathConfig.getBackupPath()).normalize();
this.BACKUP_DIR = Path.of(InstallationPathConfig.getBackupPath()).normalize();
this.datasourceProps = datasourceProps;
this.dataSource = dataSource;
this.backupNotificationService = backupNotificationService;
@@ -111,7 +109,7 @@ public class DatabaseService implements DatabaseServiceInterface {
@Deprecated(since = "2.0.0", forRemoval = true)
private void moveBackupFiles() {
Path sourceDir =
Paths.get(InstallationPathConfig.getConfigPath(), "db", "backup").normalize();
Path.of(InstallationPathConfig.getConfigPath(), "db", "backup").normalize();
if (!Files.exists(sourceDir)) {
log.info("Source directory does not exist: {}", sourceDir);
@@ -217,7 +215,7 @@ public class DatabaseService implements DatabaseServiceInterface {
List<FileInfo> backupList = this.getBackupList();
backupList.sort(Comparator.comparing(FileInfo::getModificationDate).reversed());
Path latestExport = Paths.get(backupList.get(0).getFilePath());
Path latestExport = Path.of(backupList.get(0).getFilePath());
executeDatabaseScript(latestExport);
}
@@ -258,9 +256,13 @@ public class DatabaseService implements DatabaseServiceInterface {
@Override
public void exportDatabase() {
List<FileInfo> filteredBackupList =
this.getBackupList().stream()
.filter(backup -> !backup.getFileName().startsWith(BACKUP_PREFIX + "user_"))
.collect(Collectors.toList());
new ArrayList<>(
this.getBackupList().stream()
.filter(
backup ->
!backup.getFileName()
.startsWith(BACKUP_PREFIX + "user_"))
.toList());
if (filteredBackupList.size() > 5) {
deleteOldestBackup(filteredBackupList);
@@ -341,7 +343,7 @@ public class DatabaseService implements DatabaseServiceInterface {
for (FileInfo backup : backupList) {
try {
Files.deleteIfExists(Paths.get(backup.getFilePath()));
Files.deleteIfExists(Path.of(backup.getFilePath()));
deletedFiles.add(Pair.of(backup, true));
} catch (IOException e) {
log.error("Error deleting backup file: {}", backup.getFileName(), e);
@@ -359,7 +361,7 @@ public class DatabaseService implements DatabaseServiceInterface {
if (!backupList.isEmpty()) {
FileInfo lastBackup = backupList.get(backupList.size() - 1);
try {
Files.deleteIfExists(Paths.get(lastBackup.getFilePath()));
Files.deleteIfExists(Path.of(lastBackup.getFilePath()));
deletedFiles.add(Pair.of(lastBackup, true));
} catch (IOException e) {
log.error("Error deleting last backup file: {}", lastBackup.getFileName(), e);
@@ -381,7 +383,7 @@ public class DatabaseService implements DatabaseServiceInterface {
p -> p.getFileName().substring(7, p.getFileName().length() - 4)));
FileInfo oldestFile = filteredBackupList.get(0);
Files.deleteIfExists(Paths.get(oldestFile.getFilePath()));
Files.deleteIfExists(Path.of(oldestFile.getFilePath()));
log.info("Deleted oldest backup: {}", oldestFile.getFileName());
} catch (IOException e) {
log.error("Unable to delete oldest backup, message: {}", e.getMessage(), e);
@@ -3,7 +3,6 @@ package stirling.software.proprietary.security.service;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.TimeUnit;
@@ -76,7 +75,7 @@ public class KeyPairCleanupService {
return;
}
Path privateKeyDirectory = Paths.get(InstallationPathConfig.getPrivateKeyPath());
Path privateKeyDirectory = Path.of(InstallationPathConfig.getPrivateKeyPath());
Path keyFile = privateKeyDirectory.resolve(keyId + KeyPersistenceService.KEY_SUFFIX);
if (Files.exists(keyFile)) {
@@ -3,7 +3,6 @@ package stirling.software.proprietary.security.service;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
@@ -20,7 +19,6 @@ import java.time.format.DateTimeFormatter;
import java.util.Base64;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
@@ -82,7 +80,7 @@ public class KeyPersistenceService implements KeyPersistenceServiceInterface {
*/
private void loadExistingKeysFromDisk() {
try {
Path keyDirectory = Paths.get(InstallationPathConfig.getPrivateKeyPath());
Path keyDirectory = Path.of(InstallationPathConfig.getPrivateKeyPath());
if (!Files.exists(keyDirectory)) {
log.info("No existing keys found, generating new keypair");
@@ -99,7 +97,7 @@ public class KeyPersistenceService implements KeyPersistenceServiceInterface {
b.getFileName().compareTo(a.getFileName())) // Most
// recent
// first
.collect(Collectors.toList());
.toList();
}
if (keyFiles.isEmpty()) {
@@ -275,7 +273,7 @@ public class KeyPersistenceService implements KeyPersistenceServiceInterface {
eligible);
return eligible;
})
.collect(Collectors.toList());
.toList();
}
private String generateKeyId() {
@@ -284,20 +282,17 @@ public class KeyPersistenceService implements KeyPersistenceServiceInterface {
}
private KeyPair generateRSAKeypair() {
KeyPairGenerator keyPairGenerator = null;
try {
keyPairGenerator = KeyPairGenerator.getInstance("RSA");
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(2048);
return keyPairGenerator.generateKeyPair();
} catch (NoSuchAlgorithmException e) {
log.error("Failed to initialize RSA key pair generator", e);
throw new IllegalStateException("RSA key pair generator is not available", e);
}
return keyPairGenerator.generateKeyPair();
}
private void ensurePrivateKeyDirectoryExists() throws IOException {
Path keyPath = Paths.get(InstallationPathConfig.getPrivateKeyPath());
Path keyPath = Path.of(InstallationPathConfig.getPrivateKeyPath());
if (!Files.exists(keyPath)) {
Files.createDirectories(keyPath);
@@ -312,7 +307,7 @@ public class KeyPersistenceService implements KeyPersistenceServiceInterface {
* <p>Public key stored as: keyId.pub
*/
private void storeKeyPair(String keyId, KeyPair keyPair) throws IOException {
Path keyDirectory = Paths.get(InstallationPathConfig.getPrivateKeyPath());
Path keyDirectory = Path.of(InstallationPathConfig.getPrivateKeyPath());
// Store private key
Path privateKeyFile = keyDirectory.resolve(keyId + KEY_SUFFIX);
@@ -346,7 +341,7 @@ public class KeyPersistenceService implements KeyPersistenceServiceInterface {
private PrivateKey loadPrivateKey(String keyId)
throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
Path keyFile =
Paths.get(InstallationPathConfig.getPrivateKeyPath()).resolve(keyId + KEY_SUFFIX);
Path.of(InstallationPathConfig.getPrivateKeyPath()).resolve(keyId + KEY_SUFFIX);
if (!Files.exists(keyFile)) {
throw new IOException("Private key not found: " + keyFile);
@@ -369,8 +364,7 @@ public class KeyPersistenceService implements KeyPersistenceServiceInterface {
*/
private String loadPublicKey(String keyId) throws IOException {
Path publicKeyFile =
Paths.get(InstallationPathConfig.getPrivateKeyPath())
.resolve(keyId + PUB_KEY_SUFFIX);
Path.of(InstallationPathConfig.getPrivateKeyPath()).resolve(keyId + PUB_KEY_SUFFIX);
if (!Files.exists(publicKeyFile)) {
throw new IOException("Public key not found: " + publicKeyFile);
@@ -5,7 +5,6 @@ import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import org.springframework.stereotype.Service;
@@ -101,7 +100,7 @@ public class LoginAttemptService {
return attemptsCache.entrySet().stream()
.filter(entry -> entry.getValue().getAttemptCount() >= MAX_ATTEMPT)
.map(Map.Entry::getKey)
.collect(Collectors.toList());
.toList();
}
public int getRemainingAttempts(String key) {
@@ -31,21 +31,14 @@ public class RefreshRateLimitService {
this.jwtProperties = applicationProperties.getSecurity().getJwt();
}
private static class RefreshAttempt {
private final AtomicInteger count = new AtomicInteger(0);
private final Instant firstAttempt = Instant.now();
private record RefreshAttempt(AtomicInteger count, Instant firstAttempt) {
RefreshAttempt() {
this(new AtomicInteger(0), Instant.now());
}
int incrementAndGet() {
return count.incrementAndGet();
}
Instant getFirstAttempt() {
return firstAttempt;
}
int getCount() {
return count.get();
}
}
private final Map<String, RefreshAttempt> attempts = new ConcurrentHashMap<>();
@@ -72,7 +65,7 @@ public class RefreshRateLimitService {
// Clean up if outside grace window
Instant cutoff = Instant.now().minusMillis(graceWindowMillis);
if (attempt.getFirstAttempt().isBefore(cutoff)) {
if (attempt.firstAttempt().isBefore(cutoff)) {
attempts.remove(tokenHash);
}
@@ -98,15 +91,9 @@ public class RefreshRateLimitService {
? configuredMinutes
: JwtConstants.DEFAULT_REFRESH_GRACE_MINUTES;
Instant cutoff = Instant.now().minusMillis(graceMinutes * 60000L);
int removed =
attempts.entrySet().stream()
.filter(entry -> entry.getValue().getFirstAttempt().isBefore(cutoff))
.mapToInt(
entry -> {
attempts.remove(entry.getKey());
return 1;
})
.sum();
int beforeSize = attempts.size();
attempts.entrySet().removeIf(entry -> entry.getValue().firstAttempt().isBefore(cutoff));
int removed = beforeSize - attempts.size();
if (removed > 0) {
log.debug("Cleaned up {} expired refresh tracking entries", removed);
@@ -17,6 +17,7 @@ import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.http.MediaTypeFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.multipart.MultipartFile;
import io.github.pixee.security.Filenames;
@@ -175,7 +176,6 @@ public class AiWorkflowService {
initialRequest.setFiles(files);
initialRequest.setConversationHistory(new ArrayList<>(request.getConversationHistory()));
initialRequest.setEnabledEndpoints(endpointResolver.getEnabledEndpointUrls());
listener.onProgress(AiWorkflowProgressEvent.of(AiWorkflowPhase.ANALYZING));
WorkflowState state = new WorkflowState.Pending(initialRequest);
@@ -580,6 +580,10 @@ public class AiWorkflowService {
log.error("Plan step on tool {} timed out: {}", e.getEndpointPath(), e.getMessage());
return new WorkflowState.Terminal(
cannotContinue(toolTimeoutMessage(e.getEndpointPath(), e)));
} catch (HttpServerErrorException e) {
String reason = extractDetailFromHttpError(e);
log.error("Plan step failed (HTTP {}): {}", e.getStatusCode(), reason);
return new WorkflowState.Terminal(cannotContinue(reason));
} catch (Exception e) {
log.error("Failed to execute plan: {}", e.getMessage(), e);
return new WorkflowState.Terminal(
@@ -605,6 +609,27 @@ public class AiWorkflowService {
return String.format("The %s tool failed: %s", endpointPath, reason);
}
/**
* Extracts the {@code detail} field from an HTTP error response body if it is valid JSON,
* otherwise falls back to the exception message. This lets controller-level error messages
* (e.g. missing system dependency) surface cleanly in the chat response.
*/
private String extractDetailFromHttpError(HttpServerErrorException e) {
try {
String body = e.getResponseBodyAsString();
if (body != null && !body.isBlank()) {
JsonNode node = objectMapper.readTree(body);
JsonNode detail = node.get("detail");
if (detail != null && detail.isTextual() && !detail.asText().isBlank()) {
return detail.asText();
}
}
} catch (Exception ignored) {
// fall through to generic message
}
return "The request could not be completed. Please try again or contact your system administrator.";
}
/**
* Adapt the AI workflow's {@link ProgressListener} to the engine's {@link
* PolicyProgressListener}: each step start maps to an {@code EXECUTING_TOOL} progress event
@@ -11,7 +11,6 @@ import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.apache.commons.lang3.StringUtils;
@@ -413,7 +412,7 @@ public class AuditService {
return m;
})
.collect(Collectors.toList());
.toList();
data.put("files", fileInfos);
}
@@ -4,7 +4,6 @@ import java.io.*;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.*;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
@@ -64,7 +63,7 @@ public class ServerCertificateService implements ServerCertificateServiceInterfa
}
private Path getKeystorePath() {
return Paths.get(InstallationPathConfig.getConfigPath(), KEYSTORE_FILENAME);
return Path.of(InstallationPathConfig.getConfigPath(), KEYSTORE_FILENAME);
}
private boolean hasProOrEnterpriseAccess() {
@@ -5,7 +5,6 @@ import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Base64;
@@ -55,7 +54,7 @@ public class SignatureService implements PersonalSignatureServiceInterface {
@Override
public byte[] getPersonalSignatureBytes(String username, String fileName) throws IOException {
validateFileName(fileName);
Path userPath = Paths.get(SIGNATURE_BASE_PATH, username, fileName);
Path userPath = Path.of(SIGNATURE_BASE_PATH, username, fileName);
if (!Files.exists(userPath)) {
throw new FileNotFoundException("Personal signature not found");
@@ -76,7 +75,7 @@ public class SignatureService implements PersonalSignatureServiceInterface {
}
String folderName = "shared".equals(scope) ? ALL_USERS_FOLDER : username;
Path targetFolder = Paths.get(SIGNATURE_BASE_PATH, folderName);
Path targetFolder = Path.of(SIGNATURE_BASE_PATH, folderName);
// Only enforce limits for personal signatures (not shared)
if ("personal".equals(scope)) {
@@ -170,13 +169,13 @@ public class SignatureService implements PersonalSignatureServiceInterface {
List<SavedSignatureResponse> signatures = new ArrayList<>();
// Load personal signatures
Path personalFolder = Paths.get(SIGNATURE_BASE_PATH, username);
Path personalFolder = Path.of(SIGNATURE_BASE_PATH, username);
if (Files.exists(personalFolder)) {
signatures.addAll(loadSignaturesFromFolder(personalFolder, "personal", true));
}
// Load shared signatures
Path sharedFolder = Paths.get(SIGNATURE_BASE_PATH, ALL_USERS_FOLDER);
Path sharedFolder = Path.of(SIGNATURE_BASE_PATH, ALL_USERS_FOLDER);
if (Files.exists(sharedFolder)) {
signatures.addAll(loadSignaturesFromFolder(sharedFolder, "shared", false));
}
@@ -189,7 +188,7 @@ public class SignatureService implements PersonalSignatureServiceInterface {
validateFileName(signatureId);
// Only allow deletion from personal folder
Path personalFolder = Paths.get(SIGNATURE_BASE_PATH, username);
Path personalFolder = Path.of(SIGNATURE_BASE_PATH, username);
boolean deleted = false;
if (Files.exists(personalFolder)) {
@@ -227,7 +226,7 @@ public class SignatureService implements PersonalSignatureServiceInterface {
validateFileName(signatureId);
// Try personal folder first
Path personalFolder = Paths.get(SIGNATURE_BASE_PATH, username);
Path personalFolder = Path.of(SIGNATURE_BASE_PATH, username);
Path metadataPath = personalFolder.resolve(signatureId + ".json");
if (Files.exists(metadataPath)) {
@@ -237,7 +236,7 @@ public class SignatureService implements PersonalSignatureServiceInterface {
}
// If not found in personal, try shared folder
Path sharedFolder = Paths.get(SIGNATURE_BASE_PATH, ALL_USERS_FOLDER);
Path sharedFolder = Path.of(SIGNATURE_BASE_PATH, ALL_USERS_FOLDER);
Path sharedMetadataPath = sharedFolder.resolve(signatureId + ".json");
if (Files.exists(sharedMetadataPath)) {
@@ -251,7 +250,7 @@ public class SignatureService implements PersonalSignatureServiceInterface {
public boolean isSharedSignature(String signatureId) {
validateFileName(signatureId);
Path sharedFolder = Paths.get(SIGNATURE_BASE_PATH, ALL_USERS_FOLDER);
Path sharedFolder = Path.of(SIGNATURE_BASE_PATH, ALL_USERS_FOLDER);
return Files.exists(sharedFolder.resolve(signatureId + ".json"));
}
@@ -274,7 +273,7 @@ public class SignatureService implements PersonalSignatureServiceInterface {
// Private helper methods
private void enforceStorageLimits(String username, String dataUrlToAdd) throws IOException {
Path userFolder = Paths.get(SIGNATURE_BASE_PATH, username);
Path userFolder = Path.of(SIGNATURE_BASE_PATH, username);
if (!Files.exists(userFolder)) {
return; // First signature, no limits to check
@@ -3,7 +3,6 @@ package stirling.software.proprietary.storage.config;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Locale;
import java.util.Optional;
@@ -58,8 +57,8 @@ public class StorageProviderConfig {
}
basePathValue = InstallationPathConfig.getPath() + "storage";
}
Path basePath = Paths.get(basePathValue).toAbsolutePath().normalize();
Path installRoot = Paths.get(InstallationPathConfig.getPath()).toAbsolutePath().normalize();
Path basePath = Path.of(basePathValue).toAbsolutePath().normalize();
Path installRoot = Path.of(InstallationPathConfig.getPath()).toAbsolutePath().normalize();
if (!basePath.startsWith(installRoot)) {
// Warn rather than hard-fail: admins may legitimately point storage at an external
// volume, but an unexpected path could indicate a misconfiguration or traversal
@@ -4,7 +4,6 @@ import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Optional;
import java.util.UUID;
@@ -80,7 +79,7 @@ public class LocalStorageProvider implements StorageProvider {
if (filename == null || filename.isBlank()) {
return "file";
}
String stripped = Paths.get(filename).getFileName().toString().replaceAll("\\p{Cntrl}", "");
String stripped = Path.of(filename).getFileName().toString().replaceAll("\\p{Cntrl}", "");
return stripped.isBlank() ? "file" : stripped;
}
}
@@ -4,7 +4,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.time.Duration;
import java.util.Optional;
import java.util.UUID;
@@ -150,7 +150,7 @@ public class S3StorageProvider implements StorageProvider, AutoCloseable {
if (originalFilename == null || originalFilename.isBlank()) {
return null;
}
// Strip CR/LF and other control chars before path parsing (Paths.get throws on them on
// Strip CR/LF and other control chars before path parsing (Path.of throws on them on
// Windows, and they defeat header parsers).
String stripped = originalFilename.replaceAll("\\p{Cntrl}", "");
// Use only the basename to avoid leaking directory structure into the header.
@@ -184,7 +184,7 @@ public class S3StorageProvider implements StorageProvider, AutoCloseable {
if (filename == null || filename.isBlank()) {
return "file";
}
String stripped = Paths.get(filename).getFileName().toString().replaceAll("\\p{Cntrl}", "");
String stripped = Path.of(filename).getFileName().toString().replaceAll("\\p{Cntrl}", "");
return stripped.isBlank() ? "file" : stripped;
}
}
@@ -14,7 +14,6 @@ import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
@@ -361,7 +360,7 @@ public class FileStorageService {
return files.stream()
.sorted(Comparator.comparing(StoredFile::getCreatedAt).reversed())
.map(file -> buildResponse(file, user, roleByFileId.get(file.getId())))
.collect(Collectors.toList());
.toList();
}
public StoredFileResponse getAccessibleFileResponse(User user, Long fileId) {
@@ -400,7 +399,7 @@ public class FileStorageService {
.filter(Objects::nonNull)
.map(User::getUsername)
.sorted(String.CASE_INSENSITIVE_ORDER)
.collect(Collectors.toList())
.toList()
: List.of();
List<ShareLinkResponse> shareLinks =
ownedByCurrentUser && isShareLinksEnabled()
@@ -419,7 +418,7 @@ public class FileStorageService {
.expiresAt(share.getExpiresAt())
.build())
.sorted(Comparator.comparing(ShareLinkResponse::getCreatedAt))
.collect(Collectors.toList())
.toList()
: List.of();
List<SharedUserResponse> sharedUsers =
ownedByCurrentUser
@@ -440,7 +439,7 @@ public class FileStorageService {
Comparator.comparing(
SharedUserResponse::getUsername,
String.CASE_INSENSITIVE_ORDER))
.collect(Collectors.toList())
.toList()
: List.of();
return StoredFileResponse.builder()
.id(file.getId())
@@ -762,7 +761,7 @@ public class FileStorageService {
.accessType(access.getAccessType().name())
.accessedAt(access.getAccessedAt())
.build())
.collect(Collectors.toList());
.toList();
}
public List<FileShareAccess> listAccessedShareLinks(User user) {
@@ -818,7 +817,7 @@ public class FileStorageService {
.build();
})
.filter(response -> response.getShareToken() != null)
.collect(Collectors.toList());
.toList();
}
public void ensureSharingEnabled() {
@@ -1007,7 +1006,7 @@ public class FileStorageService {
file.getHistoryStorageKey(),
file.getAuditLogStorageKey())
.filter(value -> value != null && !value.isBlank())
.collect(Collectors.toList());
.toList();
}
private void cleanupStoredObject(StoredObject storedObject) {
@@ -76,7 +76,7 @@ public class SigningSessionController {
.map(
stirling.software.proprietary.workflow.util.WorkflowMapper
::toResponse)
.collect(java.util.stream.Collectors.toList());
.toList();
return ResponseEntity.ok(responses);
} catch (Exception e) {
log.error("Error listing sessions for user {}", principal.getName(), e);
@@ -98,7 +98,7 @@ public class SigningFinalizationService {
// Step 1.5: Add summary page BEFORE digital signing (if enabled)
// CRITICAL: Must be done before signing to avoid invalidating signatures
if (Boolean.TRUE.equals(settings.includeSummaryPage)) {
if (Boolean.TRUE.equals(settings.includeSummaryPage())) {
log.info(
"Adding summary page before digital signing for session {}",
session.getSessionId());
@@ -108,11 +108,13 @@ public class SigningFinalizationService {
// Suppress digital certificate visual block when summary page is enabled
// (wet signatures already applied in Step 1 and will still appear)
Boolean showVisualSignature =
Boolean.TRUE.equals(settings.includeSummaryPage) ? false : settings.showSignature;
Boolean.TRUE.equals(settings.includeSummaryPage())
? false
: settings.showSignature();
log.info(
"Finalization settings: includeSummaryPage={}, showVisualSignature={}",
settings.includeSummaryPage,
settings.includeSummaryPage(),
showVisualSignature);
// Step 2: Apply digital certificates per SIGNED participant
@@ -150,8 +152,8 @@ public class SigningFinalizationService {
log.info(
"Applying signature for {} with reason='{}', location='{}'",
fresh.getEmail(),
sigMeta.reason,
sigMeta.location);
sigMeta.reason(),
sigMeta.location());
pdf =
applyDigitalSignature(
@@ -159,10 +161,10 @@ public class SigningFinalizationService {
fresh,
submission,
showVisualSignature,
settings.pageNumber,
sigMeta.reason,
sigMeta.location,
settings.showLogo);
settings.pageNumber(),
sigMeta.reason(),
sigMeta.location(),
settings.showLogo());
}
return pdf;
@@ -682,7 +684,7 @@ public class SigningFinalizationService {
textDark,
textMuted,
"Subject:",
certInfo.subjectCN);
certInfo.subjectCN());
rRowY -= LINE_H;
drawLabelValue(
cs,
@@ -693,7 +695,7 @@ public class SigningFinalizationService {
textDark,
textMuted,
"Issuer:",
certInfo.issuerCN);
certInfo.issuerCN());
rRowY -= LINE_H;
drawLabelValue(
cs,
@@ -704,7 +706,7 @@ public class SigningFinalizationService {
textDark,
textMuted,
"Serial:",
certInfo.serialNumber);
certInfo.serialNumber());
rRowY -= LINE_H;
drawLabelValue(
cs,
@@ -715,7 +717,7 @@ public class SigningFinalizationService {
textDark,
textMuted,
"Valid From:",
certInfo.validFrom);
certInfo.validFrom());
rRowY -= LINE_H;
drawLabelValue(
cs,
@@ -726,7 +728,7 @@ public class SigningFinalizationService {
textDark,
textMuted,
"Valid Until:",
certInfo.validUntil);
certInfo.validUntil());
rRowY -= LINE_H;
drawLabelValue(
cs,
@@ -737,7 +739,7 @@ public class SigningFinalizationService {
textDark,
textMuted,
"Algorithm:",
certInfo.algorithm);
certInfo.algorithm());
}
yPos -= cardH + 12;
@@ -1075,10 +1077,9 @@ public class SigningFinalizationService {
}
String alias = aliases.nextElement();
Certificate cert = keystore.getCertificate(alias);
if (!(cert instanceof X509Certificate)) {
if (!(cert instanceof X509Certificate x509)) {
return null;
}
X509Certificate x509 = (X509Certificate) cert;
String subjectCN = extractCN(x509.getSubjectX500Principal().getName());
String issuerCN = extractCN(x509.getIssuerX500Principal().getName());
@@ -1256,55 +1257,19 @@ public class SigningFinalizationService {
// ===== PRIVATE INNER TYPES =====
private static class SessionSignatureSettings {
final Boolean showSignature;
final Integer pageNumber;
final Boolean showLogo;
final Boolean includeSummaryPage;
private record SessionSignatureSettings(
Boolean showSignature,
Integer pageNumber,
Boolean showLogo,
Boolean includeSummaryPage) {}
SessionSignatureSettings(
Boolean showSignature,
Integer pageNumber,
Boolean showLogo,
Boolean includeSummaryPage) {
this.showSignature = showSignature;
this.pageNumber = pageNumber;
this.showLogo = showLogo;
this.includeSummaryPage = includeSummaryPage;
}
}
private record ParticipantSignatureMetadata(String reason, String location) {}
private static class ParticipantSignatureMetadata {
final String reason;
final String location;
ParticipantSignatureMetadata(String reason, String location) {
this.reason = reason;
this.location = location;
}
}
private static class CertificateInfo {
final String subjectCN;
final String issuerCN;
final String serialNumber;
final String validFrom;
final String validUntil;
final String algorithm;
CertificateInfo(
String subjectCN,
String issuerCN,
String serialNumber,
String validFrom,
String validUntil,
String algorithm) {
this.subjectCN = subjectCN;
this.issuerCN = issuerCN;
this.serialNumber = serialNumber;
this.validFrom = validFrom;
this.validUntil = validUntil;
this.algorithm = algorithm;
}
}
private record CertificateInfo(
String subjectCN,
String issuerCN,
String serialNumber,
String validFrom,
String validUntil,
String algorithm) {}
}
@@ -553,7 +553,7 @@ public class WorkflowSessionService {
dto.setMyStatus(p.getStatus());
return dto;
})
.collect(java.util.stream.Collectors.toList());
.toList();
}
/**
@@ -3,7 +3,6 @@ package stirling.software.proprietary.workflow.util;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -80,12 +79,12 @@ public class WorkflowMapper {
response.setParticipants(
session.getParticipants().stream()
.map(p -> toParticipantResponse(p, objectMapper, includeShareTokens))
.collect(Collectors.toList()));
.toList());
} else {
response.setParticipants(
session.getParticipants().stream()
.map(p -> toParticipantResponse(p, includeShareTokens))
.collect(Collectors.toList()));
.toList());
}
// Calculate participant counts
+3 -3
View File
@@ -16,7 +16,7 @@ Java forms the core of Stirling-PDF. When adding new features or handling errors
2. **Use `try-with-resources`** when working with streams or other closable resources to ensure clean-up even on failure.
3. **Return meaningful HTTP status codes** in controllers by throwing `ResponseStatusException` or using `@ExceptionHandler` methods.
4. **Log with context** using the project’s logging framework. Include identifiers or IDs that help trace the issue.
5. **Internationalise messages** by placing user-facing text in `messages_en_GB.properties` and referencing them with message keys.
5. **Internationalise messages** by placing user-facing text in `messages_en_US.properties` and referencing them with message keys.
## JavaScript
@@ -55,11 +55,11 @@ except Exception as err:
## Internationalisation (i18n)
All user-visible error strings should be defined in the main translation file (`messages_en_GB.properties`). Other language files will use the same keys. Refer to messages in code rather than hard-coding text.
All user-visible error strings should be defined in the main translation file (`messages_en_US.properties`). Other language files will use the same keys. Refer to messages in code rather than hard-coding text.
When creating new messages:
1. Add the English phrase to `messages_en_GB.properties`.
1. Add the English phrase to `messages_en_US.properties`.
2. Reference the message key in your Java, JavaScript, or Python code.
3. Update other localisation files as needed.
+4 -4
View File
@@ -16,7 +16,7 @@ Fork Stirling-PDF and create a new branch out of `main`.
- Use hyphenated format: `pl-PL` (not underscore)
2. Copy the reference translation file:
- Source: `frontend/editor/public/locales/en-GB/translation.toml`
- Source: `frontend/editor/public/locales/en-US/translation.toml`
- Destination: `frontend/editor/public/locales/pl-PL/translation.toml`
3. Translate all entries in the TOML file
@@ -47,10 +47,10 @@ ignore = [
## Add New Translation Tags
> [!IMPORTANT]
> If you add any new translation tags, they must first be added to the `en-GB/translation.toml` file. This ensures consistency across all language files.
> If you add any new translation tags, they must first be added to the `en-US/translation.toml` file. This ensures consistency across all language files.
- New translation tags **must be added** to `frontend/editor/public/locales/en-GB/translation.toml` to maintain a reference for other languages.
- After adding the new tags to `en-GB/translation.toml`, add and translate them in the respective language file (e.g., `pl-PL/translation.toml`).
- New translation tags **must be added** to `frontend/editor/public/locales/en-US/translation.toml` to maintain a reference for other languages.
- After adding the new tags to `en-US/translation.toml`, add and translate them in the respective language file (e.g., `pl-PL/translation.toml`).
- Use the scripts in `scripts/translations/` to validate and manage translations (see `scripts/translations/README.md`)
Make sure to place the entry under the correct language section. This helps maintain the accuracy of translation progress statistics and ensures that the translation tool or scripts do not misinterpret the completion rate.
+1 -1
View File
@@ -5,7 +5,7 @@ ARG BASE_VERSION=1.0.2
ARG BASE_IMAGE=stirlingtools/stirling-pdf-base:${BASE_VERSION}
# Stage 1: Build the Java application and frontend
FROM gradle:9.3.1-jdk25@sha256:85aec999629f4774a383cb792da4b598bdf5a7e69c4b9570bb70c0f919179183 AS app-build
FROM gradle:9.5.1-jdk25@sha256:8de3543f1772bb66be3b275893e5977b6d8bd2b0d25551faa5846a821d1f0600 AS app-build
ARG TASK_VERSION=3.49.1
RUN apt-get update \
+1 -1
View File
@@ -6,7 +6,7 @@ ARG BASE_VERSION=1.0.2
ARG BASE_IMAGE=stirlingtools/stirling-pdf-base:${BASE_VERSION}
# Stage 1: Build the Java application and frontend
FROM gradle:9.3.1-jdk25 AS app-build
FROM gradle:9.5.1-jdk25@sha256:8de3543f1772bb66be3b275893e5977b6d8bd2b0d25551faa5846a821d1f0600 AS app-build
ARG TASK_VERSION=3.49.1
RUN apt-get update \
+1 -1
View File
@@ -2,7 +2,7 @@
# Single JAR contains both frontend and backend with minimal dependencies
# Stage 1: Build application with embedded frontend
FROM gradle:9.3.1-jdk25 AS build
FROM gradle:9.5.1-jdk25@sha256:8de3543f1772bb66be3b275893e5977b6d8bd2b0d25551faa5846a821d1f0600 AS build
# Install Node.js and npm for frontend build
ARG TASK_VERSION=3.49.1
+1 -1
View File
@@ -3,7 +3,7 @@
## Overview
The script [`scripts/counter_translation.py`](../scripts/counter_translation.py) checks the translation progress of the property files in the directory `app/core/src/main/resources/`.
It compares each `messages_*.properties` file with the English reference file `messages_en_GB.properties` and calculates a percentage of completion for each language.
It compares each `messages_*.properties` file with the English reference file `messages_en_US.properties` and calculates a percentage of completion for each language.
In addition to console output, the script automatically updates the progress badges in the project’s `README.md` and maintains the configuration file [`scripts/ignore_translation.toml`](../scripts/ignore_translation.toml), which lists translation keys to be ignored for each language.
+1
View File
@@ -5,6 +5,7 @@ description = "AI Document Engine"
requires-python = ">=3.13"
dependencies = [
"fastapi>=0.116.0",
"jinja2>=3.1.0",
"pgvector>=0.3.6",
"psycopg[binary]>=3.2",
"pydantic>=2.0.0",
+2
View File
@@ -2,6 +2,7 @@
from .execution import ExecutionPlanningAgent
from .orchestrator import OrchestratorAgent
from .pdf_create import PdfCreateAgent
from .pdf_edit import PdfEditAgent, PdfEditParameterSelector, PdfEditPlanSelection
from .pdf_questions import PdfQuestionAgent
from .pdf_review import PdfReviewAgent
@@ -10,6 +11,7 @@ from .user_spec import UserSpecAgent
__all__ = [
"ExecutionPlanningAgent",
"OrchestratorAgent",
"PdfCreateAgent",
"PdfEditAgent",
"PdfEditParameterSelector",
"PdfEditPlanSelection",
+22 -1
View File
@@ -8,6 +8,7 @@ from pydantic_ai import Agent
from pydantic_ai.output import ToolOutput
from pydantic_ai.tools import RunContext
from stirling.agents.pdf_create import PdfCreateAgent
from stirling.agents.pdf_edit import PdfEditAgent
from stirling.agents.pdf_questions import PdfQuestionAgent
from stirling.agents.pdf_review import PdfReviewAgent
@@ -26,6 +27,7 @@ from stirling.contracts import (
format_conversation_history,
format_file_names,
)
from stirling.contracts.pdf_create import PdfCreateOrchestrateResponse
from stirling.services import AppRuntime
logger = logging.getLogger(__name__)
@@ -76,6 +78,16 @@ class OrchestratorAgent:
"Delegate requests to convert a PDF to Markdown or extract its content as readable text."
),
),
ToolOutput(
self.delegate_pdf_create,
name="delegate_pdf_create",
description=(
"Delegate requests to create a new PDF document from scratch based on a"
" description. Use this when the user wants to generate a new document"
" (e.g. 'create an invoice', 'write a report', 'make a contract',"
" 'draft a letter'). No input file is required."
),
),
ToolOutput(
self.unsupported_capability,
name="unsupported_capability",
@@ -92,6 +104,8 @@ class OrchestratorAgent:
"Use delegate_pdf_review when the user wants the PDF returned with review"
" comments attached — anything like 'review this', 'annotate with comments',"
" 'leave feedback on the PDF'. "
"Use delegate_pdf_create when the user wants to generate a new document from"
" scratch with no input file — invoices, reports, letters, contracts, etc. "
"Use delegate_pdf_ingest for any request to convert a PDF to Markdown "
"or extract its content as readable text. "
"Use unsupported_capability when the user asks about the assistant itself "
@@ -133,12 +147,13 @@ class OrchestratorAgent:
return await self._run_pdf_edit(request)
case SupportedCapability.AGENT_DRAFT:
return await self._run_agent_draft(request)
case SupportedCapability.PDF_CREATE:
return await self._run_pdf_create(request)
case (
SupportedCapability.ORCHESTRATE
| SupportedCapability.AGENT_REVISE
| SupportedCapability.AGENT_NEXT_ACTION
| SupportedCapability.MATH_AUDITOR_AGENT
| SupportedCapability.PDF_TO_MARKDOWN
):
raise ValueError(f"Cannot resume orchestrator with capability: {capability}")
case _ as unreachable:
@@ -175,6 +190,12 @@ class OrchestratorAgent:
async def _run_pdf_review(self, request: OrchestratorRequest) -> PdfReviewOrchestrateResponse:
return await PdfReviewAgent(self.runtime).orchestrate(request)
async def delegate_pdf_create(self, ctx: RunContext[OrchestratorDeps]) -> PdfCreateOrchestrateResponse:
return await self._run_pdf_create(ctx.deps.request)
async def _run_pdf_create(self, request: OrchestratorRequest) -> PdfCreateOrchestrateResponse:
return await PdfCreateAgent(self.runtime).orchestrate(request)
async def unsupported_capability(
self,
ctx: RunContext[OrchestratorDeps],
@@ -0,0 +1,3 @@
from .agent import PdfCreateAgent
__all__ = ["PdfCreateAgent"]
@@ -0,0 +1,443 @@
"""PDF Create Agent — chunked multi-agent pipeline.
Flow:
1. MetaPlannerAgent (smart_model) analyses the request and produces DocumentMeta:
title, tone, shared terms, style, and cannot_do_reason. No sections yet.
2. SectionPlannerAgent (smart_model) reads the meta and produces DocumentSections:
ordered list of PlannedSection with heading, type, depth, and key_points.
3. Python assembles DocumentPlan from meta + sections, then groups sections into
chunks, each staying under the output-token ceiling.
4. SectionWriterAgents (smart_model) run in parallel via asyncio.gather.
Each returns a WrittenSections with fully populated DocumentSection objects.
5. The assembler collects sections in plan order → GeneratedDocument.
6. Jinja renders the document to HTML. The LLM never writes HTML.
The planner is split into two calls (meta then sections) so each LLM output schema
stays small enough for grammar compilation on all model tiers including Haiku.
"""
from __future__ import annotations
import asyncio
import logging
import re
from dataclasses import dataclass
from pathlib import Path
from jinja2 import Environment, FileSystemLoader
from pydantic_ai import Agent
from pydantic_ai.output import NativeOutput
from stirling.contracts import (
EditCannotDoResponse,
EditPlanResponse,
OrchestratorRequest,
ToolOperationStep,
format_conversation_history,
)
from stirling.contracts.pdf_create import (
DocumentMeta,
DocumentPlan,
DocumentSection,
DocumentSections,
GeneratedDocument,
PdfCreateOrchestrateResponse,
PlannedSection,
SectionDepth,
WrittenSections,
)
from stirling.models.agent_tool_models import AgentToolId, CreatePdfFromHtmlAgentParams
from stirling.services import AppRuntime
logger = logging.getLogger(__name__)
_TEMPLATES_DIR = Path(__file__).parent / "templates"
# ── Token budget ──────────────────────────────────────────────────────────────────────────────────
# Conservative per-section token estimates mapped from planner-assigned depth.
_DEPTH_TOKENS: dict[SectionDepth, int] = {
SectionDepth.BRIEF: 250,
SectionDepth.STANDARD: 550,
SectionDepth.DETAILED: 1200,
}
# Maximum output tokens per writer call. Stays well below the quality cliff (~4k).
_CHUNK_CEILING = 3000
# Cap on simultaneous writer calls so a large document doesn't open a burst of LLM
# connections and trip provider rate limits.
_MAX_PARALLEL_WRITERS = 10
# ── Chunk dataclass ───────────────────────────────────────────────────────────────────────────────
@dataclass(frozen=True)
class _Chunk:
index: int
sections: list[PlannedSection]
# Descriptions of neighbouring chunks from the plan — passed to writers as
# read-only context so they can open/close their sections naturally.
context_before: str | None
context_after: str | None
# ── Chunking logic ────────────────────────────────────────────────────────────────────────────────
def _describe_sections(sections: list[PlannedSection]) -> str:
"""One-line summary of a chunk used as neighbour context."""
return "; ".join(f'"{s.heading}" ({s.type.value})' for s in sections)
def _make_chunks(sections: list[PlannedSection]) -> list[_Chunk]:
"""Group planned sections into chunks, each under _CHUNK_CEILING output tokens.
Section boundaries are atomic — a section is never split across chunks.
A single section whose estimated cost exceeds the ceiling gets its own chunk.
asyncio.gather preserves insertion order so chunk index is only used for logging.
"""
if not sections:
return []
groups: list[list[PlannedSection]] = []
current: list[PlannedSection] = []
current_tokens = 0
for section in sections:
cost = _DEPTH_TOKENS[section.depth]
if current and current_tokens + cost > _CHUNK_CEILING:
groups.append(current)
current = [section]
current_tokens = cost
else:
current.append(section)
current_tokens += cost
if current:
groups.append(current)
chunks: list[_Chunk] = []
for i, group in enumerate(groups):
context_before = _describe_sections(groups[i - 1]) if i > 0 else None
context_after = _describe_sections(groups[i + 1]) if i < len(groups) - 1 else None
chunks.append(
_Chunk(
index=i,
sections=group,
context_before=context_before,
context_after=context_after,
)
)
return chunks
# ── Prompts ───────────────────────────────────────────────────────────────────────────────────────
_META_PLANNER_SYSTEM_PROMPT = """\
You are a document planner. Your job is Step 1 of 2: produce the document header — NOT the
section list (that comes in Step 2) and NOT any body text (section writers handle that).
Analyse the user's request and produce a DocumentMeta with:
- title, subtitle (if appropriate), reference_number (only if the user supplies one explicitly)
- tone_brief: one sentence describing register and style
(e.g. "Formal legal language, third person, present tense." or
"Professional business tone, active voice.")
- shared_terms: consistent names for key entities AND ground-truth facts used throughout
the document. Two rules:
1. Capture EVERY value the user states explicitly that could be referenced in more than
one section. This includes — but is not limited to:
· Named parties, organisations, products, or systems
· Numeric values: amounts, quantities, percentages, durations, limits
· Identifiers: version numbers, reference codes, model names
· Dates and time periods
· Units of measure or currency
2. For any fact that will appear in two or more sections and that the user did NOT specify
(e.g. a default time, a standard rate, a typical threshold), assign ONE specific value
here. Do NOT let multiple writers independently invent the same fact.
Examples: {"the Agreement": "this Non-Disclosure Agreement", "the Client": "Acme Corp",
"contract value": "£120,000", "notice period": "30 days"}
- document_context: a single sentence anchoring the temporal or versioning context of the
document, if the user provides one. Leave empty if the user provides no such context.
- style_primary_color: accent and heading colour. Set ONLY when the user explicitly names a
colour or colour scheme (e.g. "make it red", "use navy blue"). Use CSS named colours
(e.g. "magenta", "navy", "crimson") or hex values. Leave null if no colour is stated.
- style_background_color: page background colour. Set only if explicitly requested.
- style_body_text_color: body text colour. Set only if explicitly requested.
- cannot_do_reason: set this ONLY when the request is not asking to create a document at all
(e.g. a question, a greeting, an edit request to an existing document). Never set it
because the document is large, complex, or technically detailed. Leave null otherwise.
RULES:
1. Extract ALL information the user provides. Do not invent content.
2. Do not produce any sections — that is Step 2.
"""
_SECTIONS_PLANNER_SYSTEM_PROMPT = """\
You are a document planner. Your job is Step 2 of 2: produce the ordered section list for
a document whose header has already been decided. Do NOT write any body text.
You will be given:
- The document meta (title, tone, shared terms, etc.) produced in Step 1
- The original user request
Produce a DocumentSections with an ordered list of PlannedSection objects.
For each section choose:
type — the most appropriate section type:
text — prose paragraphs (narrative, obligations, terms, descriptions)
key_value — labelled fields (parties, dates, metadata, identifiers)
line_items — tables with column headers (expenses, schedules, item lists)
bullet_list — unordered items (requirements, responsibilities, definitions)
signature — sign-off blocks for named parties or roles
depth — honest estimate of content volume:
brief (~250 tokens) — 1-2 items, a short paragraph, or a small table
standard (~550 tokens) — a few paragraphs, a medium table, or a moderate list
detailed (~1200 tokens) — long clauses, complex multi-row tables, or dense content
key_points — specific points this section MUST cover, taken directly from the user's input.
These are instructions to the writer, not summaries. Be precise and complete.
Every fact, name, date, amount, and requirement the user provides must appear somewhere.
For large documents, include enough key_points that the writer can produce substantial
content.
RULES:
1. Extract ALL information the user provides. Do not invent content.
2. Assign depth honestly — for a long detailed document most sections will be detailed.
3. For large documents, produce as many sections as needed — there is no section count limit.
4. Use the shared_terms from the meta exactly when writing key_points.
"""
_WRITER_SYSTEM_PROMPT = """\
You are a section writer for a structured document.
Write ONLY the sections assigned to you — no extras, no merging, no skipping.
SECTION TYPES — produce sections of exactly the requested type:
text — prose paragraphs. Use \\n\\n between paragraphs.
key_value — list of (label, value) pairs. Labels ≤ 5 words. Values verbatim from the data.
line_items — table. Every row must have exactly as many cells as there are columns.
bullet_list — flat list of items.
signature — list of signatory names/roles.
RULES:
1. Write ONLY the sections in your assignment list, in the order given.
2. Cover every key_point listed for each section. Do not omit any.
3. Use the shared_terms exactly — no paraphrasing or substituting alternatives.
Shared terms are ground truth. If your general knowledge or a common default would
produce a different value (e.g. a different duration, amount, date, or version number),
the shared term takes precedence. This applies everywhere in the document, including
boilerplate, FAQ, and summary sections.
4. Match the depth for each section: brief = concise, standard = moderate, \
detailed = thorough.
5. Maintain the document's tone throughout.
6. Do not reference other sections by number (e.g. "as defined in Section 3").
7. If a document_context is provided, use it to anchor any dates, versions, or time
references you generate. Do not invent a different temporal or versioning context.
"""
def _build_sections_prompt(meta: DocumentMeta, user_request: str, history: str) -> str:
lines: list[str] = [
"Document meta from Step 1:",
f" Title: {meta.title}",
f" Tone: {meta.tone_brief}",
]
if meta.subtitle:
lines.append(f" Subtitle: {meta.subtitle}")
if meta.document_context:
lines.append(f" Document context: {meta.document_context}")
if meta.shared_terms:
lines.append(" Shared terms:")
for term, referent in meta.shared_terms.items():
lines.append(f" {term} → {referent}")
lines.append(f"\nConversation history:\n{history}")
lines.append(f"\nUser request: {user_request}")
return "\n".join(lines)
def _build_writer_prompt(plan: DocumentPlan, chunk: _Chunk) -> str:
lines: list[str] = [
f"Document: {plan.title}",
f"Tone: {plan.tone_brief}",
]
if plan.document_context:
lines.append(f"Document context: {plan.document_context}")
if plan.shared_terms:
lines.append("Ground-truth facts and shared terms (use exactly — these override defaults):")
for term, referent in plan.shared_terms.items():
lines.append(f" {term} → {referent}")
if chunk.context_before:
lines.append(f"\nThe sections BEFORE yours cover: {chunk.context_before}")
if chunk.context_after:
lines.append(f"The sections AFTER yours cover: {chunk.context_after}")
lines.append(f"\nWrite these {len(chunk.sections)} section(s) in order:")
for i, s in enumerate(chunk.sections, 1):
lines.append(f"\n--- Section {i} ---")
lines.append(f"Heading: {s.heading}")
lines.append(f"Type: {s.type.value}")
lines.append(f"Depth: {s.depth.value}")
lines.append("Key points to cover:")
for point in s.key_points:
lines.append(f" - {point}")
return "\n".join(lines)
# ── Helpers ───────────────────────────────────────────────────────────────────────────────────────
def _build_jinja_env() -> Environment:
return Environment(
loader=FileSystemLoader(str(_TEMPLATES_DIR)),
autoescape=True,
trim_blocks=True,
lstrip_blocks=True,
)
def _safe_filename(title: str) -> str:
slug = re.sub(r"[^\w\s-]", "", title.lower())
slug = re.sub(r"[\s_-]+", "-", slug).strip("-")
return (slug[:60] or "document") + ".pdf"
# ── Agent ─────────────────────────────────────────────────────────────────────────────────────────
class PdfCreateAgent:
def __init__(self, runtime: AppRuntime) -> None:
self.runtime = runtime
self._jinja_env = _build_jinja_env()
self._meta_planner: Agent[None, DocumentMeta] = Agent(
model=runtime.smart_model,
output_type=NativeOutput(DocumentMeta),
system_prompt=_META_PLANNER_SYSTEM_PROMPT,
model_settings={**runtime.smart_model_settings, "temperature": 0.1},
)
self._sections_planner: Agent[None, DocumentSections] = Agent(
model=runtime.smart_model,
output_type=NativeOutput(DocumentSections),
system_prompt=_SECTIONS_PLANNER_SYSTEM_PROMPT,
model_settings={**runtime.smart_model_settings, "temperature": 0.1},
)
self._writer: Agent[None, WrittenSections] = Agent(
model=runtime.smart_model,
output_type=NativeOutput(WrittenSections),
system_prompt=_WRITER_SYSTEM_PROMPT,
model_settings={**runtime.smart_model_settings, "temperature": 0.3},
)
async def orchestrate(self, request: OrchestratorRequest) -> PdfCreateOrchestrateResponse:
history = format_conversation_history(request.conversation_history)
# ── Phase 1: plan meta ─────────────────────────────────────────────────
logger.info("[pdf-create] phase 1/6: planning document meta")
meta_prompt = f"Conversation history:\n{history}\n\nUser request: {request.user_message}"
meta_result = await self._meta_planner.run(meta_prompt)
meta = meta_result.output
if meta.cannot_do_reason:
logger.info("[pdf-create] cannot_do: %s", meta.cannot_do_reason)
return EditCannotDoResponse(reason=meta.cannot_do_reason)
logger.info("[pdf-create] meta: title=%r tone=%r", meta.title, meta.tone_brief)
# ── Phase 2: plan sections ─────────────────────────────────────────────
logger.info("[pdf-create] phase 2/6: planning sections")
sections_prompt = _build_sections_prompt(meta, request.user_message, history)
sections_result = await self._sections_planner.run(sections_prompt)
planned_sections = sections_result.output
if not planned_sections.sections:
logger.info("[pdf-create] sections planner returned empty sections")
return EditCannotDoResponse(reason="No document sections could be planned from the request.")
plan = DocumentPlan.assemble(meta, planned_sections)
# ── Phase 3: chunk ─────────────────────────────────────────────────────
chunks = _make_chunks(plan.sections)
logger.info(
"[pdf-create] phase 3/6: chunked — sections=%d chunks=%d",
len(plan.sections),
len(chunks),
)
# ── Phase 4: write in parallel, bounded ────────────────────────────────
logger.info("[pdf-create] phase 4/6: writing %d chunk(s) in parallel", len(chunks))
total_chunks = len(chunks)
semaphore = asyncio.Semaphore(_MAX_PARALLEL_WRITERS)
written_chunks: list[WrittenSections] = await asyncio.gather(
*[self._write_chunk(plan, chunk, total_chunks, semaphore) for chunk in chunks]
)
# ── Phase 5: assemble in plan order (gather preserves insertion order) ──
all_sections: list[DocumentSection] = []
for written in written_chunks:
all_sections.extend(written.sections)
logger.info("[pdf-create] phase 5/6: assembled %d sections", len(all_sections))
doc = GeneratedDocument(
title=plan.title,
subtitle=plan.subtitle,
reference_number=plan.reference_number,
style=plan.style,
sections=all_sections,
)
# ── Phase 6: render ────────────────────────────────────────────────────
logger.info("[pdf-create] phase 6/6: rendering HTML")
html = self._render(doc)
filename = _safe_filename(plan.title)
logger.info(
"[pdf-create] done — filename=%r html_bytes=%d",
filename,
len(html),
)
return EditPlanResponse(
summary=f"Created {plan.title}",
steps=[
ToolOperationStep(
tool=AgentToolId.CREATE_PDF_FROM_HTML_AGENT,
parameters=CreatePdfFromHtmlAgentParams(
html_content=html,
filename=filename,
),
)
],
)
async def _write_chunk(
self, plan: DocumentPlan, chunk: _Chunk, total_chunks: int, semaphore: asyncio.Semaphore
) -> WrittenSections:
async with semaphore:
prompt = _build_writer_prompt(plan, chunk)
result = await self._writer.run(prompt)
logger.info(
"[pdf-create] chunk %d/%d wrote %d sections",
chunk.index + 1,
total_chunks,
len(result.output.sections),
)
return result.output
def _render(self, doc: GeneratedDocument) -> str:
template = self._jinja_env.get_template("document.html.jinja2")
return template.render(doc=doc)
@@ -0,0 +1,301 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
:root {
--color-bg: #ffffff;
--color-primary: #1e3a5f;
--color-subtitle: #475569;
--color-ref: #6b7280;
--color-label: #374151;
--color-body: #1a1a1a;
--color-border-light: #e2e8f0;
--color-border-heading: #cbd5e1;
--font-body: "Helvetica Neue", Arial, sans-serif;
--font-size-base: 10pt;
}
@page {
size: A4;
margin: 20mm;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
font-family: var(--font-body);
font-size: var(--font-size-base);
line-height: 1.5;
color: var(--color-body);
background: var(--color-bg);
}
/* ── Header ── */
.doc-header {
margin-bottom: 20pt;
padding-bottom: 10pt;
border-bottom: 2pt solid var(--color-primary);
}
.doc-title {
font-size: 20pt;
font-weight: 700;
color: var(--color-primary);
line-height: 1.2;
}
.doc-subtitle {
font-size: 11pt;
color: var(--color-subtitle);
margin-top: 3pt;
}
.doc-reference {
font-size: 9pt;
color: var(--color-ref);
margin-top: 4pt;
}
/* ── Sections ── */
section {
margin-bottom: 16pt;
page-break-inside: avoid;
break-inside: avoid;
}
section.line-items-section {
page-break-inside: auto;
break-inside: auto;
}
section h2 {
font-size: 11pt;
font-weight: 700;
color: var(--color-primary);
border-bottom: 0.5pt solid var(--color-border-heading);
padding-bottom: 3pt;
margin-bottom: 8pt;
}
/* ── Text ── */
.text-body p {
margin-bottom: 6pt;
}
.text-body p:last-child {
margin-bottom: 0;
}
/* ── Key-value ── */
.kv-table {
width: 100%;
border-collapse: collapse;
}
.kv-table td {
padding: 3pt 0;
vertical-align: top;
}
.kv-table td.kv-label {
font-weight: 600;
color: var(--color-label);
width: 36%;
padding-right: 10pt;
}
.kv-table td.kv-value {
color: var(--color-body);
}
/* ── Line items ── */
.line-items-table {
width: 100%;
border-collapse: collapse;
font-size: 9.5pt;
}
.line-items-table thead tr {
background-color: var(--color-primary);
color: #ffffff;
}
.line-items-table thead th {
padding: 5pt 8pt;
text-align: left;
font-weight: 600;
}
.line-items-table thead th:not(:first-child) {
text-align: right;
}
.line-items-table tbody td {
padding: 4pt 8pt;
border-bottom: 0.5pt solid var(--color-border-light);
vertical-align: top;
}
.line-items-table tbody td:not(:first-child) {
text-align: right;
}
.line-items-table tbody tr:last-child td {
border-bottom: none;
}
.line-items-table tbody tr {
page-break-inside: avoid;
break-inside: avoid;
}
.line-items-table tr.total-row td {
padding: 5pt 8pt;
font-weight: 700;
border-top: 1pt solid var(--color-primary);
}
.line-items-table tr.total-row td:not(:first-child) {
text-align: right;
}
/* ── Bullet list ── */
.bullet-list {
padding-left: 14pt;
}
.bullet-list li {
margin-bottom: 3pt;
}
.bullet-list li:last-child {
margin-bottom: 0;
}
/* ── Signature ── */
.signature-grid {
width: 100%;
margin-top: 8pt;
}
.signatory {
display: inline-block;
width: 44%;
margin-right: 5%;
margin-bottom: 8pt;
vertical-align: top;
}
.sig-line {
border-bottom: 1pt solid var(--color-label);
height: 28pt;
margin-bottom: 4pt;
}
.sig-name {
font-size: 9pt;
color: var(--color-label);
}
</style>
{%- if doc.style %}
<style>
:root {
{%- if doc.style.primary_color %}
--color-primary: {{ doc.style.primary_color }};
{%- endif %}
{%- if doc.style.background_color %}
--color-bg: {{ doc.style.background_color }};
{%- endif %}
{%- if doc.style.body_text_color %}
--color-body: {{ doc.style.body_text_color }};
--color-label: {{ doc.style.body_text_color }};
{%- endif %}
}
</style>
{%- endif %}
</head>
<body>
<div class="doc-header">
<div class="doc-title">{{ doc.title }}</div>
{%- if doc.subtitle %}
<div class="doc-subtitle">{{ doc.subtitle }}</div>
{%- endif %}
{%- if doc.reference_number %}
<div class="doc-reference">{{ doc.reference_number }}</div>
{%- endif %}
</div>
{%- for section in doc.sections %}
{%- if section.type == "text" %}
<section>
{%- if section.heading %}
<h2>{{ section.heading }}</h2>
{%- endif %}
<div class="text-body">
{%- for para in section.body.split('\n\n') %}
<p>{{ para | replace('\n', ' ') }}</p>
{%- endfor %}
</div>
</section>
{%- elif section.type == "key_value" %}
<section>
{%- if section.heading %}
<h2>{{ section.heading }}</h2>
{%- endif %}
<table class="kv-table">
<tbody>
{%- for label, value in section.pairs %}
<tr>
<td class="kv-label">{{ label }}</td>
<td class="kv-value">{{ value }}</td>
</tr>
{%- endfor %}
</tbody>
</table>
</section>
{%- elif section.type == "line_items" %}
<section class="line-items-section">
{%- if section.heading %}
<h2>{{ section.heading }}</h2>
{%- endif %}
<table class="line-items-table">
<thead>
<tr>
{%- for col in section.columns %}
<th>{{ col }}</th>
{%- endfor %}
</tr>
</thead>
<tbody>
{%- for row in section.rows %}
<tr>
{%- for cell in row %}
<td>{{ cell }}</td>
{%- endfor %}
</tr>
{%- endfor %}
{%- if section.total_row %}
<tr class="total-row">
{%- for cell in section.total_row %}
<td>{{ cell }}</td>
{%- endfor %}
</tr>
{%- endif %}
</tbody>
</table>
</section>
{%- elif section.type == "bullet_list" %}
<section>
{%- if section.heading %}
<h2>{{ section.heading }}</h2>
{%- endif %}
<ul class="bullet-list">
{%- for item in section.items %}
<li>{{ item }}</li>
{%- endfor %}
</ul>
</section>
{%- elif section.type == "signature" %}
<section>
{%- if section.heading %}
<h2>{{ section.heading }}</h2>
{%- endif %}
<div class="signature-grid">
{%- for signatory in section.signatories %}
<div class="signatory">
<div class="sig-line"></div>
<div class="sig-name">{{ signatory }}</div>
</div>
{%- endfor %}
</div>
</section>
{%- endif %}
{%- endfor %}
</body>
</html>
+16
View File
@@ -80,6 +80,15 @@ from .pdf_comments import (
PdfCommentResponse,
TextChunk,
)
from .pdf_create import (
DocumentMeta,
DocumentSections,
PdfCreateCannotDoResponse,
PdfCreateOrchestrateResponse,
PdfCreateRequest,
PdfCreateResponse,
PdfCreateSuccessResponse,
)
from .pdf_edit import (
EditCannotDoResponse,
EditClarificationRequest,
@@ -130,6 +139,8 @@ __all__ = [
"DeleteDocumentResponse",
"PurgeOwnerResponse",
"Discrepancy",
"DocumentMeta",
"DocumentSections",
"DiscrepancyKind",
"EditCannotDoResponse",
"EditClarificationRequest",
@@ -164,6 +175,11 @@ __all__ = [
"PdfCommentRequest",
"PdfCommentResponse",
"PdfContentType",
"PdfCreateCannotDoResponse",
"PdfCreateOrchestrateResponse",
"PdfCreateRequest",
"PdfCreateResponse",
"PdfCreateSuccessResponse",
"PdfEditRequest",
"PdfEditResponse",
"PdfEditTerminalResponse",
+1 -1
View File
@@ -88,11 +88,11 @@ class SupportedCapability(StrEnum):
PDF_EDIT = "pdf_edit"
PDF_QUESTION = "pdf_question"
PDF_REVIEW = "pdf_review"
PDF_CREATE = "pdf_create"
AGENT_DRAFT = "agent_draft"
AGENT_REVISE = "agent_revise"
AGENT_NEXT_ACTION = "agent_next_action"
MATH_AUDITOR_AGENT = "math_auditor_agent"
PDF_TO_MARKDOWN = "pdf_to_markdown"
class ConversationMessage(ApiModel):
+226
View File
@@ -0,0 +1,226 @@
"""Contracts for the PDF Create Agent.
The agent accepts a natural-language prompt and returns a single
CREATE_PDF_FROM_HTML_AGENT plan step carrying the rendered HTML.
Pipeline:
1. PlannerAgent (smart_model) → DocumentPlan: structured skeleton, no body text.
2. Python chunks the plan by token budget.
3. SectionWriterAgents (smart_model, parallel) → WrittenSections per chunk.
4. Assembler collects sections in plan order → GeneratedDocument.
5. Jinja renders GeneratedDocument → HTML. The LLM never writes HTML.
"""
from __future__ import annotations
import re
from enum import StrEnum
from typing import Annotated, Literal
from pydantic import Field, field_validator
from stirling.models import ApiModel
from .common import ConversationMessage
from .pdf_edit import EditCannotDoResponse, EditPlanResponse
class SectionType(StrEnum):
TEXT = "text"
KEY_VALUE = "key_value"
LINE_ITEMS = "line_items"
BULLET_LIST = "bullet_list"
SIGNATURE = "signature"
class TextSection(ApiModel):
"""One or more prose paragraphs. Use for introductions, summaries, and narrative content."""
type: Literal[SectionType.TEXT] = SectionType.TEXT
heading: str | None = None
body: str = Field(description="Paragraph text. Use \\n\\n to separate paragraphs.")
class KeyValueSection(ApiModel):
"""Labelled fields. Use for contact info, dates, invoice details, and metadata."""
type: Literal[SectionType.KEY_VALUE] = SectionType.KEY_VALUE
heading: str | None = None
pairs: list[tuple[str, str]] = Field(description="List of (label, value) pairs.")
class LineItemsSection(ApiModel):
"""A table with column headers and data rows. Use for invoices, expenses, schedules."""
type: Literal[SectionType.LINE_ITEMS] = SectionType.LINE_ITEMS
heading: str | None = None
columns: list[str] = Field(description="Column header names.")
rows: list[list[str]] = Field(description="Data rows; each row must match columns in length.")
total_row: list[str] | None = None
class BulletListSection(ApiModel):
"""An unordered list. Use for requirements, responsibilities, or any enumerated items."""
type: Literal[SectionType.BULLET_LIST] = SectionType.BULLET_LIST
heading: str | None = None
items: list[str]
class SignatureSection(ApiModel):
"""Signature blocks. Use when the document requires sign-off from named parties."""
type: Literal[SectionType.SIGNATURE] = SectionType.SIGNATURE
heading: str | None = None
signatories: list[str] = Field(description="Names or roles to sign, e.g. 'John Smith, CEO'.")
type DocumentSection = Annotated[
TextSection | KeyValueSection | LineItemsSection | BulletListSection | SignatureSection,
Field(discriminator="type"),
]
# Named colour or hex only — anything else is dropped so a colour can't inject CSS into the
# <style> block (which would let WeasyPrint fetch an attacker-controlled url() → SSRF).
_SAFE_COLOR_RE = re.compile(r"^#[0-9a-fA-F]{3,8}$|^[a-zA-Z]{1,30}$")
class DocumentStyle(ApiModel):
"""Document colours, inferred by the meta planner and rendered into the engine's Jinja
template (never sent to Java). Unsafe colours are dropped to ``None``."""
primary_color: str | None = Field(default=None)
background_color: str | None = Field(default=None)
body_text_color: str | None = Field(default=None)
@field_validator("primary_color", "background_color", "body_text_color", mode="after")
@classmethod
def _drop_unsafe_color(cls, value: str | None) -> str | None:
if value is None:
return None
return value if _SAFE_COLOR_RE.fullmatch(value) else None
class GeneratedDocument(ApiModel):
"""The full document model passed to Jinja for HTML rendering."""
title: str
subtitle: str | None = None
reference_number: str | None = None
style: DocumentStyle | None = None
sections: list[DocumentSection]
# ── Planner models ────────────────────────────────────────────────────────────────────────────────
class SectionDepth(StrEnum):
BRIEF = "brief"
STANDARD = "standard"
DETAILED = "detailed"
class PlannedSection(ApiModel):
"""One section in the document plan. Contains structure and intent — no body text."""
heading: str
type: SectionType
depth: SectionDepth
key_points: list[str]
class DocumentMeta(ApiModel):
"""Document header fields produced by the first planner call.
Contains everything except the section list. Kept deliberately flat so the
JSON schema stays small enough for grammar compilation on all model tiers.
Style is expressed as three flat optional strings rather than a nested object
for the same reason; assemble() reconstructs DocumentStyle from them.
"""
cannot_do_reason: str | None = None
title: str = ""
subtitle: str | None = None
reference_number: str | None = None
tone_brief: str = ""
shared_terms: dict[str, str] = Field(default_factory=dict)
document_context: str = ""
style_primary_color: str | None = None
style_background_color: str | None = None
style_body_text_color: str | None = None
class DocumentSections(ApiModel):
"""Section list produced by the second planner call."""
sections: list[PlannedSection] = Field(default_factory=list)
class DocumentPlan(ApiModel):
"""Assembled plan: meta + sections. Not used as a direct LLM output schema."""
cannot_do_reason: str | None = None
title: str = ""
subtitle: str | None = None
reference_number: str | None = None
tone_brief: str = ""
shared_terms: dict[str, str] = Field(default_factory=dict)
document_context: str = ""
style: DocumentStyle | None = None
sections: list[PlannedSection] = Field(default_factory=list)
@classmethod
def assemble(cls, meta: DocumentMeta, sections: DocumentSections) -> DocumentPlan:
style_fields = {
"primary_color": meta.style_primary_color,
"background_color": meta.style_background_color,
"body_text_color": meta.style_body_text_color,
}
inferred_style = DocumentStyle(**style_fields) if any(style_fields.values()) else None
return cls(
cannot_do_reason=meta.cannot_do_reason,
title=meta.title,
subtitle=meta.subtitle,
reference_number=meta.reference_number,
tone_brief=meta.tone_brief,
shared_terms=meta.shared_terms,
document_context=meta.document_context,
style=inferred_style,
sections=sections.sections,
)
class WrittenSections(ApiModel):
"""Sections produced by one section-writer agent for one chunk."""
sections: list[DocumentSection]
# ── Request/response contracts ───────────────────────────────────────────────────────────────────
class PdfCreateRequest(ApiModel):
user_message: str
conversation_history: list[ConversationMessage] = Field(default_factory=list)
class PdfCreateSuccessResponse(ApiModel):
outcome: Literal["document_created"] = "document_created"
document: GeneratedDocument
class PdfCreateCannotDoResponse(ApiModel):
outcome: Literal["cannot_do"] = "cannot_do"
reason: str
type PdfCreateResponse = Annotated[
PdfCreateSuccessResponse | PdfCreateCannotDoResponse,
Field(discriminator="outcome"),
]
type PdfCreateOrchestrateResponse = Annotated[
EditPlanResponse | EditCannotDoResponse,
Field(discriminator="outcome"),
]
@@ -1,13 +1,15 @@
"""Agent tool IDs, parameter models, and registry.
tool_models.py is auto-generated from the Java OpenAPI spec. This file is its
manually-maintained counterpart for tools backed by AI agent pipelines.
Hand-maintained counterpart to the generated tool_models.py, for engine-emitted tools
hidden from the OpenAPI spec: AI-backed agents and deterministic conversions like HTML-to-PDF.
"""
from __future__ import annotations
from enum import StrEnum
from pydantic import Field
from stirling.models.base import ApiModel
from stirling.models.tool_models import ParamToolModel, ToolEndpoint
@@ -15,6 +17,7 @@ from stirling.models.tool_models import ParamToolModel, ToolEndpoint
class AgentToolId(StrEnum):
MATH_AUDITOR_AGENT = "/api/v1/ai/tools/math-auditor-agent"
PDF_COMMENT_AGENT = "/api/v1/ai/tools/pdf-comment-agent"
CREATE_PDF_FROM_HTML_AGENT = "/api/v1/ai/tools/create-pdf-from-html-agent"
class MathAuditorAgentParams(ApiModel):
@@ -25,7 +28,12 @@ class PdfCommentAgentParams(ApiModel):
prompt: str | None = None
type AgentParamModel = MathAuditorAgentParams | PdfCommentAgentParams
class CreatePdfFromHtmlAgentParams(ApiModel):
html_content: str
filename: str = Field(pattern=r"^.+\.pdf$")
type AgentParamModel = MathAuditorAgentParams | PdfCommentAgentParams | CreatePdfFromHtmlAgentParams
type AnyToolId = ToolEndpoint | AgentToolId
type AnyParamModel = ParamToolModel | AgentParamModel
@@ -33,4 +41,5 @@ type AnyParamModel = ParamToolModel | AgentParamModel
AGENT_OPERATIONS: dict[AgentToolId, type[AgentParamModel]] = {
AgentToolId.MATH_AUDITOR_AGENT: MathAuditorAgentParams,
AgentToolId.PDF_COMMENT_AGENT: PdfCommentAgentParams,
AgentToolId.CREATE_PDF_FROM_HTML_AGENT: CreatePdfFromHtmlAgentParams,
}
+4 -1
View File
@@ -37,7 +37,10 @@ def _build_anthropic_http_client() -> httpx.AsyncClient:
have died in the pool. See ``STIRLING_HTTP_DEBUG`` traces of slice 6
on 2026-05-06 for the concrete failure mode this addresses.
"""
return httpx.AsyncClient(limits=httpx.Limits(max_keepalive_connections=0))
return httpx.AsyncClient(
limits=httpx.Limits(max_keepalive_connections=0),
timeout=httpx.Timeout(connect=30.0, read=300.0, write=30.0, pool=5.0),
)
@dataclass(frozen=True)
+564
View File
@@ -0,0 +1,564 @@
"""Tests for the chunked PdfCreateAgent pipeline.
Coverage:
1. Section model validation (each section type round-trips correctly)
2. Jinja rendering (_render produces valid HTML for each section type)
3. _safe_filename produces clean slugs
4. _make_chunks groups sections correctly by token budget
5. orchestrate() produces the correct EditPlanResponse via planner + writer mocks
6. orchestrate() returns EditCannotDoResponse when meta planner signals cannot_do
7. orchestrate() returns EditCannotDoResponse when sections planner returns empty list
"""
from __future__ import annotations
import pytest
from conftest import build_app_settings
from pydantic_ai.models.test import TestModel
from pydantic_ai.profiles import ModelProfile
from stirling.agents.pdf_create.agent import (
PdfCreateAgent,
_make_chunks,
_safe_filename,
)
from stirling.contracts import (
EditCannotDoResponse,
EditPlanResponse,
OrchestratorRequest,
)
from stirling.contracts.pdf_create import (
BulletListSection,
DocumentMeta,
DocumentSections,
DocumentStyle,
GeneratedDocument,
KeyValueSection,
LineItemsSection,
PlannedSection,
SectionDepth,
SectionType,
SignatureSection,
TextSection,
WrittenSections,
)
from stirling.models.agent_tool_models import AgentToolId, CreatePdfFromHtmlAgentParams
from stirling.services import build_runtime
from stirling.services.runtime import AppRuntime
_NATIVE_PROFILE = ModelProfile(supports_json_schema_output=True)
# ── Fixtures ──────────────────────────────────────────────────────────────────────────────────────
@pytest.fixture
def runtime() -> AppRuntime:
return build_runtime(build_app_settings())
@pytest.fixture
def agent(runtime: AppRuntime) -> PdfCreateAgent:
return PdfCreateAgent(runtime)
# ── Helpers ───────────────────────────────────────────────────────────────────────────────────────
def _invoice_doc() -> GeneratedDocument:
return GeneratedDocument(
title="Invoice",
subtitle="Acme Corp",
reference_number="Invoice #INV-001",
sections=[
KeyValueSection(
heading="Details",
pairs=[("Date", "2026-05-06"), ("Due", "2026-06-06"), ("Currency", "USD")],
),
LineItemsSection(
heading="Line Items",
columns=["Description", "Qty", "Unit Price", "Total"],
rows=[
["Consulting services", "10", "$500.00", "$5,000.00"],
["Expenses", "1", "$200.00", "$200.00"],
],
total_row=["Total", "", "", "$5,200.00"],
),
TextSection(
heading="Payment Terms",
body="Payment is due within 30 days.\n\nPlease reference the invoice number.",
),
SignatureSection(
heading="Authorised By",
signatories=["Jane Smith, CEO", "Bob Jones, CFO"],
),
],
)
def _simple_meta() -> DocumentMeta:
return DocumentMeta(
title="Invoice",
subtitle="Acme Corp",
tone_brief="Professional business tone.",
shared_terms={"the Client": "Acme Corp"},
)
def _simple_sections() -> DocumentSections:
return DocumentSections(
sections=[
PlannedSection(
heading="Details",
type=SectionType.KEY_VALUE,
depth=SectionDepth.BRIEF,
key_points=["Date: 2026-05-06", "Due: 2026-06-06"],
),
PlannedSection(
heading="Line Items",
type=SectionType.LINE_ITEMS,
depth=SectionDepth.STANDARD,
key_points=["Consulting services, 10h, $500/h", "Expenses, $200"],
),
]
)
def _written_sections() -> WrittenSections:
return WrittenSections(
sections=[
KeyValueSection(
heading="Details",
pairs=[("Date", "2026-05-06"), ("Due", "2026-06-06")],
),
LineItemsSection(
heading="Line Items",
columns=["Description", "Qty", "Unit Price", "Total"],
rows=[["Consulting services", "10", "$500.00", "$5,000.00"]],
total_row=["Total", "", "", "$5,000.00"],
),
]
)
def _orchestrator_request(message: str = "Create an invoice for Acme Corp") -> OrchestratorRequest:
return OrchestratorRequest(
user_message=message,
files=[],
conversation_history=[],
artifacts=[],
enabled_endpoints=[],
)
# ── Section model validation ──────────────────────────────────────────────────────────────────────
def test_text_section_round_trips() -> None:
s = TextSection(heading="Summary", body="Hello\n\nWorld")
assert s.type == "text"
assert s.heading == "Summary"
assert "World" in s.body
def test_key_value_section_round_trips() -> None:
s = KeyValueSection(pairs=[("Name", "Alice"), ("Role", "Engineer")])
assert s.type == "key_value"
assert s.pairs[0] == ("Name", "Alice")
def test_line_items_section_with_total_row() -> None:
s = LineItemsSection(
columns=["Item", "Amount"],
rows=[["Widget", "$10"]],
total_row=["Total", "$10"],
)
assert s.total_row is not None
assert s.total_row[1] == "$10"
def test_line_items_section_optional_total_row() -> None:
s = LineItemsSection(columns=["Item", "Amount"], rows=[["Widget", "$10"]])
assert s.total_row is None
def test_bullet_list_section_round_trips() -> None:
s = BulletListSection(items=["Alpha", "Beta", "Gamma"])
assert s.type == "bullet_list"
assert len(s.items) == 3
def test_signature_section_round_trips() -> None:
s = SignatureSection(signatories=["Alice", "Bob"])
assert s.type == "signature"
assert "Alice" in s.signatories
def test_generated_document_optional_fields() -> None:
doc = GeneratedDocument(title="Simple Doc", sections=[TextSection(body="Hello")])
assert doc.subtitle is None
assert doc.reference_number is None
# ── Jinja rendering ───────────────────────────────────────────────────────────────────────────────
def test_render_produces_html(agent: PdfCreateAgent) -> None:
doc = _invoice_doc()
html = agent._render(doc)
assert "<!DOCTYPE html>" in html
assert "Invoice" in html
assert "INV-001" in html
def test_render_includes_all_section_types(agent: PdfCreateAgent) -> None:
doc = GeneratedDocument(
title="All Sections",
sections=[
TextSection(body="Some prose text."),
KeyValueSection(pairs=[("Key", "Value")]),
LineItemsSection(columns=["A", "B"], rows=[["1", "2"]]),
BulletListSection(items=["item one"]),
SignatureSection(signatories=["Alice"]),
],
)
html = agent._render(doc)
assert "Some prose text." in html
assert "Key" in html and "Value" in html
assert "<th>" in html
assert "item one" in html
assert "Alice" in html
def test_render_escapes_html_in_content(agent: PdfCreateAgent) -> None:
doc = GeneratedDocument(
title="XSS Test",
sections=[TextSection(body="<script>alert('xss')</script>")],
)
html = agent._render(doc)
assert "<script>" not in html
assert "&lt;script&gt;" in html
def test_render_total_row_present(agent: PdfCreateAgent) -> None:
doc = GeneratedDocument(
title="Table",
sections=[
LineItemsSection(
columns=["Item", "Total"],
rows=[["Widget", "$10"]],
total_row=["Total", "$10"],
)
],
)
html = agent._render(doc)
assert "total-row" in html
def test_render_no_total_row_skips_tfoot(agent: PdfCreateAgent) -> None:
doc = GeneratedDocument(
title="Table",
sections=[LineItemsSection(columns=["Item"], rows=[["Widget"]])],
)
html = agent._render(doc)
assert "<tfoot>" not in html
def test_render_subtitle_and_reference(agent: PdfCreateAgent) -> None:
doc = GeneratedDocument(
title="My Doc",
subtitle="Subtitle Here",
reference_number="REF-42",
sections=[TextSection(body="Content.")],
)
html = agent._render(doc)
assert "Subtitle Here" in html
assert "REF-42" in html
# ── _safe_filename ────────────────────────────────────────────────────────────────────────────────
def test_safe_filename_basic() -> None:
assert _safe_filename("My Invoice") == "my-invoice.pdf"
def test_safe_filename_strips_special_chars() -> None:
assert _safe_filename("Report: Q1/2026!") == "report-q12026.pdf"
def test_safe_filename_empty_title() -> None:
assert _safe_filename("!!!") == "document.pdf"
# ── _make_chunks ──────────────────────────────────────────────────────────────────────────────────
def _planned(heading: str, depth: SectionDepth) -> PlannedSection:
return PlannedSection(
heading=heading,
type=SectionType.TEXT,
depth=depth,
key_points=["placeholder"],
)
def test_make_chunks_empty_returns_empty() -> None:
assert _make_chunks([]) == []
def test_make_chunks_single_section_is_one_chunk() -> None:
chunks = _make_chunks([_planned("Intro", SectionDepth.STANDARD)])
assert len(chunks) == 1
assert chunks[0].index == 0
assert len(chunks[0].sections) == 1
assert chunks[0].context_before is None
assert chunks[0].context_after is None
def test_make_chunks_groups_under_ceiling() -> None:
# 5 × STANDARD (550 each) = 2750 — fits in one chunk under ceiling of 3000
sections = [_planned(f"Section {i}", SectionDepth.STANDARD) for i in range(5)]
chunks = _make_chunks(sections)
assert len(chunks) == 1
assert len(chunks[0].sections) == 5
def test_make_chunks_splits_when_over_ceiling() -> None:
# 6 × STANDARD (550 each) = 3300 — must split (3000 ceiling)
# First chunk: 5 sections (2750), second: 1 section (550)
sections = [_planned(f"Section {i}", SectionDepth.STANDARD) for i in range(6)]
chunks = _make_chunks(sections)
assert len(chunks) == 2
assert len(chunks[0].sections) == 5
assert len(chunks[1].sections) == 1
def test_make_chunks_oversized_section_gets_own_chunk() -> None:
# DETAILED (1200) + 3×STANDARD (1650) = 2850; adding a 4th STANDARD (550) = 3400 > 3000.
# So first chunk holds DETAILED + 3 STANDARDs; last STANDARD spills to chunk 2.
sections = [
_planned("Big Table", SectionDepth.DETAILED),
_planned("Terms", SectionDepth.STANDARD),
_planned("Notes", SectionDepth.STANDARD),
_planned("Extra", SectionDepth.STANDARD),
_planned("Appendix", SectionDepth.STANDARD),
]
chunks = _make_chunks(sections)
assert len(chunks) == 2
assert chunks[0].sections[0].heading == "Big Table"
assert len(chunks[0].sections) == 4
assert chunks[1].sections[0].heading == "Appendix"
def test_make_chunks_neighbour_context() -> None:
# 6 × STANDARD (550 each) = 3300 → splits into chunk of 5 (2750) + chunk of 1 (550)
many = [_planned(f"S{i}", SectionDepth.STANDARD) for i in range(5)]
many.append(_planned("Last", SectionDepth.STANDARD))
chunks = _make_chunks(many)
assert len(chunks) == 2
assert chunks[0].context_after is not None
assert chunks[1].context_before is not None
assert chunks[0].context_before is None
assert chunks[1].context_after is None
def test_make_chunks_preserves_section_order() -> None:
headings = [f"Section {i}" for i in range(8)]
sections = [_planned(h, SectionDepth.STANDARD) for h in headings]
chunks = _make_chunks(sections)
reassembled = [s.heading for chunk in chunks for s in chunk.sections]
assert reassembled == headings
# ── orchestrate() ─────────────────────────────────────────────────────────────────────────────────
@pytest.mark.anyio
async def test_orchestrate_returns_plan_step(agent: PdfCreateAgent) -> None:
meta = _simple_meta()
sections = _simple_sections()
written = _written_sections()
with (
agent._meta_planner.override(
model=TestModel(profile=_NATIVE_PROFILE, custom_output_text=meta.model_dump_json())
),
agent._sections_planner.override(
model=TestModel(profile=_NATIVE_PROFILE, custom_output_text=sections.model_dump_json())
),
agent._writer.override(model=TestModel(profile=_NATIVE_PROFILE, custom_output_text=written.model_dump_json())),
):
result = await agent.orchestrate(_orchestrator_request())
assert isinstance(result, EditPlanResponse)
assert len(result.steps) == 1
step = result.steps[0]
assert step.tool == AgentToolId.CREATE_PDF_FROM_HTML_AGENT
assert isinstance(step.parameters, CreatePdfFromHtmlAgentParams)
assert step.parameters.filename.endswith(".pdf")
assert "<!DOCTYPE html>" in step.parameters.html_content
assert "Invoice" in step.parameters.html_content
@pytest.mark.anyio
async def test_orchestrate_cannot_do_from_planner(agent: PdfCreateAgent) -> None:
cannot_do_meta = DocumentMeta(cannot_do_reason="This is not a document creation request.")
with agent._meta_planner.override(
model=TestModel(profile=_NATIVE_PROFILE, custom_output_text=cannot_do_meta.model_dump_json())
):
result = await agent.orchestrate(_orchestrator_request("what is 2+2?"))
assert isinstance(result, EditCannotDoResponse)
assert "not a document" in result.reason
@pytest.mark.anyio
async def test_orchestrate_empty_sections_returns_cannot_do(agent: PdfCreateAgent) -> None:
meta = DocumentMeta(title="Empty", tone_brief=".")
empty_sections = DocumentSections(sections=[])
with (
agent._meta_planner.override(
model=TestModel(profile=_NATIVE_PROFILE, custom_output_text=meta.model_dump_json())
),
agent._sections_planner.override(
model=TestModel(profile=_NATIVE_PROFILE, custom_output_text=empty_sections.model_dump_json())
),
):
result = await agent.orchestrate(_orchestrator_request("do the thing"))
assert isinstance(result, EditCannotDoResponse)
@pytest.mark.anyio
async def test_orchestrate_assembles_multiple_chunks(agent: PdfCreateAgent) -> None:
"""Two chunks of written sections are assembled in order into one document."""
meta = DocumentMeta(title="Multi-Chunk Doc", tone_brief="Formal.")
sections = DocumentSections(
sections=[
PlannedSection(heading="Intro", type=SectionType.TEXT, depth=SectionDepth.BRIEF, key_points=["x"]),
PlannedSection(
heading="Details",
type=SectionType.KEY_VALUE,
depth=SectionDepth.BRIEF,
key_points=["y"],
),
]
)
# The writer override is shared across all parallel calls, so use a combined
# WrittenSections that contains all sections — both chunks will return the same
# payload and we verify the final HTML contains the expected content.
combined = WrittenSections(
sections=[
TextSection(heading="Intro", body="Introduction text."),
KeyValueSection(heading="Details", pairs=[("Key", "Value")]),
]
)
with (
agent._meta_planner.override(
model=TestModel(profile=_NATIVE_PROFILE, custom_output_text=meta.model_dump_json())
),
agent._sections_planner.override(
model=TestModel(profile=_NATIVE_PROFILE, custom_output_text=sections.model_dump_json())
),
agent._writer.override(model=TestModel(profile=_NATIVE_PROFILE, custom_output_text=combined.model_dump_json())),
):
result = await agent.orchestrate(_orchestrator_request("Create a multi-chunk doc"))
assert isinstance(result, EditPlanResponse)
html = result.steps[0].parameters.html_content # type: ignore[union-attr]
assert "Introduction text." in html
assert "Details" in html
# ── Style inference ───────────────────────────────────────────────────────────────────────────────
@pytest.mark.anyio
async def test_orchestrate_applies_planner_inferred_style(agent: PdfCreateAgent) -> None:
"""Style extracted by the meta planner is applied to the rendered HTML."""
meta = DocumentMeta(
title="Styled Doc",
tone_brief="Professional.",
style_primary_color="magenta",
)
sections = _simple_sections()
written = _written_sections()
with (
agent._meta_planner.override(
model=TestModel(profile=_NATIVE_PROFILE, custom_output_text=meta.model_dump_json())
),
agent._sections_planner.override(
model=TestModel(profile=_NATIVE_PROFILE, custom_output_text=sections.model_dump_json())
),
agent._writer.override(model=TestModel(profile=_NATIVE_PROFILE, custom_output_text=written.model_dump_json())),
):
result = await agent.orchestrate(_orchestrator_request("Make an invoice, magenta styling"))
assert isinstance(result, EditPlanResponse)
html = result.steps[0].parameters.html_content # type: ignore[union-attr]
assert "magenta" in html
def test_render_applies_style(agent: PdfCreateAgent) -> None:
"""DocumentStyle fields are injected as CSS custom properties in the rendered HTML."""
doc = GeneratedDocument(
title="Styled",
sections=[TextSection(body="Content.")],
style=DocumentStyle(primary_color="magenta", background_color="#111111"),
)
html = agent._render(doc)
assert "--color-primary: magenta" in html
assert "--color-bg: #111111" in html
def test_document_style_drops_unsafe_colors() -> None:
"""Unsafe colours (not named/hex) are dropped, closing the <style> url() injection."""
safe = DocumentStyle(primary_color="navy", background_color="#1e3a5f", body_text_color="#fff")
assert (safe.primary_color, safe.background_color, safe.body_text_color) == (
"navy",
"#1e3a5f",
"#fff",
)
unsafe = DocumentStyle(
primary_color="red; background: url(http://evil.test/steal)",
background_color="expression(alert(1))",
body_text_color="navy; }",
)
assert unsafe.primary_color is None
assert unsafe.background_color is None
assert unsafe.body_text_color is None
# A trailing newline must not slip a value through (fullmatch, not $-before-newline).
assert DocumentStyle(primary_color="navy\n").primary_color is None
@pytest.mark.anyio
async def test_orchestrate_drops_unsafe_planner_color(agent: PdfCreateAgent) -> None:
"""An unsafe colour inferred by the meta planner never reaches the rendered HTML."""
meta = DocumentMeta(
title="Doc",
tone_brief="Professional.",
style_primary_color="blue; background: url(http://evil.test/)",
)
sections = _simple_sections()
written = _written_sections()
with (
agent._meta_planner.override(
model=TestModel(profile=_NATIVE_PROFILE, custom_output_text=meta.model_dump_json())
),
agent._sections_planner.override(
model=TestModel(profile=_NATIVE_PROFILE, custom_output_text=sections.model_dump_json())
),
agent._writer.override(model=TestModel(profile=_NATIVE_PROFILE, custom_output_text=written.model_dump_json())),
):
result = await agent.orchestrate(_orchestrator_request("make it blue"))
assert isinstance(result, EditPlanResponse)
html = result.steps[0].parameters.html_content # type: ignore[union-attr]
assert "evil.test" not in html
assert "url(" not in html
+2
View File
@@ -604,6 +604,7 @@ version = "0.1.0"
source = { editable = "." }
dependencies = [
{ name = "fastapi" },
{ name = "jinja2" },
{ name = "opentelemetry-sdk" },
{ name = "pgvector" },
{ name = "posthog" },
@@ -630,6 +631,7 @@ dev = [
[package.metadata]
requires-dist = [
{ name = "fastapi", specifier = ">=0.116.0" },
{ name = "jinja2", specifier = ">=3.1.0" },
{ name = "opentelemetry-sdk", specifier = ">=1.39.0" },
{ name = "pgvector", specifier = ">=0.3.6" },
{ name = "posthog", specifier = ">=3.0.0" },
+1 -1
View File
@@ -1,5 +1,5 @@
<!doctype html>
<html lang="en-GB">
<html lang="en-US">
<head>
<meta charset="UTF-8" />
<base href="%BASE_URL%" />
File diff suppressed because it is too large Load Diff

Some files were not shown because too many files have changed in this diff Show More