Open History to see the last commit
1import {
2 OrchestratorMcpEnvironmentTarget,
3 ScheduledTaskId,
4 ScheduledTask,
5 OrchestrationSearchThreadsInput,
6 OrchestrationThreadSearchMatch,
7 OrchestrationV2ThreadForkSourcePoint,
8 OrchestrationV2ContextTransfer,
9 TrimmedNonEmptyString,
10 ModelSelection,
11 RuntimeMode,
12 ProviderInteractionMode,
13 RuntimeRequestId,
14 ProviderUserInputAnswers,
15 IsoDateTime,
16 OrchestratorMcpFailure,
17 OrchestrationV2DispatchCommandResult,
18 ThreadId,
19 RunId,
20 NonNegativeInt,
21 ProjectId,
22} from "@t3tools/contracts";
23import * as Crypto from "effect/Crypto";
24import * as Schema from "effect/Schema";
25import { Tool, Toolkit } from "effect/ai";
26
27import * as ThreadSearch from "../../../orchestration-v2/ThreadSearch.ts";
28import * as ScheduledTaskService from "../../../scheduledTasks/ScheduledTaskService.ts";
29import * as ThreadManagementService from "../../../orchestration-v2/ThreadManagementService.ts";
30import * as McpInvocationContext from "../../McpInvocationContext.ts";
31
32const ThreadOrganizeTool = Tool.make("t3_thread_organize", {
33 description:
34 "Pin, snooze, settle, archive, or mark a thread unread. Omit `threadId` for this thread. `snooze` needs `snoozedUntil`. Settling this thread applies at turn end. A failed turn or queued message leaves it active.",
35 parameters: Schema.Struct({
36 environmentId: OrchestratorMcpEnvironmentTarget,
37 threadId: Schema.optional(ThreadId),
38 action: Schema.Literals([
39 "pin",
40 "unpin",
41 "snooze",
42 "unsnooze",
43 "settle",
44 "unsettle",
45 "archive",
46 "unarchive",
47 "mark_unread",
48 ]),
49 snoozedUntil: Schema.optional(IsoDateTime),
50 }),
51 success: Schema.Union([
52 OrchestrationV2DispatchCommandResult,
53 Schema.Struct({ settlesWhenTurnEnds: Schema.Literal(true) }),
54 ]),
55 failure: OrchestratorMcpFailure,
56 failureMode: "return" as const,
57 dependencies: [
58 McpInvocationContext.McpInvocationContext,
59 ThreadManagementService.ThreadManagementService,
60 Crypto.Crypto,
61 ],
62})
63 .annotate(Tool.Title, "Organize a thread")
64 .annotate(Tool.Strict, true)
65 .annotate(Tool.Destructive, true);
66
67const commandTool = {
68 success: OrchestrationV2DispatchCommandResult,
69 failure: OrchestratorMcpFailure,
70 failureMode: "return" as const,
71 dependencies: [
72 McpInvocationContext.McpInvocationContext,
73 ThreadManagementService.ThreadManagementService,
74 Crypto.Crypto,
75 ],
76};
77const queueEntry = Schema.Struct({
78 queuedRunId: RunId,
79 text: Schema.String,
80 truncated: Schema.Boolean,
81});
82const QUEUE_READ_ACTIONS = ["list", "read"] as const;
83const QueueTool = Tool.make("t3_queue", {
84 ...commandTool,
85 description:
86 "Manage a thread's queued messages. Omit `threadId` for this thread. list: send order, paged by `cursor` and `limit`. read: up to 16,000 chars of one. edit: replace its `text`; attachments stay. cancel. reorder: before `beforeRunId`, or to the end with null. promote_to_steer: send it now as steering to active `targetRunId`. Writes fail once the run is no longer queued.",
87 parameters: Schema.Struct({
88 action: Schema.Literals([
89 ...QUEUE_READ_ACTIONS,
90 "edit",
91 "cancel",
92 "reorder",
93 "promote_to_steer",
94 ]),
95 environmentId: OrchestratorMcpEnvironmentTarget,
96 threadId: Schema.optional(ThreadId),
97 queuedRunId: Schema.optional(RunId),
98 text: Schema.optional(Schema.String.check(Schema.isMaxLength(100000))),
99 beforeRunId: Schema.optional(Schema.NullOr(RunId)),
100 targetRunId: Schema.optional(RunId),
101 cursor: Schema.optional(NonNegativeInt),
102 limit: Schema.optional(Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 100 }))),
103 }),
104 success: Schema.Union([
105 Schema.Struct({ items: Schema.Array(queueEntry), nextCursor: Schema.NullOr(NonNegativeInt) }),
106 queueEntry,
107 OrchestrationV2DispatchCommandResult,
108 ]),
109}).annotate(Tool.Destructive, true);
110
111const question = Schema.Struct({
112 id: Schema.String,
113 header: Schema.String,
114 question: Schema.String,
115 options: Schema.Array(
116 Schema.Struct({
117 label: Schema.String,
118 description: Schema.String,
119 value: Schema.optional(Schema.String),
120 }),
121 ),
122 multiSelect: Schema.optional(Schema.Boolean),
123 allowCustomAnswer: Schema.optional(Schema.Boolean),
124 initialAnswer: Schema.optional(Schema.String),
125 required: Schema.optional(Schema.Boolean),
126});
127const PendingRequestTool = Tool.make("t3_pending_request", {
128 ...commandTool,
129 description:
130 "Pending user questions in a thread. Omit `threadId` for this thread. list: their ids; approvals are not listed. read: one by `requestId`. respond: answer one with `answers`; this cannot approve a permission request.",
131 parameters: Schema.Struct({
132 action: Schema.Literals(["list", "read", "respond"]),
133 environmentId: OrchestratorMcpEnvironmentTarget,
134 threadId: Schema.optional(ThreadId),
135 requestId: Schema.optional(RuntimeRequestId),
136 answers: Schema.optional(ProviderUserInputAnswers),
137 }),
138 success: Schema.Union([
139 Schema.Struct({ requestIds: Schema.Array(RuntimeRequestId) }),
140 Schema.Struct({ requestId: RuntimeRequestId, questions: Schema.Array(question) }),
141 OrchestrationV2DispatchCommandResult,
142 ]),
143})
144 .annotate(Tool.Destructive, true)
145 .annotate(Tool.OpenWorld, true);
146
147const threadConfiguration = Schema.Struct({
148 threadId: ThreadId,
149 modelSelection: ModelSelection,
150 runtimeMode: RuntimeMode,
151 interactionMode: ProviderInteractionMode,
152});
153const ThreadConfigureTool = Tool.make("t3_thread_configure", {
154 ...commandTool,
155 description:
156 "Read a thread's provider, model, and modes, or set its provider, model, and options with `modelSelection`. Omit `threadId` for this thread. Permission modes stay. Values from `orchestrator_capabilities`.",
157 parameters: Schema.Struct({
158 environmentId: OrchestratorMcpEnvironmentTarget,
159 threadId: Schema.optional(ThreadId),
160 modelSelection: Schema.optional(ModelSelection),
161 }),
162 success: Schema.Union([threadConfiguration, OrchestrationV2DispatchCommandResult]),
163}).annotate(Tool.Destructive, true);
164
165const ThreadTransferTool = Tool.make("t3_thread_transfer", {
166 ...commandTool,
167 description:
168 "Move context between threads. Omit `threadId` for this thread. fork: copy it from `sourcePoint` (default latest stable run) into a new thread with the same setup; no turn runs yet. merge_back: merge it from `sourcePoint` into a related `targetThreadId` in the same project. status: its context transfers.",
169 parameters: Schema.Struct({
170 action: Schema.Literals(["fork", "merge_back", "status"]),
171 environmentId: OrchestratorMcpEnvironmentTarget,
172 threadId: Schema.optional(ThreadId),
173 sourcePoint: Schema.optional(OrchestrationV2ThreadForkSourcePoint),
174 targetThreadId: Schema.optional(ThreadId),
175 title: Schema.optional(TrimmedNonEmptyString),
176 }),
177 success: Schema.Union([
178 Schema.Struct({ sequence: NonNegativeInt, targetThreadId: ThreadId }),
179 Schema.Struct({
180 transfers: Schema.Array(
181 Schema.Struct({
182 id: OrchestrationV2ContextTransfer.fields.id,
183 sourceThreadId: OrchestrationV2ContextTransfer.fields.sourceThreadId,
184 targetThreadId: OrchestrationV2ContextTransfer.fields.targetThreadId,
185 status: OrchestrationV2ContextTransfer.fields.status,
186 }),
187 ),
188 }),
189 ]),
190}).annotate(Tool.Destructive, true);
191
192const ThreadSearchTool = Tool.make("t3_thread_search", {
193 ...commandTool,
194 description:
195 "Search active threads by title, branch, PR, project, then message text. Skips subagent threads. `projectId`: one project, else yours, else all outside a thread. Not exhaustive.",
196 parameters: Schema.Struct({
197 environmentId: OrchestratorMcpEnvironmentTarget,
198 ...OrchestrationSearchThreadsInput.fields,
199 projectId: Schema.optional(ProjectId),
200 }),
201 success: Schema.Struct({
202 matches: Schema.Array(
203 Schema.Struct({
204 threadId: OrchestrationThreadSearchMatch.fields.threadId,
205 projectId: OrchestrationThreadSearchMatch.fields.projectId,
206 title: Schema.String,
207 link: Schema.optional(Schema.String),
208 source: Schema.Literals(["title", "user", "assistant"]),
209 snippet: Schema.optional(OrchestrationThreadSearchMatch.fields.snippet),
210 messageCreatedAt: Schema.optional(OrchestrationThreadSearchMatch.fields.messageCreatedAt),
211 }),
212 ),
213 }),
214 dependencies: [...commandTool.dependencies, ThreadSearch.ThreadSearch],
215})
216 .annotate(Tool.Readonly, true)
217 .annotate(Tool.Destructive, false);
218
219const ScheduledTaskRunTool = Tool.make("run_scheduled_task_now", {
220 ...commandTool,
221 description:
222 "Run one scheduled task now. Needs a full-access or default caller. Returns its thread, or null if not started. Success means started, not finished.",
223 parameters: Schema.Struct({
224 environmentId: OrchestratorMcpEnvironmentTarget,
225 taskId: ScheduledTaskId,
226 }),
227 success: Schema.Struct({
228 taskId: ScheduledTaskId,
229 threadId: ScheduledTask.fields.threadId,
230 link: Schema.optional(Schema.String),
231 lastRunStatus: ScheduledTask.fields.lastRunStatus,
232 runCount: NonNegativeInt,
233 nextRunAt: ScheduledTask.fields.nextRunAt,
234 }),
235 dependencies: [...commandTool.dependencies, ScheduledTaskService.ScheduledTaskService],
236})
237 .annotate(Tool.Destructive, true)
238 .annotate(Tool.OpenWorld, true);
239
240export const ThreadToolkit = Toolkit.make(
241 ScheduledTaskRunTool,
242 ThreadSearchTool,
243 ThreadConfigureTool,
244 ThreadTransferTool,
245 ThreadOrganizeTool,
246 PendingRequestTool,
247 QueueTool,
248);