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

tools.ts

108 lines3.9 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 every device host, what each host can run, and what is open in this thread's Device panel. Call 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. Pass `deviceId` and `hostId` from `device_list`. Boots it if needed and shows it in the user's Device panel. Returns the `agent-device` CLI call pinned to that device. Drive the device with that CLI.",
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 "Get a PNG of an open device's screen. 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. `shutdown=true` also turns it off.",
88 parameters: DeviceToolCloseInput,
89 success: Schema.Record(Schema.String, Schema.Never),
90 failure: DeviceToolFailure,
91 dependencies,
92})
93 .annotate(Tool.Title, "Close device")
94 .annotate(Tool.Readonly, false)
95 .annotate(Tool.Destructive, true)
96 .annotate(Tool.Idempotent, true)
97 .annotate(Tool.OpenWorld, true);
98​
99export const DeviceStandardToolkit = Toolkit.make(DeviceListTool, DeviceOpenTool, DeviceCloseTool);
100​
101export const DeviceScreenshotToolkit = Toolkit.make(DeviceScreenshotTool);
102​
103export const DeviceToolkit = Toolkit.make(
104 DeviceListTool,
105 DeviceOpenTool,
106 DeviceScreenshotTool,
107 DeviceCloseTool,
108);