Skip to content

Commit cdcd1e0

Browse files
committed
feat(es-app): show session PR references in sidebar (anomalyco#132)
1 parent 319e54e commit cdcd1e0

3 files changed

Lines changed: 112 additions & 8 deletions

File tree

‎packages/es-app/src/components/session-info.tsx‎

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
* this session references, and links shared in the conversation. */
44
import { createMemo, createResource, For, Show } from "solid-js"
55
import { A } from "@solidjs/router"
6-
import type { Part, Session } from "@opencode-ai/sdk/v2"
6+
import type { Message, Part, Session } from "@opencode-ai/sdk/v2"
77
import { es, type IssueRow } from "../api"
88
import { ago, linkUrl, useDashboard } from "../state"
99
import { refFromHref, ticketMatchesRef, ticketUrl } from "../ticket-url"
1010
import { docHref } from "../pages/doc"
1111
import { TicketIcon } from "./icons"
1212
import { rightOpen, rightWidth, toggleRight } from "../ui"
13-
import { PrList } from "./pr"
13+
import { Pr } from "./pr"
14+
import { prFromHref } from "../pr-matching"
1415

1516
const URL_RE = /https?:\/\/[^\s)\]}"'`>]+/g
1617
// #N / repo#N / owner/repo#N mentions and markdown-ish path tokens
@@ -29,6 +30,7 @@ const home = (p: string) => p.replace(/^\/Users\/[^/]+/, "~")
2930
export default function SessionInfo(props: {
3031
sessionID: string
3132
session?: Session
33+
messages: Message[]
3234
parts: Record<string, Part[]>
3335
}) {
3436
return (
@@ -71,6 +73,7 @@ function transcriptStrings(parts: Record<string, Part[]>): { texts: string[]; to
7173
function SessionInfoBody(props: {
7274
sessionID: string
7375
session?: Session
76+
messages: Message[]
7477
parts: Record<string, Part[]>
7578
}) {
7679
const { state, editspace, allProjects } = useDashboard()
@@ -91,7 +94,10 @@ function SessionInfoBody(props: {
9194
(name) => es.docs(name || undefined).catch(() => undefined),
9295
)
9396

94-
const scanned = createMemo(() => transcriptStrings(props.parts))
97+
const sessionParts = createMemo(() => Object.fromEntries(props.messages
98+
.filter((message) => message.sessionID === props.sessionID)
99+
.map((message) => [message.id, props.parts[message.id] ?? []])))
100+
const scanned = createMemo(() => transcriptStrings(sessionParts()))
95101

96102
// tracker tickets this session references: the lane's claimed issue plus
97103
// transcript mentions (#N for gh; issues/<feature>/NN-*.md pointers for
@@ -172,7 +178,7 @@ function SessionInfoBody(props: {
172178

173179
const links = createMemo(() => {
174180
const seen = new Map<string, string>()
175-
for (const parts of Object.values(props.parts)) {
181+
for (const parts of Object.values(sessionParts())) {
176182
for (const part of parts) {
177183
if (part.type !== "text") continue
178184
for (const raw of (part as any).text?.match(URL_RE) ?? []) {
@@ -185,6 +191,31 @@ function SessionInfoBody(props: {
185191
return [...seen.keys()].slice(-25).reverse()
186192
})
187193

194+
const linkedPrs = createMemo(() => {
195+
const found = new Map<string, { repo: string; number: number; url: string }>()
196+
for (const parts of Object.values(sessionParts())) {
197+
for (const part of parts) {
198+
const text = part.type === "text" ? part.text : part.type === "tool" &&
199+
part.state.status === "completed" ? part.state.output : ""
200+
for (const raw of text.match(URL_RE) ?? []) {
201+
const pr = prFromHref(raw.replace(/[.,;:]+$/, ""))
202+
if (pr) found.set(pr.url, pr)
203+
}
204+
}
205+
}
206+
return found
207+
})
208+
209+
const prs = createMemo(() => {
210+
const found = new Map(linkedPrs())
211+
const rich = new Map<string, { url: string }>((thread()?.prs ?? []).flatMap((pr: { url: string }) => {
212+
const ref = prFromHref(pr.url)
213+
return ref ? [[ref.url, pr] as const] : []
214+
}))
215+
for (const [url, pr] of rich) if (!found.has(url)) found.set(url, prFromHref(url)!)
216+
return [...found.values()].map((ref) => ({ ref, data: rich.get(ref.url) }))
217+
})
218+
188219
const tokens = () => (props.session as any)?.tokens
189220

190221
return (
@@ -314,10 +345,16 @@ function SessionInfoBody(props: {
314345
</div>
315346
</Show>
316347

317-
<Show when={thread()?.prs?.length}>
348+
<Show when={prs().length}>
318349
<div class="info-section">
319-
<div class="info-heading">open PRs</div>
320-
<PrList prs={thread().prs} compact />
350+
<div class="info-heading">PRs referenced</div>
351+
<For each={prs()}>{(pr) => pr.data
352+
? <Pr pr={pr.data} compact />
353+
: <a class="pr-title-row" href={linkUrl(pr.ref.url)} target="_blank" rel="noopener noreferrer"
354+
title={`${pr.ref.repo}#${pr.ref.number}`}>
355+
<span class="pr-number" dir="ltr">{pr.ref.repo}#{pr.ref.number}</span>
356+
</a>}
357+
</For>
321358
</div>
322359
</Show>
323360

‎packages/es-app/src/pages/session.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1009,7 +1009,7 @@ function SessionView(props: { sessionID: string; directory: string }) {
10091009
<Show when={rightOpen()}>
10101010
<div class="drag-handle" onMouseDown={(e) => startDrag("right", e)} />
10111011
</Show>
1012-
<SessionInfo sessionID={sessionID} session={session()} parts={live.data.part} />
1012+
<SessionInfo sessionID={sessionID} session={session()} messages={messages()} parts={live.data.part} />
10131013
</main>
10141014
)
10151015
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { createRequire } from "node:module"
2+
import { mkdtemp, rm } from "node:fs/promises"
3+
import { tmpdir } from "node:os"
4+
import path from "node:path"
5+
import { startPanelPreview } from "./composer-panel-preview.mjs"
6+
import { directory } from "./composer-fixture.mjs"
7+
8+
const { chromium, _electron, expect } = createRequire(new URL("../../app/package.json", import.meta.url))("@playwright/test")
9+
const preview = await startPanelPreview()
10+
const fixture = preview.fixture
11+
fixture.messages[1].parts[0].text = [
12+
...Array.from({ length: 31 }, (_, index) => `https://github.com/example/repo/pull/${index + 1}`),
13+
"[duplicate](https://github.com/EXAMPLE/REPO/pull/1/files?diff=split#top)",
14+
"https://duo.fyi/ink/https://github.com/example/repo/pull/2?tab=files#review",
15+
"https://github.com/example/repo/issues/900 https://github.com.evil.test/example/repo/pull/901",
16+
"https://github.com/example/repo/pull/902bad",
17+
].join("\n\n")
18+
fixture.messages[1].parts.push({ id: "tool_prs", messageID: "msg_2", sessionID: "ses_a", type: "tool", tool: "read", callID: "read_prs",
19+
state: { status: "completed", input: {}, output: "Result: https://github.com/tools/output/pull/42#discussion", title: "Read", time: { start: 1, end: 2 } } })
20+
fixture.messagesB = [{ info: { ...fixture.messages[0].info, id: "other", sessionID: "ses_b" }, parts: [
21+
{ ...fixture.messages[0].parts[0], id: "other_part", messageID: "other", sessionID: "ses_b", text: "https://github.com/other/repo/pull/999" },
22+
] }]
23+
const state = await (await fetch(`${fixture.url}/api/state`)).json()
24+
state.threads[0].prs = [{ repo: "example/repo", number: 1, url: "https://github.com/example/repo/pull/1", title: "Merged PR", state: "merged" },
25+
{ repo: "example/repo", number: 2, url: "https://github.com/example/repo/pull/2", title: "Closed PR", state: "closed" }]
26+
const profile = process.env.ES_DESKTOP_EXECUTABLE ? await mkdtemp(path.join(tmpdir(), "sidebar-prs-")) : undefined
27+
const app = profile ? await _electron.launch({ executablePath: process.env.ES_DESKTOP_EXECUTABLE,
28+
env: { ...process.env, ELECTRON_RUN_AS_NODE: "", ES_DESKTOP_USER_DATA: profile, ES_DESKTOP_RENDERER: "",
29+
OPENCODE_URL: fixture.url, ES_DASHBOARD_URL: fixture.url, OPENCODE_SERVER_PASSWORD: "" },
30+
}) : undefined
31+
const browser = app ? undefined : await chromium.launch({ channel: "chrome", headless: true })
32+
const page = app ? await app.firstWindow() : await browser.newPage()
33+
if (app) {
34+
await page.waitForURL("http://127.0.0.1:*/")
35+
preview.origin = new URL(page.url()).origin
36+
preview.url = `${preview.origin}/session/ses_a?directory=${encodeURIComponent(directory)}`
37+
}
38+
await page.context().route("**/*", route => new URL(route.request().url()).origin === preview.origin ? route.continue() : route.abort())
39+
await page.route("**/api/state*", route => route.fulfill({ json: state }))
40+
const chips = page.locator(".session-info .pr-title-row")
41+
try {
42+
await page.goto(preview.url)
43+
await expect(chips).toHaveCount(32)
44+
await expect(page.locator(".info-heading", { hasText: "PRs referenced" })).toHaveCount(1)
45+
await expect(chips.nth(0)).toHaveAttribute("href", "https://duo.fyi/ink/https://github.com/example/repo/pull/1")
46+
await expect(chips.nth(0).locator(".icon-merged")).toHaveCount(1)
47+
await expect(chips.nth(1).locator(".icon-closed")).toHaveCount(1)
48+
await expect(chips.nth(2).locator(".pr-icon")).toHaveCount(0)
49+
await expect(chips.nth(31)).toHaveAttribute("href", "https://duo.fyi/ink/https://github.com/tools/output/pull/42")
50+
await expect(page.locator(".session-info .pr-title-row[href*='/999'], .session-info .pr-title-row[href*='/900'], .session-info .pr-title-row[href*='/901'], .session-info .pr-title-row[href*='/902']")).toHaveCount(0)
51+
await page.reload()
52+
await expect(chips).toHaveCount(32)
53+
const info = { ...fixture.messages[0].info, id: "live_pr", sessionID: "ses_a" }
54+
const part = { ...fixture.messages[0].parts[0], id: "live_part", messageID: info.id, text: "https://github.com/live/repo/pull/43" }
55+
fixture.messages.push({ info, parts: [part] })
56+
fixture.emit("message.updated", { info })
57+
fixture.emit("message.part.updated", { part })
58+
await expect(chips).toHaveCount(33)
59+
await expect(chips.filter({ hasText: "live/repo#43" })).toHaveAttribute("href", "https://duo.fyi/ink/https://github.com/live/repo/pull/43")
60+
expect(fixture.calls.filter(call => !["GET", "HEAD", "OPTIONS"].includes(call.method))).toEqual([])
61+
console.log(`PASS sidebar PRs ${app ? "native" : "browser"}: >25, metadata/unknown, tool output, strict links, other session, reload, live addition; zero mutations`)
62+
} finally {
63+
await app?.close()
64+
await browser?.close()
65+
await preview.close()
66+
if (profile) await rm(profile, { recursive: true, force: true })
67+
}

0 commit comments

Comments
 (0)