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 SecureRandom from "effect/crypto/SecureRandom";
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 SecureRandom.SecureRandom,
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 SecureRandom.SecureRandom,
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 preview: Schema.optional(Schema.String),
121 }),
122 ),
123 multiSelect: Schema.optional(Schema.Boolean),
124 allowCustomAnswer: Schema.optional(Schema.Boolean),
125 initialAnswer: Schema.optional(Schema.String),
126 required: Schema.optional(Schema.Boolean),
127});
128const PendingRequestTool = Tool.make("t3_pending_request", {
129 ...commandTool,
130 description:
131 "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.",
132 parameters: Schema.Struct({
133 action: Schema.Literals(["list", "read", "respond"]),
134 environmentId: OrchestratorMcpEnvironmentTarget,
135 threadId: Schema.optional(ThreadId),
136 requestId: Schema.optional(RuntimeRequestId),
137 answers: Schema.optional(ProviderUserInputAnswers),
138 }),
139 success: Schema.Union([
140 Schema.Struct({ requestIds: Schema.Array(RuntimeRequestId) }),
141 Schema.Struct({ requestId: RuntimeRequestId, questions: Schema.Array(question) }),
142 OrchestrationV2DispatchCommandResult,
143 ]),
144})
145 .annotate(Tool.Destructive, true)
146 .annotate(Tool.OpenWorld, true);
147
148const threadConfiguration = Schema.Struct({
149 threadId: ThreadId,
150 modelSelection: ModelSelection,
151 runtimeMode: RuntimeMode,
152 interactionMode: ProviderInteractionMode,
153});
154const ThreadConfigureTool = Tool.make("t3_thread_configure", {
155 ...commandTool,
156 description:
157 "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`.",
158 parameters: Schema.Struct({
159 environmentId: OrchestratorMcpEnvironmentTarget,
160 threadId: Schema.optional(ThreadId),
161 modelSelection: Schema.optional(ModelSelection),
162 }),
163 success: Schema.Union([threadConfiguration, OrchestrationV2DispatchCommandResult]),
164}).annotate(Tool.Destructive, true);
165
166const ThreadTransferTool = Tool.make("t3_thread_transfer", {
167 ...commandTool,
168 description:
169 "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.",
170 parameters: Schema.Struct({
171 action: Schema.Literals(["fork", "merge_back", "status"]),
172 environmentId: OrchestratorMcpEnvironmentTarget,
173 threadId: Schema.optional(ThreadId),
174 sourcePoint: Schema.optional(OrchestrationV2ThreadForkSourcePoint),
175 targetThreadId: Schema.optional(ThreadId),
176 title: Schema.optional(TrimmedNonEmptyString),
177 }),
178 success: Schema.Union([
179 Schema.Struct({ sequence: NonNegativeInt, targetThreadId: ThreadId }),
180 Schema.Struct({
181 transfers: Schema.Array(
182 Schema.Struct({
183 id: OrchestrationV2ContextTransfer.fields.id,
184 sourceThreadId: OrchestrationV2ContextTransfer.fields.sourceThreadId,
185 targetThreadId: OrchestrationV2ContextTransfer.fields.targetThreadId,
186 status: OrchestrationV2ContextTransfer.fields.status,
187 }),
188 ),
189 }),
190 ]),
191}).annotate(Tool.Destructive, true);
192
193const ThreadSearchTool = Tool.make("t3_thread_search", {
194 ...commandTool,
195 description:
196 "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.",
197 parameters: Schema.Struct({
198 environmentId: OrchestratorMcpEnvironmentTarget,
199 ...OrchestrationSearchThreadsInput.fields,
200 projectId: Schema.optional(ProjectId),
201 }),
202 success: Schema.Struct({
203 currentThreadId: Schema.optional(Schema.NullOr(ThreadId)),
204 matches: Schema.Array(
205 Schema.Struct({
206 threadId: OrchestrationThreadSearchMatch.fields.threadId,
207 projectId: OrchestrationThreadSearchMatch.fields.projectId,
208 title: Schema.String,
209 link: Schema.optional(Schema.String),
210 source: Schema.Literals(["title", "user", "assistant"]),
211 snippet: Schema.optional(OrchestrationThreadSearchMatch.fields.snippet),
212 messageCreatedAt: Schema.optional(OrchestrationThreadSearchMatch.fields.messageCreatedAt),
213 }),
214 ),
215 }),
216 dependencies: [...commandTool.dependencies, ThreadSearch.ThreadSearch],
217})
218 .annotate(Tool.Readonly, true)
219 .annotate(Tool.Destructive, false);
220
221const ScheduledTaskRunTool = Tool.make("run_scheduled_task_now", {
222 ...commandTool,
223 description:
224 "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.",
225 parameters: Schema.Struct({
226 environmentId: OrchestratorMcpEnvironmentTarget,
227 taskId: ScheduledTaskId,
228 }),
229 success: Schema.Struct({
230 taskId: ScheduledTaskId,
231 threadId: ScheduledTask.fields.threadId,
232 link: Schema.optional(Schema.String),
233 lastRunStatus: ScheduledTask.fields.lastRunStatus,
234 runCount: NonNegativeInt,
235 nextRunAt: ScheduledTask.fields.nextRunAt,
236 }),
237 dependencies: [...commandTool.dependencies, ScheduledTaskService.ScheduledTaskService],
238})
239 .annotate(Tool.Destructive, true)
240 .annotate(Tool.OpenWorld, true);
241
242export const ThreadToolkit = Toolkit.make(
243 ScheduledTaskRunTool,
244 ThreadSearchTool,
245 ThreadConfigureTool,
246 ThreadTransferTool,
247 ThreadOrganizeTool,
248 PendingRequestTool,
249 QueueTool,
250);