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

tools.ts

279 lines10.5 KB

1import {
2 ScheduledTaskId,
3 ScheduledTask,
4 OrchestrationSearchThreadsInput,
5 OrchestrationSearchThreadsResult,
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} from "@t3tools/contracts";
21import * as Crypto from "effect/Crypto";
22import * as Schema from "effect/Schema";
23import { Tool, Toolkit } from "effect/unstable/ai";
24​
25import * as ThreadSearch from "../../../orchestration-v2/ThreadSearch.ts";
26import * as ScheduledTaskService from "../../../scheduledTasks/ScheduledTaskService.ts";
27import * as ThreadManagementService from "../../../orchestration-v2/ThreadManagementService.ts";
28import * as McpInvocationContext from "../../McpInvocationContext.ts";
29​
30const ThreadOrganizeTool = Tool.make("t3_thread_organize", {
31 description:
32 "Pin, snooze, settle, archive, or mark a thread unread in the calling project. Omit threadId for this thread. snooze requires snoozedUntil. Existing thread lifecycle rules apply; this does not schedule a future action.",
33 parameters: Schema.Struct({
34 threadId: Schema.optional(ThreadId),
35 action: Schema.Literals([
36 "pin",
37 "unpin",
38 "snooze",
39 "unsnooze",
40 "settle",
41 "unsettle",
42 "archive",
43 "unarchive",
44 "mark_unread",
45 ]),
46 snoozedUntil: Schema.optional(IsoDateTime),
47 }),
48 success: OrchestrationV2DispatchCommandResult,
49 failure: OrchestratorMcpFailure,
50 failureMode: "return" as const,
51 dependencies: [
52 McpInvocationContext.McpInvocationContext,
53 ThreadManagementService.ThreadManagementService,
54 Crypto.Crypto,
55 ],
56})
57 .annotate(Tool.Title, "Organize a thread")
58 .annotate(Tool.Destructive, true);
59​
60const queueTarget = { threadId: Schema.optional(ThreadId), queuedRunId: RunId };
61const commandTool = {
62 success: OrchestrationV2DispatchCommandResult,
63 failure: OrchestratorMcpFailure,
64 failureMode: "return" as const,
65 dependencies: [
66 McpInvocationContext.McpInvocationContext,
67 ThreadManagementService.ThreadManagementService,
68 Crypto.Crypto,
69 ],
70};
71const queueEntry = Schema.Struct({
72 queuedRunId: RunId,
73 text: Schema.String,
74 truncated: Schema.Boolean,
75});
76const QueueListTool = Tool.make("t3_queue_list", {
77 ...commandTool,
78 description:
79 "List queued messages in delivery order. Results are a live offset page; use t3_thread_read for full thread history.",
80 parameters: Schema.Struct({
81 threadId: Schema.optional(ThreadId),
82 cursor: Schema.optional(NonNegativeInt),
83 limit: Schema.optional(Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 100 }))),
84 }),
85 success: Schema.Struct({
86 items: Schema.Array(queueEntry),
87 nextCursor: Schema.NullOr(NonNegativeInt),
88 }),
89})
90 .annotate(Tool.Readonly, true)
91 .annotate(Tool.Destructive, false);
92const QueueReadTool = Tool.make("t3_queue_read", {
93 ...commandTool,
94 description: "Read up to 16,000 characters of a queued message in the calling project.",
95 parameters: Schema.Struct(queueTarget),
96 success: queueEntry,
97})
98 .annotate(Tool.Readonly, true)
99 .annotate(Tool.Destructive, false);
100const QueueEditTool = Tool.make("t3_queue_edit", {
101 ...commandTool,
102 description:
103 "Replace a queued message's text, preserving its attachments. The service rejects runs that are no longer queued.",
104 parameters: Schema.Struct({
105 ...queueTarget,
106 text: Schema.String.check(Schema.isMaxLength(100000)),
107 }),
108}).annotate(Tool.Destructive, true);
109const QueueCancelTool = Tool.make("t3_queue_cancel", {
110 ...commandTool,
111 description: "Cancel a queued run using the existing queue command.",
112 parameters: Schema.Struct(queueTarget),
113}).annotate(Tool.Destructive, true);
114const QueueReorderTool = Tool.make("t3_queue_reorder", {
115 ...commandTool,
116 description: "Move a queued run before another queued run, or to the end with beforeRunId=null.",
117 parameters: Schema.Struct({ ...queueTarget, beforeRunId: Schema.NullOr(RunId) }),
118}).annotate(Tool.Destructive, true);
119const QueuePromoteTool = Tool.make("t3_queue_promote_to_steer", {
120 ...commandTool,
121 description:
122 "Deliver a queued message as steering to the specified active run. Existing provider and run-state rules apply.",
123 parameters: Schema.Struct({ ...queueTarget, targetRunId: RunId }),
124}).annotate(Tool.Destructive, true);
125​
126const requestTarget = { threadId: Schema.optional(ThreadId), requestId: RuntimeRequestId };
127const question = Schema.Struct({
128 id: Schema.String,
129 header: Schema.String,
130 question: Schema.String,
131 options: Schema.Array(
132 Schema.Struct({
133 label: Schema.String,
134 description: Schema.String,
135 value: Schema.optional(Schema.String),
136 }),
137 ),
138 multiSelect: Schema.optional(Schema.Boolean),
139 allowCustomAnswer: Schema.optional(Schema.Boolean),
140 required: Schema.optional(Schema.Boolean),
141});
142const pendingRequest = Schema.Struct({
143 requestId: RuntimeRequestId,
144 questions: Schema.Array(question),
145});
146const PendingRequestListTool = Tool.make("t3_pending_request_list", {
147 ...commandTool,
148 description:
149 "List pending user questions in a thread in the calling project. Approval requests are not included.",
150 parameters: Schema.Struct({ threadId: Schema.optional(ThreadId) }),
151 success: Schema.Struct({ requestIds: Schema.Array(RuntimeRequestId) }),
152})
153 .annotate(Tool.Readonly, true)
154 .annotate(Tool.Destructive, false);
155const PendingRequestReadTool = Tool.make("t3_pending_request_read", {
156 ...commandTool,
157 description:
158 "Read a pending user question. Answer with t3_pending_request_respond; existing live or message response handling is used.",
159 parameters: Schema.Struct(requestTarget),
160 success: pendingRequest,
161})
162 .annotate(Tool.Readonly, true)
163 .annotate(Tool.Destructive, false);
164const PendingRequestRespondTool = Tool.make("t3_pending_request_respond", {
165 ...commandTool,
166 description:
167 "Answer a pending user-input request using the existing runtime response command. This cannot approve a permission request.",
168 parameters: Schema.Struct({ ...requestTarget, answers: ProviderUserInputAnswers }),
169})
170 .annotate(Tool.Destructive, true)
171 .annotate(Tool.OpenWorld, true);
172​
173const ThreadConfigurationTool = Tool.make("t3_thread_configuration", {
174 ...commandTool,
175 description:
176 "Read a thread's provider/model selection and modes in the calling project. orchestrator_capabilities lists available providers and models.",
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 this calling thread's provider, model and options with the existing selection command. This does not change permission modes or other threads. Use orchestrator_capabilities to choose a selection.",
191 parameters: Schema.Struct({ modelSelection: ModelSelection }),
192}).annotate(Tool.Destructive, true);
193​
194const transferResult = Schema.Struct({ sequence: NonNegativeInt, targetThreadId: ThreadId });
195const ThreadForkTool = Tool.make("t3_thread_fork", {
196 ...commandTool,
197 description:
198 "Fork this thread from a stable run or checkpoint using the existing fork command. The fork inherits the source configuration. Acceptance does not mean a provider turn has completed.",
199 parameters: Schema.Struct({
200 sourcePoint: OrchestrationV2ThreadForkSourcePoint,
201 title: Schema.optional(TrimmedNonEmptyString),
202 }),
203 success: transferResult,
204}).annotate(Tool.Destructive, true);
205const ThreadMergeBackTool = Tool.make("t3_thread_merge_back", {
206 ...commandTool,
207 description:
208 "Merge context from this thread back to a related thread in the same project. Existing lineage and transfer rules apply.",
209 parameters: Schema.Struct({
210 targetThreadId: ThreadId,
211 sourcePoint: OrchestrationV2ThreadForkSourcePoint,
212 }),
213 success: transferResult,
214}).annotate(Tool.Destructive, true);
215const ThreadTransfersTool = Tool.make("t3_thread_transfers", {
216 ...commandTool,
217 description: "Read context transfer status for a thread in the calling project.",
218 parameters: Schema.Struct({ threadId: Schema.optional(ThreadId) }),
219 success: Schema.Struct({
220 transfers: Schema.Array(
221 Schema.Struct({
222 id: OrchestrationV2ContextTransfer.fields.id,
223 sourceThreadId: OrchestrationV2ContextTransfer.fields.sourceThreadId,
224 targetThreadId: OrchestrationV2ContextTransfer.fields.targetThreadId,
225 status: OrchestrationV2ContextTransfer.fields.status,
226 }),
227 ),
228 }),
229})
230 .annotate(Tool.Readonly, true)
231 .annotate(Tool.Destructive, false);
232​
233const ThreadSearchTool = Tool.make("t3_thread_search", {
234 ...commandTool,
235 description:
236 "Search active thread titles and content with the app's existing bounded search. Returns matches in the calling project from the global top matches; other-project matches are omitted, so this may return fewer than limit. No pagination or exhaustive-result guarantee.",
237 parameters: OrchestrationSearchThreadsInput,
238 success: OrchestrationSearchThreadsResult,
239 dependencies: [...commandTool.dependencies, ThreadSearch.ThreadSearch],
240})
241 .annotate(Tool.Readonly, true)
242 .annotate(Tool.Destructive, false);
243​
244const ScheduledTaskRunTool = Tool.make("run_scheduled_task_now", {
245 ...commandTool,
246 description:
247 "Run a scheduled task in the calling project now through the existing scheduler. Requires a full-access/default caller. Each call is a new manual run; completion means dispatch/bookkeeping completed, not that the provider turn finished.",
248 parameters: Schema.Struct({ taskId: ScheduledTaskId }),
249 success: Schema.Struct({
250 taskId: ScheduledTaskId,
251 threadId: ScheduledTask.fields.threadId,
252 lastRunStatus: ScheduledTask.fields.lastRunStatus,
253 runCount: NonNegativeInt,
254 nextRunAt: ScheduledTask.fields.nextRunAt,
255 }),
256 dependencies: [...commandTool.dependencies, ScheduledTaskService.ScheduledTaskService],
257})
258 .annotate(Tool.Destructive, true)
259 .annotate(Tool.OpenWorld, true);
260​
261export const ThreadToolkit = Toolkit.make(
262 ScheduledTaskRunTool,
263 ThreadSearchTool,
264 ThreadForkTool,
265 ThreadMergeBackTool,
266 ThreadTransfersTool,
267 ThreadConfigurationTool,
268 ThreadConfigureTool,
269 PendingRequestListTool,
270 PendingRequestReadTool,
271 PendingRequestRespondTool,
272 ThreadOrganizeTool,
273 QueueListTool,
274 QueueReadTool,
275 QueueEditTool,
276 QueueCancelTool,
277 QueueReorderTool,
278 QueuePromoteTool,
279);