mirror of
https://github.com/arsvendg/Stirling-PDF.git
synced 2026-07-16 19:33:11 +02:00
Add document context for edit agent (#6152)
# Description of Changes Adds the ability for the Edit agent to request the content of the document before it decides which parameters it needs. This makes it able to process requests like `Split the document after the page containing the "My Section" section`, allowing for document context-based requests for all[^1] tools. I had to make a few changes elsewhere to make this work, including: - Moving the requesting of content out of the Question Agent and into a common location - Added specific API docs for the Split param because the generic ones were not specific enough for the AI to be able to reliably perform the correct operation - Fixed an issue in the tool models generator which caused the Redact params to only be half-generated (causing Pydantic to crash when the AI tried to run Redact) - Added missing logging to a bunch of tools and hooked it up properly so it'll print to stderr - Made the limits for the max pages/chars to extract from PDFs configurable via env var [^1]: Many of the tools can't actually do anything useful with the context at this stage, but will just need the tool API to be extended with new features like page-specific operations to be automatically able to do smart operations without needing to change the Edit agent itself.
This commit is contained in:
+2
-2
@@ -21,7 +21,7 @@ import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import stirling.software.SPDF.config.swagger.MultiFileResponse;
|
||||
import stirling.software.SPDF.model.api.PDFWithPageNums;
|
||||
import stirling.software.SPDF.model.api.SplitPagesRequest;
|
||||
import stirling.software.common.annotations.AutoJobPostMapping;
|
||||
import stirling.software.common.annotations.api.GeneralApi;
|
||||
import stirling.software.common.service.CustomPDFDocumentFactory;
|
||||
@@ -48,7 +48,7 @@ public class SplitPDFController {
|
||||
+ " specified page numbers or ranges. Users can specify pages using"
|
||||
+ " individual numbers, ranges, or 'all' for every page. Input:PDF"
|
||||
+ " Output:PDF Type:SIMO")
|
||||
public ResponseEntity<StreamingResponseBody> splitPdf(@ModelAttribute PDFWithPageNums request)
|
||||
public ResponseEntity<StreamingResponseBody> splitPdf(@ModelAttribute SplitPagesRequest request)
|
||||
throws IOException {
|
||||
|
||||
MultipartFile file = request.getFileInput();
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package stirling.software.SPDF.model.api;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
|
||||
import io.swagger.v3.oas.annotations.Hidden;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import io.swagger.v3.oas.annotations.media.Schema.RequiredMode;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import stirling.software.common.model.api.PDFFile;
|
||||
import stirling.software.common.util.GeneralUtils;
|
||||
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public class SplitPagesRequest extends PDFFile {
|
||||
|
||||
@Schema(
|
||||
description =
|
||||
"Split points - page numbers after which the PDF will be cut. For example,"
|
||||
+ " `\"2\"` produces two documents (pages 1-2 and pages 3+); `\"2,5\"`"
|
||||
+ " produces three (pages 1-2, 3-5, 6+). Supports ranges (e.g."
|
||||
+ " `\"1,3,5-9\"` splits after pages 1, 3, 5, 6, 7, 8, 9, yielding 8"
|
||||
+ " documents), `\"all\"` (split after every page), or functions like"
|
||||
+ " `\"2n+1\"`, `\"3n\"`, `\"6n-5\"`.",
|
||||
defaultValue = "all",
|
||||
requiredMode = RequiredMode.REQUIRED)
|
||||
private String pageNumbers;
|
||||
|
||||
@Hidden
|
||||
public List<Integer> getPageNumbersList(PDDocument doc, boolean oneBased) {
|
||||
int pageCount = doc.getNumberOfPages();
|
||||
return GeneralUtils.parsePageList(pageNumbers, pageCount, oneBased);
|
||||
}
|
||||
}
|
||||
+9
-9
@@ -26,7 +26,7 @@ import org.springframework.http.MediaType;
|
||||
import org.springframework.mock.web.MockMultipartFile;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import stirling.software.SPDF.model.api.PDFWithPageNums;
|
||||
import stirling.software.SPDF.model.api.SplitPagesRequest;
|
||||
import stirling.software.common.service.CustomPDFDocumentFactory;
|
||||
import stirling.software.common.util.TempFileManager;
|
||||
|
||||
@@ -74,7 +74,7 @@ class SplitPDFControllerTest {
|
||||
new MockMultipartFile(
|
||||
"fileInput", "input.pdf", MediaType.APPLICATION_PDF_VALUE, pdfBytes);
|
||||
|
||||
PDFWithPageNums request = new PDFWithPageNums();
|
||||
SplitPagesRequest request = new SplitPagesRequest();
|
||||
request.setFileInput(file);
|
||||
request.setPageNumbers("3");
|
||||
|
||||
@@ -93,7 +93,7 @@ class SplitPDFControllerTest {
|
||||
new MockMultipartFile(
|
||||
"fileInput", "input.pdf", MediaType.APPLICATION_PDF_VALUE, pdfBytes);
|
||||
|
||||
PDFWithPageNums request = new PDFWithPageNums();
|
||||
SplitPagesRequest request = new SplitPagesRequest();
|
||||
request.setFileInput(file);
|
||||
request.setPageNumbers("1,2,3");
|
||||
|
||||
@@ -112,7 +112,7 @@ class SplitPDFControllerTest {
|
||||
new MockMultipartFile(
|
||||
"fileInput", "input.pdf", MediaType.APPLICATION_PDF_VALUE, pdfBytes);
|
||||
|
||||
PDFWithPageNums request = new PDFWithPageNums();
|
||||
SplitPagesRequest request = new SplitPagesRequest();
|
||||
request.setFileInput(file);
|
||||
request.setPageNumbers("1");
|
||||
|
||||
@@ -131,7 +131,7 @@ class SplitPDFControllerTest {
|
||||
new MockMultipartFile(
|
||||
"fileInput", "input.pdf", MediaType.APPLICATION_PDF_VALUE, pdfBytes);
|
||||
|
||||
PDFWithPageNums request = new PDFWithPageNums();
|
||||
SplitPagesRequest request = new SplitPagesRequest();
|
||||
request.setFileInput(file);
|
||||
request.setPageNumbers("3,7");
|
||||
|
||||
@@ -150,7 +150,7 @@ class SplitPDFControllerTest {
|
||||
new MockMultipartFile(
|
||||
"fileInput", "input.pdf", MediaType.APPLICATION_PDF_VALUE, pdfBytes);
|
||||
|
||||
PDFWithPageNums request = new PDFWithPageNums();
|
||||
SplitPagesRequest request = new SplitPagesRequest();
|
||||
request.setFileInput(file);
|
||||
request.setPageNumbers("2");
|
||||
|
||||
@@ -171,7 +171,7 @@ class SplitPDFControllerTest {
|
||||
new MockMultipartFile(
|
||||
"fileInput", "input.pdf", MediaType.APPLICATION_PDF_VALUE, pdfBytes);
|
||||
|
||||
PDFWithPageNums request = new PDFWithPageNums();
|
||||
SplitPagesRequest request = new SplitPagesRequest();
|
||||
request.setFileInput(file);
|
||||
request.setPageNumbers("5");
|
||||
|
||||
@@ -190,7 +190,7 @@ class SplitPDFControllerTest {
|
||||
new MockMultipartFile(
|
||||
"fileInput", "input.pdf", MediaType.APPLICATION_PDF_VALUE, pdfBytes);
|
||||
|
||||
PDFWithPageNums request = new PDFWithPageNums();
|
||||
SplitPagesRequest request = new SplitPagesRequest();
|
||||
request.setFileInput(file);
|
||||
request.setPageNumbers("all");
|
||||
|
||||
@@ -209,7 +209,7 @@ class SplitPDFControllerTest {
|
||||
new MockMultipartFile(
|
||||
"fileInput", "no_extension", MediaType.APPLICATION_PDF_VALUE, pdfBytes);
|
||||
|
||||
PDFWithPageNums request = new PDFWithPageNums();
|
||||
SplitPagesRequest request = new SplitPagesRequest();
|
||||
request.setFileInput(file);
|
||||
request.setPageNumbers("1");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user