Build a public AI summarizer with saved history #
On this page
Prompt:
Build a public page where anyone can summarize a note and read saved notes and summaries. Keep that history in PostgreSQL. Visitors do not sign in. Test in development before I approve production.
Start by preparing the database-backed service. Use the database walkthrough to prepare the files first. Then adapt the linked AI and web client examples into the same application. Keep its source text and generated summaries in database rows so they survive a page reload or an application restart.
This walkthrough uses a server so you can run the same process locally and check its database-backed health route before you deploy. You can also build this workflow with functions that use PostgreSQL and call AI over fetch.
This is a shared public history: every visitor can read the saved text. Choose it only for material intended to be public. For private notes belonging to each signed-in user, start with Build a notes app with sign-in and a database.
What your AI does #
Your AI builds one backend with a database, adds a route that calls AI, and gives the browser a form and a history view. It tests the combined behavior before deployment and reports what it checked.
What you need #
- A connected AI coding agent, signed in or connected with a token whose grants cover the work.
- A decision to make the notes and summaries public, and a choice of response when AI cannot answer.
- An application size and the current size and usage rules. Read AI charges and balance before trying live model calls.
Before your AI starts #
Read this sequence and the linked procedures before running them. The database walkthrough can deploy: use it to prepare the files first, then return here before its deploy step. Confirm that the person wants hosted development before turning it on. With nobody to ask, turn it on only if their request already asks for development. Otherwise stop and report before deploying.
No visitor credential is needed for the public page. The backend calls AI with its own platform credential, which stays on the server. Put no database URL or platform credential in browser files.
Steps #
1. Prepare the database-backed service #
If you do not already have an application, call tzcloud_create_application with the server's name and chosen size. Keep its identifier. In Build a database-backed service, pass the identifier to the scaffold with --application <application id> or adapt the example files. Prepare the manifest, migrations and data layer. Keep the public audience and the database-backed health check. Stop before deployment.
Adapt the notes table to keep the submitted text and a summary that can be empty while AI is unavailable. Use a new migration for a schema already used in production. If a local run already applied the file to development, follow Keep the schema as migrations before editing it.
Give the backend routes to save a note, record its summary and read the saved history. Use parameterized queries as the database walkthrough does. A failed AI call must leave a readable saved note whose summary is still absent.
A shared database is one monthly item for the application; stored files are charged by size (Stay inside the Free Allowance).
2. Add the AI call to the backend #
Follow Call an AI model to take the AI Generation package and write the code that calls it. Keep both Database and AI Generation entries in the manifest's packages list.
From the note-submission route, save the text, call generateText with a request for a short summary, and store the returned text against that note. Return the saved note and summary to the browser. Never take the platform credential from the browser.
For allowance_unavailable, keep the note saved and show that no summary is available. This example makes its summary attempt when a visitor submits a note. This refusal is not an account pause: the application can keep serving.
For account_paused alone, follow Handle an account pause: stop outbound work without retrying. That section also covers provider capacity failures and the other fallback choices.
3. Add the public form and history view #
Follow Serve a web client for the page, script and styles under client/. Have the form post text to the backend and fetch saved history on page load. Display a missing summary clearly.
The browser calls the backend on its own hostname. It never calls the platform's AI route directly. Render saved text and AI output as text, so a visitor's markup is not executed. Keep files under client/ public and free of credentials.
4. Test the code, turn on development, and run locally #
Use Test your application locally with the Database and AI Generation test doubles, which stand in for the live services. Check a successful summary and an unavailable-AI answer: both keep the original note, and only the successful answer stores a summary.
Submit the complete manifest. If you have a shell, run the provision command before turning on hosted development, using Run your app on your computer for local settings and credentials.
Call tzcloud_create_environment with the application identifier from step 1 and environment: "development". The local service uses the development database. Follow Test in development, then promote for the next call if any development records still need to be created.
Check the public page without signing in. Submit a note, reload, and check that history comes from the backend. Restart the local service and read the same database again. Test doubles prove the code paths; they do not prove a hosted deploy or a live AI call.
5. Deploy to development and check the result #
If you changed the manifest, submit the complete manifest again. For files in a local project folder, follow Deploy from your computer. For files held in chat, follow Deploy from a chat. Development must already be on, so the deploy reaches it.
Open the development hostname returned by tzcloud_create_environment, without signing in. Submit a note, read its summary, then reload and confirm the saved history. If AI is unavailable, confirm that the note remains with no summary; report the successful live summary as unchecked.
For a hosted restart check, follow Restart an environment on development, then read the same history again. If your connection offers no restart tool, report hosted restart persistence as unchecked. Read status and logs if a check fails. Keep development's records separate from production's.
6. Approve production and check it #
On the development hostname, check every changed route and read the development logs for errors. Fix failures and redeploy before asking to promote.
Record the development version that passed. If development may change before promotion, pass that version number to promote. Follow Test in development, then promote. With no prior approval and nobody available to ask, stop before the promote and report the development version and unchecked production results. After an approved promote, read production's serving version and open its returned address. When the new server starts in production, it applies pending migrations to production's database before listening. The development test note stays in development.
Check production reads. On an app with real users, obtain the person's approval in this run before submitting a test note on production. With nobody available to approve it, leave the production write and live summary unchecked on an app with real users, and report them.
Expected result #
A visitor can submit text without signing in and read its saved summary in the shared history. Reloading the page or restarting the backend keeps the database records. An unavailable AI answer leaves the submitted note readable with no summary.
Your AI reports the address, serving version and checks passed, including which live AI calls or production writes remain unchecked.
Refusals #
Use the refusals reference and the linked guides for the operation that failed. These are common cases, not the full list.
| Refusal | Cause | Remedy |
|---|---|---|
allowance_application_required |
The AI call did not use an application-scoped credential. | Keep the call in the backend using its application's platform credential. |
allowance_unavailable |
No platform key or price is in effect for the AI route. | Keep the saved note, show that its summary is unavailable, and follow the AI guide's fallback. |
account_paused |
The account has paused. | Follow the AI guide's stop rule; do not retry outbound work. |
For database connection or migration failures, use Add a database: Refusals. Report the failed step and leave any unproved result unchecked.
Related #
- Build a database-backed service shows how to declare the database service, keep migrations and use the database.
- Call an AI model owns the AI transport, fallback and test double.
- Serve a web client owns browser files and same-host requests.
- Find the guide for a task covers other workflows.