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

tools.ts

251 lines10.1 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. Set includeImage=false for text-only output with the same page metadata.",
115 parameters: Schema.Struct({
116 ...PreviewAutomationTabTargetInput.fields,
117 includeImage: Schema.optional(
118 Schema.Boolean.annotate({
119 description:
120 "Include the PNG image in the tool response. Defaults to true. Set false for text-only output.",
121 }),
122 ),
123 }),
124 success: PreviewAutomationSnapshot,
125 failure: PreviewAutomationError,
126 dependencies,
127 }).annotate(Tool.Title, "Inspect browser page"),
128);
129​
130export const PreviewClickTool = browserTool(
131 Tool.make("preview_click", {
132 description:
133 "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.",
134 parameters: PreviewAutomationClickInput,
135 success: PreviewActionResult,
136 failure: PreviewAutomationError,
137 dependencies,
138 }).annotate(Tool.Title, "Click preview page"),
139);
140​
141export const PreviewTypeTool = browserTool(
142 Tool.make("preview_type", {
143 description:
144 "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.",
145 parameters: PreviewAutomationTypeInput,
146 success: PreviewActionResult,
147 failure: PreviewAutomationError,
148 dependencies,
149 }).annotate(Tool.Title, "Type into preview page"),
150);
151​
152export const PreviewPressTool = browserTool(
153 Tool.make("preview_press", {
154 description:
155 "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']}.",
156 parameters: PreviewAutomationPressInput,
157 success: PreviewActionResult,
158 failure: PreviewAutomationError,
159 dependencies,
160 }).annotate(Tool.Title, "Press key in preview page"),
161);
162​
163export const PreviewScrollTool = safeBrowserTool(
164 Tool.make("preview_scroll", {
165 description:
166 "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.",
167 parameters: PreviewAutomationScrollInput,
168 success: PreviewActionResult,
169 failure: PreviewAutomationError,
170 dependencies,
171 }).annotate(Tool.Title, "Scroll preview page"),
172);
173​
174export const PreviewEvaluateTool = browserTool(
175 Tool.make("preview_evaluate", {
176 description:
177 "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.",
178 parameters: PreviewAutomationEvaluateInput,
179 success: Schema.Unknown,
180 failure: PreviewAutomationError,
181 dependencies,
182 }).annotate(Tool.Title, "Evaluate JavaScript in preview"),
183);
184​
185export const PreviewWaitForTool = readonlyBrowserTool(
186 Tool.make("preview_wait_for", {
187 description:
188 "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.",
189 parameters: PreviewAutomationWaitForInput,
190 success: PreviewActionResult,
191 failure: PreviewAutomationError,
192 dependencies,
193 }).annotate(Tool.Title, "Wait for preview page condition"),
194);
195​
196export const PreviewRecordingStartTool = safeBrowserTool(
197 Tool.make("preview_recording_start", {
198 description:
199 "Start recording the collaborative browser tab selected by tabId, or this agent session's current tab when omitted.",
200 parameters: PreviewAutomationTabTargetInput,
201 success: PreviewAutomationRecordingStatus,
202 failure: PreviewAutomationError,
203 dependencies,
204 }).annotate(Tool.Title, "Start browser recording"),
205);
206​
207export const PreviewRecordingStopTool = safeBrowserTool(
208 Tool.make("preview_recording_stop", {
209 description:
210 "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.",
211 parameters: PreviewAutomationTabTargetInput,
212 success: PreviewAutomationRecordingArtifact,
213 failure: PreviewAutomationError,
214 dependencies,
215 }).annotate(Tool.Title, "Stop browser recording"),
216);
217​
218export const PreviewToolkit = Toolkit.make(
219 PreviewStatusTool,
220 PreviewOpenTool,
221 PreviewNavigateTool,
222 PreviewResizeTool,
223 PreviewSetAppearanceTool,
224 PreviewSnapshotTool,
225 PreviewClickTool,
226 PreviewTypeTool,
227 PreviewPressTool,
228 PreviewScrollTool,
229 PreviewEvaluateTool,
230 PreviewWaitForTool,
231 PreviewRecordingStartTool,
232 PreviewRecordingStopTool,
233);
234​
235export const PreviewStandardToolkit = Toolkit.make(
236 PreviewStatusTool,
237 PreviewOpenTool,
238 PreviewNavigateTool,
239 PreviewResizeTool,
240 PreviewSetAppearanceTool,
241 PreviewClickTool,
242 PreviewTypeTool,
243 PreviewPressTool,
244 PreviewScrollTool,
245 PreviewEvaluateTool,
246 PreviewWaitForTool,
247 PreviewRecordingStartTool,
248 PreviewRecordingStopTool,
249);
250​
251export const PreviewSnapshotToolkit = Toolkit.make(PreviewSnapshotTool);