diff --git a/backend/app/routers/adventures.py b/backend/app/routers/adventures.py index 8a6d795..a58a797 100644 --- a/backend/app/routers/adventures.py +++ b/backend/app/routers/adventures.py @@ -46,8 +46,10 @@ ACTION_LIST_COLUMNS = ( models.Action.variant_index, # SP9: the pager's key. Deferred, it would be a lazy load per row — a query # behind every message on the page, which is the whole thing `load_only` - # is here to stop. + # is here to stop. `branch_id` rides along for the same reason: the pager + # reads it to tell a local step from a branch switch. models.Action.parent_id, + models.Action.branch_id, models.Action.created_at, ) @@ -1014,6 +1016,7 @@ def list_variants( index=i, text=row.text, reasoning=row.reasoning, + branch_id=row.branch_id, created_at=row.created_at.isoformat() if row.created_at else None, active=row.live, ) diff --git a/backend/app/schemas.py b/backend/app/schemas.py index 7a47785..268e65e 100644 --- a/backend/app/schemas.py +++ b/backend/app/schemas.py @@ -200,6 +200,11 @@ class ActionOut(ORMModel): # pre-SP9 pair and SP8 drops them. take_count: int = 1 take_index: int = 0 + # Which line this node is on, so the pager can tell the two kinds of step + # apart without asking the server first: a take on this branch is a leaf + # with nothing under it, and showing it is a local matter; a take on another + # branch has a story of its own, and going there is a branch switch. + branch_id: int | None = None created_at: datetime @@ -211,6 +216,9 @@ class VariantOut(BaseModel): index: int text: str reasoning: str | None = None + # See ActionOut.branch_id: it decides whether choosing this take is a local + # step or a branch switch. + branch_id: int | None = None created_at: str | None = None active: bool = False diff --git a/frontend/src/api.js b/frontend/src/api.js index e3f78de..49bf762 100644 --- a/frontend/src/api.js +++ b/frontend/src/api.js @@ -105,10 +105,15 @@ export const api = { // attempts themselves are fetched when the reader actually pages through. listVariants: (advId, actionId) => request(`/adventures/${advId}/actions/${actionId}/variants`), - selectVariant: (advId, actionId, index) => - request(`/adventures/${advId}/actions/${actionId}/variant`, { - method: 'POST', body: JSON.stringify({ index }), - }), + // The same endpoint under the name the pager uses. "Take" is what the UI + // calls one of these now, and the vocabulary is worth keeping straight — + // `variant` belongs to the pre-tree pair of columns SP8 drops. + listTakes: (advId, actionId) => + request(`/adventures/${advId}/actions/${actionId}/variants`), + // No `selectVariant` / `forkFromAttempt` here any more. Both endpoints still + // exist and are tested, but the pager needs neither: stepping between takes + // tells the server nothing, and what used to be "take this path" is now + // whatever the reader writes next, carried by `after_id` on the turn itself. // The story tree (Phase 14). One request draws the whole shape however many // forks there are. The three that change it answer with the story as it now @@ -122,11 +127,18 @@ export const api = { }), deleteBranch: (advId, branchId) => request(`/adventures/${advId}/branches/${branchId}`, { method: 'DELETE' }), - // Take the story down one attempt. A fork only when it has to be: while the - // attempts are still at the tip they are leaves, and the server switches. - forkFromAttempt: (advId, actionId) => - request(`/adventures/${advId}/actions/${actionId}/fork`, { method: 'POST' }), + // Play a turn again, differently (SP9). An AI turn regenerates; a player's + // own takes the text given. Streams, because it is a turn like any other. + // + // Reaches any turn, not only the newest — which is the whole difference from + // `retry`, and the reason the pager can offer this on every message. + addTake: (advId, actionId, text, handlers, signal) => + streamSSE(`/adventures/${advId}/actions/${actionId}/takes`, { text }, handlers, signal), + // `afterId` names the take the turn is played after. Omitted it means the + // tip, which is every ordinary turn. Naming a take the story moved past is + // what forks a branch — stepping between takes to read them does not, and + // the server is never told about it. sendAction: (advId, payload, handlers, signal) => streamSSE(`/adventures/${advId}/actions`, payload, handlers, signal), retry: (advId, handlers, signal) => streamSSE(`/adventures/${advId}/retry`, {}, handlers, signal), diff --git a/frontend/src/index.css b/frontend/src/index.css index cc40de0..e7ae65f 100644 --- a/frontend/src/index.css +++ b/frontend/src/index.css @@ -426,51 +426,50 @@ button:disabled { opacity: 0.45; cursor: default; transform: none; box-shadow: n } .story .action-tools button:hover { color: var(--text); } -/* The attempts at one AI beat. Stays quiet until hovered — it's a footnote on - the message, not part of the prose. */ -.attempts { +/* The takes of one turn: ‹ 2/4 ›. Stays quiet until hovered — it's a footnote + on the message, not part of the prose. + + Small and unemphatic on purpose. Stepping through takes reads them and does + nothing else, so it should not carry the visual weight of a decision; the + decision is made by writing, which happens in the composer below. */ +.take-pager { display: flex; align-items: center; - gap: 6px; - flex-wrap: wrap; + gap: 2px; margin-top: 6px; font-family: var(--font-ui); opacity: 0.45; transition: opacity 0.15s; } -.story .action:hover .attempts, -.attempts:focus-within { opacity: 1; } -.attempt-chip { - padding: 3px 10px; - font-size: 0.73rem; - line-height: 1.5; - border-radius: 999px; +.story .action:hover .take-pager, +.take-pager:focus-within { opacity: 1; } +.take-pager button { + width: 20px; + height: 20px; + padding: 0; + font-size: 0.9rem; + line-height: 1; color: var(--text-dim); background: transparent; - border: 1px solid var(--border); + border: 1px solid transparent; + border-radius: 4px; } -.attempt-chip:hover:not(:disabled) { color: var(--text); border-color: var(--border-bright); } -.attempt-chip[aria-pressed="true"] { - color: var(--bg); - background: var(--accent); - border-color: var(--accent); - font-weight: 600; +.take-pager button:hover:not(:disabled) { + color: var(--text); + border-color: var(--border-bright); } -.attempt-chip:disabled { opacity: 0.35; cursor: default; } -/* Dashed until hovered: taking a path the story moved past creates a branch, - so it should not look like the same weight of click as browsing one. */ -.take-path { - padding: 3px 10px; - font-size: 0.73rem; - line-height: 1.5; - color: var(--accent); - background: transparent; - border: 1px dashed var(--accent-dim); +.take-pager button:disabled { opacity: 0.3; cursor: default; } +.take-count { + min-width: 30px; + text-align: center; + font-size: 0.72rem; + font-variant-numeric: tabular-nums; + color: var(--text-dim); } -.take-path:hover:not(:disabled) { color: var(--accent-bright); border-style: solid; } -.take-path:disabled { opacity: 0.35; cursor: default; } -.attempt-note { - margin-left: 2px; +/* Only while a take that isn't the live one is on screen: it says what the + next thing typed will do, because that is the click that forks. */ +.take-note { + margin-left: 6px; font-size: 0.72rem; font-style: italic; color: var(--accent-dim); diff --git a/frontend/src/pages/Play.jsx b/frontend/src/pages/Play.jsx index d2d1d9b..6666276 100644 --- a/frontend/src/pages/Play.jsx +++ b/frontend/src/pages/Play.jsx @@ -952,48 +952,59 @@ function WorldStateDrawer({ advId, refreshKey }) { ) } -// The attempts at one turn, and the way onto one the story left behind. +// The takes of one turn: ‹ 2/4 ›, and nothing else. // -// This replaces the ‹ 2/3 › pager, and the reason is not that chips look -// better: a pager can only step between attempts, and stepping has nothing to -// say about the thing the tree makes possible — taking a path the story moved -// past *and keeping both*. Every attempt is its own node now (SP4), so a chip -// is a node, and "take this path" is a fork (SP5). +// SP7 shipped chips instead, on the grounds that a pager can only step between +// takes while a chip could also offer "take this path". Driving it by hand said +// otherwise. The chip meant two different things depending on where the reader +// was standing — a real switch at the tip, a preview needing a second button +// above it — and two meanings in one control is what made the tree unusable. // -// Two cases behind one control. While the turn is the tip its attempts are -// still leaves, so choosing one is a switch and the server restores the state -// that attempt produced. Once the story has moved past, choosing one is a -// local preview — the turns after it were written as a continuation of -// whatever is live — and taking it forks a branch. -function AttemptChips({ advId, action, isLast, busy, preview, onPreview, onSwitched, onForked, onError }) { - const [variants, setVariants] = useState(null) +// So the pager comes back, and stepping is all it does. Stepping is free: it +// tells the server nothing, because reading a take is not a decision. The +// decision is made by *writing* below one, and that is where the branch is +// created (SP9, `after_id`). +// +// One step still reaches the server, and it is not a fork either. A take that +// has a story of its own lives on its own branch, so going there is a branch +// switch — the story below has to change, and only the server can say to what. +// A take on this branch is a leaf by construction: whatever was played after +// this turn was played after the take that is live, so a take that is not live +// has nothing under it and the transcript simply ends there. +function TakePager({ advId, action, busy, preview, onPreview, onSwitchedBranch, onError }) { + const [takes, setTakes] = useState(null) const [loading, setLoading] = useState(false) - const count = action.variant_count - const live = action.variant_index + const count = action.take_count + const live = action.take_index const current = preview ? preview.index : live - async function show(next) { - if (next === current || loading || busy) return + async function step(delta) { + const next = current + delta + if (next < 0 || next >= count || loading || busy) return setLoading(true) try { - if (isLast) { + // Fetched once per message, then cached — walking back and forth through + // the takes should not re-hit the server for a list that has not changed. + const list = takes || await api.listTakes(advId, action.id) + if (!takes) setTakes(list) + const target = list[next] + if (target.branch_id !== action.branch_id) { + // It has a story of its own. Only the server knows what is under it. + onPreview(null) + onSwitchedBranch(await api.switchBranch(advId, target.branch_id)) + } else if (next === live) { onPreview(null) - onSwitched(await api.selectVariant(advId, action.id, next)) } else { - // Fetched once per message, then cached — moving back and forth - // between attempts shouldn't re-hit the server. - const list = variants || await api.listVariants(advId, action.id) - if (!variants) setVariants(list) - onPreview(next === live ? null : { + onPreview({ actionId: action.id, index: next, - // The attempt's own node id. A fork is addressed by the node being - // taken, never by its ordinal — the group renumbers whenever an - // attempt is added, and an ordinal held across that points at a - // different take. - attemptId: list[next].id, - text: list[next].text, - reasoning: list[next].reasoning, + // The take's own node id, never its ordinal: the group renumbers + // whenever a take is added, and an ordinal held across that points + // at a different one. This is what `after_id` is given if the reader + // writes from here. + takeId: target.id, + text: target.text, + reasoning: target.reasoning, }) } } catch (err) { @@ -1003,45 +1014,16 @@ function AttemptChips({ advId, action, isLast, busy, preview, onPreview, onSwitc } } - async function take(attemptId) { - if (loading || busy) return - setLoading(true) - try { - const page = await api.forkFromAttempt(advId, attemptId) - onPreview(null) - onForked(page) - } catch (err) { - onError(err.message) - } finally { - setLoading(false) - } - } - + if (count < 2) return null return ( -