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

tools.ts

161 lines6.1 KB

1import {
2 McpCapabilityUnavailableError,
3 ModelSelection,
4 ProviderInteractionMode,
5 RuntimeMode,
6 ThreadId,
7 TrimmedNonEmptyString,
8} from "@t3tools/contracts";
9import * as Schema from "effect/Schema";
10import * as Tool from "effect/unstable/ai/Tool";
11import * as Toolkit from "effect/unstable/ai/Toolkit";
12​
13import * as McpInvocationContext from "../../McpInvocationContext.ts";
14import * as ProjectionSnapshotQuery from "../../../orchestration/Services/ProjectionSnapshotQuery.ts";
15import * as ThreadBootstrap from "../../../orchestration/Services/ThreadBootstrap.ts";
16​
17const dependencies = [
18 McpInvocationContext.McpInvocationContext,
19 ProjectionSnapshotQuery.ProjectionSnapshotQuery,
20 ThreadBootstrap.ThreadBootstrap,
21];
22​
23export const ThreadStartWorktreeInput = Schema.Struct({
24 baseBranch: Schema.optional(
25 TrimmedNonEmptyString.annotate({
26 description: "Branch to start the worktree from. Defaults to this thread's branch.",
27 }),
28 ),
29 branch: Schema.optional(
30 TrimmedNonEmptyString.annotate({
31 description: "Name for the new worktree branch. Defaults to a generated t3code/<hex> name.",
32 }),
33 ),
34 startFromOrigin: Schema.optional(
35 Schema.Boolean.annotate({
36 description: "Fetch origin and start from the remote baseBranch when it exists.",
37 }),
38 ),
39});
40export type ThreadStartWorktreeInput = typeof ThreadStartWorktreeInput.Type;
41​
42export const ThreadStartInput = Schema.Struct({
43 prompt: TrimmedNonEmptyString.check(Schema.isMaxLength(16_000)).annotate({
44 description:
45 "First user message of the new thread. Write it for a person and an agent who have none of this thread's context: goal, decisions already made, file paths, and what is left.",
46 }),
47 title: Schema.optional(
48 TrimmedNonEmptyString.check(Schema.isMaxLength(200)).annotate({
49 description: "Sidebar title for the new thread. Defaults to the first line of the prompt.",
50 }),
51 ),
52 interactionMode: Schema.optional(
53 ProviderInteractionMode.annotate({
54 description: "default or plan. Defaults to this thread's interaction mode.",
55 }),
56 ),
57 modelSelection: Schema.optional(
58 ModelSelection.annotate({
59 description:
60 "Provider instance and model for the new thread, as { instanceId, model, options? }. Defaults to this thread's selection; the result echoes the resolved selection so you can copy its shape.",
61 }),
62 ),
63 worktree: Schema.optional(
64 ThreadStartWorktreeInput.annotate({
65 description:
66 "Give the new thread its own git worktree instead of sharing this checkout. Pass {} for the defaults. The project's setup script runs in the new worktree.",
67 }),
68 ),
69 clientRequestId: Schema.optional(
70 TrimmedNonEmptyString.check(Schema.isMaxLength(128)).annotate({
71 description:
72 "Your own id for this request. Repeating a call with the same id returns the thread created the first time instead of creating a second one.",
73 }),
74 ),
75});
76export type ThreadStartInput = typeof ThreadStartInput.Type;
77​
78export const ThreadStartResult = Schema.Struct({
79 threadId: ThreadId,
80 title: Schema.String,
81 modelSelection: ModelSelection,
82 runtimeMode: RuntimeMode,
83 interactionMode: ProviderInteractionMode,
84 branch: Schema.NullOr(Schema.String),
85 worktreePath: Schema.NullOr(Schema.String),
86 alreadyStarted: Schema.Boolean.annotate({
87 description: "True when clientRequestId matched a thread this tool created before.",
88 }),
89});
90export type ThreadStartResult = typeof ThreadStartResult.Type;
91​
92export class ThreadStartCallerNotFoundError extends Schema.TaggedError<ThreadStartCallerNotFoundError>()(
93 "ThreadStartCallerNotFoundError",
94 { threadId: Schema.String },
95) {
96 override get message(): string {
97 return `Thread ${this.threadId} was not found.`;
98 }
100​
101export class ThreadStartCallerArchivedError extends Schema.TaggedError<ThreadStartCallerArchivedError>()(
102 "ThreadStartCallerArchivedError",
103 { threadId: Schema.String },
104) {
105 override get message(): string {
106 return `Thread ${this.threadId} is archived and cannot start new threads.`;
107 }
109​
110export class ThreadStartProjectNotFoundError extends Schema.TaggedError<ThreadStartProjectNotFoundError>()(
111 "ThreadStartProjectNotFoundError",
112 { projectId: Schema.String },
113) {
114 override get message(): string {
115 return `Project ${this.projectId} was not found.`;
116 }
118​
119export class ThreadStartWorktreeBaseRequiredError extends Schema.TaggedError<ThreadStartWorktreeBaseRequiredError>()(
120 "ThreadStartWorktreeBaseRequiredError",
121 {},
122) {
123 override get message(): string {
124 return "This thread has no branch. Pass worktree.baseBranch.";
125 }
127​
128export class ThreadStartFailedError extends Schema.TaggedError<ThreadStartFailedError>()(
129 "ThreadStartFailedError",
130 { cause: Schema.Defect() },
131) {
132 override get message(): string {
133 return "Could not start the new thread.";
134 }
136​
137export const ThreadStartToolError = Schema.Union([
138 McpCapabilityUnavailableError,
139 ThreadStartCallerNotFoundError,
140 ThreadStartCallerArchivedError,
141 ThreadStartProjectNotFoundError,
142 ThreadStartWorktreeBaseRequiredError,
143 ThreadStartFailedError,
144]);
145export type ThreadStartToolError = typeof ThreadStartToolError.Type;
146​
147const ThreadStartTool = Tool.make("t3_thread_start", {
148 description:
149 "Hand work off to a NEW top-level T3 Code thread that the user drives from the sidebar. Creates the thread in this thread's project and immediately starts its first turn with your prompt. The new thread inherits this thread's checkout, provider, model, permission mode, and interaction mode unless overridden; pass worktree to give it a fresh git worktree instead of sharing this checkout. This is not a subagent: it does not report back, and you do not wait for it. Use it when the user asks to continue, split off, or delegate work into a separate session.",
150 parameters: ThreadStartInput,
151 success: ThreadStartResult,
152 failure: ThreadStartToolError,
153 dependencies,
154})
155 .annotate(Tool.Title, "Start a T3 thread")
156 .annotate(Tool.Readonly, false)
157 .annotate(Tool.Destructive, false)
158 .annotate(Tool.Idempotent, false)
159 .annotate(Tool.OpenWorld, false);
160​
161export const ThreadsToolkit = Toolkit.make(ThreadStartTool);