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

tools.ts

153 lines7.3 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/unstable/ai";
26import * as ProjectService from "../../../project/ProjectService.ts";
27import * as ScratchWorkspace from "../../../project/ScratchWorkspace.ts";
28import * as ThreadManagementService from "../../../orchestration-v2/ThreadManagementService.ts";
29import * as SourceControlRepositoryService from "../../../sourceControl/SourceControlRepositoryService.ts";
30import * as McpInvocationContext from "../../McpInvocationContext.ts";
31​
32const shared = {
33 success: Project,
34 failure: OrchestratorMcpFailure,
35 failureMode: "return" as const,
36 dependencies: [
37 McpInvocationContext.McpInvocationContext,
38 ThreadManagementService.ThreadManagementService,
39 ProjectService.ProjectService,
40 Crypto.Crypto,
41 ],
42};
43const ProjectListTool = Tool.make("t3_project_list", {
44 ...shared,
45 description:
46 "List registered projects in this environment. Pages use the current project snapshot and may shift between calls.",
47 parameters: Schema.Struct({
48 cursor: Schema.optional(NonNegativeInt),
49 limit: Schema.optional(Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 100 }))),
50 }),
51 success: Schema.Struct({
52 projects: Schema.Array(Project),
53 nextCursor: Schema.NullOr(NonNegativeInt),
54 }),
55})
56 .annotate(Tool.Readonly, true)
57 .annotate(Tool.Destructive, false);
58const ProjectReadTool = Tool.make("t3_project_read", {
59 ...shared,
60 description:
61 "Read a registered project in this environment, including its workspace and saved scripts.",
62 parameters: Schema.Struct({ projectId: ProjectId }),
63})
64 .annotate(Tool.Readonly, true)
65 .annotate(Tool.Destructive, false);
66const ProjectCreateTool = Tool.make("t3_project_create", {
67 ...shared,
68 description:
69 "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.",
70 parameters: ProjectCreatePayload,
71}).annotate(Tool.Destructive, true);
72const ProjectUpdateTool = Tool.make("t3_project_update", {
73 ...shared,
74 description:
75 "Update a registered project's settings. Omitted fields are preserved. Uses the same project service as the app.",
76 parameters: Schema.Struct({ projectId: ProjectId, ...ProjectUpdatePayload.fields }),
77}).annotate(Tool.Destructive, true);
78const ProjectDeleteTool = Tool.make("t3_project_delete", {
79 ...shared,
80 description:
81 "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.",
82 parameters: Schema.Struct({ projectId: ProjectId, force: Schema.optionalKey(Schema.Boolean) }),
83}).annotate(Tool.Destructive, true);
84const ProjectCloneTool = Tool.make("t3_project_clone", {
85 ...shared,
86 description:
87 "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.",
88 parameters: SourceControlCloneRepositoryInput,
89 success: SourceControlCloneRepositoryResult,
90 dependencies: [
91 ...shared.dependencies,
92 SourceControlRepositoryService.SourceControlRepositoryService,
93 ],
94})
95 .annotate(Tool.Destructive, true)
96 .annotate(Tool.OpenWorld, true);
97const ThreadLaunchTool = Tool.make("t3_thread_launch", {
98 ...shared,
99 description:
100 '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. 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. Requires a full-access/default caller.',
101 parameters: Schema.Struct({
102 projectId: Schema.optional(ProjectId),
103 scratch: Schema.optional(
104 Schema.Boolean.annotate({
105 description:
106 "Launch without a project, in its own folder under the environment's Scratch project. Not with projectId or workspaceStrategy.",
107 }),
108 ),
109 title: TrimmedNonEmptyString,
110 modelSelection: Schema.optional(ModelSelection),
111 runtimeMode: Schema.optional(RuntimeMode),
112 interactionMode: Schema.optional(ProviderInteractionMode),
113 workspaceStrategy: Schema.optional(
114 OrchestrationV2ThreadLaunchWorkspaceStrategy.annotate({
115 description:
116 "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.",
117 }),
118 ),
119 message: Schema.optional(
120 Schema.String.check(Schema.isMaxLength(120000)).annotate({
121 description:
122 "First task prompt, delivered after workspace preparation. Omit message and attachments to create an idle thread.",
123 }),
124 ),
125 attachments: Schema.optional(Schema.Array(McpAttachmentInput).check(Schema.isMaxLength(8))),
126 }),
127 success: Schema.Struct({
128 threadId: ThreadId,
129 projectId: ProjectId,
130 modelSelection: ModelSelection,
131 runId: Schema.NullOr(RunId),
132 status: Schema.NullOr(OrchestrationV2RunStatus),
133 }),
134 dependencies: [
135 ...shared.dependencies,
136 ThreadLaunchService.ThreadLaunchService,
137 ScratchWorkspace.ScratchWorkspace,
138 FileSystem.FileSystem,
139 ServerConfig.ServerConfig,
140 ],
141})
142 .annotate(Tool.Destructive, true)
143 .annotate(Tool.OpenWorld, true);
144​
145export const ProjectToolkit = Toolkit.make(
146 ThreadLaunchTool,
147 ProjectListTool,
148 ProjectReadTool,
149 ProjectCreateTool,
150 ProjectUpdateTool,
151 ProjectDeleteTool,
152 ProjectCloneTool,
153);