The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { McpAttachmentInput } from "../attachment/input.ts"; |
| 2 | import { |
| 3 | clampedPositiveInt, |
| 4 | NonNegativeInt, |
| 5 | ModelSelection, |
| 6 | TrimmedNonEmptyString, |
| 7 | ThreadId, |
| 8 | RunId, |
| 9 | OrchestrationV2DelegatedFrom, |
| 10 | OrchestrationV2RunStatus, |
| 11 | OrchestrationV2ThreadLaunchWorkspaceStrategy, |
| 12 | RuntimeMode, |
| 13 | ProviderInteractionMode, |
| 14 | Project, |
| 15 | ProjectCreatePayload, |
| 16 | ProjectUpdatePayload, |
| 17 | ProjectId, |
| 18 | OrchestratorMcpClientRequestId, |
| 19 | OrchestratorMcpEnvironmentTarget, |
| 20 | OrchestratorMcpFailure, |
| 21 | SourceControlCloneRepositoryInput, |
| 22 | SourceControlCloneRepositoryResult, |
| 23 | } from "@t3tools/contracts"; |
| 24 | import * as FileSystem from "effect/FileSystem"; |
| 25 | import * as DocumentText from "../../../provider/DocumentText.ts"; |
| 26 | import * as ServerConfig from "../../../config.ts"; |
| 27 | import * as ProviderRegistry from "../../../provider/ProviderRegistry.ts"; |
| 28 | import * as ServerSettings from "../../../serverSettings.ts"; |
| 29 | import * as ThreadLaunchService from "../../../orchestration-v2/ThreadLaunchService.ts"; |
| 30 | import * as Digest from "effect/crypto/Digest"; |
| 31 | import * as SecureRandom from "effect/crypto/SecureRandom"; |
| 32 | import * as Schema from "effect/Schema"; |
| 33 | import { Tool, Toolkit } from "effect/ai"; |
| 34 | import * as ProjectService from "../../../project/ProjectService.ts"; |
| 35 | import * as ProjectCloneTracker from "../../../project/ProjectCloneTracker.ts"; |
| 36 | import * as ManagedProjectFolders from "../../../project/ManagedProjectFolders.ts"; |
| 37 | import * as ThreadManagementService from "../../../orchestration-v2/ThreadManagementService.ts"; |
| 38 | import * as SourceControlRepositoryService from "../../../sourceControl/SourceControlRepositoryService.ts"; |
| 39 | import * as GitVcsDriver from "../../../vcs/GitVcsDriver.ts"; |
| 40 | import * as McpInvocationContext from "../../McpInvocationContext.ts"; |
| 41 | |
| 42 | const shared = { |
| 43 | success: Project, |
| 44 | failure: OrchestratorMcpFailure, |
| 45 | failureMode: "return" as const, |
| 46 | dependencies: [ |
| 47 | McpInvocationContext.McpInvocationContext, |
| 48 | ThreadManagementService.ThreadManagementService, |
| 49 | ProjectService.ProjectService, |
| 50 | ServerSettings.ServerSettingsService, |
| 51 | SecureRandom.SecureRandom, |
| 52 | ], |
| 53 | }; |
| 54 | const ProjectListTool = Tool.make("t3_project_list", { |
| 55 | ...shared, |
| 56 | description: "List projects. Pages can shift.", |
| 57 | parameters: Schema.Struct({ |
| 58 | environmentId: OrchestratorMcpEnvironmentTarget, |
| 59 | cursor: Schema.optional(NonNegativeInt), |
| 60 | limit: Schema.optional(clampedPositiveInt(100)), |
| 61 | }), |
| 62 | success: Schema.Struct({ |
| 63 | projects: Schema.Array(Project), |
| 64 | nextCursor: Schema.NullOr(NonNegativeInt), |
| 65 | }), |
| 66 | }) |
| 67 | .annotate(Tool.Readonly, true) |
| 68 | .annotate(Tool.Destructive, false); |
| 69 | const ProjectReadTool = Tool.make("t3_project_read", { |
| 70 | ...shared, |
| 71 | description: "Read one project: workspace and saved scripts.", |
| 72 | parameters: Schema.Struct({ |
| 73 | environmentId: OrchestratorMcpEnvironmentTarget, |
| 74 | projectId: ProjectId, |
| 75 | }), |
| 76 | }) |
| 77 | .annotate(Tool.Readonly, true) |
| 78 | .annotate(Tool.Destructive, false); |
| 79 | const ProjectCreateTool = Tool.make("t3_project_create", { |
| 80 | ...shared, |
| 81 | description: |
| 82 | "Register a project folder. `createWorkspaceRootIfMissing` creates it; a registered folder is rejected. No `workspaceRoot`: makes a git repo from `title` with README, icon, first commit (`commitError` on failure; project still exists). Clone first with `t3_project_clone` if needed.", |
| 83 | parameters: Schema.Struct({ |
| 84 | environmentId: OrchestratorMcpEnvironmentTarget, |
| 85 | ...ProjectCreatePayload.fields, |
| 86 | workspaceRoot: Schema.optional(ProjectCreatePayload.fields.workspaceRoot), |
| 87 | }), |
| 88 | success: Schema.Struct({ ...Project.fields, commitError: Schema.optional(Schema.String) }), |
| 89 | dependencies: [...shared.dependencies, ManagedProjectFolders.ManagedProjectFolders], |
| 90 | }).annotate(Tool.Destructive, true); |
| 91 | const ProjectUpdateTool = Tool.make("t3_project_update", { |
| 92 | ...shared, |
| 93 | description: "Update one project's settings. Omitted fields are unchanged.", |
| 94 | parameters: Schema.Struct({ |
| 95 | environmentId: OrchestratorMcpEnvironmentTarget, |
| 96 | projectId: ProjectId, |
| 97 | ...ProjectUpdatePayload.fields, |
| 98 | }), |
| 99 | }).annotate(Tool.Destructive, true); |
| 100 | const ProjectDeleteTool = Tool.make("t3_project_delete", { |
| 101 | ...shared, |
| 102 | description: "Delete one project. Nonempty needs `force: true`. Folder on disk stays.", |
| 103 | parameters: Schema.Struct({ |
| 104 | environmentId: OrchestratorMcpEnvironmentTarget, |
| 105 | projectId: ProjectId, |
| 106 | force: Schema.optionalKey(Schema.Boolean), |
| 107 | }), |
| 108 | dependencies: [ |
| 109 | ...shared.dependencies, |
| 110 | ProjectCloneTracker.ProjectCloneTracker, |
| 111 | ManagedProjectFolders.ManagedProjectFolders, |
| 112 | ], |
| 113 | }).annotate(Tool.Destructive, true); |
| 114 | const ProjectCloneTool = Tool.make("t3_project_clone", { |
| 115 | ...shared, |
| 116 | description: |
| 117 | "Clone a repository (`destinationPath` new or empty). Register after with `t3_project_create`, with the same `environmentId`.", |
| 118 | parameters: Schema.Struct({ |
| 119 | environmentId: OrchestratorMcpEnvironmentTarget, |
| 120 | ...SourceControlCloneRepositoryInput.fields, |
| 121 | }), |
| 122 | success: SourceControlCloneRepositoryResult, |
| 123 | dependencies: [ |
| 124 | ...shared.dependencies, |
| 125 | SourceControlRepositoryService.SourceControlRepositoryService, |
| 126 | ], |
| 127 | }) |
| 128 | .annotate(Tool.Destructive, true) |
| 129 | .annotate(Tool.OpenWorld, true); |
| 130 | const ThreadLaunchTool = Tool.make("t3_thread_launch", { |
| 131 | ...shared, |
| 132 | description: |
| 133 | "Create one new top-level thread, bound to its workspace before the agent starts. Task in `message`. Omit `projectId`, model and modes to inherit yours (outside a thread, pass `projectId` or `scratch`); modes cannot exceed yours. After an error or lost reply, retry with the same `clientRequestId`. Follow it with `t3_thread_read` or `t3_thread_wait`. Mention it with its `link`.", |
| 134 | parameters: Schema.Struct({ |
| 135 | environmentId: OrchestratorMcpEnvironmentTarget, |
| 136 | projectId: Schema.optional(ProjectId), |
| 137 | scratch: Schema.optional( |
| 138 | Schema.Boolean.annotate({ |
| 139 | description: |
| 140 | "No project: a new Scratch folder. Not with `projectId` or `workspaceStrategy`.", |
| 141 | }), |
| 142 | ), |
| 143 | title: TrimmedNonEmptyString, |
| 144 | modelSelection: Schema.optional(ModelSelection), |
| 145 | runtimeMode: Schema.optional(RuntimeMode), |
| 146 | interactionMode: Schema.optional(ProviderInteractionMode), |
| 147 | workspaceStrategy: Schema.optional( |
| 148 | OrchestrationV2ThreadLaunchWorkspaceStrategy.annotate({ |
| 149 | description: |
| 150 | "'worktree': new checkout from `baseRef`. Honor a requested base. Otherwise, independent work uses the default branch as `remote/branch` (e.g. `origin/main`) with `startFromOrigin: true`; with no remote, use the local default branch. Required unpublished commits and dependent or stacked work use their local base with `startFromOrigin: false`. 'existing_worktree': bind `worktreePath`, a project worktree. 'root': project checkout, the default, not your worktree. Uncommitted edits are not copied.", |
| 151 | }), |
| 152 | ), |
| 153 | message: Schema.optional( |
| 154 | Schema.String.check(Schema.isMaxLength(120000)).annotate({ |
| 155 | description: "First prompt. Omit it and `attachments` for idle.", |
| 156 | }), |
| 157 | ), |
| 158 | attachments: Schema.optional(Schema.Array(McpAttachmentInput).check(Schema.isMaxLength(8))), |
| 159 | outputSchema: Schema.optional( |
| 160 | Schema.Record(Schema.String, Schema.Json).annotate({ |
| 161 | description: "As in `t3_thread_send`. Needs `message`.", |
| 162 | }), |
| 163 | ), |
| 164 | clientRequestId: Schema.optional(OrchestratorMcpClientRequestId), |
| 165 | /** Set by a linked environment launching its agent's delegated task here; ignored otherwise. */ |
| 166 | delegatedFrom: Schema.optional(OrchestrationV2DelegatedFrom), |
| 167 | }), |
| 168 | success: Schema.Struct({ |
| 169 | threadId: ThreadId, |
| 170 | link: Schema.optional(Schema.String), |
| 171 | projectId: ProjectId, |
| 172 | modelSelection: ModelSelection, |
| 173 | runId: Schema.NullOr(RunId), |
| 174 | status: Schema.NullOr(OrchestrationV2RunStatus), |
| 175 | }), |
| 176 | dependencies: [ |
| 177 | ...shared.dependencies, |
| 178 | ThreadLaunchService.ThreadLaunchService, |
| 179 | ManagedProjectFolders.ManagedProjectFolders, |
| 180 | GitVcsDriver.GitVcsDriver, |
| 181 | FileSystem.FileSystem, |
| 182 | ServerConfig.ServerConfig, |
| 183 | ServerSettings.ServerSettingsService, |
| 184 | ProviderRegistry.ProviderRegistry, |
| 185 | DocumentText.DocumentText, |
| 186 | Digest.Digest, |
| 187 | ], |
| 188 | }) |
| 189 | .annotate(Tool.Destructive, true) |
| 190 | .annotate(Tool.OpenWorld, true); |
| 191 | |
| 192 | export const ProjectToolkit = Toolkit.make( |
| 193 | ThreadLaunchTool, |
| 194 | ProjectListTool, |
| 195 | ProjectReadTool, |
| 196 | ProjectCreateTool, |
| 197 | ProjectUpdateTool, |
| 198 | ProjectDeleteTool, |
| 199 | ProjectCloneTool, |
| 200 | ); |