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);
49
50const browserTool = <T extends Tool.Any>(tool: T): T =>
51 tool.annotate(Tool.OpenWorld, true).annotate(Tool.Destructive, true) as T;
52
53const safeBrowserTool = <T extends Tool.Any>(tool: T): T =>
54 tool.annotate(Tool.OpenWorld, true).annotate(Tool.Destructive, false) as T;
55
56const readonlyBrowserTool = <T extends Tool.Any>(tool: T): T =>
57 safeBrowserTool(tool).annotate(Tool.Readonly, true).annotate(Tool.Idempotent, true) as T;
58
59const PreviewStatusTool = Tool.make("preview_status", {
60 description:
61 "Report preview tab state: owner, pending dialog, URL, title, loading, viewport, and size. `tabs` lists this thread's tabs. `profiles` lists logins for `preview_open`. Read any listed tab. Act only on your tab, or an unclaimed tab with no human in control.",
62 parameters: PreviewAutomationTabTargetInput,
63 success: PreviewAutomationStatus,
64 failure: PreviewToolFailure,
65 dependencies,
66})
67 .annotate(Tool.Title, "Get preview status")
68 .annotate(Tool.Readonly, true)
69 .annotate(Tool.Destructive, false)
70 .annotate(Tool.Idempotent, true);
71
72const PreviewOpenTool = browserTool(
73 Tool.make("preview_open", {
74 description:
75 "Open a preview tab in this thread and show it. `open=false` keeps it in the background. Reuses your current tab unless `tabId` or `reuseExistingTab=false` says otherwise. Parallel subagents in one session must each use `reuseExistingTab=false`, then their own returned `tabId`.",
76 parameters: PreviewAutomationOpenInput,
77 success: PreviewAutomationStatus,
78 failure: PreviewToolFailure,
79 dependencies,
80 })
81 .annotate(Tool.Title, "Open browser preview")
82 .annotate(Tool.Destructive, false),
83);
84
85const PreviewDialogTool = browserTool(
86 Tool.make("preview_dialog", {
87 description:
88 "Accept or dismiss the dialog `preview_status` reports. Pass `promptText` to accept a prompt. Needs your own tab, or an unclaimed one. Desktop hosts may not support this.",
89 parameters: PreviewAutomationDialogInput,
90 success: PreviewAutomationStatus,
91 failure: PreviewToolFailure,
92 dependencies,
93 }).annotate(Tool.Title, "Resolve browser dialog"),
94);
95
96const PreviewNavigateTool = safeBrowserTool(
97 Tool.make("preview_navigate", {
98 description:
99 "Navigate the chosen tab. Pass `url` for a website, or `target:{kind:'environment-port',port}` for a dev server. Exactly one of `url` and `target`.",
100 parameters: PreviewAutomationNavigateInput,
101 success: PreviewAutomationStatus,
102 failure: PreviewToolFailure,
103 dependencies,
104 }).annotate(Tool.Title, "Navigate browser preview"),
105);
106
107const PreviewResizeTool = safeBrowserTool(
108 Tool.make("preview_resize", {
109 description:
110 "Resize the chosen tab: {mode:'fill'}, {mode:'freeform',width,height}, or {mode:'preset',preset,orientation}. Changes CSS size only, not the user agent.",
111 parameters: PreviewAutomationResizeInput,
112 success: Schema.Struct({ ...PreviewAutomationResizeResult.fields, ...presentationFields }),
113 failure: PreviewToolFailure,
114 dependencies,
115 })
116 .annotate(Tool.Title, "Resize browser viewport")
117 .annotate(Tool.Idempotent, true),
118);
119
120const PreviewSetAppearanceTool = safeBrowserTool(
121 Tool.make("preview_set_appearance", {
122 description:
123 "Set `prefers-color-scheme` for the chosen tab: 'dark', 'light', or 'system' to clear the override.",
124 parameters: PreviewAutomationSetColorSchemeInput,
125 success: Schema.Struct({
126 ...PreviewAutomationSetColorSchemeResult.fields,
127 ...presentationFields,
128 }),
129 failure: PreviewToolFailure,
130 dependencies,
131 })
132 .annotate(Tool.Title, "Set preview appearance")
133 .annotate(Tool.Idempotent, true),
134);
135
136// Not read-only: save=true writes the screenshot to disk.
137export const PreviewSnapshotTool = safeBrowserTool(
138 Tool.make("preview_snapshot", {
139 description:
140 "Use it only when the current request asks for browser work, including checking an edit in the browser. Never use it unasked to verify your own edit, or to repeat a snapshot from an earlier turn. Works on a tab the user is driving. Returns page state, elements, diagnostics, and screenshot size. Target one element with `locator='aria-ref=<ref>'` from `accessibilityTree`, even in frames. Refs die on navigation, a new snapshot, or handoff. Text caps near 20 KB; on your own or an unclaimed tab, `preview_evaluate` reads more. To show a screenshot: `save=true`, then embed .",
141 parameters: Schema.Struct({
142 ...PreviewAutomationTabTargetInput.fields,
143 includeImage: Schema.optional(
144 Schema.Boolean.annotate({
145 description: "Default false. True only to see the page.",
146 }),
147 ),
148 save: Schema.optional(
149 Schema.Boolean.annotate({
150 description: "Write PNG, return `screenshotPath`.",
151 }),
152 ),
153 }),
154 success: PreviewAutomationSnapshot,
155 failure: PreviewToolFailure,
156 dependencies,
157 }).annotate(Tool.Title, "Inspect browser page"),
158);
159
160const PreviewClickTool = browserTool(
161 Tool.make("preview_click", {
162 description: "Click exactly one target. Prefer a Playwright locator. `x` and `y` go together.",
163 parameters: PreviewAutomationClickInput,
164 success: PreviewActionResult,
165 failure: PreviewToolFailure,
166 dependencies,
167 }).annotate(Tool.Title, "Click preview page"),
168);
169
170const PreviewTypeTool = browserTool(
171 Tool.make("preview_type", {
172 description: "Type literal text into one input. `clear=true` replaces the current text.",
173 parameters: PreviewAutomationTypeInput,
174 success: PreviewActionResult,
175 failure: PreviewToolFailure,
176 dependencies,
177 }).annotate(Tool.Title, "Type into preview page"),
178);
179
180const PreviewHoverTool = safeBrowserTool(
181 Tool.make("preview_hover", {
182 description: "Hover one target to show menus or tooltips.",
183 parameters: PreviewAutomationHoverInput,
184 success: PreviewActionResult,
185 failure: PreviewToolFailure,
186 dependencies,
187 }).annotate(Tool.Title, "Hover preview page"),
188);
189
190const PreviewSelectTool = browserTool(
191 Tool.make("preview_select", {
192 description:
193 "Pick options in one native <select>, by value or label. For a custom dropdown, click it open, then click the option.",
194 parameters: PreviewAutomationSelectInput,
195 success: Schema.Struct({
196 ...PreviewAutomationSelectResult.fields,
197 ...presentationFields,
198 }),
199 failure: PreviewToolFailure,
200 dependencies,
201 }).annotate(Tool.Title, "Select preview option"),
202);
203
204const PreviewDragTool = browserTool(
205 Tool.make("preview_drag", {
206 description: "Drag one element onto another.",
207 parameters: PreviewAutomationDragInput,
208 success: PreviewActionResult,
209 failure: PreviewToolFailure,
210 dependencies,
211 }).annotate(Tool.Title, "Drag in preview page"),
212);
213
214const PreviewUploadTool = browserTool(
215 Tool.make("preview_upload", {
216 description:
217 "Give files from this environment to the page. After a click opens a file picker, `preview_status` reports it. Pass absolute paths to answer it, or an empty list to cancel. Or pass a locator for an <input type=file>.",
218 parameters: PreviewAutomationUploadInput,
219 success: PreviewActionResult,
220 failure: PreviewToolFailure,
221 dependencies,
222 }).annotate(Tool.Title, "Upload files to preview page"),
223);
224
225const PreviewPressTool = browserTool(
226 Tool.make("preview_press", {
227 description: "Press one key, such as {key:'Enter'} or {key:'a',modifiers:['Meta']}.",
228 parameters: PreviewAutomationPressInput,
229 success: PreviewActionResult,
230 failure: PreviewToolFailure,
231 dependencies,
232 }).annotate(Tool.Title, "Press key in preview page"),
233);
234
235const PreviewScrollTool = safeBrowserTool(
236 Tool.make("preview_scroll", {
237 description:
238 "Scroll the chosen tab, or one container with a locator or selector. Positive `deltaY` scrolls down. Positive `deltaX` scrolls right.",
239 parameters: PreviewAutomationScrollInput,
240 success: PreviewActionResult,
241 failure: PreviewToolFailure,
242 dependencies,
243 }).annotate(Tool.Title, "Scroll preview page"),
244);
245
246/** Wrap non-objects: MCP requires object structuredContent, and Claude Code rejects invalid results entirely. */
247export const PreviewEvaluateResult = Schema.Struct({
248 ...presentationFields,
249 value: Schema.Unknown.annotate({
250 description: "The JSON-serializable value the expression produced, or null.",
251 }),
252});
253
254const PreviewEvaluateTool = browserTool(
255 Tool.make("preview_evaluate", {
256 description:
257 "Run JavaScript in the page. It can change page state. Returns `{value}`, up to 64 KB. Needs your tab, or an unclaimed one. To read a tab the user drives, use `preview_snapshot`. Page tools reached here: read data only, never create, save, or display. Show results with `html_render`.",
258 parameters: PreviewAutomationEvaluateInput,
259 success: PreviewEvaluateResult,
260 failure: PreviewToolFailure,
261 dependencies,
262 }).annotate(Tool.Title, "Evaluate JavaScript in preview"),
263);
264
265const PreviewWaitForTool = readonlyBrowserTool(
266 Tool.make("preview_wait_for", {
267 description:
268 "Wait until all given locator, selector, text, and URL conditions match in the chosen tab.",
269 parameters: PreviewAutomationWaitForInput,
270 success: PreviewActionResult,
271 failure: PreviewToolFailure,
272 dependencies,
273 }).annotate(Tool.Title, "Wait for preview page condition"),
274);
275
276const PreviewRecordingStartTool = safeBrowserTool(
277 Tool.make("preview_recording_start", {
278 description: "Start recording the chosen tab.",
279 parameters: PreviewAutomationTabTargetInput,
280 success: Schema.Struct({ ...PreviewAutomationRecordingStatus.fields, ...presentationFields }),
281 failure: PreviewToolFailure,
282 dependencies,
283 }).annotate(Tool.Title, "Start browser recording"),
284);
285
286const PreviewRecordingStopTool = safeBrowserTool(
287 Tool.make("preview_recording_stop", {
288 description:
289 "Stop recording the chosen tab. Returns a local path to the recording file (max 50 MiB).",
290 parameters: PreviewAutomationTabTargetInput,
291 success: Schema.Struct({ ...PreviewAutomationRecordingArtifact.fields, ...presentationFields }),
292 failure: PreviewToolFailure,
293 dependencies: [...dependencies, FileSystem.FileSystem, ServerConfig.ServerConfig],
294 }).annotate(Tool.Title, "Stop browser recording"),
295);
296
297export const PreviewToolkit = Toolkit.make(
298 PreviewDialogTool,
299 PreviewStatusTool,
300 PreviewOpenTool,
301 PreviewNavigateTool,
302 PreviewResizeTool,
303 PreviewSetAppearanceTool,
304 PreviewSnapshotTool,
305 PreviewClickTool,
306 PreviewTypeTool,
307 PreviewHoverTool,
308 PreviewSelectTool,
309 PreviewDragTool,
310 PreviewUploadTool,
311 PreviewPressTool,
312 PreviewScrollTool,
313 PreviewEvaluateTool,
314 PreviewWaitForTool,
315 PreviewRecordingStartTool,
316 PreviewRecordingStopTool,
317);
318
319export const PreviewStandardToolkit = Toolkit.make(
320 PreviewDialogTool,
321 PreviewStatusTool,
322 PreviewOpenTool,
323 PreviewNavigateTool,
324 PreviewResizeTool,
325 PreviewSetAppearanceTool,
326 PreviewClickTool,
327 PreviewTypeTool,
328 PreviewHoverTool,
329 PreviewSelectTool,
330 PreviewDragTool,
331 PreviewUploadTool,
332 PreviewPressTool,
333 PreviewScrollTool,
334 PreviewEvaluateTool,
335 PreviewWaitForTool,
336 PreviewRecordingStartTool,
337 PreviewRecordingStopTool,
338);
339
340export const PreviewSnapshotToolkit = Toolkit.make(PreviewSnapshotTool);