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

tools.ts

268 lines10.3 KB

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 clampedPositiveInt,
23} from "@t3tools/contracts";
24import * as SecureRandom from "effect/crypto/SecureRandom";
25import * as Schema from "effect/Schema";
26import { McpSchema, Tool, Toolkit } from "effect/ai";
27​
28import * as ThreadSearch from "../../../orchestration-v2/ThreadSearch.ts";
29import * as ProviderRegistry from "../../../provider/ProviderRegistry.ts";
30import * as ScheduledTaskService from "../../../scheduledTasks/ScheduledTaskService.ts";
31import * as ThreadManagementService from "../../../orchestration-v2/ThreadManagementService.ts";
32import * as McpInvocationContext from "../../McpInvocationContext.ts";
33​
34const ThreadOrganizeTool = Tool.make("t3_thread_organize", {
35 description:
36 "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.",
37 parameters: Schema.Struct({
38 environmentId: OrchestratorMcpEnvironmentTarget,
39 threadId: Schema.optional(ThreadId),
40 action: Schema.Literals([
41 "pin",
42 "unpin",
43 "snooze",
44 "unsnooze",
45 "settle",
46 "unsettle",
47 "archive",
48 "unarchive",
49 "mark_unread",
50 ]),
51 snoozedUntil: Schema.optional(IsoDateTime),
52 }),
53 success: Schema.Union([
54 OrchestrationV2DispatchCommandResult,
55 Schema.Struct({ settlesWhenTurnEnds: Schema.Literal(true) }),
56 ]),
57 failure: OrchestratorMcpFailure,
58 failureMode: "return" as const,
59 dependencies: [
60 McpInvocationContext.McpInvocationContext,
61 ThreadManagementService.ThreadManagementService,
62 SecureRandom.SecureRandom,
63 ],
64})
65 .annotate(Tool.Title, "Organize a thread")
66 .annotate(Tool.Strict, true)
67 .annotate(Tool.Destructive, true);
68​
69const commandTool = {
70 success: OrchestrationV2DispatchCommandResult,
71 failure: OrchestratorMcpFailure,
72 failureMode: "return" as const,
73 dependencies: [
74 McpInvocationContext.McpInvocationContext,
75 ThreadManagementService.ThreadManagementService,
76 SecureRandom.SecureRandom,
77 ],
78};
79const queueEntry = Schema.Struct({
80 queuedRunId: RunId,
81 text: Schema.String,
82 truncated: Schema.Boolean,
83});
84const QUEUE_READ_ACTIONS = ["list", "read"] as const;
85const QueueTool = Tool.make("t3_queue", {
86 ...commandTool,
87 description:
88 "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.",
89 parameters: Schema.Struct({
90 action: Schema.Literals([
91 ...QUEUE_READ_ACTIONS,
92 "edit",
93 "cancel",
94 "reorder",
95 "promote_to_steer",
96 ]),
97 environmentId: OrchestratorMcpEnvironmentTarget,
98 threadId: Schema.optional(ThreadId),
99 queuedRunId: Schema.optional(RunId),
100 text: Schema.optional(Schema.String.check(Schema.isMaxLength(100000))),
101 beforeRunId: Schema.optional(Schema.NullOr(RunId)),
102 targetRunId: Schema.optional(RunId),
103 cursor: Schema.optional(NonNegativeInt),
104 limit: Schema.optional(clampedPositiveInt(100)),
105 }),
106 success: Schema.Union([
107 Schema.Struct({ items: Schema.Array(queueEntry), nextCursor: Schema.NullOr(NonNegativeInt) }),
108 queueEntry,
109 OrchestrationV2DispatchCommandResult,
110 ]),
111}).annotate(Tool.Destructive, true);
112​
113const question = Schema.Struct({
114 id: Schema.String,
115 header: Schema.String,
116 question: Schema.String,
117 options: Schema.Array(
118 Schema.Struct({
119 label: Schema.String,
120 description: Schema.String,
121 value: Schema.optional(Schema.String),
122 preview: Schema.optional(Schema.String),
123 }),
124 ),
125 multiSelect: Schema.optional(Schema.Boolean),
126 allowCustomAnswer: Schema.optional(Schema.Boolean),
127 initialAnswer: Schema.optional(Schema.String),
128 required: Schema.optional(Schema.Boolean),
129});
130const PendingRequestTool = Tool.make("t3_pending_request", {
131 ...commandTool,
132 description:
133 "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.",
134 parameters: Schema.Struct({
135 action: Schema.Literals(["list", "read", "respond"]),
136 environmentId: OrchestratorMcpEnvironmentTarget,
137 threadId: Schema.optional(ThreadId),
138 requestId: Schema.optional(RuntimeRequestId),
139 answers: Schema.optional(ProviderUserInputAnswers),
140 }),
141 success: Schema.Union([
142 Schema.Struct({ requestIds: Schema.Array(RuntimeRequestId) }),
143 Schema.Struct({ requestId: RuntimeRequestId, questions: Schema.Array(question) }),
144 OrchestrationV2DispatchCommandResult,
145 ]),
146})
147 .annotate(Tool.Destructive, true)
148 .annotate(Tool.OpenWorld, true);
149​
150const threadConfiguration = Schema.Struct({
151 threadId: ThreadId,
152 modelSelection: ModelSelection,
153 runtimeMode: RuntimeMode,
154 interactionMode: ProviderInteractionMode,
155});
156const ThreadConfigureTool = Tool.make("t3_thread_configure", {
157 ...commandTool,
158 description:
159 "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`.",
160 parameters: Schema.Struct({
161 environmentId: OrchestratorMcpEnvironmentTarget,
162 threadId: Schema.optional(ThreadId),
163 modelSelection: Schema.optional(ModelSelection),
164 }),
165 success: Schema.Union([threadConfiguration, OrchestrationV2DispatchCommandResult]),
166 dependencies: [...commandTool.dependencies, ProviderRegistry.ProviderRegistry],
167}).annotate(Tool.Destructive, true);
168​
169const ThreadTransferTool = Tool.make("t3_thread_transfer", {
170 ...commandTool,
171 description:
172 "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.",
173 parameters: Schema.Struct({
174 action: Schema.Literals(["fork", "merge_back", "status"]),
175 environmentId: OrchestratorMcpEnvironmentTarget,
176 threadId: Schema.optional(ThreadId),
177 sourcePoint: Schema.optional(OrchestrationV2ThreadForkSourcePoint),
178 targetThreadId: Schema.optional(ThreadId),
179 title: Schema.optional(TrimmedNonEmptyString),
180 }),
181 success: Schema.Union([
182 Schema.Struct({ sequence: NonNegativeInt, targetThreadId: ThreadId }),
183 Schema.Struct({
184 transfers: Schema.Array(
185 Schema.Struct({
186 id: OrchestrationV2ContextTransfer.fields.id,
187 sourceThreadId: OrchestrationV2ContextTransfer.fields.sourceThreadId,
188 targetThreadId: OrchestrationV2ContextTransfer.fields.targetThreadId,
189 status: OrchestrationV2ContextTransfer.fields.status,
190 }),
191 ),
192 }),
193 ]),
194}).annotate(Tool.Destructive, true);
195​
196const ThreadSearchTool = Tool.make("t3_thread_search", {
197 ...commandTool,
198 description:
199 "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.",
200 parameters: Schema.Struct({
201 environmentId: OrchestratorMcpEnvironmentTarget,
202 ...OrchestrationSearchThreadsInput.fields,
203 projectId: Schema.optional(ProjectId),
204 }),
205 success: Schema.Struct({
206 currentThreadId: Schema.optional(Schema.NullOr(ThreadId)),
207 matches: Schema.Array(
208 Schema.Struct({
209 threadId: OrchestrationThreadSearchMatch.fields.threadId,
210 projectId: OrchestrationThreadSearchMatch.fields.projectId,
211 title: Schema.String,
212 link: Schema.optional(Schema.String),
213 source: Schema.Literals(["title", "user", "assistant"]),
214 snippet: Schema.optional(OrchestrationThreadSearchMatch.fields.snippet),
215 messageCreatedAt: Schema.optional(OrchestrationThreadSearchMatch.fields.messageCreatedAt),
216 }),
217 ),
218 }),
219 dependencies: [...commandTool.dependencies, ThreadSearch.ThreadSearch],
220})
221 .annotate(Tool.Readonly, true)
222 .annotate(Tool.Destructive, false);
223​
224const ScheduledTaskRunTool = Tool.make("run_scheduled_task_now", {
225 ...commandTool,
226 description:
227 "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.",
228 parameters: Schema.Struct({
229 environmentId: OrchestratorMcpEnvironmentTarget,
230 taskId: ScheduledTaskId,
231 }),
232 success: Schema.Struct({
233 taskId: ScheduledTaskId,
234 threadId: ScheduledTask.fields.threadId,
235 link: Schema.optional(Schema.String),
236 lastRunStatus: ScheduledTask.fields.lastRunStatus,
237 lastRunError: ScheduledTask.fields.lastRunError,
238 runCount: NonNegativeInt,
239 nextRunAt: ScheduledTask.fields.nextRunAt,
240 }),
241 dependencies: [...commandTool.dependencies, ScheduledTaskService.ScheduledTaskService],
242})
243 .annotate(Tool.Destructive, true)
244 .annotate(Tool.OpenWorld, true);
245​
246const SubmitOutputTool = Tool.make("t3_submit_output", {
247 ...commandTool,
248 description:
249 "Return this turn's result as data, when the message asked for one. `json`: JSON text matching the schema it gave. A rejection says what did not fit; fix it and call again.",
250 // Stored on the run, which every client of the thread holds.
251 parameters: Schema.Struct({ json: Schema.String.check(Schema.isMaxLength(200000)) }),
252 success: Schema.Struct({ runId: RunId }),
253})
254 // It writes only the caller's own run, so a read-only or plan-mode agent can still answer.
255 .annotate(Tool.Readonly, true)
256 .annotate(Tool.Destructive, false)
257 .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients);
258​
259export const ThreadToolkit = Toolkit.make(
260 ScheduledTaskRunTool,
261 ThreadSearchTool,
262 ThreadConfigureTool,
263 ThreadTransferTool,
264 ThreadOrganizeTool,
265 PendingRequestTool,
266 QueueTool,
267 SubmitOutputTool,
268);