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

tools.ts

390 lines12.7 KB

1import {
2 IssueCapabilities,
3 IssueListState,
4 IssueOperationError,
5 IssueState,
6 IssueTrackerKind,
7 IssueUnavailableError,
8 McpCapabilityUnavailableError,
9 OrchestratorMcpFailure,
10 ProjectId,
11 ThreadId,
12 ThreadIssueLinkSource,
13 TrimmedNonEmptyString,
14} from "@t3tools/contracts";
15import * as Schema from "effect/Schema";
16import * as Tool from "effect/ai/Tool";
17import * as Toolkit from "effect/ai/Toolkit";
18​
19import * as IssueService from "../../../issue/IssueService.ts";
20import * as Orchestrator from "../../../orchestration-v2/Orchestrator.ts";
21import * as ThreadIssueService from "../../../orchestration-v2/ThreadIssueService.ts";
22import * as ThreadManagementService from "../../../orchestration-v2/ThreadManagementService.ts";
23import * as ProjectService from "../../../project/ProjectService.ts";
24import * as McpInvocationContext from "../../McpInvocationContext.ts";
25​
26const dependencies = [
27 McpInvocationContext.McpInvocationContext,
28 ThreadManagementService.ThreadManagementService,
29 Orchestrator.OrchestratorV2,
30 ProjectService.ProjectService,
31 IssueService.IssueService,
32 ThreadIssueService.ThreadIssueService,
33];
34​
35/**
36 * Either the issue's URL, or its key within a project's tracker. A key alone is read through the
37 * project named, or the caller's own thread's project; a URL is read through whichever project
38 * here is a checkout of that repository, read-only when none is.
39 */
40export const IssueTargetInput = Schema.Struct({
41 url: Schema.optional(
42 TrimmedNonEmptyString.annotate({
43 description: "The issue's web URL. Preferred when you have it.",
44 }),
45 ),
46 key: Schema.optional(
47 TrimmedNonEmptyString.annotate({
48 description: "The issue's key in the project's tracker, such as its number.",
49 }),
50 ),
51 projectId: Schema.optional(
52 ProjectId.annotate({
53 description: "Project whose tracker holds key. Defaults to this thread's project.",
54 }),
55 ),
56});
57export type IssueTargetInput = typeof IssueTargetInput.Type;
58​
59const ThreadTargetInput = Schema.Struct({
60 threadId: Schema.optional(
61 ThreadId.annotate({ description: "Thread to act on. Omit for this thread." }),
62 ),
63});
64​
65export class IssueTargetIncompleteError extends Schema.TaggedError<IssueTargetIncompleteError>()(
66 "IssueTargetIncompleteError",
67 {},
68) {
69 override get message(): string {
70 return "Pass either url, or key (with projectId when not running inside a T3 thread).";
71 }
73​
74export class IssueProjectRequiredError extends Schema.TaggedError<IssueProjectRequiredError>()(
75 "IssueProjectRequiredError",
76 {},
77) {
78 override get message(): string {
79 return "Pass projectId: this MCP client is not running inside a T3 thread.";
80 }
82​
83export class IssueThreadRequiredError extends Schema.TaggedError<IssueThreadRequiredError>()(
84 "IssueThreadRequiredError",
85 {},
86) {
87 override get message(): string {
88 return "Pass threadId: this MCP client is not running inside a T3 thread.";
89 }
91​
92export class IssueThreadNotFoundError extends Schema.TaggedError<IssueThreadNotFoundError>()(
93 "IssueThreadNotFoundError",
94 { threadId: Schema.String },
95) {
96 override get message(): string {
97 return `Thread ${this.threadId} was not found.`;
98 }
100​
101export class IssueProjectNotFoundError extends Schema.TaggedError<IssueProjectNotFoundError>()(
102 "IssueProjectNotFoundError",
103 { projectId: Schema.String },
104) {
105 override get message(): string {
106 return `Project ${this.projectId} was not found or has no issue tracker.`;
107 }
109​
110export class IssueLinkFailedError extends Schema.TaggedError<IssueLinkFailedError>()(
111 "IssueLinkFailedError",
112 { cause: Schema.Defect() },
113) {
114 override get message(): string {
115 return "Could not change the issue link.";
116 }
118​
119export const IssueToolError = Schema.Union([
120 OrchestratorMcpFailure,
121 McpCapabilityUnavailableError,
122 IssueUnavailableError,
123 IssueOperationError,
124 IssueTargetIncompleteError,
125 IssueProjectRequiredError,
126 IssueThreadRequiredError,
127 IssueThreadNotFoundError,
128 IssueProjectNotFoundError,
129 IssueLinkFailedError,
130]);
131export type IssueToolError = typeof IssueToolError.Type;
132​
133/** How a tool names an issue back: enough to pass to any other issue tool. */
134const IssueKeyIdentity = {
135 projectId: ProjectId,
136 tracker: IssueTrackerKind,
137 host: Schema.String,
138 scope: Schema.String,
139 key: Schema.String,
140};
141const IssueIdentity = { ...IssueKeyIdentity, url: Schema.String };
142/** A write named by key alone never read the issue, so it has no URL to hand back. */
143const IssueIdentityAfterWrite = { ...IssueKeyIdentity, url: Schema.optional(Schema.String) };
144​
145export const IssueEntry = Schema.Struct({
146 ...IssueIdentity,
147 displayKey: Schema.String,
148 title: Schema.String,
149 state: IssueState,
150 author: Schema.NullOr(Schema.String),
151 assignees: Schema.Array(Schema.String),
152 labels: Schema.Array(Schema.String),
153 updatedAt: Schema.String,
154 commentCount: Schema.Int,
155});
156export type IssueEntry = typeof IssueEntry.Type;
157​
158export const ListIssuesResult = Schema.Struct({
159 issues: Schema.Array(IssueEntry),
160 /** Pass back as `cursors` to read the next slice of each scope that has one. */
161 nextCursors: Schema.Record(Schema.String, Schema.String),
162 errors: Schema.Array(Schema.Struct({ projectId: ProjectId, message: Schema.String })),
163});
164export type ListIssuesResult = typeof ListIssuesResult.Type;
165​
166export const ReadIssueResult = Schema.Struct({
167 ...IssueIdentity,
168 displayKey: Schema.String,
169 title: Schema.String,
170 state: IssueState,
171 /** False for an issue outside every project's own scope: it can be read, not changed. */
172 writable: Schema.Boolean,
173 capabilities: IssueCapabilities,
174 states: Schema.Array(IssueState),
175 author: Schema.NullOr(Schema.String),
176 assignees: Schema.Array(Schema.String),
177 labels: Schema.Array(Schema.String),
178 createdAt: Schema.String,
179 updatedAt: Schema.String,
180 closedAt: Schema.NullOr(Schema.String),
181 body: Schema.String,
182 comments: Schema.Array(
183 Schema.Struct({
184 id: Schema.String,
185 author: Schema.NullOr(Schema.String),
186 body: Schema.String,
187 createdAt: Schema.String,
188 }),
189 ),
190 /** Older comments exist beyond the page returned. */
191 commentsTruncated: Schema.Boolean,
192 linkedPullRequests: Schema.Array(
193 Schema.Struct({
194 url: Schema.String,
195 title: Schema.String,
196 state: Schema.String,
197 closesIssue: Schema.Boolean,
198 }),
199 ),
200});
201export type ReadIssueResult = typeof ReadIssueResult.Type;
202​
203export const CreateIssueResult = Schema.Struct({
204 ...IssueIdentity,
205 displayKey: Schema.String,
206});
207export type CreateIssueResult = typeof CreateIssueResult.Type;
208​
209export const IssueWriteResult = Schema.Struct({ ...IssueIdentityAfterWrite });
210​
211export const ThreadIssueEntry = Schema.Struct({
212 ...IssueIdentity,
213 source: ThreadIssueLinkSource,
214 /** As the tracker named the issue when it was linked; `read_issue` has its current state. */
215 displayKey: Schema.String,
216 title: Schema.String,
217});
218export type ThreadIssueEntry = typeof ThreadIssueEntry.Type;
219​
220export const LinkIssueResult = Schema.Struct({
221 ...IssueIdentity,
222 alreadyLinked: Schema.Boolean.annotate({
223 description: "True when the issue was linked to this thread before the call.",
224 }),
225});
226export type LinkIssueResult = typeof LinkIssueResult.Type;
227​
228export const UnlinkIssueResult = Schema.Struct({
229 ...IssueIdentityAfterWrite,
230 wasLinked: Schema.Boolean.annotate({
231 description: "False when the issue was not linked to this thread to begin with.",
232 }),
233});
234export type UnlinkIssueResult = typeof UnlinkIssueResult.Type;
235​
236export const ListThreadIssuesResult = Schema.Struct({
237 issues: Schema.Array(ThreadIssueEntry),
238});
239export type ListThreadIssuesResult = typeof ListThreadIssuesResult.Type;
240​
241const ListIssuesTool = Tool.make("list_issues", {
242 description:
243 "List issues in a project's tracker, newest updated first. Pass the returned nextCursors as cursors for the next page.",
244 parameters: Schema.Struct({
245 projectId: Schema.optional(
246 ProjectId.annotate({ description: "Defaults to this thread's project." }),
247 ),
248 state: Schema.optional(IssueListState.annotate({ description: "Defaults to open." })),
249 query: Schema.optional(
250 TrimmedNonEmptyString.check(Schema.isMaxLength(200)).annotate({
251 description: "Free text the tracker matches itself.",
252 }),
253 ),
254 limit: Schema.optional(Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 100 }))),
255 cursors: Schema.optional(Schema.Record(Schema.String, Schema.String)),
256 }),
257 success: ListIssuesResult,
258 failure: IssueToolError,
259 dependencies,
260})
261 .annotate(Tool.Title, "List issues")
262 .annotate(Tool.Readonly, true)
263 .annotate(Tool.Destructive, false)
264 .annotate(Tool.Idempotent, true)
265 .annotate(Tool.OpenWorld, true);
266​
267const ReadIssueTool = Tool.make("read_issue", {
268 description:
269 "Read an issue: body, state, labels, assignees, recent comments and linked pull requests. Pass its url, or its key.",
270 parameters: IssueTargetInput,
271 success: ReadIssueResult,
272 failure: IssueToolError,
273 dependencies,
274})
275 .annotate(Tool.Title, "Read issue")
276 .annotate(Tool.Readonly, true)
277 .annotate(Tool.Destructive, false)
278 .annotate(Tool.Idempotent, true)
279 .annotate(Tool.OpenWorld, true);
280​
281const CreateIssueTool = Tool.make("create_issue", {
282 description: "File an issue in a project's tracker.",
283 parameters: Schema.Struct({
284 projectId: Schema.optional(
285 ProjectId.annotate({ description: "Defaults to this thread's project." }),
286 ),
287 scope: Schema.optional(
288 TrimmedNonEmptyString.annotate({
289 description: "A bound team or repository, such as OPS. Defaults to the project's first.",
290 }),
291 ),
292 title: TrimmedNonEmptyString.check(Schema.isMaxLength(1024)),
293 body: Schema.optional(Schema.String.check(Schema.isMaxLength(65_536))),
294 labels: Schema.optional(Schema.Array(TrimmedNonEmptyString).check(Schema.isMaxLength(50))),
295 assignees: Schema.optional(Schema.Array(TrimmedNonEmptyString).check(Schema.isMaxLength(25))),
296 }),
297 success: CreateIssueResult,
298 failure: IssueToolError,
299 dependencies,
300})
301 .annotate(Tool.Title, "Create issue")
302 .annotate(Tool.Readonly, false)
303 .annotate(Tool.Destructive, false)
304 .annotate(Tool.Idempotent, false)
305 .annotate(Tool.OpenWorld, true);
306​
307const CommentOnIssueTool = Tool.make("comment_on_issue", {
308 description: "Post a comment on an issue.",
309 parameters: Schema.Struct({
310 ...IssueTargetInput.fields,
311 body: Schema.String.check(Schema.isNonEmpty()).check(Schema.isMaxLength(65_536)),
312 }),
313 success: IssueWriteResult,
314 failure: IssueToolError,
315 dependencies,
316})
317 .annotate(Tool.Title, "Comment on issue")
318 .annotate(Tool.Readonly, false)
319 .annotate(Tool.Destructive, false)
320 .annotate(Tool.Idempotent, false)
321 .annotate(Tool.OpenWorld, true);
322​
323const SetIssueStateTool = Tool.make("set_issue_state", {
324 description: "Move an issue to one of its states. read_issue lists them under states.",
325 parameters: Schema.Struct({
326 ...IssueTargetInput.fields,
327 state: TrimmedNonEmptyString.annotate({
328 description: "A state name from read_issue's states.",
329 }),
330 }),
331 success: IssueWriteResult,
332 failure: IssueToolError,
333 dependencies,
334})
335 .annotate(Tool.Title, "Set issue state")
336 .annotate(Tool.Readonly, false)
337 .annotate(Tool.Destructive, false)
338 .annotate(Tool.Idempotent, true)
339 .annotate(Tool.OpenWorld, true);
340​
341const LinkIssueTool = Tool.make("link_issue", {
342 description:
343 "Link an issue to a thread so T3 Code shows it beside the thread. Link every issue you work on or file.",
344 parameters: Schema.Struct({ ...IssueTargetInput.fields, ...ThreadTargetInput.fields }),
345 success: LinkIssueResult,
346 failure: IssueToolError,
347 dependencies,
348})
349 .annotate(Tool.Title, "Link issue to thread")
350 .annotate(Tool.Readonly, false)
351 .annotate(Tool.Destructive, false)
352 .annotate(Tool.Idempotent, true)
353 .annotate(Tool.OpenWorld, false);
354​
355const UnlinkIssueTool = Tool.make("unlink_issue", {
356 description: "Remove an issue link from a thread. The issue itself is not changed.",
357 parameters: Schema.Struct({ ...IssueTargetInput.fields, ...ThreadTargetInput.fields }),
358 success: UnlinkIssueResult,
359 failure: IssueToolError,
360 dependencies,
361})
362 .annotate(Tool.Title, "Unlink issue from thread")
363 .annotate(Tool.Readonly, false)
364 .annotate(Tool.Destructive, true)
365 .annotate(Tool.Idempotent, true)
366 .annotate(Tool.OpenWorld, false);
367​
368const ListThreadIssuesTool = Tool.make("list_thread_issues", {
369 description: "List the issues linked to a thread.",
370 parameters: ThreadTargetInput,
371 success: ListThreadIssuesResult,
372 failure: IssueToolError,
373 dependencies,
374})
375 .annotate(Tool.Title, "List thread issues")
376 .annotate(Tool.Readonly, true)
377 .annotate(Tool.Destructive, false)
378 .annotate(Tool.Idempotent, true)
379 .annotate(Tool.OpenWorld, false);
380​
381export const IssuesToolkit = Toolkit.make(
382 ListIssuesTool,
383 ReadIssueTool,
384 CreateIssueTool,
385 CommentOnIssueTool,
386 SetIssueStateTool,
387 LinkIssueTool,
388 UnlinkIssueTool,
389 ListThreadIssuesTool,
390);