Skip to content

Commit 7368bd3

Browse files
refactor(source-control): drivers build their own services and own host media (pingdotgg#23994)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 3d4c753 commit 7368bd3

44 files changed

Lines changed: 947 additions & 494 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎apps/mobile/src/features/pull-requests/PullRequestMedia.tsx‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import { createContext, useContext, useMemo, useState } from "react";
22
import { pullRequestMediaSource, pullRequestImageSource } from "./pullRequestMediaSource";
33
import { useFilePreviewGallery } from "../../components/FilePreviewGallery";
4-
import type { AssetResource, EnvironmentId } from "@t3tools/contracts";
5-
import { githubMediaFetchUrl } from "@t3tools/shared/githubMedia";
4+
import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
5+
import {
6+
SOURCE_CONTROL_MEDIA_ASSET,
7+
type AssetResource,
8+
type EnvironmentId,
9+
} from "@t3tools/contracts";
610
import { videoMimeType } from "@t3tools/shared/video";
711
import { Pressable, View } from "react-native";
812
import { Image } from "expo-image";
@@ -25,9 +29,12 @@ export const PullRequestMediaContext = createContext<{
2529

2630
export function usePullRequestMediaUrl(url: string) {
2731
const context = useContext(PullRequestMediaContext);
28-
const fetchUrl = githubMediaFetchUrl(url);
32+
const fetchUrl = sourceControlClients.mediaUrl(url)?.fetchUrl;
2933
const resource = useMemo<AssetResource | null>(
30-
() => (context && fetchUrl ? { _tag: "github-media", cwd: context.cwd, url: fetchUrl } : null),
34+
() =>
35+
context && fetchUrl
36+
? { _tag: SOURCE_CONTROL_MEDIA_ASSET, cwd: context.cwd, url: fetchUrl }
37+
: null,
3138
[context, fetchUrl],
3239
);
3340
const asset = useAssetUrlState(context?.environmentId ?? null, resource);

‎apps/mobile/src/features/pull-requests/pullRequestMediaSource.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { EnvironmentId } from "@t3tools/contracts";
2-
import { githubMediaFetchUrl } from "@t3tools/shared/githubMedia";
1+
import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
2+
import { SOURCE_CONTROL_MEDIA_ASSET, type EnvironmentId } from "@t3tools/contracts";
33
import type { FilePreviewItem } from "../../components/FilePreviewModal.types";
44

55
export interface PullRequestMediaLocation {
@@ -9,11 +9,11 @@ export interface PullRequestMediaLocation {
99

1010
/** Keep the original resource identity so previewers can resolve and renew its capability. */
1111
export function pullRequestMediaSource(url: string, location: PullRequestMediaLocation | null) {
12-
const fetchUrl = githubMediaFetchUrl(url);
12+
const fetchUrl = sourceControlClients.mediaUrl(url)?.fetchUrl;
1313
return location && fetchUrl
1414
? {
1515
environmentId: location.environmentId,
16-
resource: { _tag: "github-media" as const, cwd: location.cwd, url: fetchUrl },
16+
resource: { _tag: SOURCE_CONTROL_MEDIA_ASSET, cwd: location.cwd, url: fetchUrl },
1717
}
1818
: { uri: url };
1919
}

‎apps/mobile/src/lib/videoPreviewSource.ts‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import type { AssetResource, ChatFileAttachment, EnvironmentId } from "@t3tools/contracts";
1+
import {
2+
SOURCE_CONTROL_MEDIA_ASSET,
3+
type AssetResource,
4+
type ChatFileAttachment,
5+
type EnvironmentId,
6+
} from "@t3tools/contracts";
27
import { videoMimeType } from "@t3tools/shared/video";
38

49
import type { DraftComposerFileAttachment } from "./composerImages";
@@ -17,7 +22,13 @@ export type MediaVideoPreviewSource = {
1722
readonly environmentId: EnvironmentId;
1823
readonly resource: Extract<
1924
AssetResource,
20-
{ readonly _tag: "attachment" | "media-file" | "draft-workspace-file" | "github-media" }
25+
{
26+
readonly _tag:
27+
| "attachment"
28+
| "media-file"
29+
| "draft-workspace-file"
30+
| typeof SOURCE_CONTROL_MEDIA_ASSET;
31+
}
2132
>;
2233
}
2334
);
@@ -45,10 +56,10 @@ export function mediaVideoThumbnailKey(source: MediaVideoPreviewSource): string
4556
source.resource.path,
4657
source.srcFragment ?? "",
4758
]
48-
: source.resource._tag === "github-media"
59+
: source.resource._tag === SOURCE_CONTROL_MEDIA_ASSET
4960
? [
5061
"media-video",
51-
"github-media",
62+
SOURCE_CONTROL_MEDIA_ASSET,
5263
source.environmentId,
5364
source.resource.cwd,
5465
source.resource.url,

‎apps/server/src/assets/AssetAccess.test.ts‎

Lines changed: 58 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import * as NodeHttpPlatform from "@effect/platform-node/NodeHttpPlatform";
44
import * as NodeFSP from "node:fs/promises";
55
import {
66
AssetAccessError,
7+
AssetCreateUrlInput,
78
AssetPreviewTypeValidationError,
9+
SOURCE_CONTROL_MEDIA_ASSET,
810
ThreadId,
911
TurnItemId,
1012
} from "@t3tools/contracts";
@@ -15,12 +17,11 @@ import * as DateTime from "effect/DateTime";
1517
import * as Effect from "effect/Effect";
1618
import * as FileSystem from "effect/FileSystem";
1719
import * as Layer from "effect/Layer";
18-
import * as Redacted from "effect/Redacted";
1920
import * as Path from "effect/Path";
2021
import * as PlatformError from "effect/PlatformError";
2122
import * as Schema from "effect/Schema";
2223
import * as TestClock from "effect/testing/TestClock";
23-
import { HttpClient, HttpClientResponse, HttpServerResponse } from "effect/http";
24+
import { HttpClientRequest, HttpClientResponse, HttpServerResponse } from "effect/http";
2425
import { vi } from "vite-plus/test";
2526

2627
import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
@@ -33,8 +34,10 @@ import { assetFileResponse } from "../http.ts";
3334
import { ASSET_ROUTE_PREFIX, issueAssetUrl, readMcpAppHtml, resolveAsset } from "./AssetAccess.ts";
3435
import * as NativeAppIconResolver from "./NativeAppIconResolver.ts";
3536
import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
36-
import * as GitHubCredentials from "@t3tools/source-control-github/server/GitHubCredentials";
37-
import { githubMediaResponse } from "./GitHubMediaFetch.ts";
37+
import type { SourceControlDriverInstance } from "@t3tools/source-control-core/server/driver";
38+
import * as BuiltInDrivers from "../sourceControl/builtInDrivers.ts";
39+
import { sourceControlClients } from "../sourceControl/sourceControlClients.ts";
40+
import { sourceControlMediaResponse } from "./sourceControlMediaResponse.ts";
3841
import * as HostProcess from "@t3tools/shared/HostProcess";
3942

4043
vi.mock("node:fs/promises", async (importOriginal) => {
@@ -178,53 +181,6 @@ describe("AssetAccess", () => {
178181
);
179182
});
180183

181-
it.effect("loads private media immediately after login with the GitHub credential", () => {
182-
let lookups = 0;
183-
const authorizations: Array<string | undefined> = [];
184-
return Effect.gen(function* () {
185-
const asset = {
186-
url: "https://raw.githubusercontent.com/owner/repo/main/shot.png",
187-
cwd: "/repo",
188-
expiresAt: Number.MAX_SAFE_INTEGER,
189-
};
190-
expect((yield* githubMediaResponse(asset, {})).status).toBe(404);
191-
expect((yield* githubMediaResponse(asset, {})).status).toBe(200);
192-
expect((yield* githubMediaResponse(asset, {})).status).toBe(200);
193-
expect(lookups).toBe(3);
194-
expect(authorizations).toEqual([undefined, "Bearer signed-in", "Bearer signed-in"]);
195-
}).pipe(
196-
Effect.provide(
197-
Layer.mock(GitHubCredentials.GitHubCredentials)({
198-
get: (host) =>
199-
++lookups === 1
200-
? Effect.fail(new GitHubCredentials.GitHubNotSignedInError({ host }))
201-
: Effect.succeed({
202-
host,
203-
token: Redacted.make("signed-in"),
204-
source: "gh" as const,
205-
fingerprint: "fingerprint",
206-
}),
207-
}),
208-
),
209-
Effect.provideService(
210-
HttpClient.HttpClient,
211-
HttpClient.make((request) => {
212-
authorizations.push(request.headers.authorization);
213-
return Effect.succeed(
214-
HttpClientResponse.fromWeb(
215-
request,
216-
new Response(null, {
217-
status: request.headers.authorization ? 200 : 404,
218-
headers: { "content-type": "image/png" },
219-
}),
220-
),
221-
);
222-
}),
223-
),
224-
Effect.scoped,
225-
);
226-
});
227-
228184
it.effect("serves an image a tool returned inline from the stored item", () =>
229185
Effect.gen(function* () {
230186
const resource = {
@@ -994,51 +950,66 @@ describe("AssetAccess", () => {
994950
}).pipe(Effect.provide(layerTest)),
995951
);
996952

997-
it.effect("serves GitHub-hosted pull request media through the repository's credential", () =>
998-
Effect.gen(function* () {
999-
const resolve = (relativeUrl: string) => {
1000-
const suffix = relativeUrl.slice(`${ASSET_ROUTE_PREFIX}/`.length);
1001-
const separator = suffix.indexOf("/");
1002-
return resolveAsset(suffix.slice(0, separator), suffix.slice(separator + 1));
1003-
};
1004-
const issue = (url: string) =>
1005-
issueAssetUrl({ resource: { _tag: "github-media", cwd: "/repo", url } });
1006-
1007-
const attachment = yield* issue(
1008-
"https://github.com/user-attachments/assets/1a1842fb-6383-492f-873c-57aa0033fa6c",
1009-
);
1010-
expect(attachment.relativeUrl.endsWith("/1a1842fb-6383-492f-873c-57aa0033fa6c")).toBe(true);
1011-
expect(yield* resolve(attachment.relativeUrl)).toEqual({
1012-
kind: "github-media",
1013-
url: "https://github.com/user-attachments/assets/1a1842fb-6383-492f-873c-57aa0033fa6c",
1014-
cwd: "/repo",
1015-
expiresAt: attachment.expiresAt,
953+
it.effect("serves a host's credentialed media through that host's driver", () => {
954+
const fetched: Array<{ url: string; headers: Readonly<Record<string, string>> }> = [];
955+
const url = "https://github.com/owner/repo/blob/main/docs/shot.png";
956+
const media = sourceControlClients.mediaUrl(url)!;
957+
const layerDrivers = Layer.succeed(BuiltInDrivers.SourceControlDrivers, [
958+
{
959+
driver: { kind: media.definition.kind, definition: media.definition },
960+
instance: {
961+
sourceControl: {} as SourceControlDriverInstance["sourceControl"],
962+
discovery: {} as SourceControlDriverInstance["discovery"],
963+
pullRequests: null,
964+
fetchMedia: (request) =>
965+
Effect.sync(() => {
966+
fetched.push(request);
967+
return HttpClientResponse.fromWeb(
968+
HttpClientRequest.get(request.url),
969+
new Response("bytes", { status: 206, headers: { "content-length": "5" } }),
970+
);
971+
}),
972+
},
973+
},
974+
]);
975+
return Effect.gen(function* () {
976+
// Every client and server release spells the resource this way on the wire.
977+
const { resource } = yield* Schema.decodeUnknownEffect(AssetCreateUrlInput)({
978+
resource: { _tag: "github-media", cwd: "/repo", url },
1016979
});
1017-
1018-
const committed = yield* issue("https://github.com/owner/repo/blob/main/docs/shot.png");
1019-
expect(yield* resolve(committed.relativeUrl)).toMatchObject({
1020-
url: "https://raw.githubusercontent.com/owner/repo/main/docs/shot.png",
980+
const issued = yield* issueAssetUrl({ resource });
981+
const suffix = issued.relativeUrl.slice(`${ASSET_ROUTE_PREFIX}/`.length);
982+
const separator = suffix.indexOf("/");
983+
const asset = yield* resolveAsset(suffix.slice(0, separator), suffix.slice(separator + 1));
984+
expect(asset).toEqual({
985+
kind: "source-control-media",
986+
url: media.fetchUrl,
987+
cwd: "/repo",
988+
expiresAt: issued.expiresAt,
1021989
});
990+
if (asset?.kind !== "source-control-media") return;
1022991

1023-
const legacy = yield* issue("https://github.com/owner/repo/assets/45952064/1a1842fb");
1024-
expect(yield* resolve(legacy.relativeUrl)).toMatchObject({
1025-
url: "https://github.com/owner/repo/assets/45952064/1a1842fb",
1026-
});
1027-
const lfs = yield* issue("https://media.githubusercontent.com/media/owner/repo/main/a.mp4");
1028-
expect(yield* resolve(lfs.relativeUrl)).toMatchObject({
1029-
url: "https://media.githubusercontent.com/media/owner/repo/main/a.mp4",
992+
const response = yield* sourceControlMediaResponse(asset, {
993+
range: "bytes=0-4",
994+
cookie: "session",
1030995
});
1031-
const awkward = yield* issue("https://raw.githubusercontent.com/o/r/main/100%.png");
1032-
expect(awkward.relativeUrl.endsWith("/100%25.png")).toBe(true);
996+
expect(fetched).toEqual([{ url: media.fetchUrl, headers: { range: "bytes=0-4" } }]);
997+
expect(response.status).toBe(206);
998+
// The upstream sent no type, so it comes from the file name the host gave the media.
999+
expect(response.headers["content-type"]).toBe("image/png");
1000+
expect(response.headers["content-length"]).toBe("5");
1001+
}).pipe(Effect.provide(Layer.merge(layerTest, layerDrivers)), Effect.scoped);
1002+
});
10331003

1004+
it.effect("refuses media no host keeps behind a credential, without echoing the URL", () =>
1005+
Effect.gen(function* () {
10341006
for (const url of [
10351007
"https://example.com/shot.png",
10361008
"https://example.com/shot.png?token=private-media-token",
1037-
"http://github.com/user-attachments/assets/1a1842fb",
1038-
"https://github.com/owner/repo/pull/1",
1039-
"https://github.com/owner/repo/blob/main/",
10401009
]) {
1041-
const error = yield* issue(url).pipe(Effect.flip);
1010+
const error = yield* issueAssetUrl({
1011+
resource: { _tag: SOURCE_CONTROL_MEDIA_ASSET, cwd: "/repo", url },
1012+
}).pipe(Effect.flip);
10421013
expect(error._tag).toBe("AssetGitHubMediaUrlValidationError");
10431014
const encoded = yield* Schema.encodeEffect(Schema.fromJsonString(AssetAccessError))(error);
10441015
expect(encoded).not.toContain(url);

‎apps/server/src/assets/AssetAccess.ts‎

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { AssetResource } from "@t3tools/contracts";
22
import {
33
AssetAttachmentNotFoundError,
4-
AssetGitHubMediaUrlValidationError,
4+
AssetSourceControlMediaUrlValidationError,
55
AssetPreviewTypeValidationError,
66
AssetProjectFaviconInspectionError,
77
AssetProjectFaviconNotFoundError,
@@ -13,6 +13,7 @@ import {
1313
AssetWorkspacePathValidationError,
1414
AssetWorkspaceResolutionError,
1515
AssetWorkspaceRootNormalizationError,
16+
SOURCE_CONTROL_MEDIA_ASSET,
1617
ThreadId,
1718
ToolActivityNativeAppReference,
1819
TurnItemId,
@@ -30,7 +31,6 @@ import {
3031
readImageDimensions,
3132
type ImageDimensions,
3233
} from "@t3tools/shared/imageDimensions";
33-
import { githubMediaFetchUrl, githubMediaFileName } from "@t3tools/shared/githubMedia";
3434
import { MCP_APP_MAX_HTML_BYTES } from "@t3tools/shared/mcpApp";
3535
import {
3636
isProjectFaviconPath,
@@ -64,6 +64,7 @@ import * as ProjectFaviconResolver from "../project/ProjectFaviconResolver.ts";
6464
import { isWithinRoot } from "../workspace/KnownWorkspaces.ts";
6565
import * as WorkspacePaths from "../workspace/WorkspacePaths.ts";
6666
import * as NativeAppIconResolver from "./NativeAppIconResolver.ts";
67+
import { sourceControlClients } from "../sourceControl/sourceControlClients.ts";
6768
import { extractIcnsPng } from "./icns.ts";
6869
import { openMediaFile, readMediaFileHeader, type OpenMediaFile } from "./MediaFile.ts";
6970
import * as HostProcess from "@t3tools/shared/HostProcess";
@@ -160,8 +161,8 @@ const AssetClaimsSchema = Schema.Union([
160161
}),
161162
Schema.Struct({
162163
version: Schema.Literal(1),
163-
kind: Schema.Literal("github-media"),
164-
/** Already narrowed to a GitHub media host at mint time; the signature is what keeps it there. */
164+
kind: Schema.Literal(SOURCE_CONTROL_MEDIA_ASSET),
165+
/** A host's `fetchUrl`, narrowed at mint time; the signature is what keeps it there. */
165166
url: Schema.String,
166167
cwd: Schema.String,
167168
expiresAt: Schema.Number,
@@ -189,7 +190,7 @@ export type ResolvedAsset =
189190
readonly mimeType: string;
190191
}
191192
| {
192-
readonly kind: "github-media";
193+
readonly kind: "source-control-media";
193194
readonly url: string;
194195
readonly cwd: string;
195196
readonly expiresAt: number;
@@ -803,19 +804,19 @@ export const issueAssetUrl = Effect.fn("AssetAccess.issueAssetUrl")(function* (i
803804
fileName = "native-app-icon.png";
804805
break;
805806
}
806-
case "github-media": {
807-
const fetchUrl = githubMediaFetchUrl(input.resource.url);
808-
if (fetchUrl === null) {
809-
return yield* new AssetGitHubMediaUrlValidationError({});
807+
case SOURCE_CONTROL_MEDIA_ASSET: {
808+
const media = sourceControlClients.mediaUrl(input.resource.url);
809+
if (media === null) {
810+
return yield* new AssetSourceControlMediaUrlValidationError({});
810811
}
811812
claims = {
812813
version: 1,
813-
kind: "github-media",
814-
url: fetchUrl,
814+
kind: SOURCE_CONTROL_MEDIA_ASSET,
815+
url: media.fetchUrl,
815816
cwd: input.resource.cwd,
816817
expiresAt,
817818
};
818-
fileName = githubMediaFileName(fetchUrl);
819+
fileName = media.fileName;
819820
break;
820821
}
821822
}
@@ -926,9 +927,9 @@ export const resolveAsset = Effect.fn("AssetAccess.resolveAsset")(function* (
926927
return faviconPath === claims.filePath ? yield* projectFaviconAsset(faviconPath) : null;
927928
}
928929

929-
if (claims.kind === "github-media") {
930+
if (claims.kind === SOURCE_CONTROL_MEDIA_ASSET) {
930931
return {
931-
kind: "github-media",
932+
kind: "source-control-media",
932933
url: claims.url,
933934
cwd: claims.cwd,
934935
expiresAt: claims.expiresAt,

0 commit comments

Comments
 (0)