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

t3McpToolPresentation.ts

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