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

tools.ts

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