Build private document chat
Compose application-owned login, private uploads, native vector search, and chat with source permissions and recovery.
Last updated 2026-11-26
What you'll achieve
- Compose private uploads, ingestion, native vector retrieval, and saved chat
- Preserve customer authorization and reconcile interrupted provider work
- Replace application providers and test the complete flow locally
Build chat over your customers' private documents
An upload-to-chat application lets a signed-in customer upload a document, prepare searchable content, and ask questions with citations. You can use this pattern for a team knowledge base, support portal, research workspace, or an assistant inside your SaaS application.
The flow composes optional application helpers, private storage, your application's DEFAULT database, libSQL native vector search, and your embedding and model providers. You control the application's identity provider, permissions, subscriptions, provider accounts, credentials, data, and user interface. Your users sign in to your application. BlinkHost account authentication and BlinkHost's internal email services are not application providers.
Use the deterministic provider fixtures to test your application’s logic locally. Before serving customers, test your chosen embedding and model providers with representative documents and measure response time and cost.
Configure the application
Choose an identity provider and configure its trusted issuer, audience, callback URL, signature verification, and session policy. Install the helper's login/session, organization, membership, and resource schemas through your application migrations. Verify the provider login before creating a session. Protect cookie-authenticated changes with exact-origin checks and CSRF validation, including login and logout. See secure application sessions for the runtime cookie contract.
Configure storage, embeddings, model access, and any billing or email provider with your own accounts. Keep credentials in backend secrets and trusted configuration. Do not return them to the browser, place them in URLs, or log them. Install document, embedding, conversation, provenance, and spending schemas before handling requests; constructing a helper does not create tables.
Bind the application and organization to the verified session. Resolve resource ownership and, for paid chat, the user's subscription binding from trusted application data. A request cannot choose its own subscription, role, provider account, or authorized organization. Separate conversation permission from document-read permission: joining a conversation does not grant access to its sources.
Create one authority and one operation budget per invocation. Pass that original budget through database, storage, embedding, model, and callback work, and close request authorities in a finally/defer guard. A new ingestion page or later chat request needs fresh authentication and authority.
Upload and publish searchable content
- Authorize the upload and store the bytes using the optional FileClient with a storage adapter such as SupabaseFileStore, or a compatible adapter for your storage service. Persist the exact immutable file reference and document binding in application-owned storage. The binding identifies the actor, source, revision, and chunking recipe; it is not an access grant.
- Start an ingestion revision with a fresh session and document permission. Download outside the SQL transaction. Check current binding and file access around external work, and verify the downloaded content's length and digest. Never reuse an earlier upload grant as permission to read later.
- Resume ingestion in bounded pages and persist progress. Keep incomplete revisions in staging. Publish a revision only after verifying all its expected content. Schedule later pages through your application's authenticated worker or a fresh customer request.
- Reserve document-embedding spending, call the configured embedding provider, and settle the observed cost. Publish the vectors only after successful validation and settlement. An incomplete response leaves unresolved spending for reconciliation and must not make an index ready.
The Go and Rust reference composition currently accepts UTF-8 plain text from 1 to 65,536 bytes, chunks of 1,024 to 4,096 bytes, overlap up to one quarter of the chunk size, and ingestion pages of one to eight chunks. These are bounds of this reference example, not general storage-upload or compiled-module limits. PDF parsing, OCR, and other formats need a separately bounded parser and their own validation.
Retrieve, answer, and save
Start from a fresh session. Check current conversation permission, paid entitlement where configured, and independent document-read permission. Load the conversation's bounded history and verify its source dependencies before sending content to an embedding or model provider.
Embed the question using a trusted embedding profile and a stable request identity. Settle its separate embedding cost before using the result. The reference retrieval uses libSQL's native exact cosine search over the application's document vectors and returns up to four citations. Native vector search ranks embeddings; your application's permissions and source checks decide which content may be used. Embedding models and language models remain services you configure.
Reload the current source binding, publication revision, file access, recipe, and content integrity. The Go and Rust reference validates all chunks of a selected document in bounded pages, including chunks outside the search result. Recheck selected citations and dependencies inherited from earlier answers before model input, output callbacks, and history persistence. Citation IDs locate content; they grant no authority. Do not replace these checks with a cached upload permission.
Reserve model spending and call your configured model outside a database writer transaction. Persist the answer and exact source provenance atomically after confirmed model settlement. Emit the completed/finish outcome only after confirmed persistence. History derived from an earlier answer retains that answer's source dependencies, even when the latest search finds no new citations. Revoked or changed sources can prevent reusing that history.
The Go and Rust default database-authorized model adapter buffers a bounded provider response before delivering decoded text callbacks. It can therefore validate permissions around callbacks without holding external HTTP work open during a database transaction. This delays its first application text callback until the provider response has arrived. It is not a promise of live token delivery to the browser. A custom transport must preserve authorization, cancellation, accounting, response bounds, and the runtime's HTTP/database interaction contract.
Recover without repeating paid or external effects
Keep a stable business operation ID for each upload, embedding, model call, protected write, and notification. A timeout does not prove the provider or database did nothing. Preserve the ID and recorded outcome, then reconcile through a fresh authorized request. Do not automatically repeat an uncertain mutation or paid call with a new ID.
| Situation | Application behavior |
|---|---|
| Session, membership, document access, or paid entitlement is revoked | Stop subsequent protected work and output; require fresh authority for later requests. |
| Ingestion is interrupted | Read primary progress and resume bounded pages; incomplete content stays staging. |
| Upload or SQL commit acknowledgement is lost | Reconcile the exact reference or operation ID before deciding whether new work is needed. |
| Embedding/model spending is reserved or unknown | Keep the durable claim, expose reconciliation, and withhold successful completion. |
| Model output arrives but saving the answer fails | Report an incomplete result; already delivered text cannot be recalled. Do not claim a saved conversation. |
| A notification is queued | Let an independently authenticated worker deliver through your application's provider with a stable delivery key. |
There is no transaction spanning your database, storage service, billing service, and model provider. Earlier confirmed work can remain committed when a later step fails. For example, a settled query-embedding cost remains recorded if subsequent generation fails. Show these states explicitly in your application's UI and diagnostics.
Extend the composition
Helpers are optional building blocks. You can replace identity, email, storage, database, embedding, and model adapters with implementations of their public contracts. Preserve exact scope, current authorization, primary-read and transaction guarantees, cancellation, deadlines, response bounds, and single-attempt handling of uncertain effects. Use maintained signature and cryptographic implementations instead of writing your own.
The Go reference composes SDKSessionApplication, SessionPrivateIngestion, SessionPrivateReferences, PrivateVectorRetrieval, and SessionRAGContext. The Rust reference provides the matching session, private ingestion, reference, retrieval, and context modules. Python and JavaScript/TypeScript expose corresponding optional helpers; constructors and supported optional features are language-specific. Use the matching release's language guide and compatibility matrix rather than assuming identical import names.
Keep provider adapters and resource authorization on the server. The browser may choose UI behavior and render authorized answers and citations, but it must not select trusted credentials, subscription bindings, or permission decisions. For email, give the worker your application's provider configuration; a customer account on BlinkHost is not an email-delivery account for your app.
Verify your application locally
Use isolated test data and your own deterministic provider fixtures. Run unit tests for policies and adapters, then integration tests with real libSQL and the supported compiled Wasm runtime. A native-language database test alone does not exercise the hosted runtime. Test sign-in through upload, paged ingestion, vector publication, question embedding, saved answer/provenance, and any queued notification.
Include a second customer and organization, expired and revoked sessions, a changed subscription binding, revoked file access between pages, corrupted content, interrupted uploads, lost commit replies, cancelled model output, duplicate request IDs, and worker restart. Check both the response and persisted records. An HTTP success or received answer text alone does not prove completion or correct spending.
Measure compilation/artifact size, guest memory, cold and warm request time, database exchanges, retrieval time, provider response timing, and cleanup separately. Application text callbacks are not browser delivery measurements. Record whether a result came from a local fixture, a compiled runtime, or an actual external provider, and validate browser behavior before release.
Help improve this page
Sign in to send page-specific feedback. For account-specific help, email support@blinkhost.me.