# Cotton Story: the core's methods The Ink redesign's data (permanent ids for every line, places, people, pictures, things, costumes, drafts, Final Draft out) is in `assets/design/story/CONTRACT.md`; it wins where the two differ. Every rule of Story lives in the Rust core once. Both phones call these methods through cotton-ffi (`cotton_call(handle, method, json)`), exactly as every other `.*` method; the Worker answers the same rules for MCP. A method answers `{ "ok": … }`-style JSON like the rest of the ffi, or the ffi's usual error `{ code, message }`; Story's own refusals are worded from the `story` line set (`kits/shell/lines/story.json`, `refusal.*`). These names and shapes are fixed from the first push. A body that has not landed answers the error `{ code: "not_yet", message }`. Any later change to a name or a shape is written at the top of the core session's report first. ## Words used below - **body**: a scene's text in Fountain, without its heading line. Lines are split on `\n` (a `\r\n` is read as `\n`). - **offsets** (`cursor`, `start`, `end`): UTF-16 code units from the start of the body (Kotlin `String` indices; Swift `String.utf16`). Never bytes. - **line** (an index): 0-based line number in the body. - **kind**: one of `action`, `character`, `dialogue`, `parenthetical`, `transition`, `heading`, `centered`, `note`, `blank`. The element bar's five map as Action = `action`, Character = `character`, Speech = `dialogue`, Aside = `parenthetical`, Cut = `transition` (bar words: `story` lines `bar.*`; every kind's name: `kind.*`). - **story**, **scene**: record ids of `story_story` and `story_scene`. - **names** (optional on most calls): extra names to treat as characters, besides the story's `story_character` rows. When `story` or `scene` is given, the core reads that story's names itself. ## Tables `story_story` (title, logline, written_by, language, translation_of, format, made_at, chosen_at; derived scenes, pages), `story_scene` (story, position, setting, place, time, heading, summary, body, status; derived words, pages, minutes, speakers), `story_character` (story, name, role, about, want, picture; derived scenes, lines), `story_place` (story, name, about; derived scenes), `story_note` (story, title, body, scene). The definitions are in `crates/cotton-core/kits/personal/tables/story_*.json`. `story` is taken in the tree (Social stories), so the story table is `story_story` (DEVIATIONS in report.md). Derived columns are written by the core only (`story.scene.write`, `story.fountain.read`, `story.copy`, `story.rename`); a phone never writes them. `pages` is in pages with eighths (`0.375` = 3/8); `minutes` is pages at one minute a page; `speakers` is the names that speak, one a line, in order of first speech. ## The current story Scenes, Characters, Places, Notes and Script show the current story only. The tool's views say so in data: `"current": {"column": "story", "by": "chosen_at"}` (ToolView `current`, `cotton_core::definitions::tool_views:: CurrentOf`). The current story is the `story_story` row with the latest `chosen_at`; while none has one, the newest story. The core narrows those views itself wherever it reads them (deck, counts, tiles); a phone draws what it is given. - `story.choose { story }` → `{ story }`: makes it current (writes `chosen_at` = now). Stories' card tap. - `story.current {}` → `{ story: { id, title, language, format } | null, stories: n }`: what the screen titles itself with. `null` with `stories: 0` is the first launch: offer the one act, Start a story (`stories.first`). - `story.start { title }` → `{ story }`: a new story with only its title, made current. Language "English", format "feature", made_at today. ## Writing - `story.lines { body, story?, scene?, names?, chosen? }` → `{ lines: [ { kind, text, start, end, name? } ] }` `chosen: { line, kind }` is the kind the bar shows for the line being typed (from the last `story.press`/`story.kind` answer): it stands while the text alone reads that line as action or blank. One entry per line of the body, in order. `start`/`end` are the line's offsets (end exclusive, the `\n` not included). `name` is on a `character` line: the bare name without extensions such as `(V.O.)` or `(CONT'D)` and without a forcing `@`. A character line is known by position (a line on its own after a blank, with speech under it) and by the story's names, never only by capitals: a Telugu name the story knows is a character line. Runs on every keystroke: answers in a few milliseconds for a 10-page scene. - `story.press { body, cursor, key, kind?, story?, scene?, names? }` → `{ body, cursor, kind, line, names? }` `kind` (optional) is the kind the bar shows for the cursor's line; on Return a line chosen as a name is written so it reads as one (capitals, or `@` in a script without them). `line` is the cursor's line index. `key` is `"return"` or `"tab"`. Answers the new body and cursor and the kind of the line the cursor now stands on. The rules: - Return after a character line: a new line for `dialogue`. - Return after a parenthetical: `dialogue`. - Return at the end of a non-empty dialogue line: a blank line and a new empty line of kind `character`, ready for the reply (`names` offered, the scene's other speaker first). An empty Return on that empty line makes it `action` (the "empty Return after speech"). - Return on action: a blank separating line and a new `action` line. - Tab on an empty line: `character`; `names` offers the scene's other speaker first (the one who did not speak last), then the rest. - Tab on an empty line chosen as a name: `action`; on an empty speech line: an aside, `()` written and the cursor inside; on an empty aside: speech again. - Tab on a line with words turns it to the next kind: action ↔ character, dialogue ↔ parenthetical, a transition or heading to action. `names` is present only when `kind` is `character`. - `story.kind { body, line, kind, story?, scene? }` → `{ body, cursor, kind, line, names? }` Makes line `line` that kind (the element bar). The core rewrites the line so Fountain reads it as that kind on its own: a Latin character name in capitals, a non-Latin one forced with `@` (stripped again by `story.lines`); an aside wrapped in `( )`; a transition in capitals ending `TO:` or forced with `>`; action forced with `!` only where it would otherwise read as something else. `cursor` is the end of that line. - `story.names { story?, scene?, prefix?, before? }` → `{ names: [ string ] }` (`before`: how many of the scene's lines stand above the line typed) Names to offer on a character line, filtered by `prefix` (case-insensitive, any script). The scene's other speaker first, then the scene's speakers by how lately they spoke, then the story's characters by how much they speak. - `story.scene.write { id, body?, heading?, setting?, place?, time?, summary?, status? }` → `{ scene: { id, heading, setting, place, time, words, pages, minutes, speakers }, made: { characters: [ id ], places: [ id ] } }` The one write for a scene. Send what changed: the writer sends `body` as the person types (debounced; every keystroke is kept local first, no Save button). `heading` typed whole sets `setting`, `place` and `time` from it; `setting`/`place`/`time` (place by NAME, a string) set the heading. A place or a speaker the story has no row for is made, for that story only, and listed in `made`. Derived figures are rewritten. The writer MUST keep the body through this method (not `records.update`), or the scene's figures, speakers and the story's characters fall behind. - `story.scene.new { story?, after? }` → `{ scene, position }` A new empty scene (status `draft`, setting `INT.`) after scene `after`, or at the end of `story` (the current story when neither is given). Positions after it move down by one. - `story.scene.move { id, by }` → `{ positions: [ { id, position } ] }` `by` is `-1` (Move earlier) or `1` (Move later). Or `{ id, to }` with a 1-based position. - `story.scene.duplicate { id }` → `{ scene }`: a copy right after it. - `story.scene.near { id }` → `{ previous: id | null, next: id | null, number, of }`: the writer's previous/next and "scene 3 of 12". - `story.rename { table, id, name }` → `{ scenes }`: `table` is `story_character` or `story_place`. Renames the row and the name through the story's scene bodies (character lines) or headings (places); answers how many scenes changed. Removing a row never removes text. ## Cards - `story.write { id }` (a scene; the scene table's Write command) → `{ platform_action: "open_writer", scene }`: a scene card's tap opens the writer on it (shell.deck opens `writer` for a table whose Write command is `story.write` on `$id`). - `story.card { id }` (a scene) → `{ number, heading, summary | null, opening: [ { kind, text } ], length, status, status_word }` `scene.card`'s words: the scene number in story order, its heading, its summary or else its first lines (at most 4), `length` said in words ("under a page", "2 pages", from `length.*`). - `story.chat { id }` (a scene) → `{ speakers: [ name ], two_hander: bool, items: [ { kind: "speech", who, side: "left" | "right" | null, aside: string | null, text } | { kind: "action" | "transition", text } ] }` `scene.chat`: speech as bubbles; for a two-hander `side` puts the first speaker left and the other right; otherwise `side` is null and names stand over every bubble. - Character, place, story and note cards read their rows' columns (`role`, `want`, `scenes`, `lines`, `logline`, `pages`, `language`). ## The script - `story.script { story?, paper?, region? }` → ``` { paper: { name: "a4" | "letter", width_pt, height_pt, margins_pt: { top, bottom, left, right } }, title_page: { title, credit, author, lines: [ string ] }, pages: [ { number, lines: [ { kind, text, indent, scene?: { id, number } } ] } ], scenes: [ { id, number, page } ] } ``` The whole story laid out for the reader and the PDF, by screenplay rules: Courier 12 pt, 6 lines an inch, about 55 lines a page; `indent` is in characters from the left margin (action 0, character 22, dialogue 10, parenthetical 16, transition right-aligned: its indent makes it end at the right margin); every line is already wrapped (action 60 characters, dialogue 35, parenthetical 25). A row's `kind` is an element kind, `blank`, or `more` for `(MORE)`; a continued speech's name row reads `NAME (CONT'D)`. Headings carry their scene number on both sides (`scene.number`). A speech broken across a page ends `(MORE)` and resumes with the name and `(CONT'D)`; a name, heading or aside is never left alone at the foot of a page. `paper` is `a4` or `letter`; without it, `region` (the phone's ISO country) picks Letter for US, CA, PH, MX, CL, CO, VE, else A4. The PDF is drawn by the platform from these pages (`PdfDocument` / `UIGraphicsPDFRenderer`), Courier Prime with the platform's font fallback for other scripts. A 120-page story answers in under a second. ## Figures - `story.figures { heading, body, names? }` → `{ words, pages, minutes, speakers, speeches }`: pure, for a preview; the stored figures are written by `story.scene.write`. ## Out of the door and back - `story.fountain.write { story }` → `{ text, name }`: the whole story as Fountain (title page keys Title, Credit, Author, Draft date; each scene its heading, then its body), `name` the file name `.fountain`. Reading what this writes gives the same story back, byte for byte. - `story.fountain.read { text, name? }` → `{ story, scenes, characters, places }`: a new story from a Fountain file (`.fountain` or `.txt`): title page keys, scenes split on headings, characters and places made; the story is made current. - `story.copy { story, language }` → `{ story }`: a whole copy linked by `translation_of`, its language set, scenes, characters, places and notes copied, made current. Stories' held act Make a copy to translate. ## The four screens `story.list`, `story.scenes`, `story.interview`, `story.scene` and `story.draft` answer the fan, the scene list, Your story, a scene and its draft page whole; `story.scene.add`, `.place`, `.remove`, `story.interval`, `story.sequence.*`, `story.line.*`, `story.answer`, `story.skip` and `story.character.add` write them (`crates/cotton-ffi/src/phone/story_desk.rs`). A scene written line by line keeps `story_line` rows and its `body` from them; `lines_sum` names the body the rows were kept for, and a body written elsewhere wins over them. Order keys (`position`) may be fractions. The questions, the time-of-day dots and the tints are data (`crates/cotton-core/kits/story/writing.json`). ## MCP (the Worker) An assistant lists and reads the five tables with the generic record tools (list_records, get_record_by_table, search_records), adds characters and notes with create_record, and uses Story's own verbs where a generic write cannot keep the rules (`apps/business/src/mcp/tools/story.ts`): `story_scene_write` (as `story.scene.write`; without `id` it makes the scene, at the end of `story` or after `after`), `story_scene_move`, `story_copy`, `story_fountain_write` (read a whole story) and `story_fountain_read`. A scene's figures are readonly to a token: only these verbs write them. Story is sealed, so every verb works inside the owner's window (request_unlock). The tool's `instructions` tell an assistant how a body is written.