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

t3McpToolPresentation.ts

470 lines16.6 KB

1export type T3McpToolLogo = "t3-code";
2​
3export interface T3McpToolPresentation {
4 readonly displayName: string;
5 readonly logo: T3McpToolLogo;
6}
7​
8export type T3McpToolSummaryAction =
9 | "capabilities"
10 | "delegate"
11 | "task-status"
12 | "task-cancel"
13 | "schedule-run"
14 | "schedule-create"
15 | "schedule-list"
16 | "schedule-update"
17 | "schedule-delete"
18 | "thread-create"
19 | "thread-list"
20 | "thread-read"
21 | "thread-send"
22 | "thread-wait"
23 | "thread-interrupt"
24 | "workflow-stop"
25 | "background-wait"
26 | "thread-configuration"
27 | "thread-configure"
28 | "thread-fork"
29 | "thread-merge"
30 | "thread-search"
31 | "thread-transfers"
32 | "thread-organize"
33 | "thread-update"
34 | "queue-list"
35 | "queue-read"
36 | "queue-edit"
37 | "queue-cancel"
38 | "queue-reorder"
39 | "queue-steer"
40 | "question-list"
41 | "question-read"
42 | "question-respond"
43 | "secret-request"
44 | "worktree-handoff"
45 | "worktree-list"
46 | "worktree-status"
47 | "project-list"
48 | "project-read"
49 | "project-create"
50 | "project-update"
51 | "project-delete"
52 | "project-clone"
53 | "environment-read"
54 | "environment-links"
55 | "environment-link"
56 | "environment-unlink"
57 | "environment-update"
58 | "attachment-prepare"
59 | "attachment-discard"
60 | "attachment-send"
61 | "link-pr"
62 | "unlink-pr"
63 | "list-prs"
64 | "watch-pr"
65 | "unwatch-pr"
66 | "write-pr-guides"
67 | "read-pr-guide"
68 | "browser"
69 | "device"
70 | "html-preview"
71 | "html-render";
72​
73export interface T3McpToolDefinition {
74 readonly displayName: string;
75 readonly labels: readonly [action: string, running: string, completed: string, detail: string];
76 readonly icon: "t3-code" | "browser" | "device" | "pull-request";
77 readonly summaryAction: T3McpToolSummaryAction;
79​
80function tool(
81 labels: T3McpToolDefinition["labels"],
82 summaryAction: T3McpToolSummaryAction,
83 icon: T3McpToolDefinition["icon"] = "t3-code",
84 displayName = `${labels[0]} ${labels[3]}`,
85): T3McpToolDefinition {
86 return { displayName, labels, icon, summaryAction };
88​
89const T3_MCP_SERVER_ALIASES = new Set(["t3-code", "t3_code", "t3code"]);
90​
91const T3_MCP_TOOLS: Readonly<Record<string, T3McpToolDefinition>> = {
92 link_pull_request: tool(
93 ["Link", "Linking", "Linked", "a pull request"],
94 "link-pr",
95 "pull-request",
96 ),
97 link_pull_requests: tool(
98 ["Link", "Linking", "Linked", "pull requests"],
99 "link-pr",
100 "pull-request",
101 ),
102 unlink_pull_request: tool(
103 ["Unlink", "Unlinking", "Unlinked", "a pull request"],
104 "unlink-pr",
105 "pull-request",
106 ),
107 unlink_pull_requests: tool(
108 ["Unlink", "Unlinking", "Unlinked", "pull requests by state"],
109 "unlink-pr",
110 "pull-request",
111 ),
112 list_thread_pull_requests: tool(
113 ["Check", "Checking", "Checked", "linked pull requests"],
114 "list-prs",
115 "pull-request",
116 ),
117 watch_pull_request: tool(
118 ["Watch", "Watching", "Watching", "a pull request"],
119 "watch-pr",
120 "pull-request",
121 ),
122 unwatch_pull_request: tool(
123 ["Stop watching", "Unwatching", "Stopped watching", "a pull request"],
124 "unwatch-pr",
125 "pull-request",
126 ),
127 write_pull_request_guides: tool(
128 ["Write", "Writing", "Started writing", "review guides"],
129 "write-pr-guides",
130 "pull-request",
131 ),
132 read_pull_request_guide: tool(
133 ["Read", "Reading", "Read", "a review guide"],
134 "read-pr-guide",
135 "pull-request",
136 ),
137 orchestrator_capabilities: tool(
138 ["Get", "Getting", "Got", "orchestration capabilities"],
139 "capabilities",
140 ),
141 delegate_task: tool(["Delegate", "Delegating", "Delegated", "a child task"], "delegate"),
142 task_status: tool(["Get", "Getting", "Got", "delegated task status"], "task-status"),
143 task_cancel: tool(
144 ["Cancel", "Canceling", "Requested cancellation of", "a delegated task"],
145 "task-cancel",
146 ),
147 schedule_task: tool(["Schedule", "Scheduling", "Scheduled", "a task"], "schedule-create"),
148 list_scheduled_tasks: tool(["List", "Listing", "Listed", "scheduled tasks"], "schedule-list"),
149 update_scheduled_task: tool(
150 ["Update", "Updating", "Updated", "a scheduled task"],
151 "schedule-update",
152 ),
153 delete_scheduled_task: tool(
154 ["Delete", "Deleting", "Requested deletion of", "a scheduled task"],
155 "schedule-delete",
156 ),
157 request_secret: tool(["Ask for", "Asking for", "Asked for", "a secret"], "secret-request"),
158 create_threads: tool(["Create", "Creating", "Created", "T3 threads"], "thread-create"),
159 t3_thread_start: tool(["Start", "Starting", "Started", "a T3 thread"], "thread-create"),
160 t3_thread_list: tool(["List", "Listing", "Listed", "T3 threads"], "thread-list"),
161 t3_thread_read: tool(["Read", "Reading", "Read", "a T3 thread"], "thread-read"),
162 t3_thread_send: tool(["Send", "Sending", "Sent", "to a T3 thread"], "thread-send"),
163 t3_thread_wait: tool(["Wait", "Waiting", "Waited", "for a T3 thread"], "thread-wait"),
164 t3_workflow_stop: tool(
165 ["Stop", "Stopping", "Requested a stop for", "a workflow"],
166 "workflow-stop",
167 ),
168 t3_background_wait: tool(
169 ["Update", "Updating", "Updated", "background waits"],
170 "background-wait",
171 ),
172 t3_thread_interrupt: tool(
173 ["Interrupt", "Interrupting", "Requested an interrupt of", "a T3 thread"],
174 "thread-interrupt",
175 ),
176 t3_worktree_handoff: tool(
177 ["Hand off", "Handing off", "Handed off", "thread to a git worktree"],
178 "worktree-handoff",
179 ),
180 t3_worktree_status: tool(["Get", "Getting", "Got", "thread worktree status"], "worktree-status"),
181 preview_status: tool(["Get", "Getting", "Got", "preview browser status"], "browser", "browser"),
182 preview_open: tool(
183 ["Open", "Opening", "Opened", "a page in the preview browser"],
184 "browser",
185 "browser",
186 ),
187 preview_navigate: tool(
188 ["Navigate", "Navigating", "Navigated", "the preview browser"],
189 "browser",
190 "browser",
191 ),
192 preview_dialog: tool(
193 ["Respond", "Responding", "Responded", "to a preview browser dialog"],
194 "browser",
195 "browser",
196 ),
197 preview_snapshot: tool(
198 ["Take a snapshot of", "Taking a snapshot of", "Took a snapshot of", "the preview page"],
199 "browser",
200 "browser",
201 "Snapshot the preview page",
202 ),
203 preview_act: tool(["Use", "Using", "Used", "the preview browser"], "browser", "browser"),
204 preview_click: tool(
205 ["Click", "Clicking", "Clicked", "in the preview browser"],
206 "browser",
207 "browser",
208 ),
209 preview_press: tool(
210 ["Press", "Pressing", "Pressed", "a key in the preview browser"],
211 "browser",
212 "browser",
213 ),
214 preview_type: tool(["Type", "Typing", "Typed", "in the preview browser"], "browser", "browser"),
215 preview_hover: tool(
216 ["Hover", "Hovering", "Hovered", "in the preview browser"],
217 "browser",
218 "browser",
219 ),
220 preview_select: tool(
221 ["Choose", "Choosing", "Chose", "an option in the preview browser"],
222 "browser",
223 "browser",
224 ),
225 preview_drag: tool(
226 ["Drag", "Dragging", "Dragged", "in the preview browser"],
227 "browser",
228 "browser",
229 ),
230 preview_upload: tool(
231 ["Upload", "Uploading", "Uploaded", "files to the preview browser"],
232 "browser",
233 "browser",
234 ),
235 preview_scroll: tool(
236 ["Scroll", "Scrolling", "Scrolled", "the preview browser"],
237 "browser",
238 "browser",
239 ),
240 preview_resize: tool(
241 ["Resize", "Resizing", "Resized", "the preview browser"],
242 "browser",
243 "browser",
244 ),
245 preview_evaluate: tool(
246 ["Evaluate", "Evaluating", "Evaluated", "script in the preview browser"],
247 "browser",
248 "browser",
249 ),
250 preview_wait_for: tool(
251 ["Wait", "Waiting", "Waited", "for the preview page"],
252 "browser",
253 "browser",
254 ),
255 preview_set_appearance: tool(
256 ["Set", "Setting", "Set", "preview browser appearance"],
257 "browser",
258 "browser",
259 ),
260 preview_recording_start: tool(
261 ["Start", "Starting", "Started", "recording the preview browser"],
262 "browser",
263 "browser",
264 ),
265 preview_recording_stop: tool(
266 ["Stop", "Stopping", "Stopped", "recording the preview browser"],
267 "browser",
268 "browser",
269 ),
270 device_list: tool(["List", "Listing", "Listed", "simulators and emulators"], "device", "device"),
271 device_open: tool(
272 ["Open", "Opening", "Opened", "a device in the Device panel"],
273 "device",
274 "device",
275 ),
276 device_screenshot: tool(
277 ["Take a screenshot of", "Taking a screenshot of", "Took a screenshot of", "the device"],
278 "device",
279 "device",
280 ),
281 device_close: tool(["Close", "Closing", "Closed", "a device"], "device", "device"),
282 run_scheduled_task_now: tool(
283 ["Run", "Running", "Requested a run of", "a scheduled task"],
284 "schedule-run",
285 ),
286 t3_queue: tool(["Manage", "Managing", "Managed", "queued messages"], "queue-edit"),
287 t3_pending_request: tool(
288 ["Handle", "Handling", "Handled", "pending questions"],
289 "question-respond",
290 ),
291 t3_queue_list: tool(["List", "Listing", "Listed", "queued messages"], "queue-list"),
292 t3_queue_read: tool(["Read", "Reading", "Read", "a queued message"], "queue-read"),
293 t3_queue_edit: tool(["Edit", "Editing", "Edited", "a queued message"], "queue-edit"),
294 t3_queue_cancel: tool(
295 ["Cancel", "Canceling", "Requested cancellation of", "a queued run"],
296 "queue-cancel",
297 ),
298 t3_queue_reorder: tool(["Reorder", "Reordering", "Reordered", "a queued run"], "queue-reorder"),
299 t3_queue_promote_to_steer: tool(
300 ["Steer with", "Steering with", "Requested steering with", "a queued message"],
301 "queue-steer",
302 ),
303 t3_pending_request_list: tool(
304 ["List", "Listing", "Listed", "pending questions"],
305 "question-list",
306 ),
307 t3_pending_request_read: tool(["Read", "Reading", "Read", "pending questions"], "question-read"),
308 t3_pending_request_respond: tool(
309 ["Answer", "Answering", "Answered", "pending questions"],
310 "question-respond",
311 ),
312 t3_thread_configuration: tool(
313 ["Read", "Reading", "Read", "thread configuration"],
314 "thread-configuration",
315 ),
316 t3_thread_configure: tool(["Use", "Using", "Used", "thread model settings"], "thread-configure"),
317 t3_thread_transfer: tool(["Use", "Using", "Used", "thread transfers"], "thread-fork"),
318 t3_thread_fork: tool(["Fork", "Forking", "Requested a fork of", "this thread"], "thread-fork"),
319 t3_thread_merge_back: tool(
320 ["Merge", "Merging", "Requested a merge of", "thread context"],
321 "thread-merge",
322 ),
323 t3_thread_search: tool(["Search", "Searching", "Searched", "thread content"], "thread-search"),
324 t3_thread_transfers: tool(["Read", "Reading", "Read", "thread transfers"], "thread-transfers"),
325 t3_thread_organize: tool(["Organize", "Organizing", "Organized", "a thread"], "thread-organize"),
326 t3_thread_update: tool(["Update", "Updating", "Updated", "T3 thread metadata"], "thread-update"),
327 t3_worktree_list: tool(["List", "Listing", "Listed", "workspace branches"], "worktree-list"),
328 t3_preview_list: tool(["List", "Listing", "Listed", "preview tabs"], "browser", "browser"),
329 t3_preview_close: tool(["Close", "Closing", "Closed", "a preview tab"], "browser", "browser"),
330 t3_environment_read: tool(
331 ["Read", "Reading", "Read", "environment preferences"],
332 "environment-read",
333 ),
334 t3_environment_links: tool(
335 ["List", "Listing", "Listed", "linked environments"],
336 "environment-links",
337 ),
338 t3_environment_link: tool(
339 ["Ask to link", "Asking to link", "Asked to link", "an environment"],
340 "environment-link",
341 ),
342 t3_environment_unlink: tool(
343 ["Forget", "Forgetting", "Forgot", "a linked environment"],
344 "environment-unlink",
345 ),
346 t3_environment_preferences_update: tool(
347 ["Update", "Updating", "Updated", "environment preferences"],
348 "environment-update",
349 ),
350 t3_thread_launch: tool(["Launch", "Launching", "Launched", "a project thread"], "thread-create"),
351 t3_project_list: tool(["List", "Listing", "Listed", "projects"], "project-list"),
352 t3_project_read: tool(["Read", "Reading", "Read", "a project"], "project-read"),
353 t3_project_create: tool(["Register", "Registering", "Registered", "a project"], "project-create"),
354 t3_project_update: tool(["Update", "Updating", "Updated", "a project"], "project-update"),
355 t3_project_delete: tool(["Delete", "Deleting", "Deleted", "a project"], "project-delete"),
356 t3_project_clone: tool(["Clone", "Cloning", "Cloned", "a repository"], "project-clone"),
357 t3_attachment_prepare_upload: tool(
358 ["Prepare", "Preparing", "Prepared", "an attachment upload"],
359 "attachment-prepare",
360 ),
361 t3_attachment_discard: tool(
362 ["Discard", "Discarding", "Discarded", "a pending attachment"],
363 "attachment-discard",
364 ),
365 t3_thread_send_attachments: tool(["Send", "Sending", "Sent", "attachments"], "attachment-send"),
366 html_preview: tool(["Preview", "Previewing", "Previewed", "an HTML page"], "html-preview"),
367 html_render: tool(["Render", "Rendering", "Rendered", "an HTML page"], "html-render"),
368};
369​
370export const T3_MCP_TOOL_NAMES: ReadonlySet<string> = new Set(Object.keys(T3_MCP_TOOLS));
371​
372function normalizeT3McpToolLabel(value: string): string {
373 return value.replace(/\s+(?:complete|completed)\s*$/i, "").trim();
375​
376// ACP tool prefixes vary; match loosely but require a known display-name entry.
377function resolveT3McpToolName(value: string): string | null {
378 const label = normalizeT3McpToolLabel(value);
379 const mcpMatch = /^mcp__(?<server>.+?)__(?<tool>.+)$/i.exec(label);
380 if (mcpMatch?.groups) {
381 const { server, tool } = mcpMatch.groups;
382 return server !== undefined &&
383 tool !== undefined &&
384 T3_MCP_SERVER_ALIASES.has(server.toLowerCase())
385 ? tool
386 : null;
387 }
388​
389 const namespaceMatch = /^(?<server>t3-code|t3_code|t3code)(?:[.:/]|\s*·\s*)(?<tool>.+)$/i.exec(
390 label,
391 );
392 if (namespaceMatch?.groups) {
393 return namespaceMatch.groups.tool ?? null;
394 }
395​
396 const prefixed = /^(?:mcp[-_]{1,2})?t3[-_ ]?code(?:__|[-_.:/ ])(?<tool>.+)$/i.exec(label);
397 const candidate = prefixed?.groups?.tool ?? label;
398 if (Object.hasOwn(T3_MCP_TOOLS, candidate)) return candidate;
399 // OpenCode 2 thread prefixes can contain underscores; choose the longest known tool-name suffix.
400 if (!/^t3-code-/i.test(label)) return null;
401 let longest: string | null = null;
402 for (const tool of Object.keys(T3_MCP_TOOLS)) {
403 if (label.endsWith(`_${tool}`) && tool.length > (longest?.length ?? 0)) longest = tool;
404 }
405 return longest;
407​
408export function resolveT3McpToolId(toolName: string | null | undefined): string | null {
409 const name = toolName == null ? null : resolveT3McpToolName(toolName);
410 return name !== null && Object.hasOwn(T3_MCP_TOOLS, name) ? name : null;
412​
413type SummaryInput = Readonly<Record<string, unknown>>;
414​
415const asSummaryInput = (input: unknown): SummaryInput | undefined =>
416 typeof input === "object" && input !== null ? (input as SummaryInput) : undefined;
417​
418/** Action tools present each call as the single-purpose tool its action replaced. */
419const ACTION_TOOLS: Readonly<Record<string, (action: string) => string | undefined>> = {
420 preview_act: (action) => `preview_${action}`,
421 t3_queue: (action) => `t3_queue_${action}`,
422 t3_pending_request: (action) => `t3_pending_request_${action}`,
423 t3_thread_transfer: (action) =>
424 ({ fork: "t3_thread_fork", merge_back: "t3_thread_merge_back", status: "t3_thread_transfers" })[
425 action
426 ],
427};
428​
429/** Pass the call's input so an action tool resolves to the labels and summary of its action. */
430export function resolveT3McpToolDefinition(
431 toolName: string | null | undefined,
432 input?: unknown,
433): T3McpToolDefinition | null {
434 const name = toolName == null ? null : resolveT3McpToolName(toolName);
435 if (name === null || !Object.hasOwn(T3_MCP_TOOLS, name)) return null;
436 const action = asSummaryInput(input)?.action;
437 const actionTool = typeof action === "string" ? ACTION_TOOLS[name]?.(action) : undefined;
438 return actionTool !== undefined && Object.hasOwn(T3_MCP_TOOLS, actionTool)
439 ? T3_MCP_TOOLS[actionTool]!
440 : T3_MCP_TOOLS[name]!;
442​
443/** Tools whose summary depends on which fields a call sets. */
444const ACTION_SUMMARIES: Readonly<
445 Record<string, (input: SummaryInput) => T3McpToolSummaryAction | undefined>
446> = {
447 t3_thread_configure: ({ modelSelection }) =>
448 modelSelection === undefined ? "thread-configuration" : "thread-configure",
449 schedule_task: ({ scheduledTaskId }) =>
450 scheduledTaskId === undefined ? "schedule-create" : "schedule-update",
451};
452​
453export function resolveT3McpSummaryAction(
454 toolName: string | null | undefined,
455 input: unknown,
456): T3McpToolSummaryAction | null {
457 const definition = resolveT3McpToolDefinition(toolName, input);
458 if (definition === null) return null;
459 const summarize = ACTION_SUMMARIES[resolveT3McpToolName(toolName!) ?? ""];
460 const fields = asSummaryInput(input);
461 return (fields && summarize?.(fields)) ?? definition.summaryAction;
463​
464export function resolveT3McpToolPresentation(
465 toolName: string | null | undefined,
466 input?: unknown,
467): T3McpToolPresentation | null {
468 const definition = resolveT3McpToolDefinition(toolName, input);
469 return definition === null ? null : { displayName: definition.displayName, logo: "t3-code" };