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

tools.ts

229 lines9.5 KB

1import { McpAttachmentInput } from "../attachment/input.ts";
2import {
3 clampedPositiveInt,
4 NonNegativeInt,
5 ModelSelection,
6 TrimmedNonEmptyString,
7 ThreadId,
8 RunId,
9 OrchestrationV2DelegatedFrom,
10 OrchestrationV2WorkflowOrigin,
11 OrchestrationV2RunStatus,
12 OrchestrationV2ThreadLaunchWorkspaceStrategy,
13 RuntimeMode,
14 ProviderInteractionMode,
15 Project,
16 ProjectCreatePayload,
17 ProjectUpdatePayload,
18 ProjectId,
19 OrchestratorMcpClientRequestId,
20 OrchestratorMcpEnvironmentTarget,
21 OrchestratorMcpFailure,
22 SourceControlCloneRepositoryInput,
23 SourceControlCloneRepositoryResult,
24 OrchestratorMcpThreadImportInput,
25 OrchestratorMcpThreadImportResult,
26} from "@t3tools/contracts";
27import * as FileSystem from "effect/FileSystem";
28import * as DocumentText from "../../../provider/DocumentText.ts";
29import * as ServerConfig from "../../../config.ts";
30import * as ProviderRegistry from "../../../provider/ProviderRegistry.ts";
31import * as ServerSettings from "../../../serverSettings.ts";
32import * as ThreadImportService from "../../../orchestration-v2/ThreadImportService.ts";
33import * as HandoffImport from "../../../peer/handoff/HandoffImport.ts";
34import * as ThreadLaunchService from "../../../orchestration-v2/ThreadLaunchService.ts";
35import * as Digest from "effect/crypto/Digest";
36import * as SecureRandom from "effect/crypto/SecureRandom";
37import * as Schema from "effect/Schema";
38import { McpSchema, Tool, Toolkit } from "effect/ai";
39import * as ProjectService from "../../../project/ProjectService.ts";
40import * as ProjectCloneTracker from "../../../project/ProjectCloneTracker.ts";
41import * as ManagedProjectFolders from "../../../project/ManagedProjectFolders.ts";
42import * as ThreadManagementService from "../../../orchestration-v2/ThreadManagementService.ts";
43import * as SourceControlRepositoryService from "../../../sourceControl/SourceControlRepositoryService.ts";
44import * as GitVcsDriver from "../../../vcs/GitVcsDriver.ts";
45import * as McpInvocationContext from "../../McpInvocationContext.ts";
46​
47const shared = {
48 success: Project,
49 failure: OrchestratorMcpFailure,
50 failureMode: "return" as const,
51 dependencies: [
52 McpInvocationContext.McpInvocationContext,
53 ThreadManagementService.ThreadManagementService,
54 ProjectService.ProjectService,
55 ServerSettings.ServerSettingsService,
56 SecureRandom.SecureRandom,
57 ],
58};
59const ProjectListTool = Tool.make("t3_project_list", {
60 ...shared,
61 description: "List projects. Pages can shift.",
62 parameters: Schema.Struct({
63 environmentId: OrchestratorMcpEnvironmentTarget,
64 cursor: Schema.optional(NonNegativeInt),
65 limit: Schema.optional(clampedPositiveInt(100)),
66 }),
67 success: Schema.Struct({
68 projects: Schema.Array(Project),
69 nextCursor: Schema.NullOr(NonNegativeInt),
70 }),
71})
72 .annotate(Tool.Readonly, true)
73 .annotate(Tool.Destructive, false);
74const ProjectReadTool = Tool.make("t3_project_read", {
75 ...shared,
76 description: "Read one project: workspace and saved scripts.",
77 parameters: Schema.Struct({
78 environmentId: OrchestratorMcpEnvironmentTarget,
79 projectId: ProjectId,
80 }),
81})
82 .annotate(Tool.Readonly, true)
83 .annotate(Tool.Destructive, false);
84const ProjectCreateTool = Tool.make("t3_project_create", {
85 ...shared,
86 description:
87 "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.",
88 parameters: Schema.Struct({
89 environmentId: OrchestratorMcpEnvironmentTarget,
90 ...ProjectCreatePayload.fields,
91 workspaceRoot: Schema.optional(ProjectCreatePayload.fields.workspaceRoot),
92 }),
93 success: Schema.Struct({ ...Project.fields, commitError: Schema.optional(Schema.String) }),
94 dependencies: [...shared.dependencies, ManagedProjectFolders.ManagedProjectFolders],
95}).annotate(Tool.Destructive, true);
96const ProjectUpdateTool = Tool.make("t3_project_update", {
97 ...shared,
98 description: "Update one project's settings. Omitted fields are unchanged.",
99 parameters: Schema.Struct({
100 environmentId: OrchestratorMcpEnvironmentTarget,
101 projectId: ProjectId,
102 ...ProjectUpdatePayload.fields,
103 }),
104}).annotate(Tool.Destructive, true);
105const ProjectDeleteTool = Tool.make("t3_project_delete", {
106 ...shared,
107 description: "Delete one project. Nonempty needs `force: true`. Folder on disk stays.",
108 parameters: Schema.Struct({
109 environmentId: OrchestratorMcpEnvironmentTarget,
110 projectId: ProjectId,
111 force: Schema.optionalKey(Schema.Boolean),
112 }),
113 dependencies: [
114 ...shared.dependencies,
115 ProjectCloneTracker.ProjectCloneTracker,
116 ManagedProjectFolders.ManagedProjectFolders,
117 ],
118}).annotate(Tool.Destructive, true);
119const ProjectCloneTool = Tool.make("t3_project_clone", {
120 ...shared,
121 description:
122 "Clone a repository (`destinationPath` new or empty). Register after with `t3_project_create`, with the same `environmentId`.",
123 parameters: Schema.Struct({
124 environmentId: OrchestratorMcpEnvironmentTarget,
125 ...SourceControlCloneRepositoryInput.fields,
126 }),
127 success: SourceControlCloneRepositoryResult,
128 dependencies: [
129 ...shared.dependencies,
130 SourceControlRepositoryService.SourceControlRepositoryService,
131 ],
132})
133 .annotate(Tool.Destructive, true)
134 .annotate(Tool.OpenWorld, true);
135const ThreadLaunchTool = Tool.make("t3_thread_launch", {
136 ...shared,
137 description:
138 "Create a top-level thread in its workspace before starting. Task in `message`. Omit `projectId`, model and modes to inherit yours (outside a thread, pass `projectId` or `scratch`); modes cannot exceed yours. On error or lost reply, reuse `clientRequestId`. Follow it with `t3_thread_read` or `t3_thread_wait`. Mention it with its `link`.",
139 parameters: Schema.Struct({
140 environmentId: OrchestratorMcpEnvironmentTarget,
141 projectId: Schema.optional(ProjectId),
142 scratch: Schema.optional(
143 Schema.Boolean.annotate({
144 description:
145 "No project: a new Scratch folder. Not with `projectId` or `workspaceStrategy`.",
146 }),
147 ),
148 title: TrimmedNonEmptyString,
149 generateTitle: Schema.optional(Schema.Boolean),
150 modelSelection: Schema.optional(ModelSelection),
151 runtimeMode: Schema.optional(RuntimeMode),
152 interactionMode: Schema.optional(ProviderInteractionMode),
153 workspaceStrategy: Schema.optional(
154 OrchestrationV2ThreadLaunchWorkspaceStrategy.annotate({
155 description:
156 "'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.",
157 }),
158 ),
159 message: Schema.optional(
160 Schema.String.check(Schema.isMaxLength(120000)).annotate({
161 description: "First prompt. Omit it and `attachments` for idle.",
162 }),
163 ),
164 attachments: Schema.optional(Schema.Array(McpAttachmentInput).check(Schema.isMaxLength(8))),
165 outputSchema: Schema.optional(
166 Schema.Record(Schema.String, Schema.Json).annotate({
167 description: "As in `t3_thread_send`. Needs `message`.",
168 }),
169 ),
170 clientRequestId: Schema.optional(OrchestratorMcpClientRequestId),
171 /** Set by a linked environment launching its agent's delegated task here; ignored otherwise. */
172 delegatedFrom: Schema.optional(OrchestrationV2DelegatedFrom),
173 /** Set by a linked environment whose workflow run this thread is a step of; ignored otherwise. */
174 workflowRun: Schema.optional(OrchestrationV2WorkflowOrigin),
175 }),
176 success: Schema.Struct({
177 threadId: ThreadId,
178 link: Schema.optional(Schema.String),
179 projectId: ProjectId,
180 modelSelection: ModelSelection,
181 runId: Schema.NullOr(RunId),
182 status: Schema.NullOr(OrchestrationV2RunStatus),
183 }),
184 dependencies: [
185 ...shared.dependencies,
186 ThreadLaunchService.ThreadLaunchService,
187 ManagedProjectFolders.ManagedProjectFolders,
188 GitVcsDriver.GitVcsDriver,
189 FileSystem.FileSystem,
190 ServerConfig.ServerConfig,
191 ServerSettings.ServerSettingsService,
192 ProviderRegistry.ProviderRegistry,
193 DocumentText.DocumentText,
194 Digest.Digest,
195 ],
196})
197 .annotate(Tool.Destructive, true)
198 .annotate(Tool.OpenWorld, true);
199​
200const ThreadImportTool = Tool.make("t3_thread_import", {
201 ...shared,
202 description:
203 "Create a thread here that continues a conversation handed off from another T3 Code environment. `messages` become its history; `continuationPrompt` starts its first run. A retry with the same `source` returns the same thread.",
204 parameters: OrchestratorMcpThreadImportInput,
205 success: OrchestratorMcpThreadImportResult,
206 dependencies: [
207 ...shared.dependencies,
208 ThreadImportService.ThreadImportService,
209 ProviderRegistry.ProviderRegistry,
210 HandoffImport.HandoffImport,
211 GitVcsDriver.GitVcsDriver,
212 FileSystem.FileSystem,
213 ],
214})
215 .annotate(Tool.Destructive, true)
216 .annotate(Tool.Idempotent, true)
217 // A linked environment's handoff calls this; nothing else is offered it.
218 .annotate(McpSchema.EnabledWhen, McpInvocationContext.linkedCallersOnly);
219​
220export const ProjectToolkit = Toolkit.make(
221 ThreadLaunchTool,
222 ThreadImportTool,
223 ProjectListTool,
224 ProjectReadTool,
225 ProjectCreateTool,
226 ProjectUpdateTool,
227 ProjectDeleteTool,
228 ProjectCloneTool,
229);