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

tools.ts

407 lines13.7 KB

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