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

tools.ts

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