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

t3McpToolPresentation.ts

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