Files in, artifacts out

Send images, PDFs and text files, then download published project files or open sandboxed HTML previews.

intermediate9 min read

You drop a PDF into Chat, ask for a calculator based on a table inside it, and get a card with a preview. In a Developer chat, you can ask for a report or Git patch from the project and get a card that downloads the file. The PDF takes one route into the conversation. The calculator and the project file take two routes out.

In this chapter

  • Follow a file from the composer into the material the model receives.
  • Tell what changes when the selected model can look at images.
  • Ask Developer to publish a project file and find the saved download.
  • Trace a published HTML document from the conversation to its preview.

A PDF is uploaded before the message is sent

Sending a PDF from an existing chat
  • Browser
  • Server app.ts
  • Core
  • PDF worker pdf-worker.ts
  • Pi Durable
  • Disk
  1. Browser → Browser · Attach PDF

    The draft holds the file

    The browser recognizes the PDF. Press Send to start the upload.

  2. Browser → Server · POST /uploads

    The browser sends raw bytes

    The server checks the filename and compares the declared type with the signature in the bytes.

  3. Server → Disk · Save original

    The original gets its own directory

    The server writes original and metadata.json under uploads/<uuid>/ in the data directory.

  4. Server → Browser · Upload ID

    The draft keeps the upload ID

    If Send needs another try, the browser can refer to the same original.

  5. Browser → Server · POST /messages

    The message refers to the upload

    The message carries the prompt, its request ID, and an attachmentIds array.

  6. Server → Core · First check

    Core checks the chat and model

    Core looks for a prior receipt first. For a new message, it checks the chat and host capacity, then leaves the admission queue with the model’s image capability.

  7. Core → Server · Prepare uploads

    The upload service checks the original

    Outside the admission queue, the service reads the upload metadata and checks the batch size.

  8. Server → PDF worker · Fork PDF worker

    A separate process opens the original

    The worker extracts selectable text and renders pages if the model accepts images.

  9. PDF worker → Server · Page parts

    The worker returns prepared content

    The upload service checks the prepared size and adds the filename label.

  10. Server → Core · Prepared upload

    Core checks the chat again

    Core checks the returned IDs and their order, then takes another turn in the admission queue. In this example the model’s image support stayed the same.

  11. Core → Pi Durable · Admit input

    Core hands Pi the prepared parts

    Core supplies the prepared model input and display fields that record the original’s ID and the PDF mode.

  12. Pi Durable → Disk · Commit input

    The conversation keeps the prepared parts

    Pi Durable saves the entry and the work to answer it in durable.sqlite.

  13. Core → Server · Submission ID

    Core returns the receipt reference

    Core arms the run deadline after the commit and returns the submission ID and request ID.

  14. Server → Browser · 202 Accepted

    The browser can clear the sent draft

    The response acknowledges admission. The reply itself arrives through the conversation stream.

Browser
Server app.ts
Core
PDF worker pdf-worker.ts
Pi Durable
Disk

Browser → Browser

The draft holds the file

The browser recognizes the PDF. Press Send to start the upload.

Follow the original to the upload directory, then follow the prepared pages to the conversation. The upload ID connects the two paths.

Drop a file into the composer and the browser decides how to send it. attachments.ts recognizes PDFs and PNG, JPEG, GIF, and WebP images by the reported file type, falling back to the extension when the type is missing or generic. Those files wait in the draft for Send. Other files have to decode as UTF-8 text and pass a check for binary control characters before they can join the draft. attachments.ts

Press Send with a PDF and the browser posts its bytes to POST /api/v1/uploads before it submits the message. A new chat is created before the upload. The upload gives the file a randomly generated ID, which POST /api/v1/conversations/:id/messages carries in attachmentIds. If you’re sending a text file, the message request carries the name and text in textFiles, and core adds a File: heading when it assembles the text for the model. App.svelte contracts.ts

A successful upload is a saved original. The message still has to make it through admission, the step that accepts it for a reply. The browser keeps the upload ID so a retry can use the copy already on the host. If Pipal already accepted the exact message under that request ID, core can return the original receipt before opening any of its files. attachments.ts index.ts

Deep diveWhat the upload route checks before it writes

POST /api/v1/uploads takes the raw request body, the URL-encoded X-Filename header, and Content-Type. save() decodes and trims the filename, rejecting an empty name, a name over 255 UTF-16 code units, ASCII control characters, and either kind of slash. It checks the 10 MiB limit and compares the declared type against recognizable bytes at the start of the file. An image also needs readable, positive dimensions within 32 megapixels. app.ts uploads.ts

The filename stays in metadata.json, alongside the ID, detected type, and byte size. The generated UUID names the directory, and the bytes go in original. If saving the pair fails, save() removes the directory. The signed-in GET /api/v1/uploads/:id route serves the original by ID, without a chat-specific ownership check. uploads.ts app.ts

The model gets prepared content

What reaches the model
FeatureText-only model Image-capable model
Page with selectable textNumbered page textNumbered page text and a rendered page image
Scan with no selectable text anywherePreparation rejects the PDFPage images with [No selectable text on this page.] for each page
Standalone screenshotThe composer disables Send. The server also rejects the image during preparation.Prepared image content

Choose up to two columns

FeatureText-only model Image-capable model
Page with selectable textNumbered page textNumbered page text and a rendered page image
Scan with no selectable text anywherePreparation rejects the PDFPage images with [No selectable text on this page.] for each page
Standalone screenshotThe composer disables Send. The server also rejects the image during preparation.Prepared image content
Compare what Pipal prepares for the same file, assuming it passes the size and decoding checks.

Ask a text-only model to explain a PDF with selectable text and Pipal sends the text in page order. If the model accepts images, Pipal also renders each page, so the model gets the layout of the table as well as the words in it. A scan may contain no selectable text at all. In that case the text-only request fails during preparation, while an image-capable model can receive the rendered pages. pdf-worker.ts

A PDF can take time to open and render. Core checks whether the chat can take a message and whether the model accepts images, then lets go of the admission queue before preparing the files. Another chat can take its turn during that work. On the way back in, core checks again, because the chat may now be busy or you may have changed models in the meantime. Once the message is accepted, the server responds with HTTP 202 and the submission and request IDs. index.ts app.ts

Deep diveHow preparation stays outside the admission lock

Core compares the current model’s image support with the mode it prepared for. A change in that capability sends the batch through preparation again. The upload service allows two batches at once and returns HTTP 429 if both slots are in use. It prepares each batch’s files in order and checks a 12 MiB aggregate budget against UTF-8 text bytes and base64 image lengths before adding file labels. Pi’s Photon image worker thread prepares standalone images within a 2,000 × 2,000 pixel box and a 2 MiB base64 limit. index.ts uploads.ts README.md

packages/core/src/index.ts
for (;;) {
const result = await exclusive(admit);
if (result.id !== undefined) return { id: String(result.id), requestId };
// …
preparedFor = result.supportsImages;
files = await result.prepare();

Each PDF gets a forked Node.js process with an empty environment and Node permissions that restrict JavaScript filesystem reads. The parent sets a 256 MiB V8 old-space limit and kills the child if it takes longer than 20 seconds. A thread in the child samples resident memory every 25 milliseconds and kills the child above 1 GiB. That second check covers memory outside the JavaScript heap, including native canvas allocations. pdf.ts pdf-worker.ts

A document can have up to 12 pages and a budget of 32,768 UTF-16 code units for selectable text. Each page must fit in the remaining budget before trimming. The worker then deducts the trimmed text. Rendered pages scale to 1,536 pixels on the longer edge. The worker tries PNG for pages with enough matching neighboring pixels and keeps it if it fits within 512 KiB. Otherwise it uses JPEG. The base64 image data has a 3 MiB ceiling per page and a 6 MiB ceiling per PDF. pdf-worker.ts

Developer publishes a copy of a project file

Ask Developer to save a report in the selected project and attach it to the chat. With pipal.coding available in that workspace, Developer calls publish_file with a project-relative path such as out/report.csv. Publication waits for approval unless a matching YOLO grant covers it. The tool reads an existing regular file of up to 512 KiB and saves its bytes with the filename, report.csv in this example. project-coding.ts index.ts

Click Download file on the card to retrieve that saved copy. You can still download it after the workspace file has changed or been deleted. If Developer publishes a correction with revises set to the first file’s artifact ID, the new card says Download updated file. Both cards stay available. web-artifacts.ts WebArtifact.svelte

For a Git patch, Developer’s instructions say to capture the changes for your task, including new files, and publish the candidate before verification. If a check leads to a correction, Developer publishes the updated patch with the candidate’s ID in revises. When file publishing is unavailable, the instructions call for a unified diff in the reply. developer.md

If the turn limit arrives after publication, the patch stays available to download and Resume response can continue verification in the same top-level chat. patch-delivery.spec.ts

Deep diveWhat the publisher accepts from the selected project

publish_file rejects absolute paths and any .. component. It checks that the path is well-formed Unicode and that its basename contains no backslashes or Unicode control or format characters. The shared project-files.mjs worker checks the resolved project path, rejects .git components regardless of case, and requires real directories and a single-link regular file. It rejects reads if the byte count, size, or change time indicates that the file changed during the read. Core saves the bytes as base64 in a pipal.file-artifact entry, so binary files and text files both retain their original bytes. project-coding.ts project-files.mjs web-artifacts.ts

Web artifacts carry HTML for a preview

When the assistant has access to publish_web_artifact, it can save a self-contained HTML document of up to 64 KiB as a web artifactA self-contained HTML document the assistant publishes in a chat for you to preview or download.. The card offers Open preview and Download HTML. Ask for a larger input field and the assistant can use read_web_artifact to recover the HTML, then publish a new entry with revises pointing to the old ID. An .html file published through publish_file gets a file download card. web-artifacts.ts WebArtifact.svelte

When a specialist returns a delegate result, its published files and web artifacts can appear with that result in the parent chat. Pipal follows each conversation’s owning chain to the top-level chat. That shared root defines the delegation tree where artifact lookup works. A revision must point to an artifact of the same kind within that tree. read_web_artifact returns HTML source and rejects file artifact IDs. delegate.ts web-artifacts.ts

Deep diveWhy retrying a publish call keeps the same artifact ID

saveArtifact() commits either kind of entry and saves its ID in the task’s pipal.published-artifact document in the same transaction. On replay after that commit, publishProjectFile() finds the saved ID before trying to read the workspace file. The transcript projection emits card metadata, including kind: 'file' for a project file. For the web artifact tools, it strips the HTML argument from displayed publish calls and substitutes a short label for successful reads. web-artifacts.ts view.ts

Opening a preview creates a frame

The policy for an HTML artifact preview
sandbox allow-scripts allow-forms allow-modals; default-src 'none'; script-src 'unsafe-inline' 'unsafe-eval' 'wasm-unsafe-eval'; style-src 'unsafe-inline'; img-src data: blob:; media-src data: blob:; font-src data:; worker-src blob:; form-action 'none'; base-uri 'none'; frame-ancestors 'self'
packages/server/src/app.ts
  1. Frame permissions

    Scripts, form controls, and modal dialogs work. Without allow-same-origin, the frame gets a separate, opaque origin.

  2. Resource defaults

    The default blocks external subresources and connections such as fetch and WebSocket. WebRTC falls outside this policy.

  3. Local code

    The document can run inline scripts, use eval and WebAssembly, and supply inline styles.

  4. Embedded assets

    Images and media can use data or blob URLs. Fonts can use data URLs, and workers can use blob URLs.

  5. Document rules

    JavaScript can handle a form inside the frame. The browser blocks form submissions and prevents the document from setting a base URL.

  6. Allowed parents

    The browser checks who embeds the preview. The server also checks for a same-origin iframe request.

 allow-scripts allow-forms allow-modals;   'unsafe-inline' 'unsafe-eval' 'wasm-unsafe-eval'; style-src 'unsafe-inline';  data: blob:; media-src data: blob:; font-src data:; worker-src blob:;  base-uri 'none'; 
packages/server/src/app.ts

1 Frame permissions

Scripts, form controls, and modal dialogs work. Without allow-same-origin, the frame gets a separate, opaque origin.

Scroll across the exact header value or step through the annotations to see what each directive controls.

Click Open preview and WebArtifact.svelte creates an iframe, a document inside the chat page. The signed-in preview route checks for a same-origin iframe request and returns the HTML with a Content Security PolicyRules a web server sends to tell the browser which resources a document can load and what actions it can take.. That policy lets the calculator run its inline code while restricting where it can load resources. Close the preview and Svelte removes the frame. Download HTML uses a separate route that sends the source as a plain-text attachment with an .html filename. The preview policy applies to the frame. Open the downloaded file and the application you use determines how its code runs. WebArtifact.svelte app.ts

The frame’s sandbox gives it an opaque origin, an identity separate from the signed-in chat page. The policy blocks ordinary fetches and external subresources. It leaves WebRTC, a browser feature for direct connections between devices, available where the browser permits it. A preview can use those connections to send data it already has, including text typed into it. The security model follows the browser boundary in full. app.ts

What happens when the prompt comes from a calendar instead of the composer? Routines follows a message that Pipal sends while the browser is closed.

Recap

  • A message carries text files inline and refers to uploaded images and PDFs by ID.
  • Preparation runs outside the admission queue. The accepted conversation entry keeps the prepared model input.
  • Publishing a project file saves its bytes for download. Publishing a web artifact saves HTML you can download or run in a sandboxed preview.
Check yourself
  1. A PDF uploads, but Send fails on a text-only model. The PDF is a scan with no selectable text. What changes if you retry with an image-capable model?
    • The upload now has selectable text
    • The PDF worker can supply page images from the same original
    • The browser sends the original PDF directly to the provider
    Answer and explanation

    The PDF worker can supply page images from the same original

    The original can stay the same. The active model determines which parts the host prepares.

  2. Another chat sends a message while your PDF pages render. What lets that chat proceed?
    • Preparation runs outside the admission queue
    • PDF messages bypass admission
    • The PDF worker admits messages itself
    Answer and explanation

    Preparation runs outside the admission queue

    Core releases its place after the first check, then comes back to recheck the chat before committing the prepared input.

  3. Developer publishes a patch, publishes a correction, then deletes the workspace file. What does the first card download?
    • The corrected patch
    • The original patch
    • Nothing once the workspace file is gone
    Answer and explanation

    The original patch

    Publishing a revision appends a new entry. Each download uses the bytes saved with its own artifact ID.

  1. A PDF uploads, but Send fails on a text-only model. The PDF is a scan with no selectable text. What changes if you retry with an image-capable model?

  2. Another chat sends a message while your PDF pages render. What lets that chat proceed?

  3. Developer publishes a patch, publishes a correction, then deletes the workspace file. What does the first card download?

In the source

Book contents

Search the book