@devries-cameron20 / t3code Mirror · read-only / commits / e4b98f9d39
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 04:30 UTC

fix(assets): preserve the PR repository for GitHub media (#21631)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Nov 5, 2026 at 11:58 UTC e4b98f9d3967b94c518d1e2863b9626046590fd3
12 files changed +138 -37
apps/mobile/src/features/pull-requests/PullRequestMarkdown.tsx
+3 -1
@@ -57,7 +57,9 @@ export function PullRequestMarkdownRepository(props: {
57 projectId: target.input.projectId,
58 });
59 const cwd = project?.workspaceRoot;
60 - const media = cwd ? { environmentId: target.environmentId, cwd } : null;
60 + const media = cwd
61 + ? { environmentId: target.environmentId, cwd, repository: target.input.repository }
62 + : null;
63 return (
64 <AutolinksContext value={autolinks}>
65 <LineBreaksContext value={lineBreaks}>
apps/mobile/src/features/pull-requests/PullRequestMedia.tsx
+9 -17
@@ -1,12 +1,10 @@
1 import { createContext, useContext, useState } from "react";
2 -import { pullRequestMediaSource, pullRequestImageSource } from "./pullRequestMediaSource";
3 -import { useFilePreviewGallery } from "../../components/FilePreviewGallery";
4 -import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
2 import {
6 - SOURCE_CONTROL_MEDIA_ASSET,
7 - type AssetResource,
8 - type EnvironmentId,
9 -} from "@t3tools/contracts";
3 + pullRequestMediaSource,
4 + pullRequestImageSource,
5 + type PullRequestMediaLocation,
6 +} from "./pullRequestMediaSource";
7 +import { useFilePreviewGallery } from "../../components/FilePreviewGallery";
8 import { videoMimeType } from "@t3tools/shared/video";
9 import { Pressable, View } from "react-native";
10 import { Image } from "expo-image";
@@ -25,25 +23,19 @@ import {
23
24 export const PullRequestMediaVisibleContext = createContext(true);
25
28 -export const PullRequestMediaContext = createContext<{
29 - readonly environmentId: EnvironmentId;
30 - readonly cwd: string;
31 -} | null>(null);
26 +export const PullRequestMediaContext = createContext<PullRequestMediaLocation | null>(null);
27
28 export function usePullRequestMediaUrl(url: string) {
29 const context = useContext(PullRequestMediaContext);
35 - const fetchUrl = sourceControlClients.mediaUrl(url)?.fetchUrl;
36 - const resource: AssetResource | null =
37 - context && fetchUrl
38 - ? { _tag: SOURCE_CONTROL_MEDIA_ASSET, cwd: context.cwd, url: fetchUrl }
39 - : null;
30 + const source = pullRequestMediaSource(url, context);
31 + const resource = "resource" in source ? (source.resource ?? null) : null;
32 const asset = useAssetUrlState(context?.environmentId ?? null, resource);
33 const refresh = useRefreshAssetUrl(context?.environmentId ?? null, resource);
34 return {
35 uri: asset._tag === "Success" ? asset.url : resource ? null : url,
36 cacheKey: context && resource ? assetImageCacheKey(context.environmentId, resource) : undefined,
37 unavailable: resource !== null && asset._tag === "Failure",
46 - source: pullRequestMediaSource(url, context),
38 + source,
39 resolvePlaybackUri: resource ? async () => (await refresh()) ?? null : undefined,
40 };
41 }
apps/mobile/src/features/pull-requests/pullRequestMediaSource.test.ts
+9 -2
@@ -3,7 +3,11 @@ import { EnvironmentId } from "@t3tools/contracts";
3 import { filePreviewGallery } from "../../lib/filePreviewGallery";
4 import { pullRequestImageSource } from "./pullRequestMediaSource";
5
6 -const location = { environmentId: EnvironmentId.make("test"), cwd: "/repo" };
6 +const location = {
7 + environmentId: EnvironmentId.make("test"),
8 + cwd: "/repo",
9 + repository: "enterprise/private",
10 +};
11 const url = "https://github.com/user-attachments/assets/abc-123";
12
13 describe("PR media previews", () => {
@@ -13,6 +17,9 @@ describe("PR media previews", () => {
17 const selected = { ...second, sourceIdentifier: "tapped" };
18 const gallery = filePreviewGallery(selected, [first, second]);
19 expect(gallery).toEqual([first, selected]);
16 - expect(gallery.every((item) => "resource" in item)).toBe(true);
20 + expect(gallery).toMatchObject([
21 + { resource: { url, repository: location.repository } },
22 + { resource: { url: `${url}-second`, repository: location.repository } },
23 + ]);
24 });
25 });
apps/mobile/src/features/pull-requests/pullRequestMediaSource.ts
+7 -1
@@ -5,6 +5,7 @@ import type { FilePreviewItem } from "../../components/FilePreviewModal.types";
5 export interface PullRequestMediaLocation {
6 readonly environmentId: EnvironmentId;
7 readonly cwd: string;
8 + readonly repository?: string;
9 }
10
11 /** Keep the original resource identity so previewers can resolve and renew its capability. */
@@ -13,7 +14,12 @@ export function pullRequestMediaSource(url: string, location: PullRequestMediaLo
14 return location && fetchUrl
15 ? {
16 environmentId: location.environmentId,
16 - resource: { _tag: SOURCE_CONTROL_MEDIA_ASSET, cwd: location.cwd, url: fetchUrl },
17 + resource: {
18 + _tag: SOURCE_CONTROL_MEDIA_ASSET,
19 + cwd: location.cwd,
20 + url: fetchUrl,
21 + ...(location.repository ? { repository: location.repository } : {}),
22 + },
23 }
24 : { uri: url };
25 }
apps/mobile/src/lib/videoPreviewSource.ts
+1
@@ -63,6 +63,7 @@ export function mediaVideoThumbnailKey(source: MediaVideoPreviewSource): string
63 source.environmentId,
64 source.resource.cwd,
65 source.resource.url,
66 + source.resource.repository ?? "",
67 ]
68 : [
69 "media-video",
apps/server/src/assets/AssetAccess.test.ts
+25
@@ -1471,6 +1471,31 @@ describe("AssetAccess", () => {
1471 }).pipe(Effect.provide(layerTest)),
1472 );
1473
1474 + it.effect("preserves the change request repository through RPC decoding and signed media", () =>
1475 + Effect.gen(function* () {
1476 + const decodeAssetCreateUrlInput = Schema.decodeEffect(AssetCreateUrlInput);
1477 + const resource = {
1478 + _tag: SOURCE_CONTROL_MEDIA_ASSET,
1479 + cwd: "/personal-fork",
1480 + url: "https://github.com/user-attachments/assets/1a1842fb-6383-492f-873c-57aa0033fa6c",
1481 + repository: "enterprise/private",
1482 + };
1483 + const input = yield* decodeAssetCreateUrlInput({ resource });
1484 + const issued = yield* issueAssetUrl(input);
1485 + const suffix = issued.relativeUrl.slice(`${ASSET_ROUTE_PREFIX}/`.length);
1486 + const separator = suffix.indexOf("/");
1487 + const resolved = yield* resolveAsset(suffix.slice(0, separator), suffix.slice(separator + 1));
1488 +
1489 + expect(resolved).toEqual({
1490 + kind: "source-control-media",
1491 + url: resource.url,
1492 + cwd: resource.cwd,
1493 + repository: resource.repository,
1494 + expiresAt: issued.expiresAt,
1495 + });
1496 + }).pipe(Effect.provide(layerTest)),
1497 + );
1498 +
1499 it.effect("serves a host's credentialed media through that host's driver", () => {
1500 const fetched: Array<{ url: string; headers: Readonly<Record<string, string>> }> = [];
1501 const url = "https://github.com/owner/repo/blob/main/docs/shot.png";
apps/server/src/assets/AssetAccess.ts
+6 -7
@@ -169,6 +169,7 @@ const AssetClaimsSchema = Schema.Union([
169 /** A host's `fetchUrl`, narrowed at mint time; the signature is what keeps it there. */
170 url: Schema.String,
171 cwd: Schema.String,
172 + repository: Schema.optionalKey(Schema.String),
173 expiresAt: Schema.Number,
174 }),
175 ]);
@@ -197,6 +198,7 @@ export type ResolvedAsset =
198 readonly kind: "source-control-media";
199 readonly url: string;
200 readonly cwd: string;
201 + readonly repository?: string;
202 readonly expiresAt: number;
203 };
204
@@ -830,11 +832,12 @@ export const issueAssetUrl = Effect.fn("AssetAccess.issueAssetUrl")(function* (i
832 if (media === null) {
833 return yield* new AssetSourceControlMediaUrlValidationError({});
834 }
835 + const { _tag, ...resource } = input.resource;
836 claims = {
837 version: 1,
838 kind: SOURCE_CONTROL_MEDIA_ASSET,
839 + ...resource,
840 url: media.fetchUrl,
837 - cwd: input.resource.cwd,
841 expiresAt,
842 };
843 fileName = media.fileName;
@@ -944,12 +947,8 @@ export const resolveAsset = Effect.fn("AssetAccess.resolveAsset")(function* (
947 }
948
949 if (claims.kind === SOURCE_CONTROL_MEDIA_ASSET) {
947 - return {
948 - kind: "source-control-media",
949 - url: claims.url,
950 - cwd: claims.cwd,
951 - expiresAt: claims.expiresAt,
952 - } satisfies ResolvedAsset;
950 + const { version: _version, ...media } = claims;
951 + return { ...media, kind: "source-control-media" } satisfies ResolvedAsset;
952 }
953
954 if (claims.kind === "tool-output-image") {
apps/web/src/components/ChatMarkdown.tsx
+11 -1
@@ -248,6 +248,8 @@ interface ChatMarkdownProps {
248 /** Loads media a source control host keeps behind a credential through the server, which holds
249 `cwd`'s; without it a private repository's uploads load unauthenticated and 404. */
250 sourceControlMedia?: boolean | undefined;
251 + /** The change request's repository, when it can differ from the checkout at `cwd`. */
252 + sourceControlMediaRepository?: string | undefined;
253 headingLevelOffset?: number | undefined;
254 }
255
@@ -494,6 +496,7 @@ function useChatMarkdownState({
496 renderContextReference,
497 headingLevelOffset = 0,
498 sourceControlMedia = false,
499 + sourceControlMediaRepository,
500 }: ChatMarkdownProps) {
501 const resolvedTheme = useRenderedAppearance();
502 const markdownRef = useRef<HTMLElement>(null);
@@ -891,6 +894,7 @@ function useChatMarkdownState({
894 expandMedia,
895 fileLinkChip,
896 sourceControlMedia,
897 + sourceControlMediaRepository,
898 renderContextReference,
899 headingLevelOffset,
900 imageBaseDir,
@@ -1365,6 +1369,7 @@ const CHAT_MARKDOWN_COMPONENTS = {
1369 cwd,
1370 environmentId,
1371 sourceControlMedia,
1372 + sourceControlMediaRepository,
1373 imageBaseDir,
1374 threadRef,
1375 workspaceMutationId,
@@ -1451,7 +1456,12 @@ const CHAT_MARKDOWN_COMPONENTS = {
1456 return (
1457 <ChatMarkdownAssetImage
1458 environmentId={environmentId}
1454 - resource={{ _tag: SOURCE_CONTROL_MEDIA_ASSET, cwd, url: hostMediaUrl }}
1459 + resource={{
1460 + _tag: SOURCE_CONTROL_MEDIA_ASSET,
1461 + cwd,
1462 + url: hostMediaUrl,
1463 + ...(sourceControlMediaRepository ? { repository: sourceControlMediaRepository } : {}),
1464 + }}
1465 alt={altText}
1466 kind={kind}
1467 copyMarkdown={copyMarkdown}
apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx
+2 -1
@@ -236,13 +236,14 @@ function resolvePullRequestReference(
236 }
237
238 function pullRequestMarkdownContext(
239 - detail: Pick<PullRequestDetail, "provider" | "url"> | null,
239 + detail: Pick<PullRequestDetail, "provider" | "url" | "repository"> | null,
240 threadRef: ScopedThreadRef | null,
241 ) {
242 return {
243 remarkPlugins: pullRequestAutolinkPlugins(detail),
244 lineBreaks: sourceControlClients.find(detail?.provider ?? "")?.hardLineBreaks ?? false,
245 threadRef,
246 + repository: detail?.repository ?? null,
247 };
248 }
249
apps/web/src/components/pullRequest/PullRequestMarkdown.browser.test.tsx
+49 -5
@@ -1,23 +1,41 @@
1 import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
2 -import { EnvironmentId } from "@t3tools/contracts";
3 -import { expect, it } from "vite-plus/test";
2 +import { type AssetResource, EnvironmentId } from "@t3tools/contracts";
3 +import { expect, it, vi } from "vite-plus/test";
4
5 import { render } from "../../../test/render";
6 import { PullRequestMarkdown, PullRequestMarkdownContext } from "./PullRequestMarkdown";
7
8 -async function renderBody(host: string, options: { lineBreaks?: boolean } = {}) {
8 +const requested = vi.hoisted(() => [] as Array<AssetResource | null>);
9 +vi.mock(import("~/assets/assetUrls"), async (original) => ({
10 + ...(await original()),
11 + useAssetUrlState: (_environmentId, resource) => {
12 + requested.push(resource);
13 + return { _tag: "Loading" };
14 + },
15 +}));
16 +
17 +function setup() {
18 + requested.length = 0;
19 +}
20 +
21 +async function renderBody(
22 + host: string,
23 + options: { lineBreaks?: boolean; repository?: string; text?: string } = {},
24 +) {
25 + const { repository = null, text = "first line\nsecond line", ...props } = options;
26 const context = {
27 remarkPlugins: [],
28 lineBreaks: sourceControlClients.get(host).hardLineBreaks ?? false,
29 threadRef: null,
30 + repository,
31 };
32 return render(
33 <PullRequestMarkdownContext value={context}>
34 <PullRequestMarkdown
17 - text={"first line\nsecond line"}
35 + text={text}
36 cwd="/repo"
37 environmentId={EnvironmentId.make("local")}
20 - {...options}
38 + {...props}
39 />
40 </PullRequestMarkdownContext>,
41 );
@@ -43,3 +61,29 @@ it("lets text the host did not write opt out of the host's breaks", async () =>
61 await expect.element(screen.getByText(/first line/)).toBeInTheDocument();
62 expect(screen.container.querySelectorAll("p br")).toHaveLength(0);
63 });
64 +
65 +it("loads the body's host media in the pull request's repository, not the checkout's", async () => {
66 + setup();
67 + const image = "https://github.com/user-attachments/assets/7195f963-51a9-4331-be74-3e06be760422";
68 + const video = "https://github.com/user-attachments/assets/2f8c1a90-1b2c-4d5e-8f90-abcdef123456";
69 + await using _screen = await renderBody("github", {
70 + repository: "enterprise/private",
71 + text: `![shot](${image})\n\n${video}`,
72 + });
73 + await vi.waitFor(() =>
74 + expect(requested).toEqual(
75 + expect.arrayContaining([
76 + expect.objectContaining({
77 + cwd: "/repo",
78 + url: expect.stringContaining("7195f963"),
79 + repository: "enterprise/private",
80 + }),
81 + expect.objectContaining({
82 + cwd: "/repo",
83 + url: expect.stringContaining("2f8c1a90"),
84 + repository: "enterprise/private",
85 + }),
86 + ]),
87 + ),
88 + );
89 +});
apps/web/src/components/pullRequest/PullRequestMarkdown.tsx
+13 -1
@@ -23,6 +23,8 @@ export const PullRequestMarkdownContext = createContext<{
23 remarkPlugins: PluggableList;
24 lineBreaks: boolean;
25 threadRef: ScopedThreadRef | null;
26 + /** The change request's `owner/name`, which can differ from the checkout the body renders in. */
27 + repository: string | null;
28 } | null>(null);
29
30 function PullRequestHostVideo({
@@ -30,13 +32,20 @@ function PullRequestHostVideo({
32 cwd,
33 url,
34 fetchUrl,
35 + repository,
36 }: {
37 environmentId: EnvironmentId;
38 cwd: string;
39 url: string;
40 fetchUrl: string;
41 + repository: string | undefined;
42 }) {
39 - const resource: AssetResource = { _tag: SOURCE_CONTROL_MEDIA_ASSET, cwd, url: fetchUrl };
43 + const resource: AssetResource = {
44 + _tag: SOURCE_CONTROL_MEDIA_ASSET,
45 + cwd,
46 + url: fetchUrl,
47 + ...(repository ? { repository } : {}),
48 + };
49 const assetUrl = useAssetUrlState(environmentId, resource);
50 const refreshAssetUrl = useAssetUrlRefresh(environmentId, resource);
51 const src =
@@ -73,6 +82,7 @@ export function PullRequestMarkdown({
82 }) {
83 const segments = splitPullRequestBody(text);
84 const context = useContext(PullRequestMarkdownContext);
85 + const repository = context?.repository ?? undefined;
86 const resolvedThreadRef = threadRef ?? context?.threadRef ?? undefined;
87 return (
88 <div
@@ -95,6 +105,7 @@ export function PullRequestMarkdown({
105 {...(context ? { extraRemarkPlugins: context.remarkPlugins } : {})}
106 lineBreaks={lineBreaks ?? context?.lineBreaks ?? false}
107 sourceControlMedia
108 + sourceControlMediaRepository={repository}
109 onTaskListChange={
110 onTaskListChange && segment.kind === "markdown"
111 ? ({ markerOffset, checked }) =>
@@ -116,6 +127,7 @@ export function PullRequestMarkdown({
127 cwd={cwd}
128 url={segment.url}
129 fetchUrl={hostMedia.fetchUrl}
130 + repository={repository}
131 />
132 );
133 }
packages/contracts/src/assets.ts
+3 -1
@@ -54,10 +54,12 @@ export const AssetResource = Schema.Union([
54 }),
55 // Media a change request body points at on its host. A private repository serves these only
56 // to a request that carries a credential, which the client has none of, so the server fetches
57 - // them with the credential the host's driver holds for the repository at `cwd`.
57 + // them with the credential the host's driver holds for the repository at `cwd`. `repository`
58 + // names the change request's repository, which can differ from the checkout at `cwd`.
59 Schema.TaggedStruct(SOURCE_CONTROL_MEDIA_ASSET, {
60 cwd: TrimmedNonEmptyString.check(Schema.isMaxLength(ASSET_PATH_MAX_LENGTH)),
61 url: TrimmedNonEmptyString.check(Schema.isMaxLength(2048)),
62 + repository: Schema.optionalKey(TrimmedNonEmptyString.check(Schema.isMaxLength(512))),
63 }),
64 ]);
65 export type AssetResource = typeof AssetResource.Type;