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

tools.ts

110 lines4.0 KB

1import {
2 OrchestratorMcpFailure,
3 DeviceToolCloseInput,
4 DeviceToolError,
5 DeviceToolListResult,
6 DeviceToolOpenInput,
7 DeviceToolOpenResult,
8 DeviceToolScreenshotResult,
9 DeviceToolTargetInput,
10} from "@t3tools/contracts";
11import * as Schema from "effect/Schema";
12import * as FileSystem from "effect/FileSystem";
13import * as Path from "effect/Path";
14import * as ServerConfig from "../../../config.ts";
15import { Tool, Toolkit } from "effect/ai";
16​
17import * as McpInvocationContext from "../../McpInvocationContext.ts";
18import * as DeviceService from "../../../device/DeviceService.ts";
19import * as ThreadManagementService from "../../../orchestration-v2/ThreadManagementService.ts";
20import * as ProjectService from "../../../project/ProjectService.ts";
21​
22const dependencies = [
23 McpInvocationContext.McpInvocationContext,
24 ThreadManagementService.ThreadManagementService,
25 DeviceService.DeviceService,
26];
27​
28const DeviceToolFailure = Schema.Union([DeviceToolError, OrchestratorMcpFailure]);
29​
30const DeviceListTool = Tool.make("device_list", {
31 description:
32 "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.",
33 // An empty struct serializes as `anyOf [object, array]`, which some
34 // providers reject and then drop every tool on the server with it.
35 parameters: Schema.Struct({
36 hostId: Schema.optional(
37 Schema.String.annotate({ description: "Limit to one device host. Defaults to all hosts." }),
38 ),
39 }),
40 success: DeviceToolListResult,
41 failure: DeviceToolFailure,
42 dependencies,
43})
44 .annotate(Tool.Title, "List devices")
45 .annotate(Tool.Readonly, true)
46 .annotate(Tool.Destructive, false)
47 .annotate(Tool.Idempotent, true)
48 .annotate(Tool.OpenWorld, false);
49​
50const DeviceOpenTool = Tool.make("device_open", {
51 description:
52 "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.",
53 parameters: DeviceToolOpenInput,
54 success: DeviceToolOpenResult,
55 failure: DeviceToolFailure,
56 dependencies: [
57 ...dependencies,
58 FileSystem.FileSystem,
59 Path.Path,
60 ServerConfig.ServerConfig,
61 // The thread's project can name the EAS project a cloud device runs under.
62 ProjectService.ProjectService,
63 ],
64})
65 .annotate(Tool.Title, "Open device")
66 .annotate(Tool.Readonly, false)
67 .annotate(Tool.Destructive, false)
68 .annotate(Tool.Idempotent, true)
69 .annotate(Tool.OpenWorld, true);
70​
71export const DeviceScreenshotTool = Tool.make("device_screenshot", {
72 description:
73 "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.",
74 parameters: DeviceToolTargetInput,
75 success: DeviceToolScreenshotResult,
76 failure: DeviceToolFailure,
77 dependencies,
78})
79 .annotate(Tool.Title, "Screenshot device")
80 .annotate(Tool.Readonly, true)
81 .annotate(Tool.Destructive, false)
82 .annotate(Tool.Idempotent, true)
83 .annotate(Tool.OpenWorld, true);
84​
85const DeviceCloseTool = Tool.make("device_close", {
86 description:
87 "Remove a device from this thread's Device panel. Pass shutdown=true to also power the simulator or emulator off.",
88 parameters: DeviceToolCloseInput,
89 success: Schema.Record(Schema.String, Schema.Never).annotate({
90 description: "The device was closed.",
91 }),
92 failure: DeviceToolFailure,
93 dependencies,
94})
95 .annotate(Tool.Title, "Close device")
96 .annotate(Tool.Readonly, false)
97 .annotate(Tool.Destructive, true)
98 .annotate(Tool.Idempotent, true)
99 .annotate(Tool.OpenWorld, true);
100​
101export const DeviceStandardToolkit = Toolkit.make(DeviceListTool, DeviceOpenTool, DeviceCloseTool);
102​
103export const DeviceScreenshotToolkit = Toolkit.make(DeviceScreenshotTool);
104​
105export const DeviceToolkit = Toolkit.make(
106 DeviceListTool,
107 DeviceOpenTool,
108 DeviceScreenshotTool,
109 DeviceCloseTool,
110);