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

tools.ts

240 lines9.5 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 PreviewActionResult = Schema.Record(Schema.String, Schema.Never).annotate({
33 description: "The preview action completed successfully.",
34});
35​
36const browserTool = <T extends Tool.Any>(tool: T): T =>
37 tool.annotate(Tool.OpenWorld, true).annotate(Tool.Destructive, true) as T;
38​
39const safeBrowserTool = <T extends Tool.Any>(tool: T): T =>
40 browserTool(tool).annotate(Tool.Destructive, false) as T;
41​
42const readonlyBrowserTool = <T extends Tool.Any>(tool: T): T =>
43 safeBrowserTool(tool).annotate(Tool.Readonly, true).annotate(Tool.Idempotent, true) as T;
44​
45export const PreviewStatusTool = Tool.make("preview_status", {
46 description:
47 "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.",
48 parameters: PreviewAutomationTabTargetInput,
49 success: PreviewAutomationStatus,
50 failure: PreviewAutomationError,
51 dependencies,
52})
53 .annotate(Tool.Title, "Get preview status")
54 .annotate(Tool.Readonly, true)
55 .annotate(Tool.Destructive, false)
56 .annotate(Tool.Idempotent, true);
57​
58export const PreviewOpenTool = browserTool(
59 Tool.make("preview_open", {
60 description:
61 "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.",
62 parameters: PreviewAutomationOpenInput,
63 success: PreviewAutomationStatus,
64 failure: PreviewAutomationError,
65 dependencies,
66 })
67 .annotate(Tool.Title, "Open browser preview")
68 .annotate(Tool.Destructive, false),
69);
70​
71export const PreviewNavigateTool = safeBrowserTool(
72 Tool.make("preview_navigate", {
73 description:
74 "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.",
75 parameters: PreviewAutomationNavigateInput,
76 success: PreviewAutomationStatus,
77 failure: PreviewAutomationError,
78 dependencies,
79 }).annotate(Tool.Title, "Navigate browser preview"),
80);
81​
82export const PreviewResizeTool = safeBrowserTool(
83 Tool.make("preview_resize", {
84 description:
85 "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.",
86 parameters: PreviewAutomationResizeInput,
87 success: PreviewAutomationResizeResult,
88 failure: PreviewAutomationError,
89 dependencies,
90 })
91 .annotate(Tool.Title, "Resize browser viewport")
92 .annotate(Tool.Idempotent, true),
93);
94​
95export const PreviewSetAppearanceTool = safeBrowserTool(
96 Tool.make("preview_set_appearance", {
97 description:
98 "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.",
99 parameters: PreviewAutomationSetColorSchemeInput,
100 success: PreviewAutomationSetColorSchemeResult,
101 failure: PreviewAutomationError,
102 dependencies,
103 })
104 .annotate(Tool.Title, "Set preview appearance")
105 .annotate(Tool.Idempotent, true),
106);
107​
108export const PreviewSnapshotTool = readonlyBrowserTool(
109 Tool.make("preview_snapshot", {
110 description:
111 "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.",
112 parameters: PreviewAutomationTabTargetInput,
113 success: PreviewAutomationSnapshot,
114 failure: PreviewAutomationError,
115 dependencies,
116 }).annotate(Tool.Title, "Inspect browser page"),
117);
118​
119export const PreviewClickTool = browserTool(
120 Tool.make("preview_click", {
121 description:
122 "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.",
123 parameters: PreviewAutomationClickInput,
124 success: PreviewActionResult,
125 failure: PreviewAutomationError,
126 dependencies,
127 }).annotate(Tool.Title, "Click preview page"),
128);
129​
130export const PreviewTypeTool = browserTool(
131 Tool.make("preview_type", {
132 description:
133 "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.",
134 parameters: PreviewAutomationTypeInput,
135 success: PreviewActionResult,
136 failure: PreviewAutomationError,
137 dependencies,
138 }).annotate(Tool.Title, "Type into preview page"),
139);
140​
141export const PreviewPressTool = browserTool(
142 Tool.make("preview_press", {
143 description:
144 "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']}.",
145 parameters: PreviewAutomationPressInput,
146 success: PreviewActionResult,
147 failure: PreviewAutomationError,
148 dependencies,
149 }).annotate(Tool.Title, "Press key in preview page"),
150);
151​
152export const PreviewScrollTool = safeBrowserTool(
153 Tool.make("preview_scroll", {
154 description:
155 "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.",
156 parameters: PreviewAutomationScrollInput,
157 success: PreviewActionResult,
158 failure: PreviewAutomationError,
159 dependencies,
160 }).annotate(Tool.Title, "Scroll preview page"),
161);
162​
163export const PreviewEvaluateTool = browserTool(
164 Tool.make("preview_evaluate", {
165 description:
166 "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.",
167 parameters: PreviewAutomationEvaluateInput,
168 success: Schema.Unknown,
169 failure: PreviewAutomationError,
170 dependencies,
171 }).annotate(Tool.Title, "Evaluate JavaScript in preview"),
172);
173​
174export const PreviewWaitForTool = readonlyBrowserTool(
175 Tool.make("preview_wait_for", {
176 description:
177 "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.",
178 parameters: PreviewAutomationWaitForInput,
179 success: PreviewActionResult,
180 failure: PreviewAutomationError,
181 dependencies,
182 }).annotate(Tool.Title, "Wait for preview page condition"),
183);
184​
185export const PreviewRecordingStartTool = safeBrowserTool(
186 Tool.make("preview_recording_start", {
187 description:
188 "Start recording the collaborative browser tab selected by tabId, or this agent session's current tab when omitted.",
189 parameters: PreviewAutomationTabTargetInput,
190 success: PreviewAutomationRecordingStatus,
191 failure: PreviewAutomationError,
192 dependencies,
193 }).annotate(Tool.Title, "Start browser recording"),
194);
195​
196export const PreviewRecordingStopTool = safeBrowserTool(
197 Tool.make("preview_recording_stop", {
198 description:
199 "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.",
200 parameters: PreviewAutomationTabTargetInput,
201 success: PreviewAutomationRecordingArtifact,
202 failure: PreviewAutomationError,
203 dependencies,
204 }).annotate(Tool.Title, "Stop browser recording"),
205);
206​
207export const PreviewToolkit = Toolkit.make(
208 PreviewStatusTool,
209 PreviewOpenTool,
210 PreviewNavigateTool,
211 PreviewResizeTool,
212 PreviewSetAppearanceTool,
213 PreviewSnapshotTool,
214 PreviewClickTool,
215 PreviewTypeTool,
216 PreviewPressTool,
217 PreviewScrollTool,
218 PreviewEvaluateTool,
219 PreviewWaitForTool,
220 PreviewRecordingStartTool,
221 PreviewRecordingStopTool,
222);
223​
224export const PreviewStandardToolkit = Toolkit.make(
225 PreviewStatusTool,
226 PreviewOpenTool,
227 PreviewNavigateTool,
228 PreviewResizeTool,
229 PreviewSetAppearanceTool,
230 PreviewClickTool,
231 PreviewTypeTool,
232 PreviewPressTool,
233 PreviewScrollTool,
234 PreviewEvaluateTool,
235 PreviewWaitForTool,
236 PreviewRecordingStartTool,
237 PreviewRecordingStopTool,
238);
239​
240export const PreviewSnapshotToolkit = Toolkit.make(PreviewSnapshotTool);