Open History to see the last commit
1import {
2 OrchestratorMcpFailure,
3 ToolActivityIcon,
4 PreviewAutomationClickInput,
5 PreviewAutomationDialogInput,
6 PreviewAutomationDragInput,
7 PreviewAutomationError,
8 PreviewAutomationEvaluateInput,
9 PreviewAutomationHoverInput,
10 PreviewAutomationNavigateInput,
11 PreviewAutomationOpenInput,
12 PreviewAutomationPressInput,
13 PreviewAutomationRecordingArtifact,
14 PreviewAutomationRecordingStatus,
15 PreviewAutomationResizeInput,
16 PreviewAutomationResizeResult,
17 PreviewAutomationScrollInput,
18 PreviewAutomationSelectInput,
19 PreviewAutomationSelectResult,
20 PreviewAutomationSetColorSchemeInput,
21 PreviewAutomationSetColorSchemeResult,
22 PreviewAutomationSnapshot,
23 PreviewAutomationStatus,
24 PreviewAutomationTabTargetInput,
25 PreviewAutomationTypeInput,
26 PreviewAutomationUploadInput,
27 PreviewAutomationWaitForInput,
28} from "@t3tools/contracts";
29import * as Schema from "effect/Schema";
30import * as FileSystem from "effect/FileSystem";
31import { Tool, Toolkit } from "effect/ai";
32
33import * as McpInvocationContext from "../../McpInvocationContext.ts";
34import * as PreviewAutomationBroker from "../../PreviewAutomationBroker.ts";
35import * as ServerConfig from "../../../config.ts";
36import * as ThreadManagementService from "../../../orchestration-v2/ThreadManagementService.ts";
37
38const dependencies = [
39 McpInvocationContext.McpInvocationContext,
40 ThreadManagementService.ThreadManagementService,
41 PreviewAutomationBroker.PreviewAutomationBroker,
42];
43
44const PreviewToolFailure = Schema.Union([PreviewAutomationError, OrchestratorMcpFailure]);
45
46const presentationFields = { toolIcon: Schema.optional(ToolActivityIcon) };
47
48const PreviewActionResult = Schema.Struct(presentationFields).annotate({
49 description: "The preview action completed successfully.",
50});
51
52const browserTool = <T extends Tool.Any>(tool: T): T =>
53 tool.annotate(Tool.OpenWorld, true).annotate(Tool.Destructive, true) as T;
54
55const safeBrowserTool = <T extends Tool.Any>(tool: T): T =>
56 tool.annotate(Tool.OpenWorld, true).annotate(Tool.Destructive, false) as T;
57
58const readonlyBrowserTool = <T extends Tool.Any>(tool: T): T =>
59 safeBrowserTool(tool).annotate(Tool.Readonly, true).annotate(Tool.Idempotent, true) as T;
60
61const PreviewStatusTool = Tool.make("preview_status", {
62 description:
63 "Report whether a collaborative browser tab is automation-capable, including its control owner, pending dialog, URL, title, visibility, loading state, viewport mode, and measured CSS-pixel size. Pass tabId to inspect a specific tab; omit it to use this agent session's current tab, or the tab the user is viewing when you have none. Server hosts also list every tab in the thread (tabs) with its owner, including tabs the user opened, and the browser profiles preview_open accepts. You can read any listed tab; act only on your own tabs, or on an unclaimed tab while no human controls it.",
64 parameters: PreviewAutomationTabTargetInput,
65 success: PreviewAutomationStatus,
66 failure: PreviewToolFailure,
67 dependencies,
68})
69 .annotate(Tool.Title, "Get preview status")
70 .annotate(Tool.Readonly, true)
71 .annotate(Tool.Destructive, false)
72 .annotate(Tool.Idempotent, true);
73
74const PreviewOpenTool = browserTool(
75 Tool.make("preview_open", {
76 description:
77 "Initialize a collaborative browser tab and open its thread-bound inline preview by default. Set open=false for background-only automation. Pass tabId to reuse a specific existing tab, set reuseExistingTab=false to create another tab, or omit both to use this agent session's current tab. Parallel subagents sharing a provider session must each open with reuseExistingTab=false and pass their returned tabId on every call. Pass profileId (an id or name from preview_status profiles) to open under a browser profile and its saved logins; omit it for the user's default profile. Another agent session's tabs can be read but not operated.",
78 parameters: PreviewAutomationOpenInput,
79 success: PreviewAutomationStatus,
80 failure: PreviewToolFailure,
81 dependencies,
82 })
83 .annotate(Tool.Title, "Open browser preview")
84 .annotate(Tool.Destructive, false),
85);
86
87const PreviewDialogTool = browserTool(
88 Tool.make("preview_dialog", {
89 description:
90 "Accept or dismiss the server browser dialog reported by preview_status. For a prompt, supply promptText when accepting. Requires this agent to own the tab, or the tab to be unclaimed with no human in control. Desktop hosts may not support this operation.",
91 parameters: PreviewAutomationDialogInput,
92 success: PreviewAutomationStatus,
93 failure: PreviewToolFailure,
94 dependencies,
95 }).annotate(Tool.Title, "Resolve browser dialog"),
96);
97
98const PreviewNavigateTool = safeBrowserTool(
99 Tool.make("preview_navigate", {
100 description:
101 "Navigate a collaborative browser tab. Pass tabId to target a specific tab, plus {url:'https://t3.chat'} for a website or {target:{kind:'environment-port',port:5173}} for a dev server. Exactly one of url or target is required.",
102 parameters: PreviewAutomationNavigateInput,
103 success: PreviewAutomationStatus,
104 failure: PreviewToolFailure,
105 dependencies,
106 }).annotate(Tool.Title, "Navigate browser preview"),
107);
108
109const PreviewResizeTool = safeBrowserTool(
110 Tool.make("preview_resize", {
111 description:
112 "Resize a collaborative browser tab, optionally selected by tabId. Use {mode:'fill'}, {mode:'freeform',width:1024,height:768}, or {mode:'preset',preset:'iphone-12-pro',orientation:'portrait'}. This changes CSS layout breakpoints without changing the desktop browser user agent.",
113 parameters: PreviewAutomationResizeInput,
114 success: Schema.Struct({ ...PreviewAutomationResizeResult.fields, ...presentationFields }),
115 failure: PreviewToolFailure,
116 dependencies,
117 })
118 .annotate(Tool.Title, "Resize browser viewport")
119 .annotate(Tool.Idempotent, true),
120);
121
122const PreviewSetAppearanceTool = safeBrowserTool(
123 Tool.make("preview_set_appearance", {
124 description:
125 "Emulate prefers-color-scheme in a collaborative browser tab, optionally selected by tabId. Use {colorScheme:'dark'} or {colorScheme:'light'} to preview the page in that appearance, and {colorScheme:'system'} to clear the override and follow the OS appearance.",
126 parameters: PreviewAutomationSetColorSchemeInput,
127 success: Schema.Struct({
128 ...PreviewAutomationSetColorSchemeResult.fields,
129 ...presentationFields,
130 }),
131 failure: PreviewToolFailure,
132 dependencies,
133 })
134 .annotate(Tool.Title, "Set preview appearance")
135 .annotate(Tool.Idempotent, true),
136);
137
138// Not read-only: save=true writes the screenshot to disk.
139export const PreviewSnapshotTool = safeBrowserTool(
140 Tool.make("preview_snapshot", {
141 description:
142 "Inspect a page before interacting. Pass tabId to inspect a specific tab, including one the user opened (see preview_status tabs), even while they control it; omit it to use this agent session's current tab, or the tab the user is viewing when you have none. Returns page state, semantic elements, diagnostics, action history, and screenshot dimensions. Server snapshots include an accessibilityTree with refs; pass locator=aria-ref=<ref> to target one exact element, including inside frames. Refresh refs after navigation, another snapshot, or human takeover. The text is capped near 20 KB and lists what it omitted; use preview_evaluate to read more. Set includeImage=true only when you need to see the page; the image stays in tool history. Set save=true to write the PNG to disk and get back only the url and screenshotPath. Embed that path in your reply as  so the user sees it. This is the only way to show the user a screenshot; the image in the tool result is not saved anywhere.",
143 parameters: Schema.Struct({
144 ...PreviewAutomationTabTargetInput.fields,
145 includeImage: Schema.optional(
146 Schema.Boolean.annotate({
147 description:
148 "Include the PNG image in the tool response. Defaults to false. Set true only when you need to see the page.",
149 }),
150 ),
151 save: Schema.optional(
152 Schema.Boolean.annotate({
153 description:
154 "Write the screenshot PNG to disk and return its absolute path as screenshotPath. Unless includeImage=true, return only the url and screenshotPath. Defaults to false.",
155 }),
156 ),
157 }),
158 success: PreviewAutomationSnapshot,
159 failure: PreviewToolFailure,
160 dependencies,
161 }).annotate(Tool.Title, "Inspect browser page"),
162);
163
164const PreviewClickTool = browserTool(
165 Tool.make("preview_click", {
166 description:
167 "Click exactly one target in the tab selected by tabId, or this agent session's current tab when omitted. Prefer a Playwright locator; selector accepts legacy CSS; x and y must be supplied together. Set button=right for a context menu or clickCount=2 for a double-click; server browser tabs only.",
168 parameters: PreviewAutomationClickInput,
169 success: PreviewActionResult,
170 failure: PreviewToolFailure,
171 dependencies,
172 }).annotate(Tool.Title, "Click preview page"),
173);
174
175const PreviewTypeTool = browserTool(
176 Tool.make("preview_type", {
177 description:
178 "Insert literal text into one input in the tab selected by tabId, or this agent session's current tab when omitted. Prefer a Playwright locator; set clear=true to replace existing text.",
179 parameters: PreviewAutomationTypeInput,
180 success: PreviewActionResult,
181 failure: PreviewToolFailure,
182 dependencies,
183 }).annotate(Tool.Title, "Type into preview page"),
184);
185
186const PreviewHoverTool = safeBrowserTool(
187 Tool.make("preview_hover", {
188 description:
189 "Move the mouse over exactly one target in the tab selected by tabId, or this agent session's current tab when omitted, to reveal hover menus and tooltips. Server browser tabs only.",
190 parameters: PreviewAutomationHoverInput,
191 success: PreviewActionResult,
192 failure: PreviewToolFailure,
193 dependencies,
194 }).annotate(Tool.Title, "Hover preview page"),
195);
196
197const PreviewSelectTool = browserTool(
198 Tool.make("preview_select", {
199 description:
200 "Choose options in one native <select> in the tab selected by tabId, or this agent session's current tab when omitted, by option value or visible label. Custom dropdowns are not <select>; click them open and click the option instead. Server browser tabs only.",
201 parameters: PreviewAutomationSelectInput,
202 success: Schema.Struct({
203 ...PreviewAutomationSelectResult.fields,
204 ...presentationFields,
205 }),
206 failure: PreviewToolFailure,
207 dependencies,
208 }).annotate(Tool.Title, "Select preview option"),
209);
210
211const PreviewDragTool = browserTool(
212 Tool.make("preview_drag", {
213 description:
214 "Drag one element onto another in the tab selected by tabId, or this agent session's current tab when omitted. Server browser tabs only.",
215 parameters: PreviewAutomationDragInput,
216 success: PreviewActionResult,
217 failure: PreviewToolFailure,
218 dependencies,
219 }).annotate(Tool.Title, "Drag in preview page"),
220);
221
222const PreviewUploadTool = browserTool(
223 Tool.make("preview_upload", {
224 description:
225 "Give files on the environment to the page in the tab selected by tabId, or this agent session's current tab when omitted. After clicking an upload control, preview_status reports the open fileChooser; call this with absolute paths to answer it, or with an empty list to cancel. Pass a locator for an <input type=file> to set its files without a picker. Server browser tabs only.",
226 parameters: PreviewAutomationUploadInput,
227 success: PreviewActionResult,
228 failure: PreviewToolFailure,
229 dependencies,
230 }).annotate(Tool.Title, "Upload files to preview page"),
231);
232
233const PreviewPressTool = browserTool(
234 Tool.make("preview_press", {
235 description:
236 "Press one keyboard key in the tab selected by tabId, or this agent session's current tab when omitted. Examples: {key:'Enter'}, {key:'Escape'}, or {key:'a',modifiers:['Meta']}.",
237 parameters: PreviewAutomationPressInput,
238 success: PreviewActionResult,
239 failure: PreviewToolFailure,
240 dependencies,
241 }).annotate(Tool.Title, "Press key in preview page"),
242);
243
244const PreviewScrollTool = safeBrowserTool(
245 Tool.make("preview_scroll", {
246 description:
247 "Scroll the tab selected by tabId, or this agent session's current tab when omitted. Positive deltaY scrolls down and positive deltaX scrolls right; a locator/selector targets a container.",
248 parameters: PreviewAutomationScrollInput,
249 success: PreviewActionResult,
250 failure: PreviewToolFailure,
251 dependencies,
252 }).annotate(Tool.Title, "Scroll preview page"),
253);
254
255/** Wrap non-objects: MCP requires object structuredContent, and Claude Code rejects invalid results entirely. */
256export const PreviewEvaluateResult = Schema.Struct({
257 ...presentationFields,
258 value: Schema.Unknown.annotate({
259 description: "The JSON-serializable value the expression produced, or null.",
260 }),
261}).annotate({ description: "The evaluated expression result." });
262
263const PreviewEvaluateTool = browserTool(
264 Tool.make("preview_evaluate", {
265 description:
266 "Evaluate JavaScript in the tab selected by tabId, or this agent session's current tab when omitted. Needs the same control as clicking: it works on your own tabs and on the user's tab only while nobody controls it. To read a tab the user is driving, use preview_snapshot. Returns {value} with a serializable result up to 64 KB; the expression may mutate page state.",
267 parameters: PreviewAutomationEvaluateInput,
268 success: PreviewEvaluateResult,
269 failure: PreviewToolFailure,
270 dependencies,
271 }).annotate(Tool.Title, "Evaluate JavaScript in preview"),
272);
273
274const PreviewWaitForTool = readonlyBrowserTool(
275 Tool.make("preview_wait_for", {
276 description:
277 "Wait in the tab selected by tabId, or this agent session's current tab when omitted, until all supplied locator, selector, text, and URL conditions match.",
278 parameters: PreviewAutomationWaitForInput,
279 success: PreviewActionResult,
280 failure: PreviewToolFailure,
281 dependencies,
282 }).annotate(Tool.Title, "Wait for preview page condition"),
283);
284
285const PreviewRecordingStartTool = safeBrowserTool(
286 Tool.make("preview_recording_start", {
287 description:
288 "Start recording the collaborative browser tab selected by tabId, or this agent session's current tab when omitted.",
289 parameters: PreviewAutomationTabTargetInput,
290 success: Schema.Struct({ ...PreviewAutomationRecordingStatus.fields, ...presentationFields }),
291 failure: PreviewToolFailure,
292 dependencies,
293 }).annotate(Tool.Title, "Start browser recording"),
294);
295
296const PreviewRecordingStopTool = safeBrowserTool(
297 Tool.make("preview_recording_stop", {
298 description:
299 "Stop recording the collaborative browser tab selected by tabId, or this agent session's current tab when omitted, and transfer the compressed recording once (up to 50 MiB) to an evidence file readable in this agent's environment. Returns its environment-local path after transfer succeeds.",
300 parameters: PreviewAutomationTabTargetInput,
301 success: Schema.Struct({ ...PreviewAutomationRecordingArtifact.fields, ...presentationFields }),
302 failure: PreviewToolFailure,
303 dependencies: [...dependencies, FileSystem.FileSystem, ServerConfig.ServerConfig],
304 }).annotate(Tool.Title, "Stop browser recording"),
305);
306
307export const PreviewToolkit = Toolkit.make(
308 PreviewDialogTool,
309 PreviewStatusTool,
310 PreviewOpenTool,
311 PreviewNavigateTool,
312 PreviewResizeTool,
313 PreviewSetAppearanceTool,
314 PreviewSnapshotTool,
315 PreviewClickTool,
316 PreviewTypeTool,
317 PreviewHoverTool,
318 PreviewSelectTool,
319 PreviewDragTool,
320 PreviewUploadTool,
321 PreviewPressTool,
322 PreviewScrollTool,
323 PreviewEvaluateTool,
324 PreviewWaitForTool,
325 PreviewRecordingStartTool,
326 PreviewRecordingStopTool,
327);
328
329export const PreviewStandardToolkit = Toolkit.make(
330 PreviewDialogTool,
331 PreviewStatusTool,
332 PreviewOpenTool,
333 PreviewNavigateTool,
334 PreviewResizeTool,
335 PreviewSetAppearanceTool,
336 PreviewClickTool,
337 PreviewTypeTool,
338 PreviewHoverTool,
339 PreviewSelectTool,
340 PreviewDragTool,
341 PreviewUploadTool,
342 PreviewPressTool,
343 PreviewScrollTool,
344 PreviewEvaluateTool,
345 PreviewWaitForTool,
346 PreviewRecordingStartTool,
347 PreviewRecordingStopTool,
348);
349
350export const PreviewSnapshotToolkit = Toolkit.make(PreviewSnapshotTool);