Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: keep the answers typed into a PDF form when it is attached to a chat (#12377)
Nilay5eccfd988495
diff --git a/studio/frontend/src/features/chat/attachment-content.ts b/studio/frontend/src/features/chat/attachment-content.tsindex 47ea5444a1b..dc0cdf7b6a0 100644--- a/studio/frontend/src/features/chat/attachment-content.ts+++ b/studio/frontend/src/features/chat/attachment-content.ts@@ -952,6 +952,52 @@ export function getPdfAttachmentTextError(: `PDF has no readable text: ${fileName}. Scanned pages can't be read.`;}+function pdfFormFieldLines(+ annotations: {+ fieldType?: string;+ fieldName?: string;+ fieldValue?: unknown;+ alternativeText?: string;+ radioButton?: boolean;+ hidden?: boolean;+ password?: boolean;+ options?: { exportValue?: unknown; displayValue?: unknown }[];+ }[],+): string[] {+ const fields = new Map<string, string>();+ for (const {+ fieldType,+ fieldName,+ fieldValue,+ alternativeText,+ radioButton,+ hidden,+ password,+ options,+ } of annotations) {+ const value = [fieldValue]+ .flat()+ .filter((part) => typeof part === "string")+ .map((part) => {+ const shown = options?.find((option) => option.exportValue === part);+ return typeof shown?.displayValue === "string"+ ? shown.displayValue+ : part;+ })+ .join(", ");+ const unchecked = fieldType === "Btn" && value === "Off";+ if (!fieldName || !value.trim() || unchecked || hidden || password) {+ continue;+ }+ // The tooltip (/TU) is the human label behind codes like f1_01[0]; a radio+ // widget's tooltip names one option, not the group's selected value.+ const tooltip = radioButton ? "" : alternativeText;+ const label = tooltip?.replace(/\s+/g, " ").trim() || fieldName;+ fields.set(fieldName, `${label}: ${value}`);+ }+ return [...fields.values()];+}+export async function extractPdfAttachmentText(file: File): Promise<string> {assertDocumentAttachmentSize(file, "PDF");const [{ extractText, getDocumentProxy }, buffer] = await Promise.all([@@ -962,7 +1008,21 @@ export async function extractPdfAttachmentText(file: File): Promise<string> {try {// per page rather than merged: mergePages folds every newline pdf.js marks into one spaceconst { text } = await extractText(pdf);- return normalizeExtractedText(text.join("\n\n"));+ // getAnnotations re-reads the text under each link, so only forms call it+ const hasFields = await pdf.getFieldObjects().then(Boolean, () => false);+ const pages = await Promise.all(+ text.map(async (pageText, index) => {+ const annotations = hasFields+ ? await pdf+ .getPage(index + 1)+ .then((page) => page.getAnnotations())+ .catch(() => [])+ : [];+ const fields = pdfFormFieldLines(annotations);+ return [pageText, ...fields].filter(Boolean).join("\n");+ }),+ );+ return normalizeExtractedText(pages.join("\n\n"));} finally {await pdf.destroy();}diff --git a/studio/frontend/tests/attachment-preview-text.test.ts b/studio/frontend/tests/attachment-preview-text.test.tsindex cb1f982a2a8..a683419c767 100644--- a/studio/frontend/tests/attachment-preview-text.test.ts+++ b/studio/frontend/tests/attachment-preview-text.test.ts@@ -493,6 +493,7 @@ test("extractPdfAttachmentText destroys the PDF proxy after success and failure"],}),}),+ getFieldObjects: async () => null,destroy: async () => {destroyed.push("success");},@@ -527,14 +528,24 @@ test("extractPdfAttachmentText destroys the PDF proxy after success and failure"}});-function singlePagePdf(content: string, resources: string, extra: string[]) {+function singlePagePdf(+ content: string,+ resources: string,+ extra: string[],+ pageEntries = "",+ catalogEntries = "",+) {const objects = [- "<< /Type /Catalog /Pages 2 0 R >>",+ `<< /Type /Catalog /Pages 2 0 R ${catalogEntries}>>`,"<< /Type /Pages /Kids [3 0 R] /Count 1 >>",- `<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 200] /Resources ${resources} /Contents 4 0 R >>`,+ `<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 200] /Resources ${resources} /Contents 4 0 R ${pageEntries}>>`,`<< /Length ${content.length} >>\nstream\n${content}\nendstream`,...extra,];+ return pdfBytes(objects);+}++function pdfBytes(objects: string[]) {let body = "%PDF-1.4\n";const offsets = objects.map((object, index) => {const offset = body.length;@@ -594,6 +605,120 @@ test("a scanned pdf is refused unless the python tool can open it", async () =>assert.equal(getPdfAttachmentTextError(typed.name, typedText, false), null);});+test("a filled pdf form keeps the values typed into its fields", async () => {+ const font = "<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>";+ const field = (name: string, value: string, y: number) =>+ `<< /Type /Annot /Subtype /Widget /FT /Tx /T (${name}) /V (${value}) /Rect [60 ${y} 190 ${y + 20}] /P 3 0 R >>`;+ const checkbox = (name: string, state: string, y: number) =>+ `<< /Type /Annot /Subtype /Widget /FT /Btn /T (${name}) /V /${state} /AS /${state} /AP << /N << /Yes 10 0 R /Off 11 0 R >> >> /Rect [60 ${y} 70 ${y + 10}] /P 3 0 R >>`;+ const blank = "<< /Length 0 >>\nstream\n\nendstream";+ const filled = new File(+ [+ singlePagePdf(+ "BT /F1 12 Tf 20 100 Td (Name:) Tj ET",+ "<< /Font << /F1 5 0 R >> >>",+ [+ font,+ field("name", "Oscar Papa Quebec", 95),+ field("notes", "", 60),+ checkbox("agree", "Yes", 30),+ checkbox("newsletter", "Off", 10),+ blank,+ blank,+ `<< /Type /Annot /Subtype /Widget /FT /Tx /F 2 /T (internal) /V (hidden) /Rect [0 0 1 1] /P 3 0 R >>`,+ `<< /Type /Annot /Subtype /Widget /FT /Ch /Ff 131072 /T (status) /TU (Marital\\nstatus) /Opt [[(1) (Single)] [(2) (Married)]] /V (2) /Rect [60 150 190 170] /P 3 0 R >>`,+ `<< /Type /Annot /Subtype /Widget /FT /Tx /Ff 8192 /T (pin) /V (4321) /Rect [60 175 190 195] /P 3 0 R >>`,+ ],+ "/Annots [6 0 R 7 0 R 8 0 R 9 0 R 12 0 R 13 0 R 14 0 R] ",+ "/AcroForm << /Fields [6 0 R 7 0 R 8 0 R 9 0 R 12 0 R 13 0 R 14 0 R] >> ",+ ),+ ],+ "filled.pdf",+ { type: "application/pdf" },+ );+ const fieldsOnly = new File(+ [+ singlePagePdf(+ "",+ "<< >>",+ [field("name", "Romeo Sierra", 95)],+ "/Annots [5 0 R] ",+ "/AcroForm << /Fields [5 0 R] >> ",+ ),+ ],+ "fields-only.pdf",+ { type: "application/pdf" },+ );++ assert.equal(+ await extractPdfAttachmentText(filled),+ "Name:\nname: Oscar Papa Quebec\nagree: Yes\nMarital status: Married",+ );+ const fieldsOnlyText = await extractPdfAttachmentText(fieldsOnly);+ assert.equal(fieldsOnlyText, "name: Romeo Sierra");+ assert.equal(+ getPdfAttachmentTextError(fieldsOnly.name, fieldsOnlyText, false),+ null,+ );+});++test("a multi-page pdf form keeps each page's values with that page", async () => {+ const stream = (content: string) =>+ `<< /Length ${content.length} >>\nstream\n${content}\nendstream`;+ const page = (contents: number, annots: string) =>+ `<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 200] /Resources << /Font << /F1 10 0 R >> >> /Contents ${contents} 0 R /Annots [${annots}] >>`;+ const option = (exportName: string, tooltip: string, x: number, on: boolean) =>+ `<< /Type /Annot /Subtype /Widget /Parent 6 0 R /TU (${tooltip}) /AS /${on ? exportName : "Off"} /AP << /N << /${exportName} 12 0 R /Off 12 0 R >> >> /Rect [${x} 20 ${x + 10} 30] /P 3 0 R >>`;+ const form = new File(+ [+ pdfBytes([+ "<< /Type /Catalog /Pages 2 0 R /AcroForm << /Fields [6 0 R 9 0 R 13 0 R 14 0 R] >> >>",+ "<< /Type /Pages /Kids [3 0 R 4 0 R] /Count 2 >>",+ page(5, "7 0 R 8 0 R"),+ page(11, "9 0 R 13 0 R 14 0 R"),+ stream("BT /F1 12 Tf 20 100 Td (Filing status) Tj ET"),+ "<< /FT /Btn /Ff 49152 /T (form1[0].c1_1[0]) /V /S /Kids [7 0 R 8 0 R] >>",+ option("S", "Single", 20, true),+ option("M", "Married", 60, false),+ "<< /Type /Annot /Subtype /Widget /FT /Tx /T (form1[0].f2_01[0]) /TU (Date\\nsigned) /V (2026-09-30) /Rect [60 95 190 115] /P 4 0 R >>",+ "<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>",+ stream("BT /F1 12 Tf 20 100 Td (Signature) Tj ET"),+ stream(""),+ "<< /Type /Annot /Subtype /Widget /FT /Ch /Ff 2097152 /T (languages) /Opt [(English) (French) (German)] /V [(English) (German)] /Rect [60 40 190 80] /P 4 0 R >>",+ "<< /Type /Annot /Subtype /Widget /FT /Tx /T (lights) /V (Off) /Rect [60 10 190 30] /P 4 0 R >>",+ ]),+ ],+ "form.pdf",+ { type: "application/pdf" },+ );++ assert.equal(+ await extractPdfAttachmentText(form),+ "Filing status\nform1[0].c1_1[0]: S\n\nSignature\nDate signed: 2026-09-30\nlanguages: English, German\nlights: Off",+ );+});++test("a pdf with a damaged form field still reads its text", async () => {+ const damaged = new File(+ [+ singlePagePdf(+ "BT /F1 12 Tf 20 100 Td (Budget: 4200) Tj ET",+ "<< /Font << /F1 5 0 R >> >>",+ [+ "<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>",+ "<< /Type /Annot /Subtype /Widget /FT /Tx /T null /V (x) /Rect [60 20 190 40] >>",+ ],+ "/Annots [6 0 R] ",+ "/AcroForm << /Fields [6 0 R] >> ",+ ),+ ],+ "damaged.pdf",+ { type: "application/pdf" },+ );++ assert.equal(await extractPdfAttachmentText(damaged), "Budget: 4200");+});+// The bytes are requested synchronously, so the extractor is reached without// waiting on unpdf, which the preview test does not exercise.test("readAttachmentText reads a pdf under the ceiling", () => {