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
| 1 | import { |
| 2 | CommandId, |
| 3 | ThreadId, |
| 4 | type OrchestrationV2ThreadShell, |
| 5 | type PullRequestUnavailableError, |
| 6 | type PullRequestWatchUntil, |
| 7 | } from "@t3tools/contracts"; |
| 8 | import * as Cause from "effect/Cause"; |
| 9 | import * as Context from "effect/Context"; |
| 10 | import * as SecureRandom from "effect/crypto/SecureRandom"; |
| 11 | import * as Effect from "effect/Effect"; |
| 12 | import * as Layer from "effect/Layer"; |
| 13 | import * as Option from "effect/Option"; |
| 14 | import * as Schema from "effect/Schema"; |
| 15 | |
| 16 | import * as Orchestrator from "../orchestration-v2/Orchestrator.ts"; |
| 17 | import * as ProjectService from "../project/ProjectService.ts"; |
| 18 | import * as PullRequestService from "./PullRequestService.ts"; |
| 19 | import { |
| 20 | resolveTarget, |
| 21 | type PullRequestTarget, |
| 22 | type PullRequestTargetError, |
| 23 | type ResolvedTarget, |
| 24 | } from "./pullRequestTarget.ts"; |
| 25 | import { |
| 26 | threadPullRequestKeysEqual, |
| 27 | threadPullRequestsOf, |
| 28 | } from "../sourceControl/threadPullRequests.ts"; |
| 29 | |
| 30 | export class PullRequestWatchFailedError extends Schema.TaggedError<PullRequestWatchFailedError>()( |
| 31 | "PullRequestWatchFailedError", |
| 32 | { cause: Schema.Defect() }, |
| 33 | ) { |
| 34 | override get message(): string { |
| 35 | return "Could not change whether the pull request is watched."; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | export class PullRequestWatchFromSubagentError extends Schema.TaggedError<PullRequestWatchFromSubagentError>()( |
| 40 | "PullRequestWatchFromSubagentError", |
| 41 | {}, |
| 42 | ) { |
| 43 | override get message(): string { |
| 44 | return "This thread is a subagent, so it cannot watch pull requests. Its parent thread owns the pull request: finish your task and report back instead."; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | export class PullRequestWatchThreadNotFoundError extends Schema.TaggedError<PullRequestWatchThreadNotFoundError>()( |
| 49 | "PullRequestWatchThreadNotFoundError", |
| 50 | { threadId: ThreadId }, |
| 51 | ) { |
| 52 | override get message(): string { |
| 53 | return `Thread ${this.threadId} was not found.`; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | export class PullRequestWatchService extends Context.Service< |
| 58 | PullRequestWatchService, |
| 59 | { |
| 60 | readonly setWatching: (input: { |
| 61 | readonly threadId: ThreadId; |
| 62 | readonly target: PullRequestTarget; |
| 63 | readonly watching: boolean; |
| 64 | readonly until?: PullRequestWatchUntil | undefined; |
| 65 | }) => Effect.Effect< |
| 66 | ResolvedTarget & { |
| 67 | readonly watching: boolean; |
| 68 | readonly wasWatching: boolean; |
| 69 | /** Set when the PR already ended, so no watch was registered. */ |
| 70 | readonly state?: "merged" | "closed"; |
| 71 | }, |
| 72 | | PullRequestWatchFailedError |
| 73 | | PullRequestWatchFromSubagentError |
| 74 | | PullRequestWatchThreadNotFoundError |
| 75 | | PullRequestTargetError |
| 76 | | PullRequestUnavailableError |
| 77 | >; |
| 78 | } |
| 79 | >()("t3/pullRequest/PullRequestWatchService") {} |
| 80 | |
| 81 | const make = Effect.gen(function* () { |
| 82 | const engine = yield* Orchestrator.OrchestratorV2; |
| 83 | const projects = yield* ProjectService.ProjectService; |
| 84 | const pullRequests = yield* PullRequestService.PullRequestService; |
| 85 | const random = yield* SecureRandom.SecureRandom; |
| 86 | |
| 87 | const requireThread = Effect.fn("PullRequestWatchService.requireThread")(function* ( |
| 88 | threadId: ThreadId, |
| 89 | ) { |
| 90 | const thread = yield* engine |
| 91 | .getThreadShell(threadId) |
| 92 | .pipe(Effect.mapError((cause) => new PullRequestWatchFailedError({ cause }))); |
| 93 | if (thread === null || thread.deletedAt !== null) { |
| 94 | return yield* new PullRequestWatchThreadNotFoundError({ threadId }); |
| 95 | } |
| 96 | return thread; |
| 97 | }); |
| 98 | |
| 99 | // Keep the registration span stable across the service extraction. |
| 100 | const setWatching = Effect.fn("PullRequestsToolkit.setWatching")(function* (input: { |
| 101 | readonly threadId: ThreadId; |
| 102 | readonly target: PullRequestTarget; |
| 103 | readonly watching: boolean; |
| 104 | readonly until?: PullRequestWatchUntil | undefined; |
| 105 | }) { |
| 106 | const { watching } = input; |
| 107 | const thread = yield* requireThread(input.threadId); |
| 108 | const project = yield* projects.getShell(thread.projectId).pipe( |
| 109 | Effect.map(Option.getOrUndefined), |
| 110 | Effect.mapError((cause) => new PullRequestWatchFailedError({ cause })), |
| 111 | ); |
| 112 | const target = yield* resolveTarget(input.target, project); |
| 113 | const watchedLink = (shell: OrchestrationV2ThreadShell) => |
| 114 | threadPullRequestsOf(shell).find( |
| 115 | (link) => link.source !== "stack-dismissed" && threadPullRequestKeysEqual(link, target), |
| 116 | ); |
| 117 | if (watching && thread.lineage.relationshipToParent === "subagent") { |
| 118 | return yield* new PullRequestWatchFromSubagentError(); |
| 119 | } |
| 120 | const before = watchedLink(thread); |
| 121 | const wasWatching = before?.watch !== undefined; |
| 122 | // A watch on an ended PR could only report that it ended, so report it now instead. |
| 123 | const alreadyEnded = (state: "merged" | "closed") => ({ |
| 124 | ...target, |
| 125 | watching: false, |
| 126 | wasWatching, |
| 127 | state, |
| 128 | }); |
| 129 | // A closed snapshot still gets a read below, since the PR may have reopened. |
| 130 | if (watching && before?.snapshot?.state === "merged") return alreadyEnded("merged"); |
| 131 | // A pending rollup has no failure yet, so any failure the first read finds is news. |
| 132 | let checksPendingAtStart: boolean | undefined; |
| 133 | if (watching && !wasWatching) { |
| 134 | const read = yield* pullRequests |
| 135 | .summary( |
| 136 | { |
| 137 | projectId: thread.projectId, |
| 138 | host: target.host, |
| 139 | repository: target.repository, |
| 140 | number: target.number, |
| 141 | allowStale: false, |
| 142 | }, |
| 143 | { recoverTransientFailure: false, bypassCache: true }, |
| 144 | ) |
| 145 | .pipe( |
| 146 | Effect.catchTags({ |
| 147 | // Host and transport errors can recover while the watch is active. |
| 148 | // A failed read leaves the seed to the link's last sync. |
| 149 | PullRequestOperationError: () => Effect.undefined, |
| 150 | }), |
| 151 | ); |
| 152 | if (read?.state === "merged" || read?.state === "closed") return alreadyEnded(read.state); |
| 153 | checksPendingAtStart = read === undefined ? undefined : read.checksState === "pending"; |
| 154 | } |
| 155 | const uuid = yield* random.uuidV4().pipe(Effect.orDie); |
| 156 | yield* engine |
| 157 | .dispatch({ |
| 158 | type: "thread.pull-request.watch", |
| 159 | commandId: CommandId.make(`server:mcp-pr-watch:${thread.id}:${uuid}`), |
| 160 | threadId: thread.id, |
| 161 | host: target.host, |
| 162 | repository: target.repository, |
| 163 | number: target.number, |
| 164 | watching, |
| 165 | ...(watching ? { link: { url: target.url, source: "agent" as const } } : {}), |
| 166 | ...(watching && input.until !== undefined && { until: input.until }), |
| 167 | ...(watching && checksPendingAtStart !== undefined |
| 168 | ? { checksPending: checksPendingAtStart } |
| 169 | : {}), |
| 170 | }) |
| 171 | .pipe( |
| 172 | Effect.catchCause((cause) => |
| 173 | Cause.hasInterruptsOnly(cause) |
| 174 | ? Effect.failCause(cause as Cause.Cause<never>) |
| 175 | : Effect.fail(new PullRequestWatchFailedError({ cause })), |
| 176 | ), |
| 177 | ); |
| 178 | const after = yield* requireThread(thread.id); |
| 179 | return { |
| 180 | ...target, |
| 181 | watching: watchedLink(after)?.watch !== undefined, |
| 182 | wasWatching, |
| 183 | }; |
| 184 | }); |
| 185 | |
| 186 | return PullRequestWatchService.of({ setWatching }); |
| 187 | }); |
| 188 | |
| 189 | export const layer = Layer.effect(PullRequestWatchService, make); |