@devries-cameron20 / t3code Mirror · read-only
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 21, 04:45 UTC

main
ts 114 lines 3.88 KB
Raw
1 import { msg } from "gt-i18n";
2 import { type MarkedText, markedTextEnglish } from "../errors/translatable.ts";
3 import {
4 EnvironmentAuthInvalidError,
5 type PullRequestDiffInput,
6 type PullRequestDiffResult,
7 } from "@t3tools/contracts";
8 import * as Context from "effect/Context";
9 import * as Effect from "effect/Effect";
10 import * as Layer from "effect/Layer";
11 import * as Option from "effect/Option";
12 import * as Schema from "effect/Schema";
13 import { HttpClient } from "effect/http";
14
15 import * as RemoteEnvironmentAuthorization from "../authorization/service.ts";
16 import type { PreparedConnection } from "../connection/model.ts";
17 import * as ManagedRelay from "../relay/managedRelay.ts";
18 import { type RemoteEnvironmentRequestError } from "../rpc/http.ts";
19 import { executeAuthenticatedEnvironmentHttpRequest } from "./environmentHttpAuth.ts";
20
21 const DEFAULT_PULL_REQUEST_DIFF_TIMEOUT_MS = 60_000;
22
23 export class PullRequestDiffCredentialRejectedError extends Schema.TaggedError<PullRequestDiffCredentialRejectedError>()(
24 "PullRequestDiffCredentialRejectedError",
25 {
26 repository: Schema.String,
27 number: Schema.Number,
28 traceId: Schema.String,
29 cause: EnvironmentAuthInvalidError,
30 },
31 ) {
32 override get message(): string {
33 return markedTextEnglish(this.i18nMessage);
34 }
35
36 get i18nMessage(): MarkedText {
37 return [
38 msg(
39 "This environment session is no longer valid (invalid_credential). Refresh the page or quit and reopen T3 Code.",
40 ),
41 ];
42 }
43 }
44
45 export type PullRequestDiffLoadError =
46 | RemoteEnvironmentRequestError
47 | PullRequestDiffCredentialRejectedError;
48
49 export const fetchEnvironmentPullRequestDiff = Effect.fn(
50 "clientRuntime.state.fetchEnvironmentPullRequestDiff",
51 )(function* (input: {
52 readonly prepared: PreparedConnection;
53 readonly diff: PullRequestDiffInput;
54 readonly signer: Option.Option<ManagedRelay.ManagedRelayDpopSigner["Service"]>;
55 readonly remoteAuthorization?: Option.Option<
56 RemoteEnvironmentAuthorization.RemoteEnvironmentAuthorization["Service"]
57 >;
58 readonly timeoutMs?: number;
59 }) {
60 return yield* executeAuthenticatedEnvironmentHttpRequest({
61 ...input,
62 group: "pullRequests",
63 method: "POST",
64 url: (urls) => urls.diff(),
65 timeoutMs: input.timeoutMs ?? DEFAULT_PULL_REQUEST_DIFF_TIMEOUT_MS,
66 request: ({ client, headers }) => client.diff({ payload: input.diff, headers }),
67 }).pipe(
68 Effect.mapError((error) =>
69 error._tag === "EnvironmentAuthInvalidError" && error.reason === "invalid_credential"
70 ? new PullRequestDiffCredentialRejectedError({
71 repository: input.diff.repository,
72 number: input.diff.number,
73 traceId: error.traceId,
74 cause: error,
75 })
76 : error,
77 ),
78 );
79 });
80
81 export class PullRequestDiffLoader extends Context.Service<
82 PullRequestDiffLoader,
83 {
84 readonly load: (
85 prepared: PreparedConnection,
86 input: PullRequestDiffInput,
87 ) => Effect.Effect<PullRequestDiffResult, PullRequestDiffLoadError>;
88 }
89 >()("@t3tools/client-runtime/state/pullRequestDiffHttp/PullRequestDiffLoader") {}
90
91 export const layer: Layer.Layer<PullRequestDiffLoader, never, HttpClient.HttpClient> = Layer.effect(
92 PullRequestDiffLoader,
93 Effect.gen(function* () {
94 const httpClient = yield* HttpClient.HttpClient;
95 const signer = yield* Effect.serviceOption(ManagedRelay.ManagedRelayDpopSigner);
96 const remoteAuthorization = yield* Effect.serviceOption(
97 RemoteEnvironmentAuthorization.RemoteEnvironmentAuthorization,
98 );
99 // The HTTP fetch owns the load span.
100 return PullRequestDiffLoader.of({
101 load: Effect.fnUntraced(
102 function* (prepared, input) {
103 return yield* fetchEnvironmentPullRequestDiff({
104 prepared,
105 diff: input,
106 signer,
107 remoteAuthorization,
108 });
109 },
110 Effect.provideService(HttpClient.HttpClient, httpClient),
111 ),
112 });
113 }),
114 );