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

tools.ts

247 lines10.3 KB

1import {
2 OrchestratorMcpFailure,
3 ToolActivityIcon,
4 PreviewAutomationActInput,
5 PreviewAutomationDialogInput,
6 PreviewAutomationError,
7 PreviewAutomationEvaluateInput,
8 PreviewAutomationNavigateInput,
9 PreviewAutomationOpenInput,
10 PreviewAutomationRecordingArtifact,
11 PreviewAutomationRecordingStatus,
12 PreviewAutomationResizeInput,
13 PreviewAutomationResizeResult,
14 PreviewAutomationSelectResult,
15 PreviewAutomationSetColorSchemeInput,
16 PreviewAutomationSetColorSchemeResult,
17 PreviewAutomationSnapshot,
18 PreviewAutomationStatus,
19 PreviewAutomationTabTargetInput,
20 PreviewAutomationWaitForInput,
21} from "@t3tools/contracts";
22import * as Schema from "effect/Schema";
23import * as FileSystem from "effect/FileSystem";
24import { Tool, Toolkit } from "effect/ai";
25​
26import * as McpInvocationContext from "../../McpInvocationContext.ts";
27import * as PreviewAutomationBroker from "../../PreviewAutomationBroker.ts";
28import * as ServerConfig from "../../../config.ts";
29import * as ThreadManagementService from "../../../orchestration-v2/ThreadManagementService.ts";
30​
31const dependencies = [
32 McpInvocationContext.McpInvocationContext,
33 ThreadManagementService.ThreadManagementService,
34 PreviewAutomationBroker.PreviewAutomationBroker,
35];
36​
37const PreviewToolFailure = Schema.Union([PreviewAutomationError, OrchestratorMcpFailure]);
38​
39const presentationFields = { toolIcon: Schema.optional(ToolActivityIcon) };
40​
41const PreviewActionResult = Schema.Struct(presentationFields);
42​
43const browserTool = <T extends Tool.Any>(tool: T): T =>
44 tool.annotate(Tool.OpenWorld, true).annotate(Tool.Destructive, true) as T;
45​
46const safeBrowserTool = <T extends Tool.Any>(tool: T): T =>
47 tool.annotate(Tool.OpenWorld, true).annotate(Tool.Destructive, false) as T;
48​
49const readonlyBrowserTool = <T extends Tool.Any>(tool: T): T =>
50 safeBrowserTool(tool).annotate(Tool.Readonly, true).annotate(Tool.Idempotent, true) as T;
51​
52const PreviewStatusTool = Tool.make("preview_status", {
53 description:
54 "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.",
55 parameters: PreviewAutomationTabTargetInput,
56 success: PreviewAutomationStatus,
57 failure: PreviewToolFailure,
58 dependencies,
59})
60 .annotate(Tool.Title, "Get preview status")
61 .annotate(Tool.Readonly, true)
62 .annotate(Tool.Destructive, false)
63 .annotate(Tool.Idempotent, true);
64​
65const PreviewOpenTool = browserTool(
66 Tool.make("preview_open", {
67 description:
68 "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`.",
69 parameters: PreviewAutomationOpenInput,
70 success: PreviewAutomationStatus,
71 failure: PreviewToolFailure,
72 dependencies,
73 })
74 .annotate(Tool.Title, "Open browser preview")
75 .annotate(Tool.Destructive, false),
76);
77​
78const PreviewDialogTool = browserTool(
79 Tool.make("preview_dialog", {
80 description:
81 "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.",
82 parameters: PreviewAutomationDialogInput,
83 success: PreviewAutomationStatus,
84 failure: PreviewToolFailure,
85 dependencies,
86 }).annotate(Tool.Title, "Resolve browser dialog"),
87);
88​
89const PreviewNavigateTool = safeBrowserTool(
90 Tool.make("preview_navigate", {
91 description:
92 "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`.",
93 parameters: PreviewAutomationNavigateInput,
94 success: PreviewAutomationStatus,
95 failure: PreviewToolFailure,
96 dependencies,
97 }).annotate(Tool.Title, "Navigate browser preview"),
98);
99​
100const PreviewResizeTool = safeBrowserTool(
101 Tool.make("preview_resize", {
102 description:
103 "Resize the chosen tab: {mode:'fill'}, {mode:'freeform',width,height}, or {mode:'preset',preset,orientation}. Changes CSS size only, not the user agent.",
104 parameters: PreviewAutomationResizeInput,
105 success: Schema.Struct({ ...PreviewAutomationResizeResult.fields, ...presentationFields }),
106 failure: PreviewToolFailure,
107 dependencies,
108 })
109 .annotate(Tool.Title, "Resize browser viewport")
110 .annotate(Tool.Idempotent, true),
111);
112​
113const PreviewSetAppearanceTool = safeBrowserTool(
114 Tool.make("preview_set_appearance", {
115 description:
116 "Set `prefers-color-scheme` for the chosen tab: 'dark', 'light', or 'system' to clear the override.",
117 parameters: PreviewAutomationSetColorSchemeInput,
118 success: Schema.Struct({
119 ...PreviewAutomationSetColorSchemeResult.fields,
120 ...presentationFields,
121 }),
122 failure: PreviewToolFailure,
123 dependencies,
124 })
125 .annotate(Tool.Title, "Set preview appearance")
126 .annotate(Tool.Idempotent, true),
127);
128​
129// Not read-only: save=true writes the screenshot to disk.
130export const PreviewSnapshotTool = safeBrowserTool(
131 Tool.make("preview_snapshot", {
132 description:
133 "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 when asked: `save=true`, then embed ![alt](screenshotPath).",
134 parameters: Schema.Struct({
135 ...PreviewAutomationTabTargetInput.fields,
136 includeImage: Schema.optional(
137 Schema.Boolean.annotate({
138 description: "Default false. True only to see the page.",
139 }),
140 ),
141 save: Schema.optional(
142 Schema.Boolean.annotate({
143 description: "Write PNG, return `screenshotPath`.",
144 }),
145 ),
146 }),
147 success: PreviewAutomationSnapshot,
148 failure: PreviewToolFailure,
149 dependencies,
150 }).annotate(Tool.Title, "Inspect browser page"),
151);
152​
153const PreviewActTool = browserTool(
154 Tool.make("preview_act", {
155 description:
156 "Act on the chosen tab. Target with `locator='aria-ref=<ref>'` from `preview_snapshot`; `x` and `y` go together. `timeoutMs` defaults to 15000. click: `button`, `clickCount` 1 to 3. type: literal `text`, `clear=true` replaces it. hover: shows menus or tooltips. select: `values` in one native <select>, by value or label; for a custom dropdown, click it open, then click the option. drag: `source` onto `target`. upload: absolute `paths` answer the file picker `preview_status` reports, [] cancels it, or a `locator` sets an <input type=file>. press: one `key`, such as 'Enter', with `modifiers`, to the focused element; click a field first to focus it. scroll: the tab, or one container with a locator; positive `deltaY` scrolls down, positive `deltaX` right.",
157 parameters: PreviewAutomationActInput,
158 success: Schema.Union([
159 Schema.Struct({ ...PreviewAutomationSelectResult.fields, ...presentationFields }),
160 PreviewActionResult,
161 ]),
162 failure: PreviewToolFailure,
163 dependencies,
164 }).annotate(Tool.Title, "Act in preview page"),
165);
166​
167/** Wrap non-objects: MCP requires object structuredContent, and Claude Code rejects invalid results entirely. */
168export const PreviewEvaluateResult = Schema.Struct({
169 ...presentationFields,
170 value: Schema.Unknown.annotate({
171 description: "The JSON-serializable value the expression produced, or null.",
172 }),
173});
174​
175const PreviewEvaluateTool = browserTool(
176 Tool.make("preview_evaluate", {
177 description:
178 "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`.",
179 parameters: PreviewAutomationEvaluateInput,
180 success: PreviewEvaluateResult,
181 failure: PreviewToolFailure,
182 dependencies,
183 }).annotate(Tool.Title, "Evaluate JavaScript in preview"),
184);
185​
186const PreviewWaitForTool = readonlyBrowserTool(
187 Tool.make("preview_wait_for", {
188 description:
189 "Wait until all given locator, selector, text, and URL conditions match in the chosen tab.",
190 parameters: PreviewAutomationWaitForInput,
191 success: PreviewActionResult,
192 failure: PreviewToolFailure,
193 dependencies,
194 }).annotate(Tool.Title, "Wait for preview page condition"),
195);
196​
197const PreviewRecordingStartTool = safeBrowserTool(
198 Tool.make("preview_recording_start", {
199 description: "Start recording the chosen tab.",
200 parameters: PreviewAutomationTabTargetInput,
201 success: Schema.Struct({ ...PreviewAutomationRecordingStatus.fields, ...presentationFields }),
202 failure: PreviewToolFailure,
203 dependencies,
204 }).annotate(Tool.Title, "Start browser recording"),
205);
206​
207const PreviewRecordingStopTool = safeBrowserTool(
208 Tool.make("preview_recording_stop", {
209 description:
210 "Stop recording the chosen tab. Returns a local path to the recording file (max 50 MiB).",
211 parameters: PreviewAutomationTabTargetInput,
212 success: Schema.Struct({ ...PreviewAutomationRecordingArtifact.fields, ...presentationFields }),
213 failure: PreviewToolFailure,
214 dependencies: [...dependencies, FileSystem.FileSystem, ServerConfig.ServerConfig],
215 }).annotate(Tool.Title, "Stop browser recording"),
216);
217​
218export const PreviewToolkit = Toolkit.make(
219 PreviewDialogTool,
220 PreviewStatusTool,
221 PreviewOpenTool,
222 PreviewNavigateTool,
223 PreviewResizeTool,
224 PreviewSetAppearanceTool,
225 PreviewSnapshotTool,
226 PreviewActTool,
227 PreviewEvaluateTool,
228 PreviewWaitForTool,
229 PreviewRecordingStartTool,
230 PreviewRecordingStopTool,
231);
232​
233export const PreviewStandardToolkit = Toolkit.make(
234 PreviewDialogTool,
235 PreviewStatusTool,
236 PreviewOpenTool,
237 PreviewNavigateTool,
238 PreviewResizeTool,
239 PreviewSetAppearanceTool,
240 PreviewActTool,
241 PreviewEvaluateTool,
242 PreviewWaitForTool,
243 PreviewRecordingStartTool,
244 PreviewRecordingStopTool,
245);
246​
247export const PreviewSnapshotToolkit = Toolkit.make(PreviewSnapshotTool);