Open History to see the last commit
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 { ServerConfig } from "../../../config.ts";
22import { ThreadLaunchService } from "../../../orchestration-v2/ThreadLaunchService.ts";
23import * as Crypto from "effect/Crypto";
24import * as Schema from "effect/Schema";
25import { Tool, Toolkit } from "effect/unstable/ai";
26import { ProjectService } from "../../../project/ProjectService.ts";
27import { ThreadManagementService } from "../../../orchestration-v2/ThreadManagementService.ts";
28import { SourceControlRepositoryService } from "../../../sourceControl/SourceControlRepositoryService.ts";
29import { McpInvocationContext } from "../../McpInvocationContext.ts";
30
31const shared = {
32 success: Project,
33 failure: OrchestratorMcpFailure,
34 failureMode: "return" as const,
35 dependencies: [McpInvocationContext, ThreadManagementService, ProjectService, Crypto.Crypto],
36};
37const ProjectListTool = Tool.make("t3_project_list", {
38 ...shared,
39 description:
40 "List registered projects in this environment. Pages use the current project snapshot and may shift between calls.",
41 parameters: Schema.Struct({
42 cursor: Schema.optional(NonNegativeInt),
43 limit: Schema.optional(Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 100 }))),
44 }),
45 success: Schema.Struct({
46 projects: Schema.Array(Project),
47 nextCursor: Schema.NullOr(NonNegativeInt),
48 }),
49})
50 .annotate(Tool.Readonly, true)
51 .annotate(Tool.Destructive, false);
52const ProjectReadTool = Tool.make("t3_project_read", {
53 ...shared,
54 description:
55 "Read a registered project in this environment, including its workspace and saved scripts.",
56 parameters: Schema.Struct({ projectId: ProjectId }),
57})
58 .annotate(Tool.Readonly, true)
59 .annotate(Tool.Destructive, false);
60const ProjectCreateTool = Tool.make("t3_project_create", {
61 ...shared,
62 description:
63 "Register a project directory through the existing project service. Set createWorkspaceRootIfMissing to create a directory. Each call creates a new request; an existing registered workspace is rejected. Clone separately with t3_project_clone when needed.",
64 parameters: ProjectCreatePayload,
65}).annotate(Tool.Destructive, true);
66const ProjectUpdateTool = Tool.make("t3_project_update", {
67 ...shared,
68 description:
69 "Update a registered project's settings. Omitted fields are preserved. Uses the same project service as the app.",
70 parameters: Schema.Struct({ projectId: ProjectId, ...ProjectUpdatePayload.fields }),
71}).annotate(Tool.Destructive, true);
72const ProjectDeleteTool = Tool.make("t3_project_delete", {
73 ...shared,
74 description:
75 "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.",
76 parameters: Schema.Struct({ projectId: ProjectId, force: Schema.optionalKey(Schema.Boolean) }),
77}).annotate(Tool.Destructive, true);
78const ProjectCloneTool = Tool.make("t3_project_clone", {
79 ...shared,
80 description:
81 "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.",
82 parameters: SourceControlCloneRepositoryInput,
83 success: SourceControlCloneRepositoryResult,
84 dependencies: [...shared.dependencies, SourceControlRepositoryService],
85})
86 .annotate(Tool.Destructive, true)
87 .annotate(Tool.OpenWorld, true);
88const ThreadLaunchTool = Tool.make("t3_thread_launch", {
89 ...shared,
90 description:
91 '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. 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. Requires a full-access/default caller.',
92 parameters: Schema.Struct({
93 projectId: Schema.optional(ProjectId),
94 title: TrimmedNonEmptyString,
95 modelSelection: Schema.optional(ModelSelection),
96 runtimeMode: Schema.optional(RuntimeMode),
97 interactionMode: Schema.optional(ProviderInteractionMode),
98 workspaceStrategy: Schema.optional(
99 OrchestrationV2ThreadLaunchWorkspaceStrategy.annotate({
100 description:
101 "Choose where this thread runs before starting its agent: worktree creates and binds a new checkout from baseRef; existing_worktree binds worktreePath; 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.",
102 }),
103 ),
104 message: Schema.optional(
105 Schema.String.check(Schema.isMaxLength(120000)).annotate({
106 description:
107 "First task prompt, delivered after workspace preparation. Omit message and attachments to create an idle thread.",
108 }),
109 ),
110 attachments: Schema.optional(Schema.Array(McpAttachmentInput).check(Schema.isMaxLength(8))),
111 }),
112 success: Schema.Struct({
113 threadId: ThreadId,
114 projectId: ProjectId,
115 modelSelection: ModelSelection,
116 runId: Schema.NullOr(RunId),
117 status: Schema.NullOr(OrchestrationV2RunStatus),
118 }),
119 dependencies: [...shared.dependencies, ThreadLaunchService, FileSystem.FileSystem, ServerConfig],
120})
121 .annotate(Tool.Destructive, true)
122 .annotate(Tool.OpenWorld, true);
123
124export const ProjectToolkit = Toolkit.make(
125 ThreadLaunchTool,
126 ProjectListTool,
127 ProjectReadTool,
128 ProjectCreateTool,
129 ProjectUpdateTool,
130 ProjectDeleteTool,
131 ProjectCloneTool,
132);