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

tools.ts

400 lines13.2 KB

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