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

tools.ts

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