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

t3McpToolPresentation.ts

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