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

tools.ts

288 lines10.9 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 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 requires snoozedUntil. Existing thread lifecycle rules apply; this does not schedule a future action.",
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: OrchestrationV2DispatchCommandResult,
50 failure: OrchestratorMcpFailure,
51 failureMode: "return" as const,
52 dependencies: [
53 McpInvocationContext.McpInvocationContext,
54 ThreadManagementService.ThreadManagementService,
55 Crypto.Crypto,
56 ],
57})
58 .annotate(Tool.Title, "Organize a thread")
59 .annotate(Tool.Destructive, true);
60​
61const queueTarget = { threadId: Schema.optional(ThreadId), queuedRunId: RunId };
62const commandTool = {
63 success: OrchestrationV2DispatchCommandResult,
64 failure: OrchestratorMcpFailure,
65 failureMode: "return" as const,
66 dependencies: [
67 McpInvocationContext.McpInvocationContext,
68 ThreadManagementService.ThreadManagementService,
69 Crypto.Crypto,
70 ],
71};
72const queueEntry = Schema.Struct({
73 queuedRunId: RunId,
74 text: Schema.String,
75 truncated: Schema.Boolean,
76});
77const QueueListTool = Tool.make("t3_queue_list", {
78 ...commandTool,
79 description:
80 "List queued messages in delivery order. Results are a live offset page; use t3_thread_read for full thread history.",
81 parameters: Schema.Struct({
82 threadId: Schema.optional(ThreadId),
83 cursor: Schema.optional(NonNegativeInt),
84 limit: Schema.optional(Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 100 }))),
85 }),
86 success: Schema.Struct({
87 items: Schema.Array(queueEntry),
88 nextCursor: Schema.NullOr(NonNegativeInt),
89 }),
90})
91 .annotate(Tool.Readonly, true)
92 .annotate(Tool.Destructive, false);
93const QueueReadTool = Tool.make("t3_queue_read", {
94 ...commandTool,
95 description: "Read up to 16,000 characters of a queued message. Omit threadId for this thread.",
96 parameters: Schema.Struct(queueTarget),
97 success: queueEntry,
98})
99 .annotate(Tool.Readonly, true)
100 .annotate(Tool.Destructive, false);
101const QueueEditTool = Tool.make("t3_queue_edit", {
102 ...commandTool,
103 description:
104 "Replace a queued message's text, preserving its attachments. The service rejects runs that are no longer queued.",
105 parameters: Schema.Struct({
106 ...queueTarget,
107 text: Schema.String.check(Schema.isMaxLength(100000)),
108 }),
109}).annotate(Tool.Destructive, true);
110const QueueCancelTool = Tool.make("t3_queue_cancel", {
111 ...commandTool,
112 description: "Cancel a queued run using the existing queue command.",
113 parameters: Schema.Struct(queueTarget),
114}).annotate(Tool.Destructive, true);
115const QueueReorderTool = Tool.make("t3_queue_reorder", {
116 ...commandTool,
117 description: "Move a queued run before another queued run, or to the end with beforeRunId=null.",
118 parameters: Schema.Struct({ ...queueTarget, beforeRunId: Schema.NullOr(RunId) }),
119}).annotate(Tool.Destructive, true);
120const QueuePromoteTool = Tool.make("t3_queue_promote_to_steer", {
121 ...commandTool,
122 description:
123 "Deliver a queued message as steering to the specified active run. Existing provider and run-state rules apply.",
124 parameters: Schema.Struct({ ...queueTarget, targetRunId: RunId }),
125}).annotate(Tool.Destructive, true);
126​
127const requestTarget = { threadId: Schema.optional(ThreadId), requestId: RuntimeRequestId };
128const question = Schema.Struct({
129 id: Schema.String,
130 header: Schema.String,
131 question: Schema.String,
132 options: Schema.Array(
133 Schema.Struct({
134 label: Schema.String,
135 description: Schema.String,
136 value: Schema.optional(Schema.String),
137 }),
138 ),
139 multiSelect: Schema.optional(Schema.Boolean),
140 allowCustomAnswer: Schema.optional(Schema.Boolean),
141 required: Schema.optional(Schema.Boolean),
142});
143const pendingRequest = Schema.Struct({
144 requestId: RuntimeRequestId,
145 questions: Schema.Array(question),
146});
147const PendingRequestListTool = Tool.make("t3_pending_request_list", {
148 ...commandTool,
149 description:
150 "List pending user questions in a thread. Omit threadId for this thread. Approval requests are not included.",
151 parameters: Schema.Struct({ threadId: Schema.optional(ThreadId) }),
152 success: Schema.Struct({ requestIds: Schema.Array(RuntimeRequestId) }),
153})
154 .annotate(Tool.Readonly, true)
155 .annotate(Tool.Destructive, false);
156const PendingRequestReadTool = Tool.make("t3_pending_request_read", {
157 ...commandTool,
158 description:
159 "Read a pending user question. Answer with t3_pending_request_respond; existing live or message response handling is used.",
160 parameters: Schema.Struct(requestTarget),
161 success: pendingRequest,
162})
163 .annotate(Tool.Readonly, true)
164 .annotate(Tool.Destructive, false);
165const PendingRequestRespondTool = Tool.make("t3_pending_request_respond", {
166 ...commandTool,
167 description:
168 "Answer a pending user-input request using the existing runtime response command. 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:
177 "Read a thread's provider/model selection and modes. Omit threadId for this thread. orchestrator_capabilities lists available providers and models.",
178 parameters: Schema.Struct({ threadId: Schema.optional(ThreadId) }),
179 success: Schema.Struct({
180 threadId: ThreadId,
181 modelSelection: ModelSelection,
182 runtimeMode: RuntimeMode,
183 interactionMode: ProviderInteractionMode,
184 }),
185})
186 .annotate(Tool.Readonly, true)
187 .annotate(Tool.Destructive, false);
188const ThreadConfigureTool = Tool.make("t3_thread_configure", {
189 ...commandTool,
190 description:
191 "Set a thread's provider, model and options with the existing selection command. Omit threadId for this thread. This does not change permission modes. Use orchestrator_capabilities to choose a selection.",
192 parameters: Schema.Struct({
193 threadId: Schema.optional(ThreadId),
194 modelSelection: ModelSelection,
195 }),
196}).annotate(Tool.Destructive, true);
197​
198const transferResult = Schema.Struct({ sequence: NonNegativeInt, targetThreadId: ThreadId });
199const ThreadForkTool = Tool.make("t3_thread_fork", {
200 ...commandTool,
201 description:
202 "Fork a thread from a stable run or checkpoint using the existing fork command. Omit threadId to fork this thread. The fork inherits the source configuration. Acceptance does not mean a provider turn has completed.",
203 parameters: Schema.Struct({
204 threadId: Schema.optional(ThreadId),
205 sourcePoint: OrchestrationV2ThreadForkSourcePoint,
206 title: Schema.optional(TrimmedNonEmptyString),
207 }),
208 success: transferResult,
209}).annotate(Tool.Destructive, true);
210const ThreadMergeBackTool = Tool.make("t3_thread_merge_back", {
211 ...commandTool,
212 description:
213 "Merge context from a thread back to a related thread in the same project. Omit sourceThreadId to merge from this thread. Existing lineage and transfer rules apply.",
214 parameters: Schema.Struct({
215 sourceThreadId: Schema.optional(ThreadId),
216 targetThreadId: ThreadId,
217 sourcePoint: OrchestrationV2ThreadForkSourcePoint,
218 }),
219 success: transferResult,
220}).annotate(Tool.Destructive, true);
221const ThreadTransfersTool = Tool.make("t3_thread_transfers", {
222 ...commandTool,
223 description: "Read context transfer status for a thread. Omit threadId for this thread.",
224 parameters: Schema.Struct({ threadId: Schema.optional(ThreadId) }),
225 success: Schema.Struct({
226 transfers: Schema.Array(
227 Schema.Struct({
228 id: OrchestrationV2ContextTransfer.fields.id,
229 sourceThreadId: OrchestrationV2ContextTransfer.fields.sourceThreadId,
230 targetThreadId: OrchestrationV2ContextTransfer.fields.targetThreadId,
231 status: OrchestrationV2ContextTransfer.fields.status,
232 }),
233 ),
234 }),
235})
236 .annotate(Tool.Readonly, true)
237 .annotate(Tool.Destructive, false);
238​
239const ThreadSearchTool = Tool.make("t3_thread_search", {
240 ...commandTool,
241 description:
242 "Search active thread titles and content with the app's existing bounded search. Matches are limited to one project (projectId, else the calling thread's project) out of the global top matches, so this may return fewer than limit. A caller outside a T3 thread that omits projectId searches every project. No pagination or exhaustive-result guarantee.",
243 parameters: Schema.Struct({
244 ...OrchestrationSearchThreadsInput.fields,
245 projectId: Schema.optional(ProjectId),
246 }),
247 success: OrchestrationSearchThreadsResult,
248 dependencies: [...commandTool.dependencies, ThreadSearch.ThreadSearch],
249})
250 .annotate(Tool.Readonly, true)
251 .annotate(Tool.Destructive, false);
252​
253const ScheduledTaskRunTool = Tool.make("run_scheduled_task_now", {
254 ...commandTool,
255 description:
256 "Run a scheduled task 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.",
257 parameters: Schema.Struct({ taskId: ScheduledTaskId }),
258 success: Schema.Struct({
259 taskId: ScheduledTaskId,
260 threadId: ScheduledTask.fields.threadId,
261 lastRunStatus: ScheduledTask.fields.lastRunStatus,
262 runCount: NonNegativeInt,
263 nextRunAt: ScheduledTask.fields.nextRunAt,
264 }),
265 dependencies: [...commandTool.dependencies, ScheduledTaskService.ScheduledTaskService],
266})
267 .annotate(Tool.Destructive, true)
268 .annotate(Tool.OpenWorld, true);
269​
270export const ThreadToolkit = Toolkit.make(
271 ScheduledTaskRunTool,
272 ThreadSearchTool,
273 ThreadForkTool,
274 ThreadMergeBackTool,
275 ThreadTransfersTool,
276 ThreadConfigurationTool,
277 ThreadConfigureTool,
278 PendingRequestListTool,
279 PendingRequestReadTool,
280 PendingRequestRespondTool,
281 ThreadOrganizeTool,
282 QueueListTool,
283 QueueReadTool,
284 QueueEditTool,
285 QueueCancelTool,
286 QueueReorderTool,
287 QueuePromoteTool,
288);