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

tools.ts

101 lines3.9 KB

1import {
2 DeviceToolCloseInput,
3 DeviceToolError,
4 DeviceToolListResult,
5 DeviceToolOpenInput,
6 DeviceToolOpenResult,
7 DeviceToolScreenshotResult,
8 DeviceToolTargetInput,
9} from "@t3tools/contracts";
10import * as Schema from "effect/Schema";
11import * as FileSystem from "effect/FileSystem";
12import * as Path from "effect/Path";
13import { ServerConfig } from "../../../config.ts";
14import { Tool, Toolkit } from "effect/unstable/ai";
15​
16import * as McpInvocationContext from "../../McpInvocationContext.ts";
17import * as DeviceService from "../../../device/DeviceService.ts";
18​
19const dependencies = [McpInvocationContext.McpInvocationContext, DeviceService.DeviceService];
20​
21/**
22 * Deliberately a small surface: lifecycle, visibility for the user, and one
23 * image-returning verb. Driving the device (taps, typing, install, logs)
24 * happens through the preconfigured `agent-device` CLI, which has the
25 * semantic snapshot model agents need and stays current with its own
26 * releases. Wrapping its commands here would only lag behind it.
27 */
28const DeviceListTool = Tool.make("device_list", {
29 description:
30 "List iOS Simulators and Android Emulators on this environment's device hosts, which platforms each host can run, and which devices are already open in this thread's Device panel. Call this before device_open when you do not know a device id.",
31 // An empty struct serializes as `anyOf [object, array]`, which some
32 // providers reject and then drop every tool on the server with it.
33 parameters: Schema.Struct({
34 hostId: Schema.optional(
35 Schema.String.annotate({ description: "Limit to one device host. Defaults to all hosts." }),
36 ),
37 }),
38 success: DeviceToolListResult,
39 failure: DeviceToolError,
40 dependencies,
41})
42 .annotate(Tool.Title, "List devices")
43 .annotate(Tool.Readonly, true)
44 .annotate(Tool.Destructive, false)
45 .annotate(Tool.Idempotent, true)
46 .annotate(Tool.OpenWorld, false);
47​
48const DeviceOpenTool = Tool.make("device_open", {
49 description:
50 "Open a simulator or emulator for this thread: boots it if needed, starts its live stream, and shows it in the user's Device panel so they can watch. Returns the agent-device CLI invocation pinned to the device; drive the device with that CLI afterwards.",
51 parameters: DeviceToolOpenInput,
52 success: DeviceToolOpenResult,
53 failure: DeviceToolError,
54 dependencies: [...dependencies, FileSystem.FileSystem, Path.Path, ServerConfig],
55})
56 .annotate(Tool.Title, "Open device")
57 .annotate(Tool.Readonly, false)
58 .annotate(Tool.Destructive, false)
59 .annotate(Tool.Idempotent, true)
60 .annotate(Tool.OpenWorld, true);
61​
62export const DeviceScreenshotTool = Tool.make("device_screenshot", {
63 description:
64 "Capture the current screen of an open device as a PNG image. Use it to see what the user sees; for taps and text use the agent-device CLI.",
65 parameters: DeviceToolTargetInput,
66 success: DeviceToolScreenshotResult,
67 failure: DeviceToolError,
68 dependencies,
69})
70 .annotate(Tool.Title, "Screenshot device")
71 .annotate(Tool.Readonly, true)
72 .annotate(Tool.Destructive, false)
73 .annotate(Tool.Idempotent, true)
74 .annotate(Tool.OpenWorld, true);
75​
76const DeviceCloseTool = Tool.make("device_close", {
77 description:
78 "Remove a device from this thread's Device panel. Pass shutdown=true to also power the simulator or emulator off.",
79 parameters: DeviceToolCloseInput,
80 success: Schema.Record(Schema.String, Schema.Never).annotate({
81 description: "The device was closed.",
82 }),
83 failure: DeviceToolError,
84 dependencies,
85})
86 .annotate(Tool.Title, "Close device")
87 .annotate(Tool.Readonly, false)
88 .annotate(Tool.Destructive, true)
89 .annotate(Tool.Idempotent, true)
90 .annotate(Tool.OpenWorld, true);
91​
92export const DeviceStandardToolkit = Toolkit.make(DeviceListTool, DeviceOpenTool, DeviceCloseTool);
93​
94export const DeviceScreenshotToolkit = Toolkit.make(DeviceScreenshotTool);
95​
96export const DeviceToolkit = Toolkit.make(
97 DeviceListTool,
98 DeviceOpenTool,
99 DeviceScreenshotTool,
100 DeviceCloseTool,
101);