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

tools.ts

236 lines9.3 KB

1import {
2 PreviewAutomationClickInput,
3 PreviewAutomationError,
4 PreviewAutomationEvaluateInput,
5 PreviewAutomationNavigateInput,
6 PreviewAutomationOpenInput,
7 PreviewAutomationPressInput,
8 PreviewAutomationRecordingArtifact,
9 PreviewAutomationRecordingStatus,
10 PreviewAutomationResizeInput,
11 PreviewAutomationResizeResult,
12 PreviewAutomationScrollInput,
13 PreviewAutomationSetColorSchemeInput,
14 PreviewAutomationSetColorSchemeResult,
15 PreviewAutomationSnapshot,
16 PreviewAutomationStatus,
17 PreviewAutomationTabTargetInput,
18 PreviewAutomationTypeInput,
19 PreviewAutomationWaitForInput,
20} from "@t3tools/contracts";
21import * as Schema from "effect/Schema";
22import { Tool, Toolkit } from "effect/unstable/ai";
23​
24import * as McpInvocationContext from "../../McpInvocationContext.ts";
25import * as PreviewAutomationBroker from "../../PreviewAutomationBroker.ts";
26​
27const dependencies = [
28 McpInvocationContext.McpInvocationContext,
29 PreviewAutomationBroker.PreviewAutomationBroker,
30];
31​
32const browserTool = <T extends Tool.Any>(tool: T): T =>
33 tool.annotate(Tool.OpenWorld, true).annotate(Tool.Destructive, true) as T;
34​
35const safeBrowserTool = <T extends Tool.Any>(tool: T): T =>
36 browserTool(tool).annotate(Tool.Destructive, false) as T;
37​
38const readonlyBrowserTool = <T extends Tool.Any>(tool: T): T =>
39 safeBrowserTool(tool).annotate(Tool.Readonly, true).annotate(Tool.Idempotent, true) as T;
40​
41export const PreviewStatusTool = Tool.make("preview_status", {
42 description:
43 "Report whether a collaborative browser tab is automation-capable, including its 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.",
44 parameters: PreviewAutomationTabTargetInput,
45 success: PreviewAutomationStatus,
46 failure: PreviewAutomationError,
47 dependencies,
48})
49 .annotate(Tool.Title, "Get preview status")
50 .annotate(Tool.Readonly, true)
51 .annotate(Tool.Destructive, false)
52 .annotate(Tool.Idempotent, true);
53​
54export const PreviewOpenTool = browserTool(
55 Tool.make("preview_open", {
56 description:
57 "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.",
58 parameters: PreviewAutomationOpenInput,
59 success: PreviewAutomationStatus,
60 failure: PreviewAutomationError,
61 dependencies,
62 })
63 .annotate(Tool.Title, "Open browser preview")
64 .annotate(Tool.Destructive, false),
65);
66​
67export const PreviewNavigateTool = safeBrowserTool(
68 Tool.make("preview_navigate", {
69 description:
70 "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.",
71 parameters: PreviewAutomationNavigateInput,
72 success: PreviewAutomationStatus,
73 failure: PreviewAutomationError,
74 dependencies,
75 }).annotate(Tool.Title, "Navigate browser preview"),
76);
77​
78export const PreviewResizeTool = safeBrowserTool(
79 Tool.make("preview_resize", {
80 description:
81 "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.",
82 parameters: PreviewAutomationResizeInput,
83 success: PreviewAutomationResizeResult,
84 failure: PreviewAutomationError,
85 dependencies,
86 })
87 .annotate(Tool.Title, "Resize browser viewport")
88 .annotate(Tool.Idempotent, true),
89);
90​
91export const PreviewSetAppearanceTool = safeBrowserTool(
92 Tool.make("preview_set_appearance", {
93 description:
94 "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.",
95 parameters: PreviewAutomationSetColorSchemeInput,
96 success: PreviewAutomationSetColorSchemeResult,
97 failure: PreviewAutomationError,
98 dependencies,
99 })
100 .annotate(Tool.Title, "Set preview appearance")
101 .annotate(Tool.Idempotent, true),
102);
103​
104export const PreviewSnapshotTool = readonlyBrowserTool(
105 Tool.make("preview_snapshot", {
106 description:
107 "Inspect a page before interacting. Pass tabId to inspect a specific tab; omit it to use this agent session's current tab. Returns page state, semantic elements, diagnostics, action history, and a PNG screenshot.",
108 parameters: PreviewAutomationTabTargetInput,
109 success: PreviewAutomationSnapshot,
110 failure: PreviewAutomationError,
111 dependencies,
112 }).annotate(Tool.Title, "Inspect browser page"),
113);
114​
115export const PreviewClickTool = browserTool(
116 Tool.make("preview_click", {
117 description:
118 "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.",
119 parameters: PreviewAutomationClickInput,
120 success: Schema.Null,
121 failure: PreviewAutomationError,
122 dependencies,
123 }).annotate(Tool.Title, "Click preview page"),
124);
125​
126export const PreviewTypeTool = browserTool(
127 Tool.make("preview_type", {
128 description:
129 "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.",
130 parameters: PreviewAutomationTypeInput,
131 success: Schema.Null,
132 failure: PreviewAutomationError,
133 dependencies,
134 }).annotate(Tool.Title, "Type into preview page"),
135);
136​
137export const PreviewPressTool = browserTool(
138 Tool.make("preview_press", {
139 description:
140 "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']}.",
141 parameters: PreviewAutomationPressInput,
142 success: Schema.Null,
143 failure: PreviewAutomationError,
144 dependencies,
145 }).annotate(Tool.Title, "Press key in preview page"),
146);
147​
148export const PreviewScrollTool = safeBrowserTool(
149 Tool.make("preview_scroll", {
150 description:
151 "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.",
152 parameters: PreviewAutomationScrollInput,
153 success: Schema.Null,
154 failure: PreviewAutomationError,
155 dependencies,
156 }).annotate(Tool.Title, "Scroll preview page"),
157);
158​
159export const PreviewEvaluateTool = browserTool(
160 Tool.make("preview_evaluate", {
161 description:
162 "Evaluate JavaScript in the tab selected by tabId, or this agent session's current tab when omitted. Returns a serializable result up to 64 KB; the expression may mutate page state.",
163 parameters: PreviewAutomationEvaluateInput,
164 success: Schema.Unknown,
165 failure: PreviewAutomationError,
166 dependencies,
167 }).annotate(Tool.Title, "Evaluate JavaScript in preview"),
168);
169​
170export const PreviewWaitForTool = readonlyBrowserTool(
171 Tool.make("preview_wait_for", {
172 description:
173 "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.",
174 parameters: PreviewAutomationWaitForInput,
175 success: Schema.Null,
176 failure: PreviewAutomationError,
177 dependencies,
178 }).annotate(Tool.Title, "Wait for preview page condition"),
179);
180​
181export const PreviewRecordingStartTool = safeBrowserTool(
182 Tool.make("preview_recording_start", {
183 description:
184 "Start recording the collaborative browser tab selected by tabId, or this agent session's current tab when omitted.",
185 parameters: PreviewAutomationTabTargetInput,
186 success: PreviewAutomationRecordingStatus,
187 failure: PreviewAutomationError,
188 dependencies,
189 }).annotate(Tool.Title, "Start browser recording"),
190);
191​
192export const PreviewRecordingStopTool = safeBrowserTool(
193 Tool.make("preview_recording_stop", {
194 description:
195 "Stop recording the collaborative browser tab selected by tabId, or this agent session's current tab when omitted, and save it as a local evidence artifact.",
196 parameters: PreviewAutomationTabTargetInput,
197 success: PreviewAutomationRecordingArtifact,
198 failure: PreviewAutomationError,
199 dependencies,
200 }).annotate(Tool.Title, "Stop browser recording"),
201);
202​
203export const PreviewToolkit = Toolkit.make(
204 PreviewStatusTool,
205 PreviewOpenTool,
206 PreviewNavigateTool,
207 PreviewResizeTool,
208 PreviewSetAppearanceTool,
209 PreviewSnapshotTool,
210 PreviewClickTool,
211 PreviewTypeTool,
212 PreviewPressTool,
213 PreviewScrollTool,
214 PreviewEvaluateTool,
215 PreviewWaitForTool,
216 PreviewRecordingStartTool,
217 PreviewRecordingStopTool,
218);
219​
220export const PreviewStandardToolkit = Toolkit.make(
221 PreviewStatusTool,
222 PreviewOpenTool,
223 PreviewNavigateTool,
224 PreviewResizeTool,
225 PreviewSetAppearanceTool,
226 PreviewClickTool,
227 PreviewTypeTool,
228 PreviewPressTool,
229 PreviewScrollTool,
230 PreviewEvaluateTool,
231 PreviewWaitForTool,
232 PreviewRecordingStartTool,
233 PreviewRecordingStopTool,
234);
235​
236export const PreviewSnapshotToolkit = Toolkit.make(PreviewSnapshotTool);