Open History to see the last commit
1import {
2 McpCapabilityUnavailableError,
3 OrchestratorMcpFailure,
4 PositiveInt,
5 PullRequestState,
6 ThreadPullRequestLinkSource,
7 TrimmedNonEmptyString,
8 ThreadId,
9} from "@t3tools/contracts";
10import * as Schema from "effect/Schema";
11import * as Tool from "effect/ai/Tool";
12import * as Toolkit from "effect/ai/Toolkit";
13
14import * as McpInvocationContext from "../../McpInvocationContext.ts";
15import * as Orchestrator from "../../../orchestration-v2/Orchestrator.ts";
16import * as ThreadManagementService from "../../../orchestration-v2/ThreadManagementService.ts";
17import * as ProjectService from "../../../project/ProjectService.ts";
18
19const dependencies = [
20 McpInvocationContext.McpInvocationContext,
21 ThreadManagementService.ThreadManagementService,
22 Orchestrator.OrchestratorV2,
23 ProjectService.ProjectService,
24];
25
26const REGISTER_EVERY_PR =
27 "Register every pull request you open for this thread, including each layer of a stack, right after creating it.";
28
29/**
30 * Either the pull request's URL or its repository and number. Both forms
31 * resolve to the same host-level identity, so the agent can pass whichever
32 * the host CLI handed back.
33 */
34export const PullRequestTargetInput = Schema.Struct({
35 threadId: Schema.optional(
36 ThreadId.annotate({
37 description: "Thread to act on. Omit for this thread.",
38 }),
39 ),
40 url: Schema.optional(
41 TrimmedNonEmptyString.annotate({
42 description:
43 "The pull request's web URL, for example https://github.com/owner/repo/pull/123. Preferred when you have it; host, repository and number are read from it.",
44 }),
45 ),
46 repository: Schema.optional(
47 TrimmedNonEmptyString.annotate({
48 description:
49 "Repository path below the host, for example owner/repo. Required with number when url is omitted.",
50 }),
51 ),
52 number: Schema.optional(
53 PositiveInt.annotate({
54 description: "Pull request number. Required with repository when url is omitted.",
55 }),
56 ),
57 host: Schema.optional(
58 TrimmedNonEmptyString.annotate({
59 description:
60 "Host the repository lives on, for example github.com. Defaults to the host of this thread's project.",
61 }),
62 ),
63});
64export type PullRequestTargetInput = typeof PullRequestTargetInput.Type;
65
66export class PullRequestUrlInvalidError extends Schema.TaggedError<PullRequestUrlInvalidError>()(
67 "PullRequestUrlInvalidError",
68 {},
69) {
70 override get message(): string {
71 return "This is not a recognised pull request URL. Pass repository and number instead.";
72 }
73}
74
75export class PullRequestTargetIncompleteError extends Schema.TaggedError<PullRequestTargetIncompleteError>()(
76 "PullRequestTargetIncompleteError",
77 {},
78) {
79 override get message(): string {
80 return "Pass either url, or both repository and number.";
81 }
82}
83
84export class PullRequestHostRequiredError extends Schema.TaggedError<PullRequestHostRequiredError>()(
85 "PullRequestHostRequiredError",
86 {},
87) {
88 override get message(): string {
89 return "This thread's project has no recognised remote. Pass host or url.";
90 }
91}
92
93export class PullRequestThreadRequiredError extends Schema.TaggedError<PullRequestThreadRequiredError>()(
94 "PullRequestThreadRequiredError",
95 {},
96) {
97 override get message(): string {
98 return "Pass threadId: this MCP client is not running inside a T3 thread.";
99 }
100}
101
102export class PullRequestThreadNotFoundError extends Schema.TaggedError<PullRequestThreadNotFoundError>()(
103 "PullRequestThreadNotFoundError",
104 { threadId: Schema.String },
105) {
106 override get message(): string {
107 return `Thread ${this.threadId} was not found.`;
108 }
109}
110
111export class PullRequestLinkFailedError extends Schema.TaggedError<PullRequestLinkFailedError>()(
112 "PullRequestLinkFailedError",
113 { cause: Schema.Defect() },
114) {
115 override get message(): string {
116 return "Could not link the pull request.";
117 }
118}
119
120export class PullRequestUnlinkFailedError extends Schema.TaggedError<PullRequestUnlinkFailedError>()(
121 "PullRequestUnlinkFailedError",
122 { cause: Schema.Defect() },
123) {
124 override get message(): string {
125 return "Could not unlink the pull request.";
126 }
127}
128
129export class PullRequestWatchFailedError extends Schema.TaggedError<PullRequestWatchFailedError>()(
130 "PullRequestWatchFailedError",
131 { cause: Schema.Defect() },
132) {
133 override get message(): string {
134 return "Could not change whether the pull request is watched.";
135 }
136}
137
138export class PullRequestNotOpenError extends Schema.TaggedError<PullRequestNotOpenError>()(
139 "PullRequestNotOpenError",
140 { state: Schema.String },
141) {
142 override get message(): string {
143 return `The pull request is ${this.state}, so there is nothing to watch.`;
144 }
145}
146
147export class PullRequestWatchFromSubagentError extends Schema.TaggedError<PullRequestWatchFromSubagentError>()(
148 "PullRequestWatchFromSubagentError",
149 {},
150) {
151 override get message(): string {
152 return "This thread is a subagent, so it cannot watch pull requests. Its parent thread owns the pull request: finish your task and report back instead.";
153 }
154}
155
156export class PullRequestListFailedError extends Schema.TaggedError<PullRequestListFailedError>()(
157 "PullRequestListFailedError",
158 { cause: Schema.Defect() },
159) {
160 override get message(): string {
161 return "Could not list the pull request.";
162 }
163}
164
165export const PullRequestToolError = Schema.Union([
166 OrchestratorMcpFailure,
167 McpCapabilityUnavailableError,
168 PullRequestUrlInvalidError,
169 PullRequestTargetIncompleteError,
170 PullRequestHostRequiredError,
171 PullRequestThreadRequiredError,
172 PullRequestThreadNotFoundError,
173 PullRequestLinkFailedError,
174 PullRequestUnlinkFailedError,
175 PullRequestListFailedError,
176 PullRequestWatchFailedError,
177 PullRequestNotOpenError,
178 PullRequestWatchFromSubagentError,
179]);
180export type PullRequestToolError = typeof PullRequestToolError.Type;
181
182const PullRequestIdentity = {
183 host: Schema.String,
184 repository: Schema.String,
185 number: Schema.Int,
186 url: Schema.String,
187};
188
189export const LinkPullRequestResult = Schema.Struct({
190 ...PullRequestIdentity,
191 alreadyLinked: Schema.Boolean.annotate({
192 description: "True when the pull request was linked to this thread before the call.",
193 }),
194});
195export type LinkPullRequestResult = typeof LinkPullRequestResult.Type;
196
197export const UnlinkPullRequestResult = Schema.Struct({
198 host: Schema.String,
199 repository: Schema.String,
200 number: Schema.Int,
201 wasLinked: Schema.Boolean.annotate({
202 description: "False when the pull request was not linked to this thread to begin with.",
203 }),
204});
205export type UnlinkPullRequestResult = typeof UnlinkPullRequestResult.Type;
206
207export const WatchPullRequestResult = Schema.Struct({
208 ...PullRequestIdentity,
209 watching: Schema.Boolean.annotate({
210 description: "Whether T3 Code now watches the pull request for this thread.",
211 }),
212 wasWatching: Schema.Boolean.annotate({
213 description: "Whether it was already watched before the call.",
214 }),
215});
216export type WatchPullRequestResult = typeof WatchPullRequestResult.Type;
217
218export const ThreadPullRequestEntry = Schema.Struct({
219 ...PullRequestIdentity,
220 source: ThreadPullRequestLinkSource,
221 watching: Schema.Boolean,
222 state: Schema.NullOr(PullRequestState),
223 title: Schema.NullOr(Schema.String),
224 headBranch: Schema.NullOr(Schema.String),
225 baseBranch: Schema.NullOr(Schema.String),
226 isDraft: Schema.NullOr(Schema.Boolean),
227 stack: Schema.NullOr(
228 Schema.Struct({
229 kind: Schema.Literals(["native", "derived"]),
230 /** 1-based, bottom of the stack first. */
231 position: Schema.Int,
232 size: Schema.Int,
233 }),
234 ),
235});
236export type ThreadPullRequestEntry = typeof ThreadPullRequestEntry.Type;
237
238export const ListThreadPullRequestsResult = Schema.Struct({
239 pullRequests: Schema.Array(ThreadPullRequestEntry),
240 chains: Schema.Array(
241 Schema.Struct({
242 kind: Schema.Literals(["native", "derived"]),
243 /** Bottom to top. */
244 numbers: Schema.Array(Schema.Int),
245 }),
246 ),
247});
248export type ListThreadPullRequestsResult = typeof ListThreadPullRequestsResult.Type;
249
250const LinkPullRequestTool = Tool.make("link_pull_request", {
251 description: `${REGISTER_EVERY_PR} Links a pull request to this thread so T3 Code tracks it, shows its status beside the thread, and settles the thread when it merges. Pass the URL, or repository plus number. Linking an already-linked pull request succeeds with alreadyLinked=true.`,
252 parameters: PullRequestTargetInput,
253 success: LinkPullRequestResult,
254 failure: PullRequestToolError,
255 dependencies,
256})
257 .annotate(Tool.Title, "Link pull request to thread")
258 .annotate(Tool.Readonly, false)
259 .annotate(Tool.Destructive, false)
260 .annotate(Tool.Idempotent, true)
261 .annotate(Tool.OpenWorld, false);
262
263const UnlinkPullRequestTool = Tool.make("unlink_pull_request", {
264 description:
265 "Remove a pull request link from this thread, for example after closing a pull request you opened by mistake. Pass the URL, or repository plus number. Unlinking a pull request that is not linked succeeds with wasLinked=false.",
266 parameters: PullRequestTargetInput,
267 success: UnlinkPullRequestResult,
268 failure: PullRequestToolError,
269 dependencies,
270})
271 .annotate(Tool.Title, "Unlink pull request from thread")
272 .annotate(Tool.Readonly, false)
273 .annotate(Tool.Destructive, true)
274 .annotate(Tool.Idempotent, true)
275 .annotate(Tool.OpenWorld, false);
276
277const ListThreadPullRequestsTool = Tool.make("list_thread_pull_requests", {
278 description: `List the pull requests linked to a thread (omit threadId for this thread) with their last known host state, and how they chain into stacks (bottom to top). ${REGISTER_EVERY_PR}`,
279 parameters: Schema.Struct({
280 threadId: Schema.optional(
281 ThreadId.annotate({ description: "Thread to list. Omit for this thread." }),
282 ),
283 }),
284 success: ListThreadPullRequestsResult,
285 failure: PullRequestToolError,
286 dependencies,
287})
288 .annotate(Tool.Title, "List thread pull requests")
289 .annotate(Tool.Readonly, true)
290 .annotate(Tool.Destructive, false)
291 .annotate(Tool.Idempotent, true)
292 .annotate(Tool.OpenWorld, false);
293
294const WatchPullRequestTool = Tool.make("watch_pull_request", {
295 description:
296 "Have T3 Code watch an open pull request for this thread, linking it first if needed. T3 Code checks it every two minutes and wakes you with a message when a check fails, the required checks pass, someone else comments or reviews, or the branch starts to conflict with its base. Use this to monitor or babysit a pull request instead of polling, sleeping, or running a watcher. Only comments posted after this call wake you, so handle the existing ones first, then end your turn. A wake is news, not a merge decision: check readiness yourself before merging. While T3 Code watches, the thread stays in the user's Working list, not their inbox. When you hand the work back to the user, call unwatch_pull_request first so the thread returns to their inbox. Watching ends when the pull request merges or closes, when its thread settles or is archived, when T3 Code fails to read it 8 times in a row (a host rate limit only delays it), when the user stops this thread, or when you call unwatch_pull_request. Unsettle the thread before starting a new watch. A subagent cannot watch: its parent thread owns the pull request.",
297 parameters: PullRequestTargetInput,
298 success: WatchPullRequestResult,
299 failure: PullRequestToolError,
300 dependencies,
301})
302 .annotate(Tool.Title, "Watch pull request")
303 .annotate(Tool.Readonly, false)
304 .annotate(Tool.Destructive, false)
305 .annotate(Tool.Idempotent, true)
306 .annotate(Tool.OpenWorld, false);
307
308const UnwatchPullRequestTool = Tool.make("unwatch_pull_request", {
309 description:
310 "Stop T3 Code from watching a pull request for this thread. The pull request stays linked. Pass the URL, or repository plus number.",
311 parameters: PullRequestTargetInput,
312 success: WatchPullRequestResult,
313 failure: PullRequestToolError,
314 dependencies,
315})
316 .annotate(Tool.Title, "Stop watching pull request")
317 .annotate(Tool.Readonly, false)
318 .annotate(Tool.Destructive, false)
319 .annotate(Tool.Idempotent, true)
320 .annotate(Tool.OpenWorld, false);
321
322export const PullRequestsToolkit = Toolkit.make(
323 LinkPullRequestTool,
324 UnlinkPullRequestTool,
325 ListThreadPullRequestsTool,
326 WatchPullRequestTool,
327 UnwatchPullRequestTool,
328);