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

tools.ts

334 lines12.2 KB

1import {
2 McpCapabilityUnavailableError,
3 PositiveInt,
4 PullRequestState,
5 ThreadPullRequestLinkSource,
6 TrimmedNonEmptyString,
7 ThreadId,
8} from "@t3tools/contracts";
9import * as Schema from "effect/Schema";
10import * as Tool from "effect/ai/Tool";
11import * as Toolkit from "effect/ai/Toolkit";
12​
13import * as McpInvocationContext from "../../McpInvocationContext.ts";
14import * as Orchestrator from "../../../orchestration-v2/Orchestrator.ts";
15import * as ProjectService from "../../../project/ProjectService.ts";
16​
17const dependencies = [
18 McpInvocationContext.McpInvocationContext,
19 Orchestrator.OrchestratorV2,
20 ProjectService.ProjectService,
21];
22​
23const REGISTER_EVERY_PR =
24 "Register every pull request you open for this thread, including each layer of a stack, right after creating it.";
25​
26/**
27 * Either the pull request's URL or its repository and number. Both forms
28 * resolve to the same host-level identity, so the agent can pass whichever
29 * the host CLI handed back.
30 */
31export const PullRequestTargetInput = Schema.Struct({
32 threadId: Schema.optional(
33 ThreadId.annotate({
34 description: "Thread to act on. Omit for this thread.",
35 }),
36 ),
37 url: Schema.optional(
38 TrimmedNonEmptyString.annotate({
39 description:
40 "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.",
41 }),
42 ),
43 repository: Schema.optional(
44 TrimmedNonEmptyString.annotate({
45 description:
46 "Repository path below the host, for example owner/repo. Required with number when url is omitted.",
47 }),
48 ),
49 number: Schema.optional(
50 PositiveInt.annotate({
51 description: "Pull request number. Required with repository when url is omitted.",
52 }),
53 ),
54 host: Schema.optional(
55 TrimmedNonEmptyString.annotate({
56 description:
57 "Host the repository lives on, for example github.com. Defaults to the host of this thread's project.",
58 }),
59 ),
60});
61export type PullRequestTargetInput = typeof PullRequestTargetInput.Type;
62​
63export class PullRequestUrlInvalidError extends Schema.TaggedError<PullRequestUrlInvalidError>()(
64 "PullRequestUrlInvalidError",
65 {},
66) {
67 override get message(): string {
68 return "This is not a recognised pull request URL. Pass repository and number instead.";
69 }
71​
72export class PullRequestTargetIncompleteError extends Schema.TaggedError<PullRequestTargetIncompleteError>()(
73 "PullRequestTargetIncompleteError",
74 {},
75) {
76 override get message(): string {
77 return "Pass either url, or both repository and number.";
78 }
80​
81export class PullRequestHostRequiredError extends Schema.TaggedError<PullRequestHostRequiredError>()(
82 "PullRequestHostRequiredError",
83 {},
84) {
85 override get message(): string {
86 return "This thread's project has no recognised remote. Pass host or url.";
87 }
89​
90export class PullRequestThreadRequiredError extends Schema.TaggedError<PullRequestThreadRequiredError>()(
91 "PullRequestThreadRequiredError",
92 {},
93) {
94 override get message(): string {
95 return "Pass threadId: this MCP client is not running inside a T3 thread.";
96 }
98​
99export class PullRequestThreadAboveLimitsError extends Schema.TaggedError<PullRequestThreadAboveLimitsError>()(
100 "PullRequestThreadAboveLimitsError",
101 { threadId: Schema.String },
102) {
103 override get message(): string {
104 return `Thread ${this.threadId} cannot be changed from here: it runs with broader permissions than this caller, or the calling thread has no active run.`;
105 }
107​
108export class PullRequestThreadNotFoundError extends Schema.TaggedError<PullRequestThreadNotFoundError>()(
109 "PullRequestThreadNotFoundError",
110 { threadId: Schema.String },
111) {
112 override get message(): string {
113 return `Thread ${this.threadId} was not found.`;
114 }
116​
117export class PullRequestLinkFailedError extends Schema.TaggedError<PullRequestLinkFailedError>()(
118 "PullRequestLinkFailedError",
119 { cause: Schema.Defect() },
120) {
121 override get message(): string {
122 return "Could not link the pull request.";
123 }
125​
126export class PullRequestUnlinkFailedError extends Schema.TaggedError<PullRequestUnlinkFailedError>()(
127 "PullRequestUnlinkFailedError",
128 { cause: Schema.Defect() },
129) {
130 override get message(): string {
131 return "Could not unlink the pull request.";
132 }
134​
135export class PullRequestWatchFailedError extends Schema.TaggedError<PullRequestWatchFailedError>()(
136 "PullRequestWatchFailedError",
137 { cause: Schema.Defect() },
138) {
139 override get message(): string {
140 return "Could not change whether the pull request is watched.";
141 }
143​
144export class PullRequestNotOpenError extends Schema.TaggedError<PullRequestNotOpenError>()(
145 "PullRequestNotOpenError",
146 { state: Schema.String },
147) {
148 override get message(): string {
149 return `The pull request is ${this.state}, so there is nothing to watch.`;
150 }
152​
153export class PullRequestWatchFromSubagentError extends Schema.TaggedError<PullRequestWatchFromSubagentError>()(
154 "PullRequestWatchFromSubagentError",
155 {},
156) {
157 override get message(): string {
158 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.";
159 }
161​
162export class PullRequestListFailedError extends Schema.TaggedError<PullRequestListFailedError>()(
163 "PullRequestListFailedError",
164 { cause: Schema.Defect() },
165) {
166 override get message(): string {
167 return "Could not list the pull request.";
168 }
170​
171export const PullRequestToolError = Schema.Union([
172 McpCapabilityUnavailableError,
173 PullRequestUrlInvalidError,
174 PullRequestTargetIncompleteError,
175 PullRequestHostRequiredError,
176 PullRequestThreadRequiredError,
177 PullRequestThreadAboveLimitsError,
178 PullRequestThreadNotFoundError,
179 PullRequestLinkFailedError,
180 PullRequestUnlinkFailedError,
181 PullRequestListFailedError,
182 PullRequestWatchFailedError,
183 PullRequestNotOpenError,
184 PullRequestWatchFromSubagentError,
185]);
186export type PullRequestToolError = typeof PullRequestToolError.Type;
187​
188const PullRequestIdentity = {
189 host: Schema.String,
190 repository: Schema.String,
191 number: Schema.Int,
192 url: Schema.String,
193};
194​
195export const LinkPullRequestResult = Schema.Struct({
196 ...PullRequestIdentity,
197 alreadyLinked: Schema.Boolean.annotate({
198 description: "True when the pull request was linked to this thread before the call.",
199 }),
200});
201export type LinkPullRequestResult = typeof LinkPullRequestResult.Type;
202​
203export const UnlinkPullRequestResult = Schema.Struct({
204 host: Schema.String,
205 repository: Schema.String,
206 number: Schema.Int,
207 wasLinked: Schema.Boolean.annotate({
208 description: "False when the pull request was not linked to this thread to begin with.",
209 }),
210});
211export type UnlinkPullRequestResult = typeof UnlinkPullRequestResult.Type;
212​
213export const WatchPullRequestResult = Schema.Struct({
214 ...PullRequestIdentity,
215 watching: Schema.Boolean.annotate({
216 description: "Whether T3 Code now watches the pull request for this thread.",
217 }),
218 wasWatching: Schema.Boolean.annotate({
219 description: "Whether it was already watched before the call.",
220 }),
221});
222export type WatchPullRequestResult = typeof WatchPullRequestResult.Type;
223​
224export const ThreadPullRequestEntry = Schema.Struct({
225 ...PullRequestIdentity,
226 source: ThreadPullRequestLinkSource,
227 watching: Schema.Boolean,
228 state: Schema.NullOr(PullRequestState),
229 title: Schema.NullOr(Schema.String),
230 headBranch: Schema.NullOr(Schema.String),
231 baseBranch: Schema.NullOr(Schema.String),
232 isDraft: Schema.NullOr(Schema.Boolean),
233 stack: Schema.NullOr(
234 Schema.Struct({
235 kind: Schema.Literals(["native", "derived"]),
236 /** 1-based, bottom of the stack first. */
237 position: Schema.Int,
238 size: Schema.Int,
239 }),
240 ),
241});
242export type ThreadPullRequestEntry = typeof ThreadPullRequestEntry.Type;
243​
244export const ListThreadPullRequestsResult = Schema.Struct({
245 pullRequests: Schema.Array(ThreadPullRequestEntry),
246 chains: Schema.Array(
247 Schema.Struct({
248 kind: Schema.Literals(["native", "derived"]),
249 /** Bottom to top. */
250 numbers: Schema.Array(Schema.Int),
251 }),
252 ),
253});
254export type ListThreadPullRequestsResult = typeof ListThreadPullRequestsResult.Type;
255​
256const LinkPullRequestTool = Tool.make("link_pull_request", {
257 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.`,
258 parameters: PullRequestTargetInput,
259 success: LinkPullRequestResult,
260 failure: PullRequestToolError,
261 dependencies,
262})
263 .annotate(Tool.Title, "Link pull request to thread")
264 .annotate(Tool.Readonly, false)
265 .annotate(Tool.Destructive, false)
266 .annotate(Tool.Idempotent, true)
267 .annotate(Tool.OpenWorld, false);
268​
269const UnlinkPullRequestTool = Tool.make("unlink_pull_request", {
270 description:
271 "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.",
272 parameters: PullRequestTargetInput,
273 success: UnlinkPullRequestResult,
274 failure: PullRequestToolError,
275 dependencies,
276})
277 .annotate(Tool.Title, "Unlink pull request from thread")
278 .annotate(Tool.Readonly, false)
279 .annotate(Tool.Destructive, true)
280 .annotate(Tool.Idempotent, true)
281 .annotate(Tool.OpenWorld, false);
282​
283const ListThreadPullRequestsTool = Tool.make("list_thread_pull_requests", {
284 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}`,
285 parameters: Schema.Struct({
286 threadId: Schema.optional(
287 ThreadId.annotate({ description: "Thread to list. Omit for this thread." }),
288 ),
289 }),
290 success: ListThreadPullRequestsResult,
291 failure: PullRequestToolError,
292 dependencies,
293})
294 .annotate(Tool.Title, "List thread pull requests")
295 .annotate(Tool.Readonly, true)
296 .annotate(Tool.Destructive, false)
297 .annotate(Tool.Idempotent, true)
298 .annotate(Tool.OpenWorld, false);
299​
300const WatchPullRequestTool = Tool.make("watch_pull_request", {
301 description:
302 "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.",
303 parameters: PullRequestTargetInput,
304 success: WatchPullRequestResult,
305 failure: PullRequestToolError,
306 dependencies,
307})
308 .annotate(Tool.Title, "Watch pull request")
309 .annotate(Tool.Readonly, false)
310 .annotate(Tool.Destructive, false)
311 .annotate(Tool.Idempotent, true)
312 .annotate(Tool.OpenWorld, false);
313​
314const UnwatchPullRequestTool = Tool.make("unwatch_pull_request", {
315 description:
316 "Stop T3 Code from watching a pull request for this thread. The pull request stays linked. Pass the URL, or repository plus number.",
317 parameters: PullRequestTargetInput,
318 success: WatchPullRequestResult,
319 failure: PullRequestToolError,
320 dependencies,
321})
322 .annotate(Tool.Title, "Stop watching pull request")
323 .annotate(Tool.Readonly, false)
324 .annotate(Tool.Destructive, false)
325 .annotate(Tool.Idempotent, true)
326 .annotate(Tool.OpenWorld, false);
327​
328export const PullRequestsToolkit = Toolkit.make(
329 LinkPullRequestTool,
330 UnlinkPullRequestTool,
331 ListThreadPullRequestsTool,
332 WatchPullRequestTool,
333 UnwatchPullRequestTool,
334);