Public: anyone can read this; the crew can change it.
Talk's pane address bar is a switcher: a repository, a version, a path; Back returns, Diff is this conversation's #16367
Check · Review · Land · Live done — Landed and live in production.
talk/pane-bar-a → main 0 commits
16 files
changed +1071
−81
docs/design/areas/talk.md +4
| @@ -108,6 +108,10 @@ Prior art: Claude and ChatGPT (one centred column, the person's words in a quiet | ||
| 108 | 108 | |
| 109 | 109 | - `.talk-pane-bar`: the pane's bar: Preview, Code and Diff as a `.seg`, the address, Reload and Open on its own. |
| 110 | 110 | - `.talk-addr`: the address of what runs, a muted pill: which environment (Production, Pull #N) and its host; hidden under 30rem. |
| 111 | - `.talk-addr-pick`: the address bar as a switcher, a `<form method="get">` in the same muted pill: a repository `<select>`, a version `<select>` (Live, each open pull's preview), a path `<input>`; with the script a pick swaps the pane in place, without it the form's `.talk-addr-go` button (hidden under `:root.js`) sends it. | |
| 112 | - `.talk-pane-info`: one muted line under the bar: for a pull's preview who made it, its number and title, checks and review, and a link to the pull; for Live the default branch and what last landed (`paneInfo`, the terminal prints the same). | |
| 113 | - `.talk-pane-fell`: one line when a picked pull's preview is gone: "#N landed: Live has it now." | |
| 114 | - `.talk-diff-change`: in Diff, one pull this conversation's work led to: a `<details>` whose summary links the pull and says landed, open or closed, the newest open. | |
| 111 | 115 | - `.talk-reload`: Reload the preview, an icon button drawn `js-only`. |
| 112 | 116 | - `.talk-try`: the agent's one line of what to try in the frame (`pane`), above the stage: plain small muted text (`--muted-fg`), no box; text only, never a link. |
| 113 | 117 | - `.talk-stage`: the frame's surface, on the card colour with a hairline; `data-busy` dims the frame while it loads again. |
docs/design/size-budget.json +2 −2
| @@ -68,8 +68,8 @@ | ||
| 68 | 68 | "css": 48640 |
| 69 | 69 | }, |
| 70 | 70 | "/talk": { |
| 71 | "js": 39936, | |
| 72 | "css": 55808 | |
| 71 | "js": 40704, | |
| 72 | "css": 56064 | |
| 73 | 73 | }, |
| 74 | 74 | "/:owner/:name/pulls/:number": { |
| 75 | 75 | "js": 32000, |
docs/features/talk.md +11 −1
| @@ -136,7 +136,7 @@ A reply is drawn by the README renderer and then, for Talk alone, by `linkTalk` | ||
| 136 | 136 | |
| 137 | 137 | An agent reply is Markdown, drawn on the server by the same renderer as a README: fenced code is highlighted, tables and links render, and everything else is escaped (a `<script>` in a reply shows as text). A table wider than the column scrolls inside it; the page does not. The reply sits in the column like a person's turn, with none of the README's padding or line measure. |
| 138 | 138 | |
| 139 | A reply grows in one live element while it is written (see "Live") and is then stored as one finished turn; the page only appends a turn whose id it has not drawn (a repeat is ignored). Turns already on the page are not touched, so nothing above a new reply moves, and the page follows the end only if you were already there (otherwise "Latest" appears). An unfinished fence in a stored reply is closed for drawing. The page's own bytes are held by a test (`test/talk-page.test.ts`): the stylesheet under 18,100 B, the script under 50,400 B, and a page with a code reply under 53,000 B (its own page: one question and a code reply; the widgets' rules ride every page, so a chart drawn in a live reply is styled at once). | |
| 139 | A reply grows in one live element while it is written (see "Live") and is then stored as one finished turn; the page only appends a turn whose id it has not drawn (a repeat is ignored). Turns already on the page are not touched, so nothing above a new reply moves, and the page follows the end only if you were already there (otherwise "Latest" appears). An unfinished fence in a stored reply is closed for drawing. The page's own bytes are held by a test (`test/talk-page.test.ts`): the stylesheet under 18,100 B, the script under 52,900 B, and a page with a code reply under 54,000 B (its own page: one question and a code reply; the widgets' rules ride every page, so a chart drawn in a live reply is styled at once). | |
| 140 | 140 | |
| 141 | 141 | ## Following |
| 142 | 142 | |
| @@ -187,6 +187,16 @@ The terminal's Talk screen reads the same file: `d` on the list, or `d` or `x` i | ||
| 187 | 187 | |
| 188 | 188 | The preview frame opens with a sign-in link that lasts a few minutes, and every reload asks the forge for a fresh one. If the frame ever loads an old link, it says "The preview opened here has timed out." Press Reload above the preview to open it again. |
| 189 | 189 | |
| 190 | ## The pane's address bar | |
| 191 | ||
| 192 | The address over the preview is three pickers: the repository (the conversation's, then your own apps, only ones you may read), the version (Live, or an open pull's preview), and a path on the app. A pick changes the pane in place and leaves the chat where it was. Each pick is an entry in the browser's history, so Back returns to the one before, and a reload keeps it because it is in the address (`?repo=&show=&path=`). Opening the conversation fresh shows its own repository and newest version, drawn right the first time. If the pull you picked lands or closes, its preview is torn down and the pane shows Live, with one line saying so. Without the script the pickers are a form with a Go button; a version the newly chosen repository does not have falls to Live. | |
| 193 | ||
| 194 | In the terminal, the Talk screen's pane (`p`) picks the same way: `o` picks the repository, `w` the version (Live or an open pull's preview), `l` a path on the app, each from what the page's bar offers, and the pane prints the preview's address there. Its Diff is the page's: the build's branch, the pull picked, or the pulls this conversation led to, each with its change. | |
| 195 | ||
| 196 | Under the bar, one line says what you are looking at: for a pull, who made it, its number and title, its checks and its review, with a link to the pull; for Live, the default branch and what last landed. The terminal's pane prints the same line. | |
| 197 | ||
| 198 | Diff shows what this conversation's work changed: the pulls its receipts led to, open or landed, newest first, each with its change. With a pull picked in the bar, Diff shows that pull alone. A conversation that filed nothing on the repository asks you to pick a pull; it never shows the repository's newest pull on its own. | |
| 199 | ||
| 190 | 200 | ## Display (#15734) |
| 191 | 201 | |
| 192 | 202 | Four preferences of the person's own, at the end of the conversation menu (⋯ → Display), one plain form that works without the script (`POST /talk/display`, signed in, same origin): spacing (comfortable or compact: the spacing scale only, never the type size), motion (as the system says, or reduced: Talk's arrivals, the head's slide and the shimmer stop whatever the system says), what Enter does (sends, or adds a line with Ctrl or Cmd+Enter sending) and whether a reply's steps start folded or open. They are the person's `user_setting` rows (`talk.density`, `talk.motion`, `talk.enter`, `talk.steps`, read by `talkDisplay` in `src/core/settings.ts`), applied by the server so the first paint is right; a value that is not one of a preference's own choices is never kept. No theme builder, no colours. The terminal honours the same steps choice: a conversation opened in `forge talk` shows a run of steps as one line ("3 steps") or each step, and `v` there flips the choice for both places, while `D` opens Talk display to set all four (docs/features/talk-display.md); spacing, motion and Enter are the page's own (the terminal has its own rows, no animation, and its `s` prompt always sends on Enter). |
src/actions/talk_workspace.ts +166 −13
| @@ -7,8 +7,10 @@ | ||
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | 9 | import type { App } from "../app.ts"; |
| 10 | import { previewEnv } from "../apps/data.ts"; | |
| 11 | import { getRepo, type Repo } from "../core/repos.ts"; | |
| 10 | import { previewEnv, type EnvName } from "../apps/data.ts"; | |
| 11 | import { appsForRepos } from "../core/apps.ts"; | |
| 12 | import { getRepo, listReposWritableTo, type Repo } from "../core/repos.ts"; | |
| 13 | import { appsRuntime, envsFor } from "../apps/runtime.ts"; | |
| 12 | 14 | import type { User } from "../core/users.ts"; |
| 13 | 15 | import { requireReader } from "./access.ts"; |
| 14 | 16 | import { appPreviews, openEnvUrl } from "./apps.ts"; |
| @@ -18,15 +20,29 @@ export interface Apps { | ||
| 18 | 20 | readonly newestPull: (repo: Repo) => number | undefined; |
| 19 | 21 | /** `path` is one `panePath` let through: the grant a signed-in person enters by lands there. */ |
| 20 | 22 | readonly urlOf: (repo: Repo, env: string, path?: string) => string | undefined; |
| 23 | /** Every open pull whose preview is up, newest first; without it, the newest is the only one. */ | |
| 24 | readonly previews?: (repo: Repo) => readonly number[]; | |
| 21 | 25 | } |
| 22 | 26 | |
| 23 | export const realApps = (app: App, user: User): Apps => ({ | |
| 24 | newestPull: (repo) => appPreviews(app, user, repo)?.[0]?.number, | |
| 25 | urlOf: (repo, env, path = "/") => { | |
| 26 | const opened = openEnvUrl(app, user, repo, env, path); | |
| 27 | return opened.ok ? opened.value : undefined; | |
| 28 | }, | |
| 29 | }); | |
| 27 | export const realApps = (app: App, user: User): Apps => { | |
| 28 | // One read of a repository's previews however often the workspace asks. | |
| 29 | const seen = new Map<string, readonly number[]>(); | |
| 30 | const previews = (repo: Repo): readonly number[] => { | |
| 31 | const known = seen.get(repo.id); | |
| 32 | if (known !== undefined) return known; | |
| 33 | const read = (appPreviews(app, user, repo) ?? []).map((p) => p.number); | |
| 34 | seen.set(repo.id, read); | |
| 35 | return read; | |
| 36 | }; | |
| 37 | return { | |
| 38 | newestPull: (repo) => previews(repo)[0], | |
| 39 | previews, | |
| 40 | urlOf: (repo, env, path = "/") => { | |
| 41 | const opened = openEnvUrl(app, user, repo, env, path); | |
| 42 | return opened.ok ? opened.value : undefined; | |
| 43 | }, | |
| 44 | }; | |
| 45 | }; | |
| 30 | 46 | |
| 31 | 47 | export interface Workspace { |
| 32 | 48 | readonly name: string; |
| @@ -37,6 +53,8 @@ export interface Workspace { | ||
| 37 | 53 | /** The running app's address, and its origin alone (what `frame-src` may name); both or neither. */ |
| 38 | 54 | readonly url: string | undefined; |
| 39 | 55 | readonly origin: string | undefined; |
| 56 | /** The pull the person picked whose preview no longer answers (it landed or closed): Live is framed. */ | |
| 57 | readonly fell?: number; | |
| 40 | 58 | } |
| 41 | 59 | |
| 42 | 60 | /** Scheme, host and port, nothing else: no userinfo, so `https://[email protected]/` has none. */ |
| @@ -49,7 +67,7 @@ export const originOf = (url: string): string | undefined => ORIGIN.exec(url)?.[ | ||
| 49 | 67 | * Pointed (`pane`): `live` frames production even while a pull's preview is open, and the address |
| 50 | 68 | * is at `path` when `panePath` lets it through, else at the app's root. |
| 51 | 69 | */ |
| 52 | export function workspaceOf(app: App, user: User, name: string, apps: Apps = realApps(app, user), at: { readonly version?: PaneVersion; readonly path?: string } = {}): Workspace | undefined { | |
| 70 | export function workspaceOf(app: App, user: User, name: string, apps: Apps = realApps(app, user), at: { readonly version?: PaneVersion; readonly path?: string; readonly pull?: number } = {}): Workspace | undefined { | |
| 53 | 71 | const [owner, repoName, extra] = name.split("/"); |
| 54 | 72 | if (owner === undefined || repoName === undefined || extra !== undefined) return undefined; |
| 55 | 73 | const repo = getRepo(app.db, owner, repoName); |
| @@ -58,11 +76,146 @@ export function workspaceOf(app: App, user: User, name: string, apps: Apps = rea | ||
| 58 | 76 | const path = at.path === undefined ? "/" : (panePath(at.path) ?? "/"); |
| 59 | 77 | // The newest pull's preview, else production: a pull the crew has just opened shows the app as it |
| 60 | 78 | // runs until its own preview answers, never an empty stage. Pointed at `live`, production. |
| 61 | const ahead = pull === undefined || at.version === "live" ? undefined : apps.urlOf(repo, previewEnv(pull), path); | |
| 62 | const shows = ahead === undefined ? undefined : pull; | |
| 79 | // A pull the person picked is framed by its own preview; one whose preview is gone falls to Live. | |
| 80 | const up = at.pull === undefined ? false : (apps.previews?.(repo) ?? (pull === undefined ? [] : [pull])).includes(at.pull); | |
| 81 | const picked = at.pull === undefined || !up ? undefined : apps.urlOf(repo, previewEnv(at.pull), path); | |
| 82 | const ahead = at.pull !== undefined ? picked : pull === undefined || at.version === "live" ? undefined : apps.urlOf(repo, previewEnv(pull), path); | |
| 83 | const shows = ahead === undefined ? undefined : (at.pull ?? pull); | |
| 63 | 84 | const found = ahead ?? apps.urlOf(repo, "production", path); |
| 64 | 85 | const origin = found === undefined ? undefined : originOf(found); |
| 65 | return { name, repo, pull, shows, url: origin === undefined ? undefined : found, origin }; | |
| 86 | return { name, repo, pull, shows, url: origin === undefined ? undefined : found, origin, ...(at.pull !== undefined && picked === undefined ? { fell: at.pull } : {}) }; | |
| 87 | } | |
| 88 | ||
| 89 | /** Where the person pointed the pane in its address bar: a repository, a version, a path. */ | |
| 90 | export interface PaneAt { | |
| 91 | readonly repo?: string; | |
| 92 | /** `live`, or a pull whose preview to frame. */ | |
| 93 | readonly live?: boolean; | |
| 94 | readonly pull?: number; | |
| 95 | readonly path?: string; | |
| 96 | } | |
| 97 | ||
| 98 | const REPO_FORM = /^[A-Za-z0-9_.-]{1,100}\/[A-Za-z0-9_.-]{1,100}$/; | |
| 99 | ||
| 100 | /** The address bar's choice read from a page's query: `repo`, `show` (`live` or `pull-N`), `path` (checked as `panePath`). */ | |
| 101 | export function paneAtOf(query: URLSearchParams): PaneAt { | |
| 102 | const repo = query.get("repo") ?? ""; | |
| 103 | const v = query.get("show") ?? ""; | |
| 104 | const pulled = /^pull-([1-9]\d{0,8})$/.exec(v); | |
| 105 | const path = panePath(query.get("path") ?? ""); | |
| 106 | return { | |
| 107 | ...(REPO_FORM.test(repo) ? { repo } : {}), | |
| 108 | ...(v === "live" ? { live: true } : pulled === null ? {} : { pull: Number(pulled[1]) }), | |
| 109 | ...(path === undefined || path === "/" ? {} : { path }), | |
| 110 | }; | |
| 111 | } | |
| 112 | ||
| 113 | /** The choice as a query (`repo=…&show=…&path=…`, empty parts left out), for links and fragments. */ | |
| 114 | export function paneQuery(at: PaneAt): string { | |
| 115 | const q = new URLSearchParams(); | |
| 116 | if (at.repo !== undefined) q.set("repo", at.repo); | |
| 117 | if (at.live === true) q.set("show", "live"); | |
| 118 | else if (at.pull !== undefined) q.set("show", `pull-${String(at.pull)}`); | |
| 119 | if (at.path !== undefined) q.set("path", at.path); | |
| 120 | return q.toString(); | |
| 121 | } | |
| 122 | ||
| 123 | /** At most this many repositories in the address bar's first picker, and previews per repository. */ | |
| 124 | export const PANE_REPOS = 8; | |
| 125 | export const PANE_PREVIEWS = 5; | |
| 126 | ||
| 127 | /** | |
| 128 | * What the pane's address bar offers this person: the repositories (the conversation's first, then | |
| 129 | * their own apps), each with its versions (Live, then each open pull's preview), and every origin | |
| 130 | * those versions run at: the page's `frame-src` names exactly these, so a switch frames in place. | |
| 131 | * Only repositories the person may read: the conversation's is checked, the rest are ones they write. | |
| 132 | */ | |
| 133 | export function paneOffer(app: App, user: User, current: string | undefined): PaneOffer { | |
| 134 | // No repository for the pane, no pane: nothing is read. | |
| 135 | const first = current === undefined ? undefined : workspaceRepo(app, user, current); | |
| 136 | if (first === undefined) return { repos: [], origins: [] }; | |
| 137 | const runtime = appsRuntime(app); | |
| 138 | // Their own apps, with their records, in one read each. | |
| 139 | const mine = listReposWritableTo(app.db, user, { limit: 50, withoutArchived: true }).filter((r) => r.id !== first.id); | |
| 140 | const records = appsForRepos(app.db, [first.id, ...mine.map((r) => r.id)]); | |
| 141 | const names = [first, ...mine.filter((r) => records.has(r.id))].slice(0, PANE_REPOS); | |
| 142 | // Every open pull of those repositories in one read; a sealed one only for its author here. | |
| 143 | const open = openPullTitles(app.db, names.map((r) => r.id), user.id); | |
| 144 | const origins = new Set<string>(); | |
| 145 | const repos = names.map((repo) => { | |
| 146 | const record = records.get(repo.id); | |
| 147 | // A preview is offered only while it runs, as the runtime knows it (no read); a private app's | |
| 148 | // only to someone who may enter it, as `envsFor` decides for the conversation's. | |
| 149 | const up = record === undefined ? new Set<string>() : new Set((repo.id === first.id ? envsFor(app, user, record) : (runtime?.status(record) ?? [])).map((s) => s.env as string)); | |
| 150 | const previews = (open.get(repo.id) ?? []).filter((p) => up.has(previewEnv(p.number))).slice(0, PANE_PREVIEWS); | |
| 151 | if (record !== undefined && runtime !== undefined) | |
| 152 | for (const env of ["production" as EnvName, ...previews.map((p) => previewEnv(p.number))]) { | |
| 153 | const o = originOf(runtime.urlFor(record, env)); | |
| 154 | if (o !== undefined) origins.add(o); | |
| 155 | } | |
| 156 | return { name: `${repo.ownerName}/${repo.name}`, repo, versions: [{ key: "live", label: "Live" }, ...previews.map((p) => ({ key: `pull-${String(p.number)}`, label: `#${String(p.number)} ${p.title}` }))] }; | |
| 157 | }); | |
| 158 | return { repos, origins: [...origins] }; | |
| 159 | } | |
| 160 | ||
| 161 | /** What the address bar offers: each repository with its versions, and the origins they run at. */ | |
| 162 | export interface PaneOffer { | |
| 163 | readonly repos: readonly { readonly name: string; readonly repo: Repo; readonly versions: readonly { readonly key: string; readonly label: string }[] }[]; | |
| 164 | readonly origins: readonly string[]; | |
| 165 | } | |
| 166 | ||
| 167 | /** The open pulls of these repositories by repository, newest first, in one read; a sealed one only for its author. */ | |
| 168 | function openPullTitles(db: App["db"], repoIds: readonly string[], viewerId: string): Map<string, { number: number; title: string }[]> { | |
| 169 | const out = new Map<string, { number: number; title: string }[]>(); | |
| 170 | if (repoIds.length === 0) return out; | |
| 171 | const rows = db | |
| 172 | .query<{ repo_id: string; number: number; title: string }, string[]>( | |
| 173 | `SELECT i.repo_id, i.number, i.title FROM issue i JOIN pull p ON p.issue_id = i.id | |
| 174 | WHERE i.kind = 'pull' AND i.state = 'open' AND (i.sealed = 0 OR i.author_id = ?1) | |
| 175 | AND i.repo_id IN (${repoIds.map((_, n) => `?${n + 2}`).join(",")}) ORDER BY i.number DESC`, | |
| 176 | ) | |
| 177 | .all(viewerId, ...repoIds); | |
| 178 | for (const r of rows) out.set(r.repo_id, [...(out.get(r.repo_id) ?? []), { number: r.number, title: r.title }]); | |
| 179 | return out; | |
| 180 | } | |
| 181 | ||
| 182 | /** | |
| 183 | * The address bar's choice as both doors keep it, read against what the bar offers: a repository it | |
| 184 | * does not offer is dropped (the conversation's own is shown), and so is a version the chosen | |
| 185 | * repository does not offer, falling to Live: a form sent without the script carries the old | |
| 186 | * repository's version beside a new repository. A pull of this repository that has landed or closed | |
| 187 | * is kept, so the pane can say why it shows Live. | |
| 188 | */ | |
| 189 | export function paneChoice(db: App["db"], offer: PaneOffer, at: PaneAt, worksOn: string | undefined): PaneAt { | |
| 190 | const repo = at.repo !== undefined && offer.repos.some((r) => r.name === at.repo) ? at.repo : undefined; | |
| 191 | const row = offer.repos.find((r) => r.name === (repo ?? worksOn)); | |
| 192 | const offered = at.pull !== undefined && row?.versions.some((v) => v.key === `pull-${String(at.pull)}`) === true; | |
| 193 | const ended = at.pull !== undefined && !offered && row !== undefined && db.query<{ n: number }, [string, number]>("SELECT COUNT(*) AS n FROM issue i JOIN pull p ON p.issue_id = i.id WHERE i.repo_id = ?1 AND i.number = ?2 AND (i.state = 'closed' OR p.merged_at IS NOT NULL)").get(row.repo.id, at.pull)!.n > 0; | |
| 194 | return { | |
| 195 | ...(repo === undefined ? {} : { repo }), | |
| 196 | ...(at.live === true ? { live: true } : at.pull !== undefined && (offered || ended) ? { pull: at.pull } : {}), | |
| 197 | ...(at.path === undefined ? {} : { path: at.path }), | |
| 198 | }; | |
| 199 | } | |
| 200 | ||
| 201 | /** | |
| 202 | * What `workspaceOf` is pointed at: the person's choice first (Live, or a pull, and a path), else where | |
| 203 | * the agent pointed the pane (`pane`), else the newest preview at the app's root. | |
| 204 | */ | |
| 205 | export function pointedAt(at: PaneAt, steer: PaneSteer | undefined): { readonly version?: PaneVersion; readonly pull?: number; readonly path?: string } { | |
| 206 | const path = at.path ?? steer?.path; | |
| 207 | return { | |
| 208 | ...(at.live === true ? { version: "live" as const } : at.pull !== undefined ? { pull: at.pull } : steer === undefined ? {} : { version: steer.version }), | |
| 209 | ...(path === undefined ? {} : { path }), | |
| 210 | }; | |
| 211 | } | |
| 212 | ||
| 213 | /** The repository `name` stands for, when this person may read it. */ | |
| 214 | function workspaceRepo(app: App, user: User, name: string): Repo | undefined { | |
| 215 | const [owner, repoName, extra] = name.split("/"); | |
| 216 | if (owner === undefined || repoName === undefined || extra !== undefined) return undefined; | |
| 217 | const repo = getRepo(app.db, owner, repoName); | |
| 218 | return repo === undefined || !requireReader(app, user, repo).ok ? undefined : repo; | |
| 66 | 219 | } |
| 67 | 220 | |
| 68 | 221 | /** Which of an app's versions the pane frames: the newest open pull's preview, or production. */ |
src/core/chat_build.ts +40 −2
| @@ -4,9 +4,9 @@ | ||
| 4 | 4 | * Plain rows in, plain rows out; the git side lives in `actions/build.ts`. |
| 5 | 5 | */ |
| 6 | 6 | import type { Database } from "bun:sqlite"; |
| 7 | import { diffBetween, getPull } from "./pulls.ts"; | |
| 7 | import { diffBetween, getPull, pullDiff } from "./pulls.ts"; | |
| 8 | 8 | import type { Repo } from "./repos.ts"; |
| 9 | import type { Diff } from "../git/diff.ts"; | |
| 9 | import { diffCommit, type Diff } from "../git/diff.ts"; | |
| 10 | 10 | import { refTip } from "../git/read.ts"; |
| 11 | 11 | import { repoDir } from "../git/repos.ts"; |
| 12 | 12 | |
| @@ -130,3 +130,41 @@ export async function branchDiff( | ||
| 130 | 130 | const d = await diffBetween(dir, base, head); |
| 131 | 131 | return d.failed ? undefined : { diff: d.diff, from: d.from, head, branch }; |
| 132 | 132 | } |
| 133 | ||
| 134 | /** One pull a conversation led to, with its change: a landed one as it landed, an open one as it stands. */ | |
| 135 | export interface PullChange { | |
| 136 | readonly number: number; | |
| 137 | readonly title: string; | |
| 138 | readonly state: "landed" | "open" | "closed"; | |
| 139 | /** Where its files are read: the commit it landed as, else its branch. */ | |
| 140 | readonly at: string; | |
| 141 | /** The change, and the two commits it is between (for syntax); undefined when git could not read it. */ | |
| 142 | readonly diff: { readonly diff: Diff; readonly from: string; readonly to: string } | undefined; | |
| 143 | } | |
| 144 | ||
| 145 | /** | |
| 146 | * The changes of these pulls (`conversationPulls`), newest first, at most `max`: what a conversation's | |
| 147 | * Diff shows on the page and in the terminal alike. | |
| 148 | */ | |
| 149 | export async function pullChanges(db: Database, reposRoot: string, repo: Repo, numbers: readonly number[], max = 5): Promise<PullChange[]> { | |
| 150 | const dir = repoDir(reposRoot, repo.ownerName, repo.name); | |
| 151 | const out: PullChange[] = []; | |
| 152 | for (const n of numbers.slice(0, max)) { | |
| 153 | const pull = getPull(db, repo.id, n); | |
| 154 | if (pull === undefined) continue; | |
| 155 | const landed = pull.mergedOid; | |
| 156 | let diff: PullChange["diff"]; | |
| 157 | if (landed !== null) { | |
| 158 | const one = await diffCommit(dir, landed); | |
| 159 | diff = one.ok ? { diff: one.value, from: `${landed}^`, to: landed } : undefined; | |
| 160 | } else { | |
| 161 | const open = await pullDiff(dir, pull).catch(() => undefined); | |
| 162 | diff = open === undefined ? undefined : { diff: open.diff, from: open.from, to: open.status.headOid }; | |
| 163 | } | |
| 164 | out.push({ number: pull.number, title: pull.title, state: landed !== null ? "landed" : pull.state === "closed" ? "closed" : "open", at: landed ?? pull.headBranch, diff }); | |
| 165 | } | |
| 166 | return out; | |
| 167 | } | |
| 168 | ||
| 169 | /** What a conversation's Diff says when it shows no pull: it filed nothing here, or nothing it filed has a pull yet. */ | |
| 170 | export const mineEmpty = (asked: boolean): string => (asked ? "This conversation has not changed anything yet." : "Pick a pull above to see its change."); |
src/core/talk_steps.ts +11
| @@ -202,6 +202,17 @@ const receiptHref = (text: string): string | undefined => { | ||
| 202 | 202 | } |
| 203 | 203 | }; |
| 204 | 204 | |
| 205 | /** The issues on `repo` this conversation's receipts name (filed, or a new app's first), each once. */ | |
| 206 | export function receiptIssues(turns: readonly Said[], repo: string): number[] { | |
| 207 | const out: number[] = []; | |
| 208 | for (const t of turns) { | |
| 209 | if (t.role !== "tool") continue; | |
| 210 | const m = /^\/([^/\s]+\/[^/\s]+)\/issues\/([1-9]\d{0,8})$/.exec(receiptHref(t.text) ?? ""); | |
| 211 | if (m !== null && m[1] === repo && !out.includes(Number(m[2]))) out.push(Number(m[2])); | |
| 212 | } | |
| 213 | return out; | |
| 214 | } | |
| 215 | ||
| 205 | 216 | const PATH = /^(?!\/)(?!.*(?:^|\/)\.\.(?:\/|$))[^\0\n]{1,300}$/; |
| 206 | 217 | |
| 207 | 218 | /** The files the calls on `repo` read or edited, newest first, each once, at most `max`. */ |
src/core/talk_work.ts +72
| @@ -13,6 +13,10 @@ import { listTurns } from "./conversations.ts"; | ||
| 13 | 13 | import { canAdmin, canRead, getRepo, getRepoById, type Repo } from "./repos.ts"; |
| 14 | 14 | import { mayView } from "./sealed.ts"; |
| 15 | 15 | import { getUserById, type User } from "./users.ts"; |
| 16 | import { getPull } from "./pulls.ts"; | |
| 17 | import { closesFromBody } from "./pulls.ts"; | |
| 18 | import { reviewSummary } from "./reviews.ts"; | |
| 19 | import { checkSummary } from "./runs.ts"; | |
| 16 | 20 | import { callState } from "./talk_steps.ts"; |
| 17 | 21 | import { readRunLogTail } from "../runs/log.ts"; |
| 18 | 22 | |
| @@ -210,6 +214,74 @@ const forMinutes = (at: string | null, now: number): string => | ||
| 210 | 214 | ? "" |
| 211 | 215 | : ` for ${Math.max(1, Math.round((now - Date.parse(at)) / 60_000))} min`; |
| 212 | 216 | |
| 217 | /** | |
| 218 | * Why the pane shows Live when the person picked a pull: it landed, or it was closed; undefined for | |
| 219 | * any other pull. Both doors say it in these words. | |
| 220 | */ | |
| 221 | export function paneFell(db: Database, repoId: string, n: number | undefined): string | undefined { | |
| 222 | const pull = n === undefined ? undefined : getPull(db, repoId, n); | |
| 223 | if (pull === undefined) return undefined; | |
| 224 | if (pull.mergedAt !== null) return `#${String(pull.number)} landed: Live has it now.`; | |
| 225 | return pull.state === "closed" ? `#${String(pull.number)} was closed: Live is shown.` : undefined; | |
| 226 | } | |
| 227 | ||
| 228 | /** A pull's checks in words: the Diff tab's head and the pane's line say them alike. */ | |
| 229 | export const PULL_CHECKS: Readonly<Record<string, string>> = { | |
| 230 | ok: "checks passing", | |
| 231 | failed: "checks failing", | |
| 232 | running: "checks running", | |
| 233 | queued: "checks queued", | |
| 234 | cancelled: "checks cancelled", | |
| 235 | }; | |
| 236 | ||
| 237 | /** | |
| 238 | * The line under the pane's address bar, for both doors: for a pull's preview, who made it, its | |
| 239 | * number and title, its checks and its review; for Live, the default branch and the last pull | |
| 240 | * landed on it. Plain text: the page escapes it, the terminal prints it. | |
| 241 | */ | |
| 242 | export function paneInfo(db: Database, repo: { readonly id: string; readonly defaultBranch: string }, shows: number | undefined): string { | |
| 243 | if (shows === undefined) { | |
| 244 | const last = db | |
| 245 | .query<{ number: number; title: string }, [string]>("SELECT i.number, i.title FROM pull p JOIN issue i ON i.id = p.issue_id WHERE i.repo_id = ?1 AND p.merged_at IS NOT NULL ORDER BY p.merged_at DESC LIMIT 1") | |
| 246 | .get(repo.id); | |
| 247 | return `Live · ${repo.defaultBranch} · ${last === null ? "nothing landed yet" : `last landed #${String(last.number)} ${last.title}`}`; | |
| 248 | } | |
| 249 | const pull = getPull(db, repo.id, shows); | |
| 250 | if (pull === undefined) return `#${String(shows)}`; | |
| 251 | const who = getUserById(db, pull.authorId)?.username; | |
| 252 | const checks = PULL_CHECKS[checkSummary(db, pull.id, pull.headOid).state] ?? "no checks yet"; | |
| 253 | const review = reviewSummary(db, pull.id, pull.headOid, pull.authorId); | |
| 254 | const verdict = review.changesRequested > 0 ? "changes asked" : review.approvals > 0 ? "approved" : "not reviewed yet"; | |
| 255 | return [who, `#${String(pull.number)} ${pull.title}`, checks, verdict].filter((s) => s !== undefined).join(" · "); | |
| 256 | } | |
| 257 | ||
| 258 | /** | |
| 259 | * The pulls a conversation's work led to on a repository, newest first: for each issue its receipts | |
| 260 | * name, the pulls the crew's runs on it pushed (by their branch) and the pulls that say they close it. | |
| 261 | */ | |
| 262 | export function conversationPulls(db: Database, repoId: string, issues: readonly number[]): number[] { | |
| 263 | if (issues.length === 0) return []; | |
| 264 | const list = issues.slice(0, 50); | |
| 265 | // One read: the pulls on the branches the crew's runs on these issues pushed, and the pulls whose | |
| 266 | // description names one of them (kept only when it says it closes it, read as `closesFromBody`). | |
| 267 | const rows = db | |
| 268 | .query<{ number: number; body: string; ran: number }, (string | number)[]>( | |
| 269 | `SELECT pi.number, pi.body, | |
| 270 | p.head_branch IN (SELECT r.branch FROM run r JOIN issue ii ON ii.id = r.issue_id | |
| 271 | WHERE ii.repo_id = ?1 AND r.kind = 'agent' AND r.branch != '' | |
| 272 | AND ii.number IN (${list.map((_, i) => `?${i + 2}`).join(",")})) AS ran | |
| 273 | FROM issue pi JOIN pull p ON p.issue_id = pi.id | |
| 274 | WHERE pi.repo_id = ?1 AND ( | |
| 275 | p.head_branch IN (SELECT r.branch FROM run r JOIN issue ii ON ii.id = r.issue_id | |
| 276 | WHERE ii.repo_id = ?1 AND r.kind = 'agent' AND r.branch != '' | |
| 277 | AND ii.number IN (${list.map((_, i) => `?${i + 2}`).join(",")})) | |
| 278 | OR ${list.map((_, i) => `pi.body LIKE '%#' || ?${i + 2} || '%'`).join(" OR ")}) | |
| 279 | ORDER BY pi.number DESC LIMIT 200`, | |
| 280 | ) | |
| 281 | .all(repoId, ...list); | |
| 282 | return rows.filter((r) => r.ran === 1 || closesFromBody(r.body).some((n) => list.includes(n))).map((r) => r.number); | |
| 283 | } | |
| 284 | ||
| 213 | 285 | /** The words of the work line, in one place. */ |
| 214 | 286 | const WORK_WORDS = { closed: "Done: it is closed.", landed: "landed", ready: "is ready: waiting for you to land it.", checksRunning: "checks running", checksFailed: "checks failed", pushed: "pushed", queued: "Queued for", working: "has been working on it", failed: "'s run did not finish.", finished: "finished.", assigned: "Assigned to", crew: "The crew" } as const; |
| 215 | 287 |
src/tui/screens/talk_chats.ts +109 −17
| @@ -28,21 +28,22 @@ import { | ||
| 28 | 28 | } from "../../core/conversations.ts"; |
| 29 | 29 | import { conversationMarkdown } from "../../core/conversation_markdown.ts"; |
| 30 | 30 | import { publicOrigin } from "../../core/public_origin.ts"; |
| 31 | import { filesOf, repoOf, steerOf, stepOf } from "../../core/talk_steps.ts"; | |
| 31 | import { filesOf, receiptIssues, repoOf, steerOf, stepOf } from "../../core/talk_steps.ts"; | |
| 32 | 32 | import { cardsPlaced, holdsCard, plainVoice } from "../../core/talk_voice.ts"; |
| 33 | 33 | import { setTalkDisplay, talkDisplay } from "../../core/settings.ts"; |
| 34 | 34 | import { talkDisplayScreen } from "./talk_display.ts"; |
| 35 | import { liveReceipts, paneCrew, type LiveReceipt } from "../../core/talk_work.ts"; | |
| 35 | import { conversationPulls, liveReceipts, paneCrew, paneFell, paneInfo, type LiveReceipt } from "../../core/talk_work.ts"; | |
| 36 | 36 | import { tapRunLogs } from "../../runs/log.ts"; |
| 37 | 37 | import { mayReadRunLog, mayReadRuns } from "../../actions/runs.ts"; |
| 38 | 38 | import { confirmsScreen } from "./confirms.ts"; |
| 39 | 39 | import { waitingConfirms } from "../../actions/conversation_confirms.ts"; |
| 40 | import { branchDiff, chatBuildOf } from "../../core/chat_build.ts"; | |
| 40 | import { branchDiff, chatBuildOf, mineEmpty, pullChanges, type PullChange } from "../../core/chat_build.ts"; | |
| 41 | 41 | import { addCommentThrough } from "../../actions/issues.ts"; |
| 42 | 42 | import { getIssue } from "../../core/issues.ts"; |
| 43 | 43 | import { canRead, getRepo, getRepoById } from "../../core/repos.ts"; |
| 44 | 44 | import type { ShownCard } from "../../core/talk_voice.ts"; |
| 45 | import { workspaceOf } from "../../actions/talk_workspace.ts"; | |
| 45 | import { PANE_PATH_REFUSAL, paneChoice, paneOffer, panePath, pointedAt, workspaceOf, type PaneAt, type PaneOffer } from "../../actions/talk_workspace.ts"; | |
| 46 | import type { Diff } from "../../git/diff.ts"; | |
| 46 | 47 | import type { Ctx, Screen } from "../app/app.ts"; |
| 47 | 48 | import { binding } from "../app/keys.ts"; |
| 48 | 49 | import { style } from "../term/style.ts"; |
| @@ -131,6 +132,11 @@ function chatScreen(f: Session, ctx: Ctx, chat: Conversation, hit?: string): Scr | ||
| 131 | 132 | let pane = false; |
| 132 | 133 | // What the conversation's branch changed, read when the pane opens: the page's Code and Diff. |
| 133 | 134 | let changed: { branch: string; files: { path: string; additions: number; deletions: number; lines: string[] }[] } | undefined; |
| 135 | // The pane's address bar, as the page keeps it: what it offers, what the person picked, and the | |
| 136 | // Diff of this conversation's pulls (`pullChanges`) when no build or pull picked says otherwise. | |
| 137 | let offer: PaneOffer = { repos: [], origins: [] }; | |
| 138 | let at: PaneAt = {}; | |
| 139 | let mine: { asked: boolean; changes: PullChange[] } | undefined; | |
| 134 | 140 | // Display's steps choice is the person's own, the page's and the terminal's: v flips it here. |
| 135 | 141 | const stepsNow = () => (f.user === undefined ? "folded" : talkDisplay(f.app.db, f.user.id).steps); |
| 136 | 142 | type Turns = Exclude<ReturnType<typeof listTurns>, { ok: false }>["value"]; |
| @@ -158,14 +164,32 @@ function chatScreen(f: Session, ctx: Ctx, chat: Conversation, hit?: string): Scr | ||
| 158 | 164 | const here = new Set(recent.filter((t) => t.role === "confirm").map((t) => confirmOf(t.text))); |
| 159 | 165 | return waitingConfirms(f, f.user).filter((w) => here.has(w.id)); |
| 160 | 166 | }; |
| 161 | // The branch this conversation changes against the default, as the page's Code and Diff read it. | |
| 167 | // The pick read against what the bar offers (`paneChoice`), and where it points `workspaceOf`: the | |
| 168 | // same two functions the page uses. | |
| 169 | const steerOn = (name: string) => ((s) => (s?.repo === name ? s : undefined))(steerOf(recent)); | |
| 170 | const choice = (): PaneAt => paneChoice(f.app.db, offer, at, repo); | |
| 171 | const paneRepo = (): string | undefined => choice().repo ?? repo; | |
| 172 | const sign = { add: "+", del: "-", context: " " } as const; | |
| 173 | const linesOf = (d: Diff) => d.files.map((x) => ({ path: x.path, additions: x.additions, deletions: x.deletions, lines: x.hunks.flatMap((h) => h.lines.map((l) => `${sign[l.kind]}${l.content}`)) })); | |
| 174 | // Diff, as the page's tab reads it: the build's branch, or the pull picked, else what this | |
| 175 | // conversation's work changed. | |
| 162 | 176 | const readChanged = async (): Promise<void> => { |
| 163 | const ws = repo === undefined || f.user === undefined ? undefined : workspaceOf(f.app, f.user, repo); | |
| 177 | if (repo === undefined || f.user === undefined) return; | |
| 178 | offer = paneOffer(f.app, f.user, repo); | |
| 179 | const picked = choice(); | |
| 180 | const name = picked.repo ?? repo; | |
| 181 | const ws = workspaceOf(f.app, f.user, name, undefined, pointedAt(picked, steerOn(name))); | |
| 182 | changed = undefined; | |
| 183 | mine = undefined; | |
| 164 | 184 | if (ws === undefined) return; |
| 165 | const build = chatBuildOf(f.app.db, chat.id); | |
| 166 | const d = await branchDiff(f.app.db, f.app.reposRoot, { repo: ws.repo, build, pull: ws.pull ?? build?.pullNumber ?? undefined }); | |
| 167 | const sign = { add: "+", del: "-", context: " " } as const; | |
| 168 | changed = d === undefined ? undefined : { branch: d.branch, files: d.diff.files.map((x) => ({ path: x.path, additions: x.additions, deletions: x.deletions, lines: x.hunks.flatMap((h) => h.lines.map((l) => `${sign[l.kind]}${l.content}`)) })) }; | |
| 185 | const build = name === repo ? chatBuildOf(f.app.db, chat.id) : undefined; | |
| 186 | if (build !== undefined || picked.pull !== undefined) { | |
| 187 | const d = await branchDiff(f.app.db, f.app.reposRoot, { repo: ws.repo, build, pull: picked.pull ?? build?.pullNumber ?? undefined }); | |
| 188 | changed = d === undefined ? undefined : { branch: d.branch, files: linesOf(d.diff) }; | |
| 189 | return; | |
| 190 | } | |
| 191 | const issues = receiptIssues(turns, name); | |
| 192 | mine = { asked: issues.length > 0, changes: await pullChanges(f.app.db, f.app.reposRoot, ws.repo, conversationPulls(f.app.db, ws.repo.id, issues)) }; | |
| 169 | 193 | }; |
| 170 | 194 | // The crew's newest run on the repository, as the page's waiting pane says it, tailed live while the |
| 171 | 195 | // pane is open: its log taps this screen (`load`), and a new line of that run redraws it. |
| @@ -181,21 +205,31 @@ function chatScreen(f: Session, ctx: Ctx, chat: Conversation, hit?: string): Scr | ||
| 181 | 205 | }; |
| 182 | 206 | const paneLines = (): string[] => { |
| 183 | 207 | if (repo === undefined || f.user === undefined) return ["No repository yet."]; |
| 184 | // Where the agent pointed the pane, as the page frames it (`pane`). | |
| 185 | const steer = ((s) => (s?.repo === repo ? s : undefined))(steerOf(recent)); | |
| 186 | const ws = workspaceOf(f.app, f.user, repo, undefined, steer ?? {}); | |
| 208 | // Where the person picked, else where the agent pointed the pane (`pane`), as the page frames it. | |
| 209 | const picked = choice(); | |
| 210 | const name = paneRepo() ?? repo; | |
| 211 | const steer = steerOn(name); | |
| 212 | const ws = workspaceOf(f.app, f.user, name, undefined, pointedAt(picked, steer)); | |
| 187 | 213 | if (ws === undefined) return ["Not a repository you can see."]; |
| 188 | const files = filesOf(recent, repo); | |
| 214 | const files = filesOf(recent, name); | |
| 189 | 215 | const url = ws.url; |
| 216 | const fell = paneFell(f.app.db, ws.repo.id, ws.fell); | |
| 190 | 217 | return [ |
| 191 | `${repo} · ${ws.shows === undefined ? "Production" : `Pull #${String(ws.shows)}`}`, | |
| 218 | `${name} · ${ws.shows === undefined ? "Production" : `Pull #${String(ws.shows)}`}${pointedAt(picked, steer).path === undefined ? "" : ` · ${pointedAt(picked, steer).path ?? ""}`}`, | |
| 219 | paneInfo(f.app.db, ws.repo, ws.shows), | |
| 220 | ...(fell === undefined ? [] : [fell]), | |
| 221 | "o picks the repository, w the version, l a path.", | |
| 192 | 222 | "", |
| 193 | 223 | url === undefined ? "Nothing running yet." : `Preview: ${url}`, |
| 194 | 224 | ...(url === undefined || steer === undefined || steer.try === "" ? [] : [`Try: ${steer.try}`]), |
| 195 | 225 | ...(ws.url === undefined ? crewLines(ws.repo.id) : []), |
| 196 | ws.pull === undefined ? "No open pull yet." : `Diff: /${repo}/pulls/${String(ws.pull)}/files`, | |
| 226 | ws.pull === undefined ? "No open pull yet." : `Diff: /${name}/pulls/${String(ws.pull)}/files`, | |
| 197 | 227 | "", |
| 198 | ...(changed === undefined | |
| 228 | ...(mine !== undefined | |
| 229 | ? mine.changes.length === 0 | |
| 230 | ? [mineEmpty(mine.asked), ...(files.length === 0 ? [] : ["", "Files touched:", ...files.map((p) => ` ${p}`)])] | |
| 231 | : mine.changes.flatMap((c) => [`#${String(c.number)} ${c.title} · ${c.state} /${name}/pulls/${String(c.number)}`, ...(c.diff === undefined ? [" The change could not be read just now."] : linesOf(c.diff.diff).flatMap((d) => [` ${d.path} +${String(d.additions)} −${String(d.deletions)}`, ...d.lines.map((l) => ` ${l}`)])), ""]) | |
| 232 | : changed === undefined | |
| 199 | 233 | ? files.length === 0 |
| 200 | 234 | ? ["No files touched yet."] |
| 201 | 235 | : ["Files touched:", ...files.map((p) => ` ${p}`)] |
| @@ -266,6 +300,7 @@ function chatScreen(f: Session, ctx: Ctx, chat: Conversation, hit?: string): Scr | ||
| 266 | 300 | binding(["1", "2", "3"], "starter"), |
| 267 | 301 | binding(["r"], "reload"), |
| 268 | 302 | binding(["p"], "pane"), |
| 303 | binding(["o", "w", "l"], "in the pane: repository, version, path"), | |
| 269 | 304 | binding(["b"], "a result's buttons"), |
| 270 | 305 | binding(["v"], "steps open or folded"), |
| 271 | 306 | binding(["D"], "display"), |
| @@ -305,6 +340,63 @@ function chatScreen(f: Session, ctx: Ctx, chat: Conversation, hit?: string): Scr | ||
| 305 | 340 | ctx.invalidate(); |
| 306 | 341 | return true; |
| 307 | 342 | } |
| 343 | if (pane && (e.key === "o" || e.key === "w")) { | |
| 344 | // The address bar's two pickers, over what `paneOffer` offers this person. | |
| 345 | const name = paneRepo(); | |
| 346 | const row = offer.repos.find((r) => r.name === name); | |
| 347 | const items: PickItem[] = e.key === "o" ? offer.repos.map((r) => ({ id: r.name, label: r.name })) : (row?.versions ?? []).map((v) => ({ id: v.key, label: v.label })); | |
| 348 | if (items.length === 0) { | |
| 349 | note = "Nothing to pick here yet."; | |
| 350 | ctx.invalidate(); | |
| 351 | return true; | |
| 352 | } | |
| 353 | const repick = (next: PaneAt) => { | |
| 354 | at = next; | |
| 355 | runAsync(ctx, async () => { | |
| 356 | await readChanged(); | |
| 357 | ctx.invalidate(); | |
| 358 | }); | |
| 359 | }; | |
| 360 | ctx.push( | |
| 361 | pickerScreen(ctx, screen, { | |
| 362 | title: e.key === "o" ? "Repository" : "Version", | |
| 363 | items, | |
| 364 | onPick: ([id]) => { | |
| 365 | if (id === undefined) return; | |
| 366 | // A new repository starts at its newest version and its root, as the page's picker does. | |
| 367 | if (e.key === "o") repick(id === repo ? {} : { repo: id }); | |
| 368 | else { | |
| 369 | const pulled = /^pull-(\d+)$/.exec(id); | |
| 370 | const base = choice(); | |
| 371 | repick({ ...(base.repo === undefined ? {} : { repo: base.repo }), ...(pulled === null ? { live: true } : { pull: Number(pulled[1]) }), ...(base.path === undefined ? {} : { path: base.path }) }); | |
| 372 | } | |
| 373 | }, | |
| 374 | }), | |
| 375 | ); | |
| 376 | return true; | |
| 377 | } | |
| 378 | if (pane && e.key === "l") { | |
| 379 | ctx.push( | |
| 380 | promptScreen(ctx, screen, { | |
| 381 | title: "Path", | |
| 382 | label: "on the app, as the preview opens it", | |
| 383 | placeholder: "/", | |
| 384 | ...(choice().path === undefined ? {} : { value: choice().path }), | |
| 385 | onSubmit: (words) => { | |
| 386 | const path = panePath(words.trim()); | |
| 387 | if (path === undefined) return PANE_PATH_REFUSAL; | |
| 388 | const { path: _old, ...rest } = choice(); | |
| 389 | at = path === "/" ? rest : { ...rest, path }; | |
| 390 | runAsync(ctx, async () => { | |
| 391 | await readChanged(); | |
| 392 | ctx.invalidate(); | |
| 393 | }); | |
| 394 | return undefined; | |
| 395 | }, | |
| 396 | }), | |
| 397 | ); | |
| 398 | return true; | |
| 399 | } | |
| 308 | 400 | if (e.key === "a") { |
| 309 | 401 | ctx.push(confirmsScreen(f, ctx)); |
| 310 | 402 | return true; |
src/web/layout.ts +6 −5
| @@ -222,11 +222,12 @@ export interface PageOptions { | ||
| 222 | 222 | */ |
| 223 | 223 | turnstile?: boolean; |
| 224 | 224 | /** |
| 225 | * One more origin this page may frame, as `scheme://host[:port]`: the | |
| 226 | * display origin a captured page plays on. It loosens `frame-src` for this | |
| 227 | * page alone; anything that is not a bare origin is ignored. | |
| 225 | * More origins this page may frame, each as `scheme://host[:port]`: the | |
| 226 | * display origin a captured page plays on, or the apps Talk's address bar | |
| 227 | * offers. It loosens `frame-src` for this page alone; anything that is not | |
| 228 | * a bare origin is ignored. | |
| 228 | 229 | */ |
| 229 | frameSrc?: string | undefined; | |
| 230 | frameSrc?: string | readonly string[] | undefined; | |
| 230 | 231 | /** Extra palette rows for this page's context (a repo's own tabs). */ |
| 231 | 232 | palette?: PaletteItem[]; |
| 232 | 233 | /** |
| @@ -1080,7 +1081,7 @@ export function frameSources(options: Pick<PageOptions, "turnstile" | "frameSrc" | ||
| 1080 | 1081 | const origins = [ |
| 1081 | 1082 | ...(options.frameSelf ? ["'self'"] : []), |
| 1082 | 1083 | ...(options.turnstile ? [TURNSTILE_ORIGIN] : []), |
| 1083 | ...(options.frameSrc !== undefined && BARE_ORIGIN.test(options.frameSrc) ? [options.frameSrc] : []), | |
| 1084 | ...(options.frameSrc === undefined ? [] : typeof options.frameSrc === "string" ? [options.frameSrc] : options.frameSrc).filter((o) => BARE_ORIGIN.test(o)), | |
| 1084 | 1085 | ]; |
| 1085 | 1086 | return origins.length === 0 ? "" : `frame-src ${origins.join(" ")}; `; |
| 1086 | 1087 | } |
src/web/pages/talk.ts +23 −7
| @@ -74,12 +74,12 @@ import { badge, btn, icon, iconBtn, menu, meter, row, time } from "../ui.ts"; | ||
| 74 | 74 | import { inlineIcons } from "../icons.ts"; |
| 75 | 75 | import { DISPLAY_NAMES, setTalkDisplay, TALK_DISPLAY, talkDisplay, type TalkDisplay } from "../../core/settings.ts"; |
| 76 | 76 | import { peekCss, workLineHtml } from "../talk_peek.ts"; |
| 77 | import { paneCrew, receiptLines, type WorkLine } from "../../core/talk_work.ts"; | |
| 78 | import { workspaceOf } from "../../actions/talk_workspace.ts"; | |
| 77 | import { conversationPulls, paneCrew, receiptLines, type WorkLine } from "../../core/talk_work.ts"; | |
| 78 | import { paneAtOf, paneChoice, paneOffer, paneQuery, workspaceOf } from "../../actions/talk_workspace.ts"; | |
| 79 | 79 | import { buildCss, chosenTemplate, panelFor, templateChips } from "../talk_build.ts"; |
| 80 | 80 | import type { User } from "../../core/users.ts"; |
| 81 | 81 | import { chatBuildOf } from "../../core/chat_build.ts"; |
| 82 | import { runningStep, stepOf, repoOf, steerOf, type Step } from "../../core/talk_steps.ts"; | |
| 82 | import { receiptIssues, runningStep, stepOf, repoOf, steerOf, type Step } from "../../core/talk_steps.ts"; | |
| 83 | 83 | import { paneCss, VIEWS, talkPane, type View } from "./talk_preview.ts"; |
| 84 | 84 | import { TALK_KEYS, talkCss, talkJs } from "./talk_client.ts"; |
| 85 | 85 | import { liveRegionsJs } from "../client.ts"; |
| @@ -592,7 +592,20 @@ async function talkPage(ctx: Ctx, conversation: Conversation, before0: string | | ||
| 592 | 592 | const v = Number(ctx.url.searchParams.get("v") ?? ""); |
| 593 | 593 | const wait = panel === null ? undefined : pending ? "Starting the preview…" : undefined; |
| 594 | 594 | const idle = panel === null ? undefined : running ? "Making the first version. The preview shows here once it is built, and each version after it reloads in place." : "Your app shows here once its first version is made: say what to change and the crew makes it. Below are the files it starts from."; |
| 595 | const pane = await talkPane(ctx, user, builtRepo === undefined ? cited : `${builtRepo.ownerName}/${builtRepo.name}`, works, view, undefined, { | |
| 595 | // The address bar's choice (`?repo=&show=&path=`): a repository it offers this person, else the | |
| 596 | // conversation's own; the page names in `frame-src` exactly the origins it offers. | |
| 597 | const worksOn = builtRepo === undefined ? cited : `${builtRepo.ownerName}/${builtRepo.name}`; | |
| 598 | const offer = paneOffer(ctx.app, user, worksOn); | |
| 599 | const at = paneChoice(ctx.app.db, offer, paneAtOf(ctx.url.searchParams), worksOn); | |
| 600 | const paneRepo = at.repo ?? worksOn; | |
| 601 | const paneRow = paneRepo === undefined ? undefined : offer.repos.find((r) => r.name === paneRepo); | |
| 602 | // What this conversation changed is read only for the Diff tab, and only when it filed something here. | |
| 603 | const issuesHere = paneRepo === undefined || view !== "diff" ? [] : receiptIssues(turns, paneRepo); | |
| 604 | const repoRow = paneRow?.repo; | |
| 605 | const pane = await talkPane(ctx, user, paneRepo, works, view, undefined, { | |
| 606 | at: { ...(at.repo === undefined ? {} : { repo: at.repo }), ...(at.live === true ? { live: true } : at.pull === undefined ? {} : { pull: at.pull }), ...(at.path === undefined ? {} : { path: at.path }) }, | |
| 607 | offer, | |
| 608 | ...(repoRow === undefined ? {} : { mine: { pulls: conversationPulls(ctx.app.db, repoRow.id, issuesHere), asked: issuesHere.length > 0 } }), | |
| 596 | 609 | ...(buildRow === undefined ? {} : { build: buildRow }), |
| 597 | 610 | ...(Number.isInteger(v) && v > 0 ? { version: v } : {}), |
| 598 | 611 | ...(wait === undefined ? {} : { wait }), |
| @@ -606,15 +619,18 @@ async function talkPage(ctx: Ctx, conversation: Conversation, before0: string | | ||
| 606 | 619 | const split = panel !== null || pane.live || pane.crew; |
| 607 | 620 | // The frame shows what runs; beside a build it waits for the build's own preview. |
| 608 | 621 | const framed = (pane.live || pane.crew) && !pending; |
| 622 | // Every origin the address bar offers, and the frame's own: what `frame-src` names, exactly. | |
| 623 | const frames = [...new Set([...offer.origins, ...(pane.frame === undefined || !framed ? [] : [pane.frame])])]; | |
| 624 | const paneQ = paneQuery(at); | |
| 609 | 625 | const about = editing === undefined ? aboutLine(ctx) : null; |
| 610 | 626 | const display = talkDisplay(ctx.app.db, user.id); |
| 611 | 627 | const lines = receiptLines(ctx.app.db, ctx.app.config.dataDir, user, turns); |
| 612 | 628 | // The crew's line says something only while a run is live: idle is silence. |
| 613 | 629 | const crew = liveCrewRows(ctx, user); |
| 614 | const body = html`<div class="talk-page" data-shell${pane.frame === undefined || !framed ? null : html` data-frame-origin="${pane.frame}"`} data-pane="${shown}"${listing ? raw(" data-view=\"list\"") : null}${split ? raw(" data-split") : null}><h1 class="sr-only">Talk</h1> | |
| 630 | const body = html`<div class="talk-page" data-shell${!split ? null : html` data-frame-origins="${frames.join(" ")}"`} data-pane="${shown}"${listing ? raw(" data-view=\"list\"") : null}${split ? raw(" data-split") : null}><h1 class="sr-only">Talk</h1> | |
| 615 | 631 | <div class="talk-side">${chats(ctx, conversation, showArchived, typed)}</div> |
| 616 | 632 | <div class="talk" data-talk data-density="${display.density}" data-motion="${display.motion}" data-enter="${display.enter}" data-fold="${display.steps}" data-user="${user.id}" data-c="${conversation.id}" data-here="${here}" data-paged="${paged ? "1" : "0"}" data-running="${running && !paged ? "1" : "0"}"> |
| 617 | ${hereBar(ctx, conversation, context, display, split ? html`<nav class="seg talk-tabs" aria-label="Chat or preview"><a href="?pane=chat"${shown === "chat" ? raw(' aria-current="page"') : null}>Chat</a><a href="?pane=preview"${shown !== "chat" ? raw(' aria-current="page"') : null}>Preview</a></nav>` : null)} | |
| 633 | ${hereBar(ctx, conversation, context, display, split ? html`<nav class="seg talk-tabs" aria-label="Chat or preview"><a href="?pane=chat${paneQ === "" ? "" : `&${paneQ}`}"${shown === "chat" ? raw(' aria-current="page"') : null}>Chat</a><a href="?pane=preview${paneQ === "" ? "" : `&${paneQ}`}"${shown !== "chat" ? raw(' aria-current="page"') : null}>Preview</a></nav>` : null)} | |
| 618 | 634 | ${turns.length === DEFAULT_PAGE && turns[0] !== undefined |
| 619 | 635 | ? html`<p class="talk-more small"><a href="${here}?before=${turns[0].id}">Earlier</a></p>` |
| 620 | 636 | : null} |
| @@ -657,7 +673,7 @@ async function talkPage(ctx: Ctx, conversation: Conversation, before0: string | | ||
| 657 | 673 | </div> |
| 658 | 674 | ${split ? html`<div class="talk-work">${panel}${pane.html}</div>` : null} |
| 659 | 675 | </div>`; |
| 660 | return page({ ctx, title: conversation.title === "" ? "Talk" : `${conversation.title} · Talk`, body, styleCss: talkCss + peekCss + paneCss + codePaneCss + (panel === null ? "" : buildCss) + voiceCss, scriptJs: `${talkJs}\n${liveRegionsJs}`, description: "Talk to the forge.", wide: true, ...(pane.frame === undefined || !framed ? {} : { frameSrc: pane.frame }) }); | |
| 676 | return page({ ctx, title: conversation.title === "" ? "Talk" : `${conversation.title} · Talk`, body, styleCss: talkCss + peekCss + paneCss + codePaneCss + (panel === null ? "" : buildCss) + voiceCss, scriptJs: `${talkJs}\n${liveRegionsJs}`, description: "Talk to the forge.", wide: true, ...(!split ? {} : { frameSrc: frames }) }); | |
| 661 | 677 | } |
| 662 | 678 | |
| 663 | 679 | /** |
src/web/pages/talk_client.ts +31 −3
| @@ -956,7 +956,7 @@ ${dictateJs} | ||
| 956 | 956 | // A pull's preview framed when production deploys: the pane is asked what it frames now. Still the |
| 957 | 957 | // open pull, the frame stays; the pull landed, production is its change, and the page draws once |
| 958 | 958 | // for production's origin. |
| 959 | else if (d.payload.env === "production") fetch("?pane=preview&fragment=pane", { headers: { accept: "text/html" } }).then((r) => r.text()).then((text) => { | |
| 959 | else if (d.payload.env === "production") fetch(paneFragment(), { headers: { accept: "text/html" } }).then((r) => r.text()).then((text) => { | |
| 960 | 960 | const t = document.createElement("template"); |
| 961 | 961 | t.innerHTML = text; |
| 962 | 962 | const now = t.content.querySelector("[data-stage][data-env]"); |
| @@ -986,8 +986,10 @@ ${dictateJs} | ||
| 986 | 986 | document.addEventListener("forge:swapped", (e) => { |
| 987 | 987 | if (e.target.id !== "talk-pane") return; |
| 988 | 988 | const f = e.target.querySelector("[data-frame]"), shell = document.querySelector("[data-shell]"); |
| 989 | if (f && shell && shell.dataset.frameOrigin !== new URL(f.src).origin && !answering) window.location.reload(); | |
| 989 | if (f && shell && !(shell.dataset.frameOrigins || "").split(" ").includes(new URL(f.src).origin) && !answering) window.location.reload(); | |
| 990 | 990 | }); |
| 991 | // The pane read again where the address bar points it (its data-pane-q), so a reload never loses the pick. | |
| 992 | function paneFragment() { const p = document.querySelector("[data-pane-q]"), q = p ? p.getAttribute("data-pane-q") : ""; return "?pane=preview" + (q ? "&" + q : "") + "&fragment=pane"; } | |
| 991 | 993 | // A frame's address carries a grant the forge signed for two minutes, so it is never loaded twice: |
| 992 | 994 | // the pane is read again and the frame loads the fresh address it names, on the same origin. A pane |
| 993 | 995 | // that frames nothing now (the app not serving) leaves the frame as it was, under no note. |
| @@ -999,7 +1001,7 @@ ${dictateJs} | ||
| 999 | 1001 | if (n) { s.setAttribute("data-busy", ""); n.hidden = false; } |
| 1000 | 1002 | return () => { if (n) { s.removeAttribute("data-busy"); n.hidden = true; } }; |
| 1001 | 1003 | }); |
| 1002 | fetch("?pane=preview&fragment=pane", { headers: { accept: "text/html" } }).then((r) => (r.ok ? r.text() : "")).catch(() => "").then((text) => { | |
| 1004 | fetch(paneFragment(), { headers: { accept: "text/html" } }).then((r) => (r.ok ? r.text() : "")).catch(() => "").then((text) => { | |
| 1003 | 1005 | const t = document.createElement("template"); |
| 1004 | 1006 | t.innerHTML = text; |
| 1005 | 1007 | const fresh = t.content.querySelector("[data-frame]"), src = fresh && fresh.getAttribute("src"); |
| @@ -1103,6 +1105,32 @@ ${dictateJs} | ||
| 1103 | 1105 | if (p) { shell.querySelector(".talk-pane").replaceWith(p); codePane(); p.querySelectorAll("[data-frame]").forEach(watchFrame); p.dispatchEvent(new Event("forge:swapped", { bubbles: true })); } |
| 1104 | 1106 | } catch (_) { say("Could not load that; no connection."); } |
| 1105 | 1107 | }); |
| 1108 | // The pane's address bar: a pick (a repository, a version, a path) swaps the pane in place, the | |
| 1109 | // chat untouched, and is one entry in the browser's history, so Back returns to the one before and | |
| 1110 | // a reload keeps it (it is the address, which the server draws the first paint from). | |
| 1111 | const AT = ["repo", "show", "path"]; | |
| 1112 | const atOf = (q) => { const o = new URLSearchParams(); for (const k of AT) { const v = q.get(k); if (v && !(k === "path" && v === "/")) o.set(k, v); } return o; }; | |
| 1113 | const swapAt = async (at, push) => { | |
| 1114 | const shell = document.querySelector("[data-shell]"), now = shell && shell.querySelector(".talk-pane"); | |
| 1115 | if (!now) return; | |
| 1116 | const q = new URLSearchParams(window.location.search); | |
| 1117 | for (const k of AT) q.delete(k); | |
| 1118 | for (const [k, v] of at) q.set(k, v); | |
| 1119 | if (push) window.history.pushState({ at: at.toString() }, "", "?" + q); | |
| 1120 | q.set("pane", q.get("pane") === "code" || q.get("pane") === "diff" ? q.get("pane") : "preview"); | |
| 1121 | q.set("fragment", "pane"); | |
| 1122 | try { | |
| 1123 | const t = document.createElement("template"); | |
| 1124 | t.innerHTML = await (await fetch("?" + q)).text(); | |
| 1125 | const p = t.content.querySelector(".talk-pane"); | |
| 1126 | if (p && now.isConnected) { now.replaceWith(p); codePane(); p.querySelectorAll("[data-frame]").forEach(watchFrame); p.dispatchEvent(new Event("forge:swapped", { bubbles: true })); } | |
| 1127 | } catch (_) { say("Could not load that; no connection."); } | |
| 1128 | }; | |
| 1129 | const picked = (form, repoChanged) => { const q = new URLSearchParams(); for (const k of AT) if (form.elements[k]) q.set(k, form.elements[k].value); const at = atOf(q); if (repoChanged) at.delete("show"); return at; }; | |
| 1130 | document.addEventListener("change", (e) => { const f = e.target.closest && e.target.closest("[data-pane-at]"); if (f && e.target.tagName === "SELECT") swapAt(picked(f, e.target.name === "repo"), true); }); | |
| 1131 | document.addEventListener("submit", (e) => { const f = e.target.closest && e.target.closest("[data-pane-at]"); if (!f) return; e.preventDefault(); swapAt(picked(f, false), true); }); | |
| 1132 | if ("scrollRestoration" in window.history) window.history.scrollRestoration = "manual"; | |
| 1133 | window.addEventListener("popstate", () => swapAt(atOf(new URLSearchParams(window.location.search)), false)); | |
| 1106 | 1134 | // Code: a file opens in the pane from the blob page's own fragment; a folder fills itself in |
| 1107 | 1135 | // from the explorer's; the files this conversation changed are marked wherever they show. |
| 1108 | 1136 | const codePane = () => { |
src/web/pages/talk_preview.ts +58 −17
| @@ -11,10 +11,10 @@ | ||
| 11 | 11 | * streaming beside it carries on. |
| 12 | 12 | */ |
| 13 | 13 | |
| 14 | import { workspaceOf, type Apps, type PaneSteer } from "../../actions/talk_workspace.ts"; | |
| 14 | import { paneQuery, pointedAt, workspaceOf, type Apps, type PaneAt, type PaneSteer, type paneOffer } from "../../actions/talk_workspace.ts"; | |
| 15 | 15 | import { previewEnv } from "../../apps/data.ts"; |
| 16 | 16 | import type { User } from "../../core/users.ts"; |
| 17 | import { paneCrew, type PaneCrew, type Work } from "../../core/talk_work.ts"; | |
| 17 | import { paneCrew, paneFell, paneInfo, type PaneCrew, type Work } from "../../core/talk_work.ts"; | |
| 18 | 18 | import { mayReadRunLog, mayReadRuns } from "../../actions/runs.ts"; |
| 19 | 19 | import { getAppByRepo } from "../../core/apps.ts"; |
| 20 | 20 | import { appsRuntime } from "../../apps/runtime.ts"; |
| @@ -54,6 +54,12 @@ export interface PaneAsk { | ||
| 54 | 54 | readonly busy?: boolean; |
| 55 | 55 | /** Where the agent pointed the pane (`pane`), followed while it names this repository. */ |
| 56 | 56 | readonly steer?: PaneSteer; |
| 57 | /** Where the person pointed it in the address bar: it wins over the agent's. */ | |
| 58 | readonly at?: PaneAt; | |
| 59 | /** What the address bar offers this person (`paneOffer`). */ | |
| 60 | readonly offer?: ReturnType<typeof paneOffer>; | |
| 61 | /** The pulls this conversation's work led to here, and whether it filed anything here at all. */ | |
| 62 | readonly mine?: { readonly pulls: readonly number[]; readonly asked: boolean }; | |
| 57 | 63 | } |
| 58 | 64 | |
| 59 | 65 | export async function talkPane(ctx: Ctx, user: User, name: string | undefined, works: ReadonlyMap<string, Work>, view: View = "preview", apps?: Apps, ask: PaneAsk = {}): Promise<Pane> { |
| @@ -65,12 +71,20 @@ export async function talkPane(ctx: Ctx, user: User, name: string | undefined, w | ||
| 65 | 71 | }); |
| 66 | 72 | if (name === undefined) return empty(emptyLine("talkRepos")); |
| 67 | 73 | const steer = ask.steer?.repo === name ? ask.steer : undefined; |
| 68 | const ws = workspaceOf(ctx.app, user, name, apps, steer ?? {}); | |
| 74 | // The person's own choice in the address bar wins over where the agent pointed the pane. | |
| 75 | const at = ask.at ?? {}; | |
| 76 | const pointed = pointedAt(at, steer); | |
| 77 | const path = pointed.path; | |
| 78 | const ws = workspaceOf(ctx.app, user, name, apps, pointed); | |
| 69 | 79 | if (ws === undefined) return empty(emptyLine("talkHidden")); |
| 70 | 80 | const { pull, url } = ws; |
| 71 | const tab = (id: View, label: string): Html => html`<a href="?pane=${id}"${view === id ? raw(' aria-current="page"') : null}>${label}</a>`; | |
| 81 | // Every link and fragment of the pane carries the address bar's choice, so none loses it. | |
| 82 | const q = paneQuery(at); | |
| 83 | const amp = q === "" ? "" : `&${q}`; | |
| 84 | const liveSrc = `?pane=preview${amp}&fragment=pane`; | |
| 85 | const tab = (id: View, label: string): Html => html`<a href="?pane=${id}${amp}"${view === id ? raw(' aria-current="page"') : null}>${label}</a>`; | |
| 72 | 86 | // Code and Diff are the forge's own browser and diff, read only when their tab is chosen. |
| 73 | const work = { repo: ws.repo, build: ask.build, pull: pull ?? ask.build?.pullNumber ?? undefined }; | |
| 87 | const work = { repo: ws.repo, build: ask.build, pull: at.pull ?? pull ?? ask.build?.pullNumber ?? undefined }; | |
| 74 | 88 | // While nothing is framed, what the crew is doing on the repository, read from its newest run. |
| 75 | 89 | const crew = view === "preview" ? paneCrew(ctx.app.db, ctx.app.config.dataDir, ws.repo.id, user, { runs: mayReadRuns(ctx.app, user, ws.repo), logs: mayReadRunLog(ctx.app, user, ws.repo) }) : undefined; |
| 76 | 90 | const going = crew !== undefined && (crew.state === "queued" || crew.state === "running"); |
| @@ -78,26 +92,44 @@ export async function talkPane(ctx: Ctx, user: User, name: string | undefined, w | ||
| 78 | 92 | view === "code" |
| 79 | 93 | ? await codeView(ctx.app, work) |
| 80 | 94 | : view === "diff" |
| 81 | ? await diffView(ctx.app, work, ask.version) | |
| 95 | ? await diffView(ctx.app, work, ask.version, ask.mine === undefined ? undefined : { ...ask.mine, picked: at.pull }) | |
| 82 | 96 | : url === undefined || ask.wait !== undefined |
| 83 | ? html`<div class="talk-stage talk-stage-wait" data-stage${ask.busy === true ? raw(" data-busy") : null}><p class="talk-stage-note" data-stage-note>${ask.wait ?? ask.idle ?? "Nothing is running yet: the app appears here once it is built."}</p>${crew === undefined ? null : crewBlock(crew, `/${ws.repo.ownerName}/${ws.repo.name}`, ws.repo)}</div>${ask.build === undefined ? null : html`<h2 class="talk-stage-files small muted">The files it starts from</h2>${await codeView(ctx.app, work)}`}` | |
| 84 | : html`${steer === undefined || steer.try === "" ? null : html`<p class="talk-try small" data-pane-try>Try: ${steer.try}</p>`}<div class="talk-stage" data-stage data-env="${ws.shows === undefined ? "production" : previewEnv(ws.shows)}"><iframe class="talk-frame" data-frame src="${url}" title="Live preview of ${name}" referrerpolicy="no-referrer" sandbox="allow-scripts allow-same-origin allow-forms allow-popups"></iframe><p class="talk-stage-note small" data-stage-note hidden>Updating the preview…</p></div>${crewRegion(crew !== undefined && (going || crew.state === "failed") ? crew : undefined, ws.repo)}`; | |
| 97 | ? html`<div class="talk-stage talk-stage-wait" data-stage${ask.busy === true ? raw(" data-busy") : null}><p class="talk-stage-note" data-stage-note>${ask.wait ?? ask.idle ?? "Nothing is running yet: the app appears here once it is built."}</p>${crew === undefined ? null : crewBlock(crew, `/${ws.repo.ownerName}/${ws.repo.name}`, ws.repo, liveSrc)}</div>${ask.build === undefined ? null : html`<h2 class="talk-stage-files small muted">The files it starts from</h2>${await codeView(ctx.app, work)}`}` | |
| 98 | : html`${steer === undefined || steer.try === "" ? null : html`<p class="talk-try small" data-pane-try>Try: ${steer.try}</p>`}<div class="talk-stage" data-stage data-env="${ws.shows === undefined ? "production" : previewEnv(ws.shows)}"><iframe class="talk-frame" data-frame src="${url}" title="Live preview of ${name}" referrerpolicy="no-referrer" sandbox="allow-scripts allow-same-origin allow-forms allow-popups"></iframe><p class="talk-stage-note small" data-stage-note hidden>Updating the preview…</p></div>${crewRegion(crew !== undefined && (going || crew.state === "failed") ? crew : undefined, ws.repo, liveSrc)}`; | |
| 85 | 99 | // The address bar of what runs: which environment, where it lives, reload and open on its own. |
| 86 | 100 | const where = url === undefined ? null : html`<span class="talk-addr small" title="${url}"><span class="muted">${ws.shows === undefined ? "Production" : `Pull #${String(ws.shows)}`}</span> ${new URL(url).host}</span>`; |
| 87 | 101 | // Waiting, the pane is a live region on the repository's runs: a deploy that turns running swaps |
| 88 | 102 | // the frame in, and the crew's next run is read as it is queued, starts or ends. |
| 89 | 103 | const waiting = url === undefined && view === "preview"; |
| 104 | // The address bar as a switcher: the repositories offered, the versions of this one, a path. | |
| 105 | const offered = ask.offer?.repos ?? []; | |
| 106 | const here = offered.find((r) => r.name === name); | |
| 107 | const shownKey = ws.shows === undefined ? "live" : `pull-${String(ws.shows)}`; | |
| 108 | const bar = | |
| 109 | here === undefined | |
| 110 | ? where | |
| 111 | : html`<form class="talk-addr talk-addr-pick small" method="get" data-pane-at> | |
| 112 | <input type="hidden" name="pane" value="${view}"> | |
| 113 | <select name="repo" aria-label="Repository">${offered.map((r) => html`<option value="${r.name}"${r.name === name ? raw(" selected") : null}>${r.name}</option>`)}</select> | |
| 114 | <select name="show" aria-label="Version">${here.versions.map((v) => html`<option value="${v.key}"${v.key === shownKey ? raw(" selected") : null}>${v.label}</option>`)}</select> | |
| 115 | <input name="path" value="${path ?? "/"}" aria-label="Path" spellcheck="false" autocomplete="off"> | |
| 116 | <button class="btn talk-addr-go" type="submit">Go</button> | |
| 117 | </form>`; | |
| 118 | const fell = paneFell(ctx.app.db, ws.repo.id, ws.fell); | |
| 119 | const fellLine = fell === undefined ? null : html`<p class="talk-pane-fell small" role="status">${fell}</p>`; | |
| 90 | 120 | return { |
| 91 | 121 | frame: ws.origin ?? (going ? productionOrigin(ctx, ws.repo.id) : undefined), |
| 92 | 122 | live: url !== undefined, |
| 93 | 123 | crew: going, |
| 94 | html: html`<aside class="talk-pane" id="talk-pane" aria-label="Preview of ${name}" data-pane-of="${name}"${waiting ? html` data-live="run.*" data-live-repo="${ws.repo.ownerName}/${ws.repo.name}" data-live-src="?pane=preview&fragment=pane"` : null}> | |
| 124 | html: html`<aside class="talk-pane" id="talk-pane" aria-label="Preview of ${name}" data-pane-of="${name}" data-pane-q="${q}"${waiting ? html` data-live="run.*" data-live-repo="${ws.repo.ownerName}/${ws.repo.name}" data-live-src="${liveSrc}"` : null}> | |
| 95 | 125 | <div class="talk-pane-bar"> |
| 96 | 126 | <nav class="seg" aria-label="Preview, code or diff">${tab("preview", "Preview")}${tab("code", "Code")}${tab("diff", "Diff")}</nav> |
| 97 | ${where} | |
| 127 | ${bar} | |
| 98 | 128 | ${url === undefined || view !== "preview" ? null : iconBtn("sync", "Reload the preview", { variant: "ghost", type: "button", extra: "js-only talk-reload" })} |
| 99 | 129 | ${url === undefined ? null : btn(icon("external"), { href: url, label: `Open ${name} on its own`, title: "Open on its own", variant: "ghost", external: true, extra: "btn-icon" })} |
| 100 | 130 | </div> |
| 131 | <p class="talk-pane-info small muted" data-pane-info>${paneInfo(ctx.app.db, ws.repo, ws.shows)}${ws.shows === undefined ? null : html` · <a href="/${name}/pulls/${String(ws.shows)}">the pull</a>`}</p> | |
| 132 | ${fellLine} | |
| 101 | 133 | ${panel} |
| 102 | 134 | </aside>`, |
| 103 | 135 | }; |
| @@ -115,10 +147,10 @@ function productionOrigin(ctx: Ctx, repoId: string): string | undefined { | ||
| 115 | 147 | * whenever the pane is open, so the crew's next run (the person's next change) is picked up on |
| 116 | 148 | * run.* and drawn under the same frame, which the swap never touches. |
| 117 | 149 | */ |
| 118 | function crewRegion(crew: PaneCrew | undefined, repo: { readonly ownerName: string; readonly name: string }): Html { | |
| 150 | function crewRegion(crew: PaneCrew | undefined, repo: { readonly ownerName: string; readonly name: string }, src: string): Html { | |
| 119 | 151 | return crew === undefined |
| 120 | ? html`<div class="talk-crew-now" id="talk-crew-now" data-live="run.*" data-live-repo="${repo.ownerName}/${repo.name}" data-live-src="?pane=preview&fragment=pane"></div>` | |
| 121 | : crewBlock(crew, `/${repo.ownerName}/${repo.name}`, repo); | |
| 152 | ? html`<div class="talk-crew-now" id="talk-crew-now" data-live="run.*" data-live-repo="${repo.ownerName}/${repo.name}" data-live-src="${src}"></div>` | |
| 153 | : crewBlock(crew, `/${repo.ownerName}/${repo.name}`, repo, src); | |
| 122 | 154 | } |
| 123 | 155 | |
| 124 | 156 | /** |
| @@ -126,10 +158,10 @@ function crewRegion(crew: PaneCrew | undefined, repo: { readonly ownerName: stri | ||
| 126 | 158 | * (the script adds each new one as the run writes it; a log line is only ever text here), and the |
| 127 | 159 | * run's own page, which is the whole of it without the script. |
| 128 | 160 | */ |
| 129 | function crewBlock(crew: PaneCrew, base: string, repo: { readonly ownerName: string; readonly name: string }): Html { | |
| 161 | function crewBlock(crew: PaneCrew, base: string, repo: { readonly ownerName: string; readonly name: string }, src: string): Html { | |
| 130 | 162 | // Its own live region: the crew's next run is read as it is queued, starts or ends, and the frame |
| 131 | 163 | // above it is never redrawn for it. |
| 132 | return html`<div class="talk-crew-now" id="talk-crew-now" data-live="run.*" data-live-repo="${repo.ownerName}/${repo.name}" data-live-src="?pane=preview&fragment=pane"> | |
| 164 | return html`<div class="talk-crew-now" id="talk-crew-now" data-live="run.*" data-live-repo="${repo.ownerName}/${repo.name}" data-live-src="${src}"> | |
| 133 | 165 | <p class="small${crew.state === "running" ? " talk-crew-working" : ""}">${workLineHtml(crew.line)}</p> |
| 134 | 166 | ${!crew.tail || (crew.steps.length === 0 && crew.state !== "running") ? null : html`<div class="talk-crew-steps small muted" data-crew-steps="${crew.runId}" aria-live="polite">${crew.steps.map((item) => (item.thought ? html`<p class="talk-crew-thought">${item.text}</p>` : html`<p>${item.text}</p>`))}</div>`} |
| 135 | 167 | <a class="small" href="${base}/runs/${crew.runId}">See the whole run</a> |
| @@ -140,7 +172,7 @@ function crewBlock(crew: PaneCrew, base: string, repo: { readonly ownerName: str | ||
| 140 | 172 | * The pane's own styles, sent only on a page that draws one (`data-split`). Spacing is the scale. |
| 141 | 173 | */ |
| 142 | 174 | export const paneCss = /* css */ ` |
| 143 | .talk-pane-bar { display: flex; align-items: center; gap: 6px; min-width: 0; margin-bottom: 8px; } | |
| 175 | .talk-pane-bar { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; min-width: 0; margin-bottom: 8px; } | |
| 144 | 176 | .talk-addr { flex: 1; min-width: 0; padding: 6px 12px; border-radius: 999px; background: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } |
| 145 | 177 | .talk-pane-bar .btn-icon { min-width: 2.75rem; min-height: 2.75rem; } |
| 146 | 178 | .talk-stage { position: relative; border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; background: var(--card); } |
| @@ -161,6 +193,15 @@ export const paneCss = /* css */ ` | ||
| 161 | 193 | .talk-stage:not(.talk-stage-wait) + .talk-crew-now { margin-top: 12px; } |
| 162 | 194 | /* While the crew works under it, the frame gives it room: both in view at once. */ |
| 163 | 195 | .talk-stage:has(+ .talk-crew-now:not(:empty)) .talk-frame { height: calc(100dvh - 30rem); min-height: 14rem; } |
| 164 | (max-width: 30rem) { .talk-addr { display: none; } } | |
| 196 | .talk-addr-pick { display: flex; align-items: center; gap: 4px; padding: 2px 4px; overflow: visible; } | |
| 197 | .talk-addr-pick select, .talk-addr-pick input { min-width: 0; min-height: 2.75rem; border: 0; border-radius: 999px; background: transparent; color: var(--fg); font: inherit; padding: 0 8px; } | |
| 198 | .talk-addr-pick select { flex: 0 1 auto; min-width: 5rem; max-width: 14rem; text-overflow: ellipsis; } | |
| 199 | .talk-addr-pick input { flex: 1; min-width: 4rem; font-family: var(--mono); } | |
| 200 | .talk-addr-pick :is(select, input):focus-visible { background: var(--card); } | |
| 201 | (forced-colors: active) { .talk-addr-pick :is(select, input):focus-visible { outline: 2px solid CanvasText; } } | |
| 202 | :root.js .talk-addr-go { display: none; } | |
| 203 | .talk-pane-info, .talk-pane-fell { margin: 0 0 8px; overflow-wrap: anywhere; } | |
| 204 | /* On a phone the switcher takes a row of its own under the tabs, each picker a share of it. */ | |
| 205 | (max-width: 30rem) { .talk-addr:not(.talk-addr-pick) { display: none; } .talk-addr-pick { order: 1; flex-basis: 100%; } .talk-addr-pick select { flex: 1; max-width: none; } .talk-addr-pick input { flex: 0 1 6rem; } } | |
| 165 | 206 | (prefers-reduced-motion: reduce) { .talk-frame { transition: none; } } |
| 166 | 207 | `; |
src/web/talk_code.ts +35 −11
| @@ -13,7 +13,8 @@ | ||
| 13 | 13 | * version is chosen. The pull is a quiet link in the head, with its checks in words. |
| 14 | 14 | */ |
| 15 | 15 | |
| 16 | import { branchDiff, branchOf, type PaneWork } from "../core/chat_build.ts"; | |
| 16 | import { PULL_CHECKS } from "../core/talk_work.ts"; | |
| 17 | import { branchDiff, branchOf, mineEmpty, pullChanges, type PaneWork } from "../core/chat_build.ts"; | |
| 17 | 18 | import { checkSummary } from "../core/runs.ts"; |
| 18 | 19 | import { getPull } from "../core/pulls.ts"; |
| 19 | 20 | import { versionsIn } from "../actions/build_pull.ts"; |
| @@ -104,21 +105,15 @@ export async function codeView(app: App, w: PaneWork): Promise<Html> { | ||
| 104 | 105 | </div>`; |
| 105 | 106 | } |
| 106 | 107 | |
| 107 | /** Checks in words, for the pull in the Diff tab's head. */ | |
| 108 | const CHECKS: Readonly<Record<string, string>> = { | |
| 109 | ok: "checks passing", | |
| 110 | failed: "checks failing", | |
| 111 | running: "checks running", | |
| 112 | queued: "checks queued", | |
| 113 | cancelled: "checks cancelled", | |
| 114 | }; | |
| 115 | ||
| 116 | 108 | /** The Diff tab: the change itself, all of it or one version's, each file folded open inline. */ |
| 117 | 109 | export async function diffView( |
| 118 | 110 | app: App, |
| 119 | 111 | w: PaneWork, |
| 120 | 112 | version: number | undefined, |
| 113 | mine?: PaneMine, | |
| 121 | 114 | ): Promise<Html> { |
| 115 | // Beside no build, Diff is what this conversation's work changed, or the one pull picked above. | |
| 116 | if (w.build === undefined && mine !== undefined && mine.picked === undefined) return await mineView(app, w.repo, mine); | |
| 122 | 117 | const dir = repoDir(app.reposRoot, w.repo.ownerName, w.repo.name); |
| 123 | 118 | const name = `${w.repo.ownerName}/${w.repo.name}`; |
| 124 | 119 | const versions = |
| @@ -144,7 +139,7 @@ export async function diffView( | ||
| 144 | 139 | const checks = |
| 145 | 140 | pull === undefined |
| 146 | 141 | ? undefined |
| 147 | : CHECKS[checkSummary(app.db, pull.id, pull.headOid).state]; | |
| 142 | : PULL_CHECKS[checkSummary(app.db, pull.id, pull.headOid).state]; | |
| 148 | 143 | const pick = (n: number | undefined, label: string, title: string): Html => |
| 149 | 144 | html`<a |
| 150 | 145 | href="?pane=diff${n === undefined ? "" : `&v=${n}`}" |
| @@ -188,6 +183,35 @@ export async function diffView( | ||
| 188 | 183 | </div>`; |
| 189 | 184 | } |
| 190 | 185 | |
| 186 | /** | |
| 187 | * What a conversation's Diff reads beside no build: the pulls its receipts led to (`conversationPulls`), | |
| 188 | * whether it filed anything on this repository at all, and the pull picked in the address bar. | |
| 189 | */ | |
| 190 | export interface PaneMine { | |
| 191 | readonly pulls: readonly number[]; | |
| 192 | readonly asked: boolean; | |
| 193 | readonly picked: number | undefined; | |
| 194 | } | |
| 195 | ||
| 196 | /** At most this many of a conversation's pulls in its Diff, newest first. */ | |
| 197 | const MINE_SHOWN = 5; | |
| 198 | ||
| 199 | /** Each pull this conversation led to, newest first, with its change: a landed one as it landed. */ | |
| 200 | async function mineView(app: App, repo: PaneWork["repo"], mine: PaneMine): Promise<Html> { | |
| 201 | const name = `${repo.ownerName}/${repo.name}`; | |
| 202 | if (mine.pulls.length === 0) return html`<div class="talk-diff"><p class="muted small">${mineEmpty(mine.asked)}</p></div>`; | |
| 203 | const dir = repoDir(app.reposRoot, repo.ownerName, repo.name); | |
| 204 | const parts: Html[] = []; | |
| 205 | for (const [i, c] of (await pullChanges(app.db, app.reposRoot, repo, mine.pulls, MINE_SHOWN)).entries()) { | |
| 206 | const syntax = c.diff === undefined ? undefined : await diffSyntax(dir, c.diff.diff, c.diff.from, c.diff.to); | |
| 207 | parts.push(html`<details class="talk-diff-change"${i === 0 ? raw(" open") : null}> | |
| 208 | <summary class="small"><a href="/${name}/pulls/${String(c.number)}">#${String(c.number)}</a> ${c.title} <span class="muted">· ${c.state}</span></summary> | |
| 209 | ${c.diff === undefined ? html`<p class="muted small">The change could not be read just now.</p>` : renderDiff(c.diff.diff, { ...(syntax === undefined ? {} : { syntax }), fileLinkBase: `/${name}/blob/${encodeURIComponent(c.at)}` })} | |
| 210 | </details>`); | |
| 211 | } | |
| 212 | return html`<div class="talk-diff">${parts}</div>`; | |
| 213 | } | |
| 214 | ||
| 191 | 215 | /** The two tabs' own styles, beside the pane's. Spacing is the scale; the code and diff are the site's. */ |
| 192 | 216 | export const codePaneCss = ` |
| 193 | 217 | .talk-pane { min-width: 0; } |
test/talk-live-pane.test.ts +2
| @@ -320,6 +320,8 @@ describe("the pane, by the crew's newest run on the repository", () => { | ||
| 320 | 320 | } |
| 321 | 321 | const text = tui.screen.toText(); |
| 322 | 322 | expect(text).toContain("Crew: The crew is working."); |
| 323 | // The line under the address bar, as the page draws it (`paneInfo`). | |
| 324 | expect(text).toContain("Live · main · nothing landed yet"); | |
| 323 | 325 | expect(text).toContain(" Read src/a.ts"); |
| 324 | 326 | expect(text).toContain(" \u201cNow the push, so the preview picks it up.\u201d"); |
| 325 | 327 | expect(text).toContain(" Pushed its work"); |
test/talk-page.test.ts +49 −3
| @@ -454,7 +454,7 @@ describe("the page", () => { | ||
| 454 | 454 | test("the page's own bytes are held: stylesheet and script stay small", async () => { |
| 455 | 455 | const { talkCss, talkJs } = await import("../src/web/pages/talk_client.ts"); |
| 456 | 456 | expect(Buffer.byteLength(talkCss)).toBeLessThan(18_100); |
| 457 | expect(Buffer.byteLength(talkJs)).toBeLessThan(50_400); | |
| 457 | expect(Buffer.byteLength(talkJs)).toBeLessThan(52_900); | |
| 458 | 458 | // A page of its own, so what other tests said to alice never counts: one question and a code reply. |
| 459 | 459 | const sizer = await createUser(app.db, { origin: "seed", trusted: true, username: "sizer", email: "[email protected]", password: "a-good-passphrase" }); |
| 460 | 460 | if (!sizer.ok) throw new Error(sizer.error); |
| @@ -469,7 +469,7 @@ describe("the page", () => { | ||
| 469 | 469 | // Talk page, because a chart can arrive in a live reply, and a page drawn without them would |
| 470 | 470 | // show it unstyled until a reload. 48,328 measured. Then to 50,000 by the reply's one last line |
| 471 | 471 | // and the pane's first-version words: 49,466 measured. |
| 472 | expect(size).toBeLessThan(53_000); | |
| 472 | expect(size).toBeLessThan(54_000); | |
| 473 | 473 | }); |
| 474 | 474 | |
| 475 | 475 | describe("the script, driven against a DOM", () => { |
| @@ -575,7 +575,7 @@ describe("the page", () => { | ||
| 575 | 575 | const bodies: { files?: string[] }[] = []; |
| 576 | 576 | const fetchStub = async (u: string, init: { body: string; method?: string }) => { |
| 577 | 577 | urls.push(u); |
| 578 | if (u.startsWith("?pane=") || u === "/talk" || u === "http://localhost/talk" || u.endsWith("?fragment=build")) return { ok: true, text: async () => page.html }; | |
| 578 | if (u.startsWith("?pane=") || u.includes("&fragment=pane") || u === "/talk" || u === "http://localhost/talk" || u.endsWith("?fragment=build")) return { ok: true, text: async () => page.html }; | |
| 579 | 579 | if (u.endsWith("?fragment=1")) return { ok: true, text: async () => '<div class="blob">const a = 2;</div>' }; |
| 580 | 580 | if (u.endsWith("?fragment=explorer")) return { ok: true, text: async () => '<ul><li><a class="ex-file" data-ex-file tabindex="-1" title="src/a.ts" href="/erin/site/blob/b/src/a.ts">a.ts</a></li></ul>' }; |
| 581 | 581 | if (u.includes("/confirm/")) { sent.push("answer:" + u + ":" + String(init.body)); return { ok: answer.status === 200, status: answer.status, json: async () => (answer.status === 200 ? { ok: true } : { ok: false, message: "this confirm was already answered; it does nothing twice" }) }; } |
| @@ -649,6 +649,12 @@ describe("the page", () => { | ||
| 649 | 649 | t.sources.at(-1)!.on["done"]!({ data: "{}" }); |
| 650 | 650 | await pollUntil(() => loads.length > 0, "the frame loaded again"); |
| 651 | 651 | expect(loads).toEqual(["https://x.example/__forge/grant?t=fresh"]); |
| 652 | // Pointed by the address bar, the pane is read again where it points: the pick is kept. | |
| 653 | t.doc.body.setAttribute("data-pane-q", "repo=alice%2Fone&show=live"); | |
| 654 | t.doc.body.insertAdjacentHTML("beforeend", '<button type="button" class="talk-reload">Reload</button>'); | |
| 655 | t.doc.querySelector(".talk-reload").dispatchEvent(new t.window.MouseEvent("click", { bubbles: true })); | |
| 656 | await pollUntil(() => loads.length > 1, "the frame loaded again, pointed"); | |
| 657 | expect(t.urls).toContain("?pane=preview&repo=alice%2Fone&show=live&fragment=pane"); | |
| 652 | 658 | }); |
| 653 | 659 | |
| 654 | 660 | test("a head that slid away comes back when focus enters it, so a keyboard user never loses the conversation menu", async () => { |
| @@ -713,6 +719,46 @@ describe("the page", () => { | ||
| 713 | 719 | expect(t.sources.at(-1)).toBe(source); |
| 714 | 720 | }); |
| 715 | 721 | |
| 722 | test("the pane's address bar: a pick swaps the pane in place as one history entry; Back swaps the one before; a new repository drops the old version; nothing is kept in the browser", async () => { | |
| 723 | const store: Record<string, string> = {}; | |
| 724 | const t = await boot("u1", store); | |
| 725 | const shell = t.doc.createElement("div"); | |
| 726 | shell.setAttribute("data-shell", ""); | |
| 727 | const bar = (pane: string, marker: string) => `<aside class="talk-pane"><form class="talk-addr" data-pane-at><input type="hidden" name="pane" value="${pane}"><select name="repo"><option value="alice/one" selected>alice/one</option><option value="alice/two">alice/two</option></select><select name="show"><option value="live" selected>Live</option><option value="pull-3">#3 Dark</option></select><input name="path" value="/"></form><p>${marker}</p></aside>`; | |
| 728 | shell.innerHTML = bar("preview", "first"); | |
| 729 | t.doc.body.appendChild(shell); | |
| 730 | const log = t.doc.querySelector(".talk-log"); | |
| 731 | t.box.value = "half typed"; | |
| 732 | const before = t.window.history.length; | |
| 733 | t.page.html = bar("preview", "the pull"); | |
| 734 | const show = shell.querySelector("select[name=show]"); | |
| 735 | show.value = "pull-3"; | |
| 736 | show.dispatchEvent(new t.window.Event("change", { bubbles: true })); | |
| 737 | await pollUntil(() => shell.querySelector(".talk-pane p").textContent === "the pull", "the pane swapped"); | |
| 738 | expect(t.urls.at(-1)).toBe("?repo=alice%2Fone&show=pull-3&pane=preview&fragment=pane"); | |
| 739 | expect(t.window.location.search).toBe("?repo=alice%2Fone&show=pull-3"); | |
| 740 | expect(t.window.history.length).toBe(before + 1); | |
| 741 | // The address is the whole of it: nothing is kept for a later visit to redraw over the first paint. | |
| 742 | expect(Object.keys(store).some((k) => k.startsWith("talk-at"))).toBe(false); | |
| 743 | // The chat is never touched: the log, the composer's words. | |
| 744 | expect(t.doc.querySelector(".talk-log")).toBe(log); | |
| 745 | expect(t.box.value).toBe("half typed"); | |
| 746 | // Another repository: the old one's version means nothing there, so it is dropped. | |
| 747 | t.page.html = bar("preview", "two"); | |
| 748 | const repo = shell.querySelector("select[name=repo]"); | |
| 749 | repo.value = "alice/two"; | |
| 750 | repo.dispatchEvent(new t.window.Event("change", { bubbles: true })); | |
| 751 | await pollUntil(() => shell.querySelector(".talk-pane p").textContent === "two", "the second repository"); | |
| 752 | expect(t.window.location.search).toBe("?repo=alice%2Ftwo"); | |
| 753 | // Back: the address is the one before, and the pane is read for it, with no new entry. | |
| 754 | t.page.html = bar("preview", "back to the pull"); | |
| 755 | t.window.history.replaceState(null, "", "?repo=alice%2Fone&show=pull-3"); | |
| 756 | t.window.dispatchEvent(new t.window.PopStateEvent("popstate")); | |
| 757 | await pollUntil(() => shell.querySelector(".talk-pane p").textContent === "back to the pull", "Back swapped the pane"); | |
| 758 | expect(t.urls.at(-1)).toBe("?repo=alice%2Fone&show=pull-3&pane=preview&fragment=pane"); | |
| 759 | expect(t.window.history.length).toBe(before + 2); | |
| 760 | }); | |
| 761 | ||
| 716 | 762 | test("a pasted file becomes a chip with Remove, and a sent message carries its id once", async () => { |
| 717 | 763 | const t = await boot(); |
| 718 | 764 | const paste = new t.window.Event("paste", { bubbles: true, cancelable: true }) as any; |
test/talk-pane-bar.test.ts added +452
| @@ -0,0 +1,452 @@ | ||
| 1 | /** | |
| 2 | * The pane's address bar as a switcher (pane-bar A): a repository, a version (Live or an open pull's | |
| 3 | * preview), a path. It offers only repositories the person may read; the page names in `frame-src` | |
| 4 | * exactly the origins it offers; a path goes through the pane's one hostile-path check (`panePath`); | |
| 5 | * a picked pull whose preview is gone falls to Live with one line saying why; the line under the bar | |
| 6 | * says who made the pull and its checks and review, or what Live last landed, alike in both doors; | |
| 7 | * and Diff shows what this conversation's work changed, never the repository's newest pull unasked. | |
| 8 | */ | |
| 9 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 10 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 11 | import { tmpdir } from "node:os"; | |
| 12 | import { join } from "node:path"; | |
| 13 | import { newApp } from "../src/actions/apps.ts"; | |
| 14 | import { | |
| 15 | paneAtOf, | |
| 16 | paneOffer, | |
| 17 | paneQuery, | |
| 18 | } from "../src/actions/talk_workspace.ts"; | |
| 19 | import { AppsRuntime } from "../src/apps/runtime.ts"; | |
| 20 | import { createApp, type App } from "../src/app.ts"; | |
| 21 | import { getAppByRepo } from "../src/core/apps.ts"; | |
| 22 | import { appendTurn, createConversation } from "../src/core/conversations.ts"; | |
| 23 | import { createIssue } from "../src/core/issues.ts"; | |
| 24 | import { addTeamMember, createOrg, createTeam } from "../src/core/orgs.ts"; | |
| 25 | import { getRepo, type Repo } from "../src/core/repos.ts"; | |
| 26 | import { createRun } from "../src/core/runs.ts"; | |
| 27 | import { createSession } from "../src/core/sessions.ts"; | |
| 28 | import { conversationPulls, paneInfo } from "../src/core/talk_work.ts"; | |
| 29 | import { receiptIssues } from "../src/core/talk_steps.ts"; | |
| 30 | import { createUser, type User } from "../src/core/users.ts"; | |
| 31 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 32 | import { promote } from "./helpers/members.ts"; | |
| 33 | import { paneChoice } from "../src/actions/talk_workspace.ts"; | |
| 34 | import { createForgeApp } from "../src/tui/forge.ts"; | |
| 35 | import { headlessTerminal } from "../src/tui/term/terminal.ts"; | |
| 36 | import { settleApp } from "./helpers/tui.ts"; | |
| 37 | ||
| 38 | let dir: string; | |
| 39 | let app: App; | |
| 40 | let servers: Servers; | |
| 41 | let runtime: AppsRuntime; | |
| 42 | let alice: User; | |
| 43 | let bob: User; | |
| 44 | let carol: User; | |
| 45 | const repos: Record<string, Repo> = {}; | |
| 46 | ||
| 47 | const user = async (name: string, admin = false): Promise<User> => { | |
| 48 | const made = await createUser(app.db, { | |
| 49 | origin: "seed", | |
| 50 | trusted: true, | |
| 51 | username: name, | |
| 52 | email: `${name}@forge.local`, | |
| 53 | password: "a-good-passphrase", | |
| 54 | adminIfFirst: admin, | |
| 55 | }); | |
| 56 | if (!made.ok) throw new Error(made.error); | |
| 57 | promote(app.db, made.value); | |
| 58 | return made.value; | |
| 59 | }; | |
| 60 | ||
| 61 | beforeAll(async () => { | |
| 62 | dir = await mkdtemp(join(tmpdir(), "forge-pane-bar-")); | |
| 63 | app = createApp({ dataDir: dir, port: 0 }); | |
| 64 | servers = await serve(app); | |
| 65 | // The forge's admin may read everything; the people under test are not admins. | |
| 66 | await user("keeper", true); | |
| 67 | alice = await user("alice"); | |
| 68 | bob = await user("bob"); | |
| 69 | carol = await user("carol"); | |
| 70 | // A team app carol writes as a member, alice's own two, and bob's one. | |
| 71 | const org = createOrg(app.db, "team", alice.id); | |
| 72 | if (!org.ok) throw new Error(org.error); | |
| 73 | const writers = createTeam(app.db, org.value.id, "writers", "write"); | |
| 74 | if (!writers.ok) throw new Error(writers.error); | |
| 75 | addTeamMember(app.db, writers.value, carol.id); | |
| 76 | for (const [who, name, owner, visibility] of [ | |
| 77 | [alice, "one", "alice", "private"], | |
| 78 | [alice, "two", "alice", "public"], | |
| 79 | [alice, "shared", "team", "private"], | |
| 80 | [bob, "mine", "bob", "private"], | |
| 81 | ] as const) { | |
| 82 | const made = await newApp(app, who, { | |
| 83 | name, | |
| 84 | description: name, | |
| 85 | visibility, | |
| 86 | owner, | |
| 87 | }); | |
| 88 | if (!made.ok) throw new Error(String(made.error)); | |
| 89 | repos[`${owner}/${name}`] = made.value.repo; | |
| 90 | } | |
| 91 | // The runtime only names each app's address here; nothing is started. | |
| 92 | runtime = new AppsRuntime(app, { | |
| 93 | domain: "apps.test", | |
| 94 | scheme: "http", | |
| 95 | publicPort: 8080, | |
| 96 | forgeUrl: servers.url, | |
| 97 | concurrency: 1, | |
| 98 | }); | |
| 99 | }); | |
| 100 | afterAll(async () => { | |
| 101 | await runtime.stop(); | |
| 102 | servers.stop(); | |
| 103 | app.close(); | |
| 104 | await rm(dir, { recursive: true, force: true }); | |
| 105 | }); | |
| 106 | ||
| 107 | const live = (name: string): string => | |
| 108 | new URL(runtime.urlFor(getAppByRepo(app.db, repos[name]!.id)!, "production")) | |
| 109 | .origin; | |
| 110 | ||
| 111 | /** A conversation of `who` that worked on `repo`, its crew run queued so the pane is open. */ | |
| 112 | const talkOn = (who: User, repo: string, receipt?: number): string => { | |
| 113 | const chat = createConversation(app.db, who.id); | |
| 114 | if (!chat.ok) throw new Error(chat.error); | |
| 115 | appendTurn(app.db, who.id, chat.value.id, { | |
| 116 | role: "person", | |
| 117 | text: "change it", | |
| 118 | }); | |
| 119 | appendTurn(app.db, who.id, chat.value.id, { | |
| 120 | role: "tool", | |
| 121 | text: `mcp__forge__read_file ${JSON.stringify({ repo, path: "README.md" })}`, | |
| 122 | }); | |
| 123 | if (receipt !== undefined) | |
| 124 | appendTurn(app.db, who.id, chat.value.id, { | |
| 125 | role: "tool", | |
| 126 | text: JSON.stringify({ | |
| 127 | filed: true, | |
| 128 | done: true, | |
| 129 | receipt: { | |
| 130 | verb: "Filed", | |
| 131 | object: `#${String(receipt)}`, | |
| 132 | href: `/${repo}/issues/${String(receipt)}`, | |
| 133 | }, | |
| 134 | }), | |
| 135 | }); | |
| 136 | appendTurn(app.db, who.id, chat.value.id, { role: "agent", text: "On it." }); | |
| 137 | createRun(app.db, { | |
| 138 | repoId: getRepo(app.db, repo.split("/")[0]!, repo.split("/")[1]!)!.id, | |
| 139 | kind: "agent", | |
| 140 | reason: "assigned", | |
| 141 | }); | |
| 142 | return chat.value.id; | |
| 143 | }; | |
| 144 | ||
| 145 | const page = async ( | |
| 146 | who: User, | |
| 147 | id: string, | |
| 148 | query = "", | |
| 149 | ): Promise<{ html: string; frameSrc: string[] }> => { | |
| 150 | const res = await fetch(`${servers.url}/talk/${id}${query}`, { | |
| 151 | headers: { cookie: `forge_session=${createSession(app.db, who.id).token}` }, | |
| 152 | }); | |
| 153 | const csp = res.headers.get("content-security-policy") ?? ""; | |
| 154 | const frame = csp | |
| 155 | .split(";") | |
| 156 | .map((d) => d.trim()) | |
| 157 | .find((d) => d.startsWith("frame-src")); | |
| 158 | return { | |
| 159 | html: await res.text(), | |
| 160 | frameSrc: frame === undefined ? [] : frame.split(/\s+/).slice(1).sort(), | |
| 161 | }; | |
| 162 | }; | |
| 163 | ||
| 164 | describe("the address bar's choice", () => { | |
| 165 | test("a path is the pane's one hostile-path check: anything that could leave the app's address is dropped", () => { | |
| 166 | for (const bad of [ | |
| 167 | "//evil.example/x", | |
| 168 | "/a/../../b", | |
| 169 | "/%2e%2e/x", | |
| 170 | "/a\\b", | |
| 171 | "/@evil", | |
| 172 | "http://evil.example/", | |
| 173 | "x", | |
| 174 | "/a b", | |
| 175 | ]) | |
| 176 | expect([bad, paneAtOf(new URLSearchParams({ path: bad })).path]).toEqual([ | |
| 177 | bad, | |
| 178 | undefined, | |
| 179 | ]); | |
| 180 | expect( | |
| 181 | paneAtOf( | |
| 182 | new URLSearchParams({ | |
| 183 | repo: "alice/one", | |
| 184 | show: "pull-12", | |
| 185 | path: "/settings?tab=2", | |
| 186 | }), | |
| 187 | ), | |
| 188 | ).toEqual({ repo: "alice/one", pull: 12, path: "/settings?tab=2" }); | |
| 189 | expect( | |
| 190 | paneAtOf(new URLSearchParams({ repo: "not a repo", show: "pull-0" })), | |
| 191 | ).toEqual({}); | |
| 192 | expect(paneQuery({ repo: "alice/one", live: true, path: "/a" })).toBe( | |
| 193 | "repo=alice%2Fone&show=live&path=%2Fa", | |
| 194 | ); | |
| 195 | }); | |
| 196 | }); | |
| 197 | ||
| 198 | describe("what it offers", () => { | |
| 199 | test("only repositories the person may read: the conversation's first, then their own apps", () => { | |
| 200 | const offered = paneOffer(app, alice, "alice/one").repos.map((r) => r.name); | |
| 201 | expect(offered[0]).toBe("alice/one"); | |
| 202 | expect([...offered].sort()).toEqual(["alice/one", "alice/two", "team/shared"]); | |
| 203 | // A member of the team: the team's app, offered as one they write. | |
| 204 | expect( | |
| 205 | paneOffer(app, carol, "team/shared").repos.map((r) => r.name), | |
| 206 | ).toEqual(["team/shared"]); | |
| 207 | // A stranger naming alice's private app: never offered, only their own. | |
| 208 | // A stranger naming a repository they may not read has no pane at all: nothing is offered or read. | |
| 209 | expect(paneOffer(app, bob, "alice/one").repos).toEqual([]); | |
| 210 | expect(paneOffer(app, bob, "team/shared").repos).toEqual([]); | |
| 211 | // On their own app, only their own: alice's and the team's are never offered. | |
| 212 | expect(paneOffer(app, bob, "bob/mine").repos.map((r) => r.name)).toEqual(["bob/mine"]); | |
| 213 | }); | |
| 214 | ||
| 215 | test("the page's frame-src is exactly the origins the bar offers, and a repository it does not offer is never framed", async () => { | |
| 216 | const mine = await page(alice, talkOn(alice, "alice/one")); | |
| 217 | expect(mine.frameSrc).toEqual( | |
| 218 | [live("alice/one"), live("alice/two"), live("team/shared")].sort(), | |
| 219 | ); | |
| 220 | expect(mine.html).toContain('<select name="repo" aria-label="Repository">'); | |
| 221 | expect(mine.html).toContain( | |
| 222 | '<option value="alice/one" selected>alice/one</option>', | |
| 223 | ); | |
| 224 | // bob asks for alice's private app by its address: his own is shown, alice's origin never named. | |
| 225 | const his = await page(bob, talkOn(bob, "bob/mine"), "?repo=alice/one"); | |
| 226 | expect(his.frameSrc).toEqual([live("bob/mine")]); | |
| 227 | expect(his.html).toContain('data-pane-of="bob/mine"'); | |
| 228 | expect(his.html).not.toContain("alice/one"); | |
| 229 | // carol, a member, may point the bar at the team's app. | |
| 230 | const member = await page( | |
| 231 | carol, | |
| 232 | talkOn(carol, "team/shared"), | |
| 233 | "?repo=team/shared", | |
| 234 | ); | |
| 235 | expect(member.html).toContain('data-pane-of="team/shared"'); | |
| 236 | }); | |
| 237 | }); | |
| 238 | ||
| 239 | describe("a picked pull", () => { | |
| 240 | test("whose preview is gone falls to Live with one line, and the pane's links and fragments keep the choice", async () => { | |
| 241 | const repo = repos["alice/two"]!; | |
| 242 | const pr = createIssue(app.db, { | |
| 243 | repoId: repo.id, | |
| 244 | authorId: alice.id, | |
| 245 | title: "Bigger button", | |
| 246 | body: "b", | |
| 247 | kind: "pull", | |
| 248 | }); | |
| 249 | if (!pr.ok) throw new Error("pull"); | |
| 250 | app.db.run( | |
| 251 | "INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid, merged_at) VALUES (?1, 'main', 'crew/big', '', '', ?2)", | |
| 252 | [pr.value.id, new Date().toISOString()], | |
| 253 | ); | |
| 254 | const n = pr.value.number; | |
| 255 | const { html } = await page( | |
| 256 | alice, | |
| 257 | talkOn(alice, "alice/two"), | |
| 258 | `?show=pull-${String(n)}&path=/settings`, | |
| 259 | ); | |
| 260 | expect(html).toContain(`#${String(n)} landed: Live has it now.`); | |
| 261 | expect(html).toContain('<input name="path" value="/settings"'); | |
| 262 | expect(html).toContain( | |
| 263 | `href="?pane=code&show=pull-${String(n)}&path=%2Fsettings"`, | |
| 264 | ); | |
| 265 | expect(html).toContain( | |
| 266 | `data-live-src="?pane=preview&show=pull-${String(n)}&path=%2Fsettings&fragment=pane"`, | |
| 267 | ); | |
| 268 | }); | |
| 269 | }); | |
| 270 | ||
| 271 | describe("the line under the bar", () => { | |
| 272 | test("Live: the default branch and what last landed; a pull: who made it, its title, checks and review", () => { | |
| 273 | const repo = repos["alice/one"]!; | |
| 274 | expect(paneInfo(app.db, repo, undefined)).toBe( | |
| 275 | `Live · ${repo.defaultBranch} · nothing landed yet`, | |
| 276 | ); | |
| 277 | const pr = createIssue(app.db, { | |
| 278 | repoId: repo.id, | |
| 279 | authorId: alice.id, | |
| 280 | title: "Dark theme", | |
| 281 | body: "b", | |
| 282 | kind: "pull", | |
| 283 | }); | |
| 284 | if (!pr.ok) throw new Error("pull"); | |
| 285 | app.db.run( | |
| 286 | "INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid) VALUES (?1, 'main', 'crew/dark', '', 'abc')", | |
| 287 | [pr.value.id], | |
| 288 | ); | |
| 289 | expect(paneInfo(app.db, repo, pr.value.number)).toBe( | |
| 290 | `alice · #${String(pr.value.number)} Dark theme · no checks yet · not reviewed yet`, | |
| 291 | ); | |
| 292 | app.db.run("UPDATE pull SET merged_at = ?1 WHERE issue_id = ?2", [ | |
| 293 | new Date().toISOString(), | |
| 294 | pr.value.id, | |
| 295 | ]); | |
| 296 | expect(paneInfo(app.db, repo, undefined)).toBe( | |
| 297 | `Live · ${repo.defaultBranch} · last landed #${String(pr.value.number)} Dark theme`, | |
| 298 | ); | |
| 299 | }); | |
| 300 | }); | |
| 301 | ||
| 302 | describe("Diff", () => { | |
| 303 | test("is what this conversation's work changed: the pulls its receipts' issues led to; a conversation that filed nothing here asks for a pick", async () => { | |
| 304 | const repo = repos["team/shared"]!; | |
| 305 | const issue = createIssue(app.db, { | |
| 306 | repoId: repo.id, | |
| 307 | authorId: alice.id, | |
| 308 | title: "Add a footer", | |
| 309 | body: "b", | |
| 310 | }); | |
| 311 | if (!issue.ok) throw new Error("issue"); | |
| 312 | createRun(app.db, { | |
| 313 | repoId: repo.id, | |
| 314 | kind: "agent", | |
| 315 | issueId: issue.value.id, | |
| 316 | reason: "assigned", | |
| 317 | branch: "crew/footer", | |
| 318 | }); | |
| 319 | const pr = createIssue(app.db, { | |
| 320 | repoId: repo.id, | |
| 321 | authorId: alice.id, | |
| 322 | title: "A footer", | |
| 323 | body: "Adds it", | |
| 324 | kind: "pull", | |
| 325 | }); | |
| 326 | if (!pr.ok) throw new Error("pull"); | |
| 327 | app.db.run( | |
| 328 | "INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid) VALUES (?1, 'main', 'crew/footer', '', '')", | |
| 329 | [pr.value.id], | |
| 330 | ); | |
| 331 | // A pull that only says it closes the issue counts too. | |
| 332 | const closes = createIssue(app.db, { | |
| 333 | repoId: repo.id, | |
| 334 | authorId: alice.id, | |
| 335 | title: "Footer, again", | |
| 336 | body: `Closes #${String(issue.value.number)}`, | |
| 337 | kind: "pull", | |
| 338 | }); | |
| 339 | if (!closes.ok) throw new Error("pull"); | |
| 340 | app.db.run( | |
| 341 | "INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid) VALUES (?1, 'main', 'crew/footer2', '', '')", | |
| 342 | [closes.value.id], | |
| 343 | ); | |
| 344 | expect(conversationPulls(app.db, repo.id, [issue.value.number])).toEqual([ | |
| 345 | closes.value.number, | |
| 346 | pr.value.number, | |
| 347 | ]); | |
| 348 | ||
| 349 | const filed = talkOn(alice, "team/shared", issue.value.number); | |
| 350 | const diff = (await page(alice, filed, "?pane=diff&fragment=pane")).html; | |
| 351 | expect(diff).toContain(`#${String(closes.value.number)}</a> Footer, again`); | |
| 352 | expect(diff).toContain(`#${String(pr.value.number)}</a> A footer`); | |
| 353 | // Captain-style: a conversation about the repository that filed nothing on it. | |
| 354 | const about = ( | |
| 355 | await page( | |
| 356 | alice, | |
| 357 | talkOn(alice, "team/shared"), | |
| 358 | "?pane=diff&fragment=pane", | |
| 359 | ) | |
| 360 | ).html; | |
| 361 | expect(about).toContain("Pick a pull above to see its change."); | |
| 362 | expect(about).not.toContain("A footer"); | |
| 363 | }); | |
| 364 | ||
| 365 | test("a conversation's receipts name their issues on one repository, each once", () => { | |
| 366 | const turns = [ | |
| 367 | { | |
| 368 | role: "tool" as const, | |
| 369 | text: JSON.stringify({ | |
| 370 | filed: true, | |
| 371 | done: true, | |
| 372 | receipt: { href: "/a/b/issues/3" }, | |
| 373 | }), | |
| 374 | }, | |
| 375 | { | |
| 376 | role: "tool" as const, | |
| 377 | text: JSON.stringify({ | |
| 378 | filed: true, | |
| 379 | done: true, | |
| 380 | receipt: { href: "/a/b/issues/3" }, | |
| 381 | }), | |
| 382 | }, | |
| 383 | { | |
| 384 | role: "tool" as const, | |
| 385 | text: JSON.stringify({ | |
| 386 | filed: true, | |
| 387 | done: true, | |
| 388 | receipt: { href: "/a/c/issues/4" }, | |
| 389 | }), | |
| 390 | }, | |
| 391 | { role: "agent" as const, text: "/a/b/issues/9" }, | |
| 392 | ]; | |
| 393 | expect(receiptIssues(turns, "a/b")).toEqual([3]); | |
| 394 | }); | |
| 395 | }); | |
| 396 | ||
| 397 | describe("a version the chosen repository does not offer", () => { | |
| 398 | test("falls to Live, as a form sent without the script carries the old repository's version beside a new one", async () => { | |
| 399 | const offer = paneOffer(app, alice, "alice/two"); | |
| 400 | // An unknown pull, and one of another repository, are dropped; Live is shown, with no line. | |
| 401 | expect(paneChoice(app.db, offer, { repo: "alice/one", pull: 9_999 }, "alice/two")).toEqual({ repo: "alice/one" }); | |
| 402 | expect(paneChoice(app.db, offer, { repo: "bob/mine", pull: 1 }, "alice/two")).toEqual({}); | |
| 403 | const { html } = await page(alice, talkOn(alice, "alice/two"), "?repo=alice/one&show=pull-9999"); | |
| 404 | expect(html).toContain('data-pane-of="alice/one"'); | |
| 405 | expect(html).toContain('<option value="live" selected>Live</option>'); | |
| 406 | expect(html).not.toContain('class="talk-pane-fell'); | |
| 407 | }); | |
| 408 | }); | |
| 409 | ||
| 410 | describe("the terminal's pane", () => { | |
| 411 | test("picks the repository, the version and a path from what the page's bar offers, refuses a hostile path, and shows this conversation's Diff", async () => { | |
| 412 | const repo = repos["team/shared"]!; | |
| 413 | const issue = createIssue(app.db, { repoId: repo.id, authorId: alice.id, title: "A header", body: "b" }); | |
| 414 | if (!issue.ok) throw new Error("issue"); | |
| 415 | const pr = createIssue(app.db, { repoId: repo.id, authorId: alice.id, title: "The header", body: `Closes #${String(issue.value.number)}`, kind: "pull" }); | |
| 416 | if (!pr.ok) throw new Error("pull"); | |
| 417 | app.db.run("INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid) VALUES (?1, 'main', 'crew/header', '', '')", [pr.value.id]); | |
| 418 | // The newest conversation: the one the terminal opens first. | |
| 419 | talkOn(alice, "team/shared", issue.value.number); | |
| 420 | const terminal = headlessTerminal(140, 40); | |
| 421 | const { app: tui } = createForgeApp(app, { sshUsername: "alice", remoteAddress: "127.0.0.1", fingerprint: "SHA256:test", user: alice }, { terminal, exitHooks: false }); | |
| 422 | tui.start(); | |
| 423 | const press = async (...keys: string[]) => { | |
| 424 | for (const k of keys) { | |
| 425 | terminal.feed(k); | |
| 426 | await settleApp(tui); | |
| 427 | } | |
| 428 | }; | |
| 429 | await press(":", "Conversations", "\r", "\r", "p"); | |
| 430 | let text = tui.screen.toText(); | |
| 431 | expect(text).toContain("team/shared · Production"); | |
| 432 | expect(text).toContain("o picks the repository, w the version, l a path."); | |
| 433 | // Diff: the pull this conversation's issue led to, from the same core read as the page's tab. | |
| 434 | expect(text).toContain(`#${String(pr.value.number)} The header · open /team/shared/pulls/${String(pr.value.number)}`); | |
| 435 | // Another repository the bar offers. | |
| 436 | await press("o", "/", "alice/two", "\r", "\r"); | |
| 437 | text = tui.screen.toText(); | |
| 438 | expect(text).toContain("alice/two · Production"); | |
| 439 | // Live, then a path. | |
| 440 | await press("w", "\r"); | |
| 441 | await press("l"); | |
| 442 | for (const ch of "/settings") terminal.feed(ch); | |
| 443 | await press("\r"); | |
| 444 | expect(tui.screen.toText()).toContain("alice/two · Production · /settings"); | |
| 445 | // A path that could leave the app is refused in the pane's own words, and nothing moves. | |
| 446 | await press("l"); | |
| 447 | // The box holds /settings; what is typed after it climbs out of the app. | |
| 448 | for (const ch of "/../../x") terminal.feed(ch); | |
| 449 | await press("\r"); | |
| 450 | expect(tui.screen.toText()).toContain("A path in the pane is a path on the app"); | |
| 451 | }); | |
| 452 | }); |
new above
offline