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

tools.ts

156 lines6.8 KB

1import {
2 BackgroundActivityProfile,
3 BackgroundActivityProfileSelection,
4 ExecutionEnvironmentDescriptor,
5 HostPressureLevel,
6 McpServerName,
7 OrchestratorMcpEnvironmentTarget,
8 OrchestratorMcpFailure,
9 ProjectId,
10 ServerSettings,
11 SecretRef,
12 ServerSettingsPatch,
13 TrimmedNonEmptyString,
14} from "@t3tools/contracts";
15import * as Schema from "effect/Schema";
16import { Tool, Toolkit } from "effect/ai";
17import * as ServerEnvironment from "../../../environment/ServerEnvironment.ts";
18import * as HostResources from "../../../resourceTelemetry/HostResources.ts";
19import * as ThreadCommandExecutor from "../../../orchestration-v2/ThreadCommandExecutor.ts";
20import * as ThreadManagementService from "../../../orchestration-v2/ThreadManagementService.ts";
21import * as Project from "../../../project/ProjectService.ts";
22import * as SecretRequests from "../../../secrets/SecretRequests.ts";
23import * as Settings from "../../../serverSettings.ts";
24import * as McpInvocationContext from "../../McpInvocationContext.ts";
25​
26const PreferenceFields = {
27 defaultThreadEnvMode: ServerSettings.fields.defaultThreadEnvMode,
28 newWorktreesStartFromOrigin: ServerSettings.fields.newWorktreesStartFromOrigin,
29 enableProviderUpdateChecks: ServerSettings.fields.enableProviderUpdateChecks,
30 backgroundActivity: Schema.Struct({ profile: BackgroundActivityProfileSelection }),
31 sourceControlWritingStyle: Schema.Struct({
32 mode: Schema.String,
33 followChangeRequestTemplates: Schema.Boolean,
34 customInstructions: Schema.String,
35 truncated: Schema.Boolean,
36 }),
37};
38const shared = {
39 failure: OrchestratorMcpFailure,
40 failureMode: "return" as const,
41 dependencies: [
42 McpInvocationContext.McpInvocationContext,
43 ThreadManagementService.ThreadManagementService,
44 ServerEnvironment.ServerEnvironment,
45 Settings.ServerSettingsService,
46 ThreadCommandExecutor.ThreadCommandExecutor,
47 HostResources.HostResources,
48 ],
49};
50const Machine = Schema.Struct({
51 pressure: Schema.Struct({
52 memory: HostPressureLevel,
53 cpu: HostPressureLevel,
54 io: HostPressureLevel,
55 disk: HostPressureLevel,
56 }).annotate({
57 description:
58 "Whether work here is stalling, by cause. `none` is fine even when busy; `elevated` slows agents; `severe` stalls or fails them.",
59 }),
60 cpuCount: Schema.Number,
61 cpuUsedPercent: Schema.NullOr(Schema.Number),
62 memoryAvailableGb: Schema.Number,
63 memoryTotalGb: Schema.Number,
64 diskFreeGb: Schema.NullOr(Schema.Number),
65});
66const EnvironmentReadTool = Tool.make("t3_environment_read", {
67 ...shared,
68 description:
69 "Read this server's identity, preferences and current machine load. To pick the least loaded machine, read each linked environment and compare `machine`. Providers and models: `orchestrator_capabilities`.",
70 parameters: Schema.Struct({ environmentId: OrchestratorMcpEnvironmentTarget }),
71 success: Schema.Struct({
72 environmentId: ExecutionEnvironmentDescriptor.fields.environmentId,
73 label: Schema.String,
74 serverVersion: Schema.String,
75 platform: ExecutionEnvironmentDescriptor.fields.platform,
76 preferences: Schema.Struct(PreferenceFields),
77 /** Absent from older servers. */
78 machine: Schema.optionalKey(Machine),
79 }),
80})
81 .annotate(Tool.Readonly, true)
82 .annotate(Tool.Destructive, false);
83const EnvironmentPreferencesTool = Tool.make("t3_environment_preferences_update", {
84 ...shared,
85 description:
86 "Update environment-wide preferences. Live full-access or default caller thread only. Omitted fields are unchanged; `customInstructions: ''` clears it.",
87 parameters: Schema.Struct({
88 environmentId: OrchestratorMcpEnvironmentTarget,
89 defaultThreadEnvMode: ServerSettingsPatch.fields.defaultThreadEnvMode,
90 newWorktreesStartFromOrigin: ServerSettingsPatch.fields.newWorktreesStartFromOrigin,
91 enableProviderUpdateChecks: ServerSettingsPatch.fields.enableProviderUpdateChecks,
92 backgroundActivity: Schema.optionalKey(Schema.Struct({ profile: BackgroundActivityProfile })),
93 sourceControlWritingStyle: ServerSettingsPatch.fields.sourceControlWritingStyle,
94 }),
95 success: Schema.Struct(PreferenceFields),
96}).annotate(Tool.Destructive, true);
97// A secret is never a plain value: settings.json holds plain values as written.
98const McpValueRead = Schema.Union([Schema.String, Schema.Struct({ stored: Schema.Boolean })]);
99const McpValueWrite = Schema.Union([
100 Schema.String,
101 Schema.Struct({ secretRef: SecretRef, prefix: Schema.optionalKey(Schema.String) }),
102 Schema.Struct({ stored: Schema.Literal(true) }),
103]);
104const mcpServerFields = <Value extends Schema.Top>(value: Value) =>
105 Schema.Struct({
106 enabled: Schema.optionalKey(Schema.Boolean),
107 command: Schema.optionalKey(TrimmedNonEmptyString),
108 args: Schema.optionalKey(Schema.Array(Schema.String)),
109 env: Schema.optionalKey(Schema.Record(TrimmedNonEmptyString, value)),
110 url: Schema.optionalKey(TrimmedNonEmptyString),
111 headers: Schema.optionalKey(Schema.Record(TrimmedNonEmptyString, value)),
112 oauth: Schema.optionalKey(
113 Schema.Struct({
114 clientId: TrimmedNonEmptyString,
115 clientSecret: Schema.optionalKey(value),
116 }),
117 ),
118 });
119const mcpShared = {
120 ...shared,
121 dependencies: [...shared.dependencies, SecretRequests.SecretRequests, Project.ProjectService],
122};
123const McpServersReadTool = Tool.make("t3_mcp_servers_read", {
124 ...mcpShared,
125 description:
126 "List the MCP servers this environment gives agents, or with `projectId` that project's own entries. A secret shows as `{ stored }`.",
127 parameters: Schema.Struct({
128 environmentId: OrchestratorMcpEnvironmentTarget,
129 projectId: Schema.optional(ProjectId),
130 }),
131 success: Schema.Struct({
132 servers: Schema.Record(McpServerName, mcpServerFields(McpValueRead)),
133 }),
134})
135 .annotate(Tool.Readonly, true)
136 .annotate(Tool.Destructive, false);
137const McpServerUpdateTool = Tool.make("t3_mcp_server_update", {
138 ...mcpShared,
139 description:
140 "Add, replace or remove (`server: null`) an MCP server this environment gives agents. Live full-access or default caller only. `command` (+ `args`, `env`) or `url` (+ `headers`, `oauth` for a client the user registered with a server that needs one, such as Google's). Values are saved as written, so a secret is `${ENV_VAR}`, `{ secretRef, prefix? }` from `request_secret`, or `{ stored: true }` to keep one. With `projectId`, that project only; there `{ enabled }` alone switches the environment's server.",
141 parameters: Schema.Struct({
142 environmentId: OrchestratorMcpEnvironmentTarget,
143 projectId: Schema.optional(ProjectId),
144 name: McpServerName,
145 server: Schema.NullOr(mcpServerFields(McpValueWrite)),
146 }),
147 success: Schema.Struct({
148 servers: Schema.Record(McpServerName, mcpServerFields(McpValueRead)),
149 }),
150}).annotate(Tool.Destructive, true);
151export const EnvironmentToolkit = Toolkit.make(
152 EnvironmentReadTool,
153 EnvironmentPreferencesTool,
154 McpServersReadTool,
155 McpServerUpdateTool,
156);