Story authoring and library management
Stories are saved, versioned data views that users can create, edit, reuse, and organize inside a project. The backend owns story access and durable state, while the web application provides authoring tools, a searchable library, folders, references, filters, and project-wide visual themes. For the wider story lifecycle, see Stories and visualization and Story delivery and sharing.
Story authoring
Story formats
A story can use the classic format or the custom format. Classic stories use a story document with text and data blocks. Custom stories use a small app made from story files. Both formats have a title, a stable slug, a project context, and a sequence of saved versions. The story page chooses the matching authoring and preview experience from this format. A custom story also offers an app view for the rendered result and a files view for its source files. References: apps/…/story.queries.ts• L53-91, apps/…/custom-story.ts• L98-123, apps/…/_sidebar-layout.stories.$storyId.tsx• L18-35, apps/…/custom-story-view-mode.tsx• L11-34
Custom story views
Custom story data is served through the access context in which the story is opened. Owner, standalone, shared, and read-only viewer contexts can load the story result and its related query data, state, narratives, and chart support according to their permissions. Shared views use a limited data path, while source files are available only to owners and read-only viewers. The full-page and side-panel views use the same story model while applying their own edit, chat, and navigation rules. References: apps/…/custom-story.ts• L418-487, apps/…/custom-story.ts• L489-530, apps/…/custom-story-page.tsx• L53-157, apps/…/custom-story-page.tsx• L401-484, apps/…/custom-story-viewer.tsx• L47-89
The app view runs the custom story as an isolated page. It can answer data and model requests, apply the project theme, keep story state, support questions about story elements, and export table data. The host keeps these requests tied to the current story and viewer. The files view lets an author inspect the story source and shared story kit files without changing the rendered result until the edits are saved and published. Owners and editors can edit supported blocks and table formats from the app view, or edit story files from the files view, while read-only shared views remain focused on viewing and download. References: apps/…/custom-story-frame.tsx• L81-117, apps/…/custom-story-frame.tsx• L265-377, apps/…/custom-story-page.tsx• L163-211, apps/…/custom-story-viewer.tsx• L205-247
Viewer-specific query and SQL answers can be kept for the current session. A persistent viewer data cache also scopes saved story results to the story version, viewer, and access revision. This lets a download or later request reuse data without allowing rows produced under an older permission state to match the current viewer. Project cache invalidation retires the related viewer entries when project data or access changes. References: apps/…/story-request-answers.ts• L4-37, apps/…/custom-story-frame.tsx• L166-200, apps/…/story-viewer-data-cache.queries.ts• L6-75
Custom story query access follows the sources named by the selected version. A shared or edited story can expose only the queries that its own story files reference. A standalone story can use saved story-level SQL when it has it, or keep serving its saved rows when it does not retain the SQL needed for a new run. References: apps/…/custom-story.ts• L281-318, apps/…/custom-story.ts• L418-464, apps/…/custom-story.ts• L508-529, apps/…/story-query.queries.ts• L7-68
Custom story downloads package the app, theme, runtime, query results, remembered request answers, narratives, state, and chart support into a self-contained page before the server creates the requested file. The download uses the viewer’s access for data and leaves out queries that the current viewer cannot read. This gives a downloaded story a usable result even when it is no longer connected to the source page. References: apps/…/use-custom-story-download.ts• L27-57, apps/…/use-custom-story-download.ts• L72-103, apps/…/story-request-answers.ts• L27-32
Story creation
Story creation turns authored content into a first version and places the new story in the creator’s private library area. A story may start from a chat or exist as a standalone project story. Custom stories first receive a draft file set, then become visible as a version only after the app builds successfully.
The main creation flow connects the authoring surface to durable story state and then to the library.
flowchart LR
A[Chat or standalone authoring] --> B[Draft content]
B --> C{Story format}
C -->|Classic| D[Save story version]
C -->|Custom| E[Build custom story app]
E -->|Build succeeds| D
D --> F[Private library placement]
D --> G[Preview and version history]
References: apps/…/story.routes.ts• L657-693 apps/…/custom-story-authoring.ts• L22-36 apps/…/custom-story-authoring.ts• L62-85 apps/…/story.queries.ts• L396-493 apps/…/story-folder.queries.ts• L159-189
Story editing
Editing works against a draft view of the current version. Authors can edit the rendered story or its code, then save a new version rather than silently changing history. The editor guards navigation and view changes when unsaved work exists. Custom stories support focused edits to blocks and tables in the app view, or edits to story files in the files view, when the latest version is editable. File edits build before publication; a failed build keeps the draft and reports the errors without publishing it. The page and side-panel viewer also let an author select a story element for a question or edit request when that action is available. References: apps/…/use-story-page-editor.ts• L21-125, apps/…/story.routes.ts• L536-625, apps/…/custom-story-page.tsx• L68-115, apps/…/custom-story-page.tsx• L163-211, apps/…/custom-story-viewer.tsx• L101-156
The story uses an expiring edit lock to make active editing ownership visible and to reduce conflicting manual changes. The lock belongs to a user and editing context, uses database time for expiry, can be renewed by its holder, and can be released when the context ends. Version saves still check that the draft came from the latest version, so a concurrent save is reported as a version conflict rather than overwriting newer work. References: apps/…/story-edit-lock.queries.ts• L7-16, apps/…/story-edit-lock.queries.ts• L45-92, apps/…/story.queries.ts• L326-393, apps/…/story.queries.ts• L1046-1060
Version history lets an author inspect an earlier version and restore it as the active content. Historical versions remain useful for viewing and export, while editing is centered on the latest version. The selected version also determines which content and data source the custom story uses. References: apps/…/story.queries.ts• L728-776, apps/…/owned-story-page.tsx• L127-134, apps/…/use-custom-story.ts• L10-79, apps/…/custom-story-page.tsx• L146-156, apps/…/custom-story-viewer.tsx• L173-189
Story forking
Forking creates an independent chat and story copy for the person who starts the fork. The copy carries the latest story content and the available query data so the new author can continue from a useful starting point. A custom story also copies its latest files and must build before the fork is complete. References: apps/…/story-fork.ts• L29-51, apps/…/story-fork.ts• L54-120, apps/…/chat-fork.routes.ts• L149-176
Forks preserve the source story as context rather than as a live dependency. This keeps later edits to the source from changing the fork while still showing where the new work came from. References: apps/…/story-fork.ts• L35-43, apps/…/chat-fork.routes.ts• L255-270
Story library
Library state
The library combines owned chat stories, standalone stories, and stories shared into the current project. It shows the latest saved version with summary, author, live state, certification, favorites, sharing state, and view information. Archived stories form a separate state and are removed from active folder placement until restored. Access includes ownership and project sharing, including grants to specific users or groups. References: apps/…/story.routes.ts• L152-209, apps/…/story.queries.ts• L151-253, apps/…/story.queries.ts• L629-700, apps/…/stories-page.ts• L77-100
The library page loads these sources together, then applies local search, filters, sorting, folder scope, and display choices. Project changes and archive changes reset the visible selection so actions do not carry across unrelated library state. Story result data can also be retired for the whole project through a shared cache revision, so later viewers do not reuse data from an older project state. References: apps/…/_sidebar-layout.stories.index.tsx• L119-190, apps/…/_sidebar-layout.stories.index.tsx• L204-265, apps/…/story-viewer-data-cache.queries.ts• L46-75
Folders
Folders give a project a tree for organizing stories. Public folders are visible in the project. Private folders are visible to their owner, and each user receives a private root for new stories. The special “Shared with me” view represents specifically shared stories without being a normal editable folder. References: apps/…/story-folder.queries.ts• L32-48, apps/…/story-folder.queries.ts• L50-112, apps/…/story-folder.queries.ts• L138-189
Moving a story or folder changes both its library position and its sharing scope. Public placement creates project visibility for a story, while private placement removes project-wide sharing. Folder moves reject cycles and protect system folders. Folder moves and bulk placement run as all-or-nothing transactions so a partial selection does not leave the tree and sharing state out of sync. References: apps/…/story-folder.queries.ts• L306-377, apps/…/story-folder.queries.ts• L395-475, apps/…/story-folder.queries.ts• L519-524
Archiving a folder archives its descendants and the stories placed inside them. Deleting a folder keeps its contents by moving them to the parent where possible. Restoring a private folder places its root back under the owner’s private root, while public folders return to the project root. These rules let the library change structure without losing story content. References: apps/…/story-folder.queries.ts• L241-304, apps/…/story.queries.ts• L579-601
Search filters and sorting
The library search matches story title, author, summary, story ID, and chat ID. It can also restrict results by certification, live state, favorites, sharing, format, and creator. Sorting supports name, owner, update time, creation time, and views. The page stores the selected sort and display mode locally for the user. References: apps/…/stories-page.ts• L10-74, apps/…/stories-page.ts• L283-309, apps/…/stories-filters-menu.tsx• L21-40
Search and filters flatten matching stories across the current folder subtree. A pasted story or chat ID can still locate an item outside that folder scope. Normal browsing keeps folders and stories in a tree, while result mode shows matching stories as a flat result set. References: apps/…/stories-page.ts• L311-423, apps/…/stories-explorer.tsx• L59-85
Selection and placement
Users can select stories and folders for bulk archive, restore, move, or starting a chat with the selected references. Dragging a story or folder provides the same reference model and can also start a new chat when dropped on the new-chat target. Viewers and users without the required ownership cannot perform placement changes. References: apps/…/_sidebar-layout.stories.index.tsx• L267-305, apps/…/_sidebar-layout.stories.index.tsx• L550-671, apps/…/story-folder.routes.ts• L91-107
Story templates and themes
Story query templates
Story content can declare filters that map user selections to SQL query templates. The backend validates that story blocks refer to known queries and that filter definitions connect to query SQL. The web application presents controls for select, multi-select, date range, and text search values. References: apps/…/story-template-validation.ts• L15-32, apps/…/story-template-validation.ts• L95-121, apps/…/story-filter-bar.tsx• L67-183
Filter options may be fixed in the story or loaded from a data source. Active selections are debounced before query data is fetched, and the current selections are kept in the URL so a filtered story view can be revisited. References: apps/…/story-filters.ts• L29-70, apps/…/use-story-filters.ts• L31-120
Project story themes
A project story theme defines the shared visual language for its stories. It includes a shared brand identity plus light and dark variants for surfaces, colors, fonts, text roles, blocks, tables, KPI tiles, filters, tabs, and chart palettes. Stories select the variant that matches the viewer’s application color mode. The theme reaches story content through shared style values, so the same rules shape both story data blocks and authoring-facing controls. References: apps/…/story-theme.ts• L99-200, apps/…/story-theme.ts• L224-235, apps/…/story-theme-brand-styles.ts• L17-47, apps/…/story-theme-brand-styles.ts• L97-145, apps/…/custom-story-body.tsx• L73-112
Admins can edit or generate a theme, preview it, save it, restore earlier saved versions, or disable it without losing the saved theme. Generated themes remain a draft until an admin reviews and saves them. Generation can use a website, image, PDF, ZIP, document, instructions, white-label settings, or an uploaded logo. The source that defines the theme’s active mode supplies the other mode through a derived variant that keeps the brand while adjusting surfaces and readable contrast. References: apps/…/generate.ts• L87-140, apps/…/generate.ts• L264-391, apps/…/story-theme-settings.tsx• L94-235, apps/…/story-theme-settings.tsx• L240-379, apps/…/story-theme-pair.ts• L28-121
The settings editor keeps brand identity in both variants, lets an admin edit the active variant, and offers a preview before saving. Logo input is limited to supported image types and size, and a hand-uploaded logo survives generation when the generated result has no logo. References: apps/…/story-theme-settings.tsx• L170-190, apps/…/story-theme-settings.tsx• L543-585, apps/…/story-theme-settings.tsx• L661-672, apps/…/story-theme.ts• L176-190
Story references
Referenceable story set
The reference system exposes active stories that the user owns or can access through the project. It favors pinned and certified stories, then recent stories, so authors can find trusted context quickly. Archived stories are not referenceable. References: apps/…/story-reference.queries.ts• L24-52, apps/…/story-reference.queries.ts• L67-114, apps/…/story-reference.queries.ts• L187-225
Reference menu
The web application presents stories, folders, database tables, and schemas in one searchable mention menu. Story folders mirror the library tree, while quick picks use the most viewed and trusted stories. The menu also records each item’s origin so the user can see its place in the tree. References: apps/…/reference-mention-menu.tsx• L24-70, apps/…/reference-mention-menu.tsx• L81-147, apps/…/mention-origin-preview.tsx• L21-61
Reference-driven chats
Mentioning a story gives a chat a stable story reference. Selecting or dragging stories and folders can also seed a new chat, and the backend records whether the resulting chat came from a full story or a selected part. This connects library organization with continued analysis without changing the source story. References: apps/…/story-reference.ts• L29-55, apps/…/_sidebar-layout.stories.index.tsx• L637-647, apps/…/chat-fork.routes.ts• L124-147
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the story format decision aligned across story resolution, authoring, preview, query access, and fork paths.
- Keep version creation, latest-version checks, edit locks, and private-root placement aligned so concurrent saves cannot overwrite newer work and a new story appears in the library.
- Keep custom story access contexts aligned across app rendering, file reads, query data, model calls, SQL, state, narratives, chart plugins, and downloads.
- Keep custom story app and files views aligned with version selection, edit permissions, element questions, code questions, and lazy view loading.
- Keep custom story file edits, build validation, version creation, and cache invalidation aligned so failed builds do not publish partial work.
- Keep viewer-specific query and SQL request answers scoped to the viewer and data source, bounded in memory, and included in downloads only for that same source.
- Keep persistent viewer data cache keys tied to the story version, viewer, and permission revision; bump the project revision when access or source data makes old results unsafe.
- Keep story-level saved SQL aligned with story-file query references and standalone query reruns.
- Keep folder visibility changes synchronized with story sharing changes; a move must not leave a story visible in a folder with a different access scope.
- Keep folder moves transactional, cycle-safe, and protected from system-folder changes.
- Keep archive and unarchive behavior aligned for stories, folders, folder items, and scheduled story state.
- Keep filter IDs, story filter declarations, SQL filter blocks, URL parameters, and owned/shared query routes aligned.
- Keep theme variants, brand identity normalization, theme generation records, saved theme versions, active-theme lookup, and preview style values aligned.
- Keep theme controls and shared style selectors aligned so changes to typography, KPI tiles, filters, tables, charts, or story tabs affect the same story surfaces.
- Keep mention IDs and folder scope rules aligned between the reference menu, drag-and-drop data, and backend reference queries.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A custom story draft does not build | The draft is not published as a story version | Build before cutting a version from the draft |
| An author leaves with unsaved story edits | The draft can be lost or the author can switch away from the intended content | Exit guards require save or discard decisions |
| Another author saves after an editor loaded the current version | The editor can overwrite newer work or present stale content | Edit locks signal active ownership and latest-version checks reject stale saves |
| A custom story app reports repeated runtime errors | The rendered story can fail or hide useful error context | The host batches runtime errors, limits the visible list, and shows the latest message and stack |
| A custom story frame navigates away from its isolated page | The story loses its data and host controls | The frame detects a second load, closes its message channel, and stops the view |
| A custom story reads viewer rows and then tries to write shared state | Other viewers can see state that depends on the wrong viewer’s access | The host rejects shared state changes after viewer-specific rows were read |
| A project permission or source change leaves an old viewer cache revision in use | A viewer can reuse data saved under an older access state | Cache keys include the project permission revision and project invalidation advances that revision |
| A custom story uses an unavailable query or data source | A block can fail to load or a download can omit that query | Access-specific data routes enforce permissions; unknown query errors are omitted only for export |
| A standalone story needs filtered rows but saved SQL is absent | The viewer receives saved rows instead of a new filtered query result | Keep story SQL with the story when input-based reruns are required |
| A download uses remembered rows from another viewer or story source | The file can contain data from the wrong access context | Request answers are keyed by viewer and complete data source, then bounded and cleared with the session |
| A folder is moved under itself or a descendant | The folder tree would become cyclic | Backend parent-chain checks reject the move |
| A bulk folder move fails after some items change | Folder placement and story sharing can disagree | Move the selection in one transaction |
| A story moves between private and public folders without a share update | Library placement and access scope disagree | Placement and share changes run together |
| A story filter has no valid query or SQL template | The filter can show no data or fail to load options | Template warnings and runtime validation report the broken definition |
| A user edits a non-latest story version as if it were current | New work can be based on stale content | Version navigation distinguishes historical versions from the latest |
| Theme generation cannot read a source or use the project model | The result may fall back to limited source signals or fail when no usable source remains | Generation records warnings and requires an available source or model for unsupported cases |
| A generated theme is treated as saved before review | Unchecked visual choices can affect project stories | Generation stays pending until an admin validates and saves it |
| A theme pair or brand header is incomplete | One color mode or the story identity can lose the intended brand | Shared schemas normalize identity and derive the opposite mode with contrast guards |
| An archived standalone story still reserves space for story questions | The page can show an empty input area for a story that cannot accept questions | Archived standalone views remove the ask-bar inset and hide story questions |
Key files
| File | Why to read it |
|---|---|
| apps/…/story.routes.ts | Main backend boundary for story creation, versions, library state, filters, and archive actions |
| apps/…/story.queries.ts | Durable story, version, access, archive, conflict, and cached-data model |
| apps/…/story-viewer-data-cache.queries.ts | Viewer-scoped story result cache and project-wide cache revision invalidation |
| apps/…/story-query.queries.ts | Story-level saved SQL used to support custom story query access and reruns |
| apps/…/story-edit-lock.queries.ts | Expiring ownership model for concurrent story editing |
| apps/…/custom-story-authoring.ts | Custom story draft, build, publish, and first-library-placement lifecycle |
| apps/…/custom-story.ts | Custom story version, query, cache, narrative, file, and theme access rules |
| apps/…/_sidebar-layout.stories.index.tsx | End-to-end library page state, loading, selection, drag-and-drop, and bulk actions |
| apps/…/stories-page.ts | Library item model, search scope, filters, sorting, and folder-tree behavior |
| apps/…/story-folder.routes.ts | Folder permissions and story or folder movement rules |
| apps/…/story-folder.queries.ts | Folder tree, visibility propagation, archive behavior, and transactional placement state |
| apps/…/use-story-page-editor.ts | Story editing, unsaved state, version navigation, and save or restore coordination |
| apps/…/custom-story-page.tsx | Full-page custom story contexts for owners, standalone stories, and shared views; archived standalone stories also suppress the question-bar inset |
| apps/…/custom-story-viewer.tsx | Side-panel custom story viewing, editing, versioning, citations, and story actions |
| apps/…/readonly-custom-story-viewer.tsx | Read-only custom story access, data loading, and download behavior |
| apps/…/custom-story-frame.tsx | Isolated custom story runtime, host communication, data, state, and exports |
| apps/…/custom-story-view-mode.tsx | App or files view switching and element-selection control |
| apps/…/custom-story-files.tsx | Custom story source browsing, editing, build validation, and publication controls |
| apps/…/story-data-options.ts | Access-specific query, file, model, SQL, narrative, chart, and state routes |
| apps/…/story-request-answers.ts | Viewer-scoped cache of filtered and story-SQL answers used by downloads |
| apps/…/use-custom-story.ts | Custom story version selection, restoration, loading, and download setup |
| apps/…/use-custom-story-download.ts | Self-contained custom story export assembly |
| apps/…/story-tabs-bar.tsx | Custom story tab selection, naming, deletion, ordering, and block drop targets |
| apps/…/story-fork.ts | Story-to-chat fork behavior for classic and custom stories |
| apps/…/story-theme.ts | Shared story theme contract, brand identity, validation, and light or dark theme model |
| apps/…/story-theme-pair.ts | Theme-pair completion and contrast-safe derivation between color modes |
| apps/…/story-theme-brand-styles.ts | Shared visual rules for story text, blocks, filters, tabs, headers, and charts |
| apps/…/generate.ts | Theme generation from visual, written, white-label, and admin-provided sources |
| apps/…/story-theme-settings.tsx | Theme editing, generation review, logo handling, preview, and save or enable actions |
| apps/…/story-theme.routes.ts | Theme read, save, generation, restore, and enablement boundary |
| apps/…/story-reference.queries.ts | Rules for which stories can be referenced and how they are ranked |
| apps/…/reference-mention-menu.tsx | Library and folder structure presented in the story reference menu |
| apps/…/use-story-filters.ts | Story filter state, URL persistence, debounce, and filtered data selection |