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

tools.ts

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