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

tools.ts

419 lines14.2 KB

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