diff --git a/apps/mobile/src/features/threads/ThreadFeed.tsx b/apps/mobile/src/features/threads/ThreadFeed.tsx index 83b0bef9c0228..188d253b2225d 100644 --- a/apps/mobile/src/features/threads/ThreadFeed.tsx +++ b/apps/mobile/src/features/threads/ThreadFeed.tsx @@ -194,6 +194,7 @@ import { import { fileChipMenu, resolveFileChipTarget, type FileChipAction } from "./fileChipMenu"; import { useFileChipShare } from "./useFileChipShare"; import { + MarkdownImageAssetScopeContext, MarkdownImageAvailableWidthContext, ThreadMarkdownImage, ThreadMarkdownImageUnavailable, @@ -1500,14 +1501,18 @@ function renderFeedEntry( > {messages.map((reasoningMessage) => ( - + value={reasoningMessage.id} + > + + ))} @@ -1697,14 +1702,16 @@ function renderFeedEntry( > {renderedText.trim().length > 0 ? ( - + + + ) : null} {attachments.map((attachment) => { diff --git a/apps/mobile/src/features/threads/ThreadMarkdownImage.tsx b/apps/mobile/src/features/threads/ThreadMarkdownImage.tsx index a67225f1a3364..db42830b78118 100644 --- a/apps/mobile/src/features/threads/ThreadMarkdownImage.tsx +++ b/apps/mobile/src/features/threads/ThreadMarkdownImage.tsx @@ -31,6 +31,13 @@ import { */ export const MarkdownImageAvailableWidthContext = createContext(0); +/** + * The message or work-log row that shows these images. Each one signs its own + * URL, so an agent that rewrites a file and shows it again shows the new file, + * not the cached one. + */ +export const MarkdownImageAssetScopeContext = createContext(undefined); + export function ThreadMarkdownImageView(props: { readonly uri: string | null; readonly sourceKey: string; @@ -184,7 +191,11 @@ export function ThreadMarkdownImage(props: { readonly actionsSource?: MediaActionsSource; readonly onPressPreview: (source: FilePreviewSource) => void; }) { - const assetUrl = useAssetUrlState(props.environmentId, props.resource); + const assetUrl = useAssetUrlState( + props.environmentId, + props.resource, + useContext(MarkdownImageAssetScopeContext), + ); return ( - {props.renderImage({ href: viewedImagePath, alt: null, title: null })} + + {props.renderImage({ href: viewedImagePath, alt: null, title: null })} + ) : null} Promise { const connection = usePreparedConnection(environmentId); const httpBaseUrl = connection._tag === "Some" ? connection.value.httpBaseUrl : null; @@ -44,10 +47,10 @@ export function useAssetUrlRefresh( }); return useCallback(async () => { if (environmentId === null || resource === null || httpBaseUrl === null) return null; - const result = await refresh({ environmentId, input: { resource } }); + const result = await refresh({ environmentId, input: { resource, scope } }); if (result._tag === "Failure") throw squashAtomCommandFailure(result); return resolveAssetUrl(httpBaseUrl, result.value.relativeUrl); - }, [environmentId, resource, refresh, httpBaseUrl]); + }, [environmentId, resource, scope, refresh, httpBaseUrl]); } export function useAssetUrls( diff --git a/apps/web/src/components/ChatMarkdown.tsx b/apps/web/src/components/ChatMarkdown.tsx index 4a1f616aed3b7..1bbde2d9f78a6 100644 --- a/apps/web/src/components/ChatMarkdown.tsx +++ b/apps/web/src/components/ChatMarkdown.tsx @@ -231,6 +231,9 @@ interface ChatMarkdownProps { text nests under the heading that introduces it, such as a chat message's author. Rendered tags and their styling are unchanged. */ headingLevelOffset?: number | undefined; + /** Gives this text's workspace images their own signed URLs, such as per chat message. An agent + that rewrites a file and shows it again then shows the new file, not the cached one. */ + assetScope?: string | undefined; } export interface ChatMarkdownContextReference { @@ -1654,9 +1657,11 @@ export const ChatMarkdownAssetImage = memo(function ChatMarkdownAssetImage(props readonly fallbackSrc?: string | undefined; readonly workspaceRoot?: string | undefined; readonly onImageExpand?: ((preview: ExpandedImagePreview) => void) | undefined; + /** See `ChatMarkdownProps.assetScope`. */ + readonly assetScope?: string | undefined; }) { - const assetUrl = useAssetUrlState(props.environmentId, props.resource); - const refreshAssetUrl = useAssetUrlRefresh(props.environmentId, props.resource); + const assetUrl = useAssetUrlState(props.environmentId, props.resource, props.assetScope); + const refreshAssetUrl = useAssetUrlRefresh(props.environmentId, props.resource, props.assetScope); const resource = props.resource; const path = resource._tag === "media-file" @@ -1694,7 +1699,7 @@ export const ChatMarkdownAssetImage = memo(function ChatMarkdownAssetImage(props name: props.alt || (props.kind ?? "image"), src, ...(fallbackSrc === undefined - ? { asset: { environmentId: props.environmentId, resource } } + ? { asset: { environmentId: props.environmentId, resource, scope: props.assetScope } } : {}), ...(reference ? { reference } : {}), ...(relativePath && (resource._tag === "media-file" || resource._tag === "workspace-file") @@ -2302,6 +2307,7 @@ function useChatMarkdownState({ renderContextReference, headingLevelOffset = 0, githubMedia = false, + assetScope, }: ChatMarkdownProps) { const { resolvedTheme } = useTheme(); const [localMediaPreview, setLocalMediaPreview] = useState(null); @@ -2688,6 +2694,7 @@ function useChatMarkdownState({ const componentState = useMemo( () => ({ + assetScope, cwd, diffThemeName, environmentId, @@ -2719,6 +2726,7 @@ function useChatMarkdownState({ updateThreadPullRequestLink, }), [ + assetScope, cwd, diffThemeName, environmentId, @@ -3132,6 +3140,7 @@ const CHAT_MARKDOWN_COMPONENTS = { }, img: function MarkdownImage({ node, title, src, alt, ...props }) { const { + assetScope, expandMedia, cwd, environmentId, @@ -3246,6 +3255,7 @@ const CHAT_MARKDOWN_COMPONENTS = { threadId: threadRef.threadId, path: imageSource.path, }} + assetScope={assetScope} alt={altText} kind={kind} copyMarkdown={copyMarkdown} diff --git a/apps/web/src/components/chat/ExpandedImageDialog.tsx b/apps/web/src/components/chat/ExpandedImageDialog.tsx index f750693b6494c..de95e5770a4fd 100644 --- a/apps/web/src/components/chat/ExpandedImageDialog.tsx +++ b/apps/web/src/components/chat/ExpandedImageDialog.tsx @@ -44,8 +44,16 @@ function ExpandedMediaFailure({ children }: { children: ReactNode }) { function ExpandedVideo({ item }: { readonly item: ExpandedImageItem }) { const asset = item.actionsSource?.asset; - const assetUrl = useAssetUrlState(asset?.environmentId ?? null, asset?.resource ?? null); - const refreshAssetUrl = useAssetUrlRefresh(asset?.environmentId ?? null, asset?.resource ?? null); + const assetUrl = useAssetUrlState( + asset?.environmentId ?? null, + asset?.resource ?? null, + asset?.scope, + ); + const refreshAssetUrl = useAssetUrlRefresh( + asset?.environmentId ?? null, + asset?.resource ?? null, + asset?.scope, + ); const src = asset ? assetUrl._tag === "Success" ? assetUrl.url + (item.srcFragment ?? "") diff --git a/apps/web/src/components/chat/MessagesTimeline.tsx b/apps/web/src/components/chat/MessagesTimeline.tsx index 8ac769f33e7fb..2f7e437ee7c85 100644 --- a/apps/web/src/components/chat/MessagesTimeline.tsx +++ b/apps/web/src/components/chat/MessagesTimeline.tsx @@ -2397,6 +2397,7 @@ function AssistantTimelineRow({ row }: { row: Extract void; } diff --git a/packages/client-runtime/src/state/assets.test.ts b/packages/client-runtime/src/state/assets.test.ts index bd266f2616754..8834e91b8445c 100644 --- a/packages/client-runtime/src/state/assets.test.ts +++ b/packages/client-runtime/src/state/assets.test.ts @@ -168,6 +168,70 @@ describe("createAssetEnvironmentAtoms", () => { ); } + it.effect("signs a scoped query on its own and keeps the scope off the wire", () => + Effect.gen(function* () { + const environmentId = EnvironmentId.make("remote"); + const payloads: unknown[] = []; + const client = { + [WS_METHODS.assetsCreateUrl]: (payload: unknown) => { + payloads.push(payload); + return Effect.succeed({ + relativeUrl: `/api/assets/token-${payloads.length}/shot.png`, + expiresAt: 999999, + }); + }, + } as unknown as WsRpcProtocolClient; + const supervisor = EnvironmentSupervisor.of({ + target: new PrimaryConnectionTarget({ + environmentId, + label: "remote", + httpBaseUrl: "https://remote.test", + wsBaseUrl: "wss://remote.test", + }), + state: yield* SubscriptionRef.make({ + ...AVAILABLE_CONNECTION_STATE, + phase: "connected" as const, + }), + session: yield* SubscriptionRef.make(Option.some({ client } as RpcSession)), + prepared: yield* SubscriptionRef.make(Option.none()), + connect: Effect.void, + disconnect: Effect.void, + retryNow: Effect.void, + }); + const environments = EnvironmentRegistry.of({ + run: (_id, effect) => Effect.provideService(effect, EnvironmentSupervisor, supervisor), + followStream: (_id, stream) => + Stream.provideService(stream, EnvironmentSupervisor, supervisor), + } as EnvironmentRegistry["Service"]); + const registry = AtomRegistry.make(); + yield* Effect.addFinalizer(() => Effect.sync(() => registry.dispose())); + const assets = createAssetEnvironmentAtoms( + Atom.runtime(Layer.succeed(EnvironmentRegistry, environments)), + ); + const resource = { + _tag: "media-file" as const, + threadId: ThreadId.make("thread-1"), + path: "shot.png", + }; + const signedUrl = (scope?: string) => + AtomRegistry.getResult( + registry, + assets.createUrl({ environmentId, input: { resource, scope } }), + { suspendOnWaiting: true }, + ).pipe(Effect.map((result) => result.relativeUrl)); + + // Two messages that show one path each sign their own URL. + expect(yield* signedUrl("message-1")).toBe("/api/assets/token-1/shot.png"); + expect(yield* signedUrl("message-2")).toBe("/api/assets/token-2/shot.png"); + expect(yield* signedUrl("message-1")).toBe("/api/assets/token-1/shot.png"); + expect(payloads).toEqual([{ resource }, { resource }]); + // An absent scope shares the unscoped query. + expect(assets.createUrl({ environmentId, input: { resource, scope: undefined } })).toBe( + assets.createUrl({ environmentId, input: { resource } }), + ); + }).pipe(Effect.scoped), + ); + it("keys asset URL queries by environment and resource", () => { const runtime = Atom.runtime(Layer.empty) as unknown as Atom.AtomRuntime< EnvironmentRegistry, diff --git a/packages/client-runtime/src/state/assets.ts b/packages/client-runtime/src/state/assets.ts index 0729d1bfeaa51..2ae1da870969b 100644 --- a/packages/client-runtime/src/state/assets.ts +++ b/packages/client-runtime/src/state/assets.ts @@ -96,6 +96,13 @@ export function assetUrlStateFromResult( }; } +/** + * A signed URL request. `scope` stays on the client: it gives one rendering its own cached URL, + * such as one chat message or work-log row. A later message that shows a file the agent rewrote + * then signs a fresh URL, instead of reusing the earlier one that the browser already cached. + */ +export type AssetUrlQuery = AssetCreateUrlInput & { readonly scope?: string | undefined }; + export function createAssetEnvironmentAtoms( runtime: Atom.AtomRuntime, localMediaEnvironment?: Atom.Atom<{ @@ -103,7 +110,10 @@ export function createAssetEnvironmentAtoms( readonly httpBaseUrl: string; } | null>, ) { - const execute = Effect.fn("assets.createUrl")(function* (input: AssetCreateUrlInput) { + const execute = Effect.fn("assets.createUrl")(function* ({ + scope: _scope, + ...input + }: AssetUrlQuery) { const result = yield* request(WS_METHODS.assetsCreateUrl, input).pipe(Effect.result); if (Result.isSuccess(result)) return result.success; const error = result.failure;