From 0f379f0a3378b8da0a5a32c10061180d56e7347a Mon Sep 17 00:00:00 2001 From: Kit Langton Date: Sat, 24 Oct 2026 13:03:57 -0700 Subject: [PATCH] feat(app): show a turn's web sources behind the Sources experiment (#58307) --- .../extension-host.fixture.tsx | 21 +- .../sources-experiment.spec.ts | 98 +++++ packages/app/src/runtime/extension/render.tsx | 15 +- packages/app/src/session/story-model.tsx | 12 +- .../src/session/timeline/sources.stories.tsx | 339 ++++++++++++++++++ packages/gui-extensions/README.md | 2 + packages/gui-extensions/src/main.ts | 2 + packages/gui-extensions/src/renderer.ts | 3 + .../gui-extensions/src/sources/i18n/en.ts | 3 + packages/gui-extensions/src/sources/index.ts | 10 + packages/gui-extensions/src/sources/panel.tsx | 29 ++ .../gui-extensions/src/sources/renderer.tsx | 94 +++++ packages/session-ui/package.json | 4 + packages/session-ui/src/context/sources.tsx | 34 ++ .../src/message/current-message.tsx | 5 + .../src/message/message-content.tsx | 86 +++-- packages/session-ui/src/sources/experiment.ts | 57 +++ packages/session-ui/src/sources/site-icon.tsx | 53 +++ .../session-ui/src/sources/sources-chip.tsx | 57 +++ .../session-ui/src/sources/sources-panel.tsx | 193 ++++++++++ .../session-ui/src/sources/sources-title.tsx | 21 ++ packages/session-ui/src/sources/sources.css | 266 ++++++++++++++ .../session-ui/src/sources/sources.test.ts | 172 +++++++++ packages/session-ui/src/sources/sources.ts | 141 ++++++++ .../src/timeline/session-timeline-row.tsx | 20 ++ packages/ui/src/i18n/en.ts | 9 + 26 files changed, 1710 insertions(+), 36 deletions(-) create mode 100644 packages/app/component-tests/sources-experiment.spec.ts create mode 100644 packages/app/src/session/timeline/sources.stories.tsx create mode 100644 packages/gui-extensions/src/sources/i18n/en.ts create mode 100644 packages/gui-extensions/src/sources/index.ts create mode 100644 packages/gui-extensions/src/sources/panel.tsx create mode 100644 packages/gui-extensions/src/sources/renderer.tsx create mode 100644 packages/session-ui/src/context/sources.tsx create mode 100644 packages/session-ui/src/sources/experiment.ts create mode 100644 packages/session-ui/src/sources/site-icon.tsx create mode 100644 packages/session-ui/src/sources/sources-chip.tsx create mode 100644 packages/session-ui/src/sources/sources-panel.tsx create mode 100644 packages/session-ui/src/sources/sources-title.tsx create mode 100644 packages/session-ui/src/sources/sources.css create mode 100644 packages/session-ui/src/sources/sources.test.ts create mode 100644 packages/session-ui/src/sources/sources.ts diff --git a/packages/app/component-tests/extension-host.fixture.tsx b/packages/app/component-tests/extension-host.fixture.tsx index 7bffd9ea6e0..1c3aaeb1c7b 100644 --- a/packages/app/component-tests/extension-host.fixture.tsx +++ b/packages/app/component-tests/extension-host.fixture.tsx @@ -1,3 +1,4 @@ +import type { SessionMessageInfo } from "@opencode/client/promise" import { DialogProvider } from "@opencode/ui/context/dialog" import { Panel, @@ -389,7 +390,12 @@ function RealHost(props: ParentProps<{ definitions: readonly Definition[]; captu * does: a new session object each time, delivered through the same reactive inputs, while the one screen object * `ctx.screen` returns follows the route. Its composer records which session each attachment reached. */ -export function mountSessionRegion(input: { definitions: readonly Definition[]; active: string }) { +export function mountSessionRegion(input: { + definitions: readonly Definition[] + active: string + /** The routed session's messages, as its server holds them. */ + messages?: readonly SessionMessageInfo[] +}) { const container = document.createElement("div") document.body.appendChild(container) const hosts: ExtensionHost[] = [] @@ -397,9 +403,18 @@ export function mountSessionRegion(input: { definitions: readonly Definition[]; const attached: string[] = [] const object = (id: string) => { - const value = { key: `fixture\n${id}`, id, tab: id, pending: false, location: { directory: "/repo" }, visit: {} } + const value = { + key: `fixture\n${id}`, + id, + tab: id, + pending: false, + location: { directory: "/repo" }, + visit: {}, + server: { data: { session: { message: { list: () => input.messages ?? [] } } } }, + } - // SAFETY: the region and slot pass the object through; the test's extension reads only `key` and `id`. + // SAFETY: the region and slot pass the object through; the test's extensions read only `key`, `id` and the + // server's message list. // oxlint-disable-next-line anti-slop/no-chained-type-assertions -- see SAFETY above return value as unknown as MountedSession } diff --git a/packages/app/component-tests/sources-experiment.spec.ts b/packages/app/component-tests/sources-experiment.spec.ts new file mode 100644 index 00000000000..91fa0abc459 --- /dev/null +++ b/packages/app/component-tests/sources-experiment.spec.ts @@ -0,0 +1,98 @@ +import type { SessionMessageInfo } from "@opencode/client/promise" +import type { MotionKnob } from "@opencode/ui/motion-tuning" +import { expect, sourceURL, story } from "../../stories/playwright/story" + +const source = (path: string) => sourceURL(new URL(path, import.meta.url)) + +const modules = { + fixture: source("./extension-host.fixture.tsx"), + definition: source("../../gui-extensions/src/sources/index.ts"), + renderer: source("../../gui-extensions/src/sources/renderer.tsx"), + tuning: source("../../ui/src/motion-tuning.ts"), +} + +// One turn: the user message the panel's tab is keyed by, and a reply that searched the web. +const messages: SessionMessageInfo[] = [ + { id: "main", type: "user", text: "Who made One Million Checkboxes?", time: { created: 1 } }, + { + id: "msg_reply", + type: "assistant", + agent: "build", + model: { id: "claude-sonnet-4", providerID: "anthropic" }, + time: { created: 2, completed: 3 }, + content: [ + { + type: "tool", + id: "call_search", + name: "websearch", + state: { + status: "completed", + input: { query: "one million checkboxes" }, + content: [ + { type: "text", text: "## [One Million Checkboxes](https://eieio.games/blog/one-million-checkboxes)" }, + ], + }, + time: { created: 2, ran: 2, completed: 3 }, + }, + ], + }, +] + +story.beforeEach(async ({ mount }) => { + // Any story loads the app; the fixture mounts the real host beside it. + await mount("ui-line-comment--editor") +}) + +story("turning the Sources experiment off closes its open panel and loads no more site icons", async ({ page }) => { + // Site icons stay off the network, and load, so each stays an image until the experiment turns off. + await page.route("**/favicon.ico", (route) => + route.fulfill({ + contentType: "image/png", + body: Buffer.from( + "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==", + "base64", + ), + }), + ) + + const result = await page.evaluate( + async (input) => { + const [{ mountSessionRegion, until }, { default: definition }, { default: renderer }, { motionTuning }] = + await Promise.all([ + import(input.modules.fixture), + import(input.modules.definition), + import(input.modules.renderer), + import(input.modules.tuning), + ]) + + const knob = (id: string) => motionTuning.list().find((item: MotionKnob) => item.id === id) + motionTuning.set(knob("sources.enabled"), true) + motionTuning.set(knob("sources.icons"), "site") + + const host: { container: HTMLElement; unmount: () => void } = mountSessionRegion({ + active: "sources:main", + messages: JSON.parse(input.messages), + definitions: [{ ...definition, renderer: async () => ({ default: renderer }) }], + }) + + const panel = () => host.container.querySelector('[data-slot="session-sources-panel"]') + const icons = () => host.container.querySelectorAll('[data-slot="site-icon-image"]').length + + await until(() => !!panel()?.querySelector('[data-slot="sources-step-row"]')) + panel()?.querySelector('[data-slot="sources-step-row"]')?.click() + await until(() => icons() > 0) + const open = { panel: !!panel(), icons: icons() } + + motionTuning.set(knob("sources.enabled"), false) + await until(() => !panel()) + const off = { panel: !!panel(), icons: icons() } + host.unmount() + + return { open, off } + }, + // As JSON: the messages' type is too deep for `evaluate`'s serializable argument check. + { modules, messages: JSON.stringify(messages) }, + ) + + expect(result).toEqual({ open: { panel: true, icons: 1 }, off: { panel: false, icons: 0 } }) +}) diff --git a/packages/app/src/runtime/extension/render.tsx b/packages/app/src/runtime/extension/render.tsx index 8d5f0c255b9..a6f8278631a 100644 --- a/packages/app/src/runtime/extension/render.tsx +++ b/packages/app/src/runtime/extension/render.tsx @@ -1,6 +1,8 @@ import { createMemo, ErrorBoundary, For, onMount, Show, untrack, type JSX, type ParentProps } from "solid-js" import { Portal } from "solid-js/web" import { MarkdownProvider, useMarkdown } from "@opencode/session-ui/context/markdown" +import { SourcesProvider } from "@opencode/session-ui/context/sources" +import { sourcesExperiment } from "@opencode/session-ui/sources/experiment" import { ExtensionContext, Slot, Style, type MountedSession, type SlotMap } from "@opencode/gui-extensions/sdk" import { useExtensionHost } from "./host" @@ -64,7 +66,10 @@ export function ExtensionStyles() { ) } -/** Routes local markdown links in the session to extension LinkHandler contributions, keeping image loading. */ +/** + * Routes local markdown links in the session to extension LinkHandler contributions, keeping image loading. A turn's + * sources chip opens its turn through the Sources extension's handler. + */ export function ExtensionLinks(props: ParentProps<{ session: MountedSession }>) { const host = useExtensionHost() const markdown = useMarkdown() @@ -76,7 +81,13 @@ export function ExtensionLinks(props: ParentProps<{ session: MountedSession }>) // Untracked: markdown calls this inside its render effect, which must not follow the routed session. localFileExists={(href) => untrack(() => host.links.exists({ href, session: props.session }))} > - {props.children} + void host.links.open({ href: from, origin: "sources", session: props.session })} + > + {props.children} + ) } diff --git a/packages/app/src/session/story-model.tsx b/packages/app/src/session/story-model.tsx index 0724c6fc0f2..1914a6d2b0a 100644 --- a/packages/app/src/session/story-model.tsx +++ b/packages/app/src/session/story-model.tsx @@ -20,7 +20,7 @@ import type { ComposerPersistedState } from "@/composer/types" import { Button } from "@opencode/ui/button" import { Icon } from "@opencode/ui/icon" import { QueryClient, QueryClientProvider } from "@tanstack/solid-query" -import { Show, type ComponentProps } from "solid-js" +import { children, Show, type ComponentProps, type JSX } from "solid-js" import { createStore } from "solid-js/store" import { useLanguage } from "@/runtime/i18n/language" import type { WebSearchRequestModel } from "./requests/websearch" @@ -96,6 +96,8 @@ export type SessionPreviewProps = { onStop?: (item: SessionActivityItem) => void } timelineDetail?: TimelineDetail + /** Content for the side panel, shown beside the timeline while present. */ + side?: JSX.Element } export function SessionPreview(props: SessionPreviewProps) { @@ -183,6 +185,7 @@ export function createPromptController(input: { function SessionSurfaceState(props: SessionPreviewProps & { onReset: () => void }) { const language = useLanguage() + const side = children(() => props.side) const [state, setState] = createStore<{ activity: string @@ -316,7 +319,12 @@ function SessionSurfaceState(props: SessionPreviewProps & { onReset: () => void composer={} /> - + + + +