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

tools.ts

160 lines8.2 KB

1import { McpAttachmentInput } from "../attachment/input.ts";
2import {
3 NonNegativeInt,
4 ModelSelection,
5 TrimmedNonEmptyString,
6 ThreadId,
7 RunId,
8 OrchestrationV2RunStatus,
9 OrchestrationV2ThreadLaunchWorkspaceStrategy,
10 RuntimeMode,
11 ProviderInteractionMode,
12 Project,
13 ProjectCreatePayload,
14 ProjectUpdatePayload,
15 ProjectId,
16 OrchestratorMcpFailure,
17 SourceControlCloneRepositoryInput,
18 SourceControlCloneRepositoryResult,
19} from "@t3tools/contracts";
20import * as FileSystem from "effect/FileSystem";
21import * as ServerConfig from "../../../config.ts";
22import * as ThreadLaunchService from "../../../orchestration-v2/ThreadLaunchService.ts";
23import * as Crypto from "effect/Crypto";
24import * as Schema from "effect/Schema";
25import { Tool, Toolkit } from "effect/ai";
26import * as ProjectService from "../../../project/ProjectService.ts";
27import * as ManagedProjectFolders from "../../../project/ManagedProjectFolders.ts";
28import * as ThreadManagementService from "../../../orchestration-v2/ThreadManagementService.ts";
29import * as SourceControlRepositoryService from "../../../sourceControl/SourceControlRepositoryService.ts";
30import * as GitVcsDriver from "../../../vcs/GitVcsDriver.ts";
31import * as McpInvocationContext from "../../McpInvocationContext.ts";
32​
33const shared = {
34 success: Project,
35 failure: OrchestratorMcpFailure,
36 failureMode: "return" as const,
37 dependencies: [
38 McpInvocationContext.McpInvocationContext,
39 ThreadManagementService.ThreadManagementService,
40 ProjectService.ProjectService,
41 Crypto.Crypto,
42 ],
43};
44const ProjectListTool = Tool.make("t3_project_list", {
45 ...shared,
46 description:
47 "List registered projects in this environment. Pages use the current project snapshot and may shift between calls.",
48 parameters: Schema.Struct({
49 cursor: Schema.optional(NonNegativeInt),
50 limit: Schema.optional(Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 100 }))),
51 }),
52 success: Schema.Struct({
53 projects: Schema.Array(Project),
54 nextCursor: Schema.NullOr(NonNegativeInt),
55 }),
56})
57 .annotate(Tool.Readonly, true)
58 .annotate(Tool.Destructive, false);
59const ProjectReadTool = Tool.make("t3_project_read", {
60 ...shared,
61 description:
62 "Read a registered project in this environment, including its workspace and saved scripts.",
63 parameters: Schema.Struct({ projectId: ProjectId }),
64})
65 .annotate(Tool.Readonly, true)
66 .annotate(Tool.Destructive, false);
67const ProjectCreateTool = Tool.make("t3_project_create", {
68 ...shared,
69 description:
70 "Register a project directory through the existing project service. Set createWorkspaceRootIfMissing to create a directory. Omit workspaceRoot to start a new project from just its title: the app makes a Git repository for it in its own projects folder, with a README, an icon, and a first commit (commitError says why a commit failed; the project exists either way). Each call creates a new request; an existing registered workspace is rejected. Clone separately with t3_project_clone when needed.",
71 parameters: Schema.Struct({
72 ...ProjectCreatePayload.fields,
73 workspaceRoot: Schema.optional(ProjectCreatePayload.fields.workspaceRoot),
74 }),
75 success: Schema.Struct({ ...Project.fields, commitError: Schema.optional(Schema.String) }),
76 dependencies: [...shared.dependencies, ManagedProjectFolders.ManagedProjectFolders],
77}).annotate(Tool.Destructive, true);
78const ProjectUpdateTool = Tool.make("t3_project_update", {
79 ...shared,
80 description:
81 "Update a registered project's settings. Omitted fields are preserved. Uses the same project service as the app.",
82 parameters: Schema.Struct({ projectId: ProjectId, ...ProjectUpdatePayload.fields }),
83}).annotate(Tool.Destructive, true);
84const ProjectDeleteTool = Tool.make("t3_project_delete", {
85 ...shared,
86 description:
87 "Delete a project using the existing project deletion lifecycle. Nonempty projects require force=true. This does not delete the repository directory or promise a deleted-thread count.",
88 parameters: Schema.Struct({ projectId: ProjectId, force: Schema.optionalKey(Schema.Boolean) }),
89}).annotate(Tool.Destructive, true);
90const ProjectCloneTool = Tool.make("t3_project_clone", {
91 ...shared,
92 description:
93 "Clone a repository using the app's source-control service. This only clones; register the returned cwd with t3_project_create. An existing destination is not adopted or removed on failure.",
94 parameters: SourceControlCloneRepositoryInput,
95 success: SourceControlCloneRepositoryResult,
96 dependencies: [
97 ...shared.dependencies,
98 SourceControlRepositoryService.SourceControlRepositoryService,
99 ],
100})
101 .annotate(Tool.Destructive, true)
102 .annotate(Tool.OpenWorld, true);
103const ThreadLaunchTool = Tool.make("t3_thread_launch", {
104 ...shared,
105 description:
106 'Create an ordinary TOP-LEVEL thread with an explicit workspace binding before its agent starts. Use this when the user requests independent work, a new thread, or a PR stack in its own worktree; use delegate_task for child subagents. Set workspaceStrategy to {type:"worktree",baseRef:"parent-branch",branch:"new-branch",startFromOrigin:false} for a new worktree based on local commits, or {type:"existing_worktree",worktreePath:"/absolute/path",branch:"existing-branch"} to use an existing checkout. For upstream commits, set startFromOrigin:true. Omitted workspaceStrategy means the project root, NOT the caller\'s worktree. Omit projectId/modelSelection/modes to inherit those settings from the calling thread; a caller outside a T3 thread must pass projectId and gets the project\'s default model. Set scratch:true instead of projectId for a thread without a project: it runs in a fresh folder of its own, outside any repository. Put the task in message. Do not ask the agent to create its own worktree via shell: that does not update the thread binding. Each call creates a new launch with no retry key; retain threadId and use t3_thread_read/t3_thread_wait to follow preparation. After errors or lost responses, inspect t3_thread_list before retrying. Attachments must be pending uploads. The new thread may not run with broader runtime or interaction modes than the caller: the calling T3 thread\'s own modes, or the permission mode an outside agent was approved with.',
107 parameters: Schema.Struct({
108 projectId: Schema.optional(ProjectId),
109 scratch: Schema.optional(
110 Schema.Boolean.annotate({
111 description:
112 "Launch without a project, in its own folder under the environment's Scratch project. Not with projectId or workspaceStrategy.",
113 }),
114 ),
115 title: TrimmedNonEmptyString,
116 modelSelection: Schema.optional(ModelSelection),
117 runtimeMode: Schema.optional(RuntimeMode),
118 interactionMode: Schema.optional(ProviderInteractionMode),
119 workspaceStrategy: Schema.optional(
120 OrchestrationV2ThreadLaunchWorkspaceStrategy.annotate({
121 description:
122 "Choose where this thread runs before starting its agent: worktree creates and binds a new checkout from baseRef; existing_worktree binds worktreePath, which must be one of the project's git worktrees; root uses the project checkout. Omitted means root, not the caller's worktree. For a PR stack use the parent branch as baseRef and startFromOrigin:false. Uncommitted changes are not copied.",
123 }),
124 ),
125 message: Schema.optional(
126 Schema.String.check(Schema.isMaxLength(120000)).annotate({
127 description:
128 "First task prompt, delivered after workspace preparation. Omit message and attachments to create an idle thread.",
129 }),
130 ),
131 attachments: Schema.optional(Schema.Array(McpAttachmentInput).check(Schema.isMaxLength(8))),
132 }),
133 success: Schema.Struct({
134 threadId: ThreadId,
135 projectId: ProjectId,
136 modelSelection: ModelSelection,
137 runId: Schema.NullOr(RunId),
138 status: Schema.NullOr(OrchestrationV2RunStatus),
139 }),
140 dependencies: [
141 ...shared.dependencies,
142 ThreadLaunchService.ThreadLaunchService,
143 ManagedProjectFolders.ManagedProjectFolders,
144 GitVcsDriver.GitVcsDriver,
145 FileSystem.FileSystem,
146 ServerConfig.ServerConfig,
147 ],
148})
149 .annotate(Tool.Destructive, true)
150 .annotate(Tool.OpenWorld, true);
151​
152export const ProjectToolkit = Toolkit.make(
153 ThreadLaunchTool,
154 ProjectListTool,
155 ProjectReadTool,
156 ProjectCreateTool,
157 ProjectUpdateTool,
158 ProjectDeleteTool,
159 ProjectCloneTool,
160);