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

tools.ts

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