Open History to see the last commit
1import {
2 McpCapabilityUnavailableError,
3 PositiveInt,
4 PullRequestState,
5 ThreadPullRequestLinkSource,
6 TrimmedNonEmptyString,
7} from "@t3tools/contracts";
8import * as Schema from "effect/Schema";
9import * as Tool from "effect/unstable/ai/Tool";
10import * as Toolkit from "effect/unstable/ai/Toolkit";
11
12import * as McpInvocationContext from "../../McpInvocationContext.ts";
13import * as OrchestrationEngine from "../../../orchestration/Services/OrchestrationEngine.ts";
14import * as ProjectionSnapshotQuery from "../../../orchestration/Services/ProjectionSnapshotQuery.ts";
15
16const dependencies = [
17 McpInvocationContext.McpInvocationContext,
18 OrchestrationEngine.OrchestrationEngineService,
19 ProjectionSnapshotQuery.ProjectionSnapshotQuery,
20];
21
22const REGISTER_EVERY_PR =
23 "Register every pull request you open for this thread, including each layer of a stack, right after creating it.";
24
25/**
26 * Either the pull request's URL or its repository and number. Both forms
27 * resolve to the same host-level identity, so the agent can pass whichever
28 * the host CLI handed back.
29 */
30export const PullRequestTargetInput = Schema.Struct({
31 url: Schema.optional(
32 TrimmedNonEmptyString.annotate({
33 description:
34 "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.",
35 }),
36 ),
37 repository: Schema.optional(
38 TrimmedNonEmptyString.annotate({
39 description:
40 "Repository path below the host, for example owner/repo. Required with number when url is omitted.",
41 }),
42 ),
43 number: Schema.optional(
44 PositiveInt.annotate({
45 description: "Pull request number. Required with repository when url is omitted.",
46 }),
47 ),
48 host: Schema.optional(
49 TrimmedNonEmptyString.annotate({
50 description:
51 "Host the repository lives on, for example github.com. Defaults to the host of this thread's project.",
52 }),
53 ),
54});
55export type PullRequestTargetInput = typeof PullRequestTargetInput.Type;
56
57export class PullRequestUrlInvalidError extends Schema.TaggedError<PullRequestUrlInvalidError>()(
58 "PullRequestUrlInvalidError",
59 {},
60) {
61 override get message(): string {
62 return "This is not a recognised pull request URL. Pass repository and number instead.";
63 }
64}
65
66export class PullRequestTargetIncompleteError extends Schema.TaggedError<PullRequestTargetIncompleteError>()(
67 "PullRequestTargetIncompleteError",
68 {},
69) {
70 override get message(): string {
71 return "Pass either url, or both repository and number.";
72 }
73}
74
75export class PullRequestHostRequiredError extends Schema.TaggedError<PullRequestHostRequiredError>()(
76 "PullRequestHostRequiredError",
77 {},
78) {
79 override get message(): string {
80 return "This thread's project has no recognised remote. Pass host or url.";
81 }
82}
83
84export class PullRequestThreadNotFoundError extends Schema.TaggedError<PullRequestThreadNotFoundError>()(
85 "PullRequestThreadNotFoundError",
86 { threadId: Schema.String },
87) {
88 override get message(): string {
89 return `Thread ${this.threadId} was not found.`;
90 }
91}
92
93export class PullRequestLinkFailedError extends Schema.TaggedError<PullRequestLinkFailedError>()(
94 "PullRequestLinkFailedError",
95 { cause: Schema.Defect() },
96) {
97 override get message(): string {
98 return "Could not link the pull request.";
99 }
100}
101
102export class PullRequestUnlinkFailedError extends Schema.TaggedError<PullRequestUnlinkFailedError>()(
103 "PullRequestUnlinkFailedError",
104 { cause: Schema.Defect() },
105) {
106 override get message(): string {
107 return "Could not unlink the pull request.";
108 }
109}
110
111export class PullRequestListFailedError extends Schema.TaggedError<PullRequestListFailedError>()(
112 "PullRequestListFailedError",
113 { cause: Schema.Defect() },
114) {
115 override get message(): string {
116 return "Could not list the pull request.";
117 }
118}
119
120export const PullRequestToolError = Schema.Union([
121 McpCapabilityUnavailableError,
122 PullRequestUrlInvalidError,
123 PullRequestTargetIncompleteError,
124 PullRequestHostRequiredError,
125 PullRequestThreadNotFoundError,
126 PullRequestLinkFailedError,
127 PullRequestUnlinkFailedError,
128 PullRequestListFailedError,
129]);
130export type PullRequestToolError = typeof PullRequestToolError.Type;
131
132const PullRequestIdentity = {
133 host: Schema.String,
134 repository: Schema.String,
135 number: Schema.Int,
136 url: Schema.String,
137};
138
139export const LinkPullRequestResult = Schema.Struct({
140 ...PullRequestIdentity,
141 alreadyLinked: Schema.Boolean.annotate({
142 description: "True when the pull request was linked to this thread before the call.",
143 }),
144});
145export type LinkPullRequestResult = typeof LinkPullRequestResult.Type;
146
147export const UnlinkPullRequestResult = Schema.Struct({
148 host: Schema.String,
149 repository: Schema.String,
150 number: Schema.Int,
151 wasLinked: Schema.Boolean.annotate({
152 description: "False when the pull request was not linked to this thread to begin with.",
153 }),
154});
155export type UnlinkPullRequestResult = typeof UnlinkPullRequestResult.Type;
156
157export const ThreadPullRequestEntry = Schema.Struct({
158 ...PullRequestIdentity,
159 source: ThreadPullRequestLinkSource,
160 state: Schema.NullOr(PullRequestState),
161 title: Schema.NullOr(Schema.String),
162 headBranch: Schema.NullOr(Schema.String),
163 baseBranch: Schema.NullOr(Schema.String),
164 isDraft: Schema.NullOr(Schema.Boolean),
165 stack: Schema.NullOr(
166 Schema.Struct({
167 kind: Schema.Literals(["native", "derived"]),
168 /** 1-based, bottom of the stack first. */
169 position: Schema.Int,
170 size: Schema.Int,
171 }),
172 ),
173});
174export type ThreadPullRequestEntry = typeof ThreadPullRequestEntry.Type;
175
176export const ListThreadPullRequestsResult = Schema.Struct({
177 pullRequests: Schema.Array(ThreadPullRequestEntry),
178 chains: Schema.Array(
179 Schema.Struct({
180 kind: Schema.Literals(["native", "derived"]),
181 /** Bottom to top. */
182 numbers: Schema.Array(Schema.Int),
183 }),
184 ),
185});
186export type ListThreadPullRequestsResult = typeof ListThreadPullRequestsResult.Type;
187
188const LinkPullRequestTool = Tool.make("link_pull_request", {
189 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.`,
190 parameters: PullRequestTargetInput,
191 success: LinkPullRequestResult,
192 failure: PullRequestToolError,
193 dependencies,
194})
195 .annotate(Tool.Title, "Link pull request to thread")
196 .annotate(Tool.Readonly, false)
197 .annotate(Tool.Destructive, false)
198 .annotate(Tool.Idempotent, true)
199 .annotate(Tool.OpenWorld, false);
200
201const UnlinkPullRequestTool = Tool.make("unlink_pull_request", {
202 description:
203 "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.",
204 parameters: PullRequestTargetInput,
205 success: UnlinkPullRequestResult,
206 failure: PullRequestToolError,
207 dependencies,
208})
209 .annotate(Tool.Title, "Unlink pull request from thread")
210 .annotate(Tool.Readonly, false)
211 .annotate(Tool.Destructive, true)
212 .annotate(Tool.Idempotent, true)
213 .annotate(Tool.OpenWorld, false);
214
215const ListThreadPullRequestsTool = Tool.make("list_thread_pull_requests", {
216 description: `List the pull requests linked to this thread with their last known host state, and how they chain into stacks (bottom to top). ${REGISTER_EVERY_PR}`,
217 success: ListThreadPullRequestsResult,
218 failure: PullRequestToolError,
219 dependencies,
220})
221 .annotate(Tool.Title, "List thread pull requests")
222 .annotate(Tool.Readonly, true)
223 .annotate(Tool.Destructive, false)
224 .annotate(Tool.Idempotent, true)
225 .annotate(Tool.OpenWorld, false);
226
227export const PullRequestsToolkit = Toolkit.make(
228 LinkPullRequestTool,
229 UnlinkPullRequestTool,
230 ListThreadPullRequestsTool,
231);