Open History to see the last commit
1import {
2 McpCapabilityUnavailableError,
3 OrchestratorMcpEnvironmentTarget,
4 OrchestratorMcpFailure,
5 PositiveInt,
6 PullRequestReviewGuide,
7 PullRequestReviewGuideRun,
8 PullRequestState,
9 PullRequestUnavailableError,
10 ThreadPullRequestLinkSource,
11 TrimmedNonEmptyString,
12 ThreadId,
13} from "@t3tools/contracts";
14import * as Schema from "effect/Schema";
15import * as McpSchema from "effect/ai/McpSchema";
16import * as Tool from "effect/ai/Tool";
17import * as Toolkit from "effect/ai/Toolkit";
18
19import * as McpInvocationContext from "../../McpInvocationContext.ts";
20import * as Orchestrator from "../../../orchestration-v2/Orchestrator.ts";
21import * as ThreadManagementService from "../../../orchestration-v2/ThreadManagementService.ts";
22import * as ProjectService from "../../../project/ProjectService.ts";
23import * as PullRequestWatchService from "../../../pullRequest/PullRequestWatchService.ts";
24import {
25 PullRequestUrlInvalidError,
26 PullRequestTargetIncompleteError,
27 PullRequestHostRequiredError,
28 PullRequestUrlUnavailableError,
29} from "../../../pullRequest/pullRequestTarget.ts";
30import * as PullRequestReviewGuideService from "../../../pullRequest/PullRequestReviewGuideService.ts";
31
32const dependencies = [
33 McpInvocationContext.McpInvocationContext,
34 ThreadManagementService.ThreadManagementService,
35 Orchestrator.OrchestratorV2,
36 ProjectService.ProjectService,
37];
38
39const guideDependencies = [
40 ...dependencies,
41 PullRequestReviewGuideService.PullRequestReviewGuideService,
42];
43
44const watchDependencies = [...dependencies, PullRequestWatchService.PullRequestWatchService];
45
46export const PullRequestTargetInput = Schema.Struct({
47 environmentId: OrchestratorMcpEnvironmentTarget,
48 threadId: Schema.optional(
49 ThreadId.annotate({
50 description: "Thread. Default: this thread.",
51 }),
52 ),
53 url: Schema.optional(TrimmedNonEmptyString),
54 repository: Schema.optional(
55 TrimmedNonEmptyString.annotate({
56 description: "With `number`, when no `url`.",
57 }),
58 ),
59 number: Schema.optional(
60 PositiveInt.annotate({
61 description: "With `repository`, when no `url`.",
62 }),
63 ),
64 host: Schema.optional(
65 TrimmedNonEmptyString.annotate({
66 description: "Default: this project's host.",
67 }),
68 ),
69});
70export type PullRequestTargetInput = typeof PullRequestTargetInput.Type;
71
72export class PullRequestThreadRequiredError extends Schema.TaggedError<PullRequestThreadRequiredError>()(
73 "PullRequestThreadRequiredError",
74 {},
75) {
76 override get message(): string {
77 return "Pass threadId: this MCP client is not running inside a T3 thread.";
78 }
79}
80
81export class PullRequestLinkFailedError extends Schema.TaggedError<PullRequestLinkFailedError>()(
82 "PullRequestLinkFailedError",
83 { cause: Schema.Defect() },
84) {
85 override get message(): string {
86 return "Could not link the pull request.";
87 }
88}
89
90export class PullRequestUnlinkFailedError extends Schema.TaggedError<PullRequestUnlinkFailedError>()(
91 "PullRequestUnlinkFailedError",
92 { cause: Schema.Defect() },
93) {
94 override get message(): string {
95 return "Could not unlink the pull request.";
96 }
97}
98
99export class PullRequestListFailedError extends Schema.TaggedError<PullRequestListFailedError>()(
100 "PullRequestListFailedError",
101 { cause: Schema.Defect() },
102) {
103 override get message(): string {
104 return "Could not list the pull request.";
105 }
106}
107
108export class PullRequestProjectNotFoundError extends Schema.TaggedError<PullRequestProjectNotFoundError>()(
109 "PullRequestProjectNotFoundError",
110 { repository: Schema.String },
111) {
112 override get message(): string {
113 return `No project in this environment is a checkout of ${this.repository}, so its pull requests cannot be read here. Add one with t3_project_create.`;
114 }
115}
116
117export class PullRequestGuideFailedError extends Schema.TaggedError<PullRequestGuideFailedError>()(
118 "PullRequestGuideFailedError",
119 { detail: Schema.String, cause: Schema.optional(Schema.Defect()) },
120) {
121 override get message(): string {
122 return this.detail;
123 }
124}
125
126export const PullRequestToolError = Schema.Union([
127 OrchestratorMcpFailure,
128 McpCapabilityUnavailableError,
129 PullRequestUrlInvalidError,
130 PullRequestTargetIncompleteError,
131 PullRequestHostRequiredError,
132 PullRequestUrlUnavailableError,
133 PullRequestThreadRequiredError,
134 PullRequestLinkFailedError,
135 PullRequestUnlinkFailedError,
136 PullRequestListFailedError,
137 PullRequestWatchService.PullRequestWatchFailedError,
138 PullRequestUnavailableError,
139 PullRequestWatchService.PullRequestNotOpenError,
140 PullRequestWatchService.PullRequestWatchFromSubagentError,
141 PullRequestProjectNotFoundError,
142 PullRequestGuideFailedError,
143]);
144export type PullRequestToolError = typeof PullRequestToolError.Type;
145
146const PullRequestIdentity = {
147 host: Schema.String,
148 repository: Schema.String,
149 number: Schema.Int,
150 url: Schema.String,
151};
152
153/** A delegated child's report is how its parent learns the PR, so the link result asks for it. */
154export const CHILD_PULL_REQUEST_REPORT_NEXT =
155 "End your reply with: PR <full URL>, head <SHA>, checks and reviews <state>.";
156
157export const LinkPullRequestResult = Schema.Struct({
158 ...PullRequestIdentity,
159 alreadyLinked: Schema.Boolean,
160 next: Schema.optional(Schema.String),
161});
162export type LinkPullRequestResult = typeof LinkPullRequestResult.Type;
163
164const PullRequestNumbersByRepository = Schema.Array(
165 Schema.Struct({
166 host: Schema.String,
167 repository: Schema.String,
168 numbers: Schema.Array(Schema.Int),
169 }),
170);
171
172export const LinkPullRequestsResult = Schema.Struct({
173 linked: PullRequestNumbersByRepository,
174 alreadyLinked: PullRequestNumbersByRepository,
175 linkedCount: Schema.Int,
176 invalidUrls: Schema.optional(Schema.Array(Schema.String)),
177 next: Schema.optional(Schema.String),
178});
179export type LinkPullRequestsResult = typeof LinkPullRequestsResult.Type;
180
181export const UnlinkPullRequestResult = Schema.Struct({
182 host: Schema.String,
183 repository: Schema.String,
184 number: Schema.Int,
185 wasLinked: Schema.Boolean,
186});
187export type UnlinkPullRequestResult = typeof UnlinkPullRequestResult.Type;
188
189export const UnlinkPullRequestsResult = Schema.Struct({
190 unlinked: Schema.Array(
191 Schema.Struct({
192 host: Schema.String,
193 repository: Schema.String,
194 state: PullRequestState,
195 numbers: Schema.Array(Schema.Int),
196 }),
197 ),
198 unlinkedCount: Schema.Int,
199 remaining: Schema.Int,
200});
201export type UnlinkPullRequestsResult = typeof UnlinkPullRequestsResult.Type;
202
203export const WatchPullRequestResult = Schema.Struct({
204 ...PullRequestIdentity,
205 watching: Schema.Boolean,
206 wasWatching: Schema.Boolean,
207});
208export type WatchPullRequestResult = typeof WatchPullRequestResult.Type;
209
210export const ThreadPullRequestEntry = Schema.Struct({
211 ...PullRequestIdentity,
212 source: ThreadPullRequestLinkSource,
213 watching: Schema.Boolean,
214 state: Schema.NullOr(PullRequestState),
215 title: Schema.NullOr(Schema.String),
216 headBranch: Schema.NullOr(Schema.String),
217 baseBranch: Schema.NullOr(Schema.String),
218 isDraft: Schema.NullOr(Schema.Boolean),
219 stack: Schema.NullOr(
220 Schema.Struct({
221 kind: Schema.Literals(["native", "derived"]),
222 /** 1-based layer from the bottom; siblings share a layer. */
223 position: Schema.Int,
224 size: Schema.Int,
225 }),
226 ),
227});
228export type ThreadPullRequestEntry = typeof ThreadPullRequestEntry.Type;
229
230export const ListThreadPullRequestsResult = Schema.Struct({
231 pullRequests: Schema.Array(ThreadPullRequestEntry),
232 chains: Schema.Array(
233 Schema.Struct({
234 kind: Schema.Literals(["native", "derived"]),
235 /** Bottom to top; each pull request comes before the ones built on it. */
236 numbers: Schema.Array(Schema.Int),
237 }),
238 ),
239 /** Pass as `cursor` for the next page; null on the last page. */
240 nextCursor: Schema.NullOr(Schema.String),
241});
242export type ListThreadPullRequestsResult = typeof ListThreadPullRequestsResult.Type;
243
244const LinkPullRequestTool = Tool.make("link_pull_request", {
245 description:
246 "Link a PR to this thread. T3 shows its status and settles the thread on merge. For several PRs, use `link_pull_requests`.",
247 parameters: PullRequestTargetInput,
248 success: LinkPullRequestResult,
249 failure: PullRequestToolError,
250 dependencies,
251})
252 .annotate(Tool.Title, "Link pull request to thread")
253 .annotate(Tool.Readonly, false)
254 .annotate(Tool.Destructive, false)
255 .annotate(Tool.Idempotent, true)
256 .annotate(Tool.OpenWorld, false);
257
258const LinkPullRequestsTool = Tool.make("link_pull_requests", {
259 description:
260 "Link several PRs to a thread. Use it instead of one `link_pull_request` per PR. URLs that are not PRs come back in `invalidUrls`.",
261 parameters: Schema.Struct({
262 environmentId: OrchestratorMcpEnvironmentTarget,
263 threadId: Schema.optional(ThreadId.annotate({ description: "Thread. Default: this thread." })),
264 urls: Schema.Array(TrimmedNonEmptyString).check(Schema.isMinLength(1)),
265 }),
266 success: LinkPullRequestsResult,
267 failure: PullRequestToolError,
268 dependencies,
269})
270 .annotate(Tool.Title, "Link pull requests to thread")
271 .annotate(Tool.Readonly, false)
272 .annotate(Tool.Destructive, false)
273 .annotate(Tool.Idempotent, true)
274 .annotate(Tool.OpenWorld, false);
275
276const UnlinkPullRequestTool = Tool.make("unlink_pull_request", {
277 description:
278 "Remove a PR link from this thread. Succeeds with `wasLinked=false` when it was not linked.",
279 parameters: PullRequestTargetInput,
280 success: UnlinkPullRequestResult,
281 failure: PullRequestToolError,
282 dependencies,
283})
284 .annotate(Tool.Title, "Unlink pull request from thread")
285 .annotate(Tool.Readonly, false)
286 .annotate(Tool.Destructive, true)
287 .annotate(Tool.Idempotent, true)
288 .annotate(Tool.OpenWorld, false);
289
290const UnlinkPullRequestsTool = Tool.make("unlink_pull_requests", {
291 description:
292 "Unlink a thread's PRs whose last known state is in `states`. Use it instead of one `unlink_pull_request` per PR. PRs with no known state stay linked.",
293 parameters: Schema.Struct({
294 environmentId: OrchestratorMcpEnvironmentTarget,
295 threadId: Schema.optional(ThreadId.annotate({ description: "Thread. Default: this thread." })),
296 states: Schema.Array(PullRequestState).check(Schema.isMinLength(1)),
297 }),
298 success: UnlinkPullRequestsResult,
299 failure: PullRequestToolError,
300 dependencies,
301})
302 .annotate(Tool.Title, "Unlink pull requests by state")
303 .annotate(Tool.Readonly, false)
304 .annotate(Tool.Destructive, true)
305 .annotate(Tool.Idempotent, true)
306 .annotate(Tool.OpenWorld, false);
307
308const ListThreadPullRequestsTool = Tool.make("list_thread_pull_requests", {
309 description:
310 "List a thread's PRs, with last known state and stack order (bottom to top). Not an end-of-work check.",
311 parameters: Schema.Struct({
312 environmentId: OrchestratorMcpEnvironmentTarget,
313 threadId: Schema.optional(ThreadId.annotate({ description: "Thread. Default: this thread." })),
314 states: Schema.optional(Schema.Array(PullRequestState)),
315 cursor: Schema.optional(Schema.String),
316 }),
317 success: ListThreadPullRequestsResult,
318 failure: PullRequestToolError,
319 dependencies,
320})
321 .annotate(Tool.Title, "List thread pull requests")
322 .annotate(Tool.Readonly, true)
323 .annotate(Tool.Destructive, false)
324 .annotate(Tool.Idempotent, true)
325 .annotate(Tool.OpenWorld, false);
326
327const WatchPullRequestTool = Tool.make("watch_pull_request", {
328 description:
329 "Watch an open PR for this thread, linking it first if needed. T3 wakes you on failed checks, passed required checks, new comments or reviews, or conflicts. Handle old comments first. Only later ones wake you. After this call, end your turn. The watch stays on after each wake, so do not call this again on a wake. A wake is not a merge decision. Check readiness yourself.",
330 parameters: PullRequestTargetInput,
331 success: WatchPullRequestResult,
332 failure: PullRequestToolError,
333 dependencies: watchDependencies,
334})
335 .annotate(Tool.Title, "Watch pull request")
336 .annotate(Tool.Readonly, false)
337 .annotate(Tool.Destructive, false)
338 .annotate(Tool.Idempotent, true)
339 .annotate(Tool.OpenWorld, false)
340 .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromSubagents);
341
342const UnwatchPullRequestTool = Tool.make("unwatch_pull_request", {
343 description: "Stop watching a PR for this thread. It stays linked.",
344 parameters: PullRequestTargetInput,
345 success: WatchPullRequestResult,
346 failure: PullRequestToolError,
347 dependencies: watchDependencies,
348})
349 .annotate(Tool.Title, "Stop watching pull request")
350 .annotate(Tool.Readonly, false)
351 .annotate(Tool.Destructive, false)
352 .annotate(Tool.Idempotent, true)
353 .annotate(Tool.OpenWorld, false)
354 .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromSubagents);
355
356const GuideTarget = Schema.Struct({
357 url: PullRequestTargetInput.fields.url,
358 repository: PullRequestTargetInput.fields.repository,
359 number: PullRequestTargetInput.fields.number,
360 host: PullRequestTargetInput.fields.host,
361});
362
363const GuideOutcome = Schema.Struct({
364 ...PullRequestIdentity,
365 outcome: Schema.Literals(["started", "running", "failed"]).annotate({
366 description: "started: a run began. running: joined a run for this head. failed: see detail.",
367 }),
368 detail: Schema.NullOr(Schema.String),
369});
370
371export const WritePullRequestGuidesResult = Schema.Struct({
372 guides: Schema.Array(GuideOutcome),
373});
374export type WritePullRequestGuidesResult = typeof WritePullRequestGuidesResult.Type;
375
376export const ReadPullRequestGuideResult = Schema.Struct({
377 ...PullRequestIdentity,
378 guide: Schema.NullOr(PullRequestReviewGuide),
379 run: Schema.NullOr(PullRequestReviewGuideRun),
380 currentHeadSha: Schema.NullOr(Schema.String),
381 stale: Schema.Boolean.annotate({
382 description: "True when the PR head moved after the guide was written.",
383 }),
384});
385export type ReadPullRequestGuideResult = typeof ReadPullRequestGuideResult.Type;
386
387const WritePullRequestGuidesTool = Tool.make("write_pull_request_guides", {
388 description:
389 "Write review guides for up to 20 PRs (the tour in the PR Guide tab). Returns once runs start. Each run takes minutes in a temporary checkout. A run for the same head is joined. Read with `read_pull_request_guide`. Fails while guides are off.",
390 parameters: Schema.Struct({
391 environmentId: OrchestratorMcpEnvironmentTarget,
392 pullRequests: Schema.Array(GuideTarget).check(Schema.isMinLength(1), Schema.isMaxLength(20)),
393 }),
394 success: WritePullRequestGuidesResult,
395 failure: PullRequestToolError,
396 dependencies: guideDependencies,
397})
398 .annotate(Tool.Title, "Write pull request guides")
399 .annotate(Tool.Readonly, false)
400 .annotate(Tool.Destructive, false)
401 .annotate(Tool.Idempotent, false)
402 .annotate(Tool.OpenWorld, true);
403
404const ReadPullRequestGuideTool = Tool.make("read_pull_request_guide", {
405 description:
406 "Read a PR's stored review guide and run state. `guide` is null when none exists. Never starts a run.",
407 parameters: Schema.Struct({
408 environmentId: OrchestratorMcpEnvironmentTarget,
409 ...GuideTarget.fields,
410 }),
411 success: ReadPullRequestGuideResult,
412 failure: PullRequestToolError,
413 dependencies: guideDependencies,
414})
415 .annotate(Tool.Title, "Read pull request guide")
416 .annotate(Tool.Readonly, true)
417 .annotate(Tool.Destructive, false)
418 .annotate(Tool.Idempotent, true)
419 .annotate(Tool.OpenWorld, true);
420
421export const PullRequestsToolkit = Toolkit.make(
422 LinkPullRequestTool,
423 LinkPullRequestsTool,
424 UnlinkPullRequestTool,
425 UnlinkPullRequestsTool,
426 ListThreadPullRequestsTool,
427 WatchPullRequestTool,
428 UnwatchPullRequestTool,
429 WritePullRequestGuidesTool,
430 ReadPullRequestGuideTool,
431);