Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

tools.ts

930 lines32.6 KB

1import {
2 McpCapabilityUnavailableError,
3 OrchestratorMcpEnvironmentTarget,
4 OrchestratorMcpFailure,
5 PositiveInt,
6 PullRequestCheck,
7 PullRequestCommentKind,
8 PullRequestChecksState,
9 PullRequestInvolvement,
10 PullRequestListState,
11 PullRequestMergeability,
12 PullRequestMergeMethod,
13 PullRequestReviewVerdict,
14 PullRequestReviewDecision,
15 PullRequestReviewGuide,
16 PullRequestReviewGuideRun,
17 PullRequestState,
18 PullRequestUnavailableError,
19 PullRequestVisibility,
20 PullRequestWatchUntil,
21 SourceControlFailureReason,
22 ThreadPullRequestLinkSource,
23 TrimmedNonEmptyString,
24 ThreadId,
25} from "@t3tools/contracts";
26import * as Schema from "effect/Schema";
27import * as McpSchema from "effect/ai/McpSchema";
28import * as Tool from "effect/ai/Tool";
29import * as Toolkit from "effect/ai/Toolkit";
30​
31import * as McpInvocationContext from "../../McpInvocationContext.ts";
32import * as ToolFailures from "../../ToolFailures.ts";
33import * as Orchestrator from "../../../orchestration-v2/Orchestrator.ts";
34import * as ThreadManagementService from "../../../orchestration-v2/ThreadManagementService.ts";
35import * as GitManager from "../../../git/GitManager.ts";
36import * as ProjectService from "../../../project/ProjectService.ts";
37import * as PullRequestWatchService from "../../../pullRequest/PullRequestWatchService.ts";
38import {
39 PullRequestUrlInvalidError,
40 PullRequestTargetIncompleteError,
41 PullRequestHostRequiredError,
42 PullRequestUrlUnavailableError,
43 PullRequestRepositoryInvalidError,
44} from "../../../pullRequest/pullRequestTarget.ts";
45import * as PullRequestService from "../../../pullRequest/PullRequestService.ts";
46import * as PullRequestReviewGuideService from "../../../pullRequest/PullRequestReviewGuideService.ts";
47​
48const dependencies = [
49 McpInvocationContext.McpInvocationContext,
50 ThreadManagementService.ThreadManagementService,
51 Orchestrator.OrchestratorV2,
52 ProjectService.ProjectService,
53];
54​
55const forgeDependencies = [...dependencies, PullRequestService.PullRequestService];
56​
57const guideDependencies = [
58 ...dependencies,
59 PullRequestReviewGuideService.PullRequestReviewGuideService,
60];
61​
62const watchDependencies = [...dependencies, PullRequestWatchService.PullRequestWatchService];
63​
64export const PullRequestTargetInput = Schema.Struct({
65 environmentId: OrchestratorMcpEnvironmentTarget,
66 threadId: Schema.optional(
67 ThreadId.annotate({
68 description: "Thread. Default: this thread.",
69 }),
70 ),
71 url: Schema.optional(TrimmedNonEmptyString),
72 repository: Schema.optional(
73 TrimmedNonEmptyString.annotate({
74 description: "With `number`, when no `url`.",
75 }),
76 ),
77 number: Schema.optional(
78 PositiveInt.annotate({
79 description: "With `repository`, when no `url`.",
80 }),
81 ),
82 host: Schema.optional(
83 TrimmedNonEmptyString.annotate({
84 description: "Default: this project's host.",
85 }),
86 ),
87});
88export type PullRequestTargetInput = typeof PullRequestTargetInput.Type;
89​
90export class PullRequestThreadRequiredError extends Schema.TaggedError<PullRequestThreadRequiredError>()(
91 "PullRequestThreadRequiredError",
92 {},
93) {
94 override get message(): string {
95 return "Pass threadId: this MCP client is not running inside a T3 thread.";
96 }
98​
99export class PullRequestLinkFailedError extends Schema.TaggedError<PullRequestLinkFailedError>()(
100 "PullRequestLinkFailedError",
101 { cause: Schema.Defect() },
102) {
103 override get message(): string {
104 return "Could not link the pull request.";
105 }
107​
108export class PullRequestUnlinkFailedError extends Schema.TaggedError<PullRequestUnlinkFailedError>()(
109 "PullRequestUnlinkFailedError",
110 { cause: Schema.Defect() },
111) {
112 override get message(): string {
113 return "Could not unlink the pull request.";
114 }
116​
117export class PullRequestListFailedError extends Schema.TaggedError<PullRequestListFailedError>()(
118 "PullRequestListFailedError",
119 { cause: Schema.Defect() },
120) {
121 override get message(): string {
122 return "Could not list the pull request.";
123 }
125​
126export class PullRequestProjectNotFoundError extends Schema.TaggedError<PullRequestProjectNotFoundError>()(
127 "PullRequestProjectNotFoundError",
128 { repository: Schema.String },
129) {
130 override get message(): string {
131 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.`;
132 }
134​
135export class PullRequestGuideFailedError extends Schema.TaggedError<PullRequestGuideFailedError>()(
136 "PullRequestGuideFailedError",
137 { detail: Schema.String, cause: Schema.optional(Schema.Defect()) },
138) {
139 override get message(): string {
140 return this.detail;
141 }
143​
144/** The codes analytics reports for a host's refusal, by the reason the forge layer gave. */
145const FORGE_FAILURE_CODES = {
146 "missing-tool": "PullRequestForgeToolMissing",
147 unauthenticated: "PullRequestForgeUnauthenticated",
148 forbidden: "PullRequestForgeForbidden",
149 "rate-limited": "PullRequestForgeRateLimited",
150 "not-found": "PullRequestForgeNotFound",
151 unreachable: "PullRequestForgeUnreachable",
152 invalid: "PullRequestForgeInvalid",
153} as const satisfies Record<SourceControlFailureReason, string>;
154​
155export class PullRequestForgeError extends Schema.TaggedError<PullRequestForgeError>()(
156 "PullRequestForgeError",
157 {
158 detail: Schema.optional(Schema.String),
159 reason: Schema.optional(SourceControlFailureReason),
160 cause: Schema.optional(Schema.Defect()),
161 },
162) {
163 override get message(): string {
164 return this.detail ?? "Could not read this thread's project.";
165 }
166​
167 get [ToolFailures.FailureCode](): string {
168 return this.reason === undefined ? this._tag : FORGE_FAILURE_CODES[this.reason];
169 }
171​
172export class PullRequestBranchHasNoneError extends Schema.TaggedError<PullRequestBranchHasNoneError>()(
173 "PullRequestBranchHasNoneError",
174 {},
175) {
176 override get message(): string {
177 return "This thread's branch has no known PR. Pass number, or open one with pull_request_create.";
178 }
180​
181export class PullRequestCursorInvalidError extends Schema.TaggedError<PullRequestCursorInvalidError>()(
182 "PullRequestCursorInvalidError",
183 {},
184) {
185 override get message(): string {
186 return "This cursor is not from this tool. Pass a nextCursor from its last result.";
187 }
189​
190export class PullRequestNothingToDoError extends Schema.TaggedError<PullRequestNothingToDoError>()(
191 "PullRequestNothingToDoError",
192 { detail: Schema.String },
193) {
194 override get message(): string {
195 return this.detail;
196 }
198​
199export const PullRequestToolError = Schema.Union([
200 OrchestratorMcpFailure,
201 McpCapabilityUnavailableError,
202 PullRequestUrlInvalidError,
203 PullRequestTargetIncompleteError,
204 PullRequestHostRequiredError,
205 PullRequestUrlUnavailableError,
206 PullRequestRepositoryInvalidError,
207 PullRequestThreadRequiredError,
208 PullRequestLinkFailedError,
209 PullRequestUnlinkFailedError,
210 PullRequestListFailedError,
211 PullRequestWatchService.PullRequestWatchFailedError,
212 PullRequestUnavailableError,
213 PullRequestWatchService.PullRequestWatchFromSubagentError,
214 PullRequestProjectNotFoundError,
215 PullRequestGuideFailedError,
216 PullRequestForgeError,
217 PullRequestBranchHasNoneError,
218 PullRequestNothingToDoError,
219 PullRequestCursorInvalidError,
220]);
221export type PullRequestToolError = typeof PullRequestToolError.Type;
222​
223const PullRequestIdentity = {
224 host: Schema.String,
225 repository: Schema.String,
226 number: Schema.Int,
227 url: Schema.String,
228};
229​
230/** A delegated child's report is how its parent learns the PR, so the link result asks for it. */
231export const CHILD_PULL_REQUEST_REPORT_NEXT =
232 "End your reply with: PR <full URL>, head <SHA>, checks and reviews <state>.";
233​
234export const LinkPullRequestResult = Schema.Struct({
235 ...PullRequestIdentity,
236 alreadyLinked: Schema.Boolean,
237 next: Schema.optional(Schema.String),
238});
239export type LinkPullRequestResult = typeof LinkPullRequestResult.Type;
240​
241const PullRequestNumbersByRepository = Schema.Array(
242 Schema.Struct({
243 host: Schema.String,
244 repository: Schema.String,
245 numbers: Schema.Array(Schema.Int),
246 }),
247);
248​
249export const LinkPullRequestsResult = Schema.Struct({
250 linked: PullRequestNumbersByRepository,
251 alreadyLinked: PullRequestNumbersByRepository,
252 linkedCount: Schema.Int,
253 invalidUrls: Schema.optional(Schema.Array(Schema.String)),
254 next: Schema.optional(Schema.String),
255});
256export type LinkPullRequestsResult = typeof LinkPullRequestsResult.Type;
257​
258export const UnlinkPullRequestResult = Schema.Struct({
259 host: Schema.String,
260 repository: Schema.String,
261 number: Schema.Int,
262 wasLinked: Schema.Boolean,
263});
264export type UnlinkPullRequestResult = typeof UnlinkPullRequestResult.Type;
265​
266export const UnlinkPullRequestsResult = Schema.Struct({
267 unlinked: Schema.Array(
268 Schema.Struct({
269 host: Schema.String,
270 repository: Schema.String,
271 state: PullRequestState,
272 numbers: Schema.Array(Schema.Int),
273 }),
274 ),
275 unlinkedCount: Schema.Int,
276 remaining: Schema.Int,
277});
278export type UnlinkPullRequestsResult = typeof UnlinkPullRequestsResult.Type;
279​
280export const WatchPullRequestResult = Schema.Struct({
281 ...PullRequestIdentity,
282 watching: Schema.Boolean,
283 wasWatching: Schema.Boolean,
284 state: Schema.optional(
285 Schema.Literals(["merged", "closed"]).annotate({
286 description: "The PR already ended, so nothing was watched.",
287 }),
288 ),
289 checksState: Schema.optional(PullRequestChecksState),
290 next: Schema.optional(Schema.String),
291});
292export type WatchPullRequestResult = typeof WatchPullRequestResult.Type;
293​
294export const alreadyEndedWatchNext = (state: "merged" | "closed") =>
295 `This PR is already ${state}, so there is nothing to watch. Report that and end your turn.`;
296​
297export const CHECKS_ALREADY_PASSED_WATCH_NEXT =
298 "Checks already pass on this head, so no wake will come for them. Act on that now; the watch still wakes you on later changes.";
299​
300export const ThreadPullRequestEntry = Schema.Struct({
301 ...PullRequestIdentity,
302 source: ThreadPullRequestLinkSource,
303 watching: Schema.Boolean,
304 state: Schema.NullOr(PullRequestState),
305 title: Schema.NullOr(Schema.String),
306 headBranch: Schema.NullOr(Schema.String),
307 baseBranch: Schema.NullOr(Schema.String),
308 isDraft: Schema.NullOr(Schema.Boolean),
309 stack: Schema.NullOr(
310 Schema.Struct({
311 kind: Schema.Literals(["native", "derived"]),
312 /** 1-based layer from the bottom; siblings share a layer. */
313 position: Schema.Int,
314 size: Schema.Int,
315 }),
316 ),
317});
318export type ThreadPullRequestEntry = typeof ThreadPullRequestEntry.Type;
319​
320export const ListThreadPullRequestsResult = Schema.Struct({
321 pullRequests: Schema.Array(ThreadPullRequestEntry),
322 chains: Schema.Array(
323 Schema.Struct({
324 kind: Schema.Literals(["native", "derived"]),
325 /** Bottom to top; each pull request comes before the ones built on it. */
326 numbers: Schema.Array(Schema.Int),
327 }),
328 ),
329 /** Pass as `cursor` for the next page; null on the last page. */
330 nextCursor: Schema.NullOr(Schema.String),
331});
332export type ListThreadPullRequestsResult = typeof ListThreadPullRequestsResult.Type;
333​
334const LinkPullRequestTool = Tool.make("link_pull_request", {
335 description:
336 "Link a PR to this thread. T3 shows its status and settles the thread on merge. For several PRs, use `link_pull_requests`.",
337 parameters: PullRequestTargetInput,
338 success: LinkPullRequestResult,
339 failure: PullRequestToolError,
340 dependencies,
341})
342 .annotate(Tool.Title, "Link pull request to thread")
343 .annotate(Tool.Readonly, false)
344 .annotate(Tool.Destructive, false)
345 .annotate(Tool.Idempotent, true)
346 .annotate(Tool.OpenWorld, false);
347​
348const LinkPullRequestsTool = Tool.make("link_pull_requests", {
349 description:
350 "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`.",
351 parameters: Schema.Struct({
352 environmentId: OrchestratorMcpEnvironmentTarget,
353 threadId: Schema.optional(ThreadId.annotate({ description: "Thread. Default: this thread." })),
354 urls: Schema.Array(TrimmedNonEmptyString).check(Schema.isMinLength(1)),
355 }),
356 success: LinkPullRequestsResult,
357 failure: PullRequestToolError,
358 dependencies,
359})
360 .annotate(Tool.Title, "Link pull requests to thread")
361 .annotate(Tool.Readonly, false)
362 .annotate(Tool.Destructive, false)
363 .annotate(Tool.Idempotent, true)
364 .annotate(Tool.OpenWorld, false);
365​
366const UnlinkPullRequestTool = Tool.make("unlink_pull_request", {
367 description:
368 "Remove a PR link from this thread. Succeeds with `wasLinked=false` when it was not linked.",
369 parameters: PullRequestTargetInput,
370 success: UnlinkPullRequestResult,
371 failure: PullRequestToolError,
372 dependencies,
373})
374 .annotate(Tool.Title, "Unlink pull request from thread")
375 .annotate(Tool.Readonly, false)
376 .annotate(Tool.Destructive, true)
377 .annotate(Tool.Idempotent, true)
378 .annotate(Tool.OpenWorld, false);
379​
380const UnlinkPullRequestsTool = Tool.make("unlink_pull_requests", {
381 description:
382 "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.",
383 parameters: Schema.Struct({
384 environmentId: OrchestratorMcpEnvironmentTarget,
385 threadId: Schema.optional(ThreadId.annotate({ description: "Thread. Default: this thread." })),
386 states: Schema.Array(PullRequestState).check(Schema.isMinLength(1)),
387 }),
388 success: UnlinkPullRequestsResult,
389 failure: PullRequestToolError,
390 dependencies,
391})
392 .annotate(Tool.Title, "Unlink pull requests by state")
393 .annotate(Tool.Readonly, false)
394 .annotate(Tool.Destructive, true)
395 .annotate(Tool.Idempotent, true)
396 .annotate(Tool.OpenWorld, false);
397​
398const ListThreadPullRequestsTool = Tool.make("list_thread_pull_requests", {
399 description:
400 "List a thread's PRs, with last known state and stack order (bottom to top). Not an end-of-work check.",
401 parameters: Schema.Struct({
402 environmentId: OrchestratorMcpEnvironmentTarget,
403 threadId: Schema.optional(ThreadId.annotate({ description: "Thread. Default: this thread." })),
404 states: Schema.optional(Schema.Array(PullRequestState)),
405 cursor: Schema.optional(Schema.String),
406 }),
407 success: ListThreadPullRequestsResult,
408 failure: PullRequestToolError,
409 dependencies,
410})
411 .annotate(Tool.Title, "List thread pull requests")
412 .annotate(Tool.Readonly, true)
413 .annotate(Tool.Destructive, false)
414 .annotate(Tool.Idempotent, true)
415 .annotate(Tool.OpenWorld, false);
416​
417const WatchPullRequestInput = Schema.Struct({
418 ...PullRequestTargetInput.fields,
419 until: Schema.optional(
420 PullRequestWatchUntil.annotate({
421 description:
422 'How long this thread stays working. Omit to keep the current value; a new watch starts at "checks". "checks": until checks finish, for a PR you hand to the user. "merged": until it merges, closes or is unwatched, when you act on it yourself, such as merging after approval.',
423 }),
424 ),
425});
426​
427const WatchPullRequestTool = Tool.make("watch_pull_request", {
428 description:
429 "Watch an open PR for this thread, linking it if needed. T3 wakes you on failed checks, passed required checks, new comments or reviews, conflicts, and once on merge or close unless you did it via CLI. Handle old comments first; only new ones wake you. After this call, end your turn. The watch stays on: do not call this again on a wake. A wake is not a merge decision. Check readiness yourself.",
430 parameters: WatchPullRequestInput,
431 success: WatchPullRequestResult,
432 failure: PullRequestToolError,
433 dependencies: watchDependencies,
434})
435 .annotate(Tool.Title, "Watch pull request")
436 .annotate(Tool.Readonly, false)
437 .annotate(Tool.Destructive, false)
438 .annotate(Tool.Idempotent, true)
439 .annotate(Tool.OpenWorld, false)
440 .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromSubagents);
441​
442const UnwatchPullRequestTool = Tool.make("unwatch_pull_request", {
443 description: "Stop watching a PR for this thread. It stays linked.",
444 parameters: PullRequestTargetInput,
445 success: WatchPullRequestResult,
446 failure: PullRequestToolError,
447 dependencies: watchDependencies,
448})
449 .annotate(Tool.Title, "Stop watching pull request")
450 .annotate(Tool.Readonly, false)
451 .annotate(Tool.Destructive, false)
452 .annotate(Tool.Idempotent, true)
453 .annotate(Tool.OpenWorld, false)
454 .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromSubagents);
455​
456const GuideTarget = Schema.Struct({
457 url: PullRequestTargetInput.fields.url,
458 repository: PullRequestTargetInput.fields.repository,
459 number: PullRequestTargetInput.fields.number,
460 host: PullRequestTargetInput.fields.host,
461});
462​
463const GuideOutcome = Schema.Struct({
464 ...PullRequestIdentity,
465 outcome: Schema.Literals(["started", "running", "failed"]).annotate({
466 description: "started: a run began. running: joined a run for this head. failed: see detail.",
467 }),
468 detail: Schema.NullOr(Schema.String),
469});
470​
471export const WritePullRequestGuidesResult = Schema.Struct({
472 guides: Schema.Array(GuideOutcome),
473});
474export type WritePullRequestGuidesResult = typeof WritePullRequestGuidesResult.Type;
475​
476export const ReadPullRequestGuideResult = Schema.Struct({
477 ...PullRequestIdentity,
478 guide: Schema.NullOr(PullRequestReviewGuide),
479 run: Schema.NullOr(PullRequestReviewGuideRun),
480 currentHeadSha: Schema.NullOr(Schema.String),
481 stale: Schema.Boolean.annotate({
482 description: "True when the PR head moved after the guide was written.",
483 }),
484});
485export type ReadPullRequestGuideResult = typeof ReadPullRequestGuideResult.Type;
486​
487const WritePullRequestGuidesTool = Tool.make("write_pull_request_guides", {
488 description:
489 "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.",
490 parameters: Schema.Struct({
491 environmentId: OrchestratorMcpEnvironmentTarget,
492 pullRequests: Schema.Array(GuideTarget).check(Schema.isMinLength(1), Schema.isMaxLength(20)),
493 }),
494 success: WritePullRequestGuidesResult,
495 failure: PullRequestToolError,
496 dependencies: guideDependencies,
497})
498 .annotate(Tool.Title, "Write pull request guides")
499 .annotate(Tool.Readonly, false)
500 .annotate(Tool.Destructive, false)
501 .annotate(Tool.Idempotent, false)
502 .annotate(Tool.OpenWorld, true);
503​
504const ReadPullRequestGuideTool = Tool.make("read_pull_request_guide", {
505 description:
506 "Read a PR's stored review guide and run state. `guide` is null when none exists. Never starts a run.",
507 parameters: Schema.Struct({
508 environmentId: OrchestratorMcpEnvironmentTarget,
509 ...GuideTarget.fields,
510 }),
511 success: ReadPullRequestGuideResult,
512 failure: PullRequestToolError,
513 dependencies: guideDependencies,
514})
515 .annotate(Tool.Title, "Read pull request guide")
516 .annotate(Tool.Readonly, true)
517 .annotate(Tool.Destructive, false)
518 .annotate(Tool.Idempotent, true)
519 .annotate(Tool.OpenWorld, true);
520​
521export const ThreadPullRequestInput = Schema.Struct({
522 environmentId: OrchestratorMcpEnvironmentTarget,
523 number: Schema.optional(PositiveInt.annotate({ description: "Default: the branch's PR." })),
524 repository: Schema.optional(TrimmedNonEmptyString),
525});
526export type ThreadPullRequestInput = typeof ThreadPullRequestInput.Type;
527​
528export const PullRequestReadSection = Schema.Literals([
529 "body",
530 "checks",
531 "threads",
532 "comments",
533 "diff",
534]);
535export type PullRequestReadSection = typeof PullRequestReadSection.Type;
536​
537const ReadComment = Schema.Struct({
538 author: Schema.NullOr(Schema.String),
539 body: Schema.String,
540 createdAt: Schema.String,
541});
542​
543export const ReadPullRequestResult = Schema.Struct({
544 ...PullRequestIdentity,
545 title: Schema.String,
546 state: PullRequestState,
547 isDraft: Schema.Boolean,
548 visibility: Schema.optional(PullRequestVisibility).annotate({
549 description: "Only on a host with private PRs.",
550 }),
551 author: Schema.NullOr(Schema.String),
552 headBranch: Schema.String,
553 baseBranch: Schema.String,
554 headSha: Schema.NullOr(Schema.String),
555 mergeability: PullRequestMergeability,
556 behindBy: Schema.NullOr(Schema.Int),
557 autoMergeEnabled: Schema.NullOr(Schema.Boolean),
558 checks: Schema.Struct({
559 passed: Schema.Int,
560 pending: Schema.Int,
561 failed: Schema.Array(Schema.String),
562 incomplete: Schema.optional(Schema.Literal(true)).annotate({
563 description: "The host could not list some checks; checkRuns says which.",
564 }),
565 }),
566 unresolvedThreads: Schema.Int,
567 unresolvedThreadsTruncated: Schema.optional(Schema.Literal(true)).annotate({
568 description: "unresolvedThreads is a lower bound.",
569 }),
570 body: Schema.optional(Schema.String),
571 checkRuns: Schema.optional(Schema.Array(PullRequestCheck)),
572 conversation: Schema.optional(
573 Schema.Struct({
574 reviewThreads: Schema.optional(
575 Schema.Array(
576 Schema.Struct({
577 id: Schema.String,
578 path: Schema.String,
579 line: Schema.NullOr(Schema.Int),
580 isResolved: Schema.Boolean,
581 isOutdated: Schema.Boolean,
582 comments: Schema.Array(ReadComment),
583 moreComments: Schema.Boolean,
584 }),
585 ),
586 ),
587 comments: Schema.optional(
588 Schema.Array(
589 Schema.Struct({
590 ...ReadComment.fields,
591 id: Schema.String,
592 kind: PullRequestCommentKind,
593 path: Schema.NullOr(Schema.String),
594 reviewState: Schema.NullOr(Schema.String),
595 }),
596 ),
597 ),
598 truncated: Schema.Boolean,
599 nextCursor: Schema.NullOr(Schema.String),
600 }),
601 ),
602 diff: Schema.optional(
603 Schema.Struct({
604 patch: Schema.String,
605 truncated: Schema.Boolean,
606 nextCursor: Schema.NullOr(Schema.String),
607 }),
608 ),
609});
610export type ReadPullRequestResult = typeof ReadPullRequestResult.Type;
611​
612export const ListPullRequestsResult = Schema.Struct({
613 pullRequests: Schema.Array(
614 Schema.Struct({
615 number: Schema.Int,
616 title: Schema.String,
617 url: Schema.String,
618 author: Schema.NullOr(Schema.String),
619 headBranch: Schema.String,
620 baseBranch: Schema.String,
621 state: PullRequestState,
622 isDraft: Schema.Boolean,
623 reviewDecision: Schema.NullOr(PullRequestReviewDecision),
624 checksState: Schema.NullOr(PullRequestChecksState),
625 updatedAt: Schema.String,
626 }),
627 ),
628 truncated: Schema.Boolean,
629 nextCursor: Schema.NullOr(Schema.String),
630});
631export type ListPullRequestsResult = typeof ListPullRequestsResult.Type;
632​
633export const CreatePullRequestResult = Schema.Struct({
634 ...PullRequestIdentity,
635 headBranch: Schema.String,
636 created: Schema.Boolean.annotate({
637 description: "False when the branch already had an open PR.",
638 }),
639 note: Schema.optional(Schema.String),
640 next: Schema.optional(Schema.String),
641});
642export type CreatePullRequestResult = typeof CreatePullRequestResult.Type;
643​
644export const RespondToPullRequestResult = Schema.Struct({
645 ...PullRequestIdentity,
646 replied: Schema.Boolean,
647 resolved: Schema.NullOr(Schema.Boolean),
648});
649export type RespondToPullRequestResult = typeof RespondToPullRequestResult.Type;
650​
651export const MergePullRequestResult = Schema.Struct({
652 ...PullRequestIdentity,
653 state: Schema.NullOr(PullRequestState).annotate({
654 description:
655 "open while auto-merge or a merge queue waits for checks. null: done, but not read back.",
656 }),
657 note: Schema.optional(Schema.String),
658});
659export type MergePullRequestResult = typeof MergePullRequestResult.Type;
660​
661const ReadPullRequestTool = Tool.make("pull_request_read", {
662 description: "Read a PR's state and check counts. `include` adds detail.",
663 parameters: Schema.Struct({
664 ...ThreadPullRequestInput.fields,
665 include: Schema.optional(
666 Schema.Array(PullRequestReadSection).annotate({
667 description: "threads: unresolved review threads.",
668 }),
669 ),
670 includeResolved: Schema.optional(Schema.Boolean),
671 cursor: Schema.optional(TrimmedNonEmptyString),
672 }),
673 success: ReadPullRequestResult,
674 failure: PullRequestToolError,
675 dependencies: forgeDependencies,
676})
677 .annotate(Tool.Title, "Read pull request")
678 .annotate(Tool.Readonly, true)
679 .annotate(Tool.Destructive, false)
680 .annotate(Tool.Idempotent, true)
681 .annotate(Tool.OpenWorld, true)
682 .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenWithoutForge);
683​
684const ListPullRequestsTool = Tool.make("pull_request_list", {
685 description: "List PRs in this thread's repository, recently updated first.",
686 parameters: Schema.Struct({
687 environmentId: OrchestratorMcpEnvironmentTarget,
688 state: Schema.optional(PullRequestListState.annotate({ description: "Default: open." })),
689 involvement: Schema.optional(PullRequestInvolvement),
690 query: Schema.optional(TrimmedNonEmptyString.check(Schema.isMaxLength(200))),
691 limit: Schema.optional(Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 100 }))),
692 cursor: Schema.optional(TrimmedNonEmptyString),
693 }),
694 success: ListPullRequestsResult,
695 failure: PullRequestToolError,
696 dependencies: forgeDependencies,
697})
698 .annotate(Tool.Title, "List pull requests")
699 .annotate(Tool.Readonly, true)
700 .annotate(Tool.Destructive, false)
701 .annotate(Tool.Idempotent, true)
702 .annotate(Tool.OpenWorld, true)
703 .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenWithoutForge);
704​
705const UpdateNames = Schema.Array(TrimmedNonEmptyString).check(
706 Schema.isMinLength(1),
707 Schema.isMaxLength(25),
708);
709​
710const CreatePullRequestTool = Tool.make("pull_request_create", {
711 description:
712 "Push this thread's branch, or `head` when you work in another worktree, then open its PR and link it, or return its open PR.",
713 parameters: Schema.Struct({
714 environmentId: OrchestratorMcpEnvironmentTarget,
715 head: Schema.optional(TrimmedNonEmptyString),
716 title: TrimmedNonEmptyString,
717 body: Schema.String,
718 base: Schema.optional(TrimmedNonEmptyString),
719 draft: Schema.optional(Schema.Boolean),
720 private: Schema.optional(Schema.Boolean).annotate({
721 description:
722 "Visible to writers only; the branch never reaches origin. Fails on a host without private PRs.",
723 }),
724 labels: Schema.optional(UpdateNames),
725 }),
726 success: CreatePullRequestResult,
727 failure: PullRequestToolError,
728 dependencies: [...forgeDependencies, GitManager.GitManager],
729})
730 .annotate(Tool.Title, "Create pull request")
731 .annotate(Tool.Readonly, false)
732 .annotate(Tool.Destructive, false)
733 .annotate(Tool.Idempotent, true)
734 .annotate(Tool.OpenWorld, true)
735 .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenWithoutForge);
736​
737const UpdatePullRequestTool = Tool.make("pull_request_update", {
738 description:
739 "Edit a PR's title, body, labels, or reviewers, close or reopen it, or set ready (false: back to draft).",
740 parameters: Schema.Struct({
741 ...ThreadPullRequestInput.fields,
742 title: Schema.optional(TrimmedNonEmptyString),
743 body: Schema.optional(Schema.String),
744 ready: Schema.optional(Schema.Boolean),
745 state: Schema.optional(Schema.Literals(["open", "closed"])),
746 addLabels: Schema.optional(UpdateNames),
747 removeLabels: Schema.optional(UpdateNames),
748 requestReviewers: Schema.optional(
749 UpdateNames.annotate({ description: "Logins, or org/team for a team." }),
750 ),
751 }),
752 success: Schema.Struct(PullRequestIdentity),
753 failure: PullRequestToolError,
754 dependencies: forgeDependencies,
755})
756 .annotate(Tool.Title, "Update pull request")
757 .annotate(Tool.Readonly, false)
758 .annotate(Tool.Destructive, false)
759 .annotate(Tool.Idempotent, true)
760 .annotate(Tool.OpenWorld, true)
761 .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenWithoutForge);
762​
763const RespondToPullRequestTool = Tool.make("pull_request_respond", {
764 description:
765 "Reply to, resolve, or reopen a review thread. Without reviewThreadId, comment on the PR.",
766 parameters: Schema.Struct({
767 ...ThreadPullRequestInput.fields,
768 reviewThreadId: Schema.optional(TrimmedNonEmptyString),
769 body: Schema.optional(TrimmedNonEmptyString),
770 resolved: Schema.optional(Schema.Boolean),
771 }),
772 success: RespondToPullRequestResult,
773 failure: PullRequestToolError,
774 dependencies: forgeDependencies,
775})
776 .annotate(Tool.Title, "Respond on pull request")
777 .annotate(Tool.Readonly, false)
778 .annotate(Tool.Destructive, false)
779 .annotate(Tool.Idempotent, false)
780 .annotate(Tool.OpenWorld, true)
781 .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenWithoutForge);
782​
783export const ReviewLineComment = Schema.Struct({
784 path: TrimmedNonEmptyString,
785 line: PositiveInt,
786 startLine: Schema.optional(PositiveInt),
787 side: Schema.optional(
788 Schema.Literals(["new", "old"]).annotate({
789 description: "Default new. old: line numbers in the base, for removed lines.",
790 }),
791 ),
792 body: TrimmedNonEmptyString,
793});
794export type ReviewLineComment = typeof ReviewLineComment.Type;
795​
796const ReviewPullRequestTool = Tool.make("pull_request_review", {
797 description:
798 "Submit a review: approve, request changes, or comment, with optional line comments. Approving needs expectedHeadSha (headSha in pull_request_read). Refuses if the head moved from it.",
799 parameters: Schema.Struct({
800 ...ThreadPullRequestInput.fields,
801 verdict: PullRequestReviewVerdict,
802 expectedHeadSha: Schema.optional(TrimmedNonEmptyString),
803 body: Schema.optional(Schema.String),
804 comments: Schema.optional(Schema.Array(ReviewLineComment).check(Schema.isMaxLength(50))),
805 }),
806 success: Schema.Struct(PullRequestIdentity),
807 failure: PullRequestToolError,
808 dependencies: forgeDependencies,
809})
810 .annotate(Tool.Title, "Review pull request")
811 .annotate(Tool.Readonly, false)
812 .annotate(Tool.Destructive, false)
813 .annotate(Tool.Idempotent, false)
814 .annotate(Tool.OpenWorld, true)
815 .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenWithoutForge);
816​
817const MergePullRequestTool = Tool.make("pull_request_merge", {
818 description:
819 "Merge a PR. Refuses if the head moved from expectedHeadSha (headSha in pull_request_read).",
820 parameters: Schema.Struct({
821 ...ThreadPullRequestInput.fields,
822 expectedHeadSha: TrimmedNonEmptyString,
823 method: Schema.optional(PullRequestMergeMethod),
824 auto: Schema.optional(
825 Schema.Boolean.annotate({
826 description: "Merge when required checks pass, including later pushes.",
827 }),
828 ),
829 stack: Schema.optional(
830 Schema.Boolean.annotate({
831 description: "Merge this PR and every open stack layer below it at once.",
832 }),
833 ),
834 }),
835 success: MergePullRequestResult,
836 failure: PullRequestToolError,
837 dependencies: forgeDependencies,
838})
839 .annotate(Tool.Title, "Merge pull request")
840 .annotate(Tool.Readonly, false)
841 .annotate(Tool.Destructive, true)
842 .annotate(Tool.Idempotent, false)
843 .annotate(Tool.OpenWorld, true)
844 .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenWithoutForge);
845​
846export const CheckLogsResult = Schema.Struct({
847 ...PullRequestIdentity,
848 checks: Schema.Array(
849 Schema.Struct({
850 name: Schema.String,
851 url: Schema.NullOr(Schema.String),
852 log: Schema.NullOr(Schema.String),
853 truncated: Schema.Boolean,
854 detail: Schema.NullOr(Schema.String),
855 }),
856 ),
857});
858export type CheckLogsResult = typeof CheckLogsResult.Type;
859​
860export const RerunChecksResult = Schema.Struct({
861 ...PullRequestIdentity,
862 checks: Schema.Array(
863 Schema.Struct({
864 name: Schema.String,
865 rerun: Schema.Boolean,
866 detail: Schema.NullOr(Schema.String),
867 }),
868 ),
869});
870export type RerunChecksResult = typeof RerunChecksResult.Type;
871​
872const CheckLogsTool = Tool.make("ci_logs", {
873 description: "Read the tail of a PR's failing check logs, or one named check's.",
874 parameters: Schema.Struct({
875 ...ThreadPullRequestInput.fields,
876 check: Schema.optional(TrimmedNonEmptyString),
877 tailLines: Schema.optional(
878 Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 2000 })).annotate({
879 description: "Default: 200.",
880 }),
881 ),
882 }),
883 success: CheckLogsResult,
884 failure: PullRequestToolError,
885 dependencies: forgeDependencies,
886})
887 .annotate(Tool.Title, "Read CI logs")
888 .annotate(Tool.Readonly, true)
889 .annotate(Tool.Destructive, false)
890 .annotate(Tool.Idempotent, true)
891 .annotate(Tool.OpenWorld, true)
892 .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenWithoutForge);
893​
894const RerunChecksTool = Tool.make("ci_rerun", {
895 description: "Re-run a PR's failing checks, or one named check.",
896 parameters: Schema.Struct({
897 ...ThreadPullRequestInput.fields,
898 check: Schema.optional(TrimmedNonEmptyString),
899 }),
900 success: RerunChecksResult,
901 failure: PullRequestToolError,
902 dependencies: forgeDependencies,
903})
904 .annotate(Tool.Title, "Re-run CI")
905 .annotate(Tool.Readonly, false)
906 .annotate(Tool.Destructive, false)
907 .annotate(Tool.Idempotent, false)
908 .annotate(Tool.OpenWorld, true)
909 .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenWithoutForge);
910​
911export const PullRequestsToolkit = Toolkit.make(
912 LinkPullRequestTool,
913 LinkPullRequestsTool,
914 UnlinkPullRequestTool,
915 UnlinkPullRequestsTool,
916 ListThreadPullRequestsTool,
917 WatchPullRequestTool,
918 UnwatchPullRequestTool,
919 WritePullRequestGuidesTool,
920 ReadPullRequestGuideTool,
921 ReadPullRequestTool,
922 ListPullRequestsTool,
923 CreatePullRequestTool,
924 UpdatePullRequestTool,
925 RespondToPullRequestTool,
926 ReviewPullRequestTool,
927 MergePullRequestTool,
928 CheckLogsTool,
929 RerunChecksTool,
930);