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

tools.ts

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