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

tools.ts

299 lines10.8 KB

1import {
2 ScheduledTaskId,
3 ScheduledTask,
4 OrchestrationSearchThreadsInput,
5 OrchestrationThreadSearchMatch,
6 OrchestrationV2ThreadForkSourcePoint,
7 OrchestrationV2ContextTransfer,
8 TrimmedNonEmptyString,
9 ModelSelection,
10 RuntimeMode,
11 ProviderInteractionMode,
12 RuntimeRequestId,
13 ProviderUserInputAnswers,
14 IsoDateTime,
15 OrchestratorMcpFailure,
16 OrchestrationV2DispatchCommandResult,
17 ThreadId,
18 RunId,
19 NonNegativeInt,
20 ProjectId,
21} from "@t3tools/contracts";
22import * as Crypto from "effect/Crypto";
23import * as Schema from "effect/Schema";
24import { Tool, Toolkit } from "effect/ai";
25​
26import * as ThreadSearch from "../../../orchestration-v2/ThreadSearch.ts";
27import * as ScheduledTaskService from "../../../scheduledTasks/ScheduledTaskService.ts";
28import * as ThreadManagementService from "../../../orchestration-v2/ThreadManagementService.ts";
29import * as McpInvocationContext from "../../McpInvocationContext.ts";
30​
31const ThreadOrganizeTool = Tool.make("t3_thread_organize", {
32 description:
33 "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.",
34 parameters: Schema.Struct({
35 threadId: Schema.optional(ThreadId),
36 action: Schema.Literals([
37 "pin",
38 "unpin",
39 "snooze",
40 "unsnooze",
41 "settle",
42 "unsettle",
43 "archive",
44 "unarchive",
45 "mark_unread",
46 ]),
47 snoozedUntil: Schema.optional(IsoDateTime),
48 }),
49 success: Schema.Union([
50 OrchestrationV2DispatchCommandResult,
51 Schema.Struct({ settlesWhenTurnEnds: Schema.Literal(true) }),
52 ]),
53 failure: OrchestratorMcpFailure,
54 failureMode: "return" as const,
55 dependencies: [
56 McpInvocationContext.McpInvocationContext,
57 ThreadManagementService.ThreadManagementService,
58 Crypto.Crypto,
59 ],
60})
61 .annotate(Tool.Title, "Organize a thread")
62 .annotate(Tool.Destructive, true);
63​
64const queueTarget = { threadId: Schema.optional(ThreadId), queuedRunId: RunId };
65const commandTool = {
66 success: OrchestrationV2DispatchCommandResult,
67 failure: OrchestratorMcpFailure,
68 failureMode: "return" as const,
69 dependencies: [
70 McpInvocationContext.McpInvocationContext,
71 ThreadManagementService.ThreadManagementService,
72 Crypto.Crypto,
73 ],
74};
75const queueEntry = Schema.Struct({
76 queuedRunId: RunId,
77 text: Schema.String,
78 truncated: Schema.Boolean,
79});
80const QueueListTool = Tool.make("t3_queue_list", {
81 ...commandTool,
82 description: "List queued messages in send order. For full history, use `t3_thread_read`.",
83 parameters: Schema.Struct({
84 threadId: Schema.optional(ThreadId),
85 cursor: Schema.optional(NonNegativeInt),
86 limit: Schema.optional(Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 100 }))),
87 }),
88 success: Schema.Struct({
89 items: Schema.Array(queueEntry),
90 nextCursor: Schema.NullOr(NonNegativeInt),
91 }),
92})
93 .annotate(Tool.Readonly, true)
94 .annotate(Tool.Destructive, false);
95const QueueReadTool = Tool.make("t3_queue_read", {
96 ...commandTool,
97 description: "Read up to 16,000 chars of one queued message. Omit `threadId` for this thread.",
98 parameters: Schema.Struct(queueTarget),
99 success: queueEntry,
100})
101 .annotate(Tool.Readonly, true)
102 .annotate(Tool.Destructive, false);
103const QueueEditTool = Tool.make("t3_queue_edit", {
104 ...commandTool,
105 description:
106 "Replace one queued message's text. Attachments stay. Fails if the run is no longer queued.",
107 parameters: Schema.Struct({
108 ...queueTarget,
109 text: Schema.String.check(Schema.isMaxLength(100000)),
110 }),
111}).annotate(Tool.Destructive, true);
112const QueueCancelTool = Tool.make("t3_queue_cancel", {
113 ...commandTool,
114 description: "Cancel one queued run.",
115 parameters: Schema.Struct(queueTarget),
116}).annotate(Tool.Destructive, true);
117const QueueReorderTool = Tool.make("t3_queue_reorder", {
118 ...commandTool,
119 description: "Move one queued run before another, or to the end with `beforeRunId=null`.",
120 parameters: Schema.Struct({ ...queueTarget, beforeRunId: Schema.NullOr(RunId) }),
121}).annotate(Tool.Destructive, true);
122const QueuePromoteTool = Tool.make("t3_queue_promote_to_steer", {
123 ...commandTool,
124 description: "Send one queued message now as steering to the given active run.",
125 parameters: Schema.Struct({ ...queueTarget, targetRunId: RunId }),
126}).annotate(Tool.Destructive, true);
127​
128const requestTarget = { threadId: Schema.optional(ThreadId), requestId: RuntimeRequestId };
129const question = Schema.Struct({
130 id: Schema.String,
131 header: Schema.String,
132 question: Schema.String,
133 options: Schema.Array(
134 Schema.Struct({
135 label: Schema.String,
136 description: Schema.String,
137 value: Schema.optional(Schema.String),
138 }),
139 ),
140 multiSelect: Schema.optional(Schema.Boolean),
141 allowCustomAnswer: Schema.optional(Schema.Boolean),
142 initialAnswer: Schema.optional(Schema.String),
143 required: Schema.optional(Schema.Boolean),
144});
145const pendingRequest = Schema.Struct({
146 requestId: RuntimeRequestId,
147 questions: Schema.Array(question),
148});
149const PendingRequestListTool = Tool.make("t3_pending_request_list", {
150 ...commandTool,
151 description:
152 "List pending user questions in a thread. Omit `threadId` for this thread. Approvals are not listed.",
153 parameters: Schema.Struct({ threadId: Schema.optional(ThreadId) }),
154 success: Schema.Struct({ requestIds: Schema.Array(RuntimeRequestId) }),
155})
156 .annotate(Tool.Readonly, true)
157 .annotate(Tool.Destructive, false);
158const PendingRequestReadTool = Tool.make("t3_pending_request_read", {
159 ...commandTool,
160 description: "Read one pending user question. Answer it with `t3_pending_request_respond`.",
161 parameters: Schema.Struct(requestTarget),
162 success: pendingRequest,
163})
164 .annotate(Tool.Readonly, true)
165 .annotate(Tool.Destructive, false);
166const PendingRequestRespondTool = Tool.make("t3_pending_request_respond", {
167 ...commandTool,
168 description: "Answer one pending user question. This cannot approve a permission request.",
169 parameters: Schema.Struct({ ...requestTarget, answers: ProviderUserInputAnswers }),
170})
171 .annotate(Tool.Destructive, true)
172 .annotate(Tool.OpenWorld, true);
173​
174const ThreadConfigurationTool = Tool.make("t3_thread_configuration", {
175 ...commandTool,
176 description: "Read a thread's provider, model, and modes. Omit `threadId` for this thread.",
177 parameters: Schema.Struct({ threadId: Schema.optional(ThreadId) }),
178 success: Schema.Struct({
179 threadId: ThreadId,
180 modelSelection: ModelSelection,
181 runtimeMode: RuntimeMode,
182 interactionMode: ProviderInteractionMode,
183 }),
184})
185 .annotate(Tool.Readonly, true)
186 .annotate(Tool.Destructive, false);
187const ThreadConfigureTool = Tool.make("t3_thread_configure", {
188 ...commandTool,
189 description:
190 "Set a thread's provider, model, and options. Omit `threadId` for this thread. Permission modes stay. Values from `orchestrator_capabilities`.",
191 parameters: Schema.Struct({
192 threadId: Schema.optional(ThreadId),
193 modelSelection: ModelSelection,
194 }),
195}).annotate(Tool.Destructive, true);
196​
197const transferResult = Schema.Struct({ sequence: NonNegativeInt, targetThreadId: ThreadId });
198const ThreadForkTool = Tool.make("t3_thread_fork", {
199 ...commandTool,
200 description:
201 "Fork a thread from a stable run or checkpoint. Omit `threadId` for this thread. Keeps source setup. No turn has run yet.",
202 parameters: Schema.Struct({
203 threadId: Schema.optional(ThreadId),
204 sourcePoint: OrchestrationV2ThreadForkSourcePoint,
205 title: Schema.optional(TrimmedNonEmptyString),
206 }),
207 success: transferResult,
208}).annotate(Tool.Destructive, true);
209const ThreadMergeBackTool = Tool.make("t3_thread_merge_back", {
210 ...commandTool,
211 description:
212 "Merge context from one thread into a related thread in the same project. Omit `sourceThreadId` for this thread.",
213 parameters: Schema.Struct({
214 sourceThreadId: Schema.optional(ThreadId),
215 targetThreadId: ThreadId,
216 sourcePoint: OrchestrationV2ThreadForkSourcePoint,
217 }),
218 success: transferResult,
219}).annotate(Tool.Destructive, true);
220const ThreadTransfersTool = Tool.make("t3_thread_transfers", {
221 ...commandTool,
222 description: "Read context transfer status for a thread. Omit `threadId` for this thread.",
223 parameters: Schema.Struct({ threadId: Schema.optional(ThreadId) }),
224 success: Schema.Struct({
225 transfers: Schema.Array(
226 Schema.Struct({
227 id: OrchestrationV2ContextTransfer.fields.id,
228 sourceThreadId: OrchestrationV2ContextTransfer.fields.sourceThreadId,
229 targetThreadId: OrchestrationV2ContextTransfer.fields.targetThreadId,
230 status: OrchestrationV2ContextTransfer.fields.status,
231 }),
232 ),
233 }),
234})
235 .annotate(Tool.Readonly, true)
236 .annotate(Tool.Destructive, false);
237​
238const ThreadSearchTool = Tool.make("t3_thread_search", {
239 ...commandTool,
240 description:
241 "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.",
242 parameters: Schema.Struct({
243 ...OrchestrationSearchThreadsInput.fields,
244 projectId: Schema.optional(ProjectId),
245 }),
246 success: Schema.Struct({
247 matches: Schema.Array(
248 Schema.Struct({
249 threadId: OrchestrationThreadSearchMatch.fields.threadId,
250 projectId: OrchestrationThreadSearchMatch.fields.projectId,
251 title: Schema.String,
252 link: Schema.optional(Schema.String),
253 source: Schema.Literals(["title", "user", "assistant"]),
254 snippet: Schema.optional(OrchestrationThreadSearchMatch.fields.snippet),
255 messageCreatedAt: Schema.optional(OrchestrationThreadSearchMatch.fields.messageCreatedAt),
256 }),
257 ),
258 }),
259 dependencies: [...commandTool.dependencies, ThreadSearch.ThreadSearch],
260})
261 .annotate(Tool.Readonly, true)
262 .annotate(Tool.Destructive, false);
263​
264const ScheduledTaskRunTool = Tool.make("run_scheduled_task_now", {
265 ...commandTool,
266 description:
267 "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.",
268 parameters: Schema.Struct({ taskId: ScheduledTaskId }),
269 success: Schema.Struct({
270 taskId: ScheduledTaskId,
271 threadId: ScheduledTask.fields.threadId,
272 lastRunStatus: ScheduledTask.fields.lastRunStatus,
273 runCount: NonNegativeInt,
274 nextRunAt: ScheduledTask.fields.nextRunAt,
275 }),
276 dependencies: [...commandTool.dependencies, ScheduledTaskService.ScheduledTaskService],
277})
278 .annotate(Tool.Destructive, true)
279 .annotate(Tool.OpenWorld, true);
280​
281export const ThreadToolkit = Toolkit.make(
282 ScheduledTaskRunTool,
283 ThreadSearchTool,
284 ThreadForkTool,
285 ThreadMergeBackTool,
286 ThreadTransfersTool,
287 ThreadConfigurationTool,
288 ThreadConfigureTool,
289 PendingRequestListTool,
290 PendingRequestReadTool,
291 PendingRequestRespondTool,
292 ThreadOrganizeTool,
293 QueueListTool,
294 QueueReadTool,
295 QueueEditTool,
296 QueueCancelTool,
297 QueueReorderTool,
298 QueuePromoteTool,
299);